import sys

_caches = []


class _Cache(dict):
	"""A dict() subclass that appends a self-reference to _caches"""
	def __init__(self, *args, **kwargs):
		super(_Cache, self).__init__(*args, **kwargs)
		_caches.append(self)


"""
cache decorator
Caches input and output of a function. If arguments are passed to
the decorator, take those as key for the cache, otherwise the
function arguments.
"""
def cache(*keys):
	def cacheWrapper(func, localCache = _Cache()):
		def cachedFuncWrapper(*args, **kwargs):
			if keys:
				key = keys
			else:
				# treat all collections in kwargs as tuples for hashing purposes
				values = list(kwargs.values())
				for i in range(len(values)):
					try:
						values[i] = tuple(values[i])
					except TypeError:
						pass
				key = args + tuple(values) + tuple(sys.argv)

			if key not in localCache:
				localCache[key] = func(*args, **kwargs)

			return localCache[key]
		return cachedFuncWrapper
	return cacheWrapper

def clearAllCaches():
	for cache in _caches:
		cache.clear()

@@ -3,16 +3,44 @@
 port sys
+%0Afrom functools import wraps
 %0A%0A_cache
@@ -245,20 +245,38 @@
 self)%0A%0A%0A
+def cache(*keys):%0A%09
 %22%22%22
-%0A
 cache de
@@ -283,16 +283,18 @@
 corator%0A
+%0A%09
 Caches i
@@ -351,16 +351,17 @@
 ssed to%0A
+%09
 the deco
@@ -402,11 +402,11 @@
 ache
-, o
+. O
 ther
@@ -414,12 +414,17 @@
 ise 
+use 
 the%0A
+%09
 func
@@ -441,31 +441,35 @@
 ents
-.%0A%22%22%22%0Adef cache(*keys):
+ and sys.argv as key.%0A%0A%09%22%22%22
 %0A%09de
@@ -487,18 +487,21 @@
 per(func
-, 
+):%0A%09%09
 localCac
@@ -513,18 +513,32 @@
 _Cache()
+%0A%0A%09%09@wraps(func
 )
-:
 %0A%09%09def c
@@ -594,20 +594,25 @@
 %09%09key = 
+str(
 keys
+)
 %0A%09%09%09else
@@ -621,258 +621,43 @@
 %09%09%09%09
-# treat all collections in kwargs as tuples for hashing purposes%0A%09%09%09%09values = list(kwargs.values())%0A%09%09%09%09for i in range(len(values)):%0A%09%09%09%09%09try:%0A%09%09%09%09%09%09values%5Bi%5D = tuple(values%5Bi%5D)%0A%09%09%09%09%09except TypeError:%0A%09%09%09%09%09%09pass%0A%09%09%09%09key = args + tuple(values) + tuple
+key = str(args) + str(kwargs) + str
 (sys
@@ -737,17 +737,16 @@
 kwargs)%0A
-%0A
 %09%09%09retur
@@ -790,16 +790,17 @@
 Wrapper%0A
+%0A
 %09return 
@@ -813,16 +813,17 @@
 rapper%0A%0A
+%0A
 def clea

Refactor caches() decorator