import base64
import json
import re

from .module import Module
from .utils import StoreKeyValuePair, json_serial

class Node(Module):
    dependencies = ['cluster']
    ami_map = {
        # 'ap-southeast-2': 'ami-862211e5',  # docker enabled amazon
        'ap-southeast-2': 'ami-e7878484',  # arch linux
    }

    def __init__(self, **kwargs):
        super().__init__('node', **kwargs)

    def add_arguments(self, parser):
        subp = parser.add_subparsers(help='node help')

        p = subp.add_parser('ls', help='list nodes')
        p.add_argument('name', metavar='NAME', nargs='?', help='node name')
        p.set_defaults(node_handler=self.handle_list)

        p = subp.add_parser('mk', help='make a node')
        p.add_argument('name', metavar='NAME', help='node name')
        p.add_argument('--manager', '-m', action='store_true', help='manager node (Swarm)')
        p.add_argument('--availability-zone', '-a', choices=['a', 'b'], help='availability zone')
        p.add_argument('--type', '-t', default='t2.micro', help='instance type')
        p.set_defaults(node_handler=self.handle_make)

        p = subp.add_parser('putattr', help='add attribute')
        p.add_argument('name', metavar='NAME', help='node name')
        p.add_argument('values', metavar='VALUES', action=StoreKeyValuePair, nargs='*', help='attributes')
        p.set_defaults(node_handler=self.handle_put_attributes)

        p = subp.add_parser('rm', help='remove a node')
        p.add_argument('name', metavar='NAME', help='node name')
        p.set_defaults(node_handler=self.handle_remove)

        p = subp.add_parser('bastion', help='make a bastion node')
        p.set_defaults(node_handler=self.handle_bastion)

        p = subp.add_parser('ssh', help='SSH to a node')
        p.add_argument('name', metavar='NAME', nargs='?', help='node name')
        p.set_defaults(node_handler=self.handle_ssh)

        p = subp.add_parser('init', help='initialise swarm')
        p.add_argument('name', metavar='NAME', help='node name')
        p.set_defaults(node_handler=self.handle_init_swarm)

        p = subp.add_parser('join', help='join swarm')
        p.add_argument('name', metavar='NAME', help='node name')
        p.set_defaults(node_handler=self.handle_join_swarm)

        p = subp.add_parser('wait', help='wait for OK status')
        p.add_argument('name', metavar='NAME', help='node name')
        p.set_defaults(node_handler=self.handle_wait)

        p = subp.add_parser('reboot', help='reboot a node')
        p.add_argument('name', metavar='NAME', help='node name')
        p.set_defaults(node_handler=self.handle_reboot)

    def handle_list(self, args):
        self.list(args.name)

    def list(self, name):
        if name:
            ec2_cli = self.get_boto_client('ec2')
            cluster = self.client.get_selected('cluster')
            filters = [
                {
                    'Name': 'tag:cluster',
                    'Values': [cluster]
                },
                {
                    'Name': 'tag:name',
                    'Values': [name]
                },
                {
                    'Name': 'instance-state-name',
                    'Values': ['pending', 'running', 'shutting-down', 'stopping', 'stopped']
                }
            ]
            inst = ec2_cli.describe_instances(Filters=filters)['Reservations'][0]['Instances'][0]
            print(json.dumps(inst, default=json_serial, indent=2))
        else:
            for name in self.get_instance_names():
                print(name)

    def handle_make(self, args):
        self.make(args.name, args.manager, args.type)

    def make(self, name, manager, type):
        self.validate(name)
        existing = self.get_instance_names()
        if name in existing:
            self.error(f'node "{name}" already exists')
        if not len(existing) and not manager:
            self.error(f'must have at least one manager before workers')
        ctx = self.get_context({'node': name})
        ec2 = self.get_boto_client('ec2')
        image = self.ami_map[ctx['region']]
        sg_id = self.get_module('cluster').get_security_group_id()
        sn = self.get_module('cluster').get_private_subnet()
        opts = {
            'ImageId': image,
            'SubnetId': sn.id,
            'KeyName': f'fuku-{ctx["cluster"]}',
            'SecurityGroupIds': [sg_id],
            'InstanceType': type,
            'IamInstanceProfile': {
                'Name': 'ec2-profile'
            },
            'MinCount': 1,
            'MaxCount': 1
        }
        with self.template_file('arch-user-data.sh', ctx) as user_data:
            opts['UserData'] = user_data
            inst = ec2.run_instances(**opts)
        inst_id = inst['Instances'][0]['InstanceId']
        self.tag_instance(inst_id, name, manager, ec2=ec2, ctx=ctx)
        self.wait(name)
        if manager:
            self.init_swarm(name)

    def handle_put_attributes(self, args):
        self.put_attribute(args.name, args.values)

    def put_attribute(self, name, values):
        ctx = self.get_context()
        ecs = self.get_boto_client('ecs')
        arn = self.get_instance_arn(name)
        attrs = []
        for k, v in values.items():
            attrs.append({
                'name': k,
                'value': v,
                'targetType': 'container-instance',
                'targetId': arn
            })
        ecs.put_attributes(
            cluster=f'fuku-{ctx["cluster"]}',
            attributes=attrs
        )

    def handle_remove(self, args):
        self.remove(args.name)

    def remove(self, name):
        inst = self.get_instance(name)
        inst.terminate()

    def handle_init_swarm(self, args):
        self.init_swarm(args.name)

    def init_swarm(self, name):
        inst = self.get_instance(name)
        ip = inst.private_ip_address
        resp = self.ssh_run(
            f'docker swarm init --advertise-addr {ip}',
            name,
            capture='text'
        )
        try:
            token = re.search(r'token\s+(.*)\s+\\', resp).group(1)
            port = re.search(r':(\d\d\d\d)', resp).group(1)
        except AttributeError:
            self.error('swarm initialisation failed')
        # s3 = self.get_boto_client('s3')
        # try:
        #     data = s3.get_object(
        #         Bucket=ctx['bucket'],
        #         Key=f'fuku/{ctx["cluster"]}/swarm.json',
        #     )['Body'].read().decode()
        #     data = json.loads(data)
        # except:
        #     data = {
        #         'managers': []
        #     }
        # data['managers'].append({
        #     'workertoken': token,
        #     'ip': ip,
        #     'port': port
        # })
        # s3.put_object(
        #     Bucket=ctx['bucket'],
        #     Key=f'fuku/{ctx["cluster"]}/swarm.json',
        #     Body=json.dumps(data)
        # )
        ec2 = self.get_boto_client('ec2')
        ec2.create_tags(
            Resources=[inst.id],
            Tags=[
                {'Key': 'swarmtoken', 'Value': token},
                {'Key': 'swarmport', 'Value': port}
            ]
        )
        self.ssh_run(
            'docker network create --driver overlay all',
            name=name,
            capture='discard'
        )

    def handle_join_swarm(self, args):
        self.join_swarm(args.name)

    def join_swarm(self, name):
        for inst in self.iter_managers():
            try:
                token, port = None, None
                for tag in inst.tags:
                    if tag['Key'] == 'swarmtoken':
                        token = tag['Value']
                    if tag['Key'] == 'swarmport':
                        port = tag['Value']
                if token is None:
                    raise Exception
                resp = self.ssh_run(
                    f'docker swarm join --token {token} {inst.private_ip_address}:{port}',
                    name=name,
                    capture='text'
                )
                if resp != 'This node joined a swarm as a worker.':
                    self.error('failed to join swarm')
            except:
                pass
            else:
                break

    def handle_wait(self, args):
        self.wait(args.name)

    def wait(self, name):
        inst = self.get_instance(name)
        ec2 = self.get_boto_client('ec2')
        waiter = ec2.get_waiter('instance_status_ok')
        waiter.wait(InstanceIds=[inst.id])

    def handle_ssh(self, args):
        self.ssh_run('', args.name, tty=True)

    def handle_reboot(self, args):
        self.reboot(args.name)

    def reboot(self, name):
        ctx = self.get_context()
        try:
            name = name or ctx['node']
        except KeyError:
            self.error('unknown node')
        inst = self.get_instance(name)
        inst.reboot()

    def tag_instance(self, inst_id, name, manager=False, ec2=None, ctx=None):
        if ctx is None:
            ctx = self.get_context()
        if ec2 is None:
            ec2 = self.get_boto_resource('ec2')
        ec2.create_tags(
            Resources=[inst_id],
            Tags=[
                {'Key': 'Name', 'Value': f'fuku-{ctx["cluster"]}-{name}'},
                {'Key': 'name', 'Value': name},
                {'Key': 'cluster', 'Value': ctx['cluster']},
                {'Key': 'node', 'Value': 'manager' if manager else 'worker'}
            ]
        )

    def iter_instances(self):
        ec2 = self.get_boto_resource('ec2')
        cluster = self.client.get_selected('cluster')
        filters = [
            {
                'Name': 'tag:cluster',
                'Values': [cluster]
            },
            {
                'Name': 'instance-state-name',
                'Values': ['pending', 'running', 'stopping', 'stopped']
            }
        ]
        for inst in ec2.instances.filter(Filters=filters):
            yield inst

    def get_instance_names(self):
        names = []
        for inst in self.iter_instances():
            for tag in inst.tags:
                if tag['Key'] == 'name':
                    names.append(tag['Value'])
                    break
        return names

    def get_instance(self, name):
        ec2 = self.get_boto_resource('ec2')
        cluster = self.client.get_selected('cluster')
        filters = [
            {
                'Name': 'tag:cluster',
                'Values': [cluster]
            },
            {
                'Name': 'tag:name',
                'Values': [name]
            },
            {
                'Name': 'instance-state-name',
                'Values': ['pending', 'running', 'shutting-down', 'stopping', 'stopped']
            }
        ]
        insts = list(ec2.instances.filter(Filters=filters))
        if not len(insts):
            self.error(f'no node in cluster "{cluster}" with name "{name}"')
        return insts[0]

    def get_instance_arn(self, name):
        ctx = self.get_context()
        ecs = self.get_boto_client('ecs')
        paginator = ecs.get_paginator('list_container_instances')
        ec2inst = self.get_instance(name)
        instances = paginator.paginate(
            cluster=f'fuku-{ctx["cluster"]}',
        )
        for inst in instances:
            for arn in inst['containerInstanceArns']:
                res = ecs.describe_container_instances(
                    cluster=f'fuku-{ctx["cluster"]}',
                    containerInstances=[arn]
                )
                for ci in res['containerInstances']:
                    if ci['ec2InstanceId'] == ec2inst.id:
                        return arn
        raise Exception

    def get_bastion(self):
        return self.get_instance('bastion')

    def iter_managers(self):
        ctx = self.get_context()
        ec2 = self.get_boto_resource('ec2')
        filters = [
            {
                'Name': 'tag:cluster',
                'Values': [ctx['cluster']]
            },
            {
                'Name': 'tag:node',
                'Values': ['manager']
            },
            {
                'Name': 'instance-state-name',
                'Values': ['running']
            }
        ]
        for inst in ec2.instances.filter(Filters=filters):
            yield inst

    def iter_nodes(self):
        ctx = self.get_context()
        ec2 = self.get_boto_resource('ec2')
        filters = [
            {
                'Name': 'tag:cluster',
                'Values': [ctx['cluster']]
            },
            {
                'Name': 'instance-state-name',
                'Values': ['running']
            }
        ]
        for inst in ec2.instances.filter(Filters=filters):
            yield inst

    def ssh_run(self, cmd, name=None, inst=None, tty=False, capture=None):
        ctx = self.get_context()
        if inst is None:
            name = name or ctx['node']
            inst = self.get_instance(name)
        bastion = self.get_bastion()
        ip = bastion.public_ip_address
        priv_ip = inst.private_ip_address
        b64cmd = base64.b64encode(cmd.encode()).decode()
        full_cmd = f'ssh-add {ctx["pem"]} 2> /dev/null && ssh{" -t" if tty else ""} -o StrictHostKeyChecking=no -o LogLevel=QUIET  -A ec2-user@{ip} ssh{" -t" if tty else ""} -o StrictHostKeyChecking=no -o LogLevel=QUIET {priv_ip} "\`echo {b64cmd} | base64 -di\`"'
        return self.run(full_cmd, capture=capture)

    def handle_bastion(self, args):
        self.bastion()

    def bastion(self):
        name = 'bastion'
        ctx = self.get_context()
        ec2 = self.get_boto_client('ec2')
        image = self.ami_map[ctx['region']]
        sg_id = self.get_module('cluster').get_security_group_id()
        sn = self.get_module('cluster').get_public_subnet()
        opts = {
            'ImageId': image,
            'NetworkInterfaces': [
                {
                    'DeviceIndex': 0,
                    'SubnetId': sn.id,
                    'Groups': [sg_id],
                    'AssociatePublicIpAddress': True
                }
            ],
            'KeyName': f'fuku-{ctx["cluster"]}',
            # 'SecurityGroupIds': [sg_id],
            'InstanceType': 't2.nano',
            'IamInstanceProfile': {
                'Name': 'ec2-profile'
            },
            'MinCount': 1,
            'MaxCount': 1,
            # 'BlockDeviceMappings': [
            #     {
            #         'DeviceName': '/dev/xvdcz',
            #         'Ebs': {
            #             'VolumeSize': 10
            #         }
            #     }
            # ]
        }
        inst = ec2.run_instances(**opts)
        inst_id = inst['Instances'][0]['InstanceId']
        ec2.create_tags(
            Resources=[inst_id],
            Tags=[
                {'Key': 'Name', 'Value': f'fuku-{ctx["cluster"]}-{name}'},
                {'Key': 'cluster', 'Value': f'{ctx["cluster"]}'},
                {'Key': 'name', 'Value': f'{name}'},
                {'Key': 'bastion', 'Value': 'true'}
            ]
        )
        self.wait(name)
        return inst_id

    def mgr_run(self, cmd, tty=False, capture=None):
        try:
            mgr = list(self.iter_managers())[0]
        except IndexError:
            self.error('no managers available')
        return self.ssh_run(cmd, inst=mgr, tty=tty, capture=capture)

    def all_run(self, cmd):
        for node in self.iter_nodes():
            self.ssh_run(cmd, inst=node, capture=False)

    def get_my_context(self):
        ctx = {}
        node = self.store_get('selected')
        if node is not None:
            ctx['node'] = node
        return ctx

class EcsNode(Node):
    ami_map = {
        # 'ap-southeast-2': 'ami-c1a6bda2',
        'ap-southeast-2': 'ami-bc04d5de'
    }

    def add_arguments(self, parser):
        subp = parser.add_subparsers(help='node help')

        p = subp.add_parser('ls', help='list nodes')
        p.add_argument('name', metavar='NAME', nargs='?', help='node name')
        p.set_defaults(node_handler=self.handle_list)

        p = subp.add_parser('mk', help='make a node')
        p.add_argument('name', metavar='NAME', help='node name')
        p.add_argument('--availability-zone', '-a', choices=['a', 'b'], help='availability zone')
        p.add_argument('--type', '-t', help='instance type')
        p.set_defaults(node_handler=self.handle_make)

        p = subp.add_parser('putattr', help='add attribute')
        p.add_argument('name', metavar='NAME', help='node name')
        p.add_argument('values', metavar='VALUES', action=StoreKeyValuePair, nargs='*', help='attributes')
        p.set_defaults(node_handler=self.handle_put_attributes)

        p = subp.add_parser('rm', help='remove a node')
        p.add_argument('name', metavar='NAME', help='node name')
        p.set_defaults(node_handler=self.handle_remove)

        p = subp.add_parser('bastion', help='make a bastion node')
        p.set_defaults(node_handler=self.handle_bastion)

        p = subp.add_parser('ssh', help='SSH to a node')
        p.add_argument('name', metavar='NAME', nargs='?', help='node name')
        p.set_defaults(node_handler=self.handle_ssh)

        p = subp.add_parser('wait', help='wait for OK status')
        p.add_argument('name', metavar='NAME', help='node name')
        p.set_defaults(node_handler=self.handle_wait)

        p = subp.add_parser('reboot', help='reboot a node')
        p.add_argument('name', metavar='NAME', help='node name')
        p.set_defaults(node_handler=self.handle_reboot)

    def handle_make(self, args):
        self.make(args.name, args.availability_zone, args.type)

    def make(self, name, zone, type):
        self.validate(name)
        existing = self.get_instance_names()
        if name in existing:
            self.error(f'node "{name}" already exists')
        ctx = self.get_context({'node': name})
        ec2 = self.get_boto_client('ec2')
        image = self.ami_map[ctx['region']]
        sg_id = self.get_module('cluster').get_security_group_id()
        sn = self.get_module('cluster').get_private_subnet(zone=zone)
        opts = {
            'ImageId': image,
            'SubnetId': sn.id,
            'KeyName': f'fuku-{ctx["cluster"]}',
            'SecurityGroupIds': [sg_id],
            'InstanceType': type,
            'IamInstanceProfile': {
                'Name': 'ec2-profile'
            },
            'MinCount': 1,
            'MaxCount': 1,
            'UserData': f'#!/bin/bash\necho ECS_CLUSTER=fuku-{ctx["cluster"]} >> /etc/ecs/ecs.config',
            # 'BlockDeviceMappings': [
            #     {
            #         'DeviceName': '/dev/xvdcz',
            #         'VirtualName': 'ephemeral0'
            #     }
            # ]
        }
        inst = ec2.run_instances(**opts)
        inst_id = inst['Instances'][0]['InstanceId']
        self.tag_instance(inst_id, name, False, ec2=ec2, ctx=ctx)
        self.wait(name)


import sqlite3

#bot.ini ファイル読み込み
import configparser
config = configparser.ConfigParser()
config.read("config/bot.ini",encoding="utf-8")
db_sql = config["db"]["db_configuration"]
#DBコントロール
def sql_control(db_name,sql,commit):
    sql = f''' {sql}'''
    con = sqlite3.connect(f"data/{db_name}.db")
    cur = con.cursor()
    cur.execute(sql)
    if commit == True:
        con.commit()
    cur = cur.fetchall()
    con.close()
    return cur
#DB作成
def create(db_name):
    sql = f"""
    CREATE table {db_name}(
        {db_sql});
    """
    sql_control(db_name,sql,commit=True)

#column追加
def column_add(db_name,column_name):
    sql = f'''ALTER TABLE {db_name}
            ADD COLUMN "{column_name}";'''
    sql_control(db_name,sql,commit=True)

#DBにデータ追加
def add(db_name,guild,category):
    #sql = f"""insert into server_info(guild_id,category,ch1_jp,ch2_en,ch3_cn,ch4_kr,ch5_th,ch6_id)values({guild},{category},{ch1},{ch2},{ch3},{ch4},{ch5},{ch6});"""
    sql = f"""insert into {db_name}(guild_id,category)values({guild},{category});"""
    sql_control(db_name,sql,commit=True)

#DB内の特定のデータを検索
def search(db_name,guild_id):
    sql = f''' SELECT *
            FROM {db_name}
            WHERE guild_id = {guild_id}'''
    cur = sql_control(db_name,sql,commit=False)
    return cur

#DB内の特定のデータを変更
def change(db_name,guild,ch,ch_id):
    sql = f''' UPDATE {db_name}
                SET {ch} = {ch_id}
                WHERE guild_id = {guild};'''
    sql_control(db_name,sql,commit=True)

def dell(db_name,db_id):
    sql = f"""
            DELETE FROM {db_name} WHERE id="{db_id}";"""
    sql_control(db_name,sql,commit=True)


import yaml
import matplotlib.pyplot as plt
import numpy as np
import math as mt
def read_pgm(pgmf):
    """Return a raster of integers from a PGM as a list of lists."""
    p=pgmf.readline()
    width=int(pgmf.readline())
    height=int(pgmf.readline())
    #(width, height) = [int(i) for i in pgmf.readline().split()]
    depth = int(pgmf.readline())
    assert depth <= 255

    raster = []
    for y in range(height):
        row = []
        for y in range(width):
            row.append(ord(pgmf.read(1)))
        raster.append(row)
    return raster
def read_ros_map(map_path):
    map = open(map_path, 'rb')
    mapData = read_pgm(map)
    mapData = np.array(mapData)
    mapData = mapData[::-1]
    return mapData
'''
mapData=read_ros_map('./6b/map/mymap.pgm')
width,length=mapData.shape
for i in range(0,width,10):
    for j in range(0,length,10):
        if mapData[i,j]>230:
            plt.plot(j,i,'ro')
plt.show()
print(mapData[1050][100])
'''
if __name__ == '__main__':
    f = open("./6b/map/mymap.yaml")
    temp = yaml.load(f)
    resolution = temp['resolution']
    map_origin = temp['origin']
    mapData = read_ros_map('./6b/map/mymap.pgm')
    list = []
    for y in range(-6, 74):
        for x in range(0, 51):
            x_pixel = int(mt.floor((x - map_origin[0]) / resolution))
            y_pixel = int(mt.floor((y - map_origin[1]) / resolution))
            if mapData[y_pixel, x_pixel] > 225:
                list.append([x, y])
                plt.plot(x, y, 'ro')
    np.savetxt("list.txt", np.array(list), fmt='%f', delimiter=' ', newline='\r\n')
    plt.show()


# -*- coding: utf-8 -*-
from django.forms import ModelForm
from django import forms
from models import *
from multifield import *
DATE_FORMAT = ('%d.%m.%y',)
CHOICES = ((50, '50'), (100, '100'), (200, '200'), (300, '300'))
def getTuple(model, *fields):
    t = tuple(model.objects.values_list(*fields))
    return t

class MyModelMultipleChoiceField(forms.ModelMultipleChoiceField):
    def label_from_instance(self, obj):
        return "%s" % obj.type

class invoices_search(forms.Form):
    id = forms.IntegerField(required=False, label='Код')
    invno = forms.IntegerField(required=False, label='№')
    #type = MyModelMultipleChoiceField(queryset=Invoice_types.objects.only('id'), required=False, label='Тип заказа')
    type = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple, choices=getTuple(Invoice_types), required=False, label='Тип заказа')
    seller__title = forms.CharField(required=False,  label='Отпускающий')
    customer__title = forms.CharField(required=False, label='Покупатель')
    date = IntervalField(required=False, label='Дата')
    sum = forms.DecimalField(required=False,label='Сумма')
    sumB = forms.DecimalField(required=False,label='СуммаБ')
    released = forms.NullBooleanField(required=False,label='Отпущено')
    releasedB = forms.NullBooleanField(required=False,label='ОтпущеноБ')
    payed = forms.DecimalField(required=False,label='Оплачено')
    compl_date = forms.DateField(required=False,label='')
    ID_SW = forms.IntegerField(required=False, label='Код СО')
    store = forms.MultipleChoiceField(choices=getTuple(Stores), required=False, label='Склад')
    def clean(self):
        for key in self.cleaned_data:
            if not ((self.cleaned_data[key] == None) or
                    (self.cleaned_data[key] == u'') or
                    (self.cleaned_data[key] == [])):
                break
        else:
            raise forms.ValidationError('Укажите хотябы одно условие для фильтрации')
        return self.cleaned_data
class Other(forms.Form):
    elements = forms.ChoiceField(widget=forms.Select, choices=CHOICES, label='эл. на стр.')

class interval(forms.Form):
    intr = IntervalField(required=False, widget=IntervalWidget)
    note = forms.CharField(required=False)
    #date = forms.SplitDateTimeField(required=False)


import json
import pandas as pd
from tabulate import tabulate
from flatten_json import flatten

json_api_output = "/Users/alsharma/Documents/Indeed/retest issue/projects_devops_org.json"

with open(json_api_output, 'r') as j:
    contents = json.loads(j.read())

d = json.dumps(contents)

"""
# Flatten data for License Info
df_license = pd.json_normalize(contents, record_path=['results', 'id'])

# Create Columns
df_license.columns = df_license.columns.map(lambda x: x.split(".")[-1])
print(tabulate(df_license, headers='keys', tablefmt='psql'))

"""
# Flatten data for Project Info
df_projects = pd.json_normalize(contents, record_path=['results', 'projects'])

# Create Columns
df_projects.columns = df_projects.columns.map(lambda x: x.split(".")[-1])
print(tabulate(df_projects, headers='keys', tablefmt='psql'))

# Flatten data for Dependencies
df_deps = pd.json_normalize(contents, record_path=['results', 'dependencies'])
df_flattened = (flatten(record, '.') for record in df_deps)
pd.options.display.width = None

# Create Columns
df_deps.columns = df_deps.columns.map(lambda x: x.split(".")[-1])
print(tabulate(df_deps, headers='keys', tablefmt='psql'))


import os.path
import random
import torchvision.transforms as transforms
import numpy as np
from data.base_dataset import BaseDataset
import os
import glob
from PIL import Image
import json

class FlirDataset(BaseDataset):
    def initialize(self, opt, mode='train', test_map=None):
        print('ThermalDataset')
        self.opt = opt
        self.root = opt.dataroot
        self.dir_AB = os.path.join(opt.dataroot, opt.phase)
        if test_map == None:
            self.test_map = self.opt.test_map
        else:
            self.test_map = test_map

        # TODO
        ###應該是舊版的FLIR dataset
        # if test:
        #     self.A_data = np.load(os.path.join(self.root, "grayscale_test_data.npy"))
        #     self.B_data = np.load(os.path.join(self.root, "thermal_test_data.npy"))
        # else:
        #     self.A_data = np.load(os.path.join(self.root, "grayscale_training_data.npy"))
        #     self.B_data = np.load(os.path.join(self.root, "thermal_training_data.npy"))

        if mode == 'train':
            with open(os.path.join(self.root, 'images_rgb_train/train_data.json')) as f:
                self.A_data = json.load(f)
            self.A_data_path = self.root + '/images_rgb_train/data'
            self.A_data = [os.path.join(self.A_data_path, path) for path in self.A_data]
            self.B_data_path = self.root + '/images_thermal_train/data'
            self.B_data = sorted(glob.glob(os.path.join(self.B_data_path, "*.jpg")))

        elif mode == 'val':
            self.A_data_path = self.root + '/images_rgb_val/data'
            self.A_data = self.delete_small_data(self.A_data_path)
            # self.A_data = sorted(glob.glob(os.path.join(self.A_data_path, "*.jpg")))

            self.B_data_path = self.root + '/images_thermal_val/data'
            self.B_data = sorted(glob.glob(os.path.join(self.B_data_path, "*.jpg")))

        elif mode == 'test':
            with open(self.test_map) as f:
                self.test_pair = json.load(f)
            self.A_data_path = self.root + '/video_rgb_test/data'
            self.A_data = sorted(glob.glob(os.path.join(self.A_data_path, "*.jpg")))
            self.B_data_path = self.root + '/video_thermal_test/data'
            self.B_data = [os.path.join(self.B_data_path, self.test_pair[os.path.basename(path)]) for path in
                           self.A_data]

            print(f"testing data : {len(self.B_data)}")
        else:
            raise ValueError(f"phase {mode} is not recognized.")

    def delete_small_data(self, root):
        B_data = sorted(glob.glob(os.path.join(root, "*.jpg")))
        size_B = []
        for index, img_path in enumerate(B_data):
            print(f"{index}/{len(B_data)}")
            B_shape = np.asarray(Image.open(img_path)).shape
            if not (B_shape[0] < 512 or B_shape[1] < 512):
                size_B.append(img_path)
        return size_B

    def get_transform(self, phase):
        if phase == 'train':
            pass
        else:
            transform = transforms.Compose([
                transforms.ToPILImage(),
                transforms.Resize([512, 512]),
                transforms.ToTensor(),
                transforms.Normalize([0.5], [0.5])
            ])
        return transform

    def img2gray(self, img):
        rgb_to_grayscale = transforms.Grayscale()
        PIL_image = transforms.ToPILImage()(img)
        gray_img = rgb_to_grayscale(PIL_image)
        return transforms.ToTensor()(gray_img)

    def __getitem__(self, index):
        A_img_path = self.A_data[index]  # .transpose(2, 0, 1)
        B_img_path = self.B_data[index]  # .transpose(2, 0, 1)

        A = Image.open(A_img_path).convert("RGB")
        B = Image.open(B_img_path)

        # A = A.resize((self.opt.loadSize, self.opt.loadSize), Image.BICUBIC)
        A = transforms.ToTensor()(A.copy()).float()
        B = transforms.ToTensor()(B.copy()).float()
        if self.opt.isTrain:
            w_total = A.size(2)
            w = int(w_total)
            h = A.size(1)
            w_offset = random.randint(0, max(0, w - self.opt.fineSize - 1))
            h_offset = random.randint(0, max(0, h - self.opt.fineSize - 1))
            # IR 大小只有 (1,512,640)
            h_b_offset = random.randint(0, max(0, B.size(1) - self.opt.fineSize - 1))
            w_b_offset = random.randint(0, max(0, B.size(2) - self.opt.fineSize - 1))

            A = A[:, h_offset:h_offset + self.opt.fineSize, w_offset:w_offset + self.opt.fineSize]
            B = B[:, h_b_offset:h_b_offset + self.opt.fineSize, w_b_offset:w_b_offset + self.opt.fineSize]

            A_gray = self.img2gray(A)
            A = transforms.Normalize([0.5], [0.5])(A)
            B = transforms.Normalize([0.5], [0.5])(B)
            A_gray = transforms.Normalize([0.5], [0.5])(A_gray)
        else:
            self.transform = self.get_transform(phase='test')
            A_gray = self.img2gray(A)
            A = self.transform(A)
            B = self.transform(B)
            A_gray = self.transform(A_gray)

            # A = transforms.Resize([512,512])(A)
            # B = transforms.Resize([512,512])(B)
            # A = transforms.Normalize([0.5], [0.5])(A)
            # B = transforms.Normalize([0.5], [0.5])(B)

        return {'A': A, 'B': B, 'A_paths': A_img_path, 'B_paths': B_img_path, 'A_gray': A_gray}

    def __len__(self):
        return len(self.A_data)

    def name(self):
        return 'FLIR DATASET'


import logging as log

from mo.front.common.partial_infer.const import tf_const_infer
from mo.front.tf.extractors.utils import tf_dtype_extractor, tf_tensor_shape
from mo.front.tf.extractors.utils import tf_tensor_content

def tf_const_ext(pb):
    pb_tensor = pb.attr["value"].tensor
    result = {
        'data_type': tf_dtype_extractor(pb_tensor.dtype),
        'shape': tf_tensor_shape(pb_tensor.tensor_shape),
        'infer': tf_const_infer
    }
    result['value'] = tf_tensor_content(pb_tensor.dtype, result['shape'], pb_tensor)
    log.debug('Constant extractor for node gives shape = {} and value.shape = {}'.format(result['shape'],
                                                                                result['value'].shape))
    return result


import requests
import json

def test():
    url = "http://localhost:8000/api/mgr/customers"    #测试的接口url
    headers = {"Content-Type":"application/json"}
    payload = {
        "action":"modify_customer",
        "id": 1,
        "newdata":{
            "address": "江苏省常州武进市白云街44号",
            "name": "武进市 袁腾飞",
            "phonenumber": "13886666666",
            "idcard": "bbbbbbbbb"
        }
    }
    r = requests.post(url = url,json = payload,headers = headers)    #发送请求
    #return r.json
    print (r.text)                                                #获取响应报文
    print (r.status_code)

if __name__=="__main__":
    test()


"""Inference file for CNN registration on unseen data"""
from keras.models import load_model
import helpers as helper
import dataLoader as load
import math

# On server
fixed_dir = "/hepgpu3-data1/dmcsween/DataTwoWay128/fixed"
moving_dir = "/hepgpu3-data1/dmcsween/DataTwoWay128/moving"
dvf_dir = "/hepgpu3-data1/dmcsween/DataTwoWay128/DVF"
"""
# On laptop
fixed_dir = "E:/MPhys/Data128/PlanningCT"
moving_dir = "E:/MPhys/Data128/PET_Rigid"
dvf_dir = "E:/MPhys/Data128/DVF"
"""
batch_size = 1

def inference():
    inputPath = './transfer_logs/'
    outputPath = './transfer_outputs/'
    print('Load data to Transform')
    fixed_predict, moving_predict, dvf_label = load.data_reader(fixed_dir, moving_dir, dvf_dir)

    print('Turn into numpy arrays')
    fixed_array, fixed_affine = fixed_predict.get_data()
    moving_array, moving_affine = moving_predict.get_data()
    dvf_array, dvf_affine = dvf_label.get_data(is_image=False)

    print('Shuffle')
    fixed_array, moving_array, dvf_array = helper.shuffle_inplace(
        fixed_array, moving_array, dvf_array)
    fixed_affine, moving_affine, dvf_affine = helper.shuffle_inplace(
        fixed_affine, moving_affine, dvf_affine)

    print('Split into test/training data')
    test_fixed, test_moving, test_dvf, train_fixed, train_moving, train_dvf = helper.split_data(
        fixed_array, moving_array, dvf_array, split_ratio=0.05)
    test_fixed_affine, test_moving_affine, test_dvf_affine, train_fixed_affine, train_moving_affine, train_dvf_affine = helper.split_data(
        fixed_affine, moving_affine, dvf_affine, split_ratio=0.05)

    print('Load models')
    print("Fixed input", test_fixed.shape)
    print("Moving input", test_moving.shape)
    model = load_model(inputPath + 'best_model.h5')
    model.compile(optimizer='Adam', loss='mean_squared_error', metrics=["accuracy"])
    dvf = model.predict_generator(helper.generator([test_fixed, test_moving], label=test_dvf, predict=True, batch_size=batch_size), steps=math.ceil(
        test_fixed.shape[0]/batch_size), verbose=1)
    # test_loss = model.evaluate_generator(helper.generator([test_fixed, test_moving], label=test_dvf, predict=True, batch_size=batch_size), steps=math.ceil(
    #    test_fixed.shape[0]/batch_size), verbose=1)

    print('Save DVF')
    # Save images
    helper.write_images(test_fixed, test_fixed_affine, file_path=outputPath, file_prefix='fixed')
    helper.write_images(test_moving, test_moving_affine,
                        file_path=outputPath, file_prefix='moving')
    helper.write_images(dvf, test_fixed_affine, file_path=outputPath, file_prefix='dvf')
    #print("Test Loss:", test_loss)
    # Save warped
    #print("Test Loss Shape:", test_loss.shape)

def main(argv=None):
    inference()

if __name__ == '__main__':
    main()


import json
import requests
d1 = requests.get("https://raw.githubusercontent.com/BuildBot42/discord-scam-links/main/list.txt").text
d2 = requests.get("https://raw.githubusercontent.com/DevSpen/scam-links/master/src/links.txt").text
d3 = requests.get("https://raw.githubusercontent.com/Dogino/Discord-Phishing-URLs/main/scam-urls.txt").text
d4 = requests.get("https://raw.githubusercontent.com/nikolaischunk/discord-phishing-links/main/domain-list.json").json()["domains"]
d5raw = requests.get("https://raw.githubusercontent.com/Bananonz/discord-scam-links/main/links.json").text
d5 = d5raw.strip('][').replace('["','').replace('"]','').replace('"','').replace(']','').split(',')
d6raw = requests.get("https://raw.githubusercontent.com/Bananonz/discord-scam-links/main/links.json").text
d6raw = d6raw.replace("\n","")
d6 = d6raw.strip('][').replace('["','').replace('"]','').replace('"','').replace(']','').split(',')
import os
helper = []
f = open("nitroscamlinks.txt", "r")
for f1 in f:
 for line in f1.split():
    helper.append(line.replace('"]','').replace('"','').replace(',','\n'))
    print("Adding " + line + " to helper list")
for line in d1.split():
    helper.append(line.replace('"]','').replace('"','').replace(',','\n'))
    print("Adding " + line + " to helper list")
for line in d2.split():
    helper.append(line.replace('"]','').replace('"','').replace(',','\n'))
    print("Adding " + line + " to helper list")
for line in d3.split():
    helper.append(line.replace('"]','').replace('"','').replace(',','\n'))
    print("Adding " + line + " to helper list")
for line in d4:
    helper.append(line.replace('"]','').replace('"','').replace(',','\n'))
    print("Adding " + line + " to helper list")
for line in d5:
    helper.append(line.replace('"]','').replace('"','').replace(',','\n'))
    print("Adding " + line + " to helper list")
helper = set(helper)
helper2 = []
for i in helper:
    helper2.append(i.replace('https://','').replace('http://',''))
    print("Removing either https:// or http:// and adding " + i + " to helper2 list")
helper = helper2
dd = []
[dd.append(x) for x in helper if x not in dd]
helper = dd
df = helper
f.close()
f = open("nitroscamlinks.txt", "w")
f.write('')
f.close()
f = open("nitroscamlinks.txt", "a")
for line in df:
  f.write(line + '\n')
f.close()
f = open("nitroscamlinks.json", "w")
f.write('')
f.close()
f = open("nitroscamlinks.json", "a")
data = {
         "urls": df
       }
json.dump(data, f, ensure_ascii=False, indent=4)
f.close()
os.system('ls')


"""
# Sample code to perform I/O:

name = input()                  # Reading input from STDIN
print('Hi, %s.' % name)         # Writing output to STDOUT

# Warning: Printing unwanted or ill-formatted data to output will cause the test cases to fail
"""

# Write your code here
from collections import Counter

t = int(input())
for _ in range(t):
    s = input()
    counter = Counter(s)
    counter = sorted(counter.items(), key=lambda x: (x[-1], x[0]))
    ans = ''
    for k, v in counter:
        ans += k * v
    print(ans)


#!/usr/bin/python3
"""! @brief Implementation of circular Anti-Aliased ("AA") gauges with gradient coloring."""

##
# @file aa_arc_gauge.py
#
# @brief Contains implementation of circular Anti-Aliased ("AA") gauges with gradient coloring.
#

import pygame,sys,time
import pygame.freetype

pygame.init()
import sys
import os
os.environ['PYGAME_FREETYPE'] = '1'
sys.path.append('/home/pi/Sensor_Scripts/pygame_code/tricorder/')
sys.path.append('/home/pi/Sensor_Scripts/pygame_code/tricorder/freqshow_code')
sys.path.append('/home/pi/Sensor_Scripts/pygame_code/tricorder/assets')
sys.path.append('/home/pi/Sensor_Scripts/pygame_code/tricorder/assets/saved_fonts')
sys.path.append('/home/pi/Sensor_Scripts/pygame_code/tricorder/sensor_pages')
sys.path.append('/home/pi/Sensor_Scripts/pygame_code/tricorder/general_pages')
sys.path.append('/home/pi/Sensor_Scripts/pygame_code/tricorder/resources')

# import pygame.freetype
# from enum import Enum
# from pygame.sprite import Sprite
import pygame.gfxdraw
import pygame.freetype
from colors import WHITE,RED,GREY,BLACK,DARK_GREY,LIGHT_GREY,YELLOW
from global_functions import get_text_dimensions, my_map
from fonts import FONT_DIN, FONT_HELVETICA_NEUE,FONT_FEDERATION
import numpy as np
import logging

# ====================================================== #
HOME_DIR="/home/pi/Sensor_Scripts/pygame_code/tricorder/"
FONTS_DIR=HOME_DIR+"assets/saved_fonts/"

number_keys=[pygame.K_0,pygame.K_1,pygame.K_2,pygame.K_3,pygame.K_4,\
                pygame.K_5,pygame.K_6,pygame.K_7,pygame.K_8,pygame.K_9]

# ---- arc parameters ---- #
# radius=35
# offset=75
# RAD_CONSTANT=0.0174533

## Pi/2 in radians
RAD_CONSTANT=0.0175

class AA_Gauge():
    """! Generic class for an anti-aliased circular gauge with gradient coloring.

    @section Description
    Defines the base class utilized by all pages.

    @section Main Components
    - Radius
    - Range: min and max vals
    --autolims
    - Arc color
    - Background arc color
    - Weight
    - Title
    - Font: type, size, color
    """
    def __init__(self,
        screen,
        curr_val=None,
        in_min=0,
        in_max=100,
        radius=30,
        origin=(50,50),
        weight=3,
        color=RED,
        empty_arc_color=GREY,
        CURR_FONT=FONT_HELVETICA_NEUE,
        main_font_size=28,
        FONT_COLOR=WHITE,
        solid_bg_color=BLACK,
        range_color=DARK_GREY,
        range_font=FONT_HELVETICA_NEUE,
        range_font_size=24,
        suffix='',
        print_num=True,
        solid_bg=False,
        draw_empty_arc=True,
        print_range=True,
        print_title=True,
        title='',
        title_font_size=18,
        title_font=FONT_FEDERATION,
        title_font_color=LIGHT_GREY,
        auto_lims=True,
        offset=45):
        '''! Constructor'''

        ## Pygame main screen/display object
        self.screen=screen
        ## Value to display
        self.curr_val=curr_val
        ## Min possible input value
        self.in_min=in_min
        ## Max possible input value
        self.in_max=in_max
        ## Gauge thickness
        self.weight=weight
        ## Determines the bottom clipping angle
        self.offset=offset
        self.init_radius(radius)
        ## Outer radius
        self.gauge_radius=self.radius
        self.init_origin()
        self.update_boundries()

        ## Default foreground color is red.
        self.color=color
        ## Default foreground color is grey.
        self.empty_arc_color=empty_arc_color
        ## To show/hide suffix for main number display.
        self.suffix=suffix
        ## Show hide main number display.
        self.print_num=print_num
        ## Default background is transparent.
        self.solid_bg=solid_bg
        ## Empty arc is drawn by default.
        self.draw_empty_arc=draw_empty_arc
        ## Default font is HELVETICA_NEUE.
        self.CURR_FONT=CURR_FONT
        ## Font size for main numver display.
        self.main_font_size=main_font_size
        ## Default font color is white.
        self.FONT_COLOR=FONT_COLOR
        ## Default solid bg color is black.
        self.solid_bg_color=solid_bg_color
        ## Determines if range is printed. Default value is True.
        self.print_range=print_range
        ## Font for rendering range values. Default value is HELVETICA_NEUE.
        self.range_font=range_font
        ## Font size for rendering range values. Default size is 24.
        self.range_font_size=range_font_size
        ## Color for range font. Default color is DARK_GREY.
        self.range_color=range_color
        ## Print title or not. Default is True.
        self.print_title=print_title
        ## Title for gauge.
        self.title=title
        ## Default font size for title is 18.
        self.title_font_size=title_font_size
        ## Default title font is FONT_FEDERATION.
        self.title_font=title_font
        ## Default title color is LIGHT_GREY.
        self.title_font_color=title_font_color
        ## To adjust gauge limits automatically. Default is True.
        self.auto_lims=auto_lims

    def init_origin(self):
        '''! Calculates origin (x,y) for gauge.'''
        ## Adjusted x-origin for gauge, accounting for radius and weight.
        self.origin_x=self.radius+self.weight
        ## Adjusted y-origin for gauge, accounting for radius and weight.
        self.origin_y=self.radius+self.weight
        ## Adjusted origin for gauge, accounting for radius and weight.
        self.origin= (self.origin_x,self.origin_y)

    def init_radius(self,new_radius):
        '''! Saves radius and diameter for faster execution.'''
        ## Gauge radius (outer)
        self.radius=new_radius
        ## Gauge diameter (outer)
        self.d=self.radius*2
        # self.weight=self.gauge_radius//self.weight_ratio

    def update_boundries(self):
        '''! Calculates bounding rectabgle.'''
        ## Gauge thickness
        w=self.weight
        r=self.radius
        d=self.d-1
        x=self.origin_x
        y=self.origin_y
        j=(2*self.offset/360)*(r+w)
        # self.bounding_rect=[(x-(r+w//2)),(y-(r+w//2)),(d+w//2),(d+w//2)]
        self.bounding_rect=[0,0,(d+w*2),(d+w*2)]

    def adjust_gauge_lims(self):
        '''! Sets min/max limits to nearest round numbers.'''
        lower_lims=[0,0.1,1,10,100,1000,10000,100000]
        upper_lims=[0.1,1,10,100,1000,10000,100000,1000000]
        for low,up in zip(lower_lims,upper_lims):
            if low<=self.curr_val<up:
                self.in_max=up

    def update_val(self,new_val):
        '''! Uodate current value.'''
        self.curr_val=new_val
        if self.auto_lims:
            self.adjust_gauge_lims()

    def blit_gauge(self,new_val,print_val=None):
        '''! Render and return gauge as surface.'''
        try:
            # ----------------- arc params ----------------- #
            radius=self.radius
            # j=int((self.offset/360)*(radius))
            size=((self.d+self.weight),(self.d+self.weight*2))
            surf = pygame.Surface(size)
            # surf.fill(BLUE)
            # ----------------- draw background ----------------- #
            if (self.solid_bg):
                pygame.draw.rect(surf, self.solid_bg_color, self.bounding_rect)
                surf.fill(self.solid_bg_color)

            # ------------- Empty Arc Params ------------- #
            # draw empty arc
            # arc(surface, x, y, r, start_angle, stop_angle, color)

            x=size[0]//2
            y=size[1]//2-self.weight//2
            start_angle=(180-self.offset)
            end_angle=(360+self.offset)

            # rangee=np.linspace(0,255,self.weight//2)

            empty_radius_range=range(radius-self.weight//4,radius+self.weight//4)
            radius_range=range(radius-self.weight//2,radius+self.weight//2)

           # ------------- Draw Arc ------------- #
            if new_val is not None:
                try:
                    new_val=float(new_val)
                except Exception as e:
                    new_val=-2
                    print ('aa_arc_gauge: ',e)

                if print_val!=None:
                    orig=print_val
                else:
                    orig=round(new_val,2)

                # print ('new_val: ',new_val)
                # print(type(new_val))
                self.update_val(new_val)
                curr_val_scaled=my_map(self.curr_val,self.in_min,self.in_max,start_angle,end_angle)

                # --- draw empty arc --- #
                if (self.draw_empty_arc):
                    for curr_r in empty_radius_range:
                        try:
                            pygame.gfxdraw.arc(surf, x,y, curr_r,int(round(curr_val_scaled,2)), end_angle, self.empty_arc_color)
                        except OverflowError as e:
                            print (e)
                            print (curr_val_scaled)
                        except ValueError as e:
                            print (e)
                            print (curr_val_scaled)

                jj=np.linspace(start_angle, curr_val_scaled, 255)
                alpha_vals=np.linspace(20,255,len(jj))

                try:
                    arc_angles=[]
                    if (len(jj)>2):
                        for index in range((len(jj)-1)):
                            start_ang=int(jj[index])
                            end_ang=int(jj[index+1])
                            ccooll=(self.color[0], self.color[1], self.color[2], int(alpha_vals[index+1]))
                            arc_angles.append([start_ang,end_ang,ccooll])
                except Exception as e:
                    print (e)
                    print (curr_val_scaled)

                for item in arc_angles:
                    s=item[0]
                    e=item[1]
                    c=item[2]
                    for curr_r in radius_range:
                        try:
                            pygame.gfxdraw.arc(surf, x, y , curr_r,s, e, c)
                        except OverflowError as o:
                            print (o)
                            print (curr_val_scaled)

            # ------------- Print Range ------------- #
            if (self.print_range):
                r_div_2=radius//2
                range_y_pos=(radius*2+self.weight//2)

                txt_surf,w,h=get_text_dimensions(f'{self.in_min:,}',font_style=self.range_font,font_color=self.range_color,style=0,font_size=self.range_font_size)
                surf.blit(txt_surf,(10,range_y_pos-h))

                txt_surf,w,h=get_text_dimensions(f'{self.in_max:,}',font_style=self.range_font,font_color=self.range_color,style=0,font_size=self.range_font_size)
                surf.blit(txt_surf,(int(size[0]-self.weight-w),range_y_pos-h))

            # ------------- Print Curr Value

            if (self.print_num):
                if len(str(orig).split('.'))>1:
                    t0=str(orig).split('.')[0]
                    t1=str(orig).split('.')[1]
                    if t1=='0':
                        t1=''
                    else:
                        t1='.'+t1
                else:
                    t0=orig
                    t1=''

                curr_val_surf,w,h=get_text_dimensions(text="{}".format(t0),font_style=self.CURR_FONT,font_color=WHITE,style=0,font_size=self.main_font_size)
                decimal_surf,w2,h2=get_text_dimensions(text=f'{t1}',font_style=self.CURR_FONT,font_color=WHITE,style=0,font_size=self.main_font_size//2-1)
                suffix_surf,w3,h3=get_text_dimensions(text="{}".format(self.suffix),font_style=self.CURR_FONT,font_color=WHITE,style=0,font_size=self.main_font_size//2-1)

                total_len=w+max(w2,w3)

                if t1=='':
                    h_correction= -h3
                else:
                    h_correction=0

                curr_x=self.bounding_rect[0]+radius+self.weight//2-total_len//2
                curr_y=self.bounding_rect[1]+radius+self.weight//2-h//2
                surf.blit(curr_val_surf,(curr_x,curr_y))

                space=5

                surf.blit(decimal_surf,(curr_x+w+space,self.bounding_rect[1]+radius+self.weight//2-h//2))

                surf.blit(suffix_surf,(curr_x+w+space,self.bounding_rect[1]+radius+self.weight//2-h//2+h2+1+h_correction))

            # ------------- Print Title
            if (self.print_title) and self.title!='':
                # self.title='VIS'
                txt_surf,w,h=get_text_dimensions(text=self.title,font_style=self.title_font,font_color=self.title_font_color,style=1,font_size=self.title_font_size)
                surf.blit(txt_surf,(self.bounding_rect[0]+radius+self.weight//2-w//2,self.bounding_rect[1]+r_div_2+self.weight//2-h//2))
        except Exception as e:
            # raise (e)
            logging.error (e)
            surf = pygame.Surface(size)
            txt_surf,w,h=get_text_dimensions(text="XX",font_style=self.title_font,font_color=self.title_font_color,style=1,font_size=self.title_font_size)
            surf.blit(txt_surf,(self.bounding_rect[0]+radius+self.weight//2-w//2,self.bounding_rect[1]+(radius//2)+self.weight//2-h//2))

        return surf

def fps_manager(clock,screen,fps_array):
    '''! For testing performance (not used in normal execcution).'''
    curr_fps=round(clock.get_fps(),2)

    if curr_fps>0:
        fps_array=np.append(fps_array,curr_fps)

    if (3<curr_fps<70):
        fps_txt=FONT_20.render("{} fps".format(curr_fps), 1, WHITE)
        _,_,w,h=fps_txt.get_bounding_rect()
        screen.blit(fps_txt,((screen_w//2-w//2,1)))

    try:
        minn=round(np.min(fps_array),2)
        maxx=round(np.max(fps_array),2)
        avg=round(np.mean(fps_array),2)
        median=round(np.median(fps_array),2)
        total=len(fps_array)

        fps_txt=FONT_20.render("min:{}  max:{}  avg:{}  median:{} total:{}".format(minn,maxx,avg,median,total), 1, WHITE)
        _,_,w,h=fps_txt.get_bounding_rect()
        screen.blit(fps_txt,(3,screen_h-2*h))
    except:
        pass

    return fps_array

def handle_events():
    '''! For testing (not used in normal execcution).'''
    for event in pygame.event.get():
        if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and (event.key == pygame.K_ESCAPE or event.key==ord('q'))):
            pygame.quit()
            sys.exit()

# if __name__ == '__main__':

#     # pygame initialization
#     screen_w=400
#     screen_h=400
#     pygame.init()
#     screen = pygame.display.set_mode((screen_w, screen_h),pygame.RESIZABLE | pygame.HWSURFACE | pygame.DOUBLEBUF | pygame.ASYNCBLIT)
#     clock = pygame.time.Clock()

#     # gauge-related variables
#     origin=(screen_w//2,screen_h//2)
#     in_min=1
#     in_max=10

#     padding_x=0 # padding between actual bounding rect and final rect
#     padding_y=0

#     color_list=[RED,
#                 DARK_RED,
#                 YELLOW,
#                 DARK_YELLOW,
#                 ORANGE,
#                 GREEN,
#                 BLUE,
#                 GRID_BLUE,
#                 BLUE_GREY,
#                 MISTY_BLUE,
#                 PURPLE,
#                 VIOLET,
#                 CREAM]

#     color_names_list=['RED',
#                         'DARK_RED',
#                         'YELLOW',
#                         'DARK_YELLOW',
#                         'ORANGE',
#                         'GREEN',
#                         'BLUE',
#                         'GRID_BLUE',
#                         'BLUE_GREY',
#                         'MISTY_BLUE',
#                         'PURPLE',
#                         'VIOLET',
#                         'CREAM']

#     color_txt=FONT_20.render('{}'.format(color_list[0]),1,WHITE)

#     # other variables
#     max_fps=0
#     min_fps=100000
#     downcount=False
#     fps_array=np.array([])

#     # init the gauge
#     G=AA_Gauge(screen,0,radius=100,weight=8,in_max=in_max,print_num=True,solid_bg=False,solid_bg_color=YELLOW,color=RED,draw_empty_arc=True,empty_arc_color=(96,96,96,120),origin=origin,suffix='Lux',main_font_size=50)

#     while 1:
#         # determine range
#         if downcount:
#             my_range=reversed(range(in_min,in_max+1))
#         else:
#             my_range=range(in_min,in_max+1)

#         for curr_val in my_range:
#             curr_val+=1/curr_val
#             # print (curr_val)
#             clock.tick()
#             screen.fill(BLACK)
#             handle_events()

#             gauge_img=G.blit_gauge(curr_val)

#             bounding_rect=gauge_img.get_bounding_rect()
#             # print (j)

#             pos=(screen_w//4-G.radius//2,screen_h//4-G.radius//2)

#             screen.blit(gauge_img,pos)

#             left=pos[0]-padding_x
#             top=pos[1]-padding_y
#             width=bounding_rect[2]+2*padding_x
#             height=bounding_rect[3]+2*padding_y
#             rekt=[left,top,width,height]

#             pygame.gfxdraw.rectangle(screen, rekt, WHITE)

#             if (curr_val%25==0):
#                 my_color=curr_val%len(color_list)
#                 G.color=color_list[my_color]
#                 # print ('my_color: ',my_color,color_names_list[my_color])
#                 color_txt=FONT_36.render('{}'.format(color_names_list[my_color]),1,WHITE)

#             screen.blit(color_txt,(3,screen_h-100))

#             fps_array=fps_manager(clock,screen,fps_array)
#             pygame.display.update()
#             time.sleep(1)

#         downcount=not downcount
#         time.sleep(2)

#     pygame.quit()
#     exit()


import tweepy #https://github.com/tweepy/tweepy
import csv
import numpy as np
import os
import pandas as pd

# from keys import *

from google.cloud import language_v1

consumer_key = os.environ['TWITTER_CONSUMER_KEY']
consumer_secret = os.environ['TWITTER_CONSUMER_SECRET']

access_key = os.environ['TWITTER_ACCESS_KEY']
access_secret = os.environ['TWITTER_ACCESS_SECRET']

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)

def get_tweets(screen_name, limit = 100):
    """
    Takes a screen name, returns a df of their recent tweets.
    Also saves it as a csv of tweets in  new_{screen_name}_tweets.csv
    """
    #Twitter only allows access to a users most recent 3240 tweets with this method

    #initialize a list to hold all the tweepy Tweets
    alltweets = []

    #make initial request for most recent tweets (200 is the maximum allowed count)
    new_tweets = api.user_timeline(screen_name = screen_name,count=200)

    #save most recent tweets
    alltweets.extend(new_tweets)

    #save the id of the oldest tweet less one
    oldest = alltweets[-1].id - 1

    #keep grabbing tweets until there are no tweets left to grab or we've hit 200 tweets
    while len(new_tweets) > 0 and len(alltweets) < limit:
        print(f"getting tweets before {oldest}")

        #all subsiquent requests use the max_id param to prevent duplicates
        new_tweets = api.user_timeline(screen_name = screen_name,count=limit,max_id=oldest)

        #save most recent tweets
        alltweets.extend(new_tweets)

        #update the id of the oldest tweet less one
        oldest = alltweets[-1].id - 1

        print(f"...{len(alltweets)} tweets downloaded so far")

    #transform the tweepy tweets into a 2D array that will populate the csv
    outtweets = [[screen_name, tweet.id_str, tweet.created_at, tweet.text] for tweet in alltweets]

    #write the csv
#    with open(f'new_{screen_name}_tweets.csv', 'w') as f:
#        writer = csv.writer(f)
#        writer.writerow(["user_id","tweet_id","created_at","text"])
#        writer.writerows(outtweets)

    outtweets = pd.DataFrame(outtweets)
    outtweets.columns = ["user_id","tweet_id","created_at","text"]
    outtweets.to_csv(f"new_{screen_name}_tweets.csv")
    return outtweets

def load_tweets(screen_name):
    return pd.read_csv(f"new_{screen_name}_tweets.csv", index_col = 0)
def get_sentiment(text_content):
    """
    Analyzing Sentiment in a String

    Args:
      text_content The text content to analyze

    Returns document sentiment score, magnitude
    """

    client = language_v1.LanguageServiceClient()

    # Available types: PLAIN_TEXT, HTML
    type_ = language_v1.Document.Type.PLAIN_TEXT

    # Optional. If not specified, the language is automatically detected.
    # For list of supported languages:
    # https://cloud.google.com/natural-language/docs/languages
    language = "en"
    document = {"content": text_content, "type_": type_, "language": language}
#    document = {"content": text_content, "type_": type_}

    # Available values: NONE, UTF8, UTF16, UTF32
    encoding_type = language_v1.EncodingType.UTF8

    response = client.analyze_sentiment(request = {'document': document, 'encoding_type': encoding_type})

    return response.document_sentiment.score, response.document_sentiment.magnitude

def get_friends(screen_name):
    """get friends of user with entered screen name"""
    friends = pd.DataFrame({'friends': [user.screen_name for user in tweepy.Cursor(api.friends, screen_name=screen_name).items()]})
    friends.to_csv(f"{screen_name}_friends.csv")
    return friends

def load_friends(screen_name):
    """load friends of user from saved csv"""
    return pd.read_csv(f"{screen_name}_friends.csv")

def message_friend(user, friends):
    """ send a friendly DM to a friend """

    friend = np.random.choice(friends)

    # text to be sent
    text = "hi from friendly bot :) your pal {} looks like they could use a friend right now. reach out and have a nice conversation!".format(user)

    # sending the direct message
    direct_message = api.send_direct_message(friend, text)


"""
__author__ = Yash Patel
__name__   = spectral.py
__description__ = Spectral sparsification of graphs
"""

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
import time

from sparsify.spectral import SpectralSparsifier
from sparsify.sample import SampleSparsifier
from setup.sbm import create_sbm, create_clusters
from analysis.deanonymize import calc_accuracies, spectral_analysis, kmeans_analysis

def _plot(G, fn):
    """Given an input graph and a filename, plots the graph at the file destination

    Returns void
    """
    print("Plotting {} graph...".format(fn))
    pos = nx.spring_layout(G)
    nx.draw(G, pos)
    plt.axis('off')
    plt.savefig("output/sparsify/{}".format(fn))
    plt.close()

def _partition_graph(G, clusters):
    start = time.time()
    hier_partitions = spectral_analysis(G, k=len(clusters))
    hier_accuracies = calc_accuracies(clusters, hier_partitions)
    end = time.time()
    print(end-start)
    hier_accuracies["time"] = end - start

    start = time.time()
    kmeans_partitions = kmeans_analysis(G, k=len(clusters))
    kmeans_accuracies = calc_accuracies(clusters, kmeans_partitions)
    kmeans_accuracies["time"] = time.time() - start
    return hier_accuracies, kmeans_accuracies

def _run_test(G, clusters, sparsifier, param, sname):
    """Given an input graph, a sparsifier object (API of having a .sparsify(G) function),
    and a name of the sparsifier, plots the original and sparsifier versions of the graph
    in the output/sparsify/ folder

    Returns void
    """
    _plot(G, "{}_{}_original.png".format(sname, param))
    orig_hier_accuracies, orig_kmeans_accuracies = _partition_graph(G, clusters)
    sparsifier.sparsify(G)
    new_hier_accuracies, new_kmeans_accuracies = _partition_graph(G, clusters)
    _plot(G, "{}_{}_sparse.png".format(sname, param))

    delta_hier = {}
    delta_kmeans = {}

    for accuracy_metric in orig_hier_accuracies:
        if accuracy_metric != "time":
            delta_hier[accuracy_metric] = new_hier_accuracies[accuracy_metric] - \
                orig_hier_accuracies[accuracy_metric]
            delta_kmeans[accuracy_metric] = new_kmeans_accuracies[accuracy_metric] - \
                orig_kmeans_accuracies[accuracy_metric]
        else:
            delta_hier[accuracy_metric] = new_hier_accuracies[accuracy_metric]
            delta_kmeans[accuracy_metric] = new_kmeans_accuracies[accuracy_metric]

    return delta_hier, delta_kmeans

def _plot_trial_results(params, hier_deltas, kmeans_deltas, trial_type):
    params = np.array(params)
    for acc_type, deltas in zip(["hierarchical","kmeans"],[hier_deltas,kmeans_deltas]):
        metrics = {}
        for trial in deltas:
            for accuracy_metric in trial:
                if accuracy_metric not in metrics:
                    metrics[accuracy_metric] = []
                metrics[accuracy_metric].append(trial[accuracy_metric])

        print(metrics)
        for accuracy_metric in metrics:
            plt.title("Param vs. Drop in {} {}".format(acc_type, accuracy_metric))

            m, b = np.polyfit(params, metrics[accuracy_metric], 1)
            plt.scatter(params, metrics[accuracy_metric])
            plt.plot(params, m * params + b, '-')

            plt.savefig("output/sparsify/{}_{}_{}.png".format(accuracy_metric, acc_type,trial_type))
            plt.close()

def spectral_trial(params):
    epsilons = np.arange(0.25,10.0,.125)

    filtered_epsilons = []
    hier_deltas       = []
    kmeans_deltas     = []

    for epsilon in epsilons:
        try:
            cluster_sizes = [params["cluster_size"]] * params["num_clusters"]
            clusters = create_clusters(cluster_sizes)
            G = create_sbm(clusters, params["p"], params["q"], False)

            spectral_sparsifier = SpectralSparsifier(epsilon=epsilon)
            delta_hier, delta_kmeans = _run_test(G, clusters,
                spectral_sparsifier, epsilon, "spectral")

            filtered_epsilons.append(epsilon)
            hier_deltas.append(delta_hier)
            kmeans_deltas.append(delta_kmeans)

        except:
            continue
    _plot_trial_results(filtered_epsilons,hier_deltas,kmeans_deltas,"spectral")

def sample_trial(params):
    Cs = np.arange(0.25,.50,.25)

    filtered_Cs   = []
    hier_deltas   = []
    kmeans_deltas = []

    for C in Cs:
        try:
            cluster_sizes = [params["cluster_size"]] * params["num_clusters"]
            clusters = create_clusters(cluster_sizes)

            G = create_sbm(clusters, params["p"], params["q"], False)
            L = nx.normalized_laplacian_matrix(G).todense()
            w,_ = np.linalg.eig(L)
            sorted_w = sorted(w)

            sample_sparsifier = SampleSparsifier(sorted_w[len(clusters)], C=C)
            delta_hier, delta_kmeans = _run_test(G, clusters,
                sample_sparsifier, C, "sample")

            filtered_Cs.append(C)
            hier_deltas.append(delta_hier)
            kmeans_deltas.append(delta_kmeans)

        except:
            continue
    _plot_trial_results(filtered_Cs,hier_deltas,kmeans_deltas,"sample")

def main():
    params = {
        "p" : .75,
        "q" : .10,
        "cluster_size" : 10,
        "num_clusters" : 10
    }

    spectral_trial(params)

if __name__ == "__main__":
    main()


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

from setuptools import setup, find_packages

__version__ = '0.2.0'
__author__  = 'Zeuxis Lo'
__email__   = 'seekstudio@gmail.com'

setup(
    name='pluginplot',
    author=__author__,
    author_email=__email__,
    version=__version__,
    license='BSD',
    url='https://github.com/zeuxisoo/python-pluginplot',
    description='Yet another simple plugin system for Python like WordPress plugin system',
    install_requires=[],
    packages=find_packages(exclude=['tests', 'tests.*']),
    include_package_data=True,
    zip_safe=False,
    platforms='any',
    classifiers=[
        "Development Status :: 4 - Beta",
        "License :: OSI Approved :: BSD License",
        "Programming Language :: Python",
        "Programming Language :: Python :: 2",
        "Programming Language :: Python :: 2.7",
    ],
)


import sys
from time import sleep

import pygame

from alien import Alien
from bullet import Bullet

def check_keydown_events(event, ai_settings, screen, ship, bullets, aliens, stats, score_board):
    """响应按键"""
    if event.key == pygame.K_RIGHT:
        ship.moving_right = True
    elif event.key == pygame.K_LEFT:
        ship.moving_left = True
    elif event.key == pygame.K_SPACE:
        fire_bullet(ai_settings, bullets, screen, ship, stats)
    elif event.key == pygame.K_q:
        sys.exit()
    elif event.key == pygame.K_p:
        start_game(ai_settings, aliens, bullets, screen, ship, stats, score_board)

def fire_bullet(ai_settings, bullets, screen, ship, stats):
    """发射子弹,游戏中才能发射"""
    if len(bullets) < ai_settings.bullets_allowed and stats.game_active:
        new_bullet = Bullet(ai_settings, screen, ship)
        bullets.add(new_bullet)

def check_keyup_events(event, ship):
    """响应按键"""
    if event.key == pygame.K_RIGHT:
        ship.moving_right = False
    elif event.key == pygame.K_LEFT:
        ship.moving_left = False

def check_play_button(stats, play_button, mouse_x, mouse_y, aliens, bullets, ai_settings, screen, ship, score_board):
    """在玩家单击Play按钮时开始新游戏"""
    button_clicked = play_button.rect.collidepoint(mouse_x, mouse_y)
    if button_clicked and not stats.game_active:
        start_game(ai_settings, aliens, bullets, screen, ship, stats, score_board)

def start_game(ai_settings, aliens, bullets, screen, ship, stats, score_board):
    """开始新游戏,并初始化游戏速度"""
    ai_settings.initialize_dynamic_settings()
    # 重置游戏统计信息
    stats.reset_stats()
    stats.game_active = True
    # 重置记分牌
    score_board.prep_score()
    score_board.prep_high_score()
    score_board.prep_level()
    score_board.prep_ships()
    # 隐藏光标
    pygame.mouse.set_visible(False)
    # 清空外星人和子弹列表
    aliens.empty()
    bullets.empty()
    # 创建新的外星人群，并将飞船复位
    create_fleet(ai_settings, screen, aliens, ship)
    ship.center_ship()

def check_events(ai_settings, screen, ship, bullets, stats, play_button, aliens, score_board):
    """响应鼠标和按键的事件"""
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            check_keydown_events(event, ai_settings, screen, ship, bullets, aliens, stats, score_board)
        elif event.type == pygame.KEYUP:
            check_keyup_events(event, ship)
        elif event.type == pygame.MOUSEBUTTONDOWN:
            mouse_x, mouse_y = pygame.mouse.get_pos()
            check_play_button(stats, play_button, mouse_x, mouse_y, aliens, bullets, ai_settings, screen, ship,
                              score_board)

def update_screen(ai_settings, screen, ship, aliens, bullets, play_button, stats, score_board):
    """更新屏幕上的图像，并刷新画面"""
    # 填充背景颜色
    screen.fill(ai_settings.bg_color)
    # 绘制飞船
    ship.blitme()
    # 绘制子弹
    for bullet in bullets.sprites():
        bullet.draw_bullet()
    # 绘制外星人
    aliens.draw(screen)
    # 绘制记分板
    score_board.show_score()
    # 如果处于非活动状态，就绘制Play按钮
    if not stats.game_active:
        play_button.draw_button()
    # 重绘屏幕
    pygame.display.flip()

def update_bullets(ai_settings, screen, aliens, ship, bullets, stats, score_board):
    """更新子弹位置，并删除已消失的子弹"""
    # 更新子弹编组并删除已消失的子弹
    bullets.update()
    for bullet in bullets.copy():
        if bullet.rect.bottom <= 0:
            bullets.remove(bullet)
    check_bullet_alien_collisions(ai_settings, aliens, bullets, screen, ship, stats, score_board)

def check_bullet_alien_collisions(ai_settings, aliens, bullets, screen, ship, stats, score_board):
    """响应子弹和外星人的碰撞"""
    # 检查是否有子弹击中外星人，如果有，就删除相应的子弹和敌人
    collections = pygame.sprite.groupcollide(bullets, aliens, True, True)
    if collections:
        for aliens in collections.values():
            stats.score += ai_settings.alien_points * len(aliens)
            score_board.prep_score()
        # 检查最高分
        check_high_score(stats, score_board)
    if len(aliens) == 0:
        # 删除所有子弹，加快游戏节奏，提高游戏等级，并创建新的外星人群
        bullets.empty()
        ai_settings.increase_speed()
        stats.level += 1
        score_board.prep_level()
        create_fleet(ai_settings, screen, aliens, ship)

def create_fleet(ai_settings, screen, aliens, ship):
    # 创建一个外星人
    alien = Alien(ai_settings, screen)
    number_aliens_x = get_number_aliens_x(ai_settings, alien.rect.width)
    number_rows = get_number_row(ai_settings, ship.rect.height, alien.rect.height)
    # 创建外星人群
    for row_number in range(number_rows):
        for alien_number in range(number_aliens_x):
            create_alien(ai_settings, alien_number, aliens, screen, row_number)

def create_alien(ai_settings, alien_number, aliens, screen, row_number):
    """创建一个外星人并将其加入当前行"""
    alien = Alien(ai_settings, screen)
    alien_width = alien.rect.width
    alien.x = alien_width + 2 * alien_width * alien_number
    alien.rect.x = alien.x
    alien.rect.y = alien.rect.height + 2 * alien.rect.height * row_number
    aliens.add(alien)

def get_number_aliens_x(ai_settings, alien_width):
    """计算每行可容纳多少外星人"""
    available_space_x = ai_settings.screen_width - 2 * alien_width
    number_aliens_x = int(available_space_x / (2 * alien_width))
    return number_aliens_x

def get_number_row(ai_settings, ship_height, alien_height):
    """计算屏幕可容纳多少行外星人"""
    available_space_y = ai_settings.screen_height - (3 * alien_height) - ship_height
    number_rows = int(available_space_y / (2 * alien_height))
    return number_rows

def change_fleet_direction(ai_settings, aliens):
    """将外星人群向下移动，并改变左右移动方向"""
    for alien in aliens:
        alien.rect.y += ai_settings.fleet_drop_speed
    ai_settings.fleet_direction *= -1

def check_fleet_edges(ai_settings, aliens):
    """有外星人到达边缘时，做出相应的动作"""
    for alien in aliens:
        if alien.check_edges():
            change_fleet_direction(ai_settings, aliens)
            break

def ship_hit(ai_settings, stats, screen, ship, aliens, bullets, score_board):
    """响应被外星人撞到飞船事件"""
    if stats.ships_left > 0:
        # 生命-1
        stats.ships_left -= 1
        # 更新记分牌
        score_board.prep_ships()
        # 清空外星人和子弹列表
        aliens.empty()
        bullets.empty()

        # 创建新的外星人群，并将飞船复位
        create_fleet(ai_settings, screen, aliens, ship)
        ship.center_ship()

        # 暂停0.5秒
        sleep(0.5)
    else:
        stats.game_active = False
        pygame.mouse.set_visible(True)

def update_aliens(ai_settings, stats, screen, ship, aliens, bullets, score_board):
    """检查是否有外星人位于屏幕边缘，并更新外星人群中的所有外星人位置"""
    check_fleet_edges(ai_settings, aliens)
    aliens.update()

    # 检测外星人和飞船之间的碰撞
    if pygame.sprite.spritecollideany(ship, aliens):
        ship_hit(ai_settings, stats, screen, ship, aliens, bullets, score_board)

    check_aliens_bottom(ai_settings, stats, screen, ship, aliens, bullets, score_board)

def check_aliens_bottom(ai_settings, stats, screen, ship, aliens, bullets, score_board):
    """检查是否有外星人到达了屏幕底端"""
    screen_rect = screen.get_rect()
    for alien in aliens.sprites():
        if alien.rect.bottom >= screen_rect.bottom:
            # 像飞船被撞到一样处理
            ship_hit(ai_settings, stats, screen, ship, aliens, bullets, score_board)
            break

def check_high_score(stats, score_board):
    if stats.score > stats.high_score:
        stats.high_score = stats.score
        score_board.prep_high_score()


"""Add options table.

Revision ID: 21d2ff0a8680
Revises:
Create Date: 2020-04-16 00:46:55.864252

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '21d2ff0a8680'
down_revision = None
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('options',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('productId', sa.Integer(), nullable=False),
    sa.Column('price', sa.Integer(), nullable=False),
    sa.Column('weight', sa.Integer(), nullable=True),
    sa.Column('amount', sa.Integer(), nullable=True),
    sa.ForeignKeyConstraint(['productId'], ['products.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('options')
    # ### end Alembic commands ###


import logging
import os

from .command import Command
from appimagebuilder.recipe.roamer import Roamer
from appimagebuilder.modules.test import ExecutionTest, TestFailed
from ..context import Context

import docker
from docker.errors import DockerException

class RunTestCommand(Command):
    def __init__(self, context: Context, tests_settings: Roamer):
        super().__init__(context, "AppDir tests")
        self.app_dir = context.app_dir
        self.tests_settings = tests_settings

    def id(self):
        return "test"

    def __call__(self, *args, **kwargs):
        try:
            test_cases = self._load_tests(self.tests_settings())
        except DockerException as e:
            logging.error("Docker error : "+str(e))
            logging.error("(Is docker installed/started ?)")
            logging.error("Tests will be skipped")
            return

        try:
            for test in test_cases:
                test.run()
        except TestFailed as err:
            logging.error("test failed")
            logging.error(err)

            exit(1)

    def _load_tests(self, test_settings: {}):
        test_cases = []

        for name, data in test_settings.items():
            data_accessor = Roamer(data)
            env = data_accessor.env() or []
            if isinstance(env, dict):
                env = ["%s=%s" % (k, v) for k, v in env.items()]

            test = ExecutionTest(
                appdir=self.app_dir,
                name=name,
                image=data_accessor.image(),
                command=data_accessor.command(),
                before_command=data_accessor.before_command(),
                env=env,
            )
            test_cases.append(test)

        return test_cases


from flask import Blueprint, jsonify, request
from flask_login import login_required, current_user
from app.models import db, Portfolio

portfolio_routes = Blueprint('portfolio', __name__)

# Get Portfolios for
@portfolio_routes.route('/<int:user_id>', methods=['GET'])
# @login_required
def portfolio(user_id):
    portfolios = Portfolio.query.filter_by(user_id= user_id).order_by(Portfolio.id.desc()).all()
    return {"portfolio": [portfolio.to_dict() for portfolio in portfolios]}

# Create new portfolio
@portfolio_routes.route('/<int:user_id>', methods=['POST'])
# @login_required
def addPortfolio(user_id):
    # if current_user.id is not user_id:
    #     return "Unauthorized"
    new_portfolio = request.json
    portfolios = Portfolio.query.filter_by(user_id=user_id).all()

    single_portfolio = [single for single in portfolios if single.to_dict()["crypto_id"] == new_portfolio["cryptoId"]]

    # check to see if portfolio exists
    if not single_portfolio:
        portfolio = Portfolio(user_id = new_portfolio["userId"], crypto_id = new_portfolio["cryptoId"], quantity = new_portfolio["quantity"], purchase_price = new_portfolio["purchasePrice"])
        db.session.add(portfolio)
        db.session.commit()

    # return portfolios for current user
    portfolios = Portfolio.query.filter_by(user_id=user_id).all()
    return {"portfolio": [portfolio.to_dict() for portfolio in portfolios]}

# Update Porfolio
@portfolio_routes.route('/<int:user_id>', methods=['PUT'])
# @login_required
def update_portfolio(user_id):
    #if current_user.id is not user_id:
    #     return "Unauthorized"
    updated_portfolio = request.json
    single_portfolio = Portfolio.query.filter_by(user_id=user_id, crypto_id=updated_portfolio["cryptoId"]).first()

    # check to see if portfolio exists
    if single_portfolio:
        single_portfolio.quantity = updated_portfolio["quantity"]
        single_portfolio.purchase_price = updated_portfolio["purchasePrice"]
        db.session.commit()

    if single_portfolio.quantity == 0:
        Portfolio.query.filter_by(user_id=user_id, crypto_id=updated_portfolio["cryptoId"]).delete()
        db.session.commit()

    # return portfolios for current user
    portfolios = Portfolio.query.filter_by(user_id=user_id).all()
    return {"portfolio": [portfolio.to_dict() for portfolio in portfolios]}


import pygame
from plane_sprites import *

class PlaneGame(object):
    """飞机大战主游戏"""

    def __init__(self):
        print("游戏初始化")

        # 1. 创建游戏的窗口
        self.screen = pygame.display.set_mode(SCREEN_RECT.size)   # 用常量代替尺寸数值(见plane_sprites)，因set_mode返回元组，所有传入size属性。
        # 2. 创建游戏的时钟
        self.clock = pygame.time.Clock()
        # 3. 调用私有方法，精灵和精灵组的创建
        self.__create_sprites()

        # 4. 设置定时器事件 - 创建敌机 1s间隔
        pygame.time.set_timer(CREATE_ENEMY_EVENT, 1000)
        # 5. 设置每隔500ms英雄发射子弹事件
        pygame.time.set_timer(HERO_FIRE_EVENT, 500)

    def __create_sprites(self):

        # 创建背景精灵和精灵组
        bg1 = Background()  # plane_sprites中在Background类的初始化中已统一定义了路径
        bg2 = Background(True)  # plane_sprites中在Background类的初始化中用is_alt属性控制替换背景

        self.back_group = pygame.sprite.Group(bg1, bg2)

        # 创建敌机的精灵组
        self.enemy_group = pygame.sprite.Group()

        # 创建英雄的精灵和精灵组
        self.hero = Hero()
        self.hero_group = pygame.sprite.Group(self.hero)

    def start_game(self):
        print("游戏开始...")

        while True:
            # 1. 设置刷新帧率
            self.clock.tick(FRAME_PER_SEC)  # 指定刷新率,这里也是用在plane_sprites中定义的常量
            # 2. 事件监听
            self.__event_handler()
            # 3. 碰撞检测
            self.__check_collide()
            # 4. 更新/绘制精灵
            self.__update_sprites()
            # 5、 更新显示
            pygame.display.update()

            pass

    def __event_handler(self):
        for event in pygame.event.get():

            # 判断是否退出游戏
            if event.type == pygame.QUIT:
                PlaneGame.__game_over()

            elif event.type == CREATE_ENEMY_EVENT:
                print("敌机出场...")
                # 创建敌机精灵
                enemy = Enemy()

                # 将敌机精灵添加到敌机精灵组
                self.enemy_group.add(enemy)

            # 监听英雄发射子弹事件
            elif event.type == HERO_FIRE_EVENT:
                self.hero.fire()

            # 监听器键盘按键移动：
            # 第一种实现向右移动方式   *** 按下抬起才算一次移动，操作灵活性低。
            # elif event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT:
            #     print("向右移动")

            # 第二种实现向右移动的方式  *** 更加灵活
            # 使用键盘提供的方法获取键盘按键 - 按键元组
            # 返回所有按键的元组，如果某个键被按下，对应的值会是1
            keys_pressed = pygame.key.get_pressed()

            # 判断是否按下了方向键，判断元组中对应的按键索引值
            if keys_pressed[pygame.K_RIGHT]:
                # print("按住按键不放持续向右移动...")
                self.hero.speed = 2  # 数字为水平移动速度
            elif keys_pressed[pygame.K_LEFT]:
                self.hero.speed = -2
            else:
                self.hero.speed = 0

    def __check_collide(self):

        # 1. 子弹摧毁敌机
        # 使用pygame内置的模块groupcollide可以检测精灵组之间是否有碰撞,碰撞后将碰撞的两个精灵组直接销毁
        pygame.sprite.groupcollide(self.hero.bullets, self.enemy_group, True, True)

        # 2. 敌机撞毁英雄
        # 使用pygame内置的模块spritecollide(方法放回一个列表)可以检测精灵之间是否有碰撞,碰撞后将碰撞的两个精灵直接销毁
        enemies = pygame.sprite.spritecollide(self.hero, self.enemy_group, True)

        # 判断列表是否有内容
        if len(enemies) > 0:

            # 让英雄牺牲
            self.hero.kill()

            # 结束游戏
            PlaneGame.__game_over()

    def __update_sprites(self):

        self.back_group.update()
        self.back_group.draw(self.screen)

        self.enemy_group.update()
        self.enemy_group.draw(self.screen)

        self.hero_group.update()
        self.hero_group.draw(self.screen)

        self.hero.bullets.update()
        self.hero.bullets.draw(self.screen)

    # 因为是静态方法，所以不需要传入self且需要@staticmethod进行声明
    @staticmethod
    def __game_over():
        print("游戏结束")

        pygame.quit()
        exit()

if __name__ == '__main__':

    pygame.init()

    # 创建游戏对象
    game = PlaneGame()

    # 启动游戏
    game.start_game()


# 3PGMI

# Parameters to compare with results in
#   Sarenac et. al. (2018),
#   Three Phase-Grating Moiré Neutron Interferometer for Large Interferometer Area Applications

# Output fitted to a sine with fixed frequency
# Simulation run multiple times to increase total neutron count

import numpy as np
import matplotlib.pyplot as plt
from neutronpckg import NeutronWave
from neutronpckg.utils import contrast_fit

# Sim
Nx = 20e4
Sx = 20e-3
Nit = 100

# Source
sw = 500e-6
Nn = 100
wvl = 0.5e-9
theta = 0.2*np.pi/180

# Setup
L = 8.8
Ls2 = 4.75
D1 = 4.6e-2
D = 1.5e-2 # D3 - D1

L1 = Ls2 - D1
D3 = D1 + D
L3 = L - Ls2 - D3

# Gratings
P = 2.4e-6
phi1 = np.pi*0.5
phi2 = np.pi
phi3 = np.pi*0.5

# Plotting
xmin = -15e-3
xmax = 15e-3
numbins = int(2e4)

center = np.empty(numbins-1)
hist = np.zeros(numbins-1)

for ii in range(Nit):

    print()
    print("Iteration:", ii+1)
    print("  First prop")
    wave = NeutronWave(Nx, Sx, Nn=Nn, wvl=wvl)
    wave.slitSource(L1, sw, theta=theta, randPos=True)
    print("  Second prop")
    wave.rectPhaseGrating(P,phi1)
    wave.propagate(D1, pad=False)
    print("  Third prop")
    wave.rectPhaseGrating(P,phi2)
    wave.propagate(D3, pad=False)
    print("  Last prop")
    wave.rectPhaseGrating(P,phi3)
    wave.propagate(L3, pad=False)
    print("  Results")
    center, htemp = wave.hist_intensity(numbins, xlimits=(xmin,xmax), retcenter=True)
    hist += htemp/Nit

print()
print('Period:', abs(P*L/D)*1e3, 'mm')

width = center[1]-center[0]
plt.plot(center*1e3, hist, '-')

Pd = abs(P*L/D)
_, Pd, _ = contrast_fit(center, hist, abs(P*L/D), fitP=True)
C, _, fit = contrast_fit(center, hist, Pd, fitP=False)
print('Period fit:', Pd*1e3, 'mm')
print('Contrast:', C)
plt.plot(center*1e3, fit, '--', color='orange')

plt.xlabel('x [mm]')
plt.show()


import numpy as np

from prettytable import PrettyTable

def print_arguments_as_table(arguments):
    table = PrettyTable()
    table.field_names = ['Argument', 'Value']

    for key, value in arguments.items():
        table.add_row([key, value])

    print(table)

# Given a list of configuration space points C = [c_1, ..., c_N] and
# list of steps between points M = [m_1, ..., m_N-1], generate the
# trajectory in the configuration space joining the points with the
# required number of inbetween points

def interpolate_two_points(x, y, n):
    trajectory = []
    for t in np.linspace(0, 1, n):
        trajectory.append(x + t * (y - x))
    return trajectory

def generate_trajectory(points, steps):
    trajectory = []
    for i in range(len(steps)):
        trajectory += interpolate_two_points(points[i], points[i+1], steps[i])
    return trajectory


from typing import Iterable, Sequence

def indent(lines: Iterable[str], prefix='\t'):
	return ((prefix + line) for line in lines)

def it_get(it: Iterable, index: int, length: int):
	if isinstance(it, Sequence):
		return it[index]
	if index >= length:
		raise IndexError(index)
	if index < 0:
		if index < -length:
			raise IndexError(index)
		index += length
	itr = iter(it)
	try:
		for _ in range(index):
			next(itr)
		return next(itr)
	except StopIteration:
		raise IndexError(index)


"""
given a linear RV trend, what's the minimum mass implied?

Wright's blog says:
"the lowest mass object that could be contributing a linear trend turns out to
be one in an eccentric orbit with e~0.5"
https://sites.psu.edu/astrowright/2015/01/06/loooooooong-period-exoplanets/

This calculation was published in https://arxiv.org/pdf/1501.00633.pdf,
Katherina Feng et al (2015).
"""

import numpy as np
from astropy import units as u, constants as const

baseline = 10*u.year
gammadot = -0.0422 *u.m/u.s/u.day
mstar = 0.864*u.Msun

M_min = (
    (0.0164 * u.Mjup) * (baseline/(1*u.yr))**(4/3)
    * np.abs(gammadot/(1*u.m/u.s/u.year))
    * (mstar/u.Msun)**(2/3)
)

print(M_min.to(u.Mjup))
print(M_min.to(u.Mearth))

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

#
# now double check the prefactor
#
ecc = 0.5
prefactor = (
    (1.25/(2*np.pi*const.G))**(1/3)
    * (1-ecc**2)**(1/2)
    * ( (1*u.Msun)**(2/3)*(1*u.m/u.s/u.year)*(1*u.year)**(4/3) )
)
print(prefactor.to(u.Mjup))
print((prefactor/2).to(u.Mjup))

#
# what if we used better units?
#
ecc = 0.5
prefactor = (
    (1.25/(2*np.pi*const.G))**(1/3)
    * (1-ecc**2)**(1/2)
    * ( (1*u.Msun)**(2/3)*(1*u.m/u.s/u.day)*(1*u.year)**(4/3) )
)
print(prefactor.to(u.Mjup))
print((prefactor/2).to(u.Mjup))

M_min = (
    (5.992106 * u.Mjup) * (baseline/(1*u.yr))**(4/3)
    * np.abs(gammadot/(1*u.m/u.s/u.day))
    * (mstar/u.Msun)**(2/3)
)
print(M_min)


import functools
import time
import unittest
from ..setting import setting
from . import log
from ..tag import Tag
from ..case_way.case_decorators import *

CASE_TAG_FLAG = "__case_tag__"
CASE_DATA_FLAG = "__case_data__"
CASE_DATA_UNPACK_FLAG = "__case_data_unpack__"
CASE_ID_FLAG = "__case_id__"
CASE_INFO_FLAG = "__case_info__"
CASE_RUN_INDEX_FlAG = "__case_run_index_flag__"
CASE_SKIP_FLAG = "__unittest_skip__"
CASE_SKIP_REASON_FLAG = "__unittest_skip_why__"

__all__ = [
    'Tool',
    'CASE_TAG_FLAG',
    'CASE_DATA_FLAG',
    'CASE_DATA_UNPACK_FLAG',
    'CASE_ID_FLAG',
    'CASE_INFO_FLAG',
    'CASE_RUN_INDEX_FlAG',
    'CASE_SKIP_FLAG',
    'CASE_SKIP_REASON_FLAG',
    "skip", "skip_if", "data", "tag"
]

def skip(reason):
    def wrap(func):
        return unittest.skip(reason)(func)

    return wrap

def skip_if(condition, reason):
    def wrap(func):
        return unittest.skipIf(condition, reason)(func)

    return wrap

def data(*values, unpack=True):
    """注入测试数据，可以做为测试用例的数据驱动
    1. 单一参数的测试用例
    @data(10001, 10002, 10003)
    def test_receive_bless_box(self, box_id):
        print(box_id)

    2. 多个参数的测试用例
    @data(["gold", 100], ["diamond", 500])
    def test_bless(self, bless_type, award):
        print(bless_type)
        print(award)

    3. 是否对测试数据进行解包
    @data({"gold": 1000, "diamond": 100}, {"gold": 2000, "diamond": 200}, unpack=False)
    def test_get_battle_reward(self, reward):
        print(reward)
        print("获得的钻石数量是：{}".format(reward['diamond']))

    :param values:测试数据
    :param unpack: 是否解包
    :return:
    """

    def wrap(func):
        if hasattr(func, CASE_DATA_FLAG):
            log.error("{}的测试数据只能初始化一次".format(func.__name__))
        else:
            setattr(func, CASE_DATA_FLAG, values)
            setattr(func, CASE_DATA_UNPACK_FLAG, unpack)
        return func

    return wrap

def tag(*tag_type):
    """指定测试用例的标签，可以作为测试用例分组使用，用例默认会有Tag.ALL标签，支持同时设定多个标签，如：
    @tag(Tag.V1_0_0, Tag.SMOKE)
    def test_func(self):
        pass

    :param tag_type:标签类型，在tag.py里边自定义
    :return:
    """

    def wrap(func):
        if not hasattr(func, CASE_TAG_FLAG):
            tags = {Tag.ALL}
            tags.update(tag_type)
            setattr(func, CASE_TAG_FLAG, tags)
        else:
            getattr(func, CASE_TAG_FLAG).update(tag_type)
        return func

    return wrap

def _handler(func):
    @functools.wraps(func)
    def wrap(*args, **kwargs):
        time.sleep(setting.execute_interval)
        msg = "start to test {} ({}/{})".format(getattr(func, CASE_INFO_FLAG),
                                                getattr(func, CASE_RUN_INDEX_FlAG),
                                                Tool.actual_case_num)
        log.info(msg)
        result = func(*args, **kwargs)
        return result

    return wrap

class Tool:
    actual_case_num = 0
    total_case_num = 0

    @classmethod
    def create_case_id(cls):
        cls.total_case_num += 1
        return cls.total_case_num

    @classmethod
    def create_actual_run_index(cls):
        cls.actual_case_num += 1
        return cls.actual_case_num

    @staticmethod
    def modify_func_name(func):
        """修改函数名字，实现排序 eg test_fight ---> test_00001_fight

        :param func:
        :return:
        """
        case_id = Tool.create_case_id()
        setattr(func, CASE_ID_FLAG, case_id)
        if setting.sort_case:
            func_name = func.__name__.replace("test_", "test_{:05d}_".format(case_id))
        else:
            func_name = func.__name__
        return func_name

    @staticmethod
    def general_case_name_with_test_data(func_name, index, test_data):
        if setting.full_case_name:
            params_str = "_".join([str(_) for _ in test_data]).replace(".", "")
            func_name += "_{:05d}_{}".format(index, params_str)
        else:
            func_name += "_{:05d}".format(index)
        if len(func_name) > setting.max_case_name_len:
            func_name = func_name[:setting.max_case_name_len] + "……"
        return func_name

    @staticmethod
    def create_case_with_case_data(func):
        result = dict()
        for index, test_data in enumerate(getattr(func, CASE_DATA_FLAG), 1):
            if not hasattr(func, CASE_SKIP_FLAG):
                setattr(func, CASE_RUN_INDEX_FlAG, Tool.create_actual_run_index())

            func_name = Tool.modify_func_name(func)
            if isinstance(test_data, list):
                func_name = Tool.general_case_name_with_test_data(func_name, index, test_data)
                if getattr(func, CASE_DATA_UNPACK_FLAG, None):
                    result[func_name] = _handler(_feed_data(*test_data)(func))
                else:
                    result[func_name] = _handler(_feed_data(test_data)(func))

            elif isinstance(test_data, dict):
                func_name = Tool.general_case_name_with_test_data(func_name, index, test_data.values())
                if getattr(func, CASE_DATA_UNPACK_FLAG, None):
                    result[func_name] = _handler(_feed_data(**test_data)(func))
                else:
                    result[func_name] = _handler(_feed_data(test_data)(func))

            elif isinstance(test_data, (int, str, bool, float)):
                func_name = Tool.general_case_name_with_test_data(func_name, index, [test_data])
                result[func_name] = _handler(_feed_data(test_data)(func))

            else:
                raise Exception("无法解析{}".format(test_data))

        return result

    @staticmethod
    def create_case_without_case_data(func):
        if not hasattr(func, CASE_SKIP_FLAG):
            setattr(func, CASE_RUN_INDEX_FlAG, Tool.create_actual_run_index())

        result = dict()
        func_name = Tool.modify_func_name(func)
        if len(func_name) > setting.max_case_name_len:
            func_name = func_name[:setting.max_case_name_len] + "……"
        result[func_name] = _handler(func)
        return result

    @staticmethod
    def filter_test_case(funcs_dict):
        funcs = dict()
        cases = dict()
        for i in funcs_dict:
            if i.startswith("test_"):
                cases[i] = funcs_dict[i]
            else:
                funcs[i] = funcs_dict[i]

        return funcs, cases

def _feed_data(*args, **kwargs):
    def wrap(func):
        @functools.wraps(func)
        def _wrap(self):
            return func(self, *args, **kwargs)

        return _wrap

    return wrap


from utils.model import Perceptron
from utils.all_utils import prepare_data, save_model
import pandas as pd
import logging

logging_str = "[ %(asctime)s - %(levelname)s - %(module)s - %(message)s] %"
logging.basicConfig(level=logging.INFO, format=logging_str)

def main(data, eta, epochs, filename):

    df = pd.DataFrame(data)
    df

    x, y = prepare_data(df)

    model = Perceptron(eta=eta, epochs=epochs)  # Will initialize the random weights
    model.fit(x, y)

    save_model(model, filename)

if __name__ == '__main__':  # --> Entry point for this file

    LEARNING_RATE = 0.3
    EPOCHS = 10

    AND = {
            "x1": [0, 0, 1, 1],
            "x2": [0, 1, 0, 1],
            "y": [0, 0, 0, 1]
        }

    main(data=AND, eta=LEARNING_RATE, epochs=EPOCHS, filename="and.model")


import sqlite3

class d2db:
    def __init__(self, db_path):
        self.conn = sqlite3.connect(db_path)
        self.cur = self.conn.cursor()

    def __enter__(self):
        return self

    def __exit__(self, exception_type, exception_value, traceback):
        self.cur.close()
        self.conn.close()

    def query(self, definition, hash_id):
        query = "SELECT json FROM {} WHERE id={}"
        self.cur.execute(query.format(definition, hash_id))
        return (self.cur.fetchone())

    #Finds what table an ID/Hash belongs to
    def find_id_table(self, hash_id):
        self.conn.row_factory = sqlite3.Row
        self.cur = self.conn.cursor()
        self.cur.execute("SELECT name FROM sqlite_master WHERE type='table'")
        for tr in self.cur.fetchall():
            table = tr[0]
            query = "SELECT * FROM {} WHERE id=?"
            if(table == "DestinyHistoricalStatsDefinition"):
                query = "SELECT * FROM {} WHERE key=?"
            self.cur.execute(query.format(table), (hash_id,))
            for row in self.cur:
                for field in row.keys():
                    print(table, field, row[field])


import json
import sys

def json2obj(data):
	return json.loads(data)

def simplifier(my_dictionary):
	my_new_dict = {}
	for key, value in my_dictionary.items():
		if type(value) is dict: #simplify the inside
			simplified=simplifier(value)
			for new_key, new_value in simplified.items():
				my_new_dict_key = key + "." + new_key
				my_new_dict[my_new_dict_key] = new_value
		else:
			my_new_dict[key] = value
	return my_new_dict

def main():
	lines = sys.stdin.read()
	my_dictionary = json2obj(lines)
	result = simplifier(my_dictionary)
	print(result)
	return result

main()


import re

from lxml import etree
from woef.Countries import Countries

country_re = re.compile("<Country>(.*?)<\/Country>")

def get_country_from_cmdi_file_lxml(cmdi_path):
    # Return value
    country = Countries.UNKNOWN

    root = etree.parse(cmdi_path)
    # I HATE NAMESPACES
    country_nodes = root.xpath(
        "//*[local-name() = 'Source']/*[local-name() = 'Country']")

    if len(country_nodes) > 0:
        element_text = country_nodes[0].text
        if element_text == "NL":
            country = Countries.NETHERLANDS
        elif element_text == "B":
            country = Countries.BELGIUM

    return country

def get_country_from_cmdi_file(cmdi_path):
    # Return value
    country = Countries.UNKNOWN

    with open(cmdi_path, "rt") as reader:
        for line in reader:
            if "<Country>" in line:
                element_text = country_re.search(line).group(1)

                if element_text == "NL":
                    country = Countries.NETHERLANDS
                elif element_text == "B":
                    country = Countries.BELGIUM

                return country

        return country


import numpy
import math
import matplotlib.pyplot as plt

def normalize(X):
	m = X.mean(axis=0)
	sd = X.std(axis=0)
	Z = numpy.empty(X.shape)
	for i,rec in enumerate(X):
		Z[i] = (rec - m)/sd
	return Z

def sigmoid(x, theta):
	return 1/(1+math.exp(-numpy.dot(x,theta)))

def hThetaX(X, theta):
	z = numpy.dot(X,theta)
	h = 1/(1 + numpy.exp(-z))
	return h

def getGradient(X, Y, theta):
	h = hThetaX(X, theta)
	grad = numpy.dot(X.T, (Y-h))
	return grad

def getHessian(X, theta):
	predicted = hThetaX(X,theta)
	m = predicted.shape[0]
	D = numpy.eye(m)
	for i,val in enumerate(predicted):
		D[i][i] = -val*(1-val)
	return numpy.dot(X.T, numpy.dot(D, X))

def getTheta(X, Y, theta):
	hessian = numpy.mat(getHessian(X, theta))
	gradient = numpy.mat(getGradient(X, Y, theta))
	theta = theta - hessian.I*gradient
	return theta

tempX = numpy.loadtxt('logisticX.csv', delimiter=',')
Y = numpy.loadtxt('logisticY.csv', delimiter=',')

tempX = normalize(tempX)
X = [ numpy.insert(x, 0, 1) for x in tempX]
X = numpy.array(X)
#print(X)
Y = Y.reshape((Y.shape[0],1))
#print(Y)
theta = numpy.zeros((X.shape[1],1))
grad = getGradient(X,Y, theta)
m = max(grad)
while m > 0.0000001:
	t = theta
	theta = getTheta(X, Y, theta)
	m = max(abs(theta-t))
#print(theta)
#theta = [[ 0.40125316],[2.5885477] ,[-2.72558849]]

print('Theta')
print(theta)
plt.figure()
ax = plt.subplot('111')
for i,y in enumerate(Y):
	if y == 0:
		sc0 = plt.scatter(X[i,1], X[i,2], 30, color='red', marker='+')
	else:
		sc1 = plt.scatter(X[i,1], X[i,2], 30, color='blue', marker='*')

sc0.set_label('0')
sc1.set_label('1')
mins = tempX.min(axis = 0)
maxm = tempX.max(axis = 0)
x1 = numpy.linspace(mins[0]-1,maxm[0]+1,100)
x2 = numpy.linspace(mins[1]-1,maxm[1]+1,100)
x1,x2 = numpy.meshgrid(x1,x2)
z = numpy.zeros(x1.shape)
for i in range(x1.shape[0]):
	for j in range(x1.shape[1]):
		z[i][j] = sigmoid(numpy.array([1,x1[i][j], x2[i][j]]), theta)
plt.contour(x1, x2, z, levels=[0.5])
#prob = hThetaX(X,theta)
#print(prob)
#plt.plot()
ax.set_xlabel('x1')
ax.set_ylabel('x2')
plt.legend()
plt.show()