main module#
Battles two characters.
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
- class main.Character(
- *,
- name: str,
- race: Literal['Elf', 'Half-Elf', 'Human'] | None = None,
- min_damage: float = 0.0,
- max_damage: float = 0.0,
Bases:
BaseModelCore data structure representing a character.
Bases 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:
- namestr
The name of the character.
Optional#
The following attributes are optional:
- raceone of “Elf”, “Half-Elf”, “Human”, or None, default = None
The race of the character. Should be one of Elf, Half-Elf, or Human.
- min_damagefloat, default = 0.0
Minimum damage the character deals.
- max_damagefloat, 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")
- attack() float[source]#
Get the attack damage of the next attack.
- Returns:
The attack damage of the attack.
- Return type:
float
Examples
>>> from main import Character >>> character = Character(name="John Baldur") >>> character.attack() 0.0
- property avg_damage: float#
Average damage dealt by the character.
- copy_with_update(update: dict[str, Any] = {}) Character[source]#
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:
New character with optionally updated attributes.
- Return type:
Examples
>>> from main import Character >>> character = Character(name="John Baldur") >>> new_character = character.copy_with_update(update={"race": "Human"})
- max_damage: Annotated[float, Field(frozen=False, description='Maximum damage the character deals.')]#
Maximum damage the character deals. Is automatically switched with min_damage if min_damage is greater.
- min_damage: Annotated[float, Field(frozen=False, description='Minimum damage the character deals.')]#
Minimum damage the character deals. Is automatically switched with max_damage if max_damage is smaller.
- model_config = {'str_strip_whitespace': True, 'strict': True, 'validate_assignment': True}#
Pydantic configuration for the underlying validation model.
- model_post_init(context: Any = None) None[source]#
Performs extra validation and post init functions.
Warning
This method should not be called manually!
- name: Annotated[str, Field(frozen=True, description='Name of the character.')]#
Name of the character.
- race: Annotated[Literal['Elf', 'Half-Elf', 'Human'] | None, Field(frozen=True, description='Race of the character.')]#
Race of the character. Should be one of Elf, Half-Elf, or Human.
- main.battle( ) Character[source]#
Makes two characters fight.
- Parameters:
- Returns:
The winner of the two characters.
- Return type:
Examples
>>> from main import character_factory, battle >>> characters = character_factory("data/characters.csv") >>> winner = battle(characters[0], characters[1], health=10000) >>> winner.name 'Shadowheart'
- main.character_factory(filename: str) list[Character][source]#
Creates a list of characters from a file.
- Parameters:
filename (str) – The filename of the character
csvfile.- Returns:
The parsed list of characters.
- Return type:
lisf of Character
Examples
>>> from main import character_factory >>> characters = character_factory("data/characters.csv") >>> characters[0].name 'Astarion'
- main.main(argv: list[str] | None = None) int[source]#
Main function.
- Parameters:
argv (list or str, or None, default = None) – Arguments passed to argparse.
- Returns:
Exit status (zero is success).
- Return type:
int
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