general: migrate modules to use 3.9 features

This commit is contained in:
Dima Gerasimov 2024-10-19 22:10:40 +01:00 committed by karlicoss
parent d3f9a8e8b6
commit 8496d131e7
125 changed files with 889 additions and 739 deletions

View file

@ -1,11 +1,11 @@
"""
Unified Twitter data (merged from the archive and periodic updates)
"""
from typing import Iterator
from collections.abc import Iterator
from ..core import Res
from ..core.source import import_source
from .common import merge_tweets, Tweet
from .common import Tweet, merge_tweets
# NOTE: you can comment out the sources you don't need
src_twint = import_source(module_name='my.twitter.twint')

View file

@ -4,21 +4,21 @@ Twitter data from official app for Android
from __future__ import annotations
import re
from collections.abc import Iterator, Sequence
from dataclasses import dataclass
from datetime import datetime, timezone
from pathlib import Path
import re
from struct import unpack_from
from typing import Iterator, Sequence, Set
from my.core import datetime_aware, get_files, LazyLogger, Paths, Res
from my.core import LazyLogger, Paths, Res, datetime_aware, get_files
from my.core.common import unique_everseen
from my.core.sqlite import sqlite_connect_immutable
import my.config
from .common import permalink
import my.config # isort: skip
logger = LazyLogger(__name__)
@ -155,7 +155,7 @@ _SELECT_OWN_TWEETS = '_SELECT_OWN_TWEETS'
def get_own_user_id(conn) -> str:
# unclear what's the reliable way to query it, so we use multiple different ones and arbitrate
# NOTE: 'SELECT DISTINCT ev_owner_id FROM lists' doesn't work, might include lists from other people?
res: Set[str] = set()
res: set[str] = set()
# need to cast as it's int by default
for q in [
'SELECT DISTINCT CAST(list_mapping_user_id AS TEXT) FROM list_mapping',
@ -239,7 +239,7 @@ def _process_one(f: Path, *, where: str) -> Iterator[Res[Tweet]]:
NOT (statuses.in_r_user_id == -1 AND statuses.in_r_status_id == -1 AND statuses.conversation_id == 0)
'''
def _query_one(*, where: str, quoted: Set[int]) -> Iterator[Res[Tweet]]:
def _query_one(*, where: str, quoted: set[int]) -> Iterator[Res[Tweet]]:
for (
tweet_id,
user_username,
@ -263,7 +263,7 @@ def _process_one(f: Path, *, where: str) -> Iterator[Res[Tweet]]:
text=content,
)
quoted: Set[int] = set()
quoted: set[int] = set()
yield from _query_one(where=db_where, quoted=quoted)
# get quoted tweets 'recursively'
# TODO maybe do it for favs/bookmarks too? not sure

View file

@ -7,6 +7,7 @@ from __future__ import annotations
import html
import json # hmm interesting enough, orjson didn't give much speedup here?
from abc import abstractmethod
from collections.abc import Iterator, Sequence
from dataclasses import dataclass
from datetime import datetime
from functools import cached_property
@ -14,8 +15,6 @@ from itertools import chain
from pathlib import Path
from typing import (
TYPE_CHECKING,
Iterator,
Sequence,
)
from more_itertools import unique_everseen

View file

@ -1,17 +1,19 @@
from my.core import __NOT_HPI_MODULE__
from my.core import __NOT_HPI_MODULE__ # isort: skip
from collections.abc import Iterator
from itertools import chain
from typing import Iterator, Any
from typing import Any
from more_itertools import unique_everseen
# TODO add proper Protocol for Tweet
Tweet = Any
TweetId = str
from my.core import warn_if_empty, Res
from my.core import Res, warn_if_empty
@warn_if_empty
def merge_tweets(*sources: Iterator[Res[Tweet]]) -> Iterator[Res[Tweet]]:
def key(r: Res[Tweet]):

View file

@ -7,10 +7,11 @@ from __future__ import annotations
import re
import sqlite3
from abc import abstractmethod
from collections.abc import Iterator, Sequence
from dataclasses import dataclass
from datetime import datetime, timezone
from pathlib import Path
from typing import Iterator, Sequence, Union
from typing import Union
from my.core import Paths, Res, datetime_aware, get_files
from my.core.common import unique_everseen

View file

@ -1,17 +1,17 @@
"""
Twitter data (tweets and favorites). Uses [[https://github.com/twintproject/twint][Twint]] data export.
"""
from collections.abc import Iterator
from dataclasses import dataclass
from datetime import datetime, timezone
from pathlib import Path
from typing import NamedTuple, Iterator, List
from typing import NamedTuple
from my.core import Paths, Res, get_files, LazyLogger, Json, datetime_aware, stat, Stats
from my.core import Json, LazyLogger, Paths, Res, Stats, datetime_aware, get_files, stat
from my.core.cfg import make_config
from my.core.sqlite import sqlite_connection
from my.config import twint as user_config
from my.config import twint as user_config # isort: skip
# TODO move to twitter.twint config structure
@ -76,7 +76,7 @@ class Tweet(NamedTuple):
return text
@property
def urls(self) -> List[str]:
def urls(self) -> list[str]:
ustr = self.row['urls']
if len(ustr) == 0:
return []