#!/usr/local/env python
from pathlib import Path

with (Path(__file__).parent / "input.txt").open() as text:
    lines = [ line.strip() for line in text.read().split("\n") if line]

def find_all_uniques_in_list(solution: dict, _list:list):
    def find_unique(x: str):
        if len(x) == 2 or len(x) == 3 or len(x) == 4 or len(x) == 7:
            return len(x)
        return None
    for x in _list:
        unique = find_unique(x)
        if unique is not None:
            solution[unique] = x

def get_remainder(i: str, o: str):
    return [x for x in list(i) + list(o) if  x not in i]

def decipher(solution: dict, left: list):
    one = solution[2]
    seven = solution[3]
    four = solution[4]
    eight = solution[7]
    ret_str = ""
    for f in left:
        c_one = len(get_remainder(f, one))
        c_four = len(get_remainder(f, four))
        c_seven = len(get_remainder(f, seven))
        c_eight = len(get_remainder(f, eight))
        if c_one == 0 and c_four == 0 and c_seven == 0 and c_eight == 1:
            ret_str += "9"
        elif c_one == 1 and c_four == 1 and c_seven == 1 and c_eight == 1:
            ret_str += "6"
        elif c_one == 1 and c_four == 1 and c_seven == 1 and c_eight == 2:
            ret_str += "5"
        elif c_eight == 2 and c_seven == 0 and c_one == 0 and c_four == 1:
            ret_str += "3"
        elif c_eight == 2 and c_seven == 1 and c_one == 1 and c_four == 2:
            ret_str += "2"
        elif c_eight == 1 and c_four == 1 and c_seven == 0 and c_one == 0:
            ret_str += "0"
        elif c_eight == 5 and c_one == 0 and c_four == 2 and c_seven == 1:
            ret_str += "1"
        elif c_eight == 3 and c_four == 0 and c_seven == 1 and c_one == 0:
            ret_str += "4"
        elif c_eight == 4 and c_seven == 0 and c_one == 0 and c_four == 2:
            ret_str += "7"
        elif c_eight == 0:
            ret_str += "8"
        else:
            print(f, "---------")
            print(list(f) + list(one))
            print(one, c_one)
            print(four, c_four)
            print(seven, c_seven)
            print(eight, c_eight)
            exit(1)
    return int(ret_str)

def solve2(init_list: list) -> int:
    counter = 0
    solution = { k: "" for k in range(0,10)}
    for line in init_list:
        inp, outp = line.split(" | ")
        inputs = inp.split(" ")
        outputs = outp.split(" ")
        find_all_uniques_in_list(solution, inputs)
        find_all_uniques_in_list(solution, outputs)
        counter += decipher(solution, outputs)
    return counter

example = [ e.strip() for e in """be cfbegad cbdgef fgaecd cgeb fdcge agebfd fecdb fabcd edb | fdgacbe cefdb cefbgd gcbe
edbfga begcd cbg gc gcadebf fbgde acbgfd abcde gfcbed gfec | fcgedb cgb dgebacf gc
fgaebd cg bdaec gdafb agbcfd gdcbef bgcad gfac gcb cdgabef | cg cg fdcagb cbg
fbegcd cbd adcefb dageb afcb bc aefdc ecdab fgdeca fcdbega | efabcd cedba gadfec cb
aecbfdg fbg gf bafeg dbefa fcge gcbea fcaegb dgceab fcbdga | gecf egdcabf bgf bfgea
fgeab ca afcebg bdacfeg cfaedg gcfdb baec bfadeg bafgc acf | gebdcfa ecba ca fadegcb
dbcfg fgd bdegcaf fgec aegbdf ecdfab fbedc dacgb gdcebf gf | cefg dcbef fcge gbcadfe
bdfegc cbegaf gecbf dfcage bdacg ed bedf ced adcbefg gebcd | ed bcgafe cdgba cbgef
egadfb cdbfeg cegd fecab cgb gbdefca cg fgcdab egfdb bfceg | gbdfcae bgc cg cgb
gcafb gcf dcaebfg ecagb gf abcdeg gaef cafbge fdbac fegbdc | fgae cfgab fg bagce
""".split("\n") if e ]

simple_example = ["acedgfb cdfbe gcdfa fbcad dab cefabd cdfgeb eafb cagedb ab | cdfeb fcadb cdfeb cdbaf"]

spart2 = solve2(simple_example)
print('Example part 2:', spart2)
assert spart2 == 5353

part2 = solve2(example)
print('Example part 2:', part2)
assert part2 == 61229
print('Part 2:', solve2(lines))


#!/usr/bin/python3
""" Tests for the Person class """

import inspect
import unittest
import models
import pep8 as pycodestyle

from datetime import datetime
from models.engine import storage
Person = models.person.Person
module_doc = models.person.__doc__

class TestPersonDocs(unittest.TestCase):
    """
        Tests to check the documentation and style of Person class
    """

    @classmethod
    def setUpClass(self):
        """ SetUp for docstring tests """

        self.person_funcs = inspect.getmembers(Person, inspect.isfunction)

    def test_pep8_conformance(self):
        """ Test that models/person.py conforms to PEP8. """

        for path in ['models/person.py',
                     'tests/test_models/test_person.py']:
            with self.subTest(path=path):
                errors = pycodestyle.Checker(path).check_all()
                self.assertEqual(errors, 0)

    def test_module_docstring(self):
        """ Test for the existence of module docstring """

        self.assertIsNot(module_doc, None,
                         "person.py needs a docstring")
        self.assertTrue(len(module_doc) > 1,
                        "person.py needs a docstring")

    def test_class_docstring(self):
        """ Test for the Person class docstring """

        self.assertIsNot(Person.__doc__, None,
                         "Person class needs a docstring")
        self.assertTrue(len(Person.__doc__) >= 1,
                        "Person class needs a docstring")

    def test_func_docstrings(self):
        """ Test for the presence of docstrings in Person methods """

        for func in self.person_funcs:
            if func[0] != 'get_id':
                with self.subTest(function=func):
                    self.assertIsNot(
                        func[1].__doc__,
                        None,
                        "{:s} method needs a docstring".format(func[0])
                    )
                    self.assertTrue(
                        len(func[1].__doc__) > 1,
                        "{:s} method needs a docstring".format(func[0])
                    )

class TestPerson(unittest.TestCase):
    """ Tests for the person class """

    def test_init(self):
        """ Tests the initialization of the person class """

        pass


import json

class Model:
    title = "title_val"
    text = "text_val"
    author = "author_val"

    def save(self):
        d = {}
        for attr in dir(Model):
            if attr == "save" or attr.startswith("_"):
                continue
            d[attr] = getattr(self, attr)
        with open('model.json', 'w') as f:
            json.dump(d, f)
        print(d)

m = Model()
m.save()


import base64
import httplib
import json

""" returns a zip file from a github PUBLIC repo.
parameters: filename on github repo is the repository name on
github repo_owner is the owner of the repo
returns: zip file obtained
"""
def get_zipfile(filename, repo, repo_owner):
    # form call components and make google call
    host = 'api.github.com'
    body = ''
    path = '/repos/' + repo_owner + '/' + repo + '/contents/' + filename
    method = 'GET'
    # make connection using https
    github = httplib.HTTPSConnection(host)
    # make request
    github.request(
        method,
        path,
        body,
        {"User-Agent" : "mkSpace Application"}
        )
    response = github.getresponse()
    if response.status != 200:
        return False, response.status

    ghobj = json.loads(response.read())
    # decode content field of returned object
    zip_file = base64.b64decode(ghobj['content'])

    return True, zip_file


from dataclasses import dataclass

from global_var import COST_DIVISION
from qsort import quick_sort

@dataclass(frozen=True)
class Technic:
    name: str
    cost: float
    avail: bool

if __name__ == '__main__':
    """ Задание 1."""
    tech = [
        Technic('denn', 1.0, True),
        Technic('pin', 501.0, True),
        Technic('cisco', 300.2, False)
        # Add more technic
    ]

    # def set_category(product_cost: float) -> str:
    #     return '"budget"' if product_cost < COST_DIVISION else '"costly"'
    #
    #
    # for t in tech:
    #     print(
    #         ' '.join(['Product name:', t.name]),
    #         ' '.join(['Category:', set_category(t.cost)]),
    #         ' '.join(['In stock:', 'exist' if t.avail else 'not exist'])
    #     )

    """ Задание 2."""
    # Magic method 1
    product_names = [tech[i].name for i in range(len(tech))]
    product_names.sort(key=len)
    print(product_names)

    # Magic method 2
    print(sorted(tech, key=lambda tech: len(tech.name)))

    # My function quick sort
    quick_sort(tech, 0, len(tech) - 1)
    print(tech)


import getopt
import sys
import time
import datetime
import socket
import select
#import threading

host = '127.0.0.1'
port = 1518

line_fields = '{0:>8.8s} {1:>5.5s} {2:>5.5s} {3:>7.7s} {4:>5.5s} {5:>5.5s} {6:>5.5s} {7:>5.5s} {8:>5.5s} {9:>5.5s} {10:>5.5s} {11:>5.5s} {12:>5.5s} {13:>5.5s} {14:>5.5s} {15:>5.5s} {16:>5.5s} {17:>4.4s} {18:>5.5s} {19:>5.5s} {20:>5.5s} {21:>1.1s}'

response = ''

def usage ():
    print('python feedmonitor.py -h host -p port')

def filter_chars(chars):
    filtered_string = ''

    for i in range(len(chars)):
        char = chars[i]
        if ((char == '|') or (ord(char) == 0x0d) or (ord(char) == 0x0a)):
            filtered_string = filtered_string + chr(0x0d)
            continue
        if ((ord(char) >= 0x20) and (ord(char) <= 0x7e)):
            filtered_string = filtered_string + char

    return (filtered_string)

def get_line(sckt):
    global response

    timeout_count = 0
    while (timeout_count < 9):
        lines = response.split(chr(0x0d), 1)
        if (len(lines) == 2):
            response = lines[1]
            return (lines[0])
        readable, writable, exceptional = select.select([sckt], [], [], 0.1)
        if (readable):
            response = response + filter_chars(sckt.recv(999))
            timeout_count = 0
        else:
            timeout_count = timeout_count + 1

    return (None)  # timeout

def purge_response(sckt):
    global response

    response = ''

    timeout_count = 0
    while (timeout_count < 2):
        readable, writable, exceptional = select.select([sckt], [], [], 0.1)
        if (readable):
            sckt.recv(999)
            timeout_count = 0
        else:
            timeout_count = timeout_count + 1

def get_and_print_status(sckt):
    dt = datetime.datetime.now()

    status_line = line_fields.format(
        dt.strftime('%H:%M:%S'),
        session(sckt, 'TC', 1, 1, cnvrt_temp),
        session(sckt, 'gd', 0, 1, cnvrt_temp),
        session(sckt, 'gv', 0, 1, cnvrt_vacuum),
        session(sckt, 'p398', 0, 1, cnvrt_turbo_rpm),
        session(sckt, 'p310', 0, 1, cnvrt_turbo_amps),
        session(sckt, 'p316', 0, 1, cnvrt_turbo_watts),
        session(sckt, 'p326', 0, 1, cnvrt_turbo_temp),
        session(sckt, 'p330', 0, 1, cnvrt_turbo_temp),
        session(sckt, 'p342', 0, 1, cnvrt_turbo_temp),
        session(sckt, 'p346', 0, 1, cnvrt_turbo_temp),
#        session(sckt, 'gt a0', 0, 1, cnvrt_temp),
        session(sckt, 'gettemp a0', 0, 1, cnvrt_temp),
#        session(sckt, 'gt a1', 0, 1, cnvrt_temp),
        session(sckt, 'gettemp a1', 0, 1, cnvrt_temp),
        session(sckt, 'gt a2', 0, 1, cnvrt_temp),
        session(sckt, 'gt a3', 0, 1, cnvrt_temp),
        session(sckt, 'gt a5', 0, 1, cnvrt_temp),
        session(sckt, 'gt a6', 0, 1, cnvrt_temp),
        session(sckt, 'getfanrpm', 0, 1, cnvrt_fan_rpm),
        session(sckt, 'E', 1, 3, cnvrt_E_pwr),
        session(sckt, 'E', 1, 3, cnvrt_E_max),
        session(sckt, 'E', 1, 3, cnvrt_E_min),
        session(sckt, 'getcryoattemp', 0, 1, cnvrt_at_temp)
    )

    print(status_line)

def session(sckt, command, num_echo_lns, num_rspns_lns, cnvrt_func):
    for r in range(2):
        try:
            purge_response(sckt)
            lines = []
            sckt.send(command + '\r')
            for l in range(num_echo_lns):
                get_line(sckt)  # throw away echoed command
            # then get response line(s)
            for l in range(num_rspns_lns):
                lines.append('')
                ln = get_line(sckt)
                if (ln is None):
                    raise Exception('')
                lines[l] = ln
                if (not(len(lines[l]) > 0)):
                    raise Exception('')
            return (cnvrt_func(lines))
        except Exception:
            time.sleep(0.7)
            pass

    return ('----------')  # will fill because wider than any particular field

def cnvrt_temp(lines):
    return ('{0:5.1f}'.format(float(lines[0])))

def cnvrt_vacuum(lines):
    return ('{0:.1e}'.format(float(lines[0].split()[0])))

def cnvrt_turbo_rpm(lines):
    return ('{0:5d}'.format(int(lines[0], 10)))

def cnvrt_turbo_amps(lines):
    return ('{0:5.1f}'.format(int(lines[0], 10) / 100.0))

def cnvrt_turbo_watts(lines):
    return ('{0:5d}'.format(int(lines[0], 10)))

def cnvrt_turbo_temp(lines):
    return ('{0:5d}'.format(int(lines[0], 10)))

def cnvrt_fan_rpm(lines):
    return ('{0:4d}'.format(int(lines[0], 10)))

def cnvrt_E_pwr(lines):
    return ('{0:5.1f}'.format(float(lines[2])))

def cnvrt_E_min(lines):
    return ('{0:5.1f}'.format(float(lines[1])))

def cnvrt_E_max(lines):
    return ('{0:5.1f}'.format(float(lines[0])))

def cnvrt_at_temp(lines):
    if (lines[0].strip().startswith('y')):
        return ('Y')
    if (lines[0].strip().startswith('n')):
        return ('N')
    return (' ')

def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:], 'h:p:')
        for o,a in opts:
            if o in ('-h'):
                host = a
            if o in ('-p'):
                port = a
    except getopt.GetoptError:
        usage()
        sys.exit()

    if ((host == None) or (port == None)):
        usage()
        sys.exit()

    sckt = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    try:
        sckt.settimeout(3.0)
        sckt.connect((host, int(port)))
    except:
        print('exception while attempting connection to host ' + host + ' on port ' + port)
        sys.exit()

    time.sleep(0.3)

    sckt.settimeout(None)
    sckt.setblocking(1)

    purge_response(sckt)
    sckt.send(chr(0x0d))
    get_line(sckt);

    purge_response(sckt)
    sckt.send(chr(0x0d))
    get_line(sckt);

#   try three times
    for i in range(3):
        purge_response(sckt)
        sckt.send('stty')
        sckt.send(chr(0x0d))
        ln = get_line(sckt);
        if (not(ln is None) and (ln == 'OK')):
            get_and_print_status(sckt)
            break
        time.sleep(0.3)

    sckt.close()

main()


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

# Task 3:Calculate the number of identical pairs.

from __future__ import print_function

class CountidenticalPairs(object):
    @classmethod
    def main(cls, args):
        A = [None] * 6
        A[0] = 3
        A[1] = 5
        A[2] = 6
        A[3] = 3
        A[4] = 3
        A[5] = 5
        print(CountidenticalPairs().solution(A))

    def solution(self, A):
        #  use key-value store to store the unique pair
        lhm = LinkedHashMap()
        i = 0
        while len(A):
            if lhm.containsKey(A[i]):
                lhm.get(A[i]).add(i)
            else:
                a = ArrayList()
                a.add(i)
                lhm.put(A[i], a)
            i += 1

        # generate pairs
        pairs = 0
        it = lhm.keySet().iterator()
        while it.hasNext():
            key = it.next()
            # print(lhm.get(key));
            a = lhm.get(key)
            i = 1
            while i < len(a):
                pairs += i
                i += 1
            if pairs >= 1000000000:
                pairs = 1000000000
                break
        return int(pairs)


#@Author:pakchungy
#@Date:2021/5/27
#@Time:0:23
#@Project_name:finalproject

import time

import torch
import torch.nn as nn
from RNN import temperature_model_RNN as tm_RNN
import temperature_dataset as td
from torch.utils.data import DataLoader
import matplotlib.pyplot as plt
import numpy as np

from score import *

def test(model, test_loader, criterion):
    y_pred_list = []
    y_true_list = []
    loss = 0
    for i, (x, y) in enumerate(test_loader, 0):
        # 计算预测值
        y_pred = model(x)
        loss += criterion(y_pred, y)
        y_pred = y_pred.detach().numpy().reshape(y_pred.shape[0])
        y = y.detach().numpy().reshape(y.shape[0])
        if i == 0:
            y_pred_list = y_pred
            y_true_list = y
        else:
            y_pred_list = np.concatenate((y_pred_list, y_pred))
            y_true_list = np.concatenate((y_true_list, y))

    loss /= i
    # 画图
    plt.figure(figsize=(10, 5))
    plt.plot(y_true_list, 'r+', label='real data')
    plt.plot(y_pred_list, 'b*', label='pred data')
    plt.title('loss={}'.format(loss))
    plt.legend()
    plt.savefig('pre_temperature_RNN_3_test.jpg', dpi=256)
    plt.close()

    print(len(y_true_list))
    print(len(y_pred_list))

    mse(y_true_list, y_pred_list)
    rmse(y_true_list, y_pred_list)
    evs(y_true_list, y_pred_list)

if __name__ == '__main__':
    start_time = time.time()
    model = tm_RNN.TemperatureModel_RNN()
    # 加载数据
    test_loader = DataLoader(td.TemperatureDataset("C:\\Users\\ziting\\Desktop\\yanzheng\\E_weather\\weather\\meant_1901to2000.csv", mode="test", testset_ratio=1),
                             batch_size=64, shuffle=False)
    criterion = nn.MSELoss()
    # 加载模型

    # model = torch.load("CPU/temperature_model_RNN.pth")
    model = torch.load("RNN/GPU/3/temperature_model_RNN_with_GPU.pth", map_location=torch.device('cpu'))
    # 测试
    test(model, test_loader, criterion)
    print("总耗时:{}min".format((time.time() - start_time) / 60))


import time
import json
from google.cloud import pubsub_v1

with open('config/config.json') as config_file:
    config = json.load( config_file )

gcp_project_id = config['GCP_PROJECT_ID']
gcp_pubsub_topic_name = config['GCP_PUBSUB_TOPIC_NAME']

publisher = pubsub_v1.PublisherClient()
topic_path = publisher.topic_path( gcp_project_id, gcp_pubsub_topic_name )

futures = dict()

def main():

    def get_callback( fut, data ):
        def callback( fut ):
            try:
                print( "publish callback " + fut.result() )
                futures.pop( data )
            except:
                print( 'Please handle {} for {}.'.format( fut.exception(), data ) )
        return callback

    for i in range( 3 ):
        data = str( i )
        futures.update( { data: None } )
        future = publisher.publish( topic_path, data = data.encode( 'utf-8' ) )
        future.add_done_callback( get_callback( future, data ) )

    while futures:
        time.sleep( 5 )


import numpy as np
import imutils
import cv2
import cv2 as cv
import os
from skimage import morphology, measure
import argparse
import skimage.io as io
import matplotlib.pyplot as plt
import time

from skimage import color
from functools import partial
from mpl_toolkits.mplot3d import Axes3D

diameters = []

def process_one_image(val,path,factor,scale):
    img = cv2.imread(path)
    x,y,_ = img.shape
    img = cv2.resize(img,(y//scale,x//scale))

    kernel = np.ones((5,5),np.uint8)
    blurr_val = cv2.getTrackbarPos('blur','blur')
    canny = cv2.getTrackbarPos('canny','canny')
    size = cv2.getTrackbarPos('size','Low size')
    #size = size/10
    dst_val = cv2.getTrackbarPos('dst_va','dst_va')
    dst_val = dst_val/100
    temp_image = img.copy()
    img = cv2.bilateralFilter(img,blurr_val,75,75)
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

    #gray = cv2.Canny(gray,canny_val,2*canny_val)
    gray = cv.Canny(gray, threshold1 = canny , threshold2 = 2*canny)

    gray = cv2.dilate(gray,kernel,iterations = 1)
    ret, thresh = cv2.threshold(gray,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)

    # noise removal
    opening = cv2.morphologyEx(thresh,cv2.MORPH_OPEN,kernel, iterations = 1)
    closing = cv2.morphologyEx(opening,cv2.MORPH_CLOSE,kernel, iterations = 1)

    # sure background area
    sure_bg = cv2.dilate(thresh,kernel,iterations=1)

    # Finding sure foreground area
    dist_transform = cv2.distanceTransform(sure_bg,cv2.DIST_L2,3)

    # Threshold
    ret, sure_fg = cv2.threshold(dist_transform,dst_val*dist_transform.max(),255,0)

    # Finding unknown region
    sure_fg = np.uint8(sure_fg)
    unknown = cv2.subtract(sure_bg,sure_fg)

    # Marker labelling
    ret, markers = cv2.connectedComponents(sure_fg)

    # Add one to all labels so that sure background is not 0, but 1
    markers = markers+1

    # Now, mark the region of unknown with zero
    markers[unknown==255] = 0

    markers = cv2.watershed(img,markers)

    img[markers == -1] = [255,0,0]

  #------------------------------------
    import imutils
    import random as rng
    contours = []

    # loop over the unique labels, and append contours to all_cnts
    labels = markers
    for label in np.unique(labels):
        if label == 0:
            continue

        # draw label on the mask
        mask = np.zeros(thresh.shape, dtype="uint8")
        mask[labels == label] = 255

        # detect contours in the mask and grab the largest one
        cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
        #im2, cnts, hierarchy = cv2.findContours(mask.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
        cnts = imutils.grab_contours(cnts)
        c = max(cnts, key=cv2.contourArea)
        for c in cnts:
            contours.append(c)
    contours = sorted(contours, key=lambda x: cv2.contourArea(x),reverse = True)[2:]

    global diameters
    for c in contours:
        #Ignore small contours
        x,y,_ = img.shape
        if cv2.contourArea(c) > 1 and cv2.contourArea(c) < (x*y)/4:
            color = (rng.randint(0,256), rng.randint(0,256), rng.randint(0,256))
            ((x, y), r) = cv2.minEnclosingCircle(c) #finding diameters of contours
            l = round(2*r*factor,2)
            if l > size:
                diameters.append(l)
                cv2.drawContours(temp_image,[c], 0, color,2)
                #cv2.circle(temp_image, (int(x), int(y)), int(r),(0,255,0), 1)
                #cv2.putText(temp_image, "{}cm".format(2*round(r*factor,2)), (int(x) - 10, int(y)),cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,255,0), 2)

    cv2.imshow('image', temp_image)
    return #[temp_image,contours]

def fine_tune(path,factor,scale):

	cv2.namedWindow('blur', cv2.WINDOW_NORMAL)
	cv2.createTrackbar('blur','blur',25,500,partial(process_one_image,path=path,factor = factor,scale = scale))

	cv2.namedWindow('canny', cv2.WINDOW_NORMAL)
	cv2.createTrackbar('canny','canny',100,255,partial(process_one_image,path=path,factor = factor,scale = scale))
	cv2.namedWindow('Low size', cv2.WINDOW_NORMAL)
	cv2.createTrackbar('size','Low size',1,500,partial(process_one_image,path=path,factor = factor,scale = scale))
	cv2.namedWindow('dst_va', cv2.WINDOW_NORMAL)
	cv2.createTrackbar('dst_va','dst_va',10,500,partial(process_one_image,path=path,factor = factor,scale = scale))

	process_one_image(0,path,factor,scale)

	cv2.waitKey()
	blurr_val = cv2.getTrackbarPos('blur','blur')
	canny = cv2.getTrackbarPos('canny','canny')
	size = cv2.getTrackbarPos('size','Low size')
	#size = size/10
	dst_val = cv2.getTrackbarPos('dst_va','dst_va')
	dst_val = dst_val/100
	cv2.destroyAllWindows()
	return (blurr_val,canny,size,dst_val)


import logging

from django.conf import settings
from django.db.models.signals import post_save

logger = logging.getLogger(__name__)

def post_save_receiver(sender, instance, created, **kwargs):
    """
    Placeholder method to execute logic when a custom user is saved or updated.
    """

    # Just logging for now to check functionality
    logger.debug(
        "Hey Veer you just saved/updated a CustomUser!\n \
        I am the post_save hook that got triggered"
    )
    logger.debug("sender %s" % sender)
    logger.debug("instance %s" % instance)
    logger.debug("created %s" % created)
    logger.debug("kwargs %s" % kwargs)

    # Your stuff here

# Wire up our callback with active AUTH USER MODEL
post_save.connect(
    post_save_receiver,
    sender=settings.AUTH_USER_MODEL,
    dispatch_uid="post_save_receiver",
)


import kivy
kivy.require('1.9.1')

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.core.window import Window

class MainApp(App):
    def build(self):
        main = BoxLayout(orientation='vertical', height=800)
        grid = GridLayout(cols=1)
        title = Label(text='ytDl', size_hint_y=None, height=200, font_size=150)
        songLabel = Label(text='Song:', size_hint_y=None, height=150, font_size=100)
        artistLabel = Label(text='Artist:',  size_hint_y=None, height=150, font_size=100)
        song = TextInput(multiline=False, size_hint_y=None, height=100, font_size=80)
        artist = TextInput(multiline=False, size_hint_y=None, height=100, font_size=80)
        download = Button(text='Download', size_hint_y=None, height=100, font_size=80)

        grid.add_widget(title)
        grid.add_widget(songLabel)
        grid.add_widget(song)
        grid.add_widget(artistLabel)
        grid.add_widget(artist)
        grid.add_widget(download)
        main.add_widget(grid)

        return main

if __name__=='__main__':
    Window.size = (500, 800)
    MainApp().run()


from SearchEngine.Configuration import Configuration
from SearchEngine.SearchEngine import SearchEngine

config = Configuration()
config.steamer = "lancaster"
config.verbose_steamer = True

se = SearchEngine(config)

se.scan_directory("archivos/")
se.armar_archivos_estadisticos()


# SOAL IMT
massa = int(input("Masukkan Massa : "));
tinggi = int(input("Masukkan Tinggi : "));

text = "";
IMT = massa/((tinggi/100)**2);

if(IMT < 18.5):
    text = "BERAT BADAN KURANG !";
elif(IMT >= 18.5 and IMT <= 24.9):
    text = "BERAT BADAN IDEAL !";
elif(IMT >= 25.0 and IMT <= 29.9):
    text = "BB Berlebih";
elif(IMT >= 30.0 and IMT <= 39.9):
    text = "BB SANGAT BERLEBIH !";
else:
    text = "OBESITAS";

print("Massa" + str(massa) + " kg & tinggi" + str(tinggi) + " m");
print("IMT = " + str(IMT) + " ," + text);


from concurrent.futures import ProcessPoolExecutor
from time import sleep

def task(message):
    print('Sleeping...')
    sleep(2)
    print("Done Sleeping")
    return message

def main():
    executor = ProcessPoolExecutor(4)  # matches # of cores on your machine
    future = executor.submit(task, "Completed")
    print(f'Future isDone Status: {future.done()}')
    # sleep(3)
    # print(f'Future isDone Status: {future.done()}')
    # print(f'Future Result: {future.result()}')

if __name__ == '__main__':
    main()


import sys
import itertools

def processTest():
    LINE = [int(x) for x in f.readline()[:-1].split(' ')]
    R = LINE[0]
    C = LINE[1]
    N = LINE[2]

    minnoise = 100000000000
    for el in itertools.combinations(range(R*C),N):
        arr = [[0 for j in range(C)] for i in range(R)]
        for i in range(R):
            for j in range(C):
                if i+j*R in el:
                    arr[i][j] = 1
        #print(arr)
        def countNoise(arr):
            noiselvl = 0
            for i in range(R):
                for j in range(C):
                    if arr[i][j]>0:
                        if i<R-1 and arr[i+1][j]>0:
                            noiselvl +=1
                        if j<C-1 and arr[i][j+1]>0:
                            noiselvl +=1
            return noiselvl
        noise = countNoise(arr)
        if noise<minnoise:
            minnoise = noise
    return minnoise

f = open(sys.argv[1],'r')
maxtests = int(f.readline())
testnum = 1
while testnum <= maxtests:
    y = processTest()
    print('Case #'+str(testnum)+': '+ str(y))
    testnum+=1

f.close()


from os import listdir, rename, path, mkdir, walk, rmdir
from os.path import join, isfile
import os
import shutil
import pandas as pd

#Read Labels
labels = pd.read_csv('./labels.csv')
lables_indexes = labels.iloc[:,0]
labels.head().iloc[:,0]

#Create subfolders for each label
test_dir = './traffic_Data/TEST/'
for index, value in lables_indexes.items():
    label_dir_path = join(test_dir, str(value))
    mkdir(label_dir_path)

#Move TEST files to each subfolder
for filename in listdir(test_dir):
    if(isfile(join(test_dir, filename))):
        try:
            class_label = int(filename[0:3])
            rename(join(test_dir, filename), join(test_dir, str(class_label), filename))
        except:
            print(f'{filename} is not a TEST file')

#Remove empty folders
def remove_empty_folders(path_abs):
    walk = list(os.walk(path_abs))
    for path, _, _ in walk[::-1]:
        if len(listdir(path)) == 0:
            rmdir(path)

remove_empty_folders(test_dir)


import multiprocessing

from queue import Queue
from threading import Thread
from validations import validate_column, validate_line, validate_region
from concurrent.futures import ThreadPoolExecutor, Future
from SudokuThread import SudokuThread, Semaphore
from typing import Callable

"""
This is a file with multiple Game validations functions,
create threads and print results functions.

"""

def create_threads(n: int) -> list[SudokuThread]:
    """ Creates the threads

    Parameters
    ----------
    n : int
        The number of threads to be created

    Return
    ------
    list[SudokuThread]
        The list of threads created (SudokuThread's elements)

    """

    threads = []
    for _ in range(n):
        finished_lock = Semaphore(0)
        main_thread_lock = Semaphore(0)

        thread = SudokuThread(
            lock=main_thread_lock,
            finished_lock=finished_lock,
            id=_
        )

        thread.start()
        threads.append(thread)

    return threads

def print_results(results: dict[int, list[str]], process_number: int, n_threads: int = None) -> None:
    """ Print the results of the validation of a puzzle

    Parameters
    ----------
    results : dict
        The dictionary containing one result string for each validated puzzle
    process_number : int
        The number of the process that is being executed
    n_threads : int
        The number of threads that the program is using

    Return
    ------
    None

    """
    if len(results.keys()) == 0:
        print(f"Processo {process_number}: 0 erros encontrados")
        return

    errors = []
    n_erros = 0

    for thread_num in results.keys():
        n_erros += len(results[thread_num])
        error = f"T{thread_num+1}: "
        lines = []
        columns = []
        regions = []
        results[thread_num] = sorted(results[thread_num])
        for res in results[thread_num]:
            if "L" in res:
                lines.append(res)
            elif "C" in res:
                columns.append(res)
            else:
                regions.append(res)

        error += ', '.join(lines + columns + regions)
        errors.append(error)

    errors = '; '.join(errors)
    print(f"Processo {process_number}: {n_erros} erros encontrados ({errors})")

def validate_game_creating_threads_once(solutions: list[list[list[int]]], solution_number, n_threads) -> None:
    """ Validates the game creating threads once and dividing
    the validations for the number of threads in each process

    Parameters
    ----------
    solution : list[list[list[int]]]
        The solution that one process is going to validate
    solution_number : int
        The number of the puzzle that is being validated
    n_threads : int
        The number of threads that the program is using

    Return
    ------
    None

    """
    process_number = multiprocessing.current_process().name[8:]
    functions_list = [validate_line, validate_column, validate_region]
    threads = create_threads(n_threads)
    for i in range(len(solutions)):
        print(f"Processo {process_number}: resolvendo quebra-cabeças {solution_number+i}")
        results = {}
        n_funcition = 0

        while n_funcition != 27:
            for thread in threads:
                if n_funcition == 27:
                    thread.reset(target=None, args=None) # a thread não executa em tal iteração
                    thread.lock.release()
                    continue

                thread.reset(target=functions_list[n_funcition % 3], args=(n_funcition//3, solutions[i], ))
                thread.lock.release() # indica que a thread já pode começar a executar a função destinada a ela

                n_funcition +=1

            for thread in threads:
                thread.finished_lock.acquire() # espera até que a thread termine de executar a função

                if thread.result is None:
                    continue

                result, thread_num = thread.result
                if thread_num in results.keys():
                    results[thread_num].add(result)
                else:
                    results[thread_num] = set([result])

        print_results(results, process_number, n_threads)

    for thread in threads:
        thread.stop()

def validate_game_creating_threads_once_and_using_thread_pool(solutions: list[list[list[int]]], solution_number: int, n_threads: int) -> None:
    """ Validates the game using the creating threads once
    and thread pool executor method

    Parameters
    ----------
    solution : list[list[list[int]]]
        The solutions that one process is going to validate
    solution_number : int
        The number of the puzzle that is being validated
    n_threads : int
        The number of threads that the program is using

    Return
    ------
    None

    """
    process_number = multiprocessing.current_process().name[8:]
    with ThreadPoolExecutor(max_workers=n_threads, thread_name_prefix="Thread") as pool:
        functions_list = [validate_line, validate_column, validate_region]

        for i,solution in enumerate(solutions):
            print(f"Processo {process_number}: resolvendo quebra-cabeças {solution_number+i}")
            results = {}

            futures = []
            for j in range(27):
                futures.append(pool.submit(functions_list[j % 3], j//3, solution))

            for future in futures:
                result = future.result()

                if result is None:
                    continue

                error, thread_num = result
                if thread_num in results.keys():
                    results[thread_num].add(error)
                else:
                    results[thread_num] = set([error])

            print_results(results, process_number, n_threads)

def validate_game_thread_pool_executor(solutions: list[list[list[int]]], solution_number: int, n_threads: int) -> None:
    """ Validates the game using the thread pool executor
    method

    Parameters
    ----------
    solution : list[list[list[int]]]
        The solutions that one process is going to validate
    solution_number : int
        The number of the puzzle that is being validated
    n_threads : int
        The number of threads that the program is using

    Return
    ------
    None

    """
    process_number = multiprocessing.current_process().name[8:]

    for i, solution in enumerate(solutions):
        with ThreadPoolExecutor(max_workers=n_threads, thread_name_prefix="Thread") as pool:
            functions_list = [validate_line, validate_column, validate_region]

            print(f"Processo {process_number}: resolvendo quebra-cabeças {solution_number+i}")
            results = {}

            futures = []
            for j in range(27):
                futures.append(pool.submit(functions_list[j % 3], j//3, solution))

            for future in futures:
                result = future.result()

                if result is None:
                    continue

                error, thread_num = result
                if thread_num in results.keys():
                    results[thread_num].add(error)
                else:
                    results[thread_num] = set([error])

            print_results(results, process_number, n_threads)

def validate_game_sequentially(solutions: list[list[list[int]]], solution_number: int, n_threads: int) -> None:
    """ Validates the game sequentially

    Parameters
    ----------
    solution : list[list[list[int]]]
        The solutions that one process is going to validate
    solution_number : int
        The number of the puzzle that is being validated
    n_threads : int
        The number of threads that the program is using

    Return
    ------
    None

    """
    process_number = multiprocessing.current_process().name[8:]

    for i, solution in enumerate(solutions):
        functions_list = [validate_line, validate_column, validate_region]

        print(f"Processo {process_number}: resolvendo quebra-cabeças {solution_number+i}")

        results = {}
        for j in range(27):
            func = functions_list[j % 3]
            result = func(j//3, solution)

            if result is None:
                continue

            error, thread_num = result
            if thread_num in results.keys():
                results[thread_num].add(error)
            else:
                results[thread_num] = set([error])

        print_results(results, process_number, n_threads)

def handle_results(queue: Queue, n_solutions: int, process_number: int) -> None:
    """
    - Recebe um Future enviado pela `queue` representando o resultado de uma validação de uma solução
    - Uma solução validada é printada qunado o seu número de validações for igual a 27 e
      todos as soluções anteriores a ela já terem sido printadas
    """

    # cada dicionário da lista representa uma solução validada pelo processo
    # As chaves do dicionários são os identificadores das threads que realizaram a validação.
    # O svalores são os erros encontrados pelas threads
    validated_solutions: list[dict[int, list[str] | None]] = [dict() for _ in range(n_solutions)]

    # Lista com contadores indicando a quantidade de validações concluídas de cada resultado.
    # Uma solução validada só é printada qunado o seu número de validações for igual a 27 e todos as soluções anteriores a ela já terem sido printadas
    validation_counters: list[int] = [0 for _ in range(n_solutions)]

    # Indica o número da solução que será printada a seguir
    solution_to_print = 0

    """
        - Enquanto ainda haver soluções a serem printadas, recebe uma validação de alguma solução e atualiza os dados referentes a ela.
        - Caso todas as validações da solução atual tenham sido realizadas, printa os erros encontrados
    """
    while solution_to_print < n_solutions:
        # - A queue inicialmente retorna um future. Quando o resultado de tal future for obtido, ele retornaŕa uma tupla contendo tais elementos:
        #   - número da solução validada
        #   - outra tupla contendo: O erro encontrado durante a validação e athread que encontrou tal erro.
        #     Caso nenhum erro seja encontrado, esse elemento será None

        future: Future = queue.get()

        # Cada resultado retorna uma lista contendo uma tupla que representa uma validação específica.
        # Cada valiadção é composta pelo número da solução a qual ela está relacionada e outra tupla contento
        # o erro encontrado e a thread que o encontrou. Caso nenhum erro tenha sido encontrado, a última tupla será None
        results: list[tuple[int, tuple[str, int] | None]] = future.result()
        for solution_number, error_info in results:
            validation_counters[solution_number] += 1

            # se algum erro foi encontrado
            if error_info is not None:
                error_msg, thread_number = error_info

                if  thread_number in validated_solutions[solution_number].keys():
                    validated_solutions[solution_number][thread_number].append(error_msg)
                else:
                    validated_solutions[solution_number][thread_number] = [error_msg]

            # se a validação da solução atual já pode ser printada
            if validation_counters[solution_to_print] == 27:
                print_results(validated_solutions[solution_number], process_number)
                solution_to_print +=1

def execute_validations(validations_to_execute: list[Callable[[], tuple[int, tuple[str, int]]]]) -> None:
    results = []
    for validation in validations_to_execute:
        results.append(validation())
    return results

def validate_many_games_at_once(solutions: list[list[list[int]]], solution_number: int, n_threads: int) -> None:
    """ Validates the game using the creating threads once
    and thread pool executor method

    Parameters
    ----------
    solution : list[list[list[int]]]
        The solutions that one process is going to validate
    solution_number : int
        The number of the puzzle that is being validated
    n_threads : int
        The number of threads that the program is using

    Return
    ------
    None

    """
    with ThreadPoolExecutor(max_workers=n_threads, thread_name_prefix="Thread") as pool:
        process_number = multiprocessing.current_process().name[8:]
        functions_list = [validate_line, validate_column, validate_region]
        queue = Queue()

        result_handler = Thread(target=handle_results, args=(queue, len(solutions), process_number))
        result_handler.start()

        validation_batch = [] # lista contendo validações que serão realizadas por uma única thread
        for i,solution in enumerate(solutions):
            print(f"Processo {process_number}: resolvendo quebra-cabeças {solution_number+i}")
            for j in range(27):
                def validation(
                        solution_number = i,
                        func = functions_list[j % 3],
                        index = j//3,
                        solution = solution
                ):
                    return solution_number, func(index, solution)

                validation_batch.append(validation)

                if len(validation_batch) == 10 or (i == len(solutions) - 1 and j == 26):
                    future = pool.submit(execute_validations, validation_batch)
                    validation_batch = []

                    queue.put(future)

        result_handler.join()


import pandas as pd
from statistics import mean, stdev

def getStats(df, survivaldict):
	col = 'subtypes'
	list1 = df.loc[df[col] == 0, 'samples']
	list2 = df.loc[df[col] == 1, 'samples']
	list3 = df.loc[df[col] == 2, 'samples']
	list4 = df.loc[df[col] == 3, 'samples']
	list5 = df.loc[df[col] == 4, 'samples']
	vals1 = [float(survivaldict[i]) for i in list1 if (float(survivaldict[i]) > 0)]
	vals2 = [float(survivaldict[i]) for i in list2 if (float(survivaldict[i]) > 0)]
	vals3 = [float(survivaldict[i]) for i in list3 if (float(survivaldict[i]) > 0)]
	vals4 = [float(survivaldict[i]) for i in list4 if (float(survivaldict[i]) > 0)]
	vals5 = [float(survivaldict[i]) for i in list5 if (float(survivaldict[i]) > 0)]
	if vals1:
		mean1 = mean(vals1)
		std1 = stdev(vals1)
	else:
		mean1 = 'NA'
		std1 = ''

	if vals2:
		mean2 = mean(vals2)
		std2 = stdev(vals2)
	else:
		mean2 = 'NA'
		std2 = ''

	if vals3:
		mean3 = mean(vals3)
		std3 = stdev(vals3)
	else:
		mean3 = 'NA'
		std3 = ''
	if vals4:
		mean4 = mean(vals4)
		std4 = stdev(vals4)
	else:
		mean4 = 'NA'
		std4 = ''
	if vals5:
		mean5 = mean(vals5)
		std5 = stdev(vals5)
	else:
		mean5 = 'NA'
		std5 = ''
	print('Survival (months): ')
	print('Yellow: ', mean1, "+/-", std1)
	print('Blue: ', mean2, "+/-", std2)
	print('Green: ', mean3, "+/-", std3)
	print('Cyan: ', mean4, "+/-", std4)
	print('Red: ', mean5, "+/-", std5)

def getsubtypes():
	df = pd.read_csv('novel_results.csv')
	del df['Unnamed: 0']
	return df

def getmeta():
	f = open('GSE7696_series_matrix.txt')
	lines = f.readlines()
	i=0
	for line in lines:
		if "!Sample_geo_accession" in line:
			samples = line.split()
			samples.remove('!Sample_geo_accession')
			samples = [str(i).replace('"', '') for i in samples]
		if '!Sample_characteristics_ch1' in line:
			if 'survival time in months' in line:
				pre = line.split('\t')
				pre.remove('!Sample_characteristics_ch1')
				surv = [str(i).replace('survival time in months: ', '') for i in pre]
				surv = [str(i).replace('NA', '-100') for i in surv]
				surv = [str(i).replace('"', '') for i in surv]
				surv = [str(i).replace('\n', '') for i in surv]
	f.close()

	survivaldict = {samples[i]: surv[i] for i in range(len(samples))}
	return survivaldict

def main():
	df = getsubtypes()
	survivaldict = getmeta()
	getStats(df, survivaldict)

if __name__ == "__main__":
	main()


import numpy as np
import numpy.ma as ma
from netCDF4 import Dataset
import os
import os.path
import scipy.interpolate as interp
import scipy.stats as stats
from config import *

## PART 1: Import data and combine

# Import model files for RCP 2.6 and 8.5, replacing masked data with nans
def importData(dataFiles):
	data = []
	for dataFileName in dataFiles:
		dataFile = dataDir + "/" + dataFileName
		# Because latitudes and logitudes are of different sizes, we must interpolate.
		dataRaw = Dataset(dataFile, "r").variables[varOfInterest][:]
		# Loop through time, interpolating latitude and longitude
		dataInterp = np.zeros((numMonths, latitudeRes, longitudeRes))
		for i in range(numMonths):
			dataInterpGen = interp.interp2d(np.arange(dataRaw.shape[2]), np.arange(dataRaw.shape[1]), dataRaw[i])
			dataInterp[i] = dataInterpGen(np.linspace(0, dataRaw.shape[2], longitudeRes), np.linspace(0, dataRaw.shape[1], latitudeRes))
		data += [dataInterp]
	# Stack, average, and return
	return np.stack(data,axis=0).mean(axis=0)

# Create cache, try to import from cache, and if it isn't possible, import from data instead
if not os.path.exists(cacheDir):
	os.mkdir(cacheDir)
if not os.path.exists(cacheDir + '/rcpData.npz'):
	np.savez_compressed(cacheDir + '/rcpData.npz', rcp26=importData(dataFilesRCP26), rcp85=importData(dataFilesRCP85))
allData = np.load(cacheDir + '/rcpData.npz')
dataRCP26 = allData['rcp26']
dataRCP85 = allData['rcp85']

## PART 2: Process data

# Process data if it wasn't cached
if not os.path.exists(cacheDir + '/processedData.npz'):
	# Remove irrelavent realms using ocean data
	landSea = Dataset(dataDir + "/" + landSeaFile,'r')
	landSeaData = landSea.variables['LSMASK'][:]
	landSeaInterpGen = interp.interp2d(np.arange(landSeaData.shape[1]), np.arange(landSeaData.shape[0]), landSeaData)
	landSeaDataInterp = landSeaInterpGen(np.linspace(0, landSeaData.shape[1], longitudeRes), np.linspace(0, landSeaData.shape[0], latitudeRes))
	# Set mask according to realm specified in config
	if realm == 'land':
		landSeaMask = np.tile(landSeaDataInterp == 0, (numMonths, 1, 1))
	elif realm == 'sea':
		landSeaMask = np.tile(landSeaDataInterp != 0, (numMonths, 1, 1))
	elif realm == 'atmos':
		landSeaMask = False

	# Apply mask
	dataRCP26 = ma.masked_array(dataRCP26, mask=landSeaMask)
	dataRCP85 = ma.masked_array(dataRCP85, mask=landSeaMask)

	# Get difference between the two RCPs and between each RCP and its initial value and divide it to a difference (-1 for -100% and +1 for +100%) for our difference graph.
	def getPercentDifference(data1, data2):
		dataDiff = data1 - data2
		return dataDiff/data2
	# Difference from initial
	diffRCP26 = getPercentDifference(dataRCP26, dataRCP26[0,:])
	diffRCP85 = getPercentDifference(dataRCP85, dataRCP85[0,:])
	diffRCPs = getPercentDifference(dataRCP85, dataRCP26)

	# Change data to rankings, replacing oceans with -inf, then mask oceans (again; stats.rankdata doesn't like masks.) They'll come as the lowest value.
	percRCP26 = stats.rankdata(ma.filled(dataRCP26, -np.inf), method='dense').reshape(dataRCP26.shape)
	percRCP85 = stats.rankdata(ma.filled(dataRCP85, -np.inf), method='dense').reshape(dataRCP85.shape)
	percRCP26 = ma.masked_array(percRCP26, mask=(percRCP26 == 1))
	percRCP85 = ma.masked_array(percRCP85, mask=(percRCP85 == 1))

	# Normalize data to between 0 and 1 and reverse it to make them into precentiles
	minVal = min(percRCP26.min(), percRCP85.min())
	maxVal = max(percRCP26.max(), percRCP85.max())
	rangeVal = maxVal-minVal
	percRCP26 = (percRCP26 - minVal)/rangeVal
	percRCP85 = (percRCP85 - minVal)/rangeVal

	# Cache our processed data
	if realm == 'atmos':
		np.savez_compressed(cacheDir + '/processedData.npz',
			perc26=percRCP26, perc26_mask=percRCP26.mask,
			perc85=percRCP85, perc85_mask=percRCP85.mask,
			diff26=diffRCP26, diff26_mask=diffRCP26.mask,
			diff85=diffRCP85, diff85_mask=diffRCP85.mask,
			diffRCP=diffRCPs, diffRCP_mask=diffRCPs.mask,
			landSea=landSeaDataInterp)
	else: # Realm is land or sea
		np.savez_compressed(cacheDir + '/processedData.npz',
			perc26=percRCP26, perc26_mask=percRCP26.mask,
			perc85=percRCP85, perc85_mask=percRCP85.mask,
			diff26=diffRCP26, diff26_mask=diffRCP26.mask,
			diff85=diffRCP85, diff85_mask=diffRCP85.mask,
			diffRCP=diffRCPs, diffRCP_mask=diffRCPs.mask)

print("Done. Run plot.py to show.")


import traceback
from stock import Stock
from repository import Repository
from strategy_factory import StrategyFactory

class Result:
    total_cnt = 0
    buy_miss_cnt = 0
    sell_miss_cnt = 0
    total_profit_rate = 0

def print_green(s):
    print(f"\033[0;32;40m%s\033[0m" % (s))

def print_red(s):
    print(f"\033[0;31;40m%s\033[0m" % (s))

def print_result(stk, profit_rate, buy_miss = False):
    if buy_miss:
        info = "{0:{4}<10}\t{1:<10}\t{2:<10}\t{3:{4}<10}\t{5}\tMISS".format(
            stk.name, stk.code, str(stk.close), stk.industry, chr(12288), ','.join(stk.features))
        print(info)
    else:
        info = "{0:{4}<10}\t{1:<10}\t{2:<10}\t{3:{4}<10}\t{5}\t{6}".format(
            stk.name, stk.code, str(stk.close), stk.industry, chr(12288), ','.join(stk.features), "%.2f" % profit_rate)
        if profit_rate > 0:
            print_red(info)
        else:
            print_green(info)

def show_result_of_buy_open_and_sell_trend_break(stk, dates, i):
    Result.total_cnt += 1
    prices = repo.get_all_prices_after(stk.code, dates[i])

    # BUY DAY
    buy_price = prices['open'].values[0]
    highest_price = prices['high'].values[0]
    day = 1

    while True:
        low_price = prices['low'].values[day]
        high_price = prices['high'].values[day]
        # print(day, low_price, high_price, highest_price)
        if (low_price-highest_price)/highest_price < -0.06:
            sell_price = prices['close'].values[day]
            profit_rate = (sell_price-buy_price)/buy_price * 100
            Result.total_profit_rate += profit_rate
            print_result(stk, profit_rate)
            break
        else:
            highest_price = high_price if high_price > highest_price else highest_price
        day += 1

def show_result_of_buy_close_and_sell_103_in_3_days(stk, dates, i):
    Result.total_cnt += 1
    prices = repo.get_all_prices_after(stk.code, dates[i])

    # BUY DAY
    buy_price = stk.close

    # SELL DAY 1
    open_profit_rate = (prices['open'].values[0]-buy_price)/buy_price * 100
    if (open_profit_rate >= 3):
        Result.total_profit_rate += open_profit_rate
        # print_red("SELL at open price %.2f, profit: %.2f" % (prices['open'].values[0], open_profit_rate))
        return print_result(stk, open_profit_rate)
    else:
        sell_price = buy_price * 1.030
        if (prices['high'].values[0] > sell_price):
            profit_rate = 3.0
            Result.total_profit_rate += profit_rate
            # print_red("SELL at 1.030 price %.2f, profit: %.2f" % (sell_price, profit_rate))
            return print_result(stk, profit_rate)
        else:
            # print("SELL MISS")
            pass

    # SELL DAY 2
    open_profit_rate = (prices['open'].values[1]-buy_price)/buy_price * 100
    if (open_profit_rate >= 3):
        Result.total_profit_rate += open_profit_rate
        # print_red("SELL at open price %.2f, profit: %.2f" % (prices['open'].values[1], open_profit_rate))
        return print_result(stk, open_profit_rate)
    else:
        sell_price = buy_price * 1.030
        if (prices['high'].values[1] > sell_price):
            profit_rate = 3.0
            Result.total_profit_rate += profit_rate
            # print_red("SELL at 1.030 price %.2f, profit: %.2f" % (sell_price, profit_rate))
            return print_result(stk, profit_rate)
        else:
            # print("SELL MISS")
            pass

    # SELL DAY 3
    open_profit_rate = (prices['open'].values[2]-buy_price)/buy_price * 100
    if (open_profit_rate >= 3):
        Result.total_profit_rate += open_profit_rate
        # print_red("SELL at open price %.2f, profit: %.2f" % (prices['open'].values[2], open_profit_rate))
        return print_result(stk, open_profit_rate)
    else:
        sell_price = buy_price * 1.030
        if (prices['high'].values[2] > sell_price):
            profit_rate = 3.0
            Result.total_profit_rate += profit_rate
            # print_red("SELL at 1.030 price %.2f, profit: %.2f" % (sell_price, profit_rate))
            return print_result(stk, profit_rate)
        else:
            profit_rate = (prices['close'].values[2]-buy_price)/buy_price * 100
            Result.total_profit_rate += profit_rate
            Result.sell_miss_cnt += 1
            # print_green("SELL MISS, close price: %.2f, profit: %.2f" % (prices['close'].values[2], profit_rate))
            print_result(stk, profit_rate)

def show_result_of_buy_0985_and_sell_1015_in_3_days(stk, dates, i):
    Result.total_cnt += 1
    prices = repo.get_all_prices_after(stk.code, dates[i])

    # BUY DAY
    # print("%s: %.2f\t%.2f\t%.2f\t%.2f" % (
    #     dates[i+1], prices['open'].values[0], prices['close'].values[0], prices['low'].values[0], prices['high'].values[0]
    # ), end=" ")
    buy_price = prices['open'].values[0] * 0.985
    if (prices['low'].values[0] < buy_price):
        # print("BUY at 0.985 price %.2f" % (buy_price))
        pass
    else:
        # print("BUY MISS")
        Result.buy_miss_cnt += 1
        return print_result(stk, 0, True)

    # SELL DAY 1
    # print("%s: %.2f\t%.2f\t%.2f\t%.2f" % (
    #     dates[i+2], prices['open'].values[1], prices['close'].values[1], prices['low'].values[1], prices['high'].values[1]
    # ), end=" ")
    open_profit_rate = (prices['open'].values[1]-buy_price)/buy_price * 100
    if (open_profit_rate >= 3):
        Result.total_profit_rate += open_profit_rate
        # print_red("SELL at open price %.2f, profit: %.2f" % (prices['open'].values[1], open_profit_rate))
        return print_result(stk, open_profit_rate)
    else:
        sell_price = buy_price * 1.030
        if (prices['high'].values[1] > sell_price):
            profit_rate = 3.0
            Result.total_profit_rate += profit_rate
            # print_red("SELL at 1.030 price %.2f, profit: %.2f" % (sell_price, profit_rate))
            return print_result(stk, profit_rate)
        else:
            # print("SELL MISS")
            pass

    cost_price = buy_price
    dip_price = buy_price * 0.940
    if (prices['close'].values[1] < dip_price):
        cost_price = (buy_price + dip_price)/2
        # print("BUY A DIP at 0.940 price %.2f, COST %.2f" % (dip_price, cost_price))
    else:
        # print("NO BUY A DIP, COST %.2f" % (cost_price))
        pass

    # SELL DAY 2
    # print("%s: %.2f\t%.2f\t%.2f\t%.2f" % (
    #     dates[i+3], prices['open'].values[2], prices['close'].values[2], prices['low'].values[2], prices['high'].values[2]
    # ), end=" ")
    open_profit_rate = (prices['open'].values[2]-cost_price)/cost_price * 100
    if (open_profit_rate >= 2):
        Result.total_profit_rate += open_profit_rate
        # print_red("SELL at open price %.2f, profit: %.2f" % (prices['open'].values[2], open_profit_rate))
        return print_result(stk, open_profit_rate)
    else:
        sell_price = cost_price * 1.020
        if (prices['high'].values[2] > sell_price):
            profit_rate = 2.0
            Result.total_profit_rate += profit_rate
            # print_red("SELL at 1.020 price %.2f, profit: %.2f" % (sell_price, profit_rate))
            return print_result(stk, profit_rate)
        else:
            # print("SELL MISS")
            pass

    # SELL DAY 3
    # print("%s: %.2f\t%.2f\t%.2f\t%.2f" % (
    #     dates[i+4], prices['open'].values[3], prices['close'].values[3], prices['low'].values[3], prices['high'].values[3]
    # ), end=" ")
    open_profit_rate = (prices['open'].values[3]-cost_price)/cost_price * 100
    if (open_profit_rate >= 1):
        Result.total_profit_rate += open_profit_rate
        # print_red("SELL at open price %.2f, profit: %.2f" % (prices['open'].values[3], open_profit_rate))
        return print_result(stk, open_profit_rate)
    else:
        sell_price = cost_price * 1.010
        if (prices['high'].values[3] > sell_price):
            profit_rate = 1.0
            Result.total_profit_rate += profit_rate
            # print_red("SELL at 1.010 price %.2f, profit: %.2f" % (sell_price, profit_rate))
            return print_result(stk, profit_rate)
        else:
            profit_rate = (prices['close'].values[3]-cost_price)/cost_price * 100
            Result.total_profit_rate += profit_rate
            Result.sell_miss_cnt += 1
            # print_green("SELL MISS, close price: %.2f, profit: %.2f" % (prices['close'].values[3], profit_rate))
            print_result(stk, profit_rate)

def select_stocks(results):
    selected = []
    for s in results:
        for stk in results[s]:
            selected.append(stk)
    return selected

if __name__ == '__main__':

    repo = Repository()
    dates = repo.get_all_trade_dates_between('000001.SZ', '20200101', '20210101')
    # print(dates)
    for i in range(0, len(dates)):
        date = dates[i]
        print("=== " + date + " ===")

        # if date != '20210111':
        #     continue

        Result.total_cnt = 0
        Result.buy_miss_cnt = 0
        Result.sell_miss_cnt = 0
        Result.total_profit_rate = 0

        sf = StrategyFactory(repo)
        results = sf.do_strategy(date)
        selected = select_stocks(results)
        for stk in selected:
            try:
                # show_result_of_buy_0985_and_sell_1015_in_3_days(stk, dates, i)
                # show_result_of_buy_close_and_sell_103_in_3_days(stk, dates, i)
                show_result_of_buy_open_and_sell_trend_break(stk, dates, i)
            except:
                traceback.print_exc()
                pass

        print("-----------------------------")
        print("Total count: %d" % (Result.total_cnt))
        print("Buy miss count: %d" % (Result.buy_miss_cnt))
        print("Sell miss count: %d" % (Result.sell_miss_cnt))
        print("Total profit rate: %.2f" % (Result.total_profit_rate))


#!/bin/python3.4

import sys
import re
import pdb

def getPronouns(t):
	pronouns = []

	for st in t.subtrees():
		if st.label().unicode_repr().split('[')[0] in ['PRP', 'PossPro'] and len(st.leaves()) == 1:
			pronouns.append(st)

	return pronouns

def getImmediateNominal(t):
	for st in t.subtrees():
		if st.label().unicode_repr().split('[')[0] in ['Nom', 'PRP']:
			return st

def sanitize(raw):
	return re.sub(' +', ' ', str(raw).replace('\n', ''))

def printInputs(d, p):
	sys.stdout.write(p.leaves()[0]+'\t')
	for c in d:
		sys.stdout.write(sanitize(c)+' ')
	sys.stdout.write('\n')

def nextX(c, path=[]):

	while True:
		path.append(c)
		c = c.parent()
		if c is None:
			sys.stdout.write('Unable to find next X.\n')
		if c.label().unicode_repr().split('[')[0] in ['NP', 'S']:
			break

	sys.stdout.write(sanitize(c)+'\n')
	return c, path

def checkCandidate(a, p):
	sys.stdout.write(sanitize(a)+'\n')
	sys.stdout.write('Reject - TBD\n')
	return False

def lrbfTraverse(t, p, path=[], targetLeftOfPath=True, separatedByNPorS=True):

	"""
	@desc	- Traversal manager -- conducts left-ot-right, breadth-first
		  traversals with respect to tree 't'. Includes various options
	          to accomodate step-specific requirements.
	@param	- t: root node defining the tree to be traversed
	@param	- p: target pronoun, the features of which are needed to qualify the
		  antecedent
	@param	- targetLeftOfPath: 'True' if targeting portion of tree below 't' left of
		  path *or* if no path is applicable. 'False' if targeting right-hand
		  portion of the tree.
	@param 	- path: unordered list of node references through which the has cursor
		  passed while on its way to X. (leave as empty if no path applies)
	@param	- separatedByNPorS: implements the proxy binding contraint from step 3.
		  Leave unspecified if the constraint is not applicable.
	@return	- found: 'True' if resolution was successful; else 'False'
	"""

	rightOfPath = False
	tapped = [t]
	queue = [(t, separatedByNPorS)]
	qualifiedNodes = []

	while len(queue) > 0:
		current = queue.pop(0)
		for node in current[0]:

			if node in path:
				if targetLeftOfPath:
					break						#The path has been hit while exploring the lhs - explore this parent no further
				else:
					rightOfPath = True       			#The path has been hit while exploring the rhs - turn on the listener
					continue					#Provided the tree is proper, the listener can remain on after the inital switch
				if not targetLeftOfPath and not rightOfPath: 		#The listener is off while exploring the rhs - try the next one
					continue
			if type(node) is str:						#Reached a terminal; nothing to check
				continue

			if node.label().unicode_repr().split('[')[0] == 'NP':					#Potential candidate
				if current[1]:						#Step-3 check (Effectively skipped by default)
					found = checkCandidate(node, p)
					if found:
						return True

			if node not in tapped:
				tapped.append(node)
				sepStat = current[1] or node.label().unicode_repr().split('[')[0] in ['NP', 'S']
				queue.append((node, sepStat))
	return False


import torch.nn.functional as F
import pruning.function.prune as prune

class PruneLeNet5(prune.PruneModule):
    def __init__(self):
        super(PruneLeNet5, self).__init__()
        self.conv1 = prune.MaskConv2Module(1, 20, 5, 1) # in_channels/out_channels/kernel_size/stride
        self.conv2 = prune.MaskConv2Module(20, 50, 5, 1)
        self.fc1 = prune.MaskLinearModule(4 * 4 * 50, 500)
        self.fc2 = prune.MaskLinearModule(500, 10)

    def forward(self, x):
        x = F.relu(self.conv1(x))
        x = F.max_pool2d(x, 2, 2)
        x = F.relu(self.conv2(x))
        x = F.max_pool2d(x, 2, 2)
        x = x.view(-1, 4 * 4 * 50)
        x = F.relu(self.fc1(x))
        x = self.fc2(x)
        return x

    # 返回输入值x的第零维外维度乘积。例如x[m,n,q,w]，返回n*q*w
    def num_flat_features(self, x):
        size = x.size()[1:]
        num_features = 1
        for s in size:
            num_features *= s
        return num_features


"""
    this module define an affine cipher algorithm or encrypting message data
"""

class AffineCipher:
    """
        this class provide an interface encrypt and decrypt for ciphering a message
    """

    def __init__(self) -> None:
        self.__letters = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W'
                          'X','Y','Z']

    def __is_a_and_b_keys_are_coprime(self,a_key: int, b_key : int) -> bool:
        """
            check if a and be keys of affine cipher are coprime
        """

        # check if a_key a other divisor expect himself and one
        a_key_number_list = self.__find_divisor_for_number(a_key)
        b_key_number_list = self.__find_divisor_for_number(b_key)

        if len(a_key_number_list) > 0 or len(b_key_number_list) > 0:
            return False
        else:
            return True

    def __find_divisor_for_number(self, random_number):
        """
             this function find all divisor except 1 and the number itself
        """
        return  [i for i in range (2,(int(random_number/2))+1) if random_number % i == 0]

    def encrypt_message(self, a_key:int, b_key:int, message_text: str) -> str:
        """
            encrypt message text by using a and b key
        """
        if self.__is_a_and_b_keys_are_coprime(a_key, b_key):
            encrypted_message = ""

            for letter in message_text.upper():
                encrypted_message += self.__encrypt_message_letter(a_key,b_key,letter)

            return encrypted_message
        else:
            return ""

    def __encrypt_message_letter(self, a_key: int, b_key: int, message_letter: str):
        """
            this class is responsible to find the encrypted letter for each letter of the
            clear message text
        """
        enrypted_letter_index = (a_key * self.__letters.index(message_letter) + b_key ) % 26
        return self.__letters[enrypted_letter_index]

    def __decrypt_message_letter(self,a_key: int, b_key: int, message_letter: str) ->str:
        """
            this function take a encrypted letter with afficne cipher and decrypt it
        """
        decrypt_letter_index = 23 * (message_letter - b_key) % 26
        return self.__letters[decrypt_letter_index]

    def decrypt_message(self, a_key: int, b_key: int , encryted_text: str):
        """
            decrypte a message text by using a and b key
        """

        if self.__is_a_and_b_keys_are_coprime(a_key, b_key):
            orginal_message = ""

            for encrypted_letter in encryted_text:
                orginal_message += self.__decrypt_message_letter(a_key, b_key, encrypted_letter)

            return orginal_message
        else:
            return ""


import math
from typing import List, Set, Optional

import pygame.draw
from pygame import Surface

from citygame.src.constants.location_state_enum import LocationState
from citygame.src.constants.world_constants import DISTANCE_BETWEEN_LOCATIONS
from citygame.src.state.location_actor import Location
from citygame.src.util.locations import calculate_locations, calculate_regions, calculate_borders
from citygame.src.util.map_tile import MapTile
from citygame.src.util.maps import generate_map
from citygame.src.util.progress_bar import ProgressBar

NEIGHBOR_LINE_COLOR = [25, 25, 25]

class WorldState:
    """
    Representation of a world.
    """

    def __init__(self, progress_bar: ProgressBar, map_size):
        self.map_size = map_size

        self._generate_world(progress_bar, map_size)

        self.hover_location: Optional[Location] = None

        self.surface_offset_x = 0
        self.surface_offset_y = 0

        self.locations_surface = Surface((map_size, map_size), pygame.SRCALPHA, 32)
        self.locations_surface = self.locations_surface.convert_alpha()
        self.location_roads_surface = Surface((map_size, map_size), pygame.SRCALPHA, 32)
        self.location_roads_surface = self.location_roads_surface.convert_alpha()
        self.locations_to_draw: Set[Location] = set()

        self.starting_location.set_as_starting_location()
        self.location_conquered(self.starting_location)

    def __getstate__(self):
        # Copy the object's state from self.__dict__ which contains
        # all our instance attributes. Always use the dict.copy()
        # method to avoid modifying the original state.
        state = self.__dict__.copy()

        # Remove the unpicklable entries.
        keys_to_remove = set()
        for key, value in state.items():
            # Remove pygame surfaces
            if type(value) == pygame.Surface:
                keys_to_remove.add(key)

        for key in keys_to_remove:
            del state[key]

        return state

    def location_regress(self, location: Location):
        self.locations_to_draw.add(location)
        location.set_location_state(LocationState.DISCOVERED)
        self._redraw_locations()

    def location_explored(self, location: Location):
        self.locations_to_draw.add(location)
        location.set_location_state(LocationState.EXPLORED)

        self._redraw_locations()

    def location_conquered(self, location: Location):
        self.locations_to_draw.add(location)
        location.set_location_state(LocationState.CONQUERED)

        # Now that we have conquered the location we discover the neighbors
        for neighbor in location.neighbors:
            # Only discover the location if it is hidden
            if neighbor.location_state == LocationState.HIDDEN:
                neighbor.set_location_state(LocationState.DISCOVERED)

            self.locations_to_draw.add(neighbor)

        self._redraw_locations()
        # New roads are potentially available so redraw the roads
        self._redraw_location_roads()

    def _redraw_locations(self):
        self.locations_surface = Surface((self.map_size, self.map_size), pygame.SRCALPHA, 32)
        self.locations_surface = self.locations_surface.convert_alpha()

        for location in self.locations_to_draw:
            location.render(self.locations_surface, hover=False)

    def _redraw_location_roads(self):
        self.location_roads_surface = Surface((self.map_size, self.map_size), pygame.SRCALPHA, 32)
        self.location_roads_surface = self.location_roads_surface.convert_alpha()
        for location in self.locations_to_draw:
            # Only conquered locations should draw roads to additional locations
            if location.location_state != LocationState.CONQUERED:
                continue

            for neighbor in location.neighbors:
                pygame.draw.line(
                    self.location_roads_surface, NEIGHBOR_LINE_COLOR, [location.x, location.y], [neighbor.x, neighbor.y]
                )

    def get_locations(self) -> List[Location]:
        return self.locations

    def render_geography(self, surface: Surface):
        if self.map_size < surface.get_width() or self.map_size < surface.get_height():
            surface.fill(MapTile.get_rgb_value(MapTile.DEEP_WATER.value))

        self.surface_offset_x = abs(surface.get_width() - self.map_size) // 2
        self.surface_offset_y = abs(surface.get_height() - self.map_size) // 2

        surface.blit(self.map_surface, (self.surface_offset_x, self.surface_offset_y))

    def render_roads(self, surface: Surface):
        # Draw the neighboring location lines first so the location bubbles will be drawn over them
        surface.blit(self.location_roads_surface, (self.surface_offset_x, self.surface_offset_y))

    def render_locations(self, surface: Surface):
        # Draw the locations surface
        surface.blit(self.locations_surface, (self.surface_offset_x, self.surface_offset_y))

    def _generate_world(self, progress_bar: ProgressBar, map_size):
        progress_bar.set_progress(0.0, "Generating tiles...")
        self.map_tiles = generate_map(map_size, map_size)

        progress_bar.set_progress(0.3, "Generating locations...")
        location_points = calculate_locations(self.map_tiles)

        progress_bar.set_progress(0.6, "Calculating regions...")
        self.region_matrix = calculate_regions(location_points, self.map_tiles)
        self.location_to_border_points = calculate_borders(location_points, self.region_matrix)

        progress_bar.set_progress(0.8, "Calculating location graph...")
        # Create the location objects
        self.locations: List[Location] = []
        for i in range(len(location_points)):
            location_coordinates = location_points[i]
            self.locations.append(Location(i, location_coordinates[0], location_coordinates[1], self))

        # Calculate neighbors for each location
        for i in range(len(self.locations)):
            current_neighbors = []
            current_location = self.locations[i]
            # TODO Generate real names
            current_location.set_name(f"Location {i}")

            x1 = current_location.x
            y1 = current_location.y

            for j in range(len(self.locations)):
                # Do not calculate a point as a neighbor of itself
                if i == j:
                    continue

                other_location = self.locations[j]
                x2 = other_location.x
                y2 = other_location.y
                distance = math.sqrt(math.pow(x2 - x1, 2) + math.pow(y2 - y1, 2))
                if distance < (DISTANCE_BETWEEN_LOCATIONS + 1):
                    current_neighbors.append(other_location)

            # Now set the neighbors
            current_location.set_neighbors(current_neighbors)

        self.starting_location = self.locations[0]

        # Calculate location levels
        self._calculate_level(self.starting_location)

        progress_bar.set_progress(0.8, "Saving map image...")
        # Create a map surface so that we can simply draw the surface each frame instead of each tile
        self.map_surface = Surface((map_size, map_size))
        for x in range(self.map_size):
            for y in range(self.map_size):
                tile_color = MapTile.get_rgb_value(self.map_tiles[x][y])
                pygame.draw.line(self.map_surface, tile_color, [x, y], [x, y])

        progress_bar.set_progress(1.0, "Done!")

    @staticmethod
    def _calculate_level(starting_location: Location):
        starting_location.set_level(1)

        locations_visited = {starting_location}
        most_recently_visited = {starting_location}
        distance = 0
        while True:
            newly_visited = set()
            for location in most_recently_visited:
                location.set_level(distance)
                for neighbor in location.neighbors:
                    # Do not re-visit points
                    if neighbor in locations_visited:
                        continue

                    locations_visited.add(neighbor)
                    newly_visited.add(neighbor)

            distance += 1
            most_recently_visited = newly_visited
            if len(most_recently_visited) == 0:
                return


import torch
from diffusers import DiffusionPipeline

class DiffusionInferencePipeline(DiffusionPipeline):
    def __init__(self, network, scheduler, num_inference_timesteps=1000):
        super().__init__()

        self.register_modules(network=network, scheduler=scheduler)
        self.num_inference_timesteps = num_inference_timesteps

    @torch.inference_mode()
    def __call__(
        self,
        initial_noise: torch.Tensor,
        conditioner: torch.Tensor = None,
    ):
        r"""
        Args:
            initial_noise: The initial noise to be denoised.
            conditioner:The conditioner.
            n_inference_steps: The number of denoising steps. More denoising steps
                usually lead to a higher quality at the expense of slower inference.
        """

        mel = initial_noise
        batch_size = mel.size(0)
        self.scheduler.set_timesteps(self.num_inference_timesteps)

        for t in self.progress_bar(self.scheduler.timesteps):
            timestep = torch.full((batch_size,), t, device=mel.device, dtype=torch.long)

            # 1. predict noise model_output
            model_output = self.network(mel, timestep, conditioner)

            # 2. denoise, compute previous step: x_t -> x_t-1
            mel = self.scheduler.step(model_output, t, mel).prev_sample

            # 3. clamp
            mel = mel.clamp(-1.0, 1.0)

        return mel


import flask; from flask import request
import itertools
import os
import subprocess
import time

from voussoirkit import flasktools
from voussoirkit import pathclass
from voussoirkit import stringtools

import ycdl

from .. import common

site = common.site

def _get_or_insert_video(video_id):
    try:
        video = common.ycdldb.get_video(video_id)
    except ycdl.exceptions.NoSuchVideo:
        with common.ycdldb.transaction:
            video = common.ycdldb.insert_video(video_id)['video']
    return video

@site.route('/all_channels.json')
def get_all_channel_names():
    all_channels = {channel.id: channel.name for channel in common.ycdldb.get_channels()}
    response = {'channels': all_channels}
    return flasktools.json_response(response)

@site.route('/channels')
def get_channels():
    channels = common.ycdldb.get_channels()
    return common.render_template(request, 'channels.html', channels=channels)

def _render_videos_listing(videos, channel, state, orderby):
    search_terms = request.args.get('q', '').lower().strip().replace('+', ' ').split()
    if search_terms:
        lowered = ((video, video.title.lower()) for video in videos)
        videos = (
            video for (video, title) in lowered
            if all(term in title for term in search_terms)
        )

    limit = request.args.get('limit', None)
    if limit is not None:
        try:
            limit = int(limit)
            videos = itertools.islice(videos, limit)
        except ValueError:
            pass

    if not isinstance(videos, list):
        videos = list(videos)

    all_states = common.ycdldb.get_all_states()

    return common.render_template(
        request,
        'channel.html',
        all_states=all_states,
        channel=channel,
        state=state,
        orderby=orderby,
        videos=videos,
    )

@site.route('/channel/<channel_id>')
@site.route('/channel/<channel_id>/<state>')
def get_channel(channel_id, state=None):
    try:
        channel = common.ycdldb.get_channel(channel_id)
    except ycdl.exceptions.NoSuchChannel:
        try:
            with common.ycdldb.transaction:
                channel = common.ycdldb.add_channel(channel_id)
        except ycdl.ytapi.ChannelNotFound:
            flask.abort(404)

    orderby = request.args.get('orderby', None)

    videos = common.ycdldb.get_videos(
        channel_id=channel.id,
        orderby=orderby,
        state=state,
    )
    return _render_videos_listing(videos, channel=channel, state=state, orderby=orderby)

@site.route('/videos')
@site.route('/videos/<state>')
def get_videos(state=None):
    orderby = request.args.get('orderby', None)

    videos = common.ycdldb.get_videos(
        orderby=orderby,
        state=state,
    )
    return _render_videos_listing(videos, channel=None, state=state, orderby=orderby)

@site.route('/watch')
def get_watch():
    video_id = request.args.get('v', '')
    if not video_id:
        return flask.redirect('/')

    try:
        video = _get_or_insert_video(video_id)
    except ycdl.ytapi.VideoNotFound:
        flask.abort(404)

    videos = [video]
    return _render_videos_listing(videos, channel=None, state=None, orderby=None)

@flasktools.required_fields(['channel_id'], forbid_whitespace=True)
@site.route('/add_channel', methods=['POST'])
def post_add_channel():
    channel_id = request.form['channel_id']
    if not (len(channel_id) == 24 and channel_id.startswith('UC')):
        # It seems they have given us a username instead.
        try:
            channel_id = common.ycdldb.youtube.get_user_id(username=channel_id)
        except ycdl.ytapi.ChannelNotFound:
            return flasktools.json_response({}, status=404)

    with common.ycdldb.transaction:
        channel = common.ycdldb.add_channel(channel_id, get_videos=True)
    return flasktools.json_response(channel.jsonify())

@site.route('/channel/<channel_id>/delete', methods=['POST'])
def post_delete_channel(channel_id):
    try:
        channel = common.ycdldb.get_channel(channel_id)
    except ycdl.exceptions.NoSuchChannel as exc:
        return flasktools.json_response(exc.jsonify(), status=404)

    with common.ycdldb.transaction:
        channel.delete()
    response = {'id': channel.id, 'deleted': channel.deleted}
    return flasktools.json_response(response)

@site.route('/channel/<channel_id>/refresh', methods=['POST'])
def post_refresh_channel(channel_id):
    force = request.form.get('force', False)
    force = stringtools.truthystring(force, False)
    try:
        channel = common.ycdldb.get_channel(channel_id)
    except ycdl.exceptions.NoSuchChannel as exc:
        return flasktools.json_response(exc.jsonify(), status=404)

    with common.ycdldb.transaction:
        channel.refresh(force=force)
    return flasktools.json_response(channel.jsonify())

@site.route('/refresh_all_channels', methods=['POST'])
def post_refresh_all_channels():
    force = request.form.get('force', False)
    force = stringtools.truthystring(force, False)
    with common.ycdldb.transaction:
        common.ycdldb.refresh_all_channels(force=force, skip_failures=True)
    common.last_refresh = time.time()
    return flasktools.json_response({})

@flasktools.required_fields(['state'], forbid_whitespace=True)
@site.route('/channel/<channel_id>/set_automark', methods=['POST'])
def post_set_automark(channel_id):
    state = request.form['state']
    channel = common.ycdldb.get_channel(channel_id)

    try:
        with common.ycdldb.transaction:
            channel.set_automark(state)
    except ycdl.exceptions.InvalidVideoState as exc:
        return flasktools.json_response(exc.jsonify(), status=400)

    response = {'id': channel.id, 'automark': channel.automark}
    return flasktools.json_response(response)

@flasktools.required_fields(['autorefresh'], forbid_whitespace=True)
@site.route('/channel/<channel_id>/set_autorefresh', methods=['POST'])
def post_set_autorefresh(channel_id):
    autorefresh = request.form['autorefresh']
    channel = common.ycdldb.get_channel(channel_id)

    try:
        autorefresh = stringtools.truthystring(autorefresh)
        with common.ycdldb.transaction:
            channel.set_autorefresh(autorefresh)
    except (ValueError, TypeError):
        flask.abort(400)

    response = {'id': channel.id, 'autorefresh': channel.autorefresh}
    return flasktools.json_response(response)

@flasktools.required_fields(['download_directory'], forbid_whitespace=False)
@site.route('/channel/<channel_id>/set_download_directory', methods=['POST'])
def post_set_download_directory(channel_id):
    download_directory = request.form['download_directory']
    channel = common.ycdldb.get_channel(channel_id)

    try:
        with common.ycdldb.transaction:
            channel.set_download_directory(download_directory)
    except pathclass.NotDirectory:
        exc = {
            'error_type': 'NOT_DIRECTORY',
            'error_message': f'"{download_directory}" is not a directory.',
        }
        return flasktools.json_response(exc, status=400)

    abspath = channel.download_directory.absolute_path if channel.download_directory else None
    response = {'id': channel.id, 'download_directory': abspath}
    return flasktools.json_response(response)

@flasktools.required_fields(['ignore_shorts'], forbid_whitespace=True)
@site.route('/channel/<channel_id>/set_ignore_shorts', methods=['POST'])
def post_set_ignore_shorts(channel_id):
    ignore_shorts = request.form['ignore_shorts']
    channel = common.ycdldb.get_channel(channel_id)

    try:
        ignore_shorts = stringtools.truthystring(ignore_shorts)
        with common.ycdldb.transaction:
            channel.set_ignore_shorts(ignore_shorts)
    except (ValueError, TypeError):
        flask.abort(400)

    response = {'id': channel.id, 'ignore_shorts': channel.ignore_shorts}
    return flasktools.json_response(response)

@flasktools.required_fields(['name'], forbid_whitespace=False)
@site.route('/channel/<channel_id>/set_name', methods=['POST'])
def post_set_name(channel_id):
    name = request.form['name']
    channel = common.ycdldb.get_channel(channel_id)

    with common.ycdldb.transaction:
        channel.set_name(name)

    response = {'id': channel.id, 'name': channel.name}
    return flasktools.json_response(response)

@flasktools.required_fields(['extension'], forbid_whitespace=False)
@site.route('/channel/<channel_id>/set_queuefile_extension', methods=['POST'])
def post_set_queuefile_extension(channel_id):
    extension = request.form['extension']
    channel = common.ycdldb.get_channel(channel_id)

    with common.ycdldb.transaction:
        channel.set_queuefile_extension(extension)

    response = {'id': channel.id, 'queuefile_extension': channel.queuefile_extension}
    return flasktools.json_response(response)

@site.route('/channel/<channel_id>/show_download_directory', methods=['POST'])
def post_show_download_directory(channel_id):
    if not request.is_localhost:
        flask.abort(403)

    channel = common.ycdldb.get_channel(channel_id)
    if channel.download_directory:
        abspath = channel.download_directory.absolute_path
    else:
        abspath = common.ycdldb.config['download_directory']

    if not os.path.exists(abspath):
        return flask.abort(400)

    if os.name == 'nt':
        command = f'explorer.exe "{abspath}"'
        subprocess.Popen(command, shell=True)
        return flasktools.json_response({})
    else:
        command = ['xdg-open', abspath]
        subprocess.Popen(command, shell=True)
        return flasktools.json_response({})

    flask.abort(501)


import os
import sys
import pandas as pd
import numpy as np
from src.logging.logging import logging
from src.exception.exception import CustomException
from src.utils.utils import save_object
from sklearn.pipeline import Pipeline
from sklearn.impute import SimpleImputer
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OrdinalEncoder, LabelEncoder
from dataclasses import dataclass

@dataclass
class DataTransformationConfig:
    preprocessor_pickle_path = os.path.join("artifacts", "preprocessor.pkl")

class DataTransformation:
    def __init__(self):
        self.data_transformation_config = DataTransformationConfig()

    def get_data_transformation_object(self):
        try:
            cat_cols = ['Gender', 'family_history_with_overweight', 'FAVC', 'CAEC', 'SMOKE', 'SCC',
                        'CALC', 'MTRANS']
            num_cols = ['Age', 'Height', 'Weight', 'FCVC', 'NCP', 'CH2O', 'FAF', 'TUE']
            target_col = ['NObeyesdad']

            target_categories = ['Obesity_Type_I', 'Obesity_Type_III', 'Obesity_Type_II',
                                 'Overweight_Level_I', 'Overweight_Level_II', 'Normal_Weight',
                                 'Insufficient_Weight']

            # create pipeline to impute missing data and encode the columns to numerical
            cat_pipeline = Pipeline(steps=[
                ("imputer", SimpleImputer(strategy="most_frequent")),
                ("encoder", OrdinalEncoder())
            ])

            num_pipeline = Pipeline(steps=[
                ("imputer", SimpleImputer(strategy="median")),
            ])

            target_pipeline = Pipeline(steps=[
                ("encode", OrdinalEncoder(categories=[target_categories]))
            ])

            preprocessor = ColumnTransformer([
                ("cat", cat_pipeline, cat_cols),
                ("num", num_pipeline, num_cols)
            ])

            target_preprocessor = ColumnTransformer([
                ("target", target_pipeline, target_col)
            ])

            logging.info("Preprocessor file has been successfully created")
            return preprocessor, target_preprocessor

        except Exception as err:
            raise CustomException(sys, err)

    def initialize_data_transformation(self, train_path, test_path):
        try:
            logging.info("Data Transformation has been initiated")

            # read train and test data
            train_df = pd.read_csv(train_path)
            test_df = pd.read_csv(test_path)

            logging.info(f"Train dataframe head: \n {train_df.head().to_string()}")
            logging.info(f"Test dataframe head: \n {test_df.head().to_string()}")

            target_feature_col = "NObeyesdad"

            # split the data into input and target feature
            input_feature_train_df = train_df.drop([target_feature_col], axis=1)
            target_feature_train = train_df[target_feature_col]
            input_feature_test_df = test_df.drop([target_feature_col], axis=1)
            target_feature_test = test_df[target_feature_col]
            logging.info("Input and Target feature has been successfully split")

            # transform the feature using preprocessor
            preprocessor_obj, target_preprocessor_obj = self.get_data_transformation_object()
            input_train_arr = preprocessor_obj.fit_transform(input_feature_train_df)
            input_test_arr = preprocessor_obj.transform(input_feature_test_df)
            target_feature_train = target_preprocessor_obj.fit_transform(pd.DataFrame(target_feature_train))
            target_feature_test = target_preprocessor_obj.transform(pd.DataFrame(target_feature_test))

            train_arr = np.c_[input_train_arr, np.array(target_feature_train)]
            test_arr = np.c_[input_test_arr, np.array(target_feature_test)]

            logging.info("Data Transformation has been successfully completed")

            save_object(self.data_transformation_config.preprocessor_pickle_path, preprocessor_obj)
            logging.info("Preprocessor picked was successfully saved")

            return (train_arr,
                    test_arr,
                    self.data_transformation_config.preprocessor_pickle_path)
        except Exception as err:
            raise CustomException(sys, err)


"""Test cases for variable fields
"""
import unittest

from lighty.db import fields, models
from lighty.monads import ErrorMonad
from lighty import utils, validators

class TrueFalseValidator(validators.Validator):
    def validate(self, value):
        return value if value else self.error(value)

class CustomErrorMessageValidator(TrueFalseValidator):
    def message(self, value):
        return 'Value: %s' % value

class TestModel(models.Model):
    name = fields.CharField(unique=True)
    test_type = fields.CharField(choices=[('t', 'test'), ('c', 'test case')])
    positive = fields.PositiveIntegerField()

class ValidatorClassTestCase(unittest.TestCase):
    """Test case for Validator class and validate function
    """

    def testSimpleValidator(self):
        '''Test simplest validator
        '''
        validator = TrueFalseValidator()
        assert validator(True), 'Error validating True or False'
        assert isinstance(validator(False), ErrorMonad), ('Wrong validation '
                'result type: %s' % type(validator(False)))
        assert str(validator(False)) == 'Validation error', (
                'Wrong validation default error message')

    def testCustomErrorMessage(self):
        '''Test validator with customized error message format
        '''
        validator = TrueFalseValidator('Value equals to False')
        message = str(validator(False))
        assert message == 'Value equals to False', (
                'Wrong validation custom error message: %s' % message)
        validator = CustomErrorMessageValidator()
        message = str(validator(False))
        assert message == 'Value: False', (
                'Wrong validation custom error message: %s' % message)

class ValidatorsTestCase(unittest.TestCase):
    '''Test case for standart validators included in distribution
    '''

    def testChoicesValidator(self):
        '''Test ChoicesValidator class
        '''
        validator = validators.ChoicesValidator([(1, 'One'), (2, 'Two')])
        result = validator(1)
        assert result, 'Unexpected validation error: %s' % result
        result = validator(2)
        assert result, 'Unexpected validation error: %s' % result
        result = validator(3)
        assert not result, 'Validation error missed: %s' % result

class ModelValidatorsTestCase(unittest.TestCase):
    '''Test case to check model validators
    '''

    def testModelValidator(self):
        '''Test validators() method in model class
        '''
        validators = TestModel.validators()
        assert len(validators) == 3, ('Wrong validator groups number: %s' %
                                      len(validators))
        names = sorted(utils.dict_keys(validators.keys()))
        assert names == sorted(TestModel._fields), ('Wrong validator groups '
                                                    'names: %s' % names)
        assert len(validators['test_type']) == 3, ('Wrong validators number '
                'for CharField with choices: %s' % (validators['test_type'], ))
        assert len(validators['positive']) == 3, ('Wrong validators number '
                'for PositiveIntegerField: %s' % (validators['positive'], ))
#        assert len(validators['name']) == 2, ('Wrong validators number '
#                'for uniquer char field: %s' % validators['name'])
#
#    def testUniqueField(self):
#        exists = TestModel(name="1", test_type='t', positive=1).save()

class ValidateTestCase(unittest.TestCase):
    '''Test case used to check validate function
    '''

    def testValidateFunction(self):
        '''Test validate function
        '''
        valids = {'field': [TrueFalseValidator()]}
        results = validators.validate(valids, {'field': True})
        assert results['field'] is True, ('Wrong validation results: %s' %
                                          results)
        results = validators.validate(valids, {'field': False})
        assert isinstance(results['field'], ErrorMonad), (
                'Wrong validation results: %s' % results)
        assert not results, 'Wrong validation results: %s' % repr(results)

def test():
    suite = unittest.TestSuite()
    suite.addTest(ValidatorClassTestCase('testSimpleValidator'))
    suite.addTest(ValidatorClassTestCase('testCustomErrorMessage'))
    suite.addTest(ValidatorsTestCase('testChoicesValidator'))
    suite.addTest(ModelValidatorsTestCase('testModelValidator'))
    suite.addTest(ValidateTestCase('testValidateFunction'))
    return suite


'''

i might need to just write this myself. chat is having a hard time
'''
import torch
import torchtext
from torch.utils.data import Dataset
from torch.utils.data import DataLoader
from nltk.parse.corenlp import CoreNLPDependencyParser

def main():
        # Example list of sentences
    sentences = ['I prefer the morning flight through Denver', 'What is the airspeed of an unladen swallow ?']

    # Create the dataset
    dataset = DependencyParserDataset(sentences)

    # dataset[0]
    # print(dataset[0][1])
    # Create a DataLoader to iterate over the dataset
    dataloader = DataLoader(dataset, batch_size=2, shuffle=True, collate_fn=collate_fn)

    # # # Iterate over the data
    for (sentence, label) in dataloader:
    #     # Clean up the formatting of the label
    #     label = dataset.clean_label(label)
        print(sentence)
        print(label)

class DependencyParserDataset(Dataset):
    def __init__(self, sentences):
        self.sentences = sentences
        self.dep_parser = CoreNLPDependencyParser(url='http://localhost:9000')

    def __len__(self):
        return len(self.sentences)

    def __getitem__(self, index):
        sentence = self.sentences[index]

        # print("sentence\n", sentence)
        label = self.generate_label(sentence)
        # print("\nlabel\n",label)

        # Split the sentence into words
        words = sentence.split()

        # Create a vocabulary that maps words to integers
        vocabulary = {word: i for i, word in enumerate(set(words))}

        # Convert the sentence to a sequence of integers
        sentence_data = [vocabulary[word] for word in words]

        # Convert the sequence to a tensor
        sentence_tensor = torch.tensor(sentence_data, dtype=torch.long)

        return (sentence_tensor, label)

    def from_tuples(self, tuples):
        # Initialize an empty list of tokens
        tokens = []
        counter = 0

        # Iterate over the tuples
        for i, tup in enumerate(tuples):
            # Extract the token, part-of-speech, and dependency index from the tuple
            # token, pos, dep = tup[0][0], tup[0][1], i+1
            token, pos, dep = tup[0][0], tup[0][1], tup[2][0]
            # Create a dictionary for the token and add it to the list of tokens
            tokens.append({'token': token, 'pos': pos, 'dep': dep, 'key': counter})
            #we can ignore counter in our training and conversion back to human readable
            counter+=1

        return tokens

    def generate_label(self, sentence):
        parses = self.dep_parser.parse(sentence.split())
        prelim = [[(governor, dep, dependent) for governor, dep, dependent in parse.triples()] for parse in parses]
        # tokenized = nltk.word_tokenize(prelim[0])
        embeddings = torchtext.vocab.GloVe(name='6B', dim=100)
        tokens = prelim[0]
        #conversion function here
        tokens = self.from_tuples(tokens)
        print(tokens)
        embedding_vectors = [embeddings.vectors[embeddings.stoi[token['token']]] for token in
                             tokens]
        # print(embedding_vectors)

        '''
        [(('word', 'nsub'), 'connection', (('word','nn')), ...] is a terrible label bro. adjust
        your preprocessing. see the tmp file to continue. need to get dinner
        '''
        tokenized_label = embeddings.stoi(prelim)
        return embeddings.vector[tokenized_label]

    # def clean_label(self, label):
    #     return [[(governor[0], dep, dependent[0]) for governor, dep, dependent in parse] for parse in label]

def collate_fn(data):
    # Unpack the data into a list of sentences and a list of labels
    sentences, labels = zip(*data)
    # print(sentences, labels)

    # Convert the sentences to tensors
    tensor_sentences = [torch.tensor(s, dtype=torch.long) for s in sentences]
    # tensor_sentences = tensor_sentences[0]

    # Pad the sequences so they are all the same length
    padded_sentences = torch.nn.utils.rnn.pad_sequence(tensor_sentences, batch_first=True)
    print(labels)
    padded_labels = torch.nn.utils.rnn.pad_sequence(labels, batch_first=True)

    return (padded_sentences, padded_labels)

if __name__ == '__main__':
    main()