-
Notifications
You must be signed in to change notification settings - Fork 212
/
Copy pathnew_vs_init.py
165 lines (120 loc) · 4.25 KB
/
new_vs_init.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
157
158
159
160
161
162
163
164
165
import codecs
import itertools
from abc import ABC, abstractmethod
class A:
def __new__(cls, *args, **kwargs):
print('new', cls, args, kwargs)
return super().__new__(cls)
def __init__(self, *args, **kwargs):
print('init', self, args, kwargs)
def how_object_construction_works():
x = A(1, 2, 3, x=4)
# above is equivalent to below, but this can be customized using metaclasses
x = A.__new__(A, 1, 2, 3, x=4)
if isinstance(x, A):
type(x).__init__(x, 1, 2, 3, x=4)
print(x)
class UppercaseTuple(tuple):
def __new__(cls, iterable):
upper_iterable = (s.upper() for s in iterable)
return super().__new__(cls, upper_iterable)
# Error: tuples are immutable, even in init
# def __init__(self, iterable):
# print(f'init {iterable}')
# for i, arg in enumerate(iterable):
# self[i] = arg.upper()
def inheriting_immutable_uppercase_tuple_example():
print("UPPERCASE TUPLE EXAMPLE")
print(UppercaseTuple(["hi", "there"]))
class Singleton: # eg global config object, I discourage actually using this pattern
_instance = None
def __new__(cls, *args, **kwargs):
if cls._instance is None:
cls._instance = super().__new__(cls, *args, **kwargs)
return cls._instance
def singleton_example():
print("SINGLETON EXAMPLE")
x = Singleton()
y = Singleton()
print(f'{x is y=}')
class Client:
_loaded = {}
_db_file = "file.db"
def __new__(cls, client_id):
if (client := cls._loaded.get(client_id)) is not None:
print(f'returning existing client {client_id} from cache')
return client
client = super().__new__(cls)
cls._loaded[client_id] = client
client._init_from_file(client_id, cls._db_file)
return client
def _init_from_file(self, client_id, file):
# lookup client in file and read properties
print(f'reading client {client_id} data from file, db, etc.')
name = ...
email = ...
self.name = name
self.email = email
self.id = client_id
def cached_clients_example():
print("CLIENT CACHE EXAMPLE")
x = Client(0)
y = Client(0)
print(f'{x is y=}')
z = Client(1)
class EncryptedFile: # DO NOT USE ANY OF THESE FOR REAL ENCRYPTION
_registry = {} # 'rot13' -> ROT13Text
def __init_subclass__(cls, prefix, **kwargs):
super().__init_subclass__(**kwargs)
cls._registry[prefix] = cls
def __new__(cls, path: str, key=None):
prefix, sep, suffix = path.partition(':///')
if sep:
file = suffix
else:
file = prefix
prefix = "file"
subclass = cls._registry[prefix]
obj = object.__new__(subclass)
obj.file = file
obj.key = key
return obj
def read(self) -> str:
raise NotImplementedError
class Plaintext(EncryptedFile, prefix='file'):
def read(self):
with open(self.file, 'r') as f:
return f.read()
class ROT13Text(EncryptedFile, prefix='rot13'):
def read(self):
with open(self.file, 'r') as f:
text = f.read()
return codecs.decode(text, 'rot_13')
class OneTimePadXorText(EncryptedFile, prefix='otp'):
def __init__(self, path, key):
if isinstance(self.key, str):
self.key = self.key.encode()
def xor_bytes_with_key(self, b: bytes) -> bytes:
return bytes(b1 ^ b2 for b1, b2 in zip(b, itertools.cycle(self.key)))
def read(self):
with open(self.file, 'rb') as f:
btext = f.read()
text = self.xor_bytes_with_key(btext).decode()
return text
def encrypted_file_example():
print("ENCRYPTED FILE EXAMPLE")
print(EncryptedFile('plaintext_hello.txt').read())
print(EncryptedFile('rot13:///rot13_hello.txt').read())
print(EncryptedFile('otp:///otp_hello.txt', key='1234').read())
class Drawable(ABC):
@abstractmethod # marks draw with __isabstractmethod__ = True
def draw(self):
return NotImplemented
def main():
how_object_construction_works()
inheriting_immutable_uppercase_tuple_example()
singleton_example()
cached_clients_example()
encrypted_file_example()
if __name__ == '__main__':
main()