import binascii
from iroha import IrohaCrypto as ic
from iroha import Iroha, IrohaGrpc
from iroha.primitive_pb2 import can_set_my_account_detail
import sys
import json
import os

iroha = Iroha('admin@test')
net = IrohaGrpc()
admin_private_key = open('./configs/admin@test.priv').read()

def send_transaction_and_print_status(transaction):
    global net
    hex_hash = binascii.hexlify(ic.hash(transaction))
    print('Transaction hash = {}, creator = {}'.format(
        hex_hash, transaction.payload.reduced_payload.creator_account_id))
    net.send_tx(transaction)
    for status in net.tx_status_stream(transaction):
        print(status)

def create_domain():
    return

def add_peer_node():
    return

def generate_kp():
    global iroha
    pk = ic.private_key()
    user_private_key = pk
    user_public_key = ic.derive_public_key(user_private_key)
    return user_private_key, user_public_key

def create_users(user_name,domain,ple_id,pwd_hash):
    global iroha
    """
    register new user, grant permission to admin and set password & plenteum address
    """
    user_private_key, user_public_key = generate_kp()
    init_cmds = [
        iroha.command('CreateAccount', account_name=user_name, domain_id=domain,
                      public_key=user_public_key)
    ]
    init_tx = iroha.transaction(init_cmds)
    ic.sign_transaction(init_tx, admin_private_key)
    send_transaction_and_print_status(init_tx)
    account_id = user_name + '@' + domain
    grant_permission = iroha.transaction([
        iroha.command('GrantPermission', account_id='admin@test', permission=can_set_my_account_detail)
    ], creator_account=account_id)
    ic.sign_transaction(grant_permission, user_private_key)
    send_transaction_and_print_status(grant_permission)
    account_details = iroha.transaction([
        iroha.command('SetAccountDetail',
                      account_id=account_id, key='password', value=pwd_hash),
        iroha.command('SetAccountDetail',
                      account_id=account_id, key='ple_id', value=ple_id)], creator_account='admin@test')
    ic.sign_transaction(account_details, admin_private_key)
    send_transaction_and_print_status(account_details)
    user_pvt_file = './configs/' + account_id +'.priv'
    user_pub_file = './configs/' + account_id +'.pub'
    user_private_key_file = open(user_pvt_file,'wb+').write(user_public_key)
    user_public_key_file = open(user_pub_file,'wb+').write(user_private_key)
    return user_private_key, user_public_key

def create_domain_asset_manager(domain,ple_id):
    global iroha
    """
    register new domain asset manager and set password & plenteum address
    """
    user_private_key, user_public_key = generate_kp()
    init_cmds = [
        iroha.command('CreateAccount', account_name="asset_manager", domain_id=domain,
                      public_key=user_public_key)
    ]
    init_tx = iroha.transaction(init_cmds)
    ic.sign_transaction(init_tx, admin_private_key)
    send_transaction_and_print_status(init_tx)
    account_id = 'asset_manager@' + domain
    grant_permission = iroha.transaction([
        iroha.command('GrantPermission', account_id='admin@test', permission=can_set_my_account_detail)
    ], creator_account=account_id)
    ic.sign_transaction(grant_permission, user_private_key)
    send_transaction_and_print_status(grant_permission)
    account_details = iroha.transaction([
        iroha.command('SetAccountDetail',
                      account_id=account_id, key='ple_id', value=ple_id)], creator_account='admin@test')
    ic.sign_transaction(account_details, admin_private_key)
    send_transaction_and_print_status(account_details)
    return user_private_key, user_public_key

def add_asset_to_admin(asset_id, qty):
    global iroha
    """
    Add asset supply and assign to 'admin@test'
    """
    tx = iroha.transaction([
        iroha.command('AddAssetQuantity',
                      asset_id=asset_id, amount=qty)
    ])
    ic.sign_transaction(tx, admin_private_key)
    send_transaction_and_print_status(tx)

def add_asset_to_user(account_id, asset_id, qty):
    global iroha
    """
    Add asset supply and assign to 'creator'
    """
    tx = iroha.transaction([
        iroha.command('AddAssetQuantity',
                      asset_id=asset_id, amount=qty)
    ])
    ic.sign_transaction(tx, admin_private_key)
    send_transaction_and_print_status(tx)

def create_and_issue_new_asset(asset,domain,precision,qty,account_id,description):
    global iroha
    user_tx = iroha.transaction(
        [iroha.command('CreateAsset', asset_name=asset,
            domain_id=domain, precision=precision)]    )
    ic.sign_transaction(user_tx, admin_private_key)
    send_transaction_and_print_status(user_tx)
    asset_id = asset + '#' + domain
    add_asset_to_admin(asset_id=asset_id,qty=qty)
    transfer_asset('admin@test',account_id,asset_id,description,domain,qty)

def join_plenteum_asset_ledger():
    """
    Creates Plenteum Domain 'domain' and asset 'coin#domain' with precision 2 and joins global network
    """
    commands = [
        iroha.command('CreateDomain', domain_id='domain', default_role='user'),
        iroha.command('CreateAsset', asset_name='coin',
                      domain_id='domain', precision=2)
    ]
    tx = ic.sign_transaction(
        iroha.transaction(commands), admin_private_key)
    send_transaction_and_print_status(tx)

def transfer_asset(owner,recepient,asset_id,description,domain,qty):
    global iroha
    user_tx = iroha.transaction([
        iroha.command('TransferAsset', src_account_id=owner, dest_account_id=recepient,
                      asset_id=asset_id, description=description, amount=qty)])
    ic.sign_transaction(user_tx, admin_private_key)
    send_transaction_and_print_status(user_tx)

def get_blocks():
    """
    Subscribe to blocks stream from the network
    :return:
    """
    query = iroha.blocks_query()
    ic.sign_query(query, admin_private_key)
    for block in net.send_blocks_stream_query(query):
        print('The next block arrived:', block)

def set_account_detail(account_id,key,value):
    """
    Set age to user@domain by admin@test
    """
    tx = iroha.transaction([
        iroha.command('SetAccountDetail',
                      account_id=account_id, key=key, value=value)
    ])
    ic.sign_transaction(tx, admin_private_key)
    send_transaction_and_print_status(tx)

def get_asset_info(asset_id):
    """
    Get asset info
    :return:
    """
    query = iroha.query('GetAssetInfo', asset_id=asset_id)
    ic.sign_query(query, admin_private_key)
    response = net.send_query(query)
    data = response.asset_response.asset
    print('Asset id = {}, precision = {}'.format(data.asset_id, data.precision))

def get_account_assets():
    """
    List all the assets of user@domain
    """
    query = iroha.query('GetAccountAssets', account_id='admin@test')
    ic.sign_query(query, admin_private_key)

    response = net.send_query(query)
    data = response.account_assets_response.account_assets
    for asset in data:
        print('Asset id = {}, balance = {}'.format(
            asset.asset_id, asset.balance))
    return data

def get_domain_assets():
    """
    List all the assets of user@domain
    """
    query = iroha.query('GetAccountAssets', account_id='admin@test')
    ic.sign_query(query, admin_private_key)
    response = net.send_query(query)
    data = response.account_assets_response.account_assets
    for asset in data:
        print('Asset id = {}, balance = {}'.format(
            asset.asset_id, asset.balance))
    return data

def get_user_details(account_id):
    """
    Get all the kv-storage entries for user@domain
    """
    query = iroha.query('GetAccountDetail', account_id=account_id)
    ic.sign_query(query, admin_private_key)

    response = net.send_query(query)
    data = response.account_detail_response
    user = json.loads(str(data.detail))
    print('Account id = {}, details = {}'.format(account_id, data.detail))
    return user

def get_user_password(account_id):
    global iroha
    """
    Get all the kv-storage entries for user@domain
    """
    query = iroha.query('GetAccountDetail', account_id=account_id)
    ic.sign_query(query, admin_private_key)
    response = net.send_query(query)
    data = response.account_detail_response
    user = json.loads(str(data.detail))
    pwd_hash = user['admin@test']['password']
    return pwd_hash


from rpy2 import robjects as ro
import regex
import sys

"""
This method is a variation of Bissection Method. Differently from the original
Bissection Method (which only considers signal(f(a)*f(b) each iteration),
the Regula Falsi (or False Position)  does consider the values of f(a) and f(b)
when calculating the f((a+b)/2), i.e, it calculates f((a*f(a) + b*f(b))/(f(a) + (f(b))
which is, of course, an weighted mean between a and b.

The motivation behind this is the assumption that, in f function, continuous in [a,b] interval,
if exists a z real that f(z) = 0, then z is closer to |f(m)| than |f(n)| if and only if
|f(m)| < |f(n)|, for all m, n in [a, b].
"""

def bissection(function, a, b, n=100, var='x'):
	subVarRe = regex.compile(r'\b' + var + r'\b')
	for i in range(n):
		fa = ro.r(subVarRe.sub('('+str(a)+')', function))[0]
		fb = ro.r(subVarRe.sub('('+str(b)+')', function))[0]
		if fb != fa:
			curXMean = (a*fb - b*fa)/(fb - fa)
			curImgVal = ro.r(subVarRe.sub('('+str(curXMean)+')', function))[0]
			if curImgVal * fa < 0:
				b = curXMean
			else:
				a = curXMean
		else:
			break;

	return curXMean

if __name__ == '__main__':
	if len(sys.argv) < 4:
		print('usage:', sys.argv[0], '<function> <a> <b> (interval is [a, b]) <# of iterations? (optional)>')
		exit(1)
	function = sys.argv[1]
	try:
		a = float(sys.argv[2])
		b = float(sys.argv[3])
	except:
		print('Error: \'a\' and \'b\' args must be a real number.')
		exit(2)
	try:
		n = 100
		if len(sys.argv) == 5:
			n = int(sys.argv[4])
		if n <= 0:
			raise Exception()
	except:
		print('Error: number of iterations must be a positive natural value.')
		exit(3)

	root = bissection(function, a, b, n)
	print('root (z):', root, '\tAbsError:', abs(ro.r(regex.sub(r'\bx\b', '('+str(root)+')', function))[0]))


import sys
import curses

import gym
import numpy as np

STAY = np.zeros(2, dtype=np.int)
NORTH = np.array([0, -1])
EAST = np.array([1, 0])
SOUTH = np.array([0, 1])
WEST = np.array([-1, 0])

ACTIONS = ['stay', 'north', 'east', 'south', 'west']
ACTIONS_MAP = dict(enumerate(ACTIONS))
ACTIONS_RENDER_MAP = {
    0: ord('*'),
    1: ord('^'),
    2: ord('>'),
    3: ord('v'),
    4: ord('<'),
}

MAP_SIZE = np.array([10, 10])
MAX_STEPS = 50

def distance(p1, p2):
    return np.sqrt(np.sum((p2 - p1) ** 2))

ZERO = np.array([0, 0])
MAX_DISTANCE = distance(ZERO, MAP_SIZE-1)

class State:
    def __init__(self, map_size=MAP_SIZE):
        self.steps = self.last_action = self.last_reward = 0
        width, height = self.map_size = map_size

        self.map = self._generate_map()

        self._map_win_width, self._map_win_height = self.map_size + 2
        self._map_win_xlim, self._map_win_ylim = self.map_size + 1

        inner_width = self._map_win_width - 2
        self._map_win_horizontal_border = '+' + ('-' * inner_width) + '+'
        self._map_win_horizontal_filler = '|' + ('.' * inner_width) + '|'

        lowest_dim = width if width < height else height
        self.target_position = np.random.randint(0, lowest_dim, 2)
        self.player_position = np.random.randint(0, lowest_dim, 2)
        self._update_positions()

    def _update_positions(self):
        self._tx, self._ty = self.target_position + 1
        self._px, self._py = self.player_position + 1

    def _generate_map(self):
        gmap = np.zeros(self.map_size)
        return gmap

    def move_player(self, direction_name):
        direction = getattr(sys.modules[__name__], direction_name)
        self.player_position += direction

    def is_player_on_target(self):
        return np.array_equal(self.player_position, self.target_position)

    def render(self):
        self._update_positions()

        map_win = self._render_map()
        self._render_player(map_win)
        self._render_target(map_win)

        return map_win

    def _render_map(self):
        map_win = curses.newwin(self._map_win_height,
                                self._map_win_width+1, 1, 0)
        for y in range(0, self._map_win_height):
            s = self._map_win_horizontal_filler
            if y == 0 or y == self._map_win_ylim:
                s = self._map_win_horizontal_border
            map_win.addstr(y, 0, s)

        return map_win

    def _is_player_within_window(self):
        return (self._px >= 0 and self._py >= 0 and
                self._px <= self._map_win_xlim and self._py <= self._map_win_ylim)

    def _render_player(self, map_win):
        if self._is_player_within_window():
            map_win.addch(self._py, self._px,
                          ACTIONS_RENDER_MAP[self.last_action])

    def _render_target(self, map_win):
        ch = ord('O')
        if self.is_player_on_target():
            ch = ord('X')
        map_win.addch(self._ty, self._tx, ch)

class SimpleEnv(gym.Env):

    def __init__(self):
        super().__init__()
        self.action_space = gym.spaces.Discrete(len(ACTIONS))
        self.last_distance = 0

    def step(self, action):
        if action not in ACTIONS_MAP:
            raise AssertionError('invalid action')

        self.state.steps += 1
        self.state.last_action = action

        action_name = ACTIONS_MAP[action]
        self.state.move_player(action_name.upper())

        reward = 0.
        if action_name != 'stay':
            new_distance = distance(
                self.state.player_position, self.state.target_position)
            delta = self.last_distance - new_distance
            if delta < 0 and np.abs(delta) < 1:
                reward -= 0.25
            elif delta < 0:
                reward -= 0.5
            elif delta > 0 and delta < 1:
                reward += 0.25
            else:
                reward += 0.5
            self.last_distance = new_distance

        too_long = self.state.steps > MAX_STEPS
        if too_long:
            reward -= 1.
        # too_long = False

        on_target = self.state.is_player_on_target()
        if on_target:
            reward += 0.5

        mw, mh = self.state.map_size
        px, py = self.state.player_position
        out_of_map = px < 0 or px >= mw or py < 0 or py >= mh
        if out_of_map:
            reward -= 0.5

        done = out_of_map or on_target or too_long

        self.state.last_reward = reward

        return self.state, reward, done, {}

    def reset(self):
        self.state = State()
        self.last_distance = distance(
            self.state.player_position, self.state.target_position)
        return self.state

    def render(self, scr, mode='human'):
        scr.clear()
        scr.addstr(
            'Step: #{step:03d} / Last reward: {reward:+01.2f} / Last distance: {distance:01.2f}'.format(
                step=self.state.steps,
                reward=self.state.last_reward,
                distance=self.last_distance))
        scr.refresh()

        map_win = self.state.render()
        map_win.refresh()

    def close(self):
        pass

gym.register('SimpleEnv-v0', entry_point=SimpleEnv)


'''
Python implementation of the EIGA algorithm.

@author Aaron Zampaglione <azampagl@azapagl.com>
@copyright MIT
'''
from Bio.PDB.PDBParser import PDBParser
from scipy.spatial.distance import cdist

class Eiga(object):
    """
    EIGA.
    """

    # Distance matrix
    d = {}

    def __init__(self, structures):
        """
        Initializes the EIGA algorithm.

        Key arguments:
        structures -- structure tuple that contains the structure name and the .ent
                      file location.
        """
        for structure,file_name in structures:
            # Find the atomic coordinates for this structure.
            coords = self.parse_pdb(structure, file_name)
            # Calculate the distance matrices.
            self.d[structure] = cdist(coords, coords)

    def parse_pdb(self, structure, file_name):
        """
        Parses a pdb-format file and returns a list of coordinates.

        Key arguments:
        structure -- the structure's id
        file_name -- the location of the .ent file.
        """
        # Build parse object.
        parser = PDBParser()

        structure = parser.get_structure(structure, file_name)

        # Unsure how to process multiple models.
        if len(structure.get_list()) > 1:
            raise

        model = structure.get_list()[0]

        # Unsure how to process multiple models.
        if len(model.get_list()) > 1:
            raise

        # The chain contains a list of residues.
        chain = model.get_list()[0]

        # Keep track of the alpha carbon coordinates.
        coords = []

        for residue in chain:
            for atom in residue:
                # We're only looking at the primary carbon atom.
                if atom.get_name() == "C":
                    coord = atom.get_coord()
                    coords.append((coord[0], coord[1], coord[2]))

        return coords


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

"""
功能：  批量解压压缩文件
时间： 2018.2.2
"""

import os, sys
import zipfile
import rarfile

open_path = os.path.abspath('../result')
save_path = os.path.abspath('./output')

os.chdir(open_path)

def Decompression(files, file_path, save_path):
    os.getcwd()
    os.chdir(file_path)
    for file in files:
        r = zipfile.is_zipfile(file)
        if r:
            zpfd = zipfile.ZipFile(file)
            # os.chdir(save_path)
            zpfd.extractall(save_path)
            zpfd.close()
        else:
            rarfd = rarfile.RarFile(file)
            rarfd.extractall(save_path)
            rarfd.close()

def files_save(open_path):
    for file_path, sub_dirs, files in os.walk(open_path):
        Decompression(files, file_path, save_path)

files_save(open_path)


from fastapi import FastAPI

description = """
ChimichangApp API helps you do awesome stuff. 🚀

## Items

You can **read items**.

## Users

You will be able to:

* **Create users** (_not implemented_).
* **Read users** (_not implemented_).
"""

app = FastAPI(
    title="ChimichangApp",
    description=description,
    version="0.0.1",
    terms_of_service="http://example.com/terms/",
    contact={
        "name": "Deadpoolio the Amazing",
        "url": "http://x-force.example.com/contact/",
        "email": "dp@x-force.example.com",
    },
    license_info={
        "name": "Apache 2.0",
        "url": "https://www.apache.org/licenses/LICENSE-2.0.html",
    },
)

@app.get("/items/")
async def read_items():
    return [{"name": "Katana"}]


import time
import numpy as np
import cv2
import json
from flask import Flask, jsonify, request
from PIL import Image
import numpy as np
import os
import tensorflow as tf

app = Flask(__name__)

IMG_SIZE = 224

def prerocess(image):
    image = tf.cast(image, tf.float32)
    image = (image/127.5) - 1
    image = tf.image.resize(image, (IMG_SIZE, IMG_SIZE))
    return image

with open('class_names.txt', 'r') as f:
    text = f.readlines()

def remove_str(text):
    text = text.replace("\n", "")
    return text
class_list = list(map(remove_str, text))

model = tf.keras.models.load_model('trained.h5')

# test

from glob import glob

file_list = glob('cropped/*/*/*.jpeg')

for file_path in file_list:
    img = cv2.imread(file_path)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    img = prerocess(img)

    prediction = model.predict(tf.expand_dims(img, 0))[0]
    scores = tf.nn.softmax(prediction)

    label = os.path.basename(os.path.dirname(file_path))
    gt = label
    name, prob = class_list[np.argmax(scores)], 100*np.max(scores)
    print(name,prob,gt)
# @app.route('/predict', methods=['POST'])
# def predict():
#     if request.method == 'POST':
#         r = request
#         # convert string of image data to uint8
#         nparr = np.fromstring(r.data, np.uint8)
#         # decode image
#         img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
#         return jsonify({'meta':detected['meta']})

# if __name__ == '__main__':
#     app.run(host='0.0.0.0', port=8891)


import datetime
import json
import os
import re

from django.test import TestCase

import scraper.besluitenlijst
import scraper.documents
import scraper.dossiers
import scraper.parliament_members
import scraper.persons

class TestDossierScraper(TestCase):
    dossier_id = '33711'

    def test_scrape_dossier_url(self):
        dossier_url = scraper.dossiers.search_dossier_url(self.dossier_id)
        self.assertEqual(dossier_url, 'https://www.tweedekamer.nl/kamerstukken/wetsvoorstellen/detail?id=2013Z16228&dossier=33711')

class TestKamervraagScraper(TestCase):
    kamervraag_url = 'https://zoek.officielebekendmakingen.nl/kv-tk-2017Z06952'

    def test_get_kamervraag_id_and_content(self):
        kamervraag_html_url = self.kamervraag_url + '.html'
        document_id, content_html, title = scraper.documents.get_kamervraag_document_id_and_content(kamervraag_html_url)
        self.assertEqual(document_id, 'kv-tk-2017Z06952')
        self.assertEqual(len(content_html), 5990)
        self.assertEqual(title, 'Vragen van het lid Bosman (VVD) aan de Minister van Binnenlandse Zaken en Koninkrijksrelaties over het bericht «Ziekenhuis Curaçao bezorgt NL strop» (ingezonden 26 mei 2017).')
        overheidnl_document_ids = scraper.documents.get_kamervraag_antwoord_ids(self.kamervraag_url)
        self.assertEqual(len(overheidnl_document_ids), 1)
        self.assertEqual(overheidnl_document_ids[0], 'ah-tk-20162017-2167')

    def test_get_related_ids(self):
        url = 'https://zoek.officielebekendmakingen.nl/kv-tk-2017Z07318'
        related_document_ids = scraper.documents.get_kamervraag_antwoord_ids(url)
        self.assertEqual(len(related_document_ids), 2)
        self.assertEqual(related_document_ids[0], 'ah-tk-20162017-2129')
        self.assertEqual(related_document_ids[1], 'ah-tk-20162017-2338')

class TestBesluitenlijstScraper(TestCase):
    filenames = [
        'data/besluitenlijsten/besluitenlijst_example1.pdf',
        'data/besluitenlijsten/besluitenlijst_example2.pdf',
        'data/besluitenlijsten/besluitenlijst_example3.pdf',
        'data/besluitenlijsten/besluitenlijst_example4.pdf',
    ]

    @classmethod
    def setUpTestData(cls):
        cls.texts = []
        for filename in cls.filenames:
            text = scraper.besluitenlijst.besluitenlijst_pdf_to_text(filename)
            cls.texts.append(text)

    def test_regex(self):
        pattern = "\d{1,3}\.\s{0,}Agendapunt:"
        text = 'BuZa6.Agendapunt:Stukken'
        result = re.findall(pattern, text)
        self.assertEqual(result[0], '6.Agendapunt:')
        text = 'BuZa6.  Agendapunt:Stukken'
        result = re.findall(pattern, text)
        self.assertEqual(result[0], '6.  Agendapunt:')

    def test_find_agendapunten(self):
        for text in self.texts:
            items = scraper.besluitenlijst.find_agendapunten(text)
            self.assertTrue(len(items) > 0)

    def test_create_besluitenlijst(self):
        for text in self.texts:
            lijst = scraper.besluitenlijst.create_besluitenlijst(text)

    def test_find_cases(self):
        for text in self.texts:
            cases = scraper.besluitenlijst.find_cases(text)

    def test_find_case_decisions(self):
        for text in self.texts:
            punten = scraper.besluitenlijst.find_agendapunten(text)
            cases = scraper.besluitenlijst.find_cases(text)
            decisions = scraper.besluitenlijst.find_decisions(text)

    def test_create_besluit_items(self):
        for text in self.texts:
            items = scraper.besluitenlijst.create_besluit_items(text)

class TestPersonInfoScraper(TestCase):

    def test_inititals(self):
        parlement_and_politiek_id = 'vg09llk9rzrp'
        initials_expected = 'F.C.G.M.'
        initials = scraper.persons.get_initials(parlement_and_politiek_id)
        self.assertEqual(initials, initials_expected)
        parlement_and_politiek_id = 'vg09lll5uqzx'
        initials_expected = 'S.A.M.'
        initials = scraper.persons.get_initials(parlement_and_politiek_id)
        self.assertEqual(initials, initials_expected)
        parlement_and_politiek_id = 'vjuuhtscjwpn'
        initials_expected = 'T.H.P.'
        initials = scraper.persons.get_initials(parlement_and_politiek_id)
        self.assertEqual(initials, initials_expected)

class TestDocumentScraper(TestCase):

    def test_document_metadata_kamerstuk_2016(self):
        document_id = 'kst-34575-2'
        metadata = scraper.documents.get_metadata(document_id)
        self.assertEqual(metadata['publication_type'], 'Kamerstuk')
        self.assertEqual(metadata['dossier_ids'], '34575')
        self.assertEqual(metadata['date_published'], '2016-10-13')
        self.assertEqual(metadata['title_short'], 'Voorstel van wet')
        self.assertEqual(metadata['title_full'], 'Wijziging van de Zorgverzekeringswet en de Wet op de zorgtoeslag in verband met enkele inhoudelijke en technische verbeteringen (Verzamelwet Zvw 2016); Voorstel van wet; Voorstel van wet')
        self.assertEqual(metadata['category'], 'Zorg en gezondheid | Ziekten en behandelingen')
        self.assertEqual(metadata['publisher'], 'Tweede Kamer der Staten-Generaal')
        self.assertEqual(metadata['submitter'], 'E.I. Schippers')
        self.assertEqual(metadata['publication_type'], 'Kamerstuk')

    def test_document_metadata_kamerstuk_2009(self):
        document_id = 'kst-32203-2'
        metadata = scraper.documents.get_metadata(document_id)
        self.assertEqual(metadata['publication_type'], 'Kamerstuk')
        self.assertEqual(metadata['dossier_ids'], '32203')
        self.assertEqual(metadata['date_published'], '2009-11-06')
        self.assertEqual(metadata['title_short'], 'officiële publicatie')
        self.assertEqual(metadata['title_full'], 'Voorstel van Wet van de leden Van der Ham, De Wit en Teeven tot wijziging van het Wetboek van Strafrecht in verband met het laten vervallen van het verbod op godslastering; Voorstel van wet')
        self.assertEqual(metadata['category'], 'Recht | Strafrecht|Recht | Staatsrecht|Cultuur en recreatie | Cultuur')
        self.assertEqual(metadata['publisher'], 'Tweede Kamer der Staten-Generaal')
        self.assertEqual(metadata['submitter'], 'Ham van der B.|Wit de J.M.A.M.|Teeven F.')
        self.assertEqual(metadata['publication_type'], 'Kamerstuk')

    def test_document_metadata_kamerstuk_multiple_dossiers(self):
        document_id = 'kst-33037-184'
        metadata = scraper.documents.get_metadata(document_id)
        self.assertEqual(metadata['publication_type'], 'Kamerstuk')
        self.assertEqual(metadata['dossier_ids'], '33037;34532')

    def test_get_document_content_2016(self):
        url = 'https://zoek.officielebekendmakingen.nl/kst-34575-2.html'
        document_id, content_html, title = scraper.documents.get_document_id_and_content(url)
        self.assertEqual(document_id, 'kst-34575-2')
        self.assertEqual(title, 'voorstel van wet')

    def test_get_document_content_2009(self):
        url = 'https://zoek.officielebekendmakingen.nl/kst-32203-2.html'
        document_id, content_html, title = scraper.documents.get_document_id_and_content(url)
        self.assertEqual(document_id, 'kst-32203-2')
        self.assertEqual(title, 'voorstel van wet')
        url = 'https://zoek.officielebekendmakingen.nl/kst-31830-6.html'
        document_id, content_html, title = scraper.documents.get_document_id_and_content(url)
        self.assertEqual(document_id, 'kst-31830-6')
        self.assertEqual(title, 'brief van de minister van financiën')


"""exam_7 URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/3.1/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from poll.views import MainView, PollView, PollCreateView, PollUpdateView, PollDeleteView, PollChoiceCreateView, ChoiceUpdateView, ChoiceDeleteView, QuestionaireView

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', MainView.as_view(), name="poll-list"),
    path('<int:pk>/', PollView.as_view(), name="poll-view"),
    path('add_poll/', PollCreateView.as_view(), name="poll-create"),
    path('<int:pk>/update_poll/', PollUpdateView.as_view(), name="poll-update"),
    path('<int:pk>/delete_poll/', PollDeleteView.as_view(), name="poll-delete"),
    path('<int:pk>/add/', PollChoiceCreateView.as_view(), name="choice-create"),
    path('<int:pk>/update/', ChoiceUpdateView.as_view(), name='choice-update'),
    path('<int:pk>/delete_choice/', ChoiceDeleteView.as_view(), name='choice-delete'),
    path('questionaire/', QuestionaireView.as_view(), name='questionaire'),
]


from collections import Counter

class Solution:
    @staticmethod
    def contains(sub_s, t_counter):
        s_counter = Counter(sub_s)
        if len(s_counter.keys()) < len(t_counter.keys()):
            return False
        for key, count in t_counter.items():
            if s_counter.get(key) is None or s_counter.get(key) < count:
                return False
        return True

    def minWindow(self, s: str, t: str) -> str:
        t_counter = Counter(t)
        left = 0
        right = 1
        min_distance = len(s) + 1
        result = ''
        while right <= len(s):
            while right < len(s) and not self.contains(s[left:right], t_counter):
                right += 1
            while self.contains(s[left:right], t_counter):
                if right - left < min_distance:
                    min_distance = right - left
                    result = s[left:right]
                    if min_distance == len(t):
                        return result
                left += 1
            right += 1
        return result

if __name__ == '__main__':
    s = 'CBANC'
    t = 'ANC'

    result = Solution().minWindow(s=s, t=t)
    print(result)
    pass


# Fill in this file with the messages code from the Webex Teams exercise
import requests

access_token = 'your_token_here'
room_id = 'your_room_id'
message = 'Hello **DevNet Associates**!'
url = 'https://webexapis.com/v1/messages'
headers = {
    'Authorization': 'Bearer {}'.format(access_token),
    'Content-Type': 'application/json'
}
params = {'roomId': room_id, 'markdown': message}
res = requests.post(url, headers=headers, json=params)
print(res.json())


"""
import pygame

pygame.init()
screen = pygame.display.set_mode((400, 300))
done = False

WHITE = ( 255, 255, 255)
RED = ( 255, 0, 0)

rows = 5;
width = 10;
block_size = 0.1;

for y in range(rows):
    for x in range(width):
        rect = pygame.Rect(x*block_size, y*block_size, block_size, block_size)
        pygame.draw.rect(screen, WHITE, rect, 1)

pygame.draw.rect(screen, WHITE, pygame.Rect(45, 60, 60, 60), 1)

while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    pygame.display.flip()
"""

import pygame

# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)

# This sets the WIDTH and HEIGHT of each grid location
WIDTH = 20
HEIGHT = 20

# This sets the margin between each cell
MARGIN = 5

# Create a 2 dimensional array. A two dimensional
# array is simply a list of lists.
grid = []
#for row in range(10):
for row in range(20):
    # Add an empty array that will hold each cell
    # in this row
    grid.append([])
    for column in range(10):
    #for column in range(20):
        grid[row].append(0)  # Append a cell

# Set row 1, cell 5 to one. (Remember rows and
# column numbers start at zero.)
grid[0][5] = 1

# Initialize pygame
pygame.init()

# Set the WIDTH and HEIGHT of the screen
# WINDOW_SIZE = [255, 255]
WINDOW_SIZE = [255 , 510]
screen = pygame.display.set_mode(WINDOW_SIZE)

# Set title of screen
pygame.display.set_caption("Array Backed Grid")

# Loop until the user clicks the close button.
done = False

# Used to manage how fast the screen updates
clock = pygame.time.Clock()

# -------- Main Program Loop -----------
while not done:
    for event in pygame.event.get():  # User did something
        if event.type == pygame.QUIT:  # If user clicked close
            done = True  # Flag that we are done so we exit this loop
        elif event.type == pygame.MOUSEBUTTONDOWN:
            # User clicks the mouse. Get the position
            pos = pygame.mouse.get_pos()
            # Change the x/y screen coordinates to grid coordinates
            column = pos[0] // (WIDTH + MARGIN)
            row = pos[1] // (HEIGHT + MARGIN)
            # Set that location to one
            grid[row][column] = 1
            print("Click ", pos, "Grid coordinates: ", row, column)

    # Set the screen background
    screen.fill(BLACK)

    # Draw the grid
    for row in range(20):
    #for row in range(20):
        for column in range(10):
        #for column in range(20):
            color = WHITE
            if grid[row][column] == 1:
                color = RED
            pygame.draw.rect(screen,
                             color,
                             [(MARGIN + WIDTH) * column + MARGIN,
                              (MARGIN + HEIGHT) * row + MARGIN,
                              WIDTH,
                              HEIGHT])

    # Limit to 60 frames per second
    clock.tick(60)

# win = pygame.display
# surface = win.set_mode([400,400])
# loadimage = pygame.image.load('Car.jpeg')
# window = True

# while window:
#     for event in pygame.event.get():
#         if event.type == pygame.QUIT:
#             window = False
#     surface.blit(loadimage,(200,200))
#     win.update()
#     # Go ahead and update the screen with what we've drawn.
pygame.display.flip()

# # Be IDLE friendly. If you forget this line, the program will 'hang'
# on exit.
pygame.quit()


import numpy as np
import pandas as pd
import torch
import torch.nn as nn
import gc
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix

import LSTM
from DataLoader_neur import load_beh_data
from DataSaver import save_trained_model

gc.collect()

torch.cuda.empty_cache()

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

# extracting the data
beh_data_dir = "../data/COBAR_behaviour_incl_manual.pkl"
neur_data_dir = '../data/COBAR_neural.pkl'

x_train, y_train, x_validation, y_validation, x_test, y_test = load_beh_data(beh_data_dir, neur_data_dir)

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

# training the model

nb_epochs = 300
learning_rate = 1.9e-3
max_batch_size = 5000
h_dim = 50
n_layers = 2
dim_linear = 50

# checking if there is a GPU available
is_cuda = torch.cuda.is_available()

# If we have a GPU available, we'll set our device to GPU. Device variable will be used later in the code.
if is_cuda:
    device = torch.device("cuda:1")
    print("GPU is available")
else:
    device = torch.device("cpu")
    print("GPU not available, CPU used")

x_train, y_train = x_train.to(device), y_train.to(device)
x_validation, y_validation = x_validation.to(device), y_validation.to(device)
x_test, y_test = x_test.to(device), y_test.to(device)

model = LSTM.LSTMNet(input_size=len(x_train[0, 0]), output_size=5, hidden_dim=h_dim, n_layers=n_layers, dim_linear=dim_linear, device=device)
model.to(device)
errors_train, errors_test = LSTM.train(model, x_train, y_train, x_validation, y_validation, nb_epochs, learning_rate, max_batch_size)
save_trained_model(model, 'LSTM_neur', errors_train, errors_test, n_layers, h_dim, learning_rate, nb_epochs)

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

# Evaluate model predictions

criterion = nn.MSELoss()
nb_batchs = int(x_test.shape[0] // max_batch_size) + 1 * (x_test.shape[0] % max_batch_size != 0)
total_loss = 0
test_preds = []

labels_inv = {0 : 'abdominal_pushing', 1 : 'anterior_grooming', 2 : 'posterior_grooming', 3 : 'walking', 4 : 'resting'}

with torch.no_grad():

    for k in range(nb_batchs):

        input, target = x_test[k * max_batch_size : (k + 1) * max_batch_size], y_test[k * max_batch_size : (k + 1) * max_batch_size]
        model = model.double()
        pred, _ = model.forward(input)
        loss = criterion(pred, target).double()
        total_loss += loss.item() * input.shape[0]
        test_preds += list(pred)

total_loss /= x_test.shape[0]
print("Loss on test set : ", round(total_loss, 6))

test_preds = np.array([torch.argmax(p).item() for p in test_preds])
true_preds = np.array([torch.argmax(p).item() for p in y_test])

print("Accuracy on test set : ", round(100 * np.sum(test_preds == true_preds) / test_preds.shape[0], 2), " %")

for k in range(5):

    indexes = (true_preds == k)
    spec_preds = test_preds[indexes]
    spec_true_preds = true_preds[indexes]
    print("Number of samples for ", labels_inv[k], " in the test set : ", spec_preds.shape[0])
    print("Accuracy for ", labels_inv[k], " in the test set : ", round(100 * np.sum(spec_preds == spec_true_preds) / spec_preds.shape[0], 2), " %")

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

# plot confusion matrix on the test set

def plot_confusion_matrix(cm, classes, normalize=False, title='Confusion matrix', cmap=plt.cm.Blues):
    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
        print("Normalized confusion matrix")
    else:
        print('Confusion matrix, without normalization')

    print(cm)
    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=90)
    plt.yticks(tick_marks, classes)

    fmt = '.2f' if normalize else 'd'
    thresh = cm.max() / 2.
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        plt.text(j, i, format(cm[i, j], fmt), horizontalalignment="center", color="white" if cm[i, j] > thresh else "black")

    plt.tight_layout()
    plt.ylabel('True label')
    plt.xlabel('Predicted label')
    plt.savefig("../Plots/Confusion_matrix.png")

cm = confusion_matrix(true_preds, test_preds)
classes = {'abdominal_pushing' : 0, 'anterior_grooming' : 1, 'posterior_grooming' : 2, 'walking' : 3, 'resting' : 4}
plot_confusion_matrix(cm, classes, normalize=True, title='Confusion matrix', cmap=plt.cm.Blues)

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

# Evaluate model predictions per type of behavior

total_loss = 0
preds = []

with torch.no_grad():

    data = [(x_train, y_train), (x_validation, y_validation), (x_test, y_test)]

    for d in data:

        x, y = d
        print("Loop")
        print(x.shape)
        print(y.shape)
        nb_batchs = int(x.shape[0] // max_batch_size) + 1 * (x.shape[0] % max_batch_size != 0)
        print(nb_batchs)

        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]
            model = model.double()
            pred = model.forward(input)
            loss = criterion(pred, target).double()
            total_loss += loss.item() * input.shape[0]
            preds += list(pred)

total_loss /= len(preds)
print("Total loss : ", round(total_loss, 6))

preds = np.array([labels_inv[torch.argmax(p).item()] for p in preds])
true_preds = np.array([labels_inv[torch.argmax(p).item()] for p in y_train] + [labels_inv[torch.argmax(p).item()] for p in y_validation] + [labels_inv[torch.argmax(p).item()] for p in y_test])

print("Total accuracy : ", round(100 * np.sum(preds == true_preds) / preds.shape[0], 2), " %")

for k in range(5):

    indexes = (true_preds == labels_inv[k])
    spec_preds = preds[indexes]
    spec_true_preds = true_preds[indexes]
    print("Number of samples for ", labels_inv[k], " : ", spec_preds.shape[0])
    print("Accuracy for ", labels_inv[k], " : ", round(100 * np.sum(spec_preds == spec_true_preds) / spec_preds.shape[0], 2), " %")


import pygame
pygame.init()
screen = pygame.display.set_mode((680, 460))
clock = pygame.time.Clock()

# use a rect since it will greatly
# simplify movement and drawing
paddle = pygame.Rect((0, 0, 20, 80))

while True:

    if pygame.event.get(pygame.QUIT): break
    pygame.event.pump()

    # move up/down by checking for pressed keys
    # and moving the paddle rect in-place
    keys = pygame.key.get_pressed()
    if keys[pygame.K_UP]: paddle.move_ip(0, -7)
    if keys[pygame.K_DOWN]: paddle.move_ip(0, 7)

    # ensure the paddle rect does not go out of screen
    paddle.clamp_ip(screen.get_rect())

    screen.fill((0,0,0))
    pygame.draw.rect(screen, (255,255,255), paddle)
    pygame.display.flip()

    clock.tick(60)


import selenium
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import time

browser = webdriver.PhantomJS() # Get local session of PhantomJS
browser.get("https://detail.tmall.com/item.htm?id=529108481405") # Load page
#time.sleep(5) # Let the page load
try:
	price = browser.find_element_by_xpath("//*[@id=\"J_StrPriceModBox\"]/dd/span").text # get element on page
	sales = browser.find_element_by_xpath("//*[@id=\"J_DetailMeta\"]/div[1]/div[1]/div/ul/li[1]/div/span[2]").text
	num_reviews = browser.find_element_by_xpath("//*[@id=\"J_ItemRates\"]/div/span[2]").text
	num_wishlist = browser.find_element_by_xpath("//*[@id=\"J_CollectCount\"]").text
	print(price,sales,num_reviews,num_wishlist) # get element text
	print(browser.page_source)
except NoSuchElementException:
    assert 0, "can't find f_red"
browser.close()


"""
Django settings for API project.
Specific settings are imported other setting libraries for better
control.
"""
import dj_database_url
import os

print("Imported Remote Settings......")

# Root Project Directory
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR =  os.path.dirname(PROJECT_ROOT)

# SECURITY WARNING: keep the secret key used in production secret!
# To add secret key variable to Heroku Deployment use "heroku config:add DJANGO_SECRET_KEY=<your secret key>"
SECRET_KEY = os.environ.get('DJANGO_SECRET_KEY')

# Honor the 'X-Forwarded-Proto' header for request.is_secure()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

# Ensures all API requests are directed through HTTPS
SECURE_SSL_REDIRECT = True

# # Using white noise to collect static files on production
# STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

# # Static asset configuration
# STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
# STATIC_URL = '/static/'
# # Extra places for collectstatic to find static files.
# STATICFILES_DIRS = (
#     os.path.join(BASE_DIR, 'API/static/api-view'),
#     os.path.join(BASE_DIR, 'API/static'),
# )
# WHITENOISE_ROOT = os.path.join(BASE_DIR, 'staticfiles', 'root')

STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')
STATIC_URL = '/static/'
# Extra places for collectstatic to find static files.
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'API/static/api-view'),
)
# https://warehouse.python.org/project/whitenoise/
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

# Ensures all API requests are directed through HTTPS
# SECURE_SSL_REDIRECT = True

# Get geoDjango GEOS and GDAL library paths from Heroku environment variables
GEOS_LIBRARY_PATH = os.environ.get('GEOS_LIBRARY_PATH')
GDAL_LIBRARY_PATH = os.environ.get('GDAL_LIBRARY_PATH')

# https://docs.djangoproject.com/en/1.10/ref/settings/#databases
# Heroku Database
DATABASES = {
    'default': dj_database_url.config(default=os.environ['DATABASE_URL'])
}
# Add PostGIS engine
DATABASES['default']['ENGINE'] = 'django.contrib.gis.db.backends.postgis'

ALLOWED_HOSTS = ["*"]

# Default Set of DEBUG is False
DEBUG = True


#! /powerapps/share/python-anaconda-3.6/bin/python

from optparse import OptionParser
from file_utilities import  check_filename
from seqFileTools import unalign, remove_description
import phylogenetic_utilities
from utils.pbs_jobs import check_pbs
import pbs_runners
import os

def main():

    parser = OptionParser("usage: %prog [options]")
    parser.add_option("-i", "--input", dest="input_aln", help="input alignment file")
    parser.add_option("-n", "--number", dest="number", help="number of sequences to sample randomly")
    parser.add_option("--max_length", dest="max_length", help="total max branch length")
    parser.add_option("--min_length", dest="min_length", default = 0, help="total min branch length")

    (options, args) = parser.parse_args()
    input_aln = options.input_aln
    number = int(options.number)
    max_length = float(options.max_length)
    min_length = float(options.min_length)

    #check filenames
    if input_aln == None:
        parser.error("you must specify an alignemnt")
    input_aln = check_filename(input_aln)
    if number > 50:
        print("Warning - the number of sequences to sample is %i - this is large and may take time"  % number)

    count = 1
    sampled_file = input_aln.split(".")[0] + "_sampled_%i.aln" % number
    sampled_unaligned_file = input_aln.split(".")[0] + "_sampled_%i.fasta" % number
    #files for prank-codon
    codon_aln_file =  input_aln.split(".")[0] + "_sampled_%i.codon_aln.best.fas" % number
    codon_tree_file = input_aln.split(".")[0] + "_sampled_%i.codon_aln.best.phy_phyml_tree.txt" % number
    #files for mafft
    aln_file =  input_aln.split(".")[0] + "_sampled_%i.mafft_aln" % number
    tree_file = input_aln.split(".")[0] + "_sampled_%i.mafft_aln-NO_DESCRIPTION.phy_phyml_tree.txt" % number

    print("Iteration %i" % count)
    total_branch_length = iteration_mafft(input_aln, sampled_file, sampled_unaligned_file, aln_file, tree_file, number)

    success = True
    count += 1
    while (total_branch_length > max_length or  total_branch_length < min_length):
        print("Iteration %i" % count)
        total_branch_length = iteration_mafft(input_aln, sampled_file, sampled_unaligned_file, aln_file, tree_file, number)
        count += 1
        """
        if count > 101:
            print("Over 100 iterations!!!!")
            success = False
            break
        """
    if success:
        print("DONE")
    else:
        print("Did not succeed")

def iteration_mafft(input_aln, sampled_file, sampled_unaligned_file, aln_file, tree_file, number):
    job_id = pbs_runners.sampling_runner(input_aln, number, sampled_file, random=True)
    check_pbs(job_id)
    unalign(sampled_file, outfile=sampled_unaligned_file)
    while (os.path.isfile(sampled_unaligned_file) == False):
        continue
    job_id = pbs_runners.mafft_runner(sampled_unaligned_file, alignment=aln_file)
    while (os.path.isfile(aln_file) == False):
        continue
    no_description = remove_description(aln_file)
    #print(no_description, aln_file)
    check_pbs(job_id)
    job_id = pbs_runners.phyml_runner(no_description, phylip=False)
    check_pbs(job_id)
    while (os.path.isfile(tree_file) == False):
        continue
    total_branch_length = phylogenetic_utilities.total_branch_length(tree_file)
    print(total_branch_length)
    return(total_branch_length)

def iteration_prank(input_aln, sampled_file, sampled_unaligned_file, codon_aln_file, tree_file, number):
    job_id = pbs_runners.sampling_runner(input_aln, number, sampled_file, random=True)
    check_pbs(job_id)
    unalign(sampled_file, outfile=sampled_unaligned_file)
    job_id = pbs_runners.prank_codon_runner(sampled_unaligned_file)
    check_pbs(job_id)
    job_id = pbs_runners.phyml_runner(codon_aln_file, phylip=False)
    check_pbs(job_id)
    total_branch_length = phylogenetic_utilities.total_branch_length(tree_file)
    print(total_branch_length)
    return(total_branch_length)

if __name__ == "__main__":
    main()


from typing import Dict, List, Tuple
from os import path, mkdir
from enum import Enum
import json
from time import sleep

from event import Event
from project import Project

class Type(Enum):
    """
    Enum for work-logs types.
    """

    START = "start"
    END = "end"
    NDEF = "ndef"

class EventHandler():
    """
    Class to handle all existing and future work-logs.
    """

    def __init__(self, folder: str = "data", file: str = "events.json", force: bool = True):
        """
        Initialisation of the class. Set destination folder to store .json files, and the name of the file to look at and to store data to.
        The function handles the creation of the folder.
        """

        self.filename: str = folder + "/" + file
        self.list: List[Event] or None = None

        if force:
            if not path.exists(folder):
                mkdir(folder)
            if not path.exists(self.filename):
                open(self.filename, 'w').close()

        if path.exists(self.filename):
            self.list = self.load()
        else:
            print("File does not exist")

        self.exists = self.list != None

    def show(self) -> None:
        """
        Shows the content of the work-log list.
        """

        for project in self.list:
            print(project)

    def save(self) -> None:
        """
        Save the work-log list by replacing the existing file.
        """

        with open(self.filename, 'w') as f:
            json.dump([ob.__dict__ for ob in self.list], f)

    def load(self) -> List[Event]:
        """
        Load the work-log list from the file.
        """

        with open(self.filename, 'r') as f:
            try:
                list = json.load(f)
            except:
                list = []

        return [Event(e["project"], e["type"], e["time"]) for e in list]

    def add(self, project: Project) -> None:
        """
        Adds a log to the list. It autonamtically detects if it is a "start" or an "end" log.
        If a start-log is added after an other start-log, the corresponding end-log is added between them.
        """

        assert isinstance(project, Project), f"Project not found."

        last_e = self.list[-1] if self.list else Event("_", "_")

        if last_e.project != project.uid:
            type = Type.START
            if last_e.type == Type.START.value:
                self.__append(last_e.project, Type.END)
                sleep(1)
        else:
            type = Type.END if last_e.type == Type.START.value else Type.START

        self.__append(project.uid, type)
        self.save()

    def time(self, display: bool = False) -> Dict[str, str]:
        """
        Sum work times by project.
        """

        last_e = Event("_", "_")
        if len(self.list) == 0:
            return []

        if self.list[-1].type == Type.START.value:
            print("Current work not finished. Showing only partial work-time.")

        conseq: List[Tuple[int, str]] = []
        for e in self.list:
            if last_e.type == Type.START.value and e.type == Type.END.value:
                conseq.append((e - last_e, e.project))
            last_e = e

        times = {}
        unique_projects = set([tup[1] for tup in conseq])
        for p in unique_projects:
            times[p] = sum([tup[0] for tup in conseq if tup[1] == p])

        if display:
            for p, time in times.items():
                self.__print_time(f"Project '{p}'", time)

        return times

    def pause(self, display: bool = False) -> Tuple[str, int]:
        """
        Sum pause times.
        """

        last_e = Event("_", "_")

        conseq_times: List[int] = []
        for e in self.list:
            if last_e.type == Type.END.value and e.type == Type.START.value:
                conseq_times.append(e - last_e)
            last_e = e

        time = sum(conseq_times)
        if display:
            self.__print_time("Pause", time)

        return ("Pause", time)

    def __print_time(self, string: str, time: int) -> None:
        """
        Default project print format.
        """

        print(f"{string}: {time//60:0>2}h{time%60:0>2}")

    def __append(self, project: str, type: Type) -> None:
        """
        Append a project and its event type to the log-list.
        """
        self.list.append(Event(project, type.value))

        if type == Type.START:
            print(f"Work on '{project}' started.")
        elif type == Type.END:
            print(f"Work on '{project}' finished.")
        else:
            print(f"Unexpected state for '{project}'.")


from django.contrib import admin

# Register your models here.
from users.models import UserRegist, WeiboUser

class UserRegist_Manager(admin.ModelAdmin):
    # 定义列表也显示的字段
    list_display = ['id', 'username', 'email', 'phone', 'isActive', 'create_time', 'update_time']
    # 指定哪个字段可以跳转到详情页面
    list_display_links = ['username']
    # 指定列表页右侧的过滤器
    # list_filter = ['pub']
    # 指定列表页左上方的搜索栏
    # search_fields = ['title', 'market_price']
    # 指定列表页可修改的字段值
    # list_editable = ['price']

class WeiboUser_Manager(admin.ModelAdmin):
    # 定义列表也显示的字段
    list_display = ['uid', 'wuid', 'access_token']
    # 指定哪个字段可以跳转到详情页面
    list_display_links = ['wuid']

# 导入想在admin后台管理的数据类
admin.site.register(UserRegist, UserRegist_Manager)
admin.site.register(WeiboUser, WeiboUser_Manager)


#!/bin/env python

#function:
#   demo to show how to use converted model using caffe2fluid
#

import sys
import os
import numpy as np
import paddle.fluid as fluid
import paddle

def test_model(exe, test_program, fetch_list, test_reader, feeder):
    acc_set = []

    for data in test_reader():
        acc_np, pred = exe.run(program=test_program,
                               feed=feeder.feed(data),
                               fetch_list=fetch_list)
        acc_set.append(float(acc_np))

    acc_val = np.array(acc_set).mean()
    return float(acc_val)

def evaluate(net_file, model_file):
    """ main
    """
    #1, build model
    net_path = os.path.dirname(net_file)
    if net_path not in sys.path:
        sys.path.insert(0, net_path)

    from lenet import LeNet as MyNet

    #1, define network topology
    images = fluid.layers.data(name='image', shape=[1, 28, 28], dtype='float32')
    label = fluid.layers.data(name='label', shape=[1], dtype='int64')

    net = MyNet({'data': images})
    prediction = net.layers['prob']
    acc = fluid.layers.accuracy(input=prediction, label=label)

    place = fluid.CPUPlace()
    exe = fluid.Executor(place)
    exe.run(fluid.default_startup_program())

    #2, load weights
    if model_file.find('.npy') > 0:
        net.load(data_path=model_file, exe=exe, place=place)
    else:
        net.load(data_path=model_file, exe=exe)

    #3, test this model
    test_program = fluid.default_main_program().clone()
    test_reader = paddle.batch(paddle.dataset.mnist.test(), batch_size=128)

    feeder = fluid.DataFeeder(feed_list=[images, label], place=place)
    fetch_list = [acc, prediction]

    print('go to test model using test set')
    acc_val = test_model(exe, test_program, \
            fetch_list, test_reader, feeder)

    print('test accuracy is [%.4f], expected value[0.919]' % (acc_val))

if __name__ == "__main__":
    net_file = 'models/lenet/lenet.py'
    weight_file = 'models/lenet/lenet.npy'

    argc = len(sys.argv)
    if argc == 3:
        net_file = sys.argv[1]
        weight_file = sys.argv[2]
    elif argc > 1:
        print('usage:')
        print('\tpython %s [net_file] [weight_file]' % (sys.argv[0]))
        print('\teg:python %s %s %s %s' % (sys.argv[0], net_file, weight_file))
        sys.exit(1)

    evaluate(net_file, weight_file)


import os
import json
from urllib.request import urlretrieve
import numpy as np
from functools import partial

from medcat.preprocessing.tokenizers import spacy_split_all
from medcat.preprocessing.cleaners import spacy_tag_punct
from medcat.utils.spacy_pipe import SpacyPipe
from medcat.cat import CAT
from medcat.utils.helpers import doc2html
from medcat.utils.vocab import Vocab
from medcat.cdb import CDB

class CatWrap(object):
    # CHECK SHORT CONTEXT
    def __init__(self):
        vocab = Vocab()
        vocab_path = os.getenv('VOCAB_PATH', '/tmp/vocab.dat')
        # Get a vocab if it does not exist
        if not os.path.exists(vocab_path):
            vocab_url = os.getenv('VOCAB_URL')
            urlretrieve(vocab_url, vocab_path)

        vocab.load_dict(vocab_path)
        vocab.make_unigram_table()

        cdb = CDB()
        try:
            cdb.load_dict(os.getenv('CDB_PATH', '/tmp/cdb.dat'))
        except Exception as e:
            print(str(e))
            # Makes a blank cdb
            pass

        self.cat = CAT(cdb=cdb, vocab=vocab)

    def add_concepts(self, in_file):
        pass

    def get_html_and_json(self, text):
        self.cat.spacy_cat.ACC_ALWAYS = True
        doc = self.cat(text)
        doc_json = self.cat.get_json(text)
        self.cat.spacy_cat.ACC_ALWAYS = False
        return doc2html(doc), doc_json

    def get_doc(self, params, in_path, is_text=False):
        """
        params:  A dictionary with values for cuis, tuis, tokens, cntx_size,
                 filters[(<name>, <value>, <acc>), ...]
                     params = {'cuis': ['c23123', 'c3216564', ...],
                               'tuis': ['t122', 't222', ...],
                               'tokens': ['kidney', 'heart attack', ...],
                               'cntx_tokens': ['not', 'maybe wrong', 'error', ...]
                               'filters': [('negated', 1, 0.6), ('historical', '10years', 0.5)]}
        """
        no_checks = False
        cuis = params.get('cuis', [])
        tuis = params.get('tuis', [])
        tokens= [tkn for tkn in params.get('tokens', []) if len(tkn.strip()) > 0]
        if "*" in cuis or "*" in tuis:
            no_checks = True

        cntx_tokens = params.get('cntx_tokens', [])
        cntx_size = 5 # This is not really used anymore, but good for cntx_tokens
        filters = [(x[0], str(x[1]), float(x[2])) for x in params.get('filters', [])]
        f_names = [x for x in os.listdir(in_path) if ('.txt' in x or '.json' in x) and not x.startswith(".")]

        out_data = {}
        out_data['f_names'] = f_names
        for f_name in f_names:
            f = open(os.path.join(in_path, f_name), 'r')
            if '.txt' in f_name:
                # It is a text file
                data = json.loads(self.cat.get_json(f.read()))
            else:
                # Already processed by cat - json
                data = json.load(f)

            out_data['text'] = data['text']
            out_data['f_name'] = f_name
            out_data['entities'] = []

            for ent in data['entities']:
                if no_checks or ent['cui'] in cuis or ent['tui'] in tuis or any([True if x in ent['source_value'] else False for x in tokens]):
                    # Check filters first, skip if not valid
                    skip_flag = False
                    for name, value, acc in filters:
                        if name in ent:
                            ent_val = ent['name']
                            ent_acc = ent.get(name + "_acc", 100)
                            if ent_val != value or ent_acc < acc:
                                skip_flag = True
                    if skip_flag:
                        # Skip this entity
                        continue

                    # Check cntx_text filter if exists
                    """
                    if cntx_tokens:
                        # Get the tokens and remove the zero length
                        cntx_tokens = [x for x in [str(x).lower() for x in cntx_tokens] if len(x) > 0]
                        if any([True if x in ent['cntx']['text'] else False for x in cntx_tokens]):
                            # Skip this entity
                            continue
                    """
                    out_data['entities'].append(ent)

            # Doc is fine, return it
            if len(out_data['entities']) > 0:
                break

        return out_data

    def save_doc(self, data, in_path, out_path, del_cntx=True):
        """ Save the newly annotated document

        data:  a dictionary with the new data
            data = {'f_name': <file_name>,
                    'entities': [<entity>, ...]}
        """

        f_name = data['f_name']
        if '.json' not in f_name:
            os.remove(os.path.join(in_path, f_name))
            f_out = open(os.path.join(out_path, f_name.replace('.txt', '.json')), 'w')
            json.dump(data, f_out)
        else:
            f_in = open(os.path.join(in_path, f_name), 'r')
            f_out = open(os.path.join(out_path, f_name), 'w')
            ents = data['entities']
            ids = [x['id'] for x in ents]
            new_ents = []

            doc = json.load(f_in)

            for ent in doc['entities']:
                if ent['id'] not in ids:
                    new_ents.append(ent)

            new_ents.extend(ents)
            doc['entities'] = new_ents

            # Remove old file
            f_in.close()
            os.remove(os.path.join(in_path, f_name))

            json.dump(doc, f_out)


import os
import csv
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker

engine = create_engine(os.getenv("DATABASE_URL"))

db = scoped_session(sessionmaker(bind=engine))

def get_data():
	"""Returns each row as a python dictionary"""

	with open('books.csv', mode='r') as file:
			reader = csv.reader(file)

			for row in reader:

				if(row[0] == "isbn"):
					continue;

				yield {
					'isbn': row[0],
					'title': row[1],
					'author': row[2],
					'published': row[3]
					};

authors = set([])

def insert_authors():
	"""Generated data for authors"""

	# Set helps to remove duplicate records
	for row in get_data():
		authors.add(row['author'])

	# Prepare the query
	query = "INSERT INTO authors (name) VALUES "
	for author in authors:
		# Check for special characters :, '
		author = author.replace(",", " ")
		author = author.replace("'", "''")

		# Add to query
		query += f"('{author}'),"

	# Replace last comma with semicolon
	query = query[0:-1] + ';'

	# Execute the query
	db.execute(query)
	db.commit()
	print("Authors inserted successfully...")

def get_author_id(name):
	if(len(authors) <= 0):
		for row in get_data():
			authors.add(row['author'])

	# Find the author in authors set
	return list(authors).index(name) + 1

def insert_books():
	"""Generate data for books"""
	query = "INSERT INTO books (isbn, title, published, author_id) VALUES "

	count = 1
	for row in get_data():
		# Extract the data
		isbn = row["isbn"]
		title = row["title"]
		published = row["published"]
		author = get_author_id(row["author"])

		# Check for special characters :, '
		isbn = isbn.replace(",", " ")
		isbn = isbn.replace("'", "''")
		title = title.replace(",", ' ')
		title = title.replace("'", "''")

		query += f"('{isbn}', '{title}', {published}, {author}),"

	# Replace the last character with ;
	query = query[0:-1] + ';'

	# Execute the query
	db.execute(query)
	db.commit()

	print("Books inserted successfully...")

def main():
	# Alert the user
	print("="*10)
	print("Process Started: Please wait until it finishes.")
	print("You will be provided status messages during the process.")
	print("="*10)

	# Create authors table
	db.execute("CREATE TABLE IF NOT EXISTS authors(\
		id SERIAL PRIMARY KEY,\
		name VARCHAR(100) NOT NULL\
		)");

	# Create books table
	db.execute('CREATE TABLE IF NOT EXISTS books(\
		id SERIAL PRIMARY KEY,\
		isbn VARCHAR(50) NOT NULL, \
		title VARCHAR(200) NOT NULL, \
		published INTEGER NOT NULL DEFAULT 0, \
		author_id INTEGER REFERENCES authors)')

	db.commit()

	print("tables created successfully...")

	# Insert Authors
	insert_authors()

	# Insert Books
	insert_books()

if __name__ == "__main__":
	main()


from django.db import models
from polymorphic.models import PolymorphicModel
from FOMO import settings

class Category(models.Model):
    create_date = models.DateTimeField(auto_now_add=True)
    last_modified = models.DateTimeField(auto_now=True)
    name = models.TextField()
    description = models.TextField()

    def __str__(self):
        return self.name

class Product(PolymorphicModel):

    TYPE_CHOICES = (
        ('BulkProduct','Bulk Product'),
        ('IndividualProduct','Individual Product'),
        ('RentalProduct','Rental Product'),
    )
    STATUS_CHOICES = (
        ('A','Active'),
        ('I','Inactive'),
    )
    create_date = models.DateTimeField(auto_now_add=True)
    last_modified = models.DateTimeField(auto_now=True)
    status = models.TextField()
    name = models.TextField()
    description = models.TextField()
    category = models.ForeignKey('Category',on_delete=models.CASCADE)
    price = models.DecimalField(max_digits=7,decimal_places=2)

    def image_url(self):
        image = ProductImage.objects.filter(product = self)
        try:
            url = settings.STATIC_URL + 'catalog/media/products/' + image[0].filename
        except:
            url = settings.STATIC_URL + 'catalog/media/products/no_image.png'

        return url

    def image_urls(self):
        image = ProductImage.objects.filter(product = self)
        mylist=[]
        icount = 0

        #for each item in list
        try:
            for i1 in image:

                url = settings.STATIC_URL + 'catalog/media/products/' + i1.filename
                mylist.append([url])
                icount = icount+1
        except:
            url = settings.STATIC_URL + 'catalog/media/products/no_image.png'
            mylist.append([url])

        return mylist

class BulkProduct(Product):
    TITLE = 'Bulk'
    quantity = models.IntegerField()
    reorder_trigger = models.IntegerField()
    reorder_quantity = models.IntegerField()

class IndividualProduct(Product):
    TITLE = 'Individual'
    pid = models.TextField()

class RentalProduct(Product):
    TITLE = 'Rental'
    pid = models.TextField()
    max_rental_days = models.IntegerField(default=0)
    retire_date = models.DateField(null=True,blank=True)

class ProductImage(models.Model):
    filename = models.TextField()
    create_date = models.DateTimeField(auto_now_add=True)
    last_modified = models.DateTimeField(auto_now =True)
    product = models.ForeignKey('Product', on_delete = models.CASCADE,related_name='images')

## empy NOT_FOUND_PRODUCT_IMAGE = ProductImage()


# uncompyle6 version 3.7.4
# Python bytecode 2.5 (62131)
# Decompiled from: Python 3.6.9 (default, Apr 18 2020, 01:56:04)
# [GCC 8.4.0]
# Embedded file name: build/bdist.linux-i686/egg/clue/bzrserver/utils.py
# Compiled at: 2009-03-01 11:10:34
import logging
logger = logging.getLogger('clue.bzrserver')
logger.setLevel(level=logging.INFO)
security_logger = logging.getLogger('clue.bzrserver.security')
security_logger.setLevel(level=logging.WARNING)