-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
411 lines (365 loc) · 11 KB
/
index.html
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>pyception - Metaprogramacion en Python</title>
<meta name="description" content="Charla de ACM (Abril 2013)">
<meta name="author" content="Salvador Perez Martin">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link rel="stylesheet" href="css/reveal.min.css">
<link rel="stylesheet" href="css/theme/default.css" id="theme">
<!-- For syntax highlighting -->
<link rel="stylesheet" href="lib/css/zenburn.css">
<!-- If the query includes 'print-pdf', use the PDF print sheet -->
<script>
document.write( '<link rel="stylesheet" href="css/print/' + ( window.location.search.match( /print-pdf/gi ) ? 'pdf' : 'paper' ) + '.css" type="text/css" media="print">' );
</script>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section>
<h1>PYCEPTION</h1>
<h3>Metaprogramación en Python</h3>
<img src="img/manos.png" alt="'Manos dibujando' por M.C. Escher">
</section>
<section>
<section>
<h2>¿Qué es la metaprogramación?</h2>
<p>
Programas que se modifican a sí mismos
</p>
</section>
<section>
<h2>Requisitos</h2>
<p>
<ul>
<li>Lenguaje dinámico</li>
<ul>
<li>Javascript</li>
<li>Python</li>
<li>Ruby</li>
<li>...</li>
</ul>
</ul>
</p>
</section>
<section>
<h2>¿De qué va todo esto?</h2>
<pre>
<code contenteditable class="python">
<span>class MiClase():
"""Una clase normalita y corriente con un método"""
def valor():
return 1
objeto = MiClase()
objeto.valor() # 1
def suma(x,y):
return x + y
objeto.suma = suma
objeto.suma(1,2) # 3
o = MiClase()
o.suma(1,2) # ??</span>
</code>
</pre>
</section>
</section>
<section>
<section>
<h2>Tipos</h2>
<pre>
<code contenteditable class="python">
<p class="fragment" data-fragment-index='0' style="margin-bottom:-15px">>> type(5)</p>
<p class="fragment" data-fragment-index='1' style="margin-bottom:-15px">>> type([1,2,3])</p>
<p class="fragment" data-fragment-index='2' style="margin-bottom:-15px">>> type(list)</p>
<p class="fragment" data-fragment-index='3' style="margin-bottom:-15px">>> type(type)</p>
<div class="fragment" data-fragment-index='4'>
<span>>> def fun():
pass
>> type(fun)</span>
</div>
</code>
</pre>
</section>
<section>
<h2>First-class objects</h2>
<ul>
<li> Las funciones son objetos </li>
</ul>
<pre>
<code contenteditable class="python">
<span>def suma(a,b):
return a+b
funcion = suma
print funcion(1,2) # 3</span>
</code>
</pre>
</section>
<section>
<h2>First-class objects</h2>
<ul>
<li> Las funciones pueden devolver funciones </li>
</ul>
<pre>
<code contenteditable class="python">
<span>>> def generate():
def result():
print "It works ;-)"
return result
>> fun = generate()
>> fun()</span>
</code>
</pre>
</section>
</section>
<section>
<section>
<h2>Decoradores</h2>
<pre><code>
<span>>> def decorador(f):
def resultado(*args,**kwargs):
print "Entrando"
f(*args,**kwargs)
print "Saliendo"
return resultado
@decorador
def funcion(a):
print a
funcion("Estoy decorada ^^")</span>
</code></pre>
</section>
<section>
<h2>Vale, pero... ¿para qué quiero yo eso?</h2>
</section>
<section>
<h2>Ejemplo de decoradores</h2>
<ul>
<li>Añadir Caché a una operación</li>
</ul>
<pre><code class="python">
<span>>> def cache(f):
almacen = {}
def funcion(*args, **kwargs):
key = hash(args)
key2 = hash(tuple(kwargs.iteritems()))
try:
resultado = almacen[key, key2]
except KeyError:
almacen[key,key2] = f(*args,**kwargs)
resultado = almacen[key, key2]
return resultado
return funcion
>> @cache
def suma(a,b):
return a + b</span>
</code></pre>
<aside class="notes">Habla sobre las closures</aside>
</section>
</section>
<section>
<section>
<h1 class="fragment fade-out" data-fragment-index='0'>DRY</h1>
<h1 class="fragment roll-in" data-fragment-index='0'>Don't Repeat Yourself</h1>
</section>
<section>
<h2>Esto en java (por ejemplo)...</h2>
<pre><code class="java">
<span>>> public class MiClase{
private int a
private String b
private boolean c
public int getA(){
return this.a;
}
public String getB(){
return this.b;
}
public boolean getC(){
return this.c;
}
}</span>
</code></pre>
<p class="fragment" data-fragment-index='0'>Me temo que no se cumple</p>
</section>
<section>
<h2>Metaprogramación</h2>
<p>En Python si se puede</p>
<pre><code class="python">
<span>>> class MiClase():
def __init__(self):
self.a = 5
self.b = 'blr'
self.c = True
def build_get(var):
def funcion(self):
return getattr(self,var,None)
return funcion
getA = build_get('a')
getB = build_get('b')
getC = build_get('c')</span>
</code></pre>
</section>
<section>
<h1>¿Se puede acortar más?</h1>
</section>
<section>
<pre><code class="python">
<span>>> class MiClase():
def __init__(self):
self.a = 5
self.b = 'blr'
self.c = True
def build_get(var):
def funcion(self):
return getattr(self,var,None)
return funcion
getA = build_get('a')
getB = build_get('b')
getC = build_get('c')
>> class MiSegundaClase():
def __init__(self):
self.a = 5
self.b = 'blr'
self.c = True
def build_get(var):
def funcion(self):
return getattr(self,var,None)
return funcion
getA = build_get('a')
getB = build_get('b')
getC = build_get('c')</span>
</code></pre>
</section>
<section>
<h2>Sí</h2>
<pre><code class="python">
<span>>> def build_get(var):
def funcion(self):
return getattr(self,var,None)
return funcion
>> class MiClase():
def __init__(self):
self.a = 5
self.b = 'blr'
self.c = True
>> class MiSegundaClase():
def __init__(self):
self.a = 5
self.b = 'blr'
self.c = True
>> for e in ['a','b','c']:
setattr(MiClase,"get%s" % e,build_get(e))
setattr(MiSegundaClase,"get%s" % e,build_get(e))
</span>
</code></pre>
</section>
</section>
<section>
<section>
<h2>Buenas APIs</h2>
<p>¿Qué características son necesarias?</p>
<ul>
<li class="fragment" data-fragment-index='0'>Natural</li>
<li class="fragment" data-fragment-index='1'>Predecible</li>
<li class="fragment" data-fragment-index='2'>Fácil de mantener</li>
<li class="fragment" data-fragment-index='3'>Fácil de extender</li>
</ul>
</section>
<section>
<h2>¿Qué va a pasar aquí?</h2>
<pre><code class="python">
<span>>> class ShopQueries():
"""A web for my own shop"""
>> s = ShopQueries()
>> s.getItems()
</span>
</code></pre>
</section>
<section>
<h2>¿Cómo sabe Python que no existe?</h2>
<ol>
<li class="fragment" data-fragment-index='0'>Busca en el objeto</li>
<li class="fragment" data-fragment-index='1'>Busca en su clase</li>
<li class="fragment" data-fragment-index='2'>Busca en sus superclases</li>
<li class="fragment" data-fragment-index='3'>...</li>
</ol>
</section>
<section>
<h2>¿Y cómo busca Python?</h2>
<p>cuando llamas a un método, estás llamando a <pre><code class="python"><span>
objeto.__getattribute__("nombre del metodo")
</span></code></pre></p>
<h3 class="fragment" data-fragment-index='0'>Vamos a sobreescribirlo!!</h3>
</section>
<section>
<pre><code class="python">
<span>class ShopQueries():
"""A web for my own shop"""
def build_get(var):
def funcion(self):
return getattr(self,var,None)
return funcion
def __getattribute__(self, name):
if not hasattr(self, name):
if name[:3] == 'get':
setattr(ShopQueries,"get%s" % name,build_get(name[3:]))
return super(ShopQueries,self).__getattribute__(name)
>> s = ShopQueries()
>> s.getItems()</span>
</code></pre>
</section>
</section>
<section>
<h2>Cosas para leer</h2>
<ul>
<li><a href="http://unoyunodiez.com/2012/03/31/metaprogramacion-en-python/">Metaprogramación en python</a> por Salvador de la Puente </li>
<li><a href="http://pyvideo.org/video/302/pycon-2010--python-metaprogramming---64">Python Metaprogramming</a> por Nicolas Lara</li>
<li><a href="http://www.devx.com/opensource/Article/31482">Dig Deep into Python Internals</a> por Gigi Sayfan</li>
</ul>
</section>
<section>
<h2>Sobre mí</h2>
<p>
<small>Salvador Pérez Martín / <a href="http://twitter.com/kuraikibo">@kuraikibo</a></small><br/>
<small><a href="mailto:salva@acm.asoc.fi.upm.es">salva@acm.asoc.fi.upm.es</a></small>
</p>
</section>
<section>
<h2>¿Preguntas?</h2>
<p>Descárgate estas transparencias:<a href="https://github.com/kuraime/metaprogramacion-acmfi">https://github.com/kuraime/metaprogramacion-acmfi</a></p>
</section>
</div>
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.min.js"></script>
<script>
// Full list of configuration options available here:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: true,
history: true,
center: true,
theme: Reveal.getQueryHash().theme, // available themes are in /css/theme
transition: Reveal.getQueryHash().transition || 'linear', // default/cube/page/concave/zoom/linear/fade/none
// Optional libraries used to extend on reveal.js
dependencies: [
{ src: 'lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'plugin/markdown/showdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: 'plugin/zoom-js/zoom.js', async: true, condition: function() { return !!document.body.classList; } },
{ src: 'plugin/notes/notes.js', async: true, condition: function() { return !!document.body.classList; } }
// { src: 'plugin/search/search.js', async: true, condition: function() { return !!document.body.classList; } }
// { src: 'plugin/remotes/remotes.js', async: true, condition: function() { return !!document.body.classList; } }
]
});
</script>
</body>
</html>