def __init__(self, question_filepath, code_filepath): self.code_filepath = code_filepath self.short_code_filepath = '/'.join(self.code_filepath.split('/')[-2:]) super().__init__(question_filepath) # Символ с которого нужно начинать комментарий if 'start_comment_with' in self.structure.keys(): self.start_comment_with = self.structure['start_comment_with'] else: self.start_comment_with = config.get('highlight').get( 'start_comment_with') # Добавить заголовок вопроса в качестве первого комментария над кодом? if 'show_title_in_code' in self.structure.keys(): self.show_title_in_code = self.structure['show_title_in_code'] else: self.show_title_in_code = config.get('highlight').get( 'show_title_in_code') # Язык программирования if 'language' in self.structure.keys(): self.language = self.structure['language'] else: self.language = config.get('highlight').get('language') self.code = self.load_code(code_filepath) self.checksum = hashlib.md5( f"{self.short_filepath}{self.file_content}{self.code}".encode( )).hexdigest()
def edit_this_question(question): github = config.get('telegram').get('github') if question.is_code: edit_question = f'{github}/tree/master/coding/{question.short_filepath}' edit_code = f'{github}/tree/master/coding/{question.short_code_filepath}' return f'<b>Заметили ошибку?</b> Редактировать: <a href="{edit_question}">вопрос</a> '\ f'или <a href="{edit_code}">код</a>' else: edit_question = f'{github}/tree/master/{question.short_filepath}' return f'👆 <b>Заметили ошибку?</b> <a href="{edit_question}">Редактировать вопрос</a>'
def find_news(): items = [] try: root = ET.fromstring( requests.get(config.get('main', 'rss_url')).content) except requests.exceptions.ConnectionError: return [] for item in root.findall('.//channel/item'): link, title = item.find('link').text, item.find('title').text ext_id = link.split('.')[-1].split('/')[0] if not Topic.select().where(Topic.ext_id == ext_id): items.append({"title": title, "link": link}) Topic.create(title=title, link=link, ext_id=ext_id) else: Topic.update(title=title, link=link).where(Topic.ext_id == ext_id) return items
import logging from grabber import Grabber from database import config logger = logging.getLogger('youtuber') handler = logging.FileHandler('error.log') handler.setLevel(logging.ERROR) formatter = logging.Formatter('%(asctime)s - %(name)s - %(message)s') handler.setFormatter(formatter) logger.addHandler(handler) if __name__ == '__main__': grabber = Grabber(config.get('api_key', ''), config.get('channels', [])) grabber.run()
import subprocess as sp from Bio import SeqIO, SeqFeature from Bio.Alphabet import IUPAC from Bio.Seq import Seq import re from tempfile import NamedTemporaryFile as ntf import os from collections import defaultdict from copy import deepcopy from database import config merge_thresh = float(config.get('prodigal', 'merge_thresh')) # requires prodigal, but not sure how to check for that. # i'll figure it out later # I really need to figure out exactly the conversions done def call_prodigal(fastafile): """Invokes prodigal on a provided fasta file, returns the SeqRecord produced by -a. Everything is done in temporary files kept on virtual filesystem.""" # check if file exists blah blah with ntf(prefix='/dev/shm/', delete=True, suffix='.prot') as protfile, ntf(prefix='/dev/shm/', delete=True, suffix='.out') as prod: sp.call(['prodigal', '-i', fastafile, '-a', protfile.name, '-o', prod.name, '-q']) # you can't close over temporary files, so the .parse generator can't generate once this returns # hence list. sucks to be you memory return list(SeqIO.parse(protfile.name, 'fasta'))
def code_highlight(self): """ Стилизуем код из файла. :return: Изображение в бинарном виде. """ code2image = Highlight( self.language, style=config.get('highlight').get('style'), line_numbers=config.get('highlight').get('show_line_numbers'), font_size=config.get('highlight').get('font_size'), font_name=config.get('highlight').get('font_name'), line_pad=config.get('highlight').get('line_pad'), background_color=config.get('highlight').get('background_color'), highlight_color=config.get('highlight').get('highlight_color'), window_frame_color=config.get('highlight').get( 'window_frame_color'), bg_from_color=tuple(config.get('highlight').get('bg_from_color')), bg_to_color=tuple(config.get('highlight').get('bg_to_color')), close_circle=config.get('highlight').get('close_circle'), maximize_circle=config.get('highlight').get('maximize_circle'), minimize_circle=config.get('highlight').get('minimize_circle')) image = code2image.to_macos_frame( code2image.prepare_code( self.code, fake_line_numbers=config.get('highlight').get( 'show_fake_line_numbers'), show_title_in_code=self.show_title_in_code, title=self.title, comment_char=self.start_comment_with)) return code2image.to_bytes(image)
def __init__(self): self.simple_questions = Path.cwd() / "questions" self.questions_with_code = Path.cwd() / "coding" self.telebot = telebot.TeleBot(config.get('telegram').get('token')) self.chat_id = config.get('telegram').get('chat_id')
from database import config import os os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = config.get("dialogflow", "key") DIALOGFLOW_ID = config.get("dialogflow", "id") DF_PROJECT_ID = config.get("dialogflow", "project_id") DF_HELP_UUID = config.get("dialogflow", "help_intent_uuid") DF_PASS_UUID = config.get("dialogflow", "pass_intent_uuid") SKIP_UPDATES = config.get("main", "skip-updates") CHAT_ID = config.get("main", "chat_id") TOKEN = config.get("main", "token") B_TEXT = config.get("message", "button-text") M_TEXT = config.get("message", "message-text") IU_UPDATE = config.get("main", "interim_update") QUESTION_TEXT = config.get("message", "question-body") DETAIL_TEXT = config.get("message", "detailed-text") PASTE_TEXT = config.get("message", "paste-text") NOMETA_TEXT = config.get("message", "nometa-body") NEPRIVET_TEXT = config.get("message", "neprivet-text") IMPORT_DATA_FAIL = config.get("message", "import-data-fail") ADMINS = config.get("main", "admins").split(",")
import asyncio import string import aiohttp import json from database import fmt_data, config from progress import main as progress_main MAX_CONNECTION = config.get('pool_connection') async def down(url): timeout = aiohttp.ClientTimeout(connect=0.2) async with aiohttp.ClientSession( connector=aiohttp.TCPConnector(limit=MAX_CONNECTION)) as session: while True: try: resp = await session.get(url, timeout=timeout) cont = await resp.text() fmt_data(json.loads(cont), url) except (asyncio.TimeoutError, aiohttp.ClientError): print('The connect is timeout, try a new connect.') continue def main(urls): loop = asyncio.get_event_loop() to_do = [down(url) for url in urls] # 这里需要把进度条加入事件循环