Browse Source

added info about the elses

master
Julio Biason 9 years ago
parent
commit
19cda4a8b9
  1. 579
      python.html

579
python.html

@ -352,7 +352,7 @@ python meuscript.py
>>> a = 'Python' >>> a = 'Python'
>>> b = "Python" >>> b = "Python"
>>> c = """Python >>> c = """Python
>>> Rocks!""" ... Rocks!"""
</code></pre></p> </code></pre></p>
</section> </section>
@ -377,7 +377,7 @@ python meuscript.py
<p><pre><code data-trim> <p><pre><code data-trim>
&gt;&gt;&gt; a = {'Python': 'Rocks', &gt;&gt;&gt; a = {'Python': 'Rocks',
&gt;&gt;&gt; 1: 1.0} ... 1: 1.0}
</code></pre></p> </code></pre></p>
</section> </section>
@ -411,7 +411,7 @@ set(['a'])
<ul> <ul>
<li>Long (<code>a = 1L</code>)</li> <li>Long (<code>a = 1L</code>)</li>
<li>Lambdas (<code>a = lambda a: a + 2</code>)</li> <li>Lambdas (<code>a = lambda a: a + 2</code>)</li>
<li>Raw strings (<code>r'string\s'</code>, usadas para regex)</li> <li>Raw strings (<code>r'string\s'</code>, usadas para regex)</li>
</ul> </ul>
</section> </section>
</section> </section>
@ -434,7 +434,7 @@ set(['a'])
<p><pre><code data-trim> <p><pre><code data-trim>
&gt;&gt;&gt; if a == 1: &gt;&gt;&gt; if a == 1:
&gt;&gt;&gt; b = 2 ... b = 2
&gt;&gt;&gt; c = 3 &gt;&gt;&gt; c = 3
</code></pre></p> </code></pre></p>
</section> </section>
@ -445,9 +445,9 @@ set(['a'])
<p><pre><code data-trim> <p><pre><code data-trim>
&gt;&gt;&gt; a = 1 &gt;&gt;&gt; a = 1
&gt;&gt;&gt; while True: &gt;&gt;&gt; while True:
&gt;&gt;&gt; a += 1 ... a += 1
&gt;&gt;&gt; if a &gt; 10: ... if a &gt; 10:
&gt;&gt;&gt; break ... break
</code></pre></p> </code></pre></p>
</section> </section>
@ -514,7 +514,7 @@ True
<p><pre><code data-trim> <p><pre><code data-trim>
&gt;&gt;&gt; soma = 0 &gt;&gt;&gt; soma = 0
&gt;&gt;&gt; for valor em [345, 123, 123, 34]: &gt;&gt;&gt; for valor em [345, 123, 123, 34]:
&gt;&gt;&gt; soma += valor ... soma += valor
</code></pre></p> </code></pre></p>
</section> </section>
</section> </section>
@ -545,7 +545,7 @@ True
<p><pre><code data-trim> <p><pre><code data-trim>
&gt;&gt;&gt; for l in 'Python': &gt;&gt;&gt; for l in 'Python':
&gt;&gt;&gt; print l ... print l
</code></pre></p> </code></pre></p>
</section> </section>
@ -555,7 +555,7 @@ True
<p><pre><code data-trim> <p><pre><code data-trim>
&gt;&gt;&gt; d = {'Python': 'Rocks', 'Parrot': 'Dead', 'Favorite Color': 'Blue'} &gt;&gt;&gt; d = {'Python': 'Rocks', 'Parrot': 'Dead', 'Favorite Color': 'Blue'}
&gt;&gt;&gt; for key in d: &gt;&gt;&gt; for key in d:
&gt;&gt;&gt; print key, d[key] ... print key, d[key]
</code></pre></p> </code></pre></p>
</section> </section>
@ -565,7 +565,7 @@ True
<p><pre><code data-trim> <p><pre><code data-trim>
&gt;&gt;&gt; d = {'Python': 'Rocks', 'Parrot': 'Dead', 'Favorite Color': 'Blue'} &gt;&gt;&gt; d = {'Python': 'Rocks', 'Parrot': 'Dead', 'Favorite Color': 'Blue'}
&gt;&gt;&gt; for (key, value) in d.iteritems(): &gt;&gt;&gt; for (key, value) in d.iteritems():
&gt;&gt;&gt; print key, value ... print key, value
</code></pre></p> </code></pre></p>
<p class='fragment'>Forma considerada "correta" é a anterior.</p> <p class='fragment'>Forma considerada "correta" é a anterior.</p>
@ -595,7 +595,7 @@ iterável[start:end:step]
<section> <section>
<p><pre><code data-trim> <p><pre><code data-trim>
&gt;&gt;&gt; a = [1, 2, 3, 4] &gt;&gt;&gt; a = [1, 2, 3, 4]
&gt;&gt;&gt; print a[1:2] ... print a[1:2]
[2] [2]
</code></pre></p> </code></pre></p>
</section> </section>
@ -611,7 +611,7 @@ iterável[start:end:step]
<p><pre><code data-trim> <p><pre><code data-trim>
&gt;&gt;&gt; a = [1, 2, 3, 4] &gt;&gt;&gt; a = [1, 2, 3, 4]
&gt;&gt;&gt; print a[:2] ... print a[:2]
[1, 2] [1, 2]
</code></pre></p> </code></pre></p>
</section> </section>
@ -621,7 +621,7 @@ iterável[start:end:step]
<p><pre><code data-trim> <p><pre><code data-trim>
&gt;&gt;&gt; a = [1, 2, 3, 4] &gt;&gt;&gt; a = [1, 2, 3, 4]
&gt;&gt;&gt; print a[1:-1] ... print a[1:-1]
[2, 3] [2, 3]
</code></pre></p> </code></pre></p>
</section> </section>
@ -631,7 +631,7 @@ iterável[start:end:step]
<p><pre><code data-trim> <p><pre><code data-trim>
&gt;&gt;&gt; a = 'Python Rocks' &gt;&gt;&gt; a = 'Python Rocks'
&gt;&gt;&gt; print a[7:-1] ... print a[7:-1]
'Rock' 'Rock'
</code></pre></p> </code></pre></p>
</section> </section>
@ -640,7 +640,7 @@ iterável[start:end:step]
<p>Deixar os dois índices em branco cria uma cópia "flat".</p> <p>Deixar os dois índices em branco cria uma cópia "flat".</p>
<p><pre><code data-trim> <p><pre><code data-trim>
&gt;&gt;&gt; a = [1, 2, 3, 4] &gt;&gt;&gt; a = [1, 2, 3, 4]
&gt;&gt;&gt; print a[:] ... print a[:]
[1, 2, 3, 4] [1, 2, 3, 4]
</code></pre></p> </code></pre></p>
@ -672,7 +672,7 @@ iterável[start:end:step]
<p><pre><code data-trim> <p><pre><code data-trim>
&gt;&gt;&gt; def funcao(a, b, c): &gt;&gt;&gt; def funcao(a, b, c):
&gt;&gt;&gt; return (a + b) / c ... return (a + b) / c
</code></pre></p> </code></pre></p>
</section> </section>
@ -681,7 +681,7 @@ iterável[start:end:step]
<p><pre><code data-trim> <p><pre><code data-trim>
&gt;&gt;&gt; def funcao(a, b, c): &gt;&gt;&gt; def funcao(a, b, c):
&gt;&gt;&gt; return (a + b) / c ... return (a + b) / c
&gt;&gt;&gt; &gt;&gt;&gt;
&gt;&gt;&gt; funcao(b=2, c=3, a=10) &gt;&gt;&gt; funcao(b=2, c=3, a=10)
4 4
@ -734,6 +734,8 @@ iterável[start:end:step]
<p>Não existe função para o destrutor.</p> <p>Não existe função para o destrutor.</p>
<p class="fragment">(Existe, mas ninguém usa.)</p>
<p class='fragment'>Existem ainda outras funções <p class='fragment'>Existem ainda outras funções
<i>mágicas</i>, mas não vamos falar sobre elas <i>mágicas</i>, mas não vamos falar sobre elas
nesse momento.</p> </section> nesse momento.</p> </section>
@ -741,10 +743,10 @@ iterável[start:end:step]
<section> <section>
<p><pre><code data-trim> <p><pre><code data-trim>
&gt;&gt;&gt; class MyClasse(object): &gt;&gt;&gt; class MyClasse(object):
&gt;&gt;&gt; def __init__(self): ... def __init__(self):
&gt;&gt;&gt; self.valor = 0 ... self.valor = 0
&gt;&gt;&gt; def show(self): ... def show(self):
&gt;&gt;&gt; print self.valor ... print self.valor
</code></pre></p> </code></pre></p>
</section> </section>
@ -766,10 +768,10 @@ iterável[start:end:step]
<p><pre><code data-trim> <p><pre><code data-trim>
&gt;&gt;&gt; class MyClasse(object): &gt;&gt;&gt; class MyClasse(object):
&gt;&gt;&gt; def __init__(self, name): ... def __init__(self, name):
&gt;&gt;&gt; self.name = name ... self.name = name
&gt;&gt;&gt; def show(self): ... def show(self):
&gt;&gt;&gt; print self.name ... print self.name
&gt;&gt;&gt; my = MyClasse('Julio') &gt;&gt;&gt; my = MyClasse('Julio')
&gt;&gt;&gt; my.show() &gt;&gt;&gt; my.show()
@ -777,17 +779,34 @@ Julio
</code></pre></p> </code></pre></p>
</section> </section>
<section>
<p>Propriedades podem ser injetadas a qualquer momento.</p>
<pre><code class="hljs">
&gt;&gt;&gt; class A(object):
... def __init__(self):
... self.value = 10
&gt;&gt;&gt;
&gt;&gt;&gt; a = A()
&gt;&gt;&gt; a.name = 'Julio'
</code></pre>
<p class="fragment">Inserção de propriedades pode ser
barrada com o uso da variável mágica
<code>__slots__</code>, mas raramente é usado.</p>
</section>
<section> <section>
<p>Herança</p> <p>Herança</p>
<p><pre><code data-trim> <p><pre><code data-trim>
&gt;&gt;&gt; class A(object): &gt;&gt;&gt; class A(object):
&gt;&gt;&gt; def __init__(self): ... def __init__(self):
&gt;&gt;&gt; self.value = 10 ... self.value = 10
&gt;&gt;&gt; class B(A): &gt;&gt;&gt; class B(A):
&gt;&gt;&gt; def __init__(self): ... def __init__(self):
&gt;&gt;&gt; super(B, self).__init__() ... super(B, self).__init__()
&gt;&gt;&gt; self.name = 'AAAA' ... self.name = 'AAAA'
</code></pre></p> </code></pre></p>
</section> </section>
@ -796,14 +815,14 @@ Julio
<p><pre><code data-trim> <p><pre><code data-trim>
&gt;&gt;&gt; class A(object): &gt;&gt;&gt; class A(object):
&gt;&gt;&gt; def __init__(self): ... def __init__(self):
&gt;&gt;&gt; self.value = 10 ... self.value = 10
&gt;&gt;&gt; class B(object): &gt;&gt;&gt; class B(object):
&gt;&gt;&gt; def __init__(self): ... def __init__(self):
&gt;&gt;&gt; self.name = 'AAAA' ... self.name = 'AAAA'
&gt;&gt;&gt; class C(A, B): &gt;&gt;&gt; class C(A, B):
&gt;&gt;&gt; def __init__(self): ... def __init__(self):
&gt;&gt;&gt; super(C, self).__init__() ... super(C, self).__init__()
</code></pre></p> </code></pre></p>
</section> </section>
@ -812,17 +831,124 @@ Julio
</section> </section>
<section> <section>
<p>Propriedades podem ser injetadas a qualquer momento.</p> <p><code>super()</code> é mais usado para resolução
hierárquia com herança múltipla.</p>
</section>
<section>
<pre><code class="hljs"> <pre><code class="hljs">
&gt;&gt;&gt; class A(object): &gt;&gt;&gt; class Adao(object): pass
&gt;&gt;&gt; def __init__(self): &gt;&gt;&gt; class Eva(object): pass
&gt;&gt;&gt; self.value = 10 &gt;&gt;&gt; class AvoPaterno(Adao, Eva): pass
&gt;&gt;&gt; &gt;&gt;&gt; class AvohPaterna(Adao, Eva): pass
&gt;&gt;&gt; a = A() &gt;&gt;&gt; class AvoMaterno(Adao, Eva): pass
&gt;&gt;&gt; a.name = 'Julio' &gt;&gt;&gt; class AvohMaterna(Adao, Eva): pass
&gt;&gt;&gt; class Pai(AvoPaterno, AvohPaterna): pass
&gt;&gt;&gt; class Mae(AvoMaterno, AvohMaterna): pass
&gt;&gt;&gt; class Filho(Pai, Mae): pass
</code></pre>
<pre><code class="hljs">
&gt;&gt;&gt; help(Filho)
</code></pre>
</section>
<section>
<pre><code class="hljs">
Help on class Filho in module __main__:
class Filho(Pai, Mae)
| Method resolution order:
| Filho
| Pai
| AvoPaterno
| AvohPaterna
| Mae
| AvoMaterno
| AvohMaterna
| Adao
| Eva
| __builtin__.object
</code></pre>
</section>
<section>
<p>A ordem de resolução pode ser usada para inserir
mocks sem o uso de mocks.</p>
<p class="fragment">... embora só sirva para mockar
objetos "é um" e não "contém um".</p>
</section>
<section>
<pre><code class="hljs">
&gt;&gt;&gt; class Robo(object):
... def pegar(self, ferramenta):
... print 'Pegando', ferramenta
... def pra_frente(self):
... print 'Movendo pra frente'
... def pra_tras(self):
... print 'Voltando'
... def largar(self):
... print 'Largando ferramenta'
</code></pre>
</section>
<section>
<pre><code class="hljs">
&gt;&gt;&gt; class RoboDeLimpeza(Robo):
... def clean(self, repeticoes=10):
... super(Robo, self).pegar('vassoura')
... for _ in xrange(repeticoes):
... super(Robo, self).pra_frente()
... super(Robo, self).pra_tras()
... super(Robo, self).largar()
</code></pre>
</section>
<section>
<pre><code class="hljs">
&gt;&gt;&gt; class MockRobo(Robo):
... def __init__(self):
... self.acoes = []
... def pegar(self, ferramenta):
... self.acoes.append('Pegar {}'.format(ferramenta))
... def pra_frente(self):
... self.acoes.append('frente')
... def pra_tras(self):
... self.acoes.append('tras')
... def largar(self):
... self.acoes.append('largar')
</code></pre>
</section>
<section>
<pre><code class="hljs">
&gt;&gt;&gt; class MockRoboDeLimpeza(RoboDeLimpeza, MockRobo):
... pass
</code></pre>
</section>
<section>
<pre><code class="hljs">
Help on class MockRoboDeLimpeza in module __main__:
class MockRoboDeLimpeza(RoboDeLimpeza, MockRobo)
| Method resolution order:
| MockRoboDeLimpeza
| RoboDeLimpeza
| MockRobo
| Robo
| __builtin__.object
</code></pre> </code></pre>
</section> </section>
<section>
<p>
Mais informações no vídeo de Raymond Hettinger:
<a href='https://www.youtube.com/watch?v=EiOglTERPEo'>Super Considered super!</a>
</p>
</section>
</section> </section>
<section> <section>
@ -867,9 +993,8 @@ Julio
<section> <section>
<p><pre><code data-trim> <p><pre><code data-trim>
&gt;&gt;&gt; def a(l=[]): &gt;&gt;&gt; def a(l=[]):
&gt;&gt;&gt; l.append(1) ... l.append(1)
&gt;&gt;&gt; print l ... print l
&gt;&gt;&gt;
&gt;&gt;&gt; a() &gt;&gt;&gt; a()
[1] [1]
&gt;&gt;&gt; a() &gt;&gt;&gt; a()
@ -882,10 +1007,9 @@ Julio
<p><pre><code data-trim> <p><pre><code data-trim>
&gt;&gt;&gt; def a(l=None): &gt;&gt;&gt; def a(l=None):
&gt;&gt;&gt; if not l: ... if not l:
&gt;&gt;&gt; l = [] ... l = []
&gt;&gt;&gt; l.append(1) ... l.append(1)
&gt;&gt;&gt;
&gt;&gt;&gt; a() &gt;&gt;&gt; a()
[1] [1]
&gt;&gt;&gt; a() &gt;&gt;&gt; a()
@ -906,8 +1030,7 @@ Julio
<section> <section>
<p><pre><code data-trim> <p><pre><code data-trim>
&gt;&gt;&gt; def a(*args): &gt;&gt;&gt; def a(*args):
&gt;&gt;&gt; print args ... print args
&gt;&gt;&gt;
&gt;&gt;&gt; a(1) &gt;&gt;&gt; a(1)
[1] [1]
&gt;&gt;&gt; a(1, 2, 3, 4, 5) &gt;&gt;&gt; a(1, 2, 3, 4, 5)
@ -920,8 +1043,7 @@ Julio
<section> <section>
<p><pre><code data-trim> <p><pre><code data-trim>
&gt;&gt;&gt; def a(**kwargs): &gt;&gt;&gt; def a(**kwargs):
&gt;&gt;&gt; print kwargs ... print kwargs
&gt;&gt;&gt;
&gt;&gt;&gt; a(a=1) &gt;&gt;&gt; a(a=1)
{'a': 1} {'a': 1}
&gt;&gt;&gt; a(value1=10, a=2) &gt;&gt;&gt; a(value1=10, a=2)
@ -934,9 +1056,8 @@ Julio
<section> <section>
<p><pre><code data-trim> <p><pre><code data-trim>
&gt;&gt;&gt; def a(*args, **kwargs): &gt;&gt;&gt; def a(*args, **kwargs):
&gt;&gt;&gt; print args ... print args
&gt;&gt;&gt; print kwargs ... print kwargs
&gt;&gt;&gt;
&gt;&gt;&gt; a(a=1) &gt;&gt;&gt; a(a=1)
[] []
{'a': 1} {'a': 1}
@ -949,17 +1070,72 @@ Julio
<section> <section>
<p><pre><code data-trim> <p><pre><code data-trim>
&gt;&gt;&gt; def a(a, b, *args, name=None, **kwargs): &gt;&gt;&gt; def a(a, b, *args, name=None, **kwargs):
&gt;&gt;&gt; print 'a =', a ... print 'a =', a
&gt;&gt;&gt; print 'b =', b ... print 'b =', b
&gt;&gt;&gt; print 'args =', args ... print 'args =', args
&gt;&gt;&gt; print 'name = ', name ... print 'name = ', name
&gt;&gt;&gt; print 'kwargs =', kwargs ... print 'kwargs =', kwargs
</code></pre></p> </code></pre></p>
<p>Saída de uma chamada desta função fica a cargo do leitor.</p> <p>Saída de uma chamada desta função fica a cargo do leitor.</p>
</section> </section>
</section> </section>
<section>
<section>
<h3><code>for</code> tem <code>else</code></h3>
</section>
<section>
<pre><code class="hljs">
&gt;&gt;&gt; for i in xrange(10):
... if i == 1:
... break
... else:
... print 'Oh no'
</code></pre>
<p class="fragment">WAT</p>
</section>
<section>
<h3><code>for</code> tem <code>else</code></h3>
<p><code>else</code> é chamado se a execução do loop
chegar ao final.</p>
<pre><code class="hljs">
&gt;&gt;&gt; for record in all_content:
... if record.name = search:
... found = record
... break
... else:
... found = None
</code></pre>
</section>
</section>
</section>
<section>
<h3><code>try</code> tem <code>else</code></h3>
</section>
<section>
<pre><code class="hljs">
&gt;&gt;&gt; try:
... func_that_raises_exception()
... else:
... print 'Yay!'
</code></pre>
<p class="fragment"><code>else</code> é chamado quando
exceções não ocorrerem.</p>
<p class="fragment"><code>finally</code>
<strong>SEMPRE</strong> é chamado.</p>
</section>
</section>
<section> <section>
<section> <section>
<h3>"Functions are First Class Citizens"</h3> <h3>"Functions are First Class Citizens"</h3>
@ -968,12 +1144,10 @@ Julio
<section> <section>
<p><pre><code data-trim> <p><pre><code data-trim>
&gt;&gt;&gt; def funcao(a, b, c): &gt;&gt;&gt; def funcao(a, b, c):
&gt;&gt;&gt; return (a + b) / c ... return (a + b) / c
&gt;&gt;&gt;
&gt;&gt;&gt; def check(a, b, c, condition, function): &gt;&gt;&gt; def check(a, b, c, condition, function):
&gt;&gt;&gt; if condition: ... if condition:
&gt;&gt;&gt; print function(a, b, c) ... print function(a, b, c)
&gt;&gt;&gt;
&gt;&gt;&gt; check(1, 2, 3, True, funcao) &gt;&gt;&gt; check(1, 2, 3, True, funcao)
1 1
&gt;&gt;&gt; check(1, 2, 3, False, funcao) &gt;&gt;&gt; check(1, 2, 3, False, funcao)
@ -988,12 +1162,10 @@ Julio
<p><pre><code data-trim> <p><pre><code data-trim>
&gt;&gt;&gt; class A(object): &gt;&gt;&gt; class A(object):
&gt;&gt;&gt; def __init__(self): ... def __init__(self):
&gt;&gt;&gt; self.value = 10 ... self.value = 10
&gt;&gt;&gt; ... def show_name(self):
&gt;&gt;&gt; def show_name(self): ... print 'Julio'
&gt;&gt;&gt; print 'Julio'
&gt;&gt;&gt;
&gt;&gt;&gt; a = A() &gt;&gt;&gt; a = A()
&gt;&gt;&gt; a.show = show_name &gt;&gt;&gt; a.show = show_name
&gt;&gt;&gt; a.show() &gt;&gt;&gt; a.show()
@ -1017,7 +1189,7 @@ Julio
<section> <section>
<p><pre><code data-trim> <p><pre><code data-trim>
&gt;&gt;&gt; def retrieve(connection): &gt;&gt;&gt; def retrieve(connection):
&gt;&gt;&gt; # faz algo com a conexão para recuperar dados. ... # faz algo com a conexão para recuperar dados.
</code></pre></p> </code></pre></p>
<p>Problema: antes de sair executando algo na conexão, tem que ser <p>Problema: antes de sair executando algo na conexão, tem que ser
@ -1030,15 +1202,15 @@ Julio
<p><pre><code data-trim> <p><pre><code data-trim>
&gt;&gt;&gt; def retrieve(connection): &gt;&gt;&gt; def retrieve(connection):
&gt;&gt;&gt; # faz algo com a conexão para recuperar dados. ... # faz algo com a conexão para recuperar dados.
&gt;&gt;&gt;
&gt;&gt;&gt; def update(connection): &gt;&gt;&gt; def update(connection):
&gt;&gt;&gt; # atualiza algo usando a função ... # atualiza algo usando a função
&gt;&gt;&gt;
&gt;&gt;&gt; def check(connection, call): &gt;&gt;&gt; def check(connection, call):
&gt;&gt;&gt; if not connection.is_connected: ... if not connection.is_connected:
&gt;&gt;&gt; connection.retry() ... connection.retry()
&gt;&gt;&gt; call(connection) ... call(connection)
</code></pre></p> </code></pre></p>
<p>Novo problema: Todo lugar onde antes era chamado <code>retrieve</code> <p>Novo problema: Todo lugar onde antes era chamado <code>retrieve</code>
@ -1052,18 +1224,17 @@ Julio
<p><pre><code data-trim> <p><pre><code data-trim>
&gt;&gt;&gt; from functools import wrap &gt;&gt;&gt; from functools import wrap
&gt;&gt;&gt;
&gt;&gt;&gt; def check(func): &gt;&gt;&gt; def check(func):
&gt;&gt;&gt; def check_conn(*args, **kwargs): ... def check_conn(*args, **kwargs):
&gt;&gt;&gt; # acha a conexão em args ou kwargs ... # acha a conexão em args ou kwargs
&gt;&gt;&gt; if not connection.is_connected: ... if not connection.is_connected:
&gt;&gt;&gt; connection.retry() ... connection.retry()
&gt;&gt;&gt; return func(*args, **kwargs) ... return func(*args, **kwargs)
&gt;&gt;&gt; return check_conn ... return check_conn
&gt;&gt;&gt;
&gt;&gt;&gt; @check &gt;&gt;&gt; @check
&gt;&gt;&gt; def retrieve(connection): &gt;&gt;&gt; def retrieve(connection):
&gt;&gt;&gt; # faz algo com a conexão para recuperar dados ... # faz algo com a conexão para recuperar dados
</code></pre></p> </code></pre></p>
<p>Não precisa alterar nenhuma chamada de <code>retrieve</code>.</p> <p>Não precisa alterar nenhuma chamada de <code>retrieve</code>.</p>
@ -1078,22 +1249,22 @@ Julio
<pre><code class="hljs"> <pre><code class="hljs">
&gt;&gt;&gt; class CheckConn(object): &gt;&gt;&gt; class CheckConn(object):
&gt;&gt;&gt; def __init__(self, func): ... def __init__(self, func):
&gt;&gt;&gt; self.func = func ... self.func = func
&gt;&gt;&gt; ...
&gt;&gt;&gt; def __call__(self, *args, **kwargs): ... def __call__(self, *args, **kwargs):
&gt;&gt;&gt; if 'connection' in kwargs: ... if 'connection' in kwargs:
&gt;&gt;&gt; connection = kwargs['connection'] ... connection = kwargs['connection']
&gt;&gt;&gt; else: ... else:
&gt;&gt;&gt; connection = args[0] ... connection = args[0]
&gt;&gt;&gt; ...
&gt;&gt;&gt; if not connection.is_connected: ... if not connection.is_connected:
&gt;&gt;&gt; connection.retry() ... connection.retry()
&gt;&gt;&gt; self.func(*args, **kwargs) ... self.func(*args, **kwargs)
&gt;&gt;&gt; @CheckCon &gt;&gt;&gt; @CheckCon
&gt;&gt;&gt; def retrieve(connection): &gt;&gt;&gt; def retrieve(connection):
&gt;&gt;&gt; # retrieve ... # retrieve
</code></pre> </code></pre>
</section> </section>
@ -1111,16 +1282,16 @@ Julio
<pre><code class='hljs'> <pre><code class='hljs'>
&gt;&gt;&gt; class CheckConn(object): &gt;&gt;&gt; class CheckConn(object):
&gt;&gt;&gt; def __init__(self, func): ... def __init__(self, func):
&gt;&gt;&gt; self._func = func ... self._func = func
&gt;&gt;&gt; ...
&gt;&gt;&gt; @property ... @property
&gt;&gt;&gt; def func(self): ... def func(self):
&gt;&gt;&gt; return self._func ... return self._func
&gt;&gt;&gt; ...
&gt;&gt;&gt; @func.setter ... @func.setter
&gt;&gt;&gt; def func(self, value): ... def func(self, value):
&gt;&gt;&gt; self._func = func ... self._func = func
</code></pre> </code></pre>
</section> </section>
@ -1129,112 +1300,112 @@ Julio
<pre><code class='hljs'> <pre><code class='hljs'>
&gt;&gt;&gt; class CheckConn(object): &gt;&gt;&gt; class CheckConn(object):
&gt;&gt;&gt; def __init__(self, func): ... def __init__(self, func):
&gt;&gt;&gt; self._func = func ... self._func = func
&gt;&gt;&gt; ...
&gt;&gt;&gt; @staticmethod ... @staticmethod
&gt;&gt;&gt; def from_text(self, text): ... def from_text(self, text):
&gt;&gt;&gt; return CheckConn(getattr(self, text)) ... return CheckConn(getattr(self, text))
</code></pre> </code></pre>
</section> </section>
</section> </section>
<section> <section>
<section> <section>
<h3>Comprehensions e Generators</h3> <h3>Comprehensions e Generators</h3>
<p>Python permite criar listas processando listas sem <p>Python permite criar listas processando listas sem
<code>for</code> com <i>list <code>for</code> com <i>list
comprehensions</i>.</p> comprehensions</i>.</p>
<pre><code class="hljs"> <pre><code class="hljs">
&gt;&gt;&gt; a = [1, 2, 3] &gt;&gt;&gt; a = [1, 2, 3]
&gt;&gt;&gt; [item * 2 for item in a] &gt;&gt;&gt; [item * 2 for item in a]
&gt;&gt;&gt; [2, 4, 6] &gt;&gt;&gt; [2, 4, 6]
</code></pre> </code></pre>
<p>Pra quem gosta de coisas "funcionais", é o mesmo que <p>Pra quem gosta de coisas "funcionais", é o mesmo que
<code>map</code>.</p> <code>map</code>.</p>
<pre><code class="hljs"> <pre><code class="hljs">
&gt;&gt;&gt; a = [1, 2, 3] &gt;&gt;&gt; a = [1, 2, 3]
&gt;&gt;&gt; map(lamba f: f * 2, a) &gt;&gt;&gt; map(lamba f: f * 2, a)
&gt;&gt;&gt; [2, 4, 6] &gt;&gt;&gt; [2, 4, 6]
</code></pre> </code></pre>
</section> </section>
<section> <section>
<h4>Comprehensions (contd.)</h4> <h4>Comprehensions (contd.)</h4>
<p>É possível filtrar elementos com list comprehensions.</p> <p>É possível filtrar elementos com list comprehensions.</p>
<pre><code class="hljs"> <pre><code class="hljs">
&gt;&gt;&gt; a = [1, 2, 3] &gt;&gt;&gt; a = [1, 2, 3]
&gt;&gt;&gt; [item for item in a if item > 2] &gt;&gt;&gt; [item for item in a if item > 2]
&gt;&gt;&gt; [3] &gt;&gt;&gt; [3]
</code></pre> </code></pre>
<p>Funcionalmente, é o mesmo que <code>filter</code>.</p> <p>Funcionalmente, é o mesmo que <code>filter</code>.</p>
<pre><code class="hljs"> <pre><code class="hljs">
&gt;&gt;&gt; a = [1, 2, 3] &gt;&gt;&gt; a = [1, 2, 3]
&gt;&gt;&gt; filter(lambda f: f > 2, a) &gt;&gt;&gt; filter(lambda f: f > 2, a)
&gt;&gt;&gt; [3] &gt;&gt;&gt; [3]
</code></pre> </code></pre>
</section> </section>
<section> <section>
<h4>Generators</h4> <h4>Generators</h4>
<p>Enquanto que comprehensions criam novas listas, generators <p>Enquanto que comprehensions criam novas listas, generators
geram elementos sob demanda.</p> geram elementos sob demanda.</p>
<pre><code class="hljs"> <pre><code class="hljs">
&gt;&gt;&gt; a = [1, 2, 3] &gt;&gt;&gt; a = [1, 2, 3]
&gt;&gt;&gt; (item * 2 for item in a) &gt;&gt;&gt; (item * 2 for item in a)
&lt;generator object &lt;genexpr&gt; at 0x7f8673dfc050&gt; &lt;generator object &lt;genexpr&gt; at 0x7f8673dfc050&gt;
</code></pre> </code></pre>
</section> </section>
<section> <section>
<h5>Generators (contd.)</h5> <h5>Generators (contd.)</h5>
<pre><code class="hljs"> <pre><code class="hljs">
&gt;&gt;&gt; [item for item in range(5000000)] &gt;&gt;&gt; [item for item in range(5000000)]
</code></pre> </code></pre>
<p>vs</p> <p>vs</p>
<pre><code class="hljs"> <pre><code class="hljs">
&gt;&gt;&gt; (item for item in xrange(5000000)) &gt;&gt;&gt; (item for item in xrange(5000000))
</code></pre> </code></pre>
</section> </section>
<section> <section>
<h5>Generators (contd.)</h5> <h5>Generators (contd.)</h5>
<pre><code class="hljs"> <pre><code class="hljs">
&gt;&gt;&gt; [item for item in range(5000000)][:5] &gt;&gt;&gt; [item for item in range(5000000)][:5]
</code></pre> </code></pre>
<p>vs</p> <p>vs</p>
<pre><code class="hljs"> <pre><code class="hljs">
&gt;&gt;&gt; (item for item in xrange(5000000))[:5] &gt;&gt;&gt; (item for item in xrange(5000000))[:5]
</code></pre> </code></pre>
</section> </section>
<section> <section>
<h5>Generators (contd.)</h5> <h5>Generators (contd.)</h5>
<pre><code class="hljs"> <pre><code class="hljs">
&gt;&gt;&gt; def gen(max_value): &gt;&gt;&gt; def gen(max_value):
&gt;&gt;&gt; for value in xrange(max_value): ... for value in xrange(max_value):
&gt;&gt;&gt; yield value * 2 ... yield value * 2
</code></pre> </code></pre>
<p>Generator functions não podem ter <code>return</code>!</p> <p>Generator functions não podem ter <code>return</code>!</p>
</section> </section>
</section> </section>
<section> <section>
<section> <section>
@ -1249,45 +1420,45 @@ Julio
<section> <section>
<h3>Context Managers</h3> <h3>Context Managers</h3>
<pre><code class="hljs"> <pre><code class="hljs">
&gt;&gt;&gt; class Connection(object): &gt;&gt;&gt; class Connection(object):
&gt;&gt;&gt; def __init__(self): ... def __init__(self):
&gt;&gt;&gt; self._conn = None ... self._conn = None
&gt;&gt;&gt; ...
&gt;&gt;&gt; def __enter__(self): ... def __enter__(self):
&gt;&gt;&gt; self._conn = self._make_connection() ... self._conn = self._make_connection()
&gt;&gt;&gt; return self._conn ... return self._conn
&gt;&gt;&gt; ...
&gt;&gt;&gt; def __exit__(self, exc_type, exc_value, traceback): ... def __exit__(self, exc_type, exc_value, traceback):
&gt;&gt;&gt; self._conn.close() ... self._conn.close()
&gt;&gt;&gt; if exc_type: # then exc_value and traceback ... if exc_type: # then exc_value and traceback
&gt;&gt;&gt; print "Exception!", exc_type, exc_value ... print "Exception!", exc_type, exc_value
&gt;&gt;&gt; print traceback ... print traceback
</code></pre> </code></pre>
<pre><code class="hljs"> <pre><code class="hljs">
&gt;&gt;&gt; with Connection() as connection: &gt;&gt;&gt; with Connection() as connection:
&gt;&gt;&gt; connection.request('Value') ... connection.request('Value')
</code></pre> </code></pre>
</section> </section>
<section> <section>
<h3>Context Managers vs Exceptions</h3> <h3>Context Managers vs Exceptions</h3>
<pre><code class="hljs"> <pre><code class="hljs">
&gt;&gt;&gt; try: &gt;&gt;&gt; try:
&gt;&gt;&gt; conn = self._make_connection() ... conn = self._make_connection()
&gt;&gt;&gt; conn.request('value') ... conn.request('value')
&gt;&gt;&gt; finally: ... finally:
&gt;&gt;&gt; conn.close() ... conn.close()
&gt;&gt;&gt; except Exception as exc: # Bare exceptions are BAAAADDD! ... except Exception as exc: # Bare exceptions are BAAAADDD!
&gt;&gt;&gt; print 'Exception!', exc ... print 'Exception!', exc
</code></pre> </code></pre>
</section> </section>
</section> </section>
<section> <section>
<section> <section>
<h3>Docstrings</h3> <h3>Docstrings</h3>
@ -1296,8 +1467,8 @@ Julio
<pre><code class="hljs"> <pre><code class="hljs">
&gt;&gt;&gt; def func(a): &gt;&gt;&gt; def func(a):
&gt;&gt;&gt; """Função mágica""" ... """Função mágica"""
&gt;&gt;&gt; return a ... return a
&gt;&gt;&gt; print func.__doc__ &gt;&gt;&gt; print func.__doc__
Função mágica Função mágica
@ -1388,7 +1559,7 @@ def send(sender, recipient, message_body, priority):
diretórios a serem pesquisados e um makefile com a opção diretórios a serem pesquisados e um makefile com a opção
html para geração de arquivos HTML.)</p> html para geração de arquivos HTML.)</p>
</section> </section>
</section> </section>
<section> <section>
<section> <section>

Loading…
Cancel
Save