import asyncio
import board
import keypad

agua = 0
radio = 9.0
intervalo = 10
ajuste = 1.18
limite = 1000

def pluviometria():
    global agua
    agua += 1
    print("Agua: ",agua)

# def resetPluviometria()
#     global agua
#     agua = 0

def calcularMM():
    global agua
    mm = agua * 0.2794
    return mm

async def catch_pin_transitions(pin):
    """Print a message when pin goes low and when it goes high."""
    with keypad.Keys((pin,), value_when_pressed=False) as keys:
        while True:
            event = keys.events.get()
            if event:
                if event.released:
                    pluviometria()
            await asyncio.sleep(0)

async def principal():
    while True:
        global intervalo
        global agua
        agua = 0
        await asyncio.sleep(intervalo)
        print(calcularMM(), "mm")

async def main():
    interrupt_task = asyncio.create_task(catch_pin_transitions(board.GP21))
    print_task = asyncio.create_task(principal())
    await asyncio.gather(interrupt_task, print_task)

asyncio.run(main())


import time
import math
import operator

from PIL import Image
from functools import reduce
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from Utils.YamlMethod import XiaoMaUtils
from Base.InitDriver import init_driver

class Base(object):
    def __init__(self, phone_name):

        self.driver = init_driver(phone_name)
        self.window_size = self.driver.get_window_size()

    def find_element_o(self, loc, timeout=30, poll=0.5):
        """
        根据定位获取单个元素

        :param loc: 元素的表达形式, 以元祖形式传递, eg. (By.ID,ID属性值)
        :param timeout: 等待时长
        :param poll: 查询频率
        :return: 定位对象
        """
        return WebDriverWait(self.driver, timeout, poll).until(lambda x: x.find_element(*loc))

    def find_elements_o(self, loc, timeout=20, poll=0.5):
        """
        根据定位获取一组元素

        :param loc: 元素的表达形式, 以元祖形式传递, eg. (By.ID,ID属性值)
        :param timeout: 等待时长
        :param poll: 查询频率
        :return: 一组定位对象
        """
        return WebDriverWait(self.driver, timeout, poll).until(lambda x: x.find_element(*loc))

    def click_element(self, loc):
        """
        点击操作

        :param loc: 元素的表达形式, 以元祖形式传递, eg. (By.ID,ID属性值)
        :return: None
        """
        self.find_element_o(loc).click()

    def long_press_element(self, ele, duration=2000):
        """
        长按操作

        :param ele: 要长按的对象
        :param duration: 长按时长, 默认为2000ms
        :return: None
        """
        TouchAction(self.driver).long_press(el=ele, duration=duration)

    def get_ele_text(self, ele):
        """
        获取元素的文本值

        :param ele: 元素对象
        :return: str
        """
        return self.get_element_attribute(ele, 'text')

    def get_ele_bounds(self, ele):
        """
        获取元素的边界

        :param ele: 元素对象
        :return: str, 形式如: [x1,y1][x2,y2]
        """
        return self.get_element_attribute(ele, 'bounds')

    def input_text(self, loc, text):
        """
        输入操作

        :param loc: 元素的表达形式, 以元祖形式传递, eg. (By.ID,ID属性值)
        :param text: 输入的内容
        :return:
        """
        ele = self.find_element_o(loc)
        ele.clear()
        ele.send_keys(text)

    def is_display(self, loc):
        """
        判断元素是否存在, 存在返回True, 不存在返回False

        :param loc: 元素的表达形式, 以元祖形式传递, eg. (By.ID,ID属性值)
        :return: bool
        """

        try:
            self.find_element_o(loc, timeout=10)
            return True
        except Exception as e:
            return False

    def getSize(self):
        resolution = self.driver.get_window_size()  # 获取手机分辨率
        resolution_width = resolution.get('width')   # 赋值手机分辨率-宽
        resolution_height = resolution.get('height') # 赋值手机分辨率-高
        return (resolution_width, resolution_height)

    # 根据相对坐标滑动，start_x：起始横坐标，start_y：起始纵坐标，end_x：结束时横坐标，end_y：结束时纵坐标，duration：滑动持续时间，单位毫秒，默认None（一般设置500-1000毫秒 0.5-1秒比较合适）
    def location_slide(self,start_x,start_y,end_x,end_y,duration=None):
        self.driver.swipe(start_x / 1080 * self.getSize()[0], start_y / 2211 * self.getSize()[1], end_x / 1080 * self.getSize()[0],end_y / 2211 * self.getSize()[1])

    def swipe_o(self, ele=None, location=None, direction='right', times=1):
        """
        根据用户输入的方向和次数, 滑动屏幕
        :param ele: 需要滑动区域的元素
        :param location: 需滑动的位置, 元组形式, eg. (x1, x2, y1, y2)
        :param direction: 滑动方向, 可选参数['up', 'down', 'left', 'right']
        :param times: 滑动次数
        :return: None
        """

        # 根据传入的元素解析滑动区域
        if ele:
            x1, y1, x2, y2 = XiaoMaUtils.convert_bounds_to_location(self.get_ele_bounds(ele))
        # 根据传入的坐标值解析滑动区域
        elif location:
            x1, x2, y1, y2 = location
        # 根据屏幕解析滑动区域
        else:
            width = self.window_size['width']
            length = self.window_size['height']
            x1 = width * 0.25
            x2 = width * 0.75
            y1 = length * 0.25
            y2 = length * 0.75

        # 循环滑动
        for i in range(times):
            if direction == 'up':
                self.driver.swipe(x1, y2, x1, y1)
            if direction == 'down':
                self.driver.swipe(x1, y1, x1, y2)
            if direction == 'right':
                self.driver.swipe(x1, y1, x2, y1)
            if direction == 'left':
                self.driver.swipe(x2, y1, x1, y1)
        time.sleep(2)

    def wait_activity_o(self, activity, timeout=10, interval=1):
        """
        等待activity加载完成

        :param activity: activity名称
        :param timeout: 等待时长
        :param interval: 循环查询时间, 默认每1s查询一次
        :return:
        """
        self.driver.wait_activity(activity, timeout, interval)

    def get_element_attribute(self, ele, attribute):
        """
        获取元素属性

        :param ele: 元素对象
        :param attribute: 属性名称
        :return: 元素属性值
        """
        if attribute == 'size':
            return ele.size
        else:
            return ele.get_attribute(attribute)

    def screenshot_part(self, element, img_name, bounds=None):
        """
            功能: 截图指定元素
            参数:
                :param element: 要获取的元素
                :param img_name: 要保存的名字
                :param bounds: 元素边界(对于无法定位到的元素, 可直接使用边界定位)
        """
        time.sleep(2)

        # 截取屏幕, 并打开该图像
        XiaoMaUtils.get_screencap()
        img = Image.open('Image/screen.png')

        if bounds:
            (x1, y1, x2, y2) = bounds
        else:
            # 获取待截取的元素的边界
            x1 = int(XiaoMaUtils.convert_bounds_to_location(self.get_element_attribute(element, 'bounds'))[0])
            y1 = int(XiaoMaUtils.convert_bounds_to_location(self.get_element_attribute(element, 'bounds'))[1])
            x2 = int(XiaoMaUtils.convert_bounds_to_location(self.get_element_attribute(element, 'bounds'))[2])
            y2 = int(XiaoMaUtils.convert_bounds_to_location(self.get_element_attribute(element, 'bounds'))[3])

        # 设置图像裁剪区域 (x左上，y左上，x右下,y右下)
        box1 = (x1, y1, x2, y2)

        # 图像裁剪
        image1 = img.crop(box1)

        # 存储裁剪得到的图像
        image1.save("Image/" + img_name + ".png")

    @staticmethod
    def image_contrast(img1="img1.png", img2="img2.png"):
        """
            功能: 比较两张图片的差异
            返回: 0.0表示是完全一致的图片, 数值越大, 图片的差异越大
        """
        image1 = Image.open("Image/" + img1)
        image2 = Image.open("Image/" + img2)

        h1 = image1.histogram()
        h2 = image2.histogram()

        result = math.sqrt(reduce(operator.add, list(map(lambda a, b: (a - b) ** 2, h1, h2))) / len(h1))

        return result


import torch
from torch import nn
import torch.nn.functional as F
from torch.autograd import Function, Variable
from .backbone import build_backbone
from .transformer import build_transformer, TransformerEncoder, TransformerEncoderLayer

import numpy as np

import IPython
e = IPython.embed

def reparametrize(mu, logvar):
    std = logvar.div(2).exp()
    eps = Variable(std.data.new(std.size()).normal_())
    return mu + std * eps

def get_sinusoid_encoding_table(n_position, d_hid):
    def get_position_angle_vec(position):
        return [position / np.power(10000, 2 * (hid_j // 2) / d_hid) for hid_j in range(d_hid)]

    sinusoid_table = np.array([get_position_angle_vec(pos_i)
                              for pos_i in range(n_position)])
    sinusoid_table[:, 0::2] = np.sin(sinusoid_table[:, 0::2])  # dim 2i
    sinusoid_table[:, 1::2] = np.cos(sinusoid_table[:, 1::2])  # dim 2i+1

    return torch.FloatTensor(sinusoid_table).unsqueeze(0)

class GradientReversalFunc(Function):
    @staticmethod
    def forward(ctx, x, alpha):
        ctx.save_for_backward(x, alpha)
        return x

    @staticmethod
    def backward(ctx, grad_output):
        grad_input = None
        _, alpha = ctx.saved_tensors
        if ctx.needs_input_grad[0]:
            grad_input = - alpha*grad_output
        return grad_input, None

revgrad = GradientReversalFunc.apply

class GradientReversalLayer(nn.Module):
    def __init__(self, alpha=1):
        super().__init__()
        self.alpha = torch.tensor(alpha, requires_grad=False)

    def forward(self, x):
        return revgrad(x, self.alpha)

class DETRVAE(nn.Module):
    """ This is the DETR module that performs object detection """

    def __init__(self, backbones, transformer, encoder, action_dim, num_queries, dann):
        """ Initializes the model.
        Parameters:
            backbones: torch module of the backbone to be used. See backbone.py
            transformer: torch module of the transformer architecture. See transformer.py
            action_dim: action dimension
            num_queries: number of object queries, ie detection slot. This is the maximal number of objects
                         DETR can detect in a single image. For COCO, we recommend 100 queries.
            aux_loss: True if auxiliary decoding losses (loss at each decoder layer) are to be used.
        """
        super().__init__()
        self.camera_names = ["rgb1"]

        self.num_queries = num_queries
        self.transformer = transformer
        self.encoder = encoder
        hidden_dim = transformer.d_model
        self.action_head = nn.Linear(hidden_dim, action_dim)
        self.is_pad_head = nn.Linear(hidden_dim, 1)
        self.query_embed = nn.Embedding(num_queries, hidden_dim)
        self.input_proj_robot_state = nn.Linear(29, hidden_dim)

        if backbones is not None:
            # Use backbone to extract features
            self.is_feature = False
            self.input_proj = nn.Conv2d(
                backbones[0].num_channels, hidden_dim, kernel_size=1)
            self.backbones = nn.ModuleList(backbones)
            self.encoder_obs_proj = nn.Linear(
                256*3*3, hidden_dim)  # project feature to embedding
        else:
            self.fea_pos_embed = nn.Embedding(1, hidden_dim)
            self.encoder_obs_proj = nn.Linear(
                2048, hidden_dim)  # project obs to embedding
            # self.encoder_obs_proj = nn.Linear(
            #     768, hidden_dim)  # project obs to embedding for mvp
            self.is_feature = True

        # encoder extra parameters
        self.latent_dim = 256  # final size of latent z # TODO tune
        # extra cls token embedding
        self.cls_embed = nn.Embedding(1, hidden_dim)
        self.encoder_action_proj = nn.Linear(
            22, hidden_dim)  # project action to embedding
        self.encoder_qpos_proj = nn.Linear(
            29, hidden_dim)  # project qpos to embedding
        # project hidden state to latent std, var
        self.latent_proj = nn.Linear(hidden_dim, self.latent_dim*2)
        self.register_buffer('pos_table', get_sinusoid_encoding_table(
            1+1+1+num_queries, hidden_dim))  # [CLS], obs, qpos, a_seq

        # decoder extra parameters
        # project latent sample to embedding
        self.latent_out_proj = nn.Linear(self.latent_dim, hidden_dim)
        # learned position embedding for proprio and latent
        self.additional_pos_embed = nn.Embedding(2, hidden_dim)

        # Add DANN
        self.dann = dann
        if self.dann:
            self.domain_clf = nn.Sequential(
                GradientReversalLayer(),
                nn.Linear(256*7*7, hidden_dim),
                nn.BatchNorm1d(hidden_dim),
                nn.ReLU(),
                nn.Linear(hidden_dim, hidden_dim),
                nn.BatchNorm1d(hidden_dim),
                nn.ReLU(),
                nn.Linear(hidden_dim, 1),
            )

    def forward(self, image, qpos, actions=None, is_pad=None):
        """
        image: batch, num_cam, channel, height, width
        qpos: batch, qpos_dim
        env_state: None
        actions: batch, seq, action_dim
        """
        is_training = actions is not None  # train or val
        bs, _ = qpos.shape

        if not self.is_feature:
            # Image observation features and position embeddings
            all_cam_features = []
            all_cam_pos = []
            for cam_id, cam_name in enumerate(self.camera_names):
                features, pos = self.backbones[0](
                    image[:, cam_id])  # HARDCODED
                features = features[0]  # take the last layer feature
                pos = pos[0]
                all_cam_features.append(self.input_proj(features))
                all_cam_pos.append(pos)

            # fold camera dimension into width dimension
            src = torch.cat(all_cam_features, axis=3)  # (bs, 256, 7, 7)
            pos = torch.cat(all_cam_pos, axis=3)

        else:
            # Features and position embeddings
            src = self.encoder_obs_proj(image)  # hidden_dim
            pos = self.fea_pos_embed.weight  # (1, hidden_dim)

        # Obtain latent z from action sequence
        if is_training:
            # project action sequence to embedding dim, and concat with a CLS token
            action_embed = self.encoder_action_proj(
                actions)  # (bs, seq, hidden_dim)
            if not self.is_feature:
                feature = F.max_pool2d(src, kernel_size=2, stride=2)
                feature = feature.flatten(1)  # (bs, 256*3*3)
                obs_embed = self.encoder_obs_proj(feature)
            else:
                obs_embed = src
            obs_embed = torch.unsqueeze(obs_embed, axis=1)
            qpos_embed = self.encoder_qpos_proj(qpos)  # (bs, hidden_dim)
            qpos_embed = torch.unsqueeze(
                qpos_embed, axis=1)  # (bs, 1, hidden_dim)
            cls_embed = self.cls_embed.weight  # (1, hidden_dim)
            cls_embed = torch.unsqueeze(cls_embed, axis=0).repeat(
                bs, 1, 1)  # (bs, 1, hidden_dim)
            # (bs, seq+1, hidden_dim)
            encoder_input = torch.cat(
                [cls_embed, obs_embed, qpos_embed, action_embed], axis=1)
            encoder_input = encoder_input.permute(
                1, 0, 2)  # (seq+1, bs, hidden_dim)
            # do not mask cls token
            cls_joint_is_pad = torch.full((bs, 3), False).to(
                qpos.device)  # False: not a padding
            is_pad = torch.cat([cls_joint_is_pad, is_pad],
                               axis=1)  # (bs, seq+1)
            # obtain position embedding
            pos_embed = self.pos_table.clone().detach()
            pos_embed = pos_embed.permute(1, 0, 2)  # (seq+1, 1, hidden_dim)
            # query model
            encoder_output = self.encoder(
                encoder_input, pos=pos_embed, src_key_padding_mask=is_pad)
            encoder_output = encoder_output[0]  # take cls output only
            latent_info = self.latent_proj(encoder_output)
            mu = latent_info[:, :self.latent_dim]
            logvar = latent_info[:, self.latent_dim:]
            latent_sample = reparametrize(mu, logvar)
            latent_input = self.latent_out_proj(latent_sample)
        else:
            mu = logvar = None
            latent_sample = torch.zeros(
                [bs, self.latent_dim], dtype=torch.float32).to(qpos.device)
            latent_input = self.latent_out_proj(latent_sample)

        # proprioception features
        proprio_input = self.input_proj_robot_state(qpos)
        hs = self.transformer(src, None, self.query_embed.weight, pos,
                              latent_input, proprio_input, self.additional_pos_embed.weight, self.is_feature)[0]

        a_hat = self.action_head(hs)
        is_pad_hat = self.is_pad_head(hs)

        if self.dann and is_training:
            src = src.flatten(1)
            domain_logits = self.domain_clf(src)
            domain_logits = domain_logits.view(bs, 1)
            domain_logits = torch.sigmoid(domain_logits)
            return a_hat, is_pad_hat, [mu, logvar], domain_logits

        return a_hat, is_pad_hat, [mu, logvar]

def build_encoder(args):
    d_model = args.hidden_dim  # 256
    dropout = args.dropout  # 0.1
    nhead = args.nheads  # 8
    dim_feedforward = args.dim_feedforward  # 2048
    num_encoder_layers = args.enc_layers  # 4 # TODO shared with VAE decoder
    normalize_before = args.pre_norm  # False
    activation = "relu"

    encoder_layer = TransformerEncoderLayer(d_model, nhead, dim_feedforward,
                                            dropout, activation, normalize_before)
    encoder_norm = nn.LayerNorm(d_model) if normalize_before else None
    encoder = TransformerEncoder(
        encoder_layer, num_encoder_layers, encoder_norm)

    return encoder

def build_ACT_model(args):
    action_dim = 22  # TODO hardcode

    # From state
    # backbone = None # from state for now, no need for conv nets
    # From image

    transformer = build_transformer(args)

    encoder = build_encoder(args)

    if not args.is_feature:
        backbones = []
        backbone = build_backbone(args)
        backbones.append(backbone)

        model = DETRVAE(
            backbones=backbones,
            transformer=transformer,
            encoder=encoder,
            action_dim=action_dim,
            num_queries=args.num_queries,
            dann=args.dann
        )
    else:
        model = DETRVAE(
            backbones=None,
            transformer=transformer,
            encoder=encoder,
            action_dim=action_dim,
            num_queries=args.num_queries,
            dann=args.dann
        )

    n_parameters = sum(p.numel()
                       for p in model.parameters() if p.requires_grad)
    print("number of parameters: %.2fM" % (n_parameters/1e6,))

    return model


# importing models.
import argparse
import torch
import json
import torchvision
from torchvision import datasets, transforms, models
from torch import nn
from torch import optim
import torch.nn.functional as F
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np

def main():
    # getting arguments
    args = get_input_args()

    # loading model
    model = load_checkpoint(args.model_dir)

    # device = gpu_check(args.gpu)
    if args.gpu:
        device = 'cuda'
    else:
        device = 'cpu'
    # converting model to device type
    model.to(device)

    with open(args.cat_to_name, 'r') as f:
        cat_to_name = json.load(f)

    # predict image
    probs, names = predict(args.image_path,args.on_gpu, model,device,cat_to_name,topk=args.top_k)

    # Print name of predicted flower with highest probability
    print("This flower is most likely to be a: {} with a probability of {}% ".format(names[0],np.round(probs[0][0]*100,2)))

 # function for arguments
def get_input_args():
    parser = argparse.ArgumentParser(description="Train a Neural Network to Predict Images")
    parser.add_argument('--model_dir', type=str, default= "my_checkpoint.pth", help="directory of the model")
    parser.add_argument('--image_path',type=str, default= "flowers/test/1/image_06743.jpg",help="Enter path to image for prediction")
    parser.add_argument('--cat_to_name', type=str, default="cat_to_name.json", help="Enter path to cat_to_name file")
    parser.add_argument('--gpu', action = "store_true",help= "GPU")
    parser.add_argument('--top_k', type=int,  default=5, help="Top K")
    parser.add_argument('--on_gpu', type=str, default= "gpu", help="use gpu or cpu, default is gpu. Make sure gpu is turned on")

    args = parser.parse_args()

    return args

# function to load model
def load_checkpoint(filepath):
    checkpoint = torch.load(filepath)
    model = models.vgg16(pretrained=True)

    model.classifier = checkpoint['classifier']
    model.load_state_dict(checkpoint['state_dict'])
    model.class_to_idx = checkpoint['class_to_idx']

    for param in model.parameters():
        param.requires_grad = False

    return model

# function to load model using PIL image and passing it to the model
def process_image(image):
    ''' Scales, crops, and normalizes a PIL image for a PyTorch model,
        returns an Numpy array
    '''
    # open imgae using PIL.Image
    img = Image.open(image)

    # define means and std
    means = [0.485, 0.456, 0.406]
    deviations = [0.229, 0.224, 0.225]
    # define transforms
    transform = transforms.Compose([transforms.Resize(256),
                                         transforms.CenterCrop(224),
                                         transforms.ToTensor(),
                                         transforms.Normalize(means, deviations)])
    # apply transform to the image
    image = transform(img)

    # return image
    return image

# function to predict the image.
def predict(image_path,on_gpu, model,device,cat_to_name,topk=5):
    ''' Predict the class (or classes) of an image using a trained deep learning model.
    '''

    model.to(device)
    # process image
    img = process_image(image_path)
    img.unsqueeze_(0)
    # convert image to float
    img = img.float()

    model.eval()

    # check if gpu exist. if gpu exits  transformed image is converted to cuda, else pass transformed image to model
    if on_gpu == "gpu":
        with torch.no_grad():
            output = model(img.cuda())
    else:
        with torch.no_grad():
            output = model(img)

    # calculating probabilites
    prob = torch.exp(output)
    probs = np.array(prob.topk(topk)[0])
    idxs = np.array(prob.topk(topk)[1])

    # convert indicies to classes
    idxs = np.array(idxs)
    idx_to_class = {value:key for key, value in model.class_to_idx.items()}
    classes = [idx_to_class[idx] for idx in idxs[0]]

    #map the class name with collect topk classes
    names = []
    for class_ in classes:
        names.append(cat_to_name[str(class_)])

    return probs, names

if __name__ == '__main__':main()


"""
You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide a hint that indicates how many digits in said guess match your secret number exactly in both digit and position (called "bulls") and how many digits match the secret number but locate in the wrong position (called "cows"). Your friend will use successive guesses and hints to eventually derive the secret number.

Write a function to return a hint according to the secret number and friend's guess, use A to indicate the bulls and B to indicate the cows.

Please note that both secret number and friend's guess may contain duplicate digits.

Example 1:

Input: secret = "1807", guess = "7810"

Output: "1A3B"

Explanation: 1 bull and 3 cows. The bull is 8, the cows are 0, 1 and 7.
Example 2:

Input: secret = "1123", guess = "0111"

Output: "1A1B"

Explanation: The 1st 1 in friend's guess is a bull, the 2nd or 3rd 1 is a cow.
Note: You may assume that the secret number and your friend's guess only contain digits, and their lengths are always equal.
"""

class Solution:
    def getHint(self, secret: str, guess: str) -> str:
        from collections import Counter
        bulls = 0
        cows = 0
        bullInd = []
        seenCows = []
        counts = Counter(secret)
        indexes = list(range(len(secret)))
        for i, g in enumerate(guess):
            if secret[i] == g:
                bulls += 1
                counts[g] -= 1
                indexes.remove(i)

        for i in indexes:
            g = guess[i]
            if (g in counts) and counts[g] > 0:
                cows += 1
                counts[g] -= 1

        return str(bulls) + "A" + str(cows) + "B"

Leetcode SeptemberChallenge/Bulls and Cows.py


#Data Manipulation
import numpy as np
import pandas as pd

#plotting

import matplotlib.pyplot as plt

##Mchine Learning
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score

## Data fetching
df=pd.read_csv('E:/dta/Google.csv')

df=df.dropna()
df=df[['Open','High','Low','Close']]
df.head()

#####Predictor Variables
df['Open-Close']=df.Open-df.Close
df['High-Low']=df.High-df.Low
df.dropna()
X=df[['Open-Close','High-Low']]
X.head()

##Target Variable
Y=np.where(df['Close'].shift(-1)>df['Close'],1,-1)
print(Y)

##Split the Dataset
split_percentage=0.7
split=int(split_percentage*len(df))

X_train=X[:split]
Y_train=Y[:split]

X_test=X[split:]
Y_test=Y[split:]
##################OR===Split the Dataset(Randomly)
from sklearn.model_selection import train_test_split
X_train, X_test, Y_train, Y_test=train_test_split(X,Y, test_size=0.3,random_state=10)

####KNN Learning Model (K=15)
knn=KNeighborsClassifier(n_neighbors=15)

##Fit the model

knn.fit(X_train,Y_train)

####Accuracy Score

accuracy_train=accuracy_score(Y_train,knn.predict(X_train))
accuracy_test=accuracy_score(Y_test, knn.predict(X_test))

print('Train_data Accuracy:%2f' %accuracy_train)
print('Test_data Accuracy: %.2f' %accuracy_test)

#####Predict Signal
df['Predicted_Signal']=knn.predict(X)

###SPY Cumulative Returns
df['SPY_Returns']=np.log(df['Close']/df['Close'].shift(1))
Cumulative_SPY_Returns=df[split:]['SPY_Returns'].cumsum()*100

###Cumulative Strategey Returns
df['Strategy_Returns']=df['SPY_Returns']* df['Predicted_Signal'].shift(1)
Cum_Strategy_Returns=df[split:]['Strategy_Returns'].cumsum()*100

#####Plot the results
plt.figure(figsize=(10,5))
plt.plot(Cumulative_SPY_Returns,Color='r',label='SPY Returns')
plt.plot(Cum_Strategy_Returns,color='g',label='Strategy Returns')
plt.legend
plt.show()


import urllib.request
from bs4 import BeautifulSoup
import csv

class Scraper:
    def __init__(self, site):
        self.site = site

    def save_data(self,data):
        headers = data[0].keys()
        with open("news_data.csv", 'w') as out_file:
            dict_writer = csv.DictWriter(out_file,headers)
            dict_writer.writeheader()
            dict_writer.writerows(data)

    def scrape(self):
        news_list = []
        r = urllib.request.urlopen(self.site)
        html = r.read()
        parser = "html.parser"
        sp = BeautifulSoup(html,parser)
        section = sp.find('section',class_="mainbox")
        article = section.find('article',class_="leftcol MT30")
        div_list = article.find('div',class_="listingholder")
        for div in div_list:
            news_dict = {}
            figure = div.find('figure')
            if figure != -1:
                news_dict['figure'] = figure.find('img').get('src')
            h2 = div.find('h2')
            if h2 != -1:
                news_dict['title'] = h2.select('a')[0].text
                news_dict['date'] = div.select('span')[0].text
            short_description = div.find('p')
            if short_description != -1:
                news_dict['short_desc'] = short_description.get_text()
            if news_dict:
                news_list.append(news_dict)
        self.save_data(news_list)

news = "https://www.loksatta.com/sampadkiya/agralekh/"
Scraper(news).scrape()


#!/usr/bin/python3
# -*- coding: utf-8 -*-
'''
author: @thewhitetulip

A small python script to extract browsing history and bookmarks from  various
browsers, currently supports firefox and chromium (partially)

'''

import sqlite3
import os
from datetime import datetime
import sys
import argparse
import webbrowser

def execute_query(cursor, query):
    ''' Takes the cursor object and the query, executes it '''
    try:
        cursor.execute(query)
    except Exception as error:
        print(str(error) + "\n " + query)

def open_browser(url):
    '''Opens the default browswer'''
    webbrowser.open(url, autoraise=True)

def read_template():
    """ Reads the html content from the template which will be returned
    as a string to write in another file """
    file = open("template.html", 'r')
    lines = file.readlines()

    tmp = u""
    lines = [line.strip() for line in lines]
    for line in lines:
        tmp += str(line)
        tmp += "\n"
    return tmp

def history(cursor, pattern=None, src=""):
    ''' Function which extracts history from the sqlite file '''
    html = u""
    html = read_template()
    if src == 'firefox':
        sql = """select url, title, last_visit_date,rev_host
        from moz_historyvisits natural join moz_places where
        last_visit_date is not null and url  like 'http%' and title is not null
        and url not like '%google.com%' and url not like '%gmail.com%' and
        url not like '%facebook.com%' and url not like '%amazon.com%' and
        url not like '%127.0.0.1%' and url not like '%google.com%'
        and url not like '%duckduckgo.com%'
        and url not like '%change.org%' and url not like
        '%twitter.com%' and url not like '%google.co.in%' """

        if pattern is not None:
            sql += " and url like '%"+pattern+"%' "
        sql += " order by last_visit_date desc;"

        execute_query(cursor, sql)

        for row in cursor:
            last_visit = datetime.fromtimestamp(row[2]/1000000).strftime('%Y-%m-%d %H:%M:%S')
            link = row[0]
            title = row[1]

            html += "
" + str(title[:100]) +\
            "	" + "" + str(last_visit) + "	" + "" + \
            str(link[:100]) + "	" + "
\n"
            #print(a)

    if src == 'chrome':
        sql = "SELECT urls.url, urls.title, urls.visit_count, \
        urls.typed_count, datetime(urls.last_visit_time/1000000-11644473600,'unixepoch','localtime'), urls.hidden,\
        visits.visit_time, visits.from_visit, visits.transition FROM urls, visits\
         WHERE  urls.id = visits.url and urls.title is not null order by last_visit_time desc "

        execute_query(cursor, sql)
        for row in cursor:
            print("%s %s"%(row[0], row[4]))

    html += "
\n
\n\n"
    html_file = open("history.html", 'w')

    html_file.write(html)
    html_file.close()
    open_browser("history.html")

def bookmarks(cursor, pattern=None):
    ''' Function to extract bookmark related information '''

    the_query = """select url, moz_places.title, rev_host, frecency,
    last_visit_date from moz_places  join  \
    moz_bookmarks on moz_bookmarks.fk=moz_places.id where visit_count>0
    and moz_places.url  like 'http%'
    order by dateAdded desc;"""

    execute_query(cursor, the_query)

    html = u""
    html = read_template()
    html_file = open("bookmarks.html", 'w')
    for row in cursor:
        link = row[0]
        title = row[1]
        date = str(datetime.fromtimestamp(row[4]/1000000).strftime('%Y-%m-%d %H:%M:%S'))

        html += "
"+title+"	"+""+link+\
        "	"+""+date+"
\n"
        print("%s %s"%(row[0], row[1]))
    html += "
\n
\n\n"

    try:
        html_file.write(html.encode('utf8'))
    except:
        html_file.write(html)
    html_file.close()
    open_browser("bookmarks.html")

def get_path(browser):
    '''Gets the path where the sqlite3 database file is present'''
    if browser == 'firefox':
        if sys.platform.startswith('win') == True:
            path = '\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\'
        elif sys.platform.startswith('linux') == True:
            path = "/.mozilla/firefox/"
        elif sys.platform.startswith('darwin') == True:
            path = '/Library/Application Support/Firefox/Profiles/'

    #elif browser == 'chrome':
    #    if sys.platform.startswith('win') == True:
    #        path = ''
    #    elif sys.platform.startswith('linux') == True:
    #        path =  "/.config/chromium/Default/History"
    #    elif sys.platform.startswith('darwin') == True:
    #        path = ''

    return path

if __name__ == "__main__":
    DESC_PYFOX = "Extract information from firefox's internal database"
    parser = argparse.ArgumentParser(description=DESC_PYFOX)
    parser.add_argument('--bm', '-b', default="")
    parser.add_argument('--hist', '-y', default="")
    args = parser.parse_args()

    try:
        firefox_path = get_path('firefox')
        home_dir = os.environ['HOME']
        firefox_path = home_dir + firefox_path; print(firefox_path)
        profiles = [i for i in os.listdir(firefox_path) if i.endswith('.default')]
        sqlite_path = firefox_path+ profiles[0]+'/places.sqlite'
        print(sqlite_path)
        if os.path.exists(sqlite_path):
            firefox_connection = sqlite3.connect(sqlite_path)

        #chrome_sqlite_path = '/home/thewhitetulip/.config/chromium/Default/History'
        #chrome_sqlite_path = get_path('chrome')
        #if os.path.exists(chrome_sqlite_path):
        #    chrome_connection = sqlite3.connect(chrome_sqlite_path)
    except Exception as error:
        print("_main_")
        print(str(error))
        exit(1)

    cursor = firefox_connection.cursor()
    #CHROME_CURSOR = chrome_connection.cursor()

    if args.bm is not '':
        bookmarks(cursor, pattern=args.bm)
    if args.hist is not '':
        print("From firefox")
        history(cursor, pattern=args.hist, src="firefox")
        #print("From chrome")
        #history(CHROME_CURSOR, src="chrome")

    cursor.close()
    #CHROME_CURSOR.close()


from concurrent import futures
import time
import math
import logging
import grpc
import cli_grpc_pb2 as pb2
import cli_grpc_pb2_grpc as pb2_grpc

from helper_functions import read_database, find_entry

class FileServicer(pb2_grpc.FileServicer):
    def __init__(self):
        self.db = read_database()

    def stat(self, request, context):
        _reply_data = find_entry(self.db, request.uuid)
        if isinstance(_reply_data, grpc.StatusCode):
            return _reply_data
        _reply = {"data": _reply_data[0]}
        return pb2.StatReply(**_reply)

    def read(self, request, context):
        _reply_data = find_entry(self.db, request.uuid)
        if isinstance(_reply_data, grpc.StatusCode):
            return _reply_data
        _chunk_size = request.size
        _data = _reply_data[1]['data']
        if _chunk_size == 0:
            _chunk_size = len(_data)
        _idx = 0
        while _idx < len(_data):
            _chunk = _data[_idx:_idx+_chunk_size]
            _idx += _chunk_size
            _reply_db_data = {"data": _chunk}
            _reply = {"data": _reply_db_data}
            yield pb2.ReadReply(**_reply)

def serve():
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    pb2_grpc.add_FileServicer_to_server(
        FileServicer(), server)
    server.add_insecure_port('[::]:50051')
    server.start()
    server.wait_for_termination()

if __name__ == '__main__':
    logging.basicConfig()
    serve()


import sys
import os
import yaml
import re
import urllib.request

DEBUG_FLAG = True

class label_configure:
    def is_url(self, path: str) -> bool:
        pattern = "https?://[\w/:%#\$&\?\(\)~\.=\+\-]+"
        if re.match(pattern, path):
            return True
        else:
            return False

    def __init__(self, yaml_abs_path: str, target_key: str) -> None:
        self.stylesheet_str = str()
        self.yaml_abs_path_file = yaml_abs_path
        self.target_key = target_key

        self.debug = DEBUG_FLAG

        with open(self.yaml_abs_path_file, 'r') as f:
            self.yaml_data = yaml.load(f, Loader=yaml.FullLoader)
        # exit when no yaml data
        if self.yaml_data is None:
            print("no yaml data")
            sys.exit()
        self.yaml_data = self.yaml_data[self.target_key]

    # include yaml from yaml ---------------------------------------------------
        if "include" in self.yaml_data:
            path = self.yaml_data["include"]["path"]
            key = self.yaml_data["include"]["key"]
            # is url or path
            if self.is_url(path):
                # is url -> save to ~/.cache/pyamlqt/yaml/***.yaml and load it
                # exists ~/.cache/pyamlqt/yaml/***.yaml ?
                if not os.path.exists(os.path.expanduser("~/.cache/pyamlqt/yaml/" + os.path.basename(path))):
                    os.makedirs(os.path.expanduser(
                        "~/.cache/pyamlqt/yaml"), exist_ok=True)
                    urllib.request.urlretrieve(path, os.path.expanduser(
                        "~/.cache/pyamlqt/yaml/") + os.path.basename(path))
                    print("download yaml file: " + path)
                else:
                    print("yaml file is already downloaded (Please delete ~/.cache/pyamlqt/yaml/" +
                          os.path.basename(path) + " to download again)")
                path = os.path.expanduser(
                    "~/.cache/pyamlqt/yaml/") + os.path.basename(path)

            with open(path, 'r') as f:
                self.yaml_data = yaml.load(f, Loader=yaml.FullLoader)
            self.yaml_data = label_configure(path, key).yaml_data
            print("yaml_data: " + str(self.yaml_data))
            # return force
            return

    # Using common tag --------------------------------------------------------

        if "type" in self.yaml_data:
            self.type = self.yaml_data["type"]
        else:
            print("type is undefined.")
            sys.exit()

        if "x_center" in self.yaml_data:
            self.x_center = self.yaml_data["x_center"]
        else:
            self.x_center = 0

        if "y_center" in self.yaml_data:
            self.y_center = self.yaml_data["y_center"]
        else:
            self.y_center = 0

        if "width" in self.yaml_data:
            self.width = self.yaml_data["width"]
        else:
            self.width = 50

        if "height" in self.yaml_data:
            self.height = self.yaml_data["height"]
        else:
            self.height = 50

        if "text" in self.yaml_data:
            self.text = self.yaml_data["text"]
        else:
            self.text = ""
        if "font_size" in self.yaml_data:
            self.font_size = self.yaml_data["font_size"]
        else:
            self.font_size = 10

        if "font_color" in self.yaml_data:
            self.font_color = self.yaml_data["font_color"]
        else:
            self.font_color = "#000000"

        if "background_color" in self.yaml_data:
            self.background_color = self.yaml_data["background_color"]
        else:
            self.background_color = ""

        if "font" in self.yaml_data:
            self.font = self.yaml_data["font"]
        else:
            self.font = "Arial"

        if "font_bold" in self.yaml_data:
            self.font_bold = self.yaml_data["font_bold"]
        else:
            self.font_bold = False

        if "items" in self.yaml_data:
            self.items = self.yaml_data["items"]
        else:
            self.items = []

        if "max" in self.yaml_data:
            self.max = self.yaml_data["max"]
        else:
            self.max = 100

        if "min" in self.yaml_data:
            self.min = self.yaml_data["min"]
        else:
            self.min = 0

        if "step" in self.yaml_data:
            self.step = self.yaml_data["step"]
        else:
            self.step = 1

        if "default" in self.yaml_data:
            self.default = self.yaml_data["default"]
        else:
            self.default = 0

        # if "path" in self.yaml_data:
        #     self.path = self.yaml_data["path"]
        #     if script_dir != "":
        #         self.path = script_dir + self.path
        #         self.path = os.path.abspath(self.path)
        # else:
        #     self.path = ""

    # StyleSheet ---------------------------------------------------------------
        if "style" in self.yaml_data:
            # if include key
            if "include" in self.yaml_data["style"]:
                path = self.yaml_data["style"]["include"]["path"]
                key = self.yaml_data["style"]["include"]["key"]
                # is url or path
                print("path: " + path)
                if self.is_url(path):
                    # is url -> save to ~/.cache/pyamlqt/yaml/***.yaml and load it
                    # exists ~/.cache/pyamlqt/yaml/***.yaml ?
                    if not os.path.exists(os.path.expanduser("~/.cache/pyamlqt/yaml/" + os.path.basename(path))):
                        os.makedirs(os.path.expanduser(
                            "~/.cache/pyamlqt/yaml"), exist_ok=True)
                        urllib.request.urlretrieve(path, os.path.expanduser(
                            "~/.cache/pyamlqt/yaml/") + os.path.basename(path))
                        print("download yaml file: " + path)
                    else:
                        print("yaml file is already downloaded (Please delete ~/.cache/pyamlqt/yaml/" +
                              os.path.basename(path) + " to download again)")
                    print("yaml_data: " + str(self.yaml_data))
                    path = os.path.expanduser(
                        "~/.cache/pyamlqt/yaml/") + os.path.basename(path)

                with open(path, 'r') as f:
                    self.stylesheet_str = yaml.load(f, Loader=yaml.FullLoader)
                self.stylesheet_str = label_configure(
                    path, key).stylesheet_str
                print("stylesheet_str: " + str(self.stylesheet_str))

            else:
                for key, value in self.yaml_data["style"].items():
                    # configure stylesheet
                    if (key == "background-image"):
                        # check path "./" or "../" ... (relative path)
                        # image_path : url(./image/back-image.png) -> ./image/back-image.png
                        image_path = value.replace("url(", "").replace(")", "")
                        if (image_path[0] == "." or image_path[0] == ".."):
                            basepath = os.path.dirname(self.yaml_abs_path_file)
                            image_path = os.path.join(basepath, image_path)
                            print("------------------")
                            print("image_path: " + image_path)
                            print("------------------")
                        self.stylesheet_str += key + ": url(" + image_path + ");"
                    else:
                        self.stylesheet_str += key + ": " + str(value) + "; "
        else:
            self.stylesheet_str = ""

    # Label Rect ---------------------------------------------------------------
        if "rect" in self.yaml_data:
            # if include key
            if "include" in self.yaml_data["rect"]:
                path = self.yaml_data["rect"]["include"]["path"]
                key = self.yaml_data["rect"]["include"]["key"]
                # is url or path
                if self.is_url(path):
                    # is url -> save to ~/.cache/pyamlqt/yaml/***.yaml and load it
                    # exists ~/.cache/pyamlqt/yaml/***.yaml ?
                    if not os.path.exists(os.path.expanduser("~/.cache/pyamlqt/yaml/" + os.path.basename(path))):
                        os.makedirs(os.path.expanduser(
                            "~/.cache/pyamlqt/yaml"), exist_ok=True)
                        urllib.request.urlretrieve(path, os.path.expanduser(
                            "~/.cache/pyamlqt/yaml/") + os.path.basename(path))
                        print("download yaml file: " + path)
                    else:
                        print("yaml file is already downloaded (Please delete ~/.cache/pyamlqt/yaml/" +
                              os.path.basename(path) + " to download again)")
                    path = os.path.expanduser(
                        "~/.cache/pyamlqt/yaml/") + os.path.basename(path)

                with open(path, 'r') as f:
                    self.rect_width = yaml.load(f, Loader=yaml.FullLoader)
                self.rect_width = label_configure(path, key).rect_width
                self.rect_height = label_configure(path, key).rect_height
            else:
                self.rect_width = self.yaml_data["rect"]["width"]
                self.rect_height = self.yaml_data["rect"]["height"]
        else:
            pass
            self.rect_width = self.width
            self.rect_height = self.height

        self.width = self.rect_width
        self.height = self.rect_height

    # debug ---------------------------------------------------------------
        if "debug" in self.yaml_data:
            self.debug = self.yaml_data["debug"]
        else:
            self.debug = False

        self.x = self.x_center - self.width // 2
        self.y = self.y_center - self.height // 2

        # print all
        if self.debug:
            print("==========================================================")
            print("loading " + self.yaml_abs_path_file)
            print("type:" + str(self.type))
            print("x_center: " + str(self.x_center))
            print("y_center: " + str(self.y_center))
            print("width: " + str(self.width))
            print("height: " + str(self.height))
            print("text: " + str(self.text))
            print("font_size: " + str(self.font_size))
            print("font_color: " + str(self.font_color))
            print("background_color: " + str(self.background_color))
            print("font: " + str(self.font))
            print("font_bold: " + str(self.font_bold))
            print("items: " + str(self.items))
            print("max: " + str(self.max))
            print("min: " + str(self.min))
            print("step: " + str(self.step))
            print("default: " + str(self.default))
            print("path: " + str(self.path))
            print("stylesheet_str: " + str(self.stylesheet_str))
            print("==========================================================")


import requests
import os
from jose import jwk, jwt
from jose.utils import base64url_decode
import time
import re

# The below token validation is based on
# https://github.com/awslabs/aws-support-tools/blob/master/Cognito/decode-verify-jwt/decode-verify-jwt.py
# and https://aws.amazon.com/blogs/mobile/integrating-amazon-cognito-user-pools-with-api-gateway/
def validate_token(token):
    if not token:
        return False

    keys = requests.get(
        f'{os.environ["USER_POOL_ENDPOINT"]}/.well-known/jwks.json'
    ).json()["keys"]

    # Get the kid from the headers prior to verification
    headers = jwt.get_unverified_headers(token)

    # Search for the kid of the token in the downloaded public keys
    try:
        key = next(key for key in keys if key["kid"] == headers["kid"])
    except StopIteration:
        return False

    # Construct the public key
    public_key = jwk.construct(key)

    # Get the last two sections of the token, message and signature (encoded in base64)
    message, encoded_signature = str(token).rsplit(".", 1)

    # Decode the signature
    decoded_signature = base64url_decode(encoded_signature.encode("utf-8"))

    # Verify the signature
    if not public_key.verify(message.encode("utf8"), decoded_signature):
        # Signature verification failed
        return False

    # Signature successfully verified
    claims = jwt.get_unverified_claims(token)

    # Verify the token expiration
    if time.time() > claims["exp"]:
        # Token is expired
        return False

    # Verify the token issuer
    if claims["iss"] != os.environ["USER_POOL_ENDPOINT"]:
        # Token was not issued by this user pool
        return False

    return claims

def authenticate(event, context):
    claims = validate_token(event.get("authorizationToken"))
    if not claims:
        return False

    policy = AuthPolicy(claims["username"])

    method_arn = event["methodArn"].split(":")
    api_gateway = method_arn[5].split("/")
    policy.stage = api_gateway[1]

    policy.awsAccountId = method_arn[4]
    policy.restApiId = api_gateway[0]
    policy.region = method_arn[3]

    policy.allowAllMethods()

    response = policy.build()
    response["context"] = {"cognito:groups": ",".join(claims.get("cognito:groups", []))}

    return response

class HttpVerb:
    GET = "GET"
    POST = "POST"
    PUT = "PUT"
    PATCH = "PATCH"
    HEAD = "HEAD"
    DELETE = "DELETE"
    OPTIONS = "OPTIONS"
    ALL = "*"

# The below policy generator was sourced from the Lambda blueprint titled
# "api-gateway-authorizer-python"
class AuthPolicy(object):
    # The principal used for the policy, this should be a unique identifier for the end user.
    principalId = ""
    # The policy version used for the evaluation. This should always be '2012-10-17'
    version = "2012-10-17"
    # The regular expression used to validate resource paths for the policy
    pathRegex = "^[/.a-zA-Z0-9-\*]+$"

    """Internal lists of allowed and denied methods.

    These are lists of objects and each object has 2 properties: A resource
    ARN and a nullable conditions statement. The build method processes these
    lists and generates the approriate statements for the final policy.
    """
    allowMethods = []
    denyMethods = []

    # The AWS account id the policy will be generated for. This is used to create the method ARNs.
    # By default this is set to '*'
    awsAccountId = "*"
    # The API Gateway API id. By default this is set to '*'
    restApiId = "*"
    # The region where the API is deployed. By default this is set to '*'
    region = "*"
    # The name of the stage used in the policy. By default this is set to '*'
    stage = "*"

    def __init__(self, principal):
        self.principalId = principal
        self.allowMethods = []
        self.denyMethods = []

    def _addMethod(self, effect, verb, resource, conditions):
        """Adds a method to the internal lists of allowed or denied methods. Each object in
        the internal list contains a resource ARN and a condition statement. The condition
        statement can be null."""
        if verb != "*" and not hasattr(HttpVerb, verb):
            raise NameError(
                "Invalid HTTP verb " + verb + ". Allowed verbs in HttpVerb class"
            )
        resourcePattern = re.compile(self.pathRegex)
        if not resourcePattern.match(resource):
            raise NameError(
                "Invalid resource path: "
                + resource
                + ". Path should match "
                + self.pathRegex
            )

        if resource[:1] == "/":
            resource = resource[1:]

        resourceArn = "arn:aws:execute-api:{}:{}:{}/{}/{}/{}".format(
            self.region, self.awsAccountId, self.restApiId, self.stage, verb, resource
        )

        if effect.lower() == "allow":
            self.allowMethods.append(
                {"resourceArn": resourceArn, "conditions": conditions}
            )
        elif effect.lower() == "deny":
            self.denyMethods.append(
                {"resourceArn": resourceArn, "conditions": conditions}
            )

    def _getEmptyStatement(self, effect):
        """Returns an empty statement object prepopulated with the correct action and the
        desired effect."""
        statement = {
            "Action": "execute-api:Invoke",
            "Effect": effect[:1].upper() + effect[1:].lower(),
            "Resource": [],
        }

        return statement

    def _getStatementForEffect(self, effect, methods):
        """This function loops over an array of objects containing a resourceArn and
        conditions statement and generates the array of statements for the policy."""
        statements = []

        if len(methods) > 0:
            statement = self._getEmptyStatement(effect)

            for curMethod in methods:
                if curMethod["conditions"] is None or len(curMethod["conditions"]) == 0:
                    statement["Resource"].append(curMethod["resourceArn"])
                else:
                    conditionalStatement = self._getEmptyStatement(effect)
                    conditionalStatement["Resource"].append(curMethod["resourceArn"])
                    conditionalStatement["Condition"] = curMethod["conditions"]
                    statements.append(conditionalStatement)

            if statement["Resource"]:
                statements.append(statement)

        return statements

    def allowAllMethods(self):
        """Adds a '*' allow to the policy to authorize access to all methods of an API"""
        self._addMethod("Allow", HttpVerb.ALL, "*", [])

    def denyAllMethods(self):
        """Adds a '*' allow to the policy to deny access to all methods of an API"""
        self._addMethod("Deny", HttpVerb.ALL, "*", [])

    def allowMethod(self, verb, resource):
        """Adds an API Gateway method (Http verb + Resource path) to the list of allowed
        methods for the policy"""
        self._addMethod("Allow", verb, resource, [])

    def denyMethod(self, verb, resource):
        """Adds an API Gateway method (Http verb + Resource path) to the list of denied
        methods for the policy"""
        self._addMethod("Deny", verb, resource, [])

    def allowMethodWithConditions(self, verb, resource, conditions):
        """Adds an API Gateway method (Http verb + Resource path) to the list of allowed
        methods and includes a condition for the policy statement. More on AWS policy
        conditions here: http://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements.html#Condition"""
        self._addMethod("Allow", verb, resource, conditions)

    def denyMethodWithConditions(self, verb, resource, conditions):
        """Adds an API Gateway method (Http verb + Resource path) to the list of denied
        methods and includes a condition for the policy statement. More on AWS policy
        conditions here: http://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements.html#Condition"""
        self._addMethod("Deny", verb, resource, conditions)

    def build(self):
        """Generates the policy document based on the internal lists of allowed and denied
        conditions. This will generate a policy with two main statements for the effect:
        one statement for Allow and one statement for Deny.
        Methods that includes conditions will have their own statement in the policy."""
        if (self.allowMethods is None or len(self.allowMethods) == 0) and (
            self.denyMethods is None or len(self.denyMethods) == 0
        ):
            raise NameError("No statements defined for the policy")

        policy = {
            "principalId": self.principalId,
            "policyDocument": {"Version": self.version, "Statement": []},
        }

        policy["policyDocument"]["Statement"].extend(
            self._getStatementForEffect("Allow", self.allowMethods)
        )
        policy["policyDocument"]["Statement"].extend(
            self._getStatementForEffect("Deny", self.denyMethods)
        )

        return policy


import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import datetime as dt

import requests_db as rdb
import data_save

def create_pie(date, year = None):

    if len(date) == 4:          # Проверяем на запрос, на год
        year = date

    data = rdb.operation_list(date, only_table='expense', year=year)
    if not data:
        return
    df = pd.DataFrame(data)
    df = df.groupby(1).agg({2: np.sum}).sort_values(2, ascending=False)
    amount = df[2].values
    name_cat = [f'{data_save.all_category_dict[cat]}\n {num:,}' \
        for cat, num in zip(df.index, amount)]

    fig1, ax1 = plt.subplots(figsize=(10,10))

    wedges, texts, autotexts = ax1.pie(
        amount,
        labels=name_cat,
        textprops={'fontsize':14, 'color':'k'},
        autopct='%1.2f%%',
        startangle=180
        )

    ax1.axis('equal')
    ax1.legend(loc='upper right', bbox_to_anchor=(1.0, 1.0), fontsize=14)
    ax1.set_title("Расходы", size=15, weight="bold")
    plt.setp(autotexts, size=13, weight="bold")
    plt.savefig('data/saved_figure.png')

    return True

def plots_year_data_preparation(data):
    df = pd.DataFrame(data, columns=['id', 'category', 'amount', 'date'])
    df.date = df.date.apply(lambda x: data_save.inverted_month_dict[x[5:-3]])
    df.category = df.category.apply(lambda x: data_save.all_category_dict[x])

    year = df.groupby(['date', 'category']).agg({'amount': np.sum}).sort_index(ascending=False)
    data_dict = year.amount.to_dict()
    months = list(data_save.month_dict.keys())[:len((set(df.date.values)))]
    category = list(set(df.category.values))

    data_list = [[data_dict.get((month, cat), 0) for month in months] for cat in category]

    months_sum_dict = df.groupby('date').agg({'amount': np.sum}).sort_index(ascending=False).amount.to_dict()

    return (months, data_list, category), months_sum_dict

def create_plots(data, title):
    months, data_list, category = data
    fig, ax = plt.subplots(1, 1, figsize=(5 + len(months), 10))
    ax.spines[:].set_visible(False)

    plt.title(title, size=15, weight="bold")
    plt.xlabel('Месяц', size=14)
    plt.ylabel('Значение', size=14)

    for i, y_list in enumerate(data_list):
        plt.plot(months, y_list, label=category[i], marker='>')
        for x, y in enumerate(y_list):
            plt.text(x, y * 1.02, y, horizontalalignment='center', fontsize=12)

    plt.legend(fontsize=13)
    plt.savefig(f'data/plot_{title}.png')

def year_plots(year):
    income, expense = rdb.operation_list(year=year, only_table='all')
    if not income and not expense:
        return

    data_income, dict_income = plots_year_data_preparation(income)
    create_plots(data_income, 'Incomes')

    data_expense, dict_expense = plots_year_data_preparation(expense)
    create_plots(data_expense, 'Expense')

    month = dt.datetime.now().month
    months = list(data_save.month_dict.keys())[:month]
    category = ['Доход', 'Расход']
    data = [[d.get(month, 0) for month in months]for d in (dict_income, dict_expense)]
    data_set = (months, data, category)
    create_plots(data_set, 'Incomes and Expense')

    return True

if __name__ == '__main__':
	create_pie('2022', year='2022')


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Oct 28 16:52:55 2022

@author: anselmo

Probabilidade
"""

# DISTRIBUIÇÃO CONTINUA

def uniform_cdf(x: float)-> float:
    "Retorna  a probabilidade de uma variável aleatória uniforme ser < = x"
    if x < 0:
        return 0 # a aleatória uniforme nunca será menor que 0
    elif x < 1:
        return x # ex P(x ,= 0.4) = 0.4
    else:
        return 1 # a aleatória uniforme semre é menor do que 1

# DISTRIBUIÇÃO NORMAL

import matplotlib.pyplot as plt
import math

SQRT_TWO_PI =math.sqrt(2 * math.pi)

def normal_pdf(x:float, mu:float = 0, sigma: float =1) -> float:
    return (math.exp(-(x-mu) ** 2 / 2 / sigma ** 2) / (SQRT_TWO_PI * sigma))

# platagem de alguma PDFs para confeir o visual

xs = [x / 10.0 for x in range(-50,50)]

plt.plot(xs,
        [normal_pdf(x, sigma =1) for x in xs],
        '-',
        label= "mu=0, sifma=1")

plt.plot(xs,
         [normal_pdf(x, sigma=2) for x in xs],
         '--',
         label = "mu=0, sigma=2")

plt.plot(xs,
         [normal_pdf(x, sigma = 0.5) for x in xs],
         ':',
         label= "mu=u, sigma = 0.5")

plt.plot(xs,
         [normal_pdf(x, mu=-1) for x in xs],
         '-.',
         label = "mu=-1, sigma=1")
plt.legend(loc=1)
plt.title("Various Normal pdfs")
#plt.savefig("..//img/figura06_02.png")
plt.show()

# DISTRIBUIÇÃO NORMAL PADRÃO

def normal_cdf(x:float, mu:float=0, sigma: float = 1 )-> float:
    return (1 + math.erf((x-mu) / math.sqrt(2) / sigma)) / 2

# Plotando algumas CDFs de exemplo

xs = [x / 10.0 for x in range(-50,50)]

plt.plot(xs,
         [normal_cdf(x,sigma =1) for x in xs],
         "-",
         label = "mu=o, sigma = 1")
plt.plot(xs,
         [normal_cdf(x, sigma=2) for x in xs],
         "--",
         label="mu=0, sigma = 2")
plt.plot(xs,
         [normal_cdf(x, sigma =0.5) for x in xs],
         ":",
         label = "mu=0, sigma = 0.5")
plt.plot(xs,
         [normal_cdf(x, mu =-1)for x in xs],
         "-.",
         label = "mu=-1, sigma =1")
plt.legend(loc=4) # no canto direito
plt.title("Various Normal cdfs")
#plt.savefig("..//img/figura06_03.png")
plt.show()

# Pesquisa binária
def inverse_normal_cdf(p:float, mu:float = 0, sigma:float=1, tolerance:float=0.00001)->float:
    """Encontre o inverso aproximado unsadno a pesquisa binária"""
    # se não for padrão, compute o padrão e redimensione
    if mu != 0 or sigma!= 1:
        return mu + sigma * inverse_normal_cdf(p,tolerance=tolerance)

    low_z = -10.0 # normal_cdf(-10) = é (muito próximo de ) 0
    hi_z = 10.0 # normal_cdf(10) é (muito próximo de ) 1

    while hi_z > tolerance:
        mid_z = (low_z + hi_z) /2 # cosidere o ponto médio
        mid_p = normal_cdf(mid_z) # é valor de cdf

        if mid_p < p:
            low_z = mid_z # ponto médio é muito baixo, procure um maior
        else:
            hi_z = mid_z # ponto médio é muito alto, procure um menor
    return mid_z

# O TEOREMA DE LIMITE CENTRAL
import random

def bernoulli_trial(p:float) -> float:
    """Retorna 1 com probabilidade p e 0 com a probabilidade 1-p"""
    return 1 if random.random() < p else 0

def binominal(n:int, p:float)-> int:
    """retorna a soma de n trials bernoulli(p)"""
    return sum(bernoulli_trial(p) for _ in range(n))

# plotando um demostração

from collections import Counter
def binominal_histogram(p:float, n:int, mum_points:int)-> None:
    """seleciona pontos de uma binominal(n,p) e plota se histograma"""
    data = [binominal(n,p) for _ in range(num_points)]
    histogram = Counter(data)

    plt.bar([x - 0.4 for x in histogram.key()],
            [v / num_points for v in histogram.values()],
            0.8,
            color='0.75')
    mu = p * n
    sigma = math.sqrt(n*p(1-p))

    #use um gráfico de linhas para indicar a aproximação normal

    xs = range(min(data), mas(data) + 1)
    ys = [normal_cdf(i + 0.5, mu, sigma) - normal_cdf(i - 0.5, mu, sigma) for x in xs]

    plt.plot(xs,ys)
    plt.title("Biniminal distribuition vs. normal distribuition")

    plt.show()

#***********************************************************************#
def main():
    import enum, random

    """Um enum é um conjunto tipado de valores enumerados que
    deixa # o código mais descritivo e legível"""

    class Kid(enum.Enum):
        BOY = 0
        GIRL = 1

    def random_kid()-> Kid:
        return random.choice([Kid.BOY, Kid.GIRL])

    both_girls = 0
    older_girl = 0
    either_girl = 0

    random.seed(0)

    for _ in range(1000):
        younger = random_kid()
        older = random_kid()
        if older == Kid.GIRL:
            older_girl += 1
        if older == Kid.GIRL and younger == Kid.GIRL:
            both_girls += 1
        if older == Kid.GIRL or younger == Kid.GIRL:
            either_girl +=1

   # print("P(both|older):", both_girls / older_girl)# 0,514 ~1/2
   # print("P(both|either):", both_girls / either_girl) # 0.342 ~ 1/3

    assert 0.48 < both_girls / older_girl < 0.52
    assert 0.30 < both_girls / either_girl < 0.35

if __name__ == "__main__": main()


import sys, os
import numpy as np
from itertools import islice

from fpe_drawing import draw_relative_rate

def load_fpe_data(path, skip_header):
    """
    Columns:
        Bloc__Id
        Bloc_Nam
        Trial_No
        Trial_Id
        TrialNam
        ITIBegin
        __ITIEnd
        StmBegin
        _Latency
        __StmEnd
        RespFreq
        ExpcResp
        __Result

    """
    if not os.path.isfile(path):
        raise "Path was not found:"+path

    data = np.genfromtxt(path,
        delimiter="\t",
        missing_values=["NA"],
        skip_header=skip_header,
        skip_footer=1,
        filling_values=None,
        names=True,
        autostrip=True,
        dtype=None
    )
    return data

def load_fpe_timestamps(path):
    """
    Columns:
        Time
        Bloc__Id
        Trial_Id
        Trial_No
        Event
    """
    if not os.path.isfile(path):
        raise "Path was not found:"+path

    data = np.genfromtxt(path,
        delimiter="\t",
        missing_values=["NA"],
        skip_header=5,
        skip_footer=1,
        filling_values=None,
        names=True,
        autostrip=True,
        dtype=None
    )
    return data

def window(seq, n=2):
    """
    https://docs.python.org/release/2.3.5/lib/itertools-example.html
     "Returns a sliding window (of width n) over data from the iterable"
     "   s -> (s0,s1,...s[n-1]), (s1,s2,...,sn), ...                   "
    """
    it = iter(seq)
    result = tuple(islice(it, n))
    if len(result) == n:
        yield result
    for elem in it:
        result = result[1:] + (elem,)
        yield result

def get_perfomance(data):
    return [line.decode("utf-8") for line in data['__Result']]

def get_session_type(data):
    fp_strings = ['FP', 'Feature Positive']
    fn_strings = ['FN', 'Feature Negative']

    bloc_name = data['Bloc_Nam'][0].decode("utf-8")
    for fp_string in fp_strings:
        if bloc_name in fp_string:
            return 'feature positive'

    for fn_string in fn_strings:
        if bloc_name in fn_string:
            return 'feature negative'

    return None

def plot_fpfn(fp_paths, fn_paths):
    pass

def consecutive_hits(paths, skip_header=13):
    overall_performance = []
    count = 0
    for path in paths:
        count += 1
        data = load_fpe_data(path, skip_header)
        session_type = get_session_type(data)
        trials = data['Trial_Id'].max()
        performance = get_perfomance(data)
        hits = performance.count('HIT')

        print('Session %s'%count)
        print('Session Type: %s'%session_type)
        print('Trials:',trials)
        print('Hits:',hits, '(%s %%)'%round((hits/trials)*100,2),'\n')
        overall_performance += performance

    # consecutive hits
    size = 12
    count = 0
    for performance_chunk in window(overall_performance,size):
        count += 1
        if performance_chunk.count('HIT') == size:
            print('The participant reached %s consecutive hits in trial %s.'%(size,count+size-1),'\n')
            return

    print('The participant DO NOT reached %s consecutive hits in %s trials.'%(size,count+size-1),'\n')

def get_events_per_trial_in_bloc(data, ts, target_bloc=2):
    session = zip(ts['Time'], ts['Bloc__Id'], ts['Trial_No'], ts['Event'])
    events_per_trial = {}
    for time, bloc_id, trial, ev in session:
        event = ev.decode("utf-8")
        if bloc_id == target_bloc:
            if trial not in events_per_trial:
                events_per_trial[trial] = {'Type':'','Time':[],'Event':[]}

            if event == 'ITI R':
                events_per_trial[trial-1]['Time'].append(time)
                events_per_trial[trial-1]['Event'].append(event)
            else:
                events_per_trial[trial]['Time'].append(time)
                events_per_trial[trial]['Event'].append(event)

    session = zip(data['TrialNam'], data['Trial_No'])
    type1 = 'Positiva'
    type2 = 'Negativa'
    for trial_name, trial_number in session:
        name = trial_name.decode('utf-8')
        if type1 in name:
            events_per_trial[trial_number]['Type'] = type1

        if type2 in name:
            events_per_trial[trial_number]['Type'] = type2

    return events_per_trial

def get_trial_starts(trials):
    starts = []
    for i, trial in trials.items():
        for time, event in zip(trial['Time'], trial['Event']):
            if event == 'TS':
                starts.append({'Time':time, 'Type':trial['Type']})

    last_type = list(trials.values())[-1]['Type']
    last_time = starts[-1]['Time']

    l = [end['Time'] - start['Time'] for start, end in zip(starts, starts[1:])]
    last_time += np.mean(l)

    starts.append({'Time':last_time, 'Type':last_type})
    return starts

def get_start_end_intervals_with_iti(starts):
    positive_intervals = []
    negative_intervals = []
    for start, end in zip(starts, starts[1:]):
        if start['Type'] == 'Positiva':
            positive_intervals.append([start['Time'], end['Time']])

        if start['Type'] == 'Negativa':
            negative_intervals.append([start['Time'], end['Time']])

    return positive_intervals, negative_intervals

def get_all_responses(ts):
    return [time for time, event in zip(ts['Time'], ts['Event']) \
        if event.decode('utf-8') == 'R' or 'ITI R']

def rate_in(time_interval_pairwise,timestamps):
    def is_inside(timestamps,rangein, rangeout):
        return [t for t in timestamps if (t >= rangein) and (t <= rangeout)]

    return [len(is_inside(timestamps, begin, end))/(end-begin) for begin, end in time_interval_pairwise]

def get_relative_rate(data1, data2):
    return [a/(b+a) if b+a > 0 else np.nan for a, b in zip(data1, data2)]

def rate(paths, skip_header=13):
    overall_performance = []
    count = 0
    for path in paths:
        data_file = load_fpe_data(path[0],skip_header)
        timestamps_file = load_fpe_timestamps(path[1])
        responses = get_all_responses(timestamps_file)
        trials = get_events_per_trial_in_bloc(data_file,timestamps_file)
        starts = get_trial_starts(trials)
        positive_intervals, negative_intervals = get_start_end_intervals_with_iti(starts)
        positive_data = rate_in(positive_intervals,responses)
        negative_data = rate_in(negative_intervals,responses)
        relative_rate = get_relative_rate(positive_data, negative_data)
        title = path[0].replace('/home/pupil/recordings/DATA/','')
        title = title.replace('/stimulus_control/000.data','')
        title = title.replace('/','_')
        title = title+'_'+get_session_type(data_file)
        title = title.replace(' ', '_')
        draw_relative_rate(relative_rate,title, True)

def get_paths(paths):
    p = []
    for root in paths['root']:
        p.append([
            os.path.join(root, paths['file'][0]),
            os.path.join(root, paths['file'][1])])
    return p

if __name__ == '__main__':

    # paths = [
    #     "/home/pupil/recordings/DATA/2017_02_06/000_ELD/000/stimulus_control/000.data"
    # ]
    # main(paths)

    # paths = [
    #     "/home/pupil/recordings/DATA/2017_04_11/000_HER/001/stimulus_control/000.data"
    # ]
    # consecutive_hits(paths, 14)

    # paths = [
    #     "/home/pupil/recordings/DATA/2017_04_12/000_ATL/000/stimulus_control/000.data",
    #     "/home/pupil/recordings/DATA/2017_04_12/000_ATL/001/stimulus_control/000.data"
    # ]
    # consecutive_hits(paths)

    # paths = [
    #     "/home/pupil/recordings/DATA/2017_04_29/000_DEM/000/stimulus_control/000.data",
    #     "/home/pupil/recordings/DATA/2017_04_29/000_DEM/001/stimulus_control/000.data",
    #     "/home/pupil/recordings/DATA/2017_04_29/000_DEM/002/stimulus_control/000.data"
    # ]
    # consecutive_hits(paths)

    # paths = [
    #     "/home/pupil/recordings/DATA/2017_04_29/000_JES/000/stimulus_control/000.data",
    #     "/home/pupil/recordings/DATA/2017_04_29/000_JES/001/stimulus_control/000.data"
    # ]
    # consecutive_hits(paths)

    # paths = [
    #     "/home/pupil/recordings/DATA/2017_04_29/000_JUL/000/stimulus_control/000.data"
    # ]

    # consecutive_hits(paths)

    d = {
        'root': [
            '/home/pupil/recordings/DATA/2017_04_11/000_HER/001/stimulus_control'
            ],
        'file': ['000.data', '000.timestamps']
        }

    rate(get_paths(d),14)

    d = {
        'root': [
            '/home/pupil/recordings/DATA/2017_04_12/000_ATL/000/stimulus_control',
            '/home/pupil/recordings/DATA/2017_04_12/000_ATL/001/stimulus_control'
            ],
        'file': ['000.data', '000.timestamps']
        }

    rate(get_paths(d))

    d = {
        'root': [
            '/home/pupil/recordings/DATA/2017_04_29/000_DEM/000/stimulus_control',
            '/home/pupil/recordings/DATA/2017_04_29/000_DEM/001/stimulus_control',
            '/home/pupil/recordings/DATA/2017_04_29/000_DEM/002/stimulus_control'

            ],
        'file': ['000.data', '000.timestamps']
        }

    rate(get_paths(d))

    d = {
        'root': [
            '/home/pupil/recordings/DATA/2017_04_29/000_JES/000/stimulus_control',
            '/home/pupil/recordings/DATA/2017_04_29/000_JES/001/stimulus_control'
        ],
        'file': ['000.data', '000.timestamps']
    }

    rate(get_paths(d))

    d = {
        'root': [
            '/home/pupil/recordings/DATA/2017_04_29/000_JUL/000/stimulus_control'
            ],
        'file': ['000.data', '000.timestamps']
        }

    rate(get_paths(d))


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

import time
import json
import requests
from datetime import datetime
import numpy as np
import matplotlib
import matplotlib.figure
from matplotlib.font_manager import FontProperties
from matplotlib.backends.backend_agg import FigureCanvasAgg
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

plt.rcParams['font.sans-serif'] = ['FangSong']  # 设置默认字体
plt.rcParams['axes.unicode_minus'] = False  # 解决保存图像时'-'显示为方块的问题

def catch_daily():
    """抓取每日确诊和死亡数据"""

    date_list = list() # 日期
    confirm_list = list() # 确诊
    suspect_list = list() # 疑似
    dead_list = list() # 死亡
    heal_list = list() # 治愈

    url = 'https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5&callback=&_=%d'%int(time.time()*1000)
    for item in json.loads(requests.get(url=url).json()['data'])['chinaDayList']:
        month, day = item['date'].split('.')
        date_list.append(datetime.strptime('2020-%s-%s'%(month, day), '%Y-%m-%d'))
        confirm_list.append(int(item['confirm']))
        suspect_list.append(int(item['suspect']))
        dead_list.append(int(item['dead']))
        heal_list.append(int(item['heal']))

    return date_list, confirm_list, suspect_list, dead_list, heal_list

def catch_distribution():
    """抓取行政区域确诊分布数据"""

    data = {}
    url = 'https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5&callback=&_=%d'%int(time.time()*1000)
    for item in json.loads(requests.get(url=url).json()['data'])['areaTree'][0]['children']:
        if item['name'] not in data:
            data.update({item['name']:0})
        for city_data in item['children']:
            data[item['name']] += int(city_data['total']['confirm'])

    return data

def plot_daily():
    """绘制每日确诊和死亡数据"""

    date_list, confirm_list, suspect_list, dead_list, heal_list = catch_daily() # 获取数据

    plt.figure('2019-nCoV疫情统计图表', facecolor='#f4f4f4', figsize=(10, 8))
    plt.title('2019-nCoV疫情曲线', fontsize=20)

    plt.plot(date_list, confirm_list, label='确诊')
    plt.plot(date_list, suspect_list, label='疑似')
    plt.plot(date_list, dead_list, label='死亡')
    plt.plot(date_list, heal_list, label='治愈')

    plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%m-%d')) # 格式化时间轴标注
    plt.gcf().autofmt_xdate() # 优化标注（自动倾斜）
    plt.grid(linestyle=':') # 显示网格
    plt.legend(loc='best') # 显示图例
    plt.savefig('fb/2019-nCoV疫情曲线.png') # 保存为文件
    plt.show()

def plot_distribution(mode='lcc', plot_name=True):
    """绘制行政区域确诊分布数据

    mode        - 投影模式，可选项：
                -- 'lcc'            兰博托投影
                -- 'ortho'          正射投影
                -- 'etopo'          全球等经纬投影模式，etopo风格
                -- 'shadedrelief'   全球等经纬投影模式，shadedrelief风格
                -- 'bluemarble'     全球等经纬投影模式，bluemarble风格
    plot_name   - 是否打印行政区域名
    """

    data = catch_distribution()

    font_14 = FontProperties(fname='res/simsun.ttf', size=14)
    font_11 = FontProperties(fname='res/simsun.ttf', size=11)

    if mode in ['etopo', 'shadedrelief', 'bluemarble']:
        width = 1600
        height = 800
        rect = [0, 0, 1, 1]
        lat_min = -90
        lat_max = 90
        lon_min = 0
        lon_max = 360
    else:
        width = 1200
        height = 800
        rect = [0.1, 0.12, 0.8, 0.8]
        lat_min = 0
        lat_max = 60
        lon_min = 77
        lon_max = 140

    handles = [
            matplotlib.patches.Patch(color='#ffaa85', alpha=1, linewidth=0),
            matplotlib.patches.Patch(color='#ff7b69', alpha=1, linewidth=0),
            matplotlib.patches.Patch(color='#bf2121', alpha=1, linewidth=0),
            matplotlib.patches.Patch(color='#7f1818', alpha=1, linewidth=0),
]
    labels = [ '1-9人', '10-99人', '100-999人', '>1000人']

    provincePos = {
        "辽宁省":[121.7,40.9],
        "吉林省":[124.5,43.5],
        "黑龙江省":[125.6,46.5],
        "北京市":[116.0,39.9],
        "天津市":[117.0,38.7],
        "内蒙古自治区":[110.0,41.5],
        "宁夏回族自治区":[105.2,37.0],
        "山西省":[111.0,37.0],
        "河北省":[114.0,37.8],
        "山东省":[116.5,36.0],
        "河南省":[111.8,33.5],
        "陕西省":[107.5,33.5],
        "湖北省":[111.0,30.5],
        "江苏省":[119.2,32.5],
        "安徽省":[115.5,31.8],
        "上海市":[121.0,31.0],
        "湖南省":[110.3,27.0],
        "江西省":[114.0,27.0],
        "浙江省":[118.8,28.5],
        "福建省":[116.2,25.5],
        "广东省":[113.2,23.1],
        "台湾省":[120.5,23.5],
        "海南省":[108.0,19.0],
        "广西壮族自治区":[107.3,23.0],
        "重庆市":[106.5,29.5],
        "云南省":[101.0,24.0],
        "贵州省":[106.0,26.5],
        "四川省":[102.0,30.5],
        "甘肃省":[103.0,35.0],
        "青海省":[95.0,35.0],
        "新疆维吾尔自治区":[85.5,42.5],
        "西藏自治区":[85.0,31.5],
        "香港特别行政区":[115.1,21.2],
        "澳门特别行政区":[112.5,21.2]
    }

    fig = matplotlib.figure.Figure()
    fig.set_size_inches(width/100, height/100) # 设置绘图板尺寸
    axes = fig.add_axes(rect)

    if mode == 'lcc': # 兰博托投影模式
        m = Basemap(projection='lcc', llcrnrlon=77, llcrnrlat=14, urcrnrlon=140, urcrnrlat=51, lat_1=33, lat_2=45, lon_0=100, ax=axes)

        # 兰博托投影模式，全图
        #m = Basemap(projection='lcc', llcrnrlon=80, llcrnrlat=0, urcrnrlon=140, urcrnrlat=51, lat_1=33, lat_2=45, lon_0=100, ax=axes)
    elif mode == 'ortho': # 正射投影模式
        m = Basemap(projection='ortho', lat_0=36, lon_0=102, resolution='l', ax=axes)
    else: # 全球等经纬投影模式，
        m = Basemap(llcrnrlon=lon_min, urcrnrlon=lon_max, llcrnrlat=lat_min, urcrnrlat=lat_max, resolution='l', ax=axes)
        if mode == 'etopo':
            m.etopo()
        elif mode == 'shadedrelief':
            m.shadedrelief()
        else:
            m.bluemarble()

    m.readshapefile('res/china-shapefiles-master/china', 'province', drawbounds=True)
    m.readshapefile('res/china-shapefiles-master/china_nine_dotted_line', 'section', drawbounds=True)
    m.drawcoastlines(color='black') # 洲际线
    m.drawcountries(color='black')  # 国界线

    if mode in ['etopo', 'shadedrelief', 'bluemarble']:
        m.drawparallels(np.arange(lat_min,lat_max,30), labels=[1,0,0,0]) #画经度线
        m.drawmeridians(np.arange(lon_min,lon_max,30), labels=[0,0,0,1]) #画纬度线
    else:
        m.drawparallels(np.arange(lat_min,lat_max,10), labels=[1,0,0,0]) #画经度线
        m.drawmeridians(np.arange(lon_min,lon_max,10), labels=[0,0,0,1]) #画纬度线

    pset = set()
    for info, shape in zip(m.province_info, m.province):
        pname = info['OWNER'].strip('\x00')
        fcname = info['FCNAME'].strip('\x00')
        if pname != fcname: # 不绘制海岛
            continue

        for key in data.keys():
            if key in pname:
                if data[key] == 0:
                    color = '#f0f0f0'
                elif data[key] < 10:
                    color = '#ffaa85'
                elif data[key] <100:
                    color = '#ff7b69'
                elif  data[key] < 1000:
                    color = '#bf2121'
                else:
                    color = '#7f1818'
                break

        poly = Polygon(shape, facecolor=color, edgecolor=color)
        axes.add_patch(poly)

        if plot_name:
            pos = provincePos[pname]
            text = pname.replace("自治区", "").replace("特别行政区", "").replace("壮族", "").replace("维吾尔", "").replace("回族", "").replace("省", "").replace("市", "")
            if text not in pset:
                x,  y = m(pos[0], pos[1])
                axes.text(x,  y, text, fontproperties=font_11, color='#00FFFF')
                pset.add(text)

    axes.legend(handles, labels, bbox_to_anchor=(0.5, -0.11), loc='lower center', ncol=4, prop=font_14)
    axes.set_title("2019-nCoV疫情地图", fontproperties=font_14)
    FigureCanvasAgg(fig)
    fig.savefig('fb/2019-nCoV疫情地图_%s.png'%mode)

if __name__ == '__main__':
    #plot_daily()
    #plot_distribution()
    #plot_distribution(mode='ortho', plot_name=False)
    plot_distribution(mode='etopo', plot_name=False)
    #plot_distribution(mode='shadedrelief', plot_name=False)
    #plot_distribution(mode='bluemarble', plot_name=False)


#!/usr/bin/env python

import pandas as pd
import json
from pyvttbl import DataFrame
from pyvttbl.plotting import *
from collections import namedtuple

class VarianceAnalyzer(object):
    """
    This script reads in a list of learner profiles and a list of their scores (each is a json object) then performs
    an analysis of variance (ANOVA) to suggest which groups of learners are improving their marksmanship knowledge by
    completing the computer based training.
    """
    def __init__(self, learners, pre_scores, post_scores, data_format):
        """
        from learners we need their email identifier and their marksmanship rating
        from the two score lists we need the learner's total pre-lesson score and post-lesson score
        """
        super(VarianceAnalyzer, self).__init__()

        if data_format == 'csv':
            pre_csv = os.path.join(pre_scores, "learner_responses_pre-test.csv")
            post_csv = os.path.join(post_scores, "learner_responses_post-test.csv")
            self.pre_data = pd.read_csv(pre_csv)
            self.post_data = pd.read_csv(post_csv)
        elif data_format == 'json':
            # Read in the pre-lesson test results
            pre_json = []
            file_list = os.listdir(pre_scores)
            for f in file_list:
                if f.endswith(".json"):
                    of = open(os.path.join(pre_scores, f))
                    data = json.load(of)
                    pre_json.append(data)
            # Load the results into a dataframe
            self.pre_data = pd.DataFrame(pre_json)
            # Read in the post-less on test results
            post_json = []
            file_list = os.listdir(post_scores)
            for f in file_list:
                if f.endswith(".json"):
                    of = open(os.path.join(post_scores, f))
                    data = json.load(of)
                    post_json.append(data)
            # Load the results into a dataframe
            self.post_data = pd.DataFrame(post_json)

        # Read all of the learner profiles from the class
        def readClassProfiles(json_file):
            data = pd.read_json(json_file, orient='records')
            return data

        # Read in individual data from individual learner profile
        def readIndividualLearner(dict_list):
            data = pd.DataFrame.from_dict(dict_list[0], orient='index')
            for each_dict in dict_list[1:]:
                this_data = pd.DataFrame.from_dict(each_dict, orient='index')
                data.append(this_data)
            return data

        def getDiff(row):
            return row['OverallScore_post'] - row['OverallScore_pre']

        self.profiles = readClassProfiles(learners)
        self.profiles = pd.DataFrame.transpose(self.profiles)

        self.scores = pd.merge(self.pre_data, self.post_data, on='LearnerID', suffixes=['_pre', '_post'])

        self.df = pd.merge(self.scores, self.profiles, left_on='LearnerID', right_on='email_id', how='outer')
        self.df['pos_diffs'] = self.df.apply(lambda row: max(getDiff(row), 0), axis=1)
        self.df['diffs'] = self.df.apply(lambda row: getDiff(row), axis=1)
        self.df['adj_diffs'] = self.df.apply(lambda row: row['diffs'] + 100, axis=1)

        fps = []
        def gamer(s):
            if s == 0: fps.append('no')
            else: fps.append('yes')

        self.df.apply(lambda row: gamer(row['is_gamer']), axis=1)
        self.df['fps'] = fps

        ## Save off the combined dataframe as a csv
        combined_data = "../data/class_data/learners_wTests.csv"
        self.df.to_csv(combined_data, orient="records")

        ## This side is all data munging to build the database
        ###############################################################################
        ## This side is all about adjusting the lengths of columns

        def accumulate(row):
            return "_".join([row['qualification_performance'], row['education_level'], row['fps']])

        self.df['subs'] = self.df.apply(lambda row: accumulate(row), axis=1)

        combo_sizes = self.df['subs'].value_counts()
        combo_sample = min(combo_sizes)
        print(combo_sample)

        qp_sizes = self.df['qualification_performance'].value_counts()
        qp_sample = min(qp_sizes)
        print(qp_sample)

        edu_sizes = self.df['education_level'].value_counts()
        edu_sample = min(edu_sizes)
        print(edu_sample)

        fps_sizes = self.df['fps'].value_counts()
        fps_sample = min(fps_sizes)
        print(fps_sample)

        short_qp_dfs = []
        for level in list(set(self.df['qualification_performance'])):
            simple = self.df[self.df['qualification_performance'] == level]
            short = simple.sample(qp_sample)
            short_qp_dfs.append(short)
        sample_qps = pd.concat(short_qp_dfs)

        short_ed_dfs = []
        for level in list(set(self.df['education_level'])):
            simple = self.df[self.df['education_level'] == level]
            short = simple.sample(edu_sample)
            short_ed_dfs.append(short)
        sample_eds = pd.concat(short_ed_dfs)

        short_fp_dfs = []
        for level in list(set(self.df['fps'])):
            simple = self.df[self.df['fps'] == level]
            short = simple.sample(fps_sample)
            short_fp_dfs.append(short)
        sample_fps = pd.concat(short_fp_dfs)

        # Pivot Table methods for ANOVA
        def str_list(some_array):
            the_list = some_array.tolist()
            the_elements = [str(x) for x in the_list]
            return the_elements

        def int_list(some_array):
            the_list = some_array.tolist()
            the_elements = [int(x) for x in the_list]
            return the_elements

        def get_w(anova):
            top = anova['ssbn'] - anova['dfbn'] * anova['mswn']
            bottom = anova['ssbn'] + anova['sswn'] + anova['mswn']
            return top / bottom

        def do_anovas(some_df, variable):
            '''
            This method takes a dataframe,  returns the pyvttbl
            anova object for that element
            '''
            pyv_df = DataFrame()

            if variable == 'qp':
                pyv_df['qual'] = str_list(some_df['qualification_performance'])
                pyv_df['vals'] = int_list(some_df['adj_diffs'])

            elif variable == 'ed':
                pyv_df['qual'] = str_list(some_df['education_level'])
                pyv_df['vals'] = int_list(some_df['adj_diffs'])

            elif variable == 'fp':
                pyv_df['qual'] = str_list(some_df['fps'])
                pyv_df['vals'] = int_list(some_df['adj_diffs'])

            else:
                return None

            anova = pyv_df.anova1way('vals', 'qual')
            anova['omega-sq'] = get_w(anova)

            return anova

        qpw_anova = do_anovas(sample_qps, 'qp')
        edw_anova = do_anovas(sample_eds, 'ed')
        fpw_anova = do_anovas(sample_fps, 'fp')

        # Rank order variables
        Rank = namedtuple('Rank', 'var omega')

        qpt = Rank(var='Marksmanship', omega=qpw_anova['omega-sq'])
        fpt = Rank(var='FPS_experience', omega=fpw_anova['omega-sq'])
        edt = Rank(var='Education', omega=edw_anova['omega-sq'])

        ranks = [qpt, fpt, edt]
        ranks = sorted(ranks, key=lambda x: x.omega)

        ## Huom! It may or may not be necessary to elementally convert the numpy.ndarrays into plain python lists and
        #  elementally convert numpy complex data types into plain python data types, e.g., numpy.int64 into int.
        #
        #  Definitely! It is necessary to sample the population data to get samples where the size of each sub-category
        #  is the same length. For example, if there are 5 marksmanship cateogories then we need 30n x 5categories = 150
        #  elements in the pyvttbl dataframe. If we want to use that same dataframe for analysis of education level,
        #  then the sample needs to be 30n x 7levels = 210, but if we want to avoid indirect correlation then the sample
        #  size needs to be 30n x 5cats x 7levels = 1050. ...and 2100 if we want to also look at first person shooter
        #  experience. While it may be ~theoretically~ possible to attempt max(60, 150, 210), since we don't know that
        #  the 210n will also be evenly divided 105 wFPS/105 without, for example.

        def tuple_handler(data_in):
            data_out = {}
            for k, v in data_in.iteritems():
                inner = {}
                for kk, vv in v.iteritems():
                    nk = "_".join(kk)
                    inner[nk] = vv
                data_out[k] = inner
            return data_out

        def tuple_handler2(data_in):
            data_out = {}
            for k, v in data_in.iteritems():
                nk = "_".join(k)
                data_out[nk] = v
            return data_out

        qpj = self.df.qualification_performance.describe().to_dict()
        fpj = self.df.fps.describe().to_dict()
        edj = self.df.education_level.describe().to_dict()
        ooj = self.df.describe().to_dict()

        qgb = (self.df.groupby('qualification_performance')).describe()
        fgb = (self.df.groupby('fps')).describe()
        egb = (self.df.groupby('education_level')).describe()
        oob = "Not Applicable"

        qpg = tuple_handler(qgb)
        fpg = tuple_handler(fgb)
        edg = tuple_handler(egb)

        qptt = tuple_handler2(qpw_anova.multtest)
        fptt = tuple_handler2(fpw_anova.multtest)
        edtt = tuple_handler2(edw_anova.multtest)

        qpd = {"Category": "Marksmanship", "Summary": qpj, "Data": qpg, "ANOVA": qpw_anova, "Ttest": qptt}
        fpd = {"Category": "FPS Experience", "Summary": fpj, "Data": fpg, "ANOVA": fpw_anova, "Ttest": fptt}
        edd = {"Category": "Education", "Summary": edj, "Data": edg, "ANOVA": edw_anova, "Ttest": edtt}
        ood = {"Category": "Overall", "Summary": ooj, "Data": oob, "ANOVA": 'Not Applicable', "Ttest":'Not Applicable'}

        descriptions = [qpd, fpd, edd, ood]

        ## Compact Data
        def compactor(big_dict):
            cat = big_dict['Category']
            datas = []

            for k, v in big_dict['Data']['adj_diffs'].iteritems():
                means = {}
                if k.endswith('_mean'):
                    means['attr'] = k
                    means['value'] = v
                    datas.append(means)
                    #means[k]=v
            f = big_dict['ANOVA']['f']
            p = big_dict['ANOVA']['p']
            w = big_dict['ANOVA']['omega-sq']
            t = {}
            for k, v in big_dict['Ttest'].iteritems():
                nv = {}
                nv['q'] = v['q']
                nv['sig'] = v['sig']
                t[k] = nv
            return [{'KEY': cat, 'Data': datas, 'ANOVA F-stat': f, 'ANOVA p-value': p, 'ANOVA w2': w, 'Ttest': t}]
            intro = {'KEY': cat, 'Data':datas}
            return [intro]

        for d in descriptions[0:-1]:
            cd = compactor(d)
            with open(DATA_DIR + "results/" + d['Category'] + "_compact-v2.json", "w") as data_out:
                json.dump(cd, data_out)
            with open(DATA_DIR + "results/" + d['Category'] + ".json", "w") as data_out:
                json.dump(d, data_out)

        f = open(DATA_DIR + "results/" + "overall.json", "w")
        json.dump(descriptions[0], f)
        f.close()

        f = open(DATA_DIR + "results/" + "var-rankings.json", "w")
        json.dump(ranks, f, sort_keys=True)
        f.close()


import cv2

# Capture the video from the default camera
webcam_video_stream = cv2.VideoCapture("../Videos/Compressed/Bella_Ciao.mp4")

face_detection_classifier = cv2.CascadeClassifier("../Models/haarcascades/haarcascade_frontalface_default.xml")

# Initialize the array varaible to hold all the face locations in the frame
all_face_locations = []

# loop through every frame in the video
while True:
    # get the current frame from the video stream as an image
    ret, current_frame = webcam_video_stream.read()

    # resize the current frame to 1/4 size to process faster
    current_frame_small = cv2.resize(current_frame, (0, 0), fx=0.25, fy=0.25)

    # detect all faces in the image
    all_face_locations = face_detection_classifier.detectMultiScale(current_frame_small)

    for index, current_face_location in enumerate (all_face_locations):

        x, y, width, height = current_face_location

        left_pos = x
        top_pos = y
        right_pos = x + width
        bottom_pos = y + height

        print("Found face {} at top: {}, right: {}, bottom: {}, left: {}".format(index + 1, top_pos, right_pos, bottom_pos, left_pos))

        cv2.rectangle(current_frame, (left_pos, top_pos), (right_pos, bottom_pos), (0, 0, 255), 2)

    cv2.imshow("Web-Cam Video", current_frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# Release the resources
webcam_video_stream.release()
cv2.destroyAllWindows()


from django.shortcuts import render, HttpResponse
from django.contrib.auth import get_user_model
User = get_user_model()
from .models import Video, Comment

def hello(request):
    names = ["Egor", "Petr", "Fedor", "Elena"]
    return render(request, "video.html", {"content": names})
    return HttpResponse("
hello
")

def showall(request):
    videos = Video.objects.all()
    content = []#[(video,[(com,user),(),(),(),...]),(),(),(),.....]
    for vid in videos:
        list_com = []
        comments = Comment.objects.filter(Comment_Video_id = vid.id)
        for com in comments:
            user = User.objects.get(id = com.Comment_User_id)
            list_com.append((com, user))
        content.append((vid, list_com))
    return render(request, "video.html", {"content":content})

def showone(request, video_id):
    video = Video.objects.get(id = video_id)
    comment = Comment.objects.filter(Comment_Video_id=video_id)
    users = []
    for com in comment:
        user = User.objects.get(id=com.Comment_User_id)
        users.append(user)
    return render(request, "showone.html", {"video":video,
                                            "comment":comment,
                                            "users":users})

# Create your views here.


from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QComboBox, QGroupBox, QHBoxLayout

class JoinTypePicker(QGroupBox):

    def __init__(self, brush):
        super().__init__('Join Type')
        self.brush = brush

        self.picker = QComboBox()
        self.picker.addItem('Bevel')
        self.picker.addItem('Miter')
        self.picker.addItem('Round')

        self.__layout()
        self.picker.currentIndexChanged.connect(self.join_type_changed)

    def __layout(self):
        layout = QHBoxLayout()
        layout.addWidget(self.picker)
        self.setLayout(layout)

    def join_type_changed(self):
        """
        Converts the String value from the picker to a valid join type
        and sets it on the brush
        """
        new_type = self.picker.currentText()
        if new_type == 'Bevel':
            self.brush.join_type = Qt.BevelJoin
        elif new_type == 'Miter':
            self.brush.join_type = Qt.MiterJoin
        elif new_type == 'Round':
            self.brush.join_type = Qt.RoundJoin
        else:
            raise ValueError('Invalid join brush_menus type selected')


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('mii_sorter', '0001_initial'),
    ]

    operations = [
        migrations.CreateModel(
            name='MovieQuestionSet',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('movie', models.OneToOneField(on_delete=models.CASCADE, to='mii_sorter.Movie')),
            ],
        ),
        migrations.CreateModel(
            name='QuestionAnswer',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('answer', models.FloatField()),
                ('question_type', models.CharField(max_length=50, choices=[(b'actor', b'actor'), (b'story', b'store'), (b'overall', b'overall'), (b'director', b'director')])),
                ('question_set', models.ForeignKey(to='mii_rating.MovieQuestionSet', on_delete=models.CASCADE)),
            ],
        ),
        migrations.AlterUniqueTogether(
            name='questionanswer',
            unique_together=set([('question_set', 'question_type')]),
        ),
    ]


import cv2
import emilib
import time

camera = cv2.VideoCapture(0)

while True:
    ret, im = camera.read()

    ret, jpg = cv2.imencode(".jpg", im)

    msg = emilib.emi_msg(
        msgnum=1,
        data=jpg.tobytes(),
        ipaddr="127.0.0.1", # Change to the IP where server.py is in
        flag=emilib.emi_flag.EMI_MSG_MODE_BLOCK
    )

    ret = emilib.emi_msg_send(msg)

del(camera)