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

from neutron_lib.api.definitions import l3
from neutron_lib import constants as lib_constants
from neutron_lib.plugins import constants as plugin_consts

ROUTER_PORT_OWNERS = lib_constants.ROUTER_INTERFACE_OWNERS_SNAT + \
    (lib_constants.DEVICE_OWNER_ROUTER_GW,)

ROUTER_STATUS_ACTIVE = 'ACTIVE'
ROUTER_STATUS_ALLOCATING = 'ALLOCATING'
ROUTER_STATUS_ERROR = 'ERROR'

VALID_ROUTER_STATUS = (ROUTER_STATUS_ACTIVE,
                       ROUTER_STATUS_ALLOCATING,
                       ROUTER_STATUS_ERROR)

HA_ROUTER_STATE_KEY = '_ha_state'
METERING_LABEL_KEY = '_metering_labels'
FLOATINGIP_AGENT_INTF_KEY = '_floatingip_agent_interfaces'
SNAT_ROUTER_INTF_KEY = '_snat_router_interfaces'

HA_NETWORK_NAME = 'HA network tenant %s'
HA_SUBNET_NAME = 'HA subnet tenant %s'
HA_PORT_NAME = 'HA port tenant %s'
HA_ROUTER_STATE_ACTIVE = 'active'
HA_ROUTER_STATE_STANDBY = 'standby'
HA_ROUTER_STATE_UNKNOWN = 'unknown'
VALID_HA_STATES = (HA_ROUTER_STATE_ACTIVE, HA_ROUTER_STATE_STANDBY,
                   HA_ROUTER_STATE_UNKNOWN)

PAGINATION_INFINITE = 'infinite'

SORT_DIRECTION_ASC = 'asc'
SORT_DIRECTION_DESC = 'desc'

ETHERTYPE_NAME_ARP = 'arp'
ETHERTYPE_ARP = 0x0806
ETHERTYPE_IP = 0x0800
ETHERTYPE_IPV6 = 0x86DD

IP_PROTOCOL_NAME_ALIASES = {lib_constants.PROTO_NAME_IPV6_ICMP_LEGACY:
                            lib_constants.PROTO_NAME_IPV6_ICMP}

IP_PROTOCOL_NUM_TO_NAME_MAP = {
    str(v): k for k, v in lib_constants.IP_PROTOCOL_MAP.items()}

# When using iptables-save we specify '-p {proto}',
# but sometimes those values are not identical.  This is a map
# of known protocol numbers that require a name to be used and
# protocol names that require a different name to be used,
# because that is how iptables-save will display them.
#
# This is how the list was created, so there is a possibility
# it will need to be updated in the future:
#
# $ for num in {0..255}; do iptables -A INPUT -p $num; done
# $ iptables-save
#
# These cases are special, and were found by inspection:
# - 'ipv6-encap' uses 'ipv6'
# - 'icmpv6' uses 'ipv6-icmp'
# - 'pgm' uses '113' instead of its name
# - protocol '0' uses no -p argument
IPTABLES_PROTOCOL_NAME_MAP = {lib_constants.PROTO_NAME_IPV6_ENCAP: 'ipv6',
                              lib_constants.PROTO_NAME_IPV6_ICMP_LEGACY:
                                  'ipv6-icmp',
                              lib_constants.PROTO_NAME_PGM: '113',
                              '0': None,
                              '1': 'icmp',
                              '2': 'igmp',
                              '3': 'ggp',
                              '4': 'ipencap',
                              '5': 'st',
                              '6': 'tcp',
                              '8': 'egp',
                              '9': 'igp',
                              '12': 'pup',
                              '17': 'udp',
                              '20': 'hmp',
                              '22': 'xns-idp',
                              '27': 'rdp',
                              '29': 'iso-tp4',
                              '33': 'dccp',
                              '36': 'xtp',
                              '37': 'ddp',
                              '38': 'idpr-cmtp',
                              '41': 'ipv6',
                              '43': 'ipv6-route',
                              '44': 'ipv6-frag',
                              '45': 'idrp',
                              '46': 'rsvp',
                              '47': 'gre',
                              '50': 'esp',
                              '51': 'ah',
                              '57': 'skip',
                              '58': 'ipv6-icmp',
                              '59': 'ipv6-nonxt',
                              '60': 'ipv6-opts',
                              '73': 'rspf',
                              '81': 'vmtp',
                              '88': 'eigrp',
                              '89': 'ospf',
                              '93': 'ax.25',
                              '94': 'ipip',
                              '97': 'etherip',
                              '98': 'encap',
                              '103': 'pim',
                              '108': 'ipcomp',
                              '112': 'vrrp',
                              '115': 'l2tp',
                              '124': 'isis',
                              '132': 'sctp',
                              '133': 'fc',
                              '135': 'mobility-header',
                              '136': 'udplite',
                              '137': 'mpls-in-ip',
                              '138': 'manet',
                              '139': 'hip',
                              '140': 'shim6',
                              '141': 'wesp',
                              '142': 'rohc'}

# A length of a iptables chain name must be less than or equal to 11
# characters.
# <max length of iptables chain name> - (<binary_name> + '-') = 28-(16+1) = 11
MAX_IPTABLES_CHAIN_LEN_WRAP = 11
MAX_IPTABLES_CHAIN_LEN_NOWRAP = 28

# Timeout in seconds for getting an IPv6 LLA
LLA_TASK_TIMEOUT = 40

# length of all device prefixes (e.g. qvo, tap, qvb)
LINUX_DEV_PREFIX_LEN = 3
# must be shorter than linux IFNAMSIZ (which is 16)
LINUX_DEV_LEN = 14

# Possible prefixes to partial port IDs in interface names used by the OVS,
# Linux Bridge, and IVS VIF drivers in Nova and the neutron agents. See the
# 'get_ovs_interfaceid' method in Nova (nova/virt/libvirt/vif.py) for details.
INTERFACE_PREFIXES = (lib_constants.TAP_DEVICE_PREFIX,
                      lib_constants.VETH_DEVICE_PREFIX,
                      lib_constants.SNAT_INT_DEV_PREFIX)

ATTRIBUTES_TO_UPDATE = 'attributes_to_update'

# TODO(amuller): Re-define the RPC namespaces once Oslo messaging supports
# Targets with multiple namespaces. Neutron will then implement callbacks
# for its RPC clients in order to support rolling upgrades.

# RPC Interface for agents to call DHCP API implemented on the plugin side
RPC_NAMESPACE_DHCP_PLUGIN = None
# RPC interface for the metadata service to get info from the plugin side
RPC_NAMESPACE_METADATA = None
# RPC interface for agent to plugin security group API
RPC_NAMESPACE_SECGROUP = None
# RPC interface for agent to plugin DVR api
RPC_NAMESPACE_DVR = None
# RPC interface for reporting state back to the plugin
RPC_NAMESPACE_STATE = None
# RPC interface for agent to plugin resources API
RPC_NAMESPACE_RESOURCES = None

# Default network MTU value when not configured
DEFAULT_NETWORK_MTU = 1500
IPV6_MIN_MTU = 1280

ROUTER_MARK_MASK = "0xffff"

VALID_ETHERTYPES = (lib_constants.IPv4, lib_constants.IPv6)

IP_ALLOWED_VERSIONS = [lib_constants.IP_VERSION_4, lib_constants.IP_VERSION_6]

PORT_RANGE_MIN = 1
PORT_RANGE_MAX = 65535

# Configuration values for accept_ra sysctl, copied from linux kernel
# networking (netdev) tree, file Documentation/networking/ip-sysctl.txt
#
# Possible values are:
#         0 Do not accept Router Advertisements.
#         1 Accept Router Advertisements if forwarding is disabled.
#         2 Overrule forwarding behaviour. Accept Router Advertisements
#           even if forwarding is enabled.
ACCEPT_RA_DISABLED = 0
ACCEPT_RA_WITHOUT_FORWARDING = 1
ACCEPT_RA_WITH_FORWARDING = 2

# Some components communicate using private address ranges, define
# them all here. These address ranges should not cause any issues
# even if they overlap since they are used in disjoint namespaces,
# but for now they are unique.
# We define the metadata cidr since it falls in the range.
PRIVATE_CIDR_RANGE = '169.254.0.0/16'
DVR_FIP_LL_CIDR = '169.254.64.0/18'
L3_HA_NET_CIDR = '169.254.192.0/18'
METADATA_CIDR = '169.254.169.254/32'

# The only defined IpamAllocation status at this stage is 'ALLOCATED'.
# More states will be available in the future - e.g.: RECYCLABLE
IPAM_ALLOCATION_STATUS_ALLOCATED = 'ALLOCATED'

VALID_IPAM_ALLOCATION_STATUSES = (IPAM_ALLOCATION_STATUS_ALLOCATED,)

# Port binding states for Live Migration
PORT_BINDING_STATUSES = (lib_constants.ACTIVE,
                         lib_constants.INACTIVE)

VALID_FLOATINGIP_STATUS = (lib_constants.FLOATINGIP_STATUS_ACTIVE,
                           lib_constants.FLOATINGIP_STATUS_DOWN,
                           lib_constants.FLOATINGIP_STATUS_ERROR)

# Floating IP host binding states
FLOATING_IP_HOST_UNBOUND = "FLOATING_IP_HOST_UNBOUND"
FLOATING_IP_HOST_NEEDS_BINDING = "FLOATING_IP_HOST_NEEDS_BINDING"

# Possible types of values (e.g. in QoS rule types)
VALUES_TYPE_CHOICES = "choices"
VALUES_TYPE_RANGE = "range"

# Units base
SI_BASE = 1000
IEC_BASE = 1024

# Port bindings handling
NO_ACTIVE_BINDING = 'no_active_binding'

# Registered extension parent resource check mapping
# If we want to register some service plugin resources into policy and check
# the owner when operating their subresources. We can write here to use
# existing policy engine for parent resource owner check.
# Each entry here should be PARENT_RESOURCE_NAME: SERVICE_PLUGIN_NAME,
# PARENT_RESOURCE_NAME is usually from api definition.
# SERVICE_PLUGIN_NAME is the service plugin which introduced the resource and
# registered the service plugin name in neutron-lib.
EXT_PARENT_RESOURCE_MAPPING = {
    l3.FLOATINGIP: plugin_consts.L3
}
EXT_PARENT_PREFIX = 'ext_parent'

RP_BANDWIDTHS = 'resource_provider_bandwidths'
RP_INVENTORY_DEFAULTS = 'resource_provider_inventory_defaults'


#!/usr/bin/python3
"""
    This module contains test cases for State
"""
import unittest
from models.state import State
from models.base_model import BaseModel
import pep8
from datetime import datetime

class TestState(unittest.TestCase):
    """" Test cases class of State """

    def test_pep8_state(self):
        """pep8 test.
        Makes sure the Python code is up to the pep8 standard.
        """
        syntax = pep8.StyleGuide(quit=True)
        check = syntax.check_files(['models/state.py'])
        self.assertEqual(
            check.total_errors, 0,
            "Found code style errors (and warnings)."
        )

    def test_stateattr(self):
        new_state = State()
        self.assertTrue(hasattr(new_state, "id"))
        self.assertTrue(hasattr(new_state, "created_at"))
        self.assertTrue(hasattr(new_state, "updated_at"))
        self.assertTrue(hasattr(new_state, "name"))

    def test_state(self):
        new_state = State()
        self.assertIs(type(new_state.id), str)
        self.assertIs(type(new_state.created_at), datetime)
        self.assertIs(type(new_state.updated_at), datetime)
        self.assertIs(type(new_state.name), str)

    def test_inherit_State(self):
        new_inherit = State()
        self.assertIsInstance(new_inherit, BaseModel)

    def test_stateisempty(self):
        new_state = State()
        self.assertEqual(new_state.name, "")


import subprocess
import os
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import seaborn as sns

def main():
    file_name = "sim.c"
    executable_name = "run_sim"
    full_executable_path = os.getcwd() + "/" + executable_name
    subprocess.call(["gcc", "-lm", file_name, "-pthread", "-o", executable_name])

    sim_args = [(2,7),(2,9),(4,7),(4,9),(6,7),(6,9)]

    plot_file_name = "plot_data.csv"
    table_file_name = "table_data.csv"

    column_labels = ["(N,M)","Total Arrival", "Total Rejections", "Rejection Ratio",
                    "Average\nWait Time", "Max Waiting\nLine", "Max Waiting\nLine Time"]
    df_table = pd.DataFrame(columns = column_labels)

    arrivals_df_dict = {}
    waiting_df_dict = {}
    rejections_df_dict = {}

    for arg_tuple in sim_args:
        N = arg_tuple[0]
        M = arg_tuple[1]
        subprocess.call([full_executable_path, "-N " + str(N), "-M " + str(M)], stdout=open(os.devnull, 'wb'))

        # If MAXPERCAR == 7, then save the plot data.
        if arg_tuple[1] == 7:
            df_plots = pd.read_csv(plot_file_name)

            arrivals_list = df_plots.ix[:,0]
            rejects_list = df_plots.ix[:,1]
            waiting_list = df_plots.ix[:,2]

            arrivals_df_dict[arg_tuple] = arrivals_list
            waiting_df_dict[arg_tuple] = waiting_list
            rejections_df_dict[arg_tuple] = rejects_list

        # Generate and save the table.
        table_data_file = open(table_file_name, "r")
        table_values = ((table_data_file.readline()).strip()).split(",")
        row_label = "N=" +  str(N) + ",M=" + str(M)

        # This is a bit sloppy, but I struggled with getting the rows updated
        # correctly without doing it explicitly
        new_table_values = [row_label, table_values[2], table_values[3], table_values[4], table_values[5], table_values[6], table_values[7]]
        df_table.loc[row_label] = new_table_values

    make_plots(arrivals_df_dict, waiting_df_dict, rejections_df_dict)
    make_table(df_table)

# make_plots()
def make_plots(arrivals_df_dict, waiting_df_dict, rejections_df_dict):
    x_axis = np.linspace(0,600,600)

    arrivals_fig = plt.figure()
    ax0 = arrivals_fig.add_subplot(111)
    for key, value in arrivals_df_dict.items():
        ax0.plot(x_axis, value, label=key)
    ax0.legend(loc=2)
    ax0.set_title("Arrivals Plot")
    ax0.set_xlabel("Time (minutes)")
    ax0.set_ylabel("Num. of Arrivals")
    ax0.grid(True)
    plt.savefig("arrivals-plot.png", bbox_inches='tight')

    waiting_fig = plt.figure()
    ax1 = waiting_fig.add_subplot(111)
    for key, value in waiting_df_dict.items():
        ax1.plot(x_axis, value, label=key)
    ax1.legend(loc=2)
    ax1.set_title("Waiting Line Plot")
    ax1.set_xlabel("Time (minutes)")
    ax1.set_ylabel("Num of People Waiting")
    ax1.grid(True)
    plt.savefig("waiting-plot.png", bbox_inches='tight')

    rejection_fig = plt.figure()
    ax2 = rejection_fig.add_subplot(111)
    for key, value in rejections_df_dict.items():
        ax2.plot(x_axis, value, label=key)
    ax2.legend(loc=2)
    ax2.set_title("Rejections Plot")
    ax2.set_xlabel("Time (minutes)")
    ax2.set_ylabel("Num of People Rejected")
    ax2.grid(True)
    plt.savefig("rejections-plot.png", bbox_inches='tight')

# make_table()
def make_table(df_table):
    # Making table of values
    fig, ax = plt.subplots()
    fig.patch.set_visible(False)
    ax.axis('off')

    # The bbox and tight_layout numbers had to be carefully found.
    table = ax.table(cellText = df_table.values, colLabels=df_table.columns, loc='center', bbox=[-0.38, -0.3, 1.4, 0.275]) # first value is left shift
    table.auto_set_font_size(False)
    table.set_fontsize(8)
    fig.tight_layout(rect=[0.2, 0.8, 1.02, 2.2]) # 3rd value is width of cells
    plt.savefig("table.png")

    plt.show()

if __name__ == "__main__":
    main()


from typing import List, Optional, Dict
import os
import pickle

from elasticsearch_dsl import DocType, Text, Keyword, Search, Index
from elasticsearch_dsl.connections import connections
import elasticsearch
import progressbar
import numpy as np

from qanta.datasets.abstract import QuestionText
from qanta.datasets.quiz_bowl import QuizBowlDataset
from qanta.guesser.abstract import AbstractGuesser
from qanta.spark import create_spark_context
from qanta.wikipedia.cached_wikipedia import CachedWikipedia
from qanta.config import conf
from qanta.preprocess import preprocess_dataset, tokenize_question
from qanta.guesser import nn
from qanta import logging

log = logging.get(__name__)
connections.create_connection(hosts=['localhost'])
index_name = 'ir_dan_guesser'

IR_DAN_WE_TMP = '/tmp/qanta/deep/ir_dan_we.pickle'
IR_DAN_WE = 'ir_dan_we.pickle'

load_embeddings = nn.create_load_embeddings_function(IR_DAN_WE_TMP, IR_DAN_WE, log)

class Answer(DocType):
    page = Text(fields={'raw': Keyword()})
    wiki_content = Text()

    class Meta:
        index = index_name

class ElasticSearchIndex:
    @staticmethod
    def delete():
        try:
            Index(index_name).delete()
        except elasticsearch.exceptions.NotFoundError:
            log.info('Count not delete non-existent index: {}'.format(index_name))

    @staticmethod
    def exists():
        return Index(index_name).exists()

    @staticmethod
    def build(pages, rebuild_index=False):
        if rebuild_index or os.getenv('QB_REBUILD_INDEX', False):
            ElasticSearchIndex.delete()

        if ElasticSearchIndex.exists():
            log.info('Found index, skipping building index')
        else:
            Answer.init()
            cw = CachedWikipedia()
            log.info('Indexing questions and corresponding wikipedia pages...')
            bar = progressbar.ProgressBar()
            for page in bar(pages):
                wiki_content = cw[page].content
                answer = Answer(page=page, wiki_content=wiki_content)
                answer.save()

    @staticmethod
    def search(text: str, max_n_guesses: int):
        s = Search(index=index_name)[0:max_n_guesses].query(
            'multi_match', query=text, fields=['wiki_content'])
        results = s.execute()
        return [(r.page, r.meta.score) for r in results]

IRDAN_PARAMS = 'irdan_params.pickle'
es_index = ElasticSearchIndex()

def preprocess_wikipedia(all_top_guesses, cached_wikipedia, vocab):
    rows = []
    for q_guesses in all_top_guesses:
        q_rows = []
        for page, _ in q_guesses:
            content = tokenize_question(cached_wikipedia[page])[:150]
            q_rows.append(content)
            for t in content:
                vocab.append(t)
        rows.append(q_rows)
    return rows

class IrDanGuesser(AbstractGuesser):
    def __init__(self):
        super().__init__()
        guesser_conf = conf['guessers']['ElasticSearch']
        self.min_appearances = guesser_conf['min_appearances']
        self.n_cores = guesser_conf['n_cores']
        self.use_wiki = guesser_conf['use_wiki']
        self.use_qb = guesser_conf['use_qb']
        self.embeddings = None
        self.embedding_lookup = None
        self.n_classes = None
        self.max_len = None

    def qb_dataset(self):
        return QuizBowlDataset(self.min_appearances, guesser_train=True)

    def parameters(self):
        return {
            'min_appearances': self.min_appearances,
            'n_cores': self.n_cores,
            'use_wiki': self.use_wiki,
            'use_qb': self.use_qb
        }

    def build_model(self):
        pass

    def train(self, training_data):
        log.info('Building elastic search index...')
        ElasticSearchIndex.build(set(training_data[1]))

        def fetch_top_guesses(query):
            return ElasticSearchIndex.search(query, 10)

        sentences = []
        for q in training_data[0]:
            for s in q:
                sentences.append(s)

        sc = create_spark_context('QB: IR NN Re-ranker')
        all_top_guesses = sc.parallelize(sentences).map(fetch_top_guesses).collect()
        sc.stop()

        log.info('Processing training data...')
        x_train, y_train, x_test, y_test, vocab, class_to_i, i_to_class = preprocess_dataset(
            training_data
        )

        cw = CachedWikipedia()
        all_top_pages = preprocess_wikipedia(all_top_guesses, cw, vocab)

        log.info('Creating embeddings...')
        embeddings, embedding_lookup = load_embeddings(vocab=vocab, expand_glove=True, mask_zero=True)
        self.embeddings = embeddings
        self.embedding_lookup = embedding_lookup

        x_train = [nn.convert_text_to_embeddings_indices(q, embedding_lookup) for q in x_train]
        x_test = [nn.convert_text_to_embeddings_indices(q, embedding_lookup) for q in x_test]
        self.n_classes = nn.compute_n_classes(training_data[1])
        self.max_len = nn.compute_max_len(training_data)
        x_train = np.array(nn.tf_format(x_train, self.max_len, 0))
        x_test = np.array(nn.tf_format(x_test, self.max_len, 0))

    def guess(self, questions: List[QuestionText], max_n_guesses: Optional[int]):
        sc = create_spark_context(configs=[('spark.executor.cores', self.n_cores), ('spark.executor.memory', '4g')])

        def es_search(query):
            return es_index.search(query, max_n_guesses)

        return sc.parallelize(questions, 16 * self.n_cores).map(es_search).collect()

    @classmethod
    def targets(cls):
        return []

    @classmethod
    def load(cls, directory: str):
        with open(os.path.join(directory, IRDAN_PARAMS), 'rb') as f:
            params = pickle.load(f)
        guesser = IrDanGuesser()
        guesser.min_appearances = params['min_appearances']
        guesser.n_cores = params['n_cores']
        return guesser

    def save(self, directory: str):
        with open(os.path.join(directory, IRDAN_PARAMS), 'wb') as f:
            pickle.dump({
                'min_appearances': self.min_appearances,
                'n_cores': self.n_cores
            }, f)


from django.core.validators import MinValueValidator, MaxValueValidator
from django.db import models
from django.utils.translation import gettext_lazy as _

class CV(models.Model):
    title_en = models.CharField(_('Title (en)'),
                                help_text=_('Specify the title (occupation/degree) of your new CV entry'),
                                max_length=64, null=False, blank=False
                                )
    title_de = models.CharField(_('Title (de)'),
                                help_text=_('Specify the title (occupation/degree) of your new CV entry'),
                                max_length=64, null=False, blank=False
                                )

    def __str__(self):
        return self.title_en

    class Meta:
        abstract = False

class Experience(CV):
    company = models.CharField(_('Company'),
                               help_text=_('Specify the name of the company'),
                               max_length=64, null=False, blank=False
                               )

    date_from = models.DateField(_('Start of occupation'),
                                 help_text=_('Define the start of your occupation'),
                                 null=False, blank=False
                                 )
    date_to = models.DateField(_('End of your occupation'),
                               help_text=_('Define the end of your occupation\n'
                                            'Leave blank if it is still in progress'),
                               null=True, blank=True
                               )
    content_en = models.TextField(_('Content (en)'),
                                  help_text=_('Specify the main content of your occupation'),
                                  max_length=512, null=False, blank=False
                                  )
    content_de = models.TextField(_('Content (de)'),
                                  help_text=_('Specify the main content of your occupation'),
                                  max_length=512, null=False, blank=False
                                  )

    def __str__(self):
        return ''.join([self.title_en, '@', self.company])

    class Meta:
        ordering = ['-date_from']
        verbose_name = _('Experience')
        verbose_name_plural = _('Experiences')

class Education(CV):
    school = models.CharField(_('School/College'),
                              help_text=_('Specify the name of the school or college'),
                              max_length=64, null=False, blank=False
                              )
    date_from = models.DateField(_('Start of education'),
                                 help_text=_('Define the start of your education'),
                                 null=False, blank=False
                                 )
    date_to = models.DateField(_('End of your education'),
                               help_text=_('Define the end of your education\n'
                                            'Leave blank if it is still in progress'),
                               null=True, blank=True
                               )
    content_en = models.TextField(_('Content (en)'),
                                  help_text=_('Specify the main content of your education'),
                                  max_length=512, null=False, blank=False
                                  )
    content_de = models.TextField(_('Content (de)'),
                                  help_text=_('Specify the main content of your education'),
                                  max_length=512, null=False, blank=False
                                  )

    def __str__(self):
        return ''.join([self.title_en, '@', self.school])

    class Meta:
        ordering = ['-date_from']
        verbose_name = _('Education')
        verbose_name_plural = _('Education')

class Skill(CV):
    level = models.FloatField(_('Level'),
                              help_text=_('level 1 being 100% (don\'t do that...)'),
                              null=False, blank=False,
                              validators=[
                                  MinValueValidator(.05),
                                  MaxValueValidator(1.0)
                              ]
                              )

    # def __str__(self):
    #     return self.title_en

    class Meta:
        abstract = False
        verbose_name = _('Skill')
        verbose_name_plural = _('Skills')

class CodingLanguage(Skill):

    class Meta:
        verbose_name = _('Coding Language')
        verbose_name_plural = _('Coding Languages')

class SoftwareSkill(Skill):

    class Meta:
        verbose_name = _('Software Skill')
        verbose_name_plural = _('Software Skills')

class Language(Skill):

    class Meta:
        verbose_name = _('Language')
        verbose_name_plural = _('Languages')


import json, os
from cygnus import groupme, utils

def init():
    if not os.path.exists("./cygnus/plugins/alias/"):
        os.makedirs("./cygnus/plugins/alias/")
    return

def parse(obj, me):
    text = obj["text"]
    work = text.split(" ")

    if '/alias' == work[0]:
        mentions = utils.get_mentions(obj)

        if "+me" in text:
            mentions.append(obj["user_id"])

        if mentions:
            alias_name = work[1].split("@")[0].lower()

            if os.path.isfile("./cygnus/plugins/alias/%s.json" % (obj["group_id"])):
                with open("./cygnus/plugins/alias/%s.json" % (obj["group_id"]), "r") as f:
                    aliases = json.load(f)
            else:
                aliases = {}

            aliases[alias_name] = mentions

            with open("./cygnus/plugins/alias/%s.json" % (obj["group_id"]), "w") as f:
                json.dump(aliases, f)

            groupme.send_message(obj, "Alias %s created." % (alias_name))
    elif "@" in text:
        key = text.split("@")[1].split(" ")[0].lower()

        if key == "everyone":
            return

        if os.path.isfile("./cygnus/plugins/alias/%s.json" % (obj["group_id"])):
                with open("./cygnus/plugins/alias/%s.json" % (obj["group_id"]), "r") as f:
                    aliases = json.load(f)
        else:
            return

        if key in aliases:
            mentions = aliases[key]
        else:
            return

        attachments = [
            {
                "type" : "mentions",
                "user_ids" : mentions,
                "loci" : [[0, len(text)]]*len(mentions)
            }
        ]

        groupme.send_message(obj, text, attachments)


import numpy as np
from PIL import Image
import random
import torch
from torchvision import datasets, transforms

class LeftOutScaleMNIST(datasets.MNIST):
    # pct_to_keep: percentage of possible combinations to keep between 0 and 1, rounded down to nearest multiple of 1/9
    def __init__(self, root, train=True, transform=None, target_transform=None, download=False, pct_to_keep=1, color_indices=None):
        super().__init__(root, train, transform, target_transform, download)
        print(pct_to_keep)
        pct = pct_to_keep * 3
        print(pct)
        self.max_left_dist = int(pct / 2)
        self.max_right_dist = int(pct / 2) if pct % 2 == 0 else int(pct / 2) + 1
        self.held_out = [(0, 1), (1, 2), (2, 0), (3, 1), (4, 2), (5, 0), (6, 1), (7, 2), (8, 0)]
        self.control = [(0, 0), (1, 1), (2, 2), (3, 0), (4, 1), (5, 2), (6, 0), (7, 1), (8, 2)]
        self.held_out_val = self.held_out[:2]
        self.held_out_test = self.held_out[2:]
        self.control_val = self.control[:2]
        self.control_test = self.control[2:]
        self.combination_space_shape = (9, 3)
        self.class_names = ("shape", "scale")
        self.name = "left_out_scale_mnist"

    def __getitem__(self, index):
        """
        Args:
            index (int): Index

        Returns:
            tuple: (image, target) where target is index of the target class.
        """
        if self.train:
            img, target = self.train_data[index], self.train_labels[index]
        else:
            img, target = self.test_data[index], self.test_labels[index]
        assert target.item() < 9

        # Put grayscale image in RGB space
        img_array = np.stack((img.numpy(),) * 3, axis=-1)

        # Determine image size
        number_class = target.item()
        lower_bound = number_class - self.max_left_dist
        upper_bound = number_class + self.max_right_dist
        scale_class = random.randrange(lower_bound, upper_bound) % 3
        size = [28, 18, 8][scale_class]

        img = Image.fromarray(img_array)
        img.thumbnail((size, size))
        img_array = np.zeros((28, 28, 3))

        vert_offset = int((28 - size) / 2)
        horiz_offset = int((28 - size) / 2)

        img_array[vert_offset:(vert_offset + size), horiz_offset:(horiz_offset + size), :] = np.array(img)
        img_array = np.clip(img_array, 0, 254)
        img_array = img_array.astype(dtype=np.uint8)

        # 28x28 to 32x32
        zeros = np.zeros((img_array.shape[0] + 4, img_array.shape[1] + 4, img_array.shape[2]), dtype="uint8")
        zeros[2:img_array.shape[0] + 2, 2:img_array.shape[1] + 2, :] = img_array
        img_array = zeros

        img = Image.fromarray(img_array)

        # Perform any non-color transformations
        if self.transform is not None:
            img = self.transform(img)

        if self.target_transform is not None:
            target = self.target_transform(target)

        return transforms.ToTensor()(img), torch.tensor([target.item(), scale_class])


# @Time   :2018/9/10 22:34
# @Author :YTL
# @File   :operation_excel.py

import openpyxl
from collections import Iterable
import os

class OperationExcel:
    '''操作excel的类'''

    def __init__(self, filename=None):
        if filename is None:
            self.filename = os.path.join(os.getcwd(), 'test.xlsx')
        else:
            self.filename = filename
        self.operation = openpyxl.load_workbook(filename=self.filename)

    def write_header(self, header, sheet_name=None):
        if sheet_name is None or sheet_name == "":
            sheet_name = self.operation.sheetnames[0]
        sheet = self.operation[sheet_name]
        if isinstance(header, Iterable):
            for col, val in enumerate(header, 1):
                sheet.cell(row=1, column=col, value=val)
            self.operation.save(self.filename)
            print('write header success.')

    def write_line(self, data, sheet_name=None):
        if sheet_name is None or sheet_name == "":
            sheet_name = self.operation.sheetnames[0]
        sheet = self.operation[sheet_name]
        col_num = sheet.max_column

        if not isinstance(data, Iterable):
            print('data format is wrong.')
            return

        if len(data) != col_num:
            print('data length inconsistency.')
            return

        sheet.append(data)
        self.operation.save(self.filename)
        print('write line success.')

if __name__ == '__main__':
    header = ['header1', 'header2', 'header3', 'header4', 'header5']
    data = ['header1', 'header2', 'header3', 'header4', 'header5']
    op = OperationExcel()
    #op.write_header(header)
    op.write_line(data, 'Sheet1')
    op.write_line(data, 'Sheet1')


import tornado.ioloop
from tornado.web import RequestHandler, Application
import json

class FormHandler(RequestHandler):
    def get(self):
        print(dir(self))
        self.write(open('indexForm.html').read())

    def post(self):
        self.set_header("Content-Type", "application/json")
        self.write(
            json.dumps({
                'data': self.get_body_argument("message"),
            })
        )

def main():
    return Application([
        (r'/', FormHandler),
    ])

if __name__ == "__main__":
    app = main()
    app.listen(8888)
    tornado.ioloop.IOLoop.current().start()


import logging
import requests
import sys
import time
from multiprocessing import Value
import concurrent.futures
from loader import settings

logging.basicConfig(level=logging.INFO)
log = logging.getLogger(__name__)

def fetch_bootstrap_ads():
    feed_url = settings.LA_BOOTSTRAP_FEED_URL

    log.info('fetch_bootstrap_ad_ids - fetching ads from %s' % feed_url)

    distinct_items = None
    try:
        r = requests.get(feed_url)
        r.raise_for_status()
        json_result = r.json()
        if json_result and 'idLista' in json_result:
            items = json_result['idLista']
            distinct_items = list({v['annonsId']: v for v in items}.values())
    except requests.exceptions.RequestException as e:
        log.error("Failed to read from %s" % feed_url, e)
    return distinct_items

def is_ad_included(ad_id_item, last_ts, exclude_ids):
    if ad_id_item['uppdateradTid'] == last_ts and \
            str(ad_id_item['annonsId']) in exclude_ids:
        return False
    else:
        return True

def fetch_updated_ads(last_ts, exclude_ids):
    exclude_ids = set(exclude_ids)
    feed_url = settings.LA_FEED_URL + str(last_ts)
    log.info('fetch_updated_ads - fetching ads from %s' % feed_url)

    distinct_items = None
    try:
        r = requests.get(feed_url)
        r.raise_for_status()
        json_result = r.json()
        if json_result and 'idLista' in json_result:
            items = json_result['idLista']
            distinct_items = list({v['annonsId']: v for v in items
                                   if is_ad_included(v, last_ts, exclude_ids)}.values())
    except requests.exceptions.RequestException as e:
        log.error("Failed to read from %s" % feed_url, e)

    return distinct_items

def clean_stringvalues(value):
    if isinstance(value, dict):
        value = {clean_stringvalues(k): clean_stringvalues(v) for k, v in value.items()}
    elif isinstance(value, list):
        value = [clean_stringvalues(v) for v in value]
    elif isinstance(value, str):
        value = ''.join([i if ord(i) > 0 else '' for i in value])
    return value

def fetch_ad_details(ad_id, ts, url):
    fail_count = 0
    fail_max = 10
    detail_url = url + str(ad_id)
    while True:
        try:
            r = requests.get(detail_url, timeout=10)
            r.raise_for_status()
            ad = r.json()
            if ad:
                ad['id'] = str(ad['annonsId'])
                ad['updatedAt'] = ts
                ad['expiresAt'] = ad['sistaPubliceringsdatum']
                if 'kontaktpersoner' in ad:
                    del ad['kontaktpersoner']
                if 'organisationsnummer' in ad and \
                        len(ad.get('organisationsnummer', '').strip()) > 9:
                    orgnr = ad['organisationsnummer']
                    significate_number_position = 4 if len(orgnr) == 12 else 2
                    if int(orgnr[significate_number_position]) < 2:
                        ad['organisationsnummer'] = None
                clean_ad = clean_stringvalues(ad)
                return clean_ad
        except requests.exceptions.ConnectionError as e:
            fail_count += 1
            time.sleep(0.3)
            log.warning("Unable to load data from %s - Connection error, try %s" % (detail_url, fail_count))
            if fail_count >= fail_max:
                log.error("Failed to continue loading data from %s - Connection" %
                          detail_url, e)
                sys.exit(1)
        except requests.exceptions.Timeout as e:
            fail_count += 1
            time.sleep(0.3)
            log.warning("Unable to load data from %s - Timeout, try %d" % (detail_url, fail_count))
            if fail_count >= fail_max:
                log.error("Failed to continue loading data from %s - Timeout" %
                          detail_url, e)
                sys.exit(1)
        except requests.exceptions.RequestException as e:
            fail_count += 1
            time.sleep(0.3)
            log.warning("Unable to fetch data at %s, try %d" % (detail_url, fail_count))
            if fail_count >= fail_max:
                log.error("Failed to fetch data at %s, skipping" % detail_url, e)
                raise e

def execute_calls(ad_datas, details_url, parallelism):
    parallelism = int(parallelism)
    log.info('Multithreaded fetch ad details with %s processes' % str(parallelism))

    global counter
    counter = Value('i', 0)
    result_output = {}
    error_ids = []
    not_found_ids = []
    # with statement to ensure threads are cleaned up promptly
    with concurrent.futures.ThreadPoolExecutor(max_workers=parallelism) as executor:
        # Start the load operations
        future_to_fetch_result = {
            executor.submit(fetch_ad_details, ad_data['annonsId'],
                            ad_data['uppdateradTid'], details_url): ad_data for
            ad_data in ad_datas}
        for future in concurrent.futures.as_completed(future_to_fetch_result):
            input_data = future_to_fetch_result[future]
            try:
                detailed_result = future.result()
                result_output[detailed_result['annonsId']] = detailed_result
                # += operation is not atomic, so we need to get a lock for counter:
                with counter.get_lock():
                    # log.info('Counter: %s' % counter.value)
                    counter.value += 1
                    if counter.value % 100 == 0:
                        log.info("Multithreaded fetch ad details - Processed %s docs" %
                                 (str(counter.value)))
            except requests.exceptions.HTTPError as exc:
                status_code = exc.response.status_code
                error_message = 'Fetch ad details call generated an exception: %s' % \
                    (str(exc))
                log.error(error_message)
                if status_code == 404:
                    # This exception will happen if the list of updated ads contains ads
                    # with 'avpublicerad' == false even though the ad does not exist when
                    # trying to fetch the ad details (wrong status in the list).
                    not_found_ids.append(input_data)
                else:
                    error_ids.append(input_data['annonsId'])
            except Exception as exc:
                error_message = 'Fetch ad details call generated an exception: %s' % \
                    (str(exc))
                log.error(error_message)
                error_ids.append(input_data['annonsId'])

    return result_output, error_ids, not_found_ids


import itertools,copy
import glob,os,sys
import time

sys.path.append(glob.glob(os.getcwd())[0] + "/../../day5/python")

from intcode import Intcode

def get_maximum_thrust(file_location):
  """ Goes through all permutations and checks the maximum thrust value. """

  max = -sys.maxsize
  for phase in itertools.permutations([0,1,2,3,4]):
    intcodes = [Intcode(file_location,[i],verbose=False) for i in phase]
    out = 0
    for intcode in intcodes:
      terminate,out = intcode(out)
      if out > max: max = out
  return max

def feedback_thrust(file_location):
  """ Goes through all permutations and returns the maximum feedback thrust. """

  max = -sys.maxsize

  for phase in itertools.permutations([5,6,7,8,9]):

    intcodes = [Intcode(file_location,[i],False,False) for i in phase]

    terminate,out = False,0

    while not terminate:
      for intcode in intcodes:
        terminate,out = intcode(out)
    if out > max:
      max = out
  return max

def main():

  file_location = "../data/input.csv"

  # Solve puzzle
  t0 = time.time()
  part_one = get_maximum_thrust(file_location)
  time_part_one = round((time.time()-t0)*1e3)
  print("Solution to part one: %s (time taken %s[ms])" % (
      part_one, time_part_one))

  t0 = time.time()
  part_two = feedback_thrust(file_location)
  time_part_two = round((time.time()-t0)*1e3)
  print("Solution to part two: %s (time taken %s[ms])" % (
      part_two, time_part_two))

if __name__=="__main__":
  main()


import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt

def hist2D_demo(image):
    hsv = cv.cvtColor(image, cv.COLOR_BGR2HSV)
    hist = cv.calcHist([image], [0, 1], None, [180, 256], [0, 180, 0, 256])
    # cv.imshow('hist_2D', hist)
    plt.imshow(hist)
    plt.title('2D—Histogram')
    plt.show()

def break_projection_demo(sample, target):
    roi_hsv = cv.cvtColor(sample, cv.COLOR_BGR2HSV)
    target_hsv = cv.cvtColor(target, cv.COLOR_BGR2HSV)

    # show images
    cv.imshow('sample', sample)
    cv.imshow('target', target)

    roiHis = cv.calcHist([roi_hsv], [0, 1], None, [32, 32], [0, 180, 0, 256])
    cv.normalize(roiHis, roiHis, 0, 255, cv.NORM_MINMAX)  # 规划到 [0，255]
    dst = cv.calcBackProject([target_hsv], [0, 1], roiHis, [0, 180, 0, 256], 1)
    cv.imshow('calcBackProject', dst)

print('-----Hello Python------')
src = cv.imread('C:/Users/JIE/Desktop/illustration/picture/1.jpg')
src1 = cv.imread('C:/Users/JIE/Desktop/illustration/picture/1_test.jpg')
cv.namedWindow('input image', cv.WINDOW_AUTOSIZE)
cv.imshow('input image', src)
hist2D_demo(src)
break_projection_demo(src, src1)
cv.waitKey(0)
cv.destroyWindow()


# -*- coding: utf-8 -*-
"""
Created on Sun Feb 23 16:53:24 2020

@author: sushila.sipai
"""

import cx_Oracle

ip='10.2.3.21'
sid='DANPHE'
port='1521'

try:
   dns_tns=cx_Oracle.makedsn(ip,port,sid)
   db=cx_Oracle.connect('system','manager',dns_tns)

except :
   print('Connection Failed')
db.autocommit=True

mesg_req_type=['1200', '1420','1220','1804']
mesg_type=['1210', '1430','1230','1814']
with open ('LISRVR_NPS_SWIF_119993_240220_08.log', 'r') as fo :

   mesg=[]
   i=0
   flg=False
   mesg.append([])

   for line in fo:
         if "MessageId" in line.strip():
            if((line.partition(':')[2]).strip() in (mesg_type)):
                flg=True
            mesg[i].append((line.partition(':')[2]).strip())
         elif "Field 003" in line.strip():
            mesg[i].append((line.partition(':')[2]).strip())
         elif "Field 011" in line.strip():
            mesg[i].append((line.partition(':')[2]).strip())
         elif "Field 017" in line.strip():
           mesg[i].append((line.partition(':')[2]).strip())
         elif "Field 039" in line.strip():
           mesg[i].append((line.partition(':')[2]).strip())
           if(flg==True):
               i+=1
               mesg.append([])
               flg=False
           else:
            pass
   print(len(mesg))
   print (mesg)

updatecount=0
zeroappendcount=0
for n in range(len(mesg)):
    if(len(mesg[n])!=9):
        print (n)
        print (len(mesg[n]))
        if(len(mesg[n]) >0):
            if(mesg  [n][0] in (mesg_type)):
                cursor1=db.cursor()
                cursor1.execute("select QFIELD011 from custom.c24logs where QFIELD003='%s' and qfield011='%s' and qfield017='%s'"%(mesg[n][1],mesg[n][2],mesg[n][3]))
                if(len(cursor1.fetchall())> 0):
                    cursor2=db.cursor()
                    cursor2.execute("update custom.c24logs set rmessageid='%s', rfield003='%s', rfield011='%s', rfield017='%s', rfield039='%s' where QFIELD003='%s' and qfield011='%s' and qfield017='%s'"%(mesg[n][0],mesg[n][1],mesg[n][2],mesg[n][3],mesg[n][4],mesg[n][1],mesg[n][2],mesg[n][3]))
                    updatecount+=1
                print ("updated")
                print(mesg[n])
            elif(mesg[n][0] in mesg_req_type):
                for j in range(len(mesg[n]),9):
                    mesg[n].append(0)
                zeroappendcount+=1
                print("zero appended")
                print(mesg[n])
        print("update count and zero append count:")
        print(updatecount)
        print(zeroappendcount)

cursor=db.cursor()
insertfail=0
for n in range(len(mesg)):
    if(len(mesg[n])==9):
        cursor.execute("Insert into custom.c24logs (qMessageId,qField003,qField011,qField017,rMessageId,rField003,rField011,rField017,rField039) values('%s','%s','%s','%s','%s','%s','%s','%s','%s')" %(mesg[n][0],mesg[n][1],mesg[n][2],mesg[n][3],mesg[n][4],mesg[n][5],mesg[n][6],mesg[n][7],mesg[n][8]))
    else:
        insertfail+=1
        print ("failed to insert")
        print(mesg[n])
print("inset fail count:")
print(insertfail)


import json
import random
import numpy as np

from paddlehub_dataprocess import write_by_lines,read_by_lines,regrex_data,read_label
from paddlehub_dataprocess import write_title
from paddlehub_dataprocess import write_log,get_submit_cross_validation_vote

from config import *

from paddlehub_buildtask import *

def one(args, schema_labels, predict_data, predict_sents, id):
    seq_label_task, reader = get_task(args, schema_labels, id)
    # 加载PaddleHub 预训练模型ERNIE Tiny/RoBERTa large
    # 更多预训练模型 https://www.paddlepaddle.org.cn/hublist?filter=en_category&value=SemanticModel
    # model_name = "ernie_tiny"

    # PaddleHub Finetune API
    # 将自动训练、评测并保存模型
    if args.do_train:
        print("start finetune and eval process")
        seq_label_task.finetune_and_eval()
        write_log('./work/log/' + args.do_model + '.txt', args, id + ',' + str(seq_label_task.best_score))

    if args.do_predict:
        print("start predict process")
        ret = []
        id2label = {val: key for key, val in reader.label_map.items()}
        input_data = [[d] for d in predict_data]
        # print(input_data[:10])
        run_states = seq_label_task.predict(data=input_data)
        results = []
        for batch_states in run_states:
            batch_results = batch_states.run_results
            # print('batch_infers',batch_results )
            batch_infers = batch_results[0].reshape([-1]).astype(np.int32).tolist()
            seq_lens = batch_results[1].reshape([-1]).astype(np.int32).tolist()
            current_id = 0
            for length in seq_lens:
                seq_infers = batch_infers[current_id:current_id + length]
                seq_result = list(map(id2label.get, seq_infers[1: -1]))
                current_id += length if args.add_crf else args.max_seq_len
                results.append(seq_result)

        ret = []
        for sent,input, r_label in zip(predict_sents,input_data,results):
            sent["input"]=input
            sent["labels"] = r_label
            ret.append(json.dumps(sent, ensure_ascii=False))
        write_by_lines("{}.{}.{}.pred".format(output_predict_data_path, args.do_model, id), ret)
        get_submit_postprocess(args, id)
        get_submit_postprocess(args, id,check=True)

def cross_validation(args,id):
    begid=id
    for i in range(args.cross_validation_num):
        if (id < 40):
            id += 1
            continue
        schema_labels, predict_data, predict_sents = process_data(args, i)
        args.checkpoint_dir = 'models/' + args.do_model + str(id)
        one(args, schema_labels, predict_data, predict_sents, str(id))
        get_submit_postprocess(args, id, check=True)
        get_submit_postprocess(args, id, check=False)

        id += 1
    get_submit_cross_validation_vote(cross_validation_num=args.cross_validation_num, begid=begid,
                                     type=args.change_event)
    return id

def testone():##按默认执行
    # get_data()
    args = parser.parse_args()
    np.random.seed(args.random_seed)
    random.seed(args.random_seed)
    args.do_model = 'mcls'
    schema_labels, predict_data, predict_sents = process_data(args)
    shiyan = """
    mcls
        """
    write_title('./work/log/' + args.do_model + '.txt', args, shiyan)
    args.checkpoint_dir = 'models/' + args.do_model + str(id)
    one(args, schema_labels, predict_data, predict_sents, str(id))

def findlr():
    id=9
    args = parser.parse_args()
    np.random.seed(args.random_seed)
    random.seed(args.random_seed)
    args.do_model = 'role'
    schema_labels, predict_data, predict_sents = process_data(args)

    for lr in [1e-5,5e-5,3e-4,3e-6]:
        if(id<10):
            continue
        args.learning_rate=lr
        args.checkpoint_dir = 'models/' + args.do_model + str(id)
        one(args, schema_labels, predict_data, predict_sents, str(id))
        id+=1

def change_event_label():
    id=40
    args = parser.parse_args()
    np.random.seed(args.random_seed)
    random.seed(args.random_seed)
    args.do_model = 'role'
    # shiyan = """
    # 增加一列：change_event
    #     """
    # write_title('./work/log/' + args.do_model + '.txt', args, shiyan)
    for event_label in ['BIO_event','BIO','no']:
        if(event_label=='BIO_event'):
            args.add_rule=True
        else:
            args.add_rule=False
        args.change_event=event_label
        if(args.use_cross_validation):
            id=cross_validation(args,id)
        else:
            schema_labels, predict_data, predict_sents = process_data(args, i)
            args.checkpoint_dir = 'models/' + args.do_model + str(id)
            one(args, schema_labels, predict_data, predict_sents, str(id))
            get_submit_postprocess(args, id, check=True)
            get_submit_postprocess(args, id, check=False)
            id += 1
if __name__ == "__main__":
    # findlr()
    # id=17
    # testone()
    change_event_label()


import os.path as osp
import glob
import re
import os
import pdb
import json
from collections import defaultdict

import warnings

from .bases import ImageDataset
from ..datasets import DATASET_REGISTRY

def get_pid_info(trackid_associated_list):
    # pair_set = [x.split(' ') for x in info_match]
    # print('pair_set', pair_set[0:3])
    pair_0 = [k for k, v in trackid_associated_list.items() if len(v)>0]
    pair_1 = [v for k, v in trackid_associated_list.items() if len(v)>0]

    track_set = list(trackid_associated_list.keys())

    info_track = defaultdict(list)
    id_map = {}
    id = 0
    for i in range(len(pair_0)):
        # if pair_0[i] == '2019-11-26_07-50-24-562_ee_bb_03_01_00_e7':
        #     print(i, pair_1[i])
        # if id==221:
        #     aaaa=2
        if pair_0[i] not in id_map.keys() and pair_1[i] not in id_map.keys():
            id_map[pair_0[i]] = id
            id_map[pair_1[i]] = id
            info_track[id] += [pair_0[i], pair_1[i]]
            id+=1
        elif pair_1[i] not in id_map.keys():
            id_map[pair_1[i]] = id_map[pair_0[i]]
            info_track[id_map[pair_1[i]]].append(pair_1[i])
        elif pair_0[i] not in id_map.keys():
            id_map[pair_0[i]] = id_map[pair_1[i]]
            info_track[id_map[pair_0[i]]].append(pair_0[i])
        else:
            id_0 = id_map[pair_0[i]]
            id_1 = id_map[pair_1[i]]
            if id_0 != id_1:
                # print('hi')
                info_track[id_0] += info_track[id_1]
                for trackid in info_track[id_1]:
                    id_map[trackid] = id_0
                info_track.pop(id_1, None)

    print('number of IDs after merge', len(info_track))

    for trackid in track_set:
        if trackid not in id_map.keys():
            id_map[trackid] = id
            info_track[id] = [trackid]
            id+=1
    print('number of IDs', len(set(id_map.values())))

    data_info = {}
    data_info['tracklet_pids'] = id_map
    data_info['pid_info'] = info_track

    return data_info

def get_one_sample_list(data_name, data_path, anno_info, track_info, remove_info):

    samples_all = {}

    trackid_associated_list = anno_info['trackid_associated_list']
    category_list = anno_info['category_list']

    for track_id, _ in category_list.items():
        if track_id not in trackid_associated_list.keys():
            trackid_associated_list[track_id] = []
    print(data_name, '#track in anno:', len(category_list))

    frame_set_selected_list = {}
    for track_id, track in track_info.items():
        frame_set_selected_list[track_id] = list(track['frame_info'].keys())

    data_info = get_pid_info(trackid_associated_list)

    pid_info = data_info['pid_info']

    tid_shown = []
    pid_shown = []
    fname_shown = []
    img_set = []
    count = 0
    print(data_name)
    for pid, tid_list in pid_info.items():
        # print(data_name, pid)
        pid_t = '{}#{}'.format(data_name,pid)
        # pid_container.add(pid_t)
        for tid in tid_list:
            track = track_info[tid]
            att = category_list[tid]
            if att != 'client':
                continue
            # print(tid)
            # save_path_att = '{}/{}'.format(save_path, att)
            # if not os.path.exists(save_path_att):
            #     os.mkdir(save_path_att)
            # cid = '{}#{}'.format(data_name, tid.split('_')[2])
            cid = tid.split('_')[2]
            # cid_container.add(cid)
            for fname in frame_set_selected_list[tid]:
                # if 'bodyBboxOfCurrent' not in track['frame_info'][fname].keys():
                #     continue
                # bodyBboxOfCurrent = track['frame_info'][fname]['bodyBboxOfCurrent']
                # samples_all.append((os.path.join(data_path, tid, fname+'.jpg'), pid_t, cid))

                # samples_all.append(os.path.join(data_path, tid, fname+'.jpg'))
                # print(fname)

                if '{}#{}.jpg'.format(tid, fname) in remove_info:
                    continue

                samples_all[fname] = {'img_path': os.path.join(data_path, tid, fname+'.jpg'), 'pid': pid_t, 'cid': cid}
                    # shutil.copyfile(os.path.join(data_path, tid, fname+'.jpg'), os.path.join(save_path_att, '{}#{}#{}#{}.jpg'.format(data_name,pid,tid,fname)))
    # print('finished!!!!!!!!')
    return samples_all

def get_samples(data_root, remove_path, info_path, data_name_set, path_anno):
    samples_all = {}
    for data_name in data_name_set:
        data_path = '{}/{}'.format(data_root, data_name)
        remove_fname = '{}/{}.txt'.format(remove_path, data_name)

        with open('{}/{}_anno.json'.format(path_anno, data_name), 'r') as f:
            anno_info = json.load(f)

        with open('{}/{}.json'.format(info_path, data_name), 'r') as f:
            track_info = json.load(f)
        remove_info = []
        if os.path.exists(remove_fname):
            with open(remove_fname) as f:
                remove_info = f.read().splitlines()

        sample_one = get_one_sample_list(data_name, data_path, anno_info, track_info, remove_info)

        samples_all.update(sample_one)

    return samples_all

def get_samples_val(data_path_val, data_name, path_anno):

    data_path = '{}/{}'.format(data_path_val, data_name)
    remove_fname = '/hddc/ywu/data/blf/reid/sample_remove_list/{}.txt'.format(data_name)

    with open('{}/{}_anno.json'.format(path_anno, data_name), 'r') as f:
        anno_info = json.load(f)

    with open('/hddc/ywu/data/blf/reid/track_info/{}.json'.format(data_name), 'r') as f:
        track_info = json.load(f)
    remove_info = []
    if os.path.exists(remove_fname):
        with open(remove_fname) as f:
            remove_info = f.read().splitlines()

    sample_one = get_one_sample_list(data_name, data_path, anno_info, track_info, remove_info)

    return sample_one

def get_pid_cid(samples):
    pid_container = set()
    cid_container = set()
    for k, info in samples.items():

        pid_container.add(info['pid'])
        cid_container.add(info['cid'])

    return pid_container, cid_container

def get_all_sample(val_dataname, query_cam):
    date = '2020-06-25'
    data_name_set = ['100000058', '100000078', '100000181', '100000199', '100000225', '100000228', '100000282', '100000298', '100000327', '100003003', '100019002', '123000086', '123000155', '123001073']
    data_name_set = ['{}_{}'.format(x,date) for x in data_name_set]

    data_name_set_2 = ['100000007', '100000305', '100000318', '100002002', '100002006', '100002009', '100009001', '100022002', '100075009', '100076003',
    '100079002', '100079033', '123000081', '123000096']
    data_name_set_2 = ['{}_{}'.format(x,date) for x in data_name_set_2]

    data_name_set += data_name_set_2

    date_3 = '2020-07-16'
    data_name_set_3 = ['100000008', '100000526', '100003002', '100008003', '100033001', '100079008']
    data_name_set_3 = ['{}_{}'.format(x,date_3) for x in data_name_set_3]
    data_name_set_3 += ['100001107_2020-07-23']

    # val_dataname = '100000027_2020-03-17'
    data_root = '/hddc/ywu/data/blf/reid/2020-06-25'
    info_path = '/hddc/ywu/data/blf/reid/track_info'
    remove_path = '/hddc/ywu/data/blf/reid/sample_remove_list'

    data_root_val = '/hddc/ywu/data/blf/reid'

    path_anno = '/hddc/ywu/data/blf/reid/annotation'

    pid_container = set()
    pid_train_container = set()
    cid_container = set()

    # samples_train = get_samples_train(data_name_set, path_anno)
    # samples_val = get_samples_val(data_path_val, val_dataname, path_anno)
    samples_train = get_samples(data_root, remove_path, info_path, data_name_set, path_anno)#get_samples(data_name_set, path_anno)
    samples_train_2 = get_samples(data_root_val, remove_path, info_path, data_name_set_3, path_anno)
    samples_train.update(samples_train_2)

    samples_val = get_samples(data_root_val, remove_path, info_path, val_dataname, path_anno)

    pid_train_container, cid_container = get_pid_cid(samples_train)

    pid2label_train = {pid:label for label, pid in enumerate(pid_train_container)}
    cid2label = {cid:label for label, cid in enumerate(cid_container)}

    train, query, gallery = [], [], []

    for k, sample_info in samples_train.items():
        img_path = sample_info['img_path']
        pid = sample_info['pid']
        cid = sample_info['cid']
        train.append((img_path, pid2label_train[pid], cid2label[cid]))

    pid_container_val, cid_container_val = get_pid_cid(samples_val)
    pid2label_val = {pid:label for label, pid in enumerate(pid_container_val)}
    cid2label_val = {cid:label for label, cid in enumerate(cid_container_val)}
    for k, sample_info in samples_val.items():
        img_path = sample_info['img_path']
        pid = sample_info['pid']
        cid = sample_info['cid']
        # print(cid, query_cam)
        # pdb.set_trace()
        # train.append((img_path, pid2label_train[pid], cid2label[cid]))
        if cid in query_cam:
            query.append((img_path, pid2label_val[pid], cid2label_val[cid]))
        else:
            gallery.append((img_path, pid2label_val[pid], cid2label_val[cid]))

    return train, query, gallery

@DATASET_REGISTRY.register()
class Fangdao_Fanhua_35stores(ImageDataset):
    dataset_dir = ''
    def __init__(self, root='datasets', **kwargs):

        cam_info = {'100000027':{'pos':['ee:bb:03:01:00:c3', 'ee:bb:03:01:02:98'], 'door': ['ee:ee:bb:03:10:05']},#位置重复  保留05
            '100001071':{'pos':['ee:bb:03:01:00:b2', 'ee:bb:03:01:03:37', 'ee:bb:03:01:02:93'], 'door': ['ee:bb:03:01:02:ad']},
            '100000159':{'pos':['ee:bb:03:01:00:e7', 'ee:bb:03:01:02:40'], 'door': ['ee:bb:03:01:01:c6']},
            '100001093':{'pos':['ee:bb:03:01:03:9a', 'ee:bb:03:01:03:31', 'ee:bb:03:01:02:1b', 'ee:bb:03:01:03:25', 'ee:bb:03:01:00:ce', 'ee:bb:03:01:03:05', 'ee:bb:03:01:03:93'], 'door': ['ee:bb:03:01:02:b3', 'ee:bb:03:01:02:00']},
            '100001082':{'pos':['ee:bb:03:01:01:13', 'ee:bb:03:01:03:71', 'ee:bb:03:01:01:cf'], 'door': []}}

        # self.dataset_dir = data_dir

        # fname_list = os.listdir(data_dir)
        val_dataname = ['100000027_2020-03-17']
        # val_dataname = '100001071_2020-02-18'

        query_cam = []
        for name in val_dataname:
            val_storeid = name.split('_')[0]
            query_cam += cam_info[val_storeid]['pos']
        query_cam = [x.replace(':', '-') for x in query_cam]

        train, query, gallery = get_all_sample(val_dataname, query_cam)

        # pdb.set_trace()

        self.train = train
        self.query = query
        self.gallery = gallery

        super(Fangdao_Fanhua_35stores, self).__init__(train, query, gallery, **kwargs)


from selenium.webdriver import Firefox
from time import sleep # importando sleep para que o selenium espere antes de procurar em elemento

booklist = 'https://my-simple-book-list.herokuapp.com'

browser = Firefox()

browser.get(booklist)

# sleep(3)

botao = browser.find_element_by_tag_name('button')
botao.click()

sleep(1)
botaocancel = browser.find_element_by_class_name('cancel')

botaocancel.click()

# browser.quit()


from keras.models import Model
from keras.layers import Input, Dense, Dropout, BatchNormalization, concatenate
from keras.layers.convolutional import Conv2D, MaxPooling2D, AveragePooling2D

class GoogLeNet(Model):
    def __init__(self):
        super().__init__()

        input = Input(shape=(224, 224, 3))
        layer = self.Conv2d_BN(input, 64, (7, 7), strides=(2, 2), padding='same')
        layer = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding='same')(layer)
        layer = self.Conv2d_BN(layer, 192, (3, 3), strides=(1, 1), padding='same')
        layer = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding='same')(layer)
        layer = self.Inception(layer, 64)  # 256
        layer = self.Inception(layer, 120)  # 480
        layer = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding='same')(layer)
        layer = self.Inception(layer, 128)  # 512
        layer = self.Inception(layer, 128)
        layer = self.Inception(layer, 128)
        layer = self.Inception(layer, 132)  # 528
        layer = self.Inception(layer, 208)  # 832
        layer = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding='same')(layer)
        layer = self.Inception(layer, 208)
        layer = self.Inception(layer, 256)  # 1024
        layer = AveragePooling2D(pool_size=(7, 7), strides=(7, 7), padding='same')(layer)
        layer = Dropout(0.4)(layer)
        layer = Dense(1000, activation='relu')(layer)
        layer = Dense(1000, activation='softmax')(layer)

        model = Model(input, layer, name='Inception')

        model.compile(loss='categorical_crossentropy', optimizer='SGD', metrics=['accuracy'])
        model.summary()

    def Conv2d_BN(self, layer, nb_filter, kernel_size, padding='same', strides=(1, 1), name=None):
        if name is not None:
            bn_name = name + '_bn'
            conv_name = name + '_conv'
        else:
            bn_name = None
            conv_name = None

        layer = Conv2D(nb_filter, kernel_size, padding=padding, strides=strides, activation='relu', name=conv_name)(
            layer)
        layer = BatchNormalization(axis=3, name=bn_name)(layer)

        return layer

    def Inception(self, layer, nb_filter):
        branch1x1 = self.Conv2d_BN(layer, nb_filter, (1, 1), padding='same', strides=(1, 1), name=None)

        branch3x3 = self.Conv2d_BN(layer, nb_filter, (1, 1), padding='same', strides=(1, 1), name=None)
        branch3x3 = self.Conv2d_BN(branch3x3, nb_filter, (3, 3), padding='same', strides=(1, 1), name=None)

        branch5x5 = self.Conv2d_BN(layer, nb_filter, (1, 1), padding='same', strides=(1, 1), name=None)
        branch5x5 = self.Conv2d_BN(branch5x5, nb_filter, (1, 1), padding='same', strides=(1, 1), name=None)

        branchpool = MaxPooling2D(pool_size=(3, 3), strides=(1, 1), padding='same')(layer)
        branchpool = self.Conv2d_BN(branchpool, nb_filter, (1, 1), padding='same', strides=(1, 1), name=None)

        layer = concatenate([branch1x1, branch3x3, branch5x5, branchpool], axis=3)

        return layer


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

"""Main module. Launch by running python -m rest_uploader.cli"""

import os
import platform
import time
import base64
import mimetypes
import json
import requests
import csv
from tabulate import tabulate
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
from .img_process import (
    extract_text_from_image,
    extract_text_from_pdf,
    pdf_page_to_image,
    pdf_valid,
)
from .settings import SERVER, JOPLIN_NOTEBOOK, TEMP_PATH
from .api_token import get_token_suffix
from pathlib import Path

"""
2018-09-24 JRK
This program was created to upload files from a folder specified in the
PATH variable to Joplin. The following resource was helpful in figuring out
the logic for Watchdog:
https://stackoverflow.com/questions/18599339/python-watchdog-monitoring-file-for-changes

Tested with the following extensions:
.md
.txt
.pdf
.png
.jpg
.url

Caveat
Uploader only triggered upon new file creation, not modification
"""

class MyHandler(FileSystemEventHandler):
    def _event_handler(self, path):
        filename, ext = os.path.splitext(path)
        if ext not in (".tmp", ".crdownload") and ext[:2] not in (".~"):
            for i in range(10):
                filesize = os.path.getsize(path)
                if filesize < 1 or (ext == ".pdf" and not pdf_valid(path)):
                    print("Incomplete file. Retrying...")
                    if i == 9:
                        print("timeout error, invalid file")
                        return False
                    time.sleep(5)
                elif filesize > 10000000:
                    print(f"Filesize = {filesize}. Too big for Joplin, skipping upload")
                    break
                else:
                    upload(path)
                    return True
        else:
            print("Detected temp file. Temp files are ignored.")

    def on_created(self, event):
        print(event.event_type + " -- " + event.src_path)
        self._event_handler(event.src_path)

    def on_moved(self, event):
        print(event.event_type + " -- " + event.dest_path)
        self._event_handler(event.dest_path)

# Set working Directory
def set_working_directory():
    if os.getcwd() != os.chdir(os.path.dirname(os.path.realpath(__file__))):
        os.chdir(os.path.dirname(os.path.realpath(__file__)))

def set_token():
    global TOKEN
    TOKEN = get_token_suffix()

def read_text_note(filename):
    with open(filename, "r") as myfile:
        text = myfile.read()
        print(text)
    return text

def read_csv(filename):
    return csv.DictReader(open(filename))

def get_notebook_id():
    # Find the ID of the destination folder
    # adapted logic from jhf2442 on Joplin forum
    # https://discourse.joplin.cozic.net/t/import-txt-files/692
    res = requests.get(SERVER + "/folders" + TOKEN)
    folders = res.json()

    notebook_id = 0
    for folder in folders:
        if folder.get("title") == JOPLIN_NOTEBOOK:
            notebook_id = folder.get("id")
    if notebook_id == 0:
        for folder in folders:
            if "children" in folder:
                for child in folder.get("children"):
                    if child.get("title") == JOPLIN_NOTEBOOK:
                        notebook_id = child.get("id")
    return notebook_id

def create_resource(filename):
    basefile = os.path.basename(filename)
    title = os.path.splitext(basefile)[0]
    files = {
        "data": (json.dumps(filename), open(filename, "rb")),
        "props": (None, '{{"title":"{}", "filename":"{}"}}'.format(title, basefile)),
    }
    response = requests.post(SERVER + "/resources" + TOKEN, files=files)
    print(response.json())
    return response.json()

def delete_resource(resource_id):
    apitext = SERVER + "/resources/" + resource_id + TOKEN
    response = requests.delete(apitext)
    return response

def get_resource(resource_id):
    apitext = SERVER + "/resources/" + resource_id + TOKEN
    response = requests.get(apitext)
    return response

def encode_image(filename, datatype):
    encoded = base64.b64encode(open(filename, "rb").read())
    img = "data:{};base64,{}".format(datatype, encoded.decode())
    return img

def set_json_string(title, notebook_id, body, img=None):
    if img is None:
        return '{{ "title": {}, "parent_id": "{}", "body": {} }}'.format(
            json.dumps(title), notebook_id, json.dumps(body)
        )
    else:
        return '{{ "title": "{}", "parent_id": "{}", "body": {}, "image_data_url": "{}" }}'.format(
            title, notebook_id, json.dumps(body), img
        )

def upload(filename):
    """ Get the default Notebook ID and process the passed in file"""
    notebook_id = get_notebook_id()
    basefile = os.path.basename(filename)
    title, ext = os.path.splitext(basefile)
    body = basefile + " uploaded from " + platform.node() + "\n"
    datatype = mimetypes.guess_type(filename)[0]
    if datatype is None:
        # avoid subscript exception if datatype is None
        if ext in (".url", ".lnk"):
            datatype = "text/plain"
        else:
            datatype = ""
    if datatype == "text/plain":
        body += read_text_note(filename)
        values = set_json_string(title, notebook_id, body)
    if datatype == "text/csv":
        table = read_csv(filename)
        body += tabulate(table, headers="keys", numalign="right", tablefmt="pipe")
        values = set_json_string(title, notebook_id, body)
    elif datatype[:5] == "image":
        img = encode_image(filename, datatype)
        body += "\n<!---\n"
        body += extract_text_from_image(filename)
        body += "\n-->\n"
        values = set_json_string(title, notebook_id, body, img)
    else:
        response = create_resource(filename)
        body += "[{}](:/{})".format(basefile, response["id"])
        values = set_json_string(title, notebook_id, body)
        if response["file_extension"] == "pdf":
            # Special handling for PDFs
            body += "\n<!---\n"
            body += extract_text_from_pdf(filename)
            body += "\n-->\n"
            previewfile = TEMP_PATH + "preview.png"
            if not os.path.exists(previewfile):
                previewfile = pdf_page_to_image(filename)
            img = encode_image(previewfile, "image/png")
            os.remove(previewfile)
            values = set_json_string(title, notebook_id, body, img)

    response = requests.post(SERVER + "/notes" + TOKEN, data=values)
    print(response)
    print(response.text)
    print(response.json())

def watcher(path=None):
    set_working_directory()
    set_token()
    if path is None:
        path = str(Path.home())

    event_handler = MyHandler()
    print("Monitoring directory {} for files".format(path))
    observer = Observer()
    observer.schedule(event_handler, path=path, recursive=False)
    observer.start()

    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()

if __name__ == "__main__":
    watcher()


from __init__ import *
from utils import search_utils
from utils import api_utils

# rest api
from kelp_plugins import kelp_plugin_repo

@app.route("/favicon")
@app.route("/favicon.ico")
@app.route("/favicon.png")
def asset_favicon():
    return send_from_directory(f"{app.root_path}/static/", "favicon.png", mimetype="image/vnd.microsoft.icon")

@app.route("/clear-cookies", defaults={"mode": "json"})
@app.route("/clear-cookies/<mode>")
def clear_cookies(mode):
    cookies = request.cookies.items()

    cookie_list = list()
    for cookie in cookies:
        cookie_list.append({"name": cookie[0], "value": cookie[1]})

    if mode == "json":
        data = {"cleared_cookies": cookie_list}
    else:
        data = render_template(
            "cookies-cleared.html",
            cleared_cookies=cookie_list
        )
    response = make_response(data, StopCodes.Success.OK)

    for cookie in cookies:
        response.delete_cookie(cookie[0])

    return response

@app.route("/search_engine")
def search_engine():
    sorted_return = list()

    search = request.args.get("search")

    if search:
        for hit in search_utils.search_all_by_string(search):
            sorted_return.append(hit)

    if "q-search" in request.args:
        return render_template(
            "search_nav.html",
            hits=sorted_return
        )

    return api_utils.craft_response(
        sorted_return, 200
    )

# rest api

@app.route("/api/plugins/tags")
def api_plugin_tags():
    return {"tags": kelp_plugin_repo.get_all_tags()}


import time
import mediapipe as mp
import os
import cv2

class handDetector():
    def __init__(self,mode=False,is_authenticated=True,maxHands=2,detection_confidence=0.5,tracking_confidence=0.5):
        self.mode = mode
        self.maxHands = maxHands
        self.detection_confidence= detection_confidence
        self.tracking_confidence = tracking_confidence
        self.mp_hands = mp.solutions.hands
        self.hands = self.mp_hands.Hands(self.mode,self.maxHands,self.detection_confidence,self.tracking_confidence)
        self.mp_draw= mp.solutions.drawing_utils

    def Find_hands(self, img, draw=True):
        img_RGB = cv2.cvtColor( img, cv2.COLOR_BGR2RGB)
        self.results = self.hands.process(img_RGB)
        if self.results.multi_hand_landmarks:
            for hand_marks in self.results.multi_hand_landmarks:
               if draw:
                   self.mp_draw.draw_landmarks(img,hand_marks, self.mp_hands.HAND_CONNECTIONS )
        return img

    def Find_position(self, img, hand_no = 0, draw= True):
        self.land_mark_list =[]
        if self.results.multi_hand_landmarks:
            my_hand = self.results.multi_hand_landmarks[hand_no]
            for id, land_mark in enumerate(my_hand.landmark):
                 h,w,c = img.shape
                 center_x, center_y = int(land_mark.x*w), int(land_mark.y*h)
                 self.land_mark_list.append([id , center_x, center_y])
                 if draw:
                     cv2.circle(img, (center_x,center_y), 5, (255,55,55), cv2.FILLED)
        return self.land_mark_list

wCam , hCam = 640, 480
cap = cv2.VideoCapture(0)
cap.set(3, wCam)
cap.set(4, hCam)

folder_path = "nu"
my_list = os.listdir(folder_path)
print(my_list)

over_lay_list=[]
for img_path in my_list:
    image = cv2.imread(f'{folder_path}/{img_path}')
    print(f'{folder_path}/{img_path}')
    over_lay_list.append(image)
    print(len(over_lay_list))

print(len(over_lay_list))
previous_time = 0

detector = handDetector(detection_confidence=0.5)
Tip_IDS= [4, 8,12, 16,20]

while True:
   success, img = cap.read()
   img = detector.Find_hands(img)
   landmark_List = detector.Find_position(img,draw = False)

   if len(landmark_List)!=0 :
       Figures = []

       #thumb
       if landmark_List[Tip_IDS[0]][1]>landmark_List[Tip_IDS[0]-1][1]:
             Figures.append(1)
       else :
           Figures.append(0)

    #for 4 fing
       for id in range(1,5):
           if landmark_List[Tip_IDS[id]][2]<landmark_List[Tip_IDS[id]-2][2]:
             Figures.append(1)
           else :
               Figures.append(0)
       total_figure= Figures.count(1)
       print(total_figure)

       h, w, c = over_lay_list[total_figure-0].shape
       img[0:h,0:w]=over_lay_list[total_figure-0]
       cv2.rectangle(img,(20,225),(170,425),(0,255,0),cv2.FILLED)
       cv2.putText(img, str(total_figure), (45,375), cv2.FONT_HERSHEY_PLAIN,10,(255,89,45),25)

   current_time = time.time()
   fps = 1 / (current_time-previous_time)
   previous_time = current_time
   cv2.putText(img, f'FPS: {int(fps)}',(400, 55), cv2.FONT_HERSHEY_PLAIN , 3, (255,12,12),3)

   cv2.imshow("Image",img)
   if cv2.waitKey(1) & 0xFF == ord('q'):
       break

cv2.destroyAllWindows()


# import os
#
# from celery import Celery
#
# # set the default Django settings module for the 'celery' program.
# os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings')
#
# app = Celery('proj')
#
# # Using a string here means the worker doesn't have to serialize
# # the configuration object to child processes.
# # - namespace='CELERY' means all celery-related configuration keys
# #   should have a `CELERY_` prefix.
# app.config_from_object('django.conf:settings', namespace='CELERY')
#
# # Load task modules from all registered Django app configs.
# app.autodiscover_tasks()    # 发现所有tasks.py
#
#
# @app.task(bind=True)
# def debug_task(self):
#     print(f'Request: {self.request!r}')

# -*-coding=utf-8-*-

from __future__ import absolute_import

from celery import Celery

from kombu import Queue

app = Celery("cele",
             broker="redis://192.168.56.102:6379/1",
             include=['hotplay_task']
             )

app.conf.update(
    CELERY_DEFAULT_QUEUE='hotplay_sh_default_queue',
    # CELERY_QUEUES = (Queue('hotplay_jy_queue'),),  #该队列是给server2用的，并不需要在这里申明

)


from scipy.ndimage import affine_transform
import numpy as np
import nibabel as nib
from nilearn.plotting import plot_img
from matplotlib import pyplot as plt

# x = nib.load("Task04_Hippocampus/imagesTr/hippocampus_001.nii.gz")
# image = x.get_fdata()
# M = np.random.random((4,4))
# M[3] = [0,0,0,1]
# image_tr = affine_transform(image,M)
# new_image = nib.Nifti1Image(image_tr, affine=np.eye(4))
# new_image.to_filename("deformed.nii")
# plot_img("Task04_Hippocampus/imagesTr/hippocampus_001.nii.gz")
# plot_img("deformed.nii")
# plt.show()

image  = np.random.random((3,3,3))*100
image = np.around(image)
M = np.eye(4)
M[0][0] = 1.5

print("TRANSFORMATION MATRIX")
print(M)
print("")

image_tr = affine_transform(image,M, mode ="nearest")
print("INPUT IMAGE")
print(image)

print("")
print("OUTPUT IMAGE")
print(image_tr)

import cv2
from scipy.ndimage.interpolation import map_coordinates
from scipy.ndimage.filters import gaussian_filter

def elastic_transform(image,alpha, sigma):
    shape = image.shape
    random_state = np.random.RandomState(None)
    dx = gaussian_filter((random_state.rand(*shape)*2 - 1),sigma)*alpha
    dy = gaussian_filter((random_state.rand(*shape)*2 - 1),sigma)*alpha
    dz = np.zeros_like(dx)
    x,y,z = np.meshgrid(np.arange(shape[0]), np.arange(shape[1]), np.arange(shape[2]))
    indices = np.reshape(y + dy,(-1,1)),np.reshape(x + dx,(-1,1)), np.reshape(z,(-1,1))
    return map_coordinates(image,indices).reshape(shape)

# affine transformation
# elastic_transform


# encoding: UTF-8
# Autor: Silvia Ferman
# VIDEOJUEGO

import pygame
from random import randint

# DIMENSIONES PANTALLA:
ANCHO = 800
ALTO = 600

# COLORES_
BLANCO = (255, 255, 255)
VERDE_BANDERA = (27, 94, 32)
ROJO = (255, 0, 0)
AZUL = (0, 0, 255)
NEGRO = (0, 0, 0)
MORADO = (74, 35, 90)
ROSA = (255, 93, 133)
NARANJA = (255, 83, 13)

# ESTADOS_
MENU = 1
JUGANDO = 2
REGLAS = 3
RANK = 4
GAMEOVER = 5

# ESTADOS DE MOVIMIENTO
QUIETO = 1
ABAJO = 2
ARRIBA = 3
ATRAS = 4
ADELANTE = 5

# Estructura básica de un programa que usa pygame para dibujar

# Funcion que dibuja al PERSONAJE (Suricata)
def dibujarSuricata(ventana, spritePersonaje):
    ventana.blit(spritePersonaje.image, spritePersonaje.rect)

# Funcion que dibuja la ROCA (Game Over)
def dibujarRoca(ventana, spriteRoca):
    ventana.blit(spriteRoca.image, spriteRoca.rect)
    spriteRoca.rect.left -= 5
    if spriteRoca.rect.left <= -spriteRoca.rect.width:
        spriteRoca.rect.left = ANCHO + randint(20, 50)

# Funcion que dibuja a los ENEMIGOS (Cactus)
def dibujarCactus(ventana, listaEnemigos):
    # VISITAR/ACCEDER DIRECTAMENTE a cada elemento
    for enemigo in listaEnemigos:
        ventana.blit(enemigo.image, enemigo.rect)

# Funcion que mueve a los ENEMIGOS (Cactus)
def moverCactus(listaEnemigos):
    for enemigo in listaEnemigos:
        enemigo.rect.left -= 2

# Funcion que dibuja a los MISILES (Balas)
def dibujarMisiles(ventana, listaMisil):
    for bala in listaMisil:
        ventana.blit(bala.image, bala.rect)

# Funcion que mueve a los MISILES (Balas)
def moverMisiles(listaMisil):
    for bala in listaMisil:
        bala.rect.left += 10

# Funcion que dibuja el MENU
def dibujarMenu(ventana, imgButtonPlay, imgButtonReglas, imgButtonRank):
    ventana.blit(imgButtonPlay, (ANCHO // 2 - 128 , ALTO // 3 - 128))
    ventana.blit(imgButtonReglas, (ANCHO // 2 - 128, ALTO // 3 + 25))
    ventana.blit(imgButtonRank, (ANCHO // 2 - 128, ALTO // 3 + 175))

# Funcion que dibuja el fondo cuando pierdes (GAME OVER)
def dibujarGameOver(ventana, imgGameOver):
    ventanaJuego = ventana
    imagenPerdio = imgGameOver
    ventanaJuego.blit(imagenPerdio, (0,0))

def verificarBoom(listaEnemigos, listaMisil, imgEnemigo, imgEnemigoUno, imgEnemigoDos):
    puntosGanados = 0
    for k in range(len(listaMisil)-1, -1, -1):
        misil = listaMisil[k]
        for e in range(len(listaEnemigos)-1, -1, -1):  # Recorrer con INDICES
            cactus = listaEnemigos[e]
            # bala vs enemigo
            xb = misil.rect.left
            yb = misil.rect.bottom
            xe, ye, ae, alte = cactus.rect
            if xb >= xe and xb <= xe + ae and yb >= ye and yb <= ye + alte :
                # hit
                if cactus.image == imgEnemigo:
                    puntosGanados += 100
                if cactus.image == imgEnemigoUno:
                    puntosGanados += 35
                if cactus.image == imgEnemigoDos:
                    puntosGanados += 75
                listaEnemigos.remove(cactus)
                listaMisil.remove(misil)
                break
    return puntosGanados

def verificarGameOver(listaRocas, spritePersonaje):
    personaje = spritePersonaje
    for e in range(len(listaRocas) - 1, -1, -1):
        roca = listaRocas[e]
        # personaje vs roca
        xb = roca.rect.left
        yb = roca.rect.bottom
        xe, ye, ae, alte = personaje.rect
        if xb >= xe and xb <= xe + ae and yb >= ye and yb <= ye + alte:
            listaRocas.remove(roca)
            break

def dibujarPuntajes(ventana, puntosGanados):
    puntos = "PUNTOS ="
    pygame.font.init()
    fuente = pygame.font.Font(None, 100)
    textoPuntos = fuente.render(puntos, 1, (255, 83, 13), )
    ventana.blit(textoPuntos, (80, 250))

    total = str(puntosGanados)
    pygame.font.init()
    fuente = pygame.font.Font(None, 100)
    textoPuntos = fuente.render(total, 1, (255, 83, 13), )
    ventana.blit(textoPuntos, (450, 250))

def dibujarTop(ventana):
    top = "HIGH SCORE:"
    pygame.font.init()
    fuente = pygame.font.Font(None, 100)                     # Tamaño letra
    textoTop = fuente.render(top, 1, (0,0,0), )              # Color
    ventana.blit(textoTop, (30, 200))                        # Posición

    entrada = open("Puntos.txt", "r")

    puntoAnterior = entrada.readline()

    entrada.close()
    anterior = puntoAnterior
    pygame.font.init()
    fuente = pygame.font.Font(None, 200)
    textoTop = fuente.render(anterior, 1, (0, 0, 0), )
    ventana.blit(textoTop, (250, 375))

def escribirTop(puntosGanados):
    entrada = open("Puntos.txt", "r")

    puntoAnterior = int(entrada.readline())

    entrada.close()

    total = puntosGanados
    if puntoAnterior < total:
        salida = open("Puntos.txt", "w")
        salida.write(str(total))

def dibujar():
    pygame.init()
    # Crea una ventana de ANCHO x ALTO
    ventana = pygame.display.set_mode((ANCHO, ALTO))  # Crea la ventana donde dibujará
    reloj = pygame.time.Clock()  # Para limitar los fps
    termina = False  # Bandera para saber si termina la ejecución, iniciamos suponiendo que no

    # ROCA
    x = ANCHO
    listaRocas = []
    imgRoca = pygame.image.load("roca.png")
    spriteRoca = pygame.sprite.Sprite()
    spriteRoca.image = imgRoca
    spriteRoca.rect = imgRoca.get_rect()
    spriteRoca.rect.left = x
    x += 140 + randint(0, 55)
    spriteRoca.rect.bottom = ALTO - 25
    listaRocas.append(spriteRoca)

    # PERSONAJE
    imgPersonaje = pygame.image.load("suricata2.png")
    spritePersonaje = pygame.sprite.Sprite()
    spritePersonaje.image = imgPersonaje
    spritePersonaje.rect = imgPersonaje.get_rect()
    spritePersonaje.rect.left = 0
    spritePersonaje.rect.bottom = ALTO

    # ENEMIGOS
    x = ANCHO
    listaEnemigos = []
    imgEnemigo = pygame.image.load("cactus.png")
    imgEnemigoUno = pygame.image.load("Cactus2A.png")
    imgEnemigoDos = pygame.image.load("Cactus3A.png")
    for k in range(50):
        spriteEnemigo = pygame.sprite.Sprite()
        spriteEnemigoUno = pygame.sprite.Sprite()
        spriteEnemigoDos = pygame.sprite.Sprite()
        spriteEnemigo.image = imgEnemigo
        spriteEnemigoUno.image = imgEnemigoUno
        spriteEnemigoDos.image = imgEnemigoDos
        spriteEnemigo.rect = imgEnemigo.get_rect()
        spriteEnemigoUno.rect = imgEnemigoUno.get_rect()
        spriteEnemigoDos.rect = imgEnemigoDos.get_rect()
        spriteEnemigo.rect.left = x
        x += 140 + randint(0, 50)
        spriteEnemigoUno.rect.left = x
        x += 140 + randint(0, 35)
        spriteEnemigoDos.rect.left = x
        x += 140 + randint(0, 75)
        spriteEnemigo.rect.bottom = ALTO
        spriteEnemigoUno.rect.bottom = ALTO - 10
        spriteEnemigoDos.rect.bottom = ALTO - 10
        listaEnemigos.append(spriteEnemigo)
        listaEnemigos.append(spriteEnemigoUno)
        listaEnemigos.append(spriteEnemigoDos)

    # PROYECTILES / BALAS
    listaMisil = []
    imgMisil = pygame.image.load("misil.png")

    # MENU
    imgButtonPlay = pygame.image.load("buttonPlay.png")
    imgButtonReglas = pygame.image.load("buttonReglas.png")
    imgButtonRank = pygame.image.load("buttonRanking.png")
    imgGameOver = pygame.image.load("Game Over.png")

    # IMAGENES
    imgFondo = pygame.image.load("desierto.JPG")
    imgReglas = pygame.image.load("reglas.jpg")
    imgRank = pygame.image.load("ranking.jpg")

    estado = MENU

    moviendo = QUIETO

    xFondo = 0

    puntosGanados = 0

    # TIEMPOS
    timer = 0       # Acumulador de tiempo

    # TEXTO
    fuente = pygame.font.SysFont("monospace", 25)

    # AUDIO
    pygame.mixer.init()
    pygame.mixer.music.load("BACKGROUND.mp3")   # archivos largos (mp3)
    pygame.mixer.music.play(-1)

    efecto = pygame.mixer.Sound("shoot.wav")     # archivos cortos

    while not termina:  # Ciclo principal, MIENTRAS la variable termina sea False, el ciclo se repite automáticamente
        # Procesa los eventos que recibe
        for evento in pygame.event.get():
            if evento.type == pygame.QUIT:  # El usuario hizo click en el botón de salir
                termina = True      # Queremos terminar el ciclo
            elif evento.type == pygame.KEYDOWN:
                if evento.key == pygame.K_UP:
                    #spritePersonaje.rect.bottom -= 5    #  Velocidad mov. arriba
                    moviendo = ARRIBA
                elif evento.key == pygame.K_DOWN:
                    #spritePersonaje.rect.bottom += 5    # Velocidad mov. abajo
                    moviendo = ABAJO
                elif evento.key == pygame.K_LEFT:
                    moviendo = ATRAS
                elif evento.key == pygame.K_RIGHT:
                    moviendo = ADELANTE
                elif evento.key == pygame.K_SPACE:
                    # Crear una bala
                    spriteBala = pygame.sprite.Sprite()
                    spriteBala.image = imgMisil
                    spriteBala.rect = imgMisil.get_rect()
                    spriteBala.rect.left = spritePersonaje.rect.left + spritePersonaje.rect.width - 30
                    spriteBala.rect.bottom = spritePersonaje.rect.bottom - 80
                    listaMisil.append(spriteBala)
                    # SONIDO Boom
                    efecto.play()
            elif evento.type == pygame.KEYUP:
                moviendo = QUIETO
            elif evento.type == pygame.MOUSEBUTTONUP:
                xm, ym = pygame.mouse.get_pos()         # dupla: se regresan DOS variables
                print(xm, " , ", ym)
                # Preguntar si solto el mouse dentro del boton (BOTON JUGAR)
                xb = ANCHO // 2 - 128
                yb = ALTO // 3 - 128
                if xm >= xb and xm <= xb + 256 and ym >= yb and ym <= yb + 100:
                    estado = JUGANDO
                # Preguntar si solto el mouse dentro del boton (BOTON REGLAS)
                xb = ANCHO // 2 - 128
                yb = ALTO // 3 + 25
                if xm >= xb and xm <= xb + 256 and ym >= yb and ym <= yb + 100:
                    estado = REGLAS
                # Preguntar si solto el mouse dentro del boton (BOTON RANK)
                xb = ANCHO // 2 - 128
                yb = ALTO // 3 + 178
                if xm >= xb and xm <= xb + 256 and ym >= yb and ym <= yb + 100:
                    estado = RANK

        # Borrar pantalla
        ventana.fill(NEGRO)

        if estado == JUGANDO:
            # TIEMPOS
            if timer >= 2:
                timer = 0
                # Enemigos
                spriteEnemigo = pygame.sprite.Sprite()
                spriteEnemigoUno = pygame.sprite.Sprite()
                spriteEnemigoDos = pygame.sprite.Sprite()
                spriteEnemigo.image = imgEnemigo
                spriteEnemigoUno.image = imgEnemigoUno
                spriteEnemigoDos.image = imgEnemigoDos
                spriteEnemigo.rect = imgEnemigo.get_rect()
                spriteEnemigoUno.rect = imgEnemigoUno.get_rect()
                spriteEnemigoDos.rect = imgEnemigoDos.get_rect()
                spriteEnemigo.rect.left = x
                x += 140 + randint(0, 50)
                spriteEnemigoUno.rect.left = x
                x += 140 + randint(0, 35)
                spriteEnemigoDos.rect.left = x
                x += 140 + randint(0, 75)
                spriteEnemigo.rect.bottom = ALTO
                spriteEnemigoUno.rect.bottom = ALTO - 10
                spriteEnemigoDos.rect.bottom = ALTO - 10
                listaEnemigos.append(spriteEnemigo)
                listaEnemigos.append(spriteEnemigoUno)
                listaEnemigos.append(spriteEnemigoDos)

            # Actualizar ENEMIGOS / BALAS
            # MOVER Personaje
            if moviendo == ARRIBA:
                spritePersonaje.rect.bottom -= 15        # se mueve solo ARRIBA
            elif moviendo == ABAJO:
                spritePersonaje.rect.bottom += 15        # se mueve solo ABAJO
            elif moviendo == ATRAS:
                spritePersonaje.rect.left -= 5
            elif moviendo == ADELANTE:
                spritePersonaje.rect.left += 5
            moverCactus(listaEnemigos)
            moverMisiles(listaMisil)
            pg = verificarBoom(listaEnemigos, listaMisil, imgEnemigo, imgEnemigoUno, imgEnemigoDos)
            puntosGanados += pg
            verificarGameOver(listaRocas, spritePersonaje)
            # GAME OVER
            if len(listaRocas) == 0:
                estado = GAMEOVER

            # Dibujar, aquí haces todos los trazos que requieras
            ventana.blit(imgFondo, (xFondo, 0))
            ventana.blit(imgFondo, (xFondo + ANCHO, 0))
            xFondo -= 1
            if xFondo == -ANCHO:
                xFondo = 0
            dibujarSuricata(ventana, spritePersonaje)
            dibujarRoca(ventana, spriteRoca)
            dibujarCactus(ventana, listaEnemigos)
            dibujarMisiles(ventana, listaMisil)
        elif estado == MENU:
            # Dibujar MENU
            dibujarMenu(ventana, imgButtonPlay, imgButtonReglas, imgButtonRank)
        elif estado == REGLAS:
            ventana.blit(imgReglas, (0,0))
        elif estado == RANK:
            ventana.blit(imgRank, (0,0))
            dibujarTop(ventana)
        elif estado == GAMEOVER:
            dibujarGameOver(ventana, imgGameOver)
            dibujarPuntajes(ventana, puntosGanados)
            escribirTop(puntosGanados)

        # TEXTO PANTALLA
        texto = fuente.render("PUNTOS = %d" % puntosGanados, 1, NEGRO)
        ventana.blit(texto, (ANCHO // 2 - 370, 25))

        pygame.display.flip()  # Actualiza trazos (Si no llamas a esta función, no se dibuja)
        reloj.tick(40)  # 40 fps
        timer += 1/40

    # Después del ciclo principal
    pygame.quit()  # termina pygame

# Función principal
def main():
    dibujar()

# Llama a la función principal
main()


from __future__ import absolute_import
from future import standard_library
standard_library.install_aliases()
__all__ = ['PyReplyEncoder']

import pickle

import CPL
from Hub.Reply.FullReply import FullReply
from .ReplyEncoder import ReplyEncoder

class PyReplyEncoder(ReplyEncoder):
    """ Encode Replys as single-line pickled python objects.
    """

    def __init__(self, **argv):
        ReplyEncoder.__init__(self, **argv)

        # How do we terminate encoded lines?
        #
        self.EOL = argv.get('EOL', '\f')
        self.encode = self.encodeFull

    def encodeFull(self, r, nub, noKeys=False):
        """ Encode a reply for a given nub.

        Encode all the information required to track the source of the command and the reply.
        """

        fullReply = FullReply()
        fullReply.initFromReply(r, noKeys)
        fullPickle = pickle.dumps(fullReply)

        if self.debug > 6:
            CPL.log('PyEncode.encode', 'encoding FullReply %s as %r' % (fullReply, fullPickle))
        elif self.debug > 3:
            CPL.log('PyEncode.encode', 'encoding FullReply %s' % (fullReply,))

        return "%s%s" % (fullPickle, self.EOL)


import os
import sys
import time
import random
import serial
from YModem import YModem

def main(argv):
    serial_io = serial.Serial()
    try:
        filename = argv[0]
        serial_io.port = argv[1]
        serial_io.baudrate = argv[2]
    except Exception as e:
        print ('python ymodem_sender.py <filename> <device> <baudrate>')
    serial_io.parity = "N"
    serial_io.bytesize = 8
    serial_io.stopbits = 1
    serial_io.timeout = 2
    try:
        serial_io.open()
    except Exception as e:
        raise Exception("Failed to open serial port!")

    def sender_getc(size):
        return serial_io.read(size) or None

    def sender_putc(data, timeout=15):
        return serial_io.write(data)

    os.chdir(sys.path[0])
    file_path = os.path.abspath(filename)
    sender = YModem(sender_getc, sender_putc)
    sent = sender.send_file(file_path)
    serial_io.close()

if __name__ == '__main__':
    main(sys.argv[1:])


from django.db import models
import datetime
from django.utils import timezone
from django.contrib.auth.models import AbstractUser

class User(AbstractUser):
    @property
    def is_employee(self):
        if hasattr(self, 'employee'):
            return True
        return False

class Employee(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    card_id=models.BigIntegerField()
    name=models.CharField(max_length=60, blank=True, null=True)
    phone=models.BigIntegerField(blank=True, null=True)
    department=models.CharField(max_length=60, blank=True, null=True)
    email=models.EmailField(blank=True, null=True)
    address=models.CharField(max_length=100, blank=True, null=True)
    def __str__(self):
        '''if self.name==None:
            return str(self.card_id)
        else:
            return str(self.name) + ' : ' + str(self.card_id)'''
        return str(self.name)

class Record(models.Model):
    emp = models.ManyToManyField(Employee)
    ids=models.IntegerField(default=0)
    card_id=models.BigIntegerField()
    name=models.CharField(max_length=60, blank=True, null=True)
    department=models.CharField(max_length=60, blank=True, null=True)
    date=models.DateField(default=datetime.date.today)
    time_in=models.TimeField(default=datetime.datetime.now())
    time_out=models.TimeField(blank=True, null=True)
    status=models.TextField(max_length=100)

    def __str__(self):
        return str(self.name) + ' : ' + str(self.date)

class EmpLeaveRequests(models.Model):
    remp = models.ForeignKey(Employee, on_delete=models.CASCADE)
    content = models.CharField(max_length=100)
    date_from = models.DateField(max_length= 100, default=None)
    date_to = models.DateField(max_length=100, default=None)
    status = models.CharField(max_length=100, null=True)

class AppStatus(models.Model):
    leaveApp = models.ForeignKey(EmpLeaveRequests, on_delete=models.CASCADE)
    status = models.CharField(max_length=100, null=True)

class Notification(models.Model):
    content = models.CharField(max_length=1000, null=True)
    date=models.DateField(default=datetime.date.today)

class Tickets(models.Model):
    temp = models.ForeignKey(Employee, on_delete=models.CASCADE)
    content = models.CharField(max_length=100)
    date = models.DateField(max_length= 100, default=None)
    status = models.CharField(max_length=100, null=True)