import json
import logging
from typing import Any, Optional, Type, Union

import gluonts
from gluonts.core import fqname_for
from gluonts.core.serde import dump_code
from gluonts.dataset.common import Dataset
from gluonts.evaluation import Evaluator, backtest
from gluonts.model.estimator import Estimator
from gluonts.model.forecast import Quantile
from gluonts.model.forecast_generator import QuantileForecastGenerator
from gluonts.model.predictor import Predictor
from gluonts.mx.model.predictor import RepresentableBlockPredictor
from gluonts.mx.model.estimator import GluonEstimator
from gluonts.support.util import maybe_len
from gluonts.transform import FilterTransformation, TransformedDataset

from .env import TrainEnv

logger = logging.getLogger(__name__)

def log_metric(metric: str, value: Any) -> None:
    logger.info(f"gluonts[{metric}]: {dump_code(value)}")

def log_version(forecaster_type):
    name = fqname_for(forecaster_type)
    version = forecaster_type.__version__

    logger.info(f"Using gluonts v{gluonts.__version__}")
    logger.info(f"Using forecaster {name} v{version}")

def run_train_and_test(
    env: TrainEnv, forecaster_type: Type[Union[Estimator, Predictor]]
) -> None:
    log_version(forecaster_type)

    logger.info(
        "Using the following data channels: %s", ", ".join(env.datasets)
    )

    forecaster = forecaster_type.from_inputs(
        env.datasets["train"], **env.hyperparameters
    )
    logger.info(
        f"The forecaster can be reconstructed with the following expression: "
        f"{dump_code(forecaster)}"
    )

    if isinstance(forecaster, Predictor):
        predictor = forecaster
    else:
        predictor = run_train(
            forecaster=forecaster,
            train_dataset=env.datasets["train"],
            validation_dataset=env.datasets.get("validation"),
            hyperparameters=env.hyperparameters,
        )

    predictor.serialize(env.path.model)

    if "test" in env.datasets:
        run_test(env, predictor, env.datasets["test"], env.hyperparameters)

def run_train(
    forecaster: Estimator,
    train_dataset: Dataset,
    hyperparameters: dict,
    validation_dataset: Optional[Dataset],
) -> Predictor:
    num_workers = (
        int(hyperparameters["num_workers"])
        if "num_workers" in hyperparameters.keys()
        else None
    )
    shuffle_buffer_length = (
        int(hyperparameters["shuffle_buffer_length"])
        if "shuffle_buffer_length" in hyperparameters.keys()
        else None
    )
    num_prefetch = (
        int(hyperparameters["num_prefetch"])
        if "num_prefetch" in hyperparameters.keys()
        else None
    )
    if isinstance(forecaster, GluonEstimator):
        return forecaster.train(
            training_data=train_dataset,
            validation_data=validation_dataset,
            num_workers=num_workers,
            num_prefetch=num_prefetch,
            shuffle_buffer_length=shuffle_buffer_length,
        )
    else:
        return forecaster.train(
            training_data=train_dataset, validation_data=validation_dataset
        )

def run_test(
    env: TrainEnv,
    predictor: Predictor,
    test_dataset: Dataset,
    hyperparameters: dict,
) -> None:
    len_original = maybe_len(test_dataset)

    test_dataset = TransformedDataset(
        test_dataset,
        FilterTransformation(
            lambda x: x["target"].shape[-1] > predictor.prediction_length
        ),
    )

    len_filtered = len(test_dataset)

    if len_original is not None and len_original > len_filtered:
        logger.warning(
            f"Not all time-series in the test-channel have "
            f"enough data to be used for evaluation. Proceeding with "
            f"{len_filtered}/{len_original} "
            f"(~{int(len_filtered / len_original * 100)}%) items."
        )

    forecast_it, ts_it = backtest.make_evaluation_predictions(
        dataset=test_dataset, predictor=predictor, num_samples=100
    )

    test_quantiles = (
        [
            Quantile.parse(quantile).name
            for quantile in hyperparameters["test_quantiles"]
        ]
        if "test_quantiles" in hyperparameters.keys()
        else None
    )

    if isinstance(predictor, RepresentableBlockPredictor) and isinstance(
        predictor.forecast_generator, QuantileForecastGenerator
    ):
        predictor_quantiles = predictor.forecast_generator.quantiles
        if test_quantiles is None:
            test_quantiles = predictor_quantiles
        elif not set(test_quantiles).issubset(set(predictor_quantiles)):
            logger.warning(
                f"Some of the evaluation quantiles `{test_quantiles}` are "
                f"not in the computed quantile forecasts `{predictor_quantiles}`."
            )
            test_quantiles = predictor_quantiles

    if test_quantiles is not None:
        logger.info(f"Using quantiles `{test_quantiles}` for evaluation.")
        evaluator = Evaluator(quantiles=test_quantiles)
    else:
        evaluator = Evaluator()

    agg_metrics, item_metrics = evaluator(
        ts_iterator=ts_it,
        fcst_iterator=forecast_it,
        num_series=len(test_dataset),
    )

    # we only log aggregate metrics for now as item metrics may be very large
    for name, score in agg_metrics.items():
        logger.info(f"#test_score ({env.current_host}, {name}): {score}")

    # store metrics
    with open(env.path.model / "agg_metrics.json", "w") as agg_metric_file:
        json.dump(agg_metrics, agg_metric_file)
    with open(env.path.model / "item_metrics.csv", "w") as item_metrics_file:
        item_metrics.to_csv(item_metrics_file, index=False)


from crypt import methods
from flask import Flask, render_template, request
# from flask_sqlalchemy import SQLAlchemy
from flask_mysqldb import MySQL
app = Flask(__name__, template_folder='templates')
import mysql.connector

app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'mc'
app.config['MYSQL_PASSWORD'] = '1234567890'
app.config['MYSQL_DB'] = 'users'

mysql = MySQL(app)

@app.route("/", methods = ['GET', 'POST'])
def index():
    # if request.method == 'GET':
    #     cur = mysql.connection.cursor()
    #     cur.execute("SHOW DATABASE")
    #     for db in cur:
    #         print(db)
    #     mysql.connection.commit()
    #     return render_template('get.html')

    if request.method == "POST":
        details = request.form
        firstName = details['fname']
        lastName = details['lname']
        cur = mysql.connection.cursor()
        cur.execute("INSERT INTO MyUsers(firstName, lastName) VALUES (%s, %s);", (firstName, lastName))
        cur.close()
        mysql.connection.commit()
        return 'success'
        # return render_template('get.html')
    return render_template("post.html")
    # return ("
{}
").format(details['fname'])

# def hello():
#     return ("
hello
")

if __name__ == "__main__":
    app.run(host='0.0.0.0')


"""

Extracting root sum of squares from edge-wise timeseries.
=========================================================
This example uses a ``RSSETSMarker`` to compute root sum of squares
of the edge-wise timeseries using the Schaefer parcellation
(100 rois and 200 rois, 17 Yeo networks) for a 4D nifti BOLD file.

Authors: Leonard Sasse, Sami Hamdan, Nicolas Nieto, Synchon Mandal

License: BSD 3 clause

"""

import tempfile

import junifer.testing.registry  # noqa: F401
from junifer.api import collect, run
from junifer.storage import SQLiteFeatureStorage
from junifer.utils import configure_logging

###############################################################################
# Set the logging level to info to see extra information:
configure_logging(level="INFO")

##############################################################################
# Define the DataGrabber interface
datagrabber = {
    "kind": "SPMAuditoryTestingDataGrabber",
}

###############################################################################
# Define the markers interface
markers = [
    {
        "name": "Schaefer100x17_RSSETS",
        "kind": "RSSETSMarker",
        "parcellation": "Schaefer100x17",
    },
    {
        "name": "Schaefer200x17_RSSETS",
        "kind": "RSSETSMarker",
        "parcellation": "Schaefer200x17",
    },
]

###############################################################################
# Create a temporary directory for junifer feature extraction:
# At the end you can read the extracted data into a ``pandas.DataFrame``.
with tempfile.TemporaryDirectory() as tmpdir:
    # Define the storage interface
    storage = {
        "kind": "SQLiteFeatureStorage",
        "uri": f"{tmpdir}/test.sqlite",
    }
    # Run the defined junifer feature extraction pipeline
    run(
        workdir=tmpdir,
        datagrabber=datagrabber,
        markers=markers,
        storage=storage,
        elements=["sub001"],  # we calculate for one subject only
    )
    # Collect extracted features data
    collect(storage=storage)
    # Create storage object to read in extracted features
    db = SQLiteFeatureStorage(uri=storage["uri"])

    # List all the features
    print(db.list_features())
    # Read extracted features
    df_vbm = db.read_df(feature_name="BOLD_Schaefer100x17_RSSETS")

###############################################################################
# Now we take a look at the dataframe
df_vbm.head()


import numpy as np
def distance2(a1, a2):
    x1, y1, z1 = a1
    x2, y2, z2 = a2
    dx = x1 - x2
    dy = y1 - y2
    dz = z1 - z2
    return dx*dx + dy*dy + dz*dz

def angle(a1, a2, a3):
    x1, y1, z1 = a1
    x2, y2, z2 = a2
    x3, y3, z3 = a3
    v1 = np.array([x2 - x1, y2 - y1, z2 - z1])
    v2 = np.array([x2 - x3, y2 - y3, z2 - z3])
    l1 = np.sqrt(np.dot(v1, v1))
    l2 = np.sqrt(np.dot(v2, v2))
    cosa = np.dot(v1, v2) / (l1 * l2)
    return np.degrees(np.arccos(cosa))

def dihedral(a1, a2, a3, a4):
    p = np.array([a1,a2, a3, a4])
    v1 = p[1] - p[0]
    v2 = p[1] - p[2]
    v3 = p[3] - p[2]
    # Take the cross product between v1-v2 and v2-v3
    v1xv2 = _cross(v1, v2)
    v2xv3 = _cross(v2, v3)
    # Now find the angle between these cross-products
    l1 = np.sqrt(np.dot(v1xv2, v1xv2))
    l2 = np.sqrt(np.dot(v2xv3, v2xv3))
    cosa = np.dot(v1xv2, v2xv3) / (l1 * l2)

    #cosa = np.dot(v1xv2, v2xv3) / np.sqrt(np.dot(v1xv2, v1xv2) * np.dot(v2xv3, v2xv3))
    cosa = np.clip(cosa, -1, 1)

    return np.sign(np.dot(v3, v1xv2)) * np.degrees(np.arccos(cosa))

    # if np.dot(v3, v1xv2) <= 0.0:
    #     return np.degrees(np.arccos(cosa))
    # else:
    #     return -np.degrees(np.arccos(cosa))

def _cross(v1, v2):
    """ Computes the cross-product """
    # Can't use np.cross for pypy, since it's not yet implemented
    return np.array([v1[1]*v2[2] - v1[2]*v2[1],
                     v1[2]*v2[0] - v1[0]*v2[2],
                     v1[0]*v2[1] - v1[1]*v2[0]])


#!/usr/bin/env python
"""
A semi-complicated example of sizer use
"""

import wx

class MyFrame(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(
            self,
            parent,
            id,
            title,
            size=wx.DefaultSize,
            style=wx.DEFAULT_DIALOG_STYLE | wx.MAXIMIZE_BOX |
            # wx.THICK_FRAME |
            wx.RESIZE_BORDER)
        self.status = self.CreateStatusBar()

        btn1above = wx.Button(self, -1, "Create")
        btn2above = wx.Button(self, -1, "Ok")
        btn1right = wx.Button(self, -1, "Cancel")
        btnbrowser = wx.Button(self, -1, "Browse")
        btnmenu = wx.Button(self, -1, "Menu")
        lst = wx.ListCtrl(self, -1)
        self.chkMatchCase = wx.CheckBox(self, -1, "Match Case")
        self.chkRegularExpression = wx.CheckBox(self, -1, "RegularExpression")
        self.chkSubDirectories = wx.CheckBox(self, -1, "Subdirectories")

        helpsizer = wx.BoxSizer(wx.VERTICAL)
        helpsizer.Add((0, 5), 0, wx.ALL, 0)
        helpsizer.Add(btnbrowser, 0, wx.ALL, 0)
        helpsizer.Add((0, 30), 0, wx.ALL, 0)
        helpsizer.Add(btnmenu, 0, wx.ALL, 0)
        helpsizer.Add((0, 40), 0, wx.ALL, 0)
        helpsizer.Add(btn1above, 0, wx.ALL, 0)
        helpsizer.Add((0, 5), 0, wx.ALL, 0)
        helpsizer.Add(self.chkRegularExpression, 0, wx.ALL, 0)
        helpsizer.Add((0, 25), 0, wx.ALL, 0)
        helpsizer.Add(self.chkMatchCase, 0, wx.ALL, 0)
        helpsizer.Add((0, 5), 0, wx.ALL, 0)
        helpsizer.Add(self.chkSubDirectories, 0, wx.ALL, 0)
        helpsizer.Add((0, 30), 0, wx.ALL, 0)
        helpsizer.Add(btn2above, 0, wx.ALL, 0)
        helpsizer.Add((0, 10), 0, wx.ALL, 0)
        helpsizer.Add(btn1right, 0, wx.ALL, 0)

        stat1 = wx.StaticText(self, -1, "Directory:")
        stat2 = wx.StaticText(self, -1, "File Pattern:")
        stat3 = wx.StaticText(self, -1, "Search For:")
        text1 = wx.ComboBox(self, -1, "", wx.DefaultPosition, (300, -1))
        text2 = wx.ComboBox(self, -1, "")
        text3 = wx.ComboBox(self, -1, "")

        topsizer = wx.FlexGridSizer(3, 2, 5, 5)
        topsizer.AddGrowableCol(1)
        topsizer.Add(stat1, 0)
        topsizer.Add(text1, 1, wx.GROW)
        topsizer.Add(stat2, 0)
        topsizer.Add(text2, 1, wx.GROW)
        topsizer.Add(stat3, 0)
        topsizer.Add(text3, 1, wx.GROW)

        leftSizer = wx.BoxSizer(wx.VERTICAL)
        leftSizer.Add(topsizer, 0, wx.EXPAND | wx.ALL, 5)
        leftSizer.Add(lst, 1, wx.EXPAND | wx.ALL, 5)

        newSizer = wx.BoxSizer(wx.HORIZONTAL)
        newSizer.Add(leftSizer, 1, wx.EXPAND | wx.ALL, 10)
        newSizer.Add(helpsizer, 0, wx.ALIGN_TOP | wx.ALL, 10)
        lastSizer = wx.BoxSizer(wx.VERTICAL)
        self.SetSizer(newSizer)

        self.Fit()
        self.Show()

if __name__ == "__main__":
    app = wx.App(False)
    Panel = MyFrame(None, wx.ID_ANY, title="Find Files")
    app.MainLoop()


# -*- coding: utf-8 -*-
from django.contrib.sites.models import Site
from django.core.paginator import EmptyPage, InvalidPage, Paginator
from django.utils.translation import ugettext_lazy as _

from cms.models.pagemodel import Page
from cms.plugin_base import CMSPluginBase
from cms.plugin_pool import plugin_pool
from cmsplugin_photologue_pro import models as pluginmodels
from photologue import models
import re

class AlbumPlugin(CMSPluginBase):
    """Album CMS plugin."""

    name = _('Album')
    model = pluginmodels.Album
    render_template = 'cmsplugin_photologue_pro/album_plugin.html'

    def render(self, context, instance, placeholder):
        if not instance.album.is_public:
            context.update({'is_not_public': True})
            return context
        photo_instance = instance.album.photos.filter(is_public=True)
        photo_instance = photo_instance.order_by('id')
        per_page = instance.per_page or 100
        paginator = Paginator(photo_instance, per_page)
        try:
            page = max(int(context.get('request', 1).GET.get('page', 1)))
        except (ValueError, TypeError):
            page = 1
        try:
            photos = paginator.page(page)
        except (EmptyPage, InvalidPage):
            photos = paginator.page(paginator.num_pages)
        context.update({
            'gallery': instance.album,
            'photos': photos,
            'pages': paginator.page_range,
            'current_page': page,
        })
        return context

plugin_pool.register_plugin(AlbumPlugin)


import sys
sys.stdin = open('백준17404. RGB거리 2.txt', 'r')

N = int(input())
RGB = [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
answer = 1001 * 1000
for i in range(3):
    DP = [[1001] * 3 for _ in range(len(RGB))]
    DP[0][i] = RGB[0][i]
    for r in range(1, len(RGB)):
        DP[r][0] = RGB[r][0] + min(DP[r-1][1], DP[r-1][2])
        DP[r][1] = RGB[r][1] + min(DP[r-1][0], DP[r-1][2])
        DP[r][2] = RGB[r][2] + min(DP[r-1][0], DP[r-1][1])
    for j in range(3):
        if i != j:
            answer = min(DP[-1][j], answer)

print(answer)


import pandas as pd
import numpy as np
import graphdatascience
import itertools

def strfdelta(tdelta, fmt):
    d = {'days': str(tdelta.days).zfill(2)}
    h, rem = divmod(tdelta.seconds, 3600)
    m, s = divmod(rem, 60)
    d['hours'] = str(h).zfill(2)
    d['minutes'] = str(m).zfill(2)
    d['seconds'] = str(s).zfill(2)
    return fmt.format(**d)

def minutes_to_duration(x):
    return pd.to_timedelta(x, unit='m') \
        .apply(lambda x: strfdelta(x, '{days} days, {hours} hrs, {minutes} min'))

def minutes_to_duration_arr(x):
    tds = pd.to_timedelta(x, unit='m')
    return [strfdelta(td, '{days}days-{hours}hrs-{minutes}min') for td in tds]

def minutes_to_duration_arr_col(x):
    return x.apply(minutes_to_duration_arr)

def clear_all_graphs(gds):
    g_names = gds.graph.list().graphName.tolist()
    for g_name in g_names:
        g = gds.graph.get(g_name)
        gds.graph.drop(g)

def historic_path_counts(gds, sid, tid):
    df = gds.run_cypher('''
        //match incoming legIds (incoming leg numbers are > 0)
        MATCH(:EntryPoint {airportId: $sourceAirportId})-[r:RECEPTION]->() WHERE r.legNumber > 0
        WITH r.legId AS incomingLegId, r.shipmentId AS shipmentId

        //filter to only shipmentIds that go to target airport
        MATCH()-[:DELIVERY {shipmentId:shipmentId}]->(:Destination {airportId: $targetAirportId})
        WITH incomingLegId, shipmentId

        //get transfer point airport ids to optimize path search
        MATCH()-[:DELIVERY {legId:incomingLegId}]->(n:TransferPoint)
        WITH incomingLegId, shipmentId, n.airportId AS transferAirportId

        //incoming path
        MATCH p=(:EntryPoint {airportId: $sourceAirportId})-[*1..12 {legId: incomingLegId}]->(:TransferPoint {airportId: transferAirportId})
        WITH incomingLegId, shipmentId, transferAirportId, nodes(p) AS n, relationships(p) AS r
        WITH incomingLegId, shipmentId, transferAirportId,
           apoc.text.join([i IN range(1,size(n)) WHERE (i=1) OR (n[i].airportId <> n[i-1].airportId )| n[i].name], ' -> ') AS incomingAirports,
           reduce(s=0, i in r | s + i.effectiveMinutes ) AS incomingCost

        //outgoing path (leg numbers are -1)
        MATCH p=(:TransferPoint {airportId: transferAirportId})-[*1..12 {shipmentId: shipmentId, legNumber:-1}]->(:Destination {airportId: $targetAirportId})
        WITH incomingLegId, incomingAirports, incomingCost, nodes(p) AS n, relationships(p) AS r
        WITH incomingLegId, incomingAirports,
            incomingAirports + ' -> ' + apoc.text.join([i IN range(1,size(n)) WHERE n[i].airportId <> n[i-1].airportId | n[i].name], ' -> ')  AS airportPath,
            incomingCost + reduce(s=0, i in r | s + i.effectiveMinutes  ) AS totalCost

        //aggregate and return
        RETURN DISTINCT airportPath, size(collect(incomingLegId)) AS historicPathCount, avg(totalCost) AS historicAvgCostMin, stDev(totalCost) AS historicCostStdMin, collect(totalCost) AS historicCostsMin
        ORDER BY historicPathCount DESC
    ''', params={'sourceAirportId': sid, 'targetAirportId': tid})

    df['historicAvgCost'] = minutes_to_duration(df.historicAvgCostMin)
    df['historicCostStd'] = minutes_to_duration(df.historicCostStdMin)
    df['historicCosts'] = minutes_to_duration_arr_col(df.historicCostsMin)
    df.drop(columns=['historicAvgCostMin', 'historicCostStdMin', 'historicCostsMin'], inplace=True)
    return df

def get_yen_dfs(gds, g, source_node_ids, target_node_ids, k, max_avg_time=np.Inf):
    df_list = []
    for source_node_id in source_node_ids:
        for target_node_id in target_node_ids:
            tdf = gds.shortestPath.yens.stream(g, sourceNode=source_node_id, targetNode=target_node_id,
                                               k=k, relationshipWeightProperty='averageEffectiveMinutes')
            if max_avg_time < np.Inf:
                tdf = tdf[tdf.totalCost <= max_avg_time]
            df_list.append(tdf)
    return df_list

def get_solution_costs(row, path_dfs):
    solution = row.solutionIndex
    rels = {}
    path_costs = []
    for n in range(len(solution)):
        path_costs.append(path_dfs[n].totalCost[solution[n]])
        for r in path_dfs[n].path[solution[n]].relationships:
            rels[f'{r.start_node.id}-{r.end_node.id}'] = r
    return sum([r.get('cost') for r in rels.values()]), path_costs, list(rels.values())

def top_k_solutions(gds, g, source_node_ids, target_node_ids, top_k=10, yen_candidates=10, max_avg_time=np.Inf):
    # get paths
    path_dfs = get_yen_dfs(gds, g, source_node_ids, target_node_ids, yen_candidates, max_avg_time)
    # get solutions
    solution_df = pd.DataFrame(itertools.product(*[range(path_df.shape[0]) for path_df in path_dfs])) \
        .apply(tuple, axis=1).to_frame(name='solutionIndex')
    solution_df[['totalCost', 'pathCosts', 'relationships']] = solution_df.apply(get_solution_costs,
                                                                                args=([path_dfs]), axis=1,
                                                                                result_type='expand')
    # return top solutions
    return solution_df.sort_values('totalCost')[:top_k].reset_index(drop=True)

def top_k_solutions_from_names(gds, g, source_airport_names, target_airport_name, top_k=10, yen_candidates=10,
                               max_avg_time=np.Inf):
    source_node_ids = [gds.find_node_id(['EntryPoint'], {'name': i}) for i in source_airport_names]
    target_node_ids = [gds.find_node_id(['Destination'], {'name': target_airport_name})]
    return top_k_solutions(gds, g, source_node_ids, target_node_ids, top_k=top_k, yen_candidates=yen_candidates,
                           max_avg_time=max_avg_time)

def top_k_solutions_from_airport_ids(gds, g, source_airport_ids, target_airport_id, top_k=10, yen_candidates=10,
                                     max_avg_time=np.Inf):
    source_node_ids = [gds.find_node_id(['EntryPoint'], {'airportId': i}) for i in source_airport_ids]
    target_node_ids = [gds.find_node_id(['Destination'], {'airportId': target_airport_id})]
    return top_k_solutions(gds, g, source_node_ids, target_node_ids, top_k=top_k, yen_candidates=yen_candidates,
                           max_avg_time=max_avg_time)

def format_nodes_and_rels(relationships):
    records = []
    for r in relationships:
        records.append({'sourceNodeId': r.start_node.id, 'targetNodeId': r.end_node.id, 'cost': r.get('cost')})
    rel_df = pd.DataFrame.from_records(records)
    return pd.DataFrame(set(rel_df.sourceNodeId).union(rel_df.targetNodeId), columns=['nodeId']), rel_df

def remove_solution_from_db(gds, solution_tag, solution_tag_name='solutionTag', rel_type='SOLUTION_PATH'):
    gds.run_cypher(f'MATCH()-[r:{rel_type}]->() WHERE {solution_tag_name} = "{solution_tag}" DELETE r')

def remove_solution_type_from_db(gds, solution_tag_name='solutionTag', rel_type='SOLUTION_PATH'):
    gds.run_cypher(f'MATCH()-[r:{rel_type}]->() DELETE r')
    gds.run_cypher(f'DROP INDEX {rel_type.lower()}_{solution_tag_name}_index IF EXISTS')

def write_solution_to_db(gds, relationships, solution_tag, solution_tag_name='solutionTag', rel_type='SOLUTION_PATH'):
    _, rels_df = format_nodes_and_rels(relationships)

    gds.run_cypher(f'''
        CREATE INDEX {rel_type.lower()}_{solution_tag_name}_index
        IF NOT EXISTS FOR ()-[r:{rel_type}]-()
        ON (r.{solution_tag_name})
    ''')

    gds.run_cypher(f'''
        UNWIND $rels AS rels
        WITH toInteger(rels.sourceNodeId) AS sourceNodeId,
            toInteger(rels.targetNodeId) AS targetNodeId,
            rels.cost AS cost
        MATCH(n1) WHERE id(n1) = sourceNodeId
        MATCH(n2) WHERE id(n2) = targetNodeId
        MERGE(n1)-[r:{rel_type} {{{solution_tag_name}:"{solution_tag}"}}]->(n2)
        SET r.cost=cost
    ''', params={'rels': rels_df.to_dict('records')})


from PIL import Image

def decode_message(file_path):
    image = Image.open(file_path)
    image.show()
    width, height = image.size
    message = ''
    for x in range(30,width):
        y = 0
        while y < height and image.getpixel((x,y)) != 0:
            y += 1
        if y < height:
            message += chr(y)
            print('ok')
    return message

def main():
    print(decode_message('code.png'))

if __name__ == "__main__":
    main()


# Nikhil Gupta
# 19 May 2020
# Program to depict use of while loop & taking user input
#
message = input("Enter your name: ")
print(f"Hello, {message}!")
#
prompt = "We can personalise our greetings if you tell use your name."
prompt += "\nWhat is your name? "
name = input(prompt)
print(f"Hello, {name}")
#
# Using int to convert strings to integer type
#
age = input("How old are you? ")
age = int(age)
print(age+1)
#
dinner_members = input("How many members are there?" )
dinner_members = int(dinner_members)
if dinner_members%2:
	print("You all are welcome!")
else:
	print("Sorry, We have seat for odd number of members.")
#
# while loop
#
counter = 1
while counter <= 5:
	print(counter)
	counter += 1
#
# simple loop exit mechanism
#
prompt = "Tell me your age and i will add 5 to it."
prompt += "\nEnter 'quit' to exit the program.\n"
age = ""
while age != 'quit':
	age = input(prompt)
	if age != 'quit':
		age = int(age)
		age += 5
		print(f"5 years later your age will be {age}")
#
# Let me tell you how to use Flags
# say "active" flags
prompt = "We can personalise our greetings if you tell use your name."
prompt += "\nWhat is your name? "
active = True
while active:
 message = input(prompt)
 if message == 'quit':
 	active = False
 else:
 	print(message)
#
# Using break
#
prompt = "\nPlease enter the name of a city you have visited:"
prompt += "\n(Enter 'quit' when you are finished.) "
while True:
	city = input(prompt)
	if city == 'quit':
		break
	else:
		print(f"I'd love to go to {city.title()}")
#
current_number = 0
while current_number < 10:
	current_number += 1
	if current_number % 2 == 0:
		continue
	print(current_number)
#
# while loop in lists
#
unverfied_users = ['jayant','samridh','riya']
confirmed_users = []
while unverfied_users:
	current_user = unverfied_users.pop()
	prompt = f"Do you want to confir user {current_user}? "
	choice = input(prompt)
	if choice == 'yes':
		confirmed_users.append(current_user)
print(confirmed_users)
#
# Removing duplicate values from lists
#
veggies = ['tomato','potato','onion','cabbage','potato','carrort']
while 'potato' in veggies:
	veggies.remove('potato')
print(veggies)
#
# Filling dictionaries with looping
#
responses = {}
active = True
asking_name = "What is your name? "
car_choice = "Which car would you like to drive? "
repeat_choice = "Would you like to let another person reposnd? (yes/no) "
while active:
	name = input(asking_name)
	car = input(car_choice)
	responses[name]=car
	repeat = input(repeat_choice)
	if repeat == 'yes':
		active = True
	elif repeat =='no':
		active = False
print("Polling has ended!")
for key,value in responses.items():
	print(f"{key} would like to drive {value} car.")
print(responses)
#


import error
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit, fmin
from uncertainties import ufloat
from uncertainties import unumpy

vapor_pressure = [23.88, 23.75, 25.80, 27.90, 28.70, 29.90, 33.40, 33.70]
inv_temp = np.array([0.003354, 0.003332, 0.003299, 0.003277, 0.003245, 0.003224, 0.003193, 0.003173])
temp = 1/inv_temp
temperature = unumpy.uarray([temp], [0.6, 0.6, 0.6, 0.6,0.6, 0.6, 0.6, 0.6,])
inv_temperature = 1/temperature
print(inv_temperature)

def exponential(x, a, b):
    return a*np.exp(-b*x)

(a, b), cov = curve_fit(exponential, inv_temp, vapor_pressure)

print((a, b))
print(cov)

x_data = np.linspace(0.0031, 0.0034, 100)
y_data = exponential(x_data, a, b)

# Plotting
plt.grid(True)

fig, axes = plt.subplots()
axes.set_ylabel('Vapor Pressure [Bar]', fontsize=24)
axes.set_xlabel('1/Temperature [1/K]', fontsize=24)
axes.set_title('Arrhenius Graph', fontsize=24)
plt.yscale("log")

axes.errorbar(inv_temp, vapor_pressure,fmt=" ", xerr=0.0000065, yerr=error.err_vapour_pressure_bar, capsize=2, capthick=1)

axes.plot(x_data, y_data, color="green")

axes.legend(["Best fit curve", "Data points"], fontsize=24)

plt.show()


def palindrome(word):
    if len(word) <= 1: #1.мадам из 5 букв значит не подходит #2.тепенрь тут ада и т.д. №будет только д
        return 'It is a palindrome!'
    if word[0] != word[-1]: #первая и последняя совпадают значит идем дальше #когда тут д то он скажет что это полиндром
        return 'это не полидром'
    return palindrome(word[1:-1]) #убираем 1 и последнюю букву и возвращает это слово наверх
print(palindrome('мадам'))

def square(x, n):
    if n == 0:
        return 1
    if n < 0:
        return 1/square(x,-n)
    if n%2 == 1:
        return square(x, n-1)*x
    return square(x, n//2)*square(x, n//2)

print(square(7, 5))

def print_level(list_numb, level=1):
    print(f'\nLevel is:{level}, elements:')
    for i in list_numb:
        if type(i) != list:
            print(i, end='')
    for i in list_numb:
        if type(i) == list:
            print_level(i, level+1)
print_level([1,2,3,[4,5,32,[4,4]]])


from qtpy import QtWidgets, QtCore

from .widgets import (
    GridLabelWidget,
    SpacerWidget
)
from .base import BaseWidget

class ListStrictWidget(BaseWidget):
    def create_ui(self):
        self.setObjectName("ListStrictWidget")

        self._child_style_state = ""
        self.input_fields = []

        content_layout = QtWidgets.QGridLayout(self)
        content_layout.setContentsMargins(0, 0, 0, 0)
        content_layout.setSpacing(3)

        self.content_layout = content_layout
        self.content_widget = self

        any_children_has_label = False
        for child_obj in self.entity.children:
            if child_obj.label:
                any_children_has_label = True
                break

        self._any_children_has_label = any_children_has_label
        # Change column stretch factor for vertical alignment
        if not self.entity.is_horizontal:
            col_index = 2 if any_children_has_label else 1
            content_layout.setColumnStretch(col_index, 1)

        for child_obj in self.entity.children:
            self.input_fields.append(
                self.create_ui_for_entity(
                    self.category_widget, child_obj, self
                )
            )

        if self.entity.is_horizontal:
            col = self.content_layout.columnCount()
            spacer = SpacerWidget(self)
            self.content_layout.addWidget(spacer, 0, col, 2, 1)
            self.content_layout.setColumnStretch(col, 1)

        self.entity_widget.add_widget_to_layout(self, self.entity.label)

    @property
    def is_invalid(self):
        return self._is_invalid or self._child_invalid

    @property
    def _child_invalid(self):
        for input_field in self.input_fields:
            if input_field.is_invalid:
                return True
        return False

    def get_invalid(self):
        invalid = []
        for input_field in self.input_fields:
            invalid.extend(input_field.get_invalid())
        return invalid

    def make_sure_is_visible(self, path, scroll_to):
        if not path:
            return False

        entity_path = self.entity.path
        if entity_path == path:
            self.set_focus(scroll_to)
            return True

        if path.startswith(entity_path):
            for input_field in self.input_fields:
                if input_field.make_sure_is_visible(path, scroll_to):
                    return True
        return False

    def add_widget_to_layout(self, widget, label=None):
        # Horizontally added children
        if self.entity.is_horizontal:
            self._add_child_horizontally(widget, label)
        else:
            self._add_child_vertically(widget, label)

        self.updateGeometry()

    def _add_child_horizontally(self, widget, label):
        col = self.content_layout.columnCount()
        # Expand to whole grid if all children are without label
        if not self._any_children_has_label:
            self.content_layout.addWidget(widget, 0, col, 1, 2)
        else:
            if label:
                label_widget = GridLabelWidget(label, widget)
                label_widget.input_field = widget
                widget.label_widget = label_widget
                self.content_layout.addWidget(label_widget, 0, col, 1, 1)
                col += 1
            self.content_layout.addWidget(widget, 0, col, 1, 1)

    def _add_child_vertically(self, widget, label):
        row = self.content_layout.rowCount()
        if not self._any_children_has_label:
            self.content_layout.addWidget(widget, row, 0, 1, 1)

            spacer_widget = SpacerWidget(self)
            self.content_layout.addWidget(spacer_widget, row, 1, 1, 1)

        else:
            if label:
                label_widget = GridLabelWidget(label, widget)
                label_widget.input_field = widget
                widget.label_widget = label_widget
                self.content_layout.addWidget(
                    label_widget, row, 0, 1, 1,
                    alignment=QtCore.Qt.AlignRight | QtCore.Qt.AlignTop
                )
            self.content_layout.addWidget(widget, row, 1, 1, 1)

            spacer_widget = SpacerWidget(self)
            self.content_layout.addWidget(spacer_widget, row, 2, 1, 1)

    def hierarchical_style_update(self):
        self.update_style()
        for input_field in self.input_fields:
            input_field.hierarchical_style_update()

    def set_entity_value(self):
        for input_field in self.input_fields:
            input_field.set_entity_value()

    def _on_entity_change(self):
        pass

    def update_style(self):
        if not self.label_widget:
            return

        style_state = self.get_style_state(
            self.is_invalid,
            self.entity.has_unsaved_changes,
            self.entity.has_project_override,
            self.entity.has_studio_override
        )

        if self._style_state == style_state:
            return

        self.label_widget.setProperty("state", style_state)
        self.label_widget.style().polish(self.label_widget)

        self._style_state = style_state


# Implementation Of Circular Linked List
class Node:
  def __init__(self, data):
    self.data = data
    self.next = None

class CircularLinkedList:
  def __init__(self):
    self.head = None
    # self.tail = None
    # store the count of nodes
    self.n = 0

  # Insertion Operations
  # a. Insert at beginning
  def insert_at_first(self, value):
    new_node = Node(value)
    current = self.head
    # now the head is new_node
    new_node.next = self.head

    # if the inserted node is the only node in list (list is empty)
    # Linked list is empty
    if self.head is None:
      new_node.next = new_node
      # return
    # at least one node in list
    else:
      while current.next != self.head:
        current = current.next
      current.next = new_node
    # new_node is head now
    self.head = new_node

  # b. Insert at last
  def insert_at_end(self, value):
    new_node = Node(value)
    # list is empty (inserted node is the only node in list)
    if self.head is None:
      self.head = Node(value)
      self.head.next = self.head
    # if there are more than one node in list
    else:
      current = self.head
      while current.next != self.head:
        current = current.next

      current.next = new_node
      new_node.next = self.head

  # c. Insert at given position
  def insert_at_position(self, pos, value):
    node_count = self.count_nodes()
    if pos < 1 or pos > node_count+1:
      return "Invalid position."
    elif pos == 1:
      return self.insert_at_first(value)
      self.n += 1
    elif pos > 1 or pos < (count_node):
      new_node = Node(value)
      current = self.head

      # not solved
      for i in range(1, pos-1):
        current = current.next

      new_node.next = current.next
      current.next = new_node

  # Deletion Operations
  # a. Delete from beginning
  def delete_first(self):
    if self.head is None:
      return "Circular Linked List is empty."
    else:
      # if there is single node in list
      if self.head == self.tail:
        self.head = None
      # if there are more than one node in list
      else:
        temp = self.head
        self.head = temp.next
        # delete the first node
        temp = None
        self.tail.next = self.head

  # b. Delete from end
  def delete_last(self):
    pass

  # c. Delete from given position
  def delete_from_position(self, pos):
    pass

  # Count the number of nodes
  def count_nodes(self):
    return self.n

  # display linked list
  def display(self):

    if self.head is None:
      return "Circular Linked List is empty."

    # current = self.head
    # print("Circular Linked List: ", end=" ")
    # print(current.data, '->', end=' ')
    # while current.next != self.head:
    #   current = current.next
    #   print(current.data, '->', end=' ')

    current = self.head
    res = ""
    while current:
      res = res + str(current.data) + ' -> '
      current = current.next
      if current == self.head:
        break

    return res[:-3]

if __name__ == '__main__':
  cl = CircularLinkedList()
  # cl.insert_at_end(10)
  cl.insert_at_end(20)
  cl.insert_at_end(200)
  cl.insert_at_first(100)
  # cl.insert_at_first(90)
  # cl.insert_at_first(89)
  # cl.insert_at_first(10)
  print(cl.display())
  # print("Circular Linked List: ", cl.display())
  # cl.delete_first()
  # print("Circular Linked List: ", cl.__str__())


import random
import string
from Summoner import Summoner, create_default_summoner, Class
from Items.Armor import Weapon
from utils import list_from_file

def get_name_list(type):
    file = "fantasy_names.txt"
    if type == "summoner":
        file = "fantasy_names.txt"
    elif type == "weapon":
        file = "weapon_names.txt"
    return list_from_file("fantasy_names/" + file)

# type = summoner, weapon, armor
def generate_name(type):
    name = ""
    nameList = get_name_list(type)

    random.seed()
    rand = random.randrange(len(nameList))
    enemyName = nameList[rand]
    if type == "summoner":
        rand = random.randrange(len(nameList))
        enemyName += " " + nameList[rand]
    return enemyName

# If type is 0, returns a random summoner as enemy
def generate_random_enemy(p, type):
    name = ""
    if type == 0:  # summoner
        name = generate_name("summoner")
        summ = create_default_summoner(name)

        # random class
        random.seed()
        rand = random.randrange(0, 4)
        summ.cl = rand

        # setting level conditions
        if (
            p.level <= 4
        ):  # if player is level 4 or below, enemy is not above player level
            lwrBound = p.level - 3
            uprBound = p.level
        else:  # otherwise level is random in range:
            lwrBound = p.level - 5  # at least 5 levels lower than player
            uprBound = p.level + 2  # at most 2 levels above player

        if lwrBound < 1:  # ensuring no negative or 0 level
            lwrBound = 1

        # create level of enemy
        rand = random.randrange(lwrBound, uprBound + 1)
        summ.level = rand

        # level up stats acccording to class
        for i in range(summ.level):
            summ.level_up_stats()
        summ.full_heal()
        summ.reset_combat_stats()
    return summ

# generate a weapon based on class of summoner p
def generate_random_weapon(p):
    # generate random weapon name
    name = generate_name("weapon")
    # generate some flavor text // TODO //
    flavor = "A cool weapon!"
    allStatStrings = [
        "health",
        "mana",
        "healthr",
        "manar",
        "ad",
        "ap",
        "armor",
        "mr",
        "prio",
        "crit",
    ]
    allStatNums = []
    # for i in range(len(allStatStrings)):  # populate stat increases with all 0 for now
    #    allStatNums.append(0)

    # depending on class, increase likelihood of certain stats increasing
    biasStats = ["ad"]
    if p.clStr == Class.MAGE:
        biasStats = ["ap", "mana", "manar"]
    elif p.clStr == Class.TANK:
        biasStats = ["health", "healthr", "armor", "mr"]
    elif p.clStr == Class.FIGHTER:
        biasStats = ["health", "ad", "armor", "mr"]
    elif p.clStr == Class.MARKSMAN:
        biasStats = ["ad", "prio", "crit"]

    allStatsZero = True
    while allStatsZero:
        for stat in allStatStrings:
            coef = 0  # coefficient for current stat being iterated on
            if stat in biasStats:  # if current stat gets 50% chance of increase
                rand = random.randint(0, 1)
            else:  # if current stat gets 10% chance of increase
                rand = random.randint(0, 9)
            if rand == 0:  # if chance of increase succeeds
                # change this to change stat increases, currently 0-5x player level
                coef = random.randint(0, 50)
                coef = coef / 10  # coef is between 0 and 5 in increments of 0.1
            # add stat change to stat array to be passed to weapon
            allStatNums.append(round(p.level * coef))
            # constructor
        for (
            stat
        ) in (
            allStatNums
        ):  # check if no stat buffs. if that's the case, run the while loop once again
            if stat != 0:
                allStatsZero = False
        if allStatsZero:
            allStatNums = []

    # determine stat scaling
    if (
        "healthr" in biasStats
    ):  # ensuring damage only scales off of hp, mana, ad, ap, armor, mr
        biasStats.remove("healthr")
    if "manar" in biasStats:
        biasStats.remove("manar")
    if "prio" in biasStats:
        biasStats.remove("prio")
    if "crit" in biasStats:
        biasStats.remove("crit")
    allStatStrings.remove("healthr")
    allStatStrings.remove("manar")
    allStatStrings.remove("prio")
    allStatStrings.remove("crit")
    rand = random.randint(1, 10)
    stat_scale = "ad"  # default ad just in case random breaks
    if p.level == int(1):  # make it guaranteed to scale off bias stat for level 1
        rand = 8
    # 60% chance it'll scale off of a random stat (could be bias stat still)
    if rand <= 6:
        # subtract 3 to prevent scaling from prio and crit
        x = random.randint(0, len(allStatStrings) - 1)
        stat_scale = allStatStrings[x]
    else:  # 40% chance it'll scale off of bias stat
        x = random.randint(0, len(biasStats) - 1)
        stat_scale = biasStats[x]
    # determine pct stat scaling
    x = random.randint(0, 100)
    x = x / 10  # x is between 0 and 5 by increments of 0.1
    y = pow(1.6, x) + 10  # y is between 0 and ~110
    pct_scale = y / 100

    # determine physical or magic damage
    rand = random.randint(0, 1)
    dmg_type = ""
    if rand == 0:
        dmg_type = "physical"
    else:
        dmg_type = "magic"

    # determine effects
    effects = ["None"]

    # create weapon
    weapon = Weapon(
        name,
        allStatNums[0],
        allStatNums[1],
        allStatNums[2],
        allStatNums[3],
        allStatNums[4],
        allStatNums[5],
        allStatNums[6],
        allStatNums[7],
        allStatNums[8],
        allStatNums[9],
        flavor,
        stat_scale,
        pct_scale,
        dmg_type,
        effects,
    )

    return weapon

def battle(p):
    # random chance of encountering a monster or enemy summoner
    random.seed()
    rand = random.randint(1, 100)  # generate number 0-99

    # if rand < 40:  # 40% chance to encounter enemy summoner
    #    p = generate_random_enemy(p, 0)
    return p


def rem(dividend,divid):
    a = dividend
    b = divid
    c = a%b
    return c

def get_code(number):
    if number == 1:
        code = [1,1,1]
    elif number == 2:
        code = [2,1,1]
    elif number == 3:
        code = [1,2,1]
    elif number == 4:
        code = [2,2,1]
    elif number == 5:
        code = [1,1,2]
    elif number == 6:
        code = [2,1,2]
    elif number == 7:
        code = [1,2,2]
    elif number == 0:
        code = [2,2,2]
    return code

def change_calc(number):
    if number == 0:
        code = 6
    else:
        code = number
    return code

def symbol_show(number):
    sun = """——————————"""
    moon = """————  ————"""
    if number == 1:
        print(sun)
    else:
        print(moon)

if __name__ == '__main__':
    #input No.
    symbol_bottom_in = int(input("输入第一个三位数："))
    symbol_head_in = int(input("输入第二个三位数："))
    change_in = int(input("输入第三个三位数："))

    #calc No.
    change_out = change_calc(rem(change_in,6))
    symbol_head_out = get_code(rem(symbol_head_in,8))
    symbol_bottom_out = get_code(rem(symbol_bottom_in,8))
    print(change_out, symbol_head_out, symbol_bottom_out)

    #print symbol
    print("==================")
    print("爻:"+str(change_out))
    print("==================")
    for number in symbol_head_out:
        symbol_show(number)
    for number in symbol_bottom_out:
        symbol_show(number)


from bottle import get, route, post, run, request # or route
from aperter import er_, json_

@route('/')
def root():
    return '''

Wrong Route

    '''
@route('/api')
def api():
    url = request.query.url
    try:
        em = -1
        em = er_(url=url)
    except:
        print("url: "+url+" ,emotion: "+str(em))
        json = json_(status="fail")
        return json
    else:
        print("url: "+url+" ,emotion: "+str(em))
        json = json_(status="success",em=em,url=url)
        return json

@post('/er')
def er():
    try:
        em=-1
        url = request.forms.get('url')
        em = er_(url=url)
    except:
        print("url: "+url+" ,emotion: "+str(em))
        json = json_(status="fail")
        return json
    else:
        print("url: "+url+" ,emotion: "+str(em))
        json = json_(status="success",em=em,url=url)
        return json

if __name__ == '__main__':
    print("Do not close this window")
    run(host='localhost', port=8880)


import os
import shutil

__all__ = ['find_ext_files',
           'overwrite_dir'
		   ]

def find_ext_files(dir_name, exts, ignore_dirs=[]):
	""" Find all files with given extension in a given directory
	Parameters
	-------
	dir_name: str
		given directory to locate files
	exts: list
		file extensions
	Returns
	-------
	file_list: list
		files with specified extesion in given directory
	"""
	if not os.path.isdir(dir_name):
		raise AssertionError("{} is not a valid directory".format(dir_name))
	file_list = []
	for root, _, files in os.walk(dir_name, followlinks=True):
		if root.split(os.sep)[-1] in ignore_dirs: continue
		for cur_file in files:
			for ext in exts:
				if cur_file.endswith(ext):
					file_list.append(os.path.join(root, cur_file))
					break
	return file_list

def overwrite_dir(dir_name):
	""" Overwrite directory if exist, create new directory
	"""
	if os.path.exists(dir_name):
		shutil.rmtree(dir_name)
	os.makedirs(dir_name)


import json

def infntIN_ir_seed0(args):
    args.load_step = 2215000
    args.load_port = 26001
    args.load_dbname = 'combinet-test'
    args.load_colname = 'combinet'
    args.load_expId = 'inst_infant_18_fx'

    args.identifier = 'infntIN-ir-seed0'
    cfg_kwargs = {
            'network_func': 'get_resnet_18',
            'network_func_kwargs': '{"num_cat": 128}',
            }
    args.cfg_kwargs = json.dumps(cfg_kwargs)
    return args


import functools
import os.path
import subprocess
import sys

from pyperf._utils import shell_quote, popen_communicate
from pyperf._worker import WorkerTask

def bench_command(command, task, loops):
    path = os.path.dirname(__file__)
    script = os.path.join(path, '_process_time.py')
    run_script = [sys.executable, script]

    args = run_script + [str(loops)] + command
    proc = subprocess.Popen(args,
                            stdout=subprocess.PIPE,
                            stderr=subprocess.STDOUT,
                            universal_newlines=True)
    output = popen_communicate(proc)[0]
    if proc.returncode:
        raise Exception("Command failed with exit code %s"
                        % proc.returncode)

    rss = None
    try:
        lines = output.splitlines()
        timing = float(lines[0])
        if len(lines) >= 2:
            rss = int(lines[1])
    except ValueError:
        raise ValueError("failed to parse script output: %r" % output)

    if rss:
        # store the maximum
        max_rss = task.metadata.get('command_max_rss', 0)
        task.metadata['command_max_rss'] = max(max_rss, rss)
    return timing

class BenchCommandTask(WorkerTask):
    def __init__(self, runner, name, command):
        command_str = ' '.join(map(shell_quote, command))
        metadata = {'command': command_str}
        task_func = functools.partial(bench_command, command)
        WorkerTask.__init__(self, runner, name, task_func, metadata)

    def compute(self):
        WorkerTask.compute(self)
        if self.args.track_memory:
            value = self.metadata.pop('command_max_rss', None)
            if not value:
                raise RuntimeError("failed to get the process RSS")

            self._set_memory_value(value)


import os
from datetime import datetime

from lxml import etree

from lib.model.track import GpsTrack, GpsTrackpoint

_XML_NAMESPACE = {
    "tcd": "http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2",
    "xsi": "http://www.w3.org/2001/XMLSchema-instance"
}

def _get_trackpoints_from_track(
        trackpoints: list[etree._Element]
    ) -> tuple[list[GpsTrackpoint], set[datetime]]:
    """
    Extract the points and time of the read track
    """
    gps_trackpoints: list[GpsTrackpoint] = []
    track_times: set[datetime] = set()
    for trackpoint in trackpoints:

        try:
            trackpoint_time = trackpoint.find(
                './/tcd:Time',
                namespaces=_XML_NAMESPACE
            ).text
            trackpoint_lat_deg = trackpoint.find(
                './/tcd:Position/tcd:LatitudeDegrees',
                namespaces=_XML_NAMESPACE
            ).text
            trackpoint_lon_deg = trackpoint.find(
                './/tcd:Position/tcd:LongitudeDegrees',
                namespaces=_XML_NAMESPACE
            ).text
        except AttributeError:
            continue

        # Time comes with timezone included
        trackpoint_time = datetime.strptime(trackpoint_time, '%Y-%m-%dT%H:%M:%S%z')
        gps_trackpoints.append(
            GpsTrackpoint(
                longitude=trackpoint_lon_deg,
                latitude=trackpoint_lat_deg,
                time=trackpoint_time,
            )
        )
        track_times.add(trackpoint_time)
    return gps_trackpoints, track_times

def read_tcx_track(folder: str, file: bytes) -> GpsTrack|None:
    """
    Read a single GPS track
    """
    filename = os.fsdecode(file)
    if not filename.endswith('.tcx'):
        return None

    file_path = os.path.join(folder, filename)

    # Get all trackpoints
    root: etree._ElementTree = etree.parse(file_path, parser=None)
    trackpoints: list[etree._Element] = root.xpath(
        ".//tcd:Track/tcd:Trackpoint",
        namespaces=_XML_NAMESPACE)

    # Extract lat, lon and time from each trackpoint
    try:
        gps_trackpoints, track_times = _get_trackpoints_from_track(trackpoints)
    except AttributeError:
        return None

    # If no valid point is found ignore the file
    if len(gps_trackpoints) == 0:
        return None

    return GpsTrack(
        min_time=min(track_times),
        max_time=max(track_times),
        trackpoints=gps_trackpoints,
        filename=filename
    )


import re
import datetime
import threading
from queue import Queue
from pathlib import Path
from collections import defaultdict
from user_agents import parse

# 分析日志
def log_analysis(log_line: str):
    func_dict = {'datetime': lambda time: datetime.datetime.strptime(time, '%d/%b/%Y:%H:%M:%S %z'),  # 转换时间的对应函数
                 'request': lambda mode: dict(zip(('method', 'url', 'protocol'), mode.split())),  # 请求分解的对应函数
                 'useragent': lambda x: parse(x)
                 }

    # 用正则分组名有：remote, datetime, request, status, useragent
    # 日志格式：10.211.55.2 - - [14/Jul/2020:18:54:58 +0800] "GET / HTTP/1.1" 403 4897 "-" "curl/7.64.1"
    pattern = '''(?P<remote>^[\d.]{7,}) - - \[(?P<datetime>[^\[\]]+)\] "(?P<request>[^"]+)" (?P<status>\d{3,}) \d{3,} "[^"]+" "(?P<useragent>[^"]+)"$'''
    matcher = re.match(pattern, log_line)

    if matcher:  # 判断是否匹配上了，如未匹配，函数默认会返回 None
        return {k: func_dict.get(k, lambda x: x)(v) for k, v in
                matcher.groupdict().items()}  # matcher.groupdict() 会返回一个带分组名的字典，例如 {'remote': xxxxxxxx}

# 加载日志文件
def open_file(file):
    with open(file, encoding='utf8') as f:
        for line in f:
            d = log_analysis(line)
            if d:
                yield d
            else:
                # TODO 不合格的数据有多少
                # print('########################')
                continue

# 批量读取文件，并判断是否是文件
def load(*path):
    for file in path:
        f = Path(file)
        if not f.exists():  # 判断文件是否存在
            continue
        elif f.is_dir():  # 如果是目录，就迭代目录内的所有文件
            for x in f.iterdir():
                if x.is_file():  # 如果是文件就读取，否则就忽略了
                    yield from open_file(x)
        elif f.is_file():  # 如果是文件，就直接读取
            yield from open_file(f)

# 时间滑动窗口
def windows(src: Queue, handler, interval: int, width: int):
    """
    :param src: 数据源
    :param handler: 负责处理数据的函数
    :param interval: 时间间隔
    :param width: 时间宽度
    """
    start = datetime.datetime.strptime('1970/01/01 00:00:00 +0800', '%Y/%m/%d %H:%M:%S %z')
    current = datetime.datetime.strptime('1970/01/01 00:00:01 +0800', '%Y/%m/%d %H:%M:%S %z')
    delta = datetime.timedelta(seconds=(width - interval))  # 重复的部分 = 宽度 - 间隔

    buffer = []

    while True:
        data_dict = src.get()
        if data_dict:
            buffer.append(data_dict)
            current = data_dict['datetime']

        if (current - start).total_seconds() >= interval:
            ret = handler(buffer)

            buffer = [x for x in buffer if x['datetime'] > current - delta]
            start = current

# 消息分发器
def dispatcher(src):
    # 队列列表
    queues = []
    threads = []

    def reg(handler, interval, width):
        q = Queue()
        queues.append(q)

        t = threading.Thread(target=windows, args=(q, handler, interval, width))
        threads.append(t)

    def run():
        for t in threads:
            t.start()

        for x in src:
            for q in queues:
                q.put(x)

    return reg, run

# 状态码分析的 handler
def handler_status(buffer):
    d = {}
    start = buffer[0]['datetime']  # 因为日志是按时间顺序记录的，那么在同一个 buffer 内，第一条日志的时间就是这个 buffer 的起始时间
    end = buffer[-1]['datetime']  # 最后一条日志的时间就是这个 buffer 的结束时间

    for i in buffer:
        status = i['status']
        if status not in d.keys():
            d[status] = 0
        d[status] += 1

    # 总数
    total = sum(d.values())

    for k, v in d.items():
        p = (v / total) * 100  # 计算平均值
        d[k] = [d[k], p]  # 把平均值塞回d中

    # 输出到控制台
    print(f'在 {start.date()}【 {start.time()} - {end.time()} 】内')
    for status in d.items():
        print(' ' * 40, f'{status[0]} 出现 {status[1][0]} 次，占比 {int(status[1][1])}%')
    print('\n')

    return start, end, d

# 统计用户的浏览器
ua_dict = defaultdict(int)  # 这是记录 agent 出现次数的字典，对于 agent 的版本，看一小段时间是没有意义的，应该放在函数外，可记录下完整的次数

def user_agent(buffer):
    for i in buffer:
        ua = i['useragent']
        ua_dict[(ua.browser.family, ua.browser.version_string)] += 1

    # 输出到控制台
    print(f'截止：{buffer[-1]["datetime"]}')
    sort_ua = sorted(ua_dict.items(), key=lambda x: x[1], reverse=True)  # 按出现次数排序
    for agent in sort_ua:
        print(' ' * 40, f'客户端：{agent[0][0]:<20} 版本：{agent[0][1]:<15} 出现次数：{agent[1]}')
    print('\n')

    return ua_dict

if __name__ == '__main__':
    # 从命令行参数获取路径的方式
    # import sys
    # path = sys.argv[1]

    # 直接指定路径，测试时使用
    path = './test_file/access_1.log'

    reg, run = dispatcher(load(path))

    # 注册 handler，时间(s)：时间窗口的间隔、宽度
    # 日志会以一个时间窗口为单位，分发到注册的 handler
    # 日志已经被格式化完毕，handler 只要按自己的需求提取并选择如何处理即可
    reg(handler_status, 5, 5)  # 负责统计状态码
    reg(user_agent, 5, 5)  # 负责统计 agent 信息

    # 启动
    run()


from flask import Flask, render_template
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
SQLALCHEMY_TRACK_MODIFICATIONS = False
app.config.from_object('config')
db = SQLAlchemy(app)

# Sample HTTP error handling
@app.errorhandler(404)
def not_found(error):
    return render_template('404.html'), 404

from app.mod_auth.controllers import mod_auth as auth_module

# Register blueprint(s)
app.register_blueprint(auth_module)
# app.register_blueprint(xyz_module)
# ..

# Build the database:
db.create_all()


import logging
import warnings
from typing import Dict, List, Union, Any

import torch
from datasets import Dataset, DatasetDict
from datasets.arrow_dataset import Batch

from oslo.transformers.tasks.data_base import BaseProcessor

try:
    from transformers import (
        AutoTokenizer,
        PreTrainedTokenizerBase,
        RobertaTokenizer,
    )
    from transformers.file_utils import PaddingStrategy
except ImportError:
    print("You have to install `transformers` to use `oslo.transformers` modules")

logger = logging.getLogger(__name__)
logging.captureWarnings(True)

class ProcessorForTokenClassification(BaseProcessor):
    def __init__(
        self,
        tokenizer: PreTrainedTokenizerBase,
        max_seq_length: int,
        dataset: Union[Dataset, DatasetDict] = None,
    ) -> None:
        super().__init__(tokenizer, max_seq_length)
        if dataset is None:
            raise ValueError(
                "dataset argument must be set. (dataset: Union[Dataset, DatasetDict])"
            )

        if self._tokenizer.pad_token is None:
            warnings.warn(
                "If pad token doesn't exist in tokenizer, it can be a problem when applying padding."
            )

        self.label_names = self.get_label_names(dataset)
        self.label_map = self.get_label_map(self.label_names)

    def __call__(self, examples: Batch) -> Dict[str, List[int]]:
        column_names = [k for k, v in examples.items()]
        assert (
            "tokens" in column_names
        ), "The name of dataset column that you want to tokenize must be 'tokens' (not 'text')"

        dict_of_training_examples: Dict[str, List[int]] = self._tokenizer(
            examples["tokens"],
            truncation=True,
            max_length=self._max_seq_length,
            is_split_into_words=True,
            verbose=False,
        )
        all_labels = examples["labels"]
        new_labels = []
        self.make_B_to_I_label(self.label_names)

        for i, labels in enumerate(all_labels):
            word_ids = dict_of_training_examples.word_ids(batch_index=i)
            new_labels.append(self.align_labels_with_tokens(labels, word_ids))

        dict_of_training_examples["labels"] = new_labels
        return dict_of_training_examples

    def get_label_names(self, dataset: Union[Dataset, DatasetDict]) -> List[str]:
        if isinstance(dataset, Dataset):
            assert (
                "labels" in dataset.features
            ), "The name of dataset column that you want to use as a label must be 'labels'"

            features = dataset.features["labels"]
            label_names = features.feature.names
        else:
            assert (
                "train" in dataset.keys()
            ), "The key name of train dataset must be 'train'"
            assert (
                "labels" in dataset["train"].features
            ), "The name of dataset column that you want to use as a label must be 'labels'"

            features = dataset["train"].features["labels"]
            label_names = features.feature.names

        self.label_names = label_names

        return label_names

    def get_label_map(
        self, label_names: Union[List[str], Dataset, DatasetDict]
    ) -> Dict[str, Dict[str, str]]:
        if isinstance(label_names, Dataset) or isinstance(label_names, DatasetDict):
            label_names = self.get_label_names(label_names)

        id2label = {str(i): label for i, label in enumerate(label_names)}
        label2id = {v: k for k, v in id2label.items()}

        self.id2label = id2label
        self.label2id = label2id

        return {"label2id": label2id, "id2label": id2label}

    def make_B_to_I_label(self, label_names: List[str]) -> List[int]:
        # Map that sends B-Xxx label to its I-Xxx counterpart
        b_to_i_label = []
        for idx, label in enumerate(label_names):
            if label.startswith("B-") and label.replace("B-", "I-") in label_names:
                b_to_i_label.append(label_names.index(label.replace("B-", "I-")))
            else:
                b_to_i_label.append(idx)

        self.b_to_i_label = b_to_i_label

        return b_to_i_label

    def align_labels_with_tokens(self, labels, word_ids) -> List[int]:
        previous_word_idx = None
        label_ids = []
        for word_idx in word_ids:
            # Special tokens have a word id that is None. We set the label to pad token id
            if word_idx is None:
                label_ids.append(self._tokenizer.pad_token_id)
            # We set the label for the first token of each word.
            elif word_idx != previous_word_idx:
                label_ids.append(labels[word_idx])
            else:
                label_ids.append(self.b_to_i_label[labels[word_idx]])
            previous_word_idx = word_idx

        return label_ids

class DataCollatorForTokenClassification(object):
    """
    Data collator that will dynamically pad the inputs received, as well as the labels.
    """

    def __init__(
        self,
        processor: ProcessorForTokenClassification,
        padding_side: str = "right",
        label_pad_token_id: int = -100,
    ):
        if not isinstance(processor, ProcessorForTokenClassification):
            warnings.warn(
                "DataCollatorForTokenClassification is suitable for ProcessorForTokenClassification."
            )

        if processor._tokenizer.pad_token is None:
            warnings.warn(
                "If pad token doesn't exist in tokenizer, it can be a problem when applying padding."
            )

        self.tokenizer = processor._tokenizer
        self.padding_side = padding_side
        self.label_pad_token_id = label_pad_token_id

    def __call__(self, features: List[Dict[str, Any]]):
        label_name = "labels"
        labels = [feature[label_name] for feature in features]
        return self.pad(features, labels, label_name)

    def pad(self, features, labels, label_name):
        batch = self.tokenizer.pad(
            features,
            return_attention_mask=True,
            return_tensors=None,
        )

        sequence_length = torch.tensor(batch["input_ids"]).shape[1]
        padding_side = self.tokenizer.padding_side
        if padding_side == "right":
            batch[label_name] = [
                list(label) + [self.label_pad_token_id] * (sequence_length - len(label))
                for label in labels
            ]
        else:
            batch[label_name] = [
                [self.label_pad_token_id] * (sequence_length - len(label)) + list(label)
                for label in labels
            ]

        batch = {k: torch.tensor(v, dtype=torch.int64) for k, v in batch.items()}
        batch[label_name].masked_fill_(
            batch[label_name] == self.tokenizer.pad_token_id, self.label_pad_token_id
        )
        return batch


from fastapi import (
    APIRouter,
    HTTPException,
    Depends,
    status
)
from typing import List
from schemas.user import (
    UserInResp,
    UserInDB,
    UserInDbUpdate,
    UsersInResp
)
from crud.user import (
    find_user_by_id,
    create_new_user,
    get_all_users,
    update_user_in_db,
    delete_user_in_db,
    get_all_users_count
)
from utils.dependencies import (
    get_current_user
)
from uuid import UUID
from application import ps
from permissions_system.constants import (
    InternalTables,
    PermissionTypesEnum
)
from schemas.common import (
    DeleteResp
)

router = APIRouter()

@router.get("/{user_id}", response_model=UserInResp)
async def get_user_by_id(
    user_id: UUID,
    current_user=Depends(get_current_user)
):
    """
    Get a specific user by id
    """

    user = await find_user_by_id(user_id)
    if not user:
        raise HTTPException(
            status_code=404,
            detail="User not found.")
    return UserInResp(**user)

@router.get("/", response_model=UsersInResp)
async def get_users(
    offset: int = 0,
    limit: int = 10,
    current_user=Depends(get_current_user)
):
    """
    Get list of all users and
    total count of items for this resource in DB.
    """
    users = await get_all_users(offset, limit)
    count = await get_all_users_count()

    return UsersInResp(users=users, total_count=count)

@router.post("/", response_model=UserInResp)
async def create_user(user_in: UserInDB, current_user=Depends(get_current_user)):
    """
    Create new user.
    """
    user_id = await create_new_user(user_in)
    user = await find_user_by_id(user_id)
    return user

@router.put("/{user_id}", response_model=UserInResp)
async def update_user(
        user_id: UUID,
        user_in: UserInDbUpdate,
        current_user=Depends(get_current_user)):
    """
    Update specific user details.
    """
    user = await update_user_in_db(user_id, user_in)
    return user

@router.delete("/{user_id}", response_model=DeleteResp)
async def delete_user(
    user_id: UUID,
    current_user=Depends(get_current_user)
):
    """
    Delete specific user.
    """
    resp = {
        "deleted": None
    }
    if current_user["id"] != user_id and \
            current_user["group"] != "super_admin":
        raise HTTPException(status.HTTP_403_FORBIDDEN,
                            "You are not authorised for this operation.")

    deleted = await delete_user_in_db(user_id)
    resp.update(deleted=deleted)
    return resp


from flask import Flask, request, jsonify
from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
import psycopg2
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
engine = create_engine("sqlite:///citizen.db")
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///citizen.db'
db = SQLAlchemy(app)

#create model
Base = declarative_base()
class Citizen( db.Model):
    __tablename__ = 'citizen'
    id = db.Column(Integer, primary_key=True)
    prenom = db.Column(String(20))
    nom = db.Column(String(20))
    cni = db.Column(String(50))

#db.create_all()
#print('okffffff')
#Base.metadata.create_all(engine)
#print ("Table citizen crée ...")
Session = sessionmaker(bind= engine)

# definition des endpoints
@app.route("/api/citizen", methods=["POST"])
def create():
    try:
        #session = Session()
        new_citizen = Citizen(
            prenom = request.json['prenom'],
            nom = request.json['nom'],
            cni = request.json['cni'])
        db.session.add(new_citizen)
        db.session.commit()
        return new_citizen.cni
    except Exception as e:
        print(f" erreur '{e}' ")
        return {"error": "erreur lors de la creation du citoyen."}, 500

@app.route("/api/citizen", methods=['GET'])
def get():
    try:
        session = Session()
        citizen = db.session.query(Citizen).all()
        if citizen:
            result =[]
            for c in citizen:
                result.append({"id": c.id, "name": c.prenom, "nom":c.nom, "cni":c.cni})
            return jsonify(result)
        else:
            return jsonify({"error": f"Citizen non trouvé"}), 404
    except Exception as e:
        print(f"erreur '{e}'")
        return {"une erreur est survenue"}, 500

if __name__ == '__main__':
    app.run(debug=True, host="0.0.0.0")


class Solution(object):
    def minPathSum(self, grid):
        rows = len(grid)
        cols = len(grid[0])

        # First row
        first_row = grid[0]
        for col in range(1, cols):
            first_row[col] += first_row[col-1]

        # First column
        for row in range(1, rows):
            grid[row][0] += grid[row-1][0]

        for row in range(1, rows):
            for col in range(1, cols):
                grid[row][col] += min(grid[row-1][col], grid[row][col-1])

        return grid[-1][-1]


import appdaemon.plugins.hass.hassapi as hass
import appdaemon.plugins.mqtt.mqttapi as mqtt
import globals
import json
import random

"""
Monitor events and output changes to the verbose_log. Nice for debugging purposes.
Arguments:
 - events: List of events to monitor
"""

class RassToHappyBubbles(mqtt.Mqtt):
    base_topic = "home/presence"
    def initialize(self) -> None:
        self.log('Initialize RassToHappyBubbles')
        self.mqtt_subscribe('{}/#'.format(self.base_topic), namespace='mqtt')
        self.listen_event(self.event_listener, 'MQTT_MESSAGE', namespace='mqtt')

    def event_listener(self, event_name, data, kwargs):
        topic = data.get('topic', '')
        if self.base_topic	not in topic:
        	return
        #array = ['hall', 'balcony']
        #random.shuffle(array)
        #device=array[0]

        device = topic.replace('{}/'.format(self.base_topic), "")
        payload = json.loads(data['payload']) if data.get('payload') else {}
        id = payload.get('id', None)
        hb_topic="happy-bubbles/ble/{}/raw/{}".format(device,id)
        payload['hostname'] = device
        payload['mac'] = payload.get('id', '')

        payload_data = json.dumps(payload)
        self.mqtt_publish(hb_topic, payload_data, qos = 0, retain = False, namespace='mqtt')
        # self.log('publishing to: {}: {}'.format(hb_topic, payload_data))


import serial

class Light:

    def __init__(self, comport, baudrate=9600):
        self.ser = serial.Serial(comport, baudrate, timeout=1)

    def on(self, terminal):
        assert terminal in [0, 1, 2, 3]
        # Turn on light 1
        if terminal == 0:
            self.ser.write('$310FF16'.encode('utf-8'))
        # Turn on light 2
        elif terminal == 1:
            self.ser.write('$320FF15'.encode('utf-8'))
        # Turn on light 3
        elif terminal == 2:
            self.ser.write('$330FF14'.encode('utf-8'))
        # Turn on light 4
        elif terminal == 3:
            self.ser.write('$340FF13'.encode('utf-8'))

    def off(self, terminal):
        assert terminal in [0, 1, 2, 3]
        # Turn off light 1
        if terminal == 0:
            self.ser.write('$3100016'.encode('utf-8'))
        # Turn off light 2
        elif terminal == 1:
            self.ser.write('$3200015'.encode('utf-8'))
        # Turn off light 3
        elif terminal == 2:
            self.ser.write('$3300014'.encode('utf-8'))
        # Turn off light 4
        elif terminal == 3:
            self.ser.write('$3400013'.encode('utf-8'))


import csv
import json
import xml.etree.ElementTree as ET
import os

def read(filenames):
    res = {'n': 0}
    if len(filenames) == 0:
        print("В директории нет файлов с подходящим расширением")
        return {}
    for i in filenames:
        if i[-4:] == '.csv':  # Читаем csv
            res = read_csv(res, i, res['n'])
            print(i, 'прочитан')
        elif i[-5:] == '.json':  # Читаем json
            res = read_json(res, i, res['n'])
            print(i, 'прочитан')
        elif i[-4:] == '.xml':  # Читаем xml
            res = read_xml(res, i, res['n'])
            print(i, 'прочитан')
        if res == {}:
            return res
    return res

def read_csv(res, filename, n):
    b = False
    with open(filename) as file:
        csvreader = csv.reader(file)
        for row in csvreader:
            if not b:  # Если на вход поступили заголовки
                l = len(row)
                # Для упорядочевания столбцов:
                key1 = [int(i[1:]) - 1 if i[0] == 'D' else -1 for i in row]
                key2 = [int(i[1:]) - 1 if i[0] == 'M' else -1 for i in row]
                b = True
                if res['n'] == 0:  # Вычисление n
                    res['n'] = len(list(filter(lambda x: x > -1, key1)))
                    n = res['n']
                continue
            # Группирую столбцы D i-ые
            arg = ' '.join(row[key1.index(i)] for i in range(n))
            if arg == '':
                print('Столбцы D отсутствуют, некорректный ввод данных.')
                return {}
            try:
                if arg in res.keys():  # Либо изменяем значения,
                    for i in range(n):
                        res[arg][i] += int(row[key2.index(i)])
                else:  # либо добавляем новый ключ
                    res[arg] = [int(row[key2.index(i)]) for i in range(n)]
            except ValueError:
                print(f'Значение столбца М в строке {arg}',
                       ' не является числом')
                return {}
            except IndexError:
                print('Количество столбцов M меньше значения n,',
                       ' некорректный ввод данных.')
                return {}
    return res

def read_json(res, filename, n):
    with open(filename) as file:
        input_value = json.load(file)
        first = input_value['fields'][0].keys()
        if res['n'] == 0:
            for i in first:
                if i[0] == 'D':
                    res['n'] += 1
            n = res['n']
        key1 = ['D' + str(i) for i in range(1, n+1)]
        key2 = ['M' + str(i) for i in range(1, n+1)]
        try:
            for row in input_value['fields']:
                arg = ' '.join(row[i] for i in key1)
                if arg in res.keys():
                    for i in range(n):
                        res[arg][i] += int(row[key2[i]])
                else:
                    res[arg] = [int(row[key2[i]]) for i in range(n)]
        except KeyError:
            print("Отсутвствует значение в столбце М или D")
            return{}
        except ValueError:
            print(f'Значение столбца М в строке {arg}',
                       ' не является числом')
            return {}
        except IndexError:
                print('Количество столбцов M меньше значения n,',
                       ' некорректный ввод данных.')
                return {}
    return res

def read_xml(res, filename, n):
    arg = ''
    tree = ET.parse(filename)  # Создаем дерево
    root = tree.getroot()  # Запоминаем корень
    b = True
    for one in root:  # Перебираем вершины
        row = {}
        for two in one:
            row[two.attrib['name']] = two[0].text
        if b:
            first = row.keys()
            if n == 0:
                for i in first:
                    if i[0] == 'D':
                        res['n'] += 1
                n = res['n']
            key1 = ['D' + str(i) for i in range(1, n+1)]
            key2 = ['M' + str(i) for i in range(1, n+1)]
            b = False
        try:
            arg = ' '.join(row[i] for i in key1)
            if arg in res.keys():
                for i in range(n):
                    res[arg][i] += int(row[key2[i]])
            else:
                res[arg] = [int(row[key2[i]]) for i in range(n)]
        except KeyError:
            print("Отсутвствует значение в столбце М или D")
            return{}
        except ValueError:
            print(f'Значение столбца М в строке {arg}',
                       ' не является числом')
            return {}
        except IndexError:
                print('Количество столбцов M меньше значения n,',
                       ' некорректный ввод данных.')
                return {}
    return res


from typing import Optional, Tuple

from posggym import core

import posggym.envs.grid_world.render as render_lib

import posggym.envs.grid_world.driving.model as dmodel
from posggym.envs.grid_world.driving.grid import DrivingGrid

class DrivingEnv(core.DefaultEnv):
    """The Driving Grid World Environment.

    A general-sum 2D grid world problem involving multiple agents. Each agent
    controls a vehicle and is tasked with driving the vehicle from it's start
    location to a destination location while avoiding crashing into obstacles
    or other vehicles.

    This environment requires each agent to navigate in the world while also
    taking care to avoid crashing into other players. The dynamics and
    observations of the environment are such that avoiding collisions requires
    some planning in order for the vehicle to brake in time or maintain a good
    speed. Depending on the grid layout, the environment will require agents to
    reason about and possibly coordinate with the other vehicles.

    Agents
    ------
    Varied number

    State
    -----
    Each state is made up of the state of each vehicle, which in turn is
    defined by:

    - the (x, y) (x=column, y=row, with origin at the top-left square of the
      grid) of the vehicle,
    - the direction the vehicle is facing NORTH=0, SOUTH=1, EAST=2, WEST=3),
    - the speed of the vehicle (REVERSE=-1, STOPPED=0, FORWARD_SLOW=1,
      FORWARD_FAST=2),
    - the (x, y) of the vehicles destination
    - whether the vehicle has reached it's destination or not
    - whether the vehicle has crashed or not

    Actions
    -------
    Each agent has 5 actions: DO_NOTHING=0, ACCELERATE=1, DECELERATE=2,
    TURN_RIGHT=3, TURN_LEFT=4

    Observation
    -----------
    Each agent observes their current speed along with the cells in their local
    area. The size of the local area observed is controlled by the `obs_dims`
    parameter. For each cell in the observed are the agent observes whether
    they are one of four things: VEHICLE=0, WALL=1, EMPTY=2, DESTINATION=3.
    Each agent also observes the (x, y) coordinates of their destination,
    whether they have reached the destination, and whether they have crashed.

    Each observation is represented as a tuple:
        ((local obs), speed, destination coord, destination reached, crashed)

    Reward
    ------
    All agents receive a penalty of 0.00 for each step. They also recieve a
    penalty of -0.5 for hitting an obstacle (if ``obstacle_collision=True``),
    and -1.0 for hitting another vehicle. A reward of 1.0 is given if the agent
    reaches it's destination.

    Transition Dynamics
    -------------------
    Actions are deterministic and movement is determined by direction the
    vehicle is facing and it's speed:

    - Speed=-1 (REVERSE) - vehicle moves one cell in the opposite direction
    - Speed=0 (STOPPED) - vehicle remains in same cell
    - Speed=1 (FORWARD_SLOW) - vehicle move one cell in facing direction
    - Speed=1 (FORWARD_FAST) - vehicle moves two cells in facing direction

    Accelerating increases speed by 1, while deceleration decreased speed by 1.
    If the vehicle will hit a wall or an other vehicle when moving from one
    cell to another then it remains in it's current cell and its crashed state
    variable is updated. Once a vehicle reaches it's destination it is stuck.

    Episodes ends when all agents have either reached their destination or
    crashed, or the episode step limit is reached.

    """

    metadata = {"render.modes": ["human", "ansi", "rgb_array"]}

    def __init__(
        self,
        grid: DrivingGrid,
        num_agents: int,
        obs_dim: Tuple[int, int, int],
        obstacle_collisions: bool,
        **kwargs,
    ):
        self._model = dmodel.DrivingModel(
            grid, num_agents, obs_dim, obstacle_collisions, **kwargs
        )
        self._obs_dim = obs_dim
        self._viewer = None
        self._renderer: Optional[render_lib.GWRenderer] = None
        super().__init__()

    def render(self, mode: str = "human"):
        if mode == "ansi":
            grid_str = self._model.grid.get_ascii_repr(
                [vs.coord for vs in self._state],
                [vs.facing_dir for vs in self._state],
                [vs.dest_coord for vs in self._state],
            )

            output = [
                f"Step: {self._step_num}",
                grid_str,
            ]
            if self._last_actions is not None:
                action_str = ", ".join(
                    [dmodel.ACTIONS_STR[a] for a in self._last_actions]
                )
                output.insert(1, f"Actions: <{action_str}>")
                output.append(f"Rewards: <{self._last_rewards}>")

            return "\n".join(output) + "\n"
        elif mode in ("human", "rgb_array"):
            grid = self.model.grid
            if mode == "human" and self._viewer is None:
                # pylint: disable=[import-outside-toplevel]
                from posggym.envs.grid_world import viewer

                self._viewer = viewer.GWViewer(  # type: ignore
                    "Driving Env",
                    (min(grid.width, 9), min(grid.height, 9)),
                    num_agent_displays=self.n_agents,
                )
                self._viewer.show(block=False)  # type: ignore

            if self._renderer is None:
                self._renderer = render_lib.GWRenderer(
                    self.n_agents, grid, [], render_blocks=True
                )

            agent_obs_coords = tuple(
                self._model.get_obs_coords(vs.coord, vs.facing_dir)
                for vs in self._state
            )
            agent_coords = tuple(vs.coord for vs in self._state)
            agent_dirs = tuple(vs.facing_dir for vs in self._state)

            # Add agent destination locations
            other_objs = [
                render_lib.GWObject(
                    vs.dest_coord,
                    render_lib.get_agent_color(i),
                    render_lib.Shape.RECTANGLE,
                    # make dest squares slightly different to vehicle color
                    alpha=0.2,
                )
                for i, vs in enumerate(self._state)
            ]
            # Add visualization for crashed agents
            for i, vs in enumerate(self._state):
                if vs.crashed:
                    other_objs.append(
                        render_lib.GWObject(vs.coord, "yellow", render_lib.Shape.CIRCLE)
                    )

            env_img = self._renderer.render(
                agent_coords,
                agent_obs_coords,
                agent_dirs=agent_dirs,
                other_objs=other_objs,
                agent_colors=None,
            )
            agent_obs_imgs = self._renderer.render_all_agent_obs(
                env_img,
                agent_coords,
                agent_dirs,
                agent_obs_dims=self._obs_dim,
                out_of_bounds_obj=render_lib.GWObject(
                    (0, 0), "grey", render_lib.Shape.RECTANGLE
                ),
            )

            if mode == "human":
                self._viewer.display_img(env_img, agent_idx=None)  # type: ignore
                for i, obs_img in enumerate(agent_obs_imgs):
                    self._viewer.display_img(obs_img, agent_idx=i)  # type: ignore
            else:
                return (env_img, agent_obs_imgs)
        else:
            super().render(mode)

    @property
    def model(self) -> dmodel.DrivingModel:
        return self._model

    def close(self) -> None:
        if self._viewer is not None:
            self._viewer.close()  # type: ignore
            self._viewer = None


#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Faça um programa que tenha uma função notas() que pode receber
# várias notas de alunos e vai retornar um dicionário com as seguintes
# informações:
#   - Quantidades de notas
#   - A maior nota
#   - A menor nota
#   - A média da turma
#   - A situação (opcional)
# Adicione também as docstrings da função.

def notas(*n, sit=False):
    """
    ------------------------------------------------------------------------------
    notas(*n, sit=False)
    |   -> Função para analisar notas e situações de vários alunos.
    |
    |   :param n: uma ou mais notas dos alunos (aceita várias).
    |   :param sit: valor opcional, indicando se deve ou não adicionar a situação.
    |   :return: dicionário com várias informações sobre a situação da turma.
    ------------------------------------------------------------------------------
    """
    dados_notas = {'Quantidade': 0,
                   'Maior_nota': 0,
                   'Menor_nota': 0,
                   'Media_turma': 0}
    maior = menor = cont = soma = 0

    for nota in n:
        soma += nota
        if cont == 0:
            maior = nota
            menor = nota
        else:
            if nota > maior:
                maior = nota
            if nota < menor:
                menor = nota
        cont += 1

    dados_notas['Quantidade'] = len(n)
    dados_notas['Maior_nota'] = maior
    dados_notas['Menor_nota'] = menor
    dados_notas['Media_turma'] = soma / len(n)

    if sit:
        if dados_notas['Media_turma'] >= 7:
            dados_notas['Situação'] = 'BOA'
        elif dados_notas['Media_turma'] >= 5:
            dados_notas['Situação'] = 'RAZOÁVEL'
        else:
            dados_notas['Situação'] = 'RUÍM'

    return dados_notas

info = notas(3.5, 2, 6.5, 2, 7, 4, sit=True)
print(info)


import matplotlib.pyplot as plt
import numpy as np
file1="changed_ipcp"
file2="ipcp"

with open(file1, 'r') as f1:
    data1 = [line.strip().split() for line in f1.readlines()]

with open(file2, 'r') as f2:
    data2 = [line.strip().split() for line in f2.readlines()]

fig, ax = plt.subplots(figsize=(14, 10.5))
combined_data = []
k=0
mul1=1
mul2=1
for row in data1:
    name=row[0].split('.trace.gz')[0]
    name=name.split('.champsimtrace')[0]
    value1=row[1]
    mul1*=float(value1)
    value2 = data2[k][1]
    mul2*=float(value2)
    k+=1
    combined_data.append((name, float(value1), float(value2)))

# Sort the data by name
combined_data.sort(key=lambda x: x[0])

# Extract the data into separate lists for plotting
names = [x[0] for x in combined_data]
values1 = [x[1] for x in combined_data]
values2 = [x[2] for x in combined_data]

N = len(names)
ind = np.arange(N)
width = 0.35

bar1 = plt.bar(ind, values1, width, color='r')
bar2 = plt.bar(ind+width, values2, width, color='b')

plt.axhline(y=mul1**(1/len(names)), color='r', linestyle='--')  # Add a horizontal line at y
plt.axhline(y=mul2**(1/len(names)), color='b', linestyle='--')
file1=file1.replace(".txt","")
file2=file2.replace(".txt","")
print(f"Cummulative ipc of {file1}:",mul1**(1/len(names)))
print(f"Cummulative ipc of {file2}:",mul2**(1/len(names)))

plt.xlabel("Traces")
plt.ylabel('IPC') # or IPC or MISS RATE
plt.title("IPC comparison")
plt.xticks(ind+width/2, names)
plt.xticks(rotation=90)
plt.legend((bar1, bar2), (file1, file2))
#plt.ylim(ymin=100000)
#plt.ylim(0, 1.15)
# Adjust top margin
fig.subplots_adjust(top=0.95)
plt.savefig(f"{file1}_{file2}.png")


from importlib import metadata

import typer

from oterm.app.oterm import app

cli = typer.Typer()

@cli.command()
def oterm(version: bool = typer.Option(None, "--version", "-v")):
    if version:
        typer.echo(f"oterm v{metadata.version('oterm')}")
        exit(0)
    app.run()

if __name__ == "__main__":
    cli()


class CS:

    def __init__(self, full_path, cs_name, topic_class):
        self.full_path = full_path
        self.cs_name = cs_name
        self.topic = topic_class

        # Cheatsheet Metadata (not used)
        self.author = ""
        self.ref = []
        self.tags = []


'''
16/08/2019 @queenjobo @ksamocha

Script to run enrichment test part of the DeNovoWEST testing framework

For usage see 'python DNE_test.py -h'

make a change
'''

__version__ = 1.2
__date__ = '2019-01-09'

#IMPORTS--------------

import logging
import random
import argparse
import pandas as pd
import numpy as np
import itertools
import datetime
from scipy import stats
from probabilities import *

#set seed
#random.seed(31)
#np.random.seed(31)
random.seed(2014)
np.random.seed(2014)

#get todays date
now = datetime.datetime.now()
today = str(now).split(' ')[0]

#default paths
wdicpath = "input/loess_weight_dic_v4.tab"
denovopath = "input/de_novos.txt.gz"
outpath = "DeNovoWEST_enrichment_results_" + today + ".tab"

#FUNCTIONS---------------

def get_options():
    '''
    parse options
    '''
    parser = argparse.ArgumentParser()
    #parser.add_argument('--weightdic', default=wdicpath,
    #    help='path to dictionary of weights to assign variants')
    parser.add_argument('--rates', help='path to all possible exome SNVs with specific rates and weights')
    parser.add_argument('--denovos', default=denovopath,  help='path to annotated denovo variants')
    parser.add_argument('--output', default= outpath, help = "output file to save to")
    parser.add_argument('--nmales',type = int, help = 'number of males in study')
    parser.add_argument('--nfemales', type = int, help = 'number of females in study')
    parser.add_argument('--hgbuild', default = "hg19", help = 'genome assembly build')
    parser.add_argument('--nsim',default = 1000000000, type = int,help = 'minimum number of simulations for each gene (default 1 billion)')
    parser.add_argument('--pvalcap',type = float, default = 1.0, help = "stop simulations if cumulative p-value > pvalcap (default 1)")
    return parser.parse_args()

def load_denovos(path):
    '''
    get a DataFrame of DDD de novos
    '''
    logging.info("Loading de novos")
    denovos = pd.read_table(path, sep='\t', na_values='.', keep_default_na=True) # added sep
    denovos['Chrom'] = denovos['Chrom'].astype(str)
    denovos['Position'] = denovos['Position'].astype(int)
    # denovos['score'] = denovos['score'].astype(float)
    # denovos = fix_consequences(denovos)
    return(denovos)

def load_rates(ratespath,genes):
    '''
    load rates file
    '''
    #rates = pd.read_table(ratespath,names = ['symbol','chrom','pos','ref','alt','cq','prob','raw','score','maf','constrained'])
    logging.info("Loading rates")
    rates = pd.read_table(ratespath, sep='\t', na_values='.', keep_default_na=True)
    #subset to only genes observed in de novo file
    rates = rates[rates['GeneID'].isin(genes)]
    rates['Chrom'] = rates['Chrom'].astype(str)
    #rates['Position'] = rates['Position'].astype(int)
    return(rates)

def correct_for_x_chrom(rates, male_n, female_n, hgbuild):
    '''
    correct for X chromosome
    '''
    autosomal = 2 * (male_n + female_n)
    female_transmit = male_n + female_n
    male_transmit = female_n

    # get scaling factors using the alpha from the most recent SFHS (Scottish
    # Family Health Study) phased de novo data.
    alpha = 3.4
    male_k = 2 / (1 + (1 / alpha))
    female_k = 2 / (1 + alpha)

    # correct the non-PAR chrX genes for fewer transmissions and lower rate
    # (dependent on alpha)
    # need to account for PAR part of chrX! <-- TBD
    if hgbuild=="hg19" or hgbuild=="b37":
        hapX = rates['Chrom'].isin(['X', 'chrX']) & rates['Position'].between(2699520,154931044)
    elif hgbuild=="hg38" or hgbuild=="b38":
        hapX = rates['Chrom'].isin(['X', 'chrX']) & rates['Position'].between(2781479,155701382)
    else:
        hapX = rates['Chrom'].isin(['X', 'chrX'])

    x_factor = ((male_transmit * male_k) + (female_transmit * female_k)) / autosomal
    x_factor = pd.Series([x_factor] * len(hapX), index=rates.index)
    x_factor[~hapX] = 1

    rates['prob'] *= x_factor

    return rates

def get_expected_rates(rates, male, female, hgbuild):
    '''
    get expected mutation rates
    '''
    logging.info("get expected")
    autosomal = 2 * (male + female)
    rates['prob'] = rates['prob'] * autosomal

    return correct_for_x_chrom(rates, male, female, hgbuild)

#def test_gene(rates,denovos,gene,nsim,indel_weights,pvalcap):
def test_gene(rates,denovos,gene,nsim,pvalcap):
    '''
    test single gene given rates object and de novos
    '''
    generates = rates[rates.GeneID == gene]
    # Indel rates has been added to the rates df
    # generates = get_indel_rates(generates,indel_weights)
    # here add inframe/frameshift rates
    s_obs = denovos[denovos.GeneID == gene].weight.sum()
    pval,info,s_exp = get_pvalue(generates,s_obs,nsim,pvalcap)
    return(s_exp,s_obs,pval,info)

#def test_all_genes(rates,denovos,nsim,indel_weights,pvalcap):
def test_all_genes(rates,denovos,nsim,pvalcap):
    '''
    test all genes for enrichment
    '''
    logging.info("Starting tests: ")
    #genes = rates.symbol.unique()
    genes = denovos.GeneID.unique()
    results = []
    for gene in genes:
        #skip gene if no observed de novos in our dataset
        #if denovos[denovos.symbol == gene].weight.sum() == 0:
        #    continue
        #skip gene if no rates in our dataset
        if gene not in rates.GeneID.unique():
            logging.info("could not find " + str(gene))
            continue
        logging.info("testing " + str(gene))
        #s_exp,s_obs,pval,info = test_gene(rates,denovos,gene,nsim,indel_weights,pvalcap)
        s_exp,s_obs,pval,info = test_gene(rates,denovos,gene,nsim,pvalcap)
        # extract hgnc_id
        # hgnc = denovos.loc[denovos['GeneID'] == gene, 'hgnc_id'].iloc[0]
        # results.append((gene,hgnc,s_exp,s_obs,pval,info))
        results.append((gene,s_exp,s_obs,pval,info))
    return(results)

def main():
    args = get_options()

    #initialise log file
    logging.basicConfig(filename=args.output.split(".")[0]+".log",level=logging.DEBUG)

    #load denovos
    denovos = load_denovos(args.denovos)
    #get genes in de novo file so only run test on these
    genes = denovos.GeneID.unique()
    logging.info("genes:" + ",".join(genes))

    #use de novo mutation weights
    #denovos,_ = get_weights(denovos,args.weightdic,False)
    denovos = denovos[denovos.weight.apply(np.isreal)]
    denovos = denovos.dropna(subset=['weight'])

    #load rates and get expected
    rates = load_rates(args.rates,genes)
    rates = rates.dropna(subset=['weight','prob'])

    logging.info("rates loaded")
    if len(rates.index) == 0:
        logging.info("rates are empty")
        return

    if args.hgbuild != "hg19" and args.hgbuild != "b37" and args.hgbuild != "hg38" and args.hgbuild != "b38":
        logging.info("cannot recognize genome assembly build : " + args.hgbuild)
        return

    rates = get_expected_rates(rates, args.nmales, args.nfemales, args.hgbuild)
    #get weights
    #logging.info("get rates weights")
    #rates,indel_weights = get_weights(rates,args.weightdic,False)
    rates = rates[rates.weight.apply(np.isreal)]

    # run gene tests
    #results = test_all_genes(rates,denovos,args.nsim,indel_weights,args.pvalcap)
    results = test_all_genes(rates,denovos,args.nsim,args.pvalcap)

    #save results to file
    # df = pd.DataFrame.from_records(results,columns = ["symbol","hgnc_id","expected","observed","p-value","info"])
    df = pd.DataFrame.from_records(results,columns = ["GeneID","Expected","Observed","P-value","Info"])
    df.to_csv(args.output, sep = "\t", index = False)

#------------------SCRIPT--------------------------------------

if __name__=='__main__':
    main()


from flask import Flask, render_template, request, redirect
from main import Palette
from werkzeug.utils import secure_filename
from forms import UploadForm
import os
import datetime

app = Flask(__name__)

SECRET_KEY = os.urandom(32)
app.config['SECRET_KEY'] = SECRET_KEY
PEOPLE_FOLDER = os.path.join('static', 'photo')
app.config['UPLOAD_FOLDER'] = PEOPLE_FOLDER

current_year = datetime.datetime.now().year
print(current_year)

@app.route("/", methods=["GET", "POST"])
def home():
    form = UploadForm()
    if request.method == "GET":
        image = None
        for file in os.listdir(os.path.join(PEOPLE_FOLDER)):
            if os.path.isfile(os.path.join(PEOPLE_FOLDER, file)):
                image = os.path.join(PEOPLE_FOLDER, file)
                break
        return render_template('home.html', form=form, image=image, year=current_year);
    elif request.method == "POST":
        for file in os.listdir(PEOPLE_FOLDER):
            os.remove(os.path.join(PEOPLE_FOLDER, file))
            # if os.path.isfile(file):
            #     os.remove(file)
        if form.validate_on_submit():
            folder = 'images/'
            if not os.path.exists(folder):
                os.mkdir(folder)
            number = int(request.form['color_count'])
            if request.files['file']:
                f = request.files['file']
                filename = secure_filename(f.filename)
                print (number)
                new_path = os.path.join(folder, filename)
                print(new_path)
                f.save(new_path)
                palette = Palette(filename, 1200, folder, number)
                palette.get_image()
                colors = palette.get_colors()
                palette.save_to_static(folder, PEOPLE_FOLDER, 1080, filename)
                file_to_send = os.path.join(PEOPLE_FOLDER, filename)
                palette.delete_from_images()
                print(colors)
                return render_template('home.html', form=form, colors=enumerate(colors), image=file_to_send, year=current_year)

@app.route("/features")
def features():
    return render_template('features.html', year=current_year)

if __name__ == '__main__':
    app.run(debug=True, port=5000);


class Solution:
    def addToArrayForm(self, num: List[int], k: int) -> List[int]:

        s = 0
        for n in num:
            s *= 10
            s += n

        return [int(x) for x in str(s+k)]


import random
import elote as elo
import constructs.Food

class Initializer:

    def __init__(self, foods, mu=500, sigma=40):
        for food in foods:
            food.elo_competitor = elo.EloCompetitor(initial_rating=random.gauss(mu, sigma))
            food.rating = food.elo_competitor.rating

        self.foods = foods

    def __str__(self):
        return f"{[food.__str__() for food in self.foods]}"


n = int(input())
jumps = [int(i) for i in input().split()]
# jumps = [*map(int,input().split())]
MAX = 1001
dp = [MAX] * n
dp[0] = 0
for i in range(n):
    for j in range(1, jumps[i] + 1): #0칸 점프는 제자리 점프
        if i + j < n:
            dp[i+j] = min(dp[i+j], dp[i] + 1) #각 위치까지 도달할 수 있는 최소 점프수를 구한다.
        else:
            break
    if dp[-1] != MAX: break #마지막 값까지 도달하는 점프 수를 구하면 종료한다.

print(-1) if dp[-1] == MAX else print(dp[-1])