import time
from common.common_methods import *
#from common.send_email import *
from modules.admin_page import *
from modules.launch_chrome_and_login_app import *
from modules.dashboard_page import *
from modules.time_page import *
from modules.about_page import *
config_variables = load_config_file()

"""
Functional smoke test
"""

def chrome_launch_and_hrm_login(driver):
    print("Test Run Start time is : ")
    calculate_test_time()
    test_name={'name':'chrome_launch_and_hrm_login'}
    try:
        app_login = LoginPage(driver)
        app_login.login()
        save_success_status( test_name)
    except Exception as exception:
        save_failure_status( test_name, exception, driver )

def dashboard_test(driver):
    test_name={'name':'dashboard_test'}
    try:
        dashboard_page = DashboardPage(driver)
        dashboard_page.dashboard_verify()
        save_success_status( test_name)
    except Exception as exception:
        save_failure_status( test_name, exception, driver )

def dashboard_click_link(driver):
    test_name={'name':'dashboard_click_link'}
    try:
        dashboard_page = DashboardPage(driver)
        dashboard_page.dashboard_click_links()
        save_success_status( test_name)
    except Exception as exception:
        save_failure_status( test_name, exception, driver )

def time_click_link(driver):
    test_name={'name':'time_click_link'}
    try:
        time_page = TimePage(driver)
        time_page.verify_navigation_punch()
        save_success_status( test_name)
    except Exception as exception:
        save_failure_status( test_name, exception, driver )

def verify_timesheet_menu(driver):
    test_name={'name':'verify_timesheet_menu'}
    try:
        time_page = TimePage(driver)
        time_page.verify_timesheet_menu()
        save_success_status( test_name)
    except Exception as exception:
        save_failure_status( test_name, exception, driver )

def search_admin(driver):
    test_name={'name':'search_admin'}
    try:
        home_page = HomePage(driver)
        home_page.search_and_choose_admin()
        save_success_status( test_name)
    except Exception as exception:
        save_failure_status( test_name, exception, driver )

def admin_header_verification(driver):
    test_name={'name':'admin_header_verification'}
    try:
        home_page = HomePage(driver)
        home_page.verify_admin_header()
        save_success_status( test_name)
    except Exception as exception:
        save_failure_status( test_name, exception, driver )

def admin_add(driver):
    test_name={'name':'admin_add'}
    try:
        home_page = HomePage(driver)
        home_page.add()
        save_success_status( test_name)
    except Exception as exception:
        save_failure_status( test_name, exception, driver )

def verify_about_dropdown(driver):
    test_name={'name':'verify_about_dropdown'}
    try:
        home_page = AboutPage(driver)
        home_page.verify_about()
        save_success_status( test_name)
    except Exception as exception:
        save_failure_status( test_name, exception, driver )

def verify_user_management(driver):
    test_name={'name':'verify_user_management'}
    try:
        home_page = HomePage(driver)
        home_page.click_and_verify_user_management()
        save_success_status( test_name)
    except Exception as exception:
        save_failure_status( test_name, exception, driver )

def verify_help_button(driver):
    test_name={'name':'verify_help_button'}
    try:
        home_page = HomePage(driver)
        home_page.verify_help_button()
        save_success_status( test_name)
    except Exception as exception:
        save_failure_status( test_name, exception, driver )

def tear_down(driver):
    test_name={"name":"tear_down"}
    try:
        driver.close()
        driver.quit()
        save_success_status(test_name)
        print("Test Run End time is : ")
        calculate_test_time()
    except Exception as exception:
        save_failure_status(test_name, exception, driver)

if __name__ == "__main__":
    driver = initialize_chromedriver()
    if(config_variables["is_docker_headless"]=="True"):
        driver.set_window_size(1024, 768)
    chrome_launch_and_hrm_login(driver)
    dashboard_test(driver)
    dashboard_click_link(driver)
    time_click_link(driver)
    verify_timesheet_menu(driver)
    search_admin(driver)
    admin_header_verification(driver)
    admin_add(driver)
    verify_about_dropdown(driver)
    verify_user_management(driver)
    verify_help_button(driver)
    tear_down(driver)

else:
   print("***Unable to invoke test, restart again***")


# # ------ Truthy/Falsey ------ #
# print("What is your name?")
# name = input()
# if name:
#     print(f"Hello {name}, how are you?")
# else:
#     # if value is falsey ie. black, else is met
#     print("You did not give me your name!")

# # ------ Not Operator ------ #

# bool = False  # our input
# if bool != True:  # not true
#     print(False)
# else:
#     print(True)

# # ------ Try Except ------ #

def add_up():
    n1 = input("1st num\n")
    n2 = input("2nd num\n")

    try:
        print(f"{n1} + {n2} is {(int(n1) + int(n2))}!")

    except:
        print("*******************")
        print("ERROR: only numbers")
        print("*******************")
        add_up()

add_up()

# def add_up1():
#     n1 = input("1st num\n")
#     n2 = input("2nd num\n")

#     try:
#         if (n1 + n2) == 4:
#             print(n1 + n2)

#     except:
#         print("There is error")
#     else:
#         print("whats happening here")
#     finally:
#         print("i give up")

#         add_up()

# add_up1()

# ------ Scope ------ #
# light = False

# def light_switch():
#     global light  # makes variable global throught code
#     if light:
#         print("Whoa! It's bright in here")
#         light = False
#     else:
#         print("HEY! Who turned out the lights?")
#         light = True

# light_switch()
# light_switch()

# # ------ Lists and Tuple ------ #

# even_nums = [2, 4, 6, 8, 10]  # list #?Mutablle
# odd_nums = (1, 3, 5, 7, 9)  # tuple #?Immutable

# # lists can be changed, tuples cannot

# even_nums[-1] = "ten"
# #! odd_nums[-1] = "nine" --> Error

# # ------ Slicing ------ #

# aplphabet = [
#     "a",
#     "b",
#     "c",
#     "d",
#     "e",
#     "f",
#     "g",
#     "h",
#     "i",
#     "j",
#     "k",
#     "l",
#     "m",
#     "n",
#     "o",
#     "p",
#     "q",
#     "r",
#     "s",
#     "t",
#     "u",
#     "v",
#     "w",
#     "x",
#     "y",
#     "z"]
# # the alphabet in a list

# def user():
#     usernum = input("1-26?\n")
#     usernum = int(usernum) - 1
#     print(usernum)

#     return aplphabet[:12]

# print(user())

# ------ WHILE ------ #

# while True: #loop_runs = True
#     print("Run orever")

#     loop_runs = False #stops loop

# loop = True
# name = input("\nUsername:")

# while loop:
#     if name == "Admin":
#         print(f"Hello {name}")
#         break
#     else:
#         print(f"Fuck Off {name}")
#         continue


from argparse import ArgumentParser
import cv2
import sys
import time
import numpy as np
import threading
from imutils.video.filevideostream import FileVideoStream
from mark_detector import MarkDetector
from os_detector import detect_os
from pose_estimator import PoseEstimator
from stabilizer import Stabilizer

# import the Queue class from Python 3
if sys.version_info >= (3, 0):
    from queue import Queue
# otherwise, import the Queue class for Python 2.7
else:
    from Queue import Queue

print("OpenCV version: {}".format(cv2.__version__))
# multiprocessing may not work on Windows and macOS, check OS for safety.
detect_os()

CNN_INPUT_SIZE = 128

# Take arguments from user input.
parser = ArgumentParser()
parser.add_argument("--video", type=str, default="abc.mp4",
                    help="Video file to be processed.")
parser.add_argument("--cam", type=int, default=None,
                    help="The webcam index.")
args = parser.parse_args()

class Main(threading.Thread):

    def __init__(self, cap, box_que, img_que, detector, video_src, sample_frame):
        threading.Thread.__init__(self)
        # self.condition = condition
        self.cap = cap
        self.img_que = img_que
        self.box_que = box_que
        self.mark_detector = detector
        self.video_src = video_src
        self.sample_frame = sample_frame

    def get_face(self, detector, img_queue, box_queue):
        """Get face from image queue. This function is used for multiprocessing"""
        while True:
            image = img_queue.get()
            box = detector.extract_cnn_facebox(image)
            box_queue.put(box)

    def run(self):

        # Introduce mark_detector to detect landmarks.
        box_thread = threading.Thread(target=self.get_face,
                                      args=(self.mark_detector, self.img_que, self.box_que))
        box_thread.start()

        head_thread = threading.Thread(target=self.process_head_pose,
                                       args=(self.sample_frame, self.img_que, self.box_que,
                                             self.cap, self.video_src, self.mark_detector))
        head_thread.start()

    def process_head_pose(self, sample_frame, img_queue, box_queue, cap, video_src, mark_detector):
        height, width = sample_frame.shape[:2]
        pose_estimator = PoseEstimator(img_size=(height, width))

        # Introduce scalar stabilizers for pose.
        pose_stabilizers = [Stabilizer(
            state_num=2,
            measure_num=1,
            cov_process=0.1,
            cov_measure=0.1) for _ in range(6)]

        tm = cv2.TickMeter()
        # cap.start()
        while True:
            # Read frame, crop it, flip it, suits your needs.
            frame = cap.read()
            if frame is None:
                break

            # Crop it if frame is larger than expected.
            # frame = frame[0:480, 300:940]

            # If frame comes from webcam, flip it so it looks like a mirror.
            if video_src == 0:
                frame = cv2.flip(frame, 2)

            # Pose estimation by 3 steps:
            # 1. detect face;
            # 2. detect landmarks;
            # 3. estimate pose

            # Feed frame to image queue.
            img_queue.put(frame)

            # Get face from box queue.
            facebox = box_queue.get()

            if facebox is not None:
                # Detect landmarks from image of 128x128.
                face_img = frame[facebox[1]: facebox[3],
                           facebox[0]: facebox[2]]
                face_img = cv2.resize(face_img, (CNN_INPUT_SIZE, CNN_INPUT_SIZE))
                face_img = cv2.cvtColor(face_img, cv2.COLOR_BGR2RGB)

                tm.start()
                marks = mark_detector.detect_marks([face_img])
                tm.stop()

                # Convert the marks locations from local CNN to global image.
                marks *= (facebox[2] - facebox[0])
                marks[:, 0] += facebox[0]
                marks[:, 1] += facebox[1]

                # Uncomment following line to show raw marks.
                # mark_detector.draw_marks(
                #     frame, marks, color=(0, 255, 0))

                # Uncomment following line to show facebox.
                # mark_detector.draw_box(frame, [facebox])

                # Try pose estimation with 68 points.
                pose = pose_estimator.solve_pose_by_68_points(marks)

                # Stabilize the pose.
                steady_pose = []
                pose_np = np.array(pose).flatten()
                for value, ps_stb in zip(pose_np, pose_stabilizers):
                    ps_stb.update([value])
                    steady_pose.append(ps_stb.state[0])
                steady_pose = np.reshape(steady_pose, (-1, 3))

                # Uncomment following line to draw pose annotation on frame.
                # pose_estimator.draw_annotation_box(
                #     frame, pose[0], pose[1], color=(255, 128, 128))

                # Uncomment following line to draw stabile pose annotation on frame.
                pose_estimator.draw_annotation_box(
                    frame, steady_pose[0], steady_pose[1], color=(128, 255, 128))

                # Uncomment following line to draw head axes on frame.
                # pose_estimator.draw_axes(frame, stabile_pose[0], stabile_pose[1])

            # Show preview.
            cv2.imshow("Preview", frame)
            if cv2.waitKey(1) == 27:
                break

    def stop(self):
        self.join()

def main():
    # Video source from webcam or video file.

    video_src = args.cam if args.cam is not None else args.video
    if video_src is None:
        print("Warning: video source not assigned, default webcam will be used.")
        video_src = 0

    cap = FileVideoStream(video_src, queue_size=200)
    cap.start()
    # time.sleep(0.15)
    if video_src == 0:
        cap.stream.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
    sample_frame = cap.read()

    # Setup process and queues for multiprocessing.
    img_queue = Queue()
    box_queue = Queue()
    img_queue.put(sample_frame)
    mark_detector = MarkDetector()
    box_process = Main(cap, box_queue, img_queue, mark_detector, video_src, sample_frame)
    box_process.start()
    # cap.stop()

if __name__ == '__main__':
    # Video source from webcam or video file.
    main()


def _cross_entropy(df, coefs):
    return -sum(
        -z*(1-y) - np.log(1+np.exp(-z))
        for z, y
        in zip(logodds(df, coefs), df['Survived'].values)
    ) / df.shape[0]

def cross_entropy(df, coefs):
    z = logodds(df, coefs)
    logliks = -z*(1-df['Survived'].values) - np.log(1+np.exp(-z))
    return -logliks.mean()

def gradient_descent(df, coefs, eta=0.01):
    X = df[['Sex', 'Age', 'Pclass']].values
    Y = df['Survived'].values
    Yhat = expit(logodds(df, coefs))
    δ = Yhat - Y
    dcoefs = X.T @ δ / δ.shape[0]
    assert dcoefs.shape == coefs.shape
    return coefs - eta * dcoefs


import os
import json
import requests
from bs4 import BeautifulSoup as bs

from pprint import pprint

dir_path = os.path.dirname(os.path.realpath(__file__))
parent_path = os.path.dirname(dir_path)

# to update the mocs.json uncomment the last script tag in index.html and
# reload the window.
def run_image_get():
    mocs_data = dict()
    with open(dir_path + '/mocs.json', 'r', encoding='latin') as mocs_file:
        mocs_data = json.load(mocs_file)

    base_wiki_url = 'https://en.wikipedia.org'

    for key, moc in mocs_data.items():
        try:
            save_path = (parent_path +
                        '/resources/')

            saved_images = os.listdir(save_path)
            already_stored = any(
                    [str(moc['govtrack_id']) in img for img in saved_images])

            print('already stored:', moc['govtrack_id'], already_stored)

            if moc['in_office'] and not already_stored:
                try:
                    wiki_id = '/wiki/' + moc['wikipedia_id'].replace(' ', '_')
                    url = base_wiki_url + wiki_id
                    r = requests.get(url)
                    soup = bs(r.content, 'html.parser')
                    try:
                        card = soup.find('table', class_='infobox vcard')
                        img_link = card.find('a', class_='image')['href']
                        img_link = base_wiki_url + img_link

                        r = requests.get(img_link)
                        soup = bs(r.content, 'html.parser')
                        file_div = soup.find('div', class_='fullImageLink')
                        link = file_div.find('a')['href']

                        if 'https:' not in link:
                            link = 'https:' + link

                        r = requests.get(link, stream=True)
                        ending = url[-4:]
                        if '.' not in ending:
                            ending = '.jpg'
                        if '_Jr' in ending:
                            ending = '.jpg'

                        save_path += (moc['govtrack_id'] + ending)
                        with open(save_path, 'wb') as f:
                            for chunk in r.iter_content(chunk_size=1024):
                                if chunk:
                                    f.write(chunk)

                        print('stored image for', moc['govtrack_id'])

                    except AttributeError as e:
                        print('failed to find image for:', moc['govtrack_id'], url.encode('latin'))
                except KeyError as e:
                    print('no wiki url:', moc['govtrack_id'], moc['displayName'].encode('latin'))
        except KeyError as e:
            pass

run_image_get()


#!/usr/bin/python3
import csv
import numpy
import seaborn
import sys
import matplotlib.pyplot as plt
import json

def remove_not_values(perf_list):
    return [float(x.replace(',', '.')) for x in perf_list if x not in ['-', '']]

def parse_performances_from_csv_file(data_file):
    result = []
    with open(data_file, 'r') as f:
        reader = csv.reader(f, delimiter=',', quotechar='"')
        for row in reader:
            if len(row):
                result.append((row[0], remove_not_values(row[1:])))
    return result

def parse_performances_from_dict(securities):
    result = []
    for sec in securities:
        if all(i in sec for i in ["security", "performances"]):
            result.append((sec["security"], sec["performances"]))
        else:
            print(f"Invalid object {sec}")
    return result

def correlations_as_list(corr, names_list):
    result = []
    size = len(corr)
    for i in range(size):
        for j in range(size):
            # Not to get duplicates
            if i < j:
                result.append( (corr[i][j], (names_list[i], names_list[j])) )
    return result

def display_correlations(corr_dict, filter_function):
    for corr_val in sorted(corr_dict, reverse=True):
        if filter_function(corr_val):
            [sec_a, sec_b] = corr_dict[corr_val]
            print("{:.2f}: {:60} {:60}".format(corr_val, sec_a, sec_b))

def display_correlation_heatmap(corr, names_list, api_mode):
    mask = numpy.zeros_like(corr)
    mask[numpy.triu_indices_from(mask)] = True
    legends = names_list
    with seaborn.axes_style("white"):
        seaborn.set(rc={'figure.figsize':(40,30)})
        sns_plot = seaborn.heatmap(corr, mask=mask, square=True, center=0, annot=True, cmap="YlGnBu", xticklabels=legends, yticklabels=legends)
        plt.xticks(rotation=30, ha='right')
        plt.yticks(rotation=30)
        if api_mode:
            sns_plot.get_figure().savefig("output.png")
        else:
            plt.show()

def calculate_corr_matrix(perf_list, api_mode):
    perf = []
    names_list = []
    min_size = sys.maxsize
    for _, (name, perf_history) in enumerate(perf_list):
        perf_history_size = len(perf_history)
        # If vector size < 4, we discard it
        if perf_history_size >= 4:
            min_size = min(min_size, perf_history_size)
            names_list.append(name)
            perf.append(perf_history)
    if len(perf) < 2:
        raise Exception("Cannot compute correlations: not enough securities")

    # We normalize the size of all the vectors
    normalized_performances = []
    limiting = []
    for index, val in enumerate(perf):
        normalized_performances.append(val[:min_size])
        if len(val) == min_size:
            limiting.append(names_list[index])
    if not api_mode:
        print(f"\nLimiting: {min_size} quarters")
        for i in limiting:
            print(i)
    return numpy.corrcoef(normalized_performances), names_list, min_size, limiting

# Example input
# [('Security name 1', [2.32, 5.43, 4.65, 2.98, -1.15]), ('Security name 2', [9.47, 3.47, 3.1, 6.75, -7.63])
# api_mode: does not print nicely the results in the console, does not open the plot
def correlations(performances, api_mode=True):
    corr, names_list, min_size, limiting = calculate_corr_matrix(performances, api_mode)
    corr_list = correlations_as_list(corr, names_list)
    if not api_mode:
        print("\nHighly correlated securities")
        display_correlations(corr_list, lambda corr: corr > 0.8)
        print("\nNegatively correlated securities")
        display_correlations(corr_list, lambda corr: corr < -0.5)
        # based on performances by quarter
        history_years = min_size / 4
        print("\nHistory size: {} years".format(history_years))
    display_correlation_heatmap(corr, names_list, api_mode)
    return corr_list, min_size, limiting

if __name__ == '__main__':
    FILENAME = "data/portfolio.csv"
    perf_list = parse_performances_from_csv_file(FILENAME)
    # perf_json = json.loads('[{"security": "name1", "performances": [1, 2, 3, 4]}, {"security": "name2", "performances": [2, 4, 7, 9]}]')
    # perf_list = parse_performances_from_json(perf_json)
    correlations(perf_list, False)


'''
Ganga Nakarmi
West Virginia University
Coding assistance from Llew Williams

This code was used in collecting data for this research paper:
   "A crowdsourced approach to documenting users’ preferences for landscape attributes in the proposed Appalachian Geopark Region in West Virginia"

This was used to download a set of jpg images from Flickr.
The desired image names and urls were placed in a csv file for processing beforehand.
'''
import requests
import csv
import os
# need path to project folder
project_folder = os.path.abspath(os.path.dirname(__file__))

#csv folder
csv_folder = 'DESIRED_FOLDER'
csv_folder_path = os.path.join(project_folder, csv_folder)
csv_file_name = 'DESIRED_FILE_NAME.csv'
csv_folder_path_and_file = os.path.join(csv_folder_path, csv_file_name)

with open(csv_folder_path_and_file) as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    line_count = 0
    for row in csv_reader:
        if line_count != 0:
            url = row[4]
            line_count += 1
            trimmed_file_name = os.path.basename(row[4])
            image_download_folder = os.path.join(project_folder,'RELATIVE_PATH_TO_OUTPUT_FOLDER')
            trimmed_file_name_and_path = os.path.join(image_download_folder,trimmed_file_name)
            r = requests.get(url, allow_redirects=True)
        else:
            line_count += 1

    print('Processed ' + str(line_count) + ' lines.')


from sklearn.datasets import load_svmlight_file
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import numpy as np
import argparse
import time

def load_dataset(filename):
    x_train, y_train = load_svmlight_file(filename)
    x_train_dense = x_train.todense()
    y_train_matrix = np.matrix(y_train).transpose()
    return x_train_dense, y_train_matrix

def get_subset(num_samples, x_train, y_train):
    if num_samples > len(y_train):
        num_samples = len(y_train)

    indices = np.random.choice(len(y_train), num_samples, replace=False)
    return x_train[indices], y_train[indices]

def train(x_train, y_train):
    return np.linalg.pinv(x_train) * y_train

def inference(input, w):
    return input * w

def compute_error(results, y_train):
    return np.mean(np.power(results - y_train, 2))

def plot_w(weights):
    plt.bar(range(len(weights)), weights, align='center', alpha=0.5)
    plt.ylabel('Weight value')
    plt.xlabel('Feature number')
    plt.title('Weight vector representation')
    plt.show()

def plot_err(list,list_mean,list_median):
    plt.plot(*zip(*list))
    plt.plot(*zip(*list_mean))
    plt.plot(*zip(*list_median))
    plt.title('Error(#samples) in LinReg')
    plt.ylabel('Aproximation Error')
    plt.xlabel('# of Samples')
    red_label = mpatches.Patch(color='red', label='median')
    green_label = mpatches.Patch(color='green', label='mean')
    plt.legend(handles=[green_label,red_label])
    plt.show()

def plot_t(list,list_mean,list_median):
    plt.plot(*zip(*list))
    plt.plot(*zip(*list_mean))
    plt.plot(*zip(*list_median))
    plt.title('CPU_time(#samples) in LinReg')
    plt.ylabel('Required CPU time')
    plt.xlabel('# of Samples')
    red_label = mpatches.Patch(color='red', label='median')
    green_label = mpatches.Patch(color='green', label='mean')
    plt.legend(handles=[green_label, red_label])
    plt.show()

errorList = []
timeList = []

error_mean = []
t_mean = []

error_median = []
t_median = []

def main(filename, iterations):
    # inside args, we have the dataset_file attribute, which contains the filename of the selected dataset
    x_train, y_train = load_dataset(filename)

    # split the dataset in N iterations and take the ceiling (so we don't leave any sample out)
    samples_per_iteration = int(np.ceil(len(y_train) / float(iterations)))

    for iteration in range(iterations):
        # increment the number of samples for this iteration
        # example: dataset of 100 samples, 10 iterations
        # first iteration: 10 samples; second iteration: 20 samples; etc.
        num_samples = samples_per_iteration + samples_per_iteration * iteration

        # sample the dataset
        x_sampled, y_sampled = get_subset(num_samples, x_train, y_train)
        # start time measure
        t0 = time.time()
        # compute the weights
        w = train(x_sampled, y_sampled)
        # compute required cpu-time for training
        t = time.time() - t0
        # append time elapsed into list
        timeList.append((len(y_sampled), t))
        current_mean = np.mean(zip(*timeList)[1])
        current_median = np.median(zip(*timeList)[1])
        t_mean.append((len(y_sampled), current_mean))
        t_median.append((len(y_sampled),current_median))
        # inference outputs the results given an input and some weights
        results = inference(x_sampled, w)
        # compute the error given the results and the ground truth
        error = compute_error(results, y_sampled)
        # append error data into list
        errorList.append((len(y_sampled), error))
        current_mean = np.mean(zip(*errorList)[1])
        current_median = np.median(zip(*errorList)[1])
        error_mean.append((len(y_sampled), current_mean))
        error_median.append((len(y_sampled),current_median))

        print("Num samples: " + str(len(y_sampled)) + ", Error: " + str(error) + ",Time: " + str(t))

        # Plot weights in two iterations
        if iteration % int((iterations-1) / 2.) == 0 and iteration != 0:
            plot_w(w)

        if len(y_sampled) == x_train.shape[0]:
            break

        # Plot error curve
    plot_err(errorList,error_mean,error_median)

    # Plot time curve
    plot_t(timeList,t_mean,t_median)

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    # the script has two arguments, --dataset_file and --iterations
    # usage: python linear_regression.py --dataset_file datasets/housing_scale_little_regression --iterations 100
    parser.add_argument("--dataset_file", dest="dataset_file")
    parser.add_argument("--iterations", dest="iterations", type=int)
    # parse_args() puts all the function arguments into the "args" object
    args = parser.parse_args()
    main(args.dataset_file, args.iterations)


#! /usr/bin/env python

# import core.config
import config
import logging
import os
import sys

from flask import Flask, send_file, request, jsonify
from flask_sqlalchemy import SQLAlchemy
from whitenoise import WhiteNoise
from datetime import date
from dateutil.parser import parse as parse_date
from dateutil.relativedelta import *

# init flask app
app = Flask(__name__)

# Custom or Default Config
if 'APP_SETTINGS' in os.environ:
    app.config.from_object(os.environ['APP_SETTINGS'])
else:
    app.config.from_object(config.Config)

# WhiteNoise for static file delivery
noiseapp = WhiteNoise(app, root='./static/')
db = SQLAlchemy(app)

# Turn on logging
app.logger.addHandler(logging.StreamHandler(sys.stdout))
app.logger.setLevel(logging.ERROR)

# EXTRA IMPORT
# need to suppress PEP8 E402 warning here
from core.model import User

####################
# BEGINNING ROUTES #
####################

@app.route('/')
def index():
    return send_file(open('index.html'))

@app.route('/anmelden', methods=['POST'])
def register():
    # should do more validation here!
    try:
        vorname = request.form['vorname']
        nachname = request.form['nachname']
        geb = parse_date(request.form['geburtstag'])

    except Exception as e:
        raise e
        return "Unable to parse form...", 500

    # check date in range
    if geb.year not in range(1890, 2020):
        return "Unrealistisches Geburtsjahr", 400

    # look for unique username in db
    requested_usrname = vorname + nachname + unicode(geb.date())
    usr = User.query.filter_by(username=requested_usrname).first()

    if usr is None:
        # register new user
        try:
            new_user = User(vorname, nachname, geb)
            db.session.add(new_user)
            db.session.commit()
        except:
            return "Unable to save " + unicode(new_user) + " to database...", 503
        # initial score of 0
        new_user.score = 0
        return new_user.make_json_data(), 201
    else:
        # return existing user info
        return usr.make_json_data(), 200

@app.route('/savegame', methods=['POST'])
def save_game():
    """Save a game state to the database and return the user it belongs to or 404"""
    data = request.get_json(force=True)

    username = data['username']
    usr = User.query.filter_by(username=username).first_or_404()

    # do i need to call json.dumps(data) ???
    usr.savegame = data

    if 'score' in data:
        score = data['score']
        usr.update_highscore(score)

    db.session.commit()
    return usr.make_json_data(), 200

@app.route('/loadgame/<int:userid>')
def load_game(userid):
    """Tries to load a savegame from the database and returns it or 404"""
    # this should be a dict of unicode strings...
    sg = User.query.get_or_404(userid).savegame
    if sg is None:
        return "Dieser User hat noch keinen Spielstand gespeichert. ", 404
    else:
        return jsonify(sg), 200

@app.route('/bestenliste')
def bestenliste():
    """Sends Top 10 List to client"""
    best_users = User.query.add_columns(User.vorname, User.nachname, User.geb, User.highscore).order_by(User.highscore.desc()).limit(10).all()
    res = []
    # get today once
    today = date.today()

    for usr in best_users:
        # get age
        age = relativedelta(today, usr.geb).years
        age = age if age > 0 else 0
        res.append({
            'vorname': usr.vorname,
            'nachname': usr.nachname,
            'alter': age,
            'highscore': usr.highscore
        })
    return jsonify(res), 200

###########################

# Do this with username instead, can be computed in JS
# @app.route('/retrieve/<userid>')
# def retrieve(userid):
#     return User.get_user(userid).vorname


from django.db.backends.mysql.base import DatabaseCreation as MySQLCreation
from django.db.backends.mysql.base import DatabaseIntrospection as MySQLIntrospection
from django.db.backends.mysql.base import DatabaseOperations as MySQLOperations
from django.db.backends.mysql.base import DatabaseWrapper as MySQLDatabaseWrapper

try:
    import MySQLdb as Database
except ImportError as e:
    from django.core.exceptions import ImproperlyConfigured
    raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)

class DatabaseOperations(MySQLOperations):
    compiler_module = "autotest.frontend.db.backends.afe.compiler"

class DatabaseWrapper(MySQLDatabaseWrapper):

    def __init__(self, *args, **kwargs):
        self.connection = None
        super(DatabaseWrapper, self).__init__(*args, **kwargs)
        self.creation = MySQLCreation(self)
        try:
            self.ops = DatabaseOperations()
        except TypeError:
            self.ops = DatabaseOperations(connection=kwargs.get('connection'))
        self.introspection = MySQLIntrospection(self)

    def _valid_connection(self):
        if self.connection is not None:
            if self.connection.open:
                try:
                    self.connection.ping()
                    return True
                except Database.DatabaseError:
                    self.connection.close()
                    self.connection = None
        return False


"""Module for downloading titlekeys.json from that key site."""

__author__ = "Death Mask Salesman"

import hashlib as _hashlib
import urllib.request as _urllib_request

def check_url(url):
    """
    Checks if the URL's SHA-256 matches that of that key site.

    :param url: the URL submitted by the user
    :type url:  str
    :returns:   True if the checksums match, False otherwise
    :rtype:     bool
    """

    keysite_sha256 = "c7c6e54cb2daf90953954392edda8bf82f46fe6d7b262e026f9b788e\
c449f52f"
    user_url_sha256 = _hashlib.sha256(url.encode("utf-8")).hexdigest()

    return user_url_sha256 == keysite_sha256

def get_database(url, path, official):
    """
    Downloads titlekeys.json from the submitted URL.

    :param url:      the URL to download from
    :param path:     the location where to place the downloaded file
    :param official: whether the URL is that key site's official one
    :type url:       str
    :type path:      str
    :type official:  bool
    :returns:        the amount of data saved
    :rtype:          int
    """

    url_suffix = "/json" if official else ""
    path_suffix = "/titlekeys.json"

    try:
      with _urllib_request.urlopen("{0}{1}".format(url, url_suffix)) as request:
           with open("{0}{1}".format(path, path_suffix), "wb") as f:
               saved_data = f.write(request.read())
    except:
        saved_data = 0

    return saved_data


from flask import Blueprint, render_template, request, jsonify
from rectpack import (
    skyline,
    guillotine,
    SORT_PERI,
    SORT_SSIDE,
    SORT_DIFF,
    SORT_RATIO,
    SORT_AREA,
    SORT_LSIDE,
    SORT_NONE,
)

from config import BaseConfig as conf
from app.controllers import RectPacker
from app.models import User
from app.utils import serve_pil_image
from app.logger import log

blueprint = Blueprint("calculator", __name__)

@blueprint.route("/calculator/<printshop>", methods=["GET"])
@blueprint.route("/calculator", methods=["GET"])
def calculator(printshop: str = None):
    moq = conf.MOQ
    moq_unit = conf.MOQ_UNIT
    cost = conf.COST
    cost_per = conf.COST_PER
    order_url = conf.ORDER_URL
    order_enabled = conf.ORDER_ENABLED
    cut_spacing = conf.CUT_SPACING
    metric_system = conf.METRIC_SYSTEM
    sheets = conf.SHEETS
    currency = conf.CURRENCY
    show_settings = True
    img = None
    contact_name = None
    contact_email = None
    contact_phone = None

    if not printshop:
        printshop = request.args.get("printshop")
    if printshop:
        user = User.query.filter(User.shop_name == printshop).first()
        if not user and printshop.isnumeric():
            user = User.query.get(printshop)

        if user:
            moq = user.moq
            moq_unit = "Sheet" if user.is_price_per_sheet else "SQR"
            cost = user.print_price
            cost_per = "Sheet" if user.is_price_per_sheet else "SQR"
            order_url = user.buy_url
            order_enabled = user.is_enabled_buy_btn
            cut_spacing = user.cut_spacing
            metric_system = user.metric_system.value
            sheets = user.sheets
            currency = conf.CURRENCY_NAME_TO_SYMBOL[user.currency.value]
            show_settings = False
            img = user.logo_img
            contact_name = user.contact_name
            contact_email = user.contact_email
            contact_phone = user.contact_phone

    return render_template(
        "calculator/index.html",
        config=conf,
        show_settings=show_settings,
        moq=moq,
        moq_unit=moq_unit,
        cost=cost,
        cost_per=cost_per,
        order_url=order_url,
        order_enabled=order_enabled,
        cut_spacing=cut_spacing,
        metric_system=metric_system,
        sheets=sheets,
        currency=currency,
        img=img,
        contact_name=contact_name,
        contact_email=contact_email,
        contact_phone=contact_phone,
    )

@blueprint.route("/calculate", methods=["POST"])
def calculate():
    data = request.json

    log(log.INFO, "Validate required data [%s]", data)

    if not data.get("bins"):
        return jsonify({"message": "Bin(s) not found"}), 400
    elif not data.get("rectangles"):
        return jsonify({"message": "Rectangle(s) not found"}), 400
    elif data.get("meticSystem") not in ["cm", "in"]:
        return jsonify({"message": "Invalid metic system"}), 400

    log(log.INFO, "Calculate and validate square_unit")
    metic_system = data.get("meticSystem")
    square_unit = conf.METRIC_TO_SQR_UNIT_VALUE.get(metic_system)
    if not square_unit:
        return jsonify({"message": "Incorrect metric system"}), 400

    print_price = data.get("printPrice") or 0
    moq = data.get("moqQty") or 0

    price_per = data.get("pricePer")
    blade_size = data["bladeSize"]

    first_bin = data["bins"][0]
    is_sizes_equals = first_bin["size"][0] == first_bin["size"][1]
    if is_sizes_equals:
        # to make width as larger side if sizes equals
        first_bin["size"][0] += 1

    log(log.INFO, "Init RectPacker")
    rect_packer = RectPacker(
        blade_size=blade_size / 2,
        is_sizes_equals=is_sizes_equals,
        square_unit=square_unit,
        price_per=price_per,
        print_price=print_price,
        moq=moq,
    )

    log(log.INFO, "Add bins to RectPacker instance")
    for bin in data["bins"]:
        for _ in range(bin["pics"]):
            width = bin["size"][0]
            height = bin["size"][1]
            rect_packer.add_bin(width, height)

    log(log.INFO, "Add rects to RectPacker instance")
    for rect in data["rectangles"]:
        for _ in range(rect["pics"]):
            width = rect["size"][0]
            height = rect["size"][1]
            rect_packer.add_rectangle(width, height)

    try:
        log(log.INFO, "Validate added data to RectPacker instance")
        rect_packer.validate_rectangles()
    except ValueError as e:
        return jsonify({"message": str(e)}), 400

    use_in_row = data.get("useInRow")

    results = {}
    log(log.INFO, "Start find the best packing algo")
    for pack_algo in [
        skyline.SkylineBl,
        skyline.SkylineBlWm,
        skyline.SkylineMwf,
        skyline.SkylineMwfl,
        skyline.SkylineMwfWm,
        skyline.SkylineMwflWm,
        guillotine.GuillotineBssfSas,
        guillotine.GuillotineBssfLas,
        guillotine.GuillotineBssfSlas,
        guillotine.GuillotineBssfLlas,
        guillotine.GuillotineBssfMaxas,
        guillotine.GuillotineBssfMinas,
        guillotine.GuillotineBlsfSas,
        guillotine.GuillotineBlsfLas,
        guillotine.GuillotineBlsfSlas,
        guillotine.GuillotineBlsfLlas,
        guillotine.GuillotineBlsfMaxas,
        guillotine.GuillotineBlsfMinas,
        guillotine.GuillotineBafSas,
        guillotine.GuillotineBafLas,
        guillotine.GuillotineBafSlas,
        guillotine.GuillotineBafLlas,
        guillotine.GuillotineBafMaxas,
        guillotine.GuillotineBafMinas,
    ]:
        for sort_algo in [
            SORT_NONE,
            SORT_RATIO,
            SORT_PERI,
            SORT_SSIDE,
            SORT_DIFF,
            SORT_AREA,
            SORT_LSIDE,
        ]:
            try:
                log(log.INFO, "Generate sesult using [%s] algo", pack_algo)
                rect_packer.reset(bins_in_row=1, reset_bin_sizes=True)
                rect_packer.pack(
                    use_sheet_in_row=use_in_row,
                    pack_algo=pack_algo,
                    sort_algo=sort_algo,
                )
                result = rect_packer.result
                if not result.get("placed_items"):
                    continue
                result["algo"] = str(pack_algo)

                used_area = result["used_area"]
                if not results.get(used_area):
                    results[used_area] = []
                results[used_area].append(result)
            except RecursionError:
                log(log.EXCEPTION, "RecursionError")

    log(log.INFO, "Find the best result")
    min_used_area = min(results.keys())

    best_results = results[min_used_area]
    best_results = sorted(
        best_results,
        key=lambda res: (res["wasted_area"], res["used_bins"]),
    )

    result = best_results[0]
    color_schema = {}
    log(log.INFO, "Generate images for results")
    for bin in result["bins"]:
        image = rect_packer.generate_image_for_bin(bin["bin"], color_schema)
        bin["image"] = serve_pil_image(image)
        del bin["bin"]
    del result["algo"]

    return jsonify(result)


import logging
import os

def log_main(log_content, level, file_name):
    cur_path = os.path.dirname(os.path.realpath(__file__))
    file = os.path.join(cur_path, '..', 'log', file_name)

    logger = logging.getLogger(file)
    logging.basicConfig(level=logging.DEBUG, filename=file, filemode='a',
                        format='%(asctime)s -%(filename)s -%(funcName)s -%(levelname)s - %(message)s')

    # Set handler and format

    if level == 'DEBUG':
        logger.debug(log_content)
    elif level == 'INFO':
        logger.info(log_content)
    elif level == 'WARNING':
        logger.warning(log_content)
    elif level == 'ERROR':
        logger.error(log_content)
    elif level == 'CRITICAL':
        logger.critical(log_content)

def etl_log(log_content, level):
    return log_main(log_content, level, 'etl_log.log')

if __name__ == "__main__":
    etl_log('t1', 'INFO')
    etl_log('t2', 'INFO')


from flask import Flask, render_template
from data import Articles
from pymongo import MongoClient
import pprint
import operator
client = MongoClient()

app = Flask(__name__)

db = client.mydb
btc = db.btc
eth = db.eth
eos = db.eos
ltc = db.ltc
rpl = db.rpl

Themes=[]

bt=btc.find_one()
bt=str(bt['topic'])
Themes.append(bt[2:-4])

et=eth.find_one()
et=str(et['topic'])
Themes.append(et[2:-4])

eo=eos.find_one()
eo=str(eo['topic'])
Themes.append(eo[2:-4])

lt=ltc.find_one()
lt=str(lt['topic'])
Themes.append(lt[2:-4])

rp=rpl.find_one()
rp=str(rp['topic'])
Themes.append(rp[2:-4])

print(Themes)

@app.route('/')
def index():
	return render_template('home.html')

@app.route('/about')
def about():
	return render_template('about.html')

@app.route('/themes')
def themes():
	return render_template('themes.html',Themes= Themes)

@app.route('/theme/<path:id>')
def article(id):
	if id == 'Bitcoin':
		id_col=btc
	elif id == 'Ethereum':
		id_col=eth
	elif id == 'EOS':
		id_col=eos
	elif id == 'Litecoin':
		id_col=ltc
	elif id == 'Ripple':
		id_col=rpl

	d = {}
	for elem in id_col.find():
		author_name=str(elem['author'])
		if (author_name == "['{username}']" or author_name == "[]"): continue
		d.update({author_name[2:-2]:id_col.find({"author":elem['author']}).count()})

	d1 = sorted(d.items(), key=operator.itemgetter(1), reverse=True)

	return render_template('btc_info.html',id=id,dictt=d1)

@app.route('/<path:tid>/user/<path:id>')
def userInfo(tid,id):
	if tid == 'Bitcoin':
		id_col=btc
	elif tid == 'Ethereum':
		id_col=eth
	elif tid == 'EOS':
		id_col=eos
	elif tid == 'Litecoin':
		id_col=ltc
	elif tid == 'Ripple':
		id_col=rpl

	return render_template('user_info.html',id=id,author_mess=id_col.find({'author':id}),user_m_count=id_col.find({'author':id}).count(),all_m_count=id_col.find().count())

if __name__ == '__main__':
	app.run(debug=True)


bytes = b'Aragon\xc3\xa9s'
words = bytes.decode('utf-8')
print(words)

# OK this works!
# b'' must tell python it's a byte we're assigning to the variable
# We then decode the bytes into a cooked_string
# Still not sure how to import a .txt file as anything other than a string

# Note that there is only one letter in hex because it has an accent


"""Proyecto #01 de la materia de Algoritmos"""

class CryptKicker():
    """Clase encargada de proporcionar metodos para desencriptar un codigo."""

    def __init__(self):
        """"""
        self.key = "El veloz murciélago hindú comía feliz cardillo y kiwi cuando la cigüeña tocaba el saxofón detrás del palenque de paja"
        self.key_length = len(self.key)

    def __del__(self):
        """"""
        pass

    def __same_characters(self, msg=""):
        """
        Compare characters to identify if msg can be the key (self.key).
        Variable 'msg' shall be of the same size of key to be compared.

        :param msg: String of the same size that key where to compare characters
        :return: Return False on failure
                 Return None on invalid message
                 Return True on success
        """
        if len(msg) == self.key_length:
            for msg_char, key_char in zip(msg, self.key):
                if msg_char.isalpha() != key_char.isalpha():
                    return False
            return True

        return None

    def __look_for_key(self, msg=""):
        """
        Loof for a specific string that is equivalent to the key (self.key).

        :param msg: String with the message where to look for.
        :return: Return None on failure.
                 Return a string with the equivalent key in message on succes.
        """
        msg_length = len(msg)
        for str_pos in range(0, msg_length):
            max_length = str_pos + self.key_length
            key = msg[str_pos: max_length]
            if self.__same_characters(key):
                # Does exist a next character?
                next_char = msg[max_length] if max_length < msg_length else ""
                # Does exist a before character?
                before_char = msg[str_pos - 1] if str_pos - 1 >= 0 else ""
                # If one or both exists, they shall be a white space ' '
                if (next_char == "" or next_char == " ") and\
                        (before_char == "" or before_char == " "):
                    return key

        return None

    def __match_characters(self, msg=""):
        """
        Compare characters from key crypted with real meaning of key (self.key) to get actual
        value of each character in the message.

        param msg_key: String with the message with you want to compare key

        return: None for error, Dictionary with matches of characters found
        """
        if len(msg) != self.key_length:
            return None
        match_characters = {k: v for k, v in zip(msg, self.key)}

        return match_characters

    def __replace_chars(self, msg, match_characters):
        """
        Replace characters from message with found in dictionary(match_characters).
        It substitute one at the time, to avoid substitute a valid character.

        param msg: String with the characters to Replace
        param match_characters: Dictionary with keys found from self.key and with its real value

        return: None for error, String with the values of dictionary insted of keys
        """
        if isinstance(match_characters, dict) is False:
            return None

        msg_decrypted = msg[:]
        tem_msg = list()
        for char in msg_decrypted:
            if char in match_characters.keys():
                tem_msg.append(match_characters[char])
            else:
                tem_msg.append(char)
        msg_decrypted = "".join(tem_msg)

        return msg_decrypted

    def decrypt(self, msg):
        """
        Method to discover the meaning of an encrypted message.

        :param msg: String with the encrypted message
        :return: Return 'NO SE ENCONTRO SOLUCION' on failure.
                 Return string with the decrypted message on success.
                 Return None on invalid message
        """
        if isinstance(msg, str) is False:
            return None

        # Get piece of msg that represents the key
        msg_key = self.__look_for_key(msg)
        if msg_key is None:
            return "NO SE ENCONTRO SOLUCION"
        print("\n - Message key: {}".format(msg_key))
        print(" - Message key: {}\n".format(self.key))

        # Get dictionary with meaning of crypted characters
        match_characters = self.__match_characters(msg_key)
        if match_characters is None:
            return "NO SE ENCONTRO SOLUCION"
        print("\n - Dictionary: {}".format(match_characters))

        # Remove msg_key and white spaces
        msg = msg.replace(msg_key, "")
        msg = msg.replace("  ", " ")
        msg = msg.strip()

        # Substitute characters in messages based on dictionary
        msg_decrypted = self.__replace_chars(msg, match_characters)
        if msg_decrypted is None:
            return "NO SE ENCONTRO SOLUCION"
        print("\n - Message decrypted: {}\n\n".format(msg_decrypted))
        return msg_decrypted

if __name__ == "__main__":
    CK = CryptKicker()
    cases = int(input())
    for n in range(cases):
        paragraph, continue_cycle, msg_saved = list(), True, False
        while continue_cycle:
            line = input()
            if line != "":  # Ignore empty lines
                paragraph.append(line)
                msg_saved = True
            elif msg_saved:
                continue_cycle = False
        msg = " ".join(paragraph)
        CK.decrypt(msg)


import torch.nn as nn

class DataParallelModel(nn.Module):

    def __init__(self):
        super().__init__()
        self.block1 = nn.Linear(10, 20)
        self.block2 = nn.Linear(20, 20)
        self.block2 = nn.DataParallel(self.block2)
        self.block3 = nn.Linear(20, 20)

    def forward(self, x):
        x = self.block1(x)
        x = self.block2(x)
        x = self.block3(x)
        return x

def data_parallel(module, input, device_ids, output_device=None):
    if not device_ids:
        return module(input)

    if output_device is None:
        output_device = device_ids[0]

    replicas = nn.parallel.replicate(module, device_ids)
    inputs = nn.parallel.scatter(input, device_ids)
    replicas = replicas[:len(inputs)]
    outputs = nn.parallel.parallel_apply(replicas, inputs)
    return nn.parallel.gather(outputs, output_device)

class DistributedModel(nn.Module):

    def __init__(self):
        super().__init__(
            embedding=nn.Embedding(1000, 10),
            rnn=nn.Linear(10, 10).cuda(0))

    def forward(self, x):
        x = self.embedding(x)
        x = x.cuda(0)
        x = self.rnn(x)
        return x


"""
@autho: Thomas Neuer (tneuer)

In this module, different network types are implemented with tensorflow, including:
    1) a feed forward network, with possibility on Drop out
    2) an autoencoder where every layer can be specified (tanh works, but then the data needs to be downscaled)
    3) an autoencoder where the last layer is the identity (instead of tanh for example) in order to cover
    the whole range of real values.
"""

import tensorflow as tf
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

def feed_forward_nn(data, nr_features, n_classes, nodes, activations, DropOut, BN, train_phase):
    """Implement a feed forward neural network.

    The simplest but also often an effective neural network model. A DropOut can be added
    in order to improve generalisation. Only the hidden layers need to be specified.
    There are also many lines of code which are fed into tensorboard which can then be used
    to further analyze the network performnce. Inclued are:
        * the mean of the biases and weights in every layer
        * the histogram and distribution of weights and biases in every layer

    Parameters
    ----------
    data : np.matrix
        feature matrix or batches, where one row is an observation and a column is a feature.

    nr_features : int
        number of features.

    n_classes : int
        number of classes (output groups).

    nodes : list
        The length of this list determines the number of hidden layers (only those need to be specified).
        Every element in this list needs to be an integer which determines the number of nodes per layer.

    activations : list
        A list of activation functions. Needs to have the same length as 'nodes'. All regular tf.train
        activation functions are supported.

    DropOut : list
        A list of floats between 0 and 1. Determines the keep probability per layer. This list needs to have
        the same length as 'nodes'.

    BN : boolean
        True, then batch normalisation is applied

    train_phase : boolean
        Only considered if batch normalisation is true. Rebuild network for test phase (train_phase = False)

    Returns
    -------
    curr_ipt : np.matrix
        Entries of last layer WITHOUT activation function (often softmax).
    """

    depth = len(nodes) + 1
    nodes.insert(0, nr_features)
    nodes.append(n_classes)
    activation = ["tf.nn.{}".format(actv) for actv in activations]
    weights = {}
    biases = {}

    for i in range(depth):
        weights["layer{}".format(i)] = tf.Variable(tf.random_normal([nodes[i], nodes[i+1]], stddev=np.sqrt(4/(nodes[i]+nodes[i+1])) ))

        biases["layer{}".format(i)] = tf.Variable(tf.random_normal([nodes[i+1]]))

        tf.summary.histogram("Weights{}".format(i), weights["layer{}".format(i)])
        mean_w = tf.reduce_mean(weights["layer{}".format(i)])
        tf.summary.scalar("mean_weights{}".format(i), mean_w)
        tf.summary.histogram("Biases{}".format(i), biases["layer{}".format(i)])
        mean_b = tf.reduce_mean(biases["layer{}".format(i)])
        tf.summary.scalar("mean_biases{}".format(i), mean_b)
    del nodes[0]; del nodes[-1]

    curr_ipt = data
    for i in range(depth-1):
        with tf.name_scope("Layer{}".format(i)):
            layer = tf.add(tf.matmul(curr_ipt, weights["layer{}".format(i)]), biases["layer{}".format(i)])
            if BN:
                try:
                    layer = tf.contrib.layers.batch_norm(layer, center=True, scale=True, is_training=train_phase, epsilon =0.01)
                except ValueError:
                    layer = tf.contrib.layers.batch_norm(layer, center=True, scale=True, is_training=train_phase, reuse = True, epsilon=0.01)

            curr_ipt = eval(activation[i])(layer)

            if DropOut != None:
                tf.nn.dropout(curr_ipt, DropOut[i])

    with tf.name_scope("Layer{}".format(depth-1)):
        curr_ipt = tf.matmul(curr_ipt, weights["layer{}".format(depth-1)]) + biases["layer{}".format(depth-1)]
    return curr_ipt

def autoencoder(data, nr_features, nodes, activations, DropOut, lastLayer = "ID"):
    """Implement an autoencoder neural network.

    A form of neural networks which tries to map the input via a 1-1 transformation on itself.
    First a dimensionality reduction is tried to obtain. the last layer should have two nodes,
    in order to be presentable in a 2D plot. Only the architecture of the encoder needs to
    be specified.
    There are also many lines of code which are fed into tensorboard which can then be used
    to further analyze the network performnce. Inclued are:
        * the mean of the biases and weights in every layer
        * the histogram and distribution of weights and biases in every layer

    Parameters
    ----------
    data : np.matrix
        feature matrix or batches, where one row is an observation and a column is a feature.

    nr_features : int
        number of features.

    nodes : list
        The length of this list determines the number of hidden layers (only those need to be specified).
        Every element in this list needs to be an integer which determines the number of nodes per layer.

    activations : list
        A list of activation functions. Needs to have the same length as 'nodes'. All regular tf.train
        activation functions are supported.

    DropOut : list
        A list of floats between 0 and 1. Determines the keep probability per layer. This list needs to have
        the same length as 'nodes'.

    lastLayer : str ["ID", else "user"]
        Determines which activation function should be used in the last layer. If 'ID' is chosen, the last
        layer performs an identity operation. Else the same activation function as in the first layer is applied.

    Returns
    -------
    curr_ipt : np.matrix
        Entries of last layer WITHOUT activation function (often softmax).
    """

    depth = len(nodes)
    nodes.insert(0, nr_features)
    weights = {}
    biases = {}

    for i in range(depth):
        weights["encoder_layer{}".format(i)] = tf.Variable(tf.random_normal([nodes[i], nodes[i+1]], stddev=np.sqrt(4/(nodes[i]+nodes[i+1])) ))
        weights["decoder_layer{}".format(depth-i-1)] = tf.Variable(tf.random_normal([nodes[i+1], nodes[i]], stddev=np.sqrt(4/(nodes[i]+nodes[i+1])) ))

        biases["encoder_layer{}".format(i)] = tf.Variable(tf.random_normal([nodes[i+1]]))
        biases["decoder_layer{}".format(depth-i-1)] = tf.Variable(tf.random_normal([nodes[i]]))

        tf.summary.histogram("EncoderWeights{}".format(i), weights["encoder_layer{}".format(i)])
        mean_w = tf.reduce_mean(weights["encoder_layer{}".format(i)])
        tf.summary.scalar("mean_EncoderWeights{}".format(i), mean_w)
        tf.summary.histogram("EncoderBiases{}".format(i), biases["encoder_layer{}".format(i)])
        mean_b = tf.reduce_mean(biases["encoder_layer{}".format(i)])
        tf.summary.scalar("mean_EncoderBiases{}".format(i), mean_b)

        tf.summary.histogram("DecoderWeights{}".format(depth-i-1), weights["decoder_layer{}".format(depth-i-1)])
        mean_w = tf.reduce_mean(weights["decoder_layer{}".format(depth-i-1)])
        tf.summary.scalar("mean_DecoderWeights{}".format(depth-i-1), mean_w)
        tf.summary.histogram("DecoderBiases{}".format(depth-i-1), biases["decoder_layer{}".format(depth-i-1)])
        mean_b = tf.reduce_mean(biases["decoder_layer{}".format(depth-i-1)])
        tf.summary.scalar("mean_DecoderBiases{}".format(depth-i-1), mean_b)
    del nodes[0]

    activation = ["tf.nn.{}".format(actv) for actv in activations]
    def encoder(x):
        curr_ipt = x
        for i in range(depth):
            layer = tf.add(tf.matmul(curr_ipt, weights["encoder_layer{}".format(i)]), biases["encoder_layer{}".format(i)])
            curr_ipt = eval(activation[i])(layer)

            if DropOut != None:
                tf.nn.dropout(curr_ipt, DropOut[i])

        return curr_ipt

    def decoder(x):
        curr_ipt = x
        ddepth = depth-1 if lastLayer == "ID" else depth
        for i in range(ddepth):
            layer = tf.add(tf.matmul(curr_ipt, weights["decoder_layer{}".format(i)]), biases["decoder_layer{}".format(i)])
            curr_ipt = eval(activation[depth-i-1])(layer)

            if DropOut != None:
                tf.nn.dropout(curr_ipt, DropOut[i])

        if lastLayer == "ID":
            layer = tf.add(tf.matmul(curr_ipt, weights["decoder_layer{}".format(ddepth)]), biases["decoder_layer{}".format(ddepth)])
            curr_ipt = layer

            if DropOut != None:
                tf.nn.dropout(curr_ipt, DropOut[ddepth])
        return curr_ipt

    encoder_run = encoder(data)
    decoder_run = decoder(encoder_run)

    output = decoder_run

    return output, encoder_run


# coding: utf-8
from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.conf import settings

urlpatterns = patterns(
    '',
    # Examples:
    # url(r'^$', 'estofadora.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),
    url(r'^cliente/', include('estofadora.client.urls', namespace='client')),
    url(r'^item/', include('estofadora.item.urls', namespace='item')),
    url(r'^login/', include('estofadora.login.urls', namespace='login')),
    url(
        r'^relatorios/',
        include('estofadora.statement.urls', namespace='statement')
    ),
    url(r'^conta/', include('estofadora.bills.urls', namespace='bills')),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^', include('estofadora.core.urls', namespace='core')),
)

if settings.DEBUG:
    from django.conf.urls.static import static
    urlpatterns += static(
        settings.MEDIA_URL, document_root=settings.MEDIA_ROOT
    )


from flask import Flask, render_template, request, redirect
import message
import helpers
import os

#creating an object of Flassk class to create the app
app = Flask(__name__)
# os.environ['GOOGLE_APPLICATION_CREDENTIALS']='./static/jsonkey.json'

@app.route('/')
def index():
    if request.method == "GET":
        return render_template('index.html')

@app.route('/features', methods = ["GET", "POST"])
def features():
    if request.method == "GET":
        return render_template('features.html')

    else:
        phone_number = request.form["phone_number"]
        print(phone_number)
        print(type(phone_number))
        coords = request.form["coords"]
        coordinates = coords.split(',')
        try:
            os.remove('static/AfterImage.png')
            os.remove('static/BeforeImage.png')
            os.remove('static/outputAfter.png')
            os.remove('static/outputBefore.png')
            os.remove('static/result.png')
        except:
            pass
        helpers.getBeforeAndAfterImages(coordinates)
        if phone_number != "":
            message.send_message(phone_number, coords)
        return redirect('/contact')

@app.route('/contact')
def contact():
    return render_template('contact.html')

@app.route('/pricing') #by default is GET request
def leaderboard():
    return render_template('pricing.html')

if __name__ == '__main__':
    #./ngrok http 3000
    app.run(port=3000) #debug = True //in order to not run every time


#!/usr/bin/python3
def print_matrix_integer(matrix=[[]]):
    state = False
    for col in range(0, len(matrix)):
        for row in range(0, len(matrix[col])):
            if (row != len(matrix[col]) - 1):
                print("{:d}".format(matrix[col][row]), end=" ")
            else:
                print("{:d}".format(matrix[col][row]))
            state = True
    if not state:
        print("")


import pandas as pd
import requests
import zipfile
import io
import re
import os

def get_data_from_url(url, name):
    url=url.strip()
    url_data = requests.get(url)
    extension = url[-4:]
    print(extension)
    if extension == '.zip':
        z = zipfile.ZipFile(io.BytesIO(url_data.content))
        directory_name = 'data/'+name.replace(' ','_')
        os.makedirs(directory_name)
        z.extractall(directory_name)
    elif extension == '.csv':
        try:
            df = pd.read_csv(io.StringIO(url_data.content.decode('utf-8')))
        except:
            df = pd.read_csv(io.StringIO(url_data.content.decode('latin-1')))
        name = name.replace('/', '_')
        output_path = 'data/'+name.lower().replace(' ','_')+extension
        df.to_csv(output_path)
    else:
        file_name= 'data/'+name.lower().replace(' ','_')+'.xls'
        output = open(file_name, 'wb')
        output.write(url_data.content)
        output.close()

def get_url(bulk_download):
    if r'(CSV)' in bulk_download:
        output =re.search(r'(CSV).*(https*://.*[.zip|.csv])',bulk_download).group()
        output=re.search(r'https*://.*[zip|xls|xlsx|csv](?!;)',output).group()
        output=output.split(';')
        output=output[0]
        print('csv')
    elif r'(Excel)' in bulk_download:
        output=re.search(r'(Excel).*(https*://.*[.zip|.xls|.xlsx])',bulk_download).group()
        output=re.search(r'https*://.*[.zip|.xls|.xlsx|.csv]',output).group()
        output=output.split(';')
        output=output[0]
        print('xls')
    else:
        output=re.search(r'https*://.*[.zip|.xls|.xlsx|.csv]',bulk_download).group()
        output=output.split(';')
        output=output[0]
        print('random')
    unwanted = ['=csv','=xml','=excel','=zip']
    for i in unwanted:
        if i in output.lower():
            output=output.replace(i,'')
    return output

catalog = pd.read_excel('world_bank_data_catalog.xls')
current =catalog[catalog['Last Revision Date'] =='Current']
for index, series in enumerate(current.iterrows()):
    name = series[1]['Name']
    link = series[1]['Bulk Download']
    download_link = get_url(link)
    print(index, series[0],download_link,name) ##prints index to track links are faulty
    get_data_from_url(download_link, name)

tmp_catalog = catalog[catalog['Last Revision Date'] !='Current']
index_2017 = [i for i in tmp_catalog.index if tmp_catalog['Last Revision Date'] [i].year==2017]
updated_2017 = catalog.iloc[index_2017]
links_2017 = updated_2017[['Name','Bulk Download']].dropna()
links_2017.drop(66, inplace=True) ##dropping data i don't want
links_2017.drop(153, inplace=True)##dropping data i don't want

for index, series in enumerate(links_2017.iterrows()):
    name = series[1]['Name']
    link = series[1]['Bulk Download']
    download_link = get_url(link)
    print(index, series[0],download_link,name) ##prints index to track links are faulty
    get_data_from_url(download_link, name)


import collections
import numpy as np
from random import randint

class Greedy:
    def __init__(self, cities, initial_city=None):
        self.cities = cities
        self.initial_city = initial_city
        self.path = []
        self.total_lenght = 0.0

    def validateTSP(self, path):
        return [item for item, count in collections.Counter(path).items() if count > 1]

    def run(self):
        stats = []
        if(self.initial_city == None):
            initial_city = randint(0, len(self.cities) -1)
            self.path.append(initial_city)
        else:
            self.path.append(self.initial_city)

        for i in range(0, len(self.cities)-1):
            city_lenght = min(k for k in self.cities[self.initial_city] if k > 0)
            index = self.cities[self.initial_city].index(city_lenght)
            if index in self.path:
                while index in self.path:
                    self.cities[self.initial_city][index] = 0
                    city_lenght = min(k for k in self.cities[self.initial_city] if k > 0)
                    index = self.cities[self.initial_city].index(city_lenght)

            self.total_lenght += city_lenght
            self.path.append(index)
            stats.append(Greedy.pull_stats(self.total_lenght, i))

        return self.path, self.total_lenght, stats

    @staticmethod
    def pull_stats(cities, iteration=1):
        #fitnesses = [ individual.fitness.values[0] for individual in population ]
        return {
        'i': iteration,
        'mu': np.mean(cities),
        'std': np.std(cities),
        'max': np.max(cities),
        'min': np.min(cities)
        }


import json

repetidos = [1,2,3,"1","2","3",3,4,5]
r = [1,"5",2,"3"]
d_str = '{"valor":125.3,"codigo":123}'

#Parte 1
lista_1 = list(set(map(str,repetidos)))
#Parte 2
lista_2 = list(set (repetidos) and set(r))
#Parte 3
diccionario = json.loads(d_str)