#!/usr/bin/env python3
"""
flowdronetello.py

FlowDroNet implementation for the DJI/Ryze Tello

Written by Moritz Sperling
Based on the work of A. Loquercio et al., 2018 (https://github.com/uzh-rpg/rpg_public_dronet)
and P. Ferriere (https://github.com/philferriere/tfoptflow)
and D. Fuentes (https://github.com/damiafuentes/DJITelloPy)

Licensed under the MIT License (see LICENSE for details)
"""

import os
import sys
import cv2
import json
import time
import pygame
import djitellopy
import numpy as np
from copy import deepcopy
from pygame.locals import *
localpath = os.path.dirname(os.path.realpath(__file__))
sys.path.insert(0, localpath + '/../workflow/util/')
from img_utils import flow_to_img, pred_as_bar, pred_as_indicator
sys.path.insert(0, './tfoptflow/tfoptflow/')
from model_flowdronet import ModelFlowDroNet, _DEFAULT_FLOWDRONET_OPTS

#
#        8888888          d8b 888    d8b          888 d8b                   888    d8b
#          888            Y8P 888    Y8P          888 Y8P                   888    Y8P
#          888                888                 888                       888
#          888   88888b.  888 888888 888  8888b.  888 888 .d8888b   8888b.  888888 888  .d88b.  88888b.
#          888   888 "88b 888 888    888     "88b 888 888 88K          "88b 888    888 d88""88b 888 "88b
#          888   888  888 888 888    888 .d888888 888 888 "Y8888b. .d888888 888    888 888  888 888  888
#          888   888  888 888 Y88b.  888 888  888 888 888      X88 888  888 Y88b.  888 Y88..88P 888  888
#        8888888 888  888 888  "Y888 888 "Y888888 888 888  88888P' "Y888888  "Y888 888  "Y88P"  888  888
#

class FrontEnd(object):
    """ Maintains the Tello display and moves it through the keyboard keys.
        Press escape key to quit.
        The controls are:
            - Tab: Takeoff
            - Shift: Land
            - Space: Emergency shutdown
            - Backspace: Shutdown
            - WSAD: Forward, backward, left and right.
            - Q and E: Counter clockwise and clockwise rotations
            - R and F: Up and down.
            - P: Switch through controllable parameters
            - + and -: Raise or lower parameter
            - #: Enable / Disable FlowDroNet
            - O: Show / Hide optical flow
            - C: Toggle recording of frames
    """

    def __init__(self):

        # FlowDroNet Configuration
        self.pwcnet_ckpt_path = localpath + '/models/pwc-net/pwcnet.ckpt-11000'
        self.dronet_model_path = localpath + '/models/FlowDroNet/model_graph_final.pb'
        self.output_folder = '/recordings/test_0'
        self.logfile = "log.json"
        self.FPS = 10
        self.hud_scale = 1.5
        self.starting_speed = 100

        # Configuration of controllable parameters
        # initial values
        self.params = {
            'v_max': 50,
            'r_max': 100,
            'r_scale': 0,
            'prev_len': 3,
            'prev_coll': 0.6,
            'prev_steer': 0.3,
            'reverse_thresh': 0.6,
            'reverse_offset': 0.7,
        }
        # stepsize
        self.params_d = {
            'v_max': 5,
            'r_max': 5,
            'r_scale': 50,
            'prev_len': 1,
            'prev_coll': 0.1,
            'prev_steer': 0.1,
            'reverse_thresh': 0.1,
            'reverse_offset': 0.1,
        }
        # max (min is 0)
        self.params_m = {
            'v_max': 100,
            'r_max': 100,
            'r_scale': 1000,
            'prev_len': 10,
            'prev_coll': 1,
            'prev_steer': 1,
            'reverse_thresh': 1,
            'reverse_offset': 1,
        }

        # Logging
        self.log_info = {
            'cur_v': [],
            'cur_s': [],
            'prd_c': [],
            'prd_s': []
        }

        # Init internal variables (do not change anything below here)
        self.for_back_velocity = 0  # Drone velocities between -100~100
        self.left_right_velocity = 0
        self.up_down_velocity = 0
        self.yaw_velocity = 0
        self.internalSpeed = 100
        self.send_rc_control = False
        self.v_old = [0, ]
        self.s_old = [0, ]
        self.was_dronet = True
        self.last_pred_col = 0
        self.last_pred_ang = 0
        self.last_time = time.time()
        self.is_armed = False
        self.battery_percentage = 0
        self.should_stop = False
        self.record_data = False
        self.show_raw_data = False
        self.frame = None
        self.current_parameter = 0
        self.param_keys = list(self.params.keys())
        self.log_count = 0

        # Configure the pwc-net model for inference, starting with the default options
        self.nn_opts = deepcopy(_DEFAULT_FLOWDRONET_OPTS)
        self.nn_opts['verbose'] = False
        self.nn_opts['batch_size'] = 1
        self.nn_opts['ckpt_path'] = self.pwcnet_ckpt_path
        self.nn_opts['dronet_mode'] = 'rgb'
        self.nn_opts['dronet_model_path'] = self.dronet_model_path
        self.target_size = (int(self.nn_opts['y_shape'][1]), int(self.nn_opts['y_shape'][0]))

        # Create output folder if not exists
        if not os.path.exists(os.path.join(self.output_folder, str(self.log_count).zfill(2))):
            os.makedirs(os.path.join(self.output_folder, str(self.log_count).zfill(2)))

        # Init pygame with display
        pygame.init()
        pygame.display.init()
        pygame.display.set_caption("FlowDroNeTello")
        self.screen = pygame.display.set_mode([int(self.hud_scale * self.target_size[0]) + 44,
                                               int(self.hud_scale * self.target_size[1]) + 36])

        # Show bootscreen
        loadimg = cv2.imread(localpath + "/../misc/images/startuplogo.png")
        loadimg = np.fliplr(loadimg)
        loadimg = np.rot90(loadimg)
        loadimg = pygame.surfarray.make_surface(loadimg)
        self.screen.fill([0, 0, 0])
        self.screen.blit(loadimg, (0, 0))
        pygame.display.update()
        pygame.time.set_timer(USEREVENT + 1, int(1. / self.FPS * 1000))

        # Instantiate the model in inference mode and display the model configuration
        self.nn = ModelFlowDroNet(mode='test', options=self.nn_opts)
        self.nn.print_config()

        # Init Tello object that interacts with the Tello drone
        self.tello = djitellopy.Tello()

    #
    #      888b     d888          d8b               888
    #      8888b   d8888          Y8P               888
    #      88888b.d88888                            888
    #      888Y88888P888  8888b.  888 88888b.       888      .d88b.   .d88b.  88888b.
    #      888 Y888P 888     "88b 888 888 "88b      888     d88""88b d88""88b 888 "88b
    #      888  Y8P  888 .d888888 888 888  888      888     888  888 888  888 888  888
    #      888   "   888 888  888 888 888  888      888     Y88..88P Y88..88P 888 d88P
    #      888       888 "Y888888 888 888  888      88888888 "Y88P"   "Y88P"  88888P"
    #                                                                         888
    #                                                                         888
    #                                                                         888
    #

    def run(self):

        if not self.tello.connect():
            print("Tello not connected")
            return

        if not self.tello.set_speed(self.internalSpeed):
            print("Speed as low as possible")
            return

        # In case streaming is on. This happens when we quit this program without the escape key.
        if not self.tello.streamoff():
            print("Could not stop video stream")
            return

        if not self.tello.streamon():
            print("Could not start video stream")
            return

        # start framereader and read initial frame
        frame_read = self.tello.get_frame_read()
        img = cv2.resize(frame_read.frame, self.target_size)
        prv = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

        # Main Loop
        while not self.should_stop:
            # sometimes read battery status
            if np.random.random() < 0.05:
                self.battery_percentage = self.tello.get_battery()

            # read frame
            img = cv2.resize(frame_read.frame, self.target_size)
            cur = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

            # create img pair
            if self.for_back_velocity > 0:
                img_pair = [prv, cur]
            else:
                img_pair = [cur, prv]

            # get output from flowdronet
            if self.show_raw_data or self.is_armed:
                flow = self.get_dronet_command(img_pair)
            else:
                flow = None
            prv = cur

            # produce hud
            hud = self.update_hud(cur, flow)

            # save frame and hud if recording
            if self.record_data:
                self.save_img(frame_read.frame, 'rec_img')
                self.save_img(hud, 'rec_hud')

            # handle input from dronet or user
            for event in pygame.event.get():
                if event.type == USEREVENT + 1:
                    self.send_input()
                elif event.type == QUIT:
                    self.should_stop = True
                elif event.type == KEYDOWN:
                    if (event.key == K_ESCAPE) or (event.key == K_BACKSPACE):
                        self.should_stop = True
                    else:
                        self.keydown(event.key)
                elif event.type == KEYUP:
                    self.keyup(event.key)

                # shutdown stream
                if frame_read.stopped:
                    frame_read.stop()
                    break

            # wait a little
            time.sleep(1 / self.FPS)

        # save log
        if self.record_data:
            with open(os.path.join(self.output_folder, str(self.log_count).zfill(2), self.logfile) , "w") as f:
                json.dump(self.log_info, f)

        # always call before finishing to deallocate resources
        self.tello.end()
        pygame.quit()
        exit(0)

    def get_dronet_command(self, img_pair):

        # inference
        flow, steer_coll = self.nn.flowdronet_predict((img_pair,))
        theta, p_t = steer_coll[0], steer_coll[1]
        self.last_pred_col = p_t
        self.last_pred_ang = theta

        # calculate resulting velocity:
        if p_t < self.params['reverse_thresh']:
            # if the probability to crash is low, then go forward with v_max * (1 - prob) but also
            # take a percentage of the previous average speed into account:
            # v_new = (1 - a) * v_old +  a * (1 - p_t) * v_max
            mean_velocity = np.mean(self.v_old)
            prev_velocity = (1 - self.params['prev_coll']) * mean_velocity if not np.isnan(mean_velocity) else 0
            velocity = prev_velocity + self.params['prev_coll'] * (1 - p_t) * self.params['v_max']
        else:
            # if the probability to crash is high, instantly move backwards to keep flow alive.
            # v_new = (1 - d - p_t) * v_max
            velocity = (1 - self.params['reverse_offset'] - p_t) * self.params['v_max']
            self.v_old.clear()

        # theta_new = (1 - b) * s_old + b * theta * scale with range[-r_max .. r_max]
        steering = np.clip((1 - self.params['prev_steer']) * np.mean(self.s_old)
                           + self.params['prev_steer'] * theta * self.params['r_scale'],
                       -self.params['r_max'], self.params['r_max']) * np.clip(p_t * 2, 0.2, 1)

        # set forward and yaw velocity if dronet is armed
        if self.is_armed:
            # append current velocity and steering angle for averaging
            self.v_old.append(velocity)
            self.s_old.append(steering)

            # set actual velocities
            self.for_back_velocity = int(velocity)
            self.yaw_velocity = int(steering)

            # delete oldest values
            if len(self.v_old) > self.params['prev_len']:
                self.v_old.pop(0)
                self.s_old.pop(0)

        if self.record_data:
            self.log_info['cur_v'].append(int(velocity))
            self.log_info['cur_s'].append(int(steering))
            self.log_info['prd_c'].append(float(p_t))
            self.log_info['prd_s'].append(float(theta))

        return flow

    #
    #      8888888                            888         888b     d888          888    888                    888
    #        888                              888         8888b   d8888          888    888                    888
    #        888                              888         88888b.d88888          888    888                    888
    #        888   88888b.  88888b.  888  888 888888      888Y88888P888  .d88b.  888888 88888b.   .d88b.   .d88888 .d8888b
    #        888   888 "88b 888 "88b 888  888 888         888 Y888P 888 d8P  Y8b 888    888 "88b d88""88b d88" 888 88K
    #        888   888  888 888  888 888  888 888         888  Y8P  888 88888888 888    888  888 888  888 888  888 "Y8888b.
    #        888   888  888 888 d88P Y88b 888 Y88b.       888   "   888 Y8b.     Y88b.  888  888 Y88..88P Y88b 888      X88
    #      8888888 888  888 88888P"   "Y88888  "Y888      888       888  "Y8888   "Y888 888  888  "Y88P"   "Y88888  88888P'
    #                       888
    #                       888
    #                       888
    #

    def keydown(self, key):
        """ Update velocities based on key pressed
        Arguments:
            key: pygame key
        """
        if key == pygame.K_w:  # set forward velocity
            self.is_armed = False
            self.for_back_velocity = self.params['v_max']
        elif key == pygame.K_s:  # set backward velocity
            self.is_armed = False
            self.for_back_velocity = -self.params['v_max']
        elif key == pygame.K_a:  # set left velocity
            self.is_armed = False
            self.left_right_velocity = -self.params['v_max']
        elif key == pygame.K_d:  # set right velocity
            self.is_armed = False
            self.left_right_velocity = self.params['v_max']
        elif key == pygame.K_r:  # set up velocity
            self.is_armed = False
            self.up_down_velocity = self.params['v_max']
        elif key == pygame.K_f:  # set down velocity
            self.is_armed = False
            self.up_down_velocity = -self.params['v_max']
        elif key == pygame.K_e:  # set yaw clockwise velocity
            self.is_armed = False
            self.yaw_velocity = self.params['r_max']
        elif key == pygame.K_q:  # set yaw counter clockwise velocity
            self.is_armed = False
            self.yaw_velocity = -self.params['r_max']
        elif key == pygame.K_TAB:  # takeoff
            self.tello.takeoff()
            self.send_rc_control = True
        elif key == pygame.K_LSHIFT:  # land
            self.is_armed = False
            self.tello.land()
            self.send_rc_control = False
        elif key == pygame.K_SPACE:  # emergency shutdown
            self.is_armed = False
            self.tello.emergency()
            self.send_rc_control = False
            self.should_stop = True
        elif key == pygame.K_HASH:  # arm/disarm dronet
            self.is_armed = not self.is_armed

            # instantly start moving to actually have optical flow and not just noise
            self.for_back_velocity = self.starting_speed if self.is_armed else 0
            self.yaw_velocity = 0
        elif key == pygame.K_p:  # switch through parameters
            if self.current_parameter < len(self.params) - 1:
                self.current_parameter = self.current_parameter + 1
            else:
                self.current_parameter = 0
        elif key == pygame.K_PLUS:  # raise current parameter
            what = self.param_keys[self.current_parameter]
            if self.params[what] < self.params_m[what] - 0.01:
                self.params[what] = self.params[what] + self.params_d[what]
        elif key == pygame.K_MINUS:  # lower current parameter
            what = self.param_keys[self.current_parameter]
            if self.params[what] > 0.01:
                self.params[what] = self.params[what] - self.params_d[what]
        elif key == pygame.K_o:  # show/hide optical flow and predictbars
            self.show_raw_data = not self.show_raw_data
            if self.show_raw_data:
                self.screen = pygame.display.set_mode([int(self.hud_scale * self.target_size[0] + 76),
                                                       int(self.hud_scale * 2 * self.target_size[1] + 76)])
            else:
                self.screen = pygame.display.set_mode([int(self.hud_scale * self.target_size[0] + 44),
                                                       int(self.hud_scale * self.target_size[1] + 36)])
        elif key == pygame.K_c:  # toggle recording of data
            self.record_data = not self.record_data

            if self.record_data:
                if not os.path.exists(os.path.join(self.output_folder, str(self.log_count).zfill(2))):
                    os.makedirs(os.path.join(self.output_folder, str(self.log_count).zfill(2)))
                    print(os.path.join(self.output_folder, str(self.log_count).zfill(2)))
                else:
                    with open(os.path.join(self.output_folder, str(self.log_count).zfill(2), self.logfile), "w") as f:
                        json.dump(self.log_info, f)
                    self.log_count = self.log_count + 1
                    self.log_info['cur_v'].clear()
                    self.log_info['cur_s'].clear()
                    self.log_info['prd_c'].clear()
                    self.log_info['prd_s'].clear()

    def keyup(self, key):
        """ Update velocities based on key released
        Arguments:
            key: pygame key
        """
        if key == pygame.K_w or key == pygame.K_s:  # set zero forward/backward velocity
            self.for_back_velocity = 0
        elif key == pygame.K_a or key == pygame.K_d:  # set zero left/right velocity
            self.left_right_velocity = 0
        elif key == pygame.K_r or key == pygame.K_f:  # set zero up/down velocity
            self.up_down_velocity = 0
        elif key == pygame.K_q or key == pygame.K_e:  # set zero yaw velocity
            self.yaw_velocity = 0

    def send_input(self):
        """ Update routine. Send velocities to Tello."""
        print("V: " + str(self.for_back_velocity) + "; Y: " + str(self.yaw_velocity))
        if self.send_rc_control:
            self.tello.send_rc_control(self.left_right_velocity, self.for_back_velocity, self.up_down_velocity,
                                       self.yaw_velocity)

    #
    #        888    888          888                                888b     d888          888    888                    888
    #        888    888          888                                8888b   d8888          888    888                    888
    #        888    888          888                                88888b.d88888          888    888                    888
    #        8888888888  .d88b.  888 88888b.   .d88b.  888d888      888Y88888P888  .d88b.  888888 88888b.   .d88b.   .d88888 .d8888b
    #        888    888 d8P  Y8b 888 888 "88b d8P  Y8b 888P"        888 Y888P 888 d8P  Y8b 888    888 "88b d88""88b d88" 888 88K
    #        888    888 88888888 888 888  888 88888888 888          888  Y8P  888 88888888 888    888  888 888  888 888  888 "Y8888b.
    #        888    888 Y8b.     888 888 d88P Y8b.     888          888   "   888 Y8b.     Y88b.  888  888 Y88..88P Y88b 888      X88
    #        888    888  "Y8888  888 88888P"   "Y8888  888          888       888  "Y8888   "Y888 888  888  "Y88P"   "Y88888  88888P'
    #                                888
    #                                888
    #                                888
    #

    def update_hud(self, frame, flow):
        """Draw drone info and print on frame"""

        # resize to desired scale
        new_size = (int(self.target_size[0] * self.hud_scale), int(self.target_size[1] * self.hud_scale))
        img = cv2.resize(frame, new_size)

        # show blinking red dot when recording
        if self.record_data and round(time.time()) % 2 == 0:
            cv2.putText(img, "Recording", (img.shape[1] - 187, 38),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 0, 0), lineType=30)
            if round(time.time()) % 2 == 0:
                cv2.circle(img, (new_size[0] - 30, 30), 15, (255, 0, 0), -1)

        # show battery percentage
        #cv2.putText(img, "Battery: {:d} %".format(int(self.battery_percentage)), (img.shape[1] - 187, img.shape[0] - 12),
        #            cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 0, 0), lineType=30)

        # display info on screen
        if self.is_armed:
            stats = ["FlowDroNet active."]
            if self.was_dronet:
                stats.append("Predictions:")
                stats.append("[C: {:4.3f}] [SA: {:4.3f}]".format(float(self.last_pred_col), float(self.last_pred_ang)))
                stats.append("Commands:")
                stats.append("[V: {:03d}] [SA: {:03d}]".format(int(self.for_back_velocity), int(self.yaw_velocity)))
            else:
                stats.append("Command overwritten ...")
        elif self.show_raw_data:
            stats = ["FlowDroNet disarmed.", "Predictions:",
                     "[C: {:4.3f}] [SA: {:4.3f}]".format(float(self.last_pred_col), float(self.last_pred_ang))]
        else:
            stats = ["FlowDroNet disabled."]

        stats.append(self.param_keys[self.current_parameter]
                     + ": {:4.1f}".format(self.params[self.param_keys[self.current_parameter]]))
        for idx, stat in enumerate(stats):
            text = stat.lstrip()
            cv2.putText(img, text, (10, 30 + (idx * 30)),
                        cv2.FONT_HERSHEY_SIMPLEX,
                        0.8, (255, 0, 0), lineType=30)

        # display yaw velocity as indicator in the middle
        hspacer = np.ones((12, img.shape[1], 3), dtype=np.uint8) * 255
        yawbar = pred_as_indicator(self.yaw_velocity / 100., (20, img.shape[1]), "Yaw Velocity")
        yawbar = cv2.cvtColor(yawbar, cv2.COLOR_BGR2RGB)

        # show flow and predictions if desired
        if (self.show_raw_data or self.is_armed) and flow is not None:
            steerbar = pred_as_indicator(self.last_pred_ang, (20, img.shape[1]), "Steering Pred.")
            steerbar = cv2.cvtColor(steerbar, cv2.COLOR_BGR2RGB)
            flow_img = cv2.resize(flow_to_img(flow), new_size) if flow.shape[2] == 2 else cv2.resize(flow, new_size)
            self.frame = np.vstack((img, hspacer, yawbar, hspacer, steerbar, hspacer, flow_img))
        else:
            self.frame = np.vstack((img, hspacer, yawbar, hspacer))

        # display velocity as bar on the side
        vspacer = np.ones((self.frame.shape[0], 12, 3), dtype=np.uint8) * 255
        velobar = pred_as_indicator(self.for_back_velocity / 100, (20, self.frame.shape[0]), "Velocity",
                                    mode="vertical")
        velobar = cv2.cvtColor(velobar, cv2.COLOR_BGR2RGB)

        # show predictions if desired
        if (self.show_raw_data or self.is_armed) and flow is not None:
            vspacer = np.ones((self.frame.shape[0], 12, 3), dtype=np.uint8) * 255
            collbar = pred_as_bar(self.last_pred_col, (self.frame.shape[0], 20), "Collision Prob.")
            collbar = cv2.cvtColor(collbar, cv2.COLOR_BGR2RGB)
            output = np.hstack((vspacer, self.frame, vspacer, velobar, vspacer, collbar))
        else:
            output = np.hstack((vspacer, self.frame, vspacer, velobar))

        # update output
        self.frame = np.fliplr(output)
        self.frame = np.rot90(self.frame)
        self.frame = pygame.surfarray.make_surface(self.frame)
        self.screen.fill([0, 0, 0])
        self.screen.blit(self.frame, (0, 0))
        pygame.display.update()

        return cv2.cvtColor(output, cv2.COLOR_BGR2RGB)

    def save_img(self, img, string):
        """ Save frame to disk with timestamp. """
        folder = os.path.join(self.output_folder, str(self.log_count).zfill(2))
        fname = os.path.join(folder, time.strftime(string + "_%Y%m%d_%H%M%S_0.png", time.gmtime()))
        i = 0
        while os.path.exists(fname):
            fname = fname.replace('_' + str(i) + '.', '_' + str(i + 1) + '.', )
            i = i + 1
        cv2.imwrite(fname, img)

def main():
    frontend = FrontEnd()

    # run frontend
    frontend.run()

if __name__ == '__main__':
    main()


import os
import sys

from srblib import show_dependency_error_and_exit

try:
    import argparse
    from argcomplete import autocomplete
except:
    show_dependency_error_and_exit()

def get_parser():
    def _is_valid_file(parser, arg):
        if not os.path.isfile(arg):
            parser.error("The file %s does not exist!" % arg)
        else:
            return arg

    parser = argparse.ArgumentParser()
    parser.add_argument("-v","--version",
                        action='store_true',
                        help='Display version number')
    parser.add_argument("-o","--output",
                        # type=lambda x: _is_valid_file(parser,x),
                        type=str,
                        help='Output file name, default output.xlsx')
    autocomplete(parser)
    return parser.parse_args()


from tkinter import *
import tkinter.messagebox
tk = Tk()
tk.title("Tic Tac Toe")
flag=1
count=0

def btnClick(btn):
	global flag,count
	if btn['text']==' ' and flag==1:
		btn['text']="X"
		flag=0
	elif btn['text']==' ' and flag==0:
		btn['text']='O'
		flag=1
	else:
		pass
	count=count+1
	checkForWin()

def clear():
	global count
	button1['text']=' '
	button2['text']=' '
	button3['text']=' '
	button4['text']=' '
	button5['text']=' '
	button6['text']=' '
	button7['text']=' '
	button8['text']=' '
	button9['text']=' '
	count=0

def checkForWin():
    if (button1['text'] == 'X' and button2['text'] == 'X' and button3['text'] == 'X' or
        button4['text'] == 'X' and button5['text'] == 'X' and button6['text'] == 'X' or
        button7['text'] =='X' and button8['text'] == 'X' and button9['text'] == 'X' or
        button1['text'] == 'X' and button5['text'] == 'X' and button9['text'] == 'X' or
        button3['text'] == 'X' and button5['text'] == 'X' and button7['text'] == 'X' or
        button1['text'] == 'X' and button2['text'] == 'X' and button3['text'] == 'X' or
        button1['text'] == 'X' and button4['text'] == 'X' and button7['text'] == 'X' or
        button2['text'] == 'X' and button5['text'] == 'X' and button8['text'] == 'X' or
        button7['text'] == 'X' and button6['text'] == 'X' and button9['text'] == 'X'):
    	tkinter.messagebox.showinfo("Tic-Tac-Toe","X WINS :)")
    	clear()

    elif (button1['text'] == 'O' and button2['text'] == 'O' and button3['text'] == 'O' or
          button4['text'] == 'O' and button5['text'] == 'O' and button6['text'] == 'O' or
          button7['text'] == 'O' and button8['text'] == 'O' and button9['text'] == 'O' or
          button1['text'] == 'O' and button5['text'] == 'O' and button9['text'] == 'O' or
          button3['text'] == 'O' and button5['text'] == 'O' and button7['text'] == 'O' or
          button1['text'] == 'O' and button2['text'] == 'O' and button3['text'] == 'O' or
          button1['text'] == 'O' and button4['text'] == 'O' and button7['text'] == 'O' or
          button2['text'] == 'O' and button5['text'] == 'O' and button8['text'] == 'O' or
          button7['text'] == 'O' and button6['text'] == 'O' and button9['text'] == 'O'):
    	tkinter.messagebox.showinfo("Tic-Tac-Toe","O WINS :)")
    	clear()

    elif count==9:
    	tkinter.messagebox.showinfo("Tic-Tac-Toe","GAME DRAW TRY AGAIN")
    	clear()

button1 = Button(tk, text=" ", font='Times 15 bold', bg='indian red', fg='gold', height=4, width=8, command=lambda: btnClick(button1))
button1.grid(row=3, column=0)

button2 = Button(tk, text=' ', font='Times 15 bold', bg='indian red', fg='gold', height=4, width=8, command=lambda: btnClick(button2))
button2.grid(row=3, column=1)

button3 = Button(tk, text=' ',font='Times 15 bold', bg='indian red', fg='gold', height=4, width=8, command=lambda: btnClick(button3))
button3.grid(row=3, column=2)

button4 = Button(tk, text=' ', font='Times 15 bold', bg='indian red', fg='gold', height=4, width=8, command=lambda: btnClick(button4))
button4.grid(row=4, column=0)

button5 = Button(tk, text=' ', font='Times 15 bold', bg='indian red', fg='gold', height=4, width=8, command=lambda: btnClick(button5))
button5.grid(row=4, column=1)

button6 = Button(tk, text=' ', font='Times 15 bold', bg='indian red', fg='gold', height=4, width=8, command=lambda: btnClick(button6))
button6.grid(row=4, column=2)

button7 = Button(tk, text=' ', font='Times 15 bold', bg='indian red', fg='gold', height=4, width=8, command=lambda: btnClick(button7))
button7.grid(row=5, column=0)

button8 = Button(tk, text=' ', font='Times 15 bold', bg='indian red', fg='gold', height=4, width=8, command=lambda: btnClick(button8))
button8.grid(row=5, column=1)

button9 = Button(tk, text=' ', font='Times 15 bold', bg='indian red', fg='gold', height=4, width=8, command=lambda: btnClick(button9))
button9.grid(row=5, column=2)

tk.mainloop()


import os
import subprocess
import tempfile
from abc import ABCMeta

import netCDF4 as nc
import numpy as np

from ncsg import cf
from ncsg.base import AbstractNCSGObject
from ncsg.constants import NetcdfDimension, DataType, NetcdfVariable, OuterRingOrder, ClosureConvention, StopEncoding, \
    GeneralAttributes

class CFGeometryCollection(AbstractNCSGObject):
    """
    A collection of CF geometries.
    """
    # TODO: Docstring and commenting
    __metaclass__ = ABCMeta

    def __init__(self, geom_type, cindex, x, y, z=None, start_index=0, multipart_break=None, hole_break=None,
                 outer_ring_order=None, closure_convention=ClosureConvention.INDEPENDENT, string_id=None):
        geom_type = geom_type.lower()
        if geom_type.startswith('multi'):
            assert multipart_break is not None

        cindex_new = np.zeros(len(cindex), dtype=object)
        for idx, ii in enumerate(cindex):
            cindex_new[idx] = np.array(ii, dtype=DataType.INT)

        if string_id is None:
            string_id = ''
        self.string_id = string_id
        self.cindex = cindex_new
        self.x = x
        self.y = y
        self.z = z
        self.geom_type = geom_type

        self.start_index = start_index
        self.multipart_break = multipart_break
        self.hole_break = hole_break
        self.outer_ring_order = outer_ring_order
        self.closure_convention = closure_convention

        self.cf_names = {'variables': {'coordinate_index': '{}{}'.format(string_id,
                                                                         NetcdfVariable.COORDINATE_INDEX)},
                         'dimensions': {'geometry_count': '{}{}'.format(string_id, NetcdfDimension.GEOMETRY_COUNT)}}

        assert len(self.x) == len(self.y)
        if self.z is not None:
            assert len(self.z) == len(self.x)

    def __eq__(self, other):
        ret = True
        for k, v in self.__dict__.items():
            ov = other.__dict__[k]
            try:
                if k == GeneralAttributes.GEOM_TYPE_NAME:
                    if v.lower() != ov.lower():
                        raise AssertionError('Geometry types are not equal.')
                elif k == 'cindex':
                    for idx in range(len(v)):
                        _assert_array_equal_(v[idx], ov[idx])
                elif k in ['x', 'y', 'z']:
                    _assert_array_equal_(v, ov)
                else:
                    if v != ov:
                        raise AssertionError('"{}" are not equal.'.format(k))
            except AssertionError as _:
                ret = False
        return ret

    def as_shapely(self):
        ret = [None] * len(self.cindex)
        for idx in range(len(self.cindex)):
            geom = cf.to_shapely(self.geom_type, self.cindex[idx], self.x, self.y, z=self.z,
                                 start_index=self.start_index, multipart_break=self.multipart_break,
                                 hole_break=self.hole_break)
            ret[idx] = geom

        return tuple(ret)

    def describe(self, cra=False, header=True, capture=False):
        path = os.path.join(tempfile.gettempdir(), '_ncsg_describe_.nc')
        self.write_netcdf(path, cra=cra)
        ret = None
        try:
            cmd = ['ncdump']
            if header:
                cmd.append('-h')
            cmd.append(path)
            if capture:
                ret = str(subprocess.check_output(cmd))
            else:
                subprocess.check_call(cmd)
        finally:
            os.remove(path)
        return ret

    def write_netcdf(self, path_or_object, cra=False):
        string_id = self.string_id

        should_close = False
        if isinstance(path_or_object, nc.Dataset):
            ds = path_or_object
        else:
            ds = nc.Dataset(path_or_object, mode='w')
            should_close = True

        setattr(ds, GeneralAttributes.CONVENTIONS, GeneralAttributes.CONVENTIONS_VALUE)

        try:
            dname_node_count = '{}{}'.format(string_id, NetcdfDimension.NODE_COUNT)
            dname_geom_count = self.cf_names['dimensions']['geometry_count']
            vname_x = '{}{}'.format(string_id, NetcdfVariable.X)
            vname_y = '{}{}'.format(string_id, NetcdfVariable.Y)
            vname_z = '{}{}'.format(string_id, NetcdfVariable.Z)
            vname_cindex = self.cf_names['variables']['coordinate_index']

            ds.createDimension(dname_node_count, size=len(self.x))
            if cra:
                from ncsg.cra import ContiguousRaggedArray

                dname_cra_node_index = '{}{}'.format(string_id, NetcdfDimension.CRA_NODE_INDEX)
                vname_cra_stop = '{}{}'.format(string_id, NetcdfVariable.CRA_STOP)

                cra_obj = ContiguousRaggedArray.from_vlen(self.cindex, start_index=self.start_index)
                ds.createDimension(dname_geom_count, size=len(cra_obj.stops))
                ds.createDimension(dname_cra_node_index, size=len(cra_obj.value))
                cindex = ds.createVariable(vname_cindex, DataType.INT, dimensions=(dname_cra_node_index,))
                stops = ds.createVariable(vname_cra_stop, DataType.INT, dimensions=(dname_geom_count,))
            else:
                ds.createDimension(dname_geom_count, size=len(self.cindex))
                try:
                    vltype = ds.createVLType(DataType.INT, DataType.GEOMETRY_VLTYPE)
                except RuntimeError:
                    # Type is likely already created. Try to access it.
                    vltype = ds.vltypes[DataType.GEOMETRY_VLTYPE]
                cindex = ds.createVariable(vname_cindex, vltype, dimensions=(dname_geom_count,))

            if cra:
                stop_encoding = StopEncoding.CRA
                cindex_value = cra_obj.value
                stops[:] = cra_obj.stops
                setattr(stops, GeneralAttributes.RAGGED_DIMENSION, dname_cra_node_index)
            else:
                stop_encoding = StopEncoding.VLEN
                cindex_value = self.cindex
            cindex[:] = cindex_value

            cindex.cf_role = GeneralAttributes.CF_ROLE_VALUE_GEOMETRY_VARIABLE
            cindex.geom_type = self.geom_type
            setattr(cindex, GeneralAttributes.GEOM_DIMENSION, dname_geom_count)

            coordinates = [vname_x, vname_y]
            if self.z is not None:
                coordinates.append(vname_z)
            setattr(cindex, GeneralAttributes.COORDINATES, ' '.join(coordinates))
            setattr(cindex, StopEncoding.NAME, stop_encoding)
            if self.multipart_break is not None:
                cindex.multipart_break_value = self.multipart_break
            if 'polygon' in self.geom_type:
                if self.hole_break is not None:
                    cindex.hole_break_value = self.hole_break
                if self.outer_ring_order is not None:
                    setattr(cindex, OuterRingOrder.NAME, self.outer_ring_order)
                setattr(cindex, ClosureConvention.NAME, self.closure_convention)

            x = ds.createVariable(vname_x, DataType.FLOAT, dimensions=(dname_node_count,))
            x[:] = self.x
            setattr(x, GeneralAttributes.CF_ROLE_NAME, GeneralAttributes.GEOM_X_NODE)

            y = ds.createVariable(vname_y, DataType.FLOAT, dimensions=(dname_node_count,))
            y[:] = self.y
            setattr(y, GeneralAttributes.CF_ROLE_NAME, GeneralAttributes.GEOM_Y_NODE)

            if self.z is not None:
                z = ds.createVariable(vname_z, DataType.FLOAT, dimensions=(dname_node_count,))
                z[:] = self.z
                setattr(z, GeneralAttributes.CF_ROLE_NAME, GeneralAttributes.GEOM_Z_NODE)

        finally:
            if should_close:
                ds.close()

def _assert_array_equal_(actual, desired):
    actual = np.array(actual)
    desired = np.array(desired)
    if actual.dtype != desired.dtype:
        raise AssertionError('Data types are not equal.')
    cmp = actual == desired
    if not cmp.all():
        raise AssertionError('Not all values are equal.')


# TASK 4
# APPEND
# Open task4_file.txt in append plus mode ("a+")
# create a for item in range(5): loop, for each loop:
# write to the file: "append #"+ str(item)+"\n"
# use seek() to set the pointer to the beginning of the file
# print the file contents using file .read() method after exiting the loop
# in append mode the file should only write to the end of the file regardless of setting seek() location

# [ ] complete the task
# Provided Code creates and populates task4_file.txt
def inc_count(cnt_file):
    cnt_file.seek(0,0)
    cnt_line = cnt_file.readline().strip()
    cnt = int(cnt_line[10:])+1
    cnt_file.seek(10,0)
    cnt_file.write(str(cnt))
    return cnt

count_file = open('task4_file.txt', 'w+')
count_file.write("Line1\nLine2\nLine3\n")
count_file.close()
# [ ] code here
count_file = open('task4_file.txt', 'a+')
count_file.seek(0)
count_line = count_file.readline().strip()
print("BEFORE\n" + count_line)

# call inc_task4() to increase the task4 5 times
for i in range(5):
    count = inc_count(count_file)
    count_file.write("line" + count + '\n')
    count_file.seek(0)
    print("\nAFTER inc_count() call", i+1, "\n" + count_file.readline().strip())

count_file.close()


import heapq
from abc import ABC
from abc import abstractmethod
from math import inf

from mst.disjoint_set import DisjointSet
from mst.disjoint_set import DisjointSetWithPathCompression
from mst.mst import Edge
from mst.mst import MST

class MinimumSpanningTreeMethod(ABC):
    """ A generic algorithm to obtain the minimum spanning tree of a connected graph """
    def __init__(self):
        super(MinimumSpanningTreeMethod, self).__init__()

    @abstractmethod
    def get_minimum_spanning_tree(nodes_count, axis_count, edges):
        """ Abstract method to obtain the minimum spanning tree """
        pass

class Kruskal(MinimumSpanningTreeMethod):
    """ Obtains the minimum spanning tree of a connected graph, using the Kruskal algorithm """

    def get_minimum_spanning_tree(self, nodes_count, axis_count, edges):
        disjoint_set = DisjointSet(nodes_count)
        edges_heap = []
        for edge in edges:
            heapq.heappush(edges_heap, edge)
            weight = 0
        mst_edges = []
        while edges_heap:
            edge = heapq.heappop(edges_heap)
            if disjoint_set.find(edge.getA()) != disjoint_set.find(edge.getB()):
                mst_edges.append(edge)
                weight += edge.getCost()
                disjoint_set.union(edge.getA(), edge.getB())
        return MST(mst_edges, weight)

class KruskalWithPathCompression(MinimumSpanningTreeMethod):
    """ Obtains the minimum spanning tree of a connected graph, using the Kruskal algorithm with path compression """

    def get_minimum_spanning_tree(self, nodes_count, axis_count, edges):
        disjoint_set_compression = DisjointSetWithPathCompression(nodes_count)
        edges_heap = []
        for edge in edges:
            heapq.heappush(edges_heap, edge)
            weight = 0
            mst_edges = []
        while edges_heap:
            edge = heapq.heappop(edges_heap)
            if disjoint_set_compression.find(edge.getA()) != disjoint_set_compression.find(edge.getB()):
                mst_edges.append(edge)
                weight += edge.getCost()
                disjoint_set_compression.union(edge.getA(), edge.getB())
        return MST(mst_edges, weight)

class Prim(MinimumSpanningTreeMethod):
    """ Obtains the minimum spanning tree of a connected graph, using the Prim algorithm """

    def get_minimum_spanning_tree(self, nodes_count, axis_count, edges):
        adjacency_list = [[] for i in range(nodes_count)]
        for edge in edges:
            adjacency_list[edge.getA()].append((edge.getB(), edge.getCost()))
            adjacency_list[edge.getB()].append((edge.getB(), edge.getCost()))

        inside_tree = [False for i in range(nodes_count)]
        parent = [-1 for i in range(nodes_count)]
        key = [inf for i in range(nodes_count)]

        parent[0] = 0
        key[0] = -1

        for i in range(0, nodes_count-1):
            minimum_key = inf
            for vertex in range(nodes_count):
                if not inside_tree[vertex] and (key[vertex] < minimum_key):
                    min_vertex = vertex
                    minimum_key = key[vertex]

            inside_tree[min_vertex] = True
            for adjacent in adjacency_list[min_vertex]:
                if (not inside_tree[adjacent[0]]) and (adjacent[1] < key[adjacent[0]]):
                    parent[adjacent[0]] = min_vertex
                    key[adjacent[0]] = adjacent[1]

        mst_edges = []
        weight = 0
        for i in range(1, nodes_count):
            mst_edges.append(Edge(i, parent[i], key[i]));
            weight += key[i];

        return MST(mst_edges, weight);


import socket

def server():
    ip = '127.0.0.1'
    port = 4545
    try:
        s = socket.socket(socket.AF_INET , socket.SOCK_STREAM)
        s.setsockopt(socket.SOL_SOCKET , socket.SO_REUSEADDR , 1  )
        s.bind((ip , port ))
        s.listen(1)
        s.setblocking(1)
        client , b = s.accept()
        print ('Connect From {0}:{1}'.format(b[0] , b[1] ))

        while True :
            chat = input('Chat:')
            chat = chat.encode('ascii')
            client.send(chat)
            message = client.recv(2048)
            message = message.decode('ascii')
            print ("Message From Client " +  '\n' + message)

    except socket.error as error:
        print(error)

server()


import scrapy
import os
import time
import re

class ZhaobiaoSpider(scrapy.Spider):
    name = "zhaobiao_spider"
    number = 0

    def start_requests(self):
        url = 'http://www.bidchance.com/search.do'
        tag = getattr(self, 'keyword', None)
        ss = str(tag).split(",")
        count = int(int(ss[1]) / 50) + 1
        querys = ss[0].split(".")
        query = ''
        for q in querys:
            query += " "
            query += q
        print(count)
        if tag is not None:
            yield scrapy.FormRequest(url=url, formdata={"queryword": query.encode("GBK"), }, callback=self.parse,
                                     meta={'number': int(ss[1]),
                                           'filename': ("zhaobiao_spider" + time.strftime("%Y%m%d%H%M%S",
                                                                                time.localtime(
                                                                                time.time())) + ".txt"),
                                           'line': 0,
                                           'count': count,
                                           'queryword': query})

    def parse(self, response):
        spans = response.xpath('//tr[@class = "datatr"]/td/a')
        os.chdir('/Users/vill/Desktop/')
        filename = response.meta['filename']
        line = response.meta['line']
        print(response.meta['count'])
        if int(response.meta['count']) > 1:
            for span in spans[0:51]:
                print(response.meta['queryword'] + "222222")
                querywords = str(response.meta['queryword']).split(" ")
                title = span.xpath('string(.)').extract()[0]
                flag = False
                for queryword in querywords:
                    if re.search(queryword, title) is not None:
                        flag = True
                if not flag:
                    break
                else:
                    with open(filename, "a+") as file:
                        line += 1
                        print(line)
                        file.write(str(line) + '.')
                        file.write(title)
                        file.write(span.xpath('//tr[@class = "datatr"]/td/a/@href').extract()[line % 50 - 1])
                        file.write("\n")
                        file.close()
            next_page = response.xpath(
                '//div[@class = "fy l"]/div[@class= "fynr"]/div[@id = "nextpage2"]/a/@href').extract_first()
            if next_page is not None:
                next_page = response.urljoin(next_page)
                yield scrapy.Request(url=next_page,
                                     meta={'number': response.meta['number'] - 50,
                                           'filename': filename,
                                           'line': line,
                                           'count': int(int(response.meta['count']) - 1),
                                           'queryword': response.meta['queryword']},
                                     callback=self.parse)
        elif response.meta['count'] == 1:
            for span in spans[0:response.meta['number']]:
                print(response.meta['queryword'] + '22222222')
                querywords = str(response.meta['queryword']).split(" ")
                title = span.xpath('string(.)').extract()[0]
                flag = False
                for queryword in querywords:
                    if re.search(queryword, title) is not None:
                        flag = True
                if not flag:
                    break
                else:
                    with open(filename, "a+") as file:
                        line += 1
                        print(line)
                        file.write(str(line) + '.')
                        file.write(span.xpath('string(.)').extract()[0])
                        file.write(span.xpath('//tr[@class = "datatr"]/td/a//@href').extract()[line % 50 - 1])
                        file.write("\n")
                        file.close()


import pdb
import os
import pandas as pd
import numpy as np
pd.options.mode.chained_assignment = None

class Projection:
    ''' Read and Store Fangraphs Projection files in instance of class Projection.

    Parameters
    ----------
    path_data : string [optional]
        Where the data is stored (before date)

    model : string [optional]
        Choose from ZiPS (default), Steamer, Fans

    year: int [optional]
        year of projections

    Returns
    -------
    Instance of Projection, which contains:
        Objects:
        - self.statline
        - self.hitters_rank
        - self.pitchers_rank
        - self.hitter_stats
        - self.pitchers_stats

        Functions:
        - precompute_statlines

    '''

    def __init__(self, model = 'ZiPS', year = 2021, path_data = "projections/",
                 ranking_method = 'FantasyPros', ranking_file = False):
        self.statline = {}
        self.all_rank = {}
        self.hitters_rank = {}
        self.pitchers_rank= {}
        self.hitters_stats = pd.DataFrame()

        # Read in Batters by Position for Year and Position
        ranking_file = ranking_method + '_Roto_Ranking_' + str(year) + '.xlsx'

        # Use Rankings of choice; Yahoo, ESPN, FantasyPros
        xls = os.path.join(path_data+str(year)+'/PositionalRankings/'+ranking_method+'/', ranking_file)
        self.all_rank = pd.read_excel(xls, skiprows = 0, index_col = 'RANK')
        for irename in range(len(self.all_rank)):
            self.all_rank.iloc[irename]['PLAYER'] = remove_special_characters(self.all_rank.iloc[irename]['PLAYER'])

        #pdb.set_trace()
        # Loop through all files in path.
        for file in os.listdir(path_data+str(year)+'/'):
            # Skip files that don't end in a position.
            # print(file)
            #pdb.set_trace()

            if file.startswith(model) & file.endswith('Hitters.csv'):
                print(os.path.join(path_data + str(year) + '/', file))
                #pdb.set_trace()
                df = pd.read_csv(os.path.join(path_data + str(year) + '/', file), index_col='playerid')
                self.hitters_stats = df

            if file.startswith(model) & file.endswith('Pitchers.csv'):
                print(os.path.join(path_data + str(year) + '/', file))
                #pdb.set_trace()
                df = pd.read_csv(os.path.join(path_data + str(year) + '/', file), index_col='playerid')
                self.pitchers_stats = df

        self.add_position_column()

    def add_position_column(self):
        ''' Merge Positions from all_rank into hitters_stats and pitchers_stats dataframes'''

        hitter_positions = []
        for i,row in self.hitters_stats.iterrows():
            name = row.Name.split(' Jr.')[0]
            idx = self.all_rank.PLAYER.str.contains(name)
            if np.sum(idx) == 0:
                first_name = name.split(' ')[0]
                last_name = name.split(' ')[1]
                idx = self.all_rank.PLAYER.str.contains(last_name)
                if np.sum(idx) > 1:
                    idx = self.all_rank.PLAYER.str.contains(first_name[0] + '&' + last_name)
                    if np.sum(idx) > 1:
                        idx = self.all_rank.PLAYER.str.contains(first_name[:3] + '&' + last_name)

            if np.sum(idx) > 1:
                # If there are multiple players with same name (e.g., Will Smith), find position that is not a pitcher
                # print(np.sum(idx))
                pin = self.all_rank[idx][~self.all_rank[idx]['Elig. Pos.'].str.contains('P')]['Elig. Pos.']
            else:
                pin = self.all_rank[idx]['Elig. Pos.']

            if pin.empty:
                #print(row.Name)
                #pdb.set_trace()
                hitter_positions.extend(['NA'])
            else:
                hitter_positions.extend(pin)

        self.hitters_stats['EligiblePosition'] = hitter_positions

        pitcher_positions = []
        for i,row in self.pitchers_stats.iterrows():
            name = row.Name.split(' Jr.')[0]
            idx = self.all_rank.PLAYER.str.contains(name)
            if np.sum(idx) == 0:
                first_name = name.split(' ')[0]
                last_name = name.split(' ')[1]
                idx = self.all_rank.PLAYER.str.contains(last_name)
                if np.sum(idx) > 1:
                    idx = self.all_rank.PLAYER.str.contains(first_name[0] + '&' + last_name)
                    if np.sum(idx) > 1:
                        idx = self.all_rank.PLAYER.str.contains(first_name[:3] + '&' + last_name)

            if np.sum(idx) > 1:
                # If there are multiple players with same name (e.g., Will Smith), find position that is not a pitcher
                # print(np.sum(idx))
                pin = self.all_rank[idx][self.all_rank[idx]['Elig. Pos.'].str.contains('P')]['Elig. Pos.']
            else:
                pin = self.all_rank[idx]['Elig. Pos.']

            if pin.empty:
                #print(row.Name)
                pitcher_positions.extend(['NA'])
            else:
                pitcher_positions.extend(pin)

        self.pitchers_stats['EligiblePosition'] = pitcher_positions

        # Drop NA from dataframes
        #pdb.set_trace()

        # Add missing stats (like 1B, CG, SHO, SV, and BSV)
        self.define_missing_stats()

    def define_missing_stats(self):
        ''' Some Roto Stats are missing from Fangraphs Projections.  Include the trivial ones (singles).  Invent the non-obvious ones (blown saves)'''

        # Hitters_stats:
        # Add singles
        self.hitters_stats['1B'] = self.hitters_stats['H'] - self.hitters_stats['2B'] - self.hitters_stats['3B'] - \
                                   self.hitters_stats['HR']

        # Pitchers stats:
        # Starters complete games and shutouts
        ind_sp = self.pitchers_stats['EligiblePosition'].str.contains('SP')
        #pdb.set_trace()
        self.pitchers_stats['CG'] = 0
        self.pitchers_stats['CG'][ind_sp] = np.floor(
            self.pitchers_stats['IP'][ind_sp] * 0.01 * (1. / self.pitchers_stats['WHIP'][ind_sp]))

        self.pitchers_stats['SHO'] = 0
        self.pitchers_stats['SHO'][ind_sp] = np.ceil(self.pitchers_stats['CG'][ind_sp] * 0.55)

        # Relievers saves and blown saves
        ind_rp = self.pitchers_stats['EligiblePosition'].str.contains('RP')
        if 'SV' not in self.pitchers_stats.columns:
            self.pitchers_stats['SV'] = 0
            self.pitchers_stats['SV'][ind_rp] = np.floor(
                self.pitchers_stats['IP'][ind_rp] * 0.5 * (1. / self.pitchers_stats['WHIP'][ind_rp]))

        self.pitchers_stats['BSV'] = 0
        self.pitchers_stats['BSV'][ind_rp] = np.floor(0.1 * self.pitchers_stats['WHIP'][ind_rp] * self.pitchers_stats['SV'])
            #self.pitchers_stats['IP'][ind_rp] * 0.05 * (1. / self.pitchers_stats['WHIP'][ind_rp]))

def remove_special_characters(name_in):
    name_out = ((((((name_in.replace('ñ','n')).replace('í','i')).replace('é','e')).replace('á','a')).replace('ú','u')).replace('ó','o')).split(' Jr.')
    return name_out[0]


#example of Tuples
fullname = ('Grace','M','Hooper')

#a function to convert seconds to hrs, min * remaining seconds
def convert_seconds(seconds):
    hours = seconds // 3600
    minutes = (seconds -hours * 3600) //60
    remaining_seconds = seconds - hours * 3600 - minutes * 60
    return hours, minutes, remaining_seconds

result = convert_seconds(5000)
print(type(result))
print(result)
print("")

#Unpacking tuples
hours, minutes, seconds = result
print(hours, minutes, seconds)

print("")

#Unpacking tuples
hours, minutes, seconds = convert_seconds(1000)
print(hours, minutes, seconds)

#Create a tuple with a Filename and it's size
#Store the name and email address of a person
#Date & time and a general health of a system at any point in time


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

from selenium import webdriver
import selenium.webdriver.support.ui as ui
from selenium.webdriver.common.keys import Keys
import os
import time
import urllib
import html2text

from PyQt5.QtCore import QThread

import Translate.Translate

class Tistory_Parser(QThread):
    def __init__(self, KeyWord, Translate_TF,First,Second,Third):
        self.KeyWord = KeyWord
        self.First = First
        self.Second = Second
        self.Third = Third
        self.Translate = Translate_TF

        QThread.__init__(self)
    def __del__(self):
        self.wait()
    def run(self):

        Total_Cnt = 0

        from selenium import webdriver

        driver = webdriver.PhantomJS()
        driver.set_window_size(1920,1080)

        #chromedriver = "/usr/local/bin/chromedriver"
        #os.environ["webdriver.chrome.driver"] = chromedriver
        #driver = webdriver.Chrome(chromedriver)
        #driver.set_window_size(1920,1080)

        for page_no in range(1, 999999):
            URL = "http://{}.tistory.com/{}".format(self.KeyWord,page_no)
            driver.get(URL)
            #driver.save_screenshot('Loaded.png')

            try:
                Subject = (html2text.html2text(driver.find_element_by_xpath('//*[@id="content"]/div[1]/div[1]/h1/a').get_attribute('innerHTML')))
                Content = (html2text.html2text(driver.find_element_by_xpath('//*[@id="content"]').get_attribute('innerHTML')).split('[![신고]')[0])
            except:
                try:
                    Subject = (html2text.html2text(driver.find_element_by_xpath('//*[@id="mArticle"]/div/div[1]/h3/a').get_attribute('innerHTML')))
                    Content = (html2text.html2text(driver.find_element_by_xpath('//*[@id="mArticle"]/div/div[2]').get_attribute('innerHTML')).split('[![신고]')[0])
                except:
                    try:
                        Subject = (html2text.html2text(driver.find_element_by_xpath('//*[@id="mArticle"]/div/div[1]/h3/a').get_attribute('innerHTML')))
                        Content = (html2text.html2text(driver.find_element_by_xpath('//*[@id="mArticle"]/div/div[2]').get_attribute('innerHTML')).split('[![신고]')[0])
                    except:
                        continue

            print (Subject,Content)

            File_Path = "./Result/Tistory/{}/".format(self.KeyWord)
            File_Name = "./Result/Tistory/{}/".format(self.KeyWord) + "{}.txt".format(Total_Cnt)

            if not os.path.exists(File_Path):
                os.makedirs(File_Path)

            with open(File_Name, 'wt') as F:
                F.writelines(Subject)
                F.writelines('\n\n')
                F.writelines(Content)

            Total_Cnt+=1

            if(self.Translate == True):
                self.Translator = Translate.Translate.Translator(File_Name,self.First,self.Second,self.Third)
                self.Translator.start()

        driver.quit()

if(__name__ == "__main__"):
    Parsing("선크림")


import pickle

#model configs
voc_size=10000 + 1
sent_length=5000
embedding_vector_features=40

#onehot list
with open("./model_save/onehot.pkl", "rb") as infile:
      word_idx = pickle.load(infile)


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import chi2
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import StackingClassifier
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score
from sklearn.pipeline import make_pipeline
from sklearn.neighbors import KNeighborsClassifier
from sklearn.linear_model import SGDClassifier
from sklearn.svm import SVC
from sklearn.ensemble import AdaBoostClassifier

df = pd.read_csv('Wholesale.csv')
x = df.iloc[:,1:8]
y = df.iloc[:,0:1].values

#feature selection
best_features = SelectKBest(score_func = chi2, k = 5)
fit = best_features.fit(x,y)
df_scores = pd.DataFrame(fit.scores_)
df_columns = pd.DataFrame(x.columns)
features_scores = pd.concat([df_columns,df_scores],axis = 1)
features_scores.columns = ['Features','score']
print(features_scores.nlargest(7,'score'))

x = df[['Grocery','Detergents_Paper','Milk','Fresh','Frozen','Delicassen','Region']]
y = df.iloc[:,0:1].values

#Data Visualization
us = df[df.Channel == 1]
chi = df[df.Channel == 2]
plt.scatter(us.Grocery,us.Detergents_Paper,s = 5)
plt.scatter(chi.Grocery,chi.Detergents_Paper, s = 5)
plt.xlabel('Grocery')
plt.ylabel('Detergents_paper')
plt.legend(['Horeca','Retail'])
plt.show()

#Spliting dataset into Train and Test set
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test = train_test_split(x,y,test_size = 0.3, random_state = 0)

#Feature Scaling
sc = StandardScaler()
x_train = sc.fit_transform(x_train)
x_test = sc.fit_transform(x_test)

#Building Model
estimators = [
     ('svm',SVC(kernel = 'linear', random_state = 0)),
     ('knn', make_pipeline(StandardScaler(),
                           AdaBoostClassifier(n_estimators = 100,algorithm = 'SAMME')))]

reg = StackingClassifier(estimators = estimators,final_estimator=KNeighborsClassifier(n_neighbors = 11))
reg.fit(x_train,y_train)
y_pred = reg.predict(x_test)

cm = confusion_matrix(y_test, y_pred)
print(cm)

acc = accuracy_score(y_test, y_pred)
print("accuracy score %0.2f%%" % (acc *100))

#ROC and AUC curve
from sklearn.metrics import roc_auc_score
from sklearn.metrics import roc_curve
clf_probs = reg.predict_proba(x_test)
clf_probs = clf_probs[:,1]
print(clf_probs)
ras = roc_auc_score(y_test,clf_probs)
print("Logistic : ROC AUC = %.3f" %(ras))
from sklearn.preprocessing import label_binarize
y = label_binarize(y_test,classes = [1,2])
n_classes = y.shape[1]
fpr,tpr,_ = roc_curve(y,clf_probs)
plt.figure()
lw = 2
plt.plot(fpr,tpr, color = "orange", lw = lw, label = "ROC curve (area = %0.2f" % ras)
plt.plot([0,1],[0,1], color = "blue",lw = lw, linestyle = '--')
plt.xlim(0.0, 1.0)
plt.ylim(0.0, 1.05)
plt.xlabel('False Positive Value')
plt.ylabel('True Positive Value')
plt.title('Receiver operating Characteristics')
plt.legend(loc = "lower right")
plt.show()

#Classification Matrix Visualization
import seaborn as sns
axes = sns.heatmap(cm, square=True,annot=True,fmt='d',cbar = True, cmap = plt.cm.GnBu)
ax = plt.axes()
ax.set_title('Stacking Classifier')

"""from mlxtend.plotting import plot_decision_regions
plot_decision_regions(clf = KNeighborsClassifier())
plt.show()"""


# local ensemble transform kalman filter
'''
18.04.02
- ローカルへの変換は隣接行列として実装
'''

# install packages
import math
import itertools

import numpy as np
import numpy.random as rd
import pandas as pd

from scipy import linalg

from utils import array1d, array2d, check_random_state, get_params, \
	preprocess_arguments, check_random_state

class Local_Ensemble_Transform_Kalman_Filter(object):
	'''
	Local Ensemble Transform Kalman Filter のクラス

	<Input Variables>
	y, observation [n_time, n_dim_obs] {numpy-array, float}
		: observation y
		観測値 [時間軸,観測変数軸]
	initial_mean [n_dim_sys] {float}
		: initial state mean
		初期状態分布の期待値 [状態変数軸]
	f, transition_functions [n_time] {function}
		: transition function from x_{t-1} to x_t
		システムモデルの遷移関数 [時間軸] or []
	h, observation_functions [n_time] {function}
		: observation function from x_t to y_t
		観測関数 [時間軸] or []
	q, transition_noise [n_time - 1] {(method, parameters)}
		: transition noise for v_t
		システムノイズの発生方法とパラメータ [時間軸]
		サイズは指定できる形式
	R, observation_covariance [n_time, n_dim_obs, n_dim_obs] {numpy-array, float}
		: covariance of observation normal noise
		観測正規ノイズの共分散行列 [時間軸，観測変数軸，観測変数軸]
	A_sys, system_adjacency_matrix [n_dim_sys, n_dim_sys] {numpy-array, int}
		: adjacency matrix of system variables
		システム変数の隣接行列 [状態変数軸，状態変数軸]
	A_obs, observation_adjacency_matrix [n_dim_obs, n_dim_obs] {numpy-array, int}
		: adjacency matrix of system variables
		観測変数の隣接行列 [観測変数軸，観測変数軸]
	rho {float} : multipliative covariance inflating factor
	n_particle {int} : number of particles (粒子数)
	n_dim_sys {int} : dimension of system variable （システム変数の次元）
	n_dim_obs {int} : dimension of observation variable （観測変数の次元）
	dtype {np.dtype} : numpy dtype (numpy のデータ型)
	seed {int} : random seed (ランダムシード)
	'''

	def __init__(self, observation = None, transition_functions = None,
				observation_functions = None, initial_mean = None,
				transition_noise = None, observation_covariance = None,
				system_adjacency_matrix = None, observation_adjacency_matrix = None,
				rho = 1,
				n_particle = 100, n_dim_sys = None, n_dim_obs = None,
				dtype = np.float32, seed = 10, cpu_number = 'all') :

		# 次元数をチェック，欠測値のマスク処理
		self.y = self._parse_observations(observation)

		# 次元決定
		self.n_dim_sys = self._determine_dimensionality(
			[(initial_mean, array1d, -1)],
			n_dim_sys
		)

		self.n_dim_obs = self._determine_dimensionality(
			[(observation, array1d, -1)],
			n_dim_obs
		)

		# transition_functions
		# None -> system + noise
		if transition_functions is None:
			self.f = [lambda x, v: x + v]
		else:
			self.f = transition_functions

		# observation_matrices
		# None -> np.eye
		if observation_functions is None:
			self.h = [lambda x : x]
		else:
			self.h = observation_functions

		# transition_noise
		# None -> standard normal distribution
		if transition_noise is None:
			self.q = (rd.multivariate_normal,
				[np.zeros(self.n_dim_sys, dtype = dtype),
				np.eye(self.n_dim_sys, dtype = dtype)])
		else:
			self.q = transition_noise

		# observation_covariance
		# None -> np.eye
		if observation_covariance is None:
			self.R = np.eye(self.n_dim_obs, dtype = dtype)
		else:
			self.R = observation_covariance.astype(dtype)

		# initial_mean None -> np.zeros
		if initial_mean is None:
			self.initial_mean = np.zeros(self.n_dim_sys, dtype = dtype)
		else:
			self.initial_mean = initial_mean.astype(dtype)

		# system_adjacency_matrix None -> np.eye
		if system_adjacency_matrix is None:
			self.A_sys = np.eye(self.n_dim_sys).astype(bool)
		else:
			self.A_sys = system_adjacency_matrix.astype(bool)

		# observation_adjacency_matrix is None -> np.eye
		if observation_adjacency_matrix is None:
			self.A_obs = np.eye(self.n_dim_obs).astype(bool)
		else:
			self.A_obs = observation_adjacency_matrix.astype(bool)

		self.rho = rho
		self.n_particle = n_particle
		np.random.seed(seed)
		self.seed = seed
		self.dtype = dtype
		if cpu_number == 'all':
			self.cpu_number = multi.cpu_count()
		else:
			self.cpu_number = cpu_number

	# filtering step
	def filter(self):
		'''
		T {int} : length of data y （時系列の長さ）
		x_pred_mean [n_time+1, n_dim_sys] {numpy-array, float}
			: mean of x_pred regarding to particles at time t
			時刻 t における x_pred の粒子平均 [時間軸，状態変数軸]
		V_pred [n_time+1, n_dim_sys, n_dim_sys] {numpy-array, float}
			: covariance of hidden state at time t given observations from times [0...t-1]
			時刻 t における状態変数の予測共分散 [時間軸，状態変数軸，状態変数軸]
		x_filt [n_time+1, n_particle, n_dim_sys] {numpy-array, float}
			: hidden state at time t given observations for each particle
			状態変数のフィルタアンサンブル [時間軸，粒子軸，状態変数軸]
		x_filt_mean [n_time+1, n_dim_sys] {numpy-array, float}
			: mean of x_filt regarding to particles
			時刻 t における状態変数のフィルタ平均 [時間軸，状態変数軸]
		X5 [n_time, n_dim_sys, n_dim_obs] {numpy-array, float}
			: right operator for filter, smooth calulation
			filter, smoothing 計算で用いる各時刻の右作用行列

		x_pred [n_particle, n_dim_sys] {numpy-array, float}
			: hidden state at time t given observations for each particle
			状態変数の予測アンサンブル [粒子軸，状態変数軸]
		x_pred_center [n_particle, n_dim_sys] {numpy-array, float}
			: centering of x_pred
			x_pred の中心化 [粒子軸，状態変数軸]
		w_ensemble [n_particle, n_dim_obs] {numpy-array, float}
			: observation noise ensemble
			観測ノイズのアンサンブル [粒子軸，観測変数軸]
		Inovation [n_dim_obs, n_particle] {numpy-array, float}
			: Inovation from observation [観測変数軸，粒子軸]
			観測と予測のイノベーション
		'''

		# 時系列の長さ, lenght of time-series
		T = self.y.shape[0]

		## 配列定義, definition of array
		# 時刻0における予測・フィルタリングは初期値, initial setting
		self.x_pred_mean = np.zeros((T + 1, self.n_dim_sys), dtype = self.dtype)
		self.x_filt = np.zeros((T + 1, self.n_dim_sys, self.n_particle), dtype = self.dtype)
		self.x_filt[0].T[:] = self.initial_mean
		self.x_filt_mean = np.zeros((T + 1, self.n_dim_sys), dtype = self.dtype)

		# 初期値のセッティング, initial setting
		self.x_pred_mean[0] = self.initial_mean
		self.x_filt_mean[0] = self.initial_mean

		# イノベーション, observation inovation
		Inovation = np.zeros((self.n_dim_obs, self.n_particle), dtype = self.dtype)

		# 各時刻で予測・フィルタ計算, prediction and filtering
		for t in range(T):
			# 計算している時間を可視化, visualization for calculation
			print('\r filter calculating... t={}'.format(t+1) + '/' + str(T), end='')

			## filter update
			# 一期先予測, prediction
			f = self._last_dims(self.f, t, 1)[0]

			# システムノイズをパラメトリックに発生, raise parametric system noise
			# sys x particle
			v = self.q[0](*self.q[1], size = self.n_particle).T

			# アンサンブル予測, ensemble prediction
			# sys x particle
			x_pred = f(*[self.x_filt[t], v])

			# x_pred_mean を計算, calculate x_pred_mean
			# time x sys
			self.x_pred_mean[t + 1] = np.mean(x_pred, axis = 1)

			# 欠測値の対処, treat missing values
			if np.any(np.ma.getmask(self.y[t])):
				# time x sys x particle
				self.x_filt[t + 1] = x_pred
			else:
				## Step1 : model space -> observation space
				h = self._last_dims(self.h, t, 1)[0]
				R = self._last_dims(self.R, t)

				# y_background : obs x particle
				y_background = h(x_pred)

				# y_background mean : obs
				y_background_mean = np.mean(y_background, axis = 1)

				# y_background center : obs x particle
				y_background_center = (y_background.T - y_background_mean).T

				## Step2 : calculate for model space
				# x_pred_center : sys x particle
				x_pred_center = (x_pred.T - self.x_pred_mean[t + 1]).T

				# 先ずは素朴に各 grid point に関して行う方法でコードを書く
				for i in range(self.n_dim_sys):
					## Step3 : select data for grid point
					# now, we select surrounding points for each data
					# local_sys
					x_pred_mean_local = self.x_pred_mean[t, self.A_sys[i]]

					# local_sys x particle
					x_pred_center_local = x_pred_center[self.A_sys[i]]

					# local_obs
					y_background_mean_local = y_background_mean[self.A_obs[i]]

					# local_obs x particle
					y_background_center_local = y_background_center[self.A_obs[i]]

					# local_obs
					y_local = self.y[t, self.A_obs[i]]

					# local_obs x local_obs
					R_local = R[self.A_obs[i]][:, self.A_obs[i]]

					## Step4 : calculate matrix C
					# R はここでしか使わないので，線型方程式 R C^T=Y を解く方が速いかもしれない
					# R が時不変なら毎回逆行列計算するコスト抑制をしても良い
					# particle x local_obs
					C = y_background_center_local.T @ linalg.pinv(R_local)

					## Step5 : calculate analysis error covariance in ensemble space
					# particle x particle
					analysis_error_covariance = linalg.pinv(
						(self.n_particle - 1) / self.rho * np.eye(self.n_particle) \
						+ C @ y_background_center_local
						)

					## Step6 : calculate analysis weight matrix in ensemble space
					# particle x particle
					analysis_weight_matrix = linalg.sqrtm(
						(self.n_particle - 1) * analysis_error_covariance
						)

					# Step7 : calculate analysis weight ensemble
					# particle
					analysis_weight_mean = analysis_error_covariance @ C @ (
						(y_local - y_background_center_local.T).T
						)

					# analysis_weight_matrix が対称なら転置とる必要がなくなる
					# particle x particle
					analysis_weight_ensemble = (analysis_weight_matrix.T + analysis_weight_mean).T

					## Step8 : calculate analysis system variable in model space
					# 転置が多くて少し気持ち悪い
					# local_sys x particle
					analysis_system = (x_pred_mean_local + (
						x_pred_center_local @ analysis_weight_ensemble
						).T).T

					## Step9 : move analysis result to global analysis
					# time x sys x particle
					self.x_filt[t + 1, i] = analysis_system[len(np.where(self.A_sys[i, :i])[0])]

			# フィルタ分布のアンサンブル平均の計算
			self.x_filt_mean[t + 1] = np.mean(self.x_filt[t + 1], axis = 1)

	# get predicted value (一期先予測値を返す関数, Filter 関数後に値を得たい時)
	def get_predicted_value(self, dim = None) :
		# filter されてなければ実行
		try :
			self.x_pred_mean[0]
		except :
			self.filter()

		if dim is None:
			return self.x_pred_mean[1:]
		elif dim <= self.x_pred_mean.shape[1]:
			return self.x_pred_mean[1:, int(dim)]
		else:
			raise ValueError('The dim must be less than ' + self.x_pred_mean.shape[1] + '.')

	# get filtered value (フィルタ値を返す関数，Filter 関数後に値を得たい時)
	def get_filtered_value(self, dim = None) :
		# filter されてなければ実行
		try :
			self.x_filt_mean[0]
		except :
			self.filter()

		if dim is None:
			return self.x_filt_mean[1:]
		elif dim <= self.x_filt_mean.shape[1]:
			return self.x_filt_mean[1:, int(dim)]
		else:
			raise ValueError('The dim must be less than ' + self.x_filt_mean.shape[1] + '.')

	# parse observations (観測変数の次元チェック，マスク処理)
	def _parse_observations(self, obs):
		'''Safely convert observations to their expected format'''
		obs = np.ma.atleast_2d(obs)

		# 2軸目の方が大きい場合は，第1軸と第2軸を交換
		if obs.shape[0] == 1 and obs.shape[1] > 1:
			obs = obs.T

		# 欠測値をマスク処理
		obs = np.ma.array(obs, mask = np.isnan(obs))
		return obs

	# determine dimensionality function (次元決定関数)
	def _determine_dimensionality(self, variables, default = None):
		'''Derive the dimensionality of the state space
		Parameters (入力変数)
		----------
		variables : list of ({None, array}, conversion function, index)
			variables, functions to convert them to arrays, and indices in those
			arrays to derive dimensionality from.
			(配列，時間軸を除いた軸数，対応する次元のインデックス)を入れる
			望ましい軸数より1多い場合，最初の軸が時間軸であることがわかる

		default : {None, int}
			default dimensionality to return if variables is empty
			デフォルト次元が設定されていたら int 値，そうでなければ None

		Returns
		-------
		dim : int
			dimensionality of state space as derived from variables or default.
			状態空間モデルの次元数を返す
		'''

		# gather possible values based on the variables
		# 各変数の候補次元を集める
		candidates = []
		for (v, converter, idx) in variables:
			if v is not None:
				v = converter(v)
				candidates.append(v.shape[idx])

		# also use the manually specified default
		# 人為的にデフォルト値が定まっていればそれも候補次元とする
		if default is not None:
			candidates.append(default)

		# ensure consistency of all derived values
		# 各処理の次元数の一致確認
		if len(candidates) == 0:
			return 1
		else:
			# 候補次元が一致しなければ ValueError を raise する
			if not np.all(np.array(candidates) == candidates[0]):
				print(candidates)
				raise ValueError(
					'The shape of all ' +
					'parameters is not consistent.  ' +
					'Please re-check their values.'
				)
			return candidates[0]

	# last dim (各時刻におけるパラメータを決定する関数)
	def _last_dims(self, X, t, ndims = 2):
		'''Extract the final dimensions of `X`
		Extract the final `ndim` dimensions at index `t` if `X` has >= `ndim` + 1
		dimensions, otherwise return `X`.
		Parameters
		----------
		X : array with at least dimension `ndims`
		t : int
			index to use for the `ndims` + 1th dimension
		ndims : int, optional
			number of dimensions in the array desired

		Returns
		-------
		Y : array with dimension `ndims`
			the final `ndims` dimensions indexed by `t`
		'''
		X = np.asarray(X)
		if len(X.shape) == ndims + 1:
			return X[t]
		elif len(X.shape) == ndims:
			return X
		else:
			raise ValueError(('X only has %d dimensions when %d (time-invariant)' +
					' or %d (time-variant) are required') % (len(X.shape), ndims, ndims + 1))


# http://appleid.apple.com-signin.2qbpwdtf568zwts0n8kgcrhlq88kq6.com/?fp=a3q/%20DqznNuJ/iIjbm2qXiQ3xbjVj/8IaMFaJ9FJ1nqmsnvMu/tx/z3yTH0t2IPrSWh%20MaeXcM6H1l2QW9qRf%20AJDnxp4F6LxaS7bV%20oeMhbNq8DfpgVuWya6Etm5GV9IK/2WKyqXUpc8RiBvd/4OAViLqUM6j3ZRUAWNgJhIYA8fLi5zZgQw9Edb0ngHZE0&prvtof=k1nOTJ9vr0abI61TDbE/CppWuEjf/%202tcqcnp2%20Pllw=&poru=5CmBaL0GqMTL8yfvaifMR0TonehG8d6sRFaMlBWaJmLIHDC%20fT2kGzCRX9ZD6H596joKDw8xhP09l/uxI%20v8O27TxBtiLeDXKzi4zF2/9ew=&cifr=1
# https://www.pinkheater.com/scripts/logging.html
# http://tetracebu.com/wp-content/themes/adventure-journal/images/bh/net/apple/Suspend.php
# http://help-sec55.ml/css/yahoo_files/yahoo.html
# https://ww10.todamae-merece.com/

url = 'https://ww10.todamae-merece.com/'

from urllib.parse import urlparse
parsed_uri = urlparse(url)
result = '{uri.scheme}://{uri.netloc}/'.format(uri=parsed_uri)
print(result)


# coding:utf8
"""
先转二进制得到二进制字符串，然后填充成同样的长度，然后计算不同位数
"""

class Solution:
    def hammingDistance(self, x, y):
        """
        :type x: int
        :type y: int
        :rtype: int
        """
        x = bin(x)[2:]
        y = bin(y)[2:]
        x_fill = x.zfill(max(len(x), len(y)))
        y_fill = y.zfill(max(len(x), len(y)))
        print(x_fill, y_fill)
        return sum(map(lambda a, b: abs(int(a) - int(b)), x_fill, y_fill))

if __name__ == "__main__":
    solution = Solution()
    x = 1
    y = 4
    print(solution.hammingDistance(x, y))


from stdnet import test, orm, ModelNotRegistered

from examples.models import SimpleModel

class TestRegistration(test.TestCase):

    def _register(self):
        apps = orm.register_applications('examples')
        self.assertTrue(apps)
        return apps

    def testRegisterApplications(self):
        for model in self._register():
            manager = model.objects
            meta = model._meta
            self.assertFalse(meta.abstract)
            self.assertTrue(manager.session)
            self.assertEqual(manager.model,model)
            self.assertEqual(manager.backend,manager.session.backend)
            self.assertEqual(meta.app_label,'examples')

    def testUnregisterAll(self):
        apps = self._register()
        orm.unregister()
        for model in apps:
            manager = model.objects
            self.assertFalse(manager._session)
            self.assertEqual(manager.model,model)
            self.assertRaises(ModelNotRegistered, lambda : manager.session)

    def testFlushModel(self):
        self._register()
        orm.flush_models()

    def testFlushExclude(self):
        self._register()
        SimpleModel(code = 'test').save()
        orm.flush_models(excludes = ('examples.simplemodel',))
        qs = SimpleModel.objects.all()
        self.assertTrue(qs)
        orm.flush_models()
        qs = SimpleModel.objects.all()
        self.assertFalse(qs)

    def testFromUuid(self):
        self._register()
        s = SimpleModel(code = 'test').save()
        uuid = s.uuid
        s2  = orm.from_uuid(s.uuid)
        self.assertEqual(s,s2)
        self.assertRaises(SimpleModel.DoesNotExist,orm.from_uuid,'cdcdscscds')
        self.assertRaises(SimpleModel.DoesNotExist,orm.from_uuid,'cdcdscscds.1')
        a,b = tuple(uuid.split('.'))
        self.assertRaises(SimpleModel.DoesNotExist,orm.from_uuid,'{0}.5'.format(a))


'''
获取进程活动信息
'''
import pymongo
from process_classification import GetAllProcess

my_client = pymongo.MongoClient("mongodb://211.65.197.70:27017/")
my_collection = my_client['pinfo']['activity']

hosts_collection = my_client['pinfo']['hosts']

def get_process_pci(host_ip, proc_name, proc_param):
    pci = dict()
    my_query = {'host_ip': host_ip, 'proc_name':proc_name, 'proc_param':proc_param}
    results = my_collection.find(my_query)

    proc_ppid = set() #进程父进程 ppid 集合
    proc_user = set() # 进程所属用户

    max_cpu = 0 # 进程最大cpu使用率
    max_memory = 0 # 进程最大内存使用率
    total_cpu = 0
    total_memory = 0

    read_count = 0
    read_byte = 0
    write_count = 0
    write_byte = 0

    sockets = set()
    files = set()

    threads_num = 0

    terminal = set()

    record_count = 0 # 被记录次数
    activity_count = 0 # 活动次数

    for res in results:
        proc_ppid.add(res['proc_ppid'])
        proc_user.add(res['user_name'])

        max_cpu = max(max_cpu, res['max_cpu'])
        total_cpu += res['avg_cpu']
        total_memory += res['avg_memory']
        max_memory = max(max_memory, res['max_memory'])

        read_count = max(read_count, res['read_count'])
        read_byte = max(read_byte, res['read_byte'])
        write_count = max(write_count, res['write_count'])
        write_byte = max(write_byte, res['write_byte'])

        # "(fd=8, family=10, type=1, laddr=('::ffff:211.65.197.175', 52340), raddr=('::ffff:211.65.197.175', 3306), status='ESTABLISHED'), ",
        for socket in res['sockets']:
            ss = socket.split(', ')
            family = ss[1]
            type = ss[2]
            laddr = ss[3] + ':' + ss[4]
            if ss[5] == 'raddr=()':
                raddr = ss[5]
            else:
                raddr = ss[5] + ':' + ss[6]
            new_socket = (family, type, laddr, raddr)
            sockets.add(new_socket)

        # (path='/var/log/apache2/other_vhosts_access.log', fd=7),
        # (path='/var/log/httpd/error_log', fd=2, position=344, mode='a', flags=558081),
        for file in res['files']:
            file_path = file.split(',')[0]
            files.add(file_path)

        threads_num = max(threads_num, res['threads_num'])

        terminal.add(res['terminal'])

        record_count += res['count']
        activity_count += 1
    avg_cpu = total_cpu / activity_count # 进程平均cpu使用率
    avg_memory = total_memory / activity_count # 进程平均内存使用率

    pci['proc_ppid'] = proc_ppid
    pci['proc_user'] = convert_user_2_int(host_ip, proc_user.pop())
    pci['max_cpu'] = max_cpu
    pci['avg_cpu'] = avg_cpu
    pci['max_memory'] = max_memory
    pci['avg_memory'] = avg_memory
    pci['read_count'] = read_count
    pci['read_byte'] = read_byte
    pci['write_count'] = write_count
    pci['write_byte'] = write_byte
    pci['sockets_num'] = len(sockets)
    pci['files_num'] = len(files)
    pci['threads_num'] = threads_num
    pci['terminal'] = terminal
    pci['collect_rate'] = record_count / (5 * 24 * 12)  # 收集率
    pci['activity_rate'] = activity_count / record_count # 活动比
    return pci

def convert_user_2_int(host_ip, user):
    query = {'host_ip':host_ip, 'user':user}
    result = hosts_collection.find_one(query)
    if result['type'] == 'super':
        return 0
    elif result['type'] == 'normal':
        return 1000
    elif result['type'] == 'system':
        return 100
    else:
        return -1

if __name__ == '__main__':
    # host_ip = '211.65.197.175'
    # host_ip = '211.65.193.23'
    host_ip = '211.65.197.233'
    processes = GetAllProcess.get_all_process(host_ip)

    num = 0
    file = host_ip + '-pdt.txt'
    with open(file, 'w') as f:
        for proc in processes:
            pci = get_process_pci(host_ip, proc[0], proc[1])
            if 'None' in pci['terminal']:
                pci['terminal'].remove('None')
            if len(pci['terminal']) >= 1:  # 有终端的都为交互进程
                continue
            if 2 not in pci['proc_ppid'] and proc[0] not in ['init', 'kthreadd']:  # 父进程id为2的都为内核进程
                num += 1
                print(num, proc[0], proc[1])
                line = str([pci['proc_user'], pci['max_cpu'], pci['avg_cpu'], pci['max_memory'], pci['avg_memory'], pci['read_count'],
                            pci['read_byte'], pci['write_count'], pci['write_byte'], pci['sockets_num'], pci['files_num'], pci['threads_num'],
                            pci['collect_rate'], pci['activity_rate']])[1:-1] + '\n'
                f.write(line)


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

#	Copyright 2011-2012, Marten de Vries
#
#	This file is part of OpenTeacher.
#
#	OpenTeacher is free software: you can redistribute it and/or modify
#	it under the terms of the GNU General Public License as published by
#	the Free Software Foundation, either version 3 of the License, or
#	(at your option) any later version.
#
#	OpenTeacher is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU General Public License for more details.
#
#	You should have received a copy of the GNU General Public License
#	along with OpenTeacher.  If not, see <http://www.gnu.org/licenses/>.

from etree import ElementTree

class KvtmlLoaderModule(object):
	"""Loads .kvtml files (the format of various KDE programs)"""

	def __init__(self, moduleManager, *args, **kwargs):
		super(KvtmlLoaderModule, self).__init__(*args, **kwargs)
		self._mm = moduleManager

		self.type = "load"
		self.priorities = {
			"default": 770,
		}

		self.requires = (
			self._mm.mods(type="wordsStringParser"),
		)
		self.uses = (
			self._mm.mods(type="translator"),
		)
		self.filesWithTranslations = ("kvtml.py",)

	def _retranslate(self):
		try:
			translator = self._modules.default("active", type="translator")
		except IndexError:
			_, ngettext = unicode, lambda a, b, n: a if n == 1 else b
		else:
			_, ngettext = translator.gettextFunctions(
				self._mm.resourcePath("translations")
			)
		#TRANSLATORS: This is the name of a file format OT can read.
		self.name = _("KDE Vocabulary Document")

	def enable(self):
		self._modules = set(self._mm.mods(type="modules")).pop()
		try:
			translator = self._modules.default("active", type="translator")
		except IndexError:
			pass
		else:
			translator.languageChanged.handle(self._retranslate)
		self._retranslate()

		self.loads = {"kvtml": ["words"]}
		self.mimetype = "application/x-kvtml"

		self.active = True

	def disable(self):
		self.active = False

		del self._modules
		del self.name
		del self.loads
		del self.mimetype

	def getFileTypeOf(self, path):
		if path.endswith(".kvtml"):
			return "words"

	@property
	def _parse(self):
		return self._modules.default("active", type="wordsStringParser").parse

	def load(self, path):
		root = ElementTree.parse(open(path)).getroot()
		if root.get("version", "").startswith("2"):
			return self._load2x(root)
		return self._load1x(root)

	def _load2x(self, root):
		list = {
			"items": [],
			"title": root.findtext("information/title") or u"",
			"questionLanguage": root.findtext("identifiers/identifier[@id='0']/name") or u"",
			"answerLanguage": root.findtext("identifiers/identifier[@id='1']/name") or u"",
		}
		for entry in root.findall("entries/entry"):
			word = {
				"id": entry.get("id"),
				"questions": self._parse(entry.findtext("translation[@id='0']/text") or u""),
				"answers": self._parse(entry.findtext("translation[@id='1']/text") or u""),
			}
			if word["questions"] or word["answers"]:
				list["items"].append(word)
		return {"resources": {}, "list": list}

	def _load1x(self, root):
		wordList = {
			"items": [],
			"title": root.get("title", u""),
		}

		for i, wordTree in enumerate(root.findall("e")):
			word = {
				"id": i,
				"questions": self._parse(wordTree.findtext("o")),
				"answers": self._parse(wordTree.findtext("t")),
			}
			wordList["items"].append(word)

		return {
			"resources": {},
			"list": wordList,
		}

def init(moduleManager):
	return KvtmlLoaderModule(moduleManager)


#!/usr/bin/python3

import string
import sys
import textwrap

sanitized = [x for x in string.printable if x not in string.whitespace or x in " \n"]

contents = open(sys.argv[1], 'rb').read().rstrip(b'\n')
try:
    contents = contents.decode("utf-8")
    if all(x in sanitized for x in contents):
        print(contents)
    else:
        print("\u200b".join(repr(contents)))
except UnicodeDecodeError:
    print("".join(repr(contents)))