import pickle
import shutil, psutil
import time
from os import execl
from sys import executable
from pyrogram import (
    Client,
    filters
)
from pyrogram.types import Message
from bot import (
    botStartTime,
    AUTHORIZED_CHATS,
    OWNER_ID
)
import bot.helper.ext_utils.fs_utils
from bot.helper.ext_utils import fs_utils
from bot.helper.telegram_helper.bot_commands import BotCommands
from bot.helper.telegram_helper.message_utils import *
from bot.helper.ext_utils.bot_utils import get_readable_file_size, get_readable_time

@Client.on_message(
    filters.command(BotCommands.StatsCommand) &
    filters.chat(AUTHORIZED_CHATS)
)
def stats(client: Client, message: Message):
    currentTime = get_readable_time((time.time() - botStartTime))
    total, used, free = shutil.disk_usage('.')
    total = get_readable_file_size(total)
    used = get_readable_file_size(used)
    free = get_readable_file_size(free)
    cpuUsage = psutil.cpu_percent(interval=0.5)
    memory = psutil.virtual_memory().percent
    netio = psutil.net_io_counters()
    sent = netio[0]
    recieved = netio[1]
    stats = f'Bot Uptime: {currentTime}\n' \
            f'Total Disk space: {total}\n' \
            f'Used: {used}\n' \
            f'Free: {free}\n' \
            f'CPU: {cpuUsage}%\n' \
            f'RAM: {memory}%\n' \
            f"Sent: {get_readable_file_size(sent)}\n" \
            f"Recieved: {get_readable_file_size(recieved)}"
    sendMessage(stats, client, message)

@Client.on_message(
    filters.command(BotCommands.StartCommand) &
    filters.chat(AUTHORIZED_CHATS)
)
def start(client: Client, message: Message):
    start_string = start_string = f'''
Ya! I'm Alive Send A Magnet/.torrent/Https Link
'''
    sendMessage(start_string, client, message)

@Client.on_message(
    filters.command(BotCommands.RestartCommand) &
    filters.user(OWNER_ID)
)
def restart(client: Client, message: Message):
    restart_message = sendMessage(
        "Restarting, Please wait!",
        client,
        message
    )
    # Save restart message object in order to reply to it after restarting
    fs_utils.clean_all()
    with open('restart.pickle', 'wb') as status:
        pickle.dump(restart_message, status)
    execl(executable, executable, "-m", "bot")

@Client.on_message(
    filters.command(BotCommands.PingCommand) &
    filters.chat(AUTHORIZED_CHATS)
)
def ping(client: Client, message: Message):
    start_time = int(round(time.time() * 1000))
    reply = sendMessage("Starting Ping", client, message)
    end_time = int(round(time.time() * 1000))
    editMessage(f'{end_time - start_time} ms', reply)

@Client.on_message(
    filters.command(BotCommands.LogCommand) &
    filters.user(OWNER_ID)
)
def log(client: Client, message: Message):
    sendLogFile(client, message)


from typing import List, Dict, Optional
from collections import defaultdict

from SPARQLWrapper import SPARQLWrapper, JSON

from noisemon.tools.cache_to_redis import get_cacher
from noisemon.tools.retry_request import retry_request
from noisemon.settings import settings
from noisemon.logger import logger

redis_params = {
    "host": settings.REDIS_HOST,
    "port": settings.REDIS_PORT,
    "db": settings.REDIS_DB,
    "client_name": "wikidata_cache",
}
redis_cacher = get_cacher(redis_params, EXPIRE=60 * 60 * 24 * 7)
decorator = redis_cacher(key_argument_position=1)

class Wikidata:
    def __init__(self):
        self.sparql = SPARQLWrapper("http://query.wikidata.org/sparql")
        self.sparql.setReturnFormat(JSON)

    # @retry_request
    # @redis_cacher(key_argument_position=1)
    def lookup_companies_by_ticker(self, ticker: str) -> List:
        """
        Given string ticker returns a list of junk
        """
        raise Exception("Obsolete")

    # @redis_cacher(key_argument_position=1)
    @retry_request
    @decorator
    def lookup_entity_label_by_qid(self, qid) -> Optional[str]:
        """
        Given QID, lookup for name in russian (fallback to english)
        """
        logger.debug(f"Performing wikidata entities lookup by QID: {qid}")
        if "http" in qid:
            qid = qid.split("/")[-1]
        query = """
            SELECT DISTINCT ?label WHERE {
                wd:%s rdfs:label ?label FILTER (lang(?label)="ru" || lang(?label)="en").
            }
            LIMIT 1""" % (qid)

        self.sparql.setQuery(query)
        results = self.sparql.query().convert()

        if not results["results"]["bindings"]:
            return None
        return results["results"]["bindings"][0]["label"]["value"]

    # @redis_cacher
    @decorator
    @retry_request
    def lookup_aliases_by_ticker(self, ticker: str) -> Dict[str, List[str]]:
        """
        Given ticker, lookup for aliases and return em with respect to
        entity QID, e.g. Dict[QID, List of aliases (unique)]
        """
        logger.debug(f"Performing wikidata entity aliases lookup given ticker: {ticker}")
        query = """
        SELECT DISTINCT ?id ?idLabel ?alias WHERE {
            SERVICE wikibase:label { bd:serviceParam wikibase:language "ru,en". }
            VALUES ?ticker { "%s"}

            ?id p:P414 ?exchange.
            ?exchange pq:P249 ?ticker.
            ?id skos:altLabel ?alias .
        }
        LIMIT 150""" % (ticker,)

        res = defaultdict(set)

        self.sparql.setQuery(query)
        results = self.sparql.query().convert()

        for x in results["results"]["bindings"]:
            res[x["id"]["value"]].add(x["alias"]["value"])
        for x in results["results"]["bindings"]:
            res[x["id"]["value"]].add(x["idLabel"]["value"])

        res = {key: list(value) for key, value in res.items()}
        return res


import numpy as np
from PIL import Image
from k_means_clustering import k_means

#Given an image, return the pixel RGB data as a 1D vector
def read_image_pixels(filename):
    im = Image.open(filename)
    pixels = im.load()
    RGB_vectors = []
    for i in range(im.size[0]):
        for j in range(im.size[1]):
            RGB_vectors.append(pixels[i,j])
    return np.asarray(RGB_vectors)

#Take an image, segment it, and output the segmented image
def write_clustered_image(infile,outfile, k):
    print("Importing Data...")
    data = read_image_pixels(infile)

    print("Clustering...")
    means, indicators = k_means(data,k,True)

    print("Segmenting...")
    segment(data,means,indicators)

    size = Image.open(infile).size
    img_array = reshape(data,size)

    out = Image.fromarray(img_array.astype('uint8'))
    out.save(outfile)

#Replace pixels in data with the means to which they are assigned
def segment(data,means,indicators):
    for n in range(len(data)):
        tmp = means[np.argmax(indicators[n])]
        data[n] = np.asarray([int(x) for x in tmp])

#Convert 1D vector to a 2D matrix that can be written to an image
def reshape(data, size):
    img_array = np.zeros((size[1],size[0],len(data[0])))
    for i in range(size[1]):
        for j in range(size[0]):
            img_array[i][j] = data[(i + j*size[1])]
    return img_array

#MAIN
if __name__ == '__main__':
    k=4
    #write_clustered_image("Images/ferrari.jpg","Images/ferrari_segmented_" + str(k)+ ".jpg",k)
    #write_clustered_image("Images/bugatti.jpg","Images/bugatti_segmented_" + str(k)+ ".jpg",k)
    #write_clustered_image("Images/tree.jpg","Images/tree_segmented_" + str(k)+ ".jpg",k)
    #write_clustered_image("Images/penguin.jpg","Images/penguin_segmented_" + str(k)+".jpg",k)
    #write_clustered_image("Images/droplets.jpg","Images/droplets_segmented_" + str(k)+".jpg",k)
    for k in range(2,8):
        write_clustered_image("Images/highland_park_flowers.jpeg","Images/highland_park_flowers_segmented_" + str(k) +".jpeg",k)


from time import sleep
from web3 import Web3
import tomllib
import json

with open("config.toml", "rb") as config_file:
    config = tomllib.load(config_file)
with open("abi.json", "r") as config_file:
    abi = json.load(config_file)

w3 = Web3(Web3.HTTPProvider(config["PRIVATE"]["WSS_APIKEY"]))
contract = w3.eth.contract(
    address=config["PUBLIC"]["marimo_address"], abi=abi)

def check_marimo():
    block = w3.eth.get_block(block_identifier="latest")
    base_fee_per_gas = getattr(block, "baseFeePerGas")
    gas_in_gwei = Web3.from_wei(base_fee_per_gas, "gwei")
    # check for gas price
    print(
        f"bh: {getattr(block, 'number')}")
    if gas_in_gwei > config["PUBLIC"]["gas_price"]:
        print(f"Gas too expensive. {gas_in_gwei} GWEI")
    else:
        # get all marimbo from PUBLIC_KEY acc and iterate over it
        marimos_owned = contract.functions.tokensOfOwner(
            config["PRIVATE"]["PUBLIC_KEY"]).call()
        for marimo_id in marimos_owned:
            marimo_life = contract.functions.getElapsedTimeFromLastWaterChanged(
                marimo_id).call()
            life_threshold = 60 * 60 * 24 * \
                (100 - config["PUBLIC"]["water_clarity"])
            # check for marimos life
            if marimo_life < life_threshold:
                print("Marimo life is good.")
            else:
                print(
                    f"Marimo life is {marimo_life / 60 / 60 / 24} days")
                nonce = w3.eth.get_transaction_count(
                    config["PRIVATE"]["PUBLIC_KEY"])
                marimo_txn = contract.functions.changeWater(marimo_id).build_transaction({
                    'chainId': 1,
                    'gas': 300000,
                    'maxFeePerGas': base_fee_per_gas + Web3.to_wei(1, "gwei"),
                    'maxPriorityFeePerGas': w3.to_wei('1', 'gwei'),
                    "nonce": nonce
                })
                signed_txn = w3.eth.account.sign_transaction(
                    marimo_txn,
                    private_key=config["PRIVATE"]["PRIVATE_KEY"]
                )
                w3.eth.send_raw_transaction(signed_txn.rawTransaction)
                print(signed_txn.hash)
                receipt = w3.eth.wait_for_transaction_receipt(signed_txn.hash)
                print(receipt)

if __name__ == '__main__':
    while True:
        check_marimo()
        sleep(5)


from __future__ import unicode_literals

import time

from django.core.management.base import BaseCommand

from search.datasources.bookshops.frFR.dilicom import dilicomScraper
from search.models import Card

# py2/3
try:
    input = raw_input
except NameError:
    pass

class Command(BaseCommand):

    def add_arguments(self, parser):
        # parser.add_argument(
        #     '-V',
        #     dest='verbose',
        #     help='debug output.',
        # )
        pass

    def update_all(self):
        # All cards with an ISBN.
        cards = Card.objects.filter(isbn__isnull=False)
        self.stdout.write("Updating {} cards.".format(cards.count()))

        confirmation = True
        # confirmation = raw_input("Continue ? [Y/n]")
        if confirmation == "n":
            exit(0)

        isbns = cards.values_list("isbn", flat=True)  # [it.isbn for it in cards]

        # isbns = isbns[:100]  # DEBUG:
        # isbns = isbns[50:500]  # TODO:
        # isbns = isbns[430:500]  # TODO:
        dilicom_query = dilicomScraper.Scraper(*isbns)
        self.stdout.write("Searching all on Dilicom...")
        bklist, errors = dilicom_query.search()

        if len(bklist) != len(isbns):
            self.stdout.write("--- beware: the search results have not the same length that our query: {} vs {}".format(len(bklist), len(isbns)))
            # exit(1)

        cards_updated = []
        cards_not_updated = []
        count_ok = 0
        for i, card in enumerate(cards):
            bk = list(filter(lambda it: it['isbn'] == card.isbn, bklist))
            if bk:
                bk = bk[0]

            if not bk:
                # self.stdout.write("No matching result for card {}. Pass.".format(card.isbn))
                cards_not_updated.append(card)
                continue

            # self.stdout.write("{}: => {}".format(card.title, bk))

            # THE update.
            # same publisher?
            # print("--publishers: {} VS {}".format(bk.get('publishers'), " + ".join([it.name for it in card.publishers.all()])))

            pub_name = ""
            if bk.get('publishers'):
                pub_name = bk.get('publishers')[0]
            if not pub_name:
                print("Could not find the name of distributor for {}. Exiting.".format(bk.get('publishers')[0]))
                exit(1)

            print("* {} - updating {}".format(i, card.isbn))

            try:
                gln = bk.get('distributor_gln')
                if not gln:
                    print("Could not get distributor GLN from {}. Exiting.".format(bk))
                    exit(1)
                Card.update_from_dict(card, bk,
                                      distributor_gln=gln,
                                      publisher_name=pub_name)

                cards_updated.append(card)
                count_ok += 1
            except Exception as e:
                self.stdout.write(" ! failed for {}: {}".format(bk.get('isbn'), e))

            # Updating 4.000 cards is resource heavy.
            if i % 100 == 0:
                time.sleep(0.2)

        self.stdout.write("-------------------")
        self.stdout.write("Cards OK: {}.".format(count_ok))
        self.stdout.write("Cards updated:{}".format(len(cards_updated)))
        self.stdout.write("Cards not updated:{}".format(len(cards_not_updated)))
        # self.stdout.write("\n".join([it.isbn for it in cards_not_updated]))
        self.stdout.write("Done.")

    def handle(self, *args, **options):
        self.stdout.write("Go...")
        try:
            self.update_all()
        except KeyboardInterrupt:
            self.stdout.write("User abort.")


import sys, time, requests
from datetime import datetime
from todoist_api_python.api import TodoistAPI

TODOIST_ACCESS_TOKEN = '' #At the bottom of Settings > Integrations in the web/desktop app

if len(sys.argv) != 2:
	print("Usage: " + sys.argv[0] + " label")
	sys.exit(0)

def reschedule_label(label):
	items = api.get_tasks(label=label)
	for item in items:
		date = item.due.datetime
		if date is None:
			date = item.due.date
		if datetime.fromisoformat(date) < datetime.now():
			print("Overdue (" + label + "): " + date + " - " + item.content)
			try:
				api.update_task(task_id=item.id, due_string=item.due.string)
			except requests.exceptions.HTTPError as err:
				time.sleep(5)
				api.update_task(task_id=item.id, due_string=item.due.string)

api = TodoistAPI(TODOIST_ACCESS_TOKEN)

reschedule_label(sys.argv[1])


# config: utf8

import importlib
import os
import sys

from ... import config
from .module_scanner import ModuleScanner

class DirScanner(object):
    """
    扫描指定的目录，得到所有的服务元数据, 该目录一般是对应项目的根目录
    """

    def __init__(self, target_dir):
        self.cur_dir = os.path.abspath(os.curdir)
        self.target_dir = target_dir
        sys.path.append(target_dir)
        os.chdir(target_dir)

    def gather(self):
        """
        获取目标目录下的复合条件的所有类型
        :return:
        """
        target_dir = self.target_dir
        if config.outside_server:
            # 如果是生成独立项目，则需要在扫描时进入该子目录，扫描完成后退出来
            target_dir = os.path.join(config.outside_server_path, config.source_project_name)
        self.scan_file(target_dir, "")
        if config.outside_server:
            os.chdir(self.target_dir)
        return ModuleScanner().gather()

    def scan_file(self, target_dir, rel_path):
        """
        :param target_dir:
        :param rel_path
        :return:
        """

        # if os.path.exists(os.path.join(target_dir, "__init__.py")):
        #     self.import_file(rel_path, "")

        for entry in os.scandir(target_dir):
            if entry.is_file() and entry.name.endswith(".py"):
                self.import_file(rel_path, entry.name[:-3])
            elif entry.is_dir():
                # only python package dir
                next_dir = os.path.join(target_dir, entry.name)
                if not os.path.exists(os.path.join(next_dir, "__init__.py")):
                    continue

                # import the package first
                self.import_file(rel_path, entry.name)
                # then get into deeper
                self.scan_file(
                    next_dir,
                    ".".join(rel_path and [rel_path, entry.name] or [entry.name]))

    @staticmethod
    def import_file(target_dir: str, target_file: str):
        path_elem = target_dir and [target_dir, target_file] or [target_file]
        path = ".".join(path_elem)
        try:
            importlib.import_module(path)
        except Exception as e:
            if "_tkinter" not in str(e):
                print(("导入文件 %s 出错，错误信息为: %s" % (path, str(e))))

    def __del__(self):
        # sys.path.remove(self.cur_dir)
        # os.chdir(self.cur_dir)
        pass


from NeuralNet import NeuralNet, kerasmodel
import numpy as np
import tensorflow as tf

model = tf.keras.models.load_model('finalmodel.keras')
X_Test = np.linspace(0, 1.0, 100)
Y_Test = np.sin(X_Test*2*np.pi)
Y_Pred = model.predict(X_Test)
print(Y_Pred)
'''
minscore = 1e300
minparams = None
with open('dumpfile.dat', "r") as f:
    for line in f:
        col = line.split('|')
        parameters = np.array([float(x) for x in col[0].split()])
        score = float(col[1])
        if score < minscore:
            minscore = score
            minparams = parameters
            print(minscore)
print(minparams)
weights = model.get_weights()
nweights = 0
for i, layer in enumerate(weights):
    for j, x in np.ndenumerate(layer):
        weights[i][j] = minparams[nweights]
        nweights += 1
model.set_weights(weights)

'''

#A function which plots the data set and the function y = sin(x) using matplotlib.
def plot(X, Y, Y_Pred):
    import matplotlib.pyplot as plt
    plt.plot(X, Y, 'o')
    plt.plot(X, Y_Pred)
    plt.show()
#    plt.savefig('fig.png')

plot(X_Test, Y_Test, Y_Pred)


from pytesseract import pytesseract
from googletrans import Translator

t = Translator()
path =  r"C:\Program Files\Tesseract-OCR\tesseract.exe"
image = r'img_name.png'

pytesseract.tesseract_cmd = path_to_tesseract

img = Image.open(image)
text = pytesseract.image_to_string(img)
after_translate = t.translate(text[:-1], dest="he")

print(after_translate.text[::-1]) ### you dont have to add the [::-1] i added it because vs prints hebrew reversed


import re
import traceback

import requests
from bs4 import BeautifulSoup

HOME_URL = "http://ntucoder.net/Message"

comments = set()

url = HOME_URL

page = 1
while True:
    try:
        r = requests.get(url)
        soup = BeautifulSoup(r.content, 'lxml')
        for comment in soup.select('div.message-info > div'):
            txt = re.sub('^[^:]+:', '', comment.text.strip()).strip()
            if txt != '':
                comments.add(txt)
        url = "http://ntucoder.net{}".format(soup.select_one('#view-previous-con > a')['href'])
        page+= 1
    except:
        traceback.print_exc()
        break

print("Total pages = {}".format(page))

with open('ntucoder.txt', 'w', encoding='utf8') as fp:
    for x in comments:
        fp.write(x + '\n\n')


import matplotlib as mpl
mpl.use('TkAgg')
import numpy as np
import seaborn as sns
from matplotlib import pyplot as plt
from matplotlib.animation import FuncAnimation
from matplotlib import rc
rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']})
rc('text', usetex=True)

def gaussian(x, time, conductivity):
    x = np.multiply(conductivity, np.square(x))
    exp = np.negative(np.divide(x, time))
    num = np.power(np.e, exp)
    den = np.sqrt(time)
    return np.divide(num, den)

def animate(time, x, conductivity):
    # animation function passes 0 at the very beginning
    if time < 0.02:
        time = 0.0001
    y = gaussian(x, time, conductivity)

    graph.set_ydata(y)
    plt.title(r'$Time$ $=$ ${0}$ $(s)$'.format(np.round(time, decimals=1)), fontsize=20)

    return graph, ax

def main():
    plt.style.use('ggplot')

    # Thermal conductivity of air
    conductivity = 0.024

    resolution = 250
    max_plot_boundary = 100
    x = np.linspace(-max_plot_boundary, max_plot_boundary, num=resolution)

    # matplotlib requires these objects to be global:
    # https://matplotlib.org/api/animation_api.html#funcanimation
    global fig, ax, graph
    fig, ax = plt.subplots()

    # initial distribution
    y = gaussian(x, time=0.1, conductivity=conductivity)

    ax.set_xlabel(r'$Direction$ $(cm)$', fontsize=20)
    ax.set_ylabel(r'$Temperature$ $(^\circ C)$', fontsize=20)
    graph, = ax.plot(x, y)

    # 60 frames per second
    anim = FuncAnimation(fig, animate, fargs=(x, conductivity,),
            frames=np.arange(0, 100, 0.02), interval=20)

    plt.ylim(0, 3)
    plt.show()

if __name__ == '__main__':
    main()


import pandas as pd
import json
import numpy as np
from time import time
from tkinter import Tk, messagebox
from tkinter.filedialog import askopenfilename
from string import ascii_uppercase as upstr
import file_split as fs
from import_csv import FileFinder
import data_formatter as formatter
import enspire_od_join as enspire_od_join
import pt_calculations as pt_calculations
from import_od import import_od

# TODO just create separate inputs module

def concat_projs(df):
    all_projs_df = pd.DataFrame()
    pass

def run_main(proj_dict):
    proj_concat = False
    file_finder = FileFinder()

    proj_dict = fs.split_projects(proj_dict)

    for proj, inner_dict in proj_dict.items():
        project_title = proj
        if "-" in proj:
            inner_dict["proj_name"] = proj.split("-")[0]
            inner_dict["ab_name"] = proj.split("-")[-1]
        else:
            inner_dict["ab_name"] = ""
            inner_dict["proj_name"] = proj

        proj_data = proj_dict[proj]

        source_df = file_finder.data_finder(proj_data)

        df_list = formatter.data_format(source_df, proj_data)

        if proj_data["od_file"]:
            raw_od = import_od(proj_data)
            joined_df_list = enspire_od_join.join_dfs(df_list, raw_od, proj_data)
            main_join_dfs = joined_df_list[:2]
            final_display_df = joined_df_list[2]
            final_display_rep_df = joined_df_list[3]
            completed_main_dfs = pt_calculations.make_calculations(main_join_dfs, proj_data)
        else:
            main_dfs = df_list[:2]
            final_display_df = df_list[2]
            final_display_rep_df = df_list[3]
            completed_main_dfs = pt_calculations.make_calculations(main_dfs, proj_data)

        final_main_df = completed_main_dfs[0]
        final_main_rep_df = completed_main_dfs[1]

        with pd.ExcelWriter(f"../test_files/test_outputs/{project_title}_output_test.xlsx") as writer:
            final_main_df.to_excel(writer, sheet_name="Calculations")
            final_display_df.to_excel(writer, sheet_name="Display_Ready")
            final_main_rep_df.to_excel(writer, sheet_name="Rep_Calculations")
            final_display_rep_df.to_excel(writer, sheet_name="Rep_Display_Ready")
        print(f"Project {project_title} has been output.")

        # Also return these four dataframes into list?
        # clean_df, main_df, clean_rep_df, main_rep_df = test_formatter.data_format(source_df, proj_data)
        # df_list = [clean_df, main_df, clean_rep_df, main_rep_df]
        # dfs_return = join_dfs(df_list, raw_od, proj_data)
        #
        # clean_join_df = dfs_return[0]
        # main_join_df = dfs_return[1]
        # clean_rep_join_df = dfs_return[2]
        # main_rep_join_df = dfs_return[3]
        #
        # complete_df = eight_pt_calculations.make_calculations(main_join_df, proj_data)
        # complete_rep_df = eight_pt_calculations.make_calculations(main_rep_join_df, proj_data)
        #

if __name__ == "__main__":
    start_time = time()
    window = Tk()
    window.withdraw()
    try:
        with open(r"C:\Users\esimonds\parser_data.json", "r") as parser_file:
            proj_data_dict = json.load(parser_file)
    except FileNotFoundError:
        messagebox.showinfo(title="Warning!", message="There's no file, did you complete the parser form?")
    # proj_names = [
    #     "SSF00626",
    # ]
    # proj_data_dict = {
    #     proj: {"plates": "",
    #            "raw_file": "",
    #            "od_file": r"L:\Molecular Sciences\Small Scale Runs\SSF00626 DSS Spaniel_discrete chaperone set\SSF00626 Spaniel (96DW) ELN v2.xlsm",
    #            "std_row": "",
    #            "std_pos": "",
    #            "std_conc": "",
    #            "volumes": vol_sp,
    #            "points": 4
    #            }
    #     for proj in proj_names
    # }
    # for proj, inner in proj_data_dict.items():
    #     add_std = input("Use standard? y/n    ")
    #     if add_std == 'y':
    #         more_ids = True
    #         std_conc = input("Enter standard concentration, e.g. '100, 50, 25, 12, 6, 3': ").split(",")
    #         std_conc_len = len(std_conc)
    #
    #         std_ids = []
    #         count = 1
    #         while more_ids == True:
    #             z = input("Enter 'column', 'row', 'none: ").lower()
    #             y = input("Enter staring well id, e.g. 'A11' or 'G1'").capitalize()
    #
    #             if z == "column":
    #                 col_letter = y[:1]
    #                 letter_idx = upstr.index(col_letter)
    #                 col_num = y[1:]
    #                 std_ids += [f"{upstr[letter]}{str(col_num).zfill(2)}" for letter in range(letter_idx, std_conc_len)]
    #
    #             elif z == 'row':
    #                 row_letter = y[:1]
    #                 row_num = int(y[1:])
    #                 std_ids += [f"{row_letter}{str(num).zfill(2)}" for num in range(row_num, row_num + std_conc_len)]
    #
    #             elif z == 'none':
    #                 break
    #
    #             else:
    #                 print("Sorry, you may not have typed 'column' or 'row'.")
    #
    #             add_more = input("Hit 'enter' to add replicates, or 'n' to continue: ").lower()
    #             if add_more == "n":
    #                 std_conc *= count
    #                 more_ids = False
    #             else:
    #                 count += 1
    #
    #         std_dict = dict(zip(std_ids, std_conc))
    #         inner["std_conc"] = std_dict
    #
    #
    #     if proj == "SSF00626":
    #         inner["plates"] = plate_ids_1
            # inner["volumes"] = vol_ak
            # inner["od_file"] = r"L:\Molecular Sciences\Small Scale Runs\SSF00627 DSS Spaniel003_discrete host screening\Copy of SSF00627 DSS Spaniel ELN_v2 copy.xlsm"
    run_main(proj_data_dict)
    window.destroy()
    end_time = time()
    split = round(end_time - start_time, 2)
    print(f"Program runtime: {split}s")


"""Modify MCMC chain to burn first few steps and compute post-collapse parameters"""

import argparse
import logging
from pathlib import Path
import sys
from typing import Any, Union

import emcee
import h5py
import numpy as np
import poskiorb
import yaml

sys.path.append("src/models/mcmc")
from likelihood import log_likelihood

def parse_args() -> argparse.Namespace:
    """Parse command line arguments"""

    parser = argparse.ArgumentParser(
        description="clean MCMC chain from burners & compute stellar parameters after collapse",
        epilog="@asimazbunzel on GitHub",
    )
    parser.add_argument(
        "-C",
        "--config-file",
        dest="config_file",
        help="YAML-format configuration filename",
        type=str,
    )

    return parser.parse_args()

def load_yaml(fname: Union[str, Path]) -> Any:
    """Load configuration file with YAML format

    Parameters
    ----------
    fname : `str / Path`
        YAML filename

    Returns
    -------
    `yaml.load`
    """

    if isinstance(fname, Path):
        fname = str(fname)

    with open(fname) as f:
        return yaml.load(f, Loader=yaml.FullLoader)

def main(config_file: str = "") -> None:
    """Runs data processing scripts to turn raw data into cleaned data

    Parameters
    ----------
    config_file : `str`
        Configuration filename

    output_filename : `str`
        Processed data filename
    """
    logger = logging.getLogger(__name__)
    logger.info("making final data set from raw data")

    # load config file
    config = load_yaml(fname=config_file)

    # set some constant values
    nsteps = config["MCMC"].get("steps")
    nburn = config["MCMC"].get("burn")
    filename = config["MCMC"].get("filename")
    output_filename = config["MCMC"].get("processed_filename")
    priorsD = config["MCMC"].get("priorDistributions")
    # Cygnus X-1 properties
    stellarParameters = config["StellarParameters"]

    # load samples & remove burned steps
    print("loading MCMC chain and burning steps", end="... ")
    reader = emcee.backends.HDFBackend(filename, read_only=True)
    samples = reader.get_chain(flat=True, discard=nburn)
    limit_to = (nsteps - nburn) - 1
    samples_r = samples[np.random.choice(samples.shape[0], limit_to, replace=False), :]
    print("done !")
    print("samples pre-CC shape:", samples_r.shape, type(samples_r))

    kwargs = dict()
    kwargs.update(stellarParameters)
    kwargs.update(priorsD)

    # evaluate kicks model
    print("computing binary stellar parameters after kick", end="... ", flush=True)
    samples1, samples2 = [], []
    for k in range(len(samples_r)):
        # replace values of theta and phi outside of boundaries:
        # 0 < theta < pi, and 0 < phi < 2 * pi
        theta = samples_r[k, 4]
        phi = samples_r[k, 5] % (2 * np.pi)

        samples_r[k, 5] = phi
        if theta < 0 or theta > np.pi:
            theta = np.pi - (theta % np.pi)
        samples_r[k, 4] = theta

        # compute new orbit after kick
        (
            a_post,
            p_post,
            e,
            cos_i,
            v_sys,
            _,
            _,
            _,
            _,
        ) = poskiorb.utils.binary_orbits_after_kick(
            a=poskiorb.utils.P_to_a(samples_r[k, 0], samples_r[k, 1], samples_r[k, 2]),
            m1=samples_r[k, 1],
            m2=samples_r[k, 2],
            m1_remnant_mass=kwargs["M_BH"],
            w=samples_r[k, 3],
            theta=samples_r[k, 4],
            phi=samples_r[k, 5],
            ids=np.ones(1),
        )

        # compute likelihood
        args = [
            samples_r[k, 0],
            samples_r[k, 1],
            samples_r[k, 2],
            samples_r[k, 3],
            samples_r[k, 4],
            samples_r[k, 5],
        ]
        ll = log_likelihood(args, **kwargs)

        if not np.isfinite(ll):
            continue

        samples1.append(
            [float(samples_r[k,0]), float(poskiorb.utils.P_to_a(samples_r[k, 0], samples_r[k, 1], samples_r[k, 2])),
             float(samples_r[k,1]), float(samples_r[k,2]), float(samples_r[k,3]), float(samples_r[k,4]), float(samples_r[k,5])]
        )
        samples2.append(
            [float(p_post), float(e), np.rad2deg(np.arccos(float(cos_i))), float(v_sys), ll]
        )
    print("done !")

    # use numpy arrays
    samples_pre = np.asarray(samples1)
    samples_post = np.asarray(samples2)
    print("sample shapes pre, post-CC:", samples_pre.shape, samples_post.shape)

    with h5py.File(output_filename, "w") as f:
        f.create_dataset("mcmc/pre-cc", data=samples_pre, compression="gzip")
        f.create_dataset("mcmc/post-cc", data=samples_post, compression="gzip")

if __name__ == "__main__":

    # parse command line arguments
    args = parse_args()

    main(config_file=args.config_file)


import json
import typing as t

from pyseto import Key, Paseto

from .config import PASETO_VERSIONS_ACCEPTABLE
from .exceptions import EncodeError

PASETO_DEFAULT_USE_ISS: bool = True
PASETO_DEFAULT_USE_IAT: bool = False
PASETO_DEFAULT_EXP: int = 3600
PASETO_DEFAULT_USE_KID: bool = False
PASETO_DEFAULT_SERIALIZER: t.Any = json

class PasetoIssuer(object):
    def __init__(self, app=None, add_context_processor=True):
        if app is not None:
            self.init_app(app, add_context_processor)

    def init_app(self, app, add_context_processor=True):
        """
        Configures an Flask application to use this PasetoIssuer.
        """
        app.paseto_issuer = self

        # _iss
        self._iss = app.config.get("PASETO_ISS", "")
        if not self._iss:
            raise ValueError("PASETO_ISS must be set.")
        if not isinstance(self._iss, str):
            raise ValueError("PASETO_ISS must be str.")

        # _use_iss
        self._use_iss = app.config.get("PASETO_USE_ISS", PASETO_DEFAULT_USE_ISS)
        if not isinstance(self._use_iss, bool):
            raise ValueError("PASETO_USE_ISS must be bool.")

        # _use_iat
        self._use_iat = app.config.get("PASETO_USE_IAT", PASETO_DEFAULT_USE_IAT)
        if not isinstance(self._use_iat, bool):
            raise ValueError("PASETO_USE_IAT must be bool.")

        # _exp
        self._exp = app.config.get("PASETO_EXP", PASETO_DEFAULT_EXP)
        if not isinstance(self._exp, int) or self._exp < 0:
            raise ValueError("PASETO_EXP must be int (>= 0).")

        # _use_kid
        self._use_kid = app.config.get("PASETO_USE_KID", PASETO_DEFAULT_USE_KID)
        if not isinstance(self._use_kid, bool):
            raise ValueError("PASETO_USE_KID must be bool.")

        # _serializer
        self._serializer: t.Any = app.config.get("PASETO_SERIALIZER", PASETO_DEFAULT_SERIALIZER)
        if not hasattr(self._serializer, "dumps") or not callable(self._serializer.dumps):
            raise ValueError("PASETO_SERIALIZER must have a callable 'dumps'.")

        # _keys
        keys: list = app.config.get("PASETO_PRIVATE_KEYS", [])
        if not keys:
            raise ValueError("PASETO_PRIVATE_KEYS must be set.")
        self._keys: dict = {}
        key: t.Any = None
        kid: bytes = b""
        for k in keys:
            if "paserk" in k:
                try:
                    key = Key.from_paserk(k["paserk"])
                except Exception as err:
                    raise ValueError("Invalid PASERK data.") from err
                if key.purpose == "local":
                    raise ValueError("A local key is not allowed.")
            else:
                if "version" not in k:
                    raise ValueError("A key object must have a 'paserk' or a pair of 'version' and 'key'.")
                if not isinstance(k["version"], int):
                    raise ValueError("A 'version' in PASETO_PRIVATE_KEYS must be int.")
                if k["version"] not in PASETO_VERSIONS_ACCEPTABLE:
                    raise ValueError(f"Invalid PASETO version: {k['version']}.")
                if "key" not in k:
                    raise ValueError("A key object must have a 'paserk' or a pair of 'version' and 'key'.")
                try:
                    key = Key.new(k["version"], "public", k["key"])
                except Exception as err:
                    raise ValueError("A 'key' must be a PEM formatted key.") from err
            kid = key.to_paserk_id()
            self._keys[kid] = {"key": key}

        self._paseto = Paseto.new(exp=self._exp, include_iat=self._use_iat)
        return

    def issue(self, payload: dict, kid: str = "") -> str:
        key = self._keys[list(self._keys)[0]] if len(self._keys) == 1 else self._keys.get(kid, None)
        if not key:
            raise ValueError("A signing key is not found.")

        footer: dict = {}
        if self._use_iss:
            payload["iss"] = self._iss
        if self._use_kid:
            footer["kid"] = list(self._keys.keys())[0] if len(self._keys) == 1 else kid
        try:
            if not footer:
                return self._paseto.encode(key["key"], payload, serializer=self._serializer)
            return self._paseto.encode(key["key"], payload, footer, serializer=self._serializer)
        except Exception as err:
            raise EncodeError("Failed to encode a token.") from err


from unittest import mock

import tensorflow as tf
from tfx.orchestration import mlmd_connection_manager as mlmd_cm

import ml_metadata as mlmd

class MlmdConnectionManagerTest(tf.test.TestCase):

  def setUp(self):
    super().setUp()
    self.mock_store_factory = mock.patch.object(
        mlmd, 'MetadataStore', autospec=True).start()
    self.addCleanup(mock.patch.stopall)

  def test_primary_handle(self):
    with mlmd_cm.MLMDConnectionManager.fake() as cm:
      connection_config = cm.primary_mlmd_handle.connection_config
      self.assertEqual(connection_config.WhichOneof('config'), 'fake_database')
      self.mock_store_factory.assert_called_with(connection_config)

    self.mock_store_factory.reset_mock()

    with mlmd_cm.MLMDConnectionManager.sqlite('foo') as cm:
      connection_config = cm.primary_mlmd_handle.connection_config
      self.assertEqual(connection_config.WhichOneof('config'), 'sqlite')
      self.mock_store_factory.assert_called_with(connection_config)

  def test_unusable_without_enter(self):
    cm = mlmd_cm.MLMDConnectionManager.fake()
    with self.assertRaisesRegex(RuntimeError, 'not entered yet'):
      cm.primary_mlmd_handle  # pylint: disable=pointless-statement

  def test_enter_synced_with_handle(self):
    cm = mlmd_cm.MLMDConnectionManager.fake()
    with cm:
      handle = cm.primary_mlmd_handle
      self.assertIsNotNone(handle.store)
    with self.assertRaisesRegex(
        RuntimeError, 'Metadata object is not in enter state'):
      handle.store  # pylint: disable=pointless-statement

  def test_multiple_enterable(self):
    cm = mlmd_cm.MLMDConnectionManager.fake()
    with cm:
      with cm:
        m1 = cm.primary_mlmd_handle
      m2 = cm.primary_mlmd_handle
      self.assertIs(m1, m2)
    with self.assertRaises(RuntimeError):
      cm.primary_mlmd_handle  # pylint: disable=pointless-statement

if __name__ == '__main__':
  tf.test.main()


"""
Compute the RCA.

title: rca.py
author: Valentin Louf
email: valentin.louf@bom.gov.au
institution: Monash University and Bureau of Meteorology
date: 30/03/2020
"""
import gc
import os

import pyart
import cftime
import numpy as np
import pandas as pd
import xarray as xr

from .cluttermask import _read_radar

def composite_mask(date, timedelta=7, indir="compomask", prefix="cpol_cmask_", freq_thrld=0.9):
    """
    Generate composite clutter mask.

    Parameters:
    -----------
    date: Timestamp
        Date of processing
    timedelta: int
        Time delta for the composite.
    indir: str
        Where clutter mask are stored
    prefix: str
        What is the clutter mask file prefix.
    freq_thrld: float
        Frequency threshold (0 < f < 1).

    Returns:
    --------
    mask: ndarray
        Clutter mask
    """

    def get_mask_list(date, timedelta, indir, prefix):
        drange = pd.date_range(date - pd.Timedelta(f"{timedelta}D"), date)
        flist = []
        for day in drange:
            file = os.path.join(indir, prefix + "{}.nc".format(day.strftime("%Y%m%d")))
            if os.path.isfile(file):
                flist.append(file)
        return flist

    flist = get_mask_list(date, timedelta, indir, prefix)
    if len(flist) == 1:
        composite = xr.open_dataset(flist[0]).clutter_mask.values
    else:
        cmask = [xr.open_dataset(f).clutter_mask.values for f in flist]
        cmaskarr = np.concatenate(cmask, axis=np.newaxis).reshape((len(flist), 360, 20))
        compo_freq = cmaskarr.sum(axis=0) / len(flist)
        composite = compo_freq > freq_thrld
        if np.sum(composite) == 0:
            print(f'BAD COMPOSITE FOR {date}')
            composite = compo_freq != 0

    return composite

def single_mask(date, indir="compomask", prefix="cpol_cmask_"):
    """
    Generate clutter mask.

    Parameters:
    -----------
    date: Timestamp
        Date of processing
    indir: str
        Where clutter mask are stored
    prefix: str
        What is the clutter mask file prefix.

    Returns:
    --------
    mask: ndarray
        Clutter mask
    """
    file = os.path.join(indir, prefix + "{}.nc".format(date.strftime("%Y%m%d")))
    if not os.path.isfile(file):
        file = sorted(glob.glob(os.path.join(indir, '*.nc')))[-1]

    print(f'Using emergency mask {file}')
    cmask = xr.open_dataset(file).clutter_mask.values
    return cmask

def extract_clutter(infile, clutter_mask, refl_name="total_power"):
    """
    Extract the clutter and compute the RCA value.

    Parameters:
    -----------
    infile: str
        Input radar file.
    clutter_mask: numpy.array(float)
        Clutter mask (360 deg x 20 km)
    refl_name: str
        Uncorrected reflectivity field name.

    Returns:
    --------
    dtime: np.datetime64
        Datetime of infile
    rca: float
        95th percentile of the clutter reflectivity.
    """
    # Radar data.
    radar = _read_radar(infile, refl_name)

    dtime = cftime.num2pydate(radar.time["data"][0], radar.time["units"])

    sl = radar.get_slice(0)
    r = radar.range["data"]
    azi = radar.azimuth["data"][sl]
    try:
        refl = radar.fields[refl_name]["data"][sl][:, r < 20e3].filled(np.NaN)
    except AttributeError:
        refl = radar.fields[refl_name]["data"][sl][:, r < 20e3]
    zclutter = np.zeros_like(refl) + np.NaN

    r = r[r < 20e3]
    R, A = np.meshgrid(r, azi)
    R = (R // 1000).astype(int)
    A = (np.round(A) % 360).astype(int)

    # Mask.
    RC, AC = np.meshgrid(np.arange(20), np.arange(360))

    npos = np.where(clutter_mask)
    for ir, ia in zip(RC[npos], AC[npos]):
        pos = (R == ir) & (A == ia)
        zclutter[pos] = refl[pos]

    try:
        zclutter = zclutter[~np.isnan(zclutter)]
        rca = np.percentile(zclutter, 95)
    except IndexError:
        # Empty array full of NaN.
        raise ValueError("All clutter is NaN.")

    del radar
    return dtime, rca


"""
Simple Bot taken from the python-telegram-bot examples.
Deployed using heroku.
"""

import logging
import os
import random
import string

import telegram
from PIL import Image
from telegram import Update
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext

import secrets
import static

def get_random_alphanumeric_string(length):
    """ Get a string of random alphanumeric characters of specified length"""
    return ''.join((random.choice(string.ascii_letters + string.digits) for _ in range(length)))

TOKEN = os.environ['ENV_BOT_TOKEN']
PORT = int(os.environ.get('PORT', 5000))

# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
                    level=logging.INFO)

logger = logging.getLogger(__name__)

# Define a few command handlers. These usually take the two arguments update and
# context. Error handlers also receive the raised TelegramError object in error.
def start(update, context):
    """Send a message when the command /start is issued."""
    update.message.reply_text(static.start_text, parse_mode=telegram.ParseMode.MARKDOWN_V2)

def help_nandler(update, context):
    """Send a message when the command /help is issued."""
    update.message.reply_text(static.commands_text, parse_mode=telegram.ParseMode.MARKDOWN_V2)

def coords(update, context):
    """Send the coordinates picture"""
    try:
        y, x = map(float, update.message.text.split()[1:])
        if y < static.min_h or x < static.min_w or y > static.max_h or x > static.max_w:
            raise ValueError
    except ValueError:
        update.message.reply_text('Incorrect coordinates specified')
        return

    background = Image.open('res/map.jpg', 'r')
    bg_w, bg_h = background.size
    fg_size = int(min(bg_w, bg_h) * 7 / 100)
    foreground = Image.open('res/loc.png', 'r').convert('RGBA').resize((fg_size, fg_size))

    offset = (int((x - static.min_w) / (static.max_w - static.min_w) * bg_w - fg_size / 2),
              int(bg_h - (y - static.min_h) / (static.max_h - static.min_h) * bg_h - fg_size))
    background.paste(foreground, offset, foreground)  # third parameter is alpha mask

    filename = get_random_alphanumeric_string(12)
    background.save(f'res/{filename}.png')
    update.message.reply_photo(photo=open(f'res/{filename}.png', 'rb'))
    os.remove(f'res/{filename}.png')

def location(update: Update, context: CallbackContext):
    user_location = update.message.location
    if static.min_w <= user_location.longitude <= static.max_w and \
            static.min_h <= user_location.latitude <= static.max_h:
        update.message.reply_text(f'You are on point! '
                                  f'Your location is: {user_location.longitude} {user_location.latitude}')
        update.message.text = f'/coords {user_location.latitude} {user_location.longitude}'
        coords(update, context)
    else:
        update.message.reply_text(f'You are not inside the map. '
                                  f'Your location however is: {user_location.longitude} {user_location.latitude}')

def error(update, context):
    """Log Errors caused by Updates."""
    logger.warning('Update "%s" caused error "%s"', update, context.error)

def main():
    """Start the bot."""
    # Create the Updater and pass it your bot's token.
    # Make sure to set use_context=True to use the new context based callbacks
    # Post version 12 this will no longer be necessary
    updater = Updater(TOKEN, use_context=True)

    # Get the dispatcher to register handlers
    dp = updater.dispatcher

    # on different commands - answer in Telegram
    dp.add_handler(CommandHandler("start", start))
    dp.add_handler(CommandHandler("help", help_nandler))
    dp.add_handler(CommandHandler("coords", coords))
    dp.add_handler(MessageHandler(Filters.location, location))

    # log all errors
    dp.add_error_handler(error)

    # Start the Bot
    # Use updater instead of poller
    updater.start_webhook(listen="0.0.0.0",
                          port=int(PORT),
                          url_path=TOKEN)
    updater.bot.setWebhook('https://ams-test-task.herokuapp.com/' + TOKEN)

    # Run the bot until you press Ctrl-C or the process receives SIGINT,
    # SIGTERM or SIGABRT. This should be used most of the time, since
    # start_polling() is non-blocking and will stop the bot gracefully.
    updater.idle()

if __name__ == '__main__':
    main()


from docusign_rooms import RoomsApi, RolesApi, RoomForCreate, FieldDataForCreate
from flask import session, request

from ..utils import create_rooms_api_client

class Eg001CreateRoomWithDateController:
    @staticmethod
    def get_args():
        """Get required session and request arguments"""
        return {
            "account_id": session["ds_account_id"],  # Represents your {ACCOUNT_ID}
            "access_token": session["ds_access_token"],  # Represents your {ACCESS_TOKEN}
            "room_name": request.form.get("room_name"),
        }

    @staticmethod
    def worker(args):
        """
        1. Create an API client with headers
        2. Get Default Admin role id
        3. Create RoomForCreate object
        4. Post the room using SDK
        """
        # Step 1. Create an API client with headers
        #ds-snippet-start:Rooms1Step2
        api_client = create_rooms_api_client(
            access_token=args["access_token"]
        )
        #ds-snippet-end:Rooms1Step2

        # Step 2. Get Default Admin role id
        roles_api = RolesApi(api_client)
        roles = roles_api.get_roles(account_id=args["account_id"])
        role_id = [role.role_id for role in roles.roles if role.is_default_for_admin][0]

        # Step 3. Create RoomForCreate object
        #ds-snippet-start:Rooms1Step3
        room = RoomForCreate(
            name=args["room_name"],
            role_id=role_id,
            transaction_side_id="listbuy",
            field_data=FieldDataForCreate(
                data={
                    'address1': '123 EZ Street',
                    'address2': 'unit 10',
                    'city': 'Galaxian',
                    'state': 'US-HI',
                    'postalCode': '88888',
                    'companyRoomStatus': '5',
                    'comments': '''Lorem ipsum dolor sit amet, consectetur adipiscin'''
                }
            )
        )
        #ds-snippet-end:Rooms1Step3

        # Step 4. Post the room using SDK
        #ds-snippet-start:Rooms1Step4
        rooms_api = RoomsApi(api_client)
        response = rooms_api.create_room(
            body=room,
            account_id=args["account_id"]
        )
        #ds-snippet-end:Rooms2Step4
        return response


from django.contrib.auth.models import User
from django.test import Client, TestCase

import unittest

from model_mommy import mommy

from . import models
from . import views

class VoteTest(TestCase):
    def setUp(self):
        self.client = Client()

        self.users = mommy.make(User, _quantity=2)

        self.features = mommy.make(
            models.Feature,
            _quantity=2,
            _fill_optional=True,
        )

    def test_feature_exists(self):
        features = models.Feature.objects.all()
        self.assertIn(self.features[0], features)
        self.assertIn(self.features[1], features)

    def test_feature_page(self):
        self.client.force_login(self.users[0])
        response = self.client.get('/')

        self.assertEqual(response.status_code, 200)
        self.assertContains(response, self.features[0].description)
        self.assertContains(response, self.features[1].description)

    def test_feature_page_bad_user(self):
        response = self.client.get('/')

        self.assertEqual(response.status_code, 200)
        self.assertNotContains(response, self.features[0].description)
        self.assertNotContains(response, self.features[1].description)

    def test_feature_vote_add(self):
        self.assertEqual(self.features[0].votes(), 0)
        self.assertEqual(self.features[1].votes(), 0)

        self.client.force_login(self.users[0])
        response = self.client.get('/votes/{}/'.format(self.features[0].pk))

        self.assertRedirects(response, '/', 302, 200)
        self.assertEqual(self.features[0].votes(), 1)
        self.assertEqual(self.features[1].votes(), 0)

        response = self.client.get('/')
        self.assertContains(response, '1')

        self.client.logout()

        self.client.force_login(self.users[1])
        response = self.client.get('/votes/{}/'.format(self.features[0].pk))

        self.assertRedirects(response, '/', 302, 200)
        self.assertEqual(self.features[0].votes(), 2)
        self.assertEqual(self.features[1].votes(), 0)

        response = self.client.get('/')
        self.assertContains(response, '2')


from src.day19_not_enough_minerals.blueprint import Blueprint
from re import findall
from z3 import IntVector, Optimize, And

def get_blueprints(path):
    blueprints = []
    with open(path) as file:
        for line in file:
            id_and_costs = list(map(lambda string: int(string), findall(r'\d+', line)))
            blueprints.append(Blueprint(*id_and_costs))
    return blueprints

def solve_optimization_problem(blueprint: Blueprint, n):
    solver = Optimize()  # Z3 Optimize solver object
    # Z3 variables vectors
    ore, clay, obsidian, geode = IntVector("ore", n+1),  IntVector("clay", n+1), IntVector("obsidian", n+1),\
        IntVector("geode", n+1)
    ore_robots, clay_robots, obsidian_robots, geode_robots = IntVector("ore_robots", n+1),\
        IntVector("clay_robots", n+1), IntVector("obsidian_robots", n+1), IntVector("geode_robots", n+1)
    new_ore_robots, new_clay_robots, new_obsidian_robots, new_geode_robots = IntVector("new_ore_robots", n+1),\
        IntVector("new_clay_robots", n+1), IntVector("new_obsidian_robots", n+1), IntVector("new_geode_robots", n+1)
    # Initial state
    solver.add(And(ore[0] == 0, clay[0] == 0, obsidian[0] == 0, geode[0] == 0))
    solver.add(And(ore_robots[0] == 1, clay_robots[0] == 0, obsidian_robots[0] == 0, geode_robots[0] == 0))
    solver.add(And(new_ore_robots[0] == 0, new_clay_robots[0] == 0, new_obsidian_robots[0] == 0,
                   new_geode_robots[0] == 0))
    # Equations for each minute
    for t in range(1, n + 1):
        # Resources generation and consumption
        solver.add(ore[t] == ore[t-1] + ore_robots[t-1] - new_ore_robots[t-1] * blueprint.ore_for_ore_robot -
                   new_clay_robots[t-1] * blueprint.ore_for_clay_robot - new_obsidian_robots[t-1] *
                   blueprint.ore_for_obsidian_robot - new_geode_robots[t-1] * blueprint.ore_for_geode_robot)
        solver.add(clay[t] == clay[t-1] + clay_robots[t-1] - new_obsidian_robots[t-1] *
                   blueprint.clay_for_obsidian_robot)
        solver.add(obsidian[t] == obsidian[t-1] + obsidian_robots[t-1] - new_geode_robots[t-1] *
                   blueprint.obsidian_for_geode_robot)
        solver.add(geode[t] == geode[t-1] + geode_robots[t-1])
        # Robots fabrication
        solver.add(ore_robots[t] == ore_robots[t-1] + new_ore_robots[t-1])
        solver.add(clay_robots[t] == clay_robots[t-1] + new_clay_robots[t-1])
        solver.add(obsidian_robots[t] == obsidian_robots[t-1] + new_obsidian_robots[t-1])
        solver.add(geode_robots[t] == geode_robots[t-1] + new_geode_robots[t-1])
        # Buying constraints
        solver.add(new_ore_robots[t] * blueprint.ore_for_ore_robot <= ore[t])
        solver.add(new_clay_robots[t] * blueprint.ore_for_clay_robot <= ore[t])
        solver.add(new_obsidian_robots[t] * blueprint.ore_for_obsidian_robot <= ore[t])
        solver.add(new_geode_robots[t] * blueprint.ore_for_geode_robot <= ore[t])
        solver.add(new_obsidian_robots[t] * blueprint.clay_for_obsidian_robot <= clay[t])
        solver.add(new_geode_robots[t] * blueprint.obsidian_for_geode_robot <= obsidian[t])
        solver.add(And(0 <= new_ore_robots[t], new_ore_robots[t] <= 1))
        solver.add(And(0 <= new_clay_robots[t], new_clay_robots[t] <= 1))
        solver.add(And(0 <= new_obsidian_robots[t], new_obsidian_robots[t] <= 1))
        solver.add(And(0 <= new_geode_robots[t], new_geode_robots[t] <= 1))
        solver.add(new_ore_robots[t] + new_clay_robots[t] + new_obsidian_robots[t] + new_geode_robots[t] <= 1)
    solver.maximize(geode[n])
    solver.check()
    return solver.model()[geode[n]].as_long()

def maximize_for_all_blueprints(path, slicing=None, minutes=24):
    blueprints = get_blueprints(path)
    outcome = 1 if slicing is not None else 0
    slicing_param = min(len(blueprints), slicing if slicing is not None else len(blueprints))
    for blueprint in blueprints[:slicing_param]:
        result = solve_optimization_problem(blueprint, minutes)
        if slicing is None:
            outcome += blueprint.id * result
        else:
            outcome *= result
    return outcome


import logging
import sys
import traceback
from functools import (
    partial,
)
from pathlib import (
    Path,
)

import jinete as jit

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

def main():
    logger.info("Starting...")
    file_path = Path(sys.argv[1])
    solver = jit.Solver(
        loader_kwargs={"file_path": file_path},
        algorithm=jit.GraspAlgorithm,
        algorithm_kwargs={"first_solution_kwargs": {"episodes": 1, "randomized_size": 2}, "episodes": 5},
        storer=jit.StorerSet,
        storer_kwargs={
            "storer_cls_set": {
                jit.PromptStorer,
                partial(jit.GraphPlotStorer, file_path=file_path.parent / f"{file_path.name}.png"),
                partial(jit.FileStorer, file_path=file_path.parent / f"{file_path.name}.output"),
            }
        },
    )
    result = solver.solve()  # noqa
    logger.info("Finished...")

if __name__ == "__main__":
    try:
        main()
    except Exception as exc:
        traceback.print_exc()
        raise exc


import pandas as pd
import json
import matplotlib
import matplotlib.pyplot as plt

def show_fitbit_steps():
    dates = pd.date_range('20170310', '20170324')
    df_step_per_min = pd.DataFrame()
    step_per_day = list()

    for date in dates:
        fname = 'data/' + date.strftime('%Y%m%d') + '_step.json'
        f = open(fname)
        time = list()
        value = list()

        json_data = json.loads(f.read())
        date_time = json_data['activities-steps'][0]['dateTime']
        data_set = json_data['activities-steps-intraday']['dataset']
        step_per_day.append(int(json_data['activities-steps'][0]['value']))
        f.close()

        for data in data_set:
            time.append(data['time'])
            value.append(data['value'])

        df_step_per_min[date_time] = value

    df_step_per_min.index = time
    df_step_per_min['mean'] = df_step_per_min.mean(axis=1).astype(int)

    df_step_per_day = pd.DataFrame(step_per_day, index=dates, columns=['step'])

    df_step_per_min.plot(y='mean', subplots=True, title='Mean step per minute')
    df_step_per_day.plot(subplots=True, title='Step per day')
    plt.show()

if __name__ == '__main__':
    show_fitbit_steps()


import paddle
import numpy as np

if paddle.fluid.core.is_compiled_with_npu():
  paddle.fluid.framework._set_dygraph_tracer_expected_place(paddle.NPUPlace(0))

x_np = np.array(list(range(0, 3 * 4 * 2 * 2))).reshape([3, 4, 2, 2]).astype(np.float32)
# x_np = np.ones([3, 4, 1, 2]).astype(np.float32)

y_np = np.array([x for x in range(4 * 1 * 2)]).reshape([4, 1, 2]).astype(np.float32)

x_np = np.random.random([100, 3]).astype(np.float32)
y_np = np.random.random([100,]).astype(np.float32)
# x_np = np.array([1, 2]).reshape([2]).astype(np.float32)
# y_np = np.array([1, 2, 3, 4, 5, 6, 7, 8]).reshape([4, 2]).astype(np.float32)
x_var = paddle.to_tensor(x_np, stop_gradient=False)
y_var = paddle.to_tensor(y_np, stop_gradient=False)

out = paddle.fluid.layers.nn.elementwise_add(x_var, y_var, axis=0)
# print(out)
s = out.sum()
# print(s)
s.backward()
# print(out.grad)
# print(x_var.grad)
# print(y_var.grad)


import datetime
import pytz
import random
from flask import render_template, request, redirect, url_for
from flask import Flask
from flask_migrate import Migrate
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SECRET_KEY'] = '5791628bb0b13ce0c676dfde280ba111'
app.config['SQLALCHEMY_DATABASE_URI'] = ('postgresql://uqmsiv4msken1z8u9jbr:'
                                         'ScqDwM7eRnwZOy8DtwESB06fmkiPCD@'
                                         'bgvj1gcg7xvieqm7pey1-postgresql.services.clever-cloud.com:5432/'
                                         'bgvj1gcg7xvieqm7pey1')
app.config['SQLALCHEMY_ENGINE_OPTIONS'] = {
    'pool_pre_ping': True,
    'pool_recycle': 3600
}
db = SQLAlchemy(app)
migrate = Migrate(app, db)

class CancerAwarenessQuestionnaireOutside(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    inner_table_id = db.Column(db.Integer)
    number = db.Column(db.Integer)
    suspicion = db.Column(db.Boolean)
    question1 = db.Column(db.Boolean)
    question2 = db.Column(db.Boolean)
    question3 = db.Column(db.Boolean)
    question4 = db.Column(db.Boolean)
    question5 = db.Column(db.Boolean)
    question6 = db.Column(db.Boolean)
    question7 = db.Column(db.Boolean)
    question8 = db.Column(db.Boolean)
    question9 = db.Column(db.Boolean)
    date_sent = db.Column(db.DateTime)

suspicion = False

@app.route('/', methods=['GET', 'POST'])
def form():
    if request.method == 'POST':
        global suspicion
        suspicion = False

        def convert(answer):
            global suspicion
            if answer == 'yes':
                suspicion = True
                return True
            else:
                return False

        numbers_raw = CancerAwarenessQuestionnaireOutside.query.with_entities(
            CancerAwarenessQuestionnaireOutside.number).all()
        numbers = []
        for number in numbers_raw:
            numbers.append(number.number)
        number = random.randint(100000, 300000)
        while number in numbers:
            number = random.randint(100000, 300000)

        q = CancerAwarenessQuestionnaireOutside()
        q.number = number
        q.question1 = convert(request.form['rq1'])
        q.question2 = convert(request.form['rq2'])
        q.question3 = convert(request.form['rq3'])
        q.question4 = convert(request.form['rq4'])
        q.question5 = convert(request.form['rq5'])
        q.question6 = convert(request.form['rq6'])
        q.question7 = convert(request.form['rq7'])
        q.question8 = convert(request.form['rq8'])
        q.question9 = convert(request.form['rq9'])
        q.suspicion = suspicion
        q.date_sent = datetime.datetime.now(pytz.timezone("Europe/Moscow")).strftime("%Y-%m-%d %H:%M:%S")
        db.session.add(q)
        db.session.commit()

        return redirect(url_for('result', number=number, status=suspicion))

    return render_template("form.html")

@app.route('/result/<int:number>/<int:status>')
def result(number, status):
    return render_template("result.html", status=status, number=number)


from twisted.application import internet, service
from twisted.internet import protocol, reactor

class Echo(protocol.Protocol):
    def dataReceived(self, data):
        self.transport.write(data)

class EchoFactory(protocol.Factory):
    def buildProtocol(self, addr):
        return Echo()

# from echo import EchoFactory

application = service.Application("echo")
echoService = internet.TCPServer(8000, EchoFactory())
echoService.setServiceParent(application)


""" Utility functions and classes for SRP

Context : SRP
Module  : SRPGW
Version : 1.2.0
Author  : Stefano Covino
Date    : 04/03/2019
E-mail  : stefano.covino@brera.inaf.it
URL:    : http://www.merate.mi.astro.it/utenti/covino

Usage   :

Remarks :

History : (29/11/2016) First version.
        : (07/12/2016) Better management of input coordinates and masks.
        : (04/03/2019) GAIA DR2
"""

from astropy.table import Table, join, unique
from astropy import units as u
import SRPGW as GW
from astroquery.xmatch import XMatch

def GetGAIAxMatch(tab, rds, cccol=(GW.RACOL,GW.DECCOL)):
    itab = Table([tab[cccol[0]],tab[cccol[1]]])
    #otab = XMatch.query(cat1=itab,cat2='vizier:I/337/gaia',max_distance=rds*u.arcsec,colRA1=cccol[0],colDec1=cccol[1])
    otab = XMatch.query(cat1=itab,cat2='vizier:I/345/gaia2',max_distance=rds*u.arcsec,colRA1=cccol[0],colDec1=cccol[1])
    #
    otabsel = Table([otab[cccol[0]],otab[cccol[1]],otab['source_id']])
    #
    if len(otabsel) > 0:
        newtab = join(tab, otabsel, join_type='left')
    else:
        tab[GW.GAIACOL] = -99
        newtab = tab
    #
    try:
        newtab2 = unique(newtab,GW.IDCOL)
    except KeyError:
        newtab2 = newtab
    #
    if 'source_id' in newtab2.columns.keys():
        newtab2['source_id']._sharedmask = False
        newtab2['source_id'][newtab2['source_id'].mask == True] = -99
        #
        if GW.GAIACOL in newtab2.columns.keys():
            newtab2.remove_column(GW.GAIACOL)
        newtab2.rename_column('source_id',GW.GAIACOL)
    #
    return newtab2


import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets("./data/", one_hot=True)

batch_size = 64
lr = 1e-4
# high lr can make GAN not study anything
img_dim = 784
noise_dim = 100
n_epochs = 20

G_input = tf.placeholder(tf.float32, shape=[None, noise_dim])
# D_input = tf.placeholder(tf.float32, shape=[None, 28, 28, 1])
D_input = tf.placeholder(tf.float32, shape=[None, 784])
is_training = tf.placeholder(tf.bool)

def leaky_relu(x, alpha=0.2):
    return 0.5*(1+alpha)*x + 0.5*(1-alpha)*abs(x)

def sample_Z(n_sample, n_dim):
    return np.random.uniform(-1., 1., size=[n_sample, n_dim])

def conv2d(input, kernel, strides=[1, 2, 2, 1], scope_name='conv2d', conv_type='SAME', spectral_norm=True, update_collection=None):
    out_dim = kernel[3]
    with tf.variable_scope(scope_name):
        weights = tf.get_variable('weights', kernel, tf.float32, initializer=tf.random_normal_initializer(stddev=0.02))
        bias = tf.get_variable('bias', out_dim, tf.float32, initializer=tf.constant_initializer(0))
        if spectral_norm:
            weights = weights_spectral_norm(weights, update_collection=update_collection)

        conv = tf.nn.bias_add(tf.nn.conv2d(input, weights, strides=strides, padding=conv_type), bias)
        return conv

def deconv2d(input, kernel, output_shape, strides=[1, 2, 2, 1], scope_name='deconv2d', deconv_type='SAME'):
    out_dim = kernel[2]
    with tf.variable_scope(scope_name):
        weights = tf.get_variable('weights', kernel, tf.float32, initializer=tf.random_normal_initializer(stddev=0.02))
        bias = tf.get_variable('bias', out_dim, tf.float32, initializer=tf.constant_initializer(0))
        try:
            deconv = tf.nn.bias_add(tf.nn.conv2d_transpose(input, weights, output_shape, strides=strides, padding=deconv_type), bias)
        except:
            deconv = tf.nn.bias_add(tf.nn.deconv2d(input, weights, output_shape, strides=strides, padding=deconv_type), bias)

        return deconv

def linear(input, output_size, scope_name='linear', spectral_norm=True, update_collection=None):
    shape = input.get_shape()
    input_size = shape[1]

    with tf.variable_scope(scope_name):
        weights = tf.get_variable('weights', [input_size, output_size], tf.float32, initializer=tf.random_normal_initializer(stddev=0.02))
        bias = tf.get_variable('bias', output_size, tf.float32, initializer=tf.constant_initializer(0))

        if spectral_norm:
            weights = weights_spectral_norm(weights, update_collection=update_collection)
        output = tf.add(tf.matmul(input, weights), bias)
        return output

def l2_norm(input, eps=1e-12):
    input_norm = input / (tf.reduce_sum(input**2)**0.5+eps)
    return input_norm

def weights_spectral_norm(weights, u=None, iteration=1, update_collection=None, reuse=False, name='weights_SN'):
    with tf.variable_scope(name) as scope:
        if reuse:
            scope.reuse_variables()

        w_shape = weights.get_shape().as_list()
        w_mat = tf.reshape(weights, [-1, w_shape[-1]])

        if u is None:
            u = tf.get_variable('u', shape=[1, w_shape[-1]], initializer=tf.truncated_normal_initializer(), trainable=False)

        def power_iteration(u, ite):
            v_ = tf.matmul(u, tf.transpose(w_mat))
            v_hat = l2_norm(v_)
            u_ = tf.matmul(v_hat, w_mat)
            u_hat = l2_norm(u_)
            return u_hat, v_hat, ite + 1

        u_hat, v_hat, _ = power_iteration(u, iteration)

        sigma = tf.matmul(tf.matmul(v_hat, w_mat), tf.transpose(u_hat))
        w_mat = w_mat / sigma

        if update_collection is None:
            with tf.control_dependencies([u.assign(u_hat)]):
                w_norm = tf.reshape(w_mat, w_shape)
        else:
            if not(update_collection=='NO_OPS'):
                print(update_collection)
                tf.add_to_collection(update_collection, u.assign(u_hat))
            w_norm = tf.reshape(w_mat, w_shape)

        return w_norm

def GNet(Z, batch_size, reuse=False):
    with tf.variable_scope('GNet', reuse=reuse):
        h1 = linear(Z, 7*7*128, scope_name='g_1')
        h1 = tf.layers.batch_normalization(h1, training=is_training)
        h1 = tf.nn.relu(h1)
        h1 = tf.reshape(h1, shape=[-1, 7, 7, 128])

        h2 = deconv2d(h1, kernel=[5, 5, 64, 128], output_shape=[batch_size, 14, 14, 64], scope_name='g_2')
        h2 = tf.layers.batch_normalization(h2, training=is_training)
        h2 = tf.nn.relu(h2)

        h3 = deconv2d(h2, kernel=[5, 5, 1, 64], output_shape=[batch_size, 28, 28, 1], scope_name='g_3')
        h3 = tf.nn.tanh(h3)
        h3 = tf.reshape(h3, [-1, 784])

        return h3

def DNet(X, reuse=False):
    X = tf.reshape(X, [-1, 28, 28, 1])
    with tf.variable_scope('DNet', reuse=reuse):
        h1 = conv2d(X, kernel=[5, 5, 1, 64], scope_name='d_0')
        h1 = tf.layers.batch_normalization(h1, training=is_training)
        h1 = tf.nn.leaky_relu(h1)

        h2 = conv2d(h1, kernel=[5, 5, 64, 128], scope_name='d_1')
        h2 = tf.layers.batch_normalization(h2, training=is_training)
        h2 = tf.nn.leaky_relu(h2)
        h2 = tf.reshape(h2, shape=[-1, 7*7*128])

        h3 = linear(h2, output_size=1024, scope_name='d_2')
        h3 = tf.nn.leaky_relu(h3)
        h3 = linear(h3, output_size=2, scope_name='d_3')

        return h3

G_sample = GNet(G_input, batch_size=batch_size)
# G_sample_ = GNet(G_input, batch_size=n)
print(G_sample.get_shape())

r_logit = DNet(D_input)
f_logit = DNet(G_sample, reuse=True)

D_loss = -(tf.reduce_mean(r_logit) - tf.reduce_mean(f_logit))
G_loss = -tf.reduce_mean(f_logit)

# GP
fake_data = G_sample
real_data = D_input
alpha = tf.random_uniform(shape=[batch_size, 1], minval=0., maxval=1.)
x_hat = alpha*real_data + (1-alpha)*fake_data
D_x_hat = DNet(x_hat, reuse=True)
grad_D_x_hat = tf.gradients(D_x_hat, [x_hat])[0]
slopes = tf.sqrt(tf.reduce_sum(tf.square(grad_D_x_hat), reduction_indices=1))
gradient_penalty = tf.reduce_mean((slopes-1.)**2)
D_loss +=  10*gradient_penalty

t_vars = tf.trainable_variables()
d_vars = [var for var in t_vars if 'd_' in var.name]
g_vars = [var for var in t_vars if 'g_' in var.name]

D_step = tf.train.AdamOptimizer(learning_rate=lr).minimize(D_loss, var_list=d_vars)
G_step = tf.train.AdamOptimizer(learning_rate=lr).minimize(G_loss, var_list=g_vars)
# clip_D = [p.assign(tf.clip_by_value(p, -0.1, 0.1)) for p in d_vars]

init = tf.global_variables_initializer()
D_loss_ = []
G_loss_ = []

with tf.Session() as sess:
    sess.run(init)
    writer = tf.summary.FileWriter('./graphs/SNGAN', tf.get_default_graph())

    for i in range(5000):
        X_batch, _ = mnist.train.next_batch(batch_size)
        X_batch = X_batch*2 - 1
        Z_batch = sample_Z(batch_size, noise_dim)

        _, loss_D = sess.run([D_step, D_loss], feed_dict={D_input: X_batch, G_input: Z_batch, is_training: True})

        # Z_batch = sample_Z(batch_size, noise_dim)
        _, loss_G = sess.run([G_step, G_loss], feed_dict={G_input: Z_batch, is_training: True})
        _, loss_G = sess.run([G_step, G_loss], feed_dict={G_input: Z_batch, is_training: True})

        if(i%100==0):
            print("Step: %d\tLoss_G: %.4f\tLoss_D: %.4f"%(i, loss_G, loss_D))
            # print("Step: %d\tLoss_G: %.4f\tLoss_D: %.4f"%(i, loss_G, loss_D))

        if(i%10==0):
            D_loss_.append(loss_D)
            G_loss_.append(loss_G)

    # batch_size = 6
        if(i%100==0):
            n = batch_size
            canvas = np.empty((28*n, 28*n))
            for k in range(n):
                z_ = sample_Z(n, noise_dim)
                g = sess.run(G_sample, feed_dict={G_input: z_, is_training: False})
                g = (g+1)/2
                g = -1*(g-1)
                for j in range(n):
                    canvas[k*28: (k+1)*28, j*28: (j+1)*28] = g[j].reshape([28, 28])

            plt.figure(figsize=(n, n))
            plt.imshow(canvas, origin="upper", cmap="gray")
            plt.savefig('./image_SNGAN/WGAN_SNGAN_DCGAN_MNIST_step_{}.png'.format(i))
            plt.close()
            # plt.show()

    xplot = np.arange(500)
    plt.plot(xplot, D_loss_, label='D_loss')
    plt.plot(xplot, G_loss_, label='G_loss')
    plt.legend()
    # plt.savefig('loss_curve_WGAN_DCGAN_MNIST_3.png')
    plt.show()


import sys
sys.path.insert(0, "../../")
from helpers import utils
# --------------------------------------------------
from typing import List
import copy

def mapping(char):
    if char == "(":
        return 1
    elif char == ")":
        return -1
    elif char == "[":
        return 2
    elif char == "]":
        return -2
    elif char == "{":
        return 3
    elif char == "}":
        return -3
    elif char == "<":
        return 4
    elif char == ">":
        return -4
def read(filename: str) -> List[str]:
    data = []
    with open(filename, 'r') as f:
        for _, row in enumerate(f):
            data.append(map(mapping,list(row.replace('\n', ''))))
    return data

def part_1(data, verbose=False):
    utils.start_timer()
    dict_ = {
        -1: [3,0],
        -2: [57,0],
        -3: [1197,0],
        -4: [25137,0]
    }
    for row in data:
        queue = []
        for char in row:
            if char > 0:
                queue.append(char)
            else:
                opening = queue.pop()
                if char*-1 != opening:
                    dict_[char][1] += 1
                    break
    sum_ = 0
    for _, value in dict_.items():
        sum_ += value[0]*value[1]
    utils.pprint(1, sum_)

def part_2(data,verbose=False):
    utils.start_timer()
    sum_list = []
    for row in data:
        queue = []
        complete = []
        valid = True
        for char in row:
            if char > 0:
                queue.append(char)
            else:
                opening = queue.pop()
                if char*-1 != opening:
                    valid = False
                    break
        if valid:
            sum_ = 0
            for char in reversed(queue):
                sum_ *= 5
                sum_ += char
            sum_list.append(sum_)
    sum_list.sort()
    mid = sum_list[len(sum_list)//2]
    utils.pprint(2, mid)

if __name__ == "__main__":
    print("Advent of Code 2021 - Day 10")
    print("-----------------------------")
    print("Example 1:")
    # 340773
    examples = [
        "()",
        "[]",
        "[<>({}){}[([])<>]]",
        "(((((((((())))))))))",
        "(]", # 57
        "(((()))}", # 1197
        "<([]){()}[{}])", # 3
        "{()()()>", # 25137
    ]
    data = [map(mapping,list(e)) for e in examples]
    part_1(copy.deepcopy(data))
    part_2(copy.deepcopy(data))

    print("Example 2:")
    data = read('ex.txt')
    part_1(copy.deepcopy(data))
    part_2(copy.deepcopy(data))

    print("-----------------------------")
    print("Test:")
    data = read('input.txt')
    part_1(copy.deepcopy(data))
    part_2(copy.deepcopy(data))


#!/usr/bin/env python3

import cgi

# ヘッダの出力
print("Content-Type: text/html")
print("")

# 送信されたフォームデータを取得する
form = cgi.FieldStorage()

# フォームにv1にv2のデータが含まれるか
if (not 'v1' in form) or (not 'v2' in form):
    # 含まれないのでフォームを表示
    print("""
        <form>

        <meta http-equiv="content-type" content="text/html;charset=utf-8">

足して58になるように計算せよ

        <input type="text" name="v1">+
        <input type="text" name="v2">
        <input type="submit" value="計算">
        </form>
    """)
else:
    v1 = form.getvalue("v1", "0")
    v2 = form.getvalue("v2", "0")
    try:
        a1 = int(v1) + int(v2)
    except:
        a1 = 0
    if a1 == 58:
        print("
True
")
    else:
        print("
", "False", a1, "
")


from bokeh.plotting import figure, show

##  defines values on each axis
x = [1, 2, 3, 4, 5]
y = [7, 1, 10, 15, 5]

## Create a new graph plot with a title and axis labels
plot = figure(title="An Example Line Graph", x_axis_label='X Axis', y_axis_label='Y axis')

#plots the line on the graph with a legend label
plot.line(x, y, legend_label="A Key Example.", line_width=5)

#shows the plot in the browser
show(plot)


import connexion
from flask import render_template

from logging.config import dictConfig

dictConfig({
    'version': 1,
    'formatters': {'default': {
        'format': '[%(asctime)s] %(levelname)s in %(name)s: %(message)s',
    }},
    'handlers': {'wsgi': {
        'class': 'logging.StreamHandler',
        'stream': 'ext://flask.logging.wsgi_errors_stream',
        'formatter': 'default'
    }},
    'root': {
        'level': 'INFO',
        'handlers': ['wsgi']
    }
})

app = connexion.App(__name__, specification_dir='openapi/')
app.add_api('specification.yml')

@app.route('/')
def home():
    return render_template('home.html')

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8080, debug=True)


import tkinter as tk
from typing import List

def makeRoot() -> tk.Tk:
    root = tk.Tk()
    root.title('Calculator')
    root.config(padx=10, pady=10, background='#fff')
    root.resizable(False, False)
    return root

def makeLabel(root, **gridOptions) -> tk.Label:
    label = tk.Label(root, text='', anchor='e', justify='right', background='#fff')
    label.grid(**gridOptions)
    return label

def makeDisplay(root, **gridOptions) -> tk.Entry:
    display = tk.Entry(root)
    display.grid(**gridOptions)
    display.config( font=('Helvetica', 40, 'bold'), justify='right', bd=1, relief='flat', highlightthickness=1, highlightcolor='#ccc')
    display.bind('<Control-a>', displayControlA)
    return display

def displayControlA(event):
    event.widget.select_range(0, 'end')
    event.widget.icursor('end')
    return 'break'

def makeButton(root, text, **gridOptions) -> tk.Button:
    btn = tk.Button(root, text=text)
    btn.grid(**gridOptions)
    btn.config(font=('Helvetica', 15, 'normal'), pady=40, width=1, background='#f1f2f3', bd=0, cursor='hand2', highlightthickness=0, highlightcolor='#ccc', activebackground='#ccc', highlightbackground='#ccc')
    return btn

def makeButtons(root, startingRow) -> List[List[tk.Button]]:
    buttonTexts: List[List[str]] = [
        ['7', '8', '9', '+', 'C'],
        ['4', '5', '6', '-', '/'],
        ['1', '2', '3', '*', '^'],
        ['0', '.', '(', ')', '='],
    ]

    buttons: List[List[tk.Button]] = []

    for row, rowValue in enumerate(buttonTexts, start=startingRow):
        buttonRow = []
        for col, colValue in enumerate(rowValue):
            btn = makeButton(root, text=colValue, row=row, column=col, sticky='news', padx=5, pady=5)
            buttonRow.append(btn)
        buttons.append(buttonRow)
    return buttons


import torch
from torch import nn
from torchtuples import tuplefy

class DenseVanillaBlock(nn.Module):
    def __init__(self, in_features, out_features, bias=True, batch_norm=True, dropout=0., activation=nn.ReLU,
                 w_init_=lambda w: nn.init.kaiming_normal_(w, nonlinearity='relu')):
        super().__init__()
        self.linear = nn.Linear(in_features, out_features, bias)
        if w_init_:
            w_init_(self.linear.weight.data)
        self.activation = activation()
        self.batch_norm = nn.BatchNorm1d(out_features) if batch_norm else None
        self.dropout = nn.Dropout(dropout) if dropout else None

    def forward(self, input):
        input = self.activation(self.linear(input))
        if self.batch_norm:
            input = self.batch_norm(input)
        if self.dropout:
            input = self.dropout(input)
        return input

class ProportionalBlock(nn.Module):
    """
        Simulate the last hidden layer in the proportional design. The goal for this operation is
        to exploit the node (X^T b) and apply the formulas of 3 link functions.
    """
    def __init__(self, out_features, option):
        """
        :param out_features: The dimension of the output layer (also that of the output layer for the whole
        neural network).
        :param option: The option for the link functions.
        "log-log": The log-log link function.
        "log-log-2" The log-log link function (implementation following from nnet-survival)
        "log": The log link function.
        "logit": The logit link function.
        """
        super().__init__()
        self.a = torch.nn.Parameter(torch.randn((1, out_features)))
        nn.init.kaiming_normal_(self.a.data, nonlinearity='relu')
        self.option = option

    def forward(self, input):
        option = self.option
        if option == "log-log":
            input = input + torch.log(-torch.log(torch.sigmoid(self.a)))
            input = 1 - torch.exp(-torch.exp(input))
            return input
        elif option == "log-log-2":
            input = torch.pow(torch.sigmoid(self.a), torch.exp(input))
            input = 1 - input
            return input
        elif option == "log":
            input = input + torch.log(torch.sigmoid(self.a))
            Softplus = nn.Softplus()
            input = Softplus(input)
            return input
        elif option == "logit":
            return torch.sigmoid(input + self.a)

class DenseProportionalBlock(nn.Module):
    """
    Simulate the second last hidden layer in the proportional design. The goal for this operation is
    to combine all the effects of the input layer into one node.
    Illustration: (X (R^n), b (R^n) ) -> X^T b (R)
    """
    def __init__(self, in_features,
                 w_init_=lambda w: nn.init.kaiming_normal_(w, nonlinearity='relu')):
        super().__init__()
        self.linear = nn.Linear(in_features, 1, False)
        if w_init_:
            w_init_(self.linear.weight.data)

    def forward(self, input):
        input = self.linear(input)
        return input

class MLPVanilla(nn.Module):
    """
    Vanilla MLP, simulating the fully-connected neural network.
    """
    def __init__(self, in_features, num_nodes, out_features, batch_norm=True, dropout=None, activation=nn.ReLU,
                 output_activation=None, output_bias=True,
                 w_init_=lambda w: nn.init.kaiming_normal_(w, nonlinearity='relu')):
        """
        :param in_features: The dimension of the input vector.
        :param num_nodes: The number of nodes (dimension) for each hidden layer.
        :param out_features: The dimension of the output vector.
        :param batch_norm: Whether applying batch normalization in each layer.
        :param dropout: Whether applying dropout in each layer.
        :param activation: The activation function in each hidden layer (we recommend ReLU and not change it).
        :param output_activation: The activation function for the output layer.
        :param output_bias: Whether there will be bias term in the output layer.
        :param w_init_: The initialization technique.
        """
        super().__init__()
        num_nodes = tuplefy(in_features, num_nodes).flatten()
        if not hasattr(dropout, '__iter__'):
            dropout = [dropout for _ in range(len(num_nodes) - 1)]
        net = []
        for n_in, n_out, p in zip(num_nodes[:-1], num_nodes[1:], dropout):
            net.append(DenseVanillaBlock(n_in, n_out, True, batch_norm, p, activation, w_init_))
        net.append(nn.Linear(num_nodes[-1], out_features, output_bias))
        if output_activation:
            net.append(output_activation)
        self.net = nn.Sequential(*net)

    def forward(self, input):
        return self.net(input)

class MLPProportional(nn.Module):
    def __init__(self, in_features, num_nodes, out_features, batch_norm=True, dropout=None, activation=nn.ReLU,
                 output_activation=None, output_bias=True,
                 w_init_=lambda w: nn.init.kaiming_normal_(w, nonlinearity='relu'), option=None):
        """
        :param in_features: The dimension of the input vector.
        :param num_nodes: The number of nodes (dimension) for each hidden layer.
        :param out_features: The dimension of the output vector.
        :param batch_norm: Whether applying batch normalization in each layer.
        :param dropout: Whether applying dropout in each layer.
        :param activation: The activation function in each hidden layer (we recommend ReLU and not change it).
        :param output_activation: The activation function for the output layer.
        :param output_bias: Whether there will be bias term in the output layer.
        :param w_init_: The initialization technique.
        :param option: Whether the link function is.
        This will take effect in the neural network structure "ProportionalBlock".
        """
        if option not in ['log-log', 'log', 'logit', 'log-log-2']:
            raise ValueError("Please provide with a link function, it should be one of ['log-log', 'log-log-2', 'log', 'logit']")
        super().__init__()
        num_nodes = tuplefy(in_features, num_nodes).flatten()
        if not hasattr(dropout, '__iter__'):
            dropout = [dropout for _ in range(len(num_nodes) - 1)]
        net = []
        for n_in, n_out, p in zip(num_nodes[:-1], num_nodes[1:], dropout):
            net.append(DenseVanillaBlock(n_in, n_out, True, batch_norm, p, activation, w_init_))

        # New Change!
        net.append(DenseProportionalBlock(num_nodes[-1]))
        net.append(ProportionalBlock(out_features, option))
        self.net = nn.Sequential(*net)

    def forward(self, input):
        return self.net(input)


from random import randint
from collections import OrderedDict

# special memory-mapped IO addresses
mmio_locations = OrderedDict([
    ("IOA", 248), # toggle switches SW11-SW8
    ("IOB", 249), # toggle switches  SW7-SW0
    ("IOC", 250), # output on LED7-LED0
    ("IOD", 251), # output on LED15-LED8
    ("IOE", 252), # output on seven-segment
    ("IOF", 253), # output on seven-segment
    ("IOG", 254), # output on seven-segment
    ("IOH", 255)  # output on seven-segment
])

class Memory():
    def __init__(self, size, randomize_memory = False, use_mmio = True):
        global mmio_locations

        self.size = size
        self.mem = [0] * size
        for addr in range (size):
            self.mem[addr] = randint(-128, 128) if randomize_memory else 0
        self.use_mmio = use_mmio

        if (use_mmio):
            self.mem[mmio_locations["IOA"]] = 0;
            self.mem[mmio_locations["IOB"]] = 0;

    def loadHalfWord(self, addr):
        raise 'NYI'
        return val

    def storeHalfWord(self, addr, val):
        raise 'NYI'
        return 1

    def loadByte(self, addr):
        # print('LB: Addr: %d: Val: %d' % (addr, self.mem[addr]))
        if (addr >= self.size):
            print("Warning: address %d larger than memory space; translating to address %d" % (addr, addr % self.size))
            addr = addr % self.size
        val = self.mem[addr]
        return val

    def storeByte(self, addr, val):
        global mmio_locations

        # print('SB: Addr: %d: Val: %d' % (addr, val))
        if (addr >= self.size):
            print("Warning: address %d larger than memory space; translating to address %d" % (addr, addr % self.size))
            addr = addr % self.size
        if (self.use_mmio and (addr == mmio_locations["IOA"] or addr == mmio_locations["IOB"])):
            print("Warning: can't write to memory-mapped switches; ignoring write to address %d" % addr)
            return -1 # don't write to IO switches
        self.mem[addr] = val
        return 1

    def setSwitch(self, switchID, bit):
        global mmio_locations

        bit &= 1

        if (not self.use_mmio):
            return -2

        if (switchID < 0 or switchID > 11):
            print("Warning: switch ID %d exceeds available switches" % switchID)
            return -1

        setmask = 1 << (switchID % 8)

        addr = mmio_locations["IOB"]
        if (switchID > 7):
            addr = mmio_locations["IOA"]

        if(bit == 0):
            setmask = ~setmask;

            self.mem[addr] = self.mem[addr] & setmask
        else:
            self.mem[addr] = self.mem[addr] | setmask

    def getSwitch(self, switchID):
        global mmio_locations

        if (not self.use_mmio):
            return -2

        if (switchID < 0 or switchID > 11):
            print("Warning: switch ID %d exceeds available switches" % switchID)
            return -1

        if (switchID > 7):
            return (self.mem[mmio_locations["IOA"]] >> (switchID - 8)) & 1
        else:
            return (self.mem[mmio_locations["IOB"]] >> switchID) & 1

    def init_testmode(self):
        for i in range(self.size):
            self.mem[i] = i

    def dump(self):
        quarter = int(self.size/4)
        half = 2 * quarter
        half_quarter = 3 * quarter
        for i in range (quarter):
            print('MEM %d[%d (0x%x)] | MEM %d[%d (0x%x)] | MEM %d[%d (0x%x) | MEM %d[%d (0x%x)' % \
                (i, self.mem[i], self.mem[i] & 0xff,\
                i+quarter, self.mem[i+quarter], self.mem[i+quarter] & 0xff,\
                i+half, self.mem[i+half], self.mem[i+half] & 0xff,\
                i+half_quarter, self.mem[i+half_quarter], \
                self.mem[i+half_quarter] & 0xff\
                ))

if __name__ == '__main__':
    test_mem = Memory(256)
    test_mem.init_testmode()
    test_mem.dump_mem()


import torch
import torch.nn as nn
import torch.nn.functional as F

def extract_chunks(A, mel_ids, ofx, tar_len=128):
    ids = torch.arange(0, tar_len, device=A.device)[
        None, :].repeat(len(mel_ids), 1) + ofx[:, None]
    ids = ids + mel_ids[:, None] * A.size(-1)

    chunks = A.transpose(0, 1).flatten(1)[:, ids.long()].transpose(0, 1)

    return chunks

def calc_fmatch_loss(fmaps_gen, fmaps_org):
    loss_fmatch = 0
    for (fmap_gen, fmap_org) in zip(fmaps_gen, fmaps_org):
        fmap_org.detach_()
        loss_fmatch += (fmap_gen - fmap_org).abs().mean()
    loss_fmatch = loss_fmatch / len(fmaps_gen)
    return loss_fmatch

class Conv2DSpectralNorm(nn.Conv2d):
    """Convolution layer that applies Spectral Normalization before every call."""

    def __init__(self, cnum_in,
                 cnum_out, kernel_size, stride, padding=0, n_iter=1, eps=1e-12, bias=True):
        super().__init__(cnum_in,
                         cnum_out, kernel_size=kernel_size,
                         stride=stride, padding=padding, bias=bias)
        self.register_buffer("weight_u", torch.empty(self.weight.size(0), 1))
        nn.init.trunc_normal_(self.weight_u)
        self.n_iter = n_iter
        self.eps = eps

    def l2_norm(self, x):
        return F.normalize(x, p=2, dim=0, eps=self.eps)

    def forward(self, x):

        weight_orig = self.weight.flatten(1).detach()

        for _ in range(self.n_iter):
            v = self.l2_norm(weight_orig.t() @ self.weight_u)
            self.weight_u = self.l2_norm(weight_orig @ v)

        sigma = self.weight_u.t() @ weight_orig @ v
        self.weight.data.div_(sigma)

        x = super().forward(x)

        return x

class DConv(nn.Module):
    def __init__(self, cnum_in,
                 cnum_out, ksize=5, stride=2, padding='auto'):
        super().__init__()
        padding = (ksize-1)//2 if padding == 'auto' else padding
        self.conv_sn = Conv2DSpectralNorm(
            cnum_in, cnum_out, ksize, stride, padding)
        #self.conv_sn = spectral_norm(nn.Conv2d(cnum_in, cnum_out, ksize, stride, padding))
        self.leaky = nn.LeakyReLU(negative_slope=0.2)

    def forward(self, x):
        x = self.conv_sn(x)
        x = self.leaky(x)
        return x

class Discriminator3(nn.Module):
    def __init__(self, cnum_in, cnum):
        super().__init__()
        self.conv1 = DConv(cnum_in, cnum)
        self.conv2 = DConv(cnum, 2*cnum)
        self.conv3 = DConv(2*cnum, 4*cnum)
        self.conv4 = DConv(4*cnum, 4*cnum)
        self.conv5 = DConv(4*cnum, 4*cnum)

    def forward(self, x):
        x1 = self.conv1(x)
        x2 = self.conv2(x1)
        x3 = self.conv3(x2)
        x4 = self.conv4(x3)
        x5 = self.conv5(x4)
        x = nn.Flatten()(x5)

        return x, [x1, x2, x3, x4]


import os
from models.gray_rgb import gray_rgbnet,BasicBlock

import torch

device = torch.device("cuda:3" if torch.cuda.is_available() else "cpu") #set cuda number
print(device)

from os import path
import numpy as np
import cv2

global_models = ["4@1","4@2","4@3"]

model_names = ["36000_loss_0.0006.pth","36000_loss_0.0041.pth","36000_loss_0.0060.pth"] #set name of your model here

global_actions = ["dev","test"] #"test" or "dev"

data_root = "/nfs/private/wdh/cefa-train/dataset/CASIA-CeFA" #dataset path
# data_root = "/Users/wdh/Downloads/CASIA-CeFA/"

for id_model,global_model in enumerate(global_models):
    for global_action in global_actions:
        checkpoints_root = "./checkpoints/gray%s"%global_model
        res_name = "%s_%s_res.txt"%(global_model,global_action)
        model_name = model_names[id_model]

        channels = 3
        rows = 256
        cols = 256

        times = 1

        net = gray_rgbnet(BasicBlock, [2, 2, 2, 2], num_classes=4)

        net.load_state_dict(torch.load(path.join(checkpoints_root,model_name),map_location=device))
        net.eval()
        net.to(device)

        lines = open(path.join(data_root,res_name)).readlines()
        paths = tuple(line.split(" ")[0] for line in lines)

        res_lines = []
        for index,rpath in enumerate(paths[0:]):
            rpath = rpath.strip("\n")
            video_path = path.join(data_root, rpath)
            rgb_root = path.join(video_path, "profile")
            # print(rgb_root)

            filenames = os.listdir(rgb_root)
            frame_count = len(filenames)

            cur_res_dir = []
            for t in range(times):
                id1 = 1

                if global_action == "dev":
                    id2 = id1
                else:
                    id2 = frame_count

                ids = [id1, id2]
                imgs = []
                img_y = None
                for k,id in enumerate(ids):
                    try:
                        img = cv2.resize(cv2.imread(path.join(rgb_root,"%04d.jpg"%id)), (cols, rows))
                    except:
                        print(path.join(rgb_root,"%04d.jpg"%id))
                    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

                    if global_model == "4@1" and global_action == "test":
                        gamma = 0.5
                        invGamma = 1.0 / gamma
                        table = np.array([((i / 255.0) ** invGamma) * 255 for i in np.arange(0, 256)]).astype("uint8")
                        img = cv2.LUT(np.array(img, dtype=np.uint8), table)

                    img = cv2.equalizeHist(img)
                    img = (img / 255.0).astype("float32")
                    img = np.expand_dims(img, axis=0)

                    imgs.append(img)
                    if k == 0:
                        img_y = img.copy()

                cur_tensor = np.concatenate(imgs, 0)
                cur_tensor = np.expand_dims(cur_tensor,axis=0)
                cur_tensor_y = np.expand_dims(img_y,axis=0)
                cur_tensor = torch.tensor(cur_tensor,device=device)
                cur_tensor_y = torch.tensor(cur_tensor_y,device=device)
                # print(cur_tensor.shape)

                res = net(cur_tensor,cur_tensor_y)

                if global_action == "dev":
                    p_real = res.cpu().detach().numpy()[0, 1]
                else:
                    p_real = res.cpu().detach().numpy()[0, 3]

                cur_res_dir.append(p_real)

            p_real_dir = np.mean(cur_res_dir)
            res_line = rpath + " " + str(p_real_dir)
            print(res_line)

            res_lines.append(res_line + "\n")

        f = open(res_name.replace("_res","_gray_res"), "w+")
        f.writelines(res_lines)
        f.close()


from django.db import models

from WiredWorldProject.account.models import Profile
from WiredWorldProject.product.models import Product
from WiredWorldProject.review.validators import rating_validation

class Review(models.Model):
    profile = models.OneToOneField(
        Profile,
        on_delete=models.CASCADE,
        null=False,
        blank=False
    )
    product = models.OneToOneField(
        Product,
        on_delete=models.CASCADE,
        null=False,
        blank=False
    )
    content = models.TextField(
        null=False,
        blank=False
    )
    stars = models.PositiveIntegerField(
        validators=[
            rating_validation
        ]
    )
    created_at = models.DateTimeField(
        auto_now_add=True
    )