import cartopy
import cartopy.crs as ccrs
import matplotlib
import pandas

def plot_patches(ax,obs,**kwargs):
    """Plot observations as points.

    Args:
        ax (:obj:`cartopy.mpl.geoaxes.GeoAxes`): Axes on which to draw.
        obs (:obj:`pandas.DataFrame`): Data frame containing obs positions.

    Keyword Args:
        obs_projection (:obj:`cartopy.crs.CRS`): Projection in which observations latitudes and longitudes are defined. Default is :class:`cartopy.crs.PlateCarree`.
        lat_label (:obj:`str`): Key, in the obs dataframe, of the latitude data. Defaults to 'Latitude'.
        lon_label (:obj:`str`): Key, in the obs dataframe, of the longitude data. Defaults to 'Longitude'.
        radius (:obj:`float`): Radius of circle marking each ob. (degrees). Defaults to 1.
        facecolor (see :mod:`matplotlib.colors`): Main colour of the circle to be plotted for each ob. Defaults to 'yellow'.
        edgecolor (see :mod:`matplotlib.colors`): Border colour of the circle to be plotted for each ob. Defaults to 'black'.
        alpha (:obj:`float`): Alpha value for facecolor and edgecolor. Defaults to 0.85. Will be multiplied by the observation weight if present.
        zorder (:obj:`float`): Standard matplotlib parameter determining which things are plotted on top (high zorder), and which underneath (low zorder), Defaults to 25.

    Returns:
        Nothing - adds the obs. points to the plot.

    |
    """

    kwargs.setdefault('obs_projection',ccrs.PlateCarree())
    kwargs.setdefault('lat_label'     ,        'Latitude')
    kwargs.setdefault('lon_label'     ,       'Longitude')
    kwargs.setdefault('radius'        ,                 1)
    kwargs.setdefault('facecolor'     ,          'yellow')
    kwargs.setdefault('edgecolor'     ,           'black')
    kwargs.setdefault('alpha'         ,              0.85)
    kwargs.setdefault('zorder'        ,                25)

    rp=ax.projection.transform_points(kwargs.get('obs_projection'),
                                   obs[kwargs.get('lon_label')].values,
                                   obs[kwargs.get('lat_label')].values)
    new_longitude=rp[:,0]
    new_latitude=rp[:,1]

    # Plot each ob as a circle
    for i in range(0,len(new_longitude)):
        weight=1.0
        if 'weight' in obs.columns: weight=obs['weight'].values[i]
        ax.add_patch(matplotlib.patches.Circle((new_longitude[i],
                                                new_latitude[i]),
                                                radius=kwargs.get('radius'),
                                                facecolor=kwargs.get('facecolor'),
                                                edgecolor=kwargs.get('edgecolor'),
                                                alpha=kwargs.get('alpha')*weight,
                                                zorder=kwargs.get('zorder')))

# Plot observations
def plot(ax,obs,**kwargs):
    """Plot observations.

    Generic function for plotting observations. Use the 'type' argument to choose the plot style.

    Args:
        ax (:obj:`cartopy.mpl.geoaxes.GeoAxes`): Axes on which to draw.
        obs (:obj:`pandas.DataFrame`): Dataframe containing obs positions.

    Kwargs:
        type (:obj:`str`, optional): Style to plot. Only option is: 'patches', (default) which delegates plotting to :meth:`plot_patches`.

        Other keyword arguments are passed to the style-specific plotting function.

    Returns:
        Nothing - adds the obs. points to the plot.
    |
    """

    kwargs.setdefault('type','patches')

    if kwargs.get('type')=='patches':
        return plot_patches(ax,obs,**kwargs)

    raise Exception('Unsupported observations plot type %s' %
                         kwargs.get('type'))


#-*- coding: UTF-8 -*-
import os
import json
import time
from core.keyboardinterceptor import KeyboardInterceptor
from queue import Queue
from core.keyhandler import KeyHandler

DEFAULT_CONFIG = {
    "buffCoef1": 0.3,
    "buffCoef2": 0.6,
    "buffCoef3": 4.0,
    "buffCoef4": 8.0,
    "buffCoef5": 12.0,
    "shiftBuff": 2,
    "spaceBuff": 3
}

class AppController:
    queue = None
    interceptor = None
    keyHandler = None
    engaged = False
    ui = None

    def __init__(self):
        pass

    def start(self):
        self.queue = Queue()
        self.interceptor = KeyboardInterceptor(self.queue)
        self.interceptor.controller = self
        self.keyHandler = KeyHandler(self.interceptor,self.queue,self.getConfig())
        self.interceptor.start()
        self.keyHandler.start()
        self.engaged = True
        self.updateUIStatus()
        print('controller start')

    def stop(self):
        print('controller stop')
        self.keyHandler.stop()
        self.interceptor.stop()
        self.engaged = False
        self.updateUIStatus()

    def startFilter(self):
        self.interceptor.enable = True

    def stopFilter(self):
        self.interceptor.enable = False

    def getConfig(self):
        configData = None
        if not os.path.exists('config.json'):
            return DEFAULT_CONFIG
        with open('config.json','r',encoding='utf8')as f:
            configData = json.load(f)
        return configData

    def submitConfig(self,configData):
        self.stop()
        with open("config.json", 'w') as f:
            json.dump(configData, f, indent=4, ensure_ascii=False)
        self.start()

    def updateUIStatus(self):
        self.ui.updateStatus(self.engaged,self.interceptor.enable)

if __name__ == '__main__':
    app = AppController()
    app.submitConfig({'buffCoef1': 0.1, 'buffCoef2': 0.6, 'buffCoef3': 99, 'buffCoef4': 8, 'buffCoef5': 12, 'shiftBuff': 2, 'spaceBuff': 3})
    time.sleep(1)
    print(app.getConfig())


import re
from collections import OrderedDict
import math

class Army(list):
    armies = []

    def __init__(self, name):
        super().__init__()
        self.name = name
        self.__class__.armies.append(self)

    @property
    def units(self):
        return sum([g.units for g in self])

    def append(self, group):
        super().append(group)
        group.army = self

    def reset(self):
        for g in self:
            g.reset()

    def boost(self, boost):
        for g in self:
            g.set_boost(boost)

    def __repr__(self):
        return "Army<name={}, units={}>".format(self.name, self.units)

class Group():
    def __init__(self, input):
        match = re.match(r"(\d+) units each with (\d+) hit points\s?(\((.+)\))? with an attack that does (\d+) (\w+) damage at initiative (\d+)", input)
        self.__input = input

        self.units_orig = int(match.group(1))
        self.units = self.units_orig

        self.hp = int(match.group(2))
        self.attack_power = int(match.group(5))
        self.dmg_type = match.group(6)
        self.initiative = int(match.group(7))
        self.boost = 0
        self.weaknesses = []
        self.immunities = []

        self.army = []  # this will be set when the group is added to the army

        if match.group(4):
            split = [x.strip() for x in match.group(4).split(';')]
            for x in split:
                if x.startswith("weak to "):
                    x = x.replace("weak to ","")
                    self.weaknesses = [w.strip() for w in x.split(',')]
                    pass
                if x.startswith("immune to "):
                    x = x.replace("immune to ","")
                    self.immunities = [i.strip() for i in x.split(',')]

    @property
    def effective_power(self):
        return self.units * (self.attack_power + self.boost)

    @property
    def group(self):
        return self.army.index(self) + 1

    @property
    def total_hp(self):
        return self.units * self.hp

    @property
    def dead(self):
        return self.units == 0

    def set_boost(self, boost):
        self.boost = boost

    def reset(self):
        self.units = self.units_orig

    def attack_dmg(self, defending_group):
        if self.dmg_type in defending_group.immunities:
            return 0
        if self.dmg_type in defending_group.weaknesses:
            return self.effective_power * 2
        return self.effective_power

    def attack(self, defending_group):
        dmg = self.attack_dmg(defending_group)

        units_left = max(0, int(math.ceil((defending_group.total_hp - dmg)/defending_group.hp)))
        units_killed = min(defending_group.units, defending_group.units - units_left)
        defending_group.units = units_left

        return (units_killed, units_left)

    def __repr__(self):
        return "{} Group {} <effective={}, initiative={}, units={}>'".format(self.army.name, self.group, self.effective_power, self.initiative, self.units)

immune = Army("Immune System")
infection = Army("Infection")

with open("input.txt", "r") as ins:
    input = ins.read()
    input = input.strip().split("\n\n")
    # immune system
    for group in input[0].split("\n")[1:]:
        immune.append(Group(group))

    # infection system
    for group in input[1].split("\n")[1:]:
        infection.append(Group(group))

immune_orig = immune
infection_orig = infection

def target_selection():
    # returns a dictionary of which group will target which other group
    ret = OrderedDict()
    groups = [g for g in (infection+immune) if not g.dead]
    groups = sorted(groups, key=lambda x: (x.effective_power, x.initiative), reverse=True)

    for g in groups:
        defending_army = infection if g in immune else immune
        # we choose the one with the highest attack, followed by highest effective, then lastly highest initiative
        real_order = [x for x in sorted(defending_army, key=lambda x: (g.attack_dmg(x), x.effective_power, x.initiative), reverse=True) if x not in ret.values() and not x.dead]
        if len(real_order) == 0:
            continue

        if g.attack_dmg(real_order[0]) == 0:  # double check that the attacker can actually deal damage.
            continue

        # some debugging stuff, can get rid of in favor of real_order
        max_dmg = max([g.attack_dmg(x) for x in defending_army])
        order = [x for x in defending_army if g.attack_dmg(x) == max_dmg]
        #[print("{} group {} would deal defending group {} {} damage {}".format(g.army.name, g.group, x.group, g.attack_dmg(x), '*' if real_order[0] == x else '')) for x in order ]

        ret[g] = real_order[0]

    return ret

def attack(targets):
    # we fight in decresing initiative
    #print("")
    attackers = sorted([t for t in targets.keys() if not t.dead], key=lambda x: x.initiative, reverse=True)
    for attacker in attackers:
        if attacker.dead:
            continue  # this attacker was killed somewhere in this loop, skip them
        defender = targets[attacker]
        killed, left = attacker.attack(defender)
        if killed == 0:
            pass
        #print("{} group {} attacks defending group {}, killing {} units, dead: {}".format(attacker.army.name, attacker.group, defender.group, killed, defender.dead))
        pass
    #print ("="*20)
    pass

def fight():
    while not any(army.units == 0 for army in Army.armies):
        old_unit_count = infection.units, immune.units
        targets = target_selection()
        attack(targets)
        if (infection.units, immune.units) == old_unit_count:
            return True

fight()

print([a for a in Army.armies if a.units != 0][0])

# Couldn't get the stalemate logic worked out correctly, so I just analyze the print out here to determine the answer. I know that test 14 cause thes army to die,
# and this starts at test 15 which starts a series of stalemates. So the next test that the army srurvives is the winner.
# todo: fix the stalemate logic - when hitting a stalemate, it should continue to test in decresing values until it does not hit a stalemate
min_test = 0
max_test = 30
stalemates = set([])

while True:
    for a in Army.armies:
        a.reset()

    if max_test is None:
        # continue testing by adding 2000 to min
        test = min_test + 2000
        immune.boost(test)
        fight()

        if immune.units != 0:
            max_test = test
        else:
            min_test = test
        continue

    test = min_test + int((max_test - min_test) / 2)

    immune.boost(test)
    stalemate = fight()

    if stalemate:
        min_test += 1
        stalemates.add(test)
        print("from {} to {} test: {} stalemate".format(min_test, max_test, test))
        continue

    print("from {} to {} test: {} army died: {}, units: {}".format(min_test, max_test, test, immune.units == 0,immune.units ))

    if immune.units == 0:
        # immune died, our test was not high enough,
        min_test = test
    else:
        max_test = test

    if max_test - min_test == 1:
        print(max_test)
        break


import _io
from io import BytesIO

import numpy as np
import streamlit as st
import plyfile as ply
from numpy.lib import recfunctions as rfn

def parse_npz(byte_obj: _io.BytesIO) -> np.ndarray:
    """
    Parse point cloud saved as a compressed numpy array.

    :param byte_obj: input object to be parsed.
    :type byte_obj: _io.BytesIO
    :returns: point cloud
    :rtype: np.ndarray
    """
    blob_container = np.load(byte_obj, encoding="bytes")
    blob_name = blob_container.__dict__["files"][-1]
    return blob_container[blob_name]

def parse_npy(byte_obj: _io.BytesIO) -> np.ndarray:
    """
    Parse point cloud saved as a numpy array.

    :param byte_obj: input object to be parsed.
    :type byte_obj: _io.BytesIO
    :returns: point cloud
    :rtype: np.ndarray
    """
    blob = np.load(byte_obj, encoding="bytes")
    return blob

def _construct_blob(points: np.ndarray) -> np.ndarray:
    x, y, z = points[:, 0][:, np.newaxis], points[:, 1][:, np.newaxis], points[:, 2][:, np.newaxis]
    features = points[:, 3][:, np.newaxis]
    x_range = int(np.max(x)) + 1
    y_range = int(np.max(y)) + 1
    z_range = int(np.max(z)) + 1

    blob = np.zeros((x_range, y_range, z_range))
    blob[(x.astype(int), y.astype(int), z.astype(int))] = features
    return blob

def parse_ply(byte_obj: _io.BytesIO) -> np.ndarray:
    """
    Parse ply files
    ply file structure:
    x y z feature
    where x, y, z are coordinates and feature is a feature value at point with the given x, y, z coordinates

    :param byte_obj: input object to be parsed.
    :type byte_obj: _io.BytesIO
    :returns: point cloud
    :rtype: np.ndarray
    """
    plydata = ply.PlyData.read(byte_obj)

    points = np.array(plydata.elements[0].data)
    points = rfn.structured_to_unstructured(points)

    return _construct_blob(points)

def parse_xyz_pts_txt(byte_obj: _io.BytesIO, ext: str) -> np.ndarray:
    """
    Parse xyz, pts and txt files
    - xyz and txt file structure: should contain only numerical data (without any header), each line should have
        the following format:
        x y z feature
        where x, y, z are coordinates and feature is a feature value at point with the given x, y, z coordinates;
        lines should be separated with \n character
    - pts file structure: in the first line there should be information about the number of points in the given
        point cloud, later the structure follows the xyz format structure

    :param byte_obj: input object to be parsed.
    :type byte_obj: _io.BytesIO
    :param ext: file extension
    :type ext: str
    :returns: point cloud
    :rtype: np.ndarray
    """
    if ext == 'pts':
        _ = byte_obj.readline()
    points = np.loadtxt(byte_obj, encoding="bytes")
    return _construct_blob(points)

def parse_csv(byte_obj: _io.BytesIO) -> np.ndarray:
    """
    Parse csv files
    - csv file structure: should contain only numerical data (without any header), each line should have
        the following format:
        x, y, z, feature
        where x, y, z are coordinates and feature is a feature value at point with the given x, y, z coordinates;
        lines should be separated with \n character

    :param byte_obj: input object to be parsed.
    :type byte_obj: _io.BytesIO
    :returns: point cloud
    :rtype: np.ndarray
    """
    try:
        points = np.loadtxt(byte_obj, delimiter=',', encoding="bytes")
    except ValueError:
        _ = byte_obj.readline()
        points = np.loadtxt(byte_obj, delimiter=',', encoding="bytes")
    return _construct_blob(points)

def parse(file: st.runtime.uploaded_file_manager.UploadedFile) -> np.ndarray:
    """
    Parse point cloud.

    :param byte_obj: input object to be parsed.
    :type byte_obj: _io.BytesIO
    :returns: point cloud
    :rtype: np.ndarray
    """
    byte_object = BytesIO(file.getvalue())

    ext = file.__dict__["name"].split(".")[-1]
    if ext == "npz":
        return parse_npz(byte_object)
    elif ext == "npy":
        return parse_npy(byte_object)
    elif ext == "ply":
        return parse_ply(byte_object)
    elif ext == "xyz" or ext == "pts" or ext == "txt":
        return parse_xyz_pts_txt(byte_object, ext)
    elif ext == "csv":
        return parse_csv(byte_object)


from pathlib import Path
from typing import Dict, Iterable, List, Optional

from playhouse.sqlite_ext import SqliteExtDatabase
import json
import click

from ainar.database import proxy
from ainar.database.coco import Annotation, Category, Image
from ainar.datasets.coco import Coco, load_dataset
from ainar.filters import filter_dataset, parse_filter_string
from ainar.aggregates import aggregate

@click.command()
@click.argument("dataset_json", type=click.Path(exists=True))
@click.option("--cache", type=Path, default=None, help="")
@click.option("--filter", default=None, help="")
@click.option("--output", default=None, help="")
@click.option("--export", default=None, help="")
def main(**args):
    args = validate_args(**args)

    # Check cache file existence
    has_cache = args["cache"].exists()

    # Create sqlite client
    db = SqliteExtDatabase(args["cache"])
    proxy.initialize(db)

    # TODO: Add force analize option
    # Analize dataset if cache does not exists
    if not has_cache:
        store_dataset(db, load_dataset(args["dataset_json"]))

    # Filtering
    categories = filter_dataset(args["filter"])

    # Aggregation
    data = aggregate(categories, args["filter"])

    # Display results
    display_data(data, args["output"])

    # Export to file
    if args["export"]:
        export(categories)

def validate_args(
    dataset_json: Path,
    cache: Optional[str],
    filter: Optional[str],
    output: Optional[str],
    export: Optional[str],
):
    return {
        "dataset_json": dataset_json,
        "cache": cache if cache else create_cache_path_from_dataset_json(dataset_json),
        "filter": parse_filter_string(filter) if filter else {},
        "output": json.loads(output) if output else {},
        "export": export,
    }

def create_cache_path_from_dataset_json(path: Path):
    return Path("./hogehoge.sqlite3")

def store_dataset(db: SqliteExtDatabase, dataset: Coco):
    # Create tables
    db.create_tables([Annotation, Category, Image], safe=True)

    images = {image["id"]: image for image in dataset.images}
    annotations = [
        {
            **annotation,
            "area_ratio": (
                annotation["area"]
                / (images[annotation["image_id"]]["width"] * images[annotation["image_id"]]["height"])
            ),
        }
        for annotation in dataset.annotations
    ]

    # Save data
    with db.atomic():
        Category.insert_many(dataset.categories).execute()
        Image.insert_many(dataset.images).execute()
        Annotation.insert_many(annotations).execute()

def display_data(data: List[Dict], options: Dict):
    # TODO: Display data
    print(data)

def export(categories: Iterable[Category]):
    # TODO: Export data
    pass

if __name__ == "__main__":
    main()


import bpy
from bpy.props import BoolProperty, IntProperty

#法線をアクティブにする、既にチェック済み場合は無視。
def oSm(b):
    for o in bpy.context.selected_objects:
        if o.type == 'MESH':
            bpy.ops.object.shade_smooth()
            if o.data.use_auto_smooth == False:
                o.data.use_auto_smooth = True
                o.data.auto_smooth_angle = b
        elif o.type == 'CURVE':
            o.data.splines[0].use_smooth = True

def si_active_normal():
    oCM = bpy.context.object.mode
    if oCM == 'OBJECT':
        oSm(1.0472)
    else:
        bpy.ops.object.mode_set(mode='OBJECT')
        oSm(1.0472)
        bpy.ops.object.mode_set ( mode = oCM )

#サブディビジョンモデファイア削除、法線設定もデフォルトに戻す
def oDeln():
    for o in bpy.context.selected_objects:
        if o.type == 'MESH':
            bpy.ops.object.shade_flat()
            o.data.use_auto_smooth = False
            o.data.auto_smooth_angle = 0.523599
        elif o.type == 'CURVE':
            o.data.splines[0].use_smooth = False

def si_noactive_normal():
    oCM = bpy.context.object.mode
    if oCM == 'OBJECT':
        oDeln()
    else:
        bpy.ops.object.mode_set(mode='OBJECT')
        oDeln()
        bpy.ops.object.mode_set ( mode = oCM )

#サブディビジョン適応。法線適応コマンドのために、
#一々オブジェクトモードに切り替えて実行後、もとに戻す。
def oDel():
    for o in bpy.context.selected_objects:
        for m in o.modifiers:
            if(m.type == "SUBSURF"):
                o.modifiers.remove(m)
    return

def delete_subdiv():
    oCM = bpy.context.object.mode
    if oCM == 'OBJECT':
        oDel()
    else:
        bpy.ops.object.mode_set(mode='OBJECT')
        oDel()
        bpy.ops.object.mode_set ( mode = oCM )

def GetModifires():
    for i in bpy.context.selected_objects:
        #モディファイアの適応数
        num = len(i.modifiers)
        return num

def xsisubdiv(oL, oAdd, maxSubdivCount=3):
    #サブディビモデファイアの名前が一致したらプレビューの数値を増やす
    for o in bpy.context.selected_objects:
        #メッシュかカーブ判定
        if o.type == 'MESH' or o.type == 'CURVE':
            #名称規則でSI_subdivが存在しなければ新規でモデファイア生成
            if o.modifiers.get("SI_subdiv") == None:
                #すでに知らん名前のサブディビモデファイアがあれば全てさよならグッバイ
                for m in o.modifiers:
                    if(m.type == "SUBSURF"):
                        o.modifiers.remove(m)
                oLevel =o.modifiers.new("SI_subdiv",type = "SUBSURF")
                oLevel.render_levels = oL
                oLevel.levels = oL
                #ケージモードON
                o.modifiers["SI_subdiv"].show_on_cage = True
                print(o.name + " add SI_subdiv,Set CageMode")

            #名称規則でSI_subdivが存在していれば、サブディビジョンレベルを＋１
            #ただし、知らん名前のSUBSURFがあれば放置
            else:
                for m in o.modifiers:
                    if(m.type == "SUBSURF" or m.name == "SI_subdiv"):
                        #上限設定
                        if m.render_levels < maxSubdivCount:
                            m.render_levels = m.render_levels+ oAdd
                            m.levels = m.levels+ oAdd
                            print(o.name + " subdiv level " + str(m.levels))
    #ビューのリフレッシュ
    bpy.context.scene.frame_current = bpy.context.scene.frame_current
    return

class si_del_subdiv_OT_object(bpy.types.Operator):
    bl_idname = "object.si_del_sudiv"
    bl_label = "Delete Subdivision"
    bl_description = "Subdivisions like Softimage"
    bl_options = {'REGISTER', 'UNDO'}

    def execute(self, context):
        delete_subdiv()
        si_noactive_normal()
        return {'FINISHED'}

class si_add_subdiv_OT_object(bpy.types.Operator):
    bl_idname = "object.si_add_sudiv"
    bl_label = "Add Subdivision"
    bl_description = "Subdivisions like Softimage"
    bl_options = {'REGISTER', 'UNDO'}

    #モデファイアを最後に移動するオプション
    si_moveindex : BoolProperty(default = True, name = "Move Modifire", description = "Move Modifire")
    maxSubdivCountInt : IntProperty(default = 3, name = "Max Subdivisions", description = "Max Subdivisions")

    def execute(self,context):
        si_active_normal()
        xsisubdiv(1, 1, maxSubdivCount= self.maxSubdivCountInt)

        if self.si_moveindex:
            #選択オブジェクトからモデファイア数を戻す
            num = GetModifires()
            bpy.ops.object.modifier_move_to_index(modifier="SI_subdiv",index= num - 1)
        return {'FINISHED'}

class si_minus_subdiv_OT_object(bpy.types.Operator):
    bl_idname = "object.si_minus_sudiv"
    bl_label = "Subtract subdivision"
    bl_description = "Subdivisions like Softimage"
    bl_options = {'REGISTER', 'UNDO'}

    def execute(self,context):
        xsisubdiv(1, -1, maxSubdivCount= 99)
        return {'FINISHED'}


import requests,bs4
from twilio.rest import Client

accountSID='**********************************'      #I have obtained the accountsid,authtoken and twilio no but will not be showing as it can be misused
authToken='********************************'

twilioCli=Client(accountSID,authToken)
mytwiliono='************'
myno='*************'
res=requests.get('https://weather.com/en-GB/weather/tenday/l/81cc0aa50e5a2f8287fd6848b8a6940233a43352ee461cf19c0dbd775515a51d')
res.raise_for_status()
parse=bs4.BeautifulSoup(res.text,'html.parser')
rain=parse.select('#detailIndex0 > div:nth-child(2) > div:nth-child(1) > div:nth-child(2) > div:nth-child(3) > div:nth-child(1) > span:nth-child(2)')
#getting the rain chance percentage
rainper=str(rain[0].getText())
rainperno=rainper[:-1]
# print(rainperno)
if int(rainperno)>70:
    twilioCli.messages.create(body='Akshaya it might rain today,make sure to take an umbrlla',from_=mytwiliono,to=myno)


import requests
from bs4 import BeautifulSoup
import re
import sys
sys.path.append('/home/nandagopal/eagle')
from data_collection.config import *

f_ratios_list = list(open(f_ratios_file, 'r').readlines())

def find_data_for_all_stock(sector=None):
    """
    method to be called from airflow

    loops through all urls and get their financial ratios

    :return:
    """
    for url in open(money_control_urls_file, 'r').readlines():
        if sector in url :
            find_data(url)

def find_data(url):
    """

    :param url: finace ration url of money control for single stock
    :return:

    INE428A01015,Basic EPS,-65.34,  -59.63,  -4.36,  -12.68,  11.39
    INE428A01015,Diluted Eps,-65.34,  -59.63,  -4.36,  -12.68,  11.39
    INE428A01015,Cash EPS,-39.09,  -53.69,  -2.11,  -10.21,  12.61

    """
    ofile = open(finance_csv, 'a')
    isin, sector, f_ratios, pl_ratios, q_ratios = url.split(delimiter)
    url = f_ratios
    print(isin, url)
    res = requests.get(url).text
    soup = BeautifulSoup(res, 'lxml')
    td_list = list(soup.find_all('td'))
    idx = 0
    for ele in td_list:
        ele = str(ele)
        try:
            for f_ratio in f_ratios_list:
                f_ratio = f_ratio.rstrip('\n')
                if f_ratio in ele :
                    print(f_ratio)
                    ratios = []
                    for i in range(1, 6):
                        data = td_list[idx+i]
                        # print(data)
                        ratios.append(re.findall(r'-?\d+.\d+',str(data))[0])
                    print(ratios)
                    ofile.write(isin + ',' + f_ratio +"," +",  ".join(ratios) + '\n')
            idx += 1
        except Exception as err:
            print(err)

find_data_for_all_stock('Banks - Public Sector')


import itertools

amt = int(input().rstrip())
for i in range(amt):
	line = input().rstrip().split(" ")
	ooga = list(line[1])
	ooga.sort()
	all_perms = list(itertools.permutations(ooga))
	#print(all_perms)
	print("{} {}".format(''.join(all_perms[ int(line[0]) ]) , ''.join(all_perms[len(all_perms)-int(line[0]) -1 ])))

#print(''.join(test[2]))


import collections

#FUNCTIONS THAT INVOLVE BUILDING AND MODIFYING BASIC GRAPHS

#takes an int specifying the number of vertices or an iterable containing the vertex names, and
#a list of the edges [(0, 1), (1, 2), (0, 2), (2, 4, 1)] (the third parameter, if given, is the order of a directed edge)
#with that, it clears and reconstructs the graph.
#TODO: make a directed version of this
def buildGraph(g, vertices, edges=None, vdata={}, edata={}):
    """
    @param g: The graph to write to.
    @type g: L{GraphEngine.BaseObjects.BaseGraph}
    @param vertices: A list of edges

    """
    g.clearGraph()

    if isinstance(vertices, collections.Iterable):
        uvertices = vertices
    else:
        uvertices = range(vertices)

    for x in uvertices:
        g.addVertex(x)

    if edges is not None:
        for x in edges:
            l = 0
            if len(x) >= 3:
                l = x[2]
            e = g.addEdge(x[0], x[1], l)
            if len(x) == 4:
                for i in x[3]:
                    g.setEdgeData(e, i[0], i[1])

    #print(vdata)
    for vd in vdata:
        for i in vdata[vd]:
            #print(str(vd) + " " + str(i[0]) + " " + str(i[1]))
            g.setVertexData(vd, i[0], i[1])

    for ed in edata:
        for i in edata[ed]:
            g.setEdgeData(ed, i[0], i[1])
    return

#Builds the graphs from dictionaries
#vertices should be either a number or a dictionary.
#If it is a number, then that many vertices will simply be added with
#integer ids.
#If it is a dictionary, then each entry is a vertex. There are 2 entries that are important:
#"data" and "name". "data" should be a dictionary of data entries attached to that vertex. "name" is
#the name of the vertex. if this is not present, then the dictionary key in vertices of that entry will
#be used.
#edges should be None or a dictionary. If None, then there will be no edges. If a dictionary.
#then each entry will be an edge. #There are 4 important entries to each edge. "name", "vertices", "directed", and "data".
#"data" and "name" work the same as with vertices. "vertices" is a tuple containing the 2 vertices connected by
#the edge. "directed" should be True or False. If True, then it is directed from the first vertex in the "vertices" tuple
#to the second. You could also set "directed" to -1 to have it be directed from the second to the first.
def buildGraphD(g, vertices, edges=None):
    g.clearGraph()
    if not isinstance(vertices, collections.Iterable):
        o = {}
        for v in range(vertices):
            o[v] = {"name":v}
        vertices = o

    for key in vertices:
        name = vertices[key]["name"] if "name" in vertices[key] else key
        datadic = vertices[key]["data"] if "data" in vertices[key] else None
        g.addVertex(name)
        if datadic is not None:
            for k in datadic:
                g.setVertexData(name, k, datadic[k])

    if edges is not None:
        for key in edges:
            name = edges[key]["name"] if "name" in edges[key] else key
            vertices = edges[key]["vertices"]
            directed = edges[key]["directed"] if "directed" in edges[key] else 0
            datadic = edges[key]["data"] if "data" in edges[key] else None
            g.addEdge(vertices[0], vertices[1], directed, name)
            if datadic is not None:
                for k in datadic:
                    g.setEdgeData(name, k, datadic[k])
    return

#appends g2 and all of its properties to g. Returns the first vertex id and the first edge id
def appendGraph(g, g2):
   #get and sort the vertex lists
   vl = [x for x in g2.getVertexList()]
   vl.sort()
   el = [x for x in g2.getEdgeList()]
   el.sort()

   vstart = g.getOrder()
   estart = g.getSize()

   #setup the new vertices
   for i in vl:
      n = g.addVertex()
      for di in g2.getVertexDataKeys(i):
         g.setVertexData(n, di, g2.getVertexData(i, di))

   v2ne = {}
   for i in range(len(el)):
      v2ne[el[i]] = i
   #set up the new edges
   for i in el:
      edgeinfo = g2.getEdgeInfo(i)
      n = g.addEdge(v2ne[edgeinfo[0]]+vstart, v2ne[edgeinfo[1]]+vstart, edgeinfo[2])
      for di in g2.getEdgeDataKeys(i):
         g.setEdgeData(n, di, g2.getEdgeData(i, di))
   return (vstart, estart)


"""
Plot helpers

"""

import matplotlib.pyplot as plt
import numpy as np

__all__ = ['plot_pressure', 'plot_filltime', 'show_flowfront', 'show_kmaps']

def plot_pressure(pr, showlegend=True):
    """
    Plot pressure for all the rows

    Parameters
    ----------
    pr : np.ndarray
        Pressure values
    showlegend : bool
        Show legend on the plot

    Returns
    -------
    None

    """
    plt.plot(np.transpose(pr))

    plt.title('Pressure values for all nodes')
    plt.xlabel('x nodes')
    plt.ylabel('pressure level')
    #if showlegend:
    #    plt.legend(['row' + str(i+1) for i in range(self.ynodes)])

    plt.show()

def plot_filltime(ft, showlegend=True):
    """
    Plot filltime for all the rows

    Parameters
    ----------
    ft : np.ndarray
        Filltime for all nodes
    showlegend : bool
        Show legend on the plot

    Returns
    -------
    None

    """

    plt.plot(np.transpose(ft))

    plt.title('filltime for all nodes')
    plt.xlabel('x nodes')
    plt.ylabel('filltime')
    #if showlegend:
    #    plt.legend(['row' + str(i+1) for i in range(self.ynodes)])

    plt.show()

def show_flowfront(ft):
    """
    Show flowfront

    Parameters
    ----------
    ft: np.ndarray
        flowtime

    Returns
    -------
    None

    """

    from copy import copy

    #cmap = copy(plt.cm.get_cmap("tab20b"))
    cmap = copy(plt.cm.get_cmap("jet"))
    cmap.set_under(color='white')

    plt.title('flowfront')
    #plt.imshow(ft, cmap=cmap, interpolation="bilinear", origin="lower", vmin=1e-12)
    plt.imshow(ft, cmap=cmap, origin="lower", vmin=1e-12)
    plt.xlim(0.5, ft.shape[1]-.5)
    plt.ylim(0.5, ft.shape[0]-.5)
    plt.colorbar()
    plt.show()

def show_kmaps(m):
    """
    Show K maps

    Parameters
    ----------
    m: Mesh
        Mesh data to be shown

    Returns
    -------
    None

    """

    from copy import copy

    cmap = copy(plt.cm.get_cmap("tab20b"))
    cmap.set_under(color='black')

    kxx = m.get_cell_kxx()
    kyy = m.get_cell_kyy()
    kxy = m.get_cell_kxy()

    fig, (ax1, ax2, ax3) = plt.subplots(3)
    fig.suptitle('K maps for the mesh')

    #im = ax1.imshow(kxx, cmap=cmap, interpolation="bilinear", origin="lower")
    im = ax1.imshow(kxx, cmap=cmap, origin="lower")
    ax1.set_xlim(0, kxx.shape[1]-1)
    ax1.set_ylim(0, kxx.shape[0]-1)
    ax1.set_title('Kxx')
    fig.colorbar(im, ax=ax1)

    ax2.imshow(kyy, cmap=cmap, origin="lower")
    ax2.set_xlim(0, kyy.shape[1]-1)
    ax2.set_ylim(0, kyy.shape[0]-1)
    ax2.set_title('Kyy')
    fig.colorbar(im, ax=ax2)

    ax3.imshow(kxy, cmap=cmap, origin="lower")
    ax3.set_xlim(0, kxy.shape[1]-1)
    ax3.set_ylim(0, kxy.shape[0]-1)
    ax3.set_title('Kxy')
    fig.colorbar(im, ax=ax3)

    plt.show()


import requests
def getHTMLText(url,kv):
    try:
        hd = {'user_agent':'Mozilla/5.0'}
        r = requests.get(url,headers = hd,params = kv)
        r.raise_for_status() #如果不是200，引发HTTPError异常
        r.encoding = r.apparent_encoding
        return r.text
    except:
        return "异常"

if __name__ == "__main__" :
    keyword = input('请输入搜索关键词')
    a = input('使用百度请按1，使用360搜索请按2')
    if a == '1':
        kv = {'wd=':keyword}
        url = "https://www.baidu.com/s"
    if a ==  '2':
        kv ={'q':keyword}
        url = "https://www.so.com/s"

    print(getHTMLText(url,kv)[:])

第1单元/2实例3：百度360搜索关键词提交.py

2实例3：百度360搜索关键词提交.py


from django.urls import path
from .views import (
    SiteSettingsView,
    PageListView,
    PageDetailView,
    NewsListView,
    NewsByCategoryListView,
    NewsByDateListView,
    NewsDetailView,
    ContactDetailsView,
    ContactDetailsMessageView,
)

urlpatterns = [
    path("site-settings/", SiteSettingsView.as_view(), name="site-settings"),
    path("pages/", PageListView.as_view(), name="pages"),
    path("pages/<slug:slug>/", PageDetailView.as_view(), name="page-detail"),
    path("news/", NewsListView.as_view(), name="news"),
    path("news/category/", NewsByCategoryListView.as_view(), name="news-by-category"),
    path("news/date/", NewsByDateListView.as_view(), name="news-by-date"),
    path("news/<slug:slug>/", NewsDetailView.as_view(), name="news-detail"),
    path("contact-details/", ContactDetailsView.as_view(), name="contact-details"),
    path("contact-details/message/", ContactDetailsMessageView.as_view(), name="contact-details-message"),
]


import random
import logging
import time
import argparse
import ConfigParser
from datetime import datetime

parser = argparse.ArgumentParser(description='Random Log Generator.')
parser.add_argument('-i', '--iterations', type=int, help='Number of iterations', required=True)
parser.add_argument('-f', '--framework', type=str, default='lumen', help='Name of framework')
parser.add_argument('-s', '--sleep', type=int, default=0, help='Delay for the log to be printed')
args = parser.parse_args()

config = ConfigParser.ConfigParser()
config.readfp(open('loggenerator.conf'))
msg = config.get(args.framework,"msg").split(',')

logging.basicConfig(
		filename='%s.log'%(str(datetime.now().date())), \
		level=logging.DEBUG,\
		format='%(message)s')

for i in range (args.iterations):
	logging.info('[%s] '%(str(datetime.now().strftime('%Y-%m-%d %H:%M:%S')))+msg[random.randint(0, len(msg)-1)])
	time.sleep(args.sleep)


"""Creates an interactive website displaying characteristics of electric vehicles"""

# Import Libraries
import pandas as pd
import numpy as np
import streamlit as st
import plotly_express as px
import plotly.figure_factory as ff

## READ AND CLEAN DATA
# Set Application Name
st.set_page_config(page_title='Electric Vehicle Data Visualizations')

# Read in dataset
url = 'https://raw.githubusercontent.com/kellyshreeve/Web-App-Project/main/ElectricCarData_Clean.csv'
ev = pd.read_csv(url)

# Rename columns with snake case
ev = ev.rename(
    columns={'Brand':'brand',
             'Model':'model',
             'AccelSec':'accel_sec',
             'TopSpeed_KmH':'top_speed_kmh',
             'Range_Km':'range_km',
             'Efficiency_WhKm':'efficiency_whkm',
             'FastCharge_KmH':'fast_charge_kmh',
             'RapidCharge':'rapid_charge',
             'PowerTrain':'power_train',
             'PlugType':'plug_type',
             'BodyStyle':'body_style',
             'Segment':'segment',
             'Seats':'seats',
             'PriceEuro':'price_euro'
             }
)

# Clean duplicates and missing values
ev['fast_charge_kmh'] = ev['fast_charge_kmh'].replace('-', np.NaN) # Change '-' to NaN

ev['fast_charge_kmh'] = pd.to_numeric(ev['fast_charge_kmh']) # Change fast_charge_kmh to int type data

ev = ev.drop_duplicates(subset=['brand', 'model']).reset_index(drop=True)

# Create top brands data frame
top_brands = ev[(ev['brand']=='Tesla ') | (ev['brand']=='Audi ') | (ev['brand']=='Nissan ')
                | (ev['brand']=='Volkswagen ') | (ev['brand']=='Skoda ') | (ev['brand']=='Renault ')
                | (ev['brand']=='Porsche ') | (ev['brand']=='BMW ') | (ev['brand']=='Ford ')
                | (ev['brand']=='Kia ') | (ev['brand']=='Smart ') | (ev['brand']=='Byton ')
                | (ev['brand']=='Mercedes ') | (ev['brand']=='Hyundai ') | (ev['brand']=='Opel ')]

## CREATE HEADER
st.header('Electric Vehicle Market Visualizations')

# Create Text
st.markdown('Welcome to the electric vehicle data visualization page! Click the buttons \nand toggle the colors to see how price, efficiency, and range of electric vehicles compare across the market.')

## CREATE BAR CHARTS
# Create a bar chart of average price by brand
price_bar = px.histogram(top_brands, x='brand', y='price_euro', histfunc='avg',
                          title='Average Price by Brand', text_auto='.2s',
                          labels={'price_euro':'Price (Euros)', 'brand':'Brand Name'},
                          color_discrete_sequence=[px.colors.qualitative.Plotly[7]],
                          width=800, height=500)

price_bar.update_layout({
    'plot_bgcolor':'rgba(0, 0, 0, 0)',
    'paper_bgcolor':'rgba(0, 0, 0, 0)'
})

price_bar.update_traces(textfont_size=11, textposition='outside') # Add labels above bars

price_bar.update_layout(xaxis={'categoryorder':'total descending'}) # Arrange in order from expensive to inexpensive

price_bar.update_xaxes(showgrid=False) # Turn off x grid
price_bar.update_yaxes(showgrid=False) # Turn off y grid

# Create bar chart of average efficiency by brand
eff_bar = px.histogram(top_brands, x='brand', y='efficiency_whkm', histfunc='avg',
                        title='Average efficiency by Brand', text_auto='.2s',
                        labels={'efficiency_whkm':'Efficiency (WhKm)', 'brand':'Brand Name'},
                        color_discrete_sequence=[px.colors.qualitative.Plotly[7]],
                        width=800, height=500)

eff_bar.update_layout({
    'plot_bgcolor':'rgba(0, 0, 0, 0)',
    'paper_bgcolor':'rgba(0, 0, 0, 0)'
})

eff_bar.update_traces(textfont_size=11, textposition='outside')

eff_bar.update_layout(xaxis={'categoryorder':'array', 'categoryarray':
    ['Porsche ', 'Audi ', 'Tesla ', 'Mercedes ', 'Byton ', 'Ford ', 'BMW ',
     'Nissan ', 'Skoda ', 'Hyundai ', 'Kia ', 'Opel ', 'Volkswagen ', 'Renault ', 'Smart ']})

eff_bar.update_xaxes(showgrid=False)
eff_bar.update_yaxes(range=[0,250], showgrid=False) # Set y axis range

# Bar chart of average range by brand
range_bar = px.histogram(top_brands, x='brand', y='range_km', histfunc='avg',
                         title='Average Range by Brand', text_auto='.2s',
                         labels={'range_km':'Range (Km)', 'brand':'Brand Name'},
                         color_discrete_sequence=[px.colors.qualitative.Plotly[7]],
                         width=800, height=500)

range_bar.update_layout({
    'plot_bgcolor':'rgba(0, 0, 0, 0)',
    'paper_bgcolor':'rgba(0, 0, 0, 0)'
})

range_bar.update_traces(textfont_size=11, textposition='outside')

range_bar.update_layout(xaxis={'categoryorder':'array', 'categoryarray':
    ['Porsche ', 'Audi ', 'Tesla ', 'Mercedes ', 'Byton ', 'Ford ', 'BMW ',
     'Nissan ', 'Skoda ', 'Hyundai ', 'Kia ', 'Opel ', 'Volkswagen ', 'Renault ', 'Smart ']})

range_bar.update_xaxes(showgrid=False)
range_bar.update_yaxes(showgrid=False)

# Create Header
st.subheader('By Brand')

# Radio Buttons
option = st.radio(label='Choose characteristic:', options=['Price', 'Efficiency', 'Range'])
st.write('<style>div.row-widget.stRadio > div{flex-direction:row;}</style>', unsafe_allow_html=True) # Make buttons horizontal

# Displays for radio button options
if option == 'Price':
    st.plotly_chart(price_bar)
elif option == 'Efficiency':
    st.plotly_chart(eff_bar)
elif option == 'Range':
    st.plotly_chart(range_bar)

# DENSITY PLOTS
# Header for body style
st.subheader('By Body Style')

# Drop down menu
option_2 = st.selectbox('Choose characteristic:', ('Price', 'Efficiency', 'Range'))

x_value_dens = (option_2=='Price' and 'price_euro') or (option_2=='Efficiency' and 'efficiency_whkm') or (option_2=='Range' and 'range_km')
x_label_dens = (option_2=='Price' and 'Price (Euros)') or (option_2=='Efficiency' and 'Efficiency (WhKm)') or (option_2=='Range' and 'Range (Km)')

# Create histograms by drop down selection
suv_hatch_sed = ev[(ev['body_style']=='SUV') | (ev['body_style']=='Hatchback') | (ev['body_style']=='Sedan')]

suv = suv_hatch_sed[suv_hatch_sed['body_style']=='SUV'][x_value_dens]
hatch = suv_hatch_sed[suv_hatch_sed['body_style']=='Hatchback'][x_value_dens]
sedan = suv_hatch_sed[suv_hatch_sed['body_style']=='Sedan'][x_value_dens]

hist_data = [sedan, suv, hatch]
group_labels = ['Sedan', 'SUV', 'Hatchback']

density = ff.create_distplot(hist_data, group_labels, show_hist=False, show_rug=False,
                                colors=[px.colors.qualitative.Plotly[0],
                                        px.colors.qualitative.Plotly[7],
                                        px.colors.qualitative.Plotly[9]])

density.update_layout({
    'plot_bgcolor':'rgba(0, 0, 0, 0)',
    'paper_bgcolor':'rgba(0, 0, 0, 0)'
})

density.update_layout(title=f'{option_2} by Body Style', xaxis_title=x_label_dens, yaxis_title='Percent of Body Type')
density.update_xaxes(showgrid=False)
density.update_yaxes(showgrid=False)

st.plotly_chart(density)

# CREATE SCATTER PLOTS
# Header for scatter plot
st.subheader('By Price')

# Drop down nenu for scatter plot
y_axis = st.selectbox('Y Axis:', ('Efficiency', 'Range'))

# Create scatter plot from drop down selection
y_value_scat = (y_axis=='Efficiency' and 'efficiency_whkm') or (y_axis=='Range' and 'range_km')

y_label_scat = (y_axis=='Price' and 'Price (Euros)') or (y_axis=='Efficiency' and 'Efficiency (WhKm)') or (y_axis=='Range' and 'Range (Km)')

price_efficiency = px.scatter(data_frame=ev, title=f'{y_axis} vs Price', x='price_euro', y=y_value_scat,
           labels={'price_euros':'Price (Euros)', y_value_scat:y_label_scat},
           color='brand', width=900, height=500, hover_data=['model'])

price_efficiency.update_layout({
    'plot_bgcolor':'rgba(0, 0, 0, 0)',
    'paper_bgcolor':'rgba(0, 0, 0, 0)'
})

price_efficiency.update_xaxes(showgrid=False)
price_efficiency.update_yaxes(showgrid=False)

# Display scatter plot on website
st.plotly_chart(price_efficiency)

st.markdown('Graphics on this site were created from data on the 103 top electric vehicles on the market today.')


import os
from pyglet.gl import *

from .player import Player
from .platform import Platform

class mainMenu():
	def __init__(self, main_window):
		self.main_window = main_window

		self.main_batch = pyglet.graphics.Batch()
		depressed = pyglet.image.load(f'{os.getcwd()}/resources/play_game_button.png')
		pressed = pyglet.image.load(f'{os.getcwd()}/resources/play_game_button.png')

		self.frame = pyglet.gui.Frame(self.main_window, order=4)
		self.play_button = pyglet.gui.ToggleButton(100, 400, pressed=pressed, depressed=depressed, batch=self.main_batch)
		self.play_button.set_handler('on_toggle', self.play_game)
		self.frame.add_widget(self.play_button)

	def play_game(self, value):
		self.main_window.view = GameView(self.main_window)

	def render(self):
		glClearColor(25/255, 94/255, 131/255, 0/255)
		self.main_batch.draw()

class GameView():
	def __init__(self, main_window):
		self.main_window = main_window

		self.platform = Platform(self)
		self.player = Player(self)

	def render(self):
		glClearColor(94/255, 25/255, 131/255, 0/255)
		self.platform.render()
		self.player.render()


import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from matplotlib.figure import Figure
from matplotlib.backends.backend_gtk3cairo import FigureCanvasGTK3Cairo as FigureGTKCanvas
from matplotlib.backends.backend_gtk3 import NavigationToolbar2GTK3 as NavigationToolbar
from matplotlib.patches import Rectangle
import matplotlib.image as image
import numpy as np

class MatPlotLibGTKWindow(Gtk.Window):
    """An abstract base class for a matplotlib drawing widget.

    Extend Gtk.Window, and have a matplotlib.Figure attribute as an handle
    to the matplotlib API. This window have a Gtk.DrawingArea on wich
    a gtk3cairo Matplotlib backend make the plot.

    """

    def __init__(self, title="", application=None):
        Gtk.Window.__init__(self, application=application)

        # matplotlib figure and canvas
        self.figure = Figure(dpi=36)
        self.gtk_drawing_area = FigureGTKCanvas(self.figure)

        # container and layout
        box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
        self.add(box)

        self.sw = Gtk.ScrolledWindow()
        self.sw.add_with_viewport(self.gtk_drawing_area)
        box.pack_start(self.sw, True, True, 0)

        toolbar = NavigationToolbar(self.gtk_drawing_area, self)
        box.pack_start(toolbar, False, True, 0)

        # window config
        self.set_title(title)
        self.set_default_size(600, 600)
        self.set_position(Gtk.WindowPosition.CENTER)

    def prepareFigure(self):
        raise NotImplementedError

    def draw(self):
        pass
        #self.figure.show()

class CellViewer(MatPlotLibGTKWindow):
    """A Gtk Window to view an halftone cell."""

    def __init__(self, cell, application=None):
        if cell is None :
            raise AttributeError("cell should not be None")
        MatPlotLibGTKWindow.__init__(self, title="Cell view", application=application)
        self.cell = cell

        self.prepareFigure()
        self.plotWhiteningOrder()
        #self.draw()
        self.show_all()

    def prepareFigure(self):
        """Draw axes, ticks, and pixels shape of the cell"""
        # axes
        f_axes = self.figure.gca(xlim=(-1.5,1.5), ylim=(-1.5,1.5))
        f_axes.spines['bottom'].set_position(('data',-1.5))
        f_axes.spines['left'].set_position(('data',-1.5))
        f_axes.spines['left'].set_color('black')

        # x-ticks
        cn = self.cell.coordinates
        xt = [p.x for p in cn[0:self.cell.width]]
        #f_axes.set_xticks(xt)

        # y-ticks
        yt = []
        for i in range(0, len(cn), self.cell.width):
            yt.append(cn[i].y)
        #f_axes.set_yticks(yt)

        f_axes.grid(True)

        # draw pixel limit
#        ix = (xt[1] - xt[0]) / 2
#        iy = (yt[1] - yt[0]) / 2
#        xt.append(xt[-1:][0] + (2 * ix))
#        f_axes.vlines([x - ix for x in xt], -1 - iy, 1 + iy)
#        yt.append(yt[-1:][0] + (2 * iy))
#        f_axes.hlines([y - iy for y in yt], -1 - ix, 1 + ix)

    def plotWhiteningOrder(self):
        arr = np.zeros((self.cell.width, self.cell.height), dtype=np.uint8)
        for p in self.cell.whiteningOrder:
            arr[p.x][p.y] = 1 - (p.w / 255)
        self.figure.figimage(arr)

#        f_axes = self.figure.gca()
#        cwo = self.cell.whiteningOrder
#        cn = self.cell.coordinates
#
#        dcw = cn[1].x - cn[0].x
#        dch = cn[self.cell.width].y - cn[0].y
#        ix = dcw / 2
#        iy = dch / 2
#
#        for d in cwo:
#            np = cn[d.x + self.cell.width * d.y]
#            col = str(1 - (d.w / 255))
#            r = Rectangle((np.x - ix, np.y - iy), dcw, dch, color=col)
#            f_axes.add_patch(r)

    def plotCellData(self):
        pass


import torch.nn as nn
import torch.nn.functional as F
import torch

class DEQCpResidualBlockAdjacentBlock(nn.Module):
    def __init__(self, dilations, in_channels, out_channels, kernel_size, block_id):
        super(DEQCpResidualBlockAdjacentBlock, self).__init__()
        self.kernel_size = kernel_size
        self.dilation = dilations
        self.block_id = block_id

        self.conv1 = nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=(1, kernel_size),
                                   padding=0, dilation=dilations[0], bias=True)
        self.rel1 = nn.ReLU()
        self.conv2 = nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=(1, kernel_size),
                                   padding=0, dilation=2*dilations[0], bias=True)
        self.rel2 = nn.ReLU()
        self.init_weights()

    def init_weights(self):
        bound = 0.01
        self.conv1.weight.data.normal_(0, bound)
        self.conv1.bias.data.normal_(0, bound)
        self.conv2.weight.data.normal_(0, bound)
        self.conv2.bias.data.normal_(0, bound)

    def copy(self, func):
        self.conv1.weight.data = func.conv1.weight.data.clone()
        self.conv1.bias.data = func.conv1.bias.data.clone()
        self.conv2.weight.data = func.conv2.weight.data.clone()
        self.conv2.bias.data = func.conv2.bias.data.clone()

    def forward_conv(self, conv, x, dilation):
        padding = [(self.kernel_size - 1) * dilation, 0, 0, 0, 0, 0]
        padded = torch.nn.functional.pad(x, padding)
        input_expanded = padded.unsqueeze(dim=2)
        conv.dilation = dilation
        y = conv(input_expanded)
        y = y.squeeze(dim=2)
        return y

    # dilations for deq is a tupple eg(1,4) so we have 4 convolutions or 2 residual blocks -> 1,2,4,8
    def forward(self, z1ss, uss, *kwargs):
        # pytorch expect BCHW input that is why we permute and add height in dim 2

        y1 = self.forward_conv(self.conv1, z1ss, self.dilation[0])
        y1 = self.layer_norm(y1)
        y1 = self.rel1(y1)

        y1 = self.forward_conv(self.conv2, y1, 2 * self.dilation[0])
        y1 = self.layer_norm(y1)
        y1 = self.rel2(y1)

        y1 = uss + y1

        y2 = y1
        y2 = self.forward_conv(self.conv1, y2, self.dilation[1])
        y2 = self.layer_norm(y2)
        y2 = self.rel1(y2)

        y2 = self.forward_conv(self.conv2, y2, 2 * self.dilation[1])
        y2 = self.layer_norm(y2)
        y2 = self.rel2(y2)

        return uss + y2

    def layer_norm(self, x, epsilon=1e-8):
        shape = x.shape
        x = x.permute(0, 2, 1)
        mean = x.mean(dim=len(shape) - 1, keepdim=True)
        variance = x.var(dim=len(shape) - 1, keepdim=True)

        x = (x - mean) / torch.sqrt(variance + epsilon)
        y = x.permute(0, 2, 1)
        return y


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# test-train-shakeel/format_data.py
#
# format time series data for training
#

import os
import re
import pandas as pd
import datetime as dt
import pandas as pd
import numpy as np
import scipy
#import matlab.engine

from sklearn.preprocessing import minmax_scale

#linux内のmatlabを起動する
#mat = matlab.engine.start_matlab()
#mat.cd(r'matlab', nargout=0)

class preprocess_for_Siamese_Net():
    """
    siamese network用のデータ前処理クラス
    """

    def __init__(self):
        super()

    @classmethod
    def convert_csv_to_mergedcsv(self, climo_csv_path:str, gauss_csv_path:str) -> pd:
        """
        2つのセンサーの時系列csvデータを同時刻部のみを切り出しmergeする

        arg:
            - climo_csv_path:climomasterデータのcsvパス
            - gauss_csv_path:milli gauss meterデータのcsvパス
        return:
            - merged_df:二つのセンサーの時系列csvデータを同時刻部のみを切り出しmergeしたデータフレーム
        """

        def climo_momenttime(x_linespace:int,beginning_time:str,sampling_interval:int)-> dt:
            """
            climomaster.dfの各データ行の日付型を生成

            args:
                - x_linespace: 等間隔の数列
                - beginning_time: 開始時刻
                - sampleing_interval: サンプリング間隔

            return:
                - moment_time: 日付型
            """

            #開始時刻の文字列からデータの日付型を取得
            beginning_time = beginning_time.replace('#','')
            beginning_time_list = re.split('[/: ]',beginning_time)
            beginning_year = int(beginning_time_list[2])
            beginning_month = int(beginning_time_list[1])
            beginning_day = int(beginning_time_list[0])
            beginning_hour = int(beginning_time_list[3])
            beginning_minite = int(beginning_time_list[4])
            beginning_second = int(beginning_time_list[5])
            beginning_time = dt.datetime(beginning_year,beginning_month,beginning_day,beginning_hour,beginning_minite,beginning_second)

            #サンプリング間隔
            elapsed_time = x_linespace * sampling_interval
            moment_time = beginning_time + dt.timedelta(seconds = elapsed_time)
            return moment_time

        def gauss_momenttime(time:str)-> dt:
            """
            milli gauss meter.dfの各データ行の日付型を生成

            arg:
                -time:時刻

            return:
                - moment_time: 日付型
            """
            moment_time_list = re.split('[/: ]',time)
            year = int(moment_time_list[0])
            month = int(moment_time_list[1])
            day = int(moment_time_list[2])
            hour = int(moment_time_list[3])
            minute = int(moment_time_list[4])
            second = int(moment_time_list[5])
            moment_time = dt.datetime(year,month,day,hour,minute,second)
            return moment_time

        #パスからデータフレームを取得
        climo_csv_df = pd.read_csv(climo_csv_path,names = ["No.","V(m/s)","T(C)","H(%RH)","A(hPa)","Atom.F"])

        #climomaster.csvの先頭部のinfo情報のみをclimo_info_dictに抽出
        climo_info_df = climo_csv_df[:13]
        climo_info_df =  climo_info_df.rename(columns={'No.': 'info','V(m/s)':'data'})
        climo_info_df = climo_info_df.drop(["T(C)","H(%RH)","A(hPa)","Atom.F"],axis=1)
        climo_info_dict = dict(zip(climo_info_df['info'], climo_info_df['data']))

        #開始時間とサンプリング間隔を抽出
        beginning_time = climo_info_dict['[Measurement beginning time]']
        sampling_interval = int(climo_info_dict['[S-TIME(S) -- Sampling interval(s)]'])

        #climomaster.csvのデータ部の情報を加工し、climo_dfに抽出
        climo_df = climo_csv_df[19:]
        climo_df =  climo_df.rename(columns={'No.': 'Time'})
        climo_df = climo_df.drop(["A(hPa)","Atom.F"],axis=1)
        climo_df = climo_df.astype({'Time':int, 'V(m/s)':float, "T(C)":float ,"H(%RH)":float})

        #一行ずつ時間を更新
        for i in range(len(climo_df)):
            number = sampling_interval * (i+1)
            climo_df.loc[i+19,"Time"] = climo_momenttime(number,beginning_time,sampling_interval)
        #行の部分に時間をセットする
        climo_df = climo_df.set_index('Time')

        #milli gauss meterのデータフレームを取得
        gauss_df = pd.read_csv(gauss_csv_path, encoding = 'shift_jis')

        #gauss.csvのデータ部の情報を加工し、gauss_dfを取得
        gauss_df['Time'] = gauss_df['年月日'].str.cat(gauss_df['時刻'], sep = ' ')
        gauss_df = gauss_df.drop(columns = {'年月日','時刻'})
        gauss_df = gauss_df.rename(columns = {'データ01(mG)':'φ(mG)'})
        #全ての"Time"行にgauss_momenttime関数を適用
        gauss_df['Time'] = gauss_df['Time'].map(gauss_momenttime)
        #行の部分に時間をセットする
        gauss_df = gauss_df.set_index('Time')

        #両方のcsvをmergeし、時刻の共通部のみを切り出す
        merged_df = pd.merge(climo_df,gauss_df,left_index=True,right_index = True)
        merged_df = merged_df.dropna()
        merged_df.plot(subplots = True)

        return merged_df

    @classmethod
    def normalization(data_array:np)->np:
        """
        時系列データ(numpy配列)を配列内で最大値最小値を取り、正規化する

        args:
            - data_array(np): データのnumpy配列
        return:
            - rms_data(np):正規化処理を加えた後のデータのnumpy配列
        """
        normalizated_data = minmax_scale(data_array)
        return normalizated_data

    @classmethod
    def rms(data_array:np)->np:
        """
        時系列データ(numpy配列)に実効値処理を加える

        args:
            - data_array(np): データのnumpy配列
        return:
            - rms_data(np):実効値処理を加えた後のデータのnumpy配列
        """
        square_data_array = data_array**2
        abs_square_data_array = np.abs(square_data_array)
        sum_abs_square_data = np.sum(abs_square_data_array)
        sum_mean_square_data = sum_abs_square_data / len(data_array)
        rms_data = np.sqrt(data_array / sum_mean_square_data)
        return rms_data

    @classmethod
    def convert_mergedcsv_to_matfile(self,csv_path:str,
                                        is_wind_vel_converted: bool = True,
                                        is_temp_converted: bool = False,
                                        is_humid_converted: bool = False,
                                        is_gauss_converted: bool = True):
        """
        メルスペクトログラム変換する関数
        """

        #データの取り込み
        merged_data_df = pd.read_csv(csv_path,names = ["Time","V(m/s)","T(C)","H(%RH)","φ(mG)"],skiprows=1)

        #風速をmatファイルに変換
        if is_wind_vel_converted:
            wind_vel_df = merged_data_df['V(m/s)']
            wind_vel_array = np.array(wind_vel_df.values[1:], dtype = 'float')
            mel_spect_wind_vel_data = mat.mel_spectrogram_bad(matlab.double(wind_vel_array),matlab.double(2900))
            scipy.io.savemat('data/mat/wind_vel/wind'+ csv_path.replace('.csv','') + '.mat', {'wind_feat':mel_spect_wind_vel_data})
            print("convert wind velocity data to mat file.")

        #温度をmatファイルに変換
        if is_temp_converted:
            temp_df = merged_data_df['T(C)']
            temp_array = np.array(temp_df.values[1:], dtype = 'float')
            mel_spect_temp_data = mat.mel_spectrogram_bad(matlab.double(temp_array),matlab.double(2900))
            scipy.io.savemat('data/mat/temp/temp'+ csv_path.replace('.csv','') + '.mat', {'temp_feat':mel_spect_temp_data})
            print("convert temperature data to mat file.")

        #湿度をmatファイルに変換
        if is_humid_converted:
            humid_df = merged_data_df['H(%RH)']
            humid_array = np.array(humid_df.values[1:], dtype = 'float')
            mel_spect_humid_data = mat.mel_spectrogram_bad(matlab.double(humid_array),matlab.double(2900))
            scipy.io.savemat('data/mat/humid/humid'+ csv_path.replace('.csv','') + '.mat', {'humid_feat':mel_spect_humid_data})
            print("convert humidity data to mat file.")

        #磁束密度をmatファイルに変換
        if is_gauss_converted:
            gauss_df = merged_data_df['H(%RH)']
            gauss_array = np.array(gauss_df.values[1:], dtype = 'float')
            mel_spect_gauss_data = mat.mel_spectrogram_bad(matlab.double(gauss_array),matlab.double(2900))
            scipy.io.savemat('data/mat/gauss/gauss'+ csv_path.replace('.csv','') + '.mat', {'gauss_feat':mel_spect_gauss_data})
            print("convert milli Gauss data to mat file.")

    def generate_npy_for_siamese(mat1_dir,mat1_feat,mat2_dir,mat2_feat):
        """
        siamese networks用に2対のデータセットとラベルのnpyファイルを生成
        args:
            - mat1_dir(str): matファイルが格納されているディレクトリ
            - mat1_feat: ['wind_feat','temp_feat','humid_feat','gauss_feat']のどれか、mat1_dirの種類による
            - mat2_dir(str): matファイルが格納されているディレクトリ(もう一組)
            - mat2_feat: ['wind_feat','temp_feat','humid_feat','gauss_feat']のどれか、mat1_dirの種類による
        """

        def convert_mat_to_list(mat_dir:str,feature_type:str):
            """
            ディレクトリ内のmatファイルの組み合わせを全てlistに変換する関数
            args:
                - mat_dir(str): matファイルが格納されているディレクトリ名
                - feature_type(str): ['wind_feat','temp_feat','humid_feat','gauss_feat']のどれか、mat_dirの種類による
            """

            mat_files = os.listdir(mat_dir)
            mat2_a = []
            mat2_b = []
            mat2_y = []
            for i in range(len(mat_files)):
                    a_path = mat_files[i]
                    temp1 = scipy.io.loadmat(mat_dir + a_path)
                    feat1 = np.array(temp1[feature_type]).reshape(999,50)
                    for j in range(len(mat_files)):
                        b_path = mat_files[j]
                        temp2 = scipy.io.loadmat(mat_dir + b_path)
                        feat2 = np.array(temp2[feature_type]).reshape(999,50)
                        mat2_a.append(feat1)
                        mat2_b.append(feat2)
                        if(a_path[0]==b_path[0]):
                            mat2_y.append(ord(a_path[0])-48)
                        else:
                            mat2_y.append(-1)

            return mat2_a,mat2_b,mat2_y

        mat1_a,mat1_b,mat1_y = convert_mat_to_list(mat1_dir,mat1_feat)
        mat2_a,mat2_b,mat2_y = convert_mat_to_list(mat2_dir,mat2_feat)
        label=[]

        #ラベリング
        for i in range(len(mat2_a)):
            if(mat1_y[i] == -1 or mat2_y[i] == -1):
                label.append(0)
            elif(mat1_y[i] == mat2_y[i]):
                label.append(1)
            else:
                label.append(0)

        #npyファイルに変換s
        datadir = "data/train-npy/"
        np.save(datadir + mat1_feat + '_a_set', mat1_a)
        np.save(datadir + mat1_feat + '_b_set', mat1_b)
        np.save(datadir + mat2_feat + '_a_set', mat2_a)
        np.save(datadir + mat2_feat + '_b_set', mat2_b)
        np.save(datadir + 'labels', label)
        return


import json
import unittest
from graph import Graph

class GraphTests(unittest.TestCase):

	def setUp(self):
		# create graph from JSON file
		self.g = Graph('model/data/data.json')
		# read JSON file into Python dictionary
		with open('model/data/data.json') as f:
			self.json_data = json.load(f)

	# test that Graph constructor correctly built graph from JSON file
	def test_json_to_graph(self):
		# each movie and actor in JSON data should have a node in graph
		self.assertEqual(len(self.json_data[1]), len(self.g.movie_vertices))
		self.assertEqual(len(self.json_data[0]), len(self.g.actor_vertices))
		# test that edge weight are properly set to (age * gross) and edges all built
		bruce_willis = None
		blind_date = None
		for actor in self.g.actor_vertices:
			if actor.name == 'Bruce Willis':
				bruce_willis = actor
		for movie in self.g.movie_vertices:
			if 'Blind Date' in movie.name:
				blind_date = movie
		# christian bale has 10 movies in test set
		self.assertEqual(len(bruce_willis.neighbors), 91)
		# both edge directions should have correct weight
		self.assertIn(bruce_willis.age * blind_date.gross, bruce_willis.neighbors.values())
		self.assertIn(bruce_willis.age * blind_date.gross, blind_date.neighbors.values())

	# test graph is properly converted back to JSON
	def test_graph_to_json(self):
		# convert graph to json
		d = self.g.to_json()
		# check that dictionaries are the same (order doesn't matter)
		self.assertItemsEqual(self.json_data, d)

	# test small number oldest X
	def test_oldest_X_small(self):
		oldest = self.g.get_oldest_X_actors(2)
		# oldest person(s) in test set is 94
		self.assertEqual(oldest[1][1], 94)

	# test large number oldest X
	def test_oldest_X_large(self):
		# choose number larger than test set
		oldest = self.g.get_oldest_X_actors(100000)
		# oldest person(s) in test set is 94
		self.assertEqual(oldest[-1][1], 94)
		# younger person in test set is -1?
		self.assertEqual(oldest[0][1], -1)

	# test a year with small number of movies
	def test_movies_from_year_small(self):
		movies = self.g.get_movies_from_year(2017)
		# only one movie from 2017 in test set
		self.assertEqual(len(movies), 1)
		self.assertEqual(movies[0], 'The Bye Bye Man')

	# test a year that hasn't happened
	def test_movies_from_year_invalid(self):
		# no movies from 2018 exist yet
		movies = self.g.get_movies_from_year(2018)
		self.assertEqual(len(movies), 0)

	# test a year with large number of movies
	def test_movies_from_year_large(self):
		movies = self.g.get_movies_from_year(2015)
		# 28 movies from 2015 in test set
		self.assertEqual(len(movies), 3)
		# one of these movies should be Vice
		self.assertIn('Vice', movies)

	# test a year with small number of movies
	def test_actors_from_year_small(self):
		actors = self.g.get_actors_from_year(2017)
		# 1 actor in test set from John Wick: Chapter 2 (only 2017 movie we have)
		self.assertEqual(len(actors), 1)
		# Keanu Reeves should be the actor
		self.assertIn('Faye Dunaway', actors.pop())

	# look for actors in invalid year
	def test_actors_from_year_invalid(self):
		# no movies from 2018 exist yet
		actors = self.g.get_actors_from_year(2018)
		self.assertEqual(len(actors), 0)

	# look for actors in year with a lot of movies
	def test_actors_from_year_large(self):
		actor_names = self.g.get_actors_from_year(2015)
		# 10 actors in test set from 2015 movies
		self.assertEqual(len(actor_names), 10)
		# two of these actors should be Danny McBride and Bruce Willis
		self.assertIn('Bruce Willis', actor_names)
		self.assertIn('Danny McBride', actor_names)

	# check top grossing with small arg
	def test_top_grossing_actors_small(self):
		top_actors = self.g.get_top_X_grossing_actors(1)
		# top grossing actor in test set is Morgan Freeman with ~3.2 billion gross
		self.assertEqual('Bruce Willis', top_actors[0][0])
		self.assertEqual(562709189, top_actors[0][1])

	# check top grossing with invalid arg
	def test_top_grossing_actors_invalid_param(self):
		# invalid param should not cause an error
		top_actors = self.g.get_top_X_grossing_actors(-1)
		self.assertEqual(len(top_actors), 0)

	# test top grossing with large arg
	def test_top_grossing_actors_large(self):
		# choose number larger than set
		top_actors = self.g.get_top_X_grossing_actors(100000)
		# top grossing actor in test set is Bruce Willis
		self.assertEqual('Bruce Willis', top_actors[308][0])
		self.assertEqual(562709189, top_actors[308][1])
		# second top grossing actor is Faye Dunaway
		self.assertEqual('Faye Dunaway', top_actors[307][0])
		self.assertEqual(515893034, top_actors[307][1])
		# several bottom grossing actors, all with 0
		self.assertEqual(0, top_actors[0][1])

	# test DFS traversal
	def test_dfs_traversal(self):
		self.g.dfs_traversal()
		# all nodes should be visited now
		for actor_node in self.g.actor_vertices:
			self.assertTrue(actor_node.visited)
		for movie_node in self.g.movie_vertices:
			self.assertTrue(movie_node.visited)

	# Test BFS traversal w/ a depth 1
	def test_bfs_d1(self):
		for actor in self.g.actor_vertices:
			if actor.name == 'Bruce Willis':
				bruce_willis = actor
			if actor.name == 'Kim Basinger':
				kim_bas = actor
		i = self.g.bfs(bruce_willis, kim_bas.name)
		# shortest path should have length 2
		self.assertEqual(i, 1)

	# Test BFS traversal w/ a larger depth
	def test_bfs_d2(self):
		for actor in self.g.actor_vertices:
			if actor.name == 'Kirstie Alley':
				bruce_willis = actor
			if actor.name == 'John Pankow':
				other = actor
		i = self.g.bfs(bruce_willis, other.name)
		# shortest path should have length 2
		self.assertEqual(i, 2)
		# lengths should be reciprocal
		j = self.g.bfs(other, bruce_willis.name)
		self.assertEqual(j,2)

	# Test nodes not reachable
	def test_bfs_unreachabel(self):
		for actor in self.g.actor_vertices:
			if actor.name == 'Bruce Willis':
				bruce_willis = actor
		i = self.g.bfs(bruce_willis, 'Not an Actor')
		# -1 should be returned
		self.assertEqual(i, -1)

	# Test BFS traversal w/ a larger depth
	def test_bfs_d3(self):
		for actor in self.g.actor_vertices:
			if actor.name == 'Danny Aiello':
				first = actor
			if actor.name == 'Kirstie Alley':
				other = actor
		i = self.g.bfs(first, other.name)
		# shortest path should have length 2
		self.assertEqual(i, 2)
		# lengths should be reciprocal
		j = self.g.bfs(other, first.name)
		self.assertEqual(j, 2)

	# Test top 5 hub actors
	def test_hub_actors(self):
		# select top 5 hub actors
		hub_actors = self.g.get_hub_actors(5)
		# top actor should be Bruce willis
		self.assertEqual(hub_actors[-1][0], 'Bruce Willis')
		self.assertEqual(hub_actors[-1][1], 305)
		# second to top is Jack Warden
		self.assertEqual(hub_actors[-2][0], 'Jack Warden')
		self.assertEqual(hub_actors[-2][1], 39)

	# Test getting hub actors with larger parameter
	def test_hub_actors_large(self):
		# select top 5 hub actors
		hub_actors = self.g.get_hub_actors(500)
		# top actor should still be Bruce willis
		self.assertEqual(hub_actors[-1][0], 'Bruce Willis')
		self.assertEqual(hub_actors[-1][1], 305)
		# Multiple bottom individuals with no connections
		self.assertEqual(hub_actors[0][1], 0)

	# Test hub actors with invalid param
	def test_hub_actors_invalid(self):
		hub_actors = self.g.get_hub_actors(-15)
		self.assertEqual(hub_actors, [])

	# Test gross for age group
	def test_get_gross_for_age_group(self):
		gross = self.g.get_gross_for_age_group(60, 69)
		self.assertEqual(gross, 1005773688)
		gross2 = self.g.get_gross_for_age_group(20, 29)
		self.assertEqual(gross2, 21767523)

	# Test gross for invalid age group
	def test_get_gross_for_age_group_invalid(self):
		# start age larger than end
		gross = self.g.get_gross_for_age_group(69, 60)
		self.assertEqual(gross, 0)
		# no actors in age group
		gross = self.g.get_gross_for_age_group(100, 150)
		self.assertEqual(gross, 0)

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