From 79ef354b73f2d40e48fb47d51288d8896b3414ac Mon Sep 17 00:00:00 2001 From: renzon Date: Tue, 11 Apr 2017 16:32:17 -0300 Subject: [PATCH 01/16] Adicionado exemplo do Timer Isso fica de acordo com link nos slides do curso --- exemplos/__init__.py | 0 exemplos/timer.py | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 exemplos/__init__.py create mode 100644 exemplos/timer.py diff --git a/exemplos/__init__.py b/exemplos/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/exemplos/timer.py b/exemplos/timer.py new file mode 100644 index 0000000..ae7db19 --- /dev/null +++ b/exemplos/timer.py @@ -0,0 +1,36 @@ +from tkinter import Frame, Label, Button + + +class Timer(Frame): + def __init__(self): + super().__init__() + self.inicio = self.agora = 15 + self.pendente = None # alarme pendente + self.grid() + self.mostrador = Label( + self, width=2, anchor='e', font='Helvetica 120 bold') + self.mostrador.grid(column=0, row=0, sticky='nswe') + self.bt_start = Button(self, text='Start', command=self.start) + self.bt_start.grid(column=0, row=1, sticky='we') + self.atualizar_mostrador() + + def atualizar_mostrador(self): + self.mostrador['text'] = str(self.agora) + + def start(self): + if self.pendente: + self.after_cancel(self.pendente) + self.agora = self.inicio + self.atualizar_mostrador() + self.pendente = self.after(1000, self.tictac) + + def tictac(self): + self.agora -= 1 + self.atualizar_mostrador() + if self.agora > 0: + self.pendente = self.after(1000, self.tictac) + + +if __name__ == '__main__': + timer = Timer() + timer.mainloop() From a25d3fa5624a2d0f4715a02f6bb57de546fa09c5 Mon Sep 17 00:00:00 2001 From: renzon Date: Wed, 12 Apr 2017 21:53:54 -0300 Subject: [PATCH 02/16] Mamifero convertido para Python 3 --- caes/cao.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/caes/cao.py b/caes/cao.py index 43320a6..1ebca74 100644 --- a/caes/cao.py +++ b/caes/cao.py @@ -1,6 +1,4 @@ -# coding: utf-8 - -class Mamifero(object): +class Mamifero: """vertebrados dotados de glândulas mamárias""" From bab096b3ca3bba19b7be941bb1097b153362e897 Mon Sep 17 00:00:00 2001 From: renzon Date: Wed, 12 Apr 2017 22:54:27 -0300 Subject: [PATCH 03/16] =?UTF-8?q?Passando=20rel=C3=B3gio=20para=20Python3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- caes/cao.py | 2 +- relogio.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/caes/cao.py b/caes/cao.py index 1ebca74..11198c5 100644 --- a/caes/cao.py +++ b/caes/cao.py @@ -34,7 +34,7 @@ class Pequines(Cao): """ nervoso = True -class GrandeMixin(object): +class GrandeMixin: """ Mixin: muda o latido""" def latir(self, vezes=1): # faz de conta que cães grandes não mudam diff --git a/relogio.py b/relogio.py index e1ac857..b663600 100644 --- a/relogio.py +++ b/relogio.py @@ -5,7 +5,7 @@ class Relogio(tkinter.Label): def __init__(self): - tkinter.Label.__init__(self) + super().__init__() self.pack() self['text'] = strftime('%H:%M:%S') self['font'] = 'Helvetica 120 bold' From 0570c4b414fc63dabd5896eb6e1f1a8660925878 Mon Sep 17 00:00:00 2001 From: renzon Date: Sun, 16 Apr 2017 12:22:58 -0300 Subject: [PATCH 04/16] =?UTF-8?q?=CF=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- trens/README.md | 13 +++++++++++++ trens/__init__.py | 2 ++ trens/trem_gof.py | 24 ++++++++++++++++++++++++ 3 files changed, 39 insertions(+) create mode 100644 trens/README.md create mode 100644 trens/__init__.py create mode 100644 trens/trem_gof.py diff --git a/trens/README.md b/trens/README.md new file mode 100644 index 0000000..e083e1b --- /dev/null +++ b/trens/README.md @@ -0,0 +1,13 @@ +#Esse arquvivo documeta o comportamento de um Trem + +```python + >>> from trens.trem_gof import Trem + >>> t = Trem(4) + >>> for vagao in t: + ... print(vagao) + vagao #1 + vagao #2 + vagao #3 + vagao #4 + +``` \ No newline at end of file diff --git a/trens/__init__.py b/trens/__init__.py new file mode 100644 index 0000000..ef12d17 --- /dev/null +++ b/trens/__init__.py @@ -0,0 +1,2 @@ +# -*- coding: utf-8 -*- +from __future__ import absolute_import, unicode_literals \ No newline at end of file diff --git a/trens/trem_gof.py b/trens/trem_gof.py new file mode 100644 index 0000000..1367f49 --- /dev/null +++ b/trens/trem_gof.py @@ -0,0 +1,24 @@ +class Trem: + def __init__(self, num_vagoes): + self.num_vagoes = num_vagoes + + def __iter__(self): + return IteradorTrem(self.num_vagoes) + + +class IteradorTrem: + def __init__(self, num_vagoes): + self.atual = 0 + self.ultimo_vagao = num_vagoes - 1 + + def __next__(self): + if self.atual <= self.ultimo_vagao: + self.atual += 1 + return 'vagao #%s' % self.atual + else: + raise StopIteration() + + +if __name__ == '__main__': + for vagao in Trem(4): + print(vagao) From 8cf0feec5ea8aff3f9dcf016a645e42b39dcccc2 Mon Sep 17 00:00:00 2001 From: renzon Date: Sun, 16 Apr 2017 19:39:15 -0300 Subject: [PATCH 05/16] Exemplo de Trem com protocolo de sequencia --- trens/__init__.py | 2 -- trens/trem_seq.py | 17 +++++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 trens/trem_seq.py diff --git a/trens/__init__.py b/trens/__init__.py index ef12d17..e69de29 100644 --- a/trens/__init__.py +++ b/trens/__init__.py @@ -1,2 +0,0 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import, unicode_literals \ No newline at end of file diff --git a/trens/trem_seq.py b/trens/trem_seq.py new file mode 100644 index 0000000..cacd7b8 --- /dev/null +++ b/trens/trem_seq.py @@ -0,0 +1,17 @@ +class Trem: + def __init__(self, num_vagoes): + self.num_vagoes = num_vagoes + + def __len__(self): + return self.num_vagoes + + def __getitem__(self, pos): + indice = pos if pos >= 0 else self.num_vagoes + pos + if 0 <= indice < self.num_vagoes: + # indice 2 -> vagao #3 + return 'vagao #%s' % (indice + 1) + raise IndexError('vagao inexistente %s' % pos) + +if __name__ == '__main__': + for vagao in Trem(4): + print(vagao) From 3e99ab955c1fe531c441cb6d56b73a336d460ab0 Mon Sep 17 00:00:00 2001 From: renzon Date: Tue, 18 Apr 2017 14:59:16 -0300 Subject: [PATCH 06/16] =?UTF-8?q?Implementado=20Trem=20com=20uso=20de=20fu?= =?UTF-8?q?n=C3=A7=C3=A3o=20geradora?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- trens/trem_gen.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 trens/trem_gen.py diff --git a/trens/trem_gen.py b/trens/trem_gen.py new file mode 100644 index 0000000..4e7d860 --- /dev/null +++ b/trens/trem_gen.py @@ -0,0 +1,12 @@ +class Trem: + def __init__(self, num_vagoes): + self.num_vagoes = num_vagoes + + def __iter__(self): + for vagao in range(1, self.num_vagoes + 1): + yield 'vagao #%s' % vagao + + +if __name__ == '__main__': + for vagao in Trem(4): + print(vagao) From 8bb55c5440514fbc46ecb47c0f39e944ee35e0b7 Mon Sep 17 00:00:00 2001 From: renzon Date: Tue, 18 Apr 2017 15:20:07 -0300 Subject: [PATCH 07/16] =?UTF-8?q?Implementado=20Trem=20com=20uso=20de=20ex?= =?UTF-8?q?press=C3=A3o=20geradora?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- trens/trem_exp_gen.py | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 trens/trem_exp_gen.py diff --git a/trens/trem_exp_gen.py b/trens/trem_exp_gen.py new file mode 100644 index 0000000..03e62a3 --- /dev/null +++ b/trens/trem_exp_gen.py @@ -0,0 +1,11 @@ +class Trem: + def __init__(self, num_vagoes): + self.num_vagoes = num_vagoes + + def __iter__(self): + return ('vagao #%s' % vagao for vagao in range(1, self.num_vagoes + 1)) + + +if __name__ == '__main__': + for vagao in Trem(4): + print(vagao) From d849195e8c1fe782cb99da8ae36fcba62c46846b Mon Sep 17 00:00:00 2001 From: renzon Date: Wed, 19 Apr 2017 23:10:08 -0300 Subject: [PATCH 08/16] =?UTF-8?q?Exerc=C3=ADcio=20de=20=C3=A1rvore,=20enun?= =?UTF-8?q?ciado?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- exercicios/arvore.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 exercicios/arvore.py diff --git a/exercicios/arvore.py b/exercicios/arvore.py new file mode 100644 index 0000000..df89d3d --- /dev/null +++ b/exercicios/arvore.py @@ -0,0 +1,22 @@ +class Arvore: + ''' + >>> for noh in Arvore(0, Arvore(-2, Arvore(-4), Arvore(-3)), Arvore( + ... 10)): + ... print(noh) + -4 + -1 + -2 + 0 + 10 + + ''' + + def __init__(self, valor, esquerda=None, direita=None): + self.esquerda = esquerda + self.direita = direita + + + + # 0 + # -2 10 + # -4 -1 From e7ad405456d097f68514ec29c3532d088e36a3e9 Mon Sep 17 00:00:00 2001 From: renzon Date: Mon, 24 Apr 2017 20:30:02 -0300 Subject: [PATCH 09/16] Atualizando Baralho com naipes unicode --- baralho.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/baralho.py b/baralho.py index 3d92cbc..edb1f8c 100644 --- a/baralho.py +++ b/baralho.py @@ -4,9 +4,10 @@ Carta = namedtuple('Carta', 'valor naipe') + class Baralho: - valores = [str(n) for n in range(2, 11)] + list('JQKA') - naipes = 'paus ouros copas espadas'.split() + valores = '1 2 3 4 5 6 7 8 9 10 J Q K A'.split() + naipes = '♣ ♢ ♡ ♠'.split() def __init__(self): self.cartas = [Carta(v, n) for n in self.naipes for v in self.valores] @@ -16,7 +17,3 @@ def __len__(self): def __getitem__(self, pos): return self.cartas[pos] - - - - From f4abae9e6251a84ef61fab4152130cdfabc7b6e9 Mon Sep 17 00:00:00 2001 From: renzon Date: Mon, 24 Apr 2017 20:37:05 -0300 Subject: [PATCH 10/16] =?UTF-8?q?Atributo=20valor=20adicionado=20=C3=A0=20?= =?UTF-8?q?Arvore?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- exercicios/arvore.py | 1 + 1 file changed, 1 insertion(+) diff --git a/exercicios/arvore.py b/exercicios/arvore.py index df89d3d..5c0d464 100644 --- a/exercicios/arvore.py +++ b/exercicios/arvore.py @@ -12,6 +12,7 @@ class Arvore: ''' def __init__(self, valor, esquerda=None, direita=None): + self.valor = valor self.esquerda = esquerda self.direita = direita From c9f0c2e56fbb83147fd0db6658c8a381b1eb4db1 Mon Sep 17 00:00:00 2001 From: renzon Date: Mon, 24 Apr 2017 20:40:57 -0300 Subject: [PATCH 11/16] =?UTF-8?q?Arvore=20corrigida=20para=20coincidir=20c?= =?UTF-8?q?om=20=C3=A1rvore=20bin=C3=A1ria=20de=20busca?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- exercicios/arvore.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/exercicios/arvore.py b/exercicios/arvore.py index 5c0d464..5daf184 100644 --- a/exercicios/arvore.py +++ b/exercicios/arvore.py @@ -1,11 +1,11 @@ class Arvore: ''' - >>> for noh in Arvore(0, Arvore(-2, Arvore(-4), Arvore(-3)), Arvore( + >>> for noh in Arvore(0, Arvore(-2, Arvore(-4), Arvore(-1)), Arvore( ... 10)): ... print(noh) -4 - -1 -2 + -1 0 10 @@ -15,9 +15,3 @@ def __init__(self, valor, esquerda=None, direita=None): self.valor = valor self.esquerda = esquerda self.direita = direita - - - - # 0 - # -2 10 - # -4 -1 From 40b8238d2afb3931d16b9fcda7c2ef6f1e07ceeb Mon Sep 17 00:00:00 2001 From: renzon Date: Mon, 24 Apr 2017 21:48:29 -0300 Subject: [PATCH 12/16] Removendo 1 do conjunto de cartas do baralho --- baralho.py | 2 +- carta.py | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/baralho.py b/baralho.py index edb1f8c..bc5a61f 100644 --- a/baralho.py +++ b/baralho.py @@ -6,7 +6,7 @@ class Baralho: - valores = '1 2 3 4 5 6 7 8 9 10 J Q K A'.split() + valores = '2 3 4 5 6 7 8 9 10 J Q K A'.split() naipes = '♣ ♢ ♡ ♠'.split() def __init__(self): diff --git a/carta.py b/carta.py index 4ca0374..40a0494 100644 --- a/carta.py +++ b/carta.py @@ -1,8 +1,11 @@ -class Carta(object): +class Carta: def __init__(self, valor, naipe): self.valor = valor self.naipe = naipe def __repr__(self): return 'Carta(valor=%r, naipe=%r)' % (self.valor, self.naipe) + +if __name__ == '__main__': + print(Carta('A', '♣')) From 91eee5f6af62264ac3b8f445650deafea971bbaf Mon Sep 17 00:00:00 2001 From: renzon Date: Mon, 24 Apr 2017 22:44:12 -0300 Subject: [PATCH 13/16] Adicionando Exemplo do Vetor --- exemplos/vetor.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 exemplos/vetor.py diff --git a/exemplos/vetor.py b/exemplos/vetor.py new file mode 100644 index 0000000..45b02f2 --- /dev/null +++ b/exemplos/vetor.py @@ -0,0 +1,44 @@ +import math + + +class Vetor: + def __init__(self, x, y): + self.y = y + self.x = x + + def __repr__(self) -> str: + return 'Vetor({}, {})'.format(self.x, self.y) + + def __abs__(self): + return math.sqrt(self.x ** 2 + self.y ** 2) + + def __add__(self, vetor): + return Vetor(self.x + vetor.x, self.y + vetor.y) + + def __iadd__(self, other): + self.x += other.x + self.y += other.y + return self + + def __mul__(self, n): + return Vetor(self.x * n, self.y * n) + + def __rmul__(self, n): + return self * n + + +if __name__ == '__main__': + vetor_1 = Vetor(3, 4) + print(vetor_1) + print(abs(vetor_1)) + vetor_2 = Vetor(1, 1) + print(vetor_1 + vetor_2) + print(vetor_1) + print(vetor_2) + print(id(vetor_1)) + vetor_1 += vetor_2 + print(id(vetor_1)) + print(vetor_1) + + print(vetor_1 * 2) + print(2 * vetor_1) From bb0539167aea13a40e84d26483201508c36bdf02 Mon Sep 17 00:00:00 2001 From: renzon Date: Mon, 24 Apr 2017 23:01:37 -0300 Subject: [PATCH 14/16] =?UTF-8?q?Adicionado=20exerc=C3=ADcio=20de=20multip?= =?UTF-8?q?lica=C3=A7=C3=A3o=20de=20Matrizes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- exercicios/matriz.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 exercicios/matriz.md diff --git a/exercicios/matriz.md b/exercicios/matriz.md new file mode 100644 index 0000000..372d7d1 --- /dev/null +++ b/exercicios/matriz.md @@ -0,0 +1,22 @@ +# Criando classe matriz que sobrecarrega multiplicacao de Matrizes + +Deve ser possível criar duas matrizes + +``` python + >>> from exercicios.matriz import Matriz + >>> m1=Matriz([[1] , [2]) # Matriz 2x1 + >>> m2=Matriz([3 , 4]) # Matriz 1x2 + +``` + +Deve ser possível multiplicar Matrizes onde o número de colunas da primeira +é igual ao número de linhas da segunda + + + +``` python + >>> m1 @ m2 + Matriz([[3, 4], [6, 8]]) + +``` + From c67e6ac81d9950d34c46cc0c2e661086aedf7bbc Mon Sep 17 00:00:00 2001 From: renzon Date: Wed, 26 Apr 2017 22:11:14 -0300 Subject: [PATCH 15/16] Exemplo de classe abstrata bicicleta --- exemplos/bicicleta.py | 62 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 exemplos/bicicleta.py diff --git a/exemplos/bicicleta.py b/exemplos/bicicleta.py new file mode 100644 index 0000000..074156b --- /dev/null +++ b/exemplos/bicicleta.py @@ -0,0 +1,62 @@ +import abc + + +class Bicleta(abc.ABC): + _marca = 'Caloi' + + def __init__(self): + self._velocidade = 0 + + @classmethod + def marca(cls): + return cls._marca + + @staticmethod + def rodas(): + return 2; + + @property + def velocidade(self): + print('velocidade') + return self._velocidade + + @velocidade.setter + def velocidade(self, valor): + if valor >= 0: + self._velocidade = valor + else: + self._velocidade = 0 + + @abc.abstractmethod + def pedalar(self): + """Cada classe concreta deve definir o método pedalar com seu + incremento na velocidade""" + + @abc.abstractmethod + def frear(self): + """Cada classe concreta deve definir o método frear com seu + decremento na velocidade""" + + +class Monark(Bicleta): + _marca = 'Monark' + + def pedalar(self): + self.velocidade += 10 + + def frear(self): + self.velocidade -= 3 + + +if __name__ == '__main__': + bicicleta = Monark() + print(Bicleta.marca()) + bicicleta.pedalar() + bicicleta.frear() + bicicleta.frear() + bicicleta.frear() + bicicleta.frear() + bicicleta.velocidade = -4 + print(bicicleta.velocidade) + print(Monark.marca()) + print(Monark.rodas()) From 93607f46e0748a7524352303579f7594141e0e98 Mon Sep 17 00:00:00 2001 From: renzon Date: Wed, 26 Apr 2017 22:21:07 -0300 Subject: [PATCH 16/16] Exemplo de decorador de classe --- exemplos/aluno.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 exemplos/aluno.py diff --git a/exemplos/aluno.py b/exemplos/aluno.py new file mode 100644 index 0000000..31f7b66 --- /dev/null +++ b/exemplos/aluno.py @@ -0,0 +1,25 @@ +import functools + + +@functools.total_ordering +class Aluno: + def __init__(self, nota): + self.nota = nota + + def __eq__(self, other): + return self.nota == other.nota + + def __gt__(self, other): + return self.nota > other.nota + + +if __name__ == '__main__': + aluno_nota_10 = Aluno(10) + outro_aluno_nota_10 = Aluno(10) + aluno_nota_1 = Aluno(1) + print(aluno_nota_10 == outro_aluno_nota_10) # True + print(aluno_nota_10 == aluno_nota_1) # False + print(aluno_nota_10 > outro_aluno_nota_10) # True + print(aluno_nota_10 > aluno_nota_1) # False + print(aluno_nota_10 >= aluno_nota_1) # False + print(aluno_nota_10 <= aluno_nota_1) # False