Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adicionado exemplo do Timer #2

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
9 changes: 3 additions & 6 deletions baralho.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '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]
Expand All @@ -16,7 +17,3 @@ def __len__(self):

def __getitem__(self, pos):
return self.cartas[pos]




6 changes: 2 additions & 4 deletions caes/cao.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# coding: utf-8

class Mamifero(object):
class Mamifero:
"""vertebrados dotados de glândulas mamárias"""


Expand Down Expand Up @@ -36,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
Expand Down
5 changes: 4 additions & 1 deletion carta.py
Original file line number Diff line number Diff line change
@@ -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', '♣'))
Empty file added exemplos/__init__.py
Empty file.
25 changes: 25 additions & 0 deletions exemplos/aluno.py
Original file line number Diff line number Diff line change
@@ -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
62 changes: 62 additions & 0 deletions exemplos/bicicleta.py
Original file line number Diff line number Diff line change
@@ -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())
36 changes: 36 additions & 0 deletions exemplos/timer.py
Original file line number Diff line number Diff line change
@@ -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()
44 changes: 44 additions & 0 deletions exemplos/vetor.py
Original file line number Diff line number Diff line change
@@ -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)
17 changes: 17 additions & 0 deletions exercicios/arvore.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Arvore:
'''
>>> for noh in Arvore(0, Arvore(-2, Arvore(-4), Arvore(-1)), Arvore(
... 10)):
... print(noh)
-4
-2
-1
0
10

'''

def __init__(self, valor, esquerda=None, direita=None):
self.valor = valor
self.esquerda = esquerda
self.direita = direita
22 changes: 22 additions & 0 deletions exercicios/matriz.md
Original file line number Diff line number Diff line change
@@ -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]])

```

2 changes: 1 addition & 1 deletion relogio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
13 changes: 13 additions & 0 deletions trens/README.md
Original file line number Diff line number Diff line change
@@ -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

```
Empty file added trens/__init__.py
Empty file.
11 changes: 11 additions & 0 deletions trens/trem_exp_gen.py
Original file line number Diff line number Diff line change
@@ -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)
12 changes: 12 additions & 0 deletions trens/trem_gen.py
Original file line number Diff line number Diff line change
@@ -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)
24 changes: 24 additions & 0 deletions trens/trem_gof.py
Original file line number Diff line number Diff line change
@@ -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)
17 changes: 17 additions & 0 deletions trens/trem_seq.py
Original file line number Diff line number Diff line change
@@ -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)