forked from cool-RR/python_toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcached_type.py
More file actions
62 lines (46 loc) · 1.91 KB
/
cached_type.py
File metadata and controls
62 lines (46 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# Copyright 2009-2017 Ram Rachum.
# This program is distributed under the MIT license.
'''
Defines the `CachedType` metaclass.
See its documentation for more details.
'''
from python_toolbox.sleek_reffing import SleekCallArgs
class SelfPlaceholder:
'''Placeholder for `self` when storing call-args.'''
class CachedType(type):
'''
A metaclass for sharing instances.
For example, if you have a class like this:
class Grokker(object, metaclass=caching.CachedType):
def __init__(self, a, b=2):
self.a = a
self.b = b
Then all the following calls would result in just one instance:
Grokker(1) is Grokker(1, 2) is Grokker(b=2, a=1) is Grokker(1, **{})
This metaclass understands keyword arguments.
All the arguments are sleekreffed to prevent memory leaks. Sleekref is a
variation of weakref. Sleekref is when you try to weakref an object, but if
it's non-weakreffable, like a `list` or a `dict`, you maintain a normal,
strong reference to it. (See documentation of
`python_toolbox.sleek_reffing` for more details.) Thanks to sleekreffing
you can avoid memory leaks when using weakreffable arguments, but if you
ever want to use non-weakreffable arguments you are still able to.
(Assuming you don't mind the memory leaks.)
'''
def __new__(mcls, *args, **kwargs):
result = super().__new__(mcls, *args, **kwargs)
result.__cache = {}
return result
def __call__(cls, *args, **kwargs):
sleek_call_args = SleekCallArgs(
cls.__cache,
cls.__init__,
*((SelfPlaceholder,) + args),
**kwargs
)
try:
return cls.__cache[sleek_call_args]
except KeyError:
cls.__cache[sleek_call_args] = value = \
super().__call__(*args, **kwargs)
return value