forked from dbradul/python_course
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlesson_classes.py
More file actions
98 lines (70 loc) · 2.14 KB
/
lesson_classes.py
File metadata and controls
98 lines (70 loc) · 2.14 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
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
import re
import time
import json
import yaml
import dicttoxml, xmltodict
from functools import partial
from pprint import pprint as pp
class timer():
def __init__(self, message):
self.message = message
def __enter__(self):
self.start = time.time()
return None
def __exit__(self, type, value, traceback):
elapsed_time = (time.time() - self.start) * 1000
print(self.message.format(elapsed_time))
class Q:
def __init__(self, *, as_string='', **params):
self._params = params
self._as_string = as_string
def __or__(self, other):
self._as_string = f'({str(self)} OR {str(other)})'
return self #Q(as_string=f'({str(self)} OR {str(other)})')
def __and__(self, other):
return Q(as_string=f'({str(self)} AND {str(other)})')
def __invert__(self):
return Q(as_string=f'NOT {str(self)}')
def __str__(self):
result = ''
if self._as_string:
result = self._as_string
elif self._params:
result = [f'{k}={repr(v)}' for k, v in self._params.items()][0]
return result
filter = (Q(first_name='J') | Q(last_name='J', telephone='+38000')) & ~Q(email='test@gmail.com')
print(filter)
filter = Q(first_name='J') | (Q(last_name='J', telephone='+38000') & ~Q(email='test@gmail.com'))
print(filter)
import logging
logger = logging.getLogger(__name__)
logger2 = logging.getLogger(__name__)
print(id(logger), id(logger2))
class Attribute:
def __init__(self, initval=None, name='var'):
self.val = initval
self.name = name
def __get__(self, obj, objtype):
print('Retrieving', self.name, id(obj))
return obj.__dict__[self.name] # self.val
def __set__(self, obj, val):
print('Updating', self.name, id(obj))
self.val = val
obj.__dict__[self.name] = val
def __delete__(self, obj):
# print('Deleting', self.name, id(obj))
self.val = None
class A:
attr = Attribute(name='attr')
# attr = 'DEMO'
a = A()
print(id(a))
a.attr = 42
print(a.attr)
# del a.attr
#
# b = A()
# print(id(b))
# b.attr = 43
# print(a.attr)
# print(b.attr)