from flask import Blueprint, render_template, session, abort
from flask_login import current_user
from monolith.database import (
    db,
    Restaurant,
    Positive,
)
from monolith.forms import ReservationForm
from monolith.services import (
    UserService,
    RestaurantServices,
)
from monolith.utils import DispatcherMessage
from monolith.app_constant import CALCULATE_RATING_RESTAURANTS

home = Blueprint("home", __name__)

@home.route("/")
def index():
    DispatcherMessage.send_message(CALCULATE_RATING_RESTAURANTS, [])
    restaurants = db.session.query(Restaurant).all()
    if current_user is None:
        _test = "anonymous_test"
    else:
        _test = "logged_test"
    if "ROLE" in session:
        if session["ROLE"] == "HEALTH":
            n_positive = db.session.query(Positive).filter_by(marked=True).count()
            n_healed = (
                db.session.query(Positive)
                .filter_by(marked=False)
                .distinct(Positive.user_id)
                .count()
            )
            return render_template(
                "index_health.html",
                _test=_test,
                n_positive=n_positive,
                n_healed=n_healed,
            )
        elif session["ROLE"] == "OPERATOR":
            if "RESTAURANT_ID" in session:
                restaurant_id = session["RESTAURANT_ID"]
                model = RestaurantServices.get_all_restaurants_info(restaurant_id)
                if model is None:
                    abort(501)
                weekDaysLabel = [
                    "Monday",
                    "Tuesday",
                    "Wednesday",
                    "Thursday",
                    "Friday",
                    "Saturday",
                    "Sunday",
                ]
                return render_template(
                    "restaurantsheet.html",
                    id=restaurant_id,
                    name=model.name,
                    lat=model.lat,
                    lon=model.lon,
                    phone=model.phone,
                    covid_measures=model.covid_measures,
                    hours=model.opening_hours,
                    cuisine=model.cusine,
                    weekDaysLabel=weekDaysLabel,
                    photos=model.photos,
                    reviews=RestaurantServices.get_three_reviews(restaurant_id),
                    dishes=model.dishes,
                    _test=_test,
                )
            else:
                return render_template("norestaurant.html", _test=_test)
        elif session["ROLE"] == "CUSTOMER":
            form = ReservationForm()
            is_positive = UserService.is_positive(current_user.id)
            return render_template(
                "index_customer.html",
                _test=_test,
                restaurants=restaurants,
                form=form,
                is_positive=is_positive,
            )

    return render_template("index.html", _test=_test, restaurants=restaurants)


import pygame
from utils import cycle_sprite, update, place_items

pygame.init()

#VALUES
MAX_HEIGHT = 500
MAX_WIDTH = 500
BACKGROUND_COLOR = (255, 100, 255)

SPRITE_WIDTH = 40
SPRITE_HEIGHT = 60

x = (MAX_WIDTH / 2) - (SPRITE_WIDTH / 2)
y = (MAX_HEIGHT / 2) - (SPRITE_HEIGHT / 2)

PLAYER = pygame.image.load("maria.png")
PLAYER_RECT = PLAYER.get_rect()

SPEED = 10

SCORE = 0

#make the window, set bg color
win = pygame.display.set_mode((MAX_WIDTH, MAX_HEIGHT))
win.fill(BACKGROUND_COLOR)

run = True

player_cycle = 0

#put the prizes and enemies on the screen

enemy_list = []
enemy_token_list = []

prize_list = []
prize_token_list = []

while run:
  pygame.time.delay(50)

  pygame.display.set_caption("SCORE: " + str(SCORE))

  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      run = False

  keys = pygame.key.get_pressed()

  if keys[pygame.K_LEFT] and (x-SPEED >= 0):
    x -= SPEED
    player_cycle = update(player_cycle)
  if keys[pygame.K_RIGHT] and (x+SPEED <= MAX_WIDTH-SPRITE_WIDTH):
    x += SPEED
    player_cycle = update(player_cycle)
  if keys[pygame.K_UP] and (y-SPEED >= 0):
    y -= SPEED
    player_cycle = update(player_cycle)
  if keys[pygame.K_DOWN] and (y+SPEED <= MAX_HEIGHT-SPRITE_HEIGHT):
    y += SPEED
    player_cycle = update(player_cycle)

  win.fill(BACKGROUND_COLOR)

  pygame.display.update()

pygame.quit()


from django import forms
from django.forms import ModelForm
from django.forms import widgets
from django.forms.models import ModelChoiceField
from django.forms.widgets import Widget
from . models import TipoMoneda, Contacto

class ContactoForm(forms.ModelForm):

    class Meta:
        model= Contacto
        fields = ['numero_identificaion', 'nombre_completo', 'telefono', 'direccion', 'email' , 'pais','contraseña','moneda']
        labels ={
            'numero_identificaion': ' Ingrese su Numero de Identificacion:',
            'nombre_completo': 'Ingrese su Nombre completo:',
            'telefono': 'Ingrese su Telefono:',
            'direccion': 'Ingrese su Direccion:',
            'email': 'Ingrese su Email:',
            'pais': 'Ingrese su Pais:',
            'contraseña': 'Ingrese su Contraseña',
            'moneda': 'Ingrese su Moneda',
        }
        widgets={
            'numero_identificaion': forms.TextInput(
                attrs={
                    'class': 'form-control',
                    'placeholder': 'Numero de Identificacion',
                    'id': 'numero_identificaion',
                    'name': 'numero_identificaion',

                }
            ),
            'nombre_completo': forms.TextInput(
                attrs={
                    'class': 'form-control',
                    'placeholder': 'Nombre Completo',
                    'id': 'nombre_completo',
                    'name': 'nombre_completo',
                }
            ),
            'telefono': forms.TextInput(
                attrs={
                    'class': 'form-control',
                    'placeholder': 'Telefono',
                    'id': 'telefono',
                    'name': 'telefono',
                }
            ),

            'direccion': forms.TextInput(
                attrs={
                    'class': 'form-control',
                    'placeholder': 'Direccion',
                    'id': 'direccion',
                    'name': 'direccion',
                }
            ),
            'email': forms.TextInput(
                attrs={
                    'class': 'form-control',
                    'placeholder': 'Email',
                    'id': 'email',
                    'name': 'email',
                    'type': 'email',
                }
            ),
            'pais': forms.TextInput(
                attrs={
                    'class': 'form-control',
                    'placeholder': 'Pais',
                    'id': 'pais',
                    'name': 'pais',
                }
            ),
            'contraseña': forms.TextInput(
                attrs={
                    'class': 'form-control',
                    'placeholder': 'Contraseña',
                    'id': 'pais',
                    'name': 'contraseña',
                    'type': 'password'
                }
            ),
            'moneda': forms.Select(
                attrs={
                    'class': 'form-control',
                    'id': 'moneda',
                    'name': 'moneda'
                }
            ),

        }


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

# In[1]:

import pandas as pd
import numpy as np

# In[4]:

data=pd.read_csv("vgsales.csv")
data

# In[6]:

#DATA CLEANING
#Mengecek Missing Value
data.isnull().sum()

# In[10]:

# melakukan isian missing value,
from scipy.stats import mode

data['Year'] = data['Year'].fillna(data['Year'].mode()[0])
data['Publisher'] = data['Publisher'].fillna(data['Publisher'].mode()[0])

# In[12]:

#mengecek data missing value
data.isnull().sum()

# In[14]:

data.info()

# In[17]:

data['Year']=data['Year'].astype('int64')
data.info()

# In[18]:

data.to_csv("vgsales_clean.csv")

# In[42]:

#import package
import pandas as pd
import numpy as np
import matplotlib as plt
from pylab import *
import seaborn as sns
from matplotlib import *
import sys
from pylab import pl
f = figure.Figure(figsize=(17,10))

# In[43]:

#TOTAL PENJUALAN DAN JUMLAH GAME DI SELURUH DUNIA (BAR CHART)
plt.figure(figsize=(17,10))

plt.subplot(2,2,1)
sns.barplot(x='Year', y='Global_Sales', data=data.groupby(data['Year']).sum().Global_Sales.reset_index(), palette='Spectral')
plt.ylabel('Global_sales')
plt.xlabel('Tahun')
plt.xticks(rotation=90)
plt.title('Total Penjualan Game di Seluruh Dunia')

plt.subplot(2,2,2)
sns.barplot(x=data['Year'].value_counts().index, y=data['Year'].value_counts())
plt.ylabel('Jumlah Game')
plt.xlabel('Tahun')
plt.xticks(rotation=90)
plt.title('Total Jumlah Game yang Dirilis setiap Tahun')

# Karna data sebelumnya belum tampil, naka

# In[51]:

EU= data.pivot_table('EU_Sales', columns='Name',index='Year', aggfunc='sum').sum(axis=1)
NA= data.pivot_table('NA_Sales', columns='Name',index='Year', aggfunc='sum').sum(axis=1)
JP= data.pivot_table('JP_Sales', columns='Name',index='Year', aggfunc='sum').sum(axis=1)
Other= data.pivot_table('Other_Sales', columns='Name',index='Year', aggfunc='sum').sum(axis=1)
years= Other.index.astype(int)

plt.figure(figsize=(10,6))
ax = sns.pointplot(x=years, y=EU, color='mediumslateblue', scale=0.7)
ax = sns.pointplot(x=years, y=NA, color='cornflowerblue', scale=0.7)
ax = sns.pointplot(x=years, y=JP, color='orchid', scale=0.7)
ax = sns.pointplot(x=years, y=Other, color='thistle', scale=0.7)
ax.set_xticklabels(labels=years, rotation=90)
ax.set_xlabel(xlabel='Tahun', fontsize=14)
ax.set_ylabel(ylabel='Penjualan', fontsize=14)
ax.set_title(label='Penjualan Game di Berbagai Negara', fontsize=15)
ax.legend(handles=ax.lines[::len(years)+1], labels=['European Unio','Nort America', 'Japan','Other'])
plt.show()

# In[55]:

#Persentase Genre Game (Pie Chart)
plt.figure(figsize=(8,8))
color=['#0a5f38','#048243','#0cb577','#12e193','#05ffa6','#7bfdc7','#b8ffeb','#13eac9','#24bca8','#029386','#014d4e']
exp=[0.1,0,0,0,0,0,0,0,0,0,0,0]
plt.pie(data['Genre'].value_counts(), labels=data['Genre'].value_counts().index, autopct='%1.1f%%',explode=exp, colors=color);
plt.title('Genre');

# In[61]:

df= data.groupby(['Publisher']).sum()['Global_Sales']
df=pd.DataFrame(df.sort_values(ascending=False))[0:10]
publisher=df.index
df.columns=['Global Sales']

plt.figure(figsize=(10,6))
ax= sns.barplot(y=publisher, x='Global Sales', data=df, palette='Purples_r')
ax.set_xlabel(xlabel='Global Sales', fontsize=14)
ax.set_ylabel(ylabel='Publisher', fontsize=14)
ax.set_title(label='Top 10 Total Penjualan Publisher', fontsize=15)
ax.set_yticklabels(labels= publishers)
plt.show()

# In[ ]:


from uplogic.nodes import ULConditionNode
from uplogic.nodes import ULOutSocket
from uplogic.utils.constants import LOGIC_OPERATORS
from uplogic.utils.constants import STATUS_WAITING
from uplogic.utils import is_waiting

class ULCompare(ULConditionNode):
    def __init__(self, operator='GREATER'):
        ULConditionNode.__init__(self)
        self.operator = operator
        self.param_a = None
        self.param_b = None
        self.threshold = None
        self.RESULT = ULOutSocket(self, self.get_result)

    def get_result(self):
        socket = self.get_output('result')
        if socket is None:
            a = self.get_input(self.param_a)
            b = self.get_input(self.param_b)
            threshold = self.get_input(self.threshold)
            operator = self.get_input(self.operator)
            if is_waiting(a, b, threshold):
                return STATUS_WAITING
            if operator > 1:  # eq and neq are valid for None
                if a is None:
                    return
                if b is None:
                    return
            if threshold is None:
                threshold = 0
            if threshold > 0 and abs(a - b) < threshold:
                a = b
            if operator is None:
                return
            return self.set_output(
                'result',
                LOGIC_OPERATORS[operator](a, b)
            )
        return socket

    def evaluate(self):
        self._set_ready()


class Solution:
    def moveZeroes(self, nums: List[int]) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        fast= 0
        for slow in range(len(nums)):
            if nums[slow] == 0:
                if fast < slow:
                    fast = slow + 1
                while fast < len(nums) and nums[fast] == 0:
                    fast+=1
                if fast == len(nums):
                    return
                nums[slow] = nums[fast]
                nums[fast] = 0


#! /usr/bin/python3
import sys
import os

def main():
    if (len(sys.argv) != 2):
        print("使用方法：./replace_case.py 模板文件")
        return

    template = ""
    dirname = os.path.dirname(sys.argv[1])
    title = []
    cases = []

    # 删除旧文件
    os.system('rm ' + dirname + '/case_*')
    # 解析模板文件
    with open(sys.argv[1], "r", encoding='utf-8') as template_file:
        template = template_file.read()

    # 解析ini文件
    try:
      with open(os.path.join(dirname, 'cases.ini'), "r", encoding='utf-8') as replace_file:
          all = replace_file.read()
          all = all.split("\n")
          title = all[0].split()
          for item in all[1:]:
              item = item.strip()
              if item == '': continue
              if item[0] == '#': continue
              if item[0] == ';': continue
              cases.append(item)
    except FileNotFoundError:
        print(os.path.join(dirname, 'cases.ini') + " 文件不存在")
        return

    print("Title: " + str(title))

    id = 0
    for case in cases:
        prototxt = template
        values = case.strip()
        for i in range(len(title)):
            value = ""
            # 处理多维度shape
            if "shape" in title[i]:
                if values[0] != "{":
                    print("shape 格式错误，请用{}包围")
                    return
                end = values.find("}") + 1
                shape = values[:end]
                dim_list = shape[1:-1].split()
                value = "{\n"
                for dim in dim_list:
                    value = value + "    dims: " + dim + "\n"
                value = value + "  }"
                values = values[end+1:]
            else:
                # 其他参数
                split = values.split()
                value = split[0]
                values = " ".join(split[1:])
            prototxt = prototxt.replace(title[i], value)
            # print(title[i] + ":" + value)
            # print("case_" + str(id) + ".prototxt:\n" + prototxt)
        with open(os.path.join(dirname, "case_" + "{0:04d}".format(id) + ".prototxt"), "w") as file:
            file.write(prototxt)
        print(str(case) + " \t\t==>  case_" + "{0:04d}".format(id) + ".prototxt 生成成功")
        id += 1

    print("Over")

if __name__ == "__main__":
    main()


from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome('C:\\webdriver\\chromedriver.exe')
driver.get("http://www.python.org")
assert "Python" in driver.title
elem = driver.find_element_by_name("q")
elem.clear()
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)

algun_resultado = driver.find_element_by_xpath('//*[@id="content"]/div/section/form/ul/li[2]/h3/a')
algun_resultado.click()

assert "No results found." not in driver.page_source
#driver.close()


from tkinter import *
from tkinter import ttk
from tkinter import messagebox
import tkinter as tk

 #определяем калькулятор
root = Tk()
root.title("Калькулятор")

#функция калькулятора
def add_digit(var):
    value = calc.get()
    if value[0] == "0" and len(value) == 1: #если в начале стоит 0, то она обрезается, len нужен, чтобы нолик не удалялся в начале какой либо операции
        value = value[1:]
    calc.delete(0, tk.END)
    calc.insert(0, value + var)

def add_oper(oper):
    value = calc.get()
    if value[-1] in "-+/*": #замена операции, если водят другую операцию последней
        value = value[:-1]
    elif "+" in value or "-" in value or "/" in value or "*" in value: #условие для сложения значений, если мы добавляем еще одну оперпцию
        calculate()
        value = calc.get()
    calc.delete(0, tk.END)
    calc.insert(0, value + oper)

def calculate():
    value = calc.get()
    calc.delete(0, tk.END)
    try:
        calc.insert(0, eval(value))
    except ZeroDivisionError:
        messagebox.showinfo("Ошибка!", "Нельзя делить на ноль!")
        calc.insert(0, "0")

def add_del(del1):
    calc.delete(0, tk.END)
    calc.insert(0, "0")

#функция для кнопок
def type_digit(var):
    return ttk.Button(text=var, command=lambda : add_digit(var))

def type_oper(oper):
    return ttk.Button(text=oper, command=lambda : add_oper(oper))

def type_del(del1):
    return ttk.Button(text=del1, command=lambda : add_del(del1))

def type_equal(eql):
    return ttk.Button(text=eql, command=calculate)

#окно ввода
calc = ttk.Entry(root, justify=tk.RIGHT, font="Arial 18")
calc.insert(0, "0")
calc.grid(row=0, column=0, columnspan=4, stick="we")

#кнопки ввода
type_digit("1").grid(row=1, column=0, stick="wens")
type_digit("2").grid(row=1, column=1, stick="wens")
type_digit("3").grid(row=1, column=2, stick="wens")
type_digit("4").grid(row=2, column=0, stick="wens")
type_digit("5").grid(row=2, column=1, stick="wens")
type_digit("6").grid(row=2, column=2, stick="wens")
type_digit("7").grid(row=3, column=0, stick="wens")
type_digit("8").grid(row=3, column=1, stick="wens")
type_digit("9").grid(row=3, column=2, stick="wens")
type_digit("0").grid(row=4, column=0, stick="wens")
type_oper("+").grid(row=1, column=3, stick="wens")
type_oper("-").grid(row=2, column=3, stick="wens")
type_oper("/").grid(row=3, column=3, stick="wens")
type_oper("*").grid(row=4, column=3, stick="wens")
type_equal("=").grid(row=4, column=2, stick="wens")
type_del("C").grid(row=4, column=1, stick="wens")

root.grid_columnconfigure(0, minsize=60)
root.grid_columnconfigure(1, minsize=60)
root.grid_columnconfigure(2, minsize=60)

root.grid_rowconfigure(1, minsize=60)
root.grid_rowconfigure(2, minsize=60)
root.grid_rowconfigure(3, minsize=60)
root.grid_rowconfigure(4, minsize=60)

root.mainloop()


#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
-------------------------------------------------
   File Name：     run_experiment
   Description :
   Author :       chenhao
   date：          2021/4/6
-------------------------------------------------
   Change Activity:
                   2021/4/6:
-------------------------------------------------
"""

import os
import logging
import traceback

import shutil

import fire

from config_ai.experiments import ExperimentFactory

logger = logging.getLogger(__name__)

def run_experiment(config_path):
    experiment = ExperimentFactory.create(config_path=config_path)
    try:
        experiment.run()
    except Exception as e:
        logger.error("run experiment failed, clean experiment files...")
        experiment_path = experiment.experiment_path
        if os.path.exists(experiment_path):
            logger.info(f"removing failed experiment :{experiment_path}")
            shutil.rmtree(experiment_path)
        logger.exception("")

if __name__ == '__main__':
    fire.Fire(run_experiment)


import bindings
from compile import FullyQualifiedCommand, KeyCombo
import yaml

allcommands = []
commandsmap = {}

def register_command(combo, cmd):
    combo = str(combo)
    if combo in commandsmap:
        commandsmap[combo].append(cmd.to_dict())
    else:
        commandsmap[combo] = [cmd.to_dict()]

for context_group in bindings.keyconfig_data:
    context = context_group[0]
    types = context_group[1]
    specs = context_group[2]
    for item in specs["items"]:
        command = item[0]
        keycombo = KeyCombo.from_dict(item[1])
        options = item[2]
        fqcommand = FullyQualifiedCommand(
            context, types["space_type"], types["region_type"], command
        )
        allcommands.append(fqcommand)
        register_command(keycombo, fqcommand)

print(yaml.dump(commandsmap))


from .category import Category
from estrutura_ordem_nums.category_type import CategoryType
from utilidades.print_helper import list_item

class Visitor(Category):
    def __init__(self, genero, idade, encontrou):
        super().__init__(CategoryType.VISITOR)

        self.genero = genero
        self.idade = idade
        self.encontrou = encontrou

    def show_info(self):
        list_item("Gênero", self.genero)
        list_item("idade", self.idade)
        list_item("De onde que você ouviu falar da instiutição da casa da criança com cancer? ", self.encontrou)


# -*- coding: utf-8 -*-
"""
Define some generic socket functions for network modules
"""

# Import python libs
import os
import re
import logging
import subprocess
import socket
import platform

from hubblestack.utils.versions import LooseVersion
import hubblestack.utils.path
import hubblestack.utils.platform
from hubblestack.utils._compat import ipaddress

# Attempt to import wmi
try:
    import wmi
    import hubblestack.utils.winapi
except ImportError:
    pass

log = logging.getLogger(__name__)

try:
    import ctypes
    import ctypes.util
    libc = ctypes.cdll.LoadLibrary(ctypes.util.find_library("c"))
    res_init = libc.__res_init
except (ImportError, OSError, AttributeError, TypeError):
    pass

def natural_ipv4_netmask(ip, fmt='prefixlen'):
    """
    Returns the "natural" mask of an IPv4 address
    """
    bits = _ipv4_to_bits(ip)

    if bits.startswith('11'):
        mask = '24'
    elif bits.startswith('1'):
        mask = '16'
    else:
        mask = '8'

    if fmt == 'netmask':
        return cidr_to_ipv4_netmask(mask)
    else:
        return '/' + mask

def _ipv4_to_bits(ipaddr):
    """
    Accepts an IPv4 dotted quad and returns a string representing its binary
    counterpart
    """
    return ''.join([bin(int(x))[2:].rjust(8, '0') for x in ipaddr.split('.')])

def cidr_to_ipv4_netmask(cidr_bits):
    """
    Returns an IPv4 netmask
    """
    try:
        cidr_bits = int(cidr_bits)
        if not 1 <= cidr_bits <= 32:
            return ''
    except ValueError:
        return ''

    netmask = ''
    for idx in range(4):
        if idx:
            netmask += '.'
        if cidr_bits >= 8:
            netmask += '255'
            cidr_bits -= 8
        else:
            netmask += '{0:d}'.format(256 - (2 ** (8 - cidr_bits)))
            cidr_bits = 0
    return netmask

def interfaces():
    """
    Return a dictionary of information about all the interfaces on the minion
    """
    if hubblestack.utils.platform.is_windows():
        return win_interfaces()
    elif hubblestack.utils.platform.is_netbsd():
        return netbsd_interfaces()
    else:
        return linux_interfaces()

def win_interfaces():
    """
    Obtain interface information for Windows systems
    """
    with hubblestack.utils.winapi.Com():
        c = wmi.WMI()
        ifaces = {}
        for iface in c.Win32_NetworkAdapterConfiguration(IPEnabled=1):
            ifaces[iface.Description] = dict()
            if iface.MACAddress:
                ifaces[iface.Description]['hwaddr'] = iface.MACAddress
            if iface.IPEnabled:
                ifaces[iface.Description]['up'] = True
                for ip in iface.IPAddress:
                    if '.' in ip:
                        if 'inet' not in ifaces[iface.Description]:
                            ifaces[iface.Description]['inet'] = []
                        item = {'address': ip,
                                'label': iface.Description}
                        if iface.DefaultIPGateway:
                            broadcast = next((i for i in iface.DefaultIPGateway if '.' in i), '')
                            if broadcast:
                                item['broadcast'] = broadcast
                        if iface.IPSubnet:
                            netmask = next((i for i in iface.IPSubnet if '.' in i), '')
                            if netmask:
                                item['netmask'] = netmask
                        ifaces[iface.Description]['inet'].append(item)
                    if ':' in ip:
                        if 'inet6' not in ifaces[iface.Description]:
                            ifaces[iface.Description]['inet6'] = []
                        item = {'address': ip}
                        if iface.DefaultIPGateway:
                            broadcast = next((i for i in iface.DefaultIPGateway if ':' in i), '')
                            if broadcast:
                                item['broadcast'] = broadcast
                        if iface.IPSubnet:
                            netmask = next((i for i in iface.IPSubnet if ':' in i), '')
                            if netmask:
                                item['netmask'] = netmask
                        ifaces[iface.Description]['inet6'].append(item)
            else:
                ifaces[iface.Description]['up'] = False
    return ifaces

def linux_interfaces():
    """
    Obtain interface information for *NIX/BSD variants
    """
    ifaces = dict()
    ip_path = hubblestack.utils.path.which('ip')
    ifconfig_path = None if ip_path else hubblestack.utils.path.which('ifconfig')
    if ip_path:
        cmd1 = subprocess.Popen(
            '{0} link show'.format(ip_path),
            shell=True,
            close_fds=True,
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT).communicate()[0]
        cmd2 = subprocess.Popen(
            '{0} addr show'.format(ip_path),
            shell=True,
            close_fds=True,
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT).communicate()[0]
        ifaces = _interfaces_ip("{0}\n{1}".format(
            hubblestack.utils.stringutils.to_str(cmd1),
            hubblestack.utils.stringutils.to_str(cmd2)))
    elif ifconfig_path:
        cmd = subprocess.Popen(
            '{0} -a'.format(ifconfig_path),
            shell=True,
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT).communicate()[0]
        ifaces = _interfaces_ifconfig(hubblestack.utils.stringutils.to_str(cmd))
    return ifaces

def _interfaces_ip(out):
    """
    Uses ip to return a dictionary of interfaces with various information about
    each (up/down state, ip address, netmask, and hwaddr)
    """
    ret = dict()

    def parse_network(value, cols):
        """
        Return a tuple of ip, netmask, broadcast
        based on the current set of cols
        """
        brd = None
        scope = None
        if '/' in value:  # we have a CIDR in this address
            ip, cidr = value.split('/')  # pylint: disable=C0103
        else:
            ip = value  # pylint: disable=C0103
            cidr = 32

        if type_ == 'inet':
            mask = cidr_to_ipv4_netmask(int(cidr))
            if 'brd' in cols:
                brd = cols[cols.index('brd') + 1]
        elif type_ == 'inet6':
            mask = cidr
            if 'scope' in cols:
                scope = cols[cols.index('scope') + 1]
        return (ip, mask, brd, scope)

    groups = re.compile('\r?\n\\d').split(out)
    for group in groups:
        iface = None
        data = dict()

        for line in group.splitlines():
            if ' ' not in line:
                continue
            match = re.match(r'^\d*:\s+([\w.\-]+)(?:@)?([\w.\-]+)?:\s+<(.+)>', line)
            if match:
                iface, parent, attrs = match.groups()
                if 'UP' in attrs.split(','):
                    data['up'] = True
                else:
                    data['up'] = False
                if parent:
                    data['parent'] = parent
                continue

            cols = line.split()
            if len(cols) >= 2:
                type_, value = tuple(cols[0:2])
                iflabel = cols[-1:][0]
                if type_ in ('inet', 'inet6'):
                    if 'secondary' not in cols:
                        ipaddr, netmask, broadcast, scope = parse_network(value, cols)
                        if type_ == 'inet':
                            if 'inet' not in data:
                                data['inet'] = list()
                            addr_obj = dict()
                            addr_obj['address'] = ipaddr
                            addr_obj['netmask'] = netmask
                            addr_obj['broadcast'] = broadcast
                            addr_obj['label'] = iflabel
                            data['inet'].append(addr_obj)
                        elif type_ == 'inet6':
                            if 'inet6' not in data:
                                data['inet6'] = list()
                            addr_obj = dict()
                            addr_obj['address'] = ipaddr
                            addr_obj['prefixlen'] = netmask
                            addr_obj['scope'] = scope
                            data['inet6'].append(addr_obj)
                    else:
                        if 'secondary' not in data:
                            data['secondary'] = list()
                        ip_, mask, brd, scp = parse_network(value, cols)
                        data['secondary'].append({
                            'type': type_,
                            'address': ip_,
                            'netmask': mask,
                            'broadcast': brd,
                            'label': iflabel,
                        })
                        del ip_, mask, brd, scp
                elif type_.startswith('link'):
                    data['hwaddr'] = value
        if iface:
            ret[iface] = data
            del iface, data
    return ret

def _interfaces_ifconfig(out):
    """
    Uses ifconfig to return a dictionary of interfaces with various information
    about each (up/down state, ip address, netmask, and hwaddr)
    """
    ret = dict()

    piface = re.compile(r'^([^\s:]+)')
    pmac = re.compile('.*?(?:HWaddr|ether|address:|lladdr) ([0-9a-fA-F:]+)')
    if hubblestack.utils.platform.is_sunos():
        pip = re.compile(r'.*?(?:inet\s+)([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)(.*)')
        pip6 = re.compile('.*?(?:inet6 )([0-9a-fA-F:]+)')
        pmask6 = re.compile(r'.*?(?:inet6 [0-9a-fA-F:]+/(\d+)).*')
    else:
        pip = re.compile(r'.*?(?:inet addr:|inet [^\d]*)(.*?)\s')
        pip6 = re.compile('.*?(?:inet6 addr: (.*?)/|inet6 )([0-9a-fA-F:]+)')
        pmask6 = re.compile(
            r'.*?(?:inet6 addr: [0-9a-fA-F:]+/(\d+)|prefixlen (\d+))(?: Scope:([a-zA-Z]+)| scopeid (0x[0-9a-fA-F]))?')
    pmask = re.compile(r'.*?(?:Mask:|netmask )(?:((?:0x)?[0-9a-fA-F]{8})|([\d\.]+))')
    pupdown = re.compile('UP')
    pbcast = re.compile(r'.*?(?:Bcast:|broadcast )([\d\.]+)')

    groups = re.compile('\r?\n(?=\\S)').split(out)
    for group in groups:
        data = dict()
        iface = ''
        updown = False
        for line in group.splitlines():
            miface = piface.match(line)
            mmac = pmac.match(line)
            mip = pip.match(line)
            mip6 = pip6.match(line)
            mupdown = pupdown.search(line)
            if miface:
                iface = miface.group(1)
            if mmac:
                data['hwaddr'] = mmac.group(1)
                if hubblestack.utils.platform.is_sunos():
                    expand_mac = []
                    for chunk in data['hwaddr'].split(':'):
                        expand_mac.append('0{0}'.format(chunk) if len(chunk) < 2 else '{0}'.format(chunk))
                    data['hwaddr'] = ':'.join(expand_mac)
            if mip:
                if 'inet' not in data:
                    data['inet'] = list()
                addr_obj = dict()
                addr_obj['address'] = mip.group(1)
                mmask = pmask.match(line)
                if mmask:
                    if mmask.group(1):
                        mmask = _number_of_set_bits_to_ipv4_netmask(
                            int(mmask.group(1), 16))
                    else:
                        mmask = mmask.group(2)
                    addr_obj['netmask'] = mmask
                mbcast = pbcast.match(line)
                if mbcast:
                    addr_obj['broadcast'] = mbcast.group(1)
                data['inet'].append(addr_obj)
            if mupdown:
                updown = True
            if mip6:
                if 'inet6' not in data:
                    data['inet6'] = list()
                addr_obj = dict()
                addr_obj['address'] = mip6.group(1) or mip6.group(2)
                mmask6 = pmask6.match(line)
                if mmask6:
                    addr_obj['prefixlen'] = mmask6.group(1) or mmask6.group(2)
                    if not hubblestack.utils.platform.is_sunos():
                        ipv6scope = mmask6.group(3) or mmask6.group(4)
                        addr_obj['scope'] = ipv6scope.lower() if ipv6scope is not None else ipv6scope
                # SunOS sometimes has ::/0 as inet6 addr when using addrconf
                if not hubblestack.utils.platform.is_sunos() \
                        or addr_obj['address'] != '::' \
                        and addr_obj['prefixlen'] != 0:
                    data['inet6'].append(addr_obj)
        data['up'] = updown
        if iface in ret:
            # SunOS optimization, where interfaces occur twice in 'ifconfig -a'
            # output with the same name: for ipv4 and then for ipv6 addr family.
            # Every instance has it's own 'UP' status and we assume that ipv4
            # status determines global interface status.
            #
            # merge items with higher priority for older values
            # after that merge the inet and inet6 sub items for both
            ret[iface] = dict(list(data.items()) + list(ret[iface].items()))
            if 'inet' in data:
                ret[iface]['inet'].extend(x for x in data['inet'] if x not in ret[iface]['inet'])
            if 'inet6' in data:
                ret[iface]['inet6'].extend(x for x in data['inet6'] if x not in ret[iface]['inet6'])
        else:
            ret[iface] = data
        del data
    return ret

def _number_of_set_bits_to_ipv4_netmask(set_bits):  # pylint: disable=C0103
    """
    Returns an IPv4 netmask from the integer representation of that mask.

    Ex. 0xffffff00 -> '255.255.255.0'
    """
    return cidr_to_ipv4_netmask(_number_of_set_bits(set_bits))

def _number_of_set_bits(x):
    """
    Returns the number of bits that are set in a 32bit int
    """
    # Taken from http://stackoverflow.com/a/4912729. Many thanks!
    x -= (x >> 1) & 0x55555555
    x = ((x >> 2) & 0x33333333) + (x & 0x33333333)
    x = ((x >> 4) + x) & 0x0f0f0f0f
    x += x >> 8
    x += x >> 16
    return x & 0x0000003f

def netbsd_interfaces():
    """
    Obtain interface information for NetBSD >= 8 where the ifconfig
    output diverged from other BSD variants (Netmask is now part of the
    address)
    """
    # NetBSD versions prior to 8.0 can still use linux_interfaces()
    if LooseVersion(os.uname()[2]) < LooseVersion('8.0'):
        return linux_interfaces()

    ifconfig_path = hubblestack.utils.path.which('ifconfig')
    cmd = subprocess.Popen(
        '{0} -a'.format(ifconfig_path),
        shell=True,
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT).communicate()[0]
    return _netbsd_interfaces_ifconfig(hubblestack.utils.stringutils.to_str(cmd))

def _netbsd_interfaces_ifconfig(out):
    """
    Uses ifconfig to return a dictionary of interfaces with various information
    about each (up/down state, ip address, netmask, and hwaddr)
    """
    ret = dict()

    piface = re.compile(r'^([^\s:]+)')
    pmac = re.compile('.*?address: ([0-9a-f:]+)')

    pip = re.compile(r'.*?inet [^\d]*(.*?)/([\d]*)\s')
    pip6 = re.compile(r'.*?inet6 ([0-9a-f:]+)%([a-zA-Z0-9]*)/([\d]*)\s')

    pupdown = re.compile('UP')
    pbcast = re.compile(r'.*?broadcast ([\d\.]+)')

    groups = re.compile('\r?\n(?=\\S)').split(out)
    for group in groups:
        data = dict()
        iface = ''
        updown = False
        for line in group.splitlines():
            miface = piface.match(line)
            mmac = pmac.match(line)
            mip = pip.match(line)
            mip6 = pip6.match(line)
            mupdown = pupdown.search(line)
            if miface:
                iface = miface.group(1)
            if mmac:
                data['hwaddr'] = mmac.group(1)
            if mip:
                if 'inet' not in data:
                    data['inet'] = list()
                addr_obj = dict()
                addr_obj['address'] = mip.group(1)
                mmask = mip.group(2)
                if mip.group(2):
                    addr_obj['netmask'] = cidr_to_ipv4_netmask(mip.group(2))
                mbcast = pbcast.match(line)
                if mbcast:
                    addr_obj['broadcast'] = mbcast.group(1)
                data['inet'].append(addr_obj)
            if mupdown:
                updown = True
            if mip6:
                if 'inet6' not in data:
                    data['inet6'] = list()
                addr_obj = dict()
                addr_obj['address'] = mip6.group(1)
                mmask6 = mip6.group(3)
                addr_obj['scope'] = mip6.group(2)
                addr_obj['prefixlen'] = mip6.group(3)
                data['inet6'].append(addr_obj)
        data['up'] = updown
        ret[iface] = data
        del data
    return ret

def get_fqhostname():
    """
    Returns the fully qualified hostname
    """
    l = [socket.getfqdn()]

    # try socket.getaddrinfo
    try:
        addrinfo = socket.getaddrinfo(
            socket.gethostname(), 0, socket.AF_UNSPEC, socket.SOCK_STREAM,
            socket.SOL_TCP, socket.AI_CANONNAME
        )
        for info in addrinfo:
            # info struct [family, socktype, proto, canonname, sockaddr]
            if len(info) >= 4:
                l.append(info[3])
    except socket.gaierror:
        pass

    return l and l[0] or None

def ip_addrs(interface=None, include_loopback=False, interface_data=None):
    """
    Returns a list of IPv4 addresses assigned to the host. 127.0.0.1 is
    ignored, unless 'include_loopback=True' is indicated. If 'interface' is
    provided, then only IP addresses from that interface will be returned.
    """
    return _ip_addrs(interface, include_loopback, interface_data, 'inet')

def _ip_addrs(interface=None, include_loopback=False, interface_data=None, proto='inet'):
    """
    Return the full list of IP adresses matching the criteria

    proto = inet|inet6
    """
    ret = set()

    ifaces = interface_data \
        if isinstance(interface_data, dict) \
        else interfaces()
    if interface is None:
        target_ifaces = ifaces
    else:
        target_ifaces = dict([(k, v) for k, v in iter(ifaces.items())
                              if k == interface])
        if not target_ifaces:
            log.error('Interface %s not found.', interface)
    for ip_info in target_ifaces.values():
        addrs = ip_info.get(proto, [])
        addrs.extend([addr for addr in ip_info.get('secondary', []) if addr.get('type') == proto])

        for addr in addrs:
            addr = ipaddress.ip_address(addr.get('address'))
            if not addr.is_loopback or include_loopback:
                ret.add(addr)
    return [str(addr) for addr in sorted(ret)]

def ip_addrs6(interface=None, include_loopback=False, interface_data=None):
    """
    Returns a list of IPv6 addresses assigned to the host. ::1 is ignored,
    unless 'include_loopback=True' is indicated. If 'interface' is provided,
    then only IP addresses from that interface will be returned.
    """
    return _ip_addrs(interface, include_loopback, interface_data, 'inet6')

def is_ipv6(ip):
    """
    Returns a bool telling if the value passed to it was a valid IPv6 address
    """
    try:
        return ipaddress.ip_address(ip).version == 6
    except ValueError:
        return False

def is_ipv4(ip):
    """
    Returns a bool telling if the value passed to it was a valid IPv4 address
    """
    try:
        return ipaddress.ip_address(ip).version == 4
    except ValueError:
        return False

def refresh_dns():
    """
    issue #21397: force glibc to re-read resolv.conf
    """
    try:
        res_init()
    except NameError:
        # Exception raised loading the library, thus res_init is not defined
        pass

def in_subnet(cidr, addr=None):
    """
    Returns True if host or (any of) addrs is within specified subnet, otherwise False
    """
    try:
        cidr = ipaddress.ip_network(cidr)
    except ValueError:
        log.error("Invalid CIDR '%s'", cidr)
        return False

    if addr is None:
        addr = ip_addrs()
        addr.extend(ip_addrs6())
    elif not isinstance(addr, (list, tuple)):
        addr = (addr,)

    return any(ipaddress.ip_address(item) in cidr for item in addr)

def _generate_minion_id():
    '''
    Get list of possible host names and convention names.

    :return:
    '''
    # There are three types of hostnames:
    # 1. Network names. How host is accessed from the network.
    # 2. Host aliases. They might be not available in all the network or only locally (/etc/hosts)
    # 3. Convention names, an internal nodename.

    class DistinctList(list):
        '''
        List, which allows one to append only distinct objects.
        Needs to work on Python 2.6, because of collections.OrderedDict only since 2.7 version.
        Override 'filter()' for custom filtering.
        '''
        localhost_matchers = [r'localhost.*', r'ip6-.*', r'127[.]\d', r'0\.0\.0\.0',
                              r'::1.*', r'ipv6-.*', r'fe00::.*', r'fe02::.*', r'1.0.0.*.ip6.arpa']

        def append(self, p_object):
            if p_object and p_object not in self and not self.filter(p_object):
                super(self.__class__, self).append(p_object)
            return self

        def extend(self, iterable):
            for obj in iterable:
                self.append(obj)
            return self

        def filter(self, element):
            'Returns True if element needs to be filtered'
            for rgx in self.localhost_matchers:
                if re.match(rgx, element):
                    return True

        def first(self):
            return self and self[0] or None

    hostname = socket.gethostname()

    hosts = DistinctList().append(
        hubblestack.utils.stringutils.to_unicode(socket.getfqdn(hubblestack.utils.stringutils.to_bytes(hostname)))
    ).append(platform.node()).append(hostname)
    if not hosts:
        try:
            for a_nfo in socket.getaddrinfo(hosts.first() or 'localhost', None, socket.AF_INET,
                                            socket.SOCK_RAW, socket.IPPROTO_IP, socket.AI_CANONNAME):
                if len(a_nfo) > 3:
                    hosts.append(a_nfo[3])
        except socket.gaierror:
            log.warning('Cannot resolve address {addr} info via socket: {message}'.format(
                addr=hosts.first() or 'localhost (N/A)', message=socket.gaierror)
            )
    # Universal method for everywhere (Linux, Slowlaris, Windows etc)
    for f_name in ('/etc/hostname', '/etc/nodename', '/etc/hosts',
                   r'{win}\system32\drivers\etc\hosts'.format(win=os.getenv('WINDIR'))):
        try:
            with hubblestack.utils.files.fopen(f_name) as f_hdl:
                for line in f_hdl:
                    line = hubblestack.utils.stringutils.to_unicode(line)
                    hst = line.strip().split('#')[0].strip().split()
                    if hst:
                        if hst[0][:4] in ('127.', '::1') or len(hst) == 1:
                            hosts.extend(hst)
        except IOError:
            pass

    # include public and private ipaddresses
    return hosts.extend([addr for addr in ip_addrs()
                         if not ipaddress.ip_address(addr).is_loopback])

def generate_minion_id():
    '''
    Return only first element of the hostname from all possible list.

    :return:
    '''
    try:
        ret = hubblestack.utils.stringutils.to_unicode(_generate_minion_id().first())
    except TypeError:
        ret = None
    return ret or 'localhost'


import cv2 as cv
import numpy as np

img = cv.imread('Resources/gray_img.png')
gray_img = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
ret, thrash = cv.threshold(gray_img, 240, 255, cv.CHAIN_APPROX_NONE)
contours, hierarchy = cv.findContours(thrash, cv.RETR_TREE, cv.CHAIN_APPROX_NONE)

for contour in contours:
    cv.drawContours(img, contour, 0, (0, 0, 0), 5)
    approx = cv.approxPolyDP(contour, 0.01*cv.arcLength(contour, True), True)
    x, y, w, h = cv.boundingRect(approx)

    if len(approx) == 3:
        cv.putText(img, "Triangle", (x, y), cv.FONT_HERSHEY_COMPLEX, 0.5, (0, 0, 0))

    elif len(approx) == 4:
        aspectRatio = float(w)/h
        if aspectRatio >= 0.95 and aspectRatio < 1.05:
            cv.putText(img, "Square", (x, y), cv.FONT_HERSHEY_COMPLEX, 0.5, (0, 0, 0))
        else:
            cv.putText(img, "Rectangle", (x, y), cv.FONT_HERSHEY_COMPLEX, 0.5, (0, 0, 0))

    elif len(approx) == 5:
        cv.putText(img, "Pentagon", (x, y), cv.FONT_HERSHEY_COMPLEX, 0.5, (0, 0, 0))

    else:
        cv.putText(img, "Circle", (x, y), cv.FONT_HERSHEY_COMPLEX, 0.5, (0, 0, 0))

cv.waitKey(0)


import random

from django.core import serializers
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse

from Carts.cart import get_single_item
from Carts.models import *
from Products.models import *
from Stats import stats

# Create your views here.

def _generate_cart_id():
    cart_id = ''
    characters = 'ABCDEFGHIJKLMNOPQRQSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()'
    cart_id_length = 50
    for y in range(cart_id_length):
        cart_id += characters[random.randint(0, len(characters) - 1)]
    return cart_id

def view(request):
    try:
        the_id = request.session['cart_id']
    except KeyError:
        the_id = None

    # the_id = 6
    if the_id:
        try:
            cart = Carts.objects.get(session_id=the_id)
            request.session['total_items'] = cart.cartitems_set.count()
            empty = False
            context = {'cart': cart, }
        except Carts.DoesNotExist:
            if request.session['total_items']:
                del request.session['total_items']
            empty = True
            context = {'empty': empty}
    else:
        empty = True
        context = {'empty': empty}

    # Others queryset
    search_recs = stats.recommended_from_search(request)
    recently_viewed = stats.get_recently_viewed(request)
    view_recs = stats.recommended_from_views(request)
    context['search_recs'] = search_recs
    context['recently_viewed'] = recently_viewed
    context['view_recs'] = view_recs

    template = 'carts/carts.html'
    return render(request, template, context)

def add_to_cart(request, slug):
    try:
        the_id = request.session['cart_id']
    except:
        cart = Carts()
        session_id = str(_generate_cart_id())
        cart.session_id = session_id
        cart.save()
        request.session['cart_id'] = cart.session_id
        the_id = cart.session_id
    try:
        cart = Carts.objects.get(session_id=the_id)
    except:
        del request.session['cart_id']
        return HttpResponseRedirect(reverse('cart_view'))

    try:
        product = Product.objects.get(slug=slug)
    except Product.DoesNotExist:
        print('The product is not register')
    except:
        pass

    cart_item, created = CartItems.objects.get_or_create(cart=cart, product=product)
    try:
        qty = request.GET.get('qty')
    except KeyError:
        print('No QTY attribute founded in request session')
    except:
        pass

    if qty == None:
        qty = 0

    if created:
        if int(qty) == 0:
            qty = 1
        cart_item.quantity = int(qty)
        cart_item.save()
    else:
        if int(qty) == 0:
            cart_item.delete()
        else:
            cart_item.quantity += int(qty)
            cart_item.save()

    new_total = 0.00
    for items in cart.cartitems_set.all():
        line_total = float(items.product.sale_price) * items.quantity
        items.line_total = line_total
        new_total += line_total
        items.save()

    request.session['total_items'] = cart.cartitems_set.count()
    cart.total = new_total
    cart.save()

    # Others queryset
    search_recs = stats.recommended_from_search(request)
    recently_viewed = stats.get_recently_viewed(request)
    view_recs = stats.recommended_from_views(request)

    template = 'carts/carts.html'
    context = locals()
    return render(request, template, context)

def remove_from_cart(request, slug):
    try:
        product = Product.objects.get(slug=slug)
        item = CartItems.objects.get(product=product)
    except:
        return HttpResponseRedirect(reverse('cart_view'))
    item.delete()
    return HttpResponseRedirect(reverse('cart_view'))

def update_cart(request):
    post_data = request.POST.copy()
    item_id = post_data['item_id']
    quantity = request.GET.get('qty')
    cart_item = get_single_item(request, item_id)
    if cart_item:
        if int(quantity) > 0:
            cart_item.quantity = int(quantity)
            cart_item.save()
            return HttpResponseRedirect(reverse('cart_view'))
        else:
            product_slug = cart_item.product.slug
            remove_from_cart(request, product_slug)
    else:
        return HttpResponseRedirect(reverse('cart_view'))

def clear_cart(request):
    pass

def base(request):
    return render(request, 'base.html', locals())


import argparse
import os
import time

import gym
import gym_minigrid
from gym_minigrid.wrappers import ImgObsWrapper, FullyObsWrapper
import torch
import numpy as np

import parl_minigrid
from parl_minigrid.envs.wrappers import EpisodeTerminationWrapper

from stable_baselines3.common.monitor import Monitor
from stable_baselines3.dqn.policies import DQNPolicy

from parl_agents.tuning.utils import TuneObjectiveCallback
from parl_agents.dqn.ddqn import DDQN
from parl_agents.nn_models.babyai_cnn import BabyAIFullyObsSmallCNN
from parl_agents.wrappers.step_cost import MiniGridCostWrapper, DQNRewardWrapper

import optuna
from optuna.integration.skopt import SkoptSampler
from optuna.pruners import BasePruner, MedianPruner, SuccessiveHalvingPruner
from optuna.samplers import BaseSampler, RandomSampler, TPESampler

N_TS=1
N_ES=1

# parameter sampler
def sample_params(trial: optuna.Trial):
    learning_rate = trial.suggest_loguniform("lr", 1e-5, 1e-3)
    buffer_size = trial.suggest_categorical("buffer_size", [1024, 4096, 10240, 40960, 102400, 409600])
    learning_starts = trial.suggest_int("learning_starts", 8, 10240)
    batch_size = trial.suggest_categorical("batch_size", [8, 16, 32, 64, 128, 256])
    train_freq = trial.suggest_int("train_freq", 4, 4096)
    gradient_steps = trial.suggest_int("gradient_steps", 1, 256)
    target_update_interval = trial.suggest_uniform("target_update_interval", 1, 40960)
    exploration_fraction = trial.suggest_uniform("exploration_fraction", 0.01, 0.2)
    exploration_final_eps = trial.suggest_loguniform("exploration_final_eps", 0.01, 0.2)

    return {
        "learning_rate": learning_rate,
        "buffer_size": buffer_size,
        "learning_starts":learning_starts,
        "batch_size": batch_size,
        "train_freq": train_freq,
        "gradient_steps": gradient_steps,
        "target_update_interval": target_update_interval,
        "exploration_fraction": exploration_fraction,
        "exploration_final_eps": exploration_final_eps,
    }

def create_env(args):
    train_env = gym.make(args.env, train_mode=True, max_steps=1024, num_train_seeds=N_TS, num_test_seeds=N_ES)
    train_env = FullyObsWrapper(train_env)
    train_env = ImgObsWrapper(train_env)
    train_env = MiniGridCostWrapper(train_env)
    train_env = DQNRewardWrapper(train_env, reward_scale=100, step_cost=0)
    train_env = EpisodeTerminationWrapper(train_env)
    train_env = Monitor(train_env)
    return train_env

class ParameterTuner:
    def __init__(self, args, hyper_params):
        self.study_name = " ".join([args.study_name, args.env])
        self.n_trials = args.trials
        self.n_timesteps = args.timesteps
        self.hyper_params = hyper_params
        self.args = args

    def objective(self, trial: optuna.Trial) -> float:
        train_env = create_env(self.args)
        self.hyper_params.update(sample_params(trial))

        # create agent
        policy_kwargs = dict(
            features_extractor_class = BabyAIFullyObsSmallCNN,
            features_extractor_kwargs = dict(features_dim=128),
            net_arch = [128, 128],
            normalize_images = False
        )

        model = DDQN(
            policy=DQNPolicy,
            env=train_env,
            tau=1.0,
            gamma=0.99,
            exploration_initial_eps=1.0,
            max_grad_norm=10,
            tensorboard_log=None,
            policy_kwargs=policy_kwargs,
            verbose=0,
            **self.hyper_params
        )
        model.trial = trial

        # call back for getting objective value
        tune_obj_call_back = TuneObjectiveCallback(trial)
        try:
            model.learn(total_timesteps=self.n_timesteps, callback=tune_obj_call_back)
            model.env.close()
        except AssertionError as e:
            model.env.close()
            print(e)
            raise optuna.exceptions.TrialPruned()

        is_pruned = tune_obj_call_back.is_pruned
        mean_ep_length = tune_obj_call_back.mean_ep_length

        del model.env
        del model

        if is_pruned:
            raise optuna.exceptions.TrialPruned()

        return mean_ep_length

    def hyperparam_optimization(self):
        self.tensorboard_log = None

        sampler = TPESampler(n_startup_trials=2, seed=0)
        pruner = MedianPruner(n_startup_trials=2, n_warmup_steps=0)
        study = optuna.create_study(
            sampler=sampler,
            pruner=pruner,
            study_name=self.study_name,
            load_if_exists=True,
            direction="minimize",
        )
        try:
            study.optimize(self.objective, n_trials=self.n_trials)
        except KeyboardInterrupt:
            pass

        print("Number of finished trials: ", len(study.trials))
        print("Best trial:")
        trial = study.best_trial
        print("Value: ", trial.value)
        print("Params: ")
        for key, value in trial.params.items():
            print(f"    {key}: {value}")
        report_name = (
            f"report_{self.study_name}_steps-{self.n_timesteps}_{int(time.time())}.csv"
        )
        log_path = os.path.join(os.path.dirname(__file__), report_name)
        # Write report
        os.makedirs(os.path.dirname(log_path), exist_ok=True)
        study.trials_dataframe().to_csv(log_path)

if __name__ == "__main__":
    print("cuda.is_available:{}".format(torch.cuda.is_available()))
    print("cuda.device_count:{}".format(torch.cuda.device_count()))
    if torch.cuda.is_available():
        print("cuda.current_device:{}".format(torch.cuda.current_device()))
        print("cuda.get_device_name:{}".format(torch.cuda.get_device_name(torch.cuda.current_device())))
    else:
        print("no cuda device")

    parser = argparse.ArgumentParser()
    parser.add_argument("--study_name", type=str, default="ddqn tuning")
    parser.add_argument("--env", type=str, default="MazeRooms-8by8-DoorKey-v0")
    parser.add_argument("--timesteps", type=int, default=1000000)
    parser.add_argument("--trials", type=int, default=100)
    args = parser.parse_args()

    hyper_params = {}
    exp_manager = ParameterTuner(args, hyper_params)
    exp_manager.hyperparam_optimization()


from django.forms import ModelForm, DateInput
from .models import CalendarModel

class CalendarModelForm(ModelForm):
    class Meta:
        model = CalendarModel
        widgets = {
            'start_time': DateInput(attrs={'type': 'date'}, format='%Y-%m-%d'),
        }
        fields = '__all__'

    def __init__(self, *args, **kwargs):
        super(CalendarModelForm, self).__init__(*args, **kwargs)
        self.fields['start_time'].input_formats = ('%Y-%m-%d',)


# /*******************************************************************************
#  Author		: Gil Gerard Austria, Justin Bernstein, Hayden Daly
#  Date			: 2019-06-09
#  Version      : Sprint.02.Proj.06
#  Description	: Parses Dictionary Data to Output All Errors in Individuals and Families Using Functions Imported from UserStories
#                 Creates/Appends to Txt file ( {fileName}_output.txt )
#                 Returns Table with Individual Data
#  Github       : https://github.com/haydendaly/GEDCOM-Parser
#  Pledge		:"I pledge my honor that I have abided by the Stevens Honor System"	- Gil Gerard Austria, Justin Bernstein, Hayden Daly
#  *******************************************************************************/

# Built-in Python Modules
import sys
import json

# Non-Built-In Python Modules that Need to be Installed
from prettytable import PrettyTable

# Import userstories
from userstories import hdUserStories, jbUserStories, gaUserStories

# Errors:
#   us01 -
#   us02 -
#   us03 -
#   us04 -
#   us05 -
#   us06 -
#   us21 -
#   us42 - (Implemented Directly in All Functions that Output Errors (in userstories))
def displayErrors(fileName, GEDCOM_dict):

    us01 = jbUserStories.us01(GEDCOM_dict)
    with open(fileName[:-4] + '_output.txt', 'a+') as output:
        output.write('[us_01] - Error: Date Occurs After Current Date\n')
        output.write(us01['invalidIndiDates']+'\n\n')
        output.write(us01['invalidFamDates']+'\n\n')
    print('\n[us01] Error: Date Occurs After Current Date')
    print( us01 )

    us02 = jbUserStories.us02(GEDCOM_dict)
    with open(fileName[:-4] + '_output.txt', 'a+') as output:
        output.write("[us02] - Error: Birth Occurs After Marriage Date\n")
        output.write(us02.get_string(title="[us02] - Error: Birth Occurs After Marriage Date")+'\n\n')
    print('\n[us02] Error: Date Occurs After Current Date')
    print( us02 )

    us03 = jbUserStories.us03(GEDCOM_dict)
    with open(fileName[:-4] + '_output.txt', 'a+') as output:
        output.write("[us03] - Error: Birth Occurs After Death Date\n")
        output.write(us02.get_string(title="[us03] - Error: Birth Occurs After Death Date")+'\n\n')
    print('\n[us03] Error: Birth date Occurs After Death Date')
    print( us03 )

    us04 = jbUserStories.us04(GEDCOM_dict)
    with open(fileName[:-4] + '_output.txt', 'a+') as output:
        output.write("[us04] - Error: Divorce Occurs Before Marriage Date\n")
        output.write(us04.get_string(title="[us04] - Error: Divorce Occurs Before Marriage Date")+'\n\n')
    print('\n[us04] Error: Marriage Occurs After Divorce Date')
    print( us04 )

    us05 = jbUserStories.us05(GEDCOM_dict)
    with open(fileName[:-4] + '_output.txt', 'a+') as output:
        output.write("[us05] - Error: Death Occurs Before Marriage Date\n")
        output.write(us05.get_string(title="[us05] - Error: Death Occurs Before Marriage Date")+'\n\n')
    print('\n[us05] Error: Marriage Occurs After Death Date')
    print( us05 )

    us06 = hdUserStories.us06(GEDCOM_dict)
    with open(fileName[:-4] + '_output.txt', 'a+') as output:
        output.write("[us06] - Error: Divorce Before Death\n")
        output.write(us06.get_string(title="[us06] - Error: Divorce Before Death")+'\n\n')
    print('\n[us06] Error: Divorce Before Death')
    print( us06 )

    us09 = jbUserStories.us09(GEDCOM_dict)
    with open(fileName[:-4] + '_output.txt', 'a+') as output:
        output.write("[us09] - Error: Birth Occurs After Parents Death Date\n")
        output.write(us09.get_string(title="[us09] - Error: Birth Occurs After Parents Death Date")+'\n\n')
    print('\n[us09] Error: Birth Occurs After Parents Death Date')
    print( us09 )

    us21 = hdUserStories.us21(GEDCOM_dict)
    with open(fileName[:-4] + '_output.txt', 'a+') as output:
        output.write("[us21] - Error: Incorrect Gender for Role\n")
        output.write(us21.get_string(title="[us21] - Error: Incorrect Gender for Role")+'\n\n')
    print('\n[us21] Error: Incorrect Gender for Role')
    print( us21 )

    us08 = jbUserStories.us08(GEDCOM_dict)
    with open(fileName[:-4] + '_output.txt', 'a+') as output:
        output.write("[us08] - Error: Birth Occurs Before Marriage or After Parents Divorce Date\n")
        output.write(us09.get_string(title="[us08] - Error: Birth Occurs Before Marriage or After Parents Divorce Date")+'\n\n')
    print('\n[us08] Error: Birth Occurs Before Marriage or After Parents Divorce Date')
    print( us08 )

    us12 = jbUserStories.us12(GEDCOM_dict)
    with open(fileName[:-4] + '_output.txt', 'a+') as output:
        output.write("[us12] - Error: Birth Occurs 60 or 80 years after the birth or mother and father respectively\n")
        output.write(us12.get_string(title="[us12] - Error: Birth Occurs 60 or 80 years after the birth or mother and father respectively")+'\n\n')
    print('\n[us12] Error: Birth Occurs 60 or 80 years after the birth or mother and father respectively')
    print( us12 )

    return

def main(fileName, GEDCOM_dict):

    # Case when the Program is Run Directly from Command Line and on an Valid Input File (Sanitized GEDCOM File from 1_sanitizedGEDCOM.py)
    if ( fileName.endswith('_dict.json') ):
        # READ FROM JSON DATA
        with open( fileName ) as GEDCOM_JSON:
            GEDCOM_dict = json.loads( GEDCOM_JSON.read() )

        displayErrors( fileName, GEDCOM_dict )

        return
    # Case when the Program is run from 0_main.py
    else:
        if ( len(GEDCOM_dict) != 0 ):
            displayErrors( fileName, GEDCOM_dict )

            return
        else:
            raise 'Empty Input, no GEDCOM Dictionary Data to Display'

    return

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


from typing import List
class Solution:
    def productExceptSelf(self, nums: List[int]) -> List[int]:
        product = 1
        res = [1] * len(nums)
        for i in range(1, len(nums)):
            product *= nums[i-1]
            res[i] *= product

        product = 1
        for j in range(len(nums)-2, -1, -1):
            product *= nums[j+1]
            res[j] *= product
        return res


from google.cloud import firestore

db = firestore.Client()

class Cinema(object):
    def __init__(self):
        pass

    def post_book_cinema(self, date, time, seat):
        ref = db.collection(u'cinema').document(f'{date}T{time}')
        if ref.get().exists:
            ref.update({
                        "bookedSeats": firestore.ArrayUnion([seat])
            })
        else:
            ref.set({
                        "bookedSeats": firestore.ArrayUnion([seat])
            })

    def get_book_cinema(self, date, time):
        ref = db.collection(u'cinema').document(f'{date}T{time}').get()
        if ref.exists:
            r = ref.to_dict()
            return r['bookedSeats']
        return []

    def get_book_date(self, date):
        retVal = []
        for time in ["19:30", "21:00", "22:30"]:
            ref = db.collection(u'cinema').document(f'{date}T{time}').get()
            if ref.exists:
                r = ref.to_dict()
                r['time'] = time
                retVal.append(r)
        return retVal


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

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html
from jandan import settings
import os
import urllib
import scrapy

class JandanPipeline(object):
    def process_item(self, item, spider):
        dir_path = "/home/tao/scrapyspider/jandan/download"

        file_name = str(item['score']) + "_" + item['url'][-8:-4] + item['url'][-4:]
        file_path = '%s/%s' % (dir_path,file_name)

        with open(file_path,'wb') as file_writer:
            conn = urllib.urlopen("http://"+item['url'])#下载图片
            file_writer.write(conn.read())
        file_writer.close()
        return item


from schemas import UserModel
from sqlalchemy.orm import Session

from db.models import User
from libgravatar import Gravatar

async def get_user_by_email(email: str, db: Session) -> User:
    """
    Gets user by email address.

    :param email: Email address.
    :type email: str
    :param db: Database session.
    :type db: Session
    :return: User.
    :rtype: User
    """
    return db.query(User).filter(User.email == email).first()

async def create_user(body: UserModel, db: Session) -> User:
    """
    Creates new user.

    :param body: Data for contact.
    :type body: UserModel
    :param db: Database session.
    :type db: Session
    :return: Created user.
    :rtype: User
    """
    avatar = None
    try:
        g = Gravatar(body.email)
        avatar = g.get_image()
    except Exception as e:
        print(e)

    new_user = User(**body.model_dump(), avatar=avatar)
    db.add(new_user)
    db.commit()
    db.refresh(new_user)
    return new_user

async def update_token(user: User, token: str | None, db: Session) -> None:
    """
    Updates refresh token for specific user.

    :param user: User for which token will update.
    :type user: User
    :param token: Refresh token.
    :type token: str | None
    :param db: Database session.
    :type db: Session
    :return: None.
    :rtype: None
    """
    user.refresh_token = token
    db.commit()

async def confirmed_email(email: str, db: Session) -> None:
    """
    Confirms email for specific user.

    :param email: Email address.
    :type email: str
    :param db: Database session.
    :type db: Session
    :return: None.
    :rtype: None
    """
    user = await get_user_by_email(email, db)
    user.confirmed = True
    db.commit()

async def update_avatar(email: str, url: str, db: Session) -> User:
    """
    Updates avatar for specific user.

    :param email: Email address.
    :type email: str
    :param url: Cloudinary URL address.
    :type url: str
    :param db: Database session.
    :type db: Session
    :return: Updated user.
    :rtype: User
    """
    user = await get_user_by_email(email, db)
    user.avatar = url
    db.commit()
    return user


from typing import List

def insertionSort(array: List[int]) -> None:
    """Main insertion sort algorithm\n
    Sorts array in-place; returns None
    """
    for i in range(1, len(array)):  # Iterate through entire array
        comparator = array[i]  # Make comparison with this value
        for section in range(i - 1, -2, -1):  # Iterate through array from i backwards
            if (
                comparator > array[section]
            ):  # Insert comparator into the array, in its correct position
                array[section + 1] = comparator
                break
            else:
                array[section + 1] = array[
                    section
                ]  # If comparator <= array[section], move array[section] forward to make space
        else:
            array[
                section + 1
            ] = comparator  # If loop wasn't broken, insert comparator in the right spot

if __name__ == "__main__":
    try:
        from utils import randomSequence
    except ModuleNotFoundError:
        import os, sys  # Import shuffler from parent directory

        dir_path = os.path.dirname(os.path.realpath(__file__))
        parent_dir_path = os.path.abspath(os.path.join(dir_path, os.pardir))
        sys.path.insert(0, parent_dir_path)
        from utils import randomSequence

    print("INSERTION SORT")

    shuffledArray = randomSequence(0, 1000)

    print(shuffledArray, "\n")

    insertionSort(shuffledArray)

    print(shuffledArray)


import requests
from bs4 import BeautifulSoup

### 코드를 작성하세요 ###
page = requests.get("https://workey.codeit.kr/orangebottle/index")
soupedPage = BeautifulSoup(page.text, "html.parser")
branchList = soupedPage.select(".branch")
branch_infos = []
for info in branchList:
    branch_info = info.select("p span")
    branch_name = branch_info[0].get_text()
    address = branch_info[2].get_text()
    phone_number = branch_info[3].get_text()
    # address = info.select(".address")
    # phone_number = info.select(".phoneNum")
    # print(branch_name)
    branch_infos.append([branch_name, address, phone_number])

# 출력 코드
print(branch_infos)


# 2095. Delete the Middle Node of a Linked List
#Medium
#problem statement: https://leetcode.com/problems/delete-the-middle-node-of-a-linked-list/
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def deleteMiddle(self, head: Optional[ListNode]) -> Optional[ListNode]:
        #decide the #
        n = 1
        cur = head
        while cur.next != None:
            cur = cur.next
            n += 1

        mid = 0
        if n%2 == 1:
            mid = floor(n/2)
        else:
            mid = n/2

        print(mid)
        mid = int(mid)
        de = head

        def deleteNext(node):
            the = node.next
            afterThe = node.next.next
            the.val = None
            node.next = afterThe

        for i in range(mid + 1):
            if i == mid-1:
                deleteNext(de)
                return head
            de = de.next

        '''
        #decide the #
        n = len(head)
        mid = 0
        if n%2 == 1:
            mid = floor(n/2)
        else:
            mid = n/2

        res = head.copy()
        res.pop(mid)
        return res
        '''

leetcode75/2095. Delete the Middle Node of a Linked List.py


import numpy as np
import matplotlib.pyplot as plt
from deap import base, tools, creator, algorithms
import random
from copy import deepcopy

f_read = open("C101.txt")
i = 0
returnMat = np.zeros((101,7),dtype = int)

for line in  f_read.readlines():
    i = i + 1
    demo = line.split()
    if i == 5:
        car_number = int(demo[0])
        car_Q = int(demo[1])
    if i == 11:
        servetime = int(demo[6])
    if i > 9 and i < 111:
        for j in range(7):
            returnMat[i-10][j] = int(demo[j])

#字典保存参数信息
dataDict = {}
#坐标信息
dataDict['NodeCoor'] = []
#需求信息
dataDict['Demand'] = []
#时间窗口
dataDict['Timewindow'] = []
#装载量
dataDict['MaxLoad'] = car_Q
#服务时间
dataDict['ServiceTime'] = servetime

for i in range(101):
    dataDict['NodeCoor'].append((returnMat[i][1],returnMat[i][2]))
    dataDict['Timewindow'].append((returnMat[i][4],returnMat[i][5]))
    dataDict['Demand'].append(returnMat[i][3])

#print(dataDict)

def genInd(dataDict = dataDict):
    '''生成个体'''
    nCustomer = len(dataDict['NodeCoor']) - 1 # 顾客数量
    perm = np.random.permutation(nCustomer) + 1 # 生成顾客的随机排列,注意顾客编号为1--n
    pointer = 0 # 迭代指针
    lowPointer = 0 # 指针指向下界
    permSlice = []
    # 当指针不指向序列末尾时
    while pointer < nCustomer -1:
        vehicleLoad = 0
        # 当不超载时，继续装载

        while (vehicleLoad < dataDict['MaxLoad']) and (pointer < nCustomer -1):
            vehicleLoad += dataDict['Demand'][perm[pointer]]
            pointer += 1
        if lowPointer+1 < pointer:
            tempPointer = np.random.randint(lowPointer+1, pointer)
            permSlice.append(perm[lowPointer:tempPointer].tolist())
            lowPointer = tempPointer
            pointer = tempPointer
        else:
            permSlice.append(perm[lowPointer::].tolist())
            break
    # 将路线片段合并为染色体
    ind = [0]
    for eachRoute in permSlice:
        ind = ind + eachRoute + [0]
    return ind

#-----------------------------------
## 评价函数
# 染色体解码
def decodeInd(ind):
    '''从染色体解码回路线片段，每条路径都是以0为开头与结尾'''
    indCopy = np.array(deepcopy(ind)) # 复制ind，防止直接对染色体进行改动
    idxList = list(range(len(indCopy)))
    zeroIdx = np.asarray(idxList)[indCopy == 0]
    routes = []
    for i,j in zip(zeroIdx[0::], zeroIdx[1::]):
        routes.append(ind[i:j]+[0])
    return routes

def calDist(pos1, pos2):
    '''计算距离的辅助函数，根据给出的坐标pos1和pos2，返回两点之间的距离
    输入： pos1, pos2 -- (x,y)元组
    输出： 欧几里得距离'''
    return np.sqrt((pos1[0] - pos2[0])*(pos1[0] - pos2[0]) + (pos1[1] - pos2[1])*(pos1[1] - pos2[1]))

ind = genInd(dataDict)
print(decodeInd(ind))


#coding UTF-8
# This script will decode a qencoded message.
import sys
d = ""
while sys.stdin:
    # Reset 16 byte buffer
    e = ""
    # Gather 16 hexadecimal bytes.
    while len(e) < 16:
        # Read a byte from input.
        b = sys.stdin.read(1)
        # Terminate when stdin exhausted.
        if not b:
            # Get gathered bytes and decode from hex
            sys.stdout.write(d.decode('hex'))
            exit()
        # Get meaningful characters.
        elif b == "q" or b == "Q":
            # Add to buffer
            e = e + b
        # Strip other characters.
        else:
            continue
    else:
        # Count the number of upper case Q's in buffer
        c = e.count('Q')
        # Convert to a single hex digit
        a = hex(c)
        # Take two hex digits and combine to form a byte
        d = d + a[2]
        continue


from db_utils.models import User, Dish, Restaurant, Order, Segment, UserPreference
from db_utils.session_factory import SessionFactory

session_fact = SessionFactory()

with session_fact.session_scope() as session:
    user = User(name="Piyush", gender="male")
    session.add(user)
    session.flush()

    dish = Dish(name="red-meat", is_veg=False)
    session.add(dish)

    restaurant = Restaurant(name="Restaurant1")
    restaurant.dishes.append(dish)
    session.add(restaurant)

    order = Order(user_id=user.id, restaurant_id=restaurant.id)
    session.add(order)

    user_pref = UserPreference(user_id=user.id, rest=restaurant.name, food=dish.name)
    session.add(user_pref)

    # segment 1
    config = {
        "gender": {
            "operator": "neq",
            "value": "female"
        }
    }
    segment = Segment(name="segment1", config=config)
    session.add(segment)

    # segment 2
    config = {
        "gender": {
            "operator": "neq",
            "value": "female"
        },
        "or": [
            {"preference.food": {
                "operator": "eq",
                "value": "red-meat"
            }},
            {"order_count": {
                "operator": "gt",
                "value": 1
            }}
        ]
    }
    segment = Segment(name="segment2", config=config)
    session.add(segment)

    # segment 3
    config = {
        "gender": {
            "operator": "neq",
            "value": "female"
        },
        "and": [
            {"preference.food": {
                "operator": "eq",
                "value": "red-meat"
            }},
            {"order_count": {
                "operator": "gte",
                "value": 1
            }}
        ]
    }
    segment = Segment(name="segment3", config=config)
    session.add(segment)

    # segment 4
    config = {
        "gender": {
            "operator": "neq",
            "value": "female"
        },
        "and": [
            {"preference.food": {
                "operator": "eq",
                "value": "red-meat"
            }},
            {"order_count": {
                "operator": "gt",
                "value": 1
            }}
        ]
    }
    segment = Segment(name="segment4", config=config)
    session.add(segment)


def main():

    #inserir numero maior que zero
    num = 0
    while num <=0:
        num = int(input("Enter number: "))

    ePrimo = True #flag

#verificar se numero é primo
    for x in range(2, num):
        if num % x == 0:
            ePrimo = False
            break
#mostrar resultado
    if ePrimo == True: #<=> if ePrimo:
        print ("Number %d is cousin "%(num))
    else:
        print ("Number %d is not cousin" %(num))

#start program
main()


import machine
from machine import Pin, Timer
import time

led=Pin(2, Pin.OUT)

def toggleLED(timer):
    led.value(not led.value())

tim4 = Timer(4)
tim4.init(period=300, mode=machine.Timer.PERIODIC, callback=toggleLED)
time.sleep(20)
tim4.deinit()


import pandas as pd
import csv
import os
import sys
import torch
import shutil
import pickle
import numpy as np
from skimage.measure import compare_ssim, compare_psnr

def load_state_dict(model, fname):
    """
    Set parameters converted from Caffe models authors of VGGFace2 provide.
    See https://www.robots.ox.ac.uk/~vgg/data/vgg_face2/.

    Arguments:
        model: model
        fname: file name of parameters converted from a Caffe model, assuming the file format is Pickle.
    """
    with open(fname, 'rb') as f:
        weights = pickle.load(f, encoding='latin1')

    own_state = model.state_dict()
    for name, param in weights.items():
        if name in own_state:
            try:
                own_state[name].copy_(torch.from_numpy(param))
            except Exception:
                raise RuntimeError('While copying the parameter named {}, whose dimensions in the model are {} and whose '\
                                   'dimensions in the checkpoint are {}.'.format(name, own_state[name].size(), param.size()))
        else:
            raise KeyError('unexpected key "{}" in state_dict'.format(name))

class AverageMeter(object):
    """Computes and stores the average and current value"""
    def __init__(self):
        self.reset()

    def reset(self):
        self.val = 0
        self.avg = 0
        self.sum = 0
        self.count = 0

    def update(self, val, n=1):
        self.val = val
        self.sum += val * n
        self.count += n
        self.avg = self.sum / self.count

def get_psnr(im1, im2):
    return compare_psnr(im1, im2, data_range=255)

def get_ssim(im1, im2):
    return compare_ssim(im1, im2, data_range=255, gaussian_weights=True, use_sample_covariance=False, multichannel=True)


import re, os
from bs4 import BeautifulSoup
import time
start_time = time.time()

def links(start, path):
    """Функция возвращает все ссылки из папки с путем path, родителем которых является start."""
    with open("./wiki/" + start) as file:
        link_re = set(re.findall(r'(?<=/wiki/)[\w()]+', file.read()))
        set_links = link_re & set(os.listdir(path)) - {start}
        return set_links

def bfs(graph, start, end, path):
    """Функция выполняет обход графа graph в ширину от вершины start до вершины end.
        Граф представляется в виде списка смежности (словарь множеств, в частности)."""
    fired = set(start)
    connection = dict()
    order = [start]
    while order:
        way = order.pop(0)
        for neighbour in graph[way]:
            if neighbour not in fired:
                fired.add(neighbour)
                order.append(neighbour)
                connection[neighbour] = way
            if end in links(neighbour, path):
                connection[end] = neighbour
                return connection
            graph[neighbour] = links(neighbour, path)

def surprise(start, end, lst, path):
    """Функция возвращает кратчайший путь от start до end в виде списка"""
    parents = bfs({start: links(start, path)}, start, end, path)
    while end != start:
        lst.append(end)
        end = parents[end]
        if end == start:
            lst.append(start)
    return lst[::-1]

def parse(start, end, path):
    """Функция соберает статистику по нескольким страницам из wikipedia и возвращает её в виде словаря.
        Объектами сататистических данных являются содержание на странице картинок определённой ширины,
        количество заголовков, первая буква текста внутри которых соответствует заглавной букве E, T или C,
        длина максимальной последовательности ссылок, между которыми нет других тегов, открывающихся или закрывающихся,
        количество списков (ul, ol), не вложенных в другие списки."""
    out = {}
    for file in surprise(start, end, [], path):
        out[file] = [0, 0, 0, 0]
        with open(path + file) as data:
            soup = BeautifulSoup(data, "lxml")
        body = soup.find(id="bodyContent")
        for item in body.find_all('img', width=True):
            width = item['width']
            if int(width) >= 200:
                out[file][0] += 1

        for i in body.select('h1, h2, h3, h4, h5, h6'):
            if i.text[0] in 'ETC':
                out[file][1] += 1

        len_links = 0
        for _ in body.find_all('a'):
            brothers = body.a.find_next_siblings()
            other_len = 1
            for link in brothers:
                if link.name == 'a':
                    other_len += 1
                elif link.name != 'a':
                    break
            if len_links < other_len:
                len_links = other_len
            body.a = body.a.find_next("a")
        out[file][2] = len_links

        for item in body.select('ol, ul'):
            not_in = [i.name for i in item.parents]
            if 'ul' and 'li' not in not_in:
                out[file][3] += 1
    return out

print(parse( 'Stone_Age', 'Python_(programming_language)', './wiki/'))

print(time.time() - start_time)


import logging as lg
from datetime import datetime
from threading import RLock

from .worker import WorkerState
from .worker_monitor import WorkerMonitor

_logger = lg.getLogger(__name__)

class WorkerManager(object):
    """Monitor and Maintain workers states.

    The WorkerManager monitor the state of workers and maintain the list of active/dead workers.
    It is not really part of the execution pipeline, its role is just to keep tack of which
    machines are active and fetch their state.
    """  # NOQA

    def __init__(self, workers_factory):
        self.worker_factory = workers_factory
        self.workers = []

        self.lock = RLock()

        # monitor
        self.monitor = WorkerMonitor(self)

    def start(self):
        _logger.info("Stating tanit worker manager.")
        self.monitor.start()
        _logger.info("Tanit worker manager started.")

    def stop(self):
        _logger.info("Stopping tanit worker manager.")
        self.monitor.stop()
        with self.lock:
            for wkr in self.list_workers():
                wkr.stop()
        _logger.info("Tanit worker manager stopped.")

    def disable_monitor(self):
        self.monitor.heartbeat_check_interval = -1

    def get_worker(self, wid):
        with self.lock:
            for wkr in self.workers:
                if wkr.wid == wid:
                    return wkr
            return None

    def list_workers(self, state=None):
        with self.lock:
            if state is None:
                return self.workers
            else:
                return [wkr for wkr in self.list_workers() if wkr.state == state]

    def list_active_workers(self):
        return self.list_workers(state=WorkerState.ACTIVE)

    def register_worker(self, worker):
        """Register a Worker.

        Register the worker in the worker manager.
        Note: This does not activate the worker.
        """
        _logger.info("Registering new Worker [ %s ].", worker.wid)
        with self.lock:
            remote_worker = self.get_worker(worker.wid)
            if remote_worker is not None:
                if remote_worker.wid == worker.wid:
                    _logger.warning("Worker [ %s ] is already registered.", worker.wid)
                elif (
                    remote_worker.address == worker.address
                    and remote_worker.port == worker.port
                    and worker.address is not None
                    and worker.port is not None
                ):
                    _logger.warning(
                        "Worker running on address [ %s ] and port [ %s ] "
                        + "is already registered.",
                        worker.address,
                        worker.port,
                    )
                elif remote_worker.address == worker.address:
                    _logger.warning(
                        "Another Worker [ %s ] is already running on [ %s ].",
                        worker.wid,
                        worker.address,
                    )
            else:
                remote_worker = self.worker_factory.create_worker(worker)
                remote_worker.start()
                self.workers.append(remote_worker)
        _logger.info("Worker [ %s ] registered.", worker.wid)

    def activate_worker(self, wid):
        """Activate a Worker.

        Transition worker to state `ACTIVE`.
        Worker in any state except `DEACTIVATING` can be activated
        """
        _logger.info("Activating Worker [ %s ]", wid)
        with self.lock:
            remote_worker = self.get_worker(wid)
            if remote_worker is not None:
                if remote_worker.state == WorkerState.DEACTIVATING:
                    _logger.error("Cannot activate worker in decommissioning state.")
                    raise IllegalWorkerStateException(
                        "Cannot activate worker in decommissioning state."
                    )
                elif remote_worker.state == WorkerState.DEAD:
                    _logger.warning(
                        "Worker %s is in %s state, forcing activation"
                        % (
                            remote_worker.wid,
                            WorkerState._VALUES_TO_NAMES[WorkerState.DEAD],
                        )
                    )
                _logger.info(
                    "Transitioning worker from state %s to %s"
                    % (
                        WorkerState._VALUES_TO_NAMES[remote_worker.state],
                        WorkerState._VALUES_TO_NAMES[WorkerState.ACTIVE],
                    )
                )
                remote_worker.state = WorkerState.ACTIVE
            else:
                raise NoSuchWorkerException("No such worker [ %s ]", wid)
        _logger.info("Worker [ %s ] Activated", wid)

    def deactivate_worker(self, wid):
        """Deactivate a Worker.

        Transition worker to state `DEACTIVATING`.
        This does not really decommission the worker but rather schedule it
        for decommissioning by the `WorkerDecommissioner`.
        Only workers in state `ACTIVE` transition to `DEACTIVATING` state.
        """
        _logger.info("Deactivating Worker [ %s ]", wid)
        with self.lock:
            remote_worker = self.get_worker(wid)
            if remote_worker is not None:
                if remote_worker.state == WorkerState.ACTIVE:
                    remote_worker.state = WorkerState.DEACTIVATING
                elif remote_worker.state in [
                    WorkerState.DEACTIVATING,
                    WorkerState.DEACTIVATED,
                ]:
                    _logger.info("Worker [ %s ] already decommissioned.", wid)
                elif remote_worker.state == WorkerState.ALIVE:
                    _logger.info(
                        "Worker %s in state %s , forcing state %s"
                        % (
                            remote_worker.wid,
                            WorkerState._VALUES_TO_NAMES[WorkerState.ALIVE],
                            WorkerState._VALUES_TO_NAMES[WorkerState.DEACTIVATED],
                        )
                    )
                    remote_worker.state = WorkerState.DEACTIVATED
                else:
                    _logger.error(
                        "Cannot transition worker %s from state %s to %s"
                        % (
                            remote_worker.wid,
                            WorkerState._VALUES_TO_NAMES[remote_worker.state],
                            WorkerState._VALUES_TO_NAMES[WorkerState.DEACTIVATING],
                        )
                    )
                    raise IllegalWorkerStateException(
                        "Cannot transition worker %s from state %s to %s"
                        % (
                            remote_worker.wid,
                            WorkerState._VALUES_TO_NAMES[remote_worker.state],
                            WorkerState._VALUES_TO_NAMES[WorkerState.DEACTIVATING],
                        )
                    )
            else:
                raise NoSuchWorkerException("No such worker [ %s ]", wid)
        _logger.info("Worker [ %s ] deactivated", wid)

    def register_heartbeat(self, worker):
        with self.lock:
            remote_worker = self.get_worker(worker.wid)
            if remote_worker is not None:
                remote_worker.last_hear_beat = datetime.now()
                if remote_worker.state == WorkerState.DEAD:
                    remote_worker.state = WorkerState.ALIVE
            else:
                # register the worker without activating
                _logger.info(
                    "Received heartbeat from unknown worker %s, registering it."
                    % worker.wid
                )
                self.register_worker(worker)

class IllegalWorkerStateException(Exception):
    pass

class NoSuchWorkerException(Exception):
    pass

class AlreadyRegisteredWorkerException(Exception):
    pass