import numpy as np
import pandas as pd
import pickle, gc, shap, math, random, time

import matplotlib.pyplot as plt
import seaborn as sns

from sklearn.model_selection import KFold
from sklearn.preprocessing import LabelEncoder
from sklearn import metrics
from sklearn import model_selection
import lightgbm as lgb
import xgboost as xgb
from catboost import CatBoostClassifier
from sklearn.linear_model import LinearRegression
from sklearn.svm import NuSVR, SVR

# This script can also be used for stacking purpose by inputing training data set with oof results as features additional to earlier engineered features from different data sets.

def train_model_classification(X, y, params, groups, folds, model_type='lgb', eval_metric='auc', columns=None, plot_feature_importance=False, model=None,
                               verbose=10000, early_stopping_rounds=200, n_estimators=50000, weight = None, seed='no'):
    """
    A function to train a variety of regression models.
    Returns dictionary with oof predictions, test predictions, scores and, if necessary, feature importances.

    :params: X - training data, can be pd.DataFrame
    :params: X_test - test data, can be pd.DataFrame
    :params: y - target
    :params: folds - folds to split data
    :params: model_type - type of model to use
    :params: eval_metric - metric to use
    :params: columns - columns to use. If None - use all columns
    :params: plot_feature_importance - whether to plot feature importance of LGB
    :params: model - sklearn model, works only for "sklearn" model type

    """
    columns = X.columns if columns is None else columns
    models = []

    metrics_dict = {'auc': {'lgb_metric_name': 'auc',
                        'catboost_metric_name': 'AUC',
                        'sklearn_scoring_function': metrics.roc_auc_score},
                    }

    result_dict = {}

    # out-of-fold predictions on train data
    oof = np.zeros(len(X))

    scores = []
    train_loss = []
    feature_importance = pd.DataFrame()

    if groups is None:
        splits = folds.split(X)

    elif groups == 'stra':
        splits = folds.split(X,y)

    else:
        splits = folds.split(X, groups = groups)
        print('no')

    for fold_n, (train_index, valid_index) in enumerate(splits):

        print(f'Fold {fold_n + 1} started at {time.ctime()}')

        if type(X) == np.ndarray:
            X_train, X_valid = X[columns][train_index], X[columns][valid_index]
            y_train, y_valid = y[train_index], y[valid_index]
            weight_train = weight[train_index]
        else:
            X_train, X_valid = X[columns].iloc[train_index], X[columns].iloc[valid_index]
            y_train, y_valid = y.iloc[train_index], y.iloc[valid_index]
            weight_train = weight[train_index]

        if model_type == 'lgb':

            model = lgb.LGBMClassifier(**params, n_estimators = n_estimators, n_jobs = -1)
            model.fit(X_train, y_train, sample_weight = weight_train,
                    eval_set=[(X_train, y_train), (X_valid, y_valid)], eval_metric=metrics_dict[eval_metric]['lgb_metric_name'],
                    verbose=verbose, early_stopping_rounds=early_stopping_rounds)

            y_pred_valid = model.predict_proba(X_valid)[:, 1]
            y_pred_train = model.predict_proba(X_train)[:, 1]
            models.append(model)

        if model_type == 'xgb':
            train_data = xgb.DMatrix(data=X_train, label=y_train, feature_names=X.columns)
            valid_data = xgb.DMatrix(data=X_valid, label=y_valid, feature_names=X.columns)

            watchlist = [(train_data, 'train'), (valid_data, 'valid_data')]
            model = xgb.train(dtrain=train_data, num_boost_round=n_estimators, evals=watchlist, early_stopping_rounds=early_stopping_rounds, verbose_eval=verbose, params=params)
            y_pred_valid = model.predict(xgb.DMatrix(X_valid, feature_names=X.columns), ntree_limit=model.best_ntree_limit)
            y_pred_train = model.predict(xgb.DMatrix(X_train, feature_names=X.columns), ntree_limit=model.best_ntree_limit)
            models.append(model)

        if model_type == 'sklearn':
            model = model
            model.fit(X_train, y_train)

            y_pred_valid = model.predict(X_valid).reshape(-1,)
            score = metrics_dict[eval_metric]['sklearn_scoring_function'](y_valid, y_pred_valid)
            print(f'Fold {fold_n}. {eval_metric}: {score:.4f}.')
            print('')
            models.append(model)

        if model_type == 'cat':
            model = CatBoostClassifier(iterations=n_estimators,  eval_metric=metrics_dict[eval_metric]['catboost_metric_name'], **params,
                                      )
            model.fit(X_train, y_train, eval_set=(X_valid, y_valid), cat_features=[], use_best_model=True, verbose=verbose,early_stopping_rounds=early_stopping_rounds)
            y_pred_valid = model.predict_proba(X_valid)[:,1]
            y_pred_train = model.predict_proba(X_train)[:,1]
            models.append(model)

        oof[valid_index] = y_pred_valid.reshape(-1,)
        if eval_metric != 'group_mae':
            scores.append(metrics_dict[eval_metric]['sklearn_scoring_function'](y_valid, y_pred_valid))
            train_loss.append(metrics_dict[eval_metric]['sklearn_scoring_function'](y_train, y_pred_train))
        else:
            scores.append(metrics_dict[eval_metric]['scoring_function'](y_valid, y_pred_valid, X_valid['type']))

        with open(f'./models/models_{model_type}_{seed}.pickle', 'wb') as handle:
            pickle.dump(models, handle, protocol = pickle.HIGHEST_PROTOCOL)

        gc.collect()

        if model_type == 'lgb' and plot_feature_importance:
            # feature importance
            fold_importance = pd.DataFrame()
            fold_importance["feature"] = columns
            fold_importance["importance"] = model.feature_importances_
            fold_importance["shap_values"] = abs(shap.TreeExplainer(model).shap_values(X_valid)[:,:len(columns)]).mean(axis=0).T
            fold_importance["fold"] = fold_n + 1
            feature_importance = pd.concat([feature_importance, fold_importance], axis=0)

    print('Train loss mean: {0:.6f}, std: {1:.6f}.'.format(np.mean(train_loss), np.std(train_loss)))
    print('CV mean score: {0:.6f}, std: {1:.6f}.'.format(np.mean(scores), np.std(scores)))

    result_dict['oof'] = oof
    result_dict['scores'] = scores
    result_dict['models'] = models

    if model_type == 'lgb':
        if plot_feature_importance:
            feature_importance["importance"] /= folds.n_splits
            cols = feature_importance[["feature", "importance"]].groupby("feature").mean().sort_values(
                by="importance", ascending=False)[:50].index

            best_features = feature_importance.loc[feature_importance.feature.isin(cols)]

            plt.figure(figsize=(16, 12));
            sns.barplot(x="importance", y="feature", data=best_features.sort_values(by="importance", ascending=False));
            plt.title('LGB Features (avg over folds)');

            result_dict['feature_importance'] = feature_importance

    return result_dict

def predict(df, models, model_type = 'lgb'):
    prediction = np.zeros(len(df))
    for model in models:

        if model_type == 'lgb':
            pred = model.predict_proba(df)[:, 1]
        if model_type == 'xgb':
            pred = model.predict(xgb.DMatrix(df, feature_names=df.columns), ntree_limit=model.best_ntree_limit)
        if model_type == 'cat':
            pred = model.predict_proba(df)[:,1]
        prediction += pred/len(models)

    return prediction

class train_config:

    def __init__(self, n_splits, features, model_type, model_params, eval_metric, early_stopping_rounds, n_estimators,
                 save_oof, seed):

        self.n_splits = n_splits
        self.features = features
        self.model_type = model_type
        self.model_params = model_params
        self.eval_metric = eval_metric
        self.early_stopping_rounds = early_stopping_rounds
        self.n_estimators = n_estimators
        self.save_oof = save_oof
        self.seed = seed

def model_train(df_train, train_config):

    n_splits = train_config.n_splits
    seed = train_config.seed
    folds = KFold(n_splits, shuffle = True, random_state = seed)

    X_train = df_train[train_config.features]
    y_train = df_train['TARGET']
    weight = df_train['weight']

    result_dict = train_model_classification(
                             X=X_train,
                             y=y_train,
                             params=train_config.model_params,
                             groups = None,
                             folds=folds, model_type=train_config.model_type, eval_metric=train_config.eval_metric,
                             plot_feature_importance=True,verbose=100, early_stopping_rounds=train_config.early_stopping_rounds,
                             n_estimators=train_config.n_estimators,weight = weight, seed = seed)

    return result_dict

if __name__ == "__main__":

    with open('./processed/train_processed.pickle', 'rb') as handle:
        train = pickle.load(handle)

    with open('./processed/test_processed.pickle', 'rb') as handle:
        test = pickle.load(handle)

    config_path = sys.argv[1]
    with open(config_path) as json_file:
        config = json.load(json_file)

    t_config = train_config(
        n_splits = config['n_splits'],
        features = config['features'],
        model_type = config['model_type'],
        model_params = config['model_params'],
        eval_metric = config['eval_metric'],
        early_stopping_rounds = config['early_stopping_rounds'],
        n_estimators = config['n_estimators'],
        save_oof = config['save_oof'],
        seed = config['seed'],
    )

    result_dict = model_train(train, t_config)

    models = result_dict['models']
    prediction = predict(test[t_config.features], models)

    if t_config.save_oof == True:

        train[f'oof_{t_config.model_type}_{t_config.seed}'] = result_dict['oof']
        test[f'oof_{t_config.model_type}_{t_config.seed}'] = prediction

        with open('./processed/train_processed.pickle', 'wb') as handle:
            pickle.dump(train, handle, protocol = pickle.HIGHEST_PROTOCOL)
        with open('./processed/test_processed.pickle', 'wb') as handle:
            pickle.dump(test, handle, protocol = pickle.HIGHEST_PROTOCOL)

    sample_submission = pd.read_csv('./input/sample_submission.csv')
    sample_submission['TARGET'] = prediction
    sample_submission.to_csv(f'./submissions/submission_{t_config.model_type}_{t_config.seed}.csv', index=False)


# Define your item pipelines here
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
# useful for handling different item types with a single interface

from itemadapter import ItemAdapter
from scrapy.exceptions import DropItem

import os
from urllib.parse import urlparse
from scrapy.pipelines.images import ImagesPipeline

DETAILS_STORE = '/home/tales/Documents/bti/pesquisa/PVC16102-2019/PVC16102-2019/scraper/olx/details'

class OlxPipeline:
    # Attributes
    def __init__(self):
        self.id = 0

    def process_item(self, item, spider):
        # Drop the area if has error or if is less than 25m2
        adapter = ItemAdapter(item)
        if adapter.get('area') == 'AREA-ERR' or adapter.get('area') < '25' or adapter.get('area') == '0':
                raise DropItem(f'THE AREA IS MISSING OR IS LESS THAN 25 M2 IN {item}')
        if adapter.get('categoria') == 'T':
                raise DropItem(f'DROP TERRAIN {item}')
        if adapter.get('tipo') == '--' or adapter.get('tipo') == 'A':
                raise DropItem(f'DROP: NO TYPE OR RENT {item}')
        else:
            newItem = {"id" : self.id} # Creates a id for each house
            newItem.update(item)
            item = newItem
            self.id+=1
            with open(os.path.join(DETAILS_STORE, str(item.get('id'))), "w") as f:
                f.write('TÍTULO\n' + item.get('titulo') + '\n' + 'DESCRIÇÃO\n' + item.get('description'))
            return item

class MyImagesPipeline(ImagesPipeline):
    def file_path(self, request, response=None, info=None, *, item=None):
        return str(item.get('id')) + '/' + os.path.basename(urlparse(request.url).path)


import boto3
import json

def get_security_hub_info():
    """
    A fucntion that gives security hub products info
    """

    conn = boto3.client('ec2')
    regions = [region['RegionName'] for region in conn.describe_regions()['Regions']]
    product_info = []

    for region in regions:
        # not availble in this region
        if region == 'ap-east-1':
            continue

        client = boto3.client('securityhub', region_name=region)
        # get product information
        response = client.describe_products()['Products']

        for res in response:
            req_info = []
            req_info.append(res)
            product_info.append(req_info)

    # convert products list into dictionary
    product_dict = {"Products": product_info}

    # convert dictionary into json
    products_json = json.dumps(product_dict, indent=4, default=str)

    print(products_json)

get_security_hub_info()

Aws Discovery/security and identity/security hub.py


import json
import os
from typing import Any, Dict, Optional
import zipfile

from leptonai.config import CACHE_DIR
from leptonai._internal.db import DB
from leptonai.util import create_cached_dir_if_needed
from leptonai.registry import Registry

schema_registry = Registry()
type_str_registry = Registry()

class BasePhoton:
    """Base class for all Photons."""

    photon_type = "base"
    """photon_type defines the type of the photon.
    It is used to identify the photon at building and deployment time. Should not be
    changed by user code.
    """

    extra_files = {}
    """Extra files that should be included in the photon.
    Extra files are files that are not part of the model source file, but are
    required to run the model. It takes two forms:
        - a dictionary of {remote_path: local_path} pairs, where the remote_path
          is relative to the cwd of the photon at runtime, and local_path is
          the path pointing to the file in the local file system. If local_path
          is relative, it is relative to the current working directory of the
          local environment.
        - a list of paths (relative to the cwd of the local directory) to be included.
          The remote path will be the same as the local path.
    """

    def __init_subclass__(cls, **kwargs):
        super().__init_subclass__(**kwargs)
        for base in cls.__bases__:
            if issubclass(base, BasePhoton) and base.photon_type == cls.photon_type:
                # do not override load if its photon_type the same as parent class
                break
        else:
            type_str_registry.register(cls.photon_type, cls.load)

    def __init__(self, name: str, model: str):
        self._photon_name = name
        self._photon_model = model

    def save(self, path: Optional[str] = None):
        """
        Saves the photon to a local zip file.
        """
        if path is None:
            # If the user has not explicitly specified a path, we will be using the
            # cache directory to store the photon.
            create_cached_dir_if_needed()
            # assuming maximum 1000 versions for now
            for version in range(1000):
                if version == 0:
                    path = str(CACHE_DIR / f"{self._photon_name}.photon")
                else:
                    path = str(CACHE_DIR / f"{self._photon_name}.{version}.photon")
                if not os.path.exists(path):
                    break
            else:
                raise ValueError(
                    f"Can not find a valid path for creating photon {self._photon_name}"
                )
        with zipfile.ZipFile(path, "w") as f:
            metadata = self.metadata
            checked_extra_files = self._extra_files
            metadata["extra_files"] = list(checked_extra_files.keys())
            f.writestr(
                "metadata.json",
                json.dumps(metadata),
            )
            for name, path_or_content in checked_extra_files.items():
                if isinstance(path_or_content, bytes):
                    f.writestr(name, path_or_content)
                    continue
                elif isinstance(path_or_content, str):
                    if os.path.exists(path_or_content):
                        with open(path_or_content, "rb") as extra_file:
                            f.writestr(name, extra_file.read())
                    else:
                        f.writestr(name, path_or_content)
                else:
                    raise ValueError(
                        "extra_files value should be str or bytes, got"
                        f" {path_or_content}"
                    )

        # use path as local id for now
        add_photon(str(path), self._photon_name, self._photon_model, str(path))
        return path

    @staticmethod
    def load(path: str):
        """
        Loads a photon from a local zip file.
        """
        metadata = BasePhoton.load_metadata(path, unpack_extra_files=True)
        photon_type = metadata["type"]
        photon_cls = type_str_registry.get(photon_type)
        if photon_cls is None:
            raise ValueError(
                f"Can not find Photon class for type '{photon_type}', please"
                " make sure the corresponding module is imported."
            )
        with zipfile.ZipFile(path, "r") as photon_file:
            photon = photon_cls(photon_file, metadata)
        return photon

    @staticmethod
    def load_metadata(path: str, unpack_extra_files: bool = False):
        with zipfile.ZipFile(path, "r") as photon_file:
            with photon_file.open("metadata.json") as config:
                metadata = json.load(config)
            if unpack_extra_files:
                for path in metadata["extra_files"]:
                    dirname = os.path.dirname(path)
                    if dirname:
                        os.makedirs(dirname, exist_ok=True)
                    with open(path, "wb") as out, photon_file.open(path) as extra_file:
                        out.write(extra_file.read())
        return metadata

    @property
    def metadata(self) -> Dict[str, Any]:
        return {
            "name": self._photon_name,
            "model": self._photon_model,
            "type": self.photon_type,
        }

    @property
    def _extra_files(self):
        """
        Returns a dict of extra files to be included in the photon.

        If extra_files is a dict, it will be returned directly, with the keys being
        the file path in the photon, and the values being the file path in the buidling
        environment. If extra_files is a list, each list item should be a relative file
        path in the building environment, and the file will be included in the photon with the
        same name and path.
        """

        def recursive_include(dirpath):
            res = {}
            for root, _, files in os.walk(dirpath):
                for file in files:
                    path = os.path.join(root, file)
                    res[path] = path
            return res

        res = {}
        if isinstance(self.extra_files, list):
            # Verify if the file exists too.
            for path in self.extra_files:
                if not os.path.exists(path):
                    raise ValueError(
                        f"Can not find extra file {path} in the building environment"
                    )
                if os.path.isdir(path):
                    res.update(recursive_include(path))
                else:
                    res[path] = path
        elif isinstance(self.extra_files, dict):
            for from_, to_ in self.extra_files.items():
                if not os.path.exists(to_):
                    raise ValueError(
                        f"Can not find extra file {to_} in the building environment"
                    )
                if os.path.isdir(to_):
                    res.update(recursive_include(to_))
                else:
                    res[from_] = to_
        else:
            raise ValueError(
                f"extra_files should be either a dict or a list, got {self.extra_files}"
            )
        return res

    def __str__(self):
        return f"Photon(name={self._photon_name}, model={self._photon_model})"

def add_photon(id: str, name: str, model: str, path: str):
    DB().cursor().execute(
        """INSERT INTO photon (id, name, model, path, creation_time) VALUES (?, ?, ?, ?, strftime('%s','now'))""",
        (id, name, model, path),
    )
    DB().commit()

def find_all_local_photons():
    res = DB().cursor().execute("SELECT * FROM photon ORDER BY creation_time DESC")
    records = res.fetchall()
    return records

def find_local_photon(name: str) -> Optional[str]:
    res = (
        DB()
        .cursor()
        .execute(
            "SELECT * FROM photon WHERE name = ? ORDER BY creation_time DESC", (name,)
        )
    )
    record_or_none = res.fetchone()
    if record_or_none is None:
        return None
    else:
        return record_or_none[0]

def remove_local_photon(name: str, remove_all: bool = False):
    res = (
        DB()
        .cursor()
        .execute(
            "SELECT path FROM photon WHERE name = ? ORDER BY creation_time DESC",
            (name,),
        )
    )
    if remove_all:
        path_or_none = res.fetchone()
        while path_or_none:
            path = path_or_none[0]
            if os.path.exists(path):
                os.remove(path)
            path_or_none = res.fetchone()
        DB().cursor().execute("DELETE FROM photon WHERE name = ?", (name,))
        DB().commit()
    else:
        path_or_none = res.fetchone()
        if path_or_none is None:
            return
        path = path_or_none[0]
        if os.path.exists(path):
            os.remove(path)
        DB().cursor().execute(
            "DELETE FROM photon WHERE name = ? AND path = ?", (name, path)
        )
        DB().commit()


import torch
import random
import torch.optim
import pandas as pd
import torch.nn as nn

# Load data and remove the junks
df = pd.read_csv('soil_weather_data.csv')
df = df.drop(['state_name','county_name', 'Value_y', 'lon_y','lat_y'], axis=1)

#exclude labels
# exit()

labels = df['Value_x'].tolist()
# labels = (labels - labels.min()) / (labels.max() - labels.min())
# labels = labels.tolist()
df_no_labels = df.drop(['Value_x'], axis=1)

#Normalize the data:
df_no_labels = (df_no_labels - df_no_labels.min()) / (df_no_labels.max() - df_no_labels.min())
aez = df_no_labels['AEZ_classes'].tolist()
mapping = {}
for i, e in enumerate(set(aez)):
	mapping[e] = i
#splitting train and test sets
random.seed(0)
indices = list(range(15408))
random.shuffle(indices)
test_indices = indices[:5000]
train_indices = indices[5000:]

#Loss function
def RRMSE(output, target):
	nom = (output - target)**2
	denom = target ** 2
	return torch.sqrt(torch.mean(nom / denom))

#ML model 1: simple regressor
simple_reg = nn.Linear(274, 1)

#ML model 2: MLP
MLP = nn.Sequential(nn.Linear(274, 256), nn.ReLU(), nn.Linear(256,1))

selected_model = MLP
optimizer = torch.optim.Adam(selected_model.parameters(), lr=0.0001)
epochs = 300
batch_size = 256

def one_hot(v):
	a = []
	for i in range(19):
		a.append(0)
	a[mapping[v]] = 1.
	return a

def retrieve_data(ind):
	inputs = []
	lbl = []
	for i in ind:
		inputs.append(df_no_labels.iloc[i].tolist())
		lbl.append(labels[i])
	for i in range(len(inputs)):
		inputs[i] = inputs[i][:-5] + one_hot(inputs[i][-5]) + inputs[i][-4:]
	inputs = torch.tensor(inputs)
	lbl = torch.tensor(lbl)
	return inputs, lbl.reshape(-1, 1)

best = 100

all_losses = []

def test():
	global best, all_losses
	with torch.no_grad():
		inputs, lbl = retrieve_data(test_indices)
		out = selected_model(inputs)
		loss = RRMSE(out, lbl)
		all_losses.append(loss)
		if loss < best:
			best = loss
			torch.save(selected_model.state_dict(),'model_best_mlp.pt')
		print("RRMSE: ", loss.item(), "Best:", best)

for e in range(epochs):
	start_ind = 0
	end_ind = batch_size
	batch_cnt = 0
	while(start_ind < len(train_indices)):
		batch_ind = train_indices[start_ind:end_ind]
		start_ind = end_ind
		end_ind = min(len(train_indices), end_ind + batch_size)
		inputs, lbl = retrieve_data(batch_ind)

		out = selected_model(inputs)
		loss = RRMSE(out, lbl)
		if batch_cnt % 10 == 0:
			print(e, batch_cnt, loss.item())
		batch_cnt += 1
		loss.backward()
		optimizer.step()

	test()

torch.save(torch.tensor(all_losses), 'mlp-losses.pt')


import yaml

import logging.config
import os
import sys

project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
sys.path.insert(0, project_root)

from common import data_processing as dp
from cli_version import cli_argument_parser

CLI_VERSION = "cli_version"

with open(os.path.join(CLI_VERSION, "logging_config.yaml"), "r") as config:
    log_config = yaml.safe_load(config.read())

logging.config.dictConfig(log_config)
logger = logging.getLogger("cli_app")

parser = cli_argument_parser.CliArgumentParser()
parser.configure_parser()
args = parser.parse()
personal_data_path = args.pdata
financial_data_path = args.fdata
countries = args.countries

try:
    personal_data, financial_data = dp.load_data(personal_data_path, financial_data_path)
    logger.info(f"Loaded personal data with schema {personal_data.columns}.")
    logger.info(f"Loaded financial data with schema {financial_data.columns}.")

    if countries:
        personal_data = dp.filter_data(personal_data, countries)

    personal_data_filtered = personal_data.drop(columns=["first_name", "last_name", "country"])
    financial_data_filtered = financial_data.drop(columns=["cc_n"])

    datasets_merged = dp.create_client_data(
        personal_data_filtered,
        financial_data_filtered,
        {"id": "client_identifier", "btc_a": "bitcoin_address", "cc_t": "credit_card_type"}
    )
    logger.info(f"Merged two datasets. Final schema: {datasets_merged.columns}.")

    output_path = os.path.join(CLI_VERSION, "client_data")
    os.makedirs(output_path, exist_ok=True)

    dp.save_data(datasets_merged, os.path.join(output_path, "client_data.csv"))
except Exception as exception:
    logger.error(exception)


class Scene:
  def __init__(self,index,no,text,question,branch1,branch2 ):
    self.index= index
    self.no = no
    self.text = text
    self.question= question
    self.branch1= branch1
    self.branch2= branch2

scenes = []

file = open("StoryLine.txt", "r")
for line in file:
    line = file.readline()
    attributes = line.split("#")
    s = Scene(attributes[0], attributes[1],attributes[2],attributes[3],attributes[4],attributes[5])

    scenes.append(s)
    print(len(scenes))

i = 0
print (scenes[i].text)
userChoice=input(scenes[i].question)
while scenes[i].branch1 != "":
    if userChoice == "1":
        i = int(scenes[i].branch1)
    else:
        i = int(scenes[i].branch2)
    print (scenes[i].text)
    userChoice=input(scenes[i].question)


"""
Style: main and subroutines
Components: main program and subroutines
Connectors: procedure calls
Constraints: components can't communicate using shared memory (e.g. global
variables)
Gains: change the implementation of a subroutine without affecting the main
program.
"""

from random import randint

import pygame
from pygame.locals import KEYDOWN, QUIT, K_ESCAPE, K_UP, K_DOWN, K_LEFT, K_RIGHT

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

def process_input(prev_direction):
    game_status, direction = 1, prev_direction
    for event in pygame.event.get():
        if event.type == QUIT:
            game_status = 0 # 0 for game over, 1 for play
        if event.type == KEYDOWN:
            key = event.key
            if key == K_ESCAPE:
                game_status = 0
            elif key == K_UP:
                direction = (0, -1)
            elif key == K_DOWN:
                direction = (0, 1)
            elif key == K_LEFT:
                direction = (-1, 0)
            elif key == K_RIGHT:
                direction = (1, 0)
    return game_status, direction

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

def draw_everything(screen, mybox, pellets, borders):
    screen.fill((0, 0, 64))  # dark blue
    [pygame.draw.rect(screen, (0, 191, 255), b) for b in borders]  # # Deep Sky Blue
    [pygame.draw.rect(screen, (255, 192, 203), p) for p in pellets]  # pink
    pygame.draw.rect(screen, (0, 191, 255), mybox)  # Deep Sky Blue
    pygame.display.update()

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

def create_box(dims):
    w, h = dims
    box = pygame.Rect(w / 2, h / 2, 10, 10)  # start in middle of the screen
    direction = 0, 1  # start direction: down
    return box, direction

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

def collide(box, boxes):
    # return True if box collides with any entity in boxes
    return box.collidelist(boxes) != -1

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

def move(box, direction):
    return box.move(direction[0], direction[1])

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

def create_borders(dims, thickness=2):
    w, h = dims
    return [pygame.Rect(0, 0, thickness, h),
            pygame.Rect(0, 0, w, thickness),
            pygame.Rect(w - thickness, 0, thickness, h),
            pygame.Rect(0, h - thickness, w, thickness)]

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

def create_pellet(dims, offset):
    w, h = dims
    return pygame.Rect(randint(offset, w - offset),
                       randint(offset, h - offset), 5, 5)

def create_pellets(dims, qty, offset=10):
    # this is the only subroutine independent of Pygame
    return [create_pellet(dims, offset) for _ in range(qty)]

def eat_and_replace_colliding_pellet(box, pellets, dims, offset=10):
    p_index = box.collidelist(pellets)
    if p_index != -1:  # ate a pellet: grow, and replace a pellet
        pellets[p_index] = create_pellet(dims, offset)
        box.size = box.width * 1.2, box.height * 1.2
    return box, pellets

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

pygame.init()
clock = pygame.time.Clock()

# display
dims = 400, 300
screen = pygame.display.set_mode(dims)

# game objects
borders = create_borders(dims)
pellets = create_pellets(dims, 4)
mybox, direction = create_box(dims)

# game loop
game_status = 1  # 0 for game over, 1 for play
while game_status:

    game_status, direction = process_input(direction)

    mybox = move(mybox, direction)
    if collide(mybox, borders):
        mybox, direction = create_box(dims)
    mybox, pellets = eat_and_replace_colliding_pellet(mybox, pellets, dims)

    draw_everything(screen, mybox, pellets, borders)

    clock.tick(50)  # or sleep(.02) to have the loop Pygame-independent


from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf.urls import include
from django.contrib import admin
from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='SoccerHome'),  # home page main app
    path('Teams', views.team_home, name='SoccerTeam'),
    #path('viewTeams', views.create_home, name='SoccerTeamCreate'),
    path('register', views.register_page, name='registerPage'),
    path('Player', views.player_register, name='registerForPlayer'),  # Registering page path for a player
    path('team', views.team_register, name='registerForTeam'),  # Registering page Path for a team
    path('coach', views.coach_register, name='registerForCoach'),  # Registering page Path for a Coaches
    path('referee', views.referee_register, name='registerForReferee'),  # Registering page Path for a Referee
    path('tournament', views.tournament_register, name='registerForTournament'),  # Registering page Path for Tournament
    path('details', views.details_page, name='DetailsPage'),
    path('<int:pk>/PlayerDetail/', views.player_details, name="PlayerDetails"),
    path('<int:pk>/TeamDetail/', views.team_details, name="TeamDetails"),
    path('<int:pk>/CoachDetail/', views.coach_details, name="CoachDetails"),
    path('<int:pk>/RefereeDetail/', views.referee_details, name="RefereeDetails"),
    path('<int:pk>/TournamentDetail/', views.tournament_details, name="TournamentDetails"),
    path('<int:pk>/PlayerDelete/', views.player_delete, name="PlayerDelete"),
    path('<int:pk>/CoachDelete/', views.coach_delete, name="CoachDelete"),
    path('<int:pk>/RefereeDelete/', views.referee_delete, name="RefereeDelete"),
    path('<int:pk>/TournamentDelete/', views.tournament_delete, name="TournamentDelete"),
    path('<int:pk>/TeamDelete/', views.team_delete, name="TeamDelete")
]


from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
from Cython.Build import cythonize
import numpy
import os

# If you have the Intel MKL installed,
# you can use the Intel Link Line advisor
# and replace the fftw3 library with MKL
# once you have compiled the FFTW3 interface
# called fftw3xc

libs = ['m', 'fftw3f']
args = ['-std=c99', '-O3','-DuseOMP', '-fopenmp']
sources = ['src/ccfj.pyx', 'src/CrossCorr.cpp','src/FJcpu.cpp']
include = ['include',numpy.get_include()]
linkerargs = ['-Wl,-rpath,lib','-fopenmp']
libdirs = ['lib']

extensions = [
    Extension("ccfj",
              sources=sources,
              include_dirs=include,
              libraries=libs,
              library_dirs=libdirs,
              extra_compile_args=args,
              language='c++',

              extra_link_args=linkerargs)
]

setup(name='ccfj',
      version='0.1',
      author='Zhengbo Li et al.',
      packages=['ccfj'],
      ext_modules=cythonize(extensions),
      )


# -*- coding: utf-8 -*-
"""
Created on Thu Oct 11 14:54:08 2018

@author: Luciano A. Masullo
"""

import time
import sys
import struct
import numpy as np

# Tag Types
tyEmpty8      = struct.unpack(">i", bytes.fromhex("FFFF0008"))[0]
tyBool8       = struct.unpack(">i", bytes.fromhex("00000008"))[0]
tyInt8        = struct.unpack(">i", bytes.fromhex("10000008"))[0]
tyBitSet64    = struct.unpack(">i", bytes.fromhex("11000008"))[0]
tyColor8      = struct.unpack(">i", bytes.fromhex("12000008"))[0]
tyFloat8      = struct.unpack(">i", bytes.fromhex("20000008"))[0]
tyTDateTime   = struct.unpack(">i", bytes.fromhex("21000008"))[0]
tyFloat8Array = struct.unpack(">i", bytes.fromhex("2001FFFF"))[0]
tyAnsiString  = struct.unpack(">i", bytes.fromhex("4001FFFF"))[0]
tyWideString  = struct.unpack(">i", bytes.fromhex("4002FFFF"))[0]
tyBinaryBlob  = struct.unpack(">i", bytes.fromhex("FFFFFFFF"))[0]

# Record types
rtPicoHarpT3     = struct.unpack(">i", bytes.fromhex('00010303'))[0]
rtPicoHarpT2     = struct.unpack(">i", bytes.fromhex('00010203'))[0]
rtHydraHarpT3    = struct.unpack(">i", bytes.fromhex('00010304'))[0]
rtHydraHarpT2    = struct.unpack(">i", bytes.fromhex('00010204'))[0]
rtHydraHarp2T3   = struct.unpack(">i", bytes.fromhex('01010304'))[0]
rtHydraHarp2T2   = struct.unpack(">i", bytes.fromhex('01010204'))[0]
rtTimeHarp260NT3 = struct.unpack(">i", bytes.fromhex('00010305'))[0]
rtTimeHarp260NT2 = struct.unpack(">i", bytes.fromhex('00010205'))[0]
rtTimeHarp260PT3 = struct.unpack(">i", bytes.fromhex('00010306'))[0]
rtTimeHarp260PT2 = struct.unpack(">i", bytes.fromhex('00010206'))[0]
rtMultiHarpNT3   = struct.unpack(">i", bytes.fromhex('00010307'))[0]
rtMultiHarpNT2   = struct.unpack(">i", bytes.fromhex('00010207'))[0]

def readHeaders(inputfile):

    magic = inputfile.read(8).decode("utf-8").strip('\0')
    if magic != "PQTTTR":
        print("ERROR: Magic invalid, this is not a PTU file.")
        inputfile.close()
        exit(0)

    version = inputfile.read(8).decode("utf-8").strip('\0')
    print('Version', version)

    tagDataList = []    # Contains tuples of (tagName, tagValue)
    while True:
        tagIdent = inputfile.read(32).decode("utf-8").strip('\0')
        tagIdx = struct.unpack("<i", inputfile.read(4))[0]
        tagTyp = struct.unpack("<i", inputfile.read(4))[0]
#        print(tagTyp)
        if tagIdx > -1:
            evalName = tagIdent + '(' + str(tagIdx) + ')'
        else:
            evalName = tagIdent
#        outputfile.write("\n%-40s" % evalName)
        if tagTyp == tyEmpty8:
            inputfile.read(8)
#            outputfile.write("<empty Tag>")
            tagDataList.append((evalName, "<empty Tag>"))
        elif tagTyp == tyBool8:
            tagInt = struct.unpack("<q", inputfile.read(8))[0]
            if tagInt == 0:
#                outputfile.write("False")
                tagDataList.append((evalName, "False"))
            else:
#                outputfile.write("True")
                tagDataList.append((evalName, "True"))
        elif tagTyp == tyInt8:
            tagInt = struct.unpack("<q", inputfile.read(8))[0]
#            outputfile.write("%d" % tagInt)
            tagDataList.append((evalName, tagInt))
        elif tagTyp == tyBitSet64:
            tagInt = struct.unpack("<q", inputfile.read(8))[0]
#            outputfile.write("{0:#0{1}x}".format(tagInt,18))
            tagDataList.append((evalName, tagInt))
        elif tagTyp == tyColor8:
            tagInt = struct.unpack("<q", inputfile.read(8))[0]
#            outputfile.write("{0:#0{1}x}".format(tagInt,18))
            tagDataList.append((evalName, tagInt))
        elif tagTyp == tyFloat8:
            tagFloat = struct.unpack("<d", inputfile.read(8))[0]
#            outputfile.write("%-3E" % tagFloat)
            tagDataList.append((evalName, tagFloat))
        elif tagTyp == tyFloat8Array:
            tagInt = struct.unpack("<q", inputfile.read(8))[0]
#            outputfile.write("<Float array with %d entries>" % tagInt/8)
            tagDataList.append((evalName, tagInt))
        elif tagTyp == tyTDateTime:
            tagFloat = struct.unpack("<d", inputfile.read(8))[0]
            tagTime = int((tagFloat - 25569) * 86400)
            print('tagTime', tagTime)
            tagTime = time.gmtime(tagTime)
#            outputfile.write(time.strftime("%a %b %d %H:%M:%S %Y", tagTime))
            tagDataList.append((evalName, tagTime))
        elif tagTyp == tyAnsiString:
            tagInt = struct.unpack("<q", inputfile.read(8))[0]
            tagString = inputfile.read(tagInt).decode("utf-8").strip("\0")
#            outputfile.write("%s" % tagString)
            tagDataList.append((evalName, tagString))
        elif tagTyp == tyWideString:
            tagInt = struct.unpack("<q", inputfile.read(8))[0]
            tagString = inputfile.read(tagInt).decode("utf-16le", errors="ignore").strip("\0")
#            outputfile.write(tagString)
            tagDataList.append((evalName, tagString))
        elif tagTyp == tyBinaryBlob:
            tagInt = struct.unpack("<q", inputfile.read(8))[0]
#            outputfile.write("<Binary blob with %d bytes>" % tagInt)
            tagDataList.append((evalName, tagInt))
        else:
            print("ERROR: Unknown tag type")
            exit(0)
        if tagIdent == "Header_End":
            break

    # Reformat the saved data for easier access
    tagNames = [tagDataList[i][0] for i in range(0, len(tagDataList))]
    tagValues = [tagDataList[i][1] for i in range(0, len(tagDataList))]

    # get important variables from headers
    numRecords = tagValues[tagNames.index("TTResult_NumberOfRecords")]
    globRes = tagValues[tagNames.index("MeasDesc_GlobalResolution")]
    timeRes = tagValues[tagNames.index("MeasDesc_Resolution")]

    return numRecords, globRes, timeRes

def readPT3(inputfile, numRecords):

    oflcorrection = 0
    dlen = 0
    T3WRAPAROUND = 65536

    dtime_array = np.zeros(numRecords)
    truensync_array = np.zeros(numRecords)

    for recNum in range(0, numRecords):

        try:
            recordData = "{0:0{1}b}".format(struct.unpack("<I", inputfile.read(4))[0], 32)
        except:
            print("The file ended earlier than expected, at record %d/%d."\
                  % (recNum, numRecords))
            exit(0)

        channel = int(recordData[0:4], base=2)
        dtime = int(recordData[4:16], base=2)
        nsync = int(recordData[16:32], base=2)

        if channel == 0xF: # Special record

            if dtime == 0: # Not a marker, so overflow
#                print("%u OFL * %2x\n" % (recNum, 1))
                oflcorrection += T3WRAPAROUND

            else: # got marker
                truensync = oflcorrection + nsync
                print("%u MAR %2x %u\n" % (recNum, truensync, dtime))

        else: # standard record, photon count

            if channel == 0 or channel > 4: # Should not occur
                print("Illegal Channel: #%1d %1u" % (dlen, channel))

            truensync = oflcorrection + nsync

            dtime_array[recNum] = dtime
            truensync_array[recNum] = truensync

            dlen += 1

        if recNum % 100000 == 0:
            sys.stdout.write("\rProgress: %.1f%%" % (float(recNum)*100/float(numRecords)))
            sys.stdout.flush()

    return dtime_array, truensync_array


import X3Dpackage

X3D0 = (X3Dpackage.X3D().setProfile("Immersive").setVersion("3.3")
      .setHead(X3Dpackage.head()
        .addMeta(X3Dpackage.meta().setName("title").setContent("x3dconnectorProto"))
        .addMeta(X3Dpackage.meta().setName("creator").setContent("Lost, Doug Sanden I think"))
        .addMeta(X3Dpackage.meta().setName("generator").setContent("manual"))
        .addMeta(X3Dpackage.meta().setName("identifier").setContent("https://coderextreme.net/X3DJSONLD/x3dconnectorProto.x3d"))
        .addMeta(X3Dpackage.meta().setName("description").setContent("a generic proto to connect two objects")))
      .setScene(X3Dpackage.Scene()
        .addChildren(X3Dpackage.Viewpoint().setPosition([0,0,5]).setDescription("Only Viewpoint"))
        .addChildren(X3Dpackage.Background().setSkyColor([0.4,0.4,0.4]))
        .addChildren(X3Dpackage.Transform().setDEF("DECLpoint_G1_node").setTranslation([0,0,0])
          .addChildren(X3Dpackage.Shape()
            .setGeometry(X3Dpackage.Sphere(setRadius = 0.1))
            .setAppearance(X3Dpackage.Appearance()
              .setMaterial(X3Dpackage.Material().setDiffuseColor([1,0,0]))))
          .addChildren(X3Dpackage.PositionInterpolator().setDEF("DECLpoint_G1_PI1").setKey([0,1]).setKeyValue([0,0,0,0,5,0]))
          .addChildren(X3Dpackage.Script().setDEF("DECLpoint_G1_MB1")
            .addField(X3Dpackage.field().setName("translation").setAccessType("inputOutput").setType("SFVec3f").setValue("0 0 0"))
            .addField(X3Dpackage.field().setName("old").setAccessType("inputOutput").setType("SFVec3f").setValue("0 0 0"))
            .addField(X3Dpackage.field().setName("set_location").setAccessType("inputOnly").setType("SFTime"))
            .addField(X3Dpackage.field().setName("keyValue").setAccessType("inputOutput").setType("MFVec3f").setValue("0 0 0 0 5 0")).setSourceCode('''ecmascript:\n"+
"		function set_location(value) {\n"+
"                    old = translation;\n"+
"		    translation = new SFVec3f(Math.random()*10-5, Math.random()*10-5, Math.random()*10-5);\n"+
"                    keyValue = new MFVec3f([old, translation]);\n"+
"		    // Browser.println(keyValue);\n"+
"		}\n"+
"''')
)
          .addChildren(X3Dpackage.TimeSensor().setDEF("DECLpoint_G1_CL1").setCycleInterval(3).setLoop(True))
          .addChildren(X3Dpackage.ROUTE().setFromNode("DECLpoint_G1_CL1").setFromField("cycleTime").setToNode("DECLpoint_G1_MB1").setToField("set_location"))
          .addChildren(X3Dpackage.ROUTE().setFromNode("DECLpoint_G1_CL1").setFromField("fraction_changed").setToNode("DECLpoint_G1_PI1").setToField("set_fraction"))
          .addChildren(X3Dpackage.ROUTE().setFromNode("DECLpoint_G1_MB1").setFromField("keyValue").setToNode("DECLpoint_G1_PI1").setToField("keyValue"))
          .addChildren(X3Dpackage.ROUTE().setFromNode("DECLpoint_G1_PI1").setFromField("value_changed").setToNode("DECLpoint_G1_node").setToField("set_translation")))
        .addChildren(X3Dpackage.Transform().setDEF("DECLpoint_G2_node").setTranslation([0,0,0])
          .addChildren(X3Dpackage.Shape()
            .setGeometry(X3Dpackage.Sphere(setRadius = 0.1))
            .setAppearance(X3Dpackage.Appearance()
              .setMaterial(X3Dpackage.Material().setDiffuseColor([1,0,0]))))
          .addChildren(X3Dpackage.PositionInterpolator().setDEF("DECLpoint_G2_PI1").setKey([0,1]).setKeyValue([0,0,0,0,5,0]))
          .addChildren(X3Dpackage.Script().setDEF("DECLpoint_G2_MB1")
            .addField(X3Dpackage.field().setName("translation").setAccessType("inputOutput").setType("SFVec3f").setValue("0 0 0"))
            .addField(X3Dpackage.field().setName("old").setAccessType("inputOutput").setType("SFVec3f").setValue("0 0 0"))
            .addField(X3Dpackage.field().setName("set_location").setAccessType("inputOnly").setType("SFTime"))
            .addField(X3Dpackage.field().setName("keyValue").setAccessType("inputOutput").setType("MFVec3f").setValue("0 0 0 0 5 0")).setSourceCode('''ecmascript:\n"+
"		function set_location(value) {\n"+
"                    old = translation;\n"+
"		    translation = new SFVec3f(Math.random()*10-5, Math.random()*10-5, Math.random()*10-5);\n"+
"                    keyValue = new MFVec3f([old, translation]);\n"+
"		    // Browser.println(keyValue);\n"+
"		}\n"+
"''')
)
          .addChildren(X3Dpackage.TimeSensor().setDEF("DECLpoint_G2_CL1").setCycleInterval(3).setLoop(True))
          .addChildren(X3Dpackage.ROUTE().setFromNode("DECLpoint_G2_CL1").setFromField("cycleTime").setToNode("DECLpoint_G2_MB1").setToField("set_location"))
          .addChildren(X3Dpackage.ROUTE().setFromNode("DECLpoint_G2_CL1").setFromField("fraction_changed").setToNode("DECLpoint_G2_PI1").setToField("set_fraction"))
          .addChildren(X3Dpackage.ROUTE().setFromNode("DECLpoint_G2_MB1").setFromField("keyValue").setToNode("DECLpoint_G2_PI1").setToField("keyValue"))
          .addChildren(X3Dpackage.ROUTE().setFromNode("DECLpoint_G2_PI1").setFromField("value_changed").setToNode("DECLpoint_G2_node").setToField("set_translation")))
        .addChildren(X3Dpackage.Group()
          .addChildren(X3Dpackage.Transform().setDEF("DECLx3dconnector_connector1_trans")
            .addChildren(X3Dpackage.Transform().setDEF("DECLx3dconnector_connector1_rotscale")
              .addChildren(X3Dpackage.Shape()
                .setAppearance(X3Dpackage.Appearance()
                  .setMaterial(X3Dpackage.Material().setDiffuseColor([0.2,0.7,0.7]).setTransparency(0.5)))
                .setGeometry(X3Dpackage.Cylinder(setRadius = 0.05)))))
          .addChildren(X3Dpackage.Script().setDEF("DECLx3dconnector_connector1_S1")
            .addField(X3Dpackage.field().setName("startnode").setAccessType("initializeOnly").setType("SFNode")
              .addChildren(X3Dpackage.Group().setUSE("DECLpoint_G1_node")))
            .addField(X3Dpackage.field().setName("endnode").setAccessType("initializeOnly").setType("SFNode")
              .addChildren(X3Dpackage.Group().setUSE("DECLpoint_G2_node")))
            .addField(X3Dpackage.field().setName("position").setAccessType("inputOutput").setType("SFNode")
              .addChildren(X3Dpackage.Transform().setUSE("DECLx3dconnector_connector1_trans")))
            .addField(X3Dpackage.field().setName("rotscale").setAccessType("inputOutput").setType("SFNode")
              .addChildren(X3Dpackage.Transform().setUSE("DECLx3dconnector_connector1_rotscale")))
            .addField(X3Dpackage.field().setName("set_startpoint").setAccessType("inputOnly").setType("SFVec3f"))
            .addField(X3Dpackage.field().setName("set_endpoint").setAccessType("inputOnly").setType("SFVec3f")).setSourceCode('''ecmascript:\n"+
"        function recompute(startpoint,endpoint){\n"+
"	    if (typeof endpoint === 'undefined') {\n"+
"		return;\n"+
"	    }\n"+
"            var dif = endpoint.subtract(startpoint);\n"+
"            var dist = dif.length()*0.5;\n"+
"            var dif2 = dif.multiply(0.5);\n"+
"            var norm = dif.normalize();\n"+
"            var transl = startpoint.add(dif2);\n"+
"	    if (typeof Quaternion !== 'undefined') {\n"+
"		    return {\n"+
"			    scale : new SFVec3f(1.0,dist,1.0),\n"+
"			    translation : transl,\n"+
"			    rotation : new Quaternion.rotateFromTo(new SFVec3f(0.0,1.0,0.0), norm)\n"+
"		    };\n"+
"	    } else if (typeof SFRotation !== 'undefined') {\n"+
"		    return {\n"+
"			    scale : new SFVec3f(1.0,dist,1.0),\n"+
"			    translation : transl,\n"+
"			    rotation : new SFRotation(new SFVec3f(0.0,1.0,0.0),norm)\n"+
"		    };\n"+
"	    } else {\n"+
"		    return {\n"+
"			    scale : new SFVec3f(1.0,dist,1.0),\n"+
"			    translation : transl\n"+
"		    };\n"+
"	    }\n"+
"	}\n"+
"	function recompute_and_route(startpoint, endpoint) {\n"+
"	      var trafo = recompute(startpoint, endpoint);\n"+
"	      position.translation = trafo.translation;\n"+
"	      rotscale.rotation = trafo.rotation;\n"+
"	      rotscale.scale = trafo.scale;\n"+
"	}\n"+
"        function initialize(){\n"+
"            recompute_and_route(startnode.translation,endnode.translation);\n"+
"        }\n"+
"        function set_startpoint(val,t){\n"+
"            recompute_and_route(val,endnode.translation);\n"+
"        }\n"+
"        function set_endpoint(val,t){\n"+
"            recompute_and_route(startnode.translation,val);\n"+
"        }\n"+
"''')
))
        .addChildren(X3Dpackage.ROUTE().setFromNode("DECLpoint_G1_node").setFromField("translation").setToNode("DECLx3dconnector_connector1_S1").setToField("set_startpoint"))
        .addChildren(X3Dpackage.ROUTE().setFromNode("DECLpoint_G2_node").setFromField("translation").setToNode("DECLx3dconnector_connector1_S1").setToField("set_endpoint"))))


GIVEN_FILE_NAME = "given_12.txt"
INPUT_FILE_NAME = "input_12.txt"

from typing import List, Tuple

class Grid:
    def __init__(self):
        self.rows: List[list] = []
        self.starting_point: Tuple(int, int) = None
        self.ending_point: Tuple(int, int) = None

    def add_row(self, row: list):
        self.rows.append(row)

    def get_rows(self) -> List[list]:
        return self.rows

    def get_columns(self) -> List[list]:
        return list(map(list, zip(*self.rows)))

    def get_item(self, column, row):
        if (
            row < 0
            or column < 0
            or row >= len(self.rows)
            or column >= len(self.rows[0])
        ):
            return None
        return self.rows[row][column]

    def set_starting_point(self, column: int, row: int):
        self.starting_point = (column, row)

    def set_ending_point(self, column: int, row: int):
        self.ending_point = (column, row)

    def __repr__(self):
        return "\n".join("".join(str(item) for item in row) for row in self.rows)

class Solver:
    STARTING_POINT = "S"
    ENDING_POINT = "E"

    def parse_file(self, input_file_name: str) -> Grid:
        grid = Grid()
        current_row = 0
        with open(input_file_name) as f:
            current_column = 0
            for line in f:
                stripped_line: str = line.strip()
                list_of_elevations: list[str] = []
                for elevation_raw in stripped_line:
                    if elevation_raw == self.STARTING_POINT:
                        list_of_elevations.append("a")
                        grid.set_starting_point(current_column, current_row)
                    elif elevation_raw == self.ENDING_POINT:
                        list_of_elevations.append("z")
                        grid.set_ending_point(current_column, current_row)
                    else:
                        list_of_elevations.append(elevation_raw)
                    current_column += 1
                grid.add_row(list_of_elevations)
                current_column = 0
                current_row += 1
        return grid

    def get_shortest_scenic_path(self, grid: Grid) -> int:
        min_length = None
        current_row = 0
        possible_starting_points = []
        for row in grid.rows:
            current_column = 0
            for height in row:
                if height == "a" or height == "S":
                    possible_starting_points.append((current_column, current_row))
                current_column += 1
            current_row += 1
        for starting_point in possible_starting_points:
            column, row = starting_point
            grid.set_starting_point(column, row)
            length = self.get_shortest_path_length(grid)
            if length > 0 and (min_length is None or length < min_length):
                min_length = length
        return min_length

    def get_shortest_path_length(self, grid: Grid) -> int:
        starting_column, starting_row = grid.starting_point
        ending_column, ending_row = grid.ending_point
        points_to_visit = [(starting_column, starting_row, 0)]
        min_length = None
        visited_points = set()
        while points_to_visit:
            current_column, current_row, length_so_far = points_to_visit.pop(0)
            if current_column == ending_column and current_row == ending_row:
                if min_length is None or length_so_far < min_length:
                    min_length = length_so_far
            current_elevation = grid.get_item(current_column, current_row)
            # Top
            top_elevation = grid.get_item(current_column, current_row - 1)
            if (
                top_elevation
                and ord(top_elevation) <= (ord(current_elevation) + 1)
                and (current_column, current_row - 1) not in visited_points
            ):
                points_to_visit.append(
                    (current_column, current_row - 1, length_so_far + 1)
                )
                visited_points.add((current_column, current_row - 1))

            # Left
            left_elevation = grid.get_item(current_column - 1, current_row)
            if (
                left_elevation
                and ord(left_elevation) <= (ord(current_elevation) + 1)
                and (current_column - 1, current_row) not in visited_points
            ):
                points_to_visit.append(
                    (current_column - 1, current_row, length_so_far + 1)
                )
                visited_points.add((current_column - 1, current_row))

            # Bottom
            bottom_elevation = grid.get_item(current_column, current_row + 1)
            if (
                bottom_elevation
                and ord(bottom_elevation) <= (ord(current_elevation) + 1)
                and (current_column, current_row + 1) not in visited_points
            ):
                points_to_visit.append(
                    (current_column, current_row + 1, length_so_far + 1)
                )
                visited_points.add((current_column, current_row + 1))

            # Right
            right_elevation = grid.get_item(current_column + 1, current_row)
            if (
                right_elevation
                and ord(right_elevation) <= (ord(current_elevation) + 1)
                and (current_column + 1, current_row) not in visited_points
            ):
                points_to_visit.append(
                    (current_column + 1, current_row, length_so_far + 1)
                )
                visited_points.add((current_column + 1, current_row))
        return min_length or -1

import unittest

class SolverTest(unittest.TestCase):
    def solve_1(self, input_file_name: str) -> int:
        solver = Solver()
        grid = solver.parse_file(input_file_name)
        shortest_path_length: int = solver.get_shortest_path_length(grid)
        return shortest_path_length

    def test_given_1(self):
        shortest_path_length = self.solve_1(GIVEN_FILE_NAME)
        self.assertEqual(shortest_path_length, 31)

    def test_input_1(self):
        shortest_path_length = self.solve_1(INPUT_FILE_NAME)
        self.assertEqual(shortest_path_length, 468)

    def solve_2(self, input_file_name: str) -> int:
        solver = Solver()
        grid = solver.parse_file(input_file_name)
        shortest_scenic_path: int = solver.get_shortest_scenic_path(grid)
        return shortest_scenic_path

    def test_given_2(self):
        shortest_scenic_path = self.solve_2(GIVEN_FILE_NAME)
        self.assertEqual(shortest_scenic_path, 29)

    def test_input_2(self):
        shortest_scenic_path = self.solve_2(INPUT_FILE_NAME)
        self.assertEqual(shortest_scenic_path, 459)

unittest.main(exit=False)


from flask import jsonify, make_response, request
from flask_pymongo import PyMongo, ObjectId
from ..app import app

mongo = PyMongo(app)
valoraciones = mongo.db.valoraciones

# ---- ENDPOINTS -------------------------------------------------------------------------

@app.route("/api/v1/valoraciones", methods=["POST"])
def createValoracion():
    if request.is_json:
        if valoraciones.find_one({'idviaje' : ObjectId(request.json['idviaje']), 'usuarioQueValora' : ObjectId(request.json["usuarioQueValora"]), 'usuarioValorado' : ObjectId(request.json['usuarioValorado'])}) is None :

            try:
                res = valoraciones.insert_one({
                    "usuarioValorado": ObjectId(request.json["usuarioValorado"]),
                    "nota": str(request.json["nota"]),
                    "descripcion": str(request.json["descripcion"]),
                    "usuarioQueValora": ObjectId(request.json["usuarioQueValora"]),
                    "idviaje": ObjectId(request.json["idviaje"]),
                    "nombre": str(request.json["nombre"]),
                    "apellidos": str(request.json["apellidos"]),
                    "url_foto_perfil": str(request.json["url_foto_perfil"]),
                })
                return jsonify(msg="Valoración creada", new_id=str(res.inserted_id))

            except:
                respuesta = jsonify(msg="Petición no válida, faltan campos o no son del tipo correcto")
                return make_response(respuesta, 400)
        else :
            print(valoraciones.find_one({'idviaje' : ObjectId(request.json['idviaje']), 'usuarioQueValora' : ObjectId(request.json["usuarioQueValora"]), 'usuarioValorado' : ObjectId(request.json['usuarioValorado'])}))
            return jsonify(msg="El usuario ya ha valorado a esa persona en el mismo trayecto")
    else:
        respuesta = jsonify(msg="Petición no válida, se requiere JSON")
        return make_response(respuesta, 400)

@app.route("/api/v1/valoraciones/<id>", methods=["GET"])
def getValoracionesPorUsuario(id):
    media = 0
    res = []
    for doc in valoraciones.find({'usuarioValorado' : ObjectId(id)}):
        media = media + int(doc['nota'])
        res.append({
            '_id' : str(doc['_id']),
            'usuarioValorado' : str(doc['usuarioValorado']),
            'nota' : doc['nota'],
            'descripcion' : doc['descripcion'],
            'usuarioQueValora' : str(doc['usuarioQueValora']),
            'idviaje' : str(doc['idviaje']),
            "nombre": doc["nombre"],
            "apellidos": doc["apellidos"],
            "url_foto_perfil": doc["url_foto_perfil"]
        })

    if media > 0:
        media = int(media/len(res))
    return jsonify({ 'media' : media, 'valoraciones' : res})

@app.route("/api/v1/valoraciones/<uValorado>/<uValora>/<idViaje>", methods=["GET"])
def getValoracion(uValorado,uValora,idViaje):

    res = valoraciones.find_one({
        'idviaje' : ObjectId(idViaje),
        'usuarioQueValora' : ObjectId(uValora),
        'usuarioValorado' : ObjectId(uValorado)})
    print(res)
    if (res is not None):
        return jsonify({ "msg": str(res["_id"])})
    else:
        return jsonify("null")


import numpy as np
import matplotlib.pyplot as plt
import math
import scipy.linalg as linalg
import scipy.signal as signal
import json
from collections import deque

def peakHold(sig, holdTime):
    """
    sig: 入力信号。
    holdTime: ホールド時間。単位はサンプル数。
    """
    out = np.empty(len(sig))
    buffer = deque([0 for _ in range(holdTime)])
    hold = deque([])
    for i in range(len(sig)):
        x0 = sig[i]

        if len(hold) > 0:
            idx = len(hold) - 1
            while idx >= 0:
                if hold[idx] < x0:
                    hold.pop()
                else:
                    break
                idx -= 1

        hold.append(x0)

        buffer.append(x0)
        delayOut = buffer.popleft()

        if len(hold) > 0 and delayOut == hold[0]:
            hold.popleft()

        out[i] = hold[0] if len(hold) > 0 else 0

    return out

def applyCharacteristicCurve(amp, threshold):
    with np.errstate(divide='ignore', invalid='ignore'):
        return np.where(amp > threshold, threshold / amp, 1)

def getTriangleWindow(length):
    win = signal.get_window("bartlett", length)[::-1]
    win /= np.sum(win)
    return win

def applyLimiter(sig, absed, holdTime):
    hold = peakHold(absed, holdTime)
    gain = applyCharacteristicCurve(hold, 1.0)
    fir = getTriangleWindow(holdTime)
    smoothed = np.convolve(gain, fir)[holdTime - 1:]
    return sig * smoothed

def generateSignal(upRate, length, seed=None):
    rng = np.random.default_rng(seed)
    sig = 2.0 * rng.binomial(1, 0.5, length) - 1
    up = signal.resample(sig, upRate * len(sig))
    downAbs = np.max(np.abs(up).reshape((len(sig), upRate)), axis=1)
    return (sig, up, downAbs)

def testGenerateSignal():
    length = 16
    seed = 6871867313
    sig, up, downAbs = generateSignal(16, length, seed)

    plt.plot(np.abs(up))
    plt.plot(np.repeat(downAbs, 16))
    plt.grid()
    plt.show()

if __name__ == "__main__":
    length = 2048
    holdTime = 128
    upRate = 16
    seed = 6415456
    fix = 10**(-0.2 / 20)  # Safe guard gain. Not used for demonstration.

    sig, up, downAbs = generateSignal(upRate, length, seed)

    # This implementation skips pre-lowpass which applies to `sig` to reduce overshoots.

    limited = applyLimiter(sig, downAbs, holdTime)

    upLimited = signal.resample(limited, upRate * len(limited))
    peak = np.max(np.abs(upLimited))
    print(f"Peak [dB]: {20 * math.log10(peak)}")
    # upLimited *= fix  # Safe guard gain is disabled for demonstration.

    peakIndex = np.where(np.abs(upLimited) > 1)[0]
    plt.plot(up, lw=1, color="red", alpha=0.5, label="16x Source")
    plt.plot(upLimited, lw=1, color="black", label="16x Limited")
    plt.scatter(peakIndex, upLimited[peakIndex], marker="s", zorder=10, label="Overshoot")
    plt.legend(loc="lower right")
    plt.grid(color="#f0f0f0")
    plt.tight_layout()
    plt.show()


# coding: utf-8

import requests

def file_get_contents(filename, use_include_path=False, context=None, offset=None, maxlen=None):
    if filename.lower().startswith("http://") or filename.lower().startswith("https://"):
        resp = requests.get(filename)
        content = resp.content
        if offset is not None:
            content = content[offset:]
        if maxlen is not None:
            content = content[:maxlen]
        return content.decode("utf-8", errors="ignore")
    else:
        with open(filename) as f:
            return f.read()

if __name__ == '__main__':
    print(file_get_contents("https://www.onevgo.com"))


import random
r=[x for x in range(1,101)]
print("Welcome to The Guessing Game.\nGuess a number between 0 to 100.\nLet the Computer find it.\n")
while True:
	guess=random.randint(r[0],r[-1])
	print("Computer's guess is %d"%guess)
	x=input("\nIs Computer's Guess higher or lower or correct? ")
	if x=="lower":
		counter=r[0]
		while True:
			if counter>guess:
				break
			r.remove(counter)
			counter+=1
	elif x=="higher":
		counter=r[-1]
		while True:
			if counter<guess:
				break
			r.remove(counter)
			counter-=1
	elif x=="correct":
		print("\nComputer: I love wining the game.")
		break
	else:
		print("\nI'm sorry I didn't get that.")


# Problem: The function name, its docstring, and parameter list of 'fn' are hidden by
# the wrapper function.

# Solution: The functools.wraps decorator, opies the lost metadata from the undecorated function
# to the decorated closure.

from functools import wraps # wraps(fn) -> dadurch gehen Metadaten wie Docu nicht verloren
import time

# Debugger
print("----------DEBUGGER----------")

def debug(fn):
    @wraps(fn)
    def debugger(*args, **kwargs):
        args_values_types = [(a, type(a)) for a in args]
        kwargs_values_types = [(k, v, type(v)) for k, v in kwargs.items()]
        print("Args: {}".format(args_values_types))
        print("Kwargs: {}".format(kwargs_values_types))
        print("Function {} called".format(fn.__name__))
        fn_result = fn(*args, **kwargs)
        print("Function {} returns: {}".format(fn.__name__, fn_result))
        print("---------------------------------------------")
        return fn_result
    return debugger

@debug
def do_something(a, b, c=None):
    """Do something.
    """
    return a + b if c else 0

@debug
def do_something2(a, b, c=None):
    return a - b if c else 0

@debug
def do_something3(a, b, c=None):
    return a * b if c else 0

@debug
def do_something4(a, b, c=None):
    return a / b if c else 0

do_something(10, 20, c=1)
do_something2(10, 20, c=1)
do_something3(10, 20, c=1)
do_something4(10, 20, c=1)

# TIMER
print("----------TIMER----------")

def timing(fn):
    @wraps(fn)
    def timer(*args, **kwargs):
        start_time = time.perf_counter()
        fn_result = fn(*args, **kwargs)
        end_time = time.perf_counter()
        time_duration = end_time - start_time
        print("Function {} took: {} s".format(fn.__name__, time_duration))
        return fn_result
    return timer

@timing
def do_something5(a, b, c=None):
    """Do something.
    """
    return a + b if c else 0

do_something5(a=10, b=20, c=True)

@timing
def iterate(n):
    val = 0
    for i in range(n):
        val += i
    return val

iterate(1_000_000)


import pygame
import random
import time

pygame.init()

displayW = 1920
displayH = 1080

Display = pygame.display.set_mode((displayW,displayH), pygame.FULLSCREEN)

pygame.display.set_caption('game')

clock = pygame.time.Clock()

black = (0, 0, 0)
white = (255,255,255)
red = (255, 1, 1)
blue = (1, 1, 255)

x = (displayW/10)
y = (displayH - 155)
width = 125
height = 125
speed = 10
jump = False
jumpCount = 10

OBx = displayW - 70
OBy = displayH - 130
OBw = 80
OBh = 80

OB2x = displayW - 70
OB2y = displayH - 230
OB2w = 80
OB2h = 80

OB3x = displayW - 70
OB3y = displayH - 330
OB3w = 80
OB3h = 80

on = True

points = 0
lives = 3
on = True
testv = random.randrange(1, 9)

def player():
	pygame.draw.rect(Display, red, (x, y, width, height))

def Object():
	pygame.draw.rect(Display, white, (OBx, OBy, OBw, OBh))

def Object2():
	pygame.draw.rect(Display, white, (OB2x, OB2y, OB2w, OB2h))

def Object3():
	pygame.draw.rect(Display, white, (OB3x, OB3y, OB3w, OB3h))

def movement():
	global x, y, speed, width, jump, jumpCount
	movement = pygame.key.get_pressed()
	if movement[pygame.K_a] or movement[pygame.K_LEFT] and x > speed + 25:
		x -= speed
	elif movement[pygame.K_d]or movement[pygame.K_RIGHT] and x < displayW - 25 - speed - width:
		x += speed
	if not(jump):
		if movement[pygame.K_w]or movement[pygame.K_UP]:
			jump = True;
	else:
		if jumpCount >= -10:
			y -= (jumpCount * abs(jumpCount)) * .4
			jumpCount -= .5
		else:
			jumpCount = 10
			jump = False
def test():
	global testv, OBx, OB2x, OB3x
	if testv >= 1 and testv <= 3:
		OBx -= 20
		Object()
		ObjectCollision()
	if testv >= 4 and testv <= 6:
		OB2x -= 20
		Object2()
		Object2Collision()
	if testv >= 7 and testv <= 9:
		OB3x -= 20
		Object3()
		Object3Collision()

def ObjectCollision():
	global OBx, OBy, DisplayH, DisplayW, x, y, testv
	if OBx < 30:
		OBx = displayW - 70
		OBy = displayH - 130
		testv = random.randrange(1, 9)
		print('1')
	if x + 95 > OBx and x < OBx + 80 and y + 95 > OBy and y < OBy + 80:
		hit()

def Object2Collision():
	global OB2x, OB2y, DisplayH, DisplayW, x, y, testv
	if OB2x < 30:
		OB2x = displayW - 70
		OB2y = displayH - 230
		testv = random.randrange(1, 9)
		print('2')
	if x + 95 > OB2x and x < OB2x + 80 and y + 95 > OB2y and y < OB2y + 80:
		hit()

def Object3Collision():
	global OB3x, OB3y, DisplayH, DisplayW, x, y, testv
	if OB3x < 30:
		OB3x = displayW - 70
		OB3y = displayH - 330
		testv = random.randrange(1, 9)
		print('3')
	if x + 95 > OB3x and x < OB3x + 80 and y < OB3y + 80 and y + 120 > OB3y:
		hit()

def Boundarybox():
	pygame.draw.rect(Display, blue, (0, 0, 30, displayH)) #left
	pygame.draw.rect(Display, blue, (displayW, 0, -30, displayH)) #right
	pygame.draw.rect(Display, blue, (displayW, displayH, -displayW, -30)) #bottom
	pygame.draw.rect(Display, blue, (0, 0, displayW, 30)) #top

def fonttype(point, font):
	fontOBor = font.render(point, True, white)
	return fontOBor, fontOBor.get_rect()

def hit():
	global on, displayW, displayH
	Display.fill(black)
	largeText = pygame.font.SysFont('arial',115)
	fontOBor, TextRect = fonttype("Lost", largeText)
	TextRect.center = ((displayW/2),(displayH/2))
	Display.blit(fontOBor, TextRect)
	pygame.display.update()
	clock.tick(15)
	time.sleep(2)
	on = False
	print('lost')

def game():
	global on, points, OBx, OB2x, OB3x
	on = True
	while on:
		for event in pygame.event.get():
			if event.type == pygame.QUIT:
				on = False

		movement()
		Display.fill(black)
		Boundarybox()
		test()
		player()
		pygame.display.update()
		clock.tick(75)
		QorE = pygame.key.get_pressed()
		if QorE[pygame.K_ESCAPE]:
			on = False
game()


from django.conf import settings
from django.conf.urls.static import static
from django.urls import path, include
from .views import (
    Overview,
    MultipleOrdersView,
    OrderView,
    OrderItemView,
    SupportView,
    Errand,
    ProfileView,
    InfoView,
    Users,
    SpecificUser,
    EditUser,
    NewAddress,
    EditAdress,
    EditAdresses,
    SettingsView,
    EditCompany,
)

from member.views import CookieSettingsView, changePassword

app_name = 'support'

urlpatterns = [
    path('overview/', Overview.as_view(), name='overview'),
    path('orders/', MultipleOrdersView.as_view(), name='orders'),
    path('vieworder/<slug>', OrderView.as_view(), name='vieworder'),
    path('orderItem/<slug>', OrderItemView.as_view(), name='orderItem'),
    path('support/', SupportView.as_view(), name='support'),
    path('errand/<slug>', Errand.as_view(), name='errand'),
    path('search_users/', Users.as_view(), name='search_users'),
    path('specific_user/', SpecificUser.as_view(), name='specific_user'),
    path('my_profile/', ProfileView.as_view(), name='my_profile'),
    path('my_info/', InfoView.as_view(), name='my_info'),
    path('edit_user/', EditUser.as_view(), name='edit_user'),
    path('edit_company/', EditCompany.as_view(), name='edit_company'),
    path('edit_address/', EditAdress.as_view(), name='edit_address'),
    path('edit_addresses/', EditAdresses.as_view(), name='edit_addresses'),
    path('new_address/', NewAddress.as_view(), name='new_address'),
    path('settings/', CookieSettingsView.as_view(), name='settings'),
    path('user_settings/', SettingsView.as_view(), name='user_settings'),

]

# move cookies for general use to base


from django.urls import path
from .views import SignUpView, IndexView, sort, add_receipt, delete_receipt, update_receipt, include_receipts

urlpatterns = [
    path('signup/', SignUpView.as_view(), name='signup'),
    path('', IndexView.as_view(), name='index'),
]

htmx_urlpatterns = [
    path('sort/', sort, name='sort'),
    path('add-receipt/', add_receipt, name='add-receipt'),
    path('delete-receipt/<int:pk>/', delete_receipt, name='delete-receipt'),
    path('update-receipt/<int:pk>/', update_receipt, name='update-receipt'),
    path('include-receipts/', include_receipts, name='include-receipts')
]

urlpatterns += htmx_urlpatterns


# -*- coding: utf-8 -*-
# @author: Vitor Vilas Boas
import itertools
import numpy as np
from scipy.fftpack import fft
from scipy.linalg import eigh
from sklearn.pipeline import Pipeline
from scipy.signal import lfilter, butter
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.model_selection import cross_val_score, StratifiedShuffleSplit, StratifiedKFold

class CSP():
    def __init__(self, n_components):
        self.n_components = n_components
        # self.filters_ = None
    def fit(self, X, y):
        e, c, s = X.shape
        classes = np.unique(y)
        Xa = X[classes[0] == y,:,:]
        Xb = X[classes[1] == y,:,:]
        S0 = np.zeros((c, c))
        S1 = np.zeros((c, c))
        for epoca in range(int(e/2)):
            S0 += np.dot(Xa[epoca,:,:], Xa[epoca,:,:].T) #covA Xa[epoca]
            S1 += np.dot(Xb[epoca,:,:], Xb[epoca,:,:].T) #covB Xb[epoca]
        [D, W] = eigh(S0, S0 + S1)
        ind = np.empty(c, dtype=int)
        ind[0::2] = np.arange(c - 1, c // 2 - 1, -1)
        ind[1::2] = np.arange(0, c // 2)
        W = W[:, ind]
        self.filters_ = W.T[:self.n_components]
        return self # instruction add because cross-validation pipeline
    def transform(self, X):
        XT = np.asarray([np.dot(self.filters_, epoch) for epoch in X])
        XVAR = np.log(np.mean(XT ** 2, axis=2)) # Xcsp
        return XVAR

if __name__ == "__main__":

    subjects = ['aa','al','av','aw','ay']
    classes = [1, 3] # only RH, FT
    n_channels = 118
    csp_ncomp = np.arange(2, n_channels+1, 2)

    suj = subjects[1] # 0 a 4
    n_comp = 18 # 2 a n_channels

    fs = 100
    fl = 8
    fh = 30
    nf = fs/2.

    ordem = 5
    b, a = butter(ordem, [fl/nf, fh/nf], btype='bandpass')

    bin0 = int(fl * (fs/nf)) # para fl = 8 bin0 = 15
    binN = int(fh * (fs/nf)) # para fl = 8 bin0 = 15

    t_start,t_end = 0,4
    W_Start = int(t_start * fs)
    W_End = int(t_end * fs)

    X = list(np.load('/mnt/dados/datasets/eeg_epochs/BCI3_4a/' + suj + '.npy'))

    X = np.concatenate([X[0],X[1]]) # X[0]=RH, X[1]=FT
    y = np.concatenate([np.zeros(int(len(X)/2)), np.ones(int(len(X)/2))]) # vetor gabarito

    X = X[:,:,W_Start:W_End]

    # Spectral filtering
    XFF = fft(X)
    REAL = np.transpose(np.real(XFF)[:,:,bin0:binN], (2, 0, 1))
    IMAG = np.transpose(np.imag(XFF)[:,:,bin0:binN], (2, 0, 1))
    XF0 = list(itertools.chain.from_iterable(zip(IMAG, REAL)))
    XF = np.transpose(XF0, (1, 2, 0))

    # Temporal filtering
    # XF = lfilter(b, a, X)

    ## Cross-validation:
    process = Pipeline([('CSP', CSP(n_comp)), ('classifier', LinearDiscriminantAnalysis())])
    #kf = StratifiedShuffleSplit(10, test_size=0.1, random_state=0)
    kf = StratifiedKFold(10, False, 0)
    scores = cross_val_score(process, XF, y, cv=kf)

    acc = np.mean(scores)
    print(suj, round(acc*100,1))


# task 1

# 1. Поработайте с переменными,
# создайте несколько,
# выведите на экран,
# запросите у пользователя несколько чисел и строк
# и сохраните в переменные,
# выведите на экран.

name = input('Ваше имя? ')
print('Привет, %s!' % name)
birthday_year = int(input('В каком году вы родились?'))
birthday_date = int(input('Какого числа вы родились?'))
birthday_month = input('В каком месяце вы родились?')
current_year = int(input('Какой сейчас год? '))
current_age = current_year - birthday_year
print('Меня зовут ', name)
print('Моя дата рождения: ', birthday_date, birthday_month, birthday_year, )
print('Мой возраст', current_age, 'лет')


import json
import requests
import re
from bs4 import BeautifulSoup
import urllib.parse
import urllib.request
import os.path

IMG_BASE_URL = 'https://miro.medium.com/max/1200/'

def ensure_file_extension(filename):
    if not (filename.endswith(".jpg") or filename.endswith(".jpg") or filename.endswith(".jpg")):
        print("Turning " + filename + " into " + filename + ".jpg")
        return filename + ".jpg"
    return filename

def parse(data):
    """
        Parses the raw JSON data from Medium post pages to our own format.

        Parameters:
        data (dict): JSON data for a Medium post

        Returns:
        dict: {
            'title': 'title',
            'subtitle': 'subtitle',
            'paragraphs': [
                {
                    'type': 'P',  # 'H3', 'H4', 'IMG', 'ULI', 'OLI', 'BQ'
                    'text': 'text',  # Only used for text paragraphs
                    'markups': [
                        {
                            'type': 'STRONG',  # 'EM', 'A'
                            'start': 0,
                            'end': 0
                        }
                    ],  # Only used for text paragraphs
                    'img_filename': 'img_filename'  # Only used for the IMG paragraphs
                }
            ]
        }
    """
    paragraph_list = []
    markup_dict = {}
    for k, p in data.items():
        if p.get('__typename', '') == 'Paragraph':
            paragraph_list.append(p)
        if p.get('__typename', '') == 'Markup':
            markup_dict[k] = p

    paragraphs = []
    for p in paragraph_list:
        paragraph_type = p['type']
        if paragraph_type == 'IMG':
            img_filename = p['metadata']['id'].split('ImageMetadata:')[1]
            if os.path.isfile(img_filename):
                print("Image already existed: " + img_filename)
            else:
                print("Downloading... " + IMG_BASE_URL + img_filename)
                # Medium returns 403 for the default user-agent
                opener = urllib.request.build_opener()
                opener.addheaders = [('User-agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4)\
                        AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36')]
                urllib.request.install_opener(opener)
                local_filename = ensure_file_extension(img_filename)
                urllib.request.urlretrieve(IMG_BASE_URL + img_filename, local_filename)
            paragraphs.append({'type': paragraph_type, 'img_filename': local_filename})
        else:
            text = p['text']
            markups = []
            for markup_reference in p['markups']:
                markup = markup_dict[markup_reference['id']]
                start = markup['start']
                end = markup['end']
                markup_type = markup['type']
                markups.append({'type': markup_type, 'start': start, 'end': end})
            paragraphs.append({'type': paragraph_type, 'text': text, 'markups': markups})

    # Medium treats the first block of type 'H3' as Title.
    # If Title is followed by a block of type 'H4', the 'H4' is considered subtitle.
    title = None
    subtitle = None
    for idx in range(len(paragraphs)):
        p = paragraphs[idx]
        if p['type'] == 'H3':
            title = p['text']
            paragraphs.pop(idx)
            if idx < len(paragraphs) and paragraphs[idx]['type'] == 'H4':
                subtitle = paragraphs[idx]['text']
                paragraphs.pop(idx)
            break

    return {
        'title': title,
        'subtitle': subtitle,
        'paragraphs': paragraphs
    }

def crawl_medium(url):
    page = requests.get(url)
    if page.status_code != 200:
        raise Exception('Failed to open the URL')

    soup = BeautifulSoup(page.text, 'html.parser')

    # HTML format required:
    # <script>window.__APOLLO_STATE__ = {...json data...}</script>
    script_tags = soup.find_all('script', string=re.compile('__APOLLO_STATE__'))
    if len(script_tags) != 1:
        raise Exception('Can not find the content. The output HTML structure might have changed')

    json_data = script_tags[0].string.split('__APOLLO_STATE__ = ')[1].split('</script>')[0]
    return parse(json.loads(json_data, encoding='utf-8'))