Skip to content

Commit 95ebb72

Browse files
authored
Merge pull request #73 from rism-digital/performance-enhancements
Performance enhancements
2 parents 565ab64 + 1c480b0 commit 95ebb72

14 files changed

Lines changed: 419 additions & 346 deletions

File tree

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
runs-on: ubuntu-latest
1919
strategy:
2020
matrix:
21-
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13"]
21+
python-version: ["3.10", "3.11", "3.12", "3.13"]
2222
defaults:
2323
run:
2424
working-directory: .

.github/workflows/coverage_readme.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ jobs:
2222
persist-credentials: false
2323
fetch-depth: 0
2424

25-
- name: Set up Python 3.12
25+
- name: Set up Python 3.13
2626
uses: actions/setup-python@v5
2727
with:
28-
python-version: 3.12
28+
python-version: 3.13
2929
cache: 'pip'
3030
cache-dependency-path: '**/pyproject.toml'
3131

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,5 @@ docs/_build/
6464

6565
# PyBuilder
6666
target/
67+
.idea
68+
.DS_Store

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,6 @@ Since the `EDTFField` and the `_earliest` and `_latest` field values are set aut
514514
* Fix formatting: `ruff format --config pyproject.toml`
515515
* Linting and formatting checks and attempted fixes are also run as precommit hooks if you installed them.
516516

517-
### Coverage and benchmraks
517+
### Coverage and benchmarks
518518

519519
Coverage reports are generated and added as comments to commits, and also visible in the actions log. Benchmarks are run on pull requests and are published [here]( https://ixc.github.io/python-edtf/dev/bench/) and also visible in the actions log.

edtf/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
UncertainOrApproximate,
2323
Unspecified,
2424
UnspecifiedIntervalSection,
25+
is_valid_edtf,
2526
parse_edtf,
2627
)
2728

@@ -46,6 +47,7 @@
4647
"trim_struct_time",
4748
"text_to_edtf",
4849
"parse_edtf",
50+
"is_valid_edtf",
4951
# parser_exceptions
5052
"EDTFParseException",
5153
# parser_classes

edtf/appsettings.py

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
except ImportError:
1313
EDTF = {}
1414

15-
SEASON_MONTHS_RANGE = EDTF.get(
15+
SEASON_MONTHS_RANGE: dict[int, list[int]] = EDTF.get(
1616
"SEASON_MONTHS_RANGE",
1717
{
1818
# season id: [earliest_month, last_month]
@@ -27,7 +27,7 @@
2727
},
2828
)
2929

30-
SEASON_L2_MONTHS_RANGE = EDTF.get(
30+
SEASON_L2_MONTHS_RANGE: dict[int, list[int]] = EDTF.get(
3131
"SEASON_L2_MONTHS_RANGE",
3232
{
3333
# season id: [earliest_month, last_month]
@@ -67,9 +67,9 @@
6767
},
6868
)
6969

70-
DAY_FIRST = EDTF.get("DAY_FIRST", False) # Americans!
70+
DAY_FIRST: bool = EDTF.get("DAY_FIRST", False) # Americans!
7171

72-
SEASONS = EDTF.get(
72+
SEASONS: dict[int, str] = EDTF.get(
7373
"SEASONS",
7474
{
7575
21: "spring",
@@ -78,25 +78,38 @@
7878
24: "winter",
7979
},
8080
)
81-
INVERSE_SEASONS = EDTF.get("INVERSE_SEASONS", {v: k for k, v in SEASONS.items()})
81+
INVERSE_SEASONS: dict[str, int] = EDTF.get(
82+
"INVERSE_SEASONS", {v: k for k, v in SEASONS.items()}
83+
)
8284
# also need to interpret `fall`
8385
INVERSE_SEASONS["fall"] = 23
8486

8587
# changing these will break tests
86-
PADDING_DAY_PRECISION = EDTF.get("PADDING_DAY_PRECISION", relativedelta(days=1))
87-
PADDING_MONTH_PRECISION = EDTF.get("PADDING_MONTH_PRECISION", relativedelta(months=1))
88-
PADDING_YEAR_PRECISION = EDTF.get("PADDING_YEAR_PRECISION", relativedelta(years=1))
89-
PADDING_SEASON_PRECISION = EDTF.get("PADDING_SEASON_PRECISION", relativedelta(weeks=12))
90-
PADDING_DECADE_PRECISION = EDTF.get("PADDING_DECADE_PRECISION", relativedelta(years=10))
91-
PADDING_CENTURY_PRECISION = EDTF.get(
88+
PADDING_DAY_PRECISION: relativedelta = EDTF.get(
89+
"PADDING_DAY_PRECISION", relativedelta(days=1)
90+
)
91+
PADDING_MONTH_PRECISION: relativedelta = EDTF.get(
92+
"PADDING_MONTH_PRECISION", relativedelta(months=1)
93+
)
94+
PADDING_YEAR_PRECISION: relativedelta = EDTF.get(
95+
"PADDING_YEAR_PRECISION", relativedelta(years=1)
96+
)
97+
PADDING_SEASON_PRECISION: relativedelta = EDTF.get(
98+
"PADDING_SEASON_PRECISION", relativedelta(weeks=12)
99+
)
100+
PADDING_DECADE_PRECISION: relativedelta = EDTF.get(
101+
"PADDING_DECADE_PRECISION", relativedelta(years=10)
102+
)
103+
PADDING_CENTURY_PRECISION: relativedelta = EDTF.get(
92104
"PADDING_CENTURY_PRECISION", relativedelta(years=100)
93105
)
94-
PADDING_MILLENNIUM_PRECISION = EDTF.get(
106+
PADDING_MILLENNIUM_PRECISION: relativedelta = EDTF.get(
95107
"PADDING_MILLENNIUM_PRECISION", relativedelta(years=1000)
96108
)
97-
MULTIPLIER_IF_UNCERTAIN = EDTF.get("MULTIPLIER_IF_UNCERTAIN", 1.0)
98-
MULTIPLIER_IF_APPROXIMATE = EDTF.get("MULTIPLIER_IF_APPROXIMATE", 1.0)
99-
MULTIPLIER_IF_BOTH = EDTF.get("MULTIPLIER_IF_BOTH", 2.0)
100-
DELTA_IF_UNKNOWN = EDTF.get("DELTA_IF_UNKNOWN", relativedelta(years=10))
109+
MULTIPLIER_IF_UNCERTAIN: float = EDTF.get("MULTIPLIER_IF_UNCERTAIN", 1.0)
110+
MULTIPLIER_IF_APPROXIMATE: float = EDTF.get("MULTIPLIER_IF_APPROXIMATE", 1.0)
111+
MULTIPLIER_IF_BOTH: float = EDTF.get("MULTIPLIER_IF_BOTH", 2.0)
112+
DELTA_IF_UNKNOWN: relativedelta = EDTF.get("DELTA_IF_UNKNOWN", relativedelta(years=10))
113+
DELTA_IF_EMPTY: relativedelta = relativedelta(None)
101114

102-
DEBUG_PYPARSING = False
115+
DEBUG_PYPARSING: bool = False

edtf/convert.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def old_specs_to_new_specs_expression(expression):
2121
return expression
2222

2323

24-
def dt_to_struct_time(dt):
24+
def dt_to_struct_time(dt) -> struct_time:
2525
"""
2626
Convert a `datetime.date` or `datetime.datetime` to a `struct_time`
2727
representation *with zero values* for data fields that we cannot always
@@ -70,8 +70,7 @@ def trim_struct_time(st: struct_time, strip_time: bool = False) -> struct_time:
7070
"""
7171
if strip_time:
7272
return struct_time(list(st[:3]) + TIME_EMPTY_TIME + TIME_EMPTY_EXTRAS)
73-
else:
74-
return struct_time(list(st[:6]) + TIME_EMPTY_EXTRAS)
73+
return struct_time(list(st[:6]) + TIME_EMPTY_EXTRAS)
7574

7675

7776
def struct_time_to_jd(st: struct_time) -> float:
@@ -116,7 +115,7 @@ def jd_to_struct_time(jd: float) -> struct_time:
116115
return struct_time([year, month, day, hour, minute, second] + TIME_EMPTY_EXTRAS)
117116

118117

119-
def _roll_negative_time_fields(year, month, day, hour, minute, second):
118+
def _roll_negative_time_fields(year, month, day, hour, minute, second) -> tuple:
120119
"""
121120
Fix date/time fields which have nonsense negative values for any field
122121
except for year by rolling the overall date/time value backwards, treating
@@ -152,4 +151,5 @@ def _roll_negative_time_fields(year, month, day, hour, minute, second):
152151
year += int(month / 12.0) # Adjust by whole year in months
153152
year -= 1 # Subtract 1 for negative minutes
154153
month %= 12 # Convert negative month to positive remainder
155-
return (year, month, day, hour, minute, second)
154+
155+
return year, month, day, hour, minute, second

edtf/jdutil.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ def __sub__(self, other):
396396

397397
return jd_to_datetime(combined)
398398

399-
elif isinstance(other, (datetime, dt.datetime)):
399+
elif isinstance(other, datetime | dt.datetime):
400400
diff = datetime_to_jd(self) - datetime_to_jd(other)
401401

402402
return dt.timedelta(diff)
@@ -407,7 +407,7 @@ def __sub__(self, other):
407407
raise TypeError(s)
408408

409409
def __rsub__(self, other):
410-
if not isinstance(other, (datetime, dt.datetime)):
410+
if not isinstance(other, datetime | dt.datetime):
411411
s = "jdutil.datetime supports '-' with: "
412412
s += "jdutil.datetime and datetime.datetime"
413413
raise TypeError(s)

0 commit comments

Comments
 (0)