-
Notifications
You must be signed in to change notification settings - Fork 28
/
test4.py
156 lines (123 loc) · 4.58 KB
/
test4.py
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
"""
Python Singleton with parameters (so the same parameters get you the same object)
with support to default arguments and passing arguments as kwargs (but no support for pure kwargs).
And implementation for MongoClient class from pymongo package.
"""
# from pymongo import MongoClient
import inspect
# class Singleton(type):
# """ Simple Singleton that keep only one value for all instances
# """
# def __init__(cls, name, bases, dic):
# super(Singleton, cls).__init__(name, bases, dic)
# cls.instance = None
# def __call__(cls, *args, **kwargs):
# if cls.instance is None:
# cls.instance = super(Singleton, cls).__call__(*args, **kwargs)
# return cls.instance
class SingletonArgs(type):
""" Singleton that keep single instance for single set of arguments. E.g.:
assert SingletonArgs('spam') is not SingletonArgs('eggs')
assert SingletonArgs('spam') is SingletonArgs('spam')
Only supports hashable arguments.
"""
_instances = {}
# _init = {}
# def __init__(cls, name, bases, dct):
# pass
# cls._init[cls] = cls.get('__init__', None)
# cls._init[cls] = cls.__init__
# parents = inspect.getmro(cls)
# for parent in parents:
# if parent.__init__:
# cls._init[cls] = parent.__init__
# break
# print('***:', cls._init)
def __call__(cls, *args, **kwargs):
# init = cls._init[cls]
# if init is not None:
# # print('-----')
# # ig = inspect.getcallargs(object.__init__, None, *args, **kwargs)
# # print(ig)
# # print(ig.items())
# key = (cls, frozenset(inspect.getcallargs(object.__init__, None, *args, **kwargs).items()))
# else:
# key = cls
key = (cls, frozenset(inspect.getcallargs(cls.__init__, None, *args, **kwargs).items()))
if key not in cls._instances:
cls._instances[key] = super(SingletonArgs, cls).__call__(*args, **kwargs)
return cls._instances[key]
# class SingletonMongoClient(object):
# """ Class based on Singleton type to work with MongoDB connections
# """
# __metaclass__ = SingletonArgs
# def __init__(self, url, db_name=None):
# if db_name:
# self.connection = MongoClient(url)[db_name]
# else:
# self.connection = MongoClient(url).get_default_database()
# def connection(self):
# return self.connection
# def db_init(db_name=None):
# url = 'mongodb://localhost:27017/'
# c = SingletonMongoClient(url, db_name).connection
# return c
def tests():
# class A(object, metaclass=SingletonArgs):
# FOO = 'bar'
# # pass
# assert A() is A()
class B(metaclass=SingletonArgs):
def __init__(self, key):
self.key = key
assert B('key1') is B('key1')
assert B('key1') is not B('key2')
class C(metaclass=SingletonArgs):
def __init__(self, key=None):
self.key = key
assert C() is C()
assert C() is C(None)
assert C(None) is C(key=None)
assert C() is C(key=None)
assert C() is not C('key')
assert C('key') is C('key')
assert C('key') is C(key='key')
assert C('key1') is not C(key='key2')
assert C(key='key1') is not C(key='key2')
class D(metaclass=SingletonArgs):
def __init__(self):
pass
class E(metaclass=SingletonArgs):
def __init__(self):
pass
assert D() is not E()
class F(metaclass=SingletonArgs):
def __init__(self, k1, k2=None, **kwargs):
self.k1 = k1
self.k2 = k2
assert F('k1') is F('k1', None)
assert F('k1') is not F('k2', None)
assert F('k1') is F('k1', k2 = None)
assert F('k1') is F(k2 = None, k1='k1')
assert F(k1='k1',k2 = None) is F(k2 = None, k1='k1')
class H(metaclass=SingletonArgs):
def __init__(self, k1=None, k2=None):
self.k1 = k1
self.k2 = k2
assert H('k1') is H('k1', None)
assert H('k1') is not H('k2', None)
assert H('k1') is H('k1', k2 = None)
assert H('k1') is H(k2 = None, k1='k1')
assert H(k1='k1',k2 = None) is H(k2 = None, k1='k1')
class I(H):
pass
# def __init__(self, k1=None, k2=None):
# self.k1 = k1
# self.k2 = k2
assert I('k1') is I('k1', None)
assert I('k1') is not I('k2', None)
assert I('k1') is I('k1', k2 = None)
assert I('k1') is I(k2 = None, k1='k1')
assert I(k1='k1',k2 = None) is I(k2 = None, k1='k1')
if __name__ == "__main__":
tests()