forked from michabirklbauer/python_template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
408 lines (347 loc) · 11.9 KB
/
Copy pathmain.py
File metadata and controls
408 lines (347 loc) · 11.9 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#!/usr/bin/env python3
#
# /// script
# requires-python = ">=3.12"
# dependencies = [
# "polars>=1.41.2",
# "pydantic>=2.13.4",
# ]
# ///
# PLEASE BE AWARE THAT SCRIPT METADATA WILL OVERRIDE THE PYPROJECT.TOML
# SCRIPT NAME
# 2026 (c) YOUR NAME
# https://github.com/username/
# your.mail@mail.com
r"""Battles two characters.
.. code-block:: text
:caption: Example Usage
usage: main.py [-h] -f FILE [-c1 CHARACTER_1] [-c2 CHARACTER_2] [-hp HEALTH] [--version]
Battles two characters.
options:
-h, --help show this help message and exit
-f, --file FILE character file to read characters from (str).
-c1, --character-1 CHARACTER_1
index of the first character to use (int).
-c2, --character-2 CHARACTER_2
index of the second character to use (int).
-hp, --hit-points HEALTH
health of all characters (int).
--version show program's version number and exit
(c) Micha Birklbauer, 2026
"""
from __future__ import annotations
import argparse
import random
import logging
import polars as pl
from pydantic import BaseModel, Field, ConfigDict, computed_field
from typing import Annotated, Optional, Literal, Any, override
__version = "2.0.0"
__date = "2026-06-05"
logger = logging.getLogger(__name__)
# these examples use the numpy docstring style
# https://numpydoc.readthedocs.io/en/latest/format.html#docstring-standard
class Character(BaseModel):
r"""Core data structure representing a character.
Bases Pydantic `BaseModel <https://pydantic.dev/docs/validation/latest/api/pydantic/base_model/#pydantic.BaseModel>`_.
Attributes Summary
------------------
Here is a short summary about the class attributes, for more details
on the specific Pydantic validation requirements please refer to the corresponding attributes
themselves.
Required
^^^^^^^^
The following attributes are required:
name : str
The name of the character.
Optional
^^^^^^^^
The following attributes are optional:
race : one of "Elf", "Half-Elf", "Human", or None, default = None
The race of the character. Should be one of Elf, Half-Elf, or Human.
min_damage : float, default = 0.0
Minimum damage the character deals.
max_damage : float, default = 0.0
Maximum damage the character deals.
Notes
-----
Minimum and maximum damage are automatically switched depending on which is
greater.
Examples
--------
>>> from main import Character
>>> character = Character(name="John Baldur")
"""
name: Annotated[str, Field(frozen=True, description="Name of the character.")]
r"""
Name of the character.
"""
race: Annotated[
Optional[Literal["Elf", "Half-Elf", "Human"]],
Field(frozen=True, description="Race of the character."),
] = None
r"""
Race of the character. Should be one of Elf, Half-Elf, or Human.
"""
min_damage: Annotated[
float, Field(frozen=False, description="Minimum damage the character deals.")
] = 0.0
r"""
Minimum damage the character deals. Is automatically switched with max_damage
if max_damage is smaller.
"""
max_damage: Annotated[
float, Field(frozen=False, description="Maximum damage the character deals.")
] = 0.0
r"""
Maximum damage the character deals. Is automatically switched with min_damage
if min_damage is greater.
"""
model_config = ConfigDict(
validate_assignment=True, strict=True, str_strip_whitespace=True
)
r"""
Pydantic configuration for the underlying validation model.
"""
@computed_field(description="Average damage dealt by the character.")
@property
def avg_damage(self) -> float:
r"""
Average damage dealt by the character.
"""
return (self.min_damage + self.max_damage) / 2.0
@override
def model_post_init(self, context: Any = None) -> None:
r"""
Performs extra validation and post init functions.
Warnings
--------
This method should not be called manually!
"""
if self.min_damage > self.max_damage:
self.__dict__["min_damage"], self.__dict__["max_damage"] = ( # pyright: ignore[reportIndexIssue]
self.max_damage,
self.min_damage,
)
def __getitem__(self, key: str) -> Any:
r"""
Support for dict-like access.
"""
try:
return getattr(self, key)
except AttributeError as e:
raise KeyError(f"'{key}' is not a valid field!") from e
def __contains__(self, key: str) -> bool:
r"""
Support for ``in`` operator.
"""
return hasattr(self, key)
def copy_with_update(self, update: dict[str, Any] = {}) -> Character:
r"""Creates a deep copy of the class with optional attribute updates.
Parameters
----------
update : dict of str, any, default = empty dict
Dictionary mapping attribute names (str) to their updated values.
The default (empty dict) will create a deep copy with the original
attribute values.
Returns
-------
Character
New character with optionally updated attributes.
Examples
--------
>>> from main import Character
>>> character = Character(name="John Baldur")
>>> new_character = character.copy_with_update(update={"race": "Human"})
"""
return Character(
name=update["name"] if "name" in update else self.name,
race=update["race"] if "race" in update else self.race,
min_damage=update["min_damage"]
if "min_damage" in update
else self.min_damage,
max_damage=update["max_damage"]
if "max_damage" in update
else self.max_damage,
)
def attack(self) -> float:
r"""Get the attack damage of the next attack.
Returns
-------
float
The attack damage of the attack.
Examples
--------
>>> from main import Character
>>> character = Character(name="John Baldur")
>>> character.attack()
0.0
"""
return self.min_damage + (self.max_damage - self.min_damage) * random.random() # noqa: S311
def character_factory(filename: str) -> list[Character]:
r"""Creates a list of characters from a file.
Parameters
----------
filename : str
The filename of the character ``csv`` file.
Returns
-------
lisf of Character
The parsed list of characters.
Examples
--------
>>> from main import character_factory
>>> characters = character_factory("data/characters.csv")
>>> characters[0].name
'Astarion'
"""
df = pl.read_csv(filename)
characters: list[Character] = list()
for row in df.iter_rows(named=True):
characters.append(
Character(
name=str(row["name"]),
race=str(row["race"]) if "race" in row else None, # pyright: ignore[reportArgumentType] # ty: ignore[invalid-argument-type]
min_damage=float(row["min_damage"]),
max_damage=float(row["max_damage"]),
)
)
return characters
def battle(
character_1: Character, character_2: Character, health: float = 100.0
) -> Character:
r"""Makes two characters fight.
Parameters
----------
character_1 : Character
One of the two characters that should battle.
character_2 : Character
One of the two characters that should battle.
health : float, default = 100.0
The amount of hit points both characters have.
Returns
-------
Character
The winner of the two characters.
Examples
--------
>>> from main import character_factory, battle
>>> characters = character_factory("data/characters.csv")
>>> winner = battle(characters[0], characters[1], health=10000)
>>> winner.name
'Shadowheart'
"""
health_1 = health
health_2 = health
initiative = random.random() # noqa: S311
if initiative < 0.5:
logger.info(f"Character {character_1.name} has initiative!")
else:
logger.info(f"Character {character_2.name} has initiative!")
while True:
if initiative < 0.5:
attack: float = character_1.attack()
logger.info(f"Character {character_1.name} deals {attack} damage!")
health_2 -= attack
if health_2 <= 0:
break
attack: float = character_2.attack()
logger.info(f"Character {character_2.name} deals {attack} damage!")
health_1 -= attack
if health_1 <= 0:
break
else:
attack: float = character_2.attack()
logger.info(f"Character {character_2.name} deals {attack} damage!")
health_1 -= attack
if health_1 <= 0:
break
attack: float = character_1.attack()
logger.info(f"Character {character_1.name} deals {attack} damage!")
health_2 -= attack
if health_2 <= 0:
break
if health_1 <= 0:
logger.info(f"Character {character_2.name} won!")
return character_2
logger.info(f"Character {character_1.name} won!")
return character_1
##### MAIN FUNCTION #####
def main(argv: Optional[list[str]] = None) -> int:
"""Main function.
Parameters
----------
argv : list or str, or None, default = None
Arguments passed to argparse.
Returns
-------
int
Exit status (zero is success).
Examples
--------
>>> from main import main
>>> main(["-f", "data/characters.csv"])
INFO:main:Both characters have 130.0 hit points! The battle begins:
INFO:main:Character Shadowheart has initiative!
INFO:main:Character Shadowheart deals 311.13673321167755 damage!
INFO:main:Character Shadowheart won!
0
"""
parser = argparse.ArgumentParser(
prog="main.py",
description="Battles two characters.",
epilog="(c) Micha Birklbauer, 2026",
)
parser.add_argument(
"-f",
"--file",
dest="file",
required=True,
help="character file to read characters from (str).",
type=str,
)
parser.add_argument(
"-c1",
"--character-1",
dest="character_1",
default=0,
help="index of the first character to use (int).",
type=int,
)
parser.add_argument(
"-c2",
"--character-2",
dest="character_2",
default=1,
help="index of the second character to use (int).",
type=int,
)
parser.add_argument(
"-hp",
"--hit-points",
dest="health",
default=130,
help="health of all characters (int).",
type=int,
)
parser.add_argument("--version", action="version", version=__version)
args = parser.parse_args(argv)
logging.basicConfig(level=logging.INFO)
try:
characters = character_factory(args.file)
character_1 = int(args.character_1)
character_2 = int(args.character_2)
health = float(args.health)
logger.info(f"Both characters have {health} hit points! The battle begins:")
if character_1 < 0 or character_1 >= len(characters):
raise IndexError("Character 1 is not a valid index in the character file!")
if character_2 < 0 or character_2 >= len(characters):
raise IndexError("Character 1 is not a valid index in the character file!")
_ = battle(characters[character_1], characters[character_2], health)
except Exception as _e:
logger.exception("An error occurred while running the script!")
return 1
return 0
######## SCRIPT #########
if __name__ == "__main__":
m = main()