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

import re
from csv import DictReader

def extract_keywords(title):
    """Extracts keywords from a title, and returns the title and a dictionary of keys and values"""
    dict = {}
    for key, value in re.findall('%(\w+) ([^%]+)', title):
        dict[key] = value

    if title.find('%') != -1:
        title = title[:title.find('%')].strip()

    return title, dict

def latex_escape(str):
    """Replaces unescaped special characters with escaped versions, and does
    other special character conversions."""

    str = str.replace('~','{\\textasciitilde}')
#    str = str.replace('β','\\beta')

    # escape these characters if not already escaped
    special_chars = r'\#\@\&\$\_\%'
    patternstr = r'([^\\])([%s])' % (special_chars)
    str = re.sub(patternstr, '\\1\\\\\\2', str)

    # fix superscripts
#    str = re.sub(r'([^$])\^(.*?) ', r'\1$^\2$ ',  str)
    return str

class Paper:
    def __init__(self, line, subconf):
        self.id, rest = line.split(' ', 1)
        if re.match(r'^\d+', rest) is not None:
            self.time, comment = rest.split(' ', 1)
            self.poster = False
        else:
            self.time = None
            self.poster = True
            comment = rest

        if self.id.find('/') != -1:
            tokens = self.id.split('/')
            self.id = '%s-%s' % (tokens[1].lower(), threedigits(tokens[0]))
        else:
            self.id = '%s-%s' % (subconf, threedigits(self.id))

    def __str__(self):
        return "%s %s" % (id, time)

def threedigits(str):
    return '%03d' % (int(str))

class Session:
    def __init__(self, line, date):
        (self.time, namestr) = line[2:].split(' ', 1)
        self.date = date
        self.papers = []
        self.desc = None
        self.num = None

        (self.name, self.keywords) = extract_keywords(namestr)

        if self.name.find(':') != -1:
            #print "FOUND IT", line, self.name
            colonpos = self.name.find(':')
            self.desc = self.name[colonpos+2:]
            self.name = self.name[:colonpos]
            self.num = self.name.split(' ')[-1][:-1]
        # print >> sys.stderr, "LINE %s NAME %s DESC %s" % (line, self.name, self.desc)

        self.poster = False
        l = self.name.lower()
        if 'poster' in l or 'demo' in l or 'best paper' in l:
            self.poster = True

    def __str__(self):
        return "SESSION [%s/%s] %s %s %s" % (self.date, self.time, self.name, self.desc, self.num)

    def add_paper(self,paper):
        self.papers.append(paper)

    def chair(self):
        """Returns the (first name, last name) of the chair, if found in a %chair keyword"""

        if self.keywords.has_key('chair'):
            fullname = self.keywords['chair']
            if ',' in fullname:
                names = fullname.split(', ')
                return (names[1].strip(), names[0].strip())
            else:
                # This is just a heuristic, assuming the first name is one word and the last
                # name is 1+ words
                names = fullname.split(' ', 1)
                return (names[0].strip(), names[1].strip())
        else:
            return ('', '')

    def chairs(self):
        chairs = []
        for kw, val in self.keywords.items():
            if kw.startswith("chair"):
                fullname = val
                if ', ' in fullname:
                    names = fullname.split(', ', 1)
                    chairs.append((names[1].strip(), names[0].strip()))
                else:
                    names = fullname.split(' ', 1)
                    chairs.append((names[0].strip(), names[1].strip()))
        return chairs


import tensorflow as tf
from nets.policies import get_policy
from nets.value_functions import MLPValueFunc

# Implementation of Deterministic Soft Policy Gradients (DSPG)

class SoftPolicyGradient(object):
    def __init__(self, sess, conf, state_shape, action_shape):
        self.sess = sess
        self.state_shape = state_shape
        self.action_shape = action_shape
        self.tau = conf.tau
        self.gamma = conf.gamma
        self.global_norm = conf.global_norm
        self.num_samples = conf.num_samples
        self.actor_lr = conf.actor_learning_rate
        self.Q_critic_lr = conf.Q_critic_learning_rate

        self.policy = get_policy(conf.pi_type, conf.hidden_dims_pi, conf.reg, conf.reg_factor)
        self.value = MLPValueFunc(conf.hidden_dims_Q, conf.hidden_dims_Q)

        # self._init_placeholders() #-------------------------------
        self.state = state = tf.placeholder(tf.float32, [None] + list(self.state_shape), name="obs_ph")
        self.action = action = tf.placeholder(tf.float32, (None, self.action_shape), name="act_ph")
        self.reward = tf.placeholder(tf.float32, [None], name="r_ph")
        self.next_state = tf.placeholder(tf.float32, [None] + list(self.state_shape), name="nextobs_ph")
        self.done = tf.placeholder(tf.float32, [None], name="done_ph")
        # networks
        self.sampled_action, self.deterministic_action = self.sample_pi_network(state, 'pi')  # [-inf, inf]
        self.Q = self.value.Q_network(state, tf.tanh(action), 'Q')
        _, _ = self.sample_pi_network(state, 'target_pi')
        self.target_Q = self.value.Q_network(state, tf.tanh(action), 'target_Q')

        # self._init_actor_update()  #-------------------------------

        # constructing pi loss
        apply_grads = conf.apply_grad
        Q_sampled, log_pi_sampled = self.get_sampled_Q_and_pi(self.state, 'Q', 'pi', apply_grads)

        self.pi_variables = pi_variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope='pi/')

        if apply_grads:
            print("shi using apply gradients")
            advantage = tf.subtract(Q_sampled, log_pi_sampled, name="advantage")
            advantage = tf.stop_gradient(advantage)

            # Whiten the advantage.
            adv_mean = tf.reduce_mean(advantage)
            advantage = advantage - adv_mean

            pi_grad = tf.gradients(log_pi_sampled, pi_variables, -advantage)
            pi_grad, _ = tf.clip_by_global_norm(pi_grad, self.global_norm)

            self.pi_loss = tf.multiply(log_pi_sampled, (-advantage), name="pi_loss_apply_grad") # just a placeholder

            self.train_pi = tf.train.AdamOptimizer(
                learning_rate=self.actor_lr).apply_gradients(zip(pi_grad, pi_variables), name="apply_grad_adam")

        else:
            advantage = tf.subtract(Q_sampled, log_pi_sampled, name="advantage")
            self.pi_loss = tf.multiply(log_pi_sampled, (-advantage), name="pi_loss")
            self.train_pi = tf.train.AdamOptimizer(
                learning_rate=self.actor_lr).minimize(-advantage, var_list=pi_variables, name="adam_pi")

        # Why apply gradients above when you can just minimize. :)
        # self.train_pi = tf.train.AdamOptimizer(
        #     learning_rate=self.actor_lr).minimize(self.pi_loss, var_list=self.pi_variables)

        # My logic to instead simply maximise q value along the pi action, so as to only train variables in scope main/pi

        # self._init_critic_update() #-------------------------------
        # constructing Q loss
        target_Q_s2_sampled, log_pi_s2_sampled = self.get_sampled_Q_and_pi(self.next_state, 'target_Q', 'target_pi')
        target_Q_s2_sampled = tf.reduce_mean(tf.reshape(target_Q_s2_sampled, (self.num_samples, -1)), axis=0)
        log_pi_s2_sampled = tf.reduce_mean(tf.reshape(log_pi_s2_sampled, (self.num_samples, -1)), axis=0)
        target_Q = tf.add(self.reward , (1 - self.done) * self.gamma * (target_Q_s2_sampled - log_pi_s2_sampled), name="q_backup")
        self.Q_loss = Q_loss = tf.reduce_mean(tf.square(self.Q - target_Q), name="shi_sqr_q_loss")

        self.Q_variables = Q_variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope='Q/')
        self.train_Q = tf.train.AdamOptimizer(
            learning_rate=self.Q_critic_lr).minimize(Q_loss, var_list=Q_variables, name="s_train_q_op")

        # self._init_target_ops() #-------------------------------
        target_Q_variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope='target_Q/')
        target_pi_variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope='target_pi/')
        with tf.name_scope('soft_polyak'):
            # soft update
            soft_update_Q_ops = [tf.assign(xbar, self.tau*x + (1 - self.tau)*xbar)
                                    for (xbar, x) in zip(target_Q_variables, self.Q_variables)]
            soft_update_pi_ops = [tf.assign(xbar, self.tau * x + (1 - self.tau) * xbar)
                                for (xbar, x) in zip(target_pi_variables, self.pi_variables)]
            soft_update_Q_ops.extend(soft_update_pi_ops)
            self.soft_update_ops = tf.group(*soft_update_Q_ops)
        with tf.name_scope('hard_polyak'):
            # hard update
            hard_update_Q_ops = [tf.assign(xbar, x) for (xbar, x) in zip(target_Q_variables, self.Q_variables)]
            hard_update_pi_ops = [tf.assign(xbar, x) for (xbar, x) in zip(target_pi_variables, self.pi_variables)]
            hard_update_Q_ops.extend(hard_update_pi_ops)
            self.hard_update_ops = tf.group(*hard_update_Q_ops)

        sess.run(tf.global_variables_initializer())
        sess.run(self.hard_update_ops)

    def get_sampled_Q_and_pi(self, state, name_Q, name_pi, apply_grads=True):
        state = tf.tile(state, [self.num_samples, 1])
        sampled_action, _ = self.sample_pi_network(state, name_pi, reuse=True)
        if(apply_grads):
            sampled_action = tf.stop_gradient(sampled_action)
        Q_sampled = self.value.Q_network(state, tf.tanh(sampled_action), name_Q, reuse=True)
        log_pi_sampled = self.pi_network_log_prob(sampled_action, state, name_pi, reuse=True)
        return Q_sampled, log_pi_sampled

    def pi_network_log_prob(self, action, state, name, reuse=None):
        with tf.variable_scope(name, reuse=reuse):
            state_feature = self.policy.feature_extractor(state)
            parameters = self.policy.produce_policy_parameters(self.action_shape, state_feature)
            log_prob = self.policy.policy_parameters_to_log_prob(action, parameters)
        return log_prob

    def sample_pi_network(self, state, name, reuse=None):
        with tf.variable_scope(name, reuse=reuse):
            state_feature = self.policy.feature_extractor(state)
            parameters = self.policy.produce_policy_parameters(self.action_shape, state_feature)
            sample = self.policy.policy_parameters_to_sample(parameters)
        return sample, parameters[0]

    def sampling_actions(self, state, is_deterministic=False):
        if is_deterministic:
            return self.sess.run(self.deterministic_action, feed_dict={self.state: state})
        else:
            return self.sess.run(self.sampled_action, feed_dict={self.state: state})

    def trainer(self, transitions):
        state, action, reward, next_state, done = transitions
        [_, _, Q, Q_loss, pi_loss] = self.sess.run(
            [self.train_Q, self.train_pi, self.Q, self.Q_loss, self.pi_loss],
            feed_dict={self.state: state,
                       self.action: action,
                       self.reward: reward,
                       self.next_state: next_state,
                       self.done: done
                       })
        self.sess.run(self.soft_update_ops)
        return Q, Q_loss, pi_loss


__author__ = 'jk'
# -*- coding: utf-8 -*-
from unittest_data_provider import data_provider
from unittest import TestCase
import unittest
from selenium import webdriver
from online.helpers.page import Page

class SeleniumTest(TestCase):
    driver = None

    pos_requests = lambda: (
        (u'кафе',),
        (u'бар',),
        (u'метро',)
    )

    neg_requests = lambda: (
        (u'фффпеанфегеа',),
        (u'1111111111',),
        (u'asdffasf',)
    )

    @classmethod
    def setUpClass(cls):
        cls.driver = webdriver.Firefox()
        cls.driver.implicitly_wait(10)
        cls.base_url = "http://2gis.ru"
        cls.page = Page(cls.driver)
        cls.page.open(cls.base_url)

    @classmethod
    def tearDownClass(cls):
        cls.driver.quit()

    @data_provider(pos_requests)
    def test_positive_search(self, request):
        page = self.page
        page.search_bar.clear_input()
        page.search_bar.search_by_name(request)
        self.assertTrue(page.search_result.has_results)

    @data_provider(neg_requests)
    def test_negative_search(self, request):
        page = self.page
        page.search_bar.clear_input()
        page.search_bar.search_by_name(request)
        self.assertTrue(page.error_search_result.has_no_results)

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


from django.urls import path, include
from . import views

urlpatterns = [
    path('', views.index, name='index'),
    path('visitors_home/', views.visitors_home, name='visitors_home'),
    path('add_guest/', views.GuestCreateView.as_view(), name='addguest'),
    path('list_guests/', views.GuestListView.as_view(), name='listguests'),
    path('add_message/<int:pk>', views.MessageCreateView.as_view(), name='addmessage'),
    path("vacancies/", views.vacancies, name='vacancies'),
]


__author__ = 'mohamedhusain'
'''Monday, 25 January 2016 10:00 AM
   Give the user separate hour, minute , seconds format, given the user request.
'''

import time

def get_hours():           # gives hours in int type
    std_time= time.ctime()
    return st2int(std_time[11:13])

def get_minutes():           # gives minute in int type
    std_time= time.ctime()
    return st2int(std_time[14:16])

def get_seconds():           # gives minute in int type
    std_time= time.ctime()
    return st2int(std_time[17:19])

def int1(string):
    final=9999
    if string is '0':
        final = 0
    elif string is '1':
        final = 1
    elif string is '2':
        final = 2
    elif string is '3':
        final = 3
    elif string is '4':
        final = 4
    elif string is '5':
        final = 5
    elif string is '6':
        final = 6
    elif string is '7':
        final = 7
    elif string is '8':
        final = 8
    elif string is '9':
        final = 9
    return final

def st2int(text):  # Converts str type into int type
    fin = int1(text[1]) +  10 * int1(text[0])
    return fin

# <------------------Main---------------------->

get_hours()
get_minutes()
get_seconds()


import numpy as np
import pickle
from collections import defaultdict
import random

def softmax(wonda):
    #wondaはx
    return np.exp(wonda) / np.sum(np.exp(wonda))

def create_onehot(arg, ids):
    #ここでvecのlengthは決定しておく
    vec = np.zeros(len(ids))
    vec[ids[arg]] = 1
    return vec

def update_weights(network, delta, lam):
    for w, d in zip(network, delta):
        w += lam*d

def find_max(p):
    y = 0
    for i in range(len(p)):
        if p[i] > p[y]:
            y = i
    return y #確率が最も高いindexを返す

def forward_rnn(network, x):
    h = list() #hidden layer
    p = list() #probability
    y = list() #prediction
    w_rx, w_rh, w_oh, b_r, b_o = network

    for t in range(len(x)):
        if t > 0:
            h.append(np.tanh(np.dot(w_rx, x[t]) + np.dot(w_rh, h[t-1]) + b_r))
        else:
            h.append(np.tanh(np.dot(w_rx, x[t]) + b_r))
        p.append(softmax(np.dot(w_oh, h[t]) + b_o))
        y.append(find_max(p[t]))
    return h, p, y
def backward_rnn(network, x, h, p, p_co, err_c):
    d_weight_rx = np.zeros((2, len(x_ids)))
    d_weight_rh = np.zeros((2,2))
    d_weight_oh = np.zeros((len(y_ids), 2))
    d_b_r = np.zeros(2)
    d_b_o = np.zeros(len(y_ids))
    w_rx, w_rh, w_oh, b_r, b_o = network

    delta_r_ = np.zeros(len(b_r)) #t+1から伝搬されるエラー
    for t in reversed(range(len(x))):
        #p_ = create_onehot(y_co[t]) #なんか変
        delta_o_ = (p_co[t] - p[t])  #出力層のエラー(多分tつけんと麻酔)
        """
        print ('正解ラベル')
        print (p_co[t])
        print ('softmaxされた確率')
        print (p[t])
        print ('差分')
        print (delta_o_)
        """
        err_c += delta_o_
        d_weight_oh += np.outer(delta_o_, h[t])
        d_b_o += delta_o_                        #出力重み勾配
        delta_r = np.dot(delta_r_, w_rh) + np.dot(delta_o_, w_oh) #逆伝搬
        #delta_r = np.dot(delta_o_, w_oh) #逆伝搬
        delta_r_ = delta_r * (1 - h[t] ** 2) #tanhの重み勾配
        d_weight_rx += np.outer(delta_r_, x[t])
        d_b_r += delta_r_ #hidden layer重み勾配
        if t != 0:
            d_weight_rh += np.outer(delta_r_, h[t-1])

    ddddd = [d_weight_rx, d_weight_rh, d_weight_oh, d_b_r, d_b_o]
    return ddddd, err_c

if __name__ == '__main__':
    """
    やることまとめ
    単語をid化,train_dataからラベルと、テキストから素性を持ってくる
    ネットワーク、つまり重みを初期化する
    持ってきたラベルと素性を一つずつ(for)持ってきてforward,backwardそして重み更新して終わり
    ここまで前回、ここからRNN
    瀕死推定のため少しids等が異なる、forwardが一つ前のhidden unitを見る、勾配がhidden unitを通ってback
    この3点が大きく異なるはず
    """
    l = 10
    train_f = '../../data/wiki-en-train.norm_pos'
    #train_f='../../test/05-train-input.txt'
    feat_lab = list()
    layer_num = 2
    lam = .01
    x_ids = defaultdict(lambda:len(x_ids))
    y_ids = defaultdict(lambda:len(y_ids))

    #素性の数を数えて重みの初期化を行う
    #出現単語、瀕死にidsを振る,その後初期化とか
    with open(train_f, 'r') as train_init:
        for line in train_init:
            words = line.split()
            for word in words:
                x, y = word.split('_')
                x_ids[x]
                y_ids[y]
    with open(train_f, 'r') as train_init:
        for line in train_init:
            x_list = list()
            y_list = list()
            words = line.split()
            for word in words:
                x, y = word.split('_')
                x_list.append(create_onehot(x, x_ids)) #単語列
                y_list.append(create_onehot(y, y_ids)) #瀕死列
            #feat_labに一文単位で各単語の素性と正解ラベルを入れていく
            feat_lab.append((x_list, y_list))
        #ここからネットワークの初期化
        weight_rx = (np.random.rand(2, len(x_ids)) - .5) * .2 * .1
        weight_rh = (np.random.rand(2,2) - .5) * .2*.1
        weight_oh = (np.random.rand(len(y_ids), 2) - .5) * .2 * .1
        b_r = np.zeros(2)
        b_o = np.zeros(len(y_ids))
        network = [weight_rx, weight_rh, weight_oh, b_r ,b_o]
    #ここから学習
    for i in range(l):
        random.shuffle(feat_lab)
        err_count = 0
        print ('epoch {}'.format(i))
        for x, y_correct in feat_lab:
            h, p, y_predict = forward_rnn(network, x)
            delta, err_count = backward_rnn(network, x, h, p, y_correct, err_count)
            update_weights(network, delta, lam)
        #print (err_count)
    with open('network.dump', 'wb') as net_f:
        pickle.dump(network, net_f)

    with open('x_ids.dump', 'wb') as ids_f1:
        pickle.dump(dict(x_ids), ids_f1)

    with open('y_ids.dump', 'wb') as ids_f2:
        pickle.dump(dict(y_ids), ids_f2)


# -*- coding: utf-8 -*-
"""
    最终输出数据
"""

# transfer_code	transfer_state	out_warehouse	in_warehouse	in_server	fnsku	seller_sku	quantity	price	delivery_code	shipment_code	shipment_state	quantity_shipped	quantity_received	quantity_incase	sku	sku_name	department

import pandas as pd

def output_data(source_wb, target_wb):
    """
        最终输出数据
    """
    print("开始生成最终结果...")
    source_wb = source_wb
    target_wb = target_wb
    # 1. 读取数据，作为df
    df = pd.read_excel(source_wb)

    df.rename(columns={
        'transfer_code':'调拨单号',
        'transfer_state':'调拨单状态',
        'out_warehouse':'调出仓库',
        'in_warehouse':'调入仓库',
        'in_server':'账号',
        'quantity':'调拨数量',
        'price':'调拨采购成本(USD)',
        'delivery_code':'物流单号',
        'shipment_id':'shipment单号',
        'shipment_state':'shipment状态',
        'shipment_record_date':'shipment日期',
        'sku_name':'产品名称',
        'department':'事业部',
        'item_price':'加权采购成本',

    }, inplace=True)

    out_list = [
        '调拨单号',
        '调拨单状态',
        '调出仓库',
        '调入仓库',
        '账号',
        'fnsku',
        'seller_sku',
        'sku',
        '产品名称',
        '事业部',
        '调拨数量',
        '调拨采购成本(USD)',
        '物流单号',
        'shipment单号',
        'shipment状态',
        'shipment日期',
        'quantity_shipped',
        'quantity_received',
        'quantity_incase',
        '加权采购成本',
    ]

    df = df[out_list]

    df.to_excel(target_wb, '基础数据', index=False)

if __name__ == "__main__":
    source_wb = '结果输出/调拨单_匹配sku_匹配事业部_匹配成本.xlsx'
    target_wb = '结果输出/调拨单_匹配sku_匹配事业部_匹配成本_最终输出.xlsx'
    output_data(source_wb, target_wb)


from __future__ import absolute_import
from __future__ import print_function
from __future__ import division

import torch
import copy
from PIL import Image
from zipfile import ZipFile
import os
import sys
import os.path as osp
import torchreid

def read_image(path):
    """Reads image from path using ``PIL.Image``.

    Args:
        path (str): path to an image.

    Returns:
        PIL image
    """
    got_img = False
    if not osp.exists(path):
        raise IOError('"{}" does not exist'.format(path))
    while not got_img:
        try:
            img = Image.open(path).convert('RGB')
            got_img = True
        except IOError:
            print('IOError incurred when reading "{}". Will redo. Don\'t worry. Just chill.'.format(path))
            pass
    return img

class Dataset(object):
    """An abstract class representing a Dataset.

    This is the base class for ``ImageDataset`` and ``VideoDataset``.

    Args:
        train (list): contains tuples of (img_path(s), pid).
        val_query (list): contains tuples of (img_path(s), pid).
        val_gallery (list): contains tuples of (img_path(s), pid).
        query (list): contains tuples of (img_path(s), pid).
        gallery (list): contains tuples of (img_path(s)).
        transform: transform function.
        mode (str): 'train', 'query' or 'gallery'.
        verbose (bool): show information.
    """
    _junk_pids = []  # contains useless person IDs, e.g. background, false detections

    def __init__(self, train, val_query, val_gallery, query, gallery, transform=None, mode='train', verbose=True, **kwargs):
        self.train = train
        # split by ourself
        self.val_query = val_query
        self.val_gallery = val_gallery
        # the provided dataset
        self.query = query
        self.gallery = gallery

        self.transform = transform
        self.mode = mode
        self.verbose = verbose

        self.num_train_pids = self.get_num_pids(self.train)

        if self.mode == 'train':
            self.data = self.train
        elif self.mode == 'val_query':
            self.data = self.val_query
        elif self.mode == 'val_gallery':
            self.data = self.val_gallery
        elif self.mode == 'query':
            self.data = self.query
        elif self.mode == 'gallery':
            self.data = self.gallery
        else:
            raise ValueError('Invalid mode. Got {}, but expected to be '
                             'one of [train | val_query | val_gallery | query | gallery ]'.format(self.mode))

        if self.verbose:
            self.show_summary()

    def __getitem__(self, index):
        img_path, pid = self.data[index]
        img = read_image(img_path)
        if self.transform is not None:
            img = self.transform(img)
        return img, pid, img_path

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

    def parse_data(self, data):
        """Parses data list and returns the number of person IDs
        and the number of camera views.

        Args:
            data (list): contains tuples of (img_path(s), pid, camid)
        """
        pids = set()
        for _, pid in data:
            pids.add(pid)
        return len(pids)

    def get_num_pids(self, data):
        """Returns the number of training person identities."""
        return self.parse_data(data)

    def show_summary(self):
        """Shows dataset statistics."""
        num_train_pids = self.parse_data(self.train)
        num_val_query_pids = self.parse_data(self.val_query)
        num_val_gallery_pids = self.parse_data(self.val_gallery)

        print('=> Loaded {}'.format(self.__class__.__name__))
        print('  ----------------------------------------')
        print('  subset   | # ids | # images')
        print('  ----------------------------------------')
        print('  train    |  {:5d}  | {:8d}'.format(num_train_pids, len(self.train)))
        print('  Vquery   |  {:5d}  | {:8d}'.format(num_val_query_pids, len(self.val_query)))
        print('  Vgallery |  {:5d}  | {:8d}'.format(num_val_gallery_pids, len(self.val_gallery)))
        print('  query    | UNKNOWN | {:8d}'.format(len(self.query)))
        print('  gallery  | UNKNOWN | {:8d}'.format(len(self.gallery)))
        print('  ----------------------------------------')

class MyDataset(Dataset):
    """
        - identities: 4,768. (2272 train id + 500 val id + 1996 only_one_image id)
        - images: 20429 (train) + 1,348 (query) + 5,366(gallery).
        - cameras: None.
    """

    def __init__(self, root="project/ywchong/ywchong/CODE/zc/AIC/data", **kwargs):
        self.root = osp.abspath(osp.expanduser(root))

        # unzip yourself, else messy code caused
        # train_path = osp.join(root, 'train.zip')
        # test_path = osp.join(root, 'test_a.zip')
        #
        # # Extract the file
        # exdir = osp.join(root, 'MyDataset')
        # if not osp.isdir(exdir):
        #     print("Extracting zip file...")
        #     with ZipFile(train_path) as z:
        #        z.extractall(path=root)
        #     with ZipFile(test_path) as z:
        #         z.extractall(path=root)
        self.train_dir = osp.join(root, "MyDataset_V2/bounding_box_train_remove_1_and_2")  # change here to adjust the train set
        # self.list_train_path = osp.join(root, 'train_list.txt')  # useless, we have already rename the image except the raw test set
        self.val_query_dir = osp.join(root, "MyDataset_V2/query_v2")  # change here to adjust the val set query
        self.val_gallery_dir = osp.join(root, "MyDataset_V2/bounding_box_test_distractor_v2")  # change here to adjust the val set gallery
        self.query_dir = osp.join(root, "chu_test_b/chu_test_b/query_b/query_b")  # true query
        self.gallery_dir = osp.join(root, "chu_test_b/chu_test_b/gallery_b/gallery_b")  # true gallery, no pid label
        #self.list_query_path = osp.join(root, 'chu_test_a/chu_test_a/query_a_list.txt')

        train = self.process_dir(self.train_dir, relabel=True)  # we remove some images, thus this must relabel, else the pid_value maybe exceed total class num
        val_query = self.process_dir(self.val_query_dir)
        val_gallery = self.process_dir(self.val_gallery_dir)
        query = self.process_dir(self.query_dir, no_pid=True)  # this pid exist, but maybe useless except multi-query, and best not modify this
        gallery = self.process_dir(self.gallery_dir, no_pid=True)

        super(MyDataset, self).__init__(train, val_query, val_gallery, query, gallery, **kwargs)

    def process_dir(self, dir_path, no_pid=False, relabel=False):
        data = []
        pid_container = set()
        for image_name in os.listdir(dir_path):
            pid = int(image_name.split("_")[0]) if not no_pid else -1
            pid_container.add(pid)

        pid2label = {pid: label for label, pid in enumerate(pid_container)}

        for image_name in os.listdir(dir_path):
            pid = int(image_name.split("_")[0]) if not no_pid else -1  # when retrieve, use distance but not pid, thus random pid is given and do not effect the result
            img_path = osp.join(dir_path, image_name)
            if relabel:
                pid = pid2label[pid]
            data.append((img_path, pid,))
        return data

class MyDataManager():
    def __init__(self, root="project/ywchong/ywchong/CODE/zc/AIC/data", height=256, width=128,
                 transforms='random_flip', norm_mean=None, norm_std=None, use_gpu=True, batch_size_train=32,
                 batch_size_test=32, workers=4, num_instances=4, train_sampler='', loss='softmax',):

        self.height = height
        self.width = width
        self.use_gpu = (torch.cuda.is_available() and use_gpu)
        super(MyDataManager, self).__init__()

        self.transform_tr, self.transform_te = torchreid.data.transforms.build_transforms(
            self.height, self.width, transforms=transforms,
            norm_mean=norm_mean, norm_std=norm_std
        )

        print('=> Loading train dataset')
        trainset = MyDataset(root, transform=self.transform_tr, mode='train')
        train_sampler = torchreid.data.sampler.build_train_sampler(
            trainset.train, train_sampler,
            batch_size=batch_size_train,
            num_instances=num_instances,
            loss=loss,
        )
        self.num_train_pids = trainset.parse_data(trainset.train)

        self.trainloader = torch.utils.data.DataLoader(
            trainset,
            sampler=train_sampler,
            batch_size=batch_size_train,
            shuffle=False,
            num_workers=workers,
            pin_memory=self.use_gpu,
            drop_last=True
        )

        print('=> Loading val (self_test) dataset')
        self.valloader = {'val_query': None, 'val_gallery': None}
        self.valdataset = {'val_query': None, 'val_gallery': None}
        val_queryset = MyDataset(root, transform=self.transform_te, mode='val_query')
        self.valloader['val_query'] = torch.utils.data.DataLoader(
            val_queryset,
            batch_size=batch_size_test,
            shuffle=False,
            num_workers=workers,
            pin_memory=self.use_gpu,
            drop_last=False)
        val_galleryset = MyDataset(root, transform=self.transform_te, mode='val_gallery', verbose=False)
        self.valloader['val_gallery'] = torch.utils.data.DataLoader(
            val_galleryset,
            batch_size=batch_size_test,
            shuffle=False,
            num_workers=workers,
            pin_memory=self.use_gpu,
            drop_last=False)
        self.valdataset['val_query'] = val_queryset.query
        self.valdataset['val_gallery'] = val_galleryset.gallery

        print('=> Loading raw test dataset')
        self.testloader = {'query': None, 'gallery': None}
        self.testdataset = {'query': None, 'gallery': None}
        queryset = MyDataset(root, transform=self.transform_te, mode='query')
        self.testloader['query'] = torch.utils.data.DataLoader(
            queryset,
            batch_size=batch_size_test,
            shuffle=False,
            num_workers=workers,
            pin_memory=self.use_gpu,
            drop_last=False)
        galleryset = MyDataset(root, transform=self.transform_te, mode='gallery', verbose=False)
        self.testloader['gallery'] = torch.utils.data.DataLoader(
            galleryset,
            batch_size=batch_size_test,
            shuffle=False,
            num_workers=workers,
            pin_memory=self.use_gpu,
            drop_last=False)
        self.testdataset['query'] = queryset.query
        self.testdataset['gallery'] = galleryset.gallery
        print('\n')


import numpy as np
import torch
import torch.nn as nn
from torch.autograd import Variable

# from e2c_controller_cat import NormalDistribution

torch.set_default_dtype(torch.float32)
# torch.set_default_tensor_type('torch.cuda.FloatTensor')

'''
modify e2c to concatenate img and m,
which are taken care of in Encoder_cat, Decoder_cat
'''

# copy from e2c_controller_cat
class NormalDistribution(object):
	"""
	Wrapper class representing a multivariate normal distribution parameterized by
	N(mu,Cov). If cov. matrix is diagonal, Cov=(sigma).^2. Otherwise,
	Cov=A*(sigma).^2*A', where A = (I+v*r^T).
	"""

	def __init__(self, mu, sigma, logsigma, *, v=None, r=None):
		self.mu = mu
		self.sigma = sigma
		self.logsigma = logsigma
		self.v = v
		self.r = r

	@property
	def cov(self):
		"""This should only be called when NormalDistribution represents one sample"""
		if self.v is not None and self.r is not None:
			assert self.v.dim() == 1
			dim = self.v.dim()
			v = self.v.unsqueeze(1)  # D * 1 vector
			rt = self.r.unsqueeze(0)  # 1 * D vector
			A = torch.eye(dim) + v.mm(rt)
			return A.mm(torch.diag(self.sigma.pow(2)).mm(A.t()))
		else:
			return torch.diag(self.sigma.pow(2))

class Encoder_cat(nn.Module):
	def __init__(self, dim_in, dim_out, dim_m):
		'''
		dim_in, dim_out: dimensions config for the image encoder
		dim_m: the dimension of measurements that are simply passed through a constant layeer
		'''
		super(Encoder_cat, self).__init__()

		self.enc = nn.Sequential(
			nn.Linear(dim_in, 150),
			nn.BatchNorm1d(150),
			nn.ReLU(),
			nn.Linear(150, 150),
			nn.BatchNorm1d(150),
			nn.ReLU(),
			nn.Linear(150, 150),
			nn.BatchNorm1d(150),
			nn.ReLU(),
			nn.Linear(150, dim_out*2)
		)

		# for m, directly copy m and set var to a constant, see in forward

	def forward(self, img, m):
		'''
		separate the module into two parts,
		one is derived from original encoder network to process img x
		the other is a constant linear layer to pass measurement m
		'''
		img_mean, img_var = self.enc(img).chunk(2, dim=1)
		m_mean = m
		m_var = torch.Tensor(m.size()).uniform_(0.1, 0.9)
		# concate the tensors
		mean = torch.cat([img_mean, m_mean], dim=1)
		var = torch.cat([img_var, m_var], dim=1)

		return mean, var # otherwise just return tv

class Decoder_cat(nn.Module):
	def __init__(self, dim_in, dim_out, dim_m):
		super(Decoder_cat, self).__init__()
		self.dim_in = dim_in
		self.dim_out = dim_out
		self.dim_m = dim_m

		self.dec = nn.Sequential(
			nn.Linear(self.dim_in, 200),
			nn.BatchNorm1d(200),
			nn.ReLU(),
			nn.Linear(200, 200),
			nn.BatchNorm1d(200),
			nn.ReLU(),
			nn.Linear(200, self.dim_out),
			nn.BatchNorm1d(self.dim_out), # TODO: why add BN, SM layers compared to encoder?
			nn.Sigmoid()
		)

		self.cat = nn.Identity() # a constant layer linear function

	def forward(self, z):
		# unevenly split the tensor
		z_img, z_m = torch.split(z, (self.dim_in, self.dim_m), dim=1)
		# TODO: concatenate img_dec and m_dec
		return torch.cat([self.dec(z_img), self.cat(z_m)], dim=1)

# copy from e2c_configs
class Transition_cat(nn.Module):
	def __init__(self, dim_z, dim_u):
		'''
		dim_z: total dimension of concatenated img and m
		dim_u: dimension of action
		'''
		super(Transition_cat, self).__init__()
		self.dim_z = dim_z
		self.dim_u = dim_u

		self.trans = nn.Sequential(
			nn.Linear(dim_z, 100),
			nn.BatchNorm1d(100),
			nn.ReLU(),
			nn.Linear(100, 100),
			nn.BatchNorm1d(100),
			nn.ReLU(),
			nn.Linear(100, dim_z*2)
		)

		self.fc_B = nn.Linear(dim_z, dim_z * dim_u)
		self.fc_o = nn.Linear(dim_z, dim_z)

	def forward(self, h, Q, u):
		# h is mean, Q is var, u is action
		batch_size = h.size()[0]
		# torch chunk method: split into 2 chunks
		v, r = self.trans(h).chunk(2, dim=1)
		v1 = v.unsqueeze(2)
		rT = r.unsqueeze(1)
		I = Variable(torch.eye(self.dim_z).repeat(batch_size, 1, 1))
		if rT.data.is_cuda:
			I.dada.cuda()
		A = I.add(v1.bmm(rT))
		B = self.fc_B(h).view(-1, self.dim_z, self.dim_u)
		o = self.fc_o(h)

		# need to compute the parameters for distributions
		# as well as for the samples
		u = u.unsqueeze(2)
		# Q.mu.unsqueeze(2) (z^bar): torch.Size([128, 100, 1])
		d = A.bmm(Q.mu.unsqueeze(2)).add(B.bmm(u)).add(o.unsqueeze(2)).squeeze(2)
		# print("d", d.size()) # ([128, 100])
		sample = A.bmm(h.unsqueeze(2)).add(B.bmm(u)).add(o.unsqueeze(2)).squeeze(2)

		return sample, NormalDistribution(d, Q.sigma, Q.logsigma, v=v, r=r)

class E2C_cat(nn.Module):

	def __init__(self, dim_img, dim_img_lat, dim_m, dim_u):

		super(E2C_cat, self).__init__()
		self.dim_img = dim_img
		self.dim_img_lat = dim_img_lat
		self.dim_m = dim_m
		self.dim_z = self.dim_img_lat + self.dim_m
		self.dim_u = dim_u

		self.encoder = Encoder_cat(self.dim_img, self.dim_img_lat, self.dim_m)
		self.decoder = Decoder_cat(self.dim_img_lat, self.dim_img, self.dim_m)
		self.trans = Transition_cat(self.dim_z, self.dim_u)

	def encode(self, img, m):
		return self.encoder(img, m)  # return two: z_mean, z_var

	def decode(self, z):
		return self.decoder(z)

	def transition(self, z, Qz, u):
		# TODO
		return self.trans(z, Qz, u)

	def reparam(self, mean, logvar):
		# reparameterization trick for inference model
		std = logvar.mul(0.5).exp_()
		self.z_mean = mean
		self.z_sigma = std
		eps = torch.FloatTensor(std.size()).normal_()
		if std.data.is_cuda:
			eps.cuda()
		eps = Variable(eps)
		return eps.mul(std).add_(mean), NormalDistribution(mean, std, torch.log(std))

	def forward(self, img, m, u, img_next, m_next):
		self.img = img
		self.m = m
		self.u = u
		self.img_next = img_next
		self.m_next = m_next

		mean, logvar = self.encode(img, m)
		mean_next, logvar_next = self.encode(img_next, m_next)

		# simultaneously perform reparameterization on z (combined by img & m)
		self.z, self.Qz = self.reparam(mean, logvar)
		self.z_next, self.Qz_next = self.reparam(mean_next, logvar_next)
		# self.img_dec, self.m_dec = self.decode(self.z)
		self.x_dec = self.decode(self.z)
		# self.img_next_dec, self.m_next_dec = self.decode(self.z_next)
		self.x_next_dec = self.decode(self.z_next)
		self.z_next_pred, self.Qz_next_pred = self.transition(self.z, self.Qz, u)
		# self.img_next_pred_dec, self.m_next_pred_dec = self.decode(self.z_next_pred)
		self.x_next_pred_dec = self.decode(self.z_next_pred)
		# return self.img_next_pred_dec, self.m_next_pred_dec
		return self.x_next_pred_dec

	def latent_embeddings(self, img, m):
		return self.encode(img, m)[0]

	def predict(self, img, m, u):
		mean, logvar = self.encode(img, m)
		z, Qz = self.reparam(mean, logvar)
		z_next_pred, Qz_next_pred = self.transition(z, Qz, u)
		return self.decode(z_next_pred)

	def split_dec(self, z):
		return torch.split(self.decode(z), (self.dim_img, self.dim_m), dim=1) # return img_dec, m_dec

	def cat(self, img, m):
		return torch.cat([img, m], dim=1)

	def split(self, x_dec):
		return torch.split(x_dec, (self.dim_img, self.dim_m), dim=1)


"""
实现一个排序函数，通过参数指定排序函数用来事项不同顺序排序
"""

def new_sort(args, st=None):
    if not st:
        for i in range(len(args) - 1):
            for j in range(i + 1, len(args)):
                if args[i] < args[j]:
                    args[i], args[j] = args[j], args[i]
        return args
    else:
        return st(args)

def my_sorted(args):
    args = sorted(args)
    return args

if __name__ == '__main__':
    args = [1, 12, 3, 5, 4, 9]
    print(new_sort(args))
    print(new_sort(args, my_sorted))


file = "example_input.txt"
# file = "input.txt"

def SomeFunction(data):
    line = data[0]

    for i in range(len(line) - 14):
        if len(set(line[i:i + 14])) == 14:
            return i + 14

    return -1

if __name__ == "__main__":
    # reading in the input
    with open(file, "r") as f:
        inputs = f.readlines()

    print(SomeFunction(inputs))


# Copyright 2015 Hewlett-Packard Development Company, L.P.
# Copyright 2016 Rackspace Inc.
# Copyright 2017 F5 Networks
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import pytest

from tempest import config
from tempest.lib.common.utils import data_utils
from tempest import test

from f5lbaasdriver.test.tempest.tests.api import base

CONF = config.CONF

class CommonNetworks(base.F5BaseTestCase):
    """Test that f5_common_networks performs proper behavior"""
    timeout_for_agent_start = 5
    agent_ini_file = "/etc/neutron/services/f5/f5-openstack-agent.ini"

    @classmethod
    def resource_setup(cls):
        super(CommonNetworks, cls).resource_setup()
        cls.__re_init_agent(True)
        if not test.is_extension_enabled('lbaas', 'network'):
            msg = "lbaas extension not enabled!"
            raise cls.resource_setupskipException(msg)
        network_name = data_utils.rand_name('network')
        cls.network = cls.create_network(network_name)
        cls.subnet = cls.create_subnet(cls.network)

        cls.create_lb_kwargs = {'tenant_id': cls.subnet['tenant_id'],
                                'vip_subnet_id': cls.subnet['id']}
        cls.load_balancer = \
            cls._create_active_load_balancer(**cls.create_lb_kwargs)
        cls.load_balancer_id = cls.load_balancer['id']

        # create listener
        listener_kwargs = {'loadbalancer_id': cls.load_balancer_id,
                           'protocol': 'HTTP',
                           'protocol_port': 80}
        cls.listener = cls._create_listener(**listener_kwargs)
        cls.client = cls.plugin_rpc.get_client()
        cls.context = cls.plugin_rpc.get_context()

    def __re_init_agent(cls, f5_common_networks):
        """Performs action to modify the agent config

        This is a no-opt that is to be filled in on just how things will
        interact with the agent
        """
        pass

    @classmethod
    def resource_cleanup(cls):
        super(CommonNetworks, cls).resource_cleanup()
        cls.__re_init_agent(False)

    def construct_and_clean(cls, kw_args, construct_method, delete_method,
                            cleanup_args=None):
        obj = construct_method(**kw_args)
        args = [delete_method, obj.get('id')]
        if cleanup_args:
            # less costly than a splice...
            args = [delete_method]
            args.extend(cleanup_args)
            args.append(obj.get('id'))
        cls.addCleanup(*args)

    @pytest.mark.skip(reason="RBAC and dynamic agent not implemented yet")
    def test_common_networks(self):
        # create pool
        pool_wkargs = dict(listener_id=self.listener['id'],
                           protocol='HTTP',
                           lb_algorithm='ROUND_ROBIN')
        pool = self.construct_and_clean(pool_wkargs, self._create_pool,
                                        self._delete_pool)
        # add ping monitor
        monitor_kwargs = dict(pool_id=pool['id'], type='PING', delay=1,
                              timeout=1, max_retries=5)
        self.construct_and_clean(monitor_kwargs, self._create_health_monitor,
                                 self._delete_health_monitor)
        # create member:
        allocation_pool = self.subnet['allocation_pools'][0]
        member_kwargs = dict(pool_id=pool['id'], protocol_port=8080,
                             address=allocation_pool['start'],
                             subnet_id=self.subnet['id'])
        member = self.construct_and_clean(member_kwargs, self._create_member,
                                          self._delete_member,
                                          cleanup_args=[pool.get('id')])
        assert self._wait_for_member_status(member, 'ONLINE') == 'ONLINE'
        assert 'Common' in self.load_balancer['id']
        assert 'Common' in self.network


import sys
import pickle
import numpy as np
from transe import TRANSE

def load(filepath):
    S = set()
    for line in open(filepath):
        h,l,t = line.strip().split('\t')
        S.add((h,l,t))
    return S

modelfilepath = 'transe.model'
testfilepath = 'FB15k/freebase_mtr100_mte100-test.txt'

with open(modelfilepath) as f:
    transe,E,L = pickle.load(f)

S = load(testfilepath)

meanrank = 0.
hit10 = 0.
for h,l,t in S:
    dsim = transe.predict(E[h],L[l])
    meanrank += (dsim<dsim[E[t]]).sum()
    if E[t] in np.argpartition(dsim,10)[:10]:
        hit10 += 1
meanrank /= len(S)
hit10 /= len(S)

print("MeanRank: %s" % meanrank)
print("Hit10: %s" % hit10)


from constants import *
from icons import *
import PySimpleGUI as sg

class cDisplayer:
    def __init__(self, display):
        self.display = display
        self.lighted_squares = []

    def clear(self):
        for i in range(8):
            for j in range(8):
                if self.display[i][j].ImageData != empty_icon:
                    self.display[i][j].Update(image_data=empty_icon)
                    self.display[i][j].ImageData=empty_icon

    def load(self, board):
        for sqr in board.squares:
            icon = self._get_icon(sqr.piece)
            if icon != self.display[7-sqr.rowIdx][sqr.colIdx].ImageData:
                self.display[7-sqr.rowIdx][sqr.colIdx].Update(image_data=icon)
                self.display[7-sqr.rowIdx][sqr.colIdx].ImageData = icon

    def draw_square(self, sqr, piece):
        icon = self._get_icon(piece)
        self.display[7-sqr.rowIdx][sqr.colIdx].Update(image_data=icon)
        self.display[7-sqr.rowIdx][sqr.colIdx].ImageData = icon

    def light_squares(self, squares, intensity=1):
        self.lighted_squares += squares
        for sqr in squares:
            color = self._get_color(sqr, intensity)
            self.display[7-sqr.rowIdx][sqr.colIdx].Update(button_color=color)

    def unlight_squares(self):
        self.light_squares(self.lighted_squares, 0)
        self.lighted_squares = []

    def _get_icon(self, piece):
        if piece is None:
            return empty_icon
        elif piece.color == WHITE:
            if piece.kind == PAWN:
                return white_pawn_icon
            elif piece.kind == KNIGHT:
                return white_knight_icon
            elif piece.kind == BISHOP:
                return white_bishop_icon
            elif piece.kind == ROOK:
                return white_rook_icon
            elif piece.kind == QUEEN:
                return white_queen_icon
            else:
                return white_king_icon
        else:
            if piece.kind == PAWN:
                return black_pawn_icon
            elif piece.kind == KNIGHT:
                return black_knight_icon
            elif piece.kind == BISHOP:
                return black_bishop_icon
            elif piece.kind == ROOK:
                return black_rook_icon
            elif piece.kind == QUEEN:
                return black_queen_icon
            else:
                return black_king_icon

    def _get_color(self, sqr, intensity):
        if (sqr.rowIdx + sqr.colIdx) % 2 == 0:
            #dark square
            if intensity == 0:
                return COLOR_BG_DARK_BASIC
            elif intensity == 1:
                return COLOR_BG_DARK_HLIGHTED_1
            elif intensity == 2:
                return COLOR_BG_DARK_HLIGHTED_2
        else:
            #light square
            if intensity == 0:
                return COLOR_BG_LIGHT_BASIC
            elif intensity == 1:
                return COLOR_BG_LIGHT_HLIGHTED_1
            elif intensity == 2:
                return COLOR_BG_LIGHT_HLIGHTED_2

    def get_promoted_piece_from_dialog(self):
        promote_window = sg.Window('what piece do you want', [[sg.Button('Queen'), sg.Button('Rook'), sg.Button('Bishop'), sg.Button('Knight')]])
        piece, _  = promote_window.read()
        promote_window.close()
        if piece == 'Queen':
            return QUEEN
        elif piece == 'Rook':
            return ROOK
        elif piece == 'Bishop':
            return BISHOP
        else:
            return KNIGHT

    def inform(self, msg):
        sg.popup(msg)


from django.contrib.auth import get_user_model

from rest_framework import serializers
from core.models import Reward, StudentReward

class RewardSerializer(serializers.ModelSerializer):
    """Serializer for reward objects"""

    class Meta:
        model = Reward
        fields = ('id', 'reward', 'description', 'created_at', 'updated_at', 'image',)
        read_only_fields = ('id', 'created_at', 'updated_at')

class StudentRewardSerializer(serializers.ModelSerializer):
    """Serializer for studentreward objects"""
    student = serializers.PrimaryKeyRelatedField(
        many=False,
        queryset=get_user_model().objects.all()
    )
    reward = serializers.PrimaryKeyRelatedField(
        many=False,
        queryset=Reward.objects.all()
    )

    class Meta:
        model = StudentReward
        fields = ('id', 'student', 'reward', 'notes', 'created_at', 'updated_at')
        read_only_fields = ('id', 'created_at', 'updated_at')

class RewardImageSerializer(serializers.ModelSerializer):
    """Serializers for uploading images to rewards"""

    class Meta:
        model = Reward
        fields = ('id', 'image')
        read_only_fields = ('id',)


# -*- coding: utf-8 -*-
"""
:Purpose:   This class module is used to convert a dictionary (or JSON
            file) into a object - where the dictionary's key/value
            pairs become object attributes.

:Platform:  Linux/Windows | Python 3.5
:Developer: J Berendt
:Email:     support@73rdstreetdevelopment.co.uk

:Comments:  Basic concept `attribution`_.

.. _attribution: https://stackoverflow.com/a/1639197/6340496

"""

import os
import json

# ALLOW SMALL CLASS
# pylint: disable=too-few-public-methods

class Dict2Obj(object):
    """
    Create a Python object from a standard dictionary, or JSON file.

    Args:
        dictionary (dict): A standard Python dictionary where all
            key/value pairs will be converted into an object.
        source (str): Source for the conversion.

            * 'dict': a standard Python dictionary
            * 'json': uses content from a JSON file

        filepath (str): Full file path to the JSON file to be used.

    :Design:
        A Python object is created from the passed dictionary (or JSON
        file), where each of the dictionary's key/value pairs is turned
        into an object attribute/value pair.

    Note:
        1) The dictionary or JSON file **must be in a flat format**.
        In other words, functionality **does not support** nested
        dictionaries and/or lists.

        2) This can be useful when loading a JSON config file into
        memory, as you can then access it like an object, rather than a
        dictionary.

    :Example:
        To convert a dictionary into an object::

            from utils3.dict2obj import Dict2Obj

            # A SAMPLE DICTIONARY
            d = dict(a=1, b=2, title='This is a title.')
            # CONVERT IT
            my_obj = Dict2Obj(dictionary=d)
            # TEST IT
            print(my_obj.title)
            > This is a title.

    """
    def __init__(self, dictionary=None, source='dict', filepath=None):
        """Class initialiser."""
        self._dictionary = dictionary
        self._source = source
        self._filepath = filepath

        # VALIDATE PARAMETERS
        if self._validate(source=self._source, filepath=self._filepath):

            # TEST SOURCE OF DICT
            if self._source.lower() == 'json':
                # READ FROM JSON FILE
                dict_ = self._read_json(filepath=self._filepath)
            else:
                # CREATE OBJECT FROM PASSED DICTIONARY
                dict_ = self._dictionary

            # LOOP THROUGH THE DICT AND SET CLASS ATTRIBUTES
            for k, v in dict_.items(): setattr(self, k, v)

    def _validate(self, source, filepath) -> bool:
        """
        Run the following validation tests:

            * The 'source' value is valid
            * If 'json' source, a file path is provided
            * If 'json' source, the provided file path exists

        Args:
            source (str): Source for the conversion.
                (i.e. 'dict' or 'json')
            filepath (str): Full file path to the JSON file.

        Returns:
            True if **all** tests pass, otherwise False.

        """
        success = self._validate_source_value(source=source)
        if success: success = self._validate_source(source=source, filepath=filepath)
        if success: success = self._validate_fileexists(source=source, filepath=filepath)
        return success

    @staticmethod
    def _validate_fileexists(source, filepath) -> bool:
        """
        Validation test: If a 'json' source, test the file path exists.

        Args:
            source (str): Source for the conversion.
                (i.e. 'dict' or 'json')
            filepath (str): Full file path to the JSON file.

        Returns:
            True if the source is 'dict'; or if source is 'json' and
            the file exists, otherwise False.

        """
        success = False
        # ONLY TEST IF SOURCE IS JSON
        if source.lower() == 'json':
            if all([source.lower() == 'json', os.path.exists(filepath)]):
                success = True
            else:
                print('The file provided does not exist:\n(%s)' % (filepath))
        else:
            success = True
        return success

    @staticmethod
    def _validate_source(source, filepath) -> bool:
        """
        Validation test: If a 'json' source, test a file path is
        provided.

        Args:
            source (str): Source for the conversion.
                (i.e. 'dict' or 'json')
            filepath (str): Full file path to the JSON file.

        Returns:
            True if the source is 'dict'; or if source is 'json' and a
            file path is provided, otherwise False.

        """
        success = False
        if all([source.lower() == 'json', filepath is None]):
            print('A file path must be provided for the JSON file.')
        else:
            success = True
        return success

    @staticmethod
    def _validate_source_value(source) -> bool:
        """
        Validation test: The value of the ``source`` parameter is valid.

        Args:
            source (str): Source for the conversion.
                (i.e. 'dict' or 'json')

        Returns:
            True if a valid source, otherwise False.

        """
        source_values = ['dict', 'json']
        success = False
        if source in ['dict', 'json']:
            success = True
        else:
            print('The source provided (\'%s\') is invalid.  Valid options are: %s.' %
                  (source, source_values))
        return success

    @staticmethod
    def _read_json(filepath) -> dict:
        """
        Read values from a JSON file into a dictionary.

        Args:
            filepath (str): Full file path to the JSON file.

        Returns:
            A dictionary containing the JSON data.

        """
        with open(filepath) as f:
            data = json.loads(f.read())
        return data


import sonnet as snt
import tensorflow as tf

def conv_block(channels):
    return snt.Sequential([
        snt.Conv2D(channels, 3), tf.nn.relu,
        snt.Conv2D(channels, 3), tf.nn.relu
    ])

modules = {'conv_block': conv_block}

class ResNet(snt.Module):
    def __init__(self, initial_trans, modules):
        self._initial_trans = initial_trans
        self._res_modules = [snt.Residual(m) for m in modules]

    def _all_reprs(self, inputs):
        x = self._initial_trans(inputs)
        reprs = [x]
        for module in self._res_modules:
            x = module(x)
            reprs.append(x)
        return reprs

    def _build(self, inputs):
        return self._all_reprs(inputs)[-1]


from app import db
from flask_wtf import FlaskForm
from wtforms import Form, FormField, IntegerField, SelectField, FieldList, DecimalField, StringField, widgets, SelectMultipleField
from wtforms.validators import DataRequired, Length, Optional
from app.web.common.forms.addressForm import AddressForm
from app.web.criteria.forms.criteria import CriteriaForm
from app.web.criteria.models.investment_criteria import InvestmentCriteria
from app.web.util.enum.property_type import PropertyType

class ContactForm(FlaskForm):
    first_name = StringField('first_name', validators=[Optional()])
    last_name = StringField('last_name', validators=[Optional()])
    email = StringField('email', validators=[Optional()])
    phone_number = StringField('phone', validators=[Optional()])
    contact_type = SelectField('type', choices=[("",""),
                                                ("Investor", "Investor"),
                                                ("Builder", "Builder"),
                                                ("Wholesaler", "Wholesaler"),
                                                ("Realtor", "Realtor"),
                                                ("Property Manager", "Property Manager"),
                                                ("Lender", "Lender"),
                                                ("Other Professional", "Other Professional")],
                                                validators=[Optional()])
    investment_strategy = SelectField('type', choices=[("",""),
                                                        ("TBD", "TBD"),
                                                        ("Rentals", "Rentals"),
                                                        ("Fix and Flips", "Fix and Flips"),
                                                        ("Land (Builder)", "Land (Builder)")],
                                                        validators=[Optional()])

    investment_criteria = SelectMultipleField('Label', choices=PropertyType.choices(),
                                                            option_widget = widgets.CheckboxInput(),
                                                            widget = widgets.ListWidget(prefix_label=False),
                                                            coerce=PropertyType.coerce)

    #investment_criteria = FieldList(FormField(CriteriaForm))

    def populate_obj(self, contact):
        profiles = []
        for profile in self.investment_criteria.data:
            new_profile = InvestmentCriteria()
            new_profile.property_type = profile
            profiles.append(new_profile)
        self.investment_criteria.data = profiles
        super(FlaskForm, self).populate_obj(contact)


import datetime, json, time, oauth2

def oauth_request(config, url, http_method, post_body, http_headers):
  application_token = config["application_token"]
  application_secret = config["application_secret"]
  consumer_token = config["consumer_token"]
  consumer_secret = config["consumer_secret"]

  consumer = oauth2.Consumer(consumer_token, consumer_secret)
  client = oauth2.Client(consumer, oauth2.Token(application_token, application_secret))
  response, content = client.request(url, http_method, post_body, http_headers)
  return response, content

def twitter_get(config, url):
  return oauth_request(config, "https://api.twitter.com/1.1/" + url, "GET", "", None)

def store(output_file, config, url):
  response, content = twitter_get(config, url)
  output_file.write(url)
  output_file.write("\n")
  output_file.flush()
  output_file.write(str(response))
  output_file.write("\n")
  output_file.flush()
  output_file.write(str(content))
  output_file.write("\n")
  output_file.flush()

def timestamp():
  return datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S")

get_urls = [
  "statuses/mentions_timeline",
  "statuses/user_timeline",
  "statuses/home_timeline",
  "statuses/retweets_of_me",
  "statuses/retweets/:id",
  "statuses/show/:id",
  "statuses/oembed",
  "statuses/retweeters/ids",
  "statuses/lookup",
  "direct_messages/sent",
  "direct_messages/show",
  "search/tweets",
  "direct_messages",
  "friendships/no_retweets/ids",
  "friends/ids",
  "followers/ids",
  "friendships/incoming",
  "friendships/outgoing",
  "friendships/show",
  "friends/list",
  "followers/list",
  "friendships/lookup",
  "account/settings",
  "account/verify_credentials",
  "blocks/list",
  "blocks/ids",
  "users/lookup",
  "users/show",
  "users/search",
  "users/profile_banner",
  "mutes/users/ids",
  "mutes/users/list",
  "users/suggestions/:slug",
  "users/suggestions",
  "users/suggestions/:slug/members",
  "favorites/list",
  "lists/list",
  "lists/statuses",
  "lists/memberships",
  "lists/subscribers",
  "lists/subscribers/show",
  "lists/members/show",
  "lists/members",
  "lists/show",
  "lists/subscriptions",
  "lists/ownerships",
  "saved_searches/list",
  "saved_searches/show/:id",
  "geo/id/:place_id",
  "geo/reverse_geocode",
  "geo/search",
  "trends/place",
  "trends/available",
  "application/rate_limit_status",
  "help/configuration",
  "help/languages",
  "help/privacy",
  "help/tos",
  "trends/closest"
]

if __name__ == "__main__":
  root_folder = "json"
  config_path = root_folder + "/" + "config.json"
  output_path = root_folder + "/" + timestamp() + ".json"

  with open(config_path, "rb") as config_file:
    config = json.load(config_file)
    with open(output_path, "wb") as output_file:
      for url in get_urls:
        if ":" in url:
          continue
        url += ".json"
        store(output_file, config, url)
        time.sleep(1)


import graphene
from graphene_django import DjangoObjectType
from graphene_django.filter import DjangoFilterConnectionField
from graphene.relay.node import from_global_id

from api import utils
from pos_store import forms, models

class AvailableItem(DjangoObjectType):
    class Meta:
        model = models.AvailableItem
        interfaces = [graphene.relay.Node]
        filter_fields = ["purchasable_item"]
        fields = ["purchasable_item"]

class AvailableItemArguments(utils.GraphqlModelFormMutation):
    class Meta:
        abstract = True

    def clean(self, **input):

        purchasable_item = input.get("purchasable_item")
        store = input.get("store")

        if store and purchasable_item:
            purchasable_item = int(from_global_id(purchasable_item)[1])
            store = int(from_global_id(store)[1])
            input["store"] = store
            input["purchasable_item"] = purchasable_item

class AddAvailableItem(AvailableItemArguments):

    class Meta:
        form_class = forms.AddAvailableItem
        exclude_fields = ["id"]

class Query:

    available_items = DjangoFilterConnectionField(AvailableItem, store=graphene.ID(required=True))

    def resolve_available_items(self, info, **args):
        try:
            store = args.get("store")
            store = int(from_global_id(store)[1])
            return models.AvailableItem.objects.filter(store=store).all()
        except Exception as e:
            raise Exception("Store ID not valid.")

class Mutation:
    pass


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Oct 16 20:41:24 2017

@author: adrienne
"""
#%% SET-UP

import os
entry_point = r'/home/adrienne/Code/PythonProjects/CFPB/src'
os.chdir(entry_point)

#%% GET ONTOLOGY

from owlready2 import *
onto_path.append(r'/home/adrienne/Code/PythonProjects/CFPB/ontologies')
onto = get_ontology("http://www.lesfleursdunormal.fr/static/_downloads/pizza_onto.owl")
onto.load()

#%% EXPERIMENT

list(onto.classes())
list(onto.individuals())
list(onto.object_properties())
list(onto.data_properties())
list(onto.annotation_properties())
list(onto.disjoint_classes())

# searching for all entities with an IRI ending with ‘Topping’:
onto.search(iri = "*Topping")

# searches for all entities that are related to another one with the ‘has_topping’ relation:
onto.search(has_topping = "*")

#%% SPARQL QUERY WITH RDFLIB

from SPARQLWrapper import SPARQLWrapper, JSON

# Query attempt #1
sparql = SPARQLWrapper("http://dbpedia.org/sparql")  # Goes to Virtuoso SPARQL Query Editor site
sparql.setQuery("""
    PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
    SELECT ?label
    WHERE { <http://dbpedia.org/resource/Asturias> rdfs:label ?label }
""")
sparql.setReturnFormat(JSON)
results = sparql.query().convert()

for result in results["results"]["bindings"]:
    print(result["label"]["value"])

# Query attempt #2
sparql.setQuery("""
                PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
                SELECT *
                  WHERE
                    {
                      ?e <http://dbpedia.org/ontology/series>         <http://dbpedia.org/resource/The_Sopranos>  .
                      ?e <http://dbpedia.org/ontology/releaseDate>    ?date                                       .
                      ?e <http://dbpedia.org/ontology/episodeNumber>  ?number                                     .
                      ?e <http://dbpedia.org/ontology/seasonNumber>   ?season
                    }
                  ORDER BY DESC(?date)
                  """)

sparql.setReturnFormat(JSON)
results = sparql.query().convert()

for result in results["results"]["bindings"]:
    print('Name: ' + result['e']['value'])
    print('Season ' + result['season']['value'] + ', Episode ' + result['number']['value'])
    print('Release date: ' + result['date']['value'])
    print()


__all__ = ["Person"]

def _setState(state, val, default=None):
    try:
        result = state[val]
        if result:
            return result
        else:
            return default
    except KeyError:
        return default

def _mergeName(old, new, key):
    if len(":".join(new[key])) > len(":".join(old[key])):
        old[key] = new[key]

def _mergeNames(old, new):
    for key in ["titles", "firstnames", "surnames", "suffixes"]:
        _mergeName(old, new, key)

    if old["orig_name"] != new["orig_name"]:
        old["orig_name"] = f"{old['orig_name']} or {new['orig_name']}"

def _mergeProjects(old, new, key):
    old = old[key]
    new = new[key]

    for project, values in new.items():
        if project in old:
            for value in values:
                if value not in old[project]:
                    old[project].append(value)
        else:
            old[project] = values

class Person:
    """Holds information about a Person in the network"""
    def __init__(self, props=None, getHook=None):
        self._getHook = getHook

        self.state = {
            "titles": [],
            "firstnames": [],
            "surnames": [],
            "suffixes": [],
            "id": None,
            "positions": {},
            "affiliations": {},
            "projects": {},
            "sources": {},
            "alive": None,
            "gender": None,
            "notes": [],
            "orig_name": None,
        }

        self.setState(props)

    def merge(self, other):
        import copy as _copy
        state = _copy.copy(self.state)

        _mergeNames(state, other.state)

        _mergeProjects(state, other.state, "positions")
        _mergeProjects(state, other.state, "affiliations")
        _mergeProjects(state, other.state, "sources")

        for project, dates in other.state["projects"].items():
            state["projects"][project] = dates

        p = Person()
        p.state = state
        p._getHook = self._getHook

        return p

    def __str__(self):
        return f"Person({self.getName()})"

    def __repr__(self):
        return self.__str__()

    def getID(self):
        return self.state["id"]

    def couldBe(self, other):
        if self.getSurname() != other.getSurname():
            return False

        if self.getInitials() == other.getInitials():
            return True

        if self.firstInitial() == other.firstInitial():
            return True

        return False

    def getFirstName(self):
        try:
            return " ".join(self.state["firstnames"])
        except KeyError:
            return None

    def firstInitial(self):
        try:
            for name in self.state["firstnames"]:
                return name[0].upper()
        except Exception:
            pass

        return None

    def getInitials(self):
        initials = []

        try:
            for name in self.state["firstnames"]:
                initials.append(name[0].upper())
        except Exception:
            pass

        return f"{'.'.join(initials)}."

    def getTitle(self):
        try:
            return " ".join(self.state["titles"])
        except KeyError:
            return None

    def getSurname(self):
        try:
            return " ".join(self.state["surnames"])
        except KeyError:
            return None

    def getSuffix(self):
        try:
            return " ".join(self.state["suffixes"])
        except KeyError:
            return None

    def getName(self):
        parts = []

        for part in [self.getTitle, self.getFirstName,
                     self.getSurname, self.getSuffix]:
            n = part()
            if n:
                parts.append(n)

        return " ".join(parts)

    def getPositions(self):
        result = {}

        positions = self.state["positions"]

        for project in positions.keys():
            if project not in result:
                result[project] = []

            for position in positions[project]:
                result[project].append(self._getHook(position))

        return result

    def getAffiliations(self):
        result = {}

        affiliations = self.state["affiliations"]

        for project in affiliations.keys():
            if project not in result:
                result[project] = []

            for affiliation in affiliations[project]:
                result[project].append(self._getHook(affiliation))

        return result

    def getBorn(self):
        try:
            return self.state["alive"].getStart()
        except KeyError:
            return None

    def getDied(self):
        try:
            return self.state["alive"].getEnd()
        except KeyError:
            return None

    def getLifeTime(self):
        try:
            return self.state["alive"]
        except KeyError:
            return None

    def setState(self, state):
        if not state:
            return

        self.state["suffixes"] = _setState(state, "suffixes", [])
        self.state["surnames"] = _setState(state, "surnames", [])
        self.state["firstnames"] = _setState(state, "firstnames", [])
        self.state["titles"] = _setState(state, "titles", [])
        self.state["id"] = _setState(state, "id")
        self.state["positions"] = _setState(state, "positions", {})
        self.state["affiliations"] = _setState(state, "affiliations", {})
        self.state["projects"] = _setState(state, "projects", {})
        self.state["sources"] = _setState(state, "sources", {})
        self.state["alive"] = _setState(state, "alive")
        self.state["gender"] = _setState(state, "gender")
        self.state["orig_name"] = _setState(state, "orig_name")
        self.state["notes"] = _setState(state, "notes", [])

        if self.state["orig_name"] == "None" or \
           self.state["orig_name"] is None:
            raise ValueError(f"No name for {self}?")

    def toDry(self):
        return self.state

    @staticmethod
    def unDry(value):
        return Person(value)

    @staticmethod
    def load(data):
        return Person(data)


def get_next_3(arr, ind):
    res = list()
    for _ in range(3):
        if ind + 1 >= len(arr):
            res.append(arr.pop(0))
        else:
            res.append(arr.pop((ind + 1)))
    return res

def select_destination(arr, num):
    for diff in range(1, 10):
        n = (num-diff) % 10
        if n in arr:
            return n

cups = list(map(int, list('496138527')))

current_ind = 0

for move in range(100):
    # print("move: ", move+1)
    current_num = cups[current_ind % len(cups)]
    next_3 = get_next_3(cups, current_ind % len(cups))
    destination_num = select_destination(cups, current_num)
    # print(current_num, destination_num)
    # print(cups, next_3)
    destination_ind = cups.index(destination_num)
    cups = cups[:destination_ind+1] + next_3 + cups[destination_ind+1:]
    # print(cups)
    current_ind = (cups.index(current_num) + 1) % len(cups)

ind_1 = cups.index(1)
print(''.join(map(str, cups[ind_1+1:] + cups[:ind_1])))


class ContactInfo:
    def __init__(self, name, email):
        self.name = name
        self.email = email

    def print_info(self):
        print('{0} : {1}'.format(self.name, self.email))

if __name__ == '__main__':
     hong = ContactInfo('홍길동','aaa@aaa.com')
     lee = ContactInfo('이순신', 'bbb@bbb.com')

     hong.print_info()
     lee.print_info()


import json
import os

class kegman_conf():
  def __init__(self, CP=None):
    self.conf = self.read_config()
    if CP is not None:
      self.init_config(CP)

  def init_config(self, CP):
    write_conf = False
    if self.conf['tuneGernby'] != "1":
      self.conf['tuneGernby'] = str(1)
      write_conf = True

    # only fetch Kp, Ki, Kf sR and sRC from interface.py if it's a PID controlled car
    if CP.lateralTuning.which() == 'pid':
      if self.conf['Kp'] == "-1":
        self.conf['Kp'] = str(round(CP.lateralTuning.pid.kpV[0],3))
        write_conf = True
      if self.conf['Ki'] == "-1":
        self.conf['Ki'] = str(round(CP.lateralTuning.pid.kiV[0],3))
        write_conf = True
      if self.conf['Kf'] == "-1":
        self.conf['Kf'] = str('{:f}'.format(CP.lateralTuning.pid.kf))
        write_conf = True
      if self.conf['Kp2'] == "-1":
        self.conf['Kp2'] = str(round(CP.lateralTuning.pid.kpV[0],3))
        write_conf = True
      if self.conf['Ki2'] == "-1":
        self.conf['Ki2'] = str(round(CP.lateralTuning.pid.kiV[0],3))
        write_conf = True
      if self.conf['Kf2'] == "-1":
        self.conf['Kf2'] = str('{:f}'.format(CP.lateralTuning.pid.kf))
        write_conf = True

    if self.conf['steerRatio'] == "-1":
      self.conf['steerRatio'] = str(round(CP.steerRatio,3))
      write_conf = True

    if self.conf['steerRateCost'] == "-1":
      self.conf['steerRateCost'] = str(round(CP.steerRateCost,3))
      write_conf = True

    if write_conf:
      self.write_config(self.config)

  def read_config(self):
    self.element_updated = False

    if os.path.isfile('/data/kegman.json'):
      with open('/data/kegman.json', 'r') as f:
        self.config = json.load(f)

      if "battPercOff" not in self.config:
        self.config.update({"battPercOff":"100"})
        self.config.update({"carVoltageMinEonShutdown":"11800"})
        self.element_updated = True

      if "tuneGernby" not in self.config:
        self.config.update({"tuneGernby":"1"})
        self.config.update({"deadzone":"0.0"})

      if "Kp" not in self.config:
        self.config.update({"Kp":"-1"})
        self.config.update({"Ki":"-1"})
        self.config.update({"Kf":"-1"})
        self.element_updated = True

      if "Kp2" not in self.config:
        self.config.update({"Kp2":"-1"})
        self.config.update({"Ki2":"-1"})
        self.config.update({"Kf2":"-1"})
        self.element_updated = True

      if "steerRatio" not in self.config:
        self.config.update({"steerRatio":"-1"})
        self.config.update({"steerRateCost":"-1"})
        self.element_updated = True

      if "sR_boost" not in self.config:
        self.config.update({"sR_boost":"0"})
        self.config.update({"sR_BP0":"0"})
        self.config.update({"sR_BP1":"0"})
        self.config.update({"sR_time":"1"})
        self.element_updated = True

      if "sR_Kp" not in self.config:
        self.config.update({"sR_Kp":"0.25"})
        self.config.update({"sR_Ki":"0.05"})
        self.config.update({"sR_Kf":"0.00005"})
        self.element_updated = True

      if "sR_Kp2" not in self.config:
        self.config.update({"sR_Kp2":"0.25"})
        self.config.update({"sR_Ki2":"0.05"})
        self.config.update({"sR_Kf2":"0.00005"})
        self.element_updated = True

      if "cV_Ratio" not in self.config:
        self.config.update({"cV_Ratio":"0.7"})
        self.element_updated = True

      if "cV_Dist" not in self.config:
        self.config.update({"cV_Dist":"-5"})
        self.element_updated = True

      if self.element_updated:
        print("updated")
        self.write_config(self.config)

    else:
      self.config = {"cameraOffset":"0.06", "battChargeMin":"70", "battChargeMax":"80", \
                    "wheelTouchSeconds":"3600", "battPercOff":"100", "carVoltageMinEonShutdown":"11800", \
                    "tuneGernby":"1", "deadzone":"0.0",\
                    "cv_Ratio":"0.7", "cv_Dist":"-5",\
                    "Kp":"-1", "Ki":"-1", "Kf":"-1",  \
                    "Kp2":"-1", "Ki2":"-1", "Kf2":"-1",  \
                    "steerRatio":"-1", "steerRateCost":"-1", \
                    "sR_boost":"0", "sR_BP0":"0", "sR_BP1":"0", "sR_time":"1", \
                    "sR_Kp":"0.25", "sR_Ki":"0.05", "sR_Kf":"0.00005", \
                    "sR_Kp2":"0.25", "sR_Ki2":"0.05", "sR_Kf2":"0.00005"}

      self.write_config(self.config)
    return self.config

  def write_config(self, config):
    try:
      with open('/data/kegman.json', 'w') as f:
        json.dump(self.config, f, indent=2, sort_keys=True)
        os.chmod("/data/kegman.json", 0o764)
    except IOError:
      os.mkdir('/data')
      with open('/data/kegman.json', 'w') as f:
        json.dump(self.config, f, indent=2, sort_keys=True)
        os.chmod("/data/kegman.json", 0o764)


import datetime
from flask import (
    Blueprint,
    render_template,
    request,
    redirect,
    url_for,
    g,
    session,
    current_app,
    jsonify
)
from flask.ext.login import login_user, logout_user, current_user, login_required
from app import db, bcrypt, login_manager
from app.forms import PatientForm, PrescreenForm, ScreeningResultForm
from app.models import (
    AppUser,
    Patient,
    PhoneNumber,
    Address,
    Employer,
    IncomeSource,
    HouseholdMember,
    DocumentImage,
    EmergencyContact,
    Service,
    ActionLog,
    PatientReferral,
    SlidingScale,
    PatientScreeningResult
)
from app.prescreening import calculate_fpl, calculate_pre_screen_results
from app.utils import upload_file, send_document_image, translate_object
from itertools import chain
from sqlalchemy import and_, or_
from werkzeug.datastructures import FileStorage

screener = Blueprint('screener', __name__, url_prefix='')

@screener.before_request
def before_request():
    g.user = current_user

@screener.route("/login", methods=["GET", "POST"])
def login():
    """Display login page and check credentials."""
    if request.method == 'POST':
        user = AppUser.query.filter(AppUser.email == request.form['email']).first()
        if user:
            if bcrypt.check_password_hash(
                user.password.encode('utf8'),
                request.form['password']
            ):
                user.authenticated = True
                db.session.add(user)
                db.session.commit()
                login_user(user, remember=True)
                return redirect(url_for('screener.index'))
            else:
                return redirect(url_for('screener.login'))
        else:
            return redirect(url_for('screener.login'))
    else:
        return render_template("login.html")

@screener.route("/logout", methods=["GET"])
@login_required
def logout():
    """Log the user out and redirect to login page."""
    user = current_user
    user.authenticated = False
    db.session.add(user)
    db.session.commit()
    logout_user()
    return redirect(url_for('screener.login'))

@login_manager.user_loader
def load_user(email):
    """Look up the user by email address."""
    return AppUser.query.filter(AppUser.email == email).first()

@screener.route('/new_patient', methods=['POST', 'GET'])
@login_required
def new_patient():
    """Display the form for a new patient, and create a new patient after submission."""
    form = PatientForm()

    if form.validate_on_submit():
        patient = Patient()
        update_patient(patient, form, request.files)
        db.session.add(patient)
        db.session.commit()
        return redirect(url_for('screener.patient_details', id=patient.id))
    else:
        return render_template('patient_details.html', patient={}, form=form)

@screener.route('/patient_details/<id>', methods=['POST', 'GET'])
@login_required
def patient_details(id):
    """Display the full patient details form for an existing user."""
    patient = Patient.query.get(id)
    form = PatientForm(obj=patient)

    if request.method == 'POST' and form.validate_on_submit():
        update_patient(patient, form, request.files)
        db.session.commit()
        patient.update_stats()
        return render_template(
            'patient_details.html',
            patient=patient,
            form=form,
            save_message=True
        )
    else:
        if request.method == 'GET':
            # If this patient has a referral to the current organization in SENT status,
            # update it to RECEIVED
            sent_referrals = [
                r for r in patient.referrals
                if r.to_service_id == current_user.service_id
                and r.in_sent_status()
            ]
            for referral in sent_referrals:
                referral.mark_received()
            if sent_referrals:
                db.session.commit()

            patient.update_stats()

        return render_template(
            'patient_details.html',
            patient=patient,
            form=form,
            save_message=False
        )

def update_patient(patient, form, files):
    """Update a patient record with information from submitted form."""
    for field_name, class_name in [
        ('income_sources', IncomeSource),
        ('phone_numbers', PhoneNumber),
        ('addresses', Address),
        ('emergency_contacts', EmergencyContact),
        ('household_members', HouseholdMember),
        ('employers', Employer),
        ('document_images', DocumentImage)
    ]:
        if form[field_name]:
            # If the last row in a many-to-one section doesn't have any data, don't save it
            if not bool([val for key, val in form[field_name][-1].data.iteritems() if (
                val != ''
                and val is not None
                and key != 'id'
                and not (key in ['state', 'employee'])
                and not (
                    type(val) is FileStorage
                    and val.filename == ''
                )
            )]):
                form[field_name].pop_entry()

            # Add a new child object for each new item in a many-to-one section
            new_row_count = len(form[field_name].entries) - getattr(patient, field_name).count()
            if new_row_count > 0:
                for p in range(new_row_count):
                    getattr(patient, field_name).append(class_name())

            # When a user clicks the delete icon on a many-to-one row, it clears
            # all the data in that row. If any existing rows have no data, delete
            # them from patient object and then from the form.
            for row in form[field_name]:
                if not bool([val for key, val in row.data.iteritems() if (
                    val != ''
                    and val is not None
                    and key != 'id'
                    and not (key in ['state', 'employee'])
                )]):
                    row_index = int(row.name[-1])
                    # Delete from patient object
                    db.session.delete(getattr(patient, field_name)[row_index])
                    # Deletion from form FieldList requires popping all entries
                    # after the one to be removed, then readding them
                    to_re_add = []
                    for _ in range(len(form[field_name].entries) - row_index):
                        to_re_add.append(form[field_name].pop_entry())
                    to_re_add.pop()
                    for row in to_re_add:
                        form[field_name].append_entry(data=row.data)

    # Upload any new document images
    for index, entry in enumerate(form.document_images):
        if entry.file_name.data and entry.file_name.data.filename:
            entry.file_name.data = upload_file(entry.file_name.data)
        else:
            entry.file_name.data = patient.document_images[index].file_name

    # Populate the patient object with all the updated info
    form.populate_obj(patient)
    return

@screener.route('/delete/<id>', methods=['POST', 'GET'])
@login_required
def delete(id):
    """Hard delete a patient. Soft-deleting is usually a better idea."""
    patient = Patient.query.get(id)
    db.session.delete(patient)
    db.session.commit()
    return redirect(url_for('screener.index'))

@screener.route('/document_image/<image_id>')
@login_required
def document_image(image_id):
    """Display an uploaded document image."""
    _image = DocumentImage.query.get(image_id)
    if current_app.config['SCREENER_ENVIRONMENT'] == 'prod':
        file_path = 'http://s3.amazonaws.com/{bucket}/{uploaddir}/{filename}'.format(
            bucket=current_app.config['S3_BUCKET_NAME'],
            uploaddir=current_app.config['S3_FILE_UPLOAD_DIR'],
            filename=_image.file_name
        )
    else:
        file_path = '/documentimages/' + _image.file_name
    return render_template('documentimage.html', file_path=file_path)

@screener.route('/documentimages/<filename>')
@login_required
def get_image(filename):
    """Serve a document image file."""
    return send_document_image(filename)

@screener.route('/new_prescreening', methods=['POST', 'GET'])
@login_required
def new_prescreening():
    """Display the page that allows the user to select which services
    to pre-screen for.
    """
    if request.method == 'POST':
        session['service_ids'] = request.form.getlist('services')
        return redirect(url_for('screener.prescreening_basic'))
    services = Service.query.all()
    return render_template('new_prescreening.html', services=services)

@screener.route('/prescreening_basic', methods=['POST', 'GET'])
@login_required
def prescreening_basic():
    """Page for inputting basic prescreening requirements--
    household size, income, insurance status.
    """
    form = PrescreenForm()
    if form.validate_on_submit():
        session['household_size'] = form.household_size.data
        session['household_income'] = form.household_income.data
        session['has_health_insurance'] = form.has_health_insurance.data
        session['is_eligible_for_medicaid'] = form.eligible_for_medicaid.data
        return redirect(url_for('screener.prescreening_results'))
    else:
        return render_template('prescreening_basic.html', form=form)

@screener.route('/prescreening_results')
@login_required
def prescreening_results():
    """Page with patient's prescreening results: which services she qualifies for and why,
    which sliding scale she'll fall into, and sample prices.
    """
    fpl = calculate_fpl(session['household_size'], int(session['household_income']) * 12)
    return render_template(
        'prescreening_results.html',
        services=calculate_pre_screen_results(
            fpl=fpl,
            has_health_insurance=session['has_health_insurance'],
            is_eligible_for_medicaid=session['is_eligible_for_medicaid'],
            service_ids=session['service_ids']
        ),
        household_size=session['household_size'],
        household_income=int(session['household_income']) * 12,
        fpl=fpl,
        has_health_insurance=session['has_health_insurance'],
        is_eligible_for_medicaid=session['is_eligible_for_medicaid']
    )

@screener.route('/patient_print/<patient_id>')
@login_required
def patient_print(patient_id):
    """Format the patient details page for printing."""
    patient = Patient.query.get(patient_id)
    form = PatientForm(obj=patient)
    return render_template('patient_details.html', patient=patient, form=form)

@screener.route('/patient_history/<patient_id>')
@login_required
def patient_history(patient_id):
    """Display all past edits to the patient's information"""
    patient = Patient.query.get(patient_id)
    patient.update_stats()

    history = ActionLog.query.\
        filter(or_(
            and_(
                ActionLog.row_id == patient_id,
                ActionLog.table_name == 'patient'
            ),
            and_(
                ActionLog.row_id.in_([p.id for p in patient.phone_numbers]),
                ActionLog.table_name == 'phone_number'
            ),
            and_(
                ActionLog.row_id.in_([p.id for p in patient.addresses]),
                ActionLog.table_name == 'address'
            ),
            and_(
                ActionLog.row_id.in_([p.id for p in patient.emergency_contacts]),
                ActionLog.table_name == 'emergency_contact'
            ),
            and_(
                ActionLog.row_id.in_([p.id for p in patient.employers]),
                ActionLog.table_name == 'employer'
            ),
            and_(
                ActionLog.row_id.in_([p.id for p in patient.document_images]),
                ActionLog.table_name == 'document_image'
            ),
            and_(
                ActionLog.row_id.in_([p.id for p in patient.income_sources]),
                ActionLog.table_name == 'income_source'
            ),
            and_(
                ActionLog.row_id.in_([p.id for p in patient.household_members]),
                ActionLog.table_name == 'household_member'
            )
        )).\
        order_by(ActionLog.action_timestamp.desc())
    # Filter out history entries that are only last modified/last modified by changes
    history = [i for i in history if not (
        i.changed_fields
        and set(i.changed_fields).issubset(['last_modified', 'last_modified_by'])
    )]

    services = dict((x.id, x) for x in Service.query.all())

    readable_names = dict(
        (column.name, column.info) for column in (
            chain(
                Patient.__table__.columns,
                PhoneNumber.__table__.columns,
                Address.__table__.columns,
                EmergencyContact.__table__.columns,
                HouseholdMember.__table__.columns,
                IncomeSource.__table__.columns,
                Employer.__table__.columns,
                DocumentImage.__table__.columns
            )
        )
    )

    return render_template(
        'history.html',
        patient=patient,
        history=history,
        services=services,
        readable_names=readable_names
    )

@screener.route('/patient_share/<patient_id>')
@login_required
def patient_share(patient_id):
    """Displays the 'Share Patient' page, which includes prescreening
    results and allows users to send referrals.
    """
    patient = Patient.query.get(patient_id)
    patient.update_stats()
    services = Service.query.all()
    # Get ids of services where the patient already has open referrals,
    # to prevent user from sending duplicates.
    open_referral_service_ids = [
        r.to_service_id for r in patient.referrals
        if (r.in_sent_status() or r.in_received_status())
    ]

    return render_template(
        'patient_share.html',
        patient=patient,
        current_user=current_user,
        services=calculate_pre_screen_results(
            fpl=patient.fpl_percentage,
            has_health_insurance=patient.insurance_status,
            is_eligible_for_medicaid="",
            service_ids=[s.id for s in services]
        ),
        household_size=patient.household_members.count() + 1,
        household_income=patient.total_annual_income,
        fpl=patient.fpl_percentage,
        has_health_insurance=patient.insurance_status,
        is_eligible_for_medicaid="",
        referral_buttons=True,
        open_referral_service_ids=open_referral_service_ids
    )

@screener.route('/add_referral', methods=["POST"])
@login_required
def add_referral():
    """Adds new referral to the database. Called when user clicks
    'Send Referral' button."""
    referral = PatientReferral(
        patient_id=request.form['patient_id'],
        from_app_user_id=request.form['app_user_id'],
        to_service_id=request.form['service_id'],
        status='SENT'
    )
    db.session.add(referral)
    db.session.commit()
    return jsonify()

@screener.route('/patient_screening_history/<patient_id>', methods=['POST', 'GET'])
@login_required
def patient_screening_history(patient_id):
    """Display a patient's past referrals and screening results, and a form
    to enter new screening results.
    """
    patient = Patient.query.get(patient_id)
    patient.update_stats()
    form = ScreeningResultForm()
    sliding_scale_options = SlidingScale.query.filter(
        SlidingScale.service_id == current_user.service_id
    )
    # Add the current organization's sliding scale options to the dropdown
    form.sliding_scale_id.choices = [
        (str(option.id), option.scale_name) for option in sliding_scale_options
    ] or [("", "N/A")]

    if form.validate_on_submit():
        screening_result = PatientScreeningResult()
        screening_result.service_id = current_user.service_id
        screening_result.eligible_yn = form.eligible_yn.data
        screening_result.sliding_scale_id = form.sliding_scale_id.data or None
        screening_result.notes = form.notes.data
        patient.screening_results.append(screening_result)

        # If the patient has an open referral to the current organization, mark
        # as completed
        open_referrals = [
            r for r in patient.referrals
            if r.to_service_id == current_user.service.id
            and r.in_received_status()
        ]
        for referral in open_referrals:
            referral.mark_completed()

        db.session.commit()

    return render_template('patient_screening_history.html', patient=patient, form=form)

@screener.route('/')
@login_required
def index():
    """Display the initial landing page, which lists patients in the
    network and allows users to search and filter them.
    """
    session.clear()
    all_patients = Patient.query.all()
    # Get patients created or updated in the last week
    recently_updated = Patient.query.filter(or_(
        Patient.last_modified > datetime.date.today() - datetime.timedelta(days=7),
        Patient.created > datetime.date.today() - datetime.timedelta(days=7)
    ))
    # Get patients with open referrals at the current user's organization
    open_referrals = Patient.query.filter(
        Patient.referrals.any(and_(
            PatientReferral.to_service_id == current_user.service_id,
            PatientReferral.status.in_(('SENT', 'RECEIVED'))
        ))
    )
    # Get referrals the current user created
    your_referrals = Patient.query.filter(
        Patient.referrals.any(
            PatientReferral.from_app_user_id == current_user.id
        )
    )

    return render_template(
        'index.html',
        all_patients=all_patients,
        recently_updated=recently_updated,
        open_referrals=open_referrals,
        your_referrals=your_referrals
    )

@screener.route('/user/<user_id>')
@login_required
def user(user_id):
    """Display the profile page for a single user."""
    user = AppUser.query.get(user_id)
    return render_template('user_profile.html', user=user)

@screener.route('/service/<service_id>')
@login_required
def service(service_id):
    """Display the profile page for a service organization."""
    service = translate_object(
        Service.query.get(service_id),
        current_app.config['BABEL_DEFAULT_LOCALE']
    )
    return render_template('service_profile.html', service=service)

#########################################
# Development-only routes
#########################################

@screener.route('/template_prototyping/')
def template_prototyping():
    """This is a dev-only route for prototyping fragments of other templates without touching
    them. The url should not be linked anywhere, and ideally it should be not be
    accessible in the deployed version."""
    return render_template('template_prototyping.html')

@screener.route('/mockup')
@login_required
def mockup():
    return render_template('MOCKUPS.html')


'''
Created on Apr 14, 2013

@author: YegorK
'''

class Trie(object):
    '''
    Basic implementation of Trie data structure
    '''

    class TrieNode(object):
        '''
        Trie node
        '''

        def __init__(self, value):
            self.Value = value
            self.Children = {}

    def __init__(self):
        '''
        Constructor
        '''
        self._root = Trie.TrieNode(None)
        self.Size = 0

    def add(self, label, value):
        node = self._root
        for ch in label:
            if ch in node.Children:
                node = node.Children[ch]
            else:
                newNode = Trie.TrieNode(None)
                node.Children[ch] = newNode
                node = newNode

        if not node.Value:
            self.Size += 1
        node.Value = value
        pass

    def lookup(self, label):
        node = self._root
        for ch in label:
            if not ch in node.Children:
                return None
            else:
                node = node.Children[ch]

        return node.Value


import tkinter as tk
from tkinter import ttk, font
from register_container import RegisterContainer
from register_successful import RegisterSuccessful
from register_failed import RegisterFailed
from file_generator import FileGenerator

class MainFrame(tk.Frame):
    def __init__(self, parent, showData):
        super().__init__(parent)
        self.register = parent.dataFrame.dataContainer

        self.registerContainer = RegisterContainer(self)
        self.registerContainer.grid(row=0, column=0, columnspan=2, padx=25, pady=(25,0))#, sticky='NSEW')

        self.registerButton = tk.Button(self, text='REGISTRARSE', bg='#4a5fff', font=font.Font(family='Helvetica', size='12', weight='bold'), command=self.validateInfo)
        self.registerButton.grid(row=1, column=0, padx=25, pady=10, sticky='NSEW')

        self.dataButton = tk.Button(self, text='PARTICIPANTES', bg='#ffbf45', font=font.Font(family='Helvetica', size='12', weight='bold'), command=showData)
        self.dataButton.grid(row=1, column=1, padx=25, pady=10, sticky='NSEW')

    def validateInfo(self):
        if all(self.registerContainer.data()):
            self.register.addParticipant(self.registerContainer.data())
            FileGenerator.createTicket(len(self.register.participants), self.registerContainer.data())
            self.registerContainer.clear()
            success = RegisterSuccessful(self)
            success.mainloop()
        else:
            failed = RegisterFailed()
            failed.mainloop()


import random

import pytest

from hstrat.hstrat import stochastic_algo

@pytest.mark.parametrize(
    "replicate",
    range(5),
)
def test_policy_consistency(replicate):
    random.seed(replicate)
    assert stochastic_algo.CalcMrcaUncertaintyRelExact is None


import numpy as np
import math as m
import time
import sys

def ML_find_a(a,isderiv):
    if isderiv == 0:
        return N/a-Sum-N*m.log(b)/(b**a)
    else:
        return -N/(a*a)+N*m.log(b)*m.log(b)*b**(-a)

try:
    func
except NameError:
    func = ML_find_a

def NewtonRaphson(init,func,tol):
    xtemp = init
    err = 999.9
    while err > tol:
        xtemplast = xtemp
        xtemp = xtemplast - func(xtemplast,0)/func(xtemplast,0)
        err = m.fabs((xtemp-xtemplast)/xtemp)
    return xtemp