import xdj
@xdj.Controller(
    url="courseware/team/(?P<course_key_string>([^/]+/[^/]+/[^/]+|[^/:]+:[^/+]+\+[^/+]+(\+[^/]+)?))(?:/(?P<email>.+))?",
    template="courseware_team.html"
)
class CoursewareTeamController(xdj.BaseController):
    def on_get(self,model):
        return self.render(model)

    def loadStaffs(self,model):
        from django.db.models import Q
        from student.models import CourseAccessRole
        from opaque_keys.edx.keys import CourseKey
        course_id = CourseKey.from_string(model.params.course_key_string)
        filter = Q(course_id=course_id) & (~Q(user_id=model.user.id))
        staffs = CourseAccessRole.objects.filter(filter).all()
        total = CourseAccessRole.objects.filter(filter).count()

        items = []

        for staff in staffs:
            items.append(dict(
                username = staff.user.username
            ))

        return dict(
            items = items,
            total =total
        )


import os

import cv2 as cv
import numpy as np

def truth(path, truth_root):
    """Load the corresponding truth image."""
    # Get the common part between the image path and truth root.
    common = os.path.commonprefix([path, truth_root])
    # Find the part of the image path that is not common with the truth root.
    rel_impath = os.path.relpath(path, common)
    # Take that relative path from the truth root, and that is the truth image.
    truth_impath = os.path.join(truth_root, rel_impath)
    truth_im = cv.imread(truth_impath, cv.IMREAD_GRAYSCALE)
    if type(truth_im) == None.__class__:
        raise ValueError("no truth image found at '{}'".format(truth_impath))
    return truth_im

def compare(imregion, path, truth_root):
    """Compare the given image region with the associated ground truth. Returns
    a tuple containing the confusion matrix and the comparison image.

    Green where the region covers the truth.
    Red where the truth is not covered by the region.
    Blue where the region is not atop the truth.
    """
    # Load the truth image for this region.
    truth_im = truth(path, truth_root)

    # Construct a 3-color image in BGR.
    result = np.zeros((imregion.shape[0], imregion.shape[1], 3),
            dtype=np.uint8)

    # True positives are the same in the ground truth and input image.
    true_positive = (truth_im > 0) & (imregion == truth_im)

    # False positives are hits in the input image and not the ground truth.
    false_positive = imregion > truth_im

    # False negatives are hits in the grouth truth and not the input image.
    false_negative = truth_im > imregion

    # True negatives are zero in both the ground truth and input image.
    true_negative = (truth_im == 0) & (imregion == 0)

    result[:,:,0][false_positive] = 255 # blue
    result[:,:,1][true_positive]  = 255 # green
    result[:,:,2][false_negative] = 255 # red

    pixels = float(truth_im.size)
    confusion = np.matrix([[true_positive.sum()/pixels, false_negative.sum()/pixels],
                           [false_positive.sum()/pixels, true_negative.sum()/pixels]])
    return confusion, result


import rdflib, requests
from flask import Flask, render_template, request, jsonify

#######################################################################################################################################################
#######################################################################################################################################################
########################################################## Environment ###############################################33###########################
#######################################################################################################################################################

app = Flask(__name__)

# Local database
g = rdflib.Graph()
g.parse("data/rdf-with-links.ttl", format="ttl")

#######################################################################################################################################################
#######################################################################################################################################################
########################################################## SPARQL Queries ###############################################33###########################
#######################################################################################################################################################
def run_sparql_query(district_name, year="2021"):
    g = rdflib.Graph()
    g.parse("data/rdf-with-links.ttl", format="ttl")

    query = f"""
            PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
            PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
            PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
            PREFIX owl: <http://www.w3.org/2002/07/owl#>
            PREFIX dbo: <http://dbpedia.org/ontology/>
            PREFIX nso: <http://madridwastemanagement.org/group01/ontology/>
            PREFIX wst: <http://www.disit.org/km4city/schema#>
            PREFIX time: <http://www.w3.org/2006/time#>

            SELECT ?wasteName ?wikidataLink ?m (SUM(?val) AS ?totalAmount)
            WHERE {{
              ?districtInstance a dbo:District ;
                    rdfs:label "{district_name}";
                    wst:hasResidue ?wasteinstance.
              ?wasteinstance rdfs:label ?wasteName;
                 nso:hasTotal ?totalinstance.
              ?totalinstance nso:value ?val;
                 time:month ?m;
                 time:year "{year}"^^xsd:gYear.
              OPTIONAL {{ ?wasteinstance owl:sameAs ?wikidataLink. }}
            }}
            GROUP BY ?wasteName ?wikidataLink ?m
            ORDER BY ?m ?wasteName
            """
    results = g.query(query)
    output = []
    for row in results:
        output.append({
            "wasteName": str(row[0]),
            "wikidataLink": str(row[1]) if row[1] is not None else "",
            "month": str(row[2]),
            "totalAmount": float(row[3])
        })
    return output

def format_waste_type(waste_type):
    waste_type_map = {
        "CDW": "Construction and Demolition Waste",
        "biomedicalWaste": "Biomedical Waste",
        "clothing": "Clothing",
        "glass": "Glass",
        "non-recyclable-waste": "Non Recyclable Waste",
        "organicFood": "Organic Food",
        "packagingProductsOfPlastics": "Packaging Products of Plastic",
        "wastePaper": "Waste Paper",
        "wasteContainer": "Waste Container",
        "horseBed": "Horse Bed"
    }
    return waste_type_map.get(waste_type, "Unknown Waste Type")

app.jinja_env.filters['format_waste'] = format_waste_type

def reverse_format_waste_type(formatted_waste_type):
    reverse_waste_type_map = {
        "Construction and Demolition Waste": "CDW",
        "Biomedical Waste": "biomedicalWaste",
        "Clothing": "clothing",
        "Glass": "glass",
        "Non Recyclable Waste": "non-recyclable-waste",
        "Organic Food": "organicFood",
        "Packaging Products of Plastic": "packagingProductsOfPlastics",
        "Waste Paper": "wastePaper",
        "Waste Container": "wasteContainer",
        "Horse Bed": "horseBed"
    }
    return reverse_waste_type_map.get(formatted_waste_type, formatted_waste_type)

def fetch_yearly_district_waste(year="2021"):
    """
    Fetches the total amount of waste for each district for a specified year.

    :param year: The year for which the waste data is to be fetched.
    """
    g = rdflib.Graph()
    g.parse("data/rdf-with-links.ttl", format="ttl")

    query = f"""
            PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
            PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
            PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
            PREFIX owl: <http://www.w3.org/2002/07/owl#>
            PREFIX dbo: <http://dbpedia.org/ontology/>
            PREFIX nso: <http://madridwastemanagement.org/group01/ontology/>
            PREFIX wst: <http://www.disit.org/km4city/schema#>
            PREFIX time: <http://www.w3.org/2006/time#>

            SELECT ?districtName (SUM(xsd:float(?val)) AS ?totalAmount) ?year
            WHERE {{
              ?districtInstance a dbo:District ;
                                rdfs:label ?districtName;
                                wst:hasResidue ?wasteinstance.
              ?wasteinstance nso:hasTotal ?totalinstance.
              ?totalinstance nso:value ?val;
                             time:year ?year.
            FILTER (?year = "{year}"^^xsd:gYear)

            }}

            GROUP BY ?districtName
            ORDER BY ?year
            """
    results = g.query(query)
    output = []
    for row in results:
        output_dict = {
            "districtName": str(row[0]),
            "totalAmount": float(row[1]),
            "year": str(row[2]),
        }
        output.append(output_dict)
    return output

def run_district_details_query(district_id):
    endpoint_url = "https://query.wikidata.org/sparql"
    """
           Given a district id it retrieves the details of the district from wikidata
           :param district_name:
           :return:
           """
    query = f"""
            PREFIX schema: <http://schema.org/>
            PREFIX wdt: <http://www.wikidata.org/prop/direct/>
            PREFIX wd: <http://www.wikidata.org/entity/>

            SELECT ?population ?description ?area ?coordinates ?label
            WHERE {{
              BIND(wd:{district_id} AS ?district)
              ?district wdt:P1082 ?population.
              OPTIONAL {{ ?district wdt:P2046 ?area}}.
              ?district schema:description ?description.
              ?district wdt:P625 ?coordinates.
              ?district rdfs:label ?label .
              FILTER (langMatches( lang(?label), "ES" ) )
              FILTER(LANG(?description) = "en")
            }}
            """
    try:
        response = requests.get(endpoint_url, params={'query': query, 'format': 'json'})
        data = response.json()
        results = data['results']['bindings']
        output = []
        for result in results:
            output_dict = {
                'population': result['population']['value'],
                'description': result['description']['value'],
                'area': result.get('area', {'value': '--'})['value'],
                'coordinates': result['coordinates']['value'],
                'name': result['label']['value']
            }
            output.append(output_dict)
            return output
    except Exception as e:
        print(f"An error occurred: {e}")
        return []

def run_wasteType_query(wikidata_id):
    endpoint_url = "https://query.wikidata.org/sparql"
    query = f"""
            PREFIX schema: <http://schema.org/>
            SELECT ?description
            WHERE {{
              wd:{wikidata_id} schema:description ?description.
              FILTER(LANG(?description) = "en")
            }}
            LIMIT 1
            """
    try:
        response = requests.get(endpoint_url, params={'query': query, 'format': 'json'})
        data = response.json()
        results = data['results']['bindings']
        if results:
            output = results[0]['description']['value']
            return output
        else:
            return "No description found"
    except Exception as e:
        print(f"An error occurred: {e}")
        return "An error occurred while fetching the description"

def fetch_waste_type_distribution(waste_type="CDW", year="2021", month="1"):
    """
    Fetches distribution data of a specific waste type (default: CDW) across all districts for a specified year (default: 2021).

    :param waste_type: The waste type to fetch data for, default is 'CDW'.
    :param year: The year for which the data is to be fetched, default is '2021'.
    """
    g = rdflib.Graph()
    g.parse("data/rdf-with-links.ttl", format="ttl")

    query = f"""
        PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
        PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
        PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
        PREFIX owl: <http://www.w3.org/2002/07/owl#>
        PREFIX dbo: <http://dbpedia.org/ontology/>
        PREFIX nso: <http://madridwastemanagement.org/group01/ontology/>
        PREFIX wst: <http://www.disit.org/km4city/schema#>
        PREFIX time: <http://www.w3.org/2006/time#>

        SELECT ?districtName ?amount
        WHERE {{
            ?district a dbo:District ;
                      rdfs:label ?districtName ;
                      wst:hasResidue ?wasteinstance.
            ?wasteinstance rdfs:label "{waste_type}" ;
                           nso:hasTotal ?total.
            ?total nso:value ?amount ;
                   time:year "{year}"^^xsd:gYear;
                   time:month  "{month}"^^xsd:gMonth .
        }}
        """
    print(f"Fetching distribution data for waste type: {waste_type}, year: {year}, month: {month}")
    results = g.query(query)
    distribution_data = []
    for row in results:
        distribution_data.append({
            "districtName": str(row[0]),
            "amount": float(row[1])
        })
    print(f"Distribution Data: {distribution_data}")
    return distribution_data

#######################################################################################################################################################
#######################################################################################################################################################
########################################################## Rooter ######### ###############################################33###########################
#######################################################################################################################################################

@app.route('/')
def index():
    initial_year = "2021"  # You can change this to the current year or any default year
    waste_data = fetch_yearly_district_waste(initial_year)
    return render_template('index.html', waste_data=waste_data)

@app.route('/updateIndexYear')
def update_year():
    selected_year = request.args.get('year', '2021')
    print(selected_year)
    waste_data = fetch_yearly_district_waste(selected_year)
    return render_template('index.html', waste_data=waste_data, selected_year=selected_year)

@app.route("/district")
def district():
    district_name = request.args.get("name")
    year = request.args.get("year", "2021")
    district_id = request.args.get("wikidataID")
    waste_results = run_sparql_query(district_name, year)
    district_details = run_district_details_query(district_id)
    return render_template("district.html", waste_results=waste_results, district_details=district_details,
                           selected_year=year)

@app.route("/wasteType")
def waste_type():
    wikidata_id = request.args.get("wikidata_id")
    waste_type_name = request.args.get("name", "Unknown Waste Type")
    formatted_waste_type_name = format_waste_type(waste_type_name)
    query_format_waste_type_name = reverse_format_waste_type(waste_type_name)
    waste_type_result = run_wasteType_query(wikidata_id)
    distribution_data = fetch_waste_type_distribution(query_format_waste_type_name)
    return render_template("wasteType.html", wasteType=formatted_waste_type_name, result=waste_type_result,
                           distribution=distribution_data)

@app.route("/updateWasteType")
def update_wasteType():
    wikidata_id = request.args.get("wikidata_id")
    waste_type_name = request.args.get("name", "Unknown Waste Type")
    year = request.args.get("year", "2021")
    month = request.args.get("month", "1")
    formatted_waste_type_name = format_waste_type(waste_type_name)
    query_format_waste_type_name = reverse_format_waste_type(waste_type_name)
    waste_type_result = run_wasteType_query(wikidata_id)
    distribution_data = fetch_waste_type_distribution(query_format_waste_type_name, year, month)
    return render_template("wasteType.html", wasteType=formatted_waste_type_name, result=waste_type_result,
                           distribution=distribution_data, year=year, month=month)

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


from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.index),
    url(r'^login$', views.user_login),
    url(r'^register$', views.user_register),
    url(r'^logout$', views.user_logout),
    url(r'^todo$', views.todo_operation),
    url(r'^yourtodo$', views.your_todo_operation),
    url(r'^task/(?P<tid>[0-9]+)$', views.single_task_operation),
    url(r'^statuschange/(?P<tid>[0-9]+)$$', views.change_status),
    url(r'^error$', views.error)
]


import numpy as np

from tree.descision_tree import DecisionTree, SimpleDecisionRule

class ValuesToBins:
    def __init__(self, x: np.ndarray, n_bins: int):
        assert n_bins < 256
        self._x = x
        self._max_values = self._x.max(axis=0)
        self._n_bins = n_bins
        self._quantiles = self._calculate_quantiles()
        assert self._quantiles.shape == (x.shape[1], n_bins)

    def _calculate_quantiles(self):
        return np.array([np.sort(np.percentile(v,
                              100 * np.arange(self._n_bins) / self._n_bins))
                         for v in self._x.T])

    def get_bins(self, x):
        assert x.shape[1] == self._x.shape[1]
        return np.array([np.digitize(x[:, i], self._quantiles[i], True) for i in range(self._x.shape[1])],
                        dtype=np.uint8).T

    def convert_to_values_rule(self, bins_rule: SimpleDecisionRule) -> SimpleDecisionRule:
        i = bins_rule.get_i()
        if bins_rule.get_bound() == self._n_bins:
            bound = self._max_values[i]
        else:
            bound = self._quantiles[i, bins_rule.get_bound()]
        return SimpleDecisionRule(bound, i)

    def convert_bins_tree_to_prediction_tree(self, tree_trained_on_bins: DecisionTree) -> DecisionTree:
        converted_root = tree_trained_on_bins.root().convert(self.convert_to_values_rule)
        return DecisionTree(converted_root)

    def bins_counts(self):
        return [len(set(self._quantiles[i])) for i in range(self._quantiles.shape[0])]


from tqdm import tqdm
import re
import time
import cProfile

from src.naive_sudoku_solver import read_and_solve_sudoku_from_string as naive_solver
from src.OOP_sudoku import read_and_solve_sudoku_from_string as oop_solver
from src.OOP_sudoku_improved import (
    read_and_solve_sudoku_from_string as improved_oop_solver,
)
from src.numpy_sudoku import read_and_solve_sudoku_from_string as numpy_solver

from src.CLI_C_caller import read_and_solve_sudoku_from_string as convoluted_C_solver
from src.CLI_C_caller import run_C_benchmark

try:
    from RamiSudoku import sudoku_solver as modulated_C_solver
except ImportError as e:
    modulated_C_solver = None
    print(f"Failed to import sudoku C-module due to {e}.")

with open("./data/10k_sudokus.csv", "r") as fp:
    benchmark_data_10k = fp.readlines()

def run_benchmark(algo_to_use, title, data=benchmark_data_10k):
    print(80 * "*")
    print(f"Running benchmark for {title}: ")

    try:
        tick = time.time()
        success_count = 0
        for line in tqdm(data):
            puzzle, solved = line.strip("\n ").split(",")
            result = algo_to_use(puzzle)
            if result == solved:
                success_count += 1
        tock = time.time()

        print(f"{title} done!")
        print(
            f"Success count: {success_count:>8}/{len(data):>8} ({success_count/len(data):>3.1%})"
        )
        print(
            f"Time: {tock-tick:>8.2} seconds for {len(data)} data. ({(tock-tick)/len(data):>1.1e} secs per sudoku)"
        )
    except Exception as e:
        print(f"\nFailed in running benchmark for {title}.")

def run_C_benchmark_wrapper(title):
    test_data_filepath = "./data/10k_sudokus.csv"
    print(80 * "*")
    print(f"Running benchmark for {title}: ")

    tick = time.time()
    output = run_C_benchmark(test_data_filepath)
    tock = time.time()

    matches = re.search("[\s]+([\d]+)[\s]*\/[\s]*([\d]+)", output)
    success_count = int(matches[1])
    total = int(matches[2])

    print(f"{title} done!")
    print(f"Success count: {success_count:>8}/{total:>8} ({success_count/total:>3.1%})")
    print(
        f"Time: {tock-tick:>8.2} seconds for {total} data. ({(tock-tick)/total:>1.1e} secs per sudoku)"
    )

if __name__ == "__main__":
    run_benchmark(naive_solver, "Naive solver")
    run_benchmark(oop_solver, "OOP solver")
    run_benchmark(convoluted_C_solver, "Convoluted C-solver")
    run_C_benchmark_wrapper("Wrapped C-Benchmark tool")
    run_benchmark(improved_oop_solver, "Improved OOP solver")
    # run_benchmark(numpy_solver, "Numpy solver")
    run_benchmark(modulated_C_solver, "Modulated C-solver")


import random

def train_test_split(X: np.array, y: np.array, ratio: int, seed=None):
    # ratio must be smaller than 1, bigger than 0
    # return error if n == 0

    # returns (train_features, train_labels), (test_features, test_labels)

    n = int(ratio * len(X))
    if n < 1:
        raise ValueError("ratio too small compared to the data size (cannot split)")

    # selecting random indices
    if seed:
        random.seed(seed)
    indices = random.sample(range(0, len(X)), n)

    # mark the selected indices
    marks = [False] * (len(X))
    for idx in indices:
        marks[idx] = True

    # select items specified by marks
    X_train_data, X_test_data = np.array([]), np.array([])
    y_train_data, y_test_data = np.array([]), np.array([])

    for idx in range(len(marks)):
        if marks[idx]:
            X_train_data = np.append(X[idx], X_train_data)
            y_train_data = np.append(y[idx], y_train_data)
        else:
            X_test_data = np.append(X[idx], X_test_data)
            y_test_data = np.append(y[idx], y_test_data)

    return X_train_data, X_test_data, y_train_data, y_test_data


import torch.nn as nn
import torch
import torch.nn.functional as F

class MultipleLoss(nn.Module):
    def __init__(self, smooth):
        super(MultipleLoss, self).__init__()
        self.smooth = smooth

    def forward(self, outputs, targets):
        ce_loss = F.cross_entropy(outputs, targets)

        # dice loss
        outputs = torch.softmax(outputs, dim=1)
        target_one_hot = F.one_hot(targets, num_classes=outputs.shape[1]).permute(0,3,1,2).float()

        numerator = 2 * torch.sum(outputs * target_one_hot) + self.smooth
        denominator = torch.sum(outputs ** 2) + torch.sum(target_one_hot ** 2) + self.smooth
        soft_dice_loss = 1 - numerator / denominator

        return soft_dice_loss + ce_loss


from __future__ import print_function, absolute_import, unicode_literals

import logging

from osbs.constants import USER_WARNING_LEVEL, USER_WARNING_LEVEL_NAME
from osbs.utils import user_warning_log_handler
from osbs.version import __version__  # noqa

def set_logging(name="osbs", level=logging.DEBUG):
    # add new level to loggers
    logging.addLevelName(USER_WARNING_LEVEL, USER_WARNING_LEVEL_NAME)
    logging.Logger.user_warning = user_warning_log_handler

    # create logger
    logger = logging.getLogger(name)
    logger.handlers = []
    logger.setLevel(level)

    # create console handler and set level to debug
    ch = logging.StreamHandler()
    ch.setLevel(logging.DEBUG)

    # create formatter
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

    # add formatter to ch
    ch.setFormatter(formatter)

    # add ch to logger
    logger.addHandler(ch)

set_logging(level=logging.WARNING)  # override this however you want


# Calculates the value of D ≤ 1000 in minimal solutions of x for which the largest value of x is obtained

from math import sqrt
from fractions import Fraction

def isSquare(n):
    return int(sqrt(n)+.5)**2 == n

def continuedFraction(array):
	if len(array) != 2:
		return array[0] + Fraction(1,continuedFraction(array[1:]))
	else:
		return array[0] + Fraction(1,array[1])

def Diophantine(x,y,D):
	return x**2 - D * y**2 == 1

cache = {}
for i in range(1,1001):
	if not isSquare(i):
		n = i
		residue = int(n ** 0.5)
		r = residue
		array = []
		denominator = residue
		numerator = n - denominator **2

		while True:
			newarray = int((residue+denominator)/numerator)
			array.append( newarray )
			newDenominator = (newarray * numerator) - denominator
			newNumerator = (n - newDenominator **2)/numerator

			if newNumerator == 1:
				break
			denominator = newDenominator
			numerator = newNumerator

		array.append(2*residue)
		if len(array) == 2 and array[0] == array[1]:
			del array[0]
		cache[i] = [r] + array * 2

max = 0
for i in cache:
	index = 2
	while True:
		fraction = continuedFraction(cache[i][:index])
		if Diophantine(fraction.numerator,fraction.denominator,i):
			break
		else:
			index += 1
	if fraction.numerator > max:
		max = fraction.numerator
		value = i
print(value)


# Same code for BotClean Stochastic,BotClean Large

#function
def next_move(posr, posc, board):
    import math
    location_d = list()

    for index,row in enumerate(board):
        for col,inrow in enumerate(row):
            if inrow == 'd':
                 location_d.append([index,col])

    bot_pos = [posr,posc]

    all_distance = list()
    for i in range(len(location_d)):
        #manhattan distance
        distance = int(abs(location_d[i][0] - bot_pos[0]) + abs(location_d[i][1] - bot_pos[1]))
        #euclidean distance
        #distance = math.sqrt(((location_d[i][0] - bot_pos[0])**2) + ((location_d[i][1] - bot_pos[1])**2))
        all_distance.append(distance)

    sortclose_d = [x for (y,x) in sorted(zip(all_distance,location_d))]

    if sortclose_d[0][0] < bot_pos[0]:
        print('UP')
    elif sortclose_d[0][0] > bot_pos[0]:
        print('DOWN')
    elif sortclose_d[0][1] > bot_pos[1]:
        print('RIGHT')
    elif sortclose_d[0][1] < bot_pos[1]:
        print('LEFT')
    else:
        print('CLEAN')

#input
pos = [int(i) for i in input().strip().split()]
board = [[j for j in input().strip()] for i in range(5)]

#function
next_move(pos[0], pos[1], board)


# -*- coding: UTF-8 -*-

"""
Policy Gradient 算法（REINFORCE）。做决策的部分，相当于机器人的大脑
"""

import numpy as np
import tensorflow as tf

try:
    xrange = xrange  # Python 2
except:
    xrange = range   # Python 3

# 策略梯度 类
class PolicyGradient:
    def __init__(self,
                 lr,      # 学习速率
                 s_size,  # state/observation 的特征数目
                 a_size,  # action 的数目
                 h_size,  # hidden layer（隐藏层）神经元数目
                 discount_factor=0.99  # 折扣因子
    ):
        self.gamma = discount_factor  # Reward 递减率

        # 神经网络的前向传播部分。大脑根据 state 来选 action
        self.state_in = tf.placeholder(shape=[None, s_size], dtype=tf.float32)

        # 第一层全连接层
        hidden = tf.layers.dense(self.state_in, h_size, activation=tf.nn.relu)

        # 第二层全连接层，用 Softmax 来算概率
        self.output = tf.layers.dense(hidden, a_size, activation=tf.nn.softmax)

        # 直接选择概率最大的那个 action
        self.chosen_action = tf.argmax(self.output, 1)

        # 下面主要是负责训练的一些过程
        # 我们给神经网络传递 reward 和 action，为了计算 loss
        # 再用 loss 来调节神经网络的参数
        self.reward_holder = tf.placeholder(shape=[None], dtype=tf.float32)
        self.action_holder = tf.placeholder(shape=[None], dtype=tf.int32)

        self.indexes = tf.range(0, tf.shape(self.output)[0]) * tf.shape(self.output)[1] + self.action_holder
        self.outputs = tf.gather(tf.reshape(self.output, [-1]), self.indexes)

        # 计算 loss（和平时说的 loss 不一样）有一个负号
        # 因为 TensorFlow 自带的梯度下降只能 minimize（最小化）loss
        # 而 Policy Gradient 里面是要让这个所谓的 loss 最大化
        # 因此需要反一下。对负的去让它最小化，就是让它正向最大化
        self.loss = -tf.reduce_mean(tf.log(self.outputs) * self.reward_holder)

        # 得到可被训练的变量
        train_vars = tf.trainable_variables()

        self.gradient_holders = []

        for index, var in enumerate(train_vars):
            placeholder = tf.placeholder(tf.float32, name=str(index) + '_holder')
            self.gradient_holders.append(placeholder)

        # 对 loss 以 train_vars 来计算梯度
        self.gradients = tf.gradients(self.loss, train_vars)

        optimizer = tf.train.AdamOptimizer(learning_rate=lr)
        # apply_gradients 是 minimize 方法的第二部分，应用梯度
        self.update_batch = optimizer.apply_gradients(zip(self.gradient_holders, train_vars))

    # 计算折扣后的 reward
    # 公式： E = r1 + r2 * gamma + r3 * gamma * gamma + r4 * gamma * gamma * gamma ...
    def discount_rewards(self, rewards):
        discounted_r = np.zeros_like(rewards)
        running_add = 0
        for t in reversed(xrange(0, rewards.size)):
            running_add = running_add * self.gamma + rewards[t]
            discounted_r[t] = running_add
        return discounted_r


import pandas as pd
import numpy as np

def clean_df(df: pd.DataFrame) -> pd.DataFrame:
    '''
    Function to clean a pandas DataFrame:
    * right data types
    * empty values for float and int columns

    Parameters:
    * data frame

    Returns:
    * cleaned data frame

    '''

    # Convert to the right data types
    df['aantal_cilinders'] = df['aantal_cilinders'].astype(float)
    df['catalogusprijs'] = df['catalogusprijs'].astype(float)

    # lege waarden
    df['aantal_cilinders'] = df['aantal_cilinders'].fillna(0)

    df['aantal_cilinders'] = np.where(np.isnan(df['aantal_cilinders']), # evaluatie/vergelijking
                                     float(0.0),                     # als True de uitkomst is --> Vervang met
                                     df['aantal_cilinders'] )  # als False de uitkomst is --> Behoud originele waarde

    df['catalogusprijs'] = np.where(np.isnan(df['catalogusprijs']), # evaluatie/vergelijking
                                     float(0.0),                     # als True de uitkomst is --> Vervang met
                                     df['catalogusprijs'] )  # als False de uitkomst is --> Behoud originele waarde

    #
    df['aantal_cilinders'] = df['aantal_cilinders'].astype(int)

    df_filtered = df.query("catalogusprijs > 0")

    # calculated column aanmaken
    df_filtered['prijs_per_cilinder'] = df_filtered['catalogusprijs'] /  df_filtered['aantal_cilinders']

    # sorteren van een dataframe
    df_filtered.sort_values(by="prijs_per_cilinder", ascending=False)

    return df_filtered

'''

# Overzicht van DataFrame:
* df.info() --> Technische informatie over de DataFrame

# Kolommen selecteren in een DataFrame
df['kolom_naam']

# Beschrijving
* df['aantal_cilinders'].unique()
* df['aantal_cilinders'].value_counts()

# filters, only cars with a price higher than 0
exclude_colors = ['N.v.t.', 'Niet geregistreerd']
df_filtered = df.query("catalogusprijs > 0 \
                        & aantal_cilinders == 4 \
                        & eerste_kleur != @exclude_colors")
'''


# -*- coding: utf-8 -*-

# by digiteng...(digiteng@gmail.com)
# https://github.com/digiteng/
# 06.2020 - 11.2020(v2.0) - 11.2021(v3.x)
from __future__ import absolute_import
from Plugins.Plugin import PluginDescriptor
from Components.config import config
from threading import Timer
from datetime import datetime
from six.moves import reload_module
from . import xtra
from . import download
from enigma import addFont
from Tools.Directories import resolveFilename, SCOPE_FONTS

addFont(resolveFilename(SCOPE_FONTS, "DejaVuSans.ttf"), "xtraRegular", 100, 1)

with open("/var/lib/opkg/info/enigma2-plugin-extensions-xtraevent.control") as origin:
	for version in origin:
		if not "Version: " in version:
			continue
		try:
			versionnumber = version.split('+')[1]
		except IndexError:
			print("[xtraEvent] can't detect version!")

try:
	if config.plugins.xtraEvent.timerMod.value == "Clock":
		tc = config.plugins.xtraEvent.timerClock.value
		dt = datetime.today()
		setclk = dt.replace(day=dt.day + 1, hour=tc[0], minute=tc[1], second=0, microsecond=0)
		ds = setclk - dt
		secs = ds.seconds + 1

		def startDownload():
			from . import download
			download.downloads("").save()

		t = Timer(secs, startDownload)
		t.start()

except Exception as err:
	with open("/tmp/xtraEvent.log", "a+") as f:
		f.write("plugin timer clock, %s\n" % (err))

def ddwn():
	try:
		if config.plugins.xtraEvent.timerMod.value == "Period":
			download.downloads("").save()
			tmr = config.plugins.xtraEvent.timer.value
			t = Timer(3600 * int(tmr), ddwn) # 1h=3600
			t.start()
	except Exception as err:
		with open("/tmp/xtra_error.log", "a+") as f:
			f.write("xtra plugin ddwn, %s\n\n" % err)

try:
	if config.plugins.xtraEvent.timerMod.value == "Period":
		Timer(30, ddwn).start()
except Exception as err:
	with open("/tmp/xtra_error.log", "a+") as f:
		f.write("xtra plugin timer start, %s\n\n" % err)

def main(session, **kwargs):
	try:
		reload_module(xtra)
		reload_module(download)
		session.open(xtra.xtra)
	except:
		import traceback
		traceback.print_exc()

def Plugins(**kwargs):
	return [PluginDescriptor(name=_("xtraEvent"), description=_("Poster, backdrop, banner, info and more") + " git-" + versionnumber, where=PluginDescriptor.WHERE_PLUGINMENU, icon="plugin.png", fnc=main)]


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='ContactBook',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('email', models.CharField(unique=True, max_length=50)),
                ('info', models.TextField()),
                ('created_on', models.DateTimeField(auto_now_add=True)),
                ('modified_on', models.DateTimeField(auto_now=True)),
            ],
        ),
        migrations.CreateModel(
            name='UserProfile',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('name', models.CharField(max_length=50)),
                ('email', models.CharField(unique=True, max_length=50)),
                ('created_on', models.DateTimeField(auto_now_add=True)),
                ('modified_on', models.DateTimeField(auto_now=True)),
            ],
        ),
        migrations.AddField(
            model_name='contactbook',
            name='created_by',
            field=models.ForeignKey(related_name='+', to='datamodel.UserProfile'),
        ),
    ]


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import ctypes
import json
import sys
sys.path.append('./')
from queue import Queue
from threading import Event, Thread
from typing import Dict, Iterable, List

import cv2
import numpy as np
import torch

import vacl_stream
import vaststream
from vaststream import *

class StreamInfo(ctypes.Structure):
    _fields_ = [
                ("stream_id", ctypes.c_uint32),
                ("device_id", ctypes.c_uint32),
                ("model_op_context", ctypes.c_uint32),
                ("model_name", ctypes.c_wchar_p)
                ]

class Bert:
    def __init__(
        self,
        model_info: str,
        bytes_size: int = 1536,
        device_id: int = 0,
        batch_size: int = 1,
        **kwargs
        ):
        # get model path and hw_config path
        if isinstance(model_info, str):
            with open(model_info) as f:
                model_info = json.load(f)

        self.vacl_stream = vacl_stream.VaclStream()
        self.input_id = 0
        self.input_dict = {}
        self.event_dict = {}
        self.result_dict = {}
        self.balance_mode = 0
        self.device_id = device_id
        self.batch_size = batch_size
        self.bytes_size = bytes_size
        self.vast_stream = vast_stream()

        self.vast_stream.init(self.device_id)
        init_context, stream_id = self.vast_stream.create_stream(self.device_id, self.balance_mode)
        assert stream_id != 0, 'Fail to create stream.'

        self.stream_id = stream_id
        self.init_op_context = init_context

        def callback(output_description, ulOutPointerArray, ulArraySize, user_data_ptr):
            user_data = ctypes.cast(user_data_ptr, ctypes.POINTER(vacl_stream.StreamInfo))
            input_id = output_description.contents.input_id
            device_ddr = self.input_dict.pop(input_id)
            if isinstance(device_ddr, list):
                for ddr in device_ddr:
                    free_reuslt = self.vast_stream.free_data_on_device(ddr, self.device_id)
                    if free_reuslt != 0:
                        raise "free error.\n"
            else:
                self.vast_stream.free_data_on_device(device_ddr, self.device_id)

            model_name = user_data.contents.model_name
            stream_output_list = self._get_stream_output(ulOutPointerArray, ulArraySize)
            heatmap = np.squeeze(stream_output_list[0])
            _, shape = self.vast_stream.get_output_shape_by_index(model_name, 0)
            ndims = shape.ndims
            heatmap_shape = []
            for i in range(ndims):
                heatmap_shape.append(shape.shapes[i])
            self.result_dict[input_id] = (heatmap_shape, heatmap)
            self.event_dict[input_id].set()
            print("callback count:", input_id)

        # create stream and build model
        op_context_id = self.vast_stream.create_stream_op(self.stream_id, 20001)
        self.vast_stream.invoke_stream_op(op_context_id, self.init_op_context)
        self.vdsp_op_context = op_context_id
        model_name = self._register_model_info(model_info)
        self.model_name = model_name
        model_op_context = self.vast_stream.invoke_run_model_op(self.stream_id, self.vdsp_op_context, model_name)
        self.model_op_context = model_op_context
        self.vast_stream.build_stream(self.stream_id)
        self.callback_output = vaststream.output_callback_type(callback)
        self.user_data = StreamInfo(self.stream_id, self.device_id, self.model_op_context, self.model_name)
        user_data_ptr = cast(POINTER(StreamInfo)(self.user_data), c_void_p)
        self.vast_stream.subscribe_device_report(self.stream_id, self.callback_output, user_data_ptr)

    def _register_model_info(self, model_info_dict: Dict):
        assert self.stream_id != 0
        model_name = model_info_dict.get('model_name')
        model_hw_cfg_file = model_info_dict.get('hw_config_file')
        model_dynamic_json = model_info_dict.get('model_dynamic_json')

        # dynamic model description has higher priority here.
        if model_dynamic_json != None:
            if self.vast_stream.register_dynamic_model(model_name, model_dynamic_json, self.device_id, model_hw_cfg_file, self.batch_size):
                raise ValueError("Failed to register dynamic model.")
        else:
            model_lib_path = model_info_dict.get('model_lib_path')
            model_graph_path = model_info_dict.get('model_graph_path')
            model_params_path = model_info_dict.get('model_params_path')
            if self.vast_stream.register_static_model(model_name, model_lib_path, model_graph_path, model_params_path, self.device_id, model_hw_cfg_file, self.batch_size):
                raise ValueError("Failed to register model.")

        return model_name

    def _get_stream_output(self, ulOutPointerArray, ulArraySize, **kwargs):
        output_count_per_batch = self.vast_stream.get_output_num_per_batch(self.model_name)
        assert output_count_per_batch != 0
        count = int(ulArraySize / output_count_per_batch)
        result_list = []
        index = 0
        for i in range(count):
            data_list = []
            for j in range(output_count_per_batch):
                size = self.vast_stream.get_output_size_by_index(self.model_name, j) # bytes
                data = np.zeros(int(size / 2), dtype=np.float16) # fp16
                output_data = data.ctypes.data_as(ctypes.c_void_p)
                self.vast_stream.get_model_output(self.model_name, j, c_uint64(ulOutPointerArray[index]), output_data)
                data_list.append(data)
                index += 1
            result_list.append(data_list)
        return result_list

    def _copy_to_device(self, data, bytes_size):
        host_addr = data.ctypes.data_as(ctypes.c_void_p)
        device_addr = self.vast_stream.copy_data_to_device(host_addr, bytes_size, self.device_id)
        return device_addr

    def _run_stream(self, inputs: List[np.ndarray]):
        # copy data to device and get input addr on device
        addrs_list = [self._copy_to_device(input, self.bytes_size) for input in inputs]
        data_array_size = len(addrs_list)
        data_array = (c_uint64 * data_array_size) (*addrs_list)
        input_id = self.input_id
        self.input_dict[input_id] = addrs_list
        self.event_dict[input_id] = Event()
        print(" Testing:", input_id, end=',')

        # run model
        ret = self.vast_stream.run_stream(self.stream_id, self.input_id, data_array, len(addrs_list))
        self.input_id += 1
        return input_id

    def run(self, inputs: List[np.ndarray]):
        inputs_len = len(inputs)
        if inputs_len < 1:
            raise ValueError('The number of inputs should be greater than 0.')

        input_id = self._run_stream(inputs)
        self.event_dict[input_id].wait()
        result = self.result_dict.pop(input_id)
        del self.event_dict[input_id]
        return result

    def run_batch(self, datasets: Iterable[List[np.ndarray]]):
        queue = Queue(20)

        def input_thread():
            for data in datasets:
                input_id = self._run_stream(data)
                queue.put(input_id)
            queue.put(None)

        thread = Thread(target=input_thread)
        thread.start()

        while True:
            input_id = queue.get()
            if input_id is None:
                break
            self.event_dict[input_id].wait()
            result = self.result_dict.pop(input_id)
            del self.event_dict[input_id]
            yield result


import tkinter as tk

window = tk.Tk()
window.title('my window')
##窗口尺寸
window.geometry('200x200')
#新建一个label
l=tk.Label(window,text='',bg='yellow')
l.pack()
#计数
counter = 0
def do_job():
    global counter
    l.config(text='do '+ str(counter))
    counter+=1
#创建菜单
menubar=tk.Menu(window)
#菜单一
filemenu=tk.Menu(menubar,tearoff=0)
#一级菜单
menubar.add_cascade(label='File',menu=filemenu)
#二级菜单
filemenu.add_command(label='New',command=do_job)
filemenu.add_command(label='Open',command=do_job)
filemenu.add_separator()#分割线
filemenu.add_command(label='Exit',command=window.quit)
#菜单二
editmenu=tk.Menu(menubar,tearoff=0)
#一级菜单
menubar.add_cascade(label='Edit',menu=editmenu)
#二级菜单
editmenu.add_command(label='Cut',command=do_job)
editmenu.add_command(label='Paste',command=do_job)
#
#菜单一子菜单
submenu=tk.Menu(filemenu)
#一级菜单
filemenu.add_cascade(label='Import',menu=submenu,underline=0)
#二级菜单
submenu.add_command(label='Submeau1',command=do_job)
submenu.add_command(label='Submeau1',command=do_job)

window.config(menu=menubar)
##显示出来
window.mainloop()


from typing import Tuple

import tensorflow as tf
from dependency_injector.providers import Callable

class PdfNormalization(Callable):
    """
    https://stats.stackexchange.com/questions/66616/converting-normalizing-very-small-likelihood-values-to-probability
    """

    def __init__(self, features_cnt: int, precision=16):
        self.pdf_in, self.normalize_pdf = self.__build_model(features_cnt, precision)

    def __build_model(self, features_cnt, precision=16):
        pdf_in, x_out = self.__transform_input(features_cnt)
        threshold = self.__calculate_threshold(precision, features_cnt)
        x_out = self.__normalize(x_out, threshold, features_cnt)

        return pdf_in, x_out

    def __transform_input(self, features_cnt: Tuple[int, int]):
        pdf_in = tf.placeholder(tf.float64, (None, features_cnt), name="pdf")
        ln = tf.log(pdf_in, "log")
        max_ln = tf.reshape(tf.reduce_max(ln, 1, name="max_pdf"), (-1, 1))
        x_out = tf.subtract(ln, max_ln, "sub")

        return pdf_in, x_out

    def __calculate_threshold(self, precision: int, feats_cnt: int):
        epsilon = tf.constant(10 ^ -precision, dtype=tf.float64, name="epsilon")
        n = tf.constant(feats_cnt, tf.float64, name="n")
        threshold = tf.log(epsilon / n, "threshold")

        return threshold

    def __normalize(self, x_out, threshold, features_cnt: int):
        x_out = tf.exp(x_out, "exp")

        def replace_with_zero_values_below_threshold(x_row):
            less_than_threshold = tf.less(x_row, threshold)
            return tf.where(less_than_threshold, tf.zeros((features_cnt,), dtype=tf.float64), x_row)

        x_out = tf.map_fn(replace_with_zero_values_below_threshold, x_out)
        x_sum = tf.reshape(tf.reduce_sum(x_out, 1, name="sum"), (-1, 1))
        x_out = tf.divide(x_out, x_sum, name="divide")

        return x_out


#
# @lc app=leetcode.cn id=85 lang=python3
#
# [85] 最大矩形
#

# @lc code=start
# 84. 柱状图中最大的矩形
# https://leetcode-cn.com/problems/number-of-atoms/solution/python-dai-ma-qing-xi-by-hardcandy-b9qv/
class Solution:
    def maximalRectangle(self, matrix):
        if not matrix:
            return 0
        rows, cols = len(matrix), len(matrix[0])
        heights = [0] * cols
        maxarea = 0
        for j in range(rows):
            for i in range(cols):
                if matrix[j][i] == '1':
                    heights[i] += 1
                else:
                    heights[i] = 0
            maxarea = max(maxarea, self.largestRectangleArea(heights))

        return maxarea

    def largestRectangleArea(self, heights):
        stack = [(-1,-1)]
        maxarea = 0
        for i, height in enumerate(heights):
            while height <= stack[-1][1]:
                _, height_ = stack.pop()
                area = height_ * (i - stack[-1][0] - 1)
                maxarea = max(area, maxarea)
            stack.append((i, height))

        stack.append((stack[-1][0]+1, -1))
        for idx,(_,height) in enumerate(stack):
            if height >=0 :
                area = height * (stack[-1][0] - stack[idx-1][0] -1)
                maxarea = max(area, maxarea)
        return maxarea
# @lc code=end

matrix = [["0","1"],["1","0"]]
sol = Solution()
print(sol.maximalRectangle(matrix))


# -*- coding: utf-8 -*-
# @Author: Cody Kochmann
# @Date:   2017-05-05 16:25:49
# @Last Modified 2017-10-05e>
# @Last Modified time: 2017-12-09 13:24:12

from __future__ import print_function
del print_function
from strict_functions import noglobals

@noglobals
def tee(pipeline, name, output_function=print):
    for i in pipeline:
        output_function('{} - {}'.format(name,i))
        yield i

del noglobals

if __name__ == '__main__':
    gen = (i for i in range(100))
    gen = tee(gen,'step1')

    gen = (i*2 for i in gen)
    gen = tee(gen,'step2')

    gen = (i**2 for i in gen)
    gen = tee(gen,'step3')

    gen = (i/2 for i in gen)
    gen = tee(gen,'step4')

    # run the generator
    print(list(i for i in gen))


#!/usr/bin/env venv/bin/python

"""Manages notifications from DBUS."""

import os
import shutil
import datetime
import json
from pathlib import Path
from dataclasses import dataclass
import dbus
from dbus.mainloop.glib import DBusGMainLoop
import gi
gi.require_version("Gtk", "3.0")
gi.require_version("GdkPixbuf", "2.0")
from gi.repository import GLib, GdkPixbuf

@dataclass
class Notification:
    """Represents a notification read from the system."""

    ntf_id: int
    title: str
    body: str
    icon: str
    app: str
    urgency: int

CACHE_PATH = Path('/tmp/notification_cache')
ntf_cache: list[Notification] = []

def get_thumbnail(path, message):
    """Save the thumbnail if bytes are sent."""
    icon_data = message[6].get('image-data')
    if icon_data:
        GdkPixbuf.Pixbuf.new_from_bytes(
            width=icon_data[0],
            height=icon_data[1],
            has_alpha=icon_data[3],
            data=GLib.Bytes(bytes(list(icon_data[6]))),
            colorspace=GdkPixbuf.Colorspace.RGB,
            rowstride=icon_data[2],
            bits_per_sample=icon_data[4],
        ).savev(bytes(path), 'png')
        return True
    return False

def get_notif(_ , message_raw):
    """Get notifications from the system."""
    if not isinstance(message_raw, dbus.lowlevel.MethodCallMessage):
        return
    message = message_raw.get_args_list()
    ntf_id = int(datetime.datetime.now().timestamp() * 1e6)
    thumbnail_path = Path(f'{CACHE_PATH}/{ntf_id}.png')
    thumbnail_status = get_thumbnail(thumbnail_path, message)
    notification = Notification(
        ntf_id=int(ntf_id),
        title=str(message[3]),
        body=str(message[4]),
        icon=str(thumbnail_path) if thumbnail_status else '',
        app=str(message[0]),
        urgency=int(message[6].get('urgency'))
    )
    # print(json.dumps(notification.__dict__), flush=True)
    while len(ntf_cache) >= 20:
        os.remove(ntf_cache[0].icon)
        ntf_cache.pop(0)
    ntf_cache.append(notification)
    # with open(f'{CACHE_PATH}/cache.json', 'w', encoding='utf-8') as file:
    #     json.dump([item.__dict__ for item in ntf_cache],
    #               file)
    ntf_complete = {
            'last': notification.__dict__,
            'cache': [item.__dict__ for item in ntf_cache]
            }
    print(json.dumps(ntf_complete), flush=True)

if __name__ == "__main__":
    shutil.rmtree(CACHE_PATH, ignore_errors=True)
    os.makedirs(CACHE_PATH)
    DBusGMainLoop(set_as_default=True)
    bus = dbus.SessionBus()
    bus.add_match_string_non_blocking("eavesdrop=true, \
                                       interface='org.freedesktop.Notifications', \
                                       member='Notify'")
    bus.add_message_filter(get_notif)
    loop = GLib.MainLoop()
    try:
        loop.run()
    except KeyboardInterrupt:
        bus.close()


from __future__ import annotations

import random
from typing import (List,
                    Tuple)

from ground.base import (Context,
                         Location,
                         Orientation)
from ground.hints import (Box,
                          Multisegment,
                          Point,
                          Polygon)
from reprit.base import generate_repr

from sect.core.utils import (contour_to_edges_endpoints,
                             to_contour_orientation)
from .edge import Edge
from .hints import Shuffler
from .leaf import Leaf
from .node import Node
from .trapezoid import Trapezoid
from .x_node import XNode
from .y_node import YNode

class Graph:
    """Represents trapezoidal decomposition graph."""

    @classmethod
    def from_multisegment(cls,
                          multisegment: Multisegment,
                          *,
                          shuffler: Shuffler = random.shuffle,
                          context: Context) -> Graph:
        """
        Constructs trapezoidal decomposition graph of given multisegment.

        Based on incremental randomized algorithm by R. Seidel.

        Time complexity:
            ``O(segments_count * log segments_count)`` expected,
            ``O(segments_count ** 2)`` worst
        Memory complexity:
            ``O(segments_count)``

        where ``segments_count = len(multisegment.segments)``

        Reference:
            https://doi.org/10.1016%2F0925-7721%2891%2990012-4
            https://www.cs.princeton.edu/courses/archive/fall05/cos528/handouts/A%20Simple%20and%20fast.pdf

        :param multisegment: target multisegment.
        :param shuffler:
            function which mutates sequence by shuffling its elements,
            required for randomization.
        :param context: geometric context.
        :returns: trapezoidal decomposition graph of the multisegment.

        >>> from ground.base import get_context
        >>> context = get_context()
        >>> Multisegment, Point, Segment = (context.multisegment_cls,
        ...                                 context.point_cls,
        ...                                 context.segment_cls)
        >>> graph = Graph.from_multisegment(
        ...     Multisegment([Segment(Point(0, 0), Point(1, 0)),
        ...                   Segment(Point(0, 0), Point(0, 1))]),
        ...     context=context
        ... )
        >>> Point(1, 0) in graph
        True
        >>> Point(0, 1) in graph
        True
        >>> Point(1, 1) in graph
        False
        >>> graph.locate(Point(1, 0)) is Location.BOUNDARY
        True
        >>> graph.locate(Point(0, 1)) is Location.BOUNDARY
        True
        >>> graph.locate(Point(1, 1)) is Location.EXTERIOR
        True
        """
        edges = [
            Edge.from_endpoints(segment.start, segment.end, False, context)
            if segment.start < segment.end
            else Edge.from_endpoints(segment.end, segment.start, False,
                                     context)
            for segment in multisegment.segments
        ]
        return cls._from_box_with_edges(
                context.segments_box(multisegment.segments), edges, shuffler,
                context
        )

    @classmethod
    def from_polygon(cls,
                     polygon: Polygon,
                     *,
                     shuffler: Shuffler = random.shuffle,
                     context: Context) -> Graph:
        """
        Constructs trapezoidal decomposition graph of given polygon.

        Based on incremental randomized algorithm by R. Seidel.

        Time complexity:
            ``O(vertices_count * log vertices_count)`` expected,
            ``O(vertices_count ** 2)`` worst
        Memory complexity:
            ``O(vertices_count)``

        where

            .. code-block:: python

                vertices_count = (len(polygon.border.vertices)
                                  + sum(len(hole.vertices)
                                        for hole in polygon.holes)
                                  + len(extra_points) + len(extra_constraints))

        Reference:
            https://doi.org/10.1016%2F0925-7721%2891%2990012-4
            https://www.cs.princeton.edu/courses/archive/fall05/cos528/handouts/A%20Simple%20and%20fast.pdf

        :param polygon: target polygon.
        :param shuffler:
            function which mutates sequence by shuffling its elements,
            required for randomization.
        :param context: geometric context.
        :returns: trapezoidal decomposition graph of the border and holes.

        >>> from ground.base import get_context
        >>> context = get_context()
        >>> Contour, Point, Polygon = (context.contour_cls, context.point_cls,
        ...                            context.polygon_cls)
        >>> graph = Graph.from_polygon(
        ...     Polygon(Contour([Point(0, 0), Point(6, 0), Point(6, 6),
        ...                      Point(0, 6)]),
        ...             [Contour([Point(2, 2), Point(2, 4), Point(4, 4),
        ...                       Point(4, 2)])]),
        ...     context=context
        ... )
        >>> Point(1, 1) in graph
        True
        >>> Point(2, 2) in graph
        True
        >>> Point(3, 3) in graph
        False
        >>> graph.locate(Point(1, 1)) is Location.INTERIOR
        True
        >>> graph.locate(Point(2, 2)) is Location.BOUNDARY
        True
        >>> graph.locate(Point(3, 3)) is Location.EXTERIOR
        True
        """
        border = polygon.border
        orienteer = context.angle_orientation
        is_border_positively_oriented = (to_contour_orientation(border,
                                                                orienteer)
                                         is Orientation.COUNTERCLOCKWISE)
        edges = [
            Edge.from_endpoints(start, end, is_border_positively_oriented,
                                context)
            if start < end
            else Edge.from_endpoints(end, start,
                                     not is_border_positively_oriented,
                                     context)
            for start, end in contour_to_edges_endpoints(border)
        ]
        for hole in polygon.holes:
            is_hole_negatively_oriented = (
                    to_contour_orientation(hole, orienteer)
                    is Orientation.CLOCKWISE
            )
            edges.extend(
                    Edge.from_endpoints(start, end,
                                        is_hole_negatively_oriented, context)
                    if start < end
                    else
                    Edge.from_endpoints(end, start,
                                        not is_hole_negatively_oriented,
                                        context)
                    for start, end in contour_to_edges_endpoints(hole)
            )
        return cls._from_box_with_edges(context.contour_box(border), edges,
                                        shuffler, context)

    @property
    def height(self) -> int:
        """
        Returns height of the root node.
        """
        return self.root.height

    def locate(self, point: Point) -> Location:
        """
        Finds location of point relative to decomposed geometry.

        Time complexity:
            ``O(self.height)``
        Memory complexity:
            ``O(1)``
        """
        return self.root.locate(point)

    __slots__ = 'root',

    def __init__(self, root: Node) -> None:
        """
        Initializes graph.

        Time complexity:
            ``O(1)``
        Memory complexity:
            ``O(1)``
        """
        self.root = root

    def __contains__(self, point: Point) -> bool:
        """
        Checks if point is contained in decomposed geometry.

        Time complexity:
            ``O(self.height)``
        Memory complexity:
            ``O(1)``
        """
        return bool(self.root.locate(point))

    __repr__ = generate_repr(__init__)

    @classmethod
    def _from_box_with_edges(cls,
                             box: Box,
                             edges: List[Edge],
                             shuffler: Shuffler,
                             context: Context) -> Graph:
        shuffler(edges)
        edges_iterator = iter(edges)
        result = cls(
                to_single_trapezoid_node_replacement(
                        box_to_leaf(box, context).trapezoid,
                        next(edges_iterator)
                )
        )
        for edge in edges_iterator:
            add_edge(result, edge)
        return result

def add_edge(graph: Graph, edge: Edge) -> None:
    assert graph.height > 0
    trapezoids = find_intersecting_trapezoids(graph, edge)
    first_trapezoid = trapezoids[0]
    if len(trapezoids) == 1:
        replacement_node = to_single_trapezoid_node_replacement(
                first_trapezoid, edge
        )
        first_trapezoid.node.replace_with(replacement_node)
    else:
        prev_above, prev_below = add_edge_to_first_trapezoid(first_trapezoid,
                                                             edge)
        # prepare for the loop
        prev_trapezoid = first_trapezoid
        for middle_trapezoid in trapezoids[1:-1]:
            prev_above, prev_below = add_edge_to_middle_trapezoid(
                    middle_trapezoid, edge, prev_above, prev_below,
                    prev_trapezoid
            )
            # prepare for next loop
            prev_trapezoid = middle_trapezoid
        add_edge_to_last_trapezoid(trapezoids[-1], edge, prev_above,
                                   prev_below, prev_trapezoid)

def add_edge_to_first_trapezoid(trapezoid: Trapezoid,
                                edge: Edge) -> Tuple[Trapezoid, Trapezoid]:
    above_node, below_node = (
        Leaf(edge.left, trapezoid.right, edge, trapezoid.above),
        Leaf(edge.left, trapezoid.right, trapezoid.below, edge)
    )
    replacement_node: Node
    replacement_node = YNode(edge, below_node, above_node)
    above, below = above_node.trapezoid, below_node.trapezoid
    # set pairs of trapezoid neighbours
    if edge.left == trapezoid.left:
        above.upper_left = trapezoid.upper_left
        below.lower_left = trapezoid.lower_left
    else:
        left_node = Leaf(trapezoid.left, edge.left, trapezoid.below,
                         trapezoid.above)
        left = left_node.trapezoid
        left.lower_left = trapezoid.lower_left
        left.upper_left = trapezoid.upper_left
        left.lower_right = below
        left.upper_right = above
        replacement_node = XNode(edge.left, left_node, replacement_node)
    above.upper_right = trapezoid.upper_right
    below.lower_right = trapezoid.lower_right
    trapezoid.node.replace_with(replacement_node)
    return above, below

def add_edge_to_last_trapezoid(
        trapezoid: Trapezoid,
        edge: Edge,
        prev_above: Trapezoid,
        prev_below: Trapezoid,
        prev_trapezoid: Trapezoid
) -> None:
    if prev_above.above is trapezoid.above:
        above_node = prev_above.node
        above = prev_above
        above.right = edge.right
    else:
        above_node = Leaf(trapezoid.left, edge.right, edge, trapezoid.above)
        above = above_node.trapezoid
        above.lower_left = prev_above
        above.upper_left = (prev_above
                            if trapezoid.upper_left is prev_trapezoid
                            else trapezoid.upper_left)
    if prev_below.below is trapezoid.below:
        below_node = prev_below.node
        below = prev_below
        below.right = edge.right
    else:
        below_node = Leaf(trapezoid.left, edge.right, trapezoid.below, edge)
        below = below_node.trapezoid
        below.upper_left = prev_below
        below.lower_left = (prev_below
                            if trapezoid.lower_left is prev_trapezoid
                            else trapezoid.lower_left)
    replacement_node: Node
    replacement_node = YNode(edge, below_node, above_node)
    # set pairs of trapezoid neighbours
    if edge.right == trapezoid.right:
        above.upper_right = trapezoid.upper_right
        below.lower_right = trapezoid.lower_right
    else:
        right_node = Leaf(edge.right, trapezoid.right, trapezoid.below,
                          trapezoid.above)
        right = right_node.trapezoid
        right.lower_right = trapezoid.lower_right
        right.upper_right = trapezoid.upper_right
        above.upper_right = below.lower_right = right
        replacement_node = XNode(edge.right, replacement_node, right_node)
    trapezoid.node.replace_with(replacement_node)

def add_edge_to_middle_trapezoid(
        trapezoid: Trapezoid,
        edge: Edge,
        prev_above: Trapezoid,
        prev_below: Trapezoid,
        prev_trapezoid: Trapezoid
) -> Tuple[Trapezoid, Trapezoid]:
    if prev_above.above is trapezoid.above:
        above = prev_above
        above.right = trapezoid.right
        above_node = above.node
    else:
        above_node = Leaf(trapezoid.left, trapezoid.right, edge,
                          trapezoid.above)
        above = above_node.trapezoid
        above.lower_left = prev_above
        above.upper_left = (prev_above
                            if trapezoid.upper_left is prev_trapezoid
                            else trapezoid.upper_left)
    if prev_below.below is trapezoid.below:
        below = prev_below
        below.right = trapezoid.right
        below_node = below.node
    else:
        below_node = Leaf(trapezoid.left, trapezoid.right, trapezoid.below,
                          edge)
        below = below_node.trapezoid
        below.upper_left = prev_below
        below.lower_left = (prev_below
                            if trapezoid.lower_left is prev_trapezoid
                            else trapezoid.lower_left)
    above.upper_right = trapezoid.upper_right
    below.lower_right = trapezoid.lower_right
    replacement_node = YNode(edge, below_node, above_node)
    trapezoid.node.replace_with(replacement_node)
    return above, below

def to_single_trapezoid_node_replacement(trapezoid: Trapezoid,
                                         edge: Edge) -> Node:
    above_node, below_node = (
        Leaf(edge.left, edge.right, edge, trapezoid.above),
        Leaf(edge.left, edge.right, trapezoid.below, edge)
    )
    result: Node
    result = YNode(edge, below_node, above_node)
    above, below = above_node.trapezoid, below_node.trapezoid
    if edge.right == trapezoid.right:
        below.lower_right = trapezoid.lower_right
        above.upper_right = trapezoid.upper_right
    else:
        right_node = Leaf(edge.right, trapezoid.right, trapezoid.below,
                          trapezoid.above)
        right = right_node.trapezoid
        right.lower_right = trapezoid.lower_right
        right.upper_right = trapezoid.upper_right
        below.lower_right = right
        above.upper_right = right
        result = XNode(edge.right, result, right_node)
    if edge.left == trapezoid.left:
        below.lower_left = trapezoid.lower_left
        above.upper_left = trapezoid.upper_left
    else:
        left_node = Leaf(trapezoid.left, edge.left, trapezoid.below,
                         trapezoid.above)
        left = left_node.trapezoid
        left.lower_left = trapezoid.lower_left
        left.upper_left = trapezoid.upper_left
        left.lower_right = below
        left.upper_right = above
        result = XNode(edge.left, left_node, result)
    return result

def box_to_leaf(box: Box, context: Context) -> Leaf:
    min_x, min_y, max_x, max_y = box.min_x, box.min_y, box.max_x, box.max_y
    delta_x, delta_y = max_x - min_x, max_y - min_y
    # handle horizontal/vertical cases
    delta_x, delta_y = delta_x or 1, delta_y or 1
    min_x, min_y, max_x, max_y = (min_x - delta_x, min_y - delta_y,
                                  max_x + delta_x, max_y + delta_y)
    point_cls = context.point_cls
    return Leaf(point_cls(min_x, min_y), point_cls(max_x, min_y),
                Edge.from_endpoints(point_cls(min_x, min_y),
                                    point_cls(max_x, min_y), False, context),
                Edge.from_endpoints(point_cls(min_x, max_y),
                                    point_cls(max_x, max_y), True, context))

def find_intersecting_trapezoids(graph: Graph, edge: Edge) -> List[Trapezoid]:
    trapezoid = graph.root.search_edge(edge)
    result = [trapezoid]
    right = edge.right
    while trapezoid.right < right:
        candidate = ((trapezoid.upper_right or trapezoid.lower_right)
                     if (edge.orientation_of(trapezoid.right)
                         is Orientation.CLOCKWISE)
                     else (trapezoid.lower_right or trapezoid.upper_right))
        assert candidate is not None, ('Expected neighbour trapezoid, '
                                       'but none found.')
        trapezoid = candidate
        result.append(trapezoid)
    return result


from DependencyAnlyzer.P4ProgramGraph import P4ProgramGraph
from P4ProgramParser.P416JsonParser import ParsedP416ProgramForV1ModelArchitecture
from P4ProgramParser.P4ProgramParserFactory import P4ProgramParserFactory
from ParserMapper.make_tcam import buildParserMapper
from RMTHardwareSimulator import RMTHardwareFactory
from util import loadP416JsonUsingAutoGeneratedJsonParser
from ParserMapper import HeaderLib
# from ParserMapper import *
# from ParserMapper import RefCount
# from P416JsonParser import  ParserOpOp
from utils import Util
import time

#64, 96, and 64 words of 8, 16, and 32b
p4ProgramParserFactory = P4ProgramParserFactory()
hw = RMTHardwareFactory.createRmtHardware(rmtHardwaRemodelName = "RMT_V1",
    instructionSetConfigurationJsonFile= "../Resources/HardwareConfigs/RMTV1ModelInstructions.json",
    hardwareSpecConfigurationJsonFile = "../Resources/HardwareConfigs/RMTV1model32Stages.json")
# p4program = p4ProgramParserFactory.getParsedP4Program(p4JsonFile="../Resources/P4ProgramsForPaper/QoSModifier/qos_modifier.json",p4VersionAndArchitecture="P416_V1_Model")
# p4program = p4ProgramParserFactory.getParsedP4Program(p4JsonFile="../Resources/P4ProgramsForPaper/L2L3Simple/l2l3Simple.json",p4VersionAndArchitecture="P416_V1_Model")
# p4program = p4ProgramParserFactory.getParsedP4Program(p4JsonFile="../Resources/P4ProgramsForPaper/L2L3Complex/l2l3complex.json",p4VersionAndArchitecture="P416_V1_Model")
p4program = p4ProgramParserFactory.getParsedP4Program(p4JsonFile="../Resources/P4ProgramsForPaper/P4anony/p4anony.json",p4VersionAndArchitecture="P416_V1_Model")

p4program.buildHeaderVector(hw)
p4ProgramGraph = P4ProgramGraph(p4program)
p4ProgramGraph.loadPipelines(hw)
headerFieldSpecsInP4ProgramToBeUsedForParserMapper, totalRawBitwidth = p4ProgramGraph.headeranalyzer(hw)
parseGraphHeaderList, parsedGraphHeaders, initHeader = HeaderLib.loadParseGraph(parserObject = p4program.parsers[0], p4ProgramGraph = p4ProgramGraph) # There is only one parser in v1model
start = int(round(time.time() * 1000))
buildParserMapper(parseGraphHeaderList, parsedGraphHeaders, hw, initHeader)
end = int(round(time.time() * 1000))
print("Total time required for parser TCAM entry generation is "+str(end-start)+" miliseconds")
print("==============================================================================")

p4ProgramGraph.loadPipelines(hw)
headerFieldSpecsInP4Program,totalRawBitwidth = p4ProgramGraph.headeranalyzer(hw)
# mappedPacketHeaderVector1 = hw.mapHeaderFieldsUsingGoogleOR(headerFieldSpecsInP4Program)
# headerFieldSpecsInP4Program = {33: 1, 16: 1} # this line is for test purpose
start = int(round(time.time() * 1000))
mappedPacketHeaderVector = hw.mapHeaderFields(headerFieldSpecsInP4Program)
end = int(round(time.time() * 1000))
print(mappedPacketHeaderVector)
print("Total time required for header vector mapping is "+str(end-start)+" miliseconds")
print("==============================================================================")
p4ProgramGraph.storePHVFieldMappingForHeaderFields(mappedPacketHeaderVector=mappedPacketHeaderVector)
Util.calculatePHVWaste(headerFieldSpecsInP4Program, mappedPacketHeaderVector,totalRawBitwidth)
start = int(round(time.time() * 1000))
p4ProgramGraph.embedPipelines(hw)
end = int(round(time.time() * 1000))
print("Total time required for TDG mapping is "+str(end-start)+" miliseconds")
print("Total Nodes in the TDG is: "+str(p4ProgramGraph.rawTDGNodeCount) )
print("Total Edges in the TDG is: "+str(p4ProgramGraph.rawTDGEdgeCount) )
hw.calculateTotalLatency(p4ProgramGraph, hw)

#===================== Upto here header mappign is done


fin=open("running-config.cfg","r")
term="nameif"
term1="ip address"
d=dict()
def ip():
	for line in fin:
		line.strip.split('/n')
		if term in line:
			line_nameif=line_nameif.strip()
			print(line_nameif)
		if term1 in line:
			line_ip_sddress=line_ip_address.strip()
			print(line_ip_address)
			dict.update({line_nameif:line_ip_address})
ip()
print(d)
fin.close()


import time

def device_scanner():
    from elements import Switch
    time.sleep(3)
    while True:
        new_count = 0
        for switch in Switch.all():
            switch.scan_ports()
            new_count += switch.scan_devices()
        if new_count > 0:
            print(f'Found {new_count} new Devices')
        time.sleep(30)


# coding: utf-8

"""
    IONOS Cloud - DNS API

    Cloud DNS service helps IONOS Cloud customers to automate DNS Zone and Record management.   # noqa: E501

    The version of the OpenAPI document: 1.12.0
    Contact: support@cloud.ionos.com
    Generated by: https://openapi-generator.tech
"""

import pprint
import re  # noqa: F401

import six

from ionoscloud_dns.configuration import Configuration

class KeyData(object):
    """NOTE: This class is auto generated by OpenAPI Generator.
    Ref: https://openapi-generator.tech

    Do not edit the class manually.
    """

    """
    Attributes:
      openapi_types (dict): The key is attribute name
                            and the value is attribute type.
      attribute_map (dict): The key is attribute name
                            and the value is json key in definition.
    """
    openapi_types = {

        'flags': 'int',

        'protocol': 'int',

        'alg': 'int',

        'pub_key': 'str',
    }

    attribute_map = {

        'flags': 'flags',

        'protocol': 'protocol',

        'alg': 'alg',

        'pub_key': 'pubKey',
    }

    def __init__(self, flags=None, protocol=None, alg=None, pub_key=None, local_vars_configuration=None):  # noqa: E501
        """KeyData - a model defined in OpenAPI"""  # noqa: E501
        if local_vars_configuration is None:
            local_vars_configuration = Configuration()
        self.local_vars_configuration = local_vars_configuration

        self._flags = None
        self._protocol = None
        self._alg = None
        self._pub_key = None
        self.discriminator = None

        if flags is not None:
            self.flags = flags
        if protocol is not None:
            self.protocol = protocol
        if alg is not None:
            self.alg = alg
        if pub_key is not None:
            self.pub_key = pub_key

    @property
    def flags(self):
        """Gets the flags of this KeyData.  # noqa: E501

        Represents the key's metadata and usage information.  # noqa: E501

        :return: The flags of this KeyData.  # noqa: E501
        :rtype: int
        """
        return self._flags

    @flags.setter
    def flags(self, flags):
        """Sets the flags of this KeyData.

        Represents the key's metadata and usage information.  # noqa: E501

        :param flags: The flags of this KeyData.  # noqa: E501
        :type flags: int
        """

        self._flags = flags

    @property
    def protocol(self):
        """Gets the protocol of this KeyData.  # noqa: E501

        Represents the protocol associated with the key (3 represents DNSSEC).  # noqa: E501

        :return: The protocol of this KeyData.  # noqa: E501
        :rtype: int
        """
        return self._protocol

    @protocol.setter
    def protocol(self, protocol):
        """Sets the protocol of this KeyData.

        Represents the protocol associated with the key (3 represents DNSSEC).  # noqa: E501

        :param protocol: The protocol of this KeyData.  # noqa: E501
        :type protocol: int
        """

        self._protocol = protocol

    @property
    def alg(self):
        """Gets the alg of this KeyData.  # noqa: E501

        Represents the signing algorithm.  # noqa: E501

        :return: The alg of this KeyData.  # noqa: E501
        :rtype: int
        """
        return self._alg

    @alg.setter
    def alg(self, alg):
        """Sets the alg of this KeyData.

        Represents the signing algorithm.  # noqa: E501

        :param alg: The alg of this KeyData.  # noqa: E501
        :type alg: int
        """

        self._alg = alg

    @property
    def pub_key(self):
        """Gets the pub_key of this KeyData.  # noqa: E501

        Represents the public key data in Base64 encoding.  # noqa: E501

        :return: The pub_key of this KeyData.  # noqa: E501
        :rtype: str
        """
        return self._pub_key

    @pub_key.setter
    def pub_key(self, pub_key):
        """Sets the pub_key of this KeyData.

        Represents the public key data in Base64 encoding.  # noqa: E501

        :param pub_key: The pub_key of this KeyData.  # noqa: E501
        :type pub_key: str
        """

        self._pub_key = pub_key
    def to_dict(self):
        """Returns the model properties as a dict"""
        result = {}

        for attr, _ in six.iteritems(self.openapi_types):
            value = getattr(self, attr)
            if isinstance(value, list):
                result[attr] = list(map(
                    lambda x: x.to_dict() if hasattr(x, "to_dict") else x,
                    value
                ))
            elif hasattr(value, "to_dict"):
                result[attr] = value.to_dict()
            elif isinstance(value, dict):
                result[attr] = dict(map(
                    lambda item: (item[0], item[1].to_dict())
                    if hasattr(item[1], "to_dict") else item,
                    value.items()
                ))
            else:
                result[attr] = value

        return result

    def to_str(self):
        """Returns the string representation of the model"""
        return pprint.pformat(self.to_dict())

    def __repr__(self):
        """For `print` and `pprint`"""
        return self.to_str()

    def __eq__(self, other):
        """Returns true if both objects are equal"""
        if not isinstance(other, KeyData):
            return False

        return self.to_dict() == other.to_dict()

    def __ne__(self, other):
        """Returns true if both objects are not equal"""
        if not isinstance(other, KeyData):
            return True

        return self.to_dict() != other.to_dict()


#!/usr/bin/python3
""""0-subs module"""
import requests

def number_of_subscribers(subreddit):
    """
    args:
        subreddit - variable for subreddit to querry
    return:
        returns number of subscribers for a given subreddit
        or 0 if invalid subreddit is given
    """

    user_agent = "0-subs"
    url = "https://www.reddit.com/r/{}/about.json".format(subreddit)

    headers = {"User-Agent": user_agent}
    try:
        response = requests.get(url, headers=headers, allow_redirects=False)
        if response.status_code == 200:
            data = response.json()
            if "data" in data and "subscribers" in data["data"]:
                return data["data"]["subscribers"]
            else:
                return 0
        else:
            return 0
    except requests.exceptions.RequestException:
        return 0


import numpy as np
positive = np.loadtxt("file/PositivePrimeData_1_million.txt",delimiter=',',dtype=int)
negative = np.loadtxt("file/NegativePrimeData_1_million.txt",delimiter=',',dtype=int)
print("positive.shape:",positive.shape)
print("negative.shape:",negative.shape)

sampleNegative = []
for i in range(0,negative.shape[0],12):
    sampleNegative.append(negative[i])

sampleNegative = np.array(sampleNegative)
print("sampleNegative.shape:",sampleNegative.shape)

BalancePrimeData_1_million = np.row_stack((sampleNegative,positive))
print("BalancePrimeData_1_million.shape:",BalancePrimeData_1_million.shape)

np.random.shuffle(BalancePrimeData_1_million)
print(BalancePrimeData_1_million)
print("BalancePrimeData_1_million.shape:",BalancePrimeData_1_million.shape)
np.savetxt("file/BalancePrimeData_1_million.txt",BalancePrimeData_1_million,fmt='%d',delimiter=',')

# import numpy as np
#
# # 太慢了，等跑完就哭了。这样不行!
#
#
# data = np.loadtxt("file/primes_in_10_million.txt",delimiter=' ',dtype=int)
# print("prime number:",data.shape)
# print(data)
# # 生成一千万内的数字
# numberTenMillion = np.array([i for i in np.arange(2,10000000)],dtype=int)
# # 给数字加上类标（质数为1，合数为0）
# zeros = np.zeros(9999998,dtype=int)
# trainingData = np.column_stack((numberTenMillion.T,zeros))
# print("trainingData.shape:",trainingData.shape)
# print(trainingData)
#
# # # 将质数后的label改为1
# # for i in range(trainingData.shape[0]):
# #     print(i)
# #     if trainingData[i][0] in set(data):
# #         trainingData[i][1] = 1
#
# # 检查修改结果
# print(trainingData[trainingData[:,1]==1,:].shape)
#
# # 保存数据集
# np.savetxt("file/trainingPrimeData_10_million.txt",trainingData,fmt='%d',delimiter=",")
#
#


#Java C# PHP PHP JAVA C java ни е инпута
# трябва да ни върне тези думи, които ги има нечетен брой пъти в списъка

# from collections import defaultdict
#
# words = input().split(" ")
# counter_of_words = defaultdict(int)
#
# for i in words:
#     counter_of_words[i.lower()] += 1
#
# print(' '. join(i for i, count in counter_of_words.items() if count % 2 != 0))

words = input(). split(" ")
dict = {}

for i in words:
    word_lower = i.lower()
    if word_lower not in dict:
        dict[word_lower] = 0
    dict[word_lower] += 1

print(dict)

for k, v in dict.items():
    if v % 2 != 0:
        print(k, end=" ")

13. Dictionaries - Lab/06. Odd Occurrences.py


from deepface import DeepFace
import cv2
import numpy as np
import mediapipe as mp
import math
import datetime

FER = mp.solutions.face_mesh

left_eye = [474, 475, 476, 477]
right_eye = [469, 470, 471, 472]

left_num = [362]
right_num = [263]

def facial_expression_and_eye_movements(video_file):
    cnt = 0
    now = datetime.datetime.now()
    data = {'angry': 0, 'disgust': 0, 'fear': 0, 'happy': 0,
            'sad': 0, 'surprise': 0, 'neutral': 0, 'time': []}
    eye_data = [0, 0, 0]
    data_new = {'angry': [], 'disgust': [], 'fear': [], 'happy': [],
                'sad': [], 'surprise': [], 'neutral': [], 'time': []}
    video = cv2.VideoCapture(video_file)

    def euclidean_distance(first, second):
        x1, y1 =first.ravel()
        x2, y2 =second.ravel()
        distance = math.sqrt((x2-x1)**2 + (y2-y1)**2)
        return distance

    def eye_position(middel, right, left):
        dist = euclidean_distance(middel, right) # 동공의 중앙과 오른쪽 거리 계산
        lefttoright = euclidean_distance(right, left) # 눈의 좌, 우측 거리 계산
        ratio = dist/lefttoright # 거리의 비율 계산
        where =""
        if ratio <= 0.48 and ratio >= 0:
            where="right"
        elif ratio > 0.48 and ratio <= 0.52:
            where="center"
        elif ratio > 0.52 and ratio <= 1:
            where = "left"
        return where, ratio

    with FER.FaceMesh(max_num_faces=1, refine_landmarks=True, min_detection_confidence=0.5, min_tracking_confidence=0.5) as face_mesh:

        while True:
            status, frame = video.read()
            cnt += 1
            if not status:
                break
            if cnt%5:
                continue
            face_analysis = DeepFace.analyze(frame, actions=['emotion'], enforce_detection=False)

            data_f = face_analysis[0]['emotion']
            check_main_emotion = face_analysis[0]['dominant_emotion']
            main_emotion = 'neutral'

            # if main_emotion != check_main_emotion:
            #     data['time'].append((datetime.datetime.now() - now).total_seconds())

            for ki in data_f:
                data[ki] += data_f[ki]
                data_new[ki].append(round(data_f[ki], 2))

            data_new['time'].append(cnt)

            frame = cv2.flip(frame, 1)
            new_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)   #Mediapipe precisa do formato de cores RGB mas o OpenCV usa o BGR
            heights_img, width_img = frame.shape[:2]
            results = face_mesh.process(new_frame)
            if results.multi_face_landmarks:
                mesh_points=np.array([np.multiply([i.x, i.y], [width_img, heights_img]).astype(int) for i in results.multi_face_landmarks[0].landmark])

                (check_x, check_y), left_radius = cv2.minEnclosingCircle(mesh_points[left_eye])
                (new_check_x,new_check_y), right_radius = cv2.minEnclosingCircle(mesh_points[right_eye])

                centerleft = np.array([check_x, check_y], dtype=np.int32)
                center_right = np.array([new_check_x, new_check_y], dtype=np.int32)

                result_position, ratio = eye_position(centerleft, mesh_points[right_num][0], mesh_points[left_num][0])

                # print(result_position, ratio)
                if result_position == 'left':
                    eye_data[0] += 1
                elif result_position == 'right':
                    eye_data[2] += 1
                elif result_position == 'center':
                    eye_data[1] += 1

    main_emotion = ''
    main_emotion_value = 0
    all_value = 0
    for i in data:
        if i != 'time':
            all_value += data[i]
            if data[i] > main_emotion_value:
                main_emotion_value = data[i]
                main_emotion = i
    for i in data:
        if i != 'time':
            data[i] = str(round(100 * data[i]/all_value, 3)) + '%'

    data['main_emotion'] = [main_emotion, str(round(100 * main_emotion_value/all_value, 3)) + '%']

    for idx, checkpoint in enumerate(data_new['time']):
        data_new['time'][idx] = 60 * checkpoint // cnt

    # del data['time']
    center_eye_data = 100 - (100 * eye_data[1]/sum(eye_data))
    return data, center_eye_data, eye_data, data_new


#!/usr/bin/env python
#

import pandas as pd
import wisepy.nn as nn
import utils.logging as logging
from utils import load_json, mkdir
import wisepy.nn.transform as transform
import os
def build_convolution(**args):

	ModelTemplate = args.get("template",None)

	template_functions = {
	"relu_softmax": nn.CNN.relu_softmax
	}
	if ModelTemplate and ModelTemplate in template_functions:
		NetArgs = args["netargs"]
		NNS = tuple(NetArgs.get("nns",[16,32,64]))
		InSize = NetArgs.get("in_s",(465,310))
		OutSize = NetArgs.get("out_s",8)

		#<-
		logging.info("Building a template convolution model \
			(%s) with:\ninput size: \
			(%d %d %d)\noutput size \
			%d" % (ModelTemplate,InSize[0],InSize[1],InSize[2],OutSize))
		#->

		model = template_functions[ModelTemplate](NNS,InSize,OutSize)

	return model
def build_sequential(**args):
	ArchitectureFile	= args["Architecture"]
	netArch = load_json(ArchitectureFile)

	if netArch["structure"] == "conv":

		#<-
		logging.info("Training a convolution model")
		#->

		return build_convolution(**netArch)

ModelFunctions = {
	"sequential": build_sequential
}

class ImageClassifier:
	def __init__(self,classifier_path=None):
		self._model = None
		if classifier_path:
			self.load_model(classifier_path)
	def load_model(self,path):
		self._model = nn.load_network(path)
	def test(self,dataset):
		return self._model.test(dataset)
	def predict_dataset(self,dataset):
		predictions = [self._model.predict(i) for i in dataset]
	def predict(self,instance):
		return self._model.predict(instance)
	def info(self):
		return self._model.info
	def labels(self):
		return self._model.labels
	def transformer(self):
		return self._model.itransformer
	def train(self,**args):
		ModelType		= args["ModelType"]
		Dataset 		= args["Dataset"]
		NamingProtocol 	= args["NamingProtocol"]
		ImageSize 		= tuple(args["ImageSize"])
		#ModelID			= args.get("ModelID",None)
		Epochs 			= args.get("Epochs",50)
		BatchSize 		= args.get("BatchSize",20)
		CheckPoint 		= args.get("CheckPoint",None)
		ITransformer	= args.get("ITransformer",None)
		Labels			= args.get("Labels",None)

		model = ModelFunctions[ModelType](**args)

		#<-
		logging.info("Start training...")
		#->

		model.train(Dataset,NamingProtocol,epochs=Epochs,
			batch_size=BatchSize,img_size=ImageSize,checkpoint=CheckPoint)

		#<-
		logging.info("Finished training...")
		#->

		# if ModelID:
		# 	TrainedModels[ModelID] = model
		# else:
		# 	TrainedModels[utl.random_text(5)] = model

		if ITransformer:
			with open(ITransformer) as f:
				transText = f.read()
			model.set_itransformer(transform.load(transText))

		if Labels:
			with open(Labels) as f:
				labels = f.read().split("\n")

			model.set_labels(labels)

		self._model = model
	def test(self,**args):
		Dataset 		= args["Dataset"]
		NamingProtocol 	= args["NamingProtocol"]

		info = self.info()

		#<-
		logging.info("Start testing...")
		#->

		result = self._model.test(Dataset,ds_mode=NamingProtocol,
			img_size=info["ImageSize"])

		#<-
		logging.info("Finished testing...")
		#->

		return result

	def publish(self,prefix):

		if os.path.exists("%s" % prefix):
			logging.warn("Model couldn't be saved because %s.json already exists!!" % prefix)
			return
		mkdir(prefix)
		self._model.save("%s/%s" % (prefix,prefix.split("/")[-1]))
def get_param():
	"""
	handle command line parameters, test using -h parameter....
	"""
	import argparse
	parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter)
	parser.add_argument('-t','--traintest',help='train or test',default='train')
	parser.add_argument('-d','--data',help='path to dataset',default=None)
	parser.add_argument('-s','--imagesize',help='training image size',default=[314,420])
	parser.add_argument('-e','--epochs',help='number of epochs for the training',default=50)
	parser.add_argument('-b','--batchsize',help='batch size for the training',default=20)
	parser.add_argument('-a','--architecture', help='network architecture file',default=None)
	parser.add_argument('-p','--prefix',
		help='model prefix (loading for testing, saving for training)',default=None)
	parser.add_argument('-l','--labels',help='file name for actual labels\' names',default=None)
	parser.add_argument('-r','--transformer', help='input transformer file', default=None)
	args = parser.parse_args()

	if type(args.imagesize) == str:
		args.imagesize = [int(i) for i in args.imagesize.split(",")]
	if type(args.epochs) == str:
		args.epochs = int(args.epochs)
	if type(args.batchsize) == str:
		args.batchsize = int(args.batchsize)

	return args.traintest,args.architecture,args.data,args.imagesize, \
		   args.epochs,args.batchsize,args.prefix,args.labels,args.transformer
def main():

	traintest,architecture,data,imagesize,epochs,batchsize,prefix,labels,transformer = get_param()

	if traintest == "train":
		trainArg = {
			"ModelType":"sequential",
			"Dataset": data,
			"ImageSize": imagesize,
			"Epochs": epochs,
			"BatchSize": batchsize,
			"Labels": labels,
			"Architecture": architecture,
			"NamingProtocol": "scikit",
			"ITransformer": transformer
		}
		clsf = ImageClassifier()
		clsf.train(**trainArg)
		clsf.publish(prefix)
	if traintest == "test":
		testArg = {
			"Dataset": data,
			"NamingProtocol": "scikit"
		}
		clsf = ImageClassifier(prefix)
		results = clsf.test(**testArg)
		print(results)

if __name__ == '__main__':
	main()


from decimal import Decimal
import itertools as it

from datetime import datetime

from app import db
import click
from .models import Ticket, Route

import xml.etree.ElementTree as ET

class FlightType:
    ONE_WAY = "ONE_WAY"
    TWO_WAY = "TWO_WAY"

class PriceType:
    ADULT = "SingleAdult"
    CHILD = "SingleChild"
    INFANT = "SingleInfant"

def get_flights(itinerary):
    """
    Returns all flights info for "OnwardPricedItinerary" and "ReturnPricedItinerary" tags
    """
    dt_format = "%Y-%m-%dT%H%M"
    onward_flights = []
    for flight in itinerary[0]:  # Flights
        extracted_data = {item.tag: item.text for item in flight}
        onward_flights.append(Ticket(
            carrier_id=extracted_data["Carrier"],
            flight_number=extracted_data["FlightNumber"],
            source=extracted_data["Source"],
            destination=extracted_data["Destination"],
            departure_time=datetime.strptime(extracted_data["DepartureTimeStamp"], dt_format),
            arrival_time=datetime.strptime(extracted_data["ArrivalTimeStamp"], dt_format),
            class_=extracted_data["Class"],
            number_of_stops=extracted_data["NumberOfStops"],
            fare_basis=extracted_data["FareBasis"],
            ticket_type=extracted_data["TicketType"],
            warning_text=extracted_data["WarningText"]
            ))
    return sorted(onward_flights, key=lambda ticket: ticket.departure_time)

def get_prices(pricing):
    """
    Extracts extracts all prices from "Pricing" tag
    """
    prices = {
        PriceType.ADULT: 0,
        PriceType.CHILD: 0,
        PriceType.INFANT: 0
    }
    for price in pricing:
        if price.attrib.get("ChargeType") == "TotalAmount":
            prices[price.attrib.get("type")] = Decimal(price.text)
    return prices

def extract(two_way=True):
    """
        extracts data from via3.xml, viaow.xml and saves it to DB
    """
    file_name = "via3.xml" if two_way else "viaow.xml"
    tree = ET.parse(file_name)
    root = tree.getroot()
    flight_type = FlightType.TWO_WAY if two_way else FlightType.ONE_WAY

    for flight in root.findall("./PricedItineraries/Flights"):
        onwards = get_flights(flight[0])  # onward flights
        prices_child_pos = 2 if two_way else 1
        currency = flight[prices_child_pos].attrib.get("currency")
        backwards = get_flights(flight[1]) if two_way else []  # backward flights
        prices = get_prices(flight[prices_child_pos])
        source = onwards[0]
        final_destination = onwards[-1]
        duration = (final_destination.arrival_time - source.departure_time).total_seconds()
        route = Route(
            carrier_id=source.carrier_id,
            source=source.source,
            destination=final_destination.destination,
            departure_time=source.departure_time,
            arrival_time=final_destination.arrival_time,
            duration=duration,
            adult_price=prices[PriceType.ADULT],
            child_price=prices[PriceType.CHILD],
            infant_price=prices[PriceType.INFANT],
            currency=currency,
            flight_type=flight_type
        )
        if two_way:
            route.back_departure_time = backwards[0].departure_time
            route.back_arrival_time = backwards[-1].arrival_time
        db.session.add(route)
        for ticket in it.chain(onwards, backwards):
            ticket = db.session.merge(ticket)
            route.tickets.append(ticket)
    db.session.commit()

@click.command()
def populate_db():
    """
    Extracts data from xml files and puts into db
    """
    db.create_all()
    extract()
    extract(two_way=False)