Skip to content

Commit

Permalink
precompile regex
Browse files Browse the repository at this point in the history
  • Loading branch information
mzuzek committed Oct 3, 2023
1 parent bd4c893 commit 7106b2b
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions pandas/io/excel/_odfreader.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@

from pandas._libs.tslibs.nattype import NaTType

# ODF variant of ISO 8601 time/duration format: "PThhhHmmMss.sssS"
# see https://www.w3.org/TR/xmlschema-2/#duration for details
ODF_ISOTIME_PATTERN = re.compile(
r"^\s*PT\s*(\d+)\s*H\s*(\d+)\s*M\s*(\d+)(\.(\d+))?\s*S$"
)


@doc(storage_options=_shared_docs["storage_options"])
class ODFReader(BaseExcelReader["OpenDocument"]):
Expand Down Expand Up @@ -186,10 +192,9 @@ def _get_column_repeat(self, cell) -> int:

def _parse_odf_time(self, value: str) -> datetime.time:
"""
Helper function to convert ODF variant of ISO 8601 formatted duration
"PnYnMnDTnHnMnS" - see https://www.w3.org/TR/xmlschema-2/#duration
This helper function parses ODF time value
"""
parts = re.match(r"^\s*PT\s*(\d+)\s*H\s*(\d+)\s*M\s*(\d+(\.\d+)?)\s*S$", value)
parts = ODF_ISOTIME_PATTERN.match(value)
if parts is None:
raise ValueError(f"Failed to parse ODF time value: {value}")
hours, minutes, seconds, _, second_part = parts.group(*range(1, 6))
Expand Down

0 comments on commit 7106b2b

Please sign in to comment.