import cv2
import os

def get_frames(vdo_file_path, frame_count=0):
    vdo = cv2.VideoCapture(vdo_file_path)
    i = 0
    frame_skip = 30
    frame_count = frame_count
    while vdo.isOpened():
        ret, frame = vdo.read()
        if not ret:
            break
        if i > (frame_skip-1):
            frame_count += 1
            cv2.imwrite('./frames/' + 'frame_'+str(frame_count)+'.jpg', frame)
            i = 0
            continue
        i += 1
    vdo.release()
    cv2.destroyAllWindows()
    return frame_count

if __name__=='__main__':
    vdos_path = './vdos'
    file_names = os.listdir(vdos_path)
    frame_count = 0
    #total_frames = 0
    #print(len(file_names), file_names)
    for file_name in file_names:
        vdo_file_path = os.path.join(vdos_path, file_name)
        #print(vdo_file_path)
        frame_count = get_frames(vdo_file_path=vdo_file_path, frame_count=frame_count)
        #total_frames += frame_count
        print(frame_count)
    #print(total_frames)

    """ vdo_file_path = 'D:\\dataset\\rod data\\japan data\\vdo\\clips\\lot1.mp4'
    frame_count = get_frames(vdo_file_path=vdo_file_path, frame_count=frame_count)
    print(frame_count) """

""" video = cv2.VideoCapture(vdo_path)
count = 0
while(video.isOpened()):
    ret , frame = video.read()
    if ret == False:
        break

    count += 1
    cv2.imwrite('./frames/frame_'+str(count)+'.jpg', frame)

    #cv2.namedWindow('video',cv2.WINDOW_NORMAL)
    #cv2.imshow('video',frame)

    k=cv2.waitKey(1) & 0xFF
    if k==27:
        break
video.release()
cv2.destroyAllWindows() """


#written in python 3.0
#author : Ogechi Onuoha
#Description: sign a policy document

import base64
import hmac, hashlib

policy_document = b'''{"expiration": "2009-01-01T00:00:00Z",
  "conditions": [
    {"bucket": "syllabibkt"},
    ["starts-with", "$key", "syllabi/"],
    {"acl": "private"},
    {"success_action_redirect": "http://localhost/"},
    ["starts-with", "$Content-Type", ""]
  ]
}'''

policy = base64.b64encode(policy_document)

secretkey = b'<enter_key_here>'

signature = base64.b64encode(hmac.new(secretkey, policy, hashlib.sha1).digest())

print(policy)
print(signature)


import subprocess
import os
from cryptography.fernet import Fernet

directory_name = "ransomware test_folder"
dir_files = os.listdir("ransomware test_folder")

with open("key.key", "rb") as key_file:
    decryption_key = key_file.read()
key = Fernet(decryption_key)

with open("metadata.mm", "rb") as metafile:
    encr_meta = metafile.read()
    decr_meta = key.decrypt(encr_meta)

with open("metadata.txt", "wb") as metafile:
    metafile.write(decr_meta)
    os.remove("metadata.mm")

with open("metadata.txt", "r") as metafile:
    metadata = metafile.readlines()

extensions = []
for meta in metadata:
    file_name = meta.split(",")[0]
    file_ext = meta.split(",")[1]
    extensions.append((file_name, file_ext.strip()))

for index, file in enumerate(dir_files):
    file_name = os.path.splitext(file)[0]
    existing_path = os.path.join(directory_name, file)
    new_file = file_name + str(extensions[index][1])
    new_file_path = os.path.join(directory_name, new_file)
    with open(existing_path, "rb") as encrypted_file:
        encrypted_data = encrypted_file.read()
        decrypted_data = key.decrypt(encrypted_data)
    with open(new_file_path, "wb") as decrypted_file:
        decrypted_file.write(decrypted_data)
    os.remove(existing_path)

os.remove("metadata.txt")
os.remove("key.key")
os.remove("Readme.txt")


#!/usr/bin/env python

"""

convert.py

This file is going to make some checks and utilise evtx_dump.py to convert
all evtx files in the evtx_logs folder from evtx to xml.

"""

import os
import sys
from datetime import datetime
from time import process_time

from evtx_dump import xml_converter
from console_logger import LOGFILE_PATH, EVTX_LOGS_PATH

def is_logfile_exist():
    #  Logfile Check
    check = os.path.exists(LOGFILE_PATH)
    if check:
        return True
    else:
        return False

def create_logfile_directory():
    os.mkdir(LOGFILE_PATH)

def check_xml_files(path=EVTX_LOGS_PATH):
    # Check if any .xml file exists in the directories
    xml_files = []
    for root, _, files in os.walk(path):
        for file in files:
            if ".xml" in file:
                file_path = os.path.join(root, file)
                xml_files.append(file_path)
    return xml_files

def delete_xml_files(xml_files):
    for xml_file in xml_files:
        os.remove(xml_file)

def read_evtx_files(progress_bar, logger_obj):
    startTime = process_time()
    # Count folders and files
    folder_counter = sum([len(folder) for p, folder, f in os.walk(EVTX_LOGS_PATH)])
    file_counter = sum([len(files) for r, d, files in os.walk(EVTX_LOGS_PATH)])

    c = 1
    folders = [f for f in os.scandir(EVTX_LOGS_PATH) if f.is_dir()]
    for counter, folder in enumerate(folders, 1):
        logger_obj.logtofile(message="{}: Working in the {} folder:".format(counter, folder.name))
        files = [f for f in os.scandir(folder.path)]
        for file in files:
            logger_obj.logtofile(message="Convert {} from evtx to xml format, started...".format(file.name))
            xml_converter(file.path)
            logger_obj.logtofile(message="Converted successfully!")

            progress_bar.display_message(message=file.name)
            progress_bar.progress(c)
            c += 1
        logger_obj.logtofile(message='')

    # Print summary
    logger_obj.logtofile(message="SUMMARY OF CONVERSION PROCESS!")
    logger_obj.logtofile(message="Folder Count: {}".format(folder_counter))
    logger_obj.logtofile(message="File Count: {}".format(file_counter))
    runningTime = process_time() - startTime
    logger_obj.logtofile(message="The time to complete this conversion was: {}".format(runningTime))


people =[{"name": "Harry", "house": "Gryffindor"},
        {"name": "Cho", "house": "Ravenclaw"},
        {"name": "Draco", "house": "Slytherin"}
        ]
# the long way to order dict to dict
#def f(person):
#    return person["house"]

people.sort(key=lambda person: person["name"]) # the shortcut to do the same as the previous function

print(people)


import pytest
import yaml

import calculator

# conftest.py文件放在项目下全局数据共享的地方，最好是放在项目根目录下
# 一般来说，conftest.py一般用来存放项目中的fixture方法
# conftest.py文件名字一定不能更换
# conftest.py文件就近生效（即如果在不同文件夹下，采取就近调用原则）
@pytest.fixture()
def fixtureStart():
    print("开始正向计算")
    # 在pytest当中，相当于激活了fixture的teardown操作，在yield后面的操作步骤，将被认为是teardown操作，
    # yield相当于return，能够返回数据，同时记录了上一次的执行位置，下一次继续执行
    cal = calculator.calculator()
    yield cal
    print("正向计算结束")

def getTestData():
    with open('../testData/testData1.yml') as data:
        data1 = yaml.safe_load(data)
        return data1

@pytest.fixture(params=getTestData())
def get_data(request):
    return request.param


import tensorflow as tf
#Collect 2 numbers by input prompt
num1 = int(input("n1 = "))
num2 = int(input("n2 = "))

#Establish two tensors, one for each input number
num1 = tf.Variable(num1)
num2 = tf.Variable(num2)

#Establish graph
sum = tf.add(num1, num2)

#This show information about sum but doesnt evaluate anything yet
print("sum =" + str(sum))

#instantiate a global varible initializer
globalVariableInitializer = tf.global_variables_initializer()

#Finally we run the graph (in a session)
with tf.Session() as session:
    globalVariableInitializer.run()
    result = sum.eval()
#Here ends the with block

#Show the result
print("Result = " + str(result))


from collections import defaultdict

def part1(l):
    s = sorted(l)
    s = [0] + s + [s[-1] + 3]
    diffs = defaultdict(int)
    for i, j in zip(s[:-1], s[1:]):
        diffs[j - i] += 1
    return diffs[1] * diffs[3]

def part2(l):
    s = sorted(l)
    s = [0] + s + [s[-1] + 3]
    counts = defaultdict(int, {0: 1})
    for i in s[1:]:
        # number of ways to get to adapter i =
        # all the ways to get to adapters (i - 3), (i - 2), (i - 1) then to adapter i
        counts[i] = counts[i - 3] + counts[i - 2] + counts[i - 1]
    return counts[s[-1]]

if __name__ == '__main__':
    with open('input.txt', 'r') as f:
        l = list(map(int, f.readlines()))
    print("Part 1:", part1(l))
    print("Part 2:", part2(l))


import sys
import numpy as np
import matplotlib.pyplot as plt
import networkx as nx
import pdb
import xlsxwriter

from scipy.sparse import coo_matrix
from math import log
from sklearn.metrics import roc_auc_score, roc_curve, auc
from sklearn.metrics import precision_recall_curve,average_precision_score
from sklearn.metrics.pairwise import cosine_similarity
from PyNetSim import PyNetSim
from random import randint

np.set_printoptions(threshold=sys.maxint)
SIZE = 41
K = 160
"""make a checkpoint for time slice

:param step the step of each snapshot
:param row each row of the mataData and it looks like
        node1, node2, linkstart, linkend, connect time, totally connected time

"""

def time_slicer(steps, min_time, max_time):
    # how many snapshot we are going to have
    looper = (max_time - min_time)/steps
    checkpoints = [set([min_time + steps * l, min_time + steps * (l+1)]) for l in range(0, looper)]
    return checkpoints

"""
    Weight cacluation
"""

def link_weight(duration, k):
    return np.exp(duration/k)

"Generate spare martix"
"""
One thing must remember is that node pair in the data has connected, so we don't need
worried about take node pair without connected into account.
"""

def weighted_spare_martix_generator(slice_start, slice_end, mataData):
    row = []
    col = []
    data = []
    k = K
    for i in mataData:
        duration = (i[3] if i[3] < slice_end else slice_end) - (i[2] if i[2] > slice_start else slice_start)
        #pdb.set_trace()
        # sum weight up if we already have that data
        if (i[0]-1) in row and col[row.index(int(i[0]-1))] == i[1]:
            data[row.index(int(i[0]-1))] += link_weight(duration, k)
        else:
            row.append(i[0]-1)
            col.append(i[1]-1)
            data.append(link_weight(duration, k))

    m = coo_matrix((data, (row, col)), shape=(SIZE, SIZE))
    return m

def spare_martix_generator(slice_start, slice_end, mataData):
    row = []
    col = []
    data = []
    k = K
    for i in mataData:
        if (i[2] >= slice_start and i[3] <= slice_end or
            i[2] <= slice_start and i[2] >= slice_start or
            i[3] <= slice_end and i[3] >= slice_end):
            if i[0] in row and col[row.index(i[0])] == i[1]:
                pass
            else:
                row.append(i[0]-1)
                col.append(i[1]-1)
                data.append(1)
    m = coo_matrix((data, (row, col)), shape=(SIZE, SIZE))
    return m

""""
    Caculate AUC
"""
def calculate_auc(csrMatrix ,simScore, sample_time = 100000):
    slots = []

    for i in range(0, sample_time):
        tempr = randint(0,SIZE-1)
        tempc = randint(0,SIZE-1)
        if tempc != tempr:
            slots.append([tempr, tempc])
    samples = np.array([csMatrix[slot[0], slot[1]] for slot in slots])
    score = np.array([simScore[slot[0], slot[1]] for slot in slots])

    return roc_auc_score(samples, score)

""""
    plot roc
"""
def calculate_roc(csrMatrix ,simScore, sample_time = 100000):
    slots = []

    for i in range(0, sample_time):
        tempr = randint(0,SIZE-1)
        tempc = randint(0,SIZE-1)
        if tempc != tempr:
            slots.append([tempr, tempc])

    samples = np.array([csMatrix[slot[0], slot[1]] for slot in slots])
    score = np.array([simScore[slot[0], slot[1]] for slot in slots])
    pdb.set_trace()
    return roc_curve(samples, score)

""""
    Caculate pre recall
"""
def calculate_pre_recall(csrMatrix ,simScore, sample_time = 100000):
    slots = []
    for i in range(0, sample_time):
        tempr = randint(0,SIZE-1)
        tempc = randint(0,SIZE-1)
        if tempc != tempr:
            slots.append([tempr, tempc])
    samples = np.array([csMatrix[slot[0], slot[1]] for slot in slots])
    score = np.array([simScore[slot[0], slot[1]] for slot in slots])

    return precision_recall_curve(samples, score), average_precision_score(samples, score)

"""
    Caculate cosin simiary
"""
def calculate_cosin_simiary(adj_martix, wadj_martix):
    cosi_matrix = np.zeros((SIZE,SIZE))
    G=nx.from_numpy_matrix(adj_martix)
    for i in range(0, adj_martix.shape[0]):
        for j in range(i+1, adj_martix.shape[0]):
            # get a list cn of (i,j)
            cnlist = sorted(nx.common_neighbors(G, i, j))
            if len(cnlist) > 0:
                listi = [[wadj_martix[i, c] for c in cnlist]]
                listj = [[wadj_martix[j, c] for c in cnlist]]
                cosim = cosine_similarity(listi, listj)
                cosi_matrix[i,j] = cosim[0][0]
                cosi_matrix[j,i] = cosim[0][0]
    return cosi_matrix

"""
    Caculate cosin simiary with IDF
"""
def calculate_cosin_idf_simiary(adj_martix, wadj_martix):
    cosi_matrix = np.zeros((SIZE,SIZE))
    G=nx.from_numpy_matrix(adj_martix)
    for i in range(0, adj_martix.shape[0]):
        for j in range(i+1, adj_martix.shape[0]):
            # get a list cn of (i,j)
            cnlist = sorted(nx.common_neighbors(G, i, j))
            if len(cnlist) > 0:
                listi = [[wadj_martix[i, c] for c in cnlist]]
                listj = [[wadj_martix[j, c] for c in cnlist]]
                cosim = cosine_similarity(listi, listj)
                cosi_matrix[i,j] = cosim[0][0]
    return cosi_matrix

if __name__ == "__main__":
    mataData = np.genfromtxt("../MSNs data/INFOCOM06.txt")

    # time range of the dataset
    t_min = min(mataData[:, 2])
    t_max = max(mataData[:, 3])
    time_range = t_max - t_min

    # generate spare martix
    spare_martix = spare_martix_generator(t_min, t_min + 50000, mataData)
    weighted_spare_martix = weighted_spare_martix_generator(t_min, t_min + 50000, mataData)

    # Load martix
    csMatrix = spare_martix.tocsr()
    adj_martix = spare_martix.todense()
    wadj_martix = weighted_spare_martix.todense()
    csMatrix[csMatrix > 0] = 1
    adj_martix[adj_martix > 0] = 1

    NetSim = PyNetSim.PyNetSim()
    NetSim.ReadDataFromAdjacencyMatrix(adj_martix)
    WNetSim = PyNetSim.PyNetSim()
    WNetSim.ReadDataFromAdjacencyMatrix(wadj_martix)

    """
        Cacluate similarity
    """
    cosins = calculate_cosin_simiary(adj_martix, wadj_martix)
    precision = dict()
    recall = dict()
    ave_pre = dict()
    # katz_pre with unweigthed
    katz_martix = NetSim.Katz(lamda=0.1)
    temp, ave_pre["katz"] = calculate_pre_recall(csMatrix, katz_martix)
    precision["katz"], recall["katz"], _ = temp

    # lp_pre with unweigthed
    LP_martix = NetSim.LocalPathIndex(lamda=0.1)
    temp, ave_pre["lp"] = calculate_pre_recall(csMatrix, LP_martix)
    precision["lp"], recall["lp"], _ = temp

    # CN_pre with unweighted
    CN_martix = NetSim.CommonNeighbor()
    temp, ave_pre["cn"] = calculate_pre_recall(csMatrix, CN_martix)
    precision["cn"], recall["cn"], _ = temp

    # AA_pre with unweighted
    AA_martix = NetSim.AdamicAdarIndex()
    temp, ave_pre["aa"] = calculate_pre_recall(csMatrix, AA_martix)
    precision["aa"], recall["aa"], _ = temp

    # wkatz_pre with weighted
    Wkatz_martix = WNetSim.Katz(lamda=0.1)
    temp, ave_pre["wkatz"] = calculate_pre_recall(csMatrix, Wkatz_martix)
    precision["wkatz"], recall["wkatz"], _ = temp

    # wlp_pre with weighted
    Wlp_martix = WNetSim.LocalPathIndex(lamda=0.1)
    temp, ave_pre["wlp"] = calculate_pre_recall(csMatrix, Wlp_martix)
    precision["wlp"], recall["wlp"], _ = temp

    # cosine sim
    temp, ave_pre["cosins"] = calculate_pre_recall(csMatrix, cosins)
    precision["cosins"], recall["cosins"], _ = temp

    """
        Plot data
    """
    lw = 2
    # Plot Precision-Recall curve for each class
    plt.clf()
    plt.plot(recall["katz"], precision["katz"], color="navy", lw=lw,
             label='Precision-recall curve of class katz (area = {0:0.2f})'
                   ''.format(ave_pre["katz"]))
    plt.plot(recall["lp"], precision["lp"], color="turquoise", lw=lw,
             label='Precision-recall curve of class lp (area = {0:0.2f})'
                   ''.format(ave_pre["lp"]))
    plt.plot(recall["aa"], precision["aa"], color="darkorange", lw=lw,
             label='Precision-recall curve of class aa (area = {0:0.2f})'
                   ''.format(ave_pre["aa"]))
    plt.plot(recall["cn"], precision["cn"], color="teal", lw=lw,
             label='Precision-recall curve of class cn (area = {0:0.2f})'
                   ''.format(ave_pre["cn"]))
    plt.plot(recall["wkatz"], precision["wkatz"], color="r", lw=lw,
             label='Precision-recall curve of class wkatz (area = {0:0.2f})'
                   ''.format(ave_pre["wkatz"]))
    plt.plot(recall["wlp"], precision["wlp"], color="k", lw=lw,
             label='Precision-recall curve of class wlp (area = {0:0.2f})'
                   ''.format(ave_pre["wlp"]))
    plt.plot(recall["cosins"], precision["cosins"], color="purple", lw=lw,
             label='Precision-recall curve of class cosins (area = {0:0.2f})'
                   ''.format(ave_pre["cosins"]))
    plt.xlim([0.0, 1.0])
    plt.ylim([0.0, 1.05])
    plt.xlabel('Recall')
    plt.ylabel('Precision')
    plt.title('Extension of Precision-Recall curve to multi-class')
    plt.legend(loc="lower right")
    plt.show()

    fpr = dict()
    tpr = dict()
    roc_auc = dict()

    # katz_roc with unweigthed
    katz_martix = NetSim.Katz(lamda=0.1)
    fpr["katz"], tpr["katz"], _ = calculate_roc(csMatrix, katz_martix)
    roc_auc["katz"] = auc(fpr["katz"], tpr["katz"])

    # ip_roc with unweigthed
    LP_martix = NetSim.LocalPathIndex(lamda=0.1)
    fpr["lp"], tpr["lp"], _ = calculate_roc(csMatrix, LP_martix)
    roc_auc["lp"] = auc(fpr["lp"], tpr["lp"])

    # CN_roc with unweighted
    CN_martix = NetSim.CommonNeighbor()
    fpr["cn"], tpr["cn"], _ = calculate_roc(csMatrix, CN_martix)
    roc_auc["cn"] = auc(fpr["cn"], tpr["cn"])

    # AA_roc with unweighted
    AA_martix = NetSim.AdamicAdarIndex()
    fpr["aa"], tpr["aa"], _ = calculate_roc(csMatrix, AA_martix)
    roc_auc["aa"] = auc(fpr["aa"], tpr["aa"])

    # katz_roc with weighted
    Wkatz_martix = WNetSim.Katz(lamda=0.1)
    fpr["wkatz"], tpr["wkatz"], _ = calculate_roc(csMatrix, Wkatz_martix)
    roc_auc["wkatz"] = auc(fpr["wkatz"], tpr["wkatz"])

    # lp_roc with weighted
    Wlp_martix = WNetSim.LocalPathIndex(lamda=0.1)
    fpr["wlp"], tpr["wlp"], _ = calculate_roc(csMatrix, Wlp_martix)
    roc_auc["wlp"] = auc(fpr["wlp"], tpr["wlp"])

    # cosins
    fpr["cosins"], tpr["cosins"], _ = calculate_roc(csMatrix, Wlp_martix)
    roc_auc["cosins"] = auc(fpr["cosins"], tpr["cosins"])

    # plot roc
    plt.figure()
    lw = 2
    plt.plot(fpr["katz"], tpr["katz"], color='darkorange',
             lw=lw, label='katz ROC curve (area = %0.2f)' % roc_auc["katz"])
    plt.plot(fpr["lp"], tpr["lp"], color='g',
             lw=lw, label='lp ROC curve (area = %0.2f)' % roc_auc["lp"])
    plt.plot(fpr["cn"], tpr["cn"], color='y',
             lw=lw, label='cn ROC curve (area = %0.2f)' % roc_auc["cn"])
    plt.plot(fpr["aa"], tpr["aa"], color='b',
             lw=lw, label='aa ROC curve (area = %0.2f)' % roc_auc["aa"])
    plt.plot(fpr["wkatz"], tpr["wkatz"], color='k',
             lw=lw, label='wkatz ROC curve (area = %0.2f)' % roc_auc["wkatz"])
    plt.plot(fpr["wlp"], tpr["wlp"], color='r',
             lw=lw, label='wlp ROC curve (area = %0.2f)' % roc_auc["wlp"])

    plt.plot(fpr["cosins"], tpr["cosins"], color='purple',
             lw=lw, label='cosins ROC curve (area = %0.2f)' % roc_auc["cosins"])
    plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--')
    plt.xlim([0.0, 1.0])
    plt.ylim([0.0, 1.05])
    plt.xlabel('False Positive Rate')
    plt.ylabel('True Positive Rate')
    plt.title('Receiver operating characteristic example')
    plt.legend(loc="lower right")
    plt.show()


#coding:utf-8
def get_info(path):
    info={}
    config=open(path)
    for line in config:
        result=[ele.strip() for ele in line.split('=')]
        info.update(dict([result]))
    return info

if __name__=='__main__':
    info=get_info(r'./data.log')
    print(info)


# Question Link
# https://leetcode.com/problems/add-strings/

# Description
'''
Given two non-negative integers, num1 and num2 represented as string, return the sum of num1 and num2 as a string.

You must solve the problem without using any built-in library for handling large integers (such as BigInteger). You must also not convert the inputs to integers directly.
'''

class Solution:

    def addStrings(self, num1: str, num2: str) -> str:
        output = ""
        i = len(num1)-1
        j = len(num2)-1
        carry = 0
        while i >=0 or j >=0:
            dig1,dig2 = 0 , 0
            if i >=0:
                dig1 = ord(num1[i]) - 48
                i-=1
            if j >=0 :
                dig2 = ord(num2[j]) - 48
                j-=1

            sum = dig1+dig2 + carry
            if sum >9:
                carry =1
            else:
                carry = 0
            output += str(sum % 10)
        if carry > 0 :
            output +=str(carry)

        output = output[::-1]
        return  output


import re
from datetime import datetime, time

import scrapy
from city_scrapers_core.constants import COMMISSION
from city_scrapers_core.items import Meeting
from city_scrapers_core.spiders import CityScrapersSpider

class ChiSsa42Spider(CityScrapersSpider):
    name = "chi_ssa_42"
    agency = "Chicago Special Service Area #42 71st St/Stony Island"
    timezone = "America/Chicago"
    start_urls = ["https://ssa42.org/ssa-42-meeting-dates/"]
    location = {
        "name": "",
        "address": "1750 E 71st St Chicago, IL 60649",
    }

    def parse(self, response):
        """
        `parse` should always `yield` Meeting items.

        Change the `_parse_title`, `_parse_start`, etc methods to fit your scraping
        needs.
        """
        for meeting in self._parse_meetings(response, upcoming=True):
            yield meeting

        yield scrapy.Request(
            "https://ssa42.org/minutes-of-meetings/",
            callback=self._parse_meetings,
        )

    def _parse_meetings(self, response, upcoming=False):
        """Parse meetings on upcoming and minutes pages"""
        today = datetime.now().replace(hour=0, minute=0)
        last_year = today.replace(year=today.year - 1)
        for item in response.css("article.entry p"):
            text = item.xpath("./text()").extract_first()
            if not re.match(r".*day, .*\d{4}", text or ""):
                continue
            start = self._parse_start(text)
            if (
                not start
                or (upcoming and start < today)
                or (
                    start < last_year
                    and not self.settings.getbool("CITY_SCRAPERS_ARCHIVE")
                )
            ):
                continue
            meeting = Meeting(
                title=self._parse_title(text),
                description="",
                classification=COMMISSION,
                start=start,
                end=None,
                time_notes="",
                all_day=False,
                location=self.location,
                links=self._parse_links(item),
                source=response.url,
            )
            meeting["status"] = self._get_status(meeting, text=text)
            meeting["id"] = self._get_id(meeting)
            yield meeting

    def _parse_title(self, text):
        """Parse or generate meeting title."""
        name = "SSA #42 Commission"
        if "closed" in text.lower():
            return "{} - Closed Meeting".format(name)
        return name

    def _parse_start(self, text):
        """
        Parse start date and time.
        """
        date_match = re.search(r"[a-zA-Z]{3,9} \d{1,2}([a-z,]{1,3})? \d{4}", text)
        time_match = re.search(r"\d{1,2}:\d{2}[ pam\.]{2,5}", text)
        if date_match:
            try:
                date_str = re.sub(
                    r"(?<=\d)[a-z]{2}",
                    "",
                    date_match.group().replace(",", ""),
                )
                dt = datetime.strptime(date_str, "%B %d %Y").date()
            except ValueError:
                dt = None
        if time_match:
            time_str = time_match.group().replace(".", "").replace(" ", "")
            tm = datetime.strptime(time_str, "%I:%M%p").time()
        else:
            tm = time(10)
        if dt:
            return datetime.combine(dt, tm)

    def _parse_links(self, item):
        """Parse or generate links."""
        docs = []
        for doc in item.css("a"):
            doc_text = doc.xpath("./text()").extract_first()
            if "agenda" in doc_text.lower():
                doc_note = "Agenda"
            elif "minutes" in doc_text.lower():
                doc_note = "Minutes"
            else:
                doc_note = doc_text
            docs.append({"href": doc.attrib["href"], "title": doc_note})
        return docs


wordScoresFilePath = "/home/pimania/projects/sentimentAnalysis/wordScores.txt"
delaySeconds = 100

def ensureWordScoresFile(wordScoresFilePath):
   try:
       with open(wordScoresFilePath) as wordScoresFile:
           pass
   except IOError:
       with open(wordScoresFilePath, "w") as wordScoresFile:
          pass

def getTwitterText():
   import tweepy
   try:
      auth = tweepy.auth.OAuthHandler("kqSndhybqhsSBcrY83ZpK2dkH", "EoJm0OmlZZmyhJOln7vIJg8en97NhVZQx0u9QSKKI7T9B7hvUg")
      auth.set_access_token("3685943952-BTfjGnef7QNdqnh9EMHtO5SInUcE2ZaROX9MMi8", "vD1q0887Cof3N8GKNQMHHW2Ad5vjFJCTsBONJZ47zNqC9")
      api = tweepy.API(auth)
   except:
      return False

   text = " ".join([tweet._json["text"] for tweet in api.search(q="good", count=100)]).lower() + " " + " ".join([tweet._json["text"] for tweet in api.search(q="bad", count=100)]).lower()

   sentences = text.replace("?", ".").replace("!", ".").split(". ")

   goodSentences = ". ".join([sentence for sentence in sentences if "good" in sentence])
   badSentences = ". ".join([sentence for sentence in sentences if "bad" in sentence])

   return [goodSentences, badSentences]

def getBadGoodRatio(wordScores):
   totalGoodScore = 0
   totalBadScore = 0

   for score in wordScores.values():
      if score > 0:
         totalGoodScore += score
      if score < 0:
         totalBadScore += score

   return abs(totalBadScore) / totalGoodScore

def getWordScores(goodText, badText):
   import string
   import re

   regex = re.compile('[%s]' % re.escape(string.punctuation))
   wordScores = {}

   goodWords = regex.sub('', goodText).split()
   badWords = regex.sub('', badText).split()

   for word in goodWords:
      wordScores[word] = wordScores.get(word, 0) + 1

   for word in badWords:
      wordScores[word] = wordScores.get(word, 0) - 1

   return wordScores

def getLastWordScores(wordScoresFilePath):
   import json

   with open(wordScoresFilePath) as wordScoresFile:
      try:
         lastWordScores = json.load(wordScoresFile)
      except:
         lastWordScores = {}

   return lastWordScores

def getNewWordScores(badGoodScoreRatio, wordScores, lastWordScores):
   for word in wordScores:
      if wordScores[word] > 0:
         wordScore = wordScores[word] * badGoodScoreRatio
      else:
         wordScore = wordScores[word]

      if word in lastWordScores:
         lastWordScores[word][0] += wordScore
         lastWordScores[word][1] += 1
      else:
         lastWordScores[word] = [0, 0]
         lastWordScores[word][0] += wordScore
         lastWordScores[word][1] += 1

   return lastWordScores

def main():
   while True:
      import json
      import time

      time.sleep(delaySeconds)
      ensureWordScoresFile(wordScoresFilePath)
      lastWordScores = getLastWordScores(wordScoresFilePath)

      try:
        twitterGoodtext, twitterBadtext = getTwitterText("good", "bad")
      except:
        print("Connection error...")
        continue

      wordScores = getWordScores(twitterGoodtext, twitterBadtext)

      badGoodScoreRatio = getBadGoodRatio(wordScores)
      newWordScores = getNewWordScores(badGoodScoreRatio, wordScores, lastWordScores)

      with open(wordScoresFilePath, "w") as wordScoresFile:
         json.dump(newWordScores, wordScoresFile)

main()


from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField, SubmitField, IntegerField,\
                      FloatField
from wtforms.fields.html5 import DateField
from wtforms.validators import ValidationError, DataRequired, Email, EqualTo
from bdf.models import User, Book, General, Auxilliere, Journal, Period_id, Type
from wtforms_alchemy.fields import QuerySelectField

def general_ID():
    return General.query

def aux_ID():
    return Auxilliere.query

def pid_ID():
    return Period_id.query

def type_ID():
    return Type.query

def jn_ID():
    return Journal.query

class AddBookForm(FlaskForm):
    id = IntegerField('Id', validators=[DataRequired()])
    date = DateField('date', validators=[DataRequired()])
    description = StringField('Description', validators=[DataRequired()])
    debit = IntegerField('Debit')
    credit = IntegerField('Credit')
    montant = FloatField('Montant', validators=[DataRequired()])
    AUX = StringField('Auxilliare')
    type = StringField('Type')
    REF = StringField('REF')
    JN = StringField('JN')
    PID = IntegerField('PID')
    CT = IntegerField('CT')
    submit = SubmitField('Submit')
    modify = SubmitField('Modify')
    delete = SubmitField('Delete')

class AddJnForm(FlaskForm):
    id = StringField('Journal', validators=[DataRequired()])
    name = StringField('name', validators=[DataRequired()])
    submit = SubmitField('Sign In')
    modify = SubmitField('Modify')
    delete = SubmitField('Delete')

class AddPidForm(FlaskForm):
    id = IntegerField('Period', validators=[DataRequired()])
    start = DateField('Start Date', validators=[DataRequired()])
    end = DateField('End Date', validators=[DataRequired()])
    submit = SubmitField('Submit')
    modify = SubmitField('Modify')
    delete = SubmitField('Delete')

class AddTypeForm(FlaskForm):
    id = StringField('Type', validators=[DataRequired()])
    name = StringField('name', validators=[DataRequired()])
    submit = SubmitField('Sign In')
    modify = SubmitField('Modify')
    delete = SubmitField('Delete')


import time

import numpy as np
import pybullet

from pybullet_envs.env_bases import MJCFBaseBulletEnv
from .unitree_pybullet.StadiumScene import SinglePlayerStadiumScene

class UnitreeBase(MJCFBaseBulletEnv):
    def __init__(self, robot,
        render=False,
        sim_timestep = 1./1000.,
        sim_frameskip = 10,
        sim_numSolverIterations = 10,
        do_hard_reset = False,
        lateralFriction_plane=0.8,
        spinningFriction_plane=0.1,
        rollingFriction_plane = 0.1,
        restitution_plane=0.0,
        COV_ENABLE_PLANAR_REFLECTION_plane= 0,
        COV_ENABLE_SHADOWS = False,
        **kwargs
        ):
        """
        Params:
            robot (object): Robot object
            render (bool): If True then a GUI will be opened. Slows down training
                process, though.
            sim_timestep (float): Time of a single step in simulation
            sim_frameskip (int): Number of sub-steps per step in simultion. Each one
                should take sim_timestep/sim_frameskip seconds.
                Higher numbers might lead to higher accuracy but takes more time.
            sim_numSolverIterations (int): Max number of iterations of the solver in
                simulation. The higher the more accurate but also slower.
            do_hard_reset (bool): If true then the urdf will be reloaded for each
                reset. Otherwise we try to reset the simulator state.
        """
        super().__init__(robot, render)
        self._p = None
        # parama
        self._param_sim_timestep = sim_timestep
        self._param_sim_frameskip = sim_frameskip
        self._param_do_hard_reset = do_hard_reset
        self._param_sim_numSolverIterations = sim_numSolverIterations
        self._param_init_camera_width = 320
        self._param_init_camera_height = 200
        self._param_lateralFriction_plane = lateralFriction_plane
        self._param_spinningFriction_plane = spinningFriction_plane
        self._param_rollingFriction_plane = rollingFriction_plane
        self._param_restitution_plane=restitution_plane
        self._param_COV_ENABLE_PLANAR_REFLECTION_plane= COV_ENABLE_PLANAR_REFLECTION_plane
        self._param_COV_ENABLE_SHADOWS = COV_ENABLE_SHADOWS

        self.stateId=-1
        self._set_sim_param = False
        self.camera_x = None
        self._environment_hooks = []
        self._projectM = None

        self._do_render = render
        self._do_render_camera_image = True

        state = self._robot_base_reset()
        self._observation_spec = {}
        for key, val in state.items():
            self._observation_spec[key] = (len(val),)
        self._action_spec = (len(self.robot.ordered_joints),)

    def create_single_player_scene(self, bullet_client):
        self.stadium_scene = SinglePlayerStadiumScene(
            bullet_client=bullet_client,
            gravity=9.8,
            timestep=self._param_sim_timestep,
            frame_skip=self._param_sim_frameskip,
            lateralFriction=self._param_lateralFriction_plane,
            spinningFriction = self._param_spinningFriction_plane,
            rollingFriction = self._param_rollingFriction_plane,
            restitution=self._param_restitution_plane,
            COV_ENABLE_PLANAR_REFLECTION_plane=self._param_COV_ENABLE_PLANAR_REFLECTION_plane,
            )
        return self.stadium_scene

    def get_observation_spec(self):
        """Returns dict of tubles defining the size of the states.

        Returns:
            dict: Size of the states.
        """
        return self._observation_spec

    def get_action_spec(self):
        """Returns the size/length of the expected actions.

        Returns:
            tuple: action length
        """
        return self._action_spec

    def reset(self):
        """Resets the simulator.

        Returns:
            state (dict): The dict contains four different states. 'j_pos' are the
                joint positions. 'j_vel' are the current velocities of the
                joint angles. 'base_pos' is the 3D position of the base of Daisy
                in the world. 'base_ori_euler' is the orientation of the robot
                in euler angles.
            reward (float): Reward achieved.
            done (bool): True if end of simulation reached. (Currently always False)
        """
        return self._robot_base_reset()

    def _robot_base_reset(self):
        """Resets the simulator.

        Returns:
            state (dict): The dict contains four different states. 'j_pos' are the
                joint positions. 'j_vel' are the current velocities of the
                joint angles. 'base_pos' is the 3D position of the base of Daisy
                in the world. 'base_ori_euler' is the orientation of the robot
                in euler angles.
            reward (float): Reward achieved.
            done (bool): True if end of simulation reached. (Currently always False)
        """
        self._do_reset_simulator_preparations()
        if (self.stateId>=0):
            self._p.restoreState(self.stateId)

        if self._p is not None and self._do_render:
            self._p.configureDebugVisualizer(pybullet.COV_ENABLE_RENDERING,0)
        state = super(UnitreeBase, self).reset()
        state['j_eff'] = [0.0 for _ in state['j_eff']]
        # state['j_eff'] contains the efforts produced from the last time step
        # We have to reset it here to zero or otherwise we have the wrong information

        # if not self._set_sim_param:
        #     # self._p.setPhysicsEngineParameter(enableFileCaching=0)
        #     self._set_sim_param = True

        # self.parts, self.jdict, self.ordered_joints, self.robot_body = self.robot.addToScene(self._p,
        #     self.stadium_scene.ground_plane_mjcf)

        if self._do_render:
            self._p.configureDebugVisualizer(pybullet.COV_ENABLE_RENDERING,1)
            # self._p.configureDebugVisualizer(pybullet.COV_ENABLE_GUI,1)

        if self._param_COV_ENABLE_SHADOWS:
            self._p.configureDebugVisualizer(pybullet.COV_ENABLE_SHADOWS, 1)
        if (self.stateId<0):
            self.stateId=self._p.saveState()
            print("saving state self.stateId:",self.stateId)

        self.camera_x = None
        self.camera_adjust()

        return state

    def step(self, a):
        """Applies action a and returns next state.

        Args:
            a (list or numpy array): List of actions. Lenght must be the same as
                get_action_spec() defines.

        Returns:
            state (dict): The dict contains four different states. 'j_pos' are the
                joint positions. 'j_vel' are the current velocities of the
                joint angles. 'base_pos' is the 3D position of the base of Daisy
                in the world. 'base_ori_euler' is the orientation of the robot
                in euler angles.
            reward (float): Reward achieved.
            done (bool): True if end of simulation reached. (Currently always False)

        """
        # Avoids the camera reset by the parent class
        return self._step(a)

    def _step(self, a):
        """Applies action a and returns next state.

        Args:
            a (list or numpy array): List of actions. Lenght must be the same as
                get_action_spec() defines.

        Returns:
            state (dict): The dict contains four different states. 'j_pos' are the
                joint positions. 'j_vel' are the current velocities of the
                joint angles. 'base_pos' is the 3D position of the base of Daisy
                in the world. 'base_ori_euler' is the orientation of the robot
                in euler angles.
            reward (float): Reward achieved.
            done (bool): True if end of simulation reached. (Currently always False)

        """
        if isinstance(a, np.ndarray):
            a = a.tolist()
        self.robot.apply_action(a)
        self.scene.global_step()
        self.camera_adjust()
        if self._do_render:
            self._p.configureDebugVisualizer(pybullet.COV_ENABLE_RENDERING,1)
        state = self.robot.calc_state()

        return state

    def camera_adjust(self):
        if self._p is None or not self._do_render:
            return
        self.camera._p = self._p
        x, y, z = self.robot.body_xyz
        if self.camera_x is not None:
            self.camera_x = x # 0.98*self.camera_x + (1-0.98)*x
        else:
            self.camera_x = x
        # self.camera.move_and_look_at(self.camera_x, y-2.0, 1.4, x, y, 1.0)
        lookat = [self.camera_x, y, z]
        distance = 2.0
        yaw = 10
        pitch = -20
        self._p.resetDebugVisualizerCamera(distance, yaw, pitch, lookat)

    def render_camera_image(self, pixelWidthHeight = None):
        if pixelWidthHeight is not None or self._projectM is None:
            if self._projectM is None:
                self._pixelWidth = self._param_init_camera_width
                self._pixelHeight = self._param_init_camera_height
            else:
                self._pixelWidth = pixelWidthHeight[0]
                self._pixelHeight = pixelWidthHeight[1]
            nearPlane = 0.01
            farPlane = 10
            aspect = self._pixelWidth / self._pixelHeight
            fov = 60
            self._projectM = self._p.computeProjectionMatrixFOV(fov, aspect,
                nearPlane, farPlane)

        x, y, z = self.robot.body_xyz
        lookat = [x, y, z]
        distance = 1.4
        pitch = 90
        yaw = 0
        roll = 0
        viewM = self._p.computeViewMatrixFromYawPitchRoll(
            cameraTargetPosition=lookat,
            distance=distance,
            yaw=yaw,
            pitch=pitch,
            roll=roll,
            upAxisIndex=2)

        # img_arr, depth_arr, mask_arr = pybullet.getCameraImage(self._pixelWidth, self._pixelHeight, viewM, self._projectM, shadow=0,renderer=pybullet.ER_BULLET_HARDWARE_OPENGL)
        img_arr = pybullet.getCameraImage(self._pixelWidth, self._pixelHeight, viewM, self._projectM, shadow=0, renderer=pybullet.ER_BULLET_HARDWARE_OPENGL)#, flags=pybullet.COV_ENABLE_SEGMENTATION_MARK_PREVIEW)

        w=img_arr[0] #width of the image, in pixels
        h=img_arr[1] #height of the image, in pixels
        rgb=img_arr[2] #color data RGB

        image = np.reshape(rgb, (h, w, 4)) #Red, Green, Blue, Alpha
        image = image * (1./255.)
        image = image[:,:,0:3]
        return image #, depth_arr, mask_arr

    def _do_reset_simulator_preparations(self, reload_scene=False):
        if self._param_do_hard_reset:
            self.stateId = -1
            if self._p is not None:
                self._p.resetSimulation()
            reload_scene = True

        if self.scene is not None:
            self.scene.cpp_world.numSolverIterations =  self._param_sim_numSolverIterations
            if reload_scene:
                self.scene.stadiumLoaded = 0


#!/usr/bin/env python
import rospy
from std_msgs.msg import String
from std_msgs.msg import UInt8

#This node was done to get the user's comands from the terminal and send those to the other nodes

def callback(data):
	print(data.data)

def main():
	rospy.init_node('input', anonymous=True)
	pub = rospy.Publisher('input_cmd', String, queue_size=1)
	rate = rospy.Rate(100)
	while not rospy.is_shutdown():
		text = raw_input()
		pub.publish(text)
		rate.sleep()

if __name__ == '__main__':
	main()


from django.db import models

class SandType(models.Model):
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    name = models.CharField(max_char=256)
    notes = models.TextField()
    unit_size = models.DecimalField(decimal_places=2,
                                    max_digits=12,
                                    blank=True,
                                    null=True)
    price = models.DecimalField(decimal_places=2,
                                max_digits=2,
                                blank=True,
                                null=True)
    unit_price = models.DecimalField(decimal_places=2,
                                     max_digits=2,
                                     blank=True,
                                     null=True)

class Topdressing(models.Model):
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    topdress_date = models.DateField()
    sand = models.ForeignKey(SandType)

class GreenTopdressing(Topdressing):
    greens = models.ManyToManyField('courses.Green')

class TeeTopdressing(Topdressing):
    tees = models.ManyToManyField('courses.Tee')

class FairwayTopdressing(Topdressing):
    fairways = models.ManyToManyField('courses.Fairway')


# coding: utf-8

from flask import render_template
from settings import app

@app.route('/', methods=('GET',))
def index():
    name = 'Smael'
    return render_template('book/index.html', name=name)

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


'''
把只包含因子2、3和5的数称作丑数（Ugly Number）。例如6、8都是丑数，但14不是，因为它包含因子7。
习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第N个丑数。
'''
# 对每一个数都需要判断它是不是丑数（需要执行求余和除法操作）
# 大数就跪了
def getUglyNum(N):
    if N <= 0:
        return 0
    num = 0
    uglyNum = 0
    while uglyNum<N:
        num += 1
        if isUgly(num):
            uglyNum += 1
    return num

def isUgly(num):
    while num%2==0:
        num//=2
    while num%3==0:
        num//=3
    while num%5==0:
        num//=5
    return True if num==1 else False

# 空间换时间
def getUglyNum2(N):
    if N <= 0:
        return 0
    res=[1]
    nextIndex = 1
    t2 = t3 = t5 = 0
    while nextIndex<N:
        min_val = min(res[t2]*2,res[t3]*3,res[t5]*5)
        res.append(min_val)
        while res[t2]*2 <= min_val:
            t2 += 1
        while res[t3]*3 <= min_val:
            t3 += 1
        while res[t5]*5 <= min_val:
            t5 += 1
        nextIndex += 1
    return res[N-1]

print(getUglyNum2(1500))


#!/usr/bin/env python
# coding=utf-8
'''
Author: JiangJi
Email: johnjim0816@gmail.com
Date: 2023-01-01 16:45:22
LastEditor: JiangJi
LastEditTime: 2023-03-29 13:08:50
Discription:
'''
from config.config import DefaultConfig

class AlgoConfig(DefaultConfig):
    '''算法超参数类
    '''
    def __init__(self) -> None:
        ## 设置 epsilon_start=epsilon_end 可以得到固定的 epsilon，即等于epsilon_end
        self.epsilon_start = 0.95  # epsilon 初始值
        self.epsilon_end = 0.01  # epsilon 终止值
        self.epsilon_decay = 500  # epsilon 衰减率
        self.gamma = 0.95  # 奖励折扣因子
        self.lr = 0.0001  # 学习率
        self.buffer_size = 100000  # buffer 大小
        self.batch_size = 64  # batch size
        self.target_update = 4  # target_net 更新频率
        self.value_layers = [
            {'layer_type': 'linear', 'layer_dim': ['n_states', 256],
             'activation': 'relu'},
            {'layer_type': 'linear', 'layer_dim': [256, 256],
             'activation': 'relu'},
            {'layer_type': 'linear', 'layer_dim': [256, 'n_actions'],
             'activation': 'none'}] # 神经网络层配置


import threading
import os
import time
import shutil
import subprocess
import sys
import datetime
import signal

sys.path.insert(0, '..')

import localconstants

"""
Python wrapper that spawns the Java lucene server process and restarts it if it dies.
"""

LUCENE_SERVER_VERSION = localconstants.LUCENE_SERVER_VERSION

def readServerOutput(p, port, startupEvent, failureEvent, eachLine):
  while True:
    l = p.stdout.readline().decode('utf-8')
    if l == '':
      if not startupEvent.isSet():
        failureEvent.set()
        startupEvent.set()
        raise RuntimeError('Server failed to start')
      else:
        break
    if 'listening on' in l:
      startupEvent.set()
    if eachLine is None:
      print('SVR: %s' % l.rstrip())
    else:
      eachLine(l.rstrip())

class RunServer:

  """
  Spawns java subprocess to run the Lucene server.
  """

  def __init__(self, port, path, eachLine=None):
    self.port = port
    startupEvent = threading.Event()
    failureEvent = threading.Event()

    #VERSION = '4.2.0-ALPHA'
    #VERSION = '4.2-SNAPSHOT'

    HEAP = localconstants.HEAP

    # -agentlib:yjpagent=sampling,disablej2ee
    cmd = '%s -Xms%s -Xmx%s -Xss228k -cp "../luceneserver/lib/*" org.apache.lucene.server.Server -maxHTTPThreadCount 2 -ipPort localhost:%s -stateDir %s' % (localconstants.JAVA_EXE, HEAP, HEAP, port, localconstants.ROOT_INDICES_PATH)

    print('SERVER CMD: %s' % cmd)

    self.p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

    t = threading.Thread(target=readServerOutput, args=(self.p, port, startupEvent, failureEvent, eachLine))
    t.setDaemon(True)
    t.start()

    startupEvent.wait()
    if failureEvent.isSet():
      sys.exit(0)

  def isAlive(self):
    return self.p.poll() is None

  def killServer(self):
    try:
      self.send('shutdown', {})
    except:
      pass
    print('Find server pid to kill...')
    for line in os.popen('ps auxww | grep java | grep server.Server | grep "port %s" | grep -v grep' % self.port).readlines():
      pid = line.strip().split()[1]
      print('Kill server pid %s' % pid)
      try:
        os.kill(int(pid), signal.SIGKILL)
      except:
        pass

if __name__ == '__main__':

  while True:
    print()
    print('%s: START SERVER' % datetime.datetime.now())
    svr = RunServer(localconstants.SERVER_PORT, localconstants.ROOT_INDICES_PATH)

    try:
      while True:
        time.sleep(.2)
        if not svr.isAlive():
          print('NOT ALIVE')
          break
    finally:
      svr.killServer()

    time.sleep(1)


"""
Utility functions to help with dataloading and preprocessing
"""

import pandas as pd

import nltk
# pretrained tokenizer
nltk.download('punkt')
from nltk.tokenize import word_tokenize

# import stemmer
from nltk.stem import PorterStemmer

# remove stopwords
nltk.download('stopwords')
from nltk.corpus import stopwords

from gensim.models import Word2Vec
from gensim import corpora

import torch
from torch.nn import Embedding

import numpy as np

import re

MIN_WORD_COUNT = 4
START_TOKEN = "<BEG>"
END_TOKEN = "<END>"
UNKNOWN_TOKEN = "<UNK>"
PADDING_TOKEN = "<PAD>"

__author__ = "Sander Schulhoff"
__email__ = "sanderschulhoff@gmail.com"

def read_json(path):
    return pd.read_json(path)

def preprocess(df, binary_classes=True):
    """
    Tokenizes and encodes class numerically

    :param binary_classes: If True, considers YTA and ESH to be the same
        class (1) and NTA and NAH to be the same class (0)
    """

    ps = PorterStemmer()

    # Select columns to apply preprocessing to
    pp_cols = ["title", "post_body", "comment_body"]

    # make all lowercase
    df[pp_cols] = df[pp_cols].applymap(lambda s:s.lower())

    # comment only

    # Removing prefixed 'b'
    df["comment_body"] = df["comment_body"].apply(lambda s:re.sub(r'^b\s+', ' ',s,flags=re.I))

    # remove all of: x200b (zero-width space)
    df["comment_body"] = df["comment_body"].apply(lambda s:re.sub(r'x200b', ' ',s))

    # Substituting multiple spaces with single space
    df["comment_body"] = df["comment_body"].apply(lambda s:re.sub(r'\s+', ' ',s,flags=re.I))

    # post only

    # combine title and body of post
    df["post_body"] = df["post_body"].apply(lambda x: x + " ")
    df["post_body"] = df["title"] + df["post_body"]

    # get rid of now redundant title column
    df.drop(columns=["title"], axis=1, inplace=True)

    # apply to post only

    # Remove all the special characters
    df["post_body"] = df["post_body"].apply(lambda s:re.sub(r'\W', ' ',s))

    # remove all single characters
    df["post_body"] = df["post_body"].apply(lambda s:re.sub(r'\s+[a-zA-Z]\s+', ' ',s))

    # Remove single characters from the start (no space before them, but space after)
    df["post_body"] = df["post_body"].apply(lambda s:re.sub(r'^[a-zA-Z]\s+', ' ',s))

    # Removing prefixed 'b'
    df["post_body"] = df["post_body"].apply(lambda s:re.sub(r'^b\s+', ' ',s,flags=re.I))

    # remove all of: x200b (zero-width space)
    df["post_body"] = df["post_body"].apply(lambda s:re.sub(r'x200b', ' ',s))

    # Substituting multiple spaces with single space
    df["post_body"] = df["post_body"].apply(lambda s:re.sub(r'\s+', ' ',s,flags=re.I))

    # Remove single numbers
    df["post_body"] = df["post_body"].apply(lambda s:re.sub(r'\d+', ' ',s,flags=re.I))

    # tokenize df
    df[["post_body", "comment_body"]] = df[["post_body", "comment_body"]].applymap(lambda s:word_tokenize(s))

    # perform stemming on posts only
    df["post_body"] = df["post_body"].apply(lambda s: [ps.stem(word) for word in s])

     # remove stopwords
    # df["post_body"] = df["post_body"].apply(lambda s: [word for word in s if word not in stopwords.words('english')])

    if binary_classes:
        df["judgement"] = df["judgement"].map({"YTA":1, "ESH":1, "NTA":0, "NAH":0})
    else:
        raise "deal with this later"

    return df

def gen_vocabulary(df, min_count=MIN_WORD_COUNT, comment=True):
    """
    Generate a list of unique words in the corpus

    :param int min_count: The minimum # of occurences of word to be included in
        the list
    :param bool comment: If true, will additionally use text in comment bodies
    """
    vocabulary = {}
    # count how many of each word occur
    def dmap(arr):
        for word in arr:
            if word in vocabulary:
                vocabulary[word]+=1
            else:
                vocabulary[word]=1

    df["post_body"].apply(lambda arr: dmap(arr))

    if comment:
        df["comment_body"].apply(lambda arr: dmap(arr))

    # remove all keys which occur less than 3 times
    vocabulary = {key : count for key, count in vocabulary.items() if count >= min_count}

    vocabulary =  list(vocabulary)

    return vocabulary

"""
Functions which support the generation/saving/loading of embeddings.
"""

def train_embeddings(df, save_path=None, eps=1000):

    posts = list(df["post_body"])

    model = Word2Vec(sentences=posts, vector_size=30, epochs=eps, min_count=MIN_WORD_COUNT, window=2)

    if save_path:
        model.save(save_path)

    return model

def load_embeddings(save_path):
    """Abstract the loading process so don't need gensim calls in other files"""
    return Word2Vec.load(save_path)

def gensim_to_pytorch(gse):
    """
    Convert a gensim word2vec to a pytorch embedding
    :return: Tuple containing a word to index dictionary and the Pytorch embedding
    """
    word_to_idx = {word:index for index, word in enumerate(gse.wv.index_to_key)}
    weights = torch.FloatTensor([gse.wv.get_vector(word) for word in gse.wv.index_to_key])
    embed = Embedding.from_pretrained(weights)

    return word_to_idx, embed

def get_embeddings(path="embeddings_30_1000", special_tokens=True):
    gensim_embeds = load_embeddings(path)

    if special_tokens:
        gensim_embeds.wv[START_TOKEN] = np.random.normal(0, 1, size=(gensim_embeds.vector_size))
        gensim_embeds.wv[END_TOKEN] = np.random.normal(0, 1, size=(gensim_embeds.vector_size))
        gensim_embeds.wv[UNKNOWN_TOKEN] = np.random.normal(0, 1, size=(gensim_embeds.vector_size))

    return gensim_to_pytorch(gensim_embeds)

if __name__ == "__main__":
    df = read_json("example.json")

    df = preprocess(df)

    # gen_vocabulary(df, MIN_WORD_COUNT)

    train_embeddings(df, "embeddings_30_1000")


#Meant to be used in the raspberries
from crypt import methods
from urllib import response
from flask import Flask, request, jsonify
from serial import Serial
import serial.tools.list_ports as list_ports
from time import sleep
from pyduino.utils import bcolors
import logging
import re
import os
import socket

logging.basicConfig(filename='slave.log', filemode='w', level=logging.DEBUG)

STEP = 1/8.0
HEADER_DELAY = 5

class ReactorServer(Flask):
    """
    Slave of HTTP Server to Serial handler.
    """

    def __init__(self, serial_port = None, baudrate=9600,*args,**kwargs):
        logging.debug("Creating server.")
        super().__init__(*args,**kwargs)
        self.connected = False
        self.reactor_id = None
        self.port = serial_port
        self.baudrate = baudrate

        self.serial_connect()

        #Routes
        @self.route("/")
        def root():
            return "REACTOR SERVER", 200

        @self.route("/connect")
        def http_connect():
            self.connect()
            return "OK", 200

        @self.route("/reset")
        def http_reset():
            self.reset()
            return "OK", 200

        @self.route("/reboot")
        def reboot():
            os.system("sudo reboot")
            return "OK", 200

        @self.route("/send",methods=['POST'])
        def http_send():
            content = request.json
            logging.info(f"Received request: {content['command']}")
            if content['await_response']:
                response = self.send(content["command"],delay=content["delay"])
            else:
                response = self._send(content["command"])
            return jsonify({"response":response}),200

        @self.route("/ping")
        def ping():
            """
            Returns reactor id based on hostname. It is assumed that the id number is the last set of digits at the end of the hostname string.
            """
            # if not self.reactor_id:
            #     if not self.connected:
            #         self.connect()
            #     response = self.send("ping",delay=1)
            #     self.reactor_id = int(re.findall(r"\d+?",response)[0])
            digit_regex = r"(\d+)(?!.*\d)"
            hostname = socket.gethostname()
            digits = int(re.findall(digit_regex,hostname)[0])
            self.reactor_id = digits
            return jsonify({"id":self.reactor_id,"serial_port":self.port,"hostname":os.uname().nodename})

    def __delete__(self, _):
        self.serial.__del__()

    def serial_connect(self):
        if self.port is None:
            logging.debug("No serial port specified. Searching for available devices.")
            self.available_ports = list_ports.comports()
            self.available_ports = list(filter(lambda x: (x.vid,x.pid) in {(1027,24577),(9025,16),(6790,29987)},self.available_ports))
            self.port = self.available_ports[0].device
        self.serial = Serial(self.port, baudrate=self.baudrate, timeout=STEP)
        logging.info(f"Connected to serial port {self.serial}.")

    def connect(self,delay=STEP):
        """
        Connection begin.
        """
        sleep(HEADER_DELAY)
        self._recv(delay)
        self._send("quiet_connect")
        self.connected = True

    def reset(self):
        """
        Resets connection.
        """
        self.serial.flush()
        self.serial.close()
        self.serial.open()
        self.connected = True

    def close(self):
        """
        Interrompe conexão.
        """
        if self.serial.is_open:
            self.send("fim")
            self.serial.close()

    def send(self, msg, delay=0, recv_delay=STEP):
        """
        Envia mensagem para o reator e retorna resposta.

        Args:
            delay (int): Delay in seconds between sending and reading.
            recv_delay (int): Delay in seconds sent to recv.
        """
        if not self.connected:
            self.connect()
        self._send(msg)
        sleep(delay)
        return self._recv(recv_delay)

    def _send(self, msg):
        self.serial.write(msg.encode('ascii') + b'\n\r')

    def _recv(self,delay=STEP):
        out = []
        for _ in range(256):
            sleep(delay)
            new = self.serial.read_until()
            if new:
                new = new.decode('ascii').strip("\n").strip("\r")
                out.append(new)
            if out and not new:
                resp = ''.join(out)
                return resp

    def __repr__(self):
        return f"{bcolors.OKCYAN}<Reactor at {self.port}>{bcolors.ENDC}"

    def set(self, data=None, **kwargs):
        """
        Define o valor de todas as variáveis a partir de um dicionário.

        Exemplo:
            >>> reator.set({"440": 50, "brilho": 100})
        """
        data = {**(data or {}), **kwargs}
        args = ",".join(f'{k},{v}' for k, v in data.items())
        cmd = f"set({args})"
        self._send(cmd)

    def get(self, key=None):
        """
        Retorna o valor de uma ou mais variáveis.
        """
        if key is None:
            return self._get_all()
        if isinstance(key, str):
            key = [key]
        return

    def _get_all(self):
        resp = self.send("get")

if __name__=="__main__":
    rs = ReactorServer(import_name="Pyduino Slave Server")
    rs.run(port=5000,host="0.0.0.0")


from time import mktime, strptime
data = []
data1 = open('../data/SystemBLogs.txt').readlines()
data2 = open('../data/SystemCLogs.txt').readlines()
fmt = '%b %d %H:%M:%S %Y'
for line in data1+data2:
    # t is a struct_time tuple
    t = strptime(line[4:24], fmt)

    # calculate seconds since the epoch
    t_epoch = mktime(t)

    # append data
    data.append([t_epoch, line])

data = [line[1] for line in sorted(data)]
open('../data/SystemsBCLogs.txt', 'wt').writelines(data)


import sys
input = lambda: sys.stdin.readline().rstrip()

sys.setrecursionlimit(10**9)

def dfs(u,p,s,f):
    if f == 0:
        ans[0] = s
    else:
        ans[1] = max(ans[1], s)

    if f == 0 and len(graph[u]) > 2-int(u==r):
        f, s = 1, 0
    for v, w in graph[u]:
        if v == p: continue
        dfs(v,u,s+w,f)

n, r = map(int,input().split())
graph = [[] for _ in range(n+1)]

for _ in range(n-1):
    a,b,c = map(int,input().split())
    graph[a].append((b,c))
    graph[b].append((a,c))

ans = [0, 0]

dfs(r,r,0,0)

print(ans[0],ans[1])


# https://www.acmicpc.net/problem/4796

n = 1
result = 0

while True:
  l, p, v = map(int, input().split())

  if l == 0 and p == 0 and v == 0:
    break

  if l < v % p:
    result = (v // p) * l + l
  else:
    result = (v // p) * l + (v % p)

  print('Case ', n, ': ', result, sep='')
  n += 1

백준/그리디/캠핑.py


import plotly.graph_objects as go
import plotly.figure_factory as ff
import statistics as ss
import pandas as pd
import random

file1 = pd.read_csv("medium_data.csv")
data = file1["reading_time"].to_list()
population_mean = ss.mean(data)

def random_set_of_mean(counter):
    dataset = []
    for i in range(0, counter):
        random_index = random.randint(0, len(data))
        value = data[random_index]
        dataset.append(value)
    mean = ss.mean(dataset)
    return mean

def show_fig(mean_list):
    daf = mean_list
    fig = ff.create_distplot([daf], ["time"], show_hist = False)
    fig.show()

mean_list = []
for i in range(0, 100):
    set_of_mean = random_set_of_mean(30)
    mean_list.append(set_of_mean)
show_fig(mean_list)

stdev = ss.mean(mean_list)
mean = ss.mean(mean_list)

first_stdev_start, first_stdev_end = mean-stdev, mean+stdev
second_stdev_start, second_stdev_end = mean-(2*stdev), mean+(2*stdev)
third_stdev_start, third_stdev_end = mean-(3*stdev), mean+(3*stdev)
print("std1", first_stdev_start, first_stdev_end)
print("std2", second_stdev_start, second_stdev_end)
print("std3", third_stdev_start, third_stdev_end)

fig = ff.create_distplot([mean_list], ["time"], show_hist = False)
fig.add_trace(go.Scatter(x = [mean, mean], y = [0, 0.6], mode = "lines", name = "MEAN"))
fig.add_trace(go.Scatter(x = [first_stdev_start, first_stdev_start], y = [0, 0.6], mode = "lines", name = "STANDARD DEVIATION 1 START"))
fig.add_trace(go.Scatter(x = [first_stdev_end, first_stdev_end], y = [0, 0.6], mode = "lines", name = "STANDARD DEVIATION 1 END"))
fig.add_trace(go.Scatter(x = [second_stdev_start, second_stdev_start], y = [0, 0.6], mode = "lines", name = "STANDARD DEVIATION 2 START"))
fig.add_trace(go.Scatter(x = [second_stdev_end, second_stdev_end], y = [0, 0.6], mode = "lines", name = "STANDARD DEVIATION 2 END"))
fig.add_trace(go.Scatter(x = [third_stdev_start, third_stdev_start], y = [0, 0.6], mode = "lines", name = "STANDARD DEVIATION 3 START"))
fig.add_trace(go.Scatter(x = [third_stdev_end, third_stdev_end], y = [0, 0.6], mode = "lines", name = "STANDARD DEVIATION 3 END"))
fig.show()

df = pd.read_csv("sample_2.csv")
data2 = df["reading_time"].to_list()
mean_of_sample2 = ss.mean(data2)
print("Mean of sample 2:- ", mean_of_sample2)
fig = ff.create_distplot([mean_list], ["time"], show_hist = False)
fig.add_trace(go.Scatter(x = [mean, mean], y = [0, 0.6], mode = "lines", name = "SAMPLING MEAN"))
fig.add_trace(go.Scatter(x = [mean_of_sample2, mean_of_sample2], y = [0, 0.6], mode = "lines", name = "MEAN OF SAMPLE 2"))
fig.add_trace(go.Scatter(x = [first_stdev_end, first_stdev_end], y = [0, 0.6], mode = "lines", name = "STANDARD DEVIATION 1 END"))
fig.add_trace(go.Scatter(x = [second_stdev_end, second_stdev_end], y = [0, 0.6], mode = "lines", name = "STANDARD DEVIATION 2 END"))
fig.show()

z_score = (mean_of_sample2 - mean)/stdev
print("Z-score is ", z_score)


import time
import threading
import datetime

from numpy import place

from SystemFlg import SystemFlg
from PrivateWS import PrivateWS, PrivateWSData
from MarketData import MarketData
from Trade import Trade
from LineNotification import LineNotification

'''
Order削除のタイミング：
・leaves_qty=0となった時、order_status = Cancelledとなった時。get_orderで取得した情報を確認しないといけない？
理屈上は、cancelはBotの指示により行われるので正常にキャンセル完了した段階でcancelled_orderを呼び出せばBotAccountで処理できる。

Fee計算：
・約定更新の度にexec_feeを取得してorder_cum_exec_feeに記録。order削除するタイミングで反映。
Pl計算：
・holdingがありholding side != exec_dataのsideの時、exec priceを使って都度plを計算する。
'''
class BotAccount:
    def __init__(self, account_id) -> None:
        self.account_id = account_id
        self.__initialize_order()
        self.__initialize_holding()
        self.__initialize_performance()
        self.__initialize_log()

    def __initialize_order(self):
        self.__lock_order_data = threading.Lock()
        self.order_id = []
        self.order_side = {}
        self.order_price = {}
        self.order_qty = {}
        self.order_leaves_qty = {}
        self.order_type = {}
        self.order_dt= {}
        self.order_status = {} #New, Partially Filled
        self.order_cum_exec_fee = {}
        self.order_checked_exec_id = {} #{order_id:[checked_exec_ids]}

    def __initialize_holding(self):
        self.__lock_holding_data = threading.Lock()
        self.holding_side = ''
        self.holding_price = 0
        self.holding_size = 0
        self.holding_dt = ''
        self.holding_period = 0

    def __initialize_performance(self):
        self.__lock_performance_data = threading.Lock()
        self.total_pl = 0
        self.total_pl_ratio = 0
        self.unrealized_pl = 0
        self.realized_pl = 0
        self.total_fee = 0
        self.num_trade = 0 #holding positionが変わったタイミングをカウント
        self.num_win = 0
        self.num_buy = 0
        self.num_sell = 0
        self.win_rate = 0
        self.num_maker_order = 0 #現状正確な把握が難しい

    def __initialize_log(self):
        self.__lock_log_data = threading.Lock()
        self.__tmp_trade_log = []
        self.trade_log = {}
        self.total_pl_log = []
        self.total_pl_ratio_log = []
        self.unrealized_pl_log = []
        self.realized_pl_log = []
        self.total_fee_log = []

    def __remove_order_data(self, oid):
        with self.__lock_order_data:
            self.order_id.remove(oid)
            del self.order_checked_exec_id[oid]
            del self.order_cum_exec_fee[oid]
            del self.order_dt[oid]
            del self.order_leaves_qty[oid]
            del self.order_price[oid]
            del self.order_qty[oid]
            del self.order_status[oid]
            del self.order_type[oid]
            del self.order_side[oid]

    def get_order_data(self, oid):
        with self.__lock_order_data:
            if oid in self.order_id:
                return {'id':oid, 'dt':self.order_dt[oid] ,'side':self.order_side[oid], 'price':self.order_price[oid], 'qty':self.order_qty[oid]
                ,'leaves_qty':self.order_leaves_qty[oid], 'type':self.order_type[oid], 'status':self.order_status[oid], 'fee':self.order_cum_exec_fee[oid]}
            else:
                print('BotAccount.get_order_data: Unknown order id !', oid)
                LineNotification.send_message('BotAccount.get_order_data: Unknown order id ! - ' +oid)
                return {'id':'', 'dt':'' ,'side':'', 'price':0, 'qty':0,'leaves_qty':0, 'type':'', 'status':'', 'fee':0}

    def get_last_order_data(self):
        with self.__lock_order_data:
            if len(self.order_id) > 0:
                oid = self.order_id[-1]
                return {'id':oid, 'dt':self.order_dt[oid] ,'side':self.order_side[oid], 'price':self.order_price[oid], 'qty':self.order_qty[oid]
                ,'leaves_qty':self.order_leaves_qty[oid], 'type':self.order_type[oid], 'status':self.order_status[oid], 'fee':self.order_cum_exec_fee[oid]}
            else:
                return {'id':'', 'dt':'' ,'side':'', 'price':0, 'qty':0,'leaves_qty':0, 'type':'', 'status':'', 'fee':0}

    def get_holding_data(self):
        with self.__lock_holding_data:
            return {'dt':self.holding_dt, 'side':self.holding_side, 'size':self.holding_size, 'price':self.holding_price, 'period':self.holding_period}

    def get_performance_data(self):
        with self.__lock_performance_data:
            return {'total_pl':self.total_pl, 'total_pl_ratio':self.total_pl_ratio, 'unrealized_pl':self.unrealized_pl, 'total_fee':self.total_fee, 'num_trade':self.num_trade,
            'num_win':self.num_win, 'num_buy':self.num_buy, 'num_sell':self.num_sell, 'win_rate':self.win_rate, 'num_maker_order':self.num_maker_order}

    def entry_order(self, id, side, price, qty, type, message):
        with self.__lock_order_data:
            self.order_id.append(id)
            self.order_side[id] = str(side).lower()
            self.order_price[id] = round(float(price),1) if str(type).lower() == 'limit' else 0 #market orderのAPI response priceは板の上下限値になる
            self.order_qty[id] = int(qty)
            self.order_checked_exec_id[id] = []
            self.order_cum_exec_fee[id] = 0
            self.order_type[id] = str(type).lower()
            self.order_dt[id] = datetime.datetime.now()
            self.order_leaves_qty[id] = int(qty)
            self.order_status[id] = 'New'
            print('#', self.account_id, ' - ', message, type, side, ' @', price, ' x', qty)
            LineNotification.send_message('#' + str(self.account_id) + ' - ' + message+ type + ' ' + side+' @'+ str(price)+ ' x' + str(qty))
            self.__tmp_trade_log.append('#' + str(self.account_id) + ' - ' + message+ side+' @'+ str(price)+ ' x' + str(qty))

    def cancel_order(self, oid):
        self.__remove_order_data(oid)
        self.__tmp_trade_log.append('#'+ str(self.account_id) + ' - cancelled order.')
        print('#',self.account_id, ' - cancelled order.')
        LineNotification.send_message('#'+ str(self.account_id) + ' - cancelled order.')

    def update_order_price(self, oid, new_price):
        with self.__lock_order_data:
            if oid in self.order_id:
                self.__tmp_trade_log.append('#' + str(self.account_id) + ' - updated order price from ' + str(self.order_price[oid]) + ' to ' + str(new_price))
                print('#',self.account_id, ' - updated order price from', self.order_price[oid], ' to ', new_price)
                LineNotification.send_message('#' + str(self.account_id) + ' - updated order price from ' + str(self.order_price[oid]) + ' to ' + str(new_price))
                self.order_price[oid] = round(float(new_price),1)
            else:
                self.__tmp_trade_log.append('#' + str(self.account_id) + ' - updated order price but order_id is not existed in BotAccount !')
                print('#',self.account_id, ' - updated order price but order_id is not existed in BotAccount !')
                LineNotification.send_message('#' + str(self.account_id) + ' - updated order price but order_id is not existed in BotAccount !')

    def update_order_amount(self, oid, new_amount):
        with self.__lock_order_data:
            if oid in self.order_id:
                self.__tmp_trade_log.append('#' + str(self.account_id) + ' - updated order amount from ' + str(self.order_qty[oid]) + ' to ' + str(new_amount))
                print('#',self.account_id, ' - updated order amount from', self.order_qty[oid], ' to ', new_amount)
                LineNotification.send_message('#' + str(self.account_id) + ' - updated order amount from ' + str(self.order_qty[oid]) + ' to ' + str(new_amount))
                self.order_leaves_qty[oid] += int(new_amount) - int(self.order_qty[oid])
                self.order_qty[oid] = int(new_amount)
            else:
                self.__tmp_trade_log.append('#' + str(self.account_id) + ' - updated order price but order_id is not existed in BotAccount !')
                print('#',self.account_id, ' - updated order price but order_id is not existed in BotAccount !')
                LineNotification.send_message('#' + str(self.account_id) + ' - updated order price but order_id is not existed in BotAccount !')

    '''
    new entry, additional entry, opposite entryを識別して適切にholding dataを更新
    '''
    def __update_holding_data_execution(self, side, executed_price, executed_qty, pl):
        with self.__lock_holding_data:
            if self.holding_side == '': #new entry
                self.holding_side = str(side).lower()
                self.holding_price = executed_price
                self.holding_period = 0
                self.holding_size = int(executed_qty)
                self.__num_trade_counter(side, pl)
            elif self.holding_side == side: #additional entry
                self.holding_price = (self.holding_price * self.holding_size + executed_price * executed_qty) / (self.holding_size + executed_qty)
                self.holding_size += int(executed_qty)
            else: #exit execution
                if self.holding_size == executed_qty: #exited all position
                    self.__num_trade_counter(side, pl)
                    self.__initialize_holding()
                else: #exit and opposite entry
                    self.holding_side = str(side).lower()
                    self.holding_size = int(executed_qty) - self.holding_size
                    self.holding_price = executed_price
                    self.holding_dt = datetime.datetime.now()
                    self.holding_period =0
                    self.__num_trade_counter(side, pl)

    def __num_trade_counter(self, side, pl):
        self.num_trade += 1
        self.num_win += 1 if pl >0 else 0
        if side == 'buy':
            self.num_buy += 1
        else:
            self.num_sell += 1

    '''

    '''
    def __update_order_data_execution(self, oid, executed_qty, leaves_qty, executed_fee, execution_id):
        if self.order_leaves_qty[oid] < executed_qty: #Partially Executed
            with self.__lock_order_data:
                self.order_leaves_qty[oid] = int(leaves_qty)
                self.order_cum_exec_fee[oid] += executed_fee
                self.order_checked_exec_id[oid].append(execution_id)
                self.order_status[oid] = 'Partially Filled'
        elif self.order_leaves_qty[oid] == executed_qty: #Fully executed
            self.__remove_order_data(oid)
        else:
            print('BotAccount-', self.account_id, ': Larger qty was executed !')
            LineNotification.send_message('BotAccount-'+ str(self.account_id) +  ': Larger qty was executed !')
            self.__remove_order_data(oid)

    def __add_log(self, i):
        with self.__lock_log_data:
            self.trade_log[i] = self.__tmp_trade_log
            self.total_pl_log.append(self.total_pl)
            self.total_pl_ratio_log.append(self.total_pl_ratio)
            self.unrealized_pl_log.append(self.unrealized_pl)
            self.realized_pl_log.append(self.realized_pl)
            self.total_fee_log.append(self.total_fee)
            self.__tmp_trade_log = []

    def move_to_next(self, dt, i, ohlc_df, order_size):
        if self.holding_side != '':
            self.unrealized_pl = self.holding_size * (self.holding_price - ohlc_df['close'].iloc[-1]) / self.holding_price if self.holding_side == 'buy' else self.holding_size * (self.holding_price - ohlc_df['close'].iloc[-1]) / self.holding_price
        else:
            self.unrealized_pl = 0
        self.total_pl = self.unrealized_pl + self.realized_pl - self.total_fee
        self.total_pl_ratio = round(self.total_pl / order_size, 6)
        if self.num_trade > 0:
            self.win_rate = round(self.num_win / self.num_trade, 4)
        else:
            self.win_rate = 0
        self.num_maker_order = 0
        self.holding_period += 1
        self.__add_log(i)

    '''
    botから与えられるTrade.get_executions()で取得できる直近の約定データにorder idが含まれており、未処理のexec_idか否かを確認する。
    '''
    def check_execution(self, exec_list):
        for d in exec_list:
            if len(self.order_id) == 0:
                break
            if d['info']['order_id'] in self.order_id: #該当するorder idを保有中
                if d['info']['exec_id'] not in self.order_checked_exec_id[d['info']['order_id']]: #約定idを未確認の時
                    self.__process_execution(d)

    '''
    約定の度に都度fee, pl, order, holdingを更新。

    前提条件：
    ・未確認のexec_idが確認済でorder_idを保有中
    '''
    def __process_execution(self, exec_dict):
        oid = exec_dict['info']['order_id']
        if int(exec_dict['info']['leaves_qty']) != self.order_leaves_qty[oid]:
            od = self.get_order_data(oid)
            executed_qty = int(od['leaves_qty']) - int(exec_dict['info']['leaves_qty'])
            executed_fee = float(exec_dict['info']['exec_fee']) * float(exec_dict['info']['exec_price'])
            fee = self.__calc_fee(float(exec_dict['info']['exec_fee']), float(exec_dict['info']['exec_price']))
            pl = self.__calc_realized_pl(exec_dict['info']['side'], float(exec_dict['info']['exec_price']), int(exec_dict['info']['exec_qty']))
            self.__update_order_data_execution(oid, int(exec_dict['info']['exec_qty']), int(exec_dict['info']['leaves_qty']), fee, exec_dict['info']['exec_id'])
            self.__update_holding_data_execution(exec_dict['info']['side'], float(exec_dict['info']['exec_price']), int(exec_dict['info']['exec_qty']), pl)
            print('BotAccount-', self.account_id, ': Executed ', exec_dict['type'], '-', exec_dict['info']['side'], ' order ', '@', float(exec_dict['info']['exec_price']), ' x', int(exec_dict['info']['exec_qty']), ' pl=', pl)
            LineNotification.send_message('BotAccount-' + str(self.account_id) + ': Executed ' + exec_dict['type']+ '-'+ exec_dict['info']['side']+ ' order '+ '@'+ str(float(exec_dict['info']['exec_price']))+ ' x'+ str(int(exec_dict['info']['exec_qty']))+ ' pl='+ str(pl))
        else:
            print('BotAccount-',self.account_id, ':leaves_qty is not matched !')
            print('execution data:')
            print(exec_dict)
            print('order data:')
            print(self.get_order_data(oid))
            LineNotification.send_message('BotAccount-' + str(self.account_id) + ':leaves_qty is not matched !')

    def __calc_fee(self, exec_fee, exec_price):
        fee = exec_fee * exec_price
        with self.__lock_performance_data:
            self.total_fee += fee
        return fee

    def __calc_realized_pl(self, exec_side, exec_price, exec_qty):
        if self.holding_side != '' and self.holding_side != exec_side:
            pl = exec_qty *  (exec_price - self.holding.price if self.holding_side == 'buy' else self.holding.price - exec_price)
            with self.__lock_performance_data:
                self.realized_pl += pl
            return pl
        else:
            return 0


__author__ = 'shaleen'
import os, sys
sys.path.append(os.path.dirname(os.getcwd()))
import warnings
warnings.filterwarnings("ignore")
from integration import dbutils
from supportsetgenerator import Generator
import QueryLister
import re
import pickle
from matplotlib import rc_file
rc_file('../experiments/matplotlibrc-singlecolumn')
import matplotlib.pyplot as plt
import numpy as np
import math
import matplotlib as mpl

mpl.rcParams['figure.figsize'] = 3.5, 1.95

class Combiner:

    g = Generator.Generator()
    q = QueryLister.Query()
    query = 'select Code, ID from Country;'
    dbutils.DBUtils.cursor.execute(query)
    res = dbutils.DBUtils.cursor.fetchall()
    dictcodeid = {}
    for i in range(0, len(res)):
        dictcodeid[res[i][0]] = res[i][1]
    try:
        os.remove('benchmarkselectsupportsize.pdf')
    except OSError:
        pass
    def Query1(self, u, size):
        with open('supportset'+str(size)+'.txt', 'rb') as f:
            support_set = pickle.load(f)
        support_set_value = support_set[3]
        support_set_undo_value = support_set[2]
        support_set_undo = support_set[1]
        support_set = support_set[0]
        disagreement = 0
        for i in range(0, len(support_set)):
            ele_1 = support_set[i]
            ele_undo_1 = support_set_undo[i]
            ele_undo_1_value = support_set_undo_value[i]
            ele_1_value = support_set_value[i]
            if self.willOutputChangeQuery1(ele_1, ele_undo_1, ele_undo_1_value, ele_1_value, u) == False:
                None
            else:
                disagreement += 1
        return float(disagreement)*100/size

    def willOutputChangeQuery1(self, ele_1, ele_undo_1, ele_undo_1_value, ele_1_value, u):
        code_changed = re.search(r"Code = \'(.*)\'", ele_1[0]).group(1)
        if self.dictcodeid[code_changed] <= u:
            return True
        else:
            return False

    def plotgraph(self):
        mpl.rcParams['figure.figsize'] = 2.8, 1.95
        fig, ax = plt.subplots()
        ax.set_ylim([0, 105])
        ax.set_xlim([1, 239])
        #plt.gca().yaxis.grid(which='major', linestyle='--', linewidth=0.3)
        ax.yaxis.set_ticks(np.arange(0, 105, 20))
        #ax.yaxis.set_ticks(np.arange(0, 100, 2), minor=True)
        ax.xaxis.set_ticks([1,32,64,128,239])
        ax.grid(which='minor', alpha=0.2)
        ax.grid(which='major', alpha=0.5)
        plt.gca().xaxis.grid(which='major', linestyle='-', linewidth=0.5, alpha=0.2)
        ax.tick_params(
                axis='x',          # changes apply to the x-axis
                which='major',      # both major and minor ticks are affected
                bottom='on',      # ticks along the bottom edge are off
                top='off',         # ticks along the top edge are off
                direction='out',
                labelbottom='on') # labels along the bottom edge are off

        ax.tick_params(
                axis='y',
                which='major',
                left='on',
                right='off',
                direction='out')
        idealsel = [0, 100/239,300/239,700/239,1500/239,3100/239,6300/239,12700/239, 100]
        disagsel10 = [self.Query1(1,10),self.Query1(2,10),self.Query1(4,10),self.Query1(8,10),self.Query1(16,10),
                      self.Query1(32,10),self.Query1(64,10),self.Query1(128,10),self.Query1(239,10)]
        disagsel100 = [self.Query1(1,100),self.Query1(2,100),self.Query1(4,100),self.Query1(8,100),self.Query1(16,100),
                      self.Query1(32,100),self.Query1(64,100),self.Query1(128,100),self.Query1(239,100)]
        disagsel1000 = [self.Query1(1,1000),self.Query1(2,1000),self.Query1(4,1000),self.Query1(8,1000),self.Query1(16,1000),
                      self.Query1(32,1000),self.Query1(64,1000),self.Query1(128,1000),self.Query1(239,1000)]
        a = [math.pow(2,x) for x in range(0,8)]
        a.append(239)
        ax.plot(a, disagsel10, color='b', marker='x', markersize=2)
        ax.plot(a, disagsel100, color='r', marker='s', markersize=2)
        ax.plot(a, disagsel1000, color='g', marker='8', markersize=2)
        ax.plot(a, idealsel, color='m', marker='v', markersize=2)
        plt.ylabel("Price")
        plt.grid(True)
        plt.xlabel("$u$ parameter in $Q^{\sigma}_u$")
        lgd = plt.legend(['10','100','1000', 'Ideal price'], loc='lower right', ncol=1, bbox_to_anchor=(1, 0),prop={'size':6})
        plt.savefig('benchmarkselectsupportsize.pdf', bbox_extra_artists=(lgd,), bbox_inches='tight')

if __name__ == "__main__":
    c = Combiner()
    c.plotgraph()


from A.B import *
S(17, 'azder', 'sample_')

def combinations(array, r):
    for i in range(len(array)):
        if r == 1: # 종료 조건
            yield [array[i]]
        else:
            for next in combinations(array[i+1:], r-1):
                yield [array[i]] + next

T = int(input())
for test_case in range(1, T + 1):
    n = int(input())
    board = [list(map(int, input().split())) for _ in range(n)]
    ans = 10e999
    for case in combinations(range(n),n//2):
        food1 = 0
        for i in case:
            for j in case:
                food1 += board[i][j]
        food2 = 0
        case2 = set(range(n)) - set(case)
        for i in case2:
            for j in case2:
                food2 += board[i][j]
        ans = min(ans, abs(food1 - food2))

    print(f'#{test_case} {ans}')

E(17, 'azder', 'sample_')


# (c) @UniBorg and Lucaco FF apresenta
# Original written by @UniBorg edit by @aidetico

from telethon import events
import asyncio
from collections import deque

@borg.on(events.NewMessage(pattern=r"\.lucaco", outgoing=True))
async def _(event):
	if event.fwd_from:
		return
	deq = deque(list("Lucaco Ehi Claro Rodando Free Fire!"))
	for _ in range(48):
		await asyncio.sleep(0.1)
		await event.edit("".join(deq))
		deq.rotate(1)


#encoding=utf-8
from __future__ import print_function, unicode_literals
import sys
sys.path.append("../")
import jieba
jieba.load_userdict("userdict.txt")
import jieba.posseg as pseg

import json
with open('comments.json','r', encoding='utf-8') as fi:
    word = fi.read()
    data = json.loads(word)
test_sent=''
for i in data:
    test_sent += i["评论"]
words = jieba.cut(test_sent)
print('\n'.join(words))
print("="*40)
result = pseg.cut(test_sent)
with open('splitting.txt', 'a', encoding='UTF-8') as f:  # 将数据存入txt
    for w in result:
        f.write(w.word+ '\n')