misc: replace uses of pytz.utc with timezone.utc where it makes sense

This commit is contained in:
Dima Gerasimov 2023-06-09 03:04:54 +01:00 committed by karlicoss
parent c91534b966
commit c12224af74
9 changed files with 24 additions and 42 deletions

View file

@ -5,8 +5,7 @@ Just a demo module for testing and documentation purposes
from .core import Paths, PathIsh from .core import Paths, PathIsh
from typing import Optional from typing import Optional
from datetime import tzinfo from datetime import tzinfo, timezone
import pytz
from my.config import demo as user_config from my.config import demo as user_config
from dataclasses import dataclass from dataclasses import dataclass
@ -16,7 +15,7 @@ from dataclasses import dataclass
class demo(user_config): class demo(user_config):
data_path: Paths data_path: Paths
username: str username: str
timezone: tzinfo = pytz.utc timezone: tzinfo = timezone.utc
external: Optional[PathIsh] = None external: Optional[PathIsh] = None

View file

@ -4,11 +4,9 @@ Github events and their metadata: comments/issues/pull requests
from ..core import __NOT_HPI_MODULE__ from ..core import __NOT_HPI_MODULE__
from datetime import datetime from datetime import datetime, timezone
from typing import Optional, NamedTuple, Iterable, Set, Tuple from typing import Optional, NamedTuple, Iterable, Set, Tuple
import pytz
from ..core import warn_if_empty, LazyLogger from ..core import warn_if_empty, LazyLogger
from ..core.error import Res from ..core.error import Res
@ -48,7 +46,7 @@ def merge_events(*sources: Results) -> Results:
def parse_dt(s: str) -> datetime: def parse_dt(s: str) -> datetime:
# TODO isoformat? # TODO isoformat?
return pytz.utc.localize(datetime.strptime(s, '%Y-%m-%dT%H:%M:%SZ')) return datetime.strptime(s, '%Y-%m-%dT%H:%M:%SZ').replace(tzinfo=timezone.utc)
# experimental way of supportint event ids... not sure # experimental way of supportint event ids... not sure

View file

@ -5,12 +5,11 @@ Google Takeout exports: browsing history, search/youtube/google play activity
from enum import Enum from enum import Enum
import re import re
from pathlib import Path from pathlib import Path
from datetime import datetime from datetime import datetime, timezone
from html.parser import HTMLParser from html.parser import HTMLParser
from typing import List, Optional, Any, Callable, Iterable, Tuple from typing import List, Optional, Any, Callable, Iterable, Tuple
from collections import OrderedDict from collections import OrderedDict
from urllib.parse import unquote from urllib.parse import unquote
import pytz
from ...core.time import abbr_to_timezone from ...core.time import abbr_to_timezone
@ -30,7 +29,7 @@ def parse_dt(s: str) -> datetime:
# old takeouts didn't have timezone # old takeouts didn't have timezone
# hopefully it was utc? Legacy, so no that much of an issue anymore.. # hopefully it was utc? Legacy, so no that much of an issue anymore..
# todo although maybe worth adding timezone from location provider? # todo although maybe worth adding timezone from location provider?
tz = pytz.utc tz = timezone.utc
else: else:
s, tzabbr = s.rsplit(maxsplit=1) s, tzabbr = s.rsplit(maxsplit=1)
tz = abbr_to_timezone(tzabbr) tz = abbr_to_timezone(tzabbr)

View file

@ -17,13 +17,11 @@ from .core.cfg import make_config
config = make_config(lastfm) config = make_config(lastfm)
from datetime import datetime from datetime import datetime, timezone
import json import json
from pathlib import Path from pathlib import Path
from typing import NamedTuple, Sequence, Iterable from typing import NamedTuple, Sequence, Iterable
import pytz
from .core.common import mcachew, Json, get_files from .core.common import mcachew, Json, get_files
@ -44,7 +42,7 @@ class Scrobble(NamedTuple):
@property @property
def dt(self) -> datetime: def dt(self) -> datetime:
ts = int(self.raw['date']) ts = int(self.raw['date'])
return datetime.fromtimestamp(ts, tz=pytz.utc) return datetime.fromtimestamp(ts, tz=timezone.utc)
@property @property
def artist(self) -> str: def artist(self) -> str:

View file

@ -112,9 +112,8 @@ def upvoted() -> Iterator[Upvote]:
from typing import Dict, Iterable, Iterator, NamedTuple from typing import Dict, Iterable, Iterator, NamedTuple
from functools import lru_cache from functools import lru_cache
import pytz
import re import re
from datetime import datetime from datetime import datetime, timezone
from multiprocessing import Pool from multiprocessing import Pool
# TODO hmm. apparently decompressing takes quite a bit of time... # TODO hmm. apparently decompressing takes quite a bit of time...
@ -151,7 +150,7 @@ def _get_bdate(bfile: Path) -> datetime:
stem = stem.replace('T', '').replace('Z', '') # adapt for arctee stem = stem.replace('T', '').replace('Z', '') # adapt for arctee
match = RE.search(stem) match = RE.search(stem)
assert match is not None assert match is not None
bdt = pytz.utc.localize(datetime.strptime(match.group(1), "%Y%m%d%H%M%S")) bdt = datetime.strptime(match.group(1), "%Y%m%d%H%M%S").replace(tzinfo=timezone.utc)
return bdt return bdt

View file

@ -1,14 +1,12 @@
""" """
[[https://roamresearch.com][Roam]] data [[https://roamresearch.com][Roam]] data
""" """
from datetime import datetime from datetime import datetime, timezone
from pathlib import Path from pathlib import Path
from itertools import chain from itertools import chain
import re import re
from typing import NamedTuple, Iterator, List, Optional from typing import NamedTuple, Iterator, List, Optional
import pytz
from .core import get_files, LazyLogger, Json from .core import get_files, LazyLogger, Json
from my.config import roamresearch as config from my.config import roamresearch as config
@ -38,7 +36,7 @@ class Node(NamedTuple):
def created(self) -> datetime: def created(self) -> datetime:
ct = self.raw.get(Keys.CREATED) ct = self.raw.get(Keys.CREATED)
if ct is not None: if ct is not None:
return datetime.fromtimestamp(ct / 1000, tz=pytz.utc) return datetime.fromtimestamp(ct / 1000, tz=timezone.utc)
# ugh. daily notes don't have create time for some reason??? # ugh. daily notes don't have create time for some reason???
title = self.title title = self.title
@ -50,13 +48,13 @@ class Node(NamedTuple):
return self.edited # fallback TODO log? return self.edited # fallback TODO log?
# strip off 'th'/'rd' crap # strip off 'th'/'rd' crap
dts = m.group(1) + ' ' + m.group(2) + ' ' + m.group(3) dts = m.group(1) + ' ' + m.group(2) + ' ' + m.group(3)
dt = datetime.strptime(dts, '%B %d %Y') dt = datetime.strptime(dts, '%B %d %Y').replace(tzinfo=timezone.utc)
return pytz.utc.localize(dt) return dt
@property @property
def edited(self) -> datetime: def edited(self) -> datetime:
rt = self.raw[Keys.EDITED] rt = self.raw[Keys.EDITED]
return datetime.fromtimestamp(rt / 1000, tz=pytz.utc) return datetime.fromtimestamp(rt / 1000, tz=timezone.utc)
@property @property
def title(self) -> Optional[str]: def title(self) -> Optional[str]:

View file

@ -1,23 +1,21 @@
""" """
Feedly RSS reader Feedly RSS reader
""" """
from my.config import feedly as config from my.config import feedly as config
from datetime import datetime, timezone
import json
from pathlib import Path from pathlib import Path
from typing import Sequence from typing import Iterable, Sequence
from ..core.common import listify, get_files from ..core.common import listify, get_files
from .common import Subscription from .common import Subscription, SubscriptionState
def inputs() -> Sequence[Path]: def inputs() -> Sequence[Path]:
return get_files(config.export_path) return get_files(config.export_path)
import json
@listify @listify
def parse_file(f: Path): def parse_file(f: Path):
raw = json.loads(f.read_text()) raw = json.loads(f.read_text())
@ -33,14 +31,9 @@ def parse_file(f: Path):
) )
from datetime import datetime
from typing import Iterable
from .common import SubscriptionState
def states() -> Iterable[SubscriptionState]: def states() -> Iterable[SubscriptionState]:
import pytz
for f in inputs(): for f in inputs():
dts = f.stem.split('_')[-1] dts = f.stem.split('_')[-1]
dt = datetime.strptime(dts, '%Y%m%d%H%M%S') dt = datetime.strptime(dts, '%Y%m%d%H%M%S').replace(tzinfo=timezone.utc)
dt = pytz.utc.localize(dt)
subs = parse_file(f) subs = parse_file(f)
yield dt, subs yield dt, subs

View file

@ -1,5 +1,5 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from datetime import datetime from datetime import datetime, timezone
from itertools import islice from itertools import islice
import pytz import pytz
@ -43,7 +43,7 @@ def test_myactivity_search() -> None:
results = list(read_html(tpath, path)) results = list(read_html(tpath, path))
res = ( res = (
datetime(year=2018, month=12, day=17, hour=8, minute=16, second=18, tzinfo=pytz.utc), datetime(year=2018, month=12, day=17, hour=8, minute=16, second=18, tzinfo=timezone.utc),
'https://en.wikipedia.org/wiki/Emmy_Noether&usg=AFQjCNGrSW-iDnVA2OTcLsG3I80H_a6y_Q', 'https://en.wikipedia.org/wiki/Emmy_Noether&usg=AFQjCNGrSW-iDnVA2OTcLsG3I80H_a6y_Q',
'Emmy Noether - Wikipedia', 'Emmy Noether - Wikipedia',
) )

View file

@ -3,11 +3,9 @@ from my.tests.common import skip_if_not_karlicoss as pytestmark
# should make lazy loading the default.. # should make lazy loading the default..
from datetime import datetime from datetime import datetime, timezone
import json import json
import pytz
def test_tweet() -> None: def test_tweet() -> None:
from my.twitter.archive import Tweet from my.twitter.archive import Tweet
@ -45,7 +43,7 @@ def test_tweet() -> None:
""" """
t = Tweet(json.loads(raw), screen_name='whatever') t = Tweet(json.loads(raw), screen_name='whatever')
assert t.permalink is not None assert t.permalink is not None
assert t.dt == datetime(year=2012, month=8, day=30, hour=7, minute=12, second=48, tzinfo=pytz.utc) assert t.dt == datetime(year=2012, month=8, day=30, hour=7, minute=12, second=48, tzinfo=timezone.utc)
assert t.text == 'this is a test tweet' assert t.text == 'this is a test tweet'
assert t.tid == '2328934829084' assert t.tid == '2328934829084'
assert t.entities is not None assert t.entities is not None