import pandas
import json

df = pandas.read_json("../data/chuckratesbeer.json")
with pandas.option_context('display.max_rows', None, 'display.max_columns', None):
    # Rating score by count
    print(df['rating_score'].value_counts())
    print(json.dumps(df['rating_score'].value_counts().to_dict()))
    my_dict = df['rating_score'].value_counts().to_dict()
    my_list = []
    e_dict = {}
    for k in my_dict:
        my_list.append([str(k), my_dict[k]])
        my_list.append(["{}_x".format(k), k])
        e_dict[k] = "{}_x".format(k)

    print(json.dumps(e_dict))
    print(json.dumps(my_list))

    # # Find highest
    # print(df.loc[df['rating_score'] == 5.0])
    #
    # # Find lowest rated
    # print(df.loc[df['rating_score'] == 0.5])
    #
    # # Count beer type most
    # print(df['beer_type'].value_counts())
    #
    # # Rating by beer_type
    # print(df.groupby(['beer_type'])['rating_score'].mean())
    # # nf = df.groupby(['beer_type'])
    # # nf.apply()
    #
    # # Make subtypes
    df[["Type", "Subtype"]] = df.beer_type.str.split(" - ", expand=True)
    # # print(df.head())
    # # print(df.groupby(['Type'])['rating_score'].mean())
    # # print(df['Type'].value_counts())
    # # print(df['beer_type'].value_counts())
    # a = df['beer_type'].value_counts()
    # print(a.index)
    # for index, i in enumerate(a.index):
    #     print('{} {}
'.format(i, a[index]))
    # a = df['Type'].value_counts()
    # print(a.index)
    # for index, i in enumerate(a.index):
    #     print('{} {}
'.format(i, a[index]))
    # # groudf['beer_type'].value_counts()ped = df.groupby('beer_type').apply(['rating_score'].mean())
    # # print(grouped)
    # # grouped.sort_values('rating_score', ascending=False)
    # # print(grouped)
    df_by_spec_count = df.groupby('beer_type')['rating_score'].agg(['mean', 'count'])
    a = df_by_spec_count.sort_values(by='mean', ascending=False).to_dict()
    for index, i in enumerate(a.index):
        print('{} {}
'.format(i, a[index]))
    print(df_by_spec_count.sort_values(by='mean', ascending=False))
    #
    df_by_spec_count = df.groupby('Type')['rating_score'].agg(['mean', 'count'])
    print(df_by_spec_count.sort_values(by='mean', ascending=False).to_dict())
    my_dict = df_by_spec_count.to_dict()["count"]
    my_list = []
    for k in my_dict:
        my_list.append([k, my_dict[k]])
    print(my_list)
    e_dict = {}
    my_dict = df_by_spec_count.to_dict()["mean"]
    for k in my_dict:
        my_list.append(["{}_x".format(k), round(my_dict[k], 2)])
        e_dict[k] = "{}_x".format(k)
    print(json.dumps(e_dict))
    print(json.dumps(my_list))

# bid
# abv
# ibu
# beer_name
# beer_type
# brewery_city
# brewery_country
# global_rating_score
# serving_type
# rating_score


import os

from django.db import models
from django.template.defaultfilters import slugify
from django.urls import reverse
from django.utils.safestring import mark_safe

from museum.settings import STATIC_URL
from museum_site.constants import STATIC_PATH, DATE_HR
from museum_site.models.base import BaseModel
from museum_site.querysets.series_querysets import Series_Queryset

class Series(BaseModel):
    objects = Series_Queryset.as_manager()
    model_name = "Series"
    table_fields = ["Series", "Updated", "Latest", "First", "Total"]
    cell_list = ["view", "last_updated", "latest_article", "first_article", "total_articles"]
    guide_word_values = {"id": "pk", "latest": "latest", "title": "title"}
    sort_options = [
        {"text": "Newest Entry", "val": "latest"},
        {"text": "Title", "val": "title"}
    ]
    sort_keys = {
        # Key - Value from <select> used in GET params
        # Value - Django order_by param
        "title": ["title"],
        "latest": ["-last_entry_date", "title"],
        "id": ["id"],
        "-id": ["-id"],
    }

    # Constants
    PREVIEW_DIRECTORY = os.path.join("pages/series-directory/")
    PREVIEW_DIRECTORY_FULL_PATH = os.path.join(
        STATIC_PATH, "pages/series-directory/"
    )

    # Fields
    title = models.CharField(max_length=80)
    slug = models.SlugField(max_length=80, editable=False)
    description = models.TextField(default="")
    preview = models.CharField(max_length=80, default="", blank=True)
    first_entry_date = models.DateField(null=True, blank=True)
    last_entry_date = models.DateField(null=True, blank=True)
    visible = models.BooleanField(default=True)

    class Meta:
        ordering = ["title"]

    def __str__(self):
        return "[" + str(self.id) + "] " + self.title

    def to_select(self):
        return self.title

    def save(self, *args, **kwargs):
        self.slug = slugify(self.title)
        if self.id:
            article_set = self.article_set.all()
            if article_set:
                self.first_entry_date = article_set.order_by("publish_date").first().publish_date
                self.last_entry_date = article_set.order_by("publish_date").last().publish_date

        # Prevent blank preview URLs
        if not self.preview:
            self.preview = self.slug + ".png"
        super(Series, self).save(*args, **kwargs)

    def get_absolute_url(self):
        return reverse("series_view", kwargs={"series_id": self.pk, "slug": self.slug})

    def preview_url(self):
        return os.path.join(self.PREVIEW_DIRECTORY, self.preview)

    def get_meta_tag_context(self):
        """ Returns a dict of keys and values for <meta> tags  """
        tags = {}
        tags["author"] = ["name", ""]
        tags["description"] = ["name", self.description]
        tags["og:title"] = ["property", self.title + " - Museum of ZZT"]
        tags["og:image"] = ["property", self.preview_url()]  # Domain and static path to be added elsewhere
        return tags

    def get_field_view(self, view="detailed"):
        return {"value": "{}".format(self.get_absolute_url(), self.title), "safe": True}

    def get_field_last_updated(self, view="detailed"):
        article = self.article_set.all().order_by("-publish_date").first()
        return {"label": "Last Updated", "value": self.last_entry_date}

    def get_field_latest_article(self, view="detailed"):
        article = self.article_set.all().order_by("-publish_date").first()
        return self.get_field_this_article(article, view, label="Latest")

    def get_field_first_article(self, view="detailed"):
        article = self.article_set.all().order_by("publish_date").first()
        return self.get_field_this_article(article, view, label="First")

    def get_field_this_article(self, article, view="detailed", label="?"):
        article.request = self.request
        article._init_access_level()
        article._init_icons()
        if view == "list" or view == "gallery":
            field = article.get_field_view("title", text_override=label)
        else:
            field = article.get_field_view("title")
        field["label"] = "{} Article".format(label)
        return field

    def get_field_total_articles(self, view="detailed"):
        return {"label": "Total Articles", "value": self.article_set.count()}

    def get_field_description(self, view="detailed"):
        return {"label": "Description", "value": self.description}

    def context_detailed(self):
        context = self.context_universal()
        context["roles"] = ["model-block", "detailed"]
        context["columns"] = []

        columns = [
            ["last_updated", "latest_article", "first_article", "total_articles", "description"],
        ]

        for col in columns:
            column_fields = []
            for field_name in col:
                field_context = self.get_field(field_name)
                column_fields.append(field_context)
            context["columns"].append(column_fields)
        return context

    def context_list(self):
        context = self.context_universal()
        context["roles"] = ["list"]
        context["cells"] = []

        for field_name in self.cell_list:
            cell_fields = self.get_field(field_name, view="list")
            context["cells"].append(cell_fields)
        return context

    def context_gallery(self):
        context = self.context_universal()
        context["roles"] = ["model-block", "gallery"]
        context["fields"] = [
            self.get_field("latest_article", view="gallery"),
            self.get_field("first_article", view="gallery"),
        ]
        return context

    def get_guideword_latest(self): return self.last_entry_date.strftime(DATE_HR)


from nltk.tokenize import RegexpTokenizer
from nltk.stem import PorterStemmer
from nltk.corpus import stopwords
from sklearn.feature_extraction.text import CountVectorizer
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import MultinomialNB
from tkinter import *
from tkinter.font import Font

# Machine Learning Section:
# Reading the Dataset File
# The dataset that I used has about 5000 emails including both spam mails
# and ham mails in an excel sheet as a csv (comma separated) file

# Using Pandas Dataframe
df = pd.read_csv('email_dataset.csv', encoding='ISO-8859-1')
data = df.to_numpy()

# Feature Array including all the emails
X = data[:, 1]
# Target Array including the type (Spam/Ham)
y = data[:, 0]

# Initializing functions to that will help in
# 1. Breaking down emails into words using Regular Expression Tokenization
# 2. Removing Stopwords
tokenizer = RegexpTokenizer('\w+')
sw = set(stopwords.words('english'))
ps = PorterStemmer()

# This Function will tokenzise all the emails, removes stopwords,
# stem all the words and then creates a new vocab.
def getStem(review):
    review = review.lower()
    tokens = tokenizer.tokenize(review)
    removed_stopwords = [w for w in tokens if w not in sw]
    stemmed_words = [ps.stem(token) for token in removed_stopwords]
    clean_review = ' '.join(stemmed_words)
    return clean_review

# This function will get me all the emails tokenized,stopwords removed and stemmed as an array
def getDoc(document):
    d = []
    for doc in document:
        d.append(getStem(doc))
    return d

stemmed_doc = getDoc(X)

# Converting the edited document into numbers using Count Vectorizer Function
vectorizer = CountVectorizer()
vc = vectorizer.fit_transform(stemmed_doc)
X = vc.todense()

# Now that I have our dataset as a matrix of frequency of relevant words
# I can divide my dataset into a training set (that will train my machine about spam and ham mail)
# and Testing set (this will be used to test the model created)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=17)

# Naive Bayes from sklearn to create Machine Learning model
model = MultinomialNB()
model.fit(X_train, y_train)
score = model.score(X_test, y_test)

# Message (email) from the user
def prepare(msg):
    d = getDoc(msg)
    return vectorizer.transform(d)

# GUI Section
# Initializing the the main gui function
# Title: Spam Email Detector
root = Tk()
root.title("Spam Email Detector")
root.iconbitmap('mail_icon.ico')
root.geometry("1252x726")

# Defining the fonts
# Title Heading Font Style
headingFont = Font(
    family="Georgia",
    size=40,
    weight="bold"
)

# Body Font Style
bodyFont = Font(
    family="Lucida Console",
    size=10
)

# Button Font Style
btnFont = Font(
    family="Lucida Console",
    weight="bold"
)

# Answer Font Style
answerFont = Font(
    family="Georgia",
    size=50,
    weight="bold"
)

# Background Layout
my_img = PhotoImage(file="Background.png")
frame = Label(root, image=my_img)
frame.place(x=0, y=0)

# Heading Label
heading = Label(root, text="Spam Detection", font=headingFont, fg="#DFDFDF", bg="#2B3032")
heading.place(x=85, y=50)

# Message from Creator and Instruction Label
creator_message = f"""With an Accuracy score of {round(score*100, 3)}%,\n
Check if the Email you type is Spam or Not.\n
This Application is trained to evaluate if an \n
email is spam or not spam (ham).\n
The model has been trained on a dataset of over \n
5000 emails including both 'Spam' and 'not Spam'\n
Please enter the email below in the text field and \n
then click on Check button."""

info = Label(text=creator_message, width=60, height=18, bg="#404244", fg="#DFDFDF", font=bodyFont, bd=2, relief=SOLID)
info.place(x=65, y=150)

# User Input Text Field for writing email
email = Text(width=58, height=15, fg="#404244", padx=10, pady=10, font=bodyFont, bd=3, relief=SUNKEN)
email.place(x=65, y=420)

# Result Button Command Function
def OnClick():
    email_message = email.get("1.0", "end")
    global res
    global message
    message = [email_message]
    message = prepare(message)

    # Model trained now its time to test our model
    y_pred = model.predict(message)
    if y_pred[0] == 'spam':
        res = "Spam"
    else:
        res = " Ham"
    answer = Label(text=res, bg="#1E2122", fg="#DFDFDF", font=answerFont)
    answer.place(x=830, y=310)

    # Try Agaib Button Command Function
    def again():
        email.delete("1.0", "end")
        answer.destroy()

    # Clicking this button will clear the answer
    # of previous mail and clears to text field for the next mail
    tryAgain_btn = Button(text="TRY AGAIN", width=15, height=2, font=btnFont, command=again)

    # Clicking this button exits from the program
    exit_btn = Button(root, text="EXIT", width=15, height=2, font=btnFont, command=root.quit)

    tryAgain_btn.place(x=840, y=150)
    exit_btn.place(x=840, y=510)

# Clicking on this button shows the result.
check_btn = Button(text="CHECK", width=15, height=2, font=btnFont, relief=SOLID, command=OnClick)
check_btn.place(x=215, y=615)

# status bar
status = Label(root, text="Created By Meenal Phartiyal  ",width=178, bd=1, relief=SUNKEN, anchor=E)
status.place(x=0, y=705)

mainloop()


import click
from app.lib.arithmetic.mul import my_mul

@click.command(name="mul_cmd", help="Multiply multiple numbers and print the result")
@click.option(
    "--element", "-e", required=True, help="element to perform op for", type=click.FLOAT,multiple=True
)
def mul_cmd(element):
    click.echo(f"the result product of elements : {element} is {my_mul(*element)}")

if __name__ == "__main__":
    mul_cmd()

mul_cli_cmds = [mul_cmd]


import json

with open("/home/lqy/桌面/img_info_cache.json", "r") as f:
    info_list = json.load(f)
i = 0
new_list = []
for info in info_list:
    i += 1
    print(i)
    if info["is_downloaded"] is True:
        new_list.append(info)

print(len(new_list))
with open("/home/lqy/桌面/img_info_valid.json", "w") as f:
    json.dump(new_list, f)


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

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_httpauth import HTTPTokenAuth, HTTPBasicAuth, MultiAuth
import os

app = Flask(__name__, template_folder="../templates", static_folder="../static")
app.debug = True
app.secret_key = 'secret'

db_url = 'sqlite:///db.sqlite'
if os.environ.get('DB_URL'):
    db_url = os.environ.get('DB_URL')

app.config.update({
    'SQLALCHEMY_DATABASE_URI': db_url,
    'SQLALCHEMY_TRACK_MODIFICATIONS': True
})

db = SQLAlchemy(app)
# = OAuth2Provider(app)
oauth = HTTPTokenAuth(scheme='Token')
auth = HTTPBasicAuth()
multi_auth = MultiAuth(oauth, auth)

import authorization.models
import authorization.routes
# import authorization.init

# init.setup()


from bson import ObjectId
from bson.json_util import dumps
import json
from domain.interfaces.TaskRepository import TaskRepository
from pymongo import MongoClient
from config import settings
from domain.models.Task import Task
from infrastructure.exceptions.IncorrectObjectIdException import IncorrectObjectIdException
from infrastructure.exceptions.NotSameOwnerTaskException import NotSameOwnerTaskException
from infrastructure.exceptions.TaskNotFoundException import TaskNotFoundException

class MongoTaskRepository(TaskRepository):

    def __init__(self):
        self.db = self.get_database()

    @staticmethod
    def get_database():
        client = MongoClient(settings.DATABASE_URL).get_database("kenwin").get_collection("task")
        return client

    def get_all_by_owner_id(self, owner_id: str, filter_by: str):

        if filter_by == "all":
            task_list = self.db.find({"owner_id": owner_id})
            json_data = dumps(task_list)
            json_response = json.loads(json_data)
            formated_response = self.format_model_response(json_response)
            return formated_response

        task_list = self.db.find({"status": filter_by, "owner_id": owner_id})
        json_data = dumps(task_list)
        json_response = json.loads(json_data)
        formated_response = self.format_model_response(json_response)
        return formated_response

    def get_by_id(self, task_id: str, owner_id) -> Task:
        if not ObjectId.is_valid(task_id):
            raise IncorrectObjectIdException()
        task_dict = self.db.find_one({"_id": ObjectId(task_id)})
        if not task_dict:
            raise TaskNotFoundException()
        if task_dict.get("owner_id") != owner_id:
            raise NotSameOwnerTaskException()
        if task_dict.get("status") == "finished":
            return Task(status=task_dict["status"],
                        id=str(task_dict["_id"]),
                        owner_id=task_dict["owner_id"],
                        description=task_dict["description"],
                        difficult=task_dict["difficult"],
                        date_created=(task_dict["date_created"]),
                        date_finished=(task_dict["date_finished"]))

        return Task(status=task_dict["status"],
                    id=str(task_dict["_id"]),
                    owner_id=task_dict["owner_id"],
                    description=task_dict["description"],
                    difficult=task_dict["difficult"],
                    date_created=(task_dict["date_created"])
                    )

    def save(self, task: Task):
        return self.db.insert_one(task.dict())

    def update(self, task_id, task):
        if not ObjectId.is_valid(task_id):
            raise IncorrectObjectIdException()
        self.db.find_one_and_update({"_id": ObjectId(task_id)}, {"$set": task.dict()}, {'_id': 0})

    @staticmethod
    def format_model_response(json_response: dict) -> dict:
        for results in json_response:
            task_id = results.get("_id").get("$oid")
            results["id"] = task_id
            results.pop("_id")
            date_created = str(results.get("date_created").get("$date"))
            results.pop("date_created")
            results["date_created"] = date_created
        return json_response


import enum
import json
from typing import Dict

import discord

class DiscordEvent(enum.Enum):
    MESSAGE = "message"
    MENTION = "mention"
    USER_STATUS = "user_status"
    USER_TYPING = "user_typing"

# Create a new client object
intents = discord.Intents.all()
client = discord.Client(intents=intents)

# function to log event and data
def log_event(event: DiscordEvent, data: Dict):
    print(f"Event: {event.value}, Data: {json.dumps(data)}")

# Use the on_ready() event to know when the bot is connected and authenticated
@client.event
async def on_ready():
    log_event(DiscordEvent.USER_STATUS, {"status": "authenticated"})

# Use the on_message() event to listen for new messages
@client.event
async def on_message(message):
    log_event(DiscordEvent.MESSAGE, {"content": message.content})

# Use the on_member_update() event to listen for user status message changes
@client.event
async def on_member_update(before, after):
    if before.status != after.status:
        log_event(
            DiscordEvent.USER_STATUS,
            {"username": after.name, "before": before.status, "after": after.status},
        )

# Use the on_mention() event to listen for new mentions
@client.event
async def on_message(message):
    if message.mention_everyone or message.mentions:
        log_event(DiscordEvent.MENTION, {"content": message.content})

# Listen for when a user starts typing
@client.event
async def on_typing(channel, user, when):
    log_event(
        DiscordEvent.USER_TYPING,
        {"channel": channel.name, "user": user.name, "when": when},
    )

# Authenticate with Discord using your token
client.run("<your bot token>")


from os import listdir
from easygui import diropenbox
import tweet_tk.tweets_to_df as tweets_to_df
from pandas import DataFrame
from time import strftime
from pickle import dump

# open path containing all jsion files
path = diropenbox()
datasets = [f for f in listdir(path) if f.split('.')[1] == 'json']
agg_dataset = DataFrame()

# load all jsons into agg_dataset
for f in datasets:
    curr_path = path+'\\'+f
    print(curr_path)
    df = tweets_to_df.tweet_json_to_df(curr_path)
    print('loaded',f,'size:',len(df))
    agg_dataset = agg_dataset.append(df)

print('\ndone aggregating. total size:',len(agg_dataset))

# drop aggregated dataset into pickle
agg_file_name = path+'/agg_' + strftime("%Y_%m_%d_%H_%M_%S") + '.pickle'

save_file = open(agg_file_name,'wb')
dump(agg_dataset,save_file)
save_file.close()
# agg_dataset


#-*- coding: UTF-8 -*-
import datetime
import unittest
import  ddt
from utils.connect_sql import dbConnect
from getdata.ExcelUtil import excelHandle
from pageobjects.common.topMenu import TopMenuPage
from utils.browser_engine import driver
from pageobjects.customer.newCustomer import  NewCustomerPage
from pageobjects.customer.customerRecord import  CustomerRecordPage
from utils.basepath_helper import excel_path

nowTime = datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S")
filepath = excel_path +"TestData.xlsx"
sheetName = "Customer_Relate"
@ddt.ddt
class newCustomerRelaInfo(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.driver = driver

    data = excelHandle(filepath, sheetName).read_excel()
    @ddt.data(*data)
    def test_01_Same_Above(self, data):
        u"""New Customer selected same as above"""
        entityType = data['entityType']
        salutation = data['salutation']
        entityClass = data['entityClass']
        suffix = data['suffix']
        typeOfBusiness = data['typeOfBusiness']
        stateOfIncorporation = data['stateOfIncorporation']
        emailType = data['emailType']
        phoneType = data['type1']

        TopMenuPage(self.driver).select_multiple_menu(2, "customers", "New", "", "")
        # CustomerRecordPage(self.driver).top_operate("Actions ", "New")
        NewCustomerPage(self.driver).business_entity(entityType, entityClass, salutation, suffix, typeOfBusiness,
                                                     stateOfIncorporation)
        NewCustomerPage(self.driver).contact(True, 1, 1,"", emailType, phoneType)
        self.assertTrue(NewCustomerPage(self.driver).save())
        self.assertTrue(NewCustomerPage(self.driver).validation_data())

    @ddt.data(*data)
    def test_02_two_contact(self,data):
        u"""New Customer with two contact,address,identifier"""
        entityType = data['entityType']
        entityClass = data['entityClass']
        salutation = data['salutation']
        suffix = data['suffix']
        typeOfBusiness = data['typeOfBusiness']
        stateOfIncorporation = data['stateOfIncorporation']
        role = data['role']
        emailType = data['emailType']
        phoneType1 = data['type1']
        phoneType2 = data['type2']
        phoneType3 = data['type3']
        # addressType = data['addressType']
        # stateCode = data['stateCode']
        # type = data['identifierName']
        addressType = dbConnect().getdata('MCDH', 'addressType','')
        stateCode = dbConnect().getdata('ALAMO', 'stateCode','')
        type = dbConnect().getdata('MCDH', 'identifierNameWithoutBan','')

        TopMenuPage(self.driver).select_multiple_menu(2, "customers", "New", "", "")
        # CustomerRecordPage(self.driver).top_operate("Actions ", "New")
        NewCustomerPage(self.driver).business_entity(entityType, entityClass, salutation, suffix,
                                                     typeOfBusiness, stateOfIncorporation)
        NewCustomerPage(self.driver).contact(False, 1, 1, role, emailType, phoneType1)
        NewCustomerPage(self.driver).add_phone("Phone 2", 1, 3, phoneType2)
        NewCustomerPage(self.driver).add_phone("Phone 3", 1, 5, phoneType3)
        NewCustomerPage(self.driver).contact(False, 3, 1, role, emailType, phoneType1)
        NewCustomerPage(self.driver).add_phone("Phone 2", 3, 3, phoneType2)
        NewCustomerPage(self.driver).add_phone("Phone 3", 3, 5, phoneType3)
        NewCustomerPage(self.driver).address("Address", addressType, stateCode)
        NewCustomerPage(self.driver).identifier("Identifier", type)
        self.assertTrue(NewCustomerPage(self.driver).save())
        self.assertTrue(NewCustomerPage(self.driver).validation_data())


import keyboard
import os
from pygame import mixer # Load the required library

def play_audio(filename):

    print(os.path.dirname(os.path.abspath(__file__)) + "\\" + filename)

    mixer.init()
    mixer.music.load(os.path.dirname(os.path.abspath(__file__)) + "\\" + filename)
    mixer.music.play()
    while mixer.music.get_busy() == True:
        try:
            if keyboard.is_pressed('esc'):
                print('Music Stopped')
                mixer.music.stop()
                break
            else:
                pass
        except:
            continue

if __name__ == "__main__":
    play_audio("lullaby_goodnight.mp3")


import pytest
from ..horizontal_rule_parser import parse_horiontal_rule

@pytest.mark.parametrize("content, start, end", [('***', 0, 3), ('____', 0, 4),
                                                 ('-----', 0, 5),
                                                 (' ------', 0, 7),
                                                 ('______ ', 0, 7),
                                                 ('______ \n', 0, 8),
                                                 ('''

-----

''', 3, 9)])
def test_parse(content, start, end):
    (start_index, end_index, hori_rule) = parse_horiontal_rule(content)
    assert start_index == start
    assert end_index == end
    assert hori_rule._content == ''

@pytest.mark.parametrize(
    "content",
    [
        # not match
        ('**'),
        ('*_-'),
        (r'\---'),
        ('a___'),
        ('*****a'),
        ('***a**'),
        ('*** ***'),
    ])
def test_parse_not_match(content):
    (start_index, end_index, hori_rule) = parse_horiontal_rule(content)
    assert start_index == -1
    assert end_index == -1
    assert hori_rule is None


import datetime

from backend.models.mysql_connection_pool import MySQLPool

class participacionModel:
    def __init__(self):
        self.mysql_pool = MySQLPool()

    def get_participacion(self, id_participacion):
        params = {'id_participacion' : id_participacion}
        rv = self.mysql_pool.execute("""SELECT p.id_participacion, p.nota_final, s.nombre_seccion, concat(u.nombre, ' ', u.apellido) as estudiante from Participacion p
                                    inner join Horario h on p.id_horario = h.id_horario
                                    inner join Seccion s on h.id_seccion = s.id_seccion
                                    inner join Estudiante e on p.id_estudiante = e.id_estudiante
                                    inner join Usuario u on e.dni = u.dni where p.id_participacion=%(id_participacion)s""", params)
        data = []
        content = {}
        for result in rv:
            content = {'id_participacion': result[0], 'fecha': result[1], 'nota_final': result[2], 'id_horario': result[3], 'nombre_seccion': result[11]}
            data.append(content)
            content = {}
        return data

    def get_participacions(self):
        rv = self.mysql_pool.execute("""SELECT p.id_participacion, p.nota_final, s.nombre_seccion, concat(u.nombre, ' ', u.apellido) as estudiante from Participacion p
                                    inner join Horario h on p.id_horario = h.id_horario
                                    inner join Seccion s on h.id_seccion = s.id_seccion
                                    inner join Estudiante e on p.id_estudiante = e.id_estudiante
                                    inner join Usuario u on e.dni = u.dni""")
        data = []
        content = {}
        for result in rv:
            content = {'id_participacion': result[0], 'nota_final': result[1], 'nombre_seccion': result[2], 'estudiante': result[3]}
            data.append(content)
            content = {}
        return data

    def create_participacion(self, nota_final, id_horario, id_estudiante):
        data = {
            'fecha' : datetime.date.today().strftime('%Y-%m-%d'),
            'nota_final' : nota_final,
            'id_horario' : id_horario,
            'id_estudiante' : id_estudiante
        }
        query = """insert into Participacion(fecha, nota_final, id_horario, id_estudiante)
        values (%(fecha)s, %(nota_final)s, %(id_horario)s, %(id_estudiante)s)"""
        cursor = self.mysql_pool.execute(query, data, commit=True)

        data['id_participacion'] = cursor.lastrowid
        return data

    def update_participacion(self, id_participacion, nota_final):
        data = {
            'id_participacion' : id_participacion,
            'nota_final' : nota_final
        }
        query = """update Participacion set nota_final = %(nota_final)s
                    where id_participacion = %(id_participacion)s"""
        cursor = self.mysql_pool.execute(query, data, commit=True)
        result = {'result':1}
        return result

    def delete_participacion(self, id_participacion):
        params = {'id_participacion' : id_participacion}
        query = """delete from Participacion where id_participacion = %(id_participacion)s"""
        self.mysql_pool.execute(query, params, commit=True)
        result = {'result': 1}
        return result


import logging

import RPi.GPIO as GPIO

import Canvas
from CustomScript import CustomScript
from helpers.Color import Color

class _WakeUpLightFastForward(CustomScript):
    def __init__(self, canvas, send_object, send_object_to_all, start_script, restart_self, set_frame_period,
                 set_frame_rate, get_connected_clients):
        super().__init__(canvas, send_object, send_object_to_all, start_script, restart_self, set_frame_period,
                         set_frame_rate, get_connected_clients)
        # setup
        self.logger = logging.getLogger("script:FF_wuplight")
        GPIO.setmode(GPIO.BCM)
        GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)
        self.set_frame_rate(24)
        self._last_force_switch_status = self._read_force_switch()
        self._light_percentage = 0
        self.logger.info("Started")

    def _read_force_switch(self) -> bool:
        return GPIO.input(17) == GPIO.LOW

    def update(self, canvas):
        force_switch = self._read_force_switch()
        if force_switch != self._last_force_switch_status:
            self._light_percentage = -0.2
            self.logger.info("Reset light percentage")
            self._last_force_switch_status = force_switch

        if self._light_percentage < 0:
            light_percentage = 0
        elif self._light_percentage > 1:
            light_percentage = 1
        else:
            light_percentage = self._light_percentage

        color_temp = 1400 + (5500 - 1400) * light_percentage
        color_value = Color.from_temperature(color_temp, light_percentage)
        self.logger.info("Color temp is {} from light percentage {}".format(color_temp, light_percentage))
        self.current_color = color_value

        self._light_percentage += 0.005

        if self._light_percentage > 1.2:
            self.logger.info("Reset light percentage, was above 1.5")
            self._light_percentage = -0.2

    def draw(self, canvas: Canvas):
        canvas.clear(self.current_color)

    def on_data(self, json, source_id):
        pass


#!/usr/bin/env python

import socket
import time
import asyncio

async def send_n_recv_data(question):
    ip = '127.0.0.1'
    tcp_port = 5005

    sockfd = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sockfd.connect((ip, tcp_port))
    sockfd.send(question.encode())
    data = sockfd.recv(1024)
    await asyncio.sleep(1)
    sockfd.close()

    return(data)

async def get_answer_by_question(question):
    print(f"Quesion: {question}")
    reply = send_n_recv_data(question)
	#await asyncio.sleep(1)
    print(f"Asnwer : {reply}")
    print()

async def main():
    task1 = loop.create_taskget_answer_by_question("One-page question")
    #await asyncio.sleep(1)
    task2 = loop.create_taskget_answer_by_question("One-line question")
    task3 = loop.create_taskget_answer_by_question("Booleon question")

    await asyncio.wait([task1, task2, task3])
    return task1, task2, task3

if (__name__ == "__main__"):
    print(f"started at {time.strftime('%X')}")
    try:
        loop = asyncio.get_event_loop()
        r1, r2, r3 = loop.run_until_complete(main())
        print(f"{r1.result()}")
        print(f"{r2.result()}")
        print(f"{r3.result()}")
    except Exception as e:
        pass
    finally:
        loop.close()
    print(f"Ended at {time.strftime('%X')}")


import collections
import functools

import tensorflow as tf

from tensorflow_federated.python.core.api import computations
from tensorflow_federated.python.core.api import intrinsics
from tensorflow_federated.python.learning import model

class LinearRegression(model.Model):
  """Example of a simple linear regression implemented directly."""

  # A tuple (x, y), where 'x' represent features, and 'y' represent labels.
  Batch = collections.namedtuple('Batch', ['x', 'y'])  # pylint: disable=invalid-name

  def __init__(self, feature_dim=2):
    # Define all the variables, similar to what Keras Layers and Models
    # do in build().
    self._feature_dim = feature_dim
    # TODO(b/124070381): Support for integers in num_examples, etc., is handled
    # here in learning, by adding an explicit cast to a float where necessary in
    # order to pass typechecking in the reference executor.
    self._num_examples = tf.Variable(0, trainable=False)
    self._num_batches = tf.Variable(0, trainable=False)
    self._loss_sum = tf.Variable(0.0, trainable=False)
    self._a = tf.Variable([[0.0]] * feature_dim, trainable=True)
    self._b = tf.Variable(0.0, trainable=True)
    # Define a non-trainable model variable (another bias term) for code
    # coverage in testing.
    self._c = tf.Variable(0.0, trainable=False)
    self._input_spec = LinearRegression.make_batch(
        x=tf.TensorSpec([None, self._feature_dim], tf.float32),
        y=tf.TensorSpec([None, 1], tf.float32))

  @property
  def trainable_variables(self):
    return [self._a, self._b]

  @property
  def non_trainable_variables(self):
    return [self._c]

  @property
  def local_variables(self):
    return [self._num_examples, self._num_batches, self._loss_sum]

  @property
  def input_spec(self):
    # Model expects batched input, but the batch dimension is unspecified.
    return self._input_spec

  @tf.function
  def _predict(self, x):
    return tf.matmul(x, self._a) + self._b + self._c

  @tf.function
  def forward_pass(self, batch, training=True):
    del training  # Unused.
    if isinstance(batch, dict):
      batch = self.make_batch(**batch)
    if not self._input_spec.y.is_compatible_with(batch.y):
      raise ValueError('Expected batch.y to be compatible with '
                       '{} but found {}'.format(self._input_spec.y, batch.y))
    if not self._input_spec.x.is_compatible_with(batch.x):
      raise ValueError('Expected batch.x to be compatible with '
                       '{} but found {}'.format(self._input_spec.x, batch.x))
    predictions = self._predict(batch.x)
    residuals = predictions - batch.y
    num_examples = tf.gather(tf.shape(predictions), 0)
    total_loss = 0.5 * tf.reduce_sum(tf.pow(residuals, 2))

    self._loss_sum.assign_add(total_loss)
    self._num_examples.assign_add(num_examples)
    self._num_batches.assign_add(1)

    average_loss = total_loss / tf.cast(num_examples, tf.float32)
    return model.BatchOutput(
        loss=average_loss, predictions=predictions, num_examples=num_examples)

  @tf.function
  def report_local_outputs(self):
    return collections.OrderedDict([
        ('num_examples', self._num_examples),
        ('num_examples_float', tf.cast(self._num_examples, tf.float32)),
        ('num_batches', self._num_batches),
        ('loss', self._loss_sum / tf.cast(self._num_examples, tf.float32)),
    ])

  @property
  def federated_output_computation(self):

    @computations.federated_computation
    def fed_output(local_outputs):
      # TODO(b/124070381): Remove need for using num_examples_float here.
      return {
          'num_examples':
              intrinsics.federated_sum(local_outputs.num_examples),
          'loss':
              intrinsics.federated_mean(
                  local_outputs.loss, weight=local_outputs.num_examples_float),
      }

    return fed_output

  @classmethod
  def make_batch(cls, x, y):
    """Returns a `Batch` to pass to the forward pass."""
    return cls.Batch(x, y)

def _dense_all_zeros_layer(input_dims=None, output_dim=1):
  """Create a layer that can be used in isolation for linear regression.

  Constructs a Keras dense layer with a single output, using biases and weights
  that are initialized to zero. No activation function is applied. When this is
  the only layer in a model, the model is effectively a linear regression model.

  Args:
    input_dims: the integer length of the input to this layers. Maybe None if
      the layer input size does not need to be specified.
    output_dim: the integer length of the flattened output tensor. Defaults to
      one, effectively making the layer perform linear regression.

  Returns:
    a `tf.keras.layers.Dense` object.
  """
  build_keras_dense_layer = functools.partial(
      tf.keras.layers.Dense,
      units=output_dim,
      use_bias=True,
      kernel_initializer='zeros',
      bias_initializer='zeros',
      activation=None)
  if input_dims is not None:
    return build_keras_dense_layer(input_shape=(input_dims,))
  return build_keras_dense_layer()

def _dense_all_zeros_regularized_layer(input_dims=None,
                                       output_dim=1,
                                       regularization_constant=0.01):
  """Create a layer that can be used in isolation for linear regression.

  Constructs a Keras dense layer with a single output, using biases and weights
  that are initialized to zero. No activation function is applied. When this is
  the only layer in a model, the model is effectively a linear regression model.
  The regularization constant is used to scale L2 regularization on the weights
  and bias.

  Args:
    input_dims: the integer length of the input to this layers. Maybe None if
      the layer input size does not need to be specified.
    output_dim: the integer length of the flattened output tensor. Defaults to
      one, effectively making the layer perform linear regression.
    regularization_constant: the float scaling magnitude (lambda) for L2
      regularization on the layer's weights and bias.

  Returns:
    a `tf.keras.layers.Dense` object.
  """
  regularizer = tf.keras.regularizers.l2(regularization_constant)
  build_keras_dense_layer = functools.partial(
      tf.keras.layers.Dense,
      units=output_dim,
      use_bias=True,
      kernel_initializer='zeros',
      bias_initializer='zeros',
      kernel_regularizer=regularizer,
      bias_regularizer=regularizer,
      activation=None)
  if input_dims is not None:
    return build_keras_dense_layer(input_shape=(input_dims,))
  return build_keras_dense_layer()

def _dense_all_ones_regularized_layer(input_dims=None,
                                      output_dim=1,
                                      regularization_constant=0.01):
  """Create a layer that can be used in isolation for linear regression.

  Constructs a Keras dense layer with a single output, using biases and weights
  that are initialized to ones. No activation function is applied. When this is
  the only layer in a model, the model is effectively a linear regression model.
  The regularization constant is used to scale L2 regularization on the weights
  and bias.

  Args:
    input_dims: the integer length of the input to this layers. Maybe None if
      the layer input size does not need to be specified.
    output_dim: the integer length of the flattened output tensor. Defaults to
      one, effectively making the layer perform linear regression.
    regularization_constant: the float scaling magnitude (lambda) for L2
      regularization on the layer's weights and bias.

  Returns:
    a `tf.keras.layers.Dense` object.
  """
  regularizer = tf.keras.regularizers.l2(regularization_constant)
  build_keras_dense_layer = functools.partial(
      tf.keras.layers.Dense,
      units=output_dim,
      use_bias=True,
      kernel_initializer='ones',
      bias_initializer='ones',
      kernel_regularizer=regularizer,
      bias_regularizer=regularizer,
      activation=None)
  if input_dims is not None:
    return build_keras_dense_layer(input_shape=(input_dims,))
  return build_keras_dense_layer()

def build_linear_regression_keras_sequential_model(feature_dims=2):
  """Build a linear regression `tf.keras.Model` using the Sequential API."""
  keras_model = tf.keras.models.Sequential()
  keras_model.add(_dense_all_zeros_layer(feature_dims))
  return keras_model

def build_linear_regression_regularized_keras_sequential_model(
    feature_dims=2, regularization_constant=0.01):
  """Build a linear regression `tf.keras.Model` using the Sequential API."""
  keras_model = tf.keras.models.Sequential()
  keras_model.add(
      _dense_all_zeros_regularized_layer(
          feature_dims, regularization_constant=regularization_constant))
  return keras_model

def build_linear_regression_ones_regularized_keras_sequential_model(
    feature_dims=2, regularization_constant=0.01):
  """Build a linear regression `tf.keras.Model` using the Sequential API."""
  keras_model = tf.keras.models.Sequential()
  keras_model.add(
      _dense_all_ones_regularized_layer(
          feature_dims, regularization_constant=regularization_constant))
  return keras_model

def build_linear_regression_keras_functional_model(feature_dims=2):
  """Build a linear regression `tf.keras.Model` using the functional API."""
  a = tf.keras.layers.Input(shape=(feature_dims,), dtype=tf.float32)
  b = _dense_all_zeros_layer()(a)
  return tf.keras.Model(inputs=a, outputs=b)

def build_linear_regression_keras_subclass_model(feature_dims=2):
  """Build a linear regression model by sub-classing `tf.keras.Model`."""
  del feature_dims  # Unused.

  class _KerasLinearRegression(tf.keras.Model):

    def __init__(self):
      super().__init__()
      self._weights = _dense_all_zeros_layer()

    def call(self, inputs, training=True):
      return self._weights(inputs)

  return _KerasLinearRegression()

def build_embedding_keras_model(vocab_size=10):
  """Builds a test model with an embedding initialized to one-hot vectors."""
  keras_model = tf.keras.models.Sequential()
  keras_model.add(tf.keras.layers.Embedding(input_dim=vocab_size, output_dim=5))
  keras_model.add(tf.keras.layers.Softmax())
  return keras_model

def build_conv_batch_norm_keras_model():
  """Builds a test model with convolution and batch normalization."""
  # This is an example of a model that has trainable and non-trainable
  # variables.
  l = tf.keras.layers
  data_format = 'channels_last'
  max_pool = l.MaxPooling2D((2, 2), (2, 2),
                            padding='same',
                            data_format=data_format)
  keras_model = tf.keras.models.Sequential([
      l.Reshape(target_shape=[28, 28, 1], input_shape=(28 * 28,)),
      l.Conv2D(
          32,
          5,
          padding='same',
          data_format=data_format,
          activation=tf.nn.relu,
          kernel_initializer='zeros',
          bias_initializer='zeros'),
      max_pool,
      l.BatchNormalization(),
      l.Conv2D(
          64,
          5,
          padding='same',
          data_format=data_format,
          activation=tf.nn.relu,
          kernel_initializer='zeros',
          bias_initializer='zeros'),
      max_pool,
      l.BatchNormalization(),
      l.Flatten(),
      l.Dense(
          1024,
          activation=tf.nn.relu,
          kernel_initializer='zeros',
          bias_initializer='zeros'),
      l.Dropout(0.4),
      l.Dense(10, kernel_initializer='zeros', bias_initializer='zeros'),
  ])
  return keras_model

def build_multiple_inputs_keras_model():
  """Builds a test model with two inputs."""
  l = tf.keras.layers
  a = l.Input((1,), name='a')
  b = l.Input((1,), name='b')
  # Each input has a single, independent dense layer, which are combined into
  # a final dense layer.
  output = l.Dense(1)(
      l.concatenate([
          l.Dense(1)(a),
          l.Dense(1)(b),
      ]))
  return tf.keras.Model(inputs={'a': a, 'b': b}, outputs=[output])

def build_multiple_outputs_keras_model():
  """Builds a test model with three outputs."""
  l = tf.keras.layers
  a = l.Input((1,))
  b = l.Input((1,))

  output_a = l.Dense(1)(a)
  output_b = l.Dense(1)(b)
  output_c = l.Dense(1)(l.concatenate([l.Dense(1)(a), l.Dense(1)(b)]))

  return tf.keras.Model(inputs=[a, b], outputs=[output_a, output_b, output_c])

def build_multiple_outputs_regularized_keras_model(
    regularization_constant=0.01):
  """Builds a test model with three outputs.

  All weights are initialized to ones.

  Args:
    regularization_constant: L2 scaling constant (lambda) for all weights and
      biases.

  Returns:
    a `tf.keras.Model` object.
  """
  dense = functools.partial(
      _dense_all_ones_regularized_layer,
      output_dim=1,
      regularization_constant=regularization_constant)
  a = tf.keras.layers.Input((1,))
  b = tf.keras.layers.Input((1,))

  output_a = dense()(a)
  output_b = dense()(b)
  output_c = dense()(tf.keras.layers.concatenate([dense()(a), dense()(b)]))

  return tf.keras.Model(inputs=[a, b], outputs=[output_a, output_b, output_c])

def build_lookup_table_keras_model():
  """Builds a test model with embedding feature columns."""
  l = tf.keras.layers
  a = l.Input(shape=(1,), dtype=tf.string)
  embedded_lookup_feature = tf.feature_column.embedding_column(
      tf.feature_column.categorical_column_with_vocabulary_list(
          key='colors', vocabulary_list=('R', 'G', 'B')),
      dimension=16)
  dense_features = l.DenseFeatures([embedded_lookup_feature])({'colors': a})
  output = l.Dense(1)(dense_features)
  return tf.keras.Model(inputs=[a], outputs=[output])

def build_preprocessing_lookup_keras_model():
  """Builds a test model using processing layers."""
  l = tf.keras.layers
  a = l.Input(shape=(1,), dtype=tf.string)
  encoded = l.experimental.preprocessing.StringLookup(vocabulary=['A', 'B'])(a)
  return tf.keras.Model(inputs=[a], outputs=[encoded])


from dataclasses import dataclass, asdict
from enum import Enum, auto
from typing import TYPE_CHECKING, Optional, List

from cloudfoundry_client.v3.entities import EntityManager, ToManyRelationship, Entity, ToOneRelationship

if TYPE_CHECKING:
    from cloudfoundry_client.client import CloudFoundryClient

class RuleProtocol(Enum):
    TCP = auto()
    UDP = auto()
    ICMP = auto()
    ALL = auto()

    def __repr__(self):
        return '%s' % self.name.lower()

@dataclass
class Rule:
    protocol: RuleProtocol
    destination: str
    ports: Optional[str] = None
    type: Optional[int] = None
    code: Optional[int] = None
    description: Optional[str] = None
    log: Optional[bool] = None

@dataclass
class GloballyEnabled:
    running: Optional[bool] = None
    staging: Optional[bool] = None

class SecurityGroupManager(EntityManager):
    def __init__(self, target_endpoint: str, client: "CloudFoundryClient"):
        super(SecurityGroupManager, self).__init__(target_endpoint, client, "/v3/security_groups")

    def create(self,
               name: str,
               rules: Optional[List[Rule]] = None,
               globally_enabled: Optional[GloballyEnabled] = None,
               staging_spaces: Optional[ToManyRelationship] = None,
               running_spaces: Optional[ToManyRelationship] = None) -> Entity:
        payload = self._generate_payload(name, rules, globally_enabled, staging_spaces, running_spaces)
        return super()._create(payload)

    def update(self,
               security_group_id: str,
               name: Optional[str] = None,
               rules: Optional[List[Rule]] = None,
               globally_enabled: Optional[GloballyEnabled] = None,
               staging_spaces: Optional[ToManyRelationship] = None,
               running_spaces: Optional[ToManyRelationship] = None) -> Entity:
        payload = self._generate_payload(name, rules, globally_enabled, staging_spaces, running_spaces)
        return super()._update(security_group_id, payload)

    def remove(self, security_group_id: str, asynchronous: bool = True) -> Optional[str]:
        return super()._remove(security_group_id, asynchronous)

    def bind_running_security_group_to_spaces(self, security_group_id: str, space_guids: ToManyRelationship) \
            -> ToManyRelationship:
        relationship = "running_spaces"
        return self._bind_spaces(security_group_id, space_guids, relationship)

    def bind_staging_security_group_to_spaces(self, security_group_id: str, space_guids: ToManyRelationship) \
            -> ToManyRelationship:
        relationship = "staging_spaces"
        return self._bind_spaces(security_group_id, space_guids, relationship)

    def unbind_running_security_group_from_space(self, security_group_id: str, space_guid: ToOneRelationship):
        relationship = "running_spaces"
        return self._unbind_space(security_group_id, space_guid, relationship)

    def unbind_staging_security_group_from_space(self, security_group_id: str, space_guid: ToOneRelationship):
        relationship = "staging_spaces"
        return self._unbind_space(security_group_id, space_guid, relationship)

    def _bind_spaces(self, security_group_id: str, space_guids: ToManyRelationship, relationship: str) \
            -> ToManyRelationship:
        url = "%s%s/%s/relationships/%s" % (self.target_endpoint, self.entity_uri, security_group_id, relationship)
        return ToManyRelationship.from_json_object(super()._post(url, space_guids))

    def _unbind_space(self, security_group_id: str, space_guid: ToOneRelationship, relationship: str):
        url = "%s%s/%s/relationships/%s/%s" \
              % (self.target_endpoint, self.entity_uri, security_group_id, relationship, space_guid.guid)
        super()._delete(url)

    @staticmethod
    def _generate_payload(name: Optional[str],
                          rules: Optional[List[Rule]],
                          globally_enabled: Optional[GloballyEnabled],
                          staging_spaces: Optional[ToManyRelationship],
                          running_spaces: Optional[ToManyRelationship]):
        payload = {}
        if name:
            payload["name"] = name
        if rules:
            payload["rules"] = [asdict(rule, dict_factory=lambda x: {k: repr(v) if k == "protocol" else v
                                                                     for (k, v) in x if v is not None})
                                for rule in rules]
        if globally_enabled:
            payload["globally_enabled"] = asdict(globally_enabled,
                                                 dict_factory=lambda x: {k: v for (k, v) in x if v is not None})
        relationships = dict()
        if staging_spaces:
            relationships["staging_spaces"] = staging_spaces
        if running_spaces:
            relationships["running_spaces"] = running_spaces
        if len(relationships) > 0:
            payload["relationships"] = relationships
        return payload


# Author:Zhang Yuan
from MyPackage import *
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import seaborn as sns
import statsmodels.api as sm
from scipy import stats

# ------------------------------------------------------------
__mypath__ = MyPath.MyClass_Path("")  # 路径类
mylogging = MyDefault.MyClass_Default_Logging(activate=False)  # 日志记录类，需要放在上面才行
myfile = MyFile.MyClass_File()  # 文件操作类
myword = MyFile.MyClass_Word()  # word生成类
myexcel = MyFile.MyClass_Excel()  # excel生成类
myini = MyFile.MyClass_INI()  # ini文件操作类
mytime = MyTime.MyClass_Time()  # 时间类
myparallel = MyTools.MyClass_ParallelCal()  # 并行运算类
myplt = MyPlot.MyClass_Plot()  # 直接绘图类(单个图窗)
mypltpro = MyPlot.MyClass_PlotPro()  # Plot高级图系列
myfig = MyPlot.MyClass_Figure(AddFigure=False)  # 对象式绘图类(可多个图窗)
myfigpro = MyPlot.MyClass_FigurePro(AddFigure=False)  # Figure高级图系列
mynp = MyArray.MyClass_NumPy()  # 多维数组类(整合Numpy)
mypd = MyArray.MyClass_Pandas()  # 矩阵数组类(整合Pandas)
mypdpro = MyArray.MyClass_PandasPro()  # 高级矩阵数组类
myDA = MyDataAnalysis.MyClass_DataAnalysis()  # 数据分析类
myDefault = MyDefault.MyClass_Default_Matplotlib()  # 画图恢复默认设置类
# myMql = MyMql.MyClass_MqlBackups() # Mql备份类
# myBaidu = MyWebCrawler.MyClass_BaiduPan() # Baidu网盘交互类
# myImage = MyImage.MyClass_ImageProcess()  # 图片处理类
myBT = MyBackTest.MyClass_BackTestEvent()  # 事件驱动型回测类
myBTV = MyBackTest.MyClass_BackTestVector()  # 向量型回测类
myML = MyMachineLearning.MyClass_MachineLearning()  # 机器学习综合类
mySQL = MyDataBase.MyClass_MySQL(connect=False)  # MySQL类
mySQLAPP = MyDataBase.MyClass_SQL_APPIntegration()  # 数据库应用整合
myWebQD = MyWebCrawler.MyClass_QuotesDownload(tushare=False)  # 金融行情下载类
myWebR = MyWebCrawler.MyClass_Requests()  # Requests爬虫类
myWebS = MyWebCrawler.MyClass_Selenium(openChrome=False)  # Selenium模拟浏览器类
myWebAPP = MyWebCrawler.MyClass_Web_APPIntegration()  # 爬虫整合应用类
myEmail = MyWebCrawler.MyClass_Email()  # 邮箱交互类
myReportA = MyQuant.MyClass_ReportAnalysis()  # 研报分析类
myFactorD = MyQuant.MyClass_Factor_Detection()  # 因子检测类
myKeras = MyDeepLearning.MyClass_tfKeras()  # tfKeras综合类
myTensor = MyDeepLearning.MyClass_TensorFlow()  # Tensorflow综合类
myMT5 = MyMql.MyClass_ConnectMT5(connect=False)  # Python链接MetaTrader5客户端类
myMT5Pro = MyMql.MyClass_ConnectMT5Pro(connect=False)  # Python链接MT5高级类
myMT5Indi = MyMql.MyClass_MT5Indicator()  # MT5指标Python版
myMT5Report = MyMT5Report.MyClass_StratTestReport(AddFigure=False)  # MT5策略报告类
myMT5Lots_Fix = MyMql.MyClass_Lots_FixedLever(connect=False)  # 固定杠杆仓位类
myMT5Lots_Dy = MyMql.MyClass_Lots_DyLever(connect=False)  # 浮动杠杆仓位类
myMT5run = MyMql.MyClass_RunningMT5()  # Python运行MT5
myMT5code = MyMql.MyClass_CodeMql5()  # Python生成MT5代码
myMoneyM = MyTrade.MyClass_MoneyManage()  # 资金管理类
myDefault.set_backend_default("Pycharm")  # Pycharm下需要plt.show()才显示图
# ------------------------------------------------------------

'''
根据MT5文章：利用箱形图（BOXPLOT）探索金融时间序列的季节性形态
https://www.mql5.com/zh/articles/7038
'''
#%%
myMT5Pro.__init__(connect=True)

rates_original = myMT5Pro.getsymboldata("EURUSD", "TIMEFRAME_D1", [2010,1,1], [2020,1,1], index_time=False)
# leave only 'time' and 'close' columns
rates = rates_original[["time","close"]]

# get percent change (price returns)
returns = pd.DataFrame(rates['close'].pct_change(1))
returns = returns.set_index(rates['time'])
returns = returns[1:]
returns.head(5)

# 把数据按照 年-月 group
indexyear = returns.index.year.rename('year')
indexmonth = returns.index.month.rename('month')
Monthly_Returns = returns.groupby([indexyear, indexmonth]).mean()
# 按月来画箱型图，每个箱型图是统计每年的指定月数据
Monthly_Returns.boxplot(column='close', by='month', figsize=(15, 8))
plt.show()
'''
我们可以看到，第五个月（五月份）的增幅中位数偏移到零轴下方，且其异常值显见高于零轴。 通常，从十年的统计数据里可以看出，五月份的市场相对于三月份有所下跌。 只有一个年份，五月份市场上涨。 这是一个有趣的思路，很符合交易者的格言“在五月份卖掉，并离开！”。
我们看一下五月份之后的六月份。 相对于五月份，六月份市场几乎总是（排除一年以外）在增长，这种情况每年都在重复。 六月份的波动范围很小，没有异常值（与五月份不同），这表明良好的季节性稳定。
请注意第 11 个月（十一月份）。 在此期间市场下跌的概率很高。 之后，在十二月份，市场通常会再度上行。 一月份（第一个月）的波动性很高，且相对于十二月份有所下跌。
所获得的数据可为交易决策提供很有用的基础条件概览。 而且，概率可以集成到交易系统当中。 例如，可以在某些月份执行更多的买卖操作。
'''

# 我们利用相同的 10 年度观察一周中每个单独交易日的价格增幅分布：
indexweek = returns.index.week.rename('week')
indexday = returns.index.dayofweek.rename('day')
Daily_Returns = returns.groupby([indexweek, indexday]).mean()
# 按 星期几 来统计
Daily_Returns.boxplot(column='close', by='day', figsize=(15, 8))
plt.show()
'''
此处零对应于星期一，四则对应于星期五。 根据价格范围，按日波动率几乎保持不变。 然而不能据此得出结论，即在一周中的某个特定日期交易更为密集。 平均来说，市场在星期一和星期五时更倾向于下行非上行。
'''
# 也许在一些单独的月份中，按日分布的样子会有所不同。
# leave only one month "returns.index[~returns.index.month.isin([1])"
for i in range(1,13): # i=1
    returns_solo = returns.drop(returns.index[~returns.index.month.isin([i])])
    indexweek_solo = returns_solo.index.week.rename('week')
    indexday_solo = returns_solo.index.dayofweek.rename('day')
    Daily_Returns_solo = returns_solo.groupby([indexweek_solo, indexday_solo]).mean()
    # 按 星期几 来统计
    Daily_Returns_solo.boxplot(column='close', by='day', figsize=(15, 8))
    plt.suptitle("Boxplot grouped by day month_%s"%i)
    plt.show()

#%%
### 分析日内形态
# 在创建交易系统时，通常要考虑日内的分布，例如，除了日线和月线的分布，还要用到小时线数据。 这轻易就可做到。
rates_original = myMT5Pro.getsymboldata("EURUSD", "TIMEFRAME_M15", [2010,1,1], [2019, 11, 25], index_time=False)
# leave only 'time' and 'close' columns
rates = rates_original[["time","close"]]
# get percent change (price returns)
returns = pd.DataFrame(rates['close'].pct_change(1))
returns = returns.set_index(rates['time'])
returns = returns[1:]

# 将数据按日和小时分组
indexday = returns.index.day.rename('day')
indexhour = returns.index.hour.rename('hour')
Hourly_Returns = returns.groupby([indexday, indexhour]).median()
Hourly_Returns.boxplot(column='close', by='hour', figsize=(10, 5))
plt.show()

#%%
### 按均线（MA）搜索去趋势化形态
# 正确检测趋势分量非常棘手。 有时，时间序列可能会太平滑。 在这种情况下，交易信号极少。 如果缩短平滑周期，那么高频成交可能无法负担点差和佣金。 我们编辑代码，以便利用移动平均线进行去趋势化：
rates_original = myMT5Pro.getsymboldata("EURUSD", "TIMEFRAME_M15", [2010,1,1], [2019, 11, 25], index_time=False)
# leave only 'time' and 'close' columns
rates = rates_original[["time","close"]]
rates = rates.set_index('time')
# set the moving average period
window = 25
# detrend tome series by MA
ratesM = rates.rolling(window).mean()
plt.figure(figsize=(10, 5))
plt.plot(rates)
plt.plot(ratesM)
plt.show()
# 我们得到了小时收盘价与 15 分钟移动平均线之间的平均偏差
ratesD = rates[window:] - ratesM[window:]
# 从收盘价中减去移动平均线值，得到一个去趋势化的时间序列
plt.plot(ratesD)
plt.show()

# 现在，我们得到每个交易小时的偏差分布小时统计信息：
Hourly_Returns = ratesD.groupby([ratesD.index.day.rename('day'), ratesD.index.hour.rename('hour')]).median()
Hourly_Returns.boxplot(column='close', by='hour', figsize=(15, 8))
plt.show()
# 下一个合乎逻辑的步骤是仔细检查分布矩，以便获得更准确的统计评估。 例如，以箱形图的形式计算得到的去趋势化序列的标准差：
Hourly_std = ratesD.groupby([ratesD.index.day.rename('day'), ratesD.index.hour.rename('hour')]).std()
Hourly_std.boxplot(column='close', by='hour', figsize=(15, 8))
plt.show()
# 另一个有趣的点是偏度。 我们计算一下：
Hourly_skew = ratesD.groupby([ratesD.index.day.rename('day'), ratesD.index.hour.rename('hour')]).skew()
Hourly_skew.boxplot(column='close', by='hour', figsize=(15, 8))
plt.show()
# 峰度的统计数据显示类似的结果：
Hourly_kurt = ratesD.groupby([ratesD.index.day.rename('day'), ratesD.index.hour.rename('hour')]).apply(pd.DataFrame.kurt)
Hourly_kurt.boxplot(column='close', by='hour', figsize=(15, 8))
plt.show()

#%%
### 搜索特定月份或一周中某天的周期形态，按均线去趋势化
rates_original = myMT5Pro.getsymboldata("EURUSD", "TIMEFRAME_M15", [2010,1,1], [2019, 11, 25], index_time=False)
# leave only 'time' and 'close' columns
rates = rates_original[["time","close"]]
rates = rates.set_index('time')
# set the moving average period
window = 25
# detrend tome series by MA
ratesM = rates.rolling(window).mean()
ratesD = rates[window:] - ratesM[window:]
# 仅分析指定月份
ratesD = ratesD.drop(ratesD.index[~ratesD.index.month.isin([11])])
Hourly_Returns = ratesD.groupby([ratesD.index.day.rename('day'), ratesD.index.hour.rename('hour')]).median()
Hourly_Returns.boxplot(column='close', by='hour', figsize=(11, 6))
plt.show()

# 仅分析指定周几
ratesD = ratesD.drop(ratesD.index[~ratesD.index.dayofweek.isin([0])])
Hourly_Returns = ratesD.groupby([ratesD.index.day.rename('day'), ratesD.index.hour.rename('hour')]).median()
Hourly_Returns.boxplot(column='close', by='hour', figsize=(11, 6))
plt.show()

Project_MT5文章调试/1.利用箱形图(BOXPLOT)探索金融时间序列的周期形态.py

1.利用箱形图(BOXPLOT)探索金融时间序列的周期形态.py


import numpy as np
from scipy.signal import fftconvolve
from scipy.ndimage import zoom
import warnings

from astropy.modeling import models
from astropy.units import Unit, Quantity

from specklepy.exceptions import SpecklepyTypeError, SpecklepyValueError
from specklepy.io import config
from specklepy.io import fits
from specklepy.logging import logger

class Telescope(object):
	"""Class carrying the parameters of a telescope.

	Attributes:
		diameter (astropy.units.Quantity):
		psf_source (str):
			Name of the PSF model or of the file, from which the PSF frames are extracted.
		psf_frame (int):
			Current frame of the PSF file.

	Optional attributes:
		central_obscuration (float, optional):
	"""

	__name__ = 'telescope'
	TIME_STEP_KEYS = ['TIMESTEP', 'INTTIME', 'CDELT3']
	RESOLUTION_KEYS = ['PIXSIZE', 'CDELT1']

	def __init__(self, diameter, psf_source, central_obscuration=None, psf_frame=0, **kwargs):
		"""Instantiate Telescope class:

		Args:
			diameter (float or astropy.units.Quantity):
				Telescope diameter, used to compute the light collecting area.
			psf_source (str):
				File name to read PSFs from or model name. Models can be either 'AiryDisk' or 'Gaussian'. The models
				require central_obscuration (float, optional): Radial fraction of the
				telescope aperture that is blocked by the secondary.
			central_obscuration (float, optional):
				Radial fraction that is centrally obscured, by the secondary mirror.
			psf_frame (int, optional):
				Index of the first frame to read from psf_source.
			kwargs:
				Are forwarded to the psf_source model.
		"""

		# Input parameters
		if isinstance(diameter, Quantity):
			self.diameter = diameter
		elif isinstance(diameter, (int, float)):
			logger.warning(f"Interpreting scalar type diameter as {diameter} m")
			self.diameter = Quantity(f"{diameter} m")
		elif isinstance(diameter, str):
			self.diameter = Quantity(diameter)
		else:
			raise SpecklepyTypeError('Telescope', 'diameter', type(diameter), 'Quantity')

		if isinstance(psf_source, str):
			self.psf_source = psf_source
		else:
			raise SpecklepyTypeError('Telescope', 'psf_source', type(psf_source), 'str')

		if isinstance(central_obscuration, float) or central_obscuration is None:
			self.central_obscuration = central_obscuration
		elif isinstance(central_obscuration, str):
			self.central_obscuration = float(central_obscuration)
		else:
			raise SpecklepyTypeError('Telescope', 'central_obscuration', type(central_obscuration), 'float')

		if isinstance(psf_frame, int):
			self.psf_frame = psf_frame
		else:
			raise SpecklepyTypeError('Telescope', 'psf_frame', type(psf_frame), 'int')

		# Derive secondary parameters
		if self.central_obscuration is not None:
			self.area = (1. - self.central_obscuration**2) * np.pi * (self.diameter / 2)**2
		else:
			self.area = np.pi * (self.diameter / 2)**2

		if psf_source.lower() in ['airydisk', 'gaussian']:
			self.model_psf(psf_source, **kwargs)
		else:
			psf, time_resolution, angular_resolution = self.read_psf_file(psf_source)
			self.psf = psf
			self.psf_resolution = angular_resolution
			if time_resolution is not None:
				self.timestep = time_resolution

	@staticmethod
	def from_file(par_file):
		params = config.read(par_file)

		# Try known first-level keys
		for key in ['TELESCOPE', 'Telescope', 'telescope']:
			if key in params.keys():
				return Telescope(**params[key])

		# Try full parameter set
		try:
			return Telescope(**params)
		except TypeError:
			raise RuntimeError(f"Could not identify parameters for initializing a Telescope instance in parameter "
							   f"file {par_file}")

	def __call__(self, *args, **kwargs):
		return self.get_photon_rate(*args, **kwargs)

	def __str__(self):
		tmp = "Telescope:\n"
		for key in self.__dict__:
			if key == 'psf':
				continue
			tmp += f"{key}: {self.__dict__[key]}\n"
		return tmp

	def model_psf(self, model, radius, psf_resolution, shape=256, **kwargs):
		"""Models the PSF given the desired model function and kwargs.

		Args:
			model (str):
				Must be either 'airydisk' or 'gaussian'.
			radius (int, float, astropy.unit.Quantity):
				Radius of the PSF model that is the radius of the first zero in an AiryDisk model or the standard
				deviation of the Gaussian model. Scalar values will be interpreted in units of arcseconds.
			psf_resolution (int, float, astropy.unit.Quantity):
				Resolution of the model PSF, equivalent to the pixel scale of the array. Scalar values will be
				interpreted in units of arcseconds.
			shape (int, optional):
				Size of the model PSF along both axes.
			kwargs are forwarded to the model function.
		"""

		# Check input parameters
		if not isinstance(model, str):
			raise SpecklepyTypeError('model_psf', 'model', type(model), 'str')

		if isinstance(radius, Quantity):
			self.radius = radius
		elif isinstance(radius, (int, float)):
			logger.warning(f"Interpreting scalar type radius as {radius} arcsec")
			self.radius = Quantity(f"{radius} arcsec")
		elif isinstance(radius, str):
			self.radius = Quantity(radius)
		else:
			raise SpecklepyTypeError('model_psf', 'radius', type(radius), 'Quantity')

		if isinstance(psf_resolution, Quantity):
			self.psf_resolution = psf_resolution
		elif isinstance(psf_resolution, (int, float)):
			logger.warning(f"Interpreting scalar type psf_resolution as {psf_resolution} arcsec")
			self.psf_resolution = Quantity(f"{psf_resolution} arcsec")
		elif isinstance(psf_resolution, str):
			self.psf_resolution = Quantity(psf_resolution)
		else:
			raise SpecklepyTypeError('model_psf', 'psf_resolution', type(psf_resolution), 'Quantity')

		if isinstance(shape, int):
			center = (shape / 2, shape / 2)
			shape = (shape, shape)
		elif isinstance(shape, tuple):
			center = (shape[0] / 2, shape[1] / 2)
		else:
			raise SpecklepyTypeError('model_psf', 'shape', type(shape), 'int or tuple')

		if model.lower() == 'airydisk':
			model = models.AiryDisk2D(x_0=center[0], y_0=center[1], radius=float(self.radius / self.psf_resolution))
		elif model.lower() == 'gaussian':
			stddev = float(self.radius / self.psf_resolution)
			model = models.Gaussian2D(x_mean=center[0], y_mean=center[1], x_stddev=stddev, y_stddev=stddev)
		else:
			raise SpecklepyValueError('model_psf', 'model', model, 'either AiryDisk or Gaussian')

		y, x = np.mgrid[0:shape[0], 0:shape[1]]
		self.psf = model(x, y)
		self.psf = self.normalize(self.psf)

	def read_psf_file(self, filename, hdu=None):
		"""Read PSF information from a FITS file.

		Args:
			filename (str):
				Name of the FITS file containing the PSF frames.
			hdu (int, str, optional):
				Specification of the HDU to read from the file. Default is the first HD
		"""

		# Set
		psf = None
		time_resolution = None
		angular_resolution = None

		# Extract header
		header = fits.get_header(filename, extension=hdu)

		if header['NAXIS'] == 2:
			psf = self.normalize(fits.get_data(filename, extension=hdu))
		else:
			# Iterate over list of known header cards containing the time resolution
			for key in self.TIME_STEP_KEYS:
				try:
					time_resolution = self.get_header_quantity(header, key)
					break
				except KeyError:
					continue

			# Check whether time resolution was found in the header
			if time_resolution is None:
				raise RuntimeError("Function read_psf_file could failed extracting the PSF time resolution.")

		# Iterate over list of known header cards containing the angular resolution
		for key in self.RESOLUTION_KEYS:
			try:
				angular_resolution = self.get_header_quantity(header, key)
				break
			except KeyError:
				continue

		# Check whether angular resolution was found in the header
		if angular_resolution is None:
			raise RuntimeError("Function read_psf_file could failed extracting the PSF angular resolution.")

		return psf, time_resolution, angular_resolution

	@staticmethod
	def get_header_quantity(header, key, aliases=None):
		"""Extract unit quantities from FITS headers, based on the unit in the comment.

		Args:
			header:
				Header of the FITS file.
			key (str):
				Key or name of the FITS header card.
			aliases (dict, optional):
				The aliases dictionary maps unit strings such that they are understood by Unit(str).

		Returns:
			quantity (Quantity):
				Quantity derived from the combination of the FITS header card value and comment.
		"""

		# Read header card
		value = header[key]
		comment = header.comments[key]

		# Handle empty comment
		if not comment:
			logger.warning("Function 'get_value()' received an empty comment string and returns scalar value.")
			return value

		# Apply fall back values
		if aliases is None:
			aliases = {'sec': 's', 'milliarcsec': 'mas', 'microns': 'micron'}

		# Replace comment entries that are not understood by astropy.units by corresponding aliases
		if comment in aliases.keys():
			comment = aliases[comment]

		return value * Unit(comment)

	def get_photon_rate(self, photon_rate_density, photon_rate_density_resolution=None, integration_time=None,
						debug=False):
		"""Propagates the 'photon_rate_density' array through the telescope.

		The photon_rate_density is multiplied by the telescope collecting area and then
		convolved with the PSF. If the resolution of the flux array is different
		from the telescopes psf_resolution, then one is resampled. If the PSF is
		non-static, it will be integrated over the 'integration_time' value.

		Args:
			photon_rate_density (np.ndarray, dtype=Quantity):
			photon_rate_density_resolution (Quantity, optional):
			integration_time(Quantity, optional):
				Required only if the PSF is non-static.
			debug (bool, optional):
				Show additional information for debugging. Default is False.

		Returns:
			photon_rate (Quantity): PSF-convolved photon rate array.
		"""

		# Input parameters
		if not isinstance(photon_rate_density, Quantity):
			raise SpecklepyTypeError('get_photon_rate', 'photon_rate_density', type(photon_rate_density), 'Quantity')

		if photon_rate_density_resolution is not None:
			if not isinstance(photon_rate_density_resolution, Quantity):
				raise SpecklepyTypeError('get_photon_rate', 'photon_rate_density_resolution',
										 type(photon_rate_density_resolution), 'Quantity')
			psf_resample_mode = True
		else:
			psf_resample_mode = False

		if integration_time is None and hasattr(self, 'timestep'):
			raise ValueError("If the PSF source of Telescope is non-static, the call function requires the "
							 "integration_time.")
		elif isinstance(integration_time, (int, float)):
			logger.warning(f"Interpreting scalar type integration_time as {integration_time} s")
			integration_time = Quantity(f"{integration_time} s")
		elif not isinstance(integration_time, Quantity):
			raise SpecklepyTypeError('get_photon_rate', 'integration_time', type(integration_time), 'Quantity')

		# Apply telescope collecting area
		photon_rate = photon_rate_density * self.area
		total_flux = np.sum(photon_rate)
		photon_rate_unit = photon_rate.unit

		# Prepare PSF if non-static
		if hasattr(self, 'timestep'):
			self.integrate_psf(integration_time=integration_time)

		# Resample photon_rate_density to psf resolution
		if psf_resample_mode:

			# Compute the zoom factor
			ratio = float(photon_rate_density_resolution / self.psf_resolution)
			# try:
			# 	ratio = float(photon_rate_density_resolution / self.psf_resolution)
			# except UnitConversionError as e:
			# 	raise UnitConversionError(f"The resolution values of the image ({photon_rate_density_resolution}) and "
			# 							  f"PSF ({self.psf_resolution}) have different units!")

			# Zoom either the image or the PSF, depending on the zoom factor
			with warnings.catch_warnings():
				warnings.simplefilter('ignore')
				if ratio < 1.0:
					self.psf = zoom(self.psf, 1/ratio, order=1) / ratio**2
					self.psf = self.normalize(self.psf)
				else:
					memory_sum = np.sum(photon_rate)
					photon_rate = zoom(photon_rate, ratio, order=1) / ratio**2
					photon_rate = photon_rate / np.sum(photon_rate) * memory_sum

		# Convolve the array with the PSF
		convolved = fftconvolve(photon_rate, self.psf, mode='same') * photon_rate_unit

		# Report on flux conservation
		logger.debug(f"Flux conservation during convolution: {total_flux} > {np.sum(convolved)}")

		return convolved.decompose()

	@staticmethod
	def normalize(array, mode='sum_circular'):
		"""Normalizes the input array depending on the mode.

		Args:
			array (np.ndarray):
				Array to be normalized.
			mode (str, optional):
				Can be either 'sum' for having a sum of 1, 'max' for having a peak value 1, or 'sum_circular' for
				subtracting a constant and then normalizing to a sum of 1. Default is 'sum_circular'.

		Returns:
			normalized (np.ndarray):
				Normalized array, according to mode.
		"""

		if not isinstance(array, np.ndarray):
			raise SpecklepyTypeError('normalize', 'array', type(array), 'np.ndarray')
		if np.sum(array) == 0:
			raise ValueError("Normalize received an array of zeros!")

		if mode not in ['sum', 'max', 'peak', 'sum_circular']:
			raise SpecklepyValueError('normalize', 'mode', mode, "'sum', 'max', or 'sum_circular'")

		if mode == 'sum':
			normalized = array / np.sum(array)
		elif mode == 'max':
			normalized = array / np.max(array)
		elif mode == 'sum_circular':
			x, y = array.shape
			low_cut = array[0, int(y/2)]
			array = np.maximum(array - low_cut, 0)
			normalized = array / np.sum(array)
		else:
			normalized = None

		return normalized

	def integrate_psf(self, integration_time, hdu=None):
		"""Integrates psf frames over the input time.

		Args:
			integration_time (Quantity):
				This is used for computing the number of frames 'n_frames', via floor division by the 'timestep'
				attribute.
			hdu (int, str, optional):
				Specifier of the HD Default is None for the first HD
		"""

		# Check input parameters
		if isinstance(integration_time, (int, float)):
			logger.warning(f"Interpreting scalar type integration_time as {integration_time} s")
			integration_time = integration_time * Unit('s')
		elif not isinstance(integration_time, Quantity):
			raise SpecklepyTypeError('integrate_psf', 'integration_time', type(integration_time), 'Quantity')

		if integration_time < self.timestep:
			raise ValueError(f"integrate_psf received integration time {integration_time} shorter than the time "
							 f"resolution of the psf source ({self.timestep})!")

		n_frames = int(integration_time / self.timestep)

		# Read PSF frames from source file
		data = fits.get_data(self.psf_source, extension=hdu)

		self.psf_frame += 1
		if self.psf_frame + n_frames < data.shape[0]:
			self.psf = np.sum(data[self.psf_frame : self.psf_frame+n_frames], axis=0)
		else:
			self.psf = np.sum(data[self.psf_frame : ], axis=0)
			self.psf += np.sum(data[ : (self.psf_frame+n_frames) % data.shape[0]], axis=0)
		self.psf_frame += n_frames - 1
		self.psf_frame = self.psf_frame % data.shape[0]

		# Normalize the integrated PSF
		self.psf = self.normalize(self.psf)


import os
import requests
import json

dir_path = os.path.dirname(os.path.realpath(__file__))

data = []
for i in range(11):
    response = requests.get('https://api.chucknorris.io/jokes/random')
    if response.status_code == 200:
        data.append(response.json())

with open (dir_path + '/jokes.json', mode='a') as file:
    json.dump(data,file)

with open(dir_path + '/jokes.json', 'r') as file:
    json_data = json.load(file)

print(json_data[2]['value'])


import string
from typing import List, Optional

import numpy as np
import scipy
from braket.circuits import Gate
from braket.circuits.gates import (
    CY,
    CZ,
    XX,
    XY,
    YY,
    ZZ,
    CCNot,
    CNot,
    CPhaseShift,
    H,
    I,
    ISwap,
    PhaseShift,
    PSwap,
    Rx,
    Ry,
    Rz,
    S,
    Si,
    Swap,
    T,
    Ti,
    V,
    Vi,
    X,
    Y,
    Z,
)

braket_gates = {
    # one-qubit, zero parameter
    "H": H,
    "X": X,
    "Y": Y,
    "Z": Z,
    "S": S,
    "Sdg": Si,
    "T": T,
    "Tdg": Ti,
    "I": I,
    "SX": V,
    "SXdg": Vi,
    # one-qubit, one parameter
    "Phase": PhaseShift,
    "RX": Rx,
    "RY": Ry,
    "RZ": Rz,
    "U1": PhaseShift,
    # two-qubit, zero parameter
    # 'CH':BraketGate.,
    "CX": CNot,
    "Swap": Swap,
    "iSwap": ISwap,
    # 'CSX':BraketGate.,
    # 'DCX': BraketGate.,
    "CY": CY,
    "CZ": CZ,
    # two-qubit, one parameter
    "RXX": XX,
    "RXY": XY,
    "RYY": YY,
    "RZZ": ZZ,
    "pSwap": PSwap,
    "CPhase": CPhaseShift,
    # multi-qubit
    "CCX": CCNot,
    # unitary
    # "Unitary": Unitary,
}

def generate_params(varnames: List[str], seed: Optional[int] = None):
    """Returns a dictionary of random parameters for a given list of variable names"""
    if seed is not None:
        np.random.seed(seed)
    params = {}
    for v in varnames:
        if v.startswith("angle"):
            params[v] = np.random.rand() * 2 * np.pi
    return params

def get_braket_gates(seed: Optional[int] = None):
    """Returns a dictionary of all braket gates with random parameters"""
    braket_gates = {attr: None for attr in dir(Gate) if attr[0] in string.ascii_uppercase}
    for gate in ["C", "PulseGate"]:
        braket_gates.pop(gate, None)

    for gate in braket_gates:
        if gate == "Unitary":
            n = np.random.randint(1, 4)
            unitary = scipy.stats.unitary_group.rvs(2**n)
            braket_gates[gate] = getattr(Gate, gate)(matrix=unitary)
        else:
            varnames = [v for v in getattr(Gate, gate).__init__.__code__.co_varnames if v != "self"]
            params = generate_params(varnames, seed=seed)
            braket_gates[gate] = getattr(Gate, gate)(**params)

    return {k: v for k, v in braket_gates.items() if v is not None}