from pathlib import Path
import numpy as np
import copy
from itertools import permutations
import re

data_folder = Path(__file__).parent.resolve()
reg = re.compile(
    r"(\w+) would (lose|gain) (\d+) happiness units by sitting next to (\w+)."
)

class Table:
    def __init__(self, data):
        happiness = []
        people = set()
        for line in data.split("\n"):
            m = reg.match(line)
            assert m is not None
            new_data = list(m.groups())
            new_data[2] = int(new_data[2])
            if new_data[1] == "lose":
                new_data[2] *= -1
            new_data.pop(1)
            happiness.append(new_data)
            for i in [0, 2]:
                people.add(new_data[i])
        self.n_people = len(people)
        self.people = dict(zip(people, np.arange(self.n_people)))
        self.happiness = self.compute_happiness_matrix(happiness)

    def compute_happiness_matrix(self, happiness):
        n = self.n_people
        happiness_matrix = np.zeros((n, n), dtype=int)
        for h in happiness:
            i = self.people[h[0]]
            j = self.people[h[2]]
            happiness_matrix[i, j] = h[1]
        return happiness_matrix

    def find_max_happiness(self,include_self=False):
        n = self.n_people
        if include_self:
            n += 1
        s = np.arange(n)
        max_happiness = 0
        for order in permutations(s):
            happiness = 0
            for j in range(n):
                i = (j + 1) % n

                if (order[i]==self.n_people) or (order[j]==self.n_people):
                    continue

                happiness += self.happiness[order[j], order[i]]
                happiness += self.happiness[order[i], order[j]]

            if happiness > max_happiness:
                max_happiness = happiness

        return max_happiness

def main():
    data_folder = Path(".").resolve()
    data = data_folder.joinpath("input.txt").read_text()
    t = Table(data)

    print("Part 1")
    print(
        "The total change in happiness for the optimal "
        + f"seating arrangement is {t.find_max_happiness()}"
    )
    print()

    print("Part 2")
    print(
        "The total change in happiness for the optimal "
        + f"seating arrangement including you is {t.find_max_happiness(True)}"
    )

if __name__ == "__main__":
    main()


import torch
import torch.nn
from collections import OrderedDict

from deep_learning.architectures.Resnet3D.model import get_pretrained_resnet
from deep_learning.architectures.ClinicalNet import ClinicalNet

def load_trained_model(model, weights_path):
    print('loading pretrained model {}'.format(weights_path))
    pretrain = torch.load(weights_path, map_location='cpu')
    new_state_dict = OrderedDict()
    for k, v in pretrain['state_dict'].items():
        name = k[6:] # remove `model.`
        new_state_dict[name] = v

    model.load_state_dict(new_state_dict)
    return model

class Ensemble(torch.nn.Module):
    def __init__(self, indices: list, fold: int, combined_clinical=False):
        super().__init__()
        self.imaging_models = torch.nn.ModuleList()
        self.clinical_model = None
        if combined_clinical:
            self.clinical_model = ClinicalNet()
        # ["T1_COR_agent_None", "T1_FS_COR_agent_GD", "T2_FS_COR_agent_None", "T1_FS_AX_agent_GD", "T2_FS_AX_agent_None"]
        # Fold 0
        if fold == 0:
             trained_paths = [
                 # Specify path to weights
                 "/path/to/weights/T1_COR_agent_None.ckpt",
                 "/path/to/weights/T1_FS_COR_agent_GD.ckpt",
                 "/path/to/weights/T2_FS_COR_agent_None.ckpt",
                 "/path/to/weights/T1_FS_AX_agent_GD.ckpt",
                 "/path/to/weights/T2_FS_AX_agent_None.ckpt",
                 # Add path to trained clinical model...
            ]
        # Fold 1
        if fold == 1:
             trained_paths = [
                 # Specify path to weights
                 "/path/to/weights/T1_COR_agent_None.ckpt",
                 "/path/to/weights/T1_FS_COR_agent_GD.ckpt",
                 "/path/to/weights/T2_FS_COR_agent_None.ckpt",
                 "/path/to/weights/T1_FS_AX_agent_GD.ckpt",
                 "/path/to/weights/T2_FS_AX_agent_None.ckpt",
            ]
        # Fold 2
        if fold == 2:
             trained_paths = [
                 # Specify path to weights
                 "/path/to/weights/T1_COR_agent_None.ckpt",
                 "/path/to/weights/T1_FS_COR_agent_GD.ckpt",
                 "/path/to/weights/T2_FS_COR_agent_None.ckpt",
                 "/path/to/weights/T1_FS_AX_agent_GD.ckpt",
                 "/path/to/weights/T2_FS_AX_agent_None.ckpt",
            ]
        # Fold 3
        if fold == 3:
             trained_paths = [
                 # Specify path to weights
                 "/path/to/weights/T1_COR_agent_None.ckpt",
                 "/path/to/weights/T1_FS_COR_agent_GD.ckpt",
                 "/path/to/weights/T2_FS_COR_agent_None.ckpt",
                 "/path/to/weights/T1_FS_AX_agent_GD.ckpt",
                 "/path/to/weights/T2_FS_AX_agent_None.ckpt",
            ]
        # Fold 4
        if fold == 4:
             trained_paths = [
                 # Specify path to weights
                 "/path/to/weights/T1_COR_agent_None.ckpt",
                 "/path/to/weights/T1_FS_COR_agent_GD.ckpt",
                 "/path/to/weights/T2_FS_COR_agent_None.ckpt",
                 "/path/to/weights/T1_FS_AX_agent_GD.ckpt",
                 "/path/to/weights/T2_FS_AX_agent_None.ckpt",
            ]
        trained_paths = [path for i, path in enumerate(trained_paths) if i in indices]
        for path in trained_paths:
            model = get_pretrained_resnet(34)
            model = load_trained_model(model, path)
            # Freeze all models
            for parameter in model.parameters():
                parameter.requires_grad = False
            self.imaging_models.append(model)

    def forward(self, x, clinical_data=None):
        predictions = []
        for x_, model in zip(x, self.imaging_models):
            pred = model(x_)
            predictions.append(pred)
        if self.clinical_model:
            predictions.append(self.clinical_model(clinical_data))
        ensemble_prediction = torch.mean(torch.stack(predictions, dim=0), 0)
        return ensemble_prediction


import logging
from smtplib import SMTPException

from django.contrib import messages
from django.core.mail import EmailMultiAlternatives
from django.http import HttpRequest
from django.template.loader import render_to_string
from django.utils.safestring import mark_safe
from django.utils.translation import gettext_lazy as _

def try_send_email_add_warning_if_failed(
        request: HttpRequest, email: str, subject: str, message: str
):
    html = render_to_string(
        'email/regular_email.html',
        context={
            'header': subject,
            'text':   message
        }
    )
    if not send_html_email(email, subject, html):
        messages.warning(request, _(f'Не вдалося надіслати повідомлення на пошту {email}'))

def send_html_email(email: str, subject: str, html: str) -> bool:
    msg = EmailMultiAlternatives(
        subject=subject,
        body=mark_safe(html),
        # django requires from_email to be explicitly set to None
        # to use settings.DEFAULT_FROM_EMAIL
        from_email=None,
        to=[email]
    )
    msg.content_subtype = 'html'
    try:
        msg.send()
    except SMTPException as e:
        logging.error(
            'failed to send email to %s with error: %s',
            email,  # pylint: disable=C0209
            e
        )
        return False
    return True


# import matplotlib.pyplot as plt
# import numpy as np
#
# # Sample data for clustered columns
# categories = ['Category A', 'Category B', 'Category C']
# values1 = [15, 25, 30]
# values2 = [10, 20, 25]
#
# # Create an array for the x-axis positions
# x = np.arange(len(categories))
#
# # Set the width of each bar
# bar_width = 0.35
#
# # Create the clustered columns
# plt.bar(x - bar_width/2, values1, bar_width, label='Value 1', color='b', align='center')
# plt.bar(x + bar_width/2, values2, bar_width, label='Value 2', color='g', align='center')
#
# # Add labels and legend
# plt.xlabel('Categories')
# plt.ylabel('Values')
# plt.title('Clustered Column Bar Chart')
# plt.xticks(x, categories)
# plt.legend()
#
# # Show the plot
# plt.tight_layout()
# plt.show()

import matplotlib.pyplot as plt
import numpy as np

# Sample data (replace with your actual data)
time = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
memory_overhead = [100, 105, 98, 110, 115, 112, 120, 130, 128, 132]

# Window size for the moving average (adjust as needed)
window_size = 4

# Calculate the moving average
smoothed_memory_overhead = np.convolve(memory_overhead, np.ones(window_size)/window_size, mode='same')

# Create a figure and axis
fig, ax = plt.subplots()

# Plot the original data
ax.plot(time, memory_overhead, label='Memory Overhead', marker='o')

# Plot the smoothed data
ax.plot(time, smoothed_memory_overhead, label='Smoothed Memory Overhead', linestyle='--')

# Set labels and legend
ax.set_xlabel('Time')
ax.set_ylabel('Memory Overhead')
ax.legend()

# Show the plot
plt.show()


import torch
import numpy as np
import pickle
from tqdm import tqdm
import pandas as pd
import math
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, log_loss
from sentence_transformers import SentenceTransformer, models
from transformers import AutoModel, AutoTokenizer
import os

os.environ['CUDA_VISIBLE_DEVICES'] = '6,5'

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
#device = 'cpu'
print("Device", device)

bert_model = SentenceTransformer("/mount/arbeitsdaten43/projekte/thesis-dp-1/banerjak/msmarco-MiniLM-L-6-v3/")
if torch.cuda.is_available():
    bert_model.cuda()

print("Initializing initial query encoder...")
bert_model.max_seq_length = 1000
torch.manual_seed(10)
torch.autograd.set_detect_anomaly(True)

embed_size = 256
init_query_embed_size = 384
num_docs = 20
batch_size = 10
epochs = 20
max_len = 256
epsilon = 0.0001

tokenizer = AutoTokenizer.from_pretrained('google/electra-small-discriminator', truncation = True)

print("Initializing the model...")
class SBERT(torch.nn.Module):
    def __init__(self):
        super().__init__()
        self.roberta = AutoModel.from_pretrained('google/electra-small-discriminator').train()
        self.pooler = torch.nn.AvgPool1d(embed_size)
    def forward(self, ids, attention_masks, token_type_ids):
        embeds = [self.roberta(id, attention_mask, token_type_id).last_hidden_state for (id, attention_mask, token_type_id) in zip(ids, attention_masks, token_type_ids)]
        embeds = [self.pooler(embed) for embed in embeds]
        embeds = [torch.squeeze(embed, dim = 2) for embed in embeds]
        ln = len(embeds)
        embeds = torch.cat(embeds, dim = 0).reshape((ln, num_docs, embed_size))
        return embeds

class GCN(torch.nn.Module):
    def __init__(self, n_h, input_features):
        super().__init__()
        W = torch.FloatTensor(input_features, n_h)
        self.weights = torch.nn.Parameter(W)
        self.relu = torch.nn.ReLU()
        stdv = 1. / math.sqrt(self.weights.size(1))
        self.weights.data.uniform_(-stdv, stdv)
        bs = torch.FloatTensor(n_h)
        self.bias = torch.nn.Parameter(bs)
        self.bias.data.uniform_(-stdv, stdv)

    def forward(self, nfs, A):
        out = torch.matmul(nfs, self.weights)
        out = torch.matmul(A, out)
        out = out + self.bias
        out = self.relu(out)
        return out

class AmbiguityNetwork(torch.nn.Module):
    def __init__(self):
        super().__init__()
        self.sbert = SBERT()
        self.gcn1 = GCN(20, embed_size)
        self.gcn2 = GCN(10, 20)
        self.gcn3 = GCN(10,10)
        self.dp = torch.nn.Dropout(p = 0.5)
        self.dense1 = torch.nn.Linear(in_features = num_docs*10 + init_query_embed_size, out_features = 64)
        self.dense2 = torch.nn.Linear(in_features = 64, out_features = 4)
        self.relu = torch.nn.ReLU()

    def forward(self, ids, attention_masks, token_type_ids, init_query):
        embeds = self.sbert(ids, attention_masks, token_type_ids)
        emb_norm = torch.nn.functional.normalize(embeds, dim = 2)
        emb_sum = torch.sum(torch.square(emb_norm), axis = 2)
        csml = torch.matmul(emb_norm, emb_norm.transpose(1,2))
        A = csml
        print(A[0])
        S = torch.sum(A, axis = 2)
        S = S + epsilon
        S = torch.pow(S, -0.5)
        #D = torch.diag_embed(torch.sum(A, axis = 2))
        #D_inv = torch.linalg.inv(D)
        D_ = torch.diag_embed(S)
        #print("D_", D_)
        #D_ = torch.pow(D_inv, 0.5)
        A_ = torch.matmul(A, torch.transpose(D_, 1,2))
        A_ = torch.matmul(D_, A_)
        A = A_ + torch.eye(embeds.shape[1]).to(device)
        out = self.gcn1(embeds, A)
        out = self.dp(out)
        out = self.gcn2(out, A)
        out = self.dp(out)
        out = self.gcn3(out, A)
        out = self.dp(out)
        out = torch.flatten(out, 1)
        out = torch.cat((out, init_query.to(device)), dim = 1)
        out = self.dense1(out)
        out = self.relu(out)
        out = self.dense2(out)
        return out

class InputDataset(torch.utils.data.Dataset):
    def __init__(self, ids, masks, token_type_ids, labels, init_qrs):
        self.ids = ids
        self.masks = masks
        self.token_type_ids = token_type_ids
        self.labels = torch.LongTensor(labels.long())
        self.init_qrs = init_qrs

    def __len__(self):
        return len(self.ids)

    def __getitem__(self, idx):
        return self.ids[idx], self.masks[idx], self.token_type_ids[idx], self.labels[idx], self.init_qrs[idx]

def prep_data():
    print("Preparing the data...")

    requests = pd.read_table('/mount/arbeitsdaten43/projekte/thesis-dp-1/banerjak/data/dev.tsv', sep = '\t', header = 0)

    df = requests

    f = open('/mount/arbeitsdaten43/projekte/thesis-dp-1/banerjak/doclists_dev.pkl', 'rb')
    docs = {}
    queries = {}
    query_list = []
    while True:
        try:
            data = pickle.load(f)
            tid = int(data['topic_id'])
            docs[tid] = data['docs']
            queries[tid] = data['queries']
        except EOFError:
            break
    f.close()

    tids = list(docs.keys())

    inputs = []
    for tid in tids:
        inputs.append(docs[tid][:num_docs])

    init_qrs = torch.zeros((len(tids), init_query_embed_size))
    ids = torch.LongTensor(len(tids), num_docs, max_len)
    masks = torch.LongTensor(len(tids), num_docs, max_len)
    token_type_ids = torch.LongTensor(len(tids), num_docs, max_len)

    print("Preparing training data...")

    for i in tqdm(range(len(tids))):
        tid = tids[i]
        documents = inputs[i]
        tokens = [tokenizer.encode_plus(doc, None, add_special_tokens = True, max_length = max_len, padding = 'max_length', return_token_type_ids = True, truncation = True) for doc in documents]
        idlist = [torch.tensor(token['input_ids'], dtype = torch.long) for token in tokens]
        torch.cat(idlist, out = ids[i])
        masklist = [torch.tensor(token['attention_mask'], dtype = torch.long) for token in tokens]
        torch.cat(masklist, out = masks[i])
        tokentypelist = [torch.tensor(token['token_type_ids'], dtype = torch.long) for token in tokens]
        torch.cat(tokentypelist, out = token_type_ids[i])
        init_qrs[i] = torch.Tensor(bert_model.encode(queries[tid]))

    return tids, ids, masks, token_type_ids, init_qrs

print("Testing")
def test():
    tids, ids, masks, token_type_ids, init_qrs = prep_data()
    model = torch.nn.DataParallel(AmbiguityNetwork())
    model.load_state_dict(torch.load('roberta_finetune_aug_model_for_test.pt'))
    model.cuda(device)
    model.eval()
    print("Testing...")
    ids = ids.to(device)
    masks = masks.to(device)
    token_type_ids = token_type_ids.to(device)
    with torch.no_grad():
        val_preds = model(ids, masks, token_type_ids, init_qrs)
    m = torch.nn.Softmax()
    val_npreds = m(val_preds).cpu().numpy()
    class_preds = np.argmax(val_npreds, axis = 1)
    class_preds = class_preds + 1
    outs = [(tids[i], class_preds[i]) for i in range(len(class_preds))]
    outs = np.array(outs)
    np.savetxt('preds_model_1_dev.txt', outs, fmt="%s %s")
    np.save('classpreds_model_1_dev.npy', class_preds)

test()


import time
import pyautogui
import cv2
import numpy as np
import datetime
import win32api
import win32con

import find_box
import log_message
import role_loc
import role_move
import send_message

map_in_store = cv2.imread('img/map_in_store.png')
open_map_btn = cv2.imread('img/open_map.png')
map_title = cv2.imread('img/map_title.png')
buy_map_tip = cv2.imread('img/buy_map_tip.png')
confirm_btn = cv2.imread('img/confirm_btn.png')
bag_left = cv2.imread('img/bag_left.png')
store_npc = cv2.imread('img/store_npc.png')
open_map_error = cv2.imread('img/open_map_error.png')
home_door_btn = cv2.imread('img/home_door_btn.png')
home_main_btn = cv2.imread('img/home_main_btn.png')
back_origin_btn = cv2.imread('img/back_origin_btn.png')
new_day_tip = cv2.imread('img/new_day_tip.png')
close_btn = cv2.imread('img/close_btn.png')
horse = cv2.imread('img/horse.png')
isdead = cv2.imread('img/isdead.png')

# 点开藏宝地图模式位置
open_box_map_pos = [537, 51]

# 要丢掉的首张图位值
first_map_pos = [1750, 350]

# 确定按钮位置
confirm_pos = [880, 450]

# 打开藏宝图等待时间（当前等级）
wait_open_time1 = 131
# 打开藏宝图等待时间（50张）
wait_open_time2 = 163
# 藏宝图探查吟唱时间减少
decreased_percent = 0.4

# 开始挖宝的坐标方向和大小
begin_find_loc_1 = [-825, -540]
begin_find_direct_1 = 0.6
find_area_1 = [65, 42]

# 挖宝区域大小
begin_find_loc_2 = [-975, -525]
begin_find_direct_2 = -0.5
find_area_2 = [61, 34]

# 背包格子大小
bag_item_size = 36
bag_width = 12

# 家园走到门口的位移距离
home_to_door = [-4, -1]

# 复活至龙星阵的具体坐标
resurrect_loc = [970,800]

def match_img(template):
    image = cv2.cvtColor(np.asarray(pyautogui.screenshot()), cv2.COLOR_RGB2BGR)
    match_res = cv2.matchTemplate(image, template, 3)
    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(match_res)
    return max_val, max_loc

def clear_map():
    pyautogui.press('m')
    time.sleep(0.5)
    max_val, max_loc = match_img(map_title)
    #print(max_val)
    if max_val < 0.95:
        pyautogui.moveTo(open_box_map_pos[0], open_box_map_pos[1])
        pyautogui.leftClick()
    #time.sleep(0.1)
    count = role_loc.get_clear_map_count()
    print('丢图数量 = '+str(count))
    for i in range(0, count):
        pyautogui.moveTo(first_map_pos[0], first_map_pos[1])
        pyautogui.rightClick()
        pyautogui.moveTo(first_map_pos[0] + 50, first_map_pos[1] + 30)
        pyautogui.leftClick()
        pyautogui.press('enter')
        # pyautogui.moveTo(confirm_pos[0], confirm_pos[1])
        # pyautogui.leftClick()
    pyautogui.moveTo(first_map_pos[0] - 50, first_map_pos[1] - 50)
    pyautogui.leftClick()
    pyautogui.press('m')
    max_val, max_loc = match_img(isdead)
    if max_val > 0.95:
        time.sleep(15)
        print("isdead的max_val = " + str(max_val))
        resurrect()
    return count

def buy_map():
    max_val = 0
    for i in range(0, 10):
        time.sleep(0.2)
        max_val, max_loc = match_img(store_npc)
        # print(max_val)
        if max_val > 0.9:
            break
        role_move.move_to([-803, -721])
        role_move.move_to([-803, -716], None, 1)
    if max_val <= 0.9:
        send_message_with_loc("Find Map NPC Error")
        return False
    pyautogui.press('f')
    time.sleep(0.5)
    max_val, max_loc = match_img(map_in_store)
    if max_val <= 0.9:
        send_message_with_loc("Open Map Store Error")
        return False
    clear_bag()
    pyautogui.moveTo(max_loc[0] + 24, max_loc[1] + 24)
    pyautogui.keyDown('shift')
    pyautogui.rightClick()
    pyautogui.keyUp('shift')
    max_val, max_loc = match_img(buy_map_tip)
    if max_val > 0.9:
        pyautogui.press('4')
        pyautogui.press('1')
        pyautogui.press('enter')
        pyautogui.click(x=None, y=None, clicks=9, interval=0.001, button='right', duration=0.0, tween=pyautogui.linear)
        # max_val, max_loc = match_img(confirm_btn)
        # if max_val > 0.9:
        #     pyautogui.moveTo(max_loc[0] + 50, max_loc[1] + 15)
        #     pyautogui.leftClick()
    time.sleep(0.5)
    return True

def open_map():
    role_move.move_to([-802, -703])
    role_move.move_to([-791, -702])
    role_move.move_to([-777, -701])
    role_move.move_to([-756, -703], None, 0, 5)
    max_val, max_loc = match_img(open_map_btn)
    pyautogui.moveTo(max_loc[0] + 24, max_loc[1] + 24)
    down_horse()
    pyautogui.leftClick()
    pyautogui.sleep(1)
    max_val, max_loc = match_img(open_map_error)
    open_map_count = 0
    if max_val < 0.9:
        pyautogui.press('m')
        time.sleep(0.5)
        max_val, max_loc = match_img(map_title)
        #print(max_val)
        if max_val < 0.95:
            pyautogui.moveTo(open_box_map_pos[0], open_box_map_pos[1])
            pyautogui.leftClick()
        open_map_count = role_loc.get_clear_map_count()
        pyautogui.press('m')
        print('开图数量 = '+str(open_map_count))
        wait_open_time = 5*(1-decreased_percent)*open_map_count-1
        #print('开图时间 = '+str(wait_open_time))
        pyautogui.sleep(wait_open_time)
        pyautogui.moveRel(0, -100)
        up_horse()
        return open_map_count
    else:
        close_dialog()
        up_horse()
        send_message_with_loc("Open Map Error")
        return False

def down_horse():
    if not is_on_horse():
        return
    pyautogui.press('t')
    pyautogui.press('shift')
    pyautogui.sleep(3)

def up_horse():
    if is_on_horse():
        return
    pyautogui.press('t')
    pyautogui.sleep(3)

def close_dialog():
    max_val, max_loc = match_img(close_btn)
    if max_val > 0.9:
        pyautogui.moveTo(max_loc[0] + 6, max_loc[1] + 6)
        pyautogui.leftClick()

def prepare_to_find():
    role_move.move_to([-779, -701])
    role_move.move_to([-793, -703])
    role_move.move_to([-793, -677])
    role_move.move_to([-795, -666])
    role_move.move_to([-795, -640])
    role_move.move_to(begin_find_loc_1, None, 1, 5)
    role_move.turn_to(begin_find_direct_1)
    loc = role_loc.get_current_loc()
    if loc is not None and abs(loc[0] - begin_find_loc_1[0]) < 5 and abs(loc[1] - begin_find_loc_1[1]) < 5:
        return True
    else:
        send_message_with_loc("Go to Find Box Error")
        return False

def find_boxs():
    count = 0
    role_move.move_to(begin_find_loc_1, None, 1, 5)
    role_move.turn_to(begin_find_direct_1)
    count += role_move.move_map(find_area_1[0], find_area_1[1], find_box.find_box_under_footer)
    role_move.move_to(begin_find_loc_2, None, 1, 5)
    role_move.turn_to(begin_find_direct_2)
    count += role_move.move_map(find_area_2[0], find_area_2[1], find_box.find_box_under_footer)
    role_move.move_to([-850, -560], None, 5, 3)
    print("开盒次数" + str(count))
    if count <= 0:
        reset_keys()
        send_message_with_loc("Find No Box")
    max_val, max_loc = match_img(isdead)
    if max_val > 0.95:
        time.sleep(15)
        print("isdead的max_val = " + str(max_val))
        resurrect()
    return count

def back_to_store():
    role_move.move_to([-795, -644])
    role_move.move_to([-795, -667])
    role_move.move_to([-795, -702])
    role_move.move_to([-802, -702])
    role_move.move_to([-803, -721])
    role_move.move_to([-803, -716], None, 0, 5)
    loc = role_loc.get_current_loc()
    if loc is not None and abs(-803 - loc[0]) < 5 and abs(-716 - loc[1]) < 5:
        return True
    else:
        send_message_with_loc("Back To Store Error")
        return False

def clear_bag():
    max_val, max_loc = match_img(bag_left)
    if max_val < 0.9:
        return
    first_loc = [max_loc[0] + 100, max_loc[1] - 90]
    pyautogui.keyDown('shift')
    for j in range(0, 4):
        for i in range(0, bag_width):
            pyautogui.moveTo(first_loc[0] + i * bag_item_size, first_loc[1] + j * bag_item_size)
            pyautogui.rightClick()
    # for i in range(0, 10):
    #     pyautogui.moveTo(first_loc[0] + i * bag_item_size, first_loc[1] + 3 * bag_item_size + 25)
    #     pyautogui.rightClick()
    pyautogui.keyUp('shift')

def reset_to_store():
    current_loc = role_loc.get_current_loc()
    if current_loc is None:
        return False
    # 处理在商店附近情况
    if abs(-803 - current_loc[0]) < 5 and abs(-716 - current_loc[1]) < 5:
        role_move.move_to([-803, -721])
    down_horse()
    max_val, max_loc = match_img(home_door_btn)
    if max_val < 0.9:
        up_horse()
        return False
    pyautogui.moveTo(max_loc[0] + 24, max_loc[1] + 24)
    pyautogui.leftClick()
    pyautogui.sleep(5)
    pyautogui.press('f')
    pyautogui.moveRel(-100, -100)
    time.sleep(1)

    max_val, max_loc = match_img(home_main_btn)
    if max_val < 0.9:
        up_horse()
        return False
    pyautogui.moveTo(max_loc[0] + 30, max_loc[1] + 15)
    pyautogui.leftClick()
    pyautogui.sleep(30)

    role_move.move(home_to_door[0], home_to_door[1])
    pyautogui.press('f')
    time.sleep(1)
    max_val, max_loc = match_img(back_origin_btn)
    if max_val < 0.9:
        up_horse()
        return False
    pyautogui.moveTo(max_loc[0] + 30, max_loc[1] + 15)
    pyautogui.leftClick()
    pyautogui.sleep(60)

    reset_visual_field()

    loc = role_loc.get_current_loc()
    up_horse()
    if loc is not None and abs(-803 - loc[0]) < 5 and abs(-715 - loc[1]) < 5:
        return True
    return False

def reset_keys():
    pyautogui.keyDown('shift')
    pyautogui.keyUp('shift')
    pyautogui.sleep(2)
    pyautogui.moveTo(find_box.footer_pos[0], find_box.footer_pos[1])
    pyautogui.sleep(2)
    pyautogui.mouseDown(button='left')
    pyautogui.sleep(2)
    pyautogui.mouseUp(button='left')
    pyautogui.sleep(2)
    pyautogui.mouseDown(button='right')
    pyautogui.sleep(2)
    pyautogui.mouseUp(button='right')
    pyautogui.sleep(2)

def try_reset():
    if not deal_new_day():
        return
    count = 0
    while not reset_to_store():
        count += 1
        send_message_with_loc("Try reset count " + str(count))
        role_move.move(-10, -10)
        time.sleep(600)
        if not deal_new_day():
            return

def deal_new_day():
    current_time = datetime.datetime.now()
    if 10 > current_time.hour > 5 and current_time.isoweekday() == 4:
        # 关服了
        return False
    max_val, max_loc = match_img(new_day_tip)
    if max_val > 0.9:
        close_dialog()
    return True

def is_on_horse():
    max_val, max_loc = match_img(horse)
    return max_val > 0.9

def reset_visual_field():
    reset_look_down()

    x, y = 1000, 700
    win32api.SetCursorPos((x, y))
    time.sleep(0.1)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, x, y)
    time.sleep(0.1)
    for i in range(0, 3):
        win32api.SetCursorPos((x, y))
        time.sleep(0.1)
        win32api.mouse_event(win32con.MOUSEEVENTF_MOVE, 0, -100)
        time.sleep(0.1)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0)
    time.sleep(0.1)

def reset_look_down():
    x, y = 1000, 120
    win32api.SetCursorPos((x, y))
    time.sleep(0.1)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, x, y)
    time.sleep(0.1)
    for i in range(0, 9):
        win32api.SetCursorPos((x, y))
        time.sleep(0.1)
        win32api.mouse_event(win32con.MOUSEEVENTF_MOVE, 0, 100)
        time.sleep(0.1)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0)
    time.sleep(0.1)

def send_message_with_loc(message):
    loc = role_loc.get_current_loc()
    direct = role_loc.get_current_direction()
    send_message.send_message(message + " " + str(loc) + " " + str(direct))

def print_log_with_loc(message):
    loc = role_loc.get_current_loc()
    direct = role_loc.get_current_direction()
    log_message.log_error(message + " " + str(loc) + " " + str(direct))

def send_message_briefing(message:list,index = 0):
    send_text = '简报第{0}次：'.format(index)+'\n'
    for index,key in enumerate(message):
        send_text = send_text+str(key)+':'+str(message[key])+'\n'
    send_message.send_message(send_text)

def resurrect(count = 3):
    for i in range(0,count):
        pyautogui.moveTo(resurrect_loc[0],resurrect_loc[1])
        pyautogui.leftClick()
        time.sleep(3)
        max_val, max_loc = match_img(isdead)
        if max_val < 0.95:
            print("复活后的max_val = " + str(max_val))
            break
    reset_to_store()
    send_message.send_message()("try to resurrect" + i + "次")


import json
from mitmproxy import ctx
from habdel_mongo import mongo_info
def response(flow):
    if "https://aweme.snssdk.com/aweme/v2/feed" in flow.request.url:  #原视频链接获取
        info = ctx.log.info
        info(flow.response.text)
        # print(flow.response.text,"888888888"*10)
        # print(flow.request.url)
    if "https://aweme.snssdk.com/aweme/v1/user/follower/list" in flow.request.url: #粉丝信息获取
        # with open('user.txt',"w")as f:
        #     f.write(flow.request.text)
        for user in json.loads(flow.response.text)['followers']:
            douyin_info={}
            douyin_info['uid']=user['uid']
            douyin_info['sec_uid'] = user['sec_uid']
            douyin_info['douyin_id'] = user['short_id']
            douyin_info['nickname'] = user['nickname']
            douyin_info['total_favorited'] = user['total_favorited']
            douyin_info['follower_count'] = user['follower_count']
            douyin_info['following_count'] = user['following_count']

            print(douyin_info)
            mongo_info.insert_item(douyin_info)


import pygame
from time import *
pygame.init()
win=pygame.display.set_mode((300,300))
sleep(5)
pygame.mixer.music.load("1.mp3")
pygame.mixer.music.play(0,0,800)
run=True
click=[0,0,0]
def check_events():
    global run,keys,mouse_pos,mouse_down,click
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            run=False
    keys=pygame.key.get_pressed()
    if keys[27]: run=False
    mouse_pos=pygame.mouse.get_pos()
    mouse_down=pygame.mouse.get_pressed()
    for i in range(3):
        if mouse_down[i]:
            click[i]+=1
        else:
            click[i]=0
clicks=[]
start_time=time()
while run:
    check_events()
    if click[0]==1:
        clicks.append(time()-start_time)


from pathlib import Path
import pendulum
from airflow.models import Variable
from docker.types import Mount

class AppConst:
    DOCKER_USER = Variable.get("DOCKER_USER", "thangphan")
    PROJECT = "real_estate"
    IMAGE_NAME = "model_serving"
    TAG = "latest"

class AppPath:
    ROOT_DIR = Path(Variable.get("ROOT_DIR"))
    CODE_DIR = Path(ROOT_DIR, "code")

    MODEL_SERVING_DIR = Path(CODE_DIR, "model_serving")

    FEATURE_REPO = Path(ROOT_DIR, "feature_repo")
    DATA_SOURCE_DIR = Path(ROOT_DIR, "data_sources")
    BATCH_DATA_DIR = Path(MODEL_SERVING_DIR, "data")

    ARTIFACTS_DIR = Path(MODEL_SERVING_DIR, "artifacts")

class DefaultConfig:
    DEFAULT_DAG_ARGS = {
        "owner": "thangphan",
        "retries": 0,
        "retry_deplay": pendulum.duration(seconds=20)
    }

    DEFAULT_DOCKER_OPERATORS_ARGS = {
        "image": f"{AppConst.DOCKER_USER}/{AppConst.PROJECT}/{AppConst.IMAGE_NAME}:{AppConst.TAG}",
        "api_version": "auto",
        "auto_remove": True,
        "mounts": [
            # Feature repo
            Mount(
                source=AppPath.FEATURE_REPO.absolute().as_posix(),
                target="/real_estate/feature_repo",
                type="bind",
            ),
            # Data source
            Mount(
                source=AppPath.DATA_SOURCE_DIR.absolute().as_posix(),
                target="/real_estate/data_sources",
                type="bind"
            ),
            # Artifacts
            Mount(
                source=AppPath.ARTIFACTS_DIR.absolute().as_posix(),
                target="/real_estate/code/model_serving/artifacts",
                type="bind"
            ),
            # Batch data
            Mount(
                source=AppPath.BATCH_DATA_DIR.absolute().as_posix(),
                target="/real_estate/code/model_serving/data",
                type="bind"
            )
        ],
        "mount_tmp_dir": False
    }


# -*- coding: utf-8 -*-
'''
Beacon to monitor disk usage.

.. versionadded:: 2015.5.0
'''

# Import Python libs
from __future__ import absolute_import
import logging
import psutil
import re

# Import Salt libs
import salt.utils

log = logging.getLogger(__name__)

__virtualname__ = 'diskusage'

def __virtual__():
    if salt.utils.is_windows():
        return False
    else:
        return __virtualname__

def beacon(config):
    '''
    Monitor the disk usage of the minion

    Specify thresholds for each disk and only emit a beacon if any of them are
    exceeded.

    code_block:: yaml

        beacons:
          diskusage:
            - /: 63%
            - /mnt/nfs: 50%
    '''
    ret = []
    for diskusage in config:
        mount = diskusage.keys()[0]

        try:
            _current_usage = psutil.disk_usage(mount)
        except OSError:
            # Ensure a valid mount point
            log.error('{0} is not a valid mount point, skipping.'.format(mount))
            continue

        current_usage = _current_usage.percent
        monitor_usage = diskusage[mount]
        if '%' in monitor_usage:
            monitor_usage = re.sub('%', '', monitor_usage)
        monitor_usage = float(monitor_usage)
        if current_usage >= monitor_usage:
            ret.append({'diskusage': current_usage, 'mount': mount})
    return ret


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

from django.db import migrations, models
from decimal import Decimal

class Migration(migrations.Migration):

    dependencies = [
        ('gas', '0005_auto_20150827_0116'),
    ]

    operations = [
        migrations.AddField(
            model_name='gas',
            name='other_percentage',
            field=models.DecimalField(max_digits=5, default=Decimal('0.00'), decimal_places=1, verbose_name='Percentage Other'),
        ),
    ]


import os
from datetime import datetime

def setnum():
    pass

# 图片命名格式化
if __name__ == '__main__':
    time1 = datetime.now()
    PATH = "Datalast_CUT"
    SAVE_PATH = "Datalast"
    if not os.path.isdir(SAVE_PATH):
        os.mkdir(SAVE_PATH)
    NUM_init = 9135  #自定义编号
    num = NUM_init
    for (dirpath, dirnames, filenames) in os.walk(PATH):
        dirpath = str(dirpath).replace("\\", "/")
        for file in filenames:
            reoldfile = dirpath + '/' + file
            # num = int(str(file).strip('.jpg'))
            # print(num)

            newfile = SAVE_PATH + "/fish" + str(num).zfill(6) + ".jpg"
            # newfile = str(SAVE_PATH+'/'+"fish","%4d"%num,".jpg")
            print(newfile)
            os.rename(reoldfile, newfile)
            num += 1
    time2 = datetime.now() - time1
    print(time2)


#!/usr/bin/env python3
# coding:utf8

import sys
import re
import requests
from bs4 import BeautifulSoup
import json
import random
from django.http import HttpResponse
from requests.packages.urllib3.exceptions import InsecureRequestWarning

reload(sys)
sys.setdefaultencoding('utf8')

requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

agents = [
    "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50",
    "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50",
    "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0",
    "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)",
    "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)",
    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)",
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1",
    "Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1",
    "Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; en) Presto/2.8.131 Version/11.11",
    "Opera/9.80 (Windows NT 6.1; U; en) Presto/2.8.131 Version/11.11",
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11",
    "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Maxthon 2.0)",
    "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; TencentTraveler 4.0)",
    "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)",
    "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; The World)",
    "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; SE 2.X MetaSr 1.0; SE 2.X MetaSr 1.0; .NET CLR 2.0.50727; SE 2.X MetaSr 1.0)",
    "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; 360SE)",
    "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Avant Browser)",
    "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"
]

headers = {
    'User-Agent': random.choice(agents),
    'Cookie': 'HJ_UID=9bc75b02-cb96-8216-4699-5380ad3d24f8; TRACKSITEMAP=3%2C; _REF=; _SREF_3=; HJ_SID=9f518693-5897-82ba-c5db-03be24a4911e; HJ_CST=0; HJ_CMATCH=1; HJ_SSID_3=8a5cf2d4-5acf-9e49-cd09-73a1d06adc74; HJ_CSST_3=1; _SREF_3='
}

def searchWord(request, keyword):
    wordURL = 'https://dict.hjenglish.com/jp/jc/' + keyword

    wordPage = requests.get(wordURL, headers=headers, verify=False)
    htmlData = wordPage.text.encode("utf8")

    soup = BeautifulSoup(htmlData.decode("utf-8", "ignore"), "html.parser")

    try:
        wordList = []

        for word in soup.find_all("div", class_="word-details-pane"):

            try:
                chinese = word.find("h2").text.encode("utf8")
            except Exception as e:
                print(e)
                chinese = e

            # print(chinese)

            pronounceArray = word.findAll("span")

            try:
                realKana = pronounceArray[0].text.replace("[", "").replace("]", "").encode("utf8")
            except Exception as e:
                print(e)
                realKana = e

            # print(realKana)

            try:
                toneStr = pronounceArray[2].text
                if toneStr == '':
                    tune = []
                else:
                    tune = toneStr.split(unicode('或', "utf8"))
            except Exception as e:
                print(e)
                tune = []

            # print(tune)

            simpleArray = word.find_all("div", class_="simple")

            meanings = []
            nominal = ""

            for simples in simpleArray:
                try:
                    nominal = simples.find("h2").text.replace("【", "").replace("】", "").replace(" ", "").replace(
                        unicode("词", "utf-8"), "").encode("utf8")
                except Exception as e:
                    print(e)
                    nominal = ""

                pattern = {
                    unicode('形容'): unicode('形'),
                    unicode('形容动'): unicode('形動'),
                    unicode('连'): unicode('連'),
                    unicode('动'): unicode('動'),
                }

                for x in pattern:
                    nominal = re.sub(x, pattern[x], nominal)

                # print(nominal)

                try:
                    meaningUl = simples.find("ul")

                    for meaningLi in meaningUl.findAll("li"):
                        meanings.append(re.sub(r'[0-9]\.', "", meaningLi.text.replace(" ", "").replace("\n", "")))
                except Exception as e:
                    print(e)

                # print(meanings)

            wordList.append(
                {
                    "kana": realKana,
                    "chinese": chinese,
                    "meanings": [x.encode("utf8") for x in meanings],
                    "nominal": nominal,
                    "tune": [x.encode("utf8") for x in tune],
                }
            )

            # print(wordList)

        print(json.dumps(wordList, ensure_ascii=False))

        return HttpResponse(json.dumps(wordList, ensure_ascii=False))
    except Exception as e:
        return HttpResponse(e)


import os
import tkinter as tk
import sys
from tkinter import filedialog
from tkinter import ttk
from openslide import open_slide
from openslide.deepzoom import DeepZoomGenerator
from modules.recorder import Recorder

class FileSelection:

    def __init__(self, master):

        self.master = master
        self.frame = tk.Frame(self.master)

        # select file button
        select_button = tk.Button(self.frame, text='Select File')
        select_button.pack(fill=tk.X)
        select_button.bind('<Button-1>', self.file_selection)

        self.frame.pack(padx=50, pady=50)

        if len(sys.argv) >= 2:
            root.tiles_directory = sys.argv[1]
        else:
            root.tiles_directory = None

    def file_selection(self, event):
        # open the file selection menu and get the file path
        root.file_path = filedialog.askopenfilename()

        # separate the file name from the full path
        root.file_name = os.path.basename(root.file_path)
        print("root.file_path: {}".format(root.file_path))

        self.frame.pack_forget()
        self.app = LevelSelection(self.master)

class LevelSelection:

    def __init__(self, master):

        frame = tk.Frame(root)
        frame.focus_force()
        slide = open_slide(root.file_path)
        dz_generator = DeepZoomGenerator(slide)

        select_level = ttk.Label(frame, text="Select Initial Level")
        select_level.pack()

        # combo box for initial level
        selection = ttk.Combobox(
            frame, values=[i for i in range(dz_generator.level_count)])
        selection.pack()

        # confirm button
        confirm = tk.Button(frame, text='OK')
        confirm.pack()

        def on_button_press(event):
            frame.pack_forget()

            Recorder(root, deep_zoom_object=dz_generator, level=int(selection.get()))
            #     print ("Recorder")

            # # if tiles_dirctory is provided in args, the visualiser tool is run
            # if root.tiles_directory is None:
            #     Recorder(root, deep_zoom_object=dz_generator, level=int(selection.get()))
            #     print ("Recorder")
            # else:
            #     Visualiser(root, deep_zoom_object=dz_generator, level=int(selection.get()))
            #     print ("Visualiser")

        confirm.bind('<Button-1>', on_button_press)

        frame.pack(padx=50, pady=50)

root = tk.Tk()
root.minsize(width=250, height=125)
root.title("WSI Viewer")

app = FileSelection(root)
root.mainloop()


from __future__ import absolute_import, division, print_function
__metaclass__ = type

ANSIBLE_METADATA = {'metadata_version': '1.1',
                    'status': ['preview'],
                    'supported_by': 'community'}

DOCUMENTATION = '''
---
module: azure_rm_cdnendpoint
version_added: "2.8"
short_description: Manage Azure Endpoint instance.
description:
    - Create, update and delete instance of Azure Endpoint.

options:
    resource_group:
        description:
            - Name of the Resource group within the Azure subscription.
        required: True
    profile_name:
        description:
            - Name of the CDN profile which is unique within the resource group.
        required: True
    name:
        description:
            - Name of the endpoint under the profile which is unique globally.
        required: True
    location:
        description:
            - Resource location.
            - Required when C(state) is I(present).
    origin_host_header:
        description:
            - "The host header value sent to the origin with each request. If you leave this blank, the request hostname determines this value. Azure CDN
               I(origins), such as Web Apps, Blob Storage, and Cloud Services require this host header value to match the origin hostname by default."
    origin_path:
        description:
            - A directory path on the origin that CDN can use to retreive content from, e.g. contoso.cloudapp.net/originpath.
    content_types_to_compress:
        description:
            - List of content types on which compression applies. The value should be a valid MIME type.
        type: list
    is_compression_enabled:
        description:
            - "Indicates whether content compression is enabled on CDN. Default value is false. If compression is enabled, content will be served as
               compressed if user requests for a compressed version. Content won't be compressed on CDN when requested content is smaller than 1 byte or
               larger than 1 MB."
    is_http_allowed:
        description:
            - Indicates whether HTTP traffic is allowed on the endpoint. Default value is true. At least one protocol (HTTP or HTTPS) must be allowed.
    is_https_allowed:
        description:
            - Indicates whether HTTPS traffic is allowed on the endpoint. Default value is true. At least one protocol (HTTP or HTTPS) must be allowed.
    query_string_caching_behavior:
        description:
            - "Defines how CDN caches requests that include query strings. You can ignore any query strings when caching, bypass caching to prevent requests
               that contain query strings from being cached, or cache every request with a unique URL."
        choices:
            - 'ignore_query_string'
            - 'bypass_caching'
            - 'use_query_string'
            - 'not_set'
    optimization_type:
        description:
            - "Specifies what scenario the customer wants this CDN endpoint to optimize for, e.g. Download, Media services. With this information, CDN can
               apply scenario driven optimization."
        choices:
            - 'general_web_delivery'
            - 'general_media_streaming'
            - 'video_on_demand_media_streaming'
            - 'large_file_download'
            - 'dynamic_site_acceleration'
    probe_path:
        description:
            - "Path to a file hosted on the origin which helps accelerate delivery of the dynamic content and calculate the most optimal routes for the CDN.
               This is relative to the origin path."
    geo_filters:
        description:
            - "List of rules defining the user's geo access within a CDN endpoint. Each geo filter defines an acess rule to a specified path or content,
               e.g. block APAC for path /pictures/"
        type: list
        suboptions:
            relative_path:
                description:
                    - "Relative path applicable to geo filter. (e.g. '/mypictures', '/mypicture/kitty.jpg', and etc.)"
                    - Required when C(state) is I(present).
            action:
                description:
                    - Action of the geo filter, i.e. C(allow) or C(block) access.
                    - Required when C(state) is I(present).
                choices:
                    - 'block'
                    - 'allow'
            country_codes:
                description:
                    - Two letter country codes defining user country access in a geo filter, e.g. AU, MX, US.
                    - Required when C(state) is I(present).
                type: list
    delivery_policy:
        description:
            - A policy that specifies the delivery rules to be used for an endpoint.
        suboptions:
            description:
                description:
                    - User-friendly description of the policy.
            rules:
                description:
                    - A list of the delivery rules.
                type: list
                suboptions:
                    order:
                        description:
                            - "The order in which the rules are applied for the endpoint. Possible values {0,1,2,3,………}. A rule with a lesser order will be
                               applied before a rule with a greater order. Rule with order 0 is a special rule. It does not require any condition and
                               I(actions) listed in it will always be applied."
                            - Required when C(state) is I(present).
                    actions:
                        description:
                            - A list of actions that are executed when all the I(conditions) of a rule are satisfied.
                            - Required when C(state) is I(present).
                        type: list
                        suboptions:
                            name:
                                description:
                                    - Constant filled by server.
                                    - Required when C(state) is I(present).
                    conditions:
                        description:
                            - A list of conditions that must be matched for the I(actions) to be executed
                        type: list
                        suboptions:
                            name:
                                description:
                                    - Constant filled by server.
                                    - Required when C(state) is I(present).
    origins:
        description:
            - The source of the content being delivered via CDN.
            - Required when C(state) is I(present).
        type: list
        suboptions:
            name:
                description:
                    - Origin name
                    - Required when C(state) is I(present).
            host_name:
                description:
                    - The address of the origin. It can be a domain name, IPv4 address, or IPv6 address.
                    - Required when C(state) is I(present).
            http_port:
                description:
                    - The value of the HTTP port. Must be between 1 and 65535
            https_port:
                description:
                    - The value of the HTTPS port. Must be between 1 and 65535
    state:
      description:
        - Assert the state of the Endpoint.
        - Use 'present' to create or update an Endpoint and 'absent' to delete it.
      default: present
      choices:
        - absent
        - present

extends_documentation_fragment:
    - azure
    - azure_tags

author:
    - "Zim Kalinowski (@zikalino)"

'''

EXAMPLES = '''
  - name: Create (or update) Endpoint
    azure_rm_cdnendpoint:
      resource_group: RG
      profile_name: profile1
      name: endpoint1
      location: WestCentralUs
      origins:
        - name: www-bing-com
          host_name: www.bing.com
          http_port: 80
          https_port: 443
'''

RETURN = '''
id:
    description:
        - Resource ID.
    returned: always
    type: str
    sample: /subscriptions/subid/resourcegroups/RG/providers/Microsoft.Cdn/profiles/profile1/endpoints/endpoint1
'''

import time
from ansible.module_utils.azure_rm_common import AzureRMModuleBase
from ansible.module_utils.common.dict_transformations import _snake_to_camel

try:
    from msrestazure.azure_exceptions import CloudError
    from msrest.polling import LROPoller
    from msrestazure.azure_operation import AzureOperationPoller
    from azure.mgmt.cdn import CdnManagementClient
    from msrest.serialization import Model
except ImportError:
    # This is handled in azure_rm_common
    pass

class Actions:
    NoAction, Create, Update, Delete = range(4)

class AzureRMEndpoint(AzureRMModuleBase):
    """Configuration class for an Azure RM Endpoint resource"""

    def __init__(self):
        self.module_arg_spec = dict(
            resource_group=dict(
                type='str',
                required=True
            ),
            profile_name=dict(
                type='str',
                required=True
            ),
            name=dict(
                type='str',
                required=True
            ),
            location=dict(
                type='str'
            ),
            origin_host_header=dict(
                type='str'
            ),
            origin_path=dict(
                type='str'
            ),
            content_types_to_compress=dict(
                type='list'
            ),
            is_compression_enabled=dict(
                type='str'
            ),
            is_http_allowed=dict(
                type='str'
            ),
            is_https_allowed=dict(
                type='str'
            ),
            query_string_caching_behavior=dict(
                type='str',
                choices=['ignore_query_string',
                         'bypass_caching',
                         'use_query_string',
                         'not_set']
            ),
            optimization_type=dict(
                type='str',
                choices=['general_web_delivery',
                         'general_media_streaming',
                         'video_on_demand_media_streaming',
                         'large_file_download',
                         'dynamic_site_acceleration']
            ),
            probe_path=dict(
                type='str'
            ),
            geo_filters=dict(
                type='list',
                options=dict(
                    relative_path=dict(
                        type='str'
                    ),
                    action=dict(
                        type='str',
                        choices=['block',
                                 'allow']
                    ),
                    country_codes=dict(
                        type='list'
                    )
                )
            ),
            delivery_policy=dict(
                type='dict',
                options=dict(
                    description=dict(
                        type='str'
                    ),
                    rules=dict(
                        type='list',
                        options=dict(
                            order=dict(
                                type='int'
                            ),
                            actions=dict(
                                type='list',
                                options=dict(
                                    name=dict(
                                        type='str'
                                    )
                                )
                            ),
                            conditions=dict(
                                type='list',
                                options=dict(
                                    name=dict(
                                        type='str'
                                    )
                                )
                            )
                        )
                    )
                )
            ),
            origins=dict(
                type='list',
                options=dict(
                    name=dict(
                        type='str'
                    ),
                    host_name=dict(
                        type='str'
                    ),
                    http_port=dict(
                        type='int'
                    ),
                    https_port=dict(
                        type='int'
                    )
                )
            ),
            state=dict(
                type='str',
                default='present',
                choices=['present', 'absent']
            )
        )

        self.resource_group = None
        self.profile_name = None
        self.name = None
        self.endpoint = dict()

        self.results = dict(changed=False)
        self.mgmt_client = None
        self.state = None
        self.to_do = Actions.NoAction

        super(AzureRMEndpoint, self).__init__(derived_arg_spec=self.module_arg_spec,
                                              supports_check_mode=True,
                                              supports_tags=True)

    def exec_module(self, **kwargs):
        """Main module execution method"""

        for key in list(self.module_arg_spec.keys()) + ['tags']:
            if hasattr(self, key):
                setattr(self, key, kwargs[key])
            elif kwargs[key] is not None:
                self.endpoint[key] = kwargs[key]

        dict_camelize(self.endpoint, ['query_string_caching_behavior'], True)
        dict_camelize(self.endpoint, ['optimization_type'], True)
        dict_camelize(self.endpoint, ['geo_filters', 'action'], True)

        response = None

        self.mgmt_client = self.get_mgmt_svc_client(CdnManagementClient,
                                                    base_url=self._cloud_environment.endpoints.resource_manager)

        resource_group = self.get_resource_group(self.resource_group)

        old_response = self.get_endpoint()

        if not old_response:
            self.log("Endpoint instance doesn't exist")
            if self.state == 'absent':
                self.log("Old instance didn't exist")
            else:
                self.to_do = Actions.Create
        else:
            self.log("Endpoint instance already exists")
            if self.state == 'absent':
                self.to_do = Actions.Delete
            elif self.state == 'present':
                if (not default_compare(self.endpoint, old_response, '', self.results)):
                    self.to_do = Actions.Update

        if (self.to_do == Actions.Create) or (self.to_do == Actions.Update):
            self.log("Need to Create / Update the Endpoint instance")

            if self.check_mode:
                self.results['changed'] = True
                return self.results

            response = self.create_update_endpoint()

            self.results['changed'] = True
            self.log("Creation / Update done")
        elif self.to_do == Actions.Delete:
            self.log("Endpoint instance deleted")
            self.results['changed'] = True

            if self.check_mode:
                return self.results

            self.delete_endpoint()
            # This currently doesnt' work as there is a bug in SDK / Service
            if isinstance(response, LROPoller) or isinstance(response, AzureOperationPoller):
                response = self.get_poller_result(response)
        else:
            self.log("Endpoint instance unchanged")
            self.results['changed'] = False
            response = old_response

        if self.state == 'present':
            self.results.update({
                'id': response.get('id', None)
                })
        return self.results

    def create_update_endpoint(self):
        '''
        Creates or updates Endpoint with the specified configuration.

        :return: deserialized Endpoint instance state dictionary
        '''
        self.log("Creating / Updating the Endpoint instance {0}".format(self.name))

        try:
            if self.to_do == Actions.Create:
                response = self.mgmt_client.endpoints.create(resource_group_name=self.resource_group,
                                                             profile_name=self.profile_name,
                                                             endpoint_name=self.name,
                                                             endpoint=self.endpoint)
            else:
                response = self.mgmt_client.endpoints.update(resource_group_name=self.resource_group,
                                                             profile_name=self.profile_name,
                                                             endpoint_name=self.name,
                                                             endpoint_update_properties=self.endpoint_update_properties)
            if isinstance(response, LROPoller) or isinstance(response, AzureOperationPoller):
                response = self.get_poller_result(response)

        except CloudError as exc:
            self.log('Error attempting to create the Endpoint instance.')
            self.fail("Error creating the Endpoint instance: {0}".format(str(exc)))
        return response.as_dict()

    def delete_endpoint(self):
        '''
        Deletes specified Endpoint instance in the specified subscription and resource group.

        :return: True
        '''
        self.log("Deleting the Endpoint instance {0}".format(self.name))
        try:
            response = self.mgmt_client.endpoints.delete(resource_group_name=self.resource_group,
                                                         profile_name=self.profile_name,
                                                         endpoint_name=self.name)
        except CloudError as e:
            self.log('Error attempting to delete the Endpoint instance.')
            self.fail("Error deleting the Endpoint instance: {0}".format(str(e)))

        return True

    def get_endpoint(self):
        '''
        Gets the properties of the specified Endpoint.

        :return: deserialized Endpoint instance state dictionary
        '''
        self.log("Checking if the Endpoint instance {0} is present".format(self.name))
        found = False
        try:
            response = self.mgmt_client.endpoints.get(resource_group_name=self.resource_group,
                                                      profile_name=self.profile_name,
                                                      endpoint_name=self.name)
            found = True
            self.log("Response : {0}".format(response))
            self.log("Endpoint instance : {0} found".format(response.name))
        except CloudError as e:
            self.log('Did not find the Endpoint instance.')
        if found is True:
            return response.as_dict()

        return False

def default_compare(new, old, path, result):
    if new is None:
        return True
    elif isinstance(new, dict):
        if not isinstance(old, dict):
            result['compare'] = 'changed [' + path + '] old dict is null'
            return False
        for k in new.keys():
            if not default_compare(new.get(k), old.get(k, None), path + '/' + k, result):
                return False
        return True
    elif isinstance(new, list):
        if not isinstance(old, list) or len(new) != len(old):
            result['compare'] = 'changed [' + path + '] length is different or null'
            return False
        if isinstance(old[0], dict):
            key = None
            if 'id' in old[0] and 'id' in new[0]:
                key = 'id'
            elif 'name' in old[0] and 'name' in new[0]:
                key = 'name'
            else:
                key = list(old[0])[0]
            new = sorted(new, key=lambda x: x.get(key, None))
            old = sorted(old, key=lambda x: x.get(key, None))
        else:
            new = sorted(new)
            old = sorted(old)
        for i in range(len(new)):
            if not default_compare(new[i], old[i], path + '/*', result):
                return False
        return True
    else:
        if path == '/location':
            new = new.replace(' ', '').lower()
            old = new.replace(' ', '').lower()
        if new == old:
            return True
        else:
            result['compare'] = 'changed [' + path + '] ' + str(new) + ' != ' + str(old)
            return False

def dict_camelize(d, path, camelize_first):
    if isinstance(d, list):
        for i in range(len(d)):
            dict_camelize(d[i], path, camelize_first)
    elif isinstance(d, dict):
        if len(path) == 1:
            old_value = d.get(path[0], None)
            if old_value is not None:
                d[path[0]] = _snake_to_camel(old_value, camelize_first)
        else:
            sd = d.get(path[0], None)
            if sd is not None:
                dict_camelize(sd, path[1:], camelize_first)

def main():
    """Main execution"""
    AzureRMEndpoint()

if __name__ == '__main__':
    main()


from functools import reduce

def my_lambda(acc,val):
    res, max = acc
    if(val>max):
        max = val
        res+=1
    return res,max

myList = [1,2,3,4,5,1]
skyscraper = reduce(my_lambda, myList, (0,0))

print(myList, "Il max cambia " ,skyscraper[0], " volte")


import json
import csv

AUTHORS_JSON_PATH = '/Users/AB/Dropbox/Dev/CWP/authors.json'
NARRATIVES_JSON_PATH = '/Users/AB/Dropbox/Dev/CWP/narratives.json'

with open(AUTHORS_JSON_PATH, 'r') as f:
    authors = json.load(f)

with open(NARRATIVES_JSON_PATH, 'r') as f:
    narratives = json.load(f)

with open('narratives.csv', 'w') as csvfile:
    # fieldnames = ['id', 'name', 'model']
    fieldnames = ['id', 'author_id', 'name', 'year', 'text']
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
    writer.writeheader()
    for item in narratives:
        row = {}
        # row['id'] = int(item['pk'])
        # row['name'] = item['fields']['name']
        # row['model'] = item['model']
        row['id'] = int(item['pk'])
        row['author_id'] = item['fields']['author_id']
        author = [x for x in authors if x['pk'] == row['author_id']][0]
        row['name'] = author['fields']['name']
        row['year'] = int(item['fields']['year'])
        row['text'] = item['fields']['text']

        writer.writerow(row)


"""
plot_model.py
Code to plot an HII region model fit and data
Based on IDL code by A.A. Kepley

Trey Wenger June 2016

dsb 21Jun2016 - Use linecolor to distinguish models instead of linestyle
dsb 02Sep2016 - Modify units labels in plots
dsb 14Sep2016 - Add spontaneous emission model; GBT data point
dsb 26Sep2016 - Create separate plots for different models
dsb 16Nov2016 - Need divide HII region size by 2 to get radius
dsb 07Dec2016 - Add obsDV and smDens to inputs.
"""
import numpy as np
import math
import matplotlib
matplotlib.rc_file('matplotlibrc')
import matplotlib.pyplot as plt
import astropy.constants as c
import astropy.units as u
from scipy.optimize import curve_fit
import utils
import argparse

import pdb
#pdb.set_trace()

# Define power law function
def powFunction(x, a, b):
    return a*np.array(x)**b

def plot_model(filenames,labels=None,
               freqrange=[1.,120.],
               linerange=[1.e-25,1.e-21],
               contrange=[1.e-29,1.e-26],
               obsDV = 75.0,
               smDen=[10.0, 25.0, 100.0]):
    """
    Plot model fits on top of the data

    Inputs:
      filenames = list of HII region model pickle files to plot
      labels    = list of labels for legend
      linerange = range for y-axis of line plot (W/m2)
      contrange = range for y-axis of cont plot (W/m2/Hz)
      obsDV     = observed RRL FWHM line width (km/s)
      smDen     = densities to use for simple model (cm-3)
    """

    # --------------------------------
    # Model: Collection of HII regions
    # --------------------------------
    #
    if len(filenames) > 4:
        raise ValueError("Can plot up to 4 models at once")
    if labels is None:
        model_labels = [None] * len(filenames)
    linecolors = ['k','r','g','b']
    #
    # Calculate best fit line and continuum flux models for a range
    # of RRL frequencies
    #
    models = [np.load(filename) for filename in filenames]
    # range of n to plot
    fit_lines = np.arange(110)+40.
    for model in models:
        model.fit_lines = fit_lines
        model.calc_HII_region_properties()
    #
    # Plot RRL
    #
    fig = plt.figure()
    # Models
    for model,linecolor,label in zip(models,linecolors,model_labels):
        plt.plot(model.rrl_freq,model.line_flux*model.num_HII_regions,
                 linecolor,linestyle='-',label=label)
    # VLA data
    plt.errorbar(model.linedata['freq_GHz'],model.linedata['intflux_Wm2'],
                 yerr=model.linedata['intflux_Wm2_err'],linestyle='',
                 color='k',marker='o')
    # GBT data
    # (H52+H53) integrated intensity = 147.1 (5.7) mJy km/s
    # H52alpha freq = 45.453 GHz
    freqGBT = 45.45373
    sInt_mks = 147.1*1.e-3*1.e-26*1.e3*freqGBT*1.e9/c.c.value
    sIntErr_mks = 5.7*1.e-3*1.e-26*1.e3*freqGBT*1.e9/c.c.value
    #plt.errorbar([freqGBT], [sInt_mks], yerr=[sIntErr_mks], color='g', marker='o')
    # labels, etc.
    plt.xlabel("Frequency (GHz)", fontsize=20)
    plt.ylabel(r"Integrated Line Flux (W$\,$m$^{-2}$)", fontsize=20)
    plt.xscale('log')
    plt.yscale('log')
    plt.xlim(freqrange)
    plt.ylim(linerange)
    plt.text(freqrange[0]*(1.2), linerange[1]*(1-0.5), 'Multiple Compact HII Region Model', fontsize=20)
    fooNe = "%.2g" % (model.electron_dens)
    fooTe = "%4.0f" % (model.electron_temp)
    plt.text(freqrange[0]*(1.2), linerange[1]*(1-0.75), r'(n$_{\rm e}$, T$_{\rm e}$)  = (' + fooNe + ' cm$^{-3}$, '  + fooTe + r' K)', fontsize=20)

    if label is not None:
        plt.legend(fontsize=12,loc='best')
    plt.tight_layout()
    plt.savefig('linefit.eps')
    #
    # Plot radio continuum
    #
    fig = plt.figure()
    for model,linecolor,label in zip(models,linecolors,model_labels):
        if label is None:
            mylabel = ''
        else:
            mylabel = label+', '
        plt.plot(model.rrl_freq,model.thermal_fluxden_rrl+model.nonthermal_fluxden_rrl,
                 linecolor,linestyle='-',label=mylabel+'Total')
        plt.plot(model.rrl_freq,model.thermal_fluxden_rrl,
                 linecolor,linestyle=':',label=mylabel+'Thermal')
        plt.plot(model.rrl_freq,model.nonthermal_fluxden_rrl,
                 linecolor,linestyle='--',label=mylabel+'Non-thermal')
    plt.errorbar(model.contdata['freq_GHz'],model.contdata['S_C_mJy']*1.e-29,
                 yerr=model.contdata['S_C_mJy_err']*1.e-29,linestyle='',
                 color='k',marker='o')
    plt.xlabel("Frequency (GHz)", fontsize=20)
    plt.ylabel(r"Continuum Flux Density (W$\,$m$^{-2}\,$Hz$^{-1}$)", fontsize=20)
    plt.xscale('log')
    plt.yscale('log')
    plt.xlim(freqrange)
    plt.ylim(contrange)
    plt.text(freqrange[0]*(1.2), contrange[1]*(1-0.5), 'Multiple Compact HII Region Model', fontsize=20)
    plt.legend(fontsize=16,loc='best')
    plt.tight_layout()
    plt.savefig('contfit.eps')

    # --------------------------------
    # Model: Spontaneous emission
    # --------------------------------
    #
    # Calculate simple model (sm): spontaneous, optically thin emission in LTE
    #  Use equations 4-9 from Puxley et al. (1991, MNRAS, 248, 585)
    #
    smTe = model.electron_temp              # K
    smDist = model.distance                 # Mpc
    smFreq = model.linedata['freq_GHz'][0]  # GHz use Ka-band freq
    smOmega = model.omega_region            # steradians
    smAngSize = math.sqrt(smOmega/math.pi)  # radians
    smLinSize = smDist*1.e6*smAngSize       # pc
    # line width (Hz)
    smDV = (obsDV*1.e3)*(smFreq*1.e9)/c.c.value
    # Planck Function (mks)
    smB = (2.0*c.h.value*(smFreq*1.e9)**3/c.c.value**2)*(1.0/(math.exp((c.h.value*smFreq*1.e9)/(c.k_B.value*smTe)) - 1.0))
    # set density list to an np array
    smDen = np.array(smDen)
    # EM cm-6 pc
    smEM = smDen**2*smLinSize
    # Continuum optical depth (Eq. 8)
    smTauC = 8.2e-2*smTe**(-1.35)*smFreq**(-2.1)*smEM
    # LTE line optical depth (Eq. 7)
    smTauL = 1.7e3*(smDV*1.e-3)**(-1)*smTe**(-2.5)*smEM
    # thermal continuum flux density (Eq. 5)
    smContFlux = smOmega*smB*(1.0 - np.exp(-smTauC))
    # spontaneous line emission (Eq. 4)
    smLineEmission = smOmega*smB*np.exp(-smTauC)*(1.0 - np.exp(-smTauL))
    # integrated line emission (mks)
    smIntLine = 1.064*smLineEmission*smDV
    # Line flux (optically thin) = a * freq^2
    aLine = smIntLine/smFreq**2
    # Cont flux (optically thin) = a * freq^(-0.1)
    aCont = smContFlux/smFreq**(-0.1)
    # number of H-ionizing photons (assume a sphere)
    #  Assume case B recombination rate (cm^3 s-1)
    #  Use Hui & Gnedin (1997) approximation
    lambda_hi = 315614.0/smTe
    alpha_B = (2.753e-14*(lambda_hi**1.5/(1.0 + (lambda_hi/2.740)**0.407)**2.242))
    smRadius = (smLinSize*u.pc).to(u.cm).value/2.0
    smNL = (4.0/3.0)*math.pi*smRadius**3*smDen**2*alpha_B
    # SFR from Murphy et al. (2011)
    smSFR = 7.29e-54*smNL

    # Plot RRL
    #
    fig = plt.figure()
    # Simple model
    xx = np.array([model.rrl_freq[len(model.rrl_freq)-1], model.rrl_freq[0]])
    for i in range(len(aLine)):
        yy = aLine[i]*xx**2
        if smTauC[i] < 1.0 and smTauL[i] < 1.0:
            fooNe = "%3.0f" % (smDen[i])
            fooTe = "%4.0f" % (smTe)
            plt.plot(xx, yy, '--', label=r'(n$_{\rm e}$, T$_{\rm e}$)  = (' + fooNe + ' cm$^{-3}$, '  + fooTe + r' K)')
    # VLA data
    plt.errorbar(model.linedata['freq_GHz'],model.linedata['intflux_Wm2'],
                 yerr=model.linedata['intflux_Wm2_err'],linestyle='',
                 color='k',marker='o')
    # GBT data
    # (H52+H53) integrated intensity = 147.1 (5.7) mJy km/s
    # H52alpha freq = 45.453 GHz
    freqGBT = 45.45373
    sInt_mks = 147.1*1.e-3*1.e-26*1.e3*freqGBT*1.e9/c.c.value
    sIntErr_mks = 5.7*1.e-3*1.e-26*1.e3*freqGBT*1.e9/c.c.value
    #plt.errorbar([freqGBT], [sInt_mks], yerr=[sIntErr_mks], color='g', marker='o')
    # labels, etc.
    plt.xlabel("Frequency (GHz)", fontsize=20)
    plt.ylabel(r"Integrated Line Flux (W$\,$m$^{-2}$)", fontsize=20)
    plt.xscale('log')
    plt.yscale('log')
    plt.xlim(freqrange)
    plt.ylim(linerange)
    plt.text(freqrange[0]*(1.2), linerange[1]*(1-0.5), 'Spontaneous Emission Model', fontsize=20)
    plt.legend(fontsize=12, loc=4)
    plt.tight_layout()
    plt.savefig('linefitSimple.eps')
    #
    # Plot radio continuum (use second density model only)
    #
    fig = plt.figure()
    # non-thermal = total observed - thermal
    xObs = model.contdata['freq_GHz']
    yObs = model.contdata['S_C_mJy']*1.e-29
    yThermal = aCont[1]*xObs**(-0.1)
    yNonthermal = yObs - yThermal
    # Fit Power law
    p0 = [yObs.mean(), -0.7]
    popt, pcov = curve_fit(powFunction, xObs, yNonthermal, p0)
    # Calculate various contributions over extended frequency range
    xx = np.array([model.rrl_freq[len(model.rrl_freq)-1], model.rrl_freq[0]])
    # Thermal
    yyThermal = aCont[1]*xx**(-0.1)
    # Non-thermal
    yyNonthermal = popt[0]*xx**(popt[1])
    # Total
    yyTotal = yyThermal + yyNonthermal
    # plot model
    plt.plot(xx, yyTotal, '-k', label='Total')
    plt.plot(xx, yyThermal, ':k', label='Thermal')
    plt.plot(xx, yyNonthermal, '--k', label='Non-thermal')
    # plot data
    plt.errorbar(model.contdata['freq_GHz'],model.contdata['S_C_mJy']*1.e-29,
                 yerr=model.contdata['S_C_mJy_err']*1.e-29,linestyle='',
                 color='k',marker='o')
    plt.xlabel("Frequency (GHz)", fontsize=20)
    plt.ylabel(r"Continuum Flux Density (W$\,$m$^{-2}\,$Hz$^{-1}$)", fontsize=20)
    plt.xscale('log')
    plt.yscale('log')
    plt.xlim(freqrange)
    plt.ylim(contrange)
    plt.text(freqrange[0]*(1.2), contrange[1]*(1-0.5), 'Spontaneous Emission Model', fontsize=20)
    plt.legend(fontsize=16,loc='best')
    plt.tight_layout()
    plt.savefig('contfitSimple.eps')

    # print results (second density model only)
    print("=============================")
    print("Spontaneous Emissionn Model")
    print("=============================")
    print("Electron temperature = {:g} K".format(smTe))
    print("Electron density = {:g} cm-3".format(smDen[1]))
    print("HII region size = {:g} pc".format(smLinSize))
    print("Non-thermal spectral index = {:g}".format(popt[1]))
    #print("Mass of ionized gas = {:g} Msun".format(best_model.mass_ion))
    print("Number of H-ionizing photons = {:g} s-1".format(smNL[1]))
    print("Star Formation Rate = {:g} Msun/yr".format(smSFR[1]))
    print("=============================")

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Plot HII region model(s) and data",
                                     formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('filenames',nargs="+",help='filenames of model pickle files')
    parser.add_argument('--labels',nargs="+",default=None,help='labels for each model')
    parser.add_argument('--freqrange',nargs=2,default=[1.,120.],type=float,help='x-axis range (min max) (GHz)')
    parser.add_argument('--linerange',nargs=2,default=[1.e-25,1.e-21],type=float,help='y-axis range (min max) for line plots (W/m2)')
    parser.add_argument('--contrange',nargs=2,default=[1.e-29,1.e-26],type=float,help='y-axis range (min max) for cont plots (W/m2/Hz)')
    parser.add_argument('--obsDV',nargs=1,default=75.0,type=float,help='observed RRL FWHM line width (km/s)')
    parser.add_argument('--smDen',nargs=3,default=[10., 25.0, 100.0],type=float,help='densities to use for simple model (cm-3)')
    args = parser.parse_args()
    plot_model(args.filenames,labels=args.labels,
               linerange=args.linerange,contrange=args.contrange,
               obsDV=args.obsDV,smDen=args.smDen)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
Объект цифрового LED индикатора.
"""

import wx
from ic.components import icwidget
from ic.utils import util
from ic.PropertyEditor import icDefInf

from ic.log import log

from ic.bitmap import bmpfunc
import wx.lib.gizmos as parentModule

LED_NUMBER_CTRL_STYLE = {'LED_ALIGN_LEFT': parentModule.LED_ALIGN_LEFT,
                         'LED_ALIGN_RIGHT': parentModule.LED_ALIGN_RIGHT,
                         'LED_ALIGN_CENTER': parentModule.LED_ALIGN_CENTER,
                         'LED_ALIGN_MASK': parentModule.LED_ALIGN_MASK,
                         'LED_DRAW_FADED': parentModule.LED_DRAW_FADED,
                         }

SPC_IC_LEDNUMBERCTRL = {'draw_faded': True,
                        'foreground_colour': None,
                        'background_colour': None,
                        'default': u'---',

                        '__parent__': icwidget.SPC_IC_WIDGET,

                        '__attr_hlp__': {'draw_faded': 'Признак, будут ли неосвещенные сегменты отображаться с выцветшей версией цвета переднего плана',
                                         'foreground_colour': u'Цвет текста (По умолчанию зеленый)',
                                         'background_colour': u'Цвет фона (По умолчанию черный)',
                                         'default': u'Значение по умолчанию',
                                         },
                        }

#   Тип компонента
ic_class_type = icDefInf._icUserType

#   Имя класса
ic_class_name = 'icLEDNumberCtrl'

#   Спецификация на ресурсное описание класса
ic_class_spc = {'type': 'LEDNumberCtrl',
                'name': 'default',
                'child': [],
                'activate': True,
                '_uuid': None,
                'style': parentModule.LED_ALIGN_LEFT,

                '__styles__': LED_NUMBER_CTRL_STYLE,
                '__events__': {},
                '__lists__': {},
                '__attr_types__': {icDefInf.EDT_TEXTFIELD: ['description', '_uuid', 'default'],
                                   icDefInf.EDT_CHECK_BOX: ['draw_faded'],
                                   icDefInf.EDT_COLOR: ['foreground_colour', 'background_colour'],
                                   },
                '__parent__': SPC_IC_LEDNUMBERCTRL,
                }

#   Имя иконки класса, которые располагаются в директории
#   ic/components/user/images
ic_class_pic = bmpfunc.createLibraryBitmap('counter.png')
ic_class_pic2 = bmpfunc.createLibraryBitmap('counter.png')

#   Путь до файла документации
ic_class_doc = 'SCADA/doc/_build/html/SCADA.usercomponents.led_number_ctrl.html'
ic_class_spc['__doc__'] = ic_class_doc

#   Список компонентов, которые могут содержаться в компоненте
ic_can_contain = []

#   Список компонентов, которые не могут содержаться в компоненте, если не определен
#   список ic_can_contain
ic_can_not_contain = None

#   Версия компонента
__version__ = (0, 1, 1, 2)

class icLEDNumberCtrl(icwidget.icWidget, parentModule.LEDNumberCtrl):
    """
    Объект цифрового LED индикатора.

    :type component_spc: C{dictionary}
    :cvar component_spc: Спецификация компонента.

        - B{type='defaultType'}:
        - B{name='default'}:

    """
    component_spc = ic_class_spc

    def __init__(self, parent, id=-1, component=None, logType=0, evalSpace=None,
                 bCounter=False, progressDlg=None):
        """
        Конструктор базового класса пользовательских компонентов.

        :type parent: C{wx.Window}
        :param parent: Указатель на родительское окно.
        :type id: C{int}
        :param id: Идентификатор окна.
        :type component: C{dictionary}
        :param component: Словарь описания компонента.
        :type logType: C{int}
        :param logType: Тип лога (0 - консоль, 1- файл, 2- окно лога).
        :param evalSpace: Пространство имен, необходимых для вычисления внешних выражений.
        :type evalSpace: C{dictionary}
        :type bCounter: C{bool}
        :param bCounter: Признак отображения в ProgressBar-е. Иногда это не нужно -
            для создания объектов полученных по ссылки. Т. к. они не учтены при подсчете
            общего количества объектов.
        :type progressDlg: C{wx.ProgressDialog}
        :param progressDlg: Указатель на идикатор создания формы.
        """
        component = util.icSpcDefStruct(self.component_spc, component, True)
        icwidget.icWidget.__init__(self, parent, id, component, logType, evalSpace)

        #   По спецификации создаем соответствующие атрибуты (кроме служебных атрибутов)
        self.createAttributes(component)

        #   !!! Конструктор наследуемого класса !!!
        #   Необходимо вставить реальные параметры конструкора.
        #   На этапе генерации их не всегда можно определить.
        parentModule.LEDNumberCtrl.__init__(self, parent,
                                            pos=self.getPos(),
                                            size=self.getSize(),
                                            style=self.getStyle())
        # Установка свойств
        self.SetDrawFaded(self.getDrawFaded())
        if self.foreground_colour:
            self.SetForegroundColour(self.foreground_colour)
        if self.background_colour:
            self.SetBackgroundColour(self.background_colour)
        if self.default:
            self.setValue(self.default)

    def getPos(self):
        """
        Позиция.
        """
        x, y = self.getICAttr('position')
        return wx.Point(x, y)

    def getSize(self):
        """
        Размер компонента.
        """
        width, height = self.getICAttr('size')
        return wx.Size(width, height)

    def getStyle(self):
        """
        Стиль.
        """
        return self.getICAttr('style')

    def getDrawFaded(self):
        """
        Признак, будут ли неосвещенные сегменты отображаться
        с выцветшей версией цвета переднего плана.
        """
        return self.draw_faded

    def setValue(self, value):
        """
        Установить значение контрола.

        :param value: Значение контрола.
            Значение может быть следующих типов: целое, вещественное и строка.
            В противном случае все остальное приводится к виду строки.
        :return: True/False.
        """
        if not isinstance(value, str):
            value = str(value)

        self.SetValue(value)
        return True


import pygame
import random
import os

from enum import Enum

pygame.init()

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

class GameObject(pygame.sprite.Sprite):
    def __init__(self, image_file, position):
        super(GameObject, self).__init__()
        self._image = pygame.image.load(image_file)
        self.rect = self._image.get_rect()
        self.width, self.height = self._image.get_size()
        self.set_position(position)

    def set_position(self, position):
        self.position = position
        self.rect.left, self.rect.top = position

    def get_position(self):
        return self.rect.left, self.rect.top

    def draw(self, screen):
        screen.blit(self._image, (self.rect.left, self.rect.top))

class AnimatedGameObject(pygame.sprite.Sprite):
    def __init__(self, images_path, position, stop_time=0.1, running=False):
        super(AnimatedGameObject, self).__init__()

        self.images = [pygame.image.load(os.path.join(images_path, image_file)) for image_file in sorted(os.listdir(images_path))]
        self.index = 0
        self._image = self.images[self.index] # Start image for animation
        self.rect = self._image.get_rect()
        self.stop_time = stop_time
        self.elapsed_time = 0

        self.set_position(position)
        self.running = running

    def animate(self, dt):
        if not self.running:
            return False

        if self.index + 1 >= len(self.images):
            self.restart_anim()
            return False

        self.elapsed_time += dt
        if self.elapsed_time >= self.stop_time:
            self.elapsed_time = 0
            self.index += 1
            self._image = self.images[self.index]
            self.rect = self._image.get_rect()
            self.set_position(self.position)

        return True

    def set_position(self, position):
        self.position = position
        self.rect.left, self.rect.top = position

    def draw(self, screen):
        screen.blit(self._image, (self.rect.left, self.rect.top))

    def restart_anim(self):
        self.index = 0
        self.running = False

class Background(GameObject):
    def __init__(self, image_file, position=(0, 0)):
        GameObject.__init__(self, image_file, position)

    def draw(self, screen):
        GameScene.SCREEN.fill(WHITE)
        super(Background, self).draw(GameScene.SCREEN)

class Ball(GameObject):
    def __init__(self, image_file, position=(-100, 0), safe=True):
        GameObject.__init__(self, image_file, position)
        self.safe = safe

    def is_inside(self, bucket):
        bucket_x0, bucket_y0 = bucket.rect.left, bucket.rect.top
        bucket_x1 = bucket.rect.right

        ball_x0 = self.rect.left
        ball_x1, ball_y1 = self.rect.right, self.rect.bottom

        if ball_x0 >= bucket_x0 \
                and ball_x1 <= bucket_x1 \
                and ball_y1 - 25 >= bucket_y0 >= ball_y1 - 50:
            return True
        else:
            return False

class Bucket(GameObject):
    def __init__(self, image_file, position):
        GameObject.__init__(self, image_file, position)
        self.x = position[0]
        self.direction = Direction.NONE
        self.dx = 3.8

class BallManager:
    BALL_COUNT = 0
    def __init__(self):
        ball = Ball(image_file='ball_small.png')
        spiky_ball = Ball(image_file='spiky_ball_small.png', safe=False)

        self.balls = [ball, spiky_ball]
        self.ball_weights = {0: 3, 1: 1}
        self.ball_speed = 7
        self.ball_x = 0
        self.ball_y = 0
        self.ball = None

        self.reset_pos()

    def reset_pos(self):
        self.ball_x = random.randrange(0, 460)
        self.ball_y = random.randrange(-600, -200)

    def generate_ball(self):
        BallManager.BALL_COUNT += 1
        self.ball = self.balls[
            random.choice([x for x in self.ball_weights for y in range(self.ball_weights[x])])
        ]
        self.reset_pos()
        self.ball.set_position((self.ball_x, self.ball_y))
        return self.ball

    def update_pos(self):
        self.ball_y += self.ball_speed
        self.ball.set_position((self.ball_x, self.ball_y))

    def get_ball_in_scene(self):
        # Do not change anything here
        if self.ball_y > -20:
            return self.ball
        else:
            return None

class Direction(Enum):
    LEFT = -1
    NONE = 0
    RIGHT = 1

class GameScene:
    SCREEN = None
    FPS = 60

    def __init__(self, width, height):
        GameScene.SCREEN = pygame.display.set_mode((width, height))
        self.width = width
        self.height = height
        self.clock = pygame.time.Clock()
        self.font = pygame.font.SysFont("monospace", 16)
        self.score = 0
        self.lives = 3
        self.running = False
        self.time_elapsed = 0
        self.bucket = Bucket('bucket_trans.png', (width * 0.45, height * 0.8))
        self.bg = Background('bg.png')
        self.anim = AnimatedGameObject('BallCollectAnim', (-100, 0))
        self.ball_manager = BallManager()
        pygame.display.set_caption('Catch em all!')

    def process_events(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.running = False

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    self.bucket.direction = Direction.LEFT
                if event.key == pygame.K_RIGHT:
                    self.bucket.direction = Direction.RIGHT

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    self.bucket.direction = Direction.NONE

    def calculate_direction(self):
        # gets the ball visible on screen
        ball_in_scene = self.ball_manager.get_ball_in_scene()

        # TODO: Write your code here

        # Update self.direction based on ball_in_scene
        # ball_in_scene has attributes like - height, width, rect.top, rect.left
        # So does self.bucket
        # self.bucket.direction = Direction.NONE

    def display_score(self):
        text = self.font.render("Score: {0}".format(self.score), 2, BLACK)
        GameScene.SCREEN.blit(text, (5, 10))

    def display_lives(self):
        text = self.font.render("Lives: {0}".format(self.lives), 2, BLACK)
        GameScene.SCREEN.blit(text, (5, 34))

    def game_loop(self):
        self.running = True

        bucket_x, bucket_y = self.bucket.get_position()
        ball = self.ball_manager.generate_ball()

        while self.running:
            # Comment the below line to stop processing events from the user
            self.process_events()

            self.calculate_direction()

            dt = self.clock.tick(GameScene.FPS)
            self.time_elapsed += dt

            self.bg.draw(GameScene.SCREEN)
            self.ball_manager.update_pos()
            ball.draw(GameScene.SCREEN)

            ball_caught = ball.is_inside(self.bucket)

            if ball_caught:
                if not ball.safe:
                    self.lives -= 1
                else:
                    self.score += 1
                    self.anim.running = True
                    self.anim.set_position((ball.rect.left + ball.width // 2 - 10, self.bucket.rect.top + 5))
                ball = self.ball_manager.generate_ball()
            elif self.ball_manager.ball_y > self.height:
                if ball.safe:
                    self.lives -= 1
                ball = self.ball_manager.generate_ball()

            bucket_new_x = bucket_x + self.bucket.dx * self.bucket.direction.value
            if 0 <= bucket_new_x <= self.width - self.bucket.width:
                bucket_x = bucket_new_x
                self.bucket.set_position((bucket_x, bucket_y))
            self.bucket.draw(GameScene.SCREEN)

            if self.anim.animate(dt / 300):
                self.anim.draw(GameScene.SCREEN)

            self.display_score()
            self.display_lives()

            pygame.display.update()

            if self.lives is 0:
                self.running = False

if __name__ == '__main__':
    scene = GameScene(500, 600)
    scene.game_loop()
    print("Final score: {0}, Total time played: {1} secs, Total balls dropped: {2}"
          .format(scene.score, scene.time_elapsed/1000, BallManager.BALL_COUNT - 1))
    pygame.quit()
    quit()


from setuptools import setup, find_packages

from pathlib import Path

this_directory = Path(__file__).parent
# long_description = (this_directory / "README.md").read_text()

with open(this_directory / "README.md", encoding="utf8") as file:
    long_description = file.read()

VERSION = '0.1.10'
DESCRIPTION = 'Python generator project'

setup(
    name = 'pynumbat',
    packages = ['pynumbat'],
    entry_points={
        "console_scripts":
            ["pynumbat=pynumbat.__main__:main"]
    },
    include_package_data=True,
    version = VERSION,
    license='MIT',
    description = DESCRIPTION,
    long_description_content_type="text/markdown",
    long_description=long_description,
    author = 'Eduardo Ismael Garcia Perez',
    author_email = 'eduardo78d@gmail.com',
    url = 'https://github.com/eduardogpg/pygenerate',
    keywords = ['Python Generate', 'Generate', 'Project'],
    install_requires=[
        'click',
    ],
    classifiers=[
        'Development Status :: 3 - Alpha',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: MIT License',
        'Programming Language :: Python :: 3',
    ],
)


import pandas as pd
import datetime as dt
import os
from difflib import SequenceMatcher

# This module takes downloaded golf score data and parses from said data
# round scores for individual golfers and course data. This information is
# written to CSVs

# NOTES
# Data is curated such that certain events are excluded
# Exclusion parameters include limited data, course changes mid tournament, or
# odd formatting

#I'm sure this is a mess of optimizability

pd.set_option('display.expand_frame_repr', False)

def find_nth(haystack, needle, n):
    # Finds the nth of a string (needle) in a list (haystack)
    start = haystack.find(needle)
    while start >= 0 and n > 1:
        start = haystack.find(needle, start + len(needle))
        n -= 1
    return start

def similar(a,b):
    # Returns similarity ratio of strings
    return SequenceMatcher(None, a, b).ratio()

def main():

    frames = []
    courses = []
    golfer_dictionary = {}
    base_directory = r'C:\Users\Mitch\Projects\Golf\Data\Tournaments'
    for x in os.listdir(base_directory):
        print(x)
        file_object = open(file=base_directory+r"\\"+x, mode='r').read()
        x = 1
        golfers = []

        while find_nth(file_object, '', x) != -1:

            start_string = ''
            nth_string = find_nth(file_object, start_string, x)
            end_string_index = file_object[nth_string:].find('	')
            golfer = file_object[nth_string + len(start_string) + 21:nth_string + end_string_index]
            round_scores = file_object[nth_string + end_string_index + 35:nth_string + end_string_index + 240]

            first_round_score = round_scores[find_nth(round_scores, '>', 1) + 1:find_nth(round_scores, '<', 2)]
            second_round_score = round_scores[find_nth(round_scores, '>', 3) + 1:find_nth(round_scores, '<', 4)]
            third_round_score = round_scores[find_nth(round_scores, '>', 5) + 1:find_nth(round_scores, '<', 6)]
            fourth_round_score = round_scores[find_nth(round_scores, '>', 7) + 1:find_nth(round_scores, '<', 8)]

            if first_round_score != "":
                first_round_score = int(first_round_score)
            if second_round_score != "":
                second_round_score = int(second_round_score)
            if third_round_score != "":
                third_round_score = int(third_round_score)
            if fourth_round_score != "":
                fourth_round_score = int(fourth_round_score)

            golfers += [[golfer, first_round_score, second_round_score, third_round_score, fourth_round_score]]
            x += 1

        next_end = file_object[file_object.find("Ending:"):].find('<')
        date_string = file_object[file_object.find("Ending:") + len("Ending: "):file_object.find("Ending:") + next_end]
        year = int(date_string[find_nth(date_string, '/', 2) + 1:])
        month = int(date_string[:find_nth(date_string, '/', 1)])
        day = int(date_string[find_nth(date_string, '/', 1) + 1:find_nth(date_string, '/', 2)])
        fourth_date = dt.date(year, month, day)
        first_date = fourth_date - dt.timedelta(days=3)
        second_date = fourth_date - dt.timedelta(days=2)
        third_date = fourth_date - dt.timedelta(days=1)

        course_end = file_object[file_object.find("Course: "):].find('<')
        course_name = file_object[
                      file_object.find("Course: ") + len("Course: "):file_object.find("Course:") + course_end]
        par_end = file_object[file_object.find("PAR: "):].find('<')
        par = file_object[file_object.find("PAR: ") + len("PAR: "):file_object.find("PAR:") + par_end]

        if "amp;" in str(course_name):
            course_name = str(course_name.replace("amp;", ""))

        course = str(course_name), int(par)

        if course not in courses:
            courses += [course]

        start_index = file_object.find('PAST ') + 67
        end_index = file_object[start_index:].find("")

        tournament_name = file_object[start_index:start_index + end_index]

        for x in golfers:
            x.insert(1, tournament_name)
            x.insert(2, course_name)
            x.insert(3, str(first_date))
            x.insert(5, str(second_date))
            x.insert(7, str(third_date))
            x.insert(9, str(fourth_date))

        frames += golfers
    # These indices are hardcoded because I couldn't think of an effective way
    # to programatically find the indices for deletion. If/when this code is
    # rerun, it would be worth again trying to automate
    courses.sort(key=lambda x: x[1])
    delete_indices = [221,217,215,212,211,210,206,205,204,203,202,201,200,199,198,195,191,190,187,183,177,176,174,172,168,163,161,156,
            149,148,146,144,142,138,137,133,132,128,127,126,124,122,121,120,117,115,114,113,112,107,106,99,96,93,92,90,81,73,69,68,61,60,53,
            48,44,35,29,26,25,16,12,2]

    for x in delete_indices:
        del courses[x]

    course_pars_dictionary = {}
    for x in courses:
        course_pars_dictionary[x[0]] = x[1]

    cdf = pd.DataFrame(courses)
    cdf.to_csv(r'C:\Users\Mitch\Projects\Golf\Data\Courses.csv')

    for x in frames:
        golfer_dictionary[x[0]] = []
    for x in frames:
        golfer_dictionary[x[0]] += [x[1:]]

    dictionary_keys = list(golfer_dictionary.keys())

    for x in dictionary_keys:
        golfer_dictionary[x] = pd.DataFrame(golfer_dictionary[x])

    golfers_list = []
    for x in dictionary_keys:
        df = golfer_dictionary[x].transpose()
        for col in range(0,len(df.columns)):
            tournament = [x, df[col][0], df[col][1], df[col][2], df[col][3], df[col][4], df[col][5], df[col][6], df[col][7], df[col][8], df[col][9]]
            golfers_list += [tournament]

    for x in range(0,len(golfers_list)):
        max_similarity = 0
        max_string = ""
        for y in course_pars_dictionary.keys():
            if similar(golfers_list[x][2],y) > max_similarity:
                max_similarity = similar(golfers_list[x][2],y)
                max_string = y
        golfers_list[x][2] = max_string
        print(golfers_list[x])
        print(float(x)/float(len(golfers_list)))

    gdf = pd.DataFrame(golfers_list)
    gdf.to_csv(r'C:\Users\Mitch\Projects\Golf\Data\Golfers.csv')
    print(gdf)
    print(cdf)

if __name__ == "__main__":
    main()