####### LING78100 TERM PROJECT
####### PART 2: WORD COUNTING

import collections
import nltk
import os

from nltk.corpus import stopwords
stop_words = set(stopwords.words("english"))

# Function 1: retrieve total word counts for all speeches in a directory
def getWordCount(directory):
    allFileNames = os.listdir(directory)

    speech_count = collections.Counter()

    print("Reading Files")
    total_tokens = 0
    for fileName in allFileNames:

        with open(directory + fileName, "r",  encoding = "utf-16") as source:
            speech = source.read()
            speech = speech.casefold()
            sentences = nltk.sent_tokenize(speech)

            for i in range(len(sentences)):
                sentences[i] = nltk.word_tokenize(sentences[i])

            for i in range(len(sentences)):
                for j in range(len(sentences[i])):
                    if sentences[i][j] not in stop_words and sentences[i][j].isalpha():
                        speech_count[sentences[i][j]] += 1
                        total_tokens += 1

    print("Read Complete")
    print(f"Total tokens for {directory} is {total_tokens}")
    return speech_count

# Function 2: create new directory to write word counts to
def createDirAndWriteCounts(directory, file_name, speech_count):
    try:
        os.mkdir(directory)
    except OSError:
        print ("Creation of the directory %s failed" % directory)
    else:
        print ("Successfully created the directory %s " % directory)

    with open(directory + file_name, "w", encoding = "utf-16") as writer:
        for i in speech_count.most_common():
            writer.write(i[0])
            writer.write("\t")
            writer.write("%d" % i[1])
            writer.write("\n")

# Function 3: main function to organize data for 3 corpora
def main():
    # Bush speeches
    directory = "Bush/"
    bush_speeches_count = getWordCount(directory)
    directory = "BushWordCount/"
    file_name = "BushSpeechesWordCount.txt"
    createDirAndWriteCounts(directory, file_name, bush_speeches_count)

    # Obama speeches
    directory = "BarryO/"
    obama_speeches_count = getWordCount(directory)
    directory = "ObamaWordCount/"
    file_name = "ObamaSpeechesWordCount.txt"
    createDirAndWriteCounts(directory, file_name, obama_speeches_count)

    # Word count for control corpus (NLTK Reuters Library)
    count = collections.Counter()
    reuters_total_tokens = 0
    for fileid in nltk.corpus.reuters.fileids():
        control_corpus = nltk.corpus.reuters.raw(fileid)
        control_corpus = control_corpus.casefold()
        sentences = nltk.sent_tokenize(control_corpus)
        for i in range(len(sentences)):
            sentences[i] = nltk.word_tokenize(sentences[i])
        for i in range(len(sentences)):
            for j in range(len(sentences[i])):
                if sentences[i][j] not in stop_words and sentences[i][j].isalpha():
                    count[sentences[i][j]] += 1
                    reuters_total_tokens += 1
    directory = "Reuters/"
    file_name = "ReutersWordCount.txt"
    createDirAndWriteCounts(directory, file_name, count)

    print(f"Total tokens for Reuters library is {reuters_total_tokens}")

main()


#!/usr/bin/env python3

import os
import sys
from functools import reduce

# create absolute mydir
mydir = os.path.abspath(os.path.dirname(__file__))
sys.path.append(os.path.join(mydir, '../lib'))

from advent import *
from intcode import *

class Grid(object):

    def __init__(self, grid):
        self.grid = list(grid)
        self.gwidth = len(self.grid[0])
        self.pos = (0,0)
        self.count = 0

    def move(self, pos):
        self.pos = addpos(self.pos, pos)
        print(self.pos, self.get(self.pos))
        return self.get(self.pos)

    def get(self, pos):
        x, y = pos[0] % (self.gwidth - 1), pos[1]
        return self.grid[y][x]

if __name__ == '__main__':
    with open('day3.txt') as f:
        grid = Grid(f.readlines())
    counts = []
    for slope in ((1, 1), (3, 1), (5, 1), (7, 1), (1, 2)):
        grid.pos = (0, 0)
        count = 0
        while True:
            try:
                count += 1 if '#' == grid.move(slope) else 0
            except IndexError:
                counts.append(count)
                print(slope, count, grid.pos)
                break
    print(reduce(lambda x,y: x*y, counts))


from random import choice
import chess
class ChessEngine:
    def __init__(self):
        self.piece_values = {'P': 100, 'N': 280, 'B': 320, 'R': 479, 'Q': 929, 'K': 60000}
        self.pst = {
            'P': [
                [0,  0,  0,  0,  0,  0,  0,  0],
                [-6,  2,  3,  4,  4,  3,  2, -6],
                [-6,  2,  8, 12, 12,  8,  2, -6],
                [-6,  4, 12, 16, 16, 12,  4, -6],
                [-6,  6, 18, 24, 24, 18,  6, -6],
                [-6,  8, 24, 32, 32, 24,  8, -6],
                [-6, 12, 36, 48, 48, 36, 12, -6],
                [0,  0,  0,  0,  0,  0,  0,  0]
            ],
            'N': [
                [-50, -40, -30, -30, -30, -30, -40, -50],
                [-40, -20,   0,   0,   0,   0, -20, -40],
                [-30,   0,  10,  15,  15,  10,   0, -30],
                [-30,   5,  15,  20,  20,  15,   5, -30],
                [-30,   0,  15,  20,  20,  15,   0, -30],
                [-30,   5,  10,  15,  15,  10,   5, -30],
                [-40, -20,   0,   5,   5,   0, -20, -40],
                [-50, -40, -30, -30, -30, -30, -40, -50]
            ],
            'B': [
                [-20, -10, -10, -10, -10, -10, -10, -20],
                [-10,   0,   0,   0,   0,   0,   0, -10],
                [-10,   0,   5,  10,  10,   5,   0, -10],
                [-10,   5,   5,  10,  10,   5,   5, -10],
                [-10,   0,  10,  10,  10,  10,   0, -10],
                [-10,  10,  10,  10,  10,  10,  10, -10],
                [-10,   5,   0,   0,   0,   0,   5, -10],
                [-20, -10, -10, -10, -10, -10, -10, -20]
            ],
            'R': [
                [0,  0,  0,  5,  5,  0,  0,  0],
                [-5,  0,  0,  0,  0,  0,  0, -5],
                [-5,  0,  0,  0,  0,  0,  0, -5],
                [-5,  0,  0,  0,  0,  0,  0, -5],
                [-5,  0,  0,  0,  0,  0,  0, -5],
                [-5,  0,  0,  0,  0,  0,  0, -5],
                [5, 10, 10, 10, 10, 10, 10,  5],
                [0,  0,  0,  0,  0,  0,  0,  0]
            ],
            'Q': [
                [-20, -10, -10, -5, -5, -10, -10, -20],
                [-10,   0,   0,  0,  0,   0,   0, -10],
                [-10,   0,   5,  5,  5,   5,   0, -10],
                [-5,   0,   5,  5,  5,   5,   0,  -5],
                [0,   0,   5,  5,  5,   5,   0,  -5],
                [-10,   5,   5,  5,  5,   5,   0, -10],
                [-10,   0,   5,  0,  0,   0,   0, -10],
                [-20, -10, -10, -5, -5, -10, -10, -20]
            ],
            'K': [
                [-30, -40, -40, -50, -50, -40, -40, -30],
                [-30, -40, -40, -50, -50, -40, -40, -30],
                [-30, -40, -40, -50, -50, -40, -40, -30],
                [-30, -40, -40, -50, -50, -40, -40, -30],
                [-20, -30, -30, -40, -40, -30, -30, -20],
                [-10, -20, -20, -20, -20, -20, -20, -10],
                [20,  20,   0,   0,   0,   0,  20,  20],
                [20,  30,  10,   0,   0,  10,  30,  20]
            ]
        }
        self.pst_b = {piece: [row[::-1] for row in self.pst[piece]] for piece in self.pst}
        self.board = chess.Board()
        self.game_over = False
        self.turn = 'w'
        self.winner = None

    def minimax(self, depth, alpha, beta, isMaximizingPlayer, board=None):
        if(board is None):
            board = self.board.copy()

        if(depth == 0):
            return self.evaluate(board)

        if(isMaximizingPlayer):
            maxEval = -99999
            moves = self.get_ordered_moves(board)
            for move in moves:
                board.push(chess.Move.from_uci(move))
                value = self.minimax(depth - 1, alpha, beta, False, board)
                board.pop()
                maxEval = max(maxEval,value)
                alpha = max(alpha,value)
                if beta <= alpha:
                    return maxEval
            return maxEval

        else:
            minEval = 99999
            moves = self.get_ordered_moves(board)
            for move in moves:
                board.push(chess.Move.from_uci(move))
                value = self.minimax(depth - 1, alpha, beta, True, board)
                board.pop()
                minEval = min(minEval,value)
                beta = min(beta,value)
                if beta <= alpha:
                    return minEval
            return minEval

    def evaluate(self, board):
        white_material = self.count_material(chess.WHITE,board)
        black_material = self.count_material(chess.BLACK,board)
        pst_evaluation = self.calculate_pst_evaluation(chess.WHITE, board) - self.calculate_pst_evaluation(chess.BLACK, board)
        evaluation = white_material - black_material + pst_evaluation
        perspective = 1 if self.turn == 'w' else -1
        return perspective * evaluation

    def count_material(self, color, board):
        material = 0
        for square in chess.SQUARES:
            piece = board.piece_at(square)
            if piece is not None:
                if piece.color == color:
                    material += self.piece_values[piece.symbol().upper()]
                else:
                    material -= self.piece_values[piece.symbol().upper()]
        return material

    def calculate_pst_evaluation(self, color, board):
        pst_evaluation = 0
        for square in chess.SQUARES:
            piece = board.piece_at(square)
            if piece is not None:
                if piece.color == color:
                    rank = chess.square_rank(square)
                    file = chess.square_file(square)
                    pst_evaluation += self.pst[piece.symbol().upper()][rank][file]
                else:
                    rank = chess.square_rank(square)
                    file = chess.square_file(square)
                    pst_evaluation -= self.pst_b[piece.symbol().upper()][rank][file]
        return pst_evaluation

    def make_random_move(self):
        if self.game_over:
            return
        self.move = choice(self.valid_moves)

    def make_move(self, move):
        try:
            if self.game_over:
                return False

            if (move == None):
                return False

            if (move + 'q') in self.get_valid_moves(self.board):
                move = move + 'q'

            if move not in self.get_valid_moves(self.board):
                print('[!] Invalid move - ' + move)
                return False

            self.board.push(chess.Move.from_uci(move))

            if self.board.is_game_over():
                self.game_over = True
                if self.board.is_checkmate():
                    self.winner = 'w' if self.turn == 'b' else 'b'
                else:
                    self.winner = 'draw'
            self.turn = 'w' if self.turn == 'b' else 'b'
            return self.get_game_state()

        except Exception as e:
            self.board.pop()
            print('[!] Error making move - ' + move)
            print('[!] BEGIN ERROR MESSAGE')
            print(e)
            print('[!] END ERROR MESSAGE')

    def get_best_move(self,depth):
        try:
            board = chess.Board(self.get_game_state())
            board.turn = True if self.turn == 'w' else False
            bestMove = None
            bestValue = -99999
            alpha = -99999
            beta = 99999
            moves = self.get_ordered_moves(board)
            for move in moves:
                board.push(chess.Move.from_uci(move))
                value = self.minimax(depth-1,alpha,beta,False,board)
                board.pop()
                if value > bestValue:
                    bestValue = value
                    bestMove = move
            return bestMove
        except Exception as e:
            print('[!] Error getting best move')
            print('[!] BEGIN ERROR MESSAGE')
            print(e)
            print('[!] END ERROR MESSAGE')

    def get_game_state(self):
        return self.board.fen()

    def get_valid_moves(self, board):
        return [str(move) for move in board.legal_moves]

    def get_ordered_moves(self,board):
        moves = self.get_valid_moves(board)
        ordered_moves = sorted(
            moves,
            key=lambda move: (
                -self.get_captured_piece_value(move),
                self.get_attacker_piece_value(move)
            )
        )
        return ordered_moves

    def get_captured_piece_value(self, move):
        captured_square = chess.Move.from_uci(move).to_square
        captured_piece = self.board.piece_at(captured_square)
        if captured_piece is None:
            return 0
        return captured_piece.piece_type

    def get_attacker_piece_value(self, move):
        from_square = chess.Move.from_uci(move).from_square
        attacker_piece = self.board.piece_at(from_square)
        if attacker_piece is None:
            return 0
        return -attacker_piece.piece_type


def createSet(array):
    setList = set()
    for i in range(int(array[0]), int(array[1]) + 1):
        setList.add(i)
    return setList

file = open("day4_1.txt", "r")

count = 0
for line in file:
    actualLine = line.rstrip('\n')
    tasks = actualLine.split(',')
    elf1 = tasks[0].split('-')
    elf2 = tasks[1].split('-')
    elf1list = createSet(elf1)
    elf2list = createSet(elf2)
    if (elf1list.issubset(elf2list) == True or elf2list.issubset(elf1list) == True):
        count += 1

print(count, "is the answer")


tryTest = 0
success = False
while True:
    try:
        age = int(input('how old are you? '))
        print(age)
        raise ValueError('hey, cut it out')
    except ZeroDivisionError:
        print('Please enter a number > 0')
        tryTest += 1
        break
    else:
        print('ok')
        success = True
        break
    finally:
        if not success:
            print(f'you have {5 - tryTest} try again')


from queue import PriorityQueue
import sys

V, E = map(int, input().split())
K = int(input())
distance = [sys.maxsize] * (V+1)

visited = [False] * (V+1)
nearList = [[] for _ in range(V+1)]
q = PriorityQueue()

for _ in range(E):
    u, v, w = map(int, input().split())
    nearList[u].append((v, w))

q.put((0, K))  # 0을 먼저 사용한 이유는 바로 파이썬 에서는 앞에 있는 수  기준으로 배열이 정렬됨
distance[K] = 0

while q.qsize() > 0:
    current = q.get()
    c_v = current[1]
    if visited[c_v]:
        continue
    visited[c_v] = True

    for tem in nearList[c_v]:
        next = tem[0]
        value = tem[1]
        if distance[next] > distance[c_v] + value:
            distance[next] = distance[c_v] + value
            q.put((distance[next], next))

for i in range(1, V+1):
    if distance[i] != sys.maxsize:
        print(distance[i])
    else:
        print("INF")

# 로컬에서 작동시에는 예제출력 대로 출력이 나오나... 온라인에서 제출시에는 시간초과 나 런타임 에러 를 나타냄


# Задайте последовательность чисел. Напишите программу,
# которая выведет список неповторяющихся элементов исходной последовательности.

import random

def create_list(nums_amount: int, min: int, max: int):
    list = []
    for i in range(0, nums_amount):
        list.append(round(random.randint(min, max), 2))
    return list

def not_repeated_elements(list: list):
    new = []
    for i in range(0, len(list)):
        if list[i] not in new:
            new.append(list[i])
    return new

new_list = create_list(10, 0, 3)
print(f'Сгенерированный список: {new_list}')
sorted_list = not_repeated_elements(new_list)
print(f'Отсортированный список: {sorted_list}')


otroci = {
    "Adam": ["Matjaž", "Cilka", "Daniel"],
    "Aleksander": [],
    "Alenka": [],
    "Barbara": [],
    "Cilka": [],
    "Daniel": ["Elizabeta", "Hans"],
    "Erik": [],
    "Elizabeta": ["Ludvik", "Jurij", "Barbara"],
    "Franc": [],
    "Herman": ["Margareta"],
    "Hans": ["Herman", "Erik"],
    "Jožef": ["Alenka", "Aleksander", "Petra"],
    "Jurij": ["Franc", "Jožef"],
    "Ludvik": [],
    "Margareta": [],
    "Matjaž": ["Viljem"],
    "Petra": [],
    "Tadeja": [],
    "Viljem": ["Tadeja"],
}

def premozenje(oseba, denar):
    premozenje_otrok = 0
    for otrok in otroci[oseba]:
        premozenje_otrok += premozenje(otrok, denar)
    return denar[oseba] + premozenje_otrok

def najbogatejsi(oseba, denar):
    imax = oseba
    cmax = denar[oseba]
    for otrok in otroci[oseba]:
        i, c = najbogatejsi(otrok, denar)
        if c > cmax:
            imax = i
            cmax = c
    return (imax, cmax)


"""
Haystack search indexes for album app

Will be imported by haystack if haystack is used.
"""
from haystack import indexes  # pylint: disable=E0401

from .models import Album

class AlbumIndex(indexes.SearchIndex, indexes.Indexable):
    """
    Search index for entire albums
    """

    title = indexes.CharField(model_attr="title")
    created_date = indexes.DateTimeField(model_attr="created_date")
    text = indexes.CharField(document=True, use_template=True)

    def get_model(self):
        """Return corresponding model"""
        return Album


class Solution(object):
    def mergeKLists(self, lists):
        from heapq import heapify, heappush, heappop
        lists = lists[:]  # Avoid changing the original list
        heads_heap = [(lists[i].val, i) for i in range(len(lists)) if lists[i] is not None]
        heapify(heads_heap)

        rv = []
        while len(heads_heap) > 0:
            val, i = heappop(heads_heap)
            rv.append(val)
            lists[i] = next_node = lists[i].next
            if next_node is not None:
                heappush(heads_heap, (next_node.val, i))

        return rv


from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes

def digestMD5(user_input, byte_user):

	myhash = hashes.MD5()
	backend = default_backend()
	hasher = hashes.Hash(myhash, backend)
	hasher.update(byte_user)
	digest = hasher.finalize()
	print("Output of original data: ", user_input)
	return digest

def digestSHA1(user_input, byte_user):

	myhash = hashes.SHA1()
	backend = default_backend()
	hasher = hashes.Hash(myhash, backend)
	hasher.update(byte_user)
	digest = hasher.finalize()
	print("Output of original data: ", user_input)
	return digest

def digestSHA256(user_input, byte_user):

	myhash = hashes.SHA256()
	backend = default_backend()
	hasher = hashes.Hash(myhash, backend)
	hasher.update(byte_user)
	digest = hasher.finalize()
	print("Output of original data: ", user_input)
	return digest

def digestSHA384(user_input, byte_user):

	myhash = hashes.SHA384()
	backend = default_backend()
	hasher = hashes.Hash(myhash, backend)
	hasher.update(byte_user)
	digest = hasher.finalize()
	print("Output of original data: ", user_input)
	return digest

def digestSHA512(user_input, byte_user):

	myhash = hashes.SHA512()
	backend = default_backend()
	hasher = hashes.Hash(myhash, backend)
	hasher.update(byte_user)
	digest = hasher.finalize()
	print("Output of original data: ", user_input)
	return digest

def main():
	#MD5, SHA1, SHA256, SHA384, SHA512
	#Try for different size messages and with slight differences in the message - already did this. Can type in anything

	user_input = input("Please enter an input to convert to bytearray data: ")
	byte_user = bytearray(user_input.encode())

	digests = ['MD5', 'SHA1', 'SHA256', 'SHA384', 'SHA512']

	for i, item in enumerate(digests,1):
		print(i, '. ' + item + "\n")
	choose_digest = input("Please choose a digest from the list above (enter number): ")

	if choose_digest == '1':
		output = digestMD5(user_input, byte_user)
		print("Output of message digest: ", output)
	elif choose_digest == '2':
		output = digestSHA1(user_input, byte_user)
		print("Output of message digest: ", output)
	elif choose_digest == '3':
		output = digestSHA256(user_input, byte_user)
		print("Output of message digest: ", output)
	elif choose_digest == '4':
		output = digestSHA384(user_input, byte_user)
		print("Output of message digest: ", output)
	elif choose_digest == '5':
		output = digestSHA512(user_input, byte_user)
		print("Output of message digest: ", output)
	else:
		print("Sorry! Number is invalid! Please run program again and choose a number from the list.")

if __name__ == "__main__":
	main()


import numpy as np
import time
import os
import copy
import torch
import torch.nn as nn
import torch.optim as optim
import pickle
import pandas as pd

from torch.optim import lr_scheduler

from model import Mars_Spectrometry_Model
from dataset import Mars_Spectrometry_Dataset_Mono

def seed_everything(seed=42):
    random.seed(seed)
    os.environ["PYTHONHASHSEED"] = str(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    torch.cuda.manual_seed(seed)
    torch.backends.cudnn.deterministic = True

def train_model(device, model, criterion, optimizer, scheduler, dataloaders, num_epochs=25):
    since = time.time()

    best_model_wts = copy.deepcopy(model.state_dict())
    best_acc = 0.0
    best_loss = 0.0

    dataset_sizes = {x: len(dataloaders[x]) for x in ["train", "val"]}

    for epoch in range(num_epochs):
        print("Epoch {}/{}".format(epoch, num_epochs - 1))
        print("-" * 10)

        # each epoch has a training and validation phase
        for phase in ["train", "val"]:
            if phase == "train":
                model.train()  # set model to training mode
            else:
                model.eval()   # set model to evaluate mode

            y_true = []
            y_pred = []

            running_loss = 0.0
            running_corrects = 0

            # iterate over data.
            for inputs, labels in dataloaders[phase]:
                inputs = inputs.to(device)
                labels = labels.to(device)

                # zero the parameter gradients
                optimizer.zero_grad()

                # forward
                # track history if only in train
                with torch.set_grad_enabled(phase == "train"):
                    outputs = model(inputs)

                    loss = criterion(outputs, labels.unsqueeze(0))

                    # backward + optimize only if in training phase
                    if phase == "train":
                        loss.backward()
                        optimizer.step()

                preds = torch.round(torch.sigmoid(outputs))

                # statistics
                running_loss += loss.item() * inputs.size(0)
                running_corrects += torch.sum(preds == labels.data)

            epoch_loss = running_loss / dataset_sizes[phase]
            epoch_acc = running_corrects.double() / dataset_sizes[phase]

            print("{} Loss: {:.4f} Acc: {:.4f}".format(
                phase, epoch_loss, epoch_acc))

            if phase == "train" and scheduler is not None:
                scheduler.step()

            # deep copy the model
            if phase == "val" and epoch_acc > best_acc:
                best_acc = epoch_acc
                best_loss = epoch_loss
                best_model_wts = copy.deepcopy(model.state_dict())

        print()

    time_elapsed = time.time() - since
    print("Training complete in {:.0f}m {:.0f}s".format(
        time_elapsed // 60, time_elapsed % 60))
    print("Best val Acc: {:4f}".format(best_acc))

    # load best model weights
    model.load_state_dict(best_model_wts)

    return model, best_acc, best_loss

def train():
    seed_torch(42)

    compounds = "basalt", "carbonate", "chloride", "iron_oxide", "oxalate", "oxychlorine", "phyllosilicate", "silicate", "sulfate", "sulfide"
    compounds_loss = {}

    # select the device
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    print("Training on device:", device)

    for compound_index, compound in enumerate(compounds):
        print(f"Training for compound {compound}")

        # initialize custom dataset and transformations
        dataset_train = Mars_Spectrometry_Dataset_Mono("dataset/train_features.pickle", "dataset/train_labels.pickle", compound_index)
        dataset_val = Mars_Spectrometry_Dataset_Mono("dataset/val_features.pickle", "dataset/val_labels.pickle", compound_index)

        num_features = dataset_train.num_features
        num_labels = dataset_train.num_labels

        # code from phase 1 of the challenge where we didn't have validation labels
        """
        # number of validation samples (30% of training samples)
        num_val_samples = int(len(dataset_train) * 0.3)

        indices = torch.randperm(len(dataset_train)).tolist()
        dataset_train = torch.utils.data.Subset(dataset_train, indices[:-num_val_samples])
        dataset_val = torch.utils.data.Subset(dataset_val, indices[-num_val_samples:])
        """

        # define training and validation data loaders
        data_loader_train = torch.utils.data.DataLoader(
            dataset_train, batch_size=1, shuffle=True, num_workers=1)

        data_loader_val = torch.utils.data.DataLoader(
            dataset_val, batch_size=1, shuffle=False, num_workers=1)

        # set up dataloaders dict
        dataloaders = {
            "train": data_loader_train,
            "val": data_loader_val
        }

        # Initialize the model
        model = Mars_Spectrometry_Model(num_features, 1)
        model.to(device)

        # Define the loss function and optimizer
        criterion = nn.BCEWithLogitsLoss(reduction="none")
        optimizer = torch.optim.Adam(model.parameters(), lr=0.001, weight_decay=1e-5)
        #scheduler = lr_scheduler.ReduceLROnPlateau(optimizer, "min", patience=5, factor=0.8, min_lr=1e-8)
        scheduler = lr_scheduler.ExponentialLR(optimizer, gamma=0.1)

        model, best_acc, best_loss = train_model(device, model, criterion, optimizer, scheduler, dataloaders, num_epochs=25)

        # Process is complete.
        print(f"Training process for compound {compound} has finished")

        torch.save(model.state_dict(), f"checkpoints/compounds/model_BCEWithLogitsLoss_{compound}.ckpt")

        compounds_loss[compound] = best_loss

    print("Losses:")
    for compound in compounds:
        print(f"{compound} : {compounds_loss[compound]}")

    avg_loss = np.mean(list(compounds_loss.values()))
    print("\nAverage loss")
    print(avg_loss)

if __name__ == "__main__":
    model = train()


import random
import pygame
import anims
import datastructs
import threading
import time
from pygame import mixer
clock = pygame.time.Clock()
WIDTH, HEIGHT = 1000, 600
FONTSIZE = 12
SECONDFONTSIZE = 15
PLAYERLENGTH = FONTSIZE+3
pygame.display.set_caption("Game")
screen = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.init()
#---------------------------------------------Threads:
thread_list = []
def add_thread(thread):
    global thread_list
    thread_list.append(thread)
def end_threads():
    global thread_list
    pass

#FONT------------------------------------------
font = pygame.font.SysFont("Consolas", FONTSIZE)
second_font = pygame.font.SysFont("Consolas", SECONDFONTSIZE)
player_text = font.render('', True,"white")
dialogue_text = font.render('', True,"white")
player_color = (255,255,255)
#PLAYER RENDERING.
def render_multi_line(text, x, y, fsize):
    lines = text.splitlines()
    global player_text
    for i, l in enumerate(lines):
        #screen.blit(font.render(l, 0, "white"), (x, y + fsize * i))
        player_text = font.render(l, 0, player_color)
        screen.blit(player_text, (x, y + fsize * i))

####################################-------------
#-------------------TYPE WRITER-----------------#
# type writer is going to be using a dialogue queue
# methods that want stuff to be written on the screen
# add their texts to the type writing que.
#
###########################################---Thread based code here.
# current_dialogue = current_dialogue + text[current_dialougue_num]
#             dialogue_text = second_font.render(current_dialogue, 0, "yellow")

dialPos = (20, 500)

screen_typing_q = datastructs.queue()
dialogue_thread_working = False
dialogue_delay = 1.5  # secs
dialogue_char_delay = 0.05  # secs
def addDialogue(text):
    global dialogue_thread_working
    global screen_typing_q
    screen_typing_q.add_to_q(text)
    if dialogue_thread_working is True:
        return
    t = threading.Thread(target=typeWriteThreadManager)
    add_thread(t)
    dialogue_thread_working = True
    t.start()
def typeWriteThreadManager():
    print("entered manager dialogue")
    global dialogue_text
    global dialogue_delay
    global screen_typing_q
    global dialogue_thread_working
    popped_text = screen_typing_q.pop_from_q()
    while popped_text is not None:
        typeWrite(popped_text)
        time.sleep(dialogue_delay)
        popped_text = screen_typing_q.pop_from_q()
    dialogue_thread_working = False
    dialogue_text = second_font.render('', 0, "yellow")
def typeWrite(text):
    global dialogue_text
    global dialogue_char_delay
    for i in range(len(text)):
        dialogue_text = second_font.render(text[0:i+1], 0, "yellow")
        if text[i] != ' ':
            time.sleep(dialogue_char_delay)

    return

#----------------------------------------------
####################################################-------------
####################################################-------------
#-------------PLAYER------------------------#

normal_color = 'white'
crazyColor = False
player_pos_x = 200
player_pos_y = 200

#for collisions and more
real_player_pos_x = 200
real_player_pos_y = 200
#-----------------------
move_cooldown = 1
strech_cooldown = 1
color_cooldown = 8

#----Speed------
movement_speed = 5
strech_speed = 3
#-----------------
max_strech = PLAYERLENGTH * 3
def player_move(key):
    global player_pos_x
    global player_pos_y
    global real_player_pos_x
    global real_player_pos_y
    if pygame.time.get_ticks() % move_cooldown == 0:
        if key == pygame.K_RIGHT:
            player_pos_x += movement_speed
        if key == pygame.K_LEFT:
            player_pos_x -= movement_speed
        if key == pygame.K_UP:
            player_pos_y -= movement_speed
        if key == pygame.K_DOWN:
            player_pos_y += movement_speed
    real_player_pos_x = player_pos_x
    real_player_pos_y = player_pos_y

def playerStrech(key):
    global player_pos_y
    global PLAYERLENGTH
    global crazyColor
    if PLAYERLENGTH > max_strech:
        PLAYERLENGTH -= strech_speed
        player_pos_y += strech_speed*4
        return
    if PLAYERLENGTH < -max_strech:
        PLAYERLENGTH += strech_speed
        player_pos_y -= strech_speed*4
        return
    if abs(PLAYERLENGTH) > max_strech*0.75:
        crazyColor = True
    else:
        crazyColor = False
    if pygame.time.get_ticks() % strech_cooldown == 0:
        if key == pygame.K_w:
            player_pos_y -= strech_speed*4
            PLAYERLENGTH += strech_speed
        if key == pygame.K_s:
            player_pos_y += strech_speed*4
            PLAYERLENGTH -= strech_speed

############animation#######################################
#######################################---------------------
#######   PLAYER WALK ANIMATION   ##########-----------------

strToRenderWalk = anims.player_walk[0]
last_player_walk = 0
j = 0
def player_shoot_image():
    global strToRenderWalk
    strToRenderWalk = anims.player_hand_strech
def player_idle():
    global strToRenderWalk
    strToRenderWalk = anims.player_walk[0]
def player_walk_anim():
    global last_player_walk
    global j
    global strToRenderWalk
    # print(j)
    now = pygame.time.get_ticks()
    cooldown = 50
    if now - last_player_walk > cooldown:
        if j >= 5:
            j = 0
        j += 1
        strToRenderWalk = anims.player_walk[j]
        last_player_walk = now
def playerColor(spec):
    global player_color
    if pygame.time.get_ticks() % color_cooldown == 0:
        if spec is None:
            player_color = (random.randrange(50,250),random.randrange(50,250),random.randrange(50,250))
        else:
            player_color = spec

#####----------------ARROW------------
arrow = None
arrow_pos = [real_player_pos_x,real_player_pos_y]
can_throw_arrow = True
def throwArrow():
    global arrow_pos

    if can_throw_arrow:
        arrow_pos = [player_pos_x,player_pos_y]
        t = threading.Thread(target=arrow_throw)
        t.start()

def arrow_throw():
    global arrow_pos
    global arrow
    global can_throw_arrow
    can_throw_arrow = False
    t = threading.Thread(target=alter_arrow_pos)
    t.start()
    color = 0
    for i in range(20):
        color += 1
        h = 50+color*10 if 50+color*10 < 255 else 255
        h2 = 255-color*10 if 255-color*10 < 0 else 0
        arrow = font.render(anims.arrow_throw[i%4], 0, (0,h,h2))
        time.sleep(0.05)
    can_throw_arrow = True
    for i in range(len(anims.arrow_burned)):
        h = 50 + color * 10 if 50 + color * 10 < 255 else 255
        h2 = 255 - color * 10 if 255 - color * 10 < 0 else 0
        arrow = font.render(anims.arrow_burned[i % len(anims.arrow_burned)], 0, (0, h, h2))
        time.sleep(0.1)
        color-=1
    player_idle()
    return
def alter_arrow_pos():
    global can_throw_arrow
    while can_throw_arrow is False:
        arrow_pos[0] += 1
        time.sleep(0.001)
    return

############################################--------------------
######### KEYS #############################--------------------

def keysAction():
    pressed = pygame.key.get_pressed()
    ###PRESSED:
    ##MOVEMENT-------
    if pressed[pygame.K_RIGHT]:
        player_walk_anim()      # the movement animation
        player_move(pygame.K_RIGHT)     # the general movement method
    if pressed[pygame.K_LEFT]:
        player_walk_anim()
        player_move(pygame.K_LEFT)
    if pressed[pygame.K_UP]:
        player_walk_anim()
        player_move(pygame.K_UP)
    if pressed[pygame.K_DOWN]:
        player_walk_anim()
        player_move(pygame.K_DOWN)
    #STRECHING
    if pressed[pygame.K_w]:
        playerStrech(pygame.K_w)
    if pressed[pygame.K_s]:
        playerStrech(pygame.K_s)
    #-------------------------
    ##EVENTS:
    for event in pygame.event.get():
        #QUIT
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
        #KEY DOWN----------------------------
        if event.type == pygame.KEYDOWN:
            #Escape
            if event.key == pygame.K_ESCAPE:
                pygame.quit()
                exit()
            if event.key == pygame.K_SPACE:
                addDialogue("hello i'm a robot")
                addDialogue("press Q to see me shooting lasers")
                addDialogue("...")
            # 2.shoot arrow
            if event.key == pygame.K_q:
                throwArrow()
                player_shoot_image()
            # ----MOVEMENT----
        #KEY UP---------------------------
        if event.type == pygame.KEYUP:
            #----MOVEMENT----
            #1.Right
            if event.key == pygame.K_RIGHT or pygame.K_LEFT:
                player_idle()

#MUSIC-----------------------------------------

################################################---------------
###################################----------------------------

def printeverysec(text):
    for i in range(len(text)):
        print(text[i])
        time.sleep(4)

# x = threading.Thread(target=printeverysec, args=('hello worlddd',))
# x.start()

if __name__ == '__main__':
    while True:
        screen.fill((23, 28, 59))
        #things that update-------------------
        keysAction()
       # print(pygame.time.get_ticks())
        #player:---------------------------------
        if crazyColor:
            playerColor(None)
        else:
            playerColor(normal_color)
        render_multi_line(strToRenderWalk, player_pos_x, player_pos_y, PLAYERLENGTH)
        # dialogueTypewrite("Hello every one, my name is robot man 2")
        #----------------------------------------
        # pygame.display.flip()
        #DIALOGUE:------
        screen.blit(dialogue_text, dialPos)
        if arrow is not None:
            screen.blit(arrow, (arrow_pos[0], arrow_pos[1]))
        # print(screen_typing_q.size)
        #---------------
        pygame.display.update()
        clock.tick(60)


#!/usr/bin/env python3

def showInstructions():
	print('''
	RPG game
	========
	Commands:
		go [direction]
		get [item]
		drop [item]
		use [item]
		Enter 'q' to QUIT
	''')

def showStatus(rooms, inventory, currentroom):

	print('---------------------')
	print('You are in the ' + currentroom)
	print('Inventory: ' + str(inventory))
	#print(rooms[currentroom]['item'])
	if "item" in rooms[currentroom]:
		for i in rooms[currentroom]['item']:
			print('You see a ' + i)

	print('---------------------')

def play(rooms, inventory, currentroom):
	showInstructions()

	while True:

		showStatus(rooms,inventory,currentroom)
		move = ''
		battle = ''
		while move == '':
			move = input('> ')

		move = move.lower().split()

		if move[0] == 'go':
			if move[1] in rooms[currentroom]:
				currentroom = rooms[currentroom][move[1]]

				if "boss" in rooms[currentroom]:
					print('''
						BATTLE MODE!

						MAKE YOUR MOVE
						1- use [item]
						2- run [direction] ''')

					while battle=='':
						battle = input('>')
					battle = battle.lower().split()

					if battle[0] == 'run':
						if battle[1] in rooms[currentroom]:
							print("Running away....")
							currentroom = rooms[currentroom][battle[1]]
						else:
							print("YOU CAN'T DO THAT!!")
							battle = ''
			else:
				print("You can't go that way!")

		if move[0] == 'get':
			if "item" in rooms[currentroom]:
				for i in range(len(rooms[currentroom]['item'])):
					if move[1] == rooms[currentroom]['item'][i]:
						inventory += [move[1]]
						print(move[1] + ' got!')
						del rooms[currentroom]['item'][i]
						break
			else:
				print("Can't get " + move[1] + '!')

		if move[0] == 'drop':
			if move[1] in inventory:
				print('You droped your item here')
				inventory.remove(move[1])
				rooms[currentroom]['item'].append(move[1])

			else:
				print("No such item in inventory!")

		if move[0] == 'q':
			break

def main():
	currentroom = 'Hall'
	inventory = []
	rooms = {
		''
		'Hall': {
			'south':'Kitchen',
			'north':'Bathroom',
			'east':'Dinning Room',
			'item':[]
			},

		'Bathroom': {
			'south':'Hall',
			'item':['plunger']
			},

		'Kitchen': {
			'north':'Hall',
			'item':[],
			'boss':'monster'
			},

		'Dinning Room':{
			'west':'Hall',
			'south':'Garden',
			'item': ['cookie']
			},

		'Garden':{
			'north':'Dinning Room',
			'east':'Dungeon',
			'item' :['sword']
			},

		'Dungeon':{
			'west':'Garden',
			'boss':'dragon',
			'item':[]

			}
		}

	play(rooms, inventory, currentroom)

main()


admin_host = "http://localhost"
"""
:var admin_host: The URL where the Hyperledger Composer admin REST API is hosted, without trailing slash.
:vartype admin_host: str
"""

admin_port = 3001
"""
:var admin_port: The port where the Hyperledger Composer REST API listens to admin requests.
				 If the port is `None`, it is not used.
				 Only the users with the 'admin' scope can use this port.
:vartype admin_port: int or None
"""

multiuser_host = "http://localhost"
"""
:var multiuser_host: The URL where the Hyperledger Composer multi-user REST API is hosted, without trailing slash.
:vartype multiuser_host: str
"""

multiuser_port = 3000
"""
:var multiuser_port: The port where the Hyperledger Composer REST API listens to multi-user requests.
					 If the port is `None`, it is not used.
:vartype multiuser_port: int or None
"""

multi_card = True
"""
:var multi_card: A flag that determines whether a multi-card workflow is followed.
				 If set to true, then each participant will have one identity for each study that they ever participate in.
				 Otherwise, a new identity is generated the first time they participate in a study.
				 Later, the same identity would be used over and over again.
:vartype multi_card: bool
"""

verify = True if admin_host.lower().startswith('https') or multiuser_host.lower().startswith('https') else False
"""
:var verify: A boolean indicating whether requests to the blockchain should verify the certificate.
			 By default, HTTPS requests verify the certificate.
			 The only case when HTTPS requests do not need to be verified is when they use self-signed certificates.
:vartype verify: bool
"""


import wikipediaapi

# Can probably do more about th config and also separate this out
wiki_client = wikipediaapi.Wikipedia('en')

def query_wiki(ctx, query):
    str_query = ''.join(query)
    str_query.replace("_", " ")
    wiki_page = wiki_client.page(str_query)
    if wiki_page.exists():
        return wiki_page.fullurl.format(ctx.message)
    else:
        return 'This page does not exist bruh.'.format(ctx.message)


from gi.repository import Gtk
from gi.repository import GObject

from matplotlib.figure import Figure
from matplotlib.backends.backend_gtk3agg import FigureCanvasGTK3Agg
from matplotlib.backends.backend_gtk3 import NavigationToolbar2GTK3

from filetab import FileTab
from plottab import PlotTab
from masktab import MaskTab
from centeringtab import CenteringTab
from integratetab import IntegrateTab

from .. import maskmaker
from .. import libconfig
from sastool.classes import SASMask

class GUIStopFittingException(Exception):
    pass

import sys
import traceback

class SASImageGuiExceptHook():
    oldexcepthook = None
    def __init__(self):
        if isinstance(sys.excepthook, self.__class__):
            raise RuntimeError('SASImageGuiExceptHook has already been installed.')
        self.oldexcepthook = sys.excepthook
        sys.excepthook = self
    def __call__(self, type_, value, traceback_):
        dialog = Gtk.MessageDialog(None, Gtk.DialogFlags.MODAL, Gtk.MessageType.ERROR, Gtk.ButtonsType.OK,
                                   str(type_) + ': ' + str(value))
        dialog.format_secondary_text('Traceback:')
        msgarea = dialog.get_message_area()
        sw = Gtk.ScrolledWindow()
        sw.set_size_request(200, 300)
        msgarea.pack_start(sw, True, True, 0)
        tv = Gtk.TextView()
        sw.add(tv)
        tv.get_buffer().set_text('\n'.join(traceback.format_tb(traceback_)))
        tv.set_editable(False)
        tv.set_wrap_mode(Gtk.WrapMode.WORD)
        # tv.get_default_attributes().font = Pango.FontDescription('serif,monospace')
        tv.set_justification(Gtk.Justification.LEFT)
        msgarea.show_all()
        dialog.set_title('Error!')
        dialog.run()
        dialog.destroy()
        return None
        # self.oldexcepthook(type_, value, traceback)
    def __del__(self):
        # THIS IS NOT QUITE OK: other excepthooks registered after SASImageGuieExceptHook will
        # be quietly removed.
        if self.oldexcepthook is not None:
            sys.excepthook = self.oldexcepthook

class SASImageGuiMain(Gtk.Window):
    _instances = []
    def __init__(self):
        SASImageGuiMain._instances.append(self)
        try:
            SASImageGuiMain._excepthook = SASImageGuiExceptHook()
        except RuntimeError:
            pass
        Gtk.Window.__init__(self, Gtk.WindowType.TOPLEVEL)

        self.connect('delete-event', self.on_delete)
        self.fig = Figure(figsize=(0.5, 0.4), dpi=72)
        self.axes = self.fig.add_subplot(111)

        vbox = Gtk.VBox()
        self.add(vbox)
        self.ribbon = Gtk.Notebook()
        vbox.pack_start(self.ribbon, False, True, 0)
        self.ribbon.set_scrollable(True)
        self.ribbon.popup_enable()

        self.ribbon_File = FileTab()
        self.ribbon.append_page(self.ribbon_File, Gtk.Label(label='File'))
        self.ribbon.set_tab_detachable(self.ribbon_File, True)
        self.ribbon_File.connect('new-clicked', self.on_file, 'newwindow')
        self.ribbon_File.connect('close-clicked', self.on_file, 'closewindow')
        self.ribbon_File.connect('quit-clicked', self.on_file, 'quitprogram')
        self.ribbon_File.connect('opened', self.on_file_opened)

        self.ribbon_Plot = PlotTab()
        self.ribbon.append_page(self.ribbon_Plot, Gtk.Label(label='Plot'))
        self.ribbon_Plot.connect('clear-graph', self.on_plot, 'clear-graph')
        self.ribbon_Plot.connect('refresh-graph', self.on_plot, 'refresh-graph')
        self.ribbon_Plot.connect('plotparams-changed', self.on_plot, 'refresh-graph')

        self.ribbon_Mask = MaskTab()
        self.ribbon.append_page(self.ribbon_Mask, Gtk.Label(label='Mask'))
        self.ribbon_Mask.connect('new-mask', self.on_mask, 'new-mask')
        self.ribbon_Mask.connect('edit-mask', self.on_mask, 'edit-mask')

        self.ribbon_Centering = CenteringTab()
        self.ribbon.append_page(self.ribbon_Centering, Gtk.Label(label='Centering'))
        self.ribbon_Centering.connect('crosshair', self.on_center)
        self.ribbon_Centering.connect('docentering', self.on_center)
        self.ribbon_Centering.connect('manual-beampos', self.on_center)

        self.ribbon_Integrate = IntegrateTab()
        self.ribbon.append_page(self.ribbon_Integrate, Gtk.Label(label='Integrate'))
        self.ribbon_Integrate.connect('integration-done', self.on_integration_done)

        for k in self.ribbon.get_children():
            self.ribbon.set_tab_detachable(k, True)
            self.ribbon.set_tab_reorderable(k, True)
            k.connect('error', self.on_error)

        self.canvas = FigureCanvasGTK3Agg(self.fig)
        self.canvas.set_size_request(800, 400)
        vbox.pack_start(self.canvas, True, True, 0)

        self.graphtoolbar = NavigationToolbar2GTK3(self.canvas, vbox)
        vbox.pack_start(self.graphtoolbar, False, True, 0)

        self.statusbar = Gtk.Statusbar()
        vbox.pack_start(self.statusbar, False, True, 0)
        self.show_all()
        self.hide()

    def on_file(self, widget, argument):  # IGNORE:W0613
        if argument == 'newwindow':
            newinstance = SASImageGuiMain()
            newinstance.show_all()
        elif argument == 'closewindow':
            self.destroy()
            self.on_delete(None, None)
        elif argument == 'quitprogram':
            Gtk.main_quit()
        return True
    def on_delete(self, widget, event, *args):  # IGNORE:W0613
        SASImageGuiMain._instances.remove(self)
        if not SASImageGuiMain._instances:
            Gtk.main_quit()
        return False
    def on_file_opened(self, widget, data):  # IGNORE:W0613
        self.statusbar.remove_all(self.statusbar.get_context_id('sastool'))
        self.statusbar.push(self.statusbar.get_context_id('sastool'), 'File opened.')
        self.ribbon_Plot.update_from_data(data)
        self.ribbon_Integrate.update_from_data(data)
        self.on_plot(None, 'refresh-graph')
        return True
    def on_error(self, widget, error):  # IGNORE:W0613
        self.statusbar.remove_all(self.statusbar.get_context_id('sastool'))
        self.statusbar.push(self.statusbar.get_context_id('sastool'), 'Error: ' + unicode(error))
        return True
    def on_plot(self, widget, argument):  # IGNORE:W0613
        if argument == 'clear-graph':
            self.fig.clf()
            self.axes = self.fig.add_subplot(1, 1, 1)
        if argument == 'refresh-graph':
            self.axes.cla()
            self.ribbon_Plot.plot2d(self.data, axes=self.axes)
        self.canvas.draw_idle()
        return True
    def get_data(self):
        return self.ribbon_File.data
    def on_mask(self, widget, argument):  # IGNORE:W0613
        if self.data is None:
            return False
        if argument == 'new-mask':
            self.data.set_mask(self.data.Intensity * 0 + 1)
        elif argument == 'edit-mask':
            mm = maskmaker.MaskMaker(parent=self, matrix=self.ribbon_Plot.lastplotraw, mask=self.data.mask)
            if mm.run() == Gtk.ResponseType.OK:
                self.data.set_mask(SASMask(mm.get_mask(), maskid=mm.maskid))
            mm.destroy()
        self.ribbon_Integrate.update_from_data(self.data)
        self.on_plot(None, 'refresh-graph')
    def on_center(self, widget, method, args):  # IGNORE:W0613
        if method == 'crosshair':
            ax = self.axes.axis()
            centercoords = (0.5 * (ax[2] + ax[3]), 0.5 * (ax[0] + ax[1]))
            if self.ribbon_Plot.get_axesunits() == 'q':
                centercoords = self.data.qtopixel(*centercoords)
            method = 'manual clicking'
        elif method == 'manual-beampos':
            centercoords = args[:2]
            method = 'manual setting'
        elif hasattr(self.data, 'find_beam_' + method):
            dialog = Gtk.Dialog('Finding beam center...', self.get_toplevel(), Gtk.DialogFlags.MODAL | Gtk.DialogFlags.DESTROY_WITH_PARENT,
                              (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL))
            dialog.set_default_response(Gtk.ResponseType.CANCEL)
            l = Gtk.Label('Centering, please wait...'); l.set_alignment(0, 0.5)
            dialog.get_content_area().pack_start(l, True, True, 0)
            pb = Gtk.ProgressBar()
            dialog.get_content_area().pack_start(pb, True, True, 0)
            def closedialogfunction(*args, **kwargs):  # IGNORE:W0613
                dialog.hide()
            dialog.get_widget_for_response(Gtk.ResponseType.CANCEL).connect('clicked', closedialogfunction)
            dialog.connect('delete-event', closedialogfunction)
            dialog.show_all()
            while Gtk.events_pending():
                Gtk.main_iteration_do(False)
            def callbackfunction():
                pb.pulse()
                while Gtk.events_pending():
                    Gtk.main_iteration_do(False)
                if not dialog.get_visible():
                    raise GUIStopFittingException
            try:
                centercoords = self.data.__getattribute__('find_beam_' + method).__call__(*args, callback=callbackfunction)
            except:
                if dialog.get_visible():
                    raise
                centercoords = None
            finally:
                dialog.destroy()
        else:
            raise ValueError('Invalid beam finding mode: ' + method)
        if centercoords is not None:
            self.data.update_beampos(centercoords, method)
            self.ribbon_Centering.set_beampos(centercoords)
            self.ribbon_Integrate.update_from_data(self.data)
            self.on_plot(None, 'refresh-graph')
    def on_integration_done(self, widget, curve, retmask, mode, radiustype):  # IGNORE:W0613
        self.fig.clf()
        self.axes = self.fig.add_subplot(1, 2, 1)
        mask1 = self.data.mask
        try:
            self.data.set_mask(retmask == 0)
            self.on_plot(None, 'refresh-graph')
            axes2 = self.fig.add_subplot(1, 2, 2)
            if mode == 'Azimuthal':
                curve.semilogy(axes=axes2)
                axes2.set_xlabel('Azimuth angle (deg.)')
                axes2.set_ylabel('Intensity')
            else:
                curve.loglog(axes=axes2)
                axes2.set_xlabel(u'q ('+libconfig.qunit()+')')
                axes2.set_ylabel('Intensity')
            self.fig.canvas.draw()
        finally:
            self.data.set_mask(mask1)
        return True

    data = property(get_data)


#! /usr/bin/env python
import os

import tables
import numpy as np
from configobj import ConfigObj

"""link stuff to reduce duplication"""

def main(h5file):
    conf_dict = ConfigObj('.h5.conf')
    h5f = tables.openFile(h5file, 'a')

    SEQS = conf_dict['systems']['SEQS']
    CDTS = conf_dict['systems']['CDTS']
    NUMS = conf_dict['systems']['NUMS']

    g = h5f.getNode(os.path.join('/', 'unv', 'ogd'))
    tablename_pattern = '{seq}{cdt}300s{num}'

    for seq in SEQS:
        for cdt in CDTS:
            for num in NUMS:
                tablename = tablename_pattern.format(seq=seq, cdt=cdt, num=num)
                if g.__contains__(tablename):
                    pass
                else:
                    # same tablename
                    target_table = h5f.getNode(os.path.join('/', 'unvp', 'ogd', tablename))
                    t = h5f.createSoftLink(g, tablename, target_table)
    h5f.close()

if __name__ == "__main__":
    h5file = '../mono_meo.h5'
    main(h5file)


from typing import Any, Dict, List, Optional, Self, TypeAlias

import numpy as np

import rombus._core.mpi as mpi

from rombus.model import RombusModel
from rombus._core import hdf5

DEFAULT_TOLERANCE = 1e-14
DEFAULT_REFINE_N_RANDOM = 100

Sample: TypeAlias = np.ndarray

class Samples(object):
    """Class for managing sets of parameter samples for Rombus."""

    def __init__(
        self, model: RombusModel, filename: Optional[str] = None, n_random: int = 0
    ):

        self.model: RombusModel = model
        """Rombus model for which the samples are computed"""

        self.n_random: int = n_random
        """Number of random points generated for this set"""

        # RNG current and initial state
        self._random: Optional[np.random._generator.Generator] = None
        """The numpy random number generator instance used for generating any random numbers in this Sample set"""
        self._random_starting_state: Optional[Dict[str, Any]] = None
        """The starting state of the generator"""

        # Initialise samples
        self.n_samples: np.int32 = np.int32(0)
        """Number of samples in this set."""
        self.samples: List[Sample] = []
        """List of samples in this set."""

        if filename:
            self._add_from_file(filename)
        if self.n_random > 0:
            self._add_random_samples(self.n_random)

    def __str__(self):
        result = f"\nContents of instance of {type(self)}:\n"
        result = result + "   model:    " + str(self.model) + "\n"
        result = result + "   n_random: " + str(self.n_random) + "\n"
        result = result + "   n_samples:" + str(self.n_samples) + "\n"
        result = result + "   samples:  " + str(self.samples)

        return result

    @classmethod
    def from_file(cls, file_in: hdf5.FileOrFilename) -> Self:
        """Create an instance of a Sample set from a Rombus file on disk.

        Parameters
        ----------
        file_in : hdf5.FileOrFilename
            Rombus file (filename or opened file) to read from

        Returns
        -------
        Self
            Returns a reference to self so that method calls can be chained
        """

        h5file, close_file = hdf5.ensure_open(file_in)
        model_str = h5file["samples/model/model_str"].asstr()[()]
        model = RombusModel.load(model_str)
        samples = cls(model)
        samples.samples = [np.array(x) for x in h5file["samples/samples"]]
        samples.n_samples = np.int32(h5file["samples/n_samples"])
        if close_file:
            h5file.close()
        return samples

    def extend(self, new_samples: List[Sample]) -> None:
        """Add additional samples to the set.

        Parameters
        ----------
        new_samples : List[Sample]
            A list of new samples
        """

        self.samples.extend(new_samples)
        self.n_samples = self.n_samples + len(new_samples)

    def write(self, h5file: hdf5.File):
        """Save samples to an open HDF5 file.

        Parameters
        ----------
        h5file : hdf5.File
            An open HDF5 file
        """

        h5_group = h5file.create_group("samples")
        self.model.write(h5_group)
        h5_group.create_dataset("samples", data=self.samples)
        h5_group.create_dataset("n_samples", data=self.n_samples)

    def _add_from_file(self, filename_in: str) -> None:
        """Add samples from file to this set.  Accepts Numpy or CSV files.

        Parameters
        ----------
        filename_in : str
            Filename of a Numpy or CSV file.
        """

        # dividing greedypoints into chunks
        if mpi.RANK_IS_MAIN:
            if filename_in.endswith(".npy"):
                samples = np.load(filename_in)
            elif filename_in.endswith(".csv"):
                samples = [
                    np.atleast_1d(x)
                    for x in np.genfromtxt(filename_in, delimiter=",", comments="#")
                ]
            else:
                raise Exception
        else:
            samples = None

        new_samples = self._decompose_samples(samples)
        n_new_samples = len(new_samples)

        self.samples.extend(new_samples)
        self.n_samples = self.n_samples + n_new_samples

    def _add_random_samples(self, n_samples: int) -> None:
        """Add randomly generated samples to the set.

        Parameters
        ----------
        n_samples : int
            Number of random samples to add
        """

        self._random = np.random.default_rng()
        self._random_starting_state = np.random.get_state()
        samples = []
        for _ in range(n_samples):
            new_sample = self.model.params.generate_random_sample(self._random)
            samples.append(new_sample)

        new_samples = self._decompose_samples(samples)
        n_new_samples = len(new_samples)

        self.samples.extend(new_samples)
        self.n_samples = self.n_samples + n_new_samples

    def _decompose_samples(
        self,
        samples: List[Sample],
    ) -> List[Sample]:
        """Split a list of samples accross MPI ranks.

        Parameters
        ----------
        samples : List[Sample]
            Set of samples to split

        Returns
        -------
        List[Sample]
            Set of samples selected for the local rank
        """

        chunks: List[List[Sample]] = [[]]
        if mpi.RANK_IS_MAIN:
            chunks = [[] for _ in range(mpi.SIZE)]
            for i, chunk in enumerate(samples):
                chunks[i % mpi.SIZE].append(chunk)

        return mpi.COMM.scatter(chunks, root=mpi.MAIN_RANK)


"""Mettre a jour les tags dans un repertoire."""

import argparse
import csv
import logging
from pathlib import Path

def key(row: dict):
    return (row["text"], row["page"], row["x0"], row["top"])

def update_tags(annodir: Path):
    for p in annodir.iterdir():
        if p.suffix != ".csv":
            continue
        if not Path(p.name).exists():
            continue
        old_tags = {}
        with open(p, "rt") as oldfh:
            for row in csv.DictReader(oldfh):
                old_tags[key(row)] = row["tag"]
        out_rows = []
        with open(Path(p.name), "rt") as newfh:
            reader = csv.DictReader(newfh)
            fieldnames = reader.fieldnames
            for row in reader:
                if not row["tag"]:
                    row["tag"] = old_tags.get(key(row), "O")
                out_rows.append(row)
        with open(p, "wt") as outfh:
            writer = csv.DictWriter(outfh, fieldnames=fieldnames)
            writer.writeheader()
            for row in out_rows:
                writer.writerow(row)

def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("annodirs", nargs="+", type=Path)
    args = parser.parse_args()
    logging.basicConfig()
    for d in args.annodirs:
        update_tags(d)

if __name__ == "__main__":
    main()


import json
import logging

from . import spinnaker as sk

class BaseGateStatus(sk.SpinnakerStatus):
    """Common base class for gate status objects."""

    @classmethod
    def new(cls, operation, original_response):
        """Factory method.

        Args:
          operation: [AgentOperation] The operation this status is for.
          original_response: [string] The original JSON with the status identifier.

        Returns:
          sk.SpinnakerStatus for handling status from a Gate request.
        """
        return cls(operation, original_response)

    @property
    def finished(self):
        """True if status indicates the request has finished."""
        return self.current_state not in ["NOT_STARTED", "RUNNING", None]

    @property
    def finished_ok(self):
        """True if status indicates the request has finished successfully."""
        return self.current_state == "SUCCEEDED"

    def export_summary_to_json_snapshot(self, snapshot, entity):
        """Implements JsonSnapshotableEntity"""
        super().export_summary_to_json_snapshot(snapshot, entity)
        detail = self.detail_doc
        if not detail:
            return

        builder = snapshot.edge_builder

        if not isinstance(detail, list):
            base_time = detail.get("startTime", 0)
            self._export_status_entry_summary_to_json_snapshot(
                detail, base_time, builder, snapshot, entity
            )
            return

        base_time = detail[0].get("startTime", 0)
        self._export_status(detail[0], builder, entity)
        self._export_time_info(detail[0], base_time, builder, entity)

        step_list_entity = snapshot.new_entity(summary=f"{len(detail)} parts")

        for index, step in enumerate(detail):
            step_entity = snapshot.new_entity()
            self._export_status_entry_summary_to_json_snapshot(
                step, base_time, builder, snapshot, step_entity
            )
            builder.make(
                step_list_entity,
                "[{}] {}".format(index, step.get("name")),
                step_entity,
            )
        builder.make(entity, "Steps", step_list_entity)

    def _export_status_entry_summary_to_json_snapshot(
        self, status_entry, base_time, builder, snapshot, entity
    ):
        self._export_status(status_entry, builder, entity)
        self._export_time_info(status_entry, base_time, builder, entity)
        self._export_error_info(status_entry, builder, entity)
        if "execution" in status_entry:
            execution = status_entry["execution"]
            execution_entity = snapshot.new_entity(summary="Execution Summary")
            relation = self._STATUS_TO_RELATION.get(execution.get("status"))
            builder.make(entity, "Execution Info", execution_entity, relation=relation)
            self._export_status_entry_summary_to_json_snapshot(
                execution, base_time, builder, snapshot, execution_entity
            )
            return

        stages = status_entry.get("stages")
        if not stages:
            return
        stage_list_entity = snapshot.new_entity(
            summary=f"{len(stages)} stages"
        )
        worst_relation_score = self._RELATION_SCORE.get(None)
        for index, stage in enumerate(stages):
            stage_entity = snapshot.new_entity()
            relation = self._export_stage_summary_to_json_snapshot(
                stage, base_time, builder, snapshot, stage_entity
            )
            score = self._RELATION_SCORE.get(relation)
            if score is not None and (
                worst_relation_score is None or worst_relation_score < score
            ):
                worst_relation_score = score
            decorator = "(*) " if stage.get("status") == "RUNNING" else ""
            builder.make(
                stage_list_entity,
                "[{}] {}{}".format(index, decorator, stage.get("name")),
                stage_entity,
                relation=relation,
            )
        builder.make(
            entity,
            "Stages",
            stage_list_entity,
            relation=self._SCORE_TO_RELATION.get(worst_relation_score),
        )

    def _maybe_export_stage_context(self, stage, builder, snapshot, entity):
        context = stage.get("context")
        if not context:
            return
        ex = context.get("exception")
        if not ex:
            return
        builder.make(
            entity, "Exception Type", ex.get("exceptionType"), relation="ERROR"
        )
        errors = (ex.get("defaults") or ex.get("details", {})).get("errors")
        if errors:
            message = "\n".join([" * " + err for err in errors])
        else:
            message = ex.get("error")
        if message:
            builder.make(entity, "Errors", message, relation="ERROR", format="pre")

    def _export_stage_summary_to_json_snapshot(
        self, stage, base_time, builder, snapshot, entity
    ):
        stage_relation = self._export_status(stage, builder, entity)
        self._export_time_info(stage, base_time, builder, entity)
        self._maybe_export_stage_context(stage, builder, snapshot, entity)
        tasks = stage.get("tasks")
        if not tasks:
            return stage_relation

        task_list_entity = snapshot.new_entity(summary=f"{len(tasks)} tasks")
        worst_relation_score = self._RELATION_SCORE.get(None)
        for index, task in enumerate(tasks):
            task_name = task.get("name")
            task_entity = snapshot.new_entity()
            relation = self._export_status(task, builder, task_entity)
            score = self._RELATION_SCORE.get(relation)
            if score is not None and (
                worst_relation_score is None or worst_relation_score < score
            ):
                worst_relation_score = score
            self._export_time_info(task, base_time, builder, task_entity)
            builder.make(task_entity, "Task Name", task_name)
            decorator = "(*) " if task.get("status") == "RUNNING" else ""
            builder.make(
                task_list_entity,
                f"[{index}] {decorator}{task_name}",
                task_entity,
                relation=relation,
            )
        builder.make(
            entity,
            "Tasks",
            task_list_entity,
            relation=self._SCORE_TO_RELATION.get(worst_relation_score),
        )
        return stage_relation

class GateTaskStatus(BaseGateStatus):
    """Specialization of BaseGateStatus for accessing Gate 'tasks' status."""

    @property
    def timed_out(self):
        """True if status indicates the request timed out."""
        return (
            self.current_state == "TERMINAL"
            and str(self.detail).find(" timed out.") > 0
        )

    def __init__(self, operation, original_response=None):
        """Construct a new Gate request status.

        Args:
          operation: [AgentOperation] The operation this status is for.
          original_response: [string] The original JSON with the status identifier.
        """
        super().__init__(operation, original_response)

        if not original_response.ok():
            self._bind_error(original_response.error_message)
            self.current_state = "HTTP_ERROR"
            return

        doc = None
        try:
            doc = json.JSONDecoder().decode(original_response.output)
        except (ValueError, TypeError):
            pass

        if isinstance(doc, dict):
            self._bind_detail_path(doc["ref"])
            self._bind_id(self.detail_path)
        else:
            self._bind_error(f"Invalid response='{original_response}'")
            self.current_state = "CITEST_INTERNAL_ERROR"

    def _update_response_from_json(self, doc):
        """Updates abstract BaseGateStatus attributes from a Gate response.

        This is called by the base class.

        Args:
           doc: [dict] JSON Document object read from response payload.
        """
        self.current_state = doc["status"]
        self._bind_exception_details(None)

        exception_details = None
        gate_exception = None
        variables = doc.get("variables", None)
        if variables:
            for elem in variables:
                if elem["key"] == "exception":
                    value = elem["value"]
                    exception_details = value["details"]
                elif elem["key"] == "kato.tasks":
                    value = elem["value"]
                    for task in value:
                        if "exception" in task:
                            gate_exception = task["exception"]["message"]
                            break

        self._bind_exception_details(exception_details or gate_exception)

class GatePipelineStatus(BaseGateStatus):
    """Specialization of BaseGateStatus for accessing Gate 'pipelines' status."""

    @classmethod
    def new(cls, operation, original_response):
        """Factory method.

        Args:
          operation: [AgentOperation] The operation this status is for.
          original_response: [string] The original JSON with the status identifier.

        Returns:
          sk.SpinnakerStatus for handling status from a Gate request.
        """
        return GatePipelineStatus(operation, original_response)

    @property
    def timed_out(self):
        """True if status indicates the request timed out."""
        return (
            self.current_state == "TERMINAL"
            and str(self.detail).find(" timed out.") > 0
        )

    def __init__(self, operation, original_response=None):
        """Construct a new Gate request status.

        Args:
          operation: [AgentOperation] The operation this status is for.
          original_response: [string] The original JSON with the status identifier.
        """
        super().__init__(operation, original_response)
        self.__saw_pipeline = False

    def _update_response_from_json(self, doc):
        """Updates abstract sk.SpinnakerStatus attributes from a Gate response.

        This is called by the base class.

        Args:
           doc: [dict] JSON Document object read from response payload.
        """
        self._bind_exception_details(None)

        if not isinstance(doc, list):
            self._bind_error(f"Invalid response='{doc}'")
            self.current_state = "CITEST_INTERNAL_ERROR"

        # It can take a while for the running pipelines to show up.
        if len(doc) == 0 or not isinstance(doc, list):
            return

        if not self.__saw_pipeline:
            logger = logging.getLogger(__name__)
            logger.info("Pipeline is now visible.")
            self.__saw_pipeline = True

        # TODO(lwander): We need a way to ensure we are monitoring the correct
        #                pipeline.
        doc = doc[0]
        if self.current_state != doc["status"]:
            self.current_state = doc["status"]
            logger = logging.getLogger(__name__)
            logger.info("Pipeline state is now %s", self.current_state)

        # TODO(ewiseblatt): 20160308
        # It looks like sometimes when we get an exception, the exception is
        # burried in one of the interior stages and not propagated back up to
        # the top level status. It's context is empty. So this might not
        # be working at all, or at least is incomplete.
        context = doc.get("context", {})
        exception_details = context.get("exception", None)
        self._bind_exception_details(exception_details)

class GateAgent(sk.SpinnakerAgent):
    """Specialization of SpinnakerAgent for Gate subsystem.

    This class just adds convienence methods specific to Gate.
    """

    def make_create_app_operation(
        self,
        bindings,
        application,
        account_name,
        cloud_providers=None,
        description=None,
    ):
        """Create a Gate operation that will create a new application.

        Args:
          bindings: [dict] key/value pairs including optional TEST_EMAIL.
          application: [string] Name of application to create.
          account_name: [string] Account name owning the application.
          cloud_providers: [string] Comma-separated list of cloud providers.
          description: [string] Text description field for the operation payload.

        Returns:
          AgentOperation.
        """
        email = bindings.get("TEST_EMAIL", "testuser@testhost.org")
        payload = self.make_json_payload_from_kwargs(
            job=[
                {
                    "type": "createApplication",
                    "account": account_name,
                    "application": {
                        "name": application,
                        "description": description or "Gate Testing Application",
                        "email": email,
                        "platformHealthOnly": True,
                        "cloudProviders": cloud_providers or "",
                    },
                    "user": "[anonymous]",
                }
            ],
            description="Create Application: " + application,
            application=application,
        )

        return self.new_post_operation(title="create_app", data=payload, path="tasks")

    def make_delete_app_operation(self, application, account_name):
        """Create a Gate operation that will delete an existing application.

        Args:
          application: [string] Name of application to create.
          account_name: [string] Spinnaker account deleting the application.

        Returns:
          AgentOperation.
        """
        payload = self.make_json_payload_from_kwargs(
            job=[
                {
                    "type": "deleteApplication",
                    "account": account_name,
                    "application": {"name": application},
                    "user": "[anonymous]",
                }
            ],
            description="Delete Application: " + application,
            application=application,
        )

        return self.new_post_operation(title="delete_app", data=payload, path="tasks")

def new_agent(bindings, port=8084):
    """Create an agent to interact with a Spinnaker Gate server.

    Args:
      bindings: [dict] Bindings that specify how to connect to the server.
         The actual parameters used depend on the hosting platform.
         The hosting platform is specified with 'host_platform'.
      port: [int] The port the server is listening on.

    Returns:
      sk.SpinnakerAgent connected to the specified gate server, or None.
      The agent will have an additional attributes:
         managed_project: The name of the project Spinnaker is managing.
         account_name: The account name that Spinnaker is configured to use.
    """
    spinnaker = GateAgent.new_instance_from_bindings(
        "gate", GateTaskStatus.new, bindings, port
    )
    return spinnaker


"""
Views of the website
"""
import datetime
from django.shortcuts import render, redirect, get_object_or_404
from django.http import HttpResponse
from django.contrib import messages
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required
from .models import Hotels, Rooms, Reservation
from .exceptions import (
    RenderingHotels,
    UserNameNotAvailable,
    UserNameAlreadyExists,
    IncorrectCredentials,
)

# Create your views here.

def home(request):
    """
    Home Page of the website
    """
    all_location = (
        Hotels.objects.values_list("hotel_location", "id").distinct().order_by()
    )
    if request.method != "POST":
        data: dict = {"all_location": all_location}
        response = render(request, "index.html", data)
        return HttpResponse(response)

    try:
        hotels: list = Hotels.objects.all().get(
            id=int(float(request.POST["search_location"]))
        )
        room_reservations: list = []
        """
        for finding the reserved rooms on this time period
        for excluding from the query set
        """
        for each_reservation in Reservation.objects.all():
            if str(each_reservation.check_in_date) < str(request.POST["cin"]) and str(
                each_reservation.check_out_date
            ) < str(request.POST["cout"]):
                pass
            elif str(each_reservation.check_in_date) > str(request.POST["cin"]) and str(
                each_reservation.check_out_date
            ) > str(request.POST["cout"]):
                pass
            else:
                room_reservations.append(each_reservation.room.id)

        rooms: list = (
            Rooms.objects.all()
            .filter(hotel=hotels, max_capacity=int(request.POST["capacity"]))
            .exclude(id__in=room_reservations)
        )

        if len(rooms) == 0:
            messages.warning(
                request, "Sorry No Rooms Are Available on this time period"
            )
        data = {"rooms": rooms, "all_location": all_location, "flag": True}
        response = render(request, "index.html", data)
    except RenderingHotels as e_x:
        messages.error(request, e_x)
        response = render(request, "index.html", {"all_location": all_location})

    return HttpResponse(response)

def about_us(request):
    """
    About us page
    """
    return HttpResponse(render(request, "about.html"))

def contact(request):

    """
    Contact page
    """

    confirmation_message = None

    if request.method == 'POST':
        confirmation_message = "Your message has been sent. Thank you!"
        messages.success(request, "Your message has been sent. Thank you!")

    return render(request, 'contact.html', {'confirmation_message': confirmation_message})

@login_required(login_url="/user")
def book_room_page(request):
    """
    Book page
    """
    room_id = int(request.GET["roomid"])
    room = get_object_or_404(Rooms, id=room_id)

    return HttpResponse(render(request, "user/book_room.html", {"room": room}))

@login_required(login_url="/user")
def book_room(request):
    if request.method != "POST":
        return HttpResponse("Access Denied")

    room_id = request.POST["room_id"]

    room = get_object_or_404(Rooms, id=room_id)
    for each_reservation in Reservation.objects.all().filter(room=room):
        if (
            str(each_reservation.check_in_date) < str(request.POST["check_in"])
            and str(each_reservation.check_out_date) < str(request.POST["check_out"])
        ) or (
            str(each_reservation.check_in_date) > str(request.POST["check_in"])
            and str(each_reservation.check_out_date) > str(request.POST["check_out"])
        ):
            pass
        else:
            messages.warning(request, "Sorry This Room Is Unavailable")
            return redirect("HomePage")

    reservation = Reservation()
    room.availability_status = "2"
    room.save()

    current_user = request.user
    user_object = User.objects.all().get(username=current_user)
    reservation.guest = user_object
    reservation.room = room
    reservation.check_in_date = request.POST["check_in"]
    reservation.check_out_date = request.POST["check_out"]

    reservation.save()
    messages.success(request, "Congratulations! Booking Successful")

    return redirect("HomePage")

def handler404(request):
    """
    404 Page
    """
    return render(request, "404.html", status=404)

@login_required(login_url="/user")
def user_bookings(request):
    """
    Book the room
    """
    if not request.user.is_authenticated:
        return redirect("userloginpage")

    user = get_object_or_404(User, id=request.user.id)
    print(f"request user id = {request.user.id}")

    bookings = Reservation.objects.filter(guest=user)

    if not bookings:
        messages.warning(request, "No Bookings Found")

    return render(request, "user/my_bookings.html", {"bookings": bookings})


#Random Wheel program
import random, time, vlc

nameList = []

def namesInput(nameList):
    while True:
        checkName = input(str("Please enter a name or type 'done' to stop: "))
        if checkName == "done":
            print("Here are the names: ", nameList, "\n")
            break
        else:
            nameList.append(checkName)

def spin(nameList):
    if len(nameList) == 0:
        print("Invalid ammount of names")
    else:
        print("Generating names:")
        while True:
            randNum = random.randint(0,len(nameList) - 1)
            print(nameList[randNum])
            nameList.pop(randNum)
            if len(nameList) == 0:
                break
            else:
                pass

print("Welcome to random name spinner! \n")

namesInput(nameList)

spin(nameList)


class Solution:
    def subdomainVisits(self, cpdomains: List[str]) -> List[str]:
        # stores the domain and times it was visited
        subDomainDict = defaultdict(int)
        arr = []

        for cpdomain in cpdomains:
            freq, dom = cpdomain.split()
            # records the times the whole domain was visited
            subDomainDict[dom] += int(freq)
            # splits the subdomains
            domList = list(dom.split('.'))

            # checks if the domain has 3 subdomains or 2
            # and records each subdomain and the number of times they are visited
            if len(domList) == 2:
                subDomainDict[domList[1]] += int(freq)

            else:
                subDomainDict[domList[1] + '.' + domList[2]] += int(freq)
                subDomainDict[domList[2]] += int(freq)

        for domain in subDomainDict:
            arr.append(str(subDomainDict[domain]) + ' ' + domain)

        return arr


class Player():
    def __init__(self, name, currentHp, maxHp, inventar, rightHand, leftHand, armor):
        self.name = name
        self.currentHp = currentHp
        self.maxHp = maxHp
        self.inventar = inventar
        self.rightHand = rightHand
        self.leftHand = leftHand
        self.armor = armor


#!/bin/python

"""
Purpose: Advent of Code 2021 - Day 15
Date: 2021-12-15
URL: https://adventofcode.com/2021/day/15
Contributor(s):
    mark moretto
"""

from __future__ import annotations

from sys import maxsize
from array import array
from heapq import heappop, heappush

# Locals
from helpful.fs import get_data
from helpful.types import *

# Get data
AOC_DAY: int = 15
USE_SAMPLE_TF: bool = False
raw_data = get_data(AOC_DAY, USE_SAMPLE_TF)

matrix = [array("I", map(int, list(r))) for r in raw_data.splitlines()]

class FunctionMixin:

    @staticmethod
    def get_dim(iterable: List[int]) -> int:
        """Return row and column size multipled by a provided scaling factor
        for a 2-D data collection.

        Returns
        -------
        int
            Single dimension multiplied by scaling factor.
        """
        h, w = len(iterable), len(iterable[0])
        if h == w:
            return h
        raise ValueError("Row and column count unequal.")

class Solution(FunctionMixin):

    SQUARE_DIRS = [[1,0], [-1,0], [0,1], [0,-1],]

    def __init__(self, m: Matrix, scale_factor: int = 1):
        self._m = m
        self.scale = scale_factor
        #--> Get general dimension and create scaled-up version.
        self.dim = self.get_dim(m)
        self.max_dim = self.dim * scale_factor
        self._divisor = 9

        #--> Worker collections
        #   Priority queue and distance map.
        self._distance_map = {(0, 0): 0}
        self._priority_q = [(0, (0, 0))]

    def _gen_neighbors(self, r, c):
        def _valid(y, x) -> bool:
            """Keep neighbors within limits."""
            return 0 <= x < self.max_dim and 0 <= y < self.max_dim

        all_dirs = [(r+d[0], c+d[1]) for d in self.SQUARE_DIRS]
        return [d for d in all_dirs if _valid(*d)]

    def __divmod(self, v):
        return divmod(v, self.dim)

    def _cost(self, r, c):
        r_div, r_mod = self.__divmod(r)
        c_div, c_mod = self.__divmod(c)
        tmp = self._m[r_mod][c_mod]
        tmp += (r_div + c_div)
        return 1 + (tmp-1) % self._divisor

    @property
    def pq_len(self) -> int:
        return len(self._priority_q)

    def djikstras_algo(self):
        """Djikstra's algorithm for finding optimal path."""
        while self.pq_len > 0:
            total, (r, c) = heappop(self._priority_q)
            if total <= self._distance_map[(r, c)]:
                for n in self._gen_neighbors(r, c):
                    this_distance = total + self._cost(*n)
                    if this_distance < self._distance_map.get(n, maxsize):
                        self._distance_map[n] = this_distance
                        heappush(self._priority_q, (this_distance, n))

    def run(self):
        self.djikstras_algo()
        return self._distance_map[(self.max_dim-1, self.max_dim-1)]

if __name__ == "__main__":
    # Part 1
    s1 = Solution(matrix, 1)
    result = s1.run()
    print(f"Part 1 result: {result}")

    # Part 2
    s2 = Solution(matrix, 5)
    result = s2.run()
    print(f"Part 2 result: {result}")


import tempfile

from unittest import TestCase
from Freya_alerce.core.utils import Utils

class TestUtilsTransform(TestCase):

    def setUp(self):
        self.test_ra = 139.33444972
        self.test_dec = 68.6350604
        self.test_hms = '09h17m20.26793280000689s +68d38m06.2174s'#+4h34m32.414496000003936s'
        self.test_mag = 14.033394136845427
        self.test_magerr = 0.0012384787365595684

    def test(self):
        #print(Utils().deg_to_hms(self.test_ra,self.test_dec))
        #print(Utils().hms_to_deg(self.test_hms))
        #self.assertEqual(self.test_hms, Utils().deg_to_hms(self.test_ra,self.test_dec))
        self.assertAlmostEqual(self.test_ra, Utils().hms_to_deg(self.test_hms)[0])
        self.assertAlmostEqual(self.test_dec, Utils().hms_to_deg(self.test_hms)[1])
        self.assertEqual(self.test_mag,Utils().flux_to_mag(0.008843869902193546))
        self.assertEqual(self.test_magerr,Utils().fluxerr_to_magerr(1.0087999726238195e-05,0.008843869902193546))

if __name__ == '__main__':
    unittest.main()


import json

from .exceptions import InvalidConfiguration

class AbstractCast(object):
    def __call__(self, value):
        raise NotImplementedError()  # pragma: no cover

class Boolean(AbstractCast):
    default_values = {
        "1": True, "true": True, "yes": True, "y": True, "on": True, "t": True,
        "0": False, "false": False, "no": False, "n": False, "off": False, "f": False
    }

    def __init__(self, values=None):
        self.values = self.default_values
        if isinstance(values, dict):
            self.values.update(values)

    def __call__(self, value):
        try:
            return self.values[str(value).lower()]
        except KeyError:
            raise InvalidConfiguration("Error casting value {!r} to boolean".format(value))

class List(AbstractCast):
    def __init__(self, delimiter=",", quotes="\"'"):
        self.delimiter = delimiter
        self.quotes = quotes

    def _parse(self, string):
        elements = []
        element = []
        quote = ""
        for char in string:
            # open quote
            if char in self.quotes and not quote:
                quote = char
                element.append(char)
                continue

            # close quote
            if char in self.quotes and char == quote:
                quote = ""
                element.append(char)
                continue

            if quote:
                element.append(char)
                continue

            if char == self.delimiter:
                elements.append("".join(element))
                element = []
                continue

            element.append(char)

        # remaining element
        if element:
            elements.append("".join(element))

        return self.cast(e.strip() for e in elements)

    def cast(self, sequence):
        return list(sequence)

    def __call__(self, value):
        return self._parse(value)

class Tuple(List):
    def cast(self, sequence):
        return tuple(sequence)

class Option(AbstractCast):
    """
    Example::
        _INSTALLED_APPS = ("foo", "bar")
        INSTALLED_APPS = config("ENVIRONMENT", default="production", cast=Option({
            "production": _INSTALLED_APPS,
            "local": _INSTALLED_APPS + ("baz",)
        }))
    """

    def __init__(self, options):
        self.options = options

    def __call__(self, value):
        try:
            return self.options[value]
        except KeyError:
            raise InvalidConfiguration("Invalid option {!r}".format(value))

class JSON(AbstractCast):
    def __call__(self, value):
        try:
            return json.loads(value)
        except json.JSONDecodeError as ex:
            raise InvalidConfiguration('Invalid option {!r}'.format(value)) from ex
        except TypeError:
            return value


class Cachorro:
    def __init__(self, nome, idade):
        self.nome = nome;
        self.idade = idade;

    def latir(self):
        print(f"{self.nome} está latindo, Au Au Au, e tem {self.idade} anos!!");

#Criação de objetos da classe "Cachorro"

cachorro1 = Cachorro("Felipe", 2);
cachorro2 = Cachorro("Maria", 3);
cachorro3 = Cachorro("João", 4);

#Chamada da função "latir"

cachorro1.latir();
cachorro2.latir();
cachorro3.latir();

letras = {
    "A": r"""
        AAA        LL           AAA       NN        NN
       AAAAA       LL          AAAAA      NNNN      NN
      AAAAAAA      LL         AAAAAAA     NN NN     NN
     AA     AA     LL        AA     AA    NN   NN   NN
    AA       AA    LLLLLL   AA       AA   NN     NN NN

    """

}

nome = "Alan"

for letra in nome:
    if letra.upper() in letras:
        print(letras[letra.upper()])
    else:
        print("\n")


def silnia_iter(n):
    wynik = 1
    if n>1:
        for i in range(2,n+1):
            wynik = wynik * i
    return wynik

def silnia_rek(n):
    if n==0:
        return 1
    else:
        return n*silnia_rek(n-1)

liczba = int(input("Podaj liczbe"))

print("Silnia rekurencyjnie :",silnia_rek(liczba))
print("Silnia iter :",silnia_iter(liczba))


import requests
import urllib3

urllib3.disable_warnings()

request_session = requests.Session()
retries = Retry(total=5, backoff_factor=0.1, status_forcelist=[500, 502, 503, 504])
request_session.mount('https://', HTTPAdapter(max_retries=retries))

api_url = "https://localhost:9000/hello/status"
response = request_session.get(api_url, verify=False)
assert response.status_code == 200


# здесь контроллеры/хендлеры/представления для обработки запросов (flask ручки). сюда импортируются сервисы из пакета service
from flask_restx import Resource, Namespace
from models import Movie, MovieSchema
from flask import request
from setup_db import db

movie_ns = Namespace('movies')

@movie_ns.route('/')
class MoviesView(Resource):
    def get(self):
        director_id = request.args.get('director_id')
        genre_id = request.args.get('genre_id')
        movies_schema = MovieSchema(many=True)
        if director_id:
            movie = Movie.query.filter(Movie.director_id == director_id).all()
            if movie:
                return movies_schema.dump(movie), 200
            else:
                return "", 404
        if genre_id:
            movie = Movie.query.filter(Movie.genre_id == genre_id).all()
            if movie:
                return movies_schema.dump(movie), 200
            else:
                return "", 404
        movie = Movie.query.all()
        if movie:
            return movies_schema.dump(movie), 200
        else:
            return "", 404

    def post(self):
        req_json = request.json
        new_movie = Movie(**req_json)
        try:
            db.session.add(new_movie)
            db.session.commit()
            return "", 201
        except Exception as e:
            print(e)
            db.session.rollback()
            return "", 500

@movie_ns.route('/<int:gid>')
class MovieView(Resource):
    def get(self, gid):
        movie = Movie.query.get(gid)
        movie_schema = MovieSchema()
        return movie_schema.dump(movie), 200

    def put(self, gid):
        movie = Movie.query.get(gid)
        req_json = request.json
        try:
            movie.title = req_json.get("title")
            movie.description = req_json.get("description")
            movie.trailer = req_json.get("trailer")
            movie.year = req_json.get("year")
            movie.rating = req_json.get("rating")
            movie.genre_id = req_json.get("genre_id")
            movie.genre = req_json.get("genre")
            movie.director_id = req_json.get("director_id")
            movie.director = req_json.get("director")
            db.session.add(movie)
            db.session.commit()
            return "", 204
        except Exception as e:
            print(e)
            db.session.rollback()
            return "", 500

    def delete(self, gid):
        movie = Movie.query.get(gid)
        if movie:
            db.session.delete(movie)
            db.session.commit()
            return "", 204
        else:
            return "", 404


#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at
#
#    https://www.apache.org/licenses/LICENSE-2.0
#
#Unless required by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#See the License for the specific language governing permissions and
#limitations under the License.

import os
import ijson
import requests
import gzip
import shutil
import gcsfs

from google.cloud import storage
from google.cloud import bigquery
from time import sleep
from threading import Thread
from queue import Queue
from urllib.parse import urlparse

project_id = "shared-analytics-services"
fs = gcsfs.GCSFileSystem(project=project_id)

def file_download(file_url,bucket_name,downloadKey,filename):
    response = requests.get(url=file_url,allow_redirects=True,).content
    with fs.open(f"{bucket_name}/{downloadKey}/{filename}",'wb') as out:
        out.write(response)

def gunzip_file(bucket_name,downloadKey,filename):
    with fs.open(f"{bucket_name}/{downloadKey}/{filename}",'rb') as f_in:
        output_file = os.path.splitext(filename)[0]
        #g = gzip.GzipFile(fileobj=f_in)
        with fs.open(f"{bucket_name}/{downloadKey}/{output_file}",'wb') as f_out:
            print(f"Splitter input file: {bucket_name}/{downloadKey}/{output_file}")
            shutil.copyfileobj(gzip.GzipFile(fileobj=f_in), f_out)
    return f"{bucket_name}/{downloadKey}/{output_file}"

def parse_filename(url):
    parsedUrl = urlparse(url)
    return os.path.basename(parsedUrl.path)

def load_json_to_bq(table_id, bucket_uri):
    client = bigquery.Client()
    job_config = bigquery.LoadJobConfig(
    autodetect=True, source_format=bigquery.SourceFormat.NEWLINE_DELIMITED_JSON, max_bad_records=10000
    )
    uri = f"gs://{bucket_uri}*.json"
    load_job = client.load_table_from_uri(
    uri, table_id, job_config=job_config
    )
    load_job.result()
    destination_table = client.get_table(table_id)
    print("Loaded {} rows to {}.".format(destination_table.num_rows, destination_table))

def producer(queue,splitter_input_file):
    record_buffer = []
    json_target_items = 'in_network.item'
    counter = 0
    buffer_size = 25
    file_part_index = 1
    file_input_buffer = 1024000 #10737418240 #10737418240 #1024000 209715200

    print('setting backend')
    backend = ijson.get_backend('yajl2_c')
    print("opening file")
    with fs.open(f"{splitter_input_file}",'rb') as data:
        print('starting to iterate items')
        for obj in backend.items(data, json_target_items,use_float=True,buf_size=file_input_buffer):
            counter +=  1
            record_buffer.append(str(obj))
            if counter == buffer_size:
                print(f"adding record array to queue.")
                queue.put((file_part_index, record_buffer))
                print("record array added to queue")
                record_buffer = []
                file_part_index += 1
                counter = 0
    if counter > 0:
        print('Flushing remaining buffer records.')
        queue.put((file_part_index, record_buffer))
        print('remaining buffer records added to queue. All items in file have been added.')

    queue.put(None)
    print('Producer: Done')

def consumer(queue, output_path, target_file_name, worker_id, bucket_name):
    storage_client = storage.Client()
    bucket = storage_client.bucket(bucket_name)

    while True:
        chunk = queue.get()
        if chunk is None:
            print(f'Queue is empty. Shutting worker {worker_id} down...')
            #no more items add signal back for other consumers
            queue.put(None)
            break
        print(f'Worker {worker_id} writing file: {chunk[0]}')

        if (len(chunk[1]) == 0):
            print('chunk size was zero. no idea why. skipping write...')
            queue.task_done()
            continue

        blob = bucket.blob(f'{output_path}{target_file_name}_{chunk[0]}.json')
        #try:
        blob.upload_from_string("\n".join(chunk[1]),timeout=3600,)
        #except:
        #with open(f'{target_file_name}_{chunk[0]}.json', 'wt', encoding='utf-8') as targetFile:
        #        for line in chunk[1]:
        #            targetFile.write(f"{line}\n")
        queue.task_done()
        print(f'chunk processed by worker {worker_id}. File Created: {output_path}{target_file_name}_{chunk[0]}.json')

def main():
    #Get the URL env var passed to the batch job based on the task index
    #Batch invoker script passes a dict with {'URL<Index Number>':'https://...'} to every task
    batch_index_id = os.environ.get('BATCH_TASK_INDEX')
    file_url = os.environ.get(f"URL{batch_index_id}")
    filename = parse_filename(file_url)
    tablename = os.path.splitext(os.path.splitext(filename)[0])[0]
    bq_dataset = "cms_datasets" #Change this to reflect your dataset
    bucket_name = "cms-processing-bucket" #Change this to reflect your target bucket for the downloads and split JSON files
    downloadKey = f"{tablename}/downloaded"
    num_file_writers = 70 #  5 for small 70 for big
    queue_size = 1500 # -1 for unlimited
    queue = Queue(queue_size)
    queue_saturation_time = 700  # in seconds 60 for small 1800 for big
    output_path = f"{tablename}/load_to_bq/"
    output_filename = "in-network-rates"
    print(f"Streaming {filename} from {file_url} to {downloadKey}")
    file_download(file_url,bucket_name,downloadKey,filename)
    print(f"Downloaded {filename}")
    splitter_input_file = gunzip_file(bucket_name,downloadKey,filename)
    print(splitter_input_file)
    print('Starting file splitter...')
    p = Thread(target=producer, args=(queue,splitter_input_file))
    p.start()

    print(f'Pausing for {queue_saturation_time/60} minutes to allow queue to saturate...')
    sleep(queue_saturation_time)
    print(f'Starting {num_file_writers} file writers... ')
    workers = [Thread(target=consumer, args=(queue,output_path,output_filename,i,bucket_name)) for i in range(num_file_writers)]
    for w in workers:
        w.start()

    p.join()

    for w in workers:
        w.join()
    load_json_to_bq(f"{project_id}.{bq_dataset}.{tablename}",f"{bucket_name}/{output_path}")
main()