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

import ast
import keyword
from collections import defaultdict, namedtuple
import contextlib

from . import asdl
from .asdl import pyasdl

def load(name):
    '''Load a ASDL Schema by name; e.g. "Python.asdl".

    Returns a Schema object.

    This tries to load from the version-specific directory, first.
    If it failed, it loads from the common-directory.
    '''
    parser, loader = asdl.load_pyschema(name)
    python_asdl = loader.load()
    return build_schema(python_asdl)

def build_schema(asdl_tree):
    schblr = SchemaBuilder()
    schblr.visit(asdl_tree)
    return schblr.schema

class _rule(namedtuple('rule', ['kind', 'fields'])):
    __slots__ = ()

    SUM = 0     # sums, e.g. Foo | Bar
    PROD = 1    # producs, e.g. (Foo, Bar)

    @classmethod
    def sum(cls, fields):
        return cls(kind=cls.SUM, fields=fields)

    @classmethod
    def product(cls, fields):
        return cls(kind=cls.PROD, fields=fields)

    @property
    def is_sum(self):
        return self.kind == self.SUM

    @property
    def is_product(self):
        return self.kind == self.PROD

class _debuginfo_t(namedtuple("_debuginfo_t", ['node', 'field', 'offset'])):
    __slots__ = ()

    def __str__(self):
        '''Create string reprentation to be used in SchemaError
        '''
        if self.field is not None:
            if self.offset is not None:
                return "At %s.%s[%d]" % (self.node, self.field, self.offset)
            else:
                return "At %s.%s" % (self.node, self.field)
        else:
            return "At %s" % (self.node)

def _debuginfo(node, field=None, offset=None):
    assert field or not offset
    return _debuginfo_t(node=node, field=field, offset=offset)

class SchemaError(Exception):
    def __init__(self, ctxt, msg):
        super(SchemaError, self).__init__("%s: %s" % (ctxt, msg))

class Schema(object):
    '''A Schema object that is used to verify against an AST.

    It is built from SchemaBuilder

    Usage:

        schema.verify(ast)
        schema.verify(ast, context=SchemaContext())
    '''
    def __init__(self, name):
        # name of the asdl module
        self.name = name

        # a dictionary of {type name -> fields}
        self.types = defaultdict(list)

        # a dictionary of {definition name -> _rule}
        self.dfns = {}

        # { type name -> fields }
        self.attributes = defaultdict(list)

    def verify(self, ast, context=None):
        '''Check against an AST raises SchemaError upon error.

        ast --- The ast being verified
        context --- [optional] a SchemaContext.
        '''
        context = context if context is not None else SchemaContext()
        return SchemaVerifier(self, context).visit(ast)

    def __str__(self):
        return "%s(name=%s, types=%s, rules=%s, attributes=%s)" % (
            type(self).__name__,
            self.name,
            self.types,
            self.dfns,
            self.attributes)

    def debug(self):
        print(("Schema %s" % self.name))
        for k, fs in self.types.items():
            print(('    --', k, fs))
        for k, fs in self.dfns.items():
            if fs.is_sum():
                print(('   ', k))
                for f in fs.fields:
                    print(('       |', f))
            else:
                print(('   ', k, '=', ', '.join(map(str, fs.fields))))

class SchemaContext(object):
    '''Keep information about context:
        - builtin type handlers

    User may expand the builtin type handlers.  See `builtin_handlers`.

    '''
    def __init__(self):
        self.__builtins = {}
        self._add_default_handler()

    def _add_default_handler(self):
        self.builtin_handlers['identifier'] = _verify_identifier
        self.builtin_handlers['int'] = _verify_int
        self.builtin_handlers['string'] = _verify_string
        self.builtin_handlers['object'] = _verify_object
        self.builtin_handlers['bool'] = _verify_bool

    @property
    def builtin_handlers(self):
        '''A dictionary of type name -> handler

        A handler is just a callable like the following:

            def handler(value):
                return is_value_valid(value) # returns boolean
        '''
        return self.__builtins

class SchemaVerifier(ast.NodeVisitor):
    '''A internal class that implement a the verification logic.
    '''
    def __init__(self, schema, context):
        '''
        schema --- a Schema object that defines a valid AST.
        context --- a SchemaConctext object.
        '''
        self.schema = schema
        self.context = context
        self._debug_context = None

    def visit(self, node):
        '''Start verification at the node.

        Verification can begin at any AST node.  Can it will recursively
        verify each and every subtree.
        '''
        current = self._get_type(node)
        self._visit(node, current)

    def _visit(self, node, current):
        nodename = type(node).__name__
        # traverse the children

        for field in current:
            with self._new_debug_context(node=nodename, field=field.name):
                value = getattr(node, str(field.name), None)
                if getattr(field, 'seq', False):
                    # is sequence?
                    children = getattr(node, str(field.name), None)
                    if children is None:
                        raise SchemaError(self._debug_context, "Missing field")
                    elif not _is_iterable(children):
                        raise SchemaError(self._debug_context,
                                          "Field must be iterable")

                    for offset, child in enumerate(children):
                        with self._new_debug_context(node=nodename,
                                                     field=field.name,
                                                     offset=offset):
                            self._sentry_dfn(field.type, child)

                elif value is None:
                    if not getattr(field, 'opt', False):
                        raise SchemaError(self._debug_context, "Missing field")
                    else:
                        pass
                elif value is not None:
                    self._sentry_dfn(field.type, value)

                else:
                    assert False, field

    @contextlib.contextmanager
    def _new_debug_context(self, **kws):
        # push
        old = self._debug_context
        self._debug_context = _debuginfo(**kws)
        yield
        # pop
        self._debug_context = old

    def _sentry_child(self, child, subtypes):
        childname = type(child).__name__
        if childname not in subtypes:
            raise SchemaError(self._debug_context, "Cannot be a %s" % childname)

    def _sentry_dfn(self, name, value):
        name = str(name)

        if name in self.context.builtin_handlers:
            # is a builtin type?
            handler = self.context.builtin_handlers[name]
            if not handler(value):
                raise SchemaError(self._debug_context,
                                  "Expected %s but got %s" % \
                                  (name, type(value)))
        else:
            # not a builtin type?
            rule = self._get_dfn(name)
            if rule.is_sum:
                self._sentry_child(value, rule.fields)
                self.visit(value)
            else:
                assert rule.is_product
                name0 = type(value).__name__
                if name0 != name:
                    raise SchemaError(self._debug_context,
                                      "Expecting %s but got %s" % \
                                      (name, name0))

                self._visit(value, rule.fields)

    def _get_dfn(self, name):
        ret = self.schema.dfns.get(str(name))
        if ret is None:
            raise SchemaError(self._debug_context,
                              "Missing definition for '%s' in the ASDL" % name)
        return ret

    def _get_type(self, cls_or_name):
        name = (cls_or_name
                if isinstance(cls_or_name, str)
                else type(cls_or_name).__name__)
        fields = self.schema.types.get(name)
        attrs = self.schema.attributes.get(name)
        if fields is None or attrs is None:
            raise SchemaError(self._debug_context,
                              "Unknown AST node type: %s" % name)
        return fields + attrs

class SchemaBuilder(pyasdl.VisitorBase):
    '''A single instance of SchemaBuilder can be used build different
    Schema from different ASDL.

    Usage:
        schblr = SchemaBuilder()
        schblr.visit(some_asdl)
        schema = schblr.schema      # get the schema object

    NOTE:
        - It ignore the attributes of the nodes.
    '''
    def __init__(self):
        super(SchemaBuilder, self).__init__()

    def visitModule(self, mod):
        self._schema = Schema(str(mod.name))
        for dfn in mod.dfns:
            self.visit(dfn)

    def visitType(self, type):
        self.visit(type.value, str(type.name))

    def visitSum(self, sum, name):
        self.schema.dfns[str(name)] = _rule.sum([str(t.name)
                                                 for t in sum.types])
        self.schema.attributes[name].extend(sum.attributes)

        for t in sum.types:
            self.visit(t, name, sum.attributes)

    def visitConstructor(self, cons, name, attrs=[]):
        typename = str(cons.name)
        self.schema.types[typename].extend(cons.fields)
        self.schema.attributes[typename].extend(attrs)

    def visitField(self, field, name):
        key = str(field.type)

    def visitProduct(self, prod, name):
        assert not hasattr(prod, 'attributes')
        self.schema.dfns[str(name)] = _rule.product(prod.fields)
        for f in prod.fields:
            self.visit(f, name)

    @property
    def schema(self):
        return self._schema

#
# Builtin types handler
#

def _verify_identifier(value):
    return isinstance(value, str)

def _verify_int(value):
    return isinstance(value, int) or isinstance(value, long)

def _verify_string(value):
    return isinstance(value, str)

def _verify_object(value):
    return isinstance(value, object)

def _verify_bool(value):
    return isinstance(value, bool)

#
#  Utilities
#
def _is_iterable(value):
    try:
        iter(value)
    except TypeError:
        return False
    else:
        return True

def verify_names(names):
    for name in names:
        if keyword.iskeyword(name):
            raise ValueError("%r is a keyword" % (name,))

def verify_schema_keywords(schema):
    """
    Verify schema, checking for the use of any Python keywords.
    """
    verify_names(schema.dfns)
    verify_names(schema.types)
    verify_names([field.name for fields in schema.types.itervalues()
                                 for field in fields])


#!/usr/bin/env python3
# -*- coding=utf-8 -*-
"""
@author:Wllen
@file:server_socket.py
@time:2018/7/31 21:09
"""
import socket
import subprocess
import struct
import json

phone = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
phone.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)  # 重用端口，解决端口冲突问题
phone.bind(("127.0.0.1", 8080))
phone.listen(5)  # listen(5) 表示最大挂起的连接数
while True:
    conn, client_addr = phone.accept()
    print(client_addr)
    while True:  # 通讯循环
        try:
            cmd = conn.recv(8096)  # 1024为接收数据的最大数，单位：bytes
            if not cmd: break  # Linux上适用
            # print("客户端的数据",data.decode("utf-8"))
            obj = subprocess.Popen(cmd.decode("utf-8"), shell=True,
                                   stdout=subprocess.PIPE,
                                   stderr=subprocess.PIPE)
            stdout = obj.stdout.read()
            stderr = obj.stderr.read()

            # 第一步 制作报头
            header_dict = {
                "filename": "None",
                "md5": "42424242",
                "total_size": len(stdout) + len(stderr)
            }
            header_json = json.dumps(header_dict)   # 将报头序列化
            header_bytes = header_json.encode("utf-8")
            header = struct.pack("i", len(header_bytes))
            # 第二步，发送报头长度给客户端
            conn.send(header)

            # 第三步，把报头发送给客户端
            conn.send(header_bytes)

            # 第四步，再发真实数据给客户端
            conn.send(stdout)
            conn.send(stderr)
        except ConnectionResetError:  # windows上适用
            break

    conn.close()
phone.close()


from clearterminal import *
MOD_NUMBER = 20 # the algorithm

# Task 1.6
def write_hashing_algorithms(book_string):
    book_id = book_string[0:3]
    hashed_id = int(book_id) % MOD_NUMBER # Task 1.5 Hashing Algorithm
    while hash_table[hashed_id] != "":
        hashed_id = hashed_id + 1
    hash_table[hashed_id] = book_string # Task 1.8

# Task 1.9
def read_hashing_algorithms(book_string):
    found = False
    out_of_range = False
    book_id = book_string[0:3]
    hashed_id = int(book_id) % MOD_NUMBER
    while not(found) and not(out_of_range):
        if hashed_id <= (len(hashed_list_from_file) -1):
            if (hashed_list_from_file[hashed_id])[0:3] != book_id:
                hashed_id = hashed_id + 1
            else:
                found = True
        else:
            out_of_range = True
    if found:
        print(hashed_list_from_file[hashed_id])
    else:
        print("Not in the hashed list")

# Creating MOD_NUMBER length array
hash_table = []
for i in range(MOD_NUMBER):
    hash_table.append("")

with open("BookCollection.txt", "r") as file:
    book_list = file.readlines()
    for book in book_list:
        write_hashing_algorithms(book)

with open("HashedCollection.txt", "w") as hashed_file:
    hashed_file.writelines(hash_table)

with open("HashedCollection.txt", "r") as hashed_file:
    hashed_list_from_file = hashed_file.readlines()
    print(hashed_list_from_file)

search1 = input("Find: ")
read_hashing_algorithms(search1)
search1 = input("Find: ")
read_hashing_algorithms(search1)

Pre-released Material A2 Exams - May-June 2021/Task1.5-1.9 - Hashing Algorithm - [Finished].py


# -*- coding: UTF-8 -*-
import json
import utils
import os

lableJsonPath = "lables.json"
PATH_DIR = "fonts"
lables = []

for child_dir in os.listdir(PATH_DIR):
    if os.path.isdir((PATH_DIR+"/"+child_dir)):
        lables.append(child_dir)

utils.readWrite(lableJsonPath, json.dumps(lables))


# time complexity O(n), space complexity O(1)
# For instance, in the case 3,3,5,0,0,3,1,4
# The max profit is (4 - 1) + (3 - 0), which is also equal to 4 - (1 - (3 - 0))

class Solution(object):
    def maxProfit(self, prices):
        """
        :type prices: List[int]
        :rtype: int
        """
        hold1 = hold2 = float("-inf")
        release1 = release2 = 0
        for i in prices:
            hold1 = max(hold1, -i)
            release1 = max(release1, hold1 + i)
            hold2 = max(hold2, release1 - i)
            release2 = max(release2, hold2 + i)
        return release2


from fractions import gcd

N,M = [int(i) for i in input().split()]

S = ([int(i) for i in input().split()])

def mdc(l):
    newl = l
    md = gcd(newl[0],newl[1])
    if len(newl) >= 2:
        for i in range(len(newl)-2):
            md = gcd(md,newl[i+2])
    return md

def criarSubSeq(sub):
    total = 0

    for i in sub:
        if i != 1:
            total += 1

    for i in range(len(sub)):
        for j in range(len(sub)-i+1):
            comp = sub[i:j+i]
            if len(comp) >= 2:
                if mdc(comp) > 1:
                    total += 1;

    return total

r = []

for i in range(M):
    inp = input().split()
    T = inp[0]
    I = int(inp[1])
    E = int(inp[1])
    V = int(inp[2])
    D = int(inp[2])
    if T == '2':
        r.append(criarSubSeq(S[(E-1):(D)]))
    else:
        S[I-1] = V

for i in r:
    print(i)


# coding=utf-8

import re
import cnconvert as cn2
import sql
import Kmail
from Spider import Spider
import logging
import logging.config

logging.config.fileConfig("config/logging.conf")

class Title:
    def __chinese(self, chapter):
        """
        中文标题:如第一百章
        :param chapter:
        :return:

        >>> self.__chinese("第两百零一章")
        201
        >>> self.__chinese("第两百测零试一章")
        201
        >>> self.__chinese("两百")
        -1
        """
        num = -1
        pattern_chi = re.compile(r'第.+章')
        match_chi = pattern_chi.search(chapter)
        if match_chi:
            chapter = match_chi.group()[1:-1]

            if "两" in chapter:
                chapter = chapter.replace("两", "二")
            try:
                num = cn2.c2n(chapter)

            # 如果无法从中文转为数字，说明章节名混入了奇怪的字符
            except Exception as e:
                string = ""
                pat = re.compile(r'[零一二三四五六七八九十百千两]+')
                match_chi = pat.findall(chapter)
                for i in match_chi:
                    string += i
                num = cn2.c2n(string)
        return num

    def __digit(self, chapterTitle):
        '''
        数字标题:如第100章

        >>> getNumOfTitle_d("第1029章")
        1029
        '''
        num = -1
        pattern_d = re.compile(r'第\d+章')
        cha = pattern_d.search(chapterTitle)
        if cha:
            rs = cha.group()[1:-1]
            if len(str(rs)) >= 1:
                num = int(rs)
        return num

    def getNumOfTitle(self, chapter):
        '''
        获得章节编号

        >>> getNumOfTitle("第1029章")
        1029
        >>> getNumOfTitle("第两百零一章")
        201
        >>> getNumOfTitle("第两百测零试一章")
        201
        '''
        num = -1
        # 若全数字
        pattern_d = re.compile(r'第\d+章')
        cha = pattern_d.search(chapter)
        if cha:
            num = self.__digit(chapter)
        # 若其他
        else:
            num = self.__chinese(chapter)
        return num

def suffix(start, end):
    '''
    start(int) end(int) 构造待发送的文件名后缀：230-235 表示从230章到235章

    >>> suffix(23,90)
    '23-90'
    '''
    suf = ""
    if start == end:
        suf = str(start)
    else:
        suf = str(start) + "-" + str(end)
    return suf

def is_chi(self, text):
    "判断是否为中文"
    return all('\u4e00' <= char <= '\u9fff' for char in text)

# +++++++++++++++++++++++++++++++++++++++++
class Novel():
    """
    小说
    """

    def __init__(self, book_name, en_book_name):
        self.book_name = book_name
        self.en_book_name = en_book_name

class NovelDownloader():
    """
    负责封装下载小说的基本操作
    """

    def __init__(self):
        self.spider = Spider()
        self.FilenameCharset = "en"

    def sort_chapter(self, list):
        return sorted(list, key=lambda l: l[1])

    def set_filename_charset(self, charset):
        self.FilenameCharset = charset

    def get_latest_updates(self, pageUrl, checknum):
        """
        获得最近更新的小说
        :param pageUrl:小说目录链接
        :param checknum:小于该数字的更新数，不发送
        :return:
        """
        lists = self.spider.get_all_chapter_links(pageUrl)
        # novelname_chi = self.spider.get_novel_name_chi(pageUrl)
        novelname_chi = "修真四万年"
        # 当前读到了
        nowat = sql.readAtChapter(novelname_chi)

        # 设置文件名语言en or chi
        if self.FilenameCharset == "en":
            # filename = self.spider.get_novel_name_en(pageUrl)
            filename = "xiuzheng40000years"
        else:
            filename = novelname_chi

        l = []
        isnew = False

        # 将新章节的url和name放入l
        for i in lists:
            if i[1] in nowat or nowat in i[1]:
                isnew = True
                continue
            if isnew:
                if not sql.hasChapter(novelname_chi, i[1]):
                    l.append(i)

        logging.info("小说标题:%s" % novelname_chi)
        # checknum=0 一有新的章节就更新 checknum=3 累积三章以上再推送
        try:
            newest = l[-1][1]
            logging.info("当前已读到%s" % nowat)
            logging.info("最新章节为%s" % newest)
        except Exception as e:
            logging.info("无新章节")
            return ""
        if checknum != 0 and len(l) != 0 and checknum > len(l):
            logging.info("[暂不发送]:当前小说更新了%d章...[%d/%d]" % (len(l), len(l), checknum))
            return ""
        else:
            for i in l:
                sql.addChapter(novelname_chi, i[1])

        # 构造待发送的文件名：该处理很不健壮！！
        cleanlist = self.__wash_novel_list(l)
        start = cleanlist[1]
        end = cleanlist[2]
        # 构造文件名
        suf = suffix(start, end)
        filename = filename + "#" + suf + ".txt"
        # 抓取l内的文章
        self.spider.download(l, filename)
        logging.info("下载文件成功...")
        sql.setAtChapter(novelname_chi, newest)
        return filename

    def __wash_novel_list(self, lists):
        l = []
        mx = 0
        mi = 1000000
        logging.info(">" * 5 + "正在处理章节信息" + "<" * 5)

        newcircle = False
        numOfChapterOne = 0

        ERROR_RANGE = 10
        # ERROR_PROBABILITY=0.85

        logging.info("待处理章节数%d" % len(lists))
        tit = Title()
        if len(lists) == 1:
            num = tit.getNumOfTitle(lists[0][1])
            mx = num
            mi = num
        else:
            for item in lists:
                try:
                    chapter = item[1]
                    # print("wash "+chapter)
                    num = tit.getNumOfTitle(chapter)

                    # 某些章节出现从1到200又从1开始，
                    # 如 1 2 3 4 …… 368 1 2 3 ……256 ，
                    # 因为分了卷名之类，所以检测是否章节名循环
                    # 如果进入新的循环，那么重置计数器
                    # mx=0 mi=1000000 newcircle=False

                    # 判断小说是否含有多个卷：当前为第一章
                    if 1 == num:
                        # 若第一章的计数不为0，说明存在多个第一章 即进入新的【卷】
                        if numOfChapterOne != 0:
                            newcircle = True
                            numOfChapterOne += 1
                    # 如果小说为正常序号：第1章~第n章
                    if not newcircle:
                        if num >= mx:
                            mx = num
                        if num <= mi:
                            mi = num
                    # 如果小说按卷等分别标序号：第一卷第1章~第二卷第30章
                    else:
                        mx = 0
                        mi = 1000000
                        newcircle = False

                    l.append(item)
                except Exception as e:
                    print(e)
        # print(">>"*20)
        logging.debug("max:%d min:%d" % (mx, mi))
        return l, mi, mx

    def run(self):
        pass

class Service:
    """
    向外提供服务
    """

    def __init__(self):
        self.novel = NovelDownloader()
        self.mail = Kmail.Mail()
        self.checknum = 3
        self.novelList = []
        self.novels = []

    def all_novels_latest_updates_2_kindle(self):
        """
        发送最近更新的小说(未阅读)到kindle中
        :param novelUrl: 小说目录链接
        :param checknum:
        :return:
        """
        for noveltouple in self.novels:
            fname = self.novel.get_latest_updates(noveltouple, self.checknum)
            if fname != "":
                self.novelList.append(fname)

        if len(self.novelList) == 0:
            logging.info("nothing to send")
        else:
            logging.debug("%s wait to be send..." % self.novelList)
            self.mail.send2kindle(self.novelList)

    def add_novel(self, noveltouple):
        self.novels.append(noveltouple)

if __name__ == '__main__':
    import doctest

    doctest.testmod(verbose=True)


# -*- coding:utf-8 -*-
import cv2
import tensorflow as tf
import numpy as np
from skimage import io,transform
from sys import path

from skimage import io,transform
import os
import glob

#读取图片及其标签函数
def read_image(path):
    images = []
    for img in glob.glob(path+'/*.tif'):
        print("reading the image:%s"%img)
        image = io.imread(img)
        image = transform.resize(image,(32,32,1))
        image = np.reshape(image , [-1,32,32,1])
        images.append(image)
    return np.asarray(images,dtype=np.float32)

def main():
    sess = tf.InteractiveSession()
#模型恢复
    saver=tf.train.import_meta_graph('model_data/model.meta')

    saver.restore(sess, 'model_data/model')
    graph = tf.get_default_graph()

    # 获取输入tensor,,获取输出tensor
    x = sess.graph.get_tensor_by_name("x:0")
    y_ = sess.graph.get_tensor_by_name("y_:0")
    results = sess.graph.get_tensor_by_name("results:0")

    # la = [2]
    # im = io.imread('./images/D.tif')

    # #调整大小
    # im = transform.resize(im,(32,32,1))
    # im = np.reshape(im , [-1,32,32,1])

    # #类型转换
    # im = np.asarray(im,dtype=np.float32)
    # la = np.asarray(la,dtype=np.int32)

    path = "./images/"
    switch = {
        '[0]': '0',
        '[1]': '1',
        '[2]': '2',
        '[3]': '3',
        '[4]': '4',
        '[5]': '5',
        '[6]': '6',
        '[7]': '7',
        '[8]': '8',
        '[9]': 'A',
        '[10]': 'B',
        '[11]': 'C',
        '[12]': 'D',
        '[13]': 'E',
        '[14]': 'F',
        '[15]': 'G',
        '[16]': 'H',
        '[17]': 'P',
        '[18]': 'S',
        '[19]': 'T',
        '[21]': 'Y',
    }
    result = []
    dataArr = read_image(path)
    print('单图测试：')
    for data in dataArr:
        res = sess.run(results, feed_dict={x:data,y_:[]})
        res = str(res)
        result.append(switch[res])
    print('识别结果为：', result)
    #关闭会话
    sess.close()

if __name__ == '__main__':
    main()


# from YBLEGACY.FLSqlQuery import FLSqlQuery
# from YBLEGACY.FLSqlCursor import FLSqlCursor
# from YBLEGACY.FLUtil import FLUtil
from YBLEGACY import qsatype
from YBLEGACY.constantes import *
from YBUTILS.viewREST import clasesBase
from YBLEGACY.FLManager import FLManager
import base64
import shutil
from django.http import HttpResponse
import psycopg2

def verificarConfiguracion():
    campos = "usarbdlocal,servidor,puerto,contrasena,nombrebd,usuario,driver,idconexion"
    mng = FLManager()
    tMtd = mng.metadata("gd_config")
    idconexion = tMtd.field("idconexion")
    if not idconexion:
        campos = "usarbdlocal,servidor,puerto,contrasena,nombrebd,usuario,driver"
    valor = {}
    valor["result"] = 1
    query = qsatype.FLSqlQuery()
    query.setTablesList("gd_config")
    query.setSelect(campos)
    query.setFrom("gd_config")
    query.setWhere("1=1")
    if query.exec_():
        if query.next():
            if idconexion and query.value("idconexion"):
                campos = "servidor,puerto,contrasena,nombrebd,usuario,driver"
                queryConexion = qsatype.FLSqlQuery()
                queryConexion.setTablesList("gd_conexiones")
                queryConexion.setSelect(campos)
                queryConexion.setFrom("gd_conexiones")
                queryConexion.setWhere("id = '" + str(query.value("idconexion")) + "'")
                if queryConexion.exec_():
                    if queryConexion.next():
                        campos = campos.split(",")
                        valor = getCamposFromQuery(valor, queryConexion, campos)
                        valor["usarbdlocal"] = query.value("usarbdlocal")
                        valor["idconexion"] = query.value("idconexion")
                    else:
                        return False
                else:
                    return False
            else:
                campos = campos.split(",")
                valor = getCamposFromQuery(valor, query, campos)
        else:
            return False
    else:
        return False

    return valor

def getCamposFromQuery(valor, query, campos):
    for i in campos:
        valor[i] = query.value(i)
    return valor

def fileUpload(prefix, pk, files, nombre=None, tipo=None):
    print(prefix, pk, files)
    for file in files:
        # print("______________________")
        # print(file)
        for f in files.getlist(file):
            if not AQGesDoc(prefix, pk, f, nombre, tipo):
                return False
    return True

def AQGesDoc(prefix, pk, file, nombre=None, tipo=None):
    # Insertar gd_documentos, gd_versionesdoc, gd_objetodoc
    conf = verificarConfiguracion()
    # print(conf)
    try:
        # print(file)
        # print(file.name)
        # # print(file.read())
        # name = file.name
        # # encode = base64.b64encode(file.read()).decode("utf-8")
        # ba = bytearray(file.read())
        # print(ba)
        # # print(encode)
        # print(1)
        # # decode = base64.b64decode(encode)
        # # print(2)
        # # print(decode)
        # filepath = '/home/juanma/cabreraImg/' + name
        # print(3)
        # print(filepath)
        # with open(filepath, 'wb') as f:
        #     f.write(ba)
        # print("????")

        codigo = obtenerCodigoDoc("general")
        creadopor = "web"
        hoy = qsatype.Date()
        fechacreacion = str(hoy)[:10]
        iddocumento = int(qsatype.FLUtil.sqlSelect(u"gd_documentos", u"nextval('gd_documentos_iddocumento_seq')", "1=1"))
        # print("________________", iddocumento)
        fichero = file.name
        nombre = nombre or file.name
        idversionactual = 1
        codtipo = tipo or "Documento"
        horacreacion = str(hoy)[-8:]
        if "idconexion" in conf:
            idconexion = conf["idconexion"]
        else:
            idconexion = None
        # print("_______________", idconexion)
        if not conf["usarbdlocal"]:
            ficheroLocal = "vacio"
            # fichero = str(base64.b64encode(file.read()).decode("utf-8"))
            fichero = psycopg2.Binary(file.read())
        else:
            # ficheroLocal = str(base64.b64encode(file.read()).decode("utf-8"))
            ficheroLocal = psycopg2.Binary(file.read())
        # print(fichero)
        if not conf["usarbdlocal"]:
            qsatype.FLSqlQuery().execSql("INSERT INTO gd_versionesdoc(modificadopor, horamodif, fechamodif, iddocumento, fichero, contenido, fecha, version) VALUES ('" + creadopor + "','" + horacreacion + "','" + fechacreacion + "','" + str(iddocumento) + "','" + nombre + "','" + str(ficheroLocal) + "','" + fechacreacion + "','" + str(idversionactual) + "')")
            qsatype.FLSqlQuery().execSql("INSERT INTO gd_versionesdoc(modificadopor, horamodif, fechamodif, iddocumento, fichero, contenido, fecha, version) VALUES ('" + creadopor + "','" + horacreacion + "','" + fechacreacion + "','" + str(iddocumento) + "','" + nombre + "'," + str(fichero) + ",'" + fechacreacion + "','" + str(idversionactual) + "')", conf["nombrebd"])
        else:
            qsatype.FLSqlQuery().execSql("INSERT INTO gd_versionesdoc(modificadopor, horamodif, fechamodif, iddocumento, fichero, contenido, fecha, version) VALUES ('" + creadopor + "','" + horacreacion + "','" + fechacreacion + "','" + str(iddocumento) + "','" + nombre + "'," + str(ficheroLocal) + ",'" + fechacreacion + "','" + str(idversionactual) + "')")

        idversionactual = qsatype.FLUtil.sqlSelect(u"gd_versionesdoc", u"idversion", "iddocumento = " + str(iddocumento) + " AND versionrep IS NULL")
        if idconexion:
            into = "gd_documentos(codigo, creadopor, fechacreacion, iddocumento, fichero, nombre, codtipo, idversionactual, horacreacion, idconexion)"
            values = "('" + str(codigo) + "','" + creadopor + "','" + fechacreacion + "','" + str(iddocumento) + "','" + file.name + "','" + nombre + "','" + codtipo + "','" + str(idversionactual) + "','" + horacreacion + "','" + str(idconexion) + "')"
        else:
            into = "gd_documentos(codigo, creadopor, fechacreacion, iddocumento, fichero, nombre, codtipo, idversionactual, horacreacion)"
            values = "('" + str(codigo) + "','" + creadopor + "','" + fechacreacion + "','" + str(iddocumento) + "','" + file.name + "','" + nombre + "','" + codtipo + "','" + str(idversionactual) + "','" + horacreacion + "')"
        qsatype.FLSqlQuery().execSql("INSERT INTO " + into + " VALUES " + values)
        # if not qsatype.FLUtil.sqlInsert(u"gd_documentos", qsatype.Array(["codigo", "creadopor", "fechacreacion", "iddocumento", "fichero", "nombre", "codtipo", "idversionactual", "horacreacion", "idconexion"]), qsatype.Array([codigo, creadopor, fechacreacion, iddocumento, nombre, nombre, codtipo, idversionactual, horacreacion, idconexion])):
        #         print("sale por aqui")
        #         return False
        if not conf["usarbdlocal"]:
            qsatype.FLSqlQuery().execSql("INSERT INTO " + into + " VALUES " + values, conf["nombrebd"])
            # if not qsatype.FLUtil.sqlInsert(u"gd_documentos", qsatype.Array(["codigo", "creadopor", "fechacreacion", "iddocumento", "fichero", "nombre", "codtipo", "idversionactual", "horacreacion", "idconexion"]), qsatype.Array([codigo, creadopor, fechacreacion, iddocumento, nombre, nombre, codtipo, idversionactual, horacreacion, idconexion]), conf["usarbdlocal"]):
            #         print("sale por aqui")
            #         return False

        qsatype.FLSqlQuery().execSql("INSERT INTO gd_objetosdoc(clave, tipoobjeto, iddocumento) VALUES ('" + str(pk) + "','" + prefix + "','" + str(iddocumento) + "')")
        if not conf["usarbdlocal"]:
            qsatype.FLSqlQuery().execSql("INSERT INTO gd_objetosdoc(clave, tipoobjeto, iddocumento) VALUES ('" + str(pk) + "','" + prefix + "','" + str(iddocumento) + "')", conf["nombrebd"])

    except Exception as e:
        print("exception gesdoc: ", e)
        return False
    # try:
    #     fichero = qsatype.FLUtil.sqlSelect(u"gd_versionesdoc", u"contenido", "iddocumento = " + str(iddocumento) + "", cx=conf["nombrebd"])
    #     decode = base64.b64decode(fichero.tobytes())
    #     filepath = '/home/juanma/cabreraImg/' + file.name
    #     with open(filepath, 'wb') as f:
    #         f.write(decode)
    # except Exception as e:
    #     print(e)
    return True

def siguienteNumero(tipo):
    conf = verificarConfiguracion()
    numero = int(qsatype.FLUtil.sqlSelect(u"gd_secuencias", u"valorout", ustr(u"nombre = '", tipo, u"'"))) + 1
    # if not qsatype.FLUtil.sqlUpdate(u"gd_secuencias", u"valorout", numero, ustr(u"nombre = '", tipo, u"'")):
    if not qsatype.FLSqlQuery().execSql("UPDATE gd_secuencias set valorout = '" + str(numero) + "' WHERE nombre = '" + tipo + "'"):
        return False
    if not qsatype.FLSqlQuery().execSql("UPDATE gd_secuencias set valorout = '" + str(numero) + "' WHERE nombre = '" + tipo + "'", conf["nombrebd"]):
        return False
    return numero

def cerosIzquierda(numero, totalCeros):
    ret = str(numero)
    numCeros = totalCeros - len(ret)
    while numCeros > 0:
        ret = "0" + ret
        numCeros = numCeros - 1
    return ret

def obtenerCodigoDoc(tipo):
    numero = siguienteNumero(tipo)
    if not numero:
        return False
    codigo = cerosIzquierda(numero, 10)
    return codigo

def generaFiles(prefix, pk):
    fichero = getFiles(prefix, pk)
    if not fichero:
        return False
    # decode = base64.b64decode(fichero["fichero"])
    decode = fichero["fichero"]
    # print(decode)
    filename = fichero["nombre"]
    disposition = "attachment"
    # response = HttpResponse(content_type='image/png')
    response = HttpResponse()
    response['Content-Disposition'] = '{disposition}; filename="{filename}"'.format(disposition=disposition, filename=filename)
    response.write(decode)
    return response

def getFiles(prefix, pk):
    conf = verificarConfiguracion()
    nombrebd = None
    if not conf:
        return {}
    if not conf["usarbdlocal"]:
        nombrebd = conf["nombrebd"]
    # if not conf["usarbdlocal"]:
    #     iddocumento = qsatype.FLUtil.sqlSelect(u"gd_objetosdoc", u"iddocumento", "clave = '" + str(pk) + "' AND tipoobjeto = '" + str(prefix) + "'", cx=nombrebd)
    # else:
    iddocumento = qsatype.FLUtil.sqlSelect(u"gd_objetosdoc", u"iddocumento", "clave = '" + str(pk) + "' AND tipoobjeto = '" + str(prefix) + "'")
    if not iddocumento:
        return False
    if not conf["usarbdlocal"]:
        fichero = qsatype.FLUtil.sqlSelect(u"gd_versionesdoc", u"contenido", "iddocumento = " + str(iddocumento) + "", cx=nombrebd)
    else:
        fichero = qsatype.FLUtil.sqlSelect(u"gd_versionesdoc", u"contenido", "iddocumento = " + str(iddocumento) + "")
    if fichero:
        fichero = fichero.tobytes()
    if not conf["usarbdlocal"]:
        nombre = qsatype.FLUtil.sqlSelect(u"gd_versionesdoc", u"fichero", "iddocumento = " + str(iddocumento) + "", cx=nombrebd)
    else:
        nombre = qsatype.FLUtil.sqlSelect(u"gd_versionesdoc", u"fichero", "iddocumento = " + str(iddocumento) + "")
    response = {}
    response["nombre"] = nombre
    response["fichero"] = fichero
    return response


import click
import pandas
from nanopath.pipelines import AssemblyPipeline
from pathlib import Path

@click.command()
@click.option(
    '--path',
    '-p',
    type=Path,
    help='Path to pipeline output directory containing results'
)
@click.option(
    '--exclude',
    '-e',
    default=None,
    type=Path,
    help='Path to file with one name per line to exclude (e.g. from manual curation)'
)
@click.option(
    '--exclude_genotype',
    '-g',
    default=None,
    type=str,
    help='List of comma separated columns of genotypes to exclude'
)
@click.option(
    '--outdir',
    '-o',
    default='collected',
    type=Path,
    help='Path to server collection output directory'
)
@click.option(
    '--illumina',
    '-i',
    is_flag=True,
    help='Assembly workflow included Illumina data [false]'
)
def collect(path, exclude, exclude_genotype, outdir, illumina):
    """ Collect output from assembly pipeline into a data table """

    ap = AssemblyPipeline(path=path, outdir=outdir)

    if exclude_genotype is not None:
        exclude_genotype = exclude_genotype.split(',')

    if exclude:
        excl = pandas.read_csv(
            exclude, header=None, names=['name']
        ).name.tolist()
    else:
        excl = None

    ont = ap.collect_genotypes(component='ont', exclude=exclude_genotype)
    nanoq = ap.collect_statistics(mode="")

    if illumina:
        ref = ap.collect_genotypes(component='illumina', exclude=exclude_genotype)
        hybrid = ap.collect_genotypes(component='hybrid', exclude=exclude_genotype)
        unicycler = ap.collect_genotypes(component='unicycler', exclude=exclude_genotype)

        if hybrid is not None or unicycler is not None:
            ap.plot_genotype_heatmap(
                reference=ref, genotypes={'medaka': ont, 'pilon': hybrid, 'unicycler': unicycler}, exclude=excl
            )

            dnadiff = ap.collect_dnadiff(exclude=excl)

            if dnadiff is not None:
                ont_dnadiff = dnadiff.loc[dnadiff['branch'] == 'ont_medaka', :]\
                    .merge(ont, on="name", how='inner').merge(nanoq, on="name")

                hybrid_dnadiff = dnadiff.loc[dnadiff['branch'] == 'hybrid_medaka', :]\
                    .merge(hybrid, on="name").merge(nanoq, on="name")

                unicycler_dnadiff = dnadiff.loc[dnadiff['branch'] == 'hybrid_unicycler', :] \
                    .merge(ont, on="name", how='inner').merge(nanoq, on="name")

                ont_dnadiff.to_csv(
                    outdir / 'ont_vs_ref.tsv', sep='\t', index=False
                )

                hybrid_dnadiff.to_csv(
                    outdir / 'hybrid_vs_ref.tsv', sep='\t', index=False
                )

                unicycler_dnadiff.to_csv(
                    outdir / 'unicycler_vs_ref.tsv', sep='\t', index=False
                )

        ref.to_csv(
            outdir / 'illumina_reference_genotypes.tsv', sep='\t', index=False
        )

    nanoq.to_csv(
        outdir / 'read_qc_all.tsv', sep='\t', index=False
    )


#!/usr/bin/env python
# -*- coding: utf-8 -*-

import nltk, re

def load_file(filename):
    corpus = []
    f = open(filename, 'r')
    for line in f:
        doc = re.findall(r'\w+',line)
        if len(doc)>0:
            corpus.append(doc)
    f.close()
    return corpus

class Vocabulary:
    def __init__(self):
        self.vocas = []        # id to word
        self.vocas_id = dict() # word to id
        self.docfreq = []      # id to document frequency

    def term_to_id(self, term):
        if term not in self.vocas_id:
            voca_id = len(self.vocas)
            self.vocas_id[term] = voca_id
            self.vocas.append(term)
            self.docfreq.append(0)
        else:
            voca_id = self.vocas_id[term]
        return voca_id

    def doc_to_ids(self, doc):
        list = []
        words = dict()
        for term in doc:
            id = self.term_to_id(term)
            list.append(id)
            if not id in words:
                words[id] = 1
                self.docfreq[id] += 1
        return list

    def __getitem__(self, v):
        return self.vocas[v]

    def size(self):
        return len(self.vocas)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri May 31 08:05:10 2019

@author: huzhicong
"""

'''

#part1
#1.1
def list_traverse(the_list):
    for item in the_list:
        print(item)

list_traverse([1,2,3,4])

#1.2
def list_traverse2(the_list):
    if len(the_list)==0:
        return
    else:
        print(the_list[0])
    return list_traverse2(the_list[1:])

list_traverse2([1,2,3,4])

#1.3

def l_tra3(the_list):
    if len(the_list)>0:
        l_tra3_aux(0,the_list)

def l_tra3_aux(index, the_list):
    if index==len(the_list):
        return
    else:
        print(the_list[index])

    return l_tra3_aux(index+1, the_list)

l_tra3([1,2,3,4])
'''

#part2

'''
将上面n-1层 从 a 移动到 wait
将最底下的第n层从 a 移动到 target
将n-1 层 从 wait 移动到 target

'''
'''
def move_tower(height,ft,tt,wt):
    if height>=1:
       move_tower(height-1,ft,wt,tt)
       move(ft,tt)
       move_tower(height-1,wt,tt,ft)

def move(f,t):
    print('%s to %s'%(f,t))

move_tower(4,'A','C','B')
'''

#part3
#3.1merge sort

def m_sort(array):

    n=len(array)
    if n>1:
        mid=n//2
        left_array=array[:mid]
        right_array=array[mid:]

        m_sort(left_array)
        m_sort(right_array)
        print('-----before-----')
        print(array)
        print('----------------')
        i=0
        j=0
        k=0

        while i<len(left_array) and j<len(right_array):
            if left_array[i]<=right_array[j]:
                array[k]=left_array[i]
                i+=1
            else:
                array[k]=right_array[j]
                j+=1
            k+=1
        while i<len(left_array):
            array[k] = left_array[i]
            i+=1
            k+=1
        while j<len(right_array):
            array[k]=right_array[j]
            j+=1
            k+=1
        print('-----after------')
        print(array)
        print('----------------')

list1=[9,76,3,4,5,3,1,2]
m_sort(list1)


import six
import sys

import rsa

def _pad_sig(message, length):
    """ Simplified padding of the message.

    Return message is length bytes and of the format:

    00 01 <FF pading> 00 message
    """

    maxlen = length - 4
    msglen = len(message)

    if msglen > maxlen:
        raise OverflowError('{} byte message > {} message limit'.format(msglen, maxlen))

    padlen = length - msglen - 3

    return b''.join([
        b'\x00\x01',
        padlen * b'\xff',
        b'\x00',
        message.encode('latin1')
    ])

class VerificationError(Exception):
    """An error in Message Verification."""

    def __init__(self, message, *args):
        message = message % args
        super(VerificationError, self).__init__(message)

class SSLError(Exception):
    """An error in Key Management."""

    def __init__(self, message, *args):
        message = message % args
        super(SSLError, self).__init__(message)

class Key(object):
    """An RSA key handler"""

    def __init__(self, fp=None):
        self.key = None
        self.public = False
        if not fp:
            return
        if isinstance(fp, six.binary_type) and fp.startswith(b'-----BEGIN'):
            # PEM formatted text
            self.raw = fp
        elif isinstance(fp, six.string_types) and fp.startswith('-----BEGIN'):
            # PEM formatted text
            self.raw = fp
        elif isinstance(fp, six.string_types):
            self.raw = open(fp, 'rb').read()
        else:
            self.raw = fp.read()

        if sys.version_info[0] < 3:
            if type(self.raw) is not unicode:
                self.raw = unicode(self.raw)

        self._load_key()

    def _load_key(self):
        try:
            self.key = rsa.PrivateKey.load_pkcs1(self.raw)
        except ValueError:
            self.key = rsa.PublicKey.load_pkcs1(self.raw)
            self.public = True
        except:
            raise ValueError("'{}' is not a valid RSA key".format(self.raw))

    @classmethod
    def generate(cls, size=1024):
        self = cls()
        (_, self.key) = rsa.newkeys(size)
        return self

    def sign(self, message):
        """ Simplified signature compatible with `openssl rsautl -sign`

        Signing logic pulled from the rsa lib, but does not add the asn1 before padding.

        """

        if self.public:
            raise SSLError('can not sign a message using a public key')

        keylength = rsa.common.byte_size(self.key.n)
        padded = _pad_sig(message, keylength)
        payload = rsa.transform.bytes2int(padded)
        encrypted = rsa.core.encrypt_int(payload, self.key.d, self.key.n)
        block = rsa.transform.int2bytes(encrypted, keylength)

        return block

    def verify(self, message, sig):
        """ Emulate `openssl rsautl -verify` """

        blocksize = rsa.common.byte_size(self.key.n)
        encrypted = rsa.transform.bytes2int(sig)
        decrypted = rsa.core.decrypt_int(encrypted, self.key.e, self.key.n)
        clearsig = rsa.transform.int2bytes(decrypted, blocksize)

        # If we can't find the signature  marker, verification failed.
        if clearsig[0:2] != b'\x00\x01':
            raise VerificationError("Verification failed, sig starts with '{}'".format(clearsig[0:2]))

        padded = _pad_sig(message, blocksize)
        if padded != clearsig:
            raise VerificationError('Verification failed')

        return True

    def private_export(self):
        if self.public:
            raise SSLError('private method cannot be used on a public key')

        return self.key.save_pkcs1('PEM')

    def public_export(self):
        return rsa.PublicKey(self.key.n, self.key.e).save_pkcs1('PEM')


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

import base64
from io import StringIO
from odoo import api, fields, models, _
import io
from itertools import groupby
import time
from odoo.exceptions import ValidationError
from odoo.tools import DEFAULT_SERVER_DATE_FORMAT, DEFAULT_SERVER_DATETIME_FORMAT
from operator import itemgetter

try:
    import xlwt
except ImportError:
    xlwt = None

class InventoryAgeReport(models.Model):
    _name = "inventory.age.wizard"
    _description = "Inventory Age Report"

    generate_type = fields.Selection([('warehouse', 'Warehouse'), ('location', 'Location')], 'Report Based On',
                                     default='warehouse')
    warehouse_ids = fields.Many2many('stock.warehouse', string='Warehouse')
    location_ids = fields.Many2many('stock.location', string='Location')
    include_all_product = fields.Boolean("Include All Products")
    product_ids = fields.Many2many('product.product', string='Products')

    @api.constrains('product_ids')
    def validate_days(self):
        if not self.include_all_product and not self.product_ids:
            raise ValidationError(("Please enter valid Products !"))

    def get_product_stock(self):
        ware_loc_list = []
        product_list = []
        obj = self.env['product.product']
        if self.location_ids:
            for loc in self.location_ids:
                for quant in loc.quant_ids.search([]):
                    ware_loc_list.append(quant.product_id.id)
        if self.warehouse_ids:
            for ware in self.warehouse_ids:
                for quant in ware.lot_stock_id.quant_ids.search([]):
                    ware_loc_list.append(quant.product_id.id)
        if self.product_ids:
            for prd in self.product_ids:
                product_list.append(prd.id)
            product_obj = self.product_ids.search([('id', 'in', ware_loc_list), ('id', 'in', product_list)])
            obj = product_obj
            return obj
        if self.include_all_product:
            product_obj = self.env['product.product'].search([('id', 'in', ware_loc_list)])
            obj = product_obj
            return obj

    def get_ware_loc(self):
        if self.generate_type == 'warehouse':
            return self.warehouse_ids
        else:
            return self.location_ids

    def print_exl_report(self):
        filename = 'Inventory Age Report.xls'
        get_product_stock = self.get_product_stock()
        get_ware_loc = self.get_ware_loc()
        workbook = xlwt.Workbook()
        stylePC = xlwt.XFStyle()
        alignment = xlwt.Alignment()
        alignment.horz = xlwt.Alignment.HORZ_CENTER
        fontP = xlwt.Font()
        fontP.bold = True
        fontP.height = 200
        stylePC.font = fontP
        stylePC.num_format_str = '@'
        stylePC.alignment = alignment
        style_title = xlwt.easyxf(
            "font:height 300; font: name Liberation Sans, bold on,color black; align: horiz center;pattern: pattern solid, fore_colour aqua;")
        style_table_header = xlwt.easyxf(
            "font:height 200; font: name Liberation Sans, bold on,color black; align: horiz center")
        style = xlwt.easyxf("font:height 200; font: name Liberation Sans,color black;")
        worksheet = workbook.add_sheet('Sheet 1')
        current_date = time.strftime(DEFAULT_SERVER_DATE_FORMAT)
        worksheet.write_merge(0, 1, 0, 11, "Inventory Age Report", style=style_title)
        worksheet.write(6, 0, 'Id', style_table_header)
        worksheet.write(6, 1, 'Reference', style_table_header)
        worksheet.write(6, 2, 'Product Name', style_table_header)
        worksheet.write(6, 3, 'Total Qty', style_table_header)
        worksheet.write(6, 4, 'Qty(% of Overall Inventory)', style_table_header)
        worksheet.write(6, 5, 'Oldest Qty', style_table_header)
        worksheet.write(6, 6, 'Value ($)', style_table_header)
        worksheet.write(6, 7, 'Value(% of Overall Inventory)', style_table_header)
        worksheet.write(6, 8, 'Average Cost', style_table_header)
        worksheet.write(6, 9, 'Average Sale Price', style_table_header)
        worksheet.write(6, 10, 'Current Sale Price', style_table_header)
        worksheet.write(6, 11, 'Current Cost', style_table_header)
        if self.generate_type == 'warehouse':
            worksheet.write_merge(3, 3, 0, 1, 'Warehouses ', style_table_header)
        else:
            worksheet.write_merge(3, 3, 0, 1, 'Locations ', style_table_header)
        col = 2
        for wl in get_ware_loc:
            worksheet.write(3, col, wl.name)
            col += 1
        row = 7
        col = 0
        count = 1
        if get_product_stock:
            total_qty = 0
            total_value = avg_cost = total_price = total_cost = avg_cost = avg_price = 0.0
            for product in get_product_stock:
                total_qty += product.qty_available
                if product.qty_available and total_qty:
                    qty_per_inv = ((product.qty_available * 100) / total_qty)
                    worksheet.write(row, col + 4, qty_per_inv, style)
                total_value += product.value_svl
                if product.value_svl and total_value:
                    value_per_inv = ((product.value_svl * 100) / total_value)
                    worksheet.write(row, col + 7, value_per_inv, style)
                total_cost += product.product_tmpl_id.standard_price
                total_price += product.product_tmpl_id.list_price
                if total_price:
                    avg_price = (len(get_product_stock) / total_price)
                    worksheet.write(row, col + 9, avg_price, style)
                if total_cost:
                    avg_cost = (len(get_product_stock) / total_cost)
                    worksheet.write(row, col + 8, avg_cost, style)
                worksheet.write(row, col, count, style)
                worksheet.write(row, col + 1, product.default_code, style)
                worksheet.write(row, col + 2, product.name, style)
                worksheet.write(row, col + 3, product.qty_available, style)
                worksheet.write(row, col + 5, product.qty_available, style)
                worksheet.write(row, col + 6, product.value_svl, style)

                worksheet.write(row, col + 10, product.product_tmpl_id.list_price, style)
                worksheet.write(row, col + 11, product.product_tmpl_id.standard_price, style)
                row += 1
                count += 1
        fp = io.BytesIO()
        workbook.save(fp)
        export_id = self.env['inventory.age.report.excel'].create(
            {'excel_file': base64.encodestring(fp.getvalue()), 'file_name': filename})
        res = {
            'view_mode': 'form',
            'res_id': export_id.id,
            'name': 'Inventory Age Report',
            'res_model': 'inventory.age.report.excel',
            'view_type': 'form',
            'type': 'ir.actions.act_window',
            'target': 'new'
        }
        return res

class InventoryAgeExcel(models.TransientModel):
    _name = "inventory.age.report.excel"
    _description = "Inventory Age Excel"

    excel_file = fields.Binary('Report file ')
    file_name = fields.Char('Excel file', size=64)


a=[]
with open("Quicksort.txt") as numbers:
    for number in numbers:
        a.append(int(number))
print(len(a))
comparisions=0
def partition(a,l,h):
    global comparisions
    if l<h:
        pivot=a[h-1]
        i=l
        j=h
        while 1:
            while((a[i]<pivot)):
                i+=1
                comparisions+=1
            while((a[j]>pivot)):
                j-=1
                comparisions+=1
            if i>=j:
                return j
            a[i],a[j]=a[j],a[i]

def quicksort(a,l,h):
    if l<h:
        i=partition(a,l,h)
        quicksort(a,l,i)
        quicksort(a,i+1,h)

n=len(a)
quicksort(a,0,n-1)
print(comparisions)


from requests import get
from bs4 import BeautifulSoup

url = "https://helion.pl/search?qa=&serwisyall=&szukaj=python&wprzyg=&wsprzed=&wyczerp="

responce = get(url)

html_soup = BeautifulSoup(responce.text, 'html.parser')

books = html_soup.find_all('div', class_="book-info")

nowe = set()

for b in books:
    nowe.add(b.a.text)

# jeśli get sobie nie radzi to korzystamy z SELENIUM
# inna możliwość to scrapy


#!/usr/bin/env python3 -tt
"""
File: reassemble.py
-------------------
Assignment 2: Quest for the Holy Grail
Course: CS 41
Name: Ali Malik
SUNet: malikali

This program pieces together slices of a shredded image by comparin the
similarity between different slices and rearranging them appropriately.

"""
import imageutils

# Great job with this file!

def pixel_similarity(p1, p2):
	"""
	This method finds the similarity between two pixels by computing the sum
	of the square of each of the RGBA channels.

	@param p1,p2: Pixels that are essentially 4-tuples with the resepctive RGBA
	values of the pixel

	"""
	return sum([(a - b)**2 for a,b in zip(p1,p2)]) # Super cool

def column_similarity(rightCol, leftCol):
	"""
	This method finds the similarity between the rightmost column of one slice
	with the leftmost column of another slice

	@param rightCol, leftCol: Lists representing the pixels of the respective columns
	they represent

	"""
	return sum([pixel_similarity(x,y) for x,y in zip(rightCol, leftCol)])

def slice_similarity(slc1, slc2):
	"""
	This method finds the similarity between two image slices by comparing
	the similarity of the rightmost column of one slice with the leftmost
	column of another slice

	@param slc1, slc2: 2-d image array in column major order representing
	image slice

	"""
	# Your comments are incredible
	rightCol = slc1[-1]
	leftCol = slc2[0]
	return column_similarity(rightCol, leftCol)

def reconstruct_image(images):
	"""
	Reconstructs the image by comparing pairs of image slices and combining
	the two slices that are the most similar.

	@param images: List of 2-d image arrays representing each slice of the
	shredded image

	"""
	# A little messy, but not too much you can do
	while len(images) != 1:
		curr_slice = images[0]
		images.remove(curr_slice)
		min_sim = 2000000000
		index = 0
		for i in range(len(images)):
			next_slice = images[i]
			x = min(slice_similarity(curr_slice, next_slice), slice_similarity(next_slice, curr_slice))
			if x < min_sim:
				min_sim = x
				index = i
		next_slice = images[index]
		if slice_similarity(curr_slice, next_slice) < slice_similarity(next_slice, curr_slice):
			images[index] = curr_slice + images[index]
		else:
			images[index] = images[index] + curr_slice

if __name__ == '__main__':
	"""
	Opens file and reads it using a list comprehension into a list called images.
	After reconstructing the image, this method then displays the image and saves
	it to the hard drive.

	"""
	IMGS_DIR = 'shredded/destination'
	images = [imageutils.load_image(filename) for filename in imageutils.files_in_directory(IMGS_DIR)]
	reconstruct_image(images)
	imageutils.show_all_images(*images)
	imageutils.save_image(images[0], 'final-destination')


#/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import numpy as np
import pandas as pd
import json
import re

reload(sys)
sys.setdefaultencoding('utf-8')

def is_json(myjson):
    try:
        json.loads(myjson)
    except ValueError:
        return False
    return True

df = pd.read_csv('C:/Users/adminn/Desktop/weibo_response.csv')

new_df = pd.DataFrame()
count = 0

for i in range(df.shape[0]):
	print(df.loc[i, "id"])

	if(df.loc[i, "reposts"] != "[]"):
		js1 = json.loads(df.loc[i, "reposts"])
		for each_one in js1:
			new_str = each_one["repost_content"]
			new_str = re.sub("[A-Za-z0-9\/\(\)\!\%\[\]\<\>\=\-\_\+\:\"\'\;\.\#\@\&\*]", "", new_str)
			new_str = new_str.strip()
			each_one["repost_content"] = new_str

			temp_df = pd.DataFrame({"id":df.loc[i, "id"]}, index = [count])
			new_df = pd.concat([new_df,temp_df], sort = False)

			for key,value in each_one.items():
				if (key == "repost_time"):
					new_df.loc[count, "time"] = value
				if (key == "repost_content"):
					new_df.loc[count, "content"] = value

			count += 1

	if(df.loc[i, "comments"] != "[]"):
		js2 = json.loads(df.loc[i, "comments"])
		for each_one in js2:
			new_str = each_one["comment_content"]
			new_str = re.sub("[A-Za-z0-9\/\(\)\!\%\[\]\<\>\=\-\_\+\:\?\"\'\;\.\#\@\&\*\~]", "", new_str)
			new_str = new_str.strip()
			each_one["comment_content"] = new_str

			temp_df = pd.DataFrame({"id":df.loc[i, "id"]}, index = [count])
			new_df = pd.concat([new_df,temp_df], sort = False)

			for key,value in each_one.items():
				if (key == "comment_time"):
					new_df.loc[count, "time"] = value
				if (key == "comment_content"):
					new_df.loc[count, "content"] = value
			count += 1

new_df.to_csv("result_weibo.csv", index = 0)


"""
Copyright 2014 Sotera Defense Solutions, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

import json
from elasticsearch import Elasticsearch
import tangelo
from datawake.conf import datawakeconfig as conf

#
# Return the graph display options
#
def query(data):

    tangelo.log('domaindive:query')
    tangelo.log(data)

    # set default es options
    host = conf.get_es_host()
    port = conf.get_es_port()
    indd = conf.get_es_index()
    url = '%s:%s'%(host,port)
    max_results_per_node = conf.get_es_mrpn()
    cred = conf.get_es_cred()
    protocol = 'https'

    # override defaults if supplied with query
    if 'protocol' in data and data['protocol'] in ['http','https']:
        protocol = data['protocol']
        if protocol != 'https': cred = None
    if 'url' in data and data['url'] is not None and len(data['url']) > 0:
        url = data['url']
        cred = None  # if the url changed reset the credentials
    if 'mrpn' in data and data['mrpn'] is not None: max_results_per_node = data['mrpn']
    if 'index' in data and data['index'] is not None and len(data['index']) >0: indd = data['index']
    if 'credentials' in data and data['credentials'] is not None and len(data['credentials']) >0: cred = data['credentials']

    tangelo.log('using elastic serach instance: '+protocol+"://"+url)

    search_terms = data['search_terms']
    es = None
    if cred is not None and len(cred) > 0:
        es = Elasticsearch([protocol+'://' + cred + '@' + url])
    else:
        es = Elasticsearch([url])

    ind = indd
    rr = []
    num = 0
    for t in search_terms:
        types = {'selection','phone','email','person','organization','misc'}
        if t['type'].lower() in types:
            num_to_search = t['id']
            if t['type'] == 'selection':
                num_to_search = t['data']
            #results = es.search(index=ind,body={"size":max_results_per_node,"fields":["_index","_type","_id"],"query":{"match_phrase": {"_all": num_to_search}}})
            results = es.search(index=ind,body={"size":max_results_per_node,"query":{"match_phrase": {"_all": num_to_search}}})
            num += results['hits']['total']
            for hit in results['hits']['hits']:
                tangelo.log(hit)
                rr.append({'nid':t['id'],'search_term':num_to_search,'eid':hit['_id'],'itype':hit['_type'],'jindex':ind,'url':url,'_source':hit['_source']})

    result = dict(num=num,hits=rr)
    tangelo.log(result)
    return json.dumps(result)

def indices():
    host = conf.get_es_host()
    port = conf.get_es_port()
    url = '%s:%s'%(host,port)
    cred = conf.get_es_cred()

    protocol = 'https'
    if cred is not None and len(cred) > 0:
        es = Elasticsearch([protocol+'://' + cred + '@' + url])
    else:
        es = Elasticsearch([url])
    indices = Elasticsearch.cat.indices(h='i')
    return json.dumps([x.strip() for x in indices.split('\n')])

post_actions = {
    'query': query,
}

get_actions = {
    'indices': indices,
}

@tangelo.restful
def post(action, *args, **kwargs):
    post_data = json.loads(tangelo.request_body().read())

    def unknown(**kwargs):
        return tangelo.HTTPStatusCode(400, "invalid service call")

    return post_actions.get(action, unknown)(**post_data)

@tangelo.restful
def get(action, *args, **kwargs):
    def unknown(**kwargs):
        return tangelo.HTTPStatusCode(400, "invalid service call")

    return get_actions.get(action, unknown)(**kwargs)


import builtins
import sys
from contextlib import contextmanager
from functools import partial
from typing import List

import termcolor
from tqdm import tqdm
from tqdm.contrib import DummyTqdmFile

SEPARATION_LINE = termcolor.colored(' '.join(['-'] * 50), attrs=['dark'])

def left_aligned(str_list: List[str]) -> List[str]:
    maxlen = max(map(len, str_list), default=0)
    return [f"{s:<{maxlen}}" for s in str_list]

def format_path(path: str) -> str:
    return termcolor.colored(path, attrs=['underline'])

def format_id(id_str: str, bracket: bool = True) -> str:
    return termcolor.colored(f"[{id_str}]" if bracket else id_str, 'cyan')

def format_highlight(string: str) -> str:
    bolder = "*" + "-" * (len(string) + 2) + "*"
    return termcolor.colored(
        f"{bolder}\n| {string.upper()} |\n{bolder}",
        color='yellow',
        attrs=['bold'],
    )

def format_highlight2(string: str) -> str:
    return termcolor.colored(string, color='green')

def format_object(obj, get_attrs=False, **kwargs):
    if get_attrs:
        kwargs.update(obj.__dict__)
    kwargs_str = ', '.join(f"{k}={v}" for k, v in kwargs.items())
    return f"{obj.__class__.__name__}({kwargs_str})"

@contextmanager
def logging_block(header: str = None, bullet: bool = True):
    '''DO NOT use it with TqdmRedirector!!!'''
    _IndentPrinter.print_header(header)
    _IndentPrinter.indent()

    old_print = builtins.print
    builtins.print = partial(_IndentPrinter.print_body, bullet=bullet)

    yield

    builtins.print = old_print
    _IndentPrinter.dedent()
    _IndentPrinter.print_footer()

class _IndentPrinter:

    '''DO NOT use it with TqdmRedirector!!!'''

    level = 0
    PRINT = builtins.print
    BULLETS = ["•", "–", "*", "·"]

    @classmethod
    def indent(cls):
        if cls.level == 0:  # maybe print is redirected somewhere...
            cls.PRINT = builtins.print
        cls.level += 1

    @classmethod
    def dedent(cls):
        cls.level -= 1

    @classmethod
    def print_header(cls, header):
        if cls.level == 0:
            print(format_highlight(header))
        elif cls.level == 1:
            print(format_highlight2(header))
        else:
            print(header)

    @classmethod
    def print_body(cls, *args, bullet: bool = True, **kwargs):
        if cls.level < 2:
            return cls.PRINT(*args, **kwargs)

        if bullet:
            bullet_symbol = cls.BULLETS[min(cls.level - 1, len(cls.BULLETS) - 1)]
            cls.PRINT(' ' * (2 * cls.level - 2) + bullet_symbol, *args, **kwargs)
        else:
            cls.PRINT(' ' * (2 * cls.level - 3), *args, **kwargs)

    @classmethod
    def print_footer(cls):
        if cls.level == 0:
            print(SEPARATION_LINE)
        elif cls.level == 1:
            print()

class TqdmRedirector:

    # ports before enable() is called
    STDOUT, STDERR, PRINT = sys.stdout, sys.stderr, builtins.print

    @classmethod
    def enable(cls):
        '''DO NOT use it under logging_block!!!'''
        # ports before being redirected, maybe there're redirected somewhere...
        cls.STDOUT, cls.STDERR, cls.PRINT = sys.stdout, sys.stderr, builtins.print
        TQDMOUT, TQDMERR = DummyTqdmFile(sys.stdout), DummyTqdmFile(sys.stderr)
        STREAMS_TO_REDIRECT = {None, cls.STDOUT, cls.STDERR, TQDMOUT, TQDMERR}

        def safe_print(*values, sep=' ', end='\n', file=None, flush=False):
            if file in STREAMS_TO_REDIRECT:
                # NOTE tqdm (v4.40.0) can't support end != '\n' and flush
                tqdm.write(sep.join(map(str, values)), file=cls.STDOUT)
            else:
                cls.PRINT(*values, sep=sep, end=end, file=file, flush=flush)

        sys.stdout, sys.stderr, builtins.print = TQDMOUT, TQDMERR, safe_print

    @classmethod
    def disable(cls):
        sys.stdout, sys.stderr, builtins.print = cls.STDOUT, cls.STDERR, cls.PRINT


import semanticscholar as sch
import unittest
from requests.exceptions import Timeout

class SemanticScholarTest(unittest.TestCase):
    def test_paper(self):
        data = sch.paper('10.1093/mind/lix.236.433', timeout=5)
        self.assertEqual(data['title'],
                         'Computing Machinery and Intelligence')

        self.assertRaises(Timeout,
                          sch.paper,
                          '10.1093/mind/lix.236.433',
                          timeout=0.01)

    def test_author(self):
        data = sch.author(2262347, timeout=5)
        self.assertEqual(data['name'], 'A. Turing')

    def test_not_found(self):
        data = sch.paper(0, timeout=5)
        self.assertEqual(len(data), 0)

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


import os
pdf_files = os.listdir()

from tika import parser
for file_name in pdf_files:
# Parse data from file
    file_data = parser.from_file(file_name)
    # Get files text content
    text = file_data['content']
    print(text[999:1100])

import spacy # NLP library, with many features, including named entity recognition (NER)
nlp = spacy.load("en_core_web_sm") # function that loads the model

data_fname = 'real_data.txt'
data_file = open(data_fname,"r")
lines = data_file.readlines()
data_file.close()

print()
print("TRAIN_DATA = [")
for line in lines:
    doc = nlp(line)
    ent_list = []
    for ent in doc.ents:
        ent_list.append( (ent.start_char,ent.end_char,ent.label_) )
    info = "('" + line[:-1] + "', \n{'entities': "+str(ent_list)+"}),"
    print(info)
print("]")
print()


from model.condominium import Condominium
from model.tower import Tower
from model.apartment import Apartment
from model.service import Service
from setup import db
from sqlalchemy import exc, and_
from multiprocessing import Process
import datetime
import time

def clear_old_services():
    while datetime.datetime.now().hour != 1:
        time.sleep(30 * 60)

    while True:
        Service.query.filter(Service.arrival > datetime.datetime.today()).update({Service.active: 0})
        db.session.commit()
        time.sleep(3600 * 24 * 2)

class ServiceController:
    def __init__(self):
        Process(target=clear_old_services).start()

    def get_service_by_id(self, service_id):
        return Service.query.filter(and_(Service.id == service_id, Service.active == 1))

    def get_service_by_date(self, condominium_id, start_datetime, end_datetime):
        return Condominium.query.filter_by(id=condominium_id)\
            .join(Tower)\
            .join(Apartment)\
            .join(Service)\
            .filter(and_(Service.arrival >= start_datetime, Service.arrival <= end_datetime, Service.active == 1))\
            .with_entities(Service.id, Service.name, Service.employee, Service.arrival, Service.apartment_id).all()

    def register_service(self, name, employee, arrival, apartment_id):
        try:
            arrival = datetime.datetime.strptime(arrival, '%Y-%m-%d %H:%M')
            service = Service(name=name, employee=employee, arrival=arrival, apartment_id=apartment_id)
            db.session.add(service)
            db.session.commit()

            return service.id

        except exc.IntegrityError:
            db.session.rollback()
            return None

    def remove_service(self, service):
        try:
            db.session.delete(service)
            db.session.commit()

            return True

        except exc.IntegrityError:
            db.session.rollback()
            return False


import pulsar

from pulsar.schema import AvroSchema
from example04_simplest_schema_java_compatible.SimpleSchema import Example

client = pulsar.Client('pulsar://localhost:6650')

topicSchema = AvroSchema(Example)
print("Schema info is: " + topicSchema.schema_info().schema())

# Replace with your topic name. However, the namespace for the topic should have these schema compatibility settings:
# "schema_auto_update_compatibility_strategy" : "Full",
# "schema_compatibility_strategy" : "ALWAYS_COMPATIBLE",
# "is_allow_auto_update_schema" : true,
# "schema_validation_enforced" : true
producer = client.create_producer(topic='persistent://climate/field-service/simplest-schema', schema=topicSchema)
producer.send(Example(name="Python producer"))

client.close()

print("Successfully sent message")