# coding: utf-8

# ## Behavioral cloning
# ---

# In[36]:

from keras.models import Sequential, model_from_json
from keras.layers.core import Dense, Activation, Flatten, Dropout, Lambda
from keras.layers import core, convolutional, pooling
from keras.layers.advanced_activations import ELU
from keras.regularizers import l2, l1
from keras.optimizers import Adam, Nadam
from keras.callbacks import ModelCheckpoint, Callback
from sklearn.model_selection import train_test_split
from sklearn.utils import shuffle
import skimage.transform as sktransform
import math
import numpy as np
import pandas as pd
from PIL import Image
import cv2
import matplotlib.pyplot as plt
from os import getcwd
import os
import csv
import random
import matplotlib.image as mpimg
# Fix error with TF and Keras
import tensorflow as tf
#from tensorflow.python.ops import functional_ops as fn_ops
#tf.python.control_flow_ops = tf
get_ipython().run_line_magic('matplotlib', 'inline')

# ### Getting the data

# * 3 laps of center driving clock wise
# * 3 laps of center driving counter clock wise
# * recovery driving

# ### Data loading

# Using pandas to load the data from the .csv file. the data are the centered images, the left images and the right images and their steering angle.

csv_path = './data/driving_log - Copie.csv'
local_data_path = '/data'
dir_abs_path = getcwd()

def change_dir(x):
    """
    Replace path of the a column of the dataframe
    example : C:\...\*\.jpg to *\.jpg
    """
    split = str(x).split('\\')
    filename = split[len(split)-1]
    dirname = split[len(split)-2]
    conca = os.path.join(dirname,filename)
    return conca

def load_data(csv_path):
    """
    Load data from the csv file
    Returns Panda DataFrame containing all the data
    """
    #df = pd.io.parsers.read_csv(csv_path, delimiter=',', header=None, names = ['center','left','right',
    #                                                                           'steering','throttle','brake','speed'])
    df = pd.io.parsers.read_csv(csv_path, delimiter=',')
    return df

def convert(strr):
    return strr.replace("\\","/")

def combine_dataframe(condi=False):
    """
    Combine the multiple csv of data into a final dataframe
    """
    if condi == True :
        df1 = load_data(csv_path)
        df2 = load_data('./data/2/driving_log.csv')
        df3 = load_data('./data/3/driving_log.csv')
        df4 = load_data('./data/4/driving_log.csv')

        df2.center = df2.center.apply(change_dir)
        df2.left = df2.left.apply(change_dir)
        df2.right = df2.right.apply(change_dir)
        df2.center = df2.center.apply(convert)
        df2.left = df2.left.apply(convert)
        df2.right = df2.right.apply(convert)

        df3.center = df3.center.apply(change_dir)
        df3.left = df3.left.apply(change_dir)
        df3.right = df3.right.apply(change_dir)
        df3.center = df3.center.apply(convert)
        df3.left = df3.left.apply(convert)
        df3.right = df3.right.apply(convert)

        df4.center = df4.center.apply(change_dir)
        df4.left = df4.left.apply(change_dir)
        df4.right = df4.right.apply(change_dir)
        df4.center = df4.center.apply(convert)
        df4.left = df4.left.apply(convert)
        df4.right = df4.right.apply(convert)

        df = pd.DataFrame()
        df = df.append(df1, ignore_index= True)
        df = df.append(df2, ignore_index= True)
        df = df.append(df3, ignore_index= True)
        df = df.append(df4, ignore_index= True)

        df.to_csv('data/driving_log_concat_raw.csv', index=False)
    else :
        pass

combine_dataframe(False)
df = pd.DataFrame()
df = load_data('./data/driving_log_concat_raw.csv')
print(df.count())

# ### Data visualisation

# Inspired from [Jeremy Shannon](https://github.com/jeremy-shannon/CarND-Behavioral-Cloning-Project)

# print a histogram to see which steering angle ranges are most overrepresented
min_sample_treshold = 150
num_bins = 45
avg_samples_per_bin = len(df['steering'])/num_bins
hist, bins = np.histogram(np.abs(df['steering']), num_bins)
width = 0.85 * (bins[1] - bins[0])
center = (bins[:-1] + bins[1:]) / 2
plt.bar(center, hist, align='center', width=width)
plt.plot((0, np.max(df['steering'])), (min_sample_treshold, min_sample_treshold), 'k-')

print(int(df['steering'].values[1]))
print(df['steering'].count())
print(hist[0])
print(bins[1]-bins[0])

# Better use panda to filter the data : much faster than using the array
start = 0. #Starting steering value
bin_n = 900# wanted amount of sample within a steering range
num_sub = 25
balanced = pd.DataFrame()
# Iterate from 0 to 1 [steering] by step of 1/45
for end in np.linspace(0,1, num_sub) :
    # save in df_range the steering values comprises within start and end range.
    # May use np.absolute(df.steering).between(-0.5, 0.5, inclusive=False) but multiplies some samples
    df_range = df[((np.absolute(df.steering) >= start) & (np.absolute(df.steering) < end))]
    # if there is data
    if (df_range.shape[0]) is not 0:
        # Minimum between the wanted number of samples within the steering range
        # and the df_range size (samples within the actual df)
        range_n = min(bin_n, df_range.shape[0])
        # Stores in DataFrame balanced range_n amount of random samples
        balanced = pd.concat([balanced,df_range.sample(range_n)])
        #balanced = balanced.append(balanced, ignore_index= True)
    else :
        # print('DID NOT')
        pass
    start = end

# remove last value of sterring (strange behavior of my data (??))
balanced = balanced.drop(balanced[(np.abs(balanced.steering) >= 0.97) & (np.abs(balanced.steering) < 1)].index)
print(balanced.count())

# print a histogram to see which steering angle ranges are most overrepresented
min_sample_treshold = bin_n
num_bins = len(np.linspace(0,1, 23))
avg_samples_per_bin = len(balanced['steering'])/num_bins
hist, bins = np.histogram(np.abs(balanced['steering']), num_bins)
width = 0.5 * (bins[1] - bins[0])
center = (bins[:-1] + bins[1:]) / 2
plt.bar(center, hist, align='center', width=width)
plt.plot((0, np.max(balanced['steering'])), (min_sample_treshold, min_sample_treshold), 'k-')

# Inspired from [Navoshta](https://github.com/navoshta/behavioral-cloning)
def preprocess_image(image, top_offset=0.375, bottom_offset=0.125):
    # Convert to YUV
    #image = cv2.cvtColor(image,cv2.COLOR_RGB2YUV)
    # Cropping the image
    top = int(top_offset * image.shape[0])
    bottom = int(bottom_offset * image.shape[0])
    image = sktransform.resize(image[top:-bottom,:],(32,128,3), mode='constant')
    # Apply gaussian blur
    image = cv2.GaussianBlur(image,(3,3),0)
    return image
# test
temp = mpimg.imread('./data/IMG/center_2016_12_01_13_30_48_287.jpg')
temp2 = preprocess_image(temp)
plt.imshow(temp2)

# #### Generators

# Copied from [Navoshta](https://github.com/navoshta/behavioral-cloning)
cameras = ['left','center','right']
cameras_steering_correction=[.25, .0, -.25]

# data : input panda data
def generate_data(data, root_path):
    while True :
        # Generate random batch of indices
        indices = np.random.permutation(data.count()[0])
        batch_size = 128
        # Travel each batch till the end of indices
        for batch in range (0,len(indices),batch_size):
            batch_indices = indices[batch:(batch+batch_size)]
            # initiate output arrays
            #x = np.empty([0,66,200,3], dtype=np.float32)
            x = np.empty([0,32,128,3], dtype=np.float32)
            y = np.empty([0], dtype=np.float32)
            # Travel each indices coutained within the batch
            # Read and process each image of the data
            for i in batch_indices:
                # Randomly select camera
                camera = np.random.randint(len(cameras))
                # Read frame image and work out steering angle
                image = mpimg.imread(os.path.join(root_path, data[cameras[camera]].values[i].strip()))
                angle = data.steering.values[i] + cameras_steering_correction[camera]
                # add shadow here
                h, w = image.shape[0], image.shape[1]
                [x1, x2] = np.random.choice(w, 2, replace=False)
                k = h / (x2 - x1)
                b = - k * x1
                for i in range(h):
                    c = int((i - b) / k)
                    image[i, :c, :] = (image[i, :c, :] * .5).astype(np.int32)
                # Randomly shift up and down while preprocessing
                v_delta = .05
                image = preprocess_image(image, top_offset = np.random.uniform(.375 - v_delta, .375 + v_delta),
                                   bottom_offset = np.random.uniform(.125 - v_delta, .125 + v_delta))
                # Append to batch
                x = np.append(x, [image], axis=0)
                y = np.append(y, [angle])
            # Randomly flip half of images in the batch
            flip_indices = random.sample(range(x.shape[0]), int(x.shape[0] / 2))
            x[flip_indices] = x[flip_indices, :, ::-1, :]
            y[flip_indices] = -y[flip_indices]
            yield (x, y)

local_project_path = './'
local_data_path = os.path.join(local_project_path, 'data')

# ### Model

# Inspired from [Navoshta](https://github.com/navoshta/behavioral-cloning) and [Navoshta](https://github.com/jeremy-shannon/CarND-Behavioral-Cloning-Project)
#
if not False:
    model = Sequential()
    # Normalize
    model.add(convolutional.Convolution2D(16, 3, 3, input_shape=(32, 128, 3), activation='relu', W_regularizer=l2(0.001)))
    model.add(pooling.MaxPooling2D(pool_size=(2, 2)))
    model.add(convolutional.Convolution2D(32, 3, 3, activation='relu', W_regularizer=l2(0.001)))
    model.add(pooling.MaxPooling2D(pool_size=(2, 2)))
    model.add(convolutional.Convolution2D(64, 3, 3, activation='relu', W_regularizer=l2(0.001)))
    model.add(pooling.MaxPooling2D(pool_size=(2, 2)))
    model.add(core.Flatten())
    model.add(core.Dense(500, activation='relu'))
    model.add(core.Dropout(.5))
    model.add(core.Dense(100, activation='relu'))
    model.add(core.Dropout(.25))
    model.add(core.Dense(20, activation='relu'))
    model.add(core.Dense(1))
    model.compile(optimizer=Adam(lr=1e-04), loss='mse')

    # initialize generators
    df_train, df_valid = train_test_split(balanced, test_size=0.2, random_state=40)

    checkpoint = ModelCheckpoint('model_{epoch:02d}.h5', save_weights_only=False)

    #history = model.fit(X, y, batch_size=128, nb_epoch=5, validation_split=0.2, verbose=2)
    history = model.fit_generator(generate_data(df_train, local_data_path),
                                  samples_per_epoch=df_train.shape[0], nb_epoch=20,
                                  validation_data=generate_data(df_valid, local_data_path),
                                  callbacks=[checkpoint],
                                  nb_val_samples=df_valid.shape[0])
    # print('Test Loss:', model.evaluate_generator(test_gen, 128))

    print(model.summary())
    model.save_weights('./model_.h5')

####### Other models not used
###### ConvNet Definintion ######
###### Usage of Keras 2 - updated from Jeremy Shannon modified nVidia CNN proposal in Keras 1.x
# for debugging purposes - don't want to mess with the model if just checkin' the data
just_checkin_the_data = False

if not just_checkin_the_data:
    model = Sequential()

    # Normalize
    model.add(Lambda(lambda x: x/127.5 - 1.0,input_shape=(66,200,3)))

    # Add three 5x5 convolution layers (output depth 24, 36, and 48), each with 2x2 stride
    model.add(Conv2D(24, (5, 5), strides=(2, 2), padding='valid', kernel_regularizer=l2(0.001)))
    model.add(ELU())
    model.add(Conv2D(36, (5, 5), strides=(2, 2), padding='valid', kernel_regularizer=l2(0.001)))
    model.add(ELU())
    model.add(Conv2D(48, (5, 5), strides=(2, 2), padding='valid', kernel_regularizer=l2(0.001)))
    model.add(ELU())

    #model.add(Dropout(0.50))

    # Add two 3x3 convolution layers (output depth 64, and 64)
    model.add(Conv2D(64, (3, 3), padding='valid', kernel_regularizer=l2(0.001)))
    model.add(ELU())
    model.add(Conv2D(64, (3, 3), padding='valid', kernel_regularizer=l2(0.001)))
    model.add(ELU())

    # Add a flatten layer
    model.add(Flatten())

    # Add three fully connected layers (depth 100, 50, 10), tanh activation (and dropouts)
    model.add(Dense(100, kernel_regularizer=l2(0.001)))
    model.add(ELU())
    #model.add(Dropout(0.50))
    model.add(Dense(50, kernel_regularizer=l2(0.001)))
    model.add(ELU())
    #model.add(Dropout(0.50))
    model.add(Dense(10, kernel_regularizer=l2(0.001)))
    model.add(ELU())
    #model.add(Dropout(0.50))

    # Add a fully connected output layer
    model.add(Dense(1))

    # Compile and train the model,
    model.compile(optimizer=Adam(lr=1e-4), loss='mse')

    ############  just for tweaking model ##############
    # pulling out 128 random samples and training just on them, to make sure the model is capable of overfitting
    # indices_train = np.random.randint(0, len(image_paths_train), 128)
    # indices_test = np.random.randint(0, len(image_paths_test), 12)
    # image_paths_train = image_paths_train[indices_train]
    # angles_train = angles_train[indices_train]
    # image_paths_test = image_paths_test[indices_test]
    # angles_test = angles_test[indices_test]
    #############################################################

    # initialize generators
    df_train, df_valid = train_test_split(balanced, test_size=0.2, random_state=40)

    checkpoint = ModelCheckpoint('model{epoch:02d}.h5', save_weights_only=False)

    #history = model.fit(X, y, batch_size=128, nb_epoch=5, validation_split=0.2, verbose=2)
    history = model.fit_generator(generate_data(df_train, local_data_path),
                                  steps_per_epoch=df_train.shape[0], epochs=1,
                                  validation_data=generate_data(df_valid, local_data_path),
                                  callbacks=[checkpoint],
                                  validation_steps=df_valid.shape[0])
    # print('Test Loss:', model.evaluate_generator(test_gen, 128))

    print(model.summary())
    model.save_weights('./model.h5')

# In[25]:

if not just_checkin_the_data:
    model = Sequential()
    # Normalize
    model.add(convolutional.Convolution2D(16, 3, 3, input_shape=(32, 128, 3), activation='relu'))
    model.add(pooling.MaxPooling2D(pool_size=(2, 2)))
    model.add(convolutional.Convolution2D(32, 3, 3, activation='relu'))
    model.add(pooling.MaxPooling2D(pool_size=(2, 2)))
    model.add(convolutional.Convolution2D(64, 3, 3, activation='relu'))
    model.add(pooling.MaxPooling2D(pool_size=(2, 2)))
    model.add(core.Flatten())
    model.add(core.Dense(500, activation='relu'))
    model.add(core.Dropout(.5))
    model.add(core.Dense(100, activation='relu'))
    model.add(core.Dropout(.25))
    model.add(core.Dense(20, activation='relu'))
    model.add(core.Dense(1))
    model.compile(optimizer=Adam(lr=1e-4), loss='mse')

    # initialize generators
    df_train, df_valid = train_test_split(balanced, test_size=0.2, random_state=40)

    checkpoint = ModelCheckpoint('model{epoch:02d}.h5', save_weights_only=False)

    #history = model.fit(X, y, batch_size=128, nb_epoch=5, validation_split=0.2, verbose=2)
    history = model.fit_generator(generate_data(df_train, local_data_path),
                                  samples_per_epoch=df_train.shape[0], nb_epoch=1,
                                  validation_data=generate_data(df_valid, local_data_path),
                                  callbacks=[checkpoint],
                                  validation_steps=df_valid.shape[0])
    # print('Test Loss:', model.evaluate_generator(test_gen, 128))

    print(model.summary())
    model.save_weights('./model.h5')


#
# A collection of useful functions when dealing with emulators
#

from __future__ import absolute_import, division
from __future__ import print_function, unicode_literals

import numpy as np
from GPy import kern

def fix_parameters(bounds):
    """
    Returns lists consisting of (parameter index, value) tuples.
    Use for visualisations in situations where there are more than 2
    inputs to a model
    """
    n_parameters = bounds.n_parameters()
    fixed_parameters = []
    for i in range(n_parameters):
        for j in range(n_parameters):
            if i == j:
                continue
            mid_vals = enumerate((bounds.lower()+bounds.upper())/2)
            mid_vals = list(mid_vals)
            mid_vals = [(idx, val) for (idx, val) in mid_vals
                        if idx not in [i, j]]

            fixed_parameters.append(mid_vals)

    return fixed_parameters

def generate_grid(lower, upper, splits, fixed=[]):
    """
    Generates a grid of evenly spaced out points for testing
    returns grid of the first paramater, grid of second parameter,
    and their values stacked with any fixed values provided
    Generated values are convenient for plotting surface
    or contour plots.
    fixed -- contains position and argument to keep fixed
    """

    if not fixed:
        p1_low, p2_low = lower
        p1_high, p2_high = upper
    else:
        # find out which parameters are not fixed and get their bounds
        n_params = len(lower)

        p1_idx, p2_idx = [i for i in range(n_params)
                          if i not in [j for (j, _) in fixed]]

        p1_low, p2_low = lower[p1_idx], lower[p2_idx]
        p1_high, p2_high = upper[p1_idx], upper[p2_idx]

    p1_range = np.linspace(p1_low, p1_high, splits)
    p2_range = np.linspace(p2_low, p2_high, splits)
    p1_grid, p2_grid = np.meshgrid(p1_range, p2_range)

    if fixed:
        # create a grid for every parameter and insert in
        # corresponding position in the grids array
        grids = [None] * n_params
        grids[p1_idx] = p1_grid
        grids[p2_idx] = p2_grid
        for (i, val) in fixed:
            fixed_grid = np.zeros((splits, splits)) + val
            grids[i] = fixed_grid
    else:
        grids = [p1_grid, p2_grid]

    grid = np.dstack(tuple(grids))

    return p1_grid, p2_grid, grid

def predict_grid(model, grid, dims=None):
    """
    Given a PDF and a grid of inputs calculates probability for
    each index in the grid
    """
    rows, cols, n_params = grid.shape
    flatten_grid = grid.reshape((rows * cols, n_params))
    pred = np.apply_along_axis(model, 1, flatten_grid)
    return pred.reshape(rows, cols)

# Functions to deal with composite kernels
def is_prod_kernel(kernel):
    """
    True when a given kernel is a GPy Prod kernel
    """
    return type(kernel) == kern.src.prod.Prod

def is_add_kernel(kernel):
    """
    True when a given kernel is a GPy Add kernel
    """
    return type(kernel) == kern.src.add.Add

def kernel_to_string(kernel, ident=0, decimal_places=4):
    """
    Converts complex GPy kernels to strings
    TODO: rewrite with .format
    """
    if kernel is None:
        return ""
    s = ""
    formatting = "{:." + str(decimal_places) + "f}"
    tab = ident * " "
    if is_prod_kernel(kernel) or is_add_kernel(kernel):
        op = "*" if is_prod_kernel(kernel) else "+"
        sub_kernels = []
        for sub_kernel in kernel.parameters:
            sub_kernels.append(kernel_to_string(sub_kernel, ident=ident + 1))
        s = "(" + op + "\n" + "\n".join(sub_kernels) + "\n" + tab + ")"
    else:
        # get name of kernel without "'>" characters
        name = str(type(kernel)).split(".")[-1]
        name = name[:-2]

        values = ",".join([formatting.format(x) for x in kernel])
        s = name + "(" + values + ")"
    return " " * ident + s

def get_total_variance(kernel):
    ans = 0
    if is_prod_kernel(kernel) or is_add_kernel(kernel):
        for sub_kernel in kernel.parameters:
            ans += get_total_variance(sub_kernel)
    else:
        if hasattr(kernel, "variance"):
            ans += kernel.variance
        elif hasattr(kernel, "variances"):
            ans += kernel.variances
        else:
            ans += 0

    return ans

def has_high_variance(kernel, threshold=10):
    variance = 0
    if is_prod_kernel(kernel) or is_add_kernel(kernel):
        for sub_kernel in kernel.parameters:
            if has_high_variance(sub_kernel):
                return True
    else:
        if hasattr(kernel, "variances"):
            variance = kernel.variances
        elif hasattr(kernel, "variance"):
            variance = kernel.variance
        else:
            # some kernels don't have variance as a parameter
            return False

    return variance > threshold

def simulate(
    model,
    parameters=None,
    times=None,
    noise_range_percent=0.05,
    n_splits=None
):
    """
    Simulates model for specified time interval with specified noise.
    Noise is normal with standart deviation as: range * noise_range_percent.
    Pass noise_range_percent=None if no noise wanted
    Returns values, times, noise_stds
    If n_splits is provided divide time interval into n_splits uniform parts.
    """

    if parameters is None:
        parameters = np.array(model.suggested_parameters())

    if times is None:
        times = model.suggested_times()

    # take times and calculate values
    if n_splits:
        min_time, max_time = min(times), max(times)
        times = np.linspace(min_time, max_time, n_splits)

    # simulate
    values = model.simulate(parameters, times)

    # noise
    # by default set 5% of range as the standard deviation
    if noise_range_percent is not None:
        noise_stds = np.abs(values.max(axis=0) - values.min(axis=0)) * noise_range_percent

        # final values
        values = values + np.random.normal(0, noise_stds, values.shape)

        return values, times, noise_stds

    return values, times


import zipfile

def zip_files(path, file_names):
    zfile = zipfile.ZipFile(path, 'w')
    for path in file_names:
        zfile.write(path)

    zfile.close()

QUERY_FILES = ['credentials/trader_bot0', 'backtester.py', 'indicators.py', 'lambda_function.py', 'sm_util.py', 'strategy.py']
zip_files('query.zip', QUERY_FILES)
AUTOUPDATER_FILES = ['autoupdater.py', 'credentials/alphavantage_key0', 'credentials/admin_credentials', 'backtester.py', 'indicators.py','sm_util.py', 'strategy.py', 'alphavantage.py']
zip_files('autoupdater.zip', AUTOUPDATER_FILES)


class Node:
    def __init__(self, value):
        self.data = value
        self.left = None
        self.right = None

def create_node(value):
    new_node = Node(value)
    return new_node

def insert_node(root, value):
    if root is None:
        return create_node(value)

    if value < root.data:
        root.left = insert_node(root.left, value)
    else:
        root.right = insert_node(root.right, value)

    return root

def in_order_traversal(root, array, index):
    if root is None:
        return

    in_order_traversal(root.left, array, index)
    array[index[0]] = root.data
    index[0] += 1
    in_order_traversal(root.right, array, index)

def binary_tree_sort(array):
    size = len(array)
    root = None

    for i in range(size):
        root = insert_node(root, array[i])

    index = [0]
    in_order_traversal(root, array, index)

array = [2, 3, 1, 923, -23, 9128, 0, 10, 20, 12]

print("\n== Estos son tus valores == \n")
print(*array, sep="\t")

print("\n\n== Ordenando... ==\n")

binary_tree_sort(array)

for num in array:
    print(num, end="\t")
print()

print("\n== Estos son tus valores ordenados ==\n")
print(*array, sep="\t")

print("\n")

input("Presiona Enter para continuar...")

Algoritmos de ordenamiento/py/12 BinaryTree.py


import asyncio
import itertools
import networkx as nx
import os
import re
from math import inf
from imdb_helper_functions import create_visited_pairs_checkpoint,\
    get_all_pages, get_correct_url, get_graph, get_results_csv,\
    get_soup, get_pages_for_description, read_graph_from_checkpoint,\
    read_labels_from_checkpoint, read_visited_pairs_from_checkpoint,\
    set_is_full_movie_is_realised

MAX_DEPTH_SEARCH = 3
ACTORS_SEARCH_LIMIT = 5
MOVIES_SEARCH_LIMIT = 5
DISTANCE_STEP = 0.5
HEADERS = {'Accept-Language': 'en', 'X-FORWARDED-FOR': '2.21.184.0'}

def get_actors_by_movie_soup(
        cast_page_soup,
        num_of_actors_limit=None
):
    all_actors = cast_page_soup.find_all(
        'td',
        attrs={'class': 'primary_photo'},
        limit=num_of_actors_limit
    )
    actors_list = []
    for actor in all_actors:
        link = actor.find('a')['href']
        actors_list.append(
            (actor.find('img')['title'],
             f'https://www.imdb.com{link}'))
    return actors_list

def get_movies_by_actor_soup(
        actor_page_soup,
        num_of_movies_limit=None
):
    all_movies = actor_page_soup.find_all(
        'div',
        attrs={'id': re.compile(r'(act(or|ress)-tt\d+)')}
    )
    movie_list = []
    for movie in all_movies:
        if set_is_full_movie_is_realised(movie):
            link = movie.find('a')['href']
            movie_list.append(
                (movie.b.text,
                 f'https://www.imdb.com{link}')
            )
        if num_of_movies_limit and len(movie_list) == num_of_movies_limit:
            break
    return movie_list

def get_movie_distance(actor_start_url,
                       actor_end_url,
                       num_of_actors_limit=None,
                       num_of_movies_limit=None,
                       num_of_depth_level_limit=None):
    actor_start_url = get_correct_url(actor_start_url)
    actor_end_url = get_correct_url(actor_end_url)

    if os.path.exists('visited_pairs.dump'):
        visited_pairs = read_visited_pairs_from_checkpoint()
        if (actor_start_url, actor_end_url) in visited_pairs:
            G_all_actors = read_graph_from_checkpoint()
            if actor_start_url in G_all_actors.nodes() and \
                    actor_end_url in G_all_actors.nodes() and \
                    nx.has_path(G_all_actors, actor_start_url, actor_end_url):
                return int(nx.shortest_path_length(G_all_actors,
                                                   actor_start_url,
                                                   actor_end_url)
                           * DISTANCE_STEP)
            return inf

    get_graph(actor_start_url,
              actor_end_url,
              get_actors_by_movie_soup,
              get_movies_by_actor_soup,
              num_of_actors_limit,
              num_of_movies_limit,
              num_of_depth_level_limit)

    G_all_actors = read_graph_from_checkpoint()
    if os.path.exists('visited_pairs.dump'):
        visited_pairs = read_visited_pairs_from_checkpoint()
    else:
        visited_pairs = []
    visited_pairs.append((actor_start_url, actor_end_url))
    create_visited_pairs_checkpoint(visited_pairs)

    if actor_start_url in G_all_actors.nodes() and\
            actor_end_url in G_all_actors.nodes() and\
            nx.has_path(G_all_actors, actor_start_url, actor_end_url):
        return int(nx.shortest_path_length(G_all_actors,
                                           actor_start_url,
                                           actor_end_url) * DISTANCE_STEP)

    return inf

def get_movie_descriptions_by_actor_soup(actor_page_soup):
    descriptions = []
    movies = get_movies_by_actor_soup(actor_page_soup)
    movies_urls = list(map(lambda x: x[1], movies))
    loop = asyncio.get_event_loop()
    movies_html = loop.run_until_complete(
        get_pages_for_description(movies_urls)
    )
    for movie in movies_html:
        soup = get_soup(movie[1])
        description = soup.find(
            'div',
            attrs={'class': 'ipc-html-content ipc-html-content--base'}
        )
        if description:
            descriptions.append(description.text)
    return descriptions

def main(urls,
         num_of_actors_limit=None,
         num_of_movies_limit=None,
         num_of_depth_level_limit=None):
    all_pairs = itertools.permutations(urls, 2)
    for pair in all_pairs:
        distance = get_movie_distance(pair[0],
                                      pair[1],
                                      num_of_actors_limit,
                                      num_of_movies_limit,
                                      num_of_depth_level_limit)
        print(f'from: {pair[0]}, '
              f'to: {pair[1]}, '
              f'distance = {distance}')

    get_results_csv(urls)

    labels = read_labels_from_checkpoint()

    loop = asyncio.get_event_loop()
    urls_actors_pages = loop.run_until_complete(get_all_pages(urls))
    for actor in urls_actors_pages:
        actor_name = labels[actor[0]]
        soup = get_soup(actor[1])
        descriptions = get_movie_descriptions_by_actor_soup(soup)
        with open(f'{actor_name}.txt', 'w', encoding='utf-8') as file:
            for el in descriptions:
                file.write(f'{el}\n')

if __name__ == "__main__":
    urls = ['https://www.imdb.com/name/nm0425005/',
            'https://www.imdb.com/name/nm1165110/',
            'https://www.imdb.com/name/nm0000375/',
            'https://www.imdb.com/name/nm0474774/',
            'https://www.imdb.com/name/nm0000329/',
            'https://www.imdb.com/name/nm0177896/',
            'https://www.imdb.com/name/nm0001191/',
            'https://www.imdb.com/name/nm0424060/',
            'https://www.imdb.com/name/nm0005527/',
            'https://www.imdb.com/name/nm0262635/']

    main(urls, ACTORS_SEARCH_LIMIT, MOVIES_SEARCH_LIMIT, MAX_DEPTH_SEARCH)


import os
import subprocess
import datetime
from file_ordering import ORDERED_CODE_FILENAMES

import_instructions = ["from " + file_name[:-3] for file_name in ORDERED_CODE_FILENAMES]

def is_import_instruction(code_line):
    for import_instruction in import_instructions:
        if import_instruction in code_line:
            return (True, '(' in code_line)
    return (False, False)

def generate_header():
    TEMPLATE_HEADER = """
	# -*- coding: UTF-8 -*-

	# Version stand-alone de la librairie python aboard.
	# https://github.com/darkrecher/aboard
	# https://aboard.readthedocs.io/fr/latest/
	#
	# Version : {{version}}
	# Date du build : {{date_build}}
	# commit git : {{git_commit_hash}}
	"""

    with open(".." + os.sep + "version.txt", "r", encoding="utf-8") as file_version:
        str_version = file_version.read()
        token_version, semicol, str_version = str_version.partition(":")
        str_version = str_version.strip()
    str_date_now = datetime.datetime.now().strftime("%Y-%m-%d")
    commit_hash = subprocess.Popen(
        "git rev-parse HEAD", stdout=subprocess.PIPE
    ).stdout.read()
    commit_hash = commit_hash.decode("utf-8").strip()

    text_header = TEMPLATE_HEADER.strip()
    data_tokens = ("version", "date_build", "git_commit_hash")
    data_values = (str_version, str_date_now, commit_hash)
    for data_token, data_value in zip(data_tokens, data_values):
        text_header = text_header.replace("{{" + data_token + "}}", data_value)

    for header_line in text_header.split("\n"):
        yield header_line.strip()

    yield("")

with open("aboard_standalone.py", "w", encoding="utf-8") as file_out:

    for line in generate_header():
        # OK pour le \n : https://docs.python.org/3/library/os.html#os.linesep
        file_out.write(line + "\n")

    for file_name in ORDERED_CODE_FILENAMES:
        with open(".." + os.sep + file_name, "r", encoding="utf-8") as file_code:

            import_multi_line = False

            for code_line in file_code.readlines():
                if import_multi_line:
                    if ')' in code_line:
                        import_multi_line = False
                else:
                    import_line, import_multi_line = is_import_instruction(code_line)
                    if not import_line:
                        file_out.write(code_line)

    file_out.write("\n")

print("Fini !!")


from typing import Optional

from fastapi import FastAPI, status, Form,UploadFile, File
from typing import List
from pydantic import BaseModel
from starlette.responses import JSONResponse
from fastapi.responses import HTMLResponse
from weather_service import WeatherService

app = FastAPI()

@app.post("/login/")
async def login(username: str = Form(...)):
    return {"username": username}

@app.post("/files/")
async def create_files(files: List[bytes] = File(...)):
    return {"file_sizes": [len(file) for file in files]}

@app.post("/uploadfiles/")
async def create_upload_files(files: List[UploadFile] = File(...)):
    return {"filenames": [file.filename for file in files]}

@app.get("/")
async def main():
    content = """
<body>
<form action="/files/" enctype="multipart/form-data" method="post">
<input name="files" type="file" multiple>
<input type="submit">
</form>
<form action="/uploadfiles/" enctype="multipart/form-data" method="post">
<input name="files" type="file" multiple>
<input type="submit">
</form>
</body>
    """
    return HTMLResponse(content=content)

class WeatherRequestModel(BaseModel):
    lat: float
    lon: float
    unit: Optional[str] = "F"

class WeatherResponseModel(BaseModel):
    currentTemp: float
    highTemp: float
    lowTemp: float
    unit : str

@app.post("/weather", response_model=WeatherResponseModel, status_code=status.HTTP_200_OK)
async def create_item(item: WeatherRequestModel, serviceId: Optional[int] = 1):

    # grab request payload params
    item_dict = item.dict()
    lat_v = item_dict.get("lat")
    lon_v = item_dict.get("lon")
    unit_v = item_dict.get("unit")

    # create the service object
    ws = WeatherService(lat_v,lon_v,unit_v)

    # fetch data based on service of choice
    print("Service id",serviceId)
    response = ws.get_weather_data(s_code=serviceId)

    # if response is not recieved from upstream service, return 400 with user friendly message
    if not response:
        return JSONResponse(status_code=400, content={"message": "Service not available"})

    return response


from django.urls import path
from . import views     # Blog app views
from .views import (
    PostListView, PostDetailView,
    PostCreateView, PostUpdateView,
    PostDeleteView, UserPostListView)    # Generic view classes class

urlpatterns = [
    path('', PostListView.as_view(), name='blog-home'), # url of blog app home page
    path('user/<str:username>/', UserPostListView.as_view(), name='user-posts'), # url for blog of a user as 'user/Shubham1406/'
    path('post/<int:pk>/', PostDetailView.as_view(), name='post-detail'), # url of each blog as 'post/23/'
    path('post/new/', PostCreateView.as_view(), name='post-create'), # url while creating new blog as 'post/new/'
    path('post/<int:pk>/update', PostUpdateView.as_view(), name='post-update'), # url where blog is updated as 'post/23/update'
    path('post/<int:pk>/delete', PostDeleteView.as_view(), name='post-delete'), # url wheere blog is deleted as 'post/23/update'
    path('about/', views.about, name='blog-about'), # url of blog app about page
]


import unittest
from creggian.parse import *

class TestParse(unittest.TestCase):
    def test_split_tsv(self):
        self.assertEqual(split_tsv('my\tname\tis'), ['my', 'name', 'is'])

    def test_split_cage_entry(self):
        self.assertEqual(split_cage_entry('chr10:100180637..100180657,-'), ('chr10', 100180637, 100180657, '-'))

    def test_parse_narrow_peak(self):
        entry = (u'chr1',
                 u'839806',
                 u'840204',
                 u'test_peak_6',
                 u'42',
                 u'.',
                 u'4.07729',
                 u'6.25019',
                 u'4.29897',
                 u'206',
                 u'Brain_Angular_Gyrus',
                 u'Histone_H3K4me3',
                 u'BI.Brain_Angular_Gyrus.H3K4me3.149.bed.gz.pp.bed',
                 u'BI.Brain_Angular_Gyrus.Input.149.bed.gz.pp.bed')

        entry_parsed = (u'chr1',
                        839806,
                        840204,
                        u'test_peak_6',
                        42,
                        u'.',
                        4.07729,
                        6.25019,
                        4.29897,
                        206,
                        u'Brain_Angular_Gyrus',
                        u'Histone_H3K4me3',
                        u'BI.Brain_Angular_Gyrus.H3K4me3.149.bed.gz.pp.bed',
                        u'BI.Brain_Angular_Gyrus.Input.149.bed.gz.pp.bed')

        self.assertEqual(parse_narrow_peak(entry), entry_parsed)

    def test_parse_tfbs_ucsc(self):
        entry = (u'chr1', u'839806', u'840204', u'name', u'845')
        entry_parsed = (u'chr1', 839806, 840204, u'name', 845)

        self.assertEqual(parse_tfbs_ucsc(entry), entry_parsed)


# -*- coding: UTF-8 -*-
# @File        :  merge_table_dict
# @CreateTime  :  2019/9/17 22:50
# @Author      :  liuzhiqiang

"""
题目描述
数据表记录包含表索引和数值，请对表索引相同的记录进行合并，即将相同索引的数值进行求和运算，输出按照key值升序进行输出。

输入描述:
先输入键值对的个数
然后输入成对的index和value值，以空格隔开

输出描述:
输出合并后的键值对（多行）

示例1
输入
================
4
0 1
0 2
1 2
3 4
================
输出
0 3
1 2
3 4
"""

# number = int(input())
# array = {}
# for i in range(number):
#     tm = input().split()
#     if int(tm[0]) in array:
#         array[int(tm[0])] += int(tm[1])
#     else:
#         array[int(tm[0])] = int(tm[1])
#
# sorted(array)
# for k, v in array.items():
#     print(k, v)

while True:
    try:
        num = int(input())
        output = {}
        for i in range(num):
            pair = input().split(' ')
            output[int(pair[0])] = output.setdefault(int(pair[0]), 0) + int(pair[1])

        output_list = sorted(output.keys())
        print(output_list)
        for key in output_list:
            print(key, output[key])
    except:
        break


###########################################
## 1 - DESCRIPTIVE STISTICS
###########################################
"""
this script is used to get some basic statistics about the collecected data.
mainly the number of words of each category is important, as the difference
of the wordcount of each category between the presidency of obama and trump
has to be taken into account for later text analysis.
"""

import sys

sys.path.append("./helper_scripts")
from xml_corpus_reader import XMLCorpusReader

# initialize corpus reader
corpus = XMLCorpusReader(root="../corpus")

# show statistics about all categories speparated by presidency, except the presidential speeches
categories = corpus.categories()
categories.remove("presidential_speeches/obama")
categories.remove("presidential_speeches/trump")
categories_obama = [category for category in categories if "obama" in category]
categories_trump = [category for category in categories if "trump" in category]
print(corpus.describe(categories=categories_obama))
print(corpus.describe(categories=categories_trump))

# show statistics about each single category
for category in corpus.categories():
    print(corpus.describe(categories=category))


#!/usr/bin/env python3
import pandas as pd

class Queries:
    def __init__(self, path=None):
        self.path = path
        self.data = pd.Series(dtype="object")
        self._load_file(path)

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

    def __iter__(self):
        return iter(self.data.items())

    def __getitem__(self, key):
        return self.data.loc[key]

    def keys(self):
        return self.data.index

    def values(self):
        return self.data.values

    def items(self):
        return self.data.items()

    def _load_file(self, path):
        if path.endswith((".csv", ".tsv")):
            self.data = self._load_tsv(path)

        elif path.endswith(".json"):
            raise DeprecationWarning()
            # self.data = self._load_json(path)

        return self.data

    def _load_tsv(self, path, drop_nan=False):
        delimiter = "\t" if path.endswith(".tsv") else ","
        queries = pd.read_csv(path, delimiter=delimiter, index_col=False)

        self._replace_nan(queries, drop_nan)
        qid, query, *_ = queries.columns
        # convert the pandas.DataFrame with the columns QID and query
        # into a pandas.Series
        queries = queries.set_index(qid, drop=False)[query]
        self._rename_axis(queries)
        return queries

    # def _load_json(self, path, drop_nan=False):
    #     queries = pd.read_json(path, typ="series")
    #     self._replace_nan(queries, drop_nan)
    #     self._rename_axis(queries)
    #     return queries

    def _replace_nan(self, series: pd.Series, drop_nan=False):
        if drop_nan:
            series.replace("", pd.NaT, inplace=True)
            series.dropna(inplace=True)  # drop rows with NaN/NaT values
        else:
            series.fillna("", inplace=True)

        return series

    def _rename_axis(self, series: pd.Series):
        series.rename_axis("QID", inplace=True)
        return series

    def qid2string(self, qid, skip_non_existing=False):
        if isinstance(qid, list):
            return [
                self.data[q] for q in qid if (not skip_non_existing) or q in self.keys()
            ]
        else:
            return (
                self.data[qid]
                if (not skip_non_existing) or qid in self.keys()
                else None
            )

if __name__ == "__main__":
    # path = "../../data/ms_marco/ms_marco_v2_1/train/queries.tsv"
    # queries = Queries(path=path)
    # print(queries.data, end="\n\n")

    path = "../../data/fandoms_qa/fandoms_all/all/queries.tsv"
    queries = Queries(path=path)
    print(queries.data, end="\n\n")

    print(queries[623686])
    print(queries[271434])

    print(len(queries))
    print(queries[0])
    print(queries.qid2string([0, 2 * len(queries)], skip_non_existing=True))


#TP Proba
import random
from math import *
from matplotlib import pyplot
import numpy

def miseajourMABA(p,A,k):
    l=k
    if (l!=A) and (l!=0):
        pas=2*(random.random()<p)-1
        l=k+pas
    return l

def trajMABA(p,A,k,n):
    traj=[k]
    for i in range(n):
        traj.append(miseajourMABA(p,A,traj[-1]))
    pyplot.plot(range(n+1),traj,color='blue')
    pyplot.show()

def tempsabsMABA(p,A,k):
    traj=[k]
    compteur=0
    while traj[-1] not in [0,A]:
        traj.append(miseajourMABA(p,A,traj[-1]))
        compteur=compteur+1
    absorbeur=traj[-1]
    return absorbeur,compteur

def tempsabsMABA1(p,A,k):
    traj=[k]
    compteur=0
    while traj[-1] not in [0,A]:
        traj.append(miseajourMABA(p,A,traj[-1]))
        compteur=compteur+1
    absorbeur=traj[-1]
    return compteur

def estimMABA(p,A,k,n):#où n est la taille de l'échantillon
    absorb=[]
    temps=[]
    for i in range(n):
        [abs,tps]=tempsabsMABA(p,A,k)
        absorb.append(abs)
        temps.append(tps)
    tpsmoy=sum(temps)/n
    pAapprox=sum(absorb)/(A*n)
    return(pAapprox,tpsmoy)

#ex2
def miseajourMA2BR(p,A,k):
    l=k
    if l==0:
        if(random.random()<p):
            pas=1
        else:
            pas=0
    elif l==A:
        if (random.random()>p):
            pas=-1
        else:
            pas=0
    else:
        pas=2*(random.random()<p)-1
    l=k+pas
    return l


import http.cookiejar
import urllib.request
# 保存Cookie
filename = 'saved_cookies.txt'
# FileCookieJar、MozillaCookieJar、LWPCookieJar约为保存cookie信息，
# 只是保存格式不同，读者可自行尝试
cookie = http.cookiejar.MozillaCookieJar(filename)
handler = urllib.request.HTTPCookieProcessor(cookie)
opener = urllib.request.build_opener(handler)
response = opener.open('http://www.baidu.com')
cookie.save(ignore_discard=True, ignore_expires=True)


from __future__ import annotations

from unittest import mock

from airflow.providers.amazon.aws.hooks.quicksight import QuickSightHook
from airflow.providers.amazon.aws.operators.quicksight import QuickSightCreateIngestionOperator

DATA_SET_ID = "DemoDataSet"
INGESTION_ID = "DemoDataSet_Ingestion"
AWS_ACCOUNT_ID = "123456789012"
INGESTION_TYPE = "FULL_REFRESH"

MOCK_RESPONSE = {
    "Status": 201,
    "Arn": "arn:aws:quicksight:us-east-1:123456789012:dataset/DemoDataSet/ingestion/DemoDataSet_Ingestion",
    "IngestionId": "DemoDataSet_Ingestion",
    "IngestionStatus": "INITIALIZED",
    "RequestId": "fc1f7eea-1327-41d6-9af7-c12f097ed343",
}

class TestQuickSightCreateIngestionOperator:
    def setup_method(self):
        self.quicksight = QuickSightCreateIngestionOperator(
            task_id="test_quicksight_operator",
            data_set_id=DATA_SET_ID,
            ingestion_id=INGESTION_ID,
        )

    @mock.patch.object(QuickSightHook, "get_conn")
    @mock.patch.object(QuickSightHook, "create_ingestion")
    def test_execute(self, mock_create_ingestion, mock_client):
        mock_create_ingestion.return_value = MOCK_RESPONSE
        self.quicksight.execute(None)
        mock_create_ingestion.assert_called_once_with(
            data_set_id=DATA_SET_ID,
            ingestion_id=INGESTION_ID,
            ingestion_type="FULL_REFRESH",
            wait_for_completion=True,
            check_interval=30,
        )


N = int(input())

scores = [0] * 301
stairs = [0] * 301
for i in range(N):
    scores[i] = int(input())

stairs[0] = scores[0]
stairs[1] = scores[0] + scores[1]
stairs[2] = max(scores[1]+scores[2], scores[0]+scores[2])
for i in range(3, N):
    stairs[i] = max(stairs[i-3] + scores[i-1] + scores[i], stairs[i-2] + scores[i])

print(stairs[N-1])


from Contendor import Contendor
import random

contendorA = Contendor("Abarradás")

contendorB = Contendor("Boltor")

while contendorA.health > 0 and contendorB.health > 0:
    contendorA.hitEnemy(contendorB)
    contendorB.hitEnemy(contendorA)
    print(str(contendorA.health) + " | " + str(contendorB.health))


""" docstring """
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.status import *
from rest_framework.views import APIView

from AudioApp.models import *
from .serializers import *

# Create your views here.
# @csrf_exempt
class AboutUsAPI(APIView):
    """
    API for Retreiving AboutUs of company
    """
    permission_classes = (IsAuthenticated,)

    def post(self, request):
        """ docstring """
        user = request.user.email
        try:
            employee = Employess.objects.get(employee_email=user)
            about_us = AboutUs.objects.get(company=employee.company)
            serializer = AboutUsSerializer(about_us)
            return Response({"message": "AboutUs Retrieved Successfully!!", "data": serializer.data},
                            status=HTTP_200_OK)
        except:
            return Response({"message": "AboutUs Not Found!!"}, status=HTTP_404_NOT_FOUND)

class TermsServicesAPI(APIView):
    """
    API for Retreiving TermsServices of company
    """
    permission_classes = (IsAuthenticated,)

    def post(self, request):
        """ docstring """
        user = request.user.email
        try:
            employee = Employess.objects.get(employee_email=user)
            term_services = TermServices.objects.get(company=employee.company)
            serializer = TermServicesSerializer(term_services)
            return Response({"message": "Terms and Services Retrieved Successfully!!", "data": serializer.data},
                            status=HTTP_200_OK)
        except:
            return Response({"message": "Terms and Services Not Found!!"}, status=HTTP_404_NOT_FOUND)

class PrivacyPolicyAPI(APIView):
    """
    API for Retreiving PrivacyPolicy of company
    """
    permission_classes = (IsAuthenticated,)

    def post(self, request):
        """ docstring """
        user = request.user.email
        try:
            employee = Employess.objects.get(employee_email=user)
            privacy_policy = PrivacyPolicy.objects.get(company=employee.company)
            serializer = PrivacyPolicySerializer(privacy_policy)
            return Response({"message": "Privacy Policy Retrieved Successfully!!", "data": serializer.data},
                            status=HTTP_200_OK)
        except:
            return Response({"message": "Privacy Policy not Found!!"}, status=HTTP_404_NOT_FOUND)


from No import No

class Lista:
    def __init__(self):
        self.first = None
        self.last = None
        self.size = 0

    def __len__(self):
        return self.size

    def add(self, value):
        if self.first:
            aux = self.first
            ant = None
            while(aux.next):
                ant = aux
                aux = aux.next
            aux.next = No(value)
            aux.next.ant = aux
            aux.ant = ant
            if self.last:
                self.last = aux.next
        else:
            self.first = No(value)
            self.last = No(value)
        self.size += 1

    def Ordenada(self):
        if self.first == None:
            print("Empty List")

        aux = self.first
        while(aux):
            print(aux.report)
            aux = aux.next
        print("Sum: " + str(self.size))
        print("\n -----------------\n")

    def visitaInvertido(self):
        aux = self.last
        print("Lista Inversa: " + str(self.size))
        while(aux):
            print(aux.report)
            aux = aux.ant
        print("Sum: " + str(self.size))
        print("\n -----------------\n")

lista = Lista()

lista.add(2)
lista.add(5)
lista.add(12)
lista.add(20)
lista.Ordenada()
lista.visitaInvertido()

Estrutura de Dados/Trabalho 2º Bim/Listatest.py


from odoo import models, fields, api, _
from odoo.exceptions import UserError, ValidationError

class AccountAnalyticAccount(models.Model):
    _inherit = 'account.analytic.account'

    agreement_id = fields.Many2one(
        comodel_name='agreement',
        string='Agreement',
        ondelete='restrict',
        readonly=True,
    )
    rent_product_id = fields.Many2one(
        comodel_name='product.product',
        compute='_compute_product_id',
        string='Rental Product',
        store=True,
    )
    group_id = fields.Many2one(
        string='Zone',
    )
    recurring_rule_type = fields.Selection(
        selection=[
            ('daily', 'Day(s)'),
            ('monthly', 'Month(s)'),
            ('yearly', 'Year(s)'),
        ],
    )
    income_type_id = fields.Many2one(
        comodel_name='agreement.income.type',
        string='Income Type',
        required=True,
        index=True,
    )

    @api.constrains('recurring_invoice_line_ids')
    def _check_recurring_invoice_line_ids(self):
        for rec in self:
            rent_products = \
                rec.recurring_invoice_line_ids.filtered(
                    lambda l: l.product_id.value_type == 'rent').mapped(
                        'product_id')
            if len(rent_products) > 1:
                raise UserError(_('Only one rental product is allowed.'))

    @api.depends('recurring_invoice_line_ids')
    def _compute_product_id(self):
        for rec in self:
            rent_products = rec.recurring_invoice_line_ids.filtered(
                lambda l: l.product_id.value_type == 'rent') \
                .mapped('product_id')
            if rent_products:
                rec.rent_product_id = rent_products[0]

    @api.model
    def _prepare_invoice_line(self, line, invoice_id):
        next_date = line.analytic_account_id.recurring_next_date
        # No create invoice lines
        if not (line.date_start or line.date_end):
            return {}
        if line.date_start and not line.date_end and \
           line.date_start > next_date:
            return {}
        if line.date_end and not line.date_start and next_date > line.date_end:
            return {}
        if not(line.date_start <= next_date <= line.date_end):
            return {}
        if line.manual:
            return {}
        invoice_line_vals = super(AccountAnalyticAccount, self)._prepare_invoice_line(line, invoice_id)
        # Overwrite account and taxes
        income_type = line.analytic_account_id.income_type_id
        if income_type and line.product_id.value_type == income_type.value_type:
            if income_type.account_id:
                invoice_line_vals['account_id'] = income_type.account_id.id
            if income_type.tax_ids:
                invoice_line_vals['invoice_line_tax_ids'] = [(6, 0, income_type.tax_ids.ids)]
        return invoice_line_vals

    @api.multi
    def recurring_create_invoice(self):
        """Create invoice only if Invoice contain some lines."""
        invoices = super().recurring_create_invoice()
        no_line_invs = invoices.filtered(lambda inv: not inv.invoice_line_ids)
        invoices -= no_line_invs
        no_line_invs.unlink()
        # Update date of next invoice
        for contract in self:
            agreement = contract.agreement_id
            if not agreement.agreement_invoice_line:
                continue
            next_date = contract.recurring_next_date
            invoice_date_days = agreement.invoice_date_days
            if next_date.day == invoice_date_days:
                continue
            contract.write({
                'recurring_next_date': '%s-%s-%s' % (
                    next_date.year, next_date.month, invoice_date_days)
            })
        return invoices

    @api.multi
    def _create_invoice(self, invoice=False):
        self.ensure_one()
        # Can not create invoice after termination date
        termination_date = self.agreement_id.termination_date
        if not self.active:
            raise ValidationError(_("Can not create invoice of %s with inactive contract.") % self.name)
        if termination_date and self.recurring_next_date > termination_date:
            raise ValidationError(_("Can not create invoice of %s after termination date.") % self.name)
        # Create invoice
        inv = super()._create_invoice(invoice=invoice)
        # Update invoice type
        type2 = 'rent'
        income_type = self.income_type_id
        if income_type and self.rent_product_id.value_type == income_type.value_type:
            type2 = income_type.invoice_type
        inv.write({
            'type2': type2,
            'name': self.rent_product_id.name,
        })
        return inv

class AccountAnalyticGroup(models.Model):
    _inherit = 'account.analytic.group'

    weight1 = fields.Char(
        string='Load weight',
    )
    weight2 = fields.Char(
        string='Other weight',
    )
    market_zone_map_ids = fields.One2many(
        comodel_name='market.zone.map',
        inverse_name='group_id',
        string='Market Zone Map',
    )

class MarketZoneMap(models.Model):
    _name = 'market.zone.map'
    _description = 'Market Zone Map'

    use_for_lock = fields.Selection(
        selection=[
            ('all', 'All'),
            ('custom', 'Custom')
        ],
        string='Use For Lock',
        default='all',
        required=True,
    )
    start_lock_number = fields.Char(
        strong='Start Lock Number',
    )
    end_lock_number = fields.Char(
        string='End Lock Number',
    )
    map = fields.Binary(
        string='Map',
    )
    group_id = fields.Many2one(
        comodel_name='account.analytic.group',
        string='Analytic Group',
    )

    @api.onchange("use_for_lock")
    def _onchange_use_for_lock(self):
        self.update({
            "start_lock_number": False,
            "end_lock_number": False,
        })

    @api.multi
    def has_map(self, lock_number):
        self.ensure_one()
        has_map = False
        if self.map:
            if self.use_for_lock == 'all':
                has_map = True
            elif self.use_for_lock == 'custom':
                try:
                    if float(self.start_lock_number) <= float(lock_number) <= float(self.end_lock_number):
                        has_map = True
                except Exception as ex:
                    has_map = False
        return has_map


import os
from PIL import Image

x = y = 0
line = 20
path = 'image'
NewImage = Image.new('RGB', (128*line, 128*line))
for i in os.listdir(path):
    i = os.path.join(path, i)
    try:
        img = Image.open(i)
        print(img)
        img = img.resize((128, 128), Image.ANTIALIAS)
        NewImage.paste(img, (x * 128, y * 128))
        x += 1
    except IOError:
        print('sad')
        x -= 1
    if x == line:
        x = 0
        y += 1
    if (x + line * y) == line * line:
        break
NewImage.save("logo.jpg")


# -*- coding: utf-8 -*-
"""
Created on Sun Nov 21 20:30:28 2021

@author: vitin
"""

import pandas as pd
import numpy as np
import string
import collections
import nltk
from nltk.corpus import stopwords

df = pd.read_excel("cardano_twt.xlsx")

content = df['content']

'''twt = ['crypto', 'Crypto', 'time', 'last', 'unvaccinatedlivesmatter', 'buy', 'like',
       'purchase', 'bought', 'target', 'sell', 'profit', 'less', 'reduce', 'impact', 'volatility',
       'oversall', 'CLICK', 'track', 'movements', 'cryptocurrency', 'project',
       'get', 'one', 'amp', 'good', 'see', 'CryptoBroski1', 'great', 'de', 'news',
       'Done', 'new', 'would', 'day', 'people', 'ShillRonin', 'know', 'us', 'got',
       'money', 'dont', 'price', 'think', 'trading', 'imayonaisee']'''

twt = stopwords.words('english')

def txt_process(txt):
    nopont = [char for char in txt if char not in string.punctuation]
    nopont = ''.join(nopont)
    sms = [word for word in nopont.split() if word.lower() not in twt]
    sms = [word for word in sms if word.lower() not in stopwords.words('english')]
    return sms

data = content.apply(txt_process)

lista = list(data)

new_lista = []
new_lista = sum(lista, new_lista)
array_teste = np.array(new_lista)

array_teste = np.array(new_lista)

counter = collections.Counter(array_teste)

print(counter.most_common(n=50))


""

load("//python/private:render_pkg_aliases.bzl", "render_pkg_aliases")
load("//python/private:text_util.bzl", "render")

_BUILD_FILE_CONTENTS = """\
package(default_visibility = ["//visibility:public"])

# Ensure the `requirements.bzl` source can be accessed by stardoc, since users load() from it
exports_files(["requirements.bzl"])
"""

def _pip_repository_impl(rctx):
    bzl_packages = rctx.attr.whl_map.keys()
    aliases = render_pkg_aliases(
        repo_name = rctx.attr.repo_name,
        rules_python = rctx.attr._template.workspace_name,
        default_version = rctx.attr.default_version,
        whl_map = rctx.attr.whl_map,
    )
    for path, contents in aliases.items():
        rctx.file(path, contents)

    # NOTE: we are using the canonical name with the double '@' in order to
    # always uniquely identify a repository, as the labels are being passed as
    # a string and the resolution of the label happens at the call-site of the
    # `requirement`, et al. macros.
    macro_tmpl = "@@{name}//{{}}:{{}}".format(name = rctx.attr.name)

    rctx.file("BUILD.bazel", _BUILD_FILE_CONTENTS)
    rctx.template("requirements.bzl", rctx.attr._template, substitutions = {
        "%%ALL_DATA_REQUIREMENTS%%": render.list([
            macro_tmpl.format(p, "data")
            for p in bzl_packages
        ]),
        "%%ALL_REQUIREMENTS%%": render.list([
            macro_tmpl.format(p, p)
            for p in bzl_packages
        ]),
        "%%ALL_WHL_REQUIREMENTS%%": render.list([
            macro_tmpl.format(p, "whl")
            for p in bzl_packages
        ]),
        "%%MACRO_TMPL%%": macro_tmpl,
        "%%NAME%%": rctx.attr.name,
    })

pip_repository_attrs = {
    "default_version": attr.string(
        mandatory = True,
        doc = """\
This is the default python version in the format of X.Y.Z. This should match
what is setup by the 'python' extension using the 'is_default = True'
setting.""",
    ),
    "repo_name": attr.string(
        mandatory = True,
        doc = "The apparent name of the repo. This is needed because in bzlmod, the name attribute becomes the canonical name.",
    ),
    "whl_map": attr.string_list_dict(
        mandatory = True,
        doc = "The wheel map where values are python versions",
    ),
    "_template": attr.label(
        default = ":requirements.bzl.tmpl",
    ),
}

pip_repository = repository_rule(
    attrs = pip_repository_attrs,
    doc = """A rule for bzlmod mulitple pip repository creation. PRIVATE USE ONLY.""",
    implementation = _pip_repository_impl,
)


# -*- coding: utf-8 -*-
import os
import time
from kubernetes import client, watch, config
import json
import re
import pandas as pd
from io import StringIO
import subprocess

# from check_pod import check_pod

# /proc/cpuinfo
def cpuinfo():
    info = os.popen("cat /proc/cpuinfo").read().split("\n")
    result = {}
    for i in range(len(info)):
        if info[i].find("\t: ") == -1:
            continue
        result[info[i].split("\t: ")[0]] = info[i].split("\t: ")[1]
    return result

# /proc/meminfo
def meminfo():
    info = os.popen("cat /proc/meminfo").read().split("\n")
    result = {}
    for i in range(len(info)):
        if info[i].find(":        ") == -1:
            continue
        result[info[i].split(":        ")[0]] = info[i].split(":        ")[1].strip()
    return result

# /proc/uptime
def uptime():
    info = os.popen("cat /proc/uptime").read().split(" ")
    result = {}
    result["run time"] = info[0].strip()
    result["idle time"] = info[1].strip()
    return result

# pending pod
def pod():
    config.load_kube_config()
    v1 = client.CoreV1Api()

    pending_pod = [x for x in v1.list_pod_for_all_namespaces(watch=False).items]
    # print(pending_pod)
    return str(pending_pod)

def getNodeName():
    config.load_kube_config()
    v1 = client.CoreV1Api()
    nodeList = []

    for n in v1.list_node().items:
        if "node" in n.metadata.name:
            nodeName = n.metadata.name
            nodeList.append(nodeName)
    return nodeList

# all ready nodes
def nodes():
    config.load_kube_config()
    v1 = client.CoreV1Api()
    ready_nodes = []
    resource = {}

    master_name = ""
    for n in v1.list_node().items:
        if "master" in n.metadata.name:
            master_name = n.metadata.name
            resource = {master_name: {}}
            # print(n.status.conditions)
        else:
            # print("11111")
            for status in n.status.conditions:
                if status.status == "True" and status.type == "Ready":
                    ready_nodes.append(n)
                    current_resource = {}
                    record_data_current_resource = {}
                    # print(n.metadata.name)
                    # total_str = os.popen('kubectl describe node ' + n.metadata.name).read()
                    # node_info = pd.read_csv(StringIO(subprocess.getoutput(
                    # 'kubectl top nodes '+ n.metadata.name)),sep='\s{2,}')
                    # for index,row in node_info.iterrows()
                    # cpu_number = row[1][:-1]#166m
                    # cpu_percent = row[2][:-1]#4%
                    # memory_number = row[3][:-2]#1646Mi
                    # memory_percent = row[4][:-1]#8%
                    # current_resource['memory'] = n.status.allocatable['memory']
                    # current_resource['ephemeral-storage'] = n.status.allocatable['ephemeral-storage']

                    total_str = os.popen(
                        "kubectl describe node " + n.metadata.name
                    ).read()
                    total = re.split("\n", total_str)
                    record_line_start = 0
                    for index_line in range(len(total)):
                        if "Allocated resources:" in total[index_line]:
                            record_line_start = index_line
                            break
                    memory = " ".join(total[record_line_start + 5].split())
                    # storage = ' '.join(total[-5].split())
                    try:
                        memory_percent = memory.split(" ")[-3][1:-2]
                        memory_number = memory.split(" ")[-4][0:-2]
                        # storage_percent = storage.split(' ')[-1][1:-2]
                        # storage_number = storage.split(' ')[-2]
                        current_resource["memory"] = {
                            "percent": memory_percent,
                            "number": memory_number,
                        }
                        # current_resource['storage'] = {'percent': storage_percent, 'number': storage_number}
                        resource[master_name][n.metadata.name] = current_resource
                        record_data_current_resource = current_resource
                    except Exception as e:
                        print(
                            "checkstatus nodes() error::",
                            e,
                            "BUT FIX TITH REOCRD DATA ",
                        )
                        print("memory:", memory)
                        resource[master_name][
                            n.metadata.name
                        ] = record_data_current_resource
                else:
                    pass
    if len(ready_nodes) == 0:
        return -1
    else:
        return resource

def check_status():
    result = []
    with open("./json/cpu_" + str(time.time()) + ".json", "w") as file_obj:
        cpu = cpuinfo()
        json.dump(cpu, file_obj)
        result.append(cpu)

    with open("./json/mem_" + str(time.time()) + ".json", "w") as file_obj:
        mem = meminfo()
        json.dump(mem, file_obj)
        result.append(mem)

    with open("./json/uptime_" + str(time.time()) + ".json", "w") as file_obj:
        up_time = uptime()
        json.dump(up_time, file_obj)
        result.append(up_time)

    with open("./json/pod_" + str(time.time()) + ".json", "w") as file_obj:
        pending_pod = pod()
        json.dump(pending_pod, file_obj)
        result.append(pending_pod)

    with open("./json/nodes_" + str(time.time()) + ".json", "w") as file_obj:
        ready_nodes = nodes()
        json.dump(ready_nodes, file_obj)
        result.append(ready_nodes)

    return result

if __name__ == "__main__":
    resource = nodes()
    print(resource)
    # while True:
    #     with open('./json/cpu_' + str(time.time()) + '.json', 'w') as file_obj:
    #         cpu = cpuinfo()
    #         json.dump(cpu, file_obj)
    #
    #     with open('./json/mem_' + str(time.time()) + '.json', 'w') as file_obj:
    #         mem = meminfo()
    #         json.dump(mem, file_obj)
    #
    #     with open('./json/uptime_' + str(time.time()) + '.json', 'w') as file_obj:
    #         uptime = uptime()
    #         json.dump(uptime, file_obj)
    #
    #     with open('./json/pod_' + str(time.time()) + '.json', 'w') as file_obj:
    #         pod = pod()
    #         json.dump(pod, file_obj)
    #
    #     with open('./json/nodes_' + str(time.time()) + '.json', 'w') as file_obj:
    #         nodes = nodes()
    #         json.dump(nodes, file_obj)
    #
    #     interval = 5000
    #     time.sleep(interval)


# https://school.programmers.co.kr/learn/courses/30/lessons/72410

import re

def solution(new_id):
    answer = ''
    # 1&2단계
    answer = re.sub('[^0-9a-z_.-]+', '', new_id.lower())
    # 3단계
    answer = re.sub('[..]+', '.', answer)
    # 4단계
    answer = answer.strip('.')
    # 5단계
    answer = "a" if answer == "" else answer
    # 6단계
    answer = answer[:15].rstrip('.')
    # 7단계
    if len(answer) <= 2:
        while len(answer) < 3:
            answer += answer[-1]

    return answer


import os
import csv
import matplotlib.pyplot as plt

#----- Define variables -----
parent_dir    = "aaa_Data" # Where B,D,R,RD folders saved. Leave as empty string to look in current directory.
folder_output = "a_Comparisons" # Code will automatically add parent_dir to this. Just name the folder.
models = [ "20210809_B/LOGS_17", "20210809_D_incH/LOGS_17", "20210809_R/LOGS_17", "20210809_RD_incH/LOGS_17" ]
folder_output = "Image_Backup/20210910_report/HRD-M=0p76"

species_list  = [ "A(Li)", "[Fe/H]_surf", "Y_surf" ]
title         = r"$Z=10^{-4}$" # r"$M/M_\odot=0.8$, $Z=0.001$"

line_colours  = [ "k", "b", "orange", "red", "green", "grey", "yellow" ]
line_styles   = [ "-", "--", ":", "-.", "-", "--", ":" ]

#----- Preferences -----
use_custom_xlim = False
use_custom_ylim = False
append_to_filename = False
add_title = False
use_custom_ylabel = True
move_title_up = True
show_legend = False
show_endpoint = False
use_custom_labels = True

custom_xlim = [ 3.68, 3.70 ]
custom_ylim = [ -17, -9 ]
append_to_filename_text = "age"
custom_ylabel_text = [ r"Lithium abundance $A(\rm{Li})$", r"Surface metallicity $[\rm{Fe}/\rm{H}]$", r"Surface He mass fraction $Y$" ]
title_y = 1.06

x_axis = "model_number" # "Teff" or "age" or "model_number"

# Option to read all folders in "loc", saves specifying models. Ignores files, but will fail if "loc" contains any folders that are not MESA logs. If in doubt, place logs in a nested folder and change loc to that.
use_all_folders = False
if use_all_folders:
	models = [ obj for obj in os.listdir( parent_dir ) if os.path.isdir(os.path.join( parent_dir, obj )) ]
	models.remove( "a_Comparisons" )

if parent_dir != "":
	folder_output = parent_dir + "/" + folder_output
custom_labels = [ "M=0." + str(m)[-2:] for m in models ] # [ "Standard", "Diffusion", "Rotation", "Rotation and diffusion" ]
labels = custom_labels if use_custom_labels else models

#----- Function to plot surface abundance vs Teff -----
def plot_surface_abundance( species, x_axis ):

	#----- Definitions -----
	x_vals = [ [] for m in models ]
	abundances = [ [] for m in models ]
	graph_filename = folder_output + "/Abundance-" + species.replace( "/", "" ) + "-vs-" + x_axis
	graph_title = title + ", species: " + species

	if append_to_filename:
		graph_filename += "-" + append_to_filename_text

	#----- Read csv files -----
	for i in range(len( models )):
		if parent_dir == "":
			the_folder = models[i]
		else:
			the_folder = parent_dir + "/" + models[i]

		model_data = csv.reader(open( the_folder + "/history.csv", "r" ))
		headers = next( model_data )

		if x_axis == "Teff":
			x_col_name = "log_Teff"
			fig_xlabel = r"$\log(T_{\rm{eff}})$"
			graph_type = "normal"
		elif x_axis == "age":
			x_col_name = "star_age"
			fig_xlabel = "Star age (yr)"
			graph_type = "normal"
		elif x_axis == "model_number":
			x_col_name = "model_number"
			fig_xlabel = "Model number"
			graph_type = "normal"
		else:
			raise ValueError( "X-column %s not understood. Please choose 'Teff' or 'age'." %Teff_or_age )

		x_col = headers.index( x_col_name )
		species_col = headers.index( species )

		for j, row in enumerate( model_data ):
			x_vals[i].append( float( row[ x_col ] ) )
			abundances[i].append( float( row[ species_col ] ) )

	#----- Plot graph -----
	plt.rcParams.update({ "font.size": 14 })
	plt.rc( "text", usetex=True )

	fig = plt.figure( figsize=(6,6/1.414) )
	ax = fig.add_subplot( 111 )

	for i in range(len( models )):
		col = 1 - ( i+1 ) / ( len(models) + 1 )
		if graph_type == "normal":
			ax.plot( x_vals[i], abundances[i], label=labels[i], color=line_colours[i], ls=line_styles[i], zorder=len(models)-i )
			#ax.plot( x_vals[i], abundances[i], label=labels[i], color=(col,col,col), ls="-", zorder=len(models)-i )
		elif graph_type == "semilogx":
			ax.semilogx( x_vals[i], abundances[i], label=labels[i], color=line_colours[i], ls=line_styles[i], zorder=len(models)-i )
		else:
			raise ValueError( "Graph type %s not recognised." %graph_type )
		if show_endpoint:
			ax.scatter( x_vals[i][-1], abundances[i][-1], s=20, color=line_colours[i], zorder=len(models)-i )

	ax.set_xlabel( fig_xlabel )
	if use_custom_ylabel:
		ax.set_ylabel( custom_ylabel_text[ species_list.index( species ) ] )
	else:
		ax.set_ylabel( "Surface abundance" )

	if use_custom_xlim:
		ax.set_xlim( custom_xlim[0], custom_xlim[1] )
	if use_custom_ylim:
		ax.set_ylim( custom_ylim[0], custom_ylim[1] )
	if add_title:
		if move_title_up:
			ax.set_title( graph_title, y=title_y )
		else:
			ax.set_title( graph_title )

	#--- Put legend outside the plot and save ---
	if show_legend:
		box = ax.get_position()
		ax.set_position( [box.x0, box.y0, box.width*0.8, box.height] )
		ax.legend( bbox_to_anchor=(1.0,1.0) )
	plt.savefig( graph_filename, bbox_inches="tight" )
	print( graph_filename )
	plt.close()

#----- Call the function -----
print( "Plots saved:" )
for x_axis in [ "Teff", "age", "model_number" ]:
	for sp in species_list :
		plot_surface_abundance( sp, x_axis )


#!/usr/bin/env python
"""The setup script."""

from glob import glob
from os.path import basename
from os.path import splitext

from setuptools import find_packages
from setuptools import setup

with open('README.md') as readme_file:
    readme = readme_file.read()

requirements = ['Click>=6.0', 'dbispipeline']

setup(
    author='Michael Voetter',
    author_email='michael.voetter@uibk.ac.at',
    classifiers=[
        'Development Status :: 2 - Pre-Alpha',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: BSD License',
        'Natural Language :: English',
        'Programming Language :: Python :: 2',
        'Programming Language :: Python :: 2.7',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.4',
        'Programming Language :: Python :: 3.5',
        'Programming Language :: Python :: 3.6',
    ],
    description='MediaEval2020 contains DBIS pipeline configurations.',
    entry_points={
        'console_scripts': ['mediaeval2020=mediaeval2020.cli:main'],
    },
    install_requires=requirements,
    license='BSD license',
    long_description=readme,
    include_package_data=True,
    keywords='mediaeval2020',
    name='mediaeval2020',
    packages=find_packages('src'),
    package_dir={'': 'src'},
    py_modules=[splitext(basename(path))[0] for path in glob('src/*.py')],
    setup_requires=[],
    test_suite='tests',
    tests_require=[],
    url='https://dbis.uibk.ac.at',
    version='0.1.0',
    zip_safe=False,
)


import warnings
from queue import SimpleQueue, PriorityQueue

class Graph:
    def __init__(self,isDirected,**kwargs):
        self.nodeList = []
        self.edges = {}
        self.nodeNames = {}
        self.isDirected = isDirected
        self.isWeighted = False
        self.n = 0
        # If an edge list was provided, read it into the graph
        for key, value in kwargs.items():
            if key == 'edgeList':
                for edge in value:
                    # If the source of the edge is not in the graph, add it
                    if not edge[0] in self.nodeNames:
                        self.addNode(str(edge[0]),[])

                    # If the target of the edge is not in the graph, add it
                    if not edge[1] in self.nodeNames:
                        self.addNode(str(edge[1]),[])

                    # Add the edge with an optional given weight (1 default)
                    if len(edge) == 2:
                        self.addEdge(edge[0],edge[1],weight=1)
                    elif len(edge) == 3:
                        self.addEdge(edge[0],edge[1],weight=edge[2])
                    else:
                        raise EdgeListNumberError()

    def addNode(self,name,edges):
        #
        if name in self.nodeNames.values():
            raise DuplicateNodeNameError(f'Node {name} already exists in the\
                                         graph')
        else:
            self.nodeList.append(self.n)
            self.edges[self.n] = {}
            for edge in edges:
                if len(edge) == 1:
                    self.addEdge(self.n,edge[0],1)
                else:
                    self.addEdge(self.n,edge[0],edge[1])
            self.nodeNames[self.n] = name
            self.n += 1

        if not self.isDirected:
            for edge in edges:
                if (edge[0] > self.n - 1):
                    self.nodeList.pop(self.n - 1)
                    self.edges.pop(self.n - 1)
                    self.nodeNames.pop(self.n - 1)
                    self.n -= 1
                    raise NodeNotFoundError(f'Node {edge} is not in the graph yet')
                else:
                    self.addEdge(edge[0],self.n-1)

    def addEdge(self,src,tgt,**kwargs):
        if (src > self.n - 1):
            raise NodeNotFoundError(f'Node {src} is not in the graph yet')
        elif (tgt > self.n - 1):
            raise NodeNotFoundError(f'Node {tgt} is not in the graph yet')
        else:
            if tgt not in self.edges[src]:
                if 'weight' in kwargs:
                    self.edges[src][tgt] = kwargs['weight']
                else:
                    self.edges[src][tgt] = 1
            else:
                warnings.warn('Edge {src} --> {tgt} already exists')

    def removeNode(self,nodeIndex):
        self.nodeNames.pop(nodeIndex)
        self.nodeList.pop(nodeIndex)
        self.edges.pop(nodeIndex)

        for node,edgeDict in self.edges.items():
            if nodeIndex in edgeDict:
                edgeDict.pop(nodeIndex)
        self.n -= 1

    def topological_sort(self):
        # Check to make sure graph is directed
        if not self.isDirected:
            raise NoTopoSortOnUndirectedGraphError()
        # Check to make sure graph is acyclic
        elif self.has_cycle():
            raise NoTopoSortOnCyclicGraphError()
        # If checks pass, do topological sort
        else:
            order = SimpleQueue() # Queue to store topological sort order
            processNext = SimpleQueue() # Queue to store nodes to process next
            nIncoming = [0]*self.n # 'Array' to hold number of incoming edges
            # Count number of incoming edges for each node
            for node in self.nodeList:
                for edge in self.edges[node]:
                    nIncoming[edge] += 1
            # Add all nodes with no incoming edges to processNext
            for node in self.nodeList:
                if nIncoming[node] == 0:
                    processNext.put(node)

            while not processNext.empty():
                curNode = processNext.get()
                for tgt in self.edges[curNode]:
                    nIncoming[tgt] -= 1
                    if nIncoming[tgt] == 0:
                        processNext.put(tgt)

                order.put(curNode)

            return order

    def has_cycle(self):
        if not self.isDirected:
            raise AllUndirectedGraphsHaveCyclesError()
        discovered = set()
        finished = set()
        for node in self.nodeList:
            if node not in discovered and node not in finished:
                discovered, finished, cycle_detected = self.dfs_visit(node,discovered,finished)
                if cycle_detected:
                    return True

        return False

    def dfs_visit(self,node,discovered,finished):
        discovered.add(node)
        cycle_detected = False
        for tgt,weight in self.edges[node].items():
            if tgt in discovered:
                cycle_detected = True
                return discovered, finished, cycle_detected
            elif tgt not in finished:
                discovered, finished, cycle_detected = self.dfs_visit(tgt,discovered,finished)

        discovered.remove(node)
        finished.add(node)
        return discovered, finished, cycle_detected

    def dijkstra(self,start):
        """Implementation of Dijkstra's algorithm to find all shortest paths
        from a given node"""
        path_weight = {i : float('inf') for i in range(self.n)}
        path_weight[start] = 0
        previous = {i : float('nan') for i in range(self.n)}
        remaining = PriorityQueue()
        for node,priority in path_weight.items():
            remaining.put((priority,node))

        while not remaining.empty():
            priority,node = remaining.get()
            for tgt,weight in self.edges[node].items():
                possibleNewWeight = path_weight[node] + weight
                if (possibleNewWeight < path_weight[tgt]):
                    path_weight[tgt] = possibleNewWeight
                    previous[tgt] = node

        return path_weight, previous

class GraphError(Exception):
    """Base class for exceptions in the Graph module."""
    pass

class NodeNotFoundError(GraphError):
    """Exception raised when a edge to a non-existent node is being added"""

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

class DuplicateNodeNameError(GraphError):
    """Exception raised when a node is added with a name of an existing node"""

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

class NoTopoSortOnUndirectedGraphError(GraphError):

    def __init__(self):
        self.message = 'Topological sort does not work on undirected graphs.'

class NoTopoSortOnCyclicGraphError(GraphError):

    def __init__(self):
        self.message = 'Topological sort does not work on cyclic graphs.'

class AllUndirectedGraphsHaveCyclesError(GraphError):

    def __init__(self):
        self.message = 'All undirected graphs have cycles.'

class EdgeListNumberError(GraphError):

    def __init__(self):
        self.message = 'Edges in list given to __init__ must be tuples of\
        length 2 or 3'


import requests
import time
from decouple import config
from goto import goto

TOKEN = config("TOKEN")
DESIRED_NAME = config("NAME")

auth = {"Authorization": "Token " + TOKEN}

def get_current_room():
    res = requests.get(
        "https://lambda-treasure-hunt.herokuapp.com/api/adv/init/",
        headers=auth
    )
    res_json = res.json()
    time.sleep(res_json["cooldown"])
    return res_json["room_id"]

def change_name():
    res = requests.post(
        "https://lambda-treasure-hunt.herokuapp.com/api/adv/change_name/",
        headers=auth,
        json={"name": DESIRED_NAME, "confirm": "aye"}
    )
    return res.json()

if __name__ == "__main__":
    current_room = get_current_room()
    change_name_room = "467"
    goto(str(current_room), change_name_room)
    response = change_name()
    print(response)


import csv
import numpy as np
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt

# Extracts label data from csv file
def extractLabels(path):
  labels = []
  lines = csv.reader(open(path, "r"))
  lines = list(lines)
  for line in lines:
    label = int(line[0])
    labels.append(label)
  return labels

# Extracts image data from csv file
def extractImages(path):
  images = []
  lines = csv.reader(open(path, "r"))
  lines = list(lines)
  for line in lines:
    pixels = line
    pixelsFloat = [float(pixel) for pixel in pixels]
    images.append(pixelsFloat)
  return images

# Extracts label data from csv file
def initialW(path):
  W = []
  lines = csv.reader(open(path, "r"))
  lines = list(lines)
  for line in lines:
    ws = line
    wf = [float(w) for w in ws]
    W.append(wf)
  return W

# Extracts label data from csv file
def initialV(path):
  V = []
  lines = csv.reader(open(path, "r"))
  lines = list(lines)
  for line in lines:
    vs = line
    vf = [float(v) for v in vs]
    V.append(vf)
  return V

# Sigmoid Function
def sigmoid(a):
  return (1 / (1 + np.exp(-a)))

# Softmax Function
def softmax(scores):
  scores = np.exp(scores)
  scores = scores / np.sum(scores, axis=1)[:, np.newaxis]  # row sum is axis=1
  return scores

# Safe logarithm function
def safelog(a):
  return np.log(a + 1e-100)

def cbindOnes(matrix):
  if (len(matrix.shape) == 1):
    return np.hstack((np.ones((matrix.shape[0], 1)), matrix[:, np.newaxis]))
  else:
    return np.hstack((np.ones((matrix.shape[0], 1)), matrix))

def rbindOnes(matrix):
  if (len(matrix.shape) == 1):
    return np.vstack((np.ones((1, matrix.shape[0])), matrix[np.newaxis, :]))
  else:
    return np.vstack((np.ones((1, matrix.shape[1])), matrix))

def oneHotEncoding(y, sampleCount, labelCount):
  # Input is for example [1, 2, 2, 5]
  # Output is then: 1 0 0 0 0
  #                 0 1 0 0 0
  #                 0 1 0 0 0
  #                 0 0 0 0 1
  y_oh = np.zeros((sampleCount, labelCount))
  y_oh[np.arange(sampleCount)[:], y[:] - 1] = 1
  return y_oh

def labelPrediction(y_pred):
  numSamples = y_pred.shape[0]
  y_labels = np.zeros(numSamples, dtype=int)
  for i in range(numSamples):
    y_labels[i] = np.argmax(y_pred[i]) + 1
  return y_labels

def gradientW(X, Z, V, y_true, y_pred):
  return -cbindOnes(X).T.dot((y_true - y_pred).dot(V[1:].T) * Z * (1 - Z))

def gradientV(Z, y_true, y_pred):
  return -Z.T.dot(y_true - y_pred)

def learn(X, y, W, V, eta, epsilon, max_iteration):
  iteration = 0

  Z = sigmoid(cbindOnes(X).dot(W))  # (500, 785).(785, 20) = (500, 20)
  y_pred = softmax(cbindOnes(Z).dot(V))  # (500, 21).(21, 5) = (500, 5)
  objective_values = [-np.sum(y * safelog(y_pred))]  # i = classes, t = data points

  while (1):
    Z = sigmoid(cbindOnes(X).dot(W))  # (500, 785).(785, 20) = (500, 20)
    y_pred = softmax(cbindOnes(Z).dot(V))  # (500, 21).(21, 5) = (500, 5)

    deltaV = gradientV(Z, y, y_pred)
    deltaW = gradientW(X, Z, V, y, y_pred)

    V[1:] = V[1:] - eta * deltaV
    W = W - eta * deltaW

    Z = sigmoid(cbindOnes(X).dot(W))
    y_pred = softmax(cbindOnes(Z).dot(V))

    objective_values.append(-np.sum(y * safelog(y_pred)))
    if iteration >= max_iteration or np.abs(objective_values[iteration] - objective_values[iteration + 1]) < epsilon:
      break

    iteration += 1

  return [y_pred, objective_values, W, V, iteration]

# H = 20
# X = N x D  with +1 being bias
# W = (D+1) x 20
# Z = X with bias . W = N x 20
# V = (20+1) x 5
# O = Z with bias . V = N x 5

#### START #####
# Initial data
images = extractImages('images.csv')  # A CSV file where each line holds pixels
labels = extractLabels('labels.csv')  # A CSV file where each line has a number for the label
labelToClass = {1: "T-Shirt", 2: "Trouser", 3: "Dress", 4: "Sneaker", 5: "Bag"}  # Labels
eta = 0.0005  # Initialize Learning rate to 0.0005
epsilon = 1e-3  # Initialize minimum error to 1e-3
max_iteration = 500  # Initialize maximum iteration count to 500
W = np.array(initialW('initial_W.csv'))  # Initial weights (I to H) (785, 20)
V = np.array(initialV('initial_V.csv'))  # Initial weight (H to O) (21, 5)

# Divide the data randomly into 2 parts: [:500] as training, [500:200] as test
X_train = np.array(images[:500])  # (500, 784)
y_train = np.array(labels[:500])  # (500, )
X_test = np.array(images[500:1000])  # (500, 784)
y_test = np.array(labels[500:1000])  # (500, )
N_train, C_train = X_train.shape[0], len(labelToClass)
N_test, C_test = X_test.shape[0], len(labelToClass)
y_train_oh = oneHotEncoding(y_train, N_train, C_train)

# Learn
y_train_pred, objective_values_train, W, V, iterationCount = learn(X_train, y_train_oh, W, V, eta, epsilon,
                                                                   max_iteration)
y_test_pred = softmax(cbindOnes(sigmoid(cbindOnes(X_test).dot(W))).dot(V))
print("Finished in this many iterations: ", iterationCount)

# Plot the Iterations / Error graph
fig, ax = plt.subplots()
ax.plot(np.arange(iterationCount + 2), objective_values_train)  # +2 one from the start and one before end
ax.set(xlabel='Iteration', ylabel='Error')
fig.savefig("test.png")
plt.show()

# Calculate confusion matrixes
y_train_pred_labels = labelPrediction(y_train_pred)
y_test_pred_labels = labelPrediction(y_test_pred)
conf_train = confusion_matrix(y_train_pred_labels, y_train, labels=[1, 2, 3, 4, 5])
print(conf_train)
print('\n')
conf_test = confusion_matrix(y_test_pred_labels, y_test, labels=[1, 2, 3, 4, 5])
print(conf_test)


import math

def callback(data):
    # 라이다에서 측정한 거리, 각도 정보를 가져옴
    distances = data.ranges
    angles = [math.radians(i) for i in range(len(distances))]

    # 거리, 각도 정보에서 가장 가까운 객체의 거리 및 각도 계산
    closest_distance = min(d for d in distances if d > 0.0)
    closest_angle = angles[distances.index(closest_distance)]

    # 객체의 좌표 계산
    x = closest_distance * math.cos(closest_angle)
    y = closest_distance * math.sin(closest_angle)
    print("Object coordinates: ({}, {})".format(x, y))

'''
레퍼런스
http://wiki.ros.org/laser_geometry#Coordinate_Calculation
https://www.jbmpa.com/python_tip/10
좌표 출력'''


import os
import glob

class HardCodeVariable():
    """
    Where did you put your data ?
    set it here, so you don't have to set in every script
    """
    def __init__(self):
        self.UCF_CC_50_PATH = "/data/cv_data/UCFCrowdCountingDataset_CVPR13_with_people_density_map/UCF_CC_50"
        self.SHANGHAITECH_PATH = "/data/ShanghaiTech"
        self.SHANGHAITECH_PATH_PART_A = "/data/ShanghaiTech/part_A/"
        self.SHANGHAITECH_PATH_PART_B = "/data/ShanghaiTech/part_B/"
        self.SHANGHAITECH_PATH_TRAIN_POSTFIX = "train_data"
        self.SHANGHAITECH_PATH_TEST_POSTFIX = "test_data"

if __name__ == "__main__":
    """
    Test if path is working
    """
    hard_code = HardCodeVariable()
    if os.path.exists(hard_code.SHANGHAITECH_PATH):
        print("exist " + hard_code.SHANGHAITECH_PATH)
        print("let see if we have train, test folder")
        train_path_a = os.path.join(hard_code.SHANGHAITECH_PATH_PART_A, hard_code.SHANGHAITECH_PATH_TRAIN_POSTFIX)
        train_path_b = os.path.join(hard_code.SHANGHAITECH_PATH_PART_B, hard_code.SHANGHAITECH_PATH_TRAIN_POSTFIX)
        test_path_a = os.path.join(hard_code.SHANGHAITECH_PATH_PART_A, hard_code.SHANGHAITECH_PATH_TEST_POSTFIX)
        test_path_b = os.path.join(hard_code.SHANGHAITECH_PATH_PART_B, hard_code.SHANGHAITECH_PATH_TEST_POSTFIX)
        if os.path.exists(train_path_a):
            print("exist " + train_path_a)
        if os.path.exists(train_path_b):
            print("exist " + train_path_b)
        if os.path.exists(test_path_a):
            print("exist " + test_path_a)
        if os.path.exists(test_path_b):
            print("exist " + test_path_b)
        print("count number of image")
        image_folder_list = [train_path_a, train_path_b, test_path_a, test_path_b]
        for image_root_path in image_folder_list:
            image_path_list = glob.glob(os.path.join(image_root_path, "images", "*.jpg"))
            density_path_list = glob.glob(os.path.join(image_root_path, "ground-truth-h5", "*.h5"))
            count_img = len(image_path_list)
            count_density_map = len(density_path_list)
            first_img = image_path_list[0]
            first_density_map = density_path_list[0]
            print("in folder " + image_root_path)
            print("--- total image" + str(count_img))
            print('--- first img ' + first_img)
            print("--- total density map " + str(count_density_map))
            print("--- first density map " + str(first_density_map))
            if count_img == count_density_map:
                print("--- number of density map = number of image")
            else:
                print("xxxxx number of density map !!!!!= number of image")


import requests
import os
import schedule
import time

def job():
    link = os.environ['APILINK']
    try:
        requests.get(link)
    except:
        None

if __name__ == "__main__":
    schedule.every(20).minutes.do(job)
    while True:
        schedule.run_pending()
        time.sleep(60)