"""
 DefendersGame
 Kevin Chen
 Version 1.0
 This code opens up a game where you control a player block with the mouse.
 The goal is to score as many points as possible.
 Blue blocks add 1 point, red blocks deduct 1 point,
 and purple blocks reset your score to 0 AND sets your lives to 1, so be careful.
 Mouseclick or press the space key to shoot a bullet.
"""
import pygame
import random
import time

# Define some colors
from pygame import mixer

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
YELLOW = (250, 238, 3)
PURPLE = (135, 3, 250)
BACKGROUND = (239, 229, 255)
# --- extra functions
FALL_SPEED = 1
MOVING_SPEED = 10
lives = 3

# --- Classes

class Block(pygame.sprite.Sprite):
    """ This class represents the blocks. """

    def __init__(self, color):
        # Call the parent class (Sprite) constructor
        super(Block, self).__init__()

        self.image = pygame.Surface([30, 15])
        self.image.fill(color)

        self.rect = self.image.get_rect()
        self.rect.x = 0
        self.rect.y = 0

class Player(pygame.sprite.Sprite):
    """ This class represents the Player. """

    def __init__(self):
        """ Set up the player on creation. """
        # Call the parent class (Sprite) constructor
        super(Player, self).__init__()

        self.image = pygame.Surface([20, 20])
        self.image.fill(BLACK)

        self.rect = self.image.get_rect()

    def update(self):
        """ Update the player's position. """
        # Get the current mouse position. This returns the position
        # as a list of two numbers.
        pos = pygame.mouse.get_pos()

        # Set the player x position to the mouse x position and sets bounds
        if pos[0] < 0:
            self.rect.x = 0
        elif pos[0] > 680:
            self.rect.x = 680
        else:
            self.rect.x = pos[0]

class Bullet(pygame.sprite.Sprite):
    """ This class represents the bullet. """

    def __init__(self):
        # Call the parent class (Sprite) constructor
        super(Bullet, self).__init__()

        self.image = pygame.Surface([4, 10])
        self.image.fill(BLACK)

        self.rect = self.image.get_rect()

    def update(self):
        """ Move the bullet. """
        self.rect.y -= 5

# Initialize Pygame and sound
pygame.init()
pygame.mixer.init()
pygame.mixer.music.load("bgm.ogg")

# Set the height and width of the screen
screen_width = 700
screen_height = 500
screen = pygame.display.set_mode([screen_width, screen_height])
# --- Sprite lists

# This is a list of every sprite. All blocks and the player block as well.
all_sprites_list = pygame.sprite.Group()

# List of good sprites in the game
good_blocks_list = pygame.sprite.Group()

# List of each bullet
bullet_list = pygame.sprite.Group()

# List of bad sprites
bad_blocks_list = pygame.sprite.Group()

# List of super sprites
god_blocks_list = pygame.sprite.Group()
mad_blocks_list = pygame.sprite.Group()
# --- Create the sprites

for i in range(40):
    # This represents a mad block
    madblock = Block(PURPLE)

    # Set a random location for the block
    madblock.rect.x = random.randrange(0, 670)
    madblock.rect.y = random.randrange(-4000, -800)

    # Add the block to the list of objects
    mad_blocks_list.add(madblock)
    all_sprites_list.add(madblock)

for i in range(4):
    # This represents a god block
    godblock = Block(YELLOW)

    # Set a random location for the block
    godblock.rect.x = random.randrange(0, 670)
    godblock.rect.y = random.randrange(-15000, -3000)

    # Add the block to the list of objects
    god_blocks_list.add(godblock)
    all_sprites_list.add(godblock)

for i in range(120):
    # This represents a good block
    goodblock = Block(BLUE)

    # Set a random location for the block
    goodblock.rect.x = random.randrange(0, 670)
    goodblock.rect.y = random.randrange(-5500, 200)

    # Add the block to the list of objects
    good_blocks_list.add(goodblock)
    all_sprites_list.add(goodblock)

for i in range(80):
    # This represents a bad block
    badblock = Block(RED)

    # Set a random location for the block
    badblock.rect.x = random.randrange(0, 670)
    badblock.rect.y = random.randrange(-5500, 200)

    # Add the block to the list of objects
    bad_blocks_list.add(badblock)
    all_sprites_list.add(badblock)

# Create a black player block
player = Player()
all_sprites_list.add(player)

# Used to manage how fast the screen updates
clock = pygame.time.Clock()

score = 0

print("Welcome to the Defenders game!")
print("The goal is to get as many points as possible before you run out of lives.")
print("You have 3 lives. Don't let the blue blocks reach the bottom!")
print("Use the mouse to control your position, and mouseclick or press the space key to fire a bullet!")
print("Avoid hitting red and purple blocks!")
time.sleep(4)
print("Get ready!")
time.sleep(1)
print("Game starts in 3...")
time.sleep(1)
print("2...")
time.sleep(1)
print("1...")
time.sleep(1)
pygame.mixer.music.play(-1)
done = False

# helper function to make a new block
def makenewblock(colour):
    newblock = Block(colour)
    newblock.rect.x = random.randrange(0, 670)
    newblock.rect.y = random.randrange(-5500, -15)
    all_sprites_list.add(newblock)
    return newblock

# -------- Main Program Loop -----------

while not done:
    # --- Event Processing

    for badblock in bad_blocks_list:
        badblock.rect.y += FALL_SPEED

    for goodblock in good_blocks_list:
        goodblock.rect.y += FALL_SPEED

    for godblock in god_blocks_list:
        godblock.rect.y += FALL_SPEED * 2
        godblock.rect.x += MOVING_SPEED * 3

    for madblock in mad_blocks_list:
        madblock.rect.y += FALL_SPEED
        madblock.rect.x += MOVING_SPEED

    for Player in all_sprites_list:
        player.rect.y = 480

    # Loop until the user clicks the close button.
    for event in pygame.event.get():
        pygame.key.set_repeat(0)
        key_press = pygame.key.get_pressed()
        if event.type == pygame.QUIT:
            done = True

        elif event.type == pygame.MOUSEBUTTONDOWN or key_press[pygame.K_SPACE]:
            # Fire a bullet if the user clicks the mouse button or space key
            bullet = Bullet()
            # Set the bullet so it is where the player is
            bullet.rect.x = player.rect.x + 8
            bullet.rect.y = player.rect.y
            # Add the bullet to the lists
            all_sprites_list.add(bullet)
            bullet_list.add(bullet)

        # Does same thing as above, except mouse can be held down for rapid fire
        # elif pygame.mouse.get_pressed()[0] or key_press[pygame.K_SPACE]:
        #     # Fire a bullet if the user clicks the mouse button or space key
        #     bullet = Bullet()
        #     # Set the bullet so it is where the player is
        #     bullet.rect.x = player.rect.x + 8
        #     bullet.rect.y = player.rect.y
        #     # Add the bullet to the lists
        #     all_sprites_list.add(bullet)
        #     bullet_list.add(bullet)

    # --- Game logic
    for madblock in mad_blocks_list:
        if madblock.rect.x >= 670:
            MOVING_SPEED = -5
        elif madblock.rect.x <= 0:
            MOVING_SPEED = 5

    for godblock in god_blocks_list:
        if godblock.rect.x >= 680:
            MOVING_SPEED = -5
        elif godblock.rect.x <= 0:
            MOVING_SPEED = 5
    # Call the update() method on all the sprites
    all_sprites_list.update()

    # Calculate mechanics for each bullet
    for bullet in bullet_list:

        # See if it hit a block
        good_block_hitlist = pygame.sprite.spritecollide(bullet, good_blocks_list, True)
        bad_block_hitlist = pygame.sprite.spritecollide(bullet, bad_blocks_list, True)
        god_block_hitlist = pygame.sprite.spritecollide(bullet, god_blocks_list, True)
        mad_block_hitlist = pygame.sprite.spritecollide(bullet, mad_blocks_list, True)
        # For each block hit, remove the bullet and add to the score
        for block in good_block_hitlist:
            bullet_list.remove(bullet)
            all_sprites_list.remove(bullet)
            score += 1
            print("You gained a point!")
            good_blocks_list.add(makenewblock(BLUE))

        for block in bad_block_hitlist:
            bullet_list.remove(bullet)
            all_sprites_list.remove(bullet)
            score -= 1
            print("You lost a point!")
            bad_blocks_list.add(makenewblock(RED))

        for block in god_block_hitlist:
            bullet_list.remove(bullet)
            all_sprites_list.remove(bullet)
            god_blocks_list.remove(block)
            score += 50
            lives += 3
            FALL_SPEED = 0
            print("WOW!! YOU HIT THE SECRET BLOCK!!")
            time.sleep(1)
            print("You gained 50 points and 3 lives!!")
            time.sleep(2)
            print("Well done! Game resumes in 3...")
            time.sleep(1)
            print("2...")
            time.sleep(1)
            print("1...")
            time.sleep(1)
            FALL_SPEED = 1

        for block in mad_block_hitlist:
            bullet_list.remove(bullet)
            all_sprites_list.remove(bullet)
            mad_blocks_list.add(makenewblock(PURPLE))
            score = 0
            lives = 1
            print("You hit an evil block!!")
            print("Your score was reset and you have 1 life left.")
            print("Be careful!")

        # Remove the bullet if it flies up off the screen
        if bullet.rect.y < -10:
            bullet_list.remove(bullet)
            all_sprites_list.remove(bullet)

    # --- Check if blocks went off the screen

    # Check if good block went off the screen
    for goodblock in good_blocks_list:
        if goodblock.rect.y >= 500:
            pygame.mixer.pause()
            good_blocks_list.remove(goodblock)
            all_sprites_list.remove(goodblock)
            lives -= 1
            if lives <= 0:
                pygame.mixer.music.stop()
                print("Game over.. you have no lives left!")
                time.sleep(1)
                print("Your final score is " + str(score) + ". Good game!")
                done = True
            else:
                FALL_SPEED = 0
                print("You lost a life!")
                time.sleep(2)
                if lives >= 2:
                    print("You have " + str(lives) + " lives left.")
                    time.sleep(1.5)
                else:
                    print("You have " + str(lives) + " life left.")
                    time.sleep(1.5)
                    print("Be careful!")
                    time.sleep(1)
                print("Resetting board...")
                for block in all_sprites_list:
                    block.rect.y -= 500
                    block.rect.x = random.randrange(20, 680)
                time.sleep(1.5)
                print("Resuming in 3...")
                time.sleep(1)
                print("2...")
                time.sleep(1)
                print("1...")
                time.sleep(1)
                pygame.mixer.music.unpause()
                FALL_SPEED = 1

    # Check if bad block went off the screen
    for badblock in bad_blocks_list:
        if badblock.rect.y >= 500:
            # Reset random location for the bad block
            badblock.rect.x = random.randrange(20, 680)
            badblock.rect.y = random.randrange(-5000, 0)

    # Check if god block went off the screen
    for godblock in god_blocks_list:
        if godblock.rect.y >= 500:
            # Delete god block
            god_blocks_list.remove(godblock)
            all_sprites_list.remove(godblock)

    # Check if mad block went off the screen
    for madblock in mad_blocks_list:
        if madblock.rect.y >= 500:
            # Reset random location for the bad block
            madblock.rect.x = random.randrange(20, 680)
            madblock.rect.y = random.randrange(-2500, -200)

    # Clear the screen
    screen.fill(BACKGROUND)

    # Draw all the sprites
    all_sprites_list.draw(screen)

    # Go ahead and update the screen with what we've drawn.
    pygame.display.flip()

    # --- Limit to 20 frames per second
    clock.tick(60)

pygame.quit()


class Solution(object):
    def longestPalindrome(self, s):
        """
        :type s: str
        :rtype: str
        """
        res = ''
        for i in range(len(s)):
            s1 = self.helper(s, i, i)
            s2 = self.helper(s, i, i+1)
            res = res if len(res) > len(s1) else s1
            res = res if len(res) > len(s2) else s2
        return res
    def helper(self, s, l , r):
        while l>=0 and r<len(s) and s[l] == s[r]:
            l -= 1
            r += 1
        return s[l+1:r]

### manacher
class Solution(object):
    def longestPalindrome(self, s):
        """
        :type s: str
        :rtype: str
        """
        if not s or len(s) < 2:
            return s
        str1 = self.pre_process(s)
        p_arr = [0]*len(str1)
        max_len, max_str = float('-inf'), ''
        c = r = -1
        for i in range(len(str1)):
            p_arr[i] = min(p_arr[2*c - i], r-i) if i < r else 1
            while i + p_arr[i] < len(str1) and i - p_arr[i] >= 0:
                if str1[i + p_arr[i]] == str1[i - p_arr[i]]:
                    p_arr[i] += 1
                else:
                    break
            if i + p_arr[i] > r:
                r = i + p_arr[i]
                c = i
            if max_len < p_arr[i]:
                max_len = p_arr[i]
                temp_start = (i - max_len + 1) // 2
                max_str = s[temp_start:temp_start+max_len-1]
        return max_str

    def pre_process(self, str1):
        fill_char = '#'
        str1 = list(str1)
        res = [fill_char]
        for char in str1:
            res.append(char)
            res.append(fill_char)
        return ''.join(res)

05_最长回文子串.py

05_最长回文子串.py


# -*- coding: utf-8 -*-
import scrapy
import json
import re

class TgcSpider(scrapy.Spider):
    name = 'tgc'
    datas = []

    # 爬虫开始时执行，有且仅执行一次
    def start_requests(self):
        for i in range(1, 931):
            self.datas.append({
                'agent': '7',
                'spm': '407.802989.5386188.1',
                'page': str(i),
                'pageSize': '40',
            })
        for data in self.datas:
            yield scrapy.FormRequest(
                url='https://leaseconsumer.souche.com//v1/secondCarSearchApi/querySecondCarListV2.json',
                callback=self.parse, formdata=data)

    def parse(self, response):
        d = json.loads(response.body.decode('utf-8'))
        d = d['data']['items']
        for i in d:
            item = {}
            item['seriesName'] = i['seriesName']
            item['modelName'] = i['modelName']
            item['modelName'] = re.sub('\s', '', item['modelName'])
            carId = i['id']
            item['mileageStr'] = i['mileageStr']
            item['prepaidAmountStr'] = i['prepaidAmountStr']
            item['cityName'] = i['cityName']
            item['installmentStr'] = i['installmentStr']
            yield scrapy.FormRequest(url='https://leaseconsumer.souche.com//v1/followShopApi/getSecCarShopPage.json',
                                     callback=self.parse_item,
                                     formdata={'agent': '7',
                                               'spm': '407.802989.5386188.1',
                                               'page': '1',
                                               'pageSize': '1000',
                                               'carType': '1',
                                               'carId': carId, },
                                               meta={'item': item})

    def parse_item(self, response):
        item = response.meta['item']
        d = json.loads(response.body.decode('utf-8'))
        d = d['data']['items']
        for i in d:
            item['shopName'] = i['shopName']
            item['shopAddr'] = i['shopAddr']
            item['tel'] = i['contactTel']
            yield item


# Author: Simon Blanke
# Email: simon.blanke@yahoo.com
# License: MIT License

from numpy.random import normal, laplace, logistic, gumbel

from .surrogate_models import (
    RandomForestRegressor,
    ExtraTreesRegressor,
    GPR_linear,
    GPR,
)

def merge_dicts(base_dict, added_dict):
    # overwrite default values
    for key in base_dict.keys():
        if key in list(added_dict.keys()):
            base_dict[key] = added_dict[key]

    return base_dict

gaussian_process = {"gp_nonlinear": GPR(), "gp_linear": GPR_linear()}

tree_regressor = {
    "random_forest": RandomForestRegressor(),
    "extra_tree": ExtraTreesRegressor(),
}

def skip_refit_75(i):
    if i <= 33:
        return 1
    return int((i - 33) ** 0.75)

def skip_refit_50(i):
    if i <= 33:
        return 1
    return int((i - 33) ** 0.5)

def skip_refit_25(i):
    if i <= 33:
        return 1
    return int((i - 33) ** 0.25)

def never_skip_refit(i):
    return 1

skip_retrain_ = {
    "many": skip_refit_75,
    "some": skip_refit_50,
    "few": skip_refit_25,
    "never": never_skip_refit,
}

distribution = {
    "normal": normal,
    "laplace": laplace,
    "logistic": logistic,
    "gumbel": gumbel,
}

class Arguments:
    def __init__(self, *args, **kwargs):
        kwargs_opt = {
            # HillClimbingOptimizer
            "epsilon": 0.05,
            "distribution": "normal",
            "n_neighbours": 1,
            "n_positions": 10,
            # StochasticHillClimbingOptimizer
            "p_down": 0.3,
            # TabuOptimizer
            "tabu_memory": 3,
            # RandomRestartHillClimbingOptimizer
            "n_iter_restart": 10,
            # RandomAnnealingOptimizer
            "annealing_rate": 0.99,
            # SimulatedAnnealingOptimizer
            "start_temp": 100,
            "norm_factor": "adaptive",
            # StochasticTunnelingOptimizer
            "gamma": 0.5,
            "warm_start_population": False,
            # ParallelTemperingOptimizer
            "system_temperatures": [0.1, 1, 10, 100],
            "n_iter_swap": 10,
            # ParticleSwarmOptimizer
            "n_particles": 10,
            "inertia": 0.5,
            "cognitive_weight": 0.5,
            "social_weight": 0.5,
            # EvolutionStrategyOptimizer
            "individuals": 10,
            "mutation_rate": 0.7,
            "crossover_rate": 0.3,
            # BayesianOptimizer
            "max_sample_size": 1000000,
            "warm_start_smbo": False,
            "xi": 0.01,
            "gpr": "gp_nonlinear",
            "skip_retrain": "some",
            # TreeStructuredParzenEstimators
            "start_up_evals": 10,
            "gamma_tpe": 0.3,
            "tree_regressor": "random_forest",
        }

        self.kwargs_opt = merge_dicts(kwargs_opt, kwargs)

    def set_opt_args(self):
        self.epsilon = self.kwargs_opt["epsilon"]
        self.distribution = distribution[self.kwargs_opt["distribution"]]
        self.n_neighbours = self.kwargs_opt["n_neighbours"]
        self.n_positions = self.kwargs_opt["n_positions"]

        self.p_down = self.kwargs_opt["p_down"]

        self.tabu_memory = self.kwargs_opt["tabu_memory"]

        self.n_iter_restart = self.kwargs_opt["n_iter_restart"]

        self.annealing_rate = self.kwargs_opt["annealing_rate"]
        self.start_temp = self.kwargs_opt["start_temp"]
        self.norm_factor = self.kwargs_opt["norm_factor"]
        self.gamma = self.kwargs_opt["gamma"]

        self.system_temperatures = self.kwargs_opt["system_temperatures"]
        self.n_iter_swap = self.kwargs_opt["n_iter_swap"]

        self.n_particles = self.kwargs_opt["n_particles"]
        self.inertia = self.kwargs_opt["inertia"]
        self.cognitive_weight = self.kwargs_opt["cognitive_weight"]
        self.social_weight = self.kwargs_opt["social_weight"]

        self.individuals = self.kwargs_opt["individuals"]
        self.mutation_rate = self.kwargs_opt["mutation_rate"]
        self.crossover_rate = self.kwargs_opt["crossover_rate"]

        self.max_sample_size = self.kwargs_opt["max_sample_size"]
        self.warm_start_smbo = self.kwargs_opt["warm_start_smbo"]
        self.xi = self.kwargs_opt["xi"]

        if isinstance(self.kwargs_opt["gpr"], str):
            self.gpr = gaussian_process[self.kwargs_opt["gpr"]]
        else:
            self.gpr = self.kwargs_opt["gpr"]

        self.skip_retrain = skip_retrain_[self.kwargs_opt["skip_retrain"]]

        self.start_up_evals = self.kwargs_opt["start_up_evals"]
        self.gamma_tpe = self.kwargs_opt["gamma_tpe"]

        if isinstance(self.kwargs_opt["tree_regressor"], str):
            self.tree_regressor = tree_regressor[self.kwargs_opt["tree_regressor"]]
        else:
            self.tree_regressor = self.kwargs_opt["tree_regressor"]


from django.conf.urls import url, include

from .api import UserList, UserDetail
from .api import CatList, CatDetail, UserCatList
from .api import PhotoList, PhotoDetail, CatPhotoList, UserPhotoList
from .api import api_root

user_urls = [
    url(r'^/(?P<id>\d+)/cats$', UserCatList.as_view(), name='usercat-list'),
    url(r'^/(?P<id>\d+)$', UserDetail.as_view(), name='user-detail'),
    url(r'^$', UserList.as_view(), name='user-list'),
]

cat_urls = [
    url(r'^/(?P<id>\d+)/photos$', CatPhotoList.as_view(), name='catphoto-list'),
    url(r'^/(?P<id>\d+)/photos/(?P<pk>\d+)$', CatPhotoList.as_view()),
    url(r'^/(?P<id>\d+)$', CatDetail.as_view(), name='cat_detail'),
    url(r'^$', CatList.as_view(), name='cat-list'),
]

photo_urls = [
#    url(r'^/add/(?P<cat_id>\d+)$', PhotoAdd.as_view(), name='photo-add'),
    url(r'^/(?P<user_id>\d+)/(?P<pk>\d+)$', PhotoDetail.as_view(), name='photo-detail'),
    url(r'^/(?P<user_id>\d+)$', UserPhotoList.as_view(), name='userphoto-list'),
    url(r'^$', PhotoList.as_view(), name='photo-list')
]

urlpatterns = [
    url(r'^$', api_root),
    url(r'^users', include(user_urls)),
    url(r'^cats', include(cat_urls)),
    url(r'^photos', include(photo_urls)),
]


import numpy as np
import matplotlib
import matplotlib.pyplot as plt

# Questão 1
# A função normal(size=n) do modulo random do numpy cria n números aleatórios seguindo uma distribuição normal.
# Sabendo o uso dessa função faça:
# Crie um array 1000 números aleatórios usando essa função.
values = np.random.normal(1, 1, 1000)
print(f'{values}\n')
# Plote os números usando o ponto como marcador.
plt.plot(values, 'o')

# Faça o histograma do array. (Gráfico de barra)
plt.bar(values, values)
plt.show()

# Calcule a média
media = np.mean(values)
print(f'Média: {media}\n')
# E o desvio padrão do array.
desvio_padrao = np.std(values)
print(f'Desvio padrão: {desvio_padrao}\n')

# Percorra o array e identifique quais pontos são maiores(>) que a média mais duas vezes(2x) o desvio padrão.
# Armazene esses pontos em uma lista.
lista = []
# Duas vezes(2x) o desvio padrão (limite):
limite = media + (2 * desvio_padrao)
for i in range(len(values)):
    if values[i] > limite:
        lista.append(values[i])
    else:
        continue
print(f'Limite: {limite}\n')

print(f'Lista: {lista}\n')

# Calcule a média dos pontos armazenados na lista
mediaLista = np.mean(lista)
print(f'Média Lista: {mediaLista}\n')
# Calcule o desvio padrão dos pontos armazenados na lista
desvio_padraoLista = np.std(lista)
print(f'Desvio padrão Lista: {desvio_padraoLista}\n')

# Questão 2
# Um tipo de experimento comum é submeter uma pessoa a algum estímulo enquanto se regsitra algum sinal eletrofisiológico.
# Nesse tipo de experimento, geralmente estamos interessados em analisar uma certa quantidade de pontos antes e depois
# do estímulo ser apresentado. Cada corte antes e depois do estímulo é chamado de trial.

# Gere agora 12000 pontos usando a função normal. Esses 12000 pontos vão representar nosso sinal.
pontos = np.random.normal(1, 1, 12000)
# Crie um array com 10 linhas, onde cada linha vai conter 500 pontos antes e depois de um certo evento,
# ou seja esse array deve ter dimensão 10x1000.
array = np.reshape(pontos, (10, 1200))
print(array)
print('-' * 30)
# Os eventos aconteceram nos índices: 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000 e 10000.
print('Os eventos aconteceram nos índices: ')
for i in range(len(array)):
    print(array[i][1199])
print('-'* 30)
# Cada linha desse array é um trial.

# Calcule a média para cada uma das 10 linhas da matriz nova:
linhas = [
    'linha 1: ', 'linha 2: ', 'linha 3: ', 'linha 4: ', 'linha 5: ',
    'linha 6: ', 'linha 7: ', 'linha 8: ', 'linha 9: ', 'linha 10: '
    ]
for i in range(len(array)):
    print(f'Média {linhas[i]}: {np.mean(array[i])}')

print('-'* 30)

# Calcule o desvio padrão para cada uma das 10 linhas da matriz nova:
for i in range(len(array)):
    print(f'Desvio Padrão {linhas[i]}: {np.std(array[i])}')

# Calcule o trial médio que vai ter tamanho 1x1000
# Criando trial medio ???
trial_medio = np.empty(1200).reshape(1, 1200)
# depois sua média:
media_TrialMedio = np.mean(trial_medio)
desvio_TrialMedio = np.std(trial_medio)
print('-' * 30)
print(f'Media Trial Médio: {media_TrialMedio}')
print(f'Media Trial Médio: {desvio_TrialMedio}')

# Questão 3
# O comando do numpy linspace(start, stop, num) cria uma quantidade de pontos igual a num no intervalo entre start e stop.
# Use a função normal para gerar 500 pontos e salve em uma variável chamada data
start = int(input('Start: '))
stop = int(input('Stop: '))
data = np.linspace(start, stop, 500)
print(data)
# e depois responda as questões abaixo.

# Use linspace() para gerar 501 pontos entre 0 e 100 e armazene em uma variável chamada f.
f = np.linspace(0, 100, 501)

# Crie uma função que recebe 3 parâmetros: start, end e array. A função deve retornar os dois índices de onde o valor mais próximo de start e end foram encontrados no array.

def MaisProximo(array, start, end):
    array = np.asarray(array)
    indice1 = (np.abs(array - start)).argmin()
    indice2 = (np.abs(array - end)).argmin()
    print(indice1)
    print(indice2)
    return indice1, indice2

print('-'*10)

print(f)
# Usando a função criada no passo anterior, encontre os índices do array f onde o start é igual a 1 e o end é igual a 4.
MaisProximo(f, 1, 4)
# Repita esse passo para start e end iguais a (4, 12) e (12, 30).
MaisProximo(f, 4, 12)
MaisProximo(f, 12, 30)

# Usando os indíces de cada intervalo calcule a média e o desvio padrão para cada intervalo de indices da variável data.
intervalo1 = [f[5:21]]
print(intervalo1)
mediaIntervalo1 = np.mean(intervalo1)
print(mediaIntervalo1)
desvioIntervalo1 = np.std(intervalo1)
print(desvioIntervalo1)

intervalo2 = [f[20:60]]
print(intervalo2)
mediaIntervalo2 = np.mean(intervalo2)
print(mediaIntervalo2)
desvioIntervalo2 = np.std(intervalo2)
print(desvioIntervalo2)

intervalo3 = [f[60:150]]
print(intervalo3)
mediaIntervalo3 = np.mean(intervalo3)
print(mediaIntervalo3)
desvioIntervalo3 = np.std(intervalo3)
print(desvioIntervalo3)

# Crie um gráfico de barras para plotar a média dos 3 intervalos. Use o desvio padrão para mostrar a barra de erro de cada barra.
N = 3  # Numero de elementos
medias = (mediaIntervalo1, mediaIntervalo2, mediaIntervalo3)
d = (desvioIntervalo1, desvioIntervalo2, desvioIntervalo3)
ind = np.arange(N)
width = 0.5
p1 = plt.bar(ind, medias, width, yerr=d)

plt.ylabel('Pontuação')
plt.title('Intensidade de Crise')
plt.xticks(ind, ('mediaIntervalo 1', 'mediaIntervalo 2', 'mediaIntervalo 3'))
plt.yticks(np.arange(0, 32, 2))
gic = plt.show()


#2. Для списка реализовать обмен значений соседних элементов,
# т.е. Значениями обмениваются элементы с индексами 0 и 1, 2 и 3 и т.д.
# При нечетном количестве элементов последний сохранить на своем месте.
# Для заполнения списка элементов необходимо использовать функцию input().

#list_original = ['Адъ', 42, 0.5, None, 777, 'L', -1, 6, 0]

original = input('Введите слово >> ')

list_original = list(original)
count = len(list_original)
print(f'Количество элементов в списке: {count}')

list_reverse = list()
i = 1
j = None

while i <= count:
    list_reverse.extend(list_original[i:j:-1])
    j = i
    i += 2

print(f'Начальный список: {list_original}.')
print(f'Список с обменом соседних элементов: {list_reverse}.')


TESTPOOL="tank"
TESTPOOL1="tank1"
TESTPOOL2="tank2"
TESTPOOL3="tank3"
TESTFS="zfs"
TESTFS1="zfs1"
TESTFS2="zfs2"

TESTDIR="/tank/zfs"
TESTDIR1="/tank1/zfs1"

TESTFILE="test_file"
TESTFILE1="test_file1"

TESTCTR="testctr"
NO_POOLS="no pools available"


import numpy as np
import random
import tensorflow as tf
from tqdm import trange

# tf.keras.mixed_precision.experimental.set_policy('mixed_float16')

class QNetwork():
    def __init__(self, state_dim):
        self.model = tf.keras.Sequential([
            tf.keras.layers.Input(state_dim),
            tf.keras.layers.Dense(64, activation='relu'),
            tf.keras.layers.Dense(64, activation='relu'),
            tf.keras.layers.Dense(2, activation='linear')
        ])
        self.loss = tf.keras.losses.MeanSquaredError()
        self.optimizer = tf.keras.optimizers.SGD(learning_rate=0.2)

    def predict(self, state):
        if state.ndim == 1:
            state = np.expand_dims(state, axis=0)
        return self.model(state)

    def train(self, experiences, gamma):
        states = np.stack(experiences[:, 0])
        actions = experiences[:, 1]
        rewards = experiences[:, 2]
        states_next = np.stack(experiences[:, 3])
        terminals = experiences[:, 4]

        q_next = self.predict(states_next)
        q_expected = rewards + (1 - terminals) * \
            gamma * np.max(q_next, axis=1)
        with tf.GradientTape() as tape:
            q = self.predict(states)
            q_train = np.copy(q)
            for i, action in enumerate(actions):
                q_train[i][action] = q_expected[i]

            loss = self.loss(q, q_train)
            grads = tape.gradient(loss, self.model.trainable_variables)
            self.optimizer.apply_gradients(
                zip(grads, self.model.trainable_variables))

class ReplayBuffer():
    def __init__(self, size=5000):
        self.buffer = []
        self.size = size

    def __len__(self):
        return len(self.buffer)

    def add(self, experiences):
        new_size = len(self.buffer) + len(experiences)
        if new_size > self.size:
            del self.buffer[0:new_size-self.size]
        self.buffer.extend(experiences)
        assert len(self.buffer) <= self.size

    def sample(self, n):
        return np.array(random.sample(self.buffer, n))

class DqnAgent():
    def __init__(self, env, state_dim, action_space):
        self.state_dim = state_dim
        self.action_space = action_space
        self.epsilon = 0.2
        self.gamma = 1.

        self.env = env
        self.q_network = QNetwork(state_dim)
        self.replay_buffer = ReplayBuffer()

    def get_action(self, state, explore=True):
        if explore and np.random.rand() < self.epsilon:
            return np.random.randint(self.action_space)
        q_values = self.q_network.predict(state)
        return np.argmax(q_values[0])

    def train(self, n_episodes, n_batch_size, t_train_interval):
        for t in trange(n_episodes, desc='Training', ncols=100):
            state = self.env.reset()
            terminal = False

            episode = []
            while not terminal:
                action = self.get_action(state)
                state_next, reward, terminal = self.env.step(action)
                episode.append(
                    np.array([state, action, reward, state_next, terminal]))
                state = state_next
            self.replay_buffer.add(episode)

            if t % t_train_interval == 0 and len(self.replay_buffer) >= n_batch_size:
                batch = self.replay_buffer.sample(n_batch_size)
                self.q_network.train(batch, self.gamma)

    def test(self, n_episodes):
        rewards = []
        for _ in trange(n_episodes, desc='Testing', ncols=100):
            state = self.env.reset()
            terminal = False
            cummulative_reward = 0

            while not terminal:
                action = self.get_action(state, explore=False)
                state, reward, terminal = self.env.step(action)
                cummulative_reward += reward

            rewards.append(cummulative_reward)
        return np.mean(rewards)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jul 31 11:51:02 2017

@author: David Dinkevich
"""

import matplotlib.pyplot as plt

# Create a 2-dimensional array of iris characteristics
def parse_data(lines, num_lines):
    # To store each characteristic of an iris
    data = [[], [], [], [], []]
    # For each line
    for i in range(num_lines):
        # Break up the line
        line = lines[i].split(',')
        # For each list in data
        for j in range(len(data)):
            # Add each component of the line to each
            # sub-array in data
            data[j].append(line[j])
    return data

with open('iris.csv') as f:
    # Store 2d array of iris characteristics
    flowers = parse_data(f.readlines(), 40)

plt.plot(flowers[0])

plt.xlabel('Flowers (index in dataset)')
plt.ylabel('Sepal Length')
plt.title('Sepal Length of Flowes in Iris')
plt.grid(True)
plt.savefig("graph.png")
plt.show()


# -*- coding: utf-8 -*-
import scrapy
from scrapy_splash import SplashRequest
from ..items import QunarCrawlItem
import re

class QunarFinSpider(scrapy.Spider):
    name = 'qunar_fin'
    allowed_domains = ['qunar.com']
    start_urls = ['https://train.qunar.com/stationToStation.htm?fromStation=%E4%B8%8A%E6%B5%B7&toStation=%E5%8C%97%E4%BA%AC&date=2017-11-11&drainage=']

    script = """
        function main(splash)
            assert(splash:go(splash.args.url))
            splash:wait(3)
            return splash:html()
        end
    """

    tackle_method = {
        1: 'self.get_train_code',
        2: 'self.get_start_end_station',
        3: 'self.get_start_end_time',
        4: 'self.get_duration',
        5: 'self.get_site_info',
        6: 'self.get_rest_tickets',
    }

    site_info = {
        1: ['sit_1', 'price_1', 'rest_ticket_1'],
        2: ['sit_2', 'price_2', 'rest_ticket_2'],
        3: ['sit_3', 'price_3', 'rest_ticket_3'],
        4: ['sit_4', 'price_4', 'rest_ticket_4'],
        5: ['sit_5', 'price_5', 'rest_ticket_5'],
    }

    link_pattern = re.compile(r'https://.*?drainage=')

    def start_requests(self):
        """处理处理url"""
        for url in self.start_urls:
            yield SplashRequest(url, self.parse_link, endpoint='execute',
                                args={'lua_source':self.script})

    def parse_link(self, response):
        link_list = self.link_pattern.finditer(response.body.decode())
        for link in link_list:
            link = re.sub(r'amp;', '', link.group())
            yield SplashRequest(link, self.parse_content, args={'lua_source': self.script},
                                endpoint='execute')

    def parse_content(self, response):
        """获取内容"""
        info_list = response.xpath('//ul[@class="tbody"]/li')
        date = re.search(r'(?<=date=)((.*)?)&', response.url).group(2)
        for info in info_list:
            item = QunarCrawlItem()
            item['date'] = date
            self.assignment(info, item)
            yield item

    def assignment(self, info, item):
        """任务分派"""
        task_list = info.xpath('./div/div')
        for index, task in enumerate(task_list[:6], 1):
            # self.get_train_code()
            eval(self.tackle_method[index])(task, item)

    def get_train_code(self, task, item):
        """获取火车号"""
        item['code'] = task.xpath('./h3/text()').extract_first().strip()

    def get_start_end_station(self, task, item):
        """获取起始站"""
        item['start_station'] = task.xpath('./p[@class="start"]/span/text()').extract_first().strip()
        item['end_station'] = task.xpath('./p[@class="end"]/span/text()').extract_first().strip()

    def get_start_end_time(self, task, item):
        """获取出发时间和到达时间"""
        item['start_time'] = task.xpath('./time[@class="startime"]/text()').extract_first().strip()
        item['end_time'] = task.xpath('./time[@class="endtime daytime"]/text()').extract_first().strip()

    def get_duration(self, task, item):
        """获取所需时间"""
        item['duration'] = task.xpath(r'./time/text()').extract_first()

    def get_site_info(self, task, item):
        """获取座位信息"""
        sit_info_list = task.xpath('./p')
        for index, sit_info in enumerate(sit_info_list, 1):
            which_site = self.site_info[index]
            item[which_site[0]] = sit_info.xpath('./text()').extract_first().strip()
            item[which_site[1]] = '¥' + sit_info.xpath('./span/text()').extract_first()

    def get_rest_tickets(self, task, item):
        """获取剩余票数"""
        ticket_info_list = task.xpath('./p')
        for index, ticket_info in enumerate(ticket_info_list, 1):
            which_site = self.site_info[index]
            try:
                item[which_site[2]] = ticket_info.xpath('./text()').extract()[0]
            except:
                item[which_site[2]] = None


'''
Created on Dec 5, 2013

@author: grmsjac6
'''
import unittest
from bioinfo.w4.EulerGraph import parseGraph,cycle,paths

class Test(unittest.TestCase):

    def testEulerCycle(self):
        lines =["0 -> 3",
                "1 -> 0",
                "2 -> 1,6",
                "3 -> 2",
                "4 -> 2",
                "5 -> 4",
                "6 -> 5,8",
                "7 -> 9",
                "8 -> 7",
                "9 -> 6"]
        graph,rev_graph = parseGraph(lines)
        euCycle = cycle(graph,rev_graph)
        sa = sum([len(a) for a in graph.values()])
        sb = sum([len(a) for a in rev_graph.values()])
        assert sa == sb == 0

    def skiptestGraphWithData(self):
        file = open('/home/giannis/Downloads/dataset_57_2.txt')
        data = list(file)
        file.close()
        lines = data
        graph,rev_graph = parseGraph(lines)
        euCycle = cycle(graph,rev_graph)

        sa = sum([len(a) for a in graph.values()])
        sb = sum([len(a) for a in rev_graph.values()])
        assert sa == sb == 0,"->".join(cycle(graph,rev_graph))

    def testPath(self):
        lines = ["0 -> 2",
                 "1 -> 3",
                 "2 -> 1",
                 "3 -> 0,4",
                 "6 -> 3,7",
                 "7 -> 8",
                 "8 -> 9",
                 "9 -> 6"]
        file = open('/home/giannis/Downloads/dataset_57_5(1).txt')
        data = list(file)
        file.close()
        lines = data#[2:-2]

        graph,rev_graph = parseGraph(lines)
        pp = sorted(paths(graph, rev_graph),key= lambda x:len(x),reverse=True)
if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testEulerCycle']
    unittest.main()


import json

from django.http import HttpResponse

from users.models import User

successResponseText = "УСПЕШНО: информация о Вас была сохранена на сервере"
failResponseText = "ОШИБКА: Вы ничего не прислали"

# сюда приходит информация из мобильного приложения Seffectle
def users(request):
    if request.method == 'GET':
        if not request.GET:
            # Если в Get запросе ничего не было прислано
            return HttpResponse(json.dumps(failResponseText, ensure_ascii=False))

        user_data = request.GET['data']

        user_data_dict = json.loads(user_data)
        print('Пришла такая инфа о юзере:')
        for key, value in user_data_dict.items():
            print('\t', key, ':', value)

        user_info = User()
        user_info.save_dict_to_class(user_data_dict)

        try:
            user_info = User.objects.get(vk_ID=user_info.vk_ID)
        except User.DoesNotExist:
            # если такого пользователя не было, то добавить в БД
            user_info.save()

        return HttpResponse(json.dumps(successResponseText, ensure_ascii=False))
    else:
        return HttpResponse("???")


from __future__ import print_function

import tensorflow as tf
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Conv2D, MaxPooling2D, Flatten
from keras.optimizers import RMSprop

import matplotlib
import matplotlib.pyplot as plt
import numpy as np

print('tensorflow:', tf.__version__)
print('keras:', keras.__version__)

#load (first download if necessary) the MNIST dataset
# (the dataset is stored in your home direcoty in ~/.keras/datasets/mnist.npz
#  and will take  ~11MB)
# data is already split in train and test datasets
(x_train, y_train), (x_test, y_test) = mnist.load_data()

# x_train : 60000 images of size 28x28, i.e., x_train.shape = (60000, 28, 28)
# y_train : 60000 labels (from 0 to 9)
# x_test  : 10000 images of size 28x28, i.e., x_test.shape = (10000, 28, 28)
# x_test  : 10000 labels
# all datasets are of type uint8

#To input our values in our network Conv2D layer, we need to reshape the datasets, i.e.,
# pass from (60000, 28, 28) to (60000, 28, 28, 1) where 1 is the number of channels of our images
img_rows, img_cols = x_train.shape[1], x_train.shape[2]
x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)

#Convert to float
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')

#Normalize inputs from [0; 255] to [0; 1]
x_train = x_train / 255
x_test = x_test / 255

print('x_train.shape=', x_train.shape)
print('x_test.shape=', x_test.shape)
print('y_train.shape=', y_train.shape)
print('y_test.shape=', y_test.shape)

num_classes = 10
epochs = 1
batch_size = 64
filters = 64

#Convert class vectors to binary class matrices ("one hot encoding")
## Doc : https://keras.io/utils/#to_categorical
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
# num_classes is computed automatically here
# but it is dangerous if y_test has not all the classes
# It would be better to pass num_classes=np.max(y_train)+1

#Let start our work: creating a convolutional neural network

# creation du modele
model = Sequential()

# ajout des couches
model.add(Conv2D(filters, kernel_size=(3,3), strides=1, padding='same', activation='relu'))
model.add(Conv2D(filters, kernel_size=(3,3), strides=1, padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.3))
model.add(Flatten())
model.add(Dense(num_classes, activation='softmax'))

# compiler le modele
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) # multiclass

# entrainer le reseau
hist = model.fit(x_train, y_train, epochs=epochs, batch_size=batch_size, validation_data=(x_test, y_test))

# evaluer le modele
score = model.evaluate(x_test, y_test)
print('Score apres' , epochs, 'epochs =', score)

plt.plot(hist.history['loss'])
plt.plot(hist.history['val_loss'])
plt.title('model loss')
plt.legend(['train', 'test'], loc='upper left')
plt.show()

# utilisation du modele
y = model.predict(x_test)

# comparaison des resultats uniquement sur la classe valide
y_err1 = y * y_test

# comparaison des resultats sur les 10 classes
y_err2 = np.absolute(y - y_test);

# sommes des resultats des comparaisons
y_err1 = np.sum(y_err1, axis=1)
y_err2 = np.sum(y_err2, axis=1)

# tris des comparaisons
y_err1_sorted = np.argsort(y_err1)
y_err2_sorted = np.argsort(y_err2)

# Affichage des 10 images les moins bien classees

for i in range(10) :
  plt.imshow(x_test[y_err1_sorted[i],:].reshape(28, 28), cmap = matplotlib.cm.binary)
  plt.axis("off")
  plt.show()


#!/usr/bin/env python3
import operator
HAPROXY_CONFIGS="./"

HAPROXY_TEMPLATE="{}/haproxy.cfg.template-http".format(HAPROXY_CONFIGS)

LB_CONFIG_FRONTEND_HOST="""
	acl host_{server_name_formatted} hdr(host) -i {server_name}"""
LB_CONFIG_FRONTEND_ACL_PATH="""
	acl {upstream_name}_path path_beg -i {server_path}"""
LB_CONFIG_FRONTEND_TO_BACKEND="""
	use_backend {upstream_name} if host_{server_name_formatted} {upstream_name}_path"""

LB_CONFIG_BACKEND="""

backend {upstream_name}
	# Stick on source
	stick-table type string len 48 size 5000k expire 10m store conn_cur
	stick on src
    # Backends"""

LB_CONFIG_BACKEND_SERVER="""
    server {upstream_name}_{id} {upstream_server} {ssl} cookie c_{upstream_name}_{id} check"""

LB_CONFIG_BACKEND_CUT_PATH="""
    http-request replace-path /{server_path_without_slash}[/]?(.*) /\\1"""

LB_CONFIG_SSL_VERIFY_NONE="ssl verify none"
LB_CONFIG_SSL="ssl"

COMMENT="""
# Reverse proxy configurations are automatically generated under this line. Do not modify them by hand.
#
# Tip: Use labels for Docker services instead.
#      For example: drouter.host="www.domain.com" drouter.path="/backend"
"""

class Files():
    def __init__(self):
        pass
    def write(self, filepath, data):
        try:
            f=open(filepath, "a")
            f.write(data)
            f.close()
            return True
        except:
            return False

    def overwrite(self, filepath, data):
        try:
            f=open(filepath, "w")
            f.write(data)
            f.close()
            return True
        except:
            return False
    def read(self, filepath):
        try:
            f=open(filepath, "r")
            data=f.read()
            f.close()
            return data
        except:
            return False

class Service():
    def __init__(self, service_id, service_name, upstream_servers, domain, path, port, ssl=None, cut_path=None):
        self.id=service_id
        self.service_name=service_name
        self.upstream_servers=upstream_servers
        self.domain=domain
        self.haproxy_host=self.domain.replace(".", "_")
        self.path=path
        self.port=port
        self.ssl=None
        if ssl == "noverify":
            self.ssl=LB_CONFIG_SSL_VERIFY_NONE
        elif ssl == "verify":
            self.ssl=LB_CONFIG_SSL
        elif ssl == None:
            self.ssl=""
        self.cut_path=None
        if not cut_path == None:
            if self.path.startswith("/"):
                self.cut_path=self.path[1:]
            if self.cut_path.endswith("/"):
                self.cut_path=self.path[:-1]
class Config():
    def __init__(self):
        self.domains={}

    def arrangeConfigs(self, services):
        services.sort(key=operator.attrgetter("path"), reverse=True)
        return services

    def createLBConfig(self, srv):

        frontend_host=LB_CONFIG_FRONTEND_HOST.format(server_name_formatted=srv.haproxy_host, server_name=srv.domain)
        frontend_acl_path=LB_CONFIG_FRONTEND_ACL_PATH.format(upstream_name=srv.service_name, server_path=srv.path)
        frontend_to_backend=LB_CONFIG_FRONTEND_TO_BACKEND.format(upstream_name=srv.service_name, server_name_formatted=srv.haproxy_host)

        def createBackendConfigs():
            backend_configs=[]
            backend=LB_CONFIG_BACKEND.format(upstream_name=srv.service_name)
            backend_configs.append(backend)
            if not srv.cut_path == None:
                cut_path_conf = LB_CONFIG_BACKEND_CUT_PATH.format(server_path_without_slash=srv.cut_path)
                backend_configs.append(cut_path_conf)
            for s in srv.upstream_servers:
                server=LB_CONFIG_BACKEND_SERVER.format(upstream_name=srv.service_name, id=srv.upstream_servers.index(s), upstream_server=s, ssl=srv.ssl)
                backend_configs.append(server)
            return backend_configs

        try:
            if self.domains[srv.domain]:
                self.domains[srv.domain]["frontend_acl_path"].append(frontend_acl_path)
                self.domains[srv.domain]["frontend_to_backend"].append(frontend_to_backend)
                self.domains[srv.domain]["backend_configs"].append(createBackendConfigs())
        except:
                self.domains[srv.domain] = {
                    "frontend_host": frontend_host
                }
                self.domains[srv.domain]["frontend_acl_path"] = []
                self.domains[srv.domain]["frontend_acl_path"].append(frontend_acl_path)
                self.domains[srv.domain]["frontend_to_backend"] = []
                self.domains[srv.domain]["frontend_to_backend"].append(frontend_to_backend)
                self.domains[srv.domain]["backend_configs"] = []
                self.domains[srv.domain]["backend_configs"].append(createBackendConfigs())

    def writeConfigs(self):
        files=Files()
        FPATH="{}/haproxy.cfg".format(HAPROXY_CONFIGS)
        files.overwrite(FPATH, COMMENT)
        files.write(FPATH, files.read("{}".format(HAPROXY_TEMPLATE)))
        data=""
        for x in self.domains:
            data=data+self.domains[x].get("frontend_host")
            for path in self.domains[x].get("frontend_acl_path"):
                data=data+path
            for clause in self.domains[x].get("frontend_to_backend"):
                data=data+clause

        for x in self.domains:
            for backend in self.domains[x].get("backend_configs"):
                for l in backend:
                    data=data+l
        files.write(FPATH, data)
# ## Testing
if __name__=="__main__":
    print(LB_CONFIG_BACKEND_CUT_PATH)


import torch
from abc import ABC, abstractmethod

class BaseModelConstructor:
    """
    This class is an abstract base class (ABC) for models
    """
    def __init__(self, opts):
        self.opts = opts
        self.model_name = opts.model_name
        self.model_object = opts.model_object
        self.input_size = opts.input_size

    def __str__(self):
        return str(self.opts)

    @abstractmethod
    def create(self, num_classes):
        """
        gather the informations from self and return the model
        :return: model object
        """
        pass

    @staticmethod
    def load_checkpoint(model, checkpoint):
        """
        :param model: pytorch model object
        :param checkpoint: checkpoint path
        :return: model.
        """
        checkpoint = torch.load(checkpoint, map_location="cpu")

        # Check the checkpoint object is Dataparallel module or not
        checkpoint_keys = [key for key in checkpoint.keys()]

        if "module" == checkpoint_keys[0][:6]:
            # If Dataparallel module use,
            # the model architecture and weights are registered as
            # "module" instance of the Dataparalle module.
            print("{} was saved as torch.nn.Dataparallel module.")
            for key in checkpoint_keys:
                checkpoint[key[7:]] = checkpoint.pop(key)

        model.load_state_dict(checkpoint)
        return model

def check_model_information(model_constructor_object):
    """
    Check the where modules models constructor objects come from.
    :param model_constructor_object
    :return: <str>
    # Cadene's pretrained-models
        # https://github.com/Cadene/pretrained-models.pytorch
        # Cadene's modules supports imagenet when defining function.
        # pretrained argument must be "imagenet"
    # Torchvision model
        # pretrained argument must be False or True.
        "spark" : models comes from spark.
        pretrained argument at SLMCModelConsturctor must be imagenet or pth path.

        "unknow" :  Unknown or User-defined Model.
                    Only supports on pth weight file.
    """
    package_source = model_constructor_object.__globals__["__package__"].split(".")[0]
    if package_source == "pretrainedmodels":
        return "pretrainedmodels"
    elif package_source == "torchvision":
        return "torchvision"
    elif package_source == "spark":
        return "spark"
    else:
        return "unknown"


#**********************************************************************
# Copyright 2020 Advanced Micro Devices, Inc
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#********************************************************************
import MaterialX as mx

import bpy
from bpy_extras.io_utils import ExportHelper

from . import HdUSD_Panel, HdUSD_ChildPanel, HdUSD_Operator
from ..mx_nodes.node_tree import MxNodeTree

class HDUSD_MATERIAL_PT_context(HdUSD_Panel):
    bl_label = ""
    bl_context = "material"
    bl_options = {'HIDE_HEADER'}

    @classmethod
    def poll(cls, context):
        if context.active_object and context.active_object.type == 'GPENCIL':
            return False
        else:
            return (context.material or context.object) and super().poll(context)

    def draw(self, context):
        layout = self.layout

        material = context.material
        object = context.object
        slot = context.material_slot
        space = context.space_data

        if object:
            is_sortable = len(object.material_slots) > 1
            rows = 1
            if is_sortable:
                rows = 4

            row = layout.row()

            row.template_list("MATERIAL_UL_matslots", "", object, "material_slots", object,
                              "active_material_index", rows=rows)

            col = row.column(align=True)
            col.operator("object.material_slot_add", icon='ADD', text="")
            col.operator("object.material_slot_remove", icon='REMOVE', text="")

            col.menu("MATERIAL_MT_context_menu", icon='DOWNARROW_HLT', text="")

            if is_sortable:
                col.separator()

                col.operator("object.material_slot_move", icon='TRIA_UP', text="").direction = 'UP'
                col.operator("object.material_slot_move", icon='TRIA_DOWN', text="").direction = 'DOWN'

            if object.mode == 'EDIT':
                row = layout.row(align=True)
                row.operator("object.material_slot_assign", text="Assign")
                row.operator("object.material_slot_select", text="Select")
                row.operator("object.material_slot_deselect", text="Deselect")

        split = layout.split(factor=0.65)

        if object:
            split.template_ID(object, "active_material", new="material.new")
            row = split.row()

            if slot:
                row.prop(slot, "link", text="")
            else:
                row.label()
        elif material:
            split.template_ID(space, "pin_id")
            split.separator()

class HDUSD_MATERIAL_PT_preview(HdUSD_Panel):
    bl_label = "Preview"
    bl_context = "material"
    bl_options = {'DEFAULT_CLOSED'}

    @classmethod
    def poll(cls, context):
        return context.material and super().poll(context)

    def draw(self, context):
        self.layout.template_preview(context.material)

class HDUSD_MATERIAL_OP_new_mx_node_tree(bpy.types.Operator):
    """Create new MaterialX node tree for selected material"""
    bl_idname = "hdusd.material_new_mx_node_tree"
    bl_label = "New"

    def execute(self, context):
        mat = context.material
        mx_node_tree = bpy.data.node_groups.new(f"MX_{mat.name}", type=MxNodeTree.bl_idname)
        mx_node_tree.create_basic_nodes(
            'RPR_rpr_uberv2' if context.scene.hdusd.use_rpr_mx_nodes else 'PBR_standard_surface')

        mat.hdusd.mx_node_tree = mx_node_tree
        return {"FINISHED"}

class HDUSD_MATERIAL_OP_link_mx_node_tree(bpy.types.Operator):
    """Link MaterialX node tree to selected material"""
    bl_idname = "hdusd.material_link_mx_node_tree"
    bl_label = ""

    mx_node_tree_name: bpy.props.StringProperty(default="")

    def execute(self, context):
        context.material.hdusd.mx_node_tree = bpy.data.node_groups[self.mx_node_tree_name]
        return {"FINISHED"}

class HDUSD_MATERIAL_OP_unlink_mx_node_tree(bpy.types.Operator):
    """Unlink MaterialX node tree from selected material"""
    bl_idname = "hdusd.material_unlink_mx_node_tree"
    bl_label = ""

    def execute(self, context):
        context.material.hdusd.mx_node_tree = None
        return {"FINISHED"}

class HDUSD_MATERIAL_MT_mx_node_tree(bpy.types.Menu):
    bl_idname = "HDUSD_MATERIAL_MT_mx_node_tree"
    bl_label = "MX Nodetree"

    def draw(self, context):
        layout = self.layout
        node_groups = bpy.data.node_groups

        for ng in node_groups:
            if ng.bl_idname != 'hdusd.MxNodeTree':
                continue

            row = layout.row()
            row.enabled = bool(ng.output_node)
            op = row.operator(HDUSD_MATERIAL_OP_link_mx_node_tree.bl_idname,
                              text=ng.name, icon='MATERIAL')
            op.mx_node_tree_name = ng.name

class HDUSD_MATERIAL_PT_material(HdUSD_Panel):
    bl_label = ""
    bl_context = "material"

    @classmethod
    def poll(cls, context):
        return context.material and super().poll(context)

    def draw(self, context):
        mat_hdusd = context.material.hdusd
        layout = self.layout

        split = layout.row(align=True).split(factor=0.4)
        col = split.column()
        col.alignment = 'RIGHT'
        col.label(text="MaterialX")
        col = split.column()
        row = col.row(align=True)
        col1 = row.column()
        col1.enabled = any(ng.bl_idname == 'hdusd.MxNodeTree' for ng in bpy.data.node_groups)
        col1.menu(HDUSD_MATERIAL_MT_mx_node_tree.bl_idname, text="", icon='MATERIAL')

        if mat_hdusd.mx_node_tree:
            row.prop(mat_hdusd.mx_node_tree, 'name', text="")
            row.operator(HDUSD_MATERIAL_OP_unlink_mx_node_tree.bl_idname, icon='X')

        else:
            row.operator(HDUSD_MATERIAL_OP_new_mx_node_tree.bl_idname, icon='ADD')

    def draw_header(self, context):
        layout = self.layout
        layout.label(text=f"Material: {context.material.name}")

class HDUSD_MATERIAL_PT_output_node(HdUSD_ChildPanel):
    bl_label = ""
    bl_parent_id = 'HDUSD_MATERIAL_PT_material'

    @classmethod
    def poll(cls, context):
        return not bool(context.material.hdusd.mx_node_tree)

    def draw(self, context):
        layout = self.layout

        node_tree = context.material.node_tree

        output_node = context.material.hdusd.output_node
        if not output_node:
            layout.label(text="No output node")
            return

        input = output_node.inputs[self.bl_label]
        layout.template_node_view(node_tree, output_node, input)

class HDUSD_MATERIAL_PT_output_surface(HDUSD_MATERIAL_PT_output_node):
    bl_label = "Surface"

class HDUSD_MATERIAL_PT_output_displacement(HDUSD_MATERIAL_PT_output_node):
    bl_label = "Displacement"
    bl_options = {'DEFAULT_CLOSED'}

class HDUSD_MATERIAL_PT_output_volume(HDUSD_MATERIAL_PT_output_node):
    bl_label = "Volume"
    bl_options = {'DEFAULT_CLOSED'}

class HDUSD_MATERIAL_OP_export_mx_file(HdUSD_Operator, ExportHelper):
    bl_idname = "hdusd.material_export_mx_file"
    bl_label = "MaterialX Export to File"
    bl_description = "Export material as MaterialX node tree to .mtlx file"

    filename_ext = ".mtlx"
    filepath: bpy.props.StringProperty(
        name="File Path",
        description="File path used for exporting material as MaterialX node tree to .mtlx file",
        maxlen=1024, subtype="FILE_PATH"
    )
    filter_glob: bpy.props.StringProperty(default="*.mtlx", options={'HIDDEN'}, )

    def execute(self, context):
        doc = context.material.hdusd.export(context.object)
        if not doc:
            return {'CANCELLED'}

        mx.writeToXmlFile(doc, self.filepath)
        return {'FINISHED'}

class HDUSD_MATERIAL_OP_export_mx_console(HdUSD_Operator):
    bl_idname = "hdusd.material_export_mx_console"
    bl_label = "MaterialX Export to Console"
    bl_description = "Export material as MaterialX node tree to console"

    def execute(self, context):
        doc = context.material.hdusd.export(context.object)
        if not doc:
            return {'CANCELLED'}

        print(mx.writeToXmlString(doc))
        return {'FINISHED'}

class HDUSD_MATERIAL_PT_export_mx(HdUSD_Panel):
    bl_label = "MaterialX Export"
    bl_space_type = "NODE_EDITOR"
    bl_region_type = "UI"
    bl_category = "Tool"

    @classmethod
    def poll(cls, context):
        tree = context.space_data.edit_tree
        return super().poll(context) and tree and \
               tree.bl_idname == bpy.types.ShaderNodeTree.__name__

    def draw(self, context):
        layout = self.layout

        layout.operator(HDUSD_MATERIAL_OP_export_mx_file.bl_idname)
        layout.operator(HDUSD_MATERIAL_OP_export_mx_console.bl_idname)

def depsgraph_update(depsgraph):
    context = bpy.context
    mx_node_tree = None
    if context.object and context.object.active_material:
        mx_node_tree = context.object.active_material.hdusd.mx_node_tree

    # trying to show MaterialX area with node tree or Shader area
    screen = context.screen
    if mx_node_tree:
        for area in screen.areas:
            if area.ui_type not in ('hdusd.MxNodeTree', 'ShaderNodeTree'):
                continue

            area.ui_type = 'hdusd.MxNodeTree'
            space = next(s for s in area.spaces if s.type == 'NODE_EDITOR')
            space.node_tree = mx_node_tree

    else:
        for area in screen.areas:
            if area.ui_type != 'hdusd.MxNodeTree':
                continue

            area.ui_type = 'ShaderNodeTree'


import os
from cffi import FFI

ffibuilder = FFI()

ffibuilder.set_source(
    "grblas.backends.suitesparse._suitesparse_grblas",
    r"""#include "GraphBLAS.h" """,
    libraries=["graphblas"],
)

thisdir = os.path.dirname(__file__)

gb_cdef = open(os.path.join(thisdir, "suitesparse_graphblas_3.3.3.h"))

ffibuilder.cdef(gb_cdef.read())

if __name__ == "__main__":
    ffibuilder.compile(verbose=True)


import os
import cherrypy
import mako.lookup

class TemplateTool(cherrypy.Tool):

    _engine = None
    '''Mako lookup instance'''

    def __init__(self, path):
        viewPath = os.path.join(path, 'static')
        self._engine = mako.lookup.TemplateLookup(directories=[viewPath])

        cherrypy.Tool.__init__(self, 'before_handler', self.render)

    def __call__(self, *args, **kwargs):
        if args and isinstance(args[0], (types.FunctionType, types.MethodType)):
            # @template
            args[0].exposed = True
            return cherrypy.Tool.__call__(self, **kwargs)(args[0])
        else:
            # @template()
            def wrap(f):
                f.exposed = True
                return cherrypy.Tool.__call__(self, *args, **kwargs)(f)
            return wrap

    def render(self, name=None):
        cherrypy.request.config['template'] = name

        handler = cherrypy.serving.request.handler

        def wrap(*args, **kwargs):
            return self._render(handler, *args, **kwargs)
        cherrypy.serving.request.handler = wrap

    def _render(self, handler, *args, **kwargs):
        template = cherrypy.request.config['template']
        if not template:
            parts = []
            if hasattr(handler.callable, '__self__'):
                parts.append(handler.callable.__self__.__class__.__name__.lower())
            if hasattr(handler.callable, '__name__'):
                parts.append(handler.callable.__name__.lower())
            template = '/'.join(parts)

        data = handler(*args, **kwargs) or {}
        renderer = self._engine.get_template('{}'.format(template))

        return renderer.render(**data)


# By: Riasat Ullah
# This file contains the variables that will be used for testing.

from objects.assignee import Assignee
from objects.events import AcknowledgeEvent, DispatchEvent, EscalateEvent
from objects.group import Group
from objects.group_level import GroupLevel
from objects.group_rotation import GroupRotation
from objects.group_routine import GroupRoutine
from objects.instance_state import InstanceState
from objects.policy import GroupPolicy, UserPolicy
from objects.task import Task
from objects.user import User
from utils import constants, times, var_names
import datetime

# User variables
correct_username = 'testuser_one'
correct_first_name = 'John'
correct_last_name = 'Smith'
correct_email = 'john.smith@gmail.com'
correct_country_code = 1
correct_phone = '3152624399'
correct_birth_year = 1994
correct_birth_month = 5
correct_birth_day = 17
correct_verified = True
correct_account_type = constants.personal
correct_policy_id = 3

correct_user = User(correct_username, correct_first_name, correct_last_name,
                    correct_email, correct_country_code, correct_phone,
                    correct_birth_year, correct_birth_month, correct_birth_day,
                    correct_verified, correct_account_type, correct_policy_id)

correct_user_2 = User(correct_username, correct_first_name, correct_last_name,
                      correct_email, correct_country_code, correct_phone,
                      correct_birth_year, correct_birth_month, correct_birth_day,
                      correct_verified, correct_account_type, 99)

correct_user_3 = User('testuser_two', 'Robert', 'Brown', 'robert.brown@gmail.com',
                      1, '5163429878', 1992, 2, 24, True, constants.personal, 8)

# Group variables
correct_group_id = 2
correct_group_policy_id = 3
correct_group_name = 'Test Group 1'
correct_group_email = None
correct_routine_id = 11
correct_routine_name = 'Test Routine 1'
correct_valid_start = datetime.datetime.strptime('2018-10-01 00:00:00', constants.timestamp_format)
correct_valid_end = datetime.datetime.strptime('9999-01-01 00:00:00', constants.timestamp_format)

correct_routine_start = datetime.datetime.strptime('8:0', constants.time_format).time()
correct_routine_end = datetime.datetime.strptime('12:0', constants.time_format).time()
incorrect_routine_end = datetime.datetime.strptime('7:0', constants.time_format).time()

correct_group_rotation_1 = GroupRotation(correct_routine_id, 1, 1, 7, correct_user.username)
correct_group_rotation_2 = GroupRotation(correct_routine_id, 1, 8, 14, correct_user_2.username)
correct_group_level = GroupLevel(correct_routine_id, 1, 15, 14, 7, [correct_group_rotation_1, correct_group_rotation_2])
correct_group_routine = GroupRoutine(correct_group_id, correct_routine_id, correct_routine_name,
                                     correct_valid_start, correct_valid_end,
                                     correct_routine_start, correct_routine_end,
                                     [correct_group_level], False)

correct_admin_name = 'testuser_one'
invalid_admin_name = 'testuser_eight'
correct_group = Group(correct_group_id, correct_group_policy_id, correct_group_name, correct_group_email,
                      [correct_group_routine], [], [correct_admin_name])

# Policy variables
test_user_policy_1 = UserPolicy(3, correct_user)
test_user_policy_2 = UserPolicy(8, correct_user_3)
test_group_policy = GroupPolicy(3, correct_group)

# Assignee variables
test_user_assignee_1 = Assignee(correct_user.username, test_user_policy_1, 1, 15)
test_user_assignee_2 = Assignee(correct_user_3.username, test_user_policy_2, 2, 15)
test_group_assignee_1 = Assignee(correct_user.username, test_group_policy, 1, 15)

# Task variable
test_task_details = {var_names.created_by: correct_user.username,
                     var_names.task_title: 'Test task title 1',
                     var_names.task_timezone: 'US/Eastern',
                     var_names.task_hour: 8,
                     var_names.task_minute: 12,
                     var_names.repeat: [constants.monday, constants.thursday],
                     var_names.text_msg: 'test task 1',
                     var_names.voice_msg_location: '',
                     var_names.video_msg_location: '',
                     var_names.with_completion_notice: True,
                     var_names.require_all_assignees_response: False,
                     var_names.urgency_level: constants.low_urgency}
test_task_assignees = [test_user_assignee_1, test_user_assignee_2]
test_task = Task(3, test_task_details, test_task_assignees)

# Instance and Event variables
test_instance_id = 43
test_instance_timestamp = times.get_current_timestamp()
test_instance_next_alert = test_instance_timestamp + datetime.timedelta(9)

test_acknowledge_event_timestamp = test_instance_timestamp - datetime.timedelta(9)
test_acknowledge_event = AcknowledgeEvent(test_instance_id, test_acknowledge_event_timestamp,
                                          constants.app, test_user_assignee_2.assignee_name)

test_dispatch_event_timestamp = test_instance_timestamp - datetime.timedelta(20)
test_dispatch_event_to = [(test_user_assignee_1.assignee_name, test_user_policy_1.policy_id)]
test_dispatch_event_next_alert = test_instance_timestamp + datetime.timedelta(10)
test_dispatch_event = DispatchEvent(test_instance_id, test_dispatch_event_timestamp,
                                    constants.app, test_dispatch_event_to,
                                    test_dispatch_event_next_alert)

test_escalate_event_timestamp = test_instance_timestamp - datetime.timedelta(10)
test_escalate_event = EscalateEvent(test_instance_id, test_escalate_event_timestamp,
                                    constants.app, constants.internal, 2)

test_instance_state_1 = InstanceState(test_instance_id, test_instance_timestamp, 2, constants.app,
                                      test_instance_timestamp, test_instance_next_alert,
                                      constants.acknowledged_state, [test_user_assignee_2],
                                      test_task, [test_dispatch_event, test_escalate_event, test_acknowledge_event])


from flask import Blueprint, jsonify
from flask import current_app as app

blueprint_api = Blueprint('doggy_api', __name__)

@blueprint_api.route('/list', methods=['GET'])
def get_list():
    data = [{ 'name': 'dog_aaa' }, { 'name': 'dog_bbb' }]
    app.logger.info('API_list called, will return %d objects', len(data))
    return jsonify(data)

@blueprint_api.route('/count', methods=['GET'])
def get_count():
    data = { 'count': 10 }
    return jsonify(data)


import os, sys, getopt
import json
import datetime
from shutil import rmtree
import string
import scipy.stats as stat
import numpy as np
from dateutil.relativedelta import *
from calendar import monthrange
from geotiff import *
from lmoments3 import distr

import matplotlib.pylab as plt
import matplotlib as mpl

def simple_plot_index(a,title="", save_img=False,save_dir='.'):
    fig, ax = plt.subplots()
    #cmap='RdBu'
    cmap = mpl.colors.LinearSegmentedColormap.from_list("", ["red","orange","yellow","white","pink","violet", "#0f0f0f"])
    plt.title(title)
    im = plt.imshow(a, interpolation='none',cmap=cmap)
    plt.colormaps()
    # cbar = fig.colorbar(cax,ticks=[-1, 0, 1, 2, 10],orientation='vertical')
    cbar = fig.colorbar(im, orientation='vertical')
    #plt.clim(-3,3)
    # cbar.ax.set_yticklabels(['< -1', '0', 1, 2,'> 10'])# vertically oriented
    # colorbar
    #ax.format_coord = Formatter(im)
    if save_img:
        plt.savefig(os.path.join(save_dir,title+'.png'))
    else:
        plt.show()

def displayHelp():

    print ('\nOptions available:')
    print ('          -n | --nostats                do not update statistics ')
    print ('          -h | --help                   display this help')

def get_all_dates(foldAdd, nameIndex):
    # remove path separator from end of folder address
    if foldAdd[-1] == os.path.sep:
        foldAdd = foldAdd[:-1]

    # get list of all files
    allFiles = [x[2] for x in os.walk(foldAdd)]

    # get dates from file names
    count = len(nameIndex)
    result = []
    for str in allFiles:
        if len(str) > 0:
            for stf in str:
                if nameIndex in stf:
                    date=stf.split('_')[1].split(".")[0]
                    #date = stf[count+1:count+9]
                     # print(date)
                    result.append(date)

    result.sort()
    return result

def get_date(dateStr):
    year = int(dateStr[0:4])
    month = int(dateStr[4:6])
    day = int(dateStr[6:8])
    return year, month, day

def diff_date_str(curDate, refDate):
    dateVal = datetime.datetime.strptime(curDate, "%Y%m%d").date()
    refDateVal = datetime.datetime.strptime(refDate, "%Y%m%d").date()
    return (dateVal - refDateVal).days

def convert_to_monthly_data (VARfold, VARprefix, indexFold, monthCount, maskFile):

    dateList = get_all_dates(VARfold, VARprefix)
    dateList.sort()

    dateListMonths = [i[0:6] for i in dateList ]

    oDateFrom = datetime.datetime.strptime(dateList[0],"%Y%m%d")
    oDateTo =   datetime.datetime.strptime(dateList[-1],"%Y%m%d")

    #in case month was not completed
    oDateTo = oDateTo.replace(day=1) + relativedelta(months=+1) + relativedelta(days=-1)

    print(oDateFrom.strftime("%Y%m%d"))
    print(oDateTo.strftime("%Y%m%d"))

    #oDateFrom = oDateFrom + relativedelta(months=monthCount)
    oDate = oDateFrom
    while (oDate <= oDateTo):

        if oDate < oDateFrom + relativedelta(months=monthCount-1):
            # remove the first monthCount from dateList
            #dateList = [s for s in dateList if oDate.strftime("%Y%m") not in s]
            oDate = oDate +relativedelta(months=+1)
            continue

        if oDate.strftime("%Y%m")  in dateListMonths:

            dateInMonth = []

            for m in range(monthCount):

                for date in dateList:

                    oDateAggr=oDate + relativedelta(months = -m)

                    year = oDateAggr.strftime("%Y")
                    month = oDateAggr.strftime("%m")

                    if  year+month in date[0:6]:

                        dateInMonth.append(date)

            if len(dateInMonth)>0:

                calculate_monthly_average(indexFold, VARfold, VARprefix, dateInMonth, oDate.strftime("%Y"), oDate.strftime("%m"), monthCount, maskFile)

            #dateListMonths.append(oDate.strftime("%Y%m"))
        else:
            print ("Missing data for : "+ oDate.strftime("%Y%m"))

        oDate = oDate +relativedelta(months=+1)

def calculate_monthly_average(indexFold, VARfold, VARprefix, dateInMonth, year, month, monthCount, maskFile):

    mask , col, row, geoTrans, geoproj = readGeotiff (maskFile)

    data3d = np.zeros((row, col, len (dateInMonth)))

    for i, key in enumerate(dateInMonth):
        #daFold = os.path.join(foldAdd, key[0:4], key[4:6], key[6:8])
        fileName = os.path.join(VARfold, key[0:4], key[4:6], key[6:8],VARprefix + "_" + key + ".tif")
        data , col, row, geoTrans, geoproj = readGeotiff (fileName)
        #simple_plot_index(data,key)
        data3d [:,:,i] = data

    data3d[data3d<1]=0
    data_mean = np.nanmean(data3d,axis=2)
    BandName="Prec_accumulation"

    os.system("mkdir -p "+os.path.join(indexFold,str(monthCount)+"-Month-Files"))

    outFileName= os.path.join(indexFold,str(monthCount)+"-Month-Files", VARprefix + "_" +year+month+".tif".format(year, month))

    #simple_plot_index(data_mean)
    print (outFileName)

    writeGeotiffSingleBand(outFileName, geoTrans, geoproj, data_mean,nodata=np.nan, BandName=BandName)

def create_statistics(mainFoldAdd, newFold, nameIndex, monthCount, maskFile):
    dateList = get_all_dates(mainFoldAdd, nameIndex)
    dateList.sort()
#    newFold = os.path.join(mainFoldAdd, "Normal PDF")
    if not(os.path.isdir(newFold)):
        os.system ("mkdir -p "+newFold)

    for month in range(1, 13):
        fileNames = []
        for str in dateList:
            yy = int(str[0:4])
            mm = int(str[4:6])
            if mm == month:
                fileNames.append(nameIndex + "_" + str + ".tif")
        compute_statistics (mainFoldAdd, fileNames, newFold, monthCount, month,nameIndex, maskFile)

def compute_statistics(mainFoldAdd, fileNames, newFold, monthCount, month,nameIndex, maskFile):
    print("Create statistics GeoTiff for {:d} months from month {:02d}".
          format(monthCount, month))

    mask, col, row, geoTrans, geoproj=  readGeotiff(maskFile)

    #data , col, row, geoTrans, geoproj = readGeotiff ("GSMAP_20200501.tif")

    data3d = np.zeros((row,col,len(fileNames)))

    for i, f in enumerate(fileNames):

        data , col, row, geoTrans, geoproj = readGeotiff (os.path.join(mainFoldAdd,f))

        data3d [:,:,i]= data

    distr_param =np.zeros((row,col,4))*np.nan

    invalid_pixels = 0

    for i in range(row):
        for j in range(col):

            if sum(np.isnan(data3d[i,j,:])) ==len(fileNames):
                continue

            d1d = data3d[i,j,:]
            dpd = d1d[np.where(d1d > 0)]  # only non null values
            zeros = d1d[d1d==0]
            if len(dpd) < 4:
                print ("Not enough valid values in time series, impossible fitting!")
                continue

            fit_dict = distr.gam.lmom_fit(dpd)
            fit = (fit_dict['a'],fit_dict['loc'],fit_dict['scale'])

            max_distance, p_value = stat.kstest(dpd,"gamma",args=fit)
            if p_value < 0.5:
                invalid_pixels += 1

                continue
                #print("Kolmogorov-Smirnov test for goodness of fit: "+str(round(p_value*100))+"%, max distance: "+str(max_distance))

            distr_param[i,j,0] = fit[0]
            distr_param[i,j,1] = fit[1]
            distr_param[i,j,2] = fit[2]
            distr_param[i,j,3] = len(zeros) / len(d1d)    # zero probability

    print ("Invalid pixel: " + str(round(invalid_pixels/(row*col)*100))+"%")
    name = "Statistics-"+ nameIndex +"-{:02d}months-Month{:02d}.tif".format(monthCount, month)
    fileStatsOut = os.path.join(newFold, name)

    writeGeotiff(fileStatsOut,geoTrans, geoproj,distr_param, nodata=np.nan, BandNames=list(fit_dict.keys()).append("P0")
                 ,globalDescr = "SPI_distr_param_c_loc_scale_P0")

def main(argv):
    with open('SPI_config.json') as jf:
        params = json.load(jf)

        # get FAPARFold tif files

        PrecFold = params["Prec_folder"]
        if PrecFold[-1] == os.path.sep:
            PrecFold = PrecFold[:-1]

        IndexFold = params["Index_folder"]
        if IndexFold[-1] == os.path.sep:
            IndexFold = PrecFold[:-1]

        PrecNameIndex = params["Prec_prefix"]

        maskFile = params["mask_file"]

        statsFold = params["Stats_folder"]

        aggMonths = params["Agg_months"]

        PrecmonthlyFold = os.path.join(IndexFold,PrecNameIndex+"-Monthly-Files")

        stats = True

        opts, a1Args = getopt.getopt(argv,"hn",["help","nostats"])

        for opt, arg in opts:
            if opt in ("-n", "--nostats"):
                stats = False

        for nmonths in aggMonths:

            # monthly averages

            convert_to_monthly_data(PrecFold, PrecNameIndex, PrecmonthlyFold, nmonths, maskFile)

            monthsFold = os.path.join(PrecmonthlyFold, "{:d}-Month-Files".format(nmonths))

            if stats:

                create_statistics(monthsFold, statsFold, PrecNameIndex, nmonths, maskFile)

if __name__ == '__main__':
    argv=sys.argv[1:]
    main(argv)
else:
    pass


from sortUtility import swap

def bubbleSort(toSort):
	print('Bubble Sort on {0}'.format(toSort))

	n = len(toSort)
	swapped = True
	while(swapped):
		swapped = False
		for i in range(1, n):
			if (toSort[i - 1] > toSort[i]):
				swap(toSort, i - 1, i)
				swapped = True
		n -= 1

	print('  Sorted {0}'.format(toSort))


import cv2
import numpy as np

image=cv2.imread('test.png',1)

cv2.imshow('test',image)
cv2.waitKey(0)
cv2.destroyAllWindows()

class imageOutput():
    def __init__(self,image):
        self.image = image
        self.lsb = []
        self.message=''

    def lsbHolder(self,shape):
        lsbbyte=[]
        for i in range(shape[0]):
            for j in range(shape[1]):
                for k in range(shape[2]):
                    if(self.image[i][j][k]>253):
                        self.lsb.append([self.image[i][j][k],i,j,k])
        for i in range(len(self.lsb)):
            lsbbyte.append("{0:b}".format(self.lsb[i][0]))
        return lsbbyte

    def decoder(self,lists):
        st=''
        z=0
        for i in range(len(lists)):
            st+= lists[i][7]
        for i in range(len(st)):
            z+= int(st[i])*(2**(len(st)-i-1))
        return z

    def result(self,z):
        self.message += chr(z)

obj = imageOutput(image)                                  #creating object

lsb = obj.lsbHolder(list(image.shape))                    #getting all pixel bytes >253

for i in range(0,len(lsb),8):
    z=obj.decoder(lsb[i:i+8])                             #decodes the message present in image
    if(z==32 or (z>64 and z<90) or (z>96 and z<123) ):
        obj.result(z)
    else:
        break

print(obj.message)


import pygame

brush = pygame.image.load('brush.png')
bigBrush = pygame.transform.scale(brush, (40, 40))
smallBrush = pygame.transform.scale(brush, (30, 30))

class button:
    def __init__(self, colour, height, width, x, y):
        self.colour = colour
        self.height = height
        self.width = width
        self.x = x
        self.y = y

    def draw(self, win, outline=False):
        if outline:
            pygame.draw.rect(win, (0,0,0), (self.x-2,self.y-2,self.height+3,self.width+3),2)
        pygame.draw.rect(win, self.colour, (self.x,self.y,self.height,self.width),0)

    def isOn(self, pos):
        if pos[0] >= self.x and pos[0] <= self.x + self.width:
            if pos[1] >= self.y and pos[0] <= self.y + self.height:
                return True
        return False

pygame.init()
pygame.display.set_icon(brush)
pygame.display.set_caption("Paint")
font = pygame.font.SysFont("comic sans", 30, True, False)
win = pygame.display.set_mode((500, 500))
canvas = win.copy()
colour = (0,0,0)
mouse = pygame.mouse
red = button((255,0,0), 30, 30, 3, 465)
orange = button((204,102,0), 30, 30, 36, 465)
yellow = button((255,255,0), 30, 30, 69, 465)
green = button((0,255,0), 30, 30, 102, 465)
blue = button((0,0,255), 30, 30, 135, 465)
purple = button((127,0,255), 30, 30, 3, 430)
pink = button((255,51,153), 30, 30, 36, 430)
black = button((0,0,0), 30, 30, 69, 430)
white = button((255,255,255), 30, 30, 102, 430)
grey = button((128,128,128), 30, 30, 135, 430)
r_select = False
o_select = False
y_select = False
green_select = False
blue_select = False
pu_select = False
pi_select = False
black_select = False
w_select = False
grey_select = False
SIZE = 10

def redraw_window():
    win.fill((255,255,255))
    win.blit(canvas, (0,0))
    win.blit(smallBrush, (168, 430))
    win.blit(bigBrush, (168, 460))
    brush_size = font.render("Brush Size: " + str(SIZE), 0, (0,0,0))
    win.blit(brush_size, (10,10))
    red.draw(win, r_select)
    orange.draw(win, o_select)
    yellow.draw(win, y_select)
    green.draw(win, green_select)
    blue.draw(win, blue_select)
    purple.draw(win, pu_select)
    pink.draw(win, pi_select)
    black.draw(win, black_select)
    white.draw(win, w_select)
    grey.draw(win, grey_select)
    pygame.draw.line(win, (0,0,0), (0,420), (500,420), 2)
    pygame.draw.circle(win, colour, (pygame.mouse.get_pos()),5)
    pygame.display.update()

canvas.fill((255,255,255))
run = True
while run:
    left_pressed, middle_pressed, right_pressed = mouse.get_pressed()
    for event in pygame.event.get():
        pos = pygame.mouse.get_pos()
        if event.type == pygame.QUIT:
            run = False
        elif left_pressed:
            if pos[0] >= 168 and pos[0] < 208:
                if pos[1] >= 460 and pos[1] < 500:
                    if SIZE < 30:
                        SIZE += 1

            if pos[0] >=168 and pos[0] < 198:
                if pos[1] >= 430 and pos[1] < 460:
                    if SIZE > 1:
                        SIZE = SIZE -1
            if red.isOn(pos):
                colour = red.colour
            elif orange.isOn(pos):
                colour = orange.colour
            elif yellow.isOn(pos):
                colour = yellow.colour
            elif green.isOn(pos):
                colour = green.colour
            elif blue.isOn(pos):
                colour = blue.colour
            elif purple.isOn(pos):
                colour = purple.colour
            elif pink.isOn(pos):
                colour = pink.colour
            elif black.isOn(pos):
                colour = black.colour
            elif white.isOn(pos):
                colour = white.colour
            elif grey.isOn(pos):
                colour = grey.colour

            if pos[1] < 410:
                pygame.draw.circle(canvas, colour, (pygame.mouse.get_pos()), SIZE)
        elif right_pressed:
            colour = (0,0,0)
    if colour == red.colour:
        r_select = True
    else:
        r_select = False
    if colour == orange.colour:
        o_select = True
    else:
        o_select = False
    if colour == yellow.colour:
        y_select = True
    else:
        y_select = False
    if colour == green.colour:
        green_select = True
    else:
        green_select = False
    if colour == blue.colour:
        blue_select = True
    else:
        blue_select = False
    if colour == purple.colour:
        pu_select = True
    else:
        pu_select = False
    if colour == pink.colour:
        pi_select = True
    else:
        pi_select = False
    if colour == black.colour:
        black_select = True
    else:
        black_select = False
    if colour == white.colour:
        w_select = True
    else:
        w_select = False
    if colour == grey.colour:
        grey_select = True
    else:
        grey_select = False
    redraw_window()
    pygame.display.update()


#!/usr/bin/env python
# Software License Agreement (BSD License)
#
# Copyright (c) 2008, Willow Garage, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
#  * Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#  * Redistributions in binary form must reproduce the above
#    copyright notice, this list of conditions and the following
#    disclaimer in the documentation and/or other materials provided
#    with the distribution.
#  * Neither the name of Willow Garage, Inc. nor the names of its
#    contributors may be used to endorse or promote products derived
#    from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# Revision $Id$

## Simple talker demo that published std_msgs/Strings messages
## to the 'chatter' topic

##rostopic pub /turtle1/cmd_vel geometry_msgs/Twist -r 1 -- '[2.0, 0.0,.0]' '[0.0, 0.0, 1.8]'

# import rospy
# from std_msgs.msg import String

# def talker():
#     pub = rospy.Publisher('chatter', String, queue_size=10)
#     rospy.init_node('talker', anonymous=True)
#     rate = rospy.Rate(10) # 10hz
#     while not rospy.is_shutdown():
#         hello_str = "hello world %s" % rospy.get_time()
#         rospy.loginfo(hello_str)
#         pub.publish(hello_str)
#         rate.sleep()

# if __name__ == '__main__':
#     try:
#         talker()
#     except rospy.ROSInterruptException:
#         pass

import rospy
import turtlesim
import geometry_msgs
import rosservice
import math

from std_msgs.msg import String, Int32, Float64
from geometry_msgs.msg import Twist, Vector3
msgval= Twist()

def talker():
    rosservice.call_service("/turtle1/teleport_absolute", [5,5,1.54])

    pub  = rospy.Publisher("turtle1/cmd_vel", Twist, queue_size=10)
    pub2 = rospy.Publisher('chatter', Float64, queue_size=10)
    freq = 10               # how many samples per second
    rate = rospy.Rate(freq) # 10hz

    # Getting Private Variables
    T    = rospy.get_param('~private_T', 5)

    # Calculating the trajectory
    numpts=T*freq           # calculate the number of points in the trajectory
    xval= [None]*numpts     # initialize xval list
    yval= [None]*numpts     # initialize yval list

    i=0                     # reset the index for the trajectory
    for i in range(0,numpts): # calculating the trajectory
        xval[i] = 3*math.sin(4*math.pi*i/T/freq)
        yval[i] = 3*math.sin(2*math.pi*i/T/freq)

    # Loop to execute the trajectory
    i=0 # reset the index for the trajectory
    while not rospy.is_shutdown():
        msgval = Twist(Vector3(1, xval[i], yval[i]),Vector3(0.0, 0.0, 1.8))
        rospy.loginfo(msgval)
        pub.publish(msgval)   # publishing the twist for the turtle to follow
        pub2.publish(yval[i]) # outputting yval/xval to chatter topid
        # increment/reset the index for the trajectory
        i += 1
        if i>=numpts:
            i=0
        rate.sleep()

if __name__ == '__main__':
    try:
        talker()
    except rospy.ROSInterruptException:
        pass


import logging
import platform
import getpass
import datetime

from crontab import CronTab
from taca.utils import statusdb
from taca.utils.config import CONFIG

def _parse_crontab():
    result = {}
    user = getpass.getuser()
    logging.info('Getting crontab for user {}'.format(user))
    try:
        crontab = CronTab(user=user)
    except Exception as e:
        logging.error('Cannot get a crontab for user: {}'.format(user))
        logging.error(e.message)
    else:
        result[user] = []
        for job in crontab.crons:
            # this is for special syntax like @monthly or @reboot
            special_syntax = str(job).split()[0] if str(job).startswith('@') else ''
            result[user].append({'Command': job.command,
                           'Comment': job.comment,
                           'Enabled': job.enabled,
                           'Minute': str(job.minutes),
                           'Hour': str(job.hours),
                           'Day of month' : str(job.dom),
                           'Month': str(job.month),
                           'Day of week': str(job.dow),
                           'Special syntax': special_syntax})
    return result

def update_cronjob_db():
    server = platform.node().split('.')[0]
    timestamp = datetime.datetime.now()
    # parse results
    result = _parse_crontab()
    # connect to db
    statusdb_conf = CONFIG.get('statusdb')
    logging.info('Connecting to database: {}'.format(CONFIG.get('statusdb', {}).get('url')))
    try:
        couch_connection = statusdb.StatusdbSession(statusdb_conf).connection
    except Exception as e:
        logging.error(e.message)
    else:
        # update document
        crontab_db = couch_connection['cronjobs']
        view = crontab_db.view('server/alias')
        # to be safe
        doc = {}
        # create doc if not exist
        if not view[server].rows:
            logging.info('Creating a document')
            doc = {
                'users': {user: cronjobs for user, cronjobs in result.items()},
                'Last updated': str(timestamp),
                'server': server,
            }
        # else: get existing doc
        for row in view[server]:
            logging.info('Updating the document')
            doc = crontab_db.get(row.value)
            doc['users'].update(result)
            doc['Last updated'] = str(timestamp)
        if doc:
            try:
                crontab_db.save(doc)
            except Exception as e:
                logging.error(e.message)
            else:
                logging.info('{} has been successfully updated'.format(server))
        else:
            logging.warning('Document has not been created/updated')


"""

New Playground
shellbacksq

279. Perfect Squares
题目描述提示帮助提交记录社区讨论阅读解答
给定正整数 n，找到若干个完全平方数（比如 1, 4, 9, 16, ...）使得它们的和等于 n。你需要让组成和的完全平方数的个数最少。

示例 1:

输入: n = 12
输出: 3
解释: 12 = 4 + 4 + 4.
示例 2:

输入: n = 13
输出: 2
解释: 13 = 4 + 9.
"""
class Solution:
    def numSquares(self, n):
        """
        :type n: int
        :rtype: int
        """
        #生成小于n的平方数序列1,4,9
        squares=self.generate_squares(n)
        queue=[]
        marked=[False]*(n+1)
        queue.append(n)
        level=0
        while queue:
            size=len(queue)
            level+=1
            print(queue)
            while size>0:
                cur=queue.pop(0)
                for s in squares:
                    nex=cur-s
                    if nex<0:
                        break
                    if nex==0:
                        return level
                    if marked[nex]:
                        continue
                    marked[nex]=True
                    queue.append(cur-s)
                size-=1
        return n

    def generate_squares(self,n):
        squares=[]
        diff=3
        square=1
        while square<=n:
            squares.append(square)
            square+=diff
            diff+=2
        return squares

def main():
    s=Solution()
    print(s.numSquares(12))

if __name__ == '__main__':
    main()


import cv2 #import cv2 library
import numpy as np

#to start the camera
capture = cv2.VideoCapture(0)

#load xml feature file so that we can detect face from image
face_haar_data = cv2.CascadeClassifier(r"C:\Users\Py.Developer_Aniket\Desktop\AI Python\Mini_Project\Face_Mask_Detection_Machine_Learning\haarcascade_frontalface_default.xml")

#collect all faces into this data
data =[]
cnt = 0
while(1):
    #load the image from camera
    flag,img = capture.read()
    #check camera is working or not
    if flag :
        #this function return array for faces from image
        faces = face_haar_data.detectMultiScale(img)
        for x,y,w,h in faces:
            #draw rectangle over face
            #cv2.rectangle(img,(x,y),(w,h),(r,g,b),boarder_thickness)
            cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,255),2)
            #slice the face from image
            face = img[y:y+w,x:x+w,:]
            #resize the face into one size .pixel
            face = cv2.resize(face,(50,50))
            #collect only 200 faces
            print(len(data))
            if(len(data) < 100):
                #store faces into dataset
                data.append(face)
                cnt = cnt + 1
                print(cnt)
        cv2.imshow("output",img) #show image
        # 27 is ASCII value of escapse key
        #It will wait for ESC button
        #if you not use this function then it will not load image
        if((cv2.waitKey(2) == 27)):
            #save data into file
            np.save("without_mask.npy",data)
            # you have to give the name '.npy' to save the data
            break;
cv2.destroyAllWindows()


import pandas as pd
import numpy as np
import matplotlib.pyplot as pyplot

x = np.arange(28*28)  # 生成数组
# print(x)
result = x.reshape((28, 28))  # 将一维数组变成28行28列  原数组不会被修改或者覆盖
# print(result)
# #img_train = pd.read_csv(x)#读取kaggle数据集，一维数组，代表28*28的图像
img_train = np.array(x)#这个是关键，转化为数组##本来就是数组了
print(img_train)
trainSet = np.zeros((28, 28))#生成0
# print(trainSet)
trainSet2 = np.zeros((1, 784))
# print(trainSet2)#变一维数组
t =img_train[0][1:]
# print(t)
trainSet=img_train.reshape(28, 28)

print(trainSet)
pyplot.imshow(trainSet)
pyplot.show()