"""
This file includes functions for manipulating and accessing the Appointment table in the MAPSDB database.
This table keeps track of the appointments that patients make with doctors
"""

from flask import Flask, request
import MapsDB

app = Flask(__name__)
db, ma = MapsDB.connect_db(app)

class Appointment(db.Model):
    """
    This class defines the data inside the appointment table. Columns include:
    id as a primary key, start_datetime, doctor_id, patient_id and medical_note.
    """
    id = db.Column(db.Integer, db.Sequence(
        'seq_reg_id', start=1, increment=1), primary_key=True)
    start_datetime = db.Column(db.DATETIME, unique=False)
    doctor_id = db.Column(db.Integer, unique=False)
    patient_id = db.Column(db.Integer, unique=False)
    medical_note = db.Column(db.String(400), unique=False, nullable=True)

    def __init__(self, start_datetime, doctor_id, patient_id, medical_note):
        """
        This function initialises the Appointment table.
        """
        self.start_datetime = start_datetime
        self.doctor_id = doctor_id
        self.patient_id = patient_id
        self.medical_note = medical_note

class AppointmentSchema(ma.Schema):
    """
    This function describes the schema for the Appointment table.
    """
    class Meta:
        # Fields to expose
        fields = ('id', 'start_datetime', 'doctor_id',
                  'patient_id', 'medical_note')

appointment_schema = AppointmentSchema()
appointments_schema = AppointmentSchema(many=True)

def add_appointment():
    """
    This function does not accept any parameters and will add an appointment to the Appoinments table
    getting the appointment information from a HTML form.
    """
    app = request.form['app_slot']
    start_datetime = app.split(',')[1]
    start_datetime = start_datetime.split('+')[0]
    doctor_id = app.split(',')[3]
    patient_id = app.split(',')[4]

    new_appointment = Appointment(start_datetime, doctor_id, patient_id, None)

    db.session.add(new_appointment)
    db.session.flush()
    db.session.commit()

def remove_appointment(start_datetime, doctor_id):
    """
    This function accepts an appointment id and will delete the appointment from the Appointment table.
    """
    appointment = Appointment.query.filter(Appointment.start_datetime.like(start_datetime),
                                            Appointment.doctor_id.like(doctor_id)
                                            ).first()
    db.session.delete(appointment)
    db.session.commit()

def add_medical_note(app_dt, doctor_id, medical_note):
    """
    This function accepts an appointment date, doctor id and a medical note and will add the supplied
    medical note the the Appointment table, using the appointment data and doctor id as keys to
    query the Appointment table.
    """

    app = Appointment.query.filter(Appointment.doctor_id.like(
        doctor_id), Appointment.start_datetime.like(app_dt)).first()
    app.medical_note = medical_note
    db.session.commit()


from django.contrib.auth.decorators import login_required, user_passes_test
from django.http import Http404
from django.shortcuts import render, get_object_or_404
from BTsuperintendent.user_access_test import grades_threshold_access, grades_threshold_status_access
from BTfaculty.models import BTGradesThreshold, BTMarks_Staging
from BThod.models import BTFaculty_user, BTCoordinator
from BTco_ordinator.models import BTFacultyAssignment, BTSubjects
from BTsuperintendent.models import BTGradePoints, BTHOD, BTCycleCoordinator
from BTfaculty.forms import GradeThresholdForm, GradeThresholdStatusForm
from ADUGDB.models import BTRegistrationStatus
from json import dumps
import statistics as stat
from import_export.formats.base_formats import XLSX
import pandas as pd

@login_required(login_url="/login/")
@user_passes_test(grades_threshold_access)
def grades_threshold(request):
    user = request.user
    groups = user.groups.all().values_list('name', flat=True)
    faculty = BTFaculty_user.objects.filter(User=user, RevokeDate__isnull=True).first()
    subjects = BTFacultyAssignment.objects.filter(RegEventId__Status=1, RegEventId__GradeStatus=1, Coordinator=faculty.Faculty).distinct('Subject','RegEventId_id')
    if not subjects:
        raise Http404('You are not allowed to add threshold marks')
    return render(request, 'BTfaculty/GradesThreshold.html', {'subjects': subjects})

@login_required(login_url="/login/")
@user_passes_test(grades_threshold_access)
def grades_threshold_assign(request, pk):
    subject_faculty = get_object_or_404(BTFacultyAssignment, id=pk)
    subject = subject_faculty.Subject
    grades = BTGradePoints.objects.filter(Regulation=subject.RegEventId.Regulation).exclude(Grade__in=['I', 'X', 'R','W'])
    prev_thresholds = BTGradesThreshold.objects.filter(Subject=subject, RegEventId=subject_faculty.RegEventId)
    marks = BTMarks_Staging.objects.filter(Registration__RegEventId=subject_faculty.RegEventId.id, Registration__sub_id=subject.id)
    marks_list = marks.values_list('TotalMarks', flat=True)
    mean = round(stat.mean(marks_list), 2)
    stdev = round(stat.stdev(marks_list), 2)
    maximum = max(marks_list)
    if request.method == 'POST':
        form = GradeThresholdForm(subject_faculty, request.POST)
        if request.POST.get('submit-form'):
            if form.is_valid():
                if prev_thresholds:
                    # if (form.cleaned_data.get('uniform_grading')=='1' and prev_thresholds.first().uniform_grading) or \
                    #     (form.cleaned_data.get('uniform_grading')=='0' and not prev_thresholds.first().uniform_grading):
                    #     if form.cleaned_data.get('uniform_grading')=='1':
                    for grade in grades:
                        if form.cleaned_data[str(grade.id)]:
                            prev_thresholds.filter(Grade=grade, Exam_Mode=False).update(Threshold_Mark=float(form.cleaned_data[str(grade.id)]))

                    exam_mode_grade = grades.filter(Grade__in=['P','F'])
                    for grade in exam_mode_grade:
                        if form.cleaned_data[str('exam_mode_')+str(grade.id)]:
                            prev_thresholds.filter(Grade=grade, Exam_Mode=True).update(Threshold_Mark=float(form.cleaned_data[str('exam_mode_')+str(grade.id)]))
                        # else:
                        #     section = form.cleaned_data.get('section')
                        #     for grade in grades:
                        #         if form.cleaned_data[section+str(grade.id)]:
                        #             prev_thresholds.filter(Grade=grade, Section=section).update(Threshold_Mark=int(form.cleaned_data[section+str(grade.id)]))
                    # else:
                    #     prev_thresholds.delete()
                    #     if form.cleaned_data.get('uniform_grading')=='1':
                    #         for grade in grades:
                    #             if form.cleaned_data[str(grade.id)]:
                    #                 threshold_mark = GradesThreshold(Grade=grade, Subject=subject, RegEventId=subject_faculty.RegEventId, \
                    #                     uniform_grading=True, Threshold_Mark=int(form.cleaned_data[str(grade.id)]))
                    #                 threshold_mark.save()
                    #     else:
                    #         section = form.cleaned_data.get('section')
                    #         for grade in grades:
                    #             if form.cleaned_data[section+str(grade.id)]:
                    #                 threshold_mark = GradesThreshold(Grade=grade, Subject=subject, RegEventId=subject_faculty.RegEventId, \
                    #                     uniform_grading=False, Section=section, \
                    #                         Threshold_Mark=int(form.cleaned_data[section+str(grade.id)]))
                    #                 threshold_mark.save()
                else:
                    # if form.cleaned_data.get('uniform_grading')=='1':
                    for grade in grades:
                        if form.cleaned_data[str(grade.id)]:
                            threshold_mark = BTGradesThreshold(Grade=grade, Subject=subject, RegEventId=subject_faculty.RegEventId, \
                                Threshold_Mark=float(form.cleaned_data[str(grade.id)]), Exam_Mode=False)
                            threshold_mark.save()
                    exam_mode_grade = grades.filter(Grade__in=['P','F'])
                    for grade in exam_mode_grade:
                        if form.cleaned_data[str('exam_mode_')+str(grade.id)]:
                            threshold_mark = BTGradesThreshold(Grade=grade, Subject=subject, RegEventId=subject_faculty.RegEventId, \
                                    Threshold_Mark=float(form.cleaned_data[str('exam_mode_')+str(grade.id)]), Exam_Mode=True)
                            threshold_mark.save()

                    # else:
                    #     section = form.cleaned_data.get('section')
                    #     for grade in grades:
                    #         if form.cleaned_data[section+str(grade.id)]:
                    #             threshold_mark = GradesThreshold(Grade=grade, Subject=subject, RegEventId=subject_faculty.RegEventId, \
                    #                 uniform_grading=False, Section=section, \
                    #                     Threshold_Mark=int(form.cleaned_data[section+str(grade.id)]))
                    #             threshold_mark.save()
                msg = 'Grades Threshold noted successfully'
                return render(request, 'BTfaculty/GradesThresholdAssign.html', {'subject':subject_faculty, 'msg':msg})
        else:
            grades_data = [grade.Grade for grade in grades]
            no_of_students = {grade.Grade:0 for grade in grades}
            for index, grade in enumerate(grades):
                if request.POST.get(str(grade.id)):
                    no_of_students[grades_data[index]] = len(list(mark for mark in marks_list if mark>=float(request.POST.get(str(grade.id)))))
            for index in range(len(grades)-1, 0, -1):
                no_of_students[grades_data[index]] = no_of_students[grades_data[index]] - no_of_students[grades_data[index-1]]
            no_of_students = dumps(no_of_students)
            return render(request, 'BTfaculty/GradesThresholdAssign.html', {'subject':subject_faculty, 'form':form, 'students':no_of_students, 'mean':mean, 'stdev':stdev, 'max':maximum})

    else:
        form = GradeThresholdForm(subject_faculty)
        no_of_students = None
        if prev_thresholds:
            grades_data = [grade.Grade for grade in grades]
            no_of_students = {grade.Grade:0 for grade in grades}
            for index, grade in enumerate(grades):
                if prev_thresholds.filter(Grade=grade.id):
                    threshold_mark = prev_thresholds.filter(Grade=grade.id).first().Threshold_Mark
                    no_of_students[grades_data[index]] = len(list(mark for mark in marks_list if mark>=threshold_mark))
            total = 0
            for index in range(len(grades)-1, 0, -1):
                no_of_students[grades_data[index]] = no_of_students[grades_data[index]] - no_of_students[grades_data[index-1]]
                total += no_of_students[grades_data[index]]
            no_of_students = dumps(no_of_students)
    return render(request, 'BTfaculty/GradesThresholdAssign.html', {'subject':subject_faculty, 'form':form,  'students':no_of_students, 'mean':mean, 'stdev':stdev, 'max':maximum})

@login_required(login_url="/login/")
@user_passes_test(grades_threshold_status_access)
def grades_threshold_status(request):
    user = request.user
    groups = user.groups.all().values_list('name', flat=True)
    subjects = None
    if 'Faculty' in groups:
        faculty = BTFaculty_user.objects.filter(User=user, RevokeDate__isnull=True).first()
        subjects = BTFacultyAssignment.objects.filter(Faculty=faculty.Faculty, RegEventId__Status=1).distinct('Subject','RegEventId_id')
    elif 'Superintendent' in groups or 'Associate-Dean' in groups:
        subjects = BTFacultyAssignment.objects.filter(RegEventId__Status=1).distinct('Subject','RegEventId_id')
    elif 'Co-ordinator' in groups:
        co_ordinator = BTCoordinator.objects.filter(User=user, RevokeDate__isnull=True).first()
        subjects = BTFacultyAssignment.objects.filter(Faculty__Dept=co_ordinator.Dept, RegEventId__Status=1).distinct('Subject','RegEventId_id')
    elif 'HOD' in groups:
        hod = BTHOD.objects.filter(User=user, RevokeDate__isnull=True).first()
        subjects = BTFacultyAssignment.objects.filter(Faculty__Dept=hod.Dept, RegEventId__Status=1).distinct('Subject','RegEventId_id')
    elif 'Cycle-Co-ordinator' in groups:
        cycle_cord = BTCycleCoordinator.objects.filter(User=user, RevokeDate__isnull=True).first()
        subjects = BTFacultyAssignment.objects.filter(RegEventId__Dept=cycle_cord.Cycle, RegEventId__BYear=1, RegEventId__Status=1).distinct('Subject','RegEventId_id')
    else:
        raise Http404('You are not allowed to view threshold marks')
    if request.method == 'POST':
        form = GradeThresholdStatusForm(subjects, request.POST)
        if form.is_valid():
            subject = request.POST['subject'].split(':')[0]
            regEvent = request.POST['subject'].split(':')[1]
            thresholds = BTGradesThreshold.objects.filter(Subject_id=subject, RegEventId_id=regEvent)
            return render(request, 'BTfaculty/GradesThresholdStatus.html', {'form':form, 'thresholds':thresholds})
    else:
        form = GradeThresholdStatusForm(subjects=subjects)
    return render(request, 'BTfaculty/GradesThresholdStatus.html', {'form':form})

def add_grades_threshold(file):
    file = pd.read_excel(file)
    error_rows=[]
    for rIndex, row in file.iterrows():
        print(row)
        fac_assign_objs = BTFacultyAssignment.objects.filter(RegEventId__Status=1, RegEventId__GradeStatus=1, RegEventId__AYear=row[0], RegEventId__ASem=row[1], RegEventId__BYear=row[2], RegEventId__BSem=row[3], \
            RegEventId__Dept=row[4], RegEventId__Regulation=row[5], RegEventId__Mode=row[6], Subject__SubCode=row[7])
        for fac_assign_obj in fac_assign_objs:
            study_grades = BTGradePoints.objects.filter(Regulation=fac_assign_obj.Subject.RegEventId.Regulation).exclude(Grade__in=['I', 'X', 'R','W'])
            index = 8
            for grade in study_grades:
                if row[index] >= 0:
                    if not BTGradesThreshold.objects.filter(Subject=fac_assign_obj.Subject, RegEventId=fac_assign_obj.RegEventId, Grade=grade, Exam_Mode=False).exists():
                        grades_threshold_row = BTGradesThreshold(Subject=fac_assign_obj.Subject, RegEventId=fac_assign_obj.RegEventId, Grade=grade, Threshold_Mark=row[index], Exam_Mode=False)
                        grades_threshold_row.save()
                    else:
                        BTGradesThreshold.objects.filter(Subject=fac_assign_obj.Subject, RegEventId=fac_assign_obj.RegEventId, Grade=grade, Exam_Mode=False).update(Threshold_Mark=row[index])
                else:
                    error_rows.append(row)
                index += 1

            exam_grades = BTGradePoints.objects.filter(Regulation=fac_assign_obj.Subject.RegEventId.Regulation, Grade__in=['P','F'])
            if fac_assign_obj.Subject.RegEventId.Regulation == 1:
                p_threshold = 15
                f_threshold = 0
            else:
                p_threshold = 17.5
                f_threshold = 0
            if not BTGradesThreshold.objects.filter(Subject=fac_assign_obj.Subject, RegEventId=fac_assign_obj.RegEventId, Grade=exam_grades.filter(Grade='P').first(), Exam_Mode=True).exists():
                grades_threshold_row = BTGradesThreshold(Subject=fac_assign_obj.Subject, RegEventId=fac_assign_obj.RegEventId, Grade=exam_grades.filter(Grade='P').first(), Threshold_Mark=p_threshold, Exam_Mode=True)
                grades_threshold_row.save()
            if not BTGradesThreshold.objects.filter(Subject=fac_assign_obj.Subject, RegEventId=fac_assign_obj.RegEventId, Grade=exam_grades.filter(Grade='F').first(), Exam_Mode=True).exists():
                grades_threshold_row = BTGradesThreshold(Subject=fac_assign_obj.Subject, RegEventId=fac_assign_obj.RegEventId, Grade=exam_grades.filter(Grade='F').first(), Threshold_Mark=f_threshold, Exam_Mode=True)
                grades_threshold_row.save()
    print("Errors")
    print(error_rows)
    return "Completed!!"


import click as cli
import wandb
import yaml

from itertools import accumulate

ALL_METRICS = {True: ["hdf95/1", "hdf95/2", "masd/1", "masd/2", "dice", "dice/1", "dice/2",
                      "acc/1", "acc/2", "prec/1", "prec/2", "rec/1", "rec/2"],
               False: ["hdf95/1", "masd/1", "dice", "acc/1", "prec/1", "rec/1"]}
CORE_METRICS = {True: ["hdf95/1", "hdf95/2", "masd/1", "masd/2", "dice", "dice/1", "dice/2"],
                False: ["hdf95/1", "masd/1", "dice"]}
# Hardcoded for lazyness
NBF = [7, 15, 14, 10, 5, 14, 12, 12, 3, 11, 5, 77]
CUMNBF = list(accumulate(NBF, initial=0))

def clean_history(hist, keeper):
    # Remove unwanted stuff from WandB history
    hist = hist[keeper]
    hist = hist.astype(float) # Just in case
    hist[hist.columns[hist.columns.str.contains("masd|hdf")]] *= 0.7 # Vox to mm
    hist[hist.columns[~hist.columns.str.contains("masd|hdf")]] *= 100 # Accuracies
    return hist

@cli.command(context_settings={"help_option_names": ["-h", "--help"],
                               "show_default": True})
@cli.argument("run_id", type=str)
@cli.option("--accuracies/--core", "-a/-C", default=True,
            help="Also compute summary for accuracies.")
@cli.option("--multiclass/--binary", "-m/-b", "multi", default=False,
            help="Whether your run was binary or multi segmentation.")
@cli.option("--sequences-files", "-s", "sequences", default=None, type=cli.Path(),
            help="If given, use the listed test files' frames to aggregate to sequence.")
@cli.option("--post-process", "-p / -P", "post", is_flag=True, default=True,
            help="Also compute the summary for post-process values")
def summarize(run_id, accuracies, multi, sequences, post):
    """ Pouet pouet pouet """
    api = wandb.Api()
    smulti = "multi-" if multi else ''
    run = api.run(f"tee-4d/3DMV-{smulti}segmentation/{run_id}")
    metrics = ALL_METRICS[multi] if accuracies else CORE_METRICS[multi]
    if "dice" not in run.summary:
        metrics.remove("dice") # Looking at multi head architecture
    else: # Old case
        metrics.remove("dice/1")
        metrics.remove("dice/2")
    pmetrics = [ f"p_{m}" for m in metrics ] if post else []
    allres = run.history() # Return `pandas.DataFrame`
    res, pres = clean_history(allres, metrics), clean_history(allres, pmetrics)
    print("Gobal summary: ")
    for c in res.columns:
        print(f"  {c.capitalize():<{7}}: {res[c].mean():>{5}.2f} ± {res[c].std():.2f}")
    if post:
        print("Post-processed global summary:")
    for c in pres.columns:
        cc = c.split('_')[-1]
        print(f"  {cc.capitalize():<{7}}: {pres[c].mean():>{5}.2f} ± {pres[c].std():.2f}")
    if sequences:
        # Get number of frames per file
        with open(sequences, 'r') as fd:
            split = yaml.safe_load(fd)
        nbf = [ x[1] for x in split["test"]["files"] ]
        cumnbf = list(accumulate(nbf, initial=0))
        print("\nPer sequence summary:")
        for i in range(len(cumnbf) - 1):
            seq = res.iloc[cumnbf[i]:cumnbf[i + 1]]
            print("  ", end='')
            print(" | ".join([
                f"{c.capitalize()}: {seq[c].mean():>{5}.2f} ± {seq[c].std():>{5}.2f}"
                    for c in seq.columns]))

if __name__ == "__main__":
    summarize()


#brute-forces IKE group names
#github.com/the-jcksn

import os
import argparse
from subprocess import PIPE, Popen
from termcolor import colored

def cmdline(command):
        process = Popen(
                args=command,
                stdout=PIPE,
                shell=True
        )
        return process.communicate()[0]

parser = argparse.ArgumentParser()
parser.add_argument('-w', default='none', dest='wordlist', help='Provide a wordlist of Group Names for bruteforcing',type=str)
parser.add_argument('-ip', default='none', dest='ip', help='Provide target IP address',type=str)
args = parser.parse_args()

if not 'SUDO_UID' in os.environ.keys():
        print('This script requires super user privs, please run again with sudo')
        quit()
if args.ip == 'none':
        print('No target IP specified with -\'ip\' - Quitting')
        quit()
if args.wordlist == 'none':
        print('No wordlist provided with \'-w\' - Quitting')
        quit()

valid = []
attempt = 'empty'
ikescan = 'sudo ike-scan -M -A -n ' + attempt + ' ' + args.ip
wordlist = args.wordlist

wl_length = 0
with open(wordlist, 'r') as lengthcheck:
        for line in lengthcheck:
                wl_length += 1
print('\nStarting Ike-Brute...')
print('\nChecking',wl_length,'group names against',args.ip,':\n')

with open(wordlist, 'r') as input:
        for line in input:
                line = line[:-1]
                attempt = line
                output = cmdline(ikescan)
                string = '1 returned notify'
                string2 = '1 returned handshake'
                output_text = str(output)
                if string in output_text:
                        print(line, colored(' - failed','red'))
                if string2 in output_text:
                        print(colored(line,'green'),colored('- success!','green'))
                        valid.append(line)
if len(valid) < 1:
        print('\nNo valid Group names found')
else:
        print(colored('\nValid Group Names:','green'))
        print('------------------')
        for line in valid:
                print(line)


import json
import re
import binascii
'''
"{0:#0{1}x}".format(42,6)
'0x002a'
{   # Format identifier
0:  # first parameter
#   # use "0x" prefix
0   # fill with zeroes
{1} # to a length of n characters (including 0x), defined by the second parameter
x   # hexadecimal number, using lowercase letters for a-f
}   # End of format identifier
"{0:0{1}x}".format(address, 4)
'''

def ConvertToHex(conf_data):

    temp = conf_data["hardware_rev"]
    temp = int(temp, 10)
    temp = "{0:0{1}x}".format(temp, 2)
    conf_data["hardware_rev"] = temp

    temp = conf_data["hardware_type"]
    temp = int(temp, 10)
    temp = "{0:0{1}x}".format(temp, 2)
    conf_data["hardware_type"] = temp

    #temp = conf_data["firmware_version"].encode("utf-8").hex()
    #conf_data["firmware_version"] = temp.upper()

    temp = conf_data["serial_number"]
    temp = int(temp, 10)
    temp = "{0:0{1}x}".format(temp, 8)
    temp = re.findall(r'.{2}', temp)
    temp.reverse()
    temp = "".join(temp)
    conf_data["serial_number"] = temp

    temp = conf_data["identifier_number"]
    temp = int(temp, 10)
    temp = "{0:0{1}x}".format(temp, 8)
    temp = re.findall(r'.{2}', temp)
    temp.reverse()
    temp = "".join(temp)
    conf_data["identifier_number"] = temp

    temp = conf_data["name_dcu"].encode("utf-8").hex()
    conf_data["name_dcu"] = temp.upper()

    temp = conf_data["cmd_password_dcu"].encode("utf-8").hex()
    conf_data["cmd_password_dcu"] = temp.upper()

    temp = conf_data["cmd_login_dcu"].encode("utf-8").hex()
    conf_data["cmd_login_dcu"] = temp.upper()

    temp = int(conf_data["dns_enable"], 10)
    temp = "{0:0{1}x}".format(temp, 2)
    conf_data["dns_enable"] = temp.upper()

    temp = int(conf_data["dhcp_enable"], 10)
    temp = "{0:0{1}x}".format(temp, 2)
    conf_data["dhcp_enable"] = temp.upper()

    temp = conf_data["mac_address"].split(":")
    str_temp = ""
    for item in temp:
        item = int(item, 16)
        str_temp = str_temp + "{0:0{1}x}".format(item, 2)
    conf_data["mac_address"] = str_temp.upper()

    temp = conf_data["ip_source"].split(".")
    str_temp = ""
    for item in temp:
        item = int(item, 10)
        str_temp = str_temp + "{0:0{1}x}".format(item, 2)
    conf_data["ip_source"] = str_temp.upper()

    temp = conf_data["mask"].split(".")
    str_temp = ""
    for item in temp:
        item = int(item, 10)
        str_temp = str_temp + "{0:0{1}x}".format(item, 2)
    conf_data["mask"] = str_temp.upper()

    temp = conf_data["ip_gateway"].split(".")
    str_temp = ""
    for item in temp:
        item = int(item, 10)
        str_temp = str_temp + "{0:0{1}x}".format(item, 2)
    conf_data["ip_gateway"] = str_temp.upper()

    temp = conf_data["ip_dds_first"].split(".")
    str_temp = ""
    for item in temp:
        item = int(item, 10)
        str_temp = str_temp + "{0:0{1}x}".format(item, 2)
    conf_data["ip_dds_first"] = str_temp.upper()

    temp = conf_data["ip_dds_second"].split(".")
    str_temp = ""
    for item in temp:
        item = int(item, 10)
        str_temp = str_temp + "{0:0{1}x}".format(item, 2)
    conf_data["ip_dds_second"] = str_temp.upper()

    temp = conf_data["ip_dns"].split(".")
    str_temp = ""
    for item in temp:
        item = int(item, 10)
        str_temp = str_temp + "{0:0{1}x}".format(item, 2)
    conf_data["ip_dns"] = str_temp.upper()

    temp = conf_data["url_dds_first"].encode("utf-8").hex()
    conf_data["url_dds_first"] = temp.upper()

    temp = conf_data["url_dds_second"].encode("utf-8").hex()
    conf_data["url_dds_second"] = temp.upper()

    temp = conf_data["identifier_scu_number"]
    temp = int(temp, 10)
    temp = "{0:0{1}x}".format(temp, 8)
    temp = re.findall(r'.{2}', temp)
    temp.reverse()
    temp = "".join(temp)
    conf_data["identifier_scu_number"] = temp

    temp = conf_data["numbers_slave"]
    temp = int(temp, 10)
    temp = "{0:0{1}x}".format(temp, 2)
    conf_data["numbers_slave"] = temp

    return conf_data

def ArrangeBytes(data):
    eeprom = ""
    # 0 page - reset and bootloader - reset off
    eeprom = eeprom + "AA00{:F<28}".format("")
    # 1 page - reserve for future
    eeprom = eeprom + "{:F<32}".format("")
    # 2 page - ip source
    eeprom = eeprom + "{:F<32}".format(data["ip_source"])
    # 3 page - ip dest - empty
    eeprom = eeprom + "{:F<32}".format("")
    # 4 page - ip gateway
    eeprom = eeprom + "{:F<32}".format(data["ip_gateway"])
    # 5 page - mask
    eeprom = eeprom + "{:F<32}".format(data["mask"])
    # 6 page - mac
    eeprom = eeprom + "{:F<32}".format(data["mac_address"])
    # 7 page - ip dns
    eeprom = eeprom + "{:F<32}".format(data["ip_dns"])
    # 8, 9 page - url - not used
    eeprom = eeprom + "{:F<64}".format("")
    # 10 page - login
    eeprom = eeprom + "{:0<32}".format(data["cmd_login_dcu"])
    # 11 page - password
    eeprom = eeprom + "{:0<32}".format(data["cmd_password_dcu"])
    # 12 page - name dcu
    eeprom = eeprom + "{:0<32}".format(data["name_dcu"])
    # 13 page - version
    eeprom = eeprom + "{:F<32}".format("")
    # 14 page - dns_dhcp enable
    eeprom = eeprom + "{:F<32}".format(data["dns_enable"] + data["dhcp_enable"])
    # 15 page - hardware type
    eeprom = eeprom + "{:F<32}".format(data["hardware_type"])
    # 16 page - hardware revision
    eeprom = eeprom + "{:F<32}".format(data["hardware_rev"])
    # 17 page - serial_number
    eeprom = eeprom + "{:F<32}".format(data["serial_number"])
    # 18 page - identifier
    eeprom = eeprom + "{:F<32}".format(data["identifier_number"])
    # 19 page - first dds ip destination
    eeprom = eeprom + "{:F<32}".format(data["ip_dds_first"])
    # 20 page - second dds ip destination
    eeprom = eeprom + "{:F<32}".format(data["ip_dds_second"])
    # 21, 22 page - first dds url
    eeprom = eeprom + "{:0<64}".format(data["url_dds_first"])
    # 23, 24 page - second dds url
    eeprom = eeprom + "{:0<64}".format(data["url_dds_second"])
    # 25 - scu id
    eeprom = eeprom + "{:F<32}".format(data["identifier_scu_number"])
    # 26 - numbers slave board
    eeprom = eeprom + "{:F<32}".format(data["numbers_slave"])

    eeprom = eeprom.upper()
    return eeprom

def AddingHead(data):
    data_list = re.findall(r'.{32}', data)
    end_data = []
    address = 0

    for part_data in data_list:
        one_row_hex = "10{0:0{1}x}00".format(address, 4) + part_data
        one_row = re.findall(r'.{2}', one_row_hex)
        sum = 0
        for byte_hex in one_row:
            sum = sum + int(byte_hex, 16)
        sum = sum%256
        crc = 256 - sum
        crc = crc % 256
        one_row_hex = ":" + one_row_hex + "{0:0{1}x}".format(crc, 2)
        address = address + 16
        end_data.append(one_row_hex.upper())

    return end_data

def main():
    conf_file = open("config_eeprom.json", "r")
    conf_data = json.load(conf_file)
    conf_file.close()

    name_file = conf_data["name_file"]

    conf_data = ConvertToHex(conf_data)
    eeprom_data = ArrangeBytes(conf_data)
    ready_eeprom = AddingHead(eeprom_data)

    out_file = open(name_file+".eep", "w")
    for row in ready_eeprom:
        out_file.write(row+"\n")

    out_file.write(":00000001FF")
    out_file.close()

    #temp = "{:0<64}".format(temp)

    print(conf_data)

if __name__ == "__main__":
    # execute only if run as a script
    main()


import pytest

from werkzeug.test import EnvironBuilder
from werkzeug.wrappers import Request

from .keys import APIKeys

@pytest.fixture()
def key_file(tmpdir):
  key_file = tmpdir.join("keys.csv")
  key_file.write(
    'key,domains\n'
    'wildcard,\n'
    '# comment,with,commas,\n'
    'multi,example.org;example.com\n'
    'single,test.local'
  )
  yield key_file.strpath

@pytest.mark.parametrize("key,referrer,permitted", [
  ["single", None, True],
  ["single", "", True],
  ["single", "--", True],
  ["single", "http", True],
  ["single", "http://", True],
  ["single", "test.local", True],
  ["single", "http://test.local", True],
  ["single", "https://test.local", True],
  ["single", "http://sub.test.local/path?arg=1", True],
  ["single", "http://sub.test.local", True],
  ["single", "https://subtest.local", False],
  ["single", "http://sub.test.local.com", False],
  ["single", "https://1.2.3.4", False],
  ["multi", "https://example.org", True],
  ["multi", "https://example.com", True],
  ["multi", "https://example.net", False],
  ["wildcard", "https://example.net", True],
  ["wildcard", "https://1.2.3.4", True],

])
def test_api_key(key_file, key, referrer, permitted):
  a = APIKeys(key_file)
  headers = []
  if referrer:
    headers.append(('Referer', referrer))
  builder = EnvironBuilder(method='GET', query_string={'key': key}, headers=headers)
  req = Request(builder.get_environ())
  assert a.is_permitted(req) == permitted


""" Cash movement GUI """
import tkinter
from gui.amount_textbox import AmountTextbox
from gui.company_combobox import CompanyCombobox
from gui.labeled_textbox import LabeledTextbox
from gui.labeled_combobox import LabeledCombobox
from gui.prime_singleton import PrimeSingleton
from gui.font import default_font
from model.payment import payment
import config

class CashMovement:
    """ GUI to record a cash movement
    The movement may be an incoming, outcoming or transfer payment
    """
    _WINDOW_WIDTH = 450
    _WINDOW_HEIGHT = 200

    def __init__(self):
        self._window = tkinter.Toplevel()
        self._window.wm_geometry(str(self._WINDOW_WIDTH) + "x" + str(self._WINDOW_HEIGHT))
        cell_y = 0

        self._company = CompanyCombobox(self._window, "Company", 0, cell_y)
        cell_y += config.CONSTANTS["GUI_CELL_HEIGHT"]

        self._direction = LabeledCombobox(self._window, "Direction", ["I", "O"], 0, cell_y)
        cell_y += config.CONSTANTS["GUI_CELL_HEIGHT"]

        self._amount = AmountTextbox(
            self._window,
            "Amount",
            0,
            config.CONSTANTS["HOME_CURRENCY"],
            0,
            cell_y)
        cell_y += config.CONSTANTS["GUI_CELL_HEIGHT"]

        self._description = LabeledTextbox(self._window, "Description", "", 0, cell_y)
        cell_y += config.CONSTANTS["GUI_CELL_HEIGHT"]

        save_button = tkinter.Button(self._window,
                                     text="OK",
                                     command=self._ok_click,
                                     font=default_font())
        save_button.place(x=config.CONSTANTS["GUI_CELL_WIDTH"], y=cell_y)

    def _ok_click(self):

        payment.record_cash_movement(
            company=self._company.company_name,
            direction=self._direction.selected_value,
            amount=self._amount.amount,
            currency=self._amount.currency,
            description=self._description.value
        )

        PrimeSingleton.get().refresh()
        self._window.destroy()


from os import getenv
from typing import Any, List, Mapping, Tuple

from airbyte_cdk.logger import AirbyteLogger
from airbyte_cdk.models import SyncMode
from airbyte_cdk.sources import AbstractSource
from airbyte_cdk.sources.streams import Stream
from source_amazon_seller_partner.auth import AWSAuthenticator
from source_amazon_seller_partner.constants import get_marketplaces
from source_amazon_seller_partner.streams import (
    BrandAnalyticsAlternatePurchaseReports,
    BrandAnalyticsItemComparisonReports,
    BrandAnalyticsMarketBasketReports,
    BrandAnalyticsRepeatPurchaseReports,
    BrandAnalyticsSearchTermsReports,
    FbaAfnInventoryByCountryReports,
    FbaAfnInventoryReports,
    FbaCustomerReturnsReports,
    FbaEstimatedFbaFeesTxtReport,
    FbaFulfillmentCustomerShipmentPromotionReport,
    FbaInventoryPlaningReport,
    FbaMyiUnsuppressedInventoryReport,
    FbaOrdersReports,
    FbaReimbursementsReports,
    FbaReplacementsReports,
    FbaShipmentsReports,
    FbaSnsForecastReport,
    FbaSnsPerformanceReport,
    FbaStorageFeesReports,
    FlatFileActionableOrderDataShipping,
    FlatFileArchivedOrdersDataByOrderDate,
    FlatFileOpenListingsReports,
    FlatFileOrdersReports,
    FlatFileOrdersReportsByLastUpdate,
    FlatFileReturnsDataByReturnDate,
    FlatFileSettlementV2Reports,
    FulfilledShipmentsReports,
    GetXmlBrowseTreeData,
    LedgerDetailedViewReports,
    LedgerSummaryViewReport,
    ListFinancialEventGroups,
    ListFinancialEvents,
    MerchantCancelledListingsReport,
    MerchantListingsFypReport,
    MerchantListingsInactiveData,
    MerchantListingsReport,
    MerchantListingsReportBackCompat,
    MerchantListingsReports,
    OrderItems,
    OrderReportDataShipping,
    Orders,
    RestockInventoryReports,
    SellerAnalyticsSalesAndTrafficReports,
    SellerFeedbackReports,
    StrandedInventoryUiReport,
    VendorDirectFulfillmentShipping,
    VendorInventoryReports,
    VendorSalesReports,
    XmlAllOrdersDataByOrderDataGeneral,
)

class SourceAmazonSellerPartner(AbstractSource):
    def _get_stream_kwargs(self, config: Mapping[str, Any]) -> Mapping[str, Any]:
        endpoint, marketplace_id, _ = get_marketplaces(config.get("aws_environment"))[config.get("region")]
        auth = AWSAuthenticator(
            token_refresh_endpoint="https://api.amazon.com/auth/o2/token",
            client_id=config.get("lwa_app_id"),
            client_secret=config.get("lwa_client_secret"),
            refresh_token=config.get("refresh_token"),
            host=endpoint.replace("https://", ""),
            refresh_access_token_headers={"Content-Type": "application/x-www-form-urlencoded"},
        )
        stream_kwargs = {
            "url_base": endpoint,
            "authenticator": auth,
            "replication_start_date": config.get("replication_start_date"),
            "marketplace_id": marketplace_id,
            "period_in_days": config.get("period_in_days", 90),
            "report_options": config.get("report_options"),
            "replication_end_date": config.get("replication_end_date"),
            "advanced_stream_options": config.get("advanced_stream_options"),
        }
        return stream_kwargs

    def check_connection(self, logger: AirbyteLogger, config: Mapping[str, Any]) -> Tuple[bool, Any]:
        """
        Check connection to Amazon SP API by requesting the Orders endpoint
        This endpoint is not available for vendor-only Seller accounts,
        the Orders endpoint will then return a 403 error
        Therefore made an exception for 403 errors (when vendor-only accounts).
        When no access, a 401 error is given.
        Validate if response has the expected error code and body.
        Show error message in case of request exception or unexpected response.
        """
        try:
            stream_kwargs = self._get_stream_kwargs(config)
            orders_stream = Orders(**stream_kwargs)
            next(orders_stream.read_records(sync_mode=SyncMode.full_refresh))

            return True, None
        except Exception as e:
            # Validate Orders stream without data
            if isinstance(e, StopIteration):
                return True, None

            # Additional check, since Vendor-only accounts within Amazon Seller API
            # will not pass the test without this exception
            if "403 Client Error" in str(e):
                stream_to_check = VendorSalesReports(**stream_kwargs)
                next(stream_to_check.read_records(sync_mode=SyncMode.full_refresh))
                return True, None

            return False, e

    def streams(self, config: Mapping[str, Any]) -> List[Stream]:
        """
        :param config: A Mapping of the user input configuration as defined in the connector spec.
        """
        stream_kwargs = self._get_stream_kwargs(config)
        streams = [
            FbaCustomerReturnsReports(**stream_kwargs),
            FbaAfnInventoryReports(**stream_kwargs),
            FbaAfnInventoryByCountryReports(**stream_kwargs),
            FbaOrdersReports(**stream_kwargs),
            FbaShipmentsReports(**stream_kwargs),
            FbaReplacementsReports(**stream_kwargs),
            FbaStorageFeesReports(**stream_kwargs),
            RestockInventoryReports(**stream_kwargs),
            FlatFileActionableOrderDataShipping(**stream_kwargs),
            FlatFileOpenListingsReports(**stream_kwargs),
            FlatFileOrdersReports(**stream_kwargs),
            FlatFileOrdersReportsByLastUpdate(**stream_kwargs),
            FlatFileSettlementV2Reports(**stream_kwargs),
            FulfilledShipmentsReports(**stream_kwargs),
            MerchantListingsReports(**stream_kwargs),
            VendorDirectFulfillmentShipping(**stream_kwargs),
            Orders(**stream_kwargs),
            OrderItems(**stream_kwargs),
            OrderReportDataShipping(**stream_kwargs),
            SellerFeedbackReports(**stream_kwargs),
            GetXmlBrowseTreeData(**stream_kwargs),
            ListFinancialEventGroups(**stream_kwargs),
            ListFinancialEvents(**stream_kwargs),
            LedgerDetailedViewReports(**stream_kwargs),
            FbaEstimatedFbaFeesTxtReport(**stream_kwargs),
            FbaFulfillmentCustomerShipmentPromotionReport(**stream_kwargs),
            FbaMyiUnsuppressedInventoryReport(**stream_kwargs),
            MerchantCancelledListingsReport(**stream_kwargs),
            MerchantListingsReport(**stream_kwargs),
            MerchantListingsReportBackCompat(**stream_kwargs),
            MerchantListingsInactiveData(**stream_kwargs),
            StrandedInventoryUiReport(**stream_kwargs),
            XmlAllOrdersDataByOrderDataGeneral(**stream_kwargs),
            MerchantListingsFypReport(**stream_kwargs),
            FbaSnsForecastReport(**stream_kwargs),
            FbaSnsPerformanceReport(**stream_kwargs),
            FlatFileArchivedOrdersDataByOrderDate(**stream_kwargs),
            FlatFileReturnsDataByReturnDate(**stream_kwargs),
            FbaInventoryPlaningReport(**stream_kwargs),
            LedgerSummaryViewReport(**stream_kwargs),
            FbaReimbursementsReports(**stream_kwargs),
        ]
        # TODO: Remove after Brand Analytics will be enabled in CLOUD:
        #  https://github.com/airbytehq/airbyte/issues/32353
        if getenv("DEPLOYMENT_MODE", "").upper() != "CLOUD":
            brand_analytics_reports = [
                BrandAnalyticsMarketBasketReports(**stream_kwargs),
                BrandAnalyticsSearchTermsReports(**stream_kwargs),
                BrandAnalyticsRepeatPurchaseReports(**stream_kwargs),
                BrandAnalyticsAlternatePurchaseReports(**stream_kwargs),
                BrandAnalyticsItemComparisonReports(**stream_kwargs),
                SellerAnalyticsSalesAndTrafficReports(**stream_kwargs),
                VendorSalesReports(**stream_kwargs),
                VendorInventoryReports(**stream_kwargs),
            ]
            streams += brand_analytics_reports
        return streams


import os
import time
import signal

from tinkoff.invest import Client

import modules.bonds_divergence as bonds_divergence

TOKEN = os.environ["INVEST_TOKEN"]

running = True

def signal_handler(signum, frame):
    global running
    running = False

def main():

    signal.signal(signal.SIGTERM, signal_handler)
    signal.signal(signal.SIGINT, signal_handler)

    with Client(TOKEN) as client:

        div = bonds_divergence.divergence(client)
        print(div)

        # loop
        counter = 0
        while(running):
            if counter % 5 == 0:
                div = bonds_divergence.divergence(client)
                print(div)

            time.sleep(1)
            counter += 1

if __name__ == "__main__":
    main()


#!/usr/bin/env python

import argparse
from collections import defaultdict
import sys

def print_docs(docs):
    not_found = []

    for k in docs:
        en_url, fr_url = k

        if 'en_text' not in docs[k]:
            not_found.append("not found(en): {0}".format(en_url))
            continue

        if 'fr_text' not in docs[k]:
            not_found.append("not found(fr): {0}".format(fr_url))
            continue

        en_text, fr_text = docs[k]['en_text'], docs[k]['fr_text']
        print("{0}\t{1}\t{2}\t{3}\t".format(en_url, fr_url, en_text, fr_text))

    if len(not_found):
        sys.stderr.write("Number of documents without matches: {0}\n".format(len(not_found)))
        for n in not_found:
            sys.stderr.write("{0}\n".format(n))

def load_docs(matches_filepath, threshold):
    map_e2f = {}
    map_f2e = {}

    docs = defaultdict(dict)

    with open(matches_filepath, 'r') as f_mapping:
        for line in f_mapping:
            score, e, f = line.strip().split('\t')
            if float(score) < float(threshold):
                continue

            map_e2f[e] = f
            map_f2e[f] = e

    for line in sys.stdin:
        lang, format, encoding, url, html, text = line.strip().split('\t')

        if url in map_e2f:
            key = (url, map_e2f[url])
            docs[key]['en_text'] = text

        elif url in map_f2e:
            key = (map_f2e[url], url)
            docs[key]['fr_text'] = text

    return docs

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('--matches', help='path to the file with matched documents', required=True)
    parser.add_argument('--threshold', help='documents with lower TF-IDF score will be skipped', default=0.1, type=float, required=False)

    args = parser.parse_args()

    docs = load_docs(args.matches, args.threshold)
    print_docs(docs)


import pandas as pd
import pandas_flavor as pf
import numpy as np

from typing import Union, Optional

from pytimetk.utils.checks import check_dataframe_or_groupby, check_date_column, check_value_column
from pytimetk.core.frequency import get_frequency, get_seasonal_frequency, get_trend_frequency
from pytimetk.utils.memory_helpers import reduce_memory_usage

from pytimetk.utils.parallel_helpers import parallel_apply, get_threads, progress_apply

from statsmodels.tsa.seasonal import seasonal_decompose
from statsmodels.tsa.seasonal import STL

@pf.register_dataframe_method
def anomalize(
    data: Union[pd.DataFrame, pd.core.groupby.generic.DataFrameGroupBy],
    date_column: str,
    value_column: str,
    period: Optional[int] = None,
    trend: Optional[int] = None,
    method: str = 'stl',
    decomp: str = 'additive',
    clean: str = 'min_max',
    iqr_alpha: float = 0.05,
    clean_alpha: float = 0.75,
    max_anomalies: float = 0.2,
    bind_data: bool = False,
    threads: int = 1,
    show_progress: bool = True,
    verbose = False,
) -> pd.DataFrame:
    '''
    The `anomalize` function is used to detect anomalies in time series data,
    either for a single time
    series or for multiple time series grouped by a specific column.

    Parameters
    ----------
    data : Union[pd.DataFrame, pd.core.groupby.generic.DataFrameGroupBy]
        The input data, which can be either a pandas DataFrame or a pandas
        DataFrameGroupBy object.
    date_column : str
        The name of the column in the data that contains the dates or timestamps.
    value_column : str
        The name of the column in the data that contains the values to be analyzed
        for anomalies.
    period : Optional[int]
        The `period` parameter specifies the length of the seasonal component in the
        time series. It is used in the decomposition process to separate the time
        series into its seasonal, trend, and remainder components. If not specified,
        the function will automatically determine the period based on the data.
    trend : Optional[int]
        The `trend` parameter is an optional integer that specifies the length of
        the moving average window used for trend estimation. If `trend` is set to
        `None`, no trend estimation will be performed.
    method : str
        The `method` parameter determines the method used for anomaly detection.
        The only available method is `twitter`, which is the default value.
        More anomaly detection methods will be added in upcoming releases.
    decomp : str
        The `decomp` parameter specifies the type of decomposition to use for time
        series decomposition. It can take two values:
        1. 'additive' - This is the default value. It specifies that the time series
            will be decomposed using an additive model.
        2. 'multiplicative' - This specifies that the time series will be decomposed
            using a multiplicative model.
    clean : str
        The `clean` parameter specifies the method used to clean the anomalies.
        It can take two values:

        1. 'min_max' - This specifies that the anomalies will be cleaned using the
            min-max method. This method replaces the anomalies with the 0.75 * lower
            or upper bound of the recomposed time series, depending on the direction
            of the anomaly. The 0.75 multiplier can be adjusted using the
            `clean_alpha` parameter.
        2. 'linear' - This specifies that the anomalies will be cleaned using
                    linear interpolation.
    iqr_alpha : float
        The `iqr_alpha` parameter is used to determine the threshold for detecting
        outliers. It is the significance level used in the interquartile range (IQR)
        method for outlier detection.
        - The default value is 0.05, which corresponds to a 5% significance level.
        - A lower significance level will result in a higher threshold, which means
        fewer outliers will be detected.
        - A higher significance level will result in a lower threshold, which means
        more outliers will be detected.
    clean_alpha : float
        The `clean_alpha` parameter is used to determine the threshold for cleaning
        the outliers. The default is 0.75, which means that the anomalies will be
        cleaned using the 0.75 * lower or upper bound of the recomposed time series,
        depending on the direction of the anomaly.
    max_anomalies : float
        The `max_anomalies` parameter is used to specify the maximum percentage of
        anomalies allowed in the data. It is a float value between 0 and 1. For
        example, if `max_anomalies` is set to 0.2, it means that the function will
        identify and remove outliers until the percentage of outliers in the data is
        less than or equal to 20%. The default value is 0.2.
    bind_data : bool
        The `bind_data` parameter determines whether the original data will be
        included in the output. If set to `True`, the original data will be included
        in the output dataframe. If set to `False`, only the anomalous data will be
        included.
    threads : int
        The `threads` parameter specifies the number of threads to use for parallel
        processing. By default, it is set to `1`, which means no parallel processing
        is used. If you set `threads` to `-1`, it will use all available processors
        for parallel processing.
    show_progress : bool
        A boolean parameter that determines whether to show a progress bar during
        the execution of the function. If set to True, a progress bar will be
        displayed. If set to False, no progress bar will be shown.
    verbose: bool
        The `verbose` parameter is a boolean flag that determines whether or not to
        display additional information and progress updates during the execution of
        the `anomalize` function. If `verbose` is set to `True`, you will see more
        detailed output.

    Returns
    -------
    pd.DataFrame
        The `anomalize` function returns a pandas DataFrame containing the original
        data with additional
    columns:
        - observed: original data
        - seasonal: seasonal component
        - seasadaj: seasonal adjusted
        - trend: trend component
        - remainder: residual component
        - anomaly: Yes/No flag for outlier detection
        - anomaly score: distance from centerline
        - anomaly direction: -1, 0, 1 inidicator for direction of the anomaly
        - recomposed_l1: lower level bound of recomposed time series
        - recomposed_l2: upper level bound of recomposed time series
        - observed_clean: original data with anomalies interpolated

    Notes
    -----
    ## Performance

    This function uses parallel processing to speed up computation for large
    datasets with many time series groups:

    Parallel processing has overhead and may not be faster on small datasets.

    To use parallel processing, set `threads = -1` to use all available processors.

    Examples
    --------
    ``` {python}
    # EXAMPLE 1: SINGLE TIME SERIES
    import pytimetk as tk
    import pandas as pd
    import numpy as np

    # Create a date range
    date_rng = pd.date_range(start='2021-01-01', end='2024-01-01', freq='MS')

    # Generate some random data with a few outliers
    np.random.seed(42)
    data = np.random.randn(len(date_rng)) * 10 + 25
    data[3] = 100  # outlier

    # Create a DataFrame
    df = pd.DataFrame(date_rng, columns=['date'])
    df['value'] = data

    # Anomalize the data
    anomalize_df = tk.anomalize(
        df, "date", "value",
        method = "twitter",
        iqr_alpha = 0.10,
        clean_alpha = 0.75,
        clean = "min_max",
        verbose = True,
    )

    anomalize_df.glimpse()
    ```

    ``` {python}
    # Visualize the results
    anomalize_df.plot_anomalies_decomp("date")
    ```

    ``` {python}
    # Visualize the anomaly bands
    (
         anomalize_df
            .plot_anomalies(
                date_column = "date",
                engine = "plotly",
            )
    )
    ```

    ``` {python}
    # Get the anomalies
    anomalize_df.query("anomaly=='Yes'")
    ```

    ``` {python}
    # Visualize observed vs cleaned
    anomalize_df.plot_anomalies_cleaned("date")
    ```

    ``` {python}
    # EXAMPLE 2: MULTIPLE TIME SERIES
    import pytimetk as tk
    import pandas as pd

    df = tk.load_dataset("wikipedia_traffic_daily", parse_dates = ['date'])

    anomalize_df = (
        df
            .groupby('Page', sort = False)
            .anomalize(
                date_column = "date",
                value_column = "value",
                method = "stl",
                iqr_alpha = 0.025,
                verbose = False,
            )
    )

    # Visualize the decomposition results

    (
        anomalize_df
            .groupby("Page")
            .plot_anomalies_decomp(
                date_column = "date",
                width = 1800,
                height = 1000,
                x_axis_date_labels = "%Y",
                engine = 'plotly'
            )
    )
    ```

    ``` {python}
    # Visualize the anomaly bands
    (
        anomalize_df
            .groupby("Page")
            .plot_anomalies(
                date_column = "date",
                facet_ncol = 2,
                width = 1000,
                height = 1000,
            )
    )
    ```

    ``` {python}
    # Get the anomalies
    anomalize_df.query("anomaly=='Yes'")
    ```

    ``` {python}
    # Visualize observed vs cleaned
    (
        anomalize_df
            .groupby("Page")
            .plot_anomalies_cleaned(
                "date",
                facet_ncol = 2,
                width = 1000,
                height = 1000,
            )
    )
    ```
    '''

    check_dataframe_or_groupby(data)
    check_date_column(data, date_column)
    check_value_column(data, value_column)

    if isinstance(data, pd.DataFrame):
        result = _anomalize(
            data = data,
            date_column=date_column,
            value_column=value_column,
            period=period,
            trend=trend,
            method=method,
            decomp=decomp,
            clean=clean,
            iqr_alpha=iqr_alpha,
            clean_alpha=clean_alpha,
            max_anomalies=max_anomalies,
            bind_data=bind_data,
            verbose=verbose,
        )

    if isinstance(data, pd.core.groupby.generic.DataFrameGroupBy):

        group_names = data.grouper.names

        # Get threads
        threads = get_threads(threads)

        if threads == 1:

            result = progress_apply(
                data,
                func = _anomalize,
                show_progress=show_progress,
                desc="Anomalizing...",
                # kwargs
                date_column=date_column,
                value_column=value_column,
                period=period,
                trend=trend,
                method=method,
                decomp=decomp,
                clean=clean,
                iqr_alpha=iqr_alpha,
                clean_alpha=clean_alpha,
                max_anomalies=max_anomalies,
                bind_data=bind_data,
                verbose=verbose,
            ).reset_index(level=group_names)

        else:

            result = parallel_apply(
                data,
                _anomalize,
                date_column=date_column,
                value_column=value_column,
                period=period,
                trend=trend,
                method=method,
                decomp=decomp,
                clean=clean,
                iqr_alpha=iqr_alpha,
                clean_alpha=clean_alpha,
                max_anomalies=max_anomalies,
                bind_data=bind_data,
                threads=threads,
                show_progress=show_progress,
                verbose=verbose,
                desc="Anomalizing...",
            ).reset_index(level=group_names)

    return result

# Monkey patch the method to pandas groupby objects
pd.core.groupby.generic.DataFrameGroupBy.anomalize = anomalize

def _anomalize(
    data: Union[pd.DataFrame, pd.core.groupby.generic.DataFrameGroupBy],
    date_column: str,
    value_column: str,
    period: Optional[int] = None,
    trend: Optional[int] = None,
    method: str = 'stl',
    decomp: str = 'additive',
    clean: str = 'linear',
    iqr_alpha: float = 0.05,
    clean_alpha: float = 0.75,
    max_anomalies: float = 0.2,
    bind_data: bool = False,
    verbose = False,
) -> pd.DataFrame:

    orig_date_column = data[date_column]

    data = reduce_memory_usage(data.copy())

    # STEP 0: Get the seasonal period and trend frequency
    if period is None:
        period = get_seasonal_frequency(data[date_column], numeric=True)
        period = int(period)
    if verbose:
        print(f"Using seasonal frequency of {period} observations")

    if trend is None:
        trend = get_trend_frequency(data[date_column], numeric=True)
        trend = int(trend)
    if verbose:
        print(f"Using trend frequency of {trend} observations")

    # STEP 1: Decompose the time series
    if method == 'twitter':

        median_span = np.round(len(data) / trend)
        median_span = int(median_span)

        result = _twitter_decompose(
            data = data,
            date_column=date_column,
            value_column=value_column,
            period=period,
            median_span=median_span,
            model=decomp,
        )
    elif method == 'stl':

        def make_odd(n):
            return n + 1 if n % 2 == 0 else n

        seasonal = make_odd(period)
        trend = make_odd(trend)

        result = _stl_decompose(
            data=data,
            date_column=date_column,
            value_column=value_column,
            period=period,
            seasonal=seasonal,
            trend=trend,
            robust = True,
        )
    else:
        raise ValueError(f"Method {method} is not supported. Please use one of 'stl' or 'twitter'.")

    # STEP 2: Identify the outliers

    outlier_df = _iqr(
        data = result,
        target = 'remainder',
        alpha = iqr_alpha,
        max_anoms = max_anomalies
    )

    # STEP 3: Recompose the time series

    result['anomaly'] = outlier_df['outlier_reported']
    result['anomaly_score'] = outlier_df['score']
    result['anomaly_direction'] = outlier_df['direction']

    result['recomposed_l1'] = result['seasonal'] + result['trend'] + outlier_df['remainder_l1']

    result['recomposed_l2'] = result['seasonal'] + result['trend'] + outlier_df['remainder_l2']

    # STEP 4: Clean the Anomalies

    if clean == 'linear':
        result['observed_clean'] = result['observed'] \
            .where(result['anomaly']=='No', np.nan) \
            .interpolate(method=clean, limit_direction='both')
    else:
        # min_max
        result['observed_clean'] = np.where(
            result['anomaly_direction'] == -1,
            result['recomposed_l1'] + ((1-clean_alpha)*(result['recomposed_l2'] - result['recomposed_l1'])/2),
            np.where(
                result['anomaly_direction'] == 1,
                result['recomposed_l2'] - ((1-clean_alpha)*(result['recomposed_l2'] - result['recomposed_l1'])/2),
                result['observed']
            )
        )

    result[date_column] = orig_date_column

    # STEP 5: Bind the data
    if bind_data:
        result = pd.concat([data, result.drop(date_column, axis=1)], axis=1)

    return reduce_memory_usage(result)

def _twitter_decompose(
    data,
    date_column,
    value_column,
    period = None,
    median_span = None,
    model = 'additive',
):
    orig_index = data.index

    series = data.set_index(date_column)[value_column]

    # Need to add freq, trend, and kwargs
    # TODO - Median Seasonal Trend (More robust to outliers)
    result = seasonal_decompose(
        series,
        period=period,
        model=model,
        extrapolate_trend='freq',
    )

    # Construct TS Decomposition DataFrame
    observed = series

    seasadj = series - result.seasonal
    seasadj.name = 'seasadj'

    # Calculate median trend
    if median_span is None:
        median_span = 4

    def repeat_sequence(seq, length_out):
        quotient, remainder = divmod(length_out, len(seq))
        return seq * quotient + seq[:remainder]

    df = pd.DataFrame(seasadj)

    df['median_index'] = sorted(repeat_sequence(list(range(median_span)), len(seasadj)))

    trend = df.groupby('median_index')['seasadj'].transform('median')

    resid = seasadj - trend

    result_df = pd.concat([observed, result.seasonal, seasadj, trend, resid], axis=1)

    result_df.columns = ['observed', 'seasonal', 'seasadj', 'trend', 'remainder']

    result_df.reset_index(inplace=True)

    result_df.index = orig_index

    return result_df

def _seasonal_decompose(
    data,
    date_column,
    value_column,
    model='additive',
    period = None,
    filt=None,
    two_sided=True,
    extrapolate_trend = 'freq'
):

    orig_index = data.index

    series = data.set_index(date_column)[value_column]

    # Need to add freq, trend, and kwargs
    result = seasonal_decompose(
        series,
        model=model,
        period = period,
        filt=filt,
        two_sided=two_sided,
        extrapolate_trend = extrapolate_trend,
    )

    # Construct TS Decomposition DataFrame
    observed = series

    seasadj = series - result.seasonal

    trend = result.trend

    resid = seasadj - trend

    result_df = pd.concat([observed, result.seasonal, seasadj, trend, resid], axis=1)

    result_df.columns = ['observed', 'seasonal', 'seasadj', 'trend', 'remainder']

    result_df.reset_index(inplace=True)

    result_df.index = orig_index

    return result_df

def _stl_decompose(
    data,
    date_column,
    value_column,
    period = None,
    **kwargs
):

    orig_index = data.index

    series = data.set_index(date_column)[value_column]

    # Need to add freq, trend, and kwargs
    stl = STL(
        series,
        period = period,
        **kwargs
    )

    result = stl.fit()

    # Construct TS Decomposition DataFrame
    observed = series

    seasadj = series - result.seasonal

    trend = result.trend

    resid = seasadj - trend

    result_df = pd.concat([observed, result.seasonal, seasadj, trend, resid], axis=1)

    result_df.columns = ['observed', 'seasonal', 'seasadj', 'trend', 'remainder']

    result_df.reset_index(inplace=True)

    result_df.index = orig_index

    return result_df

def _iqr(data, target, alpha=0.05, max_anoms=0.2):
    """
    This function is not intended for general use. It is used internally by the anomaly detection functions.

    Examples
    --------
    ``` {python}
    import pytimetk as tk
    import pandas as pd
    import numpy as np

    # Example Usage:
    df = pd.DataFrame({
        'x': list(range(100)),
        'y': np.random.randn(100)
    })

    outlier_dict = tk.iqr(df, 'y')
    ```
    """

    data = data.copy()

    # Compute the interquartile range
    q1, q3 = np.percentile(data[target], [25, 75])
    iq_range = q3 - q1
    limits = [-1*(q1 + (0.15 / alpha) * iq_range), q3 + (0.15 / alpha) * iq_range]

    # Identify the outliers
    outlier_idx = (data[target] < limits[0]) | (data[target] > limits[1])

    # Calculate the anomaly_score from the centerline
    centerline = sum(limits) / 2
    data['score'] = abs(data[target] - centerline)

    # Yes/No flag for outlier
    data['outlier_reported'] = np.where(data[target] > limits[1], "Yes", np.where(data[target] < limits[0], "Yes", "No"))

    # Direction of the outlier
    data['direction'] = np.where(data[target] > limits[1], 1, np.where(data[target] < limits[0], -1, 0))

    # Remainder Limits
    data['remainder_l1'] = limits[0]
    data['remainder_l2'] = limits[1]

    return data[['outlier_reported', 'direction', 'score', 'remainder_l1', 'remainder_l2']]


from nltk import word_tokenize
from nltk.stem import PorterStemmer
import numpy as np
import nltk

#Package with pre-trained tokenizer
nltk.download('punkt')

stemmer = PorterStemmer()

def tokenize(sentence: str) -> list:
    '''
    Get tokenized words from a sentence
    '''
    return word_tokenize(sentence)

def stem(word: str) -> str:
    '''
    Get words their root form
    '''
    return stemmer.stem(word.lower())

def bag_of_words(tokenized_sentence: list, all_words: list):
    '''
    tokenized_sentence = ['hello', 'how', 'are', 'you']
    all_words = ['hi', 'hello', 'I', 'you', 'bye', 'thank', 'cool']
    returned_value = [   0,     1,      0,    1,     0,      0,       0  ]
    '''
    tokenized_sentence = [stem(word) for word in tokenized_sentence]

    bag = np.zeros(len(all_words), dtype=np.float32)
    for index, word in enumerate(all_words):
        if word in tokenized_sentence:
            bag[index] = 1.0

    return bag

tokenized_sentence = ['hello', 'how', 'are', 'you']
all_words = ['hi', 'hello', 'I', 'you', 'bye', 'thank', 'cool']

bag = bag_of_words(tokenized_sentence, all_words)
# print(bag)


from bpclass import BPneuron, Loss
import matplotlib.pyplot as plt
import numpy as np
from random import random
import matplotlib.pyplot as plt
from IPython.display import clear_output

class test_network:
    def __init__(self, I, H, O, W01_size, W12_size):
        # 重みとバイアスの定義
        W01 = W01_size*np.random.rand(I, H)-W01_size/2
        W12 = W12_size*np.random.rand(H, O)-W12_size/2
        b1 = np.zeros(H)
        b2 = np.zeros(O)

        # モデルの生成
        self.Seccond_layer = BPneuron(W01, b1)
        self.Third_layer = BPneuron(W12, b2)
        self.test_loss_layer = Loss()

        self.loss_memo = []
        self.dx_memo = np.empty(0)
        self.bo_memo = np.empty(0)
        self.dbo_memo = np.empty(0)
        self.W12_memo = np.empty(0)

    def traning(self, test_data, target_data, epoch):
        # 学習に使う配列の決定
        for i in range(epoch):
            test_number = np.random.randint(target_data.shape[0])
            x = np.array([test_data[test_number]])
            t = np.array([target_data[test_number]])

            out1, container1 = self.Seccond_layer.forward(x)
            out2, container2 = self.Third_layer.forward(out1)
            W, b, x, y, z = container1
            W, b, x, y, z = container2

            loss = self.test_loss_layer.forward(out2, t)
            dout = self.test_loss_layer.backward()
            if i % 500 == 0:
                print("loss:", dout, "epoch:", i)
                print("_________")
                print("b:", b)
                print("_________")

            if i % 5000 == 0:
                clear_output()

            self.loss_memo.append(dout[0][0])
            dx, containe = self.Third_layer.backward(dout)
            dinput, containe = self.Seccond_layer.backward(dx)
            self.dx_memo = np.append(self.dx_memo, dx)
            self.bo_memo = np.append(self.bo_memo, b)
            self.W12_memo = np.append(self.W12_memo, W)

        return self.loss_memo

    def dx(self):
        return self.dx_memo

    def bo(self):
        return self.bo_memo

    def W_12(self):
        return self.W12_memo


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('sparkle', '0002_auto_20140701_1935'),
    ]

    operations = [
        migrations.AlterField(
            model_name='systemprofilereport',
            name='ip_address',
            field=models.GenericIPAddressField(verbose_name='IP address'),
            preserve_default=True,
        ),
        migrations.AlterField(
            model_name='version',
            name='application',
            field=models.ForeignKey(related_name='versions', to='sparkle.Application', verbose_name='application'),
            preserve_default=True,
        ),
        migrations.AlterField(
            model_name='version',
            name='channels',
            field=models.ManyToManyField(related_name='versions', verbose_name='channels', to='sparkle.Channel'),
            preserve_default=True,
        ),
    ]


import io
import socket
import struct
import cv2
import numpy as np

# Start a socket listening for connections on 0.0.0.0:8000 (0.0.0.0 means
# all interfaces)
server_socket = socket.socket()
server_socket.bind(('0.0.0.0', 8000))
server_socket.listen(0)

# Accept a single connection and make a file-like object out of it
connection = server_socket.accept()[0].makefile('rb')

cap = cv2.VideoCapture(0)

try:
    while True:
        f,frame = cap.read()
        cv2.imshow('frame', frame)
        # Read the length of the image as a 32-bit unsigned int. If the
        # length is zero, quit the loop
        image_len = struct.unpack('<L', connection.read(struct.calcsize('<L')))[0]
        if not image_len:
            break

        image = np.fromstring(connection.read(image_len), dtype=np.uint8).reshape((480,640,3))
        image2 = image.copy()
        cv2.imshow('image', image2)

        if cv2.waitKey(10) != -1:
            connection.close()
            server_socket.close()
            cap.release()
            cv2.destroyAllWindows()

finally:
    connection.close()
    server_socket.close()
    cv2.destroyAllWindows()


import json
from time import sleep

import requests
from sensors import Sensors
from hardware_controller import HardwareController

hardware_controller = HardwareController()
sensors = Sensors()

def cool_hardware_down():
    print("Trwa kończenie pracy systemu.")
    hardware_controller.stop_heating()
    print("Trwa wychładzanie par. Potrwa minutę.")
    sleep(60)
    hardware_controller.stop_water_cooling()
    print("Trwa zamykanie przepływu w chłodnicy. Potrwa 15 sekund.")
    sleep(15)
    print("Zakończono pracę systemu")

def send_results(session_id, results):
    requests.post(f"http://127.0.0.1:8000/sessions/{session_id}/results",
                  data=json.dumps(results))

def stop_session(session_id, message):
    cool_hardware_down()
    requests.put(f"http://127.0.0.1:8000/sessions/{session_id}/finish",
                 data=json.dumps({"termination_reason": message}))

def handle_results(session_id, results):
    print(results)
    send_results(session_id, results)
    if results["temperature_steam"] > 82:
        print("Temperatura par: ", results["temperature_steam"])
        stop_session(session_id, "Zakończona z powodu przekroczenia temperatury")
    if results["mass_obtained"] > 500:
        print("Masa uzysku: ", results["mass_obtained"])
        stop_session(session_id, "Zakończona z powodu nadmiaru odebranego uzysku")
    if results["liquid_reached_critical_level"]:
        print("Przekroczono krytyczny poziom cieczy.")
        stop_session(session_id, "Zakończona z powodu przekroczenia wysokości cieszy")

def handle_session(session):
    print("Znaleziono trwającą sesję: ", session["name"])
    hardware_controller.start_water_cooling()
    hardware_controller.start_heating()

    while not session["is_finished"]:
        sleep(session["time_interval"])
        handle_results(session['id'], sensors.results())
        session = requests.get(f"http://127.0.0.1:8000/sessions/{session['id']}").json()

    cool_hardware_down()

def main():
    try:
        while True:
            print("Szukam aktywnej sesji.")
            r = requests.get("http://127.0.0.1:8000/sessions/")
            for session in r.json():
                if not session["is_finished"]:
                    handle_session(session)
            sleep(5)
    except KeyboardInterrupt:
        print("Ręczne zatrzymanie.")
        cool_hardware_down()
        hardware_controller.cleanup_pins()

if __name__ == "__main__":
    main()


'''
Created on 12.04.2018

@author: hm
'''

import re
import traceback
import os.path
import tempfile
import posix
import fnmatch

import base.FileHelper
import base.MemoryLogger

class UnitTestAppInfo:
    def __init__(self, usage):
        self._usage = usage

class UnitTestCase:
    def __init__(self):
        self._userId = posix.getuid()
        self._isRoot = self._userId == 0
        self._inTestSuite = False
        self._errors = 0
        self._summary = None
        self._name = type(self).__name__
        self._asserts = 0
        self._application = UnitTestAppInfo(self.usage)
        self._logger = base.MemoryLogger.MemoryLogger(True)
        self._silentLogger = base.MemoryLogger.MemoryLogger(False)
        base.FileHelper.setLogger(self._logger)

    def _finish(self):
        '''The last action of a test case.
        Should be overwritten by sub classes
        '''

    def _describeDifference(self, str1, str2, prefix = ''):
        '''Logs the difference of two strings.
        @param str1: first string to compare
        @param str2: second string to compare
        @param prefix: '' or a prefix
        @return: a string describing the difference
        '''
        count1 = len(str1)
        count2 = len(str2)
        ixDiff = -1
        for ix in range(min(count1, count2)):
            if str1[ix] != str2[ix]:
                ixDiff = ix
                break
        if ixDiff != -1:
            rc = '+++ {:s}different at pos {:d}: {:s}/{:s}\n'.format(prefix, ixDiff + 1, str1[ixDiff:ixDiff+5], str2[ixDiff:ixDiff+5])
        elif count1 > count2:
            rc = '+++ expected is longer:\n'
        else:
            rc = '+++ expected is shorter:\n'
        rc += str1 + '\n' + str2
        if ixDiff != -1:
            rc += '\n' + ('=' * ixDiff) + '^'
        return rc

    def assertDirExists(self, current):
        '''Tests whether a given directory exists.
        @param current: the directory to inspect
        @returns: True: the directory exists
        '''
        self._asserts += 1
        rc = True
        if not os.path.exists(current):
            rc = self.error('directory does not exist: ' + current)
        elif not os.path.isdir(current):
            rc = self.error('file {} exists but it is not a directory'.format(current))
        return rc

    def assertIsEqual(self, expected, current):
        '''Tests whether two values are equal.
        @param expected: the expected value
        @param current: the value to test
        @return: True: values are equal
        '''
        rc = False
        self._asserts += 1
        if type(expected) != type(current):
            self.error('different types: ' + str(type(expected)) + ' / ' + str(type(current)))
        else:
            rc = expected == current
            if not rc:
                if type(expected) is str:
                    if expected.find('\n') < 0:
                        self.error(self._describeDifference(expected, current))
                    else:
                        listExpected = expected.splitlines()
                        listCurrent = current.splitlines()
                        ixDiff = -1
                        for ix in range(min(len(listExpected), len(listCurrent))):
                            if listExpected[ix] != listCurrent[ix]:
                                self.error(self._describeDifference(listExpected[ix], listCurrent[ix], 'in line {:d}: '.format(ix + 1)))
                                ixDiff = ix
                                break
                        if ixDiff == -1 and len(listExpected) != len(listCurrent):
                            if len(listExpected) < len(listCurrent):
                                msg = 'expected has fewer lines: {:d}/{:d}\n'.format(len(listExpected), len(listCurrent)) + listCurrent[len(listExpected)]
                            else:
                                msg = 'expected has more lines: {:d}/{:d}\n'.format(len(listExpected), len(listCurrent)) + listExpected[len(listCurrent)]
                            self.error(msg)
                elif type(expected) is int:
                    self.error('different: {:d}/{:d} [0x{:x}/0x{:x}]'.format(expected, current, expected, current))
                else:
                    self.error('different: {:s} / {:s}'.format(str(expected), str(current)))
        return rc

    def assertFalse(self, current):
        '''Tests whether a value is False.
        @param current: value to test
        @returns: True: the value is False
        '''
        rc = True
        self._asserts += 1
        if current != False:
            rc = self.error('+++ False expected, found: ' + str(current))
        return rc

    def assertFileContent(self, expectedContent, currentFile):
        '''Compares a given string with an expected file content.
        @param currentFile: the name of the file with the expected content, e.g. 'data/abc.xyz'
        @param expectedContent: the content to compare
        @returns: True: file content is the expected
        '''
        rc = True
        self._asserts += 1
        full = currentFile if currentFile.startswith(os.sep) else os.path.dirname(__file__) + os.sep + currentFile
        if not os.path.exists(full):
            rc =self.error('missing file: ' + currentFile)
        else:
            with open(full, 'r') as fp:
                current = fp.read()
            rc = self.assertIsEqual(expectedContent, current)
            if not rc:
                tempFile = self.tempFile(os.path.basename(currentFile))
                with open(tempFile, "w") as fp:
                    fp.write(expectedContent)
                self.log('meld {} {}'.format(tempFile, full))
        return rc

    def assertFileContains(self, expected, currentFile):
        '''Tests whether a given file contains a given content.
        @param expected: content to search. May be a string or a re.RegExpr instance
        @param currentFile: file to inspect
        @returns: True: the file contains the expected string
        '''
        rc = True
        self._asserts += 1
        if not os.path.isfile(currentFile):
            rc = self.error('missing file ' + currentFile)
        else:
            found = False
            with open(currentFile) as fp:
                # lineNo = 0
                for line in fp:
                    #lineNo += 1
                    #if lineNo == 126:
                    #    lineNo = 126
                    if type(expected) == str:
                        if line.find(expected) >= 0:
                            found = True
                            break
                    else:
                        if expected.search(line) is not None:
                            found = True
                            break
            if not found:
                text = expected if type(expected) == str else expected.pattern
                rc = self.error('missing content {:s} in {:s}'.format(text[0:20], currentFile))
        return rc

    def assertFileExists(self, filename):
        '''Tests whether a given file exists.
        @param filename: the name of the file to test
        @returns: True: the file exists
        '''
        rc = True
        self._asserts += 1
        if not os.path.exists(filename):
            rc = self.error('file does not exist: ' + filename)
        return rc

    def assertFileNotContains(self, unexpectedContent, currentFile):
        '''Tests whether a given file contains not a given content.
        @param unexpectedContent: content to search
        @param currentFile: file to inspect
        '''
        rc = True
        self._asserts += 1
        if not os.path.isfile(currentFile):
            rc = self.error('missing file ' + currentFile)
        else:
            found = False
            with open(currentFile) as fp:
                for line in fp:
                    if line.find(unexpectedContent) >= 0:
                        found = True
                        break
            if found:
                rc = self.error('unexpected content {:s} in {:s}'.format(unexpectedContent[0:20], currentFile))
        return rc

    def assertFileNotExists(self, filename):
        '''Tests whether a given file does not exist.
        @param filename: the name of the file to test
        @returns: True: the file does not exist
        '''
        rc = True
        self._asserts += 1
        if os.path.exists(filename):
            rc = self.error('file exists: ' + filename)
        return rc

    def assertMatches(self, expectedRegExpr, current, flags=0):
        '''Tests whether a string matches a given regular expression.
        @param expectedRegExpr: regular expression
        @param current: string to test
        @param flags: flags for re.match, e.g. re.IGNORECASE or re.MULTILINE
        @returns: True: the value matches the expected
        '''
        rc = True
        self._asserts += 1
        if not re.search(expectedRegExpr, current, flags):
            rc = self.error('+++ does not match\n' + expectedRegExpr + '\n' + current)
        return rc

    def assertNodeExists(self, path, nodePattern):
        '''Tests whether at least one file exists in a given path with a given node pattern.
        @param path: the directory to inspect
        @param nodePattern: a pattern with unix wildcards to inspect
        @return True: node found
        '''
        nodes = os.listdir(path)
        found = False
        for node in nodes:
            if fnmatch.fnmatch(node, nodePattern):
                found = True
                break
        if not found:
            self.error('node {} not found in {}'.format(nodePattern, path))
        return found

    def assertNone(self, current):
        '''Tests whether a value is None.
        @param current: value to test
        @returns: True: the value is None
        '''
        rc = True
        self._asserts += 1
        if current is not None:
            rc = self.error('+++ None expected, found: ' + str(current))
        return rc

    def assertNotNone(self, current):
        '''Tests whether a value is not None.
        @param current: value to test
        @returns: True: the value is not None
        '''
        self._asserts += 1
        if current is None:
            self.error('+++ unexpected None found')

    def assertTrue(self, current):
        '''Tests whether a value is True.
        @param current: value to test
        @returns: True: the value is True
        '''
        rc = True
        self._asserts += 1
        if current != True:
            rc = self.error('+++ True expected, found: ' + str(current))
        return rc

    def clearDirectory(self, path):
        '''Removes all files and subdirs in a given directory.
        @param path: name of the directory
        '''
        base.FileHelper.clearDirectory(path)

    def ensureDirectory(self, directory):
        '''Ensures that the given directory exists.
        @param directory: the complete name
        @return: None: could not create the directory
            otherwise: the directory's name
        '''
        rc = base.FileHelper.ensureDirectory(directory)
        return rc

    def ensureFileDoesNotExist(self, filename):
        '''Ensures that a file does not exist.
        @param filename: the file to delete if it exists.
        '''
        base.FileHelper.ensureFileDoesNotExist(filename)

    def error(self, message):
        '''Displays an error with backtrace.
        @param message: error message
        @returns: False
        '''
        self._errors += 1
        print(message)
        info = traceback.extract_stack()
        # ignore runtime methods:
        while len(info) > 1 and (info[0].filename.find('/pysrc/') > 0 or info[0]._line.startswith('tester.run()')
                or info[0].filename.find('UnitTestCase.py') > 0 and info[0]._line.startswith('method()')):
            del info[0]
        # ignore UnitTest internals:
        length = len(info)
        while length > 1 and info[length - 1].filename.find('UnitTestCase.py') > 0:
            del info[length - 1]
            length -= 1
        for entry in info:
            print('{:s}:{:d} {:s}'.format(entry.filename, entry.lineno, entry.line))
        return False

    def getSummary(self):
        '''Return the summary message.
        @return the summary message
        '''
        return self._summary

    def log(self, message):
        '''Displays a message
        @param message: message
        '''
        print(message)

    def run(self):
        '''Searches the methods starting with 'test' and call them.
        '''
        for item in self.__dir__():
            if item.startswith('test'):
                method = getattr(self, item)
                print('= ' + item)
                method()
        self._summary = '=== unit {:s}: {:d} assert(s) with {:d} error(s)'.format(self._name, self._asserts, self._errors)
        print(self._summary)
        self._finish()

    def setInTestSuite(self, value):
        '''Sets the attribute.
        @param value: the new value of _inTestSuite
        '''
        self._inTestSuite = value

    def tempDir(self, node, subdir = None):
        '''Builds the name of a directory and ensures that the directory exists.
        @param node: the directory's name (without path)
        @param subdir: None or the name of a directory inside the temp dir
        @return: None: cannot create directory
                otherwise: the name of an existing temporary directory (with path)
        '''
        rc = tempfile.gettempdir()
        if subdir is not None:
            rc += os.sep + subdir
        if node is not None:
            if rc[-1] != os.sep:
                rc += os.sep
            rc += node
        rc = self.ensureDirectory(rc)
        return rc

    def tempFile(self, node, subdir = None, subdir2 = None):
        '''Builds the name of a temporary file and ensures that the parent directories exist.
        @param node: the file's name (without path)
        @param subdir: None or the name of a directory inside the temp dir
        @param subdir2: None or the name of a directory inside subdir
        @return: the name of a temporary file (with path), e.g. /tmp/subdir/subdir2/node
        '''
        rc = tempfile.gettempdir() + os.sep
        if subdir is not None:
            rc += subdir
            rc = self.ensureDirectory(rc)
            if subdir[-1] != os.sep:
                rc += os.sep
            if subdir2 is not None:
                rc += subdir2
                self.ensureDirectory(rc)
                if subdir2[-1] != os.sep:
                    rc += os.sep
        rc += node
        return rc

    def usage(self, message):
        print(message)
        self.assertFalse(True)

    def xtestMyself(self):
        self.assertIsEqual(3, 4)
        self.assertIsEqual('Hello', 'Hallo')
        self.assertIsEqual('abc\nhallo', 'abc\nhello')
        self.assertNone('not none')
        self.assertNotNone(None)

if __name__ == '__main__':
    tester = UnitTestCase()
    tester.run()


import subprocess
from pathlib import Path

import pandas as pd
from Bio import SeqIO

def psiblast(fasta_path, out_xml_path, blast_db, iteration='3', max_sequence=10000):
    cmd = ['psiblast', '-query', fasta_path, '-db', blast_db, '-out', out_xml_path,
           '-num_iterations', iteration, '-outfmt', '5', '-evalue', '1.0e-3',
           '-max_target_seqs', str(max_sequence)]
    subprocess.run(cmd)

def main():
    multidomain_csv_path = '../../../pisces/20210225/multidomain_cullpdb_pc20_res2.0_R0.25.csv'
    multidomain_df = pd.read_csv(multidomain_csv_path)
    pdb_fasta_path = '../../../PDBseq/pdb_seqres.txt'
    out_fasta_dir = Path('../../../pisces/20210225/multi-domain_fasta')
    out_fasta_dir.mkdir(parents=True, exist_ok=True)
    out_xml_dir = Path('../../../pisces/20210225/multi-domain_blast')
    out_xml_dir.mkdir(parents=True, exist_ok=True)
    blast_db_path = '../../../../../blastdb/pdbaa_20210407/pdbaa'
    records_dict = SeqIO.to_dict(SeqIO.parse(pdb_fasta_path, 'fasta'))

    for index, rows in multidomain_df.iterrows():
        seq_id = rows['PDB_ID'].lower() + '_' + rows['Chain']
        out_fasta_path = (out_fasta_dir / seq_id.upper()).with_suffix('.fasta')
        out_xml_path = (out_xml_dir / seq_id.upper()).with_suffix('.xml')
        if out_fasta_path.exists() and out_xml_path.exists():
            continue
        try:
            print(seq_id)
            seq = records_dict[seq_id]
        except KeyError:
            print(seq_id)
        else:
            SeqIO.write(seq, out_fasta_path, 'fasta')
            psiblast(out_fasta_path, blast_db_path, out_xml_path)

if __name__ == '__main__':
    main()


from django.urls import path

from . import views

app_name = 'stores'

urlpatterns = [
    path('', views.store_list, name='store_list'),
    path('new/', views.store_create, name='store_create'),
    path('<int:pk>', views.store_detail, name='store_detail'),
    path('<int:pk>/update/', views.store_update, name='store_update'),
    path('<int:pk>/delete/', views.store_delete, name='store_delete'),
]


import toml
import requests
import time
import asyncio

async def main():
    config = toml.load("config.toml")

    while True:
        time.sleep(int(config['serverCounter']['cooldown']))

        url = '{}/total'.format(config['serverCounter']['baseUrl'])
        res = requests.get(url).json()
        count = res['count']

        await updateDbots(config, count)
        await updateDblCom(config, count)
        await updateDiscordBoats(config, count)

async def updateDbots(config, count):
    url = '{}/bots/{}/stats'.format(config['botlists']['dbotsorg']['baseUrl'], config['id'])
    data = {'server_count': count}
    headers = {'Authorization': config['botlists']['dbotsorg']['apiKey']}
    requests.post(url, data=data, headers=headers)

async def updateDblCom(config, count):
    url = '{}/bots/{}/stats'.format(config['botlists']['dblcom']['baseUrl'], config['id'])
    data = {'guilds': count}
    headers = {'Authorization': 'Bot {}'.format(config['botlists']['dblcom']['token'])}
    requests.post(url, data=data, headers=headers)

async def updateDiscordBoats(config, count):
    url = '{}/bot/{}'.format(config['botlists']['discordboats']['baseUrl'], config['id'])
    data = {'server_count': count}
    headers = {'Authorization': config['botlists']['discordboats']['apiKey']}
    requests.post(url, data=data, headers=headers)

asyncio.run(main())


import requests
from bs4 import BeautifulSoup
import os
import re

art = []
link = []

number_of_pages = int(input())
type_of_articles = input()
dir = os.getcwd()
for cycle in range(1, number_of_pages + 1):

    try:
        os.mkdir(f'Page_{cycle}')
    except FileExistsError:
        pass
    os.chdir(dir + f'\\Page_{cycle}')
    r = requests.get(f'https://www.nature.com/nature/articles?searchType=journalSearch&sort=PubDate&page={cycle}')
    if r.status_code == 200:
        data = BeautifulSoup(r.content, 'html.parser')
        tag_news = data.find_all('article')   # data.find_all('span', {'data-test': 'article.type'})

        for i in tag_news:
            if i.find('span', class_="c-meta__type").text == type_of_articles:
                link = "https://www.nature.com" + i.find('a', {"data-track-action": "view article"}).get("href")

                new_article = requests.get(link)

                article = BeautifulSoup(new_article.content, 'html.parser')
                try:
                    article_title = article.find('h1', {'class': 'article-item__title'}).text
                except AttributeError:
                    article_title = article.find('h1', {'class': 'c-article-title'}).text

                punctuation = r"―!\"#$%&'()*+,-./:;<=>?@[\]_^`{|}~"
                article_title = "".join(["_" if t == " " else t if t not in punctuation else "" for t in article_title])
                regex = re.compile(".*body.*")

                article_body = article.find('div', attrs={'class': regex}).text

                article_body = article_body.strip()
                with open(article_title + '.txt', 'wb') as file:
                    file.write(str.encode(article_body))
    os.chdir(dir)


import os
import uuid
from os.path import basename

from flask import Blueprint
from flask import current_app
from flask import render_template
from flask import request
from werkzeug.utils import redirect
from sample.storage_mock import get_sample_doc_name, SampleDocs, create_app_data

# 26-08-2022
# By further inspecting in the latest Blueprint documentation (https://flask.palletsprojects.com/en/2.2.x/api/#blueprint-objects),
# when creating a Blueprint object, the first parameter (name) is prepend to the URL endpoint. Therefore, Blueprint no longer
# allows dots in the name since it would break the URL entirely.
__name__ = __name__.replace(".","/")
blueprint = Blueprint(basename(__name__), __name__,
                      url_prefix='/server-files')

@blueprint.route('/<rc>/<op>', methods=['GET', 'POST'])
def index(rc, op):
    if request.method == 'GET':
        if op == "cosignCms":
            available_files = [ServerFileModel(SampleDocs.CMS_SIGNED_ONCE, "A sample CMS file that was signed once."), ServerFileModel(SampleDocs.CMS_SIGNED_TWICE, "A sample CMS file that was signed twice.")]
        elif op == "cosignPdf" or op =="printerFriendlyPdf":
            available_files = [ServerFileModel(SampleDocs.PDF_SIGNED_ONCE, "A sample PDF that was signed once."), ServerFileModel(SampleDocs.PDF_SIGNED_TWICE, "A sample PDF that was signed twice.")]
        elif op == "signCms" or op == "signPdf":
            available_files = [ServerFileModel(SampleDocs.SAMPLE_PDF, "A sample PDF file to be signed.")]
        else:
            return render_template('error.html', msg='Invalid Operation')

        return render_template('server_files/index.html', rc=rc, available_files=available_files)
    else:
        sample_id = int(request.form['selectedFile'])
        filename = get_sample_doc_name(sample_id)
        file_extension = filename.rsplit('.', 1)[1]

        # Copy file to the App_Data folder, where the upload files is stored.
        with open(os.path.join(current_app.static_folder, filename), 'rb') as f:
            sample_file = f.read()
        # Generate a unique filename.
        file_id = '%s.%s' % (str(uuid.uuid4()), file_extension)

        # Move the file to the "app_data" with the unique filename. Make sure
        # the "app_data" folder exists (static/util.py).
        create_app_data()
        with open(os.path.join(current_app.config['APPDATA_FOLDER'], file_id), 'wb') as f:
            f.write(sample_file)

        # Redirect the user to the signature route, passing the name of the file as
        # a URL argument.
        redirect_url = '../../' + rc
        if op == "cosignCms":
            redirect_url += '/cosign/' + file_id
        else:
            redirect_url += '/' + file_id

        return redirect(redirect_url)

class ServerFileModel:
    def __init__(self, id, description):
        self.id = id
        self.description = description
        self.download_url = '/download/sample/' + str(id)


__doc__ = """
Investigate the temperature relaxation of 1D MOT.
"""

import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
from scipy.interpolate import Akima1DInterpolator

import numpy as np
from tqdm import tqdm

class MOT1DModel:
    def __init__(self, s, delta, b):
        """
        Parameters:
        s: normalized intensity
        delta: normalized detuning
        b: coefficient before x
        """

        def prob1(x, u):
            tmp = u + b * x
            return s / (1 + s + 4 * (delta - tmp)**2) * 1.6  # counter

        def prob2(x, u):
            tmp = u + b * x
            return s / (1 + s + 4 * (delta + tmp)**2) * 1.6

        self.prob1 = prob1
        self.prob2 = prob2

    def simulate(self, u0, step, tot_steps=2000000):
        """
        In a thermodynamic setup, the atoms are assumed to located at the origin;

        The SDE is solved with vanilla Euler method
        """

        ys = [0]
        us = [u0]

        cnt = 0
        for _ in tqdm(range(tot_steps)):
            delta_u = 0
            # scatter in 2D
            # # scatter a photon with -k
            # if np.random.rand() < self.prob1(ys[-1], us[-1]) * step:
            #     delta_u += +1/1.6 * (1 + np.cos(np.random.random()*np.pi))
            # # scatter a photon with +k
            # if np.random.rand() < self.prob2(ys[-1], us[-1]) * step:
            #     delta_u += -1/1.6 * (1 + np.cos(np.random.random()*np.pi))

            # pure 1D
            # scatter a photon with -k
            if np.random.rand() < self.prob1(ys[-1], us[-1]) * step /2 :
                delta_u += +1/1.6
                cnt += 1
            # scatter a photon with +k
            if np.random.rand() < self.prob2(ys[-1], us[-1]) * step / 2:
                delta_u += -1/1.6
                cnt += 1

            ys.append(ys[-1] + us[-1] * step)
            us.append(us[-1] + delta_u)

        print('Scattered rate:', cnt / tot_steps)
        return np.array(ys), np.array(us)

def binning(x, max_bin=400):
    def mean_every_n(arr, n):
        return np.mean(arr[:(len(arr)//n)*n].reshape(-1, n), axis=1)
    return np.array([np.std(mean_every_n(x, b)) for b in range(1, max_bin)])

model = MOT1DModel(284, -16, .048)
ys, us = model.simulate(0, .05)

# plt.plot(us)
# plt.show()

def autocorr(x, t=1):
    return np.corrcoef(np.array([x[:-t], x[t:]]))[0, 1]

burnin = 5000
ys = ys[burnin:]
us = us[burnin:]
print(us[:1000])
plt.hist2d(us, ys,bins=10)
# plt.plot(*np.unique(us, return_counts=True),'+')
plt.xlabel('$x$')
plt.ylabel('$u$')
plt.tight_layout()
plt.show()
lags = list(range(1, 5000, 20))
plt.plot(lags, [autocorr(us, l) for l in tqdm(lags)])
plt.show()

ys = ys[::1000]
us = us[::1000]

print(np.mean(us**2))
plt.plot(binning(us**2))
plt.show()