From c12224af7427ec8eb25f673eb3a4729243c95577 Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Fri, 9 Jun 2023 03:04:54 +0100 Subject: [PATCH] misc: replace uses of pytz.utc with timezone.utc where it makes sense --- my/demo.py | 5 ++--- my/github/common.py | 6 ++---- my/google/takeout/html.py | 5 ++--- my/lastfm.py | 6 ++---- my/reddit/rexport.py | 5 ++--- my/roamresearch.py | 12 +++++------- my/rss/feedly.py | 17 +++++------------ tests/takeout.py | 4 ++-- tests/tweets.py | 6 ++---- 9 files changed, 24 insertions(+), 42 deletions(-) diff --git a/my/demo.py b/my/demo.py index 1023795..75954d6 100644 --- a/my/demo.py +++ b/my/demo.py @@ -5,8 +5,7 @@ Just a demo module for testing and documentation purposes from .core import Paths, PathIsh from typing import Optional -from datetime import tzinfo -import pytz +from datetime import tzinfo, timezone from my.config import demo as user_config from dataclasses import dataclass @@ -16,7 +15,7 @@ from dataclasses import dataclass class demo(user_config): data_path: Paths username: str - timezone: tzinfo = pytz.utc + timezone: tzinfo = timezone.utc external: Optional[PathIsh] = None diff --git a/my/github/common.py b/my/github/common.py index 6114045..e54bc4d 100644 --- a/my/github/common.py +++ b/my/github/common.py @@ -4,11 +4,9 @@ Github events and their metadata: comments/issues/pull requests from ..core import __NOT_HPI_MODULE__ -from datetime import datetime +from datetime import datetime, timezone from typing import Optional, NamedTuple, Iterable, Set, Tuple -import pytz - from ..core import warn_if_empty, LazyLogger from ..core.error import Res @@ -48,7 +46,7 @@ def merge_events(*sources: Results) -> Results: def parse_dt(s: str) -> datetime: # 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 diff --git a/my/google/takeout/html.py b/my/google/takeout/html.py index d4d6830..c01788d 100644 --- a/my/google/takeout/html.py +++ b/my/google/takeout/html.py @@ -5,12 +5,11 @@ Google Takeout exports: browsing history, search/youtube/google play activity from enum import Enum import re from pathlib import Path -from datetime import datetime +from datetime import datetime, timezone from html.parser import HTMLParser from typing import List, Optional, Any, Callable, Iterable, Tuple from collections import OrderedDict from urllib.parse import unquote -import pytz from ...core.time import abbr_to_timezone @@ -30,7 +29,7 @@ def parse_dt(s: str) -> datetime: # old takeouts didn't have timezone # hopefully it was utc? Legacy, so no that much of an issue anymore.. # todo although maybe worth adding timezone from location provider? - tz = pytz.utc + tz = timezone.utc else: s, tzabbr = s.rsplit(maxsplit=1) tz = abbr_to_timezone(tzabbr) diff --git a/my/lastfm.py b/my/lastfm.py index ffec05c..97c112c 100644 --- a/my/lastfm.py +++ b/my/lastfm.py @@ -17,13 +17,11 @@ from .core.cfg import make_config config = make_config(lastfm) -from datetime import datetime +from datetime import datetime, timezone import json from pathlib import Path from typing import NamedTuple, Sequence, Iterable -import pytz - from .core.common import mcachew, Json, get_files @@ -44,7 +42,7 @@ class Scrobble(NamedTuple): @property def dt(self) -> datetime: ts = int(self.raw['date']) - return datetime.fromtimestamp(ts, tz=pytz.utc) + return datetime.fromtimestamp(ts, tz=timezone.utc) @property def artist(self) -> str: diff --git a/my/reddit/rexport.py b/my/reddit/rexport.py index b1f9e3b..2d2b9a3 100644 --- a/my/reddit/rexport.py +++ b/my/reddit/rexport.py @@ -112,9 +112,8 @@ def upvoted() -> Iterator[Upvote]: from typing import Dict, Iterable, Iterator, NamedTuple from functools import lru_cache -import pytz import re -from datetime import datetime +from datetime import datetime, timezone from multiprocessing import Pool # 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 match = RE.search(stem) 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 diff --git a/my/roamresearch.py b/my/roamresearch.py index 0c1192f..2fe06d4 100644 --- a/my/roamresearch.py +++ b/my/roamresearch.py @@ -1,14 +1,12 @@ """ [[https://roamresearch.com][Roam]] data """ -from datetime import datetime +from datetime import datetime, timezone from pathlib import Path from itertools import chain import re from typing import NamedTuple, Iterator, List, Optional -import pytz - from .core import get_files, LazyLogger, Json from my.config import roamresearch as config @@ -38,7 +36,7 @@ class Node(NamedTuple): def created(self) -> datetime: ct = self.raw.get(Keys.CREATED) 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??? title = self.title @@ -50,13 +48,13 @@ class Node(NamedTuple): return self.edited # fallback TODO log? # strip off 'th'/'rd' crap dts = m.group(1) + ' ' + m.group(2) + ' ' + m.group(3) - dt = datetime.strptime(dts, '%B %d %Y') - return pytz.utc.localize(dt) + dt = datetime.strptime(dts, '%B %d %Y').replace(tzinfo=timezone.utc) + return dt @property def edited(self) -> datetime: rt = self.raw[Keys.EDITED] - return datetime.fromtimestamp(rt / 1000, tz=pytz.utc) + return datetime.fromtimestamp(rt / 1000, tz=timezone.utc) @property def title(self) -> Optional[str]: diff --git a/my/rss/feedly.py b/my/rss/feedly.py index df38435..4611ced 100644 --- a/my/rss/feedly.py +++ b/my/rss/feedly.py @@ -1,23 +1,21 @@ """ Feedly RSS reader """ - from my.config import feedly as config +from datetime import datetime, timezone +import json from pathlib import Path -from typing import Sequence +from typing import Iterable, Sequence from ..core.common import listify, get_files -from .common import Subscription +from .common import Subscription, SubscriptionState def inputs() -> Sequence[Path]: return get_files(config.export_path) -import json - - @listify def parse_file(f: Path): 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]: - import pytz for f in inputs(): dts = f.stem.split('_')[-1] - dt = datetime.strptime(dts, '%Y%m%d%H%M%S') - dt = pytz.utc.localize(dt) + dt = datetime.strptime(dts, '%Y%m%d%H%M%S').replace(tzinfo=timezone.utc) subs = parse_file(f) yield dt, subs diff --git a/tests/takeout.py b/tests/takeout.py index a40e218..cddc684 100644 --- a/tests/takeout.py +++ b/tests/takeout.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -from datetime import datetime +from datetime import datetime, timezone from itertools import islice import pytz @@ -43,7 +43,7 @@ def test_myactivity_search() -> None: results = list(read_html(tpath, path)) 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', 'Emmy Noether - Wikipedia', ) diff --git a/tests/tweets.py b/tests/tweets.py index 763fcef..3545296 100644 --- a/tests/tweets.py +++ b/tests/tweets.py @@ -3,11 +3,9 @@ from my.tests.common import skip_if_not_karlicoss as pytestmark # should make lazy loading the default.. -from datetime import datetime +from datetime import datetime, timezone import json -import pytz - def test_tweet() -> None: from my.twitter.archive import Tweet @@ -45,7 +43,7 @@ def test_tweet() -> None: """ t = Tweet(json.loads(raw), screen_name='whatever') 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.tid == '2328934829084' assert t.entities is not None