-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add boilerplate using generation script
- Loading branch information
unexcellent
committed
Nov 8, 2024
1 parent
55682aa
commit 4e20c49
Showing
6 changed files
with
309 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"""Package used for auto generating the boilerplate in the quantities.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
from __future__ import annotations | ||
|
||
from pathlib import Path | ||
|
||
TARGET_FILE_PATH = Path(__file__).parent.parent / "quantio" / "quantities.py" | ||
|
||
|
||
def generate_boilerplate(quantities_with_fields: dict[str, dict[str, str]]) -> None: | ||
"""Generate the boilerplate parts of the quantity classes.""" | ||
with TARGET_FILE_PATH.open() as target_file: | ||
target_content = target_file.readlines() | ||
|
||
target_content_with_boilerplate = [] | ||
current_class: str | None = None | ||
|
||
current_line_is_part_of_autogenerated_code = False | ||
for line in target_content: | ||
if line.startswith("class "): | ||
current_class = line.split("(")[0][6:] | ||
|
||
if "# --- This part is auto generated. Do not change manually. ---" in line: | ||
current_line_is_part_of_autogenerated_code = True | ||
target_content_with_boilerplate.append(line) | ||
target_content_with_boilerplate.extend( | ||
_generate_init(quantities_with_fields[current_class]) | ||
) | ||
target_content_with_boilerplate.extend( | ||
_generate_properties(current_class, quantities_with_fields[current_class]) | ||
) | ||
|
||
if "# --- End of auto generated part. ---" in line: | ||
current_line_is_part_of_autogenerated_code = False | ||
|
||
if current_line_is_part_of_autogenerated_code: | ||
continue | ||
|
||
target_content_with_boilerplate.append(line) | ||
|
||
for line_number, line in enumerate(target_content_with_boilerplate): | ||
if not line.endswith("\n"): | ||
target_content_with_boilerplate[line_number] += "\n" | ||
|
||
with TARGET_FILE_PATH.open("w") as target_file: | ||
target_file.writelines(target_content_with_boilerplate) | ||
|
||
|
||
def _generate_init(units: dict[str, str]) -> list[str]: | ||
code = [" " * 4 + "def __init__(", " " * 8 + "self,"] | ||
|
||
for unit in units: | ||
code.append(" " * 8 + f"{unit}: float = 0.0,") | ||
|
||
code.append(" " * 4 + ") -> None:") | ||
code.append(" " * 8 + "self._base_value = 0.0") | ||
|
||
for unit, factor in units.items(): | ||
code.append(" " * 8 + f"self._base_value += {unit} * {factor}") | ||
|
||
return code | ||
|
||
|
||
def _generate_properties(current_class: str, units: dict[str, str]) -> list[str]: | ||
code = [] | ||
|
||
for unit, factor in units.items(): | ||
code.append("") | ||
code.append(" " * 4 + "@property") | ||
code.append(" " * 4 + f"def {unit}(self) -> float:") | ||
code.append(" " * 8 + f'"""The {current_class.lower()} in {unit.replace("_", " ")}."""') | ||
code.append(" " * 8 + f"return self._base_value / {factor}") | ||
|
||
return code | ||
|
||
|
||
if __name__ == "__main__": | ||
quantities_with_fields = { | ||
"Area": { | ||
"square_miles": "1609.34**2", | ||
"square_kilometers": "10 ** (3 * 2)", | ||
"square_meters": "10**0", | ||
"square_feet": "0.3048**2", | ||
"square_inches": "0.0254**2", | ||
"square_centimeters": "10 ** (-2 * 2)", | ||
"square_millimeters": "10 ** (-3 * 2)", | ||
"square_micrometers": "10 ** (-6 * 2)", | ||
}, | ||
"Length": { | ||
"miles": "1609.34", | ||
"kilometers": "10**3", | ||
"meters": "10**0", | ||
"feet": "0.3048", | ||
"inches": "0.0254", | ||
"centimeters": "10**-2", | ||
"millimeters": "10**-3", | ||
"micrometers": "10**-6", | ||
}, | ||
"Time": { | ||
"hours": "60 * 60", | ||
"minutes": "60", | ||
"seconds": "1", | ||
"milliseconds": "10**-3", | ||
}, | ||
} | ||
|
||
generate_boilerplate(quantities_with_fields) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from __future__ import annotations | ||
|
||
from ._quantity_base import _QuantityBase | ||
|
||
|
||
class Area(_QuantityBase): | ||
"""The two-dimensional extent of an object.""" | ||
|
||
# --- This part is auto generated. Do not change manually. --- | ||
|
||
# --- End of auto generated part. --- | ||
|
||
|
||
class Length(_QuantityBase): | ||
"""The one-dimensional extent of an object or the distance between two points.""" | ||
|
||
# --- This part is auto generated. Do not change manually. --- | ||
|
||
# --- End of auto generated part. --- | ||
|
||
|
||
class Time(_QuantityBase): | ||
"""The duration of an event.""" | ||
|
||
# --- This part is auto generated. Do not change manually. --- | ||
|
||
# --- End of auto generated part. --- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.