From f0a7b5328675eda8a5d7ca933c7c26f77b4e6ccb Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Wed, 7 Jun 2023 03:27:57 +0000 Subject: [PATCH] style: format code with black Format code with black This commit fixes the style issues introduced in 6665221 according to the output from black. Details: https://app.deepsource.com/gh/Flantropy/TelegramChatAnalyzer/transform/641923ec-bf3f-4a4d-b487-ea659eded058/ --- src/commands.py | 52 +++++++++++++++++++-------------------- src/doc.py | 46 +++++++++++++++++----------------- src/main.py | 26 ++++++++++++-------- src/utils.py | 65 ++++++++++++++++++++++--------------------------- 4 files changed, 95 insertions(+), 94 deletions(-) diff --git a/src/commands.py b/src/commands.py index c471643..9e52579 100644 --- a/src/commands.py +++ b/src/commands.py @@ -17,14 +17,11 @@ def start(update, context): """ Greets user on chat startup """ - update.message.reply_text(text='Hello and welcome') + update.message.reply_text(text="Hello and welcome") def help_info(update, context): - update.message.reply_text( - text=HELP_INFO, - parse_mode=PARSEMODE_HTML - ) + update.message.reply_text(text=HELP_INFO, parse_mode=PARSEMODE_HTML) def analyze_history(update, context): @@ -34,7 +31,7 @@ def analyze_history(update, context): chat_json = _unpack_telegram_document(update) messages_df = _form_data_frame_from_json(chat_json) if messages_df is None: - update.message.reply_text('Exit early. Invalid json') + update.message.reply_text("Exit early. Invalid json") return photos = make_plots(messages_df) update.message.reply_media_group(media=photos) @@ -42,12 +39,12 @@ def analyze_history(update, context): def start_shipping_callback(update: Update, context: CallbackContext): update.message.reply_invoice( - title='Some gifts', - description='We need your address', - payload='UKASSA', - provider_token=os.getenv('UKASSA_TEST_TOKEN'), - currency='RUB', - prices=[LabeledPrice('Test', 100*100)], + title="Some gifts", + description="We need your address", + payload="UKASSA", + provider_token=os.getenv("UKASSA_TEST_TOKEN"), + currency="RUB", + prices=[LabeledPrice("Test", 100 * 100)], need_phone_number=True, need_email=True, need_shipping_address=True, @@ -57,35 +54,38 @@ def start_shipping_callback(update: Update, context: CallbackContext): def start_noshipping_callback(update: Update, context: CallbackContext): update.message.reply_invoice( - title='Premium status', - description='One month of premium access', - payload='UKASSA', - provider_token=os.getenv('UKASSA_TEST_TOKEN'), - currency='RUB', - prices=[LabeledPrice('Test', 100*100)], + title="Premium status", + description="One month of premium access", + payload="UKASSA", + provider_token=os.getenv("UKASSA_TEST_TOKEN"), + currency="RUB", + prices=[LabeledPrice("Test", 100 * 100)], need_email=True, ) def shipping_callback(update: Update, context: CallbackContext) -> None: query = update.shipping_query - if query.invoice_payload != 'UKASSA': - query.answer(ok=False, error_message='Something went wrong') + if query.invoice_payload != "UKASSA": + query.answer(ok=False, error_message="Something went wrong") return - options = [ShippingOption('1', 'Option A', [LabeledPrice('A', 100)])] - price_list = [LabeledPrice('B1', 150), LabeledPrice('B2', 200)] - options.append(ShippingOption('2', 'Option B', price_list)) + options = [ShippingOption("1", "Option A", [LabeledPrice("A", 100)])] + price_list = [LabeledPrice("B1", 150), LabeledPrice("B2", 200)] + options.append(ShippingOption("2", "Option B", price_list)) query.answer(ok=True, shipping_options=options) def pre_checkout_callback(update: Update, context: CallbackContext) -> None: query = update.pre_checkout_query - params = dict(ok=True) if query.invoice_payload == 'UKASSA'\ - else dict(ok=False, error_message='Something went wrong!') + params = ( + dict(ok=True) + if query.invoice_payload == "UKASSA" + else dict(ok=False, error_message="Something went wrong!") + ) query.answer(**params) def successful_payment(update: Update, context: CallbackContext) -> None: - update.message.reply_text('Thank you for your payment') + update.message.reply_text("Thank you for your payment") diff --git a/src/doc.py b/src/doc.py index defc154..957479b 100644 --- a/src/doc.py +++ b/src/doc.py @@ -1,23 +1,25 @@ -HELP_INFO = '1. Exporting history.' \ - '\nIn Telegram you can easily export your chat history. ' \ - 'I can only work with personal chats for now, so please pick one ' \ - 'and let\'s analyze it! ' \ - 'Here is an official guide on how to ' \ - 'get a file with your history. ' \ - 'Please note, this feature available only from Desktop App. Remember ' \ - 'to save in .json format, not in .html.' \ - '\n\n2. How it works.' \ - '\nAfter you done with exporting desired history - ' \ - 'we ready to go. Just attach "result.json" file to a regular message and send ' \ - 'it to me. I\'ll try to do my best analyzing it for you and send back some plots.' \ - 'My code is open and I do not collect any parts of your data, ' \ - 'but if you worry about it you can consider next step.' \ - '\n\n3. You should feel free to delete essential ' \ - 'parts of your messaging history (like messages text, user id, etc) before sending it to me' \ - '\nI\'ll try to stay functional. ' \ - 'But there is recommended set of messages params ' \ - 'to be present in your json: id, type, date, from, media_type. ' \ - 'Here is some resources were you can inspect ' \ - 'and transform your json: JSONEditorOnline' \ - '\n\n4. Any questions left, run into some issues or you just want to check my codebase? Welcome ' \ +HELP_INFO = ( + "1. Exporting history." + "\nIn Telegram you can easily export your chat history. " + "I can only work with personal chats for now, so please pick one " + "and let's analyze it! " + 'Here is an official guide on how to ' + "get a file with your history. " + "Please note, this feature available only from Desktop App. Remember " + "to save in .json format, not in .html." + "\n\n2. How it works." + "\nAfter you done with exporting desired history - " + 'we ready to go. Just attach "result.json" file to a regular message and send ' + "it to me. I'll try to do my best analyzing it for you and send back some plots." + "My code is open and I do not collect any parts of your data, " + "but if you worry about it you can consider next step." + "\n\n3. You should feel free to delete essential " + "parts of your messaging history (like messages text, user id, etc) before sending it to me" + "\nI'll try to stay functional. " + "But there is recommended set of messages params " + "to be present in your json: id, type, date, from, media_type. " + "Here is some resources were you can inspect " + 'and transform your json: JSONEditorOnline' + "\n\n4. Any questions left, run into some issues or you just want to check my codebase? Welcome " 'to my repo on GitHub' +) diff --git a/src/main.py b/src/main.py index a0e958c..8a7d1f5 100644 --- a/src/main.py +++ b/src/main.py @@ -4,8 +4,14 @@ import telegram.ext as tg from src.commands import ( - analyze_history, help_info, pre_checkout_callback, shipping_callback, start, - start_noshipping_callback, start_shipping_callback, successful_payment, + analyze_history, + help_info, + pre_checkout_callback, + shipping_callback, + start, + start_noshipping_callback, + start_shipping_callback, + successful_payment, ) @@ -13,24 +19,24 @@ def main(): # Setting up a basic logger logging.basicConfig( level=logging.INFO, - format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', + format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", ) logger = logging.getLogger() logger.setLevel(logging.INFO) # Creating updater and dispatcher for bot - updater = tg.Updater(token=os.getenv('BOT_TOKEN')) + updater = tg.Updater(token=os.getenv("BOT_TOKEN")) dispatcher: tg.Dispatcher = updater.dispatcher # Filters - json_only_filter = tg.Filters.document.file_extension('json') + json_only_filter = tg.Filters.document.file_extension("json") # Creating handlers handlers = [ - tg.CommandHandler('start', start), - tg.CommandHandler('help', help_info), - tg.CommandHandler('shipping', start_shipping_callback), - tg.CommandHandler('noshipping', start_noshipping_callback), + tg.CommandHandler("start", start), + tg.CommandHandler("help", help_info), + tg.CommandHandler("shipping", start_shipping_callback), + tg.CommandHandler("noshipping", start_noshipping_callback), tg.PreCheckoutQueryHandler(pre_checkout_callback), tg.ShippingQueryHandler(shipping_callback), tg.MessageHandler(json_only_filter, analyze_history), @@ -46,5 +52,5 @@ def main(): updater.idle() -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/src/utils.py b/src/utils.py index b58c549..f2362ce 100644 --- a/src/utils.py +++ b/src/utils.py @@ -32,17 +32,17 @@ def _unpack_telegram_document(update) -> dict: def _form_data_frame_from_json(chat_json) -> Optional[pd.DataFrame]: try: messages_df = pd.DataFrame( - chat_json['messages'], - columns=['id', 'type', 'date', 'from', 'text', 'media_type']) + chat_json["messages"], + columns=["id", "type", "date", "from", "text", "media_type"], + ) except KeyError as e: logging.getLogger().error( - msg=f'Unable to form DataFrame from json. ' - f'Key "messages" not found. {e}' + msg=f"Unable to form DataFrame from json. " f'Key "messages" not found. {e}' ) return else: - messages_df.set_index('id', inplace=True) - messages_df['date'] = pd.to_datetime(messages_df['date']) + messages_df.set_index("id", inplace=True) + messages_df["date"] = pd.to_datetime(messages_df["date"]) return messages_df @@ -51,64 +51,57 @@ def _make_barplot(messages_df: pd.DataFrame) -> InputMediaPhoto: :param messages_df: DataFrame with user messaging history :return: telegram.InputMediaPhoto """ - messages_per_month = messages_df['date'] \ - .groupby(messages_df['date'].dt.to_period('M')) \ - .agg('count') + messages_per_month = ( + messages_df["date"].groupby(messages_df["date"].dt.to_period("M")).agg("count") + ) plot = sns.barplot( x=messages_per_month.index, y=messages_per_month.values, - color=(0.44, 0.35, 0.95) + color=(0.44, 0.35, 0.95), ) plt.xticks(rotation=45) - plt.title('All time history') + plt.title("All time history") return __convert_plot_to_telegram_photo(plot) def _make_kde_plot(messages_df: pd.DataFrame) -> InputMediaPhoto: - plot = sns.kdeplot( - x=messages_df['date'], - hue=messages_df['from'], - shade=True - ) - plt.title('Activity by user') + plot = sns.kdeplot(x=messages_df["date"], hue=messages_df["from"], shade=True) + plt.title("Activity by user") plt.xticks(rotation=45) - plt.xlabel('') + plt.xlabel("") return __convert_plot_to_telegram_photo(plot) -def _make_media_distribution_bar_plot(messages_df: pd.DataFrame) -> Optional[InputMediaPhoto]: - logging.getLogger().info('Enter media dist function') - media_dist_df = messages_df[['from', 'media_type']].value_counts() +def _make_media_distribution_bar_plot( + messages_df: pd.DataFrame, +) -> Optional[InputMediaPhoto]: + logging.getLogger().info("Enter media dist function") + media_dist_df = messages_df[["from", "media_type"]].value_counts() if media_dist_df.empty: return media_dist_plot = media_dist_df.unstack().plot( - kind='bar', - stacked=True, - ylabel='Media messages', - xlabel='User' + kind="bar", stacked=True, ylabel="Media messages", xlabel="User" ) plt.xticks(rotation=0) - plt.title('Distribution of media messages') + plt.title("Distribution of media messages") return __convert_plot_to_telegram_photo(media_dist_plot) def _make_weekday_distribution_bar_plot(messages_df: pd.DataFrame) -> InputMediaPhoto: - dist_by_day_of_week = messages_df['from']\ - .groupby(messages_df['date'].dt.weekday)\ - .agg('value_counts') - plot = dist_by_day_of_week.unstack().plot(kind='bar') - plt.xlabel('') - plt.ylabel('Messages') + dist_by_day_of_week = ( + messages_df["from"].groupby(messages_df["date"].dt.weekday).agg("value_counts") + ) + plot = dist_by_day_of_week.unstack().plot(kind="bar") + plt.xlabel("") + plt.ylabel("Messages") plt.xticks( - list(range(7)), - ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'], - rotation=0 + list(range(7)), ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"], rotation=0 ) return __convert_plot_to_telegram_photo(plot) def make_plots(messages_df: pd.DataFrame) -> List[InputMediaPhoto]: - sns.set_theme(context='paper') + sns.set_theme(context="paper") photo_list = [ _make_barplot(messages_df), _make_media_distribution_bar_plot(messages_df),