#!/usr/bin/python
# AlloCine web API.

import htmlentitydefs
import os
import re
import urllib

# Module global: directory to cache result pages, empty to disable.
CACHE_DIR = ''

def OpenUrl(url):

    class UrlOpener(urllib.FancyURLopener):
        version = ('Mozilla/6.0 (Windows NT 6.2; WOW64; rv:16.0.1) '
                   'Gecko/20121011 Firefox/16.0.1')

    opener = UrlOpener()
    opener.addheader('Accept-Language', 'en-US,en')
    return opener.open(url).read()

def LoadUrl(url):
    name = re.sub('[^-.\w]', '', url[7:].strip('/').replace('/', '-'))
    cache = os.path.join(CACHE_DIR, name)
    if CACHE_DIR and os.path.isfile(cache):
        content = open(cache).read()
    else:
        content = OpenUrl(url)
        if CACHE_DIR:
            open(cache, 'w').write(content)
    return content

def Decode(s):

    def DecodeEntities(m):
        entity = htmlentitydefs.entitydefs.get(m.group(1), m.group(0))
        return unichr(ord(entity))

    s = re.sub('&#([0-9a-fA-F]+);', lambda m: unichr(int(m.group(1))), s)
    s = re.sub('&#x([0-9a-fA-F]+);', lambda m: unichr(int(m.group(1), 16)), s)
    s = re.sub('&(\w+?);', DecodeEntities, s)
    return s

class Movie(object):
    """Class to represent an AlloCine Movie with its information.

    Attributes:
        id: Integer with AlloCine movie ID.

    Properties, empty when not available:
        url: Link to AlloCine page of this movie.
        name: String with movie name.
        original_name: String with original movie name if foreign movie.
        year: Integer with movie year (production or release).
        production_year: Integer with production year.
        release_year: Integer with release year.
        duration: String with duration (e.g. 1h30m).
        directors: List of strings with directors names.
        actors: List of strings with actors names.
        genres: List of strings with genre names.
        nationalities: List of strings with movie nationalities.
        rating: Float with rating (spectators or press, between 0 and 5).
        rating_spectators: Float with spectators rating.
        rating_press: Float with press rating.
    """

    def __init__(self, allocine_id):
        """Create a new AlloCine Movie object.

        Args:
            allocine_id: Integer with AlloCine movie ID.
        """
        self.id = int(allocine_id)    # Work if a string number is supplied.
        self.__page = ''

    def __repr__(self):
        return '<AlloCine %i>' % self.id

    @property
    def url(self):
        return ('http://www.allocine.fr/film/fichefilm_gen_cfilm=%i.html'
                % self.id)

    @property
    def _page(self):
        if not self._page_loaded:
            self.__page = LoadUrl(self.url).decode('utf-8')
        return self.__page

    @property
    def _page_loaded(self):
        return bool(self.__page)

    @property
    def name(self):
        m = re.search('property="og:title" content="([^"]+)"', self._page)
        return Decode(m.group(1)) if m else ''

    @property
    def original_name(self):
        m = re.search('Titre original
	([^<]+)	', self._page)
        return Decode(m.group(1)) if m else self.name

    @property
    def year(self):
        if self.year_production:
            return self.year_production
        return self.year_release

    @property
    def year_production(self):
        m = re.search(' de production
	]*>([^<]+)', self._page)
        return int(m.group(1)) if m else None

    @property
    def year_release(self):
        m = re.search('itemprop="datePublished" content="([^-"]+)', self._page)
        return int(m.group(1)) if m else None

    @property
    def duration(self):
        m = re.search('itemprop="duration" content="(?:PT)?([0-9HM]+)"', self._page)
        return m.group(1).lower() if m else ''

    @property
    def directors(self):
        regexp = 'itemprop="director" .*? itemprop="name">([^<]+)'
        return [Decode(name) for name in re.findall(regexp, self._page)]

    @property
    def actors(self):
        m = re.search('itemprop="actors" (.*)', self._page)
        if not m:
            return []
        matches = re.findall('itemprop="name">([^<]+)', m.group(1))
        return [Decode(name) for name in matches]

    @property
    def genres(self):
        matches = re.findall('itemprop="genre">([^<]+)', self._page)
        return [Decode(name) for name in matches]

    @property
    def nationalities(self):
        m = re.search('Nationalit(.*?)
', self._page, re.S)
        if not m:
            return []
        matches = re.findall(']*>([^<]+)', m.group(1))
        return [Decode(n.strip()).capitalize() for n in matches]

    @property
    def rating(self):
        if self.rating_spectators:
            return self.rating_spectators
        return self.rating_press

    @property
    def rating_spectators(self):
        p = self._page.find('Spectateurs\n')
        q = self._page.find('
', p)
        m = re.search(']*>([0-9,]+)<', self._page[p:q])
        return float(m.group(1).replace(',', '.')) if m else None

    @property
    def rating_press(self):
        p = self._page.find('Presse\n')
        q = self._page.find('
', p)
        m = re.search(']*>([0-9,]+)<', self._page[p:q])
        return float(m.group(1).replace(',', '.')) if m else None


# coding: utf-8
# Universidade Federal De Campina Grande
# Ciência da Computação - 2018.2
# Helder Chaves Leite Junior - 118210158
# helder.junior@ccc.ufcg.edu.br
# Analytics Votação

def conta_votos(lista, id_votacao):

    votos = []
    sim = 0
    nao = 0
    for e in range(len(lista)):
        elemento = lista[e].split(",")
        if elemento[1] == str(id_votacao):
            if elemento[-1] == "sim":
                sim += 1
            elif elemento[-1] == "nao":
                nao += 1

    votos.append(sim)
    votos.append(nao)

    return votos

U6 - Exercícios/03 - Analytics Assembléia/solution.py


#!/usr/bin/python
#
# Copyright (c) 2017 All rights reserved
# This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
#
# http://www.apache.org/licenses/LICENSE-2.0
#

import argparse
from random import randint

import functest.utils.functest_logger as ft_logger
import functest.utils.openstack_utils as os_utils

from sdnvpn.lib import utils as test_utils
from sdnvpn.lib import config as sdnvpn_config
from sdnvpn.lib.results import Results

parser = argparse.ArgumentParser()

parser.add_argument("-r", "--report",
                    help="Create json result file",
                    action="store_true")

args = parser.parse_args()

logger = ft_logger.Logger("sdnvpn-testcase-1").getLogger()

COMMON_CONFIG = sdnvpn_config.CommonConfig()
TESTCASE_CONFIG = sdnvpn_config.TestcaseConfig('testcase_1')

def main():
    results = Results(COMMON_CONFIG.line_length)

    results.add_to_summary(0, "=")
    results.add_to_summary(2, "STATUS", "SUBTEST")
    results.add_to_summary(0, "=")

    nova_client = os_utils.get_nova_client()
    neutron_client = os_utils.get_neutron_client()
    glance_client = os_utils.get_glance_client()

    image_id = os_utils.create_glance_image(glance_client,
                                            TESTCASE_CONFIG.image_name,
                                            COMMON_CONFIG.image_path,
                                            disk=COMMON_CONFIG.image_format,
                                            container="bare",
                                            public='public')
    network_1_id = test_utils.create_net(neutron_client,
                                         TESTCASE_CONFIG.net_1_name)
    test_utils.create_subnet(neutron_client,
                             TESTCASE_CONFIG.subnet_1_name,
                             TESTCASE_CONFIG.subnet_1_cidr,
                             network_1_id)

    network_2_id = test_utils.create_net(neutron_client,
                                         TESTCASE_CONFIG.net_2_name)

    test_utils.create_subnet(neutron_client,
                             TESTCASE_CONFIG.subnet_2_name,
                             TESTCASE_CONFIG.subnet_2_cidr,
                             network_2_id)

    sg_id = os_utils.create_security_group_full(neutron_client,
                                                TESTCASE_CONFIG.secgroup_name,
                                                TESTCASE_CONFIG.secgroup_descr)

    compute_nodes = test_utils.assert_and_get_compute_nodes(nova_client)

    av_zone_1 = "nova:" + compute_nodes[0]
    av_zone_2 = "nova:" + compute_nodes[1]

    # boot INTANCES
    vm_2 = test_utils.create_instance(
        nova_client,
        TESTCASE_CONFIG.instance_2_name,
        image_id,
        network_1_id,
        sg_id,
        secgroup_name=TESTCASE_CONFIG.secgroup_name,
        compute_node=av_zone_1)
    vm_2_ip = test_utils.get_instance_ip(vm_2)

    vm_3 = test_utils.create_instance(
        nova_client,
        TESTCASE_CONFIG.instance_3_name,
        image_id,
        network_1_id,
        sg_id,
        secgroup_name=TESTCASE_CONFIG.secgroup_name,
        compute_node=av_zone_2)
    vm_3_ip = test_utils.get_instance_ip(vm_3)

    vm_5 = test_utils.create_instance(
        nova_client,
        TESTCASE_CONFIG.instance_5_name,
        image_id,
        network_2_id,
        sg_id,
        secgroup_name=TESTCASE_CONFIG.secgroup_name,
        compute_node=av_zone_2)
    vm_5_ip = test_utils.get_instance_ip(vm_5)

    # We boot vm5 first because we need vm5_ip for vm4 userdata
    u4 = test_utils.generate_ping_userdata([vm_5_ip])
    vm_4 = test_utils.create_instance(
        nova_client,
        TESTCASE_CONFIG.instance_4_name,
        image_id,
        network_2_id,
        sg_id,
        secgroup_name=TESTCASE_CONFIG.secgroup_name,
        compute_node=av_zone_1,
        userdata=u4)
    vm_4_ip = test_utils.get_instance_ip(vm_4)

    # We boot VM1 at the end because we need to get the IPs first to generate
    # the userdata
    u1 = test_utils.generate_ping_userdata([vm_2_ip,
                                            vm_3_ip,
                                            vm_4_ip,
                                            vm_5_ip])
    vm_1 = test_utils.create_instance(
        nova_client,
        TESTCASE_CONFIG.instance_1_name,
        image_id,
        network_1_id,
        sg_id,
        secgroup_name=TESTCASE_CONFIG.secgroup_name,
        compute_node=av_zone_1,
        userdata=u1)

    msg = ("Create VPN with eRT<>iRT")
    results.record_action(msg)
    vpn_name = "sdnvpn-" + str(randint(100000, 999999))
    kwargs = {
        "import_targets": TESTCASE_CONFIG.targets1,
        "export_targets": TESTCASE_CONFIG.targets2,
        "route_distinguishers": TESTCASE_CONFIG.route_distinguishers,
        "name": vpn_name
    }
    bgpvpn = os_utils.create_bgpvpn(neutron_client, **kwargs)
    bgpvpn_id = bgpvpn['bgpvpn']['id']
    logger.debug("VPN created details: %s" % bgpvpn)

    msg = ("Associate network '%s' to the VPN." % TESTCASE_CONFIG.net_1_name)
    results.record_action(msg)
    results.add_to_summary(0, "-")

    os_utils.create_network_association(
        neutron_client, bgpvpn_id, network_1_id)

    # Wait for VMs to get ips.
    instances_up = test_utils.wait_for_instances_up(vm_1, vm_2,
                                                    vm_3, vm_4,
                                                    vm_5)

    if not instances_up:
        logger.error("One or more instances is down")
        # TODO: Handle this appropriately

    results.get_ping_status(vm_1, vm_2, expected="PASS", timeout=200)
    results.get_ping_status(vm_1, vm_3, expected="PASS", timeout=30)
    results.get_ping_status(vm_1, vm_4, expected="FAIL", timeout=30)

    msg = ("Associate network '%s' to the VPN." % TESTCASE_CONFIG.net_2_name)
    results.add_to_summary(0, "-")
    results.record_action(msg)
    results.add_to_summary(0, "-")
    os_utils.create_network_association(
        neutron_client, bgpvpn_id, network_2_id)

    test_utils.wait_for_bgp_net_assocs(neutron_client,
                                       bgpvpn_id,
                                       network_1_id,
                                       network_2_id)

    logger.info("Waiting for the VMs to connect to each other using the"
                " updated network configuration")
    test_utils.wait_before_subtest()

    results.get_ping_status(vm_4, vm_5, expected="PASS", timeout=30)
    # TODO enable again when isolation in VPN with iRT != eRT works
    # results.get_ping_status(vm_1, vm_4, expected="FAIL", timeout=30)
    # results.get_ping_status(vm_1, vm_5, expected="FAIL", timeout=30)

    msg = ("Update VPN with eRT=iRT ...")
    results.add_to_summary(0, "-")
    results.record_action(msg)
    results.add_to_summary(0, "-")
    kwargs = {"import_targets": TESTCASE_CONFIG.targets1,
              "export_targets": TESTCASE_CONFIG.targets1,
              "name": vpn_name}
    bgpvpn = os_utils.update_bgpvpn(neutron_client, bgpvpn_id, **kwargs)

    logger.info("Waiting for the VMs to connect to each other using the"
                " updated network configuration")
    test_utils.wait_before_subtest()

    results.get_ping_status(vm_1, vm_4, expected="PASS", timeout=30)
    results.get_ping_status(vm_1, vm_5, expected="PASS", timeout=30)

    return results.compile_summary()

if __name__ == '__main__':
    main()


# Default Imports
from greyatomlib.python_getting_started.q01_read_data.build import read_data
data = read_data()

# Your Solution
def BC_runs(data):
    runs = 0
    for i in range(len(data['innings'][0]['1st innings']['deliveries'])):
        a = data['innings'][0]['1st innings']['deliveries'][i]
        for key in a.keys():
            if(a[key]['batsman']== 'BB McCullum'):
                b = a[key]['runs']['batsman']
                runs = runs+b
    return runs


'''
Constructors for the various cells.
( used for dmon config automation)
'''
import synapse.exc as s_exc
import synapse.axon as s_axon
import synapse.common as s_common
import synapse.cortex as s_cortex
import synapse.cryotank as s_cryotank

import synapse.lib.auth as s_auth
import synapse.lib.lmdblayer as s_lmdblayer
import synapse.lib.remotelayer as s_remotelayer

ctors = {
    'auth': s_auth.Auth,
    'axon': s_axon.Axon,
    'layer-lmdb': s_lmdblayer.LmdbLayer,
    'layer-remote': s_remotelayer.RemoteLayer,
    'cortex': s_cortex.Cortex,
    'blobstor': s_axon.BlobStor,
    'cryocell': s_cryotank.CryoCell,
    'cryotank': s_cryotank.CryoTank
}

def add(name, ctor):
    '''
    Add a Cell() constructor alias.

    Args:
        name (str): Name of the cell alias.
        ctor: Function used to create the Cell().

    Notes:
        Third party modules which implement ``synapse.lib.cell.Cell`` classes
        should import ``synapse.cells`` and register an alias and class path
        for their ``Cell`` using this function.  This can be done in a module
        ``__init__.py`` file.
    '''
    ctors[name] = ctor

async def init(name, dirn, *args, **kwargs):
    '''
    Initialize and return a Cell() object by alias.
    '''
    ctor = ctors.get(name)
    if ctor is None:
        raise s_exc.NoSuchName(name=name, mesg='No cell ctor by that name')

    return await ctor.anit(dirn, *args, **kwargs)

async def initFromDirn(dirn, *args, **kwargs):
    '''
    As above, but retrieves type from boot.yaml in dirn
    '''
    conf = s_common.yamlload(dirn, 'boot.yaml') or {}
    kind = conf.get('type')
    if type is None:
        raise s_exc.BadConfValu('boot.yaml missing type key')
    return await init(kind, dirn, *args, **kwargs)

def deploy(name, dirn, boot=None):
    '''
    Deploy a cell of the named type to the specified directory.
    '''
    ctor = ctors.get(name)
    if ctor is None:
        raise s_exc.NoSuchName(name=name, mesg='No cell ctor by that name')

    if boot is None:
        boot = {}

    boot['type'] = name

    # create the boot.yaml
    s_common.yamlsave(boot, dirn, 'boot.yaml')

    # Cell has a deploy class method (possibly per cell type)
    ctor.deploy(dirn)

def getCells():
    '''
    Get a list of registered cell aliases and their fully qualified paths.
    '''
    ret = []
    for alias, ctor in ctors.items():
        ret.append((alias, '.'.join([ctor.__module__, ctor.__qualname__])))
    return ret


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

######################################################################
## Implementation of apidoc-ish for actual Markdown: you're welcome.

from icecream import ic
import inspect
import os
import re
import sys
import traceback

"""
This PEP proposes adding `frungible doodads`_ to the core.
It extends PEP 9876 [#pep9876]_ via the BCA [#]_ mechanism.

See also:

  * [PEP 256](https://www.python.org/dev/peps/pep-0256/)
  * [`inspect`](https://docs.python.org/3/library/inspect.html)

Because there doesn't appear to be any other Markdown-friendly
docstring support in Python.
"""

PAT_PARAM = re.compile(r"(    \S+.*\:\n(?:\S.*\n)+)", re.MULTILINE)
PAT_NAME = re.compile(r"^\s+(.*)\:\n(.*)")
PAT_FWD_REF = re.compile(r"ForwardRef\('(.*)'\)")

def show_all_elements (module_name):
    module_obj = sys.modules[module_name]

    for name, obj in inspect.getmembers(module_obj):
        for n, o in inspect.getmembers(obj):
            print("\n", name, n, o)
            print(type(o))

def fix_fwd_refs (anno):
    """substitute the quoted forward references of module classes"""
    results = []

    if not anno:
        return None
    else:
        for term in anno.split(", "):
            for chunk in PAT_FWD_REF.split(term):
                if len(chunk) > 0:
                    results.append(chunk)

        return ", ".join(results)

def parse_method_docstring (docstring, arg_dict):
    md = []

    for chunk in PAT_PARAM.split(docstring):
        m_param = PAT_PARAM.match(chunk)

        if m_param:
            param = m_param.group()
            m_name = PAT_NAME.match(param)

            if m_name:
                name = m_name.group(1).strip()
                anno = fix_fwd_refs(arg_dict[name])
                descrip = m_name.group(2).strip()

                if name == "returns":
                    md.append("\n  * *{}* : `{}`  \n{}".format(name, anno, descrip))
                elif name == "yields":
                    md.append("\n  * *{}* :  \n{}".format(name, descrip))
                else:
                    md.append("\n  * `{}` : `{}`  \n{}".format(name, anno, descrip))
        else:
            chunk = chunk.strip()

            if len(chunk) > 0:
                md.append(chunk)

    return "\n".join(md)

def extract_type_annotation (sig):
    type_name = str(sig)
    type_class = sig.__class__.__module__

    try:
        if type_class != "typing":
            if type_name.startswith("<class"):
                type_name = type_name.split("'")[1]

        if type_name == "~AnyStr":
            type_name = "typing.AnyStr"
        elif type_name.startswith("~"):
            type_name = type_name[1:]

    except Exception:
        ic(type_name)
        traceback.print_exc()
    finally:
        return type_name

def get_arg_list (sig):
    arg_list = []

    for param in sig.parameters.values():
        #print(param.name, param.empty, param.default, param.annotation, param.kind)

        if param.name == "self":
            pass
        else:
            if param.kind == inspect.Parameter.VAR_POSITIONAL:
                name = "*{}".format(param.name)
            elif param.kind == inspect.Parameter.VAR_KEYWORD:
                name = "**{}".format(param.name)
            elif param.default == inspect.Parameter.empty:
                name = param.name
            else:
                if isinstance(param.default, str):
                    default_repr = repr(param.default).replace("'", '"')
                else:
                    default_repr = param.default

                name = "{}={}".format(param.name, default_repr)

            anno = extract_type_annotation(param.annotation)
            arg_list.append((name, anno))

    return arg_list

def append_doc (md, obj, parse=False, arg_dict={}):
    doc = obj.__doc__

    if doc:
        docstring = inspect.cleandoc(doc)

        if parse:
            md.append(parse_method_docstring(docstring, arg_dict))
        else:
            md.append(docstring)

        md.append("\n")

def document_method (path_list, name, obj, func_kind, gh_src_url):
    md = ["---"]

    # format a header + anchor
    frag = ".".join(path_list + [ name ])
    anchor = "#### [`{}` {}](#{})".format(name, func_kind, frag)
    md.append(anchor)

    # link to source code in Git repo
    code = obj.__code__
    line_num = code.co_firstlineno
    file = code.co_filename.replace(os.getcwd(), "")
    src_url = "[*\[source\]*]({}{}#L{})\n".format(gh_src_url, file, line_num)
    md.append(src_url)

    # format the callable signature
    sig = inspect.signature(obj)
    arg_list = get_arg_list(sig)
    arg_list_str = "{}".format(", ".join([ a[0] for a in arg_list ]))

    md.append("```python")
    md.append("{}({})".format(name, arg_list_str))
    md.append("```")

    # include the docstring, with return annotation
    arg_dict = dict([ (name.split("=")[0], anno,) for name, anno in arg_list ])
    arg_dict["yields"] = None

    ret = sig.return_annotation

    if ret:
        arg_dict["returns"] = extract_type_annotation(ret)

    append_doc(md, obj, parse=True, arg_dict=arg_dict)
    md.append("")

    return line_num, md

def document_type (path_list, name, obj):
    md = []

    # format a header + anchor
    frag = ".".join(path_list + [ name ])
    anchor = "#### [`{}` {}](#{})".format(name, "type", frag)
    md.append(anchor)

    # show type definition
    md.append("```python")
    md.append("{} = {}".format(name, obj))
    md.append("```")
    md.append("")

    return md

######################################################################
## top-level methods

def find_classes (module_obj, class_list):
    """walk the module tree to find class definitions"""
    todo_list = {}

    for class_name, class_obj in inspect.getmembers(module_obj, inspect.isclass):
        if class_name in class_list:
            todo_list[class_name] = class_obj

    return todo_list

def format_class (md, module_name, todo_list, class_name):
    """format markdown to describe the given class"""
    class_obj = todo_list[class_name]
    md.append("## [`{}` class](#{})".format(class_name, class_name))

    doc = class_obj.__doc__

    if doc:
        md.append(doc)

    obj_md_pos = {}

    for member_name, member_obj in inspect.getmembers(class_obj):
        path_list = [module_name, class_name]

        if member_name.startswith("__") or not member_name.startswith("_"):
            if member_name not in class_obj.__dict__:
                # inherited method
                continue
            elif inspect.isfunction(member_obj):
                func_kind = "method"
            elif inspect.ismethod(member_obj):
                func_kind = "classmethod"
            else:
                continue

            line_num, obj_md = document_method(path_list, member_name, member_obj, func_kind, gh_src_url)
            obj_md_pos[line_num] = obj_md

    for pos, obj_md in sorted(obj_md_pos.items()):
        md.extend(obj_md)

def format_functions (md, module_name, module_obj, gh_src_url):
    """walk the module tree for each function definition"""
    md.append("---")
    md.append("## [module functions](#{})".format(module_name, "functions"))

    for func_name, func_obj in inspect.getmembers(module_obj, inspect.isfunction):
        if not func_name.startswith("_"):
            line_num, obj_md = document_method([module_name], func_name, func_obj, "function", gh_src_url)
            md.extend(obj_md)

def format_types (md, module_name, module_obj):
    """walk the list of types in the module"""
    md.append("---")
    md.append("## [module types](#{})".format(module_name, "types"))

    for name, obj in inspect.getmembers(module_obj):
        if obj.__class__.__module__ == "typing":
            if not str(obj).startswith("~"):
                obj_md = document_type([module_name], name, obj)
                md.extend(obj_md)

def write_markdown (filename):
    """output the apidocs markdown"""
    with open(filename, "w") as f:
        for line in md:
            f.write(line)
            f.write("\n")

######################################################################
## main entry point

if __name__ == "__main__":
    ## customize the following configuration, per module
    class_list = [
        "KnowledgeGraph",
        "Subgraph",
        "SubgraphMatrix",
        "SubgraphTensor",
        "Measure",
        "Simplex0",
        "Simplex1",
        "PSLModel",
        ]

    ## NB: `inspect` is picky about paths and current working directory
    ## this only works if run from the top-level directory for the repo
    sys.path.insert(0, "../")
    import kglab

    module_name = "kglab"
    module_obj = sys.modules[module_name]
    gh_src_url = "https://github.com/DerwenAI/kglab/blob/main"

    ## NB: uncomment to analyze/troubleshoot the results of `inspect`
    #show_all_elements(module_name)
    #sys.exit(0)

    # markdown for top-level module description
    md = [ "# Reference: `{}` package".format(module_name) ]
    append_doc(md, module_obj)

    # find and format the class definitions
    todo_list = find_classes(module_obj, class_list)

    for class_name in class_list:
        format_class(md, module_name, todo_list, class_name)

    ## format the function definitions and types
    format_functions(md, module_name, module_obj, gh_src_url)
    format_types(md, module_name, module_obj)

    ## output the apidocs markdown
    ref_md_file = sys.argv[1]
    print("writing: ", ref_md_file)
    write_markdown(ref_md_file)


import cleansingAuthors, re

class Author:
    def __init__(self, publications, keywords, fullname='', src=None):
        if fullname != '':
            if re.compile(r'\s\s+').search(fullname):
                fullname = re.compile(r'\s\s+').sub(' ', fullname).strip()
            match = re.compile(r'([A-Z])\s([a-z])').search(fullname)
            if match:
                fullname = re.compile(r'([A-Z])\s([a-z])').sub(match.group(1)+match.group(2), fullname)
            fullname = cleansingAuthors.denoising_name(fullname)

        self.fullname = fullname
        self.name_chk_key = self.fullname.replace(' ', '').lower()
        self.name_variants = []

        self.wos_auid = None
        self.scp_auid = None
        self.numofmiddlename = Author.how_many_middle_name(self.fullname)

        self.src = src
        self.firstname = None
        self.lastname = None
        self.email = None

        self.affiliations = []
        if str(keywords) == 'None' or str(keywords) == 'nan':
            keywords = []
        self.keywords = keywords
        self.publications = publications

    def update_au(self, a,s,w,i):
        if i:
            self.update_info(i, 'ieee')
        if w:
            self.update_info(w, 'wos')
        if a:
            self.update_info(a, 'axv')
        if s:
            self.update_info(s, 'scp')

    def update_info(self, x, src):
        if x.get('daisng_id'):
            self.wos_auid = str(x['daisng_id'])
        if x.get('au_id'):
            self.scp_auid = str(x['au_id'])
        if x.get('firstname'):
            self.firstname = x['firstname']
        if x.get('lastname'):
            self.lastname = x['lastname']
        if x.get('email'):
            self.email = x['email']
        if x.get('affiliation'):
            self.affiliations.append(x['affiliation'])

    def to_dict(self):
        if self.wos_auid:
            self.wos_auid = str(self.wos_auid)
        if self.scp_auid:
            self.scp_auid = str(self.scp_auid)
        if len(self.affiliations) == 0:
            self.affiliations = None
        if len(self.name_variants) == 0:
            self.name_variants = None

        dict_ = { 'keywords' : dict(), 'email' : self.email, 'name_chk_key': self.name_chk_key,
            'affiliations' : self.affiliations, 'publications' : self.publications,
            'firstname' : self.firstname, 'lastname' : self.lastname, 'fullname' :self.fullname,
            'wos_auid' : self.wos_auid, 'scp_auid' : self.scp_auid, 'numofmiddlename' : self.numofmiddlename,
            'name_variants' : self.name_variants, 'src' : self.src
           }
        for k in self.keywords:
            dict_['keywords'][k] = 1
        return dict_

    @classmethod
    def from_dict(cls, dict_):
        au = Author(dict_['publications'], dict_['keywords'], dict_['fullname'], dict_['src'])
        au.affiliations = dict_['affiliations']
        au.name_chk_key = dict_['name_chk_key']
        au.email = dict_['email']
        au.lastname = dict_['lastname']
        au.firstname = dict_['firstname']
        au.scp_auid = dict_['scp_auid']
        au.numofmiddlename = dict_['numofmiddlename']
        au.wos_auid = dict_['wos_auid']
        au.name_variants = dict_['name_variants']

        return au

    @classmethod
    def how_many_middle_name(cls, x):
        if type(x) != str:
            print("hmmn, x is not str", x)
            return 0
        if x =='': return 0
        match = x.strip().split(' ')
        return len(match) - 2 if len(match) >= 2 else 0


data = open(r'C:\Users\vkumar15\Desktop\Python Session -1.txt','r')
#print(data.read())

#print(data.readline())
d = data.readlines()

#print(d[10])
rc = 0
wc = 0

for l in d:
     rc=rc+1
     col = l.split(' ')
     for w in col:
          print(w)

          if w.replace('?','')=='Python':
               wc=wc+1

print('row count : ',rc)
print('python word count : ',wc)


# -*- coding: utf-8 -*-
import json

from utilities.exceptions import NoConfigFile

def read_config(path):
    try:
        with open(path, "r", encoding="utf-8") as f:
            return json.loads(f.read())
    except FileNotFoundError:
        raise NoConfigFile("No config file found!"
                           "Check it please. Doesn't work without it.")


from xai.brain.wordbase.nouns._soil import _SOIL

#calss header
class _SOILING(_SOIL, ):
	def __init__(self,):
		_SOIL.__init__(self)
		self.name = "SOILING"
		self.specie = 'nouns'
		self.basic = "soil"
		self.jsondata = {}


from django.contrib import admin
from django.forms import TextInput, Textarea
from django.db import models

from performing.models import Performing

class PerformingAdmin(admin.ModelAdmin):
    search_fields = ['sn__number']
    list_filter = ['operation','resource_name']
    list_display = ('sn','operation','result','interval','resource_name','start_time','stop_time','duration')
    # list_editable = ('color','move_performa')
    readonly_fields = ('uid','created_date','user')
    autocomplete_fields = []
    # save_as = True
    # save_as_continue = True
    # save_on_top =True

    fieldsets = [
        ('Basic Information',{'fields': ['sn','result']}),
        ('Transaction Information',{'fields': ['operation','interval',
        		'resource_name',('start_time','stop_time'),'remark']}),
        ('System Information',{'fields':[('user','created_date')]})
        # 'perform_year','perform_month','perform_day','perform_hour'
    ]
    # resource_class      = ProductResource

    def save_model(self, request, obj, form, change):
        obj.user = request.user
        super(PerformingAdmin, self).save_model(request, obj, form, change)

admin.site.register(Performing,PerformingAdmin)


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

"""
Simple Python API client for NSoT REST API.

The easiest way to get a client is to call ``get_api_client()`` with no
arguments. This will read the user's ``~/.pynsotrc`` file and pass the values
to the client constructor::

    >>> from pynsot.client import get_api_client
    >>> api = get_api_client()
    >>> api
    AuthTokenClient(url=http://localhost:8990/api)>
"""

__author__ = 'Jathan McCollum'
__maintainer__ = 'Jathan McCollum'
__email__ = 'jathan@dropbox.com'
__copyright__ = 'Copyright (c) 2015 Dropbox, Inc.'

import getpass
import json
import logging
import os

from .vendor import click
from .vendor.requests.auth import AuthBase
from .vendor import slumber
from .vendor.slumber.exceptions import HttpClientError

from . import dotfile

# Logger
log = logging.getLogger(__name__)

# Header used for passthrough authentication.
AUTH_HEADER = 'X-NSoT-Email'

__all__ = (
    'ClientError', 'LoginFailed', 'BaseClient', 'EmailHeaderAuthentication',
    'EmailHeaderClient', 'AuthTokenAuthentication', 'AuthTokenClient',
    'get_auth_client_info', 'get_api_client'
)

class ClientError(HttpClientError):
    """Generic client error."""

class LoginFailed(ClientError):
    """Raised when login fails for some reason."""

class BaseClient(slumber.API):
    """
    Magic REST API client for NSoT.
    """
    def __init__(self, base_url=None, **kwargs):
        auth = kwargs.pop('auth', None)  # Ditch default auth
        self.default_site = kwargs.pop('default_site', None)  # Default site_id

        # Override the auth method if we have defined .get_auth()
        if auth is None:
            # Set these as object attributes so that they can be mutated in the
            # subclass .get_auth() method
            self._kwargs = kwargs
            auth = self.get_auth(base_url)

        kwargs['auth'] = auth
        kwargs['append_slash'] = True  # Append slashes!
        super(BaseClient, self).__init__(base_url, **kwargs)

    def get_auth(self, base_url=None):
        """
        Subclasses should references kwargs from ``self._kwargs``.

        :param base_url:
            Base API URL
        """
        raise NotImplementedError('SUBCLASS ME OR SOMETHING!')

    def error(self, exc):
        """
        Take errors and make them human-readable.

        :param exc:
            Exception instance
        """
        log.debug('Processing error: %r' % (exc,))
        # If it's a HTTP response, format the JSON
        try:
            try:
                err = exc.response.json()['error']
            except ValueError:
                # This is probably a JSON decoding error
                msg = exc.message
            else:
                msg = '%s %s' % (err['code'], err['message'])
        except AttributeError:
            msg = str(exc)
        raise ClientError(msg)

    def get_resource(self, resource_name):
        """
        Return a single resource object.

        :param resource_name:
            Name of resource
        """
        return getattr(self, resource_name)

    def __repr__(self):
        cls_name = self.__class__.__name__
        return '<%s(url=%s)>' % (cls_name, self._store['base_url'])

class EmailHeaderAuthentication(AuthBase):
    """Special authentication that sets the email auth header."""
    def __init__(self, email=None, auth_header=AUTH_HEADER):
        if email is None:
            raise LoginFailed('You must provide an email!')
        self.email = email
        self.auth_header = auth_header

    def __call__(self, r):
        """Set the auth header."""
        r.headers[self.auth_header] = self.email
        return r

class EmailHeaderClient(BaseClient):
    """Default client using email auth header method."""
    @classmethod
    def get_user(cls):
        """Get the local username, or if root, the sudo username."""
        user = getpass.getuser()
        if user == 'root':
            user = os.getenv('SUDO_USER')
        return user

    def get_auth(self, base_url):
        kwargs = self._kwargs
        email = kwargs.pop('email', None)
        default_domain = kwargs.pop('default_domain', 'localhost')

        if email is None and default_domain:
            log.debug('No email provided; Using default_domain: %r',
                      default_domain)
            user = self.get_user()
            email = '%s@%s' % (user, default_domain)
            log.debug('Using email: %r', email)

        auth_header = kwargs.pop('auth_header', AUTH_HEADER)
        return EmailHeaderAuthentication(email, auth_header)

class AuthTokenAuthentication(AuthBase):
    """
    Special authentication that utilizes auth_tokens.

    Adds header for "Authorization: ApiToken {email}:{auth_token}"
    """
    def __init__(self, email=None, auth_token=None):
        self.email = email
        self.auth_token = auth_token

    def __call__(self, r):
        header = 'AuthToken %s:%s' % (self.email, self.auth_token)
        r.headers['Authorization'] = header
        return r

class AuthTokenClient(BaseClient):
    """Client that uses auth_token method."""
    def get_token(self, base_url, email, secret_key):
        """
        Currently ghetto: Hit the API to get an auth_token.

        :param base_url:
            API URL

        :param email:
            User's email

        :param secret_key:
            User's secret_key
        """
        data = {'email': email, 'secret_key': secret_key}
        debug_data = data.copy()  # For debug display
        debug_data['secret_key'] = 'X' * 8

        log.debug('Getting token for user data: %r' % (debug_data,))
        try:
            url = base_url + '/authenticate/'
            headers = {'content-type': 'application/json'}
            r = slumber.requests.post(url, data=json.dumps(data),
                                      headers=headers)
        except Exception as err:
            log.debug('Got error: %s' % (err,))
            self.error(err)

        if r.ok:
            log.debug('Got response: %r' % (r,))
            return r.json()['data']['auth_token']
        else:
            msg = 'Failed to fetch auth_token from %s' % base_url
            err = HttpClientError(msg, response=r, content=r.content)
            self.error(err)

    def get_auth(self, base_url):
        kwargs = self._kwargs
        email = kwargs.pop('email', None)
        secret_key = kwargs.pop('secret_key', None)
        auth_token = self.get_token(base_url, email, secret_key)
        auth = AuthTokenAuthentication(email, auth_token)
        return auth
Client = AuthTokenClient  # Default client is auth_token

# Mapping to our two (2) hard-coded auth methods and their required arguments.
AUTH_CLIENTS = {
    'auth_header': (EmailHeaderClient, ('email', 'default_domain', 'default_site')),
    'auth_token': (AuthTokenClient, ('email', 'secret_key', 'default_site')),
}

def get_auth_client_info(auth_method):
    """
    Return the proper Client class and required args.

    :param auth_method:
        Auth method used by the client
    """
    return AUTH_CLIENTS[auth_method]

def get_api_client(auth_method=None, url=None, extra_args=None):
    """
    Safely create an API client so that users don't see tracebacks.

    Any arguments taht aren't explicitly passed will be replaced by the
    contents of the user's dotfile.

    :param auth_method:
        Auth method used by the client

    :param url:
        API URL
    """
    if extra_args is None:
        extra_args = {}

    # Read the dotfile
    try:
        log.debug('Reading dotfile.')
        client_args = dotfile.Dotfile().read()
    except dotfile.DotfileError as err:
        raise click.UsageError(err.message)

    # Merge the extra_args w/ the client_args from the config
    client_args.update(extra_args)

    # Minimum required arguments that we don't want getting passed to the client
    if auth_method is None:
        auth_method = client_args.pop('auth_method')
    if url is None:
        url = client_args.pop('url')

    # Validate the auth_method
    log.debug('Validating auth_method: %s', auth_method)
    try:
        client_class, arg_names = get_auth_client_info(auth_method)
    except KeyError:
        raise click.UsageError('Invalid auth_method: %s' % (auth_method,))

    try:
        api_client = client_class(url, **client_args)
    except ClientError as err:
        msg = str(err)
        if 'Connection refused' in msg:
            msg = 'Could not connect to server: %s' % (url,)
        raise click.UsageError(msg)
    return api_client


"""
Allowable units
"""

# Standard library modules.
import re

# Third party modules.

# Local modules.

# Globals and constants variables.

_SI_UNITS = frozenset(['m',
                       'g', # should kg, but causes problem with prefixes
                       's',
                       'A',
                       'K',
                       'mol',
                       'Cd'])

_SI_DERIVED_UNITS = frozenset([u'\u00c5', # Angstrom
                               'Bq',
                               'C',
                               'Da', # March 2014: Changed symbols for atomic mass unit from 'u' to 'Da' (Dalton), to avoid conflict with use of 'u' for micro prefix.
                               u'\u00b0\u0043', # degC
                               'degreesC', # March 2014: Changed preference to ASCII over Unicode characters
                               'F',
                               'Gy',
                               'H',
                               'Hz',
                               'J',
                               'L',
                               'lm',
                               'lx'
                               'N',
                               u'\u03a9', # ohm
                               'Ohm', # March 2014: Changed preference to ASCII over Unicode characters
                               'Pa',
                               'rad',
                               'S',
                               'Sv',
                               'sr',
                               'T',
                               'V',
                               'W',
                               'Wb',
                               ])

_NON_SI_UNITS = frozenset([u'\u00b0', # deg,
                           'degrees', # March 2014: Changed preference to ASCII over Unicode characters
                           'atoms',
                           'counts',
                           'counts/s', # March 2014: 'c/s' and 'cps' was explicitly forbidden.
                           'eV',
                           '%',
                           'mol%',
                           'vol%',
                           'wt%',
                           'mol_ppm',
                           'vol_ppm',
                           'wt_ppm',
                           'mol_ppb',
                           'vol_ppb',
                           'wt_ppb',
                           'cps/nA'])

_UNITS = _SI_UNITS | _SI_DERIVED_UNITS | _NON_SI_UNITS

_PREFIXES = frozenset(['Y', 'Z', 'E', 'P', 'T', 'G', 'M', 'k',
                      'm', u'\u00b5', 'u', 'n', 'p', 'f', 'a', 'z', 'y'])

_PREFIXES_VALUES = {'Y': 1e24, 'Z': 1e21, 'E': 1e18, 'P': 1e15, 'T': 1e12,
                    'G': 1e9, 'M': 1e6, 'k': 1e3, 'm': 1e-3, u'\u00b5': 1e-6,
                    'u': 1e-6, 'n': 1e-9, 'p': 1e-12, 'f': 1e-15, 'a': 1e-18,
                    'z': 1e-21, 'y': 1e-24}

_PATTERN = r'\A'
_PATTERN += '(?P<prefix>' + '|'.join(_PREFIXES) + ')?'
_PATTERN += '(?P<unit>' + '|'.join(_UNITS) + ')'
_PATTERN += '(?P<exponent>[+-]?\d+)?'
_PATTERN += r'\Z'

_RE_PATTERN = re.compile(_PATTERN, re.UNICODE)

def parse_unit(unit):
    match = _RE_PATTERN.match(unit)
    if match is None:
        raise ValueError('Invalid unit: %s' % unit)

    prefix = match.group('prefix')
    baseunit = match.group('unit')
    exponent = int(match.group('exponent') or 1)

    # Apply ASCII preference over Unicode characters
    if prefix is not None:
        prefix = prefix.replace(u'\u00b5', 'u')
    baseunit = baseunit.replace(u'\u00b0\u0043', 'degreesC')
    baseunit = baseunit.replace(u'\u03a9', 'Ohm')
    baseunit = baseunit.replace(u'\u00b0', 'degrees')

    return prefix, baseunit, exponent

def validate_unit(unit):
    prefix, baseunit, exponent = parse_unit(unit)

    # Special cases
    if baseunit == 'g' and prefix in ['Y', 'Z', 'E', 'P', 'T', 'G', 'M']:
        raise ValueError('Use kg to express mass greater than 1 kg')
    if baseunit == 's' and prefix in ['Y', 'Z', 'E', 'P', 'T', 'G', 'M', 'k']:
        raise ValueError('Use s to express time greater than 1 s')
    if baseunit == u'\u00c5' and prefix is not None:
        raise ValueError(u'Prefix are not allowed for \u00c5')

    return ''.join([prefix or '', baseunit, str(exponent) if exponent != 1 else ''])


import logging
from pyAitu import executor, Bot, Dispatcher
from pyAitu.models import Message, Options, Form, Header, FormClosed, ValidationRule, Submit, FormAction
from pyAitu.models.form.content.date_picker import DatePicker

API_TOKEN = 'YOUR_API_TOKEN'

bot = Bot(token=API_TOKEN)
dp = Dispatcher(bot)

logging.basicConfig(level=logging.INFO)

@dp.message_handler()
async def send_ui(message: Message):
    header = Header(
        _type="toolbar",
        title="Title",
        options=Options(
            closeable=True
        )
    )
    date_picker = DatePicker(
        content_id="date_id_1",
        title="Date title",
        selected_date="01-01-2019",
        options=Options(
            min_date="01-01-2018",
            max_date="01-01-2030"
        ),
        validations_rules=[ValidationRule(type="required", value="true", error="Это поле обязательно для заполнения")]
    )
    submit = Submit(
        content_id="submit_id",
        title="Send",
        form_action=FormAction(
            action="submit_form"
        )
    )
    form = Form(_id="date_picker_form", header=header, content=[date_picker, submit], options=Options(fullscreen=True))
    await bot.send_form(message.chat.id, form=form)

@dp.form_closed_handler()
async def get_form_closed(fc: FormClosed):
    await bot.send_message(fc.chat.id, "form closed")

if __name__ == '__main__':
    executor.start_polling(dp)


from PyQt5.QtCore import QObject, pyqtSlot, QTimer, QTime, QModelIndex
from datetime import datetime
from models.alarm_model import AlarmModel
from views.add_alarm_view import AddAlarm
from alarm_checker import AlarmChecker
from json_storage import JsonStorage
from weather_forecaster import WeatherForecaster

class ClockController(QObject):
    def __init__(self, model, forecast):
        super().__init__()

        self._model = model
        self._forecast = forecast
        self.timer = QTimer()
        self.json_storage = JsonStorage("")
        self.load_alarms()
        self.timer.timeout.connect(self.clock)
        self.timer.start(1000)
        self.checker = AlarmChecker(self._model.alarms)
        self.weather = WeatherForecaster(self._model, self._forecast)
        print(self._model.temp)

    @pyqtSlot(int)
    def add_alarm(self):
        new_alarm = AlarmModel(self._model.name, self._model.hour, self._model.minute)
        self._model.alarms = new_alarm
        self.json_storage.save(self._model.alarms)

    @pyqtSlot(int)
    def name_changed(self, value):
        self._model.name = value

    @pyqtSlot(QTime)
    def alarm_time_changed(self, value):
        self._model.hour = value.hour()
        self._model.minute = value.minute()

    @pyqtSlot(str)
    def change_time(self, value):
        self._model.time = value

    @pyqtSlot(str)
    def change_date(self, value):
        self._model.date = value

    @pyqtSlot(QModelIndex)
    def selection_changed(self, value):
        name = self._model.alarms[value.data()].name
        ret = AddAlarm(alarm_clock=self._model.alarms[name])
        res = ret.exec()
        if ret.delete:
            self._model.alarms.pop(name)
            for item in self._model.alarm_list.findItems(name):
                self._model.alarm_list.removeRow(item.row())
            self.json_storage.save(self._model.alarms)
        if res != 0:
            self.set_alarm_model(ret.alarm)

    @pyqtSlot()
    def show_add_alarm_dialog(self):
        ret = AddAlarm()
        if ret.exec():
            self.set_alarm_model(ret.alarm)

    def load_alarms(self):
        alarm_list = self.json_storage.read()
        for alarm in alarm_list:
            self._model.alarms = alarm

    def set_alarm_model(self, alarm):
        self._model.alarms = alarm
        self.json_storage.save(self._model.alarms)

    def clock(self):
        self._model.time = datetime.now().strftime('%I:%M:%S %p')
        self._model.date = datetime.now().strftime('%m-%d-%Y')


#coding:utf-8
from numpy import *
import pandas as pd

def cutsearch(str):
    import jieba
    seg_list=jieba.cut(str)
    return seg_list

def loaduselesswords():   #把停用词读取到字典里，一是因为字典查找效率高，而是因为字典不会（破坏原来的中文字符串）
    useless={}
    f = open('useless.txt', 'r')
    # f = codecs.open('test.txt', encoding='UTF-8')
    for word in f.readlines():
        word.decode(encoding='utf-8').encode(encoding='utf-8')
        word = word.strip()
        useless[word]=1
        # print word
        # print repr(word)
    f.close()
    return useless

def loadTrainData(train_sum,test_sum):#train_sum为训练集的大小,test_sum是测试集的大小，两个集是从2W中连续取的
    j=0
    f=open('user_tag_query.2W.TRAIN')#there is 20000 records
    train_fullwords=[]
    train_searchList=[] #存储训练集里所有出现的词，每个用户为一行
    train_ageList=[] #得到的标签，每个元素是整型元素，而不是字符串
    train_GenderList=[]#记录性别标签
    train_EducationList=[]#记录学历标签

    test_searchList=[]
    test_ageList=[]
    test_GenderList=[]
    test_EducationList=[]
    for user in f.readlines():
        if j<train_sum:
            userdata=user.decode(encoding='gb18030').encode(encoding='utf-8')
            # userdata=userdata.encode(encoding='utf-8')
            data=userdata.strip().split('\t')
            train_ageList.append(data[1])
            train_GenderList.append(data[2])
            train_EducationList.append(data[3])
            singlesearch = []
            for i in range(4,len(data)):
                singlesearch.extend(cutsearch(data[i]))
            train_searchList.append(singlesearch)
            train_fullwords.extend(singlesearch)

        else:
            userdata = user.decode(encoding='gb18030').encode(encoding='utf-8')
            data = userdata.strip().split('\t')
            test_ageList.append(data[1])
            test_GenderList.append(data[2])
            test_EducationList.append(data[3])
            singlesearch = []
            for i in range(4, len(data)):
                singlesearch.extend(cutsearch(data[i]))
            test_searchList.append(singlesearch)

        j += 1  # 不要忘记自增
        if j == train_sum + test_sum: break
    f.close()
    return train_fullwords,train_searchList,train_ageList,train_GenderList,train_EducationList,\
             test_searchList,test_ageList,test_GenderList,test_EducationList

def loadTrainData_(train_sum,test_sum):#train_sum为训练集的大小,test_sum是测试集的大小，两个集是从2W中连续取的
    j=0
    f=open('user_tag_query.2W.TRAIN')#there is 20000 records
    train_fullwords = []
    train_searchList=[] #存储训练集里所有出现的词，每个用户为一行
    train_ageList=[] #得到的标签，每个元素是整型元素，而不是字符串
    train_GenderList=[]#记录性别标签
    train_EducationList=[]#记录学历标签

    test_searchList=[]
    test_ageList=[]
    test_GenderList=[]
    test_EducationList=[]
    for user in f.readlines():
        if j<test_sum:
            userdata=user.decode(encoding='gb18030').encode(encoding='utf-8')
            # userdata=userdata.encode(encoding='utf-8')
            data=userdata.strip().split('\t')
            train_ageList.append(data[1])
            train_GenderList.append(data[2])
            train_EducationList.append(data[3])
            singlesearch = []
            for i in range(4,len(data)):
                singlesearch.extend(cutsearch(data[i]))
            train_searchList.append(singlesearch)
            train_fullwords.extend(singlesearch)

            test_ageList.append(data[1])
            test_GenderList.append(data[2])
            test_EducationList.append(data[3])
            singlesearch = []
            for i in range(4, len(data)):
                singlesearch.extend(cutsearch(data[i]))
            test_searchList.append(singlesearch)

        else:
            userdata = user.decode(encoding='gb18030').encode(encoding='utf-8')
            data = userdata.strip().split('\t')
            train_ageList.append(data[1])
            train_GenderList.append(data[2])
            train_EducationList.append(data[3])
            singlesearch = []
            for i in range(4, len(data)):
                singlesearch.extend(cutsearch(data[i]))
            train_searchList.append(singlesearch)
            train_fullwords.extend(singlesearch)

        j += 1  # 不要忘记自增
        if j == train_sum: break
    f.close()
    return train_fullwords,train_searchList,train_ageList,train_GenderList,train_EducationList,\
             test_searchList,test_ageList,test_GenderList,test_EducationList

def loadTestData():
    f = open('user_tag_query.2W.TEST')  # there is 20000 records
    Test_Id=[]
    Test_Search=[]
    # j=0
    for user in f.readlines():
        userdata=user.decode(encoding='gb18030').encode(encoding='utf-8')
        data=userdata.strip().split('\t')
        Test_Id.append(data[0])
        singlesearch = []
        for i in range(1,len(data)):
            singlesearch.extend(cutsearch(data[i]))
        Test_Search.append(singlesearch)
        # if j==200:break
        # j+=1
    return Test_Id,Test_Search

def count_IntofList(target,list):#计算标签列表中出现target标签的次数，target是字符形式的
    i=0
    for k in list:
        if k==target:i+=1
    return i

def trainNB_Gender(trainMatrix,trainCategory):#朴素贝叶斯分类器训练函数
    numTrainSearcher=len(trainMatrix)#一共多少个搜索者
    numWords=len(trainMatrix[0])#一个搜索者所拥有的词向量长度
    p0=count_IntofList('0',trainCategory)/float(numTrainSearcher)#计算标签中各种性别的比例
    p1=count_IntofList('1',trainCategory)/float(numTrainSearcher)
    p2=count_IntofList('2',trainCategory)/float(numTrainSearcher)
    p0Num=ones(numWords);p1Num=ones(numWords);p2Num=ones(numWords) #词计数向量，向量的大小与词集的大小相等
    for i in range(numWords):
        p0Num[i]=0.01
        p1Num[i]=0.01
        p2Num[i]=0.01
    # print 'p0Num',p0Num
    p0Denom=1;p1Denom=1;p2Denom=1   #标签计算变量，记录各种标签的总数目
    for i in range(numTrainSearcher):
        if trainCategory[i]=='0':
            p0Num+=trainMatrix[i]
            p0Denom+=sum(trainMatrix[i])
        elif trainCategory[i]=='1':
            p1Num+=trainMatrix[i]
            p1Denom+=sum(trainMatrix[i])
        else:
            p2Num+=trainMatrix[i]
            p2Denom+=sum(trainMatrix[i])
    p0Vect=log(p0Num/float(p0Denom))
    p1Vect=log(p1Num/float(p1Denom))
    p2Vect=log(p2Num/float(p2Denom))
    return p0Vect,p1Vect,p2Vect,p0,p1,p2

def trainNB_Age(trainMatrix,trainCategory):
    numTrainSearcher = len(trainMatrix)  # 一共多少个搜索者
    numWords = len(trainMatrix[0])  # 一个搜索者所拥有的词向量长度,即词集的大小
    p0 = count_IntofList('0', trainCategory) / float(numTrainSearcher)  # 计算标签中各种性别的比例
    p1 = count_IntofList('1', trainCategory) / float(numTrainSearcher)
    p2 = count_IntofList('2', trainCategory) / float(numTrainSearcher)
    p3 = count_IntofList('3', trainCategory) / float(numTrainSearcher)
    p4 = count_IntofList('4', trainCategory) / float(numTrainSearcher)
    p5 = count_IntofList('5', trainCategory) / float(numTrainSearcher)
    p6 = count_IntofList('6', trainCategory) / float(numTrainSearcher)
    p0Num = ones(numWords)
    p1Num = ones(numWords)
    p2Num = ones(numWords)  # 词计数向量，向量的大小与词集的大小相等
    p3Num = ones(numWords)
    p4Num = ones(numWords)
    p5Num = ones(numWords)
    p6Num = ones(numWords)
    for i in range(numWords):
        p0Num[i] = 0.01
        p1Num[i] = 0.01
        p2Num[i] = 0.01
        p3Num[i] = 0.01
        p4Num[i] = 0.01
        p5Num[i] = 0.01
        p6Num[i] = 0.01
    # print 'p0Num',p0Num
    p0Denom = 1
    p1Denom = 1
    p2Denom = 1  # 标签计算变量，记录各种标签的总数目
    p3Denom = 1
    p4Denom = 1
    p5Denom = 1
    p6Denom = 1
    for i in range(numTrainSearcher):
        if trainCategory[i] == '0':
            p0Num += trainMatrix[i]
            p0Denom += sum(trainMatrix[i])
        elif trainCategory[i] == '1':
            p1Num += trainMatrix[i]
            p1Denom += sum(trainMatrix[i])
        elif trainCategory[i] == '2':
            p2Num += trainMatrix[i]
            p2Denom += sum(trainMatrix[i])
        elif trainCategory[i] == '3':
            p3Num += trainMatrix[i]
            p3Denom += sum(trainMatrix[i])
        elif trainCategory[i] == '4':
            p4Num += trainMatrix[i]
            p4Denom += sum(trainMatrix[i])
        elif trainCategory[i] == '5':
            p5Num += trainMatrix[i]
            p5Denom += sum(trainMatrix[i])
        else:
            p6Num += trainMatrix[i]
            p6Denom += sum(trainMatrix[i])
    p0Vect = log(p0Num / float(p0Denom))
    p1Vect = log(p1Num / float(p1Denom))
    p2Vect = log(p2Num / float(p2Denom))
    p3Vect = log(p3Num / float(p3Denom))
    p4Vect = log(p4Num / float(p4Denom))
    p5Vect = log(p5Num / float(p5Denom))
    p6Vect = log(p6Num / float(p6Denom))
    return p0Vect, p1Vect, p2Vect,p3Vect, p4Vect, p5Vect,p6Vect, p0, p1, p2, p3, p4, p5, p6

def classifyNB_Age(vec2Classify,p0Vect, p1Vect, p2Vect,p3Vect, p4Vect, p5Vect,p6Vect, p0_, p1_, p2_, p3_, p4_, p5_, p6_): #根据贝叶斯公式计算某个用户向量属于某个标签的概率，并选出概率最大的标签作为输出
    p0=sum(vec2Classify*p0Vect)+log(p0_)
    p1=sum(vec2Classify*p1Vect)+log(p1_)
    p2=sum(vec2Classify*p2Vect)+log(p2_)
    p3 = sum(vec2Classify * p3Vect) + log(p3_)
    p4 = sum(vec2Classify * p4Vect) + log(p4_)
    p5 = sum(vec2Classify * p5Vect) + log(p5_)
    p6 = sum(vec2Classify * p6Vect) + log(p6_)
    a=[p0,p1,p2,p3,p4,p5,p6]
    return str(argsort(a)[-1])

def classifyNB_Gender(vec2Classify,p0Vect,p1Vect,p2Vect,p0_,p1_,p2_): #根据贝叶斯公式计算某个用户向量属于某个标签的概率，并选出概率最大的标签作为输出
    # print 'start-------------------start'
    s0=sum(vec2Classify*p0Vect)
    s1=sum(vec2Classify*p1Vect)
    s2=sum(vec2Classify*p2Vect)
    p0=s0+log(p0_)
    p1=s1+log(p1_)
    p2=s2+log(p2_)
    # print s0,' ',s1,' ',s2
    # print p0,' ',p1,' ',p2
    # print 'end--------------------end'
    return (('0' if p0>p2 else '2') if p0>p1 else ('1' if p1>p2 else '2'))

def new_wordsfreq(fulltext):
    freqDict={}
    i=0
    for token in fulltext:
        if freqDict.has_key(token):freqDict[token]+=1
        else:freqDict[token]=1
    return freqDict

def decrease_vocabset(setandfreq,useless):#传入的是字典，把频次大于为1的词组成一个新的列表,返回的也写成字典比较好
    vocabset={}                 #返回的也是字典，因为之后查找效率会比较高
    for word in setandfreq.keys():
        # if useless.has_key(word):i=0
        # elif setandfreq[word]>5 :vocabset.extend(word)
        if not useless.has_key(word):
            if setandfreq[word]>2:
                vocabset[word]=1
    return vocabset

fullwords,train_searchList,train_AgeList,train_GenderList,train_EducationList,\
    test_searchList,test_AgeList,test_GenderList,test_EducationList=loadTrainData(2000,200) #得到训练和测试的词矩阵

# Test_Id,Test_Search=loadTestData()

useless = loaduselesswords()

setandfreq=new_wordsfreq(fullwords)
train_vocabset=decrease_vocabset(setandfreq,useless)
setandfreq.clear()         #清理内存
jia=0
for word in train_vocabset.keys():
    train_vocabset[word]=jia   #在这个地方之后各个词的频数已经没有用了，所以用values记录键的位置
    jia+=1
# print 'len',len(train_vocabset)

def bagofwords2vec(inpuset):#把输入的一行词转化为一个向量，向量的大小为词集合的大小，即把每个用户输入的搜索词条转为向量形式
    returnvec = [0]*len(train_vocabset.keys())
    for word in inpuset:
        if train_vocabset.has_key(word):
            returnvec[train_vocabset[word]] += 1
    return returnvec

train_Mat = []  # 把原来的训练矩阵由中文词转化为向量
for i in range(len(train_searchList)):
    train_Mat.append(bagofwords2vec(train_searchList[i]))

def algortithm_test_Gender():
    p0Vect,p1Vect,p2Vect,p0,p1,p2=trainNB_Gender(array(train_Mat),array(train_GenderList)) #得到分类器所需的数据
    errorcount=0
    # out_gender=[]
    for i in range(len(test_searchList)):
        wordvector=bagofwords2vec(test_searchList[i])
        gender=classifyNB_Gender(array(wordvector),p0Vect,p1Vect,p2Vect,p0,p1,p2)
        # out_gender.append(gender)
        if gender!=test_GenderList[i]:
            errorcount+=1
    print('the gender error rate is:',float(errorcount)/len(test_searchList))
    # return out_gender

def algortithm_test_Age():
    p0Vect,p1Vect,p2Vect,p3Vect,p4Vect,p5Vect,p6Vect,p0,p1,p2,p3,p4,p5,p6=trainNB_Age(array(train_Mat),array(train_AgeList)) #得到分类器所需的数据
    errorcount=0
    # out_age=[]
    for i in range(len(test_searchList)):
        wordvector=bagofwords2vec(test_searchList[i])
        age=classifyNB_Age(array(wordvector),p0Vect,p1Vect,p2Vect,p3Vect,p4Vect,p5Vect,p6Vect,p0,p1,p2,p3,p4,p5,p6)
        # out_age.append(age)
        if age!=test_AgeList[i]:
            errorcount+=1
    print('the age error rate is:',float(errorcount)/len(test_searchList))
    # return out_age

def algortithm_test_Education():
    p0Vect,p1Vect,p2Vect,p3Vect,p4Vect,p5Vect,p6Vect,p0,p1,p2,p3,p4,p5,p6=trainNB_Age(array(train_Mat),array(train_EducationList)) #得到分类器所需的数据
    errorcount=0
    # out_education=[]
    for i in range(len(test_searchList)):
        wordvector=bagofwords2vec(test_searchList[i])
        education=classifyNB_Age(array(wordvector),p0Vect,p1Vect,p2Vect,p3Vect,p4Vect,p5Vect,p6Vect,p0,p1,p2,p3,p4,p5,p6)
        # out_education.append(education)
        if education!=test_EducationList[i]:
            errorcount+=1
    print('the education error rate is:',float(errorcount)/len(test_searchList))
    # return out_education

algortithm_test_Gender()
algortithm_test_Age()
algortithm_test_Education()

# data = {'ID':Test_Id,'age':out_age,'gender':out_gender,'education':out_education}
# df = pd.DataFrame(data)
# df.to_csv('result.csv',sep=' ',columns=["ID",'age',"gender",'education'],index=False,header=False,encoding="gbk")


import models
import requests

from flask import Blueprint, jsonify, request, Response

from playhouse.shortcuts import model_to_dict

breakfastRecipe = Blueprint('breakfastRecipes', 'breakfastRecipe')  # this will be my route

## here we are retrieving the random recipe from the spoonacular API
@breakfastRecipe.route('/', methods=["GET"])
def get_random_recipes():
    try:
        breakfastRecipe = "test code"
        breakfastRecipe = requests.get('https://api.spoonacular.com/recipes/random?apiKey=40b4dc4ae9fe4482b9d5633dd6ff2738&number=1&tags=breakfast')
        # print(breakfastRecipe.content)
        # recipe.headers['content-type':]
        # return jsonify(data=recipe.content, status={"code": 200, "message": "Success"})
        return Response(breakfastRecipe, mimetype='application/json')
    except models.DoesNotExist:
        return jsonify(data={}, status={"code": 401, "message": "Error getting the resources"})

@breakfastRecipe.route('/', methods=["POST"])
def saved_recipe():
    ## see request payload anagolous to req.body in express
    payload = request.get_json()
    print(type(payload), 'payload')
    saved_recipe = models.SavedRecipe.create(**payload)
    ## see the object
    print(saved_recipe.__dict__)
    ## Look at all the methods
    print(dir(saved_recipe))
    # Change the model to a dict
    print(model_to_dict(saved_recipe), 'model to dict')
    saved_recipe_dict = model_to_dict(saved_recipe)
    return jsonify(data=saved_recipe_dict, status={"code": 201, "message": "Success"})


import numpy as np
from os import listdir
from Example_RF_classifier import load_data
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, confusion_matrix
__author__ = "Ulysse Cote-Allard<ulysse.cote-allard.1@ulaval.ca> and David St-Onge<david.st-onge@polymtl.ca>"
__copyright__ = "Copyright 2007, MIST Lab"
__credits__ = ["David St-Onge", "Ulysse Cote-Allard", "Kyrre Glette", "Benoit Gosselin", "Giovanni Beltrame"]
__license__ = "MIT"
__version__ = "1.0"
__maintainer__ = "David St-Onge"
__email__ = "david.st-onge@polymtl.ca"
__status__ = "Production"

if __name__ == '__main__':
    """
    Main function utilized to build the training dataset and the performance of each participant and then training and using the classifier
    """
    dataLoader = load_data.DataLoader()
    # Get the list of participants from the true labels folder
    list_participants = listdir("../true_labels_informations/")

    accuracy_RF_array = []
    accuracy_CNN_array = []
    number_gestures_array = []

    accuracy_calculated_RF = []

    array_participants_id = []

    examples_imu_training_datasets = []
    examples_emg_training_datasets = []
    labels_training_datasets = []
    examples_imu_test_datasets = []
    examples_emg_test_datasets = []
    labels_test_datasets = []
    for participant in list_participants:
        identification_participant = participant.split(".")[0]
        information_labels = np.load("../true_labels_informations/" + participant, encoding="bytes")
        dataset_delay, labels_intervals, number_gestures, true_labels = information_labels
        number_gestures_array.append(number_gestures[0])
        print(identification_participant)
        array_participants_id.append(identification_participant)

        # Comment between here

        test_data_emg, test_data_imu_1, test_data_imu_2, labels_test, accuracy_RF = dataLoader.read_performance_data("../" +
                                                                                identification_participant,
                                                                                path_labels=labels_intervals,
                                                                                dataset_delay=dataset_delay[0])

        np.savez("../formated_datasets/" + identification_participant + "_accuracy_RF.npz", accuracy_RF=[accuracy_RF])
        examples_emg, examples_imu_1, examples_imu_2, labels = dataLoader.read_training(
            "../" + identification_participant,
            number_of_classes=number_gestures[0])

        np.savez("../formated_datasets/" + identification_participant + "_train_rf_features.npz", examples_emg=examples_emg,
                 examples_imu_1=examples_imu_1, examples_imu_2=examples_imu_2, labels=labels)

        np.savez("../formated_datasets/" + identification_participant + "_test_rf_features.npz", test_data_emg=test_data_emg,
                 test_data_imu_1=test_data_imu_1, test_data_imu_2=test_data_imu_2, labels_test=labels_test)

        # And here to stop building the datasets from scratch

        npzfile = np.load("../formated_datasets/" + identification_participant + "_accuracy_RF.npz", encoding="bytes")
        accuracy_RF_live_performance = npzfile['accuracy_RF'][0]

        npzfile = np.load("../formated_datasets/" + identification_participant + "_train_rf_features.npz", encoding="bytes")
        emg_train, examples_imu_1_pre_training, examples_imu_2_pre_training, labels_pre_training = npzfile['examples_emg'],\
                                                                                npzfile['examples_imu_1'],\
                                                                                npzfile['examples_imu_2'], npzfile['labels']

        npzfile = np.load("../formated_datasets/" + identification_participant + "_test_rf_features.npz", encoding="bytes")
        emg_test, test_data_imu_1, test_data_imu_2, labels_test = npzfile['test_data_emg'], npzfile['test_data_imu_1'], npzfile['test_data_imu_2'],\
                                                                  npzfile['labels_test']

        rf = RandomForestClassifier(n_estimators=500, max_features='log2', random_state=np.random.RandomState(42))
        rf.fit(np.concatenate((emg_train, examples_imu_1_pre_training, examples_imu_2_pre_training), axis=1), labels_pre_training)
        predictions = rf.predict(np.concatenate((emg_test, test_data_imu_1, test_data_imu_2), axis=1))

        accuracy_RF = accuracy_score(labels_test, predictions)

        print("ACCURACY RF LIVE PERFORMANCE: ", accuracy_RF_live_performance)
        print("ACCURACY RF CALCULATED: ", accuracy_RF)
        print("CONFUSION MATRIX: \n", confusion_matrix(labels_test, predictions))
        print("  ")
        print(npzfile.files)
        accuracy_calculated_RF.append(accuracy_RF)

    print("ACCURACY RF FROM ALL DATASETS:", accuracy_calculated_RF)
    print("participant ID: ", array_participants_id)


# Elmo
import tensorflow_hub as hub
import tensorflow as tf

# GPT2
import numpy as np
import torch
from pytorch_pretrained_bert import GPT2Tokenizer, GPT2Model, GPT2LMHeadModel

from bert_serving.client import BertClient
from gensen.gensen import GenSen, GenSenSingle

#ELMO
elmo = None
graph = None
sess = None
# Glove
embeddings_index = None

# GPT2
tokenizer = None
model = None

# Bert
bc = None

# GPT2
tokenizer = None
model = None

# Bert
bc = None

#Gensen

gensen_1 = None
gensen_2 = None

def getGensenEmb(batch,needPad = False,padLength = 30):
	global gensen_1
	global gensen_2

	if gensen_1 == None:
		gensen_1 = GenSenSingle(model_folder='gensen/data/models',filename_prefix='nli_large_bothskip',pretrained_emb='gensen/data/embedding/glove.840B.300d.h5')
		gensen_2 = GenSenSingle(model_folder = 'gensen/data/models',filename_prefix ='nli_large',pretrained_emb='./data/embedding/glove.840B.300d.h5')
	output_1, sent_1 = gensen_1.get_representation(batch, pool='last', return_numpy=True, tokenize=False)
	output_2, sent_2 = gensen_1.get_representation(batch, pool='last', return_numpy=True, tokenize=False)
	maxLength = 0
	for i in range(len(output_1)):
		if len(output_1[i])> maxLength:
			maxLength = len(output_1[i])
	length = []
	output = []

	for i in range(len(output_1)):
		temp = np.concatenate((output_1[i],output_2[i]),axis = 1)
		length.append(len(temp[~np.all(temp==0,axis = 1)]))
		#print(length[-1])
		#if needPad:
		#	if needPad == -1:
		#		if len(temp) <= maxLength:
		#			temp = np.concatenate([temp,np.zeros([maxLength - len(temp),temp.shape[1]])])
		#		else:
		#			temp = temp[:maxLength]
		#	else:
		#		if len(temp) <= padLength:
		#			temp = np.concatenate([np.array(wordList),np.zeros([padLength - len(wordList),300])])
			#	else:
			#		temp = temp[:padLength]
		output.append(temp)

	return np.array(output),np.array(length)
def getGloveEmb(batch, needPad = False, padLength = 30, path = "glove.840B.300d.txt"):
	# Input format should be wordList (After tokenization)
	# padLength == -1, pad to the max length in this batch
	global embeddings_index

	length = []
	output = []
	maxLength = 0

	for sentence in batch:
		if len(sentence) > maxLength:
			maxLength  = len(sentence)

	if embeddings_index == None or len(embeddings_index) == 0:
		print("Load embedding")
		embeddings_index = dict()
		print(path)
		f = open(path  , 'r', errors = 'ignore', encoding='utf8')  # "word2vec.6B.50d.txt" for test
		for line in f:
			splitLine = line.split(' ')  # For 3B  splitLine = line.split()
			word = splitLine[0]
			coefs = np.array([float(val) for val in splitLine[1:]])
			embeddings_index[word] = coefs
		f.close()
		print("Finish loading")

	for sentence in batch:
		#print(sentence)
		wordList = []
		for vocab in sentence:
			try:
				wordList.append(embeddings_index[vocab])
			except:
				wordList.append(np.zeros(300))
		length.append(len(wordList))

		if needPad:
			if padLength == -1:
				if len(sentence) < maxLength:
					wordList = np.concatenate([np.array(wordList),np.zeros([maxLength - len(wordList),300])])
			else:
				if len(wordList) < padLength:
					wordList = np.concatenate([np.array(wordList),np.zeros([padLength - len(wordList),300])])
				else:
					wordList = np.array(wordList)[:padLength]

		output.append(wordList)

	return np.array(output),np.array(length)

def getElmoEmb(batch, needPad = False, padLength = 30):
	# Input format should be string without tokenization
	assert type(batch[0]) is str
	global elmo
	global graph

	if elmo == None:
		graph = tf.Graph()
		with tf.Session(graph = graph) as sess:
			elmo = hub.Module("https://tfhub.dev/google/elmo/2")
			embeddings = elmo(batch,as_dict=True)["elmo"]
			sess.run(tf.global_variables_initializer())
			output = embeddings.eval(session = sess)
	else:
		with tf.Session(graph = graph) as sess:
			embeddings = elmo(batch,as_dict=True)["elmo"]
			sess.run(tf.global_variables_initializer())
			output = embeddings.eval(session = sess)

	if needPad:
		output_temp = []
		for i,sent in enumerate(output):
			if len(sent) > padLength:
				output_temp.append(sent[:padLength,:])
			else:
				z = np.zeros([padLength-sent.shape[0],sent.shape[1]])
				output_temp.append(np.concatenate([sent,z]))
		output = np.array(output_temp)

	return output, [len(output[0])]*len(batch)

def getGPT2Emb(batch, needPad = False, padLength = 30):
	# Input format should be word List(After tokenization)
	#assert type(batch[0]) is not str
	output = []
	length = []

	global model
	global tokenizer
	if model == None:
		tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
		model = GPT2Model.from_pretrained('gpt2')

	for text in batch:
		indexed_tokens_1 = tokenizer.encode(text)
		tokens_tensor_1 = torch.tensor([indexed_tokens_1])
		with torch.no_grad():
			hidden_states_1, past = model(tokens_tensor_1)
			length.append(len(hidden_states_1[0]))
			output.append(hidden_states_1[0].numpy())

	if needPad:
		output_temp = [ ]
		maxLength = np.array(length).max()

		if padLength == -1 :
			for i,hidden_states_1 in enumerate(output):
				if hidden_states_1.shape[0] < maxLength:
					output_temp.append(np.concatenate([hidden_states_1,np.zeros([maxLength-hidden_states_1.shape[0],hidden_states_1.shape[1]])]))
				else:
					output_temp.append(hidden_states_1[:maxLength,:])
		else:
			for i,hidden_states_1 in enumerate(output):
				if hidden_states_1.shape[0] < padLength:
					output_temp.append(np.concatenate([hidden_states_1,np.zeros([padLength-hidden_states_1.shape[0],hidden_states_1.shape[1]])]))
				else:
					output_temp.append(hidden_states_1[:padLength,:])

		output = np.array(output_temp)

	return np.array(output),np.array(length)

def getBertEmb(batch,needPad = False, padLength = 30):
	global bc
	if bc == None:
		bc = BertClient()
	output = bc.encode(batch)
	length = []
	output1 = []

	for i,emb in enumerate(output):
		output1.append(emb[~np.all(emb == 0, axis=1)])
		#print("output_1:",len(output1[-1]))
		length.append(len(output1[i]))
	return np.array(output),np.array(length)


# -*- coding: utf-8 -*-
"""
flup
================
This module provides upload support for Flask. The basic pattern is to set up
an `UploadSet` object and upload your files to it.
"""
import os.path
import posixpath
from flask import current_app, Blueprint, send_from_directory, abort, url_for
from itertools import chain
from werkzeug import secure_filename, FileStorage, LocalProxy

_flup = LocalProxy(lambda: current_app.extensions['flup'])

class AllExcept(object):
    def __init__(self, items):
        self.items = items

    def __contains__(self, item):
        return item not in self.items

class All(object):
    def __contains__(self, item):
        return True

TEXT = ('txt',)
DOCUMENTS = tuple('rtf odf ods gnumeric abw doc docx xls xlsx'.split())
IMAGES = tuple('jpg jpe jpeg png gif svg bmp'.split())
AUDIO = tuple('wav mp3 aac ogg oga flac'.split())
DATA = tuple('csv ini json plist xml yaml yml'.split())
SCRIPTS = tuple('js php pl py rb sh'.split())
ARCHIVES = tuple('gz bz2 zip tar tgz txz 7z'.split())
EXECUTABLES = tuple('so exe dll'.split())
ALL = All()
DEFAULTS = TEXT + DOCUMENTS + IMAGES + DATA

class UploadNotAllowed(Exception):
    pass

def tuple_from(*iters):
    return tuple(itertools.chain(*iters))

def extension(filename):
    return filename.rsplit('.', 1)[-1]

def lowercase_ext(filename):
    if '.' in filename:
        main, ext = filename.rsplit('.', 1)
        return main + '.' + ext.lower()
    else:
        return filename.lower()

def addslash(url):
    if url.endswith('/'):
        return url
    return url + '/'

class UploadConfiguration(object):
    def __init__(self, destination, base_url=None, allow=(), deny=()):
        self.destination = destination
        self.base_url = base_url
        self.allow = allow
        self.deny = deny

    @property
    def tuple(self):
        return (self.destination, self.base_url, self.allow, self.deny)

    def __eq__(self, other):
        return self.tuple == other.tuple

class UploadSet:
    def __init__(self, name='files', extensions=DEFAULTS):
        if not name.isalnum():
            raise ValueError("Name must be alphanumeric (no underscores)")
        self.name = name
        self.extensions = extensions
        self._config = None

    @property
    def config(self):
        if self._config is not None:
            return self._config
        try:
            return _flup.upload_sets_config[self.name]
        except AttributeError:
            raise RuntimeError("cannot access configuration outside request")

    def url(self, filename):
        base = self.config.base_url
        if base is None:
            return url_for('_uploads.uploaded_file', setname=self.name,
                           filename=filename, _external=True)
        else:
            return base + filename

    def path(self, filename, folder=None):
        if folder:
            target_folder = os.path.join(self.config.destination, folder)
        else:
            target_folder = self.config.destination
        return os.path.join(target_folder, filename)

    def file_allowed(self, storage, basename):
        return self.extension_allowed(extension(basename))

    def extension_allowed(self, ext):
        return ((ext in self.config.allow) or
                (ext in self.extensions and ext not in self.config.deny))

    def save(self, storage, folder=None, name=None):
        if not isinstance(storage, FileStorage):
            raise TypeError("storage must be a werkzeug.FileStorage")

        if folder is None and name is not None and "/" in name:
            folder, name = name.rsplit("/", 1)

        basename = lowercase_ext(secure_filename(storage.filename))

        if name:
            if name.endswith('.'):
                basename = name + extension(basename)
            else:
                basename = name

        if not self.file_allowed(storage, basename):
            raise UploadNotAllowed()

        if folder:
            target_folder = os.path.join(self.config.destination, folder)
        else:
            target_folder = self.config.destination
        if not os.path.exists(target_folder):
            os.makedirs(target_folder)
        if os.path.exists(os.path.join(target_folder, basename)):
            basename = self.resolve_conflict(target_folder, basename)

        target = os.path.join(target_folder, basename)
        storage.save(target)
        if folder:
            return posixpath.join(folder, basename)
        else:
            return basename

    def resolve_conflict(self, target_folder, basename):
        name, ext = basename.rsplit('.', 1)
        count = 0
        while True:
            count = count + 1
            newname = '{}_{:d}.{}'.format(name, count, ext)
            if not os.path.exists(os.path.join(target_folder, newname)):
                return newname

class TestingFileStorage(FileStorage):
    def __init__(self, stream=None, filename=None, name=None,
                 content_type='application/octet-stream', content_length=-1,
                 headers=None):
        FileStorage.__init__(self,
                             stream,
                             filename,
                             name=name,
                             content_type=content_type,
                             content_length=content_length,
                             headers=None)
        self.saved = None

    def save(self, dst, buffer_size=16384):
        if isinstance(dst, str):
            self.saved = dst
        else:
            self.saved = dst.name

class Flup(object):
    """
    :param app:         The application
    :param upload_sets: A list of instances of UploadSets
    """
    def __init__(self,
                 app=None,
                 upload_sets=None):
        self.app = app
        self.upload_sets = upload_sets
        self.upload_sets_config = {}

        if app is not None:
            self.app = app
            self.init_app(self.app)
        else:
            self.app = None

    def init_app(self, app):
        self.register_upload_sets(app, self.upload_sets)

        should_serve = any(s.base_url is None
                           for s in iter(self.upload_sets_config.values()))

        if '_uploads' not in app.blueprints and should_serve:
            app.register_blueprint(self._blueprint)

        app.extensions['flup'] = self

    def register_upload_sets(self, app, upload_sets):
        for uset in upload_sets:
            uset_config = self.config_for_set(uset, app)
            self.upload_sets_config[uset.name] = uset_config

    def config_for_set(self, uset, app):
        app_config = app.config
        prefix = 'UPLOADED_{}_'.format(uset.name.upper())
        using_defaults = False

        app_default_dest = app_config.get('UPLOADS_DEFAULT_DEST', None)
        app_default_url = app_config.get('UPLOADS_DEFAULT_URL', None)

        allow_extns = tuple(app_config.get('{}{}'.format(prefix, 'ALLOW'), ()))
        deny_extns = tuple(app_config.get('{}{}'.format(prefix, 'DENY'), ()))
        destination = app_config.get('{}{}'.format(prefix, 'DEST'))
        base_url = app_config.get('{}{}'.format(prefix, 'URL'))

        if destination is None:
            if app_default_dest:
                destination = os.path.join(app_default_dest, uset.name)
                using_defaults = True

        if destination is None:
            raise RuntimeError("""
                               no destination for set designated '{}' as {}\n
                               no application config var for '{}'\n
                               """.format(uset.name,
                                          '{}{}'.format(prefix, 'DEST'),
                                          'UPLOADS_DEFAULT_DEST')
                               )

        if base_url is None and using_defaults and app_default_url:
            base_url = addslash(app_default_url) + uset.name + '/'

        return UploadConfiguration(destination, base_url,
                                   allow_extns,
                                   deny_extns)

    @property
    def _blueprint(self):
        uploads_blueprint = Blueprint('_uploads',
                                      __name__,
                                      url_prefix='/_uploads')

        def uploaded_file(setname, filename):
            config = _flup.upload_sets_config.get(setname, None)
            if config is None:
                abort(404)
            return send_from_directory(config.destination, filename)

        uploads_blueprint.add_url_rule('/<setname>/<path:filename>',
                                       view_func=uploaded_file)

        return uploads_blueprint


import random

class RandomNumberGame:

    def __init__(self):

        """ Guess my number game class. The player has to guess a random number between 1 and 100 with as few attempts as possible.

             Attributes:
                 None
                 """

        self.solution = random.randint(1,100)

    def play_a_game():

        """Function to start a game. The txt file should have
        one number (float) per line. The numbers are stored in the data attribute.

        Args:
            None

        Returns:
            Messages
            """

        new_game = RandomNumberGame()
        new_game.is_solution()

    def take_a_guess(self):

         """Function to request and receive a guess from the player.

         Args:
             None

         Returns:
             None
             """

         try:
             self.guess = int(input("Guess a number between 1 and 100:- "))
         except ValueError:
             print("You're input is not an integer.")

    def is_solution(self):

        """Function to check, if the guess is right (then the end of the game is reached) or to give a hint, if not.

        Args:
            None

        Returns:
            Messages
            """

        self.attempts = 0
        while self.attempts < 100:
            self.attempts += 1
            self.take_a_guess()
            if self.guess == self.solution:
                print("You won! Number of tries: ", self.attempts)
                break
            elif self.guess < self.solution:
                print("No, the number must be higher")
            else:
                print("No, the number must be smaller")


# 质数判断
# 一个大于1的自然数，除了1和它本身外，不能被其他自然数（质数）整除（2, 3, 5, 7等）
while True:
    try:
        n = int(input("请输入一个整数:"))
        break

    except ValueError:
        print("输入错误：")

def vs(n):
    for i in range(2, n):
        if n % i == 0:
            print("不是质数")
            return False
            break
        else:
            continue
    print("质数")
    return True

vs(n)


# CS 1 example: Simple animation of a bouncing ball
# Devin Balkcom
# August, 2011
# Modified by THC.

from cs1lib import *

WINDOW_WIDTH = 400
WINDOW_HEIGHT = 400

FLOOR_Y = 100
CEILING_Y = WINDOW_HEIGHT - FLOOR_Y
BALL_RADIUS = 10

FRAME_RATE = 60     # how many frames to display per second
TIMESTEP = 1.0 / FRAME_RATE    # how often to refresh the frame

def draw_ball(x, y):
    disable_stroke()
    set_fill_color(.3, .5, 1)   # blue ball
    draw_circle(x, y, BALL_RADIUS)

def draw_boundaries():
    set_stroke_width(2)
    set_stroke_color(0, 0, 0) # black floor and ceiling
    enable_stroke()
    draw_line(0, FLOOR_Y, WINDOW_WIDTH, FLOOR_Y)
    draw_line(0, CEILING_Y, WINDOW_WIDTH, CEILING_Y)

def compute_next_position(position, velocity, timestep):
    return position + velocity * timestep

def main():
    # Initial coordinates for the ball.
    x = WINDOW_WIDTH / 2
    y = CEILING_Y - BALL_RADIUS

    # x and y velocities for the ball, now in pixels per second.
    v_x = 0
    v_y = -80

    set_clear_color(1, 1, 1)    # white background
    enable_smoothing()

    while not window_closed():
        # Draw the current frame.
        clear()
        draw_ball(x, y)
        draw_boundaries()

        # Check whether the next y location would cause a bounce.
        next_y = compute_next_position(y, v_y, TIMESTEP)
        if next_y - BALL_RADIUS < FLOOR_Y or next_y + BALL_RADIUS > CEILING_Y:
            v_y = -v_y  # reverse the y velocity

        # Update the x and y positions using the velocities.
        x = compute_next_position(x, v_x, TIMESTEP)
        y = compute_next_position(y, v_y, TIMESTEP)

        # Update the display and wait for the next frame.
        request_redraw()
        sleep(TIMESTEP)

# The last parameter of start_graphics, with the value True, indicates
# that all graphics should be drawn upside down.  Therefore, 0, 0 is the
# *bottom* left of the graphics window.  This view is convenient for
# physical simulations.
start_graphics(main, "Bouncing ball", WINDOW_WIDTH, WINDOW_HEIGHT, True)


import glob, io, os, sys

from PyPDF2 import PdfFileReader, PdfFileMerger

def merge_pdfs(links, output):
    print(f'Merging to {output}')
    merger = PdfFileMerger()
    for link in links:
            with open(link, 'rb') as file:
                merger.append(
                    PdfFileReader(
                        file
                    )
                )

    merger.write(output)

links = list()

def add_files(uri):
    if os.path.isdir(uri):
        for base, dirs, files in os.walk(uri):

            for file in files:
                add_files(os.path.join(base, file))

            for dir in dirs:
                add_files(os.path.join(base, dir))
    elif os.path.splitext(uri)[1].lower() == '.pdf':
        links.append(uri)

def merge_from_file(uri):
    print(f'Merging from file {uri}')
    with open(uri) as file:
        for line in file:
            for g in glob.iglob(os.path.join(os.path.dirname(uri), line)):
                # print(os.path.join('.', os.path.relpath(os.path.join(os.path.dirname(uri), g))))
                add_files(os.path.join('.', os.path.relpath(g)))

    merge_pdfs(links, uri + '.pdf')

for arg in sys.argv:
    merge_from_file(arg)


import numpy as np
import math

def x(angle, distance):
    return distance * np.cos(angle)

def y(angle, distance):
    return distance * np.sin(angle)
# create lists to vary

objects = [{},{},{}]

# objects[0] = {}

objects[0]['mass'] = [1]
objects[0]['a'] = [0]
objects[0]['e'] = [0]
objects[0]['inc'] = [0]
objects[0]['radius'] = [1]
objects[0]['x'] = np.cos(objects[0]['inc'])*objects[0]['a']
objects[0]['y'] = np.sin(objects[0]['inc'])*objects[0]['a']

# objects[1] = {}

objects[1]['mass'] = [0.0001]
objects[1]['a'] = [5]
objects[1]['e'] = [0]
objects[1]['inc'] = [0]
objects[1]['radius'] = [4.76E-4]
objects[1]['x'] = np.cos(objects[1]['inc'])*objects[1]['a']
objects[1]['y'] = np.sin(objects[1]['inc'])*objects[1]['a']

# objects[2] = {}

objects[2]['mass'] = [0.35] #np.arange(.05, .61, 0.05)
objects[2]['a'] = np.arange(10, 41, 10)
objects[2]['e'] = [0]
objects[2]['inc'] = np.arange(0, np.pi/2+0.1, np.pi/16)
objects[2]['radius'] = [4.66E-3]
objects[2]['x'] = np.arange(0, 17, 0.5)
objects[2]['y'] = np.arange(0, 17, 0.5)
# objects[2]['inc'] = np.tan(objects[2]['y']/objects[2]['x'])
# objects[2]['a'] = np.sqrt(objects[2]['y']**2 + objects[2]['x']**2)

# I want to find evey possible combination of things from each one of these things. I can do this by choosing one thing from first object then one thing from the next

case = []

for mass1 in objects[0]['mass']:
    for semi1 in objects[0]['a']:
        for  e1 in objects[0]['e']:
            for inc1 in objects[0]['inc']:
                for rad1 in objects[0]['radius']:
                    for mass2 in objects[1]['mass']:
                        for semi2 in objects[1]['a']:
                            for e2 in objects[1]['e']:
                                for inc2 in objects[1]['inc']:
                                    for rad2 in objects[1]['radius']:
                                        for mass3 in objects[2]['mass']:
                                            for e3 in objects[2]['e']:
                                                for rad3 in objects[2]['radius']:
                                                    for x3 in objects[2]['x']:
                                                        for y3 in objects[2]['y']:
                                                            case.append([mass1, semi1, e1, inc1, rad1, x(inc1, semi1),
                                                                         y(inc1, semi1)])
                                                            case.append([mass2, semi2, e2, inc2, rad2, x(inc2, semi2),
                                                                         y(inc2, semi2)])
                                                            distance = (x3**2 + y3**2)**0.5
                                                            if int(x3) is not 0:
                                                                inclination = np.arctan(y3/x3)
                                                            else:
                                                                inclination = np.pi/2
                                                            case.append([mass3, distance, e3, inclination, rad3, float(x3),
                                                                         float(y3)])

                                                    # for inc3 in objects[2]['inc']:
                                                    #     for rad3 in objects[2]['radius']:
                                                    #         case.append([mass1, semi1, e1, inc1, rad1, x(inc1, semi1), y(inc1, semi1)])
                                                    #         case.append([mass2, semi2, e2, inc2, rad2, x(inc2, semi2), y(inc2, semi2)])
                                                    #         case.append([mass3, semi3, e3, inc3, rad3, x(inc3, semi3), y(inc3, semi3)])

outputarray = np.asarray(case)
np.savetxt('objects16.csv', outputarray, fmt='%3.5f', delimiter=',')


import mxnet as mx

class Quantization_int8(mx.operator.CustomOp):
    def __init__(self, quant_mode, is_weight, is_weight_perchannel, delay_quant, ema_decay):
        self.quant_mode = quant_mode
        self.is_weight = is_weight
        self.is_weight_perchannel = is_weight_perchannel
        self.delay_quant = delay_quant
        self.ema_decay = ema_decay
        self.QUANT_LEVEL = 127
        self.init = True
    def forward(self, is_train, req, in_data, out_data, aux):
        if is_train and self.delay_quant > 0:
            self.assign(out_data[0], req[0], in_data[0])
            self.delay_quant -= 1
            return
        if self.is_weight:
            data = mx.nd.abs(in_data[0])
            if self.is_weight_perchannel:
                target_shape = (data.shape[0],) + (1,) * len(data.shape[1:])
                reduce_axis = tuple([i for i in range(len(data.shape))])
                maxs = mx.nd.max(data, axis=reduce_axis[1:])
                quant_unit = maxs / self.QUANT_LEVEL
                quant_unit = quant_unit.reshape(target_shape).broadcast_like(in_data[0])
            else:
                maxs = mx.nd.max(data)
                quant_unit = maxs / self.QUANT_LEVEL
            self.assign(out_data[0], req[0], mx.nd.round(in_data[0] / quant_unit) * quant_unit)
            # save weight maxs
            if is_train:
                aux[0][:] = maxs
        else:
            if is_train:
                data = mx.nd.abs(in_data[0])
                maxs = mx.nd.max(data)
                # udpate acativation maxs
                aux[0][:] = aux[0] * self.ema_decay + maxs * (1 - self.ema_decay)

            quant_unit = aux[0] / self.QUANT_LEVEL
            self.assign(out_data[0], req[0], mx.nd.round(in_data[0] / quant_unit) * quant_unit)
    def backward(self, req, out_grad, in_data, out_data, in_grad, aux):
        self.assign(in_grad[0], req[0], out_grad[0])

@mx.operator.register("Quantization_int8_V2")
class QuantizationInt8Prop(mx.operator.CustomOpProp):
    def __init__(self, quant_mode, is_weight, is_weight_perchannel=False, delay_quant=0, ema_decay=0.99):
        self.quant_mode = str(quant_mode)
        self.delay_quant = int(delay_quant)
        self.ema_decay = float(ema_decay)
        self.is_weight = eval(is_weight)
        self.is_weight_perchannel = eval(is_weight_perchannel)
        super(QuantizationInt8Prop, self).__init__(True)
    def list_arguments(self):
        return ['data']
    def list_outputs(self):
        return ['output']
    def list_auxiliary_states(self):
        return ["minmax"]
    def infer_shape(self, in_shape):
        shape = in_shape[0]
        if self.is_weight_perchannel and self.is_weight:
            aux_shape = [shape[0]]
        else:
            aux_shape = [1]
        return [shape], [shape], [aux_shape]
    def infer_type(self, in_type):
        return in_type, in_type, in_type

    def create_operator(self, ctx, shapes, dtypes):
        return Quantization_int8(self.quant_mode, self.is_weight,
                                 self.is_weight_perchannel,
                                 self.delay_quant, self.ema_decay)

def get_sym_output_channel(name, sym, data_shape=(1, 3, 224, 224)):
    _, out_shapes, _ = sym.infer_shape(data=data_shape)
    assert len(out_shapes) == 1, 'the output of sym is not equal to 1'
    # print('sym:{}:{}'.format(name, sym))
    return out_shapes[0][1]

def quant_conv(name, data, num_filter, kernel, stride, pad=(0,0), no_bias=True, dilate=(1,1), num_group=1,
               quant_mod='minmax', delay_quant=0, is_weight_perchannel=False):
    if is_weight_perchannel:
        assert quant_mod == "minmax", "currenet weight perchannel only support minmax node with weight"

    input_channel = get_sym_output_channel(name, data)
    weight = mx.sym.Variable(name=name + "_weight", shape=(num_filter, input_channel // num_group, kernel[0], kernel[1]),
                             dtype="float32")
    weight_q = mx.sym.Custom(data=weight, name = name + "_weight_quant", quant_mode=quant_mod, is_weight=True,
                             is_weight_perchannel = is_weight_perchannel, ema_decay=0.99, delay_quant=delay_quant,
                             op_type="Quantization_int8_V2")
    data_q = mx.sym.Custom(data=data, name = name + "_data_quant", quant_mode=quant_mod, is_weight=False,
                             is_weight_perchannel = False, ema_decay=0.99, delay_quant=delay_quant,
                             op_type="Quantization_int8_V2")
    conv = mx.symbol.Convolution(
        name=name,
        data=data_q,
        num_filter=num_filter,
        kernel=kernel,
        num_group=num_group,
        stride=stride,
        pad=pad,
        no_bias=no_bias,
        dilate=dilate,
        weight=weight_q
    )
    return conv

def quant_fc(name, data, num_hidden, quant_mod='minmax', delay_quant=0, is_weight_perchannel=False):
    if is_weight_perchannel:
        assert quant_mod == "minmax", "currenet weight perchannel only support minmax node with weight"
    input_channel = get_sym_output_channel(name, data)
    fc_weight = mx.sym.Variable(name=name +"_weight", shape=(num_hidden, input_channel), dtype="float32")
    fc_q = mx.sym.Custom(fc_weight, name= name + "_weight_quant", is_weight=True, ema_decay=0.99,
                         delay_quant=delay_quant, quant_mode = quant_mod, is_weight_perchannel=is_weight_perchannel,
                         op_type="Quantization_int8_V2")
    fc_data_q = mx.sym.Custom(data=data, name= name + "_data_quant", is_weight=False, ema_decay=0.99,
                              delay_quant=delay_quant, quant_mode = quant_mod, is_weight_perchannel=False,
                              op_type="Quantization_int8_V2")
    fc = mx.symbol.FullyConnected(data=fc_data_q, num_hidden=num_hidden, name='fc', weight=fc_q)
    return fc