"""
Given an array of positive numbers, where each element represents the max
number of jumps that can be made forward from that element, write a program
to find the minimum number of jumps needed to reach the end of the array
(starting from the first element). If an element is 0,
then we cannot move through that element.

example1:-
    Input = {2,1,1,1,4}
    Output = 3
    Explanation: Starting from index '0', we can reach the last index through:
                 0->2->3->4

example2:-
    Input = {1,1,3,6,9,3,0,1,3}
    Output = 4
    Explanation: Starting from index '0', we can reach the last index through:
                 0->1->2->3->8
"""

import math

def count_min_jumps(jumps):
    count = count_min_jumps_recursive(jumps, 0)
    return count if count != math.inf else -1

def count_min_jumps_recursive(jumps, current_idx):
    if current_idx >= len(jumps) - 1:
        return 0

    if jumps[current_idx] == 0:
        return math.inf

    start = current_idx + 1
    end = current_idx + jumps[current_idx]
    count = math.inf
    while start <= end and start < len(jumps):
        min_jump = count_min_jumps_recursive(jumps, start)
        start += 1
        if min_jump != math.inf:
            count = min(count, min_jump + 1)

    return count

print("======== count min jumps brute force ==========")
print(count_min_jumps([2, 1, 1, 1, 4]))
print(count_min_jumps([1, 2, 6, 2, 9, 3, 0, 1, 3]))
print(count_min_jumps([1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9]))
print(count_min_jumps([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]))
print(count_min_jumps([3, 0, 8, 2, 0, 0, 1]))
print(count_min_jumps([0, 0, 0, 0]))

def count_min_jumps_dp_top_bottom(jumps):
    dp = [math.inf for i in range(len(jumps))]
    count = count_min_jumps_dp_top_bottom_recursive(jumps, dp, 0)
    return count if count != math.inf else -1

def count_min_jumps_dp_top_bottom_recursive(
    jumps: list, dp: list, current_idx: int
) -> float:
    if current_idx >= len(jumps) - 1:
        return 0

    if jumps[current_idx] == 0:
        return math.inf

    if dp[current_idx] == math.inf:
        start = current_idx + 1
        end = current_idx + jumps[current_idx]
        count = math.inf
        while start <= end and start < len(jumps):
            min_jumps = count_min_jumps_dp_top_bottom_recursive(jumps, dp, start)
            start += 1
            if min_jumps != math.inf:
                count = min(count, min_jumps + 1)
                dp[current_idx] = count

    return dp[current_idx]

print("======== count min jumps dp top bottom recursive ==========")
print(count_min_jumps_dp_top_bottom([2, 1, 1, 1, 4]))
print(count_min_jumps_dp_top_bottom([1, 2, 6, 2, 9, 3, 0, 1, 3]))
print(count_min_jumps_dp_top_bottom([1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9]))
print(count_min_jumps_dp_top_bottom([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]))
print(count_min_jumps_dp_top_bottom([3, 0, 8, 2, 0, 0, 1]))
print(count_min_jumps_dp_top_bottom([0, 0, 0, 0]))

def count_min_jumps_dp_tabular_bottom_up(jumps):
    n = len(jumps)
    dp = [math.inf for i in range(n)]
    dp[0] = 0

    for start in range(n - 1):
        end = start + 1
        while end <= start + jumps[start] and end < n:
            dp[end] = min(dp[end], dp[start] + 1)
            end += 1
    return dp[n - 1] if dp[n - 1] != math.inf else -1

print("======== count min jumps dp bottom up tabular ==========")
print(count_min_jumps_dp_tabular_bottom_up([2, 1, 1, 1, 4]))
print(count_min_jumps_dp_tabular_bottom_up([1, 2, 6, 2, 9, 3, 0, 1, 3]))
print(count_min_jumps_dp_tabular_bottom_up([1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9]))
print(count_min_jumps_dp_tabular_bottom_up([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]))
print(count_min_jumps_dp_tabular_bottom_up([3, 0, 8, 2, 0, 0, 1]))
print(count_min_jumps_dp_tabular_bottom_up([0, 0, 0, 0]))


from selenium import webdriver
from selenium.webdriver.chrome.service import Service

chrome_driver_path = Service("C:\chromedriver.exe")

driver = webdriver.Chrome(service=chrome_driver_path)
driver.get("https://www.python.org/")

# search_bar = driver.find_element_by_name("q")
# print(search_bar.get_attribute("placeholder"))

# logo = driver.find_element_by_class_name("python-logo")
# print(logo.size)

# documentation_link = driver.find_element_by_css_selector(".documentation-widget a")
# print(documentation_link.text)

bug_link = driver.find_element_by_xpath("//*[@id='site-map']/div[2]/div/ul/li[3]/a")
print(bug_link.text)

driver.quit()


from ansible import errors

INTERFACE_MAPPING = {
    "as": "Async",
    "br": "Bridge",
    "di": "Dialer",
    "et": "Ethernet",
    "fa": "FastEthernet",
    "fo": "FortyGigabitEthernet",
    "gi": "GigabitEthernet",
    "lo": "Loopback",
    "mg": "Mgmt",
    "po": "Port-Channel",
    "se": "Serial",
    "te": "TenGigabitEthernet",
    "tu": "Tunnel",
    "vl": "Vlan",
    "vx": "Vxlan",
}

class FilterModule:
    """Defines a filter module object."""

    @staticmethod
    def filters():
        """Returns the Ansible filter bindings dictionary."""
        return {
            "expand_interface_name": FilterModule.expand_interface_name,
            "expand_interface_names": FilterModule.expand_interface_names,
        }

    @staticmethod
    def expand_interface_name(interface_name):
        """Expands abbreviated interface names."""
        return INTERFACE_MAPPING.get(
            interface_name.strip().lower()[0:2], interface_name
        )

    @staticmethod
    def expand_interface_names(interface_names):
        """Expands abbreviated interface names."""
        if isinstance(interface_names, str):
            # Same as previously, it's just one interface name
            return INTERFACE_MAPPING.get(
                interface_names.strip().lower()[0:2], interface_names
            )
        elif isinstance(interface_names, list):
            expanded_names = []
            for name in interface_names:
                long_name = INTERFACE_MAPPING.get(name.strip().lower()[0:2], name)
                expanded_names.append(long_name)
            return expanded_names
        else:
            raise errors.AnsibleFilterError(
                "expand_interface_names filter error: provide a string or a list of strings!"
            )


"""
Crio uma coluna nova com o timeframe que sera testado e os respectivos preços
coluna OHLC sera para testar o TPSL
um unico loop onde, quando nao for nan e flag abrir operacao == True, abre operacao
else, apenas passa pelos preços OHLC
"""

@njit(parallel=True)
def single_backtest(array, multiply_tp, multiply_sl, eur = True, balance = 1000):
    """
    Caso for operar moedas nao eur, mudar o sistema do array
    array = [0] Open
            [1] High
            [2] Low
            [3] Close
            [4] Preços do TimeFrame
            [5] TPSL
            [6] Sell Flag
            [7] Buy Flag
    tpsl = TakeProfit e StopLoss int > 0
    multiply_tpsl = float > 0
                    Será o valor que irá dividir SL
                    Se 1 significa risco 1:1, se 2, 2:1 and so on
    ---
    buy_sell: 0 = Compra
              1 = Venda
    operacao_info: 0 = Preço de Compra
                   1 = Preço de Venda
                   2 = TP Venda
                   3 = SL Venda
                   4 = TP Compra
                   5 = SL Compra
    """

    balance_backtest = balance

    operacao_info = np.zeros((6),dtype=np.float32)
    buy_sell = np.ones((2),dtype=np.bool_)

    buy_sell_tot_trades_indi = np.zeros(3, dtype=np.int32)

    buy_orders = np.zeros((array[0].size), dtype=np.float32)
    sell_orders = np.zeros((array[0].size), dtype=np.float32)
    buy_orders[0] = balance_backtest
    sell_orders[0] = balance_backtest
    buy_sell_tot_trades_indi[0] += 1
    buy_sell_tot_trades_indi[1] += 1

    tot_trades = np.zeros((array[0].size), dtype=np.float32)
    tot_trades[0] = balance_backtest
    buy_sell_tot_trades_indi[2] += 1

    #tk_normal = round(tpsl / 10000,5)
    #sl_normal = round(tpsl / 10000 / multiply_tpsl, 5)
    #tk_jpy = round(tpsl / 100)
    #sl_jpy = round(tpsl / 100 / multiply_tpsl,5)

    for i in np.arange(array[0].size): # Array tamanho

        if array[7][i] and buy_sell[0] and array[4][i] != np.nan: #Compra
            operacao_info[0] = array[4][i]
            buy_sell[0] = False
            if eur:
                operacao_info[4] = array[4][i] + round(multiply_tp * array[5][i],3) # TP
                #operacao_info[4] = array[0][i] + tk_jpy
                operacao_info[5] = array[4][i] - round(multiply_sl * array[5][i],3) # SL
                #operacao_info[5] = array[0][i] - sl_jpy
            else:
                operacao_info[4] = array[4][i] + round(multiply_tp * array[5][i],5) # TP
                #operacao_info[4] = array[0][i] + tk_normal
                operacao_info[5] = array[4][i] - round(multiply_sl * array[5][i],5) # SL
                #operacao_info[5] = array[0][i] - sl_normal
        elif array[6][i] and buy_sell[1] and array[4][i] != np.nan:
            operacao_info[1] = array[4][i]
            buy_sell[1] = False
            if eur:
                operacao_info[2] = array[4][i] - round(multiply_tp * array[5][i],3) # TP
                #operacao_info[2] = array[0][i] - tk_jpy
                operacao_info[3] = array[4][i] + round(multiply_sl * array[5][i],3) # SL
                #operacao_info[3] = array[0][i] + sl_jpy
            else:
                operacao_info[2] = array[4][i] - round(multiply_tp * array[5][i],5) # TP
                #operacao_info[2] = array[0][i] - tk_normal
                operacao_info[3] = array[4][i] + round(multiply_sl * array[5][i],5) # SL
                #operacao_info[3] = array[0][i] + sl_normal

        check_sell_sl = (array[0][i] >= operacao_info[3]) or (array[1][i] >= operacao_info[3]) or (array[2][i] >= operacao_info[3]) or (array[3][i] >= operacao_info[3])
        check_sell_buy = (array[0][i] <= operacao_info[2]) or (array[1][i] <= operacao_info[2]) or (array[2][i] <= operacao_info[2]) or (array[3][i] <= operacao_info[2])
        check_buy_sl = (array[0][i] <= operacao_info[5]) or (array[1][i] <= operacao_info[5]) or (array[2][i] <= operacao_info[5]) or (array[3][i] <= operacao_info[5])
        check_buy_tp = (array[0][i] >= operacao_info[4]) or (array[1][i] >= operacao_info[4]) or (array[2][i] >= operacao_info[4]) or (array[3][i] >= operacao_info[4])

        if array[4][i] != np.nan:
            if buy_sell[0] == False and check_buy_sl:
                balance_backtest, buy_result = finance_calculation(balance=balance_backtest, saldo_inicial=operacao_info[5],
                                                                   saldo_final=operacao_info[0], eur = eur,
                                                                   preco_eur=1)
                tot_trades[buy_sell_tot_trades_indi[2]] = balance_backtest
                buy_orders[buy_sell_tot_trades_indi[0]] = buy_orders[buy_sell_tot_trades_indi[0]-1] + buy_result
                buy_sell_tot_trades_indi[0] += 1
                buy_sell_tot_trades_indi[2] += 1
                buy_sell[0] = True
                break
            elif buy_sell[0] == False and check_buy_tp:
                balance_backtest, buy_result = finance_calculation(balance=balance_backtest, saldo_inicial=operacao_info[4],
                                                                   saldo_final=operacao_info[0], eur = eur,
                                                                   preco_eur=1)
                tot_trades[buy_sell_tot_trades_indi[2]] = balance_backtest
                buy_orders[buy_sell_tot_trades_indi[0]] = buy_orders[buy_sell_tot_trades_indi[0]-1] + buy_result
                buy_sell_tot_trades_indi[0] += 1
                buy_sell_tot_trades_indi[2] += 1
                buy_sell[0] = True
                break

            if buy_sell[1] == False and check_sell_sl:
                balance_backtest, sell_result = finance_calculation(balance=balance_backtest,saldo_inicial=operacao_info[1],
                                                                    saldo_final=operacao_info[3], eur = eur,
                                                                    preco_eur=1)
                tot_trades[buy_sell_tot_trades_indi[2]] = balance_backtest
                sell_orders[buy_sell_tot_trades_indi[1]] = sell_orders[buy_sell_tot_trades_indi[1]-1] + sell_result
                buy_sell_tot_trades_indi[1] += 1
                buy_sell_tot_trades_indi[2] += 1
                buy_sell[1] = True
                break
            elif buy_sell[1] == False and check_sell_buy:
                balance_backtest, sell_result = finance_calculation(balance=balance_backtest, saldo_inicial=operacao_info[1],
                                                                    saldo_final=operacao_info[2], eur = eur,
                                                                    preco_eur=1)
                tot_trades[buy_sell_tot_trades_indi[2]] = balance_backtest
                sell_orders[buy_sell_tot_trades_indi[1]] = sell_orders[buy_sell_tot_trades_indi[1]-1] + sell_result
                buy_sell_tot_trades_indi[1] += 1
                buy_sell_tot_trades_indi[2] += 1
                buy_sell[1] = True
                break

    return tot_trades, sell_orders, buy_orders


from sqlalchemy import Column, String, Integer, Boolean, ForeignKey, Table
from sqlalchemy.orm import relation, mapper, synonym, deferred

from eos.db import gamedata_meta
from eos.types import Item, MarketGroup, Icon
import wx
_ = wx.GetTranslation
marketgroups_table = Table(_("invmarketgroups_en"), gamedata_meta,
                           Column("marketGroupID", Integer, primary_key = True),
                           Column("marketGroupName", String),
                            Column("trnmarketGroupName", String),
                           Column("description", String),
                           Column("hasTypes", Boolean),
                           Column("parentGroupID", Integer, ForeignKey(_("invmarketgroups_en.marketGroupID"), initially="DEFERRED", deferrable=True)),
                           Column("iconID", Integer, ForeignKey("icons.iconID")))

mapper(MarketGroup, marketgroups_table,
       properties = {"items" : relation(Item, backref = "marketGroup"),
                     "parent" : relation(MarketGroup, backref = "children", remote_side = [marketgroups_table.c.marketGroupID]),
                     "icon" : relation(Icon),
                     "ID" : synonym("marketGroupID"),
                     "name" : synonym("marketGroupName"),
                     "trnname" : synonym("trnmarketGroupName"),
                     "description" : deferred(marketgroups_table.c.description)})


from struct import pack
from struct import pack_into
from struct import unpack_from
import sys
import time
from ctypes import create_string_buffer

# Helper Classes

# Byte-oriented IO
class _ByteIO:
    _debug = False;

    def recv_bytes(self, sock, n, timeout=0):
      b = bytearray()
      r = 0

      tNow = time.time()
      tEnd = tNow + timeout
      tStart = tNow

      while (r < n):
        if (timeout > 0):
          sock.settimeout(tEnd - tNow)

        p = sock.recv(n-r) # partial recv
        b.extend(p)
        r += len(p)
        tNow = time.time()

      return [b, (tNow-tStart)]

# Better: r += sock.recv_into(buffer(b, r), (n-r));
# But doesn't work on MacOS X :(

    def recv_byte_array(self, sock, timeout=0):
      [s, t0] = self.recv_bytes(sock, 4, timeout)
      sz = unpack_from('>I', buffer(s))[0]

      tr = 0;
      if (timeout > 0):
          if (t0 < timeout):
              tr = timeout - t0;
          else:
              tr = 0.001;

      [m, t1] = self.recv_bytes(sock, sz, tr)

      if self._debug:
          print >> sys.stderr, ">>[" + str(sz) + "]" + str(m)

      return [m, t0+t1]

    def send_byte_array(self, sock, b):
      sz = pack('>I', len(b));
      sock.sendall(sz);
      sock.sendall(b);

      if self._debug:
          print >> sys.stderr, "<<[" + str(len(b)) + "]" + str(b)


N, k =map(int, input().split())
coin=[]
for _ in range(N):
    coin.append(int(input()))
many = 0
for i in reversed(coin):
    if k>=i:
        many+=k//i
        k-=(k//i)*i
print(many)


# -*- coding: utf-8 -*-
# @Author: Cody Kochmann
# @Date:   2017-11-22 21:05:34
# @Last Modified 2017-11-22
# @Last Modified time: 2017-11-22 21:45:39

from functools import wraps

class on_fail(object):
    def __init__(self, output_on_fail, logger=None):
        assert logger is None or callable(logger), 'on_fail-logger needs to be a callable function, not {}'.format(logger)
        self.output_on_fail = output_on_fail
        self.logger = logger

    def __call__(self, fn):
        @wraps(fn)
        def wrapper(*a, **k):
            try:
                return fn(*a, **k)
            except Exception as ex:
                if self.logger is not None:
                    self.logger(ex)
                return self.output_on_fail
        return wrapper

if __name__ == '__main__':
    from logging import warning

    @on_fail(None)
    def test1(a,b):
        return a/b

    print(test1(3.0, 5))
    print(test1(3.0, 0))

    @on_fail(None, warning)
    def test2(a,b):
        return a/b

    print(test2(3.0, 5))
    print(test2(3.0, 0))


import pytest

from pliffy import utils

def test_abd_default():
    abd = utils.ABD()
    assert (abd.a, abd.b, abd.diff) == (None, None, None)

def test_abd_asignement():
    abd = utils.ABD(1, 2, 'milk')
    assert (abd.a, abd.b, abd.diff) == (1, 2, 'milk')

def test_abd_attribute_error():
    abd = utils.ABD()
    with pytest.raises(
            AttributeError,
            match="can't set attribute",):
        abd.a = 1

def test_pliffy_info_defaults(pliffy_info_abd_default_asdict):
    actual = utils.PliffyInfoABD()
    assert actual._asdict() == pliffy_info_abd_default_asdict

def test_pliffy_info_set_data(pliffy_info_abd_custom_asdict):
    actual = utils.PliffyInfoABD(
        data_a=[1, 2, 3, 4, 5],
        data_b=[1, 4, 6, 7, 9],
        ci_percentage=99,
        design="paired",
        measure_units="Amplitude (Volts)",
        xtick_labels=utils.ABD(a="Biceps", b="Triceps", diff="Effect"),
        decimals=4,
        plot_name="arm",
        save=True,
        save_path="/home/martin/Desktop/",
        save_type="svg",
        dpi=600,
        marker=utils.ABD(a="*", b="v", diff="."),
        marker_color=utils.ABD(a="tab:red", b="tab:blue", diff="tab:green"),
        summary_marker_size=utils.ABD(a=6, b=4, diff=2),
        raw_marker_size=utils.ABD(a=6, b=5, diff=3),
        raw_marker_transparency=0.1,
        paired_data_joining_lines=False,
        paired_data_line_color="grey",
        paired_data_line_width=2,
        paired_line_transparency=0.3,
        paired_data_plot_raw_diff=False,
        ci_line_width=2,
        fontsize=12,
        show=False,
        zero_line_color="grey",
        zero_line_width=1,
        width_height_in_inches=(3.23, 3.23),
    )
    assert actual._asdict() == pliffy_info_abd_custom_asdict


import colander
import deform
from zope.interface import implementer, invariant
from pyramid.threadlocal import get_current_request

from substanced.content import content
from substanced.schema import NameSchemaNode
from substanced.util import renamer, get_oid

from dace.objectofcollaboration.entity import Entity
from dace.descriptors import (
    SharedMultipleProperty, SharedUniqueProperty,
    CompositeUniqueProperty)
from dace.util import get_obj
from pontus.file import File, ObjectData
from pontus.schema import Schema, omit, select
from pontus.core import VisualisableElement, VisualisableElementSchema
from pontus.widget import (
    Select2Widget,
    SimpleMappingWidget,
    AjaxSelect2Widget,
    SequenceWidget)
from deform_treepy.widget import (
    DictSchemaType)

from .interface import ISmartFolder
from novaideo import _, VIEW_TYPES, log, AVAILABLE_LANGUAGES, LANGUAGES_TITLES
from novaideo.widget import (
    CssWidget,
    TextInputWidget,
    BootstrapIconInputWidget
    )
from novaideo.views.filter import FilterSchema
from novaideo.content import get_file_widget

DEFAULT_FOLDER_COLORS = {'usual_color': 'white, #2d6ca2',
                         'hover_color': 'white, #2d6ca2'}

def context_is_a_smartfolder(context, request):
    return request.registry.content.istype(context, 'smartfolder')

@colander.deferred
def view_type_widget(node, kw):
    values = list(VIEW_TYPES.items())
    values.insert(0, ('', _('- Select -')))
    return Select2Widget(css_class="viewtype-field",
                         values=values)

class CssSchema(Schema):
    usual_color = colander.SchemaNode(
        colander.String(),
        widget=CssWidget(),
        title=_('Usual color'),
        # description=('Choisir la couleur du texte et du fond de la section de menu.'),
        description=_('Choose the text and background color of the menu section.'),
        )

    hover_color = colander.SchemaNode(
        colander.String(),
        widget=CssWidget(),
        title=_('Hover color'),
        # description=('Choisir la couleur du texte et du fond de la section de menu au survol de la souris.'),
        description=_('Choose the text and background color of the menu section on mouse-over.')
        )

@colander.deferred
def relatedcontents_choice(node, kw):
    request = node.bindings['request']
    context = node.bindings['context']
    root = request.root
    values = []
    if isinstance(context, SmartFolder):
        values = [(get_oid(t), t.title) for
                  t in context.contents]

    def title_getter(id):
        try:
            obj = get_obj(int(id), None)
            if obj:
                return obj.title
            else:
                return id
        except Exception as e:
            log.warning(e)
            return id

    ajax_url = request.resource_url(
        root, '@@novaideoapi',
        query={'op': 'find_smart_folder_contents'})
    return AjaxSelect2Widget(
        values=values,
        ajax_url=ajax_url,
        ajax_item_template="related_item_template",
        title_getter=title_getter,
        multiple=True,
        page_limit=50,)

@colander.deferred
def locale_widget(node, kw):
    locales = [(l, LANGUAGES_TITLES.get(l, l)) for l in AVAILABLE_LANGUAGES]
    sorted_locales = sorted(locales)
    sorted_locales.insert(0, ('', _('- Select -')))
    return Select2Widget(values=sorted_locales)

class SmartFolderSchema(VisualisableElementSchema):
    """Schema for keyword"""

    name = NameSchemaNode(
        editing=context_is_a_smartfolder,
        )

    title = colander.SchemaNode(
        colander.String(),
        widget=TextInputWidget(css_class="smartfolder-title-field"),
        title=_('Title'),
        )

    description = colander.SchemaNode(
        colander.String(),
        widget=deform.widget.TextAreaWidget(rows=4, cols=60),
        title=_("Description"),
        )

    locale = colander.SchemaNode(
        colander.String(),
        title=_('Locale'),
        description=_('The language for which the folder will be displayed'),
        widget=locale_widget,
        missing=''
    )

    cover_picture = colander.SchemaNode(
        ObjectData(File),
        widget=get_file_widget(file_extensions=['png', 'jpg', 'svg']),
        title=_('Cover picture'),
        missing=None,
        description=_("Only PNG and SVG files are supported."),
        )

    filters = colander.SchemaNode(
        colander.Sequence(),
        omit(select(FilterSchema(
                    name='filter',
                    title=_('Filter'),
                    widget=SimpleMappingWidget(
                        css_class='object-well default-well')),
             ['metadata_filter',
              'temporal_filter', 'contribution_filter',
              'text_filter', 'other_filter']),
             ["_csrf_token_"]),
        widget=SequenceWidget(
            add_subitem_text_template=_('Add a new filter')),
        title=_('Filters'),
        description=_('Applied filters'),
        missing=[]
        )

    contents = colander.SchemaNode(
        colander.Set(),
        widget=relatedcontents_choice,
        title=_('Associated contents'),
        description=_('Choose the contents to be associated'),
        missing=[],
        default=[],
        )

    view_type = colander.SchemaNode(
        colander.String(),
        widget=view_type_widget,
        title=_("View type"),
        description=_('How to display contents'),
        default='default'
        )

    icon_data = colander.SchemaNode(
        DictSchemaType(),
        widget=BootstrapIconInputWidget(),
        title=_('Icon'),
        default={'icon': 'glyphicon-folder-open',
                 'icon_class': 'glyphicon'},
        description=_('Select an icon.')
        )

    style = omit(CssSchema(widget=SimpleMappingWidget()), ["_csrf_token_"])

    @invariant
    def contact_invariant(self, appstruct):
        contents = appstruct.get('contents', [])
        filters = appstruct.get('filters', [])
        if not contents and not filters:
            raise colander.Invalid(
                self, _('Filters or associated contents must be specified.'))

@content(
    'smartfolder',
    icon='glyphicon glyphicon-align-left',
    )
@implementer(ISmartFolder)
class SmartFolder(VisualisableElement, Entity):
    """SmartFolder class"""

    default_icon = 'glyphicon glyphicon-briefcase'
    templates = {
        'default': 'novaideo:views/templates/folder_result.pt',
        'bloc': 'novaideo:views/templates/folder_result.pt',
        'header': 'novaideo:views/templates/folder_header.pt',
    }
    name = renamer()
    children = SharedMultipleProperty('children', 'parents')
    parents = SharedMultipleProperty('parents', 'children')
    author = SharedUniqueProperty('author', 'folders')
    contents = SharedMultipleProperty('contents')
    cover_picture = CompositeUniqueProperty('cover_picture')

    def __init__(self, **kwargs):
        super(SmartFolder, self).__init__(**kwargs)
        self.set_data(kwargs)
        self.folder_order = None

    @property
    def parent(self):
        parents = self.parents
        return parents[0] if parents else None

    @property
    def root(self):
        parent = self.parent
        return parent.root if parent else self

    @property
    def folder_lineage(self):
        result = [self]
        parent = self.parent
        if parent:
            parent_result = parent.folder_lineage
            parent_result.extend(result)
            result = parent_result

        return result

    @property
    def icon(self):
        icon = getattr(self, 'icon_data', {})
        if icon:
            return icon.get('icon_class') + ' ' + icon.get('icon')
        else:
            return self.default_icon

    @property
    def url(self,):
        request = get_current_request()
        return self.get_url(request)

    def contains(self, folder):
        if folder is None:
            return False

        if folder is self:
            return True

        return any(c.contains(folder) for c in self.children)

    def all_sub_folders(self, state=None):
        if state:
            result = [f for f in self.children if state in f.state]
        else:
            result = list(self.children)

        for sub_f in list(result):
            result.extend(sub_f.all_sub_folders(state))

        return list(set(result))

    def get_order(self):
        folder_order = getattr(self, 'folder_order', None)
        if folder_order is None:
            root = self.__parent__
            folders = root.smart_folders
            if self in folders:
                self.set_order(folders.index(self))

        return getattr(self, 'folder_order', None)

    def set_order(self, order):
        self.folder_order = order

    def get_url(self, request):
        return request.resource_url(
            request.root, 'open', query={'folderid': get_oid(self)})


#From https://github.com/Ameridzuan

import scipy.io
import numpy as np

#csv file name
csvname = "your_csv_filename.csv"

#read and load the content into variable named "data"
data = scipy.io.loadmat("your_mathlab_filename.mat")

for header in data:
    #view all data in MATHLAB file
    #print(data)

    #view header available in MATHLAB file
    print(header)

    #exclude MATHLAB file header to be written in csv
    if "__" not in header:
        np.savetxt((csvname),data[header],delimiter=',', fmt='%s')


import os
from datetime import datetime
from dateutil.parser import parse as date_parser

from django.conf import settings
from django.urls import reverse

from sources.extraction.base import SingleResponseExtractProcessor
from sources.extraction.hanze.research_themes import FOCUS_AREA_TO_RESEARCH_THEME
from sources.extraction.pure import PureAPIMixin

class HanzePersonsExtractProcessor(SingleResponseExtractProcessor, PureAPIMixin):
    @classmethod
    def get_name(cls, node):
        return f"{node['name']['firstName']} {node['name']['lastName']}"

    @classmethod
    def get_isni(cls, node):
        isni_identifier = next(
            (identifier for identifier in node.get("identifiers", [])
             if "isni" in identifier.get("type", {}).get("uri", "")),
            None
        )
        return isni_identifier["id"] if isni_identifier else None

    @classmethod
    def get_is_employed(cls, node):
        today = datetime.today()
        for association in node.get("staffOrganizationAssociations", []):
            end_date = association["period"].get("endDate", None)
            if not end_date or date_parser(end_date) > today:
                break
        else:
            return False
        return True

    @classmethod
    def get_photo_url(cls, node):
        photo_list = node.get("profilePhotos", None)
        if not photo_list:
            return
        photo_url = photo_list[0].get("url", None)
        if not photo_url:
            return
        file_path_segment = "/nppo/persons/"
        if file_path_segment not in photo_url:
            return photo_url  # not dealing with a url we recognize as a file url
        start = photo_url.index(file_path_segment)
        file_path = photo_url[start + len(file_path_segment):]
        proxy_photo_url = reverse("v1:files", kwargs={
            "source": "hanze",
            "file_path": file_path
        })
        return f"https://{settings.DOMAIN}{proxy_photo_url}"

    @classmethod
    def get_job_title(cls, node):
        today = datetime.today()
        for association in node.get("staffOrganizationAssociations", []):
            end_date = association.get("period", None).get("endDate", None)
            if not end_date or date_parser(end_date, ignoretz=True) > today:
                break
        else:
            return
        job_title_object = association.get("jobTitle", None)
        if not job_title_object:
            return
        return next(iter(job_title_object["term"].values()), None)

HanzePersonsExtractProcessor.OBJECTIVE = {
    "external_id": "$.uuid",
    "name": HanzePersonsExtractProcessor.get_name,
    "first_name": "$.name.firstName",
    "last_name": "$.name.lastName",
    "prefix": lambda node: None,
    "initials": lambda node: None,
    "title": lambda node: None,
    "email": "$.user.email",
    "phone": lambda node: None,
    "skills": lambda node: [],
    "themes": lambda node: [],
    "description": lambda node: None,
    "parties": lambda node: [],
    "photo_url": HanzePersonsExtractProcessor.get_photo_url,
    "isni": HanzePersonsExtractProcessor.get_isni,
    "dai": lambda node: None,
    "orcid": "$.orcid",
    "is_employed": HanzePersonsExtractProcessor.get_is_employed,
    "job_title": HanzePersonsExtractProcessor.get_job_title,
    "research_themes": lambda node: [],
}

class HanzeProjectExtractProcessor(SingleResponseExtractProcessor, PureAPIMixin):

    @classmethod
    def get_status(cls, node):
        today = datetime.today()
        end_date = node.get("period", {}).get("endDate")
        return "ongoing" if not end_date or today <= date_parser(end_date) else "finished"

    @classmethod
    def get_title(cls, node):
        title = node["title"]
        return list(title.values())[0]

    @classmethod
    def get_description(cls, node):
        # Gather all possible descriptions
        language_code = None
        descriptions = {}
        for description in node["descriptions"]:
            if "value" not in description:
                continue
            if language_code is None:
                language_code = list(description["value"].keys())[0]
            description_type = os.path.split(description["type"]["uri"])[1]
            description_text = description["value"].get(language_code, None)
            if description_text:
                descriptions[description_type] = description_text
        # Concatenate different descriptions to be a singular text
        description = ""
        if "laymansdescription" in descriptions:
            description += descriptions["laymansdescription"]

        if "keyfindings" in descriptions:
            if description:
                description += "

"
            description += descriptions["keyfindings"]
        if "projectdescription" in descriptions:
            if description:
                description += "

"
            description += descriptions["projectdescription"]
        return description or None

    @classmethod
    def get_keywords(cls, node):
        keyword_groups = [
            keyword_group for keyword_group in node.get("keywordGroups", [])
            if keyword_group.get("logicalName", None) == "keywordContainers"
        ]
        if not keyword_groups:
            return []
        keywords = []
        for keyword_group in keyword_groups:
            keywords += keyword_group["keywords"][0]["freeKeywords"]
        return keywords

    @classmethod
    def get_products(cls, node):
        return [product["researchOutput"]["uuid"] for product in node.get("researchOutputs", [])]

    @classmethod
    def get_persons(cls, node):
        persons = []
        for participant in node.get("participants", []):
            if "person" in participant:
                person = participant["person"]
            elif "externalPerson" in participant:
                person = participant["externalPerson"]
            else:
                continue
            persons.append({
                "external_id": person["uuid"],
                "email": None,
                "name": f"{participant['name']['firstName']} {participant['name']['lastName']}"
            })
        return persons

    @classmethod
    def get_owners(cls, node):
        persons = cls.get_persons(node)
        return [persons[0]] if persons else []

    @classmethod
    def get_research_themes(cls, node):
        research_themes = []
        for keywords in node.get("keywordGroups", []):
            if keywords["logicalName"] == "research_focus_areas":
                for classification in keywords["classifications"]:
                    if classification["uri"] in FOCUS_AREA_TO_RESEARCH_THEME.keys():
                        research_themes.append(FOCUS_AREA_TO_RESEARCH_THEME[classification["uri"]])
        return research_themes

HanzeProjectExtractProcessor.OBJECTIVE = {
    "external_id": "$.uuid",
    "title": HanzeProjectExtractProcessor.get_title,
    "status": HanzeProjectExtractProcessor.get_status,
    "started_at": "$.period.startDate",
    "ended_at": "$.period.endDate",
    "coordinates": lambda node: [],
    "goal": lambda node: None,
    "description": HanzeProjectExtractProcessor.get_description,
    "contacts": HanzeProjectExtractProcessor.get_owners,
    "owners": HanzeProjectExtractProcessor.get_owners,
    "persons": HanzeProjectExtractProcessor.get_persons,
    "keywords": HanzeProjectExtractProcessor.get_keywords,
    "parties": lambda node: [],
    "products": HanzeProjectExtractProcessor.get_products,
    "research_themes": HanzeProjectExtractProcessor.get_research_themes,
}


#!/usr/bin/env python3

import os
import sys
import requests
import shlex

def bulleted_list(preface, items):

	list_ = preface

	# format each item in bullet form
	for item in items:
		list_ += "\n\t* {}".format(item)

	return list_

def run(kiwi, args):

	# list modules
	if args.list_modules:
		installed = kiwi.get_installed_module_list()

		# try getting remote module list
		try:
			modules = kiwi.get_remote_module_list()
		except requests.exceptions.RequestException:
			kiwi.say("could not reach remote to fetch module list, listing local modules only")
			modules = installed[:]

		# compare modules and mark as installed
		for module in modules:
			print('[{}] {}: {}'.format('x' if module in installed else ' ', module, kiwi.get_module_description(module)))
			if module in installed:
				installed.remove(module)

		# list unknown modules
		for module in installed:
			print('[{}] {}: {}'.format('?', module, kiwi.get_module_description(module)))

	# fetching and updating modules have the same logic behind them
	if args.get_modules is not None or args.update_modules is not None:
		modules = args.get_modules if args.get_modules is not None else args.update_modules

		# fetching / updating all modules
		if not len(modules):

			# collect remote module list if fetching, local list if updating
			if args.get_modules is not None:
				try:
					modules = kiwi.get_remote_module_list()
				except requests.exceptions.RequestException:
					kiwi.say("could not reach remote to fetch module list")
					sys.exit(1)
			else:
				modules = kiwi.get_installed_module_list()

		# fetch / update collected modules
		modules_fetched, modules_update, modules_failed = kiwi.fetch_modules(modules, args.update_modules != None)

		# fetch results
		if args.get_modules:
			kiwi.say(bulleted_list('fetch results', [
				'{} new modules fetched'.format(len(modules_fetched)),
				'{} modules could not be fetched'.format(len(modules_failed)),
				'{} modules have an available update'.format(len(modules_update)),
				'{} modules are present and up to date'.format(len(modules) - len(modules_fetched) - len(modules_update) - len(modules_failed))
			]))

		# update outdated modules if need be
		if len(modules_update) > 0:
			if kiwi.Helper.ask('\nUpdate the outdated modules?', ['y', 'n'], 'y' if args.yes else None) == 'y':
				modules_fetched, _, modules_failed = kiwi.fetch_modules(modules_update, True)
			else:
				sys.exit(0)

		# update results
		if args.update_modules or len(modules_update) > 0:
			kiwi.say(bulleted_list('update results', [
				'{} modules were updated'.format(len(modules_fetched)),
				'{} modules could not be updated'.format(len(modules_failed))
			]))

	# kiwi self update
	if args.self_update:

		if kiwi.runtime.update("I have an update", args.yes):
			kiwi.say("I'm up to date")

	# dump current configuration to file
	if args.dump_config:
		with open(args.dump_config, 'w') as config_file:
			config_file.write(kiwi.config.dump())

		kiwi.say("dumped current configuration to '{}'".format(args.dump_config))

	# start kiwi server
	if args.start_server:
		return kiwi.runtime.run(kiwi.runtime.Modules.Server, kiwi)

	# run kiwi module
	if args.module:
		return kiwi.run_module(args.module[0], shlex.join(args.module[1:]), client=(not args.server), foreground=True)


import math
def sort_it(theArray):
    newArr = []
    oddArr = []
    evenArr = []
    for i in theArray:
        if math.floor(i) % 2 == 1:
            oddArr.append(math.floor(i))
        else:
            evenArr.append(math.floor(i))

    oddArr.sort()
    evenArr.sort(reverse=True)
    newArr = oddArr + evenArr

    return newArr

print(sort_it([1, 2, 3, 4, 5, 6, 7, 8, 9]))
print(sort_it([26.66, 24.01, 52.00, 2.10, 44.15, 1.02, 11.15]))


import os
import pickle
import src.constants as cst
import src.time_wrapper as twr

@twr.timeit
def print_dict_to_pickle(name="no_name_given", dictionary={}, OUT=cst.DATA_PATH):
    """ Writes """
    if not os.path.exists(OUT):  # make the directory thing
        os.makedirs(OUT)
    with open(OUT + "/" + name + ".pickle", "wb") as handle:
        pickle.dump(dictionary, handle, protocol=pickle.HIGHEST_PROTOCOL)
    # I can't remember any more why the protocol has to be the highest

@twr.timeit
def read_pickle_to_dict(name="no_name_given", OUT=cst.DATA_PATH):
    """ Reads """
    if not os.path.exists(OUT):  # make the directory thing
        os.makedirs(OUT)
    with open(OUT + "/" + name + ".pickle", "rb") as handle:
        return pickle.load(handle)
    # I can't remember any more why the protocol has to be the highest

@twr.timeit
def circular_write_out(co, sy):
    """ Writes output """
    if not os.path.exists(co.out):  # make the directory thing
        os.makedirs(co.out)
    with open(os.path.join(co.out, co.name + "_co.pickle"), "wb") as handle:
        pickle.dump(co, handle, protocol=pickle.HIGHEST_PROTOCOL)
    # I can't remember any more why the protocol has to be the highest
    with open(os.path.join(co.out, + co.name + "_sy.pickle"), "wb") as handle:
        pickle.dump(sy, handle, protocol=pickle.HIGHEST_PROTOCOL)

@twr.timeit
def read_in(OUTPUT_DIREC, name):
    """load pickles"""
    print("looking for" + os.path.join(OUTPUT_DIREC, name + "_co.pickle"))
    with open(os.path.join(OUTPUT_DIREC, name + "_co.pickle"), "rb") as handle:
        co = pickle.load(handle)
    with open(os.path.join(OUTPUT_DIREC, name + "_sy.pickle"), "rb") as handle:
        sy = pickle.load(handle)
    return co, sy


from multiprocessing import Process

from lib.panel import Panel

class Shobaleader:
    """Orchestrator of the whole thing."""

    def __init__(self):
        """Construct."""
        self.panel = Panel()
        self.performer_class = None
        self.args = None
        self.process = None

    def render(self):
        """Put the frames on the panel."""
        performer = self.performer_class(**self.args)
        for frame in performer.perform():
            self.panel.display(frame)

    def run(self, performer_class, **kwargs):
        """Control the process."""
        if performer_class == self.performer_class and kwargs == self.args:
            return  # nocov

        self.performer_class = performer_class
        self.args = kwargs
        self.stop()

        self.process = Process(target=self.render)
        self.process.start()

    def stop(self):
        """Stop the running process."""
        if self.process:
            self.process.terminate()


def _ref(self, resp, base_name=None):
        """
        Store a response schema and return a reference to it.

        :param schema:
            Swagger response definition.
        :param base_name:
            Name that should be used for the reference.

        :rtype: dict
        :returns: JSON pointer to the original response definition.
        """

        name = base_name or resp.get('title', '') or resp.get('name', '')

        pointer = self.json_pointer + name
        self.response_registry[name] = resp

        return {'$ref': pointer}


import sys
import heapq as hq
sys.stdin = open("input.txt", "rt")

N, M, K = map(int, input().split())

q = []
distances = [[float('inf')] * K for _ in range(N+1)]
graph = [[] for _ in range(N+1)]
distances[1][0] = 0 #시작점 기준 자기자신 0
hq.heappush(q, [0, 1]) #start = 1 = 시작도시

for _ in range(M):
    s, e, cost = map(int, input().split())
    graph[s].append([cost, e])

while q:
    w, v = hq.heappop(q)
    for ww, vv in graph[v]:

        distance = w + ww

        if distance < distances[vv][K-1]:
            distances[vv][K-1] = distance #그냥 이렇게 안하고 무조건 append한 다음에 sort가능
            distances[vv].sort() # 넣고 흔드는 과정이 중요
            hq.heappush(q,[distance, vv])

ans = [x[K-1] for x in distances][1:]
for i in ans:
    print(i if i != float('inf') else -1)


import Adafruit_DHT
import time
import RPi.GPIO as GPIO
channel_DHT11 = 4
channel_Hum = 18
GPIO.setmode(GPIO.BCM)
GPIO.setup(channel_Hum,GPIO.IN)

def medirHumedad(channel_Hum):

	if GPIO.input(channel_Hum):
		print("seco")
	else:
		print("humedo")

while True:
  sensor = Adafruit_DHT.DHT11

  humedad, temperatura = Adafruit_DHT.read_retry(sensor, channel_DHT11)
  print("\nTemperatura: {}*C   Humedad: {}% ".format(temperatura, humedad))
  medirHumedad(channel_Hum)
  time.sleep(1)


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

# In[57]:

import numpy as np
import pandas as pd
import plotly.offline as pyo
import plotly.graph_objs as go
import plotly.figure_factory as ff
import dash
import dash_html_components as html
import dash_core_components as dcc
import dash_bootstrap_components as dbc
from dash.dependencies import Output,Input,State

df=pd.read_csv("train.csv")

df["Date.of.Birth"]=pd.to_datetime(df["Date.of.Birth"],format="%d-%m-%Y")
df["DisbursalDate"]=pd.to_datetime(df["DisbursalDate"],format="%d-%m-%Y")
df["Age_at_time_of_disbursement"]=((df["DisbursalDate"]-df["Date.of.Birth"])/365).dt.days
df["AVERAGE.ACCT.AGE"]=df["AVERAGE.ACCT.AGE"].apply(lambda x:int(x.split()[0].split("y")[0])*12+int(x.split()[1].split("m")[0]))
df["CREDIT.HISTORY.LENGTH"]=df["CREDIT.HISTORY.LENGTH"].apply(lambda x:int(x.split()[0].split("y")[0])*12+int(x.split()[1].split("m")[0]))

categorical_cols=['branch_id','supplier_id', 'manufacturer_id', 'Current_pincode_ID','Employment.Type','State_ID',
                  'Employee_code_ID','MobileNo_Avl_Flag', 'Aadhar_flag', 'PAN_flag', 'VoterID_flag',
       'Driving_flag', 'Passport_flag',"PERFORM_CNS.SCORE.DESCRIPTION"]

grp0=df[df["loan_default"]==0]
grp1=df[df["loan_default"]==1]

cat_df=df["State_ID"].value_counts()[:15]
data=go.Bar(x=["State_id"+str(i) for i in cat_df.index],y=cat_df.values)
cat_fig=go.Figure(data)

app = dash.Dash(external_stylesheets=[dbc.themes.BOOTSTRAP])
server=app.server

navbar = dbc.NavbarSimple(
    children=[
        dbc.NavItem(dbc.NavLink("Continuous", href="https://capstone-continuous-app.herokuapp.com")),
        dbc.NavItem(dbc.NavLink("Categorical", href="https://capstone-categorical-app.herokuapp.com"))
    ],
    brand="Univariate_Analysis",
    color="primary",
    dark=True
)

controls_cat = dbc.Card(
    [
        dbc.FormGroup(
            [
                html.P(),
                dbc.Label("X-variable"),
                html.P(),
                dcc.Dropdown(
                    id="x-variable",
                    options=[
                        {"label": col, "value": col} for col in categorical_cols
                    ],
                    value="State_ID",
                ),
                html.P(),
                dcc.Dropdown(
                    id="target_id",
                    options=[
                        {"label": i, "value": i} for i in ["No","Yes"]
                    ],
                    value="No",
                ),
                html.P(),
                dbc.Button(id="Submit-Button",children="Submit", color="info", className="mr-1",n_clicks=0)
            ] )
    ],
    body=True,
)

tab2=dbc.Container(
    [
        html.Div(navbar),
        html.Hr(),
        dbc.Row(
            [
                dbc.Col(controls_cat, md=4),
                dbc.Col(dcc.Graph(id="Figures",figure=cat_fig), md=8),
            ],
            align="center",
        ),
    ],
    fluid=True,
)

app.layout=tab2

@app.callback(Output("Figures","figure"),
              [Input("Submit-Button","n_clicks")],
             [State("x-variable","value"),
             State("target_id","value")])

def update_fig(clicks,x_variable,target):
    if target=="No":
        cat_df=df[x_variable].value_counts()[:15]
        if x_variable!="PERFORM_CNS.SCORE.DESCRIPTION":
            data=go.Bar(x=[x_variable+str(i) for i in cat_df.index],y=cat_df.values)
            fig=go.Figure(data)
            return fig
        else:
            data=go.Bar(x=[i for i in cat_df.index],y=cat_df.values)
            fig=go.Figure(data)
            return fig
    else:
        cat_df_1=grp1[x_variable].value_counts()[:15]
        ind=cat_df_1.index
        cat_df_0=grp0[x_variable].value_counts().loc[ind]
        if x_variable!="PERFORM_CNS.SCORE.DESCRIPTION":
            trace0=go.Bar(x=[x_variable+str(i) for i in cat_df_0.index],y=cat_df_0.values,name="grp0")
            trace1=go.Bar(x=[x_variable+str(i) for i in cat_df_1.index],y=cat_df_1.values,name="grp1")
            cat_fig=go.Figure([trace0,trace1])
            return cat_fig
        else:
            trace0=go.Bar(x=[i for i in cat_df_0.index],y=cat_df_0.values,name="grp0")
            trace1=go.Bar(x=[i for i in cat_df_1.index],y=cat_df_1.values,name="grp1")
            cat_fig=go.Figure([trace0,trace1])
            return cat_fig

if __name__ == "__main__":
    app.run_server()

# In[ ]:


# coding:utf8
import torch

import torch.nn as nn
from torch.autograd import Variable

import numpy as np

def print_model_parm_nums(model):
    total = sum([param.nelement() for param in model.parameters()])
    print('  Number of params: %.2fM' % (total / 1e6))
    para = sum([np.prod(list(p.size())) for p in model.parameters()])
    print('  Model {} : size: {:4f}M'.format(model._get_name(), para * 4 / 1000 / 1000))

def print_model_parm_flops(model, input):
    # prods = {}
    # def save_prods(self, input, output):
    # print 'flops:{}'.format(self.__class__.__name__)
    # print 'input:{}'.format(input)
    # print '_dim:{}'.format(input[0].dim())
    # print 'input_shape:{}'.format(np.prod(input[0].shape))
    # grads.append(np.prod(input[0].shape))

    prods = {}

    def save_hook(name):
        def hook_per(self, input, output):
            # print 'flops:{}'.format(self.__class__.__name__)
            # print 'input:{}'.format(input)
            # print '_dim:{}'.format(input[0].dim())
            # print 'input_shape:{}'.format(np.prod(input[0].shape))
            # prods.append(np.prod(input[0].shape))
            prods[name] = np.prod(input[0].shape)
            # prods.append(np.prod(input[0].shape))

        return hook_per

    list_1 = []

    def simple_hook(self, input, output):
        list_1.append(np.prod(input[0].shape))

    list_2 = {}

    def simple_hook2(self, input, output):
        list_2['names'] = np.prod(input[0].shape)

    multiply_adds = False
    list_conv = []

    def conv_hook(self, input, output):
        batch_size, input_channels, input_height, input_width = input[0].size()
        output_channels, output_height, output_width = output[0].size()

        kernel_ops = self.kernel_size[0] * self.kernel_size[1] * (self.in_channels / self.groups) * (
            2 if multiply_adds else 1)
        bias_ops = 1 if self.bias is not None else 0

        params = output_channels * (kernel_ops + bias_ops)
        flops = batch_size * params * output_height * output_width

        list_conv.append(flops)

    list_linear = []

    def linear_hook(self, input, output):
        batch_size = input[0].size(0) if input[0].dim() == 2 else 1

        weight_ops = self.weight.nelement() * (2 if multiply_adds else 1)
        bias_ops = self.bias.nelement()

        flops = batch_size * (weight_ops + bias_ops)
        list_linear.append(flops)

    list_bn = []

    def bn_hook(self, input, output):
        list_bn.append(input[0].nelement())

    list_relu = []

    def relu_hook(self, input, output):
        list_relu.append(input[0].nelement())

    list_pooling = []

    def pooling_hook(self, input, output):
        batch_size, input_channels, input_height, input_width = input[0].size()
        output_channels, output_height, output_width = output[0].size()

        kernel_ops = self.kernel_size * self.kernel_size
        bias_ops = 0
        params = output_channels * (kernel_ops + bias_ops)
        flops = batch_size * params * output_height * output_width

        list_pooling.append(flops)

    def foo(net):
        childrens = list(net.children())
        if not childrens:
            if isinstance(net, torch.nn.Conv2d):
                # net.register_forward_hook(save_hook(net.__class__.__name__))
                # net.register_forward_hook(simple_hook)
                # net.register_forward_hook(simple_hook2)
                net.register_forward_hook(conv_hook)
            if isinstance(net, torch.nn.Linear):
                net.register_forward_hook(linear_hook)
            if isinstance(net, torch.nn.BatchNorm2d):
                net.register_forward_hook(bn_hook)
            if isinstance(net, torch.nn.ReLU):
                net.register_forward_hook(relu_hook)
            if isinstance(net, torch.nn.MaxPool2d) or isinstance(net, torch.nn.AvgPool2d):
                net.register_forward_hook(pooling_hook)
            return
        for c in childrens:
            foo(c)

    foo(model)
    out = model(input)

    total_flops = (sum(list_conv) + sum(list_linear) + sum(list_bn) + sum(list_relu) + sum(list_pooling))

    print('  Number of FLOPs: %.2fG' % (total_flops / 1e9))

    # print list_bn

    # print 'prods:{}'.format(prods)
    # print 'list_1:{}'.format(list_1)
    # print 'list_2:{}'.format(list_2)
    # print 'list_final:{}'.format(list_final)

def print_forward(model, input):
    select_layer = model.layer1[0].conv1

    grads = {}

    def save_grad(name):
        def hook(self, input, output):
            grads[name] = input

        return hook

    select_layer.register_forward_hook(save_grad('select_layer'))

    out = model(input)
    # print grads['select_layer']
    print(grads)

def print_value():
    grads = {}

    def save_grad(name):
        def hook(grad):
            grads[name] = grad

        return hook

    x = Variable(torch.randn(1, 1), requires_grad=True)
    y = 3 * x
    z = y ** 2

    # In here, save_grad('y') returns a hook (a function) that keeps 'y' as name
    y.register_hook(save_grad('y'))
    z.register_hook(save_grad('z'))
    z.backward()
    print('HW')
    print("grads['y']: {}".format(grads['y']))
    print(grads['z'])

def print_layers_num(model):
    # resnet = models.resnet18()
    def foo(net):
        childrens = list(net.children())
        if not childrens:
            # if isinstance(net, torch.nn.Conv2d):
            # 可以用来统计不同层的个数
            # net.register_backward_hook(print)
            return 1
        count = 0
        for c in childrens:
            count += foo(c)
        return count

    print(foo(model))

def check_summary(model):
    def torch_summarize(model, show_weights=True, show_parameters=True):
        """Summarizes torch model by showing trainable parameters and weights."""
        from torch.nn.modules.module import _addindent

        tmpstr = model.__class__.__name__ + ' (\n'
        for key, module in model._modules.items():
            # if it contains layers let call it recursively to get params and weights
            if type(module) in [
                torch.nn.modules.container.Container,
                torch.nn.modules.container.Sequential
            ]:
                modstr = torch_summarize(module)
            else:
                modstr = module.__repr__()
            modstr = _addindent(modstr, 2)

            params = sum([np.prod(p.size()) for p in module.parameters()])
            weights = tuple([tuple(p.size()) for p in module.parameters()])

            tmpstr += '  (' + key + '): ' + modstr
            if show_weights:
                tmpstr += ', weights={}'.format(weights)
            if show_parameters:
                tmpstr += ', parameters={}'.format(params)
            tmpstr += '\n'

        tmpstr = tmpstr + ')'
        return tmpstr

    # Test
    print(torch_summarize(model))

# https://gist.github.com/wassname/0fb8f95e4272e6bdd27bd7df386716b7
# summarize a torch model like in keras, showing parameters and output shape
def show_summary(model, input):
    from collections import OrderedDict
    import pandas as pd

    import torch
    from torch import nn

    def get_names_dict(model):
        """
        Recursive walk to get names including path
        """
        names = {}

        def _get_names(module, parent_name=''):
            for key, module in module.named_children():
                name = parent_name + '.' + key if parent_name else key
                names[name] = module
                if isinstance(module, torch.nn.Module):
                    _get_names(module, parent_name=name)

        _get_names(model)
        return names

    def torch_summarize_df(x, model, weights=False, input_shape=True, nb_trainable=False):
        """
        Summarizes torch model by showing trainable parameters and weights.

        author: wassname
        url: https://gist.github.com/wassname/0fb8f95e4272e6bdd27bd7df386716b7
        license: MIT

        Modified from:
        - https://github.com/pytorch/pytorch/issues/2001#issuecomment-313735757
        - https://gist.github.com/wassname/0fb8f95e4272e6bdd27bd7df386716b7/

        Usage:
            import torchvision.models as models
            model = models.alexnet()
            df = torch_summarize_df(input_size=(3, 224,224), model=model)
            print(df)

            #              name class_name        input_shape       output_shape  nb_params
            # 1     features=>0     Conv2d  (-1, 3, 224, 224)   (-1, 64, 55, 55)      23296#(3*11*11+1)*64
            # 2     features=>1       ReLU   (-1, 64, 55, 55)   (-1, 64, 55, 55)          0
            # ...
        """

        def register_hook(module):
            def hook(module, input, output):
                name = ''
                for key, item in names.items():
                    if item == module:
                        name = key
                # <class 'torch.nn.modules.conv.Conv2d'>
                class_name = str(module.__class__).split('.')[-1].split("'")[0]
                module_idx = len(summary)

                m_key = module_idx + 1

                summary[m_key] = OrderedDict()
                summary[m_key]['name'] = name
                summary[m_key]['class_name'] = class_name
                if input_shape:
                    summary[m_key][
                        'input_shape'] = (-1,) + tuple(input[0].size())[1:]
                summary[m_key]['output_shape'] = (-1,) + tuple(output.size())[1:]
                if weights:
                    summary[m_key]['weights'] = list(
                        [tuple(p.size()) for p in module.parameters()])

                #             summary[m_key]['trainable'] = any([p.requires_grad for p in module.parameters()])
                if nb_trainable:
                    params_trainable = sum(
                        [torch.LongTensor(list(p.size())).prod() for p in module.parameters() if p.requires_grad])
                    summary[m_key]['nb_trainable'] = params_trainable
                params = sum([torch.LongTensor(list(p.size())).prod() for p in module.parameters()])
                summary[m_key]['nb_params'] = params

            if not isinstance(module, nn.Sequential) and \
                    not isinstance(module, nn.ModuleList) and \
                    not (module == model):
                hooks.append(module.register_forward_hook(hook))

        # Names are stored in parent and path+name is unique not the name
        names = get_names_dict(model)

        if next(model.parameters()).is_cuda:
            x = x.cuda()

        # create properties
        summary = OrderedDict()
        hooks = []

        # register hook
        model.apply(register_hook)

        # make a forward pass
        model(x)

        # remove these hooks
        for h in hooks:
            h.remove()

        # make dataframe
        df_summary = pd.DataFrame.from_dict(summary, orient='index')

        return df_summary

    # Test on alexnet
    df = torch_summarize_df(input, model=model)
    print(df)

    # # Output
    #              name class_name        input_shape       output_shape  nb_params
    # 1     features=>0     Conv2d  (-1, 3, 224, 224)   (-1, 64, 55, 55)      23296#nn.Conv2d(3, 64, kernel_size=11, stride=4, padding=2),
    # 2     features=>1       ReLU   (-1, 64, 55, 55)   (-1, 64, 55, 55)          0
    # 3     features=>2  MaxPool2d   (-1, 64, 55, 55)   (-1, 64, 27, 27)          0
    # 4     features=>3     Conv2d   (-1, 64, 27, 27)  (-1, 192, 27, 27)     307392
    # 5     features=>4       ReLU  (-1, 192, 27, 27)  (-1, 192, 27, 27)          0
    # 6     features=>5  MaxPool2d  (-1, 192, 27, 27)  (-1, 192, 13, 13)          0
    # 7     features=>6     Conv2d  (-1, 192, 13, 13)  (-1, 384, 13, 13)     663936
    # 8     features=>7       ReLU  (-1, 384, 13, 13)  (-1, 384, 13, 13)          0
    # 9     features=>8     Conv2d  (-1, 384, 13, 13)  (-1, 256, 13, 13)     884992
    # 10    features=>9       ReLU  (-1, 256, 13, 13)  (-1, 256, 13, 13)          0
    # 11   features=>10     Conv2d  (-1, 256, 13, 13)  (-1, 256, 13, 13)     590080
    # 12   features=>11       ReLU  (-1, 256, 13, 13)  (-1, 256, 13, 13)          0
    # 13   features=>12  MaxPool2d  (-1, 256, 13, 13)    (-1, 256, 6, 6)          0
    # 14  classifier=>0    Dropout         (-1, 9216)         (-1, 9216)          0
    # 15  classifier=>1     Linear         (-1, 9216)         (-1, 4096)   37752832
    # 16  classifier=>2       ReLU         (-1, 4096)         (-1, 4096)          0
    # 17  classifier=>3    Dropout         (-1, 4096)         (-1, 4096)          0
    # 18  classifier=>4     Linear         (-1, 4096)         (-1, 4096)   16781312
    # 19  classifier=>5       ReLU         (-1, 4096)         (-1, 4096)          0
    # 20  classifier=>6     Linear         (-1, 4096)         (-1, 1000)    4097000

def show_save_tensor():
    from torchvision import utils
    import torchvision.models as models
    from matplotlib import pyplot as plt

    def vis_tensor(tensor, ch=0, all_kernels=False, nrow=8, padding=2):
        '''
        ch: channel for visualization
        allkernels: all kernels for visualization
        '''
        n, c, h, w = tensor.shape
        if all_kernels:
            tensor = tensor.view(n * c, -1, w, h)
        elif c != 3:
            tensor = tensor[:, ch, :, :].unsqueeze(dim=1)

        rows = np.min((tensor.shape[0] // nrow + 1, 64))
        grid = utils.make_grid(tensor, nrow=nrow, normalize=True, padding=padding)
        # plt.figure(figsize=(nrow,rows))
        plt.imshow(grid.numpy().transpose((1, 2, 0)))  # CHW HWC

    def save_tensor(tensor, filename, ch=0, all_kernels=False, nrow=8, padding=2):
        n, c, h, w = tensor.shape
        if all_kernels:
            tensor = tensor.view(n * c, -1, w, h)
        elif c != 3:
            tensor = tensor[:, ch, :, :].unsqueeze(dim=1)
        utils.save_image(tensor, filename, nrow=nrow, normalize=True, padding=padding)

    vgg = models.resnet18(pretrained=True)
    mm = vgg.double()
    filters = mm.modules
    body_model = [i for i in mm.children()][0]
    # layer1 = body_model[0]
    layer1 = body_model
    tensor = layer1.weight.data.clone()
    vis_tensor(tensor)
    save_tensor(tensor, 'test.png')

    plt.axis('off')
    plt.ioff()
    plt.show()

def print_autograd_graph(model, input):
    from graphviz import Digraph
    import torch
    from torch.autograd import Variable

    def make_dot(var, params=None):
        """ Produces Graphviz representation of PyTorch autograd graph

        Blue nodes are the Variables that require grad, orange are Tensors
        saved for backward in torch.autograd.Function

        Args:
            var: output Variable
            params: dict of (name, Variable) to add names to node that
                require grad (TODO: make optional)
        """
        if params is not None:
            # assert all(isinstance(p, Variable) for p in params.values())
            param_map = {id(v): k for k, v in params.items()}

        node_attr = dict(style='filled',
                         shape='box',
                         align='left',
                         fontsize='12',
                         ranksep='0.1',
                         height='0.2')
        dot = Digraph(node_attr=node_attr, graph_attr=dict(size="12,12"))
        seen = set()

        def size_to_str(size):
            return '(' + (', ').join(['%d' % v for v in size]) + ')'

        def add_nodes(var):
            if var not in seen:
                if torch.is_tensor(var):
                    dot.node(str(id(var)), size_to_str(var.size()), fillcolor='orange')
                elif hasattr(var, 'variable'):
                    u = var.variable
                    # name = param_map[id(u)] if params is not None else ''
                    # node_name = '%s\n %s' % (name, size_to_str(u.size()))
                    node_name = '%s\n %s' % (param_map.get(id(u.data)), size_to_str(u.size()))
                    dot.node(str(id(var)), node_name, fillcolor='lightblue')

                else:
                    dot.node(str(id(var)), str(type(var).__name__))
                seen.add(var)
                if hasattr(var, 'next_functions'):
                    for u in var.next_functions:
                        if u[0] is not None:
                            dot.edge(str(id(u[0])), str(id(var)))
                            add_nodes(u[0])
                if hasattr(var, 'saved_tensors'):
                    for t in var.saved_tensors:
                        dot.edge(str(id(t)), str(id(var)))
                        add_nodes(t)

        add_nodes(var.grad_fn)
        return dot

    y = model(Variable(input))
    # print(y)

    g = make_dot(y, params=model.state_dict())
    g.view()
    # g

def modelsize(model, input, type_size=4):
    para = sum([np.prod(list(p.size())) for p in model.parameters()])
    print('Model {} : params: {:4f}M'.format(model._get_name(), para * type_size / 1000 / 1000))

    input_ = input.clone()
    input_.requires_grad_(requires_grad=False)

    mods = list(model.modules())
    out_sizes = []

    for i in range(1, len(mods)):
        m = mods[i]
        if isinstance(m, nn.ReLU):
            if m.inplace:
                continue
        out = m(input_)
        out_sizes.append(np.array(out.size()))
        input_ = out

    total_nums = 0
    for i in range(len(out_sizes)):
        s = out_sizes[i]
        nums = np.prod(np.array(s))
        total_nums += nums

    print('Model {} : intermedite variables: {:3f} M (without backward)'
          .format(model._get_name(), total_nums * type_size / 1000 / 1000))
    print('Model {} : intermedite variables: {:3f} M (with backward)'
          .format(model._get_name(), total_nums * type_size * 2 / 1000 / 1000))

if __name__ == '__main__':
    from models.doc_unet import DeepLabv3_plus_zj

    # from model import Doc_UNet
    torch.manual_seed(1)
    input = torch.randn(1, 3, 592, 800)
    # model = Xception(input_channels=3)
    print('DeepLabv3_plus_zj')
    model = DeepLabv3_plus_zj(input_channels=3, n_classes=2, features='xception')
    print_model_parm_nums(model)
    print_model_parm_flops(model, input)


# !/usr/bin/python
# -*- coding: utf-8 -*-
# @time    : 2021/2/23 21:34
# @author  : Mo
# @function: choice, model_config可配置参数

# 适配linux
import platform
import json
import sys
import os
path_root = os.path.abspath(os.path.join(os.path.dirname(__file__), "../.."))
path_sys = os.path.join(path_root, "pytorch_nlu", "pytorch_textclassification")
sys.path.append(path_sys)
print(path_root)
print(path_sys)
# 分类下的引入, pytorch_textclassification
from tcTools import get_current_time
from tcRun import TextClassification
from tcConfig import model_config

# 预训练模型地址, 本地win10默认只跑2步就评估保存模型
if platform.system().lower() == 'windows':
    # pretrained_model_dir = "D:/pretrain_models/pytorch"
    pretrained_model_dir = "E:/DATA/bert-model/00_pytorch"
    evaluate_steps = 32  # 评估步数
    save_steps = 32  # 存储步数
else:
    pretrained_model_dir = "/pretrain_models/pytorch"
    evaluate_steps = 320  # 评估步数
    save_steps = 320  # 存储步数
    ee = 0

if __name__ == "__main__":
    # 训练-验证语料地址, 可以只输入训练地址
    path_corpus = os.path.join(path_root, "pytorch_nlu", "corpus", "text_classification", "school")
    path_train = os.path.join(path_corpus, "train.json")
    path_dev = os.path.join(path_corpus, "dev.json")
    model_config["evaluate_steps"] = evaluate_steps  # 评估步数
    model_config["save_steps"] = save_steps  # 存储步数
    model_config["path_train"] = path_train  # 训练模语料, 必须
    model_config["path_dev"] = path_dev      # 验证语料, 可为None
    model_config["path_tet"] = None          # 测试语料, 可为None

    ## 参数配置 choice
    model_config["lr"] = 3e-5  # 5e-5, 1e-5 # 学习率
    model_config["max_len"] = 128  # 最大文本长度, padding
    model_config["batch_size"] = 64  # 批尺寸
    model_config["warmup_steps"] = 1000  # 预热步数
    model_config["is_active"] = False  # fc是否加激活函数
    model_config["is_dropout"] = True  # 是否随机丢弃
    model_config["is_adv"] = True  # 是否对抗训练
    # model_config["len_rate"] = 0.01   # 参与训练数据的样本数比率(如win10下少量数据跑通)
    model_config["epochs"] = 16  # 训练轮次 # 21  # 32

    model_config["output_hidden_states"] = [0, 1, 2, 3]  # 输出多层 # [0, 1, 2, 3]  # [1, 2, 11, 12]  # [8, 9, 10, 11, 12]  # [0,1,  5,6,  11,12]  # [1, 3, 5]
    model_config["loss_type"] = "PRIOR_MARGIN_LOSS"  # 损失函数
    # model_config["loss_type"] = "FOCAL_LOSS"
    # model_config["loss_type"] = "PRIOR_MARGIN_LOSS"
    # model_config["loss_type"] = "MIX_focal_prior"
    # model_config["loss_type"] = "MIX_prior_bce"
    # model_config["loss_type"] = "MIX_focal_bce"
    # model_config["loss_type"] = "BCE_MULTI"
    # model_config["loss_type"] = "BCE_LOGITS"
    # model_config["loss_type"] = "CIRCLE_LOSS"
    # model_config["loss_type"] = "CB_LOSS"
    # model_config["loss_type"] = "DB_LOSS"
    model_config["label_sep"] = "|myz|"  # 多标签分类类别标签分隔符

    # 预训练模型适配的class
    model_type = ["BERT", "ERNIE", "BERT_WWM", "ALBERT", "ROBERTA", "XLNET", "ELECTRA"]
    pretrained_model_name_or_path = {
        "BERT_WWM":  "hfl/chinese-bert-wwm-ext",
        "ROBERTA":  "hfl/chinese-roberta-wwm-ext",
        "ALBERT":  "uer/albert-base-chinese-cluecorpussmall",
        "XLNET":  "hfl/chinese-xlnet-mid",
        "ERNIE":  "nghuyong/ernie-1.0-base-zh",
        # "ERNIE": "nghuyong/ernie-3.0-base-zh",
        "BERT":  "bert-base-chinese",
        # "BERT": "hfl/chinese-macbert-base",

    }
    idx = 0  # 选择的预训练模型类型---model_type, 0为BERT,
    model_config["pretrained_model_name_or_path"] = pretrained_model_name_or_path[model_type[idx]]
    # model_config["model_save_path"] = "../output/text_classification/model_{}".format(model_type[idx] + "_" + str(get_current_time()))
    model_config["model_save_path"] = "../output/text_classification/model_{}".format(model_type[idx])
    model_config["model_type"] = model_type[idx]
    model_config["ADDITIONAL_SPECIAL_TOKENS"] = ["＋","－", "＝", "：", "．", "（", "）", "≈", "％",
                                                 "∥", "＜", "＞", "⊙", "≌", "。"]  # 新增特殊字符
    # main
    lc = TextClassification(model_config)
    lc.process()
    lc.train()

# shell
# nohup python  tet_tc_base_multi_label_choice.py > tc.tet_tc_base_multi_label_choice.py.log 2>&1 &
# tail -n 1000  -f tc.tet_tc_base_multi_label_choice.py.log
# |myz|


# Creates masks for cameras in the active chunk, based on user defined color and tolerance.
#
# This is python script for Metashape Pro. Scripts repository: https://github.com/agisoft-llc/metashape-scripts

import Metashape
from PySide2 import QtGui, QtCore, QtWidgets

# Checking compatibility
compatible_major_version = "1.6"
found_major_version = ".".join(Metashape.app.version.split('.')[:2])
if found_major_version != compatible_major_version:
    raise Exception("Incompatible Metashape version: {} != {}".format(found_major_version, compatible_major_version))

class MaskByColor(QtWidgets.QDialog):

    def __init__(self, parent):
        QtWidgets.QDialog.__init__(self, parent)

        self.color = QtGui.QColor(0, 0, 0)
        red, green, blue = self.color.red(), self.color.green(), self.color.blue()

        self.setWindowTitle("Masking by color:")

        self.btnQuit = QtWidgets.QPushButton("Quit")
        self.btnQuit.setFixedSize(100, 50)

        self.btnP1 = QtWidgets.QPushButton("Mask")
        self.btnP1.setFixedSize(100, 50)

        self.pBar = QtWidgets.QProgressBar()
        self.pBar.setTextVisible(False)
        self.pBar.setFixedSize(130, 50)

        self.selTxt = QtWidgets.QLabel()
        self.selTxt.setText("Apply to:")
        self.selTxt.setFixedSize(100, 25)

        self.radioBtn_all = QtWidgets.QRadioButton("all cameras")
        self.radioBtn_sel = QtWidgets.QRadioButton("selected cameras")
        self.radioBtn_all.setChecked(True)
        self.radioBtn_sel.setChecked(False)

        self.colTxt = QtWidgets.QLabel()
        self.colTxt.setText("Select color:")
        self.colTxt.setFixedSize(100, 25)

        strColor = "{:0>2d}{:0>2d}{:0>2d}".format(int(hex(red)[2:]), int(hex(green)[2:]), int(hex(blue)[2:]))
        self.btnCol = QtWidgets.QPushButton(strColor)
        self.btnCol.setFixedSize(80, 25)
        pix = QtGui.QPixmap(10, 10)
        pix.fill(self.color)
        icon = QtGui.QIcon()
        icon.addPixmap(pix)
        self.btnCol.setIcon(icon)
        palette = QtGui.QPalette()
        palette.setColor(QtGui.QPalette.Button, self.color)
        self.btnCol.setPalette(palette)
        self.btnCol.setAutoFillBackground(True)

        self.txtTol = QtWidgets.QLabel()
        self.txtTol.setText("Tolerance:")
        self.txtTol.setFixedSize(100, 25)

        self.sldTol = QtWidgets.QSlider()
        self.sldTol.setOrientation(QtCore.Qt.Orientation.Horizontal)
        self.sldTol.setMinimum(0)
        self.sldTol.setMaximum(99)

        hbox = QtWidgets.QHBoxLayout()
        hbox.addStretch(1)
        hbox.addWidget(self.pBar)
        hbox.addWidget(self.btnP1)
        hbox.addWidget(self.btnQuit)

        layout = QtWidgets.QGridLayout()
        layout.setSpacing(5)
        layout.addWidget(self.selTxt, 0, 0)
        layout.addWidget(self.radioBtn_all, 1, 0)
        layout.addWidget(self.radioBtn_sel, 2, 0)
        layout.addWidget(self.colTxt, 0, 1)
        layout.addWidget(self.btnCol, 1, 1)
        layout.addWidget(self.txtTol, 0, 2)
        layout.addWidget(self.sldTol, 1, 2)
        layout.addLayout(hbox, 3, 0, 5, 3)
        self.setLayout(layout)

        proc_mask = lambda: self.maskColor()
        proc_color = lambda: self.changeColor()

        QtCore.QObject.connect(self.btnP1, QtCore.SIGNAL("clicked()"), proc_mask)
        QtCore.QObject.connect(self.btnCol, QtCore.SIGNAL("clicked()"), proc_color)
        QtCore.QObject.connect(self.btnQuit, QtCore.SIGNAL("clicked()"), self, QtCore.SLOT("reject()"))

        self.exec()

    def changeColor(self):

        color = QtWidgets.QColorDialog.getColor()

        self.color = color
        red, green, blue = color.red(), color.green(), color.blue()
        if red < 16:
            red = "0" + hex(red)[2:]
        else:
            red = hex(red)[2:]
        if green < 16:
            green = "0" + hex(green)[2:]
        else:
            green = hex(green)[2:]
        if blue < 16:
            blue = "0" + hex(blue)[2:]
        else:
            blue = hex(blue)[2:]

        strColor = red + green + blue
        self.btnCol.setText(strColor)

        pix = QtGui.QPixmap(10, 10)
        pix.fill(self.color)
        icon = QtGui.QIcon()
        icon.addPixmap(pix)
        self.btnCol.setIcon(icon)

        palette = self.btnCol.palette()
        palette.setColor(QtGui.QPalette.Button, self.color)
        self.btnCol.setPalette(palette)
        self.btnCol.setAutoFillBackground(True)

        return True

    def maskColor(self):
        print("Masking...")

        tolerance = 10
        tolerance = self.sldTol.value()

        self.sldTol.setDisabled(True)
        self.btnCol.setDisabled(True)
        self.btnP1.setDisabled(True)
        self.btnQuit.setDisabled(True)

        chunk = Metashape.app.document.chunk
        mask_list = list()
        if self.radioBtn_sel.isChecked():
            for camera in chunk.cameras:
                if camera.selected and camera.type == Metashape.Camera.Type.Regular:
                    mask_list.append(camera)
        elif self.radioBtn_all.isChecked():
            mask_list = [camera for camera in chunk.cameras if camera.type == Metashape.Camera.Type.Regular]

        if not len(mask_list):
            Metashape.app.messageBox("Nothing to mask!")
            return False

        color = self.color
        red, green, blue = color.red(), color.green(), color.blue()

        processed = 0
        for camera in mask_list:

            for frame in camera.frames:
                print(frame)
                app.processEvents()
                mask = Metashape.utils.createDifferenceMask(frame.photo.image(), (red, green, blue), tolerance, False)
                m = Metashape.Mask()
                m.setImage(mask)
                frame.mask = m
                processed += 1
                self.pBar.setValue(int(processed / len(mask_list) / len(chunk.frames) * 100))

        print("Masking finished. " + str(processed) + " images masked.")

        self.sldTol.setDisabled(False)
        self.btnCol.setDisabled(False)
        self.btnP1.setDisabled(False)
        self.btnQuit.setDisabled(False)

        return True

def mask_by_color():
    global doc
    doc = Metashape.app.document

    global app
    app = QtWidgets.QApplication.instance()
    parent = app.activeWindow()

    dlg = MaskByColor(parent)

label = "Custom menu/Masking by color"
Metashape.app.addMenuItem(label, mask_by_color)
print("To execute this script press {}".format(label))


import os
import time
import argparse
import pandas as pd

from dataloader import LoadDocumentData, LoadImageData, LoadTabularData, LoadMatData

class parameter(object):
    def __init__(self):
        parser = argparse.ArgumentParser()
        # data parameter
        parser.add_argument('--data_format', type=str, default='csv',
                            help='Dataset format')
        parser.add_argument('--data_name', type=str, default='market',
                            help='Dataset name')
        parser.add_argument('--data_path', type=str, default='./data/',
                            help='Data path')
        parser.add_argument('--inject_noise', type=bool, default=False,
                            help='Whether to inject noise to train data')
        parser.add_argument('--cont_rate', type=float, default=0,
                            help='Inject noise to contamination rate')
        parser.add_argument('--anomal_rate', type=str, default='default',
                            help='Adjust anomaly rate')
        parser.add_argument('--seed', type=int, default=42,
                            help="Random seed.")
        parser.add_argument('--verbose', action='store_false', default=True,
                            help='Whether to print training details')

        if __name__ == '__main__':
            args = parser.parse_args()
        else:
            args = parser.parse_args([])

        self.__dict__.update(args.__dict__)

def preprocess(args=None):

    if args == None:
        args = parameter()
    # tabular

    if args.data_format == "mat":
        x_train, y_train, x_val, y_val, x_test, y_test = LoadMatData(args)

    elif args.data_name in ['attack', 'bcsc', 'creditcard', 'diabetic', 'donor', 'intrusion', 'market', 'thyroid']:
        x_train, y_train, x_val, y_val, x_test, y_test = LoadTabularData(args)
    # document
    elif args.data_name in ['20news', 'reuters']:
        dataloader = LoadDocumentData(args)
        # 需要指定anomal_rate
        if args.anomal_rate == 'default':
            args.anomal_rate = 0.05

        # 每一个类作为异常遍历
        for normal_idx in range(dataloader.class_num):
            x_train, x_test, y_train, y_test = dataloader.preprocess(
                normal_idx)
            x_val = x_train.copy()
            y_val = y_train.copy()

    # image
    elif args.data_name in ['mnist']:
        x_train, x_test, y_train, y_test = LoadImageData(args)
        x_val = x_train.copy()
        y_val = y_train.copy()

    return x_train, y_train, x_val, y_val, x_test, y_test


#AUTHOR: Petr Geršl (pgersl)
#NAME: note-cleaner
#CREDIT: vcerny01/rmdc

#imports

import re
import os
import argparse
import time

#global vars

link_patern = re.compile("\[\[(.*?)\]\]")
italic_pattern = re.compile("__(.*?)__")
bold_italic_pattern = re.compile("\*\*_(.*?)_\*\*")
daily_notes_names = "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"

#defs

def parse_args():
    """Define arguments"""
    parser = argparse.ArgumentParser(description="Edit Roam syntax and remove unwanted files.")
    parser.add_argument("-E", "--edit", help="Edit files", action="store_true")
    parser.add_argument("-R", "--remove", help="Remove personal files", action="store_true")
    parser.add_argument("-D", "--daily-notes", help="Remove daily notes", action="store_true")
    parser.add_argument("-I", "--indir", type=str, help="Input directory", default=".")
    parser.add_argument("-f", "--filename", type=str, help="Targeted file for removal (required when removing files)")
    parser.add_argument("-d", "--depth", type=int, help="Link depth")
    parser.add_argument("-o", "--other-files", help="Other files to remove", nargs="+", default=[])
    parser.add_argument("-e", "--exclude-removal", help="Files to exclude from removal", nargs="+", default=[])
    parser.add_argument("-x", "--exclude-editing", help="Files to exclude from editing", nargs="+", default=["note-cleaner.py"])
    return parser.parse_args()

def search_links(filename):
    """Search for links in file"""
    try:
        file = open(filename)
    except FileNotFoundError:
        print(f"File '{filename}' doesn't exist! \n Skipping...")
        return
    linked_files = re.findall(link_patern, file.read())
    return [x + ".md" for x in linked_files]

def scan_dir(directory):
    """Create a list of files in directory"""
    try:
        os.listdir(directory)
    except FileNotFoundError:
        print(f"Directory '{directory}' doesn't exist!")
        return
    return os.listdir(directory)

def edit_syntax(content: str):
    """Edit syntax"""
    italics = re.findall(italic_pattern, content)
    for italic in italics:
        full_italic = "__" + italic + "__"
        content = content.replace(full_italic, "_" + italic + "_")
    bold_italics = re.findall(bold_italic_pattern, content)
    for bold_italic in bold_italics:
        full_bold_italic = "**_" + bold_italic + "_**"
        content = content.replace(full_bold_italic, "**" + bold_italic + "**")
    return content

def remove_files(files):
    """Remove selected files"""
    for item in files:
        try:
            os.remove(item)
        except FileNotFoundError:
            print(f"File '{item}' doesn't exist or was already deleted. Skipping...")
            continue
    print(f"{str(len(files))} files succesfully removed")

def editing(exclude, input_dir):
    """Editing"""
    print("Following files will be excluded from editing: ")
    for item in exclude:
        print(f"- {item}")
    print("\n Editing files...")
    edit_files = scan_dir(input_dir)
    edit_files = [x for x in edit_files if x not in exclude]
    for filename in edit_files:
        full_path = os.path.join(input_dir, filename)
        if os.path.isdir(full_path):
            edit_files.remove(filename)
        else:
            with open(full_path, "r+") as file:
                content = edit_syntax(file.read())
                file.seek(0)
                file.write(content)
    print(f"\n {str(len(edit_files))} successfully edited.")
    return

def removing(exclude, filename, input_dir, depth, other):
    """Removing"""
    print("Following files will be excluded from deletion: ")
    for item in exclude:
        print(f"- {item}")
    print("\n Removing files...")
    print(f"\n Starting with file '{filename}'")
    final_files = [os.path.join(input_dir, filename)]
    target_files = final_files
    for i in range(depth):
        next_target_files = ["",]
        for item in target_files:
            new_targets = search_links(item)
            if new_targets is None:
                final_files.remove(item)
                continue
            new_targets = [x for x in new_targets if x not in exclude]
            next_target_files = list(set(next_target_files + new_targets))
        next_target_files = [os.path.join(input_dir, x) for x in next_target_files if x]
        next_target_files = [x for x in next_target_files if x not in final_files]
        target_files = next_target_files
        final_files = list(set(final_files + target_files + other))
    remove_files(final_files)
    return

def daily_notes(input_dir):
    """Removing daily notes"""
    print("\n Removing daily notes...")
    files = scan_dir(input_dir)
    daily_notes = []
    for name in files:
        if name.startswith(daily_notes_names):
            daily_notes.append(name)
    remove_files(daily_notes)
    return

#main

def main():
    args = parse_args()

    if args.edit:
        editing(args.exclude_editing, args.indir)
        time.sleep(5)
    if args.remove:
        removing(args.exclude_removal, args.filename, args.indir, args.depth, args.other_files)
        time.sleep(5)
    if args.daily_notes:
        daily_notes(args.indir)

    print("FINISHED!")

if __name__ == "__main__":
    main()

#end


import aiotools
import asyncio
import faust
from neo4j import GraphDatabase
from models import Question
from utils import read_env
#from aiokafkadaemon import worker, daemon

app = faust.App('ship-app', broker=read_env('KAFKA_LISTENER'))
questions_topic = app.topic('ship-topic', value_type=Question)

@app.agent(questions_topic)
async def answer(questions):
    print("TOTAL QUESTION "+len(questions))
    async for question in questions:
        print(f"HI THERE SEE ===========> {question.slack_id} {question.text}")

def create_new(tx, name):
    tx.run("CREATE (p:Person {name:$name}) RETURN p", name=name)

def add_liked(tx, name, liked_name):
    tx.run("MERGE (a:Person {name: $name}) "
           "MERGE (a)-[:LIKES]->(liked:Person {name: $liked_name})",

           name=name, liked_name=liked_name)

def add_person(name):
    try:
        uri = "bolt://neo4j:7687"
        driver = GraphDatabase.driver(uri, auth=("neo4j", "graph"))

        with driver.session() as session:
            session.write_transaction(create_new, name)
    except Exception as e:
        print(f"Failed connecting to bolt - {e}")

def add_liked_person(name, liked):
    try:
        uri = "bolt://neo4j:7687"
        driver = GraphDatabase.driver(uri, auth=("neo4j", "graph"))

        with driver.session() as session:
            session.write_transaction(create_new, name)
            session.write_transaction(add_liked, name, liked)
    except Exception as e:
        print(f"Failed connecting to bolt - {e}")

@app.agent(questions_topic)
async def read_stream(loop):
    print("======> LET ME TRY AND CHECK IT ")

async def echo(reader, writer):
   data = await reader.read(100)
   writer.write(data)
   await writer.drain()
   writer.close()

@aiotools.server
async def worker(loop, pidx, args):
   app.main()
   server = await asyncio.start_server(answer, '0.0.0.0', 8888, reuse_port=True)
   loop.run_forever(read_stream(loop))
   print(f'[{pidx}] started')
   yield  # wait until terminated
   server.close()
   await server.wait_closed()
   print(f'[{pidx}] terminated')

def main():
    try:
        uri = "bolt://neo4j:7687"
        driver = GraphDatabase.driver(uri, auth=("neo4j", "graph"))

        with driver.session() as session:
            session.write_transaction(create_new, "Alice")

        with driver.session() as session:
            session.write_transaction(create_new, "Paul")

        with driver.session() as session:
            session.write_transaction(create_new, "Rob")

        with driver.session() as session:
            session.write_transaction(create_new, "Dmitry")

        with driver.session() as session:
            session.write_transaction(add_liked, "Alice", "Neo4j")
        with driver.session() as session:
            session.write_transaction(add_liked, "Rob", "Neo4j")
        driver.close()
    except Exception as e:
        print(f"Failed connecting to bolt - {e}")

def execute():
    print("Started...")

async def echo(reader, writer):
   data = await reader.read(100)
   writer.write(data)
   await writer.drain()
   writer.close()

if __name__ == '__main__':
   # Run the above server using 4 worker processes.
   aiotools.start_server(worker, num_workers=4)


import torch
import torchvision
import torchvision.transforms as transforms

# The output of torchvision datasets are PILImage images of range [0, 1]. We transform them to Tensors of
# normalized range [-1, 1].
transform = transforms.Compose(
    [transforms.ToTensor(),
     transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])

batch_size = 4

trainset = torchvision.datasets.CIFAR10(root='/Users/ll/brain/pytorch/datasets', train=True,
                                        download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=batch_size, shuffle=True, num_workers=2)

testset = torchvision.datasets.CIFAR10(root='/Users/ll/brain/pytorch/datasets', train=False,
                                       download=True, transform=transform)
# 错误：Warning: Cannot set number of intraop threads after parallel work has started or after set_num_threads call
# when using native parallel backend (function set_num_threads)
# 解决：num_workers=2 => num_workers=0
testloader = torch.utils.data.DataLoader(testset, batch_size=batch_size, shuffle=False, num_workers=0)

classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck')


from pyhanlp import *
import sys
sys.path.append(r"C:\Users\gawei\.PyCharmCE2018.3\config\book_pyhanlp")
from ch07 import pku
from ch08.demo_hmm_ner import test

NERTrainer = JClass('com.hankcs.hanlp.model.perceptron.NERTrainer')
CRFNERecognizer = JClass('com.hankcs.hanlp.model.crf.CRFNERecognizer')

def train(corpus, model):
    recognizer = CRFNERecognizer(None)  # 空白
    recognizer.train(corpus, model)
    recognizer = CRFNERecognizer(model)
    return recognizer

if __name__ == '__main__':
    recognizer = train(pku.PKU199801_TRAIN, pku.NER_MODEL)
    test(recognizer)


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

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

class Migration(migrations.Migration):

    dependencies = [
        ('tablemanager', '0026_auto_20160229_0844'),
    ]

    operations = [
        migrations.AddField(
            model_name='datasource',
            name='vrt',
            field=tablemanager.models.XMLField(default='', help_text='GDAL VRT template in xml'),
            preserve_default=True,
        ),
        migrations.AlterField(
            model_name='foreigntable',
            name='server',
            field=models.ForeignKey(to='tablemanager.DataSource'),
            preserve_default=True,
        ),
        migrations.DeleteModel(
            name='ForeignServer',
        ),
        migrations.AlterField(
            model_name='input',
            name='source',
            field=tablemanager.models.DatasourceField(default='<OGRVRTDataSource>\n    <OGRVRTLayer name="{{self.foreign_table.name}}">\n        <SrcDataSource>PG:dbname={{db.NAME}} host=\'{{db.HOST}}\' port=\'{{db.PORT}}\' user=\'{{db.USER}}\' password=\'{{db.PASSWORD}}\'</SrcDataSource>\n    </OGRVRTLayer>\n</OGRVRTDataSource>\n', help_text='GDAL VRT definition in xml', unique=True),
            preserve_default=True,
        ),
    ]


import time
import random
####just copy and paste the below given code to your shell

# KERAS

import numpy as np
import os
import cv2
from sklearn.utils import shuffle
from sklearn.model_selection import train_test_split
from Definitions import *
import gc
from imutils import paths
from keras.utils import to_categorical

def get_data(path_to_images, remove_ratio):
    listing = os.listdir(path_to_images)

    imlist = os.listdir(path_to_images)
    farrowinglist = []
    nofarrowinglist = []
    for f in imlist:
        if not f.endswith(".jpg") and not f.endswith(".txt") and not f.endswith(".dat") and not f.endswith(".py"):
            f2 = os.listdir(path_to_images+f)
            for f3 in f2:
                if not f3.endswith(".jpg") and not f3.endswith(".txt") and not f3.endswith(".dat") and not f3.endswith(".py"):
               #     print(str(os.listdir(path_to_images+f+"\\"+f3)))
                   # print(path_to_images+f+"\\"+f3)
                    if f3 == "farrow_full":
                        farrowings = os.listdir(path_to_images+f+"\\"+f3)
                        tofarrowinglist = [path_to_images+f+"\\"+f3+"\\"+farrow for farrow in farrowings]
                        farrowinglist.extend(tofarrowinglist)
                    elif f3 == "nofarrow_full":
                        nofarrowings = os.listdir(path_to_images+f+"\\"+f3)
                        tonofarrowinglist = [path_to_images+f+"\\"+f3+"\\"+nofarrow for nofarrow in nofarrowings]
                        nofarrowinglist.extend(tonofarrowinglist)

    imlist = []
    #The line of code below reduces the number of no farrowings to match the number of farrowings
    nofarrowinglist = random.choices(nofarrowinglist, k=int(len(nofarrowinglist) / remove_ratio))

    imlist.extend(nofarrowinglist)
    imlist.extend(farrowinglist)
    num_samples = len(imlist)
    nb_class0 = len(nofarrowinglist)
    nb_class1 = len(farrowinglist)
    del farrowinglist, nofarrowinglist
    print("Number of farrowings: " + str(nb_class1) + "\n Number of no farrowings: " + str(nb_class0))
  #  print(imlist)
    im1 = cv2.imread(imlist[0], cv2.IMREAD_COLOR)
    img_rows = np.size(im1, 0)
    img_cols = np.size(im1, 1)  #  get the size of the images

    # create matrix to store all flattened images

    immatrix = np.array([np.array(cv2.imread(im2, cv2.IMREAD_COLOR)).flatten()
                      for im2 in imlist], 'f')

    del imlist, farrowings, nofarrowings, im1
    # immatrix = []
   # arr = []
   # for im2 in imlist:
   #     arr.append(np.array(cv2.imread(im2, cv2.IMREAD_COLOR)).flatten())
   # import sys
   # print(sys.getsizeof(arr))
   # immatrixtemp = np.array(arr[:int(len(arr)/2)], "f")
   # immatrixtemp2 = np.array(arr[int(len(arr)/2):], "f")
   # immatrix.extend(immatrixtemp)
   # immatrix.extend(immatrixtemp2)
    label = np.ones((num_samples,), dtype=int)
    label[0:(nb_class0-1)] = 0
    label[nb_class0:(nb_class0+nb_class1-1)] = 1
    data, Label = shuffle(immatrix, label, random_state=2)
    del immatrix
    train_data = [data, Label]
    del data, Label
    # STEP 1: split X and y into training and testing sets
    x_train, x_test, y_train, y_test = train_test_split(train_data[0], train_data[1], test_size=0.25, random_state=4)
    #x_train, x_val, y_train, y_val = train_test_split(train_data[0], train_data[1], test_size=0.25, random_state=4)
    del train_data
    # Return the sets to their original shapes
    x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, num_channels)
    x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, num_channels)

    # Scale the values from 0 to 1 instead of 0 to 255
    gc.collect()
    x_train2 = x_train.astype('float32')
    x_test2 = x_test.astype('float32')
    x_train2 /= 255
    x_test2 /= 255

    nofarrowingclass = []
    farrowingclass = []

    return (x_train2, y_train), (x_test2, y_test)


# Hello Guys
# Today we're going to learn how to handle exceptions.
# So exception as an occurence of an undesirable situation that can be detected during program execution. Example of opening file but that file does not exist is an exception.
# until now we did not implemented any code to handle exceptions. If exceptions occurred during program execution, the program is terminated with appropriate error message.
# The general syntax of try/except/finally block
# try:
#    # statements
# except ExceptionClassName:
#    # exception handler code
# finally:
#    # statements
#
# Statements that might generate an exceptions are placed in try block. The try block might also contain statements that should not be executed if an exception occurs.
# The try block is followed by one or more except and contains an exception handler. any codes contained in finally block always executed, regardless of whether the exception occurs.
#
# Exception-handling techniques
#  1. Terminate the Program - Sometimes the best decision is to let the program terminate when an exception occurs.
# Suppose you wrote a program that inputs data from file. If the input file does not exist when the program
# executes, then there is no point of continuing with the program. Error message outputs and terminate.
#  2. Fix the Error and Continue - Other case, you want to handle exception and let the program continue.
# Suppose, you have program takes an input integer. If a user inputs a character in place of a digit, the program will throw an
# ValueError. You prompt the user to input an integer again until entry is valid.
#  3. Log the Error and Continue - A program terminates when the an exception occurs usually assumes that a termination is reasonably safe.
# When a long-running server suddenly occur an exception, it cannot be terminated.

"""
Write a program that prompts the user to enter the length in feet and inches and outputs
the equivalent length in inches and in centimeters. If the user enters a negative number or a nondigit number,
throw and handle an appropriate exception and prompt the user to enter another set of numbers.
"""

import traceback # we imported this module to implement stacktrace to identify the error happened

completed = False
while not(completed):
    try:
        length_in_feet = int(input("Enter a measured number in Feet: "))
        length_in_inches = int(input("Enter a measured number in Inches: "))
        assert length_in_feet > 0 # we use assert keyword to check if entered value is valid entry.
        converted_in_inch = length_in_feet * 12
        converted_in_cm = length_in_inches * 2.54
        print(f'The entered measured number of length in feet: {length_in_feet}')
        print(f'The entered measured number of length in inches: {length_in_inches}')
        print(f'The converted length in feet to inches: {converted_in_inch}')
        print(f'The converted length in inches to centimeters: {converted_in_cm}')
        completed = True
    except AssertionError: # AssertionError, Description: Raised when an assert statement fails.
        # This exception is called when Line 39 fail to match
        print("The measured number is negative")
    except ValueError: # ValueError, Description: Raised when an operation or function receives an argument that
        # has the right type but an inappropriate value, and the situation is not described by a more precise
        # exception such as IndexError.
        # This exception is called when either Line 37 or 38 pass a nondigit.
        traceback.print_exc() # This prints the stack trace of the failed statement inside try block
        print("The measured number is nondigit")