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

import collections

from neutron_lib import constants as n_const
from oslo_log import log
from ryu.lib.packet import ethernet
from ryu.lib.packet import icmp
from ryu.lib.packet import in_proto
from ryu.lib.packet import ipv4
from ryu.lib.packet import packet
from ryu.ofproto import ether
from ryu.ofproto import nicira_ext

from dragonflow.common import utils as df_utils
from dragonflow import conf as cfg
from dragonflow.controller.common import arp_responder
from dragonflow.controller.common import constants as const
from dragonflow.controller.common import icmp_error_generator
from dragonflow.controller.common import utils
from dragonflow.controller import df_base_app
from dragonflow.db.models import constants as model_constants
from dragonflow.db.models import l2
from dragonflow.db.models import l3
from dragonflow.db.models import ovs

LOG = log.getLogger(__name__)

FIP_GW_RESOLVING_STATUS = 'resolving'

EGRESS = 'egress'

INGRESS = 'ingress'

class DNATApp(df_base_app.DFlowApp):

    def __init__(self, *args, **kwargs):
        super(DNATApp, self).__init__(*args, **kwargs)
        self.external_network_bridge = \
            cfg.CONF.df_dnat_app.external_network_bridge
        self.external_bridge_mac = ""
        self.integration_bridge = cfg.CONF.df.integration_bridge
        self.conf = cfg.CONF.df_dnat_app
        self.int_peer_patch_port = self.conf.int_peer_patch_port
        self.ex_peer_patch_port = self.conf.ex_peer_patch_port
        self.external_networks = collections.defaultdict(int)
        self.local_floatingips = collections.defaultdict(str)
        # Map between fixed ip mac to floating ip
        self.fip_port_key_cache = {}
        self.egress_ttl_invalid_handler_rate_limit = df_utils.RateLimiter(
            max_rate=self.conf.dnat_ttl_invalid_max_rate,
            time_unit=1)
        self.ingress_ttl_invalid_handler_rate_limit = df_utils.RateLimiter(
            max_rate=self.conf.dnat_ttl_invalid_max_rate,
            time_unit=1)
        self.egress_icmp_error_rate_limit = df_utils.RateLimiter(
            max_rate=self.conf.dnat_icmp_error_max_rate,
            time_unit=1)
        self.ingress_icmp_error_rate_limit = df_utils.RateLimiter(
            max_rate=self.conf.dnat_icmp_error_max_rate,
            time_unit=1)
        self.api.register_table_handler(const.INGRESS_NAT_TABLE,
                                        self.ingress_packet_in_handler)
        self.api.register_table_handler(const.EGRESS_NAT_TABLE,
                                        self.egress_packet_in_handler)

    def switch_features_handler(self, ev):
        self._init_external_bridge()
        self._install_output_to_physical_patch(self.external_ofport)
        self.external_networks.clear()
        self.local_floatingips.clear()
        self.fip_port_key_cache.clear()

    def _handle_ingress_invalid_ttl(self, event):
        LOG.debug("Get an invalid TTL packet at table %s",
                  const.INGRESS_NAT_TABLE)

        if self.ingress_ttl_invalid_handler_rate_limit():
            LOG.warning("Get more than %(rate)s TTL invalid "
                        "packets per second at table %(table)s",
                        {'rate': self.conf.dnat_ttl_invalid_max_rate,
                         'table': const.INGRESS_NAT_TABLE})
            return

        msg = event.msg

        icmp_ttl_pkt = icmp_error_generator.generate(
            icmp.ICMP_TIME_EXCEEDED, icmp.ICMP_TTL_EXPIRED_CODE, msg.data)
        network_id = msg.match.get('metadata')
        self.reinject_packet(
            icmp_ttl_pkt,
            table_id=const.L2_LOOKUP_TABLE,
            actions=[self.parser.OFPActionSetField(metadata=network_id)]
        )

    def _handle_ingress_icmp_translate(self, event):
        if self.ingress_icmp_error_rate_limit():
            LOG.warning("Get more than %(rate)s ICMP error messages "
                        "per second at table %(table)s",
                        {'rate': self.conf.dnat_icmp_error_max_rate,
                         'table': const.INGRESS_NAT_TABLE})
            return

        msg = event.msg
        pkt = packet.Packet(msg.data)
        reply_pkt = self._revert_nat_for_icmp_embedded_packet(pkt, INGRESS)
        port_key = msg.match.get('reg7')
        self.dispatch_packet(reply_pkt, port_key)

    def ingress_packet_in_handler(self, event):
        if event.msg.reason == self.ofproto.OFPR_INVALID_TTL:
            self._handle_ingress_invalid_ttl(event)
        else:
            self._handle_ingress_icmp_translate(event)

    def _handle_egress_invalid_ttl(self, event):
        LOG.debug("Get an invalid TTL packet at table %s",
                  const.EGRESS_NAT_TABLE)

        if self.egress_ttl_invalid_handler_rate_limit():
            LOG.warning("Get more than %(rate)s TTL invalid "
                        "packets per second at table %(table)s",
                        {'rate': self.conf.dnat_ttl_invalid_max_rate,
                         'table': const.EGRESS_NAT_TABLE})
            return

        msg = event.msg

        pkt = packet.Packet(msg.data)
        e_pkt = pkt.get_protocol(ethernet.ethernet)
        port_key = msg.match.get('reg6')
        floatingip = self.fip_port_key_cache.get(port_key)
        if floatingip is None:
            LOG.warning("The invalid TTL packet's destination mac %s "
                        "can't be recognized.", e_pkt.src)
            return

        icmp_ttl_pkt = icmp_error_generator.generate(
            icmp.ICMP_TIME_EXCEEDED, icmp.ICMP_TTL_EXPIRED_CODE,
            msg.data, floatingip.floating_ip_address, pkt)
        self.dispatch_packet(icmp_ttl_pkt, port_key)

    def _handle_egress_icmp_translate(self, event):
        if self.ingress_icmp_error_rate_limit():
            LOG.warning("Get more than %(rate)s ICMP error messages "
                        "per second at table %(table)s",
                        {'rate': self.conf.dnat_icmp_error_max_rate,
                         'table': const.INGRESS_NAT_TABLE})
            return

        msg = event.msg

        pkt = packet.Packet(msg.data)

        reply_pkt = self._revert_nat_for_icmp_embedded_packet(pkt, EGRESS)
        metadata = msg.match.get('metadata')

        self.reinject_packet(
            reply_pkt,
            table_id=const.L2_LOOKUP_TABLE,
            actions=[self.parser.OFPActionSetField(metadata=metadata)]
        )

    def egress_packet_in_handler(self, event):
        if event.msg.reason == self.ofproto.OFPR_INVALID_TTL:
            self._handle_egress_invalid_ttl(event)
        else:
            self._handle_egress_icmp_translate(event)

    def _revert_nat_for_icmp_embedded_packet(self, pkt, direction):
        e_pkt = pkt.get_protocol(ethernet.ethernet)
        ipv4_pkt = pkt.get_protocol(ipv4.ipv4)
        ipv4_pkt.csum = 0
        icmp_pkt = pkt.get_protocol(icmp.icmp)

        embeded_ipv4_pkt, _, payload = ipv4.ipv4.parser(icmp_pkt.data.data)
        if direction == EGRESS:
            embeded_ipv4_pkt.dst = ipv4_pkt.src
        else:
            embeded_ipv4_pkt.src = ipv4_pkt.dst
        embeded_data = embeded_ipv4_pkt.serialize(None, None) + payload
        icmp_pkt.data.data = embeded_data
        # Re-calculate when encoding
        icmp_pkt.csum = 0

        reply_pkt = packet.Packet()
        reply_pkt.add_protocol(e_pkt)
        reply_pkt.add_protocol(ipv4_pkt)
        reply_pkt.add_protocol(icmp_pkt)
        return reply_pkt

    @df_base_app.register_event(ovs.OvsPort, model_constants.EVENT_CREATED)
    @df_base_app.register_event(ovs.OvsPort, model_constants.EVENT_UPDATED)
    def ovs_port_updated(self, ovs_port, orig_ovs_port=None):
        if ovs_port.name != self.external_network_bridge:
            return

        mac = ovs_port.mac_in_use
        if (self.external_bridge_mac == mac
                or not mac
                or mac == '00:00:00:00:00:00'):
            return

        for key, floatingip in self.local_floatingips.items():
            self._install_dnat_egress_rules(floatingip, mac)

        self.external_bridge_mac = mac

    def _init_external_bridge(self):
        mapping = self.vswitch_api.create_patch_pair(
                self.integration_bridge,
                self.external_network_bridge,
                self.int_peer_patch_port,
                self.ex_peer_patch_port)
        self.external_ofport = self.vswitch_api.get_port_ofport(
            mapping[0])

    def _increase_external_network_count(self, network_id):
        self.external_networks[network_id] += 1

    def _decrease_external_network_count(self, network_id):
        self.external_networks[network_id] -= 1

    def _get_external_network_count(self, network_id):
        return self.external_networks[network_id]

    def _is_first_external_network(self, network_id):
        if self._get_external_network_count(network_id) == 0:
            # check whether there are other networks
            for key, val in self.external_networks.items():
                if key != network_id and val > 0:
                    return False
            return True
        return False

    def _is_last_external_network(self, network_id):
        if self._get_external_network_count(network_id) == 1:
            # check whether there are other networks
            for key, val in self.external_networks.items():
                if key != network_id and val > 0:
                    return False
            return True
        return False

    def _install_floatingip_arp_responder(self, floatingip):
        # install floatingip arp responder flow rules
        if floatingip.floating_ip_address.version != n_const.IP_VERSION_4:
            return
        floating_lport = floatingip.floating_lport
        arp_responder.ArpResponder(self,
                                   None,
                                   floatingip.floating_ip_address,
                                   floating_lport.mac,
                                   const.INGRESS_NAT_TABLE).add()

    def _remove_floatingip_arp_responder(self, floatingip):
        # install floatingip arp responder flow rules
        if floatingip.floating_ip_address.version != n_const.IP_VERSION_4:
            return
        floating_lport = floatingip.floating_lport
        arp_responder.ArpResponder(self,
                                   None,
                                   floatingip.floating_ip_address,
                                   floating_lport.mac,
                                   const.INGRESS_NAT_TABLE).remove()

    def _get_vm_port_info(self, floatingip):
        lport = floatingip.lport
        mac = lport.mac
        ip = lport.ip
        tunnel_key = lport.unique_key
        local_network_id = lport.lswitch.unique_key

        return mac, ip, tunnel_key, local_network_id

    def _get_vm_gateway_info(self, floatingip):
        lport = floatingip.lport
        lrouter = floatingip.lrouter
        for router_port in lrouter.ports:
            if router_port.lswitch.id == lport.lswitch.id:
                return router_port.mac
        return None

    def _install_dnat_ingress_rules(self, floatingip):
        parser = self.parser
        ofproto = self.ofproto
        match = parser.OFPMatch(eth_type=ether.ETH_TYPE_IP,
                                ipv4_dst=floatingip.floating_ip_address)

        vm_mac, vm_ip, lport_key, local_network_id = \
            self._get_vm_port_info(floatingip)
        vm_gateway_mac = self._get_vm_gateway_info(floatingip)
        if vm_gateway_mac is None:
            vm_gateway_mac = floatingip.floating_lport.mac
        actions = [
            parser.OFPActionDecNwTtl(),
            parser.OFPActionSetField(eth_src=vm_gateway_mac),
            parser.OFPActionSetField(eth_dst=vm_mac),
            parser.OFPActionSetField(ipv4_dst=vm_ip),
            parser.OFPActionSetField(reg7=lport_key),
            parser.OFPActionSetField(metadata=local_network_id)
        ]
        action_inst = parser.OFPInstructionActions(
            ofproto.OFPIT_APPLY_ACTIONS, actions)
        goto_inst = parser.OFPInstructionGotoTable(
            const.INGRESS_CONNTRACK_TABLE)
        inst = [action_inst, goto_inst]

        self.mod_flow(
            inst=inst,
            table_id=const.INGRESS_NAT_TABLE,
            priority=const.PRIORITY_MEDIUM,
            match=match)

        # Add flows to packet-in icmp time exceed and icmp unreachable message
        actions = [
            parser.OFPActionDecNwTtl(),
            parser.OFPActionSetField(eth_src=vm_gateway_mac),
            parser.OFPActionSetField(eth_dst=vm_mac),
            parser.OFPActionSetField(ipv4_dst=vm_ip),
            parser.OFPActionSetField(reg7=lport_key),
            parser.OFPActionOutput(ofproto.OFPP_CONTROLLER,
                                   ofproto.OFPCML_NO_BUFFER)
        ]
        action_inst = [parser.OFPInstructionActions(
            ofproto.OFPIT_APPLY_ACTIONS, actions)]
        for icmp_type in (icmp.ICMP_DEST_UNREACH, icmp.ICMP_TIME_EXCEEDED):
            match = parser.OFPMatch(eth_type=ether.ETH_TYPE_IP,
                                    ip_proto=in_proto.IPPROTO_ICMP,
                                    icmpv4_type=icmp_type,
                                    ipv4_dst=floatingip.floating_ip_address)
            self.mod_flow(
                inst=action_inst,
                table_id=const.INGRESS_NAT_TABLE,
                priority=const.PRIORITY_HIGH,
                match=match)

    def _remove_dnat_ingress_rules(self, floatingip):
        parser = self.parser
        ofproto = self.ofproto
        match = parser.OFPMatch(eth_type=ether.ETH_TYPE_IP,
                                ipv4_dst=floatingip.floating_ip_address)
        self.mod_flow(
            command=ofproto.OFPFC_DELETE,
            table_id=const.INGRESS_NAT_TABLE,
            priority=const.PRIORITY_MEDIUM,
            match=match)

    def _get_dnat_egress_match(self, floatingip):
        _, vm_ip, _, local_network_id = self._get_vm_port_info(floatingip)
        parser = self.parser
        match = parser.OFPMatch()
        match.set_dl_type(ether.ETH_TYPE_IP)
        match.set_metadata(local_network_id)
        match.set_ipv4_src(utils.ipv4_text_to_int(vm_ip))
        return match

    def _get_external_subnet(self, fip):
        subnets = fip.floating_lport.lswitch.subnets
        for subnet in subnets:
            if fip.floating_ip_address in subnet.cidr:
                return subnet

    def _get_external_cidr(self, fip):
        return self._get_external_subnet(fip).cidr

    def _install_dnat_egress_rules(self, floatingip, network_bridge_mac):
        fip_mac = floatingip.floating_lport.mac
        fip_ip = floatingip.floating_ip_address
        parser = self.parser
        ofproto = self.ofproto
        match = self._get_dnat_egress_match(floatingip)
        actions = [
            parser.OFPActionDecNwTtl(),
            parser.OFPActionSetField(eth_src=fip_mac),
            parser.OFPActionSetField(eth_dst=network_bridge_mac),
            parser.OFPActionSetField(ipv4_src=fip_ip)]
        action_inst = parser.OFPInstructionActions(
            ofproto.OFPIT_APPLY_ACTIONS, actions)
        goto_inst = parser.OFPInstructionGotoTable(const.EGRESS_EXTERNAL_TABLE)

        inst = [action_inst, goto_inst]

        self.mod_flow(
            inst=inst,
            table_id=const.EGRESS_NAT_TABLE,
            priority=const.PRIORITY_MEDIUM,
            match=match)

        # Add flows to packet-in icmp time exceed and icmp unreachable message
        actions.append(parser.OFPActionOutput(ofproto.OFPP_CONTROLLER,
                                              ofproto.OFPCML_NO_BUFFER))
        action_inst = [parser.OFPInstructionActions(
            ofproto.OFPIT_APPLY_ACTIONS, actions)]
        for icmp_type in (icmp.ICMP_DEST_UNREACH, icmp.ICMP_TIME_EXCEEDED):
            match = self._get_dnat_egress_match(floatingip)
            match.set_ip_proto(in_proto.IPPROTO_ICMP)
            match.set_icmpv4_type(icmp_type)
            self.mod_flow(
                inst=action_inst,
                table_id=const.EGRESS_NAT_TABLE,
                priority=const.PRIORITY_HIGH,
                match=match)

    def _remove_dnat_egress_rules(self, floatingip):
        ofproto = self.ofproto
        match = self._get_dnat_egress_match(floatingip)
        self.mod_flow(
            command=ofproto.OFPFC_DELETE,
            table_id=const.EGRESS_NAT_TABLE,
            priority=const.PRIORITY_MEDIUM,
            match=match)

    def _install_egress_nat_rules(self, floatingip):
        if self._get_external_cidr(floatingip).version != n_const.IP_VERSION_4:
            return

        match = self._get_dnat_egress_match(floatingip)
        self.add_flow_go_to_table(
            const.L3_LOOKUP_TABLE,
            const.PRIORITY_LOW,
            const.EGRESS_NAT_TABLE,
            match=match)
        if self.external_bridge_mac:
            self._install_dnat_egress_rules(floatingip,
                                            self.external_bridge_mac)

    def _remove_egress_nat_rules(self, floatingip):
        if self._get_external_cidr(floatingip).version != n_const.IP_VERSION_4:
            return

        ofproto = self.ofproto
        match = self._get_dnat_egress_match(floatingip)
        self.mod_flow(
            command=ofproto.OFPFC_DELETE,
            table_id=const.L3_LOOKUP_TABLE,
            priority=const.PRIORITY_LOW,
            match=match)

        self._remove_dnat_egress_rules(floatingip)

    def _install_ingress_nat_rules(self, floatingip):
        network_id = floatingip.floating_lport.lswitch.unique_key
        # TODO(Fei Rao) check the network type
        if self._is_first_external_network(network_id):
            # if it is the first floating ip on this node, then
            # install the common goto flow rule.
            parser = self.parser
            self.mod_flow(
                table_id=const.INGRESS_CLASSIFICATION_DISPATCH_TABLE,
                priority=const.PRIORITY_DEFAULT,
                match=parser.OFPMatch(in_port=self.external_ofport),
                inst=[
                    parser.OFPInstructionActions(
                        self.ofproto.OFPIT_APPLY_ACTIONS,
                        [
                            parser.NXActionRegLoad(
                                dst='in_port',
                                value=0,
                                ofs_nbits=nicira_ext.ofs_nbits(0, 31),
                            ),
                        ],
                    ),
                    parser.OFPInstructionGotoTable(const.INGRESS_NAT_TABLE),
                ],
            )
            # Take over reg7 == 0 for the external_ofport
            match = parser.OFPMatch(reg7=0)
            actions = [self.parser.OFPActionOutput(
                    self.external_ofport, self.ofproto.OFPCML_NO_BUFFER)]
            self.mod_flow(
                actions=actions,
                table_id=const.INGRESS_DISPATCH_TABLE,
                priority=const.PRIORITY_MEDIUM,
                match=match)
        self._install_floatingip_arp_responder(floatingip)
        self._install_dnat_ingress_rules(floatingip)
        self._increase_external_network_count(network_id)

    def _remove_ingress_nat_rules(self, floatingip):
        network_id = floatingip.floating_lport.lswitch.unique_key
        if self._is_last_external_network(network_id):
            # if it is the last floating ip on this node, then
            # remove the common goto flow rule.
            parser = self.parser
            ofproto = self.ofproto
            match = parser.OFPMatch()
            match.set_in_port(self.external_ofport)
            self.mod_flow(
                command=ofproto.OFPFC_DELETE,
                table_id=const.INGRESS_CLASSIFICATION_DISPATCH_TABLE,
                priority=const.PRIORITY_DEFAULT,
                match=match)
            match = parser.OFPMatch(reg7=0)
            self.mod_flow(
                command=ofproto.OFPFC_DELETE,
                table_id=const.INGRESS_DISPATCH_TABLE,
                priority=const.PRIORITY_MEDIUM,
                match=match)
        self._remove_floatingip_arp_responder(floatingip)
        self._remove_dnat_ingress_rules(floatingip)
        self._decrease_external_network_count(network_id)

    @df_base_app.register_event(l3.FloatingIp, model_constants.EVENT_CREATED)
    def _create_floatingip(self, fip):
        if fip.is_local:
            self._associate_floatingip(fip)

    @df_base_app.register_event(l3.FloatingIp, model_constants.EVENT_UPDATED)
    def _update_floatingip(self, fip, original_fip):
        if original_fip.lport == fip.lport:
            return

        if original_fip.is_local:
            self._disassociate_floatingip(original_fip)

        if fip.is_local:
            self._associate_floatingip(fip)

    def _associate_floatingip(self, floatingip):
        self.local_floatingips[floatingip.id] = floatingip
        lport = floatingip.lport
        self.fip_port_key_cache[lport.unique_key] = floatingip
        self._install_ingress_nat_rules(floatingip)
        self._install_egress_nat_rules(floatingip)

    def _disassociate_floatingip(self, floatingip):
        self.local_floatingips.pop(floatingip.id, 0)
        lport = floatingip.lport
        self.fip_port_key_cache.pop(lport.unique_key, None)
        self._remove_ingress_nat_rules(floatingip)
        self._remove_egress_nat_rules(floatingip)

    @df_base_app.register_event(l2.LogicalPort, l2.EVENT_BIND_LOCAL)
    def _create_local_port(self, lport):
        fips = self.db_store.get_all(
            l3.FloatingIp(lport=lport),
            index=l3.FloatingIp.get_index('lport'),
        )

        for fip in fips:
            self._associate_floatingip(fip)

    @df_base_app.register_event(l2.LogicalPort, l2.EVENT_UNBIND_LOCAL)
    def _remove_local_port(self, lport):
        ips_to_disassociate = (
            fip for fip in self.local_floatingips.values()
            if fip.lport.id == lport.id)
        for floatingip in ips_to_disassociate:
            self._disassociate_floatingip(floatingip)

    @df_base_app.register_event(l3.FloatingIp, model_constants.EVENT_DELETED)
    def _delete_floatingip(self, floatingip):
        if floatingip.is_local:
            self._disassociate_floatingip(floatingip)

    def _install_output_to_physical_patch(self, ofport):
        parser = self.parser
        ofproto = self.ofproto
        actions = [parser.OFPActionOutput(ofport,
                                          ofproto.OFPCML_NO_BUFFER)]
        actions_inst = parser.OFPInstructionActions(
            ofproto.OFPIT_APPLY_ACTIONS, actions)
        inst = [actions_inst]
        self.mod_flow(inst=inst,
                      table_id=const.EGRESS_EXTERNAL_TABLE,
                      priority=const.PRIORITY_MEDIUM, match=None)


# Migratory Birds Solution from RookieRank2 Challenge

#https://www.hackerrank.com/contests/rookierank-2/challenges/migratory-birds

#!/bin/python3

import sys

n = int(input().strip())
types = list(map(int, input().strip().split(' ')))

d = {'1':0 ,'2':0 ,'3':0 ,'4':0 ,'5':0}
for num in types:
    if num == 1:
        d['1'] += 1
    if num == 2:
        d['2'] += 1
    if num == 3:
        d['3'] += 1
    if num == 4:
        d['4'] += 1
    if num == 5:
        d['5'] += 1

print(d)
mxmum = max(d, key= d.get)
print(mxmum)


import socket
import subprocess

HOST = 'localhost'
PORT = 50007

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.connect((HOST, PORT))
    s.send('connected'.encode())
    command = s.recv(1024)
    command = command.decode()

    print(command)
    op = subprocess.Popen(command, shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
    output = op.stdout.read()
    output_error = op.stderr.read()
    s.send(output + output_error)


from celescope.tools.report import reporter
from celescope.tools.utils import add_log, format_stat, read_barcode_file
import os
import pandas as pd
import numpy as np
import glob

class Count_cite():

    def __init__(
        self,
        sample,
        outdir,
        assay,
        read_count_file,
        match_dir,
        ):
        self.sample = sample
        self.outdir = outdir
        self.assay = assay
        self.read_count_file = read_count_file
        self.match_dir = match_dir
        self.match_barcode, self.cell_total = read_barcode_file(match_dir)
        self.df_read_count = pd.read_csv(read_count_file, sep="\t", index_col=0)
        self.tsne_file = glob.glob(f'{match_dir}/*analysis/*tsne_coord.tsv')[0]

        if not os.path.exists(outdir):
            os.system('mkdir -p %s' % outdir)

        # out
        self.mtx = f'{outdir}/{sample}_citeseq.mtx.gz'
        self.stats = None
        self.stat_file = f'{self.outdir}/stat.txt'

    @add_log
    def run(self):
        stats = pd.Series()
        mapped_read = self.df_read_count['read_count'].sum()

        # in cell
        df_read_count_in_cell = self.df_read_count[self.df_read_count.index.isin(self.match_barcode)]
        mapped_read_in_cell = int(df_read_count_in_cell['read_count'].sum())
        stats = stats.append(pd.Series(
            format_stat(mapped_read_in_cell, mapped_read),
            index=['Mapped Reads in Cells']
        ))

        # UMI
        df_UMI_in_cell = df_read_count_in_cell.reset_index().groupby([
            'barcode', 'barcode_name']).agg({'UMI': 'count'})
        df_UMI_in_cell = df_UMI_in_cell.reset_index()
        df_UMI_in_cell = df_UMI_in_cell.pivot(
            index='barcode', columns='barcode_name', values='UMI')
        df_cell = pd.DataFrame(index=self.match_barcode)
        df_UMI_cell = pd.merge(
            df_cell,
            df_UMI_in_cell,
            how="left",
            left_index=True,
            right_index=True)

        # fillna
        df_UMI_cell.fillna(0, inplace=True)
        df_UMI_cell = df_UMI_cell.astype(int)
        df_UMI_cell_out = df_UMI_cell.T
        df_UMI_cell_out.to_csv(self.mtx, sep='\t', compression='gzip')

        # UMI
        UMIs = df_UMI_cell.apply(sum, axis=1)
        median = round(np.median(UMIs), 2)
        mean = round(np.mean(UMIs), 2)
        stats = stats.append(pd.Series(
            str(median),
            index=['Median UMI per Cell']
        ))

        stats = stats.append(pd.Series(
            str(mean),
            index=['Mean UMI per Cell']
        ))

        self.stats = stats

    def report(self):

        self.stats.to_csv(self.stat_file, sep=':', header=False)
        t = reporter(
        name='count_cite',
        assay=self.assay,
        sample=self.sample,
        stat_file=self.stat_file,
        outdir=self.outdir + '/..')
        t.get_report()


import nidaqmx
from nidaqmx.constants import LineGrouping

def main():
    print ("Initiating...")

    # Connect to ATI sensor to get biased force
    with nidaqmx.Task() as task:
        task.ai_channels.add_ai_voltage_chan("Dev1/ai2")
        data = 0.0
        for i in range(0, 50):
            data = data + task.read()

        print ("voltage read = ", data/50)

if __name__ == "__main__":
    main()


import sqlite3
#connecting to the database
conn = sqlite3.connect('bloodbank.db')

#creating cursor over the connection to execute commands
c = conn.cursor()

# creating a table
c.execute("""CREATE TABLE donar_table(
			name text,
			blood_type text,
			donate_organs text,
			phone_number text,
			email text,
			address text
)""")

#commiting to the database
conn.commit()

#closing the connection
conn.close()


#!/usr/bin/env python
import cv2
import numpy as np
import pprint

img_set = ("beach", "dog", "polar", "bear", "lake", "moose")
img_rgb = {}
img_hist = {}
result = {}

for img_name in img_set:
    img_rgb[img_name] = cv2.imread("Data-TP/{}.jpg".format(img_name), 0)
    print(
        "loaded image : {}, {}x{}px".format(
            img_name, img_rgb[img_name].shape[0], img_rgb[img_name].shape[1]
        )
    )
template_rgb = cv2.imread("Data-TP/waves.jpg", 0)

template_hist = cv2.calcHist(
    template_rgb, [0], None, [256], [0, 256]
)  # get template histogram

for img_name in img_set:
    img_hist["{}".format(img_name)] = cv2.calcHist(
        img_rgb[img_name], [0], None, [256], [0, 256]
    )
    result[img_name] = cv2.compareHist(
        template_hist, img_hist[img_name], cv2.HISTCMP_INTERSECT
    )
    # result[img_name] = dict()
    # result[img_name]["correlation"] = cv2.compareHist(template_hist, img_hist[img_name], cv2.HISTCMP_CORREL)
    # result[img_name]["intersect"] = cv2.compareHist(template_hist, img_hist[img_name], cv2.HISTCMP_INTERSECT)
    # result[img_name]["hellinger"] = cv2.compareHist(template_hist, img_hist[img_name], cv2.HISTCMP_BHATTACHARYYA)
    # result[img_name]["chi-squared"] = cv2.compareHist(template_hist, img_hist[img_name], cv2.HISTCMP_CHISQR)

print(
    "== histogram comparison ==\n{}\n==========================".format(
        pprint.pformat(result)
    )
)

img = list(result.keys())[list(result.values()).index(max(result.values()))]

print(
    "Closest image: (using intersect) -> {} with {} similarity".format(
        img, max(result.values())
    )
)


import requests
from bs4 import BeautifulSoup

LIMIT=50
#앞에 f를 붙이면 {} 안에 변수참조가능.
URL=f'https://www.indeed.com/jobs?q=python&limit={LIMIT}'

def get_last_page():
  result = requests.get(URL)

  soup=BeautifulSoup(result.text, 'html.parser')

  #find 함수를 사용해보자. div이면서 class=pagination을 갖고있는거.
  #여기부턴 f12 사용하며 웹사이트 직접 들어가 찾아야한다.
  pagination = soup.find('div', {'class':'pagination'})

  links=pagination.find_all('a')
  pages=[]

  for link in links[:-1]:
    pages.append(int(link.find('span').string))

  max_page=pages[-1]
  return max_page

def extract_job(html):
  title=html.find('div', {'class':'title'}).find('a')['title']
  #링크가 있는 회사가 있고 없는 회사가 있다.
  company = html.find('span', {'class':'company'})
  if company:
    company_anchor=company.find('a')
    if company_anchor is not None:
      company=str(company_anchor.string)
    else:
      company=str(company.string)
    #빈칸을 해결하기 위해 strip을 사용하자.
    company=company.strip()
  else:
    company=None
  #location이 모두 있다면 아래코드를 쓰면 된다. 근데 가끔 None이 잡히므로..
  #location = html.find('span',{'class':'location'}).string
  #다음과 같은 클래스에선 항상 loc 값이 들어있는 걸 이용.
  location=html.find('div', {'class':'recJobLoc'})['data-rc-loc']
  job_id=html['data-jk']
  #항상 print를 해주면서 확인해주는거 잊지 말자.
  return {
    'title':title,
    'company':company,
    'location':location,
    'job_id':job_id
    }

def extract_jobs(last_page):
  jobs=[]

  #어느페이지를 크롤링할지 구했으니, 이제 request만 남았다.
  # for page in range(last_page):
  #코딩중에는 page=0으로 잡아서 단순하게.
  for page in range(last_page):
    print(f'Scrapping Indeed : page {page}')
    result=requests.get(f'{URL}&start={page*LIMIT}')
    soup=BeautifulSoup(result.text, 'html.parser')
    results=soup.find_all('div', {'class':'jobsearch-SerpJobCard'})
    #a태그에서 title을 가져오도록 하자.
    for result in results:
      job=extract_job(result)
      jobs.append(job)
  return jobs

#조금만 가져오고 싶다면, 둘째줄에 1이나 2 넣자.
def get_jobs():
  '''
  이 코드가 오리지날. 시간관계상 두 페이지만 스크레이핑해보자.
  last_page=get_last_page()
  jobs=extract_jobs(last_page)
  '''
  jobs=extract_jobs(2)
  return jobs


# -*- coding: utf-8 -*-
import npc.object

class cNpc(npc.object.NpcBase):

	def __init__(self):
		npc.object.NpcBase.__init__(self)
		self.name = "异兽Npc"

	def doLook(self, who):
		content = '''我可以为你做点什么呢?
Q增加异兽寿命
Q设置异兽等级
Q增加异兽经验
Q设置异兽技能
Q增加异兽技能潜力点'''
		message.selectBoxNew(who, self.responseLook, content, self)

	def responseLook(self, who, selectNo):
		if selectNo == 1:
			self.selectPet(who, self.inputLife)
		elif selectNo == 2:
			self.selectPet(who, self.inputLevel)
		elif selectNo == 3:
			self.selectPet(who, self.inputExp)
		elif selectNo == 4:
			self.selectPet(who, self.inputSkill)
		elif selectNo == 5:
			self.selectPet(who, self.inputSkillPoint)

	def selectPet(self, who, func):
		txtList = []
		selList = []
		for petObj in who.petCtn.getAllValues():
			txtList.append("\nQ%s %d" % (petObj.name, petObj.level))
			selList.append(petObj.id)

		content = "请选择异兽：" + "".join(txtList)
		message.selectBoxNew(who, functor(self.responseSelectPet, selList, func), content, self)

	def responseSelectPet(self, who, selectNo, selList, func):
		if selectNo < 1 or selectNo > len(selList):
			return

		petId = selList[selectNo-1]
		petObj = who.petCtn.getItem(petId)
		if not petObj:
			return
		func(who, petObj)

	def inputLife(self, who, petObj):
		content = "异兽[#C02%s#n]的寿命为%d, 请输入要增加的寿命:" % (petObj.name, petObj.getLife())
		message.inputBox(who, functor(self.doAddLife, petObj.id), "增加异兽寿命", content, TYPE_LIMIT_INT)

	def doAddLife(self, who, responseVal, petId):
		petObj = who.petCtn.getItem(petId)
		if not petObj:
			return

		lifeAdded = int(responseVal)
		petObj.addLife(lifeAdded, "petHelper")

	def inputLevel(self, who, petObj):
		content = "异兽[#C02%s#n]的等级为%d, 请输入新的等级:" % (petObj.name, petObj.level)
		message.inputBox(who, functor(self.doSetLevel, petObj.id), "设置异兽等级", content, TYPE_LIMIT_INT)

	def doSetLevel(self, who, responseVal, petId):
		petObj = who.petCtn.getItem(petId)
		if not petObj:
			return
		levelNew = int(responseVal)
		if levelNew < 0:
			return

		levelAdd = levelNew - petObj.level
		if levelAdd == 0:
			message.tips(who, "异兽[#C02%s#n]已经是%d级" % (petObj.name, petObj.level))
			return

		if levelAdd < 0:
			petObj.add("level", levelAdd)
			petObj.attrChange("level")
			petObj.reCalcAttr()
		else:
			for i in xrange(levelAdd):
				petObj.exp += petObj.expNext
				petObj.upLevel()
		message.tips(who, "异兽[#C02%s#n]的等级成功设置为%d" % (petObj.name, petObj.level))

	def inputExp(self, who, petObj):
		content = "异兽[#C02%s#n]的经验为%d, 请输入要增加的经验:" % (petObj.name, petObj.exp)
		message.inputBox(who, functor(self.doAddExp, petObj.id), "增加异兽经验", content, TYPE_LIMIT_INT)

	def doAddExp(self, who, responseVal, petId):
		petObj = who.petCtn.getItem(petId)
		if not petObj:
			return
		expAdded = int(responseVal)
		petObj.rewardExp(expAdded, "petHelper")

	def inputSkillPoint(self, who, petObj):
		content = "异兽[#C02%s#n]的技能潜力点为%d, 请输入要增加的技能潜力点:" % (petObj.name, petObj.getSklPoint())
		message.inputBox(who, functor(self.doAddSkillPoint, petObj.id), "增加异兽技能潜力点", content, TYPE_LIMIT_INT)

	def doAddSkillPoint(self, who, responseVal, petId):
		petObj = who.petCtn.getItem(petId)
		if not petObj:
			return
		skillPoint = int(responseVal)
		petObj.addSklPoint(skillPoint, "petHelper")

	def inputSkill(self, who, petObj):
		skillIdList = petObj.fetch("skillList", [])
		skillInfo = ",".join([str(skillId) for skillId in skillIdList])
		content = "异兽[#C02%s#n]的现有技能:\n%s\n请输入要增加的技能:\n格式: 技能编号1,技能编号2,技能编号3..." % (petObj.name, skillInfo)
		message.inputBox(who, functor(self.doAddSkill, petObj.id), "设置异兽技能", content)

	def doAddSkill(self, who, responseVal, petId):
		petObj = who.petCtn.getItem(petId)
		if not petObj:
			return

		skillIdList = {}
		for skillId in responseVal.split(","):
			if not skillId:
				continue
			m = re.match("(\d+)=(\d+)", skillId)
			if m:
				skillId = int(m.group(1))
				level = int(m.group(2))
			else:
				skillId = int(skillId)
				level = 1
			skillIdList[skillId] = level

		if not skillIdList:
			message.tips(who, "格式错误")
			return

		for skillId, level in skillIdList.items():
			petObj.setSkill(skillId, level)

		petObj.reCalcAttr()
		message.tips(who, "设置异兽技能成功！")

from common import *
from props.defines import *
from qanda.defines import *
import message
import re


"""
Entradas
inversion-->int-->dato1
salidas
incremento-->int-->di
total-->int-->total
"""
#entrada
dato1=int(input("digite su inversio: "))
#caja negra
di=(dato1*0.2)
total=di+dato1
#salidas
print("su incremento mensual es: " +str(total))


import os
from detecter import *
from PIL import Image

image_path = r"D:\data_image\1234"
for i in os.listdir(image_path):
    detector = Detector()
    with Image.open(os.path.join(image_path,i)) as im: # 打开图片
        # boxes = detector.detect(im)
        print("----------------------------")
        boxes = detector.detect(im)
        print("size:",im.size)
        imDraw = ImageDraw.Draw(im)
        for box in boxes: # 多个框，没循环一次框一个人脸
            x1 = int(box[0])
            y1 = int(box[1])
            x2 = int(box[2])
            y2 = int(box[3])

            print((x1, y1, x2, y2))

            print("conf:",box[4]) # 置信度
            imDraw.rectangle((x1, y1, x2, y2), outline='red',width=2)
            #im.show() # 每循环一次框一个人脸
        im.show()
        # exit()


"""Add indexes

Revision ID: d8f5961bcc69
Revises: ea246209833b
Create Date: 2019-12-12 02:17:35.130739

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = 'd8f5961bcc69'
down_revision = 'ea246209833b'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_index(op.f('ix_word_anagram_hash'), 'word', ['anagram_hash'], unique=False)
    op.create_index(op.f('ix_word_length'), 'word', ['length'], unique=False)
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f('ix_word_length'), table_name='word')
    op.drop_index(op.f('ix_word_anagram_hash'), table_name='word')
    # ### end Alembic commands ###


import time
from gpiozero import Button
from picamera import PiCamera
import RPi.GPIO as IO
import socket
from signal import pause
from Naked.toolshed.shell import execute_js, muterun_js

IO.setwarnings(False)       # do not show any warnings
IO.setmode (IO.BCM)         # we are programming the GPIO by BCM pin numbers. (PIN39 as 'GPIO19')
IO.setup(19,IO.OUT)         # initialize GPIO19 as an output.
IO.setup(21,IO.IN)          # initialize GPIO26 as input
button = Button(21)

# Define host based on hostname -I (PI2 IP address)
# can't find IP address of other thing;
# find before demo, git push and git pull real quick
# run server on pi2

HOST = "192.168.43.158"     # Symbolic name meaning all available interfaces
PORT = 5007               # Arbitrary non-privileged port

camera = PiCamera()
camera.resolution = (1280,720)
IO.output(19,False)

# might want to change origin address
origin = '/home/pi/Desktop/pictures/origin.jpg'

def take_picture(pic):
    time.sleep(2)
    camera.capture(pic)

while 1:
    diff = 0
    button.wait_for_press()
    start_time = time.time()

    while button.is_active:
        now_time = time.time()
        diff = -start_time+now_time

    if diff >= 5 :           #long hold
        IO.output(19, True)
        time.sleep(.5)
        camera.capture(origin)
        IO.output(19, False)

    else:                   #short hold
        IO.output(19, True)
        time.sleep(5)

        for i in range(3):
            take_picture('/home/pi/Desktop/pictures/'+ str(i) + '.jpg')

        response = muterun_js('mismatch.js')

        if response.exitcode == 0:
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            server_address = (HOST, PORT)
            sock.connect(server_address)

            try:
                data = str(int(float((response.stdout)[0:-1])))
                sock.send(data.encode())
            finally:
                sock.close()
                IO.output(19, False)


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

from django.db import migrations, models
import projects.models

class Migration(migrations.Migration):

    dependencies = [
        ('projects', '0001_initial'),
    ]

    operations = [
        migrations.CreateModel(
            name='Screenshot',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('file_location', models.FileField(upload_to=projects.models.screenshot_directory_path)),
            ],
        ),
        migrations.AddField(
            model_name='project',
            name='live_url',
            field=models.URLField(default='www.google.com'),
            preserve_default=False,
        ),
        migrations.AddField(
            model_name='project',
            name='repo_url',
            field=models.URLField(null=True, blank=True),
        ),
        migrations.AddField(
            model_name='screenshot',
            name='project',
            field=models.ForeignKey(to='projects.Project'),
        ),
    ]


import os
import re
from enum import Enum
import numpy as np

class SplitType(Enum):
    Single = 1
    Files = 2
    Lines = 3

def __clean(data, stop_symbols):
    for s in stop_symbols:
        data = data.replace(s,'')

    data = re.sub(r'[^\w\s]|[0-9]','',data)
    return data

def loadFiles(folder, split_type, num_of_lines):
    '''
    Loads all .txt files from the given folder (with subfolders)
    returns them as a list of strings.
    '''
    enc = 'utf8'

    #Parties to exclude
    with open('exclude.txt', 'r', encoding='utf8') as ex:
        exclude = [e.replace('\n', '') for e in ex.readlines()]

    #variables
    party_docs = np.array([])
    labels = np.array([])
    symbols = [r'\ufeff']

    #Iterating through subfolders
    if len(exclude) > 0:
        print('Excluded: {0}'.format(exclude))

    for party in os.listdir(folder):
        if party not in exclude:
            p = os.path.join(folder, party)
            labels = np.append(labels,party)
            documents = np.array([])
            for f in os.listdir(p):
                #Reading files
                with open(os.path.join(p, f), "r", encoding=enc) as file:
                    doc = file.read().lower()
                    doc = __clean(doc,symbols)
                    if split_type != SplitType.Lines:
                        doc = doc.replace('\n','')
                    if split_type == SplitType.Lines:
                        doc = np.asarray(doc.splitlines())
                    documents = np.append(documents,doc)

            if split_type == SplitType.Single:
                documents = ' '.join(documents)
            if split_type == SplitType.Lines:
                documents = documents[:num_of_lines]

            party_docs = np.append(party_docs,documents)

    return (party_docs, labels)


# File: time_played.py
# Author: Jay Oliver
# Date Created: 24/03/2020
# Last Modified: 11/04/2020
# Pupose: Contains functions for manipulating a string that represents an
#         amount of time played
# Comments: The time played format is, xxd xxh xxm xxs where x is an integer.
#           The order of the time parameters is not considered important

def form(time):
    """Checks if the input parameter is a time played.

    parameters:
        - time: A string that is checked to have the time played format
    returns:
        - ret: A boolean represeting the conformaty of the input parameter
               time
    raises:
    """

    ret = -1
    temp = []
    try:
        temp = time.split(" ")
    except AttributeError:
        ret = False
    if(len([i[-1] for i in temp]) > 4 and ret == -1):
        ret = False
    elif(len(list(set([i[-1] for i in temp]))) != len(temp) and ret == -1):
        ret = False
    elif ret == -1:
        for x in temp:
            # the implementation is casefold here makes the check non case
            # sensitive
            if(x[-1].casefold() not in ["d", "h", "m", "s"]):
                ret = False
                break
            else:
                try:
                    int(x[:-1])
                    ret = True
                except ValueError:
                    ret = False
                    break

    return ret

def hours(time):
    """Convert a time played into a flaoting point number of hours.

    parameters:
        - time: The time played that will be converted into hours
    returns:
        - total: The time played converted
    raises:
    """

    if form(time):
        total = 0
        values = [int(i[:-1]) for i in time.split(" ")]
        mults = [i[-1] for i in time.split(" ")]
        for i in enumerate(mults):
            if mults[i[0]] == "d":
                mults[i[0]] = 24
            elif mults[i[0]] == "h":
                mults[i[0]] = 1
            elif mults[i[0]] == "m":
                mults[i[0]] = 1/60.0
            else:
                mults[i[0]] = 1/60.0**2
        for i in enumerate(values):
            total = total + values[i[0]] * mults[i[0]]
    else:
        raise ValueError("The given parameter was not of the time played"
                         " format")
    return total


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

import argparse

from fancyimpute import (
    SoftImpute,
    IterativeSVD,
    SimilarityWeightedAveraging,
    KNN,
    MICE,
    BiScaler,
    SimpleFill,
)
import numpy as np
import pandas as pd

from dataset_paths import PETERS2009_CSV_PATH
from score_set import ScoreSet
from mhcflurry.dataset import Dataset
from matrix_completion_helpers import (
    evaluate_predictions,
    stratified_cross_validation
)

parser = argparse.ArgumentParser()

parser.add_argument(
    "--binding-data-csv",
    default=PETERS2009_CSV_PATH)

parser.add_argument(
    "--max-ic50",
    default=50000.0,
    type=float)

parser.add_argument(
    "--save-incomplete-affinity-matrix",
    default=None,
    help="Path to CSV which will contains the incomplete affinity matrix")

parser.add_argument(
    "--only-human",
    default=False,
    action="store_true")

parser.add_argument(
    "--output-file",
    default="matrix-completion-accuracy-results.csv")

parser.add_argument(
    "--normalize-columns",
    default=False,
    action="store_true",
    help="Center and rescale columns of affinity matrix")

parser.add_argument(
    "--normalize-rows",
    default=False,
    action="store_true",
    help="Center and rescale rows of affinity matrix")

parser.add_argument(
    "--min-observations-per-peptide",
    type=int,
    default=2,
    help="Drop peptide entries with fewer than this number of measured affinities")

parser.add_argument(
    "--n-folds",
    default=10,
    type=int,
    help="Number of cross-validation folds")

parser.add_argument(
    "--verbose",
    default=False,
    action="store_true")

def create_imputation_methods(
        verbose=False,
        clip_imputed_values=False,
        knn_print_interval=20,
        knn_params=[1, 3, 5],
        softimpute_params=[1, 5, 10],
        svd_params=[5, 10, 20]):
    min_value = 0 if clip_imputed_values else None
    max_value = 1 if clip_imputed_values else None
    result_dict = {
        "meanFill": SimpleFill("mean"),
        "zeroFill": SimpleFill("zero"),
        "mice": MICE(
            n_burn_in=5,
            n_imputations=25,
            min_value=min_value,
            max_value=max_value,
            verbose=verbose),
        "similarityWeightedAveraging": SimilarityWeightedAveraging(
            orientation="columns",
            verbose=verbose),
    }
    for threshold in softimpute_params:
        result_dict["softImpute-%d" % threshold] = SoftImpute(
            threshold,
            verbose=verbose,
            min_value=min_value,
            max_value=max_value)
    for rank in svd_params:
        result_dict["svdImpute-%d" % rank] = IterativeSVD(
            rank,
            verbose=verbose,
            min_value=min_value,
            max_value=max_value)
    for k in knn_params:
        result_dict["knnImpute-%d" % k] = KNN(
            k,
            orientation="columns",
            verbose=verbose,
            print_interval=knn_print_interval)
    return result_dict

if __name__ == "__main__":
    args = parser.parse_args()
    print(args)
    imputation_methods = create_imputation_methods(
        verbose=args.verbose,
        clip_imputed_values=not (args.normalize_rows or args.normalize_rows),
    )
    print("Imputation methods: %s" % imputation_methods)

    dataset = Dataset.from_csv(args.binding_data_csv)
    X, peptide_list, allele_list = dataset.to_dense_pMHC_affinity_matrix(
        min_observations_per_allele=args.n_folds,
        min_observations_per_peptide=args.min_observations_per_peptide)
    observed_mask = np.isfinite(X)
    print("Loaded binding data, shape: %s, n_observed=%d/%d (%0.2f%%)" % (
        X.shape,
        observed_mask.sum(),
        X.size,
        100.0 * observed_mask.sum() / X.size))
    if args.save_incomplete_affinity_matrix:
        print("Saving incomplete data to %s" % args.save_incomplete_affinity_matrix)
        df = pd.DataFrame(X, columns=allele_list, index=peptide_list)
        df.to_csv(args.save_incomplete_affinity_matrix, index_label="peptide")

    scores = ScoreSet()
    kfold = stratified_cross_validation(
        X=X,
        observed_mask=observed_mask,
        n_folds=args.n_folds)
    for fold_idx, (X_fold, ok_mesh, test_coords, X_test_vector) in enumerate(kfold):
        X_fold_reduced = X_fold[ok_mesh]
        biscaler = BiScaler(
            scale_rows=args.normalize_rows,
            center_rows=args.normalize_rows,
            scale_columns=args.normalize_columns,
            center_columns=args.normalize_columns)
        X_fold_reduced_scaled = biscaler.fit_transform(X=X_fold_reduced)
        for (method_name, solver) in sorted(imputation_methods.items()):
            print("CV fold %d/%d, running %s" % (
                fold_idx + 1,
                args.n_folds,
                method_name))
            X_completed_reduced_scaled = solver.complete(X_fold_reduced)
            X_completed_reduced = biscaler.inverse_transform(
                X_completed_reduced_scaled)
            X_completed = np.zeros_like(X)
            X_completed[ok_mesh] = X_completed_reduced
            y_pred = X_completed[test_coords]
            mae, tau, auc, f1_score = evaluate_predictions(
                y_true=X_test_vector, y_pred=y_pred, max_ic50=args.max_ic50)
            scores.add_many(
                method_name,
                mae=mae,
                tau=tau,
                f1_score=f1_score,
                auc=auc)
    scores.to_csv(args.output_file)


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

import json
from pymongo import MongoClient

HOST       = '192.168.0.7'
PORT       = 27017
DB         = 'test'
COLLECTION = 'test'

class TestMongo(object):

    def __init__(self, host, port, db):
        self.clint = MongoClient(host=host, port=port)
        self.db = self.clint[db]

    def insert(self, collection, data):
        print('insert start...')
        self.db[collection].insert_many(data)
        print('done.')

    def drop_collection(self, collection):
        print('drop collection start...')
        self.db[collection].drop()
        print('done.')

def main():
    # mongoDBに接続、データベース初期化
    obj = TestMongo(HOST, PORT, DB)

    # 指定のコレクションを削除
    obj.drop_collection(COLLECTION)

    # jsonデータインポート
    with open('./data/features.json', 'r') as f:
        data = [json.loads(line) for line in f]

    # 指定のコレクションにデータをインサート
    obj.insert(COLLECTION, data)

if __name__ == '__main__':
    main()


#!/usr/bin/env python3
import os
import numpy
from pathlib import Path
import pickle
from nltk.corpus import stopwords as sw
from nltk.corpus import wordnet as wn
from nltk import wordpunct_tokenize
from nltk import WordNetLemmatizer
from nltk import sent_tokenize
from sklearn.linear_model import SGDClassifier
from nltk import pos_tag
from sklearn.base import BaseEstimator, TransformerMixin

# Import the framework
from flask import Flask, g
from flask_restful import Resource, Api, reqparse
# Create an instance of Flask
app = Flask(__name__)

# Create the API
api = Api(app)

@app.route("/")
def index():
    # Open the README file
    #with open(os.path.dirname(app.root_path) + '/README.md', 'r') as markdown_file:
    # Read the content of the file
    #content = str("\n ".join(main(file)))

    # Convert to HTML
    #if classifer is not None :
    # return markdown.markdown("classifier is loaded")
    return "
classifier is loaded
"
    #else :
    #    return "
classifier is not loaded
"
        # return markdown.markdown("classifier is not loaded"

class NLTKPreprocessor(BaseEstimator, TransformerMixin):
    #it loads a variety of corpora and models for use in tokenization.
    #By default the set of english stopwords from NLTK is used, and the WordNetLemmatizer
    #looks up data from the WordNet lexicon. Note that this takes a noticeable amount of time,
    #and should only be done on instantiation of the transformer.

    def __init__(self, stopwords=None, punct=None,
                         lower=True, strip=True):
        self.lower      = lower
        self.strip      = strip
        self.stopwords  = stopwords or set(sw.words('english'))
        self.punct      = punct or set(string.punctuation)
        self.lemmatizer = WordNetLemmatizer()

    def fit(self, X, y=None):
        return self

    def inverse_transform(self, X):
        return [" ".join(doc) for doc in X]

    def transform(self, X):
        return [list(self.tokenize(doc)) for doc in X]

        #The tokenize method breaks raw strings into sentences,
        #then breaks those sentences into words and punctuation,
        #and applies a part of speech tag. The token is then normalized:
        #made lower case, then stripped of whitespace and other types of punctuation that may be appended.
        #If the token is a stopword or if every character is punctuation, the token is ignored.
        #If it is not ignored, the part of speech is used to lemmatize the token, which is then yielded
    def tokenize(self, document):
        # Break the document into sentences
        for sent in sent_tokenize(document):
            # Break the sentence into part of speech tagged tokens
            for token, tag in pos_tag(wordpunct_tokenize(sent)):
                   # Apply preprocessing to the token
                    token = token.lower() if self.lower else token
                    token = token.strip() if self.strip else token
                    token = token.strip('_') if self.strip else token

                    token = token.strip('*') if self.strip else token

                    # If stopword, ignore token and continue
                    if token in self.stopwords:
                            continue
                    # If punctuation, ignore token and continue
                    if all(char in self.punct for char in token):
                            continue

                        # Lemmatize the token and yield
                    lemma = self.lemmatize(token, tag)
                    yield lemma

    def lemmatize(self, token, tag):
        tag = {'N': wn.NOUN,'V': wn.VERB,'R': wn.ADV,'J': wn.ADJ}.get(tag[0], wn.NOUN)
        return self.lemmatizer.lemmatize(token, tag)

class queryList(Resource):
    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument('text', required=False, type=str, action='append', default=[])
        args = parser.parse_args()

        out = {}
        out["violent"] = str(violent_model.predict(args["text"])[0])
        rural_pred = rural_model.predict(args["text"])[0]
        if rural_pred == 1:
            out["urbanrural"] = "rural"
        else:
            out["urbanrural"] = "urban"

        return out

def identity(arg):
    """
    Simple identity function works as a passthrough.
    """
    return arg

global violent_model
HOME=os.getenv("HOME")
with open(HOME+"/.pytorch_pretrained_bert/violent_model.pickle", 'rb') as f:
    violent_model = pickle.load(f)

global rural_model
with open(HOME+"/.pytorch_pretrained_bert/rural_model.pickle", 'rb') as f:
    rural_model = pickle.load(f)

api.add_resource(queryList, '/queries')
app.run(host='0.0.0.0', port=4996, debug=True)


import os.path
import sys
import pandas as pd
import logging

INPUT_DIRECTORY = '../../../data/raw/disease_SG'
INPUT_FILE = "weekly-dengue-malaria.csv"
OUTPUT_DIRECTORY = '../../../data/interim/disease_SG'
OUTPUT_FILE = "weekly-dengue-malaria-cleaned.csv"

logger = logging.getLogger(__name__)

def clean():
    input_path = os.path.join(INPUT_DIRECTORY, INPUT_FILE)
    if not os.path.isfile(input_path):
        logger.error("Input file is not found %s", os.path.abspath(input_path))
    data_frame = pd.read_csv(input_path, names=['year_week', 'disease', 'number_of_cases'])
    data_frame['country'] = 'Singapore'
    year_week = pd.DataFrame(data_frame.year_week.str.split('-').tolist(), columns=['year','week'])
    data_frame['year'] = year_week['year']
    data_frame['week'] = year_week['week'].str[1:]
    data_frame.drop('year_week', 1, inplace=True)

    os.makedirs(OUTPUT_DIRECTORY, exist_ok=True)
    output_path = os.path.join(OUTPUT_DIRECTORY, OUTPUT_FILE)
    data_frame.to_csv(output_path, index=False)

    logger.info('Data clean successfully')

if __name__ == "__main__":
    logging.basicConfig(level=logging.DEBUG)
    clean()


import math
from calc_lat_force import *
#Cyrus
#I don't understand anything that's going on. We were able to figure out some of the
#parameters here: https://app.lucidchart.com/invitations/accept/980ba211-a473-4f38-bb3f-97fc6c79325d
# Required traction to hold car in corner - ex how much centrifugal force does the car exert
#   F = mv^2/r - mass, velocity, radius
#   We want the force of the tires to equal the above equation ^^^ and solve for velocity
#   Total traction has to be calculated for EACH tire - we must account for weight transfer. Load on outside tires is more than load on inside tires
#   Dynamics has the fucntion, for x roll, here's the amount of traction on each tire
#   Front wheels are operating at different angles due to them being turned.
#

# How much traction do the tires put out? - Waiting on dynamics to hear back
# How much downforce is the car creating

def calc_corner_time_2(W, hcg, hcp, cl, cd, WB, t_f, t_r, weight_dist, cp_dist, R, degrees, target_cf, v_0, v_f):

    length = 2*math.pi*R*degrees/360

    a = weight_dist * WB
    b = WB - a

    a_prime = cp_dist * WB
    b_prime = WB - a_prime

    m = 1

    Ay_converge = .0001

    Ay = 1

    Ay_diff = 1

    t = (t_f + t_r) / 2

    while Ay_diff > Ay_converge:

        v_calc = (Ay*32.2 * R)**(1/2)

        fzr = W * weight_dist + cl * v_calc**2 * cp_dist + cd * v_calc**2 * hcp / WB

        WT = (W*Ay*hcg/(t))

        fzr_o = fzr/2 + WT/2
        fzr_i = fzr/2 - WT/2

        fyr = (calc_lat_force(fzr_o,-6*math.pi/180,0) + calc_lat_force(fzr_i,-6*math.pi/180,0))*target_cf;

        A_lat = fyr/(W*(weight_dist))

        Ay_check = Ay

        Ay = A_lat

        Ay_diff = abs(Ay_check-A_lat)

        m = m+1

        if m > 10000:
            print('iterations maxed!')
            exit(10)

    m=m-1

    v_final = (Ay*32.2 * R)**(1/2)
    time_corner = length / v_final

    t_step = .001

    x=0
    x2=0
    l=0
    l2=0

    if v_0 < v_final:

        v = v_0
        x = 0
        l = 0
        Ax = math.sqrt((v_final**4 - v_0**4)/(R*32.2)**2)

        while Ax > .001:

            l = l+1
            Ax = math.sqrt((v_final**4 - v**4)/(R*32.2)**2)
            v = v + Ax * t_step
            x = x + v*t_step

    if v_f < v_final:

        v = v_f
        x2 = 0
        l2 = 0
        Ax = math.sqrt((v_final**4 - v_f**4)/(R*32.2)**2)

        while Ax > .001:

            l2 = l2+1
            Ax = math.sqrt((v_final**4 - v**4)/(R*32.2)**2)
            v = v + Ax * t_step
            x2 = x2 + v*t_step

    newlength = length - x - x2
    time_const_speed = newlength / v_final
    time_change_speed = l * t_step + l2 * t_step
    time_corner = time_const_speed + time_change_speed

    print(newlength)
    print(time_const_speed)
    print(time_change_speed)
    print(time_corner)
    answer = [Ay, time_corner, v_final]

    return answer


# --------------------------------------------------- Librerías ------------------------------------------------------
import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
import matplotlib.animation as animation
from matplotlib import style
from matplotlib import pyplot as plt
from tkinter import *
import tkinter as tk
import serial
import time
import tkinter.font as font
import serial.tools.list_ports as lpuertos
from numpy import trapz as trapezoide

# -------------------------------------------- Comunicación Serial ---------------------------------------------------
# Búsqueda de puertos
ports = lpuertos.comports()
arport = 'None'
ncon = len(ports)

for p in range(0, ncon):
    puerto = ports[p]
    strpuerto = str(puerto)
    if 'Arduino' in strpuerto:
        p = strpuerto.split(' ')
        arport = p[0]

# Establecimiento de la comunicación serial
ser = serial.Serial(arport, baudrate=9600, timeout=1)
time.sleep(3)

# ----------------------------------- Inicialización de variables ----------------------------------------------------
LARGE_FONT = ("Verdana", 12)
style.use("ggplot")

x = []
y1 = []
y2 = []
y3 = []
global contador
contador = 0

# ---------------------- Función de obtener medidas mediante el puerto serial ----------------------------------------
def ObtVal():

    ser.write(b'A')
    leer = ser.readline().decode().split('\r\n')
    DA = int(leer.pop(0))

    ser.write(b'B')
    leer = ser.readline().decode().split('\r\n')
    DB = int(leer.pop(0))

    ser.write(b'C')
    leer = ser.readline().decode().split('\r\n')
    DC = int(leer.pop(0))

    return DA, DB, DC

# ----------------------------------- Definición de clases para interfaz gráfica ------------------------------------

class Proyecto(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        tk.Tk.iconbitmap(self, default="./Icono.ico")
        tk.Tk.wm_title(self, "Medidor de consumo de energía")

        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)
        self.frames = {}

        for F in (Inicio, Grafica):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(Inicio)

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()

class Inicio(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        pf = PhotoImage(file="uscorp.png")

        # ----------------------------------------- LABELS ---------------------------------------------------------
        #label = tk.Label(self, text="MEDIDOR DE CONSUMO DE ENERGÍA ELÉCTRICA", font=LARGE_FONT)
        #label.pack(pady=10)

        label2 = tk.Label(self, image=pf)
        label2.pf = pf
        label2.pack(pady=50)

        # ------------------------------------------- BUTTONS -----------------------------------------------------
        myfont = font.Font(family='Arial')
        button1 = tk.Button(self, text="Entrar", font=myfont, command=lambda: controller.show_frame(Grafica), width=20)
        button1.pack(pady=10)

class Grafica(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        self.es1 = IntVar()
        self.es2 = IntVar()
        self.es3 = IntVar()

        self.running = False
        self.ani = None

        # ------------------------------------------- LABELS -----------------------------------------------------

        label = tk.Label(self, text="GRÁFICA DE CONSUMO DE ENERGÍA ELÉCTRICA", font=LARGE_FONT)
        label.pack(side=tk.TOP, fill=tk.BOTH, expand=True)

        # ------------------------------------------- FRAMES -----------------------------------------------------

        f1 = tk.Frame(self, height=500, width=300, borderwidth=2)
        f1.pack(side="left", fill="y")

        f2 = tk.Frame(self, height=500, width=300, borderwidth=2)
        f2.pack(side="right", fill="y")

        f3 = tk.Frame(self, height=200)
        f3.pack(side="bottom", fill="x")

        # ------------------------------------------ CHECKBOX ----------------------------------------------------

        self.ch1 = tk.Checkbutton(f1, text="Sensor 1", font=LARGE_FONT, variable=self.es1)
        self.ch1.grid(row=0, column=0, padx=60, pady=60)

        self.ch2 = tk.Checkbutton(f1, text="Sensor 2", font=LARGE_FONT, variable=self.es2)
        self.ch2.grid(row=1, column=0, padx=60, pady=60)

        self.ch3 = tk.Checkbutton(f1, text="Sensor 3", font=LARGE_FONT, variable=self.es3)
        self.ch3.grid(row=2, column=0, padx=60, pady=60)

        # ---------------------------------------- ENTRIES & LABELS ----------------------------------------------

        label2 = tk.Label(f2, text="Consumo Sensor 1 (kW)", font=LARGE_FONT)
        label2.grid(row=0, column=0, padx=30, pady=20)

        self.e1 = tk.Entry(f2, state="disable")
        self.e1.grid(row=1, column=0, padx=30, pady=20)

        label3 = tk.Label(f2, text="Consumo Sensor 2 (kW)", font=LARGE_FONT)
        label3.grid(row=3, column=0, padx=30, pady=20)

        self.e2 = tk.Entry(f2, state="disable")
        self.e2.grid(row=4, column=0, padx=30, pady=20)

        label4 = tk.Label(f2, text="Consumo Sensor 3 (kW)", font=LARGE_FONT)
        label4.grid(row=6, column=0, padx=30, pady=20)

        self.e3 = tk.Entry(f2, state="disable")
        self.e3.grid(row=7, column=0, padx=30, pady=20)

        label5 = tk.Label(f2, text="Tiempo (s)", font=LARGE_FONT)
        label5.grid(row=9, column=0, padx=30, pady=20)

        self.e4 = tk.Entry(f2, state="disable")
        self.e4.grid(row=10, column=0, padx=30, pady=20)

        # --------------------------------------- BUTTONS --------------------------------------------------------
        myfont = font.Font(family='Arial')

        self.button1 = tk.Button(f3, text="Volver", font=myfont, width=10,
                                 command=lambda: controller.show_frame(Inicio))
        self.button1.grid(row=0, column=0, padx=180, pady=20)

        self.button2 = tk.Button(f3, text="Iniciar", font=myfont, width=10, command=lambda: self.start())
        self.button2.grid(row=0, column=1, padx=180, pady=20)

        # ------------------------------------------ GRÁFICA ----------------------------------------------------

        self.fig = plt.Figure(figsize=(5, 5), dpi=100)
        self.a = self.fig.add_subplot(111)
        self.a.set_ylim(0, 1.5)
        self.a.set_xlabel('Tiempo (s)')
        self.a.set_ylabel('Potencia (kW)')
        self.canvas = FigureCanvasTkAgg(self.fig, master=self)
        self.canvas.draw()
        self.canvas.get_tk_widget().pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)

        toolbar = NavigationToolbar2Tk(self.canvas, self)
        toolbar.update()
        self.canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=True)

    # -------------------------------------- Método Botón ----------------------------------------------------------

    def start(self):

        x.clear()
        y1.clear()
        y2.clear()
        y3.clear()

        self.contador = 0

        self.estadoch1=self.es1.get()
        self.estadoch2=self.es2.get()
        self.estadoch3=self.es3.get()

        if self.ani is None:
            # Si la animación no está corriendo entonces graficar
            self.a.cla()
            self.button2.config(text='Detener')

            self.ch1.config(state='disable')
            self.ch2.config(state='disable')
            self.ch3.config(state='disable')

            self.e1.config(state='normal')
            self.e2.config(state='normal')
            self.e3.config(state='normal')
            self.e4.config(state='normal')

            return self.graficar()

        if self.running:
            # Si la animación está corriendo, detener
            self.ani.event_source.stop()
            self.button2.config(text='Iniciar')

            self.ch1.config(state='normal')
            self.ch2.config(state='normal')
            self.ch3.config(state='normal')

            self.e1.config(state='disable')
            self.e2.config(state='disable')
            self.e3.config(state='disable')
            self.e4.config(state='disable')

        else:
            # Si la animación no está corriendo, reinicie
            self.ani.event_source.start()
            self.button2.config(text='Detener')

            self.ch1.config(state='disable')
            self.ch2.config(state='disable')
            self.ch3.config(state='disable')

            self.e1.config(state='normal')
            self.e2.config(state='normal')
            self.e3.config(state='normal')
            self.e4.config(state='normal')

        self.running = not self.running

    # --------------------------------- Función Animate para tomar datos y graficar ---------------------------------

    def graficar(self):

        self.ani = animation.FuncAnimation(self.fig, self.act_grafica, frames=10000, interval=1000)

        self.running = True
        self.button2.config(text='Detener')
        self.ani._start()

    def act_grafica(self, i):
        D1, D2, D3 = ObtVal()

        D1 = round((D1 * 1.2 / 1023), 3)
        D2 = round((D2 * 1.2 / 1023), 3)
        D3 = round((D3 * 1.2 / 1023), 3)

        x.append(self.contador)
        y1.append(D1)
        y2.append(D2)
        y3.append(D3)

        ay1 = trapezoide(y1)
        ay2 = trapezoide(y2)
        ay3 = trapezoide(y3)

        self.a.cla()

        self.a.set_ylim(0, 1.5, auto=False)

        if self.estadoch1 == 1 or self.estadoch2 == 1 or self.estadoch2 == 1:
            self.e4.delete(0, 20)
            self.e4.insert(0, str(self.contador))

        if self.estadoch1 == 1:
            self.e1.delete(0, 20)
            self.e1.insert(0, str(ay1))
            self.e1.delete(6, 20)
            self.a.plot(x, y1, 'r', label="Sensor 1")
            self.a.legend()

        if self.estadoch2 == 1:
            self.e2.delete(0, 20)
            self.e2.insert(0, str(ay2))
            self.e2.delete(6, 20)
            self.a.plot(x, y2, 'g', label="Sensor 2")
            self.a.legend()

        if self.estadoch3 == 1:
            self.e3.delete(0, 20)
            self.e3.insert(0, str(ay3))
            self.e3.delete(6, 20)
            self.a.plot(x, y3, 'b', label="Sensor 3")
            self.a.legend()

        self.a.set_xlabel('Tiempo (s)')
        self.a.set_ylabel('Potencia (kW)')
        self.contador += 1

app = Proyecto()
app.mainloop()


a = 2
b = 2
c = 2
d = 2
class Player1:
    x = 0
    y = 0
    global a
    def draw(self):
        print("I Player1 to {}".format(self.x))

    def runforward(self):
        self.x += a
        print("I run to {}".format(self.x))

    def runback(self):
        if self.x < a:
            self.x = 0
            print("I run to {}".format(self.x))
        else:
            self.x -= a
            print("I run to {}".format(self.x))

    def shoot(self):
        print("Trrrrr")

    def take(self):

        def predmet1():
            global a
            a += 1
            print("Uskorenie na 1")

        def predmet2():
            global a
            a -= 1
            print("Zamedlenie na 1")

        cmd = input("Какой предмет взять: predmet1, predmet2:")
        if cmd == "predmet1":
            predmet1()
        elif cmd == "predmet2":
            predmet2()

    def flyup(self):
        print("I can't fly")

    def flydown(self):
        print("I can't fly")

class Player2(Player1):
    global b
    def draw(self):
        print("I Player2 to {}".format(self.x))

    def runforward(self):
        self.x += b
        print("I run to {}".format(self.x))

    def runback(self):
        if self.x < b:
            self.x = 0
            print("I run to {}".format(self.x))
        else:
            self.x -= b
            print("I run to {}".format(self.x))

    def take(self):

        def predmet1():
            global b
            b += 1
            print("Uskorenie na 1")

        def predmet2():
            global b
            b -= 1
            print("Zamedlenie na 1")

        cmd = input("Какой предмет взять: predmet1, predmet2:")
        if cmd == "predmet1":
            predmet1()
        elif cmd == "predmet2":
            predmet2()

class Player3(Player1):
    global c
    def runforward(self):
        self.x += c
        print("I run to {}".format(self.x))

    def runback(self):
        if self.x < c:
            self.x = 0
            print("I run to {}".format(self.x))
        else:
            self.x -= c
            print("I run to {}".format(self.x))

    def draw(self):
        print("I Player3 to {}".format(self.x))

    def flyup(self):
        self.y+=1
        print("I'm flying to a height of {}".format(self.y))

    def flydown(self):
        if (self.y == 0):
            print("I'm on earth")
        else:
            self.y -= 1
            print("I'm flying to a height of {}".format(self.y))

    def take(self):

        def predmet1():
            global c
            c += 1
            print("Uskorenie na 1")

        def predmet2():
            global c
            c -= 1
            print("Zamedlenie na 1")

        cmd = input("Какой предмет взять: predmet1, predmet2:")
        if cmd == "predmet1":
            predmet1()
        elif cmd == "predmet2":
            predmet2()

class Player4(Player3):
    global d
    def runforward(self):
        self.x += d
        print("I run to {}".format(self.x))

    def runback(self):
        if self.x < d:
            self.x = 0
            print("I run to {}".format(self.x))
        else:
            self.x -= d
            print("I run to {}".format(self.x))

    def draw(self):
        print("I Player4 to {}".format(self.x))

    def take(self):

        def predmet1():
            global d
            d += 1
            print("Uskorenie na 1")

        def predmet2():
            global d
            d -= 1
            print("Zamedlenie na 1")

        cmd = input("Какой предмет взять: predmet1, predmet2:")
        if cmd == "predmet1":
            predmet1()
        elif cmd == "predmet2":
            predmet2()

players = [Player1(), Player2(), Player3(), Player4()]
for player in players:
    player.draw()
while True:
    cmd = input("Введите команду (shoot, runforward, runback, flyup, flydown, take): ")
    n = int(input("Введите номер персонажа (от 0 до 3): "))
    if cmd == "runforward":
        players[n].runforward()
    elif cmd == "runback":
        players[n].runback()
    elif cmd == "shoot":
        players[n].shoot()
    elif cmd == "flyup":
        players[n].flyup()
    elif cmd == "flydown":
        players[n].flydown()
    elif cmd == "take":
        players[n].take()
    else:
        break
    for player in players:
        player.draw()


# Copyright 2013 Google Inc. All Rights Reserved.
#
# 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.

"""Custom Jinja2 filters used in Course Builder."""

__author__ = 'John Orr (jorr@google.com)'

import jinja2
import safe_dom
import tags
from webapp2_extras import i18n

from models import config
from models import models

CAN_USE_JINJA2_TEMPLATE_CACHE = config.ConfigProperty(
    'gcb_can_use_jinja2_template_cache', bool, safe_dom.Text(
        'Whether jinja2 can cache bytecode of compiled templates in memcache.'),
    default_value=True)

def finalize(x):
    """A finalize method which will correctly handle safe_dom elements."""
    if isinstance(x, safe_dom.Node) or isinstance(x, safe_dom.NodeList):
        return jinja2.utils.Markup(x.sanitized)
    return x

def js_string_raw(data):
    """Escape a string so that it can be put in a JS quote."""
    if not isinstance(data, basestring):
        return data
    data = data.replace('\\', '\\\\')
    data = data.replace('\r', '\\r')
    data = data.replace('\n', '\\n')
    data = data.replace('\b', '\\b')
    data = data.replace('"', '\\"')
    data = data.replace("'", "\\'")
    data = data.replace('<', '\\u003c')
    data = data.replace('>', '\\u003e')
    data = data.replace('&', '\\u0026')
    return data

def js_string(data):
    return jinja2.utils.Markup(js_string_raw(data))

def get_gcb_tags_filter(handler):
    def gcb_tags(data):
        """Apply GCB custom tags, if enabled. Otherwise pass as if by 'safe'."""
        if not isinstance(data, basestring):
            return data
        if tags.CAN_USE_DYNAMIC_TAGS.value:
            return jinja2.utils.Markup(tags.html_to_safe_dom(data, handler))
        else:
            return jinja2.utils.Markup(data)
    return gcb_tags

def create_jinja_environment(loader, locale=None):
    """Create proper jinja environment."""

    cache = None
    if CAN_USE_JINJA2_TEMPLATE_CACHE.value:
        prefix = 'jinja2:bytecode:%s:/' % models.MemcacheManager.get_namespace()
        cache = jinja2.MemcachedBytecodeCache(
            models.MemcacheManager, timeout=models.DEFAULT_CACHE_TTL_SECS,
            prefix=prefix)

    jinja_environment = jinja2.Environment(
        autoescape=True, finalize=finalize, extensions=['jinja2.ext.i18n'],
        bytecode_cache=cache, loader=loader)

    jinja_environment.filters['js_string'] = js_string

    if locale:
        i18n.get_i18n().set_locale(locale)
        jinja_environment.install_gettext_translations(i18n)

    return jinja_environment

def get_template(template_name, dirs, locale=None, handler=None):
    """Sets up an environment and gets jinja template."""

    jinja_environment = create_jinja_environment(
        jinja2.FileSystemLoader(dirs), locale=locale)

    jinja_environment.filters['gcb_tags'] = get_gcb_tags_filter(handler)

    return jinja_environment.get_template(template_name)


def findMergeNode(headA, headB):
    curA = headA
    curB = headB
    while not curA == curB:
        if curA.next is None:
            curA = headB
        else:
            curA = curA.next
        if curB.next is None:
            curB = headA
        else:
            curB = curB.next
    return curA.data


import re

import scrapy
from Spider.day13.baidutieba.baidutieba.items import BaidutiebaItem
from Spider.ip_pool import Pool
ips = Pool(10)
ip = ips.offer_ip()

class BaiduSpider(scrapy.Spider):
    name = 'baidu'
    # allowed_domains = ['baidu.com']
    # start_urls = ['http://baidu.com/']

    def start_requests(self):
        tieba_name = ['NBA', '李毅','英雄联盟',]
        for tie_name in tieba_name:
            for page in range(1,100):
                url = "https://tieba.baidu.com/f?ie=utf-8&kw="+str(tie_name)+" &pn={}&".format(page)
                yield scrapy.Request(url)

    def parse(self, response):
        title = response.xpath('//head/title/text()').getall().split('-')[0]
        print(title,response.text)
        name_rule = 'title="主题作者: (.*?)"'
        name = re.findall(name_rule, response.text)
        content_list = response.xpath('//div[@class="threadlist_abs threadlist_abs_onlyline "]/text()').getall()
        for i in content_list:
            index = content_list.index(i)
            content = i.strip().replace('\n', '')
            item = BaidutiebaItem()
            item['title'] = title
            item['name'] = name[index]
            item['content'] = content
            yield item

if __name__ == '__main__':
    from scrapy import cmdline
    cmdline.execute(['scray', 'crawl', 'baidu'])


class Data:
    last_attack = 0

def find_target(planets):
    lowest = planets[0]
    for planet in planets:
    	if planet.owner() == 0 or planet.owner() == 2:
    		lowest = planet
    		break

    for planet in planets:
        if planet.num_ships() < lowest.num_ships() and (planet.owner() == 0 or planet.owner() == 2):
            lowest = planet

    return lowest

def do_turn(pw):
    my_planets = pw.my_planets()
    neutral_planets = pw.neutral_planets()
    enemy_planets = pw.enemy_planets()

    if len(neutral_planets) > 0 or len(enemy_planets) > 0:
        target = find_target(pw.planets())
        attacked = False
        for planet in my_planets:
            if planet.num_ships() - 16 > target.num_ships():
                pw.issue_order(planet, target, target.num_ships() + 7)
                Data.last_attack = 0
                attacked = True

        if not attacked:
            Data.last_attack += 1

        if Data.last_attack > 8:
            for planet in my_planets:
                pw.issue_order(planet, target, target.num_ships() / 2)
                Data.last_attack = 0

"""
    # If we have less than 5 planets
    if len(neutral_planets) > 0:
        target = find_target(neutral_planets)
        for planet in my_planets:
            if planet.num_ships() - 16 > target.num_ships():
                pw.issue_order(planet, target, target.num_ships() + 7)
                Data.sent.append(target.planet_id())

    elif len(enemy_planets) > 0:
        dest = find_target(enemy_planets)
        for planet in my_planets:
            if planet.num_ships() > 30:
                pw.issue_order(planet, dest, planet.num_ships() / 2)
            """


def decide(s):
    print("For {0}".format(s))
    r = "NO"
    operators = []
    try:
        for c in s:
            if c == "[" or c == "(" or c == "{":
                operators.append(c) # Flag character. Doesn't really matter what it is.
            elif c == "]":
                o = operators.pop()
                if o != "[":
                    raise Exception("NO")
            elif c == ")":
                o = operators.pop()
                if o != "(":
                    raise Exception("NO")
            elif c == "}":
                o = operators.pop()
                if o != "{":
                    raise Exception("NO")
            else:
                # Weird character. Don't care about it.
                continue
            print("PENDING: Operators {0} = {1}".format(len(operators), operators))
        if len(operators) == 0: # Done parsing the string and operators are balanced
            r = "YES"
    except:
        r = "NO"
    return r

# Complete the braces function below.
def braces(values):
    r = []
    for v in values:
        r.append(decide(v))
    return r


"""
Testing.
Only tests at the highest level; does not test helper functions.
"""

from predicates import *

def assertEqual(a, b):
    if not a == b:
        raise AssertionError

def assertError(func, error):
    try:
        func()
    except error:
        pass

def assertTrue(v):
    if not v:
        raise AssertionError

tests = []
def test(func): tests.append(func)

#### TESTS

@test
def A():
    @safe
    def add(x: int, y: int) -> int:
        if x == 0:
            return "ERROR"
        else:
            return x + y

    assertEqual(add(2, 2), 4)

    assertError(
        lambda: add(2, 'a')
    , PredicateError)

    assertError(
        lambda: add(0, 1)
    , PredicateError)

@test
def B():
    @predicate
    @safe
    def two_tuple_of(t1: type, t2: type, t: tuple) -> bool:
        return isinstance(t[0], t1) and isinstance(t[1], t2)

    @safe
    def xor_first_and_add_second(tup1: two_tuple_of[bool, int], tup2: two_tuple_of[bool, int]) -> two_tuple_of[bool, int]:
        return (tup1[0] ^ tup2[0], tup1[1] + tup2[1])

    assertEqual(
        xor_first_and_add_second(
            (True, 1),
            (False, 2),
        ),
        (True, 3),
    )

@test
def C():
    @predicate
    @safe
    def list_of(t: type, l: list) -> bool:
        return all(isinstance(el, t) for el in l)

    A = DelayedArgument()
    @delayed(A)
    def combine_lists(l1: list_of[A], l2: list_of[A]) -> list_of[A]:
        return l1 + l2

    assertEqual(
        [2, 2, 3, 3],
        combine_lists[int]([2, 2], [3, 3]),
    )

# Now we start testing incorrect usage

@test
def D():
    """
    Forgetting to immedicize a generalized predicate before using it.
    """

    @predicate
    def type_is(t: type, o: object) -> bool:
        return type(o) is t

    assertError(
        lambda: type_is(str)
    , TypeError)

@test
def E():
    """
    Using `@safe` when `@delayed` is needed
    """
    @predicate
    @safe
    def list_of(t: type, l: list) -> bool:
        return all(isinstance(el, t) for el in l)

    try:
        A = DelayedArgument()
        @safe
        def combine_lists(l1: list_of[A], l2: list_of[A]) -> list_of[A]:
            return l1 + l2
    except DelayedArgumentError: pass  # Expected

# Syntactic sugar tests

def SSA():
    @predicate
    @safe
    def List(pred, l: list) -> bool:
        return all(pred(el) for el in l)

    Int = to_predicate(int)
    String = to_predicate(str)

    assertTrue(
        List[Int | String](
            [1, 2, 3, "Hello", 4, "Goodbye"]
        )
    )

    assertError(
        lambda: List[Int | String]( [True] )
    , PredicateError)

#### END TESTS

if __name__ == "__main__":
    for t in tests:
        t()


import FLAGS
import os
import Utilities
import skimage.io as io
from scipy.spatial import distance
import time as t
import datetime
import numpy as np
import operator

def store_tensor_list(arg_list, func_to_call, log_filename):
    with open(log_filename, "w") as f:
        for arg in arg_list:
            tensor_list = func_to_call(arg)
            f.write(arg + " " + str(len(tensor_list)) + '\n')
            for tensor in tensor_list:
                [f.write(str(s) + ' ') for s in tensor.shape]
                f.write('\n')

                [f.write(str(v) + ' ') for v in list(np.array(tensor).reshape(-1))]
                f.write('\n')

def get_tensor_list_dict_from_disk(log_filename):
    start = t.time()
    print('Retrieving tensors from', log_filename, "...")

    arg_list_dict = {}
    with open(log_filename, 'r') as f:
        lines = f.readlines()
        walker = 0
        while walker < len(lines):
            arg_filename, n_blocks = lines[walker].rstrip().split(' ')
            walker += 1
            tensors_list = []
            for _ in range(int(n_blocks)):
                shape = [int(v) for v in lines[walker].rstrip().split(' ')]
                walker += 1

                tensor = np.array([int(v) for v in lines[walker].rstrip().split(' ')]).reshape(shape)
                walker += 1

                tensors_list.append(tensor)

            arg_list_dict[arg_filename] = tensors_list

    finish = t.time()
    print("Finished retrieving tensors in", datetime.timedelta(seconds=finish-start))
    return arg_list_dict


import cv2
import numpy as np
from PIL import Image

## 使用する画像指定 jpgでもpngでも可
img = cv2.imread("./original.jpg")
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

## hsvで抜き出す色の範囲を指定
# mask = cv2.inRange(hsv, (36, 0, 0), (86, 255,255))  #green
# mask = cv2.inRange(hsv, (5, 0, 0), (30, 255,255))  # red
mask = cv2.inRange(hsv, (100, 0, 0), (120, 255, 255))  # blue

imask = mask > 0
green = np.zeros_like(img, np.uint8)
green[imask] = img[imask]

# 黒と青に分けた画像の保存
cv2.imwrite("image.png", green)
img = Image.open('image.png')
black = 0
blue = 0

# 黒とその他の色に分ける
for pixel in img.getdata():

    # rgb値指定
    # if pixel < (1, 1, 1):
    if pixel == (0, 0, 0):
        black += 1
    else:
        blue += 1

print('black=' + str(black) + 'px, blue=' + str(blue) + 'px')
calc_blue = round((blue / (black + blue)), 5)
calc_black = 1 - calc_blue
print("blue(except black)={}% : black={}%".format(calc_blue * 100, calc_black * 100))


# -*- coding: utf-8 -*-
"""
Created on Tue Nov  7 18:48:16 2017

@author: Kevin
"""

import pandas as pd
import numpy as np
import os
import seaborn as sns

import matplotlib.pyplot as plt

pylab.rcParams['figure.figsize'] = (8.0, 7.0)

os.chdir('insert-path-here')

# Import data
df = pd.read_csv('Pokemon.csv')
df2 = pd.read_csv('pokemon_stats.csv')

''' Data Cleaning and Merging '''

# To ensure consistency between datasets
df['Name'] = df['Name'].str.lower()

# Fix name issues between datasets
df = df.replace({'Name': {'nidoran♀': 'nidoran-f', 'nidoran♂': 'nidoran-m', "farfetch'd": 'farfetchd',
                                'mr. mime': 'mr-mime', 'deoxysnormal forme': 'deoxys-normal', 'wormadamplant cloak': 'wormadam-plant',
                                'mime jr.': 'mime-jr', 'giratinaaltered forme': 'giratina-altered', 'shayminland forme': 'shaymin-land',
                                'basculin': 'basculin-red-striped', 'darmanitanstandard mode': 'darmanitan-standard',
                                'tornadusincarnate forme': 'tornadus-incarnate', 'thundurusincarnate forme': 'thundurus-incarnate',
                                'landorusincarnate forme': 'landorus-incarnate', 'keldeoordinary forme': 'keldeo-ordinary',
                                'meloettaaria forme': 'meloetta-aria', 'flabébé': 'flabebe', 'meowsticmale': 'meowstic-male',
                                'aegislashshield forme': 'aegislash-shield', 'pumpkabooaverage size': 'pumpkaboo-average',
                                'gourgeistaverage size': 'gourgeist-average', 'hoopahoopa confined': 'hoopa'}})

# Merge both datasets
df_merged = pd.merge(df, df2, left_on='Name', right_on='identifier', how='left')

# Drop unnecessary rows and columns
df_merged = df_merged[df_merged['is_default'] == 1]
cols_to_keep = ['Name', 'Type 1', 'Total', 'HP', 'Attack', 'Defense', 'Sp. Atk', 'Sp. Def', 'Speed',
                'Generation', 'Legendary', 'height', 'weight', 'base_experience']
df_merged = df_merged[cols_to_keep]

# View the data
df_merged.head(5)

''' Data Visualizations '''

# What is the proportion of each type in our dataset?
df_merged['Type 1'].value_counts().plot(kind='barh', color='lightblue')

# Lets view the proportion by %'s instead
labels = df_merged['Type 1'].value_counts().index
df_merged['Type 1'].value_counts().plot(kind='pie', labels=labels, autopct='%1.0f%%')

# There is 0% flying types! (rounded) - who are they?
df_merged[df['Type 1'] == 'Flying']

'''
Domain knowledge tells us Noibat and its evolution are the only Pokemon with flying as their main type.
In addition, they are actually the weakest pokemon with "dragon" types.
Let us remove the flying types
'''
df_merged = df_merged[df_merged['Type 1'] != 'Flying']

# Which Pokemon has the highest value for each stat?
variables_list = ['Total', 'HP', 'Attack', 'Defense', 'Sp. Atk', 'Sp. Def', 'Speed', 'height', 'weight', 'base_experience']

for i in variables_list:
    print(df_merged.groupby(['Name'], as_index=False)['%s' % i].max().sort_values(by=['%s' % i], ascending=False).head(3))

# Who is the 'strongest' Pokemon of each type?
strongest = df_merged.sort_values(by='Total', ascending=False)
strongest.drop_duplicates(subset=['Type 1'], keep='first')

# How powerful is each generation
plt.title('Total Power by Generation')
sns.violinplot(x = 'Generation', y='Total', data=df_merged)
#sns.plt.show()

# Where do legendaries fall within the power level in each type?
plt.figure(figsize=(12,6))
sns.swarmplot(x='Type 1', y='Total', data=df_merged, hue='Legendary')
plt.axhline(df_merged['Total'].mean(), color='red', linestyle='dashed')

# Are the variables correlated to each other?
corr_df = df_merged.corr(method='pearson')

mask = np.zeros_like(corr_df)
mask[np.triu_indices_from(mask)] = True

sns.heatmap(corr_df, cmap='RdYlGn_r', vmax=1.0, vmin=-1.0 , mask = mask, linewidths=4.5, annot=True)

''' Preparing training and testing data for algorithms '''

# Preparing our train and test data
# Set legendary False = 0, True = 1
df_merged['Legendary'] = np.where(df_merged['Legendary'] == True, 1, 0)

# Remove out a random sample of 5% of EACH TYPE of pokemon to be used as the test data
type_list = list(df_merged['Type 1'].unique())

df_test = pd.DataFrame(columns = df_merged.columns)
for i in type_list:
    df_test = df_test.append((df_merged[df_merged['Type 1'] == i]).sample(frac=0.05))

# Now remove the sample rows from the main dataset
df_train = df_merged[~df_merged['Name'].isin(df_test['Name'])]

# Standardize and scale our numeric data
from sklearn import preprocessing
max_abs_scaler = preprocessing.MaxAbsScaler()

X_train = max_abs_scaler.fit_transform(df_train.iloc[:,2:13])
X_test = max_abs_scaler.fit_transform(df_test.iloc[:,2:13])

# Create an arrays for our labels
array_train = df_train.values
array_test = df_test.values

Y_train = array_train[:,1]
Y_test = array_test[:,1]

''' Algorithm One - Naive Bayes '''

#from sklearn.model_selection import KFold

# 10-fold cross validation
from sklearn import model_selection

# Prepare the kfold model
kfold = model_selection.KFold(n_splits=10, shuffle=True)

# Leave one out cross validation model
loocv = model_selection.LeaveOneOut()

# Train the Naive Bayes model
from sklearn.naive_bayes import GaussianNB
gnb = GaussianNB()
gnb.fit(X_train, Y_train)

# Naives Bayes with Kfolds on our Test Data
results = model_selection.cross_val_score(gnb, X_test, Y_test, cv=kfold)
results.mean()

# Naive Bayes with LOOCV on our Test Data
results2 = model_selection.cross_val_score(gnb, X_test, Y_test, cv=loocv)
results2.mean()

''' Algorithm Two  - Support Vector Machines (SVM) '''

# Import SVM
from sklearn.svm import SVC
from sklearn import svm
estimator = SVC(kernel='linear')

# Let us conduct parameter tuning with GridSearchCV to optimize our results
from sklearn.model_selection import GridSearchCV

# Prep up parameters to tune
svm_parameters = [
                  {'C': [1, 10, 100, 1000], 'kernel': ['linear']},
                  {'C': [1, 10, 100, 1000], 'gamma': [0.001, 0.0001], 'kernel': ['rbf']}
                  ]

# Train our SVM classifier
svm_classifier = GridSearchCV(estimator=estimator, cv=kfold, param_grid=svm_parameters)
svm_classifier.fit(X_train, Y_train)

# The best parameters with the score:
print('Best score for data:', svm_classifier.best_score_)
print('Best C:',svm_classifier.best_estimator_.C)
print('Best Kernel:',svm_classifier.best_estimator_.kernel)
print('Best Gamma:',svm_classifier.best_estimator_.gamma)

# Test our trained SVM classifier with our test data
svm_classifier.score(X_test, Y_test)

''' Algorithm - Random Forest '''
from sklearn.ensemble import RandomForestClassifier

# Prep parameters and the Random Forest classifier
forest_clf = RandomForestClassifier()
forest_parameters = {
                        "max_features": [2, 5, 10, 'auto'],
                        "n_estimators": [50, 100, 200],
                        "criterion": ["gini", "entropy"],
                        "min_samples_leaf": [1,2,4,6],
                    }

forest_classifier = GridSearchCV(estimator=forest_clf, param_grid=forest_parameters)

# Train our Random Forest Classifier - This will take very long to run!
forest_classifier.fit(X_train, Y_train)

# The best parameters with the score:
print('Best score:', forest_classifier.best_score_)
print('Best Max_Features:', forest_classifier.best_estimator_.max_features)
print('Best N_estimators:', forest_classifier.best_estimator_.n_estimators)
print('Best criterion:', forest_classifier.best_estimator_.criterion)
print('Best Min_samples_leaf:', forest_classifier.best_estimator_.min_samples_leaf)

# Test our trained Random Forest classifier with our test data
forest_classifier.score(X_test, Y_test)

# Compare algorithms computational difficulty
import time
start_time = time.time()
gnb.fit(X_train, Y_train)
print("Naive Bayes took", time.time() - start_time, "seconds to train.")

start_time = time.time()
svm_classifier.fit(X_train, Y_train)
print("SVM took ", time.time() - start_time, "seconds to train.")

start_time = time.time()
forest_classifier.fit(X_train, Y_train)
print("Random Forest took", time.time() - start_time, "seconds to train.")

# Visualize results
labels = ['Naive Bayes', 'SVM', 'Random Forest']
scores = [0.092, 0.235, 0.147]
train_time = [0.002, 8.379, 62.117]

fig, axs = plt.subplots(nrows=1, ncols=2)
sns.set(style="darkgrid", context="talk")
sns.barplot(x=labels, y=scores, ax=axs[0])
sns.barplot(x=labels, y=train_time, ax=axs[1])


# Autor: Daniela Estrella, A017457543
# Descripción: Convertir de grados Fahrenheit a grados Celsius.

# Empieza a escribir tu código a partir de esta línea.

fahrenheit= int(input("Ingresa la temperatura en grados Fahrenheit:"))

celsius= (fahrenheit-32)/1.8

print("La temperatura es de", celsius,"grados Celsius")


class Solution:
    """
    @param x: the wall's height
    @return: YES or NO
    """
    def isBuild(self, x):
        bricks = [3, 7]

        dp = [False for _ in range(x + 1)]
        dp[0] = True

        for height in range(1, x + 1):
            for brick in bricks:
                if height - brick < 0:
                    continue
                dp[height] = dp[height] or dp[height - brick]

        return 'YES' if dp[-1] else 'NO'