from typing import Dict, Optional
import math
from cshogi import Board, CSA, KIF, BLACK, WHITE, opponent, REPETITION_WIN, REPETITION_LOSE
from cshogi.usi import Engine
from cshogi.cli import usi_info_to_score, usi_info_to_csa_comment, re_usi_info
from flask import Flask, render_template, request
from wsgiref.simple_server import make_server

TURN_SYMBOLS = ('▲', '△')
CSA_TURN_SYMBOLS = {'+': '▲', '-': '△'}

class Human:
    def __init__(self, human_input):
        self.board = Board()
        self.name = 'Human'
        self.human_input = human_input

    def usi(self, listener=None):
        return []

    def isready(self, listener=None):
        pass

    def usinewgame(self, listener=None):
        pass

    def position(self, moves=None, sfen="startpos", listener=None):
        self.board.reset()
        for move in moves:
            self.board.push_usi(move)

    def go(self, ponder=False, btime=None, wtime=None, byoyomi=None, binc=None, winc=None, nodes=None, listener=None):
        import time
        while True:
            human_input = dict(self.human_input)
            if human_input['number'] == self.board.move_number:
                usi_move = human_input['move']
                move = self.board.move_from_usi(usi_move)
                if self.board.is_legal(move):
                    return usi_move, None
            time.sleep(0.1)

    def quit(self, listener=None):
        pass

def usi_info_to_pv(board, info):
    m = re_usi_info.match(info)
    if m is None:
        return None

    # pv
    pv = []
    board2 = board.copy()
    for usi_move in m[3].split(' '):
        move = board2.move_from_usi(usi_move)
        if not board2.is_legal(move):
            break
        pv.append(TURN_SYMBOLS[board2.turn] + KIF.move_to_kif(move, board2.peek()))
        board2.push(move)

    return ' '.join(pv)

def match(moves, engine1=None, engine2=None, options1={}, options2={}, names=None, byoyomi=None, time=None, inc=None, draw=256, human_input=None, csa=None):
    from collections import defaultdict
    from time import perf_counter

    class Listener:
        def __init__(self):
            self.info = self.bestmove = ''

        def __call__(self, line):
            self.info = self.bestmove
            self.bestmove = line
    listener = Listener()

    # CSA
    if csa:
        csa_exporter = CSA.Exporter(csa, append=False)

    engines = []
    for engine in (engine1, engine2):
        if engine == 'human':
            engines.append(Human(human_input))
        else:
            engines.append(Engine(engine, connect=True))
    for engine, options in zip(engines, (options1, options2)):
        for name, value in options.items():
            engine.setoption(name, value, listener=listener)
        engine.isready(listener=listener)

    for i in range(2):
        if names[i]:
            engines[i].name = names[i]
        else:
            names[i] = engines[i].name

    board = Board()
    usi_moves = []
    repetition_hash = defaultdict(int)

    # 新規ゲーム
    for engine in engines:
        engine.usinewgame()

    if csa:
        csa_exporter.info(board, names, version='V2')

    # 対局
    is_game_over = False
    is_nyugyoku = False
    is_illegal = False
    is_repetition_win = False
    is_repetition_lose = False
    is_fourfold_repetition = False
    is_timeup = False
    remain_time = [time, time]
    while not is_game_over:
        engine_index = (board.move_number - 1) % 2
        engine = engines[engine_index]

        # 持将棋
        if board.move_number > draw:
            is_game_over = True
            break

        # position
        engine.position(usi_moves)

        start_time = perf_counter()

        # go
        listener.info = ''
        listener.bestmove = ''
        bestmove, _ = engine.go(byoyomi=byoyomi, btime=remain_time[BLACK], wtime=remain_time[WHITE], binc=inc, winc=inc, listener=listener)

        elapsed_time = perf_counter() - start_time

        if remain_time[board.turn] is not None:
            if inc_time[board.turn] is not None:
                remain_time[board.turn] += inc_time[board.turn]
            remain_time[board.turn] -= math.ceil(elapsed_time * 1000)

        if bestmove == 'resign':
            # 投了
            is_game_over = True
            break
        elif bestmove == 'win':
            # 入玉勝ち宣言
            is_nyugyoku = True
            is_game_over = True
            break
        else:
            move = board.move_from_usi(bestmove)
            if board.is_legal(move):
                if csa:
                    csa_exporter.move(move, time=int(elapsed_time), comment=usi_info_to_csa_comment(board, listener.info))
                score = usi_info_to_score(listener.info)
                pv = usi_info_to_pv(board, listener.info)
                moves.append({
                    'number': board.move_number,
                    'kif_move': TURN_SYMBOLS[(board.move_number + 1) % 2] + KIF.move_to_kif(move, board.peek()),
                    'time': math.ceil(elapsed_time),
                    'move': move,
                    'eval': (score * (1 if board.turn == BLACK else -1)) if score is not None else 'null',
                    'pv': pv if pv is not None else '',
                })
                board.push(move)
                usi_moves.append(bestmove)
                key = board.zobrist_hash()
                repetition_hash[key] += 1
                # 千日手
                if repetition_hash[key] == 4:
                    # 連続王手
                    is_draw = board.is_draw()
                    if is_draw == REPETITION_WIN:
                        is_repetition_win = True
                        is_game_over = True
                        break
                    elif is_draw == REPETITION_LOSE:
                        is_repetition_lose = True
                        is_game_over = True
                        break
                    is_fourfold_repetition = True
                    is_game_over = True
                    break
            else:
                is_illegal = True
                is_game_over = True
                break

        # 終局判定
        if board.is_game_over():
            is_game_over = True
            break

    # エンジン終了
    for engine in engines:
        engine.quit()

    # 結果出力
    if not board.is_game_over() and board.move_number > draw:
        result = '持将棋'
        csa_endgame = '%JISHOGI'
    elif is_fourfold_repetition:
        result = '千日手'
        csa_endgame = '%SENNICHITE'
    elif is_nyugyoku:
        result = TURN_SYMBOLS[board.turn] + '入玉宣言'
        csa_endgame = '%KACHI'
    elif is_illegal:
        win = opponent(board.turn)
        result = '{}の反則負け'.format('先手' if win == WHITE else '後手')
        csa_endgame = '%ILLEGAL_MOVE'
    elif is_repetition_win:
        win = board.turn
        result = '{}の反則勝ち'.format('先手' if win == BLACK else '後手')
        csa_endgame = '%+ILLEGAL_ACTION' if board.turn == WHITE else '%-ILLEGAL_ACTION'
    elif is_repetition_lose:
        win = opponent(board.turn)
        result = '{}の反則負け'.format('先手' if win == WHITE else '後手')
        csa_endgame = 'ILLEGAL_MOVE'
    elif is_timeup:
        win = opponent(board.turn)
        result = '{}の切れ負け'.format('先手' if win == WHITE else '後手')
        csa_endgame = '%TIME_UP'
    else:
        result = TURN_SYMBOLS[board.turn] + '投了'
        csa_endgame = '%TORYO'

    # CSA
    if csa:
        csa_exporter.endgame(csa_endgame)

    moves.append({
        'number': board.move_number,
        'kif_move': result,
        'time': 0,
        'move': 0,
        'eval': 'null',
        'pv': '',
    })

def run(engine1: Optional[str] = None, engine2: Optional[str] = None, options1: Dict = {}, options2: Dict = {}, name1: Optional[str] = None, name2: Optional[str] = None, byoyomi: Optional[int] = None, time: Optional[int] = None, inc: Optional[int] = None, draw: int = 256, csa: Optional[str] = None, host: str = 'localhost', port: int = 8000):
    """Initializes and runs a shogi match between two engines or replays a game from a given CSA file.
    The match or replay is rendered using Flask and is accessible via a web interface.

    :param engine1: Name or path of the first engine, or 'human' for human player. Default is None.
    :param engine2: Name or path of the second engine, or 'human' for human player. Default is None.
    :param options1: Configuration options for the first engine. Default is an empty dictionary.
    :param options2: Configuration options for the second engine. Default is an empty dictionary.
    :param name1: Optional name for the first player. Default is None.
    :param name2: Optional name for the second player. Default is None.
    :param byoyomi: Byoyomi time in milliseconds. Default is None.
    :param time: Time control for the match in milliseconds. Default is None.
    :param inc: Increment time for each move in milliseconds. Default is None.
    :param draw: Number of moves before a draw is claimed. Default is 256.
    :param csa: Path to a CSA file to replay a game. Default is None.
    :param host: Hostname to bind the Flask server to. Default is 'localhost'.
    :param port: Port number to bind the Flask server to. Default is 8000.
    """
    is_match = 'false'
    auto_update = 'false'

    if engine1 and engine2:
        from multiprocessing import Process, Manager
        manager = Manager()
        moves = manager.list()
        human_input = manager.dict({ 'number': 0, 'move': None })
        names = manager.list([name1, name2])
        humans = [engine1 == 'human', engine2 == 'human']
        match_proc = Process(target=match, args=[moves, engine1, engine2, options1, options2, names, byoyomi, time, inc, draw, human_input, csa])
        match_proc.start()
        is_match = 'true'
        auto_update = 'true'
    elif csa:
        moves = []
        kif = CSA.Parser.parse_file(csa)[0]
        names = kif.names
        board = Board(sfen=kif.sfen)
        for i, (move, prev_move, time, comment) in enumerate(zip(kif.moves, [None] + kif.moves[:-1], kif.times, kif.comments)):
            comment_items = comment.split(' ')
            eval = 'null'
            pv = []
            assert board.is_legal(move)
            board.push(move)
            if len(comment_items) > 0 and comment_items[0] != '':
                eval = int(comment_items[0])
            if len(comment_items) > 1:
                board2 = board.copy()
                for csa in comment_items[1:]:
                    move2 = board2.move_from_csa(csa[1:])
                    if board2.is_legal(move2):
                        pv.append(CSA_TURN_SYMBOLS[csa[0]] + KIF.move_to_kif(move2))
                        board2.push(move2)
                    else:
                        pv = comment_items[1:]
                        break
            moves.append({
                'number': i + 1,
                'kif_move': TURN_SYMBOLS[i % 2] + KIF.move_to_kif(move, prev_move),
                'time': time,
                'move': move,
                'eval': eval,
                'pv': ' '.join(pv),
                })
        moves.append({
            'number': i + 2,
            'kif_move': TURN_SYMBOLS[(i + 1) % 2] + CSA.JAPANESE_END_GAMES[kif.endgame],
            'time': 0,
            'move': 0,
            'eval': 'null',
            'pv': '',
        })

    app = Flask(__name__)

    @app.route("/")
    def init_board():
        autoupdate = 'false'
        human = ''
        if is_match == 'true':
            if match_proc.is_alive():
                autoupdate = request.args.get('autoupdate', default=auto_update)
                if humans[len(moves) % 2]:
                    human = 'black' if len(moves) % 2 == 0 else 'white'
        return render_template('board.html', names=names, moves=moves, is_match=is_match, autoupdate=autoupdate, human=human)

    @app.route("/update")
    def update():
        human = ''
        if humans[len(moves) % 2]:
            human = 'black' if len(moves) % 2 == 0 else 'white'
        return { 'names': list(names), 'moves': list(moves), 'human':human }

    @app.route("/move")
    def human_move():
        import time
        human_input['move'] = request.args.get('move')
        human_input['number'] = int(request.args.get('number', default=0))
        time.sleep(0.2)
        human = ''
        if names[len(moves) % 2] == 'Human':
            human = 'black' if len(moves) % 2 == 0 else 'white'
        return { 'names': list(names), 'moves': list(moves), 'human':human }

    server = make_server(host, port, app)
    server.serve_forever()

def colab(engine1=None, engine2=None, options1={}, options2={}, name1=None, name2=None, byoyomi=None, time=None, inc=None, draw=256, csa=None):
    from multiprocessing import Process
    import portpicker
    from google.colab import output

    global proc
    if 'proc' in globals():
        proc.terminate()
        proc.join()

    port = portpicker.pick_unused_port()
    proc = Process(target=run, args=(engine1, engine2, options1, options2, name1, name2, byoyomi, time, inc, draw, csa, 'localhost', port))
    proc.start()
    output.serve_kernel_port_as_iframe(port, height='680')

if __name__ == '__main__':
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument('engine1')
    parser.add_argument('engine2')
    parser.add_argument('--options1', default='')
    parser.add_argument('--options2', default='')
    parser.add_argument('--name1')
    parser.add_argument('--name2')
    parser.add_argument('--byoyomi', type=int)
    parser.add_argument('--time', type=int)
    parser.add_argument('--inc', type=int)
    parser.add_argument('--draw', type=int, default=256)
    parser.add_argument('--csa')
    parser.add_argument('--host', type=str, default='localhost')
    parser.add_argument('--port', type=int, default=8000)
    args = parser.parse_args()

    options_list = [{}, {}]
    for i, kvs in enumerate([options.split(',') for options in (args.options1, args.options2)]):
        if len(kvs) == 1 and kvs[0] == '':
            continue
        for kv_str in kvs:
            kv = kv_str.split(':', 1)
            if len(kv) != 2:
                raise ValueError('options{}'.format(i + 1))
            options_list[i][kv[0]] = kv[1]

    run(engine1=args.engine1, engine2=args.engine2,
        options1=options_list[0], options2=options_list[1],
        name1=args.name1, name2=args.name2,
        byoyomi=args.byoyomi, time=args.time, inc=args.inc, draw=args.draw,
        csa=args.csa, host=args.host, port=args.port)


#! /usr/bin/python
# vim: set ts=4 sts=4 sw=4 noet fileencoding=utf-8:
#
# Функции для работы с обложками.

import Image
import os

import logger

def find(files, outname=u'__folder.jpg'):
	logger.info(u"Looking for album art")
	extensions = [u'jpg', u'jpeg', u'gif', u'png', u'bmp']
	for file in files:
		if str(file.split('.')[-1].lower()) in extensions:
			logger.info(u'Using ' + file)
			return resize(file, 300)
	logger.info(u"  nothing")

def resize(filename, width):
	img = Image.open(unicode(filename))
	if img.mode != 'RGB':
		img = img.convert('RGB')
	if img.size[0] != img.size[1]:
		shift = (max(img.size) - min(img.size)) / 2
		if img.size[0] > img.size[1]:
			crop = (shift, 0, shift + img.size[1], img.size[1])
		else:
			crop = (0, shift, img.size[0], shift + img.size[1])
		img = img.crop(crop)
		img.load()

	outname = os.path.splitext(filename)[0] + u'.' + str(width) + u'x' + str(width) + u'.jpg'
	img.resize((width, width), Image.ANTIALIAS).save(outname, 'JPEG')
	return outname


from django.core.management.base import BaseCommand
from django.core.files.images import ImageFile
import os
from photos import models, utils
from django.db.utils import IntegrityError
from django.db import transaction

class Command(BaseCommand):
    help = 'REQUIRES upload folder with names of people inside. It uploads \
            the photos in each folder with the name as the owner.'

    def create_photo(self, ff, path, per, dev):
        person = models.Person.objects.filter(name__icontains=per)[0]
        device = models.Device.objects.filter(name__icontains=dev)[0]
        # get image hash
        img_hash = utils.hash_image(path)
        if img_hash == '':
            print('Error not a readable image for hasing')
            print('\t{}'.format(path))
            return False
        try:
            with transaction.atomic():
                photo = models.Photo.objects.create(photo_hash=img_hash, owner=person,
                                                    device=device)
                photo.document = ImageFile(open(path, 'rb'))
                photo.document.name = ff
                doc = photo.document
                create_date = utils.get_DateTimeOriginal(path)
                if create_date != '':
                    photo.create_date = create_date
                    photo.metadata = True
                # create name with owner in it
                name = '{}/{}'.format(str(person), doc.name)
                photo.backup_path = utils.save_backup(name, path)
                photo.small_thumb.save(name=doc.name, content=doc)
                photo.medium_thumb.save(name=doc.name, content=doc)
                photo.large_thumb.save(name=doc.name, content=doc)
                photo.save()
        except IntegrityError:
            p = models.Photo.objects.get(photo_hash=img_hash)
            print('**FAILED: duplicates id {}'.format(p.id))
        return True

    def handle(self, *args, **options):
        for r,d,f in os.walk('upload'):
            people = d
            break

        for p in people:
            person_path = os.path.join('upload', p)

            for r,d,f in os.walk(person_path):
                devices = [os.path.join(r, dd) for dd in d]
                break

            for device in devices:
                dev = device.split('/')[-1]
                for r,d,f in os.walk(device):
                    # Uploaded path
                    mov_loc = os.path.join(device, 'uploaded')
                    # error folder path
                    error_path = os.path.join(device, 'error')
                    # remove mac .DS_Store from list
                    try:
                        f.remove('.DS_Store')
                    except ValueError:
                        pass
                    # Only print if there are pictures here
                    if len(f) > 0:
                        print('\n***Uploading photos from {},{}***'.format(p,dev))
                        # Make uploaded folder
                        if not os.path.exists(mov_loc):
                            os.mkdir(mov_loc)

                    for ff in f:
                        pic = ff.lower()
                        # print("It's a picture!")
                        print('Uploading: {}'.format(ff))
                        path = os.path.join(r, ff)
                        created = self.create_photo(ff, path, p, dev)
                        # move photo to uploaded folder
                        if created:
                            os.rename(path, os.path.join(mov_loc, ff))
                        else:
                            if not os.path.exists(error_path):
                                os.mkdir(error_path)
                            os.rename(path, os.path.join(error_path, ff))
                    break


import random
from flask import Flask, render_template_string, render_template, request,session
import os
import re
import config

app = Flask(__name__)
app.config['SECRET_KEY'] = '4hf3j8sgh(rt&%^jf*dw'
#app.debug = True

@app.route('/', methods=['GET', 'POST'])
def index():
    session['user']='nobody'
    session['id']='flag in /admin'
    if request.method == 'POST':
        try:
            p = request.values.get('formula')
            if p != None:
                if re.match("\d{1,10}.?\d{0,5}[\+\-\*\/]\d{1,10}.?\d{0,5}",p):
                    result=eval(p)
                    return render_template_string(str(result))
                if len(p) > 10:
                    return 'what\'s this?'
                return render_template_string(p)

        except Exception as e:
            print(e)
            return 'too hard for meQ_Q'

    return render_template('index.html')

@app.route('/admin', methods=['GET', 'POST'])
def check():
    if 'user' in session:
        if session.get('user') == 'admin' and session.get('id') == '0':
            return 'Welcome,admin!This is you flag:'+config.FLAG
    return 'who R you?'

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=1337)


import os.path

import libgutenberg.GutenbergGlobals as gg
from libgutenberg.Logger import info, debug, error

from ebookmaker.parsers import webify_url
from ebookmaker import writers

class Writer(writers.BaseWriter):
    """ Writes Pics directory. """

    def copy_aux_files(self, job, dest_dir):
        """ Copy image files to dest_dir. Use image data cached in parsers. """

        for p in job.spider.parsers:
            if hasattr(p, 'resize_image') or hasattr(p, 'auxparser'):
                src_uri = p.attribs.url
                if src_uri.startswith(webify_url(dest_dir)):
                    debug('Not copying %s to %s: already there' % (src_uri, dest_dir))
                    continue

                fn_dest = gg.make_url_relative(webify_url(job.base_url), src_uri)
                fn_dest = os.path.join(dest_dir, fn_dest)

                # debug('base_url =  %s, src_uri = %s' % (job.base_url, src_uri))

                if gg.is_same_path(src_uri, fn_dest):
                    debug('Not copying %s to %s: same file' % (src_uri, fn_dest))
                    continue
                debug('Copying %s to %s' % (src_uri, fn_dest))

                fn_dest = gg.normalize_path(fn_dest)
                gg.mkdir_for_filename(fn_dest)
                try:
                    with open(fn_dest, 'wb') as fp_dest:
                        fp_dest.write(p.serialize())
                except IOError as what:
                    error('Cannot copy %s to %s: %s' % (src_uri, fn_dest, what))

    def build(self, job):
        """ Build Pics file. """

        dest_dir = os.path.abspath(job.outputdir)

        info("Creating Pics directory in: %s" % dest_dir)

        self.copy_aux_files(job, dest_dir)

        info("Done Pics directory in: %s" % dest_dir)


import sys
import json
import dateutil.parser
from s3recovery.s3recoverybase import S3RecoveryBase
from s3recovery.config import Config

METADATA_INDEX_DELIMITER = '/'

class S3RecoverCorruption(S3RecoveryBase):
    def __init__(self):
        super(S3RecoverCorruption, self).__init__()
        super(S3RecoverCorruption, self).create_logger_directory()
        super(S3RecoverCorruption, self).create_logger("S3RecoverCorruption")

    def cleanup_bucket_list_entries(self, index_id):
        """
        Performs cleanup of stale data entries

        """
        list_index_response = self.list_index(index_id)
        data_as_dict = self.parse_index_list_response(list_index_response)
        key_list = list(data_as_dict.keys())
        for key in key_list:
            if key not in self.common_keys:
                status, response = self.kv_api.delete(index_id, key)
                super(S3RecoverCorruption, self).check_response(status, "delete", response, index_id, key)

    def cleanup_bucket_metadata_entries(self, index_id):
        """
        Performs cleanup of stale data entries
        """

        list_index_response = self.list_index(index_id)
        data_as_dict = self.parse_index_list_response(list_index_response)
        key_list = list(data_as_dict.keys())
        for key in key_list:
            if (key.count(METADATA_INDEX_DELIMITER) == 1):
                key_part = key.split(METADATA_INDEX_DELIMITER)[1]
                if key_part not in self.common_keys:
                    status, response = self.kv_api.delete(index_id, key)
                    super(S3RecoverCorruption, self).check_response(status, "delete", response, index_id, key)
            else:
                # Key corruption case. Perform cleanup
                status, response = self.kv_api.delete(index_id, key)
                super(S3RecoverCorruption, self).check_response(status, "delete", response, index_id, key)

    def restore_data(self, list_index_id, list_index_id_replica, metadata_index_id,
            metadata_index_id_replica):
        """
        Performs recovery of data to be restored

        """
        if ((not self.list_result) or (not self.metadata_result)):
            self.s3recovery_log("info", "No any data to recover\n")
            return

        self.s3recovery_log("info", '#' * 60)
        self.s3recovery_log("info", "Recovering global list index table")
        self.s3recovery_log("info", '#' * 60 + "\n")
        for key, value in self.list_result.items():
            if key in self.common_keys:
                self.s3recovery_log("info", "\nRecovering {} {}".format(key,value))
                super(S3RecoverCorruption, self).put_kv(list_index_id, key, value)
                super(S3RecoverCorruption, self).put_kv(list_index_id_replica, key, value)

        self.cleanup_bucket_list_entries(list_index_id)
        self.cleanup_bucket_list_entries(list_index_id_replica)

        """ Sample entry in bucket metadata table
        i.e self.metadata_result contents

        {'12345/test': '{"ACL":"","Bucket-Name":"test","Policy":"","System-Defined"
        :{"Date":"2020-07-07T11:30:53.000Z","LocationConstraint":"us-west-2",
        "Owner-Account":"s3_test","Owner-Account-id":"12345","Owner-User":"tester",
        "Owner-User-id":"123"},"create_timestamp":"2020-07-07T11:30:53.000Z",
        "motr_multipart_index_oid":"dQmoBAAAAHg=-AgAAAAAAtMU=",
        "motr_object_list_index_oid":"dQmoBAAAAHg=-AQAAAAAAtMU=",
        "motr_objects_version_list_index_oid":"dQmoBAAAAHg=-AwAAAAAAtMU="}
        }
        """

        self.s3recovery_log("info", "\n"+ '#' * 60)
        self.s3recovery_log("info", "Recovering bucket metadata table")
        self.s3recovery_log("info", '#' * 60 + "\n")
        for key, value in self.metadata_result.items():
            if (key.count(METADATA_INDEX_DELIMITER) == 1):
                if key.split(METADATA_INDEX_DELIMITER)[1] in self.common_keys:
                    self.s3recovery_log("info", "\nRecovering {} {}".format(key,value))
                    super(S3RecoverCorruption, self).put_kv(metadata_index_id, key, value)
                    super(S3RecoverCorruption, self).put_kv(metadata_index_id_replica, key, value)

        self.cleanup_bucket_metadata_entries(metadata_index_id)
        self.cleanup_bucket_metadata_entries(metadata_index_id_replica)

        self.s3recovery_log("info", "\nS3recovery passed successfully...!!!")

    def check_consistency(self, list_index_id, list_index_id_replica, metadata_index_id, metadata_index_id_replica):
        """
        Performs consistency check of indexes to be restored

        """
        self.common_keys = []
        if ((not self.list_result) and (not self.metadata_result)):
            self.list_result = {}
            self.metadata_result = {}
            return

        if (not self.list_result):
            self.s3recovery_log("info", "GBLI empty - Cleaning up GBMI\n")
            self.cleanup_bucket_metadata_entries(metadata_index_id)
            self.cleanup_bucket_metadata_entries(metadata_index_id_replica)
            self.list_result = {}
            self.metadata_result = {}
            return

        if (not self.metadata_result):
            self.s3recovery_log("info", "GBMI empty - Cleaning up GBLI\n")
            self.cleanup_bucket_list_entries(list_index_id)
            self.cleanup_bucket_list_entries(list_index_id_replica)
            self.list_result = {}
            self.metadata_result = {}
            return

        global_key_list = list(self.list_result.keys())
        global_metadata_list = list(self.metadata_result.keys())

        for item in global_metadata_list:
            if (item.count(METADATA_INDEX_DELIMITER) == 1):
                entry = item.split(METADATA_INDEX_DELIMITER)[1]
                if (entry in global_key_list):
                    self.common_keys.append(entry)

    def recover_corruption(self, list_index_name, list_index_id, list_index_id_replica,
            metadata_index_name, metadata_index_id, metadata_index_id_replica):
        """
        Performs recovery of index to be restored

        :list_index_name:  Name of list index being processed
        :list_index_id: Id of list index being processed
        :index_id_rlist_index_id_replicaeplica: Id of list replica index being processed
        :metadata_index_name:  Name of metadata index being processed
        :metadata_index_id: Id of metadata index being processed
        :metadata_index_id_replica: Id of metadata replica index being processed

        """
        union_result = dict()
        super(S3RecoverCorruption, self).initiate(list_index_name, list_index_id,
                list_index_id_replica, log_output = True)
        self.list_result = super(S3RecoverCorruption, self).dry_run(list_index_name, list_index_id,
                list_index_id_replica, union_result)

        metadata_result = dict()
        super(S3RecoverCorruption, self).initiate(metadata_index_name, metadata_index_id,
                metadata_index_id_replica, log_output = True)
        self.metadata_result = super(S3RecoverCorruption, self).dry_run(metadata_index_name, metadata_index_id,
                metadata_index_id_replica, metadata_result)

        self.check_consistency(list_index_id, list_index_id_replica, metadata_index_id, metadata_index_id_replica)
        self.restore_data(list_index_id, list_index_id_replica, metadata_index_id,
                metadata_index_id_replica)

    def start(self):
        """
        Entry point for recover algorithm

        """
        self.recover_corruption("Global bucket index", Config.global_bucket_index_id,
            Config.global_bucket_index_id_replica, "Bucket metadata index",
            Config.bucket_metadata_index_id, Config.bucket_metadata_index_id_replica)


###==========================================================
###==========================================================
### visualize the cnv-activations of CNN in training
### act2grdcp: output structure to catalyst pipeline
###==========================================================
###==========================================================

import numpy as np

imageSize=28*28
gimax,gjmax=8,4 #1st layer has 32 filters

def getColor(k,ms,ii):
    rc=-1
    if ii>0:
        for i in range(len(ms)):
            if k in ms[i]:
                rc=i
                break
    return rc*0.3

def act1ScatterPipe(act,ms,kl,tm):
    ii=tm%550
    gi,gj,k=0,0,0
    x_axis=np.linspace(0,1,imageSize)
    z_axis=np.ones(imageSize,dtype=np.int32)
    x_stride,y_stride=1.2,1.2
    lx,ly,lz=[],[],[]
    for a in act.T:
        gi,gj=kl[k]/gjmax,kl[k]%gjmax
        lx.append(gj*x_stride+x_axis)
        ly.append(gi*y_stride+a)
        grpColor=getColor(k,ms,ii)
        lz.append(z_axis*grpColor)
        k+=1
    x=np.asarray(lx).reshape(-1,)
    xx=x.tolist()
    y=np.asarray(ly).reshape(-1,)
    yy=y.tolist()
    z=np.asarray(lz).reshape(-1,)
    zz=z.tolist()
    actScatterPipe=np.asarray(zip(xx,yy,zz))
    return actScatterPipe


import pickle

import dateutil
import pandas as pd

"""
This script will parse the Gowalla location checkin dataset and extract the data and nomralize the latitude/longitude
"""

saved_df_filename = 'gowalla_df'  # the name of the file where the dataframe will be saved
file_name = 'Gowalla_totalCheckins.txt'  # the name of the gowalla dataset file
load_from_pickle = False  # this is used for debugging, leave it as False

def parse_line(line: str):
    line_arr = line.split("\t")
    user = int(line_arr[0])
    time = dateutil.parser.parse(line_arr[1])
    lat = float(line_arr[2])
    long = float(line_arr[3])
    location = int(line_arr[4])

    return user, time, lat, long, location

if __name__ == '__main__':
    colnames = ['user', 'checkin', 'latitude', 'longitude', 'location']

    if load_from_pickle:
        with open(saved_df_filename, 'rb') as input_file:
            df = pickle.load(input_file)
    else:
        with open(file_name) as f:
            df = pd.DataFrame([parse_line(l) for l in f], columns=colnames)

        # Normalize lat
        min_lat = df['latitude'].min()
        max_lat = df['latitude'].max()
        df['latitude'] = (df['latitude'] - min_lat) / (max_lat - min_lat)

        # Normalize long
        min_long = df['longitude'].min()
        max_long = df['longitude'].max()
        df['longitude'] = (df['longitude'] - min_long) / (max_long - min_long)

        with open(saved_df_filename, 'wb') as output:
            pickle.dump(df, output, pickle.HIGHEST_PROTOCOL)

    print('Done loading Gowalla dataset')


import animation
import random
import time
import sys
import argparse

def inorder(y, zeros = False):
    if zeros:
        x = list(filter(lambda i: i!=0, y))
    else:
        x = y

    i = 0
    j = len(x)
    while i + 1 < j:
        if x[i] > x[i + 1]:
            return False
        i += 1
    return True

class radixsort(animation.SortingAlgorithm):
    def sort(self):
        arrcopy = []
        for i in range(len(self.array)):
            arrcopy.append(self.get(i))

        for i in range(len(self.array)):
            self.set(arrcopy[i],arrcopy[i])

class jsquirks(animation.SortingAlgorithm):
    def initialize(self):

        self.drawcolor((248, 24, 148))
        self.fit()

    def sort(self):
        arrcopy = []
        for i in range(len(self.array)):
            arrcopy.append(self.get(i))

        for i in range(len(self.array)):
            self.set(arrcopy[i],arrcopy[i])

    def shutdown(self):
        self.write("Javascript has Quirks")

class stalinsort(animation.SortingAlgorithm):
    def sort(self):
        while not inorder(self.array,True):
            index = random.randint(0,len(self.array)-1)
            self.array[index] = 0
            self.update()
        while True:
            self.update()

class heapsort(animation.SortingAlgorithm):
    def sort(self):
        n = len(self.array)

        # Build a maxheap.
        for i in range(n, -1, -1):
            self.update()
            self.heapify(n, i)

        # One by one extract elements
        for i in range(n-1, 0, -1):
            self.array[i], self.array[0] = self.array[0], self.array[i] # swap
            self.heapify(i, 0)
            self.update()

    def heapify(self, n, i):
        self.update()
        largest = i # Initialize largest as root
        l = 2 * i + 1     # left = 2*i + 1
        r = 2 * i + 2     # right = 2*i + 2

        # See if left child of root exists and is
        # greater than root
        if l < n and self.array[i] < self.array[l]:
            largest = l

        # See if right child of root exists and is
        # greater than root
        if r < n and self.array[largest] < self.array[r]:
            largest = r

        # Change root, if needed
        if largest != i:
            self.array[i],self.array[largest] = self.array[largest],self.array[i] # swap

            # Heapify the root.
            self.heapify(n, largest)
        self.update()

class bogosort(animation.SortingAlgorithm):
    def initialize(self):
        self.scramble(8)

        self.linewidth(50)
        self.lineheightmultiplier(40)
        self.fit()

    def sort(self):
        while not inorder(self.array):
            random.shuffle(self.array)
            self.update()

class insertionsort(animation.SortingAlgorithm):
    def sort(self):
        for index in range(1, len(self.array)):
            currentvalue = self.get(index)
            position = index

            while position>0 and self.array[position-1]>currentvalue:
                self.swap(position,position-1)
                position -= 1

            self.set(position,currentvalue)

class mergesort(animation.SortingAlgorithm):
    def sort(self):
        self.mergeSort(0,len(self.array)-1)

    def mergeSort(self,l,r):
        if l < r:

            # Same as (l+r)/2, but avoids overflow for
            # large l and h
            m = (l+(r-1))//2

            # Sort first and second halves
            self.mergeSort(l, m)
            self.mergeSort(m+1, r)
            self.merge(l, m, r)

    def merge(self, l, m, r):
        n1 = m - l + 1
        n2 = r- m

        # create temp arrays
        L = [0] * (n1)
        R = [0] * (n2)

        # Copy data to temp arrays L[] and R[]
        for i in range(0 , n1):
            L[i] = self.get(l + i)

        for j in range(0 , n2):
            R[j] = self.get(m + 1 + j)

        # Merge the temp arrays back into arr[l..r]
        i = 0     # Initial index of first subarray
        j = 0     # Initial index of second subarray
        k = l     # Initial index of merged subarray

        while i < n1 and j < n2 :
            self.update()
            if L[i] <= R[j]:
                self.set(k,L[i])
                i += 1
            else:
                self.set(k, R[j])
                j += 1
            k += 1

        # Copy the remaining elements of L[], if there
        # are any
        while i < n1:
            self.set(k,L[i])
            i += 1
            k += 1

        # Copy the remaining elements of R[], if there
        # are any
        while j < n2:
            self.set(k, R[j])
            j += 1
            k += 1

class quicksort(animation.SortingAlgorithm):
    def sort(self):
        nItems = len(self.array)
        if nItems < 2:
            return

        todo = [(0, nItems - 1)]
        while todo:
            elem_idx, pivot_idx = low, high = todo.pop()
            self.popgetstack()
            self.popgetstack()
            elem = self.get(elem_idx, update=False)
            pivot = self.get(pivot_idx, update=False)

            while pivot_idx > elem_idx:
                self.update()
                if elem > pivot:
                    self.set(pivot_idx, elem)
                    pivot_idx -= 1
                    self.popgetstack()
                    elem = self.get(pivot_idx)
                    self.set(elem_idx, elem, update=False)
                else:
                    elem_idx += 1
                    self.popgetstack()
                    elem = self.get(elem_idx)
            self.set(pivot_idx, pivot,update=False)

            lsize = pivot_idx - low
            hsize = high - pivot_idx
            if lsize <= hsize:
                if 1 < lsize:
                    todo.append((pivot_idx + 1, high))
                    todo.append((low, pivot_idx - 1))
            else:
                todo.append((low, pivot_idx - 1))
            if 1 < hsize:
                todo.append((pivot_idx + 1, high))

class shellsort(animation.SortingAlgorithm):
    def sort(self):
        sublistcount = len(self.array)//2
        while sublistcount > 0:
            for startposition in range(sublistcount):
                self.gapInsertionSort(startposition,sublistcount)

            sublistcount = sublistcount // 2

    def gapInsertionSort(self,start,gap):
        for i in range(start+gap,len(self.array),gap):
            currentvalue = self.get(i)
            position = i

            while position>=gap and self.array[position-gap]>currentvalue:
                self.set(position,self.get(position-gap))
                position = position-gap

            self.set(position,currentvalue)

class bubblesort(animation.SortingAlgorithm):
    def sort(self):
        for passnum in range(len(self.array)-1,0,-1):
            for i in range(passnum):
                if self.array[i]>self.array[i+1]:
                    temp = self.array[i]
                    self.set(i, self.get(i+1))
                    self.array[i+1] = temp

class combsort(animation.SortingAlgorithm):
    def sort(self):
        n = len(self.array)

        # Initialize gap
        gap = n

        # Initialize swapped as true to make sure that
        # loop runs
        swapped = True

        # Keep running while gap is more than 1 and last
        # iteration caused a swap
        while gap !=1 or swapped == 1:

            # Find next gap
            gap = self.getNextGap(gap)

            # Initialize swapped as false so that we can
            # check if swap happened or not
            swapped = False

            # Compare all elements with current gap
            for i in range(0, n-gap):
                if self.array[i] > self.array[i + gap]:
                    self.swap(i, i+gap)
                    swapped = True

    def getNextGap(self,gap):
        # Shrink gap by Shrink factor
        gap = (gap * 10)/13
        if gap < 1:
            return 1
        return int(gap)

parser = argparse.ArgumentParser(description="sorting visualiser")
parser.add_argument("algorithm",type=str,choices=[i.__name__ for i in animation.animators])
parser.add_argument("-d","--delay",type=int, default=0)
args = parser.parse_args()

if hasattr(args,"delay"):
    time.sleep(args.delay)

for i in animation.animators:
    if i.__name__ == args.algorithm:
        i(150).run()

# s = eval("{}(150)".format(sys.argv[1]))
# s.run()


import torch
from ignite.engine import Engine

def prepare_batch(batch, device, non_blocking):
    data, label = batch
    data = data.unsqueeze(1).float().to(device)
    label = label.to(torch.int64).to(device)

    return data, label

def create_engine(model, optimizer, loss_fn, device):
    model.to(device)

    def _update(engine, batch):
        model.train()
        data, label = prepare_batch(batch, device, non_blocking=False)

        optimizer.zero_grad()

        output = model(data)
        _loss = loss_fn(output, label)
        _loss.backward()
        optimizer.step()

        return _loss.item()

    return Engine(_update)


import Book
import ArrayList
import ArrayQueue
#import RandomQueue
import DLList
import SLLQueue
import ChainedHashTable
import BinarySearchTree
import BinaryHeap
#import AdjacencyList
import MaxQueue
import time
import algorithms

class BookStore:
    '''
    BookStore: It simulates a book system such as Amazon. It allows  searching,
    removing and adding in a shopping cart.
    '''

    def __init__(self):
        self.bookCatalog = None
        self.shoppingCart = ArrayQueue.ArrayQueue() #MaxQueue.MaxQueue()
        self.bookIndices = ChainedHashTable.ChainedHashTable()
        self.sortedTitleIndices = BinarySearchTree.BinarySearchTree()

    def loadCatalog(self, fileName: str):
        '''
            loadCatalog: Read the file filenName and creates the array list with all books.
                book records are separated by  ^. The order is key,
                title, group, rank (number of copies sold) and similar books
        '''
        self.bookCatalog = ArrayList.ArrayList()
        with open(fileName, encoding="utf8") as f:
            # The following line is the time that the computation starts
            start_time = time.time()
            for line in f:
                (key, title, group, rank, similar) = line.split("^")
                s = Book.Book(key, title, group, rank, similar)
                self.bookCatalog.append(s)
                #self.bookIndices.add(key, self.bookCatalog.size() - 1)
                #self.sortedTitleIndices.add(title, self.bookCatalog.size() - 1)
            # The following line is used to calculate the total time
            # of execution
            elapsed_time = time.time() - start_time
            print(f"Loading {self.bookCatalog.size()} books in {elapsed_time} seconds")

    def setRandomShoppingCart(self):
        q = self.shoppingCart
        start_time = time.time()
        self.shoppingCart = RandomQueue.RandomQueue()
        while q.size() > 0:
            self.shoppingCart.add(q.remove())
        elapsed_time = time.time() - start_time
        print(f"Setting radomShoppingCart in {elapsed_time} seconds")

    def setShoppingCart(self):
        q = self.shoppingCart
        start_time = time.time()
        self.shoppingCart = ArrayQueue.ArrayQueue()
        while q.size() > 0:
            self.shoppingCart.add(q.remove())
        elapsed_time = time.time() - start_time
        print(f"Setting radomShoppingCart in {elapsed_time} seconds")

    def removeFromCatalog(self, i: int):
        '''
        removeFromCatalog: Remove from the bookCatalog the book with the index i
        input:
            i: positive integer
        '''
        # The following line is the time that the computation starts
        start_time = time.time()
        self.bookCatalog.remove(i)
        # The following line is used to calculate the total time
        # of execution
        elapsed_time = time.time() - start_time
        print(f"Remove book {i} from books in {elapsed_time} seconds")

    def addBookByIndex(self, i: int):
        '''
        addBookByIndex: Inserts into the playlist the song of the list at index i
        input:
            i: positive integer
        '''
        # Validating the index. Otherwise it  crashes
        if i >= 0 and i < self.bookCatalog.size():
            start_time = time.time()
            s = self.bookCatalog.get(i)
            self.shoppingCart.add(s)
            elapsed_time = time.time() - start_time
            print(f"Added to shopping cart {s}) \n{elapsed_time} seconds")

    def searchBookByInfix(self, infix: str, cnt : int):
        '''
        searchBookByInfix: Search all the books that contains infix
        input:
            infix: A string
        '''
        start_time = time.time()
        cookieMonster123 = 0
        for penguin in self.bookCatalog:
            if infix in str(penguin.title):
                print(penguin)
                cookieMonster123 += 1
            if cookieMonster123 >= cnt:
                break
        elapsed_time = time.time() - start_time
        print(f"searchBookByInfix Completed in {elapsed_time} seconds")

    def removeFromShoppingCart(self):
        '''
        removeFromShoppingCart: remove one book from the shoppung cart
        '''
        start_time = time.time()
        if self.shoppingCart.size() > 0:
            u = self.shoppingCart.remove()
            elapsed_time = time.time() - start_time
            print(f"removeFromShoppingCart {u} Completed in {elapsed_time} seconds")

    def getCartBestSeller(self):
        '''
        getCartBestSeller: returns best-seller amongst the rest of the books in the cart
        '''
        print(f'getCartBestSeller returned')
        print(self.shoppingCart.max().title)
        return self.shoppingCart.max().title

    def addBookByKey(self, key):
        start_time = time.time()
        indicieisadiashujashdjkadsjhkl = self.bookIndices.find(key)
        if indicieisadiashujashdjkadsjhkl == None:
            print('Book not found.')
        else:
            ihukjfsdiuhjlnsfdijuohlsfdlhjiksfdwljhkn = self.bookCatalog.get(indicieisadiashujashdjkadsjhkl)
            self.shoppingCart.add(ihukjfsdiuhjlnsfdijuohlsfdlhjiksfdwljhkn)
            print(f'Added title: {ihukjfsdiuhjlnsfdijuohlsfdlhjiksfdwljhkn.title}')
        elapsed_time = time.time() - start_time
        print(f'addBookByKey Completed in {elapsed_time} seconds')

    def addBookByPrefix(self, prefix):
        book = self.sortedTitleIndices.find_smallest_greater_node(prefix)
        if book.k[0:len(prefix)] == prefix:
            if len(prefix) > 0:
                self.shoppingCart.add(self.bookCatalog.get(book.v))
                print("Added first matched title: " + book.k)
            else:
                print('Error: Prefix was not found.')
        else:
            print('Error: Prefix was not found.')

    def bestsellers_with(self, infix, structure, n):
        if n == '':     n = 0
        n = int(n)
        if infix == '': print('Invalid infix.')
        elif n < 0:     print('Invalid number of titles.')
        else:
            abcdefghijklmnopqrstuvwxyz = time.time()
            if True: #if structure == '1':
                BS = BinarySearchTree.BinarySearchTree()
                for goo in self.bookCatalog:
                    if infix in goo.title:
                        BS.add(goo.rank, goo)
                yeets = BS.in_order()
                yeets.reverse()
                for yoot, yeet in enumerate(yeets):
                    if yoot >= n and n > 0:
                        break
                    print(yeet.v)
            elif structure == '2':
                BS = BinaryHeap.BinaryHeap()
                for goo in self.bookCatalog:
                    if infix in goo.title:
                        goo.rank *= -1
                        BS.add(goo)
                for _ in range(BS.size()):
                    if _ >= n and n > 0:
                        break
                    yo = BS.remove()
                    yo.rank *= -1
                    print(yo)
            else:   print('Invalid data structure.')
            yo0123456789101112231415161718192021 = time.time() - abcdefghijklmnopqrstuvwxyz
            print(f'Displayed bestsellers_with({infix}, {structure}, {n}) in {yo0123456789101112231415161718192021} seconds')

    def sort_catalog(self, s):
        yo = time.time()
        if s == '1':
            algorithms.merge_sort(self.bookCatalog)
        elif s == '2':
            algorithms.quick_sort(self.bookCatalog, 0)
        elif s == '3':
            algorithms.quick_sort(self.bookCatalog, 'ioujfdauhisdfijhdfs')
        else:
            print('Invalid algorithm')
            return False
        yoyo = time.time() - yo
        print(f'Sorted {self.bookCatalog.size()} books in {yoyo} seconds')
        return True

    def display_catalong(self, n):
        for johnpork in range(int(n)):
            print(self.bookCatalog.get(johnpork))


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

# 定义变量 变量名 = 量
name = "Duo Zhang"
# 调用变量
print("my name is", name)
# 也可以将一个变量的值赋给另一个变量
name2 = name
# 变量名全部用大写代表这是一个常量（不能改）
PIE = 3.14
# ASCII 码每个占8 bytes
# Unicode 每个占 16 bytes
'''三个引号是多行注释'''


import csv
import cv2
import numpy as np
from model import model as m
import tensorflow as tf
from keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau
from keras.optimizers import Adam
from keras.utils.vis_utils import plot_model

IMAGE_HEIGHT = 160
IMAGE_WIDTH = 320

def load_data(csv_path='data/driving_log.csv'):
    """
    csv_path: string (path of .csv file where image name and mesurnments are stored).
    This function will be used to read image name from .csv file
    and load images and its stearing measurnment.

    This function also flip the images and store as training data.
    Obviously will be negative of original image.

    return: X_train, y_train
    """
    lines = []

    # Reading csv file and appending in a list
    with open(csv_path) as csvfile:
        reader = csv.reader(csvfile)
        for line in reader:
            lines.append(line)

        lines = lines[1:]

    images = []
    measurnments = []

    # Get images and mesurnments. Also stored fliped images and negative of mesurnment.
    for line in lines:
        source_path = line[0]
        filename = source_path.split('/')[-1]
        current_path = 'data/IMG/{}'.format(filename)
        image = cv2.imread(current_path)
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        images.append(image)
        measurnment = float(line[3])
        measurnments.append(measurnment)

        image = np.fliplr(image)
        images.append(image)
        measurnments.append(-1.0*measurnment)

    X_train = np.array(images)
    y_train = np.array(measurnments)

    return X_train, y_train

def train(X_train, y_train, save_model='model.h5'):
    """
    This function will be use to train model and save model for given training set.
    X_train: numpy array of training images
    y_train: numpy array of stearing mesurnments.
    save_model: string (name of model, default is model.h5)

    return: None
    """

    # Hyperparameters
    batch_size = 32
    epochs = 30
    learning_rate = 0.001

    # Loading model from model.py
    model = m(input_height=IMAGE_HEIGHT, input_width=IMAGE_WIDTH)

    # Plot model as image
    plot_model(model, to_file='model_plot.png', show_shapes=True, show_layer_names=True)

    # If trained model exist already then load first for further training
    if tf.gfile.Exists(save_model):
        model.load_weights(save_model)
    model.compile(loss='mse', optimizer=Adam(learning_rate))

    # Only save model which has best performed on validation set.
    # These are callbacks which are being used in "model.fit" call
    earlyStopping = EarlyStopping(monitor='val_loss', patience=5, verbose=1, mode='min')
    mcp_save = ModelCheckpoint('model.h5', save_best_only=True, monitor='val_loss', mode='min')
    reduce_lr_loss = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=7, verbose=1, epsilon=1e-4, mode='min')

    # Train the model
    model.fit(X_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1, callbacks=[earlyStopping, mcp_save, reduce_lr_loss], validation_split=0.2, shuffle=True)

    return

if __name__ == '__main__':
    # Load data
    X_train, y_train = load_data()
    # Train model
    train(X_train, y_train)


from sig_gen import sig_gen

simulation_time = 4  # time of simulation

fs = 4000.0  # main sampling frequency
fs2 = 100.0  # sampling frequency 2
dec_coef = fs / fs2

sim_point = int(simulation_time / (1.0 / fs))
sim_point2 = int(simulation_time / (1.0 / fs2))

Time=[]
Time2=[]

for t in range (0, sim_point):
    Time.append(t*1.0/fs)

for t in range (0, sim_point2):
    Time2.append(t*1.0/fs2)

def mix_signals()->list:
    Signals = [["Частота ","Амплитуда ","Модуляция"],
               [420, 3 * 1024, 8 ],
               [480, 0 * 1024, 12],
               [565, 1 * 1024, 8 ],
               [720, 1 * 1024, 12],
               [780, 1 * 1024, 8 ],
               [ 75, 3 * 1024, 0 ],
               [125, 0 * 1024, 0 ],
               [175, 2 * 1024, 0 ],
               [225, 0 * 1024, 0 ],
               [275, 1 * 1024, 0 ],
               [325, 1.5 * 1024, 0 ]]

    mix_signals = [0]*sim_point

    for signal in Signals:
        if isinstance(signal[1], int):
            if signal[1] > 0:
                y = sig_gen(signal,Time)
                mix_signals= [a + b for a, b in zip(mix_signals, y)]

    return mix_signals, Signals


import os
from selenium import webdriver

Instance = None

def initialize():
    global Instance
    if Instance is None:
        Instance = webdriver.Chrome(os.path.abspath(os.path.join(__file__, "../../.."))+"\\resource\\chromedriver.exe")
        Instance.implicitly_wait(5)
    return Instance

def close_driver():
    global Instance
    Instance.quit()
    Instance = None


import imageio
from skimage.transform import resize
import numpy as np

def center_crop(x, crop_h, crop_w=None, resize_w=64):
    if crop_w is None:
        crop_w = crop_h
    h, w = x.shape[:2]
    j = int(round((h - crop_h)/2.))
    i = int(round((w - crop_w)/2.))
    assert i+crop_w < w and j+crop_h < h, "invalid crop_h (and/or crop_w)."
    return resize(x[j:j+crop_h, i:i+crop_w], [resize_w, resize_w])

def specific_crop(x, point, crop_h, crop_w=None, resize_w=64):
    if crop_w is None:
        crop_w = crop_h
    h, w = x.shape[:2]
    j = int(point[1])
    i = int(point[0])
    assert j > 0 and i > 0
    assert i+crop_w < w and j+crop_h < h, "invalid crop_h (and/or crop_w) or starting point."
    return resize(x[j:j+crop_h, i:i+crop_w], [resize_w, resize_w])

def transform(image, npx=64, point=None, is_crop=True, resize_w=64):
    if is_crop:
        if point is None:
            cropped_image = center_crop(image, npx, resize_w=resize_w)
        else:
            cropped_image = specific_crop(image, point, npx, resize_w=resize_w)
    else:
        cropped_image = image if npx==resize_w else resize(image, [resize_w, resize_w])
    return np.array(cropped_image)/127.5 - 1.

def imread(path, is_grayscale=False):
    if (is_grayscale):
        return imageio.imread(path, as_gray=True).astype(np.float)
    else:
        return imageio.imread(path).astype(np.float)

def get_image(image_path, image_size, point=None, is_crop=True, resize_w=64, is_grayscale=False):
    return transform(imread(image_path, is_grayscale), image_size, point=point, is_crop=is_crop, resize_w=resize_w)


import os
import random
import socket
import string
from typing import List

from paramiko.buffered_pipe import PipeTimeout

from src.GoogleCloudInfo import cloud_info_list
from src.MasterNode import MasterNode
from src.SlaveNode import SlaveNode

home_user = 'am72ghiassi'

def main(args: List[str]):
    if len(args) < 6:
        print("Usage: find_runtime_one.py cloud_index region n_cpus n_mb_ram network batch_size")
        exit(1)

    cloud_info = cloud_info_list[int(args[1])]
    region = args[2]
    cpu_cores = int(args[3])
    amount_ram = int(args[4])
    network = args[5]
    batch_size = int(args[6])

    print(f"On index {int(args[1])} in regin {region} cpu_cores {cpu_cores} amount_ram {amount_ram} network {network} batch_size {batch_size}")

    ex_id = ''.join(random.choice(string.ascii_lowercase) for i in range(8))  # Generate a random project id
    master = MasterNode(f"master-{ex_id}", cloud_info, master=True, location_string=region)
    SlaveNode(f"slave-{ex_id}-0", cloud_info, master_node=master, location_string=region)
    os.makedirs(f"raw/one", exist_ok=True)
    max_epochs = 50
    filename = f"nodes1-cores{cpu_cores}-memory{amount_ram}-network{network}-batchsize{batch_size}.log"
    command = f"/home/{home_user}/bd/spark/bin/spark-submit --master spark://{master.privip}:7077 --driver-cores 1 " + \
              f"--driver-memory 1G --total-executor-cores {cpu_cores} --executor-cores {cpu_cores} --executor-memory {amount_ram}M " + \
              f"--py-files /home/{home_user}/bd/spark/lib/bigdl-0.11.0-python-api.zip,/home/{home_user}/bd/codes/{network}.py " + \
              f"--properties-file /home/{home_user}/bd/spark/conf/spark-bigdl.conf " + \
              f"--jars /home/{home_user}/bd/spark/lib/bigdl-SPARK_2.3-0.11.0-jar-with-dependencies.jar " + \
              f"--conf spark.driver.extraClassPath=/home/{home_user}/bd/spark/lib/bigdl-SPARK_2.3-0.11.0-jar-with-dependencies.jar " + \
              f"--conf spark.executer.extraClassPath=bigdl-SPARK_2.3-0.11.0-jar-with-dependencies.jar /home/{home_user}/bd/codes/{network}.py " + \
              f"--action train --dataPath /tmp/mnist --batchSize {batch_size} --endTriggerNum {max_epochs} " + \
              f"--learningRate 0.01 --learningrateDecay 0.0002 > {ex_id}-{filename}"

    print(f"Executing command: {command}")
    stdin, stdout, stderr = master.ssh.exec_command(command)

    try:
        print(stdout.read())
        print(stderr.read())
    except PipeTimeout:
        print("PipeTimeout")
    except socket.timeout:
        print("Socket timeout")
    # master.cancel()
    sftp = master.ssh.open_sftp()
    sftp.get(f'{ex_id}-{filename}', f'raw/one/{filename}')

if __name__ == '__main__':
    try:
        import sys
        main(sys.argv)
    except Exception as e:
        print(e)
    finally:
        input("finished!")


n = int(input())

for i in range(n):
    data = list(str(input()))
    answer = 0
    cnt = 0
    for j in data:
        if j == "O":
            cnt +=1
            answer = answer + cnt
        else:
            cnt =0
    print(answer)


#!/usr/bin/python3
""" A Rectangle module with the class Rectangle"""
from models.base import Base

class Rectangle(Base):
    """ Rectangle class that inherits from Base """

    def __init__(self, width, height, x=0, y=0, id=None):
        """ Constructor method that initializes the objects shown """
        super().__init__(id)
        self.width = width
        self.height = height
        self.x = x
        self.y = y

    @property
    def width(self):
        """ Getter for width """
        return self.__width

    @width.setter
    def width(self, value):
        """ Setter for width """
        if not isinstance(value, int):
            raise TypeError("width must be an integer")
        if value <= 0:
            raise ValueError("width must be > 0")
        self.__width = value

    @property
    def height(self):
        """ Getter for height """
        return self.__height

    @height.setter
    def height(self, value):
        """ Setter for height """
        if not isinstance(value, int):
            raise TypeError("height must be an integer")
        if value <= 0:
            raise ValueError("height must be > 0")
        self.__height = value

    @property
    def x(self):
        """ Getter for x """
        return self.__x

    @x.setter
    def x(self, value):
        """ Setter for x """
        if not isinstance(value, int):
            raise TypeError("x must be an integer")
        if value < 0:
            raise ValueError("x must be >= 0")
        self.__x = value

    @property
    def y(self):
        """ Getter for y """
        return self.__y

    @y.setter
    def y(self, value):
        """ Setter for y """
        if not isinstance(value, int):
            raise TypeError("y must be an integer")
        if value < 0:
            raise ValueError("y must be >= 0")
        self.__y = value

    def area(self):
        """ This method calculates and returns the area of the Rectangle"""
        return self.__width * self.__height

    def display(self):
        """ This method displays the Rectangle -- taking care of x and y -- with # characters"""
        print("\n" * self.__y, end="")
        for _ in range(self.__height):
            print(" " * self.__x + "#" * self.__width)

    def __str__(self):
        """ This method overrides __str__ to return [Rectangle] (<id>) <x>/<y> - <width>/<height>"""
        return "[Rectangle] ({}) {}/{} - {}/{}".format(
            self.id, self.__x, self.__y, self.__width, self.__height)

    def update(self, *args, **kwargs):
        """ This method adds both positional and keyword arguments to attributes in the given order"""
        if args:
            attributes = ["id", "width", "height", "x", "y"]
            for index, value in enumerate(args):
                setattr(self, attributes[index], value)
        elif kwargs:
            for key, value in kwargs.items():
                setattr(self, key, value)


import argparse
import sys
from .api import DummyText

def create_parser():
    parser = argparse.ArgumentParser(
        prog='mlrandom',
        add_help=False,
        description='Malayalam Random Text Generator'
        ' [Word | Sentence | Paragraphs]',
        epilog='''Run '%(prog)s --help'for more information.''')

    subparsers = parser.add_subparsers(
        dest='command',
        title='Commands',
        description='These are common commands used in various situations')
    parser_group = parser.add_argument_group(title='Options')
    add_help_arg(parser_group)
    add_punctuate_arg(parser_group)

    wrdparser = subparsers.add_parser(
        'word',
        add_help=False,
        help='Generate a random word',
        description='Generate a random word from a charset')
    wrdparser_group = wrdparser.add_argument_group(title='Options')
    add_word_args(wrdparser)
    add_help_arg(wrdparser_group)
    add_punctuate_arg(wrdparser_group)
    add_charset_arg(wrdparser_group)

    senparser = subparsers.add_parser(
        'sentence',
        add_help=False,
        help='Generate a random sentence',
        description='Generate a random sentence from a charset')
    senparser_group = senparser.add_argument_group(title='Options')
    add_word_args(senparser)
    add_help_arg(senparser_group)
    add_punctuate_arg(senparser_group)
    add_charset_arg(senparser_group)
    add_sentence_args(senparser_group)

    paraparser = subparsers.add_parser(
        'paragraphs',
        add_help=False,
        help='Generate multiple random paragraphs',
        description='Generate a random paragraphs from a charset')
    paraparser_group = paraparser.add_argument_group(title='Options')
    add_word_args(paraparser)
    add_help_arg(paraparser_group)
    add_punctuate_arg(paraparser_group)
    add_charset_arg(paraparser_group)
    add_sentence_args(paraparser_group)
    add_para_args(paraparser_group)

    return parser

def add_word_args(group):
    group.add_argument(
        '--minlen',
        type=int,
        default=2,
        dest='minlen',
        help='The minimum number of characeters to be required in a word.')
    group.add_argument(
        '--maxlen',
        type=int,
        default=8,
        dest='maxlen',
        help='The maximum number of characeters to be required in a word.')

def add_sentence_args(group):
    group.add_argument(
        '--wordcount', '-wc',
        type=int,
        default=8,
        dest='wordcount',
        help='The maximum number of words to be required in a sentence.')

def add_para_args(group):
    group.add_argument(
        '--paracount', '-pc',
        type=int,
        default=5,
        dest='paracount',
        help='The maximum number of paragraphs to be required in the text.')

def str2bool(v):
    if v.lower() in ('yes', 'true', 't', 'y', '1'):
        return True
    elif v.lower() in ('no', 'false', 'f', 'n', '0'):
        return False
    else:
        raise argparse.ArgumentTypeError('Boolean value expected.')

def add_punctuate_arg(group):
    group.add_argument(
        '--punctuate', '-p',
        type=str2bool,
        default=True,
        dest='punctuate',
        help='Disable punctuations in the paragraphs and sentences')

def add_charset_arg(group):
    group.add_argument(
        '--charset', '-C',
        type=str,
        default='',
        dest='charset',
        help='The characeter set that needs to be used.')

def add_help_arg(group):
    group.add_argument(
        '--help', '-h',
        action='help',
        help='Show this help message and exit')

def cli(args=sys.argv[1:]):

    parser = create_parser()
    ns = parser.parse_args(args)

    dummy = DummyText(punctuate=ns.punctuate)
    if not ns.command:
        parser.print_help()
        return
    elif ns.command == 'word':
        args = [ns.minlen, ns.maxlen, ns.charset]
        print(dummy.gen_word(*args))
        return
    elif ns.command == 'sentence':
        args = [ns.minlen, ns.maxlen, ns.charset]
        print(dummy.gen_sentence(ns.wordcount, *args))
        return
    elif ns.command == 'paragraphs':
        args = [ns.wordcount,ns.minlen, ns.maxlen, ns.charset]
        print(dummy.gen_paragraph(ns.paracount, *args))
        return


########### sample of calling FaceAPI from Python 3.6 #############
## @fujute : March4,2018
##  This is modified version from original code from " https://westus.dev.cognitive.microsoft.com/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395236 "
##
import http.client, urllib.request, urllib.parse, urllib.error, base64

headers = {
    # Request headers
    'Content-Type': 'application/json',
    'Ocp-Apim-Subscription-Key': 'PLEASE REPLACE THIS TEXT WITH YOUR FACE API KEY',
}
# https://www.microsoft.com/cognitive-services/en-us/face-api
# reference https://westus.dev.cognitive.microsoft.com/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395236

params = urllib.parse.urlencode({
    # Request parameters
    'returnFaceId': 'true',
    'returnFaceLandmarks': 'false',
    'returnFaceAttributes': 'age,gender,smile,facialHair,glasses,headPose,emotion',
})

mypictures = ["{ 'url': 'https://raw.githubusercontent.com/fuju9w/cognitive/master/man-crazy-funny-dude-45882.jpg' }","{ 'url': 'https://raw.githubusercontent.com/fuju9w/cognitive/master/peam-m1-2017.jpg' }","{ 'url': 'https://raw.githubusercontent.com/fuju9w/cognitive/master/pexels-photo-372042.jpg' }"]

try:
	conn = http.client.HTTPSConnection('southeastasia.api.cognitive.microsoft.com')
	for body in mypictures:
		print(body)
		conn.request("POST", "/face/v1.0/detect?%s" % params, body, headers)
		response = conn.getresponse()
		data = response.read()
		print(data)
		print ("------------------------------------------")
	conn.close()
except Exception as e:
    print("[Errno {0}] {1}".format(e.errno, e.strerror))

####################################


#!/usr/bin/env python3

import re
import sys

pat_open = '[{][{][<]'
pat_close = '[>][}][}]'

pat_include = 'include[:](?P<filepath>.*?)'
pat_indent = ',indent[:](?P<indent>[0-9-]+)'
pat_include_options = '(' + pat_indent + ')*'

# {{<toc,levels:2-3,source=.>}}
# {{<toc,levels:2-3,source=path/to/file>}}
pat_toc = 'toc'
pat_levels = ',levels[:](?P<level_from>[0-9]+)[-](?P<level_to>[0-9]+)'
pat_source = ',source:(?P<source>([.]|.*?))'
pat_toc_options = '(' + pat_levels + '|' + pat_source + ')*'

# {{<chars.snippet>}}
# // {{<chars.snippet,indent:-4>}}
rx_include = re.compile(
    '([/]{2}[ \t]*)*' +
    pat_open +
    pat_include + pat_include_options +
    pat_close
)

rx_toc = re.compile(
    pat_open +
    pat_toc + pat_toc_options +
    pat_close
)

rx_md_codeblock = re.compile('^[`]{3}')
rx_md_headline = re.compile('^(?P<level>[#]+)[ \t]*(?P<heading>.*)')

rx_table_delim = re.compile('[-][+][-]')

SOURCE_SELF = '.'

def indent_block(num, block):
    lines = block.split('\n')
    if num > 0:
        lines = [' ' * num + line for line in lines]
    else:
        num = num * -1
        rx = re.compile('^[ ]{' + str(num) + '}')
        lines = [rx.sub('', line) for line in lines]
    return '\n'.join(lines)

def build_md_toc(level_zero, spec):
    lines = []

    indent = '  '
    for level, title, url in spec:
        level = level - level_zero
        line = '%s* [%s](%s)' % (indent * level, title, url)
        lines.append(line)

    return '\n'.join(lines)

def create_toc(level_from, level_to, source, cur_content):
    content = cur_content
    if source != SOURCE_SELF:
        with open(source) as f:
            content = f.read()

    lines = content.split('\n')

    spec = []

    in_code_block = False
    for line in lines:
        if rx_md_codeblock.search(line):
            if not in_code_block:
                in_code_block = True
            else:
                in_code_block = False

        if not in_code_block:
            match = rx_md_headline.search(line)
            if match:
                level = len(match.group('level'))
                heading = match.group('heading').strip()

                if level_from <= level <= level_to:
                    url = '#' + heading.replace(' ', '-').lower()
                    if source != SOURCE_SELF:
                        url = '%s%s' % (source, url)
                    spec.append((level, heading, url))

    return build_md_toc(level_from, spec)

def process(infile):
    outfile = infile.replace('_in', '')
    outfile = outfile.replace('.org', '.md')

    print(" < Reading from: %s" % infile)
    with open(infile) as f:
        content = f.read()

    match = rx_include.search(content)
    while match:
        filepath = match.group('filepath')

        indent_s = match.group('indent')
        indent_chars = int(indent_s) if indent_s else 0

        print("  - Including file: %s" % filepath)
        with open(filepath) as f:
            included_content = f.read().rstrip()

        included_content = indent_block(indent_chars, included_content)
        content = content[:match.start()] + included_content + content[match.end():]

        match = rx_include.search(content)

    match = rx_toc.search(content)
    while match:
        level_from = int(match.group('level_from'))
        level_to = int(match.group('level_to'))
        source = match.group('source')

        print("  - Creating toc from file: %s" % source)
        toc = create_toc(level_from, level_to, source, content)
        content = content[:match.start()] + toc + content[match.end():]

        match = rx_toc.search(content)

    content = rx_table_delim.sub('-|-', content)

    print(" > Writing to: %s" % outfile)
    with open(outfile, 'w') as f:
        f.write(content)

if __name__ == '__main__':
    try:
        infile = sys.argv[1]
    except IndexError:
        print("Usage: %s file_in" % sys.argv[0])
    process(infile)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Sep 15 12:32:06 2019

@author: dinesh
"""

import sqlite3

connection = sqlite3.Connection("/home/dinesh/titanic.db")

cursor = connection.cursor()

table = """
        CREATE TABLE classroom (
        student_id INTEGER PRIMARY KEY,
        name VARCHAR(20),
        math INTEGER,
        phy INTEGER,
        chi INTEGER
        );
        """

#cursor.execute(table)

#cursor.commit()

query = """ select * from classroom;"""

cursor.execute(query)

r = cursor.fetchall()

for i in r:
    print(i)

connection.close()


from PyQt5.QtWidgets import QSystemTrayIcon, QApplication
from domestic.widgets.menu import *
from domestic.core import ReaderDb

class SystemTray(QSystemTrayIcon):
    def __init__(self, parent):
        super().__init__()
        self.parent = parent
        self.setVisible(True)
        self.setIcon(QIcon(":/images/rss-icon-128.png"))

        self.updateToolTip()

        self.activated.connect(self.parentShow)
        self.messageClicked.connect(self.parentShow)

    def updateToolTip(self):
        db = ReaderDb()
        db.execute("select * from store where iscache=1")
        unread = db.cursor.fetchall()
        db.execute("select * from store where isstore=1")
        store = db.cursor.fetchall()
        db.execute("select * from store where istrash=1")
        trash = db.cursor.fetchall()
        self.setToolTip(self.tr('''{} - {}

Unread: {}

Stored: {}

Deleted: {}''').format(QApplication.applicationName(),
        QApplication.applicationVersion(), len(unread), len(store), len(trash)))

    def parentShow(self):
        self.parent.show()


import json
import requests
import sys
headers = {
    'origin': 'https://m.ctrip.com',
    'content-type': 'application/json',
    'Referer': 'https://m.ctrip.com/html5/flight/swift/domestic/BJS/SHA/2019-03-15',
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
    'X-Requested-With': 'XMLHttpRequest',
}
#save dict to file, in case we need that later
def save_wb_to_file(wb, filename):
    with open(filename,'w') as fd:
        json.dump(wb, fd)

#get dict back from file when needed
def get_wb_from_file(filename):
    with open(filename) as fd:
        return json.load(fd)

code_name=dict()
cmd_len=len(sys.argv)
wb=get_wb_from_file('addr_code')
ADDR=['ABCDEF','GHIJ','KLMN','PQRSTUVW','XYZ']
for addr in ADDR:
    #print(addr)
    w1=wb.get(addr)
    for c in  addr:
        if not w1.get(c):
            continue
        for l in w1.get(c):
            name=l.get('display')
            code=l.get('data')[-3:]
            code_name.update({name:code})

print(code_name)
if cmd_len != 4:
    print('Usage: python3 air_test.py 青岛 北京 2019-05-12')
    exit()
print(sys.argv[1], end='-')
print(sys.argv[2], end=' ')
print(sys.argv[3])
DepCity=code_name.get(sys.argv[1])
ArrCity=code_name.get(sys.argv[2])
Date=sys.argv[3]

#ArrCity="青岛"
#DepCity="南京"
#Date="2019-03-19"

p={"preprdid":"","trptpe":1,"flag":8,"searchitem":[{"dccode":DepCity,"accode":ArrCity,"dtime":Date}],"subchannel":'',"tid":"{6d549c74-62d5-42e7-a2cb-35c94d349df4}","head":{"cid":"09031046111774300258","ctok":"","cver":"1.0","lang":"01","sid":"8888","syscode":"09","auth":'',"extension":[{"name":"protocal","value":"https"}]},"contentType":"json"}

r=requests.post('https://m.ctrip.com/restapi/soa2/14022/flightListSearch?_fxpcqlniredt=09031046111774300258', data=json.dumps(p), headers=headers)
wb=r.json()
#save_wb_to_file(wb,'ctrip.json')
#wb=get_wb_from_file('ctrip.json')

#print(r.url)
#print(r.headers)
#print(r.status_code)
#print(wb)
all_flight= wb.get('fltitem')
i=0

print('航班号         出发机场     到达机场        日期        起飞时间      落地时间   最低票价')
for l in all_flight:
    print(l.get('mutilstn')[i].get('basinfo').get('flgno').ljust(8), end='     ')
    print(l.get('mutilstn')[i].get('dportinfo').get('cityname'),end='(')
    print(l.get('mutilstn')[i].get('dportinfo').get('aportsname'),end=')    ')
    print(l.get('mutilstn')[i].get('aportinfo').get('cityname'), end='(')
    print(l.get('mutilstn')[i].get('aportinfo').get('aportsname'), end=')    ')
    data=l.get('mutilstn')[i].get('dateinfo').get('ddate')
    print(data.split()[0],end='     ')
    print(data.split()[1],end='     ')
    data=l.get('mutilstn')[i].get('dateinfo').get('adate')
    print(data.split()[1],end='     ')
    print(l.get('policyinfo')[i].get('priceinfo')[0].get('price'))


"""scrapli_cfg.platform.core.juniper_junos.base_platform"""
import re
from datetime import datetime
from logging import LoggerAdapter
from typing import Tuple

from scrapli.driver.network.base_driver import PrivilegeLevel
from scrapli_cfg.platform.core.juniper_junos.patterns import (
    EDIT_PATTERN,
    OUTPUT_HEADER_PATTERN,
    VERSION_PATTERN,
)

CONFIG_SOURCES = [
    "running",
]

JUNOS_ADDTL_PRIVS = {
    "shell": (
        PrivilegeLevel(
            pattern=r"^(?!root)%\s?$",
            name="shell",
            previous_priv="exec",
            deescalate="exit",
            escalate="start shell",
            escalate_auth=False,
            escalate_prompt="",
        )
    ),
    # feel like ive had issues w/ root shell in the past... if this all goes well then it can be
    # added back to scrapli core
    "root_shell": (
        PrivilegeLevel(
            pattern=r"^root@%\s?$",
            name="root_shell",
            previous_priv="exec",
            deescalate="exit",
            escalate="start shell user root",
            escalate_auth=True,
            escalate_prompt=r"^[pP]assword:\s?$",
        )
    ),
}

class ScrapliCfgJunosBase:
    logger: LoggerAdapter
    candidate_config: str
    candidate_config_filename: str
    _in_configuration_session: bool
    _replace: bool
    _set: bool
    filesystem: str

    @staticmethod
    def _parse_version(device_output: str) -> str:
        """
        Parse version string out of device output

        Args:
            device_output: output from show version command

        Returns:
            str: device version string

        Raises:
            N/A

        """
        version_string_search = re.search(pattern=VERSION_PATTERN, string=device_output)

        if not version_string_search:
            return ""

        version_string = version_string_search.group(0) or ""
        return version_string

    def _reset_config_session(self) -> None:
        """
        Reset config session info

        Resets the candidate config and config session name attributes -- when these are "empty" we
        know there is no current config session

        Args:
            N/A

        Returns:
            None

        Raises:
            N/A

        """
        self.logger.debug("resetting candidate config and candidate config file name")
        self.candidate_config = ""
        self.candidate_config_filename = ""
        self._in_configuration_session = False
        self._set = False

    def _prepare_config_payloads(self, config: str) -> str:
        """
        Prepare a configuration so it can be nicely sent to the device via scrapli

        Args:
            config: configuration to prep

        Returns:
            str: string of config lines to write to candidate config file

        Raises:
            N/A

        """
        final_config_list = []
        for config_line in config.splitlines():
            final_config_list.append(
                f"echo >> {self.filesystem}{self.candidate_config_filename} '{config_line}'"
            )

        final_config = "\n".join(final_config_list)

        return final_config

    def _prepare_load_config(self, config: str, replace: bool) -> str:
        """
        Handle pre "load_config" operations for parity between sync and async

        Args:
            config: candidate config to load
            replace: True/False replace the configuration; passed here so it can be set at the class
                level as we need to stay in config mode and we need to know if we are doing a merge
                or a replace when we go to diff things

        Returns:
            str: string of config to write to candidate config file

        Raises:
            N/A

        """
        self.candidate_config = config

        if not self.candidate_config_filename:
            self.candidate_config_filename = f"scrapli_cfg_{round(datetime.now().timestamp())}"
            self.logger.debug(
                f"candidate config file name will be '{self.candidate_config_filename}'"
            )

        config = self._prepare_config_payloads(config=config)
        self._replace = replace

        return config

    def _normalize_source_candidate_configs(self, source_config: str) -> Tuple[str, str]:
        """
        Normalize candidate config and source config so that we can easily diff them

        Args:
            source_config: current config of the source config store

        Returns:
            ScrapliCfgDiff: scrapli cfg diff object

        Raises:
            N/A

        """
        self.logger.debug("normalizing source and candidate configs for diff object")

        source_config = re.sub(pattern=OUTPUT_HEADER_PATTERN, string=source_config, repl="")
        source_config = re.sub(pattern=EDIT_PATTERN, string=source_config, repl="")
        source_config = "\n".join(line for line in source_config.splitlines() if line)
        candidate_config = re.sub(
            pattern=OUTPUT_HEADER_PATTERN, string=self.candidate_config, repl=""
        )
        candidate_config = "\n".join(line for line in candidate_config.splitlines() if line)

        return source_config, candidate_config


class MajorityElement(object):

  def majorityElement(self, nums):
    votes = 0
    majorityElement = None
    for counter, num in enumerate(nums):
        if votes == 0:
            majorityElement = num
            votes += 1
        elif majorityElement != num:
            votes -= 1
        elif majorityElement == num:
            votes += 1
        else:
            raise Exception('Reached case where votes<0 which should ideally never be reached')
    return majorityElement

from nose.tools import assert_equals, assert_raises

class TestMajorityElement(object):

  def testMajorityElement(self):
    majorityElement = MajorityElement()

    print ("All test cases passed!")

def main():
  testMajorityElement = TestMajorityElement()
  testMajorityElement.testMajorityElement()

if __name__ == '__main__':
  main()


__author__ = 'qiao'

'''
The BioCPT model class
'''

import os
import torch
from torch import nn
import torch.nn.functional as F
from transformers import AutoModel, AutoConfig

class Biencoder(nn.Module):
	def __init__(self, args):
		super(Biencoder, self).__init__()
		self.args = args

		q_path = args.bert_q_path
		d_path = args.bert_d_path

		self.config_q = AutoConfig.from_pretrained(q_path)
		self.bert_q = AutoModel.from_pretrained(q_path)

		self.config_d = AutoConfig.from_pretrained(d_path)
		self.bert_d = AutoModel.from_pretrained(d_path)

	def save_pretrained(self, path):
		self.config_q.save_pretrained(os.path.join(path, 'query_encoder'))
		self.bert_q.save_pretrained(os.path.join(path, 'query_encoder'))

		self.config_d.save_pretrained(os.path.join(path, 'doc_encoder'))
		self.bert_d.save_pretrained(os.path.join(path, 'doc_encoder'))

	def forward(self, q_input_ids, q_token_type_ids, q_attention_mask,
					d_input_ids, d_token_type_ids, d_attention_mask, weights):
		embed_q = self.bert_q(input_ids=q_input_ids,
							  attention_mask=q_attention_mask,
							  token_type_ids=q_token_type_ids).last_hidden_state[:, 0, :] # B x D

		embed_d = self.bert_d(input_ids=d_input_ids,
							  attention_mask=d_attention_mask,
							  token_type_ids=d_token_type_ids).last_hidden_state[:, 0, :] # B x D

		B = embed_q.size(dim=0)
		qd_scores = torch.matmul(embed_q, torch.transpose(embed_d, 1, 0)) # B x B

		# q to d softmax
		q2d_softmax = F.log_softmax(qd_scores, dim=1)

		# d to q softmax
		d2q_softmax = F.log_softmax(qd_scores, dim=0)

		# positive indices (diagonal)
		pos_inds = torch.tensor(list(range(B)), dtype=torch.long).to(self.args.device)

		q2d_loss = F.nll_loss(q2d_softmax,
			pos_inds,
			weight=weights,
			reduction="mean"
		)

		d2q_loss = F.nll_loss(d2q_softmax,
			pos_inds,
			weight=weights,
			reduction="mean"
		)

		loss = self.args.alpha * q2d_loss + (1 - self.args.alpha) * d2q_loss

		return loss


import cv2
import numpy as np

img = cv2.imread(r"D:\Intern\test images\test13.jpg")
#upstate = cv2.resize(upstate,(250,400))
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
cv2.imshow("Original",img)
lower = np.array([0, 48, 80], dtype=np.uint8)
upper = np.array([20, 255, 255], dtype=np.uint8)
# get mask of pixels that are in blue range
img_hsv = cv2.GaussianBlur(img_hsv, (13,13), 0)
mask = cv2.inRange(img_hsv, lower, upper)

cv2.imshow("mask",cv2.cvtColor(mask, cv2.COLOR_GRAY2RGB))

# convert single channel mask back into 3 channels
mask_rgb = cv2.cvtColor(mask, cv2.COLOR_GRAY2RGB)

# perform bitwise and on mask to obtain cut-out image that is not blue
masked_img = cv2.bitwise_and(img, mask_rgb)
cv2.imshow(" masked image",masked_img)
cv2.waitKey(0)
cv2.destroyAllWindows()


from .models import *

def create_address():
    f = open('address.csv', 'r', encoding='utf-8')
    for line in f:
        _, city, _, district, _, ward = line.split(',')
        city = city.strip()
        district = district.strip()
        ward = ward.strip()
        try:
            city = City.objects.get(name=city)
        except:
            city = City.objects.create(name=city)
        try:
            district = District.objects.get(name=district)
        except:
            district = District.objects.create(name=district, city=city)
        try:
            ward = Ward.objects.get(name=ward)
        except:
            ward = Ward.objects.create(name=ward, district=district)
    f.close()

def get_cities(request=None):
    return [{
        "id": city.id,
        "name": city.name
    } for city in City.objects.all()]

def get_districts(request):
    city = request.GET.get('city')
    return [{
        "id": district.id,
        "name": district.name
    } for district in District.objects.filter(city_id=city)]

def get_wards(request):
    district = request.GET.get('district')
    return [{
        "id": ward.id,
        "name": ward.name
    } for ward in Ward.objects.filter(district_id=district)]


from singleFile import *
from data_transform import data_transform
from scipy import stats
from sklearn.ensemble import ExtraTreesClassifier

def make_predictions(video_path, fps):

    data = singleExtract(video_path=video_path, fps=fps)

    print('Coordinates Extracted..')

    data = data_transform(data, 3, 3)

    print('Data Transformed..')

    # Loading the model
    model = ExtraTreesClassifier(n_estimators=500)
    model.load_model('xgb_33.json')

    print('Model Loaded..')

    result = model.predict(data)

    print(len(result))

    correct_labels = {
        0:'aerobic',
        1:'balance_stability',
        2:'calisthenics',
        3:'coordination_agility',
        4:'flexibility',
        5:'idle',
        6:'weight_bearing',
        7:'weightlifting'
    }

    result = list(result)

    for index,x in enumerate(result):
        result[index] = correct_labels[x]

    data['target'] = result

    print('Predictions ready for RepNet')

    return data


from aws_cdk import (
    Stack,
    Duration,
    aws_lambda as lambda_,
    aws_iam as iam,
    aws_dynamodb as dynamodb,
    aws_logs as logs,
)
from constructs import Construct

def resource_name(resource_type: str) -> str:
    return f"{resource_type}_LambdaEventSample_cdk"

class LambdaEventSampleStack(Stack):

    def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)

        role = iam.Role(
            self, resource_name("rol"),
            assumed_by=iam.ServicePrincipal("lambda.amazonaws.com"),
            managed_policies=[
                iam.ManagedPolicy.from_aws_managed_policy_name(
                    "service-role/AWSLambdaBasicExecutionRole")
            ],
            role_name=resource_name("rol"),
        )

        fn = lambda_.Function(
            self, resource_name("lmd"),
            code=lambda_.AssetCode.from_asset("src"),
            handler="lambda_function.lambda_handler",
            runtime=lambda_.Runtime.PYTHON_3_9,
            function_name=resource_name("lmd"),
            timeout=Duration.seconds(30),
            memory_size=256,
            role=role
        )

        table = dynamodb.Table(
            self, resource_name("dyn"),
            billing_mode=dynamodb.BillingMode.PROVISIONED,
            partition_key=dynamodb.Attribute(
                name="pkey", type=dynamodb.AttributeType.STRING),
            read_capacity=1,
            write_capacity=1,
            table_name=resource_name("dyn")
        )
        table.grant_read_data(role)
        fn.add_environment(
            key="DB_NAME",
            value=table.table_name,
        )

        loggroup_name = f"/aws/lambda/{fn.function_name}"
        logs.LogGroup(
            self, resource_name("log"),
            log_group_name=loggroup_name,
            retention=logs.RetentionDays.ONE_DAY,
        )


class Solver(object):
    def __init__(self, mingradnorm=1e-6, maxiter=1000, maxtime=1000,
                 minstepsize=1e-10, verbosity=2):
        """
        Generic solver base class.
        Variable attributes (defaults in brackets):
            - maxiter (1000)
                Max number of iterations to run.
            - maxtime (1000)
                Max time (in seconds) to run.
            - mingradnorm (1e-6)
                Terminate if the norm of the gradient is below this.
            - minstepsize (1e-10)
                Terminate if linesearch returns a vector whose norm is below
                this.
            - verbosity (2)
                Level of information printed by the solver while it operates, 0
                is silent, 2 is most information.
        """
        self._mingradnorm = mingradnorm
        self._maxiter = maxiter
        self._maxtime = maxtime
        self._minstepsize = minstepsize
        self._verbosity = verbosity


"""empty message

Revision ID: 1b3472d30676
Revises: 94a0e20935c5
Create Date: 2019-11-21 17:10:55.069668

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '1b3472d30676'
down_revision = '94a0e20935c5'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('credit_card',
    sa.Column('customer_ID', sa.Integer(), nullable=False),
    sa.Column('card_number', sa.Integer(), nullable=False),
    sa.Column('street_address', sa.String(), nullable=True),
    sa.Column('city', sa.String(), nullable=True),
    sa.Column('state', sa.String(), nullable=True),
    sa.Column('zip_code', sa.Integer(), nullable=True),
    sa.ForeignKeyConstraint(['customer_ID'], ['customer.customer_ID'], ),
    sa.PrimaryKeyConstraint('customer_ID', 'card_number')
    )
    op.create_table('customer_address',
    sa.Column('customer_ID', sa.Integer(), nullable=False),
    sa.Column('street_address', sa.String(), nullable=True),
    sa.Column('city', sa.String(), nullable=True),
    sa.Column('state', sa.String(), nullable=True),
    sa.Column('zip_code', sa.Integer(), nullable=True),
    sa.ForeignKeyConstraint(['customer_ID'], ['customer.customer_ID'], ),
    sa.PrimaryKeyConstraint('customer_ID')
    )
    op.create_table('pricing',
    sa.Column('product_ID', sa.Integer(), nullable=False),
    sa.Column('state', sa.String(), nullable=True),
    sa.Column('price', sa.Numeric(), nullable=True),
    sa.ForeignKeyConstraint(['product_ID'], ['customer.customer_ID'], ),
    sa.PrimaryKeyConstraint('product_ID')
    )
    op.add_column('test', sa.Column('name', sa.String(), nullable=True))
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column('test', 'name')
    op.drop_table('pricing')
    op.drop_table('customer_address')
    op.drop_table('credit_card')
    # ### end Alembic commands ###


# -*- coding: utf-8 -*-
from odoo import models, fields, api
from odoo.exceptions import UserError
import xlrd,base64,datetime

class ImportWizard(models.TransientModel):
    _inherit = 'import.wizard'

    # 数据导入
    @api.multi
    def import_excel(self):
        if self.data:
            excel_obj = xlrd.open_workbook(file_contents=base64.decodestring(self.data))
            sheets = excel_obj.sheets()
            upc_obj = self.env['b2b.upc.list']
            product_obj = self.env['product.product']
            for sh in sheets:
                for row in range(1, sh.nrows):
                    code = sh.cell(row, 0).value
                    code = code.replace(' ', '')
                    if type(code) is not unicode:
                        raise UserError(u'%s 编码必须为文本类型，不能为数字格式' % code)
                    result = upc_obj.sudo().search([('name', '=', code)])
                    if result:
                        continue
                    product = product_obj.sudo().search([('barcode', '=', code)])
                    if product:
                        continue
                    upc_obj.create({'name':code})
                    # code.assign_upc_codes()
        return {'name': u'UPC码',
                'type': 'ir.actions.act_window',
                'view_type': 'form',
                'view_mode': 'tree',
                'res_model': 'b2b.upc.list',
                'context': {'create': False},
                }


import telebot
from telebot import types
import pymorphy2

bot = telebot.TeleBot('тут должен быть токен')

@bot.message_handler(commands=["start"])
def start(m, res=False):
    bot.send_message(m.chat.id, ' Привет! Я бот для анализа текста, который ты мне пришлёшь.\nСтатистика такая:\n1. количество уникальных слов\n2. самое популярное слово (кроме союзов и предлогов)\n3. количество предложений\nДля помощи напиши /help.')

# Получение сообщений от юзера
@bot.message_handler(content_types=["text"])
def handle_text(message):
	if message.text == "/help":
		bot.send_message(message.from_user.id, "Слушай, ну вроде и так всё понятно, просто пришли мне какой нибудь текст. Я посчитаю всё и напишу тебе ответ...")

	else:
		bot.send_message(message.chat.id, 'Анализирую... ')
		res = text_analyzer(message.text)
		bot.send_message(message.chat.id, res)

def extra_parts(word, morth=pymorphy2.MorphAnalyzer()):
        return morth.parse(word)[0].tag.POS

def text_analyzer(text):
    words = text.lower().split()
    most_used = ''
    count_word = 0
    unique_words = ''
    number_of_sentences = text.count('.')+text.count('!')+text.count('?')

# удаление символов
    for word in words:
        for symbol in word:
            if symbol in '!@#$%^&*()_+-="[{]}\|/?.>,<№;:':
                word.replace(symbol, '')
    unique_words = len(words)

# удаление предлогов и союзов из списка слов
    extras = {'INTJ', 'PRCL', 'CONJ', 'PREP'}
    words = [word for word in words if extra_parts(word) not in extras]

    for word in words:
        if words.count(word) > count_word and word not in []:
            count_word = words.count(word)
            most_used = word
    return f'Статистика по тексту:\nКоличество уникальных слов - {unique_words}\nСамое популярное слово - {most_used}\nКоличество предложений - {number_of_sentences}'

bot.polling(none_stop=True, interval=0)


# CRMS replace Module
# ReplaceScript
# replaces one time strings that are unique

def NamesUnique(AllNames):
    text01 = AllNames.replace('-City Service Area (Svc. Coor. Team) ','SVAr_IDXAA,')
    text1a = text01.replace('-Animal Control District ','-ACD_IDXAB,')
    text02 = text1a.replace('-Building Inspection District ','-BI_IDXBB,')
    text03 = text02.replace('-Census Tract ','-CT_IDXCC,')
    text04 = text03.replace('-Council District ','-CD_IDXDD,')
    text05 = text04.replace('-Mapsco Page-Zone ','_IDXEE,')
    text06 = text05.replace('-Multi-Tenant Inspector ','-MT_Insp_IDXFF,')
    text07 = text06.replace('-Police Beat ','-PB_IDXGG,')
    text08 = text07.replace('-Sanitation District ','-SD_IDXHH,')
    text09 = text08.replace('-Streets Customer Service Area ','-St_CSA_IDXII,')
    text10 = text09.replace('-Garbage Pick-up Day','-GPD_IDXJJ')
    text11 = text10.replace('-Brush Collection Week ','-BCW_IDXKK,')
    text12 = text11.replace('-Fire District ','-FD_IDXLL,')
    text13 = text12.replace('-Recycling Day ','-RD_IDXMM,')
    text14 = text13.replace('-Street Maint Area ','-SMA_IDXNN,')
    text15 = text14.replace('Graffiti Abatement Request,','Graffiti Abatement Request,CCS,')
    text16 = text15.replace('Graffiti Private Property - Residential/Commercial,','Graffiti Private Property - Residential/Commercial,CCS,')
    text17 = text16.replace('Graffiti Consent Form,','Graffiti Consent Form,CCS,')
    text18 = text17.replace('Vegetation Removal Request,','Vegetation Removal Request,CCS,')
    text19 = text18.replace('Heavy Clean Request,','Heavy Clean Request,CCS,')
    text20 = text19.replace('Litter Removal Request,','Litter Removal Request,CCS,')
    text21 = text20.replace('Closure Request,','Closure Request,CCS,')
    text22 = text21.replace('STEP License,','STEP License,CCS,')
    text23 = text22.replace('Graffiti Private Property -  Apartments,','Graffiti Private Property -  Apartments,CCS,')
    text24 = text23.replace('Pool Inspection - Apartments,','Pool Inspection - Apartments,CCS,')
    text25 = text24.replace('Delinquent M/F License fee,','Delinquent M/F License fee,CCS,')
    text26 = text25.replace('Urban Rehab CAO Docket,','Urban Rehab CAO Docket,CAO,')
    text27 = text26.replace('Code Environmental Compliance,','Code Environmental Compliance,CCS,')
    text28 = text27.replace('Mow Clean Tire Removal,','Mow Clean Tire Removal,CCS,')
    text29 = text28.replace(',311 Call Center Complaint,',',311 Call Center Complaint,C311,')
    text30 = text29.replace('STEP Decal,','STEP Decal,CCS,')
    text31 = text30.replace('Sign Removal Request,','Sign Removal Request,CCS,')
    text32 = text31.replace('Wood Vendor License,','Wood Vendor License,BI,')
    return text32

# not used
def CountFiles(Process):
    count = 0
    Execut = Process.readline()
    RunExecut = Execut.next()
    while 1:
        if "End of File" in RunExecut:
            break
        else:
            count += 1
            RunExecut = Execut.next()
        continue
    return count


from math import prod

def count_find_num(primesL, limit):
    min_number = prod(primesL)
    result = set()
    result.add(min_number)
    if min_number > limit:
        return []
    old_values = {min_number}
    while min_number <= limit:
        new_values = set()
        for i in old_values:
            for n in primesL:
                mul = i * n
                new_values.add(mul)
                if mul <= limit:
                    result.add(mul)
        min_number = min(new_values)
        old_values = {i for i in new_values if i < limit}
    return [len(result), max(result)]