from __future__ import print_function
import glob
import os
import subprocess
import sys

IGNORES = [
    "bin",
    ".git",
    ".gitignore",
    "ST4",
    "VS-Code",
    "My Twighlight.tmTheme",
    "zsh-configs",
]

def shell_out(command_args, verbose=False):
    if verbose:
        print(" ".join(command_args))

    subprocess.call(command_args)

def backup_file(link_name, verbose=False):
    old_dotfiles_path = "/tmp/olddotfiles"

    if os.path.islink(link_name):
        shell_out(["rm", link_name], verbose=verbose)
    elif os.path.lexists(link_name):
        subprocess.call(["mkdir", "-p", old_dotfiles_path])
        shell_out(
            [
                "mv",
                link_name,
                os.path.join(old_dotfiles_path, os.path.basename(link_name)),
            ],
            verbose=verbose,
        )

def link_st(home_directory, verbose=False):
    filename = os.path.normpath(os.path.realpath("ST4/User"))
    link_name = os.path.join(
        home_directory,
        "Library",
        "Application Support",
        "Sublime Text",
        "Packages",
        "User",
    )
    backup_file(link_name, verbose=verbose)
    shell_out(["ln", "-s", filename, link_name], verbose=verbose)

def link_vscode(home_directory, verbose=False):
    filename = os.path.join(
        os.path.normpath(os.path.realpath("VS-Code")), "settings.json"
    )
    link_name = os.path.join(
        home_directory,
        "Library",
        "Application Support",
        "Code",
        "User",
        "settings.json",
    )
    backup_file(link_name, verbose=verbose)
    shell_out(["ln", "-s", filename, link_name], verbose=verbose)

    filename = os.path.join(
        os.path.normpath(os.path.realpath("VS-Code")), "keybindings.json"
    )
    link_name = os.path.join(
        home_directory,
        "Library",
        "Application Support",
        "Code",
        "User",
        "keybindings.json",
    )
    backup_file(link_name, verbose=verbose)
    shell_out(["ln", "-s", filename, link_name], verbose=verbose)

    filename = os.path.join(os.path.normpath(os.path.realpath("VS-Code")), "snippets")
    link_name = os.path.join(
        home_directory, "Library", "Application Support", "Code", "User", "snippets",
    )
    backup_file(link_name, verbose=verbose)
    shell_out(["ln", "-s", filename, link_name], verbose=verbose)

def link_zsh(home_directory, verbose=False):
    filename = os.path.normpath(os.path.realpath(".zshenv"))
    link_name = os.path.join(home_directory, ".zshenv")
    backup_file(link_name, verbose=verbose)
    shell_out(["ln", "-sFf", filename, link_name], verbose=verbose)

    filename = os.path.normpath(os.path.realpath("zsh-configs"))
    link_name = os.path.join(home_directory, ".zsh-configs")
    shell_out(["ln", "-sFf", filename, link_name], verbose=verbose)

    filename = os.path.normpath(
        os.path.join(os.path.realpath("zsh-configs"), "toastdriven.zsh")
    )
    prompts_name = os.path.join(home_directory, ".zprompts")

    if not os.path.exists(prompts_name):
        os.makedirs(prompts_name)

    link_name = os.path.join(prompts_name, "prompt_toastdriven_setup")
    shell_out(["ln", "-sFf", filename, link_name], verbose=verbose)

def link_fish(home_directory, verbose=False):
    filename = os.path.normpath(os.path.realpath("fish-configs"))
    link_name = os.path.join(home_directory, ".config", "fish")
    backup_file(link_name, verbose=verbose)

    config_name = os.path.join(home_directory, ".config")

    if not os.path.exists(config_name):
        os.makedirs(config_name)

    shell_out(["ln", "-sFf", filename, link_name], verbose=verbose)
    print("To setup fish:")
    print("    $ echo /usr/local/bin/fish | sudo tee -a /etc/shells")
    print("    $ chsh -s /usr/local/bin/fish")
    print("    $ ./.config/fish/setup_initial_env_vars.sh")

def link_kitty(home_directory, verbose=False):
    filename = os.path.normpath(os.path.realpath("kitty-configs/kitty.conf"))
    link_name = os.path.join(home_directory, ".config", "kitty", "kitty.conf")
    backup_file(link_name, verbose=verbose)

    config_name = os.path.join(home_directory, ".config", "kitty")

    if not os.path.exists(config_name):
        os.makedirs(config_name)

    shell_out(["ln", "-sFf", filename, link_name], verbose=verbose)
    print("To install kitty:")
    print("    $ curl -L https://sw.kovidgoyal.net/kitty/installer.sh | sh /dev/stdin")

def setup_dotfiles(home_directory, verbose=False):
    dotfile_pattern = os.path.join(".*")
    dotfile_files = glob.glob(dotfile_pattern)
    setup_symlinks(home_directory, dotfile_files, verbose=verbose)

    regular_pattern = os.path.join("*")
    regular_files = glob.glob(regular_pattern)
    setup_symlinks(home_directory, regular_files, verbose=verbose)

    create_gitingore(home_directory, verbose=verbose)
    link_st(home_directory, verbose=verbose)
    link_vscode(home_directory, verbose=verbose)
    link_zsh(home_directory, verbose=verbose)
    link_fish(home_directory, verbose=verbose)
    link_kitty(home_directory, verbose=verbose)

def setup_symlinks(home_directory, files, verbose=False):
    for filename in files:
        ignored = False

        for ignore_me in IGNORES:
            if filename == ignore_me:
                ignored = True
                break

        if ignored:
            continue

        full_path = os.path.normpath(os.path.realpath(filename))
        link_name = os.path.join(home_directory, os.path.basename(filename))

        backup_file(link_name, verbose=verbose)
        shell_out(["ln", "-s", full_path, link_name], verbose=verbose)

def create_gitingore(home_directory, verbose=False):
    gitignore_path = os.path.join(home_directory, ".gitignore")
    backup_file(gitignore_path, verbose=verbose)

    gitignore_file = open(gitignore_path, "w")
    gitignore_file.write("*.pyc\n")
    gitignore_file.write(".DS_Store\n")
    gitignore_file.close()

def usage():
    print("Usage: python bin/install.py [-v]")
    print("Please run from the top-level ``dotfiles`` directory.")
    sys.exit(1)

if __name__ == "__main__":
    verbose = False

    if len(sys.argv) > 2:
        usage()

    if not sys.argv[0].startswith("bin/"):
        usage()

    if len(sys.argv) == 2 and sys.argv[1] in ["-v", "--verbose"]:
        verbose = True

    home_directory = os.path.expanduser("~")
    setup_dotfiles(home_directory, verbose=verbose)


import os
import cv2
import glob

carpeta = "/home/alvaro.nieva/Documents/nieva_TFM/data/TUM_KITCHEN/cam3"
fps = 25
image_list = os.listdir(carpeta)
image_list.sort()

img_array = []
for filename in image_list:
    name = carpeta + '/' + filename
    img = cv2.imread(name)
    height, width, layers = img.shape
    size = (width,height)
    img_array.append(img)

out = cv2.VideoWriter('./data/tmktch_cam3.avi',cv2.VideoWriter_fourcc(*'DIVX'), fps, size)

for i in range(len(img_array)):
    out.write(img_array[i])
out.release()
print("Done")


# Import Splinter, BeautifulSoup, and Pandas
from splinter import Browser
from bs4 import BeautifulSoup as soup
import pandas as pd
from webdriver_manager.chrome import ChromeDriverManager

# Set the executable path and initialize Splinter
executable_path = {'executable_path': ChromeDriverManager().install()}
browser = Browser('chrome', **executable_path)

# ### Visit the NASA Mars News Site

# Visit the mars nasa news site
url = 'https://redplanetscience.com'
browser.visit(url)

# Optional delay for loading the page
browser.is_element_present_by_css('div.list_text', wait_time=1)

# Convert the browser html to a soup object and then quit the browser
html = browser.html
news_soup = soup(html, 'html.parser')
slide_elem = news_soup.select_one('div.list_text')

slide_elem.find('div', class_= 'content_title')

# Use the parent element to find the first a tag and save it as `news_title`
news_title = slide_elem.find('div', class_='content_title').get_text()
news_title

# Use the parent element to find the paragraph text
news_p = slide_elem.find('div', class_='article_teaser_body').get_text()
news_p

# ### JPL Space Images Featured Image
# Visit URL
url = 'https://data-class-jpl-space.s3.amazonaws.com/JPL_Space/index.html'
browser.visit(url)

# Find and click the full image button
full_image_elem = browser.find_by_tag('button')[1]
full_image_elem.click()

# Parse the resulting html with soup
html = browser.html
img_soup = soup(html, 'html.parser')
img_soup

# find the relative image url
img_url_rel = img_soup.find('img', class_='fancybox-image').get('src')
img_url_rel

# Use the base url to create an absolute url
img_url = f'https://data-class-jpl-space.s3.amazonaws.com/JPL_Space/{img_url_rel}'
img_url

# ### Mars Facts

df = pd.read_html('https://data-class-mars-facts.s3.amazonaws.com/Mars_Facts/index.html')[0]
df.head()

df.columns=['Description', 'Mars', 'Earth']
df.set_index('Description', inplace=True)
df

df.to_html()

# # D1: Scrape High-Resolution Mars’ Hemisphere Images and Titles

# ### Hemispheres

# 1. Use browser to visit the URL
url = 'https://astrogeology.usgs.gov/search/results?q=hemisphere+enhanced&k1=target&v1=Mars'
browser.visit(url)

# 2. Create a list to hold the images and titles.
# HTML Object
html_hemispheres = browser.html

# Parse HTML with Beautiful Soup
image_soup = soup(html_hemispheres, 'html.parser')

# Retreive all items that contain mars hemispheres information
results = image_soup.find_all('div', class_='collapsible results')

prac = image_soup.body.find_all('h3')

title = []

for pr in prac:
    title.append(pr.text)

    #title

# Create empty list for hemisphere urls

thumbnail_results = image_soup.body.find_all('a', class_ = 'itemLink product-item')
thumbnail_links = []

for thumbnail in thumbnail_results:

    # If the thumbnail element has an image...
    if (thumbnail.img):

        # then grab the attached link
        thumbnail_url = 'https://astrogeology.usgs.gov' + thumbnail['href']

        # Append list with links
        thumbnail_links.append(thumbnail_url)

#thumbnail_links

# Store the main_ul
hemisphere_url = 'https://astrogeology.usgs.gov'

# Loop through the items previously stored
#for result in results:

    # Store link that leads to full image website
img_url = []

    # Visit the link that contains the full image website
for urls in thumbnail_links:

    browser.visit(urls)

        # HTML Object of individual hemisphere information website
    partial_img_html = browser.html

        # Parse HTML with Beautiful Soup for every individual hemisphere information website
    parse = soup(partial_img_html, 'html.parser')

        # Retrieve full image source
    img_urls = parse.body.find('a', text = 'Sample')['href']

    img_url.append(img_urls)

#for dict in img_url:
    hemispheres = {}
    hemispheres['title'] = title
    hemispheres['img_url'] = img_url

#hemispheres

mars_hemi_zip = zip(title, img_url)

hemispheres = []

        #Iterate through the zipped object
for titles, img in mars_hemi_zip:

    mars_hemi_dict = {}

            # Add hemisphere title to dictionary
    mars_hemi_dict['title'] = titles

            # Add image url to dictionary
    mars_hemi_dict['img_url'] = img

            # Append the list with dictionaries
    hemispheres.append(mars_hemi_dict)

hemispheres


import pandas as  pd
import numpy as np
import re
from sklearn.svm import SVC
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import mean_squared_error
from sklearn.metrics import confusion_matrix
from sklearn.metrics import f1_score
from sklearn.preprocessing import MinMaxScaler
import xgboost as xgb
from imblearn.over_sampling import SMOTE
from imblearn.under_sampling import NearMiss
from sklearn.decomposition import PCA
from datetime import datetime
from datetime import date
import pymysql
pymysql.__version__
'0.9.3'

# Data
users = pd.read_sql_query('''SELECT* FROM td_user''', mysql_conn)

data = pd.read_csv('diab_risk.csv', encoding= 'utf-8')
data = pd.merge(data,users, left_on='id', right_on='email')

data = data[['What gender are you?', 'What is your ethnic group?',
       'What is your occupational status?',
       'What is your household income?',
       'Which type of diabetes do you have?',
       'How many grams of carbs do you eat daily (approximately to the nearest gram)?',
       'Intense hunger', 'Tiredness', 'Difficulty concentrating',
       'Spikes in blood glucose levels', 'Mood swings', 'Irritability',
       'Anxiety',
       'What is your height?', 'What is your waist circumference?',
       'What is your current weight in stones and pounds?', 'Unnamed: 24',
       'What is your weight in kilograms (to the nearest kilogram)?',
       'dob']]

data.columns = ['gender', 'ethnic group',
       'occupational status', 'income','diabetes_type',
       'daily carbs',
       'intense hunger', 'tiredness', 'difficulty concentrating',
       'spikes in blood glucose levels', 'mood swings', 'irritability',
       'anxiety',
       'height', 'waist',
       'stones', 'pounds',
       'weight_kg',
       'dob']

data.isnull().sum() #null values in each column of the DataFrame

# weights in Kgs from stones and pounds
def stones_pounds(data):
    # stones
    data = data.reset_index()
    stones = data[['stones']]
    stones = stones.reset_index()

    # preprocess weight
    pounds = data[["pounds"]]
    pounds.columns =['pounds']

    df = pd.concat([stones,pounds],axis=1)
    df = df.drop(df.index[0])
    df = df.reset_index()
    df = df.dropna()
    df = df.astype(int)
    # concat stones and pounds
    df['new_weights'] = df['stones'].astype(str)+"."+df['pounds'].astype(str)
    df =df.drop(columns=['stones','pounds'])
    df = df.astype(float)
    df['new_weights'] = (df['new_weights']*6.4).round()
    df = df.drop(columns=['level_0','index'])
    return df

# filling nans with converted weights
data['weight_kg'] = data.weight_kg.fillna(df.new_weights)
data = data.drop(columns=['stones','pounds'])
data = data.dropna()

def preprocess_height(data):

    data['height'] = data['height'].str.split('/').str[1]
    data['height'] = data['height'].str.replace(r'm','')
    return data

def preprocess_waist(data):

    data['waist'] = data['waist'].str.split('/').str[1]
    data['waist'] = data['waist'].str.replace(r'cm','')
    return data

data = data.dropna()

def cal_age(data):

    data['age'] = data['dob'].apply(lambda x: x.year)
    data['age'] = 2019 - data['age']

    return data

def encoder(final_df):

    final_features = final_df.drop(columns=[
        # 'index',
        'daily carbs','height','waist','weight_kg','age'])
    encoder = LabelEncoder()
    # encoding features
    final_features = final_features.apply(LabelEncoder().fit_transform)
    df = final_df[['daily carbs','height','waist','weight_kg','age']]
    final_df = pd.concat([final_features,df],axis=1)

    return final_df

def scale(encoded_df):
    # feature mattrix
    scaler = MinMaxScaler()
    x = encoded_df.drop(columns=['diabetes_type'])
    x = pd.DataFrame(scaler.fit_transform(x))

    return x

# correlation mattrix
corr = x.corr()
corr.style.background_gradient(cmap='coolwarm').set_precision(4)

# label
y = encoded_df[['diabetes_type']]

# idex2label
Labels = final_df['diabetes_type'].values
ids = encoded_df['diabetes_type'].values
idx2intent = {i:j for i, j in zip(ids, Labels)}
idx2intent[0]

x = x.reset_index()
y = y.reset_index()
features = pd.concat([x,y],axis=1)
no_diab = features[features['diabetes_type']==3]
prediab = features[features['diabetes_type']==6]
type1 = features[features['diabetes_type']==7]
type2 = features[features['diabetes_type']==8]

final_features = pd.concat([no_diab, prediab, type1, type2])
x = final_features[[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]]
y = final_features[['diabetes_type']]

# gaussian distribution plot
from scipy.stats import norm
import matplotlib.pyplot as plt
import math
mu = 0
variance = 1
sigma = math.sqrt(variance)
plt.plot(no_diab_dist[3], norm.pdf(no_diab_dist[3],mu,sigma))
plt.show()

# Bivariate Gaussian

from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D

# Our 2-dimensional distribution will be over variables X and Y
N = 60
# X = np.linspace(-3, 3, N)
X = no_diab_dist
Y = no_diab_y
X, Y = np.meshgrid(X, Y)

# Mean vector and covariance matrix
mu = np.array([0., 1.])
Sigma = np.array([[ 1. , -0.5], [-0.5,  1.5]])

# Pack X and Y into a single 3-dimensional array
pos = np.empty(X.shape + (2,))
pos[:, :, 0] = X
pos[:, :, 1] = Y

def multivariate_gaussian(pos, mu, Sigma):
    """Return the multivariate Gaussian distribution on array pos.

    pos is an array constructed by packing the meshed arrays of variables
    x_1, x_2, x_3, ..., x_k into its _last_ dimension.

    """

    n = mu.shape[0]
    Sigma_det = np.linalg.det(Sigma)
    Sigma_inv = np.linalg.inv(Sigma)
    N = np.sqrt((2*np.pi)**n * Sigma_det)
    # This einsum call calculates (x-mu)T.Sigma-1.(x-mu) in a vectorized
    # way across all the input variables.
    fac = np.einsum('...k,kl,...l->...', pos-mu, Sigma_inv, pos-mu)

    return np.exp(-fac / 2) / N

# The distribution on the variables X, Y packed into pos.
Z = multivariate_gaussian(pos, mu, Sigma)

# Create a surface plot and projected filled contour plot under it.
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_surface(X, Y, Z, rstride=3, cstride=3, linewidth=1, antialiased=True,
                cmap=cm.viridis)

cset = ax.contourf(X, Y, Z, zdir='z', offset=-0.15, cmap=cm.viridis)

# Adjust the limits, ticks and view angle
ax.set_zlim(-0.15,0.2)
ax.set_zticks(np.linspace(0,0.2,5))
ax.view_init(27, -21)

plt.show()

x_train,x_test,y_train,y_test = train_test_split(x,y,test_size =0.3)

clf = SVC(gamma='auto',verbose=True)
model = clf.fit(x_train, y_train)
y_pred = model.predict(x_test)
accuracy_score(y_test,y_pred)*100
confusion_matrix(y_test,y_pred)

f1_score(y_test, y_pred, average='macro')

mean_squared_error(y_test, y_pred)


from django.urls import path
from . import views

app_name = "polls"

urlpatterns = [
    path('list/', views.polls_list, name='list'),
    path('list/user/', views.list_by_user, name='list_by_user'),
    path('add/', views.polls_add, name='add'),
    path('edit/<int:poll_id>/', views.polls_edit, name='edit'),
    path('delete/<int:poll_id>/', views.polls_delete, name='delete_poll'),
    path('end/<int:poll_id>/', views.endpoll, name='end_poll'),
    path('edit/<int:poll_id>/choice/add/', views.add_choice, name='add_choice'),
    path('edit/choice/<int:choice_id>/', views.choice_edit, name='choice_edit'),
    path('delete/choice/<int:choice_id>/',
         views.choice_delete, name='choice_delete'),
    path('<int:poll_id>/', views.poll_detail, name='detail'),
    path('<int:poll_id>/vote/', views.poll_vote, name='vote'),
]


import settings
import threading

from common.funcs import S3, QueryHandler
from common.custom_error import InvalidBucketRequest, KeyAlreadyExists
from models.s3_object import S3Object

class ConsoleS3Object(S3):

    def __init__(self, object_type, image_name, content, acl='private'):
        self.bucket_name = self.get_bucket_name(object_type)
        super(ConsoleS3Object, self).__init__(bucket_name = self.bucket_name, name = image_name, content = content, acl = acl)

    def get_bucket_name(self, object_type):
        bucket_name = settings.articles_BUCKETS.get(object_type, None)
        if bucket_name:
            return bucket_name
        else:
            raise InvalidBucketRequest

    def handle_upload(self):
        if self.check_exists():
            raise KeyAlreadyExists
        else:
            threading.Thread(group = None, target = self.upload, name = None, args = ()).start()

    def upload(self):
        self.client.put_object(Bucket = self.bucket_name, Key = self.name, Body=self.content, ACL = self.acl)

def get_group_users(discussion_id):
    query = "SELECT users.name, users.username FROM users, discussions_users WHERE (users.username=discussions_users.username AND discussion_id=%s)";
    variables = (discussion_id, )
    group_users = QueryHandler.get_results(query, variables)
    return group_users


from allennlp.common import Params
import numpy as np
from numpy.testing import assert_allclose
import pytest

from contexteval.common.custom_test_case import CustomTestCase
from contexteval.contextualizers import PrecomputedContextualizer
from contexteval.data.dataset_readers import Conll2003NERDatasetReader

class TestConll2003NERDatasetReader():
    data_path = CustomTestCase.FIXTURES_ROOT / "data" / "ner" / "conll2003.txt"
    contextualizer_path = (CustomTestCase.FIXTURES_ROOT / "contextualizers" /
                           "precomputed_elmo" / "elmo_layers_all.hdf5")

    @pytest.mark.parametrize('lazy', (True, False))
    @pytest.mark.parametrize('use_contextualizer', (True, False))
    @pytest.mark.parametrize('label_encoding', ("IOB1", "BIOUL"))
    def test_read_from_file(self, lazy, use_contextualizer, label_encoding):
        # Set up contextualizer, if using.
        contextualizer = None
        if use_contextualizer:
            contextualizer = PrecomputedContextualizer(self.contextualizer_path)
        reader = Conll2003NERDatasetReader(label_encoding=label_encoding,
                                           contextualizer=contextualizer,
                                           lazy=lazy)
        instances = list(reader.read(str(self.data_path)))
        print(len(instances))
        # First read instance
        instance = instances[0]
        fields = instance.fields
        assert [token.metadata for token in fields["raw_tokens"].field_list] == [
            "EU", "rejects", "German", "call", "to", "boycott", "British", "lamb", "."]
        if label_encoding == "IOB1":
            assert fields["labels"].labels == [
                "I-ORG", "O", "I-MISC", "O", "O", "O", "I-MISC", "O", "O"]
        else:
            assert fields["labels"].labels == [
                "U-ORG", "O", "U-MISC", "O", "O", "O", "U-MISC", "O", "O"]
        if use_contextualizer:
            assert_allclose(
                fields["token_representations"].array[:, :2],
                np.array([[0.9611156, -0.63280857], [-0.5790216, -0.13829914],
                          [-0.35322708, -0.22807068], [-1.6707208, -1.1125797],
                          [-2.0587592, -1.5086308], [-1.3151755, -1.6046834],
                          [0.5072891, -1.5075727], [0.11287686, -1.2473724],
                          [-0.5029946, -1.4319026]]),
                rtol=1e-4)

        # Second read instance
        instance = instances[1]
        fields = instance.fields
        assert [token.metadata for token in fields["raw_tokens"].field_list] == [
            "Peter", "Blackburn"]
        if label_encoding == "IOB1":
            assert fields["labels"].labels == [
                "I-PER", "I-PER"]
        else:
            assert fields["labels"].labels == [
                "B-PER", "L-PER"]
        if use_contextualizer:
            assert_allclose(
                fields["token_representations"].array[:, :2],
                np.array([[-0.5346743, -1.1600235], [-0.7508778, -1.031188]]),
                rtol=1e-4)

        # Third read instance
        instance = instances[2]
        fields = instance.fields
        assert [token.metadata for token in fields["raw_tokens"].field_list] == [
            "BRUSSELS", "1996-08-22"]
        if label_encoding == "IOB1":
            assert fields["labels"].labels == [
                "I-LOC", "O"]
        else:
            assert fields["labels"].labels == [
                "U-LOC", "O"]
        if use_contextualizer:
            assert_allclose(
                fields["token_representations"].array[:, :2],
                np.array([[0.0324477, -0.06925768], [-0.47278678, 0.530316]]),
                rtol=1e-4)

    @pytest.mark.parametrize('max_instances', (1, 2, 0.5, 0.75, 1.0))
    @pytest.mark.parametrize('lazy', (True, False))
    def test_reproducible_with_and_without_contextualization(self, max_instances, lazy):
        uncontextualized_params = Params({
            "max_instances": max_instances,
            "lazy": lazy})
        uncontextualized_reader = Conll2003NERDatasetReader.from_params(uncontextualized_params)
        uncontextualized_instances = list(uncontextualized_reader.read(str(self.data_path)))

        contextualized_params = Params({
            "lazy": lazy,
            "max_instances": max_instances,
            "contextualizer": {
                "type": "precomputed_contextualizer",
                "representations_path": self.contextualizer_path
            }})
        contextualized_reader = Conll2003NERDatasetReader.from_params(contextualized_params)
        contextualized_instances = list(contextualized_reader.read(str(self.data_path)))
        # Assert they are the same
        for uncontextualized_instance, contextualized_instance in zip(uncontextualized_instances,
                                                                      contextualized_instances):
            assert ([token.metadata for token in uncontextualized_instance.fields["raw_tokens"].field_list] ==
                    [token.metadata for token in contextualized_instance.fields["raw_tokens"].field_list])
            assert (uncontextualized_instance.fields["labels"].labels ==
                    contextualized_instance.fields["labels"].labels)
            contextualized_extra_keys = list(set(contextualized_instance.fields.keys()) -
                                             set(uncontextualized_instance.fields.keys()))
            assert (set(contextualized_extra_keys) == set(["token_representations"]))

    @pytest.mark.parametrize('lazy', (True, False))
    @pytest.mark.parametrize('use_contextualizer', (True, False))
    @pytest.mark.parametrize('max_instances', (1, 2, 0.5, 0.75, 1.0))
    def test_truncation(self, lazy, use_contextualizer, max_instances):
        # Set up contextualizer, if using.
        contextualizer = None
        if use_contextualizer:
            contextualizer = PrecomputedContextualizer(self.contextualizer_path)
        reader = Conll2003NERDatasetReader(
            contextualizer=contextualizer,
            max_instances=max_instances,
            lazy=lazy)
        instances = list(reader.read(str(self.data_path)))
        num_total_instances = 3
        max_instances_to_num_instances = {
            int(1): 1,
            int(2): 2,
            0.5: int(num_total_instances * 0.5),
            0.75: int(num_total_instances * 0.75),
            1.0: num_total_instances}
        assert len(instances) == max_instances_to_num_instances[max_instances]


# Funciones en Pyhon
# Dividir y organizar el código en partes más sencillas.
# Encapsular el código que se repite para ser reutilizado

# Función que muestra por pantalla el resultado de multiplicar un número por 5.
def product_by_5(number):
    print(f'{number} * 5 = {number * 5}')

print('Start the program')
product_by_5(11)
print('Next')
product_by_5(42)
print('End')

# Sentencia return
# return que no devuelve ningún valor
def cuadrado_num_par(number):
    if not number % 2 == 0:
        return
    else:
        print(number ** 2)

cuadrado_num_par(8)
cuadrado_num_par(7)

# Varios return en una misma función
def is_even(number):
    if number % 2 == 0:
        return True
    else:
        return False

is_even(12)
is_even(31)

# Devolver más de un valor con return en Python.
def cuadrado_cubo(number):
    return number ** 2, number ** 3

sqrt, cube = cuadrado_cubo(3)
sqrt
cube

# Se puede devolver los diferentes resultados en una lista.
def multiples_of(number):
    result = []
    for i in range(11):
        result.append(number * i)
    return result

res = multiples_of(11)
res


"""
Funciones útiles generales.
"""
import numpy as np

def number_formatter(number, pos=None):
    """
    Convert a number into a human readable format.
    :param number:
    :param pos:
    :return: string converted
    """
    magnitude = 0
    while abs(number) >= 1000:
        magnitude += 1
        number /= 1000.0
    return '%.1f%s' % (number, ['', 'K', 'M', 'B', 'T', 'Q'][magnitude])

def numeric_profiling(df_o, col):
    """

    :param df_o:
    :param col: column to analize
    :return: dictionary
    """
    profiling = {}

    # eliminate missing values
    df = df_o.copy()
    df = df[df[col].notna()]
    df[col] = df[col].astype(float)

    profiling.update({'max': df[col].max(),
                     'min': df[col].min(),
                     'mean': df[col].mean(),
                     'stdv': df[col].std(),
                     '25%': df[col].quantile(.25),
                     'median': df[col].median(),
                     '75%': df[col].quantile(.75),
                     'kurtosis': df[col].kurt(),
                     'skewness': df[col].skew(),
                     'uniques': df[col].nunique()}
                     # 'prop_missings': df[col].isna().sum()/df.shape[0]*100,
                     # 'top1_repeated': get_repeated_values(df, col, 1),
                     # 'top2_repeated': get_repeated_values(df, col, 2),
                     # 'top3_repeated': get_repeated_values(df, col, 3)
                     )

    return profiling

def generate_label(incidentes_viales_df):
    """
    Cambiar el codigo de cierre a
    :param incidentes_viales_df: dataframe
    :return:
    """
    incidentes_viales_df.codigo_cierre.mask(incidentes_viales_df.codigo_cierre ==
                                            r"(A) La unidad de atención a emergencias fue despachada, "
                                            "llegó al lugar de los hechos y confirmó la emergencia reportada",
                                            'A', inplace=True)
    incidentes_viales_df.codigo_cierre.mask(incidentes_viales_df.codigo_cierre ==
                                            r'(N) La unidad de atención a emergencias fue despachada, '
                                            'llegó al lugar de los hechos, pero en el sitio del evento '
                                            'nadie solicitó el apoyo de la unidad',
                                            'N', inplace=True)
    incidentes_viales_df.codigo_cierre.mask(incidentes_viales_df.codigo_cierre ==
                                            r'(D) El incidente reportado se registró en dos o más '
                                            'ocasiones procediendo a mantener un único reporte (afirmativo,'
                                            ' informativo, negativo o falso) como el identificador para el '
                                            'incidente',
                                            'D', inplace=True)
    incidentes_viales_df.codigo_cierre.mask(incidentes_viales_df.codigo_cierre ==
                                            r'(F) El operador/a o despachador/a identifican, antes de dar '
                                            'respuesta a la emergencia, que ésta es falsa. O al ser '
                                            'despachada una unidad de atención a emergencias en el lugar '
                                            'de los hechos se percatan que el incidente no corresponde al '
                                            'reportado inicialmente',
                                            'F', inplace=True)
    incidentes_viales_df.codigo_cierre.mask(incidentes_viales_df.codigo_cierre ==
                                            r'(I) El incidente reportado es afirmativo y se añade '
                                            'información adicional al evento',
                                            'I', inplace=True)

    incidentes_viales_df['label'] = np.where(
        (incidentes_viales_df.codigo_cierre == 'F') | (incidentes_viales_df.codigo_cierre == 'N'), 1, 0)

    return incidentes_viales_df


# -*- coding: utf-8 -*-
import scrapy
from quotesbot.items import BaiduItem
import urllib

split_sign = '##'  # 定义分隔符
class ToScrapeSpiderXPath(scrapy.Spider):
    name = 'allperson-xpath'
    start_urls = []
    file_object = open('data/txt/painter.txt', 'r', encoding="utf-8").read()
    wordList = file_object.split("\n")  # 获取词表
    for i in wordList:  ##生成url列表
        cur = "https://baike.baidu.com/item/"
        cur = cur + str(i)
        start_urls.append(cur)

    def parse(self, response):
        print("enter parse")
        # div限定范围
        main_div = response.xpath('//div[@class="content-wrapper"]')

        url = response.url  # url直接得到
        url = urllib.parse.unquote(url)

        title = "error"
        if url.find('error') != -1:
            print("百科中找不到对应页面！")
            title = "error"
        else:
            title = response.url.split('item/')[-1]  # 通过截取url获取title
            title = urllib.parse.unquote(title)

            if title.find('fromtitle') != -1:
                print("=====fromtitle========", title)
                title = title.split('fromtitle=')[-1].split('&')[0]
            if title.find("/")!=-1:
                title = title.split('/')[0]

        img = ""  # 爬取图片url
        for p in main_div.xpath(
                './/div[@class="content"]/div[@class="side-content"]/div[@class="summary-pic"]/a/img/@src'):
            img = p.extract().strip()

        detail = ""  # 详细信息
        for detail_xpath in main_div.xpath('.//div[@class="lemma-summary"]/div[@class="para"]'):
            detail += detail_xpath.xpath('string(.)').extract()[0].strip()

        flag = 0
        baseInfoKeyList = ""  # 基本信息的key值
        for dl in main_div.xpath('.//div[@class="basic-info cmn-clearfix"]/dl'):
            for p in dl.xpath('dt'):
                if flag == 1:
                    baseInfoKeyList += split_sign
                print(p.xpath('string(.)').extract()[0])
                baseInfoKeyList += p.xpath('string(.)').extract()[0].replace(" ","").strip()
                flag = 1
        print("baseInfoKeyList", baseInfoKeyList)
        ## 继续调xpath！！！！！！！！！！！！！
        flag = 0
        baseInfoValueList = ""  # 基本信息的value值
        for dl in main_div.xpath('.//div[@class="basic-info cmn-clearfix"]/dl'):
            for p in dl.xpath('dd'):
                print("baseInfoValueList enter ")
                if flag == 1:
                    baseInfoValueList += split_sign
                all_text = p.xpath('string(.)').extract()[0].strip()
                baseInfoValueList += all_text
                flag = 1
        print("baseInfoValueList", baseInfoValueList)
        item = BaiduItem()
        item['title'] = title
        item['url'] = url
        item['image'] = img
        item['detail'] = detail
        item['baseInfoKeyList'] = baseInfoKeyList
        item['baseInfoValueList'] = baseInfoValueList

        yield item


#!/usr/bin/python3
"""
Importing Base for Rectangle class
"""

from .base import Base
"""
import base
"""

class Rectangle(Base):
    """
    Defines a Rectangle
    """
    def __init__(self, width, height, x=0, y=0, id=None):
        """
        Call super class constructor
        """
        self.width = width
        self.height = height
        self.x = x
        self.y = y
        super().__init__(id)

    @property
    def width(self):
        """
        getter for width
        """
        return self.__width

    @width.setter
    def width(self, value):
        """
        setter for width
        """
        if not isinstance(value, int):
            raise TypeError("width must be an integer")
        if value <= 0:
            raise ValueError("width must be > 0")
        self.__width = value

    @property
    def height(self):
        """
        getter for  height
        """
        return self.__height

    @height.setter
    def height(self, value):
        """
        setter for height
        """
        if not isinstance(value, int):
            raise TypeError("height must be an integer")
        if value <= 0:
            raise ValueError("height must be > 0")
        self.__height = value

    @property
    def x(self):
        """
        getter for x
        """
        return self.__x

    @x.setter
    def x(self, value):
        """
        setter for x
        """
        if not isinstance(value, int):
            raise TypeError("x must be an integer")
        if value < 0:
            raise ValueError("x must be >= 0")
        self.__x = value

    @property
    def y(self):
        """
        getter for y
        """
        return self.__y

    @y.setter
    def y(self, value):
        """
        setter for y
        """
        if not isinstance(value, int):
            raise TypeError("y must be an integer")
        if value < 0:
            raise ValueError("y must be >= 0")
        self.__y = value

    def area(self):
        """
        Returns current square area
        """
        if self.__width == 0 or self.__height == 0:
            return 0
        else:
            return self.__width * self.__height

    def display(self):
        """
        displays ## rectangle
        """
        if self.__width == 0 or self.__height == 0:
            return
        for _ in range(self.y):
            print()
        for _ in range(self.__height):
            print(' ' * self.x + '#' * self.__width)

    def update(self, *args, **kwargs):
        """
        update to arguments
        """
        if args:
            if len(args) >= 5:
                self.id = args[0]
                self.width = args[1]
                self.height = args[2]
                self.x = args[3]
                self.y = args[4]
            else:
                if len(args) > 0:
                    self.id = args[0]
                if len(args) > 1:
                    self.width = args[1]
                if len(args) > 2:
                    self.height = args[2]
                if len(args) > 3:
                    self.x = args[3]
                if len(args) > 4:
                    self.y = args[4]
        for key, value in kwargs.items():
            if hasattr(self, key):
                setattr(self, key, value)

    def to_dictionary(self):
        """
        defining a dictionary within rectangle
        """
        return {'id': self.id,
                'width': self.__width,
                'height': self.__height,
                'x': self.__x,
                'y': self.__y}

    def __str__(self):
        """
        Returns a string representation of the rectangle.
        """
        return (f"[Rectangle] ({self.id}) {self.__x}/{self.__y}"
                f" - {self.__width}/{self.__height}")


import json
import boto3
import logging
import os

from chalice import Chalice

from chalicelib.action import Action
from chalicelib.lib.helpers import parse_config
from chalicelib.lib.reminder import remind_users
from chalicelib.lib.slack import (
    Slack,
    slack_payload_extractor,
    slack_responder,
    verify_token,
)
from chalicelib.lib.sqs import send_message

app = Chalice(app_name="timereport")
app.debug = True

logger = logging.getLogger()

dir_path = os.path.dirname(os.path.realpath(__file__))
config = parse_config(f"{dir_path}/chalicelib/config.yaml")
config["backend_url"] = os.getenv("backend_url")
config["bot_access_token"] = os.getenv("bot_access_token")
config["signing_secret"] = os.getenv("signing_secret")
config["enable_lock_reminder"] = os.getenv("enable_lock_reminder")
config["format_str"] = "%Y-%m-%d"
config["command_queue"] = f"timereport-slack-command-{os.getenv('environment')}"
config["interactive_queue"] = f"timereport-slack-interactive-{os.getenv('environment')}"
config["enable_queue"] = os.getenv("enable_queue")

logger.setLevel(config["log_level"])

def dummy():
    """
    The sole purpose is to force Chalice to generate the right permissions in the policy.
    Does nothing and returns nothing.
    """
    sqs = boto3.client("sqs")
    sqs.send_message()
    sqs.get_queue_url()

@app.route(
    "/interactive",
    methods=["POST"],
    content_types=["application/x-www-form-urlencoded"],
)
def interactive():
    if False:
        dummy()

    def _handle_message(payload):
        send_message(
            config["enable_queue"],
            config["interactive_queue"],
            payload,
            interactive_handler,
        )

    return handle_slack_request(_handle_message)

@app.route(
    "/command", methods=["POST"], content_types=["application/x-www-form-urlencoded"]
)
def command():
    def _handle_message(payload):
        action_instance = Action.create(payload, config)
        if action_instance.is_valid():
            send_message(
                config["enable_queue"],
                config["command_queue"],
                payload,
                command_handler,
            )

    return handle_slack_request(_handle_message)

def handle_slack_request(action):
    payload = None
    try:
        req = app.current_request.raw_body.decode()
        req_headers = app.current_request.headers
        if not verify_token(req_headers, req, config["signing_secret"]):
            return "Slack signing secret not valid"

        payload = slack_payload_extractor(req)

        logger.info(f"Slack extracted payload for command: {payload}")

        action(payload)
    except Exception:
        logger.critical("Caught unhandled exception.", exc_info=True)

        if isinstance(payload, dict) and "response_url" in payload:
            slack_responder(
                payload["response_url"], "Failed to handle request, try again!"
            )

    return ""

@app.on_sqs_message(queue=config["command_queue"])
def command_handler(event):
    for record in event:
        action = Action.create(json.loads(record.body), config)
        action.perform_action()

@app.on_sqs_message(queue=config["interactive_queue"])
def interactive_handler(event):
    for record in event:
        action = Action.create(json.loads(record.body), config)
        action.perform_interactive()

@app.schedule("rate(1 day)")
def check_user_locks(event):
    if config["enable_lock_reminder"]:
        remind_users(
            slack=Slack(slack_token=config["bot_access_token"]),
            backend_url=config["backend_url"],
        )


from django.shortcuts import render
from .models import Seller
from products.choices import price_choices, seller_choices

def search(request):
    queryset_list1 = Seller.objects.all()

    if 'seller' in request.GET:
        name = request.GET['seller']
        if name:
            queryset_list1 = queryset_list1.filter(name__iexact=name)

    context = {
         'price_choices':price_choices,
        'seller_choices':seller_choices,
        'seller': queryset_list1
    }

    return render(request,'products/search.html', context)


#!/usr/bin/python
import sqlite3
import datetime

# purges all entries older than a month

def main():
    conn = sqlite3.connect('../db/summaries.db')
    c = conn.cursor()
    oldest = datetime.datetime.now() + datetime.timedelta(-30) # 30 days back
    c.execute('DELETE * FROM summaries WHERE created_at < ' + oldest)
    conn.commit()
    conn.close()

if __name__=='__main__':
    main()


from ..Framework.LayersHelper import Layers

import tensorflow as tf

def rcab(layers: Layers, inputs,
         filters=64, ratio=16, scope=None, reuse=None):
    """Residual channel attention block (ECCV18)

    """
    with tf.variable_scope(scope, 'RCAB', reuse=reuse):
        pre_input = inputs
        x = layers.relu_conv2d(inputs, filters, 3)
        y = layers.conv2d(x, filters, 3)
        x = tf.reduce_mean(y, axis=[1, 2], keepdims=True)
        x = layers.relu_conv2d(x, filters // ratio, 1)
        x = layers.conv2d(x, filters, 1, activation=tf.nn.sigmoid)
        y *= x
        y += pre_input
    return y

def msrb(layers: Layers, inputs, filters=64, scope=None, reuse=None):
    """Multi-scale residual block (ECCV18)

    """
    with tf.variable_scope(scope, 'MSRB', reuse=reuse):
        pre_input = inputs
        s1 = layers.relu_conv2d(inputs, filters, 3)
        p1 = layers.relu_conv2d(inputs, filters, 5)

        s2 = layers.relu_conv2d(tf.concat([s1, p1], -1), filters * 2, 3)
        p2 = layers.relu_conv2d(tf.concat([p1, s1], -1), filters * 2, 5)

        s = layers.conv2d(tf.concat([s2, p2], -1), filters, 1)
        s += pre_input
    return s

def cascade_block(layers: Layers, inputs,
                  filters=64, depth=4, scope=None, reuse=None):
    """Cascading residual block (ECCV18)

    """
    with tf.variable_scope(scope, 'CARB', reuse=reuse):
        feat = [inputs]
        for i in range(depth):
            x = layers.resblock(inputs, filters, 3, activation='relu')
            feat.append(x)
            inputs = layers.conv2d(
                tf.concat(feat, axis=-1), filters, 1,
                kernel_initializer='he_uniform')
        inputs = layers.conv2d(inputs, filters, 3)
        return inputs

def non_local(layers: Layers, inputs, filters=64, func=None, scaling=1,
              pooling=None, use_bn=None, scope=None, reuse=None):
    """Non-local neural networks (CVPR 18)

    Note:
        y_i = \frac{1}{C(x)}\Sigma_{j}(f(x_i, x_j)g(x_j))
        A pairwise function f computes a scalar between i and all j. The unary
        function g computes a representation of the input signal at the position
        j. The response is normalized by a factor C(x).

    Args:
        layers: parent object.
        inputs: input tensor.
        filters: output filter numbers.
        func: a string in ('gaussian', 'embedded', 'dot', 'concat'),
          representing the pairwise function f. Default 'embedded'.
        scaling: scaling channel numbers down.
        pooling: sub-sampling x by max-pooling, `pooling` represents strides.
        use_bn: if True, batch-normalizing last embedding output.
        scope: variable scope for this block.
        reuse: reuse flag.
    """

    def embedding(x: tf.Tensor, c, scale):
        if len(x.shape) == 4:
            return layers.conv2d(x, c // scale, 1)
        elif len(x.shape) == 5:
            return layers.conv3d(x, c // scale, 1)
        else:
            raise ValueError('input tensor dimensions must be 4 or 5.')

    def flatten(x: tf.Tensor):
        c = x.shape[-1]
        b = tf.shape(x)[0]
        return tf.reshape(x, [b, -1, c])

    def reduce(x: tf.Tensor):
        if len(x.shape) == 4:
            if pooling is not None:
                return tf.layers.max_pooling2d(x, pooling, pooling)
            else:
                return x
        elif len(x.shape) == 5:
            if pooling is not None:
                return tf.layers.max_pooling3d(x,
                                               [1, pooling, pooling],
                                               [1, pooling, pooling])
            else:
                return x
        else:
            raise ValueError('input tensor dimensions must be 4 or 5.')

    def gaussian(x: tf.Tensor):
        x0 = flatten(x)
        x1 = flatten(reduce(x))
        return tf.nn.softmax(tf.matmul(x0, x1, transpose_b=True), -1)

    def embedded(x: tf.Tensor):
        theta = flatten(embedding(x, filters, scaling))
        phi = flatten(embedding(reduce(x), filters, scaling))
        return tf.nn.softmax(tf.matmul(theta, phi, transpose_b=True), -1)

    def dot(x: tf.Tensor):
        theta = flatten(embedding(x, filters, scaling))
        phi = flatten(embedding(reduce(x), filters, scaling))
        n = tf.shape(phi)[1]
        return tf.truediv(tf.matmul(theta, phi, transpose_b=True), n)

    def concat(x: tf.Tensor):
        raise NotImplementedError

    with tf.variable_scope(scope, 'NonLocal', reuse=reuse):
        channels = inputs.get_shape().as_list()[-1]
        shape = tf.shape(inputs)
        g = embedding(inputs, filters, scaling)
        g = reduce(g)
        if func == 'gaussian':
            f = gaussian
        elif func == 'dot':
            f = dot
        elif func == 'concat':
            f = concat
        else:
            f = embedded
        corr = f(inputs)
        y = tf.matmul(corr, g)
        y = tf.reshape(y, [shape[0], shape[1], shape[2], -1])
        y = embedding(y, channels, 1)
        if use_bn:
            y = layers.batch_norm(y, layers.training_phase)
        return inputs + y


# Every color has a meaning and it symbolise certain things. So a short program representing the meaning of certain color.

d = {"Black" : "Elegance",
     "Green" : "Environment",
     "Blue" : "Loyalty",
     "White" : "Freshness"}

print("Choose a colour from :-\nBlack, Green, Blue, White-")
ee = input()
print("The colour represents",d[ee])


from __future__ import absolute_import, unicode_literals

from django.db import models

from modelcluster.fields import ParentalKey

from wagtail.wagtailcore.fields import StreamField, RichTextField
from wagtail.wagtailcore.models import Page
from wagtail.wagtailcore import blocks
from wagtail.wagtailadmin.edit_handlers import (
    FieldPanel, InlinePanel, MultiFieldPanel, StreamFieldPanel)
from wagtail.wagtailforms.models import AbstractEmailForm, AbstractFormField
from wagtail.wagtailimages.edit_handlers import ImageChooserPanel

from .blocks import CallToActionBlock, ImageTextBlock, PullQuoteBlock, SectionBlock

class StandardPage(Page):
    feature_image = models.ForeignKey(
        'wagtailimages.Image',
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name='+'
    )
    feature_image_large = models.BooleanField(default=False)
    body = StreamField([
        ('heading', blocks.CharBlock(
            classname='full title', template='blocks/heading.html')),
        ('section', SectionBlock()),
        ('image', ImageTextBlock(template='blocks/image_text.html')),
        ('quote', PullQuoteBlock(template='blocks/quote.html')),
        ('call_to_action', CallToActionBlock()),
    ])

    content_panels = Page.content_panels + [
        ImageChooserPanel('feature_image'),
        FieldPanel('feature_image_large'),
        StreamFieldPanel('body'),
    ]

class FormField(AbstractFormField):
    page = ParentalKey('FormPage', related_name='form_fields')

class FormPage(AbstractEmailForm):
    intro = RichTextField(blank=True)
    thank_you_text = RichTextField(blank=True)

    content_panels = AbstractEmailForm.content_panels + [
        FieldPanel('intro', classname='full'),
        InlinePanel('form_fields', label='Form fields'),
        FieldPanel('thank_you_text', classname='full'),
        MultiFieldPanel([
            FieldPanel('to_address', classname='full'),
            FieldPanel('from_address', classname='full'),
            FieldPanel('subject', classname='full'),
        ], 'Email')
    ]


from backend import app, logger
from flask import Blueprint
from flask_restplus import Api

from stack import api as api_stack
from auth import api as api_auth
from user import api as api_user
from trends import api as api_trends
from public import api as api_public

blueprint = Blueprint('api', __name__, url_prefix='/api')

authorizations = {
    'oauth2': {
        'type': 'oauth2',
        'in': 'header',
        'name': 'Authorization'
    }
}

api = Api(
    blueprint,
    version='1.0',
    title='Stack API',
    description='Access stack data.',
    authorizations=authorizations)

app.register_blueprint(blueprint)
api.add_namespace(api_auth)
logger.info('Auth API was registered.')
api.add_namespace(api_user)
logger.info('User API was registered.')
api.add_namespace(api_public)
logger.info('Public API was registered.')
api.add_namespace(api_stack)
logger.info('Stack API was registered.')
api.add_namespace(api_trends)
logger.info('Trends API was registered.')


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

import matplotlib.pyplot as plt
import matplotlib.cm as cm
import numpy as np
import scipy.signal as dsp
import util
from PIL import Image
#
# cargamos datos de MNIST
#
#
# CODEBOOK
#
# el codebook contiene n variantes
# de cada uno de los dígitos del 0 al 9
#
# cargamos el codebook de 0 para adivinar las dimensiones

aux = np.loadtxt('digits/256/digit_0.txt')
n,m = aux.shape
w = int(np.sqrt(m))
#
# el codebook para cada digito esta almacenado en un archivo
# llamado digit_d.txt donde d es 0,1,2...,9
#
codebook = np.empty((10,n,m))
for d in range(10):
    codebook[d] = np.loadtxt(f'digits/256/digit_{d}.txt')
#
# simbolos del codebook pasan de vectores d elargo m a
# imagenes de wxw
#
codebook = np.reshape(codebook,(10,n,w,w))
#
# imagen a procesar
#
im = Image.open('test_quart.jpg')
N, M = im.size
#
# si esta apaisada, rotar
#
if N > M:
    im = im.rotate(-90,expand=1)
    aux = M
    M = N
    N = aux
#
# intentamos adivinar si la imagen fue reescalada
# a grosso modo comparando la dimension mas grande
# con la dimensión típica de las imágenes originales
#
# 4 es la escala original, porque en la escala original
# las imagenes del codebook estan agrandadas x4
#
approx_scale = np.round(4*M/4900,1)
print('approx scale=',approx_scale)
#
# asumimos que alguien podría recortar los márgenes de
# la imagen, pero que nadie le agregaría margen
# entonces podemos concentrarnos en la zona
# en donde pusimos la marca en la imagen original
#
sw = approx_scale
x0 = N - sw*(50 + 8*w)
x1 = N
y0 = M - sw*(50 + w)
y1 = M

print(x0,y0,x1,y1)
im = im.crop((x0,y0,x1,y1))
im = im.convert('L')

I = np.asarray(im)*(1.0/255.0)
M,N = I.shape

I = 1- I
print('<I>=',np.mean(I))
#
# busqueda exhaustiva del primer 0
#
# los ceros siempre siguen una misma secuencia

best_scale = approx_scale
best_offset = 0
best_score = 0
best_char = '?'
best_imc = []
best_G = []

offset1  = ord('o') - ord('h')
offset2  = ord('l') - ord('h')
cmin = ord('0')
cmax = ord('Z')
CS   = cmax - cmin
best_i = -1
best_j = -1

print('Num. simbolos disponibles:',CS)
for c in range(CS):
    # los primeros tres ceros corresponden
    # partir de un cierto offset al azar
    # (en esta prueba no es al azar, sino 'h'
    # el segundo es 'o' y el tercero 'l')
    #
    c0 = np.mod(c,CS)
    c1 = np.mod(c+offset1,CS)
    c2 = np.mod(c+offset2,CS)

    #im0 = np.reshape(codebook[0,c0,:],(w,w))
    #im1 = np.reshape(codebook[0,c1,:],(w,w))
    #im2 = np.reshape(codebook[0,c2,:],(w,w))
    im0 = codebook[0,c0,:,:]
    im1 = codebook[0,c1,:,:]
    im2 = codebook[0,c2,:,:]

    imc = np.concatenate((im0,im1,im2),axis=1)

    imc = np.kron(imc,np.ones((int(approx_scale),int(approx_scale))))
    imc = np.fliplr(np.flipud(imc))
    imc = imc - np.mean(imc)
    imc = imc*(1.0/np.sqrt(np.sum(imc**2)))
    G = dsp.fftconvolve(I,imc,mode='same')
    limax = np.argmax(G)
    imax = int(limax/N)
    jmax = np.mod(limax,N)
    score = G[imax,jmax]
    offchar = chr(c+ord('0'))
    score = int(np.round(score))
    print(f'offset ord {c} char {offchar} {score} row {imax} col {jmax} score {score} best score {best_score} off {best_offset} chr {best_char}')

    if score > best_score:
        best_score = score
        best_offset = c
        best_char = offchar
        best_i = imax
        best_j = jmax
        dh,dw = imc.shape
        G[imax,jmax] = 10*score
        G[imax:(imax+dh),jmax:(jmax+dw)] += imc
        best_imc = np.copy(imc)
        best_G = np.copy(G)

print(f'offset ord {c} char {offchar} {score} row {imax} col {jmax} score {score} best score {best_score} off {best_offset} chr {best_char}')
plt.figure(1)
plt.imshow(I)
plt.figure(2)
best_G[best_i,best_j] = 2*np.max(best_G)
plt.imshow(best_G)
plt.figure(3)
plt.imshow(best_imc)
plt.show()

# for char0 in range(CS):
#     im0 = np.reshape(codebook[0,char0,:],(w,w))
#     for char1 in range(CS):
#         im1 = np.reshape(codebook[0,char1,:],(w,w))
#         for char2 in range(CS):
#             im2 = np.reshape(codebook[0,char2,:],(w,w))
#             imc = np.concatenate((im0,im1,im2),axis=1)
#             print(imc.shape)
#             W    = w*escala
#             i0   = M - 200 - W
#             j0   = N - 200 - 8*W
#             plt.figure()
#             plt.imshow(imc)
#             plt.show()
#             #imm = im.crop(())


import json

VALID_STATUS = ('CREATED', 'BUILDING', 'FETCHING', 'NOT FOUND')

def deserialize_msg(msg: dict):
    '''
    parses a dict msg from a redis stream
    msg: the message from a redis stream in the format
    dict[bytes, bytes(serialized json)]
    '''
    def decode_value(val):
        try:
            return json.loads(val)
        except json.decoder.JSONDecodeError:
            return val.decode('utf-8')
    return {
        key.decode('utf-8'): decode_value(value)
        for key, value in msg.items()
    }

def parse_xreadgroup(stream_response: list):
    '''
    receives redis read response with awful format
    (list of list of list of bytes) to a dict with
    beautiful format.

    format = { 'stream_name' : [ { 'msg_id' : dict_msg } ] }

    stream_response: redis xreadgroup response
    '''
    return {
        stream_obj[0].decode('utf-8'):
        [
            {
                'id':id.decode('utf-8'),
                'msg': deserialize_msg(msg)
            }
            for id,msg in stream_obj[1]
        ]
        for stream_obj in stream_response
    }

def msg_count(stream_response: list):
    i = 0
    for stream in stream_response:
        i += len(stream[1])


#Autor: Alejandro Rosas Cuesta
#Codigo: 58231
#Descripción: El programa da solución a la petición de un código que determine
# la cantidad de veces que se repiten las letras de una lista.

#Definicion de funcion para tomar los elementos de la lista y dejarlos sin repetir
def numeroDeElementosUnicos(lista):

    unico=[]
    for x in lista:
        if x not in unico:
            unico.append(x)
    return unico
#Definición de funcion para contar los elementos de la lista
def contarElementos(unico, lista):
    numeros=[]
    tamano=len(unico)
    for i in range(0, tamano):
        numeros.append(lista.count(unico[i]))
    return numeros

lista=["A","a","m","o","n","Z","z","A","p","q","n","u"]
unica=numeroDeElementosUnicos(lista)
repeticiones=contarElementos(unica, lista)
print("Lista Completa: ", lista)
print("Lista sin repetir elementos: ",unica)
print("Cantidad de repeticiones por elemento: ",repeticiones)


import streamlit as st
from database import add_data

def create():
    col1, col2 = st.columns(2)
    with col1:
        train_no = st.text_input("Train no:")
        train_name = st.text_input("Name:")
        train_type = st.text_input("Train Type:")
    with col2:
        train_src = st.selectbox("Src", ["Bangalore", "Chennai", "Mangaluru"])
        train_dst = st.selectbox("Dst", ["Bangalore", "Chennai", "Mangaluru"])
        availability = st.selectbox("Availability", ["Yes", "No"])
    if st.button("Add Train"):
        add_data(train_no, train_name, train_type, train_src, train_dst, availability)
        st.success("Successfully added Train: {}".format(train_name))


from ArrayStack import ArrayStack
from ArrayQueue import ArrayQueue
import copy

def permutations(lst):
    s = ArrayStack()
    q = ArrayQueue()
    for i in lst:
        s.push(i)
    q.enqueue([s.pop()])

    while(not s.is_empty()):
        val = s.pop()
        for i in range(len(q)):
            temp = q.dequeue()

            for i in range(len(temp)+1):
                temp2 = copy.deepcopy(temp)
                temp2.insert(i,val)
                q.enqueue(temp2)
    lst = []
    for i in range(len(q)):
        lst.append(q.dequeue())
    return lst


import sys
import itertools

ADD  = 1
MUL  = 2
IN   = 3
OUT  = 4
JMPT = 5
JMPF = 6
LESS = 7
EQU  = 8
HALT = 99
POS = 0
IMM = 1

class amp(object):
    def __init__(self, code, input):
        self.code = code.copy()
        self.ip = 0
        self.input = input.copy()
        self.terminated = False

    def _get_param(self, p, mode):
        if mode == IMM:
            return p
        return self.code[p]

    def add_input(self, x):
        self.input.append(x)

    def has_input(self):
        if self.terminated:
            return False
        if len(self.input) > 0:
            return True
        return False

    def run(self):
        last_output = 0
        mode = [0,0,0]
        while True:
            opcode = self.code[self.ip]
            mode[2], opcode = divmod(opcode,10000)
            mode[1], opcode = divmod(opcode,1000)
            mode[0], opcode = divmod(opcode,100)
            if opcode == HALT:
                self.terminated = True
                break
            if opcode == ADD:
                p1 = self._get_param(self.code[self.ip+1], mode[0])
                p2 = self._get_param(self.code[self.ip+2], mode[1])
                self.code[self.code[self.ip+3]] = p1 + p2
                self.ip += 4
            elif opcode == MUL:
                p1 = self._get_param(self.code[self.ip+1], mode[0])
                p2 = self._get_param(self.code[self.ip+2], mode[1])
                self.code[self.code[self.ip+3]] = p1 * p2
                self.ip += 4
            elif opcode == IN:
                if len(self.input) == 0:
                    return None
                self.code[self.code[self.ip+1]] = self.input.pop(0)
                self.ip += 2
            elif opcode == OUT:
                last_output = self._get_param(self.code[self.ip+1], mode[0])
                self.ip += 2
                return last_output
            elif opcode == JMPT:
                p1 = self._get_param(self.code[self.ip+1], mode[0])
                p2 = self._get_param(self.code[self.ip+2], mode[1])
                if p1 == 0:
                    self.ip += 3
                else:
                    self.ip = p2
            elif opcode == JMPF:
                p1 = self._get_param(self.code[self.ip+1], mode[0])
                p2 = self._get_param(self.code[self.ip+2], mode[1])
                if p1 != 0:
                    self.ip += 3
                else:
                    self.ip = p2
            elif opcode == LESS:
                p1 = self._get_param(self.code[self.ip+1], mode[0])
                p2 = self._get_param(self.code[self.ip+2], mode[1])
                if p1 < p2:
                    val = 1
                else:
                    val = 0
                self.code[self.code[self.ip+3]] = val
                self.ip += 4
            elif opcode == EQU:
                p1 = self._get_param(self.code[self.ip+1], mode[0])
                p2 = self._get_param(self.code[self.ip+2], mode[1])
                if p1 == p2:
                    val = 1
                else:
                    val = 0
                self.code[self.code[self.ip+3]] = val
                self.ip += 4
            else:
                print("Unknown opself.code %d at %d" % (opcode, self.ip))
                sys.exit()
        return None

with open('input') as f:
    code = f.read()

gold = code.split(',')

for i in range(len(gold)):
    gold[i] = int(gold[i])

part1 = -999999999999

perms = list(itertools.permutations([0, 1, 2, 3, 4]))
for p in perms:
    amp1 = amp(gold.copy(), [p[0],0])
    amp2 = amp(gold.copy(), [p[1]])
    amp3 = amp(gold.copy(), [p[2]])
    amp4 = amp(gold.copy(), [p[3]])
    amp5 = amp(gold.copy(), [p[4]])
    while True:
        if amp1.has_input():
            op = amp1.run()
            if op != None:
                amp2.add_input(op)
        elif amp2.has_input():
            op = amp2.run()
            if op != None:
                amp3.add_input(op)
        elif amp3.has_input():
            op = amp3.run()
            if op != None:
                amp4.add_input(op)
        elif amp4.has_input():
            op = amp4.run()
            if op != None:
                amp5.add_input(op)
        elif amp5.has_input():
            op = amp5.run()
            if op != None:
                part1 = max(part1, op)
        else:
            break

print('Part 1:', part1)

part2 = -999999999999

perms = list(itertools.permutations([5, 6, 7, 8, 9]))
for p in perms:
    amp1 = amp(gold.copy(), [p[0],0])
    amp2 = amp(gold.copy(), [p[1]])
    amp3 = amp(gold.copy(), [p[2]])
    amp4 = amp(gold.copy(), [p[3]])
    amp5 = amp(gold.copy(), [p[4]])
    last_out = 0
    while True:
        if amp1.has_input():
            op = amp1.run()
            if op != None:
                amp2.add_input(op)
        elif amp2.has_input():
            op = amp2.run()
            if op != None:
                amp3.add_input(op)
        elif amp3.has_input():
            op = amp3.run()
            if op != None:
                amp4.add_input(op)
        elif amp4.has_input():
            op = amp4.run()
            if op != None:
                amp5.add_input(op)
        elif amp5.has_input():
            op = amp5.run()
            if op != None:
                amp1.add_input(op)
                last_out = op
        else:
            break
    part2 = max(part2, last_out)

print('Part 2:', part2)


# ISBN-10 Validation
#
# https://www.codewars.com/kata/51fc12de24a9d8cb0e000001
#
# ISBN-10 identifiers are ten digits long. The first nine characters are digits 0-9. The last digit can be 0-9 or X, to indicate a value of 10.
#
# An ISBN-10 number is valid if the sum of the digits multiplied by their position modulo 11 equals zero.
#
# For example:
#
# ISBN     : 1 1 1 2 2 2 3 3 3  9
# position : 1 2 3 4 5 6 7 8 9 10
# This is a valid ISBN, because:
#
# (1*1 + 1*2 + 1*3 + 2*4 + 2*5 + 2*6 + 3*7 + 3*8 + 3*9 + 9*10) % 11 = 0
# Examples
# 1112223339   -->  true
# 111222333    -->  false
# 1112223339X  -->  false
# 1234554321   -->  true
# 1234512345   -->  false
# 048665088X   -->  true
# X123456788   -->  false

import re

def valid_ISBN10(isbn):
    print(isbn)
    if not re.match(r"^(\d){9}(\d|[X]){1}$", isbn):
        return False
    result = 0
    for ind in range(len(isbn) - 1):
        result += int(isbn[ind]) * (ind + 1)
    if isbn[-1] == "X":
        result += 10 * 10
    else:
        result += int(isbn[-1]) * 10
    return result % 11 == 0

# Best practice and clever solution:
#
# import re
#
# def valid_ISBN10(isbn):
#     return bool(re.match("\d{9}[\dX]$", isbn)) and sum("0123456789X".index(d) * i for i, d in enumerate(isbn, 1)) % 11 == 0


import numpy as np
import scipy

class KalmanFilter(object):
	def __init__(self):
		self.H = np.eye(8,8)
		for i in range(4):
			self.H[i,4+i] = 1.
		self.update_ = np.eye(4,8)

	def get_projection(self,mean,cov):
		h_m =  float(mean[3])
		std_pos = [h_m/20,h_m/20,1e-1,h_m/20]
		mean_pro = np.dot(self.update_, mean)
		cov_pro = np.linalg.multi_dot((self.update_, cov, self.update_.T)) + np.diag(np.square(std_pos))
		#print(cov,"\n")
		return mean_pro,cov_pro

	def gating_distance(self,mean,cov,trk_pos,only_position=False):
		#print(cov)
		mean_pro, cov_pro = self.get_projection(mean,cov)
		if only_position:
				mean_pro, cov_pro = mean_pro[:2], cov_pro[:2, :2]
				trk_pos = trk_pos[:, :2]
		chol_factor = np.linalg.cholesky(cov_pro)
		#print(np.linalg.eigvalsh(cov_pro),chol_factor)
		dis = trk_pos - mean_pro
		dis_cal = scipy.linalg.solve_triangular(chol_factor, dis.T, lower=True, check_finite=False,overwrite_b=True)
		return np.sum(dis_cal * dis_cal, axis=0)

	def update_tracker(self,mean,cov,trk_pos):
		mean_pro,cov_pro = self.get_projection(mean,cov)
		trk_change = trk_pos - mean_pro
		chol_factor, lower = scipy.linalg.cho_factor(cov_pro, lower=True, check_finite=False)
		kalman_gain = scipy.linalg.cho_solve((chol_factor, lower), np.dot(cov, self.update_.T).T,check_finite=False).T
		## x = x + K(y-H.x)
		new_mean = mean + np.dot(trk_change, kalman_gain.T)
		new_cov = cov - np.linalg.multi_dot((kalman_gain, cov_pro, kalman_gain.T))
		return new_mean,new_cov

	def create_tracker(self,trk_pos):
		h =  float(trk_pos[3])
		mean = np.r_[trk_pos,np.zeros_like(trk_pos)]
		std_deviation = [h/10,h/10,1e-2,h/10,h/16,h/16,1e-5,h/16]
		return mean, np.diag(np.square(std_deviation))

	def predict_tracker(self,mean_prev,cov_prev):
		## K = (P.H)/(H.P.H^T + R)
		mean_pred = np.dot(self.H,mean_prev)
		h_m  = float(mean_prev[3])
		std_dev = [h_m/20,h_m/20,1e-2,h_m/20,h_m/160,h_m/160,1e-5,h_m/160]
		R = np.diag(np.square(std_dev))
		cov_pred = np.linalg.multi_dot((self.H,cov_prev,self.H.T))+R
		return mean_pred,cov_pred


#!/usr/bin/env python

import sys
import RPi.GPIO as GPIO

# The PIN Numbers - Not the GPIO numbers. See central numbers on https://www.electronicwings.com/raspberry-pi/raspberry-pi-gpio-access
RED_PIN = 15
GREEN_PIN = 13
BLUE_PIN = 11

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)

GPIO.setup(RED_PIN, GPIO.OUT)
GPIO.setup(GREEN_PIN, GPIO.OUT)
GPIO.setup(BLUE_PIN, GPIO.OUT)

def printHelpAndExit(exitCode):
    print("Supply a single parameter for colour name [RED, GREEN, BLUE, WHITE] or OFF.")
    exit(exitCode)

if(len(sys.argv) < 2):
    printHelpAndExit(1)

def lightsOff():
    GPIO.output(RED_PIN, GPIO.LOW)
    GPIO.output(GREEN_PIN, GPIO.LOW)
    GPIO.output(BLUE_PIN, GPIO.LOW)

def lightOn(chosenLight):
    GPIO.output(chosenLight, GPIO.HIGH)

if(sys.argv[1] == "RED"):
    print("Red light ON")
    lightsOff()
    lightOn(RED_PIN)
    exit(0)

if(sys.argv[1] == "GREEN"):
    print("Green light ON")
    lightsOff()
    lightOn(GREEN_PIN)
    exit(0)

if(sys.argv[1] == "BLUE"):
    print("Blue light ON")
    lightsOff()
    lightOn(BLUE_PIN)
    exit(0)

if(sys.argv[1] == "WHITE"):
    print("White light ON")
    lightsOff()
    lightOn(BLUE_PIN)
    lightOn(RED_PIN)
    lightOn(GREEN_PIN)
    exit(0)

if(sys.argv[1] == "OFF"):
    print("Lights OFF")
    lightsOff()
    exit(0)

printHelpAndExit(2)


from jsonschema import validate, exceptions

schema = {
        "title": "Course",
        "type": "object",
        "required": ['id', 'name', 'phone', 'url', 'description', 'about',
                        'start_date', 'hascertification', 'status', 'category', 'slug'],
        "properties": {
            "id": {"type": "integer"},
            "name": {"type": "string", "minLength": 8, "maxLength": 100},
            "slug": {"type": "string"},
            "category": {"type": "integer", "minimum": 1},
            "phone": {"type": "string", "pattern": "[0-9]"},
            "url": {"type": "string", "format": "url"},
            "description": {"type": "string"},
            "about": {"type": "string"},
            "start_date": {"type": "string", "format": "date"},
            "hascertification": {"type": "boolean"},
            "status": {"type": "boolean"}
          }
        }

try:
    validate(instance=
        # {"id": 20, "name": 0, "slug": "course20A", "category": 1, "phone": "344-44444",
        # "url": "http:\/\/127.0.0.1:8000\/admin\/courses", "description": "description Course", "about": "about Course",
        # "start_date": "2018-07-01", "hascertification": True, "status": True}

        {'id': 15, 'name': 'Course15A', 'slug': 'course15A', 'category': 1, 'phone': '34444444',
        'url': 'http://127.0.0.1:8000/admin/courses', 'description': 'description Course', 'about': 'about Course',
        'start_date': '2018-07-01', 'hascertification': True, 'status': True}
             , schema=schema)
except exceptions.ValidationError as e:
    print(f"Please check: {e.message}")
else:
    print("JSON OK")


#!/usr/bin/env python3

import os
import shutil
import sys
import argparse
import subprocess

CLEAR = '\x1b[0m'
RED = '\x1b[1;34;0m'
YELLOW = '\x1b[1;33;0m'
GREEN = '\x1b[1;32;0m'
WHITE = '\x1b[1;37;0m'

def parse_cmd_arguments():
    """ Get the necessary metadata to complete the KAF."""
    parser = argparse.ArgumentParser(
        description="Process a CONLL file into a kaf one")
    parser.add_argument(
        '--language',  dest='languages',  nargs='*', default=["ca", "es", "it", ],
        help="Language to process")
    parser.add_argument(
        '--information', dest='information',  nargs='*', default=["open", "closed"],
        choices=["open", "closed"],
        help="Language to process")
    parser.add_argument(
        '--system', dest='systems',  nargs='*', default=["relaxcor", "sucre", "tanl-1", "ubiu", "bart"],
        choices=["relaxcor", "sucre", "tanl-1", "ubiu", "bart", "corry-b", "corry-c", "corry-m"],
        help="Language to process")
    parser.add_argument(
        '--annotation', dest='annotations', nargs='*', default=["gold", "regular"],
        choices=["gold", "regular"],
        help="Language to process")
    parser.add_argument(
        '--script', dest='scripts',  nargs='*', default=["../conll-scorer/v8.01/scorer.pl"],
        choices=["v8", "semeval_official"],
        help="Language to process")
    parser.add_argument(
        '--results_path', dest='results_path', action='store', default="./corpora/flat",
        help="Language to process")
    parser.add_argument(
        '--gold_path', dest='gold_path', action='store', default="./original_task",
        help="Language to process")
    parser.add_argument(
        '--metrics', dest='metrics', nargs="*", action='store', default=["MD", "ceafm", "muc", "bcub", "blanc"],
        help="Language to process")

    return parser.parse_args()

def header(metrics):
    return '\n|  | ' + ' |  |  | '.join(metrics + ["Semeval"]) + ' | | |\n' +\
        '| ' + ' | '.join(["---"] * 19) + ' |\n' + \
        '| ' + ' | '.join(["system"] + ["R", "P", "F1"] * 6) + ' |\n'

def evaluate_system(script, metrics, origin, destiny, prefix):
    error = False
    results = {}
    for metric in metrics:
        if metric == "MD":
            # Calculate mention detection with fastest metric
            m = "muc"
        else:
            m = metric
        p = subprocess.Popen(
           ["perl",  script,  m,  origin, destiny],
           stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        pipe, err = p.communicate()
        if err:
            error = True
            results[metric, "R"] = float("Nan")
            results[metric, "P"] = float("Nan")
            results[metric, "F1"] = float("Nan")
            print("Error at {} ".format(metric), end='')
            with open(prefix+"{}_error.txt".format(metric), "w") as err_file:
                err_file.write(err.decode("utf-8"))
        else:
            evaluation = pipe.decode("utf-8")
            with open(prefix + "{}_evaluation.txt".format(metric), "w") as eval_file:
                eval_file.write(evaluation)

            if metric == "MD":
                line, r_index, p_index, f1_index = -5, -4, -3, -2
            else:
                line, r_index, p_index, f1_index = -3, -4, -3, -2

            tokens = evaluation.split("\n")[line].split("%")
            results[metric, "R"] = float(tokens[r_index].split(" ")[-1])
            results[metric, "P"] = float(tokens[p_index].split(" ")[-1])
            results[metric, "F1"] = float(tokens[f1_index].split(" ")[-1])
            with open(prefix + "{}_evaluation.txt".format(metric), "w") as eval_file:
                eval_file.write(evaluation)
    return results, error

def calculate_mean(results, label):
    try:
        results[(label, "R")] = ((results[("muc", "R")] + results[("bcub", "R")] + results[("ceafm", "R")]) / 3)
        results[(label, "P")] = ((results[("muc", "P")] + results[("bcub", "P")] + results[("ceafm", "P")]) / 3)
        results[(label, "F1")] = ((results[("muc", "F1")] + results[("bcub", "F1")] + results[("ceafm", "F1")]) / 3)
        return False
    except KeyError:
        results[(label, "R")] = float("NaN")
        results[(label, "P")] = float("NaN")
        results[(label, "F1")] = float("NaN")
        print("Error at mean ", end='')
        return True

def table_inner_head(title, flat_metric):
    return "| {} | ".format(title) + " | ".join("   " for m in flat_metric) + " |\n"

def process(languages, information, annotations, systems, scripts, results_path, gold_path, metrics):
    flat_metric = [(m, k)
                   for m in (metrics + ["semeval"])
                   for k in ("R", "P", "F1")]

    with open("README.md", "w") as output:
        with open("preface.md") as pref:
            for script in scripts:
                output.writelines(pref.readlines())
                output.write(header(metrics))
                for language in languages:
                    shutil.rmtree(os.path.join("log", language), ignore_errors=True)
                    os.makedirs(os.path.join("log", language))
                    # output.write("#### Language {}\n".format(language))
                    output.write(table_inner_head("Language: {} ".format(language), flat_metric))
                    print("Language {}".format(language))
                    for annotation in annotations:
                        origin = os.path.join(results_path, language) + "_test_auto.conll"
                        if not os.path.exists(origin):
                            print("File does not exist: {}".format(origin), file=sys.stderr)
                            continue
                        for inf in information:
                            print("\tsection:    " + WHITE + "{:>8}".format(inf) + CLEAR)
                            print("\tannotation: " + WHITE + "{:>8}".format(annotation) + CLEAR)
                            results = {}
                            output.write(
                                table_inner_head("Information: {}, Annotation: {}"
                                                 .format(inf, annotation), flat_metric))
                            for system in systems:
                                print("\t\t{:.<15}...".format(system), end='')
                                destiny = os.path.join(gold_path, system, inf, annotation, language) + ".conll"
                                if not os.path.exists(destiny):
                                    print("{:.>15}".format(YELLOW+"Not reported " + CLEAR))
                                else:
                                    prefix = "log/{}/{}_{}_{}".format(language, inf, annotation, system)
                                    results[system], error = evaluate_system(script, metrics, origin, destiny, prefix)
                                    error |= calculate_mean(results[system], "semeval")
                                    if error:
                                        print("{:.>15}".format(RED + "Error" + CLEAR))
                                    else:
                                        print("{:.>15}".format(GREEN + "Processed" + CLEAR))

                                    output.write(
                                        "| {} | ".format(system) +
                                        " | ".join("{0:.2f}".format(results[system][m]) for m in flat_metric) +
                                        " |\n")

if __name__ == '__main__':
    process(**vars(parse_cmd_arguments()))


'''
Functions to evaluate an XGBoost classification model for train/validation purposes.
'''
from sklearn.metrics import confusion_matrix
from sklearn import datasets
from sklearn.metrics import roc_auc_score, roc_curve
from sklearn.model_selection import train_test_split
from sklearn.metrics import precision_score, recall_score, precision_recall_curve,f1_score, fbeta_score, auc
import pandas as pd
from importlib import reload
import numpy as np
import scipy.stats as st
import matplotlib.pyplot as plt
import seaborn as sns
import matplotlib.cm as cm
from matplotlib import pyplot
from sklearn.model_selection import KFold
from xgboost import XGBClassifier
from xgboost import plot_importance

def confusion_matrix_generator(confusion_matrix, name):
    '''
    Arguments: takes in the basic confusion matrix, and the name of the model to title the output graph.
    Returns: a visually appealing Seaborn confusion matrix graph.
    '''
    plt.figure(dpi=150)
    sns.heatmap(confusion_matrix, cmap=plt.cm.Blues, annot=True, square=True, fmt='d',
           xticklabels=['Non-Show', 'Show'],
           yticklabels=['Non-Show', 'Show'])

    plt.xlabel('Predicted Finish - Show or No Show')
    plt.ylabel('Actual Finish - Show or No Show')
    plt.title('{} confusion matrix'.format(name));

def xgboost_eval(X,y):
    '''
    Arguments: takes in a set of features X and a target variable y.  Y is a classification (0/1).
    Returns: Performs XGBoost classification and returns the scores.
    The XGBoost parameters used are: n_estimators=100, learning_rate = 0.05, max_depth=6, min_child_weight = 3, use_label_encoder = False, objective = 'binary:logistic'
    '''
    #SPlit the data into train and validation sets:
    X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=5, stratify=y)

    #Fitting the Model:
    xgb_model = XGBClassifier(n_estimators=100, learning_rate = 0.05, max_depth=6,
                         min_child_weight = 3, use_label_encoder = False, objective = 'binary:logistic')
    xgb_model.fit(X_train, y_train)

    #Scoring the Model:
    y_pred = xgb_model.predict(X_val)
    train_accuracy = xgb_model.score(X_train, y_train)
    val_accuracy = xgb_model.score(X_val, y_val)
    precision = precision_score(y_val, y_pred)
    recall = recall_score(y_val, y_pred)
    fbetascore = fbeta_score(y_val, y_pred, beta=0.5)
    f1score = f1_score(y_val, y_pred)

    #Printing Scores:
    print('Model Training Set Accuracy: {}'.format(train_accuracy))
    print('Model Validation Set Accuracy: {}'.format(val_accuracy))
    print('Model Precision Score: {}'.format(precision))
    print('Model Fbeta (beta = 0.5) Score: {}'.format(fbetascore))
    print('Model Recall Score: {}'.format(recall))
    print('Model F1 Score: {}'.format(f1score))

    #Confusion Matrix:
    cm = confusion_matrix(y_val, y_pred)
    confusion_matrix_generator(cm, 'XGBoost')

    return xgb_model

def xgboost_feature_importance(model):
    '''
    Arguments: a model and a list of feature names.
    Returns: a plot with feature importance given the model.
    '''
    xgb.plot_importance(model)
    xgb.plot_importance(mode, importance_type='gain')

def xgboost_eval_kfold(X,y, k=5, threshold = 0.5):
    '''
    Arguments: takes in a set of features X and a target variable y.  Y is a classification (0/1).  Threshold can be changed from 0.5 default.  Uses k=5 as default k for K-Fold cross validation.
    Returns: Performs XGBoost classification and returns the scores.
    The XGBoost parameters used are: n_estimators=100, learning_rate = 0.05, max_depth=6, min_child_weight = 3, use_label_encoder = False, objective = 'binary:logistic'
    '''
    X_cv, y_cv = np.array(X), np.array(y)
    kf = KFold(n_splits=k, shuffle=True, random_state = 12)

    #Setting up empty lists:
    cv_xg_acc = []
    cv_xg_prec = []
    cv_xg_rec = []
    cv_xg_fbeta = []
    cv_xg_f1 = []

    #K-Fold Loop:
    i = 1
    for train_ind, val_ind in kf.split(X_cv,y_cv):
        X_train, y_train = X_cv[train_ind], y_cv[train_ind]
        X_val, y_val = X_cv[val_ind], y_cv[val_ind]

        #Running Model and making predictions:
        xgb_model = XGBClassifier(n_estimators=100, learning_rate = 0.05, max_depth=6,
                         min_child_weight = 3, use_label_encoder = False, objective = 'binary:logistic')
        xgb_model.fit(X_train, y_train)

        y_pred = (xgb_model.predict_proba(X_val)[:, 1] >= threshold)

        #Printing Confusion Matrix for each round:
        cm = confusion_matrix(y_val, y_pred)
        print("Confusion Matrix for Fold {}".format(i))
        print(cm)
        print('\n')
        i += 1

        #Scores:
        cv_xg_acc.append(xgb_model.score(X_val, y_val))
        cv_xg_prec.append(precision_score(y_val, y_pred))
        cv_xg_rec.append(recall_score(y_val, y_pred))
        cv_xg_fbeta.append(fbeta_score(y_val, y_pred, beta=0.5))
        cv_xg_f1.append(f1_score(y_val, y_pred))

    print('XGBoost Classification w/ KFOLD CV Results (k={}, threshold = {}):'.format(k, threshold))
    print('XG Boost Accuracy scores: ', cv_xg_acc, '\n')
    print(f'Simple mean cv accuracy: {np.mean(cv_xg_acc):.3f} + {np.std(cv_xg_acc):.3f}')
    print('XG Boost Precision scores: ', cv_xg_prec, '\n')
    print(f'Simple mean cv precision: {np.mean(cv_xg_prec):.3f} +- {np.std(cv_xg_prec):.3f}')
    print('XG Boost Recall scores: ', cv_xg_rec, '\n')
    print(f'Simple mean cv recall: {np.mean(cv_xg_rec):.3f} +- {np.std(cv_xg_rec):.3f}')
    print('XG Boost Fbeta (beta=0.5) scores: ', cv_xg_fbeta, '\n')
    print(f'Simple mean cv Fbeta (beta=0.5): {np.mean(cv_xg_fbeta):.3f} +- {np.std(cv_xg_fbeta):.3f}')
    print('XG Boost F1 scores: ', cv_xg_f1, '\n')
    print(f'Simple mean cv F1: {np.mean(cv_xg_f1):.3f} +- {np.std(cv_xg_f1):.3f}')

    return xgb_model


def mygcd(a, b):
    if (b == 0):
        return a
    else:
        return mygcd(b, a % b)

a = int(input("Enter a: "))
b = int(input("Enter b: "))

result = mygcd(a,b)
print('gcd (%s , %s ) = %s ' %(a, b, result))


import argparse
import sqlite3
import sys

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "-t", "--timeout", type=str, metavar="MS", help="Set busytimeout"
    )
    parser.add_argument(
        "dbpath", type=str, metavar="DBPATH", nargs=1, help="database path"
    )
    parser.add_argument("query", type=str, metavar="QUERY", nargs=1, help="query")
    args = parser.parse_args()

    try:
        #  Required in non-utf-8 locale if dbpath contains multibyte
        #   characters. Prevents Python from whining about surrogates
        #   not allowed. Really there must be a better way, but this works:
        dbpath = args.dbpath[0].encode("utf-8", errors="surrogateescape").decode()
        dburi = "file:" + dbpath + "?mode=ro"
        con = sqlite3.connect(dburi, uri=True)
    except sqlite3.Error as e:
        print(e)
        sys.exit(1)

    if args.timeout:
        con.execute("PRAGMA busy_wait = " + args.timeout)

    con.row_factory = sqlite3.Row
    cursor = con.cursor()

    try:
        cursor.execute(args.query[0])
    except sqlite3.Error as e:
        print(e)
        sys.exit(1)

    rows = cursor.fetchall()

    #  make print below safe to handle utf-8
    utf8out = open(1, "w", encoding="utf-8", closefd=False)

    for row in rows:
        for key in row.keys():
            val = row[key]
            print(f"{key} = {val}", file=utf8out)

    con.close()
    sys.exit(0)

# vim: tabstop=4 shiftwidth=4 expandtab


class Solution:
    def numIslands(self, grid):
        """
        :type grid: List[List[str]]
        :rtype: int
        """
        if len(grid) == 0 or len(grid[0]) == 0:
            return 0

        def deeper(i, j, grid):
            if 0<=i<len(grid) and 0<=j<len(grid[0]) and grid[i][j]=="1":
                grid[i][j] = "0"

                deeper(i-1, j, grid)
                deeper(i+1, j, grid)
                deeper(i, j-1, grid)
                deeper(i, j+1, grid)

        count = 0
        for i in range(len(grid)):
            for j in range(len(grid[0])):
                if grid[i][j] == "1":
                    deeper(i,j, grid)
                    count += 1
        return count

grid = [["1","1","0","0","0"],
["1","1","0","0","0"],
["0","0","1","0","0"],
["0","0","0","1","1"]]

print(Solution().numIslands(grid))


from workflow_engine.strategies import  InputConfigMixin, ExecutionStrategy
from at_em_imaging_workflow.render_strategy_utils import RenderStrategyUtils
from at_em_imaging_workflow.models import ChunkAssignment
from rendermodules.pointmatch.schemas import TilePairClientParameters
from at_em_imaging_workflow.two_d_stack_name_manager import TwoDStackNameManager
import logging

class CreateRoughPairsStrategy(InputConfigMixin, ExecutionStrategy):
    _log = logging.getLogger(
        'at_em_imaging_workflow.strategies.rough.'
        'create_rough_pairs_strategy')

    def transform_objects_for_queue(self, source_object):
        chnk = source_object
        tile_pair_ranges = chnk.get_tile_pair_ranges()

        chunk_assignments = [
            ChunkAssignment.objects.get(
                chunk=chnk,
                section__z_index=tile_pair_ranges[x]['tempz']
            ) for x in tile_pair_ranges.keys()]

        return chunk_assignments

    def get_input(self, chk_assgn, storage_directory, task):
        inp = self.get_workflow_node_input_template(task)

        chnk = chk_assgn.chunk

        inp['render'] = RenderStrategyUtils.render_input_dict(chnk)

        inp['output_dir'] = chnk.get_storage_directory()

        tile_pair_ranges = chnk.get_tile_pair_ranges()

        section_z_index = chk_assgn.section.z_index

        for k in tile_pair_ranges.keys():
            tile_pair_range = tile_pair_ranges[k]
            if tile_pair_range['tempz'] == section_z_index:
                min_z = tile_pair_range['minz']
                max_z = tile_pair_range['maxz']

                inp["minZ"] = min_z
                inp["maxZ"] = max_z
                inp["zNeighborDistance"] = tile_pair_range["zNeighborDistance"]

        stack_names = TwoDStackNameManager.create_rough_pair_stacks(chk_assgn)
        inp['baseStack'] = stack_names['baseStack']
        inp['stack'] = stack_names['stack']

        return TilePairClientParameters().dump(inp).data

    def on_finishing(self, chk_assgn, results, task):
        self.check_key(results, ChunkAssignment.TILE_PAIR_FILE_KEY)
        chk_assgn.create_rough_tile_pair_file(
            results[ChunkAssignment.TILE_PAIR_FILE_KEY]
        )

    def get_storage_directory(self, base_storage_directory, job):
        chnk = job.enqueued_object

        return chnk.get_storage_directory(base_storage_directory)


# tests/test_expense.py

import json
from typer.testing import CliRunner
import pytest

from tracker import (
        DB_READ_ERROR,
        SUCCESS,
        __app_name__,
        __version__,
        cli,
        tracker,
)

runner = CliRunner()

def test_version():
    result = runner.invoke(cli.app, ["--version"])
    assert result.exit_code == 0
    assert "%s v %s" % (__app_name__,__version__) in result.stdout

@pytest.fixture
def mock_json_file(tmp_path):
    todo = [{
        "Description": "some milk.",
        "amount": 2.0,
        "Done": False
        }]
    db_file = tmp_path / "todo.json"
    with db_file.open("w") as db:
        json.dump(todo, db, indent=4)
    return db_file

test_data1 = {
        "description": ["Clean", "the", "house"],
        "amount": 1.0,
        "todo": {
            "Description": "Clean the house.",
            "amount": 1.0,
            "Done": False,
            },
}

test_data2 = {
        "description" : ["Wash the car"],
        "amount": 2.0,
        "todo": {
            "Description": "Wash the car.",
            "amount": 2.0,
            "Done": False,
        },
}

@pytest.mark.parametrize(
        "description, amount, expected",
        [
            pytest.param(
                test_data1["description"],
                test_data1["amount"],
                (test_data1["expense"],SUCCESS),
                ),
            pytest.param(
                test_data2["description"],
                test_data2["amount"],
                (test_data2["todo"], SUCCESS),
                ),
        ],
)

def test_add(mock_json_file, description, amount, expected):
    expenser = tracker.Tracker(mock_json_file)
    assert expenser.add(description, amount) == expected
    read = expenser._db_handler.read_expense()
    assert len(read.todo_list) == 2


# Importação das bibliotecas
import streamlit as st
import sys
sys.path.append('parts')
sys.path.append('images')
sys.path.append('data')
from parts.inicio import inicio_func
from parts.about import about_func
from parts.cenipa import cenipa_func
from parts.filtro import filtro_func

st.set_page_config(page_title="Multi App - Thiago Lopes",
                   page_icon=":computer:",
                   layout="wide",
                   initial_sidebar_state="collapsed")

#from parts.mlapp import mlapp_func
st.set_option('deprecation.showPyplotGlobalUse', False)

def main():
    # Estrutura da barra lateral
    st.sidebar.header("Multi App - Streamlit Sharing")
    st.sidebar.subheader("Por Thiago Lopes")

    # Menu de Páginas
    opcoes_menu = ("Início","FILTRO","CENIPA", "MLApp","Sobre mim")
    escolha = st.sidebar.selectbox("Escolha uma opção", opcoes_menu)

    if escolha == "Início":
        inicio_func()

    elif escolha == "FILTRO":
        filtro_func()

    elif escolha == "CENIPA":
        cenipa_func()

    elif escolha == "MLApp":
        #mlapp_func()
        st.markdown("# Em construção")

    elif escolha == 'Sobre mim':
        about_func()

if __name__ == '__main__':
    main()


import math
import sys

def factorize(n):
    factors = {}
    i = 2
    while i <= math.sqrt(n):
        if n % i == 0:
            n //= i
            factors[i] = factors.get(i, 0) + 1
        else:
            i += 1
    if n > 1:
        factors[n] = factors.get(n, 0) + 1
    return factors

def div_factorial(n, factors):
    for f, exp in factors.items():
        i = f
        factor_exp = 0
        while i <= n:
            factor_exp += n // i
            i *= f
        if factor_exp < exp:
            return False
    return True

ans = []
while True:
    try:
        n, m = map(int, sys.stdin.readline.split())

        if m == 0:
            print(m + " does not divide " + n + "!")
        else:
            factors_m = factorize(m)
            factors_m.pop(1, None)

            if div_factorial(n, factors_m):
                print((str(m) + " divides " + str(n) + "!"))
            else:
                print(m + " does not divide " + n + "!")
    except:
        EOFError