# Room definition
from lib import reader

class Choices():
    PATH = './txt/rooms.txt'

    def __init__(self, room_id):

        self.room_id = room_id
        self.choices = [
            {
             'id': 'A',
             'descriptor': self.reader_lookup('A'),
             'description': self.reader_lookup('X'),
             'exhausted': 'false',
             },
            {
             'id': 'B',
             'descriptor': self.reader_lookup('B'),
             'description': self.reader_lookup('Y'),
             'exhausted': 'false',
            },
            {
             'id': 'C',
             'descriptor': self.reader_lookup('C'),
             'description': self.reader_lookup('Z'),
             'exhausted': 'false',

            }
        ]

    def reader_lookup(self, id):
        return reader.parse(self.PATH, ''.join((self.room_id, id)))

class Room():
    PATH = './txt/rooms.txt'

    def __init__(self, room_id):
        self.name = reader.parse(self.PATH, ''.join((room_id, 'N')))
        self.description = reader.parse(self.PATH, ''.join((room_id, 'D')))
        self.visited = 'false'
        self.room_id = room_id
        self.connectors = []  # Connectors(room_id)
        self.choices = Choices(room_id).choices

    def init_room(self):
        print(self.name)
        print(self.description)

        if len(self.choices) > 0:
            self.get_choices()

    def set_visited(self):
        self.visited = 'true'

    def exhaust_choice(self, id):
        for choice in self.choices:
            if choice['id'] == id:
                choice['exhausted'] = 'true'

    def get_choices(self):
        gen = (choice for choice in self.choices if choice['exhausted'] != 'true')
        selection = ''

        for choice in gen:
            print(choice['id'] + '    ' + choice['descriptor'])
# needs to exclude exhausted items of gen instead static list
        while selection.upper() not in ['A', 'B', 'C']:
            selection = input('Select: ')
            self.exhaust_choice(selection)


#!/usr/bin/python

# read input file
# calculate size of matrix from input
# fill matrix with added weigths
# store where the maximum weight came from (=path)
# store maximum weight
# if both equally good prefer going south
# output: max weight
# option -t : output max weight \n path (e.g ESES E...East, S...South)

import sys
import argparse
import re

parser = argparse.ArgumentParser()
parser.add_argument("filename")
parser.add_argument("-t", action="store_true")
args = parser.parse_args()

## list for the read in data
input_data = []
with open(args.filename, "r") as infile:
    for line in infile:
        if line.startswith("#"):
            continue
        elif not line.strip():
            continue
        else:
            line = line.strip()
            line = re.sub("[^\w]", " ", line).split()
            input_data.append(list(line))

## matrix for weights:
rows = int(input_data[0][0])
columns = int(input_data[0][1])
input_data.pop(0) # remove the dimensions from list
weight_matrix = [[0 for x in range(columns)] for y in range(rows)]

## make individual lists for east-west and north-south
east_west_streets = []
north_south_streets = []
for i in range(0, rows):
    east_west_streets.append(input_data[i])
for i in range(rows, len(input_data)):
    north_south_streets.append(input_data[i])

## make ints out of all weigths:
for i in range(0, len(east_west_streets)):
    east_west_streets[i] = [int(x) for x in east_west_streets[i]]
for i in range(0, len(north_south_streets)):
    north_south_streets[i] = [int(x) for x in north_south_streets[i]]

max_weight = 0
## fill weight-matrix:
for i in range(0, rows):
    for j in range(0, columns):
        ## first line and column
        if i == 0 and j > 0:
            weight_matrix[i][j] = weight_matrix[i][j-1] + east_west_streets[i][j-1]
        elif j == 0 and i > 0:
            weight_matrix[i][j] = weight_matrix[i-1][j] + north_south_streets[i-1][j]
        elif i > 0 and j > 0:
            weight_matrix[i][j] = max(weight_matrix[i-1][j] + north_south_streets[i-1][j], weight_matrix[i][j-1] + east_west_streets[i][j-1])
            if i == rows-1 and j == columns-1:
                max_weight = weight_matrix[i][j]
#print(weight_matrix)
print(max_weight)

## backtracking
if args.t:
    path = ""
    i = rows-1
    j = columns-1
    while i > 0 or j > 0:
        if i > 0 and weight_matrix[i][j] == weight_matrix[i-1][j] + north_south_streets[i-1][j]:
            path = "S" + path
            i -= 1
        elif j > 0 and weight_matrix[i][j] == weight_matrix[i][j-1] + east_west_streets[i][j-1]:
            path = "E" + path
            j -= 1
    print(path)


#!/usr/bin/python3

from getpass import getpass
from hexbytes import HexBytes
import os
from pathlib import Path
import json

from eth_hash.auto import keccak
import eth_keys
from eth_utils import to_checksum_address

from brownie.cli.utils import color
from brownie.exceptions import VirtualMachineError
from brownie.network.transaction import TransactionReceipt
from brownie.network.web3 import web3
from brownie.types.convert import wei

import brownie._registry as _registry
import brownie._config
CONFIG = brownie._config.CONFIG

class Accounts:

    '''List-like container that holds all of the available Account instances.'''

    def __init__(self):
        self._accounts = []
        # prevent private keys from being stored in read history
        self.add.__dict__['_private'] = True
        _registry.add(self)

    def _notify_reset(self):
        self._accounts.clear()
        try:
            self._accounts = [Account(i) for i in web3.eth.accounts]
        except Exception:
            pass

    def _notify_revert(self):
        for i in self._accounts:
            i.nonce = web3.eth.getTransactionCount(str(i))

    def __contains__(self, address):
        try:
            address = to_checksum_address(address)
            return address in self._accounts
        except ValueError:
            return False

    def _console_repr(self):
        return str(self._accounts)

    def __iter__(self):
        return iter(self._accounts)

    def __getitem__(self, key):
        return self._accounts[key]

    def __delitem__(self, key):
        del self._accounts[key]

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

    def add(self, priv_key=None):
        '''Creates a new ``LocalAccount`` instance and appends it to the container.

        Args:
            priv_key: Private key of the account. If none is given, one is
                      randomly generated.

        Returns:
            Account instance.
        '''
        if not priv_key:
            priv_key = "0x"+keccak(os.urandom(8192)).hex()
        w3account = web3.eth.account.privateKeyToAccount(priv_key)
        if w3account.address in self._accounts:
            return self.at(w3account.address)
        account = LocalAccount(w3account.address, w3account, priv_key)
        self._accounts.append(account)
        return account

    def load(self, identifier):
        json_file = Path(CONFIG['folders']['brownie']).joinpath("data/accounts/{}.json".format(identifier))
        if not json_file.exists():
            raise FileNotFoundError("Account with this identifier does not exist")
        priv_key = web3.eth.account.decrypt(
            json.load(json_file.open()),
            getpass("Enter the password for this account: ")
        )
        return self.add(priv_key)

    def at(self, address):
        '''Retrieves an Account instance from the address string. Raises
        ValueError if the account cannot be found.

        Args:
            address: string of the account address.

        Returns:
            Account instance.
        '''
        try:
            address = to_checksum_address(address)
        except ValueError:
            raise ValueError("{} is not a valid address".format(address))
        try:
            return next(i for i in self._accounts if i == address)
        except StopIteration:
            raise ValueError("No account exists for {}".format(address))

    def remove(self, address):
        '''Removes an account instance from the container.

        Args:
            address: Account instance or address string of account to remove.'''
        self._accounts.remove(address)

    def clear(self):
        '''Empties the container.'''
        self._accounts.clear()

class _AccountBase:

    '''Base class for Account and LocalAccount'''

    def __init__(self, addr):
        self.address = addr
        self.nonce = web3.eth.getTransactionCount(self.address)

    def __hash__(self):
        return hash(self.address)

    def __repr__(self):
        return "'{0[string]}{1}{0}'".format(color, self.address)

    def __str__(self):
        return self.address

    def __eq__(self, other):
        if type(other) is str:
            try:
                address = to_checksum_address(other)
                return address == self.address
            except ValueError:
                return False
        return super().__eq__(other)

    def balance(self):
        '''Returns the current balance at the address, in wei.'''
        return web3.eth.getBalance(self.address)

    def deploy(self, contract, *args):
        '''Deploys a contract.

        Args:
            contract: ContractContainer instance.
            *args: Constructor arguments. The last argument may optionally be
                   a dictionary of transaction values.

        Returns:
            * Contract instance if the transaction confirms
            * TransactionReceipt if the transaction is pending or reverts'''
        return contract.deploy(self, *args)

    def estimate_gas(self, to, amount, data=""):
        '''Estimates the gas cost for a transaction. Raises VirtualMachineError
        if the transaction would revert.

        Args:
            to: Account instance or address string of transaction recipient.
            amount: Amount of ether to send in wei.
            data: Transaction data hexstring.

        Returns:
            Estimated gas value in wei.'''
        return web3.eth.estimateGas({
            'from': self.address,
            'to': str(to),
            'data': HexBytes(data),
            'value': wei(amount)
        })

    def _gas_limit(self, to, amount, data=""):
        if type(CONFIG['active_network']['gas_limit']) is int:
            return CONFIG['active_network']['gas_limit']
        return self.estimate_gas(to, amount, data)

    def _gas_price(self):
        return CONFIG['active_network']['gas_price'] or web3.eth.gasPrice

class Account(_AccountBase):

    '''Class for interacting with an Ethereum account.

    Attributes:
        address: Public address of the account.
        nonce: Current nonce of the account.'''

    def _console_repr(self):
        return "<Account object '{0[string]}{1}{0}'>".format(color, self.address)

    def transfer(self, to, amount, gas_limit=None, gas_price=None, data=''):
        '''Transfers ether from this account.

        Args:
            to: Account instance or address string to transfer to.
            amount: Amount of ether to send, in wei.
            gas_limit: Gas limit of the transaction.
            gas_price: Gas price of the transaction.

        Returns:
            TransactionReceipt instance'''
        try:
            txid = web3.eth.sendTransaction({
                'from': self.address,
                'to': str(to),
                'value': wei(amount),
                'gasPrice': wei(gas_price) or self._gas_price(),
                'gas': wei(gas_limit) or self._gas_limit(to, amount, data),
                'data': HexBytes(data)
            })
        except ValueError as e:
            txid = raise_or_return_tx(e)
        self.nonce += 1
        return TransactionReceipt(txid, self)

    def _contract_tx(self, fn, args, tx, name, callback=None):
        tx['from'] = self.address
        if type(CONFIG['active_network']['gas_price']) is int:
            tx['gasPrice'] = CONFIG['active_network']['gas_price']
        if type(CONFIG['active_network']['gas_limit']) is int:
            tx['gas'] = CONFIG['active_network']['gas_limit']
        try:
            txid = fn(*args).transact(tx)
        except ValueError as e:
            txid = raise_or_return_tx(e)
        self.nonce += 1
        return TransactionReceipt(txid, self, name=name, callback=callback)

class LocalAccount(_AccountBase):

    '''Class for interacting with an Ethereum account.

    Attributes:
        address: Public address of the account.
        nonce: Current nonce of the account.
        private_key: Account private key.
        public_key: Account public key.'''

    def __init__(self, address, account, priv_key):
        self._acct = account
        self.private_key = priv_key
        self.public_key = eth_keys.keys.PrivateKey(HexBytes(priv_key)).public_key
        super().__init__(address)

    def _console_repr(self):
        return "<LocalAccount object '{0[string]}{1}{0}'>".format(color, self.address)

    def save(self, identifier, overwrite=False):
        path = Path(CONFIG['folders']['brownie']).joinpath('data/accounts')
        path.mkdir(exist_ok=True)
        json_file = path.joinpath("{}.json".format(identifier))
        if not overwrite and json_file.exists():
            raise FileExistsError("Account with this identifier already exists")
        encrypted = web3.eth.account.encrypt(
            self.private_key,
            getpass("Enter the password to encrypt this account with: ")
        )
        json.dump(encrypted, json_file.open('w'))
        print("Saved to {}".format(json_file))

    def transfer(self, to, amount, gas_limit=None, gas_price=None, data=''):
        '''Transfers ether from this account.

        Args:
            to: Account instance or address string to transfer to.
            amount: Amount of ether to send, in wei.
            gas_limit: Gas limit of the transaction.
            gas_price: Gas price of the transaction.

        Returns:
            TransactionReceipt instance'''
        try:
            signed_tx = self._acct.signTransaction({
                'from': self.address,
                'nonce': self.nonce,
                'gasPrice': wei(gas_price) or self._gas_price(),
                'gas': wei(gas_limit) or self._gas_limit(to, amount, data),
                'to': str(to),
                'value': wei(amount),
                'data': HexBytes(data)
            }).rawTransaction
            txid = web3.eth.sendRawTransaction(signed_tx)
        except ValueError as e:
            txid = raise_or_return_tx(e)
        self.nonce += 1
        return TransactionReceipt(txid, self)

    def _contract_tx(self, fn, args, tx, name, callback=None):
        try:
            tx.update({
                'from': self.address,
                'nonce': self.nonce,
                'gasPrice': self._gas_price(),
                'gas': (
                    CONFIG['active_network']['gas_limit'] or
                    fn(*args).estimateGas({'from': self.address})
                )
            })
            raw = fn(*args).buildTransaction(tx)
            txid = web3.eth.sendRawTransaction(
                self._acct.signTransaction(raw).rawTransaction
            )
        except ValueError as e:
            txid = raise_or_return_tx(e)
        self.nonce += 1
        return TransactionReceipt(txid, self, name=name, callback=callback)

def raise_or_return_tx(exc):
    data = eval(str(exc))
    try:
        return next(i for i in data['data'].keys() if i[:2] == "0x")
    except Exception:
        raise VirtualMachineError(exc)


import meshio

def convert_med_to_xdmf(
    medfilename,
    cell_file="mesh_domains.xdmf",
    facet_file="mesh_boundaries.xdmf",
    cell_type="tetra",
    facet_type="triangle",
):
    """_summary_
    Args:
        medfilename (_type_): _description_
        cell_file (str, optional): _description_. Defaults to "mesh_domains.xdmf".
        facet_file (str, optional): _description_. Defaults to "mesh_boundaries.xdmf".
        cell_type (str, optional): _description_. Defaults to "tetra".
        facet_type (str, optional): _description_. Defaults to "triangle".
    Returns:
        dict, dict: the correspondance dict, the cell types
    """
    msh = meshio.read(medfilename)

    correspondance_dict = msh.cell_tags

    cell_data_types = msh.cell_data_dict["cell_tags"].keys()

    for mesh_block in msh.cells:
        if mesh_block.type == cell_type:

            meshio.write_points_cells(
                cell_file,
                msh.points,
                [mesh_block],
                cell_data={"f": [-1 * msh.cell_data_dict["cell_tags"][cell_type]]},
            )
        elif mesh_block.type == facet_type:
            meshio.write_points_cells(
                facet_file,
                msh.points,
                [mesh_block],
                cell_data={"f": [-1 * msh.cell_data_dict["cell_tags"][facet_type]]},
            )

    return correspondance_dict, cell_data_types

if __name__ == "__main__":
    for thickness in [4, 5, 6, 7, 8, 9, 10, 14]:
        folder = "{}mm_thickness".format(thickness)
        correspondance_dict, cell_data_types = convert_med_to_xdmf(
            medfilename="{}/mesh_3D_{}mm.med".format(folder, thickness),
            cell_file="{}/mesh_cells.xdmf".format(folder),
            facet_file="{}/mesh_facets.xdmf".format(folder),
            cell_type="tetra",
            facet_type="triangle",
        )

    print(correspondance_dict)


import random
import math
import networkx as nx
import matplotlib.pyplot as plt
from DiscreteAnt import DiscreteAnt

city_count = 10 #都市の数
map_size = 100 #地図の大きさ
max_stagnant = 10 #滞りの最大値
max_iteration = 100 #繰り返しの最大値
is_draw_graph = True #グラフを描画するか

ant_count = 30 #アリの数
alpha = 1 #フェロモンの影響度
beta = 5 #距離の影響度
evaporation = 0.5 #フェロモンの蒸発率
q = 500 #1匹あたりのフェロモンの総量
initial_pheromone = 1.0 #フェロモンの初期値
pr = 0.01 #アリがランダムにルートを決める確率

cities = [] #都市の座標の保存用
pheromone = [] #フェロモンの保存用
ants = [] #アリの保存用
best_route = [] #最短ルートの保存用
best_cost = float("inf") #最短ルートの移動距離

def initialize_pheromone():
    for i in range(0, city_count):
        temp = []
        for j in range(0, city_count):
            if i == j:
                temp.append(0)
            else:
                temp.append(initial_pheromone)
        pheromone.append(temp)

def create_city():
    for i in range(0, city_count):
        while True:
            city = (random.randint(0, map_size-1), random.randint(0, map_size-1))
            if city not in cities:
                break
        cities.append(city)

def create_ants():
    for i in range(0, ant_count):
        ants.append(DiscreteAnt(city_count))

def graph_score(a, b):
    distance = math.sqrt( (cities[a][0]-cities[b][0])**2 + (cities[a][1]-cities[b][1])**2 )
    return distance

def calculate_prpbability(current_index, ant):
    result = [0] * city_count

    i = ant.path[current_index - 1]

    d = 0.0
    for l in range(0, city_count):
        if not ant.visited[l]:
            #?
            d = d + (pheromone[i][l]**alpha) * (1.0/graph_score(i, l)**beta)

    for j in range(0, city_count):
        if ant.visited[j]:
            result[j] = 0.0
        else:
            #?
            n = (pheromone[i][j]**alpha) * ((1.0/graph_score(i, j))**beta)
            result[j] = n/d

    return result

def choose_next_step(current_index, ant):
    if current_index == 0 or random.uniform(0, 1) < pr:
        index = -1
        while index == -1 or ant.visited[index]:
            index = random.randint(0, city_count-1)
        return index
    else:
        prob = calculate_prpbability(current_index, ant)

        r = random.uniform(0,1)

        sum = 0
        for i in range(0, city_count):
            sum = sum + prob[i]
            if sum > r:
                return i

def march():
    for current_index in range(0, city_count):
        for ant in ants:
            next = choose_next_step(current_index, ant)
            ant.visit(current_index, next)

def update_pherpmpn():
    # pheromone evaporation
    for row in range(0, len(pheromone)):
        for col in range(0, len(pheromone[row])):
            pheromone[row][col] = pheromone[row][col] * evaporation

    # pheromone update
    for ant in ants:
        d = q / ant.calculate_cost(city_count, graph_score)

        for i in range(0, city_count-1):
            pheromone[ant.path[i]][ant.path[i+1]] = pheromone[ant.path[i]][ant.path[i+1]] + d

        pheromone[ant.path[city_count-1]][ant.path[0]] = pheromone[ant.path[city_count-1]][ant.path[i + 0]] + d

def find_route():
    global best_cost
    global best_route
    for ant in ants:
        cost = ant.calculate_cost(city_count, graph_score)
        if best_cost > cost:
            best_cost = cost
            best_route = ant.path

def reset_ants():
    for ant in ants:
        ant.clear()

def iterate():
    reset_ants()
    march()
    update_pherpmpn()
    find_route()

if __name__ == "__main__":

    # create_city()
    # print("cities")
    # for i in range(0, len(cities)):
    #     print(cities[i])
    #
    # initialize_pheromone()
    # print("pheromone")
    # for row in range(0, len(pheromone)):
    #     for col in range(0, len(pheromone[row])):
    #         print("{0:6.3f}".format(pheromone[row][col]), end=" ")
    #     print("")
    #
    # create_ants()
    # print("ants")
    # for i in range(0, len(ants)):
    #     print(ants[i])
    #
    # march()
    # print("march")
    # for i in range(0, len(ants)):
    #     print(ants[i], i)
    #     for j in range(0, city_count):
    #         print(ants[i].path[j], end=" ")
    #     print("")
    #
    # update_pherpmpn()
    # print("pheromone")
    # for row in range(0, len(pheromone)):
    #     for col in range(0, len(pheromone[row])):
    #         print("{0:6.3f}".format(pheromone[row][col]), end=" ")
    #     print("")
    #
    # find_route()
    # print("best route")
    # for i in range(0, city_count):
    #     print(best_route[i], end=" ")
    # print("")
    # print("best cost")
    # print(best_cost)
    #
    # reset_ants()
    # print("reset ants")
    # for i in range(0, len(ants)):
    #     print(ants[i], i)
    #     for j in range(0, city_count):
    #         print(ants[i].path[j], end=" ")
    #     print("")

    create_city()
    create_ants()
    initialize_pheromone()

    iteration = 0 #繰り返し回数
    stagnant = 0 #滞り回数
    while iteration < max_iteration and stagnant < max_stagnant:
        last_cost = best_cost
        iterate()
        iteration += 1
        if last_cost == best_cost: #最短ルートが更新されなかった場合
            stagnant += 1
        else:
            stagnant = 0
        print("Iteration #" + str("{0:2d}".format(iteration)) + \
              " Score=" + str(best_cost) + \
              " stagnant=" + str(stagnant))

    print("Best Score : " + str(best_cost))
    print("Best Route : ", end="")
    for i in range(0, city_count):
        print(best_route[i], end=" ")
    print("")
    print("pheromone")
    for row in range(0, len(pheromone)):
        for col in range(0, len(pheromone[row])):
            print("{0:8.5f}".format(pheromone[row][col]), end=" ")
        print("")

    if is_draw_graph:
        print("Draw Graph!!")
        graph = nx.DiGraph()
        graph.add_nodes_from(range(0, city_count))
        for j in range(0, city_count-1):
            graph.add_edge(best_route[j], best_route[j+1])
        graph.add_edge(best_route[city_count-1], best_route[0])
        nx.draw(graph, cities, with_labels=True)
        plt.show()


import parsley, itertools, math
from utils import *
from Naub       import Naub
from NaubJoint  import NaubJoint

grammar = '''
name        = letter:c (letterOrDigit*):cs      -> "".join([c] + cs)
ref         = ref_pos | ref_name | ref_desc
ref_pos     = '{}'                              -> interpreter.ref_pos()
ref_name    = '{' name:n '}'                    -> interpreter.ref_name(n)
ref_desc    = name:n                            -> interpreter.ref_desc(n)
chain       = ref:r ('-' ref)*:rs               -> RefChain([r] + rs)
spec        = chain:c (ws chain)*:cs            -> RefGraph([c] + cs)
'''

class Interpreter(object):

    def __init__(self, args, kwargs):
        self.args       = args
        self.kwargs     = kwargs
        self.ref_pos_i  = 0
        self.refs       = {}

    def ref_pos(self):
        ref             = Ref(
            val         = self.args[self.ref_pos_i])
        self.ref_pos_i += 1
        return ref

    def ref_name(self, name):
        try:    return self.refs[name]
        except:
            ref             = Ref(
                name        = name,
                val         = self.kwargs[name])
            self.refs[name] = ref
            return ref

    def ref_desc(self, desc):
        return Ref(desc = desc)

class Ref(object):

    def __init__(self, name = None, desc = None, val = None):
        self.name   = name
        self.desc   = desc
        self.val    = val

class RefJoin(object):

    def __init__(self, a, b, val = None):
        assert a != b
        self.a      = a
        self.b      = b
        self.val    = val

class RefChain(object):

    def __init__(self, refs):
        self.refs  = refs
        self.joins = self.__join()

    @gather()
    def __join(self):
        refs = self.refs
        for a, b in zip(refs, refs[1:]):
            yield RefJoin(a, b)

class RefGraph(object):

    def __init__(self, chains):
        self.chains     = chains
        refs        = self.refs
        refs        = sorted(refs,      key = lambda ref: ref.desc)
        groups      = itertools.groupby(refs, lambda ref: ref.desc)
        groups      = {group: list(refs) for group, refs in groups}
        self.groups = groups

    @property
    def joins(self):
        joins   = (chain.joins for chain in self.chains)
        return itertools.chain(*joins)

    @property
    def refs(self):
        refs    = (chain.refs for chain in self.chains)
        return itertools.chain(*refs)

    def by_name(self, name):
        return filter(self.refs, lambda ref: ref.name == name)[0]

class NaubSpecBase(object):

    def __init__(self, _ref):
        self._ref  = _ref
        self.fill_with_default_naubs()
        self.fill_with_default_naub_joins()

    @property
    def naubs(self):
        return (ref.val for ref in self._ref.refs)

    @property
    def joins(self):
        return (join.val for join in self._ref.joins)

    @property
    def groups(self):
        return {group: [ref.val for ref in refs] for group, refs in self._ref.groups.items()}

    def fill_with_default_naubs(self):
        refs = (ref for ref in self._ref.refs if ref.val is None)
        for ref in refs:
            ref.val = Naub()

    def fill_with_default_naub_joins(self):
        joins = (join for join in self._ref.joins if join.val is None)
        for join in joins:
            join.val = NaubJoint(join.a.val, join.b.val)
            join.a.val.join_naub(join.b.val, join.val)

    def colorize(self, palette):
        groups      = self.groups
        colors      = itertools.cycle(palette)
        for color, naubs in zip(colors, groups.values()):
            for naub in naubs:
                naub.color = color

    def position_on_circle(self, radius):
        naubs           = list(self.naubs)
        circle          = math.pi * 2 / len(naubs)
        for i, naub in enumerate(naubs):
            x           = i * circle
            naub.pos    = radius * math.cos(x), radius * math.sin(x)

class NaubSpecChain(NaubSpecBase):

    def __repr__(self):
        cls         = type(self)
        return "<{name} {list}>".format(
            #module  = cls.__module__,
            name    = cls.__name__,
            list    = list(self.naubs))

class NaubSpecGraph(NaubSpecBase):

    @property
    def chains(self):
        return (NaubSpecChain(chain) for chain in self._ref.chains)

def parse_spec(spec, *args, **kwargs):
    interpreter     = Interpreter(args, kwargs)
    bindings        = dict(
        interpreter = interpreter,
        RefChain    = RefChain,
        RefGraph    = RefGraph)
    parser          = parsley.makeGrammar(grammar, bindings)
    return parser(spec).spec()

def spec(spec, *args, **kwargs):
    ref_graph       = parse_spec(spec, *args, **kwargs)
    return NaubSpecGraph(ref_graph)


#!/usr/bin/env python
import pygame, sys, time
from pygame.locals import *

SoundsMaterialPath = 'material\\sounds\\'

pygame.init()

soundObj = pygame.mixer.Sound(SoundsMaterialPath+'beeps.wav')
soundObj.play()

time.sleep(1)
soundObj.stop()

soundObj = pygame.mixer.Sound(SoundsMaterialPath+'beepingsound.wav')
soundObj.play()

#pygame.mixer.music.load(SoundsMaterialPath+'backgroundmusic.mp3')
#pygame.mixer.music.play(-1, 0, 0)
time.sleep(10)
pygame.mixer.music.stop()


import json

fn = "A17000000J-030096-o7m.json"
with open(fn, encoding="utf-8") as fnObj:
    datas = json.load(fnObj)

for data in datas:
    if data["職類別"] == "軟體開發及程式設計師":
        year = data["年度"]
        industry = data["行業別"]
        salary = data["經常性薪資"]
        print("年度:", year, "職類別", industry, "經常性薪資", salary)
        print("----------------------------------------------------")


#!/usr/bin/env python
'''
*********************************************************
Copyright @ 2015 EMC Corporation All Rights Reserved
*********************************************************
'''
# -*- coding: utf-8 -*-
"""
Test InfraSIM core component:
    - QEMU
    - ipmi
    - ...
Check:
    - binary exist
    - corresponding process can be started by start service
    - corresponding process can be ended by stop service
"""

from infrasim import qemu
from infrasim import ipmi
from infrasim import socat
from infrasim import model
from infrasim import VM_DEFAULT_CONFIG
import time
import yaml
import os

def setUp():
    workspace = "{}/.infrasim/node-0".format(os.environ["HOME"])
    if os.path.exists(workspace):
        os.system("rm -rf {}".format(workspace))
    with open(VM_DEFAULT_CONFIG, 'r') as f_yml:
        conf = yaml.load(f_yml)
    node = model.CNode(conf)
    node.init_workspace()

def tearDown():
    workspace = "{}/.infrasim/node-0".format(os.environ["HOME"])
    if os.path.exists(workspace):
        os.system("rm -rf {}".format(workspace))

def test_qemu_exist():
    try:
        qemu.get_qemu()
        assert True
    except:
        assert False

def test_ipmi_exist():
    try:
        ipmi.get_ipmi()
        assert True
    except:
        assert False

def test_socat_exist():
    try:
        socat.get_socat()
        assert True
    except:
        assert False

def test_socat_process_start():
    try:
        socat.start_socat()
        time.sleep(2)
        socat.status_socat()
        assert True
    except:
        assert False

def test_ipmi_process_start():
    try:
        ipmi.start_ipmi()
        time.sleep(2)
        ipmi.status_ipmi()
        assert True
    except:
        assert False

def test_qemu_process_start():
    try:
        qemu.status_qemu()
        assert True
    except:
        assert False

def test_qemu_process_status_running():
    try:
        qemu.status_qemu()
        assert True
    except:
        assert False

def test_ipmi_process_status_running():
    try:
        ipmi.status_ipmi()
        assert True
    except:
        assert False

def test_socat_process_status_running():
    try:
        socat.status_socat()
        assert True
    except:
        assert False

def test_qemu_prcess_stop():
    try:
        qemu.stop_qemu()
        qemu.status_qemu()
        assert False
    except:
        assert True

def test_ipmi_process_stop():
    try:
        ipmi.stop_ipmi()
        ipmi.status_ipmi()
        assert False
    except:
        assert True

def test_socat_process_stop():
     try:
         socat.stop_socat()
         socat.status_socat()
         assert False
     except:
         assert True


import torch.nn as nn
import torch
import torch.nn.functional as F
import random as rd
import numpy as np
from torch.autograd import Variable
import time

class LSTMNet(nn.Module):

    def __init__(self, input_size, output_size, hidden_dim, n_layers, dim_linear, device):
        super(LSTMNet, self).__init__()

        # Defining some parameters
        self.hidden_dim = hidden_dim
        self.n_layers = n_layers
        self.device = device

        # Defining the layers
        # RNN Layer
        self.lstm = nn.LSTM(input_size, hidden_dim, n_layers, batch_first=True)
        # stack of fully connected layers
        self.seq = nn.Sequential(nn.Linear(hidden_dim, dim_linear), nn.CELU(), nn.Linear(dim_linear, output_size), nn.CELU())

    def forward(self, x):

        batch_size = x.size(0)

        # Initializing hidden state for first input using method defined below
        hidden, cell = self.init_hidden_and_cell(batch_size)
        hidden, cell = hidden.double(), cell.double()

        # Passing in the input and hidden state into the model and obtaining outputs
        out, (_, _) = self.lstm(x, (hidden, cell))
        #print('1 : ', out.shape, hidden.shape)

        # Reshaping the outputs such that it can be fit into the fully connected layers
        out = out[ : , -1]
        out = self.seq(out)

        return out, hidden

    def init_hidden_and_cell(self, batch_size):
        # This method generates the first hidden state of zeros which we'll use in the forward pass
        # We'll send the tensor holding the hidden state to the device we specified earlier as well
        hidden = torch.zeros(self.n_layers, batch_size, self.hidden_dim).to(self.device)
        cell = torch.zeros(self.n_layers, batch_size, self.hidden_dim).to(self.device)
        return hidden, cell

def train(model, x_train, y_train, x_validation, y_validation, nb_epochs, learning_rate, max_batch_size, criterion=nn.MSELoss()):

    optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
    index = np.arange(x_train.shape[0])
    criterion = criterion.double()

    errors_train, errors_validation = [], []
    nb_batchs = int(x_train.shape[0] // max_batch_size) + 1 * (x_train.shape[0] % max_batch_size != 0)

    t = time.time()

    for n in range(1, nb_epochs + 1):

        rd.shuffle(index)
        x_train, y_train = x_train[index], y_train[index]
        epoch_loss = 0

        for k in range(nb_batchs):

            # Clearing existing gradients from previous epoch
            optimizer.zero_grad()
            input, target = x_train[k * max_batch_size : (k + 1) * max_batch_size], y_train[k * max_batch_size : (k + 1) * max_batch_size]
            #print('0 : ', input.shape, target.shape)
            model = model.double()
            output, _ = model(input)
            #print('2 : ', output.shape, target.shape)
            loss = criterion(output, target).double()
            # Performing backprop
            loss.backward()
            # Updating weights
            optimizer.step()
            # Adding the weighted loss to epoch_loss
            epoch_loss += loss.item() * input.shape[0]

        errors_train.append(epoch_loss / x_train.shape[0])
        errors_validation.append(compute_loss(model, x_validation, y_validation, max_batch_size, criterion))

        if n % 10 == 0:

            print(f"Epoch: {n}/{nb_epochs}.............", end=' ')
            print(f"Train loss: {round(errors_train[-1], 6)}")
            print(f"              ............. Test loss: {round(errors_validation[-1], 6)}")
            t = time.time() - t
            print("Time elapsed for the last 10 epochs : ", round(t / 60, 2), 'min\n')
            t = time.time()

    return errors_train, errors_validation

def compute_loss(model, x, y, max_batch_size, criterion=nn.MSELoss()):

    nb_batchs = int(x.shape[0] // max_batch_size) + 1 * (x.shape[0] % max_batch_size != 0)
    total_loss = 0

    with torch.no_grad():

        for k in range(nb_batchs):

            input, target = x[k * max_batch_size : (k + 1) * max_batch_size], y[k * max_batch_size : (k + 1) * max_batch_size]
            #print('0 : ', input.shape, target.shape)
            model = model.double()
            output, _ = model(input)
            #print('2 : ', output.shape, target.shape)
            loss = criterion(output, target).double()
            # Adding the weighted loss to total_loss
            total_loss += loss.item() * input.shape[0]

    return total_loss / x.shape[0]


import numpy as np
from sklearn import linear_model
from numba import jit
#from sklearn.linear_model import LinearRegression

@jit
def polynomial_this(x,y,n):
    """
    Lager designmatrise for for en funksjon av to variabler
    for alle polynomer opp til og med grad n
    """
    X = np.c_[np.ones(len(x))]
    for i in range(1,n+1):
        X = np.c_[X,x**(i)]
        for j in range(i-1,0,-1):
            X = np.c_[X,(x**(j))*(y**(i-j))]
        X = np.c_[X,y**(i)]
    return X

def bias(true, pred):
    bias = np.mean((true - np.mean(pred))**2)
    return bias

def MSE(true, pred):
    MSE = sum((true-pred)**2)/(len(true))
    return MSE

def R2(true, pred):
    R2 = 1-(np.sum((true - pred)**2)/np.sum((true-np.mean(pred))**2))
    return R2

def KfoldCrossVal(dataset, dataset2, Numbfold):
    """
    Takes in two coupled datasets and returns them splitted into k-matching
    "folds" by produsing randomindices.

    KfoldCrosVal([1,4,5,6],[7,6,8,5],2) may return

    [[1,5],[4,6]], [[7,8],[6,2]]

    by produsing the indices [[0,2],[1,3]] from input "dataset"
    """
    indices = np.arange(len(dataset[:, 0]))
    random_indices = np.random.choice(indices, size = len(dataset[:, 0]), replace = False)
    interval = int(len(dataset[:, 0])/Numbfold)
    datasetsplit = []
    dataset2split = []
    for k in range(Numbfold):
        datasetsplit.append(dataset[random_indices[interval*k : interval*(k + 1)]])
        dataset2split.append(dataset2[random_indices[interval*k : interval*(k + 1)]])

    return np.asarray(datasetsplit), np.asarray(dataset2split)

class regression:
    def __init__(self,X,z):
        self.z = z
        self.X = X

    @jit
    def ridge(self,lambd):
        X = self.X
        beta = np.linalg.inv(X.T.dot(X)+lambd*np.identity(X.shape[1])).dot(X.T.dot(self.z))
        self.znew = X.dot(beta)

        self.beta = beta
        return beta#plutt

    @jit
    def OLS(self):
        X = self.X
        beta = np.linalg.pinv(X.T.dot(X)).dot(X.T.dot(self.z))
        self.znew = X.dot(beta)
        return beta

    @jit
    def lasso(self, lambd):
        lasso = linear_model.Lasso(alpha = lambd,fit_intercept = False)
        lasso.fit(self.X, self.z)
        beta = lasso.coef_#[:,np.newaxis]
        self.znew = self.X.dot(beta)
        return beta

    def beata_variance(self):
        sigma2 = (1./(len(self.z)-self.X.shape[1]-1))*sum((self.z-self.znew)**2)
        covar = np.linalg.inv(self.X.T.dot(self.X))*sigma2
        var = np.diagonal(covar)
        return beta_var

    def MSE(self):
        MSE = np.mean((self.z-self.znew)**2)
        return MSE

    def R2(self):
        self.R2 = 1-(np.sum((self.z - self.znew)**2)/np.sum((self.z-np.mean(self.z))**2))
        return self.R2

def FRANK(x, y):
    """
    Frankie function for testing the class
    """
    term1 = 0.75*np.exp(-(0.25*(9*x - 2)**2) - 0.25*((9*y-2)**2))
    term2 = 0.75*np.exp(-((9*x + 1)**2)/49.0 - 0.1*(9*y+1))
    term3 = 0.5*np.exp(-(9*x-7)**2/4.0 - 0.25*((9*y-3)**2))
    term4 = -0.2*np.exp(-(9*x-4)**2 - (9*y-7)**2)
    return term1 + term2 + term3 + term4

if __name__== "__main__" :
    def test_reg():
        a = 3


'''
Author: Swarfte
Date: 2021-08-09 21:36:18
LastEditors: Chau Lap Tou
LastEditTime: 2021-08-30 21:57:56
FilePath: \calculate\local_function.py
FileOutput: pyinstaller -F -w file_name.py -p C:/python/lib/site-packages
GithubName: Swarfte
'''
import random as rd
import math
import re
import sympy as SP

Lastnumber = ""#*用作記憶功能,即紀錄結果
DecimalPlaces = 2 #*預設保留的小數位

def CalcLog(sentence):#*計算對數
    get_num = sentence
    log_base = 10
    ans = 0
    if "," in get_num:#*輸入底數的情況
        number = get_num.split(",")
        log_base = float(number[0])#*用前面那個數為底
        logarithm = float(number[1])#*逗號後的為對數
    else:#*沒有輸入底數的情況
        logarithm = float(sentence)
    ans = math.log(logarithm,log_base)#*log() 函式接受兩個引數。第一個引數是數字，第二個引數是基值
    return ans

def CalcTrigonometric(sentence,mode):#*計算三角函數
    get_num = sentence
    ans = 0
    if "," in get_num:
        get_num = get_num[:len(get_num)-1]
        num = float(get_num)
        if mode == "sin" :
            ans = SP.cos(math.radians(num))#*化為弧度制
        elif mode == "tan":
            ans = SP.cot(math.radians(num))
        elif mode == "sec":
            ans = SP.csc(math.radians(num))
    else:
        num = float(get_num)
        if mode == "sin" :
            ans = SP.sin(math.radians(num))
        elif mode == "tan":
            ans = SP.tan(math.radians(num))
        if mode == "sec" :
            ans = SP.sec(math.radians(num))
    return ans#*預設保留小數位後2位

def ReplaceExpression(sentence):
    ans = sentence
    if ".." in ans: #*在運算時改變式子保留的小數點,且只允許式子出現一次
        choose = re.compile(r"\.\.[0-9]+")
        num_native = re.search(choose,ans).group()#*帶有..
        num_get = num_native[2:]
        global DecimalPlaces
        DecimalPlaces = int(num_get)
        ans = ans.replace(num_native,"")#*用空白替換

    while "log" in ans:#*處理式子中全部Log數
        choose = re.compile(r"log[0-9]+\.?[0-9]*\,[0-9]+\.?[0-9]*|log[0-9]+\.?[0-9]*")#*找出log數的位置
        log_native = re.search(choose,ans).group()#*帶有Log字串
        log_get = log_native[3:]#*獲取關建的數字(log的數字)
        log_ans = str(CalcLog(log_get)) #*獲取答案
        ans = ans.replace(log_native,log_ans)#*把答案取代輸入中的log,同時覆蓋原來的輸入端

    while "sin" in ans:#*處理式子中全迎的sin/cos
        choose = re.compile(r"sin[0-9]+\.?[0-9]*\,?")#*找出sin的位置
        num_native = re.search(choose,ans).group()#*帶有sin
        num_get = num_native[3:]
        num_ans= str(CalcTrigonometric(num_get,"sin"))
        ans = ans.replace(num_native,num_ans)

    while "tan" in ans:#*處理式子中全迎的tan/cos
        choose = re.compile(r"tan[0-9]+\.?[0-9]*\,?")#*找出tan的位置
        num_native = re.search(choose,ans).group()#*帶有tan
        num_get = num_native[3:]
        num_ans= str(CalcTrigonometric(num_get,"tan"))
        ans = ans.replace(num_native,num_ans)

    while"sec" in ans:#*處理式子中全迎的sec/csc
        choose = re.compile(r"sec[0-9]+\.?[0-9]*\,?")#*找出sec的位置
        num_native = re.search(choose,ans).group()#*帶有sce
        num_get = num_native[3:]
        num_ans= str(CalcTrigonometric(num_get,"sec"))
        ans = ans.replace(num_native,num_ans)

    return ans

class function(object):
    def __init__(self,gui):
        super().__init__()
        self.function = gui#*獲取傳入的gui
        self.input = self.function.InputLine.text()#*獲取當前輸入端的資料
        self.output = self.function.OutputLine.text()#*獲取當前輸出端的資料

    def Test(self):
        print("hello")

    def Printf(self,push,string):#*輸出新增的文字
        self.input = push.text()#*獲取傳入的元件的資料(針對Line)
        self.input += string
        push.setText(self.input)

    def Clean(self,inputline,outputline) :#*清除line的資料
        self.input = inputline.text()
        self.input = ""#*重置輸入欄
        inputline.setText(self.input)
        outputline.setText(self.input)

    def Delete(self,push):#*用作清除不需要的字元
        self.input = push.text()
        self.input = self.input[:-1]#*刪除最後的字元
        push.setText(self.input)

    def RandomMath(self,inputline,outputline):#*獲取隨機數字
        try:
            global Lastnumber
            num = 0
            self.input = inputline.text()
            if len(self.input) == 0 :#*沒有輸入數字時,則像普體骰子一樣輸出1~6
                num = rd.randint(1,6)
            else:
                if "," in self.input :#*輸入區間的話則骰出區間內的數字
                    numlist = self.input.split(",")
                    num = rd.randint(int(numlist[0]),int(numlist[1]))
                else:#*只輸入一個數字的話則為1~該數字隨機骰出一個
                    num = rd.randint(1,int(self.input))
            Lastnumber = str(num)
            outputline.setText(str(num))
        except :
            outputline.setText("error")

    def RandomEnglish(self,inputline,outputline):#*隨機生成英文
        try:
            global Lastnumber
            english =["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
            eng = ""
            self.input = inputline.text()
            if len(self.input) == 0 :#*沒有輸入數字時,則像普體選擇題一樣輸出A~D
                eng = english[rd.randint(0,3)]
            else:#*輸入區間的話則骰出區間內的字母
                if "," in self.input :
                    englist = self.input.split(",")
                    eng = english[rd.randint(int(englist[0])-1,int(englist[1])-1)]
                else:#*只輸入一個數字的話則為1~該數字隨機骰出一個字母
                    eng = english[rd.randint(1,int(self.input))]
            Lastnumber = eng
            outputline.setText(eng)
        except :
            outputline.setText("error")

    def RememberNumber(self,push):#*記憶功能
        global Lastnumber
        self.input = push.text()
        self.input += Lastnumber #*加左原本的句子上
        push.setText(self.input)

    def Basic (self,inputline,outputline):#*用作計算結果
        try:
            global Lastnumber
            self.input = inputline.text()
            self.input = ReplaceExpression(self.input)#*處理log數

            global DecimalPlaces
            try :
                ans = round(eval(self.input),DecimalPlaces)#*計算結果
            except:
                ans = str(eval(self.input))
                ans += "it is a error"
            outputline.setText(str(ans))#*在輸出端顯示結果
            Lastnumber = str(ans)
        except :
            outputline.setText("error")#*有問題時則報錯


import read_doc as rd
import matplotlib.pyplot as plt
from math import *

globalInfo = rd.collectDataFromTab(rd.readFile())

for x in ['Arithmancy', 'Astronomy', 'Herbology', 'Defense Against the Dark Arts', 'Divination', 'Muggle Studies', 'Ancient Runes', 'History of Magic', 'Transfiguration', 'Potions', 'Care of Magical Creatures', 'Charms', 'Flying']:
	xTab = []
	for feature in globalInfo:
		if feature[0] == x:
			for grade in feature[1:]:
				try:
					float(grade)
				except:
					xTab.append('NaN')
				else:
					xTab.append(float(grade))
	for y in ['Arithmancy', 'Astronomy', 'Herbology', 'Defense Against the Dark Arts', 'Divination', 'Muggle Studies', 'Ancient Runes', 'History of Magic', 'Transfiguration', 'Potions', 'Care of Magical Creatures', 'Charms', 'Flying']:
		if y == x:
			pass
		else:
			yTab = []
			for feature in globalInfo:
				if feature[0] == y:
					for grade in feature[1:]:
						try:
							float(grade)
						except:
							yTab.append('NaN')
						else:
							yTab.append(float(grade))

			if len(xTab) < len(yTab):
				yTab = yTab[:len(xTab)]
			elif len(xTab) > len(yTab):
				xTab = xTab[:len(yTab)]

			astronomy = xTab
			defense = yTab

			plt.scatter(astronomy, defense, s=0.5)

			plt.suptitle("Quelles sont les deux features qui sont semblables ?")
			plt.title(x + " et " + y)
			plt.xlabel(x)
			plt.ylabel(y)
			plt.show()


from django.contrib.auth.models import User
from django.views.generic import ListView
from django.views.generic.base import TemplateView
from django_tables2 import SingleTableView
import django_tables2 as tables
from django_tables2.config import RequestConfig
from django.shortcuts import render
import csv
from django.http import HttpResponse

from django.shortcuts import render, get_object_or_404

from .models import DailyRecord, Participant
from .tables import DailyRecordTable, DailyTable
# Create your views here.

def index(request):
	return render(request, 'dailyrecord/index.html')

def export_daily_record_csv(request):
    response = HttpResponse(content_type='text/csv')
    response['Content-Disposition'] = 'attachment; filename="nja_daily_record.csv"'

    writer = csv.writer(response)
    writer.writerow(['S.n.', 'Ogransied Date', 'Program Name',	'Venue', 'Organiser'])

    dailyrecords = DailyRecord.objects.all().values_list('id', 'date_organised', 'program_name', 'venue', 'organiser')
    for dailyrecord in dailyrecords:
        writer.writerow([u''+ str(row)+' ' for row in dailyrecord])

    return response

def dailyrecord_list(request):
	dailyrecords = DailyRecord.public.all()
	context  = {'dailyrecords': dailyrecords}
	return render(request, 'dailyrecord/programs.html', context)

#test method
# def daily_home(request):
# 	 return render(request, 'dailyrecord/daily-home.html', {'dailyrecords': DailyRecord.public.all()})

def daily_home(request):
	table = DailyTable(DailyRecord.objects.all())
	RequestConfig(request).configure(table)
	return render(request, 'dailyrecord/daily-home.html', {'dailyrecords': table})

class ProgramListAll(ListView):
	model = DailyRecord
	template_name = "dailyrecord/programs.html"

class HomePageView(TemplateView):
	template_name = "dailyrecord/index.html"

class AboutPageView(TemplateView):
	template_name = "dailyrecord/about.html"

qs = DailyRecord.public.all()

class DailyActivityPageView(SingleTableView):
	table_class = DailyRecordTable
	queryset = DailyRecord.public.all()
	# queryset = Participant(DailyRecord.objects.all())
	# RequestConfig(request).configure(queryset)
	template_name = 'dailyrecord/daily-activity-record.html'

	def get_context_data(self, **kwargs):
		context = super(DailyActivityPageView, self).get_context_data(**kwargs)
		context['table_participant'] =  Participant(DailyRecord.public.all()),
		return context

class ExtraCuricularActivityPageView(TemplateView):
	template_name = "dailyrecord/extra-curicular-activity.html"


# date: 2019-12-22
# author: jannik wiessler
# e-mail: jannik.wiessler@googlemail.com
# GUI for invoice feature extraction

import wx
from invoiceFrame import invoiceFrame
import os
import sys
import logging

# get current directory: for logger
if getattr(sys, 'frozen', False):
    application_path = os.path.dirname(sys.executable)
elif __file__:
    application_path = os.path.abspath(__file__)[0:-(len(__file__)+1)]

class MyApp(wx.App):
    def __init__(self):
        super().__init__()

        self.__frame = invoiceFrame(parent=None, title="Menu Bar App")
        self.__frame.Show()

if __name__ == '__main__':

    logging.basicConfig(filename=application_path+'/GuiWxInfo.log',
                    level=logging.DEBUG,
                    format='%(asctime)-15s %(name)s %(message)s')
    app = MyApp()
    app.MainLoop()


import pytest
from pupa.scrape import Bill
from pupa.utils.generic import get_pseudo_id
from pupa.exceptions import ScrapeValueError

def toy_bill():
    b = Bill(
        identifier="HB 2017",
        legislative_session="2012A",
        title="A bill for an act to raise the cookie budget by 200%",
        from_organization="Foo Senate",
        classification="bill",
    )
    b.add_source("http://uri.example.com/", note="foo")
    return b

def test_basic_valid_bill():
    b = toy_bill()
    b.validate()
    assert "we got here"

def test_bill_type_setting():
    # default
    b = Bill(identifier="some bill", legislative_session="session", title="the title")
    assert b.classification == ["bill"]

    # string -> list
    b = Bill(
        identifier="some bill",
        legislative_session="session",
        title="the title",
        classification="string",
    )
    assert b.classification == ["string"]

    # list unmodified
    b = Bill(
        identifier="some bill",
        legislative_session="session",
        title="the title",
        classification=["two", "items"],
    )
    assert b.classification == ["two", "items"]

    # tuple -> list
    b = Bill(
        identifier="some bill",
        legislative_session="session",
        title="the title",
        classification=("two", "items"),
    )
    assert b.classification == ["two", "items"]

def test_basic_invalid_bill():
    """Test that we can create an invalid bill, and validation will fail"""
    b = toy_bill()
    b.identifier = None
    with pytest.raises(ValueError):
        b.validate()

def test_from_organization():
    # none set
    assert get_pseudo_id(Bill("HB 1", "2014", "Some Bill").from_organization) == {
        "classification": "legislature"
    }

    # chamber set
    assert get_pseudo_id(
        Bill("SB 1", "2014", "Some Bill", chamber="upper").from_organization
    ) == {"classification": "upper"}
    # org direct set
    assert (
        Bill("HB 1", "2014", "Some Bill", from_organization="test").from_organization
        == "test"
    )

    # can't set both
    with pytest.raises(ValueError):
        Bill("HB 1", "2014", "Some Bill", from_organization="upper", chamber="upper")

def test_add_action():
    """Make sure actions work"""
    b = toy_bill()
    b.add_action("Some dude liked it.", "2013-04-29T20:00Z", chamber="lower")
    assert len(b.actions) == 1
    assert b.actions[0]["description"] == "Some dude liked it."
    assert get_pseudo_id(b.actions[0]["organization_id"]) == {"classification": "lower"}
    assert b.actions[0]["date"] == "2013-04-29T20:00Z"
    b.validate()

def test_action_extra():
    b = toy_bill()
    b.add_action(
        "an action with some extra information",
        "2017-01-01",
        extras=dict(sitting_chair="Adams"),
    )
    assert b.actions[0]["extras"] == {"sitting_chair": "Adams"}

def test_add_related_bill():
    """Make sure related bills work"""
    b = toy_bill()
    b.add_related_bill(
        identifier="HB 2020", legislative_session="2011A", relation_type="companion"
    )
    assert len(b.related_bills) == 1
    assert b.related_bills[0] == {
        "identifier": "HB 2020",
        "legislative_session": "2011A",
        "relation_type": "companion",
    }
    b.validate()

def test_add_sponsor():
    b = toy_bill()
    b.add_sponsorship(
        name="Joe Bleu",
        classification="Author",
        entity_type="person",
        primary=True,
        chamber="upper",
    )
    assert len(b.sponsorships) == 1
    assert b.sponsorships[0] == {
        "person_id": '~{"name": "Joe Bleu"}',
        "name": "Joe Bleu",
        "classification": "Author",
        "entity_type": "person",
        "primary": True,
        "organization_id": None,
    }
    b.validate()

def test_subjects():
    b = toy_bill()
    b.add_subject("Foo")
    b.add_subject("Bar")
    assert b.subject == ["Foo", "Bar"]
    b.validate()

def test_abstract():
    b = toy_bill()
    b.add_abstract("this bill is stupid", "K-5", "1969-10-20")
    b.add_abstract("this legislative document is ignorant", "6-12", "2010-10-10")
    assert b.abstracts == [
        {"note": "K-5", "abstract": "this bill is stupid", "date": "1969-10-20"},
        {
            "note": "6-12",
            "abstract": "this legislative document is ignorant",
            "date": "2010-10-10",
        },
    ]

def test_add_documents():
    b = toy_bill()

    # should only add one document since they all have same note
    b.add_document_link(
        note="Fiscal Impact",
        date="2013-04",
        url="http://hi.example.com/foo#bar",
        media_type="text/html",
    )
    b.add_document_link(note="Fiscal Impact", date="2013-04", url="http://foobar.baz")
    assert len(b.documents) == 1

    # should now be two documents
    b.add_document_link(
        note="Other Document", date="2013-04", url="http://foobar.baz/other"
    )
    assert len(b.documents) == 2

    # valid documents so far
    b.validate()

    # an invalid document
    b.add_document_link(
        note="Fiscal Impact", date="2013-04", url=None, media_type="foo"
    )
    with pytest.raises(ScrapeValueError):
        b.validate()

def test_versions():
    b = toy_bill()

    # only one document, multiple links
    b.add_version_link(url="http://pault.ag/", note="Final Version", date="2013-04")
    b.add_version_link(url="http://pault.ag/foo", note="Final Version", date="2013-04")
    b.validate()
    assert len(b.versions) == 1
    assert len(b.versions[0]["links"]) == 2

    # duplicate!
    with pytest.raises(ValueError):
        b.add_version_link(
            url="http://pault.ag/foo", note="Final Version", date="2013-04"
        )

    # ignore duplicate - nothing should change
    b.add_version_link(
        url="http://pault.ag/foo",
        note="Final Version",
        date="2013-04",
        on_duplicate="ignore",
    )
    assert len(b.versions) == 1
    assert len(b.versions[0]["links"]) == 2

    # duplicate URL
    with pytest.raises(ValueError):
        b.add_version_link(
            url="http://pault.ag/foo", note="Finals Versions", date="2013-04"
        )
    assert len(b.versions) == 1
    assert len(b.versions[0]["links"]) == 2

    # a new doc, numbers go up
    b.add_version_link(
        url="http://pault.ag/foovbar", note="Finals Versions", date="2013-04"
    )
    assert len(b.versions) == 2
    assert len(b.versions[1]["links"]) == 1

    # still validates
    b.validate()

def test_str():
    b = toy_bill()
    assert b.identifier in str(b)

def test_no_whitespace_in_uri():
    b = Bill(
        identifier="HB 2017",
        legislative_session="2012A",
        title="A bill for an act to raise the cookie budget by 200%",
        from_organization="Foo Senate",
        classification="bill",
    )
    b.add_source("http://uri.example.com/fail here", note="foo")
    with pytest.raises(ScrapeValueError):
        b.validate()


from lh3.api import *
from dashboard.utils.utils import (
    soft_anonimyzation,
    operatorview_helper,
    Chats,
    retrieve_transcript,
    search_chats,
)
from datetime import datetime, timedelta, timezone
from django.views.generic import TemplateView
from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt
from django.http import Http404
from django.http import HttpResponse, HttpResponseNotFound
from pprint import pprint as print
from django.contrib import messages
import random

from datetime import datetime
from datetime import timezone
import json

from dateutil.parser import parse

from django.http import JsonResponse

from dashboard.utils.ask_schools import (
    find_school_by_operator_suffix,
    find_queues_from_a_school_name,
    find_school_by_queue_or_profile_name,
)

import warnings

warnings.filterwarnings("ignore")

"""
List of VIEW FUNCTIONS/CLASSES
SearchProfileResultsView
get_this_profile
get_transcript
get_chats_for_this_school_using_an_username
get_chats_for_this_school_using_this_queue_name
get_chats_from_this_queue_using_only_the_queue_name
get_chats_for_this_user
get_chats_for_this_queue
get_chats_from_yesterday
get_chats_from_yesterday_from_mentees
"""

def get_chats_for_this_school_using_an_username(request, *args, **kwargs):
    """[summary]

    Args:
        request ([type]): [description]

    Returns:
        [type]: [description]
    """
    client = Client()
    username = kwargs.get("username", None)
    school_name = find_school_by_operator_suffix(username).lower()
    queues_from_school = find_queues_from_a_school_name(school_name)
    print("username: {0}".format(username))
    print("school_name: {0}".format(school_name))
    query = {
        "query": {
            "queue": queues_from_school,
            "from": "2021-01-01",
            "to": "2021-12-31",
        },
        "sort": [{"started": "descending"}],
    }
    queue_chats = client.api().post("v4", "/chat/_search", json=query)
    chats = soft_anonimyzation(queue_chats)
    today = datetime.today()
    current_year = today.year
    total_chats = len(chats)

    heatmap = [
        parse(chat.get("started")).replace(tzinfo=timezone.utc).timestamp()
        for chat in chats
    ]
    counter = {x: heatmap.count(x) for x in heatmap}
    heatmap_chats = json.dumps(counter)
    # print(chats)
    chats = [Chats(chat) for chat in chats]
    if request.is_ajax():
        return JsonResponse(
            {
                "object_list": chats,
                "heatmap_chats": heatmap_chats,
                "username": school_name,
                "current_year": current_year,
                "total_chats": total_chats,
            },
            safe=False,
        )
    return render(
        request,
        "results/chats.html",
        {
            "object_list": chats,
            "heatmap_chats": heatmap_chats,
            "username": school_name,
            "current_year": current_year,
            "total_chats": total_chats,
        },
    )

def get_chats_for_this_school_using_this_queue_name(request, *args, **kwargs):
    """[summary]

    Args:
        request ([type]): [description]

    Returns:
        [type]: [description]
    """
    client = Client()
    queue_name = kwargs.get("queue_name", None)
    school_name = find_school_by_queue_or_profile_name(queue_name)
    queues_from_school = find_queues_from_a_school_name(school_name)

    print("queue_name: {0}".format(queue_name))
    print("school_name: {0}".format(school_name))
    query = {
        "query": {
            "queue": queues_from_school,
            "from": "2021-01-01",
            "to": "2021-12-31",
        },
        "sort": [{"started": "descending"}],
    }
    # breakpoint()
    queue_chats = client.api().post("v4", "/chat/_search", json=query)
    chats = soft_anonimyzation(queue_chats)
    today = datetime.today()
    current_year = today.year
    total_chats = len(chats)

    heatmap = [
        parse(chat.get("started")).replace(tzinfo=timezone.utc).timestamp()
        for chat in chats
    ]
    counter = {x: heatmap.count(x) for x in heatmap}
    heatmap_chats = json.dumps(counter)
    # print(chats)
    chats = [Chats(chat) for chat in chats]
    if request.is_ajax():
        return JsonResponse(
            {
                "object_list": chats,
                "heatmap_chats": heatmap_chats,
                "username": school_name,
                "current_year": current_year,
                "total_chats": total_chats,
            },
            safe=False,
        )
    return render(
        request,
        "results/chats_from_queues.html",
        {
            "object_list": chats,
            "heatmap_chats": heatmap_chats,
            "username": school_name,
            "current_year": current_year,
            "total_chats": total_chats,
        },
    )

def get_chats_from_this_queue_for_this_year_using_only_the_queue_name(
    request, *args, **kwargs
):
    client = Client()
    queue_name = kwargs.get("queue_name", None)
    today = datetime.today()

    print("queue_name: {0}".format(queue_name))
    query = {
        "query": {"queue": [queue_name], "from": str(today.year)+"-01-01", "to": str(today.year)+"-12-31"},
        "sort": [{"started": "descending"}],
    }
    queue_chats, content_range = search_chats(
            client, query, chat_range=(0, 500)
        )
    # breakpoint()
    chats = soft_anonimyzation(queue_chats)
    today = datetime.today()
    current_year = today.year
    total_chats = len(chats)

    heatmap = [
        parse(chat.get("started")).replace(tzinfo=timezone.utc).timestamp()
        for chat in chats
    ]
    counter = {x: heatmap.count(x) for x in heatmap}
    heatmap_chats = json.dumps(counter)
    # print(chats)
    chats = [Chats(chat) for chat in chats]
    if request.is_ajax():
        return JsonResponse(
            {
                "object_list": chats,
                "heatmap_chats": heatmap_chats,
                "username": queue_name,
                "get_chat_for_this_year": True,
                "current_year": current_year,
                "total_chats": total_chats,
            },
            safe=False,
        )
    return render(
        request,
        "results/chats.html",
        {
            "object_list": chats,
            "get_chat_for_this_year": True,
            "heatmap_chats": heatmap_chats,
            "username": queue_name,
            "current_year": current_year,
            "total_chats": total_chats,
        },
    )

def get_chats_for_this_user(request, username):
    """[summary]

    Args:
        request ([type]): [description]
        username ([type]): [description]
        information ([type], optional): [description]. Defaults to None.

    Returns:
        [type]: [description]
    """
    client = Client()
    today = datetime.today()
    query = {
        "query": {"operator": [username], "from": str(today.year)+"-01-01", "to": str(today.year)+"-12-31"},
        "sort": [{"started": "descending"}],
    }
    chats_from_users, content_range = search_chats(
            client, query, chat_range=(0, 500)
        )
    chats = soft_anonimyzation(chats_from_users)

    today = datetime.today()
    current_year = today.year
    total_chats = len(chats_from_users)

    assignments = operatorview_helper(username)
    heatmap = [
        parse(chat.get("started")).replace(tzinfo=timezone.utc).timestamp()
        for chat in chats
    ]
    counter = {x: heatmap.count(x) for x in heatmap}
    heatmap_chats = json.dumps(counter)
    if request.is_ajax():
        return JsonResponse(
            {
                "chats": chats,
                "assignments": assignments,
                "get_chat_for_this_year": True,
                "heatmap_chats": heatmap_chats,
                "username": username,
                "current_year": current_year,
                "total_chats": total_chats,
            },
            safe=False,
        )
    chats = [Chats(chat) for chat in chats]
    return render(
        request,
        "results/chats.html",
        {
            "object_list": chats,
            "assignments": assignments,
            "get_chat_for_this_year": True,
            "heatmap_chats": heatmap_chats,
            "username": username,
            "current_year": current_year,
            "total_chats": total_chats,
        },
    )

def get_chats_for_this_user_for_this_year(request, username, information=None):
    client = Client()
    today = datetime.today()
    query = {
        "query": {
            "operator": [username],
            "from": str(today.year) + "-01-01",
            "to": str(today.year) + "-12-31",
        },
        "sort": [{"started": "descending"}],
    }
    chats_from_users, content_range = search_chats(client, query, chat_range=(0, 500))
    chats = soft_anonimyzation(chats_from_users)

    today = datetime.today()
    current_year = today.year
    total_chats = len(chats_from_users)

    assignments = operatorview_helper(username)
    heatmap = [
        parse(chat.get("started")).replace(tzinfo=timezone.utc).timestamp()
        for chat in chats
    ]
    counter = {x: heatmap.count(x) for x in heatmap}
    heatmap_chats = json.dumps(counter)
    if request.is_ajax():
        return JsonResponse(
            {
                "chats": chats,
                "assignments": assignments,
                "heatmap_chats": heatmap_chats,
                "username": username,
                "current_year": current_year,
                "total_chats": total_chats,
            },
            safe=False,
        )
    chats = [Chats(chat) for chat in chats]
    return render(
        request,
        "results/chats.html",
        {
            "object_list": chats,
            "assignments": assignments,
            "heatmap_chats": heatmap_chats,
            "username": username,
            "current_year": current_year,
            "total_chats": total_chats,
        },
    )

def get_chats_for_this_queue(request, *args, **kwargs):
    client = Client()
    queue = kwargs.get("queue_name", None)
    query = {
        "query": {"queue": [queue], "from": "2021-01-01", "to": "2021-01-19"},
        "sort": [{"started": "descending"}],
    }
    queue_chats = client.api().post("v4", "/chat/_search", json=query)
    chats = soft_anonimyzation(queue_chats)
    return JsonResponse(chats, safe=False)

def get_chats_of_today(request, *args, **kwargs):
    client = Client()
    today = datetime.today()

    query = {
        "query": {"from": today.strftime("%Y-%m-%d")},
        "sort": [{"started": "ascending"}],
    }
    chats_from_users = client.api().post("v4", "/chat/_search", json=query)
    chats = soft_anonimyzation(chats_from_users)

    heatmap = [
        parse(chat.get("started")).replace(tzinfo=timezone.utc).timestamp()
        for chat in chats
    ]
    counter = {x: heatmap.count(x) for x in heatmap}
    heatmap_chats = json.dumps(counter)
    username = "Yesterday"
    # filter chats from yesteday only
    chats = [Chats(chat) for chat in chats]

    # return JsonResponse(chats, safe=False)
    if request.is_ajax():
        return JsonResponse(chats, safe=False)
    return render(
        request,
        "results/chats.html",
        {
            "object_list": chats,
            "heatmap_chats": heatmap_chats,
            "username": username,
            "current_year": "Yesterday",
            "total_chats": len(chats),
        },
    )

def get_chats_from_yesterday(request, *args, **kwargs):
    client = Client()
    today = datetime.today()
    yesterday = today - timedelta(days=1)

    query = {
        "query": {"from": yesterday.strftime("%Y-%m-%d")},
        "sort": [{"started": "ascending"}],
    }
    # chats_from_users = client.api().post('v4', '/chat/_search', json = query)
    chats_from_users, content_range = search_chats(client, query, chat_range=(0, 350))
    chats = soft_anonimyzation(chats_from_users)

    heatmap = [
        parse(chat.get("started")).replace(tzinfo=timezone.utc).timestamp()
        for chat in chats
    ]
    counter = {x: heatmap.count(x) for x in heatmap}
    heatmap_chats = json.dumps(counter)
    username = "Yesterday"
    # filter chats from yesteday only
    chats = [
        chat
        for chat in chats
        if yesterday.strftime("%Y-%m-%d")
        == parse(chat.get("started")).strftime("%Y-%m-%d")
    ]
    chats = [Chats(chat) for chat in chats]

    # return JsonResponse(chats, safe=False)
    if request.is_ajax():
        return JsonResponse(chats, safe=False)
    return render(
        request,
        "results/chats.html",
        {
            "object_list": chats,
            "heatmap_chats": heatmap_chats,
            "username": username,
            "current_year": "Yesterday",
            "total_chats": len(chats),
        },
    )

def get_chats_from_yesterday_from_mentees(request, *args, **kwargs):
    client = Client()
    today = datetime.today()
    yesterday = today - timedelta(days=1)

    query = {
        "query": {"from": yesterday.strftime("%Y-%m-%d")},
        "sort": [{"started": "descending"}],
    }
    # chats_from_users = client.api().post('v4', '/chat/_search', json = query)
    chats_from_users, content_range = search_chats(client, query, chat_range=(0, 100))
    chats = soft_anonimyzation(chats_from_users)
    chat_picked_up_by_mentees = list()
    for chat in chats:
        if chat.get("accepted"):
            if "_int" in chat.get("operator"):
                chat_picked_up_by_mentees.append(chat)
    chats = chat_picked_up_by_mentees

    heatmap = [
        parse(chat.get("started")).replace(tzinfo=timezone.utc).timestamp()
        for chat in chats
    ]
    counter = {x: heatmap.count(x) for x in heatmap}
    heatmap_chats = json.dumps(counter)
    username = "Mentees"
    # filter chats from yesteday only
    chats = [
        chat
        for chat in chats
        if yesterday.strftime("%Y-%m-%d")
        == parse(chat.get("started")).strftime("%Y-%m-%d")
    ]
    chats = [Chats(chat) for chat in chats]

    # return JsonResponse(chats, safe=False)
    if request.is_ajax():
        return JsonResponse(chats, safe=False)
    return render(
        request,
        "results/chats.html",
        {
            "object_list": chats,
            "heatmap_chats": heatmap_chats,
            "username": username,
            "current_year": "Yesterday",
            "total_chats": len(chats),
        },
    )

def get_chats_from_yesterday_sample_size(request, *args, **kwargs):
    client = Client()
    today = datetime.today()
    yesterday = today - timedelta(days=1)

    query = {
        "query": {"from": yesterday.strftime("%Y-%m-%d")},
        "sort": [{"started": "ascending"}],
    }
    chats_from_users, content_range = search_chats(client, query, chat_range=(0, 350))
    chats_from_users = random.sample(chats_from_users, int(50 * 0.20))
    chats = soft_anonimyzation(chats_from_users)

    heatmap = [
        parse(chat.get("started")).replace(tzinfo=timezone.utc).timestamp()
        for chat in chats
    ]
    counter = {x: heatmap.count(x) for x in heatmap}
    heatmap_chats = json.dumps(counter)
    username = "Yesterday"
    # filter chats from yesteday only
    chats = [
        chat
        for chat in chats
        if yesterday.strftime("%Y-%m-%d")
        == parse(chat.get("started")).strftime("%Y-%m-%d")
    ]
    chats = [Chats(chat) for chat in chats]

    # return JsonResponse(chats, safe=False)
    if request.is_ajax():
        return JsonResponse(chats, safe=False)
    return render(
        request,
        "results/chats.html",
        {
            "object_list": chats,
            "heatmap_chats": heatmap_chats,
            "username": username,
            "current_year": "Yesterday",
            "total_chats": len(chats),
        },
    )

import os
from tempfile import gettempdir
from pathlib import Path
import pandas as pd
import pathlib
import random
from django.http import FileResponse
from shutil import rmtree

def download_get_chat_for_date_range(request, *args, **kwargs):
    filename = kwargs.get("filename", "")
    tmp_folder_name = kwargs.get("tmp_folder_name", "")
    if filename:
        tmp = os.path.join(gettempdir(), '.{}'.format(hash(int(tmp_folder_name))))
        filepath = str(pathlib.PurePath(tmp, filename))
        return FileResponse(open(filepath, "rb"), as_attachment=True, filename=filename)
    else:
        raise Http404()

def get_chat_for_date_range(request, *args, **kwargs):
    start_date = request.GET.get("start_date", "")
    end_date = request.GET.get("end_date", "")
    if start_date and end_date:
        if start_date:
            start_date_with_time = parse(start_date)

        if end_date:
            end_date_with_time = parse(end_date)

        if end_date < start_date:
            messages.warning(
                request, "The End_date should be greater than the Start Date"
            )
            return render(
                request, "results/search_between_date.html", {"object_list": None}
            )
        client = Client()
        chats = client.chats()
        to_date = (
            str(end_date_with_time.year)
            + "-"
            + "{:02d}".format(end_date_with_time.month)
            + "-"
            + str(end_date_with_time.day)
        )
        all_chats = chats.list_day(
            year=start_date_with_time.year,
            month=start_date_with_time.month,
            day=start_date_with_time.day,
            to=to_date,
        )

        #Tmp save the chats list
        df = pd.DataFrame(all_chats)
        del df['tags']
        del df['referrer']
        del df['id']
        del df['profile']
        del df['desktracker_id']
        del df['reftracker_url']
        del df['ip']
        del df['reftracker_id']
        del df['desktracker_url']
        df['school_from_operator_username'] = df['operator'].apply(lambda x: find_school_by_operator_suffix(x))
        df['school_from_queue_name'] = df['queue'].apply(lambda x: find_school_by_queue_or_profile_name(x))
        df['guest'] = df['guest'].apply(lambda x: x.split('@')[0][0:8])

        today = datetime.today().strftime("%Y-%m-%d-%H:%M")

        tmp_folder_name ='4564565464321'
        rmtree(tmp_folder_name, ignore_errors=True)
        tmp = os.path.join(gettempdir(), '.{}'.format(hash(4564565464321)))
        try:
            os.makedirs(tmp)
        except:
            pass

        filename = "list_of_chats_from_date_range_results_" + str(random.randint(1,7000))+".xlsx"
        filepath = str(pathlib.PurePath(tmp, filename))

        writer = pd.ExcelWriter(filepath, engine="xlsxwriter")
        df.to_excel(writer, index=False)
        writer.save()

        # Continue

        chats = [Chats(chat) for chat in all_chats]
        selected_chats = list()
        for chat in chats:
            if (parse(chat.started) >= start_date_with_time):
                try:
                    if (parse(chat.ended) <= end_date_with_time):
                        selected_chats.append(chat)
                except:
                    selected_chats.append(chat)
        return render(
            request, "results/search_between_date.html", {
                            "object_list": selected_chats,
                            "filename":filename,
                            "tmp_folder_name":tmp_folder_name}
        )
    else:
        messages.warning(
                request, "There should be a valid Start_Date and End_date"
            )
    return render(request, "results/search_between_date.html", {"object_list": None})

def search_chats_within_2_hours(request, *args, **kwargs):
    client = Client()
    chat_id = int(kwargs.get("chat_id", None))
    chat = client.one("chats", chat_id).get()

    if chat:
        start_date = parse(chat.get("started"))
        chats = client.chats()
        chats = chats.list_day(start_date.year, start_date.month, start_date.day)

        chat_within_2_hours = list()
        for chat in chats:
            started = parse(chat.get("started"))
            # print("{0} > {1} < {2}".format(started-timedelta(minutes=60), start_date , started+timedelta(minutes=60)))
            if started - timedelta(60) > start_date < started + timedelta(60):
                chat_within_2_hours.append(chats)

        # print(chat_within_2_hours)
        chats = None
        if chat_within_2_hours:
            chats = soft_anonimyzation(chat_within_2_hours)

        return JsonResponse(chats, safe=False)
    return JsonResponse(None, safe=False)

@csrf_exempt
def search_chats_with_this_guestID(request, *args, **kwargs):
    guest_id = request.POST.get("guest_id", None)
    chats = None
    if guest_id:
        if "@" in guest_id:
            pass
        else:
            guest_id = guest_id + "*"
        query = {
            "query": {
                "guest": [guest_id],
            },
            "sort": [{"started": "descending"}],
        }
        client = Client()
        chats = client.api().post("v4", "/chat/_search", json=query)
        chats = soft_anonimyzation(chats)
        chats = [Chats(chat) for chat in chats]
        if request.is_ajax():
            return JsonResponse(
                {"object_list": chats, "guest_id": guest_id}
            )
        return render(
            request,
            "results/search_guest.html",
            {"object_list": chats, "guest_id": guest_id},
        )
    if request.is_ajax():
        return JsonResponse(
            {"object_list": None}
        )
    return render(request, "results/search_guest.html", {"object_list": None})

class SearchGuestResultsView(TemplateView):
    template_name = "results/search_guest.html"


import numba
import numpy as np
from math import exp
from sklearn.linear_model import SGDClassifier
from sklearn.datasets.samples_generator import make_blobs
import time

np.random.seed(1111)

(X, y) = make_blobs(n_samples=500, n_features=2, centers=2, cluster_std=2.5)
X = np.c_[np.ones((X.shape[0])), X]
Xl = X.tolist()
yl = y.tolist()
w = np.random.uniform(size=(X.shape[1],))
wl = w.tolist()

def sgd_sklearn(X, y):
    clf = SGDClassifier(loss="log", fit_intercept=False, n_iter=15,
                        shuffle=False, learning_rate='constant', eta0=0.3,
                        penalty='None')
    clf.fit(X, y)
    return clf.coef_

@numba.jit
def sigmoid(r, w):
	x = 0
	for i in range(len(r)):
		x += w[i] * r[i]
	return 1.0 / (1.0 + exp(-x))

@numba.jit
def sgd_numba(X, y, w, epochs=15, eta=0.3):
    for epoch in range(epochs):
    	for i, r in enumerate(X):
            yhat = sigmoid(r, w)
            error = yhat - y[i]
            for j in range(len(r)):
                w[j] -= eta * r[j] * error
    return w

cc = sgd_numba(Xl, yl, wl)

@numba.jit
def sgd_nbnp(X, y, w, epochs=15, eta=0.3):
    for epoch in range(epochs):
    	for i in range(X.shape[0]):
    		w -= eta * X[i].T.dot((1.0 / (1.0 + np.exp(-X[i].dot(w)))) - y[i])
    return w

cc = sgd_nbnp(X, y, w)

def sgd(X, y, w, epochs=15, eta=0.3):
    for epoch in range(epochs):
    	for i in range(X.shape[0]):
    		w -= eta * X[i].T.dot((1.0 / (1.0 + np.exp(-X[i].dot(w)))) - y[i])
    return w

def test_python(benchmark):
    benchmark(sgd, X, y, w)

def test_sklearn(benchmark):
    benchmark(sgd_sklearn, X, y)

def test_numba(benchmark):
    benchmark(sgd_numba, Xl, yl, wl)

def test_nbnp(benchmark):
    benchmark(sgd_nbnp, X, y, w)


import json
import re
import pprint
import os
import argparse
import pandas as pd
import random

import numpy as np
from tqdm import tqdm

SQUAD_TEST_HEADS = ['where were', 'what political', 'what religion', 'why did', 'what type', 'what language', 'who had', 'what percentage', 'what can', 'how much']

def strip(sent):
    return sent.strip(" ").rstrip('.').rstrip('?').rstrip('!').rstrip('"')

blacklist = ["of the", "is a", "is the", "did the"]

wh = {
    "(what|what's)": 0,
    "(who|who's)": 0,
    "where": 0,
    "when": 0,
    "which": 0,
    "whose": 0,
    "whom": 0,
    "how": 0,
    "why": 0,
    "(can|could|may|might|should)": 0,
    "(is|are|were|was)": 0,
    "(will|would)": 0,
    "(do|does|did)": 0,
    "(has|have|had)": 0,
    "(name|identify|describe|define)": 0
}

wh2 = {}
wh3 = {}

keys = wh.keys()
isascii = lambda s: len(s) == len(s.encode())

def find_match(query, keys):
    for key in keys:
        if re.search('^' + key + '$', query):
            return key
    return None

def dict_add(entry, example, dict):
    if entry in blacklist:
        return
    if entry in dict:
        dict[entry].append(example)
    else:
        dict[entry] = [example]

def find_top_q_head(examples, topn):
    for example in examples:
        question_text = strip(example["question"])

        # simple tokenization
        t = question_text.split(" ")
        t = [strip(item.lower()) for item in t]

        # search if the any key is in the first three words
        flag = False
        for i in range(3):
            if i >= len(t):
                break
            key = find_match(t[i], keys)
            if key:
                wh[key] += 1
                try:
                    if key == "which" and "in which" in question_text:
                        st2 = " ".join(t[i - 1:i + 1])
                        st3 = " ".join(t[i - 1:i + 2])
                    elif key == "whom" and "by whom" in question_text:
                        st2 = " ".join(t[i - 1:i + 1])
                        st3 = " ".join(t[i - 1:i + 2])
                    else:
                        st2 = " ".join(t[i:i + 2])
                        st3 = " ".join(t[i:i + 3])
                    dict_add(st2, example, wh2)
                    # dict_add(st3, example, wh3)
                except Exception as e:
                    print(e.args)
                flag = True
                break

        if not flag:
            for i in range(len(t)):
                key = find_match(t[len(t) - i - 1], keys)
                if key:
                    wh[key] += 1
                    flag = True
                    idx = len(t) - i - 1
                    try:
                        if key == "which" and "in which" in question_text:
                            st2 = " ".join(t[i - 1:i + 1])
                            st3 = " ".join(t[i - 1:i + 2])
                        elif key == "whom" and "by whom" in question_text:
                            st2 = " ".join(t[i - 1:i + 1])
                            st3 = " ".join(t[i - 1:i + 2])
                        else:
                            st2 = " ".join(t[i:i + 2])
                            st3 = " ".join(t[i:i + 3])
                        dict_add(st2, example, wh2)
                        # dict_add(st3, wh3)
                    except Exception as e:
                        print(e.args)
                    break
        # if not flag:
            # print("No question word found: ", question_text)

    sorted_x = sorted(wh2.items(), key=lambda kv: len(kv[1]), reverse=True)
    print('#Question Head:', len(sorted_x))
    # for i in range(topn):
    #     print(sorted_x[i][0], len(sorted_x[i][1]))
    # pp = pprint.PrettyPrinter(indent=4)
    # print(sorted_x[:topn])
    # print('#Hits in Top {}:'.format(topn), sum(item[1] for item in sorted_x[:40]))
    # print('#Examples', len(examples))
    # return [kv[0] for kv in sorted_x[:topn]]
    return sorted_x[:topn]

def get_questions(examples, head, num):
    random.shuffle(examples)
    ret = []
    count = 0
    for example in examples:
        if head in example.question_text.lower() and len(example.orig_answer_text) > 0 \
                and isascii(example.orig_answer_text) and isascii(" ".join(example.doc_tokens)):
            ret.append(example)
            count += 1
            if count == num:
                break
    if count != num:
        print(head)
        print(ret)
    return ret

def read_nq_examples(input_file):
    with open(input_file, 'r') as fin:
        lines = fin.readlines()
        lines = lines[1:] # exclude the header
        source = [json.loads(line.strip()) for line in lines]

    total = 0
    examples = []
    for para in tqdm(source):
        context = para["context"]
        for qa in para["qas"]:
            total += 1
            ques = qa["question"]
            ans = qa["answers"]
            examples.append({'context': context, 'question': ques, 'answer': [{'text': ans[0]}]})

    print(examples[:5])
    return examples

def down_sample_and_split(heads, n_per_head):
    random.shuffle(heads)
    new_heads = {}

    for head in heads:
        if len(head[1]) < n_per_head:
            continue
        new_heads[head[0]] = random.sample(head[1], n_per_head)

    print(new_heads.keys())

    test = {head: new_heads[head] for head in SQUAD_TEST_HEADS if head in new_heads}

    return test

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--in_file", default='../data/newsqa/NewsQA.jsonl', type=str, required=False)
    parser.add_argument("--out_dir", default='../data/newsqa/', type=str, required=False,
                        help="Output directory")
    parser.add_argument("--out_train", default='zs_train.json', type=str, required=False)
    parser.add_argument("--out_dev", default='zs_dev.json', type=str, required=False)
    parser.add_argument("--out_test", default='zs_test.json', type=str, required=False)
    parser.add_argument('--seed', type=int, default=55, help="random seed")

    args = parser.parse_args()
    opt = vars(args)

    random.seed(opt['seed'])

    examples = read_nq_examples(opt['in_file'])
    top_heads = find_top_q_head(examples, topn=300)
    test = down_sample_and_split(top_heads, n_per_head=64)

    print('Test heads: {}'.format(test.keys()))

    # if not os.path.exists(opt['out_dir']):
    #     os.makedirs(opt['out_dir'])
    # with open(os.path.join(opt['out_dir'], opt['out_train']), 'w') as fout:
    #     json.dump(train, fout)
    # with open(os.path.join(opt['out_dir'], opt['out_dev']), 'w') as fout:
    #     json.dump(dev, fout)
    with open(os.path.join(opt['out_dir'], opt['out_test']), 'w') as fout:
        json.dump(test, fout)

if __name__ == "__main__":
    main()


# Finds in-degree distribution, out-degree distribution,
#    and reciprocal-degree distribution.
#    SNAP folds reciprocal degrees into their in/out degree calculations
#		which is less than ideal for this project
# first argument is input file name
# second argument is output file base name
# ONLY TESTED FOR PYTHON 3.3

from scipy import sparse
import numpy
import sys

def convertListToDistro(degreeList):
	#takes row sum list and converts it to degree distribution
	distro = {}

	for value in degreeList:
		try:
			distro[value] += 1
		except:
			distro[value] = 1

	return distro

def outputToFile(distroDict, outputFile):
	for key in sorted(distroDict):
		temp = str(int(key))+' '+str(distroDict[key])+'\n'
		outputFile.write(temp)

#Read the edge list passed as first parameter created for networks
File = open(sys.argv[1],"r")

#fix nodes and edges for graph being read in
NODES = 81306
EDGES = 1768149

#declare sparse matrices
inDeg = sparse.lil_matrix((NODES+1,NODES+1))
outDeg = sparse.lil_matrix((NODES+1,NODES+1))
recipDeg = sparse.lil_matrix((NODES+1,NODES+1))

for line in File:
	#split line
	temp = line.split(' ')
	edge1 = int(temp[0]) #follower
	edge2 = int(temp[1]) #followed

	#update in degree matrix
	inDeg[edge2,edge1] = 1

	#update out degree matrix
	outDeg[edge1, edge2] = 1

	#update reciprocal degree matrix if reciprocal degree exists
	if (inDeg[edge1,edge2] == 1):
		recipDeg[edge1,edge2] = 1
		recipDeg[edge2,edge1] = 1

	#ensure we are not double counting for all reciprocal edges
	if (recipDeg[edge1,edge2] == 1):
		inDeg[edge1,edge2] = 0
		outDeg[edge2,edge1] = 0
		inDeg[edge2,edge1] = 0
		outDeg[edge1, edge2] = 0

#close file
File.close()

#get row sums of each matrix into lists
inDegList = numpy.array(inDeg.sum(1)).reshape(-1,).tolist()
outDegList = numpy.array(outDeg.sum(1)).reshape(-1,).tolist()
recipDegList = numpy.array(recipDeg.sum(1)).reshape(-1,).tolist()

#convert lists to proper degree distributions
inDegDistro = convertListToDistro(inDegList)
outDegDistro = convertListToDistro(outDegList)
recipDegDistro = convertListToDistro(recipDegList)

#output degree distributions to three files
inDegFile = open('indeg_'+sys.argv[2], 'w')
outDegFile = open('outdeg_'+sys.argv[2], 'w')
recipDegFile = open('recipdeg_'+sys.argv[2], 'w')

outputToFile(inDegDistro,inDegFile)
inDegFile.close()

outputToFile(outDegDistro,outDegFile)
outDegFile.close()

outputToFile(recipDegDistro,recipDegFile)
recipDegFile.close()


from __future__ import print_function
import datetime
import pickle
import os.path
import Utils.dateFunctions
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request

SCOPES = ['https://www.googleapis.com/auth/calendar']

class GoogleCalAPI():
    def __init__(self):
        creds = None
        self.calendarId = None
        if os.path.exists('../token.pickle'):
            with open('../token.pickle', 'rb') as token:
                creds = pickle.load(token)
        # If there are no (valid) credentials available, let the user log in.
        if not creds or not creds.valid:
            if creds and creds.expired and creds.refresh_token:
                creds.refresh(Request())
            else:
                flow = InstalledAppFlow.from_client_secrets_file('Credentials/credentials.json', SCOPES)
                creds = flow.run_local_server(port=0)
            # Save the credentials for the next run
            with open('../token.pickle', 'wb') as token:
                pickle.dump(creds, token)

        self.service = build('calendar', 'v3', credentials=creds)

        self.calendar_list_entry = self.service.calendarList().get(calendarId='primary').execute()

        if not self.calendar_list_entry['accessRole']:
            print('API can\' connect!')

    def get_calendar_list_entry(self):
        return self.calendar_list_entry

    def get_my_calendars(self):
        result = self.service.calendarList().list().execute()['items'][0]
        self.calendarId = result['id']
        return result

    def get_my_events(self, date=None):
        if date is None:
            date = datetime.datetime.utcnow().isoformat() + 'Z'  # 'Z' indicates UTC time
        else:
            date = Utils.dateFunctions.getDateFromString(date)

        events_result = self.service.events().list(calendarId='primary', timeMin=date, maxResults=10, singleEvents=True,
                                                   orderBy='startTime').execute()

        events = events_result.get('items', [])
        if not events:
            return 'No upcoming events found.'
        for event in events:
            start = event['start'].get('dateTime', event['start'].get('date'))
            return {start, event['summary']}

    def create_event(self, start_time_str, summary, duration=1, unit='days', attendees=None,
                     description=None, location=None):

        interval = Utils.dateFunctions.getDateInterval(start_time_str, duration, unit)

        emails = []
        if attendees is not None & isinstance(attendees, list):
            for email in attendees:
                emails.append({'email': email})

        event = {
            'summary': summary,
            'location': location,
            'description': description,
            'start': {
                'dateTime': interval['start_time'],
                'timeZone': Utils.dateFunctions.getSystemTimeZone(),
            },
            'end': {
                'dateTime': interval['end_time'],
                'timeZone': Utils.dateFunctions.getSystemTimeZone(),
            },
            'recurrence': [
                'RRULE:FREQ=DAILY;COUNT=2'
            ],
            'attendees': emails,
            'reminders': {
                'useDefault': False,
                'overrides': [
                    {'method': 'email', 'minutes': 24 * 60},
                    {'method': 'popup', 'minutes': 10},
                ],
            },
        }

        event = self.service.events().insert(calendarId='primary', body=event).execute()
        print('Event created: %s' % (event.get('htmlLink')))


# -*- coding: utf-8 -*-
"""
Created on Thu May 13 12:31:50 2021

@author: vazqu
"""
import nltk
import pandas as pd
import string
from unidecode import unidecode
import re
from nltk.tokenize import word_tokenize, sent_tokenize
from nltk.corpus import stopwords
from nltk.tokenize import RegexpTokenizer
from nltk.stem.snowball import SnowballStemmer
import spacy

class Procesamiento():
    def __init__(self, comentarios):
        self.comentarios= comentarios

    def tokenizar(self, texto):
        texto = texto.lower()
        tokenizer = RegexpTokenizer(r'\w+')
        #token= nltk.tokenize.word_tokenize(texto)
        return tokenizer.tokenize(texto)

    def remover_url(self,text):
        url = re.compile(r'https?://\S+|www\.\S+')
        return url.sub(r'',text)

    def remover_caracteres(self,texto):
        #llega formato [['texto']]
        #print(texto)

        texto = re.sub("[#$?,:;'¡!@#$%^&*()\[\]\{\}/<´>|`+=_-]1234567890", '', texto)
        texto = re.sub("\d+", '', texto)
        texto = re.sub("[àáâãäå]", 'a', texto)
        texto = re.sub("[èéêë]", 'e', texto)
        texto = re.sub("[ìíîï]", 'i', texto)
        texto = re.sub("[òóôõö]", 'o', texto)
        texto = re.sub("[ùúûü]", 'u', texto)
        texto=unidecode(texto)

        texto= re.sub(r'\^[a-zA-Z]\s+', ' ', texto) #eliminar caracteres simples
        return texto

    def remover_stop_words(self,textoTokenizado):
        stop_words = set(stopwords.words('spanish'))
        return [word for word in textoTokenizado if word not in stop_words]

    def stemming(self, token):
        stemmer=SnowballStemmer("spanish",ignore_stopwords=False)
        datos=[]
        for i in token:
            d=[]
            for word in i:
                d.append(stemmer.stem(word))
            datos.append(d)
        return datos

    def lemantizar(self,datos):
        #python -m spacy download en_core_web_sm
        sp = spacy.load('es_core_news_md')
        lemantizado=[]
        for i in datos:
            doc=[]
            for j in i:
                for word in sp(j):
                  doc.append(word.lemma_)
            lemantizado.append(doc)
        return lemantizado

    def limpieza(self):
        self.comentarios=[self.remover_url(texto) for texto in self.comentarios]
        self.comentarios = [self.remover_caracteres(texto)for texto in self.comentarios]
        token=[self.tokenizar(i)for i in self.comentarios]
        stop=[self.remover_stop_words(i) for i in token]
        stem=self.stemming(stop)
        leman=self.lemantizar(stop)

        return token,stem, leman


from django.shortcuts import render
from django.views.generic import TemplateView
from .forms import ImageForm
from .main import detect

class MyDetectorView(TemplateView):
  # 생성자
  def __init__(self):
    self.params = {'result_list':[],
                   'result_name':"",
                   'result_img':"",
                   'form': ImageForm()}

  # GET request (index.html 파일 초기 표시)
  def get(self, req):
    return render(req, 'mydetector/index.html', self.params)

  # POST request (index.html 파일에 결과 표시)
  def post(self, req):
    # POST 메소드에 의해서 전달되는 FORM DATA
    print("post 시작")
    form = ImageForm(req.POST, req.FILES)
    # FORM DATA 에러 체크
    print("Valueform은 실행되는가")
    if not form.is_valid():
        raise ValueForm('invalid form')
    print("valueform 후")
    # FORM DATA에서 이미지 파일 얻기
    image = form.cleaned_data['image']
    # 이미지 파일을 지정해서 얼굴 인식
    result = detect(image)

    # 얼굴 분류된 결과 저장
    self.params['result_list'], self.params['result_name'], self.params['result_img'] = result

    # 페이지에 화면 표시
    return render(req, 'mydetector/index.html', self.params)