<commit_before># -*- coding: utf-8 -*-
import logging

import os
import re
import telegram
from orm.models import db, Delay, Person, fn
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters

# const
API_TOKEN = os.environ['API_TOKEN']
MENU, ENTER_DELAY, ENTER_REASON = range(3)

# global vars
state = {}

logging.basicConfig(
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    level=logging.INFO
)

logger = logging.getLogger(__name__)


# database connect and close
def db_connect_close(func):
    def wrap(*args, **kwargs):
        db.connect()
        func(*args, **kwargs)
        db.close()
    return wrap


# checks
def is_delay_correct(text):
    if re.search('[^0-9](?u)', text):
        return False
    else:
        return True


def is_reason_correct(text):
    if re.search('[^a-zA-Zа-яА-Я](?u)', text):
        return False
    else:
        return True


# db manipulations
def get_or_create_person(message):
    message_from = message.to_dict().get('from')
    username = message_from.get('username')
    first_name = message_from.get('first_name')
    last_name = message_from.get('last_name')
    person, created = Person.get_or_create(
        username=username,
        defaults={
            'first_name': first_name,
            'last_name': last_name
        }
    )
    return person


def create_delay(person, minute):
    Delay.create(person=person, minute=minute)


def create_reason(person, text):
    last_delay = list(person.delays)[-1]
    last_delay.reason = text
    last_delay.save()


@db_connect_close
def main_handler(bot, update):
    person = get_or_create_person(update.message)
    username = person.username
    user_state = state.get(username, MENU)
    text = update.message.text
    chat_id = update.message.chat_id

    if user_state == MENU and re.search('([ао]п[ао][зс]д|задерж)(?iu)', text):
        bot.sendMessage(chat_id, 'Используйте команду /late')

    elif user_state == MENU and text[0] == '/':
        bot.sendMessage(chat_id, 'Введите время задержки в минутах')
        state[username] = ENTER_DELAY

    elif user_state == ENTER_DELAY:
        if is_delay_correct(text):
            minute = int(text)
            create_delay(person, minute)
            bot.sendMessage(chat_id, 'Введите причину задержки')
            state[username] = ENTER_REASON
        else:
            bot.sendMessage(chat_id, 'Введите целое число - минуты задержки')

    elif user_state == ENTER_REASON:
        if is_reason_correct(text):
            create_reason(person, text)
            bot.sendMessage(chat_id, 'Успех!')
            state[username] = MENU
        else:
            bot.sendMessage(chat_id, 'Введите словесную причину, без цифр и '
                                     'остальных символов')


# show users delay stats
@db_connect_close
def show_stats(bot, update):
    chat_id = update.message.chat_id
    for person in Person.select():
        if person.delays.count() != 0:
            bot.sendMessage(
                chat_id,
                '{} {} @{}: \n'
                '*количество опозданий* - {}, \n'
                '*суммарное время опозданий* - {} минут'.format(
                    person.first_name,
                    person.last_name,
                    person.username,
                    person.delays.count(),
                    person.delays.aggregate(fn.Sum(Delay.minute))
                ),
                parse_mode='Markdown')


# debug function
def get_upd(bot, update):
    print(update.message)


def error(bot, update, error):
    logger.warn('Update "%s" caused error "%s"' % (update, error))


def main():
    updater = Updater(API_TOKEN)
    dp = updater.dispatcher

    dp.add_handler(CommandHandler("late", main_handler))
    dp.add_handler(CommandHandler("stats", show_stats))
    dp.add_handler(MessageHandler([Filters.text], main_handler))

    dp.add_error_handler(error)

    updater.start_polling()

    updater.idle()

if __name__ == '__main__':
    main()<commit_msg>REFACTOR<commit_after># -*- coding: utf-8 -*-
import logging
import os
import re

from telegram.ext import Updater, CommandHandler, MessageHandler, Filters

from orm.models import db, Delay, Person, fn

# const
API_TOKEN = os.environ['API_TOKEN']
MENU, ENTER_DELAY, ENTER_REASON = range(3)

logging.basicConfig(
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    level=logging.INFO
)

logger = logging.getLogger(__name__)


# database connect and close
def db_connect_close(func):
    def wrap(*args, **kwargs):
        db.connect()
        func(*args, **kwargs)
        db.close()
    return wrap


# db manipulations
def get_or_create_person(message):
    message_from = message.to_dict().get('from')
    username = message_from.get('username')
    first_name = message_from.get('first_name')
    last_name = message_from.get('last_name')
    person, created = Person.get_or_create(
        username=username,
        defaults={
            'first_name': first_name,
            'last_name': last_name
        }
    )
    return person


def extract_delay_num(text):
    


@db_connect_close
def main_handler(bot, update):
    person = get_or_create_person(update.message)
    text = update.message.text

    if re.search('([ао]п[ао][зс]д|задерж)(?iu)', text):
        num = extract_delay_num(text)
        type = extract_delay_type()
        minutes = convert_num(num, type)
        reason = extract_reason()
        save_delay(user, minutes, reason)


# show users delay stats
@db_connect_close
def show_stats(bot, update):
    chat_id = update.message.chat_id
    for person in Person.select():
        if person.delays.count() != 0:
            bot.sendMessage(
                chat_id,
                '{} {} @{}: \n'
                '*количество опозданий* - {}, \n'
                '*суммарное время опозданий* - {} минут'.format(
                    person.first_name,
                    person.last_name,
                    person.username,
                    person.delays.count(),
                    person.delays.aggregate(fn.Sum(Delay.minute))
                ),
                parse_mode='Markdown')


# debug function
def debug_update(bot, update):
    print(update.message)


def error(bot, update, error):
    logger.warn('Update "%s" caused error "%s"' % (update, error))


def main():
    updater = Updater(API_TOKEN)
    dp = updater.dispatcher

    dp.add_handler(CommandHandler("late", main_handler))
    dp.add_handler(CommandHandler("stats", show_stats))
    dp.add_handler(MessageHandler([Filters.text], main_handler))
    dp.add_error_handler(error)

    updater.start_polling()
    updater.idle()

if __name__ == '__main__':
    main()
{'fraction_non_alphanumeric': 0.12201670644391407, 'fraction_numerical': 0.0014916467780429594, 'mean_word_length': 2.6106623586429727, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7002603', 'n_tokens_mistral': 2205, 'n_tokens_neox': 2189, 'n_words': 434}