""" Module to parse DocumentRevisions LibraryStatus """
import codecs
import logging
import os
import plistlib
from riplib.plugin import Plugin

__author__ = 'osxripper'
__version__ = '0.1'
__license__ = 'GPLv3'

class LibraryStatus(Plugin):
    """
    Plugin to parse /.DocumentRevisions-V100/LibraryStatus
    """
    def __init__(self):
        """
        Initialise the class.
        """
        super().__init__()
        self.set_name("Document Revisions Library Status")
        self.set_description("Parse data from /.DocumentRevisions-V100/LibraryStatus")
        self.set_data_file("LibraryStatus")
        self.set_output_file("DocumentRevisions.txt")
        self.set_type("plist")

    def parse(self):
        """
        Parse /.DocumentRevisions-V100/LibraryStatus
        """
        with codecs.open(os.path.join(self._output_dir, self._output_file), "a", encoding="utf-8") as output_file:
            output_file.write("="*10 + " " + self._name + " " + "="*10 + "\r\n")
            if self._os_version in ["big_sur", "catalina"]:
                logging.info("This version of OSX is not supported by this plugin.")
                print("[INFO] This version of OSX is not supported by this plugin.")
                output_file.write("[INFO] This version of OSX is not supported by this plugin.\r\n")
            elif self._os_version in ["mojave", "high_sierra", "sierra", "el_capitan", "yosemite"]:
                plist_file = os.path.join(self._input_dir, ".DocumentRevisions-V100", self._data_file)
                output_file.write("Source File: {0}\r\n\r\n".format(plist_file))
                if os.path.isfile(plist_file):
                    with open(plist_file, "rb") as plist_to_load:
                        plist = plistlib.load(plist_to_load)
                    try:
                        if "databaseStateIsTrustable" in plist:
                            output_file.write("Database State Is Trustable: {0}\r\n".format(plist["databaseStateIsTrustable"]))
                    except KeyError:
                        pass
                else:
                    logging.warning("File: %s does not exist or cannot be found.\r\n", plist_file)
                    output_file.write("[WARNING] File: {0} does not exist or cannot be found.\r\n".format(plist_file))
                    print("[WARNING] File: {0} does not exist or cannot be found.".format(plist_file))
            elif self._os_version in ["mavericks", "mountain_lion", "lion", "snow_leopard"]:
                logging.info("This version of OSX is not supported by this plugin.")
                print("[INFO] This version of OSX is not supported by this plugin.")
                output_file.write("[INFO] This version of OSX is not supported by this plugin.\r\n")
            else:
                logging.warning("Not a known OSX version.")
                print("[WARNING] Not a known OSX version.")
            output_file.write("="*40 + "\r\n\r\n")
        output_file.close()


import numpy as np                                # For matrix operations and numerical processing
import pandas as pd                               # For munging tabular data
import sklearn as sk                              # For access to a variety of machine learning models
import matplotlib.pyplot as plt                   # For charts and visualizations
from IPython.display import Image                 # For displaying images in the notebook
from IPython.display import display               # For displaying outputs in the notebook
from sklearn.datasets import dump_svmlight_file   # For outputting data to libsvm format for xgboost
from time import gmtime, strftime                 # For labeling SageMaker models, endpoints, etc.

from sklearn.model_selection import train_test_split
from sklearn import datasets
from sklearn import svm
from datetime import datetime
import datetime as dt
from sklearn.tree import DecisionTreeClassifier, DecisionTreeRegressor
from sklearn.ensemble import RandomForestClassifier, RandomForestRegressor
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import r2_score
from sklearn.metrics import mean_squared_error
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix
from sklearn.metrics import classification_report
from time import gmtime, strftime
from sklearn.externals.six import StringIO
from IPython.display import Image
from sklearn.tree import export_graphviz
import time
from Preprocessing import get_category
import seaborn as sn

from TestMQTT import setup
from AWSIoTPythonSDK.MQTTLib import AWSIoTMQTTClient
import logging
import time
import argparse
import json
from math import sqrt

topic = 'project/lutron'

#import anaconda_project
#!pip install pydotplus

df = pd.read_csv('/home/ec2-user/SageMaker/IoTAnalytics/Clean_Data.csv')
Output_lookup = pd.read_csv('/home/ec2-user/SageMaker/lookup Tables - Output.csv')

# Function for random forest to predict room category

def random_forest_cat(X_train,X_test,y_train,y_test):
    random_forest = RandomForestClassifier(n_estimators=50, oob_score=True)
    random_forest.fit(X_train, y_train)

    Y_train_pred = random_forest.predict(X_train)
    Y_test_pred = random_forest.predict(X_test)

    random_forest.score(X_train, y_train)
    acc_random_forest = round(random_forest.score(X_train, y_train) * 100, 2)

    predicted = random_forest.predict(X_test)
    accuracy = accuracy_score(y_test, predicted)
    #print(f'Out-of-bag score estimate: {random_forest.oob_score_:.3}')
    #print(f'Mean accuracy score: {accuracy:.3}')
    #print(accuracy)

    report = classification_report(y_test, predicted)
    #print(report)
    matrix = confusion_matrix(y_test, predicted)
    #print(matrix)
    #sn.heatmap(matrix)
    return random_forest

def lutron_input(room,brightness):

    brightness_prediction = str(round(brightness))
    room_prediction = int(room)
    Output_Room = Output_lookup[(Output_lookup.RoomLabel ==room_prediction)]
    brightness_prediction= brightness_prediction.replace('.0', '')
    Output_Room['Output_w_Brightness'] = Output_Room['DL Zone Input'].astype(str)+ brightness_prediction
    string_list = Output_Room['Output_w_Brightness'].values.tolist()
    Output_string = '\r'.join(string_list)
    print(Output_string)
    return Output_string

# Predict Room

#predict Room top features
X1 = df.iloc[:,[9,10,13,15,17,18,20]]
Y1 = df.iloc[:,19]

#Run simulation
X1_train, X1_test, y1_train, y1_test= train_test_split(X1, Y1, test_size=0.3, shuffle=True)
room_forest = random_forest_cat(X1_train,X1_test,y1_train,y1_test)

#Predict Brightness

X = df.iloc[:,[9,10,13,15,17,18,19,20]]
Y = df.iloc[:,5]

# based on feature extraction tidy up data
X_train, X_test, y_train, y_test= train_test_split(X, Y, test_size=0.3, shuffle=True)
brightness_forest = random_forest_cat(X_train,X_test,y_train,y_test)

# Real Time DataFrame

sunrise = pd.read_csv('/home/ec2-user/SageMaker/Sunrise - Sheet1.csv')
sunrise['time0'] = pd.to_datetime(sunrise['time0'],format ='%m/%d/%Y')

# Defining Day _Label
Day_Label = {'Category': 'Day_Label','Morning': 0,'Day': 1,'Evening': 2, 'Night':3}
Day_Label = {'Category' : pd.Series(['Morning', 'Day', 'Evening','Night'], index=[0, 1, 2, 3]),'Day_Label' : pd.Series([0, 1, 2,3], index=[0, 1, 2, 3])}
Day_Label_df = pd.DataFrame(Day_Label)

MyMQTTClient = setup()
MyMQTTClient
room_forest = random_forest_cat(X1_train,X1_test,y1_train,y1_test)
brightness_forest = random_forest_cat(X_train,X_test,y_train,y_test)

Floor = 0
loopcount =0
while loopcount <10:
    # Define now
    now_time= datetime.now()

    # Get Num_Week and Weekday
    Num_Week =now_time.isocalendar()[1]
    weekday = now_time.weekday()

    # Get string time
    string_times = strftime("%Y-%m-%d %H:%M:%S", gmtime())
    date, times =string_times.split(' ')
    hour,minute,second = times.split(':')

    # Get Total minutes since Midnight
    minutes = (int(hour))*60 + int(minute)

    # Get Temp & Cloud Amount !!!!!!!!!

    # Define input data in DataFrame
    data = {'DateTime':string_times,'Num_Week' : Num_Week, 'weekday' : weekday,'Minute': minutes,'Floor': Floor,'temp':20,'cloud_amt':5 }
    predict_df = pd.DataFrame(data,index=[1])

    # Format DateTime to get sunrise & sunset times
    predict_df = predict_df.join(predict_df['DateTime'].str.split(' ', expand=True).add_prefix('time'))
    predict_df['time0'] = pd.to_datetime(predict_df['time0'],format ='%Y-%m-%d')

    predict_df = predict_df.merge(sunrise,on='time0',how="left")

    # Get Category of Day/Night/Morning/Evning
    predict_df = predict_df.join(predict_df['Sunrise'].str.split(':', expand=True).add_prefix('Sunrise'))
    predict_df = predict_df.join(predict_df['Sunset'].str.split(':', expand=True).add_prefix('Sunset'))

    predict_df['Sunrise0'] = predict_df['Sunrise0'].apply(pd.to_numeric, errors='coerce')
    predict_df['Sunrise1'] = predict_df['Sunrise1'].apply(pd.to_numeric, errors='coerce')
    predict_df['Sunset0'] = predict_df['Sunset0'].apply(pd.to_numeric, errors='coerce')
    predict_df['Sunset1'] = predict_df['Sunset0'].apply(pd.to_numeric, errors='coerce')

    predict_df['Sunrise_Sec'] = predict_df['Sunrise0']*60 + predict_df['Sunrise1']
    predict_df['Sunset_Sec'] = predict_df['Sunset0']*60 + predict_df['Sunset1']

    predict_df = predict_df.drop(['Sunrise','Sunset','Sunrise0','Sunrise1','Sunset0','Sunset1','Sunrise2','Sunset2'],axis = 1)

    predict_df['Category'] = predict_df.apply(get_category, axis = 1)
    predict_df = predict_df.merge(Day_Label_df,on='Category',how="left")
    predict_df = predict_df.drop(['Category','Sunrise_Sec','Sunset_Sec','time0','time1','DateTime'], axis=1)
    print(predict_df)

    real_time_room = room_forest.predict(predict_df)
    print (real_time_room)
    predict_df['RoomLabel'] = real_time_room
    real_time_brightness = brightness_forest.predict(predict_df)
    print (real_time_brightness)

    DL_string = lutron_input(real_time_room[0],real_time_brightness[0])
    my_list = DL_string.split('\r')
    message = {}

    for x in my_list:
        if (len(x)>0):
            message['message'] = x
            message['sequence'] = loopcount
            message['timestamp'] = string_times
            message['unique_key'] = string_times + str(loopcount)
            message['Source'] = "Model"

            messageJson = json.dumps(message)
            MyMQTTClient.publish(topic, messageJson, 1)
            print('Published topic %s: %s\n' % (topic, messageJson))
            loopcount=loopcount+1
    if Floor == 0:
        print(1)
        Floor =1
    elif Floor ==1:
        Floor = 0
    time.sleep(60)


from __future__ import annotations

from unittest import mock
from unittest.mock import MagicMock

import pytest

from airflow.providers.amazon.aws.hooks.lambda_function import LambdaHook

FUNCTION_NAME = "test_function"
PAYLOAD = '{"hello": "airflow"}'
BYTES_PAYLOAD = b'{"hello": "airflow"}'
RUNTIME = "python3.9"
ROLE = "role"
HANDLER = "handler"
CODE = {}

class LambdaHookForTests(LambdaHook):
    conn = MagicMock(name="conn")

@pytest.fixture
def hook():
    return LambdaHookForTests()

class TestLambdaHook:
    def test_get_conn_returns_a_boto3_connection(self, hook):
        assert hook.conn is not None

    @mock.patch(
        "airflow.providers.amazon.aws.hooks.lambda_function.LambdaHook.conn", new_callable=mock.PropertyMock
    )
    @pytest.mark.parametrize(
        "payload, invoke_payload",
        [(PAYLOAD, BYTES_PAYLOAD), (BYTES_PAYLOAD, BYTES_PAYLOAD)],
    )
    def test_invoke_lambda(self, mock_conn, payload, invoke_payload):
        hook = LambdaHook()
        hook.invoke_lambda(function_name=FUNCTION_NAME, payload=payload)

        mock_conn().invoke.assert_called_once_with(
            FunctionName=FUNCTION_NAME,
            Payload=invoke_payload,
        )

    @pytest.mark.parametrize(
        "hook_params, boto3_params",
        [
            pytest.param(
                {
                    "function_name": FUNCTION_NAME,
                    "runtime": RUNTIME,
                    "role": ROLE,
                    "handler": HANDLER,
                    "code": CODE,
                    "package_type": "Zip",
                },
                {
                    "FunctionName": FUNCTION_NAME,
                    "Runtime": RUNTIME,
                    "Role": ROLE,
                    "Handler": HANDLER,
                    "Code": CODE,
                    "PackageType": "Zip",
                },
                id="'Zip' as 'package_type'",
            ),
            pytest.param(
                {
                    "function_name": FUNCTION_NAME,
                    "role": ROLE,
                    "code": CODE,
                    "package_type": "Image",
                },
                {
                    "FunctionName": FUNCTION_NAME,
                    "Role": ROLE,
                    "Code": CODE,
                    "PackageType": "Image",
                },
                id="'Image' as 'package_type'",
            ),
        ],
    )
    def test_create_lambda(self, hook_params, boto3_params, hook):
        hook.conn.create_function.reset_mock()
        hook.conn.create_function.return_value = {}
        hook.create_lambda(**hook_params)

        hook.conn.create_function.assert_called_once_with(**boto3_params)

    @pytest.mark.parametrize(
        "params",
        [
            pytest.param(
                {
                    "handler": HANDLER,
                },
                id="'runtime' not provided",
            ),
            pytest.param(
                {
                    "runtime": RUNTIME,
                },
                id="'handler' not provided",
            ),
        ],
    )
    def test_create_lambda_with_zip_package_type_and_missing_args(self, params, hook):
        hook.conn.create_function.return_value = {}

        with pytest.raises(TypeError):
            hook.create_lambda(
                function_name=FUNCTION_NAME,
                role=ROLE,
                code=CODE,
                package_type="Zip",
                **params,
            )


import requests
import os
import json
import twstock

d = twstock.codes

h = {}

for k in d:
    info = d[k]
    if info.type != "股票": continue
    #print (k,info.name,info.type)
    h[k] = info.name

s = open("stock.json").read()
j = json.loads(s)

for k in j:
    #print (k,j[k])
    h[k] = j[k]

i = 0

for k in h:
    i += 1
    print(i, len(h))
    print(k, h[k])
    print("")
    if os.path.isfile("wantgoo/%s.json" % k):
        print('aaaa')
        continue
    #r=requests.post("http://pchome.megatime.com.tw/stock/sto0/ock3/sid%s.html"%k,data={"is_check":"1"})
    #r.encoding="utf8"
    while True:
        s = os.popen(
            "curl 'https://api.wantgoo.com/%E8%A1%8C%E5%8B%95%E7%89%88%E6%9C%AC_%E8%82%A1%E7%A5%A8/%E5%80%8B%E8%82%A1%E7%B7%9A%E5%9C%96/%E5%8D%B3%E6%99%82%E7%B7%9A%E5%9C%96%E8%B3%87%E6%96%99?StockNo="
            + k +
            "&type=2&days=5' -H 'Origin: https://www.wantgoo.com' -H 'Accept-Encoding: gzip, deflate, br' -H 'Accept-Language: en-US,en;q=0.9,zh-TW;q=0.8,zh;q=0.7' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.117 Safari/537.36' -H 'Accept: */*' -H 'Referer: https://www.wantgoo.com/stock/00637l' -H 'Connection: keep-alive' --compressed"
        ).read()
        if len(s) > 0: break
    f = open("wantgoo/%s.json" % k, "w")
    f.write(s)
    f.close()


import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np

from numpy.core.numeric import NaN
from numpy.lib.function_base import rot90
import pandas as pd
import sys
import os
import ast

import copy as cp
import numpy as np
from pandas.core import series

# filename = 'Jun-20-2021  19:13:01/__Jun-20-2021  19:13:01'
filename = 'Jun-20-2021  20:09:54/__Jun-20-2021  20:09:54'

df = pd.read_csv(filename + '.csv')
df_events = pd.read_csv(filename + '-events.csv')
df_missions = pd.read_csv(filename + '-missions.csv')

try:
    action = sys.argv[2]
except:
    action = 'show'

if action == 'save':
    output_dir = filename.replace('.csv','')
    try:
        os.stat(output_dir)
    except:
        os.mkdir(output_dir)

try:
    x_axis_type = sys.argv[3]
except:
    x_axis_type = 'time'

if x_axis_type not in ['events','time']:
    print('Wrong argument for x axis. Assuming time')
    x_axis_type = 'time'

# print(df.head())
# print(df_events.head())
# print(df_missions.head())

#####################################################################################
MAX_TIME = 2300
robots = ['pioneer3at_1', 'pioneer3at_2', 'UAV_1', 'UAV_2']
bat_low = {'pioneer3at_1': [(1628.86,24.66)], 'pioneer3at_2': [(1600.14,24.19)], 'UAV_1': [(2252.93,24.32)], 'UAV_2': [(1698.99,24.96)]}
fail = {'pioneer3at_1': [], 'pioneer3at_2': [461.18], 'UAV_1': [1426.93,2083.82], 'UAV_2': []}
victim = {'pioneer3at_1': [1518.94], 'pioneer3at_2': [1255.51], 'UAV_1': [806.29], 'UAV_2': []}

## List of events by robot
events = {}
events['pioneer3at_1']  = pd.Series(index = [1518.94,1628.86], data = ['v_found','bat_low'])
events['pioneer3at_2']  = pd.Series(index = [461.18,1255.51,1600.14], data = ['fail','v_found','bat_low'])
events['UAV_1']         = pd.Series(index = [806.29,1426.93,2083.82,2252.93], data = ['uav_v_found','uav_fail', 'uav_fail','uav_bat_low'])
events['UAV_2']         = pd.Series(index = [1698.99], data = ['uav_bat_low'])

for r in robots:
    fig = plt.figure(constrained_layout=True, figsize=(18,10))
    fig.suptitle(r, fontsize="x-large")
    gs = fig.add_gridspec(3, 2)
    ax1 = fig.add_subplot(gs[0,0])
    ax1.spines['right'].set_visible(False)
    ax1.spines['top'].set_visible(False)
    ax1.spines['bottom'].set_visible(False)
    ax1.set_xlim(0, MAX_TIME)
    # ax1.set_title('Current maneuver')

    ax2 = fig.add_subplot(gs[1,0])
    ax2.spines['right'].set_visible(False)
    ax2.spines['top'].set_visible(False)
    ax2.spines['bottom'].set_visible(False)
    ax2.set_xlim(0, MAX_TIME)
    # ax2.set_title('Battery level (%)')

    ax3 = fig.add_subplot(gs[2,0])
    ax3.spines['right'].set_visible(False)
    ax3.spines['top'].set_visible(False)
    ax3.spines['left'].set_visible(False)
    ax3.set_ylim(-1, 4)
    ax3.set_xlim(0, MAX_TIME)
    # ax2.set_title('events (%)')

    ax4 = fig.add_subplot(gs[:, 1], projection = '3d')
    ax4.set_title('Robot trajectory')

    #####-- CURRENT MANEUVER GRAPH --###############################################################################
    ax1.plot(df.iloc[:,0].values, df['{}_cur_maneuver'.format(r)].values, 'k', label='_nolegend_')
    # ax1.tick_params(axis='x', rotation=90)
    ax1.set_xticks([])
    ax1.set_yticks(['None','teleoperation',
                    'return to base',
                    'surroundings verification',
                    'safe land',
                    'approach',
                    'exploration',
                    'assessment',
                    'victim search'])

    for e in events[r].index:
        ax1.axvline(e, color ='r', lw = 1.5, ls='-.', alpha = 0.75)

    ax1.set_ylabel('Current maneuver', rotation='vertical')
    # ax1.legend(loc = 'best')

    #####-- BATTERY LEVEL GRAPH --##################################################################################
    ax2.plot(df.iloc[:,0].values, df['{}_battery'.format(r)].values, 'b', label='_nolegend_')
    # ax2.tick_params(axis='x', rotation=90)
    # ax2.set_xticks(range(0,2400,100))
    ax2.set_xticks([])
    ax2.set_yticks(range(0,110,10))

    # ax2.set_xlabel('Time (s)')
    ax2.set_ylabel('Battery Level (%)', rotation='vertical')

    #Scatter points
    x = [b[0] for b in bat_low[r]]
    y = [b[1] for b in bat_low[r]]
    ax2.scatter(x, y, marker='o', color='r', zorder=10)

    for e in events[r].index:
        ax2.axvline(e, color ='r', lw = 1.5, ls='-.', alpha = 0.75)

    #####-- EVENTS TIMELINE GRAPH --################################################################################
    levels = np.tile([3.5, 4], int(np.ceil(len(events[r])/2)))[:len(events[r])]

    # Create figure and plot a stem plot with the date
    ax3.vlines(events[r].index, 0, levels, color="tab:red")  # The vertical stems.
    ax3.plot(events[r].index, np.zeros_like(events[r].index), "-o", color="k", markerfacecolor="w")  # Baseline and markers on it.

    # annotate lines
    for d, l, e in zip(events[r].index, levels, events[r].values):
        ax3.annotate(e, xy=(d, l),
                    xytext=(-3, np.sign(l)*3), rotation=90, textcoords="offset points",
                    horizontalalignment="right",
                    verticalalignment="top")

    ax3.tick_params(axis='x', rotation=90)
    ax3.set_xticks(range(0,MAX_TIME+100,100))
    ax3.set_yticks([])
    ax3.set_xlabel('Time (s)')
    ax3.set_ylabel('Event', rotation='vertical')

    #####-- ROBOTS TRAJECTORIES GRAPH --###########################################################################
    graphs = {'normal': 'k', 'vsv': 'b', 'rb': 'r'}
    t = {'x':[], 'y':[], 'z':[]}
    m_trajectories = {'normal': [], 'vsv': [], 'rb': []}

    last_pose = None
    last_m_type = None
    for i in df[(df['{}_pose'.format(r)] != '[]')].index:
        pose = ast.literal_eval(df.loc[i, '{}_pose'.format(r)])
        if not df.loc[i,'{}_cur_maneuver'.format(r)] in ['surroundings verification', 'return to base']:
            m_type = 'normal'
        elif df.loc[i,'{}_cur_maneuver'.format(r)] == 'surroundings verification':
            m_type = 'vsv'
        elif df.loc[i,'{}_cur_maneuver'.format(r)] == 'return to base':
            m_type = 'rb'

        if (m_type != last_m_type) and (m_type != None):
            m_trajectories[m_type].append(cp.deepcopy(t))
            if last_pose:
                m_trajectories[m_type][len(m_trajectories[m_type])-1]['x'].append(last_pose[0])
                m_trajectories[m_type][len(m_trajectories[m_type])-1]['y'].append(last_pose[1])
                m_trajectories[m_type][len(m_trajectories[m_type])-1]['z'].append(last_pose[2])
        m_trajectories[m_type][len(m_trajectories[m_type])-1]['x'].append(pose[0])
        m_trajectories[m_type][len(m_trajectories[m_type])-1]['y'].append(pose[1])
        m_trajectories[m_type][len(m_trajectories[m_type])-1]['z'].append(pose[2])

        last_pose = pose
        last_m_type = m_type

    #Plot maneuvers trajectory
    for m in m_trajectories:
        for t in m_trajectories[m]:
            ax4.plot3D(t['x'], t['y'], t['z'], graphs[m])

    ax4.set_xlabel('X (m)')
    ax4.set_ylabel('Y (m)')
    ax4.set_zlabel('Z (m)')
    # ax4.set_xlim3d(0, 75)
    # ax4.set_ylim3d(0, 40)
    ax4.set_zlim3d(0, 15)

    # Save the figure
    if action == 'save':
        plt.savefig(output_dir + '/{}.png'.format(r))

#############################
# SHOW GRAPHS
if action == 'show':
    plt.show()


#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
Author : Trsdy

https://modenc.renegadeprojects.com/CSF_File_Format
'''

from io import TextIOWrapper
import struct
import json
import sys
import argparse

def make_header(csf: TextIOWrapper, num_labels: int, num_strings: int, locale=0):
    header_info = {
        "FSC": b" FSC",
        "CSF Version": 3,
        "NumLabels": num_labels,
        "NumStrings": num_strings,
        "unused": 0,
        "Language": locale
    }
    csf.write(struct.pack("<4sLLLLL"
            , header_info["FSC"]
            , header_info["CSF Version"]
            , header_info["NumLabels"]
            , header_info["NumStrings"]
            , header_info["unused"]
            , header_info["Language"]
            ))
    return csf

def string2bytes(content: str) -> bytes:
    raw_bytes = bytearray(content.encode("UTF-16LE"))
    for idx in range(len(raw_bytes)-1, -1, -1):
        raw_bytes[idx] = raw_bytes[idx] ^ 0xff
    return bytes(raw_bytes)

def main_coversion(csf_file: TextIOWrapper, name_str_map: dict):
    csf_file = make_header(csf_file, len(name_str_map), len(name_str_map))
    for label, ui_content in name_str_map.items():
        csf_file.write(struct.pack("<4sL", b" LBL", 1))
        uiname_bytes = label.encode('utf-8')
        csf_file.write(struct.pack(f"<L{len(uiname_bytes)}s4s", len(
            uiname_bytes), uiname_bytes, b' RTS'))
        value = string2bytes(ui_content)
        value_len = len(value)//2
        csf_file.write(struct.pack("<L", value_len))
        csf_file.write(struct.pack(f"<{value_len*2}s", value))

def parse_filenames():
    parser = argparse.ArgumentParser(
        description="Convert a JSON file to a CSF file")
    parser.add_argument('-i', '--json', type=str, required=True, help="input JSON file")
    parser.add_argument('-o', '--csf', type=str, required=True, help="output CSF file")
    args = parser.parse_args()
    return args.json, args.csf

if __name__ == "__main__":
    jsn_filename, csf_filename = parse_filenames()
    with open(jsn_filename, mode="r", encoding="GB2312") as jsn_file:
        name_str_dict = json.load(jsn_file)

        with open(csf_filename, mode="wb") as CSF_File:
            main_coversion(CSF_File, name_str_dict)


"""Platform mock upload module."""
import base64
import datetime
import hashlib
import json
import os
import signal
import uuid

from insights.core import archives, dr
from insights.core.archives import InvalidContentType
from insights.core.context import HostArchiveContext
from insights.core.hydration import create_context
from insights.specs import Specs
from insights.parsers.dnf_modules import DnfModules
from insights.parsers.installed_rpms import Installed
from insights.parsers.yum_repos_d import YumReposD
from tornado.ioloop import IOLoop
from tornado.web import Application, RequestHandler

from common.logging import init_logging, get_logger
from common import mqueue
from common.database_handler import DatabasePool, DatabasePoolConnection

LOGGER = get_logger(__name__)
STORAGE_PATH = "/tmp/storage"

class BaseHandler(RequestHandler):
    """Base Handler."""

    def _get_rh_account(self):
        if "x-rh-identity" not in self.request.headers:
            return "0"
        encoded_value = self.request.headers["x-rh-identity"]
        decoded_value = base64.b64decode(encoded_value).decode("utf-8")
        identity = json.loads(decoded_value)
        return identity.get("identity", {}).get("account_number", "0")

class UploadHandler(BaseHandler):
    """Upload Handler."""

    def data_received(self, chunk):
        pass

    @staticmethod
    def _get_system_profile(archive_path):
        profile = {}
        default_packages = (
            "insights.specs.default",
            "insights.specs.insights_archive",
            "insights.combiners",
            "insights.parsers"
        )
        for pkg in default_packages:
            dr.load_components(pkg)
        broker = dr.Broker()
        try:
            with archives.extract(archive_path) as ex:
                ctx = create_context(ex.tmp_dir, HostArchiveContext)
                broker[ctx.__class__] = ctx
                broker = dr.run(components=[Specs.machine_id, Installed, DnfModules, YumReposD],
                                broker=broker)
                if Specs.machine_id in broker:
                    profile["id"] = broker[Specs.machine_id].content[0].strip()
                profile["installed_packages"] = []
                if Installed in broker:
                    pkglist = broker[Installed]
                    for pkg_name in pkglist.packages:
                        pkg = pkglist.get_max(pkg_name)
                        profile["installed_packages"].append(pkg.nevra)

                profile["yum_repos"] = []
                if YumReposD in broker:
                    repolist = broker[YumReposD]
                    for repo_file in repolist:
                        if repo_file.file_name == 'redhat.repo':
                            for repo in repo_file:
                                if repo_file[repo].get('enabled', '1').lower() in ('1', 'true', 'enabled', 'yes', 'on'):
                                    profile["yum_repos"].append(repo)
                            break

                profile["dnf_modules"] = []
                if DnfModules in broker:
                    for module in broker[DnfModules]:
                        for module_name in module.sections():
                            profile["dnf_modules"].append({'name': module_name,
                                                           'stream': module.get(module_name, 'stream')})
                LOGGER.info(profile)
        except InvalidContentType:
            LOGGER.error("Unable to parse archive.")
        return profile

    def _get_upload_multiplier(self):
        if "x-upload-multiplier" not in self.request.headers:
            return 1
        multiplier = self.request.headers["x-upload-multiplier"]
        return int(multiplier) if multiplier.isdigit() else 1

    def post(self):
        """Answer POST request.
           curl -X POST -F "file=@./file.tar.gz" http://localhost:8100/api/v1/upload
           curl -X POST -F "file=@./file.tar.gz" -H "x-upload-multiplier: 10" http://localhost:8100/api/v1/upload
        """
        if self.request.files and "file" in self.request.files:
            sha1 = hashlib.sha1(self.request.files["file"][0]["body"]).hexdigest()
            file_name = "%s.tar.gz" % sha1
            file_path = os.path.join(STORAGE_PATH, file_name)
            if not os.path.exists(file_path):
                with open(file_path, "wb") as open_file:
                    open_file.write(self.request.files["file"][0]["body"])
            if sha1 in self.application.archive_to_profile_cache:
                profile = self.application.archive_to_profile_cache[sha1]
            else:
                profile = self._get_system_profile(file_path)
                self.application.archive_to_profile_cache[sha1] = profile
                self.application.inventory_id_to_profile_cache[profile.get("id", None)] = profile
            download_url = "http://platform_mock:8000/api/v1/download/%s" % file_name
            rh_account = self._get_rh_account()
            timestamp = datetime.datetime.utcnow().isoformat()
            upload_message = {"host": {"id": profile.get("id", None), "account": rh_account,
                                       "display_name": sha1 + ".example.com",
                                       "system_profile": {
                                           "installed_packages": profile["installed_packages"],
                                           "yum_repos": [{"id": r, "name": r, "enabled": True} for r in profile["yum_repos"]],
                                           "dnf_modules": profile["dnf_modules"],
                                           "insights_client_version": "3.0.13-1"},
                                       "insights_id": "0"
                                       },
                              "platform_metadata": {"request_id": str(uuid.uuid1()), "url": download_url,
                                                    "b64_identity": self.request.headers.get("x-rh-identity", "")},
                              "timestamp": timestamp,
                              "type": "created"
                              }
            for _ in range(self._get_upload_multiplier()):
                self.application.queue.send(upload_message)
                with DatabasePoolConnection() as conn:
                    with conn.cursor() as cur:
                        cur.execute("""
                                        insert into inventory.hosts_v1_1 values (%s, 0, %s, '[]', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, '2030-07-21 05:35:53.682554+00', '{}')
                                        on conflict (id) do update set display_name = EXCLUDED.display_name, updated = CURRENT_TIMESTAMP
                                     """, (upload_message["host"]["id"],
                                           upload_message["host"]["display_name"]))
                    conn.commit()
            LOGGER.info("New upload: %s", upload_message)
        else:
            self.set_status(400)
        self.finish()

class DownloadHandler(BaseHandler):
    """Download Handler."""

    def data_received(self, chunk):
        pass

    def get(self, path):
        """Answer GET request."""
        file_path = os.path.join(STORAGE_PATH, path)
        if not os.path.isfile(file_path):
            self.set_status(404)
        else:
            self.set_header("Content-Type", "application/octet-stream")
            with open(file_path, "rb") as open_file:
                self.write(open_file.read())
        self.finish()

class DeleteHandler(BaseHandler):
    """Delete Handler."""

    def data_received(self, chunk):
        pass

    def delete(self, inventory_id):
        """Answer DELETE request."""
        rh_account = self._get_rh_account()
        delete_message = {"type": "delete", "id": inventory_id, "account": rh_account}
        self.application.queue.send(delete_message)
        LOGGER.info("Delete: %s", delete_message)
        self.finish()

class RbacHandler(BaseHandler):
    """RBAC mock"""

    def data_received(self, chunk):
        pass

    def get(self):
        """Answer GET request"""
        self.write(json.dumps({'meta': {'count': 1, 'limit': 10, 'offset': 0},
                               'links': {'first': '/api/rbac/v1/access/?application=vulnerability&limit=1000&offset=0',
                                         'next': None, 'previous': None, 'last': '/api/rbac/v1/access/?application=vulnerability&limit=1000&offset=0'},
                               'data': [{'permission': 'vulnerability:*:*', 'resourceDefinitions': []}]}))

class InsightsRulesHandler(BaseHandler):
    """Insights rule API mock"""

    def data_received(self, chunk):
        pass

    def get(self, rule_id):  # pylint: disable=unused-argument
        """Answer GET request"""
        self.write("""{
            "rule_id": "CVE_2017_14491_dnsmasq|CVE_2017_14491_ERROR",
            "created_at": "2019-02-07T19:02:34.162678Z",
            "updated_at": "2020-02-21T13:39:30.779402Z",
            "ruleset": {
                "created_at": "2018-12-21T01:33:00Z",
                "updated_at": "2018-12-21T01:33:00Z",
                "rule_source": "https://gitlab.cee.redhat.com/insights-open-source/insights-security",
                "description": "Security"
            },
            "description": "CVE-2017-14491: dnsmasq code execution with listening processes",
            "active": true,
            "category": {
                "id": 2,
                "name": "Security"
            },
            "impact": {
                "name": "Remote Code Execution",
                "impact": 4
            },
            "likelihood": 4,
            "node_id": "3199382",
            "tags": "cve security",
            "playbook_count": 1,
            "reboot_required": false,
            "publish_date": "2017-10-02T13:00:00Z",
            "summary": "A buffer overflow vulnerability was found in `Dnsmasq`, a popular lightweight DNS and DHCP server. This can lead to remote code execution. Dnsmasq is used either standalone or directly by applications including `libvirt`.\n",
            "generic": "A vulnerability was discovered in Dnsmasq which allows an attacker to overflow a heap buffer and crash or take control of Dnsmasq. This is accomplished through DNS requests to Dnsmasq querying a domain controlled by the attacker. \n\nDnsmasq is a popular lightweight DNS and DHCP server, often used in home networks and cloud environments as a caching DNS\nstub resolver and to manage DHCP leases. It is used either standalone or directly by applications including libvirt, and\nin a number of layered products. libvirt is a management tool for managing Linux containers and virtual machines.\n\nThis vulnerability can lead to remote code execution and could be triggered by a malicious user on the network.\n\nRed Hat recommends that you update Dnsmasq packages to include the [CVE-2017-14491](http://access.redhat.com/security/cve/CVE-2017-14491) security release.",
            "reason": "This system is vulnerable because:\n\n* It is running a vulnerable package",
            "more_info": "* For more information about this specific flaw, see its [knowledge base article](https://access.redhat.com/security/vulnerabilities/3199382).\n*",
            "impacted_systems_count": 0,
            "reports_shown": false,
            "resolution_set": [
                {
                "system_type": 105,
                "resolution": "Red Hat recommends that you update the `dnsmasq` package",
                "resolution_risk": {
                    "name": "Update Package",
                    "risk": 1
                },
                "has_playbook": true
                }
            ],
            "total_risk": 4,
            "hosts_acked_count": 0,
            "rating": 0
            }""".replace('\n', ''))

class InventoryHandler(BaseHandler):
    """Inventory mock"""

    def data_received(self, chunk):
        pass

    def get(self, inventory_id):
        """Answer GET request"""
        if inventory_id not in self.application.inventory_id_to_profile_cache:
            self.set_status(404)
        else:
            prof = self.application.inventory_id_to_profile_cache[inventory_id]
            self.set_header("Content-Type", "application/json")
            self.write(
                json.dumps({"results": [{
                    "id": inventory_id,
                    "system_profile": {
                        "installed_packages": prof["installed_packages"],
                        "yum_repos": [{"id": r, "name": r, "enabled": True} for r in prof["yum_repos"]],
                        "dnf_modules": prof["dnf_modules"]}
                }]}))
        self.finish()

class PatchAdvisoriesHandler(BaseHandler):
    """Patchman Advisories mock"""

    def data_received(self, chunk):
        pass

    def get(self, system_id):  # pylint: disable=unused-argument
        """Answer GET request"""
        self.write(json.dumps({"data": [
            {
                "attributes": {
                    "advisory_type": 1,
                    "applicable_systems": 10,
                    "description": "The bash packages provide Bash...",
                    "public_date": "2020-09-01T15:22:07Z",
                    "severity": 2,
                    "synopsis": "Moderate: bash security update"},
                "id": "RHSA-2020:3592",
                "type": "advisory"}],
            'links': {},
            'meta': {}}))
        self.finish()

class PatchSystemsHandler(BaseHandler):
    """Patchman Systems mock"""

    def data_received(self, chunk):
        pass

    def get(self, advisory_id):  # pylint: disable=unused-argument
        """Answer GET request"""
        self.write(json.dumps({"data": [
            {
                "attributes": {
                    "display_name": "inv-3.example.com",
                    "last_evaluation": "2020-10-05T16:56:12.810595Z",
                    "last_upload": "2020-10-05T16:56:12.612509Z",
                    "rhsa_count": 1,
                    "rhba_count": 5,
                    "rhea_count": 0,
                    "stale": False,
                    "packages_installed": 1,
                    "packages_updatable": 1
                },
                "id": "00000000-0000-0000-0000-000000000003",
                "type": "system"}],
            'links': {},
            'meta': {}}))
        self.finish()

class PatchViewsSystemsAdvHandler(BaseHandler):
    """Patchman /views/systems/advisories mock"""

    def data_received(self, chunk):
        pass

    def post(self):
        """Answer POST request"""
        data = json.loads(self.request.body)

        systems = data.get('systems', [])
        advisories = data.get('advisories', [])

        if len(systems) >= 1 and len(advisories) >= 1:
            self.write(json.dumps({"data": {
                '00000000-0000-0000-0000-000000000005': ['RHSA-2014:1999'],
            }}))
        else:
            self.write(json.dumps({"data": {}}))
        self.finish()

class ExploitsHandler(BaseHandler):
    """Backoffice-proxy for prod sec /v1/exploits mock"""

    def data_received(self, chunk):
        pass

    def get(self):
        """Answer GET request"""
        self.write(json.dumps([
            {
                "cves": [
                    "CVE-2016-1"
                ],
                "exploits": [
                    {
                        "cvss3_temporal_metrics": "E:F/RL:U/RC:R",
                        "disclosure_date": "2010-10-18T00:00:00Z",
                        "first_seen": "2020-09-18T14:40:52Z",
                        "last_updated": "2020-09-22T02:56:51Z",
                        "name": "exploit/linux/local/name",
                        "source": "metasploit",
                        "summary": "some summary"
                    },
                ],
                "id": 1
            },
            {
                "cves": [
                    "CVE-2013-1"
                ],
                "exploits": [
                    {
                        "cvss3_temporal_metrics": "E:F/RL:U/RC:R",
                        "disclosure_date": "2010-10-18T00:00:00Z",
                        "first_seen": "2020-09-18T14:40:52Z",
                        "last_updated": "2020-09-22T02:56:51Z",
                        "name": "exploit/linux/local/name",
                        "source": "metasploit",
                        "summary": "some summary"
                    },
                ],
                "id": 2
            },
        ]))
        self.finish()

class ServerApplication(Application):
    """Platform mock application."""

    def __init__(self):
        handlers = [
            (r"/api/v1/upload/?", UploadHandler),
            (r"/api/v1/download/(.+)", DownloadHandler),
            (r"/api/v1/delete/(.+)", DeleteHandler),
            (r"/api/rbac/v1/access.+", RbacHandler),
            (r"/api/inventory/v1/hosts/(.+)/system_profile", InventoryHandler),
            (r"/api/insights/v1/rule/(.+)", InsightsRulesHandler),
            (r"/api/patch/v1/systems/(.+)/advisories", PatchAdvisoriesHandler),
            (r"/api/patch/v1/advisories/(.+)/systems", PatchSystemsHandler),
            (r"/api/patch/v1/views/systems/advisories", PatchViewsSystemsAdvHandler),
            (r"/v1/exploits", ExploitsHandler),
        ]
        Application.__init__(self, handlers)
        self.instance = IOLoop.instance()
        self.queue = mqueue.MQWriter(mqueue.EVENTS_TOPIC, bootstrap_servers="localhost:9092")
        self.archive_to_profile_cache = {}
        self.inventory_id_to_profile_cache = {}

    def start(self):
        """Start platform mock server."""
        self.instance.start()

    async def stop(self):
        """Stop platform mock server."""
        await self.queue.stop()
        self.instance.stop()

def main():
    """Main platform mock entrypoint."""
    init_logging()
    if not os.path.exists(STORAGE_PATH):
        os.makedirs(STORAGE_PATH)
    DatabasePool(1)
    LOGGER.info("Starting platform mock.")
    app = ServerApplication()
    app.listen(8000)

    def terminate(*_):
        """Trigger shutdown."""
        LOGGER.info("Signal received, stopping application.")
        IOLoop.instance().add_callback_from_signal(app.stop)

    signals = (signal.SIGHUP, signal.SIGTERM, signal.SIGINT)
    for sig in signals:
        signal.signal(sig, terminate)

    app.start()
    LOGGER.info("Shutting down.")

if __name__ == '__main__':
    main()


def powerset(s):
    def helper(cur, c):
        r.append(c)

        for i in range(cur+1, len(s)):
            helper(i, c + [s[i]])

    r = [[]]

    for i in range(len(s)):
        helper(i, [s[i]])

    print("\n".join(map(str, r)))

def powersetV2(s):
    combinations = ["0000"] + ["0"*(len(s)-len(bin(i))+2) + bin(i).lstrip("0b")
                               for i in range(1, 2 ** len(s))]

    print("\n".join(map(lambda x: str([s[i] for i, j in enumerate(x)
                                       if int(j)]), combinations)))

if __name__ == "__main__":
    the_set = [1, 2, 3, 4]
    powersetV2(the_set)


import pandas as pd
import matplotlib.pyplot as plt
import statistics as sta
import numpy as np
from sklearn.metrics import r2_score

data=pd.read_csv('C:/Users/SUDHANSHU/Downloads/car data.csv')
# data.info() # To know about your Data

# Getting the dummie variables
dummy1=pd.get_dummies(data.Fuel_Type)
dummy2=pd.get_dummies(data.Transmission)
dummy3=pd.get_dummies(data.Seller_Type)

data=pd.concat([data,dummy1,dummy2,dummy3],axis=1)
data=data.drop(['Car_Name','CNG','Dealer','Manual','Transmission','Seller_Type','Fuel_Type'],axis=1)

price1=data.get('Present_Price')
year1=data.get('Year')
sp1=data.get('Selling_Price')
kmd1=data.get('Kms_Driven')
disel11=data.get('Diesel')
disel22=data.get('Petrol')
auto1=data.get('Automatic')
deal1=data.get('Individual')
data[['Diesel','Petrol','Automatic','Individual']]=data[['Diesel','Petrol','Automatic','Individual']].astype(int)

price=[]
year=[]
sp=[]
kmd=[]
disel1=[]
disel2=[]
auto=[]
deal=[]

def Normalization(A,B):
    for i in range(len(A)):
        B.append((A[i]-sta.mean(A))/sta.stdev(A))

#  Normalization of all the data

Normalization(price1,price)
Normalization(year1,year)
Normalization(sp1,sp)
Normalization(kmd1,kmd)
Normalization(disel11,disel1)
Normalization(disel22,disel2)
Normalization(auto1,auto)
Normalization(deal1,deal)
print(type(deal1),type(deal))
# initionalization of the varibles
m1=0
m2=0
m3=0
m4=0
m5=0
m6=0
m7=0
c=0

L=0.01 # LEARNING RATE

def Grad_decent(A,B):
    Sa=0
    for i in range(len(A)):
        if type(B)==int:
            s = (m1 * year[i] + m2 * sp[i] + m3 * kmd[i]+m4*disel1[i]+m5*disel2[i]+m6*auto[i]+m7*deal[i]+ c - price[i])
        else:
            s = (m1 * year[i] + m2 * sp[i] + m3 * kmd[i]+m4*disel1[i]+m5*disel2[i]+m6*auto[i]+m7*deal[i] + c - price[i]) * B[i]
        Sa=Sa+s
    return Sa
# For undating the variables

def Update(A,B):
    A=A-(L*B)/len(price)
    return A

JJ=[] # for Storing the values of the Cost function only for graph purpose
ii=[]

for i in range(10000):
    ii.append(i)
    J = 0
    for l in range(len(price)):
        ss = (c + m1 * year[l] + m2 * sp[l] + m3 * kmd[l] + m4 * disel1[l] + m5 * disel2[l] + m6 * auto[l] + m7 * deal[l] - price[l]) ** 2
        J = J + ss
    J1 = J / (2 * len(price))
    JJ.append(J1)

    S1 = Grad_decent(price, 1)
    S2 = Grad_decent(year, year)
    S3 = Grad_decent(sp, sp)
    S4 = Grad_decent(kmd, kmd)
    S5= Grad_decent(disel1,disel1)
    S6=Grad_decent(disel2,disel2)
    S7=Grad_decent(auto,auto)
    S8=Grad_decent(deal1,deal1)
    c = Update(c, S1)
    m1 = Update(m1, S2)
    m2 = Update(m2, S3)
    m3 = Update(m3, S4)
    m4 = Update(m4, S5)
    m5 = Update(m5, S6)
    m6 = Update(m6, S7)
    m7 = Update(m7, S8)

# print(m1,m2,m3,m4,m5,m6,m7,c) to get the values.

# Next we are changing the data types for calculation purpose
price=np.array(price)
year=np.array(year)
sp=np.array(sp)
kmd=np.array(kmd)
disel1=np.array(disel1)
disel2=np.array(disel2)
auto=np.array(auto)
deal=np.array(deal)

y=(m1 * year + m2 * sp + m3 * kmd+m4*disel1+m5*disel2+m6*auto+m7*deal)
r2=r2_score(price,y)

print('The R Sq is :',r2)
# This is the graph between the Cost function an no. of iterations , it helps in deciding the values of Learning rate and no of iteration.
plt.scatter(ii,JJ)
plt.show()


# Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10^n.
#
# Example:
#
# Input: 2
# Output: 91
# Explanation: The answer should be the total numbers in the range of 0 ≤ x < 100,
# excluding 11,22,33,44,55,66,77,88,99
#
#
# Constraints:
#
# 0 <= n <= 8

class Solution:
    def countNumbersWithUniqueDigits(self, n: int) -> int:
        # n=0: 1
        # n=1: 1+9
        # n=2: 1+9+9*9, the first digit can choose from 1~9 since 0 should not be the head
        # the second digit have 9 options of 0~9 that excludes the one chosen as the first digit
        res = 1  # for n=0
        for i in range(1, n + 1):
            k = 9
            for j in range(1, i):
                k *= (10 - j)
            res += k
        return res

print(Solution().countNumbersWithUniqueDigits(2))


# get content and style images
# CONTENT_IDX: 1: Neckarfront    2: sjtu entrance
#              3: Shenzhen scene 4: Anime scene     5: anime
# STYLE_IDX:   1: starry night   2: a colorful artwork with multiple geometries
#              3: abstract art   4: Mondrian art
CONTENT_IDX = 1
STYLE_IDX = 1

WEIGHT_CONTENT = 1
WEIGHTS_STYLE = [1e11, 1e11, 1e11, 1e10, 1e10, 1e9, 1e9]
# WEIGHTS_STYLE = [1e6] * len(layers)

max_iter = 500
show_iter = 50

content_layers = [37]
style_layers = [9, 12, 16, 22, 25, 29, 32]


import sys, os, csv

sys.path.append(os.path.dirname(os.path.abspath(os.path.dirname(__file__))))
from tools import path as p
from tools import collector as c

def parse_timeline_json(tier, division):
    """timeline json파일을 csv파일로 paring하여 저장한다.

    Args:
        tier: parsing하고자 하는 tier
        division: parsing하고자 하는 division
    """
    # parsing해야하는 timeline json들이 저장되어 있는 경로
    timelines_path = p.get_parent_data_path("timelines", tier, division)
    print(timelines_path)
    timelines_dir_list = os.listdir(timelines_path)
    print(timelines_dir_list)

    # 디렉토리에 폴더가 없다면 parsing 불가
    if len(timelines_dir_list) == 0:
        print("no timeline data")
        return

    # timeline json들이 저장된 디렉토리 중 가장 마지막 폴더 번호
    last_timeline_dir_num = int(sorted(timelines_dir_list)[-1])

    # timeline csv저장될 경로.
    timeline_csv_path = p.get_additional_data_path(
        "timeline_csv", tier, division, last_timeline_dir_num
    )

    # csv fields
    fields = [
        "tier",
        "division",
        "match_id",
        "timestamp",
        "type",
        "winningTeam",
        "afterId",
        "ascendedType",
        "beforeId",
        "buildingType",
        "creatorId",
        "eventType",
        "framesInterval",
        "itemId",
        "killerId",
        "laneType",
        "levelUpType",
        "monsterSubType",
        "monsterType",
        "participantId",
        "pointCaptured",
        "skillSlot",
        "teamId",
        "towerType",
        "victimId",
        "wardType",
    ]
    # csv중 특정 column에 값을 넣기 위한 dix dictionary
    field_idx = {f: i for i, f in enumerate(fields)}

    # 마지막 수집한 timeline 데이터들이 담긴 디렉토리 경로, json 리스트
    timeline_json_path = p.get_additional_data_path(
        "timelines", tier, division, last_timeline_dir_num
    )
    timeline_json_list = os.listdir(timeline_json_path)
    for i, timeline_json_file_name in enumerate(timeline_json_list):
        try:
            # 로드할 timeline json 경로와, 파일
            timeline_json_file_path = timeline_json_path + timeline_json_file_name
            timeline_json_file = c.get_json_dict(timeline_json_file_path)

            # parsing 기본사항들
            match_id = timeline_json_file["metadata"]["matchId"]
            row = [None for _ in range(len(fields))]
            row[0], row[1], row[2] = tier, division, match_id

            # timeline csv 경로 설정.
            timeline_csv_name = p.timeline_csv_name(match_id)
            final_timeline_csv_path = timeline_csv_path + timeline_csv_name

            if os.path.exists(final_timeline_csv_path):
                print("이미 csv파일이 존재함")
                continue

            # csv 파일 생성 및 field 입력
            out_file = open(final_timeline_csv_path, "w", newline="")
            write = csv.writer(out_file)
            write.writerow(fields)

            # parsing, csv write
            frames = timeline_json_file["info"]["frames"]
            for frame_n, frame in enumerate(frames):
                events = frame["events"]
                for event_n, event in enumerate(events):
                    for key, value in event.items():
                        if key in field_idx:
                            row[field_idx[key]] = value
                    write.writerow(row)
            out_file.close()

            # 진행도
            if (i + 1) % 1000 == 0:
                percent = (i + 1) / len(timeline_json_list) * 100
                print(f"{tier}, {division} timeline {percent}% 처리")

        except IndexError as e:
            print(e)
            print("에러 발생 생략")
    print(f"{final_timeline_csv_path} 저장")

if __name__ == "__main__":
    tiers = ["SILVER", "GOLD", "PLATINUM", "DIAMOND"]
    divisions = ["I", "II", "III", "IV"]
    for tier in tiers:
        for division in divisions:
            parse_timeline_json(tier, division)


from stackDS import Stack

def decToBin(n):
    binDigit = ""
    binStack = Stack()
    q = n // 2
    binStack.push(n % 2)

    while(q > 0):
        binStack.push(q % 2)
        q //= 2
    print(binStack.size())

    while (binStack.is_empty() == False):
        binDigit += str(binStack.pop())
    return print(binDigit)

decToBin(235)


from classify import Zucchini
from coordinate import Yolo
from device import Camera

import threading
import cv2

class Result():
    def __init__(self, width, height):
        self.zuc = Zucchini()
        self.yolo = Yolo()
        self.width = width
        self.height = height

    def initialize(self,WEIGHTS="", CFG="", H5=""):
        self.weights = WEIGHTS
        self.cfg = CFG
        self.h5 = H5
        return self.check_file()

    def check_file(self):
        if self.weights == "" or self.cfg == "" or self.h5 =="":
            print("WARNING: please select all files")
            ret = 0
        print("--WEIGHTS:", self.weights.split(sep="/")[-1])
        print("--CFG:", self.cfg.split(sep="/")[-1])
        print("--H5:",self.h5.split(sep="/")[-1])

        if self.h5 !="":
            self.zuc.initialize(self.h5)
            ret = 1

        if self.weights != "" and self.cfg != "":
            self.yolo.initialize(self.width, self.height, self.weights, self.cfg)
            ret = 2

        if self.h5 !="" and self.weights != "" and self.cfg != "":
            ret = 3

        return ret

    def get_class(self, frame):
        return self.zuc.detect_zucchini(frame)

    def get_xy(self):
        if self.yolo.found:
            label, x, y, w, h = self.yolo.get_xy()
            tmp = "애호박 "+label+", 좌표:("+str(x)+", "+str(y)+")"
            return tmp
        else:
            return "Not Found"

    def get_res(self,frame):
        if self.yolo.found:
            label, x, y, w, h = self.yolo.get_xy()
            img = self.zuc.crop(frame, x,y,w,h)
            classname= self.zuc.detect_zucchini(img)
            tmp = classname+" "+label+", 좌표:("+str(x)+", "+str(y)+")"
            return tmp
        else:
            return "Not Found"

if __name__ == '__main__':
    vs = cv2.VideoCapture("zuc/zuc4.avi")
    if not vs.isOpened:
        print('Cannot load video')
        exit(0)
    w = int(vs.get(cv2.CAP_PROP_FRAME_WIDTH))
    h = int(vs.get(cv2.CAP_PROP_FRAME_HEIGHT))

    result = Result(w,h)
    result.initialize("zuc/z-yolo.weights", "zuc/zuc.cfg", "zuc/best_model.h5")
    chk = result.check_file()
    print(chk)

    t1 = threading.Thread(target=result.yolo.find_xy, daemon=True)
    t1.start()
    tmp = 0

    while True:
        ret, frame = vs.read()
        if frame is None:
            print('No frame')
            vs.release()
            break

        result.yolo.update_frame(frame)
        if result.yolo.found:
            label, x, y, w, h = result.yolo.get_xy()
            if tmp != x:
                print(result.get_res(frame))
                tmp = x
            frame = result.yolo.print_rec(frame)
        cv2.imshow('', frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break


class Car(object):
    def exclaim(self):
        print("I'm Car!")

# carオブジェクトのクラス(Car)を探す
# Carクラスのexclaim()にself引数としてcarオブジェクト渡す
car = Car()
car.exclaim()

# こうかける
Car.exclaim(car)


import requests
import json

def send_post_request_to_insert_multiple_working_hero(base_url, uri, list_person):
    url = base_url + uri
    payload = json.dumps(list_person, default=vars)
    headers = {
        'Content-Type': 'application/json'
    }
    print(type(payload))
    response = requests.request("POST", url, headers=headers, data=payload)
    return response


from ..bt import BinaryTree, Node

# Create a BinarySearchTree class
class BST(BinaryTree):
  def __init__(self, value):
    self.root = Node(value)
    self.depth = 1

# Define a method named add that accepts a value, and adds a new node with that value in the correct location in the binary search tree.
  def add(self, value):
    self.depth += 1
    new_node = Node(value)

    def search_tree(node):
      if value < node.value:
        if node.left is None:
          node.left = new_node
          print(f"Tree node {value} added to the left of {self.value} at depth {self.depth}")
        else:
          search_tree(node.left)
      else:
        if node.right is None:
          node.right = new_node
          print(f"Tree node {value} added to the left of {self.value} at depth {self.depth}")
        else:
          search_tree(node.right)

    search_tree(self.root)

# Define a method named contains that accepts a value, and returns a boolean indicating whether or not the value is in the tree at least once.
  def contains(self, value):
    current_node = self.root

    while current_node:
      if value == current_node.value:
        return True
      elif value < current_node.value:
        current_node = current_node.left
      else:
        current_node = current_node.right
    return False


from test_framework.blocktools import create_coinbase
from test_framework.mininode import CBlock
from test_framework.script import (
    CScript,
    OP_NOP,
    OP_RETURN,
)
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import (
    start_node,
    connect_nodes,
    sync_blocks,
    assert_equal,
    assert_greater_than,
    assert_raises_jsonrpc,
)
import time
import os

# Rescans start at the earliest block up to 2 hours before a key timestamp, so
# the manual prune RPC avoids pruning blocks in the same window to be
# compatible with pruning based on key creation time.
RESCAN_WINDOW = 2 * 60 * 60

def mine_large_blocks(node, n):
    # Make a large scriptPubKey for the coinbase transaction. This is OP_RETURN
    # followed by 950k of OP_NOP. This would be non-standard in a non-coinbase
    # transaction but is consensus valid.

    # Set the nTime if this is the first time this function has been called.
    # A static variable ensures that time is monotonicly increasing and is therefore
    # different for each block created => blockhash is unique.
    if "nTimes" not in mine_large_blocks.__dict__:
        mine_large_blocks.nTime = 0

    # Get the block parameters for the first block
    big_script = CScript([OP_RETURN] + [OP_NOP] * 950000)
    best_block = node.getblock(node.getbestblockhash())
    height = int(best_block["height"]) + 1
    mine_large_blocks.nTime = max(mine_large_blocks.nTime, int(best_block["time"])) + 1
    previousblockhash = int(best_block["hash"], 16)

    for _ in range(n):
        # Build the coinbase transaction (with large scriptPubKey)
        coinbase_tx = create_coinbase(height)
        coinbase_tx.vin[0].nSequence = 2 ** 32 - 1
        coinbase_tx.vout[0].scriptPubKey = big_script
        coinbase_tx.rehash()

        # Build the block
        block = CBlock()
        block.nVersion = 0x620004
        block.hashPrevBlock = previousblockhash
        block.nTime = mine_large_blocks.nTime
        block.nBits = int('207fffff', 16)
        block.nNonce = 0
        block.vtx = [coinbase_tx]
        block.hashMerkleRoot = block.calc_merkle_root()
        block.calc_sha256()
        block.solve()

        # Submit to the node
        node.submitblock(block.serialize().hex())

        previousblockhash = block.sha256
        height += 1
        mine_large_blocks.nTime += 1

def calc_usage(blockdir):
    return sum(os.path.getsize(blockdir+f) for f in os.listdir(blockdir) if os.path.isfile(blockdir+f)) / (1024. * 1024.)

class PruneTest(BitcoinTestFramework):

    def __init__(self):
        super().__init__()
        self.setup_clean_chain = True
        self.num_nodes = 6

    def setup_network(self):
        self.nodes = []
        self.is_network_split = False

        # Create nodes 0 and 1 to mine
        self.nodes.append(start_node(0, self.options.tmpdir, ["-debug", "-maxreceivebuffer=20000"], timewait=1200))
        self.nodes.append(start_node(1, self.options.tmpdir, ["-debug", "-maxreceivebuffer=20000"], timewait=1200))

        # Create node 2 to test pruning
        self.nodes.append(start_node(2, self.options.tmpdir, ["-debug", "-maxreceivebuffer=20000", "-prune=2200"], timewait=1200))
        self.prunedir = self.options.tmpdir+"/node2/regtest/blocks/"

        # Create nodes 3 and 4 to test manual pruning (they will be re-started with manual pruning later)
        self.nodes.append(start_node(3, self.options.tmpdir, ["-debug=0", "-maxreceivebuffer=20000"], timewait=1200))
        self.nodes.append(start_node(4, self.options.tmpdir, ["-debug=0", "-maxreceivebuffer=20000"], timewait=1200))

        # Create nodes 5 to test wallet in prune mode, but do not connect
        self.nodes.append(start_node(5, self.options.tmpdir, ["-debug=0", "-prune=2200"]))

        # Determine default relay fee
        self.relayfee = self.nodes[0].getnetworkinfo()["relayfee"]

        connect_nodes(self.nodes[0], 1)
        connect_nodes(self.nodes[1], 2)
        connect_nodes(self.nodes[2], 0)
        connect_nodes(self.nodes[3], 4)
        sync_blocks(self.nodes[0:5])

    def create_big_chain(self):
        # Start by creating some coinbases we can spend later
        self.nodes[1].generate(200)
        sync_blocks(self.nodes[0:2])
        self.nodes[0].generate(150)
        # Then mine enough full blocks to create more than 2200MiB of data
        mine_large_blocks(self.nodes[0], 2395)
        sync_blocks(self.nodes[0:2])
        # Note: Separated the manual testing from the main test
        # This can and should be improved in the future
        mine_large_blocks(self.nodes[3], 995)
        mine_large_blocks(self.nodes[4], 995)

    def test_height_min(self):
        if not os.path.isfile(self.prunedir+"blk00000.dat"):
            raise AssertionError("blk00000.dat is missing, pruning too early")
        print("Success")
        print("Though we're already using more than 2200MiB, current usage:", calc_usage(self.prunedir))
        print("Mining 20 more blocks should cause the first block file to be pruned")
        # Pruning doesn't run until we're allocating another chunk, 20 full blocks past the height cutoff will ensure this
        mine_large_blocks(self.nodes[0], 20)

        waitstart = time.time()
        while os.path.isfile(self.prunedir+"blk00000.dat"):
            time.sleep(0.1)
            if time.time() - waitstart > 30:
                raise AssertionError("blk00000.dat not pruned when it should be")

        print("Success")
        usage = calc_usage(self.prunedir)
        print("Usage should be below target:", usage)
        if (usage > 2200):
            raise AssertionError("Pruning target not being met")

    def create_chain_with_staleblocks(self):
        # Create stale blocks in manageable sized chunks
        print("Mine 24 (stale) blocks on Node 1, followed by 25 (main chain) block reorg from Node 0, for 12 rounds")
        for j in range(12):
            # Disconnect node 0 so it can mine a longer reorg chain without knowing about node 1's soon-to-be-stale chain
            # Node 2 stays connected, so it hears about the stale blocks and then reorg's when node0 reconnects
            # Stopping node 0 also clears its mempool, so it doesn't have node1's transactions to accidentally mine
            self.stop_node(0)
            self.nodes[0]=start_node(0, self.options.tmpdir, ["-debug","-maxreceivebuffer=20000", "-checkblocks=6"], timewait=1200)

            # Mine 24 blocks in node 1
            mine_large_blocks(self.nodes[1], 24)

            # Reorg back with 25 block chain from node 0
            mine_large_blocks(self.nodes[0], 25)

            # Create connections in the order so both nodes can see the reorg at the same time
            connect_nodes(self.nodes[1], 0)
            connect_nodes(self.nodes[2], 0)
            sync_blocks(self.nodes[0:3])

        print("Usage can be over target because of high stale rate:", calc_usage(self.prunedir))

    def reorg_test(self):
        # Node 1 will mine a 1441 block chain starting 1439 blocks back from Node 0 and Node 2's tip
        # This will cause Node 2 to do a reorg requiring 1440 blocks of undo data to the reorg_test chain
        # Reboot node 1 to clear its mempool (hopefully make the invalidate faster)
        # Lower the block max size so we don't keep mining all our big mempool transactions (from disconnected blocks)
        self.stop_node(1)
        self.nodes[1]=start_node(1, self.options.tmpdir, ["-debug", "-maxreceivebuffer=20000", "-blockmaxsize=5000", "-blockmaxweight=20000", "-checkblocks=6", "-disablesafemode"], timewait=1200)

        height = self.nodes[1].getblockcount()
        print("Current block height:", height)

        invalidheight = height-1439
        badhash = self.nodes[1].getblockhash(invalidheight)
        print("Invalidating block at height:",invalidheight,badhash)
        self.nodes[1].invalidateblock(badhash)

        # We've now switched to our previously mined-24 block fork on node 1, but that's not what we want
        # So invalidate that fork as well, until we're on the same chain as node 0/2 (but at an ancestor 1440 blocks ago)
        mainchainhash = self.nodes[0].getblockhash(invalidheight - 1)
        curhash = self.nodes[1].getblockhash(invalidheight - 1)
        print('curhash != mainchainhash: ', curhash != mainchainhash)
        while curhash != mainchainhash:
            self.nodes[1].invalidateblock(curhash)
            curhash = self.nodes[1].getblockhash(invalidheight - 1)

        assert(self.nodes[1].getblockcount() == invalidheight - 1)
        print("New best height", self.nodes[1].getblockcount())

        # Reboot node1 to clear those giant tx's from mempool
        self.stop_node(1)
        self.nodes[1]=start_node(1, self.options.tmpdir, ["-debug","-maxreceivebuffer=20000", "-blockmaxsize=5000", "-blockmaxweight=20000", "-checkblocks=6", "-disablesafemode"], timewait=1200)

        print("Generating new longer chain of 1441 more blocks")
        self.nodes[1].generate(1441)

        print("Reconnect nodes")
        connect_nodes(self.nodes[0], 1)
        connect_nodes(self.nodes[2], 1)
        sync_blocks(self.nodes[0:3], timeout=300)

        print("Verify height on node 2:",self.nodes[2].getblockcount())
        print("Usage possibly still high bc of stale blocks in block files:", calc_usage(self.prunedir))

        print("Mine 992 more blocks so we have requisite history (some blocks will be big and cause pruning of previous chain)")
        # Get node0's wallet transactions back in its mempool, to avoid the
        # mined blocks from being too small.
        self.nodes[0].resendwallettransactions()
        mine_large_blocks(self.nodes[0], 992)

        sync_blocks(self.nodes[0:3], timeout=120)

        usage = calc_usage(self.prunedir)
        print("Usage should be below target:", usage)
        if (usage > 2200):
            raise AssertionError("Pruning target not being met")

        return invalidheight, badhash

    def reorg_back(self):
        # Verify that a block on the old main chain fork has been pruned away
        assert_raises_jsonrpc(-1, "Block not available (pruned data)", self.nodes[2].getblock, self.forkhash)
        print("Will need to redownload block",self.forkheight)

        # Verify that we have enough history to reorg back to the fork point
        # Although this is more than 1440 blocks, because this chain was written more recently
        # and only its other 1441 small and 992 large block are in the block files after it,
        # its expected to still be retained
        self.nodes[2].getblock(self.nodes[2].getblockhash(self.forkheight))

        first_reorg_height = self.nodes[2].getblockcount()
        curchainhash = self.nodes[2].getblockhash(self.mainchainheight)
        self.nodes[2].invalidateblock(curchainhash)
        goalbestheight = self.mainchainheight
        goalbesthash = self.mainchainhash2

        # As of 0.10 the current block download logic is not able to reorg to the original chain created in
        # create_chain_with_stale_blocks because it doesn't know of any peer that's on that chain from which to
        # redownload its missing blocks.
        # Invalidate the reorg_test chain in node 0 as well, it can successfully switch to the original chain
        # because it has all the block data.
        # However it must mine enough blocks to have a more work chain than the reorg_test chain in order
        # to trigger node 2's block download logic.
        # At this point node 2 is within 1440 blocks of the fork point so it will preserve its ability to reorg
        if self.nodes[2].getblockcount() < self.mainchainheight:
            blocks_to_mine = first_reorg_height + 1 - self.mainchainheight
            print("Rewind node 0 to prev main chain to mine longer chain to trigger redownload. Blocks needed:", blocks_to_mine)
            self.nodes[0].invalidateblock(curchainhash)
            assert(self.nodes[0].getblockcount() == self.mainchainheight)
            assert(self.nodes[0].getbestblockhash() == self.mainchainhash2)
            goalbesthash = self.nodes[0].generate(blocks_to_mine)[-1]
            goalbestheight = first_reorg_height + 1

        print("Verify node 2 reorged back to the main chain, some blocks of which it had to redownload")
        waitstart = time.time()
        while self.nodes[2].getblockcount() < goalbestheight:
            time.sleep(0.1)
            if time.time() - waitstart > 900:
                raise AssertionError("Node 2 didn't reorg to proper height")
        assert(self.nodes[2].getbestblockhash() == goalbesthash)
        # Verify we can now have the data for a block previously pruned
        assert(self.nodes[2].getblock(self.forkhash)["height"] == self.forkheight)

    def manual_test(self, node_number, use_timestamp):
        # at this point, node has 995 blocks and has not yet run in prune mode
        node = self.nodes[node_number] = start_node(node_number, self.options.tmpdir, ["-debug=0"], timewait=900)
        assert_equal(node.getblockcount(), 995)
        assert_raises_jsonrpc(-1, "not in prune mode", node.pruneblockchain, 500)
        self.stop_node(node_number)

        # now re-start in manual pruning mode
        node = self.nodes[node_number] = start_node(node_number, self.options.tmpdir, ["-debug=0","-prune=1"], timewait=900)
        assert_equal(node.getblockcount(), 995)

        def height(index):
            if use_timestamp:
                return node.getblockheader(node.getblockhash(index))["time"] + RESCAN_WINDOW
            else:
                return index

        def prune(index, expected_ret=None):
            ret = node.pruneblockchain(height(index))
            # Check the return value. When use_timestamp is True, just check
            # that the return value is less than or equal to the expected
            # value, because when more than one block is generated per second,
            # a timestamp will not be granular enough to uniquely identify an
            # individual block.
            if expected_ret is None:
                expected_ret = index
            if use_timestamp:
                assert_greater_than(ret, 0)
                assert_greater_than(expected_ret + 1, ret)
            else:
                assert_equal(ret, expected_ret)

        def has_block(index):
            return os.path.isfile(self.options.tmpdir + "/node{}/regtest/blocks/blk{:05}.dat".format(node_number, index))

        # should not prune because chain tip of node 3 (995) < PruneAfterHeight (1000)
        assert_raises_jsonrpc(-1, "Blockchain is too short for pruning", node.pruneblockchain, height(500))

        # mine 6 blocks so we are at height 1001 (i.e., above PruneAfterHeight)
        node.generate(6)
        assert_equal(node.getblockchaininfo()["blocks"], 1001)

        # negative heights should raise an exception
        assert_raises_jsonrpc(-8, "Negative", node.pruneblockchain, -10)

        # height=100 too low to prune first block file so this is a no-op
        prune(100)
        if not has_block(0):
            raise AssertionError("blk00000.dat is missing when should still be there")

        # Does nothing
        node.pruneblockchain(height(0))
        if not has_block(0):
            raise AssertionError("blk00000.dat is missing when should still be there")

        # height=141 should prune first file
        prune(141)
        if has_block(0):
            raise AssertionError("blk00000.dat is still there, should be pruned by now")
        if not has_block(1):
            raise AssertionError("blk00001.dat is missing when should still be there")

        # height=282 should prune second file
        prune(282)
        if has_block(1):
            raise AssertionError("blk00001.dat is still there, should be pruned by now")

        # height=1000 should not prune anything more, because tip-288 is in blk00002.dat.
        prune(1000)
        if has_block(2):
            raise AssertionError("blk00002.dat is still there, should be pruned by now")

        # advance the tip so blk00002.dat and blk00003.dat can be pruned (the last 288 blocks should now be in blk00004.dat)
        node.generate(288)
        prune(1000)

        for fnum in range(7):
            if has_block(fnum):
                raise AssertionError(f"blk0000{fnum}.dat is still there, should be pruned by now")
        if not has_block(7):
            raise AssertionError("blk00007.dat is missing when should still be there")

        # stop node, start back up with auto-prune at 2200MB, make sure still runs
        self.stop_node(node_number)
        self.nodes[node_number] = start_node(node_number, self.options.tmpdir, ["-debug=0","-prune=2200"], timewait=900)

        print("Success")

    def wallet_test(self):
        # check that the pruning node's wallet is still in good shape
        print("Stop and start pruning node to trigger wallet rescan")
        self.stop_node(2)
        start_node(2, self.options.tmpdir, ["-debug=1","-prune=2200"])
        print("Success")

        # check that wallet loads successfully when restarting a pruned node after IBD.
        # this was reported to fail in #7494.
        print ("Syncing node 5 to test wallet")
        connect_nodes(self.nodes[0], 5)
        nds = [self.nodes[0], self.nodes[5]]
        sync_blocks(nds, wait=5, timeout=300)
        self.stop_node(5) #stop and start to trigger rescan
        start_node(5, self.options.tmpdir, ["-debug=1","-prune=2200"])
        print ("Success")

    def run_test(self):
        print("Warning! This test requires 21GB of disk space and takes over 20 mins (up to 2 hours)")
        print("Mining a big blockchain of 2745 blocks")
        self.create_big_chain()
        # Chain diagram key:
        # *   blocks on main chain
        # +,&,$,@ blocks on other forks
        # X   invalidated block
        # N1  Node 1
        #
        # Start by mining a simple chain that nodes 0-2 have
        # N0=N1=N2 **...*(2745)

        # stop manual-pruning nodes with 995 blocks
        self.stop_node(3)
        self.stop_node(4)

        print("Check that we haven't started pruning yet because we're below PruneAfterHeight")
        self.test_height_min()
        # Extend this chain past the PruneAfterHeight
        # N0=N1=N2 **...*(2765)

        print("Check that we'll exceed disk space target if we have a very high stale block rate")
        self.create_chain_with_staleblocks()
        # Disconnect N0
        # And mine a 24 block chain on N1 and a separate 25 block chain on N0
        # N1=N2 **...*+...+(2789)
        # N0    **...**...**(2790)
        #
        # reconnect nodes causing reorg on N1 and N2
        # N1=N2 **...*(2765) *...**(2790)
        #                   \
        #                    +...+(2789)
        #
        # repeat this process until you have 12 stale forks hanging off the
        # main chain on N1 and N2
        # N0    *************************...***************************(3065)
        #
        # N1=N2 **...*(2765) *...**(2790) *..         ..**(3040) *...**(3065)
        #                   \            \                      \
        #                    +...+(2789)  &..                    $...$(3064)

        # Save some current chain state for later use
        self.mainchainheight = self.nodes[2].getblockcount()   #3065
        self.mainchainhash2 = self.nodes[2].getblockhash(self.mainchainheight)

        print("Check that we can survive a 1440 block reorg still")
        (self.forkheight,self.forkhash) = self.reorg_test() #(1626, )
        # Now create a 1440 block reorg by mining a longer chain on N1
        # First disconnect N1
        # Then invalidate 1626 on main chain and 1625 on fork so height is 1625 on main chain
        # N1   **...*(2765) **...**(1625)X..
        #                  \
        #                   ++...+(1624)X..
        #
        # Now mine 1441 more blocks on N1
        # N1    **...*(2765) **...**(1625) @@...@(3066)
        #                 \               \
        #                  \               X...
        #                   \                 \
        #                    ++...+(1624)X..   ..
        #
        # Reconnect nodes and mine 992 more blocks on N1
        # N1    **...*(2765) **...**(1625) @@...@@@(4058)
        #                 \               \
        #                  \               X...
        #                   \                 \
        #                    ++...+(1624)X..   ..
        #
        # N2    **...*(2765) **...**(1625) @@...@@@(4058)
        #                 \               \
        #                  \               *...**(3065)
        #                   \                 \
        #                    ++...++(2789)     ..
        #
        # N0    ********************(1625) @@...@@@(4058)
        #                                 \
        #                                  *...**(3065)

        print("Test that we can rerequest a block we previously pruned if needed for a reorg")
        self.reorg_back()
        # Verify that N2 still has block 1626 on current chain (@), but not on main chain (*)
        # Invalidate 1626 on current chain (@) on N2 and we should be able to reorg to
        # original main chain (*), but will require redownload of some blocks
        # In order to have a peer we think we can download from, must also perform this invalidation
        # on N0 and mine a new longest chain to trigger.
        # Final result:
        # N0    ********************(1625) **...****(4059)
        #                                 \
        #                                  X@...@@@(4058)
        #
        # N2    **...*(2765) **...**(1625) **...****(4059)
        #                 \               \
        #                  \               X@...@@@(4058)
        #                   \
        #                    +..
        #
        # N1 doesn't change because 1626 on main chain (*) is invalid

        print("Test manual pruning with block indices")
        self.manual_test(3, use_timestamp=False)

        print("Test manual pruning with timestamps")
        self.manual_test(4, use_timestamp=True)

        print("Test wallet re-scan")
        self.wallet_test()

        print("Done")

if __name__ == '__main__':
    PruneTest().main()


#!/usr/bin/python

# importing libraries
import paho.mqtt.client as paho
import os
import socket
import ssl
import ast
import json
import os
import logging

# read AWS parametars json
json_path = "/home/pi/WorkSpace/IoT_test/AWS_IoT.txt"
connect_json = open(json_path,'r')
connect_dict = json.load(connect_json)

# AWS parameters
awshost = connect_dict["ENDPOINT"]
awsport = 8883
clientId  = "IoT_test_1"
thingName = "IoT_test"
caPath = "root-CA.crt"
certPath = "IoT_test.cert.pem"
keyPath = "'IoT_test.private.key'"
mode='both'
pub_message='Hello World!'
SUB_topic="#"

# Configure logging
logger = logging.getLogger("AWSIoTPythonSDK.core")
logger.setLevel(logging.DEBUG)
streamHandler = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
streamHandler.setFormatter(formatter)
logger.addHandler(streamHandler)

# Custom MQTT message callback
def on_message(client, userdata, message):
    print("--------------")
    print("Received a new message: ")
    print(message.payload)
    print("from topic: ")
    print(message.topic)
    print("--------------\n")

def on_connect(client, userdata, flags, rc):                # func for making connection
    print("Connection returned result: " + str(rc) )
    # Subscribing in on_connect() means that if we lose the connection and
    # reconnect then subscriptions will be renewed.
    client.subscribe(SUB_TOPIC , 1)

def on_log(client, userdata, level, msg):
    print(msg.topic+" "+str(msg.payload))

# Publish to the same topic in a loop forever
loopCount = 0
mqttc = paho.Client()                                       # mqttc object
mqttc.on_connect = on_connect                               # assign on_connect func
mqttc.on_message = on_message                               # assign on_message func
mqttc.on_log = on_log

mqttc.tls_set(caPath, certfile=certPath, keyfile=keyPath, cert_reqs=ssl.CERT_REQUIRED, tls_version=ssl.PROTOCOL_TLSv1_2, ciphers=None)      # pass parameters

mqttc.connect(awshost, awsport, keepalive=60*10)               # connect to aws server

mqttc.loop_forever()


import os
import sys

path_left = sys.argv[1]
path_right = sys.argv[2]

def list_path(path):
    dirs = []
    files = []
    with os.scandir(path) as it:
        for entry in it:
            if entry.is_file():
                files.append((entry.name,entry.stat().st_size))
            elif entry.is_dir():
                dirs.append(entry.name)
            else:
                RuntimeError("neither dir nor file: {} in {}".format(entry, path))
    return (dirs,files)

def search_missing(base, comp):
    missing = []
    common = []
    for data in base:
        if data in comp:
            common.append(data)
        else:
            missing.append(data)

    return sorted(missing), sorted(common)

def combine_and_add_path(missing_dirs, missing_files, path):
    missing = []

    for elem in missing_dirs:
        elem_tmp = os.path.join(path,elem)
        missing.append(elem_tmp)

    for elem,_ in missing_files:
        elem_tmp = os.path.join(path,elem)
        missing.append(elem_tmp)

    return missing

def compare_and_print(path_left, path_right):
    (dirs_left, files_left) = list_path(path_left)
    (dirs_right, files_right) = list_path(path_right)

    missing_files_right, common_1 = search_missing(files_left, files_right)
    missing_files_left, common_2 = search_missing(files_right, files_left)
    assert(common_1 == common_2)

    missing_dirs_right, common_1  = search_missing(dirs_left, dirs_right)
    missing_dirs_left, common_2 = search_missing(dirs_right, dirs_left)
    assert(common_1 == common_2)
    common = common_1

    #print("current path left:", path_left)
    #print("current path right:", path_right)
    #print("missing_files_left:", missing_files_left)
    #print("missing_dirs_left:", missing_dirs_left)
    #print("missing_files_right:", missing_files_right)
    #print("missing_dirs_right:", missing_dirs_right)

    missing_left = combine_and_add_path(missing_dirs_left, missing_files_left, path_left)
    missing_right = combine_and_add_path(missing_dirs_right, missing_files_right, path_right)

    return common, missing_left, missing_right

def search_dirs(path_left, path_right):
    common, missing_left, missing_right = compare_and_print(path_left, path_right)
    for elem in common:
        pl = os.path.join(path_left, elem)
        pr = os.path.join(path_right, elem)
        missing_left_tmp, missing_right_tmp = search_dirs(pl, pr)
        missing_left.extend(missing_left_tmp)
        missing_right.extend(missing_right_tmp)
    return missing_left, missing_right

missing_left, missing_right  = search_dirs(path_left, path_right)

print("================missing left:================")
for elem in missing_left:
    print(elem)
print("================missing right:================")
for elem in missing_right:
    print(elem)


from typing import Union, Optional

import shelve
import torch
from uuid import uuid4
from torch import autocast
from fastapi import FastAPI
from fastapi import Response
from diffusers import StableDiffusionPipeline
from PIL import Image

app = FastAPI()

@app.on_event("startup")
def load_pipeline():
    app.pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
    if torch.cuda.is_available():
        app.pipe.to("cuda")

@app.on_event("shutdown")
def load_pipeline():
    app.pipe = None

@app.get("/draw")
def drawing_pictures(prompt: Optional[str] = None):
    if torch.cuda.is_available():
        with autocast("cuda"):
            image = app.pipe(prompt).images[0]
    else:
        image = app.pipe(prompt).images[0]

    image_id = uuid4()
    image_path = f"out/{image_id}.png"
    image.save(image_path)
    with open(image_path, 'rb') as f:
        image_bytes = f.read()

    if prompt:
        with shelve.open("prompts") as db:
            db[image_id] = prompt
        return {"status": "success"}
    else:
        return {"error": "No prompt provided"}

    return Response(content=image_bytes, media_type="image/png")

@app.get("/prompts")
def list_prompts():
    with shelve.open("prompts") as db:
        return list(db.values())


# -*- coding: utf-8 -*-
# @Author: Arius
# @Email: arius@qq.com
# @Date:   2019-01-23 11:27:29

import signal
from functools import partial
from tornado import (
    web,
    ioloop,
    httpserver,
)
import os

from shutdown import sig_handler
from route import urls

settings = {
    'static_path': os.path.join(os.path.dirname(__file__), 'cache'),
    'debug': True,
}

class TornadoMain(web.Application):
    def __init__(self):
        web.Application.__init__(self, urls, **settings)

def main():
    app = TornadoMain()
    server = httpserver.HTTPServer(app)
    server.listen(3154)
    # signal.signal(signal.SIGTERM, partial(sig_handler, server))
    # signal.signal(signal.SIGINT, partial(sig_handler, server))
    ioloop.IOLoop.instance().start()

if __name__ == '__main__':
    main()


class DiagonalSum:
    vertices = 4

    @staticmethod
    def find(dimensions):
        diagonalSum = 1
        currentVal = 1
        for d in range(3, dimensions+1, 2):
            for v in range(0, DiagonalSum.vertices):
                currentVal += d - 1
                diagonalSum += currentVal
        return diagonalSum

if __name__ == '__main__':
    print("[Begin]")
    print("Diagonl Sum of 1001x1001:", DiagonalSum.find(1001))


from random import randrange
import numpy
import copy

#https://www.pythonpool.com/gaussian-elimination-python/
def gauss(constraints):
    matrix_copy = constraints.copy()
    n = len(matrix_copy)
    x = numpy.zeros(n)
    for i in range(n):
        for j in range(i+1, n):
            ratio = matrix_copy[j][i]/matrix_copy[i][i]
            for k in range(n+1):
                matrix_copy[j][k] = matrix_copy[j][k] - ratio * matrix_copy[i][k]
    x[n-1] = matrix_copy[n-1][n]/matrix_copy[n-1][n-1]
    for i in range(n-2,-1,-1):
        x[i] = matrix_copy[i][n]
    for j in range(i+1,n):
        x[i] = x[i] - matrix_copy[i][j]*x[j]
    x[i] = x[i]/matrix_copy[i][i]
    return x.tolist()

def seidel(objective_function: list, constraints: list, signs:list):
    #ONE VARIABLE
    if len(constraints[0]) == 2:
        #Treat all inequalities in Ax ≤ b as equalities and solve for x in each.
        #Return the point out of these that maximizes c.
        results = []
        for const in constraints:
            try:
                results.append(((const[-1]/const[0]) * objective_function[1]))
            except ZeroDivisionError:
                pass
        maximum = max(results)
        final_result_x1 = (constraint[-1]-(constraint[1]*maximum))/constraint[0]
        return final_result_x1

    #ONE CONSTRAINT
    if len(constraints) == 1:
        x2 = constraints[0][-1]/constraints[0][1]
        return [0.0,x2]

    #AS MANY CONSTRAINTS AS VARIABLES
    if len(constraints[0])-1 == len(constraints):
        #Solve the linear system Ax = b using Gaussian elimination and return the resulting point.
        matrix = copy.deepcopy(constraints)
        try:
            point = gauss(matrix)
        except ZeroDivisionError:
            return None
        opt = 0
        for i in range(len(objective_function)):
            opt += point[i]*objective_function[i]
        if all(i >= 0 for i in point):
            return [point]
        else:
            return None

    #DIFFERENT NUMBER OF CONSTRAINTS AND VARIABLES
    else:
        x = None
        while x == None:
            h = randrange(0,len(constraints))
            constraint = constraints[h]
            constraints_modified = copy.deepcopy(constraints)
            constraints_modified.pop(h)
            x = seidel(objective_function, constraints_modified, signs)
            if x is not None:
                break
        equation = 0
        for i in range(len(objective_function)):
            equation += constraint[i]*x[0][i]
        if (signs[h] == '<=') and (equation <= constraint[-1]):
            return x
        elif (signs[h] == '>=') and (equation >= constraint[-1]):
            return x
        elif (signs[h] == '=') and (equation == constraint[-1]):
            return x
        else:
            signs[h] = '='
            seidel(objective_function, constraints, signs)

# #TEST 1
# constraints = [[-1.0,1.0,0.0],[1.0,1.0,8.0],[1.0,1.0,6.0]]
# #either <=, >= or =
# signs = ['<=', '<=', '<=']
# objective_function = [0,1]

# #TEST 2
# constraints = [[-3.0,1.0,4.0],[2.0,1.0,4.0],[6.0,2.0,24.0],[-2.0,1.0,0.0]]
# #either '<=', '>=' or '='
# signs = ['<=', '<=', '<=', '<=']
# objective_function = [-1,1]

constraints = [[1.0,1.0,0.0],[-1.0,-1.0,2.0]]
#either '<=', '>=' or '='
signs = ['<=', '<=']
objective_function = [-1,1]

#Fix for recursion error
result = None
while result is None:
    try:
        result = seidel(objective_function, constraints, signs)
    except RecursionError:
        pass
print('Result: ' + str(result))


from molmod import deg
from molmod.ic import bend_angle, dihed_angle
from molmod.molecules import Molecule

from collections import deque

# generator for bonds of length k
def kth_neighbor(mol, start, k):
    que = deque()
    que.append([start])
    current_level = 1

    # level order bfs
    while len(que) > 0 and current_level < k:
        level_size = len(que)
        current_level += 1

        # iterate through the next level
        for _ in range(level_size):
            path = que.popleft()
            current_node = path[-1]

            for next_node in mol.graph.neighbors[current_node]:
                # make sure we do not visit the same node
                if next_node not in path:
                    que.append(path+[next_node])

    # if there is nothing left, there are no bonds of degree k
    while len(que) > 0:
        yield que.popleft()

# list of all unique bonds of length k
def kth_bonds(mol, k=4):
    # inner help function to check if suspect is already in unique
    def is_unique(suspect):
        for bond in unique:
            if sorted(bond) == sorted(suspect):
                return False
        return True

    # result list of unique bonds
    unique = list()

    # iterate through each atom as the starting point
    for i in range(mol.size):
        # find kth degree neighbors
        for bond in kth_neighbor(mol, i, k):
            # add bond to result set if it is unique
            if is_unique(bond):
                unique.append(bond)
                yield bond

# util to print
def pretty(it, delimiter="\n"):
    print(delimiter.join(map(str, it)))

# util to print symbols
def symbolify(mol, it):
    return map(lambda x: mol.symbols[x], it)

# initialize molecule from pdb
def setup(filename="ethane.pdb"):
    mol = Molecule.from_file(filename)

    # setup stuff
    mol.set_default_graph()
    mol.set_default_symbols()

    return mol

if __name__ == "__main__":
    # initialize molecule
    mol = setup("1IRA_R_B_clean.pdb")

    # all bonds of degree 3
    for bond in kth_bonds(mol, 3):
        # find the coordinates of the bonds
        bond_coor = list(map(lambda x: mol.coordinates[x], bond))

        # print symbols of bond and bond angle in degrees
        to_print = [bond]+symbolify(mol, bond)+[bend_angle(bond_coor)[0]/deg]
        pretty(to_print, "\t")

    # all bonds of degree 4
    for bond in kth_bonds(mol, 4):
        # find the coordinates of the bonds
        bond_coor = list(map(lambda x: mol.coordinates[x], bond))

        # print symbols of bond and dihed angle in degrees
        to_print = [bond]+symbolify(mol, bond)+[dihed_angle(bond_coor)[0]/deg]
        pretty(to_print, "\t")


# Author: Fransiskus Agapa

# ======================
# simple program where a person choose to get and use a vehicle
# the program would ask if the person is an adult or not
# there is limit for either the person is an adult of not
# ======================

from classes import Car
from classes import Bicycle
from classes import Adult
from classes import Children

print("\n== Get nad Use a Vehicle ==")
print("1. Car")
print("2. Bicycle")
print("e. Exit")
choice = input("choice: ").lower()

while choice != 'e':
    if choice == '1':
        print("\n-- Get a Car --\n")
        self_status = input("Are you an adult (yes/no)? ").lower()
        person = Adult()                 # assume user is an adult
        if self_status == "yes":
            print("\n")
            car_yes = Car()
            person.get(car_yes)
            print("\n")

        elif self_status == "no":
            try:
                car_no = Bicycle()
                person.get(car_no)       # Bicycle only has one method, so oython would complain
            except AttributeError:
                print("\n[ You are not allowed to buy and/or drive a car ]")

        else:
            print("\n[ Invalid response ]")

    elif choice == '2':
        print("\n-- Get a Bicycle --\n")
        self_status = input("Are you an adult (yes/no)? ").lower()
        person = Adult()                 # assume person is an adult
        if self_status == "yes":
            print("\n")
            try:
                bicycle_yes = Bicycle()   # on purpose to cause error to tell user if they are able to buy and drive bicycle
                person.get(bicycle_yes)
            except AttributeError:
                print("[ You are able to get and/or cycle a bycycle ]\n")

        elif self_status == "no":
            print("\n")
            bicycle_no = Bicycle()
            person = Children()
            person.get(bicycle_no)
            print("\n")

        else:
            print("\n[ Invalid response ]")

    else:
        print("\n[ Invalid choice ]")

    print("\n== Get and Use a Vehicle ==")
    print("1. Car")
    print("2. Bicycle")
    print("e. Exit")
    choice = input("choice: ").lower()

print("\n== Exit Program ==")


import tensorflow as tf
from tensorflow.python.framework import ops

def cnn_model_fn(features, labels, mode):
  """Model function for CNN."""
  # Input Layer
  input_layer = tf.constant([[1.0]])
  labels = tf.constant([0])

  # Dense Layer
  dense = tf.layers.dense(inputs=input_layer, units=1, activation=tf.nn.relu)
  dropout = tf.layers.dropout(
      inputs=dense, rate=0.4, training=mode == tf.estimator.ModeKeys.TRAIN)

  # Logits Layer
  logits = tf.layers.dense(inputs=dropout, units=2)

  predictions = {
      # Generate predictions (for PREDICT and EVAL mode)
      "classes": tf.argmax(input=logits, axis=1),
      # Add `softmax_tensor` to the graph. It is used for PREDICT and by the
      # `logging_hook`.
      "probabilities": tf.nn.softmax(logits, name="softmax_tensor")
  }

  print(mode)
  print(features)
  print(labels)
  print(tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY)

  loss = tf.losses.sparse_softmax_cross_entropy(labels=labels, logits=logits)
  optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001)
  train_op = optimizer.minimize(
        loss=loss,
        global_step=tf.train.get_global_step())
  return tf.estimator.EstimatorSpec(mode=mode, loss=loss, predictions=predictions, train_op=train_op, export_outputs={
    'regression': tf.estimator.export.RegressionOutput(tf.constant(1.0, dtype=tf.float32)),
    'classification': tf.estimator.export.ClassificationOutput(scores=tf.constant(1.0, dtype=tf.float32)),
    'predict': tf.estimator.export.PredictOutput(tf.constant(1.0, dtype=tf.float32)),
    tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: tf.estimator.export.PredictOutput(tf.constant(1.0, dtype=tf.float32))
  })

def estimator(config):
    return tf.estimator.Estimator(model_fn=cnn_model_fn, config=config)


# -*- coding:utf-8 -*-

import http

class Spider(object):
      def __init__(self, homeUrl, parser):
            super(Spider, self).__init__()
            self.homeUrl = homeUrl
            self.parser = parser

      def getAMessage(self):
            html = http.get(self.homeUrl)
            self.parser.feed(html)
            return self.parser.getMsg()

if __name__ == '__main__':
      import techweb
      import cnbeta
      import cnblog
      import tuicool

      spiders = [
            Spider(cnbeta.HOME_URL, cnbeta.CnbetaParser()),
            Spider(cnblog.HOME_URL, cnblog.CnblogParser()),
            Spider(techweb.HOME_URL, techweb.TechwebParser()),
            Spider(tuicool.HOME_URL, tuicool.TuicoolParser()),
      ]

      count = len(spiders)
      for i in range(count):
            spider = spiders[i]
            print ("-----------------------------")
            print (spider.homeUrl)
            for j in range(3):
                  text = spider.getAMessage()
                  print (text)