Ron Au commited on
Commit
360c090
1 Parent(s): 5a112e6

fix(typing): Convert to Python <3.9 types 🥲

Browse files

- Because package `youtokentome` only works on 3.8

Files changed (4) hide show
  1. README.md +1 -1
  2. app.py +4 -4
  3. modules/dataset.py +3 -3
  4. modules/details.py +19 -19
README.md CHANGED
@@ -4,7 +4,7 @@ emoji: 🧬
4
  colorFrom: gray
5
  colorTo: green
6
  sdk: gradio
7
- python_version: 3.8.9
8
  app_file: start.py
9
  models: [minimaxir/ai-generated-pokemon-rudalle]
10
  pinned: true
 
4
  colorFrom: gray
5
  colorTo: green
6
  sdk: gradio
7
+ python_version: 3.8.12
8
  app_file: start.py
9
  models: [minimaxir/ai-generated-pokemon-rudalle]
10
  pinned: true
app.py CHANGED
@@ -1,4 +1,4 @@
1
- from typing import Union
2
  from time import gmtime, strftime
3
 
4
  from fastapi import FastAPI
@@ -22,7 +22,7 @@ def index() -> FileResponse:
22
 
23
 
24
  @app.get('/new_card')
25
- def new_card() -> dict[str, Union[Details, str]]:
26
  card_logs.append(strftime('%Y-%m-%dT%H:%M:%SZ', gmtime()))
27
 
28
  details: Details = rand_details()
@@ -34,10 +34,10 @@ def new_card() -> dict[str, Union[Details, str]]:
34
 
35
 
36
  @app.get('/stats')
37
- def stats() -> dict[str, Union[int, object]]:
38
  return get_stats() | {"cards_served": len(card_logs)}
39
 
40
 
41
  @app.get('/logs')
42
- def logs() -> list[str]:
43
  return card_logs
 
1
+ from typing import Dict, List, Union
2
  from time import gmtime, strftime
3
 
4
  from fastapi import FastAPI
 
22
 
23
 
24
  @app.get('/new_card')
25
+ def new_card() -> Dict[str, Union[Details, str]]:
26
  card_logs.append(strftime('%Y-%m-%dT%H:%M:%SZ', gmtime()))
27
 
28
  details: Details = rand_details()
 
34
 
35
 
36
  @app.get('/stats')
37
+ def stats() -> Dict[str, Union[int, object]]:
38
  return get_stats() | {"cards_served": len(card_logs)}
39
 
40
 
41
  @app.get('/logs')
42
+ def logs() -> List[str]:
43
  return card_logs
modules/dataset.py CHANGED
@@ -1,6 +1,6 @@
1
  import os
2
  from random import choices, randint
3
- from typing import cast, Optional, TypedDict
4
  import h5py
5
 
6
 
@@ -12,7 +12,7 @@ h5_file: str = os.path.join(datasets_dir, datasets_file)
12
  class Stats(TypedDict):
13
  size_total: int
14
  size_mb: float
15
- size_counts: dict[str, int]
16
 
17
 
18
  def get_stats(h5_file: str = h5_file) -> Stats:
@@ -24,7 +24,7 @@ def get_stats(h5_file: str = h5_file) -> Stats:
24
  }
25
 
26
 
27
- energy_types: list[str] = ['colorless', 'darkness', 'dragon', 'fairy', 'fighting',
28
  'fire', 'grass', 'lightning', 'metal', 'psychic', 'water']
29
 
30
 
 
1
  import os
2
  from random import choices, randint
3
+ from typing import cast, Dict, List, Optional, TypedDict
4
  import h5py
5
 
6
 
 
12
  class Stats(TypedDict):
13
  size_total: int
14
  size_mb: float
15
+ size_counts: Dict[str, int]
16
 
17
 
18
  def get_stats(h5_file: str = h5_file) -> Stats:
 
24
  }
25
 
26
 
27
+ energy_types: List[str] = ['colorless', 'darkness', 'dragon', 'fairy', 'fighting',
28
  'fire', 'grass', 'lightning', 'metal', 'psychic', 'water']
29
 
30
 
modules/details.py CHANGED
@@ -1,20 +1,20 @@
1
  import random
2
  import json
3
- from typing import cast, Optional, TypedDict, Union
4
 
5
 
6
  class Attack(TypedDict):
7
  name: str
8
- cost: list[str]
9
  convertedEnergyCost: int
10
  damage: str
11
  text: str
12
 
13
 
14
- ListCollection = dict[str, Union[list[str], list[Attack]]]
15
 
16
 
17
- def load_lists(list_names: list[str], base_dir: str = "lists") -> ListCollection:
18
  lists = {}
19
 
20
  for name in list_names:
@@ -27,16 +27,16 @@ def load_lists(list_names: list[str], base_dir: str = "lists") -> ListCollection
27
  def rand_hp() -> int:
28
  # Weights from https://bulbapedia.bulbagarden.net/wiki/HP_(TCG)
29
 
30
- hp_range: list[int] = list(range(30, 340 + 1, 10))
31
 
32
- weights: list[int] = [156, 542, 1264, 1727, 1477, 1232, 1008, 640, 436, 515, 469, 279, 188,
33
  131, 132, 132, 56, 66, 97, 74, 23, 24, 25, 7, 15, 6, 0, 12, 18, 35, 18, 3]
34
 
35
  return random.choices(hp_range, weights)[0]
36
 
37
 
38
  def rand_energy(can_be_none: bool = False) -> Union[str, None]:
39
- types: list[str] = ['colorless', 'darkness', 'dragon', 'fairy', 'fighting',
40
  'fire', 'grass', 'lightning', 'metal', 'psychic', 'water']
41
 
42
  if can_be_none:
@@ -51,19 +51,19 @@ def rand_name(energy_type: str = cast(str, rand_energy())) -> str:
51
  return cast(str, random.choices(lists[energy_type])[0])
52
 
53
 
54
- def rand_species(species: list[str]) -> str:
55
  random_species: str = random.choices(species)[0]
56
 
57
  return f'{random_species.capitalize()}'
58
 
59
 
60
- def rand_length() -> dict[str, int]:
61
  # Weights from https://bulbapedia.bulbagarden.net/wiki/List_of_Pok%C3%A9mon_by_height
62
 
63
- feet_ranges: list[int] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16,
64
  17, 18, 19, 20, 21, 22, 23, 24, 26, 28, 30, 32, 34, 35, 47, 65, 328]
65
 
66
- weights: list[int] = [30, 220, 230, 176, 130, 109, 63, 27, 17, 17, 5, 5, 6,
67
  4, 3, 2, 2, 2, 1, 2, 3, 1, 2, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1]
68
 
69
  return {
@@ -75,7 +75,7 @@ def rand_length() -> dict[str, int]:
75
  def rand_weight() -> str:
76
  # Weights from https://bulbapedia.bulbagarden.net/wiki/List_of_Pok%C3%A9mon_by_weight
77
 
78
- weight_ranges: list[dict[str, int]] = [
79
  {"start": 1, "end": 22},
80
  {"start": 22, "end": 44},
81
  {"start": 44, "end": 55},
@@ -94,7 +94,7 @@ def rand_weight() -> str:
94
  {"start": 903, "end": 2204}]
95
 
96
  # 'weights' as in statistical weightings, not physical mass
97
- weights: list[int] = [271, 145, 53, 204, 57, 122, 1, 11, 57, 28, 7, 34, 4, 17, 5, 31]
98
 
99
  start: int
100
  end: int
@@ -106,7 +106,7 @@ def rand_weight() -> str:
106
 
107
 
108
  def rand_attack(
109
- attacks: list[Attack],
110
  name: str, energy_type: Optional[str],
111
  colorless_only_allowed: bool = False) -> Attack:
112
  random_attack: Attack = random.choices(attacks)[0]
@@ -126,7 +126,7 @@ def rand_attack(
126
  return random_attack
127
 
128
 
129
- def rand_attacks(attacks: list[Attack], name: str, energy_type: Optional[str], n: int = 2) -> list[Attack]:
130
  attack1: Attack = rand_attack(attacks, name, energy_type)
131
 
132
  if n > 1:
@@ -158,9 +158,9 @@ class Details(TypedDict):
158
  hp: int
159
  energy_type: str
160
  species: str
161
- length: dict[str, int]
162
  weight: str
163
- attacks: list[Attack]
164
  weakness: Union[str, None]
165
  resistance: Union[str, None]
166
  retreat: int
@@ -176,10 +176,10 @@ def rand_details() -> Details:
176
  "name": name,
177
  "hp": rand_hp(),
178
  "energy_type": energy_type,
179
- "species": rand_species(cast(list[str], lists["species"])),
180
  "length": rand_length(),
181
  "weight": rand_weight(),
182
- "attacks": cast(list[Attack], rand_attacks(cast(list[Attack], lists["attacks"]), name, energy_type=energy_type)),
183
  "weakness": rand_energy(can_be_none=True),
184
  "resistance": rand_energy(can_be_none=True),
185
  "retreat": rand_retreat(),
 
1
  import random
2
  import json
3
+ from typing import cast, Dict, List, Optional, TypedDict, Union
4
 
5
 
6
  class Attack(TypedDict):
7
  name: str
8
+ cost: List[str]
9
  convertedEnergyCost: int
10
  damage: str
11
  text: str
12
 
13
 
14
+ ListCollection = Dict[str, Union[List[str], List[Attack]]]
15
 
16
 
17
+ def load_lists(list_names: List[str], base_dir: str = "lists") -> ListCollection:
18
  lists = {}
19
 
20
  for name in list_names:
 
27
  def rand_hp() -> int:
28
  # Weights from https://bulbapedia.bulbagarden.net/wiki/HP_(TCG)
29
 
30
+ hp_range: List[int] = list(range(30, 340 + 1, 10))
31
 
32
+ weights: List[int] = [156, 542, 1264, 1727, 1477, 1232, 1008, 640, 436, 515, 469, 279, 188,
33
  131, 132, 132, 56, 66, 97, 74, 23, 24, 25, 7, 15, 6, 0, 12, 18, 35, 18, 3]
34
 
35
  return random.choices(hp_range, weights)[0]
36
 
37
 
38
  def rand_energy(can_be_none: bool = False) -> Union[str, None]:
39
+ types: List[str] = ['colorless', 'darkness', 'dragon', 'fairy', 'fighting',
40
  'fire', 'grass', 'lightning', 'metal', 'psychic', 'water']
41
 
42
  if can_be_none:
 
51
  return cast(str, random.choices(lists[energy_type])[0])
52
 
53
 
54
+ def rand_species(species: List[str]) -> str:
55
  random_species: str = random.choices(species)[0]
56
 
57
  return f'{random_species.capitalize()}'
58
 
59
 
60
+ def rand_length() -> Dict[str, int]:
61
  # Weights from https://bulbapedia.bulbagarden.net/wiki/List_of_Pok%C3%A9mon_by_height
62
 
63
+ feet_ranges: List[int] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16,
64
  17, 18, 19, 20, 21, 22, 23, 24, 26, 28, 30, 32, 34, 35, 47, 65, 328]
65
 
66
+ weights: List[int] = [30, 220, 230, 176, 130, 109, 63, 27, 17, 17, 5, 5, 6,
67
  4, 3, 2, 2, 2, 1, 2, 3, 1, 2, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1]
68
 
69
  return {
 
75
  def rand_weight() -> str:
76
  # Weights from https://bulbapedia.bulbagarden.net/wiki/List_of_Pok%C3%A9mon_by_weight
77
 
78
+ weight_ranges: List[Dict[str, int]] = [
79
  {"start": 1, "end": 22},
80
  {"start": 22, "end": 44},
81
  {"start": 44, "end": 55},
 
94
  {"start": 903, "end": 2204}]
95
 
96
  # 'weights' as in statistical weightings, not physical mass
97
+ weights: List[int] = [271, 145, 53, 204, 57, 122, 1, 11, 57, 28, 7, 34, 4, 17, 5, 31]
98
 
99
  start: int
100
  end: int
 
106
 
107
 
108
  def rand_attack(
109
+ attacks: List[Attack],
110
  name: str, energy_type: Optional[str],
111
  colorless_only_allowed: bool = False) -> Attack:
112
  random_attack: Attack = random.choices(attacks)[0]
 
126
  return random_attack
127
 
128
 
129
+ def rand_attacks(attacks: List[Attack], name: str, energy_type: Optional[str], n: int = 2) -> List[Attack]:
130
  attack1: Attack = rand_attack(attacks, name, energy_type)
131
 
132
  if n > 1:
 
158
  hp: int
159
  energy_type: str
160
  species: str
161
+ length: Dict[str, int]
162
  weight: str
163
+ attacks: List[Attack]
164
  weakness: Union[str, None]
165
  resistance: Union[str, None]
166
  retreat: int
 
176
  "name": name,
177
  "hp": rand_hp(),
178
  "energy_type": energy_type,
179
+ "species": rand_species(cast(List[str], lists["species"])),
180
  "length": rand_length(),
181
  "weight": rand_weight(),
182
+ "attacks": cast(List[Attack], rand_attacks(cast(List[Attack], lists["attacks"]), name, energy_type=energy_type)),
183
  "weakness": rand_energy(can_be_none=True),
184
  "resistance": rand_energy(can_be_none=True),
185
  "retreat": rand_retreat(),