from datetime import timedelta
import hashlib
import logging
import os
import sys
from time import sleep
import threading
from threading import Thread
import queue

from crate import client as crate_client
import pymongo
from bson.objectid import ObjectId
from volttron.platform import jsonapi

from volttron.platform import get_volttron_root, get_services_core

sys.path.insert(0, get_services_core("CrateHistorian"))
from crate_historian import crate_utils
from volttron.platform.agent import utils
from volttron.platform.dbutils import mongoutils

logging.basicConfig(level=logging.DEBUG)
_log = logging.getLogger(__name__)

for key in logging.Logger.manager.loggerDict:
    _log.debug(key)

logging.getLogger('crate.client.http').setLevel(logging.INFO)
logging.getLogger('urllib3.connectionpool').setLevel(logging.INFO)

root = os.path.dirname(os.path.abspath(__file__))
with open('{}/crate_config'.format(root), 'r') as fp:
    crate_params = jsonapi.loads(fp.read())

root = os.path.dirname(os.path.abspath(__file__))
with open('{}/mongo_config'.format(root), 'r') as fp:
    mongo_params = jsonapi.loads(fp.read())

MAX_QUEUE_SIZE = 50000
QUEUE_BATCH_SIZE = 5000

class TableQueue(queue.Queue, object):
    def __init__(self, table_name):
        super(TableQueue, self).__init__()
        self.table_name = table_name

def get_mongo_db():
    mongo_client = mongoutils.get_mongo_client(mongo_params["connection"]["params"])

    return mongo_client.get_default_database()

insert_errors = open('insert_error.log', mode='w')
topics = {}
table_names = ('device', 'device_string', 'analysis', 'analysis_string',
               'record', 'datalogger', 'datalogger_string')

# Create a queue for each of the tables.
table_queues = {}
for t in table_names:
    table_queues[t] = TableQueue(t)

total_time = 0
batch = []
batch_table_name = {}
for t in table_names:
    batch_table_name[t] = []

def build_topics_mapping():
    global topics
    mongo_db = get_mongo_db()

    for obj in mongo_db.topics.find({}):
        tname = obj['topic_name']
        tsplit = tname.split('/')
        hash = hashlib.md5(tname).hexdigest()
        topics[obj['_id']] = dict(name=tname, hash=hash)
        if tsplit[0] in ('PNNL', 'PNNL-SEQUIM', 'pnnl', 'pnnl-SEQUIM'):
            topics[obj['_id']]['table'] = 'device'
        elif tsplit[0] in ('Economizer_RCx', 'Airside_RCx'):
            if tsplit[-1] == 'Timestamp':
                topics[obj['_id']]['table'] = 'analysis_string'
            else:
                topics[obj['_id']]['table'] = 'analysis'
        else:
            topics[obj['_id']]['table'] = tsplit[0]

schema = crate_params['connection'].get('schema', 'historian')
INSERT_TOPIC_QUERY = """INSERT INTO {schema}.{table} (id, name, data_table)
                          VALUES(?, ?, ?)
                          ON DUPLICATE KEY UPDATE name=name, data_table=data_table
                        """.format(schema=schema, table='topic')

INSERT_DATA_QUERY = """INSERT INTO {schema}.%table% (topic_id, ts, result)
                      VALUES(?, ?, ?)
                      ON DUPLICATE KEY UPDATE result=result
                    """.format(schema=schema)

def insert_topic(cursor, id, name, data_table, flush=False):
    global total_time
    global batch

    batch.append((id, name, data_table))
    if len(batch) > 1000 or flush:
        cursor.executemany(INSERT_TOPIC_QUERY, batch)
        total_time += cursor.duration
        batch = []

# def insert_crate_data(cursor,table_name, topic_id, ts, data):
#     global total_time
#     global batch_table_name
#
#     ts_formatted = utils.format_timestamp(ts)
#     insert_query = INSERT_DATA_QUERY.format(table_name)
#
#     if table_name in ('analysis', 'datalogger', 'device'):
#         if not isinstance(data, int) and not isinstance(data, float):
#             insert_errors.write("table: {} topic: {} ts: {} data: {}".format(
#                 table_name, topic_id, ts, data))
#             return
#
#     the_batch = batch_table_name[table_name]
#     the_batch.append((topic_id, ts, data))
#
#     if len(the_batch) > 1000:
#         try:
#             results = cursor.executemany(insert_query, the_batch)
#         except ProgrammingError as e:
#             insert_errors.write(str(e))
#         total_time += cursor.duration
#         batch_table_name[table_name] = []

crate_created = False

def get_crate_db():
    global crate_created
    crate_db = crate_client.connect(crate_params["connection"]["params"]["host"],
                                    error_trace=True)
    if not crate_created:
        crate_utils.create_schema(crate_db,
                                  crate_params['connection'].get('schema',
                                                                 'historian'))
        crate_created = True
    return crate_db

def insert_topics_to_crate():
    global total_time

    crate_db = get_crate_db()
    cursor = crate_db.cursor()
    inserted = 0
    for mongo_id, obj in topics.items():
        insert_topic(cursor, obj["hash"], obj["name"], obj['table'])
        inserted += 1
        if inserted % 1000 == 0:
            _log.debug("Inserted topic {} in {}ms".format(inserted, total_time))

    if len(batch) > 0:
        cursor.executemany(INSERT_TOPIC_QUERY, batch)
        total_time += cursor.duration

    cursor.close()
    crate_db.close()

def insert_from_queue(insertion_queue, inserted_metrics_queue, logging_queue):
    internal = []
    insert_query = INSERT_DATA_QUERY.replace('%table%',
                                             insertion_queue.table_name)
    _log.debug('Starting queue for: {}'.format(insertion_queue.table_name))

    while True:
        item = insertion_queue.get(block=True)
        if item == "done":
            break
        internal.append(item)
        if len(internal) >= QUEUE_BATCH_SIZE:
            try:
                conn = get_crate_db()
                cursor = conn.cursor()
                cursor.executemany(insert_query, internal)
                inserted_metrics_queue.put((len(internal), cursor.duration))
                cursor.close()
                conn.close()
                internal = []
            except Exception as ex:
                logging_queue.put('An excption occured for {} {}'.format(
                    insertion_queue.table_name, ex.message))
                sys.stderr.write('Exception 1: {}'.format(ex.message))

    if len(internal) > 0:
        try:
            conn = get_crate_db()
            cursor = conn.cursor()
            cursor.executemany(insert_query, internal)
            inserted_metrics_queue.put((len(internal), cursor.duration))
            internal = []
            cursor.close()
        except Exception as ex:
            sys.stderr.write('Exception 2: {}'.format(ex.message))
    _log.debug('Ending queue for: {}'.format(insertion_queue.table_name))
    # threading.current_thread().exit()

insert_threads = []
metric_queue = queue.Queue()
logger_queue = queue.Queue()

for q in table_queues.values():

    insert_thread = Thread(target=insert_from_queue, args=[q, metric_queue,
                                                           logger_queue])
    insert_thread.daemon = True
    insert_thread.start()
    insert_threads.append(insert_thread)

def log_write(queue):
    with open("errors.txt", "w") as ferr:
        try:
            while True:
                item = queue.get(block=True)
                ferr.write("{}\n".format(str(item)))
        except Exception as ex:
            sys.stderr.write("Logger exception: {}".format(str(ex)))

logger_thread = Thread(target=log_write, args=[logger_queue])
logger_thread.daemon = True
logger_thread.start()

def start_transfer_data():
    mongo_db = get_mongo_db()
    total_time = 0
    added_to_queue = 0
    current_topic_id = None
    last_timestamp = None
    written_records = 0
    last_shown = 0

    try:
        total_data = mongo_db.data.count()
        _log.debug('Total data: {}'.format(total_data))
        result = next(mongo_db.data.find({}).sort([('ts', pymongo.ASCENDING)]).limit(1))
        _log.debug('Before: {}'.format(result['ts']))
        last_timestamp = result['ts'] - timedelta(days=1)
        _log.debug('After: {}'.format(result['ts']))

        for obj in mongo_db.data.find({'ts': {"$gt": last_timestamp}
                                       #    ,
                                       # 'topic_id': {"$in": [
                                       #     ObjectId("573f2244c56e522eface840b"),
                                       #     ObjectId("573f2244c56e522eface840f"),
                                       #     ObjectId("573f8708c56e522efacea313")
                                       # ]}
                                       }, no_cursor_timeout=True).sort([
            ('ts', pymongo.ASCENDING)
        ]):
            if obj['topic_id'] not in topics:
                _log.error("MISSING topic_id {} in data table".format(obj['topic_id']))
                continue

            current_topic_id = obj['topic_id']
            if last_timestamp is None or current_topic_id is None or obj['value'] is None:
                _log.debug('ITS NONE for: id: {} ts {} tid: {} data: {}'.format(
                    obj['_id'], obj['ts'], current_topic_id, obj['value']))
                continue
            last_timestamp = obj['ts']

            topic_obj = topics[obj['topic_id']]

            if topic_obj['table'] in ('analysis', 'datalogger', 'device'):
                    if not isinstance(obj['value'], int) and not isinstance(obj['value'], float):
                        logger_queue.put("table: {} topic: {} ts: {} data: {}".format(
                            topic_obj['table'], obj['topic_id'], obj['ts'], obj['value']))
                        continue

            if table_queues[topic_obj['table']].qsize() > MAX_QUEUE_SIZE:
                while table_queues[topic_obj['table']].qsize() > int(MAX_QUEUE_SIZE / 2):
                    sleep(1)
                # _log.debug("qsize > 10000 its: {}".format(
                #     table_queues[topic_obj['table']].qsize()))
                # sleep(5)

            table_queues[topic_obj['table']].put((topic_obj['hash'],
                                                 utils.format_timestamp(obj['ts']),
                                                 obj['value']))

            while not metric_queue.empty():
                elements, duration = metric_queue.get(block=True)
                written_records += elements
                total_time += duration

            added_to_queue += 1
            if written_records % MAX_QUEUE_SIZE == 0 and total_time > 0 and \
                    written_records != last_shown:
                _log.debug("Added to queue data {} {} records: {} total: {}ms avg: {}ms avg(s): {}".format(
                    topic_obj['table'],
                    added_to_queue,
                    written_records,
                    total_time,
                    written_records / total_time,
                    written_records / total_time / 1000
                ))
                last_shown = written_records

    except Exception as ex:
        logger_queue.put(ex.message)
        with open('last_data.txt', 'w') as lf:
            lf.write("{},{}".format(current_topic_id, last_timestamp))

build_topics_mapping()
insert_topics_to_crate()
start_transfer_data()

for q in table_queues.values():
    q.put("done")
_log.debug('Total data insertion time: {}ms'.format(total_time))

for t in insert_threads:
    t.join(timeout=5)
    sleep(0.1)
# while len(insert_threads) > 0:
#     try:
#         for i in range(len(insert_threads) - 1, -1, -1):
#             if not insert_threads[i].is_alive():
#                 #_log.debug('Removing thread: {}'.format(i))
#                 insert_threads.remove(i)
#
#     except ValueError:
#         pass
#     finally:
#         sleep(0.1)
#     #_log.debug('Insert threads now: {}'.format(insert_threads))


'''
Given an array, Arr of N numbers, and another number target, find three integers in the array
such that the sum is closest to the target. Return the sum of the three integers.

Note: If there are multiple solutions, print the maximum one.

Example 1:

Input:
N = 6, target = 2
A[] = {-7,9,8,3,1,1}
Output: 2
Explanation: There is one triplet with sum
2 in the array. Triplet elements are -7,8,
1 whose sum is 2.

Example 2:

Input:
N = 4, target = 13
A[] = {5,2,7,5}
Output: 14
Explanation: There is one triplet with sum
12 and other with sum 14 in the array.
Triplet elements are 5, 2, 5 and 2, 7, 5
respectively. Since abs(13-12) ==
abs(13-14) maximum triplet sum will be
preferred i.e 14.

Your Task:
Complete threeSumClosest() function and return the expected answer.

Expected Time Complexity: O(N*N).
Expected Auxiliary Space: O(1).
'''

class Solution:
    def threeSumClosest(self, arr, target):
        # Your Code Here

        arr.sort()
        n = len(arr)

        ans = []
        temp = []

        for i in range(n-2):
            a = i+1
            b = n-1

            while a < b:
                x = arr[i]+arr[a]+arr[b]
                if x == target:
                    return target
                else:
                    ans.append(x)
                    temp.append(abs(x-target))
                    if x > target:
                        b -= 1
                    else:
                        a += 1

        t = min(temp)+target
        t2 = target-min(temp)

        if t in ans:
            return t
        else:
            return t2


import socket
import os
import struct
from ctypes import *
import chardet
import re

#ip header
#----------------------32bit------------------------------------
#		8		|		8		|		8		|		8		|
#---------------------------------------------------------------
#版本4|包头长4	|	服务类型8	|			总长16				|
#---------------------------------------------------------------
#			标识16				|标志3|			偏移			|
#---------------------------------------------------------------
#		TTL		|		协议	|			checksum			|
#---------------------------------------------------------------
#						source_addr								|
#---------------------------------------------------------------
#						des_addr								|
#---------------------------------------------------------------
#					Option	and padding							|
#---------------------------------------------------------------

# host to listen on
host = "169.254.215.140"

#inherent Structure or Union
class IP(Structure):
    _fields_ = [
            ("ihl", c_ubyte, 4),
            ("version", c_ubyte, 4),
            ("tos", c_ubyte),
            ("len", c_ushort),
            ("id", c_ushort),
            ("offset", c_ushort),
            ("ttl", c_ubyte),
            ("protocol_num", c_ubyte),
            ("sum", c_ushort),
            ("src", c_ulong),
            ("dst", c_ulong)
        ]
    #用来创建实例
    def __new__(self, socket_buffer=None):
		#返回一个ctypes实例,此处是IP类的实例(继承)
        return self.from_buffer_copy(socket_buffer)
    #初始化实例
    def __init__(self, socket_buffer=None):
        # map protocol constants to their names
        self.protocol_map = {1:"ICMP", 6:"TCP", 17:"UDP"}
        # human readable IP addresses,<L unsign long little-endian
        self.src_address = socket.inet_ntoa(struct.pack("<L",self.src))
        self.dst_address = socket.inet_ntoa(struct.pack("<L",self.dst))
        # human readable protocol
        try:
            self.protocol = self.protocol_map[self.protocol_num]
        except:
            self.protocol = str(self.protocol_num)
        #2F00 -> 002F,总长
        self.length = socket.ntohs(self.len)
        #ip头长
        self.header_length = self.ihl*4;
        #identifier标志
        self.label = socket.ntohs(self.id)
        #offset
        self.off_set = socket.ntohs(self.offset)&0b1111111111111
        #flag标识 3bit 第二位表示是否分片，第三位表示若分片后面还有没有分片
        self.flag = socket.ntohs(self.offset)>>13

# create a raw socket and bind it to the public interface
if os.name == "nt":
    socket_protocol = socket.IPPROTO_IP
else:
    socket_protocol = socket.IPPROTO_ICMP

sniffer = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket_protocol)
sniffer.bind((host, 60001))
# we want the IP headers included in the capture.
# On the other hand,a TCP/IP service provider that supports SOCK_RAW should also support IP_HDRINCL.
sniffer.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)

#recv all
if os.name == "nt":
    sniffer.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)
try:
    while True:
        # read in a single packet,[data,src]
        raw_buffer = sniffer.recvfrom(65535)
        #数据
        raw_buffer = raw_buffer[0]
        #IP header
        ip_split = IP(raw_buffer[0:20])
        print("Protocol: %s %s -> %s" % (ip_split.protocol, ip_split.src_address, ip_split.dst_address))
        print("包头:%d" % ip_split.header_length)
        print("数据长度:%d字节" % (ip_split.length-40))
        print("偏移:%d字节" % (ip_split.off_set))
        print("标识:%s" % ip_split.label)
        print("标志位:%s" % bin(ip_split.flag))

		#extra data
        if(ip_split.length != 40):
            enc = chardet.detect(raw_buffer[40:])['encoding']
            if(enc):
                try:
                    print("数据:%s" % raw_buffer[40:].decode(enc))
                except:
                    print("Decode Error!!!")
            else:
                print("Unknow Encoding!")
        else:
            print("No Data!!!")
        print('-------------------------------')
except KeyboardInterrupt:
	if os.name == "nt":
		sniffer.ioctl(socket.SIO_RCVALL, socket.RCVALL_OFF)


# Napište funkci, která vytvoří číslo zřetězením ‹count› po sobě
# jdoucích kladných čísel počínaje zadaným číslem ‹start›.  Tato
# čísla zřetězte vyjádřená v binární soustavě. Například volání
# ‹joined(1, 3)› zřetězí sekvenci ⟦1₂ = 1⟧, ⟦(10)₂ = 2⟧ a ⟦(11)₂ =
# 3⟧ vrátí číslo ⟦(11011)₂ = 27⟧. V Pythonu lze binární čísla přímo
# zapisovat v tomto tvaru: ‹0b11011› (podobně lze stejné číslo
# zapsat v šestnáctkové soustavě zápisem ‹0x1b› nebo osmičkové jako
# ‹0o33›).
def length(number):
    count = 0
    while number > 0 :
        number //= 2
        count += 1
    return count

def joined(start, count):
    bank = 0
    for i in range (start, start + count):
        bank = bank * 2 ** length(i) + i
    return bank

def main():
    assert joined(1, 3) == 0b11011
    assert joined(10, 4) == 0b1010101111001101
    assert joined(8, 5) == 0b10001001101010111100
    assert joined(99, 2) == 0b11000111100100
    assert joined(999, 3) == 0b111110011111111010001111101001
    assert joined(1111, 1) == 0b10001010111

if __name__ == "__main__":
    main()


'''
Given two sentences words1, words2 (each represented as an array of strings), and a list of similar word pairs pairs, determine if two sentences are similar.

For example, "great acting skills" and "fine drama talent" are similar, if the similar word pairs are pairs = [["great", "fine"], ["acting","drama"], ["skills","talent"]].

Note that the similarity relation is not transitive. For example, if "great" and "fine" are similar, and "fine" and "good" are similar, "great" and "good" are not necessarily similar.

Also, a word is always similar with itself. For example, the sentences words1 = ["great"], words2 = ["great"], pairs = [] are similar, even though there are no specified similar word pairs.

Note:

The length of words1 and words2 will not exceed 1000.
The length of pairs will not exceed 2000.
The length of each pairs[i] will be 2.
The length of each words[i] and pairs[i][j] will be in the range [1, 20].
'''
class Solution(object):
    def areSentencesSimilar(self, words1, words2, pairs):
        """
        :type words1: List[str]
        :type words2: List[str]
        :type pairs: List[List[str]]
        :rtype: bool
        """
        if len(words1) != len(words2):
            return False
        d = dict()
        for p in pairs:
            if p[0] not in d:
                d[p[0]] = set()
            d[p[0]].add(p[1])
            if p[1] not in d:
                d[p[1]] = set()
            d[p[1]].add(p[0])

        for i in xrange(len(words1)):
            w1, w2 = words1[i], words2[i]
            if w1 != w2 and (w1 not in d or w2 not in d[w1]):
                return False
        return True

s = Solution()
pairs = [["great", "fine"], ["acting","drama"], ["skills","talent"]]
assert s.areSentencesSimilar(['great', 'acting', 'skills'], ['fine', 'drama', 'talent'], []) == False
assert s.areSentencesSimilar(['great', 'acting', 'skills'], ['fine', 'drama', 'talent'], pairs)
assert s.areSentencesSimilar(['great', 'acting', 'skills'], ['great', 'acting', 'skills'], [])


import tkinter as tk
import random

root = tk.Tk()
root.title("Demo")

def reset():
	global l
	l = []
	canvas.create_rectangle(1,1,800,200, fill = 'grey70')

def keypressed(event):
	key = event.keysym.upper()
	l.append(key)

	canvas.create_rectangle(1,1,800,200, fill = 'grey70')
	canvas.create_text(400, 100, text=l)
	print (l)

def showButton():
	btn = tk.Button(root, text='CHANGE WORD', command=reset)
	btn.pack()

l = []
canvas = tk.Canvas(root, width = 800, height = 200, bg= 'grey70')
canvas.pack()

showButton()

root.bind("<Key>",keypressed)

root.mainloop()


#codigo configurado na maquina de weslley
import socket
from typing import ByteString
import threading
import time

#serverAddressPort = ('201.59.169.4', 50005) #colocar ip do servidor e porta, configurados no roteador-servidor (julio)
#serverAddressPort = ('45.166.51.58', 1317) #colocar ip do servidor e porta, configurados no roteador-servidor (pedro)
#serverAddressPort = ('localhost', 50000) #colocar ip do servidor e porta, configurados no roteador-servidor (local)
serverAddressPort = ('45.71.168.217', 55837)

bufferSize = 1024
UDPClientSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)

def envia():
    global inputClient
    inputClient = ''
    global countmsg
    countmsg = 0
    inputClient=""
    while (inputClient != 'quit'):
        while True:
            if(countmsg<30):
                inputClient = input()
                msgFromClient = bytes(str(countmsg+1) +' '+inputClient, 'utf-8')
                UDPClientSocket.sendto(msgFromClient, serverAddressPort)
                countmsg = countmsg+1
                #---verificação de intervalo---
                intervalomsg = countmsg % 10
                #------------------------------
                if(intervalomsg == 0 and countmsg!=0):
                    print("intervalo de 10 segundos")
                    time.sleep(10)#mudar para 10
                    if(countmsg==30):
                        break

def recebe():
    UDPClientSocket.bind(('192.168.1.110', 34942)) #porta para receber mensagem do servidor
    #UDPClientSocket.bind(('localhost', 50001)) #porta para receber mensagem do servidor (local)
    countAck=0
    while True:
        if(countmsg<30):
            msgServidor = UDPClientSocket.recvfrom(1024)
            print("Messagem do servidor: {}".format(msgServidor[0].decode('utf-8')))
            msgServidor = msgServidor[0].decode('utf-8')
            msgAck = msgServidor.find('ack')

            if(msgAck != -1): #-1 ack nao encontrado
                countAck = countAck + 1
            if(countmsg == 30):
                taxaPerda = (countmsg-countAck)/countmsg
                print('taxa de perda: ', taxaPerda)
                UDPClientSocket.close()

enviar = threading.Thread(target=envia)
receber = threading.Thread(target=recebe)

receber.start()
enviar.start()


from typing import List
import copy

def getDivisors(nr)->List[int]:
    finalLlist = []
    n=int(nr/2)
    while n>0:
        if nr%n==0:
            finalLlist.append(n)
        n-=1
    return finalLlist

def maxProd(arr, n):
    divizors = getDivisors(n)
    maxProd=1
    n1=0
    n2=0
    arr.sort(reverse=True)
    for i,x in enumerate(arr):
        auxProd=1
        n1Loc=0
        if x%n==0:
            if x==arr[0]:
                n1=x
                n2=arr[1]
                print("Setting loc 2:", n1,n2)
            else:
                n1=x
                n2=arr[0]
                print("Setting loc 1:", n1,n2)
            break
        else:
            okD=False
            for d in divizors:
                if x%d==0:
                    okD=True
                    break
            if okD==False:
                continue
            if n1Loc==0:
                n1Loc=x
            for x2 in copy.deepcopy(arr[i+1:]):
                if (n1Loc*x2) % n==0:
                    auxProd=n1Loc*x2
                    if auxProd>maxProd:
                        maxProd=auxProd
                        n1=x
                        n2=x2
                    break

    print("Result is:", n1,n2, n1*n2)

n=15
arr=[7,8,3,6,12,5,10, 99]
print(getDivisors(n))
maxProd(arr,n)


import numpy as np

def radix4_fft(x):

	N = len(x)

	if N == 1:
		return x

	# Split the input into four parts
	x0 = x[ ::4]
	x1 = x[1::4]
	x2 = x[2::4]
	x3 = x[3::4]

	# Recursively compute the FFT of each part
	X0 = radix4_fft(x0)
	X1 = radix4_fft(x1)
	X2 = radix4_fft(x2)
	X3 = radix4_fft(x3)

	# Combine the results using twiddle factors
	X = np.zeros(N, dtype=np.complex128)
	for k in range(N//4):
		twiddle1 = np.exp(-2j * np.pi *   k / N)
		twiddle2 = np.exp(-2j * np.pi * 2*k / N)
		twiddle3 = np.exp(-2j * np.pi * 3*k / N)
		t1 = X1[k] * twiddle1
		t2 = X2[k] * twiddle2
		t3 = X3[k] * twiddle3
		X[k         ] = X0[k] +    t1 + t2 +    t3
		X[k +   N//4] = X0[k] - 1j*t1 - t2 + 1j*t3
		X[k +   N//2] = X0[k] -    t1 + t2 -    t3
		X[k + 3*N//4] = X0[k] + 1j*t1 - t2 - 1j*t3

	return X

if __name__ =='__main__':

	def error(sig,ref):
		return abs(sig-ref).sum()

	for i in range(1_000):
		N   = np.random.randint(3,5)
		sig = np.random.rand(4**N).astype(np.complex128)

		SIG = radix4_fft(sig)
		REF = np.fft.fft(sig)

		assert error(SIG,REF) < 1e-12
	pass


#
# @lc app=leetcode.cn id=144 lang=python3
#
# [144] 二叉树的前序遍历
#

# @lc code=start
# Definition for a binary tree node.

'''
class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right
'''
class Solution:
    def preorderTraversal(self, root: Optional[TreeNode]):
        '''
        中左右，每个结点操作一样：输出结点，保存右节点，
        便于接下来遍历，将当前结点变为左节点。直到当前
        结点没有子节点，数组中最后一个保存的是其兄弟右
        结点，将兄弟右结点当作当前结点，
        '''

        '''
        res = []
        tmp = []
        while root:
            if root.right:
                tmp.append(root.right)
            res.append(root.val)
            if root.left:
                root = root.left
            elif tmp != []:
                root = tmp.pop()
            else:
                root = None
        return res
        '''
        res = []
        if not root:
            return res
        res.append(root.val)
        if root.left:
            res = res + self.preorderTraversal(root.left)
        if root.right:
            res = res + self.preorderTraversal(root.right)
        return res
    # @lc code=end

144.二叉树的前序遍历.py

144.二叉树的前序遍历.py


from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint

# # Model description and training
model = prepare_model(training=True)

try:
    model.load_weights('LSTM+BN4--26--0.011.hdf5')
    print("...Previous weight data...")
except:
    print("...New weight data...")
    pass

train_file_path = 'train'
train_set = TextImageGenerator(train_file_path, img_w, img_h, batch_size, max_text_len, max_train_data_size, train_chunk_size)
train_set.build_data()

valid_file_path = 'test'
test_set = TextImageGenerator(valid_file_path, img_w, img_h, val_batch_size, max_text_len, max_test_data_size, test_chunk_size)
test_set.build_data()

# early_stop = EarlyStopping(monitor='loss', min_delta=0.001, patience=4, mode='min', verbose=0)
checkpoint = ModelCheckpoint(filepath='LSTM+BN5--{epoch:02d}--{val_loss:.3f}.hdf5', monitor='loss', verbose=0, mode='min', period=1)

model.compile(loss={'ctc': lambda y_true, y_pred: y_pred}, optimizer="Adadelta")

model.summary()

# captures output of softmax so we can decode the output during visualization
model.fit(  x=train_set.next_batch(),
            steps_per_epoch=int(train_set.data_points / batch_size),
            validation_data=test_set.next_batch(),
            epochs=30,
            callbacks=[checkpoint],
            validation_steps=int(test_set.data_points / val_batch_size)
        )


import os
import django

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'maintenance.settings')
django.setup()

from cars.models import BodyType, Brand, CarModel

body_types = [
    {'id': 1, 'name': 'Sedan'},
    {'id': 2, 'name': 'Hatchback'},
    {'id': 3, 'name': 'Coupe'},
    {'id': 4, 'name': 'Cabriolet'},
    {'id': 5, 'name': 'SUV (Sport Utility Vehicle)'},
    {'id': 6, 'name': 'Monovolum (MPV - Multi-Purpose Vehicle)'},
    {'id': 7, 'name': 'Crossover'},
    {'id': 8, 'name': 'Break (Station Wagon)'},
    {'id': 9, 'name': 'Pick-up'},
    {'id': 10, 'name': 'Roadster'},
    {'id': 11, 'name': 'Supersport'},
    {'id': 12, 'name': 'Monovolum'},
]

for body_type in body_types:
    BodyType.objects.get_or_create(id=body_type['id'], name=body_type['name'])

brands = [
    {'id': 1, 'name': 'Alfa Romeo', 'body_type_ids': [1, 2, 3, 4, 5, 12]},
    {'id': 2, 'name': 'Aston Martin', 'body_type_ids': [1, 3, 4]},
    {'id': 3, 'name': 'Audi', 'body_type_ids': [1, 2, 3, 4, 5, 7, 8]},
    {'id': 4, 'name': 'Bentley', 'body_type_ids': [1, 3, 4, 5]},
    {'id': 5, 'name': 'BMW', 'body_type_ids': [1, 2, 3, 4, 5, 10]},
    {'id': 6, 'name': 'Bugatti', 'body_type_ids': [11]},
    {'id': 7, 'name': 'Citroën', 'body_type_ids': [1, 2, 3, 4, 5, 12]},
    {'id': 8, 'name': 'Dacia', 'body_type_ids': [1, 2, 8]},
    {'id': 9, 'name': 'Ferrari', 'body_type_ids': [11, 3, 4]},
    {'id': 10, 'name': 'Fiat', 'body_type_ids': [1, 2, 3, 4]},
    {'id': 11, 'name': 'Jaguar', 'body_type_ids': [1, 3, 4, 5]},
    {'id': 12, 'name': 'Lamborghini', 'body_type_ids': [11, 3, 10, 5]},
    {'id': 13, 'name': 'Land Rover', 'body_type_ids': [5, 7]},
    {'id': 14, 'name': 'Maserati', 'body_type_ids': [1, 3, 4, 5]},
    {'id': 15, 'name': 'Mercedes-Benz', 'body_type_ids': [1, 2, 3, 4, 5, 8]},
    {'id': 16, 'name': 'MINI', 'body_type_ids': [2, 3, 4, 5]},
    {'id': 17, 'name': 'Opel', 'body_type_ids': [1, 2, 3, 4, 5, 6, 8]},
    {'id': 18, 'name': 'Peugeot', 'body_type_ids': [1, 2, 3, 4, 5, 8, 12]},
    {'id': 19, 'name': 'Porsche', 'body_type_ids': [3, 4, 5]},
    {'id': 20, 'name': 'Renault', 'body_type_ids': [1, 2, 3, 4, 5, 8, 9]},
    {'id': 21, 'name': 'Rolls-Royce', 'body_type_ids': [1, 3, 4, 5]},
    {'id': 22, 'name': 'SEAT', 'body_type_ids': [1, 2, 5]},
    {'id': 23, 'name': 'Škoda', 'body_type_ids': [1, 2, 5, 8]},
    {'id': 24, 'name': 'Tesla', 'body_type_ids': [1, 3, 5]},
    {'id': 25, 'name': 'Volkswagen', 'body_type_ids': [1, 2, 3, 4, 5, 6, 8]},
    {'id': 26, 'name': 'Volvo', 'body_type_ids': [1, 2, 5, 8]},
]

for brand in brands:
    body_types = BodyType.objects.filter(pk__in=brand['body_type_ids'])
    try:
        new_brand = Brand.objects.get(id=brand['id'])
    except Brand.DoesNotExist:
        new_brand = Brand.objects.create(id=brand['id'], name=brand['name'])
    new_brand.body_type.set(body_types)

car_models = [
    {'id': 1, 'name': 'Alfa Romeo Giulia', 'brand_ids': [1], 'body_type_ids': [12]},
    {'id': 2, 'name': 'Alfa Romeo Giulietta', 'brand_ids': [1], 'body_type_ids': [2]},
    {'id': 3, 'name': 'Alfa Romeo 4C', 'brand_ids': [1], 'body_type_ids': [11]},
    {'id': 4, 'name': 'Alfa Romeo Spider', 'brand_ids': [1], 'body_type_ids': [12]},
    {'id': 5, 'name': 'Alfa Romeo Stelvio', 'brand_ids': [1], 'body_type_ids': [5]},
    {'id': 6, 'name': 'Aston Martin Rapide', 'brand_ids': [2]},
    {'id': 7, 'name': 'Aston Martin DB11', 'brand_ids': [2]},
    {'id': 8, 'name': 'Aston Martin Vantage', 'brand_ids': [2]},
    {'id': 9, 'name': 'Audi A4', 'brand_ids': [3]},
    {'id': 10, 'name': 'Audi A3', 'brand_ids': [3]},
    {'id': 11, 'name': 'Audi TT', 'brand_ids': [3]},
    {'id': 12, 'name': 'Audi A5', 'brand_ids': [3]},
    {'id': 13, 'name': 'Audi Q5', 'brand_ids': [3]},
    {'id': 14, 'name': 'Audi Q7', 'brand_ids': [3]},
    {'id': 15, 'name': 'Audi A6', 'brand_ids': [3]},
    {'id': 16, 'name': 'Bentley Flying Spur', 'brand_ids': [4]},
    {'id': 17, 'name': 'Bentley Continental GT', 'brand_ids': [4]},
    {'id': 18, 'name': 'Bentley Continental GTC', 'brand_ids': [4]},
    {'id': 19, 'name': 'Bentley Bentayga', 'brand_ids': [4]},
    {'id': 20, 'name': 'BMW 3 Series', 'brand_ids': [5]},
    {'id': 21, 'name': 'BMW 1 Series', 'brand_ids': [5]},
    {'id': 22, 'name': 'BMW 4 Series', 'brand_ids': [5]},
    {'id': 23, 'name': 'BMW 2 Series Convertible', 'brand_ids': [5]},
    {'id': 24, 'name': 'BMW X5', 'brand_ids': [5]},
    {'id': 25, 'name': 'BMW X6', 'brand_ids': [5]},
    {'id': 26, 'name': 'BMW Z4', 'brand_ids': [5]},
    {'id': 27, 'name': 'Bugatti Chiron', 'brand_ids': [6]},
    {'id': 28, 'name': 'Citroën C4', 'brand_ids': [7]},
    {'id': 29, 'name': 'Citroën C3', 'brand_ids': [7]},
    {'id': 30, 'name': 'Citroën DS3', 'brand_ids': [7]},
    {'id': 31, 'name': 'Citroën DS4', 'brand_ids': [7]},
    {'id': 32, 'name': 'Citroën C5 Aircross', 'brand_ids': [7]},
    {'id': 33, 'name': 'Citroën C3 Aircross', 'brand_ids': [7]},
    {'id': 34, 'name': 'Citroën Berlingo', 'brand_ids': [7]},
    {'id': 35, 'name': 'Dacia Logan', 'brand_ids': [8]},
    {'id': 36, 'name': 'Dacia Sandero', 'brand_ids': [8]},
    {'id': 37, 'name': 'Dacia Duster', 'brand_ids': [8]},
    {'id': 38, 'name': 'Ferrari F8 Tributo', 'brand_ids': [9]},
    {'id': 39, 'name': 'Ferrari 812 Superfast', 'brand_ids': [9]},
    {'id': 40, 'name': 'Ferrari Portofino M', 'brand_ids': [9]},
    {'id': 41, 'name': 'Fiat Tipo', 'brand_ids': [10]},
    {'id': 42, 'name': 'Fiat 500', 'brand_ids': [10]},
    {'id': 43, 'name': 'Fiat 124 Spider', 'brand_ids': [10]},
    {'id': 44, 'name': 'Jaguar XE', 'brand_ids': [11]},
    {'id': 45, 'name': 'Jaguar F-Type', 'brand_ids': [11]},
    {'id': 46, 'name': 'Jaguar E-Pace', 'brand_ids': [11]},
    {'id': 47, 'name': 'Jaguar F-Pace', 'brand_ids': [11]},
    {'id': 48, 'name': 'Lamborghini Huracán', 'brand_ids': [12]},
    {'id': 49, 'name': 'Lamborghini Aventador', 'brand_ids': [12]},
    {'id': 50, 'name': 'Lamborghini Urus', 'brand_ids': [12]},
    {'id': 51, 'name': 'Land Rover Range Rover', 'brand_ids': [13]},
    {'id': 52, 'name': 'Land Rover Range Rover Sport', 'brand_ids': [13]},
    {'id': 53, 'name': 'Land Rover Discovery', 'brand_ids': [13]},
    {'id': 54, 'name': 'Land Rover Defender', 'brand_ids': [13]},
    {'id': 55, 'name': 'Maserati Ghibli', 'brand_ids': [14]},
    {'id': 56, 'name': 'Maserati GranTurismo', 'brand_ids': [14]},
    {'id': 57, 'name': 'Maserati Levante', 'brand_ids': [14]},
    {'id': 58, 'name': 'Maserati Quattroporte', 'brand_ids': [14]},
    {'id': 59, 'name': 'Mercedes-Benz C-Class', 'brand_ids': [15]},
    {'id': 60, 'name': 'Mercedes-Benz A-Class', 'brand_ids': [15]},
    {'id': 61, 'name': 'Mercedes-Benz E-Class', 'brand_ids': [15]},
    {'id': 62, 'name': 'Mercedes-Benz S-Class', 'brand_ids': [15]},
    {'id': 63, 'name': 'Mercedes-Benz GLC', 'brand_ids': [15]},
    {'id': 64, 'name': 'Mercedes-Benz GLE', 'brand_ids': [15]},
    {'id': 65, 'name': 'Mercedes-Benz CLA', 'brand_ids': [15]},
    {'id': 66, 'name': 'MINI Cooper', 'brand_ids': [16]},
    {'id': 67, 'name': 'MINI Clubman', 'brand_ids': [16]},
    {'id': 68, 'name': 'MINI Convertible', 'brand_ids': [16]},
    {'id': 69, 'name': 'MINI Countryman', 'brand_ids': [16]},
    {'id': 70, 'name': 'Opel Insignia', 'brand_ids': [17]},
    {'id': 71, 'name': 'Opel Corsa', 'brand_ids': [17]},
    {'id': 72, 'name': 'Opel Astra', 'brand_ids': [17]},
    {'id': 73, 'name': 'Opel Cascada', 'brand_ids': [17]},
    {'id': 74, 'name': 'Opel Mokka', 'brand_ids': [17]},
    {'id': 75, 'name': 'Opel Grandland', 'brand_ids': [17]},
    {'id': 76, 'name': 'Peugeot 308', 'brand_ids': [18]},
    {'id': 77, 'name': 'Peugeot 208', 'brand_ids': [18]},
    {'id': 78, 'name': 'Peugeot 508', 'brand_ids': [18]},
    {'id': 79, 'name': 'Peugeot 2008', 'brand_ids': [18]},
    {'id': 80, 'name': 'Peugeot 3008', 'brand_ids': [18]},
    {'id': 81, 'name': 'Peugeot 5008', 'brand_ids': [18]},
    {'id': 82, 'name': 'Peugeot Rifter', 'brand_ids': [18]},
    {'id': 83, 'name': 'Porsche 911', 'brand_ids': [19]},
    {'id': 84, 'name': 'Porsche Cayman', 'brand_ids': [19]},
    {'id': 85, 'name': 'Porsche Macan', 'brand_ids': [19]},
    {'id': 86, 'name': 'Porsche Cayenne', 'brand_ids': [19]},
    {'id': 87, 'name': 'Renault Megane', 'brand_ids': [20]},
    {'id': 88, 'name': 'Renault Clio', 'brand_ids': [20]},
    {'id': 89, 'name': 'Renault Captur', 'brand_ids': [20]},
    {'id': 90, 'name': 'Renault Kadjar', 'brand_ids': [20]},
    {'id': 91, 'name': 'Renault Koleos', 'brand_ids': [20]},
    {'id': 92, 'name': 'Renault Kangoo', 'brand_ids': [20]},
    {'id': 93, 'name': 'Renault Trafic', 'brand_ids': [20]},
    {'id': 94, 'name': 'Rolls-Royce Ghost', 'brand_ids': [21]},
    {'id': 95, 'name': 'Rolls-Royce Wraith', 'brand_ids': [21]},
    {'id': 96, 'name': 'Rolls-Royce Dawn', 'brand_ids': [21]},
    {'id': 97, 'name': 'Rolls-Royce Cullinan', 'brand_ids': [21]},
    {'id': 98, 'name': 'SEAT Leon', 'brand_ids': [22]},
    {'id': 99, 'name': 'SEAT Ibiza', 'brand_ids': [22]},
    {'id': 100, 'name': 'SEAT Arona', 'brand_ids': [22]},
    {'id': 101, 'name': 'SEAT Ateca', 'brand_ids': [22]},
    {'id': 102, 'name': 'Škoda Octavia', 'brand_ids': [23]},
    {'id': 103, 'name': 'Škoda Fabia', 'brand_ids': [23]},
    {'id': 104, 'name': 'Škoda Superb', 'brand_ids': [23]},
    {'id': 105, 'name': 'Škoda Karoq', 'brand_ids': [23]},
    {'id': 106, 'name': 'Tesla Model 3', 'brand_ids': [24]},
    {'id': 107, 'name': 'Tesla Model S', 'brand_ids': [24]},
    {'id': 108, 'name': 'Tesla Model X', 'brand_ids': [24]},
    {'id': 109, 'name': 'Volkswagen Passat', 'brand_ids': [25]},
    {'id': 110, 'name': 'Volkswagen Golf', 'brand_ids': [25]},
    {'id': 111, 'name': 'Volkswagen Polo', 'brand_ids': [25]},
    {'id': 112, 'name': 'Volkswagen Tiguan', 'brand_ids': [25]},
    {'id': 113, 'name': 'Volkswagen Touareg', 'brand_ids': [25]},
    {'id': 114, 'name': 'Volkswagen Touran', 'brand_ids': [25]},
    {'id': 115, 'name': 'Volkswagen Caddy', 'brand_ids': [25]},
    {'id': 116, 'name': 'Volvo S60', 'brand_ids': [26]},
    {'id': 117, 'name': 'Volvo V40', 'brand_ids': [26]},
    {'id': 118, 'name': 'Volvo XC40', 'brand_ids': [26]},
    {'id': 119, 'name': 'Volvo XC60', 'brand_ids': [26]},
    {'id': 120, 'name': 'Volvo XC90', 'brand_ids': [26]},
]

for car_model in car_models:
    try:
        new_car_model = CarModel.objects.get(id=car_model['id'])
    except CarModel.DoesNotExist:
        brand = Brand.objects.get(id=car_model['brand_ids'][0])
        new_car_model = CarModel.objects.create(id=car_model['id'], name=car_model['name'], brand=brand)

# cars = [
#     {'model_id': 1, 'year': 2022, 'fuel_type': 'Gasoline', 'engine': '2.0', 'fuel_consumption': 7.5, 'cost_annual': 500, 'major_repair': 1200},
#     {'model_id': 2, 'year': 2021, 'fuel_type': 'Diesel', 'engine': '1.5', 'fuel_consumption': 6.5, 'cost_annual': 300, 'major_repair': 1900},
#     {'model_id': 3, 'year': 2019, 'fuel_type': 'Gasoline', 'engine': '3.0', 'fuel_consumption': 9.5, 'cost_annual': 900, 'major_repair': 13500},

# ]
#
# for car in cars:
#     car_model = CarModel.objects.get(pk=car['model_id'])
#     Car.objects.create(
#         model=car_model,
#         year=car['year'],
#         fuel_type=car['fuel_type'],
#         engine=car['engine'],
#         fuel_consumption=car['fuel_consumption'],
#         cost_annual=car['cost_annual'],
#         major_repair=car['major_repair']
#     )


import os
import sys
import re

class Simulation:
    def __init__(self, simnNo, simDate, chipName, chipCount, chipCost):
        self.simulationNumber = simnNo
        self.simulationDate = simDate
        self.chipName = chipName
        self.chipCount = chipCount
        self.chipCost = chipCost
        self.simulationCost = round(self.chipCost * self.chipCount, 2)

    def __str__(self):
        return "{0}: {1:03d}, {2}, ${3:06.2f}".format(self.chipName, self.simulationNumber, self.simulationDate, self.simulationCost)

class Employee:
    def __init__(self, employeeName, employeeID):
        self.simulationsDict = {}
        self.employeeName = employeeName
        self.employeeID = employeeID

    def addSimulation(self,sim):
        self.simulationsDict[sim.simulationNumber] = sim

    def getSimulation(self,simNo):
        if simNo not in self.simulationsDict.keys():
            return None
        else:
            return self.simulationsDict[simNo]

    def __str__(self):
        ret_string = "{0}, {1}: {2:02d} Simulations".format(self.employeeID, self.employeeName, len(self.simulationsDict))
        return ret_string

    def getWorkload(self):
        string1 = str(self)
        string1 += "\n"

        list_strings = []
        for key in self.simulationsDict.keys():
            temp_string = str(self.simulationsDict[key])
            list_strings.append(temp_string)

        list_strings.sort()

        for item in list_strings:
            string1 += item
            string1 += "\n"

        string1 = string1[0:-1]

        return string1

if __name__ == "__main__":
    pass


import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider

class Plotter3D:
    #use the scroll wheel to change the third dimension of an image
    def __init__(self, ax, image):
        self.ax = ax
        ax.set_title('Scroll through MRI slices')

        self.image = image
        rows, cols, self.slices = image.shape
        self.ind = self.slices//2

        self.im = ax.imshow(self.image[:, :, self.ind])
        self.update()

    def on_scroll(self, event):
        print("%s %s" % (event.button, event.step))
        if event.button == 'up':
            self.ind = min((self.ind + 1), self.slices-1)
        else:
            self.ind = max((self.ind - 1) , 0)
        self.update()

    def update(self):
        self.im.set_data(self.image[:, :, self.ind])
        self.ax.set_ylabel('slice %s' % self.ind)
        self.im.axes.figure.canvas.draw()

# fig, ax = plt.subplots(1, 1)

# X = np.random.rand(20, 20, 40)

# tracker = Plotter3D(ax, X)

# fig.canvas.mpl_connect('scroll_event', tracker.on_scroll)
# plt.show()
def SliderPlot(image, specialChannelNames):
    global fig, ax, sliderT, sliderO, array, img, specialChannels, num_bVals, previous_j
    num_bVals = image.shape[3]
    specialChannels = specialChannelNames
    array = image
    #this function takes a 4d array with the 3rd index being the slice and the 4th index being the b value
    fig, ax = plt.subplots()
    img = ax.imshow(array[:,:,0,0], origin = 'upper')

    axT = fig.add_axes([0.2, 0.95, 0.65, 0.03])
    axO = fig.add_axes([0.2, 0.90, 0.65, 0.03])

    sliderT = Slider(axT, 'Slice', 0, array.shape[2]-1, valinit=0, valfmt='%i')
    sliderO = Slider(axO, 'b Val', 0, array.shape[3]-1, valinit=0, valfmt='%i')
    previous_j = 0

    sliderT.on_changed(update)
    sliderO.on_changed(update)
    plt.show()

def update(val):
    i = int(sliderT.val)
    j = int(sliderO.val)
    im = array[:,:,i,j]

    img = img = ax.imshow(array[:,:,i,j], origin = 'upper')

    #Only want to reload the colour bar for special images like D, D*, f. So we must clear the axis and make a new image for these types of images. Then when we go back to regular signal images need to recreate
    #specialImageRange = range(num_bVals - 3, num_bVals - 1)

    fig.canvas.draw_idle()


import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.nn.functional as F
import requests
import numpy as np
import sklearn

class GRU_with_Conv1D_Model (nn.Module):
    def __init__(self, total_tag, embeding_size, gru_hidden,gru_layer, filter_num_1, filter_size_1, filter_num_2, filter_size_2, output_size, max_word):
        super(GRU_with_Conv1D_Model, self).__init__()
        self.total_tag = total_tag
        self.max_word = max_word
        self.embeding_size = embeding_size
        self.gru_hidden = gru_hidden
        self.gru_layer = gru_layer
        self.filter_num_1 = filter_num_1
        self.filter_size_1 = filter_size_1
        self.filter_num_2 = filter_num_2
        self.filter_size_2 = filter_size_2
        self.output_size = output_size
        self.Embeding = nn.Embedding(self.total_tag, self.embeding_size)
        self.GRU = nn.GRU(self.embeding_size , self.gru_hidden, self.gru_layer, bidirectional = True)
        self.Conv1d_layer1 = nn.Sequential(nn.Conv1d(in_channels=self.gru_hidden*2,
                                                            out_channels=self.filter_num_1,
                                                            kernel_size=self.filter_size_1),
                                                         #nn.BatchNorm1d (self.filter_size_1),
                                                         nn.ReLU())

        self.Conv1d_layer2 = nn.Sequential(nn.Conv1d(in_channels=self.filter_num_1,
                                                            out_channels=self.filter_num_2,
                                                            kernel_size=self.filter_size_2),
                                                         #nn.BatchNorm1d (self.filter_size_2),
                                                         nn.ReLU())
        # batch size is  set to 1 , output_size is set to 2
        L = (self.max_word- self.filter_size_1+ 1)- self.filter_size_2+ 1
        self.linear = nn.Linear( self.filter_num_2 * L, self.output_size)

    def forward(self, input_data):
        embed_res = self.Embeding (input_data)
        if len(input_data) < self.max_word:
            for c in range (self.max_word - len(input_data)):
                embed_res = torch.cat((embed_res, torch.zeros(1, self.embeding_size)),dim = 0 )
        print (' Embedding :::: ' , embed_res.size())
        # let the input dimension be the (L , N , M) and the get the output with the dimension (L , N , H)
        gru_res, _ = self.GRU(embed_res.unsqueeze(1))
        print (' GRU :::: ' , gru_res.size())
        # let the input dimension be the (N , Cin , L) and then the output with the dimension (N, Cout , Lout)
        conv_res1 = self.Conv1d_layer1 (gru_res.permute(1,2,0))
        print (' conv_res1 :::: ' , conv_res1.size())
        conv_res2 = self.Conv1d_layer2 (conv_res1)
        print ('conv_res2 :::: ' , conv_res2.size())
        linear_res = self.linear(conv_res2.view(-1))
        final_res = F.sigmoid(linear_res)
        return final_res

if __name__ == '__main__':
    # Testing the code
    model_v3 = GRU_with_Conv1D_Model(total_tag= 16, embeding_size=8, gru_hidden= 6 , gru_layer=1, filter_num_1=5, filter_size_1=2, filter_num_2 = 5, filter_size_2 = 2,output_size = 7, max_word = 10)
    test_vec = torch.LongTensor([1,2,3,4,5,6,7])
    model_v3 (test_vec)
    print ('V3_Model_with_conv_and_gru ::: \n', model_v3 )


#!/usr/bin/env python
"""pymatsolver: Matrix Solvers for Python

pymatsolver is a python package for easy to use matrix solvers.

"""

from distutils.core import setup
from setuptools import find_packages

CLASSIFIERS = [
    'Development Status :: 4 - Beta',
    'Intended Audience :: Developers',
    'Intended Audience :: Science/Research',
    'License :: OSI Approved :: MIT License',
    'Programming Language :: Python',
    'Topic :: Scientific/Engineering',
    'Topic :: Scientific/Engineering :: Mathematics',
    'Topic :: Scientific/Engineering :: Physics',
    'Operating System :: Microsoft :: Windows',
    'Operating System :: POSIX',
    'Operating System :: Unix',
    'Operating System :: MacOS',
    'Natural Language :: English',
]

with open("README.rst") as f:
    LONG_DESCRIPTION = ''.join(f.readlines())

setup(
    name="pymatsolver",
    version="0.2.0",
    packages=find_packages(exclude=["*mumps", "tests"]),
    install_requires=[
        'numpy>=1.7',
        'scipy>=0.13',
    ],
    author="Rowan Cockett",
    author_email="rowanc1@gmail.com",
    description="pymatsolver: Matrix Solvers for Python",
    long_description=LONG_DESCRIPTION,
    license="MIT",
    keywords="matrix solver",
    url="http://simpeg.xyz/",
    download_url="http://github.com/rowanc1/pymatsolver",
    classifiers=CLASSIFIERS,
    platforms=["Windows", "Linux", "Solaris", "Mac OS-X", "Unix"],
    use_2to3=False
)


from sqlalchemy.future import select
from sqlalchemy.ext.asyncio import AsyncSession
from typing import Any, TypeVar, Type, Generic
from pydantic import BaseModel

ModelType = TypeVar("ModelType")
CreateSchemaType = TypeVar("CreateSchemaType", bound=BaseModel)
UpdateSchemaType = TypeVar("UpdateSchemaType", bound=BaseModel)

class BaseDAO(Generic[ModelType, CreateSchemaType, UpdateSchemaType]):

    def __init__(self, model: Type[ModelType], session: AsyncSession):
        self._model = model
        self._session = session

    async def get(self, *args, **kwargs) -> ModelType | None:
        result = await self._session.execute(
            select(self._model).filter(*args).filter_by(**kwargs)
        )
        return result.scalars().first()

    async def get_many(
        self, *args, offset: int = 0, limit: int = 100, **kwargs
    ) -> list[ModelType]:
        result = await self._session.execute(
            select(self._model)
            .filter(*args)
            .filter_by(**kwargs)
            .offset(offset)
            .limit(limit)
        )
        return result.scalars().all()

    async def update(
            self,
            *,
            obj_in: UpdateSchemaType | dict[str, Any],
            db_obj: ModelType | None = None,
            **kwargs
    ) -> ModelType | None:

        db_obj = db_obj or await self.get(**kwargs)

        if db_obj is not None:
            obj_data = db_obj.__dict__

            if isinstance(obj_in, dict):
                update_data = obj_in
            else:
                update_data = obj_in.dict(exclude_unset=True)
            for field in obj_data:
                if field in update_data:
                    setattr(db_obj, field, update_data[field])
            self._session.add(db_obj)

        return db_obj

    async def delete(
            self, *args, db_obj: ModelType | None = None, **kwargs
    ) -> ModelType | None:
        db_obj = db_obj or await self.get(*args, **kwargs)
        await self._session.delete(db_obj)
        return db_obj

    async def save(self, obj_in: CreateSchemaType) -> ModelType:
        db_obj = self._model(**dict(obj_in))
        self._session.add(db_obj)
        return db_obj

    async def commit(self):
        await self._session.commit()

    async def flush(self, *objects):
        await self._session.flush(objects)


import numpy as np;
#import functools;

import sys
sys.path.append('../')

from proxSOR import proxSOR;

# compute equality constraints
def getGeq(z,z0,z1):
    N=len(z)/2+1;
    zaug=np.array([np.concatenate((z0.flatten(),z.flatten(),z1.flatten()))]).T;
    g=(zaug[0:-2:2]-zaug[2::2])**2+(zaug[1:-1:2]-zaug[3::2])**2-4/(N**2);
    return g;

# compute gradient of inequality constraints
def getGradG(z,z0,z1):
    N=int(np.round(len(z)/2+1));

    dg=np.zeros((N,len(z)));

    for k in range(N):

        if k==0:
            idx=0;
            idy=1;
            dg[0,0]=-2.0*(z0[0]-z[idx]);
            dg[0,1]=-2.0*(z0[1]-z[idy]);

        elif k==N-1:
            idx=2*(N-1)-2;
            idy=2*(N-1)-1;
            dg[N-1,idx]=2.0*(z[idx]-z1[0]);
            dg[N-1,idy]=2.0*(z[idy]-z1[1]);

        else:
            idx=2*(k-1);
            idy=2*(k-1)+1;
            idx2=2*k;
            idy2=2*k+1;

            dg[k,idx]=2.0*(z[idx]-z[idx2]);
            dg[k,idy]=2.0*(z[idy]-z[idy2]);
            dg[k,idx2]=-2.0*(z[idx]-z[idx2]);
            dg[k,idy2]=-2.0*(z[idy]-z[idy2]);

    return dg;

# compute inequality constraints
def getGineq(z,z2,r0):
    g=(z[0::2]-z2[0])**2+(z[1::2]-z2[1])**2-r0**2;
    return g;

# gradient of inequality constraints
def getGradGineq(z,z2,r0):
    N=int(np.round(len(z)/2+1))-1;

    dg=np.zeros((N,len(z)));

    for k in range(N):
        idx=2*k;
        idy=2*k+1;
        dg[k,idx]=2*(z[idx]-z2[0]);
        dg[k,idy]=2*(z[idy]-z2[1]);

    return dg;

# function used to numerically calculate gradients -> used for debugging
def numdf(f_,x,dx):
    n=np.shape(x)[0];
    y=np.eye(n)*dx;
    m=np.shape(f_(x))[0];

    df=np.zeros((m,n));
    for k in range(n):
        tmp=(f_(x+np.array([y[:,k]]).T)-f_(x))/dx;
        df[:,k]=tmp.flatten();

    return df;

# solves the catenary problem
# --------------------------------------------------------
# note that x contains the (x,y)-coordinates of the joints
# x=(x_1,y_1,x_2,y_2,x_3,y_3, ... )
# N is the number of links in the chain
# z0 left boundary condition
# z1 right boundary condition
# z2 r0 defines inequality constraint
# --------------------------------------------------------
# outputs whole solution trajectory, cost function trajectory, trajectory of multipliers and
# trajectory of constraints
def solveCatenary(N,x0,T,alpha,TOL_const,omega,\
                  TOL_KKT,MAXITER,\
                  MAXITER_PROX,ABSTOL_PROX,RELTOL_PROX,z0,z1,z2,r0):
    n=len(x0);

    # number of equality/inequality constraints
    neq=N;
    nineq=N-1;

    # define the objective function (potential energy)
    c=np.zeros((n,1));
    c[1::2]=9.81/(N+1)*np.ones((N-1,1));

    idx_eq=np.array([[True]*neq+[False]*nineq]).T;

    xsol=np.zeros((MAXITER,n));
    fsol=np.zeros((MAXITER,1));
    lambdasol=np.zeros((MAXITER,nineq+neq));
    prox_iter=np.zeros((MAXITER,1));
    g_ineq_tot=np.zeros((MAXITER,nineq));
    g_eq_tot=np.zeros((MAXITER,neq));
    n_act_constr=np.zeros((MAXITER,1));

    xsol[0,:]=x0.T;
    lambdasol[0,:]=np.zeros((1,neq+nineq));
    fsol[0]=np.dot(c.T,x0);

    for k in range(1,MAXITER):
        x1=np.array([xsol[k-1,:].copy()]).T;

        # evaluate gradient
        df=c;

        # evaluate constraints equality and inequality
        g_eq=getGeq(x1,z0,z1);
        g_ineq=getGineq(x1,z2,r0);

        g=np.concatenate((g_eq,g_ineq));

        # check which constraints are active
        # -> equality constraints are always included
        idx=np.logical_or(g<=TOL_const,idx_eq);

        g_ineq_tot[k-1,:]=g_ineq.T;
        g_eq_tot[k-1,:]=g_eq.T;

        # check whether there are active constraints
        if sum(idx)>0:
            nineq_tmp=sum(g_ineq<=TOL_const);

            # evaluate gradients
            W1=getGradG(x1,z0,z1).T;
            W2=getGradGineq(x1,z2,r0).T;
            W=np.concatenate((W1,W2),axis=1);

            # include only the constraints which are active
            Wtmp=W[:,idx.flatten()];
            Gtmp=np.dot(Wtmp.T,Wtmp);

            # create a good initial guess for lambda
            # this is only included in Python, since the implementation of the prox
            # function is slow here
            lambdatmp=np.dot(np.linalg.pinv(Gtmp),np.dot(df.T,Wtmp).T-alpha*g[idx.flatten()]);

            # compute the multipliers lambda
            itertmp,lambdatmp=proxSOR(Gtmp,np.diag(Gtmp),omega,-np.dot(df.T,Wtmp).T+alpha*g[idx.flatten()],TOL_const*alpha/2.0,nineq_tmp,neq,\
                                    MAXITER_PROX,ABSTOL_PROX,RELTOL_PROX,lambdatmp);

            prox_iter[k-1]=itertmp;
            lambdasol[k,idx.flatten()]=lambdatmp.T;
            n_act_constr[k-1]=nineq_tmp;    # keep track of how many constraints entered at this iteration

            # perform update
            x2=x1-T*df+T*np.dot(Wtmp,lambdatmp);

        else:
            lambdasol[k,:]=np.zeros(shape=(1,neq+nineq));
            prox_iter[k-1]=0;
            n_act_constr[k-1]=0;

            # perform update -> here no constraints are active
            x2=x1-T*df;

        xsol[k,:]=x2.T;
        fsol[k]=np.dot(c.T,x2);

        if np.max(np.abs(x1-x2)) <= T*TOL_KKT:
            break;

    niter=k;
    return xsol[0:k+1,:],fsol[0:k+1],lambdasol[0:k+1,:],niter,prox_iter[0:k],g_ineq_tot[0:k,:],g_eq_tot[0:k,:],n_act_constr[0:k];


import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import colors
from matplotlib.ticker import PercentFormatter
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
from sklearn.preprocessing import MinMaxScaler

class Making_DF:
    def __init__(self, client_id, client_secret):
        '''Handle client credentials flow'''

        self.spotify = spotipy.Spotify(client_credentials_manager=SpotifyClientCredentials(client_id=client_id,
                                                                                client_secret=client_secret))

    def get_song_info(self, spotify, playlist_ids):
        '''Returns the information of each song in one or more given Spotify playlists'''

        playlists = []
        track_ids = []
        songs = []
        artists = []
        dates = []
        count = 0

        # Get songs in each playlist
        for playlist_id in playlist_ids:
            playlists.append(spotify.playlist_items(playlist_id))

        # Get information for each song
        for playlist in playlists:
            for i in range(len(playlist['items'])):
                track_ids.append(playlist['items'][i]['track']['id'])
                songs.append(playlist['items'][i]['track']['name'])
                artists.append(playlist['items'][i]['track']['album']['artists'][0]['name'])
                dates.append(2019-count)
            count += 1

        # Get audio features of each track
        track_info = []
        for i in range(len(track_ids)//100):
            track_info.append(spotify.audio_features(track_ids[i*100:i*100+100]))

        # Create final list of tracks with all of the information
        final_list = []
        for sublist in track_info:
            for item in sublist:
                if item:
                    final_list.append(item)
                else:
                    final_list.append({})

        return final_list, songs, dates, artists

    def create_dataframe(self, final_list, songs, dates, artists):
        '''Creates a dataframe using the track information'''

        data = pd.DataFrame.from_dict(final_list)
        data['track_name'] = pd.Series(songs)
        data['year'] = pd.Series(dates)
        data['artist'] = pd.Series(artists)
        return data

    def preprocessing_data(self, data):
        '''Normalize the features to all be between 0 to 1 and create final dataframe'''

        features = ['danceability', 'energy', 'key', 'loudness', 'speechiness', 'acousticness', 'instrumentalness', 'liveness', 'valence', 'tempo']
        x = data[features]
        scaler = MinMaxScaler()
        x_scaled = scaler.fit_transform(x)
        data[features] = x_scaled
        data = data[['track_name', 'artist', 'year', 'danceability', 'energy', 'key', 'loudness', 'mode', 'speechiness', 'acousticness', 'instrumentalness', 'liveness', 'valence', 'tempo']]
        data = data.dropna()

        return data

    def run_function(self, playlist_ids):
        final_list, songs, dates, artists = self.get_song_info(self.spotify, playlist_ids)
        data = self.create_dataframe(final_list, songs, dates, artists)
        data = self.preprocessing_data(data)
        return data


import pygame

class Foguete():
    def __init__(self, x, y, sala) -> None:

        self.fixedPontos = ((20,0),(28,12),(28,28),(29,30),(33,32),(35,36),(36,40),(33,36),(28,32),(22,32),(20,40),(18,32),(12,32),(7,36),(4,40),(5,36),(6,32),(8,29),(12,28),(12,12))
        self.pontos = []
        self.escala = sala.escala
        self.atualizarPosicao(x,y, sala)

    def atualizarPosicao(self, x, y, sala):
        self.sala = sala
        self.x = x
        self.y = y
        self.pontos = []
        for ponto in self.fixedPontos:
            self.pontos.append((ponto[0]+self.x*self.escala, ponto[1]+self.y*self.escala))

    def renderItem(self, screen,x,y,escala):
        pontosDesenho = []
        for ponto in self.fixedPontos:
            pontosDesenho.append((ponto[0]+x, ponto[1]+y))
        pygame.draw.polygon(screen, (230, 130, 0), pontosDesenho)

    def tick(self):
        pass

    def render(self, screen):
        # desenha o chao
        cor = self.sala.cores[self.sala.bioma]

        pygame.draw.rect(screen, cor, pygame.Rect(self.x*self.escala, self.y*self.escala, self.escala, self.escala))

        # print desenha a arvore de melancia atraves do vetor pontos
        pygame.draw.polygon(screen, (230, 130, 0), self.pontos)


from types import SimpleNamespace
import numpy as np
from . import config
from .tokenizer import tokenizer
from .model import model
from . import client

def classify(texts, server=None, progress=lambda x: x):
    num_batches = int(np.ceil(len(texts) / config.batch_size))
    result = []
    for batch in progress(np.array_split(texts, num_batches)):
        result += classify_batch(batch, server=server)
    return result

def classify_batch(batch, server):
    batch_ids = tokenize_batch(batch)
    if server is None:
        scores = model(batch_ids).numpy()
    else:
        scores = client.score_batch(batch_ids, server)
    return [score[0] < score[1] for score in scores]

def tokenize_batch(batch):
    encoded = [
        tokenizer.encode_plus(
            t,
            max_length=config.max_sequence_length,
            pad_to_max_length=True,
            truncation=True
        )
        for t in batch
    ]
    input_ids = [t['input_ids'] for t in encoded]
    return np.array(input_ids)


import cPickle as pickle
import fileinput
from utilities import *
import pandas as pd

dict_file = '/scratch/users/bchen45/HLA_prediction/MCL_MHC_project/gene_analysis/UniprotID.dict'
path_MS = '/scratch/users/bchen45/HLA_prediction/MCL_MHC_project/MS_11_11/'
path_Mut = '/scratch/users/bchen45/HLA_prediction/MCL_MHC_project/Neoantigen_binding_predictions/'
path_geneFrag = '/scratch/users/bchen45/HLA_prediction/MCL_MHC_project/FragFromMut/'
dictRNA_file = '/scratch/users/bchen45/HLA_prediction/MCL_MHC_project/gene_analysis/MCLRNASeq_ave.dict'

from collections import defaultdict
def dumb(): return defaultdict(list)
data_MCL = defaultdict(dumb)
#first patient id
#second data type: MHC1 peptides, MHC1 peptide genes, MHC2 p, MHC2 pn, mutation info, HLA typing
#all lists

def MS_clean(frag_list,gene_list):
    #get rid of any SPA
    for i in range(len(gene_list)-1,0,-1):
        if '|SPA|' in gene_list[i] or len(gene_list[i])<6:
            del gene_list[i]
            del frag_list[i]
    return [frag_list,gene_list]

#get info from HLA lines
def get_HLA_typing (str0):
    str0 = str0.rstrip()
    if 'Locus' in str0:
        return [False,0,0,0]
    else:
        str0 = str0.split('\t')
        pid = str0[0]
        for i in range(2,4):
            part1 = 'HLA-'+str0[i].split('*')[0]
            part2 = '*'+str0[i].split('*')[1][0:5]
            if i == 2:
                alle1 = part1+part2
            else:
                alle2 = part1+part2
        return [True,pid,alle1,alle2]

#load dictionary
gene_dict = pickle.load(open(dict_file,'r'))
#dictRNA=pickle.load(open(dictRNA_file,'r'))
#initiate
mcl_mhc1_all = set()
mcl_mhc2_all = set()
mcl_mut_original = set()
mcl_wt_pep = set()
mcl_mut_pep = set()

for line0 in fileinput.input():
    if len(line0)> 15:
        #get HLA typing: 0,1 HLA-A 2,3 HLA-B 4,5 HLA-C 6,7 HLA-DQA1 8,9 HLA-DQB1 10,11 HLA-DRB1 12,13
        [info_line,pid,alle1,alle2] = get_HLA_typing(line0)
        if info_line:
            data_MCL[pid]['HLA_typing'].append(alle1)
            data_MCL[pid]['HLA_typing'].append(alle2)
    else:
        pid = 'MCL'+line0.rstrip()
        data_MCL['pid']['pid'].append(pid)
        ###get mutation data
        filename0 = path_Mut+pid+'.table_of_binding_peptides.out'
        #column 2, separated by space
        data_mut = pd.read_csv(filename0,' ')
        mut_gene_list = list(data_mut['Gene'])
        mut_original_list  = list(data_mut['Full_length_peptide'])
        mut_pep_mhc1 = list(data_mut['Mut_peptide'])
        wt_pep_mhc1 = list(data_mut['Wt_peptide'])
        mut_gene_list = remove_merged(mut_gene_list)
        data_MCL[pid]['mutation']=mut_gene_list
        data_MCL[pid]['mutation'].append('IGHM')
        data_MCL[pid]['mutation_pep_original'] = mut_original_list
        data_MCL[pid]['mutation_pep_mhc1'] = mut_pep_mhc1
        data_MCL[pid]['wt_pep_mhc1'] = wt_pep_mhc1
        #update total list
        mcl_mut_original = mcl_mut_original | set(mut_original_list)
        mcl_wt_pep = mcl_wt_pep | set(wt_pep_mhc1)
        mcl_mut_pep = mcl_mut_pep | set(mut_pep_mhc1)
        #get MHC1 or MHC2 fragments and genes
        for type0 in ['MHC1','MHC2']:
            filename0 = path_MS+pid+'_'+type0+'_20151109_SQLPowerTool.csv'
            #outfile0.write('#MCL'+pid+'\t'+type0+'\n')
            #print '#MCL'+pid+'\t'+type0+'\n'
            ms_frag_list = read_col(filename0,',',0,True)
            ms_gene_list = read_col(filename0,',',2,True)
            [ms_frag_list,ms_gene_list] = MS_clean(ms_frag_list,ms_gene_list)
            ms_gene_list = get_gene_from_ms(ms_gene_list,gene_dict,pid=pid)
            data_MCL[pid][type0+'_frag'] = ms_frag_list
            data_MCL[pid][type0+'_gene'] = ms_gene_list
            if type0 == 'MHC1':
                mcl_mhc1_all = mcl_mhc1_all | set(ms_frag_list)
            else:
                mcl_mhc2_all = mcl_mhc2_all | set(ms_frag_list)

data_MCL['pooled']['all_mhc1'] = list(mcl_mhc1_all)
data_MCL['pooled']['all_mhc2'] = list(mcl_mhc2_all)
data_MCL['pooled']['all_original_mut'] = list(mcl_mut_original)
data_MCL['pooled']['all_wt_mhc1'] = list(mcl_wt_pep)[1:]
data_MCL['pooled']['all_mut_mhc1'] = list(mcl_mut_pep)
data_MCL['pooled']['key_words'] = ['all_mhc1','all_mhc2','all_original_mut','all_wt_mhc1','all_mut_mhc1']
pickle.dump(data_MCL,open('MCL_11_11_2015_data_11_03_2016v2.0.dict','wb+'))


from flask_restplus import Resource, Namespace
from flask import request
from flask_jwt import jwt_required
from src.models.store import StoreModel

store_ns = Namespace('store')

store_ns_parser = store_ns.parser()
store_ns_parser.add_argument('name',
                             type=str,
                             location='args',
                             required=False,
                             default=None,
                             help='The name of the store')

@store_ns.expect(store_ns_parser)
@store_ns.route('', methods=['GET', 'POST', 'DELETE'])
class StoreResource(Resource):

    @jwt_required()
    def get(self):
        args = store_ns_parser.parse_args()
        name = args.get('name', None)
        store = StoreModel.find_by_name(name)
        if store:
            return store.json()
        return {'message': 'Store not found'}, 404

    @jwt_required()
    def post(self):
        data = request.json
        name = data['name']
        if StoreModel.find_by_name(name):
            return {'message': 'An store with name {} already exists.'.format(name)}

        store = StoreModel(data['name'])
        try:
            store.save_to_db()
        except:
            return 500

    @jwt_required()
    def delete(self):
        args = store_ns_parser.parse_args()
        name = args.get('name', None)
        item = StoreModel.find_by_name(name)
        if item:
            item.delete_from_db()
            return {'message': 'Store deleted'}
        else:
            return {'message': 'Store not found'}

stores_ns = Namespace('stores')

@stores_ns.route('', methods=['GET'])
class StoresResource(Resource):

    def get(self):
        return [x.json() for x in StoreModel.query.all()]


from collections import defaultdict
from heapq import heappop, heappush

def dijkstra(edges, s, f):
    g = defaultdict(list)
    for a, b, c in edges:
        g[a].append((c, b))
        g[b].append((c, a))

    q = [(0, s)]
    seen = set()
    while q:
        (cost, v1) = heappop(q)
        if v1 not in seen:
            seen.add(v1)
            if v1 == f:
                return cost
            for c, v2 in g.get(v1):
                if v2 not in seen:
                    heappush(q, (cost+c, v2))

    return float("inf")

N, M = map(int, input().split())
edges = []
for _ in range(M):
    a, b, c = map(int, input().split())
    edges.append((a, b, c))

print(dijkstra(edges, 1, N))


import re
from typing import Dict

import cv2
import numpy as np
from pylibdmtx import pylibdmtx
from pyzbar import pyzbar

import id_scripts.pytesseract as pytesseract
from id_scripts.id_card_models import *

name_regex = re.compile('[^a-zA-ZáÁéÉíÍóÓöÖőŐüÜűŰúÚ.\- ]')
city_regex = re.compile('[^a-zA-Z0-9áÁéÉíÍóÓöÖőŐüÜúÚ\- ]')
date_regex = re.compile('[^0-9.]')

def __run_tesseract_multiple_images(images,
                                    extension_configs,
                                    lang,
                                    run_otsu=False,
                                    blur_image=False,
                                    cluster_image_num=0):
    for i in range(0, len(images)):
        image = images[i]
        if blur_image:
            image = cv2.GaussianBlur(image, (5, 5), 0.7)
        if run_otsu:
            image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
            _, image = cv2.threshold(image, 0, 255, cv2.THRESH_OTSU)
            image = cv2.copyMakeBorder(image, 25, 25, 15, 15, cv2.BORDER_CONSTANT, value=255)
        elif cluster_image_num != 0:
            z = image.reshape((-1, 3))
            z = np.float32(z)
            criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
            k = cluster_image_num
            ret, label, center = cv2.kmeans(z, k, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
            center = np.uint8(center)
            min_index = 0
            for j in range(1, k):
                if sum(center[j]) < sum(center[min_index]):
                    min_index = j

            for j in range(0, k):
                if min_index == j:
                    center[j][:] = 0
                else:
                    center[j][:] = 255

            res = center[label.flatten()]
            image = res.reshape(image.shape)
            image = cv2.copyMakeBorder(image, 25, 25, 15, 15, cv2.BORDER_CONSTANT, value=(255, 255, 255))

        images[i] = image

    read_str = pytesseract.run_multiple_and_get_output(images,
                                                       extension='tsv',
                                                       extension_configs=extension_configs,
                                                       config="--psm 7", lang=lang)
    #    for idx, image in enumerate(images):
    #        cv2.imshow("TEST", image)
    #        cv2.waitKey(0)
    #        cv2.imwrite("test%d.png" % idx, image, (cv2.IMWRITE_PNG_COMPRESSION, 0))

    read_values = []
    lines = read_str.split("\n")
    for line in lines:
        read_values.append(line.split("\t"))

    page_index = read_values[0].index("page_num")
    base_row = read_values.pop(0)
    ret_list = [[]]
    ret_list[0].append(base_row)
    prev_page = '1'
    while len(read_values) != 0:
        next_row = read_values.pop(0)
        if next_row[page_index] != prev_page:
            ret_list.append([])
            ret_list[-1].append(base_row)
            prev_page = next_row[page_index]

        ret_list[-1].append(next_row)

    return ret_list

def __image_digits(read_values):
    confidence_level = 0
    text_index = read_values[0].index("text")
    confidence_index = read_values[0].index("conf")
    date_parts = []
    for i in range(1, len(read_values)):
        if len(read_values[i]) > text_index:
            date_parts_candidate = re.findall(r'\d+', read_values[i][text_index])
            #            name_part_candidate = date_regex.sub('', read_values[i][text_index]).lstrip().rstrip()
            #            date_parts_candidate = name_part_candidate.split(".")
            if 3 <= len(date_parts_candidate) <= 4 \
                    and len(date_parts_candidate[0]) == 4 and date_parts_candidate[0].isdigit() \
                    and len(date_parts_candidate[1]) <= 2 and date_parts_candidate[1].isdigit() \
                    and len(date_parts_candidate[2]) <= 2 and date_parts_candidate[2].isdigit():
                date_parts = date_parts_candidate
                confidence_level = int(read_values[i][confidence_index])
                break

    if len(date_parts) == 0:
        return ConfidenceValue(value="", confidence=0)

    year = int(date_parts[0])
    month = int(date_parts[1])
    day = int(date_parts[2])
    date = str(year) + "." + str(month).zfill(2) + "." + str(day).zfill(2)
    return ConfidenceValue(value=date, confidence=confidence_level)

def __filter_read_text(read_values):
    name = ""
    confidence_levels = []
    text_index = read_values[0].index("text")
    confidence_index = read_values[0].index("conf")
    for i in range(1, len(read_values)):
        if len(read_values[i]) > text_index:
            name_part_candidate = name_regex.sub('', read_values[i][text_index]).lstrip().rstrip()
            if len(name_part_candidate) != 0:
                confidence_levels.append(read_values[i][confidence_index])
                name = name + name_part_candidate + " "

    return name, confidence_levels

def __image_name(read_values) -> (ConfidenceValue, bool):
    name, confidence_levels = __filter_read_text(read_values)

    if len(name) == 0:
        return ConfidenceValue(value="", confidence=0), False

    name = name.upper().rstrip().lstrip()

    name_parts = name.split(' ')
    for part in name_parts.copy()[::-1]:
        if len(part) == 0 or part[0] == '-' or part[0] == '.':
            confidence_levels.pop(name_parts.index(part))
            name_parts.remove(part)

    if len(name_parts) > 1:
        return ConfidenceValue(
            value=' '.join(name_parts).title(),
            confidence=min(confidence_levels[0:len(name_parts) - 1])), True
    elif len(name_parts) == 0:
        return ConfidenceValue(value="", confidence=0), False
    else:
        return ConfidenceValue(value=' '.join(name_parts).title(), confidence=min(confidence_levels)), False

with open('data/names.txt', 'r', encoding="utf-8") as f:
    names = frozenset(f.read().splitlines())

with open('data/cities.txt', 'r', encoding="utf-8") as f:
    cities = frozenset(f.read().splitlines())

def __image_city(read_values):
    name, confidence_levels = __filter_read_text(read_values)

    if len(name) == 0:
        return ConfidenceValue(value="", confidence=0)

    city = name.upper().rstrip().lstrip()

    city_parts = city.split(' ')
    for part in city_parts.copy()[::-1]:
        if len(part) == 0 or part[0] == '-':
            confidence_levels.pop(city_parts.index(part))
            city_parts.remove(part)

    best_candidate = ''
    numbers_found = ''
    confidence_level = 100

    for part in city_parts[::-1]:
        if part in cities:
            best_candidate = part
            confidence_level = confidence_levels[city_parts.index(best_candidate)]
            break
        elif len(part) >= 2 and part[0:2].isdigit() and 0 < int(part[0:2]) <= 23:
            numbers_found = part[0:2]
        else:
            confidence_level = min(confidence_level, int(confidence_levels[city_parts.index(part)]))
            best_candidate = part + " " + best_candidate

    if len(numbers_found) != 0:
        best_candidate = best_candidate + " " + numbers_found

    return ConfidenceValue(value=best_candidate.title(), confidence=confidence_level)

def __check_field_match(input_field_value, field):
    return ConfidenceValue(value=input_field_value == field.value, confidence=int(field.confidence))

def __read_barcode_image(img):
    for i in range(1, 5):
        barcode_x_scale = 1 / i
        barcode_image = cv2.resize(img, (0, 0), fx=barcode_x_scale, fy=1)

        info = pyzbar.decode(barcode_image)
        if len(info) == 0:
            continue

        for barcode in info:
            if barcode.type == "CODE128" or barcode.type == "I25":
                return barcode.data.decode("UTF-8")

def __get_datamatrix_data(img):
    info = []
    for i in range(1, 5):
        barcode_x_scale = 1 / i
        barcode_image = cv2.resize(img, (0, 0), fx=barcode_x_scale, fy=1)

        info = pylibdmtx.decode(barcode_image)
        if len(info) == 0:
            continue
        else:
            break

    for barcode in info:
        return barcode.data.decode("UTF-8")

def __get_image_part(img, part: Rectangle):
    return img[part.top:(part.bottom + 1), part.left:(part.right + 1)]

def __run_image_field_check(img,
                            validating_fields: List[ValidationField],
                            field_type_map: Dict[str, IDCardFieldTypeEnum],
                            real_field_coordinates: Dict[str, Rectangle],
                            run_otsu,
                            use_blur,
                            cluster_image_num,
                            lang) -> List[ValidationResult]:
    success_list = []
    image_parts = []

    for field in validating_fields:
        field_type = field_type_map[field.key]
        if field_type != IDCardFieldTypeEnum.BARCODE and field_type != IDCardFieldTypeEnum.DATAGRAM:
            image_parts.append(__get_image_part(img, real_field_coordinates[field.key]))

    tesseract_output = []
    if len(image_parts) != 0:
        tesseract_output = __run_tesseract_multiple_images(image_parts,
                                                           extension_configs=[],
                                                           lang=lang,
                                                           run_otsu=run_otsu,
                                                           blur_image=use_blur,
                                                           cluster_image_num=cluster_image_num)

    i = 0
    for field in validating_fields:
        field_type = field_type_map[field.key]
        read_value = ""
        if field_type == IDCardFieldTypeEnum.TEXT:
            read_value = __filter_read_text(tesseract_output[i])[0]
            i += 1
        elif field_type == IDCardFieldTypeEnum.TEXT_CITY:
            read_value = __image_city(tesseract_output[i]).value
            i += 1
        elif field_type == IDCardFieldTypeEnum.TEXT_NAME:
            read_value = __image_name(tesseract_output[i])[0].value
            i += 1
        elif field_type == IDCardFieldTypeEnum.TEXT_DATE:
            read_value = __image_digits(tesseract_output[i]).value
            i += 1
        elif field_type == IDCardFieldTypeEnum.BARCODE:
            read_value = __read_barcode_image(__get_image_part(img, real_field_coordinates[field.key]))
        elif field_type == IDCardFieldTypeEnum.DATAGRAM:
            read_value = __get_datamatrix_data(__get_image_part(img, real_field_coordinates[field.key]))

        if read_value == field.value:
            success_list.append(ValidationResult(key=field.key, is_correct=True, confidence=100))

    return success_list

def validate_fields(img,
                    fields: List[ValidationField],
                    id_card_config: IDCardConfiguration) -> List[ValidationResult]:
    available_fields = [x.key for x in id_card_config.fields]
    remaining_validating_fields = [x for x in fields if x.key in available_fields]

    field_type_map = {x.key: x.type for x in id_card_config.fields}
    field_coordinates = {x.key: x.rectangle for x in id_card_config.fields}

    field_results = []

    if len(remaining_validating_fields) != 0:  # 79
        field_results += __run_image_field_check(img,
                                                 validating_fields=remaining_validating_fields,
                                                 field_type_map=field_type_map,
                                                 real_field_coordinates=field_coordinates,
                                                 run_otsu=False, use_blur=True, cluster_image_num=5, lang=id_card_config.language)
        remaining_validating_fields = [x
                                       for x
                                       in remaining_validating_fields
                                       if x.key not in [y.key for y in field_results]]

    if len(remaining_validating_fields) != 0:  # 79
        field_results += __run_image_field_check(img,
                                                 validating_fields=remaining_validating_fields,
                                                 field_type_map=field_type_map,
                                                 real_field_coordinates=field_coordinates,
                                                 run_otsu=True, use_blur=True, cluster_image_num=0, lang=id_card_config.language)
        remaining_validating_fields = [x for x in remaining_validating_fields if
                                       x.key not in [y.key for y in field_results]]
    if len(remaining_validating_fields) != 0:  # 77
        field_results += __run_image_field_check(img,
                                                 validating_fields=remaining_validating_fields,
                                                 field_type_map=field_type_map,
                                                 real_field_coordinates=field_coordinates,
                                                 run_otsu=False, use_blur=True, cluster_image_num=0, lang=id_card_config.language)
        remaining_validating_fields = [x for x in remaining_validating_fields if
                                       x.key not in [y.key for y in field_results]]
    if len(remaining_validating_fields) != 0:
        field_results += __run_image_field_check(img,
                                                 validating_fields=remaining_validating_fields,
                                                 field_type_map=field_type_map,
                                                 real_field_coordinates=field_coordinates,
                                                 run_otsu=False, use_blur=False, cluster_image_num=0, lang=id_card_config.language)
        remaining_validating_fields = [x for x in remaining_validating_fields if
                                       x.key not in [y.key for y in field_results]]

    if len(remaining_validating_fields) != 0:  # 75
        field_results += __run_image_field_check(img,
                                                 validating_fields=remaining_validating_fields,
                                                 field_type_map=field_type_map,
                                                 real_field_coordinates=field_coordinates,
                                                 run_otsu=True, use_blur=False, cluster_image_num=0, lang=id_card_config.language)
        remaining_validating_fields = [x for x in remaining_validating_fields if
                                       x.key not in [y.key for y in field_results]]

    field_results += [ValidationResult(x.key, False, 100) for x in remaining_validating_fields]

    return field_results


#!/usr/bin/env python3

import collections
import hashlib

# up down left right
def walls(path):
	inp = "udskfozm"
	d = hashlib.md5((inp + path).encode("utf-8")).hexdigest()
	return (d[0] in "bcdef", d[1] in "bcdef", d[2] in "bcdef", d[3] in "bcdef")

# bfs without remembering visited nodes
q = collections.deque([(0, 0, "")])
found = []

while len(q):
	x, y, p = q.popleft()
	w = walls(p)

	if (x, y) == (3, 3):
		if not len(found):
			# this is the first one we find
			# since this is a bfs, it should be the shortest path
			print(p)
		found.append(len(p))
		continue

	if y > 0 and w[0]:
		q.append((x, y - 1, p + "U"))
	if y < 3 and w[1]:
		q.append((x, y + 1, p + "D"))
	if x > 0 and w[2]:
		q.append((x - 1, y, p + "L"))
	if x < 3 and w[3]:
		q.append((x + 1, y, p + "R"))

# the last one should be the longest path cause bfs
print(found[-1])


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

from django.db import models
from django.contrib.auth.models import User
from mapeo.models import Persona

# Create your models here.
class Recolector(models.Model):
    ''' Esta es la clase para el nombre del recolector
    '''
    nombre = models.CharField(max_length=200)

    def __unicode__(self):
        return self.nombre

    class Meta:
        verbose_name_plural = "Recolector"

CHOICE_SEXO = ((1,'Hombre'),(2,'Mujer'))
CHOICE_OPCION = ((1,'Si'),(2,'No')) # Este choice se utilizara en toda la aplicacion que necesite si o no
CHOICE_RANGO = (
                (1, '18 - 25'),
                (2, '26 - 45'),
                (3, '26 - 65'),
                (4, 'Más de 65')
            )
CHOICE_NIVEL_EDUCATIVO = (
                (1, 'No sabe leer o escribir'),
                (2, 'Primaria Incompleta'),
                (3, 'Primaria Completa'),
                (4, 'Secundaria Incompleta'),
                (5, 'Bachiller'),
                (6, 'Universitario'),
            )

CHOICE_2_TIPOS = ((1,'Linea Base'),(2,'Entrevista Mujer'))

class Encuesta(models.Model):
    ''' Esta es la parte de la encuesta donde van los demas
    '''
    fecha = models.DateField()
    recolector = models.ForeignKey(Recolector)
    productor = models.ForeignKey(Persona, verbose_name='Entrevistado')
    jefe = models.IntegerField(choices=CHOICE_OPCION, verbose_name='Esta persona es jefe de la casa')
    tipo_encuesta = models.IntegerField(choices=CHOICE_2_TIPOS, default=2)

    user = models.ForeignKey(User)

    #campos ocultos para querys
    year = models.IntegerField(editable=False)

    def save(self):
        self.year = self.fecha.year
        super(Encuesta, self).save()

    def __unicode__(self):
        return self.productor.nombre

    class Meta:
        verbose_name_plural = "Encuesta"

## Indicador 1: Familia

CHOICE_EDUCACION = ((1,'Hombre mas de 18 años'),(2,'Mujeres mas de 18 años'),(3,'Hombre de 7 a 18 años'),
                     (4,'Mujeres de 7 a 18 años'),(5,'Niños menos de 6 años'),(6,'Niñas menos de 6 años'))

CHOICE_DESDE = ((1,'Menos de 5 años'),(2,'Más de 5 años'),(3,'No utilizar'))

##-------------------------------------------------------------------------------

# Indicador 3 y 4. Tipo de tenencia de parcela y solar y Documento legal de la propiedad, a nombre de quién

CHOICE_TENENCIA_1 = ((1,"Tierra propia (con o sin documento)"),(2,"Tierra mediaría"),
                   (3,"Tierra prestada"),(4,"Tierra colectiva"),
                   (5,"Tierra arrendada"),(6,"Tierra familiar"),(7,"Sin acceso a tierra"),)

CHOICE_TENENCIA_2 = ((1,"Escritura pública"),(2,"Título de reforma Agraria"),
                   (3,"Promesa de venta"),(4,"Escritura posesoria"),
                   (5,"Testamento o Herencia"),(6,"Sin documento"),)

CHOICE_DUENO = ((1,"Hombre"),(2,"Mujer"),(3,"Mancomunado"),(4,"Parientes"),
                (5,"Colectivo"),(6,"No hay"))

class TenenciaFamilia(models.Model):
    nombre = models.CharField(max_length=250)

    def __unicode__(self):
        return self.nombre

    class Meta:
        verbose_name_plural = "Opciones de acceso a tierra"

class TenenciaEntre(models.Model):
    nombre = models.CharField(max_length=250)

    def __unicode__(self):
        return self.nombre

    class Meta:
        verbose_name_plural = "Opciones tenecia de tierra"

class OpcionesDueno(models.Model):
    nombre = models.CharField(max_length=250)

    def __unicode__(self):
        return self.nombre

    class Meta:
        verbose_name_plural = "Opciones de documento de la tierra"

class Tenencia(models.Model):
    ''' Modelo tipo de tenencia de la propiedad
    '''
    parcela = models.ManyToManyField(TenenciaFamilia, verbose_name='Acceso a la tierra (Familia)')
    solar = models.ManyToManyField(TenenciaEntre, verbose_name='Tenencia de tierra familiar')
    dueno = models.ManyToManyField(OpcionesDueno, verbose_name='Documento de la tierra familiar a nombre de quién')
    encuesta = models.ForeignKey(Encuesta)

    #def __unicode__(self):
    #    return u'%s' % self.get_parcela_display()

    class Meta:
        verbose_name_plural = "Tenecia de la familia"

class TenenciaEntrevistada(models.Model):
    ''' Modelo tipo de tenencia de la propiedad
    '''
    parcela = models.ManyToManyField(TenenciaFamilia, verbose_name='Acceso a la tierra (de la persona entrevistada)')
    solar = models.ManyToManyField(TenenciaEntre, verbose_name='Tenencia de tierra de la persona entrevistada')
    dueno = models.ManyToManyField(OpcionesDueno, verbose_name='Documento de la tierra de la persona entrevistas a nombre de quién')
    encuesta = models.ForeignKey(Encuesta)

    #def __unicode__(self):
    #    return u'%s' % self.get_parcela_display()

    class Meta:
        verbose_name_plural = "Tenencia entrevistada"

#-----------------------------------------------------------
CHOICE_CASA_SOLAR = (
                    (1,"Propios (con o sin documento)"),
                    (2,"De su esposo/a"),
                    (3,"De su padres o suegros"),
                    (4,"Compartidos con los padres o suegros"),
                    (5,"Compartidos con familiares"),
                    (6,"Compartidos con otra familia"),
                    (7,"Alquilados"),
                )
CHOICE_TENENCIA_SOLAR = (
                    (1,"Escritura pública"),
                    (2,"Promesa de venta"),
                    (3,"Testamento o Herencia"),
                    (4,"Título de reforma Agraria"),
                    (5,"Escritura posesoria"),
                    (6,"Sin documento"),
                    (7,"No aplica"),
                )
CHOICE_DUENO_CASA_SOLAR = ((1,"Hombre"),(2,"Mujer"),(3,"Mancomunado"))

class CasaSolar(models.Model):
    casa = models.IntegerField('La casa y el solar son', choices=CHOICE_CASA_SOLAR)
    tenencia = models.IntegerField('Tenencia de solar', choices=CHOICE_TENENCIA_SOLAR)
    dueno = models.IntegerField('Documento legal de la propiedad, a nombre de quien', choices=CHOICE_DUENO_CASA_SOLAR)
    encuesta = models.ForeignKey(Encuesta)

    class Meta:
        verbose_name_plural = "Casa y solar"

#-------------------------------------------------------------------------------

CHOICE_MANEJA = ((1,"Hombre"),(2,"Mujer"),(3,"Ambos"),(4,"Hijos/as"),
                 (5,'Hombre-Hijos'),(6,'Mujer-Hijos'),(7,'Todos'))

#-------------------------------------------------------------------------------


from django.shortcuts import get_object_or_404
from django.db import models
from django.core.validators import MaxValueValidator

LIMIT = 2

class Slot(models.Model):
    slot = models.PositiveSmallIntegerField(primary_key=True, validators=[MaxValueValidator(23)])
    name = models.JSONField(default=list)

    @classmethod
    def book(cls, slot_number, new_person):
        """
        Method for booking a new slot, if possible
        :param slot_number: A number between 0 to 23
        :param new_person: Name of person who wants to book the slot
        :return: Object of Slot if the slot is booked or else None
        """

        slot, _ = cls.objects.get_or_create(slot=slot_number)
        persons = slot.name  # grabbing the persons who have previously booking the current slots

        if len(persons) < LIMIT:
            persons.append(new_person)
            slot.name = persons
            slot.save()

            return slot
        return None

    @classmethod
    def cancel(cls, slot_number, name):
        """
        Method for canceling a slot, if possible
        :param slot_number: A number between 1 to 23
        :param name: Name of the person who wants to cancel the booking
        :return: True if the slot could be canceled else False
        """

        slot = get_object_or_404(cls, slot=slot_number)

        if name in slot.name:
            slot.name.remove(name)

            # If other person has booked the same slot then save it else, delete the object
            if len(slot.name):
                slot.save()
            else:
                slot.delete()

            return True
        return False


import math

# 有理数型をインポート

# 組合せの総数`
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

X, Y = (int(i) for i in input().split())

i = (2*X-Y)//3
j = (2*Y-X)//3

if(  i < 0 or j < 0 or (2*X-Y)%3!=0 or (2*Y-X)%3!=0 ):
    print(0)
else:
    print(cmb(i+j, i) % 1000000007)


# -*- coding: utf-8 -*-
from __future__ import unicode_literals, absolute_import

from django.http import JsonResponse
from django.shortcuts import render

# Create your views here.
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_POST

from main.forms import EmailForm
from main.mail_utils import MailSender

@require_POST
@csrf_exempt
def email_request(request):
    # Options for POST request can be found in the EmailForm model. In addition to the required parameters, there's
    # an optional parameter, mail_client, which specifies which service to use, otherwise it uses default.

    form = EmailForm(request.POST)

    status_code = 400
    response_data = {"success": False}
    if form.is_valid():
        sender = MailSender()
        mail_response = sender.send_mail_request(
            form.cleaned_data,
            mail_service=form.cleaned_data.get('mail_client')
        )
        if mail_response.ok:
            response_data["success"] = True
            status_code = 200

        response_data["message"] = mail_response.content

    return JsonResponse(response_data, status=status_code)

def index(request):
    response_code = -1
    mail_sent_to = None
    if request.method == "POST":
        form = EmailForm(request.POST)
        if form.is_valid():
            sender = MailSender()
            mail_response = sender.send_mail_request(
                form.cleaned_data, mail_service=form.cleaned_data['mail_client'])
            if mail_response.ok:
                mail_sent_to = "{} ({})".format(form.cleaned_data['to_name'], form.cleaned_data['to_email'])
                form = EmailForm()
    else:
        form = EmailForm()

    context = {
        'form': form,
        'mail_sent_to': mail_sent_to,
        'response_code': response_code
    }
    return render(request, 'main/email_form.html', context)


from django.conf.urls import patterns, include, url

from django.contrib import admin
from django.views.generic import TemplateView

admin.autodiscover()

urlpatterns = patterns('',
                       # Examples:
                       # url(r'^$', 'django_atomiadns.views.home', name='home'),
                       # url(r'^blog/', include('blog.urls')),

                       url(r'^admin/', include(admin.site.urls)),

                       url(r'^logout', 'django.contrib.auth.views.logout', {'next_page': '/'}),
                       url(r'^about/?$', TemplateView.as_view(template_name='about.html')),
                       url(r'^change_password/?$', 'web.views.change_password'),

                       # main page
                       url(r'^/?(?P<offset>\d+)?/?$', 'web.views.home'),

                       # login
                       url(r'^login/?$', 'web.views.login'),

                       # edit zone
                       url(r'^edit/(?P<zone>[^/]+)/?', 'web.views.edit'),
                       # export zone
                       url(r'^export/(?P<zone>[^/]+)/?', 'web.views.export'),

                       # import zone
                       url(r'^import/(?P<zone>[^/]+)/?', 'web.views.import_zone'),

                       # ajax calls zone

                       url(r'^ajax/add_zone/?$', 'web.post.add_zone'),
                       url(r'^ajax/remove_zone/?$', 'web.post.remove_zone'),
                       url(r'^ajax/import_zone/?', 'web.post.import_zone'),
                       # ajax calls record
                       url(r'^ajax/change_record/?$', 'web.post.change_record'),
                       url(r'^ajax/add_record/?$', 'web.post.add_record'),
                       url(r'^ajax/remove_record/?$', 'web.post.remove_record'),
)


import os
import sys
import transaction

from sqlalchemy import engine_from_config

from pyramid.paster import (
    get_appsettings,
    setup_logging,
    )

from ..models import (
    DBSession,
    set_up_tables,
    )

from ..models.user import User
from ..models.task import Task

def usage(argv):
    cmd = os.path.basename(argv[0])
    print('usage: %s <config_uri>\n'
          '(example: "%s development.ini")' % (cmd, cmd))
    sys.exit(1)

def main(argv=sys.argv):
    if len(argv) != 2:
        usage(argv)
    config_uri = argv[1]
    setup_logging(config_uri)
    settings = get_appsettings(config_uri)
    engine = engine_from_config(settings, 'sqlalchemy.')
    DBSession.configure(bind=engine)
    set_up_tables(engine)
    with transaction.manager:
        u1=User('jozko', 'jozko@mrkvicka.sk')
        u2=User('ferko', 'ferko@fetacisko.sk')
        t1=Task('zasad mrkvu', u1)
        t2=Task('nefetuj tolko', u2)
        DBSession.add_all([u1,u2,t1,t2])


#!/usr/bin/env python3

import sys
import os.path

if len(sys.argv) != 4:
    print("Make trajactory movie")
    exit("Usage: %s <t0.xyz> <history.def> N_steps" % sys.argv[0])

# global variables
iltcp= []
x= []
y= []
z= []
# global variables

def output():
    print(len(iltcp))
    print("N_steps=", sys.argv[3])
    for l in iltcp:
        k= int(l[1]) # index
        print(l[0], x[k], y[k], z[k])

with open(sys.argv[1], 'r') as T0: # Reading t0 file
    i= 0
    for line in T0:
        i +=1
        if i==1: N_atoms= int(line)
        elif i==2: pass
        else:
            (tp, xi, yi, zi, *others) = line.split()
            x.append(xi)
            y.append(yi)
            z.append(zi)
            if 2==int(tp) or 3==int(tp):
                iltcp.append( (tp, i-3) )

if(N_atoms != len(x)): exit("number inconsistent (N_atoms, len_X) %d %d" % (N_atoms, len(x)))

with open(sys.argv[2], 'r') as IN: # Read his.def
    i= 0
    output()
    for line in IN:
        i +=1
        if 1==i%3:
            N= int(line)
            if(N != 1): exit("suppose to be 1 defect only but: %d" % N)
        if 2==i%3:
            (T, step, time_) = line.split()
            if(int(step) != (i+1)/3): exit("step inconsistent: %d %d" % (int(step), (i+1)/3))
        if 0==i%3:
            (tp, ltcp, *others) = line.split()
            iltcp.append( (tp, ltcp) )
            if 0==(i/3)%int(sys.argv[3]): output()