#!/usr/bin/python3
"""
This script extracts coding sequence information from the prokka
genome annotations to produce a bed file and tsv file with those
coding sequences for use in downstream analysis.
"""
import argparse
import csv

import pandas as pd

import paths

parser = argparse.ArgumentParser(
    description="Extract coding sequences from prokka annotations")
parser.add_argument("--species", nargs=1, required=True,
    help="species being analyzed, a .tsv file")
parser.add_argument("--output-prefix", nargs=1, required=True,
    help="directory for outputs, windows are written to <output-prefix>/{prokka_annot/<species>.coding_seqs.bed,all_prokka_genomes_name_conversion.tsv}")
args = parser.parse_args()

outputs = paths.Outputs(args.output_prefix[0])
species =  pd.read_csv(args.species[0], delimiter='\t')

class gff_file:
    def __init__(self, filename):
        self.n = filename
        self.f = open(filename, 'r')
        self.insequence = False

    def __iter__(self):
        return self

    def __next__(self):
        line = next(self.f).strip()

        # skip all lines that are not sequence-regions.
        while line.startswith('#') or len(line) == 0 or not self.insequence:
            if line.startswith('##sequence-region'):
                self.insequence = True
            elif line.startswith('##'):
                self.insequence = False
            line = next(self.f).strip()
            pass

        parts = line.split('\t')
        # ignore any non CDS lines.
        while parts[2] != "CDS":
            line = next(self.f).strip()
            parts = line.split('\t')

        comment = parts[8]
        annotations = dict(tmp.split("=") for tmp in parts[8].split(";"))
        if 'gene' not in annotations:
            annotations['gene'] = "NA"

        return {
            'contig': parts[0],
            'start': int(parts[3])-1,
            'end': int(parts[4]),
            'strand': parts[6],
            'annotations': annotations
        }

def annotations_for_species(sp, conversions_tsv):
    """
    Write the bed file for each species and append the
    annotations for this species to conversions_tsv.
    """
    files = outputs.prokka_gff_files(sp)
    gf = gff_file(files[0])
    bedfile = open(outputs.prokka_bed_file(sp), 'w')
    bedfile_tsv = csv.writer(bedfile, delimiter='\t')
    for l in gf:
        conversions_tsv.writerow([
            l['annotations']['ID'],
            l['start'],
            l['end'],
            l['annotations']['gene'],
            l['annotations']['product']])
        bedfile_tsv.writerow([l['contig'], l['start'], l['end'], ".", l['annotations']['ID'], l['strand']])
    bedfile.close()

conversions = open(outputs.prokka_name_conversions(), 'w')
conversions_tsv = csv.writer(conversions, delimiter='\t')
conversions_tsv.writerow(['id', 'start', 'end', 'gene', 'product'])

species.apply(lambda row: annotations_for_species(row.species, conversions_tsv), axis='columns')

conversions.close()


from pathlib import Path
import json

from meetings.authenticate import google_credentials
from meetings.request import calendar_service, get_events

if __name__ == '__main__':
    events_json = Path("events.json")

    if not events_json.exists():
        creds = google_credentials()
        service = calendar_service(creds)
        events = get_events(service)
        json.dump(events_json.open("w"), events)

    import datetime
    import altair as alt
    from dateutil import parser
    import pandas as pd

    def _transform(row):
        if "dateTime" in row:
            return parser.parse(row["dateTime"])
        elif "date" in row:
            return parser.parse(row["date"])
        return None

    df = pd.read_json(events_json)
    df["start_time"] = df["start"].apply(_transform)
    df["end_time"] = df["end"].apply(_transform)
    df["duration"] = df["end_time"] - df["start_time"]
    df["date"] = df["start_time"].apply(lambda row: row.date())
    # df["week"] = df["date"].apply(lambda row: row.strftime("%V"))
    df["iso_week"] = df["date"].apply(lambda row: row.isocalendar()[1])
    df["iso_year"] = df["date"].apply(lambda row: row.isocalendar()[0])
    df["month"] = df["date"].apply(lambda row: row.strftime("%m"))
    df["year"] = df["date"].apply(lambda row: row.strftime("%Y"))

    df = df[["summary", "date", "iso_week", "month", "iso_year", "duration"]]
    df = df[df["duration"] < datetime.timedelta(hours=10)]

    exclude_list = {*[
        keyword.lower()
        for keyword in [
            "Emily School Zoom", "Coffee break", "Meditation", "meditation", "out of office",
            "Lunchtime catch up", "holiday", "School Collection", "home school"
        ]
    ]}

    def _filter(summary):
        for keyword in exclude_list:
            if keyword in summary.lower():
                return False
        return True

    ix = df["summary"].apply(_filter)
    df = df[ix]

    results = pd.DataFrame(
        data=[
            (year, week, x["duration"].sum())
            for (year, week), x in df.sort_values(by="date").groupby(by=["iso_year", "iso_week"])
        ],
        columns=["year", "week", "total"]
    )
    results["hours"] = results["total"].apply(lambda row: row.total_seconds() / (60 * 60))
    results["year-week"] = results["year"].astype(str) + "-" + results["week"].astype(str)
    results["hours-mean"] = results["hours"].rolling(4).mean()
    results = results.drop(labels="total", axis=1)

    # results = results[results["year"] != "2021"]

    chart = alt.Chart(results).mark_area(opacity=0.3).encode(
        x="week",
        y="hours-mean:Q",
        # y=alt.Y("hours-mean:Q", stack=None),
        row="year:N",
        color="year:N",
    ).properties(
        height=100,
        width=800
    )

    chart.save('filename.html')


# !/usr/bin/env python
# encoding: utf-8

"""
file: http_aioget.py
time: 2019/6/19 14:47
Author: twy
contact: 19983195362
des: 获取图片的异步请求类
"""

import aiohttp
import asyncio
import random
import time
import json
from spider.utils import utilsMixin

class HttpAioget(utilsMixin):
    def __init__(self, burst=100):
        super().__init__()
        self.burst = burst
        self.headers_list = [{"User-Agent": random.choice(self.gen_random_ua())}]

    async def aioget(self, title_url):
        _title = list(title_url.keys())[0]
        _url = list(title_url.values())[0]
        async with aiohttp.ClientSession() as session:
            async with session.get(_url, headers=random.choice(self.headers_list)) as response:
                _img_data = await response.read()
                # crawled_url是一个用于去重的哈希
                self.redis_con.hset("crawled_url", _url, random.randint(0, 123456789))
                self.redis_con.hset("img_data_list", _title, _img_data)

    def _http_get(self, url_list):
        try:
            loop = asyncio.get_event_loop()
            task_list = [asyncio.ensure_future(self.aioget(url)) for url in url_list]
            loop.run_until_complete(asyncio.wait(task_list))
        except Exception as e:
            print(e)

    def run(self):
        print("http请求进程启动.......")
        while True:
            _to_crawl_list = self.redis_con.lrange("to_crawl_data", 0, self.burst)
            if _to_crawl_list:
                _url_list = []
                for each in _to_crawl_list:
                    _title_url = json.loads(each)
                    _title = list(_title_url.keys())[0]
                    _url = list(_title_url.values())[0]
                    if not self.redis_con.hget("crawled_url", _url):
                        _url_list.append({_title: _url})
                if self.redis_con.llen("to_crawl_data") > 1:
                    self.redis_con.ltrim("to_crawl_data", self.burst, -1)
                else:
                    _url = self.redis_con.delete("to_crawl_data")
                self._http_get(_url_list)
            print("http请求休眠5秒！！！！")
            time.sleep(5)

http_aioget = HttpAioget()

if __name__ == "__main__":
    h = HttpAioget()
    h.run()


import sys
from collections import deque

n,m = map(int,sys.stdin.readline().split(' '))
a = list(map(int, sys.stdin.readline().split(' ')))

Q = deque([_ for _ in range(1,n+1)])

cnt = 0
for a in a:
    before_len = Q.index(a)
    after_len = n - Q.index(a)
    if before_len<after_len:
        cnt += before_len
        Q.rotate(-before_len)
    else:
        cnt += after_len
        Q.rotate(after_len)
    Q.popleft()
    n -= 1

print(cnt)

# 10 3
# 2 9 5

# 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
# 2, 3, 4, 5, 6, 7, 8, 9, 10, 1  << 1 1
# 3, 4, 5, 6, 7, 8, 9, 10, 1
# 9, 10, 1, 3, 4, 5, 6, 7, 8 >> 3     3
# 10, 1, 3, 4, 5, 6, 7, 8
# 5, 6, 7, 8, 10, 1, 3, 4 >> 4        4


import os
import filecmp
import time

import pytest
import numpy as np

from damask import VTK
from damask import grid_filters

@pytest.fixture
def reference_dir(reference_dir_base):
    """Directory containing reference results."""
    return reference_dir_base/'VTK'

@pytest.fixture
def default():
    """Simple VTK."""
    grid = np.array([5,6,7],int)
    size = np.array([.6,1.,.5])
    return VTK.from_rectilinear_grid(grid,size)

class TestVTK:

    @pytest.fixture(autouse=True)
    def _execution_stamp(self, execution_stamp):
        print('patched damask.util.execution_stamp')

    def test_rectilinearGrid(self,tmp_path):
        grid   = np.random.randint(5,10,3)*2
        size   = np.random.random(3) + 1.0
        origin = np.random.random(3)
        v = VTK.from_rectilinear_grid(grid,size,origin)
        string = v.__repr__()
        v.save(tmp_path/'rectilinearGrid',False)
        vtr = VTK.load(tmp_path/'rectilinearGrid.vtr')
        with open(tmp_path/'rectilinearGrid.vtk','w') as f:
            f.write(string)
        vtk = VTK.load(tmp_path/'rectilinearGrid.vtk','VTK_rectilinearGrid')
        assert(string == vtr.__repr__() == vtk.__repr__())

    def test_polyData(self,tmp_path):
        points = np.random.rand(100,3)
        v = VTK.from_poly_data(points)
        string = v.__repr__()
        v.save(tmp_path/'polyData',False)
        vtp = VTK.load(tmp_path/'polyData.vtp')
        with open(tmp_path/'polyData.vtk','w') as f:
            f.write(string)
        vtk = VTK.load(tmp_path/'polyData.vtk','polyData')
        assert(string == vtp.__repr__() == vtk.__repr__())

    @pytest.mark.parametrize('cell_type,n',[
                                            ('VTK_hexahedron',8),
                                            ('TETRA',4),
                                            ('quad',4),
                                            ('VTK_TRIANGLE',3)
                                            ]
                            )
    def test_unstructuredGrid(self,tmp_path,cell_type,n):
        nodes = np.random.rand(n,3)
        connectivity = np.random.choice(np.arange(n),n,False).reshape(-1,n)
        v = VTK.from_unstructured_grid(nodes,connectivity,cell_type)
        string = v.__repr__()
        v.save(tmp_path/'unstructuredGrid',False)
        vtu = VTK.load(tmp_path/'unstructuredGrid.vtu')
        with open(tmp_path/'unstructuredGrid.vtk','w') as f:
            f.write(string)
        vtk = VTK.load(tmp_path/'unstructuredGrid.vtk','unstructuredgrid')
        assert(string == vtu.__repr__() == vtk.__repr__())

    def test_parallel_out(self,tmp_path):
        points = np.random.rand(102,3)
        v = VTK.from_poly_data(points)
        fname_s = tmp_path/'single.vtp'
        fname_p = tmp_path/'parallel.vtp'
        v.save(fname_s,False)
        v.save(fname_p,True)
        for i in range(10):
            if os.path.isfile(fname_p) and filecmp.cmp(fname_s,fname_p):
                assert(True)
                return
            time.sleep(.5)
        assert(False)

    @pytest.mark.parametrize('fname',['a','a.vtp','a.b','a.b.vtp'])
    def test_filename_variations(self,tmp_path,fname):
        points = np.random.rand(102,3)
        v = VTK.from_poly_data(points)
        v.save(tmp_path/fname)

    @pytest.mark.parametrize('name,dataset_type',[('this_file_does_not_exist.vtk', None),
                                                  ('this_file_does_not_exist.vtk','vtk'),
                                                  ('this_file_does_not_exist.vtx', None)])
    def test_invalid_dataset_type(self,name,dataset_type):
        with pytest.raises(TypeError):
            VTK.load(name,dataset_type)

    def test_add_extension(self,tmp_path,default):
        default.save(tmp_path/'default.txt',parallel=False)
        assert os.path.isfile(tmp_path/'default.txt.vtr')

    def test_invalid_get(self,default):
        with pytest.raises(ValueError):
            default.get('does_not_exist')

    def test_invalid_add_shape(self,default):
        with pytest.raises(ValueError):
            default.add(np.ones(3),'valid')

    def test_invalid_add_missing_label(self,default):
        data = np.random.randint(9,size=np.prod(np.array(default.vtk_data.GetDimensions())-1))
        with pytest.raises(ValueError):
            default.add(data)

    def test_invalid_add_type(self,default):
        with pytest.raises(TypeError):
            default.add('invalid_type','valid')

    def test_comments(self,tmp_path,default):
        default.add_comments(['this is a comment'])
        default.save(tmp_path/'with_comments',parallel=False)
        new = VTK.load(tmp_path/'with_comments.vtr')
        assert new.get_comments() == ['this is a comment']

    def test_compare_reference_polyData(self,update,reference_dir,tmp_path):
        points=np.dstack((np.linspace(0.,1.,10),np.linspace(0.,2.,10),np.linspace(-1.,1.,10))).squeeze()
        polyData = VTK.from_poly_data(points)
        polyData.add(points,'coordinates')
        if update:
             polyData.save(reference_dir/'polyData')
        else:
             reference = VTK.load(reference_dir/'polyData.vtp')
             assert polyData.__repr__() == reference.__repr__() and \
                    np.allclose(polyData.get('coordinates'),points)

    def test_compare_reference_rectilinearGrid(self,update,reference_dir,tmp_path):
        grid = np.array([5,6,7],int)
        size = np.array([.6,1.,.5])
        rectilinearGrid = VTK.from_rectilinear_grid(grid,size)
        c = grid_filters.cell_coord0(grid,size).reshape(-1,3,order='F')
        n = grid_filters.node_coord0(grid,size).reshape(-1,3,order='F')
        rectilinearGrid.add(c,'cell')
        rectilinearGrid.add(n,'node')
        if update:
             rectilinearGrid.save(reference_dir/'rectilinearGrid')
        else:
             reference = VTK.load(reference_dir/'rectilinearGrid.vtr')
             assert rectilinearGrid.__repr__() == reference.__repr__() and \
                    np.allclose(rectilinearGrid.get('cell'),c)


import minimalmodbus

minimalmodbus.TIMEOUT = 0.5

class RD6006:
    def __init__(self, port, address=1, baudrate=115200):
        self.port = port
        self.address = address
        self.instrument = minimalmodbus.Instrument(port=port, slaveaddress=address)
        self.instrument.serial.baudrate = baudrate
        regs = self._read_registers(0, 4)
        self.sn = regs[1] << 16 | regs[2]
        self.fw = regs[3] / 100
        self.type = int(regs[0] / 10)

        if self.type == 6012 or self.type == 6018 or self.type == 6024:
            print("RD6012, RD6018 or RD6024 detected")
            self.voltres = 100
            self.ampres = 100
        else:
            print("RD6006 or other detected")
            self.voltres = 100
            self.ampres = 1000

    def __repr__(self):
        return f"RD6006 SN:{self.sn} FW:{self.fw}"

    def _read_register(self, register):
        try:
            return self.instrument.read_register(register)
        except minimalmodbus.NoResponseError:
            return self._read_register(register)

    def _read_registers(self, start, length):
        try:
            return self.instrument.read_registers(start, length)
        except minimalmodbus.NoResponseError:
            return self._read_registers(start, length)
        except minimalmodbus.InvalidResponseError:
            return self._read_registers(start, length)

    def _write_register(self, register, value):
        try:
            return self.instrument.write_register(register, value)
        except minimalmodbus.NoResponseError:
            return self._write_register(register, value)

    def _mem(self, M=0):
        """reads the 4 register of a Memory[0-9] and print on a single line"""
        regs = self._read_registers(M * 4 + 80, 4)
        print(
            f"M{M}: {regs[0] / self.voltres:4.1f}V, {regs[1] / self.ampres:3.3f}A, OVP:{regs[2] / self.voltres:4.1f}V, OCP:{regs[3] / self.ampres:3.3f}A"
        )

    def status(self):
        regs = self._read_registers(0, 84)
        print("== Device")
        print(f"Model   : {regs[0]/10}")
        print(f"SN      : {(regs[1]<<16 | regs[2]):08d}")
        print(f"Firmware: {regs[3]/100}")
        print(f"Input   : {regs[14] / self.voltres}V")
        if regs[4]:
            sign = -1
        else:
            sign = +1
        print(f"Temp    : {sign * regs[5]}°C")
        if regs[34]:
            sign = -1
        else:
            sign = +1
        print(f"TempProb: {sign * regs[35]}°C")
        print("== Output")
        print(f"Voltage : {regs[10] / self.voltres}V")
        print(f"Current : {regs[11] / self.ampres}A")
        print(f"Energy  : {regs[12]/1000}Ah")
        print(f"Power   : {regs[13]/100}W")
        print("== Settings")
        print(f"Voltage : {regs[8] / self.voltres}V")
        print(f"Current : {regs[9] / self.ampres}A")
        print("== Protection")
        print(f"Voltage : {regs[82] / self.voltres}V")
        print(f"Current : {regs[83] / self.ampres}A")
        print("== Battery")
        if regs[32]:
            print("Active")
            print(f"Voltage : {regs[33] / self.voltres}V")
        print(
            f"Capacity: {(regs[38] <<16 | regs[39])/1000}Ah"
        )  # TODO check 8 or 16 bits?
        print(
            f"Energy  : {(regs[40] <<16 | regs[41])/1000}Wh"
        )  # TODO check 8 or 16 bits?
        print("== Memories")
        for m in range(10):
            self._mem(M=m)

    @property
    def input_voltage(self):
        return self._read_register(14) / self.voltres

    @property
    def voltage(self):
        return self._read_register(8) / self.voltres

    @property
    def meastemp_internal(self):
        if self._read_register(4):
            return -1 * self._read_register(5)
        else:
            return 1 * self._read_register(5)

    @property
    def meastempf_internal(self):
        if self._read_register(6):
            return -1 * self._read_register(7)
        else:
            return 1 * self._read_register(7)

    @property
    def meastemp_external(self):
        if self._read_register(34):
            return -1 * self._read_register(35)
        else:
            return 1 * self._read_register(35)

    @property
    def meastempf_external(self):
        if self._read_register(36):
            return -1 * self._read_register(37)
        else:
            return 1 * self._read_register(37)

    @voltage.setter
    def voltage(self, value):
        self._write_register(8, int(value * self.voltres))

    @property
    def measvoltage(self):
        return self._read_register(10) / self.voltres

    @property
    def meascurrent(self):
        return self._read_register(11) / self.ampres

    @property
    def measpower(self):
        return (
            self._read_register(12) << 16 | self._read_register(13)
        ) / 100

    @property
    def measah(self):
        return (
            self._read_register(38) << 16 | self._read_register(39)
        ) / 1000  # TODO check 16 or 8 bit

    @property
    def measwh(self):
        return (
            self._read_register(40) << 16 | self._read_register(41)
        ) / 1000  # TODO check 16 or 8 bit

    @property
    def battmode(self):
        return self._read_register(32)

    @property
    def battvoltage(self):
        return self._read_register(33)

    @property
    def current(self):
        return self._read_register(9) / self.ampres

    @current.setter
    def current(self, value):
        self._write_register(9, int(value * self.ampres))

    @property
    def voltage_protection(self):
        return self._read_register(82) / self.voltres

    @voltage_protection.setter
    def voltage_protection(self, value):
        self._write_register(82, int(value * self.voltres))

    @property
    def current_protection(self):
        return self._read_register(83) / self.ampres

    @current_protection.setter
    def current_protection(self, value):
        self._write_register(83, int(value * self.ampres))

    @property
    def enable(self):
        return self._read_register(18)

    @enable.setter
    def enable(self, value):
        self._write_register(18, int(value))

    @property
    def ocpovp(self):
        return self._read_register(16)

    @property
    def CVCC(self):
        return self._read_register(17)

    @property
    def backlight(self):
        return self._read_register(72)

    @backlight.setter
    def backlight(self, value):
        self._write_register(72, value)

    @property
    def date(self):
        """returns the date as tuple: (year, month, day)"""
        regs = self._read_registers(48, 3)
        year = regs[0]
        month = regs[1]
        day = regs[2]
        return (year, month, day)

    @date.setter
    def date(self, value):
        """Sets the date, needs tuple with (year, month, day) as argument"""
        year, month, day = value
        self._write_register(48, year)
        self._write_register(49, month)
        self._write_register(50, day)

    @property
    def time(self):
        """returns the time as tuple: (h, m, s)"""
        regs = self._read_registers(51, 3)
        h = regs[0]
        m = regs[1]
        s = regs[2]
        return (h, m, s)

    @time.setter
    def time(self, value):
        """sets the time, needs time with (h, m, s) as argument"""
        h, m, s = value
        self._write_register(51, h)
        self._write_register(52, m)
        self._write_register(53, s)

    @property
    def read_timeout(self):
        return self.instrument.serial.timeout

    @read_timeout.setter
    def read_timeout(self, value):
        self.instrument.serial.timeout = value

    @property
    def write_timeout(self):
        return self.instrument.serial.write_timeout

    @write_timeout.setter
    def write_timeout(self, value):
        self.instrument.serial.write_timeout = value

if __name__ == "__main__":
    import serial.tools.list_ports

    ports = list(serial.tools.list_ports.comports())
    for p in ports:
        if "VID:PID=1A86:7523" in p[2]:
            print(p)
            r = RD6006(p[0])
            break
    else:
        raise Exception("Port not found")
    r.status()


#!/usr/bin/env python3

from os import chdir, path
from optparse import OptionParser

from bottle import run
from buildhck import buildhck, config

def main():
    '''main method'''
    parser = OptionParser()
    parser.add_option('-s', '--server', dest='server',
                      help='bottle.py WSGI server backend')
    parser.add_option('-p', '--port', dest='port',
                      help='server port')
    parser.add_option('-b', '--buildsdir', dest='builds_directory',
                      help='directory for builds')
    args = parser.parse_args()[0]

    config.config.update({k:v for k,v in vars(args).items() if v})

    run(**config.config)

if __name__ == "__main__":
    main()


from libgenesis import Libgen
from pathlib import Path
import pytest

class Testclass:
    """Testing of Libgen object, searching, result types, and download of the books."""
    @staticmethod
    def test_Libgen():
        # create a Libgen object with return result limit set to 50
        lg = Libgen(sort="title", sort_mode="ASC", result_limit=50)
        assert isinstance(lg, Libgen)

    @staticmethod
    @pytest.mark.asyncio
    async def test_search():
        lg = Libgen()
        # search Libgen
        result = await lg.search('japan history')
        assert isinstance(result, dict)

    @staticmethod
    @pytest.mark.asyncio
    async def test_result():
        lg = Libgen()
        result = await lg.search('japan history')
        # check first result
        ids = [*result]
        assert isinstance(result[ids[0]], dict)

    @staticmethod
    @pytest.mark.asyncio
    async def test_download():
        lg = Libgen()
        result = await lg.search('japan history')
        ids = [*result]
        async def progress(current, total, test_arg, test2_arg):
            assert isinstance(current, int)
            assert isinstance(total, (int, str))
            assert isinstance(test_arg, Libgen) and test_arg.test == 'Test string'
            assert isinstance(test2_arg, int) and test2_arg == 123456
        # download the first result and print the path
        lg.test = 'Test string'
        file = await lg.download(result[ids[0]]['mirrors']['main'],
                                 dest_folder=Path("download_test"),
                                 progress=progress,
                                 progress_args=[
                                     lg, 123456
                                 ])
        assert Path.is_file(file)


import os
import re
import jieba
import numpy as np

absolute_file_name_list = []
sentence_list =[]
word_segment_list = []
vocabulary_list = []
word_to_index_map = {}
index_to_word_map = {}
train_data_set_list = []
# 测试产物，后可删除
temp_list = []

def get_file_list(data_path):
    file_list = os.listdir(data_path)
    # absolute_file_name_list = []
    for file_name in file_list:
        absolute_file_name = data_path + file_name
        absolute_file_name_list.append(absolute_file_name)

def get_sentence_from_file(file_name):
    with open(file_name,'rt') as f:
        data = f.read()
        temp_list = re.split(r"？”|！”|。”|\?”|!”|\.“|。|！|？|\.|!|\?|……", data)
        for sentence in temp_list:
            if sentence != '':
                sentence_list.append(sentence.replace('\n','').replace(u'\u3000',u''))

def word_segment(sentence):
    sentence_word_list = []
    temp = jieba.cut(sentence, cut_all=False)
    for word in temp:
        sentence_word_list.append(word)
    word_segment_list.append(sentence_word_list)

def get_vocabulary():
    for sentence_segment in word_segment_list:
        for word in sentence_segment:
            if word not in vocabulary_list:
                vocabulary_list.append(word)

def map_word_and_index():
    for index, word in enumerate(vocabulary_list):
        word_to_index_map[word] = index
        index_to_word_map[index] = word

def get_skip_gram_train_data_set(windows_size):
    for sentence in word_segment_list:
        for index, word in enumerate(sentence):
            for i in range(index - windows_size, index + windows_size + 1):
                if i >= 0 and i < len(sentence) and i != index:
                    target_index = word_to_index_map[word]
                    context_index = word_to_index_map[sentence[i]]
                    train_data_set_list.append((target_index, context_index))
                    # 测试产物，可删除
                    temp_list.append((word,sentence[i]))

def prepare_data(windows_size):
    data_path = "/Users/cly/PycharmProjects/Word2Vec/data/"
    get_file_list(data_path)

    for file_name in absolute_file_name_list:
        get_sentence_from_file(file_name)

    for sentence in sentence_list:
        word_segment(sentence)

    get_vocabulary()

    map_word_and_index()

    get_skip_gram_train_data_set(windows_size)

    # for sentence_segement in word_segment_list:
    #     print(sentence_segement)
    #
    # for (key, value) in word_to_index_map.items():
    #     print("word:", key, " index:", value)
    #
    # for (key,value) in index_to_word_map.items():
    #     print("key:", key, " value:", value)
    #
    # for element in temp_list:
    #     print(element)
    #
    # for element in train_data_set_list:
    #     print(element)

def generate_batch(batch_size):
    input = np.ndarray(shape=(batch_size), dtype=np.int32)
    label = np.ndarray(shape=(batch_size, 1), dtype=np.int32)
    train_data_length = len(train_data_set_list)
    # print("训练集长度：",train_data_length)
    random_list = np.random.randint(train_data_length,size=batch_size)
    # print("random_list:", random_list)
    for i, index in enumerate(random_list):
        input[i]   = train_data_set_list[index][0]
        label[i,0] = train_data_set_list[index][1]

    return input, label

if __name__ == "__main__":
    prepare_data(windows_size=3)
    input, label = generate_batch(batch_size=128)
    for index in range(128):
        print("input:",input[index], "\t label:",label[index])


from django.shortcuts import render,redirect
from django.contrib import messages
from minorproject import settings
from django.core.mail import send_mail
# here i make the au
from django.contrib.auth.models import User
from  django.contrib.auth.decorators import login_required
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.hashers import make_password
from joblib import load
# here i make the section where i load the model
kidn=load('./savedModelss/kidney.joblib')
park=load('./savedModelss/parkinsons.joblib')
diabs=load('./savedModelss/diabs.joblib')
hearts=load('./savedModelss/heart.joblib')
brainsmr=load('./savedModelss/brain.joblib')

# Create your views here.

def home(request):
          return render(request,"index.html")
@login_required(login_url="logins")
def brain(request):
          # here i write the backend code for brain dataset
          if request.method=='POST':

                      n5=request.POST.get('file')

                      y_pred=''
                      y_pred=brainsmr.predict([[n5]])
                      print(y_pred)
          return render(request,"brain_mri.html")

# here i write the concept of login
def logins(request):
          if request.method=='POST':
                    n1=request.POST.get('name')
                    n2=request.POST.get('email')
                    n3=request.POST.get("password")
                    user = authenticate(request,username=n1, password=n3)
                    request.session ['email'] =n2
                    if user is not None:
                              login(request,user)
                              return render(request,"index.html")
          return render(request,"login.html")

def signout(request):
              logout(request)
          #     messages.success(request,"successfully logout")
              return redirect(home)

def sign(request):
          if request.method=='POST':
                    n1=request.POST.get('name')
                    n2=request.POST.get('firstname')
                    n3=request.POST.get('lastname')
                    n4=request.POST.get('email')
                    n5=request.POST.get('password')
                    if User.objects.filter(username=n1):
                    #   messages.error(request,"usernam already exist")
                      return redirect(sign)
                    if User.objects.filter(email=n4):
                      messages.error(request,"email already already exist")
                      return redirect(sign)
                    user=User.objects.create_user(n1,n4)
                    user.set_password(n5)
                    user.first_name=n2
                    user.last_name=n3
                    user.save()
                    messages.success(request,"succesfully created user")
          return render(request,"sign.html")
@login_required(login_url="logins")
def diab(request):
          # here i write the backend code for diabetes code
          y_pred=''
          if request.method=='POST':
                    n1=request.POST.get('Pregnancies')
                    n2=request.POST.get('glucose')
                    n3=request.POST.get('blood Pressure')
                    n4=request.POST.get('skin thickness')
                    n5=request.POST.get('insulin')
                    n6=request.POST.get('BMI')
                    n7=request.POST.get('diabetespedigreefunction')
                    n8=request.POST.get('age')
                    y_pred=diabs.predict([[n1,n2,n3,n4,n5,n6,n7,n8]])
                    print(y_pred[0])
                    if y_pred[0]==0:
                              y_pred="nondisease"
                    else:
                              y_pred="disease"
                    email=request.session['email']
                    # here i set the email concept
                    subject = "Health Report"
                    message = "Hi Welcome to the Health prediction web application , First of all we thank you for using our services. your medical check up result for diabetes is"+ y_pred+"Thank you
 Have a nice day ahead."
                    from_email = settings.EMAIL_HOST_USER
                    to_email = [email]
                    send_mail(subject, message, from_email,to_email, fail_silently=False)

          return render(request,"diabetes.html",{"y":y_pred})
@login_required(login_url="logins")
def heart(request):
          # here i write the backend of the heart model
          y_pred=''
          if request.method=='POST':
                    n1=request.POST.get('age')
                    n2=request.POST.get('sex')
                    n3=request.POST.get('cp')
                    n4=request.POST.get('trestbps')
                    n5=request.POST.get('chol')
                    n6=request.POST.get('fbs')
                    n7=request.POST.get('restecg')
                    n8=request.POST.get('thalach')
                    n9=request.POST.get('exang')
                    n10=request.POST.get('oldpeak')
                    n11=request.POST.get('slope')
                    n12=request.POST.get('ca')
                    n13=request.POST.get('thal')
                    y_pred=hearts.predict([[n1,n2,n3,n4,n5,n6,n7,n8,n9,n10,n11,n12,n13]])
                    print(y_pred[0])
                    if y_pred[0]==0:
                              y_pred="non disease"
                    else:
                              y_pred="disease"
                    email=request.session['email']
                    # here i set the email concept
                    subject = "Health Report"
                    message = "Hi Welcome to the Health prediction web application , First of all we thank you for using our services. your medical check up result for heart disease is"+ y_pred+"Thank you
 Have a nice day ahead."
                    from_email = settings.EMAIL_HOST_USER
                    to_email = [email]
                    send_mail(subject, message, from_email,to_email, fail_silently=False)

          return render(request,"heart_disease.html",{"y":y_pred})
@login_required(login_url="logins")
def kidney(request):
          # now i start writing the backend of the kidney model
          y_pred=''
          if request.method=='POST':
                    age=request.POST.get('age')
                    bloodpressure=request.POST.get('bloodpressure')
                    specificgravity=request.POST.get('specificgravity')
                    Albumin=request.POST.get('Albumin')
                    Sugar=request.POST.get('Sugar')
                    BloodGlucoseRandom=request.POST.get('BloodGlucoseRandom')
                    Blood_Urea=request.POST.get('Blood Urea')
                    Serum_Creatinine=request.POST.get('Serum Creatinine')
                    Sodium=request.POST.get('Sodium')
                    Potassium=request.POST.get('Potassium')
                    Hemoglobin=request.POST.get('Hemoglobin')
                    Packed_Cell_Volume=request.POST.get('Packed Cell Volume')
                    White_Blood_Cells=request.POST.get('White Blood Cells')
                    Red_Blood_Cells=request.POST.get('Red Blood Cells')
                    Red_Blood_Cells_normal=request.POST.get('Red Blood Cells: normal')
                    Pus_Cells_normal=request.POST.get('Pus Cells: normal')
                    Pus_Cell_Clumps_present=request.POST.get('Pus Cell Clumps: present')
                    Bacteria_present=request.POST.get('Bacteria: present')
                    Hypertension_yes=request.POST.get('Hypertension: yes')
                    Diabetes_Mellitus_yes=request.POST.get('Diabetes Mellitus: yes')
                    Coronary_Artery_Disease_yes=request.POST.get('Coronary Artery Disease: yes')
                    Appetite_poor=request.POST.get('Appetite: poor')
                    Pedal_Edema_yes=request.POST.get('Pedal Edema: yes')
                    Anemia_yes=request.POST.get('Anemia: yes')
                    y_pred=kidn.predict([[age,bloodpressure,specificgravity,Albumin,Sugar,BloodGlucoseRandom,Blood_Urea,Serum_Creatinine,Sodium,Potassium,Hemoglobin,Packed_Cell_Volume,White_Blood_Cells,Red_Blood_Cells,Red_Blood_Cells_normal,Pus_Cells_normal,Pus_Cell_Clumps_present,Bacteria_present,Hypertension_yes,Diabetes_Mellitus_yes,Coronary_Artery_Disease_yes,Appetite_poor,Pedal_Edema_yes,Anemia_yes]])
                    print(y_pred[0])
                    if y_pred[0]==0:
                              y_pred="non disease"
                    else:
                              y_pred="disease"
                    email=request.session['email']
                    # here i set the email concept
                    subject = "Health Report"
                    message = "Hi Welcome to the Health prediction web application , First of all we thank you for using our services. your medical check up result for Kidney disease is"+ y_pred+"Thank you
 Have a nice day ahead."
                    from_email = settings.EMAIL_HOST_USER
                    to_email = [email]
                    send_mail(subject, message, from_email,to_email, fail_silently=False)

          return render(request,"kidney.html",{"y":y_pred})
@login_required(login_url="logins")
def parkinson(request):
          # now i write the backend for the parkinsons disease
          y_pred=''
          if request.method=='POST':

                    n1=request.POST.get('MDVP:Fo(Hz)')
                    n2=request.POST.get('MDVP:Fhi(Hz)')
                    n3=request.POST.get('MDVP:Flo(Hz)')
                    n4=request.POST.get('MDVP:Jitter(%)')
                    n5=request.POST.get('MDVP:Jitter(Abs)')
                    n6=request.POST.get('MDVP:RAP')
                    n7=request.POST.get('MDVP:PPQ')
                    n8=request.POST.get('Jitter:DDP')
                    n9=request.POST.get('MDVP:Shimmer')
                    n10=request.POST.get('MDVP:Shimmer(dB)')
                    n11=request.POST.get('Shimmer:APQ3')
                    n12=request.POST.get('Shimmer:APQ5')
                    n13=request.POST.get('MDVP:APQ')
                    n14=request.POST.get('Shimmer:DDA')
                    n15=request.POST.get('NHR')
                    n16=request.POST.get('HNR')
                    n17=request.POST.get('RPDE')
                    n18=request.POST.get('DFA')
                    n19=request.POST.get('spread1')
                    n20=request.POST.get('spread2')
                    n21=request.POST.get('D2')
                    n22=request.POST.get('PPE')
                    y_pred=park.predict([[n1,n2,n3,n4,n5,n6,n7,n8,n9,n10,n11,n12,n13,n14,n15,n16,n17,n18,n19,n20,n21,n22]])
                    print(y_pred[0])
                    if y_pred[0]==0:
                              y_pred="nondisease"
                    else:
                              y_pred="disease"
                    email=request.session['email']
                    # here i set the email concept
                    subject = "Health Report"
                    message = "Hi Welcome to the Health prediction web application , First of all we thank you for using our services. your medical check up result for parkinsons disease is"+ y_pred+"Thank you
 Have a nice day ahead."
                    from_email = settings.EMAIL_HOST_USER
                    to_email = [email]
                    send_mail(subject, message, from_email,to_email, fail_silently=False)

          return render(request,"parkinson.html",{"y":y_pred})

def aboutus(request):
          return render(request,"aboutus.html")

@login_required(login_url="logins")
def enquire(request):
          if request.method=='POST':
                    n1=request.POST.get('subject')
                    n2=request.POST.get('enquire')
                    email=request.session['email']
                    # here i set the email concept
                    subject = n1
                    message = n2
                    from_email = email
                    to_email = [settings.EMAIL_HOST_USER]
                    send_mail(subject, message, from_email,to_email, fail_silently=False)
          return render(request,"enquire.html")


import dash
import dash_html_components as html
import dash_bootstrap_components as dbc
import dash_core_components as dcc
from dash.dependencies import Input, Output, State
import pandas as pd
from sqlalchemy import create_engine
import pymysql

postgres_url = 'mysql+pymysql://root:123456@39.105.11.250/analysis'
engine = create_engine(postgres_url)

app = dash.Dash(__name__)

app.layout = html.Div(
    dbc.Container(
        [
            dbc.Row(
                [
                    dbc.Col(dbc.Button('更新数据库信息', id='refresh-db', style={'width': '100%'}), width=2),
                    dbc.Col(dcc.Dropdown(id='db-table-names', placeholder='选择库中数据表', style={'width': '100%'}),
                            width=4),
                    dbc.Col(dbc.Button('查询', id='query', style={'width': '100%'}), width=1)
                ]
            ),
            html.Hr(),
            dbc.Row(
                [
                    dbc.Col(
                        id='query-result'  # 查询结果
                    )
                ]
            )
        ],
        style={
            'margin-top': '50px'  # 设置顶部留白区域高度
        }
    )
)

@app.callback(
    Output('db-table-names', 'options'),  # 输出表格名字
    Input('refresh-db', 'n_clicks'),  # 点击刷新按钮
    prevent_initial_call=True
)
def refresh_table_names(n_clicks):  # 函数是查询
    table_names = pd.read_sql_query("select table_name from information_schema.tables where table_schema='analysis'",
                                    con=engine)
    return [{'label': name, 'value': name} for name in table_names['TABLE_NAME']]

@app.callback(
    Output('query-result', 'children'),  # 输出查询结果
    Input('query', 'n_clicks'),  # 输出点击查询按钮
    State('db-table-names', 'value'),  # 下拉框的显示
    prevent_initial_call=True  # 初始不要默认查询
)
def query_data_records(n_clicks, value):
    if value:
        # 提取目标表格并查询其最多前500行记录
        query_result = pd.read_sql_query(f'select * from {value} limit 500', con=engine)

        return html.Div(dbc.Table.from_dataframe(query_result, striped=True),
                        style={'height': '600px', 'overflow': 'auto'})
    else:
        return dash.no_update

if __name__ == '__main__':
    app.run_server(debug=True)

静态部件篇（中）/app4.py


import requests

class MockTest:
    def __init__(self, url, request_params1):
        self.url = url
        self.request_params1 = request_params1
        self.request_headers = {}

    def MockPort1(self):
        result = requests.get(url=self.url, params=self.request_params1, headers=self.request_headers, verify=False).text

        return result

if __name__ == '__main__':
    # obj = MockTest("https://mockapi.eolink.com/PE7cd7e93684a74e31d09980ae8c1b1fe10512d7b9ed829/api/personal/dashboard/sales/ranking?orderBy=qty&date=2023-03",'')
    obj = MockTest("http://192.168.110.13:8088/shopping_cart", '')
    print(obj.MockPort1())


import cv2
import time
import numpy as np

fourcc=cv2.VideoWriter_fourcc(*'XVID')
output_file = cv2.VideoWriter('output.avi', fourcc, 20.0,(640,480))

cap=cv2.VideoCapture(0)
time.sleep(2)
bg=0

for i in range(60):
    frame,bg = cap.read()
bg = np.flip(bg, axis=1)

while(cap.isOpened()):
    frame , img = cap.read()
    if not frame:
        break
    img = np.flip(img, axis=1)
    hsv=cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

    l_black = np.array([104,153,70])
    u_black = np.array([30,30,0])
    mask = cv2.inRange(hsv,l_black,u_black)
    res = cv2.bitwise_and(frame , frame, mask=mask)

    f = frame - res
    f = np.where(f==0, image, f)

    final_output=cv2.addWeighted(res, 1, 0)
    output_file.write(final_output)

    cv2.imshow("magic",final_output)
    cv2.waitKey(1)

cap.release()
out.release()
cv2.destroyAllWindows()


from __future__ import print_function

# coding= utf-8

import mechanize
import requests
import csv
from bs4 import BeautifulSoup

br = mechanize.Browser()
br.set_handle_robots(False)

def finding_web_links(list_url):
    res = br.open(list_url)  # request url
    html = res.read()  # html
    soup = BeautifulSoup(html, 'html.parser')  # soup
    a_tags = soup.find_all('a')  # finding a tags
    links = []
    for a_tag in a_tags:
        url = a_tag.attrs.get('href')
        if not url:  # url 이 비어있는 주소라면
            continue
        if not 'http://www.polygon.com/' in url:
            # url 에 http://www.polygon.com/ 가 포함되어 있으면
            continue
        length = len('http://www.polygon.com/')
        year = url[length:length + 4]
        if not year.isdigit():
            continue
        if 'comments' in url:
            continue
        if url in links:
            continue
        links.append(url)
    return links

def finding_youtube_id(link_url):
    ####################################################
    # Generate url address for crawling
    res = br.open(link_url)
    html = res.read()
    soup = BeautifulSoup(html, 'html.parser')
    ####################################################
    # Finding youtube video ids
    youtube_string = str()
    for iframe in soup.findAll('iframe'):
        # taking iframe
        src_string = iframe.get('src')
        if 'youtube' in src_string:
            youtube_string = src_string
    start = youtube_string.rfind('/') + 1
    end = youtube_string.rfind('?')
    youtube_id = str()
    if end is -1:
        youtube_id = youtube_string[start:]
    else:
        youtube_id = youtube_string[start:end]
    if youtube_id == '':
        return None
    if youtube_id == 'videoseries':
        return None
    return youtube_id

def youtube_statistics(youtube_id):
    api_key = ''
    url = 'https://www.googleapis.com/youtube/v3/videos?id=%s&key=%s&part=snippet,contentDetails,statistics,status' \
          % (youtube_id, api_key)

    response = requests.get(url)
    json = response.json()

    statistics = json['items'][0]['statistics']
    statistics['videoId'] = youtube_id
    # 이거 만으로는 어떤 비디오 데이터 인지 몰라 그래서 video id 추가해 준거 statistics['videoId'] = video_id
    return statistics

def save_statistics(statistics_list, filename):
    with open(filename, 'wb') as file:
        fieldnames = ['videoId', 'commentCount', 'viewCount', 'favoriteCount', 'dislikeCount', 'likeCount']
        writer = csv.DictWriter(file, fieldnames=fieldnames)
        writer.writeheader()
        for statistics in statistics_list:
            writer.writerow(statistics)

    print('Save file finished!')

##############

links_result = []
for page_number in range(1, 10):
    print('Start crawling %s page.' % page_number)
    url = 'http://www.polygon.com/videos/%s' % page_number
    links_result.extend(finding_web_links(url))

youtube_ids = []
for link in links_result:
    print('Request youtube api [%s]' % link)
    youtube_id = finding_youtube_id(link)
    if youtube_id:
        youtube_ids.append(youtube_id)

print('Count of youtube ids is %s' % len(youtube_ids))

statistics_result = []
for youtube_id in youtube_ids:
    statistics = youtube_statistics(youtube_id)
    statistics_result.append(statistics)

save_statistics(statistics_result, 'statistics_video.csv')


import os
import sys
import argparse

latent_dim= 2
total_blocks= 2
learning_rate= 1e-3
batch_size= 64
train_size= 20000
plot_case= 'extrapolate'
latent_case= 'balls_supp_l_shape'
seed_grid= range(10)
input_normalization_grid= ['none']

method_grid= ['ae_base', 'ae_additive']
eval_latent_grid= ['balls_supp_l_shape', 'balls_supp_extrapolate']

#Disconnected support experiments
# latent_case= 'balls_supp_disconnected'
#method_grid= ['ae_base']

# Input Parsing
parser = argparse.ArgumentParser()
parser.add_argument('--case', type=str, default='train',
                   help= '')
parser.add_argument('--curr_method', type=str, default='none',
                   help= '')
parser.add_argument('--curr_seed', type=int, default=-1,
                    help='')

args = parser.parse_args()
case= args.case
curr_method= args.curr_method
curr_seed= args.curr_seed

if case == 'train':
    for method in method_grid:
        if method != curr_method and curr_method!='none':
            continue

        for seed in seed_grid:
            if seed != curr_seed and curr_seed!=-1:
                continue

            for input_normalization in input_normalization_grid:
                script= 'sbatch scripts/slurm_launcher.sh' \
                        + ' ' + str(latent_case) \
                        + ' ' + method \
                        + ' ' + str(latent_dim) \
                        + ' ' + str(total_blocks) \
                        + ' ' + str(train_size) \
                        + ' ' + str(seed) \
                        + ' ' + str(input_normalization) \
                        + ' ' + str(batch_size) \
                        + ' ' + str(learning_rate)
                os.system(script)

elif case == 'eval':
    for method in method_grid:
        if method != curr_method and curr_method!='none':
            continue

        for eval_latent in eval_latent_grid:

            for input_normalization in input_normalization_grid:

                script= 'sbatch scripts/slurm_launcher_eval.sh' \
                        + ' ' + str(latent_case) \
                        + ' ' + method \
                        + ' ' + str(latent_dim) \
                        + ' ' + str(total_blocks) \
                        + ' ' + str(train_size) \
                        + ' ' + str(input_normalization) \
                        + ' ' + str(batch_size) \
                        + ' ' + str(learning_rate) \
                        + ' ' + eval_latent \
                        + ' ' + plot_case
                os.system(script)


import pygame
import pygame.gfxdraw

pygame.init()

white = (255, 255, 255)
black = (0, 0, 0)

x1, y1 = map(int,input("first point: ").split())
x2, y2 = map(int,input("second point: ").split())

width = 400
height = 400
screen = pygame.display.set_mode([width, height], pygame.RESIZABLE)
pygame.display.set_caption("BLA")

done = False
points = []
count = 1

def BLA(x1, y1, x2, y2):
    dx = abs(x2 - x1)
    dy = abs(y2 - y1)
    x, y = x1, y1

    if dx == 0:
        m = 0
    else:
        m = dy / dx

    if m < 1:
        pk = 2 * dy - dx
        pygame.gfxdraw.pixel(screen, x1, y1, black)
        points.append((pk, x, y))

        for i in range(dx):
            if pk < 0:
                x += 1 if x2 > x1 else -1
                y = y
                pk = pk + 2 * dy
            else:
                x += 1 if x2 > x1 else -1
                y += 1 if y2 > y1 else -1
                pk = pk + 2 * (dy - dx)

            pygame.gfxdraw.pixel(screen, x, y, black)
            points.append((pk, x, y))
    elif m >= 1:
        pk = 2 * dx - dy
        pygame.gfxdraw.pixel(screen, x1, y1, black)
        points.append((pk, x, y))

        for i in range(dy):
            if pk < 0:
                x = x
                y += 1 if y2 > y1 else -1
                pk = pk + 2 * dx
            else:
                x += 1 if x2 > x1 else -1
                y += 1 if y2 > y1 else -1
                pk = pk + 2 * (dx - dy)

            pygame.gfxdraw.pixel(screen, x, y, black)
            points.append((pk, x, y))

while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    screen.fill(white)

    BLA(x1, y1, x2, y2)

    while count > 0:
        for point in points:
            print(point)
        count -= 1

    pygame.display.flip()

pygame.quit()


__author__ = 'brayden'

from tornado.web import asynchronous
from tornado.web import authenticated
from tornado.ioloop import IOLoop
from . import BaseServerAjaxHandler
from Minecraft.backup import Local
from functools import partial
from datetime import timedelta

class BackupServerHandler(BaseServerAjaxHandler):
    @asynchronous
    @authenticated
    def post(self, server_id):
        destination = self.application.db.get_backup_destination_by_id(self.get_argument('destination_id'))
        if not destination.Remote:
            backup = Local(destination.ID, destination.Folder)
            server_name = self.application.process_prefix + server_id
            server_folder = self.application.config.get('minecraft', 'home') + '/%s%s/' % (self.application.process_prefix, server_id)
            if destination.Backup_Limit == 0 or self.application.usernames[self.current_user]['Is_Admin'] or not len(backup.get_backups_by_server(server_name)) >= destination.Backup_Limit:
                self.application.supervisor.run_command(server_name, 'save-off')
                IOLoop.instance().add_timeout(timedelta(seconds=2), partial(self.callback, backup, server_name, server_folder))
            else:
                self.finish({"result": {"message": "Reached backup limit.", "success": False}})
        else:
            self.finish({"result": {"message": "Invalid destination argument given.", "success": False}})

    def callback(self, backup, server_name, server_folder):
        if self.get_argument('backup_type') == 'tar':
            backup.backup_server_tar(server_name, server_folder)
            self.application.supervisor.run_command(server_name, 'save-on')
            self.finish({"result": {"message": None, "success": True}})
        elif self.get_argument('backup_type') == 'zip':
            backup.backup_server_zip(server_name, server_folder)
            self.application.supervisor.run_command(server_name, 'save-on')
            self.finish({"result": {"message": None, "success": True}})
        else:
            self.finish({"result": {"message": "Invalid backup_type given.", "success": False}})


import cv2
file_path = "./img/Ch02_img/girl.jpg"
img = cv2.imread(file_path)                                 # 이미지를 기본 값으로 받기
img_gray = cv2.imread(file_path, cv2.IMREAD_GRAYSCALE)      # 이미지를 흑백으로 받기

cv2.namedWindow("origin", cv2.WINDOW_AUTOSIZE)              # 원래 크기로 이미지 창 생성
cv2.namedWindow("gray", cv2.WINDOW_NORMAL)                  # 임의의 크기로 이미지 창 생성

cv2.imshow("origin", img)           # "origin" 창에 (기본값으로 받았던)이미지 표시
cv2.imshow("gray", img_gray)        # "gray" 창에 (흑백으로 받았던)이미지 표시

cv2.moveWindow("origin", 0,0)       # 창 위치 변경
cv2.moveWindow("gray", 100,100)     # 창 위치 변경

cv2.waitKey(0)                          # 아무키나 누르면
cv2.resizeWindow("origin", 200, 200)    # 창 크기 변경 (이미지 변경 불가, 그림이 짤려서 나옴)
cv2.resizeWindow("gray", 100, 100)      # 창 크기 변경 (변경 가능)

cv2.waitKey(0)
cv2.destroyWindow("gray")           # "gray" 창 닫기

cv2.waitKey(0)
cv2.destroyAllWindows()                 # 모든 창 닫기


"""Helper functions for the pluginmanager plugin"""

import os
import shutil
import urllib.request
import json
import zipfile
import pkgutil
import importlib
import logging

from . import constants
from enki.core.core import core
from .constants import PLUGIN_DIR_PATH, REPO

# Data Definitions
# ==================
# userpluginEntry consist of
# {'module': Module
#  'plugin': Plugin or None,
#  'isloaded': Bool,
#  'name': String,
#  'author': String,
#  'version': String,
#  'doc': String}
#
# ListOfUserpluginEntry is one of:
# - []
# - lou.append(userpluginEntry)

def create_UE(module, isLoaded, modulename, pluginname,
              author, version, doc, plugin=None):
    """Create a new userpluginEntry"""
    return {'module': module,
            'plugin': plugin,
            'isLoaded': isLoaded,
            'modulename': modulename,
            'pluginname': pluginname,
            'author': author,
            'version': version,
            'doc': doc}

def loadPlugin(pluginEntry):
    """Consume a pluginEntry and load the plugin into core._loadedPlugins,
    based on it's 'isLoaded' property
    return pluginEntry
    """
    if pluginEntry['isLoaded'] is True:
        pluginEntry['plugin'] = pluginEntry['module'].Plugin()
        core.loadedPlugins().append(pluginEntry['plugin'])
    return pluginEntry

def unloadPlugin(pluginEntry):
    """Consume a pluginEntry and unload the plugin from core._loadedPlugins,
    based on it's 'isLoaded' property
    return pluginEntry
    """
    if pluginEntry['isLoaded'] is False and \
       pluginEntry['plugin'] is not None:
        idx = core.loadedPlugins().index(pluginEntry['plugin'])
        plugin = core.loadedPlugins().pop(idx)
        plugin.terminate()
        pluginEntry['plugin'] = None
    return pluginEntry

def deletePlugin(pluginEntry, pluginDirPath=PLUGIN_DIR_PATH):
    """Consume a pluginEntry and delete the plugin directory or file"""
    unloadPlugin(pluginEntry)
    dirpath = os.path.join(pluginDirPath, pluginEntry["modulename"])
    filepath = os.path.join(pluginDirPath, pluginEntry["modulename"] + '.py')
    if os.path.isdir(dirpath):
        shutil.rmtree(dirpath)
    elif os.path.exists(filepath):
        os.remove(filepath)
    else:
        logging.warning("Could not find module %s. Did not delete anything." %
              pluginEntry["modulename"])

def inUserPlugins(modulename, lope):
    """String ListOfUserpluginEntry -> Bool
    Consumes a modulename and a list of pluginEntrys
    return True if ListOfUserpluginEntry has PluginEntry with modulename
    """
    for pluginEntry in lope:
        if pluginEntry["modulename"] == modulename:
            return True
    return False

def isPluginInstalled(name, lope):
    """String ListOfUserpluginEntry -> Bool
    Consumes a plugin name and a list of pluginEntrys
    returns the plugin if ListOfUserpluginEntry has PluginEntry
            with pluginname name
    """
    for pluginEntry in lope:
        if pluginEntry["pluginname"] == name:
            return pluginEntry
    return False

def getRepo(repo=REPO):
    """Void -> Dictionary
    Loads the repo and returns it as a dictionary"""
    try:
        url = urllib.request.urlopen(repo)
    except Exception as e:
        repo = constants.EMPTY_REPO
    else:
        rawData = url.read().decode()
        repo = json.loads(rawData)
        logging.debug(repo)
    return repo

def downloadPlugin(url, tmpPath):
    """String String -> bool
    Consumes the url of a plugin archive and the path to save it
    return True if download succeeded, else False"""
    try:
        request = urllib.request.urlretrieve(url, tmpPath)
        logging.debug(request)
    except Exception as e:
        logging.exception("The download could not finish.")
        return False
    else:
        logging.info("The plugin has been downloaded")
        return tmpPath

def extractPlugin(filePath, pluginDirPath=PLUGIN_DIR_PATH):
    """String -> Bool
    Consumes the filePath of a plugin archive and extracts it to the
    userplugins-directory, return True if extract succeeded, els False"""
    try:
        zipref = zipfile.ZipFile(filePath, 'r')
        zipref.extractall(pluginDirPath)
        zipref.close()
    except Exception as e:
        logging.exception("Could not extract plugin")
        os.remove(filePath)
        return False
    else:
        logging.info("Plugin extracted")
        os.remove(filePath)
        logging.info("Plugin deleted")
        return True

def renamePluginFolder(oldName, newName, pluginDirPath=PLUGIN_DIR_PATH):
    """String String -> Bool
    Consumes the oldName and NewName of a plugin folder and renames it in the
    userplugins-directory, return True if rename succeeded, else False"""
    try:
        os.rename(os.path.join(pluginDirPath, oldName),
                  os.path.join(pluginDirPath, newName))
    except Exception as e:
        logging.exception("Could not rename plugin directory")
        return False
    else:
        logging.info("Plugin directory renamed to %s." % newName)
        return True

def getPlugins(pluginDirPath=PLUGIN_DIR_PATH):
    """String -> ListOfUserpluginEntry
    Loads all userplugins and returns them as a ListOfUserpluginEntry"""
    userPlugins = []
    for loader, name, isPackage in pkgutil.iter_modules([pluginDirPath]):
        if not inUserPlugins(name, userPlugins):
            userPlugin = getPlugin(name, pluginDirPath)
            if userPlugin:
                userPlugins.append(userPlugin)
    return userPlugins

def getPlugin(name, pluginDirPath=PLUGIN_DIR_PATH):
    """String (String) -> PluginEntry
    get plugin by it's module name and returns userpluginEntry
    """
    module = importlib.import_module('userplugins.%s' % name)
    try:
        pluginEntry = create_UE(
            module,
            shouldPluginLoad(name),
            name,
            module.__pluginname__,
            module.__author__,
            module.__version__,
            module.__doc__)
        return pluginEntry
    except AttributeError:
        logging.exception("Plugin %s misses required attributes." % name)
        return False

def initPlugins():
    """Loads all userplugins and returns them as a ListOfUserpluginEntry"""
    userPlugins = getPlugins()
    for up in userPlugins:
        loadPlugin(up)
    return userPlugins

def initPlugin(name):
    """Load plugin by it's module name
    returns userpluginEntry
    """
    return loadPlugin(getPlugin(name))

def shouldPluginLoad(name):
    """String Dict -> Bool
    Consumes a name of a plugin and the enki configuration and
    checks in the settings if it should be loaded.
    If no setting is available for the plugin, it gets created.
    Returns the setting (Bool)
    """
    if name not in core.config()["PluginManager"]["Plugins"]:
        core.config()["PluginManager"]["Plugins"][name] = {}
    if "Enabled" not in core.config()["PluginManager"]["Plugins"][name]:
        core.config()["PluginManager"]["Plugins"][name]["Enabled"] = False
    return core.config()["PluginManager"]["Plugins"][name]["Enabled"]


import enum
from gettext import find
from aiogram import types, Dispatcher
from aiogram.dispatcher.filters import Text
from aiogram.types.message import ParseMode
from aiogram.dispatcher import FSMContext
from sqlalchemy import select
import re
from states.user import AdminUpdateUser
from api_client.api_requests import api_requests
from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton
from keyboards.markup import create_markup, admin_main_menu, manage_admins_menu

async def manage_admins(message: types.Message):

    response = api_requests.get(f"/admins/")
    admins = response.json()

    for admin in admins:
        await message.answer(
            text=f"id: {admin['user']['id']}\nname: {admin['user']['name']}\nphone: {admin['user']['phone']}\ngender:{admin['user']['gender']}",
            reply_markup=InlineKeyboardMarkup().add(
                InlineKeyboardButton(
                    "delete", callback_data=f"del admin {admin['user']['id']}"
                )
            ),
        )
    await message.answer(
        "You can delete or add admin", reply_markup=create_markup(manage_admins_menu)
    )

async def delete_admin(callback_query: types.CallbackQuery):
    admin_id = callback_query.data.replace("del admin ", "")
    api_requests.delete(f"/admins/{admin_id}")
    await callback_query.answer(
        text=f"admin with id: {admin_id} deleted", show_alert=True
    )

async def add_admin_button(message: types.Message):
    await message.answer("Give users id", reply_markup=types.ReplyKeyboardRemove())

async def add_admin(message: types.Message):

    user_id = message.text.strip()
    try:
        user_id = int(user_id)
    except:
        await message.answer(
            text="Invalid user id, try again", reply_markup=types.ReplyKeyboardRemove()
        )
        return

    response = api_requests.post("/admins/", json={"user_id": user_id})

    if response.status_code == 409:
        await message.answer(
            text=response.json()["detail"],
            reply_markup=create_markup(manage_admins_menu),
        )
        return

    if response.status_code == 201:
        await message.answer(
            text="successfully added", reply_markup=create_markup(manage_admins_menu)
        )

async def manage_users(message: types.Message):

    response = api_requests.get(f"/users/")
    users = response.json()

    for user in users:
        await message.answer(
            text=f"id: {user['id']}\nname: {user['name']}\nphone: {user['phone']}\ngender:{user['gender']}",
            reply_markup=InlineKeyboardMarkup().add(
                InlineKeyboardButton("name", callback_data=f"name user {user['id']}"),
                InlineKeyboardButton("phone", callback_data=f"phone user {user['id']}"),
                InlineKeyboardButton(
                    "gender", callback_data=f"gender user {user['id']}"
                ),
                InlineKeyboardButton("delete", callback_data=f"del user {user['id']}"),
            ),
        )

async def delete_user(callback_query: types.CallbackQuery):

    id = callback_query.data.replace("del user ", "")
    api_requests.delete(f"/users/{id}")
    await callback_query.answer(text=f"user with id: {id} deleted", show_alert=True)

async def name_user(callback_query: types.CallbackQuery, state: FSMContext):

    async with state.proxy() as data:
        data["id"] = callback_query.data.replace("name user ", "")

    await AdminUpdateUser.name.set()

    await callback_query.answer(text=f"Print Name:", show_alert=True)

async def phone_user(callback_query: types.CallbackQuery, state: FSMContext):

    async with state.proxy() as data:
        data["id"] = callback_query.data.replace("phone user ", "")

    await AdminUpdateUser.phone.set()

    await callback_query.answer(text=f"Print phone:", show_alert=True)

async def gender_user(callback_query: types.CallbackQuery, state: FSMContext):

    async with state.proxy() as data:
        data["id"] = callback_query.data.replace("gender user ", "")

    await AdminUpdateUser.gender.set()
    await callback_query.answer(text=f"Print gender:", show_alert=True)

async def add_name(message: types.Message, state: FSMContext):

    async with state.proxy() as data:
        data["name"] = message.text
        dict_data = data.as_dict()

    user = api_requests.get(f"/users/{dict_data['id']}").json()
    user["name"] = dict_data["name"]

    response = api_requests.put(f"/users/{dict_data['id']}", json=user)
    await state.finish()
    await message.answer("Updated!")
    await message.answer("main menu")

async def add_phone(message: types.Message, state: FSMContext):

    phone = message.text.replace(" ", "")
    if not re.match(r"^(\+375|80)(29|25|44|33)(\d{7})$", phone):
        await message.answer("Invalid phone number, try again")
        return

    async with state.proxy() as data:
        data["phone"] = phone
        dict_data = data.as_dict()

    user = api_requests.get(f"/users/{dict_data['id']}").json()
    user["phone"] = dict_data["phone"]

    response = api_requests.put(f"/users/{dict_data['id']}", json=user)
    await state.finish()
    await message.answer("Updated!")
    await message.answer("main menu")

async def add_gender(message: types.Message, state: FSMContext):

    gender = message.text.strip()

    if gender not in ["male", "female", "other"]:
        await message.answer("Options: male, female, other")
        return

    async with state.proxy() as data:
        data["gender"] = gender
        dict_data = data.as_dict()

    user = api_requests.get(f"/users/{dict_data['id']}").json()
    user["gender"] = dict_data["gender"]

    response = api_requests.put(f"/users/{dict_data['id']}", json=user)
    await state.finish()
    await message.answer("Updated!")
    await message.answer("main menu")

def register_admin_handlers(dp: Dispatcher):
    dp.register_callback_query_handler(
        delete_admin, lambda x: x.data and x.data.startswith("del admin")
    )
    dp.register_message_handler(manage_admins, Text(equals="manage admins"))

    dp.register_callback_query_handler(
        delete_user, lambda x: x.data and x.data.startswith("del user")
    )
    dp.register_callback_query_handler(
        name_user, lambda x: x.data and x.data.startswith("name user"), state="*"
    )
    dp.register_callback_query_handler(
        phone_user, lambda x: x.data and x.data.startswith("phone user"), state="*"
    )
    dp.register_callback_query_handler(
        gender_user, lambda x: x.data and x.data.startswith("gender user"), state="*"
    )
    dp.register_message_handler(manage_users, Text(equals="manage users"))
    dp.register_message_handler(add_admin_button, Text(equals="add"))
    dp.register_message_handler(add_admin)
    dp.register_message_handler(add_name, state=AdminUpdateUser.name)
    dp.register_message_handler(add_phone, state=AdminUpdateUser.phone)
    dp.register_message_handler(add_gender, state=AdminUpdateUser.gender)


# importing all the required modules
import PyPDF2

# creating an object
file = open('testing-pypdf/pdf.pdf', 'rb')

# creating a pdf reader object
pdf = PyPDF2.PdfFileReader(file)

# print the number of pages in pdf file
print(pdf.numPages)


from __future__ import absolute_import, print_function

from bottle import default_app, install, route, request, redirect, run, template

# Import eStore model http://editor.ponyorm.com/user/pony/eStore
from pony.orm.examples.estore import *
from pony.orm.integration.bottle_plugin import PonyPlugin

# After the plugin is installed each request will be processed
# in a separate database session. Once the HTTP request processing
# is finished the plugin does the following:
#  * commit the changes to the database (or rollback if an exception happened)
#  * clear the transaction cache
#  * return the database connection to the connection pool
install(PonyPlugin())

@route('/')
@route('/products/')
def all_products():
    # Get the list of all products from the database
    products = select(p for p in Product)
    return template('''

List of products

    %for p in products:

{{ p.name }}
    %end

    ''', products=products)

@route('/products/:id/')
def show_product(id):
    # Get the instance of the Product entity by the primary key
    p = Product[id]
    # You can traverse entity relationship attributes inside the template
    # In this examples it is many-to-many relationship p.categories
    # Since the data were not loaded into the cache yet,
    # it will result in a separate SQL query.
    return template('''

{{ p.name }}

Price: {{ p.price }}

Product categories:

    %for c in p.categories:

{{ c.name }}
    %end

    Edit product info
    Return to all products
    ''', p=p)

@route('/products/:id/edit/')
def edit_product(id):
    # Get the instance of the Product entity and display its attributes
    p = Product[id]
    return template('''
    <form action='/products/{{ p.id }}/edit/' method='post'>

          Product name:
          <input type="text" name="name" value="{{ p.name }}">

          Product price:
          <input type="text" name="price" value="{{ p.price }}">

      <input type="submit" value="Save!">
    </form>

Discard changes

Return to all products
    ''', p=p)

@route('/products/:id/edit/', method='POST')
def save_product(id):
    # Get the instance of the Product entity
    p = Product[id]
    # Update the attributes with the new values
    p.name = request.forms.get('name')
    p.price = request.forms.get('price')
    # We might put the commit() command here, but it is not necessary
    # because PonyPlugin will take care of this.
    redirect("/products/%d/" % p.id)
    # The Bottle's redirect function raises the HTTPResponse exception.
    # Normally PonyPlugin closes the session with rollback
    # if a callback function raises an exception. But in this case
    # PonyPlugin understands that this exception is not the error
    # and closes the session with commit.

run(debug=True, host='localhost', port=8080, reloader=True)


from cmath import nan
import logging
import threading
import time
from matplotlib.colors import LinearSegmentedColormap
from flask import Flask, request
from flask import jsonify
import json
from node import NodeWorker
import requests
import os
import methods as mtd
# from concurrent.futures import ThreadPoolExecutor
import geopandas as gpd

app = Flask(__name__)
app.debug = True
app.config['PROPAGATE_EXCEPTIONS'] = True

# rutas de acceso
logPath = os.environ.get("LOGS_PATH",'/logs')
sourcePath = os.environ.get("SOURCE_PATH","")
nodeId = os.environ.get("NODE_ID",'prueba')
send = mtd.trueOrFalse(val=os.environ.get("SEND",1)) # si en envia
presentationValue = mtd.trueOrFalse(os.environ.get('PRESENTATION',"1"))

# CONFIG LOGS ERROR INFO
# Format to logs
FORMAT = '%(created).0f %(levelname)s {} %(message)s'.format(nodeId)
# object formatter
formatter = logging.Formatter(FORMAT)
console = logging.StreamHandler()
console.setLevel(logging.INFO)
# config format
console.setFormatter(fmt=formatter)
# config del logging
logs_info_file = './data{}/{}_info.log'.format(logPath,nodeId)
logs_error_file = './data{}/{}_error.log'.format(logPath,nodeId)
# ------- Logger Info
loggerInfo = logging.getLogger('LOGS_INFO')
hdlr_1 = logging.FileHandler(logs_info_file)
hdlr_1.setFormatter(formatter)
loggerInfo.setLevel(logging.INFO)
loggerInfo.addHandler(hdlr_1)
loggerInfo.addHandler(console)
# ------- Logger Error
loggerError = logging.getLogger("LOGS_ERROR")
hdlr_2 = logging.FileHandler(logs_error_file)
hdlr_2.setFormatter(formatter)
loggerError.setLevel(logging.ERROR)
loggerError.addHandler(hdlr_2)
loggerError.addHandler(console)

# creamos la carpeta
if (not os.path.exists(".{}/{}".format(sourcePath,nodeId))):
    os.mkdir(".{}/{}".format(sourcePath,nodeId))

state = { 'nodeId': nodeId,
            "nodes": [],
            'ip': os.environ.get('IP','127.0.0.1'),
            'publicPort': os.environ.get('PUBLIC_PORT',5000),
            'dockerPort': os.environ.get('DOCKER_PORT',5000),
            'mode': os.environ.get('MODE','DOCKER'),
            "events":0}

# Save manager node info
nodeInfoManager = {"nodeId": os.environ.get('NODE_ID_MANAGER','-'),
            "ip": os.environ.get('IP_MANAGER','127.0.0.1'),
            "publicPort": os.environ.get('PUBLIC_PORT_MANAGER',5000),
            "dockerPort": os.environ.get('DOCKER_PORT_MANAGER',5000)}
nodeManager = NodeWorker(**nodeInfoManager)
# Save sink node info
nodeInfoSink = {'nodeId': os.environ.get('NODE_ID_SINK',''),
            'ip': os.environ.get('IP_SINK','127.0.0.1'),
            'publicPort': os.environ.get('PUBLIC_PORT_SINK',5000),
            'dockerPort': os.environ.get('DOCKER_PORT_SINK',5000)}
nodeSink = NodeWorker(**nodeInfoSink)

tableState = {"numEvents":0,
            "nodeID":nodeId,
            "events":[]}

@app.route('/status/node', methods = ['GET'])
def stateN():
    global state
    return jsonify(state)

@app.route('/status/manager', methods = ['GET'])
def stateM():
    global nodeManager
    return jsonify(nodeManager.toJSON())

# ADD NODE WORKER
@app.route('/workers', methods = ['POST'])
def add_worker():
    global state
    global send
    global nodeManager
    # Recibe 5 parametros
    # {idNode: str(), ip: str(), publicPort: int, dockerPort: int}
    # get info new node
    startTime = time.time()
    nodeNewInfo = request.get_json()
    # create new node
    nodeNew = NodeWorker(**nodeNewInfo)
    # add new node
    state['nodes'].append(nodeNew)
    endTime = time.time()
    loggerInfo.info('CREATED_NODE {} {} 0 0 0 0'.format(nodeNew.nodeId,(endTime-startTime)))
    send = True
    return jsonify({'response':"OK"})

# GET ALL NODES WORKERS
@app.route('/workers', methods = ['GET'])
def show_worker():
    global state
    # show all nodes
    nodes = state['nodes']
    nodesReturn = []
    for node in nodes:
        nodesReturn.append(node.toJSON())
    return jsonify(nodesReturn)

@app.before_first_request
def presentation():
    global state
    global nodeManager
    global presentationValue
    # send info to manager node
    infoSend = {'nodeId': state['nodeId'],
                'ip': state['ip'],
                'publicPort': state['publicPort'],
                'dockerPort': state['dockerPort']}

    # send info to manager
    headers = {'PRIVATE-TOKEN': '<your_access_token>', 'Content-Type':'application/json'}
    # Node Manager
    time.sleep(5)
    startTime = time.time()
    contPresentation = 1
    while True:
        try:
            # app.logger.info(nodeManager.getURL(mode=state['mode']))
            # Node Manager
            # En dado caso que ya se haya presentado no lo vuelve hacer
            if (presentationValue == False):
                break
            # url destino del manager
            url = nodeManager.getURL(mode=state['mode'])
            response =requests.post(url, data=json.dumps(infoSend), headers=headers)
            responseJson = response.json()
            endTime = time.time()
            # OPERATION    TYPE_BLANCER    SERVICE_TIME    ARRIVAL_TIME    EXIT_TIME    LATENCIE_TIME
            serviceTime = endTime-startTime
            loggerInfo.info('CONNECTION_SUCCESSFULLY PRESENTATION_SEND {} {} {} {} 0'.format(serviceTime, startTime, endTime, 0))
            presentationValue = False
            # read json states
            break
        except requests.ConnectionError:
            loggerError.error('CONNECTION_REFUSED PRESENTATION_SEND {} {}'.format(nodeManager.nodeId, contPresentation))
            contPresentation = contPresentation + 1
            if (contPresentation == 10):
                return "CONNECTION_REFUSED"
            time.sleep(5)
    return "CONNECTION_SUCCESSFULLY"

def updateStateTable(jsonRespone,numberEvent,procesList,nodeId):
    global tableState
    eventName = "event_{}".format(numberEvent)
    loggerError.error("----------------------------------- RESPONSE {}".format(eventName))
    # saber si existe el evneto en la tabla de estado
    jsonState = {"NODE_ID":nodeId,
                "DATA_PROCESS": procesList,
                "INFO_RESPONSE":jsonRespone}
    if (eventName in tableState["events"]):
        tableState["events"][eventName] = list()
        tableState["events"][eventName].append(jsonState)
    else:
        tableState["events"][eventName].append(jsonState)

    return

def sendData(url,jsonSend,numberEvent,procesList,nodeId):
    try:
        headers = {'PRIVATE-TOKEN': '<your_access_token>', 'Content-Type':'application/json'}
        response = requests.post(url, data=json.dumps(jsonSend), headers=headers)
        jsonResponse = response.json()
        # updateStateTable(jsonRespone=jsonResponse,numberEvent=numberEvent, procesList=procesList, nodeId=nodeId)
        return "OK"
    except:
        loggerError.error('CLUSTERING_ERROR SEND_INFO {}'.format(nodeId))
        return "ERROR"

# Clustering process
@app.route('/analytics/map/mexico', methods = ['POST'])
def ploting():
    global state
    global nodeManager
    global send

    # recibimos los parametros
    message = request.get_json()
    try:
        numberEvent = state["events"] # Eventos ejecutados en el nodo
        numberEvent = numberEvent + 1 # Sumamos el evento que se ejecutara
        # ARRIVAL_TIME EXIT_TIME_MANAGER ---------------------
        arrivalTime = time.time()
        exitTimeManager = message['EXIT_TIME']
        # ----------------------------------------------------
        # fuentes
        sources = message["SOURCES"]
        sourcesNew = list()

        # geopandas de mexico "shapesfile"
        mexicoDF = gpd.read_file("./Map_Mexico/states/Mexico_Estados.shp")
        mexicoDF["ESTADO"]=mexicoDF["ESTADO"].replace("Distrito Federal","Ciudad de Mexico")

        paramsPlot = message["PARAMS"][0]
        del message["PARAMS"][0]

        cmap = LinearSegmentedColormap.from_list('mycmap', [(0,'white'),(1,'red')])
        loggerError.error("------------------------------------ Params")
        for src in range(len(sources)):
            # leemos el archivo a procesar
            # clusterData = mtd.read_CSV('.{}/{}'.format(sourcePath,sources[src]))
            nameSource = sources[src]
            if (nodeManager.getID()=="-"):
                mapData = mtd.read_CSV('.{}/{}'.format(sourcePath,nameSource))
            else:
                mapData = mtd.read_CSV('.{}/{}/{}'.format(sourcePath,nodeManager.getID(),nameSource))

            typePlot = paramsPlot["TYPE_PLOT"]
            # Aqui colocamos los mapas
            if (typePlot=="POLYGON"):
                # si es pintar polygonos
                typePolygon = paramsPlot["TYPE_POLYGON"]
                comlumnData = paramsPlot['COLUMN_DATA']
                columnPolygon = paramsPlot["COLUMN_POLYGON"]
                # MEDIANA de la columna que tiene la clase
                groupedMedian = mapData.groupby(columnPolygon).median()
                uniqueClases = mapData[columnPolygon].unique()
                if (typePolygon=="STATE"):
                    # pintara estados
                    # valores de los estados
                    valuesState = list()
                    for index, stateMx in mexicoDF.iterrows():
                        nameState = stateMx["ESTADO"]
                        # saber si tiene el estado dentro de la variable polygono
                        if (nameState in groupedMedian[columnPolygon]):
                            auxValue = groupedMedian[groupedMedian[columnPolygon]==nameState]
                            valuesState.append(auxValue.values)
                        else:
                            valuesState.append(nan)
                    # insertamos la columna con los valores
                    mexicoDF.insert(2,column="{}".format(columnPolygon))
                    loggerError.error(mexicoDF["{}".format(columnPolygon)])
                    ploting = mexicoDF.plot(figsize=(15, 9), column="{}".format(columnPolygon), legend=True,
                                            cmap=cmap, legend_kwds={"fontsize":16,"frameon":False})
                    # nombre del archivo
                    nameFile = ".{}/{}/{}.png".format(sourcePath, nodeId, nameSource.replace(".csv",""))
                    # colocamos el titulo del png
                    ploting.set_title('{}'.format(nameSource.replace(".csv","")))
                    ploting.figure.savefig(nameFile)

        exitTime = time.time()
        # si es verdadero enviamos
        if (send==True):
            workersCant = len(state["nodes"])
            jsonSend = message
            workersNodes = state["nodes"]
            modeToSend = state["mode"]
            endPoint = jsonSend["PIPELINE"][0]
            threadsList = list()
            for worker in range(workersCant):
                # Actualizamos el arreglo de fuentes a enviar
                jsonSend["SOURCES"] = sourcesNew
                url = workersNodes[worker].getURL(mode=modeToSend,
                                                endPoint=endPoint)
                initService = time.time()
                jsonSend['EXIT_TIME'] = initService
                workerID = workersNodes[worker].getID()
                t = threading.Thread(target=sendData, args=(url,jsonSend, numberEvent, sourcesNew, workerID))
                threadsList.append(t)
                t.start()

        serviceTime = exitTime-arrivalTime
        latenceTime = arrivalTime-exitTimeManager
        loggerInfo.info('MAP_DONE MEXICO {} {} {} {}'.format(serviceTime, arrivalTime, exitTime, latenceTime))
        jsonReturn ={
                "RETURN": "SUCCESSFULLY",
                "OPERATION": "MAP_DONE",
                "TIME_SERVICE": serviceTime,
                "ARRIVAL_TIME": arrivalTime,
                "EXIT_TIME": exitTime,
                "LATENCIE_TIME": latenceTime
            }

        return jsonify(jsonReturn)
    except:
        loggerError.error('MAP_ERROR MAP_SEND {}'.format(state['nodeId']))
        exitTime = time.time()
        serviceTime = exitTime-arrivalTime
        latenceTime = arrivalTime-exitTimeManager
        jsonReturn ={
                "RETURN": "SUCCESSFULLY",
                "OPERATION": "ERROR CLUSTERING_SEND",
                "TIME_SERVICE": serviceTime,
                "ARRIVAL_TIME": arrivalTime,
                "EXIT_TIME": exitTime,
                "LATENCIE_TIME": latenceTime
            }

        return jsonify(jsonReturn)

if __name__ == '__main__':
    presentation()
    app.run(host= '0.0.0.0',port=state['dockerPort'],debug=False,use_reloader=False)


import sys
import argparse
import pycurl

try:
	if len(sys.argv) < 3:
		print("Input invalid\nurltext.py <url> <list.txt>")
		sys.exit(0)
	else:
		website = sys.argv[1]
		filename = sys.argv[2]
except:
	sys.exit(0)

# filename = input("String list: ")
# website = input("Website: ")

print("Running check...\nFYI, this ignores all lines that begin with \"#\".\n")

try:
	file = open(filename,"r")
except:
	print("Unable to open file: ", filename)
	sys.exit(0)

c = pycurl.Curl();
c.setopt(pycurl.WRITEFUNCTION, lambda x: None)

try:
	c.setopt(c.URL, website)
except:
	print("Couldn't connect to: ", website)
	sys.exit(0)

for line in file:
	if line[0] == "#":
		url = line[:-2]
		c.setopt(c.URL, website + url)

		try:
			c.perform()
		except:
			print("Failed to execute with path: ", website + url)

file.close()

print("Check completed.")


from flask import Flask
from flask_cors import CORS
import torch
import numpy as np
import requests
import numpy as np
import requests
from data_loader import NERDataset
from torch.utils.data import DataLoader
from torchcrf import CRF

app = Flask(__name__)  # 如果是单独应用可以使用__name__，如果是module则用模块名
# Flask还有其他参数https://blog.csdn.net/YZL40514131/article/details/122730037
CORS(app, supports_credentials=True)  # 解决跨域

# 序列字典
tag2id = {
    "O": 0,
    "B-NAME": 1,
    "I-NAME": 2,
    "B-NOTIONAL": 4,
    "I-NOTIONAL": 5,
    "B-TICKER": 6,
    "I-TICKER": 7,
    '[CLS]': 10,
    '[SEP]': 11
}

id2tag = {_idx: _tag for _tag, _idx in list(tag2id.items())}

@app.route('/123')
def hello():
    return "hello world"

@app.route('/nlp/<string>')
def ner_function(string):

    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    #print (device)
    #device = 'cpu'
    model = torch.load('save_model.pth')
    #print(model)
    model.to(device)
    word2ix = np.load('vocab.npy', allow_pickle='TRUE').item() # 加载词典
    pred_tags = [] #

    split_word_list = string.split()
    low_word_list = [_t.lower() for _t in split_word_list] # 单词列表

    # sents = preprocess(word_list, word2ix) # 转化得来的向量
    label_list = []
    for i in range(0,len(low_word_list)):
        label_list.append('O')
    low_word_list = [low_word_list]
    label_list = [label_list]
    dataset = NERDataset(low_word_list, label_list, word2ix, tag2id, len(word2ix)-1)

    loader = DataLoader(dataset, batch_size=1,
                            shuffle=True, collate_fn=dataset.collate_fn)

    for idx, batch_samples in enumerate(loader):
        sentences, labels, masks, lens = batch_samples
        sentences = sentences.to(device)
        labels = labels.to(device)
        masks = masks.to(device)
        y_pred = model.forward(sentences)
        labels_pred = model.crf.decode(y_pred, mask=masks)
        pred_tags.extend([[id2tag.get(idx) for idx in indices] for indices in labels_pred])

    # labels_pred = model(sents)

    print(pred_tags)

    word_list = [[_t for _t in split_word_list]]
    results = entity(word_list, pred_tags) # 找到对应的实体

    return results

def preprocess(seq, to_ix):
    idxs = []
    for w in seq:
        if w in to_ix.keys():
            idxs.append(to_ix[w])
        else:
            idxs.append(len(to_ix))
    return torch.tensor(idxs, dtype=torch.long)

def entity(word_list, labels):
    # 变成实体返回
    """
        输入是字符
    """
    results = []
    for i in range(0,len(word_list[0])):
        if labels[0][i][0] == 'B':
            word = word_list[0][i]
            for j in range(i+1,len(word_list[0])):
                if labels[0][j][0] == 'I':
                    word += ' ' + word_list[0][j]
                else:
                    break
            label = labels[0][i].split('-')[1]

            results.append([word,label])

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


from pyEasyML import Settings
import os
settings = Settings(os.path.abspath(__file__))

import pandas as pd
from pyEasyML.Data.DataPreprocessing import DataPreprocessor
from pyEasyML.Data.FeatureSelection import FeatureSelector
from sklearn.feature_selection import r_regression
from pyEasyML.Classification.Classifier import Classifier

def save_cleaned_dataset(data_preprocessor:DataPreprocessor) -> None:
    dataset = data_preprocessor.read_dataset(data_preprocessor.DATASET_PATH)
    dataset = clean_dataset(dataset)
    data_preprocessor.save_dataset(dataset, data_preprocessor.DATASET_PATH)

def clean_dataset(df:pd.DataFrame) -> pd.DataFrame:
    """Cleans the dataset by removing non-numeric columns and handling missing values.
    This is a standard implementation, but you can implement your own.

    Returns:
        tuple[pd.DataFrame, ...]: The cleaned dataset.
    """
    df = df.apply(pd.to_numeric, errors='coerce')
    df = df.select_dtypes(include=['number'])
    df = handle_missing_values(df)

    return df

def handle_missing_values(dataset:pd.DataFrame) -> pd.DataFrame:
    """Handles missing values by removing columns with all missing values and filling the rest with the mean of the column.
    This is a standard implementation, but you can implement your own.

    Args:
        dataset (pd.DataFrame): The dataset to be cleaned.

    Returns:
        pd.DataFrame: The cleaned dataset.
    """
    X = dataset.columns.to_list()
    for x in X:
        if dataset[x].isna().all():
            dataset.drop(labels=[x], axis=1, inplace=True)
        elif dataset[x].isna().any():
            dataset[x] = dataset[x].fillna(dataset[x].mean())

    return dataset

def balance_dataset(train_val_dataset:pd.DataFrame) -> pd.DataFrame:
    """Balances the dataset due to rarity of missfire events. This is a standard implementation, but you can implement your own.

    Args:
        train_val_dataset (pd.DataFrame): The dataset to be balanced.

    Returns:
        pd.DataFrame: The balanced dataset.
    """
    healthy = train_val_dataset[train_val_dataset[settings.get_target_feature()] == 0]
    anomalous = train_val_dataset[train_val_dataset[settings.get_target_feature()] == 1]

    anomalous_size = len(anomalous)
    resized_healthy = healthy.sample(n=anomalous_size, random_state=42)

    concat_trained_val_dataset = pd.concat([resized_healthy, anomalous])

    train_val_dataset = concat_trained_val_dataset.sample(frac=1, random_state=42)

    return train_val_dataset

settings.set_target_feature("") # Place the name of your missfire column here.

data_preprocessor = DataPreprocessor()
data_preprocessor.DATASET_PATH = "" # Place the path of your dataset here.

#save_cleaned_dataset(data_preprocessor) # Uncomment this line if you want to clean your dataset. If dataset is already cleaned, you can skip this step.

train_val_dataset, test_dataset = data_preprocessor.get_train_val_test_datasets()

#train_val_dataset = balance_dataset(train_val_dataset) # Uncomment this line if you want to balance your dataset.

X_train, X_test, Y_train, Y_test = data_preprocessor.gen_train_test_datasets(dataset=train_val_dataset, columns=train_val_dataset.columns)

feature_selector = FeatureSelector(
    X_train, Y_train,
    model_name="GradientBoostingClassifier", # or "XGBClassifier", "GradientBoostingClassifier", "LogisticRegression", "SVC", "KNN"
    columns=train_val_dataset.columns
)

selected_features = feature_selector.select_k_best(k=5, func=r_regression) # or select_percentile, sequential_feature_selector, recursive_feature_elimination, select_from_model
print(selected_features)
settings.set_selected_features(selected_features)

X_train, X_test, Y_train, Y_test = data_preprocessor.gen_train_test_datasets(dataset=train_val_dataset)

classifier = Classifier(
                    model_name="GradientBoostingClassifier", # or "XGBClassifier", "GradientBoostingClassifier", "LogisticRegression", "SVC", "KNN"
                    default_columns=True,
                    default_target=True,
                    #...param_1 = value_1,...,param_n = value_n
                )
classifier.fit(X_train=X_train, Y_train=Y_train)

confusion_matrix = classifier.evaluate(X_test=X_test, Y_test=Y_test)
print(confusion_matrix)


import util
import matplotlib.pyplot as plt
from gda import GDA
from logreg import LogisticRegression
import numpy as np
from matplotlib.patches import Ellipse
import matplotlib.transforms as transforms

def main(train_path, valid_path):
    """
        Compares the performance between GDA and logistic regression classifiers
    Args:
        train_path: Path to CSV file containing dataset for training.
        valid_path: Path to CSV file containing dataset for validation.
    """

    # Load dataset
    x_train, y_train = util.load_dataset(train_path, add_intercept=False)
    # x_train[:, 1] = x_train[:, 1] / np.exp(x_train[:, 0])

    # Creates gaussian discriminant analysis classifer
    gda_clf = GDA()
    log_clf = LogisticRegression()

    # Train the models using training data
    gda_clf.fit(x_train, y_train)
    log_clf.fit(util.add_intercept(x_train), y_train)

    # Load validation data set
    x_val, y_val = util.load_dataset(valid_path, add_intercept=False)
    # x_val[:, 1] = x_val[:, 1] / np.exp(x_val[:, 0])

    # Plot validation data set
    fig, ax = plt.subplots()
    ax.plot(x_val[y_val == 1, -2], x_val[y_val == 1, -1], 'bx', linewidth=2)
    ax.plot(x_val[y_val == 0, -2], x_val[y_val == 0, -1], 'go', linewidth=2)

    # Plot decision boundaries (found by solving for theta^T x = 0)
    x1 = np.arange(min(x_val[:, -2]), max(x_val[:, -2]), 0.01)
    x2_gda = -(gda_clf.theta[0] / gda_clf.theta[2] + gda_clf.theta[1] / gda_clf.theta[2] * x1)
    x2_log = -(log_clf.theta[0] / log_clf.theta[2] + log_clf.theta[1] / log_clf.theta[2] * x1)
    ax.plot(x1, x2_gda, c='red', linewidth=2, label="GDA Decision Boundary")
    ax.plot(x1, x2_log, c='black', linewidth=2, label="Logistic Regression Decision Boundary")

    # Plot GDA Confidence Ellipses
    confidence_ellipse(gda_clf.mu_0, gda_clf.sigma, ax, edgecolor='green', linewidth=2)
    confidence_ellipse(gda_clf.mu_1, gda_clf.sigma, ax, edgecolor='blue', linewidth=2)

    ax.set_xlim(x_val[:, -2].min()-.1, x_val[:, -2].max()+.1)
    ax.set_ylim(x_val[:, -1].min()-.1, x_val[:, -1].max()+.1)

    # Add labels and a legend
    ax.set_xlabel('x1')
    ax.set_ylabel('x2')
    ax.legend()

def confidence_ellipse(mu, cov, ax, n_std=2.0, facecolor='none', **kwargs):
    """
    Create a plot of the covariance confidence ellipse.
    Based on the function from https://matplotlib.org/devdocs/gallery/statistics/confidence_ellipse.html.

    Args:
        mu: array-like, shape (2, )
            Mean of the gaussian data

        cov: array-like, shape (2, 2)
            Covariance matrix

        ax: matplotlib.axes.Axes
            The axes object to draw the ellipse into.

        n_std: float
            The number of standard deviations to determine the ellipse's radii.

        **kwargs:
            Forwarded to `~matplotlib.patches.Ellipse

    Returns:
        matplotlib.patches.Ellipse
    """

    pearson = cov[0, 1]/np.sqrt(cov[0, 0] * cov[1, 1])
    # Using a special case to obtain the eigenvalues of this
    # two-dimensional dataset.
    ell_radius_x = np.sqrt(1 + pearson)
    ell_radius_y = np.sqrt(1 - pearson)
    ellipse = Ellipse((0, 0), width=ell_radius_x * 2, height=ell_radius_y * 2,
                      facecolor=facecolor, **kwargs)

    # Calculating the standard deviation of x from
    # the square root of the variance and multiplying
    # with the given number of standard deviations.
    scale_x = np.sqrt(cov[0, 0]) * n_std
    mean_x = mu[0]

    # calculating the stdandard deviation of y ...
    scale_y = np.sqrt(cov[1, 1]) * n_std
    mean_y = mu[1]

    transf = transforms.Affine2D() \
        .rotate_deg(45) \
        .scale(scale_x, scale_y) \
        .translate(mean_x, mean_y)

    ellipse.set_transform(transf + ax.transData)
    return ax.add_patch(ellipse)

if __name__ == '__main__':
    main(train_path='ds1_train.csv',
         valid_path='ds1_valid.csv')

    main(train_path='ds2_train.csv',
         valid_path='ds2_valid.csv')