#
# Added Tkinter interface Chuck Duey 8/20/2015
# Dual LED 9/24/2016 Chuck Duey
# Import the modules
import RPi.GPIO as GPIO
import time
from Tkinter import *

# Define our statics
ON = 1
OFF = 0
# Define our pin usage:
button1 = 24
button2 = 25
led1 = 4
led2 = 22
# Set blink rate for global in ms and blank state
led_pwm = 50
led_state = OFF

#Set up the GPIO ports
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(button1, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(button2, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(led1, GPIO.OUT)
GPIO.setup(led2, GPIO.OUT)
led1_pwm = GPIO.PWM(led1,400)
led2_pwm = GPIO.PWM(led2,400)
led1_pwm.start(led_pwm)
led2_pwm.start(led_pwm)

def pwmChange(channel):
     global led_pwm,clock
     if channel == button1:
          if led_pwm > 0:
              led_pwm -= 5
     if channel == button2:
          if led_pwm < 100:
              led_pwm += 5
     led1_pwm.ChangeDutyCycle(led_pwm)
     led2_pwm.ChangeDutyCycle(100-led_pwm)
     clock.config(text="  LED PWM% = "+str(led_pwm))
# Define Tk root
root = Tk ()
root.title("PWM LED")
clock = Label(root, font=('times', 20, 'bold'),bg='green')
clock.pack(fill=BOTH, expand=1)
clock.config(text="  LED PWM% = "+str(led_pwm))

# Main
# Set up events
GPIO.add_event_detect(button1, GPIO.RISING, callback=pwmChange, bouncetime=300)
GPIO.add_event_detect(button2, GPIO.RISING, callback=pwmChange, bouncetime=300)
# Start main loop
root.mainloop( )
# Clean up IO ports return to normal
GPIO.cleanup()


from pyzbar.pyzbar import decode
from PIL import Image
import pyqrcode
import colorama
from colorama import Fore, Style
colorama.init()

opts = ["encoder", "decoder"]
ForeColor = ["green", "blue"]
ForeColor.sort()
BackColor = ["green", "blue"]
BackColor.sort()
Danger = "WARNING: If the background is not default the QR-Code will not be able to get decoded afterwards" + """
because of the missing transparency"""
def encode():
    Code = input("Input for Code content:\n>> ")
    File = input("Enter output file name:\n>> ")
    print(Fore.GREEN)
    print("Texture colors:" + Fore.LIGHTMAGENTA_EX)
    for i in ForeColor:
        print(i)
    ModuleColor = input("Enter Texture color:\n>> ")
    if ModuleColor == ForeColor[0]:
        print(Fore.MAGENTA + "color >> " + Fore.LIGHTBLUE_EX + ForeColor[0] + Fore.RESET)
        module_color = (0, 0, 255)
    elif ModuleColor == ForeColor[1]:
        print(Fore.MAGENTA + "color >> " + Fore.LIGHTGREEN_EX + ForeColor[1] + Fore.RESET)
        module_color = (0, 255, 0)
    else:
        print(Fore.RED + Style.BRIGHT + "unknown color" + "\n" + Style.RESET_ALL + Fore.LIGHTGREEN_EX + "selecting Default:'black'" + Fore.RESET)
        module_color = (0, 0, 0, 255)
    print(Fore.GREEN)
    print(Fore.LIGHTMAGENTA_EX + "Background colors:" )
    print(Fore.RED + Danger + Fore.RESET + Fore.LIGHTMAGENTA_EX)
    for i in BackColor:
        print(i)
    print("for transparent default background press ENTER")
    BackgroundColor = input("Enter Background color:\n>> ")
    if BackgroundColor == BackColor[0]:
        print(Fore.MAGENTA + "color >> " + Fore.LIGHTBLUE_EX + BackColor[0] + Fore.RESET)
        background = (0, 0, 255)
    elif BackgroundColor == BackColor[1]:
        print(Fore.MAGENTA + "color >> " + Fore.LIGHTGREEN_EX + BackColor[1] + Fore.RESET)
        background = (0, 255, 0)
    else:
        print(Fore.RED + Style.BRIGHT + "unknown color" + "\n" + Style.RESET_ALL + Fore.LIGHTGREEN_EX + "selecting Default:'white'[transparent]" + Fore.RESET)
        background = (255, 255, 255, 255)

    fileformat = str(File) + ".png"
    QR = pyqrcode.create(str(Code))
    QR.png(fileformat, scale=8, module_color=module_color, background=background)
    print(Fore.LIGHTGREEN_EX + "Process finfished" + "\n" + Fore.BLUE +"output file: " + Fore.RED + fileformat + Fore.RESET)

def pic():
    print(Fore.CYAN)
    print("""

     QQQQQQQQQ     RRRRRRRRRRRRRRRRR
   QQ:::::::::QQ   R::::::::::::::::R
 QQ:::::::::::::QQ R::::::RRRRRR:::::R
Q:::::::QQQ:::::::QRR:::::R     R:::::R
Q::::::O   Q::::::Q  R::::R     R:::::R
Q:::::O     Q:::::Q  R::::R     R:::::R
Q:::::O     Q:::::Q  R::::RRRRRR:::::R
Q:::::O     Q:::::Q  R:::::::::::::RR
Q:::::O     Q:::::Q  R::::RRRRRR:::::R
Q:::::O     Q:::::Q  R::::R     R:::::R
Q:::::O  QQQQ:::::Q  R::::R     R:::::R
Q::::::O Q::::::::Q  R::::R     R:::::R
Q:::::::QQ::::::::QRR:::::R     R:::::R
 QQ::::::::::::::Q R::::::R     R:::::R
   QQ:::::::::::Q  R::::::R     R:::::R
     QQQQQQQQ::::QQRRRRRRRR     RRRRRRR
             Q:::::Q
              QQQQQQ

    """)
    print(Fore.MAGENTA)
    print(Style.BRIGHT)
    print("QR operating Options:")
    for option in opts:
        print("'" + option + "'")
    print(Style.NORMAL)
    print(Fore.RESET)

pic()
X = input(">>")
if X == 'encoder':
    encode()
elif X == 'decoder':
    File = input("Enter File Name:\n>> ")
    try:

        content = decode(Image.open(str(File)))

        if content == []:
            print(Fore.RED)
            print("Es wurde keine QR - Information gefunden")
            print(Fore.RESET)
        else:
            print(Fore.GREEN)
            print("Content found:")
            print("QR content:")
            print(content)
            print(Fore.RESET)

    except:
        print(Fore.YELLOW)
        print("File does not exist")
        print(Fore.RESET)

else:
    print(Fore.RED)
    print("unknown command")
    print(Fore.RESET)


##############################################
## Author: I-No Liao                        ##
## Date of update: 2018/04/10               ##
## Description: Leetcode #100               ##
##############################################

# Given two binary trees, write a function to check if they are the same or not.
#
# Two binary trees are considered the same if they are structurally identical and the nodes have the same value.
#
#
# Example 1:
#
# Input:     1         1
#           / \       / \
#          2   3     2   3
#
#         [1,2,3],   [1,2,3]
#
# Output: true
# Example 2:
#
# Input:     1         1
#           /           \
#          2             2
#
#         [1,2],     [1,null,2]
#
# Output: false
# Example 3:
#
# Input:     1         1
#           / \       / \
#          2   1     1   2
#
#         [1,2,1],   [1,1,2]
#
# Output: false

# Definition for a binary tree node.
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution:
    # @param p, TreeNode
    # @param q, TreeNode
    # @return boolean
    def isSameTree(self, p, q):
        if p is None and q is None:
            return True
        elif p is None or q is None:
            return False
        return p.val == q.val and self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)

# Main
if __name__ == '__main__':
    n0 = TreeNode(1)
    n1 = TreeNode(2)
    n2 = TreeNode(3)
    n0.left, n0.right = n1, n2

    m0 = TreeNode(1)
    m1 = TreeNode(2)
    m2 = TreeNode(3)
    m0.left, m0.right = m1, m2

    print(Solution().isSameTree(n0, m0))


# Copyright cozybit, Inc. - 2014
# All rights reserved

"""
An adapter used to map the wpa_supplicant tests cases for mesh onto real
hardware using wtf.
"""

import importlib
import os
import Queue as queue
import re
import subprocess
import sys
import threading
import time
import unittest
import wtf

WTFCONFIG = wtf.conf
STATIONS = WTFCONFIG.mps
ADAPTERS = None
THIS_FILE = os.path.abspath(__file__)
THIS_DIR = os.path.dirname(THIS_FILE)

wpa_cli_error = ("Expected exit status of 0 from wpa_cli, got %d instead "
                 "(for command: '%s')")

def setUp(self):
    global ADAPTERS
    for station in STATIONS:
        station.comm.send_cmd("stop wpa_supplicant")
        station.comm.send_cmd("svc wifi disable")
    time.sleep(2)
    ADAPTERS = [WpasTestAdapter(sta) for sta in STATIONS]

def tearDown(self):
    for n in WTFCONFIG.nodes:
        n.stop()

class AdbCmdGen(object):
    @staticmethod
    def clear_logcat(adb_id):
        return ["adb", "-s", adb_id, "logcat", "-c"]

    @staticmethod
    def start_logcat(adb_id):
        return ["adb", "-s", adb_id, "logcat"]

    @staticmethod
    def wpa_cli(adb_id):
        return ["adb", "-s", adb_id, "shell", "wpa_cli",
                "-p/data/misc/wifi", "-iwlan0"]

    @staticmethod
    def dmesg(adb_id):
        return ["adb", "-s", adb_id, "shell", "cat", "/proc/kmsg"]

class WpaCliMonitor (object):

    EVENT_TRIM = re.compile(r'^[\s>]+')

    def __init__(self, q, adb_id, device_id):
        self._q = q
        self._adb_id = adb_id
        self._device_id = device_id

        self._wpacli_process = None
        self._logcat_process = None
        self._dmesg_process = None

        self._wpacli_logfp = None
        self._logcat_logfp = None
        self._dmesg_logfp = None

        self._wpacli_thread = None
        self._wpacli_stderr_thread = None
        self._logcat_out_thread = None
        self._logcat_err_thread = None
        self._dmesg_out_thread = None
        self._dmesg_err_thread = None

    def _gen(self):
        while True:
            line = self._wpacli_process.stdout.readline()
            if not line:
                return
            self._wpacli_logfp.write(line)
            line = line.strip()
            #print 'LaunchMonitorProcess:', repr(line)
            split = self.EVENT_TRIM.split(line)
            if len(split) > 1:
                line = split[1]
            if line.startswith("<3>"):
                yield line[3:]
            else:
                continue

    @staticmethod
    def _dump_fp_to_log(fp, logfp):
        while True:
            line = fp.readline()
            if not line:
                break
            logfp.write(line)

    def _wpa_cli_run(self, test_name):
        self._wpacli_process = subprocess.Popen(
            AdbCmdGen.wpa_cli(self._adb_id),
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE)

        subprocess.call(AdbCmdGen.clear_logcat(self._adb_id))

        self._logcat_process = (
            subprocess.Popen(AdbCmdGen.start_logcat(self._adb_id),
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE))

        self._dmesg_process = (
            subprocess.Popen(AdbCmdGen.dmesg(self._adb_id),
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE))

        self._wpacli_logfp = open("wpa_cli.%s.%s.log" %
                                  (test_name, self._device_id), "w")

        self._logcat_logfp = open("logcat.%s.%s.log" %
                                  (test_name, self._device_id), "w")

        self._dmesg_logfp = open("dmesg.%s.%s.log" %
                                 (test_name, self._device_id), "w")

        self._wpacli_stderr_thread = threading.Thread(
            target=self._dump_fp_to_log,
            args=(self._wpacli_process.stderr,
                  self._wpacli_logfp))

        self._wpacli_stderr_thread.start()

        self._logcat_out_thread = threading.Thread(
            target=self._dump_fp_to_log,
            args=(self._logcat_process.stdout,
                  self._logcat_logfp))

        self._logcat_out_thread.start()

        self._logcat_err_thread = threading.Thread(
            target=self._dump_fp_to_log,
            args=(self._logcat_process.stderr,
                  self._logcat_logfp))

        self._logcat_err_thread.start()

        self._dmesg_out_thread = threading.Thread(
            target=self._dump_fp_to_log,
            args=(self._dmesg_process.stdout,
                  self._dmesg_logfp))

        self._dmesg_out_thread.start()

        self._dmesg_err_thread = threading.Thread(
            target=self._dump_fp_to_log,
            args=(self._dmesg_process.stderr,
                  self._dmesg_logfp))

        self._dmesg_err_thread.start()

        for line in self._gen():
            self._q.put(line)

    def start(self, test_name):
        self._wpacli_thread = threading.Thread(target=self._wpa_cli_run,
                                               args=(test_name,))
        self._wpacli_thread.start()

    def stopMonitor(self):
        self._wpacli_process.kill()
        self._logcat_process.kill()
        self._dmesg_process.kill()
        self._wpacli_thread.join()
        self._wpacli_stderr_thread.join()
        self._logcat_out_thread.join()
        self._logcat_err_thread.join()
        self._dmesg_out_thread.join()
        self._dmesg_err_thread.join()
        self._wpacli_logfp.close()
        self._logcat_logfp.close()
        self._dmesg_logfp.close()

class EventMonitor (object):

    def __init__(self, sta):
        self._sta = sta
        self._q = queue.Queue()
        self._monitor = WpaCliMonitor(self._q,
                                      self._sta.comm.get_adb_id(),
                                      self._sta.comm.get_device_id())

    def waitForEvent(self, eventName):
        while True:
            line = self._q.get(timeout=10)
            #print 'waitForEvent:', line
            if line.startswith(eventName):
                return line

    def startMonitor(self, test_name):
        self._monitor.start(test_name=test_name)

    def stopMonitor(self):
        self._monitor.stopMonitor()

class WpasTestAdapter (object):

    def __init__(self, sta):
        self._sta = sta
        self._comm = sta.comm
        self._monitor = EventMonitor(sta)

    def startWpaCliMonitor(self, test_name):
        self._monitor.startMonitor(test_name)

    def stopWpaCliMonitor(self):
        self._monitor.stopMonitor()

    def getSTA(self):
        return self._sta

    def _send_to_wpa_cli(self, cmd):
        cmd = "wpa_cli -p/data/misc/wifi -iwlan0 " + cmd
        #print cmd
        code, resp = self._comm.send_cmd(cmd)
        if code != 0:
            raise ValueError(wpa_cli_error % (code, cmd))
        return str.join('', resp)

    def ping_wpa_supplicant(self):
        cmd = "wpa_cli -p/data/misc/wifi -iwlan0 ping"
        code, resp = self._comm.send_cmd(cmd)
        return code == 0 and str.join('', resp) == "PONG"

    def _send_cmd_bool_resp(self, cmd):
        resp = self._send_to_wpa_cli(cmd).strip()
        if resp != "OK":
            raise ValueError(resp)
        return resp

    def _send_cmd_int_resp(self, cmd):
        resp = self._send_to_wpa_cli(cmd).strip()
        if not resp.isdigit():
            raise ValueError(resp)
        return resp

    def dump_monitor(self):
        pass

    def request(self, req):
        return self._send_to_wpa_cli(req)

    def add_network(self):
        return self._send_cmd_int_resp("add_network")

    def set_network(self, networkId, prop, val):
        return self._send_cmd_bool_resp(
            "set_network " + networkId + " " + prop + " " + val)

    def set_network_quoted(self, networkId, prop, val):
        return self.set_network(networkId, prop, '\'"' + val + '"\'')

    def mesh_group_add(self, meshId):
        return self._send_cmd_bool_resp("mesh_group_add " + meshId)

    def mesh_group_remove(self):
        return self._send_cmd_bool_resp("mesh_group_remove wlan0")

    def remove_network(self, networkId):
        return self._send_cmd_bool_resp("remove_network " + networkId)

    def wait_event(self, eventList):
        for event in eventList:
            ev = self._monitor.waitForEvent(event)
            if ev is not None:
                return ev

def adb_ping_check(dev1, dev2):

    dev1.getSTA().comm.send_cmd("ip addr flush dev wlan0")
    dev1.getSTA().comm.send_cmd("ip addr add 10.0.0.2/24 dev wlan0")

    dev2.getSTA().comm.send_cmd("ip addr flush dev wlan0")
    dev2.getSTA().comm.send_cmd("ip addr add 10.0.0.3/24 dev wlan0")

    code, outp = dev1.getSTA().comm.send_cmd("ping -c5 10.0.0.3")
    if code != 0:
        raise Exception("No connectivity from dev1 to dev2")

    return

class WpasMeshTest(unittest.TestCase):

    def __init__(self, *args, **kw):
        unittest.TestCase.__init__(self, *args, **kw)
        fp = os.path.join(THIS_DIR, "..", "submodules", "wpa_supplicant",
                          "tests", "hwsim")
        sys.path.append(fp)
        module = importlib.import_module("test_wpas_mesh")
        self._tests = {}
        for name in module.__dict__.keys():
            if name.startswith("test_") or name.startswith("_test"):
                self._tests[name] = module.__dict__[name]

    def setUp(self):
        for adapter in ADAPTERS:
            adapter.getSTA().comm.send_cmd("start wpa_supplicant")
            while not adapter.ping_wpa_supplicant():
                time.sleep(0.5)
            adapter.startWpaCliMonitor(self.id())

    def tearDown(self):
        for adapter in ADAPTERS:
            adapter.stopWpaCliMonitor()
            adapter.getSTA().comm.send_cmd("stop wpa_supplicant")

    def test_wpas_add_set_remove_support(self):
        self._tests['test_wpas_add_set_remove_support'](ADAPTERS)

    def test_wpas_mesh_group_added(self):
        self._tests['test_wpas_mesh_group_added'](ADAPTERS)

    def test_wpas_mesh_group_remove(self):
        self._tests['test_wpas_mesh_group_remove'](ADAPTERS)

    def test_wpas_mesh_peer_connected(self):
        self._tests['test_wpas_mesh_peer_connected'](ADAPTERS)

    def test_wpas_mesh_peer_disconnected(self):
        self._tests['test_wpas_mesh_peer_disconnected'](ADAPTERS)

    def test_wpas_mesh_mode_scan(self):
        self._tests['test_wpas_mesh_mode_scan'](ADAPTERS)

    def test_wpas_mesh_open(self):
        self._tests['_test_wpas_mesh_open'](ADAPTERS, None, adb_ping_check)

    def test_wpas_mesh_open_no_auto(self):
        self._tests['_test_wpas_mesh_open_no_auto'](ADAPTERS, None,
                                                    adb_ping_check)

    def test_wpas_mesh_secure(self):
        self._tests['_test_wpas_mesh_secure'](ADAPTERS, None, adb_ping_check)

    def test_wpas_mesh_secure_no_auto(self):
        self._tests['_test_wpas_mesh_secure_no_auto'](ADAPTERS, None,
                                                      adb_ping_check)


def find_rotation_point(input_list):
    start_index = 0
    end_index = len(input_list) - 1
    if input_list[start_index] < input_list[end_index]:
        return start_index

    while start_index <= end_index:
        middle_index = start_index + (end_index - start_index) // 2
        potential_rotation_point = input_list[middle_index]
        before_rotation_point = input_list[middle_index - 1]
        last_element = input_list[end_index]

        if before_rotation_point > potential_rotation_point:
            return middle_index
        elif potential_rotation_point > last_element:
            start_index = middle_index + 1
        else:
            end_index = middle_index - 1
    return False

words = ['v', 'a', 'b', 'c', 'k']
print(find_rotation_point(words))


import numpy as np
import pandas as pd
import os
import re

from sklearn.base import BaseEstimator, TransformerMixin
from datetime import datetime

def create_uid(base_str):
    """
    Creates a UID by concatenating the current timestamp with a base string.

    :param base_str: string to concat the time to
    :returns: str
    """
    now = datetime.now()
    uid = base_str + '_' + str(now).replace(' ', '').replace('.', '').replace(':', '').replace('-', '')
    return uid

def convert_camel_case_to_snake_case(df):
    """
    Converts dataframe column headers from snakeCase to camel_case.

    :param df: any valid pandas dataframe
    :return: pandas dataframe
    """
    new_columns = []
    for column in list(df):
        new_column = re.sub(r'(?<!^)(?=[A-Z])', '_', column).lower()
        new_columns.append(new_column)
    df.columns = new_columns
    return df

def make_directories_if_not_exists(directories_list):
    """
    Makes directories if they do not exist.

    :param directories_list: list of directories to create
    """
    for directory in directories_list:
        if not os.path.exists(directory):
            os.makedirs(directory)

def clean_batting_and_pitching_players(batting_df, pitching_df, positions_df):
    """
    Removes players from batting_df who are pitchers. Likewise, removes players from the pitching_df who are not
    pitchers. This is all based on the information in positions_df.

    :param batting_df: dataframe of batting stats
    :param pitching_df: dataframe of pitching stats
    :param positions_df: dataframe of the positions played by a player
    :returns: batting_df, pitching_df
    """
    positions_df['pitching_percentage'] = positions_df['G_p'] / positions_df['G_all']
    pitchers_df = positions_df.loc[positions_df['pitching_percentage'] >= 0.99]
    pitchers_df['remove_id'] = pitchers_df['playerID'] + pitchers_df['yearID'].astype(str)
    pitchers_ids = pitchers_df['remove_id'].tolist()
    remove_pitchers_df = positions_df.loc[(positions_df['pitching_percentage'] <= 0.02) &
                                          (positions_df['pitching_percentage'] > 0)]
    remove_pitchers_df['remove_id'] = remove_pitchers_df['playerID'] + remove_pitchers_df['yearID'].astype(str)
    remove_pitchers_ids = remove_pitchers_df['remove_id'].tolist()

    batting_df = batting_df.loc[~(batting_df['playerID'] + batting_df['yearID'].astype(str)).isin(pitchers_ids)]
    pitching_df = pitching_df.loc[~(pitching_df['playerID'] + pitching_df['yearID'].astype(str)).isin(
        remove_pitchers_ids)]

    return batting_df, pitching_df

def calculate_expanding_obp(df):
    """
    Calculates an expanding on base percentage.

    :param df: pandas dataframe of raw batting stats
    :returns: pandas dataframe
    """
    coerce_numeric_list = ['H', 'BB', 'HBP', 'AB', 'SF', '2B', '3B', 'HR', 'IBB', 'G']
    for variable in coerce_numeric_list:
        df[variable] = pd.to_numeric(df[variable], errors='coerce')

    features = list(df)
    features.remove('playerID')
    features.remove('yearID')
    features.remove('teamID')

    for feature in features:
        df[feature] = df.groupby('playerID')[feature].shift()

    df['expanding_games'] = df.groupby('playerID')['G'].cumsum()
    df['expanding_hits'] = df.groupby('playerID')['H'].cumsum()
    df['expanding_walks'] = df.groupby('playerID')['BB'].cumsum()
    df['expanding_hbp'] = df.groupby('playerID')['HBP'].cumsum()
    df['expanding_at_bats'] = df.groupby('playerID')['AB'].cumsum()
    df['expanding_sac_flies'] = df.groupby('playerID')['SF'].cumsum()
    df['expanding_doubles'] = df.groupby('playerID')['2B'].cumsum()
    df['expanding_triples'] = df.groupby('playerID')['3B'].cumsum()
    df['expanding_home_runs'] = df.groupby('playerID')['HR'].cumsum()
    df['expanding_intentional_walks'] = df.groupby('playerID')['IBB'].cumsum()

    df['obp'] = (df['expanding_hits'] + df['expanding_walks'] + df['expanding_hbp']) / \
                (df['expanding_at_bats'] + df['expanding_walks'] + df['expanding_hbp'] + df['expanding_sac_flies'])
    df['slg'] = (df['expanding_hits'] - (df['expanding_doubles'] + df['expanding_triples'] + df['expanding_home_runs'])
                 + df['expanding_doubles'] * 2 + df['expanding_triples'] * 3 + df['expanding_home_runs'] * 4) / \
                df['expanding_at_bats']

    df['slg'].fillna(value=0, inplace=True)
    df['obp'].fillna(value=0, inplace=True)
    df['ops'] = df['obp'] + df['slg']
    return df

def calculate_expanding_era(df):
    """
    Calculates expanding earned run average.

    :param df: pandas dataframe with pitching stats
    :returns: dataframe
    """
    df.drop('ERA', 1, inplace=True)
    df['ER'] = df.groupby('playerID')['ER'].shift()
    df['IPouts'] = df.groupby('playerID')['IPouts'].shift()
    df['innings_pitched'] = df['IPouts'] / 3
    df['expanding_earned_runs'] = df.groupby('playerID')['ER'].cumsum()
    df['expanding_innings_pitched'] = df.groupby('playerID')['innings_pitched'].cumsum()
    df['era'] = 9 * df['expanding_earned_runs'] / df['expanding_innings_pitched']
    df['era'] = np.where(df['era'] == 0, np.nan, df['era'])
    return df

def find_total_and_lagged_all_star_appearances(df, year_lags):
    """
    Finds the total number of all-star appearances in a player's career. Also finds the total number of all-star
    appearances for every year until the end of a lookback window, defined by year_lags.

    :param df: pandas dataframe including all-star appearances
    :param year_lags: number of years to lookback for calculating all-star appearances for a window calculation
    :returns: pandas dataframe
    """
    df.dropna(subset=['yearID'], inplace=True)
    df['yearID'] = df['yearID'].astype(int)
    df_2020 = df.loc[df['yearID'] == 2019]
    df_2020['yearID'] = 2020
    df = pd.concat([df, df_2020], axis=0)
    df['all_star'] = 1

    years_df = pd.DataFrame({'yearID': range(df['yearID'].min(), df['yearID'].max() + 2)})
    players = list(df['playerID'].unique())
    main_players_df = pd.DataFrame()
    for player in players:
        player_df = df.loc[df['playerID'] == player]
        player_df = pd.merge(years_df, player_df, how='left', on='yearID')
        lag_cols = []
        for lag in range(1, year_lags + 1):
            col = f'year_lag_{lag}'
            player_df[col] = player_df['all_star'].shift(lag)
            player_df[col] = np.where(player_df[col] > 0, 1, 0)
            lag_cols.append(col)
        appearance_cols = []
        for index, lag_col in enumerate(lag_cols):
            appearance_col = f'as_appearance_last_{lag_col}_year'
            player_df[appearance_col] = player_df[lag_cols[:index + 1]].sum(axis=1)
            appearance_cols.append(appearance_col)
        player_df = player_df[['playerID', 'yearID'] + appearance_cols]
        player_df.fillna({'playerID': player}, inplace=True)
        player_df.dropna(inplace=True)
        main_players_df = main_players_df.append(player_df)

    df = pd.merge(main_players_df, df, how='left', on=['playerID', 'yearID'])
    df.fillna({'all_star': 0}, inplace=True)
    df['expanding_as_appearances'] = df.groupby('playerID')['all_star'].cumsum()
    df['expanding_as_appearances'] = df.groupby('playerID')['expanding_as_appearances'].shift(1)
    df = df.drop('teamID', 1)
    return df

def find_player_age(player_df, stats_df):
    """
    Finds a players age.

    :param player_df: dataframe with birth information
    :param stats_df: dataframe with player stats for every given year
    :returns: pandas dataframe
    """
    player_df = player_df.dropna(subset=['birthYear', 'birthMonth', 'birthDay'])
    player_df['birth_date'] = player_df['birthYear'].astype(int).astype(str) + '-' + \
                              player_df['birthMonth'].astype(int).astype(str) + '-' + \
                              player_df['birthDay'].astype(int).astype(str)
    player_df['birth_date'] = pd.to_datetime(player_df['birth_date'])
    player_df = player_df[['playerID', 'birth_date']]
    stats_df = pd.merge(stats_df, player_df, how='left', on='playerID')
    stats_df['stock_start_date'] = stats_df['yearID'].astype(str) + '-04-01'
    stats_df['stock_start_date'] = pd.to_datetime(stats_df['stock_start_date'])
    stats_df['player_age'] = ((stats_df['stock_start_date'] - stats_df['birth_date']) / np.timedelta64(1, 'Y'))
    return stats_df

def find_yearly_team_winning_percentages(df, year_lags):
    """
    Finds the team's winning percentage every year until the end of a lookback window, defined by year_lags. Also
    calculates the rolling 3 year and 5 year winning percentages.

    :param df: pandas dataframe of team wins and losses
    :param year_lags:: number of years to lookback for calculating winning percentages for a window calculation
    :returns: pandas dataframe
    """
    df['winning_percentage'] = df['W'] / (df['W'] + df['L'])
    for lag in range(1, year_lags + 1):
        df[f'winning_percentage_lag_{lag}'] = df.groupby('teamID')['winning_percentage'].shift(lag)
    df['winning_percentage_rolling_3'] = (df[f'winning_percentage_lag_1'] + df[f'winning_percentage_lag_2'] +
                                         df[f'winning_percentage_lag_3']) / 3
    df['winning_percentage_rolling_5'] = (df[f'winning_percentage_lag_1'] + df[f'winning_percentage_lag_2'] +
                                         df[f'winning_percentage_lag_3'] + df[f'winning_percentage_lag_4'] +
                                          df[f'winning_percentage_lag_5']) / 5
    return df

def find_postseason_results(df, year_lags):
    """
    Finds the team's postseason every year until the end of a lookback window, defined by year_lags.

    :param df: pandas dataframe of team postseason results
    :param year_lags:: number of years to lookback for calculating winning percentages for a window calculation
    :returns: pandas dataframe
    """
    winner_df = df[['yearID', 'round', 'teamIDwinner']]
    winner_df['round'] = winner_df['round'] + ' - winner'
    winner_df.rename(columns={'teamIDwinner': 'teamID'}, inplace=True)

    loser_df = df[['yearID', 'round', 'teamIDloser']]
    loser_df['round'] = loser_df['round'] + ' - loser'
    loser_df.rename(columns={'teamIDloser': 'teamID'}, inplace=True)
    df = pd.concat([winner_df, loser_df], axis=0)

    years_df = pd.DataFrame({'yearID': range(df['yearID'].min(), df['yearID'].max() + 2)})
    teams = list(df['teamID'].unique())
    main_teams_df = pd.DataFrame()
    for team in teams:
        team_df = df.loc[df['teamID'] == team]
        team_df.drop_duplicates(subset=['teamID', 'yearID'], keep='last', inplace=True)
        team_df = pd.merge(years_df, team_df, how='left', on='yearID')
        lag_cols = []
        for lag in range(1, year_lags + 1):
            col = f'year_lag_{lag}'
            team_df[col] = team_df['round'].shift(lag)
            lag_cols.append(col)
        result_cols = []
        for index, lag_col in enumerate(lag_cols):
            appearance_col = f'postseason_result_last_{lag_col}_year'
            team_df[appearance_col] = team_df[lag_cols[index]]
            result_cols.append(appearance_col)
        team_df = team_df[['teamID', 'yearID'] + result_cols]
        team_df.fillna({'teamID': team}, inplace=True)
        team_df.fillna('missed_playoffs', inplace=True)
        main_teams_df = main_teams_df.append(team_df)

    return main_teams_df

def create_target_dataframe(df):
    """
    Creates a dataframe of our target: the team who won the World Series.

    :param df: pandas dataframe including the team who won the World Series
    :returns: pandas dataframe
    """
    ws_df = df.loc[df['round'] == 'WS']
    ws_df = ws_df[['yearID', 'teamIDwinner']]
    ws_df['target'] = 1
    return ws_df

def flatten_data_to_team_and_year(df):
    """
    Flattens the dataframe to be one row for every tear amd year.

    :param df: pandas dataframe
    :returns: pandas dataframe
    """
    aggregates_df = df.groupby(['team_yearID', 'team_teamID']).agg({
        'batting_ops': 'median',
        'pitching_era': 'median',
        'batting_as_appearance_last_year_lag_1_year': 'sum',
        'batting_as_appearance_last_year_lag_3_year': 'sum',
        'batting_expanding_as_appearances': 'sum',
        'pitching_as_appearance_last_year_lag_1_year': 'sum',
        'pitching_as_appearance_last_year_lag_3_year': 'sum',
        'pitching_expanding_as_appearances': 'sum',
        'batting_player_age': 'mean',
        'pitching_player_age': 'mean'
    })
    aggregates_df['total_as_appearances'] = aggregates_df['batting_expanding_as_appearances'] + \
                                            aggregates_df['pitching_expanding_as_appearances']
    df.drop(list(aggregates_df), 1, inplace=True, errors='ignore')
    df.drop_duplicates(subset=['team_yearID', 'team_teamID'], keep='first', inplace=True)
    aggregates_df.reset_index(inplace=True)
    df = pd.merge(df, aggregates_df, how='left', on=['team_yearID', 'team_teamID'])
    return df

def consolidate_yearly_player_data(df):
    """
    For players who played for multiple teams in a year, consolidate that stats onto a single row.

    :param df: pandas dataframe
    :returns: pandas dataframe
    """
    df['player_year_dupe'] = df.duplicated(subset=['playerID', 'yearID'], keep=False)
    dupe_df = df.loc[df['player_year_dupe'] == True]
    df = df.loc[df['player_year_dupe'] != True]

    min_df = dupe_df.groupby(['playerID', 'yearID']).agg({'order': 'min'})
    min_df.reset_index(inplace=True)

    agg_df = dupe_df.groupby(['playerID', 'yearID']).agg(['sum'])
    agg_df.reset_index(inplace=True)
    agg_df = agg_df.droplevel(1, axis=1)
    agg_df.drop(['order', 'teamID', 'lgID', 'player_year_dupe'], 1, inplace=True)

    dupe_df = pd.merge(dupe_df, min_df, how='inner', on=['playerID', 'yearID', 'order'])
    dupe_df = pd.merge(dupe_df, agg_df, how='inner', on=['playerID', 'yearID'])
    dupe_df = dupe_df[dupe_df.columns.drop(list(dupe_df.filter(regex='_x')))]
    dupe_df.columns = dupe_df.columns.str.rstrip('_y')
    df = pd.concat([df, dupe_df], axis=0)

    df.sort_values(by=['playerID', 'yearID', 'order'], ascending=True, inplace=True)
    df.drop('player_year_dupe', 1, inplace=True)
    return df

def calculate_batting_stats(player_df, batting_df):
    """
    Omnibus function for calculating batting stats.

    :param player_df: dataframe of general player attributes
    :param batting_df: dataframe of batting stats
    :returns: pandas dataframe
    """
    batting_df.reset_index(inplace=True)
    batting_df.rename(columns={'index': 'order'}, inplace=True)
    batting_df = consolidate_yearly_player_data(batting_df)
    batting_df = calculate_expanding_obp(batting_df)
    batting_df.reset_index(inplace=True, drop=True)
    batting_df = find_player_age(player_df, batting_df)
    return batting_df

def calculate_pitching_stats(player_df, pitching_df):
    """
    Omnibus function for calculating batting stats.

    :param player_df: dataframe of general player attributes
    :param pitching_df: dataframe of pitching stats
    :returns: pandas dataframe
    """
    pitching_df.reset_index(inplace=True)
    pitching_df.rename(columns={'index': 'order'}, inplace=True)
    pitching_df = consolidate_yearly_player_data(pitching_df)
    pitching_df = calculate_expanding_era(pitching_df)
    pitching_df.reset_index(inplace=True, drop=True)
    pitching_df = find_player_age(player_df, pitching_df)
    return pitching_df

def append_all_star_appearances(all_star_df, batting_df, pitching_df):
    """
    Appends all-star appearances to batting and and pitching dataframes.

    :param all_star_df: dataframe of all-star appearances
    :param batting_df: dataframe of batting stats
    :param pitching_df: dataframe of pitching stats
    :returns: pandas dataframe
    """
    all_star_df = find_total_and_lagged_all_star_appearances(all_star_df, 5)
    batting_df = pd.merge(batting_df, all_star_df, how='left', on=['playerID', 'yearID'])
    pitching_df = pd.merge(pitching_df, all_star_df, how='left', on=['playerID', 'yearID'])
    return batting_df, pitching_df

def add_column_name_prefixes(team_records_df, batting_df, pitching_df):
    """
    Adds prefixes to every column in a set of dataframes.

    :param team_records_df: dataframe containing yearly team records
    :param batting_df: dataframe of batting stats
    :param pitching_df: dataframe of pitching stats
    :returns: tuple of three pandas dataframes
    """
    team_records_df = team_records_df.add_prefix('team_')
    batting_df = batting_df.add_prefix('batting_')
    pitching_df = pitching_df.add_prefix('pitching_')
    return team_records_df, batting_df, pitching_df

def merge_dataframes(team_records_df, postseason_df, batting_df, pitching_df):
    """
    Merges team records and postseason results onto batting and pitching stats.

    :param team_records_df: dataframe of team records
    :param postseason_df: dataframe of postseason results
    :param batting_df: dataframe of batting stats
    :param pitching_df: dataframe of pitching stats
    :returns: tuple of three dataframes
    """
    teams_df = pd.merge(team_records_df, postseason_df, how='left', left_on=['team_teamID', 'team_yearID'],
                        right_on=['teamID', 'yearID'])
    teams_batting_df = pd.merge(teams_df, batting_df, how='left', left_on=['team_teamID', 'team_yearID'],
                                right_on=['batting_teamID', 'batting_yearID'])
    teams_pitching_df = pd.merge(teams_df, pitching_df, how='left', left_on=['team_teamID', 'team_yearID'],
                                 right_on=['pitching_teamID', 'pitching_yearID'])
    return teams_df, teams_batting_df, teams_pitching_df

def prep_team_level_dataframes(team_records_df, postseason_df):
    """
    Omnibus function to prepare team records and postseason dataframes.

    :param team_records_df: dataframe of team records
    :param postseason_df: dataframe of postseason results
    :returns: tuple of dataframes
    """
    team_records_df = find_yearly_team_winning_percentages(team_records_df, 5)
    postseason_df = find_postseason_results(postseason_df, 5)
    return team_records_df, postseason_df

def create_modeling_dataframe(teams_df, teams_batting_df, teams_pitching_df, target_df):
    """
    Creates dataframe for modeling. This consists of one row for every team for every year. The target is the team who
    won the world series in the upcoming year.

    """
    teams_df = pd.concat([teams_df, teams_batting_df, teams_pitching_df], axis=0)
    teams_df.reset_index(inplace=True, drop=True)
    teams_df = teams_df.loc[teams_df['team_yearID'] >= 1905]
    teams_df = teams_df.loc[~teams_df['team_yearID'].isin([1919, 1994])]
    teams_df = teams_df[[
        'team_yearID', 'team_teamID', 'team_winning_percentage_lag_1', 'team_winning_percentage_lag_2',
        'team_winning_percentage_lag_3', 'team_winning_percentage_lag_4', 'team_winning_percentage_lag_5',
        'team_winning_percentage_rolling_3', 'team_winning_percentage_rolling_5',
        'postseason_result_last_year_lag_1_year', 'postseason_result_last_year_lag_2_year',
        'postseason_result_last_year_lag_3_year', 'postseason_result_last_year_lag_4_year',
        'postseason_result_last_year_lag_5_year', 'batting_ops', 'pitching_era',
        'batting_as_appearance_last_year_lag_1_year', 'batting_as_appearance_last_year_lag_3_year',
        'batting_expanding_as_appearances', 'pitching_as_appearance_last_year_lag_1_year',
        'pitching_as_appearance_last_year_lag_3_year', 'pitching_expanding_as_appearances',
        'batting_player_age', 'pitching_player_age'
        ]]
    teams_df = flatten_data_to_team_and_year(teams_df)
    teams_df = pd.merge(teams_df, target_df, how='left', left_on=['team_yearID', 'team_teamID'],
                        right_on=['yearID', 'teamIDwinner'])
    teams_df.fillna({'target': 0}, inplace=True)
    teams_df['target'] = teams_df['target'].astype(int)
    teams_df.fillna(value=0, inplace=True)
    return teams_df

def create_train_test_split(df, target, test_set_start):
    """
    Creates a train-test split, with every row after test_set_start being assigned to the test set.

    :param df: pandas dataframe
    :param target: name of the target column
    :param test_set_start: year to begin the test set
    """
    test_df = df.loc[df['team_yearID'] >= test_set_start]
    train_df = df.loc[df['team_yearID'] < test_set_start]
    y_train = train_df[target]
    y_test = test_df[target]
    x_train = train_df.drop(target, 1)
    x_test = test_df.drop(target, 1)
    return x_train, x_test, y_train, y_test

def drop_columns(df, drop_cols):
    """
    Drops columns from a pandas dataframe

    :param df: pandas dataframe
    :param drop_cols: list of columns to drop
    :returns: pandas dataframe
    """
    df = df.drop(drop_cols, 1)
    return df

def subtract_columns(df, col1, col2):
    """
    Subtracts two pandas dataframe columns to create a new column.

    :param df: pandas dataframe
    :param col1: string name of first column
    :param col2: string name of second column
    :returns: pandas dataframe
    """
    df[f'{col1}_{col2}_diff'] = df[col1] - df[col2]
    return df

class FeaturesToDict(BaseEstimator, TransformerMixin):
    """
    Transformer to cast features to a dictionary.
    """
    def __int__(self):
        pass

    def fit(self, X, Y=None):
        self.fitted_ = True
        return self

    def transform(self, X, Y=None):
        if isinstance(X, np.ndarray):
            X = pd.DataFrame(X)
        X = X.to_dict(orient='records')
        return X


from greyatomlib.python_getting_started.q01_read_data.build import read_data
data = read_data()

def deliveries_count(data=data):
    i=0
    deliveries = data['innings'][0]['1st innings']['deliveries']
    for delivery in deliveries:
        for delivery_number, delivery_info in delivery.items():
            if delivery_info['batsman'] == 'RT Ponting':
                i += 1

    return i


# -*- coding: utf-8 -*-
import scrapy
from tools.redisTool import RedisTool
from ammmi.items import *
import re

def validateTitle(title):
    rstr = r"[\/\\\:\*\?\"\<\>\|]"  # '/ \ : * ? " < > |'
    new_title = re.sub(rstr, "_", title)  # 替换为下划线
    return new_title

class CosplaySpiderSpider(scrapy.Spider):
    name = 'cosplay_spider'
    base_url = 'https://www.ammmi.com/category/cosplay/page/{0}'
    allowed_domains = ['www.ammmi.com']
    page_index = 1
    page_max = 136
    start_urls = [base_url.format(str(page_index))]
    # redis key and values
    complete_key = 'ammmi:cosplay:complete:urls'
    complete_values = []
    fail_key = 'ammmi:cosplay:fail:urls'
    fail_values = []
    # redis
    client = None
    host = 'localhost'
    port = 6379
    DEBUG = False

    def __init__(self):
        if self.DEBUG:
            self.page_max = 1
        RedisTool.setHost(self.host, self.port)
        self.client = RedisTool()
        self.complete_values = self.client.smembers(self.complete_key)
        self.fail_values = self.client.smembers(self.fail_key)
        self.logger.info('加载{0}:{1} {2}:{3}'.format(self.complete_key,
                                                    self.complete_values[:10:],
                                                    self.fail_key,
                                                    self.fail_values[:10:]))
        super().__init__()

    def close(self, reason):
        self.client.sadd(self.complete_key,
                         self.complete_values)
        self.client.sadd(self.fail_key,
                         self.fail_values)
        self.logger.info('保存{0}\n{1}'.format(self.complete_key, self.fail_key))
        super().close(self, reason)

    def parse(self, response):
        if 'category' in response.url:
            img_urls = []
            img_urls = response.xpath('//div[@class="bt_img mi_ne_kd mrb"]/ul/li/a//@href').extract()
            for url in img_urls:
                yield scrapy.Request(url, callback=self.parse)
            # next page
            if self.page_index <= self.page_max:
                self.page_index += 1
                yield scrapy.Request(self.base_url.format(str(self.page_index)),
                                     callback=self.parse)
        else:
            img_title = 'cosplay'
            title = ''
            img_url = ''
            img_url = response.xpath('//div[@class="yp_context"]//img//@src').extract_first()
            title = response.xpath('//div[@class="yp_context"]//img//@alt').extract_first()
            title = validateTitle(title)
            item = CosplayItem()
            item['img_title'] = img_title
            item['title'] = title
            item['img_url'] = img_url
            yield item


from module import Scraper, timer, write_csv_in_file

URL = 'http://www.tdsot.ru/catalog/'
COUNT_100 = '?count=100'
PAGE = '&PAGEN_1='

@timer
def main():
    soup = Scraper.get_soup(URL)
    main_links_data = soup.find('div', class_='catalog').find_all('tr')
    main_links = (URL[:19] + link.find('a').get('href') + COUNT_100 for link in main_links_data)

    all_links = {}
    for link in main_links:
        soup = Scraper.get_soup(link)
        try:
            total_pages = int(soup.find('ul', class_='pagination').find_all('li')[-1].find('a').get('href').split('=')[-1])
        except AttributeError:
            total_pages = 1

        all_links[link] = total_pages
        # print(link, total_pages)

    data_list = []
    title = 'стройОПТторг'
    available = 'В наличии'
    for page, total_page in all_links.items():
        for page_num in range(1, total_page + 1):
            soup = Scraper.get_soup(page + PAGE + str(page_num))
            data = soup.find('table', class_='catalog-table').find_all('tr')

            for i in data:
                name = i.find('td', class_='name').find('a').text.split()
                name = ' '.join(name)

                url = URL[:19] + i.find('td', class_='name').find('a').get('href')

                url_image = i.find('td', class_='pic').find('img').get('src')
                url_image = 'http://www.tdsot.ru' + url_image

                price = i.find('td', class_='price').find('b').text

                data = {'title': title, 'name': name, 'price': price, 'available': available, 'url': url, 'url_image': url_image}
                data_list.append(data)

    # Create csv-file and write data
    write_csv_in_file(data_list)
    print('Собрано данных: {}'.format(len(data_list)))

if __name__ == '__main__':
    main()


from datetime import datetime

from flask import request
from flask_restful import abort, Resource

class BaseResource(Resource):
    def __init__(self, storage, gossip, myself):
        self.gossip = gossip
        self.storage = storage
        self.myself = myself

    def abort_if_data_not_found(self, name):
        if name not in self.storage:
            abort(404, message=f"No data found for {name} key")

class Data(BaseResource):
    """
    Single data entry resource
    """
    def get(self, name):
        self.abort_if_data_not_found(name)
        return self.storage[name]

    def delete(self, name):
        self.abort_if_data_not_found(name)

        timestamp = datetime.utcnow().timestamp()
        author = self.myself

        self.storage.delete(name, author, timestamp)
        # Send update to other cluster nodes
        self.gossip.propagate_data('delete',
            {'author': author, 'timestamp': timestamp}
        )
        return '', 204

    def post(self, name):
        val = request.json['value']
        timestamp = datetime.utcnow().timestamp()
        author = self.myself

        self.storage.set(name, val, author, timestamp)
        # Send update to other cluster nodes
        self.gossip.propagate_data('set',
            {'name': name, 'value': val, 'author': author, 'timestamp': timestamp}
        )
        return {name: val}, 201

class DataHistory(BaseResource):
    def get(self, name):
        self.abort_if_data_not_found(name)
        return self.storage.get_history(name)

class DataSet(BaseResource):
    def get(self):
        """
        Used for data dump.
        """
        return self.storage.dump()

class DataSetHistory(BaseResource):
    def get(self):
        """
        Used for data dump.
        """
        return self.storage.dump_history()

class DataSync(BaseResource):
    def post(self):
        """
        Used for data sync between nodes.
        """
        name = request.json['name']
        value = request.json['value']
        author = request.json['author']
        timestamp = request.json['timestamp']

        self.storage.set(name, value, author, timestamp)
        return {name: value}, 201

    def delete(self):
        name = request.json['name']
        author = request.json['author']
        timestamp = request.json['timestamp']

        self.storage.delete(name, author, timestamp)
        return 200

class Nodes(BaseResource):
    def get(self):
        return {
            'online': list(self.gossip.online_nodes),
            'failed': self.gossip.fail_nodes,
            'offline': list(self.gossip.offline_nodes),
        }

    def post(self):
        node = request.json['node']
        self.gossip.propagate_data('add node', {'node': node})
        self.gossip.add_node(node, 'add node')
        return 200

    def delete(self):
        node = request.json['node']
        self.gossip.remove_node(node, 'rm node')
        return 200

class NodesSync(BaseResource):
    def get(self):
        return {
            'online': list(self.gossip.online_nodes),
            'failed': self.gossip.fail_nodes,
            'offline': list(self.gossip.offline_nodes),
        }

    def post(self):
        node = request.json['node']
        self.gossip.add_node(node, 'add node')
        return 200

    def delete(self):
        node = request.json['node']
        self.gossip.remove_node(node, 'rm node')
        return 200

class Shutdown(BaseResource):
    def post(self):
        self.gossip.denounce_self(self.myself)
        return 'Now you can shutdown the node.'


# -*- coding: utf-8 -*-
import json
import time
import hmac
import hashlib
import sys

ver = int(sys.version[0])
if ver >= 3:
    import http.client as httpcon
    from urllib.request import urlopen as urlop
    from urllib.parse import urlencode as urlenc
elif 2 <= ver < 3:
    from urllib import urlopen as urlop
    from urllib import urlencode as urlenc
    import httplib as httpcon

publicMethodList = ('last_price', 'ticker', 'trades', 'depth')
tradeMethodList = ('get_info', 'trade_history', 'active_orders', 'trade',
                   'cancel_order', 'withdraw', 'deposit_history',
                   'withdraw_history')

class Zaif(object):
    def __init__(self, api_key=None, api_secret=None):
        self.__api_key = str(api_key) if api_key is not None else ''
        self.__api_secret = str(api_secret) if api_secret is not None else ''
        self.__public_set = publicMethodList
        self.__trade_set = tradeMethodList

    def __api_query(self, method, options=None):
        if not options:
            options = {}
        base_url = 'https://api.zaif.jp/'

        if method in self.__public_set:
            request_url = base_url + 'api/1/' + method + '/' + options
            response = json.loads(urlop(request_url).read().decode('utf-8'))
            return response

        elif method in self.__trade_set:
            nonce = time.time()
            zaif_query = {"nonce": str(nonce), "method": method}

            if not options == {}:
                zaif_query.update(options)

            headers = {"key": self.__api_key,
                       "sign": hmac.new(self.__api_secret.encode('utf-8'),
                                        urlenc(zaif_query).encode('utf-8'),
                                        digestmod=hashlib.sha512).hexdigest()}

            session = httpcon.HTTPSConnection("zaif.jp")
            session.request("post",
                            "/tapi",
                            urlenc(zaif_query).encode('utf-8'),
                            headers)
            response = json.loads(session.getresponse().read().decode('utf-8'))
            session.close()

            if response['success'] == 1:
                return response

            elif response['success'] == 0:
                print(response)

    # PublicAPI

    def last_price(self, currency_pair):
        """終値を得る"""
        return self.__api_query('last_price', currency_pair)

    def ticker(self, currency_pair):
        """ティッカー"""
        return self.__api_query('ticker', currency_pair)

    def public_trade_history(self, currency_pair):
        """公開されている全ての取引履歴"""
        return self.__api_query('trades', currency_pair)

    def depth(self, currency_pair):
        """板情報"""
        return self.__api_query('depth', currency_pair)

    # TradeAPI

    def get_info(self):
        """
        現在の余力および残高、APIの権限、トレード数、
        アクティブ注文数、タイムスタンプを取得します
        """
        return self.__api_query('get_info')

    def your_trade_history(self, **options):
        """
        ユーザーの取引履歴取得
        オプション名    (必須, 説明, 型, 初期値)
        from_rec      (No, この順番のレコードから取得, 数値, 0)
        count         (No, 取得するレコード数, 数値, 1000)
        from_id       (No, このトランザクションIDのレコードから取得, 数値, 0)
        end_id        (No, このトランザクションIDのレコードまで取得, 数値, 無限)
        order         (No, ソート順, ASC (昇順)もしくは DESC (降順), DESC)
        since         (No, 開始タイムスタンプ, UNIX time, 0)
        end           (No, 終了タイムスタンプ, UNIX time, 無限)
        currency_pair (No, 通貨ペア。指定なしで全ての通貨ペア, (例) btc_jpy, 全ペア)
        """
        try:
            if options['from_rec']:
                options['from'] = options.pop('from_rec')
        except KeyError:
            pass
        return self.__api_query('trade_history', options)

    def active_orders(self, currency_pair=None):
        """
        現在有効な注文一覧
        オプション名    (必須, 説明, 型, 初期値)
        currency_pair (No, 通貨ペア。指定なしで全ての通貨ペア, (例) btc_jpy, 全ペア)
        """
        query = {}
        if currency_pair is not None:
            query['currency_pair'] = currency_pair
        return self.__api_query('active_orders', query)

    def trade(self, currency_pair, action, price, amount, limit=None):
        """
        注文を行う
        オプション名      (必須, 説明, 型)
        currency_pair	(Yes, 発注する通貨ペア, (例) btc_jpy)
        action	        (Yes, 注文の種類, bid もしくは ask)
        price	        (Yes, 指値注文価格, 数値)
        amount	        (Yes, 数量(例: 0.3), 数値)
        limit	        (No,  (リミット注文価格, 数値)
        """
        query = {'currency_pair': currency_pair,
                 'action': action,
                 'price': price,
                 'amount': amount}
        if limit is not None:
            query['limit'] = limit
        return self.__api_query('trade', query)

    def cancel_order(self, order_id):
        """
        注文の取り消し
        オプション名   (必須, 説明, 型)
        order_id     (Yes, 注文ID(tradeまたはactive_ordersで取得), 数値)
        """
        query = {'order_id': order_id}
        return self.__api_query('cancel_order', query)

    def withdraw(self, currency, address, amount, opt_fee=None):
        """
        暗号通貨の引き出しリクエスト
        オプション名  (必須, 説明, 型)
        currency	(Yes, 引き出す通貨	, btc / mona)
        address	    (Yes, 送信先のアドレス, 文字列)
        amount	    (Yes, 引き出す金額(例: 0.3), 数値)
        opt_fee	    (No,  採掘者への手数料(例: 0.003), 数値)
        """
        query = {'currency': currency,
                 'address': address,
                 'amount': amount}
        if opt_fee is not None:
            query['opt_fee'] = opt_fee
        return self.__api_query('withdraw', query)

    def deposit_history(self, currency, **options):
        """
        入金履歴の取得
        オプション名    (必須, 説明, 型, 初期値)
        currency      (Yes, jpy/btc/mona, 文字列, None)
        from          (No, この順番のレコードから取得, 数値, 0)
        count         (No, 取得するレコード数, 数値, 1000)
        from_id       (No, この入金IDのレコードから取得, 数値, 0)
        end_id        (No, この入金IDのレコードまで取得, 数値, 無限)
        order         (No, ソート順, ASC (昇順)もしくは DESC (降順), DESC)
        since         (No, 開始タイムスタンプ, UNIX time, 0)
        end           (No, 終了タイムスタンプ, UNIX time, 無限)
        """
        query = {'currency': currency}
        if not options == {}:
            query.update(options)
        return self.__api_query('deposit_history', query)

    def withdraw_history(self, currency, **options):
        """
        出金履歴の取得
        オプション名    (必須, 説明, 型, 初期値)
        currency      (Yes, jpy/btc/mona, TEXT)
        from          (No, この順番のレコードから取得, 数値, 0)
        count         (No, 取得するレコード数, 数値, 1000)
        from_id       (No, この出金IDのレコードから取得, 数値, 0)
        end_id        (No, この出金IDのレコードまで取得, 数値, 無限)
        order         (No, ソート順, ASC (昇順)もしくは DESC (降順), DESC)
        since         (No, 開始タイムスタンプ, UNIX time, 0)
        end           (No, 終了タイムスタンプ, UNIX time, 無限)
        """
        query = {'currency': currency}
        if not options == {}:
            query.update(options)
        return self.__api_query('withdraw_history', query)

    # StreamingApi

    def ws_depth(self, currency_pair):
        """
        websocket-clientモジュールが必要です
        https://github.com/liris/websocket-client
        確立されたコネクションを返しますので
        json.loads([return先].recv())をする毎に板情報を受け取れます
        """
        import websocket
        ws = 'ws://api.zaif.jp:8888/stream?currency_pair=%s' % currency_pair
        return websocket.create_connection(ws)


from prometheus_client import start_http_server, Summary, Gauge, Counter
import configparser
import os
import time
import sys
import nest

# Gauges
g = {
  'away': Gauge('nest_thermostat_away', 'Away status for entire structure (1=away 0=home 2=unkown)', ['structure']),
  'is_online': Gauge('nest_thermostat_is_online', 'Device connection status with the Nest service', ['structure', 'device']),
  'has_leaf': Gauge('nest_thermostat_has_leaf', 'Displayed when the thermostat is set to an energy-saving temperature', ['structure', 'device']),
  'target_temp': Gauge('nest_thermostat_target_temp', 'Desired temperature', ['structure', 'device']),
  'current_temp': Gauge('nest_thermostat_current_temp', 'Temperature, measured at the device', ['structure', 'device']),
  'humidity': Gauge('nest_thermostat_humidity', 'Humidity, in percent (%) format, measured at the device, rounded to the nearest 5%', ['structure', 'device']),
  'state': Gauge('nest_thermostat_state', 'Indicates whether HVAC system is actively heating, cooling or is off. Use this value to indicate HVAC activity state', ['structure', 'device']),
  'mode': Gauge('nest_thermostat_mode', 'Indicates HVAC system heating/cooling modes, like Heat•Cool for systems with heating and cooling capacity, or Eco Temperatures for energy savings', ['structure', 'device']),
  'time_to_target': Gauge('nest_thermostat_time_to_target', 'The time, in minutes, that it will take for the structure to reach the target temperature', ['structure', 'device']),

}

# Create a metric to track time spent and requests made and exceptions
REQUEST_TIME = Summary('request_processing_seconds', 'Time spent processing request')
REQUEST_EXCEPTIONS = Counter('nest_api_request_exceptions_total', 'Exceptions thrown while calling Nest API')

# Decorate function with metric.
def polling(napi):
    print("%s - Polling!" % time.time())
    with REQUEST_TIME.time():
      with REQUEST_EXCEPTIONS.count_exceptions():
        for structure in napi.structures:
            g['away'].labels(structure.name).set((1 if structure.away == "away" else 0 if structure.away == "home" else 2))
            for device in structure.thermostats:
                g['is_online'].labels(structure.name, device.name).set(device.online)
                g['has_leaf'].labels(structure.name, device.name).set(device.has_leaf)
                g['target_temp'].labels(structure.name, device.name).set(device.target)
                g['current_temp'].labels(structure.name, device.name).set(device.temperature)
                g['humidity'].labels(structure.name, device.name).set(device.humidity)
                g['state'].labels(structure.name, device.name).set((0 if device.hvac_state == "off" else 1))
                g['mode'].labels(structure.name, device.name).set((0 if device.mode == "off" else 1))
                g['time_to_target'].labels(structure.name, device.name).set(''.join(x for x in device.time_to_target if x.isdigit()))

if __name__ == '__main__':
    c = configparser.ConfigParser()
    c.read(os.path.join(os.path.abspath(os.path.dirname(__file__)),'settings.ini'))

    # Setup Nest account
    start_time = time.time()

    napi = nest.Nest(client_id=c['nest']['client_id'], client_secret=c['nest']['client_secret'], access_token_cache_file=os.path.join(os.path.abspath(os.path.dirname(__file__)),c['nest']['access_token_cache_file']))

    resp_time = time.time() - start_time
    sys.stderr.write("Nest API: %0.3fs\n" % resp_time)

    if napi.authorization_required:
      print("Go to " + napi.authorize_url + " to authorize, then enter PIN below")
      if sys.version_info[0] < 3:
        pin = raw_input("PIN: ")
      else:
        pin = input("PIN: ")
      napi.request_token(pin)

    # Start up the server to expose the metrics.
    listen_port = c['general']['port']
    start_http_server(int(listen_port))
    sys.stdout.write("Listening on port " + listen_port + "...\n")

    while True:
        polling(napi)
        time.sleep(int(c['general']['polling_interval']))


""" This Class has been combined into Bangazon.py"""
""" Would like to find a cleaner structure """

from OverRiding_Classes import *
import random

class Employee():

    def __init__(self, fn, ln):
        self.first_name = fn
        self.last_name = ln
        self.full_name = self.first_name + " " + self.last_name

    def eat(self, food=None, companions=None):
        randomRestaurant = ["Pizza Place", "Mexican Place", "Fast Food Place"]
        self.restaurant = random.choice(randomRestaurant)
        self.food = food
        self.companions = companions

        if food is None and companions is None:
            print ("{} ate lunch at {}".format(self.full_name, self.restaurant))
        elif food is None:
            print("{} ate at {} with {} from work.".format(self.full_name, self.restaurant,",".join(self.companions)))
        elif companions is None:
            print("{} ate {} at {}.".format(self.full_name, self.food, "the office"))
        else:
            print("{} ate a {} at {} with {}".format(self.full_name, self.food, self.restaurant, ", ".join(self.companions)))

        return self.restaurant

Bob_Dylan = Employee("Bob", "Dylan")
Bob_Dylan.eat(food="pizza", companions=['Bob', 'Bob', 'Dawn'])
Bob_Dylan.eat(food="pizza")


#
# Authors: Robert Abram <robert.abram@entpack.com>
#
# Copyright (C) 2015 EntPack
# see file 'LICENSE' for use and warranty information
#
# This program 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.
#
# This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
#

import os
import sys
from distutils.core import setup
from setuptools import find_packages

__VERSION__ = "0.6"

def find_package_data_files(dirs):
    paths = []
    for directory in dirs:
        for (path, directories, filenames) in os.walk(directory):
            for filename in filenames:
                paths.append(os.path.join('..', path, filename))
    return paths

def setup_package():

    # Recursively gather all non-python module directories to be included in packaging.
    core_files = find_package_data_files([
    ])

    setup(name='SilentDune-Server',
        version=__VERSION__,
        description='Silent Dune Server',
        author='Robert Abram',
        author_email='robert.abram@entpack.com',
        url='https://github.com/EntPack/SilentDune',
        packages=find_packages(exclude=['tests']),
        package_data={
            'silentdune_server': core_files,
        },
        keywords=['firewall', 'security'],  # arbitrary keywords
        classifiers=[
            'Development Status :: 4 - Beta',
            'Programming Language :: Python',
            'Environment :: Console',
            'License :: OSI Approved :: GNU General Public License v3 (GPLv3)',
            'Operating System :: POSIX :: Linux',
        ],

        install_requires=[
            'django',
            'django-oauth-toolkit',
            'djangorestframework',
            'django-compressor',
            'django-celery',
            'django-celery-with-redis',
            'django-countries',
            'django-axes',
            'django-filter',
            'cryptography',
            'mysqlclient',
            'requests',
            'raven',
            'sqlparse',
        ],

        tests_require=[
            'pytest',
            'pytest-runner',
            'pytest-pythonpath',
        ],
     )

if __name__ == "__main__":
    setup_package()


#!/usr/bin/python3
import csv
from operator import itemgetter
import sys

current_venue = ""
current_bat = ""
current_runs=0
current_deli=0

li=[]
tot_list=[]

for line in sys.stdin:
	line = line.strip()
	line_val = line.split(",")
	if(len(line_val)==5):
		venue, bat, runs, deli = line_val[0]+','+line_val[1], line_val[2], line_val[3], line_val[4]
	else:
		venue, bat, runs, deli = line_val[0], line_val[1], line_val[2], line_val[3]
	#print(venue, bat, runs,deli)
	try:
		runs=int(runs)
		deli=int(deli)
	except ValueError:
		continue
	if(current_venue == venue and current_bat == bat):
		current_runs += runs
		current_deli += deli
	else:
		if current_venue:
			li.extend([current_venue,current_bat,current_runs,current_deli])
			#print(li)
			tot_list.append(li)
		current_venue = venue
		current_bat = bat
		current_runs = runs
		current_deli = deli
	li=[]

if current_venue == venue and current_bat == bat:
	li.extend([current_venue,current_bat,current_runs,current_deli])
	#print(li)
	tot_list.append(li)

tot_list.sort()

final_list=list(filter(lambda x:x[3]>=10,tot_list))

current_venue = final_list[0][0]
hero=final_list[0][1]
max_strike=(100*final_list[0][2])/final_list[0][3]
max_runs=final_list[0][2]
for l in final_list[1:]:
	strike=(100*l[2])/l[3]
	if(current_venue == l[0] and strike>max_strike):
		hero=l[1]
		max_runs=l[2]
		max_strike=strike
	elif(current_venue == l[0] and strike==max_strike and l[2]>max_runs):
		hero=l[1]
		max_runs=l[2]
	elif(l[0]!=current_venue):
		print(current_venue,',',hero,sep="")
		current_venue=l[0]
		hero=l[1]
		max_runs=l[2]
		max_strike=strike
print(current_venue,',',hero,sep="")
	#print(l[0],',',l[1],',',l[2],',',l[3],strike)


import os
import sys

# Set compiler flags
cppPaths        = ['./include', '/usr/local/include']
cppDefines      = {}
cppFlags        = ['-Wall']
cxxFlags        = ['-std=c++14']
libs            = ['sndfile']
libPaths        = ['/usr/local/lib']
debugFlags      = ['-g', '-pg', '-fstack-check']
releaseFlags    = ['-O2']

# Establish default build environment
env = Environment(CCCOMSTR = "Compiling $TARGET",
                  LINKCOMSTR = "Linking $TARGET")

# Add verbose output from command-line
AddOption('--verbose',
          dest='verbose',
          type='string',
          nargs=1,
          action='store',
          metavar='DIR',
          help='verbose output')

# Add command-line prefix path construction variable
AddOption('--prefix',
          dest='prefix',
          type='string',
          nargs=1,
          action='store',
          metavar='DIR',
          help='installation prefix')
env = Environment(PREFIX = GetOption('prefix'))

# Create default environment
env.Append(CPPPATH      = cppPaths)
env.Append(CPPFLAGS     = cppFlags)
env.Append(CXXFLAGS     = cxxFlags)
env.Append(LIBS         = libs)
env.Append(LIBPATH      = libPaths)

# Check for debug build flag
debug = ARGUMENTS.get('debug', 0)
if int(debug):
    env.Append(CCFLAGS  = debugFlags)

# Check for release build flag
release = ARGUMENTS.get('release', 0)
if int(release):
    env.Append(CCFLAGS  = releaseFlags)

# Generate main executable
env.Object('./obj/WriteSoundFileData.o', './src/WriteSoundFileData.cpp')
env.Object('./obj/ReadSoundFileData.o', './src/ReadSoundFileData.cpp')
env.Object('./obj/SoundData.o', './src/SoundData.cpp')
env.Object('./obj/SoundFileHandle.o', './src/SoundFileHandle.cpp')
env.Object('./obj/Histogram.o', './src/Histogram.cpp')
env.Program('./bin/main', ['./obj/SoundFileHandle.o', './obj/SoundData.o',
                           './obj/Histogram.o', './obj/ReadSoundFileData.o',
                           './obj/WriteSoundFileData.o'])


import time
import paramiko
import threading
import os
import sys
import dateutil.parser
import unicodedata
import sqlite3
import datetime
from flask import Flask, current_app
from flaskr import db
from flaskr.db import get_db

APP_ROOT = os.path.dirname(os.path.abspath(__file__))
APP_STATIC = os.path.join(APP_ROOT, 'static')

threads = []
def updateLive(app, liveInterval, refreshSpeed):
	with app.app_context():
		db = get_db()
		print("Starting live status update.")
		while True:
			macAddressPool = db.execute(
				'SELECT macAddress, lastDetectTime, live, lastAliveTime, timeSpentLive FROM macAddressPool'
			).fetchall()

			for macAddress in macAddressPool:
				mac = macAddress[0]
				live = macAddress[2]

				if live == True:
					currentTime = macAddress[1]
					currentTime = (int(currentTime[:2]) * 60) + int(currentTime[-2:])

					# Get current system time in minutes starting from 0
					date = dateutil.parser.parse(datetime.datetime.now().isoformat())
					systemTime = str(date.time())[0:5]
					systemTime = (int(systemTime[:2]) * 60) + int(systemTime[-2:])

					if abs((currentTime - systemTime)) > liveInterval:
						db.execute('UPDATE macAddressPool SET live=? WHERE macAddress=?', [False, mac])
				db.commit()
			time.sleep(refreshSpeed)

def main(app, liveInterval, refreshSpeed):
	print("Starting Scheduler.")
	t = threading.Thread(target=updateLive, args=(app, liveInterval, refreshSpeed)) #node_combined[0] is ip, [1] is name
	t.dameon = True
	threads.append(t)
	t.start()

if __name__ == "__main__":
	main()


import os
import re
from bs4 import BeautifulSoup
import requests
from pathos.multiprocessing import ProcessPool as Pool

def get_index(id):
    i = 0
    url = 'http://www.513mj.com/index.php/video/play/id/{id}'.format(id=id)
    url_origin = 'http://v.51feetv.com'
    response = requests.get(url)
    soup = BeautifulSoup(response.text, 'lxml')
    path = soup.select('.F img')[0]['data-original']
    path = re.match('(.*/)',path).group(1)
    title = soup.select('.F .mt6')[0].get_text().strip().replace(' ','')
    local_path = '/Users/wang/movie/' + title
    mkdir(local_path)
    path = path + 'index.m3u8'
    index = requests.get(path)
    m3_path = parseM3u8(index.text)
    for line in m3_path:
        m3u8_name = re.search('/ppvod(.*?.m3u8)',line).group(1)
        ts_m3u8 = requests.get(url_origin+line)
        m3u8_path = local_path + m3u8_name
        with open(m3u8_path,'wb') as f:
            f.write(ts_m3u8.content)
        modify_m3u8(m3u8_path,local_path,url_origin)
        ts_path = parseM3u8(ts_m3u8.text)
        groups = [url_origin+ts for ts in ts_path]
        paths = [local_path] * len(groups)
        i = [i for i in range(0,len(groups)+1)]
        lens = [len(groups)] * len(groups)
        pool = Pool(4)
        pool.map(get_ts, groups,paths, i, lens)
    # combine(local_path, title)
        combine2(local_path,title,m3u8_path)

def parseM3u8(content):
    if "#EXTM3U" not in content:
        raise BaseException("非M3U8的链接")

    if "EXT-X-STREAM-INF" in content:  # 第一层
        file_line = content.split("\n")
        for line in file_line:
            if '.m3u8' in line:
                yield line

    if '#EXT-X-DISCONTINUITY' in content:
        file_line = content.split("\n")
        for line in file_line:
            if '.js' in line:
                yield line

def mkdir(path):
    # 去除首位空格
    path = path.strip()
    # 去除尾部 \ 符号
    path = path.rstrip("\\")

    isExists = os.path.exists(path)

    if not isExists:
        os.makedirs(path)
        print('创建成功')
    else:
        print('目录已存在')

def modify_m3u8(m3u8_path, path, url):
    i = 0
    with open(m3u8_path,'r') as f:
        lines =f.readlines()
    with open(m3u8_path,'w') as f_w:
        for line in lines:
            if '#EXT-X-KEY:METHOD=AES-128,' in line:
                old = re.search('URI="(.*)"',line).group(1)
                url = url + old
                response = requests.get(url)
                key_path = path+'/key.key'
                with open(key_path,'wb') as f:
                    f.write(response.content)
                new = '"{path}"'.format(path=key_path)
                line = '#EXT-X-KEY:METHOD=AES-128,URI=' + new + '\n'
                # re.sub('URI="(.*)"','{path}'.format(path=path+'/key.key'),line)
            if '#EXTINF:15,' in line:
                continue
            if 'http://v.51feetv.com/b.ts' in line:
                continue
            if '.js' in line:
                line = path + '/sp{i}.ts'.format(i=i) + '\n'
                i += 1
            f_w.write(line)

def get_ts(url, path, i, total):
    response = requests.get(url)
    path = path + '/sp{i}.ts'.format(i=i)
    with open(path,'wb') as f:
        f.write(response.content)
    print('下载完成 %s/%s' %(i,total))
# print(response.content)

def combine(path, title):
    exec_str = r'find ' + path + ' -name' + r' *.mp4 ' + '-exec rm -rf {} \\;'
    os.system(exec_str)
    ts = []
    lists = os.listdir(path)
    for file in lists:
        if file.endswith('.ts'):
            ts.append(file)
    ts.sort(key=lambda x:int(x[2:-3]))
    try:
        print("开始组合资源...")
        exec_str = 'cd {path} && ffmpeg -i'.format(path=path) + ' ' + '"concat:'
        for i in ts:
            exec_str = exec_str + i +'|'
        exec_str = exec_str[:-1] + '" -c copy ' + title +'.mp4'
        os.system(exec_str)  # 使用cmd命令将资源整合
        exec_str = r'find ' + path + ' -name' + r' *.ts ' + '-exec rm -rf {} \\;'
        os.system(exec_str)  # 删除原来的文件
    except:
        print("资源组合失败")
    else:
        print("资源组合成功!")

def combine2(path, title, m3u8_path):
    exec_str = r'find ' + path + ' -name' + r' *.mp4 ' + '-exec rm -rf {} \\;'
    os.system(exec_str)
    try:
        print("开始组合资源...")
        exec_str = 'ffmpeg -allowed_extensions ALL -i {m3u8} -c copy '.format(m3u8=m3u8_path) + path + '/{name}.mp4'.format(name=title)
        print(exec_str)
        os.system(exec_str)  # 使用cmd命令将资源整合
        exec_str = r'find ' + path + ' -name' + r' *.ts ' + '-exec rm -rf {} \\;'
        os.system(exec_str)  # 删除原来的文件
        os.remove(m3u8_path)

    except:
        print("资源组合失败")
    else:
        print("资源组合成功!")

if __name__ == '__main__':
    ids = input('输入ID: ')
    ids = ids.split(",")
    for id in ids:
        get_index(id)


"""
549. Binary Tree Longest Consecutive Sequence II

Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree.

Especially, this path can be either increasing or decreasing. For example, [1,2,3,4] and [4,3,2,1] are both considered valid, but the path [1,2,4,3] is not valid. On the other hand, the path can be in the child-Parent-child order, where not necessarily be parent-child order.

Example 1:

Input:
        1
       / \
      2   3
Output: 2
Explanation: The longest consecutive path is [1, 2] or [2, 1].

Example 2:

Input:
        2
       / \
      1   3
Output: 3
Explanation: The longest consecutive path is [1, 2, 3] or [3, 2, 1].
"""

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

# Runtime: 52 ms, faster than 75.74% of Python3 online submissions for Binary Tree Longest Consecutive Sequence II.
# Memory Usage: 14.4 MB, less than 100.00% of Python3 online submissions for Binary Tree Longest Consecutive Sequence II.
class Solution:
    def longestConsecutive(self, root: TreeNode) -> int:
        self.res = 0
        self.dfs(root)
        return self.res

    def dfs(self, root):
        if root is None:
            return 0, 0
        increase_len, decrease_len = 1, 1
        if root.left:
            left_increase, left_decrease = self.dfs(root.left)
            if root.val + 1 == root.left.val:
                increase_len = max(left_increase+1, increase_len)
            if root.val - 1 == root.left.val:
                decrease_len = max(left_decrease+1, decrease_len)
        if root.right:
            right_increase, right_decrease = self.dfs(root.right)
            if root.val + 1 == root.right.val:
                increase_len = max(increase_len, 1+right_increase)
            if root.val - 1 == root.right.val:
                decrease_len = max(decrease_len, 1+right_decrease)
        self.res = max(self.res, increase_len+decrease_len-1)
        return increase_len, decrease_len


# Create your views here.
from string import letters
from django.shortcuts import render_to_response, get_object_or_404
from models import Entry

def view_entry_list(request):
    ordered_entries = []
    for letter in letters[26:]:
        entries = Entry.live_entries.filter(lemma__istartswith=letter)
        if entries.count():
            ordered_entries.append({letter: entries})
        else:
            ordered_entries.append({letter: None})
    return render_to_response('lexicon_entry_list.html', {
        'entries_list': ordered_entries,
        'locale': request.locale,
        })

def view_edit_entry(request, lemma):
    entry = get_object_or_404(Entry, lemma=lemma)
    return render_to_response('lexicon_edit_entry.html', {
        'entry': entry
        })


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

"""
Name: web_server.py
Author: fengdi
Datetime: 19:10 2019-07-15
Description:

"""

import socket
import re
import time
import mini_frame
import multiprocessing

# 定义TCP服务器地址
HOST = ""
PORT = 7890
server_addr = (HOST, PORT)

# TCP服务器类
class Wsgiserver(object):

    def __init__(self, addr):
        """
        创建tcp服务器对象，绑定地址，开启监听
        :param addr: TCP服务器地址
        """
        self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.server_socket.bind(addr)
        self.server_socket.listen(128)

    def server_forever(self):
        """
        循环处理客户端连接
        :return:
        """
        while True:
            tmp_socket, addr = self.server_socket.accept()
            t1 = multiprocessing.Process(target=self.handler_req, args=(tmp_socket, ))
            t1.start()
            tmp_socket.close()
        self.server_socket.close()

    # 定义请求处理函数
    def handler_req(self, client_socket):
        """
        对客户端请求进行处理，返回需求数据
        :param client_socket: 临时套接字，与一个客户端通信
        :return: 返回请求资源，未找到返回404
        """

        # 长连接方式：接收请求数据
        while True:
            try:
                recv_data = client_socket.recv(1024).decode("utf-8")
                print(recv_data)
            except Exception as ret:
                print(">" * 50, ret)
                client_socket.close()
                return

            # 判断浏览器是否关闭
            if not recv_data:
                client_socket.close()
                return

            # 从请求数据内提取请求文件名
            request_lines = recv_data.splitlines()
            ret = re.match(r"[^/]+(/[^ ]*)", request_lines[0])

            file_name = ""
            if ret:
                file_name = ret.group(1)
                if file_name == "/":
                    file_name = "/index.html"

            if not file_name.endswith('.py'):
                # 根据请求文件名打开文件读取,返回文件内容给客户端
                try:
                    f = open("./html" + file_name, "rb")
                except FileNotFoundError:
                    response_body = "------file not found------"

                    response_header = "HTTP/1.1 404 not found\r\n"
                    response_header += "Content-Length: %d\r\n" % (len(response_body))
                    response_header += "\r\n"

                    response = response_header + response_body
                    client_socket.send(response.encode("utf-8"))
                else:
                    html_content = f.read()
                    f.close()
                    response_body = html_content

                    response_header = "HTTP/1.1 200 OK\r\n"
                    response_header += "Content-Length: %d\r\n" % (len(response_body))
                    response_header += "\r\n"

                    response = response_header.encode("utf-8") + response_body
                    client_socket.send(response)
            else:
                env = dict()
                response_body = mini_frame.application(env, self.set_response_header)

                response_header = "HTTP/1.1 %s\r\n" % self.status
                response_header += "Content-Length: %d\r\n" % (len(response_body.encode("utf-8")))

                for temp in self.headers:
                    response_header += "%s:%s\r\n" % (temp[0], temp[1])

                response_header += "\r\n"

                response = response_header + response_body
                client_socket.send(response.encode("utf-8"))

        client_socket.close()

    def set_response_header(self, status, headers):
        self.status = status
        self.headers = headers

def main():
    """
    主函数，控制整体
    :return:
    """
    myserver = Wsgiserver(server_addr)
    print("server running on port %s" % PORT)
    myserver.server_forever()

if __name__ == '__main__':
    main()


from django.contrib import admin, messages
from django.utils.translation import gettext_lazy as _
from django.utils.translation import ngettext

from core.admin import TracingMixinAdmin, make_private, make_public

from . import models

@admin.register(models.Track)
class TrackAdmin(TracingMixinAdmin):
    actions = [make_public, make_private]
    fields = ('name', 'description', 'owner', 'public', 'file')
    list_display = ('id', 'name', 'file', 'owner', 'public', 'entry_created_at', 'entry_updated_at')
    list_display_links = ('id', 'name')
    list_filter = ('public', 'entry_created_at', 'entry_updated_at')
    search_fields = ['id', 'name', 'owner__username']

@admin.register(models.Vote)
class VoteAdmin(TracingMixinAdmin):
    actions = ['make_positive', 'make_negative']
    autocomplete_fields = [
        'user',
    ]
    fields = (
        'user',
        'content_type',
        'object_id',
        'is_positive',
    )
    list_display = (
        '__str__',
        'id',
        'is_positive',
        'user',
        'content_object',
        'entry_created_at',
        'entry_updated_at',
    )
    list_display_links = (
        '__str__',
        'id',
    )
    search_fields = [
        'id__icontains',
        'user__username__icontains',
    ]

    @admin.action(description=_('Mark selected votes as positive'))
    def make_positive(self, request, queryset):
        updated = queryset.update(is_positive=True)
        self.message_user(request, ngettext(
            '%d vote was successfully marked as positive.',
            '%d votes were successfully marked as positive.',
            updated,
        ) % updated, messages.SUCCESS)

    @admin.action(description=_('Mark selected votes as negative'))
    def make_negative(self, request, queryset):
        updated = queryset.update(is_positive=False)
        self.message_user(request, ngettext(
            '%d vote was successfully marked as negative.',
            '%d votes were successfully marked as negative.',
            updated,
        ) % updated, messages.SUCCESS)


import numpy as np
from collections import deque
import cv2 as cv

def main():
    fourcc = cv.VideoWriter_fourcc(*'MJPG')
    out = cv.VideoWriter('data/output.avi',fourcc, 20.0, (1280, 720))

    cap = cv.VideoCapture(0)
     # wait for static background
    fback = wait_for_static_background(cap)
    while(1):

        _, frame = cap.read()
        frame = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
        if frame is None:
            break
        newframe = cv.absdiff(frame, fback)
        _, newframe = cv.threshold(newframe, 5, 255, cv.THRESH_TOZERO)

        circles = cv.HoughCircles(newframe, cv.HOUGH_GRADIENT, 1, 1000,
                               param1=100, param2=30,
                               minRadius=1, maxRadius=250)

        _, contours, _ = cv.findContours(np.copy(newframe), cv.RETR_EXTERNAL, cv.CHAIN_APPROX_NONE)

        newframe = resize_frame_to_out(newframe)
        newframe = cv.cvtColor(newframe, cv.COLOR_GRAY2BGR)
        if circles is not None:
            circles = np.uint16(np.around(circles))
            for i in circles[0,:]:
                # draw the outer circle
                cv.circle(newframe,(i[0],i[1]),i[2],(0,255,0),2)
                # draw the center of the circle
                cv.circle(newframe,(i[0],i[1]),2,(0,0,255),3)

            # find largest contour
            main_contour_i = 0
            for i in range(len(contours)):
                if len(contours[main_contour_i]) < len(contours[i]):
                    main_contour_i = i

            # find backbone subset
            backbone = []
            main_contour = contours[main_contour_i]
            top_pixel_i = 0
            for i in range(len(main_contour)):
                if main_contour[i][0][1] < main_contour[top_pixel_i][0][1]: # y coord
                    top_pixel_i = i

            backbone = main_contour[top_pixel_i:500]
            cv.polylines(newframe, backbone, True, (0, 0, 255), thickness=5)
            # out.write(newframe)
        else:
            cv.putText(newframe, "READY", (400, 400), cv.FONT_HERSHEY_SIMPLEX, 3, (0, 0, 200))
        cv.imshow("frame", newframe)
        k = cv.waitKey(30) & 0xff
        if k == 27:
            break

    cap.release()
    cv.destroyAllWindows()

def wait_for_static_background(cap):
    frames_count = 10
    frames_accamulator = deque(maxlen=frames_count)
    while (1):
        _, frame = cap.read()
        frame = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
        if len(frames_accamulator) == frames_count:
            diff_s = 0
            for prev_frame in frames_accamulator:
                diff = cv.absdiff(frame, prev_frame)
                _, diff = cv.threshold(diff, 35, 255, cv.THRESH_TOZERO)
                diff_s += cv.sumElems(diff)[0]
            print(diff_s)
            if diff_s < 3000:
                print("Static captured")
                return frame
        cv.imshow("frame", frame)
        k = cv.waitKey(30) & 0xff
        if k == 27:
            break
        frames_accamulator.append(frame)

def resize_frame_to_work(frame):
    W_HEIGHT = 720
    W_WIDTH = W_HEIGHT * 16 // 9
    return cv.resize(frame, (W_WIDTH, W_HEIGHT), cv.INTER_AREA)

def resize_frame_to_out(frame):
    O_HEIGHT = 720
    O_WIDTH = 1280
    return cv.resize(frame, (O_WIDTH, O_HEIGHT), cv.INTER_AREA)

def prepare_frame(frame):
    frame = resize_frame_to_work(frame)

    return frame

def draw_pixel(x, y):
    cv.circle(newframe,(x, y),2,(0,0,255),3)

if __name__ == '__main__':
    main()


# afficher_informations_personne
# Paramètres : nom, age
def afficher_informations_personne(nom, age, taille=0):
    print()
    print("Vous vous appelez " + nom + ", vous avez " + str(age) + " ans")
    # print(f"Vous vous appelez {nom}, vous avez {age} ans")
    # print("Vous vous appelez %s, vous avez %s ans" % (nom, age))

    print("L'an prochain vous aurez " + str(age + 1) + " ans")
    # print("L'an prochain vous aurez %s ans" % (age + 1))

    # == egal
    # < inférieur
    # <= inférieur ou égal
    # > supérieur
    # >= supérieur ou égal
    # True / False (Boolean)

    # age == 17 -> Vous êtres presque majeur
    # age == 18 -> Tout juste majeur : Félicitation
    # elif -> else if
    # age > 60 : Vous êtes sénior
    # age < 10 : Vous êtes enfant
    # Adolescent si age >= 12 et age < 18
    #          12 <= age < 18
    # Bebe si age == 1 ou age == 2
    if age == 17:
        print("Vous êtres presque majeur")
    elif 12 <= age < 18:
        print("Vous êtes adolescent")
    elif age == 1 or age == 2:
        print("Vous êtes un bébé")
    elif age == 18:
        print("Tout juste majeur : Félicitation")
    elif age > 60:
        print("Vous êtes sénior")
    elif age < 10:
        print("Vous êtes enfant")
    elif age >= 18:
        print("Vous êtes majeur")
    else:
        print("Vous êtes mineur")

    # afficher la taille
    if not taille == 0:
        print("Votre taille : " + str(taille) + " m")

def demander_nom():
    reponse_nom = ""
    while reponse_nom == "":
        reponse_nom = input("Quel est votre nom ? ")
    return reponse_nom

def demander_age(nom_personne):
    age_int = 0
    while age_int == 0:
        age_str = input(nom_personne + " quel est votre age ? ")
        try:
            age_int = int(age_str)
        except:
            print("ERREUR: Vous devez rentrer un nombre pour l'age")
    return age_int

# demander le nom
# nom1 = demander_nom()
# nom2 = demander_nom()
# nom1 = "personne1"
# nom2 = "personne2"

# demander l'age
# age1 = demander_age(nom1)
# age2 = demander_age(nom2)

# Afficher les résultats
# afficher_informations_personne(nom1, age1)
# afficher_informations_personne(nom2, age2)

NB_PERSONNES = 1

# la boucle for
for i in range(0, NB_PERSONNES):
    nom = "personne" + str(i+1)
    age = demander_age(nom)
    afficher_informations_personne(nom, age, 1.60)

print("""
Vous
    mettez
        ce que vous voulez
""")

print("toto", 20, "ans", "taille:", 1.70)


#coding=utf-8
from bs4 import BeautifulSoup
import os

class Html2Text(object):

    def html2Text(self, ensoup, cnsoup):
        """
        :type ensoup: BeautifulSoup
        :type cnsoup: BeautifulSoup
        :rtype: List[str]
        """
        # ensoup = BeautifulSoup(open(enpath), 'lxml')
        # cnsoup = BeautifulSoup(open(cnpath), 'lxml')

        enstr = ""
        cnstr = ""
        if ensoup.title != None:
            enstr = enstr + ensoup.title.getText() + '\n'
        for string in ensoup.body.stripped_strings:
            enstr += (string + '\n')

        if cnsoup.title != None:
            cnstr += cnsoup.title.getText() + '\n'
        for string in cnsoup.body.stripped_strings:
            cnstr += (string + '\n')

        return [enstr, cnstr]


#!/bin/python3.6
# -*- coding: utf-8 -*-
#
# MIT License
#
# Copyright (c) 2018 Robert Gustafsson
# Copyright (c) 2018 Andreas Lindhé
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import asyncio

class QuorumSend:
    def __init__(self, quorum_size, protocol):
        self.pongRx = {}
        self.protocol = protocol
        self.Q = quorum_size
        self.replies = quorum_size
        self.pingTx = None
        self.event = None
        self.aggregated = None

    async def phaseInit(self, m, opt_size=None):
        if opt_size:
            self.replies = opt_size
        else:
            self.replies = self.Q
        self.pongRx.clear()
        if (type(m[1]) == list):
            self.pingTx = []
            for i in range(len(m[1])):
                self.pingTx.append(self.protocol(m[0], m[1][i], *m[2:]))
        else:
            self.pingTx = self.protocol(*m)
        self.event = asyncio.Event()
        await self.event.wait()
        x = self.aggregated
        self.aggregated = None
        return x

    async def departure(self, server_id, payload):
        if __debug__:
            print("pingpong arrival! ")
        if (type(self.pingTx) == list):
            pingTx = self.pingTx[server_id]
        else:
            pingTx = self.pingTx

        if payload and (pingTx != None):
            msg_list = self.protocol.set_message(payload)
            msg = self.protocol(*msg_list)
            if(msg.get_req_tag() == pingTx.get_tag() and
               (msg.get_tag() == None or
               msg.get_label() == 'qry' or
               (msg.get_label() != 'qry' and
               (msg.get_tag() == msg.get_req_tag() and (
                msg.get_label() == pingTx.get_label())
              )))):
                self.pongRx[server_id] = msg
                if __debug__:
                    print("ADD to pongRx with size %s" % len(self.pongRx))
        elif not payload:
            self.pongRx.pop(server_id, None)

        if len(self.pongRx) >= self.replies:
            if __debug__:
                print("GOT ENOUGH elements")
            self.aggregated = list(self.pongRx.values())
            self.pongRx.clear()
            self.pingTx = None
            self.event.set()

        if (type(self.pingTx) == list):
            data = self.pingTx[server_id].get_bytes()
            return (data, not self.use_tcp(self.pingTx[server_id]))
        else:
            data = self.pingTx.get_bytes() if self.pingTx else None
            return (data, not self.use_tcp(self.pingTx))

    def use_tcp(self, tx):
        if not tx:
            return False
        if (len(tx.get_bytes()) > 512):
           return True
        elif ((tx.get_label() == 'fin' or tx.get_label() == 'FIN') and tx.get_mode() == 'read'):
            return True
        else:
            return False


import cv2

cap = cv2.VideoCapture(0)

while True:
    _, frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    blur = cv2.GaussianBlur(gray, (5, 5), 0)
    edge = cv2.Canny(blur, 10, 70)
    _, mask = cv2.threshold(edge, 70, 255, cv2.THRESH_BINARY)
    # cv2.imshow("original", frame)
    # cv2.imshow("gray", gray)
    # cv2.imshow("blur", blur)
    # cv2.imshow("edge", edge)
    cv2.imshow("mask", mask)
    if cv2.waitKey(1) == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()


#Day mean, Day var, Day std, Day max, Day Min

# -*- coding: utf-8 -*-
# Auther guitar79@naver.com

import numpy as np
import os
import pandas as pd
import matplotlib.pyplot as plt
np.set_printoptions(threshold=100)
def date(n,m):#From n to m-1
    return ((f['측정일시']-n)//(m-n) == 0)

#def place(n)

def array2string(x):
    return '\n'.join(','.join('%0.2f' %x for x in y) for y in x)

# data head
# ['지역', '측정소코드', '측정소명', '측정일시', 'SO2', 'CO', 'O3', 'NO2', 'PM10', 'PM25', '주소']

#th_list = [0,5,10,15,20,25,30,35,40,45,50,55,60,65]

#base directory
drbase = '/media/guitar79/8T/RS_data/Remote_Sensing/2017RNE/airkorea/'
#read directory(input data)
drin = 'temp/'
#write directory(output data)
drout = 'output/'

#make empty dataframe(1,11) with head
#head = ('지역', '측정소코드', '측정소명', '측정일시', 'SO2', 'CO', 'O3', 'NO2', 'PM10', 'PM25', '주소')
head = ('', '111111', '', '', '', '', '', '', '', '', '')
head = np.array(head)
head = head.reshape(1,11)
f = pd.DataFrame(head)
#empty dataframe(1,11) columns header
f.columns = (['지역', '측정소코드', '측정소명', '측정일시', 'SO2', 'CO', 'O3', 'NO2', 'PM10', 'PM25', '주소'])

#read data files
for i in sorted(os.listdir(drbase+drin)):
    #read csv files
    if i[-4:] == '.csv':
        #make data frame from reading csv files (like table)
        g = pd.read_csv(drbase+drin+i, encoding='euc_kr')
        g.columns = (['지역', '측정소코드', '측정소명', '측정일시', 'SO2', 'CO', 'O3', 'NO2', 'PM10', 'PM25', '주소'])

        f = f.append(g,ignore_index=True)
        #count number of the total data

f = f.drop(f.index[[0,1]])
total_datanum = len(f)

#statistics of total data
total_mean_value = array2string(f.mean().values.reshape(1,11))
total_var_value = array2string(f.var().values.reshape(1,11))
total_std_value = array2string(f.std().values.reshape(1,11))
total_max_value = array2string(f.max().values.reshape(1,11))
total_min_value = array2string(f.min().values.reshape(1,11))

#write files from statistics
with open(drbase+drout+'statistics_total.csv', 'w') as o:
    print(total_datanum,',','mean,',total_mean_value,'\n','var,',total_var_value,'\n','std,',total_std_value,'\n','max,',total_max_value,'\n','min,',total_min_value,'\n', file=o)

#make observatory code index
#ocode = list(set(np.array(f.ix[:,1])))
ocode = list(set(np.array(f.ix[:,'측정일시'])//100))
#count number of the observatory
onumber = len(ocode)

for obs in sorted(ocode):
    fo=f.loc[date(obs*100+1,obs*100+25)]
    #fo=f[f['측정소코드']].isin(obs)
    o_datanum = len(fo)
    if o_datanum != 0:
        o_mean_value = array2string(fo.mean().values.reshape(1,11))
        o_var_value = array2string(fo.var().values.reshape(1,11))
        o_std_value = array2string(fo.std().values.reshape(1,11))
        o_max_value = array2string(fo.max().values.reshape(1,11))
        o_min_value = array2string(fo.min().values.reshape(1,11))
        obss = str(obs)
        with open(drbase+drout+'statistics.csv', 'a') as o:
            print(obs, o_datanum, o_mean_value, o_var_value, o_std_value, o_max_value, o_min_value, file=o)
            print('-----------------------------------------------------------', file=o)
            #print(obs)
    else:
        with open(drbase+drout+'statistics.csv', 'a') as o:
            print(obs, 'MaM', 'MaM', 'MaM', 'MaM', 'MaM', 'NaN', file=o)
            #print('asdf')

'''
total_mean_value = f.mean().values.reshape(11,1)
total_var_value = f.var().values.reshape(11,1)
total_std_value = f.std().values.reshape(11,1)
total_max_value = f.max().values.reshape(11,1)
total_min_value = f.min().values.reshape(11,1)

#self.output += tds.text
#csv delimeter
#self.output += ','
#csv delimeter
#self.output += '\n'

      for obs in ocode:
                fo=f.loc[f[:,1] == obs]
                #fo=f[f['측정소코드']].isin(obs)
                o_datanum = len(fo)
                if o_datanum != 0:
                    o_mean_value = fo.mean().reshape(1,11)
                    o_var_value = fo.var().reshape(1,11)
                    o_std_value = fo.std().reshape(1,11)
                    o_max_value = fo.max().reshape(1,11)
                    o_min_value = fo.min().reshape(1,11)
                    with open(drbase+drout+'statistics_'+obs+'o.txt', 'a') as o:
                        print(i, obs, o_datanum, o_mean_value, o_var_value, o_std_value, o_max_value, o_min_value, file=o)
                else:
                    with open(drbase+drout+'statistics_'+obs+'o.txt', 'a') as o:
                        print(i, obs, 'NaN', 'NaN', 'NaN', 'NaN', 'NaN', 'NaN', file=o)
'''


# Definition for a binary tree node.
class TreeNode(object):
  def __init__(self, x):
    self.val = x
    self.left = None
    self.right = None

class Solution(object):
  def helper(self,node,p,q):
    if node is None:
      return -1
    a = self.helper(node.left,p,q)
    b = self.helper(node.right,p,q)
    if self.solution is not None:
      return
    elif (a==0 and b==1) or (a==1 and b==0) or (node==p and (a==1 or b==1)) or (node==q and (a==0 or b==0)):
      self.solution = node
    elif a==0 or b==0 or node==p:
      return 0
    elif a==1 or b==1 or node==q:
      return 1
    elif a==-1 and b==-1:
      return -1
  def lowestCommonAncestor(self, root, p, q):
    if p==q:
      return p
    self.solution = None
    self.helper(root,p,q)
    return self.solution