monkey_wrench.date_time._parser module

class monkey_wrench.date_time._parser.DateTimeParserBase[source]

Bases: object

A static base class for parsing items, e.g. product IDs or file paths, into datetime objects.

static _raise_value_error(item: Any) Never[source]

Helper function to raise a ValueError when the given item cannot be parsed.

static parse_by_regex(item: str, regex: str, timezone: ZoneInfo | None = None) datetime[source]

Parse the given item into a datetime object using a regular expression.

Parameters:
  • item – The item to parse.

  • regex – The regular expression to match against.

  • timezone – The timezone to add to the datetime object. Defaults to None, which means UTC will be used.

Returns:

The parsed datetime object, if successful.

Raises:

ValueError – If the given item cannot be parsed.

Example

>>> regex = r"^(19|20\d{2})(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])_(0\d|1\d|2[0-3])_([0-5]\d)$"
>>> DateTimeParserBase.parse_by_regex("20230102_22_30", regex)
datetime.datetime(2023, 1, 2, 22, 30, tzinfo=zoneinfo.ZoneInfo(key='UTC'))
static parse_by_format_string(datetime_string: str, datetime_format_string: str, timezone: ZoneInfo | None = None) datetime[source]

Parse the given datetime string into a datetime object using the given format string.

Parameters:
  • datetime_string – The datetime string to parse.

  • datetime_format_string – The format string using which the parsing is done, e.g. "%Y%m%d_%H_%M".

  • timezone – The timezone to add to the datetime object. Defaults to None, which means UTC will be used.

Returns:

The parsed datetime object, if successful.

Raises:

ValueError – If the given datetime string cannot be parsed.

Example

>>> DateTimeParserBase.parse_by_format_string("20230101_22_30", "%Y%m%d_%H_%M")
datetime.datetime(2023, 1, 1, 22, 30, tzinfo=zoneinfo.ZoneInfo(key='UTC'))
classmethod parse_collection(items: list[Any] | set[Any] | tuple[Any, ...] | Generator) list[datetime] | set[datetime] | tuple[datetime, ...] | Generator[datetime, None, None][source]

Parse the given collection of items into a collection of datetime objects.

Parameters:

items – The collection (list/set/tuple or generator) of items to parse.

Returns:

A collection of datetime objects. The type of collection matches the type of the input collection, e.g. a list as input results in a list of datetime objects.

abstractmethod static parse(item: Any) datetime[source]

Parse the given item into a datetime object.

Warning

This is an abstract static method and needs to be implemented for each derived class.

class monkey_wrench.date_time._parser.SeviriIDParser[source]

Bases: DateTimeParserBase

Static parser class for SEVIRI product IDs.

regex = '[0-9A-Za-z]+-SEVI-[0-9A-Za-z]+-[0-9]+-NA-([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})[0-9]{2}\\.[0-9]+Z-NA'
static parse(seviri_product_id: str) datetime[source]

Parse the given SEVIRI product ID into a datetime object.

Example

>>> SeviriIDParser.parse("MSG3-SEVI-MSG15-0100-NA-20150731221240.036000000Z-NA")
datetime.datetime(2015, 7, 31, 22, 12, tzinfo=zoneinfo.ZoneInfo(key='UTC'))
class monkey_wrench.date_time._parser.FilePathParser[source]

Bases: DateTimeParserBase

Static parser class for file paths.

regex = '[0-9A-Za-z]+_([0-9]{4})([0-9]{2})([0-9]{2})_([0-9]{2})_([0-9]{2})'
static parse(filepath: Path | str) datetime[source]

Parse the given filepath into a datetime object.

Parameters:

filepath – The filepath to parse. It can be either an absolute path or a relative path (e.g. just the base name). For the parsing to be successful, the filepath must have the following format: <optional_path><prefix>_<YYYY>_<mm>_<DD>_<HH>_<MM><optional_extension>, where <prefix> is mandatory but can be anything except for an empty string. See the examples below.

Examples

>>> # Input is an absolute path of type `Path`.
>>> FilePathParser.parse(Path("/home/user/dir/seviri_20150731_22_12.extension"))
datetime.datetime(2015, 7, 31, 22, 12, tzinfo=zoneinfo.ZoneInfo(key='UTC'))
>>> # Input is a relative path of type `Path`.
>>> FilePathParser.parse(Path("chimp_20150731_22_12.extension"))
datetime.datetime(2015, 7, 31, 22, 12, tzinfo=zoneinfo.ZoneInfo(key='UTC'))
>>> # Input is an absolute path of type `str`.
>>> FilePathParser.parse("/home/user/dir/prefix_20150731_22_12.extension")
datetime.datetime(2015, 7, 31, 22, 12, tzinfo=zoneinfo.ZoneInfo(key='UTC'))
>>> # Input is a relative path of type `str` and does not have an extension.
>>> FilePathParser.parse("seviri_20150731_22_12")
datetime.datetime(2015, 7, 31, 22, 12, tzinfo=zoneinfo.ZoneInfo(key='UTC'))
>>> # Input is a relative path of type `str` and its extension is numeric, i.e. `72`.
>>> FilePathParser.parse("p_20150731_22_1272")
datetime.datetime(2015, 7, 31, 22, 12, tzinfo=zoneinfo.ZoneInfo(key='UTC'))
>>> # Input is invalid (missing prefix). The following will raise an exception!
>>> # FilePathParser.parse("20150731_22_12")
>>> # Input is invalid (empty prefix). The following will raise an exception!
>>> # FilePathParser.parse("_20150731_22_12")