import ctypes
import socket
from .sockets import ThreadedUDPSocket
from .encoding import pack, unpack
from .packets import PACKETS

class ReliableUDPConnection:
	"""
	Packets will have the following structure:
	[uint protocol id]
	[uint sequence]
	[uint ack]
	[uint ack bitfields]
	[uint payload length]
	[uint payload type]
	[ ... payload bytes ... ]

	Thus header length is 24 bytes.
	"""
	def __init__(self, host, port, protocol_id = 1234):
		self.host = host
		self.port = port
		self.buffer = b''
		self.protocol_id: ctypes.uint32 = protocol_id
		self.ack: ctypes.uint32 = 0
		self.ack_bitfields: ctypes.uint32 = 0
		self.sequence: ctypes.uint32 = 0
		self.last_acked_seq: ctypes.uint32 = 0
		self.unacked = {}

		self.packet_recvd_buffer = []

	@classmethod
	def sequence_greater_than(klass, seq1: int, seq2: int):
		"""
		Returns true if seq1 is 'greater' than seq2.
		Sequence numbers are 32bit unsigned ints

		If their difference is greater than 1/2 the max sequence number (2^33 - 1)
		we consider the *smaller* one more recent
		"""
		return (
			( seq1 > seq2 ) and ( seq1-seq2 <= 2^32) or		# "close" together
			( seq1 < seq2 ) and ( seq2-seq1 >  2^32)		# "far" apart
		)

	def encode_packet(self, packet):
		payload = packet.encode()
		data = pack('IIIIII*',
			self.protocol_id, self.sequence,
			self.ack, self.ack_bitfields,
			len(payload), packet.packet_id,
			payload
		)
		return data

	def send(self, packet):
		data = self.encode_packet(packet)
		self.socket.sendto(data, (self.host, self.port))
		self.unacked[self.sequence] = data
		self.sequence += 1

	def read(self):
		if len(self.packet_recvd_buffer):
			yield self.packet_recvd_buffer.pop(0)

	def on_recv(self, addr, port, data):
		self.buffer += data
		packets, self.buffer = self.decode(self.buffer)
		self.packet_recvd_buffer.extend(packets)

	def decode(self, data):
		"""
		returns (list: Packet, trailing)
		"""
		if not data: return [], b''
		start_idx = data.find(pack('I', self.protocol_id))
		if start_idx > 0:
			# found, but after some other data? - discard "old" bytes
			print(f"Discarding {start_idx} bytes to resync")
			data = data[start_idx:]
		elif start_idx == -1:
			# could not find - wait for more data
			print(f"Discarding {start_idx} bytes to resync")
			return [], b''

		length = len(data)
		if length < 24: # less than packet header, wait for more data.
			return [], data

		proto_id, seq, ack_seq, ack_bits, payload_len, payload_type, data = unpack(
			"IIIIII*", data
		)

		remaining_len = len(data)

		if payload_len > remaining_len:
			raise ValueError("Packet too short")

		if proto_id != self.protocol_id:
			raise ValueError("Incorrect protocol id")

		payload, trailing_data = data[:payload_len], data[payload_len:]

		decoded_pkts = []
		if payload_type in PACKETS:
			pkt = PACKETS[payload_type].decode(payload)
			decoded_pkts.append(pkt)
		else:
			raise Exception(f"Undecoded packet of type {payload_type}")

		if ReliableUDPConnection.sequence_greater_than(seq, self.ack):
			# received a newer packet, ack it and shift ack bits
			self.ack_bitfields << (seq - self.ack)
			self.ack = seq
		elif ReliableUDPConnection.sequence_greater_than(seq, self.ack - 33):
			# set corresponding ack bit
			self.ack_bitfields & (1 << abs(self.ack - seq))
		else:
			pass  # older than the the previous 33 packets

		# sequence number was acked
		if ack_seq in self.unacked:
			del self.unacked[ack_seq]
		for ack_bit in range(0, 32):
			# is that seqnum's ack bit set?
			if ((ack_bits >> ack_bit) & 0x1):
				# it was acked
				ackable = (ack_seq - ack_bit)
				if ackable in self.unacked:
					del self.unacked[ackable]

		rest, trailing_data = self.decode(trailing_data)

		return (decoded_pkts + rest, trailing_data)

def Client(host, port, socketclass: socket.socket = ThreadedUDPSocket):
	conn = ReliableUDPConnection(host, port)
	conn.socket = socketclass(host, 0, on_recv = conn.on_recv)
	return conn

class Server:
	def __init__(self, host, port, connectionclass=ReliableUDPConnection, socketclass=ThreadedUDPSocket):
		self.socket = socketclass(host, port, on_recv=self.on_recv)
		self.connectionclass = connectionclass
		self.connections = {}

	def add_client(self, addr, port):
		conn = ReliableUDPConnection(addr, port)
		conn.socket = self.socket
		self.connections[(addr, port)] = conn

	def on_recv(self, addr, port, data):
		client = (addr, port)
		if client not in self.connections:
			self.add_client(addr, port)
		self.connections[client].on_recv(addr, port, data)

	def send_all(self, packet):
		for client, conn in tuple(self.connections.items()):
			conn.send(packet)

	def send_to(self, client, packet):
		self.connections[client].send(packet)

	def read(self):
		for client, conn in tuple(self.connections.items()):
			for out in conn.read():
				yield (client, out)


import numpy as np
from .optimizer import Optimizer

##########
# FAILED #
##########

# Inherits "Optimizer" for general methods and members
class Adadelta(Optimizer):

    def __init__(self, lr = 1.0, decay_rate = 0.99):
        self.lr = lr
        self.decay_rate = decay_rate

        self.avg_grad_weight = [] # decaying average of past squared weights
        self.avg_grad_bias = [] # decaying average of past squared biases

        self.last_weight_change = [] # the last weight changes squared (to substitute learning rate)
        self.last_bias_change = [] # the last bias changes squared (to substitute learning rate)

        self.epsilon = 1e-5
        self.first_update = True

    def set_layers(self, layers):
        super().set_layers(layers)

        for layer in self.layers:
            weight_zeros = np.zeros(((layer.weight.shape[0]), 1)).astype(self.data_type)
            self.avg_grad_weight.append(np.copy(weight_zeros))
            self.last_weight_change.append(np.copy(weight_zeros))

            bias_zeros = np.zeros(((layer.bias.shape[0]), 1)).astype(self.data_type)
            self.avg_grad_bias.append(np.copy(bias_zeros))
            self.last_bias_change.append(np.copy(bias_zeros))

    def update_params(self, x, y):

        # finds gradients (gradient_weight, gradient_bias)
        self.calculate_gradients(x, y)

        if self.first_update:
            for i in range(len(self.layers)):
                self.avg_grad_weight[i] = self.gradient_weight[i]**2
                self.avg_grad_bias[i] = self.gradient_bias[i]**2

        # updating the parameters (weights, biases)
        for i in range(len(self.layers)):
            weight_change = np.sqrt((self.last_weight_change[i] + self.epsilon) / (self.epsilon + self.avg_grad_weight[i])) * self.gradient_weight[i]
            self.layers[i].weight -= weight_change
            bias_change = np.sqrt((self.last_bias_change[i] + self.epsilon) / (self.epsilon + self.avg_grad_bias[i])) * self.gradient_bias[i]
            self.layers[i].bias -= bias_change

            # updating last parameter changes
            self.last_weight_change[i] = self.decay_rate * self.last_weight_change[i] + (1 - self.decay_rate) * np.sum(weight_change**2, axis = 1).reshape(-1,1)
            self.last_bias_change[i] = self.decay_rate * self.last_bias_change[i] + (1 - self.decay_rate) * np.sum(bias_change**2, axis = 1).reshape(-1,1)
        print (weight_change[1])

        if not self.first_update:
            # updating past average squared gradients
            for i in range(len(self.layers)):
                self.avg_grad_weight[i] = self.decay_rate * self.avg_grad_weight[i] + (1 - self.decay_rate) * np.sum(self.gradient_weight[i]**2, axis = 1).reshape(-1,1)
                self.avg_grad_bias[i] = self.decay_rate * self.avg_grad_bias[i] + (1 - self.decay_rate) * np.sum(self.gradient_bias[i]**2, axis=1).reshape(-1,1)

        if self.first_update:
            self.first_update = False


import unittest

from ..quantities import LcQuantity
from ...archives import BasicArchive, Qdb

from ...lcia_engine import IPCC_2007_GWP

mass_uuid = '93a60a56-a3c8-11da-a746-0800200b9a66'

class QuantitiesTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        super(QuantitiesTest, cls).setUpClass()
        cls.I = Qdb.new()
        cls.Q = BasicArchive.from_file(IPCC_2007_GWP)
        cls.gwp = cls.Q['Global Warming Air']

    def test_default_conversion(self):
        a = LcQuantity.new('Floogles', 'fl')
        self.assertTrue(a.has_property('UnitConversion'))
        self.assertEqual(a.convert('fl'), 1.0)

    def test_retrieve_quantity(self):
        self.assertIsNotNone(self.gwp)
        self.assertEqual(self.gwp.entity_type, 'quantity')

    def test_is_lcia_method(self):
        self.assertTrue(self.gwp.is_lcia_method)

    def test_convert(self):
        mass = self.I[mass_uuid]
        self.assertAlmostEqual(mass.convert('lb av'), 0.453592, 6)
        self.assertAlmostEqual(mass.convert(to='lb av'), 2.20462262, 6)

    def test_convert_missing_refunit(self):
        q = self.I.make_interface('quantity').get_canonical('electric energy')
        self.assertTrue(q.has_property('UnitConversion'))
        self.assertNotIn(q.unit, q['UnitConversion'])
        self.assertEqual(q.convert(), 1.0)
        self.assertEqual(q.convert(to='MJ'), 3.6)

    def test_valid_make_ref(self):
        gr = self.gwp.make_ref(self.Q.query)
        self.assertIsInstance(gr.name, str)

if __name__ == '__main__':
    unittest.main()


from time import localtime

year_str = input('请输入西元年份：')
if year_str.isdecimal():
    year = int(year_str)
else:
    year = localtime().tm_year

if year % 400 == 0: # 规则 1
    # 西元年份可被 400 整除者为润年
    print(f"{year} 是润年")
elif year %4 == 0 and year % 100 != 0: # 规则 2
        # 西元年份可被 4 整除但不能被 100 整除者为润年
        print(f'{year} 年是润年')
else: # 规则 3
    # 其余为平年
    print(f'{year} 是平年')


from django.urls import path
from start import views

urlpatterns = [
    path('', views.start),
    path('candidates', views.getCandidates),
    path('add_candidate', views.addCandidate),
    path('change_candidate/<int:pk>', views.changeCandidate),
    path('change_job/<int:pk>', views.changeJob),
    path('change_jobseek/<int:pk>', views.changeJobSeek),
    path('add_jobseek', views.addJobSeek),
    path('jobs', views.getJob),
    path('jobseek', views.getJobSeek),
    path('add_job', views.addJob),
    path('cerjobseek/<int:pk>', views.getCerJobSeek, name='cerjobseek'),
    path('cercandidate/<int:pk>', views.getCerCandidate, name='cercandidate'),
    path('cerjob/<int:pk>', views.getCerJob, name='cerjob'),
    path('candidates', views.getCandidates),
    path('jobs', views.getJob),
    path('jobseek', views.getJobSeek),
    path('cercandidate/get_cv/<int:pk>',views.get_cv),
    path('parser', views.parser),
    path('parseHTML',views.parseHTML)
]


# -*- coding:UTF-8 -*-

import tensorflow as tf
from tensorflow import keras
import tushare as ts
import numpy as np
import pandas as pd
import os
import time
import sys
from absl import app
from absl import flags
import random
sys.path.append("..")
from common import base_common
from common import np_common
from common.const_def import *
import tushare_data
import feature
import dl_model
import trade_base

reload(sys)
sys.setdefaultencoding('utf-8')
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

FLAGS = flags.FLAGS

# min_wave_width_left = 2
# min_wave_width_right = 2
# trade_off_threshold = 0
# up_100avg_condition = True
# up_200avg_condition = False
# wave_index = 'close'
# save_original_dataset = False

EXTREME_NONE = 0
EXTREME_PEAK = 1
EXTREME_VALLEY = 2
EXTREME_STR = ['NONE', 'PEAK', 'VALLEY']

STATUS_NONE = 0
STATUS_UP = 1
STATUS_DOWN = 2
WAVE_STATUS_STR = ['NONE', 'UP', 'DOWN']

# wave_test_dataset_sample_num = 5

class ExtremeWave(trade_base.TradeBase):
    def __init__(self,
                 o_data_source,
                 o_feature,
                 min_wave_width_left = 2,
                 min_wave_width_right = 2,
                 up_100avg_condition = True,
                 trade_off_threshold = 0,
                 cut_loss_ratio = 0.1,
                 dataset_sample_num = 5):
        class_name = 'extreme_wave'
        # inherit class setting
        self.min_wave_width_left = min_wave_width_left
        self.min_wave_width_right = min_wave_width_right
        self.up_100avg_condition = up_100avg_condition
        self.trade_off_threshold = trade_off_threshold
        app_setting_name = '%u_%u_%u_%u_%u' % (min_wave_width_left,
                                         min_wave_width_right,
                                         int(up_100avg_condition),
                                         trade_off_threshold,
                                         dataset_sample_num)
        super(ExtremeWave, self).__init__(o_data_source,
                                      o_feature,
                                      class_name,
                                      app_setting_name,
                                      0.0,
                                      dataset_sample_num,
                                      cut_loss_ratio)

    # 返回 extreme_flag, extreme_value
    def FitExtreme(self, pp_data, data_len, day_index):
        if (data_len - day_index) < (self.min_wave_width_left + self.min_wave_width_right + 1):
            return EXTREME_NONE, 0.0
        middle_index = day_index + self.min_wave_width_right
        middle_value = pp_data[middle_index][PPI_close]
        is_peak = True
        is_valley = True
        for iloop in range(1, self.min_wave_width_left + 1):
            temp_value = pp_data[middle_index + iloop][PPI_close]
            if middle_value > temp_value:
                is_valley = False
            elif middle_value < temp_value:
                is_peak = False
        for iloop in range(1, self.min_wave_width_right + 1):
            temp_value = pp_data[middle_index - iloop][PPI_close]
            if middle_value >= temp_value:
                is_valley = False
            elif middle_value <= temp_value:
                is_peak = False
        # 计算 extreme_flag 当天峰谷标志
        if is_peak:
            extreme_flag = EXTREME_PEAK
        elif is_valley:
            extreme_flag = EXTREME_VALLEY
        else:
            extreme_flag = EXTREME_NONE
        return extreme_flag, middle_value

    def TradePP(self, pp_data):
        data_len = len(pp_data)
        if data_len == 0:
            return
        self.wave_data = np.zeros((data_len), dtype=np.int)
        self.trade_flag = np.zeros((data_len), dtype=np.int)
        last_peak = []
        last_valley = []
        trade_off_count = 0
        last_extreme = EXTREME_NONE
        current_status = STATUS_NONE
        last_flag = TS_NONE

        for day_loop in reversed(range(0, data_len)):
            day_index_reversed = data_len - day_loop - 1  # day_loop 倒数的 index
            close = pp_data[day_loop][PPI_close]
            close_10_avg = pp_data[day_loop][PPI_close_10_avg]
            close_100_avg = pp_data[day_loop][PPI_close_100_avg]
            fit_flag, e_value = self.FitExtreme(pp_data, data_len, day_loop)

            # set self.trade_flag
            if fit_flag == EXTREME_PEAK:
                if len(last_peak) > 0 and e_value <= last_peak[0]:
                    self.trade_flag[day_loop] = TS_OFF
            elif fit_flag == EXTREME_VALLEY:
                if len(last_valley) > 0 and e_value <= last_valley[0]:
                    self.trade_flag[day_loop] = TS_OFF

            if len(last_valley) > 0 and close < last_valley[0]:
                self.trade_flag[day_loop] = TS_OFF
            if (len(last_peak) > 0 and close > last_peak[0]) and \
               ((not self.up_100avg_condition) or (close_10_avg > close_100_avg)) and \
               (trade_off_count >= self.trade_off_threshold):
                self.trade_flag[day_loop] = TS_ON

            # set last_peak,last_valley,last_extreme,self.wave_data
            if fit_flag == EXTREME_PEAK:
                last_peak.insert(0, e_value)
                last_extreme = EXTREME_PEAK
                current_status = WS_DOWN
            elif fit_flag == EXTREME_VALLEY:
                last_valley.insert(0, e_value)
                last_extreme = EXTREME_VALLEY
                current_status = WS_UP
            self.wave_data[day_loop] = current_status

            # set trade_off_count
            if last_flag == TS_ON:
                trade_off_count = 0
            else:
                trade_off_count += 1
            if self.trade_flag[day_loop] != TS_NONE:
                last_flag = self.trade_flag[day_loop]

    def TradeNextStatus(self, pp_data, day_index):
        trade_flag = self.trade_flag[day_index]
        return trade_flag

def main(argv):
    del argv

    o_data_source = tushare_data.DataSource(20000101, '', '', 1, 20120101, 20200106, False, False, True)
    o_feature = feature.Feature(30, feature.FUT_D5_NORM, 1, False, False)
    # o_feature = feature.Feature(30, feature.FUT_D5_NORM, 1, False, False)
    # o_feature = feature.Feature(30, feature.FUT_5REGION5_NORM, 5, False, False)
    # o_feature = feature.Feature(30, feature.FUT_2AVG5_NORM, 5, False, False)
    o_wave = ExtremeWave(o_data_source, o_feature, 2, 2, False, 0, 0.1, 5)
    split_date = 20180101
    o_dl_model = dl_model.DLModel('%s_%u' % (o_wave.setting_name, split_date),
                                  o_feature.feature_unit_num,
                                  o_feature.feature_unit_size,
                                  32, 10240, 0.004, 'mean_absolute_tp0_max_ratio_error')
    if FLAGS.mode == 'data':
        o_data_source.DownloadData()
        o_data_source.UpdatePPData()
    elif FLAGS.mode == 'testall':
        o_wave.TradeTestAll()
    elif FLAGS.mode == 'test':
        o_wave.TradeTestStock(FLAGS.c, FLAGS.show)
    elif FLAGS.mode == 'show':
        o_wave.ShowTradePP(FLAGS.c)
    elif FLAGS.mode == 'train':
        tf, tl, vf, vl, td = o_wave.GetDataset(split_date)
        tl = tl * 100.0
        vl  = vl * 100.0
        o_dl_model.Train(tf, tl, vf, vl, FLAGS.epoch)
    elif FLAGS.mode == 'rtest':
        tf, tl, tf, tl, ta = o_wave.GetDataset(split_date)
        o_dl_model.LoadModel(FLAGS.epoch)
        o_wave.RTest(o_dl_model, tf, ta, False)

    exit()

if __name__ == "__main__":
    flags.DEFINE_string('mode', 'test', 'test | testall | train')
    flags.DEFINE_string('c', '000001.SZ', 'ts code')
    flags.DEFINE_integer('epoch', 100, 'train or rtest epoch')
    flags.DEFINE_boolean('show', False, 'show trade record')
    app.run(main)


from migen import *
from litex.soc.interconnect.stream import Endpoint

class VideoStream(Module):
    def __init__(self):
        # VGA output
        self.red   = red   = Signal(8)
        self.green = green = Signal(8)
        self.blue  = blue  = Signal(8)
        self.data_valid = data_valid = Signal()

        self.source = source = Endpoint([("data", 32)])

        self.sync.pixel += [
            source.valid.eq(data_valid),
            source.data.eq(Cat(red, green, blue)),
        ]


"""
load data from the csv & sort_values
adds days delta calc vs. failure date if available
"""
import time
import pickle
import pandas as pd
import numpy as np
from tqdm import tqdm
from hlp_analysis import xstat

def days_delta_calc(df):
  """ given data_frame returns days from the failure date  """
  return df.date.progress_apply(lambda x: (x - df.loc[df[df.failure].index[0]].date).days)

def load_parse_save(in_file_name='./predictive_maintenance.csv', save_file='', debug=False):
  """
  read in input csv file & returns pased dateframe & saves
  a save_file, if save_file name is specificed
  """
  beg = time.time()
  dt_parser = lambda x: pd.datetime.strptime(x, "%m/%d/%y")
  df = pd.read_csv(in_file_name, parse_dates=[0], date_parser=dt_parser)
  #df.date = df.date.progress_apply(lambda x: pd.to_datetime(x, format="%m/%d/%Y"))
  #sort by device & date
  pm_df = df.sort_values(['device', 'date'])
  pm_df = pm_df.reset_index(drop=True)
  df = None
  pm_df.failure = pm_df.failure.apply(lambda x: True if x > 0 else False)
  # adds following new columns
  pm_df['daysDelta'] = np.nan # date from the failure date
  # convert date to pandas datetime for calculation
  # get list of failed devices for special dateDelta processing
  dev_failed = pm_df.device[pm_df.failure].tolist()
  # loop & if device is
  for ky in dev_failed:
    pm_df.loc[pm_df.device == ky, 'daysDelta'] = days_delta_calc(pm_df[pm_df.device == ky])
    beg = time.time()
  pm_df['Fail_set'] = pm_df.daysDelta.apply(lambda x: True if x <= 0.0 else False)
  grp = pm_df.groupby(['device', 'Fail_set'])
  if debug:
    print('Processing csv at {} took {:,.2f} sec'.format(time.ctime(), time.time()-beg))
  g_stat_df = grp.apply(lambda x: xstat(x, False, False))
  # saving dict of processed_csv, group, & stat_df from group
  ret_dict = {'csv_df': pm_df, 'grp_dev_fail': grp, 'g_stat_df': g_stat_df}
  if debug:
    print('Processing g_df at {} took {:,.2f} sec'.format(time.ctime(), time.time()-beg))
    print(g_stat_df.head(15))
  if save_file != '':
    with open(save_file, 'wb') as fw:
      pickle.dump(ret_dict, fw)
  return ret_dict

if __name__ == '__main__':
  pkl_file = './parsed_dataframe.pkl'
  tqdm.pandas(desc='load & parse csv')
  pkl_dict = load_parse_save(save_file='./parsed_dataframe.pkl', debug=True)


# -*- coding: utf-8 -*-
"""
Created on Thu Apr 25 12:37:32 2019

@author: victor.bertoldo
"""
# Importação das bibliotecas
import pandas as pd
import numpy as np

#Import do arquivo csv de mesmo diretorio
base = pd.read_csv('iris.csv')
# Lendo o dataset
base
#retorna o tamanho do dataset
base.shape

#Impede de mudar o valor randomico a cada execução
np.random.seed(2345)
#Faz a escolha randomica -- o a gera os valores (flag), size é o tamanho do dataset, replace = false ele impede q o valor seja usado
# novamente, p é a problabilidade o seu somatorio tem q ser igual a 1
amostra = np.random.choice(a = [0,1], size = 150, replace = True,
                            p=[0.5, 0.5])
len(amostra)
len(amostra[amostra == 1])
len(amostra[amostra == 0])


from discord.ext import commands
import discord

class Bot(commands.Bot):

    def __init__(self, command_prefix = '!', self_bot = False):
        intents = discord.Intents.all()
        intents.messages = True
        commands.Bot.__init__(self, command_prefix = command_prefix, intents = intents, self_bot = self_bot)

    async def on_ready(self):
        print(f'{self.user} is ready and online!')

    async def on_command_error(self, ctx, exception) -> None:
        print(f'exception in command {ctx.command}: {exception}')
        if ctx.command == None:
            await ctx.channel.send(f'{exception}')
        return await super().on_command_error(ctx, exception)


import requests
import datetime
import os
import csv
import pandas as pd
import pickle

def get_time():
    date = datetime.datetime.now()
    return date.strftime('%x').replace('/', '_')

def load_obj(date, country=None):
    """
    Loads existing pickle file to obj.
    :param date: search by date string format: 'DD_MM_YY'
    :param country: If country supplied will look in the output folder for specific country dir
    :return: load pickle file to variable
    """
    # Define paths
    parent = os.path.sep.join(os.getcwd().split(os.path.sep)[:-1])  # Get parent folder
    output_path = os.path.join(os.path.join(parent, 'output'))
    history_path = os.path.join(output_path, 'all_history')
    # Load data
    if country is None:  # If country was not supplied means all history request
        for files in os.listdir(history_path):
            if files.replace('.pickle', '') == date:
                with open(os.path.join(history_path, files), 'rb') as load_file:
                    return pickle.load(load_file)
    else:
        for r, d, f in os.walk(output_path):
            print(f'root: {f}, dir: {d}, file:{f}')
            if (len(r) and country in r) and (len(f) and date + '.pickle' in f):
                with open(os.path.join(r, date + '.pickle'), 'rb') as load_file:
                    return pickle.load(load_file)
    print(f'{date}.pickle not found!')

class request:
    def __init__(self, name=get_time()):
        self.name = name
        self.base = 'https://corona.lmao.ninja/'  # API - endpoint
        self.timePeriod = None
        self.country = None
        self.URL = None
        self.response = None
        self.data = None
        self.df = None

    def create_historical_country_request(self, countryName, daysBack=30):
        """
        Creates a request for historical data for specific country
        for specific duration. Default set to 30 days
        """
        historical_country = f'v2/historical/{countryName}?lastdays={daysBack}'
        self.URL = self.base + historical_country
        self.timePeriod = daysBack
        self.country = countryName
        self.print_url()

    def create_historical_all_request(self, daysBack=30):
        """
        Creates a request for historical data for all countries for specific duration.
        Default set to 30 days
        """
        historical_all = f'v2/historical?lastdays={daysBack}'
        self.URL = self.base + historical_all
        self.timePeriod = daysBack
        self.print_url()

    def send_request(self):
        """
        Sends the request and stores the data in self.data as DICT
        """
        self.response = requests.get(url=self.URL)
        print(f'Request result: {self.response}')
        self.df = pd.read_json(self.response.text)
        self.data = self.response.json()

    def print_url(self):
        """
        Prints the url when the request is created
        """
        print(f'URL set to: {self.URL}')

    def output_data(self):
        parent = os.path.sep.join(os.getcwd().split(os.path.sep)[:-1])  # Get parent folder
        output_path_dir = os.path.join(os.path.join(parent, 'output'))  # ./data_func/output
        if not os.path.exists(output_path_dir):  # check if output folder exists
            os.mkdir(output_path_dir)  # If not create output folder
        country_dir = os.path.join(output_path_dir, self.data['country'])  # ./data_func/output/Israel
        if not os.path.exists(country_dir):  # check if country folder exists
            os.mkdir(country_dir)
        # timeline_dir = os.path.join(country_dir, 'timeline')  # ./data_func/output/Israel/timeline
        # if not os.path.exists(timeline_dir):
        #     os.mkdir(timeline_dir)

        for sir in self.data['timeline'].items():
            with open(os.path.join(country_dir, f'{sir[0]}_{self.name}.csv'), 'w') as output_file:
                header = ['date', 'value']
                dict_writer = csv.DictWriter(output_file, fieldnames=header)

                dict_writer.writeheader()
                for k, v in sir[1].items():
                    dict_writer.writerow({'date': k, 'value': v})

    def save_obj(self):
        """
        Saves the created class to pickle file named as the class name.
        The path is set to ./output/all_history as default.
        If country specified in the class the path is set ot ./output/{country_name}
        """
        parent = os.path.sep.join(os.getcwd().split(os.path.sep)[:-1])  # Get parent folder
        output_path_dir = os.path.join(os.path.join(parent, 'output'))  # ./data_func/output
        if not os.path.exists(output_path_dir):  # check if output folder exists
            os.mkdir(output_path_dir)  # If not create output folder
        history_path = os.path.join(output_path_dir, 'all_history')  # ./data_func/output/all_history
        if self.country is not None:
            history_path = os.path.join(output_path_dir, self.country)
        if not os.path.exists(history_path):
            os.mkdir(history_path)
        # Save pickle obj
        with open(os.path.join(history_path, self.name + '.pickle'), 'wb') as save_file:
            pickle.dump(self, save_file)
            print(f'Saved class: {self.name}.pickle to: {history_path}')
        # Save Data frame as csv
        with open(os.path.join(history_path, self.name + '.csv'), 'w') as save_file:
            self.export_csv(os.path.join(history_path, self.name + '.csv'))
            print(f'Saved class: {self.name}.csv to: {history_path}')

    def export_csv(self, path):
        if self.df is not None:
            self.df.to_csv(path, index=False, header=True)

# Example
# x = request()
# x.create_historical_all_request(90)
# x.send_request()
# x.save_obj()
# print(x)

# x = load_obj('04_13_20')
# print(x)


import uuid

import numpy as np

class LearningModel:
    def __init__(self, w_vector: [float], d: float, model_id=None):
        self.model_id = model_id if model_id else str(uuid.uuid4())
        self.w_vector = w_vector
        self.d = d

        self.score = 0
        self.processed = False

        self.generation = None

    def apply(self, x_vector: [float]):
        x = np.array(x_vector).reshape(1, 3)
        w = np.array([self.w_vector])

        product = x * w + self.d
        average = np.sum(product, axis=1) / x_vector.shape[1]
        return np.tan(average).ravel()

    def __hash__(self):
        return hash((str(self.w_vector), self.d))

    def __eq__(self, other):
        if not isinstance(other, type(self)):
            return NotImplemented
        return self.w_vector == other.w_vector and self.d == other.d


#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on Tue Apr 14 17:20:17 2020

@author: Edyta Bosacka
"""

import os
import librosa.display
import matplotlib.pyplot as plt
import numpy
import librosa

directory = 'E:/Nagrania'
resultsDirectory = directory + "/MFCCsresult"

for filename in os.listdir(directory):
    if filename.endswith('.wav'):
        y,sr = librosa.load(directory + "/" + filename)
        mfccs = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=13, win_length= 1024, hop_length = 512)
        print(mfccs)
        plt.figure(figsize=(10,4))
        librosa.display.specshow(mfccs, x_axis = 'time')
        plt.colorbar()
        plt.title('MFCC')
        plt.tight_layout()
        plt.show()

        outputFile = resultsDirectory + "/" + "Mfcc" + ".csv"
        file = open(outputFile, 'a')
        file.write(filename+"\n")
        numpy.savetxt(file, mfccs, delimiter=",") #save MFCCs as .csv
        file.close() # close file


from copy import deepcopy
import logging
import os

from flamingo.core.errors import MultipleObjectsReturned, ObjectDoesNotExist
from flamingo.core.data_model import Content, Q
from flamingo.core.utils.string import slugify

logger = logging.getLogger('flamingo.plugins.Menu')

def is_active(section, menu, content):
    def _contains(menu, content):
        for i in menu:
            if i is content:
                return True

            if isinstance(i, (list, tuple, )):
                if _contains(i, content):
                    return True

        return False

    if isinstance(section, Section) and section.content is content:
        return True

    if (isinstance(section, Section) and
       isinstance(content['menu_path'], list) and
       section in content['menu_path']):

        return True

    if isinstance(menu, Content):
        return menu is content

    return _contains(menu, content)

def is_dict(v):
    return isinstance(v, dict)

def is_list(v):
    return isinstance(v, list)

class Section:
    def __init__(self, name, url=''):
        self.name = name
        self.url = url or slugify(self.name)
        self.content = None

    def __str__(self):
        return self.name

    def __repr__(self):
        return "<Section('{}', url='{}')>".format(self.name, self.url)

class Menu:
    THEME_PATHS = [
        os.path.join(os.path.dirname(__file__), 'theme'),
    ]

    def templating_engine_setup(self, context, templating_engine):
        templating_engine.env.globals['is_active'] = is_active
        templating_engine.env.globals['is_dict'] = is_dict
        templating_engine.env.globals['is_list'] = is_list

    def contents_parsed(self, context):
        create_indices = context.settings.get('MENU_CREATE_INDICES', False)

        index_template = context.settings.get(
            'MENU_INDEX_TEMPLATE', 'menu/index.html')

        if not hasattr(context.settings, 'MENU'):
            context.settings.MENU = {
                'main': [],
            }

        self.menu = deepcopy(context.settings.MENU)

        def resolve_links(menu):
            for item in menu:
                section_args, url = item

                if isinstance(url, list):
                    # setup section
                    if not isinstance(section_args, (list, tuple)):
                        section_args = [section_args]

                    if not isinstance(item[0], Section):
                        item[0] = Section(*section_args)

                    resolve_links(url)

                else:
                    logger.debug('resolving %s', item[1])

                    try:
                        if isinstance(item[1], (Content, Section, )):
                            logger.debug('resolving skipped')

                            return

                        if isinstance(item[1], str):
                            lookup = Q(path=item[1])

                        elif not isinstance(item[1], Q):
                            lookup = Q(item[1])

                        else:
                            lookup = item[1]

                        item[1] = context.contents.get(lookup)

                        logger.debug('%s -> %s', lookup, item[1])

                    except ObjectDoesNotExist:
                        logger.error(
                            "no content with %s %s found",
                            'path' if isinstance(lookup, str) else 'lookup',
                            lookup or repr(lookup),
                        )

                    except MultipleObjectsReturned:
                        logger.error(
                            "multiple contents found with %s %s found",
                            'path' if isinstance(lookup, str) else 'lookup',
                            lookup or repr(lookup),
                        )

        # setup menus
        if isinstance(self.menu, list):
            self.menu = {
                'main': self.menu,
            }

        elif 'main' not in self.menu:
            self.menu['main'] = []

        # resolve links
        for menu_name, menu in self.menu.items():
            resolve_links(menu)

        # create section indices
        def create_section_indices(menu, path):
            current_section = None

            def gen_content(menu, path):
                url = '/'

                for section in path + [current_section]:
                    url = os.path.join(url, section.url)

                url = os.path.join(url, 'index.html')

                content = Content(
                    type='menu/index',
                    title=current_section.name,
                    url=url,
                    output=url[1:],
                    menu=menu,
                    template=index_template,
                    menu_path=path,
                )

                context.contents.add(content)
                current_section.content = content

            for item in menu:
                if isinstance(item, Section):
                    current_section = item

                    gen_content(menu[1:], path)

                elif isinstance(item, Content):
                    item['menu_path'] = path

                elif isinstance(item, (list, tuple)):
                    if current_section:
                        create_section_indices(item, path+[current_section])

                    else:
                        create_section_indices(item, path)

        if create_indices:
            for menu_name, menu in self.menu.items():
                create_section_indices(menu, path=[])


import gym
import numpy as np
from gym.wrappers import Monitor

# A states consists of the last images_per_state
class CarRacingEnv(object):
    def __init__(self, vid_path, action_mode, images_per_state=4, time_diff_stack=1, num_skip=2, color="bw"):
        self.n = images_per_state
        self.t = time_diff_stack
        self.num_skip = num_skip
        env = gym.make('CarRacing-v0')
        self.env = Monitor(env, vid_path, force=True)

        self.color = color
        env_obs_shape = self.env.observation_space.shape

        self.obs_shape = (self.n, 3 if color=="rgb" else 1, env_obs_shape[0], env_obs_shape[1])

        imgs_to_store = 1+self.t*(self.n-1)
        self.last_nt_images = np.zeros((imgs_to_store, self.obs_shape[1], self.obs_shape[2], self.obs_shape[3]))

        self.index_to_action = CarRacingEnv.make_action_space(action_mode)

    def new_epsiode(self):
        self.last_nt_images[:, :, :, :] = 0

    def process_img(self, img):
        img = np.transpose(img, (2, 0, 1))

        if self.color == "rgb":
            mean = np.array([0.485, 0.456, 0.406]).reshape((3,1,1))
            std = np.array([0.229, 0.224, 0.225]).reshape((3,1,1))
            norm_img = (img - img.mean(axis=(1,2)).reshape(3,1,1))/img.std(axis=(1,2)).reshape(3,1,1)
            processed_img = norm_img*std + mean
        else:
            img = img.astype(float)

            processed_img = 0.2989 * img[0] + 0.5870 * img[1] + 0.1140 * img[2]
            processed_img /= 255.0
            processed_img = processed_img.reshape((1, 96, 96))

        return processed_img

    @staticmethod
    def make_action_space(action_mode):
        choices = [(0, -1, 1), (0, 1), (0, 0.2)]

        # One or more sub-actions
        if action_mode == "mutually_exclusive":
            actions = [np.zeros(len(choices)),]
            for i, choice in enumerate(choices):
                for val in choice:
                    if val != 0:
                        code = np.zeros(len(choices))
                        code[i] = val
                        actions.append(code)
            return np.stack(actions)
        # Any combination of sub-actions
        elif action_mode == "decoupled":
            actions = []
            code = np.zeros(len(choices))
            for choice_0 in choices[0]:
                code_0 = code.copy()
                code_0[0] = choice_0

                for choice_1 in choices[1]:
                    code_1 = code_0.copy()
                    code_1[1] = choice_1

                    for choice_2 in choices[2]:
                        code_2 = code_1.copy()
                        code_2[1] = choice_2
                        actions.append(code_2)
            return np.stack(actions)
        else:
            assert False, f"Action Mode of {action_mode} is not valid"

    def action_space_size(self):
        return len(self.index_to_action)

    def step(self, action_index, is_train=True):
        action = self.index_to_action[action_index]

        R = 0
        for _ in range(self.num_skip+1):
            self.env.render()
            next_img, r, done, info = self.env.step(action)
            R += r

            next_img = self.process_img(next_img)
            self.last_nt_images[1:, :, :, :] = self.last_nt_images[:-1, :, :, :].copy() # Numpy requires clone
            self.last_nt_images[0, :, :, :] = next_img

            if done:
                break

        output = self.last_nt_images.copy()[::self.t, :, :, :]

        if done: self.new_epsiode()

        return output, R, done, info

    def reset(self):

        img = self.env.reset()
        img = self.process_img(img)
        self.new_epsiode()

        self.last_nt_images[0, :, :, :] = img
        output = self.last_nt_images[::self.t].copy()

        return output

    def close(self):
        self.env.close()

if __name__ == "__main__":
    print( CarRacingEnv.make_action_space("decoupled") )
    print( type(CarRacingEnv.make_action_space("decoupled")) )
    print( CarRacingEnv.make_action_space("mutually_exclusive") )
    print( type(CarRacingEnv.make_action_space("mutually_exclusive")) )


import numpy as np
import pandas as pd
import matplotlib.pylab as plt
import seaborn as sns

def load_data():
    train_data_2011 = pd.read_csv('./data/gt_2011.csv').values
    train_data_2012 = pd.read_csv('./data/gt_2012.csv').values
    train_data_2013 = pd.read_csv('./data/gt_2013.csv').values
    train_data_2014 = pd.read_csv('./data/gt_2014.csv').values
    train_data_2015 = pd.read_csv('./data/gt_2015.csv').values

    data = np.vstack((train_data_2011,train_data_2012,train_data_2013,train_data_2014,train_data_2015))

    feature_name = [column for column in pd.read_csv('./data/gt_2011.csv')]

    return data,feature_name

def plot_feature_label_relation(data,feature_name):

    for a in range(-2,0):
        fig, axs = plt.subplots(3,3,figsize=(12,8))
        fig.suptitle('Relation between Feature and %s subplots'%(feature_name[a]))
        for i in range(3):
            for j in range(3):
                axs[i,j].scatter(data[:,3*i+j],data[:,a],s=0.1)
                axs[i,j].set(xlabel=feature_name[3*i+j], ylabel=feature_name[a])
        plt.tight_layout()
        plt.savefig('./result/Feature_%s_relation_subplots.png'%(feature_name[a]))
        plt.show()

if __name__ == '__main__':
    data,feature_name = load_data()
    plot_feature_label_relation(data,feature_name)


# This creates a table in a PostGIS enabled PostgreSQL database
# with SMOS points and (circular) pixels.

# table name
table = 'smos_grid_ia'

# set bounds for Iowa
ia = {
    'n':  44.0, 's':  40.0,
    'e': -90.0, 'w': -97.0,
}

# connect to the database
import psycopg2

conn = psycopg2.connect('')
cur = conn.cursor()

# drop the table (uncomment if needed)
#query = 'DROP TABLE %s' % (table)
#cur.execute(query)

# create the table
query = '''
CREATE TABLE %s (
dgg INT PRIMARY KEY,
longitude DOUBLE PRECISION,
latitude DOUBLE PRECISION)
''' % (table)
cur.execute(query)

# loop through isea4h9 grid and store the pixels inside the bounds
for line in open('isea4h9.txt'):
    col = line.split()

    # get the DGG, longitude, and latitude
    dgg = int(col[0])
    lon = float(col[1])
    lat = float(col[2])

    # skip points outside of the bounds
    if not ((lat > ia['s'] and lat < ia['n']) and
            (lon > ia['w'] and lon < ia['e'])): continue

    # store points inside bounds
    query = 'INSERT INTO ' + table + ' (dgg, longitude, latitude) VALUES (%s, %s, %s)'
    cur.execute(query, (dgg, lon, lat))

conn.commit()

# create the point geometry
query = "SELECT AddGeometryColumn('%s', 'geom', 4326, 'POINT', 2)" % (table)
cur.execute(query)

query = 'UPDATE %s SET geom = ST_SetSRID(ST_MakePoint(longitude, latitude), 4326)' % (table)
cur.execute(query)

conn.commit()

# create the pixel geometry (use UTM zone 15N, SRID 26915)
query = "SELECT AddGeometryColumn('%s', 'geom_43km', 4326, 'POLYGON', 2)" % (table)
cur.execute(query)

radius = 43000./2 # 43 km diameter
query = '''
UPDATE %s
SET geom_43km = ST_Transform(ST_Buffer(ST_Transform(geom, 26915), %f), 4326)
''' % (table, radius)
cur.execute(query)

conn.commit()


# python 3.6
import random
import time
import pandas as pd

from paho.mqtt import client as mqtt_client
from deepface import DeepFace

# broker = '3.122.69.139'
broker = '127.0.0.1'
port= 1883
topic='codigoIoT/mqtt/python'
topic2='codigoIoT/mqtt/index'
client_id = f'python-mqtt-{random.randint(0, 1000)}'

def connect_mqtt():
    def on_connect(client, userdata, flags, rc):
        if rc == 0:
            print("Connected to MQTT Broker!")
        else:
            print("Failed to connect, return code %d\n", rc)

    client = mqtt_client.Client(client_id)
    #client.username_pw_set(username, password)
    client.on_connect = on_connect
    client.connect(broker, port)
    return client

def publish(client,mensaje):
    #while True:
    time.sleep(1)
    msg = mensaje
    result = client.publish(topic, msg)
    time.sleep(1)
    # result2 = client.publish(topic2, indice)
    # time.sleep(1)
    print (result)
    # result: [0, 1]
    status = result[0]
    if status == 0:
        print(f"Send `{msg}` to topic `{topic}`")
    else:
        print(f"Failed to send message to topic {topic}")

print("Buscando rostro...")

df = DeepFace.find(img_path="/home/dafne/Documents/GitHub/apertura-puertas-reconocimiento-facial/deepface/faces/persona3.png", db_path="/home/dafne/Documents/GitHub/apertura-puertas-reconocimiento-facial/deepface/mydb", enforce_detection="false")

print("Resultado...")
print(df)
json_view= df.to_json(orient="index")
print("La expresion en JSON de los resultados es: ...")
# (df.identity[0])
print(json_view)

def run():
    client = connect_mqtt()
    client.loop_start()
    #publish(client)
    #publish2(client,df.identity[0])
    publish(client,json_view)

if __name__ == '__main__':
    run()


# -*- coding: utf-8 -*-
"""
Created on Fri May  5 18:41:06 2023

@author: mustafa
"""

import pydirectinput
import time
from PIL import ImageGrab
from PIL import Image
from numpy import asarray
import pytesseract

pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract'

time.sleep(1)

while True:
    try:
        '''
        imagehp = ImageGrab.grab(bbox=(350, 250, 475,260))
        imagehp.save('hp.png')
        hp = pytesseract.image_to_string(r'hp.png')
        hp=hp.replace(" ", "")
        hp=hp.replace("\n", "")
        hp = hp[0:3]
        hp = int(hp)
        '''
        imagemp = ImageGrab.grab(bbox=(350, 60, 440,92))
        imagemp.save('mp.png')
        mp = pytesseract.image_to_string(r'mp.png')
        mp=mp.replace(" ", "")
        mp=mp.replace("\n", "")
        mp = mp[0:4]
        mp = int(mp)

        if mp < 2750:
            pydirectinput.press('2')
        '''

        if hp < 515:
            pydirectinput.press('1')
        '''
    except:
        print('hata')


from typing import List
from queue import PriorityQueue

class Solution:
    def kthSmallest(self, matrix: List[List[int]], k: int) -> int:
        length = len(matrix) * (len(matrix[0]) if len(matrix) > 0 else 0)
        pq = PriorityQueue()
        for row in matrix:
            for n in row:
                pq.put(n)
                if pq.qsize() > length - k + 1:
                    pq.get()
        return pq.get()

if __name__ == "__main__":
    print(Solution().kthSmallest([[1,5,9],[10,11,13],[12,13,15]], 8))


import logging
import textwrap
from abc import abstractmethod
from copy import copy
from typing import Any, Iterable, List, Optional

import numpy as np
from scipy.stats import truncnorm

import popart
from pingpong.scope_manager import Scope

def normal_init_data(dtype, shape, mean, std_dev):
    # Truncated random normal between 2 standard devations
    data = truncnorm.rvs(-2, 2, loc=mean, scale=std_dev, size=np.prod(shape))
    data = data.reshape(shape).astype(dtype)
    return data

logger = logging.getLogger(__name__)

class Parameter:
    def __init__(self,
                 name: str,
                 shape: tuple,
                 value: Optional[np.ndarray] = None,
                 popart_tensor: Optional[str] = None):
        self.name = name
        self.shape = shape
        self.value = value
        self.popart_tensor = popart_tensor
        self.vgid = None

def flatten(l):
    for el in l:
        if isinstance(el, Iterable) and not isinstance(el, (str, bytes)):
            yield from flatten(el)
        else:
            yield el

class Block:
    """Base class for popart layers."""

    def __init__(self,
                 scope: Scope,
                 params: List[Parameter] = [],
                 dtype: np.dtype = np.float16,
                 builder=None,
                 initializers=None,
                 scope_provider=None):

        self.scope = scope
        self._params = params
        self._dtype = dtype
        self.num_params = 0
        self.total_params = None
        self.line_length = 98
        self.sub_blocks = []
        if builder is None:
            raise ValueError('Builder must be passed in as kwarg.')
        if scope_provider is None:
            raise ValueError('ScopeProvider must be passed in as kwarg.')
        self._builder = builder
        self.scope_provider = scope_provider
        self.initializers = {} if initializers is None else initializers

    def __setattr__(self, name: str, value: Any) -> None:
        if isinstance(value, Block):
            self.sub_blocks.append(value)

        elif isinstance(value, Iterable):
            for el in flatten(value):
                if isinstance(el, Block):
                    self.sub_blocks.append(el)

        super(Block, self).__setattr__(name, value)

    @property
    def builder(self):
        return self._builder

    @builder.setter
    def builder(self, _):
        raise ValueError('Cannot set builder property outside of Block class.')

    def format_row(self, fields, indent=False):
        positions = [int(self.line_length * p) for p in [.45, .7, .8, 1.]]
        line = ''
        if indent:
            fields[0] = '    ' + fields[0]
        for i in range(len(fields)):
            if i > 0:
                line = line[:-1] + ' '
            line += str(fields[i])
            line = line[:positions[i]]
            line += ' ' * (positions[i] - len(line))
        return line

    def __repr__(self):
        name = self.scope.name
        cls_name = self.__class__.__name__
        pp_phase = self.scope.execution_phase if hasattr(
            self.scope, 'execution_phase') else None
        fields = [
            name + ' (' + cls_name + ')', pp_phase, self.scope.vgid,
            f'{self.count_params():,}'.rjust(10)
        ]
        s = self.format_row(fields)

        if len(self.sub_blocks) > 1:
            modstr = "\n".join([
                "{block}".format(block=block.__repr__())
                for block in self.sub_blocks
            ])
            s = "\n".join([s, modstr])
        return s

    def summary(self):
        header = ['Layer (type)', 'Pingpong Phase', 'VGID', 'Param #']
        print(self.format_row(header))
        print('_' * self.line_length)
        total_params = 0
        for blks in self.sub_blocks:
            print(blks)
            total_params += blks.count_params()

        print('Total params: {:,}'.format(total_params))

    def count_params(self):
        if self.total_params is not None:
            return self.total_params
        child_params = 0
        for item in self.sub_blocks:
            child_params += item.count_params()
        self.total_params = child_params + self.num_params
        return self.total_params

    def __create_params__(self):
        # Create params only for a single block - not recursive
        for param in self._params:
            if param.popart_tensor is None:
                data = self.initializers.get(param.name, None)
                if isinstance(data, np.ndarray):
                    if data.dtype != self._dtype:
                        raise ValueError(
                            f"Type of {param.name} does not match value provided. \n"
                            f"Provided {data.dtype}. Required {self._dtype}")
                    if np.any(data.shape != np.array(param.shape)):
                        if np.all(data.T.shape == np.array(param.shape)):
                            data = data.T.copy()
                            logger.warn(
                                f"Initializer for {param.name} was provided transposed."
                            )
                        else:
                            raise RuntimeError(
                                f"Initializer {param.name} does not match shapes. \n"
                                f" Provided {data.shape}. Required {param.shape}")
                    param.value = data
                else:
                    param.value = normal_init_data(self._dtype,
                                                   param.shape,
                                                   mean=0,
                                                   std_dev=1)

                popart_tensor = self._builder.addInitializedInputTensor(
                    param.value, param.name)
                self.num_params += np.prod(param.value.shape)
                param.vgid = self.scope.vgid

            else:
                if param.vgid != self.scope.vgid:
                    raise RuntimeError(
                        'Shared parameter {} appears in different virtual graphs.' .format(
                            param.name))
                # Transpose shared weights
                if (self.builder.getTensorShape(param.popart_tensor) == reversed(param.shape)):
                    popart_tensor = self._builder.aiOnnx.transpose([param.popart_tensor])
                else:
                    popart_tensor = param.popart_tensor
                # Create copy of shared param so as to not modify
                # the param for the previous layer
                param = copy(param)

            param.popart_tensor = popart_tensor

    @property
    def params(self):
        """Return this Block's parameter dictionary, not recursive.

        Returns:
            Dict: List of parameters for this block.
        """
        return self._params

    @params.setter
    def params(self, value):
        self._params = value

    def next_phase(self):
        return self.scope_provider.get_next_phase()

    def total_phases(self):
        return self.scope_provider.get_prev_phase() + 1

    def __call__(self, *args):
        """Calls forward. Only accepts positional arguments."""
        with self.scope_provider(self._builder, self.scope):
            self.__create_params__()
            return self.forward(*args)

    @abstractmethod
    def forward(self, *args):
        raise NotImplementedError


import torch
import numpy
from scipy.spatial.distance import cdist
import distance as cuda
from timeit import default_timer as Timer

def cityblock(XA, XB):

    XC = list()
    for xa in XA:
        xa = xa.unsqueeze(0).expand_as(XB)
        XC.append( (xa-XB).abs().sum(0) )
    return torch.stack(XC)

XA = torch.randn(4, 4)
XB = torch.randn(4, 4)
XC = torch.zeros((XA.size(0), XB.size(0)))

print(XA.type())
print(XB.type())
print(XC.type())

start = Timer()
XC = cuda.cdist(XA.to(torch.device('cuda')), XB.to(torch.device('cuda')), "cityblock")
stop = Timer()
print('Time: {}'.format(stop - start))

start = Timer()
XD = cdist(XA.cpu().numpy(), XB.cpu().numpy(), metric="cityblock")
stop = Timer()
print('Time: {}'.format(stop - start))

start = Timer()
XE = cityblock(XA.cpu(), XB.cpu())
stop = Timer()
print('Time: {}'.format(stop - start))

print('diff: {}'.format(numpy.mean(numpy.abs(XC.cpu().numpy() - XD))))
print('diff: {}'.format( numpy.mean( numpy.abs( XC.cpu().numpy() - XE.numpy() ) ) ))
print('diff: {}'.format( numpy.mean( numpy.abs(XD - XE.numpy()) ) ) )

print(XA.cpu().float().numpy())
print(XB.cpu().float().numpy())
print(XC.cpu().float().numpy())
print(XD.astype(numpy.float32))
print(XE.cpu().float().numpy())


import os

OUT_PREFIX = "chunk_part_"

def get_filename(prefix, idx):
    return prefix + str(idx)

def split(FILE):
    bytes_per_file=307200
    # 25MB = 26214400bytes
    # 300KB = 307200bytes
    idx=0
    in_fil=p=open(FILE,"rb")
    out_filename = get_filename(OUT_PREFIX,idx)
    out_fil=open(out_filename,"wb")

    byte_count=file_count=cnt=0
    c=in_fil.read(1)
    file_stat=os.stat(FILE)
    tot_size=file_stat.st_size

    while c!='' and cnt<=tot_size:
        byte_count+=1
        out_fil.write(c)
        cnt+=1
        if byte_count>=bytes_per_file:
            byte_count=0
            file_count+=1
            out_fil.close()
            idx+=1
            out_filename = get_filename(OUT_PREFIX,idx)
            out_fil=open(out_filename,"wb")
        c=in_fil.read(1)
    in_fil.close()
    if not out_fil.close:
        out_fil.close()
    if byte_count == 0:
        os.remove(out_filename)


#!/usr/bin/env python2.7
from __future__ import print_function
from icalendar import Calendar, Event
from datetime import datetime, timedelta
from time import strptime, strftime, mktime

""" See https://secure.wideworld-sports.me/wws_membership/PrintSchedule.asp?ID=1439
and https://secure.wideworld-sports.me/wws_membership/DivisionReport.asp?ID=1439

This could be improved to go read the schedule from the web page based
on season, team name, etc.  Or just copying-and-pasting the schedule
to a text file and using grep and stuff like this one does """

def writeICS(file, sched):

    location = "Wide World Sports, 2140 Oak Valley Dr, Ann Arbor, MI 48103"
    ical = Calendar()

    for m in sorted(sched):
        event=Event()
        event.add('summary', "Fairies Soccer on "+sched[m]['desc'])
        event.add('dtstart', sched[m]['starttime'])
        event.add('dtend', sched[m]['endtime'])
        event.add('location', location)

        ical.add_component(event)

    # write out our calendar to a nicely formatted .ics file
    f = open(file,"wb")
    f.write(ical.to_ical(ical))
    f.close

if __name__ == '__main__':
    FILE="./ff"  # hopefully there is a file called 'ff' formated like:
    # 1/14/2017	Sat 	1:30 PM	Big Georges Field	Fairies	 	WWSC 8-9 Portugal
    # with tabs separating the items

    with open (FILE, "rb") as f:
        a=f.readlines()

    a = [b.strip('\n') for b in a]

    game = dict()
    for evt in a:
        # break up the line on tabs
        line = evt.split('\t')
        # cope with the date and time
        ds = strftime('%Y-%m-%dT%H:%M',
                      strptime(line[0]+" "+line[2],
                                        "%m/%d/%Y %I:%M %p"))
        # make a datetime object so icalendar.event.add works
        dss = datetime.fromtimestamp(
            mktime(strptime(line[0]+" "+line[2],
                            "%m/%d/%Y %I:%M %p")))
        # make the duration for the end time
        dse = dss + timedelta(hours=1)
        # make the game details (start, end, description)
        game[ds] = dict()
        game[ds]['desc'] = "{}: {} vs {}".format(
            line[3],
            line[4],
            line[6])
        game[ds]['starttime'] = dss
        game[ds]['endtime'] = dse

    # print a nice schedule to the screen
    for d in sorted(game):
        print ("{} -  {}".format(d, game[d]['desc']))

    # write the ICS file for importing into a calendar program
    writeICS("FairiesWinter2017Schedule.ics", game)


import os
import unittest
import json
from flask_sqlalchemy import SQLAlchemy

from flaskr import create_app
from models import setup_db, Question, Category

class TriviaTestCase(unittest.TestCase):
    """This class represents the trivia test case"""

    def setUp(self):
        """Define test variables and initialize app."""
        self.app = create_app()
        self.client = self.app.test_client
        self.database_name = "trivia_test"
        self.database_path = "postgres://{}:{}@{}/{}".format('postgres', 'password', 'localhost:5432', self.database_name)
        setup_db(self.app, self.database_path)

        self.dummy_data = {
            'question': 'TEST',
            'answer': 'TEST',
            'difficulty': 1,
            'category': 3
        }

        # binds the app to the current context
        with self.app.app_context():
            self.db = SQLAlchemy()
            self.db.init_app(self.app)
            # create all tables
            self.db.create_all()

    def tearDown(self):
        """Executed after reach test"""
        pass

    """
    TODO
    Write at least one test for each test for successful operation and for expected errors.
    """

    #test questions endpoint
    def test_get_paginated_questions(self):
        res = self.client().get('/questions')
        data = json.loads(res.data)

        self.assertEqual(res.status_code, 200)
        self.assertEqual(data['success'], True)
        self.assertTrue(data['questions'])
        self.assertTrue(data['totalQuestions'])
        self.assertTrue(data['totalPages'])

    #test 404 error for invalid page of questions
    def test_error_for_invalid_questions_page(self):
        res = self.client().get('/questions?page=100003930')
        data = json.loads(res.data)

        self.assertEqual(res.status_code, 404)
        self.assertEqual(data['success'], False)
        self.assertEqual(data['message'], 'resource not found')

    #test categories endpoint
    def test_get_categories(self):
        res = self.client().get('/categories')
        data = json.loads(res.data)

        self.assertEqual(res.status_code, 200)
        self.assertEqual(data['success'], True)
        self.assertTrue(data['categories'])
        self.assertEqual(data['total_categories'], 6)

    #test questions by category
    def test_get_questions_by_category(self):
        res = self.client().get('/categories/2/questions')
        data = json.loads(res.data)

        self.assertEqual(res.status_code, 200)
        self.assertEqual(data['success'], True)
        self.assertTrue(len(data['questions']))
        self.assertTrue(data['totalQuestions'])
        self.assertTrue(data['totalPages'])
        self.assertEqual(data['currentCategory'], 3)

    #test adding a question
    def test_add_question(self):
        res = self.client().post('/questions', json=self.dummy_data)
        data = json.loads(res.data)

        self.assertEqual(res.status_code, 200)
        self.assertEqual(data['success'], True)
        self.assertTrue(data['question'])

    #test deleting a question which creates a question to be deleted
    #without relying on another API endpoint and check it was actually deleted
    def test_delete_question(self):
        question = Question(question = 'question', answer = '321answertest123', difficulty = 1, category = 2)
        question.insert()

        question = Question.query.filter(Question.answer=='321answertest123').first()
        res = self.client().delete(f'/questions/{question.id}')
        data = json.loads(res.data)

        self.assertEqual(res.status_code, 200)
        self.assertEqual(data['success'], True)

        question_new = Question.query.filter(Question.answer=='321answertest123').first()
        self.assertFalse(question_new)

    #test deleting a question which doesn't exist
    def test_error_question_delete_id_no_exist(self):
        res = self.client().delete(f'/questions/38389940')
        data = json.loads(res.data)

        self.assertEqual(res.status_code, 404)
        self.assertEqual(data['success'], False)
        self.assertEqual(data['message'], "resource not found")

    #test trying to add a question without all the required fields
    def test_error_create_question_missing_fields(self):
        dummy_data = {
            'category': 1
        }
        res = self.client().post('/questions', json=dummy_data)
        data = json.loads(res.data)

        self.assertEqual(res.status_code, 422)
        self.assertEqual(data['success'], False)
        self.assertEqual(data['message'], "unprocessable")

    #test trying to add a question with empty fields
    def test_error_create_question_empty_fields(self):
        dummy_data = {
            'question': '',
            'answer': '',
            'difficulty': 1,
            'category': 1
        }
        res = self.client().post('/questions', json=dummy_data)
        data = json.loads(res.data)

        self.assertEqual(res.status_code, 422)
        self.assertEqual(data['success'], False)
        self.assertEqual(data['message'], "unprocessable")

    #test 405 error on PUT question
    def test_error_put_new_question(self):
        res = self.client().put('/questions', json=self.dummy_data)
        data = json.loads(res.data)

        self.assertEqual(res.status_code, 405)
        self.assertEqual(data['success'], False)
        self.assertEqual(data['message'], "method not allowed")

    #test search
    def test_search(self):
        search = 'which'
        res = self.client().post(f'/questions/{search}')
        data = json.loads(res.data)

        self.assertEqual(res.status_code, 200)
        self.assertEqual(data['success'], True)
        self.assertTrue(data['questions'])

    #test quiz with all required info
    def test_quiz(self):
        res = self.client().post('/quizzes', json={'previous_questions':[], 'quiz_category': {"type": "Art", "id": "2"}})
        data = json.loads(res.data)

        self.assertEqual(res.status_code, 200)
        self.assertEqual(data['success'], True)
        self.assertTrue(data['question'])

    #test error when missing previous questions
    def test_quiz_missing_prev_question(self):
        res = self.client().post('/quizzes', json={'quiz_category': {"type": "Art", "id": "2"}})
        data = json.loads(res.data)

        self.assertEqual(res.status_code, 422)
        self.assertEqual(data['success'], False)
        self.assertEqual(data['message'], "unprocessable")

    #test error when missing quiz category
    def test_quiz_missing_quiz_category(self):
        res = self.client().post('/quizzes', json={'previous_questions':[]})
        data = json.loads(res.data)

        self.assertEqual(res.status_code, 422)
        self.assertEqual(data['success'], False)
        self.assertEqual(data['message'], "unprocessable")

# Make the tests conveniently executable
if __name__ == "__main__":
    unittest.main()


import pytest

from src.entities import Room, Map, UndirectedGraph

@pytest.mark.parametrize("room, expected", [
    ({"id": 1, "name": "Hallway", "north": 2, "east": 7, "objects": []}, {2, 7}),
    ({"id": 7, "name": "Living room", "west": 1, "north": 4, "objects": []}, {1, 4}),
    ({"id": 5, "name": "Bedroom", "south": 2, "east": 6, "objects": [{"name": "Pillow"}]}, {2, 6})
])
def test_room_returns_linked_rooms(room, expected):
    r = Room(**room)
    assert r.get_linked_rooms() == expected

@pytest.mark.parametrize("map, id, expected", [
    (
        {
            "rooms": [
                {"id": 1, "name": "Hallway", "north": 2, "east": 7, "objects": []},
                {"id": 2, "name": "Dining Room", "north": 5, "south": 1, "west": 3, "east": 4, "objects": []},
                {"id": 3, "name": "Kitchen", "east": 2, "objects": [{"name": "Knife"}]},
                {"id": 4, "name": "Sun Room", "west": 2, "north": 6, "south": 7, "objects": []},
                {"id": 5, "name": "Bedroom", "south": 2, "east": 6, "objects": [{"name": "Pillow"}]},
                {"id": 6, "name": "Bathroom", "west": 5, "south": 4, "objects": []},
                {"id": 7, "name": "Living room", "west": 1, "north": 4, "objects": [{"name": "Potted Plant"}]}
            ]
        },
        6,
        {"id": 6, "name": "Bathroom", "west": 5, "south": 4, "objects": []}
    ),
    (
        {
            "rooms": [
                {"id": 1, "name": "Hallway", "north": 2, "east": 7, "objects": []},
                {"id": 2, "name": "Dining Room", "north": 5, "south": 1, "west": 3, "east": 4, "objects": []},
                {"id": 3, "name": "Kitchen", "east": 2, "objects": [{"name": "Knife"}]},
            ]
        },
        2,
        {"id": 2, "name": "Dining Room", "north": 5, "south": 1, "west": 3, "east": 4, "objects": []}
    ),
])
def test_map_returns_room_by_id(map, id, expected):
    m = Map(**map)
    assert m.get_room(id) == Room(**expected)

@pytest.mark.parametrize("v, neighbours", [
    (1, (2, 5, 7)),
    (0, (1,)),
])
def test_graph_set_neighbours(v, neighbours):
    graph = UndirectedGraph()
    graph.add_neighbours(v, neighbours)
    assert graph.schema_.get(v) == neighbours

@pytest.mark.parametrize("v, neighbours", [
    (1, (2, 5, 7)),
    (0, (1,)),
])
def test_graph_get_neighbours(v, neighbours):
    graph = UndirectedGraph()
    graph.schema_.update({v: neighbours})
    assert graph.get_neighbours(v) == neighbours


#!/usr/bin/env python3
import whisper, os
import numpy as np
import sounddevice as sd
from scipy.io.wavfile import write
from chatgpt import Conversation

# This is my attempt to make psuedo-live transcription of speech using Whisper.
# Since my system can't use pyaudio, I'm using sounddevice instead.
# Currently just a basic terminal implementation.
# by Nik Stromberg - nikorasu85@gmail.com - MIT 2022 - copilot

Model = 'medium'     # Whisper model size (tiny, base, small, medium, large)
English = True      # Use English-only model?
Translate = False   # Translate non-English to English?
SampleRate = 44100  # Stream device recording frequency
BlockSize = 30      # Block size in milliseconds
Threshold = 0.2     # Minimum volume threshold to activate listening
Vocals = [50, 1000] # Frequency range to detect sounds that could be speech
EndBlocks = 35      # Number of blocks to wait before sending to Whisper

class StreamHandler:
    def __init__(self, assist=None):
        if assist is None:
            class fakeAsst():
                running = True
                talking = False
                analyze = True
            self.asst = fakeAsst()
        else:
            self.asst = assist
        self.running = True
        self.padding = 0
        self.prevblock = self.buffer = np.zeros((0,1))
        self.fileready = False
        print("\033[96mLoading Whisper Model..\033[0m", end='', flush=True)
        self.model = whisper.load_model(f'{Model}{".en" if English else ""}')
        print("\033[90m Done.\033[0m")
        self.conversation = Conversation()

    def callback(self, indata, frames, time, status):
        # if status: print(status) # for debugging, prints stream errors.
        if any(indata):
            freq = np.argmax(np.abs(np.fft.rfft(indata[:, 0]))) * SampleRate / frames
            if indata.max() > Threshold and Vocals[0] <= freq <= Vocals[1] and not self.asst.talking:
                print('.', end='', flush=True)
                if self.padding < 1: self.buffer = self.prevblock.copy()
                self.buffer = np.concatenate((self.buffer, indata))
                self.padding = EndBlocks
            else:
                self.padding -= 1
                if self.padding > 1:
                    self.buffer = np.concatenate((self.buffer, indata))
                elif self.padding < 1 < self.buffer.shape[0] > SampleRate:
                    self.fileready = True
                    write('dictate.wav', SampleRate, self.buffer) # I'd rather send data to Whisper directly..
                    self.buffer = np.zeros((0,1))
                elif self.padding < 1 < self.buffer.shape[0] < SampleRate:
                    self.buffer = np.zeros((0,1))
                    print("\033[2K\033[0G", end='', flush=True)
                else:
                    self.prevblock = indata.copy() #np.concatenate((self.prevblock[-int(SampleRate/10):], indata)) # SLOW
        else:
            print("\033[31mNo input or device is muted.\033[0m")
            self.running = False

    def process(self):
        if self.fileready:
            print("\n\033[90mTranscribing..\033[0m")
            result = self.model.transcribe('dictate.wav',
                                           fp16=False,
                                           language='en' if English else '',
                                           task='translate' if Translate else 'transcribe'
                                           )
            print(f"\033[1A\033[2K\033[0G{result['text']}")

            if self.asst.analyze is not None:
                self.asst.analyze(result['text'])
            self.fileready = False
            return result

    def listen_for_commands(self, result):
        if result:
            send_text = result['text'].lower()
            chatgpt_commands = ["chat gpt", "chatgpt", "chatgpt,", "chat gpt,"]

            for cmd in chatgpt_commands:
                if cmd in send_text:
                    print("\nChatGPT is responding... \n")
                    send_text.replace(cmd, "")
                    print("\033[0;33m", self.conversation.chat(send_text), "\033[0m")
                    print("")
            reset_commands = [
                "chatgpt reset",
                "gpt reset",
                "chat, gpt"
                "chat gpt reset",
                "reset conversation"
            ]
            for cmd in reset_commands:
                if send_text in cmd:
                    self.conversation.reset()

    def listen(self):
        print("\033[32mListening.. \033[37m(Ctrl+C to Quit)\033[0m")
        with sd.InputStream(channels=1,
                            callback=self.callback,
                            blocksize=int(SampleRate * BlockSize / 1000),
                            samplerate=SampleRate
                            ):
            while self.running and self.asst.running:
                result = self.process()
                self.listen_for_commands(result)

def main():
    try:
        handler = StreamHandler()
        handler.listen()
    except (KeyboardInterrupt, SystemExit):
        pass
    finally:
        print("\nQuitting...\n")
        if os.path.exists('dictate.wav'):
            os.remove('dictate.wav')

if __name__ == '__main__':
    main()


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

import pytest
import ZConfig
import transaction

from collections import OrderedDict

from ZODB import DB
from ZODB.DemoStorage import DemoStorage
from cromlech.zodb.utils import eval_loader, init_db
from cromlech.zodb.utils import initialize_applications

from ..testing import DummySite, MyApp, SimpleApp

def test_eval_loader():
    assert eval_loader('cromlech.zodb.utils:eval_loader') is eval_loader
    assert (eval_loader('cromlech.zodb.tests.fixture:Foo').__doc__ ==
            "class to test eval_loader")
    with pytest.raises(RuntimeError):
        eval_loader(':foo')
    with pytest.raises(ImportError):
        # module does not exists
        eval_loader('cromlech.zodb.doesnotexiste:id')
    with pytest.raises(ImportError):
        # object inside module does not exists
        eval_loader('cromlech.zodb.tests.fixture:Bar')

foo = object()

def add_foo(db):
    tm = transaction.TransactionManager()
    conn = db.open(tm)
    conn.root()['foo'] = foo
    tm.get().commit()
    conn.close()

def test_init_db():
    configuration = """
        <zodb>
        <demostorage>
        </demostorage>
        </zodb>"""
    db = init_db(configuration, add_foo)
    conn = db.open()
    assert conn.root()['foo'] is foo

def test_init_db_bad_conf():
    configuration = """
        <zodb>
            <UNKNOWN_storage>
            </UNKNOWN_storage>
        </zodb>"""
    with pytest.raises(ZConfig.ConfigurationSyntaxError):
        init_db(configuration, add_foo)

def test_initialize_applications():

    def apps():
        return {'mine':MyApp, 'app': SimpleApp, 'app2': SimpleApp,
                'obj':tuple}

    def failing_apps():
        return OrderedDict((('foo', SimpleApp), ('spam', lambda: 1/0)))

    db = DB(DemoStorage())
    initialize_applications(db, apps)
    conn = db.open()
    root = conn.root()
    assert root['mine']() == b"running !"
    assert root['app']() == b"simply running !"
    assert root['app2']() == b"simply running !"
    assert isinstance(root['obj'], tuple)
    transaction.abort()
    conn.close()

    # verify it's all or nothing
    try:
        initialize_applications(db, failing_apps)
    except ZeroDivisionError:
        pass
    conn = db.open()
    root = conn.root()
    assert 'foo' not in root
    conn.close()


from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
import os

driver=webdriver.Chrome()
file = "file:///" + os.path.abspath("../../../selenium2html/modal.html")
driver.get(file)
time.sleep(2)

dr = driver.find_element_by_css_selector("#show_modal")
dr.click()
time.sleep(2)
dr.find_element_by_xpath("//*[@id='myModal']").find_element_by_id("click").click()
dr.find_element_by_xpath("//*[@id='myModal']/div[3]").click()
time.sleep(3)
driver.quit()


# -*- coding: utf-8 -*-
""" Installer for the energy.content package.
"""
from os.path import join
from setuptools import find_packages
from setuptools import setup

NAME = 'energy.content'
PATH = ['src'] + NAME.split('.') + ['version.txt']
VERSION = open(join(*PATH)).read().strip()

long_description = '\n\n'.join([
    open('README.rst').read(),
    open('CONTRIBUTORS.rst').read(),
    open(join("docs", "HISTORY.txt")).read(),
])

setup(
    name=NAME,
    version=VERSION,
    description="EnergyUnion extensions for Plone",
    long_description_content_type="text/x-rst",
    long_description=long_description,
    # Get more from https://pypi.org/classifiers/
    classifiers=[
        "Environment :: Web Environment",
        "Framework :: Plone",
        "Framework :: Plone :: Addon",
        "Framework :: Plone :: 5.1",
        "Programming Language :: Python",
        "Programming Language :: Python :: 2.7",
        "Operating System :: OS Independent",
        "License :: OSI Approved :: GNU General Public License v2 (GPLv2)",
    ],
    keywords='Python Plone',
    author='Tiberiu Ichim',
    author_email='tiberiu.ichim@eaudeweb.ro',
    url='https://github.com/collective/energy.content',
    project_urls={
        'PyPI': 'https://pypi.python.org/pypi/energy.content',
        'Source': 'https://github.com/collective/energy.content',
        'Tracker': 'https://github.com/collective/energy.content/issues',
        # 'Documentation': 'https://energy.content.readthedocs.io/en/latest/',
    },
    license='GPL version 2',
    packages=find_packages('src', exclude=['ez_setup']),
    namespace_packages=['energy'],
    package_dir={'': 'src'},
    include_package_data=True,
    zip_safe=False,
    install_requires=[
        'setuptools',
        # -*- Extra requirements: -*-
        'z3c.jbot',
        'plone.api>=1.8.4',
        'plone.restapi',
        'plone.app.dexterity',
        'collective.monkeypatcher',
    ],
    extras_require={
        'test': [
            'plone.app.testing',
            'plone.testing',
            'plone.app.contenttypes',
            'plone.app.robotframework[debug]',
        ],
    },
    entry_points="""
    [z3c.autoinclude.plugin]
    target = plone
    [console_scripts]
    update_locale = energy.content.locales.update:update_locale
    """,
)


import dataclasses
import functools
from typing import Callable, Optional

from acme import specs
from acme.agents.jax.mbop import models
from acme.jax import networks
import jax
from jax import random
import jax.numpy as jnp

# Function that takes (n_trajectories, horizon, action_dim) tensor of action
# trajectories and  (n_trajectories) vector of corresponding cumulative rewards,
# i.e. returns, for each trajectory as input and returns a single action
# trajectory.
ActionAggregationFn = Callable[[jnp.ndarray, jnp.ndarray], jnp.ndarray]

def return_weighted_average(action_trajectories: jnp.ndarray,
                            cum_reward: jnp.ndarray,
                            kappa: float) -> jnp.ndarray:
  r"""Calculates return-weighted average over all trajectories.

  This will calculate the return-weighted average over a set of trajectories as
  defined on l.17 of Alg. 2 in the MBOP paper:
  [https://arxiv.org/abs/2008.05556].

  Note: Clipping will be performed for `cum_reward` values > 80 to avoid NaNs.

  Args:
    action_trajectories: (n_trajectories, horizon, action_dim) tensor of action
      trajectories, corresponds to `A` in Alg. 2.
    cum_reward: (n_trajectories) vector of corresponding cumulative rewards
      (returns) for each trajectory. Corresponds to `\mathcal{R}` in Alg. 2.
    kappa: `\kappa` constant, changes the 'peakiness' of the exponential
      averaging.

  Returns:
    Single action trajectory corresponding to the return-weighted average of the
      trajectories.
  """
  # Substract maximum reward to avoid NaNs:
  cum_reward = cum_reward - cum_reward.max()
  # Remove the batch dimension of cum_reward allows for an implicit broadcast in
  # jnp.average:
  exp_cum_reward = jnp.exp(kappa * jnp.squeeze(cum_reward))
  return jnp.average(action_trajectories, weights=exp_cum_reward, axis=0)

def return_top_k_average(action_trajectories: jnp.ndarray,
                         cum_reward: jnp.ndarray,
                         k: int = 10) -> jnp.ndarray:
  r"""Calculates the top-k average over all trajectories.

  This will calculate the top-k average over a set of trajectories as
  defined in the POIR Paper:

  Note: top-k average is more numerically stable than the weighted average.

  Args:
    action_trajectories: (n_trajectories, horizon, action_dim) tensor of action
      trajectories.
    cum_reward: (n_trajectories) vector of corresponding cumulative rewards
      (returns) for each trajectory.
    k: the number of trajectories to average.

  Returns:
    Single action trajectory corresponding to the average of the k best
      trajectories.
  """
  top_k_trajectories = action_trajectories[jnp.argsort(
      jnp.squeeze(cum_reward))[-int(k):]]
  return jnp.mean(top_k_trajectories, axis=0)

@dataclasses.dataclass
class MPPIConfig:
  """Config dataclass for MPPI-style planning, used in mppi.py.

  These variables correspond to different parameters of `MBOP-Trajopt` as
  defined in MBOP [https://arxiv.org/abs/2008.05556] (Alg. 2).

  Attributes:
    sigma: Variance of action-additive noise.
    beta: Mixture parameter between old trajectory and new action.
    horizon: Planning horizon, corresponds to H in Alg. 2 line 8.
    n_trajectories: Number of trajectories used in `mppi_planner` to sample the
      best action. Corresponds to `N` in Alg. 2 line. 5.
    previous_trajectory_clip: Value to clip the previous_trajectory's actions
      to. Disabled if None.
    action_aggregation_fn: Function that aggregates action trajectories and
      returns a single action trajectory.
  """
  sigma: float = 0.8
  beta: float = 0.2
  horizon: int = 15
  n_trajectories: int = 1000
  previous_trajectory_clip: Optional[float] = None
  action_aggregation_fn: ActionAggregationFn = (
      functools.partial(return_weighted_average, kappa=0.5))

def get_initial_trajectory(config: MPPIConfig, env_spec: specs.EnvironmentSpec):
  """Returns the initial empty trajectory `T_0`."""
  return jnp.zeros((max(1, config.horizon),) + env_spec.actions.shape)

def _repeat_n(new_batch: int, data: jnp.ndarray) -> jnp.ndarray:
  """Create new batch dimension of size `new_batch` by repeating `data`."""
  return jnp.broadcast_to(data, (new_batch,) + data.shape)

def mppi_planner(
    config: MPPIConfig,
    world_model: models.WorldModel,
    policy_prior: models.PolicyPrior,
    n_step_return: models.NStepReturn,
    world_model_params: networks.Params,
    policy_prior_params: networks.Params,
    n_step_return_params: networks.Params,
    random_key: networks.PRNGKey,
    observation: networks.Observation,
    previous_trajectory: jnp.ndarray,
) -> jnp.ndarray:
  """MPPI-extended trajectory optimizer.

  This implements the trajectory optimizer described in MBOP
  [https://arxiv.org/abs/2008.05556] (Alg. 2) which is an extended version of
  MPPI that adds support for arbitrary sampling distributions and extends the
  return horizon using an approximate model of returns.  There are a couple
  notation changes for readability:
  A -> action_trajectories
  T -> action_trajectory

  If the horizon is set to 0, the planner will simply call the policy_prior
  and average the action over the ensemble heads.

  Args:
    config: Base configuration parameters of MPPI.
    world_model: Corresponds to `f_m(s_t, a_t)_s` in Alg. 2.
    policy_prior: Corresponds to `f_b(s_t, a_tm1)` in Alg. 2.
    n_step_return: Corresponds to `f_R(s_t, a_t)` in Alg. 2.
    world_model_params: Parameters for world model.
    policy_prior_params: Parameters for policy prior.
    n_step_return_params: Parameters for n_step return.
    random_key: JAX random key seed.
    observation: Normalized current observation from the environment, `s` in
      Alg. 2.
    previous_trajectory: Normalized previous action trajectory. `T` in Alg 2.
      Shape is [n_trajectories, horizon, action_dims].

  Returns:
    jnp.ndarray: Average action trajectory of shape [horizon, action_dims].
  """
  action_trajectory_tm1 = previous_trajectory
  policy_prior_state = policy_prior.init(random_key)

  # Broadcast so that we have n_trajectories copies of each:
  observation_t = jax.tree_map(
      functools.partial(_repeat_n, config.n_trajectories), observation)
  action_tm1 = jnp.broadcast_to(action_trajectory_tm1[0],
                                (config.n_trajectories,) +
                                action_trajectory_tm1[0].shape)

  if config.previous_trajectory_clip is not None:
    action_tm1 = jnp.clip(
        action_tm1,
        a_min=-config.previous_trajectory_clip,
        a_max=config.previous_trajectory_clip)

  # First check if planning is unnecessary:
  if config.horizon == 0:
    if hasattr(policy_prior_state, 'action_tm1'):
      policy_prior_state = policy_prior_state.replace(action_tm1=action_tm1)
    action_set, _ = policy_prior.select_action(policy_prior_params,
                                               observation_t,
                                               policy_prior_state)
    # Need to re-create an action trajectory from a single action.
    return jnp.broadcast_to(
        jnp.mean(action_set, axis=0), (1, action_set.shape[-1]))

  # Accumulators for returns and trajectories:
  cum_reward = jnp.zeros((config.n_trajectories, 1))

  # Generate noise once:
  random_key, noise_key = random.split(random_key)
  action_noise = config.sigma * random.normal(noise_key, (
      (config.horizon,) + action_tm1.shape))

  # Initialize empty set of action trajectories for concatenation in loop:
  action_trajectories = jnp.zeros((config.n_trajectories, 0) +
                                  action_trajectory_tm1[0].shape)

  for t in range(config.horizon):
    # Query policy prior for proposed action:
    if hasattr(policy_prior_state, 'action_tm1'):
      policy_prior_state = policy_prior_state.replace(action_tm1=action_tm1)
    action_t, policy_prior_state = policy_prior.select_action(
        policy_prior_params, observation_t, policy_prior_state)
    # Add action noise:
    action_t = action_t + action_noise[t]
    # Mix action with previous trajectory's corresponding action:
    action_t = (1 -
                config.beta) * action_t + config.beta * action_trajectory_tm1[t]

    # Query world model to get next observation and reward:
    observation_tp1, reward_t = world_model(world_model_params, observation_t,
                                            action_t)
    cum_reward += reward_t

    # Insert actions into trajectory matrix:
    action_trajectories = jnp.concatenate(
        [action_trajectories,
         jnp.expand_dims(action_t, axis=1)], axis=1)
    # Bump variable timesteps for next loop:
    observation_t = observation_tp1
    action_tm1 = action_t

  # De-normalize and append the final n_step return prediction:
  n_step_return_t = n_step_return(n_step_return_params, observation_t, action_t)
  cum_reward += n_step_return_t

  # Average the set of `n_trajectories` trajectories into a single trajectory.
  return config.action_aggregation_fn(action_trajectories, cum_reward)


def day2p2(file):

    dict = {
        # rock
        "A X": 3,  # rock LOSE scissors
        "A Y": 4,  # rock draw rock
        "A Z": 8,  # rock win paper

        # paper
        "B X": 1,  # paper LOSE rock
        "B Y": 5,  # paper ties paper
        "B Z": 9,  # paper win scissors

        # scissors
        "C X": 2,  # scissors lose paper
        "C Y": 6,  # scissors draw scissors
        "C Z": 7  # scissors win rock
    }

    score = 0
    lines = 0
    with open(file) as f:
        for line in f:
            line = line.rstrip('\n')
            lines = lines + 1

            if line in dict.keys():
                #print(line, dict[line])
                score += dict[line]
            else:
                print('uhoh', line)

    print(lines, score)
    return score

if __name__ == '__main__':
    day2p2("day2input.txt")


import tkinter as tk
import consulta as c
import push
import slack_mensage
import time
from datetime import datetime

root = tk.Tk()

root.geometry('600x200')
root.title("Monitor Maestro")
root.iconbitmap('equiplano-logo-vertical.ico')

sec = None
consulta = c.Consulta()
def tick():
    global sec
    global respostas

    hora = datetime.now().strftime('%H:%M:%S')

    if sec == None or sec == 0:

        sec = int(inicio.get())*60

    if hora in ['07:00:00', '12:00:00', '17:00:00']:
        print(hora)
        slack_mensage.mensage_slack('Olá ... fiquem tranquilos ... estou monitorando o Maestro de Toledo ... :white_check_mark:')

    if sec == 1:
        time['text'] = 'Consultando...'
        root.update()
        sec = 0
        if sec == 0:
            msg = consulta.login()
            print(msg)

            if msg == 'Maestro pode estar parado.':
                erro['text'] = msg
                push.push()
                root.update()
            else:
                resposta['text'] = msg
                root.update()

            time['text'] = int(inicio.get())
            root.update()
            tick()
    else:
        sec = sec - 1
        relogio = int(sec/60)
        if sec%2:
            t = '. . .'
        else: t = ' . . '
        if relogio > 1:
            time['text'] = 'A próxima consulta será realizada em menos de '+ str(relogio+1) + ' minutos ' + t
        elif relogio == 1:
            time['text'] = 'A próxima consulta será realizada em menos de ' + str(relogio+1) + ' minuto ' + t
        else:
            time['text'] = 'Restam ' + str(sec) + ' segundos para próxima consulta.'
        time.after(1000, tick)

    return sec

label = tk.Label(root, text="Tempo de verificação no Maestro")
label.pack()
inicio = tk.Entry(root, textvariable=0)
inicio.pack()
time = tk.Label(root, fg='green')
time.pack()
resposta = tk.Label(root, fg='blue')
resposta.pack()
erro = tk.Label(root, fg='red')
erro.pack()
tk.Button(root, fg='blue', text='Start', command=tick, state='normal').pack()

root.update()
root.mainloop()

tick()

#pyinstaller --onefile -n MaestroMonitoring --noconsole --icon=equiplano-logo-vertical.ico main.py


from firstfuncs_1618 import *

figdir='/home/isabela/Documents/projects/OSNAP/figures_1418_merged/'

# In this script I want to assess the current status and lay out the necessary steps to acheive uniformity.

# What does M1 2016-2018 data look like? Might be what I'm trying to match.
m1dir18=glob.glob(datadir+'OSNAP2018recovery/M1_netcdf/*MCAT*')
m1dir18
m1_18=xr.open_dataset(m1dir18[0])

def quickpresplot(xray):
    figure(figsize=(14,6))
    plot(xray.TIME,xray.PRES);

quickpresplot(m1_18)

m1_18_daily=m1_18.resample(TIME='1D').mean(dim='TIME')

add_SA_CT_PT(m1_18_daily)

m1_18_daily.to_netcdf(datadir+'OSNAP2018recovery/Daily_netcdf/M1_mcat_2018recovery_daily.nc','w',format='netCDF4')

#Note: M1, 2014-2016 is two deployments -- merge their netcdfs
m1dir16=glob.glob(datadir+'OSNAP2016recovery/M1_netcdf/*MCAT*')

m1_16=xr.open_dataset(m1dir16[0])
m1_15=xr.open_dataset(m1dir16[1])

m1_16_merged=xr.concat([m1_16,m1_15],dim='DEPTH')

quickpresplot(m1_16_merged)

m1_16_daily=m1_16_merged.resample(TIME='1D').mean(dim='TIME')

m1_16_daily['LATITUDE']=59.9
m1_16_daily['LONGITUDE']=-41.125

add_SA_CT_PT(m1_16_daily)

m1_16_daily.to_netcdf(datadir+'OSNAP2016recovery/Daily_netcdf/M1_mcat_2016recovery_daily.nc','w',format='netCDF4')

figure(figsize=(14,6))
plot(m1_16_daily.TIME,m1_16_daily.TEMP);

# How about CF data?

# 2016-2018 data is dip-calibrated and in netcdf format.
#Here's my data merging strategy so far:
# From FirstLook_TSdata_Grid_2018reco.py
CF_18_daily={}
for ii in range(1,8):
    CF_18_daily[ii]=xr.open_dataset(datadir+'OSNAP2018recovery/Daily_netcdf/CF'+str(ii)+'_mcat_2018recovery_daily.nc')

#Get 2014-2016 data there:
[date_all,month_all,prs_all,sal_all,tmp_all]=pickle.load(open(datadir+'OSNAP2016recovery/pickles/TSdailydic/TS_daily_dic_wJHIL.pickle','rb'))

#confirm that length/start date lines up
for ii in range(1,8):
    for kk in prs_all[ii]:
        print(ii,shape(prs_all[ii][kk]),date_all[ii][kk][0])
#confirmed this is true (but only within moorings, which is fine)

#make sal, ptmp, and pressure matrices
prs_mat={}
sal_mat={}
ptmp_mat={}
date_vec={}
for ii in range(2,8):
    dpths=list(date_all[ii].keys())
    print(dpths)
    date_vec[ii]=date_all[ii][dpths[-1]]
    prs_mat[ii]=zeros((len(date_vec[ii]),len(dpths)))
    sal_mat[ii]=prs_mat[ii].copy()
    ptmp_mat[ii]=prs_mat[ii].copy()
    for kk in range(len(dpths)):
        tmp_len=len(prs_all[ii][dpths[kk]])
        prs_mat[ii][:tmp_len,kk]=prs_all[ii][dpths[kk]]
        sal_mat[ii][:tmp_len,kk]=sal_all[ii][dpths[kk]]
        ptmp_mat[ii][:tmp_len,kk]=tmp_all[ii][dpths[kk]]

#make an xarray for each mooring
CF_16_daily={}
for ii in range(2,8):
    CF_16_daily[ii]=xr.Dataset({'PRES': (['TIME','DEPTH'], prs_mat[ii] ),
                'PSAL': (['TIME','DEPTH'], sal_mat[ii]),
                'PTMP': (['TIME','DEPTH'],  ptmp_mat[ii]),},
                coords={'TIME': date_vec[ii],
                        'DEPTH': list(prs_all[ii].keys()),
                        'LATITUDE': CFlat[ii-1],
                        'LONGITUDE': CFlon[ii-1]})
    add_SA_CT_PT(CF_16_daily[ii])
    CF_16_daily[ii].to_netcdf(datadir+'OSNAP2016recovery/Daily_netcdf/CF'+str(ii)+'_mcat_2016recovery_daily.nc','w',format='netCDF4')

## note: I had some issues with CF1, so I took it out for now. The already produced xarray should be fine (and in fact I'm not using it atm)
## but user should know that this will not produce it again.

#make the CF1 reconstructed version too.
test=CF_16_daily[5].PSAL

[cf1date,cf1time,cf1prs,cf1mnprs,cf1sal,cf1tmp]=pd.read_pickle(open(datadir+'OSNAP2016recovery/pickles/CF1recon/CF1_recon_JH1810.pickle', 'rb'))
#make sal, ptmp, and pressure matrices

for cc in cf1date:
    print(len(cf1date[cc]))

cf1keys=sort(list(cf1date.keys()))
cf1_datevec=cf1date[cf1keys[0]]

cf1_prs_mat=zeros((len(cf1_datevec),len(cf1keys)))
cf1_sal_mat=cf1_prs_mat.copy()
cf1_ptmp_mat=cf1_prs_mat.copy()
for kk in range(len(cf1keys)):
    cf1_prs_mat[:,kk]=cf1prs[cf1keys[kk]]
    cf1_sal_mat[:,kk]=cf1sal[cf1keys[kk]]
    cf1_ptmp_mat[:,kk]=cf1tmp[cf1keys[kk]]

CF1_16_recon_daily=xr.Dataset({'PRES': (['TIME','DEPTH'], cf1_prs_mat),
            'PSAL': (['TIME','DEPTH'], cf1_sal_mat),
            'PTMP': (['TIME','DEPTH'],  cf1_ptmp_mat),},
            coords={'TIME': cf1_datevec,
                    'DEPTH': cf1keys,
                    'LATITUDE': CFlat[0],
                    'LONGITUDE': CFlon[0]})

add_SA_CT_PT(CF1_16_recon_daily)

CF1_16_recon_daily.to_netcdf(datadir+'OSNAP2016recovery/mcat_nc/CF1_recon_2016recovery_dailymerged.nc','w',format='netCDF4')


"""Обновлении таблицы успешных заказов

Revision ID: 1d213c402d10
Revises: de9fdb3b95c9
Create Date: 2021-03-24 22:51:53.974240

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '1d213c402d10'
down_revision = 'de9fdb3b95c9'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('completed_orders', sa.Column('general_complete_time', sa.DateTime(), nullable=True))
    op.add_column('completed_orders', sa.Column('region', sa.Integer(), nullable=True))
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column('completed_orders', 'region')
    op.drop_column('completed_orders', 'general_complete_time')
    # ### end Alembic commands ###

migrations/versions/1d213c402d10_обновлении_таблицы_успешных_заказов.py