<commit_before># -*- coding: utf-8 -*-
from threading import Thread
import time
import re
import math

from utils import parse_input
import vkrequests as vkr

from __init__ import PATH
from __init__ import DATA_PATH

from __init__ import __help__
from __init__ import __author_vk_id__


class Bot(object):
    def __init__(self):
        pass


    def help(self):
        return __help__


    def say(self, words):
        argument_required = self._argument_missing(words)
        if argument_required:
            return argument_required

        del words[0]
        text = ' '.join(words)
        return text


    def calculate(self, words):
        argument_required = self._argument_missing(words)
        if argument_required:
            return argument_required

        if words[0].startswith('='):
            words[0] = words[0][1:]
        else:
            del words[0]
        words = ''.join(words).lower()
        if not re.match(u'[^\d+\-*/:().,^√πe]', words) or re.match('(sqrt\(\d+\))|(pi)', words):
            words = ' ' + words + ' '
            words = re.sub(u'(sqrt)|√', 'math.sqrt', words)
            words = re.sub(u'(pi)|π', 'math.pi', words)
            words = re.sub('\^', '**', words)
            words = re.sub(',', '.', words)
            words = re.sub(':', '/', words)            
            while True:
                if '/' in words:
                    index = re.search('[^.\d]\d+[^.\de]', words)
                    if index:
                        index = index.end() - 1
                        words = words[:index] + '.' + words[index:]
                    else:
                        break
                else:
                    break
            try:
                result = str(eval(words))
            except SyntaxError:
                result = u'Ошибка [0]'
            except NameError:
                result = u'Ошибка [1]'
            except AttributeError:
                result = u'Ошибка [2]'        
            except ZeroDivisionError:
                result = u'Деление на 0'
            except OverflowError:
                result = u'Слишком большой результат'
        else:
            result = u'Не математическая операция'
        return result

 
    def prime(self, words):
        argument_required = self._argument_missing(words)
        if argument_required:
            return argument_required

        del words[0]
        input_number = ''.join(words)
        if re.match('^\d+$', input_number) and len(input_number)<=5:
            input_number = int(input_number)
            luc_number = 0
            last_luc_number = 0
            for i in range(input_number):
                if luc_number == 0:
                    luc_number = 1
                elif luc_number == 1:
                    last_luc_number = luc_number
                    luc_number = 3
                else:
                    luc_number, last_luc_number = last_luc_number + luc_number, luc_number
                            
            if input_number != 0:
                is_prime = True if (luc_number - 1) % input_number == 0 else False
                result = 'Является простым числом' if is_prime else 'Не является простым числом'
            else:
                result = u'0 не является простым числом'
        else:
            result = u'Дано неверное или слишком большое значение'
        return result


    def activate_bot(self, message):
        if message['user_id'] == __author_vk_id__ and message['title'] != u' ... ':
            return u'Активация прошла успешно', True
        else:
            return u'Отказано в доступе', False


    def deactivate_bot(self, message):
        if message['user_id'] == __author_vk_id__ and message['title'] != u' ... ':
            return u'Деактивация прошла успешно', False
        else:
            return u'Отказано в доступе', True


    def _argument_missing(self, words):
        if len(words) == 1:
            return u'Команду необходимо использовать с аргументом'
        else:
            return False


class LongPollSession(Bot):
    def __init__(self, activated=False, custom_data=''):
        self.activated = activated
        self.authorized = False
        self.custom_data = custom_data
        self.update_processing = None
        self.run_bot = False
        self.running = False
        self.reply_count = 0
        
        if self.custom_data:
            self.custom_data = self.prepare_custom_data()


    def prepare_custom_data(self):
        pass


    def authorization(self, login= '', password= '', logout=False):
        token_path = DATA_PATH + 'token.txt'
        authorized = False
        token = None
        if logout:
            open(token_path, 'w').close()
            return

        if not (login and password):
            try:
                with open(token_path, 'r') as token_file:
                    lines = token_file.readlines()
                    if lines:
                        token = lines[0][:-1]
            except:
                token = None
                open(token_path, 'w').close()

            if token:
                if vkr.log_in(token=token):
                    authorized = True
                else:
                    open(token_path, 'w').close()
        else:
            new_token = vkr.log_in(login=login, password=password)
            if new_token:
                with open(token_path, 'w') as token_file:
                    token_file.write('{}\n{}'.format(\
                        new_token, 'НИКОМУ НЕ ПОКАЗЫВАЙТЕ СОДЕРЖИМОЕ ЭТОГО ФАЙЛА'
                        )<commit_msg>Параметр authorized изменяется на False при выходе из аккаунта<commit_after># -*- coding: utf-8 -*-
from threading import Thread
import time
import re
import math

from utils import parse_input
import vkrequests as vkr

from __init__ import PATH
from __init__ import DATA_PATH

from __init__ import __help__
from __init__ import __author_vk_id__


class Bot(object):
    def __init__(self):
        pass


    def help(self):
        return __help__


    def say(self, words):
        argument_required = self._argument_missing(words)
        if argument_required:
            return argument_required

        del words[0]
        text = ' '.join(words)
        return text


    def calculate(self, words):
        argument_required = self._argument_missing(words)
        if argument_required:
            return argument_required

        if words[0].startswith('='):
            words[0] = words[0][1:]
        else:
            del words[0]
        words = ''.join(words).lower()
        if not re.match(u'[^\d+\-*/:().,^√πe]', words) or re.match('(sqrt\(\d+\))|(pi)', words):
            words = ' ' + words + ' '
            words = re.sub(u'(sqrt)|√', 'math.sqrt', words)
            words = re.sub(u'(pi)|π', 'math.pi', words)
            words = re.sub('\^', '**', words)
            words = re.sub(',', '.', words)
            words = re.sub(':', '/', words)            
            while True:
                if '/' in words:
                    index = re.search('[^.\d]\d+[^.\de]', words)
                    if index:
                        index = index.end() - 1
                        words = words[:index] + '.' + words[index:]
                    else:
                        break
                else:
                    break
            try:
                result = str(eval(words))
            except SyntaxError:
                result = u'Ошибка [0]'
            except NameError:
                result = u'Ошибка [1]'
            except AttributeError:
                result = u'Ошибка [2]'        
            except ZeroDivisionError:
                result = u'Деление на 0'
            except OverflowError:
                result = u'Слишком большой результат'
        else:
            result = u'Не математическая операция'
        return result

 
    def prime(self, words):
        argument_required = self._argument_missing(words)
        if argument_required:
            return argument_required

        del words[0]
        input_number = ''.join(words)
        if re.match('^\d+$', input_number) and len(input_number)<=5:
            input_number = int(input_number)
            luc_number = 0
            last_luc_number = 0
            for i in range(input_number):
                if luc_number == 0:
                    luc_number = 1
                elif luc_number == 1:
                    last_luc_number = luc_number
                    luc_number = 3
                else:
                    luc_number, last_luc_number = last_luc_number + luc_number, luc_number
                            
            if input_number != 0:
                is_prime = True if (luc_number - 1) % input_number == 0 else False
                result = 'Является простым числом' if is_prime else 'Не является простым числом'
            else:
                result = u'0 не является простым числом'
        else:
            result = u'Дано неверное или слишком большое значение'
        return result


    def activate_bot(self, message):
        if message['user_id'] == __author_vk_id__ and message['title'] != u' ... ':
            return u'Активация прошла успешно', True
        else:
            return u'Отказано в доступе', False


    def deactivate_bot(self, message):
        if message['user_id'] == __author_vk_id__ and message['title'] != u' ... ':
            return u'Деактивация прошла успешно', False
        else:
            return u'Отказано в доступе', True


    def _argument_missing(self, words):
        if len(words) == 1:
            return u'Команду необходимо использовать с аргументом'
        else:
            return False


class LongPollSession(Bot):
    def __init__(self, activated=False, custom_data=''):
        self.activated = activated
        self.authorized = False
        self.custom_data = custom_data
        self.update_processing = None
        self.run_bot = False
        self.running = False
        self.reply_count = 0
        
        if self.custom_data:
            self.custom_data = self.prepare_custom_data()


    def prepare_custom_data(self):
        pass


    def authorization(self, login= '', password= '', logout=False):
        token_path = DATA_PATH + 'token.txt'
        authorized = False
        token = None
        if logout:
            open(token_path, 'w').close()
            self.authorized = False
            return

        if not (login and password):
            try:
                with open(token_path, 'r') as token_file:
                    lines = token_file.readlines()
                    if lines:
                        token = lines[0][:-1]
            except:
                token = None
                open(token_path, 'w').close()

            if token:
                if vkr.log_in(token=token):
                    authorized = True
                else:
                    open(token_path, 'w').close()
        else:
            new_token = vkr.log_in(login=login, password=password)
            if new_token:
                with open(token_path, 'w') as token_file:
                    token_file.write('{}\n{}'.format(\
                        new_token, 'НИКОМУ НЕ ПОКАЗЫВАЙТЕ СОДЕРЖИМОЕ ЭТОГО ФАЙЛА'
                        )
{'fraction_non_alphanumeric': 0.09956594915404376, 'fraction_numerical': 0.00496058109664275, 'mean_word_length': 1.5083314818929128, 'pattern_counts': {'":': 0, '<': 5, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4384748', 'n_tokens_mistral': 3335, 'n_tokens_neox': 3246, 'n_words': 804}