from contextlib import nullcontext as does_not_raise
import os
from typing import List, Union

from sqlalchemy.exc import NoResultFound
from fastapi import HTTPException, UploadFile
import pytest

from apps.postapp.schemas import CreatePost, OutputPostSchemas, UpgradePostSchema
from services.post_service import post_service
from tests.post_services_test.conftest import IUnitOfWork, OutputUserSchema, settings

from celery.contrib.testing.worker import TestWorkController

class TestPostService:
    ADD_POST = None
    ERRORS_ADD_POST = None

    current_post = {'post_uid': None}

    async def test_create_post(self, get_file: UploadFile, current_test_user: OutputUserSchema, get_uow: IUnitOfWork, celery_worker: TestWorkController):

        data = CreatePost(
            image=get_file,
            content='TEST_PYTEST',
            title='PYTEST',
            owner_uid=current_test_user.uid
        )
        post: OutputPostSchemas = await post_service.create_post(post_data=data, username=current_test_user.username, uow=get_uow)

        is_image = os.path.exists(settings.MEDIA_URL / post.image)

        assert post
        assert is_image

        self.current_post['post_uid'] = post.uid

    async def test_get_post_all(self, get_uow: IUnitOfWork):
        posts = await post_service.get_post_all(uow=get_uow)
        assert posts
        assert len(posts) == 1

    async def test_get_post_one(self, get_uow: IUnitOfWork, current_test_user: OutputUserSchema):
        post_uid = self.current_post['post_uid']
        post: OutputPostSchemas = await post_service.get_post_one(post_uid=post_uid, uow=get_uow)
        async with get_uow:
            view = await get_uow.view.find_all(post_uid=post_uid)
        assert post and len(view) == 0

        post: OutputPostSchemas = await post_service.get_post_one(post_uid=post_uid, uow=get_uow, owner_uid=current_test_user.uid)
        async with get_uow:
            view = await get_uow.view.find_all(post_uid=post_uid)

        assert post and len(view) == 1

    async def test_update_post(self, get_update_file: UploadFile, current_test_user: OutputUserSchema, get_uow: IUnitOfWork, celery_worker):
        update_data = UpgradePostSchema(
            image=get_update_file,
            content='NEW_TEST_PYTEST',
            title='NEW_PYTEST',
            owner_uid=current_test_user.uid
        )
        post_uid = self.current_post['post_uid']

        await post_service.update_post(post_uid=post_uid, update_data=update_data, username=current_test_user.username, uow=get_uow)

        async with get_uow:
            new_post: OutputPostSchemas = await get_uow.post.find_one(uid=post_uid)
            assert new_post
            new_path_image = new_post.image
            new_content = new_post.content == 'NEW_TEST_PYTEST'
            new_title = new_post.title == 'NEW_PYTEST'

            is_image = os.path.exists(settings.MEDIA_URL / new_path_image)

        assert is_image
        assert new_content
        assert new_title

    async def test_recent_publications(self, get_uow: IUnitOfWork):
        recent: List[OutputPostSchemas] = await post_service.recent_publications(uow=get_uow)
        post_uid = self.current_post['post_uid']
        assert recent
        assert len(recent) == 1
        assert recent[0].uid == post_uid

    async def test_popular_publications(self, get_uow: IUnitOfWork):
        popular: List[OutputPostSchemas] = await post_service.popular_publications(uow=get_uow)
        post_uid = self.current_post['post_uid']
        assert popular
        assert len(popular) == 1
        assert popular[0].uid == post_uid

    async def test_delete_post(self, get_uow: IUnitOfWork):
        post_uid = self.current_post['post_uid']
        async with get_uow:
            post: OutputPostSchemas = await get_uow.post.find_one(uid=post_uid)

        assert post.is_active

        await post_service.delete_post(post_uid=post_uid, uow=get_uow)

        with pytest.raises(NoResultFound, match='No row was found when one was required'):
            async with get_uow:
                deactivate_post: OutputPostSchemas = await get_uow.post.find_one(uid=post_uid)


import os
from os.path import join
import json
import numpy as np
import cv2
import random
import itertools
import re
import datetime
from keras.models import model_from_json
from keras.optimizers import SGD
import shutil
import tensorflow as tf
from keras import backend as K
from collections import Counter
import pandas as pd

letters_train = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T', 'U', 'V', 'Y', 'Z']
letters = sorted(list(letters_train))

import itertools

def check_word(word):
    spec_symbols = '#'
    match = [l in spec_symbols for l in word]
    return sum(match) >= 2 #başında ve devamında iki tane özel char varsa true

def ctc_lambda_func(args):
    y_pred, labels, input_length, label_length = args
    # the 2 is critical here since the first couple outputs of the RNN
    # tend to be garbage:
    y_pred = y_pred[:, 2:, :]
    return K.ctc_batch_cost(labels, y_pred, input_length, label_length)

def labels_to_text(labels):
    #evrim starts
    if labels[7]== len(letters) + 1:
        labels= labels[:7]
    #evrim ends
    return ''.join(list(map(lambda x: letters[int(x)], labels)))

def text_to_labels(text):
    return list(map(lambda x: letters.index(x), text))

def is_valid_str(s):
    for ch in s:
        if not ch in letters:
            return False
    return True

class TextImageGenerator:

    def __init__(self,
                 dirpath,
                 tag,
                 img_w, img_h,
                 batch_size,
                 downsample_factor,
                 max_text_len=8):

        self.img_h = img_h
        self.img_w = img_w
        self.batch_size = batch_size
        self.max_text_len = max_text_len
        self.downsample_factor = downsample_factor
        #evrim
        img_dirpath = dirpath

        self.samples = []
        start='#'
        end='#'
        for filename in os.listdir(img_dirpath):
            name, ext = os.path.splitext(filename)

            if ext in ['.png', '.jpg']:
                img_filepath = join(img_dirpath, filename)
                if check_word(name):
                    description= (name.split(start))[1].split(end)[0]
                else:
                    description = name
                #print("desc:",description)
                self.samples.append([img_filepath, description])

        self.n = len(self.samples)
        self.indexes = list(range(self.n))
        self.cur_index = 0

    def build_data(self):
        self.imgs = np.zeros((self.n, self.img_h, self.img_w))
        self.texts = []
        for i, (img_filepath, text) in enumerate(self.samples):
            img = cv2.imread(img_filepath)
            img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
            img = cv2.resize(img, (self.img_w, self.img_h))
            img = img.astype(np.float32)
            img /= 255
            # width and height are backwards from typical Keras convention
            # because width is the time dimension when it gets fed into the RNN
            self.imgs[i, :, :] = img
            self.texts.append(text)

    def get_output_size(self):
        return len(letters) + 1

    def next_sample(self):
        self.cur_index += 1
        if self.cur_index >= self.n:
            self.cur_index = 0
            random.shuffle(self.indexes)
        return self.imgs[self.indexes[self.cur_index]], self.texts[self.indexes[self.cur_index]]

    def next_batch(self,K):
        while True:
            # width and height are backwards from typical Keras convention
            # because width is the time dimension when it gets fed into the RNN
            if K.image_data_format() == 'channels_first':
                X_data = np.ones([self.batch_size, 1, self.img_w, self.img_h])
            else:
                X_data = np.ones([self.batch_size, self.img_w, self.img_h, 1])
            Y_data = np.ones([self.batch_size, self.max_text_len])
            input_length = np.ones((self.batch_size, 1)) * (self.img_w // self.downsample_factor - 2)
            label_length = np.zeros((self.batch_size, 1))
            source_str = []

            for i in range(self.batch_size):
                img, text = self.next_sample()
                img = img.T
                if K.image_data_format() == 'channels_first':
                    img = np.expand_dims(img, 0)
                else:
                    img = np.expand_dims(img, -1)
                X_data[i] = img
                #print("text:",text)
                #evrim starts
                if len(text)<8:
                    temp= text_to_labels(text)
                    temp.append(len(letters) + 1)
                    Y_data[i] = temp
                else:
                    Y_data[i] = text_to_labels(text)
                #evrim ends

                source_str.append(text)
                label_length[i] = len(text)
                #label_length[i] = 8  #evrim

            inputs = {
                'the_input': X_data,
                'the_labels': Y_data,
                'input_length': input_length,
                'label_length': label_length,
                #'source_str': source_str
            }
            outputs = {'ctc': np.zeros([self.batch_size])}
            yield (inputs, outputs)

# For a real OCR application, this should be beam search with a dictionary
# and language model.  For this example, best path is sufficient.
def decode_batch(out):
    ret = []
    for j in range(out.shape[0]):
        out_best = list(np.argmax(out[j, 2:], 1))
        out_best = [k for k, g in itertools.groupby(out_best)]
        outstr = ''
        for c in out_best:
            if c < len(letters):
                outstr += letters[c]
        ret.append(outstr)
    return ret

def load_model():
    # load json and create model
    fmodel='lr0.02_mom0.9'
    json_file = open(fmodel + '.json', 'r')
    loaded_model_json = json_file.read()
    json_file.close()
    loaded_model = model_from_json(loaded_model_json)

    # load weights into new model
    loaded_model.load_weights(fmodel + '.h5')

    sgd = SGD(lr=0.02, decay=1e-6, momentum=0.9, nesterov=True, clipnorm=5)
    # Compile model
    loaded_model.compile(loss={'ctc': lambda y_true, y_pred: y_pred}, optimizer=sgd)
    return loaded_model

def predict_plate(sess,loaded_model,input_dir):
    cnt_plate= len(os.listdir(input_dir))

    if cnt_plate==0:
        print("No input plate found")
        return '_'
    else:
        tiger_test = TextImageGenerator(input_dir, 'test', 128, 64, cnt_plate, 4)
        tiger_test.build_data()
        net_inp = loaded_model.get_layer(name='the_input').input
        net_out = loaded_model.get_layer(name='softmax').output
        #print(net_inp,tiger_test)
        for inp_value, _ in tiger_test.next_batch(K):
            X_data = inp_value['the_input']
            net_out_value = sess.run(net_out, feed_dict={net_inp:X_data})
            print("net_out_value shape:",np.shape(net_out_value))

            pred_texts = decode_batch(net_out_value)

            deletefiles(input_dir)
            return pred_texts

def deletefiles(source):
    files = os.listdir(source)
    for file in files:
        try:
            #print(file)
            os.remove(source+ file)
        except OSError as e:  ## if failed, report it back to the user ##
            print ("Error: %s - %s." % (e.filename, e.strerror))

""" for archive (not used)
source = 'python/input/'
dest  = 'python/output/'

files = os.listdir(source)

for f in files:
        shutil.move(source+f, dest)
"""
#prints the total num of test dev and train examples
def Print_overall(ann_dirpath,test_dirpath):

    test_cnt= len(os.listdir(test_dirpath))
    val_cnt=0
    train_cnt=0
    for fx in os.listdir(ann_dirpath):
        with open(ann_dirpath + "/" + fx) as f:
            source_data = json.load(f)
            #print(source_data['tags'])
            if source_data['tags'] == ['val']:
                val_cnt+=1
            else:
                train_cnt +=1

    print("Total count of files:",len(os.listdir(ann_dirpath))+ len(os.listdir(test_dirpath)))
    print("Total count of training set:",train_cnt)
    print("Total count of val set:",val_cnt)
    print("Total count of test set:",test_cnt)
    return test_cnt

def show_distribution(strx,position1,position2):

    ch=[]

    for i in range(len(strx)):
        ch.append(strx[i][position1:position2])

    data= pd.Series(ch)
    df = pd.DataFrame(data,columns=['ch'])

    output = df.groupby(['ch']).size().sort_values(ascending=False)

    return output.nlargest(10)

#classify plates as matched or non_matched
def all_plates_classify(sess,tiger_test,net_inp,net_out):
    nmatch_str=[]
    match_str=[]
    matched=0
    non_matched=0
    i=0
    for inp_value, _ in tiger_test.next_batch(K):
        testset_cnt = inp_value['the_input'].shape[0]
        X_data = inp_value['the_input']

        net_out_value = sess.run(net_out, feed_dict={net_inp:X_data})

        pred_texts = decode_batch(net_out_value)

        labels = inp_value['the_labels']
        texts = []

        for label in labels:
            text = labels_to_text(label)
            texts.append(text)
            #print("evrim2")
        for cnt in range(testset_cnt-1):
            if pred_texts[cnt] == texts[cnt]:
                matched += 1
                match_str.append(texts[cnt])
            else:
                non_matched +=  1
                nmatch_str.append(texts[cnt])
            #print("cnt:", cnt)
        return nmatch_str,match_str,non_matched,matched


import ConfigParser
import os.path
import logging

class Config:
    binaries_full_dir = ""
    binaries_dir      = "downloadables"
    samples_dir       = "buildables"
    listen_ip         = "127.0.0.1"
    listen_port       = 42424
    max_upload_size   = 16 * 1024 * 1024

    def __init__(self):

        self.config_file = "config.cfg"
        self.config      = ConfigParser.RawConfigParser()

        if not os.path.isfile(self.config_file):
            self._create_default()

        self._read_config()

        #Get current directory, so we can set the correct upload directory for
        #Flask to use.
        Config.binaries_full_dir = str(os.getcwd()) + "//" + Config.binaries_dir

    def _create_default(self):

        self.config.add_section("server")
        self.config.set("server", "listen_ip", "127.0.0.1")
        self.config.set("server", "listen_port", "42424")
        self.config.set("server", "max_upload_size_mb", "16")

        logging.info("Creating default config")
        with open(self.config_file, "wb") as fp:
            self.config.write(fp)

    def _read_config(self):

        self.config.read(self.config_file)

        Config.listen_ip   = self.config.get("server", "listen_ip")
        Config.listen_port = self.config.getint("server", "listen_port")
        Config.max_upload_size = self.config.getint("server", "max_upload_size_mb") * 1024 * 1024


import nltk
import json
import pickle
import numpy as np

import tensorflow as tf
from utils import clean_message
import random

words = []
classes = []
documents = []

data_file = open('intents.json').read()
intents = json.loads(data_file)

for intent in intents['intents']:
    for pattern in intent['patterns']:
        w = clean_message(pattern)
        words.extend(w)
        documents.append((w,intent['tag']))
        if intent['tag'] not in classes:
            classes.append(intent['tag'])

classes = sorted(list(set(classes)))

print(len(classes), "classes", classes)

pickle.dump(words, open('words.pkl', 'wb'))
pickle.dump(classes, open('classes.pkl', 'wb'))

training = []

output_empty = [0] * len(classes)

for doc in documents:
    bag = []
    pattern_words = doc[0]

    for w in words:
        bag.append(1) if w in pattern_words else bag.append(0)

        output_row = list(output_empty)
        output_row[classes.index(doc[1])] = 1
        training.append([bag, output_row])

random.shuffle(training)
training = np.array(training)

train_x = list(training[:,0])
train_y = list(training[:,1])

model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Input(shape = len(train_x[0])))
model.add(tf.keras.layers.Dense(128, activation='relu'))
model.add(tf.keras.layers.Dropout(0.6))
model.add(tf.keras.layers.Dense(64, activation = 'relu'))
model.add(tf.keras.layers.Dropout(0.6))
model.add(tf.keras.layers.Dense(len(train_y[0]), activation = 'softmax'))

model.compile(loss = 'categorical_crossentropy', optimizer=tf.keras.optimizers.SGD(learning_rate=0.01, decay = 1e-6, momentum=0.9, nesterov = True), metrics = ['accuracy'])

hist = model.fit(np.array(train_x), np.array(train_y), epochs = 20, batch_size = 32, verbose = 1)

model.save('model1.h5')


import csv
import json

from lxml.builder import E
from lxml import etree

from mesh.models import Edge, Node, EDGE_TYPES

def import_gefx(file, project):
    """
    Import GEFX data into the project.

    """
    parser = etree.XMLParser(ns_clean=True)
    tree = etree.parse(file, parser)
    root = tree.getroot()

    graph_el = root.xpath('//*[local-name() = "graph"]')
    if len(graph_el) == 0:
        return

    graph_el = graph_el[0]
    global_edge_type = graph_el.get('defaultedgetype')

    nodes = {}
    node_els = root.xpath('//*[local-name() = "node"]')
    for node_el in node_els:
        node = Node(name=node_el.get('label'), description=node_el.get('description') or "", project=project)
        node.save()
        nodes[node_el.get('id')] = node

    edge_els = root.xpath('//*[local-name() = "edge"]')
    for edge_el in edge_els:
        edge = Edge(from_node=nodes[edge_el.get('source')], to_node=nodes[edge_el.get('target')], project=project)
        edge_type = edge_el.get('type') or global_edge_type or 'undirected'
        edge.type = 1 if edge_type.lower() == 'undirected' else 2
        edge.save()

def write_gephi_csv(file, project):
    """
    Generate very simple CSV file for the project.

    """
    writer = csv.writer(file)
    edges = Edge.objects.filter(project=project)
    for e in edges:
        writer.writerow([e.from_node.name, e.to_node.name])

def write_gexf(file, project):
    """
    Generate a GEXF file for the project.

    """
    nodes = Node.objects.filter(project=project)
    node_els = []
    for n in nodes:
        node_els.append(E.node(
            id=str(n.name),
            description=n.description,
            label=n.name
        ))

    edges = Edge.objects.filter(project=project)
    edge_els = []
    for e in edges:
        edge_els.append(E.edge(
            id=str(e.id),
            source=str(e.from_node.name),
            target=str(e.to_node.name),
            type='undirected' if e.type == 1 else 'directed'
        ))

    root = E.gexf(
        E.graph(
            E.nodes(*node_els),
            E.edges(*edge_els)
        )
    )

    file.write(etree.tostring(root))

def project_graph_json(project):
    """
    Generate JSON data for visualising a project graph.

    """
    nodes = Node.objects.filter(project=project)
    edges = Edge.objects.filter(project=project)
    data = {
        'edges' : [{'id' : e.id, 'description' : e.description, 'sourceNode' : e.from_node.name, 'targetNode' : e.to_node.name, 'type' : e.type} for e in edges],
        'nodes' : [{'id' : n.name, 'description' : n.description} for n in nodes]
    }
    return json.dumps(data)


from datetime import datetime
from time import time
import sys

def get_index(length: int):
    """Gets an integer between 0 and the length provided based on the time and day of the year.

    Parameters
    ----------
    length : int

    Returns
    -------
    int
    """
    # get the current Unix timestamp as an integer
    seconds = int(time())
    # get the current day of the year
    day = datetime.now().timetuple().tm_yday

    # add the seconds to the day of the year
    total = seconds + day

    # get a value that is between 0 and the length - 1 based on the current time
    return total % (length - 1)

if __name__ == '__main__':
    print(get_index(int(sys.argv[1])))


class Solution:
    def __init__(self):
        self.s = ''
        self.hash = [0] * 256
    def replaceSpace(self, s):
        """ simple
        2. 请实现一个函数，将一个字符串中的每个空格替换成“%20”。例如，当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
        思路：1) 直接用string的replace方法 2) 用一个新的的数组，从前向后边判断边复制 3) 在原来数组上从后向前边判断边复制
        """
        return s.replace(' ', '%20')

    def replaceSpace2(self, s):
        s_len = len(s)
        space_count = 0
        for i in s:
            if i == ' ':
                space_count += 1

        s_len += 2 * space_count
        # new_array = [' '] * s_len
        new_array = [''] * s_len
        j = 0
        for i in range(len(s)):
            if s[i] == ' ':
                new_array[j] = '%'
                new_array[j + 1] = '2'
                new_array[j + 2] = '0'
                j += 3
            else:
                new_array[j] = s[i]
                j += 1
        return ''.join(new_array)

    def replaceSpace3(self, s):
        if not s:
            return None
        s_len = len(s)
        if s_len <= 0:
            return ''
        space_count = 0
        for i in s:
            if i == ' ':
                space_count += 1

        s += ' ' * space_count * 2
        j = space_count
        for i in range(s_len-1, -1, -1):
            if s[i] != ' ':
                s[i + j * 2] = s[i]
            else:
                s[i + j * 2] = '0'
                s[i + j * 2 - 1] = '2'
                s[i + j * 2 - 2] = '%'
                j -= 1
        return s

    def Permutation(self, ss):
        """
        27.字符串排序输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,
        则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。
        :param ss:
        :return:
        """
        if len(ss) <= 1:
            return ss
        res = set()
        for i in range(len(ss)):
            for j in self.Permutation(ss[:i] + ss[i+1:]):
                res.add(ss[i] + j)
        return sorted(res)

    def Permutation2(self, ss):
        if len(ss) <= 1:
            return ss
        from itertools import permutations
        res = list(map(lambda x: ''.join(list(x)), permutations(ss)))
        return res

    def FirstNotRepeatingChar(self, s):
        """
        34. 在一个字符串(0<=字符串长度<=10000，全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1（需要区分大小写）.
        idea: 暴力判断（用字符串的count方法，实际是采用哈希方法 （字典））
        :param s:
        :return:
        """
        if len(s) > 10000 or len(s) <= 0:
            return -1
        for i in s:
            if s.count(i) == 1:
                return s.index(i)

    def FirstNotRepeatingChar2(self, s):
        """
        使用字典
        :param s:
        :return:
        """
        if not s or len(s) > 1000 or len(s) <= 0:
            return -1
        d = {}
        for i in s:
            if i in d:
                d[i] = d[i] + 1
            else:
                d[i] = 1
        for k, v in d.items():
            if v == 1:
                return s.index(k)

    def FirstNotRepeatingChar3(self, s):
        """
        用字典，k为字符，v为位置，当存在时，v += len(s)
        :param s:
        :return:
        """
        s_len = len(s)
        if not s or s_len > 1000 or s_len <= 0:
            return -1
        d = {}
        for i in range(s_len):
            if s[i] in d:
                d[s[i]] += s_len
            else:
                d[s[i]] = i
        res = s_len
        for k, v in d.items():
            if v < s_len:
                res = min(res, v)
        if res == s_len:
            return -1
        else:
            return res

    def LeftRotateString(self, s, n):
        """
        43. 循环左移n位后的序列输出
        例如: "1234", 循环左移7位为："4123"
        idea: 字符串拼接技巧
        :param s:
        :param n:
        :return:
        """
        if len(s) == 0 or n < 0:
            return ''
        n = n % len(s)
        return s[n:] + s[:n]

    def StrToInt(self, s):
        """
        49. 字符串转成整数，非法输出0
        :param s:
        :return:
        """
        if not s or len(s) == 0:
            return 0
        flag = 1
        if s[0] == '+':
            s = '0' + s[1:]
        elif s[0] == '-':
            s = '0' + s[1:]
            flag = -1
        res = 0
        for i in s:
            if '0' <= i <= '9':
                res = res * 10 + ord(i) - ord('0')
            else:
                return 0
        return flag * res

    def ReverseSentence(self, s: str):
        """
        44. 反转单词顺序："student. a am I" --> "I am a student."
        idea: 字符串拆分与拼接，注意边界
        :param s:
        :return:
        """
        if s == '':
            return ''
        words = s.split(' ')[::-1]
        res = ' '.join(words)
        return res

    def match(self, s, pattern):
        """
        52. 匹配包括'.'和'*'的正则表达式。模式中的字符'.'表示任意一个字符，而'*'表示它前面的字符可以出现任意次（包含0次）。
         在本题中，匹配是指字符串的所有字符匹配整个模式。
        例如，字符串"aaa"与模式"a.a"和"ab*ac*a"匹配，但是与"aa.a"和"ab*a"均不匹配
        idea: 利用状态机，分为几种情况（分类讨论，可合并的要和合并）
        （1）当第二个字符不为‘*’时：匹配就是将字符串和模式的指针都下移一个，不匹配就直接返回false
        （2）当第二个字符为'*'时：
                匹配：
                        a.字符串下移一个，模式不动
                        b.字符串下移一个，模式下移两个
                        c.字符串不动，模式下移两个
                不匹配：字符串下移不动，模式下移两个
        搞清楚这几种状态后，用递归实现即可：
        :param s:
        :param pattern:
        :return:
        """
        if len(s) == 0 and len(pattern) == 0:
            return True
        elif len(s) > 0 and len(pattern) == 0:
            return False
        elif len(s) == 0 and len(pattern) > 0:
            if len(pattern) > 1 and pattern[1] == '*':
                return self.match(s, pattern[2:])
            else:
                return False

        if len(pattern) > 1 and pattern[1] == '*':
            if s[0] == pattern[0] or pattern[0] == '.':
                return self.match(s, pattern[2:]) or self.match(s[1:], pattern[2:]) or self.match(s[1:], pattern)
            else:
                return self.match(s, pattern[2:])
        else:
            if pattern[0] == s[0] or pattern[0] == '.':
                return self.match(s[1:], pattern[1:])
            else:
                return False

    def FirstAppearingOnce(self):
        """
        54. 请实现一个函数用来找出字符流中第一个只出现一次的字符。例如，当从字符流中只读出前两个字符"go"时，第一个只出现一次的字符是"g"。
        当从该字符流中读出前六个字符“google"时，第一个只出现一次的字符是"l"。
        当当前字符流没有存在出现一次的字符是返回“#”
        :return:
        """
        for i in self.s:
            if self.hash[ord(i)] == 1:
                return i
        return '#'

    def Insert(self, char):
        # python 中将字符转换成ASCII码用ord(ch)
        # ASCII转换成字符用chr
        self.s += char
        self.hash[ord(char)] += 1
        print(ord(char))

    def isNumeric(self, s):
        # 53.请实现一个函数用来判断字符串是否表示数值（包括整数和小数）。
        # 例如，字符串"+100","5e2","-123","3.1416"和"-1E-16"都表示数值。
        # 但是"12e","1a3.14","1.2.3","+-5"和"12e+4.3"都不是。
        sign, point, hasE = False, False, False
        for i in range(len(s)):
            if s[i].lower() == 'e':
                if hasE: return False
                if i == len(s) - 1: return False
                hasE = True
            elif s[i] == '+' or s[i] == '-':
                if sign and s[i - 1].lower() != 'e': return False
                if not sign and i > 0 and s[i - 1].lower() != 'e': return False
                sign = True
            elif s[i] == '.':
                if hasE or point: return False
                point = True
            elif ord(s[i]) < ord('0') or ord(s[i]) > ord('9'):
                return False
        return True

    def KMP1(self, pat, txt):
        m = len(pat)
        n = len(txt)
        for i in range(n-m+1):
            j = 0
            while j < m:
                if pat[j] != txt[i+j]:
                    break
                else:
                    j += 1
            if j == m:
                return i
        return -1

if __name__ == '__main__':
    solution = Solution()
    # print(solution.replaceSpace('We are Happy!'))
    print(solution.replaceSpace2('We are Happy!'))
    print(solution.replaceSpace3(['W', 'e', ' ', 'a', ' ', 'H']))
    res2 = solution.Permutation("abcbd")
    print("Permutation", len(res2), res2)
    res3 = solution.FirstNotRepeatingChar("abadfabdefaaac")
    print("FirstNotRepeatingChar: ", res3)
    res9 = solution.StrToInt('+123')
    print("StrToInt: ", res9)
    solution.Insert('g')
    print(solution.FirstAppearingOnce())
    solution.Insert('o')
    print(solution.FirstAppearingOnce())
    solution.Insert('o')
    print(solution.FirstAppearingOnce())
    solution.Insert('g')
    print(solution.FirstAppearingOnce())
    solution.Insert('l')
    print(solution.FirstAppearingOnce())
    solution.Insert('e')
    print(solution.FirstAppearingOnce())

    print(solution.Permutation2('abc'))

    res44 = solution.ReverseSentence("student. a am I")
    print("ReverseSentence", res44)
    print("KMP1", solution.KMP1('abc', 'fjafafabceabc'))

    res66 = solution.FirstNotRepeatingChar3("aabcceff")
    print(res66)


import sys
import os
dirpath = os.path.dirname(os.path.abspath(__file__))
sys.path.append(dirpath + '/../')

import torch.utils.data as Data
import multiprocessing as mp
from utils.nnet import *
from kaldiio import load_scp
from dataset.kaldi_reader import KaldiTester

class Scorer:

    def __init__(self, model_path, trials_path, scp_path,
                 batch_size=1, use_gpu=False,
                 mgr=None, processes=None, check=False):
        if processes is not None and processes < 0:
            raise Exception('`processes` should be a positive integer.')
        self.check = check
        self.model_path = model_path
        self.scp_path = scp_path
        self.batch_size = batch_size
        self.use_gpu = use_gpu
        self.processes = int(processes) if processes is not None else None
        self.trials = []
        self.data = load_scp(scp_path)
        self.shared_scores = mgr.dict()
        self.miss = 0

        with open(trials_path, 'r') as f:
            for line in f:
                tmp = line.strip().split()
                if len(tmp) != 3:
                    continue
                else:
                    utt1, utt2, is_target = tmp
                    self.trials.append((utt1, utt2, is_target))
        self.tot = len(self.trials)

    def get_all_scores(self):
        if self.processes is None or self.processes == 0:
            self.get_scores(0, len(self.trials))
        else:
            block = int(len(self.trials) / self.processes)
            jobs = [mp.Process(target=self.get_scores,
                               args=(i * block, (i + 1) * block if i < self.processes - 1 else None))
                    for i in range(self.processes)]
            for job in jobs:
                job.start()
            for job in jobs:
                job.join()
        # print('Finished calculate score.')

    def save_scores(self, save_path, override=True):
        if os.path.exists(save_path) and override is False:
            raise Exception('File/Directory %s exists, please choose another path or set `override` to True' % save_path)
        with open(save_path, 'w') as f:
            shared_scores = dict(self.shared_scores)
            for pair in shared_scores:
                utt1, utt2 = pair
                f.write(utt1 + ' ' + utt2 + ' ' + str(shared_scores[pair]) + '\n')

    def get_scores(self, start, end):
        model = torch.load(self.model_path)
        model.eval()
        if self.use_gpu:
            model = model.cuda()
        kt = KaldiTester(self.scp_path, self.trials[start:end],
                         use_gpu=self.use_gpu)
        if self.check:
            kt.clear()
            self.trials = kt.trials
        self.miss = kt.miss
        dataloader = Data.DataLoader(
            dataset=kt,
            batch_size=self.batch_size,
            shuffle=False
        )

        with torch.no_grad():
            for cnt, (mat1, mat2, utt1, utt2) in enumerate(dataloader):
                mat1 = mat1.float()
                mat2 = mat2.float()
                scores = model(mat1, mat2).squeeze()
                if self.use_gpu:
                    scores = scores.cpu()
                for utt1, utt2, score in zip(utt1, utt2, scores):
                    self.shared_scores[(utt1, utt2)] = float(score)

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-rp', '--result_path', type=str, default=None)
    parser.add_argument('-e', '--epochs', type=int, nargs='+')
    parser.add_argument('-ug', '--use_gpu', type=bool, default=True)
    parser.add_argument('-c', '--cards', type=int, nargs='+', default=[0])
    parser.add_argument('-t', '--trial', type=str, default=dirpath + '/trials')
    parser.add_argument('-chc', '--check', type=bool, default=False)
    parser.add_argument('-nb', '--num_of_process', type=int, default=0)
    parser.add_argument('-sp', '--scp_path', type=str, default=None)
    return parser.parse_args()

if __name__ == '__main__':
    from utils.tools import Params
    import time
    import re
    import argparse
    args = main()
    os.environ['CUDA_VISIBLE_DEVICES'] = '-1' if not args.use_gpu else str(args.cards).strip('[]')
    params = Params(dirpath + '/../nnet/config.json')
    models_path = dirpath + '/../nnet'
    model_path = None
    for epoch in args.epochs:
        for i in os.listdir(models_path):
            a = re.search(r'epoch_([0-9]*)$', i)
            if a is None:
                continue
            elif int(a.group(1)) == epoch:
                model_path = os.path.join(models_path, i)

        scorer = Scorer(model_path=model_path,
                        scp_path=params.test_scp_path if args.scp_path is None else args.scp_path,
                        trials_path=args.trial, batch_size=50,
                        use_gpu=args.use_gpu,
                        mgr=mp.Manager(), processes=args.num_of_process)
        # pre = time.time()
        scorer.get_all_scores()
        fr = open('./result' if args.result_path is None else args.result_path, 'a')

        basename = os.path.basename(os.path.dirname(os.path.dirname(dirpath)))
        print('***********************************', file=fr)
        print('tot is %d, miss %d, %f' % (scorer.tot, scorer.miss, scorer.miss / scorer.tot), file=fr)
        print('%s/%s' % (basename, params.model_name), file=fr)
        print('trial:%s' % (args.trial), file=fr)
        print(time.ctime(), file=fr)
        print('Epoch %d' % epoch, file=fr)
        # print('Cost time %s' % (time.time() - pre), file=fr)
        # print('Saving Scores, ', end='', file=fr)
        scorer.save_scores(dirpath + '/alpha0.5.txt')
        from score.eer_and_mindcf import Resulter
        a = Resulter(dirpath + '/alpha0.5.txt',
                     trial_file=None if args.check else args.trial,
                     trials=scorer.trials if args.check else None)
        print(a.compute_score(), file=fr)
        print('-----------------------------------', file=fr)
        fr.close()


from django.forms import widgets

class PickADateDateWidget(widgets.Input):
    class Media:
        css = {
            'all': ('pickadate/themes/default.css', 'pickadate/themes/default.date.css')
        }
        js = ('https://code.jquery.com/jquery-3.2.1.js', 'pickadate/picker.js', 'pickadate/picker.date.js')

    template_name = 'widgets/pickadate.html'

    def get_context(self, name, value, attrs):
        context = super().get_context(name, value, attrs)
        context['options'] = {'format': "yyyy-mm-dd"}
        return context


from django.urls import path
from .views import *
# (CustomerSignupView,
# ReportUserSignupView,
# CustomAuthToken, LogoutView, CustomerOnlyView, ReportUserOnlyView)
from user.api import views

urlpatterns=[
    path('signup/report-user/', ReportUserSignupView.as_view()),
    path('signup/customer/', CustomerSignupView.as_view()),
    path('login/',CustomAuthToken.as_view(), name='auth-token'),
    # path('logout/', LogoutView.as_view(), name='logout'),
    # path('logout/', views.LogoutView, name='logout-view'),
    path('logout/', LogoutView.as_view(), name='logout-view'),
    # path('logout/', views.LogoutView, name='logout-view'),
    # path('logout/', LogoutView.as_view(), name='logout')
    path('report-user/dashboard/', ReportUserOnlyView.as_view(), name='report-user-dashboard'),
    path('customer/dashboard/', CustomerOnlyView.as_view(), name='coustomer-dashboard'),
]


from django.urls import path
from . import views

urlpatterns = [
   path('', views.index, name='index'),
   path('wine/', views.wines, name='wines'),
   path('wineview/<int:id>', views.wineview, name='wine_details'),
   path('regions/', views.regions, name='regions'),
   path('regionview/<int:id>', views.regionview, name='region_details'),
   path('reviews/', views.reviews, name='reviews'),
   path('reviewview/<int:id>', views.reviewview, name='review_details'),
   path('newwine/', views.newWine, name='new_wine'),
   path('newreview/', views.newReview, name='new_review'),
   path('loginmessage/', views.loginMessage, name='loginmessage'),
   path('logoutmessage/', views.logoutMessage, name='logoutmessage'),

]


import pygame
import sys

import pygame
from pygame_gui import UIManager
from pygame_gui.elements.ui_drop_down_menu import UIDropDownMenu

BLOCK_SIZE = 50
WHITE = 255, 255, 255
BLACK = 0, 0, 0
BLUE = 0, 0, 255
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600

pygame.init()

class Block:
    def __init__(self, x, y, width, height, color):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.color = color

    def move(self, x, y):
        self.x = x
        self.y = y

    def update(self, screen):
        pygame.draw.rect(screen, self.color, (self.x, self.y, self.width, self.height))

class Level:
    def __init__(self, file_name):
        self.width = 0
        self.height = 0
        self.blocks = []
        self.main_block = None
        self.main_block_index = None
        self.color_dict = {
            0: (255, 255, 255),  # white
            1: (0, 0, 0),  # black
            2: (255, 0, 0),  # red
            3: (0, 255, 0),  # green
            4: (0, 0, 255),  # blue
        }
        with open(file_name, 'r') as f:
            lines = f.readlines()
            self.height = len(lines)
            for i, line in enumerate(lines):
                row = []
                blocks_in_row = line.strip().split()
                self.width = max(self.width, len(blocks_in_row))
                for j, val in enumerate(blocks_in_row):
                    if val == '1':
                        block = Block(j * BLOCK_SIZE, i * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, (0, 0, 0))
                        self.main_block = block
                        self.main_block_index = j

                    elif val != '0':
                        color = self.color_dict.get(int(val), (255, 255, 255))
                        row.append(Block(j * BLOCK_SIZE, i * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, color))
                if row:
                    self.blocks.append(row)

    def draw(self, surface):
        for row in self.blocks:
            for block in row:
                pygame.draw.rect(surface, block.color, (block.x, block.y, BLOCK_SIZE, BLOCK_SIZE))
        pygame.draw.rect(surface, self.main_block.color, (self.main_block.x, self.main_block.y, BLOCK_SIZE, BLOCK_SIZE))

    def get_main_block(self):
        return self.main_block

    def get_color_by_index(self, index):
        return self.color_dict.get(index, (255, 255, 255))

def check_win(blocks):
    color_dict = {}
    for block in blocks:
        if block.color not in color_dict:
            color_dict[block.color] = [(block.x, block.y)]
        else:
            color_dict[block.color].append((block.x, block.y))

    for coord_list in color_dict.values():
        if len(coord_list) > 1:
            for i in range(len(coord_list) - 1):
                if abs(coord_list[i][0] - coord_list[i + 1][0]) + abs(coord_list[i][1] - coord_list[i + 1][1]) != 50:
                    return False
    return True

def game_over(message, screen, screen_width, screen_height):
    font = pygame.font.SysFont(None, 50)
    text = font.render(message, True, WHITE)
    text_rect = text.get_rect(center=(screen_width // 2, screen_height // 2))

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

        # Отрисовка на экране текста
        screen.fill(BLACK)
        screen.blit(text, text_rect)
        pygame.display.update()

def draw_restart_button(screen, screen_width, screen_height):
    font = pygame.font.SysFont(None, 50)
    text = font.render("⟳", True, BLUE)
    text_rect = text.get_rect(topright=(screen_width - 10, 10))

    pygame.draw.circle(screen, BLUE, text_rect.center, text_rect.width // 2 + 5, 5)
    pygame.draw.line(screen, BLUE, (text_rect.centerx, text_rect.top + 10), (text_rect.centerx, text_rect.bottom - 10),
                     5)
    pygame.draw.line(screen, BLUE, (text_rect.left + 10, text_rect.centery), (text_rect.right - 10, text_rect.centery),
                     5)

class Game:
    def __init__(self):
        self.__screen_width = 800
        self.__screen_height = 700
        self.__block_size = 50
        self.__screen = pygame.display.set_mode((self.__screen_width, self.__screen_height))
        self._level_file_1 = 'level_1.txt'
        self.__clock = pygame.time.Clock()

        self.ui_manager = UIManager((self.__screen_width, self.__screen_height))
        self.level_list = ['Level 1', 'Level 2', 'Level 3']
        self.level_menu = UIDropDownMenu(self.level_list, self.level_list[0], pygame.Rect(0, 0, 100, 50), manager=self.ui_manager)

    def start(self):
        level_1 = Level(self._level_file_1)
        main_block = level_1.get_main_block()

        while True:
            restart_button_rect = pygame.Rect(770, 10, 60, 60)

            # Handle events
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    quit()
                if event.type == pygame.MOUSEBUTTONDOWN:
                    if restart_button_rect.collidepoint(event.pos):
                        level_1 = Level(self._level_file_1)
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_LEFT:
                        main_block = level_1.get_main_block()
                        main_block.x -= self.__block_size
                        if main_block.x < 0:
                            main_block.x = 0
                        for row in level_1.blocks:
                            for block in row:
                                if main_block != block and main_block.x == block.x and main_block.y == block.y:
                                    block.x -= self.__block_size
                                    if block.x < 0:
                                        block.x = 0
                    elif event.key == pygame.K_RIGHT:
                        main_block = level_1.get_main_block()
                        main_block.x += self.__block_size
                        if main_block.x + self.__block_size > self.__screen_width:
                            main_block.x = self.__screen_width - self.__block_size
                        for row in level_1.blocks:
                            for block in row:
                                if main_block != block and main_block.x == block.x and main_block.y == block.y:
                                    block.x += self.__block_size
                                    if block.x + self.__block_size > self.__screen_width:
                                        block.x = self.__screen_width - BLOCK_SIZE
                    elif event.key == pygame.K_UP:
                        main_block = level_1.get_main_block()
                        main_block.y -= self.__block_size
                        if main_block.y < 0:
                            main_block.y = 0
                        for row in level_1.blocks:
                            for block in row:
                                if main_block != block and main_block.x == block.x and main_block.y == block.y:
                                    block.y -= self.__block_size
                                    if block.y < 0:
                                        block.y = 0
                    elif event.key == pygame.K_DOWN:
                        main_block = level_1.get_main_block()
                        main_block.y += self.__block_size
                        if main_block.y + self.__block_size > self.__screen_height:
                            main_block.y = self.__screen_height - self.__block_size
                        for row in level_1.blocks:
                            for block in row:
                                if main_block != block and main_block.x == block.x and main_block.y == block.y:
                                    block.y += self.__block_size
                                    if block.y + self.__block_size > self.__screen_height:
                                        block.y = self.__screen_height - self.__block_size
                self.ui_manager.process_events(event)

            for i, row in enumerate(level_1.blocks):
                for j, block in enumerate(row):
                    if j == level_1.main_block_index:
                        continue
                    if block.x < level_1.get_main_block().x + BLOCK_SIZE and \
                            block.x + BLOCK_SIZE > level_1.get_main_block().x and \
                            block.y < level_1.get_main_block().y + BLOCK_SIZE and \
                            block.y + BLOCK_SIZE > level_1.get_main_block().y:
                        if level_1.get_main_block().x > block.x:
                            level_1.get_main_block().x = block.x - BLOCK_SIZE
                        elif level_1.get_main_block().x < block.x:
                            level_1.get_main_block().x = block.x + BLOCK_SIZE
                        elif level_1.get_main_block().y > block.y:
                            level_1.get_main_block().y = block.y - BLOCK_SIZE
                        else:
                            level_1.get_main_block().y = block.y + BLOCK_SIZE
            for row in level_1.blocks:
                for block in row:
                    if block != level_1.get_main_block():
                        if block.x == level_1.main_block.x and block.y == level_1.main_block.y:
                            # Move the main block back to its previous position
                            if event.key == pygame.K_LEFT:
                                level_1.main_block.x += BLOCK_SIZE
                            elif event.key == pygame.K_RIGHT:
                                level_1.main_block.x -= BLOCK_SIZE
                            elif event.key == pygame.K_UP:
                                level_1.main_block.y += BLOCK_SIZE
                            elif event.key == pygame.K_DOWN:
                                level_1.main_block.y -= BLOCK_SIZE
                            break
            # Update game state
            main_block = level_1.get_main_block()
            main_block.update(self.__screen)
            self.ui_manager.update(pygame.time.Clock().tick(60) / 1000.0)

            all_blocks = [block for row in level_1.blocks for block in row]
            blocks = [b for b in all_blocks if b is not main_block]
            if check_win(blocks):
                game_over("YOU WIN!", self.__screen, self.__screen_width, self.__screen_height)

            self.__screen.fill((255, 255, 255))

            self.ui_manager.draw_ui(self.__screen)
            level_1.draw(self.__screen)

            draw_restart_button(self.__screen, self.__screen_width, self.__screen_height)
            # Draw game world

            # Update display
            pygame.display.flip()

            # Limit framerate
            self.__clock.tick(60)

game = Game()
game.start()
pygame.quit()


#!/usr/bin/env python3

import argparse
from aiy.audio import say

def set_args():
    global args
    parser = argparse.ArgumentParser(description="Test Voice")
    parser.add_argument('words', nargs='*', help='Words to say')
    parser.add_argument("--lang", help='language BCP-47 code')
    parser.add_argument("--volume", help='volume. Default: 60', type=int)
    parser.add_argument("--pitch", help='pitch. Default: 130', type=int)
    args = parser.parse_args()

if __name__ == '__main__':
    set_args()
    words = ' '.join(args.words)
    say(words, args.lang, args.volume, args.pitch)


import numpy as np
from utils import logist, spm_norm, spm_softmax
from MDP_prelims import MDP_prelims
import copy

def spm_MDP_VB_LC(MDP):
    """
    # A simplified version of spm_MDP_VB (see https://www.fil.ion.ucl.ac.uk/spm/)
    # amended to include calculation of state-action prediction error and model
    # decay. Also includes 'environmental' A and B matrices (A_ENV, B_ENV),
    # which represent the 'real' environment and are used to generate
    # observations for the agent / work out the agent's actual location in the
    # real world.

    # Original code spm_MDP_VB  Copyright (C) 2005 Wellcome Trust Centre for
    # Neuroimaging Karl Friston

    # Amendments:  Anna Sales 2018, University of Bristol.

    # Inputs: an vector of structs, with one struct per trial. MDP structs hold
    # all the parameters relevant for the model during one trial, e.g. a,b,d
    # parameters, states, policies etc.

    # Returns: the same struct, but with a complete record of all calculations,
    # updates, actions and observations during each trial.
    """
    ## if there are multiple trials ensure that parameters are updated
    #--------------------------------------------------------------------------

    if isinstance(MDP, list):
        OUT = copy.deepcopy(MDP)
        for i in range(len(MDP)):
            # update concentration parameters
            #------------------------------------------------------------------
            if i > 1:
                if hasattr(OUT[i - 1],'a'): MDP[i].a = OUT[i - 1].a
                if hasattr(OUT[i - 1],'b'): MDP[i].b = OUT[i - 1].b
                if hasattr(OUT[i - 1],'c'): MDP[i].c = OUT[i - 1].c
                if hasattr(OUT[i - 1],'d'): MDP[i].d = OUT[i - 1].d
                if hasattr(OUT[i - 1],'beta'): MDP[i].beta = OUT[i - 1].beta
                if hasattr(OUT[i - 1],'SAPEall'): MDP[i].SAPEall= OUT[i - 1].SAPEall
            # solve this trial (send this MDP down to the main code below)
            #------------------------------------------------------------------
            OUT[i] = spm_MDP_VB_LC(MDP[i])
        MDP = OUT
        return MDP

    # get preliminaries needed to start the trial - e.g. initial values of state
    # and location, current versions of A,B,D based on a,b,d, values of precision.
    V, T, No, Np, Ns, Nu, A , qA, B, qB, rB, sB, d, qD, Vo, H, alpha, beta, s, o, P, x, X, u ,a, qbeta, gu, A_ENV, B_ENV = MDP_prelims(MDP)

    ## solve
    #==========================================================================
    Ni    = MDP.Ni                         # number of VB iterations
    xn    = np.zeros((Ni,Ns,T,T,Np)) + 1/Ns # history of state updates
    un    = np.zeros((Np,T*Ni))             #    policy updates
    p     = range(Np)   # number of allowable policies
    X_t = np.zeros(list(X.shape) + [T])
#    SAPE = np.zeros(T - 1)
    SAPE = []

    for t in range(T):    #Do updates, pick action, get observations for every time point in the trial

        if t > 0:

            pol_OK = [v == a[t - 1] for v in V[t - 1,:]]  #include allowable policies only
#            [~,p] = find(pol_OK)
            p = np.flatnonzero(pol_OK)

        # Get state updates over all times in task (tau) past and future
        # Variational updates (hidden states) under sequential policies
        #======================================================================
        F = np.zeros((Np,T))
        #NB 'G'  the expected free energy of policies over future time points,
        #is denoted by 'Q' in this code.

        for k in p:  # State updates for each policy, over each time point.
            x[:,:,k] = spm_softmax(np.log(x[:,:,k]) / 2)  #reset.
            for i in range(Ni): # Do Ni iterations of the state update equations (and calculate F / components of Q at the same time)
                px = x[:,:,k]  # store state probabilities for each time, for each policy
                for j in range(T):
                    # current state
                    #----------------------------------------------------------
                    qx = np.log(x[:,j,k]).reshape((-1,1))

                    # transition probabilities
                    #------------------------------------------------------
                    if k > Np - 1:
                        fB = sH
                        bB = rH
                    else:
                        if j > 0:
                            fB = sB[V[j - 1, k]]
                        if j < T - 1:
                            bB = rB[V[j, k]]

                    # evaluate free energy and gradients (v = dFdx)
                    #----------------------------------------------------------
                    v = qx
                    if j <= t: v = v - qA[o[j],:].reshape((-1,1))
                    if j == 0: v = v - qD
                    if j > 0: v = v - np.log(np.dot(fB, x[:,j - 1,k].reshape(-1,1)))
                    vF = v
                    if j < T - 1: v = v - np.log(np.dot(bB, x[:,j + 1,k].reshape(-1,1)))

                    # free energy and belief updating
                    #----------------------------------------------------------
                    F[k,j]  = np.dot(-x[:,j,k].reshape((1,-1)), vF)    ## Free energy of policies at each time point (F(pi,tau))
                    px[:,j] = spm_softmax(qx - v/Ni).ravel() ##  update equation for states.

                # hidden state updates
                #--------------------------------------------------------------
                x[:,:,k] = px    #probs of states (rows) over each policy (k, sheets), over each time (cols)

        ##  Get expected (future) FE over policies (negative path integral of free energy of policies (Q)
        #======================================================================
        Q = np.zeros((Np,T))

        for k in p:  #for each policy

            for j in range(T):
                qx = np.dot(A, x[:,j,k])
                Q[k,j] = np.dot(qx.T, Vo[:,j] - np.log(qx)) + np.dot(H, x[:,j,k]) #Expected free energy of k-th policy at time t=j

        # Calculate Q, F as sum over time - total free energy in past/future.
        F = np.sum(F, 1, keepdims = True)
        Q = np.sum(Q, 1, keepdims = True)

        ## Get policy probability and precision, pi / beta and gamma

        for i in range(Ni):

            # policy (u)
            #------------------------------------------------------------------
            #TODO: No i in this for loop
            qu = spm_softmax( np.dot(gu[t], Q[p]) + F[p] )  #pi, probability of each policy
            pu = spm_softmax( np.dot(gu[t], Q[p]) ) # pi_0
            v = qbeta - beta + np.dot((qu - pu).T, Q[p])   #update equation for beta

            # precision (gu)

            qbeta = qbeta - v/2 ## UPDATE = OLD BETA + ERROR
            gu[t] = 1/qbeta

            u[p,t] = qu.ravel()  #store history of values of policy prob.

        # Bayesian model averaging of hidden states over policies

        for i in range(T):
            X[:,i] = np.dot(np.squeeze(x[:,i,:]), u[:,t].reshape((-1,1))).ravel()
            X_t[:,i,t] = X[:,i]

        # Calculate the state-action prediction error as a KL divergence
        # between successive BMA distributions.

        if t > 0:
            St_lg_change = np.log(X_t[:,:,t]) - np.log(X_t[:,:,t-1])
            SAPE.append(np.sum(X_t[:,:,t] * St_lg_change))

        # action selection and observations

        if t < T - 1:
            # posterior expectations about (remaining) actions (q)
            if len(p) > 1:
                q = np.unique(V[t,p]) #make sure if only picks allowable actions.
            else:
                q = V[t,p]

            v = np.log( np.dot(A, X[:,t + 1].reshape((-1,1))) )

            for j in q:
                qo = np.dot(np.dot(A, B[j]), X[:,t].reshape((-1,1)))
                P[j,t] = np.dot((v - np.log(qo)).T, qo) + 16

            # action selection
            P[:,t] = spm_softmax(np.dot(alpha, P[:,t]))
            a[t] = np.argmax(P[:,t], axis = 0)  #deterministic

            # Use environment matrices to work out where agent ends up in the
            # real world.

#            s[t + 1] = find(rand < cumsum(B_ENV{a(t)}(:,s(t))),1)
            s[t + 1] = np.flatnonzero(np.random.uniform() < np.cumsum(B_ENV[a[t],:,s[t]]))[0]

            # Use environmental matrices to get an observation from the real
            # world.

            o[t + 1] = np.flatnonzero(np.random.uniform() < np.cumsum(A_ENV[:,s[t + 1]]))[0]

            # save outcome and state sampled
            #------------------------------------------------------------------
            gu[t + 1]   = gu[t]

    ## End of trial. Now do updates to concentration parameters.

    #Calculate model decay factor (here denoted 'df') based on logistic function using prediction errors

    # These are mean values worked out for GNG / EE tasks from 100 trials with
    # df =16.
    if T == 3:   #GNG
        mean = 1
    elif T == 2:  #EE
        mean = 1.8

    if hasattr(MDP, 'df_set') and (MDP.df_set is not None):  #If we're forcing the agent to use a fixed df.
        df = MDP.df_set
        df_settings = {'vals': df}    #store the values of df that the MDP used.
    else:   #Or use SAPE to calculate it from a logistic function.
        min_d = 2
        max_d = 32
        grad_d = 8
        df = logist(np.max(SAPE), grad_d, max_d, min_d, mean)
        df_settings = {'grad_d': grad_d, 'max_d': max_d, 'min_d': min_d, 'mean': mean}     #store the values of df that the MDP used.

    ##

    for t in range(T):

    # update concentration parameters - use model decay calculated above.
    #----------------------------------------------------------------------
        if hasattr(MDP, 'a'):
            decay = np.zeros(MDP.a.shape)

        if hasattr(MDP,'a'):
            i = MDP.a > 0
            tmp = np.zeros((No,1))
            tmp[o[t],0] = 1
            da = np.dot(tmp, X[:,t].reshape((1,-1)))
            dec_weights = np.tile(da[o[t], :], (No,1))
            dec_weights[~i] = 0   #don't change things that are already 0.
            mask = np.ones(MDP.a.shape, dtype = bool)
            mask[o[t], :] = 0  #only want to decay elements in row for observation seen, as per outer product in update equation
            MDP.a[mask] = MDP.a[mask] - dec_weights[mask] * ( (MDP.a[mask] - 1) / df )  #decay
            MDP.a[i] = MDP.a[i] + da[i]     #increment

        if hasattr(MDP,'b') and t > 0:

            for k in range(Np):
                v           = V[t - 1, k]
                i           = MDP.b[v] > 0
                db          = np.dot(np.dot(u[k, t - 1], x[:,t,k].reshape((-1,1))), x[:,t - 1,k].reshape((1,-1)))
                MDP.b[v][i] = MDP.b[v][i] + db[i] - (MDP.b[v][i] -1)/df

    # initial hidden states:
    #--------------------------------------------------------------------------
    if hasattr(MDP,'d'):
        i        = MDP.d > 0
        MDP.d[i] = MDP.d[i] + X[i.ravel(),0] - (MDP.d[i] - 1)/df

    ## assemble results and place in MDP structure
    MDP.P   = P              # probability of action at time 1,...,T - 1
    MDP.Q   = x              # conditional expectations over N hidden states
    MDP.X   = X              # Bayesian model averages
    MDP.X_t = X_t              # BMA at each time point over all times in a trial.
    MDP.R   = u              # conditional expectations over policies
    MDP.o   = o              # outcomes at 1,...,T
    MDP.s   = s              # states at 1,...,T
    MDP.u   = a              # action at 1,...,T
    MDP.SAPEall = MDP.SAPEall + SAPE
    MDP.w   = gu             # posterior expectations of precision (policy)
    MDP.C   = Vo             # utility
    MDP.A = A                  # this is the A matrix that has been used throughout this MDP (next time it'll be updated from a)
    MDP.A_ENV = A_ENV          # enivornmental A_ENV
    MDP.Ni = Ni               # number of iterations
    MDP.SAPE = SAPE            # state action prediction error based on changes to BMA states
    MDP.df = df               # decay factor used in trial
    MDP.beta = 1/gu[T-1]       # carry forward beta
    MDP.dfsettings = df_settings
    MDP.B = B

    return MDP


"""
Book: Building RESTful Python Web Services
Chapter 3: Improving and adding authentication to an API with Django
Author: Gaston C. Hillar - Twitter.com/gastonhillar
Publisher: Packt Publishing Ltd. - http://www.packtpub.com
"""
from rest_framework import serializers
from encargos.models import Encargo
from encargos.models import Producto
from encargos.models import Detalle_Encargo
from django.contrib.auth.models import User

class UserEncargoSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Encargo
        fields = (
            'url',
            'name')

class UserSerializer(serializers.HyperlinkedModelSerializer):
    encargos = UserEncargoSerializer(many=True, read_only=True)

    class Meta:
        model = User
        fields = (
            'url',
            'pk',
            'username',
            'encargos',
            'password'
        )

class EncargoSerializer(serializers.HyperlinkedModelSerializer):

    class Meta:
        model = Encargo
        fields = (
            'url',
            'pk',
            'created',
            'completado'
    )

class ProductoSerializer(serializers.HyperlinkedModelSerializer):
    localizacion = serializers.ChoiceField(choices=Producto.AREAS)
    area_description = serializers.CharField(source='get_area_display', read_only=True)
    class Meta:
        model = Producto
        fields = (
            'url',
            'pk',
            'name',
            'localizacion',
            'area_description',
        )

class Detalle_EncargoSerializer(serializers.ModelSerializer):
    localizacion_producto = ProductoSerializer(many=True, read_only=True)
    encargo_completado = EncargoSerializer(many=True, read_only=True)
    class Meta:
        model = Detalle_Encargo
        fields = (
            'url',
            'pk',
            'encargo',
            'producto',
            'cantidade',
            'localizacion_producto',
            'encargo_completado'
        )


__author__ = 'subadmin'

import libvirt
from classes.ClusterPool import ClusterPool

class ClusterInit(ClusterPool):

    vmInCluster = {}

    __states = {
    libvirt.VIR_DOMAIN_NOSTATE: 'no state',
    libvirt.VIR_DOMAIN_RUNNING: 'running',
    libvirt.VIR_DOMAIN_BLOCKED: 'blocked on resource',
    libvirt.VIR_DOMAIN_PAUSED: 'paused by user',
    libvirt.VIR_DOMAIN_SHUTDOWN: 'being shut down',
    libvirt.VIR_DOMAIN_SHUTOFF: 'shut off',
    libvirt.VIR_DOMAIN_CRASHED: 'crashed',
    }

    def __init__(self,poolConfig):
        ClusterPool.__init__(poolConfig)

    def add_node(self,ip):
        conn = libvirt.open("qemu+ssh://root@%s/system" % ip)
        self.vmInCluster.update({conn.getHostname(): conn})
        self.__inspest_node(conn)

    def __inspest_node(self,node_conn):
        pools = node_conn.listDefinedStoragePools()
        if self.get_pool_name() not in pools:
            storage = node_conn.storagePoolDefineXML(self.get_pool_xml(),0)
            storage.build(0)
            storage.create(0)

    def __get_valid_host(self):
        return self.vmInCluster[0]

    def __init_vm(self,vm_name,disk_size):
        vol_xml = self.create_volume_xml(vm_name,disk_size)
        conn = self.__get_valid_host()

    def is_vm_run(self,state):
        if self.__states.get(state, state) == 'running':
            return True
        return False

    def get_vm_in_cluster(self,run_only = False, get_by = "name" ):
        vms = {}
        for host in self.vmInCluster:
            for id in self.vmInCluster[host].listDomainsID():
                dom = self.vmInCluster[host].lookupByID(id)
                [state, maxmem, mem, ncpu, cputime] = dom.info()
                if (run_only and self.is_vm_run(state)) or not run_only:
                    if get_by == "status":
                        vms.update({self.__states.get(state, state): dom})
                    else:
                        vms.update({dom.name(): dom})
        return vms


from flask_session_captcha import FlaskSessionCaptcha
import base64
import logging
from flask import session

class MyFlaskSessionCaptcha(FlaskSessionCaptcha):
    def __init__(self, *args, **kwargs):
        FlaskSessionCaptcha.__init__(self, *args, **kwargs)

    def generate(self):
        """ Remplace les 1 et les 7 par d'autres valeurs par souci de lisibilité """
        answer = self.rand.randrange(self.max)
        answer = str(answer).zfill(self.digits)
        answer = answer.replace("1", "2")
        answer = answer.replace("7", "8")
        image_data = self.image_generator.generate(answer)
        base64_captcha = base64.b64encode(image_data.getvalue()).decode("ascii")
        logging.debug('Generated captcha with answer: ' + answer)
        session['captcha_answer'] = answer
        return base64_captcha


import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from sklearn import datasets

iris = datasets.load_iris()
X = iris.data[:, :2] #2 boyutlu veri
y = iris.target

x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5

plt.figure(2, figsize=(8,6))
plt.clf()

# Plot the trainig points
plt.scatter(X[:, 0], X[:, 1], c=y, cmap = plt.cm.Set1, edgecolor='k' )

plt.xlabel('Sepal length')
plt.ylabel('Sepal width')

plt.xlim(x_min, x_max)
plt.ylim(y_min,y_max)
plt.xticks()
plt.yticks()

fig = plt.figure(1,figsize = (8,6))
ax = Axes3D(fig, elev =-150, azim=110)
ax.scatter(iris.data[:, 0], iris.data[:, 1], iris.data[:, 2], c=y, cmap=plt.cm.Set1, edgecolor='k', s=40)

ax.set_title('IRIS Verisi')
ax.set_xlabel('birinci özellik')
ax.w_xaxis.set_ticklabels([])
ax.set_ylabel('ikinci özellik')
ax.w_yaxis.set_ticklabels([])
ax.set_zlabel('üçüncü özellik')
ax.w_zaxis.set_ticklabels([])

plt.show()


import pandas as pd
import matplotlib.pyplot as plt
plt.style.use('seaborn-whitegrid')
import numpy as np
# import requests

# from bokeh.io import output_notebook
# from bokeh.plotting import figure, show
# from bokeh.models import ColumnDataSource

df = pd.read_csv('global_dataset.csv', delimiter = ",", header=0, low_memory=False)

# total number of cases globally by gender
df.groupby('gender')['case_id'].nunique().plot(kind='bar')

# looking only at Female cases per year, globally
female_cases = df[df['gender'].str.startswith('Fem', na=False)]
female_cases.groupby('yearOfRegistration')['case_id'].nunique().plot(kind='bar')

plt.show()

# plotting male and female cases from 2002 to 2018
fig, ax = plt.subplots(figsize=(15,7))
# use unstack()
df.groupby(['yearOfRegistration','gender']).count()['case_id'].unstack().plot(ax=ax)
fig.suptitle('Human Trafficking Cases', fontsize=20)
ax.set_ylabel('Number of Cases')
ax.set_xlabel('Year of Registration')

plt.savefig('gender_cases.png')

# scatter plot of cases per country per year
df2 = pd.read_csv('data/cases_per_citizenship.csv', delimiter = ",", header=0)

ax1 = df2.plot(kind='scatter', x='yearOfRegistration', y='UA', color='r', s=0.5*df2.UA, label="Ukraine")
ax2 = df2.plot(kind='scatter', x='yearOfRegistration', y='RO', color='g', s=0.5*df2.RO, label="Romania", ax=ax1)
ax3 = df2.plot(kind='scatter', x='yearOfRegistration', y='ID', color='b', s=0.5*df2.ID, label='Indonesia', ax=ax1)
ax4 = df2.plot(kind='scatter', x='yearOfRegistration', y='BY', color='y', s=0.5*df2.BY, label='Belarus', ax=ax1)


import os
import xml.etree.ElementTree as ET
from PIL import Image
import datetime

class ToVOCConverter(object):
    '''
    Parent class for all the converters
    '''

    def __init__(self,imageFolder,sourceLabelFolder,outputLabelFolder):
        '''
        imageFolder: folder where all the images are
        sourceLabelFolder: path to folder where source data is saved
        outputLabelFolder: path where ouptut files are to be saved
        '''
        assert os.path.isdir(imageFolder), "Image folder {:} does not exist!".format(imageFolder)
        assert os.path.isdir(sourceLabelFolder), "Source label folder {:} does not exist!".format(sourceLabelFolder)
        assert os.path.isdir(outputLabelFolder), "Output label folder {:} does not exist!".format(outputLabelFolder)

        self.imageFolder = imageFolder
        self.sourceLabelFolder = sourceLabelFolder
        self.outputLabelFolder = outputLabelFolder
        self.currentOutFile = None
        self.currentImageFile = None

        self.time = datetime.datetime.now().isoformat()

        self.imageFormat = None
        self.database = None

        self.currentImageShape = None

    def initializeXMLFile(self):
        '''
        Write the data that has nothing to do with the labels
        '''
        root = ET.Element("annotation")
        ET.SubElement(root,"folder").text = self.imageFolder
        ET.SubElement(root,"filename").text = self.currentImageFile

        source = ET.SubElement(root,"source")
        ET.SubElement(source,"database").text = self.database
        ET.SubElement(source,"annotation").text = self.database

        conversion = ET.SubElement(root,"conversion")
        ET.SubElement(conversion,"created").text = self.time
        ET.SubElement(conversion,"updated").text = self.time

        with Image.open(os.path.join(self.imageFolder,self.currentImageFile)) as img:
            width,height = img.size
            depth = img.mode
        assert depth=="RGB", "Colormode for image {} is not RGB, it is {}".format(self.currentImageFile,depth)
        depth = 3
        self.currentImageShape = (width,height,depth)

        imgSize = ET.SubElement(root,"size")
        ET.SubElement(imgSize,"width").text = str(width)
        ET.SubElement(imgSize,"height").text = str(height)
        ET.SubElement(imgSize,"depth").text = str(depth)

        return root

    def createXMLLabelFile(self,fileName):
        '''
        This method needs to be overridden in the child class
        '''
        assert False, "createXMLLabelFile needs to be implemented in the childClass"

    def createXMLLabel(self,line):
        '''
        Create a xml style label for the line that is passed in. Return the xml data.
        Must be implemented in the child class
        '''
        assert False, "createXMLLabel needs to be implemented in the childClass"

    def convertDataset(self):
        '''
        Convert all of the label files in self.sourceLabelFolder

        Need to catch any and all errors
            -image does not exist for label
            -label does not exist for an image
        '''
        assert False, "convertDataset needs to be implemented in the childClass"


import ply.lex as lex
from ply.lex import Lexer

def InTheList(strList, arg):
    for i in strList:
        if i == arg:
            return True
    return False

class LexAnalyser:
    tokens = [
        'AIMNAME',
        'COLON',
        'SPACE'
    ]
    token_list = []
    dict = {}
    set = set()
    def t_COLON(self, t):
        r":"
        return t

    def t_AIMNAME(self, t):
        r"[A-Za-z\._][A-Za-z0-9\._]*"
        return t

    def t_error(self, t):
        print("Illegal Token")
        return False

    def __init__(self):
        self.lexer: Lexer = lex.lex(module=self)

    def t_SPACE(self, t):
        r"[ \t]+"

    def gen_tokens(self, inp):
        self.lexer.input(inp)
        while True:
            try:
                tok = self.lexer.token()
            except lex.LexError:
                self.clear()
                return False
            if not tok:
                break
            self.token_list.append(tok)
        return self.token_list

    def clear(self):
        self.token_list.clear()

    def checkString(self):
        if len(self.token_list) < 2:
            return False
        token1 = self.token_list.pop(0)
        token2 = self.token_list.pop(0)
        if token1.type != 'AIMNAME' or token2.type != 'COLON':
            return False
        self.set.add(token1.value)
        list1 = []
        local_dict = {}
        list1.append(token1.value)
        for token in self.token_list:
            if token.type != 'AIMNAME':
                return False
            if InTheList(list1, token.value):
                return False
            list1.append(token.value)
            if local_dict.get(token.value) == None:
                local_dict[token.value] = 1
            else:
                local_dict[token.value] += 1
        for k in local_dict:
            if self.dict.get(k) == None:
                self.dict[k] = 1
            else:
                self.dict[k] += 1
        self.set.add(token1.value)
        return True

    def analyse(self, str):
        self.gen_tokens(str)
        res = self.checkString()
        self.clear()
        return res


import asyncio
import logging

logging.basicConfig(format='%(asctime)s %(message)s', datefmt='[%H:%M:%S]')
log = logging.getLogger()
log.setLevel(logging.INFO)

async def sleeper(name, delay, repeat):
    for i in range(1, repeat + 1):
        log.info(f"{name}: START ({i}/{repeat}) wait:{delay}s")
        await asyncio.sleep(delay)
        log.info(f"{name}: END   ({i}/{repeat}) wait:{delay}s")
    return name

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    fut = asyncio.gather(sleeper("A", 6, 1))
    log.info("main: START run_until_complete")
    loop.run_until_complete(fut)
    log.info("main: END   run_until_complete")