# Copyright (c) 2019-2020, SPEAR3 authors (see AUTHORS.txt).
# Licensed under the BSD 3-clause license (see LICENSE.txt)

import numpy as np
import asyncio
import datetime as dt

from ..utils.spear3_api import within_spear3, _get_loss_rate
from ..utils.spear3_lt_opt import _get_origin, _set_origin, x_to_pv

async def _get_objective(x, connection, duration=1, max_step=None):
    assert len(x) == 13, 'Input dimension must be equal to 13'

    if max_step is None:
        x1 = x.reshape(1, -1)
        df = _set_origin(x1, connection)
        await asyncio.sleep(0.5)
    else:
        x0 = _get_origin(connection)
        x1 = x.reshape(1, -1)
        dx_max = np.max(np.abs(x1 - x0))
        step_num = np.ceil(dx_max / max_step)

        steps = np.linspace(x0, x1, step_num + 1)[1:]
        for step in steps:
            # set quadrupole PV values
            df = _set_origin(step, connection)
            await asyncio.sleep(0.5)

    # calculate objective
    lossrate = await _get_loss_rate(connection, duration)
    obj = np.array([-lossrate])

    return obj

@within_spear3
async def get_objective(x, connection, duration=1, max_step=None):
    return await _get_objective(x, connection, duration, max_step)

def evaluator_generator(connection, vrange=(-20, 20), duration=1, max_step=None, fixed_values=[]):
    async def evaluate(X, hook):
        assert type(X) == np.ndarray, 'Input X must be a numpy array'

        X1 = X
        if X.shape[1] < 13:
            Xf = np.tile(np.array(fixed_values), (X.shape[0], 1))
            X1 = np.hstack((X, Xf))
            assert X1.shape[1] == 13, f'Length of fixed values must be {13 - X.shape[1]}'

        PV = x_to_pv(X1, vrange[0], vrange[1])

        Y = np.zeros((PV.shape[0], 1))
        for i, pv in enumerate(PV):
            obj = await get_objective(pv, connection, duration, max_step)
            finished_time = dt.datetime.now().strftime("%m-%d-%Y_%H-%M-%S")
            Y[i] = obj
            if hook:
                hook(pv, obj, finished_time)

        return Y

    return evaluate


from bs4 import BeautifulSoup
import requests

def searchInURL():
    #assigning the Required web page to the url variable
    source= "https://en.wikipedia.org/wiki/Deep_learning"
    #the data in the URL is converted into the text and save in the response
    response= requests.get(source).text
    #parsing the data into beautiful soup in lxml format
    soup= BeautifulSoup(response, "lxml")

    # pretty-printing(to print the data in the HTML format)
    # soup.prettify()

    # Printing the title
   # wikititle =soup.title
    wikititle = soup.title.text
    print("Title is:",wikititle)

    # Writing results into a file
    with open("output1.html", "a") as file:
        file.write(str(wikititle))

    #looping through the all a tags
    for links in soup.find_all('a'):
        # fetching href's
        hrefs=links.get('href')
        with open("output1.html", "a") as file:
            file.write(str(hrefs))
            file.write("\n")
        print(hrefs)

if __name__== '__main__':
    #function call
    searchInURL()


from threading import Thread
from threading import current_thread
import time

# 自定义线程类
class CustomThread(Thread):

    # 提示： 如果自己提供了init方法，那么不会自动调用父类里面的init方法
    def __init__(self, num1, num2):
        # 手动调用父类的init方法
        super(CustomThread, self).__init__() # => super().init()
        self.num1 = num1
        self.num2 = num2

    # 模拟非常复杂的任务
    def task1(self):
        time.sleep(1)
        print("任务1执行完成", self.num1)

    # 模拟非常复杂的任务
    def task2(self):
        time.sleep(1)
        print("任务2执行完成", self.num2)

    # 执行自定义线程类里面的任务同一在run方法里面执行
    def run(self):
        print(current_thread())
        self.task1()
        self.task2()

if __name__ == '__main__':
    # 创建自定义线程对象
    custom_thread = CustomThread(1, 2)
    # run方法不能直接使用对象调用，如果这样调用不是在子线程里面执行，而是在主线程执行
    # custom_thread.run()
    # 提示： 以后启动线程统一使用start方法，start方法内部会自动调用run方法
    custom_thread.start()

06-自定义线程类.py


from collections import defaultdict
my_dict = defaultdict(int)

f = open('hightemp.txt', 'r')
for line in f:
    line = line.split()
    my_dict[line[0]] += 1

for k, v in sorted(my_dict.items(), key=lambda x: x[1], reverse=True):
    print(k, v)

#reverse=Trueで降順ソートができる
#list = [1,5,2,3,6,7]があったとして
#sorted(list)すると
#[1,2,3,5,6,7]
#sorted(list, reverse=True)すると
#[7,6,5,3,2,1]


from allennlp.common.testing import ModelTestCase
import stance_detection

class Seq2VecClassifierTest(ModelTestCase):
    def setUp(self):
        super(Seq2VecClassifierTest, self).setUp()
        self.set_up_model(
            'C:/Users/nuaax/PycharmProjects/consensus_prediction_allan/experiments/config_BiLSTM.json',
            'C:/Users/nuaax/PycharmProjects/consensus_prediction_allan/tests/fixtures/test_crossnet_reader.txt'
        )

    def test_model_can_train_save_and_load(self):
        self.ensure_model_can_train_save_and_load(self.param_file)


import logging

log = logging.getLogger(__name__)

class HawkResponseMiddleware:

    def process_response(self, request, response):
        is_hawk_request = False
        hawk_auth_was_processed = False
        if request.META.get('HTTP_AUTHORIZATION', '').startswith('Hawk'):
            is_hawk_request = True

        hawk_auth_was_processed = 'hawk.receiver' in request.META
        receiver = request.META.get('hawk.receiver', None)

        log.debug('receiver? {rec}; hawk auth processed? {auth}'
                  .format(rec=receiver, auth=hawk_auth_was_processed))
        if is_hawk_request and not hawk_auth_was_processed:
            # This is a paranoid check to make sure Django
            # isn't misconfigured.
            raise RuntimeError('Django did not handle an incoming '
                               'Hawk request properly')

        if receiver:
            # Sign our response, so clients can trust us.
            log.debug('Hawk signing the response')
            receiver.respond(content=response.content,
                             content_type=response['Content-Type'])
            response['Server-Authorization'] = receiver.response_header
        else:
            log.debug('NOT Hawk signing the response, not a Hawk request')

        return response


"""apetype tests Module
Defines TestTask that allows running a full test on a TaskBase derived class.
To run the test, create a class that inherits first from TestTask and then
from the Task class to be tested. Initiate and run.

The tests Module further defines all the tests for testing apetype itself.

Example:
>>> from apetype.tests import TestTask, TaskBase
... import numpy as np
... class Task(TaskBase):
...     a: int = 5
...     b: int = 3
...
...     def ar(_, a, b) -> np.ndarray:
...         return np.array([a,2,3])+b
...     def aplus10(_, ar) -> np.ndarray:
...         return ar+10
...     def amaal10(_, ar, aplus10) -> np.ndarray:
...         return ar*aplus10
... class TestCase(TestTask, Task):
...     a = 1
...     b = 1
...     ar = np.array([3,3,4])
...     aplus10 = np.array([3,3,4])
...     amaal10 = np.array([3,3,4])
... testcase = TestCase()
... testcase.run_test()
"""
import unittest
from ..tasks import TaskBase

class TestTask(unittest.TestCase):
    """Provide the TaskBase class to be tested either
    by creating a new class that first inherits from
    TestTask and then from the TaskBase, or initiate
    a TestTask instance passing the TaskBase class as
    an argument

    Args:
      TaskClass (None|TaskBase): class to be tested.
    """
    def __init__(self, *args, TaskClass=None, **kwargs):
        # Initialize unittest.TestCase
        super().__init__(*args, **kwargs)

        if TaskClass or (len(self.__class__.mro()) > 3 and
                issubclass(self.__class__.mro()[3], TaskBase)):
            self._task = (
                TaskClass if TaskClass else self.__class__.mro()[3]
            )(parse=False)

            # Gather defined subtask outputs
            self._expected_output = {
                subtask: self.__getattribute__(subtask)
                for subtask in self._task._output_functions
            }

            # Gather parameter settings
            self._task_param_input = {
                p[0].replace('-',''):self.__getattribute__(p[0].replace('-',''))
                for p in self._task._settings
                if hasattr(self, p[0].replace('-',''))
            }

    def setUp(self):
        if hasattr(self, '_task'):
            self._task(**self._task_param_input)

    def test_run(self):
        if not hasattr(self, '_task'): return

        # Run the task
        self._task.run()

        # Assert task output
        for subtask in self._expected_output:
            self.assertEqual(
                self._expected_output[subtask],
                self._task._output[subtask]
            )

# Example Task and TestCase (that does not fail)
# serving as an integration test for apetype
class ExampleTask(TaskBase):
    a: int = 5
    b: int = 3

    def aplusb(_, a, b) -> int:
        return a+b
    def amaalaplusb(_, a, aplusb) -> int:
        return a*aplusb

class ExampleTestCase(TestTask, ExampleTask):
    a = 1
    b = 1
    aplusb = 2
    amaalaplusb = 2


#!/usr/bin/env python3

import os
import sys
import socket

from urllib.request import urlopen

from path import path
from mbhlib import sh

HERE = path(__file__).parent
JD = HERE.parent.parent.parent
USER = path(os.environ.get('USER', os.environ.get('USERPROFILE'))).basename()
HOST = socket.gethostname().split('.')[0]
if not USER:
    try:
        USER = os.getlogin()
    except AttributeError:
        USER = 'dbuch'

HOME = path(os.environ['HOME'])

def main():
    sysargs = sys.argv[1:]
    if '-h' in sysargs or '--help' in sysargs:
        sys.stdout.write('Usage: install [--janus]\n')
        return 2

    install_scripts()
    install_binaries()

    if '--janus' in sysargs:
        install_janus()
    return 0

def _install_link(frompath, topath):
    sh('rm -rf "%s"' % topath)
    sh('ln -s "{}" "{}"'.format(frompath, topath))

def _dircopy(prefix, dirname, ext='.py'):
    for pth in (JD/dirname).files():
        topath = HOME/prefix/pth.name
        _install_link(pth, topath)

def _bincopy(dirname, ext='.py'):
    _dircopy('bin', dirname, ext=ext)

def install_scripts():
    _bincopy('scripts')

def install_binaries():
    sh('cd {junkdrawer}/src && make && make install'.format(junkdrawer=JD))

def install_janus():
    if (HOME/'.vim'/'README.md').isfile():
        cwd = os.getcwd()
        os.chdir(HOME/'.vim')
        sh('rake')
    else:
        bootstrap = HOME/'.janus-bootstrap.sh'
        if not bootstrap.isfile():
            bootstrap.write_bytes(
                urlopen('https://raw.github.com/carlhuda/janus/master/bootstrap.sh').read()
            )
        bootstrap.chmod(0o755)
        sh(bootstrap.abspath())


# import python libraries
import numpy as np
import cv2
from matplotlib import pyplot as plt
from collections import deque
# import project dependencies
from functions import *

class movingAverage():

    ## constructor
    def __init__(self, nb_periods):
        self.queue = deque(maxlen=nb_periods)

    def next(self, val):
        self.queue.append(val)
        return list(self.queue)

class undistorter():

    ## constructor
    def __init__(self, Images, nx_corners, ny_corners):
        # attributes
        self.mtx = None
        self.dist = None
        # Init arrays for storing 3d object and 2d image points
        ObjPts = []
        ImgPts = []
        # prepare object points (adapted these two nasty lines from Udacity)
        objp = np.zeros((ny_corners * nx_corners, 3), np.float32)
        objp[:, :2] = np.mgrid[0:nx_corners, 0:ny_corners].T.reshape(-1, 2)
        # Step through chessboard images
        for img in Images:
            # Convert to grayscale
            gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
            # Find chessboard corners
            ret, corners = cv2.findChessboardCorners(gray, (nx_corners, ny_corners), None)
            # Check if corners were found
            if ret == True:
                # Draw the corners
                cv2.drawChessboardCorners(img, (nx_corners, ny_corners), corners, ret)
                # Append object and image points
                ObjPts.append(objp)
                ImgPts.append(corners)
        # grab calibration image dimensions
        imgShape = (Images[0].shape[1], Images[0].shape[0])
        # calculate calibration parameters
        ret, self.mtx, self.dist, rvecs, tvecs = cv2.calibrateCamera(ObjPts, ImgPts, imgShape, None, None)

    ## undistort an image
    ## returns undistorted image
    def undistort(self, img):
        result = cv2.undistort(img, self.mtx, self.dist, None, self.mtx)
        return result

class transformer():

    ## constructor
    def __init__(self, img, pts, x_offset, y_offset):
        self.example = img
        self.mask = pts
        self.x = x_offset
        self.y = y_offset
        self.M = None
        self.Minv = None

    ### display polygon region for perspective transform
    ### no return
    def plot_mask(self):
        cv2.polylines(self.example, self.mask, True, (255, 0, 0), thickness=4)
        fig = plt.imshow(self.example)
        plt.grid()
        plt.show()

    ### calculate transformation matrix
    ### no return
    def create_transformation(self):
        # get image dims
        img_size = self.example.shape
        # Cast source points to float
        src = np.float32(self.mask)
        # Define 4 destination points
        dst = np.float32([[self.x, self.y], [img_size[1] - self.x, self.y],
                          [img_size[1] - self.x, img_size[0] - self.y],
                          [self.x, img_size[0] - self.y]])
        # Get M, the transform matrix
        self.M = cv2.getPerspectiveTransform(src, dst)
        # Get Minv, the inverse matrix for unwarping later on
        self.Minv = cv2.getPerspectiveTransform(dst, src)

    ### transform image to top-down perspective
    ### returns warped image
    def warp(self, img):
        result = cv2.warpPerspective(img, self.M, (img.shape[1], img.shape[0]))
        return result

    ### transform image back to original perspective
    ### returns unwarped image
    def unwarp(self, img):
        result = cv2.warpPerspective(img, self.Minv, (self.example.shape[1], self.example.shape[0]))
        return result

class laneFinder():

    ### constructor
    def __init__(self, mx_per_pix=None, my_per_pix=None):

        # conversions from pixels to meters
        self.mx = mx_per_pix
        self.my = my_per_pix
        # most recent center points of each line
        self.best_fit = None
        # left and right lane objects
        self.left_lane = lane()
        self.right_lane = lane()
        # left and right lane pixels
        self.x_left = None
        self.y_left = None
        self.x_right = None
        self.y_right = None
        # middle curve
        self.middle_x = None
        # moving average for radius measurement
        self.recent_r = movingAverage(10)

    ### generate pixel to meter conversions based on a sample image
    ### no return
    def generate_unit_conversion(self, binary_img, lane_width_meters=3.7, lane_distance_meters=10):
        # grab lane centers
        lane_centers = find_center_points(binary_img)
        # calculate lane width in pixels
        pixel_width = lane_centers[1] - lane_centers[0]
        # calculate conversion factors
        self.mx = lane_width_meters / pixel_width
        self.my = lane_distance_meters / binary_img.shape[0]

    ### search for lane pixels using x-sliding window technique
    ### returns image of technical lane depictions
    def find_lanes(self, binary_image, n_windows, window_width, min_pix_per_window, curve_margin, smooth_factor, max_diff):

        binary_img = np.copy(binary_image)
        # grab lane centers
        lane_centers = find_center_points(binary_img)
        # use centers as starting point for windows
        left_window_base = lane_centers[0]
        right_window_base = lane_centers[1]
        # set window height based on image and number of windows
        window_height = np.int(binary_img.shape[0]//n_windows)
        # identify x and y coordinates of all activate pixels
        nonzero = binary_img.nonzero()
        nonzero_x = np.array(nonzero[1])
        nonzero_y = np.array(nonzero[0])
        # initialize lists of lane pixel coordinates
        left_lane_pix = []
        right_lane_pix = []
        # create image to draw on
        result_img = np.zeros_like(binary_img)
        result_img = np.dstack((result_img, result_img, result_img))
        # iterate through windows vertically
        for window_idx in range(n_windows):
            # calculate window y position
            y_low = binary_img.shape[0] - (window_idx + 1) * window_height
            y_high = binary_img.shape[0] - window_idx * window_height
            # keep windows within search margin of last polynomial fit
            left_window_base = self.left_lane.verify_window(left_window_base, y_high, curve_margin, smooth_factor)
            right_window_base = self.right_lane.verify_window(right_window_base, y_high, curve_margin, smooth_factor)
            # calculate window x positions
            x_left_line_low = int(left_window_base - window_width / 2)
            x_left_line_high = int(left_window_base + window_width / 2)
            x_right_line_low = int(right_window_base - window_width / 2)
            x_right_line_high = int(right_window_base + window_width / 2)
            # draw windows
            cv2.rectangle(result_img, (x_left_line_low, y_high), (x_left_line_high, y_low), (230,230,0), 3)
            cv2.rectangle(result_img, (x_right_line_low, y_high), (x_right_line_high, y_low), (230, 230, 0), 3)
            # record activated pixels within window
            left_window_pix = ((nonzero_y >= y_low) & (nonzero_y < y_high) & (nonzero_x >= x_left_line_low) & (
                    nonzero_x < x_left_line_high)).nonzero()[0]
            right_window_pix = ((nonzero_y >= y_low) & (nonzero_y < y_high) & (nonzero_x >= x_right_line_low) & (
                    nonzero_x < x_right_line_high)).nonzero()[0]
            left_lane_pix.append(left_window_pix)
            right_lane_pix.append(right_window_pix)
            # recenter window if min pixel threshold is met
            if len(left_window_pix) >= min_pix_per_window:
                left_window_base = np.int(np.mean(nonzero_x[left_window_pix]))
            if len(right_window_pix) >= min_pix_per_window:
                right_window_base = np.int(np.mean(nonzero_x[right_window_pix]))
        # combine list of lists
        left_lane_pix = np.concatenate(left_lane_pix)
        right_lane_pix = np.concatenate(right_lane_pix)
        # extract x and y values
        self.x_left = nonzero_x[left_lane_pix]
        self.y_left = nonzero_y[left_lane_pix]
        self.x_right = nonzero_x[right_lane_pix]
        self.y_right = nonzero_y[right_lane_pix]
        # highlight lane pixels
        result_img[self.y_left, self.x_left] = [200, 50, 50]
        result_img[self.y_right, self.x_right] = [50, 50, 255]
        # fit curves to each set of pixels
        result_img = self.left_lane.fit_curve(result_img, self.x_left, self.y_left, smooth_factor, max_diff)
        result_img = self.right_lane.fit_curve(result_img, self.x_right, self.y_right, smooth_factor, max_diff)
        # save middle x values
        self.middle_x = np.array(self.left_lane.fit_x) + (np.array(self.right_lane.fit_x) - np.array(self.left_lane.fit_x)) / 2
        self.middle_x = self.middle_x.astype(np.int32)
        # format image
        result_img[np.where((result_img == [0, 0, 0]).all(axis=2))] = [240, 240, 240]
        return result_img

    ### calculate radius of lane curvature for most recent fit
    ### return radius in meters
    def get_radius(self):
        coeff = np.average((self.left_lane.last_coeff, self.right_lane.last_coeff), axis=0)
        y_eval = np.max(self.left_lane.plot_y)
        r = ((1 + (2 * coeff[0] * y_eval * self.my + coeff[1]) ** 2) ** 1.5) / abs(
            2 * coeff[0])
        return np.average(self.recent_r.next(r))

    ### project lines back down onto original image
    ### returns unwarped image with lane indicators
    def project_lines(self, detected_img, original_img, transformer_obj):
        # Create an image to draw the lines on
        warp_zero = np.zeros_like(detected_img[:, :, 0]).astype(np.uint8)
        warped_lanes = np.dstack((warp_zero, warp_zero, warp_zero))
        left_pts = self.left_lane.line_pts
        right_x = self.right_lane.fit_x
        right_pts = np.array([np.flipud(np.transpose(np.vstack([right_x, self.right_lane.plot_y])))])
        final_pts = np.hstack((left_pts, right_pts))
        middle_pts = np.array([np.transpose(np.vstack([self.middle_x, self.left_lane.plot_y]))])
        # Draw the lane area onto the warped blank image
        cv2.fillPoly(warped_lanes, np.int_([final_pts]), (40, 200, 40))
        # Draw center line onto image
        cv2.polylines(warped_lanes, np.int_([middle_pts]), False, (0,255,0), thickness=5)
        # Highlight the detected lane pixels
        warped_lanes[self.y_left, self.x_left] = [255, 0, 0]
        warped_lanes[self.y_right, self.x_right] = [0, 0, 255]
        # Warp the blank back to original image space
        unwarped = transformer_obj.unwarp(warped_lanes)
        # Combine the result with the original image
        result = cv2.addWeighted(original_img, 1, unwarped, 0.5, 0)
        return result

    ### calculate distance of vehicle from center
    ### returns distance in meters
    def get_center_distance(self, warped):
        veh_center = warped.shape[1] / 2
        pixels = self.middle_x[0] - veh_center
        meters = pixels * self.mx
        return meters

class lane():

    ### constructor
    def __init__(self):

        # store several frames of pixels found in cross-sliding windows
        self.historical_x = []
        self.historical_y = []
        # store last polynomial coefficients
        self.last_coeff = None
        # line space for plotting
        self.plot_y = None
        # curve values
        self.line_pts = None
        self.fit_x = None
        # recent radius measurements
        self.R = []
        # moving average period
        self.period = None

    ### fit polynomial to lane pixels
    ### return image with curve drawn
    def fit_curve(self, img, X, Y, period, max_diff):
        # store period
        self.period = period
        # store lane pixels
        self.historical_x.insert(0, (X))
        self.historical_y.insert(0, (Y))
        # update moving average of pixels
        if len(self.historical_x) > period:
            del(self.historical_x[-1])
            del(self.historical_y[-1])
        # concatenate all the pixels over the last n frames
        all_x = np.copy(self.historical_x)
        all_y = np.copy(self.historical_y)
        all_x = np.concatenate(all_x)
        all_y = np.concatenate(all_y)
        # generate line space for plotting
        self.plot_y = np.linspace(0, img.shape[0] - 1, img.shape[0])
        # fit curve
        new_coeff = np.polyfit(all_y, all_x, 2)
        # only accept coefficients within percent of previous
        #if len(self.historical_x) < period:
        self.last_coeff = new_coeff
        # else:
        #     diff = np.sum(np.abs(np.array(self.last_coeff) - np.array(new_coeff)) / abs(self.last_coeff)) / 3
        #     if diff <= max_diff / 100:
        #         self.last_coeff = new_coeff

        # draw curve
        self.fit_x = self.last_coeff[0] * self.plot_y**2 + self.last_coeff[1] * self.plot_y + self.last_coeff[2]
        self.line_pts = np.array([np.transpose(np.vstack([self.fit_x, self.plot_y]))])
        cv2.polylines(img, np.int_([self.line_pts]), False, (0, 255, 0), thickness=4)
        return img

    ### make sure x-sliding window stays near last fit
    ### returns x coordinate of window base
    def verify_window(self, window_x, window_y, max_margin, period):
        if len(self.historical_x) == period:
            expected_x = self.last_coeff[0] * window_y**2 + self.last_coeff[1] * window_y + self.last_coeff[2]
            if abs(expected_x - window_x) > max_margin:
                return int(expected_x)
            else:
                return window_x
        else:
            return window_x


#산포도차트

import pandas as pd
import matplotlib.pyplot as plt

marathon_2015_2017 = pd.read_csv("D:\creapple\python_matplotlib\data/marathon_2015_2017.csv")

# Select male and female runners by conditional expression
MALE_runner = marathon_2015_2017[marathon_2015_2017['M/F'] == 'M']
FEMALE_runner = marathon_2015_2017[marathon_2015_2017['M/F'] == 'F']

plt.figure(figsize=(20,20))

x_male = MALE_runner.Age
y_male = MALE_runner['Official Time']

x_female = FEMALE_runner.Age
y_female = FEMALE_runner['Official Time']

plt.plot(x_male, y_male, '.', color='b', alpha=0.5)
plt.plot(x_female, y_female, '.', color='r', alpha=0.5)
# Generate labels and title
plt.xlabel("Age", fontsize=16)
plt.ylabel("Official Time (Second)",fontsize=16)
plt.title("Distribution by Running Time and Age",fontsize=20)
# Show plot
plt.show()


# Copyright 2017 Palantir Technologies, Inc.
# License: MIT

import logging.config
import argparse
import sys
import os

log = logging.getLogger(__name__)

LOG_FORMAT = "%(asctime)s UTC - %(levelname)s - %(name)s\n%(message)s\n\n"

_critical_error_log_file = os.path.join(
    os.path.expanduser("~"), "robotframework_ls_critical.log"
)

def _critical_msg(msg):
    with open(_critical_error_log_file, "a+") as stream:
        stream.write(msg + "\n")

def add_arguments(parser):
    parser.description = "Python Language Server"

    parser.add_argument(
        "--tcp", action="store_true", help="Use TCP server instead of stdio"
    )
    parser.add_argument("--host", default="127.0.0.1", help="Bind to this address")
    parser.add_argument("--port", type=int, default=1456, help="Bind to this port")
    parser.add_argument(
        "--check-parent-process",
        action="store_true",
        help="Check whether parent process is still alive using os.kill(ppid, 0) "
        "and auto shut down language server process when parent process is not alive."
        "Note that this may not work on a Windows machine.",
    )

    log_group = parser.add_mutually_exclusive_group()
    log_group.add_argument(
        "--log-config", help="Path to a JSON file containing Python logging config."
    )
    log_group.add_argument(
        "--log-file",
        help="Redirect logs to the given file instead of writing to stderr."
        "Has no effect if used with --log-config.",
    )

    parser.add_argument(
        "-v",
        "--verbose",
        action="count",
        default=0,
        help="Increase verbosity of log output, overrides log config file",
    )

def main(args=None, after_bind=lambda server: None):
    try:
        import robotframework_ls
    except ImportError:
        # Automatically add it to the path if __main__ is being executed.
        sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
        import robotframework_ls  # @UnusedImport

    from robotframework_ls.python_ls import (
        start_io_lang_server,
        start_tcp_lang_server,
    )
    from robotframework_ls.ext.robotframework_ls_impl import (
        RobotFrameworkLanguageServer,
    )

    parser = argparse.ArgumentParser()
    add_arguments(parser)

    args = parser.parse_args(args=args if args is not None else sys.argv[1:])
    _configure_logger(args.verbose, args.log_config, args.log_file)

    if args.tcp:
        start_tcp_lang_server(
            args.host, args.port, RobotFrameworkLanguageServer, after_bind=after_bind,
        )
    else:
        stdin, stdout = _binary_stdio()
        start_io_lang_server(stdin, stdout, RobotFrameworkLanguageServer)

class RedirectedStreamErrorOnAccess(object):
    def __getattr__(self, mname):
        raise AssertionError("This stream is now redirected and should not be used.")

def _binary_stdio():
    """Construct binary stdio streams (not text mode).

    This seems to be different for Window/Unix Python2/3, so going by:
        https://stackoverflow.com/questions/2850893/reading-binary-data-from-stdin
    """
    PY3K = sys.version_info >= (3, 0)

    if PY3K:
        stdin, stdout = sys.stdin.buffer, sys.stdout.buffer
    else:
        # Python 2 on Windows opens sys.stdin in text mode, and
        # binary data that read from it becomes corrupted on \r\n
        if sys.platform == "win32":
            # set sys.stdin to binary mode
            import msvcrt

            msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
            msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
        stdin, stdout = sys.stdin, sys.stdout
    sys.stdin, sys.stdout = (
        RedirectedStreamErrorOnAccess(),
        RedirectedStreamErrorOnAccess(),
    )

    return stdin, stdout

def _configure_logger(verbose=0, log_config=None, log_file=None):
    if getattr(_configure_logger, "called", False):
        return  # i.e.: in dev mode we may call multiple times
    _configure_logger.called = True

    root_logger = logging.root

    if log_config:
        import json

        with open(log_config, "r") as f:
            logging.config.dictConfig(json.load(f))
    else:
        formatter = logging.Formatter(LOG_FORMAT)
        if log_file:
            log_file = os.path.expanduser(log_file)
            log_handler = logging.handlers.RotatingFileHandler(
                log_file,
                mode="a",
                maxBytes=50 * 1024 * 1024,
                backupCount=10,
                encoding=None,
                delay=0,
            )
        else:
            log_handler = logging.StreamHandler()
        log_handler.setFormatter(formatter)
        root_logger.addHandler(log_handler)

    if verbose == 0:
        level = logging.CRITICAL
    elif verbose == 1:
        level = logging.WARNING
    elif verbose >= 2:
        level = logging.DEBUG

    root_logger.setLevel(level)

if __name__ == "__main__":
    try:
        log.info("Initializing Language Server. Args: %s", (sys.argv[1:],))

        main()
    except:
        # Critical error (the logging may not be set up properly).
        import traceback

        # Print to file and stderr.
        with open(_critical_error_log_file, "a+") as stream:
            traceback.print_exc(file=stream)

        traceback.print_exc()


from datetime import datetime

from django import forms
from django.utils.translation import ugettext as _
from django.contrib.auth.models import User

from haystack.forms import SearchForm as HaystackSearchForm
from haystack.inputs import AutoQuery

from pybb.contrib.search.fields import TreeModelMultipleChoiceField
from pybb.models import Forum, Topic

class SearchForm(HaystackSearchForm):
    user = forms.ModelChoiceField(queryset=User.objects.all(),
                                  required=False,
                                  label=_('Username'))

    replies = forms.IntegerField(label=_('Number of answers'),
                                 min_value=0, required=False)

    search_topic_name = forms.BooleanField(required=False)
    with_attachment = forms.BooleanField(required=False)

    start_date = forms.DateTimeField(required=False)
    end_date = forms.DateTimeField(required=False)

    topic = forms.ModelChoiceField(queryset=Topic.objects.all(),
                                   required=False,
                                   label=_('Topic'))

    def __init__(self, *args, **kwargs):
        super(SearchForm, self).__init__(*args, **kwargs)

        self.fields['forums'] = TreeModelMultipleChoiceField(queryset=Forum.objects.all(),
                                                             join_field='forum_id',
                                                             required=False)

    def clean(self):
        data = super(HaystackSearchForm, self).clean()
        if data.get('start_date', None):
            if not data.get('end_date', None):
                data['end_date'] = datetime.now()

            if data['start_date'] > data['end_date']:
                raise forms.ValidationError('Start date is after end date')

        return data

    def search(self):
        if not self.is_valid():
            return self.no_query_found()

        # topic name search
        if self.cleaned_data.get('search_topic_name', False):
            sqs = self.searchqueryset.filter(
                topic_name=AutoQuery(self.cleaned_data['q']))
            sqs = sqs.filter(is_first_post=True)
        else:
            sqs = super(SearchForm, self).search()

        sqs = sqs.order_by('-created')

        forums = self.cleaned_data.get('forums', None)
        if forums:
            sqs = sqs.filter(topic_breadcrumbs__in=[f.id for f in forums])
        if self.cleaned_data.get('user', None):
            sqs = sqs.filter(user_id=self.cleaned_data['user'].pk)
        if self.cleaned_data.get('replies', None):
            sqs = sqs.filter(replies__gte=self.cleaned_data['replies'])
        if self.cleaned_data.get('topic', None):
            sqs = sqs.filter(topic_id=self.cleaned_data['topic'].pk)
        if self.cleaned_data.get('start_date', None):
            sqs = sqs.filter(updated__gte=self.cleaned_data['start_date'])
        if self.cleaned_data.get('end_date', None):
            sqs = sqs.filter(updated__lte=self.cleaned_data['end_date'])
        if self.cleaned_data.get('with_attachment', None):
            sqs = sqs.filter(has_attachment=self.cleaned_data['with_attachment'])

        return sqs


# http://aperiodic.net/phil/scala/s-99/

def flatten_nested_list_pb7(lst: list,
                            result: list) -> list:
    for element in lst:
        if isinstance(element, list):
            flatten_nested_list_pb7(lst=element, result=result)
        else:
            result.append(element)

    return result

def flatten_nested_lst_remove_duplicates_pb7(lst: list,
                                             result: list,
                                             seen_elements: set) -> list:
    for element in lst:
        if isinstance(element, list):
            flatten_nested_lst_remove_duplicates_pb7(lst=element, result=result, seen_elements=seen_elements)
        else:
            if element not in seen_elements:
                result.append(element)
                seen_elements.add(element)

    return result


def meeting_planner(slotsA, slotsB, dur):
  ptA = 0
  ptB = 0
  inter = intersection(slotsA, ptA, slotsB, ptB)
  while inter[1] - inter[0] < dur:
    if slotsA[ptA][1] < slotsB[ptB][1]:
      ptA += 1
      if ptA == len(slotsA):
        return []
    else:
      ptB += 1
      if ptB == len(slotsB):
        return []
    inter = intersection(slotsA, ptA, slotsB, ptB)
  if ptA == len(slotsA) or ptB == len(slotsB) or inter[1] <= inter[0]:
    return []
  else:
    return [inter[0], inter[0]+dur]

def intersection(slotsA, ptA, slotsB, ptB):
  if ptA == len(slotsA) or ptB == len(slotsB):
    return [0, 0]
  else:
    lower = max(slotsA[ptA][0], slotsB[ptB][0])
    upper = min(slotsA[ptA][1], slotsB[ptB][1])
    return [lower, upper]

slotsA = [[10, 50], [60, 120], [140, 210]]
slotsB = [[0, 15], [60, 70]]
dur = 8

slotsA = [[10, 50], [60, 120], [140, 210]]
slotsB = [[0, 15], [60, 70]]
dur = 12
print(meeting_planner(slotsA, slotsB, dur))


PERMISSION_DENIED_MESSAGE: str = "***PERMISSION DENIED!*** \n" \
                            "You are not permitted to use this command.  \n" \
                            "Please contact to your server master. \n."
ERROR_OCCURRED_MESSAGE: str = "***ERROR OCCURRED!*** \n" \
                              "Error has occurred while executing gcp request command. \n" \
                              "Please contact to your server master or the software developer. \n" \
                              "Error: {} \n"
OPERATION_COMPLETED_MESSAGE: str = "***Operation Completed! ***\n" \
                                   "Operation: {} has successfully completed. \n" \
                                   "This may take more 2~3 minutes that the Minecraft Server starts (stops)."
INSTANCE_IS_ALREADY_IN_REQUESTED_STATUS: str = "***Already in status of {}.*** \n" \
                                               "The instance is already in the status. \n" \
                                               "No operation has done."

PRE_STOP_OPERATION_PROCESSING: str = "Processing pre-stop operation... \n" \
                                  "Trying to shutdown Minecraft server from the console channel. \n" \
                                  "Whichever the operation is completed or not, " \
                                  "the server will shutdown in 5 minutes forcibly."

REQUEST_RECEIVED: str = "Operation: {} has requested. \n" \
                        "Please wait until the operation is done. \n"

START_REQUEST_RECEIVED_MESSAGE = "Trying to start the gcp server. \n" \
                                 "It takes 3 sec at least to complete the operation. \n" \
                                 "The minecraft server will start as soon as gcp server started. \n" \
                                 "PLEASE WAIT UNTIL YOU RECEIVE MESSAGE 'SERVER HAS STARTED!' " \
                                 "BEFORE YOU JOIN THE MINECRAFT SERVER."

STOP_REQUEST_RECEIVED_MESSAGE = "Trying to stop the gcp server. \n" \
                                 "It takes 5 minutes at least to complete the operation. \n" \
                                 "We will issue `stop` command in console channel. \n" \
                                 "And then, we will wait for 5 minutes for the Minecraft server stops." \
                                 "After all the process is done, we will shutdown GCP instance finally."


from odoo import api, models, fields, _
from odoo.exceptions import UserError, Warning, ValidationError

class Mass_mailing_list(models.Model):
    _inherit = 'mailing.list'

    # Export List(Audience) and its Members in Mailchimp
    def export_list_mailchimp(self):
        user = self.env.user
        partner = self.env.user.partner_id
        dict = {"name": partner.name,
                "company": user.company_id.name,
                "address1": partner.street,
                "address2": partner.street2,
                "city": partner.city,
                "state": partner.state_id.name,
                "zip": partner.zip,
                "country": partner.country_id.name,
                "phone": partner.phone,
                "from_name": partner.name,
                "email": partner.email,
                }
        try:
            list_id = self.env['mailchimp'].create_list(dict)
            return list_id
        except Exception as e:
            raise ValidationError(_("These are the Issues :%s") % e)

    # Update Members in List(Audience)
    def update_list_mailchimp(self, list_id, ids):
        mc_email_lst = []
        odoo_email_lst = []
        mc_list_id = False
        mailchimp = self.env['mailchimp'].search([('name', '=', 'Mailchimp')], limit=1)
        mailchimp.get_all_lists()
        for lines in mailchimp.list_lines:
            if lines.mailing_lists.ids == ids:
                mc_list_id = lines.mailchimp_list_id
        mc_list_record = self.env['mailchimp.lists'].search([('mailchimp_list_id', '=', mc_list_id)])
        for memb_line in mc_list_record.members_line:
            mc_email_lst.append(memb_line.hash)
        for id in ids:
            mail_contacts = self.env['mail.mass_mailing.list_contact_rel'].search([('list_id', '=', id)])
            for contact in mail_contacts:
                user = self.env['mail.mass_mailing.contact'].browse(contact.contact_id.id)
                odoo_email_lst.append(user.email)
                if user.email not in mc_email_lst:
                    new_member = mailchimp.createlist_members(list_id, user.name, user.email)
        for email in mc_email_lst:
            if email not in odoo_email_lst:
                del_memb = mailchimp.remove_member(list_id, email)

class Mass_mailing(models.Model):
    _inherit = 'mailing.mailing'

    exported = fields.Boolean('Exported')
    subject = fields.Char('Subject')
    use_mc_template = fields.Boolean('Use Mailchimp Templates')
    template_id = fields.Many2one('mailchimp.template', 'Template')

    @api.onchange('use_mc_template')
    def onchange_template_id(self):
        if self.use_mc_template:
            mailchimp = self.env['mailchimp'].search([('name', '=', 'Mailchimp')], limit=1)
            mailchimp.get_all_templates()

    # To create a Campaign in mailchimp with its template
    def send_to_mailchimp(self):

        data = {"html": self.body_html,
                "plain_text": "Template For Campaign :- %s" % self.name}
        list_audience = False
        error = []
        temp = 0
        mailchimp = self.env['mailchimp']
        try:
            name = self.env.user.name
            email = self.env.user.partner_id.email
            mc = self.env['mailchimp'].sudo().search([('name', '=', 'Mailchimp')], limit=1)
            mc.get_all_lists()
            for lines in mc.list_lines:
                if lines.mailing_lists.ids == self.contact_list_ids.ids:
                    list_audience = lines.mailchimp_list_id
                    lines.get_members()
                    temp = 1
            if not list_audience:
                list = self.env['mailing.list'].export_list_mailchimp()
                list_audience = list["id"]
                for id in self.contact_list_ids.ids:
                    mail_contacts = self.env['mail.mass_mailing.list_contact_rel'].search([('list_id', '=', id)])
                    if mail_contacts:
                        for contact in mail_contacts:
                            try:
                                user = self.env['mail.mass_mailing.contact'].browse(contact.contact_id.id)
                                member = mailchimp.createlist_members(list["id"], user.name, user.email)
                            except Exception as exc:
                                error.append(exc.args[0])
            if temp == 1:
                mail_list = self.env['mailing.list'].update_list_mailchimp(list_audience,
                                                                                self.contact_list_ids.ids)
            mc_template_id = False
            if self.use_mc_template:
                mc_template_id = self.template_id.mc_template_id
            campaign = mailchimp.create_campaign(self, data, name, email, list_audience, mc_template_id)
            if campaign:
                self.sudo().write({'exported': True})
                if temp == 0:
                    history = self.env['mailchimp.lists'].sudo().create(
                        {'mailing_lists': [(6, 0, self.contact_list_ids.ids)],
                         'mailchimp_list_id': list_audience,
                         'list_lines_id': mc.id})
                    history.get_members()
        except Exception as e:
            raise ValidationError(_("Error Occured with these contats : %s") % e.args[0])


import math
import microbit

def hypot(x, y):
    return math.sqrt(math.pow(x, 2) + math.pow(y, 2))

def animate_sin(operation):
    brightness_max = 9
    half_brightness_max = brightness_max / 2
    two_pi = math.pi * 2

    origo_x, origo_y = 2, 2

    max_distance = hypot(origo_x, origo_y)
    double_max_distance = max_distance * 2
    offset = 0
    last_t = microbit.running_time()

    while True:
        for x in range(0, 5):
            dist_x = x - origo_x
            for y in range(0, 5):
                dist_y = y - origo_y

                distance = (math.fabs(hypot(dist_x, dist_y)) /
                            double_max_distance)
                distance = (distance + (offset / operation)) % 1

                sin = math.sin(distance * two_pi - math.pi)

                value = round(sin * half_brightness_max + half_brightness_max)

                microbit.display.set_pixel(x, y, value)

        t_now = microbit.running_time()
        delta_t = t_now - last_t
        offset += delta_t * 0.001
        last_t = t_now
        microbit.sleep(10)

animate_sin(-1)


"""Support for HomematicIP Cloud devices."""
import logging
from pathlib import Path
from typing import Optional

from homematicip.aio.device import AsyncSwitchMeasuring
from homematicip.aio.group import AsyncHeatingGroup
from homematicip.aio.home import AsyncHome
from homematicip.base.helpers import handle_config
import voluptuous as vol

from homeassistant import config_entries
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_ENTITY_ID, CONF_NAME
from homeassistant.helpers import device_registry as dr
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.config_validation import comp_entity_ids
from homeassistant.helpers.typing import ConfigType, HomeAssistantType

from .config_flow import configured_haps
from .const import (
    CONF_ACCESSPOINT,
    CONF_AUTHTOKEN,
    DOMAIN,
    HMIPC_AUTHTOKEN,
    HMIPC_HAPID,
    HMIPC_NAME,
)
from .device import HomematicipGenericDevice  # noqa: F401
from .hap import HomematicipAuth, HomematicipHAP  # noqa: F401

_LOGGER = logging.getLogger(__name__)

ATTR_ACCESSPOINT_ID = "accesspoint_id"
ATTR_ANONYMIZE = "anonymize"
ATTR_CLIMATE_PROFILE_INDEX = "climate_profile_index"
ATTR_CONFIG_OUTPUT_FILE_PREFIX = "config_output_file_prefix"
ATTR_CONFIG_OUTPUT_PATH = "config_output_path"
ATTR_DURATION = "duration"
ATTR_ENDTIME = "endtime"
ATTR_TEMPERATURE = "temperature"

DEFAULT_CONFIG_FILE_PREFIX = "hmip-config"

SERVICE_ACTIVATE_ECO_MODE_WITH_DURATION = "activate_eco_mode_with_duration"
SERVICE_ACTIVATE_ECO_MODE_WITH_PERIOD = "activate_eco_mode_with_period"
SERVICE_ACTIVATE_VACATION = "activate_vacation"
SERVICE_DEACTIVATE_ECO_MODE = "deactivate_eco_mode"
SERVICE_DEACTIVATE_VACATION = "deactivate_vacation"
SERVICE_DUMP_HAP_CONFIG = "dump_hap_config"
SERVICE_RESET_ENERGY_COUNTER = "reset_energy_counter"
SERVICE_SET_ACTIVE_CLIMATE_PROFILE = "set_active_climate_profile"

CONFIG_SCHEMA = vol.Schema(
    {
        vol.Optional(DOMAIN, default=[]): vol.All(
            cv.ensure_list,
            [
                vol.Schema(
                    {
                        vol.Optional(CONF_NAME, default=""): vol.Any(cv.string),
                        vol.Required(CONF_ACCESSPOINT): cv.string,
                        vol.Required(CONF_AUTHTOKEN): cv.string,
                    }
                )
            ],
        )
    },
    extra=vol.ALLOW_EXTRA,
)

SCHEMA_ACTIVATE_ECO_MODE_WITH_DURATION = vol.Schema(
    {
        vol.Required(ATTR_DURATION): cv.positive_int,
        vol.Optional(ATTR_ACCESSPOINT_ID): vol.All(str, vol.Length(min=24, max=24)),
    }
)

SCHEMA_ACTIVATE_ECO_MODE_WITH_PERIOD = vol.Schema(
    {
        vol.Required(ATTR_ENDTIME): cv.datetime,
        vol.Optional(ATTR_ACCESSPOINT_ID): vol.All(str, vol.Length(min=24, max=24)),
    }
)

SCHEMA_ACTIVATE_VACATION = vol.Schema(
    {
        vol.Required(ATTR_ENDTIME): cv.datetime,
        vol.Required(ATTR_TEMPERATURE, default=18.0): vol.All(
            vol.Coerce(float), vol.Range(min=0, max=55)
        ),
        vol.Optional(ATTR_ACCESSPOINT_ID): vol.All(str, vol.Length(min=24, max=24)),
    }
)

SCHEMA_DEACTIVATE_ECO_MODE = vol.Schema(
    {vol.Optional(ATTR_ACCESSPOINT_ID): vol.All(str, vol.Length(min=24, max=24))}
)

SCHEMA_DEACTIVATE_VACATION = vol.Schema(
    {vol.Optional(ATTR_ACCESSPOINT_ID): vol.All(str, vol.Length(min=24, max=24))}
)

SCHEMA_SET_ACTIVE_CLIMATE_PROFILE = vol.Schema(
    {
        vol.Required(ATTR_ENTITY_ID): comp_entity_ids,
        vol.Required(ATTR_CLIMATE_PROFILE_INDEX): cv.positive_int,
    }
)

SCHEMA_DUMP_HAP_CONFIG = vol.Schema(
    {
        vol.Optional(ATTR_CONFIG_OUTPUT_PATH): cv.string,
        vol.Optional(
            ATTR_CONFIG_OUTPUT_FILE_PREFIX, default=DEFAULT_CONFIG_FILE_PREFIX
        ): cv.string,
        vol.Optional(ATTR_ANONYMIZE, default=True): cv.boolean,
    }
)

SCHEMA_RESET_ENERGY_COUNTER = vol.Schema(
    {vol.Required(ATTR_ENTITY_ID): comp_entity_ids}
)

async def async_setup(hass: HomeAssistantType, config: ConfigType) -> bool:
    """Set up the HomematicIP Cloud component."""
    hass.data[DOMAIN] = {}

    accesspoints = config.get(DOMAIN, [])

    for conf in accesspoints:
        if conf[CONF_ACCESSPOINT] not in configured_haps(hass):
            hass.async_add_job(
                hass.config_entries.flow.async_init(
                    DOMAIN,
                    context={"source": config_entries.SOURCE_IMPORT},
                    data={
                        HMIPC_HAPID: conf[CONF_ACCESSPOINT],
                        HMIPC_AUTHTOKEN: conf[CONF_AUTHTOKEN],
                        HMIPC_NAME: conf[CONF_NAME],
                    },
                )
            )

    async def _async_activate_eco_mode_with_duration(service) -> None:
        """Service to activate eco mode with duration."""
        duration = service.data[ATTR_DURATION]
        hapid = service.data.get(ATTR_ACCESSPOINT_ID)

        if hapid:
            home = _get_home(hapid)
            if home:
                await home.activate_absence_with_duration(duration)
        else:
            for hap in hass.data[DOMAIN].values():
                await hap.home.activate_absence_with_duration(duration)

    hass.services.async_register(
        DOMAIN,
        SERVICE_ACTIVATE_ECO_MODE_WITH_DURATION,
        _async_activate_eco_mode_with_duration,
        schema=SCHEMA_ACTIVATE_ECO_MODE_WITH_DURATION,
    )

    async def _async_activate_eco_mode_with_period(service) -> None:
        """Service to activate eco mode with period."""
        endtime = service.data[ATTR_ENDTIME]
        hapid = service.data.get(ATTR_ACCESSPOINT_ID)

        if hapid:
            home = _get_home(hapid)
            if home:
                await home.activate_absence_with_period(endtime)
        else:
            for hap in hass.data[DOMAIN].values():
                await hap.home.activate_absence_with_period(endtime)

    hass.services.async_register(
        DOMAIN,
        SERVICE_ACTIVATE_ECO_MODE_WITH_PERIOD,
        _async_activate_eco_mode_with_period,
        schema=SCHEMA_ACTIVATE_ECO_MODE_WITH_PERIOD,
    )

    async def _async_activate_vacation(service) -> None:
        """Service to activate vacation."""
        endtime = service.data[ATTR_ENDTIME]
        temperature = service.data[ATTR_TEMPERATURE]
        hapid = service.data.get(ATTR_ACCESSPOINT_ID)

        if hapid:
            home = _get_home(hapid)
            if home:
                await home.activate_vacation(endtime, temperature)
        else:
            for hap in hass.data[DOMAIN].values():
                await hap.home.activate_vacation(endtime, temperature)

    hass.services.async_register(
        DOMAIN,
        SERVICE_ACTIVATE_VACATION,
        _async_activate_vacation,
        schema=SCHEMA_ACTIVATE_VACATION,
    )

    async def _async_deactivate_eco_mode(service) -> None:
        """Service to deactivate eco mode."""
        hapid = service.data.get(ATTR_ACCESSPOINT_ID)

        if hapid:
            home = _get_home(hapid)
            if home:
                await home.deactivate_absence()
        else:
            for hap in hass.data[DOMAIN].values():
                await hap.home.deactivate_absence()

    hass.services.async_register(
        DOMAIN,
        SERVICE_DEACTIVATE_ECO_MODE,
        _async_deactivate_eco_mode,
        schema=SCHEMA_DEACTIVATE_ECO_MODE,
    )

    async def _async_deactivate_vacation(service) -> None:
        """Service to deactivate vacation."""
        hapid = service.data.get(ATTR_ACCESSPOINT_ID)

        if hapid:
            home = _get_home(hapid)
            if home:
                await home.deactivate_vacation()
        else:
            for hap in hass.data[DOMAIN].values():
                await hap.home.deactivate_vacation()

    hass.services.async_register(
        DOMAIN,
        SERVICE_DEACTIVATE_VACATION,
        _async_deactivate_vacation,
        schema=SCHEMA_DEACTIVATE_VACATION,
    )

    async def _set_active_climate_profile(service) -> None:
        """Service to set the active climate profile."""
        entity_id_list = service.data[ATTR_ENTITY_ID]
        climate_profile_index = service.data[ATTR_CLIMATE_PROFILE_INDEX] - 1

        for hap in hass.data[DOMAIN].values():
            if entity_id_list != "all":
                for entity_id in entity_id_list:
                    group = hap.hmip_device_by_entity_id.get(entity_id)
                    if group and isinstance(group, AsyncHeatingGroup):
                        await group.set_active_profile(climate_profile_index)
            else:
                for group in hap.home.groups:
                    if isinstance(group, AsyncHeatingGroup):
                        await group.set_active_profile(climate_profile_index)

    hass.services.async_register(
        DOMAIN,
        SERVICE_SET_ACTIVE_CLIMATE_PROFILE,
        _set_active_climate_profile,
        schema=SCHEMA_SET_ACTIVE_CLIMATE_PROFILE,
    )

    async def _async_dump_hap_config(service) -> None:
        """Service to dump the configuration of a Homematic IP Access Point."""
        config_path = (
            service.data.get(ATTR_CONFIG_OUTPUT_PATH) or hass.config.config_dir
        )
        config_file_prefix = service.data[ATTR_CONFIG_OUTPUT_FILE_PREFIX]
        anonymize = service.data[ATTR_ANONYMIZE]

        for hap in hass.data[DOMAIN].values():
            hap_sgtin = hap.config_entry.title

            if anonymize:
                hap_sgtin = hap_sgtin[-4:]

            file_name = f"{config_file_prefix}_{hap_sgtin}.json"
            path = Path(config_path)
            config_file = path / file_name

            json_state = await hap.home.download_configuration()
            json_state = handle_config(json_state, anonymize)

            config_file.write_text(json_state, encoding="utf8")

    hass.services.async_register(
        DOMAIN,
        SERVICE_DUMP_HAP_CONFIG,
        _async_dump_hap_config,
        schema=SCHEMA_DUMP_HAP_CONFIG,
    )

    async def _async_reset_energy_counter(service):
        """Service to reset the energy counter."""
        entity_id_list = service.data[ATTR_ENTITY_ID]

        for hap in hass.data[DOMAIN].values():
            if entity_id_list != "all":
                for entity_id in entity_id_list:
                    device = hap.hmip_device_by_entity_id.get(entity_id)
                    if device and isinstance(device, AsyncSwitchMeasuring):
                        await device.reset_energy_counter()
            else:
                for device in hap.home.devices:
                    if isinstance(device, AsyncSwitchMeasuring):
                        await device.reset_energy_counter()

    hass.helpers.service.async_register_admin_service(
        DOMAIN,
        SERVICE_RESET_ENERGY_COUNTER,
        _async_reset_energy_counter,
        schema=SCHEMA_RESET_ENERGY_COUNTER,
    )

    def _get_home(hapid: str) -> Optional[AsyncHome]:
        """Return a HmIP home."""
        hap = hass.data[DOMAIN].get(hapid)
        if hap:
            return hap.home

        _LOGGER.info("No matching access point found for access point id %s", hapid)
        return None

    return True

async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool:
    """Set up an access point from a config entry."""
    hap = HomematicipHAP(hass, entry)
    hapid = entry.data[HMIPC_HAPID].replace("-", "").upper()
    hass.data[DOMAIN][hapid] = hap

    if not await hap.async_setup():
        return False

    # Register hap as device in registry.
    device_registry = await dr.async_get_registry(hass)
    home = hap.home
    # Add the HAP name from configuration if set.
    hapname = home.label if not home.name else f"{home.name} {home.label}"
    device_registry.async_get_or_create(
        config_entry_id=home.id,
        identifiers={(DOMAIN, home.id)},
        manufacturer="eQ-3",
        name=hapname,
        model=home.modelType,
        sw_version=home.currentAPVersion,
    )
    return True

async def async_unload_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool:
    """Unload a config entry."""
    hap = hass.data[DOMAIN].pop(entry.data[HMIPC_HAPID])
    return await hap.async_reset()


import sys
import io
import json
import ijson
import sourmash
from sourmash.signature import SourmashSignature
from sourmash.signature_json import (_json_next_atomic_array,
                                         _json_next_signature,
                                         load_signature_json,
                                         load_signatures_json,
                                         load_signatureset_json_iter,
                                         save_signatures_json)
from collections import OrderedDict

def test__json_next_atomic_array():
    t = (2,3,4,5,6)
    s = json.dumps(t)
    if sys.version_info[0] < 3:
        s = unicode(s)
    it = ijson.parse(io.BytesIO(s.encode('utf-8')))
    a = _json_next_atomic_array(it)
    assert len(t) == len(a)
    assert all(x == y for x,y in zip(t, a))

# integration test more than a unit test...
def test__json_next_signature():

    name = 'Foo Bar'
    filename = '/tmp/foobar'

    minhash = (2,3,4,5,6)
    t = OrderedDict((('ksize', 21),
                     ('num', len(minhash)),
                     #('md5sum', ),
                     ('cardinality', 123456),
                     ('mins', minhash)))
    s = json.dumps(t)
    if sys.version_info[0] < 3:
        s = unicode(s)
    it = ijson.parse(io.BytesIO(s.encode('utf-8')))
    # no MD5SUM
    sig = _json_next_signature(it, name, filename,
                               ignore_md5sum=True,
                               ijson=ijson)

    ## check MD5SUM
    minhash = (5,)
    t = OrderedDict((('ksize', 20),
                     ('num', len(minhash)),
                     ('md5sum', 'eae27d77ca20db309e056e3d2dcd7d69'),
                     ('cardinality', 123456),
                     ('mins', minhash)))
    s = json.dumps(t)
    if sys.version_info[0] < 3:
        s = unicode(s)
    it = ijson.parse(io.BytesIO(s.encode('utf-8')))
    sig = _json_next_signature(it, name, filename,
                               ignore_md5sum=False,
                               ijson=ijson)

# integration test more than a unit test
def test_load_signature_json():
    name = 'Foo Bar'
    filename = '/tmp/foobar'

    minhash = (2,3,4,5,6)
    t = OrderedDict((('name', name),
                     ('filename', filename),
                     ('signatures',
                      (
                          OrderedDict((('ksize', 21),
                                       ('num', len(minhash)),
                                       #('md5sum', ),
                                       ('cardinality', 123456),
                                       ('mins', minhash))),
                      ))))
    s = json.dumps(t)
    if sys.version_info[0] < 3:
        s = unicode(s)
    it = ijson.parse(io.BytesIO(s.encode('utf-8')))
    # no MD5SUM
    sig_entry = load_signature_json(it, ignore_md5sum=True)

# integration test more than a unit test
def test_load_signaturesset_json_iter():

    t = list()
    for name, filename in (('Foo', '/tmp/foo'),
                           ('Bar', '/tmp/bar')):
        minhash = (2,3,4,5,6)
        t.append(OrderedDict((
            ('class', 'sourmash_signature'),
            ('name', name),
            ('filename', filename),
            ('signatures',
             (
                 OrderedDict((('ksize', 21),
                              ('num', len(minhash)),
                              #('md5sum', ),
                              ('cardinality', 123456),
                              ('mins', minhash))),
             )))))

    s = json.dumps(t)
    if sys.version_info[0] < 3:
        s = unicode(s)
    # no MD5SUM
    sig_entries = tuple(load_signatureset_json_iter(io.BytesIO(s.encode('utf-8')),
                                                    ignore_md5sum=True,
                                                    ijson=ijson))
    assert len(sig_entries) == 2

# integration test more than a unit test
def test_load_signaturesset_json_iter_molecules():

    t = list()
    molecules = 'DNA', 'protein', 'dayhoff', 'hp'
    names = "Foo", 'Bar', "Biz", "Baz"
    filenames = '/tmp/foo', '/tmp/bar', '/tmp/biz', '/tmp/baz'

    for molecule, name, filename in zip(molecules, names, filenames):
        minhash = (2,3,4,5,6)
        t.append(OrderedDict((
            ('class', 'sourmash_signature'),
            ('name', name),
            ('filename', filename),
            ('signatures',
             (
                 OrderedDict((('ksize', 21),
                              ('num', len(minhash)),
                              #('md5sum', ),
                              ('molecule', molecule),
                              ('cardinality', 123456),
                              ('mins', minhash))),
             )))))

    s = json.dumps(t)
    if sys.version_info[0] < 3:
        s = unicode(s)
    # no MD5SUM
    sig_entries = tuple(load_signatureset_json_iter(io.BytesIO(s.encode('utf-8')),
                                                    ignore_md5sum=True,
                                                    ijson=ijson))
    # Ensure all molecule types were read properly
    assert len(sig_entries) == 4

def test_save_load_multisig_json():
    e1 = sourmash.MinHash(n=1, ksize=20)
    sig1 = SourmashSignature(e1)

    e2 = sourmash.MinHash(n=1, ksize=25)
    sig2 = SourmashSignature(e2)

    x = save_signatures_json([sig1, sig2])
    y = list(load_signatures_json(x))

    print(x)

    assert len(y) == 2
    assert sig1 in y                      # order not guaranteed, note.
    assert sig2 in y
    assert sig1 != sig2


from airflow import DAG
from airflow.providers.postgres.operators.postgres import PostgresOperator
from airflow.sensors.external_task import ExternalTaskSensor
from airflow.utils.dates import days_ago
from datetime import timedelta

default_args = {
  'owner': 'airflow'
}

with DAG(
  dag_id='transform_load',
  start_date=days_ago(1),
  schedule_interval='@daily',
  default_args=default_args,
  catchup=False,
) as dag:
  wait_data_extracted = ExternalTaskSensor(
    task_id='wait_data_loaded',
    external_dag_id='extract_data',
    allowed_states=['success'],
    execution_delta=timedelta(minutes=1)
  )
  populate_country = PostgresOperator(
    task_id='populate_country',
    postgres_conn_id='postgres_default',
    sql='sql/populate_country.sql',
    dag=dag
  )
  populate_customer = PostgresOperator(
    task_id='populate_customer',
    postgres_conn_id='postgres_default',
    sql='sql/populate_customer.sql',
    dag=dag
  )
  populate_product = PostgresOperator(
    task_id='populate_product',
    postgres_conn_id='postgres_default',
    sql='sql/populate_product.sql',
    dag=dag
  )
  populate_date = PostgresOperator(
    task_id='populate_date',
    postgres_conn_id='postgres_default',
    sql='sql/populate_date.sql',
    dag=dag,
    params={
      'date': days_ago(1).strftime("%Y-%m-%d")
      #'start_date_x': '2020-03-01',
      #'end_date_x': '2020-03-31'
    }
  )
  populate_fact_sales = PostgresOperator(
    task_id='populate_fact_sales',
    postgres_conn_id='postgres_default',
    sql='sql/populate_fact_sales.sql',
    dag=dag
  )
  wait_data_extracted >> populate_country >> populate_customer >> populate_product >> populate_date >> populate_fact_sales


import time

from fHDHR.tools import humanized_time

from .channel import Channel
from .chan_ident import Channel_IDs

class Channels():
    """
    fHDHR Channels system
    """

    def __init__(self, fhdhr, origins):
        self.fhdhr = fhdhr
        self.fhdhr.logger.info("Initializing Channels system")

        self.origins = origins

        self.id_system = Channel_IDs(fhdhr, origins)

        self.list = {}
        for origin in list(self.origins.origins_dict.keys()):
            self.list[origin] = {}

        self.get_db_channels()

        self.channel_update_url = "/api/channels?method=scan"

        for origin in list(self.list.keys()):
            update_url = "%s&source=%s" % (self.channel_update_url, origin)
            self.fhdhr.scheduler.every(4).to(5).hours.do(self.fhdhr.api.threadget, url=update_url)

    def get_channel_obj(self, keyfind, valfind, origin=None):
        """
        Retrieve channel object by keyfind property.
        """

        if origin:

            origin = origin.lower()
            if keyfind == "number":
                matches = [self.list[origin][x].dict["id"] for x in list(self.list[origin].keys()) if self.list[origin][x].number == valfind]

            else:
                matches = [self.list[origin][x].dict["id"] for x in list(self.list[origin].keys()) if self.list[origin][x].dict[keyfind] == valfind]

            if len(matches):
                return self.list[origin][matches[0]]

        else:

            matches = []
            for origin in list(self.list.keys()):

                if keyfind == "number":
                    matches = [self.list[origin][x].dict["id"] for x in list(self.list[origin].keys()) if self.list[origin][x].number == valfind]

                else:
                    matches = [self.list[origin][x].dict["id"] for x in list(self.list[origin].keys()) if self.list[origin][x].dict[keyfind] == valfind]

                if len(matches):
                    return self.list[origin][matches[0]]

            if len(matches):
                return self.list[origin][matches[0]]

        return None

    def get_channel_list(self, keyfind, origin=None):
        """
        Get a list of channels by keyfind property.
        """

        if origin:

            if keyfind == "number":
                return [self.list[origin][x].number for x in [x["id"] for x in self.get_channels(origin)]]

            else:
                return [self.list[origin][x].dict[keyfind] for x in [x["id"] for x in self.get_channels(origin)]]

        else:

            matches = []
            for origin in list(self.list.keys()):

                if keyfind == "number":
                    next_match = [self.list[origin][x].number for x in [x["id"] for x in self.get_channels(origin)]]

                else:
                    next_match = [self.list[origin][x].dict[keyfind] for x in [x["id"] for x in self.get_channels(origin)]]

                if len(next_match):
                    matches.append(next_match)

            return matches[0]

    def get_channel_dict(self, keyfind, valfind, origin=None):
        """
        Retrieve channel object dict by keyfind property.
        """

        chan_obj = self.get_channel_obj(keyfind, valfind, origin)
        if chan_obj:
            return chan_obj.dict
        return None

    def set_channel_status(self, keyfind, valfind, updatedict, origin):
        """
        Set channel object property.
        """

        self.get_channel_obj(keyfind, valfind, origin).set_status(updatedict)

    def set_channel_enablement_all(self, enablement, origin):
        """
        Enable all channels.
        """

        for fhdhr_id in [x["id"] for x in self.get_channels(origin)]:
            self.list[fhdhr_id].set_enablement(enablement, origin)

    def set_channel_enablement(self, keyfind, valfind, enablement, origin):
        """
        Enable Channel.
        """

        self.get_channel_obj(keyfind, valfind, origin).set_enablement(enablement)

    def set_channel_favorite(self, keyfind, valfind, enablement, origin):
        """
        Favorite a Channel.
        """

        self.get_channel_obj(keyfind, valfind, origin).set_favorite(enablement)

    def get_db_channels(self, origin=None):
        """
        Retrieve existing channels from database.
        """

        if not origin:
            origins_list = list(self.list.keys())
        else:
            origins_list = origin.lower()

        if isinstance(origins_list, str):
            origins_list = [origins_list]

        for origin in origins_list:

            self.fhdhr.logger.info("Checking for %s Channel information stored in the database." % origin)
            channel_ids = self.fhdhr.db.get_fhdhr_value("channels", "list", origin) or []

            if len(channel_ids):
                self.fhdhr.logger.info("Found %s existing channels in the database." % str(len(channel_ids)))

            for channel_id in channel_ids:
                channel_obj = Channel(self.fhdhr, self.id_system, origin=origin, channel_id=channel_id)
                channel_id = channel_obj.dict["id"]
                self.list[origin][channel_id] = channel_obj

    def save_db_channels(self, origin=None):
        """
        Save Channel listing to the database.
        """

        if not origin:
            origins_list = list(self.list.keys())

        else:
            origins_list = origin.lower()

        if isinstance(origins_list, str):
            origins_list = [origins_list]

        for origin in origins_list:

            self.fhdhr.logger.debug("Saving %s channels to database." % origin)
            channel_ids = [self.list[origin][x].dict["id"] for x in list(self.list[origin].keys())]
            self.fhdhr.db.set_fhdhr_value("channels", "list", channel_ids, origin)

    def delete_channel(self, fhdhr_id, origin):
        """
        Delete a channel.
        """

        if origin in list(self.list.keys()):

            if fhdhr_id in list(self.list[origin].keys()):

                self.fhdhr.logger.debug("Deleting %s channel. Info: %s" % (origin, fhdhr_id))
                del self.list[origin][fhdhr_id]
                self.save_db_channels(origin)

    def get_channels(self, origin=None, forceupdate=False):
        """
        Pull Channels from origin.
        """

        if not origin:
            origins_list = list(self.list.keys())
        else:
            origins_list = origin.lower().lower()

        if isinstance(origins_list, str):
            origins_list = [origins_list]

        return_chan_list = []
        for origin in origins_list:

            if not len(list(self.list[origin].keys())):
                self.get_db_channels(origin=origin)

            if not forceupdate:
                return_chan_list.extend([self.list[origin][x].dict for x in list(self.list[origin].keys())])

            else:

                channel_origin_id_list = [str(self.list[origin][x].dict["origin_id"]) for x in list(self.list[origin].keys())]

                if self.fhdhr.config.dict["logging"]["level"].upper() == "NOOB":
                    self.fhdhr.logger.noob("Performing Channel Scan for %s. This Process can take some time, Please Wait." % origin)

                else:
                    self.fhdhr.logger.info("Performing Channel Scan for %s." % origin)

                channel_dict_list = self.origins.origins_dict[origin].get_channels()
                self.fhdhr.logger.info("Found %s channels for %s." % (len(channel_dict_list), origin))

                self.fhdhr.logger.info("Performing %s Channel Import, This can take some time, Please wait." % origin)

                newchan = 0
                chan_scan_start = time.time()
                for channel_info in channel_dict_list:

                    chan_existing = str(channel_info["id"]) in channel_origin_id_list

                    if chan_existing:
                        channel_obj = self.get_channel_obj("origin_id", channel_info["id"], origin)
                        self.fhdhr.logger.debug("Found Existing %s channel. Info: %s" % (origin, channel_info))

                    else:
                        self.fhdhr.logger.debug("Creating new %s channel. Info: %s" % (origin, channel_info))
                        channel_obj = Channel(self.fhdhr, self.id_system, origin, origin_id=channel_info["id"])

                    channel_id = channel_obj.dict["id"]
                    channel_obj.basics(channel_info)

                    if not chan_existing:
                        self.list[origin][channel_id] = channel_obj
                        newchan += 1

                self.fhdhr.logger.info("%s Channel Import took %s" % (origin, humanized_time(time.time() - chan_scan_start)))

                if not newchan:
                    newchan = "no"
                self.fhdhr.logger.info("Found %s NEW channels for %s." % (newchan, origin))

                self.fhdhr.logger.info("Total %s Channel Count: %s" % (origin, len(self.list[origin].keys())))
                self.save_db_channels(origin=origin)

                self.fhdhr.db.set_fhdhr_value("channels", "scanned_time", time.time(), origin)
                return_chan_list.extend([self.list[origin][x].dict for x in list(self.list[origin].keys())])

        return return_chan_list

    def get_channel_stream(self, stream_args, origin):
        """
        Retrieve Stream from Channel.
        """

        return self.origins.origins_dict[origin].get_channel_stream(self.get_channel_dict("number", stream_args["channel"]), stream_args)


import numpy as np
from sklearn.cluster import MeanShift, estimate_bandwidth
from sklearn.datasets.samples_generator import make_blobs
import matplotlib.pyplot as plt
from itertools import cycle

#Generates an array of vectors to cluster
centers = [[1, 1],[5,3],[-2,-1]]
X, _ = make_blobs(n_samples=10, centers=centers, cluster_std=0.5)

bandwidth = 1

ms = MeanShift(bandwidth=bandwidth, bin_seeding=True)

# Giving one vector at a time to the classificator, to try the algorithm in a sequential way
for i in range(len(X)):
    ms.fit([X[i]])
    labels = ms.labels_
    cluster_centers = ms.cluster_centers_

    labels_unique = np.unique(labels)
    n_clusters_ = len(labels_unique)

    print("number of estimated clusters : %d" % n_clusters_)

    # On each new graph printed, a new point is supposed to appear. But we can see that the old ones are gone.
    # That's because the function fit() reset each time it's called.
    # Meanshift can't be used for sequential processing in Scikitlearn.
    plt.figure(1)
    plt.clf()

    colors = cycle('bgrcmykbgrcmykbgrcmykbgrcmyk')
    for k, col in zip(range(n_clusters_), colors):
        my_members = labels == k
        cluster_center = cluster_centers[k]
        plt.plot(X[my_members, 0], X[my_members, 1], col + '.')
        plt.plot(cluster_center[0], cluster_center[1], 'o', markerfacecolor=col,
                 markeredgecolor='k', markersize=14)
        plt.title('Estimated number of clusters: %d' % n_clusters_)
        plt.show()


# -*- coding: utf-8 -*-
'''
This script will read the *.rgb files and produce pngs from them to be
displayed in the gui.
'''

from __future__ import print_function, absolute_import, unicode_literals, division
from collections import OrderedDict
import glob
import os

from qtpy import QtGui, QtCore
import numpy as np

try:
    import vtk
except ImportError:
    vtk = None

SCRIPT_DIRECTORY = os.path.abspath(os.path.dirname(__file__))
COLOR_MAP_EXT = '.rgb'
COLOR_MAPS = glob.glob(os.path.join(SCRIPT_DIRECTORY, '*' + COLOR_MAP_EXT))

def read_rgb(path):
    """
    Given a file path, extract the rgb values.
    """
    rgb_list = []
    with open(path) as f:
        for line in f:
            rgb = line.split()
            if len(rgb) == 3:
                try:
                    rgb_list.append([float(v) for v in rgb])
                except:
                    pass
    return rgb_list

def get_color_map_dict():
    '''return a dictionary of color maps'''
    color_dict = OrderedDict()
    for f in COLOR_MAPS:
        name = os.path.basename(f).replace(COLOR_MAP_EXT, '').lower()
        rgb = read_rgb(f)
        color_dict[name] = rgb
        color_dict[name + '_reversed'] = list(reversed(rgb))
    return color_dict

def get_color_map_pngs():
    '''return a dictionary of name:path_to_png'''
    pngs = OrderedDict()
    for f in glob.glob(os.path.join(SCRIPT_DIRECTORY,'*.png')):
        name = os.path.basename(f).replace('.png', '').lower()
        pngs[name] = f
    return pngs

icon_cache = OrderedDict()
def build_qicons():
    if icon_cache:
        return icon_cache

    icons = OrderedDict()
    for name, png in get_color_map_pngs().items():
        pix = QtGui.QPixmap(png)
        icons[name] = {
            'bar': QtGui.QIcon(pix.scaled(100, 25, transformMode=QtCore.Qt.SmoothTransformation)),
            'icon': QtGui.QIcon(pix.scaled(25, 25, transformMode=QtCore.Qt.SmoothTransformation)),
            }
    icon_cache.update(icons)
    return icons

def build_vtk_lookup_tables():
    '''build and return lookup tables for vtk'''
    LUTs = OrderedDict()
    for name, colors in get_color_map_dict().items():
        lookup_table = vtk.vtkLookupTable()
        lookup_table.SetNumberOfTableValues(len(colors))

        for i, c in enumerate(colors):
            lookup_table.SetTableValue(i, c[0], c[1], c[2], 1.0)

        LUTs[name] = lookup_table
    return LUTs

if __name__ == "__main__":
    '''Use matplotlib to generate previews of the color maps and save them as
    png files so that they can be displayed in the gui'''
    from matplotlib import pyplot as plt
    from matplotlib.colors import LinearSegmentedColormap

    def plot_color_gradients(name, cmap, gradient):
        fig, axes = plt.subplots(1, figsize=(20, 1))
        fig.subplots_adjust(top=1, bottom=0, left=0, right=1)
        axes.imshow(gradient, aspect='auto', cmap=cmap)
        # Turn off *all* ticks & spines, not just the ones with colormaps.
        axes.set_axis_off()
        fig.savefig(name+'.png', dpi=1)

    gradient = np.linspace(0, 1, 256)
    gradient = np.vstack((gradient, gradient))

    for name, colors in get_color_map_dict().items():
        # normal
        cmap = LinearSegmentedColormap.from_list(name, colors)
        plot_color_gradients(name, cmap, gradient)

        # revered
        cmap = LinearSegmentedColormap.from_list(name, list(reversed(colors)))
        plot_color_gradients(name+'_reversed', cmap, gradient)


'''
Write a recursive function to calculate the number of vowels in a string.
'''
string = input()

def vowels_count(string):
    vowels = ["a", "e", "i", "o", "u"]
    string = string.lower()

    # Base Case
        # Once we have reached our base case we can go back the function-call chain and calculate
        # the return value for the function call just before the base case.
    if len(string) == 0:
        return 0

    else:
        if string[0] in vowels:
            return 1 + vowels_count(string[1:])
        else:
            return vowels_count(string[1:])  # or 0 + vowels_count(string[1:]

print(vowels_count(string))

# _______________________________________________________
# Shorter way

def rec_vowel_count(s):
    if not s:
        return 0
    return (1 if s[0] in 'aeiouAEIOU' else 0) + rec_vowel_count(s[1:])

# https://stackoverflow.com/questions/12877671/counting-vowels-in-a-string-using-recursion
# Key Points to look into:
# List Comprehension [expression for item in list if conditional]
# List Slicing

Python/Python Fundamentals/6.Recursion/3. Programming Questions/Exercise2 (Vowels in a sting).py


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

import csv
from collections import Counter
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import math
import time

#Get the record that the number of use at least record_num
def GetRecord(file_path, record_num, out_path):
	with open(file_path, encoding='UTF-8') as cfr:
		frequency = []#the number of trip that a person travels within a period(a week)
		data_temp = []
		result = []
		k = 0
		chunks = pd.read_csv(file_path, iterator = True, header = None, chunksize = 2000000)
		for chunk in chunks:
			for row in chunk.values:
				k += 1
				if(k%2000000 == 0):
					print(k)
				if(len(data_temp) == 0):#the first row
					data_temp.append(row)
					continue
				if(row[0] == data_temp[0][0]):
					data_temp.append(row)
				else:
					if(len(data_temp) >= record_num):
						result.extend(data_temp)
					frequency.append(len(data_temp))
					data_temp.clear()
					data_temp.append(row)
		print('A total of ',k,' records.')
		print('Extract ',len(result),' records.')
		with open(out_path, 'w', newline='') as cfw:
			writer = csv.writer(cfw)
			writer.writerows(result)
		#show the frequency
		frequency = sorted(Counter(frequency).items())
		draw_frequency(frequency)
	return result

def str2time(tim):
	ts = tim.split(' ')[1].split(':')
	st = float(ts[0])*3600 + float(ts[1])*60 + float(ts[2])
	return st

#Get the commute
def GetCommute(file_path, out_path):
	with open(file_path, encoding='UTF-8') as cfr:
		data_temp = []
		result = []
		k = 0
		chunks = pd.read_csv(file_path, iterator = True, header = None, chunksize = 2000000)
		for chunk in chunks:
			for row in chunk.values:
				k += 1
				if(k%200000 == 0):
					print(k)
				if(len(data_temp) == 0):#the first row
					data_temp.append(row)
					continue
				if(row[0] == data_temp[0][0]):
					data_temp.append(row)
				else:
					if(len(data_temp) == 2 and data_temp[0][2]==data_temp[1][6] and data_temp[0][6]==data_temp[1][2] and (str2time(data_temp[1][1])-str2time(data_temp[0][5]))/3600>6):
						result.extend(data_temp)
					data_temp.clear()
					data_temp.append(row)
		with open(out_path, 'w', newline='') as cfw:
			writer = csv.writer(cfw)
			writer.writerows(result)

#Draw the hist of frequency
def draw_frequency(frequency):
	numbers = []#the number of a person travel within a period
	counts = []#the counts
	[numbers,counts] = [[k for k,v in frequency],[v for k,v in frequency]]
	rects=plt.bar(range(len(counts)), counts, color='rgbyc')
	index=[i for i in range(0,len(numbers))]
	plt.ylim(ymax=max(counts)*1.05, ymin=0)
	plt.xticks(index, numbers)
	plt.ylabel("Frequency")
	plt.xlabel("Number of Travels")
	plt.title("Statistics")
	for rect in rects:
		height = rect.get_height()
		plt.text(rect.get_x() + rect.get_width() / 2, height, str(height), ha='center', va='bottom')
	plt.show()

def EuDistance(x1, y1, x2, y2):
	return str(math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)))

def time2feature(tim):
	ts = tim.split(' ')[1].split(':')
	st = float(ts[0])*2
	if float(ts[1])>30:
		st = st+1
	return str(st)

def trip2feature(in_path,out_path,out_path_1):
	print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))
	trips = pd.read_csv(in_path,header=None,encoding='gbk')
	out_data = []
	out_data_1 = []
	i = 0
	for index,row in trips.iterrows():
		if i%200000==0:
			print(i)
		tmp = []
		tmp.append(time2feature(row[1]))
		tmp.append(row[3])
		tmp.append(row[4])
		tmp.append(time2feature(row[5]))
		tmp.append(row[7])
		tmp.append(row[8])
		tmp.append(row[9])
		tmp.append(row[10])
		tmp.append(row[11])
		tmp.append(EuDistance(float(row[3]),float(row[4]),float(row[7]),float(row[8])))
		out_data.append(tmp)
		tmp_1 = []
		tmp_1.append(row[0])
		tmp_1.append(row[3])
		tmp_1.append(row[4])
		tmp_1.append(row[7])
		tmp_1.append(row[8])
		out_data_1.append(tmp_1)
		i=i+1
	out_data = np.array(out_data,dtype = float)
	out_data_1 = np.array(out_data_1,dtype = float)
	#standardize
	out_data = (out_data-out_data.min(axis=0)) / (out_data.max(axis=0)-out_data.min(axis=0))
	np.savetxt(out_path,out_data,delimiter=',')
	np.savetxt(out_path_1,out_data_1,delimiter=',')
	print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))

def choose1from2(in_path,out_path):
	print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))
	trips = pd.read_csv(in_path,header=None,encoding='gbk')
	out_data = []
	i = 0
	for index,row in trips.iterrows():
		if i%2==0:
			out_data.append(row)
		i=i+1
	out_data = np.array(out_data,dtype = float)
	np.savetxt(out_path,out_data,delimiter=',')
	print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))

if __name__ == '__main__':
	#result = GetRecord('X:\\TRIP20180707\\201704.csv', 10, 'X:\\TRIP20180707\\10.csv')
	#trip2feature('X:\\HM\\0403cmu.csv','X:\\HM\\0403cmu_f.csv','X:\\HM\\0403cmu_l.csv')
	#GetCommute('X:\\HM\\0403.csv', 'X:\\HM\\0403cmu.csv')
	choose1from2('X:\\HM\\0403cmu_l.csv','X:\\HM2\\0403cmu_ls.csv')
	print('All done...')


import cv2
import os

nombre_video = "children_ages"
directory = "../outputs/children_ages/images"
path, dirs, files = next(os.walk(directory))
file_count = len(files)

fourcc = cv2.VideoWriter_fourcc('M','J','P','G')

out = cv2.VideoWriter('{}.avi'.format(nombre_video),fourcc, 10.0, (800,600))

for i in range(file_count):
    img_path = os.path.join(directory,f'image({i}).png')
    print(img_path)
    frame = cv2.imread(img_path)
    out.write(frame)

out.release()

# Para enviar por wpp convertir con este link https://webservice.online-convert.com/convert-for-whatsapp


"""
Code ideas from https://github.com/Newmu/dcgan and tensorflow mnist dataset reader
"""
import numpy as np
from numpy import newaxis
import scipy.misc as misc
import nibabel as nib
import random
import cv2

class BatchDatset:
    BACKGROUD_CLASS = 0
    NECROSIS_CLASS = 1
    EDEMA_CLASS = 2
    UNKNOWN_CLASS = 3
    ENHANCING_TUMOR_CLASS = 4
    files = []
    images = []
    annotations = []
    image_options = {}
    batch_offset = 0
    epochs_completed = 0
    layers = 155
    brain_idx = 0
    brain_filename = ""

    def __init__(self, records_list, image_options={}):
        """
        Intialize a generic file reader with batching for list of files
        :param records_list: list of file records to read -
        sample record: {'image': f, 'annotation': annotation_file, 'filename': filename}
        :param image_options: A dictionary of options for modifying the output image
        Available options:
        resize = True/ False
        resize_size = #size of output image - does bilinear resize
        color=True/False
        """
        print("Initializing Batch Dataset Reader...")
        print(image_options)
        self.files = records_list
        self.image_options = image_options
        # self._read_images()

    def _read_images(self):
        self.images, self.annotations = self._match_image_and_mask()

    def _match_image_and_mask(self):
        # match image and mask for single brain case
        # result image shape will be (155, 240, 240, 4)
        # result annot shape will be (155, 240, 240, 1)

        images = np.zeros((self.layers, 224, 224, 4))
        masks = np.empty((self.layers, 224, 224, 1))

        t1_file    = [file for file in self.files[self.brain_idx]["input"] if file.endswith("t1.nii.gz")][0]
        t1ce_file  = [file for file in self.files[self.brain_idx]["input"] if file.endswith("t1ce.nii.gz")][0]
        t2_file    = [file for file in self.files[self.brain_idx]["input"] if file.endswith("t2.nii.gz")][0]
        flair_file = [file for file in self.files[self.brain_idx]["input"] if file.endswith("flair.nii.gz")][0]
        mask_file   = self.files[self.brain_idx]["mask"][0]

        t1_img_array    = nib.load(t1_file).get_fdata()
        t1ce_img_array  = nib.load(t1ce_file).get_fdata()
        t2_img_array    = nib.load(t2_file).get_fdata()
        flair_img_array = nib.load(flair_file).get_fdata()
        mask_array      = nib.load(mask_file).get_fdata()

        mask_regrouped  = self._regroup_mask_array(mask_array)

        for z in range(self.layers):
            tmp1 = np.concatenate((t1_img_array[:, :, z][:, :, newaxis], t1ce_img_array[:, :, z][:, :, newaxis]), axis = 2)
            tmp2 = np.concatenate((t2_img_array[:, :, z][:, :, newaxis], flair_img_array[:, :, z][:, :, newaxis]), axis = 2)
            concatenate_img_data = np.concatenate((tmp1, tmp2), axis=2)

            if self.image_options.get("resize", False) and self.image_options["resize"]:
                resize_size = int(self.image_options["resize_size"])
                resize_image = cv2.resize(concatenate_img_data, (resize_size, resize_size), interpolation=cv2.INTER_AREA)
                resize_mask = cv2.resize(mask_regrouped[:, :, z][:, :, newaxis], (resize_size, resize_size), interpolation=cv2.INTER_AREA)
                resize_mask = resize_mask[:, :, newaxis]
            else:
                resize_image = concatenate_img_data
                resize_mask = mask_regrouped[:, :, z][:, :, newaxis]

            images[z] = resize_image
            masks[z] = resize_mask

        return images, masks

    def _set_random_brain(self):
        random_brain = random.choice(list(range(len(self.files))))
        self.brain_idx = random_brain
        self.brain_filename = self.files[self.brain_idx]["filename"]

    def get_single_brain(self, brain_index):
        self.brain_idx = brain_index
        self.brain_filename = self.files[self.brain_idx]["filename"]
        self._read_images()
        return self.images, self.annotations

    def get_single_brain_by_name(self, filename):
        self.brain_filename = filename
        for i in range(len(self.files)):
            if (self.files[i]["filename"] == filename):
                self.brain_idx = i
        self._read_images()
        return self.images, self.annotations

    def _regroup_mask_array(self, mask_array):
        regrouped = mask_array
        regrouped[regrouped == self.UNKNOWN_CLASS] = 0
        regrouped[regrouped != self.BACKGROUD_CLASS] = 1
        return regrouped

    def get_brain_filename(self):
        return self.brain_filename

    def get_records(self):
        return self.images, self.annotations

    def reset_batch_offset(self, offset=0):
        self.batch_offset = offset

    def next_batch(self, batch_size):
        self._set_random_brain()
        self._read_images()
        ##########################################
        # start = self.batch_offset
        # self.batch_offset += batch_size
        # if self.batch_offset > self.images.shape[0]:
        #     # Finished epoch
        #     self.epochs_completed += 1
        #     print("****************** Epochs completed: " + str(self.epochs_completed) + "******************")
        #     # Shuffle the data
        #     perm = np.arange(self.images.shape[0])
        #     np.random.shuffle(perm)
        #     self.images = self.images[perm]
        #     self.annotations = self.annotations[perm]
        #     # Start next epoch
        #     start = 0
        #     self.batch_offset = batch_size
        # end = self.batch_offset
        # print(f"Input Batches: from {start} to {end}")
        ###########################################
        start = random.choice(list(range(50, 100, 1)))
        end = start + batch_size
        return self.images[start:end], self.annotations[start:end]

    def get_random_batch(self, batch_size):
        self._set_random_brain()
        self._read_images()
        indexes = np.random.randint(0, self.images.shape[0], size=[batch_size]).tolist()
        return self.images[indexes], self.annotations[indexes]


# resize.py

import numpy as np
import argparse
import imutils as iu
import cv2

ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required = True, help = "Path to the image")
args = vars(ap.parse_args())

image = cv2.imread(args["image"])
cv2.imshow("Original", image)

r = 150.0 / image.shape[1]
dim = (150, int(image.shape[0] * r))

#interpolation can also be
#.INTER_LINEAR, .INTER_CUBIC, .INTER_NEAREST
resized = cv2.resize(image, dim, interpolation = cv2.INTER_AREA)
cv2.imshow("Resize (Width)", resized)

#resize by height
r = 50.0 / image.shape[0]
dim = (int(image.shape[1] * r), 50)

resized = cv2.resize(image, dim, interpolation = cv2.INTER_AREA)
cv2.imshow("Resized (height)", resized)
cv2.waitKey(0)

resized = iu.resize(image, width = 500)
cv2.imshow("Resized with imutils function", resized)
cv2.waitKey(0)