from unittest import TestCase
from unittest.mock import patch
from os import sep
from os.path import join

from .get_os import OS
from .randomstring import randomstring

from .get_home import get_home as subject

class TestGetHome(TestCase):

    def test_win(self):
        homedrive = randomstring()
        homepath = sep.join([ randomstring(), randomstring() ])
        user = randomstring()
        home = (homedrive+homepath).split(sep)
        home[-1] = user
        home = sep.join(home)
        with patch(__package__+'.get_home.get_os', return_value=OS.WIN32), patch.dict(__package__+'.get_home.environ', HOMEDRIVE=homedrive, HOMEPATH=homepath), patch(__package__+'.get_home.get_user', return_value=user):
            self.assertEqual(subject(), home)

    def test_linux(self):
        homepath = sep.join([ randomstring(), randomstring() ])
        user = randomstring()
        home = homepath.split(sep)
        home[-1] = user
        home = sep.join(home)
        with patch(__package__+'.get_home.get_os', return_value=OS.CENTOS), patch.dict(__package__+'.get_home.environ', HOME=homepath), patch(__package__+'.get_home.get_user', return_value=user):
            self.assertEqual(subject(), join(sep, 'home', user))


import re
from decimal import Decimal

# 前，中，后
# reg_duixiang1 = '(报价|询价|承诺|持有|担保|发行|股东|关联|合伙|获配|交易|配售|配套|认购|申购|投资|委托|邀请|法定代表)' \
reg_duixiang0 = '(发行|获配|交易|配售|配套|认购|投资)' \
                '(机构|者|方|人|对象|对方|对手|单位|主体|产品)' \
                '(名称|全称|简称|构成|名册|姓名)?[：:]?$'
pattern_duixiang0 = re.compile(reg_duixiang0)

reg_duixiang1 = '(报价|询价|承诺|持有|关联|合伙|委托|邀请)' \
                '(机构|者|方|人|对象|对方|对手|单位|主体|产品)' \
                '(名称|全称|简称|构成|名册|姓名)?[：:]?$'
pattern_duixiang1 = re.compile(reg_duixiang1)

# reg_duixiang2 = '(产品|单位|公司|机构|自然人|企业|账户)?(名称|全称|简称|构成|名册|姓名)'
reg_duixiang2 = '(产品|单位|公司|机构|自然人|企业|账户)?(名称|全称|简称|构成|名册|姓名)[：:]?$'
pattern_duixiang2 = re.compile(reg_duixiang2)

reg_duixiang_neg = '股东|项目'
pattern_duixiang_neg = re.compile(reg_duixiang_neg)

# duixiangs = ['标的资产', '公司', '股东', '机构', '一致行动人', '乙方', '员工持股计划']
duixiangs = ['标的资产', '公司', '机构', '一致行动人', '乙方', '员工持股计划']

reg_not_dx = '合计|总计|汇总|小计|其中|其他|其它'
pattern_not_dx = re.compile(reg_not_dx)

# -------------------------------以上对对象专用-------------------------------

# 动词，副词，名词，量词，单位
reg_shuliang1 = '(配售|获配|获售|限售|发行|发售|发股|认购|持有|持股|变动|交易|调整|申购|申报)(后|之后)?' \
                '(股份|股本|证券|股票|A股|新股|股数)?(总数|总额|数|数量)?(\(?万?[股只]\)?)?$'
pattern_shuliang1 = re.compile(reg_shuliang1)

reg_shuliang2 = '(股份|股本|证券|股票|A股|新股|股数)(总数|总额|数|数量)?(\(?万?[股只]\)?)?$'
pattern_shuliang2 = re.compile(reg_shuliang2)

# 动词，名词，单位
reg_jine = '(((出资|担保|获配|募集|配售|认购|认缴|申购|投资)(资金)?(额|金额|总额|资金)(/?\(?万?元\)?)?)|((金额|总额)(/?\(?万?元\)?)))$'
pattern_jine = re.compile(reg_jine)

# 动词，名词，单位
reg_suoding = '(((限售|锁定|流通)(时间|期|期限|月份)(\(?个?月\)?)?)|(\(个?月\)))$'
pattern_suoding = re.compile(reg_suoding)

# 动词，名词
reg_rengou = '^(((认购|出资|结算|分配|支付|对价)(方式|形式))|(现金|认购))$'
pattern_rengou = re.compile(reg_rengou)

reg_isnum = '\d+(\.\d+)?'
pattern_isnum = re.compile(reg_isnum)

def reg_dx_table(text):
    search0 = pattern_duixiang0.search(text)
    basescore = 0
    if text.find('对象') != -1:
        basescore += 0.5
    if text.find('项目') != -1:
        basescore -= 1
    if text.find('股东') != -1:
        basescore -= 0.9
    # if pattern_duixiang_neg.search(text) is not None:
    #     basescore -= 0.5

    if search0 is not None:
        return 3 + basescore
    else:
        search1 = pattern_duixiang1.search(text)
        if search1 is not None:
            return 2 + basescore
        else:
            search2 = pattern_duixiang2.search(text)
            if search2 is not None:
                return 1 + basescore
            else:
                if text in duixiangs:
                    return 1 + basescore
                else:
                    return basescore

def reg_sl_table(text):
    search1 = pattern_shuliang1.search(text)
    if search1 is not None:
        return True
    else:
        search2 = pattern_shuliang2.search(text)
        if search2 is not None:
            start = search2.span()[0]
            if start != 0:
                qian = text[start - 1:start]
                if qian != '前':
                    return True
                else:
                    return False
            else:
                return True
        else:
            return False

def reg_je_table(text):
    search = pattern_jine.search(text)
    if search is not None:
        return True
    else:
        return False

def reg_sd_table(text):
    search = pattern_suoding.search(text)
    if search is not None:
        return True
    else:
        return False

def reg_rg_table(text):
    search = pattern_rengou.search(text)
    if search is not None:
        return True
    else:
        return False

class dz_tmp():
    def __init__(self, id):
        self.countActual = 0  # 字段实际个数
        self.id = id
        self.duixiang = 'fddcUndefined'
        self.shuliang = 'fddcUndefined'
        self.jine = 'fddcUndefined'
        self.suoding = 'fddcUndefined'
        self.rengou = 'fddcUndefined'

    def desc(self):
        print('HTMLID= {} |DX= {} |数量= {} |金额= {} |锁定= {} |认购= {} '
              .format(self.id, self.duixiang, self.shuliang, self.jine, self.suoding, self.rengou))

def orgdz_byrow(id, table, startrow, rows, cols):
    dz_tmp_list = []
    for row in range(startrow + 1, rows):
        tmp = dz_tmp(id)
        for col in range(cols):
            tag = table[startrow][col]
            cell = table[row][col]
            numflag = pattern_isnum.search(cell)
            if reg_dx_table(tag):
                if tmp.duixiang == 'fddcUndefined' and pattern_not_dx.search(cell) is None:
                    tmp.duixiang = cell
                    tmp.countActual += 1
                continue
            if reg_sl_table(tag) and numflag is not None:
                num = numflag.group()
                if tag.find('万') != -1:
                    tmp.shuliang = str((Decimal(num) * 10000))
                else:
                    tmp.shuliang = num
                tmp.countActual += 1
                continue
            if reg_je_table(tag) and numflag is not None:
                num = numflag.group()
                if tag.find('万') != -1:
                    tmp.jine = str((Decimal(num) * 10000))
                else:
                    tmp.jine = num
                tmp.countActual += 1
                continue
            if reg_sd_table(tag) and numflag is not None:
                num = numflag.group()
                tmp.suoding = num
                tmp.countActual += 1
                continue
            if reg_rg_table(tag):
                tmp.rengou = cell
                tmp.countActual += 1
                continue
        if tmp.duixiang != 'fddcUndefined' and len(tmp.duixiang) > 1 and tmp.countActual > 1:
            # select min(`增发数量`), min(`增发金额`) from dingzeng where `增发数量` > 0 and `增发金额` > 0;
            # 17267,113616.860000
            if (tmp.jine != 'fddcUndefined' and float(tmp.jine) < 100000) \
                    or (tmp.shuliang != 'fddcUndefined' and float(tmp.shuliang) < 10000):  # 由训练数据统计得来
                pass
            else:
                # if len(dz_tmp_list) < 11:
                dz_tmp_list.append(tmp)

    effective = 0
    for tmp in dz_tmp_list:
        effective += tmp.countActual

    return effective, dz_tmp_list

def table_tag_byrow(id, table):
    rows = len(table)
    cols = len(table[0])
    tagcount_dict = {}
    for row in range(rows):
        for col in range(cols):
            cell = table[row][col]
            weight = reg_dx_table(cell)
            rownum = rows - 1 - row
            if weight > 0:
                tagcount_dict[row] = weight, cell, rownum
                break

    paixu = list(sorted(tagcount_dict.items(), key=lambda d: (d[1][0], d[1][2]), reverse=True))
    if len(paixu) > 0:
        startrow = paixu[0][0]
        weight = paixu[0][1][0]
        tag = paixu[0][1][1]
        effective, dz_tmp_list = orgdz_byrow(id, table, startrow, rows, cols)
        return weight, effective, dz_tmp_list, tag
    else:
        return 0, None, None, None


from datetime import datetime, timezone

def current_time():
    """
    Convert time now to desired format.
    """
    now = datetime.now(timezone.utc)
    date_format = "%a, %d %b %Y %H:%M:%S GMT"
    time = now.strftime(date_format)
    return time


from flask import request
from flask_restplus import Resource

from ..dtos.user_dto import UserDto
from ..services.user_service import UserService

api = UserDto.api
_user = UserDto.user

@api.route('/')
class UserList(Resource):
    @api.doc('list_of_registered_users')
    @api.marshal_list_with(_user, envelope='data')
    def get(self):
        """List all registered users"""
        return UserService.get_all_users()

    @api.response(201, 'User successfully created.')
    @api.response(209, 'User failed to create. Username already taken.')

    @api.doc('create a new user')
    @api.expect(_user, validate=True)
    def post(self):
        """Creates a new User """
        data = request.json
        return UserService.save_new_user(data=data)

@api.route('/<username>')
@api.param('username', 'The User identifier')
@api.response(404, 'User not found.')
class User(Resource):
    @api.doc('get a user')
    @api.marshal_with(_user)
    def get(self, username):
        """get a user given its identifier"""
        user = UserService.get_a_user(username)
        if not user:
            api.abort(404, "User not found.")
        else:
            return user


#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2021/04/21 16:09

@author: Tei Koten
"""

import os
import numpy as np
import pandas as pd

class formatLabchart(object):

    def __init__(self, file_path, save_path, col_name):
        self.file_path = file_path
        self.save_path = save_path
        self.col_name = col_name

    def split_dat(self):
        self.split_content(
            self.clean_format(),
            self.read_file())
        return None

    def read_file(self, encoding="ISO-8859-1"):
        """
        :param encoding: default = "ISO-8859-1",{"utf-8}
        :return: string
        """
        with open(self.file_path, "r", encoding=encoding) as f:
            lines = f.readlines()
        content = []
        for line in lines:
            content.append(line.strip())
        return content

    def clean_format(self):
        content = self.read_file()
        num = list()
        i = 0
        while i < len(content):
            split_line = content[i].split()
            if split_line[0] == 'Interval=':
                # print(i, i + 6)
                num.append(i)
                i = i + 6  #  it's string type from 0 to 5.
            else:
                i = i + 1
        return num

    def split_content(self, num, content):
        for i in range(len(num)):
            try:
                start, end = num[i] + 6, num[i + 1]
                res = list()
                for line in content[start:end]:
                    split_line = line.split()
                    Array = np.array(split_line).astype(np.float)
                    res.append(Array)
            except:
                start = num[i] + 6
                res = list()
                for line in content[start:]:
                    split_line = line.split()
                    Array = np.array(split_line).astype(np.float)
                    res.append(Array)
            output_res = pd.DataFrame(res, columns=self.col_name)
            output_path = os.path.join(self.save_path, str(i + 1) + '.txt')
            output_res.to_csv(output_path, index=False)
            print(i + 1, 'is finished')
        return None

if __name__ == "__main__":

    file_path = '/Users/kkkkouten/Desktop/GMD0514.txt'
    save_path = '/Users/kkkkouten/Desktop'
    col_name = ['time', 'switch', 'GM', 'LQL', 'RQL', 'AnkGyroY','motive']

    formatLabchart(file_path, save_path, col_name).split_dat()


from django import template
from reportedPhotos.models import ReportedPhotos
register = template.Library()

@register.filter
def checkandincrease(photoid):
    if ReportedPhotos.objects.filter(photoid=photoid).count() != 0:
        report = ReportedPhotos.objects.get(photoid=photoid)
    else:
        report = ReportedPhotos(photoid=photoid)

    report.howmany_reports += 1
    report.save()
    return True


def matMatMult(A,B):
    '''
    A,B The matricies to be multiplied as A * B
    Checking dimensions is left to user.
    '''
    toReturn = []
    for k in range(len(A)):
        newRow = []
        for i in range(len(A)): #
            sum = 0
            for j in range(len(B)): # For the number of entries in a column of B
                sum += A[k][j] * B[j][i]
            newRow.append(sum)
        toReturn.append(newRow)
    return toReturn

if __name__ == "__main__":
    A = [
            [0,2],
            [0,1],
            [0,0]
        ]

    B = [
            [1,2,3],
            [4,5,6],
        ]
    print(matMatMult(A,B))


ON_WIN_CARDS = [
    "Räv",
    "Falken",
]

ON_LOSE_CARDS = [
    "Biet",
    "Fjäril",
    "Gamle älgen",
    "Skatan",
]

ON_REVEAL_CARDS = [
    "Förtryckt Aborre",
    "Pilgiftsgroda",
    "Igelkotten",
    "Myrslok",
    "Uttern",
    "Groda",
    "Sälspion",
    "Ekorren",
    "Fiskmås",
    "Vildsvinet",
    "Storbent hare",
    "Falken",
    "Igelkotten 2 - The Return of Glen",
    "Vässlan",
    "Myggan",
    "Hungrig Varg",
]

ON_CYCLE_CARDS = [
    "Blodhund",
]

ON_HAND_ENTER_CARDS = [
    "Tuppen",
]

BEFORE_POWER_CARDS = [
    "Lilla Björn",
]


import boto3, json
from time import strftime

def create_iam_role_for_s3_bucket():
    global iam_client

    iam_client = boto3.client('iam')
    iam_role_name = 'iam_access_to_traffic_logs'
    iam_policy_name = 'NetworkTrafficLogsAccess'

    iam_client.create_role(
        RoleName = '{}'.format(iam_role_name),
        Description = 'Allows S3 buckets access to network traffic logs.',
        Path = '/',
        AssumeRolePolicyDocument = json.dumps(
            {
                "Version": "2012-10-17",
                "Statement": {
                    "Effect": "Allow",
                    "Principal": {"Service": "s3.amazonaws.com"},
                    "Action": "sts:AssumeRole"
                }
            },
            indent = 1,
            default = str
        )
    )

    iam_policy = iam_client.create_policy(
        PolicyName = '{}'.format(iam_policy_name),
        Description = 'Allows access to network traffic logs.',
        Path = '/',
        PolicyDocument = json.dumps(
            {
                "Version": "2012-10-17",
                "Statement": [
                    {
                    "Effect": "Allow",
                    "Action": [
                        "logs:CreateLogDelivery",
                        "logs:DeleteLogDelivery"
                        ],
                    "Resource": "*"
                    }
                ]
            },
            indent = 1,
            default = str
        )
    )

    iam_policy_arn = iam_policy['Policy']['Arn']

    iam_client.attach_role_policy(
        RoleName = '{}'.format(iam_role_name),
        PolicyArn = '{}'.format(iam_policy_arn)
    )

def select_s3_bucket():
    global s3_client, selected_bucket

    s3_client = boto3.client('s3')
    s3_buckets = s3_client.list_buckets()['Buckets']

    for i in range(len(s3_buckets)):
        print('{0}. {1}'.format(i + 1, s3_buckets[i]['Name']))

    bucket_num = input('Enter the list number of the S3 bucket to store flow logs in: ')

    while True:
        try:
            bucket_num = int(bucket_num)
            break
        except ValueError:
            print("This is not a number.")
        try:
            bucket_num <= len(s3_buckets)
            break
        except ValueError:
            print("The number must be less than or equal to the number of S3 buckets.")

    selected_bucket = s3_buckets[bucket_num - 1]['Name']

def create_s3_bucket_policy_for_logs():
    global s3_bucket_arn

    current_date_time = strftime("%m%d%Y%I%M%S")
    aws_account_num = boto3.resource('iam').CurrentUser().arn.split(':')[4]
    s3_bucket_arn = 'arn:aws:s3:::{0}/{1}/awslogs/{2}/'.format(selected_bucket, current_date_time, aws_account_num)

    s3_client.put_bucket_policy(
        Bucket = '{}'.format(selected_bucket),
        Policy = json.dumps(
            {
                "Version": "2012-10-17",
                "Statement": [
                    {
                        "Sid": "AWSLogDeliveryWrite",
                        "Effect": "Allow",
                        "Principal": {"Service": "delivery.logs.amazonaws.com"},
                        "Action": "s3:PutObject",
                        "Resource": "{}*".format(s3_bucket_arn),
                        "Condition": {"StringEquals": {"s3:x-amz-acl": "bucket-owner-full-control"}}
                    },
                    {
                        "Sid": "AWSLogDeliveryAclCheck",
                        "Effect": "Allow",
                        "Principal": {"Service": "delivery.logs.amazonaws.com"},
                        "Action": "s3:GetBucketAcl",
                        "Resource": "arn:aws:s3:::{}".format(selected_bucket)
                    }
                ]
            },
            indent = 1,
            default = str
        )
    )

def select_network_device():
    global device_type_num, ec2_client, select_network_device

    ec2_client = boto3.client('ec2')

    print('1. network interface\n2. VPC\n3. subnet')
    device_type_num = input('Enter the list number of the network device you want to monitor: ')

    while True:
        try:
            device_type_num = int(device_type_num)
            break
        except ValueError:
            print("This is not a number.")
        try:
            device_type_num <= 3
            break
        except ValueError:
            print("Enter either numbers 1, 2 or 3.")

    if device_type_num == 1:
        ec2_network_interfaces = ec2_client.describe_network_interfaces()['NetworkInterfaces']

        for i in range(len(ec2_network_interfaces)):
            print('{0}. {1}'.format(i + 1, ec2_network_interfaces[i]['NetworkInterfaceId']))

        network_interface_num = input('Enter the list number of the network interface you want to monitor: ')

        while True:
            try:
                network_interface_num = int(network_interface_num)
                break
            except ValueError:
                print("This is not a number.")
            try:
                network_interface_num <= len(ec2_network_interfaces)
                break
            except ValueError:
                print("The number must be less than or equal to the number of network interfaces.")

        select_network_device = ec2_network_interfaces[network_interface_num - 1]['NetworkInterfaceId']
    elif device_type_num == 2:
        ec2_vpcs = ec2_client.describe_vpcs()['Vpcs']

        for i in range(len(ec2_vpcs)):
            print('{0}. {1}'.format(i + 1, ec2_vpcs[i]['VpcId']))

        vpc_num = input('Enter the list number of the VPC you want to monitor: ')

        while True:
            try:
                vpc_num = int(vpc_num)
                break
            except ValueError:
                print("This is not a number.")
            try:
                vpc_num <= len(ec2_vpcs)
                break
            except ValueError:
                print("The number must be less than or equal to the number of VPCs.")

        select_network_device = ec2_vpcs[vpc_num - 1]['VpcId']
    else:
        ec2_subnets = ec2_client.describe_subnets()['Subnets']

        for i in range(len(ec2_subnets)):
            print('{0}. {1}'.format(i + 1, ec2_subnets[i]['SubnetId']))

        subnet_num = input('Enter the list number of the subnet you want to monitor: ')

        while True:
            try:
                subnet_num = int(subnet_num)
                break
            except ValueError:
                print("This is not a number.")
            try:
                subnet_num <= len(ec2_subnets)
                break
            except ValueError:
                print("The number must be less than or equal to the number of subnets.")

        select_network_device = ec2_subnets[subnet_num - 1]['SubnetId']

def create_flow_logs():
    if device_type_num == 1:
        resource_type = 'NetworkInterface'
    elif device_type_num == 2:
        resource_type = 'VPC'
    else:
        resource_type = 'Subnet'

    ec2_client.create_flow_logs(
        ResourceIds = ['{}'.format(select_network_device)],
        ResourceType = '{}'.format(resource_type),
        TrafficType = 'ALL',
        LogDestinationType = 's3',
        LogDestination = '{}'.format(s3_bucket_arn)
    )

def view_metadata():
    print(json.dumps(
            ec2_client.describe_flow_logs(
                Filters=[
                    {
                        'Name': 'resource-id',
                        'Values': [
                            '{}'.format(select_network_device)
                        ]
                    }
                ]
            )['FlowLogs'],
            indent = 1,
            default = str
        )
    )

if __name__ == "__main__":
    create_iam_role_for_s3_bucket()
    select_s3_bucket()
    create_s3_bucket_policy_for_logs()
    select_network_device()
    create_flow_logs()
    view_metadata()


from django.urls import path,include
from menu.views import MenuCategoryListView,MenuListView,ItemListView
app_name = 'menu'

urlpatterns = [
    path('category',MenuCategoryListView.as_view()),
    path('category/<category_id>/menu',MenuListView.as_view()),
    path('items',ItemListView.as_view())
]


# -*- coding:utf-8 -*-
import os
import re
import sys
import json
import zmq
import errno
import signal
from functools import wraps
from ee.common import xavier

handle_errorno = {
    #校验不过(error: the CRC)
    'handle_errno_crc_checksum_failure':-67, \
    #无权限(error: the instruction need permissions)
    'handle_errno_no_permission':-68, \
    #/任务正在运行(error: the instruction also is doing.which need long time)
    'handle_errno_running':-69, \
    'handle_errno_devices_error':-70, \
    'handle_errno_time_out':-75, \
    'handle_errno_call_handle_exception':-76, \
    'handle_errno_not_have_this_command':-190,\
    #参数出错-193...-223：参数\参数1...参数30错误
    'handle_errno_parameter_invalid':-193, \
    #执行出错-224...-254：执行\执行1...执行30错误(error: casued by ARM board doing)
    'handle_errno_execute_failure':-224,
    }

def handle_done(backstring = ""):
    sResult = "Done"
    sResult += backstring
    return sResult

def handle_error(errorno,backstring = ""):
    sResult = "Error"
    sResult += str(errorno)
    sResult += " "
    sResult += backstring
    return sResult

def is_ask_for_help(params):
    if len(params) == 1 and params[0] == "?":
        return True
    else :
        return False

class TimeoutError(Exception):
    pass

def timeout(seconds=5, error_message=os.strerror(errno.ETIME)):
    def decorator(func):
        def _handle_timeout(signum, frame):
            raise TimeoutError(error_message)

        def wrapper(*args, **kwargs):
            signal.signal(signal.SIGALRM, _handle_timeout)
            signal.alarm(seconds)
            try:
                result = func(*args, **kwargs)
            except TimeoutError:
                try:
                    xavier.proxy.reconnect()
                except Exception:
                    pass
                result = handle_error(handle_errorno["handle_errno_time_out"], "call handle timeout")
            finally:
                signal.alarm(0)
            return result

        return wraps(func)(wrapper)

    return decorator


#!/usr/bin/env python3

from bluetooth import *
from gpiozero import LED
from time import sleep
import json

ROOT = "/var/SSSS"
uuid = "4f63aea8-be86-4f54-b8c8-2f0c9d37b56b"
name = "Super Singularity Scouting Server"

# Get LED pins for GPIO stuff
led = LED(17)
err_led = LED(27)

def main():
    # Setup a socket for RFCOMM (serial over bluetooth)
    server_sock = BluetoothSocket(RFCOMM)
    server_sock.bind(("",PORT_ANY))

    # Listen on serial port 1
    server_sock.listen(1)
    port = server_sock.getsockname()[1]

    # Advertise the RFCOMM signal
    advertise_service(server_sock, name,
        service_id = uuid,
        service_classes = [ uuid, SERIAL_PORT_CLASS ],
        profiles = [ SERIAL_PORT_PROFILE ]
    )

    # Run the client
    # Keep JSON in memory!
    try:
        while True:
            print("\nWaiting for connection on RFCOMM port {}".format(port))

            client_sock, client_info = server_sock.accept()
            print("Accepted connection from {}".format(client_info[0]))

            # Wait for one data packet
            try:
                data = client_sock.recv(1024)
                led.on()
                sleep(0.2)
                led.off()
                print("Data recieved: {}".format(data))
            except:
                print("Failed to recieve data over connection")
                continue

            #
            # MATCH DATA
            #

            # Open current JSON file for reading and load to current_data
            data_file = open("{}/matchData.json".format(ROOT), "r").read().rstrip()
            try:
                current_data = json.loads(data_file)
            except:
                print("Failed to load JSON. Is your JSON actually valid?")
                raise

            # Load JSON from bluetooth
            dataJSON = json.loads(data.decode("utf-8"))
            if len(dataJSON["matchData"]) < 1 and len(dataJSON["teamData"]) < 1:
                print("Got empty list")
                send_team_data(client_sock)
                print("Disconnecting...", end="")
                client_sock.close()
                print("OK")
                continue

            current_data.append(dataJSON["matchData"][0])

            # Write file
            if len(dataJSON["matchData"]) > 1:
                print("Writing to {}/matchData.json...".format(ROOT), end="")
                try:
                    match_data = open("{}/matchData.json".format(ROOT), "w")
                    match_data.write(json.dumps(current_data))
                    match_data.close()
                except:
                    clean_exit()
                    continue
                print("OK")

            #
            # TEAM DATA
            #

            # Read team data
            team_data = open("{}/teamData.json".format(ROOT), "r").read().rstrip()
            try:
                current_data = json.loads(team_data)
            except:
                print("Failed to load JSON. Is your JSON actually valid?")
                raise

            current_data.append(dataJSON["teamData"])

            # Write team data
            if len(dataJSON["teamData"]) > 1:
                print("Writing to {}/teamData.json...".format(ROOT), end="")
                try:
                    team_data_file = open("{}/teamData.json".format(ROOT), "w")
                    team_data_file.write(json.dumps(current_data))
                    team_data_file.close()
                except:
                    clean_exit()
                    continue
                print("OK")

            print("Sending data...", end="")
            try:
                send_team_data(client_sock)
            except:
                print("FAILED!")
            print("OK")

            print("Disconnecting...", end="")
            client_sock.close()
            print("OK")

    except KeyboardInterrupt:
        server_sock.close()
        print("\nClosed connection")
    except:
        server_sock.close()
        print("An error occurred!")
        raise

    print("Finished")

def send_team_data(client_sock):
    team_data = open("{}/teamData.json".format(ROOT), "r")
    client_sock.send(team_data.read())
    team_data.close()

def clean_exit():
    err_led.on()
    print("FAILED")
    print("Disconnecting...", end="")
    client_sock.close()
    print("OK")

if __name__ == "__main__":
    main()


import numpy as np
import multiprocessing

def allocate(i, j):
    return (j*10, i*20)

fi = lambda i: i*10

if __name__=="__main__":
    pool = multiprocessing.Pool(processes=2)

    N = 100
    arr = np.zeros(N)

    r = []
    try:
        # r = [pool.apply_async(allocate, (i,), callback=callback) for i in range(N)]
        for i in range(N):
            # calli = lambda j: callback(i,j)
            def calli(res, i=i):
                print(res)
                arr[i] = res[1]
            j = np.ones(5)
            r.append(pool.apply_async(allocate, (i,j,), callback=calli))
        pool.close()
        for item in r:
            item.wait(timeout=111) # Without a timeout, you can't interrupt this.
    except KeyboardInterrupt:
        pool.terminate()
    finally:
        pool.join()

    print(arr)


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

    def __repr__(self):
        return str(self.data)

class Stack:
    def __init__(self):
        self.top = None
        self.size = 0

    def push(self, data):
        node = Node(data)
        if self.top is None:
            self.top = node

        else:
            node.next = self.top
            self.top = node

        self.size += 1

    def pop(self):
        if self.top is None:
            print("List is Empty !!!")
            return None

        else:
            data = self.top.data
            self.size -= 1
            if self.top.next:
                self.top = self.top.next
            else:
                self.top = None
            return data

    def peek(self):
        if self.top:
            return self.top.data
        else:
            return None

################################################################################3
class TreeNode:
    def __init__(self, data = None):
        self.data = data
        self.right = None
        self.left = None

    def __repr__(self):
        return str(self.data)

expr = "4 5 + 5 3 - *".split()
stack = Stack()

for term in expr:
    if term in "+-*/":
        node = TreeNode(term)
        node.right = stack.pop()
        node.left = stack.pop()
    else:
        node = TreeNode(int(term))
    stack.push(node)

def calc(node):
    if node.data is "+":
        return calc(node.left) + calc(node.right)
    elif node.data is "-":
        return calc(node.left) - calc(node.right)
    elif node.data is "*":
        return calc(node.left) * calc(node.right)
    elif node.data is "/":
        return calc(node.left) / calc(node.right)
    else:
        return node.data

root = stack.pop()
result = calc(root)
print(result)


# -----------------------------------------
# -  Input deck for realistic beam size   -
# -----------------------------------------

import numpy as np
from input.utils import *
from input.misc import *

####################### Preprocessing ##########################

run_name = "test_performance_kernel"

gamma=1000
# Ct=2
delta_alpha_B = 0.2  # assume the bending section spreads over \Delta\alpha=0.2
l_beam = 3000        # um
r_beam = 50          # um
delta_alpha_tilde = 10000 #gamma * delta_alpha_B / 2
delta_chi_tilde = 2 * r_beam * 1e-6 * gamma**2.0

print("delta_alpha_tilde={}, delta_chi_tilde={}".format(delta_alpha_tilde, delta_chi_tilde))

mesh_alpha_npt = 100 #int(delta_alpha_tilde/1.0+1.0)
mesh_chi_npt = 100 #int(delta_chi_tilde/1.0+1.0)

if not mesh_alpha_npt%2:
    mesh_alpha_npt += 1
if not mesh_chi_npt%2:
    mesh_chi_npt += 1

print("mesh_alpha_npt={}, mesh_chi_npt={}".format(mesh_alpha_npt, mesh_chi_npt))

Nw = 2000

####################### Main setup ##########################

# --- kernel mesh info ---
num_wavefronts = 2000              # number of wavefronts
num_dirs = 100                     # number of field lines
num_step = num_wavefronts          # number of steps
dt = delta_alpha_B/num_wavefronts  # time step in electron rest frame
# dt=Ct/(gamma*Nw)
remap_interval = num_step - 1      # interval of doing remapping (in time steps)
remap_scatter = False              # use scatter weights form for remap
remap_scaling[0] = 1.0             # support/smoothing length scaling factor
remap_scaling[1] = 1.0             # support/smoothing length scaling factor

# --- eletron beam info ---
num_particles = 100                  # number of particles
trajectory_type = 2                # 1: straight line, 2: circular, 3: sinusoidal
parameters[0] = gamma              # parameters for describing the motion of electron
parameters[1] = 1

def init_beam(npart, gamma, delta_gamma, emittance):
    import numpy as np
    # [x, y, px, py]
    part = np.zeros([npart,4])
    part[:,1] = 1.0                # set y0=1
    return (part)

beam = init_beam(num_particles, 10.0, 0.0, 0.0)
del beam                           # init a single particle instead

# --- comoving mesh info ---
num_gridpt_hor = mesh_alpha_npt                # number of points in x-axis
num_gridpt_ver = mesh_chi_npt                  # number of points in y-axis
mesh_span_angle = delta_alpha_tilde/gamma**3   # in radians
mesh_width = delta_chi_tilde/gamma**2          # in unit of radius

# --- wavelets ---
def load_wavelets(filename, assign_field_value=True) :
    import numpy as np
    data = np.genfromtxt(filename, delimiter=",") # read CSV file
    #data = dat[:1000,:]
    nwavelets = data.shape[0]
    field = np.zeros(nwavelets)

    def get_field(pos):
        x = pos[:,0]
        y = pos[:,1]
        ymax = y.max()
        ymin = y.min()
        print("ymin = {}, ymax = {}".format(ymin, ymax))
        return np.cos(5.0*y*2*np.pi/(ymax-ymin))   # test case, assign test field of cos(x)

    if (assign_field_value) :
        field = get_field(data[:,:2])

    # for val in field:
    #     print("exact_value = {}".format(val))

    # ensure we pass different array addresses for wavelet x,y coordinate
    wx = data[:,0].copy()
    wy = data[:,1].copy()
    return wx, wy, field

path = "../input/wavelets/analytic_test/wavelets_adaptative.csv"
#wavelet_x, wavelet_y, wavelet_field = load_wavelets(path)
## unscale if necessary
#wavelet_x /= gamma**3.0
#wavelet_y /= gamma**2.0
#wavelet_field *= gamma**4.0
del wavelet_x, wavelet_y, wavelet_field
min_emit_angle = 10.0
wavelet_type = 1   # 0: use global (x,y) coordinate; 1: use local (x',y') coordinate; 2: (TODO) use local cylindrical coordinate
use_wavelet_for_subcycle = False # True: loaded wavelets will be repeatedly emitted at each step and copied into internal wavelets array, otherwise only used when interpolation is done and not copied into internal wavelets array
num_wavelet_fields = 1

if (wavelet_type == 0) :
    # convert to global (x,y) coordinate
    wavelet_x_prime = wavelet_x.copy()
    wavelet_y_prime = wavelet_y + 1.0
    rotation_angle = 0.19989890421779444  # positive for counter clock-wise rotation
    wavelet_x = wavelet_x_prime*np.cos(rotation_angle) + wavelet_y_prime*np.sin(rotation_angle)
    wavelet_y = -wavelet_x_prime*np.sin(rotation_angle) + wavelet_y_prime*np.cos(rotation_angle)

# post-process options
postprocess = False
field_func = "cos(5.*y*6.28318530718/100)"
field_scale = (gamma**4.0)
coord_scale[0] = (gamma**3.0)
coord_scale[1] = (gamma**2.0)
coord_shift[0] = 0.0
coord_shift[1] = 0.0
file_exact = "data/exact_values.csv"
file_error = "data/error_map.csv"

####################### Diagnostics ##########################
print_interval = 100        # interval for printing simulation steps
beam_output_interval = num_step - 1
mesh_output_interval = num_step - 1
wavelet_output_interval = num_step - 1
beam_output = True

if (mpi_rank==0):
    make_output_dirs(run_name+"/beam", num_step, beam_output_start, beam_output_interval)
    mesh_output = True
    make_output_dirs(run_name+"/mesh", num_step, mesh_output_start, mesh_output_interval)
    wavelet_output = True
    make_output_dirs(run_name+"/wavelet", num_step, wavelet_output_start, wavelet_output_interval)
    make_output_dirs(run_name+"/traj", num_step, wavelet_output_start, wavelet_output_interval)


#!/usr/bin/env python3

# https://github.com/benjaminjack/python_cpp_example/

import os
import re
import sys
import sysconfig
import platform
import subprocess
import unittest

from distutils.version import LooseVersion
from setuptools import setup, Extension, find_packages
from setuptools.command.build_ext import build_ext
from setuptools.command.test import test as TestCommand
from shutil import copyfile, copymode

class CMakeExtension(Extension):
    def __init__(self, name, sourcedir=''):
        Extension.__init__(self, name, sources=[])
        self.sourcedir = os.path.abspath(sourcedir)

class CMakeBuild(build_ext):
    def run(self):
        try:
            out = subprocess.check_output(['cmake', '--version'])
        except OSError:
            raise RuntimeError(
                "CMake must be installed to build the following extensions: " +
                ", ".join(e.name for e in self.extensions))

        if platform.system() == "Windows":
            cmake_version = LooseVersion(re.search(r'version\s*([\d.]+)',
                                         out.decode()).group(1))
            if cmake_version < '3.1.0':
                raise RuntimeError("CMake >= 3.1.0 is required on Windows")

        for ext in self.extensions:
            self.build_extension(ext)

    def build_extension(self, ext):
        extdir = os.path.abspath(
            os.path.dirname(self.get_ext_fullpath(ext.name)))
        cmake_args = ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + extdir,
                      '-DPYTHON_EXECUTABLE=' + sys.executable]

        cfg = 'Debug' if self.debug else 'Release'
        build_args = ['--config', cfg]

        if platform.system() == "Windows":
            cmake_args += ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{}={}'.format(
                cfg.upper(),
                extdir)]
            if sys.maxsize > 2**32:
                cmake_args += ['-A', 'x64']
            build_args += ['--', '/m']
        else:
            cmake_args += ['-DCMAKE_BUILD_TYPE=' + cfg]
            build_args += ['--', '-j4']

        env = os.environ.copy()
        env['CXXFLAGS'] = '{} -DVERSION_INFO=\\"{}\\"'.format(
            env.get('CXXFLAGS', ''),
            self.distribution.get_version())
        if not os.path.exists(self.build_temp):
            os.makedirs(self.build_temp)
        subprocess.check_call(['cmake', ext.sourcedir] + cmake_args,
                              cwd=self.build_temp, env=env)
        subprocess.check_call(['cmake', '--build', '.'] + build_args,
                              cwd=self.build_temp)
        print()  # Add an empty line for cleaner output

def test_suite():
    test_loader = unittest.TestLoader()
    test_suite = test_loader.discover('tests', pattern='test_*.py')
    return test_suite

setup(
    name='treeck',
    version='0.1',
    author='Laurens Devos',
    author_email='',
    description='Treeck: Reason about potential instances of additive tree ensembles',
    long_description='',
    packages=find_packages('src/python'),
    install_requires=[
      "z3-solver",
      "dask",
      "distributed"
    ],
    #extra_objects=["libtreeck.so"],
    package_dir={'':'src/python'},
    ext_modules=[CMakeExtension('treeck/treeck')],
    cmdclass=dict(build_ext=CMakeBuild),
    test_suite='setup.test_suite',
    zip_safe=False,
)


def tes(l, t, k):
    e = 1
    y = t
    t = l ** 0.5
    c = int(t)
    if l % t == 0:
        e -= c
    while y <= c:
        if l % y == 0:
            e += y + (l // y)
        y += k
    return e

def divider(a):
    if a % 2 == 0:
        return tes(a, 2, 1)
    else:
        return tes(a, 3, 2)

for i in range(int(input())):
    print(divider(int(input())))


'stock trading algorithms for ibx project'

import ibx
from common import *
import market

stock_buy_list = ['AAPL', ]
stock_buy_value = 1000

def buy_stock(stocks, sectors):
    stock = stock_buy_list[0]
    try:
        price = stocks.loc[stock].price
    except:
        return
    shares = (stock_buy_value // price) + 1
    print(f'default handler buying {shares} shares of {stock}')
    ib.Buy(stock, shares)

def random_handler(stocks, sectors):
    'example random algo'

    # finish function is only called if the algo is chosen
    def finish(portfolio, prev_state):
        print('random algo selected')
        if portfolio.empty:
            buy_stock(stocks, sectors)
        else:
            for ticker, position in portfolio.iterrows():
                if position.pnl < -7:
                    print(f'heavy loss: liquidating {ticker}')
                    market.ib.Sell(ticker)
                elif position.pnl > 4:
                    print(f'profit goal attained: liquidating {ticker}')
                    market.ib.Sell(ticker)
                else:
                    print(f'position okay: no action taken for {ticker}')

    score = 0
    pslope = sectors.at['Total', '%pslope']
    if pslope >= 45 and pslope <= 65:
        score = 1
    print(f'random score: {score}')
    return score, finish

def up_handler(stocks, sectors):
    def finish(portfolio, prev_state):
        print('up algo selected')
        if portfolio.empty:
            buy_stock(stocks, sectors)
        else:
            for ticker, position in portfolio.iterrows():
                if position.pnl < -7:
                    print(f'heavy loss: liquidating {ticker}')
                    market.ib.Sell(ticker)
                elif position.pnl > 4:
                    print(f'profit goal attained: liquidating {ticker}')
                    market.ib.Sell(ticker)
                else:
                    print(f'position okay: no action taken for {ticker}')

    score = 0
    pslope = sectors.at['Total', '%pslope']
    if pslope > 65:
        score = 1
    print(f'up score: {score}')
    return score, finish

def down_handler(stocks, sectors):
    def finish(portfolio, prev_state):
        print('down algo selected')
        if portfolio.empty:
            buy_stock(stocks, sectors)
        else:
            for ticker, position in portfolio.iterrows():
                if position.pnl < -7:
                    print(f'heavy loss: liquidating {ticker}')
                    market.ib.Sell(ticker)
                elif position.pnl > 4:
                    print(f'profit goal attained: liquidating {ticker}')
                    market.ib.Sell(ticker)
                else:
                    print(f'position okay: no action taken for {ticker}')

    score = 0
    pslope = sectors.at['Total', '%pslope']
    if pslope < 65:
        score = 1
    print(f'down score: {score}')
    return score, finish

def stupid_handler(stocks, sectors):
    def finish(portfolio, prev_state):
        print('stupid algo selected')
        if portfolio.empty:
            ticker = stocks.index[-2] # select ticker with lowest slope!
            price = stocks.loc[ticker].price
            shares = (stock_buy_value // price) + 1
            print(f'stupid handler buying {shares} shares of {ticker}')
            market.ib.Buy(ticker, shares)
        else:
            for ticker, position in portfolio.iterrows():
                if position.pnl < -7:
                    print(f'heavy loss: keeping {ticker} anyway')
                    market.ib.Sell(ticker)
                elif position.pnl > 4:
                    print(f'profit goal attained: liquidating {ticker}')
                    market.ib.Sell(ticker)
                else:
                    print(f'position okay: no action taken for {ticker}')

    score = 0.5
    print(f'stupid score: {score}')
    return score, finish

algos = {
    'random': random_handler,
    'up': up_handler,
    'down': down_handler,
    'stupid': stupid_handler }

def launch(stocks, sectors):
    'score each algo and run the highest scored algo if score > 0'
    results = []
    print(f'\nPrevious market state: {launch.prev_state}')
    for algo in algos:
        # append a tuple for each algo: ( score, function )
        results.append( algos[algo](stocks, sectors) + (algo,) )

    score, func, name = sorted(results, key=lambda x: x[0])[-1] # get the hightest scored tuple
    if score > 0: # make sure score > 0
        portfolio = ib.GetPortfolio(False)
        func(portfolio, launch.prev_state) # finish the algo by calling function
        launch.prev_state = name
launch.prev_state = None


# Copyright (c), Google Inc, 2017
# Simplified BSD License (see licenses/simplified_bsd.txt or https://opensource.org/licenses/BSD-2-Clause)

try:
    import requests
    HAS_REQUESTS = True
except ImportError:
    HAS_REQUESTS = False

try:
    import google.auth
    import google.auth.compute_engine
    from google.oauth2 import service_account
    from google.auth.transport.requests import AuthorizedSession
    HAS_GOOGLE_LIBRARIES = True
except ImportError:
    HAS_GOOGLE_LIBRARIES = False

from ansible.module_utils.basic import AnsibleModule, env_fallback
from ansible.module_utils.six import string_types
from ansible.module_utils._text import to_text, to_native
import ast
import os
import json

def navigate_hash(source, path, default=None):
    if not source:
        return None

    key = path[0]
    path = path[1:]
    if key not in source:
        return default
    result = source[key]
    if path:
        return navigate_hash(result, path, default)
    else:
        return result

class GcpRequestException(Exception):
    pass

def remove_nones_from_dict(obj):
    new_obj = {}
    for key in obj:
        value = obj[key]
        if value is not None and value != {} and value != []:
            new_obj[key] = value

    # Blank dictionaries should return None or GCP API may complain.
    if not new_obj:
        return None
    return new_obj

# Handles the replacement of dicts with values -> the needed value for GCP API
def replace_resource_dict(item, value):
    if isinstance(item, list):
        items = []
        for i in item:
            items.append(replace_resource_dict(i, value))
        return items
    else:
        if not item:
            return item
        else:
            return item.get(value)

# Handles all authentication and HTTP sessions for GCP API calls.
class GcpSession(object):
    def __init__(self, module, product):
        self.module = module
        self.product = product
        self._validate()

    def get(self, url, body=None, **kwargs):
        """
        This method should be avoided in favor of full_get
        """
        kwargs.update({'json': body})
        return self.full_get(url, **kwargs)

    def post(self, url, body=None, headers=None, **kwargs):
        """
        This method should be avoided in favor of full_post
        """
        kwargs.update({'json': body, 'headers': headers})
        return self.full_post(url, **kwargs)

    def post_contents(self, url, file_contents=None, headers=None, **kwargs):
        """
        This method should be avoided in favor of full_post
        """
        kwargs.update({'data': file_contents, 'headers': headers})
        return self.full_post(url, **kwargs)

    def delete(self, url, body=None):
        """
        This method should be avoided in favor of full_delete
        """
        kwargs = {'json': body}
        return self.full_delete(url, **kwargs)

    def put(self, url, body=None):
        """
        This method should be avoided in favor of full_put
        """
        kwargs = {'json': body}
        return self.full_put(url, **kwargs)

    def patch(self, url, body=None, **kwargs):
        """
        This method should be avoided in favor of full_patch
        """
        kwargs.update({'json': body})
        return self.full_patch(url, **kwargs)

    def list(self, url, callback, params=None, array_name='items',
             pageToken='nextPageToken', **kwargs):
        """
        This should be used for calling the GCP list APIs. It will return
        an array of items

        This takes a callback to a `return_if_object(module, response)`
        function that will decode the response + return a dictionary. Some
        modules handle the decode + error processing differently, so we should
        defer to the module to handle this.
        """
        resp = callback(self.module, self.full_get(url, params, **kwargs))
        items = resp.get(array_name) if resp.get(array_name) else []
        while resp.get(pageToken):
            if params:
                params['pageToken'] = resp.get(pageToken)
            else:
                params = {'pageToken': resp[pageToken]}

            resp = callback(self.module, self.full_get(url, params, **kwargs))
            if resp.get(array_name):
                items = items + resp.get(array_name)
        return items

    # The following methods fully mimic the requests API and should be used.
    def full_get(self, url, params=None, **kwargs):
        kwargs['headers'] = self._set_headers(kwargs.get('headers'))
        try:
            return self.session().get(url, params=params, **kwargs)
        except getattr(requests.exceptions, 'RequestException') as inst:
            # Only log the message to avoid logging any sensitive info.
            self.module.fail_json(msg=inst.message)

    def full_post(self, url, data=None, json=None, **kwargs):
        kwargs['headers'] = self._set_headers(kwargs.get('headers'))

        try:
            return self.session().post(url, data=data, json=json, **kwargs)
        except getattr(requests.exceptions, 'RequestException') as inst:
            self.module.fail_json(msg=inst.message)

    def full_put(self, url, data=None, **kwargs):
        kwargs['headers'] = self._set_headers(kwargs.get('headers'))

        try:
            return self.session().put(url, data=data, **kwargs)
        except getattr(requests.exceptions, 'RequestException') as inst:
            self.module.fail_json(msg=inst.message)

    def full_patch(self, url, data=None, **kwargs):
        kwargs['headers'] = self._set_headers(kwargs.get('headers'))

        try:
            return self.session().patch(url, data=data, **kwargs)
        except getattr(requests.exceptions, 'RequestException') as inst:
            self.module.fail_json(msg=inst.message)

    def full_delete(self, url, **kwargs):
        kwargs['headers'] = self._set_headers(kwargs.get('headers'))

        try:
            return self.session().delete(url, **kwargs)
        except getattr(requests.exceptions, 'RequestException') as inst:
            self.module.fail_json(msg=inst.message)

    def _set_headers(self, headers):
        if headers:
            return self._merge_dictionaries(headers, self._headers())
        else:
            return self._headers()

    def session(self):
        return AuthorizedSession(
            self._credentials())

    def _validate(self):
        if not HAS_REQUESTS:
            self.module.fail_json(msg="Please install the requests library")

        if not HAS_GOOGLE_LIBRARIES:
            self.module.fail_json(msg="Please install the google-auth library")

        if self.module.params.get('service_account_email') is not None and self.module.params['auth_kind'] != 'machineaccount':
            self.module.fail_json(
                msg="Service Account Email only works with Machine Account-based authentication"
            )

        if (self.module.params.get('service_account_file') is not None or
                self.module.params.get('service_account_contents') is not None) and self.module.params['auth_kind'] != 'serviceaccount':
            self.module.fail_json(
                msg="Service Account File only works with Service Account-based authentication"
            )

    def _credentials(self):
        cred_type = self.module.params['auth_kind']
        if cred_type == 'application':
            credentials, project_id = google.auth.default(scopes=self.module.params['scopes'])
            return credentials
        elif cred_type == 'serviceaccount' and self.module.params.get('service_account_file'):
            path = os.path.realpath(os.path.expanduser(self.module.params['service_account_file']))
            return service_account.Credentials.from_service_account_file(path).with_scopes(self.module.params['scopes'])
        elif cred_type == 'serviceaccount' and self.module.params.get('service_account_contents'):
            try:
                cred = json.loads(self.module.params.get('service_account_contents'))
            except json.decoder.JSONDecodeError as e:
                self.module.fail_json(
                    msg="Unable to decode service_account_contents as JSON"
                )
            return service_account.Credentials.from_service_account_info(cred).with_scopes(self.module.params['scopes'])
        elif cred_type == 'machineaccount':
            return google.auth.compute_engine.Credentials(
                self.module.params['service_account_email'])
        else:
            self.module.fail_json(msg="Credential type '%s' not implemented" % cred_type)

    def _headers(self):
        if self.module.params.get('env_type'):
            return {
                'User-Agent': "Google-Ansible-MM-{0}-{1}".format(self.product, self.module.params.get('env_type'))
            }
        else:
            return {
                'User-Agent': "Google-Ansible-MM-{0}".format(self.product)
            }

    def _merge_dictionaries(self, a, b):
        new = a.copy()
        new.update(b)
        return new

class GcpModule(AnsibleModule):
    def __init__(self, *args, **kwargs):
        arg_spec = {}
        if 'argument_spec' in kwargs:
            arg_spec = kwargs['argument_spec']

        kwargs['argument_spec'] = self._merge_dictionaries(
            arg_spec,
            dict(
                project=dict(
                    required=False,
                    type='str',
                    fallback=(env_fallback, ['GCP_PROJECT'])),
                auth_kind=dict(
                    required=True,
                    fallback=(env_fallback, ['GCP_AUTH_KIND']),
                    choices=['machineaccount', 'serviceaccount', 'application'],
                    type='str'),
                service_account_email=dict(
                    required=False,
                    fallback=(env_fallback, ['GCP_SERVICE_ACCOUNT_EMAIL']),
                    type='str'),
                service_account_file=dict(
                    required=False,
                    fallback=(env_fallback, ['GCP_SERVICE_ACCOUNT_FILE']),
                    type='path'),
                service_account_contents=dict(
                    required=False,
                    fallback=(env_fallback, ['GCP_SERVICE_ACCOUNT_CONTENTS']),
                    no_log=True,
                    type='jsonarg'),
                scopes=dict(
                    required=False,
                    fallback=(env_fallback, ['GCP_SCOPES']),
                    type='list'),
                env_type=dict(
                    required=False,
                    fallback=(env_fallback, ['GCP_ENV_TYPE']),
                    type='str')
            )
        )

        mutual = []
        if 'mutually_exclusive' in kwargs:
            mutual = kwargs['mutually_exclusive']

        kwargs['mutually_exclusive'] = mutual.append(
            ['service_account_email', 'service_account_file', 'service_account_contents']
        )

        AnsibleModule.__init__(self, *args, **kwargs)

    def raise_for_status(self, response):
        try:
            response.raise_for_status()
        except getattr(requests.exceptions, 'RequestException') as inst:
            self.fail_json(msg="GCP returned error: %s" % response.json())

    def _merge_dictionaries(self, a, b):
        new = a.copy()
        new.update(b)
        return new

# This class does difference checking according to a set of GCP-specific rules.
# This will be primarily used for checking dictionaries.
# In an equivalence check, the left-hand dictionary will be the request and
# the right-hand side will be the response.

# Rules:
# Extra keys in response will be ignored.
# Ordering of lists does not matter.
#   - exception: lists of dictionaries are
#     assumed to be in sorted order.
class GcpRequest(object):
    def __init__(self, request):
        self.request = request

    def __eq__(self, other):
        return not self.difference(other)

    def __ne__(self, other):
        return not self.__eq__(other)

    # Returns the difference between a request + response.
    # While this is used under the hood for __eq__ and __ne__,
    # it is useful for debugging.
    def difference(self, response):
        return self._compare_value(self.request, response.request)

    def _compare_dicts(self, req_dict, resp_dict):
        difference = {}
        for key in req_dict:
            if resp_dict.get(key):
                difference[key] = self._compare_value(req_dict.get(key), resp_dict.get(key))

        # Remove all empty values from difference.
        sanitized_difference = {}
        for key in difference:
            if difference[key]:
                sanitized_difference[key] = difference[key]

        return sanitized_difference

    # Takes in two lists and compares them.
    # All things in the list should be identical (even if a dictionary)
    def _compare_lists(self, req_list, resp_list):
        # Have to convert each thing over to unicode.
        # Python doesn't handle equality checks between unicode + non-unicode well.
        difference = []
        new_req_list = self._convert_value(req_list)
        new_resp_list = self._convert_value(resp_list)

        # We have to compare each thing in the request to every other thing
        # in the response.
        # This is because the request value will be a subset of the response value.
        # The assumption is that these lists will be small enough that it won't
        # be a performance burden.
        for req_item in new_req_list:
            found_item = False
            for resp_item in new_resp_list:
                # Looking for a None value here.
                if not self._compare_value(req_item, resp_item):
                    found_item = True
            if not found_item:
                difference.append(req_item)

        difference2 = []
        for value in difference:
            if value:
                difference2.append(value)

        return difference2

    # Compare two values of arbitrary types.
    def _compare_value(self, req_value, resp_value):
        diff = None
        # If a None is found, a difference does not exist.
        # Only differing values matter.
        if not resp_value:
            return None

        # Can assume non-None types at this point.
        try:
            if isinstance(req_value, list):
                diff = self._compare_lists(req_value, resp_value)
            elif isinstance(req_value, dict):
                diff = self._compare_dicts(req_value, resp_value)
            elif isinstance(req_value, bool):
                diff = self._compare_boolean(req_value, resp_value)
            # Always use to_text values to avoid unicode issues.
            elif to_text(req_value) != to_text(resp_value):
                diff = req_value
        # to_text may throw UnicodeErrors.
        # These errors shouldn't crash Ansible and should be hidden.
        except UnicodeError:
            pass

        return diff

    # Compare two boolean values.
    def _compare_boolean(self, req_value, resp_value):
        try:
            # Both True
            if req_value and isinstance(resp_value, bool) and resp_value:
                return None
            # Value1 True, resp_value 'true'
            elif req_value and to_text(resp_value) == 'true':
                return None
            # Both False
            elif not req_value and isinstance(resp_value, bool) and not resp_value:
                return None
            # Value1 False, resp_value 'false'
            elif not req_value and to_text(resp_value) == 'false':
                return None
            else:
                return resp_value

        # to_text may throw UnicodeErrors.
        # These errors shouldn't crash Ansible and should be hidden.
        except UnicodeError:
            return None

    # Python (2 esp.) doesn't do comparisons between unicode + non-unicode well.
    # This leads to a lot of false positives when diffing values.
    # The Ansible to_text() function is meant to get all strings
    # into a standard format.
    def _convert_value(self, value):
        if isinstance(value, list):
            new_list = []
            for item in value:
                new_list.append(self._convert_value(item))
            return new_list
        elif isinstance(value, dict):
            new_dict = {}
            for key in value:
                new_dict[key] = self._convert_value(value[key])
            return new_dict
        else:
            return to_text(value)


# uncompyle6 version 3.7.4
# Python bytecode 2.7 (62211)
# Decompiled from: Python 3.6.9 (default, Apr 18 2020, 01:56:04)
# [GCC 8.4.0]
# Embedded file name: build/bdist.macosx-10.9-x86_64/egg/astropysics/version.py
# Compiled at: 2014-05-05 14:42:11
version = '1.0'
major = 1
minor = 0
bugfix = 0
dev = False
release = not dev


'''
od2json.py : JSONでのresponse返却関連モジュール
'''
import json
from collections import OrderedDict
from django.http import HttpResponse

class OD2Json():
    def render_json_response(self, request, contents, message, status=None):
        '''
        response を JSON で返却

        :param request:  HTTPリクエストそのもの
        :param contents: JSONの中のcontentsというパラメータに入れるデータ
        :type  contents: OrderedDict
        :param message:  JSONの中のstatusというパラメータに入れるデータ
        :type  message:  String
        :return:         response - レスポンスデータ
        '''
        data = OrderedDict([
            ('status', message),
            ('contents', contents)
        ])
        json_str = json.dumps(data, ensure_ascii=False, indent=2)
        callback = request.GET.get('callback')
        if not callback:
            callback = request.REQUEST.get('callback')  # POSTでJSONPの場合
        if callback:
            json_str = "%s(%s)" % (callback, json_str)
            response = HttpResponse(
                json_str,
                content_type='application/javascript; charset=UTF-8',
                status=status
            )
        else:
            response = HttpResponse(
                json_str,
                content_type='application/json; charset=UTF-8',
                status=status
            )
        return response


import math
import torch
import hess
import hess.utils as utils
import hess.nets
import numpy as np
import pickle

import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision
import torchvision.transforms as transforms

from compute_loss_surface import get_loss_surface
from hess.utils import get_hessian_eigs
import matplotlib.pyplot as plt
from gpytorch.utils.lanczos import lanczos_tridiag, lanczos_tridiag_to_diag

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(3, 6, 5)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(6, 16, 5)
        self.fc1 = nn.Linear(16 * 5 * 5, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)

    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv2(x)))
        x = x.view(-1, 16 * 5 * 5)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

def main():
    use_cuda =  torch.cuda.is_available()

    model = Net()
    criterion = torch.nn.CrossEntropyLoss()

    if use_cuda:
        model = model.cuda()

    transform = transforms.Compose(
            [transforms.ToTensor(),
             transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])

    trainset = torchvision.datasets.CIFAR10(root='/datasets/cifar10/', train=True,
                                            download=True, transform=transform)
    trainloader = torch.utils.data.DataLoader(trainset, batch_size=128,
                                              shuffle=True, num_workers=2)

    ## Super Trainer ##
    optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
    for epoch in range(30):  # loop over the dataset multiple times

        running_loss = 0.0
        for i, data in enumerate(trainloader, 0):
            # get the inputs; data is a list of [inputs, labels]
            inputs, labels = data

            if use_cuda:
                inputs, labels = inputs.cuda(), labels.cuda()

            # zero the parameter gradients
            optimizer.zero_grad()

            # forward + backward + optimize
            outputs = model(inputs)
            loss = criterion(outputs, labels)
            loss.backward()
            optimizer.step()

            # print statistics
            running_loss += loss.item()
            if i % 100 == 99:    # print every 2000 mini-batches
                print('[%d, %5d] loss: %.3f' %
                      (epoch + 1, i + 1, running_loss / 100))
                running_loss = 0.0

    fpath = "./outputs/"
    fname = "saved_model.pt"
    torch.save(model.state_dict(), fpath + fname)

    evals, evecs = get_hessian_eigs(loss=criterion,
                         model=model, use_cuda=use_cuda, n_eigs=200,
                         loader=trainloader, evals=True)

    print("positive evals = ", evals)
    ## clean these guys up ##
    keep = np.where(evals.cpu() != 1)
    evals = evals[keep].squeeze()
    evecs = evecs[:, keep].squeeze()

    fname = "top_evecs.pt"
    torch.save(evecs, fpath + fname)
    fname = "top_evals.pt"
    torch.save(evals, fpath + fname)

#     high_loss = get_loss_surface(pos_evecs, model, trainloader,
#                             criterion, rng=1., n_pts=25, use_cuda=use_cuda)
#     fname = "high_loss.pt"
#     torch.save(high_loss, fpath + fname)

#     low_loss = get_loss_surface(neg_evecs, model, trainloader,
#                             criterion, rng=1., n_pts=25, use_cuda=use_cuda)
#     fname = "low_loss.pt"
#     torch.save(low_loss, fpath + fname)

#     n_pars = sum(p.numel() for p in model.parameters())
#     all_loss = get_loss_surface(torch.eye(n_pars), model, trainloader,
#                             criterion, rng=1., n_pts=25, use_cuda=use_cuda)
#     fname = "all_loss.pt"
#     torch.save(all_loss, fpath + fname)

if __name__ == '__main__':
    main()


fname = input("Enter file name: ")
file=open(fname,'r')
count=dict()
lst=list()
emailname=None
emailnumber=None

for x in file:
	line=x.rstrip()
	if line.startswith('From '):
		words=line.split()
		lst.append(words[1])

for email in lst:
	count[email]= count.get(email,0)+1

for email,count in count.items():
	if emailnumber is None or count > emailnumber:
		emailname= email
		emailnumber=count

print(emailname,emailnumber)


'''

CNN+GRU

1. CNNEncoder: CNN feature extraction net

2. CNNClassifier: Simple CNN classifier to train CNNEncoder

3. CGNN: Final net to apply CNNEncoder and BiGRU
'''
from keras.models import Model, Sequential
from keras.layers import Conv2D, Dense, Input, Dropout, Flatten, GRU, MaxPool2D, Activation, BatchNormalization

def CNNEncoder(input_shape):
    model = Sequential()
    model.add(BatchNormalization(axis=1, name='bn_0'))

    # Conv block 1
    model.add(Conv2D(64, (3,3), strides=(1, 1), padding='same', name='conv_1'))
    model.add(BatchNormalization(axis=1, name='bn_1'))
    model.add(Activation('elu'))
    model.add(MaxPool2D(pool_size=(4, 2), strides=(4,2), name='pool_1'))
    model.add(Dropout(0.2, name='dropout_1'))

    # Conv block2
    model.add(Conv2D(128, (3,3), strides=(1, 1), padding='same', name='conv_2'))
    model.add(BatchNormalization(axis=1, name='bn_2'))
    model.add(Activation('elu'))
    model.add(MaxPool2D(pool_size=(5, 2), strides=(5,2), name='pool_2'))
    model.add(Dropout(0.2, name='dropout_2'))

    # Conv block3
    model.add(Conv2D(128, (3,3), strides=(1, 1), padding='same', name='conv_3'))
    model.add(BatchNormalization(axis=1, name='bn_3'))
    model.add(Activation('elu'))
    model.add(MaxPool2D(pool_size=(5, 4), strides=(5,4), name='pool_3'))
    model.add(Dropout(0.2, name='dropout_3'))

    # Conv block3
    model.add(Conv2D(128, (3,3), strides=(1, 1), padding='same', name='conv_4'))
    model.add(BatchNormalization(axis=1, name='bn_4'))
    model.add(Activation('elu'))
    model.add(MaxPool2D(pool_size=(5, 4), strides=(5,4), name='pool_4'))
    model.add(Dropout(0.2, name='dropout_4'))

    model.add(Flatten())

    return model

def CNNClassifier(input_shape):
    input = Input(input_shape)
    x = CNNEncoder(input_shape)(input)
    x = Dense(1, activation='relu')(x)
    model = Model(input,x)
    model.compile('Adam', 'mse', metrics='acc')
    return model

def CGNN(input_shape):
    input = Input(input_shape)
    x = CNNEncoder(input_shape)(input)
    Model(input, x)


#!/usr/bin/python
# Solved by Bogdan Trif @       Completed on Fri, 6 Jan 2017, 21:42
#The  Euler Project  https://projecteuler.net
'''
Special subset sums: testing        -       Problem 105

Let S(A) represent the sum of elements in set A of size n.
We shall call it a special sum set if for any two non-empty disjoint subsets, B and C, the following properties are true:

1.      S(B) ≠ S(C); that is, sums of subsets cannot be equal.
2.      If B contains more elements than C then S(B) > S(C).

For example, {81, 88, 75, 42, 87, 84, 86, 65} is not a special sum set because 65 + 87 + 88 = 75 + 81 + 84,
whereas {157, 150, 164, 119, 79, 159, 161, 139, 158} satisfies both rules for all possible subset pair combinations and S(A) = 1286.

Using sets.txt (right click and "Save Link/Target As..."), a 4K text file with one-hundred sets containing
seven to twelve elements (the two examples given above are the first two sets in the file),
identify all the special sum sets, A1, A2, ..., Ak, and find the value of S(A1) + S(A2) + ... + S(Ak).

NOTE: This problem is related to Problem 103 and Problem 106.

'''
import time
from itertools import combinations

filename = "pb105_sets.txt"
def load_file(filename):
    with open(filename) as f:
        matrix = [list(map(int, line.split(","))) for line in f.readlines()]
    f.close()
    return matrix

print(load_file(filename ))

def isSpecialSumSet(s):
    uSet = set(s)
    for i in range(2,len(s)):
        maxSet = max(uSet)
        for a in combinations(s, i):
            ss = sum(a)
            if ss <= maxSet or ss in uSet: return False
            else: uSet.add(ss)
    return True

F = load_file(filename )

print('\n--------------------------TESTS------------------------------')

print('\n================  My FIRST SOLUTION,  VERY FAST, 40 ms ===============\n')
t1  = time.time()

def solution_pb105() :
    cnt , Set_sum = 0, 0
    for i in range(len(F)):
        S = F[i]
        if isSpecialSumSet(S) == True :
            cnt+=1
            Set_sum += sum(S)
            print(str(cnt)+'.   '  ,S)

    return print('\nAnswer : \t', Set_sum )

solution_pb105()                #   Answer : 	 73702

t2  = time.time()
print('\nCompleted in :', round((t2-t1)*1000,6), 'ms\n\n')                  #       Completed in : 49.002647 ms

# print('\n===============OTHER SOLUTIONS FROM THE EULER FORUM ==============')
# print('\n--------------------------SOLUTION 1,   --------------------------')
# t1  = time.time()
#
#
#
# t2  = time.time()
# print('\nCompleted in :', round((t2-t1)*1000,6), 'ms\n\n')
#
# print('\n--------------------------SOLUTION 2,   --------------------------')
# t1  = time.time()
#
#
#
# t2  = time.time()
# print('\nCompleted in :', round((t2-t1)*1000,6), 'ms\n\n')
#
# print('\n--------------------------SOLUTION 3,   --------------------------')
# t1  = time.time()
#
#
#
# t2  = time.time()
# print('\nCompleted in :', round((t2-t1)*1000,6), 'ms\n\n')
#
# print('\n--------------------------SOLUTION 4,   --------------------------')
# t1  = time.time()
#
#
#
# t2  = time.time()
# print('\nCompleted in :', round((t2-t1)*1000,6), 'ms\n\n')
#
# print('\n--------------------------SOLUTION 5,   --------------------------')
# t1  = time.time()
#
#
#
# t2  = time.time()
# print('\nCompleted in :', round((t2-t1)*1000,6), 'ms\n\n')
#
#
# print('\n--------------------------SOLUTION 6,   --------------------------')
# t1  = time.time()
#
#
#
# t2  = time.time()
# print('\nCompleted in :', round((t2-t1)*1000,6), 'ms\n\n')
#

Project EULER/pb105 Special subset sums - testing.py


"""
-----------------------------------------------------------------------------------------
submit_fit_jobs.py
-----------------------------------------------------------------------------------------
Goal of the script:
Create jobscript to fit pRFs
-----------------------------------------------------------------------------------------
Input(s):
sys.argv[1]: subject name (e.g. 'sub-01')
sys.argv[2]: task (ex: GazeCenterFS)
sys.argv[3]: pre-processing steps (fmriprep_dct or fmriprep_dct_pca)
-----------------------------------------------------------------------------------------
Output(s):
.sh file to execute in server
-----------------------------------------------------------------------------------------
To run:
>> cd to function
>> python fit/submit_fit_fs_jobs.py [subject] [task] [preproc]
-----------------------------------------------------------------------------------------
Exemple:
cd /home/mszinte/projects/pRFgazeMod/mri_analysis/
python fit/submit_fit_jobs.py sub-001 GazeCenterFS fmriprep_dct_pca
python fit/submit_fit_jobs.py sub-001 AttendFixGazeCenterFS fmriprep_dct_pca
python fit/submit_fit_jobs.py sub-001 AttendStimGazeCenterFS fmriprep_dct_pca
-----------------------------------------------------------------------------------------
Written by Martin Szinte (martin.szinte@gmail.com)
-----------------------------------------------------------------------------------------
"""

# Stop warnings
# -------------
import warnings
warnings.filterwarnings("ignore")

# General imports
# ---------------
import numpy as np
import os
import glob
import json
import sys
import nibabel as nb
import platform
import ipdb
import datetime
deb = ipdb.set_trace
opj = os.path.join

# Settings
# --------
# Inputs
subject = sys.argv[1]
task = sys.argv[2]
preproc = sys.argv[3]

# Analysis parameters
with open('settings.json') as f:
    json_s = f.read()
    analysis_info = json.loads(json_s)

# Cluster settings
base_dir = analysis_info['base_dir']
sub_command = 'sbatch '
fit_per_hour = 6000.0
nb_procs = 32
memory_val = 48
proj_name = 'b161'

print("pRF analysis: running on Skylake")

# Create job and log output folders
try:
    os.makedirs(opj(base_dir, 'pp_data', subject, 'gauss', 'jobs'))
    os.makedirs(opj(base_dir, 'pp_data', subject, 'gauss', 'log_outputs'))
except:
    pass

# Determine data to analyse
data_file = "{base_dir}/pp_data/{sub}/func/{sub}_task-{task}_{preproc}_avg.nii.gz".format(
                        base_dir = base_dir, sub = subject, preproc = preproc, task = task)

img_data = nb.load(data_file)
data = img_data.get_fdata()
data_var = np.var(data,axis=3)
mask = data_var!=0.0
slices = np.arange(mask.shape[2])[mask.sum(axis=(0,1))>0]

for slice_nb in slices:

    num_vox = mask[:, :, slice_nb].sum()
    job_dur = str(datetime.timedelta(hours = np.ceil(num_vox/fit_per_hour)))

    # Define output file
    opfn = "{base_dir}/pp_data/{subject}/gauss/fit/{subject}_task-{task}_{preproc}_avg_est_z_{slice_nb}.nii.gz".format(
                                base_dir = base_dir,
                                subject = subject,
                                task = task,
                                preproc = preproc,
                                slice_nb = slice_nb)
    log_dir = opj(base_dir,'pp_data',subject,'gauss','log_outputs')

    if os.path.isfile(opfn):
        if os.path.getsize(opfn) != 0:
            print("output file {opfn} already exists and is non-empty. aborting analysis of slice {slice_nb}".format(
                                opfn = opfn,
                                slice_nb = slice_nb))
            continue

        # create job shell

    slurm_cmd = """\
#!/bin/bash
#SBATCH -p skylake
#SBATCH -A {proj_name}
#SBATCH --nodes=1
#SBATCH --mem={memory_val}gb
#SBATCH --cpus-per-task={nb_procs}
#SBATCH --time={job_dur}
#SBATCH -e {log_dir}/{subject}_{task}_{preproc}_fit_slice_{slice_nb}_%N_%j_%a.err
#SBATCH -o {log_dir}/{subject}_{task}_{preproc}_fit_slice_{slice_nb}_%N_%j_%a.out
#SBATCH -J {subject}_{task}_{preproc}_fit_slice_{slice_nb}\n\n""".format(proj_name = proj_name,
                                            nb_procs = nb_procs,
                                            memory_val = memory_val,
                                            log_dir = log_dir,
                                            job_dur = job_dur,
                                            subject = subject,
                                            preproc = preproc,
                                            task = task,
                                            slice_nb = slice_nb)

    # define fit cmd
    fit_cmd = "python fit/prf_fit.py {subject} {task} {preproc} {slice_nb} {opfn}".format(
                subject = subject,
                task = task,
                preproc = preproc,
                slice_nb = slice_nb,
                opfn = opfn)

    # create sh folder and file
    sh_dir = "{base_dir}/pp_data/{subject}/gauss/jobs/{subject}_{task}_{preproc}_fit_slice_{slice_nb}.sh".format(
                base_dir = base_dir,
                subject = subject,
                task = task,
                preproc = preproc,
                slice_nb = slice_nb)

    try:
        os.makedirs(opj(base_dir,'pp_data',subject,'gauss','fit'))
        os.makedirs(opj(base_dir,'pp_data',subject,'gauss','jobs'))
        os.makedirs(opj(base_dir,'pp_data',subject,'gauss','log_outputs'))
    except:
        pass

    of = open(sh_dir, 'w')
    of.write("{slurm_cmd}{fit_cmd}".format(slurm_cmd = slurm_cmd,fit_cmd = fit_cmd))
    of.close()

    # Submit jobs
    print("Submitting {sh_dir} to queue".format(sh_dir = sh_dir))
    os.system("{sub_command} {sh_dir}".format(sub_command = sub_command, sh_dir = sh_dir))


class Solution:
    def removeInvalidParentheses(self, s):
        left, right = self.calculate_invalid_parenthisis(s)
        print (left, right)
        ans = set()
        res_len = len(s) - left - right
        if res_len == 0:
            return [""]
        self.remove(s, left, right, ans, '', res_len)

        return ans

    def calculate_invalid_parenthisis(self, s):
        left = 0
        right = 0
        for i in range(0, len(s)):
            if s[i] == "(":
                left = left + 1
            if s[i] == ")":
                if left > 0:
                    left = left - 1
                else:
                    right = right + 1
        return left, right

    def remove(self, s, left, right, ans, res, res_len):
        if len(res) == res_len:
            new_left, new_right = self.calculate_invalid_parenthisis(res)
            if new_left ==0 and new_right == 0:
                ans.add(res)

        if len(s) == 0:
            return
        if len(s) == 1:
            if s[0] == "(":
                if left > 0:
                    # exclude
                    self.remove("", left - 1, right, ans, res, res_len)
                    # include
                    self.remove("", left, right, ans, res + s[0], res_len)
                else:
                    # include
                    self.remove("", left, right, ans, res + s[0], res_len)

            elif s[0] == ")":
                if right > 0:
                    # exclude
                    self.remove("", left, right -1, ans, res, res_len)
                    # include
                    self.remove("", left, right, ans, res + s[0], res_len)
                else:
                    # include
                    self.remove("", left, right, ans, res + s[0], res_len)

            else:
                # include
                self.remove("", left, right, ans, res + s[0], res_len)

        else:
            if s[0] == "(":
                if left > 0:
                    # exclude
                    self.remove(s[1::], left - 1, right, ans, res, res_len)
                    # include
                    self.remove(s[1::], left, right, ans, res + s[0], res_len)
                else:
                    # include
                    self.remove(s[1::], left, right, ans, res + s[0], res_len)

            elif s[0] == ")":
                if right > 0:
                    # exclude
                    self.remove(s[1::], left, right -1, ans, res, res_len)
                    # include
                    self.remove(s[1::], left, right, ans, res + s[0], res_len)
                else:
                    # include
                    self.remove(s[1::], left, right, ans, res + s[0], res_len)

            else:
                # letter
                # include
                self.remove(s[1::], left, right, ans, res + s[0], res_len)

solution = Solution()
print (solution.removeInvalidParentheses("(a)())()"))


import os
import h5py
import numpy as np

def delete_file(filename):
	#deletes a file if it exsits
	try:
		os.remove(filename)
	except OSError:
		pass
#--------------------------Data Loader--------------------------------#

def load_data(data_dir):
	train_data=h5py.File('%s/train_data.h5'%data_dir)
	val_data=h5py.File('%s/val_data.h5'%data_dir)
	img_mean= np.array(train_data['img_mean'])

	train_labels=(train_data['hist_labels'], train_data['raw_labels'])
	val_labels=(val_data['hist_labels'], val_data['raw_labels'])

	return train_data['images'], train_labels, val_data['images'], val_labels, img_mean

#--------------------------Batch Loader-------------------------------#

#kernel=1.0*np.array([5, 20, 80, 100, 80, 20, 5])
kernel=1.0*np.array([5, 20, 50, 80, 100, 100, 100, 80, 50, 20, 5])
kernel/=np.sum(kernel)
def vectorize_labels(labels, opt):

	# threshold
	#labels[labels<23]=23
	#labels[labels>105]=105
	#labels-=23

	k_size=len(kernel)
	pad= k_size/2

	label_vec=np.zeros((len(labels),opt.num_classes+k_size-1))
	for idx, lab in enumerate(labels):
		label_vec[idx][lab:lab+k_size]=kernel
	label_vec=label_vec[:,pad:-pad]

	#renormalize rows
	label_vec/=label_vec.sum(axis=1)[:, np.newaxis]

	return label_vec

def get_random_batch(data, labels, opt):

	hist_labels, raw_labels= labels
	rand_idx=np.random.choice(data.shape[0], opt.batch_size, replace=False)
	rand_idx=sorted(rand_idx) #h5py is weird
	batch_data= np.subtract(data[rand_idx], opt.img_mean)
	batch_labels=1.0*vectorize_labels(raw_labels[0][rand_idx], opt)
	#batch_labels=raw_labels[0][rand_idx]+1.0 #Torch <3

	if 'staged' in opt.model:
		batch_labels=[batch_labels, raw_labels[1][rand_idx]+1.0, raw_labels[2][rand_idx]+1.0]

	return batch_data, batch_labels

def get_batches_in_sequence(data, labels, opt):

	hist_labels, raw_labels= labels

	for i in xrange(data.shape[0]/opt.batch_size):
		st, end= opt.batch_size*i, opt.batch_size*i + opt.batch_size
		batch_data= np.subtract(data[st:end], opt.img_mean)
		batch_labels= 1.0*vectorize_labels(raw_labels[0][st:end], opt)
		#batch_labels=raw_labels[0][st:end]+1.0 # Torch <3
		batch_truth= raw_labels[0][st:end]

		if 'staged' in opt.model:
			batch_labels=[batch_labels, raw_labels[1][st:end]+1.0, raw_labels[2][st:end]+1.0]

		yield batch_data, batch_labels, batch_truth


from __future__ import print_function
from math import ceil
import numpy as np
import sys
import pdb

import torch
import torch.optim as optim
import torch.nn as nn

import generator
import discriminator
import helpers

CUDA = True
VOCAB_SIZE = 15000
MAX_SEQ_LEN = 40
START_LETTER = 0
BATCH_SIZE = 8
MLE_TRAIN_EPOCHS = 10
ADV_TRAIN_EPOCHS = 10
POS_NEG_SAMPLES = 100000

GEN_EMBEDDING_DIM = 300
GEN_HIDDEN_DIM = 256
DIS_EMBEDDING_DIM = 64
DIS_HIDDEN_DIM = 64

oracle_samples_path = './oracle_samples.trc'
oracle_state_dict_path = './oracle_EMBDIM32_HIDDENDIM32_VOCAB5000_MAXSEQLEN20.trc'
pretrained_gen_path = './gen_MLEtrain_EMBDIM32_HIDDENDIM32_VOCAB5000_MAXSEQLEN20.trc'
pretrained_dis_path = './dis_pretrain_EMBDIM_64_HIDDENDIM64_VOCAB5000_MAXSEQLEN20.trc'

from tqdm import tqdm
def getLength(line):
    vec=[int(token) for token in line.split()]
    return vec.index(0)
def calculatePPL(gen,testpath):
    testset=loadData(testpath)
    testset_tensor=torch.tensor(testset)

    length=[]
    with open(testpath,'r') as fin:
        for line in fin:
            length.append(getLength(line))
    length=np.array(length)

    nll_all=[]
    TEST_SIZE=testset_tensor.shape[0]
    for i in tqdm(range(0, TEST_SIZE)):
        inp, target = helpers.prepare_generator_batch(testset_tensor[i:i + 1], start_letter=START_LETTER,
                                                              gpu=CUDA)
        nll = gen.batchNLLLoss(inp, target)
        nll_all.append(float(nll.data.cpu()))
    nll_all=np.array(nll_all)

    return np.mean(2**(nll_all/length))

def train_generator_MLE(gen, gen_opt, real_data_samples, epochs):
    """
    Max Likelihood Pretraining for the generator
    """
    for epoch in range(epochs):
        print('epoch %d : ' % (epoch + 1), end='')
        sys.stdout.flush()
        total_loss = 0

        for i in range(0, POS_NEG_SAMPLES, BATCH_SIZE):
            inp, target = helpers.prepare_generator_batch(real_data_samples[i:i + BATCH_SIZE], start_letter=START_LETTER,
                                                          gpu=CUDA)
            gen_opt.zero_grad()
            loss = gen.batchNLLLoss(inp, target)
            loss.backward()
            gen_opt.step()

            total_loss += loss.data.item()

            if (i / BATCH_SIZE) % ceil(
                            ceil(POS_NEG_SAMPLES / float(BATCH_SIZE)) / 10.) == 0:  # roughly every 10% of an epoch
                print('.', end='')
                sys.stdout.flush()
        # Generate LSTM samples
        path='output/MSE-{}.samples'.format(epoch)
        generateSamples(gen, path)

        # each loss in a batch is loss per sample
        total_loss = total_loss / ceil(POS_NEG_SAMPLES / float(BATCH_SIZE)) / MAX_SEQ_LEN

        print(' average_train_NLL = %.4f' % (total_loss))

def train_generator_PG(gen, gen_opt, validation_data_samples, dis, num_batches,_id=0):
    """
    The generator is trained using policy gradients, using the reward from the discriminator.
    Training is done for num_batches batches.
    """

    for batch in range(num_batches):
        s = gen.sample(BATCH_SIZE*2)        # 64 works best
        inp, target = helpers.prepare_generator_batch(s, start_letter=START_LETTER, gpu=CUDA)
        rewards = dis.batchClassify(target)

        gen_opt.zero_grad()
        pg_loss = gen.batchPGLoss(inp, target, rewards)
        pg_loss.backward()
        gen_opt.step()

    # Generate LSTM samples
    path='output/ADV-{}.samples'.format(_id)
    generateSamples(gen, path)

    #validation_loss=0
    #VAL_SIZE=validation_data_samples.shape[0]
    #for i in range(0, VAL_SIZE, BATCH_SIZE):
    #    inp, target = helpers.prepare_generator_batch(validation_data_samples[i:i + BATCH_SIZE], start_letter=START_LETTER,
    #                                                  gpu=CUDA)
    #    gen_opt.zero_grad()
    #    loss = gen.batchNLLLoss(inp, target)
    #    validation_loss+= loss
    #helpers.batchwise_oracle_nll(gen, oracle, POS_NEG_SAMPLES, BATCH_SIZE, MAX_SEQ_LEN,
                   #                                start_letter=START_LETTER, gpu=CUDA)

    # print(' validation_loss = %.4f' % validation_loss)

def train_discriminator(discriminator, dis_opt, real_data_samples, generator, trainset, d_steps, epochs):
    """
    Training the discriminator on real_data_samples (positive) and generated samples from generator (negative).
    Samples are drawn d_steps times, and the discriminator is trained for epochs epochs.
    """

    # generating a small validation set before training (using oracle and generator)
    len_sampled=100
    perm=np.random.permutation(trainset.shape[0])
    pos_val=torch.tensor(trainset[perm[:len_sampled]])
    neg_val = generator.sample(len_sampled)
    if CUDA:
        pos_val=pos_val.cuda()
    val_inp, val_target = helpers.prepare_discriminator_data(pos_val, neg_val, gpu=CUDA)

    for d_step in range(d_steps):
        s = helpers.batchwise_sample(generator, POS_NEG_SAMPLES, BATCH_SIZE)
        dis_inp, dis_target = helpers.prepare_discriminator_data(real_data_samples, s, gpu=CUDA)
        for epoch in range(epochs):
            print('d-step %d epoch %d : ' % (d_step + 1, epoch + 1), end='')
            sys.stdout.flush()
            total_loss = 0
            total_acc = 0

            for i in range(0, 2 * POS_NEG_SAMPLES, BATCH_SIZE):
                inp, target = dis_inp[i:i + BATCH_SIZE], dis_target[i:i + BATCH_SIZE]
                dis_opt.zero_grad()
                out = discriminator.batchClassify(inp)
                loss_fn = nn.BCELoss()
                loss = loss_fn(out, target)
                loss.backward()
                dis_opt.step()

                total_loss += loss.data.item()
                total_acc += torch.sum((out>0.5)==(target>0.5)).data.item()

                if (i / BATCH_SIZE) % ceil(ceil(2 * POS_NEG_SAMPLES / float(
                        BATCH_SIZE)) / 10.) == 0:  # roughly every 10% of an epoch
                    print('.', end='')
                    sys.stdout.flush()

            total_loss /= ceil(2 * POS_NEG_SAMPLES / float(BATCH_SIZE))
            total_acc /= float(2 * POS_NEG_SAMPLES)

            val_pred = discriminator.batchClassify(val_inp)
            print(' average_loss = %.4f, train_acc = %.4f, val_acc = %.4f' % (
                total_loss, total_acc, torch.sum((val_pred>0.5)==(val_target>0.5)).data.item()/200.))

from torch import Tensor

def generateSamples(gen, path):
    with open(path,'w') as fout:
        for i in range(10):
            samples=gen.sample(1000)
            samples=np.array(Tensor.cpu(samples.data))

            for i in range(samples.shape[0]):
                row=[]
                for j in range(samples.shape[1]):
                    row.append(str(samples[i][j]))
                fout.write(' '.join(row)+'\n')

def loadData(filepath):
    ret=[]
    with open(filepath,'r') as fin:
        for line in fin:
            ret.append([int(token) for token in line.split()])
    return np.array(ret)

if __name__=="__main__":

    trainset=loadData('./dataset/train.vec')
    validationset=loadData('./dataset/valid.vec')
    testset=loadData('./dataset/test.vec')

    trainset_tensor=torch.tensor(trainset)
    validationset_tensor=torch.tensor(validationset)
    oracle=None

    gen = generator.Generator(GEN_EMBEDDING_DIM, GEN_HIDDEN_DIM, VOCAB_SIZE, MAX_SEQ_LEN, gpu=CUDA)
    dis = discriminator.Discriminator(DIS_EMBEDDING_DIM, DIS_HIDDEN_DIM, VOCAB_SIZE, MAX_SEQ_LEN, gpu=CUDA)

    if CUDA:
        #oracle = oracle.cuda()
        gen = gen.cuda()
        dis = dis.cuda()
        oracle_samples = trainset_tensor.cuda()
        trainset_tensor=trainset_tensor.cuda()
        validationset_tensor=validationset_tensor.cuda()
        ### debug
    #MLE_TRAIN_EPOCHS=1
    #########

    # GENERATOR MLE TRAINING
    print('Starting Generator MLE Training...')
    gen_optimizer = optim.Adam(gen.parameters(), lr=1e-3)
    train_generator_MLE(gen, gen_optimizer, trainset_tensor, MLE_TRAIN_EPOCHS)

    torch.save(gen.state_dict(), pretrained_gen_path)
    gen.load_state_dict(torch.load(pretrained_gen_path))

    # PRETRAIN DISCRIMINATOR
    print('\nStarting Discriminator Training...')
    d_steps=10

    ######debug######
    # d_steps=1   ###
    #################
    dis_optimizer = optim.Adagrad(dis.parameters())
    train_discriminator(dis, dis_optimizer, trainset_tensor, gen, trainset, d_steps, 3)

    torch.save(dis.state_dict(), pretrained_dis_path)
    dis.load_state_dict(torch.load(pretrained_dis_path))

    # ADVERSARIAL TRAINING
    print('\nStarting Adversarial Training...')
    d_steps=5

    ### debug ##
    # d_step=1####
    ############

    for epoch in range(ADV_TRAIN_EPOCHS):
        print('\n--------\nEPOCH %d\n--------' % (epoch+1))
        # TRAIN GENERATOR
        print('\nAdversarial Training Generator : ', end='')
        sys.stdout.flush()
        train_generator_PG(gen, gen_optimizer, validationset_tensor, dis, 1, _id=epoch)

        # TRAIN DISCRIMINATOR
        print('\nAdversarial Training Discriminator : ')
        train_discriminator(dis, dis_optimizer, trainset_tensor, gen, trainset, d_steps, 3)

        # Generate seqGAN samples
        path='output/seqGAN-epoch{}.samples'.format(epoch)
        generateSamples(gen, path)


# Author: David Amorim
import spotipy
import spotipy.util as util
from util import my_config_parser

class SpotifyAPI(object):

    CLIENT_ID = my_config_parser.config_section_map("Spotify")["client_id"]
    CLIENT_SECRET = my_config_parser.config_section_map("Spotify")["client_secret"]
    REDIRECT_URL = 'http://localhost:8080'
    SCOPE = ('user-read-currently-playing, '
             'user-read-playback-state, '
             'user-library-modify,'
             'user-library-read')

    USERNAME = 'david'
    MAX_RETRIES = 1

    def __init__(self):
        self.spotify_api = spotipy.Spotify(auth=self.get_token())
        self.token = self.get_token()
        self.memo_playing_object = None
        self.memo_track = None
        self.memo_album = None
        self.memo_main_artist = None

    def get_token(self):
        token = util.prompt_for_user_token(self.USERNAME, self.SCOPE, client_id=self.CLIENT_ID,
                                           client_secret=self.CLIENT_SECRET,
                                           redirect_uri=self.REDIRECT_URL)
        if token:
            return token
        else:
            print("Can't get token for", self.USERNAME)
            return ''

    def refresh_token(self):
            self.token = util.prompt_for_user_token(self.USERNAME, self.SCOPE, client_id=self.CLIENT_ID,
                                                    client_secret=self.CLIENT_SECRET,
                                                    redirect_uri=self.REDIRECT_URL)
            self.spotify_api = spotipy.Spotify(auth=self.token)

    def refresh_memo(self):
        try:
            self.memo_playing_object = self.spotify_api.current_user_playing_track()
            if self.memo_playing_object is None:
                return
            self.memo_track = self.memo_playing_object['item']
            if self.memo_track is None:
                return
            self.memo_album = self.spotify_api.album(self.memo_track['album']['id'])
            self.memo_main_artist = self.spotify_api.artist(self.memo_track['artists'][0]['id'])
        except Exception as e:
            print('Spotipy cant connect. Did you paste current_user_playing_track? Or check your internet connection.')

    def get_memo_track(self):
        return self.memo_track  # can be None

    def get_memo_main_artist(self):
        return self.memo_main_artist  # can be None

    def get_current_progress_sec(self):
        current_object = self.spotify_api.current_user_playing_track()
        if current_object is None:
            return 0
        return current_object['progress_ms'] / 1000

    def has_changed_playback(self):
        current_playing_object = self.spotify_api.current_user_playing_track()
        if current_playing_object is None or current_playing_object['item'] is None:
            return False

        if self.memo_track is None:
            return True

        return self.memo_track['id'] != current_playing_object['item']['id']

    def get_memo_track_name(self):
        if self.memo_track is not None:
            return self.memo_track['name']
        return 'Invalid track'

    def get_memo_track_main_artist_name(self):
        if self.memo_main_artist is not None:
            return self.memo_main_artist['name']
        return 'Invalid track'

    def get_memo_track_album_name(self):
        if self.memo_album is not None:
            return self.memo_album['name']
        return 'Invalid track'

    def get_memo_track_artist(self):
        if self.memo_track is not None:
            return self.memo_track['artists'][0]
        return 'Invalid track'

    def get_memo_track_info(self):
        response = []
        if self.memo_track is not None:
            album_name = self.memo_track['album']['name']
            title = self.memo_track['name']
            artists_names = ', '.join((n['name'] for n in self.memo_track['artists']))
            duration = self.memo_track['duration_ms']
            progress_ms = self.memo_playing_object['progress_ms']
            progress_percent = int(progress_ms / duration * 100)
            release_date = self.memo_track['album']['release_date']
            genres = ', '.join((g for g in self.memo_main_artist['genres']))
            response.append('Album: ' + album_name)
            response.append('Title: ' + title)
            response.append('Artist: ' + artists_names)
            response.append('Duration: ' + str(duration) + ' ms')
            response.append('Progress: ' + str(progress_ms) + ', ' + str(progress_percent) + '%')
            response.append('Release date: ' + str(release_date))
            response.append('Genres: ' + genres)
        else:
            response.append('No song found')
        return response

    def like_song(self):
        track = self.get_memo_track()
        if track is not None:
            track_id = track['id']
            track_list = [track_id]
            if self.spotify_api.current_user_saved_tracks_contains(track_list)[0]:  # contains returns an array...
                return 'Song already liked.'
            else:
                self.spotify_api.current_user_saved_tracks_add(track_list)
                return 'Song liked.'
        else:
            return 'Not a song.'