From fed87d58db5482e23978e0e4c123633b25225ea7 Mon Sep 17 00:00:00 2001 From: Vitor Buxbaum Date: Fri, 2 Apr 2021 19:06:34 -0300 Subject: [PATCH 1/9] =?UTF-8?q?1.14=20-=20Implementa=C3=A7=C3=A3o,=20decis?= =?UTF-8?q?=C3=A3o=20e=20asserts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 1_introducao/14_max_generico.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 1_introducao/14_max_generico.py diff --git a/1_introducao/14_max_generico.py b/1_introducao/14_max_generico.py new file mode 100644 index 0000000..0a9ccd4 --- /dev/null +++ b/1_introducao/14_max_generico.py @@ -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" From fb0521aa05240b1b3b01fdf5c73b9e8a7c273ee8 Mon Sep 17 00:00:00 2001 From: Vitor Buxbaum Date: Fri, 2 Apr 2021 19:20:33 -0300 Subject: [PATCH 2/9] =?UTF-8?q?1.15=20-=20Implementa=C3=A7=C3=A3o=20e=20de?= =?UTF-8?q?cis=C3=A3o?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 1_introducao/15_interface_item.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 1_introducao/15_interface_item.py diff --git a/1_introducao/15_interface_item.py b/1_introducao/15_interface_item.py new file mode 100644 index 0000000..4fc8574 --- /dev/null +++ b/1_introducao/15_interface_item.py @@ -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 From 4ec2034ec9e15e25c5632f57ac925cae6565a251 Mon Sep 17 00:00:00 2001 From: Vitor Buxbaum Date: Fri, 2 Apr 2021 19:26:24 -0300 Subject: [PATCH 3/9] =?UTF-8?q?1.16=20-=20Implementa=C3=A7=C3=A3o=20e=20as?= =?UTF-8?q?serts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 1_introducao/16_meu_item.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 1_introducao/16_meu_item.py diff --git a/1_introducao/16_meu_item.py b/1_introducao/16_meu_item.py new file mode 100644 index 0000000..d6221a4 --- /dev/null +++ b/1_introducao/16_meu_item.py @@ -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 From c908101e43aabcd3e3755ed385898b11016b4003 Mon Sep 17 00:00:00 2001 From: Vitor Buxbaum Date: Fri, 2 Apr 2021 19:32:25 -0300 Subject: [PATCH 4/9] =?UTF-8?q?1.17=20-=20Decis=C3=A3o?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 1_introducao/17_encontra_max.py | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 1_introducao/17_encontra_max.py diff --git a/1_introducao/17_encontra_max.py b/1_introducao/17_encontra_max.py new file mode 100644 index 0000000..a6193a5 --- /dev/null +++ b/1_introducao/17_encontra_max.py @@ -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. +""" \ No newline at end of file From 2b2cca3f835d88a74175c193e8b1e110f903a824 Mon Sep 17 00:00:00 2001 From: Vitor Buxbaum Date: Fri, 2 Apr 2021 19:32:47 -0300 Subject: [PATCH 5/9] 1.17 - Lint typo --- 1_introducao/17_encontra_max.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1_introducao/17_encontra_max.py b/1_introducao/17_encontra_max.py index a6193a5..cb5dd66 100644 --- a/1_introducao/17_encontra_max.py +++ b/1_introducao/17_encontra_max.py @@ -2,4 +2,4 @@ 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. -""" \ No newline at end of file +""" From 019710b5808d154fbc08968da95f4858deaefbaa Mon Sep 17 00:00:00 2001 From: Vitor Buxbaum Date: Sat, 3 Apr 2021 10:55:31 -0300 Subject: [PATCH 6/9] =?UTF-8?q?1.18=20-=20Implementa=C3=A7=C3=A3o=20e=20as?= =?UTF-8?q?serts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 1_introducao/18_classe_interna.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 1_introducao/18_classe_interna.py diff --git a/1_introducao/18_classe_interna.py b/1_introducao/18_classe_interna.py new file mode 100644 index 0000000..471e146 --- /dev/null +++ b/1_introducao/18_classe_interna.py @@ -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) From b00d73348294e84c8347219e14126e0dd95f8f53 Mon Sep 17 00:00:00 2001 From: Vitor Buxbaum Date: Sat, 3 Apr 2021 11:02:07 -0300 Subject: [PATCH 7/9] =?UTF-8?q?1.19=20-=20Implementa=C3=A7=C3=A3o=20e=20as?= =?UTF-8?q?sert?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 1_introducao/19_this_self.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 1_introducao/19_this_self.py diff --git a/1_introducao/19_this_self.py b/1_introducao/19_this_self.py new file mode 100644 index 0000000..5c07648 --- /dev/null +++ b/1_introducao/19_this_self.py @@ -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 From 60724ded0ec7f62f1e0bc9ae03b14a1b35b3a26c Mon Sep 17 00:00:00 2001 From: Vitor Buxbaum Date: Sat, 3 Apr 2021 11:17:18 -0300 Subject: [PATCH 8/9] =?UTF-8?q?1.20=20-=20Implementa=C3=A7=C3=A3o=20e=20as?= =?UTF-8?q?serts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 1_introducao/20_excecoes.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 1_introducao/20_excecoes.py diff --git a/1_introducao/20_excecoes.py b/1_introducao/20_excecoes.py new file mode 100644 index 0000000..f051cbb --- /dev/null +++ b/1_introducao/20_excecoes.py @@ -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 From 226012f51ab338f94e85056521368d8e5f00ab67 Mon Sep 17 00:00:00 2001 From: Vitor Buxbaum Date: Sat, 3 Apr 2021 11:18:36 -0300 Subject: [PATCH 9/9] =?UTF-8?q?1.21=20-=20Decis=C3=A3o?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 1_introducao/21_excecoes.py | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 1_introducao/21_excecoes.py diff --git a/1_introducao/21_excecoes.py b/1_introducao/21_excecoes.py new file mode 100644 index 0000000..f40a9e1 --- /dev/null +++ b/1_introducao/21_excecoes.py @@ -0,0 +1,3 @@ +""" +Implementado no arquivo 20_excecoes.py +"""