# -*- coding: utf-8 -*-
"""
.. codeauthor:: Jaume Bonet <jaume.bonet@gmail.com>

.. affiliation::
    Laboratory of Protein Design and Immunoengineering <lpdi.epfl.ch>
    Bruno Correia <bruno.correia@epfl.ch>

.. func:: get_id
.. func:: get_available_sequences
.. func:: get_sequence
.. func:: get_available_structures
.. func:: get_structure
.. func:: get_available_structure_predictions
.. func:: get_structure_prediction
.. func:: get_sequential_data
.. func:: get_dihedrals
.. func:: get_phi
.. func:: get_psi
.. func:: get_available_labels
.. func:: get_label
"""
# Standard Libraries

# External Libraries
import six
import pandas as pd
import numpy as np

# This Library

__all__ = ['get_id', 'get_available_sequences', 'get_sequence',
           'get_available_structures', 'get_structure',
           'get_available_structure_predictions', 'get_structure_prediction',
           'get_dihedrals', 'get_phi', 'get_psi',
           'get_sequential_data', 'get_available_labels', 'get_label']

def _check_type( obj ):
    if not isinstance(obj, (pd.DataFrame, pd.Series)):
        raise TypeError("Data container has to be a DataFrame/Series or a derived class.")

def _check_column( obj, ctype, seqID ):
    if "{0}_{1}".format(ctype, seqID) not in obj:
        raise KeyError(
            "{0} for {1} not found in data set. ".format(ctype.capitalize(), seqID) +
            "Column `{0}_{1}` is missing.".format(ctype, seqID))
    return "{0}_{1}".format(ctype, seqID)

def _get_available( obj, ctype ):
    if isinstance(obj, pd.DataFrame):
        return ["_".join(x.split("_")[1:]) for x in obj.columns.values if x.startswith(ctype)]
    else:
        return ["_".join(x.split("_")[1:]) for x in obj.index.values if x.startswith(ctype)]

def _get_key_sequence( obj, ctype, seqID, key_residues ):
    from rstoolbox.components import get_selection
    from .reference import get_reference_shift

    seq = obj[_check_column(obj, ctype, seqID)]
    sft = get_reference_shift(obj, seqID)

    if isinstance(obj, pd.Series):
        length = len(seq)
    else:
        length = len(seq.iloc[0])
    kr = get_selection(key_residues, seqID, sft, length)

    if isinstance(obj, pd.Series):
        if len(kr) > 1:
            # -1 because we access string positions
            return "".join(np.array(list(seq))[kr - 1])
        else:
            return ""
    else:
        if len(kr) > 1:
            return seq.apply(lambda seq: "".join(np.array(list(seq))[kr - 1]))
        else:
            return seq.apply(lambda seq: "")

def get_id( self ):
    """Return identifier data for the design(s).

    :return: :class:`str` or :class:`~pandas.Series` - depending on the input

    :raises:
        :TypeError: |indf_error|
        :KeyError: If the column ``description`` cannot be found.

    .. rubric:: Example

    .. ipython::

        In [1]: from rstoolbox.io import parse_rosetta_file
           ...: import pandas as pd
           ...: pd.set_option('display.width', 1000)
           ...: pd.set_option('display.max_columns', 500)
           ...: df = parse_rosetta_file("../rstoolbox/tests/data/input_2seq.minisilent.gz")
           ...: df.get_id()
    """
    _check_type(self)
    if "description" not in self:
        raise KeyError("Identifiers not found in data set. Column `description` is missing.")
    return self["description"]

def get_available_sequences( self ):
    """List which **sequence** identifiers are available in the data container

    :return: :func:`list` of :class:`str`

    :raises:
        :TypeError: |indf_error|.

    .. rubric:: Example

    .. ipython::

        In [1]: from rstoolbox.io import parse_rosetta_file
           ...: import pandas as pd
           ...: pd.set_option('display.width', 1000)
           ...: pd.set_option('display.max_columns', 500)
           ...: df = parse_rosetta_file("../rstoolbox/tests/data/input_2seq.minisilent.gz",
           ...:                         {'sequence': 'AB'})
           ...: df.get_available_sequences()
    """
    _check_type(self)
    return _get_available(self, "sequence_")

def get_sequence( self, seqID, key_residues=None ):
    """Return the **sequence** data for ``seqID`` available in the container.

    :param str seqID: |seqID_param|.
    :param key_residues: |keyres_param|.
    :type key_residues: |keyres_types|

    :return: :class:`str` or :class:`~pandas.Series` - depending on the input

    :raises:
        :TypeError: |indf_error|.
        :KeyError: |seqID_error|.

    .. rubric:: Example

    .. ipython::

        In [1]: from rstoolbox.io import parse_rosetta_file
           ...: import pandas as pd
           ...: pd.set_option('display.width', 1000)
           ...: pd.set_option('display.max_columns', 500)
           ...: df = parse_rosetta_file("../rstoolbox/tests/data/input_2seq.minisilent.gz",
           ...:                         {'sequence': 'AB'})
           ...: df.get_sequence('B')
    """
    _check_type(self)
    if key_residues is None:
        return self[_check_column(self, "sequence", seqID)]
    else:
        return _get_key_sequence(self, "sequence", seqID, key_residues)

def get_phi( self, seqID, key_residues=None ):
    """Return the **phi** angle for ``seqID`` available in the container.

    :param str seqID: |seqID_param|.
    :param key_residues: |keyres_param|.
    :type key_residues: |keyres_types|

    :return: :class:`~numpy.ndarray` or :class:`~pandas.Series` - depending on the input

    :raises:
        :TypeError: |indf_error|.
        :KeyError: |seqID_error|.

    .. rubric:: Example

    .. ipython::

        In [1]: from rstoolbox.io import parse_rosetta_file
           ...: import pandas as pd
           ...: pd.set_option('display.width', 1000)
           ...: pd.set_option('display.max_columns', 500)
           ...: df = parse_rosetta_file("../rstoolbox/tests/data/input_3ssepred.minisilent.gz",
           ...:                         {'dihedrals': '*'})
           ...: df.get_phi('A')
    """
    _check_type(self)
    if key_residues is None:
        return self[_check_column(self, "phi", seqID)]
    else:
        return _get_key_sequence(self, "phi", seqID, key_residues)

def get_psi( self, seqID, key_residues=None ):
    """Return the **psi** angle for ``seqID`` available in the container.

    :param str seqID: |seqID_param|.
    :param key_residues: |keyres_param|.
    :type key_residues: |keyres_types|

    :return: :class:`~numpy.ndarray` or :class:`~pandas.Series` - depending on the input

    :raises:
        :TypeError: |indf_error|.
        :KeyError: |seqID_error|.

    .. rubric:: Example

    .. ipython::

        In [1]: from rstoolbox.io import parse_rosetta_file
           ...: import pandas as pd
           ...: pd.set_option('display.width', 1000)
           ...: pd.set_option('display.max_columns', 500)
           ...: df = parse_rosetta_file("../rstoolbox/tests/data/input_3ssepred.minisilent.gz",
           ...:                         {'dihedrals': '*'})
           ...: df.get_psi('A')
    """
    _check_type(self)
    if key_residues is None:
        return self[_check_column(self, "psi", seqID)]
    else:
        return _get_key_sequence(self, "psi", seqID, key_residues)

def get_dihedrals( self, seqID, key_residues=None ):
    """Return the **dihedrals** data for ``phi-psi`` available in the container.

    :param str seqID: |seqID_param|.
    :param key_residues: |keyres_param|.
    :type key_residues: |keyres_types|

    :return: :func:`list` of :class:`~numpy.ndarray` or :class:`~pandas.DataFrame`
        - depending on the input

    :raises:
        :TypeError: |indf_error|.
        :KeyError: |seqID_error|.

    .. seealso::
        :meth:`.DesignFrame.get_phi`
        :meth:`.DesignFrame.get_psi`
        :meth:`.DesignSeries.get_phi`
        :meth:`.DesignSeries.get_psi`

    .. rubric:: Example

    .. ipython::

        In [1]: from rstoolbox.io import parse_rosetta_file
           ...: import pandas as pd
           ...: pd.set_option('display.width', 1000)
           ...: pd.set_option('display.max_columns', 500)
           ...: df = parse_rosetta_file("../rstoolbox/tests/data/input_3ssepred.minisilent.gz",
           ...:                         {'dihedrals': '*'})
           ...: df.get_dihedrals('A')
    """
    _check_type(self)
    if isinstance(self, pd.DataFrame):
        if key_residues is None:
            return pd.concat([self[_check_column(self, "phi", seqID)],
                              self[_check_column(self, "psi", seqID)]], axis=1)
        else:
            return pd.concat([_get_key_sequence(self, "phi", seqID, key_residues),
                              _get_key_sequence(self, "psi", seqID, key_residues)], axis=1)
    else:
        if key_residues is None:
            return [self[_check_column(self, "phi", seqID)],
                    self[_check_column(self, "psi", seqID)]]
        else:
            return [_get_key_sequence(self, "phi", seqID, key_residues),
                    _get_key_sequence(self, "psi", seqID, key_residues)]

def get_available_structures( self ):
    """
    List which **structure** identifiers are available in the data container

    :return: :func:`list` of :class:`str`

    :raises:
        :TypeError: |indf_error|.

    .. rubric:: Example

    .. ipython::

        In [1]: from rstoolbox.io import parse_rosetta_file
           ...: import pandas as pd
           ...: pd.set_option('display.width', 1000)
           ...: pd.set_option('display.max_columns', 500)
           ...: df = parse_rosetta_file("../rstoolbox/tests/data/input_ssebig.minisilent.gz",
           ...:                         {'structure': 'C'})
           ...: df.get_available_structures()
    """
    _check_type(self)
    return _get_available(self, "structure_")

def get_structure( self, seqID, key_residues=None ):
    """Return the **structure** data for ``seqID`` available in the container.

    :param str seqID: |seqID_param|.
    :param key_residues: |keyres_param|.
    :type key_residues: |keyres_types|

    :return: :class:`str` or :class:`~pandas.Series` - depending on the input

    :raises:
        :TypeError: |indf_error|.
        :KeyError: If the column ``structure_<seqID>`` cannot be found.

    .. rubric:: Example

    .. ipython::

        In [1]: from rstoolbox.io import parse_rosetta_file
           ...: import pandas as pd
           ...: pd.set_option('display.width', 1000)
           ...: pd.set_option('display.max_columns', 500)
           ...: df = parse_rosetta_file("../rstoolbox/tests/data/input_ssebig.minisilent.gz",
           ...:                         {'structure': 'C'})
           ...: df.get_structure('C').iloc[:5]
    """
    _check_type(self)
    if key_residues is None:
        return self[_check_column(self, "structure", seqID)]
    else:
        return _get_key_sequence(self, "structure", seqID, key_residues)

def get_available_structure_predictions( self ):
    """ List which **structure prediction** identifiers are available in the data container.

    :return: :func:`list` of :class:`str`

    :raises:
        :TypeError: |indf_error|.
    """
    _check_type(self)
    return _get_available(self, "psipred_")

def get_structure_prediction( self, seqID, key_residues=None ):
    """
    Return the structure prediction(s) data.

    :param str seqID: |seqID_param|.
    :param key_residues: |keyres_param|.
    :type key_residues: |keyres_types|

    :return: :class:`str` or :class:`~pandas.Series` - depending on the input

    :raises:
        :TypeError: |indf_error|.
        :KeyError: If the column ``psipred_<seqID>`` cannot be found.
    """
    _check_type(self)
    if key_residues is None:
        return self[_check_column(self, "psipred", seqID)]
    else:
        return _get_key_sequence(self, "psipred", seqID, key_residues)

def get_sequential_data( self, query, seqID ):
    """Provides data on the requested query.

    Basically, this allows :class:`str` access to the other getters.

    :param str query: Query type: ``sequence``, ``structure``, ``structure_prediction``.
    :param str seqID: |seqID_param|.

    :return: :class:`str` or :class:`~pandas.Series` - depending on the input

    :raises:
        :TypeError: |indf_error|.
        :KeyError: If ``query`` has a non-accepted value.

    .. seealso::
        :meth:`.DesignFrame.get_sequence`
        :meth:`.DesignFrame.get_structure`
        :meth:`.DesignFrame.get_structure_prediction`
        :meth:`.DesignSeries.get_sequence`
        :meth:`.DesignSeries.get_structure`
        :meth:`.DesignSeries.get_structure_prediction`

    .. rubric:: Example

    .. ipython::

        In [1]: from rstoolbox.io import parse_rosetta_file
           ...: import pandas as pd
           ...: pd.set_option('display.width', 1000)
           ...: pd.set_option('display.max_columns', 500)
           ...: df = parse_rosetta_file("../rstoolbox/tests/data/input_ssebig.minisilent.gz",
           ...:                         {'sequence': 'C', 'structure': 'C'})
           ...: df.get_sequential_data('sequence', 'C').iloc[:5]
           ...: df.get_sequential_data('structure', 'C').iloc[:5]
    """
    queries = ["sequence", "structure", "structure_prediction"]
    if query.lower() not in queries:
        raise KeyError("Available queries are: {}".format(",".join(queries)))

    if query.lower() == "sequence":
        return self.get_sequence(seqID)
    if query.lower() == "structure":
        return self.get_structure(seqID)
    if query.lower() == "structure_prediction":
        return self.get_structure_prediction(seqID)

def get_available_labels( self ):
    """
    List which slabels are available in the data container.

    :return: :func:`list` of :class:`str`

    :raises:
        :TypeError: |indf_error|.

    .. rubric:: Example

    .. ipython::

        In [1]: from rstoolbox.io import parse_rosetta_file
           ...: import pandas as pd
           ...: pd.set_option('display.width', 1000)
           ...: pd.set_option('display.max_columns', 500)
           ...: df = parse_rosetta_file("../rstoolbox/tests/data/input_2seq.minisilent.gz",
           ...:                         {'labels': ['MOTIF', 'CONTACT', 'CONTEXT'],
           ...:                          'sequence': 'A'})
           ...: df.get_available_labels()
    """
    _check_type(self)
    return _get_available(self, "lbl_")

def get_label( self, label, seqID=None ):
    """Return the content(s) of the **labels** of interest as a :class:`.Selection`
    for a given sequece.

    If only one ``seqID`` is available, it will automatically pick labels for that,
    even if other data is present; otherwise, ``seqID`` must be provided. This takes
    into account availability of sequence, structure and psipred data.

    :param str label: Label identifier. Will be uppercased.
    :param str seqID: |seqID_param|.

    :return: :class:`.Selection`

    :raises:
        :TypeError: |indf_error|.
        :KeyError: If ``seqID`` is not specified and more than one ``seqID`` is possible.
        :KeyError: If the column ``lbl_<label>`` cannot be found.

    .. rubric:: Example

    .. ipython::

        In [1]: from rstoolbox.io import parse_rosetta_file
           ...: import pandas as pd
           ...: pd.set_option('display.width', 1000)
           ...: pd.set_option('display.max_columns', 500)
           ...: df = parse_rosetta_file("../rstoolbox/tests/data/input_2seq.minisilent.gz",
           ...:                         {'labels': ['MOTIF', 'CONTACT', 'CONTEXT'],
           ...:                          'sequence': 'A'})
           ...: df.get_label('CONTACT', 'A')
           ...: df.get_label('CONTACT', 'B')
    """
    from rstoolbox.components import Selection, SelectionContainer
    listtypes = (list, np.ndarray, six.string_types)
    if six.PY3:
        listtypes = (list, np.ndarray, range, six.string_types)

    _check_type(self)
    if seqID is None:
        ss = set(get_available_sequences(self))
        sr = set(get_available_structures(self))
        sp = set(get_available_structure_predictions(self))
        sa = ss.union(sr).union(sp)
        if len(sa) > 1:
            raise KeyError("Information for multiple seqID is present. Choose one.")
        else:
            seqID = sa.pop()
    data = self[_check_column(self, "lbl", label.upper())]
    if isinstance(self, pd.DataFrame):
        return data.apply(lambda x: x[seqID])
    elif isinstance(data, SelectionContainer):
        return data[seqID]
    elif isinstance(data, Selection):
        return data
    elif isinstance(data, listtypes):
        return Selection(data)


# This is just a script to print json files neatly.

from pprint import pprint
import json
import argparse

#Read Args
parser = argparse.ArgumentParser()
parser.add_argument("filea", help="First Json.")
parser.add_argument("fileb", help="Second Json.")
args = parser.parse_args()

#Setup printer (just for debug output)
#pp = pprint.PrettyPrinter(indent=4)

with open(args.filea, 'r') as handle:
    filea = json.load(handle)

with open(args.fileb, 'r') as handle:
    fileb = json.load(handle)

print("PRINTING FILE A")
with open('fileaout.txt', 'w') as handle:
    pprint(filea,stream=handle)

print("PRINTING FILE B")
with open('filebout.txt', 'w') as handle:
    pprint(fileb,stream=handle)


import csv
import flet as ft
from movie_class import Movie

WELCOME = "Welcome to KinoPaís! \nThe best place to find global movies! \nPlease choose one of the countries below and revieve a list of the top ten movies from that country!\n"
COUNTS = ["Africa", "Balkans", "Benelux", "Central America", "Canada", "Germany", "Greater China", "Iberia", "India", "Italy", "Japan", "Korea", "Mexico", "Middle East", "Russia", "Scandinavia", "South America", "United Kingdom", "United States"]

COUNTRIES = ''
COUNTS = sorted(COUNTS)
for x in COUNTS:
  if x == COUNTS[len(COUNTS)-1]:
    COUNTRIES = COUNTRIES + x + '.'
  else:
    COUNTRIES = COUNTRIES + x + ', '

for x in range(len(COUNTS)):
  COUNTS[x] = COUNTS[x].lower()

def main(page: ft.Page):
  global count
  count = 0
  page.horizontal_alignment = 'center'
  def btn_click(e):
    global count
    if not txt_country.value:
        txt_country.error_text = "Please enter a country"
        page.update()
    elif txt_country.value.lower() not in COUNTS:
        txt_country.error_text = "Invalid Entry. Please Try Again"
        page.update()
    else:
        #this is where we take the users input
        if count != 0:
          try:
            page.controls.pop(5)
          except:
            pass

        count = 1

        csv_filename = txt_country.value

        movie_list = []

        fp = open(csv_filename + ".csv", mode="r", encoding='latin 1')
        fp.readline()

        for line in fp:
          line = line.strip().split(',$,')
          film_title = line[0]
          release_year = int(line[1])
          director = line[2]
          cast = list(line[3])
          personal_rating = line[4]
          average_rating = float(line[5])
          url = line[6]

          movie_list.append(Movie(film_title, release_year, director, cast, personal_rating, average_rating, url))

        lv = ft.ListView(expand=1, spacing=10, padding=20, auto_scroll=True)

        #this is where we add the algorithms output
        for i in range(10):
          lv.controls.append(ft.Text(f"{movie_list[i]}",text_align='center'))
        page.add(lv)

  title =  ft.Row([ft.Text('KinoPaís', style=ft.TextThemeStyle.DISPLAY_LARGE)],alignment=ft.MainAxisAlignment.CENTER, wrap=True)
  welcome = ft.Row([ft.Text(WELCOME, style=ft.TextThemeStyle.TITLE_SMALL, text_align='center')],alignment=ft.MainAxisAlignment.CENTER, wrap=True)
  instructions = ft.Row([ft.Text(COUNTRIES, style=ft.TextThemeStyle.TITLE_SMALL, text_align='center')],alignment=ft.MainAxisAlignment.CENTER, wrap=True)

  txt_country = ft.TextField(label="Country")

  page.add(title, welcome, instructions,
  txt_country, ft.Row([ft.ElevatedButton("GO!", icon="MOVIE", bgcolor='BLACK', on_click=btn_click)], alignment=ft.MainAxisAlignment.CENTER))

if __name__ == "__main__":
    ft.app(target=main)


import socket
import threading

HEAD_LEN = 4

active_sockets = []

def handle_connections(clients):
    while True:
        for client in clients:
            try:
                msg = my_recv(client)
                if msg is None:
                    continue
                client.sendall(f'{len(msg):0{HEAD_LEN}}'.encode('utf-8') + msg)
            except BlockingIOError:
                continue

def my_recv(sock):
    head = sock.recv(HEAD_LEN)
    if head == b'':
        active_sockets.remove(sock)
        sock.close()
        print('DISCONNECTED')
        return
    while len(head) < HEAD_LEN:
        head += sock.recv(HEAD_LEN-len(head))

    head = int(head)
    data = sock.recv(head)
    while len(data) < head:
        data += sock.recv(head-len(data))

    return data

def main():
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_socket.setblocking(False)
    server_socket.bind(('localhost', 8877))
    server_socket.listen()

    print('server is running...')
    thread = threading.Thread(target=handle_connections, args=(active_sockets,))
    thread.start()
    while True:
        try:
            conn, addr = server_socket.accept()
            active_sockets.append(conn)

        except (BlockingIOError, TimeoutError):
            pass  # xd

        except KeyboardInterrupt:
            for s in active_sockets:
                s.close()

if __name__ == '__main__':
    main()


import numpy as np
import matplotlib.pyplot as plt

import importlib
import Backtesting.Vectorized.cross_compare
importlib.reload(Backtesting.Vectorized.cross_compare)

from Utils.IOUtils import *
import os
from Backtesting.Vectorized.Strategy import SLMStrategy
from Backtesting.Vectorized.backtest import vectorizedbacktest
from Backtesting.Vectorized.cross_compare import ensembler

class LmValidation:
    def __init__(self, slm, start='2016-7-1', end='2016-10-1',symbol='ag', data_dir=r'../../Output', valid_dir=r'../../Validation', min_order=2, max_order=8, offsets_average = False, n_offsets = 5, tca = None, price='LastPrice', fixed_cost = 0.00052,**kwargs):
        '''
        :param slm: language model dataframe having at least two columns: prior and signal
        :param symbol:
        :param data_dir: directory of testing files
        :param max_order: max order of model to test
        '''
        self._slm = slm
        self._symbol = symbol
        self._data_dir = data_dir
        self._valid_dir = valid_dir
        self._min_order = min_order
        self._max_order = max_order
        self._offsets_average = offsets_average
        self._average_return = None
        self._n_offsets = n_offsets
        self._start = start
        self._end = end
        self._price_threshold = kwargs.get('px_th', 0)
        self._price=price
        self._tca = tca
        self._fixed_cost = fixed_cost

    def gen_find(self):
        '''
        Find all filenames in a directory tree that match a shell wildcard pattern.
        '''
        for path, dirlist, filelist in os.walk(self._data_dir):
            for name in fnmatch.filter(filelist, self._symbol + '*'):
                yield name

    def run(self):
        filenames = self.gen_find()
        for filename in filenames:
            print(filename)
            data = pd.read_csv(self._data_dir + '/' + filename, index_col=0)
            data = data[(pd.to_datetime(data.index) >= pd.to_datetime(self._start)) & (pd.to_datetime(data.index) < pd.to_datetime(self._end))]
            if len(data) == 0:
                continue
            else:
                signals = [SLMStrategy(data, self._slm, m-1, px_th=self._price_threshold).generatingsignal() for m in np.arange(self._min_order, self._max_order+1)]
                #tcas = [self._tca for m in np.arange(self._min_order + 1, self._max_order + 1)]
                tcas = [self._tca]*(self._max_order - self._min_order + 1)
                validator_ensemble = ensembler(vectorizedbacktest, signals, price=self._price, tcas = tcas, fixed_cost=self._fixed_cost)
                validator_ensemble.build()
                validator_ensemble.run()

                performance = validator_ensemble.calperformance()
                performance.to_csv(self._valid_dir + '/performance_' + filename)
                validator_ensemble.plot()
                plt.savefig(self._valid_dir + '/performance_' + re.sub('.csv', '.png', filename))
                plt.close()
                # validator_ensemble.plot(target_col="benchmark")
                # plt.savefig(self._valid_dir + '/benchmark_' + re.sub('.csv', '.png', filename))
                # plt.close()
                fig = plt.figure()
                #benchmark = validator_ensemble.results[0]['return'].cumsum() + 1
                benchmark = validator_ensemble.results[0]['benchmark']
                #print(len(benchmark.index))
                #initial_value = benchmark[0]
                #benchmark = benchmark/initial_value
                benchmark.plot()
                plt.title('Benchmark')
                fig.savefig(self._valid_dir + '/benchmark_' + re.sub('.csv', '.png', filename))
                plt.close()

                if self._offsets_average:
                    if not self._average_return:
                        self._average_return = [df['strategy'].reset_index(drop=True) for df in validator_ensemble.results]
                        average_performance = performance
                        average_benchmark = validator_ensemble.results[0]['benchmark']
                        #avg_equitycurve = [df['equitycurve'].reset_index(drop=True) for df in validator_ensemble.results]
                    else:
                        self._average_return = [df1.add(df2['strategy'].reset_index(drop=True), fill_value=0) for (df1, df2) in zip(self._average_return, validator_ensemble.results)]
                        average_performance = average_performance.add(performance, fill_value=0)
                        #avg_equitycurve = [df1.add(df2['equitycurve'].reset_index(drop=True), fill_value=0) for (df1, df2) in zip(avg_equitycurve, validator_ensemble.results)]
                        #average_benchmark = average_benchmark.add(validator_ensemble.results[0]['LastPrice'], fill_value=0)
        '''
        average return of all offsets
        '''
        if self._offsets_average and (self._average_return is not None):
            self._average_return = [(1+df.divide(self._n_offsets)).cumprod() for df in self._average_return]
            #avg_equitycurve = [df.divide(self._n_offsets) for df in avg_equitycurve]
            #average_benchmark = average_benchmark/average_benchmark[0]
            average_benchmark = average_benchmark.to_frame()
            average_benchmark['Date'] = pd.to_datetime(average_benchmark.index)
            #print(average_benchmark)
            fig = plt.figure()
            plt.plot(average_benchmark.Date, average_benchmark.benchmark, label='b')
            for avg_return, label in zip(self._average_return, np.arange(self._min_order, self._max_order+1)):
            #for avg_return, label in zip(avg_equitycurve, np.arange(2, 2 + len(avg_equitycurve))):
                #avg_return.plot(label=label)
                df = avg_return.to_frame()
                df = df.set_index(average_benchmark.index)
                df['Date'] = pd.to_datetime(df.index)
                plt.plot(df.Date, df.strategy, label=label)
            fig.autofmt_xdate()
            plt.legend(loc='upper left')
            plt.title('Equity Curve')
            plt.show()
            fig.savefig(self._valid_dir + '/performance_' + self._symbol + '.png')
            plt.close()

            average_performance = average_performance.divide(self._n_offsets)
            print(average_performance)
            average_performance.to_csv(self._valid_dir + '/performance_' + self._symbol + '.csv')


from utilities.data_management import in_parent_dir, move_to_root, prepare_csv_reader, prepare_csv_writer
from multiprocessing import Pool
from pathlib import Path
from time import time
from functools import partial
import config

def generate_data_modifier(header_list, data_header):
    """ Generates a function to select the desired columns from the source data """
    indexes = [data_header[header] for header in header_list]
    max_ind = max(indexes)

    def data_modifier(document, making_header=False):
        # If document does not contain enough fields, return list of None values
        if len(document) < max_ind:
            return [None] * len(indexes)
        return [document[index] for index in indexes]
    return data_modifier

def get_path(path):
    """ Function that ensures that the path given is either a string or Path (and converts to Path) """
    if isinstance(path, (Path, str)):
        return Path(path)
    raise TypeError('Path given must be of type string or Path')

def check_processes(processes):
    """ Checks passed processes to ensure they are callable """
    if callable(processes):
        return [processes]
    elif isinstance(processes, list):
        for ind, p in enumerate(processes):
            if not callable(p):
                raise ValueError('Process ' + str(ind + 1) + ' provided is not a function')
        return processes

def processor(document, processes):
    """ Function that applies multiple process functions to each document """
    for process in processes:
        document = process(document)
    return document

class job_runner:
    """ Class for long-running processes with large datasets """
    def __init__(self, processes, source_path, dest_path, n_threads=None, dataset_name=None, worker_init=None,
                 chunk_size=10000, worker_lifespan=None):
        self.get_default_params()   # Load defaults

        self.processor = check_processes(processes)     # generate_processor(check_processes(processes))
        self.worker_init = worker_init
        self.chunk_size = chunk_size
        self.worker_lifespan = worker_lifespan
        self.workers = None

        # If alternate parameter value is provided, take it
        if n_threads is not None: self.n_threads = n_threads
        if dataset_name is not None: self.dataset_name = dataset_name

        self.source_path = get_path(source_path)
        self.dest_path = get_path(dest_path)
        self.csv_reader = self.source_file = self.data_header = self.data_modifier = self.csv_writer = self.dest_file \
            = None
        self.data_ready = self.workers_ready = False

    def get_default_params(self):
        """ Loads default parameters from config file """
        self.n_threads = config.n_threads
        self.dataset_name = config.dataset

    def start_workers(self):
        """ Starts worker threads """
        self.workers = Pool(self.n_threads, initializer=self.worker_init, maxtasksperchild=self.worker_lifespan)
        self.workers_ready = True
        print('Workers ready.')

    def shutdown_workers(self):
        """ Stops all worker processes """
        if self.workers_ready:
            self.workers.close()
            self.workers.join()
            print('Workers killed.')

    def prepare_data(self, data_modifier=None):
        """ Opens buffered file-stream with source and destination files """
        self.csv_reader, self.source_file, data_header = prepare_csv_reader(self.source_path)
        self.data_header = {header: index for index, header in enumerate(data_header)}

        if data_modifier is None:
            self.data_modifier = lambda x: x
        elif isinstance(data_modifier, list):
            for item in data_modifier:
                if not isinstance(item, str) and item in self.data_header:
                    raise ValueError('Illegal data modifier, if list of strings is passed, items must match data '
                                     'headers')
            self.data_modifier = generate_data_modifier(data_modifier, self.data_header)
        elif callable(data_modifier):
            self.data_modifier = data_modifier
        else:
            raise TypeError('Data modifier must be a function')

        dest_header = [''] + self.data_modifier(data_header, True)
        self.csv_writer, self.dest_file = prepare_csv_writer(self.dest_path, dest_header)

        self.data_ready = True
        print('Data prepared.')

    def finalize_data(self):
        """ Closes file-streams """
        self.source_file.close()
        self.dest_file.close()
        print('Document finalized.')

    def process_documents(self, data_modifier=None, single_job=True, max_documents=None, list_return=False):
        """ Processes documents """
        # While there is still data to process
        if not self.data_ready:
            self.prepare_data(data_modifier)
        if not self.workers_ready:
            self.start_workers()

        print('Starting document processing')
        eof_reached = False
        documents_processed = chunk_number = 0

        while True:
            chunk_number += 1

            data_start = time()
            data, index = [], 0

            # Enable max_documents functionality
            limit = self.chunk_size
            if max_documents is not None and self.chunk_size + documents_processed > max_documents:
                limit = max_documents

            # Pull documents
            for index in range(limit):
                try:
                    document = next(self.csv_reader)
                    data.append(self.data_modifier(document))
                except StopIteration:
                    eof_reached = True
                    break

            documents_processed += index + 1
            data_time = time() - data_start

            # Process documents
            process_start = time()
            data = self.workers.map(partial(processor, processes=self.processor), data)
            if list_return:
                data = sum(data, [])
            process_time = time() - process_start

            # If it takes more time to load the data then run the process, get larger chunks at a time
            if process_time < data_time:
                self.chunk_size *= 2
                print('Process time less than data load time, doubling chunk size to', self.chunk_size)

            self.csv_writer.writerows((
                [index] + doc_data
                for index, doc_data in enumerate(data, start=documents_processed - limit)
            ))
            # self.csv_writer.writerows(enumerate(data, start=))
            print('Finished chunk', chunk_number)

            # If there are no more documents or there is a max document restriction (that is met)
            if eof_reached or (max_documents is not None and max_documents <= documents_processed):
                break

        # If this is the only job to be run, close files and kill workers
        if single_job:
            self.shutdown_workers()
            self.finalize_data()


from http import HTTPStatus

from django.contrib.auth import get_user_model
from django.test import Client, TestCase
from django.urls import reverse

from ..models import Group, Post

User = get_user_model()

class PostURLTests(TestCase):
    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.author = User.objects.create_user(username='TestAuthor')
        cls.auth_user = User.objects.create_user(username='TestAuthUser')
        cls.group = Group.objects.create(
            title='Тестовая Группа',
            slug='dnk',
            description='тестовое описание группы'
        )
        cls.post = Post.objects.create(
            text='Тестовый текст',
            author=cls.author,
        )

    def setUp(self):
        """Создаем клиент гостя, автора и зарегистрированного пользователя."""
        self.authorized_client = Client()
        self.authorized_client.force_login(PostURLTests.auth_user)
        self.authorized_client_author = Client()
        self.authorized_client_author.force_login(PostURLTests.author)

    def test_urls_response_guest(self):
        """Проверяем статус страниц для гостя."""
        url_names = (
            '/',
            f'/group/{self.group.slug}/',
            f'/profile/{self.author}/',
            f'/posts/{self.post.pk}/',
        )
        for url in url_names:
            with self.subTest():
                response = self.client.get(url)
                error_name = (
                    f'Ошибка: нет доступа'
                    f'guest до страницы {url}'
                )
                self.assertEqual(
                    response.status_code, HTTPStatus.OK, error_name
                )

    def test_urls_response_auth(self):
        """Проверяем статус страниц для зарегистрированного пользователя."""
        url_status = {
            reverse(
                'posts:post_edit', kwargs={'post_id': PostURLTests.post.pk}
            ): HTTPStatus.OK,
            reverse('posts:post_create'): HTTPStatus.OK,
        }
        for url, status_code in url_status.items():
            with self.subTest(url=url):
                response = self.authorized_client_author.get(url)
                error_name = (f'Ошибка: нет доступа auth_client'
                              f'до страницы {url}')
                self.assertEqual(response.status_code, status_code, error_name)

    def test_urls_response_guest_redirect(self):
        """Проверяем редирект страниц гостя."""
        pages = {
            '/create/': '/auth/login/?next=/create/',
            f'/posts/{self.post.pk}/edit/':
            f'/auth/login/?next=/posts/{self.post.pk}/edit/'
        }
        for page, value in pages.items():
            response = self.client.get(page)
            self.assertRedirects(response, value)

    def test_urls_response_not_author_redirect(self):
        """Проверяем редирект страниц пользователя не автора."""
        response = self.authorized_client.get(
            f'/posts/{self.post.pk}/edit/', follow=True
        )
        self.assertRedirects(
            response, (
                f'/posts/{self.post.pk}/'
            )
        )

    def test_urls_uses_correct_template(self):
        """Проверяем, что адреса используют соответствующие шаблоны."""
        pages_adress = {
            '/': 'posts/index.html',
            f'/group/{self.group.slug}/': 'posts/group_list.html',
            f'/profile/{self.post.author}/': 'posts/profile.html',
            f'/posts/{self.post.pk}/': 'posts/post_detail.html',
            '/create/': 'posts/create_post.html',
            f'/posts/{self.post.pk}/edit/': 'posts/create_post.html', }
        for address, template in pages_adress.items():
            with self.subTest(address=address):
                response = self.authorized_client_author.get(address)
                self.assertTemplateUsed(response, template)


from flask_restful import (reqparse, abort, Resource, fields, marshal)
from flask_jwt_extended import (create_access_token, create_refresh_token, get_jwt_identity, fresh_jwt_required,
                                jwt_required, jwt_refresh_token_required)

from .. import db
from ..model import User, Board, BoardPost, Comment, BoardPostVote
from ..services.login import login_required, permission_required, get_user, UserPermission
from ..services.board import (create_board, post_board, get_boards, get_posts, get_userinfo, post_post, BoardResult,
                            get_post_count, get_post_content, post_comment, delete_post, delete_comment, get_comment,
                            delete_board, edit_post, edit_comment, vote_up_post, vote_down_post, get_vote_up, get_vote_down,
                            is_voted_up, is_voted_down)

MSG_REQUIRED = 'This field is required.'

class UserField(fields.Raw):
    def format(self, value: User):
        if value == None:
            return None
        else:
            return {
                'username':value.username,
                'nickname':value.nickname
                }

post_field = {
    'id': fields.Integer,
    'title': fields.String,
    'content': fields.String,
    'vote_up': fields.Integer,
    'vote_down': fields.Integer,
    'visited': fields.Integer,
    'writer': UserField(attribute='owner')

}

class CommentField(fields.Raw):
    def format(self, value: Comment):
        return {
            'id': value.id,
            'content': value.content,
            'vote_up': value.vote_up,
            'vote_down': value.vote_down,
            'writer': UserField().format(value.owner)
        }

post_content_field = {
    'id': fields.Integer,
    'board_id': fields.Integer,
    'title': fields.String,
    'content': fields.String,
    'vote_up': fields.Integer,
    'vote_down': fields.Integer,
    'visited': fields.Integer,
    'writer': UserField(attribute='owner'),
    'comments': fields.List(CommentField)

}

class BoardManage(Resource):

    def get(self):
        boards = get_boards()
        result = []
        for b in boards:
            result.append({
                'id':b.id,
                'name':b.name,
                'count':get_post_count(b.id)
            })

        return {"boards":result}, 200

    @permission_required(UserPermission.Admin)
    def post(self):

        parser = reqparse.RequestParser()

        parser.add_argument('name', type=str, required=True, help=MSG_REQUIRED)
        parser.add_argument('permission_read', type=int, default=0)
        parser.add_argument('permission_write', type=int, default=0)

        args = parser.parse_args()

        result = create_board(Board(name=args['name'], permission_read=args['permission_read'],
                                permission_write=args['permission_write']),
                                get_user())

        return {}, 200

class BoardPostList(Resource):

    @login_required
    def get(self, board_id):
        parser = reqparse.RequestParser()
        parser.add_argument('page', type=int, default=1)
        parser.add_argument('pagesize', type=int, default=10)

        args = parser.parse_args()

        args['pagesize'] = min(args['pagesize'], 50)

        result, posts = get_posts(board_id, amount=args['pagesize'], start=(args['page'] - 1) * args['pagesize'])

        if result == BoardResult.NO_BOARD_EXISTS:
            return {"message":"게시판이 없습니다."}, 400

        output = [marshal(i, post_field) for i in posts]

        return {
            'totalcount': get_post_count(board_id),
            'count': len(output),
            'posts': output
        }, 200

    @login_required
    def post(self, board_id):
        parser = reqparse.RequestParser()

        parser.add_argument('title', type=str, required=True, help=MSG_REQUIRED)
        parser.add_argument('content', type=str, required=True, help=MSG_REQUIRED)

        args = parser.parse_args()

        newpost = BoardPost(title=args['title'], content=args['content'])
        result = post_post(board_id, newpost, get_user())
        if result == BoardResult.NO_BOARD_EXISTS:
            return {"message":"게시판이 없습니다."}, 404
        if result == BoardResult.SUCCESS:
            return {}, 201
        return {}, 500

    @permission_required(UserPermission.Admin)
    def delete(self, board_id):
        result = delete_board(board_id)

        if result == BoardResult.NOT_EXISTS:
            return {"message":"게시판이 없습니다."}, 404
        if result == BoardResult.PERMISSION_REQUIRED:
            return {"message":"권한이 없습니다."}, 403
        if result == BoardResult.SUCCESS:
            return {}, 200

        return {}, 500

class BoardPostView(Resource):

    @login_required
    def get(self, post_id):

        result, post = get_post_content(post_id)
        if result == BoardResult.SUCCESS:

            return marshal(post, post_content_field), 200
        if result == BoardResult.NOT_EXISTS:
            return {"message":"게시글이 없습니다."}, 404

        return {}, 500

    @login_required
    def delete(self, post_id):

        result = delete_post(post_id)

        if result == BoardResult.NOT_EXISTS:
            return {"message":"게시글이 없습니다."}, 404
        if result == BoardResult.NOT_OWNER:
            return {"message":"권한이 없습니다."}, 403

        if result == BoardResult.SUCCESS:
            return {}, 200

        return {}, 500

    @login_required
    def patch(self, post_id):
        parser = reqparse.RequestParser()

        parser.add_argument("title", type=str, required=True, help=MSG_REQUIRED)
        parser.add_argument("content", type=str, required=True, help=MSG_REQUIRED)

        args = parser.parse_args()

        result = edit_post(post_id, BoardPost(title=args['title'],content=args['content']))

        if result == BoardResult.NOT_EXISTS:
            return {}, 404
        if result == BoardResult.SUCCESS:
            return {}, 200

        return {}, 500

class BoardPostVote(Resource):

    @login_required
    def get(self, post_id):
        return {
            "up":is_voted_up(post_id),
            "down":is_voted_down(post_id)
        }

    @login_required
    def post(self, post_id):
        parser = reqparse.RequestParser()

        parser.add_argument("isup", type=bool, required=True, help=MSG_REQUIRED)

        args = parser.parse_args()

        if args['isup']:
            result = vote_up_post(post_id)
        else:
            result = vote_down_post(post_id)

        if result == BoardResult.SUCCESS:
            return {}, 200

        if result == BoardResult.NOT_EXISTS:
            return {}, 404

        if result == BoardResult.EXISTS:
            return {}, 409

        return {}, 500

class BoardComment(Resource):

    @login_required
    def post(self):

        parser = reqparse.RequestParser()

        parser.add_argument('post_id', type=int, required=True, help=MSG_REQUIRED)
        parser.add_argument('content', type=str, required=True, help=MSG_REQUIRED)

        args = parser.parse_args()
        post_id = args['post_id']
        content = args['content']

        comment = Comment(content=content)

        result = post_comment(post_id, comment, get_user())

        if result == BoardResult.SUCCESS:
            return {}, 201
        if result == BoardResult.NOT_EXISTS:
            return {"message":"게시글이 없습니다."}, 404
        return {}, 500

class BoardCommentAction(Resource):

    @login_required
    def get(self, comment_id):
        result, comment = get_comment(comment_id)

        if result == BoardResult.SUCCESS:
            return CommentField().format(comment), 200
        if result == BoardResult.NOT_EXISTS:
            return {"message":"덧글이 없습니다."}, 404
        return {}, 500

    @login_required
    def delete(self, comment_id):
        result = delete_comment(comment_id)

        if result == BoardResult.SUCCESS:
            return {}, 200
        if result == BoardResult.NOT_EXISTS:
            return {"message":"덧글이 없습니다."}, 404
        if result == BoardResult.NOT_OWNER:
            return {"message":"권한이 없습니다."}, 403
        return {}, 500

    @login_required
    def patch(self, comment_id):
        parser = reqparse.RequestParser()

        parser.add_argument("content", type=str, required=True, help=MSG_REQUIRED)

        args = parser.parse_args()

        result = edit_comment(comment_id, Comment(content=args['content']))

        if result == BoardResult.NOT_EXISTS:
            return {}, 404
        if result == BoardResult.SUCCESS:
            return {}, 200

        return {}, 500


#Manuel Ortiz at 2022
#Extracted from: https://leetcode.com/problems/integer-to-roman/

I,V,X,L,C,D,M = 'I','V','X','L','C','D','M'
SYMBOLS = {1: I, 5: V, 10: X, 50: L, 100: C, 500: D, 1000: M}
import math

def intToRoman(num):
    log10 = lambda num: int(math.log10(num))
    div = lambda num,n: (num//(10**n)) * (10**n)
    n,s = 0, ''
    while (num-n)>0:
        num = num - n
        l = log10(num)
        n = div(num, l)
        if n in SYMBOLS:
            s+=SYMBOLS[n]
            continue
        a,b = 10**l, (10**(l+1))
        half = b//2
        if (n<(half-a) or n>half) and n<(b-a):
            times = (n//a)%5
            s+= SYMBOLS[a]*times if not n>half else SYMBOLS[half]+(SYMBOLS[a]*times)
            continue
        diff = (b-n)
        s += (SYMBOLS[diff] + SYMBOLS[diff*10]) if diff in SYMBOLS else (SYMBOLS[half-n] + SYMBOLS[half])

    return s

intToRoman(60)


import sys
from dataset.dataset_for_mrc_squad import DatasetForMrcSquad as Dataset
from preprocess.preprocess_for_mrc import PreprocessForMRCChinese as Preprocess
from model.network.mrc_net import MRCNet as Net
from engine.mrc_train_engine import MRCTrainEngine as TrainEngine
from engine.mrc_predict_engine import MRCPredictEngine as PredictEngine
from util.util_parameter import UtilParameter as UParam
from util.util_logging import UtilLogging as ULog

if __name__ == "__main__":
    # 设置参数
    param = UParam()
    param.read_config_file("config_ernie")
    param.set_config(sys.argv[1:])
    args = param.get_config(param.GLOBAL)
    # 初始化日志
    logger = ULog(args, params=param)
    app_name = args["app_name"]
    dataset_args = param.get_config(param.DATASET)

    # 训练数据预处理
    train_dataset = Dataset(dataset_args)
    train_dataset.read_from_srcfile(dataset_args['train_file_path'], cache=dataset_args['train_example_file_name'],
                                    is_training=True)
    train_preprocess = Preprocess(args=dataset_args, examples=train_dataset.get_examples(),
                                  cache=dataset_args['train_feature_file_name'])
    train_data_generator = train_preprocess.do_preprocess()

    # # 验证数据预处理
    valid_dataset = Dataset(dataset_args)
    valid_dataset.read_from_srcfile(dataset_args['dev_file_path'], cache=dataset_args['dev_example_file_name'],
                                    is_training=True)
    valid_preprocess = Preprocess(args=dataset_args, examples=valid_dataset.get_examples(),
                                  cache=dataset_args['dev_feature_file_name'])
    valid_data_generator = valid_preprocess.do_preprocess()
    valid_data = {'features': valid_preprocess.get_features(), 'examples': valid_dataset.get_examples(),
                    'dev_file_path': dataset_args['dev_file_path']}

    # 预测数据预处理
    predict_dataset = Dataset(dataset_args)
    predict_dataset.read_from_srcfile(dataset_args['test_file_path'], cache=dataset_args['test_example_file_name'],
                                      is_training=False)
    predict_preprocess = Preprocess(args=dataset_args, examples=predict_dataset.get_examples(), is_prediction=True,
                                    cache=dataset_args['test_feature_file_name'])
    predict_data_generator = predict_preprocess.do_preprocess()

    predict_data = {'features': predict_preprocess.get_features(), 'examples': predict_dataset.get_examples(),
                    'test_file_path': dataset_args['test_file_path']}

    # 初始化网络
    net_args = param.get_config(param.MODEL_BUILD)
    net = Net(args=net_args)

    # 训练过程
    train_args = param.get_config(param.TRAIN)
    train_engine = TrainEngine(args=train_args, network=net,
                               train_data_generator=train_data_generator,
                               valid_data_generator=valid_data_generator, valid_data=valid_data)
    train_engine.train()

    # 预测过程
    test_args = param.get_config(param.PREDICT)
    predict_engine = PredictEngine(test_args, net)
    predict_engine.predict(predict_data_generator, predict_data=predict_data)


import os
import re
import datetime
import platform
import pywintypes
import win32file

def update_file_creation_date(folder_path, filename):
    filepath = os.path.join(folder_path, filename)
    pattern = r'^(\d{4})(\d{2})(\d{2})_.*\.pdf$'
    match = re.match(pattern, filename)

    if match:
        year = int(match.group(1))
        month = int(match.group(2))
        day = int(match.group(3))

        date_time = datetime.datetime(year, month, day)

        if platform.system() == 'Windows':
            handle = win32file.CreateFile(filepath, win32file.GENERIC_WRITE,
                                          win32file.FILE_SHARE_READ | win32file.FILE_SHARE_WRITE | win32file.FILE_SHARE_DELETE,
                                          None, win32file.OPEN_EXISTING, win32file.FILE_ATTRIBUTE_NORMAL, None)
            win32file.SetFileTime(handle, pywintypes.Time(date_time), None, None)
            handle.close()
        else:
            epoch_time = date_time.timestamp()
            os.utime(filepath, (epoch_time, epoch_time))

    else:
        print(f"Dateiname {filename} entspricht nicht dem erwarteten Format")

def process_folder(folder_path):
    for filename in os.listdir(folder_path):
        if filename.lower().endswith(".pdf"):
            update_file_creation_date(folder_path, filename)

def main():
    print("\nEdit Creation Date Tool\n")
    print("===============")
    print("Type exit to quit the program.")
    print("===============\n")

    folder_path = input("Folder path: ")

    print("Creation date edit done.\n")

    while folder_path.lower() != 'exit':
        process_folder(folder_path)
        folder_path = input("Folder path: ")

        print("Creation date edit done.\n")

if __name__ == '__main__':
    main()


import torch.nn as nn
from .networks import NetworkBase

class SmallNet(NetworkBase):

    def __init__(self, freeze=False, bb_nc=4, prob_nc=1):
        super(SmallNet, self).__init__()

        self._features = self._make_fatures()
        self._bb_reg = self._make_reg(bb_nc)
        self._prob_reg = self._make_reg(prob_nc, add_sigmoid=True)

        self._set_requires_grads(self._features, requires_grads=(not freeze))
        self._set_requires_grads(self._bb_reg, requires_grads=(not freeze))
        self._set_requires_grads(self._prob_reg, requires_grads=(not freeze))

    def _make_fatures(self):
        cfg = [64, 'M', 128, 'M', 256, 'M', 512, 'M', 512, 'M', 512, 'M', 512, 'M']
        layers = []
        in_channels = 3

        for v in cfg:
            if v == 'M':
                layers += [nn.MaxPool2d(kernel_size=2, stride=2)]
            else:
                conv2d = nn.Conv2d(in_channels, v, kernel_size=3, padding=1)
                layers += [conv2d, nn.ReLU(inplace=True)]
                in_channels = v

        return nn.Sequential(*layers)

    def _make_reg(self, out_nc, add_sigmoid=False):
        layers = [ nn.Linear(512*2, 512),
                   nn.ReLU(True),
                   nn.Dropout(),
                   nn.Linear(512, out_nc)]

        if add_sigmoid:
            layers.append(nn.Sigmoid())

        return nn.Sequential(*layers)

    def forward(self, x):
        features = self._features(x)
        features = features.view(features.size(0), -1)
        return self._bb_reg(features), self._prob_reg(features)


#Python is the worst language for decimals
import numpy as np
from decimal import Decimal, getcontext
class Solution(object):
    def calculateHelper(self, r, new_sol):

        if float(r) == 0:
            #print("hm")
            self.sol_array.append(new_sol)
            return
        elif r < 0:
            #print("wrong")
            return

        for k,v in self.menu.items():
            new_sol[k] = new_sol.get(k, 0) + 1

            val = float(r - v)
            self.calculateHelper(val, new_sol)

            new_sol = {}

    def calculate(self, menu, receipts):
        getcontext().prec = 2
        #start algorithm
        self.sol_array = []

        self.menu = menu
        self.reciepts = receipts

        #pseudocode
        for r in receipts:
            #helper
            print(r)
            print(self.sol_array)
            new_sol = {}
            self.calculateHelper(r, new_sol)
        return self.sol_array

if __name__ == "__main__":

    menu = {}
    menu["veggie sandwich"] = 6.85
    menu["extra veggies"] = 2.20
    menu["chicken sandwich"] = 7.85
    menu["extra chicken"] = 3.20
    menu["cheese"] = 1.25
    menu["chips"] = 1.40
    menu["nachos"] = 3.45
    menu["soda"] = 2.05

    reciepts = [4.85, 11.05, 13.75, 17.75, 18.25, 19.40, 28.25, 40.30, 75.00]
    sol = Solution()

    print(sol.calculate(menu, reciepts))


import torch
import torch.nn as nn

# https://gist.github.com/baraldilorenzo/07d7802847aaad0a35d3
class Extractor(nn.Module):
    def __init__(self):
        super(Extractor, self).__init__()
        self.padding = (1, 0, 0)
        self.kernel_size = (3, 3, 3)
        self.dilation = (1, 1, 1)
        self.stride = (1, 1, 1)
        self.activation = nn.LeakyReLU()

        self.bn1 = nn.BatchNorm3d(4, affine=True)
        self.conv1 = nn.Conv3d(in_channels=4,
                               out_channels=4,
                               kernel_size=self.kernel_size,
                               padding=self.padding,
                               stride=self.stride)
        self.conv2 = nn.Conv3d(in_channels=4,
                               out_channels=8,
                               kernel_size=self.kernel_size,
                               padding=(2, 2, 2))
        self.pool = nn.MaxPool3d(kernel_size=(3, 3, 3), stride=(1, 2, 2))

        self.bn2 = nn.BatchNorm3d(8, affine=True)
        self.conv3 = nn.Conv3d(in_channels=8,
                               out_channels=8,
                               padding=self.padding,
                               kernel_size=self.kernel_size,
                               stride=self.stride)
        self.conv4 = nn.Conv3d(in_channels=8,
                               out_channels=16,
                               kernel_size=self.kernel_size,
                               padding=(2, 2, 2))

        self.bn3 = nn.BatchNorm3d(16, affine=True)
        self.conv5 = nn.Conv3d(in_channels=16,
                               out_channels=16,
                               kernel_size=self.kernel_size,
                               padding=self.padding,
                               stride=self.stride)
        self.conv6 = nn.Conv3d(in_channels=16,
                               out_channels=32,
                               kernel_size=self.kernel_size,
                               padding=(2, 2, 2))

    def forward(self, x):
        x = self.bn1(x)
        y = self.conv1(x)
        y = self.activation(y)
        y = self.conv2(y)
        y = self.pool(y)

        y = self.bn2(y)
        y = self.conv3(y)
        y = self.activation(y)
        y = self.conv4(y)
        y = self.activation(y)
        y = self.pool(y)

        y = self.bn3(y)
        y = self.conv5(y)
        y = self.activation(y)
        y = self.conv6(y)
        y = self.activation(y)
        y = self.pool(y)

        return y.view((x.shape[0], -1, x.shape[2]))

# m = Extractor()
# x = torch.rand((32, 4, 12, 24, 72))
# y = m(x)
# print(y.size())


import disnake
from disnake.ext import commands

from src.converters import not_self_member
from src.translation import get_translator
from src.logger import get_logger
from src.bot import SEBot
from src.ext.game.services.games import DiceGame
from src.ext.game.views.game_interfaces import DiceDiscordInterface
from src.ext.game.services.lobby import Lobby
from src.ext.game.views.lobby_view import LobbyView
from src.ext.game.utils import user_to_player

logger = get_logger()
t = get_translator(route="ext.games")
GAMES_MAPPING = games_mapping = {
    t('dice_game'): (DiceGame, DiceDiscordInterface),
}

class PlayCog(commands.Cog):
    def __init__(self, bot: SEBot) -> None:
        self.bot = bot

    @commands.slash_command()
    async def play(
        self,
        inter: disnake.GuildCommandInteraction,
        bet: commands.Range[25, ...],
        game: str = commands.Param(choices=list(GAMES_MAPPING.keys())),
        player: disnake.Member = commands.Param(default=None, converter=not_self_member),
    ) -> None:
        """
        Сыграть в игру на деньги

        Parameters
        ----------
        bet: Ставка, которую будет обязан сделать каждый участвующий
        game: Игра
        player: Приглашённый игрок. Он сможет присоединиться, даже если лобби будет закрыто
        """
        (game_type, interface_type) = GAMES_MAPPING[game]
        discord_game = game_type()

        lobby = Lobby(inter.guild.id, bet, discord_game, user_to_player(inter.author))
        lobby.add(user_to_player(inter.author))
        if player:
            lobby.invite(user_to_player(player))

        view = LobbyView(inter.guild, lobby, game, interface_type)
        await view.start_from(inter)

def setup(bot) -> None:
    bot.add_cog(PlayCog(bot))


from flask import Flask, render_template, url_for, request, redirect
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
import random

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
functionDatabase = SQLAlchemy(app)

create_post = False
title = 'Šaralica'
func_search = ''

class FunctionSite(functionDatabase.Model):
    id = functionDatabase.Column(functionDatabase.Integer, primary_key=True)
    name = functionDatabase.Column(functionDatabase.String(30), nullable=False, default=' ')
    description = functionDatabase.Column(functionDatabase.String(600), nullable=False, default=' ')
    language = functionDatabase.Column(functionDatabase.String(12), nullable=False, default=' ')
    date_created = functionDatabase.Column(functionDatabase.DateTime, default=datetime.utcnow)
    bookmark = functionDatabase.Column(functionDatabase.Boolean, default=False)

    def __repr__(self):
        return '<Site %r>' % self.id

@app.route('/', methods=['POST', 'GET'])
def index():
    functions = FunctionSite.query.order_by(FunctionSite.name).all()
    card_generated = False
    bookmarked = []

    for function in functions:
        if function.bookmark:
            if not card_generated:
                card_generated = True
            bookmarked.append(function)

    marked = random.choice(bookmarked)

    if request.method == 'POST':
        global func_search
        func_search = request.form['search']
        try:
            return redirect('/pretraga')
        except:
            return 'Error: Pretraga neuspješna :('

    else:
        return render_template('index.html', create_post=create_post, title=title, functions=functions, generated=card_generated, marked=marked)

@app.route('/otvaranje')
def open():
    global create_post, title
    create_post = True
    title = 'Izrada'
    return redirect('/')
@app.route('/uređivanje/<int:id>', methods=['GET', 'POST'])
def open_edit(id):
    function_to_edit = FunctionSite.query.get_or_404(id)

    if request.method == 'POST':
        function_to_edit.description = request.form['description']
        function_to_edit.name = request.form['name']
        function_to_edit.language = request.form['language']

        try:
            functionDatabase.session.commit()
            return redirect('/')

        except:
            return 'Error: Uređivanje neuspješno :('

    else:
        return render_template('update.html', function=function_to_edit)

@app.route('/zatvaranje')
def close():
    global create_post, title
    create_post = False
    title = 'Šaralica'
    return redirect('/')

@app.route('/izrada', methods=['POST', 'GET'])
def create():
    if request.method == 'POST':
        func_description = request.form['description']
        func_name = request.form['name']
        func_language = request.form['language']
        new_func = FunctionSite(description=func_description, name=func_name, language=func_language)

        try:
            functionDatabase.session.add(new_func)
            functionDatabase.session.commit()
            return redirect('/')
        except:
            return 'Error: Izrada neuspješna :('

    else:
        return render_template('index.html', functions=functions)

@app.route('/pretraga')
def search():
    global func_search
    global isFound
    isFound = False
    functions = FunctionSite.query.order_by(FunctionSite.name).all()

    for function in functions:
        if (func_search in function.name or func_search in function.language) or (function.name in func_search or function.language in func_search):
            isFound = True

    return render_template('search.html', search=func_search, functions=functions, isFound=isFound)

@app.route('/brisanje/<int:id>')
def delete(id):
    function_to_delete = FunctionSite.query.get_or_404(id)

    try:
        functionDatabase.session.delete(function_to_delete)
        functionDatabase.session.commit()
        return redirect('/')
    except:
        return 'Error: Brisanje neuspješno :('

@app.route('/označivanje/<int:id>')
def bookmark(id):
    function_to_mark = FunctionSite.query.get_or_404(id)

    try:
        if function_to_mark.bookmark:
            function_to_mark.bookmark = False
        else:
            function_to_mark.bookmark = True
        functionDatabase.session.commit()
        return redirect('/')
    except:
        return 'Error: Označivanje neuspješno :('

@app.route('/definicija/<int:id>', methods=['GET', 'POST'])
def function(id):
    function = FunctionSite.query.get_or_404(id)
    return render_template('function.html', function=function)

if __name__ == "__main__":
    app.run(debug=True)


import music21
import os
import json

# for fn in filenameList:
#     s = converter.parse(fn)
#     k = s.analyze('key')
#     i = interval.Interval(k.tonic, pitch.Pitch('C'))
#     sNew = s.transpose(i)

resultfile = 'Bachneural.txt'
majorFile = 'Bachmajor.txt'
minorFile = 'Bachminor.txt'

def readAllBach():
	print('starting to read bach')
	paths = os.listdir('/Users/jonathanadam/Documents/muscomm/finalproject/m21/bachmidi')
	paths = ['/Users/jonathanadam/Documents/muscomm/finalproject/m21/bachmidi/' + x for x in paths]
	annotatedBassChords = []
	annotatedMajorChords = []
	annotatedMinorChords = []
	with open(resultfile,'w') as output:

		for path in paths:
			print('reading '+ path)
			midData = music21.converter.parse(path).flat

			#Normalize to C - note we're not minoring majoring yet
			k = midData.analyze('key')
			if(k.mode == 'minor'):
				i = music21.interval.Interval(k.tonic, music21.pitch.Pitch('A'))
			else:
				i = music21.interval.Interval(k.tonic, music21.pitch.Pitch('C'))
			midData = midData.transpose(i)

			midData = midData.chordify()
			midData = midData.getElementsByClass(music21.chord.Chord)

			allChords = [''.join([chr(p.midi) for p in x.pitches]) for x in midData]

			# simplifiedBass = [chr(x.bass().midi) for x in midData]
			# simplifiedChords = [x.orderedPitchClassesString[1:-1] for x in midData]
			# simplifiedBass = [x.bass().name for x in midData]

			# simplifiedChords = [sorted(set(x.pitchClasses)) for x in midData]
			# simplifiedChords = [','.join([str(y) for y in x]) for x in simplifiedChords]

			# bassAndChords = [j for i in zip(simplifiedBass, simplifiedChords) for j in i]
			bassAndChords = allChords

			finalString = ' '.join(bassAndChords)
			finalString = finalString+ '\n'
			output.write(finalString)
			# print(finalString)
			# annotatedBassChords.append(finalString)
			# if (k.mode == 'minor'):
			# 	annotatedMinorChords.append(finalString)
			# if (k.mode == 'major'):
			# 	annotatedMajorChords.append(finalString)

		# annotatedMinorChords = [x+'\n' for x in annotatedMinorChords]
		# annotatedMajorChords = [x + '\n' for x in annotatedMajorChords]

	# with open(majorFile, 'w') as output:
	# 	output.writelines(annotatedMajorChords)
	# with open(minorFile, 'w') as output:
	# 	output.writelines(annotatedMinorChords)

def main():
	readAllBach()

if __name__ == '__main__':
	main()


# Метод

import numpy as np

def encrypt(text, key):
    w = h = len(text) / len(key)
    x = 0
    y = 0
    result = ""
    while x < h:
        result += text[key[x][y] - 1];
        y += 1
        if y > w - 1:
            y = 0
            x += 1
    return result

def decrypt(text, key):
    key = key.flatten()
    result = [' '] * len(key)
    i = 0
    for ch in key:
        result[ch - 1] = text[i]
        i += 1
    return ''.join(result)

key = [
    [2, 9, 4],
    [7, 5, 3],
    [6, 1, 8]
]
encrypted = encrypt('ВЕДОМОСТЬ', np.array(key))
print(encrypted)
decrypted = decrypt(encrypted, np.array(key))
print(decrypted)


from bs4 import BeautifulSoup as soup
import requests
from datetime import datetime
from datetime import timedelta
import re
import csv

def GetStations():
    StationList = "H:\\CrashWeatherData\\LISTOFSTATIONS.txt"
    f = open(StationList, "r")
    String = f.read()
    Stations = re.findall(r'\(\w+\)', String)
    Stations = [s.strip('(') for s in Stations]
    Stations = [s.strip(')') for s in Stations]

    StationsList = []
    for station in Stations:
        if station not in StationsList:
            StationsList.append(station)

    finallistUSED = []
    finallistTOTAL = []
    for item in StationsList:

        urlcreate = ('https://www.wunderground.com/weather/us/ga/atlanta/' + item)
        url = requests.get(urlcreate)
        souptext = soup(url.content, 'html.parser')
        location = []
        i = 0
        for subheading in souptext.findAll('span', {'class':'subheading'}):
            for strong in subheading.findAll('strong'):
                text = strong.text
                text = float(text)
                if i == 1 and ( 33.59 < text < 33.94):
                    if text*100 not in range(3373, 3388):
                        location.append(text)
                elif i == 2 and ( 84.53 < text < 84.28):
                    if text*100 not in range(8428, 8444):
                        location.append(text)
                elif i == 0:
                        location.append(text)

        if len(location) == 3:
            finallistTOTAL.append(item + '-' + str(location[0]) + '-' + str(location[1]) + '-' + str(location[2]))
            finallistUSED.append(item)

    return finallistUSED , finallistTOTAL

def GetDeltaDays(Currentdate):
    date_format = "%Y-%m-%d"
    d0 = datetime.strptime(Currentdate, date_format)
    d1 = datetime.strptime('2017-10-01', date_format)
    delta = d0 - d1
    delta = (delta.days)
    print(delta)
    return delta

# Generating table from https://www.wunderground.com/
def GetTable(errorSTAT, errorCOUNT, year, month, day, urlcreate):
    i = 0
    try:
        testitem = souptext.find('div', {'class':'scrollable'})
        tester = testitem.text
        for scrollable in souptext.findAll('div', {'class':'scrollable'}):
            for TD in scrollable.findAll('tr', {'class':'ng-star-inserted'}):
                lister = [str(year) + str(month) + str(day)]
                for item in TD:
                    i += 1
                    text = str(item.text)
                    if text.find(':') != -1:
                        t = re.split(':| ', text)
                        if (t[2] == 'PM') and (t[0] != '12'):
                            h = (int(t[0]) + 12)
                        elif (t[0] == '12') and (t[2] == 'AM'):
                            h = ''
                        elif (t[2] == 'PM') and (t[0] == '12'):
                            h = int(12)
                        else:
                            h = (int(t[0]))
                        text = str(h) + str(t[1])
                        lister.append(text)

                    else:
                        t = text.translate(str.maketrans({'F': '', '%': '', '-': '0', 'n': '', ' ': '0', 'i': ''}))
                        x = t.strip()
                        lister.append(x)
                # Remove last element
                lister.pop()
                csvwriter.writerow(lister)
                csvwriter.writerow(lister)
                csvwriter.writerow(lister)
    except:
        print('miss')
        errorSTAT = 1
        errorCOUNT += 1

    return errorSTAT, errorCOUNT

Currentdate = (datetime.now()).strftime('%Y-%m-%d')
delta = GetDeltaDays(Currentdate)
NewTime = (datetime.now() - timedelta(days=(delta)) ).strftime('%Y-%m-%d')
PATH = 'H:\\CrashWeatherData\\ALL\\'
PATH2 = 'H:\\CrashWeatherData\\DATA INFO\\'

finallistUSED , finallistTOTAL = GetStations()
print('CHECK')

with open(PATH2 + "Station-Latitude-Longitude" + '.csv', 'a', newline='') as csvINFO:
    f = csvINFO
    writer = csv.DictWriter(
           f, fieldnames=["Station", "Elevation ft", "Latitude", "Longitude" ])
    writer.writeheader()
    csvINFOMKR = csv.writer(csvINFO)
    print('check')
    for station in finallistTOTAL:
        newS = station.split('-')
        newS[3] = '-' + str(newS[3])
        csvINFOMKR.writerow(newS)
    print('final')

# Total list of relevant weather stations
finallistUSED =  [
    'KGAATLAN330', 'KGAATLAN474', 'KGAATLAN432', 'KGAATLAN562', 'KGAATLAN420',
    'KGAATLAN94', 'KGADECAT57', 'KGAATLAN276', 'KGAATLAN606', 'KGAAVOND3', 'KGADECAT68',
    'KGADECAT7', 'KGAATLAN467', 'KGAATLAN431', 'KGAATLAN552', 'KGAATLAN557', 'KGANORTH4',
    'KGAFORES2', 'KGAATLAN586', 'KGAATLAN487', 'KGAATLAN378', 'KGAATLAN578', 'KGAATLAN46',
    'KGASMYRN23', 'KGATUCKE23', 'KGASMYRN21', 'KGAATLAN528', 'KGAATLAN331', 'KGAATLAN25',
    'KGALAKEC4', 'KGAMABLE2', 'KGAATLAN585', 'KGAATLAN536', 'KGASMYRN53', 'KGADUNWO9',
    'KGAMOUNT7', 'KGASIBLE2', 'KGAATLAN85', 'KGASMYRN34', 'KGASTOCK15', 'KGAATLAN16',
    'KGAATLAN525', 'KGAATLAN573', 'KGAATLAN444', 'KGAATLAN80', 'KGAATLAN449', 'KGADECAT59',
     'KGAATLAN473', 'KGAATLAN489', 'KGAATLAN393', 'KGAATLAN427', 'KGAATLAN405', 'KGAATLAN351',
      'KGADECAT47', 'KGAATLAN326', 'KGAAVOND8', 'KGAATLAN195', 'KGAATLAN495', 'KGAATLAN338',
       'KGAATLAN501', 'KGATUCKE26', 'KGAATLAN537', 'KGAOAKSO2', 'KGASMYRN57', 'KGAATLAN441',
        'KGAATLAN340', 'KGAATLAN607', 'KGAATLAN460', 'KGAATLAN361', 'KGATUCKE14', 'KGAATLAN379',
         'KGADECAT15', 'KGAATLAN592', 'KGAATLAN390', 'KGATUCKE28', 'KGADECAT72', 'KGAATLAN529',
          'KGATUCKE4', 'KGAATLAN115', 'KGAATLAN125', 'KGAATLAN497', 'KGAATLAN448']

FIN = 0

for Station in finallistUSED:
   errorCOUNT = 0
   errorSTAT = 0
   with open(PATH + Station + 'WeatherScraper' + '.csv', 'w', newline='') as csvfile:
       csvwriter = csv.writer(csvfile)

       while NewTime != '2018-06-20':

           NewTime = (datetime.now() - timedelta(days=(delta)) ).strftime('%Y-%m-%d')
           print(NewTime)
           NewTimeSP = NewTime.split('-')
           year = NewTimeSP[0]
           month = NewTimeSP[1]
           day = NewTimeSP[2]

           urlcreate = ('https://www.wunderground.com/dashboard/pws/' + Station + '/table/' + NewTime + '/' + NewTime + '/daily')
           delta -= 1

           url = requests.get(urlcreate)
           souptext = soup(url.content, 'html.parser')
           lib = 'lib-display-unit'

           errorSTAT, errorCOUNT = GetTable(errorSTAT, errorCOUNT, year, month, day, urlcreate)

           if errorSTAT == 1:
               with open(PATH + Station + 'ErrorReport' + '.csv', 'a', newline='') as csvError:
                       csvErrorReport = csv.writer(csvError)
                       csvErrorReport.writerow([finallistUSED[FIN]])
                       csvErrorReport.writerow(['URL: ', urlcreate])
                       csvErrorReport.writerow(['Missing Day!: ',  str(year) + str(month) + str(day)])
                       errorSTAT = 0

           if errorCOUNT >= 25:
               with open(PATH + Station + 'ErrorReport' + '.csv', 'a', newline='') as csvError:
                   csvErrorReport = csv.writer(csvError)
                   csvErrorReport.writerow(['ERROR LIMIT EXCEEDED'])
               NewTime = '2018-06-20'

   delta = GetDeltaDays(Currentdate)
   NewTime = (datetime.now() - timedelta(days=(delta)) ).strftime('%Y-%m-%d')
   FIN += 1

   csvfile.close()
   csvError.close()


import os
from collections import (
    namedtuple,
    OrderedDict
)
import pysam
from edlib import align

gff_record = namedtuple('GFF_record', 'seqid source type start end score strand phase attributes')
comparison = namedtuple('Comparison', 'putative_event treatment_events control_events')

SCAN_SOFT_CLIP_REGION = 15

def get_tabix_file(path, preset='gff'):
    """Return a TabixFile instance for a file at `path`."""
    if not os.path.exists("%s.gz.tbi" % path):
        pysam.tabix_index(path, preset=preset, keep_original=True)
    tabix_file = pysam.TabixFile("%s.gz" % path)
    return tabix_file

def to_gff_attributes(s):
    """Convert a GFF attribute string to an OrderedDict."""
    attr_d = OrderedDict()
    attributes = s.split(';')
    for attribute in attributes:
        k, v = attribute.split('=')
        if ',' in v:
            v = v.split(',')
        attr_d[k] = v
    return attr_d

def to_gff_record(r):
    """Convert a string representation of a GFF record to a named tuple."""
    fields = r.split('\t')
    fields[3], fields[4] = int(fields[3]), int(fields[4])
    fields[8] = to_gff_attributes(fields[8])
    return gff_record(*fields)

def gff_record_to_string(gff_record):
    """Convert a `GFF_record` namedtuple to a string representation."""
    fields = list(gff_record)
    fields[3], fields[4] = str(fields[3]), str(fields[4])
    attributes = []
    for k, v in fields[8].items():
        if isinstance(v, list):
            v = ",".join(v)
        attributes.append("%s=%s" % (k, v))
    fields[8] = ";".join(attributes)
    return "%s\n" % "\t".join(fields)

def write_gff_records(records, path):
    """Write GFF_record in records iterable to `path`."""
    with open(path, 'w') as out:
        for r in records:
            out.write(gff_record_to_string(r))

def fetch_records(tabixfile, region):
    """Wrap TabixFile.fetch in try/except clause to ignore region errors."""
    try:
        return tabixfile.fetch(reference=region.seqid,
                               start=int(region.start) - SCAN_SOFT_CLIP_REGION,
                               end=int(region.end) + SCAN_SOFT_CLIP_REGION)

    except ValueError:
        # Happens if region is not present in tabix file, that's fine and expected
        return []

def fill_comparison(putative, controls, treatment):
    """Convert tabix record to a Comparison named tuple."""
    for putative_event in putative.fetch():
        putative_record = to_gff_record(putative_event)
        putative_record_complements = [to_gff_record(r) for r in fetch_records(treatment, putative_record)]
        control_records = []
        for control in controls:
            for r in fetch_records(control, putative_record):
                control_records.append(to_gff_record(r))
        yield (comparison(putative_record, putative_record_complements, control_records))

def annotate_with_overlapping_insertions(putative_record, control_records):
    """If overlapping insertion of the same type is found list them in the overlaps attribute."""
    putative_insert_reference = putative_record.attributes.get('insert_reference_name')
    overlapping_ids = []
    for control_record in control_records:
        if control_record.source == 'findcluster' and control_record.attributes.get('insert_reference_name') == putative_insert_reference:
            overlapping_ids.append(control_record.attributes.get('ID'))
    putative_record.attributes['overlaps'] = ",".join(overlapping_ids)
    return putative_record

def filter_putative_insertions(putative, treatment, controls, min_length, output_discarded_records=True):
    """Remove insertions that are based on clipped sequences which are also present in the control."""
    for (putative_record, putative_complements, control_records) in fill_comparison(putative, controls, treatment):
        valid_record = True
        clips = []
        control_clips = []
        putative_record = annotate_with_overlapping_insertions(putative_record, control_records)
        softclip_clusters = putative_record.attributes.get('softclip_clusters')
        if not softclip_clusters:
            yield putative_record
            continue
        for putative_complement in putative_complements:
            if putative_complement.type in ('3p_clip', '5p_clip') and putative_complement.attributes['ID'] in softclip_clusters:
                # We only verify clipped sequences that are part of the insertion to verify
                clips.append(putative_complement)
        for control_record in control_records:
            if control_record.type in ('3p_clip', '5p_clip'):
                control_clips.append(control_record)
        for c in control_clips:
            for t in clips:
                if c.type == t.type:
                    if abs(c.start - t.start) < SCAN_SOFT_CLIP_REGION or abs(c.end - t.end) < SCAN_SOFT_CLIP_REGION:
                        c_seq = c.attributes.get('consensus')
                        t_seq = t.attributes.get('consensus')
                        if sequences_match(seq1=c_seq, seq2=t_seq, compare=c.type, min_length=min_length):
                            valid_record = False
                            break
            if valid_record is False:
                break
        if valid_record:
            yield putative_record
        elif output_discarded_records:
            putative_record.attributes['FAIL'] = 'clip_seq_matches_%s' % c.attributes.get('ID')
            yield putative_record

def sequences_match(seq1, seq2, compare='5p_clip', min_length=4):
    """
    Compare 2 sequences and determine whether they are likely the same.

    >>> s1, s2 = 'AAAAAAAAAA', 'AAAAAAAAATGC'
    >>> sequences_match(s1, s2, compare='3p_clip')
    True
    >>> sequences_match('TTGAAAAAAA', s2, compare='3p_clip')
    False
    >>> sequences_match(s1[::-1], s2[::-1], compare='5p_clip')
    True
    >>> sequences_match('A', 'A')
    False
    """
    if compare == '3p_clip':
        seq1 = seq1[:10]
        seq2 = seq2[:10]
    else:
        seq1 = seq1[-10:]
        seq2 = seq2[-10:]
    if len(seq1) > min_length and len(seq2) > min_length:
        if len(seq1) >= 10 and len(seq2) >= 10:
            if align(seq1, seq2)['editDistance'] < 2:
                return True
        elif compare == '3p_clip':
            return seq1.startswith(seq2) or seq2.startswith(seq1)
        else:
            return seq1.endswith(seq2) or seq2.endswith(seq1)
    return False

def confirm_insertions(putative_insertions_path, all_treatments_path, all_controls_paths, output_path, min_length=4, output_discarded_records=True):
    """Confirm putative insertions by absence of softclipping patterns in a somatic control file."""
    putative = get_tabix_file(putative_insertions_path)
    treatment = get_tabix_file(all_treatments_path)
    controls = [get_tabix_file(p) for p in all_controls_paths if not p == all_treatments_path]
    gff_record_iterator = filter_putative_insertions(putative=putative,
                                                     treatment=treatment,
                                                     controls=controls,
                                                     min_length=min_length,
                                                     output_discarded_records=output_discarded_records)
    write_gff_records(gff_record_iterator, path=output_path)


#!/usr/bin/python3

"""
Print all commits by a user
"""

if __name__ == "__main__":
    import sys
    import requests

    repo = sys.argv[1]
    owner = sys.argv[2]
    url = f"https://api.github.com/repos/{owner}/{repo}/commits"

    r = requests.get(url)
    r = r.json()
    for i in r[:10]:
        print("{}: ".format(i.get('sha')), end="")
        print("{}".format(i.get('commit').get('author').get('name')))


from django.urls import path,include
from rest_framework.authtoken.views import ObtainAuthToken

from rest_framework_simplejwt.views import (
    TokenObtainPairView,
    TokenRefreshView,
)

from . import views
from rest_framework.routers import DefaultRouter

router = DefaultRouter()

app_name = 'api'
urlpatterns = [

    #Function views
    #path('', name='home'),
    # path('parts/',views.allparts,name='allparts'),
    # path('parts/<str:pk>/',views.partdetail,name='partdetail'),
    # path('suppliers/',views.allsuppliers,name='allsuppliers'),
    # path('suppliers/<int:pk>/',views.supplierdetail,name='supplierdetail'),

    #Class API views

    path('',include(router.urls)),
    path('djangotoken/',ObtainAuthToken.as_view(),name='authview'),   # Django Token
    path('jwtoken/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
    path('jwtoken/refresh/', TokenRefreshView.as_view(), name='token_refresh'),

    path('parts/',views.AllpartsAPIView.as_view(),name='allparts'),
    path('parts/<str:pk>/',views.PartdetailAPIView.as_view(),name='partdetail'),
    path('suppliers/',views.AllsuppliersAPIView.as_view(),name='allsuppliers'),
    path('suppliers/<int:id>/',views.SupplierdetailAPIView.as_view(),name='supplierdetail'),  #custom Class-based views require actual fiield name here, not PK

    #Generic Views

    # path('parts/',views.PartsListAPIView.as_view(),name='allparts'),
    # path('parts/<str:pk>/',views.RetrieveUpdateDeletePart.as_view(),name='partdetail'),
    # path('suppliers/',views.SuppliersListAPIView.as_view(),name='allsuppliers'),
    # path('suppliers/<int:pk>/',views.RetrieveUpdateDeleteSupplier.as_view(),name='supplierdetail'),

]

 ######  Routers


import matplotlib.pyplot as py
from typing import List

"""
Created on Wed Oct 27 16:43:57 2021
Author: Jamison
Integration / Edits: Emerson
"""

'''
NOTES:
Haven't tested this on enough input files yet (I would guess it will not
catch all of the rise/fall times and will miss some)
TO DO's
    1) fit equation to rise/fall
    2) modify script to do this for each rise/fall automatically and produce an
    average fit for each file/vehicle
'''

def is_number(s):
    try:
        float(s)
        return True
    except ValueError:
        return False

def find_rise(ch, t):  # using ch2 for ch give indices of rise (ch1 gives fall)
    start = 0
    end = 0
    dV = ch[0:-1]
    for i in range(1, len(t) - 1, 1):
        dV[i - 1] = (ch[i] - ch[i - 1]) / (t[i] - t[i - 1])
    print('Calculated Derivatives\n')
    i = 0
    d_3 = 0
    d_2 = 0
    d_1 = 0
    while (1):
        if (dV[i] > 0):
            d = 1
        else:
            d = 0
        if (d * d_1 * d_2 * d_3 > 0):  # the last four points have been rising
            print('Found beginning of rise\n')
            start = i - 20
            if start < 0:
                start = 0
            while (1):
                d_3 = d_2
                d_2 = d_1
                d_1 = d
                if (dV[i] == 0):
                    d = 0
                else:
                    d = 1
                if (d_3 * d_2 * d_1 * d == 0):  # the last 4 points have been the same
                    print('Found end of rise\n')
                    end = i + 20
                    if end > len(dV) - 1:
                        end = len(dV) - 1
                    break
                i = i + 1
                if (i > len(dV) - 1):
                    break
            break
        d_3 = d_2
        d_2 = d_1
        d_1 = d
        i = i + 1
        if (i > len(dV) - 1):
            break
    return (start, end)

def rise_time_plot_data(input_data: List[dict]):
    time = [x['timestamp'] for x in input_data]
    ch1 = [x['ch1_v'] for x in input_data]
    ch2 = [x['ch2_v'] for x in input_data]

    (start, end) = find_rise(ch2, time)  # change to ch1 for fall
    print("start = ", start)
    print("end = ", end)

    py.subplot(211)
    py.plot(time[start:end], ch2[start:end], 'r.', label='canH')
    py.legend(loc='center right')
    py.subplot(212)
    py.plot(time[start:end], ch1[start:end], 'g.', label='canL')
    py.legend(loc='center right')
    py.show()


#!/usr/bin/python
# -*- coding: utf-8; -*-
#

import re

from HTMLParser import HTMLParser

from lxml import etree
from StringIO import StringIO
from lxml.html import fromstring

class TravianParser:

    def __init__(self):

        self.res = re.compile('([0-9]+)/([0-9]+)	')
        self.build = re.compile('build.php\?id=([0-9]+)')
        self.profile = re.compile('spieler.php\?uid=([0-9]+)')
        self.building = re.compile('([a-zA-Z ]+) level ([0-9]+)')
        self.movements = re.compile("([0-9]+)([a-zA-Z.]+)in([0-9\:]+)Hrs")
        self.coords = re.compile("\(([0-9\-]+)\|([0-9\-]+)\)")

        self.parser = etree.HTMLParser()

    def parse(self, html):
        #return etree.parse(StringIO(html), self.parser)
        return fromstring(html)

    def getRallyPoint(self, doc):

        persons = {}

        my = {}
        renforcement = {}
        prisoners = {}

        for table in doc.cssselect("table.troop_details"):

            village, owner = [a.text_content() for a in table.cssselect("thead td a")]
            tr = [tr for tr in table.cssselect('tbody.units tr')]
            names = [img.get("title") for img in tr[0].cssselect('td img')]
            units = [td.text_content() for td in tr[1].iter(tag="td")]

            troops = {}

            for name, number in zip(names, units):
                if name not in troops:
                    troops[name] = int(number)

            if owner == "Own troops":
                my[village] = troops
            elif "Troops imprisoned from" in owner:
                owner = owner.replace("Troops imprisoned from ", "").strip()
                if not owner in prisoners:
                    prisoners[owner] = {}

                prisoners[owner][village] = troops
            else:
                owner = owner.replace("'s troops", "").strip()
                if not owner in renforcement:
                    renforcement[owner] = {}

                renforcement[owner][village] = troops

        return [my, renforcement, prisoners]

    # spieler.php?uid=xxx ONLY !
    def getVillages(self, doc):
        villages = []

        table = doc.get_element_by_id("villages")
        for tr in table.iter(tag="tr"):
            name, coords, population = "", "", ""
            for td in tr.iter(tag="td"):
                if "class" in td.attrib:
                    cl = td.get("class")

                    if cl == "nam":
                        name = td.text_content()

                    elif cl == "hab":
                        population = td.text_content()
                    elif cl == "aligned_coords":
                        text = td.text_content().replace("\n", "").replace("\t", "")
                        coords = self.coords.findall(text)[0]

            if name and population and coords:
                villages.append([name, population, coords])

        return villages

    # ALL PAGES !
    def getProfileUid(self, doc):
        uid = 0

        div = doc.get_element_by_id("side_navi")
        for a in div.iter(tag="a"):
            href = a.get("href")

            if self.profile.match(href):
                uid = self.profile.findall(href)[0]

        return uid

    ### ALL PAGES !
    def getRes(self, doc):

        res = []

        div_res = doc.get_element_by_id("resWrap")
        for td in div_res.iter(tag='td'):
            title = td.get('title')
            id = td.get('id')
            text = td.text_content()

            if id in ["l1", "l2", "l3", "l4"]:
                current, capacity = text.split("/")
                res.append([id, int(title), int(current), int(capacity)])
                #self.Village.resources.update(id, production=int(title), value=int(current), capacity=int(capacity))

        return res

    ### dorf1.php ONLY !
    def getTroops(self, doc):
        troops = []
        try:
            div_troops = doc.get_element_by_id("troops")
            for tr in div_troops.iter(tag="tr"):
                unit_name, unit_number = "", ""
                for td in tr.iter(tag="td"):
                    if "class" in td.attrib:
                        cl = td.get("class")
                        if cl == "num":
                            unit_number = td.text_content()
                        elif cl == "un":
                            unit_name = td.text_content()

                    if unit_name and unit_number:
                        troops.append([unit_name, unit_number])

        except:
            return []
        else:
            return troops

    ### dorf1.php ONLY !
    def getFields(self, doc):
        fields = []

        div = doc.get_element_by_id("rx")
        for area in div.iter(tag='area'):
            title = area.get("title")
            href = area.get("href")
            farm, level, id = "", 0, 0

            if self.building.match(title):
                farm, level = self.building.findall(title)[0]
            if self.build.match(href):
                id = self.build.findall(href)[0]

            if farm and level and id:
                fields.append([id, farm, level])

        return fields

    ### dorf1.php ONLY !
    def getMovements(self, doc):

        try:
            table = doc.get_element_by_id("movements")
        except KeyError:
            return []
        else:
            move = []
            for tr in table.iter(tag="tr"):
                href, type, time, number = "", "", "", 0

                for link in tr.iter(tag="a"):
                    if "href" in link.attrib:
                        href = link.get("href")

                for td in tr.iter(tag="td"):
                    text = str(td.text_content()).encode('ascii', 'ignore')
                    if len(text) > 2:
                        if self.movements.match(text):
                            number, type, time = self.movements.findall(text)[0]
                            if type == "Reinf.":
                                type = "reinforcing"
                            move.append([type, number, time, href])

            return move

    ### dorf2.php ONLY !
    def getVillageBuildings(self, doc):
        buildings = []

        map = doc.get_element_by_id("map2")
        for area in map.iter(tag='area'):
            title = area.get('title')
            href = area.get('href')
            build_id, name, level, = "", "", 0

            if title and href:
                if self.build.match(href):
                    build_id = self.build.findall(href)[0]

                if self.building.match(title):
                    name, level = self.building.findall(title)[0]

                buildings.append([build_id, name, level])
                #self.Village.updateBuilding(build_id, name=name, level=level)

        return buildings

    ### ALL PAGES !
    def getVillageName(self, doc):
        name = ""

        for h1 in doc.iter(tag='h1'):
            title = h1.text_content()
            if title:
                name = title

        return name

    ### ALL PAGES !
    def getServerTime(self, doc):
        server_time = ""
        calculated = ""
        div_time = doc.get_element_by_id("ltimeWrap")
        tmp = div_time.text_content()

        for line in tmp.split("\n"):
            line = line.strip()
            if line is not "":
                if "Server time" in line:
                    server_time = line
                elif "am" in line or "pm" in line:
                    server_time += " %s" % line
                elif "Calculated" in line:
                    calculated = line

        return [server_time, calculated]