# Based on https://github.com/NATSpeech/NATSpeech
import utils.commons.single_thread_env  # NOQA
import json
import numpy as np
import os
import random
import traceback

from functools import partial
from resemblyzer import VoiceEncoder
from tqdm import tqdm

from utils.audio.align import get_mel2note
from utils.audio.mel_processing import torch_wav2spec
from utils.audio.pitch.utils import f0_to_coarse
from utils.audio.pitch_extractors import extract_pitch_simple
from utils.commons.hparams import hparams
from utils.commons.indexed_datasets import IndexedDatasetBuilder
from utils.commons.multiprocess_utils import multiprocess_run_tqdm
from utils.os_utils import remove_file, copy_file

np.seterr(divide="ignore", invalid="ignore")

class BinarizationError(Exception):
    pass

class BaseBinarizer:
    def __init__(self, processed_data_dir=None):
        if processed_data_dir is None:
            processed_data_dir = hparams["processed_data_dir"]
        self.processed_data_dir = processed_data_dir
        self.hparams = hparams
        self.binary_data_dir = hparams["binary_data_dir"]
        self.preprocess_args = hparams["preprocess_args"]
        self.binarization_args = hparams["binarization_args"]
        self.items = {}
        self.item_names = []
        if self.binarization_args["with_spk_f0_norm"]:
            self.spk_pitch_map = {}

    def load_meta_data(self):
        processed_data_dir = self.processed_data_dir
        item_list = json.load(open(f"{processed_data_dir}/metadata.json"))
        for r in tqdm(item_list, desc="Loading meta data."):
            item_name = r["item_name"]
            self.items[item_name] = r
            self.item_names.append(item_name)
        if self.binarization_args["shuffle"]:
            random.seed(1234)
            random.shuffle(self.item_names)

    @property
    def train_index_item_names(self):
        range_ = self._convert_range(self.binarization_args["train_range"])
        return self.item_names[range_[0]:range_[1]]

    @property
    def valid_index_item_names(self):
        range_ = self._convert_range(self.binarization_args["valid_range"])
        return self.item_names[range_[0]:range_[1]]

    @property
    def test_index_item_names(self) -> list:
        range_ = self._convert_range(self.binarization_args["test_range"])
        return self.item_names[range_[0]:range_[1]]

    def _convert_range(self, range_: list):
        if range_[1] == -1:
            range_[1] = len(self.item_names)
        return range_

    @property
    def train_title_item_names(self):
        return [item_name for item_name in self.item_names \
                if any(title in item_name for title in self.binarization_args["train_range"])]

    @property
    def valid_title_item_names(self):
        return [item_name for item_name in self.item_names \
                if any(title in item_name for title in self.binarization_args["valid_range"])]

    @property
    def test_title_item_names(self):
        return [item_name for item_name in self.item_names \
                if any(title in item_name for title in self.binarization_args["test_range"])]

    def meta_data(self, prefix: str, dataset_range):
        """
        Parameter
        ---------
        prefix: str
            Choose one of ["train", "valid", "test"]
        """
        if prefix == "valid":
            if dataset_range == "index":
                item_names = self.valid_index_item_names
            elif dataset_range == "title":
                item_names = self.valid_title_item_names
        elif prefix == "test":
            if dataset_range == "index":
                item_names = self.test_index_item_names
            elif dataset_range == "title":
                    item_names = self.test_title_item_names
        else:
            if dataset_range == "index":
                item_names = self.train_index_item_names
            elif dataset_range == "title":
                item_names = self.train_title_item_names
        for item_name in item_names:
            yield self.items[item_name]

    def process(self):
        self.load_meta_data()
        os.makedirs(hparams["binary_data_dir"], exist_ok=True)
        for fn in ["phone_set.json", "spk_map.json"]:
            remove_file(f"{hparams['binary_data_dir']}/{fn}")
            copy_file(f"{hparams['processed_data_dir']}/{fn}", f"{hparams['binary_data_dir']}/{fn}")
        self.note_pitch_map = self.build_pitch_map()
        self.note_dur_map = self.build_dur_map()
        self.note_tempo_map = self.build_tempo_map()
        self.process_data("valid")
        self.process_data("test")
        self.process_data("train")

    def process_data(self, prefix: str):
        """
        Parameter
        ---------
        prefix: str
            Choose one of ["train", "valid", "test"]
        """
        data_dir = hparams["binary_data_dir"]
        meta_data = list(self.meta_data(prefix, self.binarization_args["dataset_range"]))
        process_item = partial(self.process_item, preprocess_args=self.preprocess_args,
                               binarization_args=self.binarization_args)
        builder = IndexedDatasetBuilder(f"{data_dir}/{prefix}")
        ph_lengths = []
        mel_lengths = []
        total_sec = 0
        max_sec = 0
        total_file = 0
        items = []
        args = [{"item": item, "note_pitch_map": self.note_pitch_map, "note_dur_map": self.note_dur_map,
                 "note_tempo_map": self.note_tempo_map} for item in meta_data[:len(meta_data)]]
        # Get information from audio and transcript
        for item_id, item in multiprocess_run_tqdm(process_item, args, desc="Processing data"):
            if item is not None:
                items.append(item)
                if self.binarization_args["with_spk_f0_norm"] and prefix == "train":
                    self.calculate_spk_stats(item["f0"], item["spk_id"])
        # Use pre-trained speaker embeddings
        if self.binarization_args["with_spk_embed"]:
            args = [{"wav": item["wav"]} for item in items]
            for item_id, spk_embed in multiprocess_run_tqdm(
                    self.get_spk_embed, args,
                    init_ctx_func=lambda wid: {"voice_encoder": VoiceEncoder().cuda()}, num_workers=4,
                    desc="Extracting spk embed"):
                items[item_id]["spk_embed"] = spk_embed

        for item in items:
            if not self.binarization_args["with_wav"] and "wav" in item:
                del item["wav"]
            mel_lengths.append(item["len"])
            assert item["len"] > 0, (item["item_name"], item["text"], item["mel2ph"])
            if "ph_len" in item:
                ph_lengths.append(item["ph_len"])
            if max_sec < item["sec"]:
                max_sec = item["sec"]
            total_sec += item["sec"]
            if "midi_info" in item:
                del item["midi_info"]
                del item["sec"]
                del item["others"]
            if not self.binarization_args["with_mel"] and "mel" in item:
                del item["mel"]
            builder.add_item(item)
        total_file += len(items)
        builder.finalize()

        if os.path.exists(f"{data_dir}/{prefix}_lengths.npy"):
            mel_lengths_ = np.load(f"{data_dir}/{prefix}_lengths.npy").tolist()
            mel_lengths_.extend(mel_lengths)
            mel_lengths = mel_lengths_
        np.save(f"{data_dir}/{prefix}_lengths.npy", mel_lengths)
        if len(ph_lengths) > 0:
            if os.path.exists(f"{data_dir}/{prefix}_ph_lenghts.npy"):
                ph_lengths_ = np.load(f"{data_dir}/{prefix}_ph_lenghts.npy").tolist()
                ph_lengths.extend(ph_lengths_)
            np.save(f"{data_dir}/{prefix}_ph_lenghts.npy", ph_lengths)
        if self.binarization_args["with_spk_f0_norm"] and prefix == "train":
            self.build_spk_pitch_map()
        print(f"| {prefix} total files: {total_file}, total duration: {total_sec:.3f}s, max duration: {max_sec:.3f}s")

    @classmethod
    def process_item(cls, item: dict, note_pitch_map, note_dur_map, note_tempo_map, preprocess_args, binarization_args: dict):
        item["ph_len"] = len(item["ph_token"])
        item_name = item["item_name"]
        wav_fn = item["wav_fn"]
        # Get Waveform and Mel-spectrogram information
        wav, mel = cls.process_audio(wav_fn, item, binarization_args)
        if mel.shape[0] > 2:
            try:
                n_bos_frames, n_eos_frames = 0, 0
                if preprocess_args["use_text"] and preprocess_args["use_midi"]:
                    if binarization_args["with_midi_align"]:
                        # Align text information
                        cls.process_midi_align(item)
                # Mapping pitch and dur map of note
                cls.process_note(item, note_pitch_map, note_dur_map, note_tempo_map, preprocess_args, binarization_args)
                if binarization_args["trim_eos_bos"]:
                    n_bos_frames = item["duration"][0] if preprocess_args["use_midi"] else 0
                    n_eos_frames = item["duration"][-1] if preprocess_args["use_midi"] else mel.shape[0]
                    T = len(mel)
                    item["mel"] = mel[n_bos_frames:T - n_eos_frames]
                    item["mel2ph"] = item["mel2ph"][n_bos_frames:T - n_eos_frames]
                    item["duration"] = item["duration"][1:-1]
                    item["duration_midi"] = item["duration_midi"][1:-1]
                    item["len"] = item["mel"].shape[0]
                    item["wav"] = wav[n_bos_frames * hparams["hop_size"]:len(wav) - n_eos_frames * hparams["hop_size"]]
                if binarization_args["with_f0"]:
                    # Get pitch information
                    cls.process_pitch(item, n_bos_frames, n_eos_frames)
            except BinarizationError as e:
                print(f"| Skip item ({e}). item_name: {item_name}, wav_fm: {wav_fn}")
                return None
            except Exception as e:
                traceback.print_exc()
                print(f"| Skip item. item_name: {item_name}, wav_fm: {wav_fn}")
                return None
        return item

    @classmethod
    def process_audio(cls, wav_fn: str, res: dict, binarization_args: dict):
        # Get Mel-spectrogram
        wav2spec_dict = torch_wav2spec(wav_fn,
                                       fft_size=hparams["fft_size"],
                                       hop_size=hparams["hop_size"],
                                       win_length=hparams["win_size"],
                                       num_mels=hparams["num_mel_bins"],
                                       fmin=hparams["fmin"],
                                       fmax=hparams["fmax"],
                                       sample_rate=hparams["sample_rate"])
        mel = wav2spec_dict["mel"]
        wav = wav2spec_dict["wav"].astype(np.float16)

        #  Check Linear-spectrogram
        if binarization_args["with_linear"]:
            res["linear"] = wav2spec_dict["linear"]
        if "wav_norm" in wav2spec_dict:
            res["wav_norm"] = wav2spec_dict["wav_norm"]
        res.update({"mel": mel, "wav": wav, "sec": len(wav) / hparams["sample_rate"], "len": mel.shape[0]})
        return wav, mel

    @staticmethod
    def process_midi_align(item: dict):
        mel = item["mel"]
        midi_info = item["midi_info"]
        # Get align information and duration
        mel2phone, mel2note, duration, ph_token, ph_list, _, item["midi_info"] = get_mel2note(midi_info, mel, hparams["hop_size"],
                                                                                hparams["sample_rate"], item["silence"])
        item["ph_token"] = ph_token
        item["text"] = ph_list
        if len(ph_list) < hparams["binarization_args"]["min_text"] or ph_list is None:
            raise BinarizationError(
                f"| Less than min text sequence: {len(item['ph_token'])}")
        if np.array(mel2phone).max() - 1 >= len(ph_token):
            raise BinarizationError(
                f"Align does not match: mel2ph.max - 1 != {np.array(mel2phone).max() - 1}, len(ph_token): {len(ph_token)}")
        item["mel2ph"] = mel2phone
        item["mel2note"] = mel2note
        item["duration"] = duration
        # Get phoneme to word information
        assert len(ph_token) == len(duration), "| phoneme : {len(ph_token)}, ph_duration : {len(duration)}"

    @staticmethod
    def process_note(item, note_pitch_map, note_dur_map, note_tempo_map, preprocess_args, binarization_args):
        dur_enc = list()
        dur_dec = list()
        for i in range(binarization_args["max_durations"]):
            for _ in range(binarization_args["pos_resolution"]):
                dur_dec.append(len(dur_enc))
                for _ in range(2 ** i):
                    dur_enc.append(len(dur_dec) - 1)
        def d2e(x):
            return dur_enc[x] if x < len(dur_enc) else dur_enc[-1]
        if preprocess_args["use_midi"]:
            item["note_duration"] = [note_dur_map[str(d2e(note[3]))] for note in item["midi_info"]]
            item["note_pitch"] = [note_pitch_map[str(note[2])] for note in item["midi_info"]]
            item["note_tempo"] = [note_tempo_map[str(note[6])] for note in item["midi_info"]]
        else:
            item["note_duration"] = [0]
            item["note_pitch"] = [0]
            item["note_tempo"] = [0]

    @staticmethod
    def process_pitch(item: dict, n_bos_frames: int, n_eos_frames: int):
        wav, mel = item["wav"], item["mel"]
        # Get f0 from waveform
        f0 = extract_pitch_simple(wav)
        if sum(f0) == 0:
            raise BinarizationError("Empty f0")
        assert len(mel) == len(f0) // hparams["f0_resolution"], (len(mel), len(f0))
        # Quantize f0 values
        pitch_coarse = f0_to_coarse(f0)
        item["f0"] = f0
        item["pitch"] = pitch_coarse
        if hparams["binarization_args"]["with_f0cwt"]:
            _, cont_logf0 = get_cont_logf0(f0)
            logf0s_mean, logf0s_std = np.mean(cont_logf0), np.std(cont_logf0)
            cont_logf0_norm = (cont_logf0 - logf0s_mean) / logf0s_std
            cwt_spec, _ = get_logf0_cwt(cont_logf0_norm)
            item["cwt_spec"] = cwt_spec
            item["cwt_mean"] = logf0s_mean
            item["cwt_std"] = logf0s_std

    def build_pitch_map(self):
        """ Using 0 to 128 notes for MIDI. """
        pitch_map = {"0": 0}
        for i, x in enumerate(range(self.hparams["note_range"][0], self.hparams["note_range"][1])):
            pitch_map[str(x)] = i + 1
        json.dump(pitch_map, open(f"{self.binary_data_dir}/pitch_map.json", "w"), ensure_ascii=False)

        return pitch_map

    def build_dur_map(self):
        """ Using max duration for MIDI. """
        dur_map = {"[PAD]": 0, "[BOS]": 1, "[EOS]": 2}
        for i, x in enumerate(range(0, 128)):
            dur_map[str(x)] = i + 4
        json.dump(dur_map, open(f"{self.binary_data_dir}/dur_map.json", "w"), ensure_ascii=False)
        return dur_map

    def build_tempo_map(self):
        tempo_map = {"[PAD]": 0, "[BOS]": 1, "[EOS]": 2}
        tempo_range = self.binarization_args["tempo_range"]
        for i, x in enumerate(range(tempo_range[0], tempo_range[1] + 1)):
            tempo_map[str(x)] = i + 4
        json.dump(tempo_map, open(f"{self.binary_data_dir}/tempo_map.json", "w"), ensure_ascii=False)
        return tempo_map

    def calculate_spk_stats(self, f0, spk_id):
        f0_min = f0[np.nonzero(f0)].min()
        f0_max = f0.max()
        if str(spk_id) in self.spk_pitch_map:
            spk_pitch_stat = self.spk_pitch_map[str(spk_id)]
            if spk_pitch_stat["min"] > f0_min:
                self.spk_pitch_map[str(spk_id)]["min"] = f0_min
            if spk_pitch_stat["max"] < f0_max:
                self.spk_pitch_map[str(spk_id)]["max"] = f0_max
        else:
            spk_pitch_stat = {}
            spk_pitch_stat["max"] = f0_max
            spk_pitch_stat["min"] = f0_min
            self.spk_pitch_map[str(spk_id)] = spk_pitch_stat

    def build_spk_pitch_map(self):
        spk_pitch_map = {}
        stat_map_dir = f"{self.binary_data_dir}/spk_pitch_map.json"
        if os.path.exists(stat_map_dir):
            spk_pitch_map = json.load(open(stat_map_dir, "r"))
        spk_pitch_map.update(self.spk_pitch_map)
        spk_pitch_map = {key: value for key, value in sorted(spk_pitch_map.items(), key=lambda x: int(x[0]))}
        print("| Statistics of speaker's pitch is saved.")
        json.dump(spk_pitch_map, open(stat_map_dir, "w"), ensure_ascii=False)

    @staticmethod
    def get_spk_embed(wav, ctx):
        return ctx["voice_encoder"].embed_utterance(wav.astype(float))

    @property
    def num_workers(self):
        return int(os.getenv("N_PROC", hparams.get("N_PROC", os.cpu_count())))


from turtle import Turtle

ALIGNMENT = 'center'
FONT = ('courier', 20, 'normal')

class GameOver(Turtle):

    def __init__(self):
        super().__init__()
        self.hideturtle()
        self.color('white')
        self.penup()
        self.write(arg=f'Game Over', align=ALIGNMENT, move=False, font=FONT)


# -*- coding: utf-8  -*-
import yaml

from . import instructions
from . import value_containers
from .exceptions import ParserException
from .method import Method

def _process_func(method, func):
    if not func or not isinstance(func, dict):
        raise ParserException('"func" not defined')
    method.function_name = func['name']
    method.return_type = value_containers.types[func['type'].lower()]()
    _process_vars(method, func['args'], inc_arg_count=True)

def _add_label(method, label, index):
    if not isinstance(label, str):
        raise ParserException('label %s needs to be a string' % label)
    if label is None:
        raise ParserException('every local variable needs a label')
    if label in method.labels:
        raise ParserException('labels has to be unique: duplicate %s' % label)
    method.labels[label] = index

def _process_vars(method, args, inc_arg_count=False):
    for arg in args:
        t = arg['type'].lower()
        if inc_arg_count:
            method.argument_count += 1
        method.variables.append(value_containers.types[t]())
        l = arg.get('label', None)
        _add_label(method, l, len(method.variables) - 1)

def _process_ins(method, ins):
    # pass1: collect labels
    offset = 0
    label = None
    label_current = None
    for i in ins:
        if isinstance(i, dict):
            if not len(i) == 1:
                raise ParserException('bad syntax for data %s' % i)
            label_current = i.get('label', None)
            if label and label_current:
                raise ParserException('label cannot follow label: %s, %s' % (label, label_current))
            label = label_current
        else:
            label = None
            label_current = None
        if label:
            _add_label(method, label, offset)
        else:
            offset += 1
    else:
        if label:
            raise ParserException('label cannot be as last instruction %s' % i)

    # pass2: use labels and collect instructions
    offset = 0
    for i in ins:
        if isinstance(i, dict) and i.get('label', None):
            continue
        else:
            offset += 1
        if isinstance(i, str):
            inst = instructions.keywords[i.lower()]
            if not issubclass(inst, instructions.InsNoArgument):
                raise ParserException('instruction %s requires argument' % i)
            method.code.append(inst())
        elif isinstance(i, dict):
            kw, value = i.popitem()
            inst = instructions.keywords[kw.lower()]
            if issubclass(inst, instructions.InsNoArgument):
                raise ParserException('instruction %s takes no argument' % i)
            if issubclass(inst, instructions.InsArgILabel):
                if isinstance(value, str):
                    try:
                        value = method.labels[value]
                    except KeyError:
                        raise ParserException('label %s is not defined' % value)
                else:
                    raise ParserException('instruction %s requires label as argument' % i)
            elif issubclass(inst, instructions.InsArgInteger):
                if value != int(value):
                    raise ParserException('instruction %s requires integer argument' % i)
            elif issubclass(inst, instructions.InsArgFloat):
                if value != float(value):
                    raise ParserException('instruction %s requires float argument' % i)

            method.code.append(inst(instructions.contain_value(inst, value)))
        else:
            raise ParserException('unknown instruction format %s' % i)

def process_yaml(structure):
    m = Method()
    _process_func(m, structure.get('func', {}))
    _process_vars(m, structure.get('lvars', []))
    _process_ins(m, structure.get('ins', []))
    return m

def parse_string(data):
    structure = yaml.safe_load(data)
    return process_yaml(structure)

def parse_file(fname):
    with open(fname, 'r') as f:
        return parse_string(f)


import openpyxl
from openpyxl.utils import cell

def read(config):
    try:
        workbook = openpyxl.load_workbook(config.source_path, data_only=True)
        datasets = {}
        for source_tab_name in config.source_tabs:
            datasets[source_tab_name] = extract_dataset(workbook, source_tab_name, config)
        return datasets
    finally:
        workbook.close()

def extract_dataset(workbook, source_tab_name, config):
    worksheet = workbook.get_sheet_by_name(source_tab_name)
    last_data_row = find_last_data_row(worksheet, config.start_search_row, config.column_range_start)
    copy_range_start = "{}{}".format(config.column_range_start, config.start_search_row)
    copy_range_end   = "{}{}".format(config.column_range_stop, last_data_row)
    return worksheet[copy_range_start:copy_range_end]

def find_last_data_row(worksheet, start_row, column_range_start):
    column_index = cell.column_index_from_string(column_range_start)
    for current_row in range(start_row, worksheet.max_row):
        val = worksheet.cell(row = current_row, column = column_index).value
        if worksheet.cell(row = current_row, column = column_index).value == None:
            return current_row - 1
    return worksheet.max_row


stock = {
    "banana": 6,
    "apple": 0,
    "orange": 32,
    "pear": 15
}
prices = {
    "banana": 4,
    "apple": 2,
    "orange": 1.5,
    "pear": 3
}
price = 0

for fruit in stock:
    price += stock[fruit] * prices[fruit]

print(str(int(price)) + " bucks")


class SiteData:

    lang_dict = {
        'C++': 'image/langs/cpp-logo.png',
        'Python': 'image/langs/python-logo.jpg',
        'Go': 'image/langs/go-logo.png',
        'PL/pgSQL': 'image/langs/plpgsql.png'
    }

    projects = [
        {
            'name': 'web-testing-tool',
            'image': lang_dict['Python'],
            'front_description': 'Students testing tool.',
            'back_description': 'Testing tool implemented on Django. Data storage is implemented in the DBMS MongoDB.',
            'tags': [
                'web',
                'python',
                'html5',
                'js',
                'bootstrap4',
                'django',
                'rest api'
                'mongo',
                'docker',
                'travis ci',
                'github actions'
            ],
            'github_name': 'web-testing-tool',
        },
        {
            'name': 'video-tracker',
            'image': lang_dict['C++'],
            'front_description': 'Detecting and tracking objects on video with OpenCV usage.',
            'back_description': 'Application for tracking various objects and estimating their speed in video stream.',
            'tags': [
                'c++17',
                'computer vision',
                'ml',
                'cmake',
                'opencv',
                'dlib',
                'mobile net ssd',
                'video detection',
                'video tracking'
            ],
            'github_name': 'video-tracker',
        },
        {
            'name': 'smart-text-editor',
            'image': lang_dict['Python'],
            'front_description': 'Text editor with intelligent typing based on relevant search in Elasticsearch.',
            'back_description': 'Web application for editing text documents with support for intelligent typing ' +
                                'based on relevant search in Elasticsearch.',
            'tags': [
                'web',
                'python',
                'flask',
                'rest api',
                'elasticsearch',
                'docker',
                'travis ci',
                'github actions'
            ],
            'github_name': 'smart-text-editor'
        },
        {
            'name': 'vk-news-dashboard',
            'image': lang_dict['Python'],
            'front_description': 'Simple dashboard for vk news groups.',
            'back_description': 'Simple dashboard implemented on Dash. Provides monitoring VK news groups posts.',
            'tags': [
                'web',
                'python',
                'dash',
                'nlp',
                'postgres',
                'docker',
                'docker compose',
                'travis ci'
            ],
            'github_name': 'vk-news-dashboard',
            'ref': 'http://vknews.vnkrtv.ru/',
        },
        {
            'name': 'vk-tracker',
            'image': lang_dict['Python'],
            'front_description': 'Web app for tracking, analyzing and searching people in VK.',
            'back_description': 'Web app for tracking and searching VK users. ' +
                                'Implemented on django and dash, data is stored in MongoDB and Neo4j. ',
            'tags': [
                'web',
                'python',
                'html5',
                'js',
                'bootstrap4',
                'django',
                'dash',
                'mongo',
                'neo4j',
                'docker',
                'docker compose',
                'travis ci',
                'github actions',
                'vk api'
            ],
            'github_name': 'vk-tracker',
        },
        {
            'name': 'go-vk-news-loader',
            'image': lang_dict['Go'],
            'front_description': 'Service loading news from vk news groups.',
            'back_description': 'Service providing loading news from list of vk news groups into PostgreSQL DB.',
            'tags': [
                'microservice',
                'go',
                'postgres',
                'docker',
                'docker compose',
                'travis ci'
            ],
            'github_name': 'go-vk-news-loader',
        },
        {
            'name': 'telegram-channels-loader',
            'image': lang_dict['Python'],
            'front_description': 'Service providing loading messages from telegram channels.',
            'back_description': 'Service providing loading messages from given telegram channels to PostgreSQL DB.',
            'tags': [
                'microservice',
                'python',
                'telegram',
                'async',
                'postgres',
                'docker',
                'docker compose',
                'travis ci'
            ],
            'github_name': 'telegram-channels-loader',
        },
        {
            'name': 'tiktok-loader',
            'image': lang_dict['Python'],
            'front_description': 'Service loading TikTok information.',
            'back_description': 'Service loading information about russian TikTik popular users. ' +
                                'Loads to DB latest tiktoks with audios and videos.',
            'tags': [
                'microservice',
                'python',
                'tiktok',
                'postgres',
                'docker',
                'docker compose',
                'travis ci'
            ],
            'github_name': 'tiktok-loader',
        },
        {
            'name': 'web-library',
            'image': lang_dict['Python'],
            'front_description': 'Web app for storing and adding translations of various compositions.',
            'back_description': 'App for adding and storing translations of various compositions. ' +
                                'All data stored in PostgreSQL, web app implemented in Django.',
            'tags': [
                'web',
                'python',
                'html5',
                'js',
                'docker',
                'docker compose',
                'travis ci',
                'github actions'
            ],
            'github_name': 'web-library',
        },
        {
            'name': 'go-habr-loader',
            'image': lang_dict['Go'],
            'front_description': 'Utility loading all posts from habr.com.',
            'back_description': 'Utility providing loading all posts from habr.com into PostgreSQL DB.',
            'tags': [
                'utility',
                'go',
                'postgres',
                'docker',
                'travis ci'
            ],
            'github_name': 'go-habr-loader',
            'ref': 'https://www.kaggle.com/leadness/habr-posts/'
        },
        {
            'name': 'postgres-markov-chain',
            'image': lang_dict['PL/pgSQL'],
            'front_description': 'Text generator based on Markov chain.',
            'back_description': 'Text generator based on Markov chain written on pure PL/pgSQL. Providing text ' +
                                'processing, training Markov chain and generating text.',
            'tags': [
                'utility',
                'postgres',
                'plpgsql',
                'nlp',
            ],
            'github_name': 'postgres-markov-chain',
        },
        {
            'name': 'go-wiki-parser',
            'image': lang_dict['Go'],
            'front_description': 'Utility parsing large xml wiki dump into MongoDB',
            'back_description': 'Utility providing parsing large xml wiki dump into MongoDB. Stored each article as ' +
                                'dict of subtopic with list of all references for other articles.',
            'tags': [
                'utility',
                'go',
                'travis ci'
            ],
            'github_name': 'go-wiki-parser',
        },
        {
            'name': 'web-db-manager',
            'image': lang_dict['Python'],
            'front_description': 'Web app for managing specific MS SQL Server DB.',
            'back_description': 'Web application for working with the subject database of the shop stored in MS SQL ' +
                                'Server DB.',
            'tags': [
                'web',
                'python',
                'html5',
                'js',
                'flask',
                'mssql server',
                'travis ci'
            ],
            'github_name': 'web-db-manager',
        },
        {
            'name': 'matrix-multiplier',
            'image': lang_dict['C++'],
            'front_description': 'Calculates all possible products of vectors in a given matrix.',
            'back_description': 'Calculates all possible products of vectors in a given matrix with specified ' +
                                'threshold, preserving the final product chains.',
            'tags': [
                'utility',
                'c++14',
                'libtorch',
                'qt5',
                'map reduce',
                'cmake',
                'cuda'
            ],
            'github_name': 'matrix-multiplier',
        },
        {
            'name': 'cmd-vk-tracker',
            'image': lang_dict['C++'],
            'front_description': 'Console application for Windows providing monitoring VK users.',
            'back_description': 'Console application providing loading VK users information, getting ' +
                                'account changes and users relationship.',
            'tags': [
                'utility',
                'c++14',
                'cmake',
                'vk api'
            ],
            'github_name': 'cmd-vk-tracker',
        },
        {
            'name': 'wifi-detect',
            'image': lang_dict['C++'],
            'front_description': 'Small console application for detecting Wi-Fi routers and connected to them devices.',
            'back_description': 'Detects wifi routers and all devices, connected to them. Also records all captured ' +
                                'traffic in capture.pcap.',
            'tags': [
                'utility',
                'c++14',
                'cmake'
            ],
            'github_name': 'wifi-detect',
        },
        {
            'name': 'tf-net-analizer',
            'image': lang_dict['Python'],
            'front_description': 'Notebooks with NN classifying internet traffic.',
            'back_description': 'Notebooks with NN classifying internet traffic from KDD Cup 1999 data. Implemented '
                                'on TensowFlow.',
            'tags': [
                'data science',
                'ml',
                'python',
                'tensorflow',
                'sklearn',
                'jupyter notebook'
            ],
            'github_name': 'tf-net-analizer',
            'ref': 'https://www.kaggle.com/leadness/knncup99-ffnn-0-96-acc-0-57-weighted-acc'
        },
        {
            'name': 'go-tcp-chat',
            'image': lang_dict['Go'],
            'front_description': 'Simple TCP chat with tui.',
            'back_description': 'Simple TCP chat on sockets with text user interface.',
            'tags': [
                'service',
                'go'
            ],
            'github_name': 'go-tcp-chat'
        },
        {
            'name': 'my-site',
            'image': lang_dict['Python'],
            'front_description': 'This site.',
            'back_description': 'Simple single page site implemented on Flask.',
            'tags': [
                'web',
                'python',
                'html5',
                'bootstrap4',
                'flask'
            ],
            'github_name': 'my-site'
        }
    ]


from array import *
import os
from PIL import Image

Buttons=[0x300fd40bf,0x300fdc03f,0x300fd20df,0x300fda05f,0x300fd609f,0x300fde01f,0x300fd10ef,0x300fd906f,0x300fd50af,0x300fd30cf,0x300fdb24d,0x300fd728d,0x300fdf20d,0x300fd8877,0x300fd48b7]
ButtonsNames=["One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Zero","MENU","TITLE","L/R","Left_key","Right_key"]
distantToFolderMappings = {
    12: "C:/Users/rakib/OneDrive/Desktop/Optoshi/12 Feet",
    10: "C:/Users/rakib/OneDrive/Desktop/Optoshi/10 Feet",
    8: "C:/Users/rakib/OneDrive/Desktop/Optoshi/8 Feet"
}
menuMapping = {
    "One": "e",
    "Two": "pediatric"
}
distanceMapping = {
    "Eight": 8,
    "Ten": 10
}
def convertHex(binaryValue):
	tmpB2 =int(str(binaryValue),2)
	return hex(tmpB2)

class Folder:
    menuId = ""
    images = []
    name = ""
    currentIndex = 0
    def __init__(self, distance, menuId):
        self.name = menuMapping[menuId]
        distancePath = distantToFolderMappings[distance]
        actualFolderPath = distancePath+"/"+self.name
        folderImages = self.get_files_in_directory(actualFolderPath)
        self.images = [file for file in folderImages if file.endswith(".png") or file.endswith(".jpg")]

        self.currentIndex = 0
        self.menuId = menuId
        self.openImage()
        #print(self.images)
    def openImage(self):
        print("Opening from Folder "+self.name+" with index "+str(self.currentIndex))
        img = Image.open(self.images[self.currentIndex])
        self.currentImage = img
        img.show()
    def moveIndexToLeft(self):
         if self.currentIndex-1<0:
            self.currentIndex = len(self.images)-1
         else:
            self.currentIndex = self.currentIndex-1
         print("Processing done")
    def moveIndexToRight(self):
         if self.currentIndex+1<len(self.images):
            self.currentIndex = self.currentIndex + 1
         else:
            self.currentIndex = 0

    def processIrSignal(self, signal):
        status = 1
        if signal == "Left":
           self.moveIndexToLeft()
           print(signal + " Pressed on " + self.name)
        elif signal == "Right":
            self.moveIndexToRight()
            print(signal + " Pressed on " + self.name)
        elif signal == self.findKey(menuMapping, self.name):
            print("Menu button pressed. Opening Next image")
            self.moveIndexToRight()
        else:
            status = -1
            print("Command not found inside class and status is -1")
        if status == 1:
            self.openImage()
        return status
    def getImagePath(self, distance):
        return distantToFolderMappings[distance]+self.name
    def get_files_in_directory(self, directory):
        return [os.path.join(directory, f) for f in os.listdir(directory) if os.path.isfile(os.path.join(directory, f))]

    def findKey(self, dictionary, value):
        for dict_key, dict_value in dictionary.items():
            if dict_value == value:
                return dict_key
        return None

currentFolder = None
def createFolder(distance, value):
    if value in menuMapping:
        return Folder(distance, value)
    else:
        return None
def process_signal(distance, value):
    selButton = value
    global currentFolder
    if currentFolder is not None:
        status = currentFolder.processIrSignal(selButton)
        if status == -1:
            print("Status found -1 from processSignal")
            newFolder = createFolder(distance, value)
            if newFolder is not None:
                currentFolder = newFolder
    else:
        currentFolder = createFolder(distance, value)
        if currentFolder is not None:
            print(menuMapping[value]+" Menu Selected")
        else:
            print("Command not found")

while True:
    distance = 10
    while True:
        inData = input("Enter a string: ")
        if inData == "Exit" and currentFolder is None:
            print("Exiting to select distance")
            currentFolder = None
            break
        elif inData == "Exit" and currentFolder is not None:
            print("Exiting to Select folder")
            currentFolder = None
        elif inData in distanceMapping:
            distance = distanceMapping[inData]
            print(str(distance) + " Selected")
            currentFolder = None
        else:
            process_signal(distance, inData)
            print("Action taken with "+str(distance)+" feet distance")


from flask import render_template, flash, redirect, url_for, request, current_app
from flask_login import login_required, current_user

from apps.app import db
from apps.model import Task, Kind
from apps.todolist import todolist
from apps.todolist.course import AddCategory, AddToDoList, ChangeToDoList

# User View Task List
@todolist.route('/list/', methods=['GET', 'POST'])
@login_required
def list_1():
    list = AddToDoList()
    page = int(request.args.get('page', 1))
    todolistPage = Task.query.filter_by(user_id=current_user.id).paginate(per_page=current_app.config['PER_PAGE'],
                                                                          error_out=False)

    return render_template('todolist/list.html', todoObj=todolistPage, form=list)

@todolist.route('/add/', methods=['POST', 'GET'])
@login_required
def add_list():
    list = AddToDoList()
    page = int(request.args.get('page', 1))
    todolistPage = Task.query.filter_by(user_id=current_user.id).paginate(per_page=current_app.config['PER_PAGE'],
                                                                          error_out=False)

    if list.validate_on_submit():
        # get users submission
        Title = list.Title.data
        content = list.content.data
        kind_id = list.category.data
        urgent = list.urgent.data
        deadline = list.deadline.data
        # add to database
        status = False
        add = Task(task_Title=Title, task_content=content,
                   category_id=kind_id,
                   task_urgent=urgent, task_deadline=deadline, user_id=current_user.id, task_status=status)
        db.session.add(add)
        flash('Add task successfully!', category='success')
        return redirect(url_for('todolist.add_list'))
    else:
        flash('Cannot add this task!', category='error')
        return render_template('todolist/list.html', todoObj=todolistPage, form=list)

@todolist.route('/change/<int:id>', methods=['GET', 'POST'])
def change(id):
    list = ChangeToDoList()
    page = int(request.args.get('page', 1))
    todolistPage = Task.query.filter_by(user_id=current_user.id).paginate(per_page=current_app.config['PER_PAGE'],
                                                                          error_out=False)
    task = Task.query.filter_by(id=id).first()

    list.content.data = task.task_content
    # list.category.data = task.category_id
    # list.urgent.data = task.task_urgent
    # list.deadline.data = task.task_deadline
    list.Title.data = task.task_moduleTitle
    list.urgent.data = task.task_urgent
    list.category.data = task.category_id

    if list.validate_on_submit():
        content = request.form.get('content')
        Title = request.form.get('Title')
        category_id = request.form.get('category')
        urgent = request.form.get('urgent')
        task.task_content = content
        task.category_id = category_id
        task.urgent = urgent
        task.task_Title = Title

        db.session.add(task)
        flash('Task has been changed', category='success')
        return redirect(url_for('todolist.list_1'))
    else:
        flash('Changed failed', category='error')
        return render_template('todolist/change.html', form=list, todoObj=todolistPage)

@todolist.route('/delete/<int:id>/')
@login_required
def delete(id):
    task = Task.query.filter_by(id=id).first()
    # task.task_status = True
    db.session.delete(task)
    flash("Task has been deleted successfully.", category='success')
    return redirect(url_for('todolist.list_1'))

@todolist.route('/deletec/<int:id>/')
@login_required
def deletec(id):
    kind = Kind.query.filter_by(id=id).first()
    # task.task_status = True
    db.session.delete(kind)
    flash("Category has been deleted successfully.", category='success')
    category_list = AddCategory()
    KindPage = Kind.query.filter_by(user_id=current_user.id).paginate(per_page=current_app.config['PER_PAGE'],
                                                                      error_out=False)
    return render_template('todolist/category.html', kindObj=KindPage, form=category_list)

@todolist.route('/hasdone/<int:id>/')
@login_required
def hasdone(id):
    task = Task.query.filter_by(id=id).first()
    task.task_status = True
    db.session.add(task)
    flash("Task status has been changed successfully.", category='success')
    return redirect(url_for('todolist.list_1'))

@todolist.route('/hasnotdone/<int:id>/')
@login_required
def hasnotdone(id):
    task = Task.query.filter_by(id=id).first()
    task.task_status = False
    db.session.add(task)
    flash("Task status has been changed successfully.", category='success')
    return redirect(url_for('todolist.list_1'))

@todolist.route('/urgent/<int:id>/')
@login_required
def urgent(id):
    task = Task.query.filter_by(id=id).first()
    task.task_urgent = 1
    db.session.add(task)
    flash("Task priority has been changed successfully.", category='success')
    return redirect(url_for('todolist.list_1'))

@todolist.route('/noturgent/<int:id>/')
@login_required
def noturgent(id):
    task = Task.query.filter_by(id=id).first()
    task.task_urgent = 2
    db.session.add(task)
    flash("Task priority has been changed successfully.", category='success')
    return redirect(url_for('todolist.list_1'))

@todolist.route('/search/<int:id>/', methods=['POST', 'GET'])
@login_required
def search(id):
    task = Task.query.filter_by(id=id).all()
    return render_template('todolist/search.html', todoObj=task)

@todolist.route('/searchTitle/<int:id>/', methods=['POST', 'GET'])
@login_required
def search_Title(id):
    Title = request.form.get('Title')
    task = Task.query.filter_by(user_id=id, task_Title=Title).paginate(per_page=current_app.config['C_PAGE'],
                                                                          error_out=False)
    return render_template('todolist/search.html', todoObj=task)

@todolist.route('/searchContent/<int:id>/', methods=['POST', 'GET'])
@login_required
def search_Content(id):
    content = request.form.get('content')
    task = Task.query.filter_by(user_id=id, task_content=content).paginate(per_page=current_app.config['C_PAGE'],
                                                                          error_out=False)
    return render_template('todolist/search.html', todoObj=task)

@todolist.route('/searchPriority/<int:id>/', methods=['POST', 'GET'])
@login_required
def search_Urgent(id):
    priority = request.form.get('priority')
    task = Task.query.filter_by(user_id=id, task_urgent=priority).paginate(per_page=current_app.config['C_PAGE'],
                                                                          error_out=False)

    return render_template('todolist/search.html', todoObj=task)

@todolist.route('/category/', methods=['POST', 'GET'])
@login_required
def category():
    category_list = AddCategory()
    KindPage = Kind.query.filter_by(user_id=current_user.id).paginate(per_page=current_app.config['PER_PAGE'],
                                                                      error_out=False)
    return render_template('todolist/category.html', kindObj=KindPage, form=category_list)

@todolist.route('/addcategory/', methods=['POST', 'GET'])
@login_required
def add_category():
    list = AddCategory()
    KindPage = Kind.query.filter_by(user_id=current_user.id).paginate(per_page=current_app.config['PER_PAGE'],
                                                                      error_out=False)
    if list.validate_on_submit():
        # get category submission
        name = list.category.data
        user_id = current_user.id
        # add to database
        add = Kind(name=name, user_id=user_id)
        db.session.add(add)
        flash('Add category successfully!', category='success')
        return redirect(url_for('todolist.category'))
    else:
        flash('Cannot add this category', category='error')
        return render_template('todolist/category.html', kindObj=KindPage, form=list)

@todolist.route('/complete/<int:id>/', methods=['POST', 'GET'])
@login_required
def complete(id):
    todolistPage = Task.query.filter_by(user_id=current_user.id).paginate(per_page=current_app.config['C_PAGE'],
                                                                          error_out=False)

    return render_template('todolist/complete.html', todoObj=todolistPage)

@todolist.route('/uncomplete/<int:id>/', methods=['POST', 'GET'])
@login_required
def uncomplete(id):
    todolistPage = Task.query.filter_by(user_id=current_user.id).paginate(per_page=current_app.config['C_PAGE'],
                                                                          error_out=False)

    return render_template('todolist/uncomplete.html', todoObj=todolistPage)


from __future__ import print_function, division
import vim
import subprocess

python_format = '/home/ekern/python_format/python_format.py'

if __name__ == '__main__':
    start = vim.current.range.start
    end = vim.current.range.end
    buf = vim.current.buffer

    while buf[end].rstrip().endswith('\\'):
        end += 1

    while (start >= 0) and buf[start - 1].rstrip().endswith('\\'):
        start -= 1

    text = ''.join([l + '\n' for l in buf[start : end + 1]])

    p = subprocess.Popen(['python', python_format], stdout = subprocess.PIPE,
        stderr = subprocess.PIPE, stdin = subprocess.PIPE)

    stdout, stderr = p.communicate(input = text)

    if not stdout and not stderr:
        print('No output from python_format')
    elif stderr:
        print('Error from python_format:', stderr)
    elif stdout != text:
        lines = stdout.splitlines()

        vim.command('%d,%ds/%s/%s/g' %
            (start + 1, end + 1, '^.*\\n' * (end - start + 1),
            '\\r' * len(lines)))

        for i, line in enumerate(lines):
            buf[start + i] = line


import time
from torch import optim
from torch.utils.data import DataLoader
from torchvision import datasets, transforms
from ..models import *
from ..utils import AverageMeter, calculate_accuracy, Logger, MyDataset
from visdom import Visdom

DatasetsList = ['CIFAR10', 'CIFAR100']

ModelList = {'AlexNet': AlexNet, 'alexnet': alexnet,
             'DenseNet': DenseNet, 'densenet121': densenet121, 'densenet169': densenet169,
             'densenet201': densenet201, 'densenet161': densenet161,
             'Inception3': Inception3, 'inception_v3': inception_v3,
             'ResNet': ResNet, 'resnet18': resnet18, 'resnet34': resnet34,
             'resnet50': resnet50, 'resnet101': resnet101, 'resnet152': resnet152,
             'SqueezeNet': SqueezeNet, 'squeezenet1_0': squeezenet1_0, 'squeezenet1_1': squeezenet1_1,
             'VGG': VGG, 'vgg11': vgg11, 'vgg11_bn': vgg11_bn, 'vgg13': vgg13, 'vgg13_bn': vgg13_bn,
             'vgg16': vgg16, 'vgg16_bn': vgg16_bn, 'vgg19_bn': vgg19_bn, 'vgg19': vgg19,
             'se_resnet18': se_resnet18, 'se_resnet34': se_resnet34, 'se_resnet50': se_resnet50,
             'se_resnet101': se_resnet101, 'se_resnet152': se_resnet152,
             'hr18_net': hr18_net,
             'mobilenetv3': mobilenetv3,
             'shufflenetv2': shufflenetv2}

class TrainPipline(object):
    def __init__(self, opt):
        self.root_path = opt['path']['root_path']
        self.result_path = os.path.join(self.root_path, opt['path']['result_path'])
        self.datasets_path = os.path.join(self.root_path, opt['path']['datasest_path'])

        self.n_classes = opt['model']['n_classes']
        self.momentum = opt['model']['momentum']
        self.weight_decay = opt['model']['weight_decay']
        self.nesterov = opt['model']['nesterov']

        self.n_epochs = opt['train']['n_epochs']
        self.batch_size = opt['train']['batch_size']
        self.learning_rate = opt['train']['learning_rate']
        self.n_threads = opt['train']['n_threads']
        self.checkpoint = opt['train']['checkpoint']

        self.no_cuda = opt['cuda']['no_cuda']
        self.model_name = ''
        self.model_ft = ''

        self.visdom_log_file = os.path.join(self.result_path, 'log_files', 'visdom.log')
        self.vis = Visdom(port=8097,
                          log_to_filename=self.visdom_log_file,
                          env='myTest_1')

        self.vis_loss_opts = {'xlabel': 'epoch',
                              'ylabel': 'loss',
                              'title': 'losses',
                              'legend': ['train_loss', 'val_loss']}

        self.vis_tpr_opts = {'xlabel': 'epoch',
                             'ylabel': 'tpr',
                             'title': 'val_tpr',
                             'legend': ['tpr@fpr10-2', 'tpr@fpr10-3', 'tpr@fpr10-4']}

        self.vis_epochloss_opts = {'xlabel': 'epoch',
                                   'ylabel': 'loss',
                                   'title': 'epoch_losses',
                                   'legend': ['train_loss', 'val_loss']}

    def datasets(self, data_name=None):
        assert data_name in DatasetsList

        if data_name == 'CIFAR10':
            training_data = datasets.CIFAR10(root='./modelzoo/datasets/', train=True, download=False,
                                             transform=transforms.Compose([
                                                       # transforms.RandomResizedCrop(224),
                                                       transforms.Pad(96),
                                                       transforms.ToTensor(),
                                                       transforms.Normalize((0.1307,), (0.3081,))]))

            val_data = datasets.CIFAR10(root='./modelzoo/datasets/', train=False, download=False,
                                        transform=transforms.Compose([
                                                  # transforms.RandomResizedCrop(224),
                                                  transforms.Pad(96),
                                                  transforms.ToTensor(),
                                                  transforms.Normalize((0.1307,), (0.3081,))]))

        elif data_name == 'CIFAR100':
            training_data = datasets.CIFAR100(root='./modelzoo/datasets/', train=True, download=True,
                                              transform=transforms.Compose([
                                                        # transforms.RandomResizedCrop(224),
                                                        transforms.Pad(96),
                                                        transforms.ToTensor(),
                                                        transforms.Normalize((0.1307,), (0.3081,))]))

            val_data = datasets.CIFAR100(root='./modelzoo/datasets/', train=False, download=True,
                                         transform=transforms.Compose([
                                                   # transforms.RandomResizedCrop(224),
                                                   transforms.Pad(96),
                                                   transforms.ToTensor(),
                                                   transforms.Normalize((0.1307,), (0.3081,))]))
        else:
            train_txt_path = os.path.join(self.datasets_path, 'train.txt')
            val_txt_path = os.path.join(self.datasets_path, 'val.txt')

            my_transform = transforms.Compose([transforms.Resize(224),
                                               transforms.ToTensor()])

            training_data = MyDataset(train_txt_path, transform=my_transform)
            val_data = MyDataset(val_txt_path, transform=my_transform)

        return training_data, val_data

    def model(self, model_name='resnet18', model_path=None):
        assert model_name in ModelList
        self.model_name = model_name
        # model_ft = resnet18(pretrained=True)
        # num_ftrs = model_ft.fc.in_features
        # model_ft.fc = nn.Linear(num_ftrs, 10)

        self.model_ft = ModelList[self.model_name](num_classes=self.n_classes)
        if model_path is not None:
            self.model_ft.load_state_dict(model_path)
        else:
            self.model_ft.apply(weights_init)

        return self.model_ft

    def train(self, training_data, val_data, model):
        # data init
        train_loader = DataLoader(training_data,
                                  batch_size=self.batch_size,
                                  shuffle=True,
                                  # num_workers=self.n_threads,
                                  pin_memory=True)
        # result writer
        train_logger = Logger(os.path.join(self.result_path, self.model_name + '_train.log'),
                              ['epoch', 'loss', 'acc', 'lr'])
        train_batch_logger = Logger(os.path.join(self.result_path, self.model_name + '_train_batch.log'),
                                    ['epoch', 'batch', 'iter', 'loss', 'acc', 'lr'])
        val_logger = Logger(os.path.join(self.result_path, self.model_name + '_test.log'),
                            ['time', 'loss', 'acc'])

        # optimizer init
        optimizer = optim.SGD(model.parameters(),
                              lr=self.learning_rate,
                              momentum=self.momentum,
                              weight_decay=self.weight_decay,
                              nesterov=self.nesterov)

        # loss init
        criterion = nn.CrossEntropyLoss()

        print(model)
        if not self.no_cuda:
            model = nn.DataParallel(model, device_ids=[0, 1, 2, 3]).cuda()
        # start train
        for i in range(0, self.n_epochs + 1):
            self.train_epoch(i, train_loader, model, criterion, optimizer,
                             train_logger, train_batch_logger)
            self.validation(val_data, model, criterion, val_logger)

    def train_epoch(self, epoch, data_loader, model, criterion, optimizer,
                    epoch_logger, batch_logger):
        print('train at epoch {}'.format(epoch))
        # set model to train mode
        model.train()

        batch_time = AverageMeter()
        data_time = AverageMeter()
        losses = AverageMeter()
        accuracies = AverageMeter()

        end_time = time.time()
        for i, (inputs, targets) in enumerate(data_loader):
            data_time.update(time.time() - end_time)

            if not self.no_cuda:
                model = model.cuda()
                inputs = inputs.cuda()
                targets = targets.cuda()

            # inputs = Variable(inputs)
            # targets = Variable(targets)
            outputs = model(inputs)

            loss = criterion(outputs, targets)
            acc = calculate_accuracy(outputs, targets)

            losses.update(loss.data, inputs.size(0))
            accuracies.update(acc, inputs.size(0))

            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

            batch_time.update(time.time() - end_time)
            end_time = time.time()

            batch_logger.log({
                'epoch': epoch,
                'batch': i + 1,
                'iter': (epoch - 1) * len(data_loader) + (i + 1),
                'loss': losses.val,
                'acc': accuracies.val,
                'lr': optimizer.param_groups[0]['lr']
            })

            self.vislog_batch(i, losses.val)

            print('Epoch: [{0}][{1}/{2}]\t'
                  'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
                  'Data {data_time.val:.3f} ({data_time.avg:.3f})\t'
                  'Loss {loss.val:.4f} ({loss.avg:.4f})\t'
                  'Acc {acc.val:.3f} ({acc.avg:.3f})'.format(
                   epoch,
                   i + 1,
                   len(data_loader),
                   batch_time=batch_time,
                   data_time=data_time,
                   loss=losses,
                   acc=accuracies))

        epoch_logger.log({
            'epoch': epoch,
            'loss': losses.avg,
            'acc': accuracies.avg,
            'lr': optimizer.param_groups[0]['lr']
        })

        if epoch % self.checkpoint == 0:
            save_file_path = os.path.join(self.result_path, self.model_name+'save_{}.pth'.format(epoch))
            states = {
                'epoch': epoch + 1,
                'state_dict': model.state_dict(),
                'optimizer': optimizer.state_dict(),
            }
            torch.save(states, save_file_path)

    def validation(self, val_data, model, criterion, val_logger):
        val_loader = torch.utils.data.DataLoader(
            val_data,
            batch_size=self.batch_size,
            shuffle=False,
            # num_workers=self.n_threads,
            pin_memory=True)

        model.eval()
        losses = AverageMeter()
        accuracies = AverageMeter()

        end_time = time.time()
        for i, (inputs, targets) in enumerate(val_loader):

            if not self.no_cuda:
                inputs = inputs.cuda()
                targets = targets.cuda()

            outputs = model(inputs)

            loss = criterion(outputs, targets)
            acc = calculate_accuracy(outputs, targets)

            losses.update(loss.data, inputs.size(0))
            accuracies.update(acc, inputs.size(0))

        test_time = time.time() - end_time

        val_logger.log({'time': test_time,
                        'loss': losses.avg,
                        'acc': accuracies.avg})

        print('TestTime {test_time:.3f}\t'
              'Loss {loss.val:.4f} ({loss.avg:.4f})\t'
              'Acc {acc.val:.3f} ({acc.avg:.3f})'.format(
               test_time=test_time,
               loss=losses,
               acc=accuracies))

    def vislog_batch(self, batch_idx,loss):
        x_value = batch_idx
        y_value = loss
        self.vis.line([y_value], [x_value],
                      name='train_loss',
                      win='losses',
                      update='append')
        self.vis.line([2], [x_value],
                      name='test_loss',
                      win='losses',
                      update='append')
        self.vis.update_window_opts(win='losses', opts=self.vis_loss_opts)

def weights_init(m):
    # weight initialization
    if isinstance(m, nn.Conv2d):
        nn.init.kaiming_normal_(m.weight, mode='fan_out')
        # nn.init.xavier_normal_(m.weight)
        if m.bias is not None:
            nn.init.zeros_(m.bias)
    elif isinstance(m, nn.BatchNorm2d):
        nn.init.ones_(m.weight)
        nn.init.zeros_(m.bias)
    elif isinstance(m, nn.Linear):
        nn.init.normal_(m.weight, 0, 0.01)
        if m.bias is not None:
            nn.init.zeros_(m.bias)


T = int(input())
for t in range(1, T+1):
    n, m = map(int, input().split())
    li = list(map(int, input().split()))

    rli = []
    for i in li:
        if i%4 == 0 or i%6 == 0 or i%7 == 0 or i%9 == 0 or i%11 == 0:  # 보석의 배수에 하나라도 해당되면
            rli.append(i)       # 새로운 리스트에 추가

    result = []
    for i in range(1 << len(rli)):  # 0땜에 공집합 포함
        subset = []
        for j in range(len(rli)):
            if i & (1 << j):
                subset.append(rli[j])
        if sum(subset) <= m:        # 예산 이하인 값만 저장
            result.append(sum(subset))
    result.sort()   # 정렬
    print(f'#{t} {result[-1]}')     #정렬후 예산으로 살수 있는 보석의 최고값 프린트

알고수업/부울경_2반_이미현/Algo2_부울경_2반_이미현.py


import tagNtokenize
import correlation_cf as cf
import time
from os.path import dirname, join
import pickle

current_dir = dirname(__file__)
file_path = join(current_dir, 'question_answers.pickle')
with open(file_path, 'rb') as f:
        question_answers= pickle.load(f)

current_milli_time = lambda: int(round(time.time() * 1000))

def getResponse(uIn, sub):
    global question_answers
    tagtime = current_milli_time()
    tagged = tagNtokenize.tagNtokenize(uIn, True)
    tagtime = current_milli_time() - tagtime

    max_u = 0.6
    str_ans = "Sorry I don't quite know how to answer that!"

    corrtime = current_milli_time()

    # If in subquestions
    if(sub =="1"):
        str_ans = "That's interesting"
        sub = ""
    elif(sub != ""):
            for quest in question_answers[sub]:
                tagged2 = tagNtokenize.tagNtokenize(quest)
                temp_u = cf.correlate(tagged,tagged2)
##                      print("u= " + str(temp_u))
                if(temp_u>max_u):
                        try:
                                str_ans = question_answers[sub][quest]
##                                      print(tagged2)
                                max_u=temp_u
                        except:
                                print("Error in Sub referencing")
                                sub = ""

    for quest in question_answers:
##                print("\n"+quest)
            tagged2 = tagNtokenize.tagNtokenize(quest)
            temp_u = cf.correlate(tagged,tagged2)
##                print("u= " + str(temp_u))
            if(temp_u>max_u):
                    max_u=temp_u
                    str_ans = question_answers[quest]['ans']
##                        print(tagged2)
                    sub = quest
    corrtime = current_milli_time() - corrtime

    if(max_u <= 0.6):
        for t in tagged:
            if "NN" in t[1]:
                namedEntity = tagNtokenize.NER([(t[0],t[1])])
                for n in namedEntity:
                    if("PERSON" in n):
                        str_ans = "Who is " + t[0] + "?"
                        sub = ""
                    elif("GPE" in n):
                        str_ans = "Where is " + t[0] + "?"
                        sub = "!"

    return [str_ans, sub]


from flask import Flask, make_response, jsonify, request
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.metrics.pairwise import cosine_similarity
import requests
import numpy as np
import pandas as pd
import json

app = Flask(__name__)

rows = []

@app.route('/getDB')
def emplace():
    prod_name = ''

    prod_name = request.args.get('name')

    res = requests.get('http://43.201.114.241:8080/userinfo/product/')

    rows = res.json()

    product_name = []
    ingredients_list = []
    calories = []
    price = []
    amount = []
    carbohydrate = []
    cholesterol = []
    company = []
    cooking_type = []
    fat = []
    id = []
    primary_type = []
    product_category = []
    product_image = []
    protein = []
    sat_fat = []
    secondary_type = []
    serving_size = []
    sodium = []
    specific = []
    sugar = []
    trans_fat = []
    vegan_option = []

    for i in range(len(rows)):
        product_name.append(rows[i]["product_name"])
        ingredients_list.append(rows[i]["ingredient"])
        calories.append(rows[i]["calory"])
        price.append(rows[i]["price"])
        amount.append(rows[i]["amount"])
        carbohydrate.append(rows[i]["carbohydrate"])
        cholesterol.append(rows[i]["cholesterol"])
        vegan_option.append(rows[i]["vegan_option"])
        trans_fat.append(rows[i]["trans_fat"])
        sugar.append(rows[i]["sugar"])
        company.append(rows[i]["company"])
        cooking_type.append(rows[i]["cooking_type"])
        fat.append(rows[i]["fat"])
        id.append(rows[i]["id"])
        primary_type.append(rows[i]["primary_type"])
        product_category.append(rows[i]["product_category"])
        product_image.append(rows[i]["product_image"])
        protein.append(rows[i]["protein"])
        sat_fat.append(rows[i]["sat_fat"])
        secondary_type.append(rows[i]["secondary_type"])
        serving_size.append(rows[i]["serving_size"])
        sodium.append(rows[i]["sodium"])
        specific.append(rows[i]["specific"])

    data = pd.DataFrame({"product_name": product_name,
                        "ingredients_list": ingredients_list,
                         "calories": calories,
                         "price": price,
                         "amount": amount,
                         "carbohydrate": carbohydrate,
                         "cholesterol": cholesterol,
                         "company": company,
                         "cooking_type": cooking_type,
                         "fat": fat,
                         "id": id,
                         "primary_type": primary_type,
                         "product_category": product_category,
                         "product_image": product_image,
                         "protein": protein,
                         "sat_fat": sat_fat,
                         "secondary_type": secondary_type,
                         "serving_size": serving_size,
                         "sodium": sodium,
                         "specific": specific,
                         "sugar": sugar,
                         "trans_fat": trans_fat,
                         "vegan_option": vegan_option})

    cnt_vector = CountVectorizer(ngram_range=(1, 3))
    vector_categ = cnt_vector.fit_transform(data['ingredients_list'])
    categ_sim = cosine_similarity(
        vector_categ, vector_categ).argsort()[:, ::-1]

    target_idx = data[data['product_name'] == prod_name].index.values
    target_idx = target_idx[:1]
    sim_idx = categ_sim[target_idx, :].reshape(-1)
    sim_idx = sim_idx[sim_idx != target_idx]

    result = data.iloc[sim_idx].sort_values('price', ascending=False)
    data = data.iloc[target_idx[0]]
    result = result[(result['price'] > (data.price*0.9)) &
                    (result['price'] < (data.price*1.1))]
    result = result[(result['calories'] > (data.calories*0.9)) &
                    (result['calories'] < (data.calories*1.1))]

    result = result.to_json(orient='records', force_ascii=False)

    result = json.loads(result)

    return make_response(jsonify(result), 200)

if __name__ == '__main__':
    app.run(host='0.0.0.0')


# https://www.hackerrank.com/challenges/recursive-digit-sum/problem

#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'superDigit' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
#  1. STRING n
#  2. INTEGER k
#

def getAns(num):
    if num <= 9:
        return num

    sum = 0
    for x in str(num):
        sum = sum+int(x)
    return getAns(sum)

def superDigit(n, k):
    # Write your code here
    sum = 0
    for x in str(n):
        sum = sum+int(x)

    q = int(sum*k)
    print(q)
    ans = getAns(q)
    return ans

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    first_multiple_input = input().rstrip().split()

    n = first_multiple_input[0]

    k = int(first_multiple_input[1])

    result = superDigit(n, k)

    fptr.write(str(result) + '\n')

    fptr.close()


# A full binary tree is a binary tree where each node has exactly 0 or 2 children.

# Return a list of all possible full binary trees with N nodes.  Each element of the answer is the root node of one possible tree.

# Each node of each tree in the answer must have node.val = 0.

# You may return the final list of trees in any order.

# Example 1:

# Input: 7
# Output: [[0,0,0,null,null,0,0,null,null,0,0],[0,0,0,null,null,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,null,null,null,null,0,0],[0,0,0,0,0,null,null,0,0]]
# Explanation:

# Note:

# 1 <= N <= 20

class Solution:
    def allPossibleFBT(self, N: 'int') -> 'List[TreeNode]':
        if N % 2 == 0:
            return []
        return self.helper(N)

    def helper(self,n):
        root = TreeNode(0)
        remain = n-1
        if remain==0:
            return [root]
        ans = []
        for left in range(1,remain,2):
            left_subtrees = self.helper(left)
            right_subtrees = self.helper(remain-left)
            for left in left_subtrees:
                for right in right_subtrees:
                    root.left = left
                    root.right = right
                    ans.append(self.get_copy(root))
        return ans

    def get_copy(self,root):
        tmproot = TreeNode(0)
        def helper(node1,node2):
            if node2.left:
                tmpleft = TreeNode(0)
                node1.left = tmpleft
                helper(tmpleft,node2.left)
            if node2.right:
                tmpright = TreeNode(0)
                node1.right = tmpright
                helper(tmpright,node2.right)
        helper(tmproot,root)
        return tmproot

class Solution:
    def allPossibleFBT(self, N: 'int') -> 'List[TreeNode]':
        if N % 2 == 0:
            return []
        self.cache = {}
        return self.helper(N)

    def helper(self,n):
        if n in self.cache:
            return self.cache[n]
        remain = n-1
        if remain==0:
            self.cache[n] = [TreeNode(0)]
            return self.cache[n]
        ans = []
        for left in range(1,remain,2):
            left_subtrees = self.helper(left)
            right_subtrees = self.helper(remain-left)
            for left in left_subtrees:
                for right in right_subtrees:
                    tmp = TreeNode(0)
                    tmp.left = left
                    tmp.right = right
                    ans.append(tmp)
        self.cache[n] = ans
        return ans


# Cemantix game solver
import logging
import os
import yaml

from src import *

os.chdir(os.path.abspath(os.path.dirname(__file__)))
with open("config.yaml", "r") as config_file:
    config = yaml.load(config_file, Loader=yaml.FullLoader)
logging.basicConfig(filename=f"./logs/cemantix_{dt.datetime.now().strftime(format='%Y-%m-%d_%Hh%M')}.log", level=logging.INFO)

def main():
    model = load_model(embedding=config['word2vec'])
    game = CemantixGame(executable_path="./artifacts/chromedriver.exe",game_url=config["game_url"])
    if config["agent_type"] == "bandit":
        agent = CemantixBandit(model=model, **config["agent_params"])
    elif config["agent_type"] == "gangster":
        agent = CemantixGangster(model=model, **config["agent_params"])
    else:
        raise ValueError("Unknown agent_type")
    game.play_strategy(agent, max_iter=config["max_iter"])
    game.save_result()
    game.end()

main()


import torch
from torch import nn
from .strategy import Strategy
from .utils import ner_predict, re_predict

class EntropySampling(Strategy):
    def __init__(self, annotator_config_name, pool_size, setting: str='knn', engine: str='gpt-35-turbo-0301',
                 reduction: str='mean'):
        super().__init__(annotator_config_name, pool_size, setting, engine)
        assert reduction in ['mean', 'sum', 'max']
        self.reduction = reduction

    def query(self, args, k: int, model: nn.Module, features):
        pool_indices = self._get_pool_indices()
        pool_features = [features[i] for i in pool_indices]
        if self.task_type == 'ner':
            pred_logits = ner_predict(args, pool_features, model)
            uncertainties = []
            for logit in pred_logits:
                prob = torch.softmax(logit, dim=-1)
                entropy = torch.special.entr(prob).sum(dim=-1) # entropy over each token
                if self.reduction == 'mean':
                    uncertainties.append(entropy.mean())
                elif self.reduction == 'sum':
                    uncertainties.append(entropy.sum())
                elif self.reduction == 'max':
                    uncertainties.append(entropy.max())
            uncertainties = torch.stack(uncertainties)
        elif self.task_type == 're':
            pred_logits = re_predict(args, pool_features, model)
            prob = torch.softmax(pred_logits, dim=-1)
            entropy = torch.special.entr(prob).sum(dim=-1)
            uncertainties = entropy
        else:
            raise ValueError('tbd.')
        lab_indices = torch.topk(uncertainties, k=k)[1]
        lab_indices = [pool_indices[i] for i in lab_indices]
        return lab_indices


""" Run Length Encoding """

def main():
    """ print changed password """
    text = input()
    collector = []
    each_al = ""
    result = ""
    count = ""
    if len(text) > 1:
        for jay in range(len(text)-1):
            if text[jay] == text[jay+1]:
                each_al += text[jay]
            else:
                each_al += text[jay]
                collector.append(each_al)
                each_al = ""
            count = str(jay)
        # บวกชุดสุดท้ายเข้าไป
        jay = int(count)
        each_al += text[jay+1]
        collector.append(each_al)
        for k in range(len(collector)):
            result += str(len(collector[k]))
            result += str(collector[k][0])
    else:
        result += "1%s"%text
    print(result)
main()


# Um professor quer sortear um dos seus quatro alunos para apagar o quadro.
# Faça um programa que ajude ele, lendo o nome dos alunos e escrevendo na tela o nome do escolhido.
import random

print('='*23, '\n QUEM APAGARÁ A LOUSA❓')
print('='*23)

n1 = input('Primeiro nome: ')
n2 = input('Segundo nome: ')
n3 = input('Terceiro nome: ')
n4 = input('Quarto nome: ')

l = [n1, n2, n3, n4]

a = random.choice(l)
print('Pois eh...\nFoi tu:\n¯\_(ツ)_/¯ {} ¯\_(ツ)_/¯'.format(a))

Só exercícios/ex019-Sortei Apagar lousa.py


# -*- coding: utf-8 -*-
# @Project : CrawlersTools
# @Time    : 2022/6/21 17:06
# @Author  : MuggleK
# @File    : proxy.py

import httpx

from loguru import logger

def get_proxies(proxy_url=None, http2=False):
    """
    默认httpx代理模式
    @param proxy_url: 代理请求链接
    @param http2: 默认http1.1规则
    @return:
    """
    if not proxy_url: return

    protocol = 'http://'
    try:
        proxy = httpx.get(proxy_url).text.strip()
        proxy = protocol + proxy
        if http2:
            return {protocol: proxy, 'https://': proxy}
        return {"http": proxy, "https": proxy}
    except Exception as err:
        logger.error(f'获取代理失败：{err}')


import sqlite3
import os
import bamnostic_mod as bn
import argparse
import bisect
import time

#Downloads\Lung\47b982b3-c7ce-4ca7-8c86-c71c15979620\G28588.NCI-H1915.1.bam
#Downloads\Lung\98a0206b-29f5-42d3-957b-6480e2fde185\G20483.HCC-15.2.bam
#Downloads\Lung\18004fb1-89a2-4ba1-a321-a0aa854e98c3\G25210.NCI-H510.1.bam
#Downloads\Lung\47030e40-acbd-4905-939c-d57441d7042e\G25222.NCI-H2171.1.bam
#Downloads\Lung\1357785f-f84b-4688-9b4c-0c2b5472ef51\G27281.RERF-LC-MS.1.bam
#Downloads\Lung\e48ea2ee-1dda-4061-a199-6e22fd2df382\G25212.NCI-H661.1.bam
#Downloads\Lung\f03dbfee-a523-438f-8459-f47f2ff1880f\G25224.NCI-H2066.1.bam

#Downloads\HeadAndNeck\0e67231f-97be-447c-b3b0-a656fc30a62d\G27454.PE_CA-PJ15.2.bam
#Downloads\HeadAndNeck\1acf65a0-0268-4288-9904-33bff618a31d\G27515.PE_CA-PJ41__clone_D2_.2.bam
#Downloads\HeadAndNeck\1f290458-df28-4c78-b73d-0202fb53bb0e\G27220.SCC-4.1.bam
#Downloads\HeadAndNeck\2b507086-977b-4cb7-abd9-83ee4ce9a893\G27489.PE_CA-PJ34__clone_C12_.2.bam
#Downloads\HeadAndNeck\7ed3e895-6826-430d-a39d-338111f16083\G27512.SNU-1214.2.bam
#Downloads\HeadAndNeck\c11aa745-72ea-44ca-b70d-7811c2f244b7\G27533.SNU-1066.2.bam
#Downloads\HeadAndNeck\dc8393c0-7d9e-4040-a91a-5783544cac35\G28853.HSC-4.3.bam

parser = argparse.ArgumentParser(description='takes the given .bam file and looks through all the reads to construct a count of all exons and splices in the reference splice graphs', usage='splicerSampleProcessor database_directory bam_file sample_name novelSplicesToggle(True|False)')
parser.add_argument("Database", help='The path to where you want to store the database file.')
parser.add_argument("Bam", help='The .bam file to count the reads from')
parser.add_argument("sampleName", help='Name for current sample in the sample table')
parser.add_argument("novelSplices", choices=['True', 'False'], help='Controls whether the program tries to find new splices')
args = parser.parse_args()

os.chdir("D:\\")
#get connection to the sqlite database
conn = sqlite3.connect(args.Database + os.path.sep + 'splice.sqlite', isolation_level=None)
c = conn.cursor()

c.execute('''CREATE TABLE IF NOT EXISTS Sample
             (Sample_ID INTEGER PRIMARY KEY NOT NULL DEFAULT NULL,
              Sample_Name varchar(40) NOT NULL DEFAULT NULL,
              Total_Reads INTEGER NOT NULL DEFAULT NULL,
              Transcript_Reads INTEGER NOT NULL DEFAULT NULL);''')
c.execute("CREATE INDEX IF NOT EXISTS idx_sample_name ON Sample(sample_name);")

c.execute('''CREATE TABLE IF NOT EXISTS Sample_Attribs
             (Sample_ID INTEGER NOT NULL DEFAULT NULL,
              Attribute varchar(255) NOT NULL DEFAULT NULL,
              Value varchar(255) NOT NULL DEFAULT NULL);''')

c.execute('''CREATE TABLE IF NOT EXISTS Exon_Counts
             (Sample_ID INTEGER  NOT NULL DEFAULT NULL,
              SG_Exon_ID INTEGER NOT NULL DEFAULT NULL,
              Count INTEGER NOT NULL DEFAULT NULL);''')
c.execute("CREATE INDEX IF NOT EXISTS idx_ec_sample_id ON Exon_Counts(sample_id);")
c.execute("CREATE INDEX IF NOT EXISTS idx_ec_exon_id ON Exon_Counts(SG_Exon_ID);")

c.execute('''CREATE TABLE IF NOT EXISTS Splice_Counts
             (Sample_ID INTEGER  NOT NULL DEFAULT NULL,
              SG_Splice_ID INTEGER NOT NULL DEFAULT NULL,
              Count INTEGER NOT NULL DEFAULT NULL);''')
c.execute("CREATE INDEX IF NOT EXISTS idx_sc_sample_id ON Splice_Counts(sample_id);")
c.execute("CREATE INDEX IF NOT EXISTS idx_sc_splice_id ON Splice_Counts(SG_Splice_ID);")

#Find out the next assignable ID for this sample
c.execute("Select MAX(Sample_ID) FROM Sample")
ret = c.fetchone()
prevId = ret[0]
if prevId:
    Sample_Id = int(prevId)+1
else:
    Sample_Id = 1

novelSplices = args.novelSplices

#initialize the splice dictionary
sDict = {}
eDict = {}
epDict = {}
ecDict = {}
scDict = {}
discoverySplices = {}

start_time = time.time()

c.execute("SELECT SG_Splice_ID, Start_Position, Stop_Position, Chromosome FROM SG_Splice")
ret = c.fetchall()
#load the splice dictionary keyed on start-stop with the sg id as the value
for y in range(len(ret)):
    key = str(ret[y][1])+'-'+str(ret[y][2])
    chrom = ret[y][3]
    if not chrom.startswith("chr"):
        chrom = "chr"+chrom
    if chrom == "chrMT":
        chrom = "chrM"
    if chrom not in sDict:
        sDict[chrom] = {}
        if novelSplices == 'True':
            discoverySplices[chrom] = {}
    sDict[chrom][key] = ret[y][0]

c.execute("SELECT SG_Exon_ID, Start_Position, Stop_Position, Chromosome FROM SG_Exon")
ret = c.fetchall()
#load the exon dictionary keyed on the start and the stop with the sg id as the value
for y in range(len(ret)):
    chrom = ret[y][3]
    if not chrom.startswith("chr"):
        chrom = "chr"+chrom
    if chrom == "chrMT":
        chrom = "chrM"
    if chrom not in eDict:
        eDict[chrom] = {}
        epDict[chrom] = []

    #add start
    eDict[chrom][ret[y][1]] = ret[y][0]
    #add stop
    eDict[chrom][ret[y][2]] = ret[y][0]
    #add to tuple exon positions list (flip start and stop to correct if negative strand)
    if ret[y][1] < ret[y][2]:
        epDict[chrom].append((ret[y][1], ret[y][2]))
    else:
        epDict[chrom].append((ret[y][2], ret[y][1]))

#sorted list of all exon start stop tuples keyed on chromosome
for key in epDict:
    epDict[key] = sorted(epDict[key])

#"hg19test.bam"
samfile = bn.AlignmentFile(args.Bam, "rb")

def exonIncrement(start, stop, chro):
    exonIds = []
    try:
        pList = epDict[chro]
        #flip start in stop to correct for negative strand
        if start > stop:
            temp = start
            start = stop
            stop = temp
        #find the index that start belongs at
        idx = bisect.bisect(pList, (start,stop))

        i = idx
        if i == len(pList):
            return([])
        #move down the exons adding the ids of those included in the read
        while (i > -1  and start <= pList[i][1]):
            exonIds.append(eDict[chro][pList[i][0]])
            i-=1
        #ISSUE IF NEVER LOOP****************
        #if it goes off the end of a known exon add none and scrap the read
        if start < pList[i+1][0]:
            return([])

        i = idx
        looped = False
        #move up the exons adding ids of those included in the read
        while (i < len(pList) and i > -1 and stop >= pList[i][0]):
            exonIds.append(eDict[chro][pList[i][1]])
            i+=1
            looped = True
        #if it goes of the end of a known exon add none and scrap the read
        if looped and stop > pList[i-1][1]:
            return([])
    except Exception:
        pass

    return(exonIds)

fns = open ('novelSplices.txt', 'w')
i = 0

totalCount = 0
missingAttrCount = 0
tranCount = 0
totalDupeCount = 0
tranDupeCount = 0
totalJRcount = 0
tranJRcount = 0
totalNJRcount = 0
tranNJRcount = 0
exceptionCount = 0
prevRead = ""
prevExons = ""
prevSplices = ""
for read in samfile:
    #read does not have an alignment
    if (
        not hasattr(read, 'reference_name')  or read.reference_name == None  or
        not hasattr(read, 'reference_start') or read.reference_start == None or
        not hasattr(read, 'reference_end')   or read.reference_end == None   or
        not hasattr(read, 'cigarstring')     or read.cigarstring == None     or
        not hasattr(read, 'cigar')           or read.cigar == None
       ):
        missingAttrCount += 1
        continue

    dupeTag = False
    exonSet = set()
    spliceSet = set()
    readR_S = read.reference_start
    readR_E = read.reference_end
    i+=1
    totalCount += 1
    if totalCount % 1000000 == 0:
        print(f"{totalCount:,d}")
        break
    tranBool = False
    cigarString = read.cigarstring
    cigar = read.cigar
    chro = read.reference_name
    if not chro.startswith("chr"):
        chro = "chr"+chro
    if str(readR_S)+"-"+str(readR_E)+"-"+cigarString+"-"+chro == prevRead:
        dupeTag = True
        totalDupeCount += 1
        for exon in prevExons:
            tranBool = True
            ecDict[exon] += 1
        for splice in prevSplices:
            tranBool = True
            scDict[splice] += 1
        if tranBool == True:
            tranDupeCount += 1
    elif "N" in cigarString:
        totalJRcount += 1
        #initialize the start and stop based on the first junction
        start = readR_S+cigar[0][1]
        stop = start+cigar[1][1]+1
        #exon check from the start of the read to the start of the first splice
        exonSet.update(exonIncrement(readR_S+1, start, chro))
        for x in range(int(len(cigar)/2)):
            #if this is not the first junction adjust the start and stop
            if x != 0:
                start = stop+cigar[x*2][1]-1
                #exon check from the end of the last splice to the beginning of this splice
                exonSet.update(exonIncrement(stop, start, chro))
                stop = start+cigar[x*2+1][1]+1

            #check if the splice is known and count it if so
            try:
                if str(start)+"-"+str(stop) in sDict[chro] or str(stop)+"-"+str(start) in sDict[chro]:
                    spliceID = sDict[chro][str(start)+"-"+str(stop)]
                    spliceSet.add(spliceID)
                    tranBool = True
                    if spliceID in scDict:
                        scDict[spliceID] += 1
                    else:
                        scDict[spliceID] = 1
                elif novelSplices == 'True':
                    if start in eDict[chro] and stop in eDict[chro]:
                        if str(start)+"-"+str(stop) in discoverySplices[chro]:
                            discoverySplices[chro][str(start)+"-"+str(stop)]+=1
                        else:
                            discoverySplices[chro][str(start)+"-"+str(stop)]=1

                        experiSplicect = 1
            except Exception as e:
                exceptionCount += 1
            exonID = ""

        exonSet.update(exonIncrement(stop, readR_E, chro))
        if (tranBool or len(exonSet) != 0):
            tranJRcount += 1
    else:
        totalNJRcount += 1
        start = readR_S+1
        stop = start+cigar[0][1]
        exonSet.update(exonIncrement(start, stop, chro))
        if (len(exonSet) != 0):
            tranNJRcount += 1
        else:
            print("Missing: " + chro + ' ' + str(start) + ' ' + str(stop))

    #add in all the sets
    for exon in exonSet:
        tranBool = True
        #print("exon: "+str(exon))
        if exon in ecDict:
            ecDict[exon] += 1
        else:
            ecDict[exon] = 1

    if tranBool == True:
        tranCount += 1

    #set this line to prevRead
    if dupeTag == False:
        prevRead = str(readR_S)+"-"+str(readR_E)+"-"+cigarString+"-"+chro
        prevExons = exonSet
        prevSplices = spliceSet

    #if i == 5000000:
     #   break

c.execute('begin')
for key in scDict:
    c.execute("INSERT INTO Splice_Counts VALUES("+str(Sample_Id)+", "+str(key)+", "+str(scDict[key])+")")

for key in ecDict:
    c.execute("INSERT INTO Exon_Counts VALUES("+str(Sample_Id)+", "+str(key)+", "+str(ecDict[key])+")")

#add this sample to the sample table
c.execute("INSERT INTO Sample VALUES("+str(Sample_Id)+", "+args.sampleName+", "+str(totalCount)+", "+str(tranCount)+")")
c.execute('commit')
for chromkey in discoverySplices:
    for skey in discoverySplices[chromkey]:
        fns.write(skey + ", Count: " + str(discoverySplices[chromkey][skey])+'\n')
fns.close()
print("missing attribute reads: " + str(missingAttrCount))
print("transcript junction reads: "+str(tranJRcount))
print("total junction reads: "+str(totalJRcount))
print("transcript duplicate reads: "+str(tranDupeCount))
print("total duplicate reads: "+str(totalDupeCount))
print("transcript Non junction reads: "+str(tranNJRcount))
print("total Non junction reads: "+str(totalNJRcount))

print("number of exceptions caught: "+ str(exceptionCount))

print("--- %.2f seconds---" % (time.time() - start_time))
print('Done')


from replit import db
from util import str_to_arr
from person import Person

def matches_to_string(matches):
	string = "List of matches:\n"
	for match in matches:
		string += match + "\n"
	return string

async def make_connections(message, person_calling):
	matches = []

	person_1 = Person.str_to_person(db[f"{person_calling}"])
	person_1_interests = str_to_arr(person_1.interests)
	person_1_courses = str_to_arr(person_1.courses)

	for person in db.keys():
		similarities = 0

		if str(person) == str(person_calling):
			continue

		person_2 = Person.str_to_person(db[f"{person}"])
		person_2_interests = str_to_arr(person_2.interests)
		person_2_courses = str_to_arr(person_2.courses)

		if person_1.year == person_2.year:
			similarities += 1

		for interest in person_1_interests:
			if interest in person_2_interests:
				similarities += 1

		for course in person_1_courses:
			if course in person_2_courses:
				similarities += 1

		if int(similarities/2) >= person_1.fit:
			matches.append(person_2.user)

	await message.author.send(matches_to_string(matches))
	return


import os
from bot.misc.util import download, calculate_hash
from bot.functions import lessonsToday
from bot.database.main import filesDB
import hashlib
import datetime

files = filesDB()

def filesCheck(urls) -> list:
    done = []
    filesHash = []
    for name, url in urls.items():
        h = files.get(name)
        if h == -1:
            download(url, name)
            rez = calculate_hash(f'{name}')
            filesHash.append({'name': name, 'hash': rez, 'date': datetime.datetime.now()})
        else:
            done.append({'file_id': h['file_id'],
                         'url': url})
    return done, filesHash


import cv2
import numpy as np

video_path = '/Users/bigphess/Desktop/omnidirection/res/rabbit_250fps.mp4'
cap = cv2.VideoCapture(0)
cap2 = cv2.VideoCapture(video_path)

while True:
	ret, frame = cap2.read()
	# image = cv2.imread('/Users/bigphess/Downloads/IMG_6453.JPG')

	debug = frame
	if cv2.waitKey(100) & 0xFF == ord('q'):
		cv2.imwrite('/Users/bigphess/Desktop/xu.jpg',debug)
		print("successful write")
		break

	# h,w,ch = debug.shape

	print('the size of the frame is {} and {}'.format(debug.shape[1],debug.shape[0]))
	cv2.imshow('??', frame)
	cv2.waitKey(0)
	break

	# debug = cv2.logPolar(frame,(debug.shape[0]/2,debug.shape[1]/2),100,cv2.WARP_FILL_OUTLIERS)

	# cv2.waitKey(1)


import re
import sys

import numpy as np
import pandas as pd
from sklearn.base import TransformerMixin
from sklearn.compose import ColumnTransformer
from sklearn.feature_extraction.text import HashingVectorizer
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import OneHotEncoder

class PreprocessDataframe(TransformerMixin):
    def fit(self, df, _):
        # Drop random/singular columns
        # self.mask = [bool(pd.Series(col).dropna().size) for col, s in X.T]
        self.drop_cols = df.columns[df.isna().all(axis=0)]
        print('Drop cols', self.drop_cols, file=sys.stderr)
        return self

    def transform(self, df: pd.DataFrame):
        assert (df.iloc[:, 0].str.count(' ') == 2).all()
        df = df.drop(columns=df.columns[0])  # process pid, container id/image

        return df.drop(columns=self.drop_cols)

class PreprocessText(TransformerMixin):
    fit = lambda x, *_: x

    def _fix(self, s):
        s = re.sub(r'(?<![.:\d])[a-f\d\[\]]+(:+[a-f\d\[\]]+)+|\d+([.:-]+\d+){2,4}', ' ', s)  # IP:port
        return s

    assert not _fix(None, '''
        123.123.123.123:1234
        123:[fab:12e:123:123]:1234
    ''').strip()

    def transform(self, X):
        Xt = [self._fix(s) for s in X]
        return Xt

class Transformer(TransformerMixin):
    CATEGORICAL_WEIGHT = 30

    def fit(self, df, _):
        transformers = []
        for i, (_, s) in enumerate(df.items()):
            n_words = s.fillna('').astype(str).str.count(r'\b\w+')
            assert n_words.mean() > 0
            if n_words.mean() <= 1:
                print(i, n_words.mean(), np.unique(n_words), sep='\t')
                transformers.append((
                    str(i),
                    OneHotEncoder(handle_unknown='ignore', dtype=np.int8),
                    [i] * self.CATEGORICAL_WEIGHT,
                ))
            else:
                n_features = round(max(5, min(50, n_words.median() * 5)))
                print(i, f'{n_words.mean():.2f}', n_features, sep='\t')
                transformers.append((
                    str(i),
                    make_pipeline(
                        PreprocessText(),
                        HashingVectorizer(
                            n_features=n_features,
                            token_pattern=r'[>\w-]+',
                            ngram_range=(1, 2),
                            lowercase=False,
                            alternate_sign=False,
                            norm=None,
                            dtype=np.uint8,
                        ),
                    ),
                    i
                ))
        self.transformer = ColumnTransformer(
            transformers=transformers,
            # n_jobs=-1,
        )
        X = df.fillna('').astype(str).values
        self.transformer.fit(X)
        return self

    def transform(self, df):
        X = df.fillna('').astype(str).values
        Xt = self.transformer.transform(X)
        assert Xt.size, Xt
        return Xt


"""
Given an array of strings, return another array containing all of its longest strings.

Example

For inputArray = ["aba", "aa", "ad", "vcd", "aba"], the output should be
solution(inputArray) = ["aba", "vcd", "aba"].

Input/Output

    [execution time limit] 4 seconds (py3)

    [input] array.string inputArray

    A non-empty array.

    Guaranteed constraints:
    1 ≤ inputArray.length ≤ 10,
    1 ≤ inputArray[i].length ≤ 10.

    [output] array.string

    Array of the longest strings, stored in the same order as in the inputArray.

"""

def solution(inputArray):
    longest = []
    length = 0
    for i in range (len(inputArray)):
        if len(inputArray[i]) > length:
            length = len(inputArray[i])

    for x in range(len(inputArray)):
        if len(inputArray[x]) == length:
            longest.append(inputArray[x])

    print(longest)
    return longest


#!/usr/bin/python3 -u
import sys, re, math
from img2c import *

def ascii2c(lines, img, dest = sys.stdout, h = None, w = None):
   width = max([len(line) for line in lines])
   height = len(lines)

   image = [[0 for x in range(width)] for y in range(height)]

   y = 0
   for line in lines:
      x = 0
      for c in line:
         if c != ' ':
            image[y][x] = 1
         else:
            image[y][x] = 0
         x += 1
      y += 1

   printComment(image, height, width, dest)
   printArray(image, img, height, width, dest, h, w)

   return width, height

def main():
   letter = False
   if "--letter" in sys.argv:
      letter = True
      sys.argv.remove("--letter")

   if len(sys.argv) < 2:
      print("Usage: ascii2c.py <source> [destination]")
      sys.exit()

   source = sys.argv[1]
   try:
      destination = sys.argv[2]
      img = re.sub(r"^.*[/\\]", "", destination)
      img = re.sub(r"\..*$", "", img)
   except:
      destination = sys.stdout
      img = re.sub(r"^.*[/\\]", "", source)
      img = re.sub(r"\..*$", "", img)

   try:
      lines = [x.strip('\n') for x in open(source, "r").readlines()]
   except:
      print("Error: Cannot open file: %s" % source)
      sys.exit()

   if letter:
      lines = lines[1:]

   if destination != sys.stdout:
      try:
         dest = open(destination, "w")
      except:
         print("Error: Cannot open file: %s" % destination)
         sys.exit()
   else:
      dest = destination

   print("#include <pm_graphics.h>", file=dest)
   print(file=dest)

   w, h = ascii2c(lines, '_' + img, dest)
   print(file=dest)
   print("const pm_image %s = {" % img, file=dest)
   print("\t_%s, %d, %d" % (img, w, h), file=dest)
   print("};", file=dest)

   dest.close()

if __name__ == "__main__":
   main()