"""
Logging facilities

These are thin wrappers on top of Twisted's logging facilities; logs
are all directed either to stdout (if Evennia is running in
interactive mode) or to $GAME_DIR/server/logs.

The log_file() function uses its own threading system to log to
arbitrary files in $GAME_DIR/server/logs.

Note: All logging functions have two aliases, log_type() and
log_typemsg(). This is for historical, back-compatible reasons.

"""

from __future__ import division

import os
import time
from datetime import datetime
from traceback import format_exc
from twisted.python import log
from twisted.internet.threads import deferToThread

_LOGDIR = None
_TIMEZONE = None

def timeformat(when=None):
    """
    This helper function will format the current time in the same
    way as twisted's logger does, including time zone info.

    Args:
        when (int, optional): This is a time in POSIX seconds on the form
            given by time.time(). If not given, this function will
            use the current time.

    Returns:
        timestring (str): A formatted string of the given time.
    """
    when = when if when else time.time()

    # time zone offset: UTC - the actual offset
    tz_offset = datetime.utcfromtimestamp(when) - datetime.fromtimestamp(when)
    tz_offset = tz_offset.days * 86400 + tz_offset.seconds
    # correct given time to utc
    when = datetime.utcfromtimestamp(when - tz_offset)
    tz_hour = abs(int(tz_offset // 3600))
    tz_mins = abs(int(tz_offset // 60 % 60))
    tz_sign = "-" if tz_offset >= 0 else "+"

    return '%d-%02d-%02d %02d:%02d:%02d%s%02d%02d' % (
        when.year, when.month, when.day,
        when.hour, when.minute, when.second,
        tz_sign, tz_hour, tz_mins)

def log_trace(errmsg=None):
    """
    Log a traceback to the log. This should be called from within an
    exception.

    Args:
        errmsg (str, optional): Adds an extra line with added info
            at the end of the traceback in the log.

    """
    tracestring = format_exc()
    try:
        if tracestring:
            for line in tracestring.splitlines():
                log.msg('[::] %s' % line)
        if errmsg:
            try:
                errmsg = str(errmsg)
            except Exception as e:
                errmsg = str(e)
            for line in errmsg.splitlines():
                log.msg('[EE] %s' % line)
    except Exception:
        log.msg('[EE] %s' % errmsg)
log_tracemsg = log_trace

def log_err(errmsg):
    """
    Prints/logs an error message to the server log.

    Args:
        errormsg (str): The message to be logged.

    """
    try:
        errmsg = str(errmsg)
    except Exception as e:
        errmsg = str(e)
    for line in errmsg.splitlines():
        log.msg('[EE] %s' % line)
    #log.err('ERROR: %s' % (errormsg,))
log_errmsg = log_err

def log_warn(warnmsg):
    """
    Prints/logs any warnings that aren't critical but should be noted.

    Args:
        warnmsg (str): The message to be logged.

    """
    try:
        warnmsg = str(warnmsg)
    except Exception as e:
        warnmsg = str(e)
    for line in warnmsg.splitlines():
        log.msg('[WW] %s' % line)
    #log.msg('WARNING: %s' % (warnmsg,))
log_warnmsg = log_warn

def log_info(infomsg):
    """
    Prints any generic debugging/informative info that should appear in the log.

    infomsg: (string) The message to be logged.
    """
    try:
        infomsg = str(infomsg)
    except Exception as e:
        infomsg = str(e)
    for line in infomsg.splitlines():
        log.msg('[..] %s' % line)
log_infomsg = log_info

def log_dep(depmsg):
    """
    Prints a deprecation message.

    Args:
        depmsg (str): The deprecation message to log.
    """
    try:
        depmsg = str(depmsg)
    except Exception as e:
        depmsg = str(e)
    for line in depmsg.splitlines():
        log.msg('[DP] %s' % line)
log_depmsg = log_dep

# Arbitrary file logger

LOG_FILE_HANDLES = {} # holds open log handles

def log_file(msg, filename="game.log"):
    """
    Arbitrary file logger using threads.

    Args:
        filename (str, optional): Defaults to 'game.log'. All logs
            will appear in the logs directory and log entries will start
            on new lines following datetime info.

    """
    global LOG_FILE_HANDLES, _LOGDIR, _TIMEZONE

    if not _LOGDIR:
        from django.conf import settings
        _LOGDIR = settings.LOG_DIR
    if not _TIMEZONE:
        from django.utils import timezone as _TIMEZONE

    def callback(filehandle, msg):
        "Writing to file and flushing result"
        msg = "\n%s [-] %s" % (_TIMEZONE.now(), msg.strip())
        filehandle.write(msg)
        # since we don't close the handle, we need to flush
        # manually or log file won't be written to until the
        # write buffer is full.
        filehandle.flush()

    def errback(failure):
        "Catching errors to normal log"
        log_trace()

    # save to server/logs/ directory
    filename = os.path.join(_LOGDIR, filename)

    if filename in LOG_FILE_HANDLES:
        filehandle = LOG_FILE_HANDLES[filename]
    else:
        try:
            filehandle = open(filename, "a")
            LOG_FILE_HANDLES[filename] = filehandle
        except IOError:
            log_trace()
            return
    deferToThread(callback, filehandle, msg).addErrback(errback)


import pygame
from pygame.locals import *
from moving_objects import Duck, DuckCollection, Aim
from scroller import BackgroundScroller
import sys

def load_image(name, transparent=False):
    """Function that handles image loading
    Returns the image and its Rect"""
    try:
        img = pygame.image.load(name)
    except pygame.error:
        raise SystemExit("Could not load image " + name)
    if not transparent:
        img = img.convert()
    img = img.convert_alpha()
    img_rect = img.get_rect()

    return img, img_rect

WIDTH = 600
HEIGHT = 300
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))

wave1, _ = load_image("bin/wave01.png", True)
wave2, _ = load_image("bin/wave02.png", True)
wave3, _ = load_image("bin/wave03.png", True)
wave4, _ = load_image("bin/wave04.png", True)
wave5, _ = load_image("bin/wave05.png", True)

duck, r = load_image("bin/duck.png", True)
DC = DuckCollection(screen, duck, r)

aim = Aim(screen, 14)
aimspeed = 2
aimXYv = [0, 0]

bg = BackgroundScroller(screen, [wave1, wave2, wave3, wave4, wave5], [4, -2, 3, -3, -1], (66, 134, 244))
clock = pygame.time.Clock()

while True:
    clock.tick(60)
    for ev in pygame.event.get():
        if ev.type == QUIT:
            pygame.quit()
            sys.exit()
        if ev.type == KEYDOWN:
            if ev.key == K_UP:
                aimXYv[1] -= aimspeed
            elif ev.key == K_DOWN:
                aimXYv[1] += aimspeed
            elif ev.key == K_LEFT:
                aimXYv[0] -= aimspeed
            elif ev.key == K_RIGHT:
                aimXYv[0] += aimspeed
        elif ev.type == KEYUP:
            if ev.key == K_UP:
                aimXYv[1] += aimspeed
            elif ev.key == K_DOWN:
                aimXYv[1] -= aimspeed
            elif ev.key == K_LEFT:
                aimXYv[0] += aimspeed
            elif ev.key == K_RIGHT:
                aimXYv[0] -= aimspeed

    if aimXYv[0] != 0 or aimXYv[1] != 0:
        aim.move(*aimXYv)

    if len(DC.ducks) <= 1:
        DC.create_duck()
        DC.create_duck()

    bg.draw()
    DC.draw()
    aim.draw()
    pygame.display.update()


import os
import zipfile

class FileCompressor(object):
    @staticmethod
    def factory(compressed_file_name, root_path):
        try:
            os.remove(compressed_file_name)
        except:
            pass
        zipf = zipfile.ZipFile(compressed_file_name, 'w')
        return FileCompressor(compressed_file_name, zipf, root_path)

    def __init__(self, filename, zipfile, root_path):
        self._filename = filename
        self._file = zipfile
        self._root_path = root_path

    @property
    def filename(self):
        return self._filename

    def add_file(self, source_path, dest_filename):
        self._file.write(
            os.path.join(self._root_path, source_path),
            dest_filename,
            zipfile.ZIP_DEFLATED
        )

    def _write_file(self, root, file, path, in_folder=''):
        self._file.write(
            os.path.join(root, file),
            os.path.join(in_folder, os.path.relpath(os.path.join(root, file), path)),
            zipfile.ZIP_DEFLATED
        )

    def add_directory(self, path, include_venv, in_folder='', add_init=False):
        for root, dirs, files in os.walk(path):
            for file in files:
                if include_venv:
                    self._write_file(root, file, path, in_folder)
                else:
                    if not 'venv' in root.split('/'):
                        self._write_file(root, file, path, in_folder)

    def close(self):
        self._file.close()


class MinCoins(object):
    '''
    Solution:
    1. Problem can be solved using recursion and has overlapping subproblems and so use dynamic programming. Recursive
        step would be based on whether the coin is chosen or not.
    2. If coin is not chosen, the value of the cell would be previous row's extract, otherwise it would be minimum of
        [previous row (same column) value, 1 + column minus coinValue (same row)]
    3. Return the last cell's value if less than infinity (amount + 1 here) else -1.
    '''
    def coinChange(self, coins, amount):
        """
        :type coins: List[int]
        :type amount: int
        :rtype: int
        """

        # O(numCoins * Amount) Time Complexity | O(numCoins * Amount) Space Complexity
        dpTable = [[None for i in range(amount + 1)] for i in range(len(coins) + 1)]
        maxValue = amount + 1

        for i in range(len(coins) + 1):
            dpTable[i][0] = 0

        for j in range(1, amount + 1):
            dpTable[0][j] = maxValue

        for rowCoin in range(1, len(coins) + 1):
            for colAmount in range(1, amount + 1):
                if colAmount < coins[rowCoin - 1]:
                    dpTable[rowCoin][colAmount] = dpTable[rowCoin - 1][colAmount]
                else:
                    dpTable[rowCoin][colAmount] = min(dpTable[rowCoin - 1][colAmount],
                                                      1 + dpTable[rowCoin][colAmount - coins[rowCoin - 1]])

        minCoins = dpTable[len(coins)][amount]
        minCoins = minCoins if minCoins < maxValue else -1

        return minCoins


import pandas as pd
import os
import copy

monthly_data_path = '/Users/charles/PycharmProjects/Games_Research/Filter_Excel_From_HTML/OUTPUT_DATA/Calendar_monthly_csv/'
monthly_data_file_list = os.listdir(monthly_data_path)
monthly_data_file_list.sort()

genre_file_path = '/Users/charles/PycharmProjects/Games_Research/IV_append/genre_excel/'
genre_data_file_list = os.listdir(genre_file_path)
genre_data_file_list.sort()

genre_list = ['Action', 'Adventure', 'Early+Access', 'Ex+Early+Access', 'Free', 'Indie', 'Massively', 'RPG',
              'Simulation', 'Sports', 'Strategy', 'Others']

# 2018-05-01 to 2019-04-30
# print(monthly_file_list[28:40])
# max_count: 666

monthly_data_file_list = monthly_data_file_list[28:40]

# print(monthly_data_file_list)
# print(genre_data_file_list)

genre_list_dic = {}
genre_statis_dic = {}

id_genre_statis_df = pd.DataFrame(columns=genre_list)

for i, _genre_file in enumerate(genre_data_file_list):
    genre_data_df = pd.read_csv(genre_file_path + _genre_file, index_col=0)['ID']
    id_list = genre_data_df.tolist()
    id_set = set(id_list)
    genre_list_dic[genre_list[i]] = id_set

# print(genre_statis_dic)

# for key in genre_list_dic.keys():
#     print(key)
#     print(len(genre_list_dic[key]))

# for i, _genre in enumerate(genre_data_file_list):
#     _genre_s= _genre.split('.', 1)[0]
#     genre_list.append(_genre_s)
#
# genre_list.append('Others')

for i, month_data in enumerate(monthly_data_file_list):

    for i, _genre_tag in enumerate(genre_list):
        genre_statis_dic[genre_list[i]] = 0

    month_data_df = pd.read_csv(monthly_data_path + month_data, index_col=0)[['ID', 'Game']]

    repeat_count = 0

    for index, row in month_data_df.iterrows():

        has_genre_count = 0

        for key in genre_list_dic.keys():

            if row['ID'] in genre_list_dic[key]:
                # print(key)
                c_count = genre_statis_dic[key]
                c_count = c_count + 1
                genre_statis_dic[key] = c_count
                has_genre_count += 1

        if has_genre_count != 0:
            repeat_count = repeat_count + has_genre_count - 1
        else:
            c_count = genre_statis_dic['Others']
            c_count = c_count + 1
            genre_statis_dic['Others'] = c_count

    print(genre_statis_dic)
    print(repeat_count)
    id_genre_statis_df = id_genre_statis_df.append(
        [{'Action': genre_statis_dic['Action'], 'Adventure': genre_statis_dic['Adventure'], 'Early+Access': genre_statis_dic['Early+Access'], 'Ex+Early+Access': genre_statis_dic['Ex+Early+Access'], 'Free': genre_statis_dic['Free'], 'Indie': genre_statis_dic['Indie'], 'Massively':genre_statis_dic['Massively'], 'RPG':genre_statis_dic['RPG'],
              'Simulation':genre_statis_dic['Simulation'], 'Sports':genre_statis_dic['Sports'], 'Strategy':genre_statis_dic['Strategy'], 'Others':genre_statis_dic['Others']}], ignore_index=True)

id_genre_statis_df.to_csv('/Users/charles/PycharmProjects/Games_Research/IV_append/genre_statistic/'+'genre_statis(12 months).csv')
print(id_genre_statis_df)


#!encoding=utf8
import service
from web.persistence.userdao import UserDAO
class UserService(service):
    def __init__(self):
        self.userDao = UserDAO()
        pass
    '''
        :returns user:查询结果，msg原因
    '''
    def login(self, user):
        user, msg = self.userDao.getUser(user.account)
        if user == None:
            return user, msg
        else:
            if user.status == 1:
                return None, "用户已登录"
            else:
                user.status = 1
                return user, "登录成功"

    def logout(self, user):
        user, msg = self.userDao.getUser(user.account)
        if user == None:
            return False, "帐号不存在"
        else:
            if user.status == 0:
                return False, "帐号未登录"
            else:
                user.status = 0
                self.userDao.update(user)
                return True, "登出成功"


'''
Remove Duplicates from List
Create a function that takes a list of items, removes all duplicate items and returns a new list in the same sequential order as the old list (minus duplicates).
Examples
["John", "Taylor", "John"] ➞ ["John", "Taylor"]

[1, 0, 1, 0] ➞ [1, 0]

['The', 'big', 'cat'] ➞ ['The', 'big', 'cat']
Notes
Test cases contain lists with both strings and numbers.
Case sensitive.
'''

def removeDups(lst):
    clean_lst = []
    [clean_lst.append(x) for x in lst if x not in clean_lst]
    return clean_lst

print(removeDups([1, 0, 1, 0]))


from numpy import array,sum,sqrt
from numpy.linalg import norm
import unittest
import os
from acousticsim.representations.gammatone import to_gammatone

from scipy.io import loadmat

from numpy.testing import assert_array_almost_equal

from matplotlib import pyplot

TEST_DIR = os.path.abspath('tests/data')

filenames = [os.path.splitext(x)[0] for x in os.listdir(TEST_DIR) if x.endswith('.wav')]

class GammatoneTest(unittest.TestCase):
    def setUp(self):
        self.num_bands = 4
        self.freq_lims = (80,7800)

    def test_gammatone(self):
        for f in filenames:
            wavpath = os.path.join(TEST_DIR,f+'.wav')
            matpath = os.path.join(TEST_DIR,f+'_gammatone_env.mat')
            if not os.path.exists(matpath):
                continue
            m = loadmat(matpath)
            bm, env = to_gammatone(wavpath, self.num_bands,self.freq_lims)
            #pyplot.plot(bm)
            #pyplot.show()
            assert_array_almost_equal(m['bm'],bm)
            assert_array_almost_equal(m['env'],env)
            #raise(ValueError)
            break

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


from .models import Order
from box.apps.sw_shop.sw_cart.utils import get_cart
from box.apps.sw_shop.sw_cart.models import CartItem
try:
  from box.apps.sw_payment.liqpay.utils import get_liqpay_context
  from box.apps.sw_payment.liqpay.models import LiqpayConfig
  print('import box.apps.sw_payment.sw_liqpay')
except:
  from sw_liqpay.utils import get_liqpay_context
  from sw_liqpay.models import LiqpayConfig
  print('import sw_liqpay')
from django.utils import timezone
from datetime import datetime

def get_order_liqpay_context(request, params={}):
  cart  = get_cart(request)
  order = Order.objects.get(
    cart=cart,
    ordered=False,
  )
  amount = 0
  for cart_item in CartItem.objects.filter(cart=cart):
    amount += cart_item.total_price
  order_id = str(order.id)
  # order_id += datetime.now().strftime("%Y-%m-%d %H:%M:%S")
  print(order_id)
  def_params = {
    'amount': amount,
    'description': str(order.comments),
    'order_id': order_id,
    'action': 'pay',
    'currency': 'UAH',
    'version': '3',
    'sandbox': int(LiqpayConfig.get_solo().sandbox_mode),
    'server_url': f'/sw_order/liqpay_callback/',
    # 'server_url': f'{Site.objects.get_current()}pay_callback/',
  }
  def_params.update(params)
  signature, data = get_liqpay_context(def_params)
  context = {
    'signature': signature,
    'data': data,
    'callback_url':'/sw_order/liqpay_callback/',
  }
  return context


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

import argparse
from pygments import highlight
from pygments.lexers import BashLexer
from pygments.formatters import TerminalFormatter
import os
import sys

ssh_tunnel_systemd = """[Unit]
Description={}
After=network-online.target

[Service]
EnvironmentFile={}
User={}
{}

# Restart every >2 seconds to avoid StartLimitInterval failure
RestartSec=5
Restart=always

[Install]
WantedBy=multi-user.target"""

def input_with_default(prompt, default=''):
    value = input(prompt + '\033[1m').strip()
    if len(value) == 0:
        value = default
    print('\033[0m', end='')
    return value

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--ssh', type=bool, required=False, default=True, help='Using SSH tunnel template')
    args = vars(parser.parse_args())

    uid = os.getuid()
    if uid != 0:
        arg = '--ssh False'
        if args['ssh'] is True:
            arg = '--ssh True'
        os.system('sudo $(which python3) \'{}\' {}'.format(os.path.join(os.getcwd(), __file__), arg))
    else:
        service_name = input_with_default('Service name: ')
        service_file_path = '/etc/systemd/system/{}.service'.format(service_name)
        description = input_with_default('Description: ')
        env = input_with_default('EnvironmentFile: ')
        user = input_with_default('User: [\033[1m${USER}\033[0m] ', '${USER}')
        if args['ssh'] is True:
            command = 'ExecStart=/usr/bin/ssh -p {} -N -o "PubkeyAuthentication=yes" -o ExitOnForwardFailure=yes -o "PasswordAuthentication=no" -o "StrictHostKeyChecking=false" -o "ServerAliveInterval 60" -o "ServerAliveCountMax 64" -R {}:{}:{}:{} -i {} {}'

            ssh_connect_port = input_with_default('SSH connect port: [\033[1m22\033[0m] ', '22')
            ssh_tunnel_ip = input_with_default('SSH tunnel remote accept IP: [\033[1m0.0.0.0\033[0m] ', '0.0.0.0')
            ssh_tunnel_remote_port = input_with_default('SSH tunnel remote port: [\033[1m${REMOTE_PORT}\033[0m] ', '${REMOTE_PORT}')
            ssh_tunnel_local_ip = input_with_default('SSH tunnel local ip: [\033[1mlocalhost\033[0m] ', 'localhost')
            ssh_tunnel_local_port = input_with_default('SSH tunnel local port: [\033[1m${LOCAL_PORT}\033[0m] ', '${LOCAL_PORT}')
            ssh_connect_key = input_with_default('SSH connect identification key: [\033[1m${SSH_IDENTIFICATION_KEY}\033[0m] ', '${SSH_IDENTIFICATION_KEY}')
            ssh_connect_host = input_with_default('SSH connect host: [\033[1mroot@${PUBLIC_SERVER}\033[0m] ', 'root@${PUBLIC_SERVER}')

            command = command.format(ssh_connect_port, ssh_tunnel_ip, ssh_tunnel_remote_port, ssh_tunnel_local_ip, ssh_tunnel_local_port, ssh_connect_key, ssh_connect_host)
        else:
            command = 'ExecStart=' + input_with_default('ExecStart: ')
        script = ssh_tunnel_systemd.format(description, env, user, command)
        print('\nWriting to\033[1m', service_file_path, '\033[0m')
        print(highlight(script, BashLexer(), TerminalFormatter()))
        confirm = input_with_default('Confirm scripts above? [\033[1mY\033[0mn]', 'y')
        if len(confirm) == 0 or confirm == 'y' or confirm == 'Y':
            with open(service_file_path, 'w') as service_fd:
                service_fd.write(script)
            print('All done, you may start the service via:')
            print('\033[1msudo systemctl enable {}.service\nsudo service {} start\033[0m'.format(service_name, service_name))


import random

print('Welcome to the Rock, Paper, Scissors game!')
print()
print('Winning Rules: \n'
      'Rock vs paper -> paper wins \n'
      'Rock vs scissor -> rock wins \n'
      'Paper vs scissors -> scissors win \n')
input('Press Enter to start. ')

your_score = 0
comp_score = 0

while True:
    print()
    your_choice = input('Rock, paper or scissors? ').lower()
    while your_choice != 'rock' and your_choice != 'paper' and your_choice != 'scissors':
        your_choice = input('Invalid input, try again: ').lower()

    random_num = random.randint(0, 2)
    if random_num == 0:
        comp_choice = 'rock'
    elif random_num == 1:
        comp_choice = 'paper'
    elif random_num == 2:
        comp_choice = 'scissors'
    else:
        comp_choice = 'rock'

    print()
    print('Your choice:', your_choice)
    print("Computer's choice:", comp_choice)
    print()

    if your_choice == 'rock':
        if comp_choice == 'rock':
            print("It's a tie!")
        elif comp_choice == 'paper':
            print('You lose!')
            comp_score += 1
        elif comp_choice == 'scissors':
            print('You win!')
            your_score += 1
    elif your_choice == 'paper':
        if comp_choice == 'rock':
            print('You win!')
            your_score += 1
        elif comp_choice == 'paper':
            print("It's a tie!")
        elif comp_choice == 'scissors':
            print('You lose!')
            comp_score += 1
    elif your_choice == 'scissors':
        if comp_choice == 'rock':
            print('You lose!')
            comp_score += 1
        elif comp_choice == 'paper':
            print('You win!')
            your_score += 1
        elif comp_choice == 'scissors':
            print("It's a tie")

    print('You have', your_score, 'wins')
    print('The computer has', comp_score, 'wins')
    print()

    play_again = input('Play again? (Y/N) ').lower()
    while play_again != 'n' and play_again != 'y':
        play_again = input('Invalid input, try again: ').lower()

    if play_again == 'n':
        break

    print('\n ............. \n')


from functools import lru_cache
def simetricen(niz):
    if niz == "":
        return True
    elif niz[0] == niz[-1]:
        return simetricen(niz[1:-1])

test = "01110"

test2 = "00101011"

@lru_cache(maxsize=None)
def stevilo_delov(w, je_simetricen):
    if w == []:
        return 0
    if je_simetricen(w):
        return 1

    options = [stevilo_delov(w[:i], je_simetricen) +
               stevilo_delov(w[i:], je_simetricen) for i in range(1, len(w))]

    return min(options)

def razdeli(niz):
    sez = []
    if niz == "":
        return sez
    lower = 0
    for i in range(len(niz)):
        if i == len(niz):
            sez.append(niz[lower:])
        if simetricen(niz[lower:i]) and not simetricen(niz[lower:i + 1]):
            sez.append(niz[lower:i])
            lower = i + 1
    return sez

def vsotno_simetricen(niz):
    n = len(niz) // 2
    if len(niz) % 2 == 0:
        leva = 0
        for i in niz[:n]:
            leva += int(i)
        desna = 0
        for j in niz[n:]:
            desna += int(j)
        return desna == leva
    else:
        leva = 0
        for i in niz[:n]:
            leva += int(i)
        desna = 0
        for j in niz[n+1:]:
            desna += int(j)
        return desna == leva

test3 = "01001000"
test4 = "1011"


import hashlib
import sys
import time
import base64

info_string = \
r"""
  _____        _   _         _____                                   _
 /  ___|      | | | |       |  ___|                                 | |
 \ `--.  _ __ | | | | _ __  | |__   _ __    ___  _ __  _   _  _ __  | |_
  `--. \| '__|| | | || '_ \ |  __| | '_ \  / __|| '__|| | | || '_ \ | __|
 /\__/ /| |   | |_| || | | || |___ | | | || (__ | |   | |_| || |_) || |_
 \____/ |_|    \___/ |_| |_|\____/ |_| |_| \___||_|    \__, || .__/  \__|
                                                       __/ || |
        Types: MD5-SHA1-SHA256-BASE64                 |___/ |_|
 ALERT: Descriptografia pode talvez ser demorada!
        \_-=> DEPENDENDO DO PROCESSADOR QUE VOCÊ VAI USAR!
        |_-=> TAMBÈM DA SUA WORDLIST!
        |_-=> OU O TIPO DE CRIPTROGRAFIA UTILIZADA!
 Desevolvido por SrBlue.
"""

print(info_string)
if len(sys.argv) < 3:
    print(" Use: unencrypt.py [type] [hash]")
    print(" Exemplo: unencrypt.py md5 21232f297a57a5a743894a0e4a801fc3 ")
    print(" TYPES: md5 sha1 sha256 base64")
    sys.exit()

tipo = str(sys.argv[1])
if tipo == "md5" or tipo == "MD5":
    type = 1
elif tipo == "sha1" or tipo == "SHA1":
    type = 2
elif tipo == "sha256" or tipo == "SHA256":
    type = 3
elif tipo == "base64" or tipo == "BASE64":
    type = 4
else:
    print(" Tipo de criptografia não disponivel.")
    print(" Tipos desponiveis: MD5 SHA1 SHA256 BASE64")
    sys.exit()

start = time.time()
un_criptution = str(sys.argv[2])
un_cript = "Nao foi encontrado na wordlist."

try:
    wordlist = open("wordlist.txt",encoding="UTF-8")
except:
    print(" Arquivo wordlist.txt não encontrado.")
    print(" porfavor adicione o arquivo no diretorio desse script.")
    sys.exit()

trans = "hash"
with wordlist as infile:
    for linha in infile:
        if type == 1:    ##md5
            trans = hashlib.md5(linha.strip().encode()).hexdigest()
        elif type == 2:  ##sha1
            trans = hashlib.sha1(linha.strip().encode()).hexdigest()
        elif type == 3:  ##sha256
            trans = hashlib.sha256(linha.strip().encode()).hexdigest()
        elif type == 4:  ##base64
            trans = un_criptution
            un_cript = base64.standard_b64decode(un_criptution)
            break
        else:
            print(" ---ERROR--- ")
            print(" ENTRE EM CONTATO COM DESEVOLVEDOR!!!!!")

        if trans == un_criptution:
            un_cript = linha
            break
    wordlist.close()

term = time.time()
fim = term - start
print("-> Tempo Ocorrido: {} | CRACK: {} | Hash: {} ".format(fim,un_cript,trans))


class Solution(object):
    def longestValidParentheses(self, s):
        """
        :type s: str
        :rtype: int
        """
        maxlen = 0
        stk = []
        pairs = {}
        continuous = 0 # left continuous length to carry over by ONLY next '('
        continueStart = 0	#starting index where the left continuous length to carry over starts
        carry = {}  # continuous length to carry over from left when "(" key got matching ")"
        for i in range(0, len(s)):
            if s[i] == '(':
                stk.append(i)
                carry[i] = continuous
                continuous = 0
                continueStart = i - 1
            if s[i] == ')':
                if len(stk) == 0:
                    continuous = 0
                    continueStart = i + 1
                    continue
                p = stk.pop()
                pairs[p] = i
                cur = i - p + 1 + carry[p]
                if continueStart < p:
                    continuous = continuous + cur
                else:
                    continuous = cur
                    continueStart = p
                maxlen = max(maxlen, cur)
        return maxlen

# doodle each variable as a row, doodle each parrenthese as a column, so that we mimic debugger step-over with pencil and brain


from openpyxl import Workbook

wb = Workbook()

ws = wb.get_active_sheet()
print (ws.title)
ws.title = 'SheetA'

ws['A1'].value = 4
ws.cell(row=3, column=1).value = 6

new_ws = wb.create_sheet(title='sheetB')

for r in range(100):
    for col in range(10):
        new_ws.cell( row =r ,  column = col).value = r + col

wb.save(filename='new_file.xlsx')


from Parameter import*
#基本界面绘制,包括棋盘,线条,棋子
class game_draw():#建立游戏绘制对象
    def __init__(self):
        self.start = start
        self.line_color = line_color
        self.line_wide = line_wide
        self.grid_wide = grid_wide
        self.th = th
        self.basic_color = basic_color
        self.screen=screen
        self.radius=radius
        self.touch_sensitive=touch_sensitive#点击敏感度,越小敏感度越高,最大值设为17合适,否则容易引起误触
        self.chessraius=chessraius#棋子直径
        self.font_size = font_size # 字体大小
        self.ABC_list = ABC_list
        self.compensate=compensate

        self.board=board
        self.black=black
        self.white=white
        self.font=font

    def draw_line(self,start_pos,end_pos):
        pygame.draw.aaline(self.screen, self.line_color, start_pos,end_pos,self.line_wide)
    def draw_circle(self,circle_centre):
        pygame.draw.circle(self.screen, self.basic_color, circle_centre, self.radius)
    def generate_font(self,string):
        return self.font.render(string, True, self.basic_color)
    def change_absolute(self,xy):#将点击绝对坐标换算成精确相对坐标
        result=["a","b"]
        for i in range(19):
            abosolute_point=self.start+i*self.grid_wide#计算出棋盘点的绝对坐标
            if(abs(xy[0]-abosolute_point)<=self.touch_sensitive):
                result[0]=i
            if(abs(xy[1]-abosolute_point)<=self.touch_sensitive):
                result[1]=i
        if(result[0]=="a" or result[1]=="b"):
            return None
        return result
    def change_relative(self,absolutepoint):#将相对坐标换算成绝对坐标
        x=absolutepoint[0]*self.grid_wide+start-self.chessraius/2
        y=absolutepoint[1]*self.grid_wide+start-self.chessraius/2
        return [x,y]
    def basic_draw(self):
        screen.blit(self.board, [0, 0])  # 绘制底部木纹条理
        for i in range(0, self.th+1):
            add = i * self.grid_wide
            self.draw_line((self.start + add, self.start), (self.start + add, self.start + self.grid_wide * self.th))  # 绘制竖线
            self.draw_line((self.start, self.start + add), (self.start + self.grid_wide * self.th, self.start + add))  # 绘制横线
            text_number = self.generate_font(str(i + 1))
            self.screen.blit(text_number, (self.start + add, self.start-self.font_size))  # 绘制数字
            letter = self.ABC_list[i]
            text_letter=self.generate_font(letter)
            screen.blit(text_letter, (self.start - self.font_size, self.start + add - self.compensate))  # 绘制字母
        if (self.th == 14):
            self.draw_circle((self.start + 3 * self.grid_wide, self.start + 3 * self.grid_wide))  # 仅当是标准棋盘时候绘制棋盘圆点
            self.draw_circle((self.start + 11 * self.grid_wide, self.start + 3 * self.grid_wide))
            self.draw_circle((self.start + 7 * self.grid_wide, self.start + 7 * self.grid_wide))
            self.draw_circle((self.start + 3 * self.grid_wide, self.start + 11 * self.grid_wide))
            self.draw_circle((self.start + 11 * self.grid_wide, self.start + 11 * self.grid_wide))
    def chess_draw(self,matrix):
        for i in range(th + 1):  # 遍历游戏信息矩阵绘制棋子
            for j in range(th + 1):
                if (matrix[i][j] == 1):
                    screen.blit(black, self.change_relative([i, j]))
                elif (matrix[i][j] == -1):
                    screen.blit(white, self.change_relative([i, j]))


# -*- coding: utf-8 -*-
import math
n=int(input('digite n:'))
if n<0:
    n=(-1)*n
soma=0
termo=1
numerador=1
denominador=n
while termo <=n:
    soma=soma+denominador/numerador
    termo=termo+1
    denominador=denominador-1
print('%.5f'%soma)


r"""Functional interface"""
import math
import torch
from torch import Tensor
from typing import List, Optional
import scipy
import numpy as np
from scipy import linalg
import random

# TODO: use foreach API in optim._functional to do all the computation

def get_beta(v, std = 1e-3):

    v_np = v.detach().cpu().numpy()[0:1] # we take one gradient vector - they all point to the same direction
    ns_basis = scipy.linalg.null_space(v_np)
    mean = np.zeros(v.shape[1])
    noise = np.random.normal(0, std, v.shape)
    ns_noise = noise @ ns_basis@ns_basis.T
    beta = torch.tensor(ns_noise) - v
    assert beta.shape == v.shape
    return beta

def _make_sparse(grad, grad_indices, values):
    size = grad.size()
    if grad_indices.numel() == 0 or values.numel() == 0:
        return torch.empty_like(grad)
    return torch.sparse_coo_tensor(grad_indices, values, size)

def adagrad(params: List[Tensor],
            grads: List[Tensor],
            state_sums: List[Tensor],
            state_steps: List[int],
            *,
            lr: float,
            weight_decay: float,
            lr_decay: float,
            eps: float):
    r"""Functional API that performs Adagrad algorithm computation.
    See :class:`~torch.optim.Adagrad` for details.
    """

    for (param, grad, state_sum, step) in zip(params, grads, state_sums, state_steps):
        if weight_decay != 0:
            if grad.is_sparse:
                raise RuntimeError("weight_decay option is not compatible with sparse gradients")
            grad = grad.add(param, alpha=weight_decay)

        clr = lr / (1 + (step - 1) * lr_decay)

        if grad.is_sparse:
            grad = grad.coalesce()  # the update is non-linear so indices must be unique
            grad_indices = grad._indices()
            grad_values = grad._values()
            size = grad.size()

            state_sum.add_(_make_sparse(grad, grad_indices, grad_values.pow(2)))
            std = state_sum.sparse_mask(grad)
            std_values = std._values().sqrt_().add_(eps)
            param.add_(_make_sparse(grad, grad_indices, grad_values / std_values), alpha=-clr)
        else:
            state_sum.addcmul_(grad, grad, value=1)
            std = state_sum.sqrt().add_(eps)
            param.addcdiv_(grad, std, value=-clr)

def adam(params: List[Tensor],
         grads: List[Tensor],
         exp_avgs: List[Tensor],
         exp_avg_sqs: List[Tensor],
         max_exp_avg_sqs: List[Tensor],
         state_steps: List[int],
         *,
         amsgrad: bool,
         beta1: float,
         beta2: float,
         lr: float,
         weight_decay: float,
         eps: float):
    r"""Functional API that performs Adam algorithm computation.
    See :class:`~torch.optim.Adam` for details.
    """

    for i, param in enumerate(params):

        grad = grads[i]
        exp_avg = exp_avgs[i]
        exp_avg_sq = exp_avg_sqs[i]
        step = state_steps[i]

        bias_correction1 = 1 - beta1 ** step
        bias_correction2 = 1 - beta2 ** step

        if weight_decay != 0:
            grad = grad.add(param, alpha=weight_decay)

        # Decay the first and second moment running average coefficient
        exp_avg.mul_(beta1).add_(grad, alpha=1 - beta1)
        exp_avg_sq.mul_(beta2).addcmul_(grad, grad, value=1 - beta2)
        if amsgrad:
            # Maintains the maximum of all 2nd moment running avg. till now
            torch.maximum(max_exp_avg_sqs[i], exp_avg_sq, out=max_exp_avg_sqs[i])
            # Use the max. for normalizing running avg. of gradient
            denom = (max_exp_avg_sqs[i].sqrt() / math.sqrt(bias_correction2)).add_(eps)
        else:
            denom = (exp_avg_sq.sqrt() / math.sqrt(bias_correction2)).add_(eps)

        step_size = lr / bias_correction1

        param.addcdiv_(exp_avg, denom, value=-step_size)

def adamw(params: List[Tensor],
          grads: List[Tensor],
          exp_avgs: List[Tensor],
          exp_avg_sqs: List[Tensor],
          max_exp_avg_sqs: List[Tensor],
          state_steps: List[int],
          *,
          amsgrad: bool,
          beta1: float,
          beta2: float,
          lr: float,
          weight_decay: float,
          eps: float):
    r"""Functional API that performs AdamW algorithm computation.
    See :class:`~torch.optim.AdamW` for details.
    """
    for i, param in enumerate(params):
        grad = grads[i]
        exp_avg = exp_avgs[i]
        exp_avg_sq = exp_avg_sqs[i]
        step = state_steps[i]

        # Perform stepweight decay
        param.mul_(1 - lr * weight_decay)

        bias_correction1 = 1 - beta1 ** step
        bias_correction2 = 1 - beta2 ** step

        # Decay the first and second moment running average coefficient
        exp_avg.mul_(beta1).add_(grad, alpha=1 - beta1)
        exp_avg_sq.mul_(beta2).addcmul_(grad, grad, value=1 - beta2)
        if amsgrad:
            # Maintains the maximum of all 2nd moment running avg. till now
            torch.maximum(max_exp_avg_sqs[i], exp_avg_sq, out=max_exp_avg_sqs[i])
            # Use the max. for normalizing running avg. of gradient
            denom = (max_exp_avg_sqs[i].sqrt() / math.sqrt(bias_correction2)).add_(eps)
        else:
            denom = (exp_avg_sq.sqrt() / math.sqrt(bias_correction2)).add_(eps)

        step_size = lr / bias_correction1

        param.addcdiv_(exp_avg, denom, value=-step_size)

def sgd(params: List[Tensor],
        d_p_list: List[Tensor],
        momentum_buffer_list: List[Optional[Tensor]],
        *,
        weight_decay: float,
        momentum: float,
        lr: float,
        dampening: float,
        nesterov: bool,
        add_noise=False):
    r"""Functional API that performs SGD algorithm computation.
    See :class:`~torch.optim.SGD` for details.
    """

    for i, param in enumerate(params):

        d_p = d_p_list[i]
        if weight_decay != 0:
            d_p = d_p.add(param, alpha=weight_decay)

        if momentum != 0:
            buf = momentum_buffer_list[i]

            if buf is None:
                buf = torch.clone(d_p).detach()
                momentum_buffer_list[i] = buf
            else:
                buf.mul_(momentum).add_(d_p, alpha=1 - dampening)

            if nesterov:
                d_p = d_p.add(buf, alpha=momentum)
            else:
                d_p = buf

        if (not add_noise) or ( len(param.shape) == 1 ):
            param.add_(d_p, alpha=-lr)
        else:
            with torch.no_grad():
                v = -lr * d_p
                beta = get_beta(v)
                param.add_(beta)

def adadelta(params: List[Tensor],
             grads: List[Tensor],
             square_avgs: List[Tensor],
             acc_deltas: List[Tensor],
             *,
             lr: float,
             rho: float,
             eps: float,
             weight_decay: float):
    r"""Functional API that performs Adadelta algorithm computation.
    See :class:`~torch.optim.Adadelta` for details.
    """

    for (param, grad, square_avg, acc_delta) in zip(params, grads, square_avgs, acc_deltas):
        if weight_decay != 0:
            grad = grad.add(param, alpha=weight_decay)

        square_avg.mul_(rho).addcmul_(grad, grad, value=1 - rho)
        std = square_avg.add(eps).sqrt_()
        delta = acc_delta.add(eps).sqrt_().div_(std).mul_(grad)
        param.add_(delta, alpha=-lr)
        acc_delta.mul_(rho).addcmul_(delta, delta, value=1 - rho)

def rmsprop(params: List[Tensor],
            grads: List[Tensor],
            square_avgs: List[Tensor],
            grad_avgs: List[Tensor],
            momentum_buffer_list: List[Tensor],
            *,
            lr: float,
            alpha: float,
            eps: float,
            weight_decay: float,
            momentum: float,
            centered: bool):
    r"""Functional API that performs rmsprop algorithm computation.
    See :class:`~torch.optim.RMSProp` for details.
    """

    for i, param in enumerate(params):
        grad = grads[i]
        square_avg = square_avgs[i]

        if weight_decay != 0:
            grad = grad.add(param, alpha=weight_decay)

        square_avg.mul_(alpha).addcmul_(grad, grad, value=1 - alpha)

        if centered:
            grad_avg = grad_avgs[i]
            grad_avg.mul_(alpha).add_(grad, alpha=1 - alpha)
            avg = square_avg.addcmul(grad_avg, grad_avg, value=-1).sqrt_().add_(eps)
        else:
            avg = square_avg.sqrt().add_(eps)

        if momentum > 0:
            buf = momentum_buffer_list[i]
            buf.mul_(momentum).addcdiv_(grad, avg)
            param.add_(buf, alpha=-lr)
        else:
            param.addcdiv_(grad, avg, value=-lr)

def rprop(params: List[Tensor],
          grads: List[Tensor],
          prevs: List[Tensor],
          step_sizes: List[Tensor],
          *,
          step_size_min: float,
          step_size_max: float,
          etaminus: float,
          etaplus: float):
    r"""Functional API that performs rprop algorithm computation.
    See :class:`~torch.optim.Rprop` for details.
    """

    for i, param in enumerate(params):
        grad = grads[i]
        prev = prevs[i]
        step_size = step_sizes[i]

        sign = grad.mul(prev).sign()
        sign[sign.gt(0)] = etaplus
        sign[sign.lt(0)] = etaminus
        sign[sign.eq(0)] = 1

        # update stepsizes with step size updates
        step_size.mul_(sign).clamp_(step_size_min, step_size_max)

        # for dir<0, dfdx=0
        # for dir>=0 dfdx=dfdx
        grad = grad.clone(memory_format=torch.preserve_format)
        grad[sign.eq(etaminus)] = 0

        # update parameters
        param.addcmul_(grad.sign(), step_size, value=-1)

        prev.copy_(grad)

def adamax(params: List[Tensor],
           grads: List[Tensor],
           exp_avgs: List[Tensor],
           exp_infs: List[Tensor],
           state_steps: List[int],
           *,
           eps: float,
           beta1: float,
           beta2: float,
           lr: float,
           weight_decay: float):
    r"""Functional API that performs adamax algorithm computation.
    See :class:`~torch.optim.Adamax` for details.
    """

    for i, param in enumerate(params):
        grad = grads[i]
        exp_avg = exp_avgs[i]
        exp_inf = exp_infs[i]
        step = state_steps[i]

        if weight_decay != 0:
            grad = grad.add(param, alpha=weight_decay)

        # Update biased first moment estimate.
        exp_avg.mul_(beta1).add_(grad, alpha=1 - beta1)
        # Update the exponentially weighted infinity norm.
        norm_buf = torch.cat([
            exp_inf.mul_(beta2).unsqueeze(0),
            grad.abs().add_(eps).unsqueeze_(0)
        ], 0)
        torch.amax(norm_buf, 0, keepdim=False, out=exp_inf)

        bias_correction = 1 - beta1 ** step
        clr = lr / bias_correction

        param.addcdiv_(exp_avg, exp_inf, value=-clr)

def asgd(params: List[Tensor],
         grads: List[Tensor],
         axs: List[Tensor],
         mus: List[float],
         etas: List[float],
         *,
         weight_decay: float,
         lambd: float):
    r"""Functional API that performs asgd algorithm computation.
    See :class:`~torch.optim.ASGD` for details.
    """

    for i, param in enumerate(params):
        grad = grads[i]
        mu = mus[i]
        ax = axs[i]
        eta = etas[i]

        if weight_decay != 0:
            grad = grad.add(param, alpha=weight_decay)

        # decay term
        param.mul_(1 - lambd * eta)

        # update parameter
        param.add_(grad, alpha=-eta)

        # averaging
        if mu != 1:
            ax.add_(param.sub(ax).mul(mu))
        else:
            ax.copy_(param)

def nadam(params: List[Tensor],
          grads: List[Tensor],
          exp_avgs: List[Tensor],
          exp_avg_sqs: List[Tensor],
          mu_products: List[float],
          state_steps: List[int],
          *,
          beta1: float,
          beta2: float,
          lr: float,
          weight_decay: float,
          momentum_decay: float,
          eps: float):
    r"""Functional API that performs NAdam algorithm computation.
    See :class:`~torch.optim.NAdam` for details.
    """

    for i, param in enumerate(params):
        grad = grads[i]
        exp_avg = exp_avgs[i]
        exp_avg_sq = exp_avg_sqs[i]
        mu_product = mu_products[i]
        step = state_steps[i]

        bias_correction2 = 1 - beta2 ** step

        if weight_decay != 0:
            grad = grad.add(param, alpha=weight_decay)

        # calculate the momentum cache \mu^{t} and \mu^{t+1}
        mu = beta1 * (1. - 0.5 * (0.96 ** (step * momentum_decay)))
        mu_next = beta1 * (1. - 0.5 * (0.96 ** ((step + 1) * momentum_decay)))
        mu_product = mu_product * mu
        mu_product_next = mu_product * mu * mu_next

        # decay the first and second moment running average coefficient
        exp_avg.mul_(beta1).add_(grad, alpha=1 - beta1)
        exp_avg_sq.mul_(beta2).addcmul_(grad, grad, value=1 - beta2)

        denom = exp_avg_sq.div(bias_correction2).sqrt().add_(eps)
        param.addcdiv_(grad, denom, value=-lr * (1. - mu) / (1. - mu_product))
        param.addcdiv_(exp_avg, denom, value=-lr * mu_next / (1. - mu_product_next))

def radam(params: List[Tensor],
          grads: List[Tensor],
          exp_avgs: List[Tensor],
          exp_avg_sqs: List[Tensor],
          state_steps: List[int],
          *,
          beta1: float,
          beta2: float,
          lr: float,
          weight_decay: float,
          eps: float):
    r"""Functional API that performs RAdam algorithm computation.
    See :class:`~torch.optim.RAdam` for details.
    """

    for i, param in enumerate(params):
        grad = grads[i]
        exp_avg = exp_avgs[i]
        exp_avg_sq = exp_avg_sqs[i]
        step = state_steps[i]

        bias_correction1 = 1 - beta1 ** step
        bias_correction2 = 1 - beta2 ** step

        if weight_decay != 0:
            grad = grad.add(param, alpha=weight_decay)

        # Decay the first and second moment running average coefficient
        exp_avg.mul_(beta1).add_(grad, alpha=1 - beta1)
        exp_avg_sq.mul_(beta2).addcmul_(grad, grad, value=1 - beta2)

        # correcting bias for the first moving moment
        bias_corrected_exp_avg = exp_avg / bias_correction1

        # maximum length of the approximated SMA
        rho_inf = 2 / (1 - beta2) - 1
        # compute the length of the approximated SMA
        rho_t = rho_inf - 2 * step * (beta2 ** step) / bias_correction2

        if rho_t > 5.:
            # Compute the variance rectification term and update parameters accordingly
            rect = math.sqrt((rho_t - 4) * (rho_t - 2) * rho_inf / ((rho_inf - 4) * (rho_inf - 2) * rho_t))
            adaptive_lr = math.sqrt(bias_correction2) / exp_avg_sq.sqrt().add_(eps)

            param.add_(bias_corrected_exp_avg * lr * adaptive_lr * rect, alpha=-1.0)
        else:
            param.add_(bias_corrected_exp_avg * lr, alpha=-1.0)

def sparse_adam(params: List[Tensor],
                grads: List[Tensor],
                exp_avgs: List[Tensor],
                exp_avg_sqs: List[Tensor],
                state_steps: List[int],
                *,
                eps: float,
                beta1: float,
                beta2: float,
                lr: float):
    r"""Functional API that performs Sparse Adam algorithm computation.
    See :class:`~torch.optim.SparseAdam` for details.
    """
    for i, param in enumerate(params):
        grad = grads[i]
        grad = grad.coalesce()  # the update is non-linear so indices must be unique
        grad_indices = grad._indices()
        grad_values = grad._values()
        size = grad.size()

        exp_avg = exp_avgs[i]
        exp_avg_sq = exp_avg_sqs[i]
        step = state_steps[i]

        def make_sparse(values):
            constructor = grad.new
            if grad_indices.dim() == 0 or values.dim() == 0:
                return constructor().resize_as_(grad)
            return constructor(grad_indices, values, size)

        # Decay the first and second moment running average coefficient
        #      old <- b * old + (1 - b) * new
        # <==> old += (1 - b) * (new - old)
        old_exp_avg_values = exp_avg.sparse_mask(grad)._values()
        exp_avg_update_values = grad_values.sub(old_exp_avg_values).mul_(1 - beta1)
        exp_avg.add_(make_sparse(exp_avg_update_values))
        old_exp_avg_sq_values = exp_avg_sq.sparse_mask(grad)._values()
        exp_avg_sq_update_values = grad_values.pow(2).sub_(old_exp_avg_sq_values).mul_(1 - beta2)
        exp_avg_sq.add_(make_sparse(exp_avg_sq_update_values))

        # Dense addition again is intended, avoiding another sparse_mask
        numer = exp_avg_update_values.add_(old_exp_avg_values)
        exp_avg_sq_update_values.add_(old_exp_avg_sq_values)
        denom = exp_avg_sq_update_values.sqrt_().add_(eps)
        del exp_avg_update_values, exp_avg_sq_update_values

        bias_correction1 = 1 - beta1 ** step
        bias_correction2 = 1 - beta2 ** step
        step_size = lr * math.sqrt(bias_correction2) / bias_correction1

        param.add_(make_sparse(-step_size * numer.div_(denom)))


import pandas as pd
import numpy as np
from ast import literal_eval
import multiprocessing
from tensorflow import keras
from datetime import datetime
from sklearn import utils
from gensim.models.doc2vec import Doc2Vec, TaggedDocument

import model_creation
import model_training
import model_evaluation

# ---------------------- CONFIGURACIÓN ----------------------
# Posibles valores: dm, dbow
modelo = 'dbow'
epocas = 7
batch_size = 20
n_dimensions = 300
# -----------------------------------------------------------

# Lectura del dataset preprocesado
df = pd.read_csv('../dataset/ticnn_preprocessed.csv')

dm_value = 0
if modelo == 'dm':
    dm_value = 1

[print('La variante seleccionada es Distributed Memory') if dm_value == 1 else print('La variante seleccionada es Distributed Bag of Words')]

# Alteracion aleatoria del orden de las entradas
df = df.sample(frac=1)

# Convertimos la columna text a lista de strings
df['text'] = df['text'].apply(literal_eval)

# Convertimos las variables a listas
texts = list(df['text'])
targets = list(df['type'])

# Dividimos en conjunto de entrenamiento y pruebas
x_train, y_train, x_test, y_test = model_creation.train_test_split(texts, targets)

# Generamos los TaggedDocuments
train_documents = []
test_documents = []

for i in range(len(x_train)):
    train_documents.append(
        TaggedDocument(words=x_train[i], tags=[y_train[i]])
    )

for i in range(len(x_test)):
    test_documents.append(
        TaggedDocument(words= x_test[i], tags=[y_test[i]])
    )

# Creamos el modelo Doc2Vec
print('Creando el modelo Doc2Vec')
d2v_model = Doc2Vec(dm=dm_value,
                    vector_size=n_dimensions,
                    min_count=2,
                    workers=multiprocessing.cpu_count())

# Construimos el vocabulario utilizando solamente el conjunto de entrenamiento
print('Construyendo el vocabulario...')
d2v_model.build_vocab([x for x in train_documents])

# Entrenamos el modelo Doc2Vec
print('Entrenamos el modelo Doc2Vec')
train_documents = utils.shuffle(train_documents)
d2v_model.train(train_documents,
                total_examples=len(train_documents),
                epochs=10)

# Aplicamos la función vector for learning
print('Aplicando la función vector for learning... (Este paso tomará varios minutos)')
y_train, x_train = model_training.vector_for_learning(d2v_model, train_documents)
y_test, x_test = model_training.vector_for_learning(d2v_model, test_documents)

# Generamos el modelo neuronal
model = model_training.generate_d2v_dense_nn()
print(model.summary())

# Definimos la ruta en la que se guardarán los logs de TensorBoard
logdir = "logs\\d2v\\" + modelo + "\\" + datetime.now().strftime("%Y%m%d-%H%M%S")

# Definimos el callback de TensorBoard
tensorboard_callback = keras.callbacks.TensorBoard(log_dir=logdir, histogram_freq=1)

# Entrenamos el modelo
history = model.fit(np.array(x_train),
                    np.array(y_train),
                    epochs=epocas,
                    validation_data=(np.array(x_test), np.array(y_test)),
                    callbacks=[tensorboard_callback]
                    )

# Representación gráfica de los resultados
model_evaluation.plot_history(history)


# class MyClass:
#       def SayHello():
#         print("Hello there!")

# MyClass.SayHello()

# class Person:
#   def greet (self):
#     print("Hello")

# me = Person()
# me.greet()

# matt = Person()
# matt.greet()

# class MyClass:
#     Greeting = ""
#     def __init__(self):
#       print("Upon Initialization: Hello!")
#     def instance_method(self):
#       print("Hello {0}".format(self.Greeting))
#     def class_method():
#       print("Hello {0}".format(self.Greeting))

# digtalCrafts = MyClass()
# MyClass.Greeting = "Matt"
# digtalCrafts.instance_method()

# flatIron = MyClass()
# flatIron.instance_method()

# utAustin = MyClass()
# utAustin.instance_method()

# MyClass.Greeting = "Travis"
# digtalCrafts.instance_method()
# flatIron.instance_method()
# utAustin.instance_method()

# test.class_method()

# class Person:
  #global variable
#   GlobalPerson = "Zelda"
#   def __init__ (self, name):
#     self.name = name
#     print(name)
#   def greet(self, friend):
#     print("Hello {} and {} and {}".format(self.name, friend, self.GlobalPerson))

# matt = Person("Fisher")
# matt.greet("Travis")

# person1 = Person("Hussein")
# person1.greet("Skyler")

# Person.GlobalPerson = "Eric"
# matt.greet("Travis")
# person1.greet("Skyler")

class Person:
  def __init__ (self, name):
    self.name = name
    self.count = 0
  def greet (self):
    self._greet()
  def _greet (self):
    self.count += 1
    if self.count > 1:
      print("Stop being so nice")
      self.__reset_count()
    else:
      print("Hello", self.name)
  def __reset_count(self):
    self.count = 0

matt = Person("Fisher")
matt.greet()
matt.greet()
matt.greet()

travis = Person("Ramos")
travis.greet()


from psychopy.iohub.client import launchHubServer
from psychopy import core, event
io=launchHubServer()

io_buttons = []
button_clock = core.Clock()

print("PRESS BUTTON NOW.")
core.wait(10)
button_clock.reset()
while button_clock.getTime() < 5:
    buttons = io.devices.keyboard.state #check constantly
    io_buttons.append(buttons)
    print(buttons)

io.quit()

print('button presses detected ', len(io_buttons))


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

    #객체가 소멸될때
    #반드시 한번 호출된다
    def __del__(self):
        print(f'{self.data} is deleted')
class DLinkedList:
    def __init__(self):
        self.head=Node()
        self.tail=Node()
        self.d_size = 0

        self.head.next=self.tail
        self.tail.prev=self.head

    def empty(self):
        if self.d_size==0:
            return True
        return False

    def size(self):
        return self.d_size

    def add_first(self, data):
        #새로운 노드를 만들었다!!
        new_node=Node(data)

        #new_node 기준에서 연결
        new_node.prev=self.head
        new_node.next=self.head.next

        self.head.next.prev=new_node
        self.head.next=new_node

        self.d_size+=1

    def add_last(self,data):

        new_node=Node(data)

        new_node.next=self.tail
        new_node.prev=self.tail.prev

        self.tail.prev.next=new_node
        self.tail.prev=new_node

        self.d_size+=1

    def insert_after(self, data, node):

        new_node=Node(data)

        new_node.prev=node
        new_node.next=node.next

        node.next.prev=new_node
        node.next=new_node

        self.d_size+=1

    def insert_before(self, data, node):

        new_node=Node(data)

        new_node.prev=node.prev
        new_node.next=node

        node.prev.next=new_node
        node.prev=new_node

        self.d_size+=1

    def search_forward(self, target):
        cur = self.head.next

        while cur is not self.tail:
            if cur.data==target:
                return cur
            cur=cur.next

        return None

    def search_backward(self, target):
        cur = self.tail.prev

        while cur is not self.head:
            if cur.data==target:
                return cur
            cur=cur.prev

        return None

    def delete_first(self):
        if self.empty():
            return

        self.head.next=self.head.next.next
        self.head.next=self.head

        self.d_size-=1

    def delete_last(self):
        if self.empty():
            return

        self.tail.prev=self.tail.prev.prev
        self.tail.prev.next=self.tail

        self.d_size-=1

    def delete_node(self, node):
        if self.empty():
            return

        node.next.prev=node.prev
        node.prev.next=node.next

        self.d_size-=1

#제너레이터
def show_list(dlist):
    cur=dlist.head.next
    while cur is not dlist.tail:
        yield cur.data
        cur=cur.next


import serial
import time

def get_control_Arduino(code):
    ser = serial.Serial('/dev/ttyACM1',9600)
    time.sleep(2)
    ser.write(code)
    data = ser.readline()
    if code == '1':
        data = float(data.strip())
        return data
    return

#getArduinodata()


import os
import os.path
import librosa
from hparams import hparams as hp

class Stem(object):
    def __init__(self, path, targets=None):
        self.audio = path
        if targets:
            self.targets = targets

    def load(self):
        return librosa.load(self.audio, sr=hp.sample_rate)[0]

class DSD(object):
    def __init__(self, root_dir, mix_dir="Mixtures", src_dir="Sources"):
        self.root_dir = root_dir
        self.mixtures = os.path.join(root_dir, mix_dir)
        self.sources = os.path.join(root_dir, src_dir)

    def _parse(self):
        tracks = []
        mixfs = os.listdir(self.mixtures)
        srcfs = os.listdir(self.sources)
        total = len(mixfs) + 1
        for i in range(1,total):
            mixdir = next(f for f in mixfs if int(f[:3]) == i)
            srcdir = next(f for f in srcfs if int(f[:3]) == i)
            targets = {}
            mixpath = os.path.join(self.mixtures, mixdir)
            srcpath = os.path.join(self.sources, srcdir)
            mix = os.listdir(mixpath)[0]
            srcs = os.listdir(srcpath)
            for src in srcs:
                fname = src[:-4]
                stem = Stem(os.path.join(srcpath, src))
                targets[fname] = stem
            tracks.append(Stem(os.path.join(mixpath, mix), targets=targets))
        return tracks

    def load_tracks(self):
        return self._parse()


class SentinelNode:

    def __init__(self):
        self.next = self
        self.prev = self
        self.value = None

class LinkedNode:

    def __init__(self, value=None):
        self.next = None
        self.value = value

class SinglyLinkedList:

    def __init__(self):
        self.sentinel = SentinelNode()

    def mass_insert(self, value_list):
        for i in xrange(len(value_list)-1, -1, -1):
            self.insert(LinkedNode(value_list[i]))

    def insert(self, new_node):
        new_node.next = self.sentinel.next
        self.sentinel.next = new_node

    def search(self, value):
        walker = self.sentinel.next
        while walker != self.sentinel:
            if value == walker.value:
                return walker

            walker = walker.next

        return None

    def delete(self, node):
        self.sentinel.next =  node.next

    def pop(self):
        popping = self.sentinel.next
        self.sentinel.next = popping.next
        return popping

    def to_array(self):
        result = []
        walker = self.sentinel.next
        while walker != self.sentinel:
            result.append(walker.value)
            walker = walker.next

        return result

if __name__ == '__main__':
    sl = SinglyLinkedList()
    sl.insert(LinkedNode(1))
    sl.mass_insert([3, 4, 5, 5, 6, 4, 1])
    assert sl.to_array() == [1, 4, 6, 5, 5, 4, 3, 1]
    assert sl.sentinel.next.value == 1
    assert sl.pop().value == 1
    assert sl.to_array() == [4, 6, 5, 5, 4, 3, 1]
    assert sl.search('nope') == None
    assert sl.search(5).value == 5
    sl.delete(sl.search(4))
    assert sl.to_array() == [6, 5, 5, 4, 3, 1]


#!/usr/bin/env python2
from __future__ import print_function
# Insecure Shell 2 hacker - Hackover 2016
# Thom Wiggers <thom@thomwiggers.nl>

import socket
import os
from binascii import hexlify
import struct

def send(s, data):
    try:
        s.send(struct.pack('!h', len(data)))
        s.send(data)
    except:
        os.kill(os.getpid(), 9)

def recv(s):
    try:
        l = struct.unpack('!h', s.recv(2))[0]
        data = s.recv(l)
    except:
        os.kill(os.getpid(), 9)
    return data

def connect(host, port):
    nonce_len = len("Nonce:") + 32
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    s.setblocking(1)
    s.connect((host, port))
    t = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    t.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    t.setblocking(1)
    t.connect((host, port))
    print("connected")
    # no longer needed to do this
    nonce = "Nonce:;echo hi;                       "
    assert len(nonce) == nonce_len, "{} != {}".format(len(nonce), nonce_len)
    print("Sending challenge")
    print("challenge: %s" % nonce)
    send(s, nonce)
    enccmd = recv(s)
    print("Encrypted response: {}".format(hexlify(enccmd)))
    send(s, "ok")
    challenge = recv(s)
    print("Being challenged by s: {}".format(challenge))
    print("Sending challenge to t")
    send(t, challenge)
    encnonce = recv(t)
    print("Getting response from t {}".format(hexlify(encnonce)))
    print("Sending response from t to s")
    send(s, encnonce)
    print("We should now get ok")
    okmsg = recv(s)
    assert okmsg == "ok"
    print("We got ok")
    print("Sending commands to s")
    send(s, "get-flag")
    print(recv(s))

if __name__ == "__main__":
    print("client")
    connect("challenges.hackover.h4q.it", 40804)
    # connect('localhost', 40804)


import random

class cresource:
    def __init__(self, id, name="name"):
        self.__id = id
        self.name = "name"
    def getResourceID(self):
        print (self.__id)

#vid = cresource(24)
#print(vid.getResourceID())

def flush_batch():
	open('takenf.txt', 'w').close()

def assignID(option="res"):
	taken = [line.strip() for line in open("takenf.txt", 'r')]
	tryid = str(random.randint(1,1000))

	while(tryid in list(taken)):
		tryid = str(random.randint(1,1000))

	taken.append(tryid)
	print("Assigned RID:",tryid)
	takenf= open("takenf.txt",'w')  #later, make persistent
	for ele in taken:
		takenf.write(str(ele) + "\n")

	takenf.close()

assignID()
#print("Yakkow, please give 7")


import logging
from inspect import isclass

from qtpy.QtCore import Signal, Property, Slot

import Devices
from Devices import IDS_ueye

from .app_interface_object import AppInterfaceObject

logger = logging.getLogger('UEyeDevices')

class UEyeDevices(AppInterfaceObject):
    devicesChanged = Signal()

    def __init__(self, parent=None):
        super(UEyeDevices, self).__init__(parent)

        self._devices = []

    @Slot()
    def refresh(self):
        if not self._app_interface:
            logger.debug("Cannot refresh without an appInterface")
            return

        devices = []
        data = self._app_interface.app_image
        for name, device in data.get('devices', dict()).items():
            type_ = device['type'].value
            class_ = getattr(Devices, type_, None)
            if isclass(class_) and issubclass(class_, IDS_ueye):
                devices.append(name)

        self._devices = devices
        self.devicesChanged.emit()

    @Property(list, notify=devicesChanged)
    def devices(self):
        return self._devices


from django import forms
from hotel.child_supplement_policy.models import ChildSupplementPolicy
from hotel.inventory.models import HotelInventory
from hotel.models import Hotels

UNIT = [
	('per child per night', 'per child per night'),
	('per child per stay', 'per child per stay'),
]

CATEGORY = [
	('From', 'From'),
	('Up To', 'Up To'),
]

PRICE = [
	('Free', 'Free'),
	('Percentage', 'Percentage'),
]

AGE = [
	('0', '0'),
	('1', '1'),
	('2', '2'),
	('3', '3'),
	('4', '4'),
	('5', '5'),
	('6', '6'),
	('7', '7'),
	('8', '8'),
	('9', '9'),
	('10', '10'),
	('11', '11'),
	('12', '12'),
	('13', '13'),
	('14', '14'),
	('15', '15'),
	('16', '16'),
	('17', '17'),
]

Day = (
	('Sunday', 'Sunday'),
	('Monday', 'Monday'),
	('Tuesday', 'Tuesday'),
	('Wednesday', 'Wednesday'),
	('Thursday', 'Thursday'),
	('Friday', 'Friday'),
	('Saturday', 'Saturday')
)

class ChildSupplementPolicyForm(forms.ModelForm):
	hotel = forms.ModelChoiceField(widget=forms.HiddenInput, queryset=Hotels.objects.all(), empty_label=None)
	unit = forms.ChoiceField(widget=forms.Select(
		attrs={'class': "form-control custom-input-box myselect"}), choices=UNIT, label='Price', required=False)
	age_category = forms.ChoiceField(widget=forms.Select(
		attrs={'class': "form-control custom-input-box myselect"}), choices=CATEGORY, label='Age')
	age_start = forms.ChoiceField(widget=forms.Select(
		attrs={'class': "form-control custom-input-box myselect"}), choices=AGE, label='Age')
	age_end = forms.ChoiceField(widget=forms.Select(
		attrs={'class': "form-control custom-input-box myselect"}), choices=AGE, label='Age',required=False)
	cost_status = forms.ChoiceField(widget=forms.Select(
		attrs={'class': "form-control custom-input-box myselect"}), choices=PRICE, label='Price')
	cost = forms.DecimalField(label='Price',widget=forms.TextInput(attrs={'style':'height:28px'}), required=False )
	season_start_date = forms.DateField(widget=forms.DateInput(attrs={'class': "myDatePicker"}), required=False,
	                                    label='Policy applicable from?')
	season_end_date = forms.DateField(widget=forms.DateInput(attrs={'class': "myDatePicker"}), required=False,
	                                  label='Policy applicable till?')
	day = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple(attrs={'class':'customDay'}), choices=Day,
	                                label='Select days if this policy is applicable on day basis.', required=False,)

	def __init__(self, hotel,action ,*args, **kwargs):
		super(ChildSupplementPolicyForm, self).__init__(*args, **kwargs)
		self.fields['hotel'].initial = hotel
		self.fields['cost'].initial = 0
		self.name = 'childSupplementForm'
		self.fields['day'].widget.attrs.pop('id', None)
		self.action = action

	class Meta:
		model = ChildSupplementPolicy
		fields = [
			"age_category",
			"age_start",
			"age_end",
			"cost_status",
			"cost",
			"unit",
			"hotel",
			"season_start_date",
			"season_end_date",
			"day"
		]

	class Media:
		js = ('js/hotelInventoryChildSupplement.js',)


from django.contrib import admin
from .models import Draft, DraftProperty

class HideAdmin(admin.ModelAdmin):
    def get_model_perms(self, request):
        return {}

class DraftPropertyInline(admin.TabularInline):
    model = DraftProperty
    extra = 3

class DraftAdmin(admin.ModelAdmin):
    list_display = ['title', 'number', 'is_active']
    list_filter = ['is_active',]
    prepopulated_fields = {"slug": ("title",)}
    inlines = [
        DraftPropertyInline,
    ]

admin.site.register(Draft, DraftAdmin)
admin.site.register(DraftProperty, HideAdmin)


#!/usr/bin/python3

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os,sys
import plyfile
import numpy as np
import argparse
import h5py

# reduced_length_dict = {"MarketplaceFeldkirch":[10538633,"marketsquarefeldkirch4-reduced"],
#                        "StGallenCathedral":[14608690,"stgallencathedral6-reduced"],
#                        "sg27":[28931322,"sg27_10-reduced"],
#                        "sg28":[24620684,"sg28_2-reduced"]}
#
# full_length_dict = {"stgallencathedral_station1":[31179769,"stgallencathedral1"],
#                     "stgallencathedral_station3":[31643853,"stgallencathedral3"],
#                     "stgallencathedral_station6":[32486227,"stgallencathedral6"],
#                     "marketplacefeldkirch_station1":[26884140,"marketsquarefeldkirch1"],
#                     "marketplacefeldkirch_station4":[23137668,"marketsquarefeldkirch4"],
#                     "marketplacefeldkirch_station7":[23419114,"marketsquarefeldkirch7"],
#                     "birdfountain_station1":[40133912,"birdfountain1"],
#                     "castleblatten_station1":[31806225,"castleblatten1"],
#                     "castleblatten_station5":[49152311,"castleblatten5"],
#                     "sg27_station3":[422445052,"sg27_3"],
#                     "sg27_station6":[226790878,"sg27_6"],
#                     "sg27_station8":[429615314,"sg27_8"],
#                     "sg27_station10":[285579196,"sg27_10"],
#                     "sg28_station2":[170158281,"sg28_2"],
#                     "sg28_station5":[267520082,"sg28_5"]}
from data_utils import read_xyz_label_from_las, save_xyz_label_to_las, read_points_from_las, \
    rewrite_las_with_new_labels

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--datafolder', '-d', help='Path to input *_pred.h5', required=True)
    # parser.add_argument('--version', '-v', help='full or reduced', type=str, required=True)
    args = parser.parse_args()
    print(args)

    # if args.version == 'full':
    #     length_dict = full_length_dict
    # else:
    #     length_dict = reduced_length_dict

    import time
    start_time_dict = {}
    total_time_dict = {}

    current_time_ms = lambda: int(round(time.time() * 1000))

    def timer_start(msg):
        global start_time_dict
        global total_time_dict
        start_time_dict[msg] = current_time_ms()
        if not msg in total_time_dict:
            total_time_dict[msg] = 0

    def timer_pause(msg):
        global start_time_dict
        global total_time_dict
        total_time_dict[msg] += current_time_ms() - start_time_dict[msg]

    def timer_stop(msg):
        global total_time_dict
        timer_pause(msg)
        print("{} completed in {}ms".format(msg, total_time_dict[msg]))
        total_time_dict[msg] = 0

    def get_pred_prefixes(datafolder):
        fs = os.listdir(datafolder)
        preds = []
        for f in fs:
            if f[-8:] == '_pred.h5':
                preds += [f]
        pred_pfx = []
        for p in preds:
            if '_zero' in p:
                pred_pfx += [p.split('_zero')[0]]
        return np.unique(pred_pfx)

    SAVE_TO_EXT = '.las'
    LOAD_FROM_EXT = '.las'
    categories_list = get_pred_prefixes(args.datafolder)

    # categories_list = [category for category in length_dict]
    print(categories_list)

    for category in categories_list:
        output_path = os.path.join(args.datafolder, "results", category + "_pred" + SAVE_TO_EXT)
        if not os.path.exists(os.path.join(args.datafolder,"results")):
            os.makedirs(os.path.join(args.datafolder,"results"))
        pred_list = [pred for pred in os.listdir(args.datafolder)
                     if category in pred  and pred.split(".")[0].split("_")[-1] == 'pred']

        print('pred_list: {}'.format(pred_list))

        #     label_length = length_dict[category][0]
        #     merged_label = np.zeros((label_length),dtype=int)
        #     merged_confidence = np.zeros((label_length),dtype=float)
        merged_label = None
        merged_confidence = None

        for pred_file in pred_list:
            print(os.path.join(args.datafolder, pred_file))
            data = h5py.File(os.path.join(args.datafolder, pred_file))
            labels_seg = data['label_seg'][...].astype(np.int64)
            indices = data['indices_split_to_full'][...].astype(np.int64)
            confidence = data['confidence'][...].astype(np.float32)
            data_num = data['data_num'][...].astype(np.int64)

            if merged_label is None:
                # calculating how many labels need to be there in the output
                label_length = 0
                for i in range(indices.shape[0]):
                    label_length = np.max([label_length, np.max(indices[i][:data_num[i]])])
                label_length += 1
                merged_label = np.zeros((label_length), dtype=int)
                merged_confidence = np.zeros((label_length), dtype=float)
            else:
                label_length2 = 0
                for i in range(indices.shape[0]):
                    label_length2 = np.max([label_length2, np.max(indices[i][:data_num[i]])])
                label_length2 += 1
                if label_length < label_length2:
                    # expaning labels and confidence arrays, as the new file appears having mode of them
                    extend_array_label = np.zeros(label_length2 - label_length, dtype=int)
                    extend_array_confidence = np.zeros(label_length2 - label_length, dtype=float)
                    merged_label = np.append(merged_label, extend_array_label, 0)
                    merged_confidence = np.append(merged_confidence, extend_array_confidence, 0)
                    # for i in range(label_length2 - label_length):
                    #     merged_label = np.append(merged_label, 0)
                    #     merged_confidence = np.append(merged_confidence, 0.0)
                    label_length = label_length2

            for i in range(labels_seg.shape[0]):
                temp_label = np.zeros((data_num[i]),dtype=int)
                pred_confidence = confidence[i][:data_num[i]]
                temp_confidence = merged_confidence[indices[i][:data_num[i]]]

                temp_label[temp_confidence >= pred_confidence] = merged_label[indices[i][:data_num[i]]][temp_confidence >= pred_confidence]
                temp_label[pred_confidence > temp_confidence] = labels_seg[i][:data_num[i]][pred_confidence > temp_confidence]

                merged_confidence[indices[i][:data_num[i]][pred_confidence > temp_confidence]] = pred_confidence[pred_confidence > temp_confidence]
                merged_label[indices[i][:data_num[i]]] = temp_label

        if len(pred_list) > 0:
            # concatenating source points with the final labels and writing out resulting file
            final_labels = np.ndarray((merged_label.shape[0], 1), np.int64)
            final_labels[:, 0] = merged_label  # + 1
            points_path = os.path.join(args.datafolder, category + LOAD_FROM_EXT)

            rewrite_las_with_new_labels(points_path, output_path, merged_label)
            # if LOAD_FROM_EXT == ".las":
            #     points, h = read_points_from_las(points_path)
            # else:
            #     print('Reading {}'.format(points_path))
            #     points = np.loadtxt(points_path)
            # if SAVE_TO_EXT == '.las':
            #     save_xyz_label_to_las(output_path, points, final_labels, h)
            # else:
            #     final = np.concatenate([points, final_labels], axis=-1)
            #     print('Writing {}'.format(output_path))
            #     np.savetxt(output_path, final, fmt='%1.3f %1.3f %1.3f %i %i')

if __name__ == '__main__':
    main()


from xai.brain.wordbase.nouns._blessing import _BLESSING

#calss header
class _BLESSINGS(_BLESSING, ):
	def __init__(self,):
		_BLESSING.__init__(self)
		self.name = "BLESSINGS"
		self.specie = 'nouns'
		self.basic = "blessing"
		self.jsondata = {}


import MeCab
import sys

text_path = './neko.txt'
analized_text_path = './neko.txt.mecab'
m = MeCab.Tagger('-d /etc/alternatives/mecab-dictionary')

with open(text_path) as input, open(analized_text_path, mode='w') as output:
	output.write(m.parse(input.read()))


'''
ESTIMATED TIME TO COMPLETE: 6 minutes

The greatest common divisor of two positive integers is the largest integer that divides each of them without remainder. For example,

gcd(2, 12) = 2

gcd(6, 12) = 6

gcd(9, 12) = 3

gcd(17, 12) = 1

A clever mathematical trick (due to Euclid) makes it easy to find greatest common divisors. Suppose that a and b are two positive integers:

If b = 0, then the answer is a

Otherwise, gcd(a, b) is the same as gcd(b, a % b)
'''
def gcdRecur(a, b):
	'''
	a, b: positive integers

	returns: a positive integer, the greatest common divisor of a & b.
	'''
	if b == 0:
		return a
	else:
		return gcdRecur(b, a % b)

print (gcdRecur(88, 168))

def gcdIter(a, b):
	'''
	a, b: positive integers

	returns: a positive integer, the greatest common divisor of a & b.
	'''
	testValue = min(a, b)

	# Keep looping until testValue divides both a & b evenly
	while a % testValue != 0 or b % testValue != 0:
		testValue -= 1

	return testValue

MITx-6.00.1x/lec4_DECOMPOSITION, ABSTRACTION, FUNCTIONS/greatest common divisor.py


import json

fileName = "example_topology"
OFName = "openFlowEntries.json"

with open(fileName, "r") as f:
    lines = f.readlines()

ruleList = list()

for line in lines:
    fields = line.split()
    if len(fields) == 8:  # 0,1,2,4,5,6
        id = fields[0]
        sw = fields[1]
        ip = fields[2]
        inport = fields[4]
        outport = fields[5]
        priority = fields[6]
        rule = {}
        rule['dpid'] = int(sw) + 1
        rule['priority'] = int(priority)
        rule['cookie'] = 0
        rule['table_id'] = 0
        rule['match'] = {"in_port": int(inport),
                         "nw_src": ip,
                         "dl_type": 2048}
        rule['actions'] = []
        rule['actions'].append({
            "type": "OUTPUT",
            "port": int(outport)})
        ruleList.append(rule)
with open(OFName, 'w', newline='\n') as OFfile:
    json.dump(ruleList, OFfile, indent=2, separators=(',', ':'))


'''
for Weiji's grant proposal June 12, 2020
0.5 margin on both sides left and right
Figure has 4 panels
Panel A: initial board visualization for example puzzle at 4 levels
Panel B: subjects distance to goal with actual puzzle (subject time course)
Panel C: MAG visualizations for initial board positions
Panle D: BFS tree visualization at the end of MakeMove call, with principal variation marked as blue
'''
import matplotlib.pyplot as plt
from Board import *
import pandas as pd
import random
from graphviz import Digraph

def plot_board(board, name='', show_car_label=False):
	'''
	visualize board position and save into png figure
	figure name: lv10_<puzzlename>_board.png
	'''
	matrix = get_board_matrix(board)
	cmap = plt.cm.Set1
	cmap.set_bad(color='white')
	fig, ax = plt.subplots(figsize=(16,12))
	ax.set_xticks(np.arange(-0.5, 5, 1))
	ax.set_yticks(np.arange(-0.5, 5, 1))
	ax.set_axisbelow(True)
	ax.grid(b=True, which='major',color='gray', linestyle='-', linewidth=1, alpha=0.1)
	ax.set_xticklabels([])
	ax.set_yticklabels([])
	ax.tick_params(axis='both', which='both',length=0)
	im = ax.imshow(matrix, cmap=cmap)
	if show_car_label:
		for i in range(len(matrix)):
			for j in range(len(matrix[i])):
				num = matrix[i, j]
				if num == 0:
					num = 'R'
				elif num > 0:
					num -= 1
				else:
					num = ''
				text = ax.text(j, i, num, ha="center", va="center", color="black", fontsize=36)
	for axis in ['top','bottom','left','right']:
		ax.spines[axis].set_linewidth(4)
		ax.spines[axis].set_zorder(0)
	fig.patch.set_facecolor('white')
	fig.patch.set_alpha(0.2)
	plt.savefig('/Users/yichen/Desktop/'+name+'_board.png',
			facecolor = fig.get_facecolor(), transparent = True) # lv10_<puzzlename>_board.png
	plt.close()
	return '/Users/yichen/Desktop/'+name+'_board.png'

def plot_MAG(board, name=''):
	matrix = get_board_matrix(board)
	cmap = plt.cm.Set1
	dot = Digraph(format='png')
	dot.node('r',label='R')
	mag = MAG(board)
	mag.construct()

	for f in finished_list:
		dot.node(f.tag)
		#print(f.tag)
		for edge_car in f.edge_to:
			dot.node(edge_car.tag)
			dot.edge(f.tag, edge_car.tag)
			#print("edge: " + edge_car.tag)
	# save and show
	dot.render(filename, view=False)

if __name__ == '__main__':

	# data = pd.read_csv('/Users/yichen/Desktop/trialdata_valid_true_dist7_processed.csv')

	level6_puzzles = ['prb11647', 'prb1707', 'prb23259', 'prb10206', 'prb2834', 'prb8786', 'prb28111', 'prb32795', 'prb26567', 'prb21272', 'prb14047', 'prb14651', 'prb32695', 'prb13171', 'prb29232', 'prb15290', 'prb12604', 'prb20059']
	level10_puzzles = ['prb38526', 'prb3217', 'prb34092', 'prb29414', 'prb12715', 'prb62015', 'prb54081', 'prb717', 'prb31907', 'prb42959', 'prb79230', 'prb9718', 'prb14898', 'prb22436', 'prb62222', 'prb68910', 'prb33509', 'prb46224']
	level13_puzzles = ['prb38725', 'prb29585', 'prb33117', 'prb20888', 'prb55384', 'prb6671', 'prb343', 'prb47495', 'prb68514', 'prb29600', 'prb23404', 'prb19279', 'prb3203', 'prb14485', 'prb34551', 'prb72800', 'prb65535']
	level15_puzzles = ['prb24227', 'prb45893', 'prb44171', 'prb25861', 'prb1267', 'prb15595', 'prb54506', 'prb48146', 'prb78361', 'prb25604', 'prb29027', 'prb46639', 'prb46580', 'prb10166', 'prb24406', 'prb58853', 'prb57223']

	# for random_puzzle in level15_puzzles:
	# # random_puzzle = random.choice(level6_puzzles)
	# 	print('Random chosen puzzle: '+random_puzzle)
	# 	board = json_to_board('/Users/yichen/Documents/RushHour/exp_data/data_adopted/'+random_puzzle+'.json')
	# 	plot_board(board, name='lv15_'+random_puzzle)


# -*- coding: utf-8 -*-
import sys
import os
import codecs
import re
import time

import warnings
warnings.filterwarnings(action='ignore', category=UserWarning, module='gensim')
from gensim import models
from gensim.models.doc2vec import LabeledSentence

sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/../lib/utils")
from conf import Conf
from log import Log
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/../lib/documents")
from wiki import Wiki

class LabeledLineSentence(object):
	def __init__(self, filename):
		self.filename = filename
	def __iter(self):
		for uid, line in enumerate(open(filename)):
			yield LabeledSentence(words = line.split(), labels = ['sent_%s' % uid])

start_time = time.perf_counter()
sentences = []
#docs = ["./enwiki-articles1-sample"]
docs = ["../../data/wikitext/datasets/enwiki-20170501-pages-articles1.xml-p10p30302"]
#docs = [
#	"../../data/wikitext/datasets/enwiki-20170501-pages-articles1.xml-p10p30302",
#	"../../data/wikitext/datasets/enwiki-20170501-pages-articles2.xml-p30304p88444"
#	]
wiki = Wiki()
docs_map = wiki.convert_docs_to_map(docs, doc_type="enwiki")
for title,contents in docs_map.items():
	sentences.append(LabeledSentence(words=contents, tags=[title]))
wiki.substitute_redirect_articles(docs_map)
wiki.delete_redirect_only_articles(docs_map)
print("create sentences finished. sentences len is " + str(len(sentences)))
print("perf_counter = {:.7f}".format(time.perf_counter() - start_time))

print("create model")
start_time = time.perf_counter()
#model = models.Doc2Vec(
#	sentences, dm=0, size=300, window=15, alpha=.025,
#	min_alpha=.025, min_count=1, sample=1e-6)
model = models.Doc2Vec(dm=0, size=300, window=15, alpha = .025, min_alpha =.025, min_count = 1)
model.build_vocab(sentences)
print("perf_counter = {:.7f}".format(time.perf_counter() - start_time))

print('\ntrain start')
for epoch in range(20):
	print('Epoch: {}'.format(epoch + 1))
	start_time = time.perf_counter()
	model.train(sentences,  total_examples=len(sentences), epochs=model.iter)
	model.alpha -= (0.025 - 0.0001) / 19
	model.min_alpha = model.alpha
	print("perf_counter = {:.7f}".format(time.perf_counter() - start_time))

print("save model")
model.save('wikitext_articles1_doc2vec.model')


import os
import numpy as np
import torch
import json
import math

def temp2coco():
    basicPath = os.sys.path[0]
    trainPath = os.path.join(basicPath, "train")
    valPath = os.path.join(basicPath, "val")
    annoPath = os.path.join(basicPath, "train_annotations.json")

    classList = ['i2', 'i4', 'i5', 'io', 'ip', 'p11', 'p23', 'p26', 'p5', 'pl30',
                 'pl40', 'pl5', 'pl50', 'pl60', 'pl80', 'pn', 'pne', 'po', 'w57']
    class2id = {}  # 建立类和类编号的字典
    for i in range(len(classList)):
        class2id[classList[i]] = i

    train_dirs = os.listdir(trainPath)
    val_dirs = os.listdir(valPath)

    with open(annoPath, 'r') as f:
        anno = json.load(f)

    train_outfile = os.path.join(basicPath, "train.json")
    val_outfile = os.path.join(basicPath, "val.json")

    train_coco = {}
    train_coco['info'] = "spytensor created"
    train_coco['license'] = ["license"]
    train_coco['images'] = []
    train_coco['annotations'] = []
    train_coco['categories'] = []

    for i in range(len(classList)):
        cat = {}
        cat['id'] = i
        cat['name'] = classList[i]
        train_coco['categories'].append(cat)

    val_coco = {}
    val_coco['info'] = "spytensor created"
    val_coco['license'] = ["license"]
    val_coco['images'] = []
    val_coco['annotations'] = []
    val_coco['categories'] = train_coco['categories']

    sum_train = 0
    sum_val = 0
    for key in anno['imgs']:
        img = {}
        img["height"] = 2048
        img["width"] = 2048
        img["id"] = anno['imgs'][key]['id']
        img["file_name"] = anno['imgs'][key]['path'][6:]

        temp = str(key) + '.jpg'
        if temp in train_dirs:
            train_coco['images'].append(img)
            for j in range(len(anno['imgs'][key]['objects'])):
                target = anno['imgs'][key]['objects'][j]
                obj = {}
                obj['id'] = sum_train
                obj['image_id'] = int(key)
                obj['category_id'] = class2id[target['category']]
                obj['segementation'] = []

                if 'ellipse_org' in target:
                    seg_temp = []
                    for v in target['ellipse_org']:
                        seg_temp.append(v[0])
                        seg_temp.append(v[1])
                    obj['segementation'].append(seg_temp)

                obj['bbox'] = [math.floor(target['bbox']['xmin'])]
                obj['bbox'].append(math.floor(target['bbox']['ymin']))
                w = math.floor(target['bbox']['xmax'] - target['bbox']['xmin'])
                h = math.floor(target['bbox']['ymax'] - target['bbox']['ymin'])
                obj['bbox'].append(w)
                obj['bbox'].append(h)

                obj['iscrowd'] = 0
                obj['area'] = float((w + 1)*(h + 1))

                train_coco['annotations'].append(obj)
                sum_train = sum_train + 1
        else:
            val_coco['images'].append(img)
            for j in range(len(anno['imgs'][key]['objects'])):
                target = anno['imgs'][key]['objects'][j]
                obj = {}
                obj['id'] = sum_val
                obj['image_id'] = int(key)
                obj['category_id'] = class2id[target['category']]
                obj['segementation'] = []

                if 'ellipse_org' in target:
                    seg_temp = []
                    for v in target['ellipse_org']:
                        seg_temp.append(v[0])
                        seg_temp.append(v[1])
                    obj['segementation'].append(seg_temp)

                obj['bbox'] = [math.floor(target['bbox']['xmin'])]
                obj['bbox'].append(math.floor(target['bbox']['ymin']))
                w = math.floor(target['bbox']['xmax'] - target['bbox']['xmin'])
                h = math.floor(target['bbox']['ymax'] - target['bbox']['ymin'])
                obj['bbox'].append(w)
                obj['bbox'].append(h)

                obj['iscrowd'] = 0
                obj['area'] = float((w + 1) * (h + 1))

                val_coco['annotations'].append(obj)
                sum_val = sum_val + 1

    with open(train_outfile, 'w') as f:
        json.dump(train_coco, f)
    with open(val_outfile, 'w') as f:
        json.dump(val_coco, f)

if __name__ =='__main__':
    temp2coco()


#%%
# -*- coding: utf-8 -*-
import numpy as np
import sys
import glob
import matplotlib.pyplot as plt
import pandas as pd
import skimage.io
import skimage.segmentation
import skimage.morphology
sys.path.insert(0, '../../../')
import mut.image
import mut.viz
import mut.stats
colors = mut.viz.pub_style()

# Define the experimental parameters
DATE = 20180412
USERNAME = 'mrazomej'
RUN_NO = 1
CLASS = 'DNA'
MUT = ['Q21M']
OP = 'O2'
IPTG = [0]
IP_DIST = 0.16 # in µm per pixel

# %%
data_dir = '../../../data/images/{}/'.format(DATE)
# data_dir = 'data/images/'

# Load the images for the flat field
noise_files = glob.glob('{}/{}_camera_noise*/*.tif'.format(data_dir, DATE))
noise_ims = [skimage.io.ImageCollection(f)[0] for f in noise_files]
avg_noise = mut.image.projection(noise_ims, mode='mean')
yfp_files = glob.glob('{}/{}_YFP_profile_*/*.tif'.format(data_dir, DATE))
yfp_ims = [skimage.io.ImageCollection(f)[0] for f in yfp_files]
avg_field = mut.image.projection(yfp_ims, mode='mean')

# %% Segment auto fluorescence and store in a dataframe.
df = pd.DataFrame([], columns=['date', 'username', 'mutant', 'operator', 'strain',
                               'IPTGuM', 'repressors', 'mean_intensity',
                               'area', 'eccentricity'])
samples = glob.glob('{}/{}_*/*.tif'.format(data_dir, DATE))
for i, s in enumerate(samples):
    if ('camera_noise' not in s) and ('YFP_profile') not in s:
        try:
            _, mutant, strain, op, IPTG, _ = s.split('/')[-2].split('_')
            IPTG = float(IPTG.split('uM')[0])
            R = int(strain[1:])
            op = 'O2'
        except:
            _, strain, _ = s.split('/')[-2].split('_')
            R = 0
            IPTG = 0
            op = 'O2'
            mutant = 'none'

        # Median filter the fluorescence image.
        im = skimage.io.ImageCollection(s)

        seg = mut.image.log_segmentation(im[1], thresh=0.0001)
        seg_label = skimage.measure.label(seg)
        selem = skimage.morphology.square(3)

        im_filt = skimage.filters.median(im[2], selem)

        # Flatten the image.
        im_flat = mut.image.generate_flatfield(im_filt, avg_noise, avg_field, median_filt=False )

        # Compute the properties and assemble the dataframe.
        props = skimage.measure.regionprops(seg_label, im_flat)
        for p in props:
            cell_dict = {'strain':strain, 'area':p.area * IP_DIST**2,
                       'eccentricity':p.eccentricity,
                       'mean_intensity':p.mean_intensity,
                       'date': DATE, 'username':USERNAME, 'mutant':mutant,
                       'operator': op, 'IPTGuM':IPTG, 'repressors': R}
            df = df.append(cell_dict, ignore_index=True)

# %% Prune the data frame to restrict to single cells.
filt_df = df[(df['area'] >= 1) & (df['area'] <= 4) & (df['eccentricity'] >= 0.8)]

_ = plt.hist(df['eccentricity'], bins=100)
#%%  Compute the fold-change.
mean_auto = filt_df[filt_df['strain']=='auto']['mean_intensity'].mean()
mean_delta = filt_df[filt_df['strain']=='delta']['mean_intensity'].mean()
def compute_fc(df):
    fc = (df['mean_intensity'].mean() - mean_auto) / (mean_delta - mean_auto)
    new_df = {'date': df.date.unique()[0], 'mutant':df.mutant.unique()[0],
    'repressors':df.repressors.unique()[0], 'username':df.username.unique()[0], 'mean_YFP':df.mean_intensity.mean(),
    'fold_change': fc, 'run_number': RUN_NO}
    fc = pd.Series(new_df)
    return fc
grouped = filt_df.groupby(['strain']).apply(compute_fc)
fc_df = pd.DataFrame(grouped).reset_index()
fc_df.to_csv('./output/{}_r{}_{}_{}_microscopy_fold_change.csv'.format(DATE, RUN_NO, MUT[0], OP),
index=False)

# Plot the fold-change vs repressor copy number
fig, ax = plt.subplots(1, 1)
ax.set_xlabel('repressor copy number')
ax.set_ylabel('fold-change')
fc_df = fc_df[fc_df['repressors'] != 0]
fc_df.sort_values('repressors', inplace=True)
_ = ax.loglog(fc_df['repressors'], fc_df['fold_change'], '--o')
plt.tight_layout()
plt.savefig('./output/{}_r{}_{}_{}_microscopy_fold_change.png'.format(DATE, RUN_NO, MUT[0],
OP), bbox_inches='tight')

print(fc_df)


import os
import zipfile

def download_patent_file(df_file):
    storage_dir = "data"
    url = "https://bulkdata.uspto.gov/data/patent/grant/redbook/bibliographic/{}/{}".format(df_file["year"].values[0], df_file["file"].values[0])
    storage_path = os.path.join(os.getcwd(), storage_dir)

    path = os.path.join(storage_path, df_file["file"].values[0])
    print('Attempting to download {} to {}'.format(url, path))
    #cmd = 'curl -o {} {}'.format(path, url)
    #os.system(cmd)
    print("Unzipping the file {}".format(df_file["file"].values[0]))
    print(path)
    zip_ref = zipfile.ZipFile(path, 'r')
    zip_ref.extractall(storage_path)
    zip_ref.close()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import threading, os

def run_node(param):
    os.system(cmd + param)

# def run_node(i):
    # os.system(cmd + node_list[i])

cmd = 'java -jar C:\\Users\\Han\\selenium_test\\selenium-server-standalone-3.11.0.jar -role '
# node_list = ['hub', 'node', 'node -port 5556','node -port 5557']
node_list = ['hub', 'node -port 5556','node -port 5557']
for i in range(len(node_list)):
    # class threading.Thread(group=None, target=None, name=None, args=(), kwargs={}, *, daemon=None)
    # args=() -> tuple -> need ","
    t = threading.Thread(target=run_node,args=(node_list[i],))
    # t = threading.Thread(target=run_node,args=(i,))
    t.start()

# 1. start selenium server hub and node on localhost
# localhost windows(192.168.2.41) VMware Network Adapter VMnet1(192.168.91.1):
# (C:\Users\Han\Anaconda3) C:\Users\Han\selenium_test\ch09>python run_selenium-server.py
# 15:39:08.704 INFO [GridLauncherV3.launch] - Selenium build info: version: '3.11.0', revision: 'e59cfb3'
# 15:39:08.719 INFO [GridLauncherV3$3.launch] - Launching a Selenium Grid node on port 5557
# 15:39:08.737 INFO [GridLauncherV3.launch] - Selenium build info: version: '3.11.0', revision: 'e59cfb3'
# 15:39:08.754 INFO [GridLauncherV3$2.launch] - Launching Selenium Grid hub on port 4444
# 15:39:08.784 INFO [GridLauncherV3.launch] - Selenium build info: version: '3.11.0', revision: 'e59cfb3'
# 15:39:08.791 INFO [GridLauncherV3$3.launch] - Launching a Selenium Grid node on port 5556

# 2. start selenium server node on remote ubuntu 16
# remote ubuntu 16(192.168.157.130):
# han@han-virtual-machine:~$ java -jar selenium-server-standalone-3.11.0.jar -role node -port 5555 -hub http://192.168.2.41:4444/grid/register

# http://localhost:4444/grid/console

# 3. launch python script
# (C:\Users\Han\Anaconda3) C:\Users\Han\selenium_test\ch09>python local_hub_remote_node_2.py
# http://127.0.0.1:5556/wd/hub chrome
# http://127.0.0.1:5557/wd/hub firefox
# http://192.168.157.130:5555/wd/hub firefox