import os, time
import zmq
import zmq_helper
import json, joblib
import ast
import training, inference

class Node:
    """A peer-to-peer node that can act as client or server at each round"""

    def __init__(self, context, node_id, peers):
        self.context = context
        self.node_id = node_id
        self.peers = peers
        self.log_prefix = "[" + str(node_id).upper() + "] "
        self.in_connection = None
        self.out_connection = {}
        self.local_model = None  # local model
        self.local_history = None
        self.initialize_node()

    def initialize_node(self):
        """Creates one zmq.ROUTER socket as incoming connection and n number
        of zmq.DEALER sockets as outgoing connections as per adjacency matrix"""
        self.local_history = []
        self.in_connection = zmq_helper.act_as_server(self.context, self.node_id)
        if len(self.peers) > 0:
            for server_id in self.peers :
                self.out_connection[server_id] = zmq_helper.act_as_client(self.context, server_id, self.node_id)

    def print_node_details(self):
        print("*" * 60)
        print("%s Node ID = %s" % (self.log_prefix, self.node_id))
        print("%s Peer IDs = %s %s" % (self.log_prefix, type(self.peers), self.peers))
        print("%s Context = %s" % (self.log_prefix, self.context))
        print("%s Incoming Connection = %s" % (self.log_prefix, self.in_connection))
        print("%s Outgoing Connections = %s" % (self.log_prefix, self.out_connection))
        print("%s History = %s" % (self.log_prefix, self.local_history))
        print("*" * 60)

    def save_model(self):
        model_filename = "/usr/thisdocker/dataset/" + str(self.node_id) + ".pkl"
        joblib.dump(self.local_model, model_filename)

    def load_prev_model(self):
        model_filename = "/usr/thisdocker/dataset/" + str(self.node_id) + ".pkl"
        self.local_model = joblib.load(model_filename)

    def send_model(self, to_node):
        try:
            zmq_helper.send_zipped_pickle(self.out_connection[to_node], self.local_model)
            # self.out_connection[to_node].send_string(self.local_model)
        except Exception as e:
            print("%sERROR establishing socket for to-node" % self.log_prefix)

    def receive_model(self):
        from_node = self.in_connection.recv(0)  # Reads identity
        self.local_model = zmq_helper.recv_zipped_pickle(self.in_connection)  # Reads model object
        # self.local_model = self.in_connection.recv_string()
        return from_node

    def training_step(self, step):
        # local model training
        build_flag = True if step == 1 else False
        self.local_model = training.local_training(self.node_id, self.local_model, build_flag)
        # self.local_model = {"from": self.node_id}  # for debugging
        # self.save_model()

    def inference_step(self):
        inference.eval_on_test_set(self.local_model)

def main():
    """main function"""
    context = zmq.Context()  # We should only have 1 context which creates any number of sockets
    node_id = os.environ["ORIGIN"]
    peers_list = ast.literal_eval(os.environ["PEERS"])
    this_node = Node(context, node_id, peers_list)

    # Read comm template config file
    comm_template = json.load(open('comm_template.json'))
    total_rounds = len(comm_template.keys())

    for i in range(1, total_rounds + 2):
        if i == total_rounds+1 :
            if this_node.node_id == "node"+str(comm_template[str(total_rounds)]["to"]):
                # Last node training
                this_node.training_step(i)
                # Global accuracy
                this_node.inference_step()
        else :
            ith_round = comm_template[str(i)]
            from_node = "node" + str(ith_round["from"])
            to_node = "node" + str(ith_round["to"])
            if node_id == from_node:
                # This node is dealer and receiving node is router
                training_start = time.process_time()
                this_node.training_step(i)
                print("%sTime : Training Step = %s" % (this_node.log_prefix, str(time.process_time() - training_start)))

                print("%sSending iteration %s from %s to %s" % (this_node.log_prefix, str(i), from_node, to_node))
                this_node.send_model(to_node)
            elif node_id == to_node:
                # This node is router and sending node is dealer
                rcvd_from = this_node.receive_model()
                print("%sReceived object %s at iteration %s" % (this_node.log_prefix, str(this_node.local_model), str(i)))

                this_node.save_model()

                # Logging iteration and prev_node for audit
                this_node.local_history.append({"iteration":i, "prev_node":rcvd_from.decode("utf-8")})

    # this_node.print_node_details()

if __name__ == "__main__":
    main()


from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
from django.core.exceptions import ObjectDoesNotExist

from django_extensions.db.fields import CreationDateTimeField, ModificationDateTimeField

class DjangoManager(models.Manager):
    def versions(self):
        return [("django_%s" % (x.version), "Django %s" % (x.version)) for x in self.model.objects.all().order_by("version")]

    def add_object_compatibility(self, object, version):
        try:
            django_object = self.model.objects.get(version = version)
        except ObjectDoesNotExist:
            django_object = None

        content_type_object = ContentType.objects.get_for_model(object.__class__)
        try:
            comp = DjangoCompatibility.objects.get(content_type = content_type_object,
                                                   object_id = object.id,
                                                   django = django_object)
        except ObjectDoesNotExist:
            comp = DjangoCompatibility(content_type = content_type_object, object_id = object.id, django = django_object)
            comp.save()
        return comp

    def versions_for_object(self, object):
        content_type_object = ContentType.objects.get_for_model(object.__class__)
        return ["django_%s" % (x.django.version) for x in DjangoCompatibility.objects.filter(content_type = content_type_object, object_id = object.id)]

class PythonManager(models.Manager):
    def versions(self):
        return [("python_%s" % (x.version), "Python %s" % (x.version)) for x in self.model.objects.all().order_by("version")]

    def add_object_compatibility(self, object, version):
        try:
            python_object = self.model.objects.get(version = version)
        except ObjectDoesNotExist:
            python_object = None

        content_type_object = ContentType.objects.get_for_model(object.__class__)
        try:
            comp = PythonCompatibility.objects.get(content_type = content_type_object,
                                                   object_id = object.id,
                                                   python = python_object)
        except ObjectDoesNotExist:
            comp = PythonCompatibility(content_type = content_type_object, object_id = object.id, python = python_object)
            comp.save()
        return comp

    def versions_for_object(self, object):
        content_type_object = ContentType.objects.get_for_model(object.__class__)
        return ["python_%s" % (x.python.version) for x in PythonCompatibility.objects.filter(content_type = content_type_object, object_id = object.id)]

class SourceManager(models.Manager):
    def parse_source_data(self, data):
        import re
        final_sources = []
        sources = [x for x in data.split(",") if x.strip()]
        for source in sources:
            uri = source
            final_sources.append(('http', uri))
        return final_sources

    def add_sources(self, object, sources):
        final_sources = []
        for source in sources:
            source_object = self.model.objects.add_source(object, source[0], source[1])
            final_sources.append(source_object)
        return final_sources

    def add_source(self, object, protocol, uri):
        content_type_object = ContentType.objects.get_for_model(object.__class__)
        object_id = object.id
        mode = SourceMode.objects.get(name = protocol)
        source = self.model(active = True,
                            content_type = content_type_object,
                            object_id = object_id,
                            mode = mode,
                            uri = uri)
        source.save()
        return source

class SourceMode(models.Model):
    name = models.CharField(max_length = 75, unique = True)

    def __unicode__(self):
        return self.name

class Source(models.Model):
    active = models.BooleanField(default = True)
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey()
    mode = models.ForeignKey(SourceMode)
    uri = models.TextField()

    date_created = CreationDateTimeField()
    date_modified = ModificationDateTimeField()

    objects = SourceManager()

    def __unicode__(self):
        return "%s::%s" % (self.mode.name, self.uri)

class PythonPackage(models.Model):
    name = models.CharField(max_length = 75)
    version = models.TextField()

class PythonPackageDependency(models.Model):
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    base_object = generic.GenericForeignKey()

    depends_on = models.ForeignKey(PythonPackage)

class Django(models.Model):
    version = models.CharField(max_length = 75, unique = True)

    objects = DjangoManager()

    def __unicode__(self):
        return "Django %s" % (self.version)

class Python(models.Model):
    version = models.CharField(max_length = 75, unique = True)

    objects = PythonManager()

    def __unicode__(self):
        return "Python %s" % (self.version)

class DjangoCompatibility(models.Model):
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    base_object = generic.GenericForeignKey()

    django = models.ForeignKey(Django)

    def name(self):
        return self.django.__unicode__()

class PythonCompatibility(models.Model):
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    base_object = generic.GenericForeignKey()

    python = models.ForeignKey(Python)

    def name(self):
        return self.python.__unicode__()


from typing import Dict

import requests
from requests import Response

BASE_URL = 'https://aw3.automationintesting.online'
MESSAGE_ENDPOINT = f'{BASE_URL}/message'

def create_message(payload: Dict) -> Response:
    response = requests.post(MESSAGE_ENDPOINT, json=payload)
    if not response.ok:
        raise ConnectionError(f'Unable to create message: {response.content}')
    return response


from django import forms

class UserBookForm(forms.Form):
    CHOICES = [
        ('te', 'Temporary Exchange'),
        ('pe', 'Permanent Exchange'),
        ('rt', 'Rental'),
        ('sl', 'Sale')
    ]
    health = forms.DecimalField(label='',
        required = True,
        widget=forms.TextInput(
            attrs={'placeholder':'Health'}
        )
    )

    moe = forms.ChoiceField(required=True,
        choices = CHOICES,
        label = ''
    )

    ebook = forms.FileField(
        label = 'Ebook',
        required = False
    )

    def clean_health(self):
        health = self.cleaned_data.get('health')
        if int(health) >= 0 and int(health) <= 5:
            return health
        raise forms.ValidationError('Health Not In Range')


#!/usr/bin/env python

def str2bool(v):
    import argparse
    if isinstance(v, bool):
        return v
    if v.lower() in ('yes', 'true', 't', 'y', '1'):
        return True
    elif v.lower() in ('no', 'false', 'f', 'n', '0'):
        return False
    else:
        raise argparse.ArgumentTypeError('Boolean value expected.')

def parse_args():
    import argparse

    p = argparse.ArgumentParser(description="Copy files")
    p.add_argument(
        'path', type=str, help='source file path')
    p.add_argument(
        'type', type=str, help='file type to copy')
    p.add_argument(
        'target', type=str, help='path to copy files to')
    p.add_argument(
        'max_size', type=int, help='size in MB to copy')
    p.add_argument(
        'max_files_per_folder', type=int,
        help='max files for per folder for target 0=unlimited')
    p.add_argument(
        '--clear-cache', type=str2bool,
        nargs='?', const=True, default=False,
        help='clear the cache')

    return p.parse_args()

def get_files(file_path, file_type):

    # TODO fix shitty portability
    cf = file_path.replace('/', '_') + '_' + file_type
    cache_file = f'cache/{cf}.pickle'
    files = read_cache(cache_file)

    if files:
        return files

    from pathlib import Path
    file_list = []
    count = 0
    for path in Path(file_path).rglob(f'*.{file_type}'):
        count += 1
        file_list.append((path, path.stat().st_size))
        if count % 100 == 0:
            print(f'Found {count} files')

    # Save cache
    # cache based on source folder
    with open(cache_file, 'wb') as f:
        import pickle
        pickle.dump(file_list, f)
    return file_list

def get_copy_files(files, max_size):
    import random
    print("Randomising...")
    random.shuffle(files)
    print("Randomised.")
    current_size = 0
    file_list = []
    for file in files:
        # Use base 10 for MB
        if current_size + file[1] > max_size*1000*1000:
            break
        file_list.append(file[0])
        current_size += file[1]
    return file_list

def copy_files(files, target_folder, max_files_per_folder):
    import shutil
    import pathlib
    fno = 0
    fc = 0
    for file in files:
        if max_files_per_folder == 0:
            tf = target_folder
        else:
            tf = target_folder + f'/{fno:08d}'
            if fc == 0:
                pathlib.Path(tf).mkdir(parents=True, exist_ok=True)
            tf = tf + f'/{fc:02d}.mp3'
        print(f"copying {file} to {tf}")
        try:
            shutil.copy(str(file), tf)
            fc += 1
            if fc >= max_files_per_folder:
                fno += 1
                fc = 0
        except Exception:
            print(f"ERROR: Failed to copy {file}")

def read_cache(cache_file):
    import pickle

    try:
        with open(cache_file, 'rb') as f:
            cache = pickle.load(f)
    except FileNotFoundError:
        print("Not found")
        cache = None
    return cache

args = parse_args()
if args.clear_cache:
    import pathlib
    # TODO improve shitty portability
    cache_file = args.path.replace('/', '_') + '_' + args.type
    f = pathlib.Path(f'cache/{cache_file}.pickle')
    if f.is_file():
        f.unlink()

file_list = get_files(args.path, args.type)
files = get_copy_files(file_list, args.max_size)

copy_files(files, args.target, args.max_files_per_folder)


# 导入包
import numpy as np
import time

# 打印版本
print(np.__version__)

# numpy与原生Python的性能对比--------------------------------------------------

# 需求：实现两个数组额加法

# 1.原生python
def sum_python(n):
    a = [i ** 2 for i in range(n)]  # 平方
    b = [i ** 3 for i in range(n)]  # 立方
    c = []
    for i in range(n):
        c.append(a[i] + b[i])
        pass
    return c

print(sum_python(10))

# 2.numpy实现
def sum_numpy(n):
    '''
    numpy实现两个数组的加法（对应位上元素相加）
    （python原生语法中两个list相加是把两个list合并成一个大list而不是对应位置元素相加）
    :param n: 数组的长度
    :return: 和
    '''
    a = np.arange(n) ** 2
    b = np.arange(n) ** 3
    return a + b  # 对应位上元素相加

r = sum_numpy(10)
print(r)
print(type(r))

# 运行时间的比较--------------------

# 1.原生python
tps = time.time()
for i in range(10000):
    sum_python(100)
    i += 1
tpe = time.time()
print('python time cost', tpe - tps, 's')

# 2.numpy实现
tns = time.time()
for i in range(10000):
    sum_numpy(100)
    i += 1
tne = time.time()
print('numpy time cost', tne - tns, 's')


from collections.abc import Iterator

def get_diagnostic_code(input_list, list_of_inputs, logging=False):
    if not isinstance(list_of_inputs, Iterator):
        list_of_inputs = iter(list_of_inputs)
    input_list.extend([0] * 10000)
    i = 0
    relative_base = 0

    while True:
        op_code = input_list[i]
        mode_1 = int(op_code / 100) % 10
        mode_2 = int(op_code / 1000) % 10
        mode_3 = int(op_code / 10000) % 10
        op_code = op_code % 100

        if op_code == 99:
            break

        if mode_1 == 1:
            value_1 = i+1
        elif mode_1 == 0:
            value_1 = input_list[i+1]
        elif mode_1 == 2:
            value_1 = input_list[i+1] + relative_base

        if mode_2 == 1:
            value_2 = i+2
        elif mode_2 == 0:
            value_2 = input_list[i+2]
        elif mode_2 == 2:
            value_2 = input_list[i+2] + relative_base

        try:
            if mode_3 == 1:
                value_3 = i+3
            elif mode_3 == 0:
                value_3 = input_list[i+3]
            elif mode_3 == 2:
                value_3 = input_list[i+3] + relative_base
        except Exception:
            pass

        if op_code == 1:
            input_list[value_3] = input_list[value_1] + input_list[value_2]
            i += 4
        elif op_code == 2:
            input_list[value_3] = input_list[value_1] * input_list[value_2]
            i += 4
        elif op_code == 3:
            input_list[value_1] = next(list_of_inputs)
            i += 2
        elif op_code == 4:
            value = input_list[value_1]
            if logging:
                print(value)
            yield value
            i += 2
        elif op_code == 5:
            if input_list[value_1]:
                i = input_list[value_2]
            else:
                i += 3
        elif op_code == 6:
            if input_list[value_1] == 0:
                i = input_list[value_2]
            else:
                i += 3
        elif op_code == 7:
            if input_list[value_1] < input_list[value_2]:
                input_list[value_3] = 1
            else:
                input_list[value_3] = 0
            i += 4
        elif op_code == 8:
            if input_list[value_1] == input_list[value_2]:
                input_list[value_3] = 1
            else:
                input_list[value_3] = 0
            i += 4
        elif op_code == 9:
            relative_base += input_list[value_1]
            i += 2
        else:
            print("INVALID OP CODE: " + str(op_code))

def initalise_game_board(game_runner, board_size):
    global board, score
    for _ in range(board_size + 1):
        x = next(game_runner)
        y = next(game_runner)
        value = next(game_runner)
        if (x, y) == (-1, 0):
            score = value
        else:
            game_board[x, y] = value

def move_joystick():
    global game_board
    ball = [k for k, v in game_board.items() if v == 4][0][0]
    paddle = [k for k, v in game_board.items() if v == 3][0][0]
    if paddle < ball:
        return 1
    elif paddle > ball:
        return -1
    return 0

def update_board(game_runner):
    global score
    value = None
    while value != 4:
        try:
            x = next(game_runner)
        except StopIteration:
            break
        y = next(game_runner)
        value = next(game_runner)
        if (x, y) == (-1, 0):
            score = value
        else:
            game_board[x, y] = value

if __name__ == '__main__':
    # 0 empty tile
    # 1 wall tile
    BLOCK_TILES = 2
    # 3 horizontal paddle tile
    # 4 ball tile

    # Get the input data
    with open('input/day_13.txt') as f:
        file_data = f.read()
    input_list = [int(x) for x in file_data.split(',')]

    # Part 1:
    global game_board
    game_board = {}
    game_runner = get_diagnostic_code(input_list, [])
    for x in game_runner:
        y = next(game_runner)
        value = next(game_runner)
        game_board[x, y] = value
    print('Number of block tiles:', list(game_board.values()).count(BLOCK_TILES))

    # Part 2:
    board_size = len(game_board)
    inputs = []
    game_runner = get_diagnostic_code([2] + input_list[1:], inputs)
    initalise_game_board(game_runner, board_size)
    while True:
        if list(game_board.values()).count(2) == 0:
            break
        inputs.append(move_joystick())
        update_board(game_runner)

    print('Final score:' + str(score))


import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
import matplotlib.patches as patches
import cv2
# write your script here, we recommend the above libraries for making your animation
from LucasKanadeBasis import LucasKanadeBasis
from LucasKanade import LucasKanade

# write your script here, we recommend the above libraries for making your animation
frames = np.load('../data/sylvseq.npy')
bases = np.load('../data/sylvbases.npy')

H, W, T = frames.shape
sylvseqrects = np.zeros((T, 4))

rect1 = np.array([101, 61, 155, 107]).astype('float').T
rect2 = np.array([101, 61, 155, 107]).astype('float').T

fig=plt.figure(figsize=(4, 1))
columns = 5
rows = 1
index = 1

for i in range(1, T):

	p1 = LucasKanadeBasis(frames[:, :, i-1], frames[:, :, i], rect1, bases)
	p2 = LucasKanade(frames[:, :, i-1], frames[:, :, i], rect2)
	rect1 += np.array([p1[1], p1[0], p1[1], p1[0]]).T

	sylvseqrects[i, :] = rect1
	rect2 += np.array([p2[1], p2[0], p2[1], p2[0]]).T

	print('frame: ', i)

	frame = frames[:, :, i].copy()
	cv2.rectangle(frame, (int(rect1[0]),int(rect1[1])), (int(rect1[2]),int(rect1[3])),(255), 1)
	cv2.rectangle(frame, (int(rect2[0]),int(rect2[1])), (int(rect2[2]),int(rect2[3])),(0), 1)

	cv2.imshow('input', frame)

	if i in [1, 200, 300, 350, 400]:

		rect_patch = patches.Rectangle((rect1[0],rect1[1]),
										rect1[2]-rect1[0],
										rect1[3]-rect1[1],
										linewidth=1,edgecolor='y',facecolor='none')

		rect_lk_patch = patches.Rectangle((rect2[0],rect2[1]),
										rect2[2]-rect2[0],
										rect2[3]-rect2[1],
										linewidth=1,edgecolor='g',facecolor='none')

		ax = fig.add_subplot(rows, columns, index)
		ax.add_patch(rect_patch)
		ax.add_patch(rect_lk_patch)

		ax.imshow(frames[:, :, i], cmap='gray')
		ax.get_yaxis().set_visible(False)
		ax.get_xaxis().set_visible(False)
		index+=1

	if cv2.waitKey(10) == ord('q'):
		break
np.save('sylvseqrects.npy', sylvseqrects)
cv2.destroyAllWindows()
plt.show()


import torch,torchvision
import torch.nn as nn
import torch.nn.functional as F
import torchvision.transforms as transforms
from torchvision import models
from torch import optim
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
from PIL import Image
import cv2
import os

device='cuda' if torch.cuda.is_available() else 'cpu'
print(torch.cuda.is_available())

train_transform=transforms.Compose([
transforms.ToTensor(),
    transforms.Resize((224,224)),
    transforms.RandomAffine(degrees=(25)),
    transforms.RandomRotation(25),
    transforms.RandomHorizontalFlip(0.5),
    transforms.RandomVerticalFlip(0.5),
    transforms.Normalize((0.5,0.5,0.5),(1,1,1))
    ])
transform=transforms.Compose([
    transforms.ToTensor(),
    transforms.Resize((224,224)),
    transforms.Normalize((0.5,0.5,0.5),(1,1,1))
])

train_data=torchvision.datasets.ImageFolder('dataset/dataset_/train',transform=train_transform)
test_data=torchvision.datasets.ImageFolder('dataset/dataset_/test',transform=transform)
val_data=torchvision.datasets.ImageFolder('dataset/dataset_/val',transform=transform)

classes=train_data.class_to_idx
print(classes)
print(len(classes))

batch_size=32
epochs=200
num_classes=4
load_model=True

train_loader=torch.utils.data.DataLoader(train_data,batch_size=batch_size,shuffle=True)
test_loader=torch.utils.data.DataLoader(test_data,batch_size=batch_size,shuffle=True)
val_loader=torch.utils.data.DataLoader(val_data,batch_size=batch_size,shuffle=True)

model_ft = models.resnet18(pretrained=True)
num_ftrs = model_ft.fc.in_features
model_ft.fc = nn.Linear(num_ftrs, 4)

model_ft = model_ft.to(device)
model_ft

loss_fn=nn.CrossEntropyLoss()
optimizer=optim.SGD(model_ft.parameters(),lr=0.0001)

path="models"

for epoch in range(epochs):
    for i,(images,labels) in enumerate(train_loader):
        images=images.to(device)
        labels=labels.to(device)
        outputs=model_ft(images)
        loss=loss_fn(outputs,labels)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

    print('Epoch [{}/{}],loss {}'.format(epoch+1,epochs,loss.item()))
    torch.save(model_ft.state_dict(),os.path.join(path,'epoch-{}.pt'.format(epoch)))

with torch.no_grad():
    correct=0
    total=0
    for i,(images,labels) in enumerate(val_loader):
        images=images.to(device)
        labels=labels.to(device)
        outputs=model_ft(images)
        pred=torch.max(outputs,1)[1]
        total+=labels.size(0)
        correct += (pred == labels).sum().item()
        accu=correct/total
    print("Accuracy :%.2f"%accu)


import os
import argparse
from langchain.llms import OpenAI
from langchain import PromptTemplate
from langchain.chains import LLMChain, SequentialChain
import warnings
from datasets import load_dataset
from evaluate import load
import numpy as np
from wasabi import color
import pyutils.io as io
import tqdm.auto as tqdm

def create_math_qa_chain(llm):
    initial_solution_prompt_template = """
    You will be given a math problem with multiple-choice answers. Reason through the problem step-by-step, and finally respond with the letter corresponding to the right answer choice.
    Question:
    {text}
    Options:
    {options}
    """
    initial_solution_prompt = PromptTemplate(
        input_variables=["text", "options"],
        template=initial_solution_prompt_template,
    )
    initial_solution_chain = LLMChain(llm=llm, prompt=initial_solution_prompt, output_key="initial_solution")

    feedback_prompt_template = """
    You will be given a math problem with multiple-choice answers, and a proposed answer and explanation from a student. If answer is already correct, just ouput \"CORRECT\".
    Instruction:
    {text}
    Options:
    {options}
    Student's answer:
    {initial_solution}
    """
    ilf_feedback_prompt = PromptTemplate(
        input_variables=["text", "options", "initial_solution"],
        template=feedback_prompt_template
    )
    feedback_chain = LLMChain(llm=llm, prompt=ilf_feedback_prompt, output_key="feedback")

    refinement_prompt_template = """
    You will be given a math problem with multiple-choice answers, and a proposed answer and explanation from a student. You will also be provided feedback a teacher provided on that initial solution. Based on the feedback, reason through the problem step-by-step, and finally respond with the letter corresponding to the right answer choice.
    Instruction:
    {text}
    Options:
    {options}
    Student's answer:
    {initial_solution}
    Teacher's feedback:
    {feedback}
    """
    ilf_refinement_prompt = PromptTemplate(
        input_variables=["text", "options", "initial_solution", "feedback"],
        template=refinement_prompt_template
    )
    refinement_chain = LLMChain(llm=llm, prompt=ilf_refinement_prompt, output_key="refinement")

    ilf_chain = SequentialChain(
        chains=[initial_solution_chain, feedback_chain, refinement_chain],
        input_variables=["text", "options"],
        output_variables=["initial_solution", "feedback", "refinement"],
    )
    return ilf_chain

def create_mbpp_chain(llm):
    initial_solution_prompt_template = """
    You will be given a Python programming task and one unit test. Write a function that satisfies the specification in task description and passes the unit test. Imporant: Do not include the test case in your solution!
    Instruction:
    {text}
    Unit test:
    {test_list[0]}
    """
    initial_solution_prompt = PromptTemplate(
        input_variables=["text", "test_list"],
        template=initial_solution_prompt_template,
    )
    initial_solution_chain = LLMChain(llm=llm, prompt=initial_solution_prompt, output_key="initial_solution")

    feedback_prompt_template = """
    You will be given a Python programming task, one unit test and a candidate solution. Your job is to provide short feedback on how to improve the candidate solution such that it satisfies the specification in task description and passes the unit test. Be as concise as possible! Do not provide the corrected solution, limit yourself to short feedback in natural language. Focus on correctness, not on following Python style guide or good variable naming. Don't require docstring or test cases. If the solution is already okay, just ouput \"OK\".
    Instruction:
    {text}
    Unit test:
    {test_list[0]}
    Code:
    {initial_solution}
    """
    ilf_feedback_prompt = PromptTemplate(
        input_variables=["text", "test_list", "initial_solution"],
        template=feedback_prompt_template
    )
    feedback_chain = LLMChain(llm=llm, prompt=ilf_feedback_prompt, output_key="feedback")

    refinement_prompt_template = """
    You will be given a Python programming task, one unit test, an initial solution and feedback an expert provided on that initial solution. Your job is to rewrite the initial solution based on the feedback.
    Instruction:
    {text}
    Unit test:
    {test_list[0]}
    Initial solution:
    {initial_solution}
    Feedback:
    {feedback}
    """
    ilf_refinement_prompt = PromptTemplate(
        input_variables=["text", "test_list", "initial_solution", "feedback"],
        template=refinement_prompt_template
    )
    refinement_chain = LLMChain(llm=llm, prompt=ilf_refinement_prompt, output_key="refinement")

    ilf_chain = SequentialChain(
        chains=[initial_solution_chain, feedback_chain, refinement_chain],
        input_variables=["text", "test_list"],
        output_variables=["initial_solution", "feedback", "refinement"],
    )
    return ilf_chain

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--model_name",)
    parser.add_argument("--dataset", choices=["math_qa", "mbpp"])
    parser.add_argument("--output_path")
    parser.add_argument("--phase", choices=["train", "validation"], default="train")
    parser.add_argument("--num_examples", type=int, default=100)
    args = parser.parse_args()
    llm = OpenAI(model_name=args.model_name)
    if args.dataset == "math_qa":
        ilf_chain = create_math_qa_chain(llm)
    elif args.dataset == "mbpp":
        ilf_chain = create_mbpp_chain(llm)
    else:
        raise KeyError(args.dataset)
    os.makedirs(os.path.split(args.output_path)[0], exist_ok=True)
    ds = load_dataset(args.dataset, split=args.phase)
    outputs_list = []
    for i in tqdm.trange(args.num_examples):
        inputs = ds[i]
        if args.dataset == "math_qa":
            outputs = ilf_chain({"text": inputs["Problem"], "options": inputs["options"]})
        elif args.dataset == "mbpp":
            outputs = ilf_chain(inputs)
        else:
            raise KeyError(args.dataset)
        outputs_list.append(outputs)
    io.write_jsonl(outputs_list, args.output_path)

if __name__ == "__main__":
    main()


# -*- coding: utf-8 -*-
"""Config for the Trunk.
"""

__authors__ = "emenager, tnavez"
__contact__ = "etienne.menager@inria.fr, tanguy.navez@inria.fr"
__version__ = "1.0.0"
__copyright__ = "(c) 2020, Inria"
__date__ = "Jun 29 2022"

import sys
import pathlib
sys.path.insert(0, str(pathlib.Path(__file__).parent.absolute())+"/../")
sys.path.insert(0, str(pathlib.Path(__file__).parent.absolute()))

from Libraries.BaseConfig import BaseConfig

import Mesh.Constants as Const

class Config(BaseConfig):
    def __init__(self):
        super(Config,self).__init__("ContactFinger")
        self.scene_name = "3ContactFinger"
        self.nb_robot = 3
        self.set_scene_config({"source": [-400.0, -50, 100],
                               "target": [30, -25, 100],
                               "goalPos": [0., 35.0, -95.0, 0.0, 0.0, 0.0],
                               "contact_radius": 2,
                               "nb_contact_points": 3
})

    def get_actuators_variables(self):
        return {"cable1": [0, 0, 20],
                "cable2": [0, 0, 20]}

    def get_contacts_variables(self):
        x_comp = 30
        y_comp = 10
        z_comp = 20
        return {"contactP1_x": [x_comp, -40, 40],
                "contactP1_y": [y_comp, -20, 40],
                "contactP1_z": [z_comp, -2, 30],
                "contactP2_x": [x_comp, -40, 40],
                "contactP2_y": [y_comp, -20, 40],
                "contactP2_z": [z_comp, -2, 30],
                "contactP3_x": [x_comp, -40, 40],
                "contactP3_y": [y_comp, -20, 40],
                "contactP3_z": [z_comp, -2, 30]}

    def get_n_dt(self):
        return 5

    def get_n_eq_dt(self):
        return 1

    def get_trajectory(self):
        import numpy as np
        goals = []
        circle_center= np.array([0., 35.0, -95.0, 0, 0, 0])
        goals.append(circle_center)

        circle_radius = 1
        # circle_radius = 1.5
        n_samples = 20
        for i in range(n_samples):
            goals.append(circle_center + np.array([circle_radius * np.cos(2 * np.pi * i / n_samples), circle_radius * np.sin(2 * np.pi * i / n_samples),
                 0, 0, 0, 0]))
        return goals


# 효율적인 화폐 구성

# INF 초기화값 : 10000같이 더이상 표기가 안되는 수 -> 여기선 초기화로 사용됨

#정수 N, M을 입력 받기
n, m = map(int, input().split())

array = []
# n개의 화폐 단위 정보를 입력 받기
for i in range(n):
    array.append(int(input()))

# DP 테이블 초기화
d = [10001] * (m + 1)

# 다이나믹 프로그래밍 진행 (바텀업)
d[0] = 0
for i in range(n):
    print("인덱스 번호 :", i)
    for j in range(array[i], m + 1):
        # (i-k)원을 만드는 방법이 존재하는 경우
        if d[j - array[i]] != 10001:
            # 디버그용
            print(d[i], d[j] - array[i] + 1)
            d[j] = min(d[j], d[j - array[i]] + 1)

# 계산된 결과 출력
# 최종적으로 M원을 만드는 방법이 없는 경우
if d[m] == 10001:
    print(-1)
else:
    print("최종 : ", d[m])


import pigpio
import time

class DCMotor:
    FREE = 0x00
    BREAK = 0x01
    FORWARD = 0x02
    BACKWARD = 0x03
    """
    This class encapsulates a DC Motor control with pwm, inA, inB pins.
    """

    def __init__(self, pi, pwm, inA, inB):
        self.pi = pi

        self._pwm_pin = pwm
        self._inA_pin = inA
        self._inB_pin = inB

        self._pwm_pin_mode = self.pi.get_mode(self._pwm_pin)
        self._inA_pin_mode = self.pi.get_mode(self._inA_pin)
        self._inB_pin_mode = self.pi.get_mode(self._inB_pin)

        self.pi.set_mode(self._pwm_pin, pigpio.OUTPUT)
        self.pi.set_mode(self._inA_pin, pigpio.OUTPUT)
        self.pi.set_mode(self._inB_pin, pigpio.OUTPUT)

        self.pi.write(self._inA_pin, 0)
        self.pi.write(self._inB_pin, 0)
        self.pi.hardware_PWM(self._pwm_pin, 20000, 0)

        self._inited = True

    def setDuty(self, duty):
        if duty > 1.0:
            duty = 1.0
        elif duty < -1.0:
            duty = -1.0
        self.pi.hardware_PWM(self._pwm_pin, 20000, int(1e6*duty))

    def setDir(self, dir):
        if dir == self.FREE:
            self.pi.write(self._inA_pin, 0)
            self.pi.write(self._inB_pin, 0)
        elif dir == self.BREAK:
            self.pi.write(self._inA_pin, 1)
            self.pi.write(self._inB_pin, 1)
        elif dir == self.FORWARD:
            self.pi.write(self._inA_pin, 0)
            self.pi.write(self._inB_pin, 1)
        elif dir == self.BACKWARD:
            self.pi.write(self._inA_pin, 1)
            self.pi.write(self._inB_pin, 0)

    def setSpeed(self, speed):
        if speed > 0:
            self.setDir(self.FORWARD)
            self.setDuty(speed)
        elif speed < 0:
            self.setDir(self.BACKWARD)
            self.setDuty(-speed)
        else:
            self.setDir(self.FREE)
            self.setDuty(0)

    def cancel(self):
        """
        Cancels the motor and returns the gpios to their original mode
        """
        if self._inited:
            self._inited = False
            self.setSpeed(0)
            self.pi.set_mode(self._pwm_pin, self._pwm_pin_mode)
            self.pi.set_mode(self._inA_pin, self._inA_pin_mode)
            self.pi.set_mode(self._inB_pin, self._inB_pin_mode)

if __name__ == "__main__":
    pi = pigpio.pi()

    motor = DCMotor(pi, 13, 6, 5)
    for i in range(0, 51, 1):
        print(i)
        motor.setSpeed(i/100)
        time.sleep(0.1)
    motor.setSpeed(0)
    # end_time = time.time() + 60
    # while time.time() < end_time:
    #     time.sleep(0.03)

    motor.cancel()
    pi.stop()


import json, threading
import re, requests
from lxml import etree
from queue import Queue

class DouBan(threading.Thread):
    def __init__(self, q=None):
        super().__init__()
        self.base_url = 'https://movie.douban.com/chart'
        self.headers = {
            'User-Agent':
            'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36',
            'Referer': 'https://movie.douban.com/explore'
        }
        self.q = q
        self.ajax_url = 'https://movie.douban.com/j/chart/top_list?type={}&interval_id=100%3A90&action=&start={}&limit=20'

    # 获取网页的源码
    def get_content(self, url, headers):
        response = requests.get(url, headers=headers)
        return response.text

    # 获取电影指定信息
    def get_movie_info(self, text):
        text = json.loads(text)
        item = {}
        for data in text:
            score = data['score']
            image = data['cover_url']
            title = data['title']
            actors = data['actors']
            detail_url = data['url']
            vote_count = data['vote_count']
            types = data['types']
            item['评分'] = score
            item['图片'] = image
            item['电影名'] = title
            item['演员'] = actors
            item['详情页链接'] = detail_url
            item['评价数'] = vote_count
            item['电影类别'] = types
            print(item)

    # 获取电影api数据的
    def get_movie(self):
        headers = {
            'X-Requested-With':
            'XMLHttpRequest',
            'User-Agent':
            'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36',
        }

        # 获取api数据，并判断分页
        while True:
            if self.q.empty():
                break
            n = 0
            while True:
                text = self.get_content(self.ajax_url.format(self.q.get(), n),
                                        headers=headers)
                if text == '[]':
                    break
                self.get_movie_info(text)
                n += 20

    # 获取所有类型的type——id
    def get_types(self):
        html_str = self.get_content(self.base_url,
                                    headers=self.headers)  # 分类页首页
        html = etree.HTML(html_str)
        types = html.xpath(
            '//div[@class="types"]/span/a/@href')  # 获得每个分类的连接，但是切割type
        # print(types)
        type_list = []
        for i in types:
            p = re.compile('type=(.*?)&interval_id=')  # 筛选id，拼接到api接口的路由
            type = p.search(i).group(1)
            type_list.append(type)
        return type_list

    def run(self):
        self.get_movie()

if __name__ == '__main__':
    # 创建消息队列
    q = Queue()
    # 将任务队列初始化，将我们的type放到消息队列中
    t = DouBan()
    types = t.get_types()
    for tp in types:
        q.put(tp[0])
    # 创建一个列表，列表的数量就是开启线程的树木
    crawl_list = [1, 2, 3, 4]
    for crawl in crawl_list:
        # 实例化对象
        movie = DouBan(q=q)
        movie.start()


print('Задача 7. Отрезок')

# Напишите программу,
# которая считывает с клавиатуры два числа a и b,
# считает и выводит на консоль
#среднее арифметическое всех чисел из отрезка [a; b], которые кратны числу 3.

a = int(input("Число a: "))
b = int(input("число b: "))
c = 0 #счетчик
c1 = 0 #сумму
for n in range(a,b,1):
    if n % 3 == 0:
            c += 1
            c1 += n
c = c1 / c
print("Среднее арифметическое:", c)


class Solution:
    def coinChange(self, coins: List[int], amount: int) -> int:
        ans = [float("inf")] * (amount + 1)
        ans[0] = 0

        for c in coins:
            for amt in range(amount + 1):
                # print(c, amt)
                if c <= amt:
                    ans[amt] = min(ans[amt - c] + 1, ans[amt])

        # print(ans)
        return ans[-1] if ans[-1] != float("inf") else -1


import json
from functools import partial

from pytest import fixture, mark
from traitlets.config import Config

from ..generic import GenericOAuthenticator
from .mocks import setup_oauth_mock

def user_model(username, **kwargs):
    """Return a user model"""
    return {
        "username": username,
        "scope": "basic",
        "groups": ["group1"],
        **kwargs,
    }

@fixture
def generic_client(client):
    setup_oauth_mock(
        client,
        host='generic.horse',
        access_token_path='/oauth/access_token',
        user_path='/oauth/userinfo',
    )
    return client

def _get_authenticator(**kwargs):
    return GenericOAuthenticator(
        token_url='https://generic.horse/oauth/access_token',
        userdata_url='https://generic.horse/oauth/userinfo',
        **kwargs,
    )

@fixture
def get_authenticator(generic_client):
    """
    http_client can't be configured, only passed as argument to the constructor.
    """
    return partial(_get_authenticator, http_client=generic_client)

@mark.parametrize(
    "test_variation_id,class_config,expect_allowed,expect_admin",
    [
        # no allow config tested
        ("00", {}, False, None),
        # allow config, individually tested
        ("01", {"allow_all": True}, True, None),
        ("02", {"allowed_users": {"user1"}}, True, None),
        ("03", {"allowed_users": {"not-test-user"}}, False, None),
        ("04", {"admin_users": {"user1"}}, True, True),
        ("05", {"admin_users": {"not-test-user"}}, False, None),
        ("06", {"allowed_groups": {"group1"}}, True, None),
        ("07", {"allowed_groups": {"test-user-not-in-group"}}, False, None),
        ("08", {"admin_groups": {"group1"}}, True, True),
        ("09", {"admin_groups": {"test-user-not-in-group"}}, False, False),
        # allow config, some combinations of two tested
        (
            "10",
            {
                "allow_all": False,
                "allowed_users": {"not-test-user"},
            },
            False,
            None,
        ),
        (
            "11",
            {
                "allowed_users": {"not-test-user"},
                "admin_users": {"user1"},
            },
            True,
            True,
        ),
        (
            "12",
            {
                "allowed_groups": {"group1"},
                "admin_groups": {"group1"},
            },
            True,
            True,
        ),
        (
            "13",
            {
                "allowed_groups": {"group1"},
                "admin_groups": {"test-user-not-in-group"},
            },
            True,
            False,
        ),
        (
            "14",
            {
                "allowed_groups": {"test-user-not-in-group"},
                "admin_groups": {"group1"},
            },
            True,
            True,
        ),
        (
            "15",
            {
                "allowed_groups": {"test-user-not-in-group"},
                "admin_groups": {"test-user-not-in-group"},
            },
            False,
            False,
        ),
        (
            "16",
            {
                "admin_users": {"user1"},
                "admin_groups": {"group1"},
            },
            True,
            True,
        ),
        (
            "17",
            {
                "admin_users": {"user1"},
                "admin_groups": {"test-user-not-in-group"},
            },
            True,
            True,
        ),
        (
            "18",
            {
                "admin_users": {"not-test-user"},
                "admin_groups": {"group1"},
            },
            True,
            True,
        ),
        (
            "19",
            {
                "admin_users": {"not-test-user"},
                "admin_groups": {"test-user-not-in-group"},
            },
            False,
            False,
        ),
    ],
)
async def test_generic(
    get_authenticator,
    generic_client,
    test_variation_id,
    class_config,
    expect_allowed,
    expect_admin,
):
    print(f"Running test variation id {test_variation_id}")
    c = Config()
    c.GenericOAuthenticator = Config(class_config)
    c.GenericOAuthenticator.username_claim = "username"
    authenticator = get_authenticator(config=c)

    handled_user_model = user_model("user1")
    handler = generic_client.handler_for_user(handled_user_model)
    auth_model = await authenticator.get_authenticated_user(handler, None)

    if expect_allowed:
        assert auth_model
        assert set(auth_model) == {"name", "admin", "auth_state"}
        assert auth_model["admin"] == expect_admin
        auth_state = auth_model["auth_state"]
        assert json.dumps(auth_state)
        assert "access_token" in auth_state
        assert "oauth_user" in auth_state
        assert "refresh_token" in auth_state
        assert "scope" in auth_state
        user_info = auth_state[authenticator.user_auth_state_key]
        assert auth_model["name"] == user_info[authenticator.username_claim]
    else:
        assert auth_model == None

async def test_generic_data(get_authenticator, generic_client):
    c = Config()
    c.GenericOAuthenticator.allow_all = True
    authenticator = get_authenticator()

    handled_user_model = user_model("user1")
    handler = generic_client.handler_for_user(handled_user_model)
    data = {"testing": "data"}
    auth_model = await authenticator.authenticate(handler, data)

    assert auth_model

async def test_generic_callable_username_key(get_authenticator, generic_client):
    c = Config()
    c.GenericOAuthenticator.allow_all = True
    c.GenericOAuthenticator.username_key = lambda r: r["alternate_username"]
    authenticator = get_authenticator(config=c)

    handled_user_model = user_model("user1", alternate_username="zoe")
    handler = generic_client.handler_for_user(handled_user_model)
    auth_model = await authenticator.get_authenticated_user(handler, None)

    assert auth_model["name"] == "zoe"

async def test_generic_claim_groups_key_callable(get_authenticator, generic_client):
    c = Config()
    c.GenericOAuthenticator.claim_groups_key = lambda r: r["policies"]["roles"]
    c.GenericOAuthenticator.allowed_groups = ["super_user"]
    authenticator = get_authenticator(config=c)

    handled_user_model = user_model("user1", policies={"roles": ["super_user"]})
    handler = generic_client.handler_for_user(handled_user_model)
    auth_model = await authenticator.get_authenticated_user(handler, None)

    assert auth_model

async def test_generic_claim_groups_key_nested_strings(
    get_authenticator, generic_client
):
    c = Config()
    c.GenericOAuthenticator.claim_groups_key = "permissions.groups"
    c.GenericOAuthenticator.admin_groups = ["super_user"]
    authenticator = get_authenticator(config=c)

    handled_user_model = user_model("user1", permissions={"groups": ["super_user"]})
    handler = generic_client.handler_for_user(handled_user_model)
    auth_model = await authenticator.get_authenticated_user(handler, None)

    assert auth_model
    assert auth_model["admin"]

@mark.parametrize(
    "name, allowed",
    [
        ("allowed", True),
        ("notallowed", False),
    ],
)
async def test_check_allowed_no_auth_state(get_authenticator, name, allowed):
    authenticator = get_authenticator(allowed_users={"allowed"})
    # allow check always gets called with no auth model during Hub startup
    # these are previously-allowed users who should pass until subsequent
    # this check is removed in JupyterHub 5
    assert await authenticator.check_allowed(name, None)


from django.shortcuts import render
from .models import Articles
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
from django.template.context_processors import csrf

def articles(request):
    articles_list = Articles.objects.all().order_by("-date")
    paginator = Paginator(articles_list,5)
    page = request.GET.get('page',1)
    try:
        articles = paginator.page(page)
    except PageNotAnInteger:
        articles = paginator.page(1)
    except EmptyPage:
        articles = paginator.page(paginator.num_pages)
    return render(request, 'news/posts.html', {'articles': articles})

def article(request, article_id=1):
    args = {}
    args.update(csrf(request))
    args['article'] = Articles.objects.get(id=article_id)
    return render(request, 'news/post.html', args)


#-*- encoding: UTF-8 -*-
from django import forms
from AlyMoly.mantenedor.models import Bodega
from AlyMoly.movimiento.models import ProductoBodega, Ingreso, Producto, Egreso, Traspaso
from AlyMoly.utils import widgets

class IngresoMercaderiaForm(forms.ModelForm):

    def __init__(self,*args,**kwargs):
        super(IngresoMercaderiaForm,self).__init__(*args,**kwargs)
        try:
            self.initial['bodega'] = unicode(kwargs['instance'].existencia.bodega.id)
            self.initial['existencia'] = unicode(kwargs['instance'].existencia.producto.codigo_barra)
        except KeyError:
            #No existe la instancia en un formulario de no-edición
            pass

    bodega = forms.ModelChoiceField(
                               required=True,
                               label=u"Bodega",
                               queryset=Bodega.objects.all(),
                               widget=forms.Select(attrs={'style':'width:230px'}),
                               help_text=u"Seleccione la Bodega"
                               )

    existencia = forms.CharField(label=u"Producto",
                                 widget=widgets.NombreProductoWidget())

    def clean(self):
        cleaned_data = self.cleaned_data
        bodega = cleaned_data.get("bodega")
        producto = cleaned_data.get("existencia")

        if bodega and producto:
            cleaned_data["existencia"] = ProductoBodega.objects.get(bodega=bodega,producto=producto)

        return cleaned_data

    def clean_existencia(self):
        "Verifica que exista el codigo de barra de existencia"
        data = self.cleaned_data["existencia"]
        try:
            producto = Producto.objects.get(codigo_barra=data)
            return producto
        except:
            raise forms.ValidationError(u"No existe el código de barra ingresado.")

    class Meta:
        model = Ingreso
        fields = '__all__'

class EgresoMercaderiaForm(forms.ModelForm):

    def __init__(self,*args,**kwargs):
        super(EgresoMercaderiaForm,self).__init__(*args,**kwargs)
        try:
            self.initial['bodega'] = unicode(kwargs['instance'].existencia.bodega.id)
            self.initial['existencia'] = unicode(kwargs['instance'].existencia.producto.codigo_barra)
        except KeyError:
            #No existe la instancia en un formulario de no-edición
            pass

    bodega = forms.ModelChoiceField(
                               required=True,
                               label=u"Bodega",
                               queryset=Bodega.objects.all(),
                               widget=forms.Select(attrs={'style':'width:230px'}),
                               help_text=u"Seleccione la Bodega"
                               )

    existencia = forms.CharField(label=u"Producto",
                                 widget=widgets.NombreProductoWidget())

    def clean_existencia(self):
        "Verifica que exista el codigo de barra de existencia"
        data = self.cleaned_data["existencia"]
        try:
            producto = Producto.objects.get(codigo_barra=data)
            return producto
        except:
            raise forms.ValidationError(u"No existe el código de barra ingresado.")

    def clean(self):
        """ Valida lo siguiente:
        - Evita descontar mas existencias de la que existen en bodega
        - Soporta el egreso de productos compuestos
        """
        cleaned_data = self.cleaned_data
        bodega = cleaned_data.get("bodega")
        producto = cleaned_data.get("existencia")
        cantidad = cleaned_data.get("cantidad")
        if bodega and producto and cantidad:
            cleaned_data["existencia"] = ProductoBodega.objects.get(bodega=bodega,producto=producto)
            registro = None
            try:
                registro = ProductoBodega.objects.get(bodega=bodega,producto__compone=producto)
                total = registro.existencia - (registro.producto.cantidad_compone * cantidad)
            except ProductoBodega.DoesNotExist:
                registro = ProductoBodega.objects.get(bodega=bodega,producto=producto)
                total = registro.existencia - cantidad
            if total < 0 and not self.instance.id:
                del cleaned_data['cantidad']
                raise forms.ValidationError(u"No hay existencias suficientes para realizar el egreso desde la bodega.")

        return cleaned_data

    class Meta:
        model = Egreso
        fields = '__all__'

class TraspasoForm(forms.ModelForm):

    def __init__(self,*args,**kwargs):
        super(TraspasoForm,self).__init__(*args,**kwargs)
        try:
            if kwargs['instance']:
                self.initial['producto'] = unicode(kwargs['instance'].producto.codigo_barra)
        except KeyError:
            #No existe la instancia en un formulario de no-edición
            pass

    producto = forms.CharField(label=u'Producto',
                               widget=widgets.NombreProductoWidget())

    def clean(self):
        """Valida lo siguiente
        -bodegas seleccionadas son distintas
        -bodega de origen tiene existencias suficientes"""
        cleaned_data = self.cleaned_data
        origen = cleaned_data.get("origen")
        destino = cleaned_data.get("destino")
        producto = cleaned_data.get("producto")
        cantidad = cleaned_data.get("cantidad")

        if origen and destino and producto and cantidad:
            if origen == destino:
                raise forms.ValidationError(u"Debe seleccionar bodegas distintas.")
            registro = None
            try:
                registro = ProductoBodega.objects.get(bodega=origen,producto__compone=producto)
                total = registro.existencia - (registro.producto.cantidad_compone * cantidad)
            except ProductoBodega.DoesNotExist:
                registro = ProductoBodega.objects.get(bodega=origen,producto=producto)
                total = registro.existencia - cantidad
            if total < 0 and not self.instance.id:
                raise forms.ValidationError(u"No hay existencias suficientes para traspasar desde la bodega de origen \"%s\"." % origen)

        # Se retorna el diccioario de datos limpios
        return cleaned_data

    def clean_producto(self):
        value = self.cleaned_data['producto']
        try:
            return Producto.objects.get(codigo_barra=value)
        except Producto.DoesNotExist:
            raise forms.ValidationError(u'No existe el producto de código de barra "%s".' % value)

    class Meta:
        model = Traspaso
        fields = '__all__'


# 내가 너무 어렵게 생각했음
# 완전탐색, hash로 풀 수 있음

n, k = map(int, input().split())
a = list(map(int, input().split()))

# n개의 수가 주어졌을 때 k번이상 등장하는 수 중 최대값

a.sort()

s = 0
e = 0
cur_num = a[0]
max_num = -1
for i in range(1, n):
    if cur_num == a[i]:
        if i == n-1:
            e = i
        e += 1
        continue
    else:
        e = i-1

    # print(s, e, e-s+1, cur_num)

    # k개 이상일 때
    if e - s + 1 >= k:
        # 최대값 찾기
        if cur_num > max_num:
            max_num = cur_num

    if i != n-1:
        s = i
        cur_num = a[i]

# print(s, e, e-s+1, cur_num)
if e-s+1 >= k:
    if cur_num > max_num:
        max_num = cur_num

print(max_num)

### hashmap 풀이
n, k = map(int, input().split())
a = list(map(int, input().split()))
freq = {}

for i in range(n):
    freq[a[i]] = freq.get(a[i], 0) + 1

result = -1
for key in freq.keys():
    # print(key, value)
    if freq[key] >= k:
        result = max(result, key)

print(result)

코드트리/모의고사/네카라쿠배1/2_k번이상등장하는최대.py

2_k번이상등장하는최대.py


'''

Problem 290 | Word Pattern
https://leetcode.com/problems/word-pattern/

'''

class Solution:
    def wordPattern(self, pattern: str, s: str) -> bool:
        lookup = {}
        s = s.split()
        if len(pattern) != len(s) or len(set(pattern)) != len(set(s)): return False
        for i in range(len(pattern)):
            if pattern[i] not in lookup.keys():
                lookup[pattern[i]] = s[i]
            elif lookup[pattern[i]] != s[i]:
                return False
        return True

Data Structures/Hash Tables/Word Pattern.py


#

# 使用python asyncio编写tcp服务端，要求: 监听7000端口, 设置端口可重用, 新建连接的时候打印新连接信息，断开连接的时候也打印断开时的信息，连接设置tcp nodelay选项, 使用包头为: 4字节做包体长度, 4字节做pack_id, 8字节做user_id。

import asyncio
import struct
import datetime
import yaml
import logging

async def handle_client(reader, writer):
    peername = writer.get_extra_info('peername')
    print(f'New connection from {peername}')
    # socket = writer.get_extra_info('socket')
    # socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
    try:
        while True:
            # 读取包头
            header = await reader.readexactly(16)
            body_length, pack_id, user_id = struct.unpack('!LLQ', header)

            # 读取包体
            body = await reader.readexactly(body_length)

            print(f'Received {len(header) + len(body)} bytes from {peername}: pack_id={pack_id}, user_id={user_id}, body={body}')

            # 回复响应
            response = b'OK'
            response_header = struct.pack('!LLQ', len(response), pack_id, user_id)
            writer.write(response_header + response)
            await writer.drain()
    except asyncio.exceptions.IncompleteReadError:
        pass
    finally:
        # 连接关闭时打印当前时间
        print(f'Connection from {peername} closed at {datetime.datetime.now()}')
        writer.close()

async def main():
    server = await asyncio.start_server(handle_client, '127.0.0.1', 7000, reuse_port=True)

    print('Server started')

    async with server:
        await server.serve_forever()

if __name__ == '__main__':
    asyncio.run(main())


"""Signup processing (Waiting list and payments).

So far only dummy functionality, i.e. if a payment is posted, all courses
are set to accepted.

As soon as we have payment service provider, the definite functionality needs
to be implemented.

TODO: Send notification mails
"""

import json
from functools import wraps
from itertools import chain

from bson import ObjectId
from flask import current_app, abort
from eve.methods.patch import patch_internal

def wrap_response(function):
    """Wrapper to modify payload for successful requests (status 2xx)."""
    @wraps(function)
    def _wrapped(_, response):
        if response.status_code // 100 == 2:
            payload = json.loads(response.get_data(as_text=True))

            if '_items' in payload:
                function(payload['_items'])
            else:
                function([payload])

            response.set_data(json.dumps(payload))
    return _wrapped

@wrap_response
def new_signups(signups):
    """Update the status for all signups to a course."""
    def get_id(course):
        """Return the course id, necessary to cope with embedding.

        If the client requests `course` to be embedded, it will be a dict
        with the id as key. Otherwise `course` will be just the id.
        """
        return course['_id'] if isinstance(course, dict) else course

    # Remove duplicates by using a set
    courses = set(get_id(item['course']) for item in signups)
    # Re-format signups into a dict so we can update them easier later
    signups_by_id = {str(item['_id']): item for item in signups}

    modified = chain.from_iterable(update_signups(course)
                                   for course in courses)

    for _id in modified:
        # Update response payload if needed
        try:
            signups_by_id[_id]['status'] = 'reserved'
        except AttributeError:
            pass  # Not in response, nothing to do

def deleted_signup(signup):
    """Update status of course the signup belonged to."""
    update_signups(signup['course'])

def patched_signup(update, original):
    """Update status of all signups of the original and new course."""
    # Only need to do something if course is changed
    if 'course' in update:
        update_signups(update['course'])
        update_signups(original['course'])

def patched_course(update, original):
    """If the number of spots changed, update signups of course."""
    if 'spots' in update:
        update_signups(original['_id'])

def block_course_deletion(course):
    """If a course has signups, it can't be deleted."""
    count = current_app.data.driver.db['signups'].count({
        'course': course['_id']
    })

    if count > 0:
        abort(409, "Course cannot be deleted as long as it has signups.")

def update_signups(course_id):
    """Update waiting list for a course.

    The course id can be given as string or ObjectId.

    We can assume that the course exists, otherwise Eve stops earlier.

    Return list of ids of all reserved signups.
    """
    course_id = ObjectId(course_id)  # Ensure we are working with ObjectId

    # Determine how many spots the course has
    total_spots = (current_app.data.driver.db['courses']
                   .find_one({'_id': course_id}, projection={'spots': 1})
                   .get('spots', 0))

    # Next, count current signups not on waiting list
    signups = current_app.data.driver.db['signups']
    taken_spots = signups.count({'course': course_id,
                                 'status': {'$ne': 'waiting'}})

    available_spots = total_spots - taken_spots

    if available_spots <= 0:
        return []

    # Finally, get as many signups on the waiting list as spots available
    # sort by _updated, use nethz as tie breaker
    chosen_signups = signups.find({'course': course_id,
                                   'status': 'waiting'},
                                  projection=['_id'],
                                  sort=[('_updated', 1), ('nethz', 1)],
                                  limit=available_spots)

    signup_ids = [item['_id'] for item in chosen_signups]

    signups.update_many({'_id': {'$in': signup_ids}},
                        {'$set': {'status': 'reserved'}})

    return [str(item) for item in signup_ids]

def mark_as_paid(payments):
    """After successful payment, set status to `accepted`."""
    # Check if payments is not a list
    if not isinstance(payments, list):
        payments = [payments]

    for payment in payments:

        for signup in payment['signups']:
            data = {'status': 'accepted'}
            patch_internal('signups',
                           _id=str(signup),
                           payload=data,
                           concurrency_check=False,
                           skip_validation=True)

def mark_as_unpaid(payments):
    """Before a payment is deleted, set status to `reserved`."""
    # Check if payments is not a list
    if not isinstance(payments, list):
        payments = [payments]

    for payment in payments:

        for signup in payment['signups']:
            data = {'status': 'reserved'}
            patch_internal('signups',
                           _id=str(signup),
                           payload=data,
                           concurrency_check=False,
                           skip_validation=True)


from Bandit import *
from pylab import *
import matplotlib.pyplot as plt

class EpsilonGreedy:
    #eps of -1 will use an average reward (1/n) rathr than a constant step size
    def __init__(self, bandit, alpha, eps):
        self.name = "Epsilon Greedy"
        self.bandit = bandit
        self.numberOfArms = len(self.bandit.arms)
        self.numberOfPullsArray = [0]*self.numberOfArms

        self.Q = [0.0]*self.numberOfArms

        self.alpha = alpha
        self.eps = eps

    def learn(self, reward, armIndex):
        #Update the Action values
        self.numberOfPullsArray[armIndex]+=1

        if (self.alpha ==-1):
            stepSize = 1/self.numberOfPullsArray[armIndex]
        else:
            stepSize = self.alpha

        #do the learning
        self.Q[armIndex]+= stepSize*(reward - self.Q[armIndex])

    def policy(self):
        armIndex = 0
        #Decide to explore vs. Exploit
        randomE = random()
        if (randomE < self.eps):
            armIndex = randint(0,self.numberOfArms)
        else:
            #Exploit / Choose the best current action
            armIndex= argmax(self.Q)

        return armIndex

    def reset(self):
        self.Q = [0.0]*self.numberOfArms
        self.numberOfPullsArray = [0]*self.numberOfArms


def number_of_digits(number):
    counter = 0
    while number > 0:
        number //= 10
        counter += 1
    return counter

result = ""

cm = input()
cm = int(cm)

for i in "|...." * cm:
    result += i
result += "|\n0"

for i in range(1, cm + 1):
    result = result + (" " * int(5 - number_of_digits(i))) + str(i)

print(result)


from airflow import DAG
from airflow.operators.dummy_operator import DummyOperator
from airflow.models import Param
from common.operators.gce import (
    StartGCEOperator,
    StopGCEOperator,
    CloneRepositoryGCEOperator,
    SSHGCEOperator,
)
from airflow.providers.google.cloud.operators.bigquery import (
    BigQueryExecuteQueryOperator,
    BigQueryInsertJobOperator,
)
import datetime
from pathlib import Path
from common.config import (
    GCP_PROJECT_ID,
    DAG_FOLDER,
    ENV_SHORT_NAME,
    DATA_GCS_BUCKET_NAME,
    BIGQUERY_TMP_DATASET,
)
from common.utils import get_airflow_schedule

from common.alerts import task_fail_slack_alert

from common import macros

SQL_BASE_PATH = Path("dependencies/export_posthog/sql")

GCE_INSTANCE = f"export-posthog-{ENV_SHORT_NAME}"
BASE_PATH = "data-gcp/jobs/etl_jobs/internal/posthog"
dag_config = {
    "PROJECT_NAME": GCP_PROJECT_ID,
    "ENV_SHORT_NAME": ENV_SHORT_NAME,
}

default_dag_args = {
    "start_date": datetime.datetime(2020, 12, 21),
    "retries": 1,
    "on_failure_callback": task_fail_slack_alert,
    "retry_delay": datetime.timedelta(minutes=5),
    "project_id": GCP_PROJECT_ID,
}
DATE = "{{ ts_nodash }}"

dag_config = {
    "STORAGE_PATH": f"gs://{DATA_GCS_BUCKET_NAME}/posthog_export_{ENV_SHORT_NAME}/export_posthog_{DATE}",
    "BASE_DIR": "data-gcp/jobs/etl_jobs/internal/export_posthog/",
}

gce_params = {
    "instance_name": f"export-posthog-data-{ENV_SHORT_NAME}",
    "instance_type": "n1-standard-16",
}

schedule_dict = {"prod": "0 8 * * *", "dev": "0 12 * * *", "stg": "0 10 * * *"}

with DAG(
    "export_posthog",
    default_args=default_dag_args,
    description="Export to analytics data posthog",
    schedule_interval=get_airflow_schedule(schedule_dict[ENV_SHORT_NAME]),
    catchup=False,
    dagrun_timeout=datetime.timedelta(minutes=1440),
    user_defined_macros=macros.default,
    template_searchpath=DAG_FOLDER,
    params={
        "branch": Param(
            default="production" if ENV_SHORT_NAME == "prod" else "master",
            type="string",
        ),
        "instance_type": Param(
            default=gce_params["instance_type"],
            type="string",
        ),
        "instance_name": Param(
            default=gce_params["instance_name"],
            type="string",
        ),
        "days": Param(
            default=0,
            type="integer",
        ),
    },
) as dag:
    start = DummyOperator(task_id="start", dag=dag)
    export_tasks = []
    for sql_query in ["export_firebase_pro_events", "export_firebase_native_events"]:
        table_id = f"{DATE}_{sql_query}"
        export_task = BigQueryExecuteQueryOperator(
            task_id=f"{sql_query}",
            sql=(SQL_BASE_PATH / f"{sql_query}.sql").as_posix(),
            write_disposition="WRITE_TRUNCATE",
            use_legacy_sql=False,
            destination_dataset_table=f"{BIGQUERY_TMP_DATASET}.{table_id}",
            dag=dag,
        )

        export_bq = BigQueryInsertJobOperator(
            task_id=f"{sql_query}_to_bucket",
            configuration={
                "extract": {
                    "sourceTable": {
                        "projectId": GCP_PROJECT_ID,
                        "datasetId": BIGQUERY_TMP_DATASET,
                        "tableId": table_id,
                    },
                    "compression": None,
                    "destinationUris": f"{dag_config['STORAGE_PATH']}/{table_id}/data-*.parquet",
                    "destinationFormat": "PARQUET",
                }
            },
            dag=dag,
        )
        export_task.set_upstream(start)
        export_bq.set_upstream(export_task)

        export_tasks.append(export_bq)

    gce_instance_start = StartGCEOperator(
        task_id="gce_start_task",
        preemptible=False,
        instance_name="{{ params.instance_name }}",
        instance_type="{{ params.instance_type }}",
        retries=2,
        labels={"job_type": "long_task"},
    )

    fetch_code = CloneRepositoryGCEOperator(
        task_id="fetch_code",
        instance_name="{{ params.instance_name }}",
        python_version="3.10",
        command="{{ params.branch }}",
        retries=2,
    )

    install_dependencies = SSHGCEOperator(
        task_id="install_dependencies",
        instance_name="{{ params.instance_name }}",
        base_dir=dag_config["BASE_DIR"],
        command="pip install -r requirements.txt --user",
        dag=dag,
    )

    pro_storage_path = (
        f"{dag_config['STORAGE_PATH']}/{DATE}_export_firebase_pro_events/"
    )
    native_storage_path = (
        f"{dag_config['STORAGE_PATH']}/{DATE}_export_firebase_native_events/"
    )

    firebase_pro_events_export = SSHGCEOperator(
        task_id="firebase_pro_events_export",
        instance_name="{{ params.instance_name }}",
        base_dir=dag_config["BASE_DIR"],
        command="python main.py " f"--source-gs-path {pro_storage_path}",
        dag=dag,
    )

    native_events_export = SSHGCEOperator(
        task_id="native_events_export",
        instance_name="{{ params.instance_name }}",
        base_dir=dag_config["BASE_DIR"],
        command="python main.py " f"--source-gs-path {native_storage_path}",
        dag=dag,
    )

    gce_instance_stop = StopGCEOperator(
        task_id="gce_stop_task", instance_name="{{ params.instance_name }}"
    )

    (
        export_tasks
        >> gce_instance_start
        >> fetch_code
        >> install_dependencies
        >> firebase_pro_events_export
        >> native_events_export
        >> gce_instance_stop
    )


#!/usr/bin/env python

import sys

class Display(object):
    def __init__(self):
        self._clock = 0
        self._x = 1
        self._x_hist = {}
        self._record()

    def _record(self):
        self._x_hist[self._clock] =  self._x

    def x_at_clock(self, clock):
        return self._x_hist[clock]

    def noop(self):
        self._clock += 1
        self._record()

    def add_x(self, num):
        self._clock += 1
        self._record()
        self._clock += 1
        self._record()
        self._x += num

def main():
    filename = sys.argv[1]
    with open(filename) as f:
        lines = f.readlines()

    d = Display()

    for l in lines:
        l = l.strip()
        if l == 'noop':
            d.noop()
        else:
            (inst, op) = l.split()
            d.add_x(int(op))

    clock = 0
    for l in range(6):
        line = ''
        for c in range(40):
            x = d.x_at_clock(c+1+(40*l))
            spr = [x-1, x, x+1]
            if c in spr:
                line += '#'
            else:
                line += '.'
        print(line)

if __name__ == '__main__':
    main()


from collections import defaultdict

FILE_NAME = "input16.in"

fields = defaultdict(list)
your_ticket = []
other_tickets = defaultdict(list)

with open(FILE_NAME, 'r') as file:
    fieldos = True
    your = False
    other = False
    other_index = 0
    for line in file:
        if line == "\n":
            continue
        if "your" in line:
            fieldos = False
            your = True
            continue
        if "nearby" in line:
            other = True
            your = False
            continue
        if fieldos:
            value = line.strip().split(": ")
            key, ranges = value
            first_range, second_range = ranges.split(" or ")
            fields[key].append(first_range)
            fields[key].append(second_range)
        else:
            value = line.strip().split(",")
            if your:
                your_ticket = list(map(int, value))
            if other:
                other_tickets[other_index] = list(map(int, value))
                other_index += 1

invalids = 0
new_others = defaultdict(list)
bad = False

for key, value in other_tickets.items():   #PART 1
    bad = False
    for val in value:
        some_good = False
        for _, conditions in fields.items():
            if some_good:
                break
            for cond in conditions:
                lower, upper = map(int, cond.split("-"))
                if lower <= val <= upper:
                    some_good = True
                    break
        if not some_good:
            invalids += val
            bad = True
    if not bad:
        new_others[key] = value

print(invalids)

fields_options = [set() for _ in range(len(fields))]   #PART 2
help_index = -1
for key, value in new_others.items():    #create  array, each row index represents sets of possibilities of fields for ticket index (1-20)
    help_index += 1
    for index, val in enumerate(value):
        possible_conds = set()
        for condition, ranges in fields.items():
            good = False
            for rango in ranges:
                lower, upper = map(int, rango.split("-"))
                if lower <= val <= upper:
                    good = True
            if good:
                possible_conds.add(condition)
        if help_index == 0:
            fields_options[index] = possible_conds
        else:
            fields_options[index] &= possible_conds

real_positions = [None for _ in range(len(fields))]

while None in real_positions:    #determine position of each field
    to_be_deleted = set()
    for index, conds in enumerate(fields_options):
        if len(conds) == 0:
            continue
        if len(conds) == 1:
            what = next(iter(conds))
            real_positions[index] = what
            to_be_deleted.add(what)
    for val in to_be_deleted:
        for i in range(len(fields_options)):
            if val in fields_options[i]:
                fields_options[i].remove(val)

res = 1
for index, value in enumerate(real_positions):  #get result of 2.part
    if "departure" in value:
        res *= your_ticket[index]
print(res)


import pandas as pd
import astropy as ap
from astropy.table import Table
from astropy.coordinates import SkyCoord
import healpy as hp
import matplotlib as mpl
mpl.use('agg')
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
from matplotlib.patches import Patch
from matplotlib.lines import Line2D
from astropy.time import Time, TimeDelta
import astropy.units as u
mpl.style.use('/home/apizzuto/Nova/scripts/novae_plots.mplstyle')

###############################################################################
#######              Load all novae from Anna's paper            ##############
###############################################################################
def clean_mag_col(p):
    if type(p) == str:
        if '(I)' in p:
            p = p.split(' (I)')[0]
        p = float(p)
    else:
        p = float(p)
    return p

tab = Table.read('/home/apizzuto/Nova/source_list/appendix.tex')
df = tab.to_pandas()
coords = SkyCoord(frame="galactic", l=df['$l$']*u.degree, b=df['$b$']*u.degree)
equatorial = coords.icrs
df['ra'] = equatorial.ra.deg
df['dec'] = equatorial.dec.deg
df['gamma'] = [~np.char.startswith(fl, '$<$') for fl in df['Flux']]
df = df.replace(['-'], np.nan)
df[u'$t_2$'] = df[u'$t_2$'].astype(float)
df['Peak Time'] = [Time(t, format='iso') for t in df['Peak Time']]
df['Peak'] = df['Peak'].apply(clean_mag_col)

###############################################################################
#######          Load gamma novae from the list we've made       ##############
###############################################################################
gamma_df = pd.read_csv('/home/apizzuto/Nova/source_list/gamma_ray_novae.csv')
gamma_coords = SkyCoord(gamma_df['RA'], gamma_df['Dec'], unit='deg')

def mjd_to_time(date):
    return Time(date, format='mjd')

gamma_df['Start Time'] = gamma_df['Start Time'].apply(mjd_to_time)
gamma_df['Stop Time'] = gamma_df['Stop Time'].apply(mjd_to_time)

###############################################################################
#######              Load novae from master galnovae list        ##############
###############################################################################
def correct_date(nov_date):
    try:
        year, month, day = nov_date.split(' ')
        try:
            day, frac_day = day.split('.')
        except:
            day = day
            frac_day = '0'
        new_time = Time(f"{year}-{month}-{day} 00:00:00", format='iso') + TimeDelta(float(f"0.{frac_day}")*86400., format='sec')
    except:
        new_time = Time(f"1800-01-01 00:00:00", format='iso')
    return new_time

def format_coords(ras, decs):
    coord_str = [f"{ra} {dec.replace('<', '')}" for ra, dec in zip(ras, decs)]
    coords = SkyCoord(coord_str, unit=(u.hourangle, u.deg))
    new_ras = coords.ra
    new_decs = coords.dec
    return new_ras, new_decs

def input_name(variable, obscure):
    return np.where(variable == '', obscure, variable)

def clean_mag(mag):
    if mag == '':
        return None
    else:
        for substr in ['<', 'J', 'g', 'V', 'I', 'v', ':', 'R', 'B', 'p', '?', 'r']:
            mag = mag.replace(substr, '')
        if mag[-1] == '.':
            mag = mag[:-1]
        mag = float(mag)
        return mag

pivs = [('Name', 1), ('Date', 15), ('Variable', 32), ('RA', 44),
        ('Dec', 60), ('Disc. Mag', 75), ('Max Mag.', 84), ('Min Mag.', 92),
        ('T3', 104), ('Class', 111), ('Obscure xid', 119), ('Discoverer', 141), ('Refs', 182), ('', -1)]
with open('/home/apizzuto/Nova/source_list/galnovae.txt', 'r') as f:
    lines = f.readlines()
    novae = []
    for line in lines[1:-1]:
        novae.append({pivs[ii][0]: line[pivs[ii][1]-1:pivs[ii+1][1]-1].strip() for ii in range(len(pivs)-1)})

novae = pd.DataFrame(novae)

novae['Max Mag.'] = novae['Max Mag.'].apply(clean_mag)
novae['Date'] = novae['Date'].apply(correct_date)
novae = novae.drop(novae[novae['Date'] < Time("2010-01-01 00:00:00", format='iso')].index, inplace = False)
novae = novae.drop(columns=["Discoverer", "Refs", "T3", "Class"], inplace = False)
novae['RA'], novae['Dec'] = format_coords(novae['RA'], novae['Dec'])
novae['Variable'] = input_name(novae['Variable'], novae['Obscure xid'])

###############################################################################
#######             Load novae from Elias              ######
###############################################################################

elias_novae = pd.read_csv('/home/apizzuto/Nova/source_list/nova_stats_from_elias_reformatted.csv')
def clean_time_str(t):
    if not '.' in t:
        tmp = Time(t, scale='utc', format='iso')
    else:
        date, frac = t.split('.')
        frac = '.' + frac
        frac = float(frac)
        mjd_date = Time(date, format='iso', scale='utc')
        tmp = Time(mjd_date + frac, scale='utc', format='mjd')
        tmp.format = 'iso'
    return tmp

elias_novae = elias_novae[['Nova', 'Date of optical peak',
       'RA', 'Dec',
       'Average Flux (10^-7 ph/s/cm^2)',
       'Spectral index',
       'gamma_start', 'gamma_end']]

elias_novae['Date of optical peak'] = elias_novae['Date of optical peak'].apply(clean_time_str)
elias_novae['gamma_start'] = elias_novae['gamma_start'].apply(clean_time_str)
elias_novae['gamma_end'] = elias_novae['gamma_end'].apply(clean_time_str)

###############################################################################
#######   Load novae from x-ray/gamma analysis (used for gamma times)      ####
###############################################################################
xray_tab = Table.read('/home/apizzuto/Nova/source_list/gamma_nova_from_xray_paper.tex').to_pandas()

tstart = []
for t in xray_tab['Time$_{\\rm \\gamma-ray\\ start}$']:
    if 'table' in t:
        t = t.split('\\')[0]
    tmp = Time(t, format='mjd')
    tstart.append(tmp)

tstop = []
for t in xray_tab['Time$_{\\rm \\gamma-ray\\ end}$']:
    if 'table' in t:
        t = t.split('\\')[0]
        t = t.split('--')[0]
    tmp = Time(t, format='mjd')
    tstop.append(tmp)

xray_tab['Time$_{\\rm \\gamma-ray\\ start}$'] = tstart
xray_tab['Time$_{\\rm \\gamma-ray\\ end}$'] = tstop

###############################################################################
#######             Load novae from optical peak time stuff              ######
###############################################################################
optical_info = pd.read_csv('/home/apizzuto/Nova/source_list/nova_optical_peaks.csv')
optical_info['OpticalPeak'] = [Time(t, format='iso') for t in optical_info['OpticalPeak']]

###############################################################################
#######             Keep dictionary of the extra peak mags              ######
###############################################################################
peak_mag = {
    'V3661 Oph': 10.6, 'V3662 Oph': 14.1, 'V659 Sct': 8.3,
    'V1659 Sco': 12.3, 'V1656 Sco': 11.4, 'V1707 Sco': 10.3,
    'V5853 Sgr': 10.7, 'V2860 Ori': 9.4, 'V611 Sct': 13.4,
    'V5856 Sgr': 5.4, 'V670 Ser': 11.8, 'V1660 Sco': 13.0,
    'V549 Vel': 9.3, 'V392 Per': 6.2, 'V555 Nor': 12.43,
    'MGAB-V207': 3.7
}

###############################################################################
#######              Create one master dataframe for it all      ##############
###############################################################################
master_dict = dict(Name = [], Date = [], Peak = [], RA = [], Dec = [], gamma = [],
                  gamma_start = [], gamma_stop = [], gamma_norm = [],
                  gamma_ind = [], gamma_cutoff = [],
                  refs = [], gamma_lim = [])
all_names = set(novae['Variable']) | set(df['Name']) | set(gamma_df['Name'])

for name in all_names:
    print(f"Beginning {name}")
    if name in ["V1404 Cen", "V5854 Sgr", "V1657 Sco", "V3663 Oph",
        "FM Cir", "V3731 Oph", "V3730 Oph", "V2891 Cyg", "V1709 Sco",
        "V5852 Sgr", "V611 Sct"]:
        print(f"Skipping {name}")
        continue
    master_dict['refs'].append('')
    if name in gamma_df['Name'].unique():
        print(f"\t - is gamma nova")
        df_ind = gamma_df[gamma_df['Name'] == name].index.values[0]
        for mkey, key in [('Name', 'Name'), ('RA', 'RA'), ('Dec', 'Dec'),
                         # ('gamma_start', 'Start Time'), ('gamma_stop', 'Stop Time'),
                         ('gamma_norm', 'Flux'), ('gamma_ind', 'Index'),
                         ('gamma_cutoff', 'Cutoff')]:
            master_dict[mkey].append(gamma_df[key][df_ind])
        if name in xray_tab['Nova'].unique():
            print(f"\t - gamma nova {name} included in x-ray analysis,")
            print("\t\t using gamma time from that paper")
            xind = xray_tab[xray_tab['Nova'] == name].index.values[0]
            for mkey, x_key, key in [('gamma_start', 'Time$_{\\rm \\gamma-ray\\ start}$', 'Start Time'),
                ('gamma_stop', 'Time$_{\\rm \\gamma-ray\\ end}$', 'Stop Time')]:
                master_dict[mkey].append(xray_tab[x_key][xind])
            master_dict['refs'][-1] = master_dict['refs'][-1] + 'xray:' + xray_tab['Reference'][xind]
        else:
            print(f"\t - Gamma nova {name} not included in x-ray analysis")
            print("\t\t using gamma time from our own list")
            for mkey, x_key, key in [('gamma_start', 'Time$_{\\rm \\gamma-ray\\ start}$', 'Start Time'),
                ('gamma_stop', 'Time$_{\\rm \\gamma-ray\\ end}$', 'Stop Time')]:
                master_dict[mkey].append(gamma_df[key][df_ind])

        # Peak time
        if name in elias_novae['Nova'].unique():
            el_ind = elias_novae[elias_novae['Nova'] == name].index.values[0]
            print("\t - Gamma nova found in Elias spreadsheet. Using spectroscopic optical peak")
            for mkey, key in [('Date', 'Date of optical peak')]:
                master_dict[mkey].append(elias_novae[key][el_ind])
                print(type(elias_novae[key][el_ind]))
            master_dict['refs'][-1] = master_dict['refs'][-1] + 'Elias'
        elif name in df['Name'].unique():
            df_ind = df[df['Name'] == name].index.values[0]
            print("\t - Gamma nova found in Anna's paper, using peak time from that", name)
            for mkey, key in [('Date', 'Peak Time')]:
                master_dict[mkey].append(df[key][df_ind])
                print(type(df[key][df_ind]))
            master_dict['refs'][-1] = master_dict['refs'][-1] + 'Anna'
        elif name in optical_info['Name'].unique():
            opt_ind = optical_info[optical_info['Name'] == name].index.values[0]
            print(f"\t - Gamma nova {name} found in our list of optical times")
            print("\t\t using time from that list")
            master_dict['Date'].append(optical_info['OpticalPeak'][opt_ind])
            master_dict['refs'][-1] = master_dict['refs'][-1] + 'optical:' + optical_info['Ref'][opt_ind]
        else:
            print("\t - COULD NOT FIND GAMMA NOVA IN EITHER, NO TIME AVAILABLE?", name)
            for mkey, key in [('Date', 'Date')]:
                master_dict[mkey].append(np.nan)

        # Peak magnitude
        if name in df['Name'].unique():
            df_ind = df[df['Name'] == name].index.values[0]
            master_dict['Peak'].append(df['Peak'][df_ind])
            master_dict['refs'][-1] = master_dict['refs'][-1] + 'Anna'
        elif name in novae['Variable'].unique():
            print("\t - Taking peak mag from galnovae file for", name)
            df_ind = novae[novae['Variable'] == name].index.values[0]
            for mkey, key in [('Peak', 'Max Mag.')]:
                master_dict[mkey].append(novae[key][df_ind])
        else:
            if name == 'V3890 Sgr':
                print('\t - Using data from ATels for V3890 Sgr')
                master_dict['Peak'].append(6.7)
                master_dict['refs'][-1] = master_dict['refs'][-1] + 'optical:Atel13047'
            else:
                print(f"\t - NO MAX MAGNITUDE FOR NOVA {name}")
                master_dict['Peak'].append(peak_mag[name])
        master_dict['gamma'].append(True)
        master_dict['gamma_lim'].append(np.nan)

    elif name in df['Name'].unique():
        print(f"\t - Found in Anna's paper, all info from there")
        df_ind = df[df['Name'] == name].index.values[0]
        for mkey, key in [('Name', 'Name'), ('Date', 'Peak Time'), ('Peak', 'Peak'),
                         ('RA', 'ra'), ('Dec', 'dec'), ('gamma', 'gamma')]:
            master_dict[mkey].append(df[key][df_ind])
        for mkey in ['gamma_start', 'gamma_stop', 'gamma_norm', 'gamma_ind', 'gamma_cutoff']:
            master_dict[mkey].append(np.nan)
        master_dict['refs'][-1] = master_dict['refs'][-1] + 'Anna'
        master_dict['gamma_lim'].append(float(df['Flux'][df_ind].split('$')[-1]))

    elif name in novae['Variable'].unique():
        print(f"\t - Found in Galnovae file, getting info from there")
        df_ind = novae[novae['Variable'] == name].index.values[0]
        for mkey, key in [('Name', 'Variable'), ('Peak', 'Max Mag.'),
                         ('RA', 'RA'), ('Dec', 'Dec')]:
            master_dict[mkey].append(novae[key][df_ind])
        if name in optical_info['Name'].unique():
            print(f"\t - Optical peak time from local file")
            opt_ind = optical_info[optical_info['Name'] == name].index.values[0]
            for mkey, opt_key in [('Date', 'OpticalPeak')]:
                master_dict[mkey].append(optical_info[opt_key][opt_ind])
            master_dict['refs'][-1] = master_dict['refs'][-1] + 'optical: ' + optical_info['Ref'][opt_ind]
        else:
            print(f"\t - No info on optical peak for {name}")
            master_dict['Date'].append(np.nan)
        master_dict['gamma'].append(False)
        for mkey in ['gamma_start', 'gamma_stop', 'gamma_norm', 'gamma_ind', 'gamma_cutoff']:
            master_dict[mkey].append(np.nan)
        master_dict['gamma_lim'].append(np.nan)
    else:
        print("why here tho?")

for i, (name, p) in enumerate(zip(master_dict['Name'], master_dict['Peak'])):
    try:
        if np.isnan(p):
            master_dict['Peak'][i] = peak_mag[name]
    except:
        pass

master_df = pd.DataFrame.from_dict(master_dict)

###############################################################################
#######         Cut out novae that occured outside of GRECO      ##############
###############################################################################
min_greco_mjd = np.min(np.load('/data/ana/analyses/greco_online/version-002-p10/IC86_2012.data.npy')['time'])
min_greco_time = Time(min_greco_mjd, format='mjd')

max_greco_mjd = np.max(np.load('/data/ana/analyses/greco_online/version-002-p10/IC86_2019.data.npy')['time'])
# max_greco_mjd = 59000. # End of May 2020 lines up with end of 2019 data
max_greco_time = Time(max_greco_mjd, format='mjd')

master_df = master_df[master_df['Date'] > min_greco_time]
master_df = master_df[master_df['Date'] < max_greco_time]

master_df = master_df.reset_index()

# pd.set_option('display.max_rows', 100)
# pd.options.display.max_colwidth = 100
# print(master_df)
# import sys
# sys.exit()

master_df.to_pickle('/home/apizzuto/Nova/master_nova_dataframe.pkl')

###############################################################################
#######              Plot all of the novae in our dataframe      ##############
###############################################################################
fig = plt.figure(figsize=(8,4), dpi=200, facecolor='w')

gplane = SkyCoord(frame='galactic', b = np.zeros(5000)*u.degree, l = np.linspace(0.0, 360., 5000)*u.degree)
gplane_icrs = gplane.icrs
gcent = SkyCoord(frame='galactic', b = [0.0]*u.degree, l = [0.0]*u.degree)
gcent_icrs = gcent.icrs
cols = [sns.xkcd_rgb['orange pink'] if k is True else sns.xkcd_rgb['light navy blue'] for k in master_df['gamma']]
s = [14 if k is True else 10 for k in master_df['gamma']]

legend_els = [ Line2D([0], [0], marker='o', ls = '', color=sns.xkcd_rgb['orange pink'], label=r'$\gamma$ detected'),
              Line2D([0], [0], marker='o', ls = '', color=sns.xkcd_rgb['light navy blue'], label='Optical only')]

ax = fig.add_subplot(111, projection='mollweide')
ax.grid(True, alpha = 0.35, zorder=1, ls = '--')

equatorial = SkyCoord(ra=master_df['RA']*u.deg, dec=master_df['Dec']*u.deg)
ax.scatter(-1*equatorial.ra.wrap_at('360d').radian + np.pi, equatorial.dec.radian,
           zorder=20, s = s, c = cols)
# ax.scatter(-1*gamma_coords.ra.wrap_at('360d').radian + np.pi, gamma_coords.dec.radian,
#            zorder=20, s = s, c = sns.xkcd_rgb['orange pink'])

ax.scatter(-1.*gplane_icrs.ra.wrap_at('360d').radian + np.pi, gplane_icrs.dec.radian,
           zorder=10, c = 'k', s = 0.5)

ax.set_xticklabels(["{:.0f}".format(v) + r'$^{\circ}$' for v in np.linspace(330., 30., 11)], fontsize = 14)
ax.set_yticklabels(["{:+.0f}".format(v) + r'$^{\circ}$' for v in np.linspace(-75., 75., 11)], fontsize = 14)
plt.text(110.*np.pi / 180., -45 * np.pi / 180, 'Equatorial\n(J2000)')
ax.legend(loc=(0.2, -0.18), handles=legend_els, ncol = 2, frameon=False)
plt.savefig('/home/apizzuto/public_html/novae/skymap_all_novae.png', dpi=200, bbox_inches='tight')

###############################################################################
#######              Make latex table of novae for paper      ##############
###############################################################################
def clean_date(date):
    return date.iso.split(' ')[0]

def clean_ra(ra):
    return f"{ra:.2f}" + "$^{\circ}$"

def clean_dec(dec):
    return f"{dec:+.2f}" + "$^{\circ}$"

def clean_gam(gamma):
    return '\checkmark' if gamma else '--'

novae = pd.read_pickle('/home/apizzuto/Nova/master_nova_dataframe.pkl')
novae = novae.sort_values('Date')
novae = novae[["Name", "Date", "RA", "Dec", "Peak", "gamma", "refs"]]
for col, cleaner in [('Date', clean_date), ('RA', clean_ra), ('Dec', clean_dec),
                    ('gamma', clean_gam)]:
    novae[col] = novae[col].apply(cleaner)

novae = novae.rename(columns={'Date': 'Peak Date', 'RA': '$\\alpha$',
                             'Dec': '$\delta$', 'gamma': '$\gamma$-detected',
                             'refs': 'Reference', 'Peak': "Peak Mag."})

latex_str = novae.to_latex(longtable=True, index_names=False, index=False)
latex_str = latex_str.replace('\\textasciicircum ', '^')
latex_str = latex_str.replace('\$', '$')
latex_str = latex_str.replace('\\textbackslash ', '\\')
latex_str = latex_str.replace('\\{', '{')
latex_str = latex_str.replace('\\}', '}')
with open('./tmp_latex_table_paper.tex', 'w') as fi:
    fi.writelines(latex_str)


from telegram.ext import Updater, MessageHandler, Filters, CommandHandler
import csv
with open("worldcities.csv", newline='', encoding = 'UTF-8') as csvfile:
    data = csv.reader(csvfile, quoting=csv.QUOTE_ALL)

    def start(bot, update):
        update.message.reply_text("Cities game. If you want to stop enter /stop. You start)")

    def stop(bot, update, user_data):
        update.message.reply_text("Game over.")
        user_data.clear()

    def answer(update, user_data): #function to answer on user's city, user_data - dictionary with used cities, last character, number of used cities
        csvfile.seek(0)
        for row in data:
            ans = row[1].upper()
            if (ans[0] == user_data[-2] and (not (ans in user_data.values()))):
                update.message.reply_text(ans)
                user_data[-1] += 1
                user_data[user_data[-1]] = ans
                return ans[-1]#returns last character of answer

        update.message.reply_text("I'm lost")
        exit(0)

    def ansOnCity(bot, update, user_data): #handler function of entered word
        inp = update.message.text
        inp = inp.upper()
        if (not (len(user_data)==0) and inp[0] != user_data[-2]):
            update.message.reply_text("Enter city which begins on " + user_data[-2] + '\n')
        else:
            if (len(user_data) == 0):
                user_data[-1] = 0#number of used cities
                user_data[-2] = 'A'#last character of the last city
            hasans = False
            csvfile.seek(0)
            for row in data:
                ans = row[1].upper()
                if (inp == ans):
                    hasans = True
                    if (not (inp in user_data.values())):
                        user_data[-1] += 1
                        user_data[user_data[-1]] = inp
                        user_data[-2] = inp[-1]
                        user_data[-2] = answer(update, user_data)
                        break
                    else:
                        update.message.reply_text("This city was called")
                        break
            if not (hasans):
                update.message.reply_text("This city does not exist (or Bot doesn't know it)")

    updater = Updater("my token")

    disp = updater.dispatcher

    disp.add_handler(CommandHandler("start", start))
    disp.add_handler(CommandHandler('stop', stop, pass_user_data=True))

    text = MessageHandler(Filters.text, ansOnCity, pass_user_data=True)

    disp.add_handler(text)

    updater.start_polling()

    updater.idle()


# save model not replace

import face_recognition
import cv2
import os
import pickle

print(cv2.__version__)

j=0

Encodings=[]
Names=[]

with open('train.pkl','rb') as f:
    Names=pickle.load(f)
    Encodings=pickle.load(f)

for root,dirs, files in os.walk(image_dir):
    for file in files:
        print(root)
        print(file)
        testImagePath=os.path.join(root,file)
        testImage=face_recognition.load_image_file(testImagePath)
        facePositions=face_recognition.face_locations(testImage)
        allEncodings=face_recognition.face_encodings(testImage,facePositions)
        testImage=cv2.cvtColor(testImage,cv2.COLOR_RGB2BGR)

        for (top,right,bottom,left),face_encoding in zip(facePositions,allEncodings):
            name='Unknown Person'
            matches=face_recognition.compare_faces(Encodings,face_encoding)

            if True in matches:
                first_match_index=matches.index(True)
                name=Names[first_match_index]

            cv2.rectangle(testImage,(left,top),(right,bottom),(0,0,255),2)
            cv2.putText(testImage,name,(left,top-6),font,.75,(0,255,255),2)

        cv2.imshow('Picture', testImage)
        cv2.moveWindow('Picture',0,0)

        if cv2.waitKey(0)==ord('q'):
            cv2.destroyAllWindows()

# เมื่อ run program จะทำการวนซ่้ำรูปภาพที่ recoginize in folder unknow