|
|
|
@ -352,7 +352,7 @@ python meuscript.py
|
|
|
|
|
>>> a = 'Python' |
|
|
|
|
>>> b = "Python" |
|
|
|
|
>>> c = """Python |
|
|
|
|
>>> Rocks!""" |
|
|
|
|
... Rocks!""" |
|
|
|
|
</code></pre></p> |
|
|
|
|
</section> |
|
|
|
|
|
|
|
|
@ -377,7 +377,7 @@ python meuscript.py
|
|
|
|
|
|
|
|
|
|
<p><pre><code data-trim> |
|
|
|
|
>>> a = {'Python': 'Rocks', |
|
|
|
|
>>> 1: 1.0} |
|
|
|
|
... 1: 1.0} |
|
|
|
|
</code></pre></p> |
|
|
|
|
</section> |
|
|
|
|
|
|
|
|
@ -434,7 +434,7 @@ set(['a'])
|
|
|
|
|
|
|
|
|
|
<p><pre><code data-trim> |
|
|
|
|
>>> if a == 1: |
|
|
|
|
>>> b = 2 |
|
|
|
|
... b = 2 |
|
|
|
|
>>> c = 3 |
|
|
|
|
</code></pre></p> |
|
|
|
|
</section> |
|
|
|
@ -445,9 +445,9 @@ set(['a'])
|
|
|
|
|
<p><pre><code data-trim> |
|
|
|
|
>>> a = 1 |
|
|
|
|
>>> while True: |
|
|
|
|
>>> a += 1 |
|
|
|
|
>>> if a > 10: |
|
|
|
|
>>> break |
|
|
|
|
... a += 1 |
|
|
|
|
... if a > 10: |
|
|
|
|
... break |
|
|
|
|
</code></pre></p> |
|
|
|
|
</section> |
|
|
|
|
|
|
|
|
@ -514,7 +514,7 @@ True
|
|
|
|
|
<p><pre><code data-trim> |
|
|
|
|
>>> soma = 0 |
|
|
|
|
>>> for valor em [345, 123, 123, 34]: |
|
|
|
|
>>> soma += valor |
|
|
|
|
... soma += valor |
|
|
|
|
</code></pre></p> |
|
|
|
|
</section> |
|
|
|
|
</section> |
|
|
|
@ -545,7 +545,7 @@ True
|
|
|
|
|
|
|
|
|
|
<p><pre><code data-trim> |
|
|
|
|
>>> for l in 'Python': |
|
|
|
|
>>> print l |
|
|
|
|
... print l |
|
|
|
|
</code></pre></p> |
|
|
|
|
</section> |
|
|
|
|
|
|
|
|
@ -555,7 +555,7 @@ True
|
|
|
|
|
<p><pre><code data-trim> |
|
|
|
|
>>> d = {'Python': 'Rocks', 'Parrot': 'Dead', 'Favorite Color': 'Blue'} |
|
|
|
|
>>> for key in d: |
|
|
|
|
>>> print key, d[key] |
|
|
|
|
... print key, d[key] |
|
|
|
|
</code></pre></p> |
|
|
|
|
</section> |
|
|
|
|
|
|
|
|
@ -565,7 +565,7 @@ True
|
|
|
|
|
<p><pre><code data-trim> |
|
|
|
|
>>> d = {'Python': 'Rocks', 'Parrot': 'Dead', 'Favorite Color': 'Blue'} |
|
|
|
|
>>> for (key, value) in d.iteritems(): |
|
|
|
|
>>> print key, value |
|
|
|
|
... print key, value |
|
|
|
|
</code></pre></p> |
|
|
|
|
|
|
|
|
|
<p class='fragment'>Forma considerada "correta" é a anterior.</p> |
|
|
|
@ -595,7 +595,7 @@ iterável[start:end:step]
|
|
|
|
|
<section> |
|
|
|
|
<p><pre><code data-trim> |
|
|
|
|
>>> a = [1, 2, 3, 4] |
|
|
|
|
>>> print a[1:2] |
|
|
|
|
... print a[1:2] |
|
|
|
|
[2] |
|
|
|
|
</code></pre></p> |
|
|
|
|
</section> |
|
|
|
@ -611,7 +611,7 @@ iterável[start:end:step]
|
|
|
|
|
|
|
|
|
|
<p><pre><code data-trim> |
|
|
|
|
>>> a = [1, 2, 3, 4] |
|
|
|
|
>>> print a[:2] |
|
|
|
|
... print a[:2] |
|
|
|
|
[1, 2] |
|
|
|
|
</code></pre></p> |
|
|
|
|
</section> |
|
|
|
@ -621,7 +621,7 @@ iterável[start:end:step]
|
|
|
|
|
|
|
|
|
|
<p><pre><code data-trim> |
|
|
|
|
>>> a = [1, 2, 3, 4] |
|
|
|
|
>>> print a[1:-1] |
|
|
|
|
... print a[1:-1] |
|
|
|
|
[2, 3] |
|
|
|
|
</code></pre></p> |
|
|
|
|
</section> |
|
|
|
@ -631,7 +631,7 @@ iterável[start:end:step]
|
|
|
|
|
|
|
|
|
|
<p><pre><code data-trim> |
|
|
|
|
>>> a = 'Python Rocks' |
|
|
|
|
>>> print a[7:-1] |
|
|
|
|
... print a[7:-1] |
|
|
|
|
'Rock' |
|
|
|
|
</code></pre></p> |
|
|
|
|
</section> |
|
|
|
@ -640,7 +640,7 @@ iterável[start:end:step]
|
|
|
|
|
<p>Deixar os dois índices em branco cria uma cópia "flat".</p> |
|
|
|
|
<p><pre><code data-trim> |
|
|
|
|
>>> a = [1, 2, 3, 4] |
|
|
|
|
>>> print a[:] |
|
|
|
|
... print a[:] |
|
|
|
|
[1, 2, 3, 4] |
|
|
|
|
</code></pre></p> |
|
|
|
|
|
|
|
|
@ -672,7 +672,7 @@ iterável[start:end:step]
|
|
|
|
|
|
|
|
|
|
<p><pre><code data-trim> |
|
|
|
|
>>> def funcao(a, b, c): |
|
|
|
|
>>> return (a + b) / c |
|
|
|
|
... return (a + b) / c |
|
|
|
|
</code></pre></p> |
|
|
|
|
</section> |
|
|
|
|
|
|
|
|
@ -681,7 +681,7 @@ iterável[start:end:step]
|
|
|
|
|
|
|
|
|
|
<p><pre><code data-trim> |
|
|
|
|
>>> def funcao(a, b, c): |
|
|
|
|
>>> return (a + b) / c |
|
|
|
|
... return (a + b) / c |
|
|
|
|
>>> |
|
|
|
|
>>> funcao(b=2, c=3, a=10) |
|
|
|
|
4 |
|
|
|
@ -734,6 +734,8 @@ iterável[start:end:step]
|
|
|
|
|
|
|
|
|
|
<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 |
|
|
|
|
<i>mágicas</i>, mas não vamos falar sobre elas |
|
|
|
|
nesse momento.</p> </section> |
|
|
|
@ -741,10 +743,10 @@ iterável[start:end:step]
|
|
|
|
|
<section> |
|
|
|
|
<p><pre><code data-trim> |
|
|
|
|
>>> class MyClasse(object): |
|
|
|
|
>>> def __init__(self): |
|
|
|
|
>>> self.valor = 0 |
|
|
|
|
>>> def show(self): |
|
|
|
|
>>> print self.valor |
|
|
|
|
... def __init__(self): |
|
|
|
|
... self.valor = 0 |
|
|
|
|
... def show(self): |
|
|
|
|
... print self.valor |
|
|
|
|
</code></pre></p> |
|
|
|
|
</section> |
|
|
|
|
|
|
|
|
@ -766,10 +768,10 @@ iterável[start:end:step]
|
|
|
|
|
|
|
|
|
|
<p><pre><code data-trim> |
|
|
|
|
>>> class MyClasse(object): |
|
|
|
|
>>> def __init__(self, name): |
|
|
|
|
>>> self.name = name |
|
|
|
|
>>> def show(self): |
|
|
|
|
>>> print self.name |
|
|
|
|
... def __init__(self, name): |
|
|
|
|
... self.name = name |
|
|
|
|
... def show(self): |
|
|
|
|
... print self.name |
|
|
|
|
|
|
|
|
|
>>> my = MyClasse('Julio') |
|
|
|
|
>>> my.show() |
|
|
|
@ -777,17 +779,34 @@ Julio
|
|
|
|
|
</code></pre></p> |
|
|
|
|
</section> |
|
|
|
|
|
|
|
|
|
<section> |
|
|
|
|
<p>Propriedades podem ser injetadas a qualquer momento.</p> |
|
|
|
|
|
|
|
|
|
<pre><code class="hljs"> |
|
|
|
|
>>> class A(object): |
|
|
|
|
... def __init__(self): |
|
|
|
|
... self.value = 10 |
|
|
|
|
>>> |
|
|
|
|
>>> a = A() |
|
|
|
|
>>> 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> |
|
|
|
|
<p>Herança</p> |
|
|
|
|
|
|
|
|
|
<p><pre><code data-trim> |
|
|
|
|
>>> class A(object): |
|
|
|
|
>>> def __init__(self): |
|
|
|
|
>>> self.value = 10 |
|
|
|
|
... def __init__(self): |
|
|
|
|
... self.value = 10 |
|
|
|
|
>>> class B(A): |
|
|
|
|
>>> def __init__(self): |
|
|
|
|
>>> super(B, self).__init__() |
|
|
|
|
>>> self.name = 'AAAA' |
|
|
|
|
... def __init__(self): |
|
|
|
|
... super(B, self).__init__() |
|
|
|
|
... self.name = 'AAAA' |
|
|
|
|
</code></pre></p> |
|
|
|
|
</section> |
|
|
|
|
|
|
|
|
@ -796,14 +815,14 @@ Julio
|
|
|
|
|
|
|
|
|
|
<p><pre><code data-trim> |
|
|
|
|
>>> class A(object): |
|
|
|
|
>>> def __init__(self): |
|
|
|
|
>>> self.value = 10 |
|
|
|
|
... def __init__(self): |
|
|
|
|
... self.value = 10 |
|
|
|
|
>>> class B(object): |
|
|
|
|
>>> def __init__(self): |
|
|
|
|
>>> self.name = 'AAAA' |
|
|
|
|
... def __init__(self): |
|
|
|
|
... self.name = 'AAAA' |
|
|
|
|
>>> class C(A, B): |
|
|
|
|
>>> def __init__(self): |
|
|
|
|
>>> super(C, self).__init__() |
|
|
|
|
... def __init__(self): |
|
|
|
|
... super(C, self).__init__() |
|
|
|
|
</code></pre></p> |
|
|
|
|
</section> |
|
|
|
|
|
|
|
|
@ -812,17 +831,124 @@ Julio
|
|
|
|
|
</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"> |
|
|
|
|
>>> class A(object): |
|
|
|
|
>>> def __init__(self): |
|
|
|
|
>>> self.value = 10 |
|
|
|
|
>>> |
|
|
|
|
>>> a = A() |
|
|
|
|
>>> a.name = 'Julio' |
|
|
|
|
>>> class Adao(object): pass |
|
|
|
|
>>> class Eva(object): pass |
|
|
|
|
>>> class AvoPaterno(Adao, Eva): pass |
|
|
|
|
>>> class AvohPaterna(Adao, Eva): pass |
|
|
|
|
>>> class AvoMaterno(Adao, Eva): pass |
|
|
|
|
>>> class AvohMaterna(Adao, Eva): pass |
|
|
|
|
>>> class Pai(AvoPaterno, AvohPaterna): pass |
|
|
|
|
>>> class Mae(AvoMaterno, AvohMaterna): pass |
|
|
|
|
>>> class Filho(Pai, Mae): pass |
|
|
|
|
</code></pre> |
|
|
|
|
|
|
|
|
|
<pre><code class="hljs"> |
|
|
|
|
>>> 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"> |
|
|
|
|
>>> 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"> |
|
|
|
|
>>> 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"> |
|
|
|
|
>>> 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"> |
|
|
|
|
>>> 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> |
|
|
|
|
</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> |
|
|
|
@ -867,9 +993,8 @@ Julio
|
|
|
|
|
<section> |
|
|
|
|
<p><pre><code data-trim> |
|
|
|
|
>>> def a(l=[]): |
|
|
|
|
>>> l.append(1) |
|
|
|
|
>>> print l |
|
|
|
|
>>> |
|
|
|
|
... l.append(1) |
|
|
|
|
... print l |
|
|
|
|
>>> a() |
|
|
|
|
[1] |
|
|
|
|
>>> a() |
|
|
|
@ -882,10 +1007,9 @@ Julio
|
|
|
|
|
|
|
|
|
|
<p><pre><code data-trim> |
|
|
|
|
>>> def a(l=None): |
|
|
|
|
>>> if not l: |
|
|
|
|
>>> l = [] |
|
|
|
|
>>> l.append(1) |
|
|
|
|
>>> |
|
|
|
|
... if not l: |
|
|
|
|
... l = [] |
|
|
|
|
... l.append(1) |
|
|
|
|
>>> a() |
|
|
|
|
[1] |
|
|
|
|
>>> a() |
|
|
|
@ -906,8 +1030,7 @@ Julio
|
|
|
|
|
<section> |
|
|
|
|
<p><pre><code data-trim> |
|
|
|
|
>>> def a(*args): |
|
|
|
|
>>> print args |
|
|
|
|
>>> |
|
|
|
|
... print args |
|
|
|
|
>>> a(1) |
|
|
|
|
[1] |
|
|
|
|
>>> a(1, 2, 3, 4, 5) |
|
|
|
@ -920,8 +1043,7 @@ Julio
|
|
|
|
|
<section> |
|
|
|
|
<p><pre><code data-trim> |
|
|
|
|
>>> def a(**kwargs): |
|
|
|
|
>>> print kwargs |
|
|
|
|
>>> |
|
|
|
|
... print kwargs |
|
|
|
|
>>> a(a=1) |
|
|
|
|
{'a': 1} |
|
|
|
|
>>> a(value1=10, a=2) |
|
|
|
@ -934,9 +1056,8 @@ Julio
|
|
|
|
|
<section> |
|
|
|
|
<p><pre><code data-trim> |
|
|
|
|
>>> def a(*args, **kwargs): |
|
|
|
|
>>> print args |
|
|
|
|
>>> print kwargs |
|
|
|
|
>>> |
|
|
|
|
... print args |
|
|
|
|
... print kwargs |
|
|
|
|
>>> a(a=1) |
|
|
|
|
[] |
|
|
|
|
{'a': 1} |
|
|
|
@ -949,17 +1070,72 @@ Julio
|
|
|
|
|
<section> |
|
|
|
|
<p><pre><code data-trim> |
|
|
|
|
>>> def a(a, b, *args, name=None, **kwargs): |
|
|
|
|
>>> print 'a =', a |
|
|
|
|
>>> print 'b =', b |
|
|
|
|
>>> print 'args =', args |
|
|
|
|
>>> print 'name = ', name |
|
|
|
|
>>> print 'kwargs =', kwargs |
|
|
|
|
... print 'a =', a |
|
|
|
|
... print 'b =', b |
|
|
|
|
... print 'args =', args |
|
|
|
|
... print 'name = ', name |
|
|
|
|
... print 'kwargs =', kwargs |
|
|
|
|
</code></pre></p> |
|
|
|
|
|
|
|
|
|
<p>Saída de uma chamada desta função fica a cargo do leitor.</p> |
|
|
|
|
</section> |
|
|
|
|
</section> |
|
|
|
|
|
|
|
|
|
<section> |
|
|
|
|
<section> |
|
|
|
|
<h3><code>for</code> tem <code>else</code></h3> |
|
|
|
|
</section> |
|
|
|
|
|
|
|
|
|
<section> |
|
|
|
|
<pre><code class="hljs"> |
|
|
|
|
>>> 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"> |
|
|
|
|
>>> 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"> |
|
|
|
|
>>> 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> |
|
|
|
|
<h3>"Functions are First Class Citizens"</h3> |
|
|
|
@ -968,12 +1144,10 @@ Julio
|
|
|
|
|
<section> |
|
|
|
|
<p><pre><code data-trim> |
|
|
|
|
>>> def funcao(a, b, c): |
|
|
|
|
>>> return (a + b) / c |
|
|
|
|
>>> |
|
|
|
|
... return (a + b) / c |
|
|
|
|
>>> def check(a, b, c, condition, function): |
|
|
|
|
>>> if condition: |
|
|
|
|
>>> print function(a, b, c) |
|
|
|
|
>>> |
|
|
|
|
... if condition: |
|
|
|
|
... print function(a, b, c) |
|
|
|
|
>>> check(1, 2, 3, True, funcao) |
|
|
|
|
1 |
|
|
|
|
>>> check(1, 2, 3, False, funcao) |
|
|
|
@ -988,12 +1162,10 @@ Julio
|
|
|
|
|
|
|
|
|
|
<p><pre><code data-trim> |
|
|
|
|
>>> class A(object): |
|
|
|
|
>>> def __init__(self): |
|
|
|
|
>>> self.value = 10 |
|
|
|
|
>>> |
|
|
|
|
>>> def show_name(self): |
|
|
|
|
>>> print 'Julio' |
|
|
|
|
>>> |
|
|
|
|
... def __init__(self): |
|
|
|
|
... self.value = 10 |
|
|
|
|
... def show_name(self): |
|
|
|
|
... print 'Julio' |
|
|
|
|
>>> a = A() |
|
|
|
|
>>> a.show = show_name |
|
|
|
|
>>> a.show() |
|
|
|
@ -1017,7 +1189,7 @@ Julio
|
|
|
|
|
<section> |
|
|
|
|
<p><pre><code data-trim> |
|
|
|
|
>>> def retrieve(connection): |
|
|
|
|
>>> # faz algo com a conexão para recuperar dados. |
|
|
|
|
... # faz algo com a conexão para recuperar dados. |
|
|
|
|
</code></pre></p> |
|
|
|
|
|
|
|
|
|
<p>Problema: antes de sair executando algo na conexão, tem que ser |
|
|
|
@ -1030,15 +1202,15 @@ Julio
|
|
|
|
|
|
|
|
|
|
<p><pre><code data-trim> |
|
|
|
|
>>> def retrieve(connection): |
|
|
|
|
>>> # faz algo com a conexão para recuperar dados. |
|
|
|
|
>>> |
|
|
|
|
... # faz algo com a conexão para recuperar dados. |
|
|
|
|
|
|
|
|
|
>>> def update(connection): |
|
|
|
|
>>> # atualiza algo usando a função |
|
|
|
|
>>> |
|
|
|
|
... # atualiza algo usando a função |
|
|
|
|
|
|
|
|
|
>>> def check(connection, call): |
|
|
|
|
>>> if not connection.is_connected: |
|
|
|
|
>>> connection.retry() |
|
|
|
|
>>> call(connection) |
|
|
|
|
... if not connection.is_connected: |
|
|
|
|
... connection.retry() |
|
|
|
|
... call(connection) |
|
|
|
|
</code></pre></p> |
|
|
|
|
|
|
|
|
|
<p>Novo problema: Todo lugar onde antes era chamado <code>retrieve</code> |
|
|
|
@ -1052,18 +1224,17 @@ Julio
|
|
|
|
|
|
|
|
|
|
<p><pre><code data-trim> |
|
|
|
|
>>> from functools import wrap |
|
|
|
|
>>> |
|
|
|
|
>>> def check(func): |
|
|
|
|
>>> def check_conn(*args, **kwargs): |
|
|
|
|
>>> # acha a conexão em args ou kwargs |
|
|
|
|
>>> if not connection.is_connected: |
|
|
|
|
>>> connection.retry() |
|
|
|
|
>>> return func(*args, **kwargs) |
|
|
|
|
>>> return check_conn |
|
|
|
|
>>> |
|
|
|
|
... def check_conn(*args, **kwargs): |
|
|
|
|
... # acha a conexão em args ou kwargs |
|
|
|
|
... if not connection.is_connected: |
|
|
|
|
... connection.retry() |
|
|
|
|
... return func(*args, **kwargs) |
|
|
|
|
... return check_conn |
|
|
|
|
|
|
|
|
|
>>> @check |
|
|
|
|
>>> def retrieve(connection): |
|
|
|
|
>>> # faz algo com a conexão para recuperar dados |
|
|
|
|
... # faz algo com a conexão para recuperar dados |
|
|
|
|
</code></pre></p> |
|
|
|
|
|
|
|
|
|
<p>Não precisa alterar nenhuma chamada de <code>retrieve</code>.</p> |
|
|
|
@ -1078,22 +1249,22 @@ Julio
|
|
|
|
|
|
|
|
|
|
<pre><code class="hljs"> |
|
|
|
|
>>> class CheckConn(object): |
|
|
|
|
>>> def __init__(self, func): |
|
|
|
|
>>> self.func = func |
|
|
|
|
>>> |
|
|
|
|
>>> def __call__(self, *args, **kwargs): |
|
|
|
|
>>> if 'connection' in kwargs: |
|
|
|
|
>>> connection = kwargs['connection'] |
|
|
|
|
>>> else: |
|
|
|
|
>>> connection = args[0] |
|
|
|
|
>>> |
|
|
|
|
>>> if not connection.is_connected: |
|
|
|
|
>>> connection.retry() |
|
|
|
|
>>> self.func(*args, **kwargs) |
|
|
|
|
... def __init__(self, func): |
|
|
|
|
... self.func = func |
|
|
|
|
... |
|
|
|
|
... def __call__(self, *args, **kwargs): |
|
|
|
|
... if 'connection' in kwargs: |
|
|
|
|
... connection = kwargs['connection'] |
|
|
|
|
... else: |
|
|
|
|
... connection = args[0] |
|
|
|
|
... |
|
|
|
|
... if not connection.is_connected: |
|
|
|
|
... connection.retry() |
|
|
|
|
... self.func(*args, **kwargs) |
|
|
|
|
|
|
|
|
|
>>> @CheckCon |
|
|
|
|
>>> def retrieve(connection): |
|
|
|
|
>>> # retrieve |
|
|
|
|
... # retrieve |
|
|
|
|
</code></pre> |
|
|
|
|
</section> |
|
|
|
|
|
|
|
|
@ -1111,16 +1282,16 @@ Julio
|
|
|
|
|
|
|
|
|
|
<pre><code class='hljs'> |
|
|
|
|
>>> class CheckConn(object): |
|
|
|
|
>>> def __init__(self, func): |
|
|
|
|
>>> self._func = func |
|
|
|
|
>>> |
|
|
|
|
>>> @property |
|
|
|
|
>>> def func(self): |
|
|
|
|
>>> return self._func |
|
|
|
|
>>> |
|
|
|
|
>>> @func.setter |
|
|
|
|
>>> def func(self, value): |
|
|
|
|
>>> self._func = func |
|
|
|
|
... def __init__(self, func): |
|
|
|
|
... self._func = func |
|
|
|
|
... |
|
|
|
|
... @property |
|
|
|
|
... def func(self): |
|
|
|
|
... return self._func |
|
|
|
|
... |
|
|
|
|
... @func.setter |
|
|
|
|
... def func(self, value): |
|
|
|
|
... self._func = func |
|
|
|
|
</code></pre> |
|
|
|
|
</section> |
|
|
|
|
|
|
|
|
@ -1129,12 +1300,12 @@ Julio
|
|
|
|
|
|
|
|
|
|
<pre><code class='hljs'> |
|
|
|
|
>>> class CheckConn(object): |
|
|
|
|
>>> def __init__(self, func): |
|
|
|
|
>>> self._func = func |
|
|
|
|
>>> |
|
|
|
|
>>> @staticmethod |
|
|
|
|
>>> def from_text(self, text): |
|
|
|
|
>>> return CheckConn(getattr(self, text)) |
|
|
|
|
... def __init__(self, func): |
|
|
|
|
... self._func = func |
|
|
|
|
... |
|
|
|
|
... @staticmethod |
|
|
|
|
... def from_text(self, text): |
|
|
|
|
... return CheckConn(getattr(self, text)) |
|
|
|
|
</code></pre> |
|
|
|
|
</section> |
|
|
|
|
</section> |
|
|
|
@ -1228,8 +1399,8 @@ Julio
|
|
|
|
|
|
|
|
|
|
<pre><code class="hljs"> |
|
|
|
|
>>> def gen(max_value): |
|
|
|
|
>>> for value in xrange(max_value): |
|
|
|
|
>>> yield value * 2 |
|
|
|
|
... for value in xrange(max_value): |
|
|
|
|
... yield value * 2 |
|
|
|
|
</code></pre> |
|
|
|
|
|
|
|
|
|
<p>Generator functions não podem ter <code>return</code>!</p> |
|
|
|
@ -1251,23 +1422,23 @@ Julio
|
|
|
|
|
|
|
|
|
|
<pre><code class="hljs"> |
|
|
|
|
>>> class Connection(object): |
|
|
|
|
>>> def __init__(self): |
|
|
|
|
>>> self._conn = None |
|
|
|
|
>>> |
|
|
|
|
>>> def __enter__(self): |
|
|
|
|
>>> self._conn = self._make_connection() |
|
|
|
|
>>> return self._conn |
|
|
|
|
>>> |
|
|
|
|
>>> def __exit__(self, exc_type, exc_value, traceback): |
|
|
|
|
>>> self._conn.close() |
|
|
|
|
>>> if exc_type: # then exc_value and traceback |
|
|
|
|
>>> print "Exception!", exc_type, exc_value |
|
|
|
|
>>> print traceback |
|
|
|
|
... def __init__(self): |
|
|
|
|
... self._conn = None |
|
|
|
|
... |
|
|
|
|
... def __enter__(self): |
|
|
|
|
... self._conn = self._make_connection() |
|
|
|
|
... return self._conn |
|
|
|
|
... |
|
|
|
|
... def __exit__(self, exc_type, exc_value, traceback): |
|
|
|
|
... self._conn.close() |
|
|
|
|
... if exc_type: # then exc_value and traceback |
|
|
|
|
... print "Exception!", exc_type, exc_value |
|
|
|
|
... print traceback |
|
|
|
|
</code></pre> |
|
|
|
|
|
|
|
|
|
<pre><code class="hljs"> |
|
|
|
|
>>> with Connection() as connection: |
|
|
|
|
>>> connection.request('Value') |
|
|
|
|
... connection.request('Value') |
|
|
|
|
</code></pre> |
|
|
|
|
</section> |
|
|
|
|
|
|
|
|
@ -1276,12 +1447,12 @@ Julio
|
|
|
|
|
|
|
|
|
|
<pre><code class="hljs"> |
|
|
|
|
>>> try: |
|
|
|
|
>>> conn = self._make_connection() |
|
|
|
|
>>> conn.request('value') |
|
|
|
|
>>> finally: |
|
|
|
|
>>> conn.close() |
|
|
|
|
>>> except Exception as exc: # Bare exceptions are BAAAADDD! |
|
|
|
|
>>> print 'Exception!', exc |
|
|
|
|
... conn = self._make_connection() |
|
|
|
|
... conn.request('value') |
|
|
|
|
... finally: |
|
|
|
|
... conn.close() |
|
|
|
|
... except Exception as exc: # Bare exceptions are BAAAADDD! |
|
|
|
|
... print 'Exception!', exc |
|
|
|
|
</code></pre> |
|
|
|
|
</section> |
|
|
|
|
|
|
|
|
@ -1296,8 +1467,8 @@ Julio
|
|
|
|
|
|
|
|
|
|
<pre><code class="hljs"> |
|
|
|
|
>>> def func(a): |
|
|
|
|
>>> """Função mágica""" |
|
|
|
|
>>> return a |
|
|
|
|
... """Função mágica""" |
|
|
|
|
... return a |
|
|
|
|
|
|
|
|
|
>>> print func.__doc__ |
|
|
|
|
Função mágica |
|
|
|
|