from turtle import forward
from typing import Optional, Union

from opendelta.utils.signature import get_arg_names, get_arg_names_inside_func
from opendelta.utils.name_based_addressing import *
from opendelta.basemodel import DeltaBase
from transformers.models.t5 import T5ForConditionalGeneration
import torch
import torch.nn as nn
from opendelta import BaseDeltaConfig
import math

class ReparameterizeFunction(nn.Module):
    """
    """
    def __init__(self, in_dim, mid_dim=512):
        super().__init__()
        self.in_dim = in_dim
        self.mid_dim = mid_dim

    def instantiate(self, module_list=[]):
        self.module_list = module_list
        self.record_parameters()
        self.define_reparameterization_network()

    def goto(self, module, path):
        n = module
        for p in path:
            n = getattr(n, p)
        return n

    def record_parameters(self):
        r""" Enumerate the parameters that need to be reparameterized.
        Then, delete the original parameters.
        """
        tot1 = 0
        tot2 = 0
        ith = 0

        self.path_list = []
        for module_idx, module in enumerate(self.module_list):
            self.path_list.append([])
            for n, parameters in module.named_parameters():
                ith ^= 1
                numel = parameters.numel()
                shape = parameters.size()

                names = n.split('.')
                path = names[:-1]
                name = names[-1]
                if(name=="bias" or name=="weight" or name=="lora_A"):
                    tot1 +=numel
                elif(name=="lora_B"):
                    tot2 +=numel
                self.path_list[-1].append((path, name, numel, shape))
            for path, name, _, _ in self.path_list[-1]:
                delattr(self.goto(module, path), name)
        self.total_parameters_num1 = tot1
        self.total_parameters_num2 = tot2

    def define_reparameterization_network(self) -> None:
        r""" Build the reparameterize module
        """
        in_embed1 = torch.empty(self.in_dim)
        nn.init.uniform_(in_embed1)
        self.in_embed1 = nn.Parameter(in_embed1)
        self.transform1 = nn.Sequential(
            nn.Linear(self.in_dim, self.mid_dim, bias=False),
            nn.Tanh(),
            nn.Linear(self.mid_dim, self.total_parameters_num1, bias=False)
        )

        in_embed2 = torch.empty(self.in_dim)
        nn.init.zeros_(in_embed2)
        self.in_embed2 = nn.Parameter(in_embed2)
        self.transform2 = nn.Sequential(
            nn.Linear(self.in_dim, self.mid_dim, bias=False),
            nn.Tanh(),
            nn.Linear(self.mid_dim, self.total_parameters_num2, bias=False)
        )

    def allocate_parameter(self):
        r""" At the beginning of each forward pass through the whole network(PLM),
        cacalulate the reparameterized params for later use in each layer.
        """
        params1 = self.transform1(self.in_embed1)
        params2 = self.transform2(self.in_embed2)

        cur1 = 0
        cur2 = 0
        ith = 0

        for module_idx, module in enumerate(self.module_list):
            for path, name, numel, shape in self.path_list[module_idx]:
                ith ^= 1
                if(name=="bias" or name=="weight" or name=="lora_A"):
                    setattr(self.goto(module, path), name, params1[cur1: cur1 + numel].view(shape))
                    cur1 +=numel
                elif(name=="lora_B"):
                    setattr(self.goto(module, path), name, params2[cur2: cur2 + numel].view(shape))
                    cur2 += numel

    def pre_forward(self, *args, **kwargs):
        r""" Firstly forward through the reparameterized network, and then go through normal forward pass of the PLM.
        """
        self.allocate_parameter()
        return args, kwargs

class LowRankLinear(nn.Module):
    #  ------------------------------------------------------------------------------------------
    #  Copyright (c) Microsoft Corporation. All rights reserved.
    #  Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
    #  ------------------------------------------------------------------------------------------
    #  copy from loralib and do some refactor
    def __init__(self,
        in_features,
        out_features,
        weight,
        r=8,
        lora_alpha=16,
        lora_dropout=0.0,
    ):
        super().__init__()
        self.r = r
        self.lora_alpha = lora_alpha
        self.lora_dropout = lora_dropout
        # self.lin = nn.Linear(in_features, out_features) #
        if lora_dropout > 0.:
            self.lora_dropout = nn.Dropout(p=lora_dropout)
        else:
            self.lora_dropout = lambda x: x
        if r > 0:
            self.lora_A = nn.Parameter(weight.new_zeros((r, in_features)))
            self.lora_B = nn.Parameter(weight.new_zeros((out_features, r)))
            self.scaling = self.lora_alpha / self.r
            # self.lin.reset_parameters() #
            nn.init.kaiming_uniform_(self.lora_A, a=math.sqrt(5))
            nn.init.zeros_(self.lora_B)

    def forward(self, x):
        return (self.lora_dropout(x) @ self.lora_A.T @ self.lora_B.T) * self.scaling

class LoraConfig(BaseDeltaConfig):
    r"""
    This is the configuration class to store the configuration of a :py:class:`~LoraModel`

    """
    def __init__(
        self,
        lora_r=8,
        lora_alpha=16,
        lora_dropout=0.0,
        in_dim =4,
        mid_dim=4,
        reparameterize=False,
        **kwargs
    ):
        super().__init__(**kwargs)
        arg_names = get_arg_names_inside_func(self.__init__)
        for arg_name in arg_names:
            if not hasattr(self, arg_name): # the arg has not been registered in parent config
                setattr(self, arg_name, locals()[arg_name])

class LoraModel(DeltaBase):

    config_class = LoraConfig
    delta_type = "lora"
    default_modified_modules = ['attn.q', 'attn.v']
    def __init__(self,
                 backbone_model: nn.Module,
                 lora_r=8,
                 lora_alpha=16,
                 lora_dropout=0.0,
                 in_dim=4,
                 mid_dim=4,
                 modified_modules: Optional[bool] = None,
                 unfrozen_modules: Optional[bool] = None,
                 common_structure: Optional[bool] = None,
                 interactive_modify: Optional[Union[bool, int]] = False,
                 reparameterize: Optional[bool] = False,
                 ):
        DeltaBase.__init__(self,
                           backbone_model,
                           modified_modules=modified_modules,
                           unfrozen_modules=unfrozen_modules,
                           common_structure=common_structure,
                           interactive_modify=interactive_modify,
                           )
        arg_names = get_arg_names_inside_func(self.__init__)
        for arg_name in arg_names:
            if not hasattr(self, arg_name): # not registered in parent class
                setattr(self, arg_name, locals()[arg_name])
        self.delta_modules = nn.ModuleList()

        if reparameterize:
            reparams = ReparameterizeFunction(
                in_dim = self.in_dim,
                mid_dim = self.mid_dim,
            )
        else:
            reparams = None
        self.delta_modules.append(self.backbone_model.classifier)
        self.add_all_delta_to_backbone(self.backbone_model,
                                   self.modified_modules,
                                   reparams = reparams,
                                   )

    def update_module(self, module: nn.Module, key: str):
        parent_ref, child_name, child_ref = self.find_module(module, key)
        parallel_module = self.new_module_like(child_module=child_ref)
        self.insert_parallel_module(child_ref, delta_module=parallel_module, delta_name="lora")

    def _pseudo_data_to_instantiate(self, module):
        # no need to pass pseudo input, so overwrite it
        pass

    def new_module_like(self, child_module):
        if isinstance(child_module, nn.Linear):
            in_features, out_features = child_module.in_features, child_module.out_features
            new_module = LowRankLinear(in_features = in_features,
                                     out_features = out_features,
                                     weight = child_module.weight,
                                     r = self.lora_r,
                                     lora_alpha = self.lora_alpha,
                                     lora_dropout = self.lora_dropout)
            self.delta_modules.append(new_module)
        else:
            raise NotImplementedError
        return new_module


import colorsys
from random import randint
from time import time

def _hsv2rgb(h, s, v):
    return tuple(round(i * 255) for i in colorsys.hsv_to_rgb(h, s, v))

def _color_brightness(color, brightness):
    # Brightness (float): 0-1
    return (int(brightness * color[0]), int(brightness * color[1]), int(brightness * color[2]))

def _blend(color_1, color_2, percentage_color_1):
    r = int((color_1[0] * percentage_color_1 + color_2[0] * (1 - percentage_color_1)) / 2)
    g = int((color_1[1] * percentage_color_1 + color_2[1] * (1 - percentage_color_1)) / 2)
    b = int((color_1[2] * percentage_color_1 + color_2[2] * (1 - percentage_color_1)) / 2)

    r = min(255, max(0, r))
    g = min(255, max(0, g))
    b = min(255, max(0, b))

    return (r, g, b)

def breathe_color(pixel_count, color_1=(255, 255, 255), color_2=(0, 0, 0), speed=20):
    """Continuously fades between two colors

    Args:
        pixel_count (int): Number of LEDs in the strips $$ no_input
        color_1 (color): The color to fade to
        color_2 (color): The color to fade from
        speed (float): How fast the lights should fade from the one color to the other $$ range(0, 100)

    Yields:
        list: The colors to be output to the pixels
    """
    blend = 0
    while True:
        blended = _blend(color_1, color_2, blend)

        yield [blended] * pixel_count

        blend += speed / 2000

        if blend > 1:
            speed = -abs(speed)
        elif blend < 0:
            speed = abs(speed)
    pass

def solid_color(pixel_count, color=(255, 0, 0)):
    """Sets the lights to a single solid color

    Args:
        pixel_count (int): Number of LEDs in the strips $$ no_input
        color (color): The color to display

    Yields:
        list: The colors to be output to the pixels
    """
    while True:
        yield [color] * pixel_count

def rainbow_breathe(pixel_count, speed=20):
    """Loops all of the pixels simultaneously through a rainbow of colors

    Args:
        pixel_count (int): Number of LEDs in the strips $$ no_input
        speed (float): The speed to loop though the rainbow $$ range(0, 100)

    Yields:
        list: The colors to be output to the pixels
    """
    hue = 0
    while True:
        yield [_hsv2rgb(hue, 1, 1)] * pixel_count
        hue += speed / 2000
        hue %= 1

def rainbow_wave(pixel_count, wave_speed=20, wave_period=100):
    """Scrolls a wave of RGB puke across the light Strips

    Args:
        pixel_count (int): Number of LEDs in the strips $$ no_input
        wave_speed (float): The speed that the wave should move $$ range(0,100)
        wave_period (float): The number of pixels before a color repeats $$ range(1,300)

    Yields:
        list: The colors to be output to the pixels
    """
    offset = 0
    while True:
        yield [_hsv2rgb((offset + i / wave_period) % 1, 1, 1) for i in range(pixel_count)]
        offset += wave_speed / 2000

def stars(pixel_count, starriness=10, color=(255, 255, 255)):
    '''Twinkly little stars

    Args:
        pixel_count (int): Number of LEDS in the strips $$ no_input
        starriness (float): Percentage of the light strip that should be taken by stars $$ range(0, 100)
        color (color): The color of the stars

    Yields:
        list: The colors to be output to the pixels
    '''

    def get_star_count(stars):
        space_count = stars.count(0)
        star_count = len(stars) - space_count
        return star_count

    starriness /= 100.0
    stars = [0] * pixel_count
    while True:
        num_stars = get_star_count(stars)
        star_ratio = num_stars / len(stars)

        # Summon new stars
        while star_ratio < starriness:
            num_stars = get_star_count(stars)
            star_ratio = num_stars / len(stars)
            stars[randint(0, len(stars) - 1)] = randint(255 // 2, 255)

        # Fade out current stars
        for i in range(len(stars)):
            if stars[i] > 0:
                stars[i] -= 1
                if stars[i] < 0:
                    stars[i] = 0

        # Create list to yield
        pixels = []
        for i in range(len(stars)):
            pixels.append(_color_brightness(color, stars[i] / 255))

        yield pixels

def fade(pixel_count, from_color=(0, 0, 0), to_color=(255, 255, 255), duration=3600):
    ''' Fades from from_color to to_color over duration seconds

    Args:
        pixel_count (int): Number of LEDS in the strips $$ no_input
        from_color (color): The color to start at
        to_color (color): The color to end at
        duration (int): The number of seconds to perform the fade over $$ input range(1, 86400)

    Yields:
        list: The colors to be output to the pixels
    '''

    time_start = time()
    pixels = [from_color] * pixel_count
    while True:
        percent_complete = 1 - (time() - time_start) / duration
        percent_complete = percent_complete if percent_complete < 1 else 1
        color = _blend(from_color, to_color, percent_complete)
        yield [color] * pixel_count

# TODO: fade_in(duration), fade_out(duration), galaxy, snow, weather


from django.contrib import admin
from django.forms import TextInput, Textarea
from django.db import models

# Register your models here.

from .models import Product, Sku, Size, Spec, Material

class SkuInline(admin.StackedInline):
    model = Sku
    extra = 1

class SizeAdmin(admin.ModelAdmin):
    list_display = ['variant', 'sage_size', 'bed_size', 'imperial_width', 'imperial_length',
                    'metric_width', 'metric_length',
                    'amazon', 'description_long', ]

class ProductAdmin(admin.ModelAdmin):
    list_display = ['descriptive_content', 'id', 'product_type', 'product_code_root', 'bullet_point_1', 'bullet_point_2', 'bullet_point_3']
    inlines = [SkuInline]
    formfield_overrides = {
        models.CharField: {'widget': TextInput(attrs={'size':'100'})},
        models.TextField: {'widget': Textarea(attrs={'rows':4, 'cols':40})},
    }

    fieldsets = (
        (None,
            {'fields': (
                'descriptive_content',
                'product_type',
                'product_code_root',
                'material',
                'ebay_material',
                'ebay_features',
                'ebay_type',
                'bullet_point_1',
                'bullet_point_2',
                'bullet_point_3',
                'bullet_point_4',
                'bullet_point_5',)
            }
         ),
        ('Advanced options', {
            'classes': ('wide', 'collapse',),
            'fields': ( 'manufacturer_description',
              'factory_description',
              'sage_description',
              'sage_category',
              'in_the_box',
              'keywords')

        }),
    )

class SpecAdmin(admin.ModelAdmin):
    list_display = ['quilting_pattern', 'fixing']

class MaterialAdmin(admin.ModelAdmin):
    list_display = ['description', 'material', 'thread_count', 'weight']

admin.site.register(Product, ProductAdmin)
admin.site.register(Size, SizeAdmin)
admin.site.register(Spec, SpecAdmin)
admin.site.register(Material, MaterialAdmin)


# -*- coding: utf-8 -*-
import scrapy
from  sport.items import footballItem

class FootballSpider(scrapy.Spider):
    name = 'football'
    allowed_domains = ['soccer.hupu.com']
    start_urls = ['http://soccer.hupu.com/']

    def parse(self, response):
        #获取联赛
        league_title = response.xpath('//li[@class="hp-dropDownMenu"][position()<=4]/a/@title').extract()
        league_url = response.xpath('//li[@class="hp-dropDownMenu"][position()<=4]/a/@href').extract()
        for title,url in zip(league_title,league_url):
            yield scrapy.Request(url=url, meta={'league': title},
                                    callback=self.team_detail,dont_filter=True)
    def team_detail(self,response):
        #获取球队
        league = response.meta.get('league')
        for team in response.xpath('//ul[@class="england-list-item"]/li'):
            team_url = team.xpath("a/@href").extract()
            team_name = team.xpath("a/@title").extract()
            team_url = team_url[0] if team_url else ''
            team_name = team_name[0] if team_name else ''
            #print(team_name,team_url)
            if not team_url:
                continue
            yield scrapy.Request(url=team_url, meta={'league': league,'team_name':team_name},
                                    callback=self.player_detail,dont_filter=True)

    def player_detail(self,response):
        #获取球员
        league = response.meta.get('league')
        team_name = response.meta.get('team_name')
        #team_name = response.xpath("//div[@class='team_info left']/h3/span/text()").extract_first("")
        team_logo = response.xpath("//li[@class=' left pic_logo']/img/@src").extract_first("")
        team_coach =response.xpath("//div[@class='team_info left']//dl[@class='clearfix']/dd[1]/text()").extract_first("")
        team_build = response.xpath("//div[@class='team_info left']//dl[@class='clearfix']/dd[2]/text()").extract_first("")
        team_city = response.xpath("//div[@class='team_info left']//dl[@class='clearfix']/dd[3]/text()").extract_first("")
        team_place = response.xpath("//div[@class='team_info left']/ul[2]/li[1]/text()").extract_first("")
        team_addr = response.xpath("//div[@class='team_info left']/ul[2]/li[2]/text()").extract_first("")
        team_href = response.xpath("//div[@class='team_info left']/ul[2]/li/a/@href").extract_first("")
        for playinfo in response.xpath('//table[@class="team_player"]'):
            #print(playinfo.xpath('//tr[not(@calss)]'))

            for player in playinfo.xpath('tr'):
                #print(player)
                player_number = player.xpath("td[1]/text()").extract()
                player_cha_name = player.xpath("td[2]/a/text()").extract()
                player_eng_name = player.xpath("td[3]/a/text()").extract()
                player_age = player.xpath("td[4]/text()").extract()
                player_courtry = player.xpath("td[5]/text()").extract()
                player_job = player.xpath("td[6]/text()").extract()
                #player_job = player.xpath("td[6]/text()").extract_first("")

                player_number = player_number[0] if player_number else ''
                player_cha_name = player_cha_name[0] if player_cha_name else ''
                player_eng_name = player_eng_name[0] if player_eng_name else ''
                player_age = player_age[0] if player_age else ''
                player_courtry = player_courtry[0] if player_courtry else ''
                player_job = player_job[0] if player_job else ''
                #print(player_number,player_cha_name,player_eng_name,player_age,player_courtry,player_job)
                if not player_number.isdigit():
                    continue
                item = footballItem()
                item['league'] = league #联赛名称
                item['team_name'] = team_name     #球队名称
                item['player_number'] = player_number   #球员号码
                item['player_cha_name'] = player_cha_name  #球员中文名
                item['player_eng_name'] = player_eng_name #球员英文名
                item['player_age'] = player_age       #球员年龄
                item['player_courtry'] = player_courtry  #球员所属国籍
                item['player_job'] = player_job #球员位置
                item['team_logo'] = team_logo   #球队logo
                item['team_coach'] = team_coach #球队教练
                item['team_build'] = team_build #成立时间
                item['team_city'] = team_city   #所在城市
                item['team_place'] = team_place #主球场
                item['team_addr'] = team_addr   #联系地址
                item['team_href'] = team_href   #官网
                yield item

if __name__ == '__main__':
    from lxml import etree

    path="E:/youbest/project/spider/sport/football.html"
    fd = open(path,'rb')
    html = fd.read()
    response = etree.HTML(html)
    #print(response.text)
    d=response.xpath('//table[@class="team_player"]')
    #print(d)


import pytest
import random
import skiplist
import multidict
import time

def setup_module(module):
    pass

def made_n_list(n: int):
    skip_list = skiplist.SkipList()
    for i in range(n):
        skip_list.update(True, i)
    return skip_list

def made_rand_n_list(n: int):
    skip_list = skiplist.SkipList()
    multiset = multidict.MultiDict()
    for _ in range(n):
        rand_num = random.randint(0, n * 2)
        multiset.add(str(rand_num), "")
        skip_list.update(True, rand_num)
    return [skip_list, multiset]

def test_skiplist_update_insert():
    skip_list = made_n_list(1000)
    for _ in range(100):
        rand_num = random.randint(0, 2000)
        assert skip_list.contains(rand_num) == (rand_num < 1000)

def test_skiplist_update_insert_random():
    skip_list, multiset = made_rand_n_list(1000)
    assert skip_list.__len__() == multiset.__len__()
    for _ in range(300):
        rand_num = random.randint(0, 1500)
        assert skip_list.contains(rand_num) == multiset.__contains__(str(rand_num))

def test_skiplist_update_delete():
    skip_list = made_n_list(1000)
    for i in range(500, 1000):
        skip_list.update(False, i)
    for _ in range(100):
        rand_num = random.randint(0, 1000)
        assert skip_list.contains(rand_num) == (rand_num < 500)

def test_skiplist_update_delete_random():
    skip_list, multiset = made_rand_n_list(1000)
    for i in range(500):
        rand_num = random.randint(0, 1500)
        skip_list.update(False, rand_num)
        if multiset.__contains__(str(rand_num)):
            multiset.pop(str(rand_num))
    for i in range(300):
        rand_num = random.randint(0, 1500)
        assert skip_list.contains(rand_num) == multiset.__contains__(str(rand_num))

def test_verify():
    skip_list = made_n_list(1500)
    validation = [random.randint(0, 3000) for _ in range(100)]
    for i in validation:
        response = skip_list.verify(i)
        assert response.validates_against()
        assert response.subject_contained() == (i < 1500)

def test_verify_random():
    skip_list, multiset = made_rand_n_list(1500)
    validation = [random.randint(0, 3000) for _ in range(150)]
    for i in validation:
        response = skip_list.verify(i)
        assert response.validates_against()
        assert response.subject_contained() == multiset.__contains__(str(i))

def common_test(n, m):
    skip_list, multiset = made_rand_n_list(n)
    argss = [(random.randint(0, 2), random.randint(0, 10000)) for _ in range(m)]
    lops = [lambda x: skip_list.verify(x).subject_contained(),
            lambda x: skip_list.update(True, x),
            lambda x: skip_list.update(False, x)]
    sops = [lambda x: str(x) in multiset, lambda x: multiset.add(str(x), x),
            lambda x: None if str(x) not in multiset else multiset.pop(str(x))]
    for op, c in argss:
        lres = lops[op](c)
        sres = sops[op](c)
        if op == 0:
            assert lres == sres
        assert skip_list.__len__() == multiset.__len__()

def test_light():
    common_test(2000, 4000)

def test_medium():
    common_test(4000, 10000)

def test_heavy():
    common_test(6000, 40000)


import pygame, random, time
pygame.init()
pygame.mixer.init()
pygame.font.init()
pygame.mixer.music.load("music.mp3")
pygame.mixer.music.play()
ismusicb = True
issoundb = True
w = pygame.display.set_mode((700, 700))
pygame.display.set_caption("Shooter Test")
clock = pygame.time.Clock()
isG = True
cycle = True
ec = (0, 0, 0)
bc = (10, 10, 10)
class Sprite:
    def __init__(self, x, y, w, h, cl=(255, 255, 255), img=None):
        self.color = cl
        self.img = img
        self.rect = pygame.Rect((x, y), (w, h))
    def debug(self):
        pygame.draw.rect(w, self.color, self.rect)
    def checkcol(self, x, y):
        if self.rect.collidepoint(x, y):
            return True
class Player(Sprite):
    def __init__(self, x, y, w, h, cl=(255, 255, 255), img=None, speed=3, damage=1):
        super().__init__(x, y, w, h, cl, img)
        self.sp = speed
        self.dmg = damage
        self.bullets = list()
        self.shootcd = 1
        self.shoottime = time.time()
    def update(self):
        keys = pygame.key.get_pressed()
        if keys[pygame.K_d]:
            self.rect.x += self.sp
        if keys[pygame.K_a]:
            self.rect.x -= self.sp
        if keys[pygame.K_f] and time.time()-self.shoottime >= self.shootcd:
            self.bullets.append(Sprite(self.rect.centerx, self.rect.y, 6, 6, cl=(200,200,0)))
            self.shoottime = time.time()
            if issoundb:
                shoot.play()
class Enemy(Sprite):
    def __init__(self, x, y, w, h, cl=(255, 255, 255), img=None, hp=1, mr = True):
        super().__init__(x, y, w, h, cl, img)
        self.hp = hp
        self.cd = time.time()
        self.mr = mr
    def update(self):
        self.debug()
        if self.rect.y <= 700 and time.time()-self.cd >= 0.85:
            self.rect.y += 15
            self.cd = time.time()
class TextArea:
    def __init__(self, x=0, y=0, width=10, height=10, color=(0,0,0)):
        self.rect = pygame.Rect(x, y, width, height)
        self.fill_color = color
    def set_text(self, text, fsize=12, text_color=(255, 255, 255)):
        self.text = text
        self.image = pygame.font.Font(None, fsize).render(text, True, text_color)
    def draw(self, shift_x=0, shift_y=0):
        pygame.draw.rect(w, self.fill_color, self.rect)
        w.blit(self.image, (self.rect.x + shift_x, self.rect.y + shift_y))
    def checkcol(self, x, y):
        if self.rect.collidepoint(x, y):
            return True
class Upgrade(Sprite):
    def __init__(self, x, y, w, h, utype, cl=(255, 255, 255), img=None):
        super().__init__(x, y, w, h, cl, img)
        self.utype = utype
    def update(self):
        self.debug()
        self.rect.y += 1
        #
mk = 28
miss = 0
plr = Player(350, 600, 30, 30, (255, 255, 255))
enemies = list()
espawnt = time.time()
espawndel = random.randint(2, 6)
wtext = TextArea(350, 350)
killcount = TextArea(10, 20)
misscount = TextArea(10, 50)
upgrades = list()
upgradetypes = ["dmg", "speed", "cd"]
ts = time.time()
levelup = 15
level = 1
enemyhpmin = 1
enemyhpmax = 1
inmenu = True
insettings = False
#sounds
die = pygame.mixer.Sound("enemydie.mp3")
levelups = pygame.mixer.Sound("levelup.mp3")
shoot = pygame.mixer.Sound("shot.ogg")
upgradesound = pygame.mixer.Sound("upgrade.ogg")
###
#menu
menub = list()
startb = TextArea(20, 60, 200, 90, (100, 100, 100))
startb.set_text("Начать", 80, (10, 10, 10))
settingb = TextArea(20, 200, 200, 90, (100, 100, 100))
settingb.set_text("Настройки", 50, (10, 10, 10))
exitb = TextArea(20, 340, 200, 90, (100, 100, 100))
exitb.set_text("Выход", 80, (10, 10, 10))
menub.append(startb)
menub.append(settingb)
menub.append(exitb)
#settings
settingsb = list()
ismusic = TextArea(20, 60, 200, 90, (0, 100, 0))
ismusic.set_text("Музыка", 80, (10, 10, 10))
issound = TextArea(20, 200, 200, 90, (0, 100, 0))
issound.set_text("Звук", 80, (10, 10, 10))
settingsb.append(ismusic)
settingsb.append(issound)
bosses = list()
lastkillfeed = 0
debugBoss = True
while cycle:
    w.fill(bc)
    if not inmenu:
        if isG:
            if lastkillfeed + 30 == mk:
                print("Появился босс! Убейте его для получения очков урона!")
                bosses.append(Enemy(random.randint(30, 650), 30, 40, 40, (140, 140, 140), None, level + 10))
                lastkillfeed = mk
                debugBoss = False
            for i in bosses:
                i.debug()
                if i.hp <= 0:
                    plr.dmg += 10
                    print(f"Вы победили босса! Игроку добавлено 10 дмг. У игрока {plr.dmg} дмг.")
                    bosses.remove(i)
                if i.rect.y >= 670:
                    bosses.remove(i)
                    print("Вы упустили босса.")
                if time.time() - i.cd >= 0.2:
                    i.rect.y += 1
                    i.cd = time.time()
                if i.mr:
                    i.rect.x += 1
                else:
                    i.rect.x -= 1
                if i.rect.x <= 0:
                    i.mr = True
                if i.rect.x >= 650:
                    i.mr = False
            if time.time()-espawnt >= espawndel:
                espawnt = time.time()
                espawndel = random.randint(2, 6)
                enemies.append(Enemy(random.randint(30, 650), 30, 30, 30, (255, 255, 255), None, random.randint(enemyhpmin, enemyhpmax)))
            for i in enemies:
                if i.hp <= 0:
                    mk += 1
                    if random.randint(1, 10) >= 7:
                        sus = True
                        while sus:
                            random.shuffle(upgradetypes)
                            if upgradetypes[0] == "dmg":
                                upgrades.append(Upgrade(i.rect.x, i.rect.y, 25, 25, upgradetypes[0], (100, 0, 0)))
                            elif upgradetypes[0] == "speed":
                                if plr.sp > 6:
                                    upgradetypes.pop(0)
                                upgrades.append(Upgrade(i.rect.x, i.rect.y, 25, 25, upgradetypes[0], (50, 50, 100)))
                                sus = False
                            elif upgradetypes[0] == "cd":
                                if plr.shootcd < 0.2:
                                    upgradetypes.pop(0)
                                else:
                                    upgrades.append(Upgrade(i.rect.x, i.rect.y, 25, 25, upgradetypes[0], (0, 100, 0)))
                                    sus = False
                    enemies.remove(i)
                    if issoundb:
                        die.play()
                if  i.rect.y >= 700:
                    enemies.remove(i)
                    miss += 1
                i.update()
            for i in plr.bullets:
                if i.rect.y <= 0:
                    plr.bullets.remove(i)
                for enemy in enemies:
                    if pygame.Rect.colliderect(i.rect, enemy.rect):
                        enemy.hp -= plr.dmg
                        plr.bullets.remove(i)
                for enemy in bosses:
                    if pygame.Rect.colliderect(i.rect, enemy.rect):
                        enemy.hp -= plr.dmg
                        plr.bullets.remove(i)
                i.rect.y -= 5
                i.debug()
            for i in upgrades:
                i.update()
                if pygame.Rect.colliderect(i.rect, plr.rect):
                    if i.utype == "dmg":
                        plr.dmg += 1
                        print(f"Урон игрока теперь {plr.dmg}")
                    if i.utype == "cd":
                        plr.shootcd -= 0.05
                        print(f"Кулдаун стрельбы теперь {plr.shootcd}с")
                    if i.utype == "speed":
                        if plr.sp <= 6:
                            plr.sp += 0.3
                            print(f"Скорость игрока теперь {plr.sp}")
                    if issoundb:
                        upgradesound.play()
                    upgrades.remove(i)
            killcount.set_text(f"kills: {mk}", 32)
            misscount.set_text(f"missed: {miss}", 32)
            killcount.draw()
            misscount.draw()
            keys = pygame.key.get_pressed()
            plr.update()
            plr.debug()
            '''
            if miss >= 3:
                isG = False
                bc = (200, 0, 0)
                wtext.set_text("YOU LOST.", 64)
            elif mk >= 3:
                isG = False
                bc = (0, 200, 0)
                wtext.set_text("YOU WIN!", 64)
            '''
            if time.time() - ts >= levelup:
                level += 1
                if random.randint(1, 5) >= 4:
                    enemyhpmin += random.randint(1, 2)
                enemyhpmax += random.randint(1, 2)
                ts = time.time()
                print(f"Переход на {level} уровень. Теперь у врагов от {enemyhpmin} до {enemyhpmax} хп.")
                if issoundb:
                    levelups.play()
        if not isG:
            wtext.draw()
    elif inmenu and not insettings:
        for i in menub:
            i.draw()
        for e in pygame.event.get():
            if e.type == pygame.QUIT:
                cycle = False
            if e.type == pygame.MOUSEBUTTONDOWN:
                if e.button == 1:
                    x, y = e.pos
                    if startb.checkcol(x, y):
                        inmenu = False
                    elif exitb.checkcol(x, y):
                        cycle = False
                    elif settingb.checkcol(x, y):
                        insettings = True
    elif inmenu and insettings:
        for i in settingsb:
            i.draw()
        for e in pygame.event.get():
            if e.type == pygame.QUIT:
                cycle = False
            if e.type == pygame.KEYDOWN:
                if e.key == pygame.K_ESCAPE:
                    inmenu = True
                    insettings = False
            if e.type == pygame.MOUSEBUTTONDOWN:
                if e.button == 1:
                    x, y = e.pos
                    if ismusic.checkcol(x, y):
                        if ismusicb:
                            pygame.mixer.music.stop()
                            ismusicb = False
                            ismusic.fill_color = (100, 0, 0)
                        else:
                            pygame.mixer.music.play()
                            ismusicb = True
                            ismusic.fill_color = (0, 100, 0)
                    elif issound.checkcol(x, y):
                        if issoundb:
                            issoundb = False
                            issound.fill_color = (100, 0, 0)
                        else:
                            issoundb = True
                            issound.fill_color = (0, 100, 0)
    for e in pygame.event.get():
        if e.type == pygame.QUIT:
            cycle = False
        if e.type == pygame.KEYDOWN:
            if e.key == pygame.K_ESCAPE:
                inmenu = True
                insettings = False
    pygame.display.update()
    clock.tick(60)


import urllib2
import lxml.html as LH
from lxml import etree
import pandas as pd

def get_sp500_metadata(*args, **kwargs):
    wiki = urllib2.urlopen("https://en.wikipedia.org/wiki/List_of_S%26P_500_companies")
    html = wiki.read()
    wiki_content = LH.fromstring(html)

    table = wiki_content.find_class("wikitable")[0]
    table_html = etree.tostring(table)
    sp500_wiki = pd.read_html(table_html, header=0)[0]
    sp500_wiki.index = range(1, sp500_wiki.shape[0]+1)

    sp500_wiki.drop(columns=["SEC filings", "GICS Sub Industry", "Address of Headquarters", "Date first added[3][4]", "CIK"], inplace=True)

    sp500_wiki_columns = dict(zip(
        sp500_wiki.columns,
        [
            "Symbol",
            "Company",
            "Sector"
        ]
    ))
    sp500_wiki.columns = [sp500_wiki_columns[colname] for colname in sp500_wiki.columns]
    return sp500_wiki


import numpy as np
from general_robotics_toolbox import *
from pandas import read_csv
import sys
from io import StringIO
from scipy.signal import find_peaks
# sys.path.append('../abb_motion_program_exec')
from abb_motion_program_exec_client import *
sys.path.append('../')
sys.path.append('../../toolbox')
from ilc_toolbox import *

from robots_def import *
from error_check import *
from MotionSend import *
from lambda_calc import *
from blending import *

def main():

	dataset='wood/'
	solution_dir='curve_pose_opt1/'
	data_dir="../../data/"+dataset+solution_dir
	cmd_dir='curve1_400_curve_pose_opt1/'

	curve = read_csv(data_dir+"Curve_in_base_frame.csv",header=None).values

	robot=abb6640(d=50)

	v=400
	s = speeddata(v,9999999,9999999,999999)
	z = z10

	ms = MotionSend()
	breakpoints,primitives,p_bp,q_bp=ms.extract_data_from_cmd(cmd_dir+'command.csv')

	###ilc toolbox def
	ilc=ilc_toolbox(robot,primitives)

	###TODO: extension fix start point, moveC support
	max_error=999
	inserted_points=[]
	iteration=50
	for i in range(iteration):

		ms = MotionSend()
		###execution with plant
		logged_data=ms.exec_motions(robot,primitives,breakpoints,p_bp,q_bp,s,z)
		# Write log csv to file
		with open("recorded_data/curve_exe_v"+str(v)+"_z10.csv","w") as f:
			f.write(logged_data)

		ms.write_data_to_cmd('recorded_data/command.csv',breakpoints,primitives, p_bp,q_bp)

		StringData=StringIO(logged_data)
		df = read_csv(StringData, sep =",")
		##############################data analysis#####################################
		lam, curve_exe, curve_exe_R,curve_exe_js, speed, timestamp=ms.logged_data_analysis(robot,df,realrobot=True)
		#############################chop extension off##################################
		lam, curve_exe, curve_exe_R,curve_exe_js, speed, timestamp=ms.chop_extension(curve_exe, curve_exe_R,curve_exe_js, speed, timestamp,curve[0,:3],curve[-1,:3])

		##############################calcualte error########################################
		error,angle_error=calc_all_error_w_normal(curve_exe,curve[:,:3],curve_exe_R[:,:,-1],curve[:,3:])
		print(max(error),min(speed))
		#############################error valley detection###############################
		valleys=[np.argmin(speed)]

		##############################plot error#####################################

		fig, ax1 = plt.subplots()
		ax2 = ax1.twinx()
		ax1.plot(lam, speed, 'g-', label='Speed')
		ax2.plot(lam, error, 'b-',label='Error')
		ax2.scatter(lam[valleys],error[valleys],label='valleys')
		ax2.plot(lam, np.degrees(angle_error), 'y-',label='Normal Error')
		ax2.axis(ymin=0,ymax=2)
		ax1.axis(ymin=0,ymax=1.2*v)

		ax1.set_xlabel('lambda (mm)')
		ax1.set_ylabel('Speed/lamdot (mm/s)', color='g')
		ax2.set_ylabel('Error/Normal Error (mm/deg)', color='b')
		plt.title("Speed and Error Plot")
		ax1.legend(loc="upper right")

		ax2.legend(loc="upper left")

		plt.legend()
		plt.savefig('recorded_data/iteration_'+str(i))
		plt.clf()
		# plt.show()

		##########################################calculate gradient for valleys######################################
		###restore trajectory from primitives
		curve_interp, curve_R_interp, curve_js_interp, breakpoints_blended=form_traj_from_bp(q_bp,primitives,robot)

		curve_js_blended,curve_blended,curve_R_blended=blend_js_from_primitive(curve_interp, curve_js_interp, breakpoints_blended, primitives,robot,zone=10)
		lam_blended=calc_lam_cs(curve_blended)
		print('estimating speed')
		speed_est=traj_speed_est(robot,curve_js_blended,lam_blended,v)
		plt.plot(lam_blended,speed_est)
		plt.savefig('recorded_data/speedest_iteration_'+str(i))
		plt.clf()

		for valley in valleys:
			######gradient calculation related to nearest 3 points from primitive blended trajectory, not actual one
			_,valley_speed_curve_idx=calc_error(curve_exe[valley],curve[:,:3])  # index of original curve closest to max error point

			###get closest to worst case point on blended trajectory
			_,valley_speed_curve_blended_idx=calc_error(curve_exe[valley],curve_blended)

			###############get numerical gradient#####
			###find closest 3 breakpoints
			order=np.argsort(np.abs(breakpoints_blended-valley_speed_curve_blended_idx))
			breakpoint_interp_2tweak_indices=order[:3]

			print('calculating gradient')
			dv_dq=ilc.get_speed_gradient_from_model(q_bp,breakpoints_blended,curve_js_blended,curve_blended,valley_speed_curve_blended_idx,breakpoint_interp_2tweak_indices,speed_est,v)
			p_bp, q_bp=ilc.update_bp_speed(p_bp,q_bp,dv_dq,speed[valley],breakpoint_interp_2tweak_indices,v)

if __name__ == "__main__":
	main()


def main():
    default_list = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

    less_that_5 = [num for num in default_list if num < 5]
    print("Numbers less than 5 are:")
    [print(num, end=' ') for num in less_that_5]
    print("")

if __name__ == '__main__':
    main()


import sys
from pathlib import Path

import click

from pinata.api_key import get_key_manager
from pinata.exceptions import PinataException
from pinata.sdk import Pinata
from pinata.utils import prettify_date

def profile_option():
    return click.option(
        "--profile",
        "-p",
        help="The profile to use",
        default=get_key_manager().default_profile_name,
    )

class ExceptionHandlingGroup(click.Group):
    def invoke(self, ctx):
        try:
            return super().invoke(ctx)
        except PinataException as err:
            click.echo(str(err), err=True)

@click.group(cls=ExceptionHandlingGroup)
def cli():
    pass

@cli.group("api-key")
def keys():
    """Manage your API key."""

@keys.command("list")
def list_keys():
    """List your API key profile names."""

    key_manager = get_key_manager()
    profile_names = key_manager.profile_names

    if not profile_names:
        _echo_no_profile()

    for name in profile_names:
        click.echo(name)

def _get_pinata(profile: str) -> Pinata:
    key_manager = get_key_manager()
    api_key, api_secret = key_manager.get_key_pair(profile)
    return Pinata.from_api_key(api_key, api_secret)

@keys.command("import")
@click.argument("profile_name")
@click.option("--api-key", help="The API key.", prompt=True)
@click.option("--api-secret", help="The API secret.", prompt=True)
def import_key(profile_name, api_key, api_secret):
    """Import an existing API key pair."""
    key_manager = get_key_manager()
    key_manager.set_key_pair(profile_name, api_key, api_secret)
    click.echo(f"Successfully added API key profile {profile_name}")

@keys.command("remove")
@click.argument("profile_name")
def remove_key(profile_name):
    """Remove an API key pair profile."""
    key_manager = get_key_manager()
    key_manager.delete_key_pair(profile_name)

@keys.command("rename")
@click.option("--old-name", help="The name of the profile to change.", required=True)
@click.option("--new-name", help="The new name of the profile.", required=True)
def rename_key(old_name, new_name):
    """Remove an API key pair profile."""
    key_manager = get_key_manager()
    key_manager.rename_key_pair(old_name, new_name)
    click.echo(f"Successfully renamed API key profile '{old_name}' to '{new_name}'.")

@cli.command()
@click.option("--status", default="pinned", type=click.Choice(["all", "pinned", "unpinned"]))
@profile_option()
def list_pins(status, profile):
    """List pins."""
    if not profile:
        _echo_no_profile()
        sys.exit(1)

    pinata = _get_pinata(profile)
    response = pinata.data.search_pins(status=status)
    pins = response["rows"]
    for pin in pins:
        name = pin["metadata"].get("name", "<Unnamed>")
        date_str = prettify_date(pin["date_pinned"])
        row = f"Name: {name}, CID (IPFS Pin Hash): {pin['ipfs_pin_hash']}, Pinned: {date_str}"
        click.echo(row)

@cli.command()
@click.argument("file_path", type=Path)
@profile_option()
def pin(file_path, profile):
    """Pin a new file."""
    pinata = _get_pinata(profile)
    cid = pinata.pin_file(file_path)
    click.echo(f"Successfully unpinned content. CID={cid}")

@cli.command()
@click.argument("content_hash")
@profile_option()
def unpin(content_hash, profile):
    """Remove a pin."""
    pinata = _get_pinata(profile)
    pinata.unpin(content_hash)
    click.echo("Successfully unpinned content.")

def _echo_no_profile():
    click.echo("There are no stored API keys.")


from django.shortcuts import render
from django.http import HttpRequest

from . import graph_utils

from pandas_datareader import yahoo as yahoo
from yahoo_fin import stock_info as yf

import datetime
import requests as rq
# from io import BytesIO
# import base64

sesh = None
USER_AGENT = 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/116.0'

def graph_view(request):

    global sesh

    if not sesh:
        sesh = rq.Session()
        sesh.headers.update({
            'User-Agent': USER_AGENT
        })

    product = request.GET.get('product', 'AMZN')
    strike = float(request.GET.get('strike', 130))
    expiry_str = request.GET.get('expiry', '2023-09-08')
    long = int(request.GET.get('long', 100))
    cover = int(request.GET.get('cover', 100))
    U = request.GET.get('spot', None)

    expiry = datetime.datetime.strptime(expiry_str, '%Y-%m-%d')

    context = {
        # 'graph_div': graph_div,
        'product': product,
        'strike': strike,
        'expiry': expiry_str,
        'long': long,
        'cover': cover,
    }

    try:
        U = U or round(yf.get_live_price(product), 2)
        U = float(U)
        context['spot'] = U

        tk = yahoo.options.Options(product, session=sesh)
        df = tk.get_call_data(expiry=expiry)
        df = df.reset_index()
        call = df[df['Strike'] == strike].iloc[0]

        K = strike
        P = call['Bid']

        # graph_div = get_payoff_graph(U, K, P, long, cover)

        op_list = [
            {'type': 'u',              'side': 'b', 'price': U, 'size': long},
            {'type': 'c', 'strike': K, 'side': 's', 'price': P, 'size': cover},
        ]

        x, (y_long, y_call), (label_long, label_call), combined = graph_utils.get_plots(U, op_list, spot_range=50)

        capital_required = U*long
        capital_withheld = capital_required - P*cover

        context.update({
            'x': x.tolist(),
            'y_long': y_long.tolist(),
            'y_call': y_call.tolist(),
            'label_long': label_long,
            'label_call': label_call,
            'y_combined': combined.tolist(),
            'capital_required': capital_required,
            'capital_withheld': capital_withheld,
        })

    except Exception as e:
        print(e)
        if 'spot' not in context:
            context['spot'] = 0
        context.update({
            'x': [],
            'y_long': [],
            'y_call': [],
            'label_long': 'null',
            'label_call': 'null',
            'y_combined': [],
        })

    return render(request, 'marketdata/covered_call.html', context=context)

# def get_payoff_graph(U, K, P, long_q, cover_q):

#     op_list = [
#         {'type': 'u',              'side': 'b', 'price': U, 'size': long_q},
#         {'type': 'c', 'strike': K, 'side': 's', 'price': P, 'size': cover_q},
#     ]

#     fig = graph_utils.multi_plotter(spot=U,spot_range=25, op_list=op_list)
#     tmp = BytesIO()
#     fig.savefig(tmp, format='png')
#     encoded = base64.b64encode(tmp.getvalue()).decode('utf-8')

#     graph_div = f'<img src=\'data:image\png;base64,{encoded}\'>'

#     return graph_div


import urllib.request
import socket

class Crawler(object):
    """
    抓取类,用于抓取对应的URL
    """

    def __init__(self, logger):
        """
        初始化函数

        Args:
            logger: 用于记录日志的对象
        Returns:
            None
        """
        self.logger = logger

    def get_html(self, url):
        """
        获取指定URL的HTML内容

        Args:
            url (str): 指定的URL

        Returns:
            bytes: 返回指定URL的HTML内容，如果获取失败则返回-1或-2
        """
        try:
            self.logger.info('start get url:' + url)
            # 使用urllib.request.urlopen打开指定的URL
            page=urllib.request.urlopen(url)
            # 读取页面内容
            html=page.read()
            page.close()
        # 如果出现URLError或socket.timeout异常，则记录日志并返回-1或-2
        except urllib.request.URLError as e:
            self.logger.warning('url error:' + url + ' ' + str(e))
            return -1
        except socket.timeout as e:
            self.logger.warning('time out' + str(e))
            return -2
        return html


from typing import List

from .ast import PrimitiveType, StructType, EnumType
from .Output import Output

class PythonOutput(Output):
    '''Manages Output of Python Bindings

    Using a self nesting structure, a PythonOutput is created for each IDL
    module.
    '''

    primitive_types = {  # (Python Type, Default Default Value)
        PrimitiveType.Kind.bool: ('bool', 'False'),
        PrimitiveType.Kind.u8: ('int', '0'),
        PrimitiveType.Kind.i8: ('int', '0'),
        PrimitiveType.Kind.u16: ('int', '0'),
        PrimitiveType.Kind.i16: ('int', '0'),
        PrimitiveType.Kind.u32: ('int', '0'),
        PrimitiveType.Kind.i32: ('int', '0'),
        PrimitiveType.Kind.u64: ('int', '0'),
        PrimitiveType.Kind.i64: ('int', '0'),
        PrimitiveType.Kind.f32: ('float', '0.0'),
        PrimitiveType.Kind.f64: ('float', '0.0'),
        PrimitiveType.Kind.c8: ('str', "'\\x00'"),
        PrimitiveType.Kind.c16: ('str', "'\\x00'"),
        PrimitiveType.Kind.s8: ('str', "''"),
        PrimitiveType.Kind.s16: ('str', "''"),
    }

    def __init__(self, context: dict, name: str):
        self.submodules: List[PythonOutput] = []
        self.module = None
        new_context = context.copy()
        new_context.update(dict(
            output=context['output'] / name,
            types=[],
            has_struct=False,
            has_enum=False,
        ))
        super().__init__(new_context, new_context['output'],
            {'__init__.py': 'user.py'})

    def write(self):
        super().write()
        for submodule in self.submodules:
            submodule.write()

    def visit_root_module(self, root_module):
        self.module = root_module
        super().visit_module(root_module)

    def visit_module(self, module):
        submodule = PythonOutput(self.context, module.local_name())
        self.submodules.append(submodule)
        submodule.visit_root_module(module)

    def is_local_type(self, type_node):
        return type_node in self.module.types.values()

    def get_python_type_string(self, field_type):
        if isinstance(field_type, PrimitiveType):
            return self.primitive_types[field_type.kind][0]
        elif self.is_local_type(field_type):
            return field_type.local_name()
        else:
            return field_type.name.join()

    def get_python_default_value_string(self, field_type):
        if isinstance(field_type, PrimitiveType):
            return self.primitive_types[field_type.kind][1]
        else:
            type_name = self.get_python_type_string(field_type)
            if isinstance(field_type, StructType):
                return type_name + '()'
            elif isinstance(field_type, EnumType):
                return type_name + '.' + field_type.default_member
            else:
                raise NotImplementedError(repr(field_type) + " is not supported")

    def visit_struct(self, struct_type):
        self.context['has_struct'] = True
        self.context['types'].append(dict(
            local_name=struct_type.local_name(),
            type_support=self.context['native_package_name'] if struct_type.is_topic_type else None,
            struct=dict(
                fields=[dict(
                    name=name,
                    type=self.get_python_type_string(node.type_node),
                    default_value=self.get_python_default_value_string(node.type_node),
                ) for name, node in struct_type.fields.items()],
            ),
        ))

    def visit_enum(self, enum_type):
        self.context['has_enum'] = True
        self.context['types'].append(dict(
            local_name=enum_type.local_name(),
            enum=dict(
                members=[
                    dict(name=name, value=value) for name, value in enum_type.members.items()
                ],
            ),
        ))


# -*- coding: utf-8 -*-
import scrapy
from bs4 import BeautifulSoup
from baike.items import Crop
import re

class CropSpider(scrapy.Spider):
    name = 'crop'
    allowed_domains = ['baike.baidu.com']
    file_object = open('fenlei.txt', 'r', encoding='utf-8').read()
    wordList = file_object.split()  # 获取词表
    start_urls = []
    for i in wordList:  ##生成url列表
        cur = "https://baike.baidu.com/item/"
        cur = cur + str(i)
        start_urls.append(cur)

    # def __init__(self, type):
    #     self.type = type

    def parse(self, response):
        sketch = response.text
        exc = BeautifulSoup(sketch, "html.parser")
        crop_context = Crop()
        name = response.xpath(
            "/html/body[@class='wiki-lemma normal']/div[@class='body-wrapper']/div[@class='content-wrapper']/div[@class='content']/div[@class='main-content']/dl[@class='lemmaWgt-lemmaTitle lemmaWgt-lemmaTitle-']/dd[@class='lemmaWgt-lemmaTitle-title']/h1").extract_first()
        pattern = re.compile('<\w*>(.*)</\w*>')
        n = pattern.findall(name)
        p = exc.find_all(name='div', attrs='para')
        for i in range(len(p)):
            crop_context['context'] = p[i].get_text()
            crop_context['name'] = n[0]
            yield crop_context
            pass


from multiprocessing import Process
from holodex.utils.files import get_yaml_data
from holodex.components import *
from holodex.viz import Hand2DVisualizer, Hand3DVisualizer, MPImageVisualizer
from holodex.viz.visualizer_3d import OculusLeftHandDirVisualizer
from holodex.camera.realsense_camera import RealSenseRobotStream
from holodex.camera.camera_streamer import RobotCameraStreamer

def notify_process_start(notification_statement):
    print("***************************************************************")
    print("     {}".format(notification_statement))
    print("***************************************************************")

def start_robot_cam_stream(cam_serial_num, robot_cam_num):
    notify_process_start("Starting Robot Camera Stream {} Process".format(robot_cam_num))
    camera = RealSenseRobotStream(cam_serial_num, robot_cam_num)
    camera.stream()

def stream_cam_tcp(cam_num, host, port, cam_rotation_angle):
    notify_process_start("Starting Robot Camera TCP Stream Process")
    camera = RobotCameraStreamer(cam_num, host, port, cam_rotation_angle)
    camera.stream()

def start_mp_detector(options):
    notify_process_start("Starting Mediapipe Detection Process")
    detector = MPHandDetector(options['cam_serial_num'], options['resolution'], options['alpha'])
    detector.stream()

def start_oculus_detector(options):
    notify_process_start("Starting OculusVR Detection Process")
    detector = OculusVRHandDetector(options['host'], options['keypoint_stream_port'])
    detector.stream()

def keypoint_transform(detector_type):
    notify_process_start("Starting Keypoint transformation Process")
    transformer = TransformHandCoords(detector_type)
    transformer.stream()

def plot_2d(detector_type, *args):
    notify_process_start("Starting 2D Hand Plotting Process")
    plotter = Hand2DVisualizer(detector_type, *args)
    plotter.stream()

def plot_3d(detector_type):
    notify_process_start("Starting 3D Hand Plotting Process")
    plotter = Hand3DVisualizer(detector_type)
    plotter.stream()

def plot_oculus_left_hand():
    notify_process_start("Starting Oculus Left Hand Direction Plotting Process")
    plotter = OculusLeftHandDirVisualizer()
    plotter.stream()

def viz_hand_stream(rotation_angle):
    notify_process_start("Starting Mediapipe Hand Prediction Image Stream Process")
    visualizer = MPImageVisualizer(rotation_angle)
    visualizer.stream()

def mp_teleop(detector_config):
    notify_process_start("Starting Teleoperation Process")
    teleop = MPDexArmTeleOp()
    teleop.move(detector_config['finger_configs'])

def vr_teleop(detector_config):
    notify_process_start("Starting Teleoperation Process")
    teleop = VRDexArmTeleOp()
    teleop.move(detector_config['finger_configs'])

def deploy_model(configs):
    if configs.model not in ['VINN', 'BC']:
        raise NotImplementedError("{} not implemented".format(configs.model))

    notify_process_start("Starting Deployment Process")
    deployer = DexArmDeploy(configs)
    deployer.solve()

def get_camera_stream_processes(configs):
    robot_camera_processes = []
    robot_camera_stream_processes = []

    for idx, cam_serial_num in enumerate(configs['robot_cam_serial_numbers']):
        robot_camera_processes.append(
            Process(target = start_robot_cam_stream, args = (cam_serial_num, idx + 1, ))
        )

    if 'tracker' in configs.keys(): # Since we use this for deployment as well
        if configs.tracker.type == 'VR':
            if configs.tracker['stream_robot_cam']:
                robot_camera_stream_processes.append(
                    Process(target = stream_cam_tcp, args = (
                        configs.tracker['stream_camera_num'],
                        configs.tracker['host'],
                        configs.tracker['robot_cam_stream_port'],
                        configs.tracker['stream_camera_rotation_angle'],
                    ))
                )

    return robot_camera_processes, robot_camera_stream_processes

def get_detector_processes(teleop_configs):
    if teleop_configs.tracker.type == 'MP':
        detection_process = Process(target = start_mp_detector, args = (teleop_configs.tracker, ))
        keypoint_transform_processes = [Process(target = keypoint_transform, args = ('MP', ))]

        plotter_processes = []
        if teleop_configs.tracker['visualize_graphs']:
            plotter_processes.append(Process(target = plot_2d, args = (teleop_configs.tracker.type, )))
            plotter_processes.append(Process(target = plot_3d, args = (teleop_configs.tracker.type, ))),

        if teleop_configs.tracker['visualize_pred_stream']:
            plotter_processes.append(Process(target = viz_hand_stream, args = (teleop_configs.tracker['pred_stream_rotation_angle'], )))

    elif teleop_configs.tracker.type == 'VR':
        detection_process = Process(target = start_oculus_detector, args = (teleop_configs.tracker, ))
        keypoint_transform_processes = [
            Process(target = keypoint_transform, args = ('VR_RIGHT', )),
            Process(target = keypoint_transform, args = ('VR_LEFT', ))
        ]

        plotter_processes = []
        if teleop_configs.tracker['visualize_right_graphs']:
            plotter_processes.append(Process(target = plot_2d, args = ('VR_RIGHT', teleop_configs.tracker['host'], teleop_configs.tracker['plot_stream_port'], )))
            plotter_processes.append(Process(target = plot_3d, args = ('VR_RIGHT', )))

        if teleop_configs.tracker['visualize_left_graphs']:
            plotter_processes.append(Process(target = plot_oculus_left_hand))

    else:
        raise NotImplementedError("No such detector exists!")

    return detection_process, keypoint_transform_processes, plotter_processes

def get_teleop_process(teleop_configs):
    if teleop_configs.tracker.type == 'MP':
        teleop_process = Process(target = mp_teleop, args = (teleop_configs, ))
    elif teleop_configs.tracker.type == 'VR':
        teleop_process = Process(target = vr_teleop, args = (teleop_configs, ))

    return teleop_process

def get_deploy_process(configs):
    deploy_process = Process(target = deploy_model, args = (configs, ))
    return deploy_process


# imports
import pandas as pd
import folium
import folium.features
import folium.map
import geopandas

# read in the data
borders = geopandas.read_file('states.json')
csvFile = pd.read_csv("tax.csv", thousands=',')
population = pd.read_csv("population.csv", thousands=',')

population['NAME'] = population['NAME'].str[1:]
population['Population'] = pd.to_numeric(population['Population'])

csvFile['Tax'] = csvFile['Tax'].astype('int')

csvFile = csvFile.map(lambda x: x.strip() if isinstance(x, str) else x)

# join csvfile and borders into combined
combined = borders.merge(population.merge(csvFile, on="NAME"), on="NAME")

# creates the default map with zoom to US
m = folium.Map(location=[48, -102], zoom_start=3)

# FOLIUM CREATING VISUALIZATION
# creates the choropleth and names it cp
cp = folium.Choropleth(
    # both geojson data and data come from new_borders
    geo_data=combined,
    data=combined,
    # key is fips, value and color is cases
    columns=["NAME", "Percent"],
    # eliminated as many grey counties as I could
    # for some reason there are still some left
    key_on="feature.properties.NAME",
    # color from colorbrewer
    fill_color="BuPu",
    fill_opacity=0.7,
    line_opacity=0.2,
    # change legend name
    legend_name="Percent of Personal Income Collected as Tax (%)",
    name="choropleth",
).add_to(m) #add to map

# # EXPANSION
# # hover tooltip
folium.features.GeoJsonTooltip(
    # fields displayed and their aliases
        fields=['NAME',
                'Population',
                'TotalTax',
                'Tax',
                'Percent'],
        aliases=['State Name: ',
                 'Population: ',
                 'Total Tax Revenue by State: $',
                 'Average Tax Revenue per Capita: $',
                 'Percent of Personal Income Collected as Tax: ',],
        localize=True,
                ).add_to(cp.geojson) # add to cholopleth's geojson

# add a layercontrol
folium.LayerControl().add_to(m)

# save it as an html
m.save("Tax.html")

print(combined)


from .protocol import Protocol
from .ip import IP
from .arp import ARP
from utils import b2mac, btable, color

frame_type = btable({
    0x0800: 'IP',
    0x0806: 'ARP',
    0x0835: 'RARP',
})

class Ethernet(Protocol):
    protocol = 'Ethernet'

    def parse(self):
        assert len(self._data) >= 14, 'invalid ethernet frame'
        self.src = self._read('6B')
        self.dst = self._read('6B')
        self.data_type = self._read('>H')

        if frame_type[self.data_type] == 'IP':
            ip = IP(self, self._data[self._pos:])
            ip.parse()
            self.child.append(ip)
        elif frame_type[self.data_type] in ('ARP', 'RARP'):
            arp = ARP(self, self._data[self._pos:])
            arp.parse()
            self.child.append(arp)
        else:
            raise Exception('Unrecognize frame type %04X' % self.data_type)

    def __str__(self):
        return '[%s] [%s -> %s]' % (
                color(self.protocol, 'cyan'),
                color(b2mac(self.src), 'yellow'),
                color(b2mac(self.dst), 'yellow')
        )


class Solution:
    def find_permutation(self, s):
        l = 0
        r = len(s)
        list_str = list(s)
        self.permute(list_str,l,r)

    def permute(self,s,l,r):
        if l== r:
            print(''.join(s))
        for i in range(l,r):
            s[l],s[i] = s[i],s[l]
            self.permute(s,l+1,r)
            s[l],s[i] = s[i],s[l]

soln = Solution()
soln.find_permutation('ABCD')


"""
move_arm_to_slider.py
Description:
    Trying to build a basic simulation where we move the gripper of the Kinova Gen3 6DoF
    to the target location and grip an object.
"""

import importlib
import sys
from urllib.request import urlretrieve

# Start a single meshcat server instance to use for the remainder of this notebook.
server_args = []
from meshcat.servers.zmqserver import start_zmq_server_as_subprocess
proc, zmq_url, web_url = start_zmq_server_as_subprocess(server_args=server_args)

# from manipulation import running_as_notebook

# Imports
import numpy as np
import pydot
from ipywidgets import Dropdown, Layout
from IPython.display import display, HTML, SVG
import matplotlib.pyplot as plt

from pydrake.all import (
    AddMultibodyPlantSceneGraph, ConnectMeshcatVisualizer, DiagramBuilder,
    FindResourceOrThrow, GenerateHtml, InverseDynamicsController,
    MultibodyPlant, Parser, Simulator, RigidTransform , RotationMatrix,
    ConstantValueSource, ConstantVectorSource, AbstractValue,
    RollPitchYaw, LogVectorOutput, plot_system_graphviz,
    LeafSystem, BasicVector )
from pydrake.multibody.jupyter_widgets import MakeJointSlidersThatPublishOnCallback

# setting path
sys.path.append('/root/kinova_drake/')

from kinova_station import KinovaStationHardwareInterface, EndEffectorTarget, GripperTarget, KinovaStation
from controllers.velocity import VelocityCommand, VelocityCommandSequence, VelocityCommandSequenceController
from observers.camera_viewer import CameraViewer

#######################
## Class Definitions ##
#######################

###############
## Functions ##
###############

def add_loggers_to_system(builder,station):
    # Loggers force certain outputs to be computed
    wrench_logger = LogVectorOutput(station.GetOutputPort("measured_ee_wrench"),builder)
    wrench_logger.set_name("wrench_logger")

    pose_logger = LogVectorOutput(station.GetOutputPort("measured_ee_pose"), builder)
    pose_logger.set_name("pose_logger")

    twist_logger = LogVectorOutput(station.GetOutputPort("measured_ee_twist"), builder)
    twist_logger.set_name("twist_logger")

    gripper_logger = LogVectorOutput(station.GetOutputPort("measured_gripper_velocity"), builder)
    gripper_logger.set_name("gripper_logger")

    return pose_logger

def create_velocity_control_hw_scenario(station_in):
    """
    Description:
        Anchors it to a "ground plane" and gives it the
        RobotiQ 2f_85 gripper.
        This should also initialize the meshcat visualizer so that we can easily view the robot.
    Usage:
        builder, controller, diagram, diagram_context = create_velocity_control_hw_scenario(station)
    """

    builder = DiagramBuilder()

    # Constants
    gripper_type = '2f_85'
    dt = 0.001

    pusher_position = [0.8,0.5,0.25]
    # pusher_rotation=[0,np.pi/2,0]
    pusher_rotation=[0,0,0]

    # Start with the Kinova Station object
    # station = KinovaStationHardwareInterface(time_step=dt,n_dof=6)

    # Start assembling the overall system diagram
    builder = DiagramBuilder()
    builder.AddSystem(station)

    # Setup loggers
    pose_logger = add_loggers_to_system(builder,station)

    # Setup Controller
    cs = setup_triangle_command_sequence()
    controller, v_estimator = setup_controller_and_connect_to_station(cs,builder,station)

    # Log Velocity Estimator
    vel_estimate_logger = LogVectorOutput(v_estimator.GetOutputPort("estimated_ee_velocity"), builder)
    vel_estimate_logger.set_name("velocity_estimate_logger")

    # Build the system diagram
    diagram = builder.Build()
    diagram.set_name("system_diagram")
    diagram_context = diagram.CreateDefaultContext()

    # context = diagram.CreateDefaultContext()
    # station.meshcat.load()
    diagram.Publish(diagram_context)

    ## Set up initial positions ##

    # Set default arm positions
    # station.go_home(diagram, diagram_context)

    # Set starting position for any objects in the scene
    # station.SetManipulandStartPositions(diagram, diagram_context)

    # Return builder, controller, etc.
    return builder, controller, diagram, diagram_context, pose_logger, vel_estimate_logger

def setup_triangle_command_sequence():
    """
    Description:
        Creates the command sequence that we need to achieve the infinity sequence.
    Notes:
        Each velocity is a six-dimensional vector where each dimension represents the following rates:
        - [roll, pitch, yaw, x, y, z]
    """
    # Constants
    triangle_side_duration = 10.0

    # Create the command sequence object
    vcs = VelocityCommandSequence([])

    # 1. Initial Command (Pause for 5s)
    init_velocity = np.zeros((6,))
    vcs.append(VelocityCommand(
        name="pause1",
        target_velocity=init_velocity,
        duration=2,
        gripper_closed=False))

    # 2. Upper Right
    deltap1 = np.zeros((6,))
    deltap1[3:] = np.array([0.2,0.2,0])
    vcs.append(VelocityCommand(
        name="upper_right",
        target_velocity=deltap1/triangle_side_duration,
        duration=triangle_side_duration,
        gripper_closed=False))

    # 3. Lower Right
    deltap2 = np.zeros((6,))
    deltap2[3:] = np.array([0.2,-0.2,0])
    vcs.append(VelocityCommand(
        name="upper_right",
        target_velocity=deltap2/triangle_side_duration,
        duration=triangle_side_duration,
        gripper_closed=False))

    # 4. Return to STart
    deltap3 = np.zeros((6,))
    deltap3[3:] = np.array([-0.4,0,0])
    vcs.append(VelocityCommand(
        name="return_home",
        target_velocity=deltap3/triangle_side_duration,
        duration=triangle_side_duration,
        gripper_closed=False))

    # 5. Pause
    vcs.append(VelocityCommand(
        name="pause2",
        target_velocity=init_velocity,
        duration=2,
        gripper_closed=False))

    return vcs

def setup_controller_and_connect_to_station(cs,builder,station):
    """
    Description:
        Defines the controller (PID) which is a CommandSequenceController as defined in
        kinova_drake.
    Inputs:
        cs = A CommandSequence object which helps define the CommandSequenceController.
    """

    # Create the velocity estimator
    v_estimator = builder.AddSystem(VelocityCalculator())

    # Create the controller and connect inputs and outputs appropriately
    #Kp = 10*np.eye(6)
    # Kp = np.diag([0.2,0.2,0.2,2,2,2])
    # Kd = 2*np.sqrt(Kp)

    Kp = 10*np.eye(6)
    Kd = 2*np.sqrt(Kp)

    controller = builder.AddSystem(VelocityCommandSequenceController(
        cs,
        command_type=EndEffectorTarget.kWrench,  # wrench commands work best on hardware
        Kp=Kp,
        Kd=Kd))
    controller.set_name("controller")

    # Connect the Controller to the station
    builder.Connect(                                  # Send commands to the station
                controller.GetOutputPort("ee_command"),
                station.GetInputPort("ee_target"))
    builder.Connect(
            controller.GetOutputPort("ee_command_type"),
            station.GetInputPort("ee_target_type"))
    builder.Connect(
            controller.GetOutputPort("gripper_command"),
            station.GetInputPort("gripper_target"))
    builder.Connect(
            controller.GetOutputPort("gripper_command_type"),
            station.GetInputPort("gripper_target_type"))

    # Connect the Station to the Estimator
    builder.Connect(
        station.GetOutputPort("measured_ee_pose"),
        v_estimator.GetInputPort("current_ee_pose")
    )

    # Connect the Estimator to the Controller
    builder.Connect(                                        # Send estimated state information
            v_estimator.GetOutputPort("estimated_ee_velocity"), # to the controller
            controller.GetInputPort("ee_velocity"))
    builder.Connect(
            station.GetOutputPort("measured_ee_twist"),
            controller.GetInputPort("ee_twist"))
    #controller.ConnectToStation(builder, station)

    return controller, v_estimator

###############################################
# Important Flags

run = True
show_station_diagram = False
show_plots = True

n_dof = 6
###############################################

time_step = 0.025

# Building Diagram
with KinovaStationHardwareInterface(n_dof) as station:
    builder, controller, diagram, diagram_context, pose_logger, vel_estimate_logger = create_velocity_control_hw_scenario(station)

    if show_station_diagram:
        # Show the station's system diagram
        plt.figure()
        plot_system_graphviz(diagram,max_depth=1)
        plt.show()

    if run:
        # # First thing: send to home position
        # station.go_home(diagram,diagram_context)

        # We use a simulator instance to run the example, but no actual simulation
        # is being done: it's all on the hardware.
        simulator = Simulator(diagram, diagram_context)
        simulator.set_target_realtime_rate(1.0)
        simulator.set_publish_every_time_step(False)  # Usually, this should be set to False. Otherwise, simulation will be very slow and won't look like real time.

        # We'll use a super simple integration scheme (since we only update a dummy state)
        # and set the maximum timestep to correspond to roughly 40Hz
        integration_scheme = "explicit_euler"
        time_step = 0.025
        #ResetIntegratorFromFlags(simulator, integration_scheme, time_step)

        # Run simulation
        simulator.Initialize()
        simulator.AdvanceTo(controller.cs.total_duration())

        # Collect Data
        pose_log = pose_logger.FindLog(diagram_context)
        log_times  = pose_log.sample_times()
        pose_data = pose_log.data()
        print(pose_data.shape)

        vel_log = vel_estimate_logger.FindLog(diagram_context)
        vel_log_times = vel_log.sample_times()
        vel_data = vel_log.data()
        print(vel_data.shape)

        if show_plots:

            # Plot Data - First Half
            fig = plt.figure()
            ax_list1 = []

            for plt_index1 in range(6):
                ax_list1.append( fig.add_subplot(231+plt_index1) )
                plt.plot(log_times,pose_data[plt_index1,:])
                plt.title('Pose #' + str(plt_index1))

            fig2 = plt.figure()
            ax_list2 = []
            for plt_index2 in range(6):
                ax_list2.append( fig2.add_subplot(231+plt_index2) )
                plt.plot(vel_log_times[10:],vel_data[plt_index2,10:])
                plt.title('Vel #' + str(plt_index2))

            plt.show()

    #Wait at end
    input('Press ENTER to end python program.')


from gpiozero import LED
from time import sleep

class Lights:
	def __init__(self):
		self.red = LED(7)
		self.amber = LED(25)
		self.green = LED(8)

	def set(self, level):
		self.red.off()
		self.amber.off()
		self.green.off()

		if level == 0:
			print("level 1 lights")
			self.green.blink()
			sleep(2)
			self.green.on()
		elif level == 1:
			print("level 2 lights")
			self.amber.blink()
			sleep(2)
			self.amber.on()
		elif level == 2:
			print("level 3 lights")
			self.red.blink()
			sleep(2)
			self.red.on()
		else:
			self.red.on()
			self.amber.on()
			self.green.on()


import cv2 as cv
import numpy as np
import tracking
import json
import img_proc as proc
from Lego import Lego

if __name__ == '__main__':

    scale_percent = 100
    MIN_DISTANCE = 50
    LEGO_UNIT_SIZE = 17

    cap = cv.VideoCapture("video_tracking_dataset/video_tracking_04.avi")
    ret, frame1 = cap.read()
    dim = proc.calc_resize(frame1, scale_percent)
    min_roi = int(dim[1]/3)
    max_roi = int(dim[1]*5/6)

    # read file
    with open('colors_video_tracking.json') as json_file:
        data = json_file.read()
    # parse file
    obj = json.loads(data)

    legos_dict = {}
    while cap.isOpened():
        ret, frame = cap.read()
        lego_lst = []
        if ret:
            #print("--------- NEW FRAME -----------")
            all_masks = np.zeros((dim[1], dim[0]), dtype=np.uint8)
            frame_resized = cv.resize(frame, (dim), interpolation=cv.INTER_AREA)
            cv.imshow('frame resized', frame_resized)

            for color in obj['colors']:
                color_name = color['color']
                hsv_low = np.array(color['hsv_low'], np.uint8)
                hsv_upper = np.array(color['hsv_upper'], np.uint8)
                frame_bw = proc.apply_mask(frame_resized, hsv_low, hsv_upper)
                all_masks = cv.bitwise_or(all_masks, frame_bw)
                lego_color_lst = tracking.find_legos(frame_bw, color_name)
                lego_lst.extend(lego_color_lst)

            cv.imshow('all masks', all_masks)

            for lego in lego_lst:
                if lego.ref_point[1] > 15:
                    tracking.update_dict(legos_dict, lego, MIN_DISTANCE)
                    key = tracking.get_key(legos_dict, lego, MIN_DISTANCE)
                    legos_dict[key].append(lego)

            if legos_dict:
                print("dict content:")
                for key, value in legos_dict.items():
                    lego = value[2]
                    if min_roi < lego.ref_point[1] < max_roi and lego.rect is True:
                        Lego.compute_ratio(lego, LEGO_UNIT_SIZE, dim[0], dim[1])
                    print("{} : {}" .format(key, str(lego)))
                    lego.draw_info(frame_resized, key)

                cv.imshow('draw info', frame_resized)

        else:
            print("Can't receive frame (stream end?). Exiting ...")
            break

        k = cv.waitKey(1)
        if k == ord('q'):
            break

    cap.release()
    cv.destroyAllWindows()


import pandas as pd
import argparse
from pathlib import Path
import sys

pd.options.mode.chained_assignment = None

parser = argparse.ArgumentParser()
parser.add_argument("-copyprobs_file",
                    help="Should be named in form anc.chr.master_all_copyprobsperlocus.txt.gz",
                    required=True)
parser.add_argument("-bp",
                    required=True)
parser.add_argument("-chr",
                    help="Chromosome number",
                    required=True)
parser.add_argument("-anc",
                    help="Ancestry being analysed",
                    required=True)
parser.add_argument("-reverse_cols",
                    help="Whether to reverse column names; can take two values: True or False",
                    required=True)
args = parser.parse_args()
if args.reverse_cols == 'True':
    reverse_cols = True
else:
    reverse_cols = False

if reverse_cols:  # Can't run on incorrectly ordered cols
    sys.exit()
else:  # For correctly labelled cols in copyprobs:
    def impute(impute_snp_list_):
        global painted_snp_list
        anc_copyprobs = pd.read_csv(args.copyprobs_file, sep=" ", usecols=['ID']).set_index('ID', drop=True)
        df = pd.DataFrame(index=anc_copyprobs.index)
        for snp in impute_snp_list_:
            if snp in painted_snp_list:
                temp_anc_copyprobs = pd.read_csv(args.copyprobs_file, sep=" ",
                                                 usecols=['ID', str(snp)],
                                                 dtype={'ID': str, str(snp): 'int8'}).set_index("ID", drop=True)
                df.loc[:, snp] = temp_anc_copyprobs[str(snp)]
            else:
                painted_snp_list.append(snp)
                painted_snp_list.sort(reverse=True)
                snp1 = painted_snp_list[painted_snp_list.index(snp)-1]
                snp2 = painted_snp_list[painted_snp_list.index(snp)+1]
                painted_snp_list.remove(snp)
                temp_anc_copyprobs = pd.read_csv(args.copyprobs_file, sep=" ",
                                                 usecols=['ID', str(snp1), str(snp2)],
                                                 dtype={'ID': str, str(snp1): 'int8', str(snp2): 'int8'})\
                    .set_index("ID", drop=True)
                D1 = abs(snp - snp1)
                D2 = abs(snp - snp2)
                temp_anc_copyprobs.loc[:, snp] = (1 - (D1 / (D1 + D2))) * temp_anc_copyprobs.iloc[:, 0] + (
                        1 - (D2 / (D1 + D2))) * temp_anc_copyprobs.iloc[:, 1]
                df.loc[:, snp] = temp_anc_copyprobs[snp]
        return df

    print("You have indicated that the columns in copyprobs are correctly labelled")
    Path("imputed/").mkdir(parents=True, exist_ok=True)
    if Path("imputed/temp." + str(args.anc) + "." + str(args.chr) + ".master_all_copyprobsperlocus.txt.gz").is_file():
        print("temp." + str(args.anc) + "." + str(args.chr) + ".master_all_copyprobsperlocus.txt.gz already imputed")
        sys.exit()
    painted_snp_list = pd.read_csv(str(args.copyprobs_file), sep=" ", nrows=0).columns.tolist()  # descending=correct
    del painted_snp_list[0]
    painted_snp_list = [int(x) for x in painted_snp_list]
    impute_snp_list = [args.bp]
    impute_snp_list = [int(x) for x in impute_snp_list]
    if len(impute_snp_list) == 0:
        print("No SNPs to be imputed for " + str(args.chr) + ", exiting")
        sys.exit()
    impute_snp_list.sort(reverse=True)  # descending
    imputed_snps = impute(impute_snp_list)  # NB saves in correct order!
    imputed_snps.to_csv("imputed/temp." + str(args.bp) + "." + str(args.anc) + ".master_all_copyprobsperlocus.txt.gz",
                        compression='gzip')


from check_gnocchi_service import *

def RHELOSP_34688_test():
    res = 1

    #1. Check that there is no test project
    field = "project"
    proj_value = "test"
    print("Checking that there is no %s in the list of %s" % (proj_value, field))
    res = check_field(field, proj_value)

    if res[0] !=0:
        return 1

    #2. Create test project
    if res[1] != 'Got':
        print("Creating %s %s" % (proj_value, field))
        res = build_field(field, proj_value)

        if res[0] !=0:
            return 1

    #3.0. Check that there is no test user
    field = "user"
    user_value = "test"
    print("Checking that there is no %s in the list of %s" % (user_value, field))
    res = check_field(field, user_value)

    if res[0] !=0:
        return 1
    #3.1. Create test user
    if res[1] != 'Got':
        print("Creating %s %s" % (user_value, field))
        res = build_field(field, user_value)

        if res[0] !=0:
            return 1

    #5. Add test to user role
    role = "user"
    res = add_role(proj_value, user_value, role)

    if res[0] !=0:
        return 1

    #6. Check that the project has been created
    field = "project"

    print("Checking that there is %s in the list of %s" % (proj_value, field))
    res = check_field(field, proj_value)

    if res[0] != 0:
        return 1

    #7. Create new testrc file, replace pass, project and user to test
    res = copy_file(orig_rc, test_rc)
    if res[0] !=0:
        return 1

    res = change_file(test_rc, 'OS_USERNAME', user_value)
    if res[0] !=0:
        return 1

    res = change_file(test_rc, 'OS_PASSWORD', user_value)
    if res[0] !=0:
        return 1

    res = change_file(test_rc, 'OS_PROJECT_NAME', proj_value)
    if res[0] !=0:
        return 1

    #8. Switch to testrc file
    res = switch_context(test_rc)
    if res != 0:
        return 1

    #9. Test that the list of resources is empty
    res = list_resources
    if res[1] != 'Empty':
        return 1

    return 0

if __name__ == "__main__":
    res = RHELOSP_34688_test()
    if res == 0:
        print("RHELOSP_34688_test Finished successfully")
    else:
        print("RHELOSP_34688_test failed")
    sys.exit(res)


'''
Autores:   Manuel López Amo-Ocón
           Alejangro Galván Pérez-Ilzarbe
           Santiago Cebellán
           Alejandro Meza Tudela

'''

#importacion de librerias
import numpy as np #agrega soporte para vectores y matrices, contituye biblioteca de funciones de alto nivel
import random as rand
from numpy import random #random permite la generacion de numeros aleatorios

from random import choices #choices esta dedicado a la representacion de pesos
import os
import seaborn as sns
import matplotlib.pyplot as plt #Dedicado a la representacion grafica
from scipy.stats import rv_discrete
import pandas as pd

#importacion de la clase persona
from PersonaV13 import *
from EdificioV15 import *

#Clase Simulador: contiene lo necesario para llevar a cabo la simulacion
'''
Lista de atributos:
                    dtrEdad: variable que contiene una distribucion de las edades
                    ciudad: ciudad que se crea para la simulacion
                    dia: entero que representa el paso de los dias
                    hora: entero que representa el paso de las horas
                    numpersonasinicial: entero que representa el numero de personas iniciales de la simulacion
                    numpersonas: entero que representa el numero de personas de la simulacion.
                    numedificios: entero que representa el numero de edificios totales.
                    CatalogoPersonas: lista que contiene a las personas de la simulacion
                    cementerio: lista que contiene las personas muertes debido a la enfermedad
                    RegistroSanos: lista que contiene a los individuos que estan sanos
                    RegistroMuertos: lista que contiene los individuos que van muriendo
                    mortalidadEdadesCovid : lista de listas, que contiene la probabilidad de muerte dependiendo de ciertos rangos
                                            de edad
'''

class Simulador:
        def __init__(self, numpersonas,personasEdificio,maxPiso,mortalidadEdadesCovid,aforoMedio):
            self.dtrEdad=self.generador_distribucion()
            self.aforoMedio=aforoMedio
            self.ciudadViviendas=self.crearViviendas(numpersonas,personasEdificio,maxPiso)
            self.ciudadOficinas=self.crearOficinas(numpersonas,personasEdificio,maxPiso,aforoMedio)
            self.dia=0 #se inicializa el dia con un valor de 0
            self.hora=0 #se inicializa las horas con un valor de 0
            self.numpersonasinicial=numpersonas
            self.numViviendas=len(self.ciudadViviendas)
            self.numOficinas=len(self.ciudadOficinas)
            #self.capacidades=np.zeros(self.numedificios)
            self.CatalogoPersonas=self.crearPoblacion()

            self.acomodarGente() #llamada al metodo definido un poco mas abajo

            self.cementerio=[] #lista vacia que simula un cementerio

            #self.distrEdad=self.dtrEdad
            self.RegistroSanos=[numpersonas]
            self.RegistroMuertos=[]
            self.mortalidadEdadesCovid=mortalidadEdadesCovid

        #Metodo que devuelve una lista de personas: una poblacion para nuestro caso particular
        def crearPoblacion(self):
            poblacion=[]
            for i in range(self.numpersonasinicial):
                poblacion.append(Persona(i,self.dtrEdad))
            return poblacion

        #Metodo que crea una lista de listas, que son las oficinas
        def crearOficinas(self,numpersonas,personasEdificio,maxCapacidad,aforoMedio):
            Oficinas=[]
            personasColocadas=0
            edificios=0
            while (numpersonas>personasColocadas): #mientras existan personas por colocar...
                personasEsteEdificio=random.randint(int(personasEdificio/2),int((personasEdificio/2)*3))
                if (numpersonas<personasEsteEdificio+personasColocadas):
                    personasEsteEdificio=numpersonas-personasColocadas
                personasColocadas+=personasEsteEdificio
                EdificioActual=Oficina(numeroEdificio=edificios,capacidadEdificio=personasEsteEdificio,maxCapacidad=maxCapacidad,
                                       aforo=aforoMedio, vestibulo=True)
                Oficinas.append(EdificioActual) #se adiciona una oficina, a la lista de oficinas
                edificios+=1
            return Oficinas

        def acomodarGente(self):
            personasAcomodadas=0
            for i in self.ciudadViviendas:
                capactual=i.capacidadEdificio
                listaActual=self.CatalogoPersonas[personasAcomodadas:personasAcomodadas+capactual]
                i.acomodar(listaActual)
                i.meterInicio(listaActual)
                personasAcomodadas+=capactual
            CatalogoBarajado=self.CatalogoPersonas.copy()
            random.shuffle(CatalogoBarajado)
            #for i in CatalogoBarajado:
            #    print(i)
            personasAcomodadas=0
            for i in self.ciudadOficinas:
                capactual=i.capacidadEdificio
                listaActual=CatalogoBarajado[personasAcomodadas:personasAcomodadas+capactual]
                i.acomodar(listaActual)
                personasAcomodadas+=capactual

        #Metodo que simula el paso del tiempo de la simulacion
        def pasar_tiempo(self,mediaincubacion,desvincubacion,mediaduracion,desvduracion,posibilidadContagio):
            #self.RegistroSanos.append(self.personas_sanas())
            for i in self.ciudadViviendas:
                i.edificioIAS ( self.hora,self)
                i.contagiarEdificio(posibilidadContagio,mediaincubacion,desvincubacion,self.dia)
            for i in self.ciudadOficinas:
                i.edificioIAS ( self.hora,self)
                i.contagiarEdificio(posibilidadContagio,mediaincubacion,desvincubacion,self.dia)

            if (self.hora<23):
                self.hora+=1
            else:#comienza un nuevo dia
                self.hora=0
                self.dia+=1
                for i in self.ciudadViviendas:
                    #Llamar a las transiciones de estados
                    i.transicionEdificio(self.dia, mediaincubacion,desvincubacion,mediaduracion,desvduracion,self.mortalidadEdadesCovid)
                for i in self.ciudadOficinas:
                    #Llamar a las transiciones de estados
                #transicionEstados(self.dia, mediaincubacion,desvincubacion,mediaduracion,desvduracion,self.mortalidadEdadesCovid)
                     i.transicionEdificio(self.dia, mediaincubacion,desvincubacion,mediaduracion,desvduracion,self.mortalidadEdadesCovid)

        #Metodo que permite a una persona irse de su lugar actual
        def irse(self,persona):
            if persona.lugarActual[2]==0:
                self.ciudadViviendas[persona.lugarActual[0]].departamentos[persona.lugarActual[1]].remove(persona)
            elif persona.lugarActual[2]==1:
                self.ciudadOficinas[persona.lugarActual[0]].departamentos[persona.lugarActual[1]].remove(persona)
            elif persona.lugarActual[2]==2:
                self.ciudadOficinas[persona.lugarActual[0]].vestibulo.remove(persona)

         #Metodo que permite a una persona ir a un lugar que es diferente del que estaba
        def meterse(self,persona,lugar,tipolugar):
            if tipolugar==0:
                self.ciudadViviendas[lugar[0]].departamentos[lugar[1]].append(persona)
            elif tipolugar==1:
                self.ciudadOficinas[lugar[0]].departamentos[lugar[1]].append(persona)
            elif tipolugar==2:
                self.ciudadOficinas[lugar[0]].vestibulo.append(persona)

         #Metodo que crea una lista de listas, que son las viviendas
        def crearViviendas(self,numpersonas,personasEdificio,maxCapacidad):
            Viviendas=[]
            personasColocadas=0
            edificios=0
            while (numpersonas>personasColocadas):#mientras existan personas por colocar...
                personasEsteEdificio=random.randint(int(personasEdificio/2),int((personasEdificio/2)*3))
                if (numpersonas<personasEsteEdificio+personasColocadas):
                    personasEsteEdificio=numpersonas-personasColocadas
                personasColocadas+=personasEsteEdificio
                print(edificios,personasEsteEdificio,maxCapacidad)
                EdificioActual=Vivienda(numeroEdificio=edificios,capacidadEdificio=personasEsteEdificio,maxCapacidad=maxCapacidad)
                Viviendas.append(EdificioActual)
                edificios+=1
            return Viviendas

        #Metodo que permite la impresion de los individuos del catalago de personas
        def printearCatalogo(self):
            for i in self.CatalogoPersonas:
                print(i)

        #Metodo que crea a los 'pacientes cero'
        def contagio_fijo(self, cambioEstadoInicial,numpacientescero):
            personas=rand.sample(self.CatalogoPersonas,k=numpacientescero)
            for i in personas:
                i.estado="asintomatico"
                i.cambioEstado= cambioEstadoInicial

        def graficoPersonasEdades(self):
            edades=[self.CatalogoPersonas[i].edad for i in range(len(self.CatalogoPersonas))]
            arr=np.reshape(edades, (len(self.CatalogoPersonas), 1)).T[0]
            plt.hist(arr, bins=50)
            plt.show()

        def  graficoPersonasSanas(self):
            arr=np.reshape(self.RegistroSanos, (len(self.RegistroSanos), 1)).T[0]
            data=pd.DataFrame({'horas':range(0,len(self.RegistroSanos)),'personas_sanas':arr})
            sns.relplot(x="horas",y="personas_sanas", kind="line", data=data)

        def  graficoPersonasMuertas(self):
            arr=np.reshape(self.RegistroMuertos, (len(self.RegistroMuertos), 1)).T[0]
            data=pd.DataFrame({'horas':range(0,len(self.RegistroMuertos)),'personas_muertas':arr})
            sns.relplot(x="horas",y="personas_muertas", kind="line", data=data)

        #Funcion que se encarga de mostrar el cementerio
        def mostrarcementerio(self):
            print()
            print("Se va a mostrar el cementerio: ")
            if(len(self.cementerio) == 0):
                print("No ha habido ningun fallecido")
            else:
                for i in range (len(self.cementerio)):
                    print(f"{i.dni} , {i.edad}")

        def generador_distribucion(self):
            datos_edad = pd.read_csv("distribucion_edad.csv")
            l=list(datos_edad.iloc[:,0]) # cogemos todos los datos de la primera columna, pertenecientes a todas las filas
            l2= [] #definicion de una lista auxiliar
            for i in range(len(l)-1):
                l2.append(int(l[i])/int(l[-1]))
            #Esto es para ajustar porque parece que el numero total de personas y el numero de personas por edad no acaba de coincidir
            #creo que es que corte los mayores de 100 años.
            l2.append(1-sum(l2))

            l3=[i for i in range(len(l2))]
            #Esta es la distribución que obtenemos
            dtrEdad = rv_discrete(values=(l3,l2))

            #Se procede a eliminar, para que no queden variables sueltas
            del l
            del l2
            del l3
            return dtrEdad


# Machine Learning Project
# By James Quaife: j.quiafe1@nuigalway.ie, SID: 14100104
# and Andrew East: a.east1@nuigalway.ie, SID: 16280042
# National University of Ireland, Galway
# Computer Science CT475: Machine Learning
# November 2018
# Supervisor: Dr. Michael Madden

# Teamwork Attribution: This file was written by James Quiafe

class Model:
    def __init__(self):
        self.modelNum = -1
        self.test = []
        self.train = []
        self.treeRoot = None

class Tree:
    next_unique_id = -1

    def __init__(self):
        self.isLeaf = False
        self.predicted = None
        self.splitAttribute = None
        self.threshold = None
        self.leftChild = None
        self.rightChild = None
        self.unique_id = -1  # Unique identifier needed to make nodes unique in Graphviz node tag strings
        self.numCases = None
        self.numCasesMajorityClass = -1


import datetime, uuid
from tornado import escape
from sqlalchemy import create_engine, Column
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.dialects.mysql import (INTEGER, DATETIME, VARCHAR, TEXT, BOOLEAN)
from sqlalchemy.orm import sessionmaker

MYSQL_DB = create_engine('mysql+pymysql://root:123456@localhost/erp_message?charset=utf8')
SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://root:123456@localhost/erp_message?charset=utf8'

Session = sessionmaker(bind=MYSQL_DB)

Base = declarative_base()

class Message(Base):
    """
    留言
    """
    __tablename__ = "messages"
    id = Column(INTEGER, primary_key=True)  # 主键
    uuid = Column(VARCHAR(40), unique=True)  # uuid识别码
    created = Column(DATETIME, default=datetime.datetime.utcnow)  # 创建时间
    body = Column(TEXT)  # 留言内容
    receiver = Column(VARCHAR(20))  # 接受者
    flag = Column(BOOLEAN, default=False)  # 是否被标为已读/是否继续推送

class MessageManager(object):
    def __init__(self):
        self.session = Session()

    def add(self, body):
        """
        添加新消息，并返回包含uuid和内容的字典
        """
        body_dict = escape.json_decode(body.request.body)
        new_message = Message(uuid=str(uuid.uuid4()), body=body_dict["body"], receiver=body_dict["receiver"])
        self.session.add(new_message)
        self.session.commit()
        return {'id': new_message.uuid, 'body': new_message.body}

    def remove(self, handler):
        """
        将指定消息设为已读
        """
        message_uuid = handler.get_argument('uuid')
        remove_message = self.session.query(Message).filter_by(uuid=message_uuid).first()
        remove_message.flag = True
        self.session.commit()

    def list(self):
        """
        以字典形式返回目前所有未读消息
        """
        queryset = self.session.query(Message.uuid, Message.body).filter_by(flag=False).all()
        result = []
        for each_query in queryset:
            result.append({"id":each_query.uuid, "body":each_query.body})
        # print(result)
        return result

# class Sending(Base): 已废弃
#     """
#     分发和已读情况
#     """
#     __tablename__ = "sending"
#     id = Column(INTEGER, primary_key=True)  # 主键
#     message_id = Column(INTEGER, ForeignKey('messages.id'))  # 外键关联‘留言’的主键
#     receiver = Column(VARCHAR(20))  # 接受者
#     flag = Column(BOOLEAN, default=False)  # 是否被标为已读


# 42888. 오픈채팅방

# Enter, Leave => 메시지를 출력해야 함
# Enter, Change => 닉네임을 저장 또는 수정해야 함

def solution(record):
    result = []  # 출력할 결과
    user = {}  # 사용자의 아이디와 닉네임을 담을 딕셔너리
    command_list = []  # 명령과 사용자의 아이디를 담을 리스트

    for i in range(len(record)):
        # 1. Enter 명령이 들어왔을 때,
        if record[i][:5] == "Enter":
            # 명령, 아이디, 닉네임을 나눠줌
            command, user_id, name = record[i].split()
            # user 딕셔너리에 아이디:닉네임 쌍을 넣어줌
            user.update({user_id: name})
            # command_list에 실행할 명령과 그 아이디를 넣어줌
            command_list.append([command, user_id])

        # 2. Leave 명령이 들어왔을 때,
        elif record[i][:5] == "Leave":
            # 명령, 아이디를 나눠줌 (닉네임은 받지 않음)
            command, user_id = record[i].split()
            # command_list에 실행할 명령과 그 아이디를 넣어줌
            command_list.append([command, user_id])

        # 3. Change 명령이 들어왔을 때,
        elif record[i][:6] == "Change":
            # 명령, 아이디, 닉네임을 나눠줌
            command, user_id, name = record[i].split()
            # user 딕셔너리 안에 있는 해당 user_id의 닉네임을 바꿔줌
            user.update({user_id: name})

    # 메시지 저장
    # 닉네임이 바뀌면 이전 메시지의 닉네임까지 전부 바뀌어야 해서 조건문을 따로 빼주었다
    for i in range(len(command_list)):
        # Enter 명령일 때, 해당 아이디의 닉네임을 넣은 문장 저장
        if command_list[i][0] == "Enter":
            result.append(f'{user.get(command_list[i][1])}님이 들어왔습니다.')
        # Leave 명령일 때, 해당 아이디의 닉네임을 넣은 문장 저장
        elif command_list[i][0] == "Leave":
            result.append(f'{user.get(command_list[i][1])}님이 나갔습니다.')

    # print(result)

    return result

solution(
    [
        "Enter uid1234 Muzi",
        "Enter uid4567 Prodo",
        "Leave uid1234",
        "Enter uid1234 Prodo",
        "Change uid4567 Ryan",
    ]
)