forked from kachayev/talks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kyivpy#6.py
331 lines (274 loc) · 7.81 KB
/
kyivpy#6.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# Let me start...
name = "Alexey Kachayev"
topic = "Descriptors && OOP"
# Simple non-data descriptor
class Speaker(object):
def __get__(self, obj, objtype):
return "Alexey"
# Usage example
class Conf(object):
speaker = Speaker() ## <--- Descriptor!
# Check how it works
Conf.speaker ## <--- attr resolve for class
kyivpy = Conf()
kyivpy.speaker ## <--- attr resolve for class instance
# Let's get little bit more information
class Speaker(object):
def __get__(self, obj, objtype):
print self, obj, objtype; return "Alexey"
# The same usage example
class Conf(object):
speaker = Speaker()
# Check how it works now
Conf.speaker
Conf().speaker
kyivpy = Conf()
kyivpy.speaker
# Inspect __get__ calls
import inspect
class Speaker(object):
def __get__(self, obj, objtype):
print inspect.getframeinfo(inspect.currentframe().f_back); return "Alexey"
# The same usage example
class Conf(object):
speaker = Speaker()
# Diving deeper into OOP model...
Conf.speaker
kyivpy = Conf()
Conf.__dict__
Conf.speaker
Conf.__dict__['speaker']
Conf.__dict__['speaker'].__get__(None, Conf)
## REMEMBER about: object.__getattribute__
type(kyivpy).__dict__['speaker'].__get__(kyivpy, type(kyivpy))
# Next line will create "speaker" as
# string object in instance __dict__
kyivpy.speaker = "Alexey K."
kyivpy.__dict__
kyivpy.speaker
# Data descriptor example
class Speaker(object):
def __get__(self, obj, objtype):
print "Getting"
return "Alexey"
def __set__(self, obj, value):
print "Setting"
# The same usage example
class Conf(object):
speaker = Speaker()
kyivpy = Conf()
kyivpy.speaker
# Will work with descriptor instead of instance __dict__
kyivpy.speaker = "Alexey K."
kyivpy.__dict__
kyivpy.__dict__['speaker'] = "Alexey Kachayev"
kyivpy.speaker
kyivpy.__dict__
#
# Descriptor in day-to-day pratice...
# Create API which will look like this:
#
# class XmlDoc(object):
# name = Field("name")
# .. other fields ..
#
class Field(object):
def __init__(self, name):
self.name = name
self.field = "_field_" + name
def __get__(self, obj, objtype):
if obj is None:
return self
return getattr(obj, self.field, "")
def __set__(self, obj, value):
setattr(obj, self.field, value)
class XmlDoc(object):
name = Field("name")
date = Field("date")
speakers = Field("speakers")
# Check how it will work
kyivpy = XmlDoc()
kyivpy.name = "Kyiv #6"
kyivpy.date = "Today"
kyivpy.speakers = "Alexey, Andrey"
kyivpy.__dict__
kyivpy.name
# Add rendering facilities
class Field(object):
def __init__(self, name):
self.name = name
self.field = "_field_" + name
def __get__(self, obj, objtype):
if obj is None:
return self
return getattr(obj, self.field, "")
def __set__(self, obj, value):
setattr(obj, self.field, value)
def render(self, obj):
return "<{tag}>{value}</{tag}>".format(tag = self.name, value=getattr(obj, self.field, ""))
# Check once more...
class XmlDoc(object):
name = Field("name")
date = Field("date")
speakers = Field("speakers")
kyivpy = XmlDoc()
kyivpy.name = "Kyiv #6"
kyivpy.speakers = "Alexey, Andrey"
kyivpy.date = "Today"
kyivpy.__dict__
XmlDoc.__dict__
XmlDoc.__dict__.values()
# Filter all Fields from doc
filter(lambda x: type(x) == Field, XmlDoc.__dict__.values())
# Call rendering function
from operator import methodcaller
map(
methodcaller("render", kyivpy),
filter(lambda x: type(x) == Field, XmlDoc.__dict__.values())
)
# Add ordering functionality
from itertools import count
class Field(object):
counter = count(1)
def __init__(self, name):
self.name = name
self.field = "_field_" + name
self.order = Field.counter.next()
def __get__(self, obj, objtype):
return getattr(obj, self.field, "")
def __set__(self, obj, value):
setattr(obj, self.field, value)
class XmlDoc(object):
name = Field("name")
date = Field("date")
speakers = Field("speakers")
# Rendering example with ordering
kyivpy = XmlDoc()
kyivpy.name = "#6"
kyivpy.speakers = "Alexey, ... CO"
kyivpy.date = "Today"
# Filtering
filter(lambda x: type(x) == Field, XmlDoc.__dict__.values())
# Mapping and other stuff...
map(
attrgetter("order"),
filter(lambda x: type(x) == Field, XmlDoc.__dict__.values())
)
map(
methodcaller("render", kyivpy),
sorted(
filter(lambda x: type(x) == Field, XmlDoc.__dict__.values()),
key=attrgetter("order")
)
)
map(
attrgetter("name"),
sorted(
filter(lambda x: type(x) == Field, XmlDoc.__dict__.values()),
key=attrgetter("order")
)
)
# Functions/bounded methods/unbounded methods
class Talk(object):
def __init__(self, name):
self.name = name
def say(self):
print self.name, ">> mmm..something"
# Check it...
me = Talk("Alexey")
me.say()
me.say
Talk.say
# Instance methods
type(me.say)
type(Talk.say)
# How we can get instance method from function object
Talk.__dict__
Talk.__dict__['say']
Talk.__dict__['say'].__get__(None, Talk)
Talk.__dict__['say'].__get__(None, Talk)()
Talk.__dict__['say'].__get__(me, type(me))
Talk.__dict__['say'].__get__(None, Talk).im_self
print Talk.__dict__['say'].__get__(None, Talk).im_self
print Talk.__dict__['say'].__get__(None, Talk).im_func
Talk.__dict__['say'].__get__(me, type(me)).im_func
Talk.__dict__['say'].__get__(me, type(me)).im_self
# Dynamic binding implementation
def finish(self):
print self.name, "<< finished"
me.finish = finish
me.finish()
me.__dict__
me.finish = finish.__get__(me, type(me))
me.__dict__
me.finish()
# Own implementation for @property
class Property(object):
def __init__(self, fget, fset):
self.fget = fget
self.fset = fset
def __get__(self, obj, objtype):
if obj is None:
return self
return self.fget(obj)
def __set__(self, obj, value):
self.fset(obj, value)
# Usage example...
class Talk(object):
def getname(self):
return self.__name
def setname(self, value):
self.__name = value
def __init__(self, name):
self.name = name
name = Property(getname, setname)
me = Talk("Alexey")
me.name
me.__dict__
me.__name
me._Talk__name
# Own implementation for @staticmethod
class StaticMethod(object):
def __init__(self, f):
self.f = f
def __get__(self, obj, objtype):
return self.f
# Usage example...
class Talk(object):
def start():
print "Started ..."
start_static = StaticMethod(start)
me = Talk()
conf = Talk()
conf.start
conf.start()
conf.start_static()
conf.start_static
# Own implementation of @classmethod functionality
class ClassMethod(object):
def __init__(self, f):
self.f = f
def __get__(self, obj, objtype):
# We need this function in order to call it with type(obj)
# as first argument. Partial function execution will
# work fine also (functools.partial)
def wrapper(*args):
return self.f(objtype, *args)
return wrapper
# Usage example
class Talk(object):
def __init__(self, name):
self.name = name
def from_list(cls, speakers):
return map(cls, speakers)
from_list = ClassMethod(from_list)
# Common case for working with @classmethod
Talk.from_list(["Alexey", "Vsevolod"])
# Check that everything works fine
map(attrgetter("name"), Talk.from_list(["Alexey", "Vsevolod"]))
# Check that descriptor will return "wrapper" function
Talk.__dict__['from_list']
Talk.__dict__['from_list'].__get__(None, Talk)
Talk.__dict__['from_list'].__get__(None, Talk).__name__
# Here you can find IPython session from conference
git = "github.com/kachayev/talks"