Ruff
This commit is contained in:
parent
6f22e8d239
commit
634bd5b03f
|
@ -1,9 +1,8 @@
|
||||||
import argparse
|
import argparse
|
||||||
import dataclasses
|
import dataclasses
|
||||||
import logging
|
|
||||||
import datetime
|
import datetime
|
||||||
from collections.abc import Iterable, Iterator, Mapping
|
import logging
|
||||||
from typing import Callable
|
from collections.abc import Callable, Iterable, Iterator, Mapping
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from . import (
|
from . import (
|
||||||
|
@ -18,7 +17,10 @@ from .markdown import format_messages
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def group_messages(messages: Iterable[Message], key_fn: Callable[[Message], str]) -> dict[str, list[Message]]:
|
def group_messages(
|
||||||
|
messages: Iterable[Message],
|
||||||
|
key_fn: Callable[[Message], str],
|
||||||
|
) -> dict[str, list[Message]]:
|
||||||
by_key: dict[str, list[Message]] = {}
|
by_key: dict[str, list[Message]] = {}
|
||||||
for msg in messages:
|
for msg in messages:
|
||||||
by_key.setdefault(key_fn(msg), []).append(msg)
|
by_key.setdefault(key_fn(msg), []).append(msg)
|
||||||
|
@ -48,18 +50,23 @@ MAX_AVERAGE_MESSAGES_PER_PERIOD = 120
|
||||||
TOO_FEW_MESSAGES_TO_CARE = 2
|
TOO_FEW_MESSAGES_TO_CARE = 2
|
||||||
|
|
||||||
PERIOD_KEYS_BY_NAME: Mapping[str, Callable[[Message], str]] = {
|
PERIOD_KEYS_BY_NAME: Mapping[str, Callable[[Message], str]] = {
|
||||||
'full': (lambda msg: 'full'),
|
'full': (lambda msg: 'full'),
|
||||||
'year': year_period_key,
|
'year': year_period_key,
|
||||||
'quarter': year_quarter_period_key,
|
'quarter': year_quarter_period_key,
|
||||||
'month': year_and_month_period_key,
|
'month': year_and_month_period_key,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def group_messages_by_period(messages: Iterable[Message], period_key: str | None = None) -> tuple[dict[str, list[Message]], Callable[[Message], str]]:
|
def group_messages_by_period(
|
||||||
|
messages: Iterable[Message],
|
||||||
|
period_key: str | None = None,
|
||||||
|
) -> tuple[dict[str, list[Message]], Callable[[Message], str]]:
|
||||||
# Determine key function
|
# Determine key function
|
||||||
possible_period_keys: Iterable[Callable[[Message], str]] = PERIOD_KEYS_BY_NAME.values()
|
possible_period_keys: Iterable[Callable[[Message], str]] = (
|
||||||
|
PERIOD_KEYS_BY_NAME.values()
|
||||||
|
)
|
||||||
if period_key is not None:
|
if period_key is not None:
|
||||||
possible_period_keys = [PERIOD_KEYS_BY_NAME[period_key]]
|
possible_period_keys = [PERIOD_KEYS_BY_NAME[period_key]]
|
||||||
del period_key
|
del period_key
|
||||||
|
|
||||||
# Group by key
|
# Group by key
|
||||||
|
@ -89,10 +96,19 @@ def parse_args():
|
||||||
parser.add_argument('--output', type=Path)
|
parser.add_argument('--output', type=Path)
|
||||||
parser.add_argument('--myself', type=str, default='Myself')
|
parser.add_argument('--myself', type=str, default='Myself')
|
||||||
parser.add_argument('--overwrite', action='store_true', dest='overwrite_files')
|
parser.add_argument('--overwrite', action='store_true', dest='overwrite_files')
|
||||||
parser.add_argument('--period', dest='period_key', choices=list(PERIOD_KEYS_BY_NAME.keys()))
|
parser.add_argument(
|
||||||
parser.add_argument('--skip-this-period', action='store_true', dest='skip_this_period')
|
'--period',
|
||||||
|
dest='period_key',
|
||||||
|
choices=list(PERIOD_KEYS_BY_NAME.keys()),
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
'--skip-this-period',
|
||||||
|
action='store_true',
|
||||||
|
dest='skip_this_period',
|
||||||
|
)
|
||||||
return parser.parse_args()
|
return parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
logging.basicConfig()
|
logging.basicConfig()
|
||||||
logging.getLogger().setLevel('INFO')
|
logging.getLogger().setLevel('INFO')
|
||||||
|
@ -125,7 +141,10 @@ def main():
|
||||||
logger.info(' "%s": Skipped due to too few messages', chat_id)
|
logger.info(' "%s": Skipped due to too few messages', chat_id)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
messages_by_period, period_key_fn = group_messages_by_period(messages_in_chat, args.period_key)
|
messages_by_period, period_key_fn = group_messages_by_period(
|
||||||
|
messages_in_chat,
|
||||||
|
args.period_key,
|
||||||
|
)
|
||||||
logger.info(
|
logger.info(
|
||||||
' "%s": %d messages, %d periods (%d msg/period avg)',
|
' "%s": %d messages, %d periods (%d msg/period avg)',
|
||||||
chat_id,
|
chat_id,
|
||||||
|
@ -134,7 +153,7 @@ def main():
|
||||||
len(messages_in_chat_original) / len(messages_by_period),
|
len(messages_in_chat_original) / len(messages_by_period),
|
||||||
)
|
)
|
||||||
|
|
||||||
this_period_name = period_key_fn(Message(datetime.datetime.now(), '','',''))
|
this_period_name = period_key_fn(Message(datetime.datetime.now(), '', '', ''))
|
||||||
|
|
||||||
for period_key_name, messages in messages_by_period.items():
|
for period_key_name, messages in messages_by_period.items():
|
||||||
file_escaped_chat_id = chat_id.replace(' ', '-')
|
file_escaped_chat_id = chat_id.replace(' ', '-')
|
||||||
|
|
Loading…
Reference in New Issue
Block a user