# -*- coding: utf-8 -*-
"""
Created on Fri Jan 21 23:00:03 2022

@author: Mohammed Amine Farez
"""

# Import the necessary libraries:
import cv2
import mediapipe as mp
import math

# Initialise MediaPipe solution for Hand Tracking:
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
mp_hands = mp.solutions.hands

# Import the Encodings of the Gestures of Interest:
Question =  [71.84, 69.43, 51.87, 38.95, 85.09, 67.78, 40.8, 37.11, 147.56, 77.88, 47.3, 42.2, 182.02, 72.99, 44.72, 39.05, 183.7, 57.56, 34.93, 32.28]
Tired =  [57.31, 55.15, 39.82, 30.89, 74.24, 62.97, 44.55, 35.51, 146.33, 73.68, 47.52, 38.47, 165.56, 71.7, 45.4, 36.35, 162.59, 54.74, 34.53, 28.28]
Critical =  [76.49, 68.45, 47.71, 32.65, 129.25, 66.1, 38.83, 33.73, 136.34, 77.67, 53.0, 34.79, 112.93, 79.16, 51.0, 33.11, 103.58, 67.12, 36.88, 24.6]
Sick = [60.22, 57.25, 44.72, 35.44, 66.71, 76.12, 45.88, 32.65, 141.6, 88.28, 47.76, 33.12, 158.07, 82.73, 46.17, 32.7, 156.7, 66.61, 37.66, 29.61]

encodings = [Question, Tired, Critical, Sick]
encodings_name = ["Question", "Tired", "Critical", "Sick"]

# For video input:
#cap = cv2.VideoCapture(r'THE PATH TO THE FILE')

# For webcam input:
cap = cv2.VideoCapture(0)

# Obtain the number of frames in Video:
len_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))

# Count the frames:
count = 0

# Launch the MediaPipe Hand Tracking with 0.5 Confidence:
with mp_hands.Hands(
    model_complexity=0,
    min_detection_confidence=0.5,
    min_tracking_confidence=0.5) as hands:

    # Iterate over each Frame:
    while cap.isOpened():
        success, image = cap.read()
        count += 1

        # Neglect the first 30 Frames for better accuracy:
        if count < 30:
            continue

        # Break if the Video comes to an end:
        if count == len_frames:
            break

        # Neglect empty Frames:
        if not success:
            print("Ignoring empty camera frame.")
            continue

        # Mark the image as not writeable to improve performance:
        image.flags.writeable = False

        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        results = hands.process(image)

        # Draw the hand annotations on the image:
        image.flags.writeable = True
        image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)

        # Initialise the lists for 2D pixel values:
        x_encoding = []
        y_encoding = []
        lml = []
        xl = []
        yl = []

        # Initialise the list for the number of matches obtained for each Hand Gesture class:
        matches = []

        # When the Hand is detected:
        if results.multi_hand_landmarks:
            # Draw the Landmarks:
            for hand_landmarks in results.multi_hand_landmarks:
                mp_drawing.draw_landmarks(
                image,
                hand_landmarks,
                mp_hands.HAND_CONNECTIONS,
                mp_drawing_styles.get_default_hand_landmarks_style(),
                mp_drawing_styles.get_default_hand_connections_style())
            # Transform the 3D points to 2D pixel values:
            for id, lm in enumerate(results.multi_hand_landmarks[0].landmark):
                #  Obtain the Height of Width of the Frame:
                h, w, _ = image.shape
                # Append the x,y coordinates for each point of the landmark:
                xc, yc = int(lm.x * w), int(lm.y * h)
                lml.append([id, xc, yc])
                xl.append(xc)
                yl.append(yc)

            # Calculate the distances between x values, and between y values:
            for i in range(0,len(xl)-1):
                x_encoding.append(abs(xl[i]-xl[i+1]))
                y_encoding.append(abs(yl[i]-yl[i+1]))

            # Calculate the Euclidean distance between each point of the Landmark:
            dist = [round(math.sqrt(i**2 +j**2),2) for i, j in zip(x_encoding, y_encoding)]

            # Compare the distances obtained with the pretrained encodings:
            for encoding in encodings:
                mat = 0

                # Normalize the distances by the first distance between point 0 and point 1:
                result = [round((i*encoding[0])/(j*dist[0]),2) for i, j in zip(dist, encoding)]

                # For each comparison, if the result is between 0.71 and 1.21, we have a match:
                for i in range(0,len(result)):
                    test = result[i]
                    if test < 1.21 and test > 0.71:
                        mat += 1
                        print(mat)

                # Append the number of matched distances with each Hand Gesture class:
                matches.append(mat)

            # Obtain the Class with the highest number of matches:
            gesture_class = max(matches)

            # Obtain the name of the Hand Gesture:
            hand_gesture = encodings_name[matches.index(gesture_class )]

            # Display the result if at least Half the distances match (10/20):
            if gesture_class > 10:
                cv2.putText(image,hand_gesture,(100, 150),cv2.FONT_HERSHEY_SIMPLEX,1,(0,255,0),2)

        # Flip the image horizontally for a selfie-view display:
        #cv2.imshow('MediaPipe Hands', cv2.flip(image, 1))
        cv2.imshow('MediaPipe Hands', image)

        # Break when Q is pressed:
        if cv2.waitKey(10) & 0xFF == ord('q'):
            break

# Destroy windows and release the camera when the process has finished:
cv2.destroyAllWindows()
cap.release()


import time
import datetime

while(1):
    now = datetime.datetime.now()
    time.sleep(1)
    if now.minute == 0 and now.second == 0:
        print("뻐꾹! 현재시간은 {} 입니다.".format(now))
    else:
        print(now)


import requests
import settings

def get_request(number):
    # Делаем запрос к api
    try:
        params_req = {"get" : "num", "format" : "json", "num" : number}
        req = requests.get(settings.url_api, params=params_req)
        res = req.json()
        return res["0"]["operator"]
    except Exception as e:
        print(e)
        return "no data"

def get_data_from_file():
    # Получаем данные из файла с номерами
    with open(settings.name_file, encoding="utf-8") as f:
        list_data = []
        for elem in f.readlines():
            list_data.append([elem.split(',')[0], elem.split(',')[1].replace('\n','')])
        return list_data

def check_data(data):
    # Проверяем совпадает ли данные из базы с госреестром
    count_true_data = 0
    count_error_data = 0
    result_list = []
    for elem in data:
        res = get_request(elem[0])
        flag_position = 0
        if res == elem[1]:
            count_true_data += 1
            flag_position = 1
        elif res == "no data":
            count_error_data += 1
            flag_position = 2
        result_list.append(",".join([elem[0], elem[1], res, str(flag_position)]))
    print(f"Checked phones: {len(data)} Is true: {count_true_data} Is false: {len(data) - count_true_data - count_error_data} Undefined: {count_error_data}")
    return result_list

def main():
    data_from_file = get_data_from_file()
    result = check_data(data_from_file)
    with open('result.txt', 'a+', encoding="utf-8") as f:
        for elem in result:
            f.write(elem + '\n')

if __name__ == "__main__":
    main()


from sklearn.metrics import silhouette_score, calinski_harabasz_score, davies_bouldin_score
from collections import defaultdict
import os
from utils import deserialize_features
import numpy as np
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt

def create_cluster_artists(artist_dict: defaultdict):
    v = np.vstack([artist_dict[k] for k in artist_dict.keys()])     #Stack vertically the arrays belonging to an artist
    y = []      #labels
    for k in artist_dict.keys():
        y = y + ([k.split('.')[0]] * len(artist_dict[k]))
    return v, y

def create_cluster_movements(artist_dict, cluster_name):
    v = np.vstack([artist_dict[k] for k in artist_dict.keys()])
    y = [cluster_name] * v.shape[0]
    return v, y

def create_scatter_plot(v, y):
    """
    Create a scatter plot to represent in a bidimensional space the paintings.
    """
    pca = PCA(n_components=100, random_state=42)
    pca_embedding = pca.fit_transform(v)
    labels = [0 if y[i] == y[0] else 1 for i in range(len(y))]      # Convert textual labels to numeric labels, which
                                                            # is what the 'c' parameter of the scatter function expects
    y = list(set(y))
    scatter = plt.scatter(*zip(*pca_embedding[:, :2]), c=labels, cmap='BrBG')
    plt.legend(handles=scatter.legend_elements()[0], labels=y)
    plt.show()

if __name__ == '__main__':
    artists_dict = defaultdict()
    dir = 'merged'

    '''
    #With this cycle you check the similarities of every possible artists pairing
    artists = os.listdir(dir)
    l = []
    for i in range(len(artists)):
        artists_dict = defaultdict()
        ar1 = artists[i].split('.')[0]
        artists_dict[ar1] = deserialize_features(os.path.join(dir, ar1+'.npy'))
        j = i+1
        while j < len(artists):
            ar2 = artists[j].split('.')[0]
            artists_dict[ar2] = deserialize_features(os.path.join(dir, ar2 + '.npy'))
            (v, y) = create_cluster_artists(artists_dict)
            l.append((silhouette_score(v, y), ar1, ar2))
            artists_dict.pop(ar2)
            j += 1
    l.sort()
    print(l)
    '''
    '''
    #Cycle to compare couples of artists
    artists = ['Vincent_van_Gogh', 'Titian']
    for artist in artists:
        artists_dict[artist] = deserialize_features(os.path.join(dir, artist+'.npy'))
    (v, y) = create_cluster_artists(artists_dict)

    '''
    #Comparison between artistic movements
    renaissance_artists = ['Sandro_Botticelli', 'Titian', 'Leonardo_da_Vinci', 'Michelangelo', 'Raphael']
    impressionism_artists = ['Claude_Monet', 'Edouard_Manet', 'Camille_Pissarro', 'Edgar_Degas', 'Pierre-Auguste_Renoir']
    artist_dict_ren = defaultdict()
    artist_dict_impr = defaultdict()

    for artist in renaissance_artists:
        artist_dict_ren[artist] = deserialize_features(os.path.join(dir, artist+'.npy'))

    for artist in impressionism_artists:
        artist_dict_impr[artist] = deserialize_features(os.path.join(dir, artist+'.npy'))

    (v0, y0) = create_cluster_movements(artist_dict_ren, 'Renaissance')
    (v1, y1) = create_cluster_movements(artist_dict_impr, 'Impressionism')
    v = np.vstack([v0, v1])
    y = y0 + y1

    create_scatter_plot(v, y)
    print(davies_bouldin_score(v, y))
    print(calinski_harabasz_score(v, y))
    print(silhouette_score(v, y))


# (c) 2021 Okawo
# This code is licensed under MIT license (see LICENSE for details)

import logging
import os
import stat
import enum

class log_levels(enum.Enum):
    DEBUG = logging.DEBUG
    INFO = logging.INFO
    WARNING = logging.WARNING
    ERROR = logging.ERROR
    CRITICAL = logging.CRITICAL

def setup_custom_logger(
    name,
    level,
    file,
    format="[%(asctime)s] %(name)s %(levelname)s - %(message)s",
    console_logging=False,
):
    logger = logging.getLogger(name)
    logger.setLevel(level)

    log_path = os.path.normpath(os.path.join(os.path.dirname(__file__), file))
    dir_p = os.path.dirname(log_path)
    if not os.path.exists(dir_p):
        os.mkdir(dir_p)

    # logging entry format
    logger_formatter = logging.Formatter(
        "[%(asctime)s] %(name)s %(levelname)s - %(message)s"
    )

    # if i don't this bullshit, it creates root owned log files in some cases
    os.chmod(dir_p, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO)
    if not os.path.exists(log_path):
        with open(log_path, 'wt') as f:
            f.write('fuck you')
    os.chmod(log_path, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO)

    logger_handler = logging.FileHandler(log_path)
    logger_handler.setLevel(level)
    logger_handler.setFormatter(logger_formatter)
    logger.addHandler(logger_handler)

    if console_logging:
        console_logger_handler = logging.StreamHandler()
        console_logger_handler.setLevel(level)
        console_logger_handler.setFormatter(logger_formatter)
        logger.addHandler(console_logger_handler)

    return logger


import numpy as np
import re
from pyparsing import Word, hexnums, WordEnd, Optional, alphas, alphanums, printables, ParseException
import os

MAX_OPCODE_LENGHT = 64
NUMBER_OF_INSTRUCTIONS = 3200

regex = re.compile(r'[\n\r\t\?\;\_\'\-]')
hex_integer = Word(hexnums) + WordEnd()
search = Optional('.') + Word(alphas,alphanums)('section') + ":" + hex_integer('adress') + Optional((hex_integer*(1,))("instructions") + Word(alphas,alphanums)("opcode")) + Optional(Word(alphas,alphanums)('first_operant')) + Optional("," + Word(printables)('second_operant'))

PROCESS_VERSION = 3

empty_files_counter = 0
empty_file_ids = []

small_files_counter = 0
small_file_ids = []

super_small_files_counter = 0
super_small_file_ids = []

def pad(A, length):
    arr = np.zeros(length)
    arr[:len(A)] = A
    return arr

def pad2D(A, height, width):
    c_height, c_width = A.shape
    return np.pad(A, ((0,(height-c_height)),(0,width-c_width)), 'constant')

def asm_to_np(file):
    RESULT = []
    counter = 0
    counter_2 = 0
    for line in file.readlines():
            line = regex.sub(" ", line)
            try:
                line = search.parseString(line)
                section = line.section.encode(encoding = 'ASCII', errors = 'ignore')
                adress = bytes.fromhex(''.join(line.adress.asList() if not line.adress == None else ''))
                opcode = line.opcode.encode(encoding = 'ASCII', errors = 'ignore') if not line.opcode == '' else ' '.encode(encoding = 'ASCII', errors = 'ignore')
                operants = f'{line.first_operant},{line.second_operant}'.encode(encoding = 'ASCII', errors = 'ignore')
                instructions = bytes.fromhex(''.join(line.instructions.asList() if not line.instructions == '' else ''))
                byteline = section[1].to_bytes(1, byteorder='big')+adress[2:]+instructions[:4]+opcode[0].to_bytes(1, byteorder='big')+operants
                np_array = np.frombuffer(byteline, dtype='uint8')
                np_array = pad(np_array[0:int(MAX_OPCODE_LENGHT/8)],int(MAX_OPCODE_LENGHT/8))
                RESULT.append(np_array)
                counter += 1
                if counter >= NUMBER_OF_INSTRUCTIONS: break
            except ParseException as e:
                continue
            except ValueError as e:
                continue
            counter_2 += 1
            if counter_2 >= 12000:
                break
    RESULT = pad2D(np.asarray(RESULT)[:NUMBER_OF_INSTRUCTIONS,:int(MAX_OPCODE_LENGHT/8)],NUMBER_OF_INSTRUCTIONS,int(MAX_OPCODE_LENGHT/8))
    return RESULT

def preprocess(file_name:str, origin_folder:str, dest_folder:str):
    asm_path = f"{origin_folder}/{file_name}.asm"
    vec_path = f"{dest_folder}/{PROCESS_VERSION}/{file_name}_{NUMBER_OF_INSTRUCTIONS}_{int(MAX_OPCODE_LENGHT/8)}.npy"
    if not os.path.exists(vec_path):
        f = open(asm_path, "r", encoding = 'unicode_escape', errors='ignore')
        output = asm_to_np(f)
        np.save(vec_path,output)
    else:
        output = np.load(vec_path)
    return output


import Tkinter
import random
import math

import numpy as np

from PIL import Image,ImageTk,ImageOps, ImageEnhance, ImageDraw

class App():
	def __init__(self):

		self.seed_value = np.random.randint(2 ** 32)

		print (self.seed_value)

		np.random.seed(self.seed_value)

		self.grid_x = 384 * 2
		self.grid_y = 384 * 2

		self.display_x = 700
		self.display_y = 700

		self.save_images = False

		if self.save_images:
			try:
				os.makedirs("pics/" + str(self.seed_value))
			except OSError as exc:
				if exc.errno == errno.EEXIST and os.path.isdir(path):
					pass

		self.top = Tkinter.Tk()
		self.w = Tkinter.Canvas(self.top, width = self.display_x * 2, height = self.display_y);self.w.pack()
		self.im = Image.new("RGB",(self.grid_x,self.grid_y),"white")
		self.p = ImageTk.PhotoImage(self.im)

		self.im_graph = Image.new("RGB",(5000,1000),"white")
		self.im_graph_draw = ImageDraw.Draw(self.im_graph)
		self.p_graph = ImageTk.PhotoImage(self.im_graph.resize((700,700),Image.ANTIALIAS))

		self.timer = None
		self.reset()
		self.bit = self.w.create_image(0,0, image = self.p, anchor=Tkinter.NW)
		self.bit_graph = self.w.create_image(self.display_x,0, image = self.p_graph,anchor=Tkinter.NW)
		self.b = Tkinter.Button(text="Start",command = self.calc_rules);self.b.pack()

	def reset(self):

		self.sets = {}

		self.generation = 0
		self.last_generation = 0

		self.old_rule_count = 0

		self.old_sums = None

		if self.timer:
			self.top.after_cancel(self.timer)
			self.timer = None

		self.grid_and_rules = np.zeros( (self.grid_x,self.grid_y,19), dtype=np.uint8)

		d = [0.0,
			 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,
			 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0]

		do = [1.0,
			 1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,
			 1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0]

		s = self.grid_x / 4 #np.random.randint(6,7)

		for x in xrange(s,self.grid_x ,s * 2):
			for y in xrange(s,self.grid_y ,s * 2):

				for r in xrange(19):
					d[r] = np.random.random() * do[r]

				for r in xrange(19):
					self.grid_and_rules[(x - s):(x + s),(y - s):(y + s),r] =  np.random.random((s * 2,s * 2)) < d[r]

		self.color_me()

	def calc_rules(self):

		for counter in xrange(1):

			accum = np.zeros( (self.grid_x,self.grid_y,19), dtype=np.uint8 )

			temp = self.grid_and_rules

			for x in xrange(1,19):
				temp[:,:,x] *= temp[:,:,0]

			for dy in [-1,0,1]:
				for dx in [-1,0,1]:

					left = np.roll(temp,dx,axis = 0)
					up = np.roll(left,dy,axis = 1)

					accum += up

			grid = accum[:,:,0]

			for x in xrange(1,19):

				temp[:,:,x] = (accum[:,:,x] > 0) & (grid > 0) & (((accum[:,:,x] == (grid )) )| ((temp[:,:,0] == 0)))

			on_rules = temp[:,:,1:10]
			off_rules = temp[:,:,10:]

			accum2 = np.zeros( (self.grid_x,self.grid_y), dtype=np.uint8 )

			for x in xrange(0,9):
				accum2 |= (grid == (on_rules[:,:,x] * (x + 1))) & (temp[:,:,0] == 1) & (grid > 0)
				accum2 |= ((grid + 1) == (off_rules[:,:,x] * (x + 1))) & (temp[:,:,0] == 0)

			self.grid_and_rules[:,:,0] = accum2
			self.grid_and_rules[:,:,1:10] = on_rules
			self.grid_and_rules[:,:,10:] = off_rules

		self.generation += 1

		self.color_me()

		self.timer = self.top.after_idle(self.calc_rules)

	def color_me(self):

		new_keys = 0

		colors = np.zeros( (self.grid_x,self.grid_y,3), dtype=np.uint8 )

		lookup = [0,
					10,11,12,1,2,3,
					13,14,15,4,5,6,
					16,17,18,7,8,9]

		color_lookups = [tuple([0,0,0])] * 19

		for x in xrange(1,7):
			colors[:,:,1] += self.grid_and_rules[:,:,lookup[x]] * 2 ** (x )
			color_lookups[lookup[x]] = tuple([0,42 * (x),0])

		for x in xrange(7,13):
			colors[:,:,2] += self.grid_and_rules[:,:,lookup[x]] * 2 ** (x - 6)
			color_lookups[lookup[x]] = tuple([0,0,42 * (x - 6)])

		for x in xrange(13,19):
			colors[:,:,0] += self.grid_and_rules[:,:,lookup[x]] * 2 ** (x - 12)
			color_lookups[lookup[x]] = tuple([42 * (x - 12),0,0])

		colors[:,:,0] *= self.grid_and_rules[:,:,0] + 1
		colors[:,:,1] *= self.grid_and_rules[:,:,0] + 1
		colors[:,:,2] *= self.grid_and_rules[:,:,0] + 1

		if self.generation % 50 == 0:

			rule_sets = self.grid_and_rules[:,:]
			rule_sets = rule_sets.reshape(-1,rule_sets.shape[2])
			unique_rule_sets = np.unique(rule_sets.view(rule_sets.dtype.descr * rule_sets.shape[1]))

			sums = [0] * 19

			if self.old_rule_count > 0:

				gen_diff = self.generation - self.last_generation

				gen_mod = self.generation % 5000

				if gen_mod < (self.last_generation % 5000):
					self.im_graph_draw.rectangle((0,0,5000,1000),fill="white")

				self.im_graph_draw.line((gen_mod - gen_diff, 1000 - self.old_rule_count,gen_mod,1000 - len(unique_rule_sets)),fill="purple")

				for x in xrange(19):
					sums[x] = (np.sum(self.grid_and_rules[:,:,x]) * 1000) / (self.grid_x * self.grid_y)

					self.im_graph_draw.line((gen_mod - gen_diff, 1005 - self.old_sums[x] ,gen_mod,1005 - sums[x]),fill=color_lookups[x])
			else:
				for x in xrange(19):
					sums[x] = (np.sum(self.grid_and_rules[:,:,x]) * 1000) / (self.grid_x * self.grid_y)

			self.old_rule_count = len(unique_rule_sets)

			self.old_sums = sums

			self.last_generation = self.generation

			self.p_graph = ImageTk.PhotoImage(self.im_graph.resize((700,700),Image.ANTIALIAS))

			self.bit_graph = self.w.create_image(self.display_x,0, image = self.p_graph,anchor=Tkinter.NW)

		temp = self.im

		colors[(colors == 0).all(axis = -1)] = [0,0,0]

		self.im = Image.fromarray(colors)

		temp2 = Image.blend(temp,self.im,1.0)

		self.p = ImageTk.PhotoImage(temp2.resize((self.display_x,self.display_y),Image.ANTIALIAS))

		self.im = Image.blend(temp,self.im,1.0)

		self.bit = self.w.create_image(0,0, image = self.p, anchor=Tkinter.NW)

		if self.save_images and self.generation % 5 == 0:
			self.im.resize((384*2,384*2),Image.ANTIALIAS).save("pics/" + str(self.seed_value) + "image." + str(self.generation/5) + ".jpeg")

app=App()
app.top.mainloop()


from django.test import SimpleTestCase, Client
from opentracing.mocktracer import MockTracer
from django.conf import settings
import opentracing
import mock

from signalfx_tracing.libraries.django_.instrument import (
    config,
    get_middleware_and_setting_name,
)
from signalfx_tracing import instrument

from .conftest import DjangoTestSuite

class TestDjangoOpenTracingMiddleware(SimpleTestCase, DjangoTestSuite):
    def test_set_global_tracer_middleware_sanity(self):
        config.set_global_tracer = True
        config.tracer_callable = "opentracing.mocktracer.MockTracer"
        config.tracer_parameters = dict(scope_manager=None)
        instrument(django=True)

        client = Client()
        client.get("/one/")
        tracer = opentracing.tracer
        span = tracer.finished_spans().pop()

        assert span.tags["path"] == "/one/"
        assert span.tags["method"] == "GET"

        assert settings.OPENTRACING_TRACER_CALLABLE == config.tracer_callable
        assert settings.OPENTRACING_TRACER_PARAMETERS == dict(scope_manager=None)
        assert settings.OPENTRACING_TRACER._tracer is opentracing.tracer

    def test_unset_global_tracer_middleware_sanity(self):
        config.set_global_tracer = False
        tracer = MockTracer()
        instrument(tracer, django=True)
        client = Client()
        client.get("/one/")

        span = tracer.finished_spans().pop()
        assert span.tags["path"] == "/one/"
        assert span.tags["method"] == "GET"

        assert not hasattr(settings, "OPENTRACING_TRACER_CALLABLE")
        assert not hasattr(settings, "OPENTRACING_TRACER_PARAMETERS")
        assert settings.OPENTRACING_TRACER.tracer is tracer
        assert opentracing.tracer is not tracer

    def test_middleware_untraced(self):
        client = Client()
        with mock.patch("opentracing.span.Span.set_tag") as set_tag:
            client.get("/one/")
            assert set_tag.called is False

        assert not hasattr(settings, "OPENTRACING_TRACED_ATTRIBUTES")
        assert not hasattr(settings, "OPENTRACING_TRACER_CALLABLE")
        assert not hasattr(settings, "OPENTRACING_TRACER_PARAMETERS")
        assert not hasattr(settings, "OPENTRACING_TRACE_ALL")
        assert not hasattr(settings, "OPENTRACING_SET_GLOBAL_TRACER")
        assert not hasattr(settings, "OPENTRACING_TRACING")
        assert not hasattr(settings, "OPENTRACING_TRACER")
        _, setting = get_middleware_and_setting_name()
        assert config.middleware_class not in getattr(settings, setting)


from argparse import ArgumentParser
from pathlib import Path

import medianizer.app_logger as app_logger
from medianizer.file_location import FileLocation
from medianizer.metadata import Metadata

logger = app_logger.get_logger(__name__)

def parse_args():
    parser = ArgumentParser()
    parser.add_argument('--path', help='Specify the path of the media files',
                        required=True)
    parser.add_argument('--from-name', required=False, action='store_true',
                        help='Try to fetch creation date from name. By default'
                             ', creation date fetched from metadata only')
    return parser.parse_args()

class Executor:
    def __init__(self, path, date_from_name=None):
        self.path = path
        self.date_from_name = date_from_name
        self.location = FileLocation(self.path)
        self.metadata = Metadata()

    def process_image(self):
        for image in self.location.locate_jpg_files(self.path):
            try:
                with open(image, 'rb') as img:
                    date = self.metadata.fetch_date(img, self.date_from_name)
                    file_name = f'{date["date"]}-{date["date_time"]}.jpg'
                    new_image_path = Path(self.path, 'medianizer_sorted',
                                          date['year'], date['month'],
                                          file_name).resolve()
                    if image.name == file_name:
                        continue
                    logger.info(f'Rename file {image} to {file_name}')
                    if self.location.verify_path(new_image_path):
                        logger.info('The image with the same metadata '
                                    'already exists in the dest path. Moving '
                                    f'the {image} to "manual_sort" directory')
                        file_name = f'duplicate_of_{file_name}'
                        image.rename(image.parent/file_name)
                        self.location.media_sort(image.parent/file_name,
                                                 manual_sort=True)
                        continue
                    image.rename(image.parent/file_name)
                    self.location.media_sort(image.parent/file_name,
                                             dir=new_image_path.parent)
            except IOError:
                logger.error(f'Unable to open the {image} file.')
            except KeyError:
                logger.error(f'Unable to read the {image} EXIF metadata. '
                             'Moving the image to the "manual_sort" directory')
                self.location.media_sort(image, manual_sort=True)

def main():
    args = parse_args()
    executor = Executor(args.path, args.from_name)
    logger.info('Start medianizer app')
    if not executor.location.verify_path(args.path):
        raise ValueError('The given path does not exist')
    logger.info(f'The given path has been found: {args.path}')
    executor.process_image()

if __name__ == '__main__':
    main()


# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import os

from setuptools import find_packages, setup

try:
    here = os.path.dirname(os.path.abspath(__file__))
    description = open(os.path.join(here, "README")).read()
except OSError:
    description = ""

version = "0.0"

with open(os.path.join(here, "requirements.txt")) as f:
    dependencies = f.read().splitlines()

dependency_links = []

setup(
    name="talos",
    version=version,
    description="Performance testing framework for Windows, Mac and Linux.",
    long_description=description,
    classifiers=[
        "Programming Language :: Python :: 2.7",
        "Programming Language :: Python :: 2 :: Only",
    ],
    # Get strings from http://www.python.org/pypi?%3Aaction=list_classifiers
    author="Mozilla Foundation",
    author_email="tools@lists.mozilla.org",
    url="https://wiki.mozilla.org/Buildbot/Talos",
    license="MPL",
    packages=find_packages(exclude=["ez_setup", "examples", "tests"]),
    include_package_data=True,
    package_data={
        "": [
            "*.config",
            "*.css",
            "*.gif",
            "*.htm",
            "*.html",
            "*.ico",
            "*.js",
            "*.json",
            "*.manifest",
            "*.php",
            "*.png",
            "*.rdf",
            "*.sqlite",
            "*.svg",
            "*.xml",
            "*.xul",
        ]
    },
    zip_safe=False,
    install_requires=dependencies,
    dependency_links=dependency_links,
    entry_points="""
      # -*- Entry points: -*-
      [console_scripts]
      talos = talos.run_tests:main
      talos-results = talos.results:main
      """,
    test_suite="tests",
)


import json

lt = [
    {'name': '王静', 'age': '7', 'height': 130},
    {'name': '闫晓红', 'age': 13, 'height': 160},
    {'name': '刘惠芬', 'age': 16, 'height': 158},
    {'name': '赵铁柱', 'age': 20, 'height': 170}
]

string = json.dumps(lt, ensure_ascii=False)

# print(string)
obj = json.loads(string)

print(type(obj))


import redis
import time

conn = redis.Redis()

while True:

    cities = conn.keys()

    for city in cities:

        d = conn.hgetall(city)

        for ref in d:
            if int(d[ref]) > 1:
                count = int(d[ref])
                count -= 1
                d[ref] = str(count)

        conn.hmset(city,d)

    time.sleep(2)


import pickle
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import itertools
import os

results = None

# matplotlib.rcParams.update({'font.size': 12})

color_list = plt.cm.tab10(np.linspace(0, 1, 10))
colors = {'lstm': color_list[0], 'pf_e2e': color_list[1], 'pf_ind_e2e': color_list[2], 'pf_ind': color_list[3]}
labels = {'lstm': 'LSTM', 'pf_e2e': 'DPF (e2e)', 'pf_ind_e2e': 'DPF (ind+e2e)', 'pf_ind': 'DPF (ind)', 'ff': 'FF', 'odom': 'Odom. baseline'}
# conditions = ['normal', 'no_motion_likelihood', 'learn_odom', 'no_proposer']
# conditions = ['normal', 'learn_odom', 'no_inject']
# clabels = {'normal': 'Default', 'no_motion_likelihood': 'W/o motion likelihood', 'learn_odom': 'Learned odometry', 'no_proposer': 'W/o particle proposer', 'no_inject': "No inject"}

if False:
    test_conditions = ['odom5_imgTG', 'odom10_imgTG']

    conditions = ['orig_odom5_imgTG', 'odom5_imgTG_odom5_imgTG',
                  'odom10_imgTG_odom5_imgTG', 'odom5_imgTG_odom10_imgTG',
                    'odom10_imgTG_odom10_imgTG', 'orig_odom10_imgTG']

    clabels = {'orig_odom5_imgTG':'5',
               'odom5_imgTG_odom5_imgTG':'mo5,me5',
               'odom5_imgTG_odom10_imgTG':'mo5,me10',
               'orig_odom10_imgTG':'10',
               'odom10_imgTG_odom10_imgTG': 'mo10,me10',
               'odom10_imgTG_odom5_imgTG': 'mo10,me5',
               'odom5_imgTG': '5',
               'odom10_imgTG': '10',
                }
    xlabels = ['A', 'B']
    exp = 'swapmo'
else:

    test_conditions = ['odom10_imgG', 'odom10_imgTG']

    conditions = ['orig_odom10_imgG', 'odom10_imgG_odom10_imgG', 'odom10_imgG_odom10_imgTG',
                  'odom10_imgTG_odom10_imgG', 'odom10_imgTG_odom10_imgTG', 'orig_odom10_imgTG']

    # clabels = {'orig_odom10_imgG':'G',
    #            'odom10_imgG_odom10_imgG':'meG,moG',
    #            'odom10_imgG_odom10_imgTG':'meG,moTG',
    #            'orig_odom10_imgTG':'TG',
    #            'odom10_imgTG_odom10_imgTG': 'meTG,moTG',
    #            'odom10_imgTG_odom10_imgG': 'meTG,moG',
    #            'odom10_imgG': 'G',
    #            'odom10_imgTG': 'TG',
    #             }

    clabels = {'orig_odom10_imgG':'A(A)*',
           'odom10_imgG_odom10_imgG':'A(A)',
           'odom10_imgG_odom10_imgTG':'A(B)',
           'orig_odom10_imgTG':'B(B)*',
           'odom10_imgTG_odom10_imgTG': 'B(B)',
           'odom10_imgTG_odom10_imgG': 'B(A)',
           'odom10_imgG': 'A',
           'odom10_imgTG': 'B',
            }
    xlabels = ['C', 'D']
    exp = 'swapme'

vmax = 0.4

task = 'nav02'
# methods = ['pf_ind', 'pf_e2e', 'pf_ind_e2e', 'lstm']
methods = ['pf_ind_e2e']

# load results
results = dict()

count = 0
for cond in conditions:
    # log_path = '/home/rbo/Desktop/log/'+task+'_ab1'
    log_path = '../log/'+exp+'/'+cond
    for filename in [f for f in os.listdir(log_path) if os.path.isfile(os.path.join(log_path, f))]:
        full_filename = os.path.join(log_path, filename)
        print('loading {}:'.format(count) + full_filename + ' ...')
        try:
            # if 'DeepThought' not in filename:
            # if 'DeepThought' in filename:
            with open(full_filename, 'rb') as f:
                result = pickle.load(f)
                # result_name = result['task'][0] + '/' + result['method'][0] + '/' + str(result['num_episodes'][0]) + '/' + result['condition'][0]
                result_name = cond #+ '_' + result['exp_params'][0]['file_ending'] #result['exp_params'][0]['task'] + '/' + result['exp_params'][0]['method'] + '/' + str(result['exp_params'][0]['num_episodes']) + '/' + result['exp_params'][0]['ab_cond']
                print(result_name)
                if result_name not in results.keys():
                    results[result_name] = result
                else:
                    for key in result.keys():
                        if key in results[result_name].keys():
                            results[result_name][key] += result[key]
                        else:
                            results[result_name][key] = result[key]
                        # print(result_name, key)
                count += 1
        except Exception as e:
            print(e)
            print('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!')

print()
for result_name, r in results.items():
    print(result_name, len(r['exp_params']))
    # print(result_name, len(r['test_odom5_imgTG_mse']))

print('Loaded {} results'.format(count))

task = 'nav02'
step = 3

episodes = [1000]
# episodes = [1000]

means = []
ses = []

for c, condition in enumerate(conditions):

    means.append(np.zeros([len(test_conditions), 5]))
    ses.append(np.zeros([len(test_conditions), 5]))

    for tc, test_condition in enumerate(test_conditions):

        result_name = condition
        if result_name in results.keys():
            result = results[result_name]

            hist = np.array([[h[i] for i in range(0, 50, 10)] for h in result['test_'+test_condition+'_hist' ]])  # result x time x sqe [.0, 0.1, .., 10.0]
            err = 1. - np.sum(hist[:,:,:10], axis=-1) # sqe < 1.0
            # err = np.sum(hist[:,:,:10], axis=-1) # sqe < 1.0
            print(result_name, err)
            means[c][tc] = np.mean(err, axis=0)
            ses[c][tc] = np.std(err, axis=0, ddof=1) / np.sqrt(len(err))

        else:
            print(result_name + 'not found')
            means[tc] *= np.nan
            ses[tc] *= np.nan

# means[num_episodes]['min'] = np.stack([means[num_episodes][method] for method in methods], axis=0).min(axis=1)

# ax = fig.add_subplot(111)
# # Turn off axis lines and ticks of the big subplot
# ax.spines['top'].set_color('none')
# ax.spines['bottom'].set_color('none')
# ax.spines['left'].set_color('none')
# ax.spines['right'].set_color('none')
# ax.tick_params(labelcolor='w', top='off', bottom='off', left='off', right='off')

m = np.array(means)[:, :, step].T
s = np.array(ses)[:, :, step].T

for i in range(2):
    z = m[i, :]
    y = s[i, :]
    x = [[z[0]] * 2 + [z[2]] * 2,
         [z[1]] * 2 + [z[2]] * 2,
         [z[3]] * 2 + [z[5]] * 2,
         [z[3]] * 2 + [z[4]] * 2]
    plt.figure(i, [1.35,1.35])
    plt.imshow(x, interpolation='nearest', vmin=-0.33*vmax, vmax=vmax, cmap='viridis_r')
    plt.plot([-0.5, 3.5],[1.5, 1.5], '-w', linewidth=0.5)
    plt.plot([1.5, 1.5],[-0.5, 3.5], '-w', linewidth=0.5)

    for j, x_coord, y_coord, value, s_value in [
            (0, 0.5, 0, z[0], y[0]),
            (1, 0.5, 1, z[1], y[1]),
            (2, 2.5, 0.5, z[2], y[2]),
            (3, 0.5, 2.5, z[3], y[3]),
            (5, 2.5, 2, z[5], y[5]),
            (4, 2.5, 3, z[4], y[4])]:
        if j == 0 or j == 5:
            # text = '{:.4s}*\n+-{:.4s}'.format('{:.3f}'.format(value)[1:],'{:.2f}'.format(s_value)[1:])
            text = ' {:.4s}*'.format('{:.3f}'.format(value)[1:],'{:.2f}'.format(s_value)[1:])
        else:
            # text = '{:.4s}\n+-{:.4s}'.format('{:.3f}'.format(value)[1:],'{:.2f}'.format(s_value)[1:])
            text = '{:.4s}'.format('{:.3f}'.format(value)[1:],'{:.2f}'.format(s_value)[1:])

        plt.text(x_coord, y_coord, text, va='center', ha='center', color='white', fontweight='normal')

    plt.gca().set_aspect('equal')

    plt.xlabel('Motion model')
    plt.xticks([0.5, 2.5], xlabels)
    plt.ylabel('Measurem. model')
    plt.yticks([0.5, 2.5], xlabels)
    plt.tight_layout(0.0, 0.0, 0.0)
    print('saving')
    plt.savefig('../plots/cr/'+exp+'%s.pdf' % i, bbox_inches="tight", transparent=True, dpi=600, frameon=True, facecolor='w', pad_inches=0.01)

plt.figure('colorbar', [0.6, 1.35])
a = np.array([[0.0, 0.3]])
img = plt.imshow(a, cmap="viridis_r", vmin=-0.33*vmax, vmax=vmax)
plt.gca().set_visible(False)
cax = plt.axes([0.0, 0.2, 0.1, 0.65])
plt.colorbar(orientation="vertical", cax=cax, label='Error rate', boundaries=np.linspace(0,0.4,100), ticks=np.linspace(0.0, 0.4, 5))

plt.savefig('../plots/cr/colorbar.pdf'.format(s), transparent=True, dpi=600, frameon=False, facecolor='w', pad_inches=0.01)

plt.show()


# // Given a MxN matrix where each element can either be 0 or 1.
# We need to find the shortest path between a given source cell to a
# destination cell. The path can only be created out of a cell if its
# value is 1.

# // Sample input:
# // {{1, 1, 1, 1},
# //  {1, 0, 0, 1},
# //  {1, 1, 1, 1}}

# //  {0,0} -> {2, 1}

# // Sample output:
# // 3

from collections import deque

def isValid(grid, x, y):
    if 0 <= x < len(grid) and 0 <= y < len(grid[0]) and grid[x][y] != 0:
        return True
    else:
        return False

def minDistance(grid, start, end):
    # empty conditions
    if not start or not end or not grid or not grid[0]:
        return -1
    # validity check
    if not(isValid(grid, start[0], start[1])) or not(isValid(grid, end[0], end[1])):
        return -1
    # trivial
    if start == end:
        return 0
    # breadth first search
    q = deque([(start[0], start[1], 0)])
    visited = set()
    visited.add((start[0], start[1]))
    while q:
        x, y, depth = q.popleft()
        if x == end[0] and y == end[1]:
            return depth
        else:
            for i, j in ((x+1, y), (x-1, y), (x, y+1), (x, y-1)):
                if isValid(grid, i, j) and (i, j) not in visited:
                    q.append((i, j, depth+1))
                    visited.add((i, j))
    return -1

A = [[1, 0, 1, 1],
     [1, 1, 0, 1],
     [1, 1, 1, 1]]

s = (0, 0)
e = (2, 1)

print(minDistance(A, e, s))


from django.urls import path, include
from . import views
from django.conf.urls import url
from django.views.generic import TemplateView

REACT_ROUTES = [
    'service-list',
    'think-bank',
    'post/',
    r'^post/(?P<user>\d+)/(?P<id>\d+)/$',
    'userbank/',
    r'^userbank/(?P<userbankid>\d+)/$',
]

urlpatterns = [
    path('', views.index),
    url(r'^(%s)?$' % '|'.join(REACT_ROUTES),
        TemplateView.as_view(template_name='frontend/index.html')),
]


import requests
import json

def challenge3(page):
    url = "https://www.python-spider.com/api/challenge3"
    payload = f"page={page}"
    headers = {
        'cookie': 'm=74ccb88b480335eac2a340f0f3846979|1675243619000'
    }
    response = requests.request("POST", url, headers=headers, data=payload)
    return response.text

def run():
    data_num = 0
    for page in range(1, 101):
        response_text = challenge3(page)
        res_dict = json.loads(response_text)
        data_list = res_dict.get('data')
        print(data_list)
        for data in data_list:
            data_num += int(data.get('value'))
        print(data_num)
    print(data_num)

if __name__ == '__main__':
    run()

猿人学练习/3js加密js第六节实战例题/main.py


import sqlite3

class DataBase:
    def __init__(self):
        print("test")

    def __retrieve(self, query):
        connection = sqlite3.connect(self.__db_CPU_Load.db)
        cursor = connection.cursor()
        cursor.execute(query)
        rows = cursor.fetchall()
        count = 0
        results = {}
        for row in rows:
            results[count] = dict(zip([c[0] for c in cursor.description], row))
            count += 1
        connection.close()
        return  results

    def get_last(self, value="10"):
        if not value.isnumeric():
           value = 10
        else:
            value = abs(int(value))
        query = f"SELECT * FROM {self.CPULoad}"\
                f"  ORDER BY created_at DESC"\
            f"  LIMIT {value}"
        return self.__retrieve(query)

    def get_in_last(self, value="1", period="MINUTES"):
        period = period.upper()
        if period not in (
            'SECONDS', 'MINUTES', 'HOURS', 'DAYS'
            'WEEKS', 'MONTHS', 'YEARS'):
            period = 'MINUTES'

        if not value.isnumeric():
           value = -1
        else:
           value = -abs(int(value))
        query = f"SELECT * FROM {self.CPULoad}"\
                f"  WHERE created_at > datetime('now', '{value} {period}')"\
                f"  ORDER BY created_at DESC"
        return self.__retrieve(query)


# -*- coding: cp936 -*-
import os
import shutil
import datetime

fileWritedHeadline=open("headline.txt",'w')
def las2txt(filePath_forwardLog,filePath_txtLog):
        fileOpened_LasLog=open(filePath_forwardLog,'r')
        fileWrited_txtLog=open(filePath_txtLog.lower().replace(".las",".txt"),'w')
        lineIndex=0
        flag=0
        logSeriesList=[]
        firstMeet = 1  ## 标志第一空行不写
        numOfseriers = 0
        valueList =[]
        for line in fileOpened_LasLog.readlines():
            lineIndex+=1
            if flag==1 and line.startswith('~'): ##结束读取~c段
                flag=4
            if line.startswith('~C'): ##开始读取~c段
                flag=1
            if  flag==1 and (not (line.startswith('~') or line.startswith('#') )): ##写Headline
                splitLine=line.split()
                logSeriesList.append(splitLine[0])

            if line.startswith('~A'):  #开始写数据
                flag=2
            if  flag==2 :  #开始写测井系类头数据
                temp='\t'.join(logSeriesList)
                fileWrited_txtLog.write(temp+'\n')
                flag=3
                numOfseriers = len(logSeriesList)

            if  flag==3 and (not (line.startswith('~') or line.startswith('#') )): #开始写数据行
                splitLine=line.split()
                valueList.extend(splitLine)
                if len(valueList) == numOfseriers:
                    fileWrited_txtLog.write('\t'.join(valueList)+'\n')
                    valueList=[]

        fileWritedHeadline.write(os.path.basename(filePath_forwardLog)+"\t"+'\t'.join(logSeriesList)+"\n")
        fileWrited_txtLog.close()
        print(filePath_forwardLog+"convert to txtlog complete.")

if __name__=="__main__":
    starttime = datetime.datetime.now()

    sourceDirPath="log_las"
    goalDirPath='log_txt'

    print ('prepare las to ',goalDirPath)
    if os.path.exists(goalDirPath):
        shutil.rmtree(goalDirPath)
    os.mkdir(goalDirPath)

    ##  把操作目录下文件存入filenameslist
    fileNames=os.listdir(sourceDirPath)
    for wellItem in fileNames:
        print ('Las2txt...'+'-'*10,wellItem)
        fileOpened=sourceDirPath+'\\'+wellItem
        fileWrited=goalDirPath+'\\'+wellItem
        las2txt(fileOpened,fileWrited)

    fileWritedHeadline.close()
    #long running
    endtime = datetime.datetime.now()
    print ("耗时(秒):" + str((endtime - starttime).seconds))
    print("处理完毕")


"""
Simplified histogram function, that is able to be parallelised using JAX/XLA.
"""
import jax.numpy as np
from jax import jit, vmap

@jit
def _get_bin(x, bin_edges):
    n = bin_edges.shape[0] - 1
    a_min = bin_edges[0]
    a_max = bin_edges[-1]

    bin = np.array(n * (x - a_min) / (a_max - a_min), int)

    return bin

_mapped_get_bin = vmap(_get_bin, (0, None), 0)

@jit
def _histogram(rt_array, bin_edges):
    rt_array = np.ravel(rt_array)
    bins_map = _mapped_get_bin(rt_array, bin_edges)
    counts = np.bincount(bins_map, None, length=bin_edges.shape[0])[:-1]

    return counts


from __future__ import unicode_literals
import numpy as np
from collections import Counter
from convlab2.policy.larl.multiwoz.latent_dialog.utils import Pack
import json
from nltk.tokenize import WordPunctTokenizer
import logging

PAD = '<pad>'
UNK = '<unk>'
USR = 'YOU:'
SYS = 'THEM:'
BOD = '<d>'
EOD = '</d>'
BOS = '<s>'
EOS = '<eos>'
SEL = '<selection>'
SPECIAL_TOKENS_DEAL = [PAD, UNK, USR, SYS, BOD, EOS]
SPECIAL_TOKENS = [PAD, UNK, USR, SYS, BOS, BOD, EOS, EOD]
STOP_TOKENS = [EOS, SEL]
DECODING_MASKED_TOKENS = [PAD, UNK, USR, SYS, BOD]

class DealCorpus(object):
    def __init__(self, config):
        self.config = config
        self.train_corpus = self._read_file(self.config.train_path)
        self.val_corpus = self._read_file(self.config.val_path)
        self.test_corpus = self._read_file(self.config.test_path)
        self._extract_vocab()
        self._extract_goal_vocab()
        self._extract_outcome_vocab()
        print('Loading corpus finished.')

    def _read_file(self, path):
        with open(path, 'r') as f:
            data = f.readlines()

        return self._process_dialogue(data)

    def _process_dialogue(self, data):
        def transform(token_list):
            usr, sys = [], []
            ptr = 0
            while ptr < len(token_list):
                turn_ptr = ptr
                turn_list = []
                while True:
                    cur_token = token_list[turn_ptr]
                    turn_list.append(cur_token)
                    turn_ptr += 1
                    if cur_token == EOS:
                        ptr = turn_ptr
                        break
                all_sent_lens.append(len(turn_list))
                if turn_list[0] == USR:
                    usr.append(Pack(utt=turn_list, speaker=USR))
                elif turn_list[0] == SYS:
                    sys.append(Pack(utt=turn_list, speaker=SYS))
                else:
                    raise ValueError('Invalid speaker')

            all_dlg_lens.append(len(usr) + len(sys))
            return usr, sys

        new_dlg = []
        all_sent_lens = []
        all_dlg_lens = []
        for raw_dlg in data:
            raw_words = raw_dlg.split()

            # process dialogue text
            cur_dlg = []
            words = raw_words[raw_words.index(
                '<dialogue>') + 1: raw_words.index('</dialogue>')]
            words += [EOS]
            usr_first = True
            if words[0] == SYS:
                words = [USR, BOD, EOS] + words
                usr_first = True
            elif words[0] == USR:
                words = [SYS, BOD, EOS] + words
                usr_first = False
            else:
                print('FATAL ERROR!!! ({})'.format(words))
                exit(-1)
            usr_utts, sys_utts = transform(words)
            for usr_turn, sys_turn in zip(usr_utts, sys_utts):
                if usr_first:
                    cur_dlg.append(usr_turn)
                    cur_dlg.append(sys_turn)
                else:
                    cur_dlg.append(sys_turn)
                    cur_dlg.append(usr_turn)
            if len(usr_utts) - len(sys_utts) == 1:
                cur_dlg.append(usr_utts[-1])
            elif len(sys_utts) - len(usr_utts) == 1:
                cur_dlg.append(sys_utts[-1])

            # process goal (6 digits)
            # FIXME FATAL ERROR HERE !!!
            cur_goal = raw_words[raw_words.index(
                '<partner_input>') + 1: raw_words.index('</partner_input>')]
            # cur_goal = raw_words[raw_words.index('<input>')+1: raw_words.index('</input>')]
            if len(cur_goal) != 6:
                print('FATAL ERROR!!! ({})'.format(cur_goal))
                exit(-1)

            # process outcome (6 tokens)
            cur_out = raw_words[raw_words.index(
                '<output>') + 1: raw_words.index('</output>')]
            if len(cur_out) != 6:
                print('FATAL ERROR!!! ({})'.format(cur_out))
                exit(-1)

            new_dlg.append(Pack(dlg=cur_dlg, goal=cur_goal, out=cur_out))

        print('Max utt len = %d, mean utt len = %.2f' % (
            np.max(all_sent_lens), float(np.mean(all_sent_lens))))
        print('Max dlg len = %d, mean dlg len = %.2f' % (
            np.max(all_dlg_lens), float(np.mean(all_dlg_lens))))
        return new_dlg

    def _extract_vocab(self):
        all_words = []
        for dlg in self.train_corpus:
            for turn in dlg.dlg:
                all_words.extend(turn.utt)
        vocab_count = Counter(all_words).most_common()
        raw_vocab_size = len(vocab_count)
        discard_wc = np.sum([c for t, c in vocab_count])
        print('vocab size of train set = %d,\n' % (raw_vocab_size,) +
              'cut off at word %s with frequency = %d,\n' % (vocab_count[-1][0], vocab_count[-1][1]) +
              'OOV rate = %.2f' % (1 - float(discard_wc) / len(all_words),))
        self.vocab = SPECIAL_TOKENS_DEAL + \
            [t for t, cnt in vocab_count if t not in SPECIAL_TOKENS_DEAL]
        self.vocab_dict = {t: idx for idx, t in enumerate(self.vocab)}
        self.unk_id = self.vocab_dict[UNK]

        global DECODING_MASKED_TOKENS
        from string import ascii_letters, digits
        letter_set = set(list(ascii_letters + digits))
        vocab_list = [t for t, cnt in vocab_count]
        masked_words = []
        for word in vocab_list:
            tmp_set = set(list(word))
            if len(letter_set & tmp_set) == 0:
                masked_words.append(word)
        # DECODING_MASKED_TOKENS += masked_words
        print('Take care of {} special words (masked).'.format(len(masked_words)))

    def _extract_goal_vocab(self):
        all_goal = []
        for dlg in self.train_corpus:
            all_goal.extend(dlg.goal)
        vocab_count = Counter(all_goal).most_common()
        raw_vocab_size = len(vocab_count)
        discard_wc = np.sum([c for t, c in vocab_count])

        print('goal vocab size of train set = %d, \n' % (raw_vocab_size,) +
              'cut off at word %s with frequency = %d, \n' % (vocab_count[-1][0], vocab_count[-1][1]) +
              'OOV rate = %.2f' % (1 - float(discard_wc) / len(all_goal),))
        self.goal_vocab = [UNK] + [g for g, cnt in vocab_count]
        self.goal_vocab_dict = {t: idx for idx,
                                t in enumerate(self.goal_vocab)}
        self.goal_unk_id = self.goal_vocab_dict[UNK]

    def _extract_outcome_vocab(self):
        all_outcome = []
        for dlg in self.train_corpus:
            all_outcome.extend(dlg.out)
        vocab_count = Counter(all_outcome).most_common()
        raw_vocab_size = len(vocab_count)
        discard_wc = np.sum([c for t, c in vocab_count])

        print('outcome vocab size of train set = %d, \n' % (raw_vocab_size,) +
              'cut off at word %s with frequency = %d, \n' % (vocab_count[-1][0], vocab_count[-1][1]) +
              'OOV rate = %.2f' % (1 - float(discard_wc) / len(all_outcome),))
        self.outcome_vocab = [UNK] + [o for o, cnt in vocab_count]
        self.outcome_vocab_dict = {t: idx for idx,
                                   t in enumerate(self.outcome_vocab)}
        self.outcome_unk_id = self.outcome_vocab_dict[UNK]

    def get_corpus(self):
        id_train = self._to_id_corpus('Train', self.train_corpus)
        id_val = self._to_id_corpus('Valid', self.val_corpus)
        id_test = self._to_id_corpus('Test', self.test_corpus)
        return id_train, id_val, id_test

    def _to_id_corpus(self, name, data):
        results = []
        for dlg in data:
            if len(dlg.dlg) < 1:
                continue
            id_dlg = []
            for turn in dlg.dlg:
                id_turn = Pack(utt=self._sent2id(turn.utt),
                               speaker=turn.speaker)
                id_dlg.append(id_turn)
            id_goal = self._goal2id(dlg.goal)
            id_out = self._outcome2id(dlg.out)
            results.append(Pack(dlg=id_dlg, goal=id_goal, out=id_out))
        return results

    def _sent2id(self, sent):
        return [self.vocab_dict.get(t, self.unk_id) for t in sent]

    def _goal2id(self, goal):
        return [self.goal_vocab_dict.get(g, self.goal_unk_id) for g in goal]

    def _outcome2id(self, outcome):
        return [self.outcome_vocab_dict.get(o, self.outcome_unk_id) for o in outcome]

    def sent2id(self, sent):
        return self._sent2id(sent)

    def goal2id(self, goal):
        return self._goal2id(goal)

    def outcome2id(self, outcome):
        return self._outcome2id(outcome)

    def id2sent(self, id_list):
        return [self.vocab[i] for i in id_list]

    def id2goal(self, id_list):
        return [self.goal_vocab[i] for i in id_list]

    def id2outcome(self, id_list):
        return [self.outcome_vocab[i] for i in id_list]

class NormMultiWozCorpus(object):
    logger = logging.getLogger()

    def __init__(self, config):
        self.bs_size = 94
        self.db_size = 30
        self.bs_types = ['b', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'b', 'b', 'b', 'b', 'b', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'b', 'b', 'c', 'c', 'c', 'b', 'b', 'b', 'b', 'b', 'c', 'c', 'c', 'c', 'c',
                         'c', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'b', 'b', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'b', 'b', 'b', 'b', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'b', 'b', 'b']
        self.domains = ['hotel', 'restaurant', 'train',
                        'attraction', 'hospital', 'police', 'taxi']
        self.info_types = ['book', 'fail_book', 'fail_info', 'info', 'reqt']
        self.config = config
        self.tokenize = lambda x: x.split()
        self.train_corpus, self.val_corpus, self.test_corpus = self._read_file(
            self.config)
        self._extract_vocab()
        self._extract_goal_vocab()
        self.logger.info('Loading corpus finished.')

    def _read_file(self, config):
        train_data = json.load(open(config.train_path))
        valid_data = json.load(open(config.valid_path))
        test_data = json.load(open(config.test_path))

        train_data = self._process_dialogue(train_data)
        valid_data = self._process_dialogue(valid_data)
        test_data = self._process_dialogue(test_data)

        return train_data, valid_data, test_data

    def _process_dialogue(self, data):
        new_dlgs = []
        all_sent_lens = []
        all_dlg_lens = []

        for key, raw_dlg in data.items():
            norm_dlg = [Pack(speaker=USR, utt=[BOS, BOD, EOS], bs=[
                             0.0]*self.bs_size, db=[0.0]*self.db_size)]
            for t_id in range(len(raw_dlg['db'])):
                usr_utt = [BOS] + self.tokenize(raw_dlg['usr'][t_id]) + [EOS]
                sys_utt = [BOS] + self.tokenize(raw_dlg['sys'][t_id]) + [EOS]
                norm_dlg.append(Pack(speaker=USR, utt=usr_utt,
                                     db=raw_dlg['db'][t_id], bs=raw_dlg['bs'][t_id]))
                norm_dlg.append(Pack(speaker=SYS, utt=sys_utt,
                                     db=raw_dlg['db'][t_id], bs=raw_dlg['bs'][t_id]))
                all_sent_lens.extend([len(usr_utt), len(sys_utt)])
            # To stop dialog
            norm_dlg.append(Pack(speaker=USR, utt=[BOS, EOD, EOS], bs=[
                            0.0]*self.bs_size, db=[0.0]*self.db_size))
            # if self.config.to_learn == 'usr':
            #     norm_dlg.append(Pack(speaker=USR, utt=[BOS, EOD, EOS], bs=[0.0]*self.bs_size, db=[0.0]*self.db_size))
            all_dlg_lens.append(len(raw_dlg['db']))
            processed_goal = self._process_goal(raw_dlg['goal'])
            new_dlgs.append(Pack(dlg=norm_dlg, goal=processed_goal, key=key))

        self.logger.info('Max utt len = %d, mean utt len = %.2f' % (
            np.max(all_sent_lens), float(np.mean(all_sent_lens))))
        self.logger.info('Max dlg len = %d, mean dlg len = %.2f' % (
            np.max(all_dlg_lens), float(np.mean(all_dlg_lens))))
        return new_dlgs

    def _extract_vocab(self):
        all_words = []
        for dlg in self.train_corpus:
            for turn in dlg.dlg:
                all_words.extend(turn.utt)
        vocab_count = Counter(all_words).most_common()
        raw_vocab_size = len(vocab_count)
        keep_vocab_size = min(self.config.max_vocab_size, raw_vocab_size)
        oov_rate = np.sum(
            [c for t, c in vocab_count[0:keep_vocab_size]]) / float(len(all_words))

        self.logger.info('cut off at word {} with frequency={},\n'.format(vocab_count[keep_vocab_size - 1][0],
                                                                          vocab_count[keep_vocab_size - 1][1]) +
                         'OOV rate = {:.2f}%'.format(100.0 - oov_rate * 100))

        vocab_count = vocab_count[0:keep_vocab_size]
        self.vocab = SPECIAL_TOKENS + \
            [t for t, cnt in vocab_count if t not in SPECIAL_TOKENS]
        self.vocab_dict = {t: idx for idx, t in enumerate(self.vocab)}
        self.unk_id = self.vocab_dict[UNK]
        self.logger.info("Raw vocab size {} in train set and final vocab size {}".format(
            raw_vocab_size, len(self.vocab)))

    def _process_goal(self, raw_goal):
        res = {}
        for domain in self.domains:
            all_words = []
            d_goal = raw_goal[domain]
            if d_goal:
                for info_type in self.info_types:
                    sv_info = d_goal.get(info_type, dict())
                    if info_type == 'reqt' and isinstance(sv_info, list):
                        all_words.extend(
                            [info_type + '|' + item for item in sv_info])
                    elif isinstance(sv_info, dict):
                        all_words.extend(
                            [info_type + '|' + k + '|' + str(v) for k, v in sv_info.items()])
                    else:
                        print('Fatal Error!')
                        exit(-1)
            res[domain] = all_words
        return res

    def _extract_goal_vocab(self):
        self.goal_vocab, self.goal_vocab_dict, self.goal_unk_id = {}, {}, {}
        for domain in self.domains:
            all_words = []
            for dlg in self.train_corpus:
                all_words.extend(dlg.goal[domain])
            vocab_count = Counter(all_words).most_common()
            raw_vocab_size = len(vocab_count)
            discard_wc = np.sum([c for t, c in vocab_count])

            self.logger.info('================= domain = {}, \n'.format(domain) +
                             'goal vocab size of train set = %d, \n' % (raw_vocab_size,) +
                             'cut off at word %s with frequency = %d, \n' % (vocab_count[-1][0], vocab_count[-1][1]) +
                             'OOV rate = %.2f' % (1 - float(discard_wc) / len(all_words),))

            self.goal_vocab[domain] = [UNK] + [g for g, cnt in vocab_count]
            self.goal_vocab_dict[domain] = {
                t: idx for idx, t in enumerate(self.goal_vocab[domain])}
            self.goal_unk_id[domain] = self.goal_vocab_dict[domain][UNK]

    def get_corpus(self):
        id_train = self._to_id_corpus('Train', self.train_corpus)
        id_val = self._to_id_corpus('Valid', self.val_corpus)
        id_test = self._to_id_corpus('Test', self.test_corpus)
        return id_train, id_val, id_test

    def _to_id_corpus(self, name, data):
        results = []
        for dlg in data:
            if len(dlg.dlg) < 1:
                continue
            id_dlg = []
            for turn in dlg.dlg:
                id_turn = Pack(utt=self._sent2id(turn.utt),
                               speaker=turn.speaker,
                               db=turn.db, bs=turn.bs)
                id_dlg.append(id_turn)
            id_goal = self._goal2id(dlg.goal)
            results.append(Pack(dlg=id_dlg, goal=id_goal, key=dlg.key))
        return results

    def _sent2id(self, sent):
        return [self.vocab_dict.get(t, self.unk_id) for t in sent]

    def _goal2id(self, goal):
        res = {}
        for domain in self.domains:
            d_bow = [0.0] * len(self.goal_vocab[domain])
            for word in goal[domain]:
                word_id = self.goal_vocab_dict[domain].get(
                    word, self.goal_unk_id[domain])
                d_bow[word_id] += 1.0
            res[domain] = d_bow
        return res

    def id2sent(self, id_list):
        return [self.vocab[i] for i in id_list]

    def pad_to(self, max_len, tokens, do_pad):
        if len(tokens) >= max_len:
            return tokens[: max_len-1] + [tokens[-1]]
        elif do_pad:
            return tokens + [0] * (max_len - len(tokens))
        else:
            return tokens


import gridfs,pymongo
from bson import ObjectId
from datetime import datetime, timedelta
from werkzeug.exceptions import BadRequest
from src.definitions.status_codes import ResponseStatus, RESPONSE_MSG_MAP
from src.definitions.status_codes import key_mapper, default_data

class DbFunctions:
    def __init__(self):
        pass

    @classmethod
    def update_one(cls, database, collection_name, query={}, fields={}, upsert=False,email=""):
        fields.update({"updated_by":email,"updated_at":datetime.now()})
        database.get_collection(collection_name).update_one(query, {"$set": fields}, upsert=upsert)

    @classmethod
    def update_many(cls, database, collection_name, query={}, fields={}):
        database.get_collection(collection_name).update_many(query, {"$set": fields})

    @classmethod
    def insert_one(cls, database, collection_name, data,email=""):
        data.update({"createdby": email, "created_at": datetime.now()})
        object_id = (
            database.get_collection(collection_name).insert_one(data).inserted_id
        )
        return object_id

    @classmethod
    def insert_many(cls, database, collection_name, data):
        database.get_collection(collection_name).insert_many(data)

    @classmethod
    def delete_one(cls, database, collection_name, data, query={}):
        database.get_collection(collection_name).delete_one(data, query)

    @classmethod
    def find_all(cls, database, collection_name, data, query={}):
        data = {i: 1 for i in data}
        read_data = list(
            database.get_collection(collection_name).find(
                query, data, sort=[("_id", pymongo.DESCENDING)]
            )
        )
        for item in read_data:
            if isinstance(item, dict):
                item["_id"] = str(item["_id"])
        return read_data

    @classmethod
    def find_one(cls, database, collection_name, data, query={}):
        data = {i: 1 for i in data}
        data.update({"_id": 0})
        read_data = database.get_collection(collection_name).find_one(query, data) or {}
        return read_data

    @classmethod
    def update_inc(cls, database, collection_name, query={}, fields={}, upsert=False):
        database.get_collection(collection_name).update_one(
            query, {"$inc": fields}, upsert=upsert
        )

    @classmethod
    def push(cls, database, collection_name, query={}, fields={}, ip="", upsert=False):
        try:
            fields["history"].update({"ip_address": ip})
        except:
            pass
        database.get_collection(collection_name).update_one(
            query, {"$push": fields}, upsert=upsert
        )

    @classmethod
    def aggregate(cls, database, collection_name, pip):
        piplines = ["$match", "$unwind", "$match", "$project"]
        project = pip[3]
        data = {"_id": 0}
        for field in project:
            if "." in field:
                data.update({field.split(".")[-1]: field})
            else:
                data.update({field[1:]: 1})
        pip[3] = data
        pipline = [{val: pip[indx]} for indx, val in enumerate(piplines)]
        return list(database.get_collection(collection_name).aggregate(pipline))

    @classmethod
    def distinct(cls, database, collection_name, fields, query={}):
        return database.get_collection(collection_name).distinct(fields, query)

    @classmethod
    def latest_record(cls, database, collection_name, query={}):
        try:
            data = list(
                database.get_collection(collection_name)
                .find(query)
                .sort("_id", -1)
                .limit(1)
            )[0]
        except IndexError:
            data = {
                "email": query["email"],
                "attempt": 0,
                "valid_upto": datetime(1900, 1, 1),
                "status": False,
                "logged_in_once": False,
            }
            data["_id"] = cls.insert_one(
                database=database, collection_name=collection_name, data=data
            )
        return data

    @staticmethod
    def savefile(database, batch_file, filename):
        gridfs.GridFS(database, collection="CMS_FILE").put(
            batch_file, filename=filename
        )

    @classmethod
    def update_many_(
        cls, database, collection_name, queries=[], fields=[], upsert=False
    ):
        for num, query in enumerate(queries):
            database.get_collection(collection_name).update_one(
                query, {"$set": fields[num]}, upsert=upsert
            )

class RequestResponse:
	__slots__ = ["coll_name", "query", "db_name", "operation", "data", "fields", "pipeline", "response", "email",
	             "password", "res", "token", "logger_id", "ip_address","cifrado"]

	def __init__(self, request):
		self.response = ResponseStatus.Sucess
		self.res = default_data.NULL
		self.logger_id, self.email = default_data.STR, ""
		try:
			self.ip_address = request.remote_addr
		except AttributeError:
			pass
		self.attr_setter(request)
		try:
			if not self.email:
				self.email = self.query.get(key_mapper.EMAIL, "")
		except AttributeError:
			pass
		self.email = self.email.lower()

	def attr_setter(self, request):
		try:
			for key, val in request.args.items():
				setattr(self, key, val)
		except:
			pass
		try:
			try:
				request_json = request.get_json()
			except:
				request_json = request
			for key, val in request_json.items():
				if key == "query":
					if isinstance(val, list):
						for num, rec in enumerate(val):
							if rec.get("_id") is not None:
								val[num]["_id"] = ObjectId(rec.get("_id"))
					else:
						if val.get("_id") is not None:
							val["_id"] = ObjectId(val.get("_id"))
				setattr(self, key, val)
		except (BadRequest,AttributeError,TypeError):
			pass

	def to_dict(self):

		return {"msg": RESPONSE_MSG_MAP.get(self.response.name),
		        "res": self.res, "response_code": self.response.value,
		        "logger_id": self.logger_id}

class AttrDict(dict):

	def __getattr__(self, attr):
		return self.get(attr, "")

	def __setattr__(self, attr, value):
		self[attr] = value

def type_convertor(a):
	return str(a)

def time_convertor(time_obj, weeks=default_data.ZERO, minutes=default_data.ZERO, days=default_data.ZERO,
                   years=default_data.ZERO, hours=default_data.ZERO, months=default_data.ZERO,
                   seconds=default_data.ZERO):
	return time_obj + timedelta(
		minutes=minutes,
		days=days,
		seconds=seconds,
		hours=hours,
		weeks=weeks)


from abc import ABC, abstractmethod
import torch
from .loss import build_loss

class TaskHead(torch.nn.Module, ABC):

    def __init__(self):
        torch.nn.Module.__init__(self)

    @abstractmethod
    def forward(self, images, texts):
        pass

    @abstractmethod
    def compute_loss(self, source, target=None, labels=None):
        pass

class Classification(TaskHead):

    def __init__(self, loss, embed_dim=512, num_classes=3):
        super().__init__()
        self.classification_head = torch.nn.Linear(embed_dim, num_classes)
        self.softmax = torch.nn.Softmax(dim=1)
        self.loss = loss

    def forward(self, composed):
        return self.classification_head(composed)

    def probabilities(self, composed):
        logits = self(composed)
        return self.softmax(logits)

    def compute_loss(self, source, target=None, labels=None):
        logits = self(source)
        preds = torch.argmax(logits, dim=1)
        accuracy = (preds == labels).sum().item() / len(labels)
        loss_value = self.loss(logits, labels)
        metrics = {"loss": loss_value.item(),
                   "accuracy": accuracy}
        return loss_value, metrics

class Regression(TaskHead):

    def __init__(self, loss, embed_dim=512):
        super().__init__()
        self.regression_head = torch.nn.Linear(embed_dim, 1)
        self.loss = loss

    def forward(self, composed):
        return torch.sigmoid(self.regression_head(composed))

    def compute_loss(self, source, target=None, labels=None):
        output = self(source)
        loss_value = self.loss(output, labels.float())
        metrics = {"loss": loss_value.item()}
        return loss_value, metrics

class NormalizationLayer(torch.nn.Module):
    """Class for normalization layer."""

    def __init__(self, normalize_scale=1.0, learn_scale=True):
        super().__init__()
        self.norm_s = torch.log(torch.FloatTensor([normalize_scale]))
        if learn_scale:
            self.norm_s = torch.nn.Parameter(self.norm_s)
        self.epsilon = 1e-9

    def forward(self, x):
        norm = torch.norm(x, dim=1, keepdim=True).expand_as(x)
        factor = torch.exp(self.norm_s)
        features = factor * x / (norm + self.epsilon)
        return features

class Metric(TaskHead):

    def __init__(self, loss, initial_normalization_factor=4.0):
        super().__init__()
        self.loss = loss
        self.normalization_layer = NormalizationLayer(
            normalize_scale=initial_normalization_factor, learn_scale=True)

    def forward(self, composed):
        return self.normalization_layer(composed)

    def compute_loss(self, source, target, labels=None):
        source_emb = self.forward(source)
        target_emb = self.forward(target)

        assert source_emb.shape[1] == target_emb.shape[1]
        loss_value = self.loss(source_emb, target_emb, labels=labels)
        metrics = {"loss": loss_value.item()}
        if torch.isnan(loss_value):
            import IPython; IPython.embed()
        return loss_value, metrics

def get_task_head(cfg):
    loss_obj, task = build_loss(cfg)

    if task == "metric":
        head = Metric(loss_obj, cfg.MODEL.INITIAL_NORMALIZATION_FACTOR)
    elif task == "regression":
        head = Regression(loss_obj, cfg.MODEL.EMBED_DIM)
    else:
        head = Classification(loss_obj, embed_dim=cfg.MODEL.EMBED_DIM,
                              num_classes=cfg.DATASET.NUM_CLASSES)

    return head, task


"""Platform for Eloverblik sensor integration."""
import logging
from homeassistant.const import ENERGY_KILO_WATT_HOUR
from homeassistant.helpers.entity import Entity
from pyeloverblik.eloverblik import Eloverblik
from pyeloverblik.models import TimeSeries

_LOGGER = logging.getLogger(__name__)
from .const import DOMAIN

async def async_setup_entry(hass, config, async_add_entities):
    """Set up the sensor platform."""
    eloverblik = hass.data[DOMAIN][config.entry_id]

    sensors = []
    sensors.append(EloverblikEnergy("Eloverblik Energy Total", 'total', eloverblik))
    for x in range(1, 25):
        sensors.append(EloverblikEnergy(f"Eloverblik Energy {x-1}-{x}", 'hour', eloverblik, x))
    async_add_entities(sensors)

class EloverblikEnergy(Entity):
    """Representation of a Sensor."""

    def __init__(self, name, sensor_type, client, hour=None):
        """Initialize the sensor."""
        self._state = None
        self._data_date = None
        self._data = client
        self._hour = hour
        self._name = name
        self._sensor_type = sensor_type

        if sensor_type == 'hour':
            self._unique_id = f"{self._data.get_metering_point()}-{hour}"
        else:
            self._unique_id = f"{self._data.get_metering_point()}-total"

    @property
    def name(self):
        """Return the name of the sensor."""
        return self._name

    @property
    def unique_id(self):
        """The unique id of the sensor."""
        print(f"uniq: {self._unique_id}")
        return self._unique_id

    @property
    def state(self):
        """Return the state of the sensor."""
        return self._state

    @property
    def device_state_attributes(self):
        """Return state attributes."""
        attributes = dict()
        attributes['Metering date'] = self._data_date

        return attributes

    @property
    def unit_of_measurement(self):
        """Return the unit of measurement."""
        return ENERGY_KILO_WATT_HOUR

    def update(self):
        """Fetch new state data for the sensor.
        This is the only method that should fetch new data for Home Assistant.
        """
        self._data.update()

        self._data_date = self._data.get_data_date()

        if self._sensor_type == 'total':
            self._state = self._data.get_total_day()
        else:
            self._state = self._data.get_usage_hour(self._hour)