-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPokemon.py
More file actions
83 lines (63 loc) · 2.13 KB
/
Pokemon.py
File metadata and controls
83 lines (63 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import requests
from collections.abc import Iterator
class BasePokemon:
name: str
def __init__(self,name: str) -> None:
self.name = name
def __str__(self):
return f'name={self.name}'
class Pokemon(BasePokemon):
name: str
id: str
height: str
weight: str
def __init__(self, name: str, id: str, height: str, weight: str) -> None:
BasePokemon.__init__(self, name)
self.__name = self.name
self.__id = id
self.__height = height
self.__weight = weight
@property
def names(self):
return self.__name
@property
def id(self):
return self.__id
@property
def height(self):
return self.__height
@property
def weight(self):
return self.__weight
def __str__(self):
return f'name={self.__name}, id={self.__id}, height={self.__height}, weight={self.__weight}'
class PokeAPI:
name_id: str
max_counter: int
def get_pokemon(self, name_id: str) -> Pokemon:
for i in range(len(pokeball)):
if pokeball[i].name == name_id or pokeball[i].id == name_id:
return pokeball[i]
else:
url = "https://pokeapi.co/api/v2/pokemon/" + name_id + "/"
result = requests.get(url).json()
pokeball.append(Pokemon(result["name"], result["id"], result["height"], result["weight"]))
return Pokemon(result["name"], result["id"], result["height"], result["weight"])
def get_all(self, max_counter: int, get_full=False) -> Iterator:
counter: int = 1
while counter <= max_counter:
url = "https://pokeapi.co/api/v2/pokemon/" + str(counter) + "/"
result = requests.get(url).json()
if get_full == False:
yield BasePokemon(result["name"])
else:
yield Pokemon(result["name"], result["id"], result["height"], result["weight"])
counter += 1
pokeball = []
maxx_weight: int = 0
print(PokeAPI().get_pokemon("ditto"))
for i in PokeAPI().get_all(50, True):
if maxx_weight < i.weight:
maxx_weight = i.weight
pok = i
print(pok)