import logging
from functools import partial, reduce

import numpy as np
import pandas as pd
import pypsa
import scipy as sp
from _helpers import configure_logging, update_p_nom_max
from add_electricity import load_costs
from cluster_network import cluster_regions, clustering_for_n_clusters
from pypsa.clustering.spatial import (
    aggregateoneport,
    busmap_by_stubs,
    get_clustering_from_busmap,
)
from pypsa.io import import_components_from_dataframe, import_series_from_dataframe
from scipy.sparse.csgraph import connected_components, dijkstra

logger = logging.getLogger(__name__)

def simplify_network_to_380(n):
    """
    Fix all lines to a voltage level of 380 kV and remove all transformers.

    The function preserves the transmission capacity for each line while
    updating its voltage level, line type and number of parallel bundles
    (num_parallel).

    Transformers are removed and connected components are moved from
    their starting bus to their ending bus. The corresponding starting
    buses are removed as well.
    """
    logger.info("Mapping all network lines onto a single 380kV layer")

    n.buses["v_nom"] = 380.0

    (linetype_380,) = n.lines.loc[n.lines.v_nom == 380.0, "type"].unique()
    n.lines["type"] = linetype_380
    n.lines["v_nom"] = 380
    n.lines["i_nom"] = n.line_types.i_nom[linetype_380]
    n.lines["num_parallel"] = n.lines.eval("s_nom / (sqrt(3) * v_nom * i_nom)")

    trafo_map = pd.Series(n.transformers.bus1.values, n.transformers.bus0.values)
    trafo_map = trafo_map[~trafo_map.index.duplicated(keep="first")]
    several_trafo_b = trafo_map.isin(trafo_map.index)
    trafo_map[several_trafo_b] = trafo_map[several_trafo_b].map(trafo_map)
    missing_buses_i = n.buses.index.difference(trafo_map.index)
    missing = pd.Series(missing_buses_i, missing_buses_i)
    trafo_map = pd.concat([trafo_map, missing])

    for c in n.one_port_components | n.branch_components:
        df = n.df(c)
        for col in df.columns:
            if col.startswith("bus"):
                df[col] = df[col].map(trafo_map)

    n.mremove("Transformer", n.transformers.index)
    n.mremove("Bus", n.buses.index.difference(trafo_map))

    return n, trafo_map

def _prepare_connection_costs_per_link(n, costs, renewable_carriers, length_factor):
    if n.links.empty:
        return {}

    return {
        tech: (
            n.links.length
            * length_factor
            * (
                n.links.underwater_fraction
                * costs.at[tech + "-connection-submarine", "capital_cost"]
                + (1.0 - n.links.underwater_fraction)
                * costs.at[tech + "-connection-underground", "capital_cost"]
            )
        )
        for tech in renewable_carriers
        if tech.startswith("offwind")
    }

def _compute_connection_costs_to_bus(
    n,
    busmap,
    costs,
    renewable_carriers,
    length_factor,
    connection_costs_per_link=None,
    buses=None,
):
    if connection_costs_per_link is None:
        connection_costs_per_link = _prepare_connection_costs_per_link(
            n, costs, renewable_carriers, length_factor
        )

    if buses is None:
        buses = busmap.index[busmap.index != busmap.values]

    connection_costs_to_bus = pd.DataFrame(index=buses)

    for tech in connection_costs_per_link:
        adj = n.adjacency_matrix(
            weights=pd.concat(
                dict(
                    Link=connection_costs_per_link[tech].reindex(n.links.index),
                    Line=pd.Series(0.0, n.lines.index),
                )
            )
        )

        costs_between_buses = dijkstra(
            adj, directed=False, indices=n.buses.index.get_indexer(buses)
        )
        connection_costs_to_bus[tech] = costs_between_buses[
            np.arange(len(buses)), n.buses.index.get_indexer(busmap.loc[buses])
        ]

    return connection_costs_to_bus

def _adjust_capital_costs_using_connection_costs(n, connection_costs_to_bus, output):
    connection_costs = {}
    for tech in connection_costs_to_bus:
        tech_b = n.generators.carrier == tech
        costs = (
            n.generators.loc[tech_b, "bus"]
            .map(connection_costs_to_bus[tech])
            .loc[lambda s: s > 0]
        )
        if not costs.empty:
            n.generators.loc[costs.index, "capital_cost"] += costs
            logger.info(
                "Displacing {} generator(s) and adding connection costs to capital_costs: {} ".format(
                    tech,
                    ", ".join(
                        "{:.0f} Eur/MW/a for `{}`".format(d, b)
                        for b, d in costs.items()
                    ),
                )
            )
            connection_costs[tech] = costs
    pd.DataFrame(connection_costs).to_csv(output.connection_costs)

def _aggregate_and_move_components(
    n,
    busmap,
    connection_costs_to_bus,
    output,
    aggregate_one_ports={"Load", "StorageUnit"},
    aggregation_strategies=dict(),
    exclude_carriers=None,
):
    def replace_components(n, c, df, pnl):
        n.mremove(c, n.df(c).index)

        import_components_from_dataframe(n, df, c)
        for attr, df in pnl.items():
            if not df.empty:
                import_series_from_dataframe(n, df, c, attr)

    _adjust_capital_costs_using_connection_costs(n, connection_costs_to_bus, output)

    generator_strategies = aggregation_strategies["generators"]

    carriers = set(n.generators.carrier) - set(exclude_carriers)
    generators, generators_pnl = aggregateoneport(
        n,
        busmap,
        "Generator",
        carriers=carriers,
        custom_strategies=generator_strategies,
    )

    replace_components(n, "Generator", generators, generators_pnl)

    for one_port in aggregate_one_ports:
        df, pnl = aggregateoneport(n, busmap, component=one_port)
        replace_components(n, one_port, df, pnl)

    buses_to_del = n.buses.index.difference(busmap)
    n.mremove("Bus", buses_to_del)
    for c in n.branch_components:
        df = n.df(c)
        n.mremove(c, df.index[df.bus0.isin(buses_to_del) | df.bus1.isin(buses_to_del)])

def simplify_links(
    n,
    costs,
    renewables,
    length_factor,
    p_max_pu,
    exclude_carriers,
    output,
    aggregation_strategies=dict(),
):
    ## Complex multi-node links are folded into end-points
    logger.info("Simplifying connected link components")

    if n.links.empty:
        return n, n.buses.index.to_series()

    # Determine connected link components, ignore all links but DC
    adjacency_matrix = n.adjacency_matrix(
        branch_components=["Link"],
        weights=dict(Link=(n.links.carrier == "DC").astype(float)),
    )

    _, labels = connected_components(adjacency_matrix, directed=False)
    labels = pd.Series(labels, n.buses.index)

    G = n.graph()

    def split_links(nodes):
        nodes = frozenset(nodes)

        seen = set()
        supernodes = {m for m in nodes if len(G.adj[m]) > 2 or (set(G.adj[m]) - nodes)}

        for u in supernodes:
            for m, ls in G.adj[u].items():
                if m not in nodes or m in seen:
                    continue

                buses = [u, m]
                links = [list(ls)]  # [name for name in ls]]

                while m not in (supernodes | seen):
                    seen.add(m)
                    for m2, ls in G.adj[m].items():
                        if m2 in seen or m2 == u:
                            continue
                        buses.append(m2)
                        links.append(list(ls))  # [name for name in ls])
                        break
                    else:
                        # stub
                        break
                    m = m2
                if m != u:
                    yield pd.Index((u, m)), buses, links
            seen.add(u)

    busmap = n.buses.index.to_series()

    connection_costs_per_link = _prepare_connection_costs_per_link(
        n, costs, renewables, length_factor
    )
    connection_costs_to_bus = pd.DataFrame(
        0.0, index=n.buses.index, columns=list(connection_costs_per_link)
    )

    for lbl in labels.value_counts().loc[lambda s: s > 2].index:
        for b, buses, links in split_links(labels.index[labels == lbl]):
            if len(buses) <= 2:
                continue

            logger.debug("nodes = {}".format(labels.index[labels == lbl]))
            logger.debug("b = {}\nbuses = {}\nlinks = {}".format(b, buses, links))

            m = sp.spatial.distance_matrix(
                n.buses.loc[b, ["x", "y"]], n.buses.loc[buses[1:-1], ["x", "y"]]
            )
            busmap.loc[buses] = b[np.r_[0, m.argmin(axis=0), 1]]
            connection_costs_to_bus.loc[buses] += _compute_connection_costs_to_bus(
                n,
                busmap,
                costs,
                renewables,
                length_factor,
                connection_costs_per_link,
                buses,
            )

            all_links = [i for _, i in sum(links, [])]

            lengths = n.links.loc[all_links, "length"]
            name = lengths.idxmax() + "+{}".format(len(links) - 1)
            params = dict(
                carrier="DC",
                bus0=b[0],
                bus1=b[1],
                length=sum(
                    n.links.loc[[i for _, i in l], "length"].mean() for l in links
                ),
                p_nom=min(n.links.loc[[i for _, i in l], "p_nom"].sum() for l in links),
                underwater_fraction=sum(
                    lengths
                    / lengths.sum()
                    * n.links.loc[all_links, "underwater_fraction"]
                ),
                p_max_pu=p_max_pu,
                p_min_pu=-p_max_pu,
                underground=False,
                under_construction=False,
            )

            logger.info(
                "Joining the links {} connecting the buses {} to simple link {}".format(
                    ", ".join(all_links), ", ".join(buses), name
                )
            )

            n.mremove("Link", all_links)

            static_attrs = n.components["Link"]["attrs"].loc[lambda df: df.static]
            for attr, default in static_attrs.default.items():
                params.setdefault(attr, default)
            n.links.loc[name] = pd.Series(params)

            # n.add("Link", **params)

    logger.debug("Collecting all components using the busmap")

    _aggregate_and_move_components(
        n,
        busmap,
        connection_costs_to_bus,
        output,
        aggregation_strategies=aggregation_strategies,
        exclude_carriers=exclude_carriers,
    )
    return n, busmap

def remove_stubs(
    n,
    costs,
    renewable_carriers,
    length_factor,
    simplify_network,
    output,
    aggregation_strategies=dict(),
):
    logger.info("Removing stubs")

    across_borders = simplify_network["remove_stubs_across_borders"]
    matching_attrs = [] if across_borders else ["country"]
    busmap = busmap_by_stubs(n, matching_attrs)

    connection_costs_to_bus = _compute_connection_costs_to_bus(
        n, busmap, costs, renewable_carriers, length_factor
    )

    _aggregate_and_move_components(
        n,
        busmap,
        connection_costs_to_bus,
        output,
        aggregation_strategies=aggregation_strategies,
        exclude_carriers=simplify_network["exclude_carriers"],
    )

    return n, busmap

def aggregate_to_substations(n, aggregation_strategies=dict(), buses_i=None):
    # can be used to aggregate a selection of buses to electrically closest neighbors
    # if no buses are given, nodes that are no substations or without offshore connection are aggregated

    if buses_i is None:
        logger.info(
            "Aggregating buses that are no substations or have no valid offshore connection"
        )
        buses_i = list(set(n.buses.index) - set(n.generators.bus) - set(n.loads.bus))

    weight = pd.concat(
        {
            "Line": n.lines.length / n.lines.s_nom.clip(1e-3),
            "Link": n.links.length / n.links.p_nom.clip(1e-3),
        }
    )

    adj = n.adjacency_matrix(branch_components=["Line", "Link"], weights=weight)

    bus_indexer = n.buses.index.get_indexer(buses_i)
    dist = pd.DataFrame(
        dijkstra(adj, directed=False, indices=bus_indexer), buses_i, n.buses.index
    )

    dist[
        buses_i
    ] = np.inf  # bus in buses_i should not be assigned to different bus in buses_i

    for c in n.buses.country.unique():
        incountry_b = n.buses.country == c
        dist.loc[incountry_b, ~incountry_b] = np.inf

    busmap = n.buses.index.to_series()
    busmap.loc[buses_i] = dist.idxmin(1)

    line_strategies = aggregation_strategies.get("lines", dict())
    generator_strategies = aggregation_strategies.get("generators", dict())
    one_port_strategies = aggregation_strategies.get("one_ports", dict())

    clustering = get_clustering_from_busmap(
        n,
        busmap,
        aggregate_generators_weighted=True,
        aggregate_generators_carriers=None,
        aggregate_one_ports=["Load", "StorageUnit"],
        line_length_factor=1.0,
        line_strategies=line_strategies,
        generator_strategies=generator_strategies,
        one_port_strategies=one_port_strategies,
        scale_link_capital_costs=False,
    )
    return clustering.network, busmap

def cluster(
    n,
    n_clusters,
    focus_weights,
    solver_name,
    algorithm="hac",
    feature=None,
    aggregation_strategies=dict(),
):
    logger.info(f"Clustering to {n_clusters} buses")

    clustering = clustering_for_n_clusters(
        n,
        n_clusters,
        custom_busmap=False,
        aggregation_strategies=aggregation_strategies,
        solver_name=solver_name,
        algorithm=algorithm,
        feature=feature,
        focus_weights=focus_weights,
    )

    return clustering.network, clustering.busmap

if __name__ == "__main__":
    if "snakemake" not in globals():
        from _helpers import mock_snakemake

        snakemake = mock_snakemake("simplify_network", simpl="")
    configure_logging(snakemake)

    params = snakemake.params
    solver_name = snakemake.config["solving"]["solver"]["name"]

    n = pypsa.Network(snakemake.input.network)
    Nyears = n.snapshot_weightings.objective.sum() / 8760

    n, trafo_map = simplify_network_to_380(n)

    technology_costs = load_costs(
        snakemake.input.tech_costs,
        params.costs,
        params.max_hours,
        Nyears,
    )

    n, simplify_links_map = simplify_links(
        n,
        technology_costs,
        params.renewable_carriers,
        params.length_factor,
        params.p_max_pu,
        params.simplify_network["exclude_carriers"],
        snakemake.output,
        params.aggregation_strategies,
    )

    busmaps = [trafo_map, simplify_links_map]

    if params.simplify_network["remove_stubs"]:
        n, stub_map = remove_stubs(
            n,
            technology_costs,
            params.renewable_carriers,
            params.length_factor,
            params.simplify_network,
            snakemake.output,
            aggregation_strategies=params.aggregation_strategies,
        )
        busmaps.append(stub_map)

    if params.simplify_network["to_substations"]:
        n, substation_map = aggregate_to_substations(n, params.aggregation_strategies)
        busmaps.append(substation_map)

    # treatment of outliers (nodes without a profile for considered carrier):
    # all nodes that have no profile of the given carrier are being aggregated to closest neighbor
    if params.simplify_network["algorithm"] == "hac":
        carriers = params.simplify_network["feature"].split("-")[0].split("+")
        for carrier in carriers:
            buses_i = list(
                set(n.buses.index) - set(n.generators.query("carrier == @carrier").bus)
            )
            logger.info(
                f"clustering preparation (hac): aggregating {len(buses_i)} buses of type {carrier}."
            )
            n, busmap_hac = aggregate_to_substations(
                n, params.aggregation_strategies, buses_i
            )
            busmaps.append(busmap_hac)

    if snakemake.wildcards.simpl:
        n, cluster_map = cluster(
            n,
            int(snakemake.wildcards.simpl),
            params.focus_weights,
            solver_name,
            params.simplify_network["algorithm"],
            params.simplify_network["feature"],
            params.aggregation_strategies,
        )
        busmaps.append(cluster_map)

    # some entries in n.buses are not updated in previous functions, therefore can be wrong. as they are not needed
    # and are lost when clustering (for example with the simpl wildcard), we remove them for consistency:
    remove = [
        "symbol",
        "tags",
        "under_construction",
        "substation_lv",
        "substation_off",
        "geometry",
        "underground",
    ]
    n.buses.drop(remove, axis=1, inplace=True, errors="ignore")
    n.lines.drop(remove, axis=1, errors="ignore", inplace=True)

    update_p_nom_max(n)

    n.meta = dict(snakemake.config, **dict(wildcards=dict(snakemake.wildcards)))
    n.export_to_netcdf(snakemake.output.network)

    busmap_s = reduce(lambda x, y: x.map(y), busmaps[1:], busmaps[0])
    busmap_s.to_csv(snakemake.output.busmap)

    cluster_regions(busmaps, snakemake.input, snakemake.output)


import requests
import nltk
import operator
import math
import json
import numpy as np
import pandas as pd
from time import time
from bs4 import BeautifulSoup
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
from nltk.tokenize import RegexpTokenizer
from nltk.corpus import wordnet
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, precision_recall_fscore_support
from sklearn.model_selection import train_test_split, cross_val_score
from itertools import combinations
from collections import Counter
from statistics import mean
from treatment import diseaseDetail

class DiseasePredictor:
    def __init__(self):
        # nltk.download('all')

        self.stop_words = stopwords.words('english')

        # Load Dataset scraped from NHP (https://www.nhp.gov.in/disease-a-z) & Wikipedia
        # Scrapping and creation of dataset csv is done in a separate program
        self.df_comb = pd.read_csv("./dataset/dis_sym_dataset_comb.csv") # Disease combination
        self.df_norm = pd.read_csv("./dataset/dis_sym_dataset_norm.csv") # Individual Disease

        self.X = self.df_comb.iloc[:, 1:]
        self.Y = self.df_comb.iloc[:, 0:1]

        # Using Logistic Regression (LR) Classifier as it gives better accuracy compared to other
        # classification models as observed in the comparison of model accuracies in Model_latest.py
        # Cross validation is done on dataset with cv = 5
        self.lr = LogisticRegression()
        self.lr = self.lr.fit(self.X, self.Y)
        self.scores = cross_val_score(self.lr, self.X, self.Y, cv=5)

        # List of symptoms
        self.dataset_symptoms = list(self.X.columns)

        # Preprocess
        self.lemmatizer = WordNetLemmatizer()
        self.splitter = RegexpTokenizer(r'\w+')

    # returns the list of synonyms of the input word from thesaurus.com (https://www.thesaurus.com/)
    # and wordnet (https://www.nltk.org/howto/wordnet.html)
    def synonyms(self, term):
        synonyms = []
        response = requests.get('https://www.thesaurus.com/browse/{}'.format(term))
        soup = BeautifulSoup(response.content,  "html.parser")
        try:
            container=soup.find('section', {'class': 'MainContentContainer'})
            row=container.find('div',{'class':'css-191l5o0-ClassicContentCard'})
            row = row.find_all('li')
            for x in row:
                synonyms.append(x.get_text())
        except:
            None
        for syn in wordnet.synsets(term):
            synonyms+=syn.lemma_names()
        return set(synonyms)

    def setInitialSymptoms(self, initial_symptoms):
        # Taking symptoms from user as input str(input("Please enter symptoms separated by comma(,):\n"))
        self.user_symptoms = initial_symptoms.lower().split(',')
        # Preprocessing the input symptoms
        self.processed_user_symptoms=[]
        for sym in self.user_symptoms:
            sym=sym.strip()
            sym=sym.replace('-',' ')
            sym=sym.replace("'",'')
            sym = ' '.join([self.lemmatizer.lemmatize(word) for word in self.splitter.tokenize(sym)])
            self.processed_user_symptoms.append(sym)

        # Taking each user symptom and finding all its synonyms and appending it to the pre-processed symptom string
        user_symptoms = []
        for user_sym in self.processed_user_symptoms:
            user_sym = user_sym.split()
            str_sym = set()
            for comb in range(1, len(user_sym)+1):
                for subset in combinations(user_sym, comb):
                    subset=' '.join(subset)
                    subset = self.synonyms(subset)
                    str_sym.update(subset)
            str_sym.add(' '.join(user_sym))
            user_symptoms.append(' '.join(str_sym).replace('_',' '))

        # Loop over all the symptoms in dataset and check its similarity score to the synonym string of the user-input
        # symptoms. If similarity>0.5, add the symptom to the final list
        found_symptoms = set()
        for idx, data_sym in enumerate(self.dataset_symptoms):
            data_sym_split=data_sym.split()
            for user_sym in user_symptoms:
                count=0
                for symp in data_sym_split:
                    if symp in user_sym.split():
                        count+=1
                if count/len(data_sym_split)>0.5:
                    found_symptoms.add(data_sym)
        found_symptoms = list(found_symptoms)

        # Show the related symptoms found in the dataset and ask user to select among them
        # select_list = input("\nPlease select the relevant symptoms. Enter indices (separated-space):\n").split()

        # Find other relevant symptoms from the dataset based on user symptoms based on the highest co-occurance with the
        # ones that is input by the user
        dis_list = set()
        self.final_symp = []
        counter_list = []
        for idx, itm in enumerate(found_symptoms):
            symp=found_symptoms[idx]
            self.final_symp.append(symp)
            dis_list.update(set(self.df_norm[self.df_norm[symp]==1]['label_dis']))

        for dis in dis_list:
            row = self.df_norm.loc[self.df_norm['label_dis'] == dis].values.tolist()
            row[0].pop(0)
            for idx,val in enumerate(row[0]):
                if val!=0 and self.dataset_symptoms[idx] not in self.final_symp:
                    counter_list.append(self.dataset_symptoms[idx])

        # Symptoms that co-occur with the ones selected by user
        dict_symp = dict(Counter(counter_list))
        self.dict_symp_tup = sorted(dict_symp.items(), key=operator.itemgetter(1),reverse=True)
        return found_symptoms

    def getRelatedSymptoms(self):
        found_symptoms=[]
        for tup in self.dict_symp_tup:
            found_symptoms.append(tup[0])
        return found_symptoms

    def setSymptom(self, symptom):
        self.final_symp = symptom
        return self.final_symp

    def predict(self):
        # Create query vector based on symptoms selected by the user
        sample_x = [0 for x in range(0,len(self.dataset_symptoms))]
        for val in self.final_symp:
            sample_x[self.dataset_symptoms.index(val)]=1

        # Predict disease
        lr = LogisticRegression()
        lr = lr.fit(self.X, self.Y)
        prediction = lr.predict_proba([sample_x])

        k = 10
        diseases = list(set(self.Y['label_dis']))
        diseases.sort()
        topk = prediction[0].argsort()[-k:][::-1]

        topk_dict = {}
        # Show top 10 highly probable disease to the user.
        for idx,t in  enumerate(topk):
            match_sym=set()
            row = self.df_norm.loc[self.df_norm['label_dis'] == diseases[t]].values.tolist()
            row[0].pop(0)

            for idx,val in enumerate(row[0]):
                if val!=0:
                    match_sym.add(self.dataset_symptoms[idx])
            prob = (len(match_sym.intersection(set(self.final_symp)))+1)/(len(set(self.final_symp))+1)
            prob *= mean(self.scores)
            topk_dict[t] = prob

        topk_sorted = dict(sorted(topk_dict.items(), key=lambda kv: kv[1], reverse=True))

        diseaseNames = []
        diseaseProbabilities = []

        for key in topk_sorted:
            prob = topk_sorted[key]*100
            diseaseNames.append(diseases[key])
            diseaseProbabilities.append(round(prob, 2))

        predictions = [{"name": d, "probability": p} for d, p in zip(diseaseNames, diseaseProbabilities)]
        return predictions


'''mostly borrowed from 1.py
pass 2-d array assignement to methods
initiate it in main
'''

def read_input(s): #s is a string of 81 digits (0-9)
	assert len(s)==81, "Wrong input size; should have 81 digits! "
	data = [int(x) for x in list(s)]
	return [ data[9*i:9*i+9] for i in range(9) ]

def print_board(arr):
	for row in arr:
		print(row)

def valid(arr, r, c, val):
	return rowValid(arr, r, val) and colValid(arr, c, val) and boxValid(arr, r, c, val)

def rowValid(arr, row, val):
	for i in range(9):
		if arr[row][i]==val: return False
	return True

def colValid(arr, col, val):
	for i in range(9):
		if arr[i][col]==val: return False
	return True

def boxValid(arr, row, col, val):
	boxRow = row - row % 3;
	boxCol = col - col % 3;
	for i in range(3):
		for j in range(3):
			if arr[boxRow+i][boxCol+j]==val: return False
	return True

def done(arr):
	return all([arr[i][j] != 0 for i in range(9) for j in range(9)])

def find_open_cell(arr):
	for i in range(9):
		for j in range(9):
			if arr[i][j]==0:
				return (i, j)
	return (-1, -1)

def solve(arr): # re
	if done(arr): return True #all cells are assigned, solution found
	else:
		row, col = find_open_cell(arr)
		for x in range(1,10):
			if valid(arr, row, col, x): #if the value doesn't violate sudoku rules
				arr[row][col] = x # make temporary assignment
				if solve(arr):
					return True
				else:
					arr[row][col] = 0 # backtrack
		return False

if __name__=="__main__":
	s1 = "003020600900305001001806400008102900700000008006708200002609500800203009005010300"
	#s2 = "390060807020030050000005096900502400000000000003907002810600000030050080502090043"
	assignment = read_input(s1)
	solve(assignment)
	print_board(assignment)


import numpy as np
import cv2

img = cv2.imread('input.jpg',3)  # load a color image - of 3 channel
#cv2.imshow('output',img)   # show the image on screen window , window automatically fit to the window size
r = 300.0/img.shape[1]  #find the aspect ratio
dim1 = (300,int(img.shape[0]*r)) # set the dimention width and height (300x300 - with Aspect ratio)
dim2 = (300,300) # set the dimention width and height - without Aspect ratio
new_img_ar = cv2.resize(img,dim1,interpolation = cv2.INTER_AREA)
new_img = cv2.resize(img,dim2,interpolation = cv2.INTER_AREA)
cv2.imshow('output',new_img)
cv2.imwrite("resized_image_with_AR.jpg",new_img_ar)
cv2.imwrite("resized_image.jpg",new_img)
cv2.waitKey(0)           # waitkey is a keyboard binding function.It's argument is the time in milliseconds.
cv2.destroyAllWindows()


#!/usr/bin/env python

from rds_backup import RDSBackup
import os
import boto3
import logging

logging.basicConfig()
logging.getLogger('RDS').setLevel(logging.INFO)

def handler(event, context):

    try:
        expiration_range_days = int(os.environ['BACKUP_RETENTION_DAYS'])

    except Exception as e:
      logging.fatal('Error getting BACKUP_RETENTION_DAYS environment var')
      raise e

    try:
      rdscon = RDSBackup()
      session = boto3.Session()
      rdscon.connect(session)
      rdscon.delete_expired_backups(expiration_range_days)

      return
    except Exception as e:
      raise e

if __name__ == '__main__':
    handler(0, 0)


import requests,base64,glob,json
from os import system,walk
from time import sleep
from random import choice
from colorama import Fore
from shutil import copyfileobj
from pystyle import Center,Colors,Colorate,System

System.Size(200,40)

b = Fore.LIGHTBLUE_EX
w = Fore.LIGHTYELLOW_EX
rw = Fore.LIGHTWHITE_EX

def clear():
    system('cls')

def title(text):
    system('title '+ text)

def getImagePath():
    file_path_type = ["ChangeToken/Images/*.jpg"]
    images = glob.glob(choice(file_path_type))
    random_image = choice(images)
    file_path = str(random_image).replace('\\','/')
    return file_path

def getImages(image_path):
    with open(image_path, "rb") as img_file:
        b64_string = base64.b64encode(img_file.read())
        return str(b64_string).replace('b\'','')

clear()
title(f'Discord Username ^& PFP ^& Bio Scraper  ^|  Made by seadhy#9999')
logo = Colorate.Vertical(Colors.yellow_to_red, Center.XCenter("""

  ██████ ▓█████ ▄▄▄        ██████  ███▄ ▄███▓ ▄▄▄        ██████  ██░ ██      ██████  ▄████▄   ██▀███   ▄▄▄       ██▓███  ▓█████  ██▀███
▒██    ▒ ▓█   ▀▒████▄    ▒██    ▒ ▓██▒▀█▀ ██▒▒████▄    ▒██    ▒ ▓██░ ██▒   ▒██    ▒ ▒██▀ ▀█  ▓██ ▒ ██▒▒████▄    ▓██░  ██▒▓█   ▀ ▓██ ▒ ██▒
░ ▓██▄   ▒███  ▒██  ▀█▄  ░ ▓██▄   ▓██    ▓██░▒██  ▀█▄  ░ ▓██▄   ▒██▀▀██░   ░ ▓██▄   ▒▓█    ▄ ▓██ ░▄█ ▒▒██  ▀█▄  ▓██░ ██▓▒▒███   ▓██ ░▄█ ▒
  ▒   ██▒▒▓█  ▄░██▄▄▄▄██   ▒   ██▒▒██    ▒██ ░██▄▄▄▄██   ▒   ██▒░▓█ ░██      ▒   ██▒▒▓▓▄ ▄██▒▒██▀▀█▄  ░██▄▄▄▄██ ▒██▄█▓▒ ▒▒▓█  ▄ ▒██▀▀█▄
▒██████▒▒░▒████▒▓█   ▓██▒▒██████▒▒▒██▒   ░██▒ ▓█   ▓██▒▒██████▒▒░▓█▒░██▓   ▒██████▒▒▒ ▓███▀ ░░██▓ ▒██▒ ▓█   ▓██▒▒██▒ ░  ░░▒████▒░██▓ ▒██▒
▒ ▒▓▒ ▒ ░░░ ▒░ ░▒▒   ▓▒█░▒ ▒▓▒ ▒ ░░ ▒░   ░  ░ ▒▒   ▓▒█░▒ ▒▓▒ ▒ ░ ▒ ░░▒░▒   ▒ ▒▓▒ ▒ ░░ ░▒ ▒  ░░ ▒▓ ░▒▓░ ▒▒   ▓▒█░▒▓▒░ ░  ░░░ ▒░ ░░ ▒▓ ░▒▓░
░ ░▒  ░ ░ ░ ░  ░ ▒   ▒▒ ░░ ░▒  ░ ░░  ░      ░  ▒   ▒▒ ░░ ░▒  ░ ░ ▒ ░▒░ ░   ░ ░▒  ░ ░  ░  ▒     ░▒ ░ ▒░  ▒   ▒▒ ░░▒ ░      ░ ░  ░  ░▒ ░ ▒░
░  ░  ░     ░    ░   ▒   ░  ░  ░  ░      ░     ░   ▒   ░  ░  ░   ░  ░░ ░   ░  ░  ░  ░          ░░   ░   ░   ▒   ░░          ░     ░░   ░
      ░     ░  ░     ░  ░      ░         ░         ░  ░      ░   ░  ░  ░         ░  ░ ░         ░           ░  ░            ░  ░   ░
                                                                                ░
                                ⌜――――――――――――――――――――――――――――――――――――――――――――――――――――⌝
                                ┇      [Discord] seadhy#9999                         ┇
                                ┇      [Github]  https://github.com/seadhy           ┇
                                ⌞――――――――――――――――――――――――――――――――――――――――――――――――――――⌟

"""))

class Tools:
    def __init__(self, want_proxy: bool):
        if want_proxy: self.proxies = open('proxies.txt','r',encoding='utf-8').read().splitlines()
        self.usernames = open('Scrapes/usernames.txt','r',encoding='utf-8').read().splitlines()
        self.token = json.load(open('config.json','r',encoding='utf-8'))['account_token']
        self.xsup = 'eyJvcyI6IldpbmRvd3MiLCJicm93c2VyIjoiQ2hyb21lIiwiZGV2aWNlIjoiIiwic3lzdGVtX2xvY2FsZSI6ImVuLVVTIiwiYnJvd3Nlcl91c2VyX2FnZW50IjoiTW96aWxsYS81LjAgKFdpbmRvd3MgTlQgMTAuMDsgV2luNjQ7IHg2NCkgQXBwbGVXZWJLaXQvNTM3LjM2IChLSFRNTCwgbGlrZSBHZWNrbykgQ2hyb21lLzEwNi4wLjAuMCBTYWZhcmkvNTM3LjM2IiwiYnJvd3Nlcl92ZXJzaW9uIjoiMTA2LjAuMC4wIiwib3NfdmVyc2lvbiI6IjEwIiwicmVmZXJyZXIiOiIiLCJyZWZlcnJpbmdfZG9tYWluIjoiIiwicmVmZXJyZXJfY3VycmVudCI6IiIsInJlZmVycmluZ19kb21haW5fY3VycmVudCI6IiIsInJlbGVhc2VfY2hhbm5lbCI6InN0YWJsZSIsImNsaWVudF9idWlsZF9udW1iZXIiOjE1MTYzOCwiY2xpZW50X2V2ZW50X3NvdXJjZSI6bnVsbH0='

        self.scraped_counter = 0
        self.param = {
            "limit": 100
        }

        self.p = Fore.LIGHTMAGENTA_EX
        self.w = Fore.LIGHTWHITE_EX
        self.b = Fore.LIGHTBLUE_EX
        self.g = Fore.LIGHTGREEN_EX
        self.r = Fore.LIGHTRED_EX
        self.c = Fore.LIGHTCYAN_EX
        self.y = Fore.LIGHTYELLOW_EX

        title(f'Discord Username ^& PFP ^& Bio Scraper  ^| Total Scraped: {self.scraped_counter}  ^|  Made by seadhy#9999')
        if self.token == 'enter account token':
            print(f'{self.b}[{self.p}Debug Mode{self.b}] {self.w}[{self.y}i{self.w}] Error -> Enter Account Token in config.json!')
            sleep(9999)
    def scrapeInfo(self,channelid: str, want_proxy: bool):
        file_number = len(next(walk('Scrapes/Images/'))[2])

        while True:
            try:
                headers = {
                    'accept': '*/*',
                    'authorization': self.token,
                    'cache-control': 'no-cache',
                    'pragma': 'no-cache',
                    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36',
                    'x-debug-options': 'bugReporterEnabled',
                    'x-super-properties': self.xsup,
                }
                r = requests.get(f'https://discord.com/api/v9/channels/{channelid}/messages',params=self.param,headers=headers)

                for info in r.json():

                    username = info['author']['username']
                    userid = info['author']['id']
                    userpfp = info['author']['avatar']
                    if username not in self.usernames:
                        try:
                            self.usernames.append(username)
                        except:
                            continue
                        r2 = requests.get(url=f'https://cdn.discordapp.com/avatars/{userid}/{userpfp}.jpg?size=512',stream=True)
                        if want_proxy:
                            proxy = choice(self.proxies)
                            proxies = {
                                "http": f'http://{proxy}'
                            }

                            headers = {
                                "accept": "*/*",
                                "accept-encoding": "gzip, deflate, br",
                                'authorization': self.token,
                                "dnt": "1",
                                "sec-fetch-dest": "empty",
                                "sec-fetch-mode": "cors",
                                "sec-fetch-site": "same-origin",
                                "sec-gpc": "1",
                                "x-debug-options": "bugReporterEnabled",
                                "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36",
                                "x-super-properties": self.xsup
                            }

                            r3 = requests.get(url=f'https://discord.com/api/v9/users/{userid}/profile',headers=headers, proxies=proxies, timeout=10)
                            try:
                                userbio = r3.json()['user']['bio']
                            except KeyError:
                                print(f'{self.b}[{self.p}Debug Mode{self.b}] {self.w}[{self.y}i{self.w}] Error -> {self.r}No biography')
                                userbio = False
                            except requests.exceptions.JSONDecodeError as e:
                                print(r3.text)
                                print(f'{self.b}[{self.p}Debug Mode{self.b}] {self.w}[{self.y}i{self.w}] Error -> {self.r}Rate Limited!')
                                userbio = False

                        if want_proxy:
                            print(f'{self.b}[{self.p}Seasmash{self.b}] {self.w}[{self.g}+{self.w}] Username, PFP and Bio of the account {self.c}{username}{self.w} are scraped.')
                        else:
                            print(f'{self.b}[{self.p}Seasmash{self.b}] {self.w}[{self.g}+{self.w}] Username and PFP of the account {self.c}{username}{self.w} are scraped.')

                        file_number += 1
                        self.scraped_counter += 1
                        title(f'Discord Username ^& PFP ^& Bio Scraper  ^|  Total Scraped: {self.scraped_counter}  ^|  Made by seadhy#9999')
                        if userpfp != None:
                            with open(f'Scrapes/Images/images-{file_number}.jpg','wb') as down_file:
                                copyfileobj(r2.raw, down_file)

                        else:
                            file_number -= 1
                        with open('Scrapes/usernames.txt','a',encoding='utf-8') as file:
                            file.write(f"{username}\n")
                        if want_proxy and userbio != False:
                            if userbio != '[]' and not "\n" in userbio and not 'discord.gg/' in userbio and "" != userbio and not '.gg/' in userbio:
                                with open('Scrapes/bio.txt','a',encoding='utf-8') as file:
                                    file.write(f"{userbio}\n")
                self.param = {
                        'before': r.json()[-1]['id'],
                        'limit': 100
                    }
            except Exception as e:
                if 'Unauthorized'in r.text:
                    print(f'{self.b}[{self.p}Debug Mode{self.b}] {self.w}[{self.y}i{self.w}] Error -> {self.r}Token Invalid! Change token in \'config.json\'')
                    sleep(10)
                    break
                else:
                    print(f'{self.b}[{self.p}Debug Mode{self.b}] {self.w}[{self.y}i{self.w}] Error -> {self.r}{str(e).capitalize()}')
                sleep(5)

print(logo)
while True:
    print(f"""
{b}[{w}1{b}]{rw} Username & PFP & Bio Scraper
{b}[{w}2{b}]{rw} Escape
    """)
    user_choice = int(input('> '))
    if user_choice == 1:
        print(f"\n{b}[{w}?{b}]{rw} Channel ID: ")
        channel_id = input('> ')
        print(f"\n{b}[{w}?{b}]{rw} Do you want to use a proxy? (y/n)")
        print(f"{b}[{w}*{b}]{rw} WARNING: If you don't want to use it, the program won't collect the bio!")
        want_proxy = input('> ')
        if want_proxy.lower() == 'y':
            want_proxy = True
        elif want_proxy.lower() == 'n':
            want_proxy = False
        clear()
        print(logo)
        tool = Tools(want_proxy)
        tool.scrapeInfo(channel_id, want_proxy)
    elif user_choice == 2:
        print("Exiting...")
        sleep(3)
        break


from distutils.core import setup

DISTNAME = 'tethne'
AUTHOR = 'E. Peirson, Digital Innovation Group @ ASU'
MAINTAINER = 'Erick Peirson'
MAINTAINER_EMAIL = 'erick [dot] peirson [at] asu [dot] edu'
DESCRIPTION = ('Bibliographic network and corpus analysis for historians')
LICENSE = 'GNU GPL 3'
URL = 'http://diging.github.io/tethne/'
VERSION = '0.6.4-beta'

PACKAGES = [ 'tethne',
             'tethne.analyze',
             'tethne.classes',
             'tethne.model',
             'tethne.model.corpus',
             'tethne.model.managers',
             'tethne.model.social',
             'tethne.networks',
             'tethne.persistence',
             'tethne.persistence.hdf5',
             'tethne.readers',
             'tethne.services',
             'tethne.utilities',
             'tethne.writers',
            ]

setup(
    name=DISTNAME,
    author=AUTHOR,
    maintainer=MAINTAINER,
    maintainer_email=MAINTAINER_EMAIL,
    description=DESCRIPTION,
    license=LICENSE,
    url=URL,
    version=VERSION,
    packages = PACKAGES,
    install_requires=[
        "networkx >= 1.8.1",
        "matplotlib >= 1.3.1",
        "tables >= 3.1.1",
        "Unidecode >= 0.04.16",
        "geopy >= 0.99",
        "nltk",
        "scipy==0.14.0",
        "numpy==1.8.1",
    ],
)


import pandas as pd
from sklearn.neural_network import MLPClassifier
import numpy as np
import json
import scipy as sc
import numpy as np

train_file=pd.read_json(r'D:\Data Analysis\Project2\Project2\Data-Analysis-Project2\train.json', orient='records')
test_file=pd.read_json(r'D:\Data Analysis\Project2\Project2\Data-Analysis-Project2\test.json', orient='records')
test_output=pd.read_csv(r'D:\Data Analysis\Project2\Project2\Data-Analysis-Project2\sample_solution.csv',header=0)
##================
#Features
##================

##================
test_labels=test_output['cuisine']
train_input=train_file['ingredients']
#train_input=train_input.astype('float32',(20,))
train_output=train_file['cuisine']
test_input=test_file['ingredients']

t=[]
for i in train_input:
    t.extend(i)

ing=set(t)
print(len(ing),' is the length of the ingreduents in train_input')

te=[]
for i in test_input:
    te.extend(i)

ingte=set(te)
print(len(ingte),' is the length of the ingredients in test input')

element=list(ing)
train=np.zeros((39775,6714))
test=np.zeros((9945,6714))

print("It will take time")
x=0
y=0
for i in train_input:
	for j in i:
		y=0
		for k in element:
			if(j==k):
				train[x,y]=1
			y=y+1
	x=x+1
print("over")
print("It will take time")
x=0
y=0
for i in test_input:
	for j in i:
		y=0
		for k in element:
			if(j==k):
				test[x,y]=1
			y=y+1
	x=x+1

print("Over")

classifier=MLPClassifier(hidden_layer_sizes=1,activation='relu',solver='sgd',alpha=0.0001,batch_size=64,learning_rate='adaptive')
classifier.fit(train[0:39774,:],train_output)
ypred=classifier.predict(test)

##========
##def encode(uniquevals,array):
##    for cnt,ele in enumerate(uniquevals):
##    max(cnt)

##========
##t=train_input
##for i in t:
##	for z in range(len(i),65):
##		i.extend('X')

#test_input=test_input.astype('float32',(20,))

##best=0
##accuracy=0
##hl=[1]
##act=['logistic', 'tanh', 'relu']
##sol=['lbfgs','sgd','adam']
##al=[0.0001,0.0005]
##bs=[64,128]
##lr=['constant','invscaling','adaptive']
##best_params = [0,0,0,0,0,0]
##for h in hl:
##    for a in act:
##        for s in sol:
##            for a1 in al:
##                for b in bs:
##                    for l in lr:
##                        classifier=MLPClassifier(hidden_layer_sizes=h,activation=a,solver=s,alpha=a1,batch_size=b,learning_rate=l)
##                        classifier.fit(train_input,train_output)
##                        ypred=classifier.predict(test_input)
##
##
##
##
##print(ypred)


import torch
import torchvision

class Hsigmoid(torch.nn.Module):
    def __init__(self, inplace = True):
        super(Hsigmoid, self).__init__()
        self.inplace = inplace

    def forward(self, x):
        return torch.nn.functional.relu(x+3, inplace = self.inplace)/6.0

class SEModule(torch.nn.Module):
    def __init__(self, channel, reduction = 4):
        super(SEModule, self).__init__()
        self.avg_pool = torch.nn.AdaptiveAvgPool2d(1)
        self.fc = torch.nn.Sequential(
            torch.nn.Linear(channel, channel//reduction, bias=False),
            torch.nn.ReLU(inplace=True),
            torch.nn.Linear(channel // reduction, channel, bias=False),
            Hsigmoid()
        )

    def _initialize_weights(self):
        # weight initialization
        for m in self.modules():
            if isinstance(m, torch.nn.Conv2d):
                torch.nn.init.kaiming_normal_(m.weight, mode='fan_out')
                if m.bias is not None:
                    torch.nn.init.zeros_(m.bias)
            elif isinstance(m, torch.nn.BatchNorm2d):
                torch.nn.init.ones_(m.weight)
                torch.nn.init.zeros_(m.bias)
            elif isinstance(m, torch.nn.Linear):
                torch.nn.init.normal_(m.weight, 0, 0.01)
                if m.bias is not None:
                    torch.nn.init.zeros_(m.bias)

    def forward(self, x):
        b, c, _, _= x.size()
        y = self.avg_pool(x).view(b, c)
        y = self.fc(y).view(b, c, 1, 1)
        return x * y.expand_as(x)

class Hswish(torch.nn.Module):
    def __init__(self, inplace=True):
        super(Hswish, self).__init__()
        self.inplace = inplace

    def forward(self, x):
        return x * torch.nn.functional.relu6(x + 3., inplace=self.inplace) / 6.

def get_training_dataloader(mean, std, batch_size=16, num_workers=2, shuffle=True):
    """ return training dataloader
    Args:
        path: path to cifar100 training python dataset
        batch_size: dataloader batchsize
        num_workers: dataloader num_works
        shuffle: whether to shuffle
    Returns: train_data_loader:torch dataloader object
    """

    transform_train = torchvision.transforms.Compose([
        #torchvision.transforms.ToPILImage(),
        torchvision.transforms.RandomCrop(32, padding=4),
        torchvision.transforms.RandomHorizontalFlip(),
        torchvision.transforms.ToTensor(),
        torchvision.transforms.Normalize(mean, std)
    ])
    #cifar100_training = CIFAR100Train(path, transform=transform_train)
    cifar100_training = torchvision.datasets.CIFAR100(root='./data', train=True, download=True, transform=transform_train)
    cifar100_training_loader = torch.utils.data.DataLoader(
        cifar100_training, shuffle=shuffle, num_workers=num_workers, batch_size=batch_size)

    return cifar100_training_loader

def get_test_dataloader(mean, std, batch_size=16, num_workers=2, shuffle=True):
    """ return training dataloader
    Args:
        path: path to cifar100 test python dataset
        batch_size: dataloader batchsize
        num_workers: dataloader num_works
        shuffle: whether to shuffle
    Returns: cifar100_test_loader:torch dataloader object
    """

    transform_test = torchvision.transforms.Compose([
        torchvision.transforms.ToTensor(),
        torchvision.transforms.Normalize(mean, std)
    ])
    #cifar100_test = CIFAR100Test(path, transform=transform_test)
    cifar100_test = torchvision.datasets.CIFAR100(root='./data', train=False, download=True, transform=transform_test)
    cifar100_test_loader = torch.utils.data.DataLoader(
        cifar100_test, shuffle=shuffle, num_workers=num_workers, batch_size=batch_size)

    return cifar100_test_loader


# -*- coding: utf-8 -*-
import base
class Host( base.BaseHost ):
    dev_mode = False

    short_key = '10p'
    long_key = '10pix'
    host='10pix.ru'
    max_file_size = 5242880 #5Mb

    user_agent = 'Client 10pix 1.2.5.3 (Microsoft Windows NT 5.1.2600 Service Pack 3)'
    action = 'http://%s/'%host
    form = {
            'xml':'1',
            'sizeBar':'1',
            }

    def as_file(self, _file):
        return {
                'uploadType':'0',
                'fileUpload': _file,
                }

    def as_url(self, _url):
        return {
                'uploadType':'1',
                'url': _url,
                }

    def postload(self ):
        _src = self.response.body
        _regx = r'<thumbnailLink>(.+?)</thumbnailLink>'
        _url = self.findall(_regx ,_src)[0]
        self.img_url = '%s'%_url.replace('th.','')
        self.img_thumb_url = '%s'%_url

if __name__ == '__main__':
    h= Host()
    h.test_file()
    h.test_url()


import pytest
import requests

class TestMasterGET:
    @pytest.mark.dependency(
        depends=[
            "test_connection.py::test_master_connection"
        ],
        scope="session",
    )
    def test_get_request(self, master_url):
        assert requests.get(master_url).ok, "The status of the request code is not 200, it should only be 200"

    @pytest.mark.dependency(
        depends=[
            "test_connection.py::test_reaper_connection",
            "test_master.py::TestMasterGET::test_get_request"
        ],
        scope="session",
    )
    @pytest.mark.parametrize('test_input', [('run_web_parser', 'get_data')])
    def test_get_content(self, test_input, master_url):
        available_commands = requests.get(master_url).json()['available_commands']
        is_same_commands = sorted(available_commands.keys()) == sorted(test_input)
        assert is_same_commands, "Expected commands are not the same as actual available commands, should be same"

    @pytest.mark.dependency(
        depends=[
            "test_connection.py::test_reaper_connection",
            "test_master.py::TestMasterGET::test_get_request"
        ],
        scope="session",
    )
    def test_get_content_help(self, master_url):
        available_commands = requests.get(master_url).json()['available_commands']
        is_all_help_is_str = all((isinstance(help_content, str) for help_content in available_commands.values()))
        assert is_all_help_is_str, "Help on command is not of str type, should be str"

class TestMasterAPI:
    @pytest.mark.dependency(
        depends=[
            "test_connection.py::test_master_connection",
            "test_connection.py::test_reaper_connection",
            "test_connection.py::test_keeper_connection",
        ],
        scope="session",
    )
    @pytest.mark.parametrize('test_data,expected', [
        ({
             'command_name': 'run_web_parser',
             'data': {
                 'first_paginator_url_template': 'https://rabota.by/search/vacancy?text=Python&page={page_number}',
                 'words_to_find': ['python'],
                 'request_headers': {'user-agent': 'job_parser/1.1.1'},
                 'block_link_class': 'bloko-link HH-LinkModifier',
                 'start_page': 0,
                 'end_page': 1,
             }
         }, True),
        ({
             'command_name': 'run_web_parser',
             'data': {
                 'first_paginator_url_template': 'https://rabota.by/search/vacancy?text=Python&page={page_number}',
                 'words_to_find': ['Linux'],
                 'request_headers': {'user-agent': 'job_parser/1.1.1'},
                 'classes_to_exclude': ['recommended-vacancies', 'related-vacancies-wrapper'],
                 'block_link_class': 'bloko-link HH-LinkModifier',
                 'start_page': 0,
                 'end_page': 1,
             }
         }, True),
    ])
    def test_run_web_parser(self, test_data, expected, master_url):
        response = requests.post(master_url, json=test_data)
        assert response.ok == expected


#!/usr/bin/python3

# ## {R2D2919B742E} ##
# ###########################################################################
# What if magic existed?
# What if a place existed where your every thought and dream come to life.
# There is only one catch: it has to be written down.
# Such a place exists, it is called programming.
#    - Scott Taylor Reph, RightThumb.com
# ###########################################################################
# ## {C3P0D40fAe8B} ##

import os
import sys
import time
##################################################
import _rightThumb._construct as __
import _rightThumb._matrix as _matrix
a, app, application = _matrix.theApp()
appDBA = _matrix.clearFocus( __name__, __file__ )
_matrix.appReg = appDBA
# _.pr( 'appDBA', appDBA )
app.focus( appDBA )
def focus( parentApp='', childApp='', reg=True ):
	global appDBA
	f = _matrix.appName( appDBA, parentApp, childApp )
	if reg:
		_matrix.appReg = f
	return f
_matrix.registeredApps.append( focus() )
import _rightThumb._base3 as _
import _rightThumb._base4 as ___
##################################################
program = {

	'file': 'matrix1.py',
	'liveAppName': _matrix.thisApp( __file__ ),
	'description': 'matrix test 1',

	'categories': [
						'matrix',
						'test',
	],

	'usage': [
						# 'epy another',
						# 'e nmap',
						# '',
	],
	'relatedapps': [
						# 'p another -file file.txt',
						# '',
	],
	'prerequisite': [
						# 'p another -file file.txt',
						# '',
	],
	'examples': [
						'p matrix1',
						'p matrix1 -file',
						'p matrix1 -file two',
						'',
						['p matrix1 -file one two', 'red'],
						'',
	],
	'columns': [
					{ 'name': 'name', 'abbreviation': 'n' },
					# { 'name': '{1}', 'abbreviation': '{0}', 'sort': '{2}' },
	],
	'aliases': [
					# 'this',
					# 'app',
	],

}

def appSwitches():
	pass
	# _.pr('appSwitches')
	app.switch( 'Memory', '-m,-mem,-memory' )
	app.switch( 'Files', '-f,-file,-files','file.txt', isPipe=True, isRequired=True, description='Files' )

focusID = None
def registration():
	global appDBA
	global program
	global focusID
	focusID = app.focus( appDBA ).register( program )
	appSwitches()
	app.load()

	if app.load_switches:
		_.myFileLocation_Print = False
		app.switch( 'Files' ).trigger( _.myFileLocations )
		app.switch( 'Folder' ).trigger( _.myFolderLocations )
		app.switch( 'URL' ).trigger( _.urlTrigger )
		app.defaultScriptTriggers()

	app.processSwitches()
	app.postLoad()

registration()

# app.switches().dump()
# sys.exit()

def test():
	i=0
	while i < 100:
		pass
	_.pr( 'ran' )

def handler( arg1='E1', arg2='E2' ):
	i=0
	# asdf
	while True:
		_.pr( i, arg1, arg2 )
		i+=1
		time.sleep(.5)

# app.asyn( 'test', {'script': handler, 'name': 'a', 'timeout': 1, 'kwargs': { 'arg1': 'a', 'arg2': 'b',  } } )
# app.asyn( 'test', {'script': handler, 'name': 'b', 'timeout': 1, 'kwargs': ['1'] } )
# app.asyn( 'test', {'script': handler, 'name': 'c', 'timeout': 1, 'kwargs': ('2') } )

def change(data):
	return '|'+str(data)+'|'

def callersname( i=0 ):
	callers = []
	i=0
	error=False
	while not error:
		try:
			callers.append(sys._getframe(i).f_code.co_name)
		except Exception as e:
			error=True
		i+=1

	return callers

def one(i):
	return callersname( i )
def two(i):
	return one( i )
def three(i):
	return two( i )
def four(i):
	return three( i )
def five(i):
	return four( i )

def fn(i):
	fn.f += i
	return fn.f

# sys.getfilesystemencoding()
# _.pr( 'mem', app.switch('Memory').isActive() )
if app.switch('Memory').isActive():
	_matrix.memoryPrint = True
	_.pr( '_matrix.memoryPrint = True' )
# def x():
# x()
def action():

	# _.pr( a.c(r=1) )
	# _.pr( a.c(isChild=True) )
	# sys.exit()
	# _.pr( sys._getframe().f_code.co_name, dir(sys._getframe()) )

	# _.pr( five(0) )
	# sys._getframe(  ).f_code.co_filename

	# fn.f = 1
	# _.pr( fn.f )
	# _.pr( fn.f )

	# sys.exit()
	global appDBA

	# sys._getframe().f_code.co_name

	app.data( 'AnyDesk.json' ).set( ___.table.get( 'AnyDesk.json' ) )
	# _.pr( app.data( 'AnyDesk.json' ).get() )
	# _.printVarSimple( app.data( 'AnyDesk.json' ).get() )

	# app.switch( 'Files', { 'name': 'Files', 'switches': '-f,-files', 'focus': _matrix.appReg } )

	x = app.switch( 'Files' ).about()
	# _.printVarSimple( x )

	# _.pr( "a.ir()", a.ir() )
	# _.pr( 'Files', a.s('Files').a() )

	if a.ir() and a.s('Files').a() and a.s('Files').l() and not app.switch('Files').inVal('one') and a.ps('thisProcess', 'Files and One', i=1 ):
		_.pr( 'Missing one' )
	if a.ir() and a.s('Files').a() and a.s('Files').l() and app.switch('Files').inVal('one') and a.ps('thisProcess', 'Files and One', i=1 ):
		_.pr( 'Works' )

	if a.ir() and a.s('Files').a() and not a.s('Files').l() and a.ps('thisProcess', 'Files and One', i=1 ):
		_.pr( 'No Switch Values' )
	if a.ir() and not a.s('Files').a() and a.ps('thisProcess', 'Files and One', i=1 ):
		_.pr( 'Missing Files Switch' )

	testVar = {}
	if a.ir() and a.s('Files').a() and a.docIF( 'test' in testVar.keys(), 'hasKey', 'test in testVar' ):
		a.ps('Files and test', 'docIF test', i=1 )
		_.pr( 'docIF True' )
	else:
		a.ps('Files and test', 'docIF test', i=1 )
		_.pr( 'docIF False' )

	pass
	# _.pr( ' i am here ' )
	# temp = app.ext( 'test', 'struct2_test5' , trackingID=123 )
	# temp = app.ext( 'test', 'struct2_test5' ).imp.action()
	# app.ext( 'test' ).imp.action()
	# temp = app.ext( 'test', 'struct2_test5' ).schedule(unload=True)
	# data = app.id( temp ).singleGetWait()
	# temp = app.ext( 'test', 'struct2_test5' ).schedule()
	# _.pr('NEW HERE',_matrix.genUUID())
	# _.pr('HERE')
	# app.viewLog()
	# data = app.ext( 'test', 'struct2_test5' ).action()
	data = app.ext( 'test', 'matrix2' ).action(unload=True,schedule=False,timeout=120)
	_.pr( '**********', data )
	# _.pr( '**********', data )
	# app.switch('Files').callers()

	# _.pr( a.c(isChild=True) )

	_.colorThis( ['                                                    ******************  THIS APP IS DONE  ****************** '] )
	_.colorThis( [ appDBA, '_matrix.app.memory_max', _.formatSize(_matrix.app.memory_max) ], 'yellow' )
	# app.totalMemory()
	# _.pr(list( app.records['switch'].keys() ))
	# _.printVarSimple( list( app.records['switch'].keys() ) )

# epyi matrix -file _async_child
# epyi matrix -file _audit_child
# epyi matrix -file _data_child
# epyi matrix -file _db_child
# epyi matrix -file _ext_child
# epyi matrix -file _focus_child
# epyi matrix -file _procedure_child
# epyi matrix -file _process_child
# epyi matrix -file _reg_child
# epyi matrix -file _switch_child
# epyi matrix -file _table_child
# epyi matrix -file _task_child

# epy genBase4Sections

# p file + _child --c | p line --c -make " n {} "

	# test()

	# test = ___.table.get( 'appRegistration.json' )

if __name__ == '__main__':
	app.asyn( 'action', action, trigger=app.focus(appDBA).unregister )

# sys._getframe().f_code.co_name


from tensorflow.python.data.util import nest as data_nest
from tf_agents.replay_buffers import table
from tf_agents.replay_buffers.tf_uniform_replay_buffer import TFUniformReplayBuffer, _valid_range_ids

import collections
import logging
import numpy as np
import tensorflow as tf

logger = logging.getLogger(__name__)

# this is set so that mathematical operations on NaN are raised.
np.seterr(all='raise')

# NOTE: storing the weights in 'probabilities' to be consistent with uniform buffer
BufferInfo = collections.namedtuple('BufferInfo', ['ids', 'probabilities'])

MIN_PRIORITY = 0.0001

class TfPrioritizedReplayBuffer(TFUniformReplayBuffer):
    """
    This class implements a prioritized replay buffer based on the paper linked below.
    Link to Paper: https://arxiv.org/pdf/1511.05952.pdf

    This replay buffer will track priorities, sample a batch according to them, calculate weights and let us
    update priorities after the loss has become known.

    For simplicity, sampling the experiences is implemented in the O(N). For more efficient way to perform sampling,
    the use of trees is required: https://github.com/openai/baselines/blob/master/baselines/deepq/replay_buffer.py#L71

    This priority buffer is designed to only handle 1 step mini_batch extractions. Time stacking is therefore not
    supported either.

    This priority buffer is also design to only 1 max_length(Segments of batches).
    This simplifies the way the priorities are tracked. Our current use case requires adding a batch once after
    initializing a buffer which makes it possible to use max_length == 1.
    """

    def __init__(self, data_spec,
                 batch_size,
                 max_length=1,
                 alpha=0.6,
                 scope='TFUniformReplayBuffer',
                 device='cpu:*',
                 table_fn=table.Table):
        """
        Args:
            data_spec: A TensorSpec or a list/tuple/nest of TensorSpecs describing a
                       single item that can be stored in this buffer.
            batch_size: Batch dimension of tensors when adding to buffer.
            max_length: The maximum number of items that can be stored in a single
                        batch segment of the buffer.
            alpha: α determines how much prioritization is used, with α = 0 corresponding to the uniform case.
            scope: Scope prefix for variables and ops created by this class.
            device: A TensorFlow device to place the Variables and ops.
            table_fn: Function to create tables `table_fn(data_spec, capacity)` that
                      can read/write nested tensors.
        Raises:
            ValueError: If batch_size does not evenly divide capacity.
        """
        if max_length != 1:
            raise NotImplementedError('TfPrioritizedReplayBuffer only supports a batch segments '
                                      'size of 1, but received `max_length` {}.'.format(max_length))

        super(TfPrioritizedReplayBuffer, self).__init__(data_spec, batch_size, max_length, scope, device, table_fn)
        logger.info("Creating an instance of %s. Params: data_spec: %s, capacity: %s, alpha: %s, segment_length: %s" %
                    (str(type(self).__name__), str(data_spec), str(batch_size), str(alpha), str(max_length)))

        self._alpha = alpha

        # an array in which we keep track of the priorities. The size of this array is equal to the size of the replay
        # buffer. Items stored at a given index in the Priority array map to the experience at the same index in the
        # buffer. The content of the items in the priority array represent the loss of their respective experience the
        # last time that experience was used for training.
        self._priority_table = np.zeros((self._capacity_value,), dtype=np.float32)

    def _add_batch(self, items):
        """Adds a batch of items to the replay buffer.

        Args:
          items: A tensor or list/tuple/nest of tensors representing a batch of
          items to be added to the replay buffer. Each element of `items` must match
          the data_spec of this class. Should be shape [batch_size, data_spec, ...]
        Returns:
          An op that adds `items` to the replay buffer.
        Raises:
          ValueError: If called more than once.
        """
        super()._add_batch(items)
        logger.info("Adding a batch of %s experiences to Replay buffer" % str(self._capacity_value))
        # WARNING: This is implemented under the assumption that add batch is only called once
        self._priority_table = np.ones(self._capacity_value, dtype=np.float32)

    @staticmethod
    def compute_beta(beta, curr_iter, total_iter):
        return min(1.0, beta + curr_iter * (1.0 - beta) / total_iter)

    def get_next(self,
                 sample_batch_size=None,
                 beta=0.4,
                 num_steps=1,
                 time_stacked=False):
        """Returns an item or batch of items sampled based on the experience's priorities from the buffer.

            Only num_steps = 1 and time_stacked=False are supported.

            Args:
                sample_batch_size: (Optional.) An optional batch_size to specify the
                    number of items to return. See get_next() documentation.
                beta (float): ratio to use when computing the importance sampling weights
                num_steps: (Optional.)  Optional way to specify that sub-episodes are
                    desired. See get_next() documentation.
                time_stacked: Bool, when true and num_steps > 1 get_next on the buffer
                    would return the items stack on the time dimension. The outputs would be
                    [B, T, ..] if sample_batch_size is given or [T, ..] otherwise.
            Returns:
                A 2 tuple, containing:
                    - An item, sequence of items, or batch thereof sampled uniformly
                    from the buffer.
                    - BufferInfo NamedTuple, containing:
                        - The items' ids.
                        - The importance sampling weight of each item.
            Raises:
                ValueError: if num_steps is bigger than the capacity.
        """
        num_steps_value = num_steps if num_steps is not None else 1

        if num_steps_value != 1:
            raise NotImplementedError('TfPrioritizedReplayBuffer only supports a batches with num_step '
                                      'size of 1, but received batch with num_steps'
                                      'size {}.'.format(num_steps_value))
        with tf.device(self._device), tf.name_scope(self._scope):
            with tf.name_scope('get_next'):
                min_val, max_val = _valid_range_ids(
                    self._get_last_id(), self._max_length, num_steps)
                tf.compat.v1.assert_greater(
                    max_val,
                    min_val,
                    message='TFUniformReplayBuffer is empty. Make sure to add items '
                            'before sampling the buffer.')

        # select index based on the priorities of the experiences in the buffer
        selected_idx, selected_weight = self._sample_ids(sample_batch_size, beta)

        # convert ids to tensor
        rows_to_get = tf.convert_to_tensor(selected_idx)
        importance_sampling_weigths = tf.convert_to_tensor(selected_weight)
        data = self._data_table.read(rows_to_get)

        buffer_info = BufferInfo(ids=rows_to_get, probabilities=importance_sampling_weigths)
        return data, buffer_info

    def _sample_ids(self, sample_size, beta):
        """
        select the indices of the experience to pull from the prioritize replay buffer in addition to compute it's
        importance sampling weight.

        Params:
            sample_size (int): number of ids to retrieve
            beta (float): This a factor to which the Important Sampling is present
        """
        priorities = self._priority_table

        # update the priorities by the power of alpha
        probabilities = priorities ** self._alpha
        probabilities /= probabilities.sum()

        # select an index stochastically
        idx = np.random.choice(len(probabilities), sample_size, p=probabilities)

        # compute important sampling weights of the selected indices
        selected_probabilities = probabilities[idx]
        importance_sampling_weights = (self._capacity_value * selected_probabilities) ** (-beta)
        importance_sampling_weights /= importance_sampling_weights.max()

        return idx, importance_sampling_weights

    def update_priorities(self, indices, new_priorities):
        """
        Updates new priorities of the experiences in the given indices.

        Params:
            batch_indices: the indices to update
            batch_priorities: the new priorities
        """
        for idx, priority in zip(indices, new_priorities):
            ceil_priority = max(priority, MIN_PRIORITY)
            self._priority_table[idx] = ceil_priority

    def as_dataset(self,
                   sample_batch_size=None,
                   beta=0.4,
                   num_steps=None,
                   num_parallel_calls=None):
        """Creates a dataset that returns entries from the buffer.

            Args:
                sample_batch_size: (Optional.) An optional batch_size to specify the
                    number of items to return. See as_dataset() documentation.
                beta: This a factor to which the Important Sampling is present
                num_steps: (Optional.)  Optional way to specify that sub-episodes are
                    desired. See as_dataset() documentation.
                num_parallel_calls: (Optional.) Number elements to process in parallel.
                See as_dataset() documentation.
            Returns:
                A dataset of type tf.data.Dataset, elements of which are 2-tuples of:
                    - An item or sequence of items or batch thereof
                    - Auxiliary info for the items (i.e. ids, probs).
            Raises:
                ValueError: If the data spec contains lists that must be converted to
                tuples.
        """
        # data_tf.nest.flatten does not flatten python lists, nest.flatten does.
        if tf.nest.flatten(self._data_spec) != data_nest.flatten(self._data_spec):
            raise ValueError(
                'Cannot perform gather; data spec contains lists and this conflicts '
                'with gathering operator.  Convert any lists to tuples.  '
                'For example, if your spec looks like [a, b, c], '
                'change it to (a, b, c).  Spec structure is:\n  {}'.format(
                    tf.nest.map_structure(lambda spec: spec.dtype, self._data_spec)))

        def get_next(_):
            return self.get_next(sample_batch_size, beta=beta, num_steps=num_steps, time_stacked=False)

        return tf.data.experimental.Counter().map(
            get_next,
            num_parallel_calls=num_parallel_calls)


import datetime
from flask import Blueprint, request, current_app as app, jsonify
from flask_restful import abort
from flask_jwt_extended import (
    create_access_token,
    create_refresh_token,
    jwt_optional,
    get_jwt_identity,
    set_access_cookies,
    set_refresh_cookies,
    unset_jwt_cookies,
)
from werkzeug.exceptions import Unauthorized
from sqlalchemy.orm.exc import NoResultFound
from marshmallow import ValidationError

from db import db
from models.user import User
from schema.user_schema import UserSchema
from resources.errors import InternalServerError

login_api = Blueprint("login_api", __name__)

@login_api.route("/login", methods=["POST"])
@jwt_optional
def login():
    logged_in_user_handle = get_jwt_identity()
    if logged_in_user_handle:
        app.logger.info("Got logged in user handle: {}".format(logged_in_user_handle))
        return jsonify(login=True), 200

    try:
        login_data = UserSchema().load(request.get_json(), partial=True)
    except ValidationError as e:
        abort(
            400,
            message="Missing login fields",
            status=400,
            errors=e.messages,
        )

    app.logger.info("Validated login payload")

    try:
        user = db.session.query(User).filter(User.email == login_data["email"]).one()
        app.logger.info("Found user for login {}".format(user))
        authorized = user.check_password(login_data["password"])
        if not authorized:
            raise Unauthorized("Invalid password")
    except (NoResultFound, Unauthorized) as e:
        abort(
            401,
            message="Error authorizing user with email '{}': {}".format(
                login_data.email, e
            ),
            status=401,
        )
    except Exception:
        raise InternalServerError

    app.logger.info("Generating JWT token for user")
    token_identity = str(user.handle)
    access_token_exp = datetime.timedelta(days=app.config["JWT_ACCESS_TOKEN_EXP_DAYS"])
    refresh_token_exp = datetime.timedelta(
        days=app.config["JWT_REFRESH_TOKEN_EXP_DAYS"]
    )

    # Create the tokens we will be sending back to the user
    app.logger.info("Generating access and refresh tokens")
    access_token = create_access_token(
        identity=token_identity, expires_delta=access_token_exp
    )
    refresh_token = create_refresh_token(
        identity=token_identity, expires_delta=refresh_token_exp
    )

    # Set the JWTs and the CSRF double submit protection cookies
    # in this response
    response = jsonify(login=True)
    set_access_cookies(response, access_token)
    set_refresh_cookies(response, refresh_token)
    return response, 200

@login_api.route("/logout", methods=["POST"])
def logout():
    resp = jsonify(logout=True)
    unset_jwt_cookies(resp)
    return resp, 200


import feedparser
import validators
import sys
import pprint
import fnmatch
import platform
import time
import os

class Feed:
    """
    `Author`: Eliakah kakou

    This class allows for more functionality in regards
    to the dictionary entered in the constructor
    """

    def __init__(self, feed):
        """
        `Author`: Eliakah kakou

        The constructor

        `feed`: dict instance containing set of information about feed
        """
        self.feed = feed

    def extract(self):
        """
        `Author`: Eliakah kakou

        This method returns a subset of the dictionary
        """
        return {k: self.feed[k] for k in ('id', 'published', 'title', 'link', 'summary')}

    def record_content(self):
        return self.feed['link'], self.feed['published'];

class Feeder:
    """
    `Author`: Bill Clark

    An interface to fulfill a strategy pattern design in the Grapher with feeders.
    An implementation of this interface will be usable with the Grapher so long as
    it provides the appropriate returns.

    Feeder instances should be created with a link to some sort of file. The result
    to come out of it's methods will be used by the Parser Module to generate topics
    about the information in the Feed. A Feed represents a single article of related
    data. The implementation should generate Feed objects as defined above in order
    to be encapsulated.
    """

    def __init__(self, file):
        """
        A feeder should be initialized to read from it's source when commanded to
        via the load_feeds method. Feeds is a list of Feed objects specifically,
        and is interacted with in the feeds generator call.

        `file`: The file to process later.
        """
        self.feeds = []
        self.path = file

    def contents(self):
        """
        Returns the feeds the Feeder is holding on to via a generator structure.
        """
        for feed in self.feeds:
            yield feed
            # feeds = []

    def fetch(self):
        """
        Calling this method should retrieve information using the file provided
        at initialization. Following that, that information will be standardized
        into a Feed object and stored in the feeds instance variable. This is going
        to be implemented in very different ways, depending on the file type used.
        """
        pass

class RssFeeder(Feeder):
    """
    `Author`: Eliakah kakou

    This class generates a list of feed instances containing relevant data about the file
    """

    def __init__(self, file):
        """
        `Author`: Eliakah kakou

        The constructor, initializes the RssFeeded instance

        `file`: path to file containing list of links
        """
        Feeder.__init__(self, file)
        self.feeds = []
        self.links = []
        self.path = file
        self.__getLinks(file)

    def load_feeds(self):
        """
        `Author`: Eliakah kakou

        returns 'feeds' which contains all of the feed instances
        """
        self.fetch()
        return self.feeds

    def __getLinks(self, file):
        """
        `Author`: Eliakah kakou

        This method inserts each link from the file as an entry into the 'links' list

        `file`:path to file containing list of links
        """
        input_file = open(file)
        try:
            for i, line in enumerate(input_file):
                self.links.append(line)
                # print line,
        finally:
            input_file.close()

    def fetch(self):
        """
        `Author`: Eliakah kakou

        This method appends all of the feed instances to 'feeds'
        """
        for i in range(len(self.links)):
            self.feeds.extend(self.__getFeeds(self.links[i]))

    def __getFeeds(self, url):
        """
        `Author`: Eliakah kakou

        This method generates a Feed instance from the url given

        `file`: full file path

        `return`: Feed generated from file
        """
        flag = validators.url(url)
        if flag:
            feeds = feedparser.parse(url)
        else:
            sys.exit("Invalid Url: Please try again!")

        list = feeds['entries']
        for i in range(len(list)):
            list[i] = Feed(list[i])
            feeds = list

        return feeds

class DocFeeder(Feeder):
    """
     `Author`: Eliakah kakou

     This class recursively access each file in a given directory,
     then generates a list of feed instances containing relevant data about the file
    """

    def __init__(self, file):
        """
        `Author`: Eliakah kakou

        The constructor, initializes the DocFeeded instance

        `file`: directory path
        """
        Feeder.__init__(self, file)
        self.feeds = []
        self.paths = []
        self.path = file
        self.__getPaths(file)

    def __getPaths(self, file):
        """
        `Author`: Eliakah kakou

        This method inserts each link from the file as an entry into the 'paths' list

        `file`: path to original directory
        """
        #print "getting Paths"
        matches = []
        for filename in self.__find_files(file, "*.*"):
            matches.append(filename)
        self.paths = matches

    def __find_files(self, directory, pattern):
        """
        `Author`: Eliakah kakou

        This method yields every single file in the directory matching the pattern

        `directory`: directory path

        `pattern`: file name pattern

        `return`: file paths matching pattern
        """
        #print "finding files"
        for root, dirs, files in os.walk(directory):
            for basename in files:
                if fnmatch.fnmatch(basename, pattern):
                    filename = os.path.join(root, basename)
                    yield filename

    def fetch(self):
        """
        `Author`: Eliakah kakou

        This method appends all of the feed instances to 'feeds'
        """
        #print "fetching ..."
        for i in range(len(self.paths)):
            self.feeds.append(self.__getFeeds(self.paths[i]))

    def load_feeds(self):
        """
        `Author`: Eliakah kakou

        `return`: 'feeds' which contains all of the feed instances
        """
        #print "Loading feeds"
        self.fetch()
        return self.feeds

    def __getFileName(self, path):
        """
        `Author`: Eliakah kakou

        This method extracts the file name from the path

        `path`: full path to the file

        `return`: the file name
        """
        drive, path = os.path.splitdrive(path)
        path, filename = os.path.split(path)
        name = filename.split(".")
        return name[0]

    def __getFeeds(self, file):
        """
        `Author`: Eliakah kakou

        This method generates a Feed instance from the file given

        `file`: full file path

        `return`: Feed generated from file
        """
        title = self.__getFileName(file)
        input_file = open(file)
        try:
            content = input_file.read()
        finally:
            input_file.close()

        stamp = time.ctime(self.__creation_date(file))

        f = {'id': file, 'published': stamp, 'title': title, 'link': file, 'summary': content}
        f = Feed(f)
        return f

    def __creation_date(self, file):
        """
        `Author`: Eliakah kakou

        This method returns the creation date of the file,
        or replaces with the last modified if the former is unavailable

        `return`: date on which file was created
    ...
        """
        if platform.system() == 'Windows':
            return os.path.getctime(file)
        else:
            stat = os.stat(file)
            try:
                return stat.st_birthtime
            except AttributeError:
                return stat.st_mtime

if __name__ == "__main__":
    feeder = RssFeeder('links.txt')
    feeder.fetch()
    feeds = feeder.feeds

    for i in range(len(feeds)):
        pprint.pprint(feeds[i].extract())

        # feeder = DocFeeder('C:\Users\Research\Desktop\parking')
        # feeds = feeder.load_feeds()
        #
        # for i in range(len(feeds)):
        #     pprint.pprint(feeds[i].extract())


from ldf_server.backends.backend import Backend, TriplesResult
from rdflib import Graph

class RDFLibBackend(Backend):
    def __init__(self, graph_uri):
        self.graph = Graph().parse(graph_uri, format="turtle")

    def triples(self, triple_pattern, start_index=''):

        triples = sorted(
            self.graph.triples(triple_pattern)
        )
        total_triples = len(triples)
        try:
            start = int(start_index)
        except:
            start = 0

        page_size = 100
        next_start = start + page_size

        if total_triples < next_start:
            next_start = None

        return TriplesResult(
            total_triples,
            next_start,
            triples[start:start+page_size]
        )


# Method Mine
n = int(input())
num_list = list(map(int,input().split()))

max_sum = 0
selected_num = 0

for x in num_list:
  tmp = 0
  for y in str(x):
    tmp += int(y)
  if tmp > max_sum:
    max_sum = tmp
    selected_num = x

print(selected_num)

# Method Lecture
def digit_sum(x):
  sum = 0

  while x > 0:
    sum += x % 10
    x //= 10

  return sum

n = int(input())
num_list = list(map(int,input().split()))

max_sum = -2147000000
selected_num = 0

for x in num_list:
  tot = digit_sum(x)
  if tot > max_sum:
    max_sum = tot
    selected_num = x

print(selected_num)

6. 자릿수의 합.py


import flask
import neomodel
import redis
import uuid

from flask import Flask, jsonify, request
from flask_api import status
from data_models import Person, Place, WentToPlaceRel, ContactWithRel
from neomodel import config
from utilities import make_response

def create_new_user():
    content = request.get_json()
    try:
        phone_number = content['phoneNumber']
    except:
        phone_number = None
    if (phone_number == None):
        return jsonify({'response': 'bad request, please try again and specify a \'phoneNumber\' parameter in the JSON request body'}, status.HTTP_400_BAD_REQUEST)
    else:
        try:
            #iF THE PERSON IS Found properly, this applies
            existingPerson = Person.nodes.get(phoneNumber=phone_number)
        except (neomodel.exceptions.MultipleNodesReturned):
            return jsonify({'response': 'The Phone Number' + phone_number + 'is already registered with multiple users'}, status.HTTP_403_FORBIDDEN)
        except (neomodel.exceptions.DoesNotExist):
            #iF THE PERSON IS NOT FOUND, this applies
            newUser = Person(phoneNumber=phone_number).save()
            return jsonify({'response': 'new number created - ' + phone_number}, status.HTTP_200_OK)
        return jsonify({'response': 'The Phoene Number' + phone_number + 'is already a registered user'}), status.HTTP_403_FORBIDDEN

def report_contact(personA, personB):
    return "contact!!!"

def generateID():
    custom_id = uuid.uuid1()
    return custom_id

def ingestPushNotificationAndCreateID(r):
    content = request.get_json()
    #Step 1, check to ensure that push_token field is present
    try:
        push_token = content['push_token']
    except:
        push_token = None
    if (push_token == None):
        return make_response({'response': 'bad request, there was no \'push_token\' property in the request body'}, status.HTTP_400_BAD_REQUEST)
    else:
        #Step 2, generate a custom ID
        custom_ID = generateID()
        #Step 3, store ID in our redis cluster
        r.set(str(custom_ID), push_token)

    return make_response({ "identifier": custom_ID}, status.HTTP_200_OK)


import time
import PyTango as pt
from PyTango.server import Device, DeviceMeta
from PyTango.server import attribute
from PyTango.server import device_property

class PhaseshifterDS(Device):
    __metaclass__ = DeviceMeta

    phase = attribute(label="Phase",
                      dtype=float,
                      access=pt.AttrWriteType.READ_WRITE,
                      unit="deg",
                      format="3.2f",
                      min_value=0.0,
                      max_value=720.0,
                      fget="get_phase",
                      fset="set_phase",
                      doc="RF phase",
                      memorized=True,
                      hw_memorized=True)

    dac_ds_name = device_property(dtype=str,
                                  doc="Name of the underlying AD5370 DAC device server")
    phase_volt_cal = device_property(dtype=float,
                                     doc="Calibration factor in degrees/volt",
                                     default_value=36.0)
    dac_channel = device_property(dtype=int,
                                  doc="Channel connected to the phase shifter",
                                  default_value=39)

    def init_device(self):
        self.debug_stream("In init_device:")
        Device.init_device(self)
        self.phase_val = 0.0
        self.dac_dev = pt.DeviceProxy(self.dac_ds_name)
        self.set_state(pt.DevState.ON)

    def get_phase(self):
        self.debug_stream("In get_phase:")
        return self.phase_val, time.time(), pt.AttrQuality.ATTR_VALID

    def set_phase(self, new_phase):
        self.debug_stream("In set_phase: New phase " + str(new_phase))
        self.phase_val = new_phase
        self.dac_dev.write_attribute("".join(("channel", str(self.dac_channel))), new_phase / self.phase_volt_cal)

if __name__ == "__main__":
    pt.server.server_run((PhaseshifterDS,))


import os
import shutil
from urllib3 import PoolManager, Retry
from urllib3.exceptions import HTTPError
from typing import Union
from .store import BinaryStore

http = PoolManager(retries=Retry(connect=5, read=2, redirect=5))

class WebStore:
    """
    Downloads files into `BinaryStore`s.
    """
    @staticmethod
    def get(url: str, file_name: str = None, download_store: BinaryStore = None) -> Union[str, None]:
        """
        Download a file into the web store.

        :param url: The URL to download
        :param file_name: The name to give to the downloaded file. `None` for name extraction from the URL
        :param download_store: The store to download the file in. `None` for temporary store.
        :return: The downloaded file path
        """
        if file_name is None:
            file_name = url
            if '?' in file_name:
                file_name, _ = file_name.split('?', 1)
            if '/' in file_name:
                _, file_name = file_name.rsplit('/', 1)

        if download_store is None:
            download_store = BinaryStore()

        # Check if file was downloaded previously
        if download_store.isfile(file_name):
            return download_store.get_path(file_name)

        # Download the file
        try:
            with http.request('GET', url, preload_content=False) as response:
                if response.status != 200:
                    # Server couldn't return the file
                    return None

                with download_store.open(file_name, 'wb') as file_obj:
                    shutil.copyfileobj(response, file_obj)

        except HTTPError:
            return None

        # Download finished, return the file path
        return download_store.get_path(file_name)

    @staticmethod
    def decompress(url: str, compression: str = None, store: BinaryStore = None, **kwargs) -> Union[BinaryStore, None]:
        """
        Downloads a zip file and unzips it.

        :param url: The url of the archive.
        :param compression: The type of the compression. If `None` then the compression is decided based on the suffix
            of the url.
        :param store: The store to unzip the file into it. `None` for temporary store.
        :param kwargs: Extra keyword arguments to pass to `BinaryStore`'s relevant extraction function
        :return: The store the archive was extracted to.
        """
        if compression is None:
            if os.path.extsep not in url:
                raise ValueError(f'Compression not specified')

        if store is None:
            store = BinaryStore()

        compressed = WebStore.get(url)
        if compressed is None:
            return None

        store.decompress(compressed, compression, **kwargs)
        return store


import torch
import pdb
import torch.nn as nn
import math
from torch.autograd import Variable
from torch.autograd import Function

import numpy as np

def Binarize(tensor,quant_mode='det'):
    if quant_mode=='det':
        return tensor.sign()
    else:
        return tensor.add_(1).div_(2).add_(torch.rand(tensor.size()).add(-0.5)).clamp_(0,1).round().mul_(2).add_(-1)

def Ternarize(tensor):
    th = 0.5
    t = tensor.clone().zero_()
    t[tensor.ge(th)] = 1.
    t[tensor.le(-th)] = -1.
    return t

class STETernery(torch.autograd.Function):
    @staticmethod
    def forward(ctx, input, inplace):
        if inplace:
            ctx.mark_dirty(input)

        output = Ternarize(input)
        return output

    @staticmethod
    def backward(ctx, grad_output):
        """
        Straight Through Estimator
        """
        return grad_output, None, None, None, None

class TerneryHardTanh(nn.Module):
    def __init__(self, inplace=True):
        super(TerneryHardTanh, self).__init__()
        self.inplace = inplace
    def forward(self, input):
        input = torch.nn.functional.hardtanh(input)
        input = STETernery.apply(input, self.inplace)
        return input

class HingeLoss(nn.Module):
    def __init__(self):
        super(HingeLoss,self).__init__()
        self.margin=1.0

    def hinge_loss(self,input,target):
            #import pdb; pdb.set_trace()
            output=self.margin-input.mul(target)
            output[output.le(0)]=0
            return output.mean()

    def forward(self, input, target):
        return self.hinge_loss(input,target)

class SqrtHingeLossFunction(Function):
    def __init__(self):
        super(SqrtHingeLossFunction,self).__init__()
        self.margin=1.0

    def forward(self, input, target):
        output=self.margin-input.mul(target)
        output[output.le(0)]=0
        self.save_for_backward(input, target)
        loss=output.mul(output).sum(0).sum(1).div(target.numel())
        return loss

    def backward(self,grad_output):
       input, target = self.saved_tensors
       output=self.margin-input.mul(target)
       output[output.le(0)]=0
       import pdb; pdb.set_trace()
       grad_output.resize_as_(input).copy_(target).mul_(-2).mul_(output)
       grad_output.mul_(output.ne(0).float())
       grad_output.div_(input.numel())
       return grad_output,grad_output

def non_zeroQ(input, numBits=4):
    output = input / 2 / torch.max(torch.abs(input)) + 1 / 2
    output=output.mul(2**numBits-1).round().div(2**numBits-1)
    output = 2 * output - 1
    return output

def Quantize(tensor,quant_mode='det',  params=None, numBits=8):
    if quant_mode=='det':
        tensor.clamp_(-2**(numBits-1),2**(numBits-1))
        tensor = non_zeroQ(tensor, numBits=numBits)
    else:
        tensor=tensor.mul(2**(numBits-1)).round().add(torch.rand(tensor.size()).add(-0.5)).div(2**(numBits-1))
        quant_fixed(tensor, params)
    return tensor
# import torch.nn._functions as tnnf

class BinarizeLinear(nn.Linear):

    def __init__(self, *kargs, **kwargs):
        super(BinarizeLinear, self).__init__(*kargs, **kwargs)

    def forward(self, input):
        # if input.size(1) != 784:
        #     # input.data=Quantize(input.data, numBits=2)
        #     # input.data=Binarize(input.data)
        #     input = STETernery.apply(input, True)
        if not hasattr(self.weight,'org'):
            self.weight.org=self.weight.data.clone()
        self.weight.data=Binarize(self.weight.org)
        out = nn.functional.linear(input, self.weight)
        if not self.bias is None:
            self.bias.org=self.bias.data.clone()
            out += self.bias.view(1, -1).expand_as(out)

        return out

class BinarizeConv2d(nn.Conv2d):

    def __init__(self, *kargs, **kwargs):
        super(BinarizeConv2d, self).__init__(*kargs, **kwargs)

    def forward(self, input):
        # if input.size(1) != 3:
        #     # input.data=Quantize(input.data, numBits=2)
        #     # input.data = Binarize(input.data)
        #     input = STETernery.apply(input, True)
        if not hasattr(self.weight,'org'):
            self.weight.org=self.weight.data.clone()
        self.weight.data=Binarize(self.weight.org)

        out = nn.functional.conv2d(input, self.weight, None, self.stride,
                                   self.padding, self.dilation, self.groups)

        if not self.bias is None:
            self.bias.org=self.bias.data.clone()
            out += self.bias.view(1, -1, 1, 1).expand_as(out)

        return out