-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from vbuxbaum/cap-introducao
Cap introducao - implementações 14 a 21
- Loading branch information
Showing
8 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
""" | ||
Nesta implementação em Java, o tipo genérico 'Item' implementa o método | ||
compara() que é assegurado pela interface 'Item'. | ||
Como o Python permite que seja feita a implementação do comparador '>' através | ||
do magic method '__gt__', foi decidido pela sua utilização. | ||
""" | ||
|
||
|
||
class Max: | ||
def max(itens): | ||
item_max = itens[0] | ||
for item in itens[1:]: | ||
if item > item_max: | ||
item_max = item | ||
return item_max | ||
|
||
|
||
if __name__ == "__main__": | ||
itens_teste_inteiro = [1, 2, 3, 4, 5] | ||
assert Max.max(itens_teste_inteiro) == 5 | ||
|
||
itens_teste_string = ["zhiuh", "poas", "asd", "basd", "choi"] | ||
assert Max.max(itens_teste_string) == "zhiuh" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
""" | ||
Python não oferece a implementação explícita de interfaces, como Java, mas o | ||
mesmo comportamento pode ser replicado de duas formas principais. | ||
""" | ||
|
||
|
||
from abc import ABC, abstractmethod | ||
|
||
|
||
class Item1(object): | ||
def __gt__(self, outro_item): | ||
raise NotImplementedError() | ||
|
||
|
||
class Item2(ABC): | ||
@abstractmethod | ||
def create_purchase_invoice(self, purchase): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class Item1(object): | ||
def __gt__(self, outro_item): | ||
raise NotImplementedError() | ||
|
||
|
||
class MeuItem(Item1): | ||
def __init__(self, chave): | ||
self.chave = chave | ||
|
||
def __gt__(self, outro_item): | ||
return self.chave > outro_item.chave | ||
|
||
|
||
if __name__ == "__main__": | ||
item_teste_1 = MeuItem(321) | ||
item_teste_2 = MeuItem(123) | ||
|
||
assert item_teste_1 > item_teste_2 | ||
|
||
item_teste_3 = MeuItem('321') | ||
item_teste_4 = MeuItem('123') | ||
|
||
assert item_teste_3 > item_teste_4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
""" | ||
Nesta implementação em Java, o livro apenas testa a implementação 14. Como já | ||
foram implementados testes na implementação 16, a implementação 17 se torna | ||
redundante. | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class Lista: | ||
class Celula: | ||
pass | ||
|
||
|
||
if __name__ == "__main__": | ||
lista_teste = Lista() | ||
celula_teste = Lista().Celula() | ||
|
||
assert isinstance(lista_teste, Lista), type(lista_teste) | ||
assert not isinstance(celula_teste, Lista), type(celula_teste) | ||
assert isinstance(celula_teste, Lista.Celula), type(celula_teste) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
""" | ||
Nesta implementação em Java, o objetivo é ilustrar o uso do objeto 'this'. Em | ||
Python, o objeto equivalente é 'self'. | ||
""" | ||
|
||
|
||
class Conta: | ||
def __init__(self, saldo): | ||
self.saldo = saldo | ||
|
||
|
||
if __name__ == "__main__": | ||
|
||
conta_teste = Conta(123.45) | ||
|
||
assert conta_teste.saldo == 123.45 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class Divisor: | ||
def divisao_except(a, b): | ||
try: | ||
if not b: | ||
raise Exception("Divisão por 0") | ||
return a / b | ||
except Exception as erro: | ||
return str(erro) | ||
|
||
def divisao_raise(a, b): | ||
if not b: | ||
raise Exception("Divisão por 0, raise externo") | ||
return a / b | ||
|
||
|
||
if __name__ == "__main__": | ||
assert Divisor.divisao_except(3, 2) == 1.5 | ||
assert Divisor.divisao_except(3, 0) == "Divisão por 0" | ||
|
||
try: | ||
Divisor.divisao_raise(3, 0) | ||
except Exception as erro: | ||
assert str(erro) == "Divisão por 0, raise externo", erro |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
""" | ||
Implementado no arquivo 20_excecoes.py | ||
""" |