"""Module implementing classes for reading and writing bits."""
import math

class BitInputStream():
    """Class handling streaming bits from the input."""

    def __init__(self, inp):
        """Initialize new instance with input and empty buffer."""
        self.input = inp
        self.bitbuffer = 0
        self.buffersize = 0

    def read_bit(self):
        """Stream bit from buffer.

        If buffer is empty, read from input.
        """
        if self.bitbuffer == -1:
            return -1
        if self.buffersize == 0:
            temp = self.input.read(1)
            if len(temp) == 0:
                self.bitbuffer = -1
                return -1
            self.bitbuffer = temp[0]
            self.buffersize = 8
        self.buffersize -= 1
        return (self.bitbuffer >> self.buffersize) & 1

    def close(self):
        """Close stream by closing input and reseting buffer."""
        self.input.close()
        self.bitbuffer = -1
        self.buffersize = 0

class BitOutputStream():
    """Class handling streaming bits to the output."""

    def __init__(self, out):
        """Initialize new instance with output and empty buffer."""
        self.output = out
        self.bitbuffer = 0
        self.buffersize = 0
        self.totalbytes = 0
        self.sym_counter = [0 for _ in range(256)]

    def write_bit(self, bit):
        """Stream bit to the buffer.

        If buffer is full, write to input.
        """
        if bit not in (0, 1):
            raise ValueError("Argument must be 0 or 1")
        self.bitbuffer = (self.bitbuffer << 1) | bit
        self.buffersize += 1
        if self.buffersize == 8:
            towrite = bytes((self.bitbuffer,))
            self.output.write(towrite)
            self.totalbytes += 1
            self.sym_counter[self.bitbuffer] += 1
            self.bitbuffer = 0
            self.buffersize = 0

    def close(self):
        """Close stream by flushing buffer and closing output."""
        while self.buffersize != 0:
            self.write_bit(0)
        self.output.close()

    def get_totalbytes(self):
        """Return total bytes written to the output."""
        return self.totalbytes

    def get_output_entropy(self):
        """Calculate and return entropy of the output code."""
        return sum([x*(math.log(self.totalbytes, 2)-math.log(x, 2)) for x in self.sym_counter if x > 0])/self.totalbytes


class Solution:
    def find132pattern(self, nums: List[int]) -> bool:
        stk = []
        right = float("-inf")
        for i in range(len(nums)-1, -1, -1):
            if nums[i] < right: return True
            while len(stk)!=0 and nums[i] > stk[-1]:
                right = max(right, stk[-1])
                stk.pop()
            stk.append(nums[i])

        return False


import logging
import math
import struct
import usb.util

def to_hex(val):
	return ' '.join([bytes([i]).hex() for i in val])

class ControlVaultCommunicator:
	def __init__(self, device, spi_master=0x01, spi_slave=0x00, spi_crc=0x00):
		self.logger = logging.getLogger(__name__)
		self.device = device
		self.bulk_in, self.bulk_out = self._find_endpoints()

		self.spi_master = spi_master
		self.spi_slave = spi_slave
		self.spi_crc = spi_crc
		self.spi_slave_prefix = struct.pack('>BB', self.spi_slave, self.spi_crc)

	def ctrl_transfer(self, *args, **kwargs):
		self.logger.debug('Control: {} {}'.format(args, kwargs))
		return self.device.ctrl_transfer(*args, **kwargs)

	def write(self, *args, **kwargs):
		return self.bulk_out.write(*args, **kwargs)

	def read(self, *args, **kwargs):
		return self.bulk_in.read(*args, **kwargs)

	def send_packet(self, payload):
		length = len(payload)
		packet = struct.pack('>BBH', self.spi_master, self.spi_crc, length) + payload
		self.logger.debug('Put: {}'.format(to_hex(packet)))
		self.write(packet)

	def recv_packet(self):
		packet = self.read(64, timeout=5000).tobytes()
		tag = packet[0:2]
		if tag != self.spi_slave_prefix:
			raise Exception('Unknown tag: {}'.format(tag.hex()))
		length = packet[2:4]
		length = struct.unpack('>H', length)[0]

		for i in range(0, math.ceil(length/64) - 1):
			packet += self.read(64).tobytes()

		self.logger.debug('Got: {}'.format(to_hex(packet)))
		return packet[4:]

	def talk(self, exchange):
		for packet in exchange:
			self.send_packet(bytes.fromhex(packet))
			data = self.recv_packet()

			if data[1] == 0x61:
				packet = self.recv_packet()

	def _find_endpoints(self):
		self.logger.debug('Enumerating interfaces...')
		configuration = self.device.get_active_configuration()
		bcm_interface = None
		has_contacted = False
		has_contactless = False
		for interface in configuration:
			if interface.bInterfaceClass == 0xff:
				if bcm_interface is not None:
					raise Exception('More than one vendor-specific interface found!')
				bcm_interface = interface
			if interface.bInterfaceClass == 0xb:
				if interface.iInterface == 0x5:
					has_contacted = True
				if interface.iInterface == 0x6:
					has_contactless = True
		if not has_contactless:
			raise Exception('No contactless reader on this device!')
		if bcm_interface is None:
			raise Exception('Cannot find vendor-specific interface')
		self.logger.debug('Interface found: {}'.format(bcm_interface._str()))

		self.logger.debug('Enumerating endpoints...')
		bulk_in = None
		bulk_out = None
		for endpoint in bcm_interface:
			if endpoint.bmAttributes & usb.util._ENDPOINT_TRANSFER_TYPE_MASK == usb.util.ENDPOINT_TYPE_BULK:
				if endpoint.bEndpointAddress & usb.util._ENDPOINT_DIR_MASK == usb.util.ENDPOINT_IN:
					if bulk_in is not None:
						raise Exception('More than one BULK IN endpoint found!')
					bulk_in = endpoint
					self.logger.debug('BULK IN found: {}'.format(bulk_in._str()))
				if endpoint.bEndpointAddress & usb.util._ENDPOINT_DIR_MASK == usb.util.ENDPOINT_OUT:
					if bulk_out is not None:
						raise Exception('More than one BULK OUT endpoint found!')
					bulk_out = endpoint
					self.logger.debug('BULK OUT found: {}'.format(bulk_out._str()))

		if bulk_in is None:
			raise Exception('BULK IN endpoint not found!')
		if bulk_out is None:
			raise Exception('BULK OUT endpoint not found!')

		self.logger.debug('Endpoint discovery successful.')
		return bulk_in, bulk_out


class Node(object):
    def __init__(self, data):
        self.data = data
        self.next = None

class Link(object):
    def __init__(self):
        self._head = None

    def add(self, data):
        node = Node(data)
        node.next = self._head
        self._head = node

    def travel(self):
        cur = self._head
        while cur:
            print(cur.data)
            cur = cur.next

    def isEmpty(self):
        return self._head==None

    def size(self):
        cur = self._head
        count = 0
        while cur:
            count+=1
            cur = cur.next

        return count

    def append(self,data):
        node = Node(data)
        if self._head == None:
            self._head = node
            return
        else:
            cur = self._head
            pre = None
            while cur:
                pre = cur
                cur = cur.next

            pre.next = node

    def search(self,data):
        find = False
        cur = self._head
        while cur:
            if cur.data == data:
                find = True
                break
            else:
                cur = cur.next
        return find

    def insert(self,position,data):
        node = Node(data)
        pre = None
        cur = self._head
        for i in range(position):
            pre = cur
            cur = cur.next
        pre.next= node
        node.next = cur

    def remove(self,data):
        pass

l = Link()
l.add(2)
l.add(3)

l.travel()

print(l.size())

基础算法/算法/链表.py


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

"""
根据半径计算圆的周长和面积
"""
import math

radius = float(input("请输入半径：\n"))
circumference = 2 * math.pi * radius
area = math.pi * math.pow(radius, 2)
print("周长为%.2f" % circumference)
print("面积为%.2f" % area)


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [
        ('scenic', '0017_auto_20170512_0906'),
    ]

    operations = [
        migrations.CreateModel(
            name='Facilities',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('name', models.CharField(default=b'-', max_length=512, blank=True)),
                ('scenic_facilities', models.ForeignKey(to='scenic.Scenic', null=True)),
            ],
            options={
                'ordering': ['-id'],
            },
        ),
        migrations.CreateModel(
            name='Point',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('latitude', models.TextField(null=True, blank=True)),
                ('facilities_point', models.ForeignKey(to='scenic.Facilities', null=True)),
            ],
            options={
                'ordering': ['-id'],
            },
        ),
    ]


# Process of responding to exceptions in a cuustom way during the execution of a program
# Try: code
#except: error message why code isn't running

try:
    numerator = int(input('Enter numerator:'))
    denominator = int(input('Enter denominator:'))

    result = numerator/denominator
    print(result)

    example_list = [2,5,6,7,8,9]
    i = int(input('Enter index'))
    print(example_list[i])
except ValueError:
    print('You entered a float or string')
except ZeroDivisionError:
    print('Zero can\'t divide a number')
except IndexError:
    print('Index isn\'t within list range')


import serial
import time

class Arduino():

    def connect_arduino(self):
        conn = False
        print("Searching for Arduino...")
        while not conn:
            if(self.ser.inWaiting()>0):
                data = self.ser.read()
                data = str(data)
                print(data)
#                if data == 'a':
#                    self.ser.write('a')
#                    time.sleep(0.001)
#                if data == 'b':
#                    self.ser.flush()
#                    self.ser.write('b')
#                    conn = True
#                    print "Connection to Arduino established"
#                time.sleep(0.001)

    def __init__(self):
        self.ser = serial.Serial('COM6', 9600)
        self.ser.flushInput()
        self.connect_arduino()

    def send_command(self, command):
        self.ser.write(command)


num=int(input("Enter a number to check whether palindrome : "))
og = num
rev= 0

while num != 0:
    rem = num % 10
    rev = rev * 10 + rem
    num //= 10

if rev == og:
    print ("Palindrome")
else:
    print("Not palindrome")

# 123
# 1+2+3 = 6


##

from tkinter import *
import time
import random

tk = Tk()
tk.title("Игра Арканоид")
tk.resizable(0,0)
tk.wm_attributes("-topmost",1)
canvas = Canvas(tk, width=500, height=400, bd=0, highlightthickness=0)
canvas.pack()
tk.update()

class Board:
    def __init__(self, canvas, color):
        self.canvas = canvas
        self.board_width = 150
        self.id = canvas.create_rectangle(0,0,self.board_width,15,fill=color)

        self.canvas_height = self.canvas.winfo_height()
        self.canvas_width = self.canvas.winfo_width()

        self.x = 0
        self.speed_x = 5

        self.xx = self.canvas_width / 2 - self.board_width /2
        self.yy = self.canvas_height - 40
        self.canvas.move(self.id, self.xx, self.yy)
    def move_left(self, event):
        if self.xx + (-1)*self.speed_x >= 0:
            self.x = (-1)*self.speed_x
            self.xx = self.xx + self.x
            self.draw()
        #print("left pressed")
    def move_right(self, event):
        if self.xx+self.speed_x <= self.canvas_width - self.board_width:
            self.x = self.speed_x
            self.xx = self.xx + self.x
            self.draw()
        #print("right pressed")
    def draw(self):
        self.canvas.move(self.id, self.x, 0)

class Ball:
    def __init__(self, canvas, color):
        self.canvas = canvas
        self.id = canvas.create_oval(10,10,25,25,fill=color)
        self.canvas.move(self.id, 250, 150)
        start = [-3,-2,-1,1,2,3]
        random.shuffle(start)
        self.x = start[0]
        self.y = start[1]
        self.canvas_height = self.canvas.winfo_height()
        self.canvas_width = self.canvas.winfo_width()
    def draw(self):
        self.canvas.move(self.id, self.x, self.y)
        pos = self.canvas.coords(self.id)
        #print(pos)
        #       x1      y1      x2      y2
        #print(pos[0], pos[1], pos[2], pos[3])

        if pos[1]<=0:
            #self.y = 1
            self.y = self.y * (-1)
        if pos[3]>=self.canvas_height:
            #self.y = -1
            self.y = self.y * (-1)

        if pos[0]<=0:
            #self.x = 1
            self.x = self.x * (-1)
        if pos[2]>=self.canvas_width:
            #self.x = -1
            self.x = self.x * (-1)

ball = Ball(canvas, "red")
board = Board(canvas, "blue")
canvas.bind_all("<KeyPress-Left>",board.move_left)
canvas.bind_all("<KeyPress-Right>",board.move_right)

while 1:
    ball.draw()
    tk.update_idletasks()
    tk.update()
    time.sleep(0.005)


# import asyncio
import pandas as pd
import numpy as np

from datetime import datetime
from v2.historical_data import HistoricalData
from v2.binance_client import BinanceClient

client = BinanceClient(futures=False)
history = HistoricalData(client)

symbol = "DOGEUSDT"
interval = "1m"
fromDate = int(datetime.strptime('2022-12-13 00:00:00', '%Y-%m-%d %H:%M:%S').timestamp() * 1000)
toDate = int(datetime.strptime('2022-12-14 23:00:00', '%Y-%m-%d %H:%M:%S').timestamp() * 1000)

file_dir = 'data/{}_{}_{}.csv'.format(symbol, fromDate, toDate)

# data = history.get_historical_data(interval, symbol, fromDate, toDate)
# df = history.get_data_frame(data)
# df.to_csv(file_dir)
def round_2(x):
    try:
        return round(x,6)
    except:
        return x

first_index_larger=lambda seq, m: [ii for ii in range(0, len(seq)) if seq[ii] > m][0]
first_index_smaller=lambda seq, m: [ii for ii in range(0, len(seq)) if seq[ii] < m][0]

def draw_sth():

    import matplotlib.pyplot as plt

    x= df.index.tolist()
    y1= df['Average']
    plt.plot(x,y1)
    plt.xlabel('X-axis')
    plt.ylabel('Y-axis')
    plt.title("A simple line graph")
    plt.show()

def cal_time(date_time, hours_wating=5):
    from datetime import timedelta
    given_time = datetime.strptime(date_time, '%Y-%m-%d %H:%M:%S')
    final_time = given_time - timedelta(hours=hours_wating)
    return final_time

def read_and_find_history(percentage=5, percentage_loss=2, hours_wating=5):
    global file_dir
    df = pd.read_csv(file_dir)
    df = df[df['Volume'] != 0]
    count = df['Volume'].count()
    print('=> Total:' + str(count))

    positive_name = '+{}%'.format(percentage)
    negative_name = '-{}%'.format(percentage)

    df['Average'] = df[['High','Close']].mean(axis=1).astype(float).apply(round_2)
    df[positive_name] = (df['Average'] * (1 + percentage/100)).astype(float).apply(round_2)
    df[negative_name] = (df['Average'] * (1 + (-1*percentage_loss/100))).astype(float).apply(round_2)
    # index = df.index.tolist()

    # Delete columns
    df.drop('Date', inplace=True, axis=1)
    df.drop('Open', inplace=True, axis=1)
    df.drop('High', inplace=True, axis=1)
    df.drop('Close', inplace=True, axis=1)
    df.drop('Low', inplace=True, axis=1)
    df.drop('Time', inplace=True, axis=1)

    # get data as arrays
    average = df['Average'].to_numpy()
    negative_percent = df[negative_name].to_numpy()
    positive_percent = df[positive_name].to_numpy()
    timestamp = df['Timestamp'].to_numpy()
    take_profit_time = (np.ones(count) * -1).astype(str)
    order_price_est = (np.ones(count) * -1).astype(float)
    order_price = (np.ones(count) * -1).astype(float)
    order_time = (np.ones(count) * -1).astype(str)

    for i, v in enumerate(positive_percent.copy()):
        """
            Get from current index to index has increased by x percentage
            In the middle of array, there's no value blew -x percentage (negative_percent)
        """
        if v <= average[i:].max() and v >= average[i:].min():
            j = np.where(average[i:] > v)[0][0] + i
            if negative_percent[i] < average[i:j].min():

                take_profit_time[i] = timestamp[j]

                # Find pre price and index (before it direct to +x percentage)
                price_pre_est = round_2(average[j] * (1 + (-1*percentage/100)))
                if average[i:j].min() < price_pre_est:

                    price_est = average[:j].min() #interval_est.mean()
                    index_est = np.where(average[:j] <= price_est)[0][-1]
                    if index_est != 0 and index_est > i:
                        # Find time of price (before it direct to +x percentage)
                        order_price[i] = average[index_est]
                        order_time[i] = timestamp[index_est]

    # reindex to original indices
    df['TakeProfitPrice'] = df[positive_name]
    df['TakeProfitTime'] = take_profit_time
    # df['OrderPriceEst'] = order_price_est
    df['OrderPrice'] = order_price
    df['OrderTime'] = order_time
    df['OrderPrice'] = df['OrderPrice'].astype(float).apply(round_2)
    df = df[df['TakeProfitTime'] != '-1']
    df = df[df['OrderTime'] != '-1.0']

    df.drop(positive_name, inplace=True, axis=1)
    df.drop(negative_name, inplace=True, axis=1)

    df['WaitingTime'] = df['OrderTime'].map(cal_time)
    df = df[df['WaitingTime'] < df['Timestamp']]
    file_dir = 'data/edited_{}_{}_{}.csv'.format(symbol, fromDate, toDate)
    df.to_csv(file_dir)
    print(df.head())
    print('=> After Total:' + str(df['Volume'].count()))
read_and_find_history()


from libs.Plugin_Base import PluginBase
from queue import Queue

class KeDaCamera(PluginBase):

    # 构造方法
    def __init__(self):
        self._to_port = 5513
        self._my_ip = self.get_local_ip()
        self.get_my_port()
        self.pkg = b"\x4e\x44\x54\x31\x08\x00\x27\xce\x13\x0b\x00\x00\x00\x00\x00\x00\x00\x01\x31\x2e" \
                   b"\x33\x2e\x30\x2e\x30\x00\x32\x32\x34\x2e\x31\x36\x2e\x33"

    # 获取数据
    def start(self, q, t_ip):
        self.q = q
        self._to_ip = t_ip
        result = self.send_one(self.pkg)
        if result[1]:
            dic = {}
            dic['ip'] = result[1][0]
            dic['info'] = result[0]
            q.put(dic)

# 插件信息函数
def get_plugin_info():
    plugin_info = {
        "name": "Keda_Camera.py",
        "model_name": "Keda_Camera",
        "info": "用于扫描科达品牌摄像头的插件",
        "type": "camera",
        "brand": "科达",
    }
    return plugin_info

#启动扫描类
class Check():
    def __init__(self):
        self.hika = KeDaCamera()

    @staticmethod
    def check(q, t_ip):
        Check().hika.start(q, t_ip)

if __name__ == '__main__':
    t_ip = '192.168.0.152'
    q = Queue()
    Check().check(q, t_ip)


while True:
    even_sum = 0
    odd_sum = 0
    enter = input("Enter integer:  ")
    if enter!= "Done":
        for x in range(1, int(enter)+1):
            if x % 2 == 0:
                even_sum = even_sum + x
            elif x % 2 != 0:
                odd_sum = odd_sum + x
        print("The sum of even number in this range is ", even_sum)
        print("The sum of odd number in this range is ", odd_sum)

    else:
        break


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

# # Figures for the paper

# In[1]:

from scipy.integrate import quad
from numpy import *
from matplotlib.pylab import *
import matplotlib as mpl
import os
import sys

# changing parameters

try:
    assert len(sys.argv)==2, 'No correct number of arguments'
    from data_Fullset import SCxL23 as exppar
    print('No shrinkage correction')
    shflag = ''
except:
    assert len(sys.argv)==3, 'No correct number of arguments'
    shrinkage = float(sys.argv[2])
    if shrinkage > 0.0:
        print('We use corrected shrinkage')
        shflag = 'sh'
    else:
        from data_Fullset import SCxL23 as exppar
        print('No shrinkage correction')
        shflag = ''

condition = sys.argv[1]
#condition = '1000_80_65'

folderstore = './fconditions/spatial/'

# In[2]:

import PIL.Image as Image
import pickle

# In[3]:

def plot_trace(data,t,ax=None,c='C0',band= None):
    if ax is None:
        ax = gca()
    vtracso = data*1.0
    vtracso.sort(axis=1)
    ax.plot(t,vtracso[:,250],c)
    if band == 0:
        pass
    elif band == 1:
        ax.fill_between(t,vtracso[:,79],vtracso[:,421],color=c,alpha=0.5)
    elif band ==2:
        ax.fill_between(t,vtracso[:,5],vtracso[:,495],color=c,alpha=0.2)
    else:
        ax.fill_between(t,vtracso[:,5],vtracso[:,495],color=c,alpha=0.2)
        ax.fill_between(t,vtracso[:,79],vtracso[:,421],color=c,alpha=0.5)
    return(ax)

# In[4]:

from matplotlib.patches import ConnectionPatch

# In[5]:

def getint(x):
    xc = x*1.0
    xc.sort()
    sh = xc.shape[0]
    xmed = xc[sh//2]
    s0= int(sh*(1-.6827)/2)
    s1 = sh-s0
    x0 = xc[s0]
    x1 = xc[s1]
    s0b= int(sh*(1-.95)/2)
    s1b = sh-s0b
    try:
        x0b = xc[s0b]
        x1b = xc[s1b]
    except:
        x0b = xc[0]
        x1b = xc[-1]
    return((xmed,x0,x1,x0b,x1b))

# In[ ]:

# In[6]:

try:
    with open(folderstore+"gatinginfovslv31"+condition+shflag+".pickle","rb") as f:
        aps = pickle.load(f)
except:
    print('no final file!')
    try:
        with open(folderstore+"gatinginfovslv31_temp"+condition+shflag+".pickle","rb") as f:
            aps = pickle.load(f)
    except:
        print('no temporary file either!')

# with open("/mnt/data/gatinginfovslv2_temp.pickle","rb") as f:
#     aph = pickle.load(f)

# In[7]:

dsv = array([1,2,3,4,5,6,7,8,9])*7.5
dsvc = list(-1.0*dsv); dsvc.reverse()
dsv = concatenate((dsvc,[0],dsv))
print(dsv,dsv.shape)

# In[8]:

labels = ['Vspine','Vsoma','Vdendrite','Ca']
EL0s = [-70,-70,-70,0]
nr = 1
shle = {}
shle0 = {}
for lab in labels:
    shle[lab] = zeros((19,11))
    shle0[lab] = zeros((19,11))

#shle['Vspine'] = zeros((19,11))#shle['Vsoma'] = zeros((19,11))#shle['Vdendrite'] = zeros((19,11))#shle['Ca'] = zeros((19,11))

for il,lab in enumerate(labels):
    EL0s = list(aps[('EL0',0)])
    EL0 = EL0s[il]
    # columns: no inh, shaft inh, axo-spinal inh
    ap0 = aps[(lab,0.0,0)]-EL0
    for i in range(1,nr):
        ap0 = row_stack((ap0,aps[(lab,0.0,i)]-EL0))

    #Effect very close to inh
    r0shaft = ap0[:,1]/ap0[:,0]
    r0axspi = ap0[:,2]/ap0[:,0]
    for j,dss in enumerate(dsv):
        ap = aps[(lab,dss,0)]-EL0
        for i in range(1,nr):
            ap = row_stack((ap,aps[(lab,dss,i)]-EL0))
        rshaft = ap[:,1]/ap[:,0]
        raxspi = ap[:,2]/ap[:,0]
        difr = column_stack(((1-rshaft)/(1-r0shaft),(1-raxspi)/(1-r0axspi)))
        # difr = column_stack((rshaft,raxspi))
        shle0[lab][j,1:] = concatenate((getint(difr[:,0]),getint(difr[:,1])))
        shle0[lab][j,0] = dss
        difr[:,0] = difr[:,0]*(1-r0shaft)
        difr[:,1] = difr[:,1]*(1-r0axspi)
        shle[lab][j,1:] = concatenate((getint(difr[:,0]),getint(difr[:,1])))
        shle[lab][j,0] = dss

# In[11]:

shleh = shle
shleh0 = shle0

# In[18]:

with open(folderstore+"gatinginfo_lengthv31"+condition+shflag+".pickle","wb") as f:
    pickle.dump([shle,shleh,shle0,shleh0],f)

# In[ ]:


#!/usr/local/bin/python3
# -*- coding: utf-8 -*-
'''
loger
将nginx日志转存到MySQL数据库中
version 1.0.0
author cn.baiying@gmail.com
last modify 2016-04-14
'''
import os
import sys
import re
import datetime
import time
import requests
import pymysql

''' 入口方法，程序会自动查找上一时间段的日志文件，也可以通过timeFlay参数指定某一时间段的日志 '''
def log2db(timeFlag = ""):
	logDir = r"/alidata/log/nginx/access/api_log_hour/"
	oneHourBefore = datetime.datetime.now() + datetime.timedelta(hours = -1)
	if timeFlag == "":
		timeFlag = oneHourBefore.strftime("%Y%m%d%H")

	ymd = timeFlag[:8]
	logDir += ymd + "/"
	logFile = logDir + "api.66pei.com.log." + timeFlag
	if os.path.exists(logFile):
		getLog(logFile, timeFlag)
	else:
		runlog("log file " + logFile + " not exists.")

''' 记录程序运行时异常日志，日志记录存入python_run.log文件中 '''
def runlog(log = ""):
	runLogFile = "/alidata/python/python_run.log"
	now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
	log = "[" + now + "] " + log
	try:
		with open(runLogFile, 'a') as rlf:
			print(log, file = rlf)
	except IOError as err:
		print(log, file = rlf)

''' 读取选定的日志文件内容，并将日志数据经过处理后存储到数据库中 '''
def getLog(logFile, timeFlag = ""):
	data = []
	try:
		with open(logFile) as logs:
			for log in logs:
				if log != "":
					logDic = convertLog(log)
					data.append(logDic)
		res = save2db(data, timeFlag)
		if res:
			runlog(logFile + " success.")

	except IOError as err:
		runlog(str(error))

''' 将日志数据转换为数据字典 '''
def convertLog(log = ""):
	ip = r"?P<ip>[\d.]*"
	date = r"?P<date>\d+/\w+/\d+"
	log_time = r"?P<time>\S+"
	method = r"?P<method>\S+"
	request = r"?P<request>\S+"
	status = r"?P<status>\d+"
	bodyBytesSent = r"?P<bodyBytesSent>\d+"
	refer = r"?P<refer>[^\"]*"
	userAgent=r"?P<userAgent>.*"
	p =  re.compile(r"(%s)\ [0-9\-]\ [0-9\-]\ \[(%s)\:(%s)\ [\S]+\]\ \"(%s)?[\s]?(%s)?.*?\"\ (%s)\ (%s)\ \"(%s)\"\ \"(%s).*?\"" % (ip, date, log_time, method, request, status, bodyBytesSent, refer, userAgent), re.VERBOSE)
	m = re.findall(p, log)[0]
	res = {}
	res['ip'] = m[0]
	accTime = convertTime(" ".join(m[1:3]))
	res['time_str'] = accTime['string']
	res['time_int'] = accTime['int']
	if len(m[3]) > 16:
		res['method'] = ""
	else:
		res['method'] = m[3]
	res['request'] = m[4]
	res['status'] = m[5]
	res['body'] = m[6]
	res['refer'] = m[7]
	res['agent'] = m[8]

	params = convertParams(m[4])
	res['url'] = params['url']
	if params['customer_id'] == "":
		res['customer_id'] = 0
	else:
		res['customer_id'] = params['customer_id']

	res['appid'] = params['_appid']
	res['appversion'] = params['_appversion']
	res['os'] = params['_os']
	res['func'] = requests.utils.unquote(params['_func'])
	res['sku'] = requests.utils.unquote(params['sku'])
	res['brand_id'] = requests.utils.unquote(params['brand_id'])
	return res

''' 格式化日志中的时间字段 '''
def convertTime(timeStr = ""):
	arr = timeStr.split()
	dt = datetime.datetime.strptime(" ".join(arr[:2]), '%d/%b/%Y %H:%M:%S')
	res = {}
	res['string'] = dt.strftime("%Y-%m-%d %H:%M:%S")
	res['int'] = int(time.mktime(time.strptime(res['string'], '%Y-%m-%d %H:%M:%S')))
	return res

''' 将日志中的请求参数转换为数据字典，供convertLog方法调用 '''
def convertParams(request = ""):
	columns = ['url', 'customer_id', '_appid', '_appversion', '_os', '_func', 'sku', 'brand_id']
	res = {}
	res['url'] = ""
	res['customer_id'] = 0
	res['_appid'] = ""
	res['_appversion'] = ""
	res['_os'] = ""
	res['_func'] = ""
	res['sku'] = ""
	res['brand_id'] = ""
	if('?' in request):
		arr = request.split('?')
		res['url'] = arr[0]
		for item in arr[1].split('&'):
			tmp = item.split('=')
			if tmp[0] in columns:
				res[tmp[0]] = tmp[1]
	return res

''' 将整理出来的日志数据字典保存到数据库中 '''
def save2db(data = [], timeFlag = ""):
	try:
		conn = pymysql.connect(host='127.0.0.1', user='root', passwd='', db='log', port=3306, charset='utf8')
		cur = conn.cursor()
		# 判断此批数据是否已经入过库了，如果已入过，则删除重入
		sql = "DELETE FROM 66_api WHERE time_flag = '%s'" % timeFlag
		cur.execute(sql)

		# 生成入库sql
		for item in data:
			sql = r'''INSERT INTO 66_api SET '''
			spl = ""
			for (key, val) in item.items():
				if isinstance(val, str) and "'" in val:
					val = val.replace("'", "‘")
				sql += spl + "%s = '%s'" % (key, val)
				spl = ", "
			sql += ", dt = '%s'" % time.strftime('%Y-%m-%d %H-%M-%S', time.localtime(time.time()))
			sql += ", time_flag = '%s'" % timeFlag
			cur.execute(sql)

		conn.commit()
		cur.close
		conn.close
		return True
	except pymysql.Error as err:
		runlog(str(err))
		return False

# 执行程序
if len(sys.argv) == 1:
	log2db()
else:
	log2db(sys.argv[1])


from flask import Flask
import redis
import json

from ...service.entity.book import Book
from ...exception.exception import BookAlreadyExistsException

app = Flask(__name__)

BOOK_COUNTER = "book_counter"
BOOK_ID_PREFIX = "book_"

class BookRepository:

    def __init__(self):
        self.db = redis.Redis(host = "redis", port = 6379, decode_responses = True)
        if self.db.get(BOOK_COUNTER) == None:
            self.db.set(BOOK_COUNTER, 0)

    def save(self, book_req):
        app.logger.debug("Saving new book: {0}.".format(book_req))
        book = self.find_book_by_title(book_req.title)

        if book != None:
            raise BookAlreadyExistsException("Book title \"{0}\" already exist.".format(book_req.title))

        book = Book(self.db.incr(BOOK_COUNTER), book_req.author_id, book_req.title, book_req.year)

        book_id = BOOK_ID_PREFIX + str(book.id)
        book_json = json.dumps(book.__dict__)

        self.db.set(book_id, book_json)

        app.logger.debug("Saved new book: (id: {0}).".format(book.id))
        return book.id

    def find_book_by_title(self, title):
        n = int(self.db.get(BOOK_COUNTER))

        for i in range(1, n + 1):
            book_id = BOOK_ID_PREFIX + str(i)

            if not self.db.exists(book_id):
                continue

            book_json = self.db.get(book_id)
            book = Book.from_json(json.loads(book_json))

            if book.title == title:
                return book

        return None

    def count_all(self):
        app.logger.debug("Starting counting all books")
        n = int(self.db.get(BOOK_COUNTER))

        n_of_books = 0

        for i in range(1, n + 1):
            book_id = BOOK_ID_PREFIX + str(i)

            if self.db.exists(book_id):
                n_of_books += 1

        app.logger.debug("Counted all books (n: {0})".format(n_of_books))
        return n_of_books

    def find_n_books(self, start, limit):
        app.logger.debug("Finding n of books (start: {0}, limit: {1}".format(start, limit))
        n = int(self.db.get(BOOK_COUNTER))

        books = []
        counter = 1

        for i in range(1, n + 1):
            book_id = BOOK_ID_PREFIX + str(i)

            if not self.db.exists(book_id):
                continue

            if counter < start:
                counter += 1
                continue

            book_json = self.db.get(book_id)
            book = Book.from_json(json.loads(book_json))
            books.append(book)

            if len(books) >= limit:
                break

        app.logger.debug("Found {0} books.".format(len(books)))
        return books


import unittest

from mock import MagicMock

from bounded_contexts.central_console.models.contact_person import ContactPerson

class TestContactPerson(unittest.TestCase):

    def test_instance(self):
        expected_email = MagicMock()
        expected_name = MagicMock()

        actual_user = ContactPerson(expected_email, expected_name)

        self.assertEqual(actual_user.email, expected_email)
        self.assertEqual(actual_user.name, expected_name)

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


"""Sequence-to-sequence model for human motion prediction."""
import random
import torch
from torch import nn

class Seq2SeqModel(nn.Module):
    """Sequence-to-sequence model for human motion prediction"""

    def __init__(self,
                 architecture,
                 rnn_size,
                 num_layers=1,
                 num_joints=21,
                 residual_velocities=False,
                 dropout=0.0,
                 teacher_ratio=0.0):
        """Create the model.
        Args:
          architecture: [basic, tied] whether to tie the encoder and decoder.
          rnn_size: number of units in the rnn.
          num_layers: number of rnns to stack.
          residual_velocities: whether to use a residual connection that models velocities.
        """
        super(Seq2SeqModel, self).__init__()

        self.input_size = num_joints*3
        self.num_joints = num_joints
        # Summary writers for train and test runs
        self.rnn_size = rnn_size
        self.dropout = nn.Dropout(dropout)
        self.teacher_forcing = teacher_ratio
        self.residual_velocities = residual_velocities
        # === Create the RNN that will keep the state ===
        self.encoder = torch.nn.GRU(
            self.input_size, self.rnn_size, batch_first=True, num_layers=num_layers)
        self.decoder = torch.nn.GRU(
            self.input_size, self.rnn_size, num_layers=num_layers, dropout=dropout)
        self.projector = nn.Linear(self.rnn_size*num_layers, self.input_size)

    def forward(self, encoder_inputs, decoder_inputs):
        """
        Args:
            encoder_inputs: batch of dance pose sequences , shape=(batch_size,source_seq_length,num_joints*3)
            decoder_inputs: batch of dance pose sequences , shape=(batch_size,target_seq_length-1,num_joints*3)
        Returns:
            outputs: batch of predicted dance pose sequences, shape=(batch_size,target_seq_length-1,num_joints*3)
        """
        # First calculate the encoder hidden state
        _, encoder_hidden_state = self.encoder(encoder_inputs)
        batch_size = decoder_inputs.size(0)
        outputs = []
        first_decode = True
        next_state = encoder_hidden_state
        # Iterate over decoder inputs
        for inp in decoder_inputs.transpose(0, 1):
            # Perform teacher forcing
            if random.random() < self.teacher_forcing and not first_decode:
                inp = prev_output
            _, next_state = self.decoder(inp.unsqueeze(0), next_state)
            # Apply residual network to help in smooth transition between subsequent poses
            if self.residual_velocities:
                output = inp + self.projector(self.dropout(next_state.view(batch_size, -1)))
            else:
                output = self.projector(self.dropout(next_state))
            # Store the output for Teacher Forcing: use the prediction as
            # the next input instead of feeding the ground truth
            prev_output = output
            outputs.append(output)
            first_decode = False

        outputs = torch.stack(outputs)
        return outputs.transpose(0, 1)

    def generate(self, warmup_input, n_frames):
        with torch.no_grad():
            outputs = []
            batch_size = warmup_input.size(0)
            hidden = torch.zeros(batch_size, self.rnn_size)
            for inp in warmup_input.transpose(0, 1):
                hidden = self.decoder(inp, hidden)

            last_warmup_input = warmup_input.transpose(0, 1)[-1]
            if self.residual_velocities:
                output = last_warmup_input + self.projector(hidden)
            else:
                output = self.projector(hidden)

            outputs.append(output)

            for _ in range(n_frames-1):
                hidden = self.decoder(output, hidden)
                if self.residual_velocities:
                    output = output + self.projector(hidden)
                else:
                    output = self.projector(hidden)
                outputs.append(output)

            outputs = torch.stack(outputs)
            return outputs.transpose(0, 1)


import tensorflow as tf
from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D, Input
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import random

# input X vector
X = [[[0.0], [0.0]], [[0.0],[1.0]], [[1.0], [0.0]], [[1.0], [1.0]]]
# output Y vector
Y = [[[0.0]], [[1.0]], [[1.0]], [[0.0]]]

# Data = pd.read_csv('Plant Growth Research/Plant_Growth_Data.csv')
# Data = Data.sample(frac=2, replace=True, random_state=1)
# x_data = np.array(Data[['Day']].values)
# y_data = np.array(Data[['Height']].values)

# Placeholders for input and output
tf.compat.v1.disable_eager_execution()
x = tf.compat.v1.placeholder(tf.float32, shape=[2,1])
y = tf.compat.v1.placeholder(tf.float32, shape=[1,1])

# W matrix
W1 = tf.Variable([[1.0,0.0], [1.0, 0.0]], shape=[2,2], name="W1")
W2 = tf.Variable([[0.0] , [1.0]], shape=[2,1], name ="W1")

# Biases
B1 = tf.Variable([[0.0], [0.0]], shape=[2,1], name = "B1")
B2 = tf.Variable([0.0], shape=1, name = "B2")

# Hidden layer and outout layer
# activation function is Sigmoid (0,1)
output =tf.sigmoid(tf.matmul(tf.sigmoid(tf.matmul(W1, x) + B1), W2, transpose_a =True) + B2)

# error estimation

e = tf.reduce_mean(tf.math.squared_difference(y, output))
train = tf.compat.v1.train.GradientDescentOptimizer(0.5).minimize(e)

init = tf.compat.v1.global_variables_initializer()
sess = tf.compat.v1.Session()
sess.run(init)

for i in range (1001):
    for k in range(0,4):
        j = random.randint(0,3)
        sess.run(train, feed_dict={x: X[j], y: Y[j]})
        sess.run(e, feed_dict={x: X[j], y: Y[j]})
    if i % 1000 == 0:
        print('\nEpoch: ' + str(i))
        for k in range(0,4):
            print('\nError: ' + str(sess.run(e, feed_dict={x: X[k], y: Y[k]})))

        for k in range(0,4):
            print (sess.run(output,feed_dict={x: X[k]}))

# for i in range(25):
#     for j in range(25):
#         x_data = np.array([[i/25],[j/25]])
#         answer = sess.run(output,feed_dict={x: x_data})
#         plt.scatter(i/25,j/25, c= str(answer[0][0]) )

# plt.show()

sess.close()
print ("Complete")


# 1334. Find the City With the Smallest Number of Neighbors at a Threshold Distance
class Solution:
    def findTheCity(self, n: int, edges: List[List[int]], distanceThreshold: int) -> int:
        matrix = [[float('inf') for _ in range(n)] for _ in range(n)]
        for i in range(n):
            matrix[i][i] = 0

        # create matrix
        for edge in edges:
            matrix[edge[0]][edge[1]] = edge[2]
            matrix[edge[1]][edge[0]] = edge[2]

        # Floyd-Warshall
        for k in range(n):
            for i in range(n):
                for j in range(n):
                    if matrix[i][j] > matrix[i][k] + matrix[k][j]:
                        matrix[i][j] = matrix[i][k] + matrix[k][j]

        # list to store number of cities that are reachable within distanceThreshold
        reaciableCitiesNum = [0 for _ in range(n)]

        for cityNum in range(len(matrix)):  # cityNum is the index of matrix
            for dist in matrix[cityNum]:
                if dist <= distanceThreshold:
                    reaciableCitiesNum[cityNum] += 1

        # get the list of index which has the smallest number in reaciableCitiesNum list
        SmallestNumReaceableCites = [i for i, x in enumerate(reaciableCitiesNum) if x == min(reaciableCitiesNum)]

        return max(SmallestNumReaceableCites)


class Solution:
    def maximalSquare(self, matrix: List[List[str]]) -> int:
        dp=[[0 for i in range(len(matrix[0]))] for j in range(len(matrix))]
        ans=0
        for i in range(len(dp)):
            dp[i][0]=int(matrix[i][0])
            if dp[i][0]==1:
                ans=1
        for j in range(len(dp[0])):
            dp[0][j]=int(int(matrix[0][j]))
            if dp[0][j]==1:
                ans=1
        for i in range(1,len(dp)):
            for j in range(1,len(dp[0])):
                if matrix[i][j]=='1':
                    dp[i][j]=min(dp[i-1][j],dp[i][j-1],dp[i-1][j-1])+1
                    ans=max(ans,dp[i][j])
        return ans*ans


import pickle
from models import matching

def matching_check_file(InputMFCC, regis_directory_with_file):
    with open(f"{regis_directory_with_file}Voicerecognition.pkl","rb") as pkl4:
        data=pickle.load(pkl4)
        mini=1800
        match_status = False
        for i in data:
            #print(InputMFCC.shape)
            print(i,matching(InputMFCC,data[i]),"FINALMATCHES")
            value=matching(InputMFCC,data[i])
            if value<=mini:
                match_status = True
                break
    return match_status


import adal

from msrest.authentication import Authentication

from azure.cli._util import CLIError

class AdalAuthentication(Authentication):#pylint: disable=too-few-public-methods

    def __init__(self, token_retriever):
        self._token_retriever = token_retriever

    def signed_session(self):
        session = super(AdalAuthentication, self).signed_session()

        try:
            scheme, token = self._token_retriever()
        except adal.AdalError as err:
            raise CLIError(err)

        header = "{} {}".format(scheme, token)
        session.headers['Authorization'] = header
        return session


import json

from django.conf import settings
from django.core.management.base import BaseCommand
from django.utils.html import strip_tags

from tomato.models import ClientLogEntry

class Command(BaseCommand):
    help = "Update constants file with latest data from server"
    CONSTANTS_FILE_PATH = settings.PROJECT_DIR / "constants.json"

    def add_arguments(self, parser):
        parser.add_argument("-b", "--bump-protocol-version", action="store_true", help="Bump protocol version.")

    def handle(self, *args, **options):
        with open(self.CONSTANTS_FILE_PATH, "r") as file:
            constants = json.load(file)

        if options["bump_protocol_version"]:
            constants["protocol_version"] += 1

        constants.update(
            {
                "client_log_entry_types": sorted(ClientLogEntry.Type.values),
                "settings_descriptions": {
                    key: strip_tags(value[1].replace("<br>", " "))
                    for key, value in settings.CONSTANCE_CONFIG.items()
                    if key not in settings.CONSTANCE_SERVER_ONLY_SETTINGS
                },
            }
        )

        with open(self.CONSTANTS_FILE_PATH, "w") as file:
            json.dump(constants, file, indent=2, sort_keys=True)
            file.write("\n")
        self.stdout.write(self.style.SUCCESS("Successfully wrote constants.json file!"))


import tensorflow as tf
import cifar10
from datetime import datetime
import time

FLAGS = tf.app.flags.FLAGS
tf.app.flags.DEFINE_string('train_dir', 'cifar10_train_try',
                           '''Directory where to write event logs and checkpoint''')
tf.app.flags.DEFINE_integer('max_steps', 110000, '''Number of batches to run''')
tf.app.flags.DEFINE_boolean('log_device_placement', False, '''Whether to log device placement''')
tf.app.flags.DEFINE_integer('log_frequency', 10, '''How ofter to log results to the console''')

def stats_graph(graph):
    run_meta = tf.RunMetadata()
    flops = tf.profiler.profile(graph,run_meta=run_meta, cmd='op', options=tf.profiler.ProfileOptionBuilder.float_operation())
    params = tf.profiler.profile(graph,run_meta=run_meta, cmd='op', options=tf.profiler.ProfileOptionBuilder.trainable_variables_parameter())
    print('FLOPs: {};    Trainable params: {}'.format(flops.total_float_ops, params.total_parameters))

g=tf.Graph()
def train():
    with g.as_default() :

        global_step = tf.train.get_or_create_global_step()

        with tf.device('/cpu:0'):
            images, labels = cifar10.distorted_inputs()

        logits = cifar10.inference3model(images)
        stats_graph(g)
        loss = cifar10.loss(logits, labels)
        train_op = cifar10.train(loss, global_step)

        class _LoggerHook(tf.train.SessionRunHook):

            def begin(self):

                self._step = -1
                self._start_time = time.time()

            def before_run(self, run_context):

                self._step += 1
                # 在这里返回你想在运行过程中产看的信息，以list的形式传递,如:[loss, accuracy]
                return tf.train.SessionRunArgs(loss)

            def after_run(self, run_context, run_values):

                if self._step % FLAGS.log_frequency == 0:
                    current_time = time.time()
                    duration = current_time - self._start_time
                    self._start_time = current_time

                    # results返回的是上面before_run()的返回结果，上面是loss所以返回loss
                    # 如若上面返回的是个list,则返回的也是个list
                    loss_value = run_values.results
                    examples_per_sec = FLAGS.log_frequency * FLAGS.batch_size / duration
                    sec_per_batch = float(duration / FLAGS.log_frequency)

                    print('%s: step %d, loss = %.2f (%.1f examples/sec; %.3f sec/batch)'
                          % (datetime.now(), self._step, loss_value, examples_per_sec, sec_per_batch))

        with tf.train.MonitoredTrainingSession(checkpoint_dir=FLAGS.train_dir,
                                               hooks=[tf.train.StopAtStepHook(last_step=FLAGS.max_steps),
                                                      tf.train.NanTensorHook(loss),
                                                      _LoggerHook()],
                                               save_checkpoint_secs=60,
                                               config=tf.ConfigProto(
                                                   log_device_placement=FLAGS.log_device_placement
                                               )) as mon_sess:
            while not mon_sess.should_stop():
                mon_sess.run(train_op)

def main(argv=None):
    cifar10.maybe_download_and_extract()
    if tf.gfile.Exists(FLAGS.train_dir):
        tf.gfile.DeleteRecursively(FLAGS.train_dir)
    tf.gfile.MakeDirs(FLAGS.train_dir)
    train()

if __name__ == '__main__':
    tf.app.run()


# master_checkout.py is designed to check out and pull from master (or main, see later) all of your repositories.
# It works on the assumption that it lives alongside your git repos, something like this:
# workspace/
#   ├─ backend/
#   ├─ frontend/
#   ├─ tda/
#   ├─ tda_amadeus/
#   ├─ master_checkout.py

# However it can also be called from anywhere if the first parameter you pass it is the directory
# where you projects live.
# Simply run the file and it will checkout all of your projects. If there
# are any problems, it will report back the projects that failed so you can check if you
# had any leftover code or commits leftover.
# If you have any projects that have a primary branch as `main` instead of `master` , add them as strings
# to the following constant:
PROJECTS_WITH_MAIN_BRANCH = ['tools', 'tda']

# USAGE (in top level directory)
# python master_checkout.py
# OR (from anywhere)
# python master_checkout.py ~/workspace/
import os
import subprocess
from pathlib import Path
import sys

class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKCYAN = '\033[96m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'

def checkout_primary(project_name):
    primary_branch = 'master' if project_name not in PROJECTS_WITH_MAIN_BRANCH else 'main'
    checkout_master_process =  subprocess.Popen(['git', 'checkout', primary_branch],
                           stdout=subprocess.PIPE,
                           universal_newlines=True)
    checkout_master_process.communicate()
    return checkout_master_process.returncode

def git_pull():
    pull_process = subprocess.Popen(['git', 'pull'],
                           stdout=subprocess.PIPE,
                           universal_newlines=True)
    pull_process.communicate()
    return pull_process.returncode

if (len(sys.argv) > 1):
    first_arg = sys.argv[1]
    path_to_repos = Path(first_arg)
    if os.path.isdir(path_to_repos):
        working_dir = path_to_repos
    else:
        print(f"{bcolors.FAIL} It looks like the supplied path '{path_to_repos}' is not a proper directory{bcolors.ENDC}")
        sys.exit()
else:
    working_dir = os.getcwd()

os.chdir(working_dir)

all_current_directories = os.listdir(working_dir)
directories_with_failures=[]

for directory in all_current_directories:
    if os.path.isdir(directory) and not directory.startswith('.'):
        print(f"{bcolors.HEADER}{bcolors.BOLD}------------------------- {directory.upper()} ------------------------- {bcolors.ENDC}")

        os.chdir(directory)
        master_checkout_return_code = checkout_primary(directory)
        pull_from_origin_return_code = git_pull()

        if master_checkout_return_code != 0 or pull_from_origin_return_code != 0:
            directories_with_failures.append(directory)

        os.chdir('..')

print("\n")

if directories_with_failures:
    print(f"{bcolors.FAIL} There were failures checking out to the following directories: {str(directories_with_failures)}{bcolors.ENDC}")
else:
    print(f"{bcolors.OKGREEN}Successfully checked out and pulled from master or main in all directories{bcolors.ENDC}")


def checkForDigitCancelling(numer,denom):
    strNumer = str(numer)
    strDenom = str(denom)
    taille = len(strNumer)
    for i in range(0,taille):
        if ((int(strDenom[taille-1-i]))>0) and ((strNumer[1] == strDenom[0]) or (strNumer[0] == strDenom[1])) and ((int(strNumer[i]))/(int(strDenom[taille-1-i])) == numer/denom):
            return True
    return False

lst = []
checkForDigitCancelling(22, 99)
for secondnum in range(10,100):
    for firstnum in range(10,secondnum):
        if checkForDigitCancelling(firstnum, secondnum) and ((firstnum*secondnum) % 11 != 0):
            print(str(firstnum) + " and " + str(secondnum))

"""answer and explanation :
Solutions :
16 and 64
26 and 65
19 and 95
49 and 98

Which equate to :
1/4
2/5
1/5
1/2

If we multiply them together, we get 1/100. The denominator is 100, which is the answer."""


import os
os.system('cls')

premio=780000.00
premio_porcentagem=780000.00/100

primeiro=premio_porcentagem*46
premio=premio-primeiro

segundo=premio_porcentagem*32
premio=premio-segundo

terceiro=premio

print("Premio do primeiro ganhador: "+str(primeiro))
print("Premio do segundo ganhador: "+str(segundo))
print("Premio do terceiro ganhador: "+str(terceiro))
print(primeiro+segundo+terceiro)


import logging
import os
import shutil
import subprocess
import urllib.request

FILENAME_FOR_UNITFILE = "ccam.service"
PATH_FOR_UNITFILE = "/etc/systemd/system"
unitfile_fullpath = "%s/%s" % (PATH_FOR_UNITFILE, FILENAME_FOR_UNITFILE)

WEB_FILE_PATH = os.environ.get("S3_BUCKET_ARTIFACTS") + "/ccam"
LOCAL_FILE_PATH = "/usr/local/bin/ccam"

COMMANDS_TO_STOP_AND_DISABLE_SERVICE = [
    ["systemctl", "disable", FILENAME_FOR_UNITFILE],
    ["systemctl", "stop", FILENAME_FOR_UNITFILE],
]

COMMANDS_TO_PURGE_SYSTEMD_CONFIG = [
    ["systemctl", "daemon-reload"],
]

# DISABLE unit file
try:
    for command in COMMANDS_TO_STOP_AND_DISABLE_SERVICE:
        logging.info("running command to STOP and DISABLE services %s", repr(command))
        subprocess.check_output(command)
except Exception as e:
    logging.exception(
        "error while stopping and disabling service with error %s", repr(e)
    )

# remove systemd unitfile
try:
    os.unlink(unitfile_fullpath)
except Exception as e:
    logging.exception("error removing systemd unit file with error %s", repr(e))

# PURGE COMMANDS TO SYSTEMD CONFIG
try:
    for command in COMMANDS_TO_PURGE_SYSTEMD_CONFIG:
        logging.info("running command to PURGE UNITFILE FROM SYSTEMD %s", repr(command))
        subprocess.check_output(command)
except Exception as e:
    logging.exception(
        "error while PURGING UNIT FILE FROM SYSTEMD with error %s", repr(e)
    )

# data and config should be under other directories
# set by environment vars, therefore they will not be
# deleted with the following operation to remove the
# repo
try:
    os.unlink(LOCAL_FILE_PATH)
    logging.info("removed local repo at path: %s", LOCAL_FILE_PATH)
except Exception as e:
    logging.exception(
        "could not remove local BINARY at path: %s, with error: %s",
        LOCAL_FILE_PATH,
        repr(e),
    )


#write a function to test if a number entered by the user is armstrong or not. eg: 371,   3^3+7^3+1^3=371
#This is only just for 3digit.
'''def armstrong():
    a=int(input("Enter first digit"))
    b=int(input("Enter second digit"))
    c=int(input("Enter third digit"))
    d=a**3
    e=b**3
    f=c**3
    g=a*100+b*10+c
    if d+e+f==g:
        print("IT is armstrong")
    else:
        print("It is not armstrong number")
armstrong()'''

def armstrong():
    num=int(input("Enter number to check armstrong number"))
    a=num
    sum=0
    while a>0:               # a=371
        b=a%10              #   b=1
        c=b**3              #c=1**3=3
        sum=sum+c           #sum=0+3=3
        d=a//10                #d=371//10=37
        a=d                     #a=37
    if sum==num:
            print("It is armstrong number")
    else:
            print("It is not armstrong number")
armstrong()


import os

import xlwt
from bs4 import BeautifulSoup
import time
from multiprocessing import Pool

from pip._vendor import requests

j=1#定义全局变量,从第1行写入数据
def transform(string):#统一单位将单位万转为普通单位
    if string[-1]=='万':
        string1=string.replace('万','')
        return str(float(string1)*10000)
    else:
        return string

def singleweb(url):#爬取单页内容
    global j
    r=requests.get(url,headers=head)
    r.encoding=r.apparent_encoding
    r.raise_for_status()
    soup=BeautifulSoup(r.text,'html.parser')
    soups1=soup.find_all('div',class_='info')
    for soup1 in soups1:
        things=[]
        soup2=soup1.find('span',class_='type avid')
        things.append(soup2.text)
        soup3=soup1.find('span',class_='type hide')
        things.append(soup3.text)
        things.append(soup1.a.get('title'))
        things.append(transform(soup1.find('span',title='观看').text.replace('\n','').replace('\r','').replace(' ',''))) #去掉多余字符
        things.append(transform(soup1.find('span',title='弹幕').text.replace('\n','').replace('\r','').replace(' ','')))#去掉多余字符
        things.append(soup1.find('span',title='上传时间').text.replace('\n','').replace('\r','').replace(' ',''))#去掉多余字符
        things.append(soup1.find('span',title='up主').text.replace('\n','').replace('\r','').replace(' ',''))#去掉多余字符
        for k in range(0,7):
            sheet.write(j,k,things[k])
        print('\r正在爬取第'+str(j)+'条信息',end='')
        j=j+1
if __name__ == "__main__":
    try:
        os.makedirs('D://xlwt')#创早储存地址
    except:
        pass
    keyword=input('输入搜索关键词:')
    urls=[]
    print('爬取的信息将在操作完成后保存在D://xlwt里面')
    header=['av号','类型','标题','观看','弹幕','上传时间','up主']
    for i in range(1,51):#爬取50页，也是上限
        urls.append('https://search.bilibili.com/all?keyword='+keyword+f'&page={i}')
    head={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36'}
    book=xlwt.Workbook(encoding='utf-8')#建立excel工作区
    sheet=book.add_sheet('sheet1')
    for i in range(0,7):#写入表头
        sheet.write(0,i,header[i])
    for url in urls:
        singleweb(url)
    book.save('D://xlwt//'+keyword+'.xls')#保存内容
    print('\n操作完成')

爬取B站粉丝/爬b站粉丝.py


import pygame
import random
import math

pygame.init()

screen = pygame.display.set_mode((1200, 800))
pygame.display.set_caption("Space Invader")
icon = pygame.image.load("./img/ufo.png")
pygame.display.set_icon(icon)
BG = pygame.image.load("./img/bg.png")

playerImg = pygame.image.load("./img/player.png")
playerX = 600
playerY = 700
player_xMove = 0

enemyImg = []
enemyX = []
enemyY = []
enemy_XMove = []
enemy_YMove = []
num_of_enemies = 6

for i in range(num_of_enemies):
    enemyImg.append(pygame.image.load("./img/enemy.png"))
    enemyX.append(random.randint(0, 1200))
    enemyY.append(random.randint(-60, 150))
    enemy_XMove.append(6)
    enemy_YMove.append(30)

bulletImg = pygame.image.load("./img/bullet.png")
bulletX = 0
bulletY = 700
bullet_xMove = 0
bullet_YMove = 18
bulletState = "ready"

scoreValue = 0
font = pygame.font.Font("freesansbold.ttf", 32)
textX = 10
textY = 10

def show_score(x, y):
    score = font.render("Score: " + str(scoreValue), True, (255, 255, 255))
    screen.blit(score, (x, y))

def enemy(x, y, i):
    screen.blit(enemyImg[i], (x, y))

def player(x, y):
    screen.blit(playerImg, (x, y))

def fire(x, y):
    global bulletState
    bulletState = "fire"
    screen.blit(bulletImg, (x + 25, y))

def is_collision(enemyx, enemyy, bulletx, bullety):
    distance = math.sqrt((math.pow(enemyx - bulletx, 2)) +
                         (math.pow(enemyy - bullety, 2)))
    if distance < 27:
        return True
    else:
        return False

running = True
while running:
    screen.fill((0, 0, 0))
    screen.blit(BG, (0, 0))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                player_xMove = -8
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                player_xMove = 8
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                if bulletState == "ready":
                    bulletX = playerX
                    fire(bulletX, bulletY)

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or pygame.K_RIGHT:
                player_xMove = 0

    playerX += player_xMove

    if playerX < 0:
        playerX = 0
    elif playerX > 1136:
        playerX = 1136

    for i in range(num_of_enemies):
        enemyX[i] += enemy_XMove[i]
        if enemyX[i] < 0:
            enemy_XMove[i] = 6
            enemyY[i] += enemy_YMove[i]
        elif enemyX[i] > 1136:
            enemy_XMove[i] = -6
            enemyY[i] += enemy_YMove[i]
        collision = is_collision(enemyX[i], enemyY[i], bulletX, bulletY)
        if collision:
            bulletY = 700
            bulletState = "ready"
            scoreValue += 1
            enemyX[i] = random.randint(0, 1200)
            enemyY[i] = random.randint(-60, 150)

        enemy(enemyX[i], enemyY[i], i)

    if bulletState == "fire":
        fire(bulletX, bulletY)
        bulletY -= bullet_YMove

    if bulletY < 0:
        bulletY = 700
        bulletState = "ready"

    player(playerX, playerY)
    show_score(textX, textY)
    pygame.display.update()


import datetime

from sanic import response, Blueprint

from .middleware import Middleware
from .snowflakes import channel_snowflake

class Channels:
    @staticmethod
    def setup(app):
        Channels.app = app

        bp = Blueprint("channelsapi", url_prefix="/api/channels")
        bp.middleware(Middleware.check_auth, "request")

        bp.add_route(Channels.app.ctx.limiter.limit("1 per second")(Channels.create), "/create")
        app.blueprint(bp)

    @staticmethod
    async def create(request):
        data = request.json

        if data["scope"] in ["dm", "private"]:
            if "members" not in data:
                return response.json({"error": "members not provided"})

        date = datetime.datetime.now()

        channel = {
            "name": data["name"],
            "scope": data["scope"],
            "public": data["public"],
            "members": [] if data.get("members") is None else data["members"],
            "creator": request.ctx.auth,
            "attrs": {},
            "created-at": date,
            "message-tag": channel_snowflake(data["name"], request.ctx.auth, date),
            "parent": None,
            "roles": {}
        }

        await Channels.app.ctx.db["channels"].insert_one({**channel})

        return response.json(channel)


from collections import deque

from .base import BaseYarnAPI, get_logger
from .constants import FinalApplicationStatus, YarnApplicationState
from .errors import IllegalArgumentError
from .hadoop_conf import check_is_active_rm, get_resource_manager_endpoint

log = get_logger(__name__)
LEGAL_STATES = {s for s, _ in YarnApplicationState}
LEGAL_FINAL_STATUSES = {s for s, _ in FinalApplicationStatus}

def validate_yarn_application_state(state, required=False):
    if state:
        if state not in LEGAL_STATES:
            msg = "Yarn Application State {} is illegal".format(state)
            raise IllegalArgumentError(msg)
    else:
        if required:
            msg = "state argument is required to be provided"
            raise IllegalArgumentError(msg)

def validate_yarn_application_states(states, required=False):
    if states:
        if not isinstance(states, list):
            msg = "States should be list"
            raise IllegalArgumentError(msg)

        illegal_states = set(states) - LEGAL_STATES
        if illegal_states:
            msg = "Yarn Application States {} are illegal".format(",".join(illegal_states))
            raise IllegalArgumentError(msg)
    else:
        if required:
            msg = "states argument is required to be provided"
            raise IllegalArgumentError(msg)

def validate_final_application_status(final_status, required=False):
    if final_status:
        if final_status not in LEGAL_FINAL_STATUSES:
            msg = "Final Application Status {} is illegal".format(final_status)
            raise IllegalArgumentError(msg)
    else:
        if required:
            msg = "final_status argument is required to be provided"
            raise IllegalArgumentError(msg)

class ResourceManager(BaseYarnAPI):
    """
    The ResourceManager REST API's allow the user to get information about the
    cluster - status on the cluster, metrics on the cluster,
    scheduler information, information about nodes in the cluster,
    and information about applications on the cluster.

    If `service_endpoint` argument is `None` client will try to extract it from
    Hadoop configuration files.  If both `address` and `alt_address` are
    provided, the address corresponding to the ACTIVE HA Resource Manager will
    be used.

    :param List[str] service_endpoints: List of ResourceManager HTTP(S)
        addresses
    :param int timeout: API connection timeout in seconds
    :param AuthBase auth: Auth to use for requests configurations
    :param boolean verify: Either a boolean, in which case it controls whether
        we verify the server's TLS certificate, or a string, in which case it must
        be a path to a CA bundle to use. Defaults to ``True``
    """

    def __init__(
        self,
        service_endpoints=None,
        timeout=30,
        auth=None,
        verify=True,
        geog_code="inland",
    ):
        active_service_endpoint = None
        if not service_endpoints:
            active_service_endpoint = get_resource_manager_endpoint(timeout, auth, verify, geog_code)
        else:
            for endpoint in service_endpoints:
                if check_is_active_rm(endpoint, timeout, auth, verify):
                    active_service_endpoint = endpoint
                    break

        if active_service_endpoint:
            super(ResourceManager, self).__init__(active_service_endpoint, timeout, auth, verify)
        else:
            raise Exception("No active RMs found")

    def get_active_endpoint(self):
        """
        The active address, port tuple to which this instance is associated.
        :return: str service_endpoint: Service endpoint URL corresponding to
        the active address of RM
        """
        return self.service_uri.to_url()

    def cluster_information(self):
        """
        The cluster information resource provides overall information about
        the cluster.

        :returns: API response object with JSON data
        :rtype: :py:class:`yarn_api_client.base.Response`
        """
        path = "/ws/v1/cluster/info"
        return self.request(path)

    def cluster_metrics(self):
        """
        The cluster metrics resource provides some overall metrics about the
        cluster. More detailed metrics should be retrieved from the jmx
        interface.

        :returns: API response object with JSON data
        :rtype: :py:class:`yarn_api_client.base.Response`
        """
        path = "/ws/v1/cluster/metrics"
        return self.request(path)

    def cluster_scheduler(self):
        """
        A scheduler resource contains information about the current scheduler
        configured in a cluster. It currently supports both the Fifo and
        Capacity Scheduler. You will get different information depending on
        which scheduler is configured so be sure to look at the type
        information.

        :returns: API response object with JSON data
        :rtype: :py:class:`yarn_api_client.base.Response`
        """
        path = "/ws/v1/cluster/scheduler"
        return self.request(path)

    def cluster_applications(
        self,
        state=None,
        states=None,
        final_status=None,
        user=None,
        queue=None,
        limit=None,
        started_time_begin=None,
        started_time_end=None,
        finished_time_begin=None,
        finished_time_end=None,
        application_types=None,
        application_tags=None,
        de_selects=None,
    ):
        """
        With the Applications API, you can obtain a collection of resources,
        each of which represents an application.

        :param str state: state of the application [deprecated]
        :param List[str] states: applications matching the given application
            states
        :param str final_status: the final status of the application -
            reported by the application itself
        :param str user: user name
        :param str queue: queue name
        :param str limit: total number of app objects to be returned
        :param str started_time_begin: applications with start time beginning
            with this time, specified in ms since epoch
        :param str started_time_end: applications with start time ending with
            this time, specified in ms since epoch
        :param str finished_time_begin: applications with finish time
            beginning with this time, specified in ms since epoch
        :param str finished_time_end: applications with finish time ending
            with this time, specified in ms since epoch
        :param List[str] application_types: applications matching the given
            application types, specified as a comma-separated list
        :param List[str] application_tags: applications matching any of the
            given application tags, specified as a comma-separated list
        :param List[str] de_selects: a generic fields which will be skipped in
            the result
        :returns: API response object with JSON data
        :rtype: :py:class:`yarn_api_client.base.Response`
        :raises yarn_api_client.errors.IllegalArgumentError: if `state` or
            `final_status` incorrect
        """
        path = "/ws/v1/cluster/apps"

        validate_yarn_application_state(state)
        validate_yarn_application_states(states)
        validate_final_application_status(final_status)

        loc_args = (
            ("state", state),
            ("states", ",".join(states) if states else None),
            ("finalStatus", final_status),
            ("user", user),
            ("queue", queue),
            ("limit", limit),
            ("startedTimeBegin", started_time_begin),
            ("startedTimeEnd", started_time_end),
            ("finishedTimeBegin", finished_time_begin),
            ("finishedTimeEnd", finished_time_end),
            (
                "applicationTypes",
                ",".join(application_types) if application_types else None,
            ),
            (
                "applicationTags",
                ",".join(application_tags) if application_tags else None,
            ),
            ("deSelects", ",".join(de_selects) if de_selects else None),
        )

        params = self.construct_parameters(loc_args)

        return self.request(path, params=params)

    def cluster_application_statistics(self, states=None, application_types=None):
        """
        With the Application Statistics API, you can obtain a collection of
        triples, each of which contains the application type, the application
        state and the number of applications of this type and this state in
        ResourceManager context.

        This method work in Hadoop > 2.0.0

        :param List[str] states: states of the applications. If states is not
            provided, the API will enumerate all application states and
            return the counts of them.
        :param List[str] application_types: types of the applications,
            specified as a comma-separated list. If application_types is not
            provided, the API will count the applications of any application
            type. In this case, the response shows * to indicate any
            application type. Note that we only support at most one
            applicationType temporarily. Otherwise, users will expect
            an BadRequestException.
        :returns: API response object with JSON data
        :rtype: :py:class:`yarn_api_client.base.Response`
        """
        path = "/ws/v1/cluster/appstatistics"

        validate_yarn_application_states(states)

        loc_args = (
            ("states", ",".join(states) if states else None),
            (
                "applicationTypes",
                ",".join(application_types) if application_types else None,
            ),
        )
        params = self.construct_parameters(loc_args)

        return self.request(path, params=params)

    def cluster_application(self, application_id):
        """
        An application resource contains information about a particular
        application that was submitted to a cluster.

        :param str application_id: The application id
        :returns: API response object with JSON data
        :rtype: :py:class:`yarn_api_client.base.Response`
        """
        path = "/ws/v1/cluster/apps/{appid}".format(appid=application_id)

        return self.request(path)

    def cluster_application_attempts(self, application_id):
        """
        With the application attempts API, you can obtain a collection of
        resources that represent an application attempt.

        :param str application_id: The application id
        :returns: API response object with JSON data
        :rtype: :py:class:`yarn_api_client.base.Response`
        """
        path = "/ws/v1/cluster/apps/{appid}/appattempts".format(appid=application_id)

        return self.request(path)

    def cluster_application_attempt_info(self, application_id, attempt_id):
        """
        With the application attempts API, you can obtain an extended info about
        an application attempt.

        :param str application_id: The application id
        :param str attempt_id: The attempt id
        :returns: API response object with JSON data
        :rtype: :py:class:`yarn_api_client.base.Response`
        """
        path = "/ws/v1/cluster/apps/{appid}/appattempts/{attemptid}".format(appid=application_id, attemptid=attempt_id)

        return self.request(path)

    def cluster_app_attempt_containers(self, application_id, attempt_id):
        """
        With the application attempts API, you can obtain an information
        about container related to an application attempt.

        :param str application_id: The application id
        :param str attempt_id: The attempt id
        :returns: API response object with JSON data
        :rtype: :py:class:`yarn_api_client.base.Response`
        """
        path = "/ws/v1/cluster/apps/{appid}/appattempts/{attemptid}/containers".format(
            appid=application_id, attemptid=attempt_id
        )

        return self.request(path)

    def cluster_app_attempt_info(self, application_id, attempt_id, container_id):
        """
        With the application attempts API, you can obtain an information
        about container related to an application attempt.

        :param str application_id: The application id
        :param str attempt_id: The attempt id
        :param str container_id: The container id
        :returns: API response object with JSON data
        :rtype: :py:class:`yarn_api_client.base.Response`
        """
        path = "/ws/v1/cluster/apps/{appid}/appattempts/{attemptid}/containers/{containerid}".format(
            appid=application_id, attemptid=attempt_id, containerid=container_id
        )

        return self.request(path)

    def cluster_application_state(self, application_id):
        """
        (This feature is currently in the alpha stage and may change in the
        future)

        With the application state API, you can obtain the current
        state of an application.

        :param str application_id: The application id
        :returns: API response object with JSON data
        :rtype: :py:class:`yarn_api_client.base.Response`
        """
        path = "/ws/v1/cluster/apps/{appid}/state".format(appid=application_id)

        return self.request(path)

    def cluster_application_kill(self, application_id):
        """
        (This feature is currently in the alpha stage and may change in the
        future)

        With the application kill API, you can kill an application
        that is not in FINISHED or FAILED state.

        :param str application_id: The application id
        :returns: API response object with JSON data
        :rtype: :py:class:`yarn_api_client.base.Response`
        """

        data = {"state": "KILLED"}
        path = "/ws/v1/cluster/apps/{appid}/state".format(appid=application_id)

        return self.request(path, "PUT", json=data)

    def cluster_nodes(self, states=None):
        """
        With the Nodes API, you can obtain a collection of resources, each of
        which represents a node.

        :param List[str] states: the states of the node, specified as a
            comma-separated list valid values are: NEW, RUNNING, UNHEALTHY,
            DECOMMISSIONING, DECOMMISSIONED, LOST, REBOOTED, SHUTDOWN
        :returns: API response object with JSON data
        :rtype: :py:class:`yarn_api_client.base.Response`
        :raises yarn_api_client.errors.IllegalArgumentError: if `healthy`
            incorrect
        """
        path = "/ws/v1/cluster/nodes"

        validate_yarn_application_states(states)

        loc_args = (("states", ",".join(states) if states else None),)
        params = self.construct_parameters(loc_args)

        return self.request(path, params=params)

    def cluster_node(self, node_id):
        """
        A node resource contains information about a node in the cluster.

        :param str node_id: The node id
        :returns: API response object with JSON data
        :rtype: :py:class:`yarn_api_client.base.Response`
        """
        path = "/ws/v1/cluster/nodes/{nodeid}".format(nodeid=node_id)

        return self.request(path)

    def cluster_submit_application(self, data):
        """
        (This feature is currently in the alpha stage and may change in the
        future)

        With the New Application API, you can obtain an application-id which
        can then be used as part of the Cluster Submit Applications API to
        submit applications. The response also includes the maximum resource
        capabilities available on the cluster.

        For data body definition refer to:
        (https://hadoop.apache.org/docs/current/hadoop-yarn/hadoop-yarn-site/ResourceManagerRest.html
            #Cluster_Writeable_APIs)

        :param dict data: Application details
        :returns: API response object with JSON data
        :rtype: :py:class:`yarn_api_client.base.Response`
        """
        path = "/ws/v1/cluster/apps"

        return self.request(path, "POST", json=data)

    def cluster_new_application(self):
        """
        (This feature is currently in the alpha stage and may change in the
        future)

        With the New Application API, you can obtain an application-id which
        can then be used as part of the Cluster Submit Applications API to
        submit applications. The response also includes the maximum resource
        capabilities available on the cluster.

        :returns: API response object with JSON data
        :rtype: :py:class:`yarn_api_client.base.Response`
        """
        path = "/ws/v1/cluster/apps/new-application"

        return self.request(path, "POST")

    def cluster_get_application_queue(self, application_id):
        """
        (This feature is currently in the alpha stage and may change in the
        future)

        With the application queue API, you can query the queue of a
        submitted app

        :param str application_id: The application id
        :returns: API response object with JSON data
        :rtype: :py:class:`yarn_api_client.base.Response`
        """
        path = "/ws/v1/cluster/apps/{appid}/queue".format(appid=application_id)

        return self.request(path)

    def cluster_change_application_queue(self, application_id, queue):
        """
        (This feature is currently in the alpha stage and may change in the
        future)

        Move a running app to another queue using a PUT request specifying the
        target queue.

        To perform the PUT operation, authentication has to be
        setup for the RM web services. In addition, you must be authorized to
        move the app. Currently you can only move the app if you’re using the
        Capacity scheduler or the Fair scheduler.

        Please note that in order to move an app, you must have an
        authentication filter setup for the HTTP interface. The functionality
        requires that a username is set in the HttpServletRequest. If no filter
        is setup, the response will be an “UNAUTHORIZED” response.

        :param str application_id: The application id
        :param str queue: queue name
        :returns: API response object with JSON data
        :rtype: :py:class:`yarn_api_client.base.Response`
        """
        path = "/ws/v1/cluster/apps/{appid}/queue".format(appid=application_id)

        return self.request(path, "PUT", json={"queue": queue})

    def cluster_get_application_priority(self, application_id):
        """
        (This feature is currently in the alpha stage and may change in the
        future)

        With the application priority API, you can query the priority of a
        submitted app

        :param str application_id: The application id
        :returns: API response object with JSON data
        :rtype: :py:class:`yarn_api_client.base.Response`
        """
        path = "/ws/v1/cluster/apps/{appid}/priority".format(appid=application_id)

        return self.request(path)

    def cluster_change_application_priority(self, application_id, priority):
        """
        (This feature is currently in the alpha stage and may change in the
        future)

        Update priority of a running or accepted app using a PUT request
        specifying the target priority.

        To perform the PUT operation, authentication has to be
        setup for the RM web services. In addition, you must be authorized to
        move the app. Currently you can only move the app if you’re using the
        Capacity scheduler or the Fair scheduler.

        Please note that in order to move an app, you must have an
        authentication filter setup for the HTTP interface. The functionality
        requires that a username is set in the HttpServletRequest. If no filter
        is setup, the response will be an “UNAUTHORIZED” response.

        :param str application_id: The application id
        :param int priority: application priority
        :returns: API response object with JSON data
        :rtype: :py:class:`yarn_api_client.base.Response`
        """
        path = "/ws/v1/cluster/apps/{appid}/priority".format(appid=application_id)

        return self.request(path, "PUT", json={"priority": priority})

    # def cluster_node_container_memory(self):
    #     """
    #     This endpoint allows clients to gather info on the maximum memory that
    #     can be allocated per container in the cluster.
    #     :returns: integer specifying the maximum memory that can be allocated in
    #     a container in the cluster
    #     """
    #
    #     return _get_maximum_container_memory(CONF_DIR)

    def cluster_scheduler_queue(self, yarn_queue_name):
        """
        Given a queue name, this function tries to locate the given queue in
        the object returned by scheduler endpoint.

        The queue can be present inside a multilevel structure. This solution
        tries to locate the queue using breadth-first-search algorithm.

        :param str yarn_queue_name: case sensitive queue name
        :return: queue, None if not found
        :rtype: dict
        """
        scheduler = self.cluster_scheduler().data
        scheduler_info = scheduler["scheduler"]["schedulerInfo"]

        bfs_deque = deque([scheduler_info])
        while bfs_deque:
            vertex = bfs_deque.popleft()
            if vertex["queueName"] == yarn_queue_name:
                return vertex
            elif "queues" in vertex:
                for queue in vertex["queues"]["queue"]:
                    bfs_deque.append(queue)

        return None

    def cluster_scheduler_queue_available(self, candidate_partition, availability_threshold):
        """
        Checks whether the requested memory satisfies the available space of the queue
        This solution takes into consideration the node label concept in cluster.
        Following node labelling, the resources can be available in various partition.
        Given the partition data it tells you if the used capacity of this partition is spilling
        the threshold specified.

        :param str candidate_parition: node label partition (case sensitive)
        :param float availability_threshold: value can range between 0 - 100 .
        :return: Boolean
        """

        if candidate_partition["absoluteUsedCapacity"] > availability_threshold:
            return False
        return True

    def cluster_queue_partition(self, candidate_queue, cluster_node_label):
        """
        A queue can be divided into multiple partitions having different node labels.
        Given the candidate queue and parition node label, this extracts the partition
        we are interested in.

        :param dict candidate_queue: queue dictionary
        :param str cluster_node_label: case sensitive node label name
        :return: partition, None if not Found.
        :rtype: dict
        """
        for partition in candidate_queue["capacities"]["queueCapacitiesByPartition"]:
            if partition["partitionName"] == cluster_node_label:
                return partition
        return None

    def cluster_reservations(
        self,
        queue=None,
        reservation_id=None,
        start_time=None,
        end_time=None,
        include_resource_allocations=None,
    ):
        """
        The Cluster Reservation API can be used to list reservations. When listing reservations
        the user must specify the constraints in terms of a queue, reservation-id, start time or
        end time. The user must also specify whether or not to include the full resource allocations
        of the reservations being listed. The resulting page returns a response containing
        information related to the reservation such as the acceptance time, the user, the resource
        allocations, the reservation-id, as well as the reservation definition.

        :param str queue: the queue name containing the reservations to be listed. if not set, this
            value will default to “default”
        :param str reservation_id: the reservation-id of the reservation which will be listed. If
            this parameter is present, start-time and end-time will be ignored.
        :param str start_time:  reservations that end after this start-time will be listed. If
            unspecified or invalid, this will default to 0.
        :param str end_time: reservations that start after this end-time will be listed. If
            unspecified or invalid, this will default to Long.MaxValue.
        :param str include_resource_allocations: true or false. If true, the resource allocations
            of the reservation will be included in the response. If false, no resource allocations
            will be included in the response. This will default to false.
        :returns: API response object with JSON data
        :rtype: :py:class:`yarn_api_client.base.Response`
        """
        path = "/ws/v1/cluster/reservation/list"

        loc_args = (
            ("queue", queue),
            ("reservation-id", reservation_id),
            ("start-time", start_time),
            ("end-time", end_time),
            ("include-resource-allocations", include_resource_allocations),
        )

        params = self.construct_parameters(loc_args)

        return self.request(path, params=params)

    def cluster_new_delegation_token(self, renewer):
        """
        (This feature is currently in the alpha stage and may change in the
        future)

        API to create delegation token.

        All delegation token requests must be carried out on a Kerberos
        authenticated connection(using SPNEGO). Carrying out operations on a non-kerberos
        connection will result in a FORBIDDEN response. In case of renewing a token, only
        the renewer specified when creating the token can renew the token. Other users(including
        the owner) are forbidden from renewing tokens.

        :param str renewer: The user who is allowed to renew the delegation token
        :returns: API response object with JSON data
        :rtype: :py:class:`yarn_api_client.base.Response`
        """
        path = "/ws/v1/cluster/delegation-token"

        return self.request(path, "POST", json={"renewer": renewer})

    def cluster_renew_delegation_token(self, delegation_token):
        """
        (This feature is currently in the alpha stage and may change in the
        future)

        API to renew delegation token.

        All delegation token requests must be carried out on a Kerberos
        authenticated connection(using SPNEGO). Carrying out operations on a non-kerberos
        connection will result in a FORBIDDEN response. In case of renewing a token, only
        the renewer specified when creating the token can renew the token. Other users(including
        the owner) are forbidden from renewing tokens.

        :param str delegation_token: Delegation token
        :returns: API response object with JSON data
        :rtype: :py:class:`yarn_api_client.base.Response`
        """
        path = "/ws/v1/cluster/delegation-token/expiration"

        return self.request(path, "POST", headers={"Hadoop-YARN-RM-Delegation-Token": delegation_token})

    def cluster_cancel_delegation_token(self, delegation_token):
        """
        (This feature is currently in the alpha stage and may change in the
        future)

        API to cancel delegation token.

        All delegation token requests must be carried out on a Kerberos
        authenticated connection(using SPNEGO). Carrying out operations on a non-kerberos
        connection will result in a FORBIDDEN response.

        :param str delegation_token: Delegation token
        :returns: API response object with JSON data
        :rtype: :py:class:`yarn_api_client.base.Response`
        """
        path = "/ws/v1/cluster/delegation-token"

        return self.request(
            path,
            "DELETE",
            headers={"Hadoop-YARN-RM-Delegation-Token": delegation_token},
        )

    def cluster_new_reservation(self):
        """
        (This feature is currently in the alpha stage and may change in the
        future)

        Use the New Reservation API, to obtain a reservation-id which can then be used as part of
        the Cluster Reservation API Submit to submit reservations.

        :returns: API response object with JSON data
        :rtype: :py:class:`yarn_api_client.base.Response`
        """
        path = "/ws/v1/cluster/reservation/new-reservation"

        return self.request(path, "POST")

    def cluster_submit_reservation(self, data):
        """
        The Cluster Reservation API can be used to submit reservations. When submitting a
        reservation the user specifies the constraints in terms of resources, and time that is
        required. The resulting response is successful if the reservation can be made. If a
        reservation-id is used to submit a reservation multiple times, the request will succeed
        if the reservation definition is the same, but only one reservation will be created. If
        the reservation definition is different, the server will respond with an error response.
        When the reservation is made, the user can use the reservation-id used to submit the
        reservation to get access to the resources by specifying it as part of Cluster Submit
        Applications API.

        For data body definition refer to:
        (https://hadoop.apache.org/docs/current/hadoop-yarn/hadoop-yarn-site/ResourceManagerRest.html
            #Cluster_Reservation_API_Submit)

        :param dict data: Reservation details
        :returns: API response object with JSON data
        :rtype: :py:class:`yarn_api_client.base.Response`
        """
        path = "/ws/v1/cluster/reservation/submit"

        return self.request(path, "POST", json=data)

    def cluster_update_reservation(self, data):
        """
        The Cluster Reservation API Update can be used to update existing reservations.Update of a
        Reservation works similarly to submit described above, but the user submits the
        reservation-id of an existing reservation to be updated. The semantics is a try-and-swap,
        successful operation will modify the existing reservation based on the requested update
        parameter, while a failed execution will leave the existing reservation unchanged.

        For data body definition refer to:
        (https://hadoop.apache.org/docs/current/hadoop-yarn/hadoop-yarn-site/ResourceManagerRest.html
            #Cluster_Reservation_API_Update)

        :param dict data: Reservation details
        :returns: API response object with JSON data
        :rtype: :py:class:`yarn_api_client.base.Response`
        """
        path = "/ws/v1/cluster/reservation/update"

        return self.request(path, "POST", json=data)

    def cluster_delete_reservation(self, reservation_id):
        """
        The Cluster Reservation API Update can be used to update existing reservations.Update of a
        Reservation works similarly to submit described above, but the user submits the
        reservation-id of an existing reservation to be updated. The semantics is a try-and-swap,
        successful operation will modify the existing reservation based on the requested update
        parameter, while a failed execution will leave the existing reservation unchanged.

        :param str reservation_id: The id of the reservation to be deleted (the system automatically
            looks up the right queue from this)
        :returns: API response object with JSON data
        :rtype: :py:class:`yarn_api_client.base.Response`
        """
        path = "/ws/v1/cluster/reservation/delete"

        return self.request(path, "POST", json={"reservation-id": reservation_id})

    def cluster_application_timeouts(self, application_id):
        """
        Cluster Application Timeouts API can be used to get all configured timeouts of an
        application. When you run a GET operation on this resource, a collection of timeout objects
        is returned. Each timeout object is composed of a timeout type, expiry-time and remaining
        time in seconds.

        :param str application_id: The application id
        :returns: API response object with JSON data
        :rtype: :py:class:`yarn_api_client.base.Response`
        """
        path = "/ws/v1/cluster/apps/{appid}/timeouts".format(appid=application_id)

        return self.request(path)

    def cluster_application_timeout(self, application_id, timeout_type):
        """
        The Cluster Application Timeout resource contains information about timeout.

        :param str application_id: The application id
        :param str timeout_type: Timeout type. Valid values are the members of the
            ApplicationTimeoutType enum. LIFETIME is currently the only valid value. .
        :returns: API response object with JSON data
        :rtype: :py:class:`yarn_api_client.base.Response`
        """
        path = "/ws/v1/cluster/apps/{appid}/timeouts/{timeout_type}".format(
            appid=application_id, timeout_type=timeout_type
        )

        return self.request(path)

    def cluster_update_application_timeout(self, application_id, timeout_type, expiry_time):
        """
        Update timeout of an application for given timeout type.

        :param str application_id: The application id
        :param str timeout_type: Timeout type. Valid values are the members of the
            ApplicationTimeoutType enum. LIFETIME is currently the only valid value.
        :param str expiry_time: Time at which the application will expire in
            ISO8601 yyyy-MM-dd’T’HH:mm:ss.SSSZ format.
        :returns: API response object with JSON data
        :rtype: :py:class:`yarn_api_client.base.Response`
        """
        path = "/ws/v1/cluster/apps/{appid}/timeout".format(appid=application_id)

        return self.request(
            path,
            "PUT",
            json={"timeout": {"type": timeout_type, "expiryTime": expiry_time}},
        )

    def cluster_scheduler_conf_mutation(self):
        """
        (This feature is currently in the alpha stage and may change in the
        future)

        API to retrieve the scheduler’s configuration that is currently loaded into
        scheduler’s context.

        :returns: API response object with JSON data
        :rtype: :py:class:`yarn_api_client.base.Response`
        """
        path = "/ws/v1/cluster/scheduler-conf"

        return self.request(path)

    def cluster_modify_scheduler_mutation(self, data):
        """
        (This feature is currently in the alpha stage and may change in the
        future)

        API to modify the scheduler configuration

        For data body definition refer to:
        (https://hadoop.apache.org/docs/current/hadoop-yarn/hadoop-yarn-site/ResourceManagerRest.html
            #Scheduler_Configuration_Mutation_API)

        :param dict data: sched-conf dictionary object
        :returns: API response object with JSON data
        :rtype: :py:class:`yarn_api_client.base.Response`
        """
        path = "/ws/v1/cluster/scheduler-conf"

        return self.request(path, "PUT", json=data)


from datetime import datetime
from typing import Optional, Dict

from sqlalchemy import (
	DateTime,
	Date,
	Boolean,
	Integer,
	Unicode,
	LargeBinary,
	Column,
	ForeignKey,
	Table,
	MetaData,
	Float,
	insert,
	select,
	func,
)
from sqlalchemy.orm import registry, relationship

from domain import interface, dataclasses

naming_convention = {
	'ix': 'ix_%(column_0_label)s',
	'uq': 'uq_%(table_name)s_%(column_0_name)s',
	'ck': 'ck_%(table_name)s_%(constraint_name)s',
	'fk': 'fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s',
	'pk': 'pk_%(table_name)s'
}

# даем имя схемы только для БД MSSQL, связано с инфраструктурными особенностями
metadata = MetaData(naming_convention=naming_convention)

users = Table(
	'users',
	metadata,
	Column('id', Integer, primary_key=True),
	Column('email', Unicode(255), nullable=True),
	Column('fam', Unicode(255), nullable=False),
	Column('name', Unicode(255), nullable=False),
	Column('otc', Unicode(255), nullable=True),
	Column('phone', Unicode(255), nullable=False),
)

coords = Table(
	'coords',
	metadata,
	Column('id', Integer, primary_key=True),
	Column('latitude', Float, nullable=False),
	Column('longitude', Float, nullable=False),
	Column('height', Integer, nullable=False),
)

levels = Table(
	'levels',
	metadata,
	Column('id', Integer, primary_key=True),
	Column('winter', Unicode(255), nullable=False),
	Column('summer', Unicode(255), nullable=False),
	Column('autumn', Unicode(255), nullable=False),
	Column('spring', Unicode(255), nullable=False),
)

images = Table(
	'images',
	metadata,
	Column('id', Integer, primary_key=True),
	Column('image', Unicode(255), nullable=False),
	Column('title', Unicode(255), nullable=False),
	Column('data_id', Integer, ForeignKey('data.id'), nullable=False),
)

pereval_added = Table(
	'pereval_added',
	metadata,
	Column('id', Integer, primary_key=True),
Column('date_added', Date, server_default=func.now()),
	Column('beauty_title', Unicode(255), nullable=False),
	Column('title', Unicode(255), nullable=False),
	Column('other_titles', Unicode(255), nullable=False),
	Column('connect', Unicode(255), nullable=False),
	Column('add_time', DateTime, default=datetime.utcnow()),
	Column('user_id', Integer, ForeignKey('users.id'), nullable=False),
	Column('coords_id', Integer, ForeignKey('coords.id'), nullable=False),
	Column('level_id', Integer, ForeignKey('levels.id'), nullable=False),
Column('status', Boolean, default=False),
)

pereval_areas = Table(
	'pereval_areas',
	metadata,
	Column('id', Integer, primary_key=True),
	Column('id_parent', Integer, nullable=False),
	Column('title', Integer, nullable=False),
)

spr_activities_types = Table(
	'spr_activities_types',
	metadata,
	Column('id', Integer, primary_key=True),
	Column('title', Integer, nullable=False),
)

mapper = registry()

mapper.map_imperatively(dataclasses.User, users)
mapper.map_imperatively(dataclasses.Coords, coords)
mapper.map_imperatively(dataclasses.Level, levels)
mapper.map_imperatively(
	dataclasses.PerevalAdded,
	pereval_added,

	properties={
		'user': relationship(
			dataclasses.User,
			backref='data',
			lazy='joined',
		),
		'coords': relationship(
			dataclasses.Coords,
			backref='data',
			lazy='joined',
		),
		'levels': relationship(
			dataclasses.Level,
			backref='data',
			lazy='joined',
		)
	}
)

mapper.map_imperatively(
	dataclasses.Image,
	images,
	properties={
		'pereval': relationship(
			dataclasses.PerevalAdded,
			backref='images',
			lazy='joined',
		)
	}
)

class Repository(interface.Repository):
	def __init__(self, engine):
		self.engine = engine

	def add_data(self, data_for_add: Dict) -> int:
		query = insert(dataclasses.PerevalAdded, data_for_add)
		result = self.engine.execute(query)
		return result.inserted_primary_key[0]

	def add_user(self, user: Dict) -> int:
		query = insert(dataclasses.User, user)
		result = self.engine.execute(query)
		return result.inserted_primary_key[0]

	def add_coords(self, coords: Dict) -> int:
		query = insert(dataclasses.Coords, coords)
		result = self.engine.execute(query)
		return result.inserted_primary_key[0]

	def add_level(self, level: Dict) -> int:
		query = insert(dataclasses.Level, level)
		result = self.engine.execute(query)
		return result.inserted_primary_key[0]

	def add_image(self, image: Dict) -> int:
		query = insert(dataclasses.Image, image)
		result = self.engine.execute(query)
		return result.inserted_primary_key[0]

	def get_user_by_phone(self, user_phone: str) -> Optional[dataclasses.User]:
		query = select(dataclasses.User).where(dataclasses.User.phone == user_phone)
		row = self.engine.execute(query).one_or_none()
		return dataclasses.User(**row) if row is not None else None