1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-06-25 00:37:31 +02:00

Use TypedDict for more granular typing

This commit is contained in:
Kelly Brazil
2022-10-15 13:44:44 -07:00
parent 5b8cb497de
commit 11b0863a65

View File

@ -6,7 +6,16 @@ import shutil
from datetime import datetime, timezone from datetime import datetime, timezone
from textwrap import TextWrapper from textwrap import TextWrapper
from functools import lru_cache from functools import lru_cache
from typing import List, Dict, Iterable, Union, Optional, TextIO from typing import List, Dict, Iterable, Union, Optional, TypedDict, TextIO
TSFormatType = TypedDict(
'TSFormatType',
{
'id': int,
'format': str,
'locale': Optional[str]
}
)
def _asciify(string: str) -> str: def _asciify(string: str) -> str:
@ -381,7 +390,7 @@ class timestamp:
If the conversion completely fails, all fields will be None. If the conversion completely fails, all fields will be None.
""" """
formats: tuple[Dict[str, Union[int, str, None]], ...] = ( formats: tuple[TSFormatType, ...] = (
{'id': 1000, 'format': '%a %b %d %H:%M:%S %Y', 'locale': None}, # manual C locale format conversion: Tue Mar 23 16:12:11 2021 or Tue Mar 23 16:12:11 IST 2021 {'id': 1000, 'format': '%a %b %d %H:%M:%S %Y', 'locale': None}, # manual C locale format conversion: Tue Mar 23 16:12:11 2021 or Tue Mar 23 16:12:11 IST 2021
{'id': 1100, 'format': '%a %b %d %H:%M:%S %Y %z', 'locale': None}, # git date output: Thu Mar 5 09:17:40 2020 -0800 {'id': 1100, 'format': '%a %b %d %H:%M:%S %Y %z', 'locale': None}, # git date output: Thu Mar 5 09:17:40 2020 -0800
{'id': 1300, 'format': '%Y-%m-%dT%H:%M:%S.%f%Z', 'locale': None}, # ISO Format with UTC (found in syslog 5424): 2003-10-11T22:14:15.003Z {'id': 1300, 'format': '%Y-%m-%dT%H:%M:%S.%f%Z', 'locale': None}, # ISO Format with UTC (found in syslog 5424): 2003-10-11T22:14:15.003Z
@ -517,10 +526,10 @@ class timestamp:
for fmt in optimized_formats: for fmt in optimized_formats:
try: try:
locale.setlocale(locale.LC_TIME, fmt['locale']) # type: ignore locale.setlocale(locale.LC_TIME, fmt['locale'])
dt = datetime.strptime(normalized_datetime, fmt['format']) # type: ignore dt = datetime.strptime(normalized_datetime, fmt['format'])
timestamp_naive = int(dt.replace(tzinfo=None).timestamp()) timestamp_naive = int(dt.replace(tzinfo=None).timestamp())
timestamp_obj['format'] = fmt['id'] # type: ignore timestamp_obj['format'] = fmt['id']
locale.setlocale(locale.LC_TIME, None) locale.setlocale(locale.LC_TIME, None)
break break
except Exception: except Exception: