Skip to content

Commit

Permalink
Small changes and fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
jenskeiner committed Aug 17, 2023
1 parent 11f663a commit 783e7a3
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions exchange_calendars_extensions/api/changes.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import datetime as dt
from enum import Enum, unique
from functools import reduce
from typing import Union, List, Tuple
from typing import List, Tuple

import pandas as pd
from pydantic import BaseModel, Field, model_validator, validate_call
from pydantic import BaseModel, Field, RootModel, model_validator, validate_call
from pydantic.functional_validators import BeforeValidator
from typing_extensions import Self, Annotated, Literal
from typing_extensions import Literal, Union, Annotated, Dict, Any, Self


@unique
Expand All @@ -27,7 +27,7 @@ class DayType(str, Enum):
QUARTERLY_EXPIRY = 'quarterly_expiry'


def _to_timestamp(value: Union[pd.Timestamp, str]) -> pd.Timestamp:
def _to_timestamp(value: Any) -> pd.Timestamp:
"""
Convert value to Pandas timestamp.
Expand All @@ -50,6 +50,7 @@ def _to_timestamp(value: Union[pd.Timestamp, str]) -> pd.Timestamp:
if not isinstance(value, pd.Timestamp):
try:
# Convert value to timestamp.
# noinspection PyTypeChecker
value = pd.Timestamp(value)
except ValueError as e:
# Failed to convert key to timestamp.
Expand Down Expand Up @@ -101,6 +102,7 @@ def _to_time(value: Union[dt.time, str]):
if not isinstance(value, dt.time):
for f in ('%H:%M', '%H:%M:%S'):
try:
# noinspection PyTypeChecker
value = dt.datetime.strptime(value, f).time()
break
except ValueError:
Expand Down Expand Up @@ -315,3 +317,7 @@ def all_days(self) -> Tuple[pd.Timestamp]:
All days in the changeset.
"""
return tuple(sorted(set(map(lambda x: x.date, self.add)).union(set(self.remove))))


# A type alias for a dictionary of changesets, mapping exchange key to a corresponding change set.
ChangeSetDict = RootModel[Dict[str, ChangeSet]]

0 comments on commit 783e7a3

Please sign in to comment.