import numpy as np
from scipy.stats.kde import gaussian_kde
import matplotlib.pyplot as plt
from multiprocessing import Pool
import sys
import os

# Get the projection data
# Maybe should be reworked with numpy
def get_data_from_file(file_name):
	file = open(file_name, 'r')
	proj = []
	line = file.readline()
	while line.find('&') == -1:
		if line.find('@') == -1:
			proj.append(float(line.split()[0]))
		line = file.readline()
	file.close()
	return proj

# Calculate the distribution
# Get bins and put data to the bins
def KDE(data, grid):
    delta = (grid[1] - grid[0])
    dist = [0.] * len(grid)
    k = 0
    if type(data) is np.ndarray:
        for value in data:
        	dist[int(round((value - grid[0]) / delta))] += 1.
        	k+=1
    else:
    	dist[int(round((data - grid[0]) / delta))] += 1.
    	k+=1
    return [x/(k*delta) for x in dist]

# Plotting
def plot_dist(data, PATH, PC):
	N = PC
	data = np.array(data)
	#KDEpf = gaussian_kde(data)
	print(np.amin(data))
	x = np.linspace(np.amin(data), np.amax(data), 100)
	dist = KDE(data,x)
	plt.ylabel('Probability density (a.u.)')
	plt.xlabel('PC projection value (A)')
	plt.title('Distribution of the PC%s projection' % N)
	plt.plot(x, dist, 'r', color = 'blue')
	plt.savefig(PATH + 'PC%s_dist.png' % N)
	plt.clf()

def main(file_name, range_fnames):
	PATH = os.getcwd() + '/'
	# if both file or range of files are not defined
	if not file_name and not range_fnames:
		print("The projections files have to be provided.\n\
		Use pcalipids.py projdist -h for help")
	# if range is defined
	elif range_fnames:
		# define file name for the first projection file in list
		firstFile = range_fnames[:range_fnames.find('-')]
		# define file name for the last projection file in list
		lastFile  = range_fnames[range_fnames.find('-')+1:]
		# find first PC
		first = int(firstFile[firstFile.rfind('_')+1:firstFile.rfind('.')])
		# find last PC
		last = int(lastFile[lastFile.rfind('_')+1:lastFile.rfind('.')])
		# define filemask
		mask = firstFile[:firstFile.rfind('_')]
		# define filerez
		rez = firstFile[firstFile.rfind('.'):]
		for i in range(first,last+1):
			proj = get_data_from_file(PATH + mask + "_" + str(i) + rez)
			proj = [(proj,PATH,i)]
			with Pool(2) as p:
				p.starmap(plot_dist, proj)
	# if file is defined
	else:
		proj = get_data_from_file(PATH + file_name)
		PC = int(file_name[file_name.rfind('_')+1:file_name.rfind('.')])
		proj = [(proj,PATH,PC)]
		with Pool(2) as p:
			p.starmap(plot_dist, proj)


from pathlib import Path
from configparser import ConfigParser
from setuptools import setup, find_packages

config = ConfigParser()
config.read('setup.cfg')

setup(
    name="pymino",
    license="MIT",
    author="forevercynical",
    author_email="me@cynical.gg",
    description="Easily create a bot for Amino Apps using a modern easy to use synchronous library.",
    long_description=(Path(__file__).parent / "README.md").read_text(),
    long_description_content_type="text/markdown",
    url="https://github.com/forevercynical/pymino",
    version = config.get('metadata', 'version'),
    packages=find_packages(),
    install_requires=[
        "requests==2.31.0",
        "colorama==0.4.6",
        "websocket-client==1.6.1",
        "diskcache==5.6.1",
        "aiohttp==3.8.4"
    ],
    keywords=[
        "amino",
        "pymino",
        "narvii",
        "amino-api",
        "narvii-bots",
        "aminoapps",
        "amino-bot",
        "amino-bots"
    ],
    classifiers=[
        "Programming Language :: Python :: 3",
        "Programming Language :: Python :: 3.8",
        "Programming Language :: Python :: 3.9",
        "Topic :: Software Development :: Libraries :: Python Modules",
    ],
    python_requires=">=3.8"
)


from collections import Counter

def solution(want, number, discount):
    answer = 0
    dictionary = dict()

    for i in range(len(want)):
        dictionary[want[i]] = number[i]

    for i in range(len(discount)-9):
        if dictionary == Counter(discount[i:i+10]):
            answer += 1
    return answer

print(solution(["banana", "apple", "rice", "pork", "pot"], [3, 2, 2, 2, 1], ["chicken", "apple", "apple", "banana", "rice", "apple", "pork", "banana", "pork", "rice", "pot", "banana", "apple", "banana"]))


# s = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# c = []
#
# for i in range(1,11):
#     # d = i * i
#     c.append(i * i)
# print(c)

# lst = []
# n = int(input("Колич элементов"))
# for num in range(n):
#     x = int(input("Кратное 3 число"))
#     list.append(x)
# print(lst)

a = [1, 2, 3]
b = [11, 22, 33]
c = []
for i in range(len(a)):
    c.append(a[i])
    c.append(b[i])
print(c)

d = []
n=int(input('Введите количество циклов: '))
for i in range(n):
    x=int(input('Введите число: '))
    d.append(x)
print(d)
k=int(input('Введите index: '))
d.pop(k)
print(d)


# -*- coding: utf-8 -*-
"""
Get the locations used to fetch tide data from NIWA using the tide API.
sli229
"""

from shapely.geometry import LineString, Point
import geopandas as gpd
from sqlalchemy.engine import Engine

class NoTideDataException(Exception):
    """Exception raised when no tide data is to be used for the BG-Flood model."""
    pass

def get_regional_council_clipped_from_db(
        engine: Engine,
        catchment_area: gpd.GeoDataFrame) -> gpd.GeoDataFrame:
    """
    Retrieve regional council clipped data from the database based on the catchment area.

    Parameters
    ----------
    engine : Engine
        The engine used to connect to the database.
    catchment_area : gpd.GeoDataFrame
        A GeoDataFrame representing the catchment area.

    Returns
    -------
    gpd.GeoDataFrame
        A GeoDataFrame containing the regional council clipped data for the catchment area.
    """
    # Extract the catchment polygon from the GeoDataFrame
    catchment_polygon = catchment_area["geometry"][0]
    # Construct the query to retrieve the regional council clipped data
    query = f"""
    SELECT *
    FROM region_geometry_clipped AS rgc
    WHERE ST_Intersects(rgc.geometry, ST_GeomFromText('{catchment_polygon}', 2193));
    """
    # Execute the query and retrieve the result as a GeoDataFrame
    regions_clipped = gpd.GeoDataFrame.from_postgis(query, engine, geom_col="geometry")
    return regions_clipped

def get_nz_coastline_from_db(
        engine: Engine,
        catchment_area: gpd.GeoDataFrame,
        distance_km: int = 1) -> gpd.GeoDataFrame:
    """
    Retrieve the New Zealand coastline data within a specified distance of the catchment area from the database.

    Parameters
    ----------
    engine : Engine
        The engine used to connect to the database.
    catchment_area : gpd.GeoDataFrame
        A GeoDataFrame representing the catchment area.
    distance_km : int, optional
        Distance in kilometers used to buffer the catchment area for coastline retrieval. Default is 1 kilometer.

    Returns
    -------
    gpd.GeoDataFrame
        A GeoDataFrame containing the New Zealand coastline data within the specified distance of the catchment area.
    """
    # Convert distance from kilometers to meters
    distance_m = distance_km * 1000
    # Buffer the catchment area to create a buffered polygon
    catchment_area_buffered = catchment_area.buffer(distance=distance_m, join_style=2)
    catchment_area_buffered_polygon = catchment_area_buffered.iloc[0]
    # Construct the query to retrieve the New Zealand coastline data within the buffered catchment area
    query = f"""
    SELECT *
    FROM nz_coastlines AS coast
    WHERE ST_Intersects(coast.geometry, ST_GeomFromText('{catchment_area_buffered_polygon}', 2193));
    """
    # Execute the query and retrieve the result as a GeoDataFrame
    coastline = gpd.GeoDataFrame.from_postgis(query, engine, geom_col="geometry")
    return coastline

def get_catchment_boundary_info(catchment_area: gpd.GeoDataFrame) -> gpd.GeoDataFrame:
    """
    Get information about the boundary segments of the catchment area.

    Parameters
    ----------
    catchment_area : gpd.GeoDataFrame
        A GeoDataFrame representing the catchment area.

    Returns
    -------
    gpd.GeoDataFrame
        A GeoDataFrame containing information about the boundary segments of the catchment area.

    Raises
    ------
    ValueError
        If the position of a catchment boundary line cannot be identified.
    """
    # Retrieve the coordinates of the exterior boundary of the catchment area
    boundary_coords = catchment_area["geometry"].iloc[0].exterior.coords
    # Create an empty list to store boundary segment properties
    boundary_segments = []
    # Loop through each boundary line segment
    for i in range(len(boundary_coords) - 1):
        # Get the start and end points of the current boundary segment
        start_point, end_point = boundary_coords[i], boundary_coords[i + 1]
        # Find the centroid of the current boundary segment
        centroid = Point((start_point[0] + end_point[0]) / 2, (start_point[1] + end_point[1]) / 2)
        # Determine the position of the centroid relative to the catchment area
        if centroid.x == min(boundary_coords)[0]:
            position = 'left'
        elif centroid.x == max(boundary_coords)[0]:
            position = 'right'
        elif centroid.y == min(boundary_coords)[1]:
            position = 'bot'
        elif centroid.y == max(boundary_coords)[1]:
            position = 'top'
        else:
            raise ValueError("Failed to identify catchment boundary line position.")
        # Create a LineString object for the current boundary segment
        boundary_geom = LineString([start_point, end_point])
        # Add the boundary segment and its properties to the list
        boundary_segments.append({'line_position': position, 'boundary': boundary_geom})
    # Convert the list of boundary segments to a GeoDataFrame
    boundary_info = gpd.GeoDataFrame(boundary_segments, geometry='boundary', crs=catchment_area.crs)
    # Calculate and add the centroid of each boundary segment as a new column
    boundary_info['centroid'] = boundary_info['boundary'].centroid
    return boundary_info

def get_catchment_boundary_lines(catchment_area: gpd.GeoDataFrame) -> gpd.GeoDataFrame:
    """
    Get the boundary lines of the catchment area.

    Parameters
    ----------
    catchment_area : gpd.GeoDataFrame
        A GeoDataFrame representing the catchment area.

    Returns
    -------
    gpd.GeoDataFrame
        A GeoDataFrame containing the boundary lines of the catchment area.
    """
    # Get the boundary information of the catchment area
    boundary_info = get_catchment_boundary_info(catchment_area)
    # Select the required columns and rename 'boundary' to 'geometry'
    boundary_lines = boundary_info[['line_position', 'boundary']].rename(columns={'boundary': 'geometry'})
    # Set the 'geometry' column as the active geometry column
    boundary_lines = boundary_lines.set_geometry('geometry', crs=catchment_area.crs)
    return boundary_lines

def get_catchment_boundary_centroids(catchment_area: gpd.GeoDataFrame) -> gpd.GeoDataFrame:
    """
    Get the centroids of the boundary lines of the catchment area.

    Parameters
    ----------
    catchment_area : gpd.GeoDataFrame
        A GeoDataFrame representing the catchment area.

    Returns
    -------
    gpd.GeoDataFrame
        A GeoDataFrame containing the centroids of the boundary lines of the catchment area.
    """
    # Get the boundary information of the catchment area
    boundary_info = get_catchment_boundary_info(catchment_area)
    # Select the required columns and rename 'centroid' to 'geometry'
    boundary_centroids = boundary_info[['line_position', 'centroid']].rename(columns={'centroid': 'geometry'})
    # Set the 'geometry' column as the active geometry column
    boundary_centroids = boundary_centroids.set_geometry('geometry', crs=catchment_area.crs)
    return boundary_centroids

def get_non_intersection_centroid_position(
        catchment_area: gpd.GeoDataFrame,
        non_intersection_area: gpd.GeoDataFrame) -> gpd.GeoDataFrame:
    """
    Determine the positions of non-intersection centroid points relative to the boundary lines of the catchment area.

    Parameters
    ----------
    catchment_area : gpd.GeoDataFrame
        A GeoDataFrame representing the catchment area.
    non_intersection_area : gpd.GeoDataFrame
        A GeoDataFrame representing the non-intersection area.

    Returns
    -------
    gpd.GeoDataFrame
        A GeoDataFrame containing the positions of non-intersection centroid points relative to the catchment boundary
        lines. The GeoDataFrame includes the 'position' column denoting the relative position and the 'geometry' column
        representing the centroid points of the non-intersection areas.
    """
    # Get the boundary lines of the catchment area
    boundary_lines = get_catchment_boundary_lines(catchment_area)
    # Explode the non-intersection area to ensure each geometry is a single part
    non_intersections = non_intersection_area.explode(index_parts=False, ignore_index=True)
    # Calculate the centroid for each non-intersection geometry
    non_intersections['centroid'] = non_intersections.centroid
    # Determine the position of each centroid relative to the boundary lines
    for index, row in non_intersections.iterrows():
        centroid = row['centroid']
        # Calculate the distance from the centroid to each boundary line
        distances = {}
        for _, boundary_row in boundary_lines.iterrows():
            distances[boundary_row['line_position']] = centroid.distance(boundary_row['geometry'])
        # Find the name of the closest line based on the minimum distance
        closest_line = min(distances, key=distances.get)
        # Assign the position of the centroid based on the closest line
        non_intersections.at[index, 'position'] = closest_line
    # Select the required columns and rename 'centroid' to 'geometry'
    non_intersections = non_intersections[['position', 'centroid']].rename(columns={'centroid': 'geometry'})
    # Set the 'geometry' column as the active geometry column
    non_intersections = non_intersections.set_geometry('geometry')
    return non_intersections

def get_tide_query_locations(
        engine: Engine,
        catchment_area: gpd.GeoDataFrame,
        distance_km: int = 1) -> gpd.GeoDataFrame:
    """
    Get the locations used to fetch tide data from NIWA using the tide API.

    Parameters
    ----------
    engine : Engine
        The engine used to connect to the database.
    catchment_area : gpd.GeoDataFrame
        A GeoDataFrame representing the catchment area.
    distance_km : int, optional
        Distance in kilometers used to buffer the catchment area for coastline retrieval. Default is 1 kilometer.

    Returns
    -------
    gpd.GeoDataFrame
        A GeoDataFrame containing the locations used to fetch tide data from NIWA using the tide API.

    Raises
    ------
    NoTideDataException
        If no coastline is found within the specified distance of the catchment area.
    """
    # Get the regional council clipped data for the catchment area
    regions_clipped = get_regional_council_clipped_from_db(engine, catchment_area)
    # Determine the non-intersection area within the catchment
    non_intersection_area = catchment_area.overlay(regions_clipped, how='difference')
    if not non_intersection_area.empty:
        # Get the centroid positions of non-intersection areas relative to the catchment boundary lines
        tide_query_location = get_non_intersection_centroid_position(catchment_area, non_intersection_area)
    else:
        # Get the New Zealand coastline data within the specified distance of the catchment area
        coastline = get_nz_coastline_from_db(engine, catchment_area, distance_km)
        if not coastline.empty:
            coastline_geom = coastline['geometry'].iloc[0]
            # Get the centroid positions of the catchment boundary lines
            boundary_centroids = get_catchment_boundary_centroids(catchment_area)
            # Calculate the distance from each boundary centroid to the coastline
            boundary_centroids['dist_to_coast'] = boundary_centroids.distance(coastline_geom)
            # Select the boundary centroid closest to the coastline
            tide_query_location = boundary_centroids.sort_values('dist_to_coast').head(1)
            # Rename the 'line_position' column to 'position' for consistency
            tide_query_location = tide_query_location[['line_position', 'geometry']].rename(
                columns={'line_position': 'position'})
        else:
            # If no coastline is found, raise an exception
            raise NoTideDataException(
                f"No relevant tide data could be found within {distance_km}km of the catchment area. "
                f"As a result, tide data will not be used in the BG-Flood model.")
    # Convert the CRS of the tide query locations to ensure compatibility with the tide API
    tide_query_location = tide_query_location.to_crs(4326).reset_index(drop=True)
    return tide_query_location


#!/usr/bin/env python3.5

import tensorflow as tf
import numpy as np

from parse_data import create_feature_sets_and_labels

data_sets = ['lists/japanese_list.txt',
             'lists/english_list.txt',
             'lists/truncated_latin_list.txt']

#data_sets = ['lists/japanese_list.txt',
#             'lists/english_list.txt']

train_x, train_y, test_x, test_y = create_feature_sets_and_labels(data_sets)

n_nodes_hl1 = 500
n_nodes_hl2 = 500
n_nodes_hl3 = 500

# Latin support will come later...
n_classes = 3
# n_classes = 2

batch_size = 100
# we go through batches of 100 features at a time, so that we do not have to
# load everything into memory at the same time?

# Matrix is height x width
x = tf.placeholder('float', [None,1080])
# imagine a 1 x 784 sized matrix, pixels are all moved into one row from square
# image
y = tf.placeholder('float')
# y is the label of x?

def neural_network_model(data):

    # using a bias because when an input is zero, then neurons do not fire,
    # which is not ideal. this ensures that some neurons will fire even when
    # input is all zeros.

    hidden_1_layer = {'weights': tf.Variable(tf.random_normal([len(train_x[0]), n_nodes_hl1])),
                      'biases': tf.Variable(tf.random_normal([n_nodes_hl1]))}

    hidden_2_layer = {'weights': tf.Variable(tf.random_normal([n_nodes_hl1, n_nodes_hl2])),
                      'biases': tf.Variable(tf.random_normal([n_nodes_hl2]))}

    hidden_3_layer = {'weights': tf.Variable(tf.random_normal([n_nodes_hl2, n_nodes_hl3])),
                      'biases': tf.Variable(tf.random_normal([n_nodes_hl3]))}

    output_layer = {'weights': tf.Variable(tf.random_normal([n_nodes_hl3, n_classes])),
                      'biases': tf.Variable(tf.random_normal([n_classes]))}

    l1 = tf.add(tf.matmul(data, hidden_1_layer['weights']), hidden_1_layer['biases'])
    l1 - tf.nn.relu(l1)
    # relu is our treshold function (rectified linear is the real name)

    l2 = tf.add(tf.matmul(l1, hidden_2_layer['weights']), hidden_2_layer['biases'])
    l2 - tf.nn.relu(l2)

    l3 = tf.add(tf.matmul(l2, hidden_3_layer['weights']), hidden_3_layer['biases'])
    l3 - tf.nn.relu(l3)

    output = tf.matmul(l3, output_layer['weights']) + output_layer['biases']

    return output

def train_neural_network(x):

    prediction = neural_network_model(x)
    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=y))

    # learning rate = 0.001, default
    optimizer = tf.train.AdamOptimizer().minimize(cost)
    # optimizer = tf.train.AdamOptimizer(0.01).minimize(cost)
    hm_epochs = 10
    # how many epochs?

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        # initializes all of the variables, the session is now live!

        writer = tf.summary.FileWriter("logs/", sess.graph)
        for epoch in range(hm_epochs):
            epoch_loss = 0
            i = 0
            while i < len(train_x):
                start = i
                end = i + batch_size

                batch_x = np.array(train_x[start:end])
                batch_y = np.array(train_y[start:end])
                # print(batch_x)
                # print(batch_y)
                # print(type(batch_x))
                # print(type(batch_y))
                # print(len(batch_x))
                # print(len(batch_y))

                # chunks through dataset for you... but in the real world we would
                # have to do this on our own. mnist does this automatically
                _, c = sess.run([optimizer, cost], feed_dict={x:batch_x, y:batch_y})
                epoch_loss += c
                i += batch_size

            print('Epoch', epoch+1, 'completed out of', hm_epochs, 'loss:', epoch_loss)

        correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y,1))
        # checks to see whether we are correct or not

        accuracy = tf.reduce_mean(tf.cast(correct, 'float'))
        # casts correct to type float

        print('Accuracy:', accuracy.eval({x:test_x, y:test_y}))

if __name__ == '__main__':
    train_neural_network(x)


import sys
from dataclasses import dataclass
from pathlib import Path

import numpy as np
import pandas as pd
import utils
from generation.graph import SATGraph

@dataclass
class History:
    directory: Path
    step: pd.DataFrame
    reruns: dict[str, pd.DataFrame]
    episode: pd.DataFrame
    evaluation: pd.DataFrame

    def get_graph(
        self,
        episode: int,
        eval_episode: float = np.nan,
        max_steps: int = sys.maxsize,
    ) -> SATGraph:
        raw_template = self.episode.loc[(episode, eval_episode), "template"]
        template = utils.parse_template(raw_template)  # type: ignore
        if isinstance(template, list):
            graph = SATGraph.from_clauses(template)
        else:
            graph = SATGraph.from_template(template)

        actions = self.step.loc[
            (episode, eval_episode, slice(None)), ["action_0", "action_1"]
        ]
        assert (np.diff(actions.index.get_level_values("step")) == 1).all()

        for (_, _, step), a0, a1 in actions.itertuples(name=None):
            if step >= max_steps:
                break
            graph.merge(a0, a1)
        return graph

    @staticmethod
    def load(
        directory: str | Path,
        load_step: bool = False,
        load_reruns: bool = False,
        load_eval: bool = False,
    ) -> "History":
        if not isinstance(directory, Path):
            directory = Path(directory)

        episode = _read(
            directory / "history_episode", index_cols=["episode", "eval_episode"]
        )
        have_episode_metrics = any(
            col.startswith("metrics/") for col in episode.columns
        )

        if load_step or not have_episode_metrics:
            step = _read(
                directory / "history_step",
                index_cols=["episode", "eval_episode", "step"],
            )
        else:
            step = pd.DataFrame()

        if not have_episode_metrics:
            last_step = step.reset_index(level="step")
            last_step = last_step.loc[~last_step.index.duplicated(keep="last")]
            metric_cols = [
                col for col in last_step.columns if col.startswith("metrics/")
            ]
            episode.loc[:, metric_cols] = last_step.loc[:, metric_cols]

        if not load_step:
            step = pd.DataFrame(columns=step.columns)

        reruns = {}
        if load_reruns:
            rerun_dirs = {
                *directory.glob("rerun*.parquet"),
                *directory.glob("rerun*.csv"),
            }
            for rerun_dir in rerun_dirs:
                reruns[rerun_dir.stem] = _read(rerun_dir, index_cols=["episode", "run"])

        if load_eval:
            eval_ = _read(directory / "eval", index_cols=[], allow_missing=True)
        else:
            eval_ = pd.DataFrame()

        # Update old column names:
        episode.rename(
            columns={"timing/logger": "timing/callbacks"}, inplace=True, errors="ignore"
        )
        step.rename(
            columns={"timing/logger": "timing/callbacks"}, inplace=True, errors="ignore"
        )

        return History(
            directory=directory,
            step=step,
            reruns=reruns,
            episode=episode,
            evaluation=eval_,
        )

def _read(path: Path, index_cols: list[str], allow_missing: bool=False):
    full_path = path.with_suffix(".parquet")
    if full_path.exists():
        df = pd.read_parquet(full_path)
    else:
        full_path = path.with_suffix(".csv")
        if full_path.exists():
            df = pd.read_csv(full_path)
        elif allow_missing:
            return pd.DataFrame()
        else:
            msg = str(path.with_suffix(".{parquet,csv}"))
            raise FileNotFoundError(msg)

    if not index_cols:
        return df

    if set(df.index.names) != {None}:
        df.reset_index(inplace=True)

    for col in index_cols:
        if col not in df.columns:
            df[col] = np.nan

    df.set_index(index_cols, inplace=True)

    return df


from __future__ import print_function

import types

debug_events = False

class EventType (object):
    """
    Usage::
        class MyClass (Observable):
            @EventType
            def MY_EVENT(param1, param2):
                pass

            def __init__(self, arg1, arg2):
                super(MyClass, self).__init__()

            def something_that_fires_an_event():
                param1, param2 = something()
                self.notify(MyClass.MY_EVENT, param1, param2)

    It is not recommended that the prototype function uses default parameters.
    There's no way to support them on Pythons other than CPython.
    """
    def __init__(self, prototype_function):
        self.prototype_function = prototype_function

    def __str__(self):
        return self.prototype_function.func_name

    def __repr__(self):
        return 'EventType({})'.format(self.prototype_function.func_name)

    @property
    def prototype_argcount(self):
        return self.prototype_function.func_code.co_argcount

    @property
    def prototype_has_args(self):
        return bool(self.prototype_function.func_code.co_flags & 0b100)

    @property
    def prototype_has_kwargs(self):
        return bool(self.prototype_function.func_code.co_flags & 0b1000)

    def matches_signature(self, function):
        # Compare argument lengths for now

        argcount = function.func_code.co_argcount - (1 if isinstance(function, types.MethodType) else 0)
        has_args = bool(function.func_code.co_flags & 0b100)
        has_kwargs = bool(function.func_code.co_flags & 0b1000)

        # Conditions where `function` could not be callable in `prototype_function`'s place:
        # 1. `function` has fewer arguments than `prototype_function` and lacks an *args parameter
        # 2. `function` has more arguments than `prototype_function`, not counting `function`'s default parameters
        # 3. `prototype_function` has an *args parameter but `function` doesn't
        # 4. `prototype_function` has a **kwargs parameter, but `function` doesn't
        #     - In theory, you might be safe if `function` always implements the arguments that are passed through
        #       **kwargs, but that's not type safety. Don't define kwargs on the interface if you can't use them in
        #       the implementation!

        # Without using the inspect module (which only works on CPython, not PyPy or other verions), I cannot determine:
        # a. Any of `prototype_function`'s default parameters, which could be used on `function`
        # b. Whether any of `function`'s parameters have defaults, which would allow it to safely have more parameters than `prototype_function`
        #     - This makes check #2 impossible

        return (has_args or (not self.prototype_has_args and self.prototype_argcount <= argcount)) \
            and (has_kwargs or not self.prototype_has_kwargs)

class Observable (object):
    """Data structure useful for implementing the Observable pattern"""

    class Handle (object):
        """Utility class for Observable objects"""
        def __init__(self, callback, event_type, limit, manager):
            #TODO might need to use weak references
            self._callback = callback
            self._event_type = event_type
            self._limit = limit
            self._manager = manager

        def cancel(self):
            """Removes the callback from its original Observable"""
            self._manager.unobserve(self._event_type, self)

    def __init__(self, supported_event_types=set(), scan_for_event_types=True):
        """
        :param supported_event_types: Optional list of permitted `EventType`s
        :param scan_for_event_types: Search through the class's attributes for
                                     EventType objects
        :return:
        """
        self._observer_handles = {}

        self._supported_event_types = supported_event_types \
            if type(supported_event_types) == set \
            else set(supported_event_types)

        if scan_for_event_types:
            self._supported_event_types |= _find_event_types(type(self))

    @staticmethod
    def _find_event_types(clazz):
        return {
            attr_value
            for attr_name in dir(clazz)
            for attr_value in [getattr(clazz, attr_name)]
            if isinstance(attr_value, EventType)
        }

    def observe(self, event_type, callback, limit=float('inf')):
        """Registers a callback
        :param callback: Function to call on notify() for corresponding
                         event_type
        :param event_type: Key designating the event's type.
        :param limit: Optional maximum number of times to execute callback.
                      By default, callbacks are immortal.
        :return: A Handle which may be used to cancel()'ed to remove
                 it from the Observable
        """
        if event_type not in self._supported_event_types:
            raise KeyError('Event type {} is not supported by this Observable'.format(repr(event_type)))
        elif 'matches_signature' in dir(event_type) and not event_type.matches_signature(callback):
            raise TypeError('Callback {} ({} args) cannot be called by event type {} ({} args)'.format(
                repr(callback), callback.func_code.co_argcount,
                repr(event_type), event_type.prototype_argcount,
            ))
        else:
            if not(event_type in self._observer_handles):
                self._observer_handles[event_type] = []

            handle = Observable.Handle(callback, event_type, limit, self)
            self._observer_handles[event_type].append(handle)
            return handle

    def unobserve(self, event_type, handle):
        """Stop calling the Handle's callback for event_type
        :param handle:
        :param event_type:
        :return:
        """
        self._observer_handles[event_type].remove(handle)

    # noinspection PyProtectedMember
    def notify(self, event_type, *args, **kwargs):
        """ Calls all observers with a corresponding event_type in the order they were registered
        :param event_type: Matches add():event_type
        :param args: Ordered arguments to pass to the callbacks
        :param kwargs: Named arguments to pass to the callbacks
        :return: None
        """

        if debug_events:
            print('[Event] {}: args={} kwargs={}'.format(event_type, repr(args), repr(kwargs)))

        if self._supported_event_types and not(event_type in self._supported_event_types):
            raise KeyError('Event type {} is not supported by this Observable'.format(repr(event_type)))

        elif event_type in self._observer_handles:
            observers = self._observer_handles[event_type]

            # Call each listener and decay it
            for listener in list(observers):
                listener._callback(*args, **kwargs)
                listener._limit -= 1

            # Cull expired callbacks
            observers[:] = [
                listener
                for listener in observers
                if listener._limit != 0
            ]

            # Cull childless event_types
            if len(observers) == 0:
                del self._observer_handles[event_type]


import numpy as np
import matplotlib.pyplot as plt
import scipy.integrate as integrate
from scipy.linalg import lstsq

num=3
def exp(x):
    return np.exp(x)
def coscos(x):
    return np.cos(np.cos(x))
def u(x,f,k):
    return f(x)*np.cos(k*x)
def v(x,f,k):
    return f(x)*np.sin(k*x)
def coefficients_vs_n(func,name):
    global num
    n=[i for i in range(51)]
    a=np.zeros(51,dtype=float)
    a[0]=integrate.quad(func,0,2*np.pi)[0]/(2*np.pi)
    for i in range(1,50,2):
        a[i]=integrate.quad(u,0,2*np.pi,args=(func,(i//2)+1))[0]/np.pi
        a[i+1]=integrate.quad(v,0,2*np.pi,args=(func,(i//2)+1))[0]/np.pi
    plt.semilogy(n,np.abs(a),'ro')
    plt.xlabel(r'n$\rightarrow$')
    plt.ylabel(r'Magnitude of coefficients $\rightarrow$')
    plt.title('semilogy plot for magnitude of the coefficients of {} vs n'.format(name))
    plt.legend()
    plt.savefig('Figure{}.png'.format(num))
    plt.grid()
    plt.show()
    num=num+1
    plt.loglog(n,np.abs(a),'ro')
    plt.xlabel(r'n$\rightarrow$')
    plt.ylabel(r'Magnitude of coefficients $\rightarrow$')
    plt.title('loglog plot for magnitude of the coefficients of {} vs n'.format(name))
    plt.legend()
    plt.savefig('Figure{}.png'.format(num))
    plt.grid()
    plt.show()
    num=num+1
    return a
def least_squares_approach(func,a,name):
    global num
    n=[i for i in range(51)]
    A=np.zeros((400,51))
    A[:,0]=1
    xi=np.linspace(0,2*np.pi,401)
    xi=xi[:-1]
    b= func(xi)
    for k in range(1,26):
        A[:,2*k-1]=np.cos(k*xi)
        A[:,2*k]=np.sin(k*xi)
    c=np.linalg.lstsq(A,b,rcond = None)[0]
    plt.semilogy(n,np.abs(c),'go',label='Least squares approach')
    plt.semilogy(n,np.abs(a),'ro',label='Fourier series approach')
    plt.xlabel(r'n$\rightarrow$')
    plt.ylabel(r'Magnitude of coefficients$\rightarrow$')
    plt.title('semilogy plot for magnitude of the coefficients of {} vs n'.format(name))
    plt.legend()
    plt.savefig('Figure{}.png'.format(num))
    plt.grid()
    plt.show()
    num=num+1
    plt.loglog(n,np.abs(c),'go',label='Least squares approach')
    plt.semilogy(n,np.abs(a),'ro',label='Fourier series approach')
    plt.xlabel(r'n$\rightarrow$')
    plt.ylabel(r'Magnitude of coefficients$\rightarrow$')
    plt.title('loglog plot for magnitude of the coefficients {} vs n'.format(name))
    plt.legend()
    plt.savefig('Figure{}.png'.format(num))
    plt.grid()
    plt.show()
    num=num+1
    return c,A
def compare(a,c1,name):
    global num
    deviation=abs(a-c1)
    n=[i for i in range(51)]
    coloumn=np.argmax(deviation)
    plt.plot(n,deviation,'ro')
    plt.plot(coloumn,deviation[coloumn],'dg',label=r"maximum deviation at %d= %f"%(coloumn,deviation[coloumn]))
    plt.xlabel(r'n$\rightarrow$')
    plt.ylabel(r'Deviation$\rightarrow$')
    plt.title('Deviation between least squares approach and direct integration({})'.format(name))
    plt.legend()
    plt.savefig('Figure{}.png'.format(num))
    plt.show()
    num+=1
x=np.linspace(-2*np.pi,4*np.pi,500)
plt.semilogy(x,exp(x))
plt.grid(True)
plt.xlabel(r'x$\rightarrow$')
plt.ylabel(r'$e^{x}\rightarrow$')
plt.title('semilogy plot of $e^{x}$')
plt.savefig('Figure1.png')
plt.legend()
plt.show()
plt.plot(x,coscos(x))
plt.grid()
plt.xlabel(r'x$\rightarrow$')
plt.ylabel(r'$\cos(\cos(x))\rightarrow$')
plt.title('Plot of $\cos(\cos(x))$')
plt.legend()
plt.savefig('Figure2.png')
plt.show()

a1=coefficients_vs_n(exp,name='$e^x$')
a2=coefficients_vs_n(coscos,name='$\cos(\cos(x))$')
c1,A=least_squares_approach(exp,a1,name='$e^x$')
c2,B=least_squares_approach(coscos,a2,name='$\cos(\cos(x))$')
compare(a1,c1,name='$e^x$')
compare(a2,c2,name='$\cos(\cos(x))$')

x1 = np.linspace(0,2*np.pi,401)
x1=x1[:-1]

b1=np.matmul(A,c1)
plt.semilogy(x1,b1,'go',label='estimated')
plt.semilogy(x1,exp(x1),label='True function')
plt.grid()
plt.xlabel(r'x$\rightarrow$')
plt.ylabel(r'$e^{x}\rightarrow$')
plt.title('semilogy plot of estimated $e^{x}$ and it\'s estimated function.')
plt.legend()
plt.savefig('Figure{}.png'.format(num))
plt.show()
num+=1

b2=np.matmul(B,c2)
plt.plot(x1,b2,'go',label='estimated')
plt.plot(x1,coscos(x1),label='True function')
plt.grid()
plt.xlabel(r'x$\rightarrow$')
plt.ylabel(r'$\cos(\cos(x))\rightarrow$')
plt.title('plot of $\cos(\cos(x))$ and it\'s estimated function.')
plt.legend()
plt.savefig('Figure{}.png'.format(num))
plt.show()
num+=1


# This is my main code file
"""

comments ddddddddddddddddddddddddddddddd
 ddddddddddddddddd
 ddddddddddddddddd

"""
import logging

name = 'roshan'
age = 30
case = 12
RANK = 1

print(f"my name is {'name'}={name} and "
      f"my age is {'age'}={age}")

print('my name is %s and my age is %s'
      % (name, age))

print('my name is %s' % name)

logging.basicConfig(level=logging.INFO)
logging.info('Test %s %s',
             case, age)

logging.info('my name is %s',
             name)


from django.utils.translation import ugettext_lazy as _
from django.db import models

from shop.models.order import OrderPayment

#######################################################################
# Stripe
#######################################################################

class StripeOrderData(models.Model):
    order_payment = models.OneToOneField(
        OrderPayment,
        on_delete=models.CASCADE,
        primary_key=True
    )
    receipt_url = models.CharField(
        verbose_name=_("Receipt's URL"),
        max_length=1000
    )
    stripe_session_data = models.TextField(
        verbose_name=_("Stripe's Data")
    )
    stripe_payment_data = models.TextField(
        verbose_name=_("Stripe's Payment Data")
    )

    def __str__(self):
        return str(self.order_payment)


import os
import json

"""实现了使用json文件为数据库的，增删改查功能，find、add、del、update

1、系统关键字：find、add、del、update、from staff_table、set、where暂时仅支持小写字母；
2、数据内容区分大小写；
3、单文件、无异常，后续扩展空间比较大。特别在分词这块。

函数：
    数据库函数：
        save_db(file_path=db_file)
        load_db(file_path=db_file)
        init_db()
    索引控制函数：
        id_increase()
        push_message(count, message)
        pull_message()
    数据库操作函数（增、删、改、查）：
        staff_add(info)
        staff_del(id_set)
        staff_update(id_set, info_dict)
        staff_find(id_set, cols)
    命令行分词函数：
        where(condition_dict, compare_list, operator_list = [])
        split_cmd
"""
#保存了一份初始员工信息文件，用于实验数据错乱后恢复。
db_file = r'staff_table.json'
db_file_init = r'staff_table_init.json'

#员工信息表，在运行过程全程保存在内存中（staff_table全局变量）
#维护两个索引（ID，phone）,以及一个消息队列
staff_table = {}
columns = ['id', 'name', 'age', 'phone', 'dept', 'enroll_date']
id_index = []
phone_index = []

message_tmp = {'count': 0, 'message': []}

def save_db(file_path=db_file):
    """把员工信息保存为'staff_table.json'文件
    """
    global staff_table

    with open(file_path, 'w', encoding='UTF-8') as staff_table_f:
        json.dump(staff_table, staff_table_f, ensure_ascii=False, indent=4)

def load_db(file_path=db_file):
    """读取用户信息（id,name,age,phone,dept,enroll_date）
    """
    global staff_table, id_index, phone_index

    with open(file_path, 'r', encoding='UTF-8') as staff_table_f:
        staff_table = json.load(staff_table_f)
        if staff_table:
            staff_table = {int(key): staff_table[key]
                           for key in staff_table
                           }  #json只能存储str格式的key，加载时转换回int

            phone_index = [staff_table[key]['phone'] for key in staff_table]
            id_index = list(staff_table.keys())
        else:
            push_message(0, '员工信息表为空，请输入“init”初始化或使用“add”命令添加数据')

def init_db():
    """通过'staff_table_init.json'文件初始化还原'staff_table.json'文件数据库
    """
    load_db(db_file_init)
    save_db()

    push_message(len(staff_table), '从init文件中还原实验数据成功')

def id_increase():
    """
    提供最新id
    """
    new_id = max(id_index) + 1
    id_index.append(new_id)
    return new_id

def push_message(count, message):
    """把消息推送到消息队列，count是影响到的记录数，message是具体消息。
    """
    message_tmp['count'] += count
    message_tmp['message'].append(message)

def pull_message():
    global message_tmp

    count = message_tmp['count']
    message = message_tmp['message']

    message_tmp = {'count': 0, 'message': []}

    return count, message

def staff_add(info):
    """添加新员工信息

    1、检测新员工手机号是否重复，唯一则允许添加；
    2、新员工id自增+1,并添加传入的新员工信息列表（不包含id），并返回成功信息；
    3、并保存到文件中。

    参数：
        info：新员工信息列表

    返回：
        new_id，添加成功返回新员工id号，失败False
    """
    if len(info) == 5:
        if info[2] not in phone_index:
            new_id = id_increase()  #新员工id自增

            employee = {}
            for i, c in enumerate(columns[1:]):
                employee[c] = info[i]
            staff_table[new_id] = employee

            phone_index.append(info[2])

            push_message(1, '%s号新员工信息添加成功'% new_id)
            save_db()
            return new_id
        else:
            push_message(0, '手机号已存在')
            return False
    else:
        push_message(0, '新员工信息列数不匹配')

def staff_del(id_set):
    """删除对应id的员工信息，并保存到文件
    """
    for id in id_set:
        del staff_table[id]

        id_str_list = [str(id) for id in id_set]
        if id_str_list:
            push_message(len(id_set), ','.join(id_str_list) + ' 号员工信息删除成功')
        else:
            push_message(0, '无删除条目')

    save_db()

def staff_update(id_set, info_dict):
    """
    """
    if info_dict.get('phone') and info_dict['phone'] in phone_index:
        push_message(0, '手机号已存在')
        return False
    elif not set(info_dict.keys()).issubset(set(columns)):
        push_message(0, '提交的修改项中含有未知列')
    else:
        for id in id_set:
            for key in info_dict:
                staff_table[int(id)][key] = info_dict[key]

        id_str_list = [str(id) for id in id_set]
        if id_str_list:
            push_message(len(id_set), ','.join(id_str_list) + ' 号员工信息已修改')
        else:
            push_message(0, '修改结果空')
        save_db()
        return True

def staff_find(id_set, cols):
    """
    """
    if cols[0] == '*':
        cols = columns

    result = []
    for row, id in enumerate(id_set):
        result.append([])
        for c in cols:
            if c == 'id':
                result[row].append(id)
            else:
                result[row].append(staff_table[id][c])
        push_message(1, result[row])

    id_str_list = [str(id) for id in id_set]
    if id_str_list:
        push_message(0, '查询结果为ID: ' + ','.join(id_str_list) + ' 号员工信息')
    else:
        push_message(0, '查询结果空')

def where(condition_dict, compare_list, operator_list = []):
    """将condition字典内的值与staff_table内的相应值做compare（逻辑运算，like）返回符合条件的id。

    可以处理复杂的多重逻辑与集合运算，例 where dept like IT and age = 22;

    参数：
        condition_dict = {column:value, ...}   键值对数目与compare_list相等。
            例中应当传入 {'dept': 'IT', 'age': 22}

        compare_list = [compare_operator, ...]  列表元素数量与condition_dict键值对相等。
            例中应当传入 {'like', '='}

        operate_list = [logical_operate, ...]  列表元素数量比condition_dict键值对少1个。
            例中应当传入 {'and'}
    """
    from functools import reduce

    def compare(value_s, value_t, op):
        """比较传入参数value_s，与staff_table内的value_t，是否符合op关系（大小，等于，like）。
        """
        if op in ['=', '>', '<', '>=', '<=']:
            op = '==' if op == '=' else op
            if isinstance(value_t, str) and isinstance(value_s, str):
                eval_str = '\'%s\''% value_s + op + '\'%s\''% value_t
            else:
                eval_str = str(value_s) + op + str(value_t)
            return True if eval(eval_str) else False
        elif op == 'like':
            return True if value_s in value_t else False

    def __get_operate(operator_list=operator_list):
        """依次取出operate_list内的逻辑操作符，供logical_op函数调用用于集合运算。
        """
        operator = operator_list[0]
        operator_list = operator_list[1:]
        return operator

    def logical_op(x_set, y_set):
        """根据__get_operate函数提供的逻辑操作字符，转化为相应的逻辑操作。

        参数：
            x_set,y_set: 是根据每次遍历condition_dict与compare_list运算获得的集合。
            例中的结果是 x_set = {1, 3, 5, 9}，y_set = {8, 1},本函数仅使用"and"操作符
            运算结果为 {1}
        """
        operate = __get_operate()

        if operate == 'and':
            return x_set & y_set
        elif operate == 'or':
            return x_set & y_set
        else:
            return x_set

    #分别计算condition_dict中的每一个条件与staff_table内的值进行比较运算的结果集
    #存入id_set_list
    if condition_dict:
        id_set_list = []
        for key, cmp in zip(condition_dict, compare_list):
            id_set_inner = set()
            id_set_list.append(id_set_inner)
            for id in staff_table:
                source = condition_dict[key]
                targe = staff_table[id][key] if key != 'id' else id
                if compare(source, targe, cmp):
                    id_set_inner.add(id)

        id_set = reduce(logical_op, id_set_list)  #集合运算
    else:
        id_set = set(id_index)     #没有条件，则返回全部ID

    return id_set  #返回符合条件的ID集合

def split_cmd(cmd):
    cmd = cmd.strip(' \n').split(' ', 1)
    cmd_head, cmd_body = cmd if len(cmd) > 1 else [cmd[0],[]]
    cmd_head = cmd_head.lower()

    def split_where(where_str):
        condition_dict = {}
        operator_list = []

        cmp_str = where_str.strip()    #仅一组逻辑运算，需补充
        key, value, operator = split_cmp(cmp_str)

        condition_dict[key] = value
        operator_list.append(operator)

        id_set = where(condition_dict, operator_list)
        return id_set

    def split_cmp(cmp_str):
        operator = ['=', '>', '<', '>=', '<=', 'like']
        for op in operator:
            if op in cmp_str:
                key, value = cmp_str.split(op)
                return key.strip(' \'\"'), value.strip(' \'\"') , op

    def split_cols(cols_str):
        cols = cols_str.strip().split(',')
        return cols

    if cmd_head in ['q', '退出']:
        exit()
    elif cmd_head == 'init':
        init_db()

    elif cmd_head == 'find':
        cols_str, where_str = cmd_body.split('from staff_table where')
        cols = split_cols(cols_str)
        id_set = split_where(where_str)
        staff_find(id_set, cols)

    elif cmd_head == 'add':
        cols_str = cmd_body.split('staff_table')[1]
        info = split_cols(cols_str)
        staff_add(info)

    elif cmd_head == 'del':
        where_str = cmd_body.split('from staff where')[1]
        id_set = split_where(where_str)
        staff_del(id_set)

    elif cmd_head == 'update':
        info_dict = {}
        cmp_str, where_str = cmd_body.split('set')[1].split('where')
        id_set = split_where(where_str)
        key, value, cmp = split_cmp(cmp_str)
        info_dict[key] = value
        staff_update(id_set, info_dict)
    else:
        push_message(0, '该命令还未设置，请查看命令输入是否出错')

if __name__ == '__main__':
    load_db()
    print('\n'.join(pull_message()[1]))
    while True:
        print(
            "1、find：例 find * from staff_table where age > 22",
            "2、add: 例 add staff_table Alex Li,25,134435344,IT,2015‐10‐29",
            "3、del: 例 del from staff where id=3",
            "4、update: 例 update staff_table set dept='Market' where dept = 'IT'",
            "5、init：还原所有实验数据", "6、q：退出程序",sep='\n')
        cmd = input('\n>>>')
        if not cmd:
            continue
        split_cmd(cmd)
        count, message = pull_message()
        for m in message:
            print(m)
        print('共影响条目%s条\n' % count)


import requests
import pandas as pd
import numpy as np
from time import sleep

sleep(5)

# клиентское приложение использует заранее подготовленные данные
# выбирает из таблицы случайный объект и передает его нв сервер
X = pd.read_csv('app/data/x_eval.csv')

n = np.random.randint(0, X.shape[0]-1)
feature = list(X.loc[n].values)

# если между приложениями установлена связь, предсказание будет выведена на экран
if __name__ == '__main__':
    r = requests.post('http://0.0.0.0/predict', json=feature)
    print(f'Status: {r.status_code}')

    if r.status_code == 200:
        print(f"Prediction: {r.json()}")
    else:
        print(r.text)


class Solution:
    def isPossible(self, target: List[int]) -> bool:
        # Pick Largest element from given array
        # Subtract from sum of rest of the elements
        # Keep doing this until all one
        total = sum(target)
        arr = [-num for num in target]
        heapify(arr) # Created Max Heap using negative value
        while True:
            val = -heappop(arr)
            total -= val
            if val == 1 or total == 1:
                return True
            if val < total or total == 0 or val % total == 0:
                return False
            val %= total
            total += val
            heappush(arr, -val)


from PyQt5 import uic
from PyQt5.QtWidgets import QWidget, QCompleter
import MainWindow as mw
import StockWindow as stw
import DatabaseController as dbc

class PurchaseWindow(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        uic.loadUi("PurchaseWindow.ui", self)
        self.setFixedSize(1900, 980)
        self.homeButton.clicked.connect(self.goHome)
        self.newPurchaseButton.clicked.connect(self.goToNewPurchase)
        self.transportButton.clicked.connect(self.goToTransport)
        self.purchaseTable.setColumnWidth(0, 100)
        self.purchaseTable.setColumnWidth(1, 125)
        self.purchaseTable.setColumnWidth(2, 150)
        self.purchaseTable.setColumnWidth(3, 150)
        self.purchaseTable.setColumnWidth(4, 250)
        self.purchaseTable.setColumnWidth(5, 200)
        self.purchaseTable.setColumnWidth(6, 160)
        self.purchaseTable.setColumnWidth(7, 200)
        self.purchaseTable.setColumnWidth(8, 135)
        self.purchaseTable.setColumnWidth(9, 145)

    def goToNewPurchase(self):
        self.cw = NewPurchaseWindow()
        self.cw.show()
        self.close()

    def goHome(self):
        self.cw = mw.MainWindow()
        self.cw.show()
        self.close()

    def goToTransport(self):
        self.cw = TransportWindow()
        self.cw.show()

class NewPurchaseWindow(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        uic.loadUi("NewPurchaseWindow.ui", self)
        self.setFixedSize(1900, 980)
        self.homeButton.clicked.connect(self.goHome)
        self.backButton.clicked.connect(self.goBack)
        self.addItemButton.clicked.connect(self.goToAddNewItem)
        self.addSupplier.clicked.connect(self.addNewSupplier)
        self.table.setColumnWidth(0, 150)
        self.table.setColumnWidth(1, 650)
        self.table.setColumnWidth(2, 200)
        self.table.setColumnWidth(3, 200)
        self.table.setColumnWidth(4, 200)
        self.table.setColumnWidth(5, 200)
        self.table.setColumnWidth(6, 195)
        try:
            conn = dbc.getConnection()
            cursor = conn.cursor()
            cursor.execute("select company_name from supplier_info")
            supplier_list = cursor.fetchall()
            conn.close()
        except Exception as e:
            print(e)
        try:
            for item in supplier_list:
                self.supplier_CB.addItem(item[0])
        except Exception as e:
            print(e)
        item_list = []
        try:
            conn = dbc.getConnection()
            cursor = conn.cursor()
            cursor.execute("select name from stock")
            items = cursor.fetchall()
            for item in items:
                item_list.append(item[0])
            conn.close()
        except Exception as e:
            print(e)
        completer = QCompleter(item_list)
        self.itemName.setCompleter(completer)

    def addNewSupplier(self):
        self.cw = mw.AddSupplierWindow()
        self.cw.show()
        newSupplier = self.cw.companyName.text()
        self.supplier_CB.addItem(newSupplier)

    def addNewPurchase(self):
        conn = dbc.getConnection()
        cursor = conn.cursor()

    def goHome(self):
        self.cw = mw.MainWindow()
        self.cw.show()
        self.close()

    def goBack(self):
        self.cw = PurchaseWindow()
        self.cw.show()
        self.close()

    def goToAddNewItem(self):
        self.cw = stw.AddNewItemWindow()
        self.cw.show()

class TransportWindow(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        uic.loadUi("TransportWindow.ui", self)
        self.setFixedSize(1100, 650)


import numpy as np
import numpy.typing as npt
from metrics.utils import *

def Variance(
    img: npt.NDArray[np.float64], labels: npt.NDArray[np.int_]
) -> npt.NDArray[np.float64]:
    """
    Compute Superpixel variance.

    Inputs
    ------
    img: np.ndarray (float) shape:(R x C x d)
        Reshaped NumPy array of image data with each row containing a pixel and its features.
    labels: np.ndarray (int) shape:(R x C)
        NumPy array containing superpixel label of each pixel.

    Output
    ------
    kscores: np.ndarray (float) shape:(K x 1)
        Local variance value for each superpixel.
    """
    img = np.reshape(img, (img.shape[0] * img.shape[1], img.shape[2]))
    labels = np.reshape(labels, (labels.shape[0] * labels.shape[1], 1))
    centers = ComputeCenters(img, labels)
    K = get_K(labels)

    kscores = np.full([K, 1], 0.00)

    for k in range(0, K):
        clust = img[np.where(labels == k)[0], :]
        nk = clust.shape[0]

        num = np.sum(np.linalg.norm((clust - centers[k]), None, 1) ** 2)

        kscores[k] = num / nk

    return kscores


from typing import List, Any

from pyutter.widgets.layout import Container
from pyutter.core.primitive import Style, Widget

class Card(Container):
    def __init__(self, children: List[Any], id=None, style: Style = None):
        style = style if style is not None else Style()
        style['box-shadow'] = '0 4px 8px 0 rgba(0,0,0,0.2)'
        style['border-radius'] = '0px'
        style['background'] = 'white'

        super().__init__(children, id, style)

class Chip(Container):
    def __init__(self, children: List[Any], id=None, size: int = 32, style: Style = None):
        style = style if style is not None else Style()
        style['display'] = 'flex'
        style['padding'] = f'0 {size / 2}px'
        style['align-items'] = 'center'
        style['height'] = f'{size}px'
        style['line-height'] = f'{size}px'
        style['border-radius'] = f'{size / 2}px'
        style['background-color'] = '#f1f1f1'
        style['margin-bottom'] = '5px'
        style['margin-right'] = '5px'

        super().__init__(children, id, style)

class AppBar(Widget):
    def __init__(self, children: List[Any], id=None, style: Style = None):
        style = style if style is not None else Style()
        style['box-shadow'] = '0 4px 8px 0 rgba(0,0,0,0.2)'
        style['display'] = 'flex'
        style['height'] = f'64px'
        style['width'] = f'100%'
        style['background'] = 'white'
        style['min-width'] = 'auto'
        # style['position'] = 'fixed'

        # style['box-shadow']= "var(--bs-sm)"
        super().__init__('div', children, id, style)


import logging
from time import time

import numpy as np
import pandas as pd
from pandas.errors import EmptyDataError
from glob import glob
from datetime import datetime
from ntpath import basename

def parse_isd_timestamp_hourly(arg):
    return datetime(year=int(arg[0:4]), month=int(arg[4:6]), day=int(arg[6:8]), hour=int(arg[8:10]),
                    minute=0, second=0, microsecond=0)

def parse_isd_timestamp_minutely(arg, arg_minute_flag):
    minute = int(arg[10:])
    return datetime(year=int(arg[0:4]), month=int(arg[4:6]), day=int(arg[6:8]), hour=int(arg[8:10]),
                    minute=minute, second=0, microsecond=0)

if __name__ == '__main__':
    start_time = time()

    console_formatter = logging.Formatter('%(asctime)s : %(name)s :: %(levelname)s : %(message)s')
    file_formatter = logging.Formatter('%(asctime)s : %(message)s')
    logger = logging.getLogger(__name__)
    logger.setLevel(logging.DEBUG)

    console_handler = logging.StreamHandler()
    console_handler.setFormatter(console_formatter)
    console_handler.setLevel(logging.INFO)
    logger.addHandler(console_handler)

    logger.info('started')

    output_folder = '../output/'
    input_folder = '../../usca_isd_data/'
    yr_modahrmn = 'YR--MODAHRMN'
    usecols = [yr_modahrmn, 'DIR', 'SPD', 'SKC', 'VSB', 'TEMP', 'DEWP', 'SLP', 'ALT']
    usecols = [yr_modahrmn, 'SKC']
    minute_flag = False
    if minute_flag:
        output_file = 'SKC-2018-minutely.csv'
    else:
        output_file = 'SKC-2018-hourly.csv'
    full_output_file = output_folder + output_file
    result_df = None
    count = 0
    na_values = ['*', '**', '***', '****', '*****', '******']
    for input_file in glob(input_folder + '*.txt'):
        try:
            short_name = basename(input_file).replace('-2018.isd.txt', '')
            if minute_flag:
                df = pd.read_csv(input_file, delim_whitespace=True, parse_dates=[yr_modahrmn], usecols=usecols,
                                 date_parser=parse_isd_timestamp_minutely, na_values=na_values)
            else:
                df = pd.read_csv(input_file, delim_whitespace=True, parse_dates=[yr_modahrmn], usecols=usecols,
                                 date_parser=parse_isd_timestamp_hourly, na_values=na_values)
            logger.info('%s %d %s %s' % (short_name, len(df), df[yr_modahrmn].min(), df[yr_modahrmn].max()))
            if result_df is None:
                result_df = df[[yr_modahrmn, 'SKC']].rename(columns={'SKC': short_name})
            else:
                result_df = result_df.merge(df[[yr_modahrmn, 'SKC']].rename(columns={'SKC': short_name}),
                                            on=yr_modahrmn, how='outer')
                result_df = result_df.drop_duplicates(subset=list(result_df)[:-1], keep='last')
                logger.info('after join %d rows and %d columns' % (len(result_df), len(list(result_df))))
                if count % 10 == 5:
                    logger.info('writing result to %s' % full_output_file)
                    result_df.to_csv(full_output_file, index=False)
                count += 1
        except EmptyDataError as empty_data_error:
            logger.warning(empty_data_error)
    logger.info('after final join %d rows and %d columns' % (len(result_df), len(list(result_df))))
    logger.info('writing result to %s' % full_output_file)
    result_df.to_csv(full_output_file, index=False)

    logger.info('done')

    finish_time = time()
    elapsed_hours, elapsed_remainder = divmod(finish_time - start_time, 3600)
    elapsed_minutes, elapsed_seconds = divmod(elapsed_remainder, 60)
    logger.info('Time: {:0>2}:{:0>2}:{:05.2f}'.format(int(elapsed_hours), int(elapsed_minutes), elapsed_seconds))
    console_handler.close()
    logger.removeHandler(console_handler)


import numpy as np
import cv2
from sklearn.decomposition import PCA
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import cross_validate
from sklearn.metrics import accuracy_score

random_state = 330
train_X = np.zeros((360, 2576))
train_Y = np.zeros(360)
test_X = np.zeros((40, 2576))
test_Y = np.zeros(40)

def cv2normalize(x):
    return cv2.normalize(x, None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F)

def load_dataset():
    # There are 40 persons and 10 images for each person.
    # Each grayscale image's size is 56 x 46 (2576 pixels).
    #
    # Need to split the data to:
    #   - training set: 9 * 40 = 360 images
    #       Size: (360, 2576)
    #   - testing set: 1 * 40 = 40 images
    #       Size: (40, 2576)

    for i in range(40):
        for j in range(10):
            imgname = 'p2_data/' + str(i+1) + '_' + str(j+1) + '.png'
            img = cv2.imread(imgname, cv2.IMREAD_GRAYSCALE)
            if j < 9:
                train_X[i*9 + j] = np.reshape(img, (1, -1))
                train_Y[i*9 + j] = i + 1
            else:
                test_X[i] = np.reshape(img, (1, -1))
                test_Y[i] = i + 1

    print('Training set:', train_X.shape)
    print('Testing set:', test_X.shape)
    print('-' * 20)

def pca_trainset():
    # Draw mean face
    mean = np.mean(train_X, axis=0).reshape(1, -1)
    mean_img = mean.reshape(56, 46)

    # cv2.imwrite('2_1-meanface.png', mean_img)
    print('Drawing mean face done.\n')

    # Draw the eigenfaces.
    #   - The training set is (n_samples, n_features)
    pca = PCA(random_state=random_state)
    L = pca.fit(train_X - mean)
    for i in range(0, 4):
        eigenface = L.components_[i].reshape(56, 46)
        eigenface = cv2normalize(eigenface)
        cv2.imwrite('2_1-v' + str(i+1) + '.png', eigenface)
    print('Drawing first 4 eigenfaces done.\n')

    # Reconstruct person2 image1 and project it onto the PCA eigenspace
    n = [3, 50, 170, 240, 345]
    input_img = cv2.imread('p2_data/2_1.png', cv2.IMREAD_GRAYSCALE).reshape(1, -1)
    project = pca.transform(input_img - mean)
    print('Projected:', project.shape)
    print('Eigenspace:', L.components_.shape)
    print('Mean:', mean.shape, '\n')

    print('Reconstructing Person 2 Image 1...')
    for i, j in enumerate(n):
        recon_img = (project[:, :j] @ L.components_[:j]) + mean
        mse = np.mean((input_img - recon_img) ** 2)
        print('n = %3d,' % j, 'MSE =', round(mse, 4))
        cv2.imwrite('2_2-p2i1-re-by-top-' + str(j) + '.png', cv2normalize(recon_img).reshape(56, 46))

    return pca, mean

def crossvalidation(pca, mean):
    print('\nPerforming 3-fold cross validation and selecting best k, n...')
    candidate_k = [1, 3, 5]
    candidate_n = [3, 50, 170]
    k = 1
    n = 3
    max_value = 0
    train_X_reduced = pca.transform(train_X - mean)

    for i in candidate_k:
        for j in candidate_n:
            knn = KNeighborsClassifier(n_neighbors=i)
            scores = cross_validate(knn, train_X_reduced[:, :j], train_Y, cv=3, scoring='accuracy')
            mean_score = round(np.mean(scores['test_score']), 5)
            if (mean_score > max_value):
                k = i
                n = j
                max_value = mean_score
            print('k = %d, n = %3d, scores =' % (i, j), mean_score)

    return k, n

def predict(k, n, pca, mean):
    test_X_reduced = pca.transform(test_X - mean)
    train_X_reduced = pca.transform(train_X - mean)
    knn = KNeighborsClassifier(n_neighbors=k)
    knn.fit(train_X_reduced[:, :n], train_Y)
    pred_Y = knn.predict(test_X_reduced[:, :n])
    accuracy = accuracy_score(y_pred=pred_Y, y_true=test_Y)
    print('\nAccuracy on testing set:', accuracy)

def main():
    load_dataset()
    pca, mean = pca_trainset()
    k, n = crossvalidation(pca, mean)
    predict(k, n, pca, mean)

if __name__ == "__main__":
    main()


import torch
import torch.nn as nn
from sequence_features import prepare_sents_as_inp_vecs_next
from sequence_features import prepare_edus_seq_as_inp_vecs
from general import print_memory_usage

def encoder_forward(lstm1, lstm2, samples, vocab, tag_to_ind_map, bs, dropout_prob, train=True, \
	hidden_size=200):
	use_def = not train
	# Return a list of tensors where each represents a sequence of words
	# inside a sentence, and has a shape of sent_len * vectorized concatenated word tag len
	# Tensors are sorted in reversal order by the number of their rows (sent_len)
	# Extract the inputs of the first Bi-LSTM layer
	for x_vecs, batch_to_sent_ind, batch_to_tree in prepare_sents_as_inp_vecs_next(\
		samples, vocab, tag_to_ind_map, use_def, bs):
		# Return one tensor of dimenion bs * max_sent_len * hidden_size
		states = (torch.randn(2, len(x_vecs), hidden_size), torch.randn(2, len(x_vecs), hidden_size))
		states = detach(states)
		encoded_words, _ = lstm1(x_vecs, states)
		# apply dropout in training only
		encoded_words = nn.functional.dropout(encoded_words, p=dropout_prob, training=train)
		# Based on the encoded words (the outputs of the LSTM) the edus representations are calculated.
		calc_edus_representations(vocab, encoded_words, batch_to_sent_ind, batch_to_tree)

	# Preprare input vectors for the second Bi-LSTM encoder
	# Return a tensor of dimension bs * max edus seq len * (2*hidden_size)
	for x_vecs, batch_to_sample, batch_to_span_ind in prepare_edus_seq_as_inp_vecs(\
		samples, vocab, tag_to_ind_map, bs):
		states = (torch.randn(2, len(x_vecs), hidden_size), torch.randn(2, len(x_vecs), hidden_size))
		states = detach(states)
		encoded_edus, _ = lstm2(x_vecs, states)
		encoded_edus = nn.functional.dropout(encoded_edus, p=dropout_prob, training=train)
		set_encoded_edus(encoded_edus, batch_to_sample, batch_to_span_ind)

def calc_edus_representations(vocab, encoded_words, batch_to_sent_ind, batch_to_tree):
	"""re
		For each edu in tree, calculate its representation by average pooling of
		the encoded words inside that edu. These representations is used as input vectors
		for the EDUS sequence encoder.
	"""
	for i in range(len(batch_to_sent_ind)):
		sent_ind = batch_to_sent_ind[i]
		tree = batch_to_tree[i]
		last_edu_ind = len(tree._EDUS_table) - 1
		if len(tree._edu_represent_table) == 1:
			tree._edu_represent_table += [0] * (len(tree._EDUS_table) - 1)

		edu_ind = tree._sent_to_first_edu_ind[sent_ind]

		while edu_ind <= last_edu_ind and tree._edu_to_sent_ind[edu_ind] == sent_ind:
			# s, t are computed from 1
			s, t = tree._edus_seg_in_sent[edu_ind]
			edu_represent = 0
			for k in range(s, t + 1):
				edu_represent += encoded_words[i, k - 1, :] # dim 400

			tree._edu_represent_table[edu_ind] = edu_represent / (t - s + 1)
			edu_ind += 1

def set_encoded_edus(encoded_edus, batch_to_sample, batch_to_span_ind):
	for i in range(len(encoded_edus)):
		sample = batch_to_sample[i]
		span_ind = batch_to_span_ind[i]
		s, t = sample._spans[span_ind]
		if sample._encoded_spans == []:
			sample._encoded_spans = [0] * (len(sample._spans))
		encoded_span = 0
		for k in range(s, t + 1):
			encoded_span += encoded_edus[i, k - s, :]
		sample._encoded_spans[span_ind] = encoded_span / (t - s + 1)

def detach(states):
	return [state.detach() for state in states]


import csv
import re

with open('formated.csv', 'w', newline='') as g:
    writer = csv.writer(g)
    with open('new1log.csv', newline='') as f:
        reader = csv.reader(f)
        for row in reader:
            s = []
            if re.search("2022-[0-9][0-9]-[0-9][0-9].[0-9][0-9]:[0-9][0-9]:[0-9][0-9].[0-9][0-9][0-9]",row[1][1:24]):
                s.append(row[1])
                x=reader.__next__()
                s.append(x[1])
                while(x[1][:16]!="last_time_stamp:"):
                    if x[1][:6]=="dummy:":
                        print('dummy')
                        break
                    x=reader.__next__()
                    s.append(x[1])
                x=reader.__next__()
                s.append(x[1])
                x=reader.__next__()
                s.append(x[1])
                x=reader.__next__()
                s.append(x[1])
            writer.writerow(s)


from typing import (
    cast,
)

from wasm.datatypes import (
    TableIdx,
    ValType,
)
from wasm.exceptions import (
    ValidationError,
)
from wasm.instructions import (
    BaseInstruction,
    Block,
    Br,
    BrIf,
    BrTable,
    Call,
    CallIndirect,
    Else,
    If,
    Loop,
)
from wasm.opcodes import (
    BinaryOpcode,
)

from .context import (
    ExpressionContext,
)

def validate_control_instruction(instruction: BaseInstruction, ctx: ExpressionContext) -> None:
    """
    Validate a single control instruction.
    """
    if instruction.opcode is BinaryOpcode.UNREACHABLE:
        validate_unreachable(ctx)
    elif instruction.opcode is BinaryOpcode.BLOCK:
        validate_block(cast(Block, instruction), ctx)
    elif instruction.opcode is BinaryOpcode.END:
        validate_end(ctx)
    elif instruction.opcode is BinaryOpcode.IF:
        validate_if(cast(If, instruction), ctx)
    elif instruction.opcode is BinaryOpcode.ELSE:
        validate_else(cast(Else, instruction), ctx)
    elif instruction.opcode is BinaryOpcode.LOOP:
        validate_loop(cast(Loop, instruction), ctx)
    elif instruction.opcode is BinaryOpcode.BR:
        validate_br(cast(Br, instruction), ctx)
    elif instruction.opcode is BinaryOpcode.BR_IF:
        validate_br_if(cast(BrIf, instruction), ctx)
    elif instruction.opcode is BinaryOpcode.BR_TABLE:
        validate_br_table(cast(BrTable, instruction), ctx)
    elif instruction.opcode is BinaryOpcode.NOP:
        pass  # NOP is always valid
    elif instruction.opcode is BinaryOpcode.CALL:
        validate_call(cast(Call, instruction), ctx)
    elif instruction.opcode is BinaryOpcode.CALL_INDIRECT:
        validate_call_indirect(cast(CallIndirect, instruction), ctx)
    elif instruction.opcode is BinaryOpcode.RETURN:
        validate_return(ctx)
    else:
        raise Exception(f"Invariant: unhandled opcode {instruction.opcode}")

def validate_unreachable(ctx: ExpressionContext) -> None:
    """
    Validate an UNREACHABLE instruction as part of expression validation.
    """
    ctx.mark_unreachable()

def validate_block(instruction: Block, ctx: ExpressionContext) -> None:
    """
    Validate a BLOCK instruction as part of expression validation.
    """
    ctx.push_control_frame(instruction.result_type, instruction.result_type)

def validate_if(instruction: If, ctx: ExpressionContext) -> None:
    """
    Validate an IF instruction as part of expression validation.
    """
    ctx.pop_operand_and_assert_type(ValType.i32)
    ctx.push_control_frame(instruction.result_type, instruction.result_type)

def validate_else(instruction: Else, ctx: ExpressionContext) -> None:
    """
    Validate an ELSE instruction as part of expression validation.
    """
    frame = ctx.pop_control_frame()
    ctx.push_control_frame(frame.end_types, frame.end_types)

def validate_br(instruction: Br, ctx: ExpressionContext) -> None:
    """
    Validate a BR instruction as part of expression validation.
    """
    ctx.control_stack.validate_label_idx(instruction.label_idx)

    frame = ctx.control_stack.get_label_by_idx(instruction.label_idx)
    expected_label_types = frame.label_types

    ctx.pop_operands_of_expected_types(expected_label_types)
    ctx.mark_unreachable()

def validate_br_if(instruction: BrIf, ctx: ExpressionContext) -> None:
    """
    Validate a BR_IF instruction as part of expression validation.
    """
    ctx.control_stack.validate_label_idx(instruction.label_idx)

    frame = ctx.control_stack.get_label_by_idx(instruction.label_idx)
    label_types = frame.label_types

    ctx.pop_operand_and_assert_type(ValType.i32)
    ctx.pop_operands_of_expected_types(label_types)

    for valtype in label_types:
        ctx.operand_stack.push(valtype)

def validate_br_table(instruction: BrTable, ctx: ExpressionContext) -> None:
    """
    Validate a BR_TABLE instruction as part of expression validation.
    """
    ctx.control_stack.validate_label_idx(instruction.default_idx)

    frame = ctx.control_stack.get_label_by_idx(instruction.default_idx)
    expected_label_types = frame.label_types

    for idx, label_idx in enumerate(instruction.label_indices):
        ctx.control_stack.validate_label_idx(label_idx)
        label_frame = ctx.control_stack.get_label_by_idx(label_idx)

        if label_frame.label_types != expected_label_types:
            raise ValidationError(
                f"Label index at position {idx} has incorrect type.  Expected: "
                f"{expected_label_types}  Got: {label_frame.label_types}"
            )

    ctx.pop_operand_and_assert_type(ValType.i32)
    ctx.pop_operands_of_expected_types(expected_label_types)
    ctx.mark_unreachable()

def validate_end(ctx: ExpressionContext) -> None:
    """
    Validate an END instruction as part of expression validation.
    """
    frame = ctx.pop_control_frame()
    for valtype in frame.end_types:
        ctx.operand_stack.push(valtype)

def validate_loop(instruction: Loop, ctx: ExpressionContext) -> None:
    """
    Validate an LOOP instruction as part of expression validation.
    """
    ctx.push_control_frame(tuple(), instruction.result_type)

def validate_call(instruction: Call, ctx: ExpressionContext) -> None:
    """
    Validate an CALL instruction as part of expression validation.
    """
    ctx.validate_function_idx(instruction.function_idx)
    function_type = ctx.get_function(instruction.function_idx)

    ctx.pop_operands_of_expected_types(function_type.params)
    for valtype in function_type.results:
        ctx.operand_stack.push(valtype)

def validate_call_indirect(instruction: CallIndirect, ctx: ExpressionContext) -> None:
    """
    Validate an CALL_INDIRECT instruction as part of expression validation.
    """
    ctx.validate_table_idx(TableIdx(0))
    ctx.validate_type_idx(instruction.type_idx)
    function_type = ctx.get_type(instruction.type_idx)

    ctx.pop_operand_and_assert_type(ValType.i32)
    ctx.pop_operands_of_expected_types(function_type.params)

    for valtype in function_type.results:
        ctx.operand_stack.push(valtype)

def validate_return(ctx: ExpressionContext) -> None:
    """
    Validate an RETURN instruction as part of expression validation.
    """
    if ctx.returns is not None:
        ctx.pop_operands_of_expected_types(ctx.returns)
    ctx.mark_unreachable()


from inode import INode
from pyobuz.node import getNode, Flag
from pyobuz.settings import settings
from pyobuz.i8n import _
from pyobuz.cache import cache

class Node_root(INode):
    '''Our root node, we are displaying all qobuz nodes from here
    '''
    def __init__(self, parameters={}):
        super(Node_root, self).__init__(parameters)
        self.kind = Flag.ROOT
        self.label = 'Qobuz'
        self.content_type = 'files'

    def populate(self, renderer=None):
        self.append(getNode(Flag.USERPLAYLISTS, self.parameters))
        if settings['recommendation_enable']:
            self.append(getNode(Flag.RECOMMENDATION, self.parameters))
        self.append(getNode(Flag.PURCHASES, self.parameters))
        self.append(getNode(Flag.FAVORITES, self.parameters))
        if settings['search_enable']:
            search = getNode(Flag.SEARCH, self.parameters)
            search.search_type = 'albums'
            self.append(search)
            search = getNode(Flag.SEARCH, self.parameters)
            search.search_type = 'tracks'
            self.append(search)
            search = getNode(Flag.SEARCH, self.parameters)
            search.search_type = 'artists'
            self.append(search)
        self.append(getNode(Flag.FRIEND_LIST, self.parameters))
        self.append(getNode(Flag.GENRE, self.parameters))
        self.append(getNode(Flag.PUBLIC_PLAYLISTS, self.parameters))
        return True

    def append(self, item):
        item.add_action('cache_delete_all',
                        label=_('Erase all data from cache'), target=Flag.ROOT)
        item.add_action('cache_delete_old',
                        label=_('Erase old data from cache'), target=Flag.ROOT)
        super(Node_root, self).append(item)

    def cache_delete_all(self, **ka):
        return cache.delete_all(**ka)

    def cache_delete_old(self):
        return cache.delete_old()

#    def gui_scan(self):
#        '''Scanning directory specified in query parameter
#        '''
#        query = self.get_parameter('query', unQuote=True)
#        print "Scanning folder: %s" % (query)
#        executeBuiltin('XBMC.UpdateLibrary("music", "%s")' % (query))


'''
This script used a pre-trained word2vec model to create training data for
    sentiment analysis.
    Note: requires a pre-trained model from:
    https://github.com/3Top/word2vec-api
'''

# General Python
import re

# Machine learning
from gensim.models.word2vec import Word2Vec as w
import numpy as np

def normalized(a, axis=-1, order=2):
    """ Normalize a numpy vector
    """
    l2 = np.atleast_1d(np.linalg.norm(a, order, axis))
    l2[l2==0] = 1
    return a / np.expand_dims(l2, axis)

# load model from file
MODEL_DIMENSIONALITY = 300
model = w.load_word2vec_format('/home/andrei/dev/word2vec-api/model.bin.gz', binary=True)

# read the pre-processed bioscope input file and get the polarity of each line
lines = [line.rstrip('\n') for line in open('bioscope/full_papers.txt')]
numLines = len(lines)

# create the sentence data matrix to be filled in later
sentenceData = np.zeros((MODEL_DIMENSIONALITY, numLines))
sentenceLabels = np.zeros(numLines)

for i in xrange(0, numLines):
    line = lines[i]
    polarity, sentence = line.split(' ', 1)
    polarity = int(polarity)

    # remove non alpha chars
    sentence = re.sub("[^a-zA-Z\s-]", "", sentence)
    # replace all '-' chars with ' '
    sentence = re.sub('-', ' ', sentence)

    # get the numpy value of the current word in the sentence
    print('polarity: %d, sentence: %s' % (polarity, sentence))
    words = sentence.split(' ')

    # get the total vector sum of the entire sentence
    s_val = np.zeros(MODEL_DIMENSIONALITY)
    for word in words:
        try:
            w_val = model[word]
        except:
            w_val = np.zeros(MODEL_DIMENSIONALITY)
        s_val = s_val + w_val
    s_val = normalized(s_val)

    sentenceData[:,i] = s_val
    sentenceLabels[i] = polarity
    print('step %d/%d' % (i, numLines))

# save the data and labels to files
np.save('bioscope/word2vec_sentence_data.npy', sentenceData)
np.save('bioscope/word2vec_sentence_labels.npy', sentenceLabels)


from django.shortcuts import render
from django.utils import timezone
from django.contrib.auth.decorators import login_required, user_passes_test
from .models import Schulklasse, Raum, Tag, Stunde, Lehrer, Schulfach, Uebergreifung, VorgabeEinheit
from django.forms import modelformset_factory

from django.http import HttpResponseRedirect
from .forms import LehrerForm, SchulfachForm, UebergreifungForm, BlockenForm, TagesForm, VorgabenForm, KlassenForm, PartnerForm, LehrfaecherForm, RaumForm, UnterrrichtForm, RaumBelegtForm, NutzbarForm

def rechte_check(user):
    return user.username in ['Direktor','franzi']

# Create your views here.
@login_required
@user_passes_test(rechte_check)
def input_main(request):
     return render(request, 'datainput/input_main.html',)

@login_required
@user_passes_test(rechte_check)
def room_page(request):
    rooms = Raum.objects.order_by('Name')
    tage = Tag.objects.order_by('Index')
    stunden = Stunde.objects.order_by('Index')
    roomToSlot = []
    for raum in rooms:
        stundenListe = []
        for stunde in stunden:
            tagBelegungsListe = []
            for tag in tage:
                tagBelegungsListe.append(raum.Nichtfrei.filter(Stunde=stunde, Tag=tag).exists())
            stundenListe.append((stunde, tagBelegungsListe))
        roomToSlot.append((raum, stundenListe))

    if request.method == "POST":
        if 'submitRaum' in request.POST:
            formRaum = RaumForm(request.POST)
            if formRaum.is_valid():
                formRaum.save()
        else:
            formRaum = RaumForm()
        if 'submitBelegung' in request.POST:
            formBelgegt = RaumBelegtForm(request.POST)
            if formBelegt.is_valid():
                formBelegt.save()
        else:
            formBelegt = RaumBelegtForm()
        if 'submitNutzbar' in request.POST:
            formNutzbar = NutzbarForm(request.POST)
            if formNutzbar.is_valid():
                formNutzbar.save()
        else:
            formNutzbar = NutzbarForm()
    else:
        formRaum = RaumForm()
        formBelegt = RaumBelegtForm()
        formNutzbar = NutzbarForm()

    return render(request, 'datainput/room_page.html', {
        'rooms': rooms,
        'tage': tage,
        'stunden': stunden,
        'roomToSlot': roomToSlot,
        'formRaum':formRaum,
        'formBelegt': formBelegt,
        'formNutzbar': formNutzbar,
    })

@login_required
@user_passes_test(rechte_check)
def teacher_page(request):

    lehrers = Lehrer.objects.order_by('Name')
    if request.method == "POST":
        if 'submitLehrer' in request.POST:
            formLehrer = LehrerForm(request.POST)
            if formLehrer.is_valid():
                formLehrer.save()
        else:
            formLehrer = LehrerForm()
        if 'submitFaecher' in request.POST:
            formFaecher = UnterrrichtForm(request.POST)
            if formFaecher.is_valid():
                formFaecher.save()
        else:
            formFaecher = UnterrrichtForm()
    else:
        formLehrer = LehrerForm()
        formFaecher = UnterrrichtForm()
    return render(request,'datainput/teacher_page.html', {'lehrers':lehrers, 'formLehrer': formLehrer, 'formFaecher': formFaecher})

@login_required
@user_passes_test(rechte_check)
def curriculum_page(request):
    klassen = Schulklasse.objects.order_by('Name')

    if request.method == "POST":
        if 'submitKlasse' in request.POST:
            formKlasse = KlassenForm(request.POST)
            if formKlasse.is_valid():
                formKlasse.save()
        else:
            formKlasse = KlassenForm()
        if 'submitFaecher' in request.POST:
            formFaecher = LehrfaecherForm(request.POST)
            if formFaecher.is_valid():
                formFaecher.save()
        else:
            formFaecher = LehrfaecherForm()
    else:
        formKlasse = KlassenForm()
        formFaecher = LehrfaecherForm()
    return render(request,'datainput/curriculum_page.html', {'klassen': klassen, 'formKlasse':formKlasse, 'formFaecher': formFaecher})

@login_required
@user_passes_test(rechte_check)
def mainteacher_page(request):
    klassen = Schulklasse.objects.order_by('Name')

    if request.method == "POST":
        if 'submitKlasse' in request.POST:
            formKlasse = KlassenForm(request.POST)
            if formKlasse.is_valid():
                formKlasse.save()
        else:
            formKlasse = KlassenForm()
        if 'submitPartner' in request.POST:
            formPartner = PartnerForm(request.POST)
            if formPartner.is_valid():
                formPartner.save()
        else:
            formPartner = PartnerForm()
    else:
        formKlasse = KlassenForm()
        formPartner = PartnerForm()

    return render(request, 'datainput/mainteacher_page.html', {'klassen':klassen, 'formKlasse':formKlasse, 'formPartner': formPartner})

@login_required
@user_passes_test(rechte_check)
def parallel_page(request):
    faecher = Schulfach.objects.order_by('Name')
    parallels = []
    for fach in faecher:
        parallelfaecher=[fach]
        for parfach in faecher:
            if parfach.Parallel == fach:
                parallelfaecher.append(parfach)
        if len(parallelfaecher) > 1:
            parallels.append(parallelfaecher)

    if request.method == "POST":
        form = SchulfachForm(request.POST)
        if form.is_valid():
            form.save()
    else:
        form = SchulfachForm()

    return render(request,'datainput/parallel_page.html', {'parallels': parallels, 'form': form})

@login_required
@user_passes_test(rechte_check)
def uebergreifend_page(request):
    uebergreifend = Uebergreifung.objects.order_by('fach')

    if request.method == "POST":
        form = UebergreifungForm(request.POST)
        if form.is_valid():
            form.save()
    else:
        form = UebergreifungForm()

    return render(request,'datainput/uebergreifend_page.html', {'uebergreifend':uebergreifend, 'form':form})

@login_required
@user_passes_test(rechte_check)
def unavailable_page(request):
    # lehrers = Lehrer.objects.order_by('Name')
    # return render(request, 'datainput/unavailable_page.html', {'lehrers':lehrers})

    lehrers = Lehrer.objects.order_by('Name')
    tage = Tag.objects.order_by('Index')
    stunden = Stunde.objects.order_by('Index')
    stundenListe = []
    for stunde in stunden:
        tagBelegungsListe = []
        for tag in tage:
            tagNichtda = lehrers.filter(NichtDa__Stunde = stunde, NichtDa__Tag = tag)
            tagBelegungsListe.append(tagNichtda)
        stundenListe.append((stunde, tagBelegungsListe))

    if request.method == "POST":
        form = BlockenForm(request.POST)
        if form.is_valid():
            form.save()
    else:
        form = BlockenForm()

    return render(request, 'datainput/unavailable_page.html', {
        'lehrers': lehrers,
        'tage': tage,
        'stunden': stunden,
        'stundenListe': stundenListe,
        'form': form
    })

@login_required
@user_passes_test(rechte_check)
def hoursperday_page(request):
    klassen =  Schulklasse.objects.order_by('Name')
    tage = Tag.objects.order_by('Index')

    if request.method == "POST":
        form = TagesForm(request.POST)
        if form.is_valid():
            form.save()
    else:
        form = TagesForm()

    return render(request, 'datainput/hoursperday_page.html', {'klassen':klassen, 'tage':tage, 'form': form})

@login_required
@user_passes_test(rechte_check)
def guidelines_page(request):
    # klassen =  Schulklasse.objects.order_by('Name')
    # return render(request, 'datainput/guidelines_page.html', {'klassen':klassen})

    klassen = Schulklasse.objects.order_by('Name')
    tage = Tag.objects.order_by('Index')
    stunden = Stunde.objects.order_by('Index')
    vorgaben = VorgabeEinheit.objects.order_by('Schulklasse')
    klasseToVorgabe = []
    for klasse in klassen:
        vorgabenListe = []
        for stunde in stunden:
            tagVorgabenListe = []
            for tag in tage:
                # Hier in der Zeile ist ein Fehler
                tagVorgabenListe.append(vorgaben.filter(Zeitslot__Stunde=stunde, Zeitslot__Tag=tag, Schulklasse=klasse).first())
            vorgabenListe.append((stunde, tagVorgabenListe))
        klasseToVorgabe.append((klasse, vorgabenListe))

    if request.method == "POST":
        form = VorgabenForm(request.POST)
        if form.is_valid():
            form.save()
    else:
        form = VorgabenForm()

    return render(request, 'datainput/guidelines_page.html', {
        'klassen': klassen,
        'tage': tage,
        'stunden': stunden,
        'klasseToVorgabe': klasseToVorgabe,
        'form':form
    })


def sumOfN(N):
    N = abs(N)
    return (N*N+N) // 2

def Day7(mylist):

    '''
    min_fuel = float('inf')

    #ar = numpy.array(mylist)
    #print(numpy.mean(ar))

    #PartOne
    for i in range(len(mylist)):
        cost = 0
        for j in range(0,len(mylist)):
            step = abs(mylist[i]-mylist[j])
            #print(cost)

        if cost < min_fuel:
            min_fuel = cost

    print(min_fuel)

    #PartTwo
    '''
    x = min(mylist)
    y = max(mylist)
    scores = []
    #print(x,y)
    for i in range(x,y+1):
        val = sum(sumOfN(i-j) for j in mylist)
        scores.append(val)
    print(min(scores))

if __name__=="__main__":
    mylist = []

    with open('input','r') as f:
        for line in f:
            mylist+=list(map(int,line.rstrip().split(',')))

    Day7(mylist)


#imprime links de autores solamente

from bs4 import BeautifulSoup
from selenium import webdriver
import time, requests, datetime, csv, urllib.parse, os, lxml
from selenium.common import exceptions as SE
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as W
from selenium.webdriver.support import expected_conditions as EC
from csv import writer

USER_AGENT = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
              'Chrome/61.0.3163.100 Safari/537.36'}
chrome_options = Options()
chrome_options.add_argument("--headless")
options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-logging'])
chrome_options.add_argument("--window-size=1920,1080")
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--proxy-server='direct://'")
chrome_options.add_argument("--proxy-bypass-list=*")
chrome_options.add_argument("--start-maximized")
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--ignore-certificate-errors')
chrome_options.add_argument('--log-level=3')
chrome_options.add_argument('--allow-running-insecure-content')

driver = webdriver.Chrome(executable_path=os.path.abspath("chromedriver"), options=chrome_options)

button_locators = "//button[@class='gs_btnPR gs_in_ib gs_btn_half gs_btn_lsb gs_btn_srt gsc_pgn_pnx']"
wait = W(driver, 2)

urls = []
with open(r"instituciones.csv", 'r') as f:
    for line in f:
        urls.append(line)

for url in urls:
    driver.get(url)
    try:
        button_link = wait.until(EC.element_to_be_clickable((By.XPATH, button_locators)))
    except:
        pass
    start_time = time.time()
    start_timing = datetime.datetime.now()
    response = requests.get(url, headers=USER_AGENT)
    response.raise_for_status()

    while button_link:
        try:
            wait.until(EC.visibility_of_element_located((By.ID, 'gsc_sa_ccl')))
            soup = BeautifulSoup(driver.page_source, 'lxml')
            posts = soup.find_all('div', attrs={'class': 'gsc_1usr'})
            time.sleep(2)

            for post in posts:
                linkfoto = post.find('a', attrs={'class': 'gs_ai_pho'})
                l = linkfoto.get('href')
                link = urllib.parse.urljoin("https://scholar.google.com", l)
                print(link)

                with open(r'Autor.csv', 'a', encoding="utf-8", newline='') as s:
                    writer = csv.writer(s)
                    writer.writerow([link])

            button_link = wait.until(EC.element_to_be_clickable((By.XPATH, button_locators)))
            button_link.click()

        except SE.TimeoutException:
            print(f'Página parseada {url}')
            break
    end_time = time.time()
    print("This page took: " + str(end_time - start_time) + " seconds!")

    with open('resultadoslinkautores.txt', 'a', encoding="utf-8", newline='') as fd:
        writer = csv.writer(fd)
        writer.writerow([f'Página {url}  tomó: ' + str(end_time - start_time) + "segundos"])

print("*** {} urls scraped ***".format(len(urls)))
driver.quit()