From 634bd5b03fc3a451764fa2b89c4771e5aebdb6a4 Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Sun, 17 Nov 2024 12:21:08 +0100 Subject: [PATCH] Ruff --- libpurple_to_markdown/__main__.py | 49 +++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/libpurple_to_markdown/__main__.py b/libpurple_to_markdown/__main__.py index cfd7f91..9b3bf95 100644 --- a/libpurple_to_markdown/__main__.py +++ b/libpurple_to_markdown/__main__.py @@ -1,9 +1,8 @@ import argparse import dataclasses -import logging import datetime -from collections.abc import Iterable, Iterator, Mapping -from typing import Callable +import logging +from collections.abc import Callable, Iterable, Iterator, Mapping from pathlib import Path from . import ( @@ -18,7 +17,10 @@ from .markdown import format_messages 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]] = {} for msg in messages: by_key.setdefault(key_fn(msg), []).append(msg) @@ -48,18 +50,23 @@ MAX_AVERAGE_MESSAGES_PER_PERIOD = 120 TOO_FEW_MESSAGES_TO_CARE = 2 PERIOD_KEYS_BY_NAME: Mapping[str, Callable[[Message], str]] = { - 'full': (lambda msg: 'full'), - 'year': year_period_key, - 'quarter': year_quarter_period_key, - 'month': year_and_month_period_key, + 'full': (lambda msg: 'full'), + 'year': year_period_key, + 'quarter': year_quarter_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 - 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: - possible_period_keys = [PERIOD_KEYS_BY_NAME[period_key]] + possible_period_keys = [PERIOD_KEYS_BY_NAME[period_key]] del period_key # Group by key @@ -89,10 +96,19 @@ def parse_args(): parser.add_argument('--output', type=Path) parser.add_argument('--myself', type=str, default='Myself') 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('--skip-this-period', action='store_true', dest='skip_this_period') + parser.add_argument( + '--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() + def main(): logging.basicConfig() logging.getLogger().setLevel('INFO') @@ -125,7 +141,10 @@ def main(): logger.info(' "%s": Skipped due to too few messages', chat_id) 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( ' "%s": %d messages, %d periods (%d msg/period avg)', chat_id, @@ -134,7 +153,7 @@ def main(): 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(): file_escaped_chat_id = chat_id.replace(' ', '-')