#!/usr/bin/env /Library/Frameworks/Python.framework/Versions/3.6/bin/python3

from ......API.JSONX.JSONX import JSONX
from .....PaloAltoNetworks import PaloAltoNetworks
from ......Logger.PoliciesLogger.PoliciesLogger import PoliciesLogger as Plogger

class Security(PaloAltoNetworks):
	"""
	Class to configure Security Rule under Device Group
	"""

	def __init__(self, panorama_ip, api_key):
		# print('++++ Security Class')
		super().__init__(panorama_ip, api_key)

		self.security_config = (
			'https://%s/restapi/9.0/Policies/Security%sRules?location=device-group&device-group=%s&name=%s'
		)

		self.security_move = (
			'https://%s/restapi/9.0/Policies/Security%sRules?action=move&location=device-group&device-group=%s'
			'&name=%s&where=%s'
		)
		# print('---- Security Class')

	def __str__(self):
		return self.__class__.__name__

	def configure_security_rule(
		self, action, position, rule_name, device_group, source_zone='any', source_address='any', source_user='any',
		hip_profile='any', destination_zone='any', destination_address='any', application='any', service='any',
		url_category='any', rule_action='allow', virus=None, vulnerability=None, spyware=None,
		url_filtering=None, file_blocking=None, data_filtering='', wildfire=None,
		log_start='', log_forwarding=''
	):
		"""
		Method to add security rule under to a Device Group in Panorama

		:param action: Action - add or update
		:type action: str
		:param position: Pre or Post - position of security rule
		:type position: str
		:param rule_name: Security rule name
		:type rule_name: str
		:param device_group: Device group name
		:type device_group: str
		:param source_zone: Source Zone
		:type source_zone: list
		:param source_address: Source Address
		:type source_address: list
		:param source_user: Source User
		:type source_user: list
		:param hip_profile: HIP Profile
		:type hip_profile: list
		:param destination_zone: Destination Zone
		:type destination_zone: list
		:param destination_address: Destination Address
		:type destination_address: list
		:param application: Layer 7 Application
		:type application: list
		:param service: TCP/UDP Service
		:type service: list
		:param url_category: URL category
		:type url_category: list
		:param rule_action: Allow or Deny
		:type rule_action: str
		:param virus: Anti-Virus security profile
		:type virus: str
		:param vulnerability: Vulnerability/IPS security profile
		:type vulnerability: str
		:param spyware: Anti-Spyware security profile
		:type spyware: str
		:param url_filtering: URL Filtering security profile
		:type url_filtering: str
		:param file_blocking: File Blocking security profile
		:type file_blocking: str
		:param data_filtering: File Blocking security profile
		:type data_filtering: str
		:param wildfire: Wildfire security profile
		:type wildfire: str
		:param log_start: Log at start of the session
		:type log_start: str
		:param log_forwarding: Log forward profile name
		:type log_forwarding: str
		:return: None
		:rtype: None

		Example:

		configure_security_rule(
		action='add', position='Pre', rule_name='rule1', device_group='PANW', source_zone=['trust'],
		source_address=['10.10.10.0/24', '172.16.10.0/24'], source_user=['any'], hip_profile=['any'],
		destination_zone=['untrust'], destination_address=['1.1.1.0/24', '2.2.2.0/24'],
		application=['facebook', 'ssl'], service=['application-default'], url_category=['any'], rule_action='allow',
		data_filtering='something'
		)
		"""

		header = dict()
		header['X-PAN-KEY'] = self.api_key

		jsx = JSONX()

		if action == 'add':
			try:
				security_rule = dict()

				security_rule['@name'] = rule_name
				security_rule['@location'] = "device-group"
				security_rule['@device-group'] = device_group

				security_rule['from'] = {}
				security_rule['from']['member'] = []

				for zone in source_zone:
					security_rule['from']['member'].append(zone)

				security_rule['source'] = {}
				security_rule['source']['member'] = []

				for address in source_address:
					security_rule['source']['member'].append(address)

				security_rule['source-user'] = {}
				security_rule['source-user']['member'] = []

				for user in source_user:
					security_rule['source-user']['member'].append(user)

				security_rule['hip-profiles'] = {}
				security_rule['hip-profiles']['member'] = []

				for hip in hip_profile:
					security_rule['hip-profiles']['member'].append(hip)

				security_rule['to'] = {}
				security_rule['to']['member'] = []

				for zone in destination_zone:
					security_rule['to']['member'].append(zone)

				security_rule['destination'] = {}
				security_rule['destination']['member'] = []

				for address in destination_address:
					security_rule['destination']['member'].append(address)

				security_rule['application'] = {}
				security_rule['application']['member'] = []

				for app in application:
					security_rule['application']['member'].append(app)

				security_rule['service'] = {}
				security_rule['service']['member'] = []

				for serv in service:
					security_rule['service']['member'].append(serv)

				security_rule['category'] = {}
				security_rule['category']['member'] = []

				for cat in url_category:
					security_rule['category']['member'].append(cat)

				security_rule['action'] = rule_action

				security_rule['profile-setting'] = {}
				security_rule['profile-setting']['profiles'] = {}

				if virus:
					security_rule['profile-setting']['profiles']['virus'] = {}
					security_rule['profile-setting']['profiles']['virus']['member'] = []
					security_rule['profile-setting']['profiles']['virus']['member'].append(virus)

				if vulnerability:
					security_rule['profile-setting']['profiles']['vulnerability'] = {}
					security_rule['profile-setting']['profiles']['vulnerability']['member'] = []
					security_rule['profile-setting']['profiles']['vulnerability']['member'].append(vulnerability)

				if spyware:
					security_rule['profile-setting']['profiles']['spyware'] = {}
					security_rule['profile-setting']['profiles']['spyware']['member'] = []
					security_rule['profile-setting']['profiles']['spyware']['member'].append(spyware)

				if url_filtering:
					security_rule['profile-setting']['profiles']['url-filtering'] = {}
					security_rule['profile-setting']['profiles']['url-filtering']['member'] = []
					security_rule['profile-setting']['profiles']['url-filtering']['member'].append(url_filtering)

				if file_blocking:
					security_rule['profile-setting']['profiles']['file-blocking'] = {}
					security_rule['profile-setting']['profiles']['file-blocking']['member'] = []
					security_rule['profile-setting']['profiles']['file-blocking']['member'].append(file_blocking)

				if wildfire:
					security_rule['profile-setting']['profiles']['wildfire-analysis'] = {}
					security_rule['profile-setting']['profiles']['wildfire-analysis']['member'] = []
					security_rule['profile-setting']['profiles']['wildfire-analysis']['member'].append(wildfire)
			except KeyError as e:
				Plogger.policies.info('{}: {}'.format('Security Rule - Dict KeyError', e))
			except ValueError as e:
				Plogger.policies.info('{}: {}'.format('Security Rule - Dict ValueError', e))
			except Exception as e:
				Plogger.policies.info('{}: {}'.format('Security Rule - Dict Generic Error', e))
			else:
				if data_filtering:
					security_rule['profile-setting']['profiles']['data-filtering'] = {}
					security_rule['profile-setting']['profiles']['data-filtering']['member'] = []
					security_rule['profile-setting']['profiles']['data-filtering']['member'].append(data_filtering)

				if log_start == 'yes':
					security_rule['log-start'] = 'yes'

				if log_forwarding:
					security_rule['log-setting'] = log_forwarding

				entry = dict()
				entry['entry'] = []
				entry['entry'].append(security_rule)

				uri = self.security_config % (self.panorama_ip, position, device_group, rule_name)

				jsx.exec_json_post(uri, header, Plogger.policies, 'Security-Rule', rule_name, entry)

	def move_security_rule(self, position, device_group, rule_name, where):
		"""
		Method to move Panorama security rule under a specific device group

		:param position: Pre or Post - position of security rule
		:type position: str
		:param device_group: Device group name
		:type device_group: str
		:param rule_name: Security rule name
		:type rule_name: str
		:param where: top or bottom; position where security rule needs to be moved to
		:type where: str
		:return: None
		:rtype: None
		"""

		header = dict()
		header['X-PAN-KEY'] = self.api_key

		jsx = JSONX()

		uri = self.security_move % (self.panorama_ip, position, device_group, rule_name, where)

		jsx.exec_json_post(
			uri, header, Plogger.policies, 'Security-Rule', rule_name,
			smsg='Successfully moved to {}'.format(where),
			fmsg='Failed to move to {}'.format(where)
		)

	# @staticmethod
	# def __xcode():
	# 	"""
	# 	Method to return Panorama security rule xpath and element to place in API request string
	# 	:return: xpath and element (XML code)
	# 	"""
	#
	# 	xpath = (
	# 		"/config/devices/entry[@name='localhost.localdomain']/device-group/entry[@name='%s"
	# 		"']/pre-rulebase/security/rules"
	# 	)
	#
	# 	element = """
	# 				<entry name="%s">
	# 					<profile-setting>
	# 						<profiles>
	# 							<url-filtering>
	# 							<member>%s</member>
	# 							</url-filtering>
	# 							<file-blocking>
	# 							<member>%s</member>
	# 							</file-blocking>
	# 							<virus>
	# 							<member>%s</member>
	# 							</virus>
	# 							<spyware>
	# 							<member>%s</member>
	# 							</spyware>
	# 							<vulnerability>
	# 							<member>%s</member>
	# 							</vulnerability>
	# 							<wildfire-analysis>
	# 							<member>%s</member>
	# 							</wildfire-analysis>
	# 						</profiles>
	# 					</profile-setting>
	# 					<to>
	# 						<member>%s</member>
	# 					</to>
	# 					<from>
	# 						<member>%s</member>
	# 					</from>
	# 					<source>
	# 						%s
	# 					</source>
	# 					<destination>
	# 						%s
	# 					</destination>
	# 					<source-user>
	# 						<member>any</member>
	# 					</source-user>
	# 					<category>
	# 						<member>any</member>
	# 					</category>
	# 					<application>
	# 						<member>%s</member>
	# 					</application>
	# 					<service>
	# 						<member>%s</member>
	# 					</service>
	# 					<hip-profiles>
	# 						<member>any</member>
	# 					</hip-profiles>
	# 					<action>%s</action>
	# 					<log-setting>%s</log-setting>
	# 				</entry>
	# 				"""
	#
	# 	return xpath, re.sub(r'\s\s+|\n', '', element)
	#
	# @staticmethod
	# def __move_xcode():
	# 	"""
	# 	Method to return Panorama xpath to place in API request string to move security rule
	# 	:return: xpath
	# 	"""
	#
	# 	xpath = (
	# 		"/config/devices/entry[@name='localhost.localdomain']/device-group/entry[@name='%s"
	# 		"']/pre-rulebase/security/rules/entry[@name='%s']"
	# 	)
	#
	# 	return xpath
	#
	# def xadd_security_rule(
	# 	self, ip, key, dg, rule_name, szone, sadd, dzone, dadd, app, service, action, log, url_filtering='default',
	# 	file_blocking='basic file blocking', virus='default', spyware='default', vulnerability='default',
	# 	wildfire='default'
	# ):
	# 	"""
	# 	Method to configure security rule under a specific device group
	#
	# 	:param ip: Panorama ip
	# 	:param key: API key
	# 	:param dg: Device group name
	# 	:param rule_name: Security rule name
	# 	:param szone: Source Zone
	# 	:param sadd: Source Address
	# 	:param dzone: Destination Zone
	# 	:param dadd: Destination Address
	# 	:param app: Layer 7 Application
	# 	:param service: TCP/UDP Service
	# 	:param action: Allow or Deny
	# 	:param log: Log location
	# 	:param url_filtering: URL Filtering security profile
	# 	:param file_blocking: File Blocking security profile
	# 	:param virus: Anti-Virus security profile
	# 	:param spyware: Anti-Spyware security profile
	# 	:param vulnerability: Vulnerability/IPS security profile
	# 	:param wildfire: Wildfire security profile
	# 	:return: Not planning to return success or failure as of now; may change in future
	# 	"""
	#
	# 	xpath, element = self.__xcode()
	#
	# 	response = requests.get(self.configure.format(
	# 		ip,
	# 		xpath % dg,
	# 		element % (
	# 			rule_name, url_filtering, file_blocking, virus, spyware, vulnerability, wildfire, dzone, szone,
	# 			sadd, dadd, app, service, action, log
	# 		),
	# 		key), verify=False)
	#
	# 	self.get_api_status(response, rule_name, '{}: Security-Rule'.format(dg), self.logger)
	#
	# def move_security_rule_to_top(self, ip, key, dg, rule_name):
	# 	"""
	# 	Method to move security rule to top under a specific device group
	#
	# 	:param ip: Panorama ip
	# 	:param key: API key
	# 	:param dg: Device group name
	# 	:param rule_name: Security rule name
	# 	:return: Not planning to return success or failure as of now; may change in future
	# 	"""
	#
	# 	xpath = self.__move_xcode()
	#
	# 	response = requests.get(self.tmove.format(
	# 		ip,
	# 		xpath % (dg, rule_name),
	# 		key
	# 	))
	#
	# 	smsg = 'Successfully moved to top'
	# 	fmsg = 'Failed to move to top'
	#
	# 	self.get_api_status(response, rule_name, '{}: Security-Rule'.format(dg), self.logger, smsg, fmsg)

		# jcode = self.__jcode()

	# @staticmethod
	# def __jcode():
	# 	"""
	# 	Method to return Panorama Security Rule Json Code
	# 	:return: Json code
	# 	:rtype: dict
	# 	"""
	#
	# 	code = {
	# 			"@name": "",
	# 			"@location": "device-group",
	# 			"@device-group": "",
	# 			"from": {
	# 				"member": []
	# 			},
	# 			"source": {
	# 				"member": []
	# 			},
	# 			"source-user": {
	# 				"member": []
	# 			},
	# 			"hip-profiles": {
	# 				"member": []
	# 			},
	# 			"to": {
	# 				"member": []
	# 			},
	# 			"destination": {
	# 				"member": []
	# 			},
	# 			"application": {
	# 				"member": []
	# 			},
	# 			"service": {
	# 				"member": []
	# 			},
	# 			"category": {
	# 				"member": []
	# 			},
	# 			"action": "",
	# 			'profile-setting': {
	# 				'profiles': {
	# 					'virus': {
	# 						'member': []
	# 					},
	# 					'vulnerability': {
	# 						'member': []
	# 					},
	# 					'spyware': {
	# 						'member': []
	# 					},
	# 					'url-filtering': {
	# 						'member': []
	# 					},
	# 					'file-blocking': {
	# 						'member': []
	# 					},
	# 					'wildfire-analysis': {
	# 						'member': []
	# 					}
	# 				}
	# 			},
	# 		}
	#
	# 	return code

# jcode['@name'] = rule_name
# jcode['@device-group'] = device_group
# jcode['from']['member'].append(source_zone)
# jcode['source']['member'].append(source_address)
# jcode['source-user']['member'].append(source_user)
# jcode['hip-profiles']['member'].append(hip_profile)
# jcode['to']['member'].append(destination_zone)
# jcode['destination']['member'].append(destination_address)
# jcode['application']['member'].append(application)
# jcode['service']['member'].append(service)
# jcode['category']['member'].append(url_category)
# jcode['action'] = rule_action
# jcode['profile-setting']['profiles']['virus']['member'].append(virus)
# jcode['profile-setting']['profiles']['vulnerability']['member'].append(vulnerability)
# jcode['profile-setting']['profiles']['spyware']['member'].append(spyware)
# jcode['profile-setting']['profiles']['url-filtering']['member'].append(url_filtering)
# jcode['profile-setting']['profiles']['file-blocking']['member'].append(file_blocking)
# jcode['profile-setting']['profiles']['wildfire-analysis']['member'].append(wildfire)

# jcode['profile-setting']['profiles']['data-filtering'] = {'member': []}
# jcode['profile-setting']['profiles']['data-filtering']['member'].append(data_filtering)

# jcode['log-start'] = 'yes'
# jcode['log-setting'] = log_forwarding


import pandas as pd

df_incidencias = pd.read_csv("C:/Users/deb/Documents/Tesis/incident-classifier/data/dataset-original.csv")

# filtro por las categorias seleccionadas

df_filtrado = df_incidencias[(df_incidencias['NUMERO_CLASIFICADOR']=='B.25')|(df_incidencias['NUMERO_CLASIFICADOR']=='D.68')]

print(df_filtrado)

ruta_destino = "C:/Users/deb/Documents/Tesis/incident-classifier/data/df_filtrado.csv"
df_filtrado.to_csv(ruta_destino, index=False)


#!/usr/bin/python

from mininet.topo import Topo
from mininet.net import Containernet
from mininet.node import Controller
from mininet.node import Node
from mininet.log import setLogLevel, info
from mininet.cli import CLI
from mininet.link import TCLink

IMAGE_NAME = "flipperthedog/emqx-ip"

class LinuxRouter(Node):
    "A Node with IP forwarding enabled."

    def config(self, **params):
        super(LinuxRouter, self).config(**params)
        # Enable forwarding on the router
        self.cmd('sysctl net.ipv4.ip_forward=1')

    def terminate(self):
        self.cmd('sysctl net.ipv4.ip_forward=0')
        super(LinuxRouter, self).terminate()

def run():
    net = Containernet(controller=Controller)
    net.addController('c0', port=6654)

    info('*** Adding routers\n')
    r1 = net.addHost('r1', cls=LinuxRouter, ip='10.0.0.1/24')
    r2 = net.addHost('r2', cls=LinuxRouter, ip='10.1.0.1/24')

    info('*** Adding switches\n')
    s1, s2 = [net.addSwitch(s) for s in ('s1', 's2')]

    info('*** Adding host-switch links\n')
    net.addLink(s1, r1, intfName2='r1-eth1',
                params2={'ip': '10.0.0.1/24'})

    net.addLink(s2, r2, intfName2='r2-eth1',
                params2={'ip': '10.1.0.1/24'})

    info('*** Adding switch-switch link\n')
    net.addLink(r1, r2, intfName1='r1-eth2', intfName2='r2-eth2', params1={'ip': '10.100.0.1/24'},
                params2={'ip': '10.100.0.2/24'})

    info('*** Adding routing\n')
    # r1.cmd("ip route add 10.1.0.0/24 via 10.100.0.1")
    # r2.cmd("ip route add 10.0.0.0/24 via 10.100.0.2")
    r1.cmd("ip route add 10.1.0.0/24 via 10.100.0.2 dev r1-eth2")
    r2.cmd("ip route add 10.0.0.0/24 via 10.100.0.1 dev r2-eth2")

    info('*** Adding hosts\n')
    d1 = net.addHost(name='d1', ip='10.0.0.251/24', defaultRoute='via 10.0.0.1')
    d2 = net.addHost(name='d2', ip='10.1.0.252/24', defaultRoute='via 10.1.0.1')

    info('*** Adding host-switch link\n')
    for d, s in [(d1, s1), (d2, s2)]:
        info(net.addLink(d, s))

    info('*** Starting network\n')
    net.start()
    net.staticArp()

    info('*** Routing Table on Router:\n')
    print((net['r1'].cmd('route')))

    info('*** Routing Table on Router:\n')
    print((net['r2'].cmd('route')))

    info('*** Testing connectivity\n')
    net.pingAll()

    CLI(net)
    net.stop()

if __name__ == '__main__':
    setLogLevel('info')
    run()


import pandas as pd
import json
import urllib3
from datetime import datetime, timedelta
import io
import boto3
import boto3.session
from codeguru_profiler_agent import with_lambda_profiler

class Request:

    def request_surveys(self):
        is_empty = False
        id = ""
        surveys = {'hasMore':False, 'items':[]}
        while not is_empty:
            query = f"https://api.movidesk.com/public/v1/survey/responses?token=&startingAfter={id}"
            http = urllib3.PoolManager()
            response = http.request('GET', query)
            if response.status == 200:

                parcial_survey = json.loads(response.data.decode('utf-8'))
                if parcial_survey["items"] == []:
                    is_empty = True
                else:
                    id = parcial_survey["items"][-1]["id"]
                    surveys["items"].extend(parcial_survey["items"])
        return surveys

class Survey:
    class Item:
        column = ("id", "question_id", "type", "client_id", "ticket_id", "response_data", "commentary", "value")
        def __init__(self, id, questionId, type , clientId, ticketId, responseDate, commentary, value, *args, **kwargs):
            self.id = id
            self.question_id = questionId
            self.type = type
            self.client_id = clientId
            self.ticket_id = ticketId
            self.response_data = responseDate
            self.commentary = commentary
            self.value = value

        def get(self):
            return (self.id, self.question_id, self.type, self.client_id, self.ticket_id, self.response_data, self.commentary, self.value)
        def __str__(self):
            return f"{self.id} {self.question_id} {self.response_data} {self.value}"
        def __repr__(self):
            return self.__str__()

    column = ("has_more")
    def __init__(self, hasMore, items, *args, **kwargs):
        self.has_more = hasMore
        self.items = [self.Item(**item) for item in items if item != [] or item is not None]

    def get(self):
        return [(self.has_more, *item.get()) for item in self.items]
    def __str__(self):
        return f"{self.has_more}"
    def __repr__(self):
        return self.__str__()

def alert_error_sns(client, text, is_failed):
    # Se der erro manda publica mensagem no SNS
    if is_failed:
        response = client.publish(
            TopicArn="arn:aws:sns:us-east-2:686836150544:dados-movidesk-topic",
            Message=text,
            Subject='Erro de atualização',
        )
    else:
        response = client.publish(
            TopicArn="arn:aws:sns:us-east-2:686836150544:dados-movidesk-topic",
            Message=text,
            Subject='Sucesso na atualização.',
        )

@with_lambda_profiler(profiling_group_name="aws-lambda-dados-movidesk")
def lambda_handler(event, context):

    buffer_report = io.BytesIO()
    aws_region = "us-east-2"
    s3 = boto3.client('s3', region_name=aws_region)

    sns = boto3.client('sns',   region_name=aws_region)

    cont_attempts = 0
    while True:
        cont_attempts += 1
        buffer_report.seek(0)
        update_report(s3)
        is_failed = False
        if cont_attempts > 5:
            break

    if is_failed:
        alert_error_sns(sns, f"Erro ao dar load no relatório de surveys do Movidash. Dia de referencia: {datetime.today() - timedelta(days=1)}", is_failed)
    else:
        alert_error_sns(sns, f"Sucesso na atualização do relatório de surveys.. Dia de referencia: {datetime.today() - timedelta(days=1)}", is_failed)
    return 0

def update_report(s3):

    dayli_buffer = io.BytesIO()
    dayli_buffer.write(json.dumps(Request().request_surveys()).encode())
    dayli_buffer.seek(0)
    arquivo = json.load(dayli_buffer)
    dayli_buffer.close()
    surveys = Survey(**arquivo)
    csv_buffer = io.StringIO()
    dataframe = pd.DataFrame([item.get() for item in surveys.items], columns=surveys.items[0].column)
    dataframe = dataframe.query('type == 3')
    pd.DataFrame(dataframe).to_csv(csv_buffer, index=False, header=dataframe.columns, sep=';', encoding="utf-8")
    content = csv_buffer.getvalue()
    csv_buffer.close()
    s3.put_object(Bucket='dados.datalake.movidesk', Body=content, Key="ticket-surveys/surveys.csv")

movidesk api/Códigos Python de extração do movidesk/Consulta Survey API/lambda_survey.py


from fasttext import load_model
import streamlit as st
import os
from gensim.utils import simple_preprocess
import wget

@st.cache(allow_output_mutation=True)
def download_model():
    # Download model
    model_name = "model.bin"
    model_url = "https://drive.google.com/file/d/1-7ZNbPdwBifcKFScNjb1GEop4gTyVEkc/view?usp=sharing"
    dir_path = os.path.dirname(os.path.realpath(__file__))
    file_model = wget.download(model_url, out=model_name)

    # Import model to FastText
    path = dir_path + "/" + model_name
    model = load_model(model_name)

    # Decrypto prediction to category name
    category_file_name = "/path_to_end_category.txt"
    path_to_end_category = {}
    with open(dir_path + category_file_name, encoding='utf8') as file:
        for line in file:
            key, value = line.split("_/_")
            path_to_end_category[int(key)] = value

download_model()

def make_prediction(corpus):
    corpus = simple_preprocess(corpus)
    res = model.predict(corpus)
    if res in path_to_end_category.keys():
        ans = path_to_end_category[res[0]]
    else:
        ans = "Категория для товара отстутвует"

    return ans

# _______________________Web________________________________________ #
# _______________________Title_____________________________________ #
st.title("Предскажем категорию товара по его названию и описанию")
st.write(
    """
    **Автор** Фомин Анатолий Витальевич
    **Контактные данные** +7(926)4395526, kmno4.172@gmail.com
    **Цель** Помочь продавцам сориентироваться среди тысяч категорий
    ***
    """)

# _______________________Page_Layout______________________________ #
col2, col3 = st.columns((10, 1))

st.text_input(
    label="Введи название и описание товара",
    value=""

)

# Предсказать КИН по данным:
pred = predict_1(dist=federal_distr, pvt=pvt, carbonate=is_carbonate, por=por, perm=perm, visc=visc)
# print("Предсказанный КИН:", pred)

st.write(
    f""" ## Предсказанный КИН на основании месторождений аналогов: {pred:.2f}
    """
)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Nov 13 19:32:15 2021

@author: ron
"""

from .base import AbstractTechnicalIndicator
from collections import deque
import numpy as np

class SMA(AbstractTechnicalIndicator):

    def __init__(self, period):
        """
        Parameters
        ----------
        ready: to indicate whether data is sufficent to calucalte the result
        """
        self.data = deque(maxlen=period)
        self.data_store = []
        self.lastest_val = 0
        self.period = period
        self.ready = False

    def update(self, price):
        self.data.append(price)
        #data is sufficient
        if not self.ready and len(self.data) == self.period:
            self.ready = True

        if self.ready:
            self.lastest_val = np.mean(self.data)
            self.data_store.append(self.lastest_val)

    def __bool__(self):
        return self.ready


import numpy as np
from numpy import vstack
from numpy import sqrt
from pandas import read_csv
from sklearn.metrics import mean_squared_error
from torch.utils.data import Dataset
from torch.utils.data import DataLoader
from torch.utils.data import random_split
from torch import Tensor
from torch.nn import Linear
from torch.nn import ReLU, Sigmoid
from torch.nn import Module
from torch.optim import SGD
from torch.nn import MSELoss
from torch.nn.init import kaiming_uniform_
from torch.nn.init import xavier_uniform_

# Model hyper-parameters
num_epochs = 100

# Dataset definition: Data loading
class CSVDataset(Dataset):
    # load the dataset
    def __init__(self, path):
        # load csv file as a dataframe using pandas
        df = read_csv(path, header=None)
        # store inputs and outputs
        self.X = df.values[:,:-1]
        self.y = df.values[:,-1]
        #print("Input data shape:", np.shape(self.X))
        #print("Input label shape:", np.shape(self.y))
        # Ensure input X and y values are floats
        self.X = self.X.astype('float32')
        self.y = self.y.astype('float32')
        self.y = self.y.reshape(len(self.y), 1)
        print("input data shape:", np.shape(self.X))

    # Number of rows in dataset
    def __len__(self):
        return len(self.X)

    # Get a row at an index
    def __getitem__(self, idx):
        return [self.X[idx], self.y[idx]]

    # Get indices for train and test rows
    def get_splits(self, n_train=0.7):
        train_split = round(n_train * len(self.X))
        test_split = len(self.X) - train_split
        return random_split(self, [train_split, test_split])

# Model definition
class MLP(Module):
    # define model architecture
    def __init__(self, n_inputs):
        super(MLP,self).__init__()
        # input to first MLP hidden layer
        self.hiddenlayer1 = Linear(n_inputs, 10)
        xavier_uniform_(self.hiddenlayer1.weight)
        self.activation1 = Sigmoid()
        # input to second MLP hidden layer
        self.hiddenlayer2 = Linear(10, 8)
        xavier_uniform_(self.hiddenlayer2.weight)
        self.activation2 = Sigmoid()
        # input tp third MLP hidden layer (last layer)
        self.hiddenlayer3 = Linear(8, 1)
        xavier_uniform_(self.hiddenlayer3.weight)

    # forward propagate input
    def forward(self, X):
        # input to first MLP hidden layer
        X = self.hiddenlayer1(X)
        X = self.activation1(X)
        # input to second MLP hidden layer
        X = self.hiddenlayer2(X)
        X = self.activation2(X)
        # input to third MLP hidden layer
        X = self.hiddenlayer3(X)
        return X

# prepare dataset
def prepare_data(path):
    # Load dataset
    dataset = CSVDataset(path)
    # Calculate split
    train, test = dataset.get_splits()
    train_dl = DataLoader(train, batch_size=32, shuffle=True)
    test_dl = DataLoader(test, batch_size=32, shuffle=False)
    return train_dl, test_dl

# model training
def train_model(train_dl, model):
    # define optimization
    loss_fn = MSELoss()
    optimizer = SGD(model.parameters(), lr=0.01, momentum=0.9)

    # enumerate epochs
    for epoch in range(num_epochs):
        # enumerate mini batches
        for i, (inputs, targets) in enumerate(train_dl):
            # clear gradients
            optimizer.zero_grad()
            # compute model output
            yhats = model(inputs)
            # calculate loss
            loss = loss_fn(yhats, targets)
            # back propagate
            loss.backward()
            # update model weights
            optimizer.step()

# evaluate model
def evaluate_model(test_dl, model):
    predictions, actuals = list(), list()
    # enumerate mini batches
    for i, (inputs, targets) in enumerate(test_dl):
        yhats = model(inputs)
        # retrieve numpy array from pytorch tensor
        yhats = yhats.detach().numpy()
        ys = targets.numpy()
        predictions.append(yhats)
        actuals.append(ys)

   # stack arrays vertically that are stored in batches
    predictions, actuals = vstack(predictions), vstack(actuals)
    # calculate performance metric
    acc = mean_squared_error(predictions, actuals)
    return acc

# predict class for a given input
def predict(row, model):
    # convert row to data
    row = Tensor([row])
    # predict input
    yhat = model(row)
    # retrieve numpy array by detaching from pytorch tensor
    yhat = yhat.detach().numpy()
    return yhat

# Driver functions
# prepare data
dataset_path = 'https://raw.githubusercontent.com/jbrownlee/Datasets/master/housing.csv'
train_dl, test_dl = prepare_data(dataset_path)
print("Number of samples in train and test set:", len(train_dl.dataset), len(test_dl.dataset))
# define network architecture
mlpregression_model = MLP(13)
# train the model
train_model(train_dl, mlpregression_model)
# evaluate model
mse = evaluate_model(test_dl, mlpregression_model)
print("Mean square error: %3f. Root mean square error: %f"%(mse, sqrt(mse)))
# predict class for a given input
row = [0.00632,18.00,2.310,0,0.5380,6.5750,65.20,4.0900,1,296.0,15.30,396.90,4.98]
yhat = predict(row, mlpregression_model)
print("Predicted: %.3f"%yhat)


class Flower:

    def __init__(self):
        self.nama = input('JENIS BUNGA        :')
        self.kelopak = int(input('JUMLAH KELOPAK     :'))
        self.price = int(input('HARGA SATUAN       :'))
        # print(f'Jenis Bunga     :{self.nama}')
        # print(f'Jumlah Kelopak  :{self.kelopak}')
        # print(f'Harga saturan   :{self.price}')
    #methods
    def harga_bunga(self):
        total = self.kelopak * self.price
        print(f'Harga bunga jenis {self.nama} dengan jumlah kelopak {self.kelopak} adalah {total}')

bunga = Flower()
bunga.harga_bunga()


import random

test = {
	'a': 1,
	'b': 2,
	'c': 3,
	'd': 4
}

del test['d']

for t in test:
	print(test[t])

print('d' in test)

test['a'] += 1
test['b'] *= 2
print(test['a'])
print(test['b'])

# ------

x = [
	'aaa',
	'bbb',
	'ccc',
	{
		'ddd': 'eee'
	}
]

for idx, item in enumerate(x):
	print(idx, item)

# ------

r = ['a','b','c','d','e']
s = ['a','d','f','g','e']
si = None
unique = False
while not unique:
	si = s[random.randint(0, len(s)-1)]
	if (si not in r):
		unique = True
print (si)

# ------

def power (a,b):
	return a**b, b**a

c, d = power(2,3)
print(c,d)

# ------

class TestClass ():
	def __str__ (self):
		return self.__class__.__name__

class_ = globals()['TestClass']
instance = class_()
print(instance)


# modulo de fecha
import datetime

# fecha actualcon hora minutos y segundos
fecha_actual = datetime.datetime.today()
print(fecha_actual)

# fecha actual hora minutos y segundos
fecha_actual2 = datetime.datetime.now()
print(fecha_actual2)

# obtener año
ano = fecha_actual2.year
print(ano)

# obtener mes
mes = fecha_actual2.month
print(mes)

# obtener dia
dia = fecha_actual2.day
print(dia)

# strftime(param) se le pasa un parametro (ver documentacion) y arroja un resultado
# ver nombre del dia completo ejemplo
dia_completo = fecha_actual2.strftime("%A")
print(dia_completo)

# importando modulo
from mi_modulo import Suma, saludo_cordial

Suma(10, 19)
saludo_cordial("Diana Laura")


import cv2
import numpy as np
if __name__ == "__main__":
    img = cv2.imread("tower.jpeg")
    # cv2.imshow("input", img)
    # cv2.waitKey(0)
    w, h, _ = img.shape
    src = np.float32([[180,94],[377,39],[242,312],[436,265]])
    dis = np.float32([[0,0],[w,0],[0,h],[w,h]])
    matrix = cv2.getPerspectiveTransform(src,dis)
    result = cv2.warpPerspective(img,matrix,(w,h))
    cv2.imshow("input",img)
    cv2.imshow("output",result)
    cv2.waitKey(0)


#!/usr/bin/python3

'''
Copyright (C) 2022 Cody Martin BLSOPS LLC

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.
'''

import os
import sys
import time
import base64
import json
import argparse
import colorama
import requests

DESCRIPTION = '''
	==========================================================
	#                                                        #
	#  If no access token or refresh_token is supplied,      #
	#  module will look in the REFRESH_TOKEN environment     #
	#  variable and request an access token                  #
	#                                                        #
	#  Outputs results in a text file, and a json file       #
	#  compatible with BloodHound                            #
	#                                                        #
	==========================================================
'''

# Set up our colors
colorama.init()
SUCCESS = colorama.Fore.GREEN
DANGER = colorama.Fore.RED
WARNING = colorama.Fore.YELLOW
RESET = colorama.Style.RESET_ALL
VALID = colorama.Fore.CYAN

# For use when requesting new access tokens with refresh token
URI = 'https://login.microsoftonline.com/Common/oauth2/token'
CLIENT_ID = 'd3590ed6-52b3-4102-aeff-aad2292ab01c'

# User agent to use with requests
USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:98.0) Gecko/20100101 Firefox/98.0'

def main():

	"""Runner method"""
	arg_parser = argparse.ArgumentParser(
		prog='get_tenant.py',
		usage=SUCCESS + '%(prog)s' + RESET + \
			' [-t|--access_token <access_token>]' + \
			' [-r|--refresh_token <refresh_token>]',
		description=DESCRIPTION,
		formatter_class=argparse.RawDescriptionHelpFormatter)
	arg_parser.add_argument(
		'-t',
		'--access_token',
		metavar='<access_token>',
		dest='access_token',
		type=str,
		help='The access token you would like to use',
		required=False)
	arg_parser.add_argument(
		'-r',
		'--refresh_token',
		metavar='<refresh_token>',
		dest='refresh_token',
		type=str,
		help='The refresh token you would like to use',
		required=False)
	arg_parser.add_argument('-R',
		'--refresh_token_file',
		metavar='<refresh_token_file>',
		dest='refresh_token_file',
		type=str,
		help='A JSON file saved from token_juggle.py ' \
			'containing the refresh token you would like to use.',
		required=False)
	arg_parser.add_argument('-o',
		'--outfile_path',
		metavar='<path>',
		dest='outfile_path',
		type=str,
		help='The path of where you want '\
			'the tenant data saved.'\
			'\nIf not supplied, module defaults to '\
			'the current directory.',
		required=False)

	args = arg_parser.parse_args()

	# Handle outfile path
	outfile_path_base = args.outfile_path
	if outfile_path_base is None:
		outfile_path_base = time.strftime('%Y-%m-%d_%H-%M-%S_')
	elif outfile_path_base[-1] != '/':
		outfile_path_base = outfile_path_base + '/' + time.strftime('%Y-%m-%d_%H-%M-%S_')
	outfile_text = outfile_path_base + 'tenant.txt'
	outfile_bloodhound = outfile_path_base + 'tenant_bloodhound.json'

	# Check to see if any graph or refresh token is given in the arguments
	# If both are given, will use graph token
	# If no token given, will check for a refresh token file
	# If no arguments are given, will look in the REFRESH_TOKEN environment variable
	if args.refresh_token is None and args.access_token is None and \
		args.refresh_token_file is None:
		try:
			refresh_token = os.environ['REFRESH_TOKEN']
		except KeyError:
			print(DANGER, '\n\tNo refresh token found.\n', RESET)
			arg_parser.print_help()
			sys.exit()
	elif args.refresh_token is None and args.access_token is None:
		path = args.refresh_token_file
		try:
			with open(path, encoding='UTF-8') as json_file:
				json_file_data = json.load(json_file)
				json_file.close()
		except OSError as error:
			print(str(error))
			sys.exit()
		refresh_token = json_file_data['refresh_token']
	elif args.access_token is not None:
		access_token = args.graph_token
	else:
		refresh_token = args.refresh_token

	# If we have a refresh token, use it to request the necessary graph access token
	if refresh_token is not None:
		# Setting up our post request
		headers = {
			'User-Agent': USER_AGENT
		}
		# body of our request
		data = {
			'client_id': CLIENT_ID,
			'resource': 'https://graph.microsoft.com',
			'grant_type': 'refresh_token',
			'refresh_token': refresh_token,
			'scope': 'openid',
		}

		# Sending the request
		json_data = {}
		try:
			response = requests.post(URI, data=data, headers=headers)
			json_data = response.json()
			response.raise_for_status()
		except requests.exceptions.HTTPError:
			print(DANGER)
			print(json_data['error'])
			print(json_data['error_description'])
			print(RESET)
			sys.exit()
		access_token = json_data['access_token']

	parts = access_token.split('.')
	payload = parts[1]
	payload_string = base64.b64decode(payload + '==')
	payload_json = json.loads(payload_string)

	tenant_id  = payload_json['tid']

	user = payload_json['upn']
	endpoint = f'https://login.microsoftonline.com/GetUserRealm.srf?login={user}'
	user_realm_json = requests.get(endpoint).json()

	tenant_name = user_realm_json['FederationBrandName']

	bloodhound_json_data = {
		'meta': {
			'count': 1,
			'type': 'aztenants',
			'version': 4
		},
		'data': [{
			'ObjectID': tenant_id,
			'DisplayName': tenant_name
		}]
	}
	with open(outfile_bloodhound, 'w+', encoding='UTF-8') as bloodhound_json_out:
		json.dump(bloodhound_json_data, bloodhound_json_out, indent = 4)
	with open(outfile_text, 'w+', encoding='UTF-8') as outfile:
		outfile.write(f'Tenant ID: {tenant_id}')
		outfile.write(f'Tenant Name: {tenant_name}')
	print(f'{SUCCESS}Tenant ID{RESET}: {tenant_id}')
	print(f'{SUCCESS}Tenant Name{RESET}: {tenant_name}')

if __name__ == '__main__':
	main()
	sys.exit()


from django.urls import path
from . import views
from django.contrib.auth.views import LogoutView

urlpatterns = [
    path('', views.index, name='index'),
    path('profile/', views.profile_view, name='profile'),
    path('logout/', LogoutView.as_view(), name='logout'),
    path('cars/', views.car_list, name='car_list'),
    path('cars/<int:pk>/', views.car_detail, name='car_detail'),
    path('orders/', views.OrderListView.as_view(), name='order_list'),
    path('orders/<int:pk>/', views.OrderDetailView.as_view(), name='order_detail'),
    path('myorders/', views.UserOrdersListView.as_view(), name='my_orders'),
    path('mycars/', views.UserCarListView.as_view(), name='my_cars'),
    path('myorders/<int:pk>', views.UserOrderDetailView.as_view(), name='my_order'),
    path('myorders/new', views.UserOrderCreateView.as_view(), name='my-order-new')
]


from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.support.ui import Select

try:
    link = "http://suninjuly.github.io/selects1.html"
    browser = webdriver.Chrome()
    browser.get(link)

    # Посчитать сумму заданных чисел
    num1 = browser.find_element(By.CSS_SELECTOR, "#num1").text
    print(num1)
    num2 = browser.find_element(By.CSS_SELECTOR, "#num2").text
    print(num2)
    x = int(num1) + int(num2)
    print(x)

    # Выбрать в выпадающем списке значение равное расчитанной сумме
    Select(browser.find_element(By.TAG_NAME, "select")).select_by_value(str(x))

    # Нажать Submit
    browser.find_element(By.CSS_SELECTOR, "button[type='submit']").click()

finally:
    # успеваем скопировать код за 30 секунд
    time.sleep(30)
    # закрываем браузер после всех манипуляций
    browser.quit()


import pprint

from stable_baselines3 import PPO

from di import register
from environments.Snake.SnakeGym import SnakeGym
from trainers.BaseTrainer import BaseTrainer

@register
class SnakeTrainer(BaseTrainer):

    def __init__(self):
        super().__init__()
        self._name = "snake"
        self.env = SnakeGym

        self.training_algorithm = PPO

        self.config = {
            "death_penalty": -10,
            "dist_reward": 10,
            "ent_coef": 0.02,
            "food_distance_growth_rate": 1.05,
            "food_reward": 25,
            "gae_lambda": 0.95,
            "gamma": 0.99,
            "learning_rate": 1.5e-4,
            "living_bonus": -0.1,
            "max_step": 4096,
            "n_steps": 1024,
            "num_envs": 1,
            "policy": "MlpPolicy",
            "total_timesteps": 100000,
            "vf_coef": 0.5
        }

        self.model_parameters = [
            "ent_coef",
            "gae_lambda",
            "gamma",
            "learning_rate",
            "n_steps",
            "policy",
            "vf_coef",
        ]

        self.game_parameters = [

        ]

    def play(self):
        env = self._create_env()()
        env.unwrapped.play()


#!/usr/bin/env python3
# vim: syntax=python ts=4 et sw=4 sts=4:

# Common supporting classes and calculations.

import datetime
from wilks import wilks

PercentageTable = [
    [100.00, 97.80, 95.50, 93.90, 92.20, 90.70, 89.20, 87.80, 86.30], # 1 rep
    [95.50,  93.90, 92.20, 90.70, 89.20, 87.80, 86.30, 85.00, 83.70], # 2 reps
    [92.20,  90.70, 89.20, 87.80, 86.30, 85.00, 83.70, 82.40, 81.10], # 3 reps
    [89.20,  87.80, 86.30, 85.00, 83.70, 82.40, 81.10, 79.90, 78.60], # 4 reps
    [86.30,  85.00, 83.70, 82.40, 81.10, 79.90, 78.60, 77.40, 76.20], # 5 reps
    [83.70,  82.40, 81.10, 79.90, 78.60, 77.40, 76.20, 75.10, 73.90], # 6 reps
    [81.10,  79.90, 78.60, 77.40, 76.20, 75.10, 73.90, 72.30, 70.70], # 7 reps
    [78.60,  77.40, 76.20, 75.10, 73.90, 72.30, 70.70, 69.40, 68.00], # 8 reps
    [76.20,  75.10, 73.90, 72.30, 70.70, 69.40, 68.00, 66.70, 65.30], # 9 reps
    [73.90,  72.30, 70.70, 69.40, 68.00, 66.70, 65.30, 64.00, 62.60], # 10 reps
    [70.70,  69.40, 68.00, 66.70, 65.30, 64.00, 62.60, 61.30, 59.90], # 11 reps
    [68.00,  66.70, 65.30, 64.00, 62.60, 61.30, 59.90, 58.60, 00.00], # 12 reps
]

# Not used, but kept around for future error checking.
def percentage_tabular(reps, rpe):
    # Insufficient reps to accurately predict RPE or E1RM.
    if reps > 12 or reps < 1:
        return 0.0

    if rpe == 10.0: return PercentageTable[reps - 1][0]
    if rpe == 9.5:  return PercentageTable[reps - 1][1]
    if rpe == 9.0:  return PercentageTable[reps - 1][2]
    if rpe == 8.5:  return PercentageTable[reps - 1][3]
    if rpe == 8.0:  return PercentageTable[reps - 1][4]
    if rpe == 7.5:  return PercentageTable[reps - 1][5]
    if rpe == 7.0:  return PercentageTable[reps - 1][6]
    if rpe == 6.5:  return PercentageTable[reps - 1][7]
    if rpe == 6.0:  return PercentageTable[reps - 1][8]
    return 0.0

# This is the above chart expressed as a piecewise function.
# This enables using a larger set of real numbers for RPEs, like 8.75.
def percentage(reps, rpe):
    # No prediction if failure occurred, or if RPE is unreasonably low.
    if reps < 1 or rpe < 4:
        return 0

    # Handle the obvious case early to avoid error margins.
    if reps == 1 and rpe == 10:
        return 100

    # x is defined such that 1@10 = 0, 1@9 = 1, 1@8 = 2, etc.
    # By definition of RPE, then also:
    #  2@10 = 1@9 = 1
    #  3@10 = 2@9 = 1@8 = 2
    # And so on. That pattern gives the equation below.
    x = (10 - rpe) + (reps - 1)

    # The logic breaks down for super-high numbers,
    # and it's too hard to extrapolate an E1RM from super-high-rep sets anyway.
    if x >= 16:
        return 0

    intersection = 2.92

    # The highest values follow a quadratic.
    # Parameters were resolved via GNUPlot and match extremely closely.
    if x <= intersection:
        a = 0.347619
        b = -4.60714
        c = 99.9667
        return a*x*x + b*x + c

    # Otherwise it's just a line, since Tuchscherer just guessed.
    m = -2.64249
    b = 97.0955
    return m*x + b

def calc_weight(e1rm, reps, rpe):
    # weight / percentage(reps, rpe) * 100 = e1rm
    # weight = e1rm / 100 * percentage(reps, rpe);
    return e1rm / 100 * percentage(reps, rpe)

def calc_e1rm(weight, reps, rpe):
    if rpe == 0:
        return 0
    return weight / percentage(reps, rpe) * 100

#####################################################################

class Set:
    def __init__(self, weight, reps, rpe=0, failure=False):
        self.weight = float(weight)
        self.reps = int(reps)
        self.rpe = float(rpe)
        self.failure = bool(failure)

        if self.reps < 0:
            error("Negative reps")
        if self.reps == 0 and not self.failure:
            error("Invalid set: no reps done and no failure.")

    def e1rm(self):
        factor = percentage(self.reps, self.rpe)

        # Use the Tuchscherer chart.
        if factor > 0:
            return self.weight / factor * 100

        # If RPEs are missing or the chart is inadequate, don't mislead.
        return 0.0

    # For cases that the RPE chart doesn't cover, we have the Epley formula.
    def epley(self):
        if self.reps == 0:
            return 0.0

        # Use RPE to include any reps not done.
        reps = self.reps
        if self.rpe:
            reps += 10 - self.rpe

        if reps == 1:
            return self.weight
        return self.weight + ((self.weight * reps) / 30)

    # Compared to the specified E1RM, how much fatigue did this set demonstrate?
    # Numbers returned are in units of "fatigue percents".
    def fatigue(self, best_e1rm):
        set_e1rm = self.e1rm()
        if set_e1rm == 0 or best_e1rm == 0:
            return 0
        return max(0, (1 - (set_e1rm / best_e1rm)) * 100)

class Lift:
    def __init__(self, name):
        self.name = name
        self.sets = []

    def addsets(self, slist):
        self.sets += slist

    # Get a list of non-warmup, actual training sets.
    def get_worksets(self):
        # Lifts that track RPE track them for every set.
        highrpe = list(filter(lambda x: x.rpe >= 6.0, self.sets))
        if len(highrpe) > 0:
            return highrpe

        # If this is one of the earlier lifts that didn't track RPE,
        # just take some of the heaviest sets of what's present.
        topweight = max([0] + list(map(lambda x: x.weight, self.sets)))
        cutoff = topweight * 0.70
        return filter(lambda x: x.weight >= cutoff, self.sets)

    # TODO: Should trust higher RPEs more.
    def e1rm(self):
        return max([0] + list(map(lambda x: x.e1rm(), self.get_worksets())))

    def epley(self):
        return max([0] + list(map(lambda x: x.epley(), self.get_worksets())))

    def volume(self):
        return sum(map(lambda x: x.reps, self.get_worksets()))

    def tonnage(self):
        return sum(map(lambda x: x.weight * x.reps, self.get_worksets()))

    def fatigue(self):
        best_e1rm = self.e1rm()
        if best_e1rm == 0:
            return 0
        # Compare to the most recent set that had an e1rm.
        for i in range(len(self.sets), 0, -1):
            e1rm = self.sets[i-1].e1rm()
            if e1rm == 0:
                continue
            return self.sets[i-1].fatigue(best_e1rm)
        return 0

class TrainingSession:
    def __init__(self, year, month, day):
        self.date = datetime.date(year, month, day)
        self.bodyweight = 0.0
        self.lifts = []

    def setbodyweight(self, bodyweight):
        self.bodyweight = bodyweight

    def addlift(self, l):
        self.lifts.append(l)

    def e1rm(self, matchfn):
        lifts = filter(matchfn, self.lifts)
        return max([0] + list(map(Lift.e1rm, lifts)))

    def epley(self, matchfn):
        lifts = filter(matchfn, self.lifts)
        return max([0] + list(map(Lift.epley, lifts)))

    def volume(self, matchfn):
        lifts = filter(matchfn, self.lifts)
        return sum(x.volume() for x in lifts)

    def tonnage(self, matchfn):
        lifts = filter(matchfn, self.lifts)
        return sum(x.tonnage() for x in lifts)

    def fatigue(self, matchfn):
        lifts = filter(matchfn, self.lifts)
        return sum(x.fatigue() for x in lifts)

    def epley_wilks(self, matchfn):
        lifts = filter(matchfn, self.lifts)
        def liftwilks(lift):
            return wilks(True, self.bodyweight/2.20462262, lift.epley()/2.20462262)
        return max([0] + list(map(liftwilks, lifts)))

# Handles the case of multiple TrainingSessions on the same day,
# but really just exists so that the code between daily/weekly
# uses the same types and the same interface.
class DailyIterator:
    def __init__(self, sessions):
        self.sessions = sessions

    def __iter__(self):
        self.idx = 0
        return self

    def __sameday(self, a, b):
        return a.toordinal() == b.toordinal()

    def __next__(self):
        if self.idx >= len(self.sessions):
            raise StopIteration

        startdate = self.sessions[self.idx].date
        acc = []
        for i in range(self.idx, len(self.sessions)):
            if self.__sameday(startdate, self.sessions[i].date):
                acc.append(self.sessions[i])
            else:
                break

        self.idx += len(acc)
        return acc

class WeeklyIterator:
    def __init__(self, sessions):
        self.sessions = sessions

    def __iter__(self):
        self.idx = 0
        return self

    # Whether the two given dates are in the same training week.
    # A training week is Sunday through Saturday (inclusive).
    def __sameweek(self, a, b):
        # Canonicalize each date according to the last Sunday.
        # isoweekday() gives 1 for Monday, 7 for Sunday.
        # So in terms of mapping how much to subtract:
        #  isoweekday -> subtract_amount
        #  [1,2,3,4,5,6,7] -> [1,2,3,4,5,6,0]
        week_a = a.toordinal() - (a.isoweekday() % 7)
        week_b = b.toordinal() - (b.isoweekday() % 7)
        return week_a == week_b

    def __next__(self):
        if self.idx >= len(self.sessions):
            raise StopIteration

        # Generate a list of sessions from here until Saturday (inclusive).
        # In terms of weekday(), Saturday is a return value of 6.
        startdate = self.sessions[self.idx].date
        acc = []

        for i in range(self.idx, len(self.sessions)):
            if self.__sameweek(startdate, self.sessions[i].date):
                acc.append(self.sessions[i])
            else:
                break

        self.idx += len(acc)
        return acc

# Return a single date as representative of the list of dates.
# For choosing a date on which to mark weekly volume, etc.
def canonical_date(sessionlist):
    return sessionlist[-1].date


from tools import *

def get_mysql_data(device_id, area_config_id, platform, app_version):
    advs = []
    is_vip_flag = is_vip('token')
    if is_vip_flag == True:
        return advs
    sql_data = sql(device_id, area_config_id, platform, app_version)
    advs = MysqlData().getdata(sql_data[0], sql_data[1])
    return advs

if __name__ == '__main__':
    device_id = '20190712141352c5590aeffbbec19d7b474f3958f971c8018c311326c075ef'
    res = get_mysql_data(device_id, 30, 1, 40100)
    print(res)


# -*- coding: utf-8 -*-
from django import template
from django.utils.safestring import SafeText
from glynt.apps.transact.models import Transaction

register = template.Library()

@register.inclusion_tag('transact/partials/transaction_choice.html', takes_context=True)
def transactions(context, transaction):
    if type(transaction) in [SafeText, str, unicode]:
        transaction = Transaction.objects.filter(slug=unicode(transaction))
    elif type(transaction) in [list, tuple]:
        transaction = Transaction.objects.filter(slug__in=transaction)
    else:
        transaction = Transaction.objects.all()

    context.update({
        'transactions': transaction
    })
    return context

@register.simple_tag(takes_context=True)
def transpose_company_data(context, value):
    user = context.get('user')
    if user:
        try:
            company = user.companies.all()[0]
        except IndexError:
            company = None
    t = template.Template(value)
    c = template.Context(company.__dict__)
    return t.render(c)


def verifica_nulos(matriz):
   lista_nulos = []
   for linha in range(len(matriz)):
       for coluna in range(len(matriz[linha])):
           if matriz[linha][coluna] == ' ':
              lista_nulos.append([f" O elemento nulo está na linha {linha}, e na coluna {coluna}"])

   return lista_nulos

m = [
  [333, ' ', 7, 4],
  [2, 5, 8, 3,],
  [5, ' ', 5, 8],
  [3, 8, 9, 3]
  ]

print(verifica_nulos(m))


from women.models import *
from django.db import connection
connection.queries
from django.db.models import Min, Max, Count
def look():
    print(Women.objects.all().count())
    print(Women.objects.all()[3:8])
    Women.objects.order_by('-pk')
    Women.objects.all().reverse()
    Women.objects.filter(pk__lte=2)# less then equal
    w = Women.objects.get(pk=2) #строго одна запись
    c3 = Category.objects.get(pk=1)
    print(c3.women_set.exists())# count() и exists() применяются к любой выборке
    c2 = Category.objects.get(pk=2)
    print(c2.women_set.count())
    Women.objects.filter(cat__name__contains='цы')
    Category.objects.filter(women__title__contains='ли')
    Women.objects.aggregate(cat_min=Min('cat_id'), cat_max=Max('cat_id'))
    Women.objects.values('title', 'cat_id').get(pk=1) # values запрашивает не все поля
    c = Category.objects.annotate(total=Count('women')).filter(total__gt=0)
    Women.objects.raw('SELECT * FROM women_women')


array=list(input().split())
#print(array)
n=int(array[0])
k=int(array[1])
str='0123456789ABCDEF'
stack=[]
#print(k)
while n!=0:
    stack.append(str[n%k])
    n=n//k
while stack!=[]:
    print(stack.pop(),end='')


import json
import tempfile
import zipfile
from contextlib import contextmanager
from functools import wraps
from traceback import TracebackException

import boto3

codepipeline_client = boto3.client("codepipeline")
ec2_client = boto3.client("ec2")
sts_client = boto3.client("sts")

def codepipeline_lambda_handler(func):
    """
    Decorates a lambda handler function to set up logging and error handling.
    Sends a failure result to CodePipeline if the decorated function raises
    an exception, otherwise sending a success result.

    """

    @wraps(func)
    def wrapped(event, context):

        # Set up logging and log the event.
        # Example event: https://docs.amazonaws.cn/en_us/lambda/latest/dg/services-codepipeline.html
        log.context = {"RequestId": context.aws_request_id}
        log("EVENT", json.dumps(event, indent=2))

        # Get the job data.
        job = event["CodePipeline.job"]

        # Process the job and then notify CodePipeline of its success or failure.
        try:
            func(event, context)
            log("SUCCESS")
            codepipeline_client.put_job_success_result(jobId=job["id"])
        except Exception as error:
            log("FAILURE", exception=error)
            codepipeline_client.put_job_failure_result(
                jobId=job["id"],
                failureDetails={
                    "type": "JobFailed",
                    "message": f"{error.__class__.__name__}: {error}",
                    "externalExecutionId": context.aws_request_id,
                },
            )

    return wrapped

@contextmanager
def create_zip_file(files):
    """
    Creates a temporary zip file on disk. The files parameter must be a
    dictionary of {filename: contents} which will be written to the zip
    file. The zip file path is yielded as the context value.

    """

    with tempfile.NamedTemporaryFile() as temp_file:
        with zipfile.ZipFile(temp_file.name, "w") as zip_file:
            for filename, contents in files.items():
                zip_file.writestr(filename, contents)
        yield temp_file.name

def get_artifact_s3_client(job):
    """
    Returns an S3 client with access to the CodePipeline artifact S3 bucket.

    """

    creds = job["data"]["artifactCredentials"]
    session = boto3.Session(
        aws_access_key_id=creds["accessKeyId"],
        aws_secret_access_key=creds["secretAccessKey"],
        aws_session_token=creds["sessionToken"],
    )
    return session.client("s3")

def get_cloudformation_template(cfn_client, stack_name):
    """
    Returns the template body of a CloudFormation stack.

    """

    response = cfn_client.get_template(StackName=stack_name)
    return response["TemplateBody"]

def get_input_artifact_location(job):
    """
    Returns the S3 location of the input artifact.

    """

    input_artifact = job["data"]["inputArtifacts"][0]
    input_location = input_artifact["location"]["s3Location"]
    input_bucket = input_location["bucketName"]
    input_key = input_location["objectKey"]
    return (input_bucket, input_key)

def get_output_artifact_location(job):
    """
    Returns the expected S3 destination location of the output artifact.
    The Lambda function needs to place an output artifact there.

    """

    output_artifact = job["data"]["outputArtifacts"][0]
    output_location = output_artifact["location"]["s3Location"]
    output_bucket = output_location["bucketName"]
    output_key = output_location["objectKey"]
    return (output_bucket, output_key)

def get_session(role_arn, session_name, duration_seconds=900):
    """
    Returns a boto3 session for the specified role.

    """

    response = sts_client.assume_role(
        RoleArn=role_arn,
        RoleSessionName=session_name,
        DurationSeconds=duration_seconds,
    )
    creds = response["Credentials"]
    return boto3.Session(
        aws_access_key_id=creds["AccessKeyId"],
        aws_secret_access_key=creds["SecretAccessKey"],
        aws_session_token=creds["SessionToken"],
    )

def get_user_parameters(job):
    """
    Returns the user parameters that were defined in CodePipeline.

    """

    return json.loads(
        job["data"]["actionConfiguration"]["configuration"]["UserParameters"]
    )

def log(title, message="", exception=None):
    """
    Logs a message, which will show up in CloudWatch Logs.

    """

    parts = [str(title)]
    for key, value in log.context.items():
        parts.append(key)
        parts.append(value)
    if message != "":
        parts.append(str(message))
    value = " ".join(parts)
    if exception:
        value += "\n"
        value += "\n".join(TracebackException.from_exception(exception).format())
    print(value.replace("\n", "\r"))

log.context = {}


# 6. Make a class Fraction that contains two instance variables, nr and dr
# (nr stands for numerator and dr for denominator). Define an __init__ method that provides values
# for these instance variables. Make the denominator optional by providing a default argument of 1.
#
# In the __init__ method, make the denominator positive if it is negative.
# For example  -2/-3 should be changed to 2/3 and 2/-3 to -2/3.
#
# Write a method named show that prints numerator, then '/' and then the denominator.
#
# Make sure that you write this class as we will be using it to learn magic methods.

class Fraction:

    def __init__(self, nr, dr=1):
        self.nr = nr
        # making the denominator postive if it is negative
        self.dr = dr if dr >=0 else -dr

    def display(self):
        print(f'{self.nr}/{self.dr}')

f1 = Fraction(2, 3)
f2 = Fraction(-2, -3)
f1.display()
f2.display()


import torch
import torch.nn.functional as F
from torch import nn

'''LeNet_3_32_32 in PyTorch.'''

# codes are import from https://github.com/xternalz/WideResNet-pytorch/blob/master/wideresnet.py
# original author: xternalz

import math

class Expression(nn.Module):
    def __init__(self, func):
        super(Expression, self).__init__()
        self.func = func

    def forward(self, input):
        return self.func(input)

class Model(nn.Module):
    def __init__(self, i_c=1, n_c=10):
        super(Model, self).__init__()

        self.conv1 = nn.Conv2d(i_c, 32, 5, stride=1, padding=2, bias=True)
        self.pool1 = nn.MaxPool2d((2, 2), stride=(2, 2), padding=0)

        self.conv2 = nn.Conv2d(32, 64, 5, stride=1, padding=2, bias=True)
        self.pool2 = nn.MaxPool2d((2, 2), stride=(2, 2), padding=0)

        self.flatten = Expression(lambda tensor: tensor.view(tensor.shape[0], -1))
        self.fc1 = nn.Linear(7 * 7 * 64, 1024, bias=True)
        self.fc2 = nn.Linear(1024, n_c)

    def forward(self, x_i, _eval=False):

        if _eval:
            # switch to eval mode
            self.eval()
        else:
            self.train()

        x_o = self.conv1(x_i)
        x_o = torch.relu(x_o)
        x_o = self.pool1(x_o)

        x_o = self.conv2(x_o)
        x_o = torch.relu(x_o)
        x_o = self.pool2(x_o)

        x_o = self.flatten(x_o)

        x_o = torch.relu(self.fc1(x_o))

        self.train()

        return self.fc2(x_o)

class BasicBlock(nn.Module):
    def __init__(self, in_planes, out_planes, stride, dropRate=0.0):
        super(BasicBlock, self).__init__()
        self.bn1 = nn.BatchNorm2d(in_planes)
        self.relu1 = nn.LeakyReLU(0.1, inplace=True)
        self.conv1 = nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
                               padding=1, bias=False)
        self.bn2 = nn.BatchNorm2d(out_planes)
        self.relu2 = nn.LeakyReLU(0.1, inplace=True)
        self.conv2 = nn.Conv2d(out_planes, out_planes, kernel_size=3, stride=1,
                               padding=1, bias=False)
        self.droprate = dropRate
        self.equalInOut = (in_planes == out_planes)
        self.convShortcut = (not self.equalInOut) and nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=stride,
                                                                padding=0, bias=False) or None

    def forward(self, x):
        if not self.equalInOut:
            x = self.relu1(self.bn1(x))
        else:
            out = self.relu1(self.bn1(x))
        out = self.relu2(self.bn2(self.conv1(out if self.equalInOut else x)))
        if self.droprate > 0:
            out = F.dropout(out, p=self.droprate, training=self.training)
        out = self.conv2(out)
        return torch.add(x if self.equalInOut else self.convShortcut(x), out)

class NetworkBlock(nn.Module):
    def __init__(self, nb_layers, in_planes, out_planes, block, stride, dropRate=0.0):
        super(NetworkBlock, self).__init__()
        self.layer = self._make_layer(block, in_planes, out_planes, nb_layers, stride, dropRate)

    def _make_layer(self, block, in_planes, out_planes, nb_layers, stride, dropRate):
        layers = []
        for i in range(int(nb_layers)):
            layers.append(block(i == 0 and in_planes or out_planes, out_planes, i == 0 and stride or 1, dropRate))
        return nn.Sequential(*layers)

    def forward(self, x):
        return self.layer(x)

class WideResNet(nn.Module):
    def __init__(self, depth, num_classes, widen_factor=1, dropRate=0.0):
        super(WideResNet, self).__init__()
        nChannels = [16, 16 * widen_factor, 32 * widen_factor, 64 * widen_factor]
        assert ((depth - 4) % 6 == 0)
        n = (depth - 4) / 6
        block = BasicBlock
        # 1st conv before any network block
        self.conv1 = nn.Conv2d(3, nChannels[0], kernel_size=3, stride=1,
                               padding=1, bias=False)
        # 1st block
        self.block1 = NetworkBlock(n, nChannels[0], nChannels[1], block, 1, dropRate)
        # 2nd block
        self.block2 = NetworkBlock(n, nChannels[1], nChannels[2], block, 2, dropRate)
        # 3rd block
        self.block3 = NetworkBlock(n, nChannels[2], nChannels[3], block, 2, dropRate)
        # global average pooling and classifier
        self.bn1 = nn.BatchNorm2d(nChannels[3])
        self.relu = nn.LeakyReLU(0.1, inplace=True)
        self.fc = nn.Linear(nChannels[3], num_classes)
        self.nChannels = nChannels[3]

        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
                m.weight.data.normal_(0, math.sqrt(2. / n))
            elif isinstance(m, nn.BatchNorm2d):
                m.weight.data.fill_(1)
                m.bias.data.zero_()
            elif isinstance(m, nn.Linear):
                m.bias.data.zero_()

    def forward(self, x, _eval=False):
        if _eval:
            # switch to eval mode
            self.eval()
        else:
            self.train()

        out = self.conv1(x)
        out = self.block1(out)
        out = self.block2(out)
        out = self.block3(out)
        out = self.relu(self.bn1(out))
        out = F.avg_pool2d(out, 8)
        out = out.view(-1, self.nChannels)

        self.train()

        return self.fc(out)

class RestNetBasicBlock(nn.Module):
    def __init__(self, in_channels, out_channels, stride):
        super(RestNetBasicBlock, self).__init__()
        self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=stride, padding=1)
        self.bn1 = nn.BatchNorm2d(out_channels)
        self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=stride, padding=1)
        self.bn2 = nn.BatchNorm2d(out_channels)

    def forward(self, x):
        output = self.conv1(x)
        output = F.relu(self.bn1(output))
        output = self.conv2(output)
        output = self.bn2(output)
        return F.relu(x + output)

class RestNetDownBlock(nn.Module):
    def __init__(self, in_channels, out_channels, stride):
        super(RestNetDownBlock, self).__init__()
        self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=stride[0], padding=1)
        self.bn1 = nn.BatchNorm2d(out_channels)
        self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=stride[1], padding=1)
        self.bn2 = nn.BatchNorm2d(out_channels)
        self.extra = nn.Sequential(
            nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=stride[0], padding=0),
            nn.BatchNorm2d(out_channels)
        )

    def forward(self, x):
        extra_x = self.extra(x)
        output = self.conv1(x)
        out = F.relu(self.bn1(output))

        out = self.conv2(out)
        out = self.bn2(out)
        return F.relu(extra_x + out)

class RestNet18(nn.Module):
    def __init__(self):
        super(RestNet18, self).__init__()
        self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3)
        self.bn1 = nn.BatchNorm2d(64)
        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)

        self.layer1 = nn.Sequential(RestNetBasicBlock(64, 64, 1),
                                    RestNetBasicBlock(64, 64, 1))

        self.layer2 = nn.Sequential(RestNetDownBlock(64, 128, [2, 1]),
                                    RestNetBasicBlock(128, 128, 1))

        self.layer3 = nn.Sequential(RestNetDownBlock(128, 256, [2, 1]),
                                    RestNetBasicBlock(256, 256, 1))

        self.layer4 = nn.Sequential(RestNetDownBlock(256, 512, [2, 1]),
                                    RestNetBasicBlock(512, 512, 1))

        self.avgpool = nn.AdaptiveAvgPool2d(output_size=(1, 1))

        self.fc = nn.Linear(512, 10)

    def forward(self, x):
        out = self.conv1(x)
        out = self.layer1(out)
        out = self.layer2(out)
        out = self.layer3(out)
        out = self.layer4(out)
        out = self.avgpool(out)
        out = out.reshape(x.shape[0], -1)
        out = self.fc(out)
        return out

class CNN_1_cifar10(nn.Module):
    def __init__(self):
        super(CNN_1_cifar10, self).__init__()
        self.conv1 = nn.Conv2d(3, 6, 5)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(6, 16, 5)
        self.fc1 = nn.Linear(16 * 5 * 5, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)

    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv2(x)))
        x = x.view(-1, 16 * 5 * 5)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

class Alex_1_cifar10(nn.Module):
    def __init__(self):
        super(Alex_1_cifar10, self).__init__()
        self.conv1 = nn.Conv2d(3, 6, 5)
        self.conv2 = nn.Conv2d(6, 16, 5)
        self.fc1 = nn.Linear(16 * 5 * 5, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)

    def forward(self, x):
        x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
        x = F.max_pool2d(F.relu(self.conv2(x)), 2)
        x = x.view(x.size()[0], -1)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

class net_cifar10(nn.Module):
    def __init__(self):
        # the input size = Batch *3*32*32
        super(net_cifar10, self).__init__()
        self.seq = torch.nn.Sequential(
            nn.Conv2d(in_channels=3, out_channels=64, kernel_size=(3, 3)),
            nn.ReLU(),
            nn.Conv2d(64, 64, (3, 3)),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=(2, 2)),
            nn.Conv2d(64, 128, (3, 3)),
            nn.ReLU(),
            nn.Conv2d(128, 128, (3, 3)),
            nn.ReLU(),
            nn.MaxPool2d((2, 2)),
        )
        self.seq2 = nn.Sequential(
            nn.Linear(in_features=128 * 5 * 5, out_features=256),
            nn.ReLU(),
            nn.Linear(256, 256),
            nn.ReLU(),
            nn.Linear(256, 10)
        )

        self.modules_to_hook = ('seq.1',
                                'seq.3',
                                'seq.6',
                                'seq.8',
                                'seq2.4')

    def forward(self, x):
        r1 = self.seq(x)
        r1 = r1.view(r1.size(0), -1)
        r2 = self.seq2(r1)
        return r2

class LeNet_cifar10(nn.Module):
    def __init__(self):
        super(LeNet_cifar10, self).__init__()
        self.conv1 = nn.Conv2d(3, 6, 5)
        self.conv2 = nn.Conv2d(6, 16, 5)
        self.fc1 = nn.Linear(16 * 5 * 5, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)

    def forward(self, x):
        out = F.relu(self.conv1(x))
        out = F.max_pool2d(out, 2)
        out = F.relu(self.conv2(out))
        out = F.max_pool2d(out, 2)
        out = out.view(out.size(0), -1)
        out = F.relu(self.fc1(out))
        out = F.relu(self.fc2(out))
        out = self.fc3(out)
        return out

# x1 = torch.rand(1, 3, 32, 32)
# x2 = torch.rand(1, 1, 28, 28)
#
# net1 = net_cifar10()
# net2 = Net_mnist()
# print(net1(x1).size())
# print(net2(x2).size())

def function(data=[]):
    data.append(1)
    return data
function()
function()
function()
print(function())


import collections
import signal
import sys
import time
import wave
import keras
import audio_utility as au
from array import array
from struct import pack

import pyaudio
from pydub import AudioSegment
from pydub.playback import play
import webrtcvad

import librosa
import numpy as np

FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 16000
CHUNK_DURATION_MS = 30  # supports 10, 20 and 30 (ms)
PADDING_DURATION_MS = 1000  # 1 sec jugement
CHUNK_SIZE = int(RATE * CHUNK_DURATION_MS / 1000)  # chunk to read
CHUNK_BYTES = CHUNK_SIZE * 2  # 16bit = 2 bytes, PCM
NUM_PADDING_CHUNKS = int(PADDING_DURATION_MS / CHUNK_DURATION_MS)
# NUM_WINDOW_CHUNKS = int(240 / CHUNK_DURATION_MS)
NUM_WINDOW_CHUNKS = int(400 / CHUNK_DURATION_MS)  # 400 ms/ 30ms  ge
NUM_WINDOW_CHUNKS_END = NUM_WINDOW_CHUNKS * 2

START_OFFSET = int(NUM_WINDOW_CHUNKS * CHUNK_DURATION_MS * 0.5 * RATE)

vad = webrtcvad.Vad(1)

pa = pyaudio.PyAudio()
stream = pa.open(
    format=FORMAT,
    channels=CHANNELS,
    rate=RATE,
    input=True,
    start=False,
    # input_device_index=2,
    frames_per_buffer=CHUNK_SIZE,
)

got_a_sentence = False
leave = False

# initialize inference
feature_extractor = au.SpeechFeatures()
wanted_words = [
    "_silence_",
    "_unknown_",
    "backward",
    "forward",
    "go",
    "left",
    "right",
    "stop",
]
# import keras model
model = keras.models.load_model(
    "models/vgg_19_model_mfcc_49x40_left_right_forward_backward_stop_go.hdf5"
)
model.summary()

def inference(test_audio):
    y, sr = librosa.load(test_audio, sr=16000)
    # fix the length of test audio to 1s or 16000 samples
    y = librosa.util.fix_length(y, size=16000)
    # normalize the test audio into [-1. 1]
    # y = np.clip(y, -1.0, 1.0)

    # extract the features
    audio_feature = feature_extractor.mfcc(y, fs=16000)
    audio_feature = audio_feature.flatten()
    audio_feature = np.expand_dims(audio_feature, axis=0)
    # inference
    prediction = model.predict(audio_feature)
    prediction = np.argmax(prediction)
    print(prediction, wanted_words[int(prediction)])

def handle_int(sig, chunk):
    global leave, got_a_sentence
    leave = True
    got_a_sentence = True

def record_to_file(path, data, sample_width):
    "Records from the microphone and outputs the resulting data to 'path'"
    # sample_width, data = record()
    data = pack("<" + ("h" * len(data)), *data)
    wf = wave.open(path, "wb")
    wf.setnchannels(1)
    wf.setsampwidth(sample_width)
    wf.setframerate(RATE)
    wf.writeframes(data)
    wf.close()

def normalize(snd_data):
    "Average the volume out"
    MAXIMUM = 16384  # 16384
    times = float(MAXIMUM) / max(abs(i) for i in snd_data)
    r = array("h")
    for i in snd_data:
        r.append(int(i * times))
    return r

signal.signal(signal.SIGINT, handle_int)

while not leave:
    ring_buffer = collections.deque(maxlen=NUM_PADDING_CHUNKS)
    triggered = False
    voiced_frames = []
    ring_buffer_flags = [0] * NUM_WINDOW_CHUNKS
    ring_buffer_index = 0

    ring_buffer_flags_end = [0] * NUM_WINDOW_CHUNKS_END
    ring_buffer_index_end = 0
    buffer_in = ""
    # WangS
    raw_data = array("h")
    index = 0
    start_point = 0
    StartTime = time.time()
    stream.start_stream()

    while not got_a_sentence and not leave:
        chunk = stream.read(CHUNK_SIZE)
        # add WangS
        raw_data.extend(array("h", chunk))
        index += CHUNK_SIZE
        TimeUse = time.time() - StartTime

        active = vad.is_speech(chunk, RATE)

        # sys.stdout.write('1' if active else '_')
        ring_buffer_flags[ring_buffer_index] = 1 if active else 0
        ring_buffer_index += 1
        ring_buffer_index %= NUM_WINDOW_CHUNKS

        ring_buffer_flags_end[ring_buffer_index_end] = 1 if active else 0
        ring_buffer_index_end += 1
        ring_buffer_index_end %= NUM_WINDOW_CHUNKS_END

        # start point detection
        if not triggered:
            ring_buffer.append(chunk)
            num_voiced = sum(ring_buffer_flags)
            if num_voiced > 0.8 * NUM_WINDOW_CHUNKS:
                # sys.stdout.write(' Open ')
                triggered = True
                start_point = index - CHUNK_SIZE * 20  # start point
                # voiced_frames.extend(ring_buffer)
                ring_buffer.clear()
        # end point detection
        else:
            # voiced_frames.append(chunk)
            ring_buffer.append(chunk)
            num_unvoiced = NUM_WINDOW_CHUNKS_END - sum(ring_buffer_flags_end)
            if num_unvoiced > 0.90 * NUM_WINDOW_CHUNKS_END or TimeUse > 10:
                triggered = False
                got_a_sentence = True

        sys.stdout.flush()

    sys.stdout.write("\n")

    stream.stop_stream()
    got_a_sentence = False

    # write to file
    raw_data.reverse()
    for index in range(start_point):
        raw_data.pop()
    raw_data.reverse()
    raw_data = normalize(raw_data)
    print("recorded")
    start = time.time()
    record_to_file("recording_.wav", raw_data, 2)
    print(f"time to save audio: {time.time() - start}")
    # leave = True
    start = time.time()
    inference("recording_.wav")
    print(f"time inference: {time.time() - start}")
    start = time.time()
    song = AudioSegment.from_wav("recording_.wav")
    play(song)
    print(f"time to play audio: {time.time() - start}")

stream.close()


# https://www.cvxpy.org/        # 최적화 solver

import numpy as np

# 1. Linear Equations
    # 1.1. Solving Linear Equations

    # Matrix Define
A = np.array([[4,-5], [-2,3]])
b = np.array([-13,9]).reshape(-1,1)

    # A-1 · b
# np.linalg.inv(A).dot(b)
np.linalg.inv(A) @ b

    # matrix 형태
# np.asmatrix(A).I.dot(b)
np.asmatrix(A).I @ b
# np.asmatrix(A).I * b

    # 1.4. Vector-Vector Products
x = np.array([1,1]).reshape(-1,1)       # column vector
y = np.array([2,3]).reshape(-1,1)       # column vector

# 내적 (inner product)
x.T.dot(y)
x.T @ y

# np.asmatrix(x).T * np.asmatrix(y)     # matrix
# np.asmatrix(x).T @ np.asmatrix(y)     # matrix

# 2. Norms (strenth or distance in linear space)

x = np.array([4,3]).reshape(-1,1)

np.linalg.norm(x,1)     # 1 norm
np.linalg.norm(x,2)     # 2 norm

x = np.matrix([[1],[2]])
y = np.matrix([[2],[-1]])

x.T * y     # 0 : orthogonal

# Linear Equation Approximate Solution ***
# np.linalg.inv(A.T @ A) @ A.T @ B

# 4.2. Geometic Point of View: Projection
# 4.2.1. Orthogonal Projection onto a Subspace
A = np.matrix([[1,0],
               [0,1],
               [0,0]])
B = np.matrix([[1],
               [1],
               [1]])

X = (A.T @ A).I @ A.T @ B   # Approximate Solution
B_star = A * X
B_star      # 수선의 발 위치


import serial
import sys
import os
from parse import parse
import time
import io
from tqdm import tqdm
from rumboot.OpFactory import OpFactory
from rumboot.ImageFormatDb import ImageFormatDb
from rumboot.xfer import xferManager
from serial.serialutil import to_bytes
import socket
import select

if sys.platform != "win32":
	import tty
	import termios
else:
    import codecs
    import ctypes

class terminal:
        verbose=True
        logstream=None
        plusargs={}
        initial_loop_done = False
        runlist = []
        formats = ImageFormatDb("rumboot.images")
        chip = None
        dumps = {}
        curbin = "bootrom"
        progress = tqdm(disable=True)
        replay = False
        replay_till_the_end = False
        ser = None
        cmdline = None
        xfer = None
        xferparams = None
        desc_widget = None
        prog_widget = None
        shell_prompt = None

        def __fixCodePage(self):
            if sys.__stdout__.encoding != 'cp65001':
                os.system("echo off")
                os.system("chcp 65001") # Change active page code
                sys.__stdout__.write("\x1b[A") # Removes the output of chcp command
                sys.__stdout__.flush()
            LF_FACESIZE = 32
            STD_OUTPUT_HANDLE = -11

            class COORD(ctypes.Structure):
                _fields_ = [("X", ctypes.c_short), ("Y", ctypes.c_short)]

            class CONSOLE_FONT_INFOEX(ctypes.Structure):
                _fields_ = [("cbSize", ctypes.c_ulong),
                ("nFont", ctypes.c_ulong),
                ("dwFontSize", COORD),
                ("FontFamily", ctypes.c_uint),
                ("FontWeight", ctypes.c_uint),
                ("FaceName", ctypes.c_wchar * LF_FACESIZE)]

            font = CONSOLE_FONT_INFOEX()
            font.cbSize = ctypes.sizeof(CONSOLE_FONT_INFOEX)
            font.nFont = 12
            font.dwFontSize.X = 7
            font.dwFontSize.Y = 12
            font.FontFamily = 54
            font.FontWeight = 400
            font.FaceName = "Lucida Console"
            handle = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
            ctypes.windll.kernel32.SetCurrentConsoleFontEx(handle, ctypes.c_long(False), ctypes.pointer(font))

        def __init__(self, port, speed, xferparams = None):
            if sys.platform == 'win32':
                self.__fixCodePage()
            self.port = port
            self.speed = speed
            self.reopen(port, speed, xferparams)

        def serial(self):
            return self.ser

        def reopen(self, port=None, speed=None, xferparams = None):
            if port != None:
                self.port = port
            if speed != None:
                self.speed = speed
            if xferparams != None:
                self.xferparams = xferparams

            if self.xfer != None:
                self.xfer.close()

            self.xfer = xferManager(self, self.xferparams)

            if self.ser != None:
                self.ser.close()
                self.ser = None

            if self.port.find("file://") == 0:
                self.ser = open(self.port.replace("file://",""), "rb+")
                self.replay = True
            elif self.port.find("://") < 0:
                self.ser = serial.Serial(self.port, self.speed, timeout=5)
            else:
                self.ser = serial.serial_for_url(self.port, timeout=5)
            self.opf = OpFactory("rumboot.ops", self)

        def close(self):
            self.ser.close()
            self.ser = None
            self.progress.close()

        def hack_enable_serial_delay(self, delay):
            if hasattr(self.ser,"__write_orig"):
                raise Exception("INTERNAL ERROR: serial delay hack already applied")

            def write_delayed(data):
                data = to_bytes(data)
                data = [bytes([b]) for b in data]
                for b in data:
                    self.ser.__write_orig(b)
                    time.sleep(delay)

            if not hasattr(self.ser, '__write_orig'):
                self.ser.__write_orig = self.ser.write
                self.ser.write = write_delayed

        def set_chip(self, chip):
            self.chip = chip
            self.xfer.setChip(chip)
            if self.hack("serial_delay"):
                self.hack_enable_serial_delay(0.00005)

        def tqdm(self, *args, **kwargs):
            self.progress.close()
            self.progress = tqdm(*args, **kwargs)

        def add_binaries(self, path):
            if type(path) is str:
                path = open(path, "rb")
            if type(path) is list:
                for p in path:
                    self.runlist.append(p[0])
            else:
                self.runlist.append(path)

        def load_binaries(self, path, use_stdin=False, break_after_uploads=True, timeout=-1):
            self.add_binaries(path)
            return terminal.loop(use_stdin, break_after_uploads, timeout)

        def add_dumps(self, dumps):
            self.dumps.update(dumps)

        def next_binary(self, peek=False):
            if len(self.runlist) > 0:
                ret = self.runlist[0]
                if not peek:
                    self.runlist.pop(0)
                    self.curbin = ret
                return ret
            return None

        def current_binary(self):
            return self.curbin

        def current_dump(self):
            if self.curbin == "bootrom":
                return self.dumps["rom"]
            if self.curbin.name in self.dumps:
                return self.dumps[self.curbin.name]

        def log(self, skipecho, *args, **kwargs):
            # Sometimes we get a weird exception on certain windows systems/setups
            # Try to catch any exceptions and dispose of them here
            try:
                if not skipecho and self.verbose:
                    self.progress.write(*args, **kwargs)
                    sys.stdout.flush()
                if not self.logstream == None:
                    self.logstream.write(*args)
                    self.logstream.write("\n")
            except:
                pass

        def sync(self):
            if not self.replay:
                self.ser.reset_input_buffer()
            c1 = b'a'
            c2 = b'd'
            while True:
                c2 = self.ser.read(1)
                if c1 == b'U' and c2 == b'\r':
                    break
                if c1 == b'U' and c2 == b'\n':
                    break
                c1 = c2

        def write(self, *args, **kwargs):
            return self.ser.write(*args, **kwargs)

        def read(self, *args, **kwargs):
            return self.ser.read(*args, **kwargs)

        def readline(self, *args, **kwargs):
            line = self.ser.readline(*args, **kwargs).decode("utf-8", errors="replace").strip()
            return line.replace("\r","")

        def hack(self, name):
            if name in self.chip.hacks and self.chip.hacks[name]:
                return True
            return False

        def set_progress_widgets(self, desc_widget = None, prog_widget = None):
            self.desc_widget = desc_widget
            self.prog_widget = prog_widget

        def progress_start(self, description, total):
            if self.prog_widget == None:
                self.tqdm(desc=description, total=total, unit_scale=True, unit_divisor=1024, unit='B', disable=False)
            else:
                pass

        def progress_update(self, total, position, increment):
            self.progress.update(increment)

        def progress_end(self):
            self.tqdm(disable=True)

        def wait(self, format, timeout=1000, callback = None):
            lines = []
            while True:
                line = self.readline()
                self.log(False, line, end='\n')
                ret = parse(format, line)
                if ret != None:
                    return ret, lines
                else:
                    lines.append(line)
                    if callable(callback):
                        callback(line)

        def shell_mode(self, prompt):
            self.shell_prompt = prompt

        def wait_prompt(self, initial=False):
            if initial:
                self.ser.write(b"\r\n")
            found = False
            data = b""
            while True:
                ret = self.ser.read_until(self.shell_prompt.encode())
                data = data + ret
                ret = ret.decode(errors="replace")
                self.log(False, ret, end='\n')
                if ret.find(self.shell_prompt) > -1:
                    found = True
                    if not initial:
                        break
                if found and len(ret) == 0:
                    break
            return data

        def cmd(self, cmd, response, timeout=15, endline = "\r\n", callback = None):
            data = cmd + endline
            self.write(data.encode("ascii"))
            return self.wait(response, timeout=timeout, callback=callback)

        def shell_cmd(self, cmd, timeout=10, initial=False):
            if initial:
                self.wait_prompt(initial)
            cmd = cmd.encode() + b"\r"
            self.ser.write(cmd)
            ret = self.wait_prompt(False).decode(errors="replace")
            ret = ret[len(cmd):]
            ret = ret[:-len(self.shell_prompt)-2]
            return ret

        def loop(self, use_stdin=False, break_after_uploads=False, timeout=-1):
            if not self.initial_loop_done:
                self.initial_loop_done = True
                self.opf.on_start()

            return_code = 0
            time_start = time.time()
            if use_stdin:
                old_settings = termios.tcgetattr(sys.stdin)
            try:
                if use_stdin:
                    tty.setcbreak(sys.stdin.fileno())

                if not self.hack("skipsync"):
                    self.sync()
                while True:
                    ret = None
                    if use_stdin :
                        line = b''
                        sym = b''
                        while sym != b'\n':
                            if select.select([self.ser], [], [], 0) == ([self.ser], [], []):
                                sym = self.ser.read(1)
                                line = line + sym
                                sys.stdout.write(str(sym, 'utf-8', errors='replace'))
                                sys.stdout.flush()
                            if select.select([sys.stdin], [], [], 0) == ([sys.stdin], [], []):
                                insym = sys.stdin.read(1)
                                self.ser.write(insym.encode())
                    else:
                        line = self.ser.readline()

                    if timeout >= 0 and time.time() - time_start > timeout:
                        return_code = 5
                        break

                    if line == b'':
                        continue
                    try:
                        line = line.decode().rstrip()
                    except:
                        continue

                    ret = self.opf.handle_line(line, use_stdin)
                    if type(ret) is int:
                        if not self.replay_till_the_end:
                            return ret
                        else:
                            return_code = ret

                    if break_after_uploads and len(self.runlist) == 0:
                        break

                    if self.replay:
                        if (self.ser.tell() == os.fstat(self.ser.fileno()).st_size):
                            print("== END OF LOG REACHED ==")
                            break
            finally:
                if use_stdin:
                    termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_settings)
                    return return_code


"""
### 6) Realiza un programa que pida al usuario un número entero entre el 0 al 9, y que mientras el número no sea correcto se repite el proceso.
# Luego debe comprobar si el número se encuentra en la lista de números [0,1,3,5,8] y notificarlo:
"""

import re

print("Bienvenido al programa.")

while True :

    selecion = input("Introduce un número del 0 al 9: ")

    if not re.match('^[0-9]$', selecion) :
        print("No has intrucido un número correcto. Por favor, repite el proceso.\n")
    else:
        break

lista_numeros = [0,1,3,5,8]

if selecion in lista_numeros :
    print( f"El número {selecion} existe en la lista de números")

else :
    print( f"El número {selecion} NO existe en la lista de números")

Python/UDEMY/Py3 Cero a Experto/Ejercicios Seccion 4/4.6-Ejercicio.py


import numpy as np
import pickle
import sys

from typing import List, Tuple
from pathlib import Path
from argparse import ArgumentParser
from matplotlib import pyplot as plt

from sklearn.metrics import silhouette_score, calinski_harabasz_score, davies_bouldin_score
from scipy.cluster.hierarchy import linkage, dendrogram

import logging
from loguru import logger

logger.remove()  # Remove the default logger
logger.add(sys.stdout, level="INFO")  # Add a console logger

def load_embeddings(file: Path) -> Tuple[List[Path], np.ndarray]:
    embeddings = pickle.loads(file.read_bytes())
    files, vectors = zip(*list(embeddings.items()))
    return files, vectors

def save_dendrogram(data: np.ndarray, dendrogram_file: Path, linkage_type: str):
    linkage_matrix = linkage(data, method=linkage_type)
    plt.figure(figsize=(12, 6))
    plt.title(f"Dendrogram ({linkage_type} linkage)")
    dendrogram(linkage_matrix, orientation='top')
    plt.savefig(dendrogram_file)
    plt.close()

if __name__ == "__main__":
    parser = ArgumentParser()
    parser.add_argument('embeddings_file', type=Path)
    parser.add_argument('--output-dir', type=Path, required=True, default='data/dendrograms/')
    args = parser.parse_args()

    logger.info("Loading embeddings...")
    _, vectors = load_embeddings(file=args.embeddings_file)

    dendrogram_linkages = ['ward', 'complete', 'centroid', 'median']

    logger.info("Saving Dendrograms for Different Linkage Methods...")

    for linkage_type in dendrogram_linkages:
        logger.info(f"Saving dendrogram for linkage: {linkage_type}")

        dendrogram_file = args.output_dir / f"dendrogram_{linkage_type}.png"
        save_dendrogram(vectors, dendrogram_file, linkage_type)

    logger.info("Dendrograms saved for different linkage methods.")