import os

import numpy as np
from matplotlib import pyplot as plt
from matplotlib.collections import LineCollection
from tqdm import trange

import arena

def turn(heading, turn_rate_sd):
    """
    Turn a heading in a random direction
    :param heading: current heading array
    :param turn_rate_sd: s.d. of turn rate from normal distribution
    """
    return heading + np.random.normal(0, turn_rate_sd, size=heading.shape)

def get_speed(shape, speed_mean=1, speed_sd=0.5):
    """
    Get a random speed for each mosquito
    :param shape: output shape
    :param speed_mean: mean speed
    :param speed_sd: standard deviation
    :return: speed array
    """
    # Non-zero minimum speed avoids issues analysing heading later
    return np.fmax(0.01, np.random.normal(speed_mean, speed_sd, size=shape))

def move(heading, speed):
    """
    Convert speed and heading to vector move
    :param pos: current position (array)
    :param heading: current heading (array)
    :param speed: current speed (array)
    :return: new position (array)
    """
    return speed * np.array([np.cos(heading), np.sin(heading)]).T

def grad_move(pos, x_grad, y_grad, speed):
    """
    Move points along a gradient
    :param pos: current position (Nx2 array)
    :param x_grad: x gradient (HxW array)
    :param y_grad: y gradient (HxW array)
    :param speed: current speed (Nx array)
    :return: vector movement for each input pos (Nx2 array)
    """
    # Linear index of position
    idx = np.floor(pos[:, 1]).astype(int) * x_grad.shape[1] + np.floor(pos[:, 0]).astype(int)

    # Get gradient at current position
    x_move = x_grad.flat[idx] * speed.flatten()
    y_move = y_grad.flat[idx] * speed.flatten()

    return np.c_[x_move, y_move]

def is_at_site(pos, sites, site_density=None):
    """
    Check if a point is at a site
    :param pos: current position (Nx2 array)
    :param sites: site locations (HxW array)
    :return: boolean array of length N
    """
    # Linear index of position
    idx = np.floor(pos[:, 1]).astype(int) * sites.shape[1] + np.floor(pos[:, 0]).astype(int)

    # Check if at site
    if site_density is not None:
        passes = (sites.flat[idx] != 0) & (site_density.flat[idx] < np.random.uniform(1, 10, len(idx)))
        site_density.flat[idx[passes]] += 1
        return passes, site_density
    else:
        return sites.flat[idx] != 0

def isalive(pos, age, bounds):
    """
    Check if a point is alive (i.e. within the bounds)
    :param pos: current position (Nx2 array)
    :param bounds: bounds of the arena (2x2 array)
    :return: boolean array of length N
    """
    return np.all(pos > bounds[:, 0], axis=1) & np.all(pos < bounds[:, 1], axis=1) & (age < 100)

def plot(bounds, mosquitoes, trails, fed):
    """Plots mosquito trails and current position"""
    fig, ax = plt.subplots(figsize=(14.4, 10))
    # Setup axes
    ax.set_xlim(bounds[0, :])
    ax.set_ylim(bounds[1, :])
    ax.set_aspect('equal')
    ax.set_xticks([])
    ax.set_yticks([])

    # Show habitat + houses
    plt.imshow(np.ma.masked_where(arena.breed_distance > 10, arena.breed_distance), alpha=0.2)
    y, x = np.where(arena.feed_sites != 0)
    ax.scatter(x, y, color='brown', s=2, alpha=0.5)

    # Plot current position
    ax.scatter(mosquitoes[fed, 0], mosquitoes[fed, 1], color='r', s=2, alpha=0.5)
    ax.scatter(mosquitoes[~fed, 0], mosquitoes[~fed, 1], color='b', s=2, alpha=0.5)
    # Plot trails
    segs = trails.transpose((1, 0, 2))
    segs = np.ma.masked_where(segs == -999, segs)
    lines = LineCollection(segs, colors='k', alpha=0.2, linewidths=1)
    ax.add_collection(lines)

    fig.tight_layout()

    return fig, ax

if __name__ == '__main__':
    os.makedirs('../output', exist_ok=True)

    height, width = arena.feed_sites.shape
    bounds = np.array([[0, width], [0, height]])
    n = 100
    mosquitoes = np.random.uniform(bounds[:, 0], bounds[:, 1], size=(n, 2))
    headings = np.random.uniform(0, 2*np.pi, size=n)
    fed = np.zeros_like(headings).astype(bool)
    timer = np.zeros_like(headings)
    age = np.random.uniform(0, 50, size=fed.shape)

    site_density = np.zeros_like(arena.breed_sites, dtype=float)

    trails = np.copy([mosquitoes])
    trails_fed = np.copy([fed])

    grad_weight = 0.5

    for t in trange(500):
        alive = isalive(mosquitoes, age, bounds)
        timer[timer > 0] -= 1
        age[alive] += 1
        site_density[site_density > 0] -= 0.1
        site_density[site_density < 0] = 0
        n_mos = len(alive)

        # Turn & Move
        headings = turn(headings, np.pi/2)
        speed = get_speed((n_mos, 1), 1, 0.5)

        heading_move = np.zeros_like(mosquitoes)
        heading_move[alive] = move(headings[alive], speed[alive])

        # Follow gradient
        feed_idx = alive & ~fed & (timer == 0)
        move_to_food = np.zeros_like(mosquitoes)
        move_to_food[feed_idx] = grad_move(mosquitoes[feed_idx], arena.feed_xgrad, arena.feed_ygrad, grad_weight * speed[feed_idx])

        breed_idx = alive & fed & (timer == 0)
        move_to_breed = np.zeros_like(mosquitoes)
        move_to_breed[breed_idx] = grad_move(mosquitoes[breed_idx], arena.breed_xgrad, arena.breed_ygrad, grad_weight * speed[breed_idx])

        # Combine
        mosquitoes += heading_move + move_to_food + move_to_breed
        alive = isalive(mosquitoes, age, bounds)

        # Check if mosquitoes have reached food/breeding site
        feed_idx = alive & ~fed & (timer == 0)  # recalculate as may have moved out of bounds
        can_feed = np.zeros_like(fed)
        can_feed[feed_idx] = is_at_site(mosquitoes[feed_idx], arena.feed_sites)

        breed_idx = alive & fed & (timer == 0)
        can_breed = np.zeros_like(fed)
        can_breed[breed_idx], site_density = is_at_site(mosquitoes[breed_idx], arena.breed_sites, site_density)

        fed[feed_idx & can_feed] = True
        fed[breed_idx & can_breed] = False

        timer[feed_idx & can_feed] = 10
        timer[breed_idx & can_breed] = 5

        # Create new mosquitoes
        new_mosquitoes = mosquitoes[can_breed].copy()
        new_mosquitoes = np.repeat(new_mosquitoes, 3, axis=0)

        n_new = new_mosquitoes.shape[0]
        new_headings = np.random.uniform(0, 2*np.pi, size=n_new)

        mosquitoes = np.concatenate([mosquitoes, new_mosquitoes], axis=0)
        headings = np.concatenate([headings, new_headings], axis=0)

        fed = np.concatenate([fed, np.zeros_like(new_headings).astype(bool)], axis=0)
        timer = np.concatenate([timer, np.zeros_like(new_headings)], axis=0)
        age = np.concatenate([age, np.zeros_like(new_headings)], axis=0)

        trails = np.pad(trails, ((0, 0), (0, n_new), (0, 0)), mode='constant', constant_values=-999)
        trails_fed = np.pad(trails_fed, ((0, 0), (0, n_new)), mode='constant', constant_values=0)

        # Records movement trail
        trails = np.concatenate([trails, [mosquitoes]], axis=0)
        trails_fed = np.vstack([trails_fed, fed])

        alive = isalive(mosquitoes, age, bounds)

        if False:
            print(n_new)
        else:
            fig, _ = plot(bounds, mosquitoes[alive], trails[:, alive, :], fed[alive])
            fig.savefig(f'../output/{t:03d}.png', dpi=400)
            plt.close(fig)

    # plot(bounds, mosquitoes[alive], trails[:, alive, :], fed[alive])
    plot(bounds, mosquitoes, trails, fed)
    plt.show()
    print(len(fed))


from django import template
from datetime import datetime, timedelta
from dateutil.relativedelta import relativedelta

TYPES = {
    'datetime': datetime
}

register = template.Library()

@register.filter
def add_days(start_date, days):
    delta = start_date + timedelta(days=days)
    return datetime.strftime(delta, "%d-%m-%Y")

@register.filter
def add_months(start_date, months):
    delta = start_date + relativedelta(months=months)
    return datetime.strftime(delta, "%d-%m-%Y")

@register.filter
def is_instance(obj, type_name):
    return isinstance(obj, TYPES[type_name])


while True:
    print("MAIN MENU".center(80, "—"))
    print("1. FizzBuzz (Easy 1)")
    print("2. Leap Year (Easy 2)")
    print("3. Harf Pyramid Right (Easy 3.1)")
    print("4. Harf Pyramid Left (Easy 3.2)")
    print("5. Triangle Void (Easy 3.3)")
    print("6. Mid Pyramid (Easy 3.4)")
    print("7. Dimond Sharp (Easy 3.5)")
    print("8. Pyramid Abcde (Easy 3.6)")
    print("9. Prime Numbers (Medium 1)")
    print("0. Exit")

    while True:
        choice = int(input("Enter your choice: "))
        print("")
        if choice == 1:
            import Program.FizzBuzz
        elif choice == 2:
            import Program.LeapYear
        elif choice == 3:
            import Program.HarfPyramidRight
        elif choice == 4:
            import Program.HarfPyramidLeft
        elif choice == 5:
            import Program.trianVoid
        elif choice == 6:
            import Program.midrapy
        elif choice == 7:
            import Program.DimondSharp
        elif choice == 8:
            import Program.pyramidAbcde
        elif choice == 9:
            import Program.PrimeNumbers
        elif choice == 0:
            exit()
        else:
            print("Invalid choice please try again")


from ..lsh import norm_vectors
from ..bloom_count import bloom
from .utils import umap_embed
from ..pattern_overlap import PatternOverlap

import numpy as np
import pandas as pd
import itertools
from typing import List
import networkx as nx

from bokeh.models import ColumnDataSource, HoverTool, StaticLayoutProvider
from bokeh.plotting import Figure, figure, from_networkx

def make_powerset(n: int) -> List[List[int]]:
    powerset = []
    for size in range(n + 1):
        for set_ in itertools.combinations(range(n), size):
            powerset.append(pd.Series(list(set_)))
    return powerset

def make_powerset_patterns(n: int, bits: int = 3) -> pd.DataFrame:
    sets = make_powerset(n)

    po = PatternOverlap(np.asarray(sets))

    embeds = po.embs
    overlaps, neighbor_sets = po.get_overlaps(max_ham_distance=32)

    G = nx.convert_matrix.from_numpy_matrix(overlaps)

    umbeds = umap_embed(embeds, n_neighbors=20, umap_kwargs={"random_state": 123})
    umbeds["set"] = [list(x) for x in sets]
    return umbeds, G

def make_bokeh_figure(umbeds: pd.DataFrame, G=None) -> Figure:
    datasource = ColumnDataSource(umbeds)

    plot_figure = figure(
        title=f"UMAP projection of embedded patterns",
        plot_width=600,
        plot_height=600,
        tools=("pan, wheel_zoom, reset"),
    )

    #     plot_figure.add_layout(Legend(title="Perspective"))

    plot_figure.add_tools(
        HoverTool(
            tooltips="""

            Rows:
            @set

    """
        )
    )

    plot_figure.circle(
        "u_x",
        "u_y",
        source=datasource,
        line_alpha=0.6,
        fill_alpha=0.6,
        size=12,
    )

    if G:

        graph_renderer = from_networkx(G, nx.spring_layout)

        fixed_layout = {
            index: [row["u_x"], row["u_y"]] for index, row in umbeds.iterrows()
        }
        fixed_layout_provider = StaticLayoutProvider(graph_layout=fixed_layout)
        graph_renderer.layout_provider = fixed_layout_provider

        plot_figure.renderers.append(graph_renderer)

    return plot_figure


from __future__ import print_function
from optparse import OptionParser
import numpy as np
import ROOT
ROOT.gROOT.SetBatch(True)

usage = "usage: %prog [options] file1.root file2.root"
parser = OptionParser()
parser.add_option(
    "-l", "--label", dest="label", type="string", default="unlabeled", help="label to recognize output files"
)
parser.add_option(
    "-e", "--era", dest="era", type="string", default="era", help="era label"
)
parser.add_option(
    "--mc_files",
    dest="mc_files",
    type="string",
    default="",
    help="string containing the comma separated files with the mc efficiencies"
)
parser.add_option(
    "--mc_labels",
    dest="mc_labels",
    type="string",
    default="",
    help="string containing the comma separated files with the mc labels"
)
parser.add_option(
    "--data_files",
    dest="data_files",
    type="string",
    default="",
    help="string containing the comma separated files with the data efficiencies"
)
parser.add_option(
    "--data_labels",
    dest="data_labels",
    type="string",
    default="",
    help="string containing the comma separated files with the data labels"
)
parser.add_option(
    "--variables", dest="variables", type="string", default="pt_pfmetnomu_t1", help="comma separated variables for which plots are supposed to be created"
)
parser.add_option(
    "--variables_labels", dest="variables_labels", type="string", default="PFMETNoMu", help="comma separated variable labels for which plots are supposed to be created"
)
parser.add_option(
    "--probe_triggers", dest="probe_triggers", type="string", default="HLT_blabla", help="trigger expression for which the efficiencies are shown"
)

(options, args) = parser.parse_args()

mc_files =  options.mc_files.split(",")
mc_labels =  options.mc_labels.split(",")
data_files =  options.data_files.split(",")
data_labels =  options.data_labels.split(",")
variables = options.variables.split(",")
variables_labels = options.variables_labels.split(",")
probe_triggers = options.probe_triggers

print("mc files:\n")
print(mc_files)
print("------------------------------------------")
print("data files:\n")
print(data_files)
print("------------------------------------------")
print("variables:\n")
print(variables)

mc_effs = []
data_effs = []

for mc_file,mc_label in zip(mc_files,mc_labels):
    print(mc_file)
    rfile = ROOT.TFile.Open(mc_file,"READ")
    effs = []
    for i, (var,var_label) in enumerate(zip(variables,variables_labels)):
        eff = rfile.Get(var).Clone(mc_label+"__"+var)
        eff.SetTitle("{};{};{}".format(mc_label,var_label,"#epsilon"))
        eff.SetLineWidth(2)
        effs.append(eff)
    mc_effs.append(effs)
    rfile.Close()

print(mc_effs)

for data_file,data_label in zip(data_files,data_labels):
    print(data_file)
    rfile = ROOT.TFile.Open(data_file,"READ")
    effs = []
    for i, (var,var_label) in enumerate(zip(variables,variables_labels)):
        eff = rfile.Get(var).Clone(data_label+"__"+var)
        eff.SetTitle("{};{};{}".format(data_label,var_label,"#epsilon"))
        eff.SetLineWidth(2)
        effs.append(eff)
    data_effs.append(effs)
    rfile.Close()

print(data_effs)

era_label = ROOT.TLatex(400,1.12,options.era)
era_label.SetTextSize(0.05)

probe_triggers_label = ROOT.TLatex(20,1.04,probe_triggers)
probe_triggers_label.SetTextSize(0.04)

cms_label = ROOT.TLatex(0.2,1.17,"CMS")
wip_label = ROOT.TLatex(0.2,1.12,"work in progress")
cms_label.SetTextSize(0.05)
wip_label.SetTextSize(0.035)

# plots for different variable in same region
for mc_eff_set,data_eff_set,mc_label,data_label in zip(mc_effs,data_effs,mc_labels,data_labels):
    c=ROOT.TCanvas()
    l=ROOT.TLegend(0.6,0.33,0.9,0.5)
    line = ROOT.TLine(0,0.95,500,0.95)
    line.SetLineStyle(ROOT.kDashed)
    dummy_eff = mc_eff_set[0].Clone("dummy")
    dummy_eff.SetLineColorAlpha(0,0)
    dummy_eff.SetTitle(";METNoMu (GeV);efficiency")
    dummy_eff.Draw()
    ROOT.gPad.Update()
    dummy_eff.GetPaintedGraph().GetXaxis().SetRangeUser(0,500)
    dummy_eff.GetPaintedGraph().GetXaxis().SetTitleSize(0.05)
    dummy_eff.GetPaintedGraph().GetXaxis().SetLabelSize(0.045)
    dummy_eff.GetPaintedGraph().GetYaxis().SetTitleSize(0.05)
    dummy_eff.GetPaintedGraph().GetYaxis().SetLabelSize(0.045)
    dummy_eff.GetPaintedGraph().GetYaxis().SetTitleOffset(0.9)
    for i,mc_eff in enumerate(mc_eff_set):
        mc_eff.SetLineColor(ROOT.kRed+1*i*(i%2==0)-1*i*(i%2==1))
        mc_eff.SetMarkerStyle(20+1*i)
        mc_eff.SetMarkerColor(ROOT.kRed+1*i*(i%2==0)-1*i*(i%2==1))
        mc_eff.Draw("same")
        ROOT.gPad.Update()
        l.AddEntry(mc_eff,"{}, {}".format(mc_eff.GetTitle(),mc_eff.GetPaintedGraph().GetXaxis().GetTitle().replace("METNoMu (GeV)","")),"lp")
    for i,data_eff in enumerate(data_eff_set):
        data_eff.SetLineColor(ROOT.kBlue+1*i*(i%2==0)-1*i*(i%2==1))
        data_eff.SetMarkerStyle(20+1*i)
        data_eff.SetMarkerColor(ROOT.kBlue+1*i*(i%2==0)-1*i*(i%2==1))
        data_eff.Draw("same")
        ROOT.gPad.Update()
        l.AddEntry(data_eff,"{}, {}".format(data_eff.GetTitle(),data_eff.GetPaintedGraph().GetXaxis().GetTitle().replace("METNoMu (GeV)","")),"lp")
    l.Draw("same")
    line.Draw("same")
    cms_label.Draw("same")
    wip_label.Draw("same")
    probe_triggers_label.Draw("same")
    era_label.Draw("same")
    c.Print(options.label+"_"+mc_label.replace(" ","")+"__"+data_label.replace(" ","")+".pdf")

# plots with same variable in different regions, therefore transpose lists
mc_effs_t = np.array(mc_effs).T.tolist()
data_effs_t = np.array(data_effs).T.tolist()
mc_labels_t = np.array(mc_labels).T.tolist()
data_labels_t = np.array(data_labels).T.tolist()

for mc_eff_set,data_eff_set,var_label in zip(mc_effs_t,data_effs_t,variables_labels):
    c=ROOT.TCanvas()
    l=ROOT.TLegend(0.6,0.25,0.9,0.5)
    line = ROOT.TLine(0,0.95,500,0.95)
    line.SetLineStyle(ROOT.kDashed)
    dummy_eff = mc_eff_set[0].Clone("dummy")
    dummy_eff.SetLineColorAlpha(0,0)
    dummy_eff.SetTitle(";{};efficiency".format(var_label))
    dummy_eff.Draw()
    ROOT.gPad.Update()
    dummy_eff.GetPaintedGraph().GetXaxis().SetRangeUser(0,500)
    dummy_eff.GetPaintedGraph().GetXaxis().SetTitleSize(0.05)
    dummy_eff.GetPaintedGraph().GetXaxis().SetLabelSize(0.045)
    dummy_eff.GetPaintedGraph().GetYaxis().SetTitleSize(0.05)
    dummy_eff.GetPaintedGraph().GetYaxis().SetLabelSize(0.045)
    dummy_eff.GetPaintedGraph().GetYaxis().SetTitleOffset(0.9)
    for i,mc_eff in enumerate(mc_eff_set):
        mc_eff.SetLineColor(ROOT.kRed+1*i*(i%2==0)-1*i*(i%2==1))
        mc_eff.SetMarkerStyle(20+1*i)
        mc_eff.SetMarkerColor(ROOT.kRed+1*i*(i%2==0)-1*i*(i%2==1))
        mc_eff.Draw("same")
        ROOT.gPad.Update()
        l.AddEntry(mc_eff,"{}".format(mc_eff.GetTitle()),"lp")
    for i,data_eff in enumerate(data_eff_set):
        data_eff.SetLineColor(ROOT.kBlue+1*i*(i%2==0)-1*i*(i%2==1))
        data_eff.SetMarkerStyle(20+1*i)
        data_eff.SetMarkerColor(ROOT.kBlue+1*i*(i%2==0)-1*i*(i%2==1))
        data_eff.Draw("same")
        ROOT.gPad.Update()
        l.AddEntry(data_eff,"{}".format(data_eff.GetTitle()),"lp")
    l.Draw("same")
    line.Draw("same")
    cms_label.Draw("same")
    wip_label.Draw("same")
    probe_triggers_label.Draw("same")
    era_label.Draw("same")
    c.Print(options.label+"_"+var_label.replace(" ","").replace("(GeV)","")+".pdf")


# --*-- encoding: utf-8 --*--

count = 0

file = open("data/input.txt")
data = file.readlines()
file.close()

windowed = []

for (index, value) in enumerate(data):
    if (index > 1) and (index < len(data)):
        current, previous, antePrevious = value, data[index - 1], data[index - 2]
        print(antePrevious, previous,current, int(current) + int(previous) + int(antePrevious))
        windowed.append(int(current) + int(previous) + int(antePrevious))
    # if(index > 20):
    #     break

for (index, value) in enumerate(windowed):
    if (index < len(data)):
        current, previous = value, windowed[index - 1]
        print(previous, current, current > previous)
        if(int(current) > int(previous)):
            count +=1

print(count)


from django.core.management.base import BaseCommand, CommandError

from core.models import User
from core.service.auth_service import merge_users, reset_creation_dates

class Command(BaseCommand):
    help = "Merges all users with the provided email in case-insensitive terms"

    def add_arguments(self, parser):
        parser.add_argument(
            "email", type=str, help="The exact email of the user account"
        )

    def handle(self, *args, **options):
        email = options["email"].lower()

        all_users = User.objects.filter(email__iexact=email)
        if all_users.count() == 0:
            raise CommandError("No user with email %s exists" % email)
        all_users = list(all_users)
        main_user = all_users.pop()

        for user in all_users:
            self.style.SUCCESS(
                "Merging user %s into user %s" % (str(user), str(main_user))
            )
            merge_users(from_user=user, into_user=main_user)

        main_user.email = main_user.email.lower()
        main_user.save()

        if main_user.player:
            reset_creation_dates(main_user.player)


import hashlib
import logging
import os
import re
import shutil

import distro

from config import Config
from package_manager.apt import Apt
from package_manager.pacman import Pacman
from utils import file_exists, file_info

used_exclude_files = []
used_content_filters = []

def get_base_distribution():
    base_distribution = distro.like()
    if base_distribution == '':
        base_distribution = distro.id()
    if base_distribution == '':
        logging.fatal(
            'Unable to detect your distribution. Please file a bug with the following information included:\n%s',
            distro.linux_distribution(full_distribution_name=False))
        exit(1)
    return base_distribution

def get_system_package_manager():
    base_distribution = get_base_distribution()
    logging.debug('Detected \'%s\' as base distribution', base_distribution)
    distribution_package_manager_map = {
        'arch': Pacman(),
        'debian': Apt(),
    }
    package_manager = distribution_package_manager_map.get(base_distribution)
    logging.debug('Using \'%s\' as package manager', package_manager.name)
    return package_manager

def get_installed_not_listed_packages(listed_packages: [str], package_manager) -> [str]:
    installed_not_listed_packages: [str] = []
    installed_packages = package_manager.get_packages()
    for installed_package in installed_packages:
        found = False
        for listed_package in listed_packages:
            if installed_package == listed_package:
                found = True
        if not found and package_manager.is_package_explicitly_installed(installed_package):
            logging.debug(f'\'{installed_package}\' installed but not listed')
            installed_not_listed_packages.append(installed_package)
    return installed_not_listed_packages

def get_listed_not_installed_packages(listed_packages: [str], package_manager) -> [str]:
    listed_not_installed_packages: [str] = []
    installed_packages = package_manager.get_packages()
    for listed_package in listed_packages:
        found = False
        for installed_package in installed_packages:
            if listed_package == installed_package:
                found = True
        if not found:
            logging.debug(f'\'{listed_package}\' listed but not installed')
            listed_not_installed_packages.append(listed_package)
    return listed_not_installed_packages

def get_etc_files(exclude_files: [str]):
    all_files = []
    for root, sub_dirs, files in os.walk('/etc'):
        for file in files:
            file_path = os.path.join(root, file)
            if not check_is_excluded(file_path, exclude_files):
                all_files.append(file_path)
    return all_files

def get_modified_not_listed_files(config: Config, exclude_files: [str], registered_files: {}, hash_method: str) -> {}:
    modified_files = []
    new_files = get_etc_files(exclude_files).copy()

    for path in registered_files:
        if check_is_excluded(path, exclude_files):
            continue
        if path in new_files:
            new_files.remove(path)

        should_hash_sum = registered_files[path]

        # We got an empty hash that means the file is a symlink
        if should_hash_sum == '':
            continue

        if not verify_hash(path, should_hash_sum, hash_method):
            modified_files.append(path)

    for new_file in new_files:
        modified_files.append(new_file)

    files = {}
    for file in modified_files:
        if file not in config.files:
            files[file] = file_info(file)

    return files

def get_listed_not_modified_files(config: Config, exclude_files: [str], registered_files: {}, hash_method: str) -> {}:
    not_modified_files = []

    for path in config.files:
        if check_is_excluded(path, exclude_files):
            not_modified_files.append(path)
            continue

        if not file_exists(path, follow_symlinks=False):
            not_modified_files.append(path)

        if path in registered_files:
            should_hash_sum = registered_files[path]

            # We got an empty hash that means the file is a symlink
            if should_hash_sum == '':
                continue

            if verify_hash(path, should_hash_sum, hash_method):
                not_modified_files.append(path)

    return not_modified_files

def verify_hash(path, should_hash_sum, hash_method):
    file_hash = None
    if hash_method == 'sha256':
        file_hash = hashlib.sha256()
    elif hash_method == 'md5':
        file_hash = hashlib.md5()
    else:
        logging.fatal(f'Unsupported hash method: {hash_method}')
        exit(1)
    with open(path, 'rb') as file:
        fb = file.read(65536)
        while len(fb) > 0:
            file_hash.update(fb)
            fb = file.read(65536)
    is_hash_sum = file_hash.hexdigest()
    return is_hash_sum == should_hash_sum

def save_files(config_directory: str, preset: str, files: {}, content_filters: [], relative_path: str):
    files_path = os.path.join(config_directory, f'{preset}_files')
    if os.path.exists(files_path):
        shutil.rmtree(files_path)
    os.mkdir(files_path)
    stat_info = os.stat(os.path.join(config_directory, '..'))
    for path in files:
        logging.debug(f'Saving {path}')
        local_path = os.path.join(files_path, path.strip('/'))
        local_dir = os.path.dirname(local_path)
        if not os.path.exists(local_dir):
            os.makedirs(local_dir)
        absolute_path = os.path.join(relative_path, path)
        if file_exists(absolute_path, follow_symlinks=False):
            if os.path.islink(absolute_path):
                shutil.copy(absolute_path, local_path, follow_symlinks=False)
            else:
                content = read_filtered_content(absolute_path, content_filters, relative_path)
                if isinstance(content, str):
                    with open(local_path, 'w') as file:
                        file.write(content)
                else:
                    with open(local_path, 'wb') as file:
                        file.write(content)
                # So we are not annoying with the permissions for users
                os.chown(local_path, stat_info.st_uid, stat_info.st_gid)

def apply_files(config_directory: str, preset: str, files: {}, relative_path: str):
    files_path = os.path.join(config_directory, f'{preset}_files')
    if not os.path.exists(files_path):
        if len(files) > 0:
            logging.fatal(f'Could not find required {preset}_files directory')
            exit(1)
        return
    for path in files:
        logging.debug(f'Applying {path}')
        local_path = os.path.join(files_path, path.strip('/'))
        if file_exists(local_path, follow_symlinks=False):
            uid = int(files[path].split(':')[0])
            gid = int(files[path].split(':')[1])
            mode = int('100' + files[path].split(':')[2], 8)
            absolute_path = os.path.join(relative_path, path)
            if file_exists(absolute_path, follow_symlinks=False):
                os.remove(absolute_path)
            else:
                logging.debug(f'File {path} was not available on the system')
            if not file_exists(os.path.dirname(absolute_path), follow_symlinks=False):
                os.makedirs(os.path.dirname(absolute_path))
                current_path = os.path.dirname(absolute_path)
                while current_path != os.path.abspath(relative_path):
                    _set_permission(current_path, None, uid, gid)
                    current_path = os.path.dirname(current_path)
            shutil.copy(local_path, absolute_path, follow_symlinks=False)
            _set_permission(absolute_path, mode, uid, gid)
        else:
            logging.fatal(f'Could not find required {preset}_files{path} file')
            exit(1)

def _set_permission(path, mode, uid, gid):
    if not os.path.islink(path):
        if mode is not None:
            try:
                os.chmod(path, mode)
            except PermissionError:
                logging.warning(
                    f'Unable to chmod {path}. This could be because it is a symlink or because you have set the '
                    f'wrong permissions on the file')
        try:
            os.chown(path, uid, gid)
        except PermissionError:
            logging.warning(
                f'Unable to chown {path}. This could be because it is a symlink or because you have set the '
                f'wrong permissions on the file')

def get_available_not_listed_files(config: Config, exclude_files: [str]):
    not_listed_files = []
    home_dir = os.path.expanduser('~')
    all_files = []

    exclude_files_expanded = []
    for exclude_file in exclude_files:
        exclude_files_expanded.append(os.path.join(home_dir, exclude_file))

    for name in os.listdir(home_dir):
        if name.startswith('.'):
            if os.path.isfile(os.path.join(home_dir, name)):
                all_files.append(os.path.join(home_dir, name))
    for root, _, files in os.walk(os.path.join(home_dir, '.config')):
        for file in files:
            all_files.append(os.path.join(root, file))

    for path in all_files:
        if check_is_excluded(path, exclude_files_expanded):
            continue
        listed = False
        for file in config.files:
            if os.path.join(home_dir, file) == path:
                listed = True
                break
        if not listed:
            not_listed_files.append(path)

    files = {}
    for file in not_listed_files:
        if file not in config.files:
            files[os.path.relpath(file, home_dir)] = file_info(file)

    return files

def get_listed_not_available_files(config: Config, exclude_files: [str]):
    not_available_files = []
    home_dir = os.path.expanduser('~')

    for path in config.files:
        path = os.path.join(home_dir, path)
        if check_is_excluded(path, exclude_files):
            not_available_files.append(path)
            continue

        if not file_exists(path, follow_symlinks=False):
            not_available_files.append(os.path.relpath(path, home_dir))

    return not_available_files

def check_is_excluded(path: str, exclude_files: [str]) -> bool:
    for exclude_file in exclude_files:
        if _path_match_pattern(path, exclude_file):
            if exclude_file not in used_exclude_files:
                used_exclude_files.append(exclude_file)
            logging.debug(f'Excluding {path} because of exclude rule {exclude_file}')
            return True
    return False

def _path_match_pattern(path, pattern):
    return re.match(f'^{re.sub(r"/$", "/.*", pattern.replace("*", ".*"))}$', path)

def get_listed_not_used_exclude_files(config, relative_path):
    listed_not_used_exclude_files = []
    for exclude_file in config.exclude_files:
        if os.path.join(relative_path, exclude_file) not in used_exclude_files:
            listed_not_used_exclude_files.append(exclude_file)
    return listed_not_used_exclude_files

def read_filtered_content(path: str, content_filters: [], relative_path: str):
    filtered_content = []
    matching_content_filters = []
    for content_filter in content_filters:
        filter_path = list(content_filter.keys())[0]
        if _path_match_pattern(path, os.path.join(relative_path, filter_path)):
            matching_content_filters.append(content_filter)
    try:
        with open(path, 'r') as file:
            content = file.read()
            for line in content.split('\n'):
                do_filter = False
                for content_filter in matching_content_filters:
                    if re.match(f'^{content_filter[list(content_filter.keys())[0]]}$', line):
                        if path not in used_content_filters:
                            used_content_filters.append(content_filter)
                        do_filter = True
                        break
                if not do_filter:
                    filtered_content.append(line)
        return '\n'.join(filtered_content)
    except UnicodeDecodeError:
        with open(path, 'rb') as file:
            return file.read()

def get_listed_not_used_content_filters(config):
    listed_not_used_content_filters = []
    for content_filter1 in config.content_filters:
        used = False
        path1 = list(content_filter1.keys())[0]
        for content_filter2 in used_content_filters:
            path2 = list(content_filter2.keys())[0]
            if path1 == path2 and content_filter1[path1] == content_filter2[path2]:
                used = True
        if not used:
            listed_not_used_content_filters.append(content_filter1)
    return listed_not_used_content_filters


def rotate(str1,str2):
    rotated = list(str1)
    for i in range(2):
        popped = rotated.pop(0)
        rotated.append(popped)
    print("".join(rotated))
    if (("".join(rotated))== str2):
        return True
    for i in range(2):
        popped = rotated.pop()
        rotated.insert(0,popped)
    print("".join(rotated))
    if (("".join(rotated)) == str2):
        return True
    return False

result = rotate("amazon","azonam")
print(result)


from django.contrib import admin
from .models import (
    User, Subject, Quiz, Question, Answer, Student, TakenQuiz, StudentAnswer
)

@admin.register(User)
class UserAdmin(admin.ModelAdmin):
    list_display = ['username', 'is_student', 'is_teacher']
    list_filter = ['is_student', 'is_teacher']
    search_fields = ['username']
    ordering = ['username']
    fieldsets = [
        ['User Information', {'fields': ['username', 'password']}],
        ['Permissions', {'fields': ['is_student', 'is_teacher']}],
    ]

@admin.register(Subject)
class SubjectAdmin(admin.ModelAdmin):
    list_display = ['name', 'color']
    list_filter = ['name']
    search_fields = ['name']
    ordering = ['name']
    fieldsets = [
        ['Subject Information', {'fields': ['name', 'color']}],
    ]

@admin.register(Quiz)
class QuizAdmin(admin.ModelAdmin):
    list_display = ['name', 'subject', 'owner']
    list_filter = ['subject', 'owner']
    search_fields = ['name']
    ordering = ['name']
    fieldsets = [['Quiz Information', {'fields': ['name', 'subject', 'owner']}],
                 ]

@admin.register(Question)
class QuestionAdmin(admin.ModelAdmin):
    list_display = ['text', 'quiz']
    list_filter = ['quiz']
    search_fields = ['text']
    ordering = ['text']
    fieldsets = [['Question Information', {'fields': ['text', 'quiz']}],
                 ]

@admin.register(Answer)
class AnswerAdmin(admin.ModelAdmin):
    list_display = ['text', 'question', 'is_correct']
    list_filter = ['is_correct', 'question']
    search_fields = ['text']
    ordering = ['text']
    fieldsets = [['Answer Information', {'fields': ['text', 'question', 'is_correct']}],
                 ]

@admin.register(Student)
class StudentAdmin(admin.ModelAdmin):
    list_display = ['user', 'score']
    list_filter = ['score']
    search_fields = ['user__username']
    ordering = ['user__username']
    fieldsets = [['Student Information', {'fields': ['user', 'score']}],
                 ]

@admin.register(TakenQuiz)
class TakenQuizAdmin(admin.ModelAdmin):
    list_display = ['student', 'quiz', 'score', 'percentage', 'date']
    list_filter = ['quiz', 'score', 'percentage', 'date']
    search_fields = ['student__user__username']
    ordering = ['student__user__username']
    fieldsets = [['Taken Quiz Information', {'fields': ['student', 'quiz', 'score', 'percentage', 'date']}], ]

@admin.register(StudentAnswer)
class StudentAnswerAdmin(admin.ModelAdmin):
    list_display = ['student', 'answer']
    list_filter = ['answer']
    search_fields = ['student__user__username', 'answer__text']
    ordering = ['student__user__username']
    fieldsets = [
        ['Student Answer Information', {'fields': ['student', 'answer']}],
    ]


filmes_assistidos = {"Matrix", "A viagem de Chihiro", "X"}
filme_usuario = input("Digite o filme que voce assistiu: ")

if filme_usuario in filmes_assistidos:
    print("O seu filme está na lista")
else:
    print("O seu filme não está na lista")

number = 7
user_input=input("Enter'y'if you would like to play:").lower()
if user_input == "y":
    user_number = int(input("Guess our number:"))
    if user_number == number:
        print("You guessed correctly!")
    elif number - user_number in (1, -1):
        #ou podemos usar abs(numer - number) == 1, onde o abs sempre dará o valor absoluto (caso seja -1, se torna 1)
        print("You were off by one.")
    else:
        print("Sorry,it's wrong!")
else:
    print("Be back when you decide to play our game! ;)")


import os
import json
import argparse
from tqdm import tqdm
from transformers import BertTokenizer, BertModel
from transformers import XLNetTokenizer

def init_arg_parser():
    arg_parser = argparse.ArgumentParser()

    arg_parser.add_argument('--data_path', default='orig_data')
    arg_parser.add_argument('--temp_data', default='temp_data')
    arg_parser.add_argument('--bert_path', default='/home/lishuan/pretrain_model/chinese_xlnet_base/')
    arg_parser.add_argument('--label_info', default={
        '解除质押': 0, '股份回购': 1, '股东减持': 2, '亏损': 3, '中标': 4, '高管变动': 5, '企业破产': 6,
        '股东增持': 7, '被约谈': 8, '企业收购': 9, '公司上市': 10, '企业融资': 11, '质押': 12
    })
    args = arg_parser.parse_args()
    return args

def data_pro(args):
    train_path = os.path.join(args.data_path, 'duee_fin_train.json')
    dev_path = os.path.join(args.data_path, 'duee_fin_dev.json')

    train_data = data_read(train_path)
    dev_data = data_read(dev_path)
    p(train_data)
    print(len(dev_data))

    train_ids, train_data_info = convert_text_to_id(train_data, args)
    dev_ids, dev_data_info = convert_text_to_id(dev_data, args)

    print(train_data_info, '\n', dev_data_info)

    train_ids_path = os.path.join(args.temp_data, 'xlnet_train_event_ids.json')
    dev_ids_path = os.path.join(args.temp_data, 'xlnet_dev_event_ids.json')

    data_save(train_ids, train_ids_path)
    data_save(dev_ids, dev_ids_path)

def data_read(path):
    text_data = []
    with open(path, mode="r", encoding="utf-8") as f:
        for line in f:
            text_data.append(json.loads(line))
    return text_data

def convert_text_to_id(text_data, args):
    tokenizer = XLNetTokenizer.from_pretrained(args.bert_path)
    data_ids = []
    data_info = {label_str: 0 for label_str in args.label_info.keys()}
    for sample in tqdm(text_data):
        if 'event_list' not in sample.keys() or 'event_type' not in sample['event_list'][0].keys():
            continue
        input_info = tokenizer.encode_plus(sample['text'], add_special_tokens=True)
        data_ids.append(
            [input_info['input_ids'], input_info['token_type_ids'],
             input_info['attention_mask'], args.label_info[sample['event_list'][0]['event_type']]]
        )
        data_info[sample['event_list'][0]['event_type']] += 1
    return data_ids, data_info

def data_save(data, path):
    with open(path, mode="a", encoding="utf-8") as f:
        json.dump(data, f)
    f.close()

def p(data, num=5):
    for i in range(num):
        print(data[i])

if __name__ == '__main__':
    args = init_arg_parser()
    data_pro(args)


###.1

# import sys
#
# from collections import deque
#
# N,M = map(int,sys.stdin.readline().split())
#
# adjacency_list = [[] for i in range(N+1)]
#
# for i in range(M):
#
#     a,b = map(int,sys.stdin.readline().split())
#
#     adjacency_list[b].append(a)
#
#
# def dfs(start):
#
#     visited = [0]*(N+1)
#
#     count = 0
#
#     stack = deque([start])
#
#     while stack:
#
#         current = stack.pop()
#
#         visited[current] = 1
#
#         for i in adjacency_list[current]:
#
#             if visited[i] == 0:
#
#                 stack.append(i)
#
#                 count = count + 1
#
#     return count
#
# result = []
#
# for j in range(1,N+1):
#
#     output = dfs(j)
#
#     result.append(output)
#
# max_count = 0
#
# box = []
#
# for i in range(N):
#
#     if max_count <= result[i]:
#
#         max_count = result[i]
#
#         box.append(i+1)
#
#     else:
#
#         continue
#
# for m in sorted(box):
#
#     print(m, end=' ')

###.2

import sys

from collections import deque

N,M = map(int,sys.stdin.readline().split())

adjacency_list = [[] for i in range(N+1)]

for i in range(M):

    a,b = map(int,sys.stdin.readline().split())

    adjacency_list[b].append(a)

def bfs(start):

    queue = deque([start])

    visited = [0] * (N + 1)

    visited[start] = 1

    count = 0

    while queue:

        current = queue.popleft()

        count = count + 1

        for i in adjacency_list[current]:

            if visited[i] == 0:

                visited[i] = 1

                queue.append(i)

            else:

                continue

    return count

result = []

for m in range(1,N+1):

    output = bfs(m)

    result.append(output)

for index,value in enumerate(result):

    if value == max(result):

        print(index+1,end=' ')

# 방문처리를 하는 코드는 반드시 큐에 방문할 노드를 집어넣기 전해줘야 한다. 이렇게 하지 않을 경우 간선관계가 많은 노드, 즉 인기가 많은 노드가 중복적으로 큐에 담겨서 중복 방문하는 것처럼 표시되는 잘못된 경우가 발생할 수 있다.
# 반례: 1->2,1->3,1->4,2->5,3->5,4->5,5->6,5->7 과 같이 5노드가 다른 노드와의 간선 관계까 많이 형성되어 있을 때, 5노드가 큐에 담기기전에 방문처리를 하지 않게 되면, 5가 큐에 담기고 나서도 방문처리가 아직 안된상황이 발생하여 5가 중복하여 담겨질 수 있다.


# https://leetcode.com/problems/subsets/

class Solution:
    def subsets(self, nums: list[int]) -> list[list[int]]:
        """Return all possible subsets (the power set)."""

        all_subs = []
        empty = []

        def backtrack(index):
            if index == len(nums):
                all_subs.append(empty.copy())
                return

            empty.append(nums[index])
            backtrack(index + 1)
            empty.pop()
            backtrack(index + 1)

        backtrack(0)

        return all_subs

if __name__ == '__main__':
    sol = Solution()
    x = [1, 2, 3,]
    print(sol.subsets(x))


import numpy as np
import pandas as pd
#import sys
#from matplotlib import pyplot as plt
#from matplotlib import style
#style.use("ggplot")
#from openpyxl import *
from sklearn import svm
import tkinter
from tkinter import filedialog
import openpyxl
#----------------机器学习:用向量机进行匹配---------------
def getsvm(trainSet,tmpltSet,ptnset):
    clf = svm.SVC(kernel='linear', C = 1.0)
    clf.fit(trainSet, tmpltSet)
    result = clf.predict(ptnset)
    return result
#------------------获取excel文件路径---------------
def getExcelFilePath():
    while True:
        question = input("Do you want to get a excel file?(yes(y)/no(n)")[0]
        if question in ["y", "Y"]:
            win = tkinter.Tk()
            win.withdraw()
            path = filedialog.askopenfilename().replace("/", "\\")
            if path[-4:] != "xlsx":
                print("The file is not a excel file, try again:")
                continue
            print("The excel %s input successfully!" %path)
            return path
        elif question in ["n", "N"]:
            print("Exiting......")
            break
        else:
            print("The choice is wrong, try again:")

#----- get trainSet, ptnSet and tmpltSet-----
def getSetDatas(data, selectItemName):
    #SourceData = pd.read_excel(path, sheet_name=sheetName, engine="openpyxl")
    popItem = data.drop(index=selectItemName)

    #Train set
    TrainSetArray = np.array(popItem)

    #TmpltSet
    TmpltSet = popItem.index
    TmpltSetArray = np.array(TmpltSet)

    #PtnSet
    PtnSet = data.loc[selectItemName]
    PtnSetArray = np.array([PtnSet])

    result = (TrainSetArray, TmpltSetArray, PtnSetArray)

    return result

#----------------write to Excel File--------------------
def writeToExcelFile(path, resultList):
    selectedHerbName = resultList[:, 0]
    attribute_selected = resultList[:, 1]
    matchedHerbName = resultList[:, 2]
    attribute_matched  = resultList[:, 3]
    dataResult = pd.DataFrame({"待测药":selectedHerbName, "待测药趋向性":attribute_selected, "匹配药":matchedHerbName, "匹配药趋向性":attribute_matched})
    wb = openpyxl.load_workbook(path)
    writer = pd.ExcelWriter(path, engine="openpyxl")
    writer.book = wb
    dataResult.to_excel(writer, sheet_name="result", columns=None)
    writer.save()
    writer.close()
    print("Write to the excel file successfully!")

#---------------handle source data--------------
def handleSourceData(path, sheetName):
    with pd.ExcelFile(path) as xlsbook:
        SourceData = pd.read_excel(xlsbook, sheet_name=sheetName)
        SourceData = SourceData.set_index("中药名称").loc[:, "趋向性":]
        SourceData = SourceData.dropna(axis=0) #除去含有缺失值的行
        #SourceData = SourceData.dropna(axis=1) #除去含有缺失值的列
    return SourceData

#---------------main function-------------------
def main():
    sheetName = 'GdaSet'
    path = getExcelFilePath()
    Data = handleSourceData(path, sheetName)
    resultList = []
    itemNames = list(Data.index)
    for item in itemNames:
        attribute_item = int(Data.loc[item, ["趋向性"]].values[0])
        dataSample = Data.loc[:, "C数":]
        setDatas = getSetDatas(dataSample, item)
        matchResult = getsvm(setDatas[0], setDatas[1], setDatas[2])[0]
        attribute_result = int(Data.loc[matchResult, ["趋向性"]].values[0])
        print("The herb:{0:}, attribute_item:{1:}, match result: {2:}, attribute_result:{3:}\n".format(item, attribute_item, matchResult, attribute_result))
        resultList.append([item, attribute_item, matchResult, attribute_result])
    writeToExcelFile(path, np.array(resultList))

if __name__ == "__main__":
    main()


# -*- coding:utf-8 -*-
'''
有时候我们需要的并不是最后的结果，而是中间某一层的结果，这个例子演示如何得到中间结果
'''
from keras.layers import Dense,Activation,Input
from keras.models import  Sequential,Model
import  numpy as np

x=np.random.standard_normal(size=(200,12))
y=np.random.randint(low=0,high=2,size=(200,1))
# this is your initial model
input=Input(shape=(12,))
hidden_1=Dense(20,activation='tanh')(input)
hidden_2=Dense(64,activation='tanh',name='hidden_2')(hidden_1)
hidden_3=Dense(1,activation='softmax')(hidden_2)
model=Model(input=input,output=hidden_3)

# we train it
model.compile(loss='mse',optimizer='sgd')
model.fit(x,y,epochs=20,batch_size=10)

# we build a new model with the activations of old model
# this model is truncted after the first layer
test_x=np.random.standard_normal(size=(1,12))

model2= Model(inputs=model.input, outputs=model.get_layer('hidden_2').output)
activations=model2.predict(test_x)
print(activations)


# pip install bertopic

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

import nltk
import gensim
import gensim.corpora as corpora
import pandas as pd
import numpy as np
from bertopic import BERTopic
from sentence_transformers import SentenceTransformer
from umap import UMAP
from hdbscan import HDBSCAN
from nltk.stem import WordNetLemmatizer
from nltk.tokenize import word_tokenize
from sklearn.feature_extraction.text import CountVectorizer
from bertopic.vectorizers import ClassTfidfTransformer
from gensim.models.coherencemodel import CoherenceModel

nltk.download('wordnet')
nltk.download('omw-1.4')
nltk.download('punkt')

colnames=['name', 'description', 'price', 'link']
docs1 = pd.read_csv('appliance.csv',names=colnames, header=None)
docs2 = pd.read_csv('laptops.csv',names=colnames, header=None)
docs3 = pd.read_csv('phones.csv',names=colnames, header=None)
docs4 = pd.read_csv('sneakers.csv',names=colnames, header=None, sep=';')
docs5 = pd.read_csv('toys.csv',names=colnames, header=None, sep=';')
docs6 = pd.read_csv('makiyazh.csv',names=colnames, header=None, sep=';')
docs7 = pd.read_csv('reduced_data.csv',names=colnames, header=None)

docs = docs1.append(docs2, ignore_index=True)
docs = docs.append(docs3, ignore_index=True)
docs = docs.append(docs3, ignore_index=True)
docs = docs.append(docs4, ignore_index=True)
docs = docs.append(docs5, ignore_index=True)
docs = docs.append(docs6, ignore_index=True)
docs = docs.append(docs7, ignore_index=True)

# docs['comb'] = docs['name'] + " " + docs['description']
docs['comb'] = docs['name']

docs.head()

filtered_text = []
lemmatizer = WordNetLemmatizer()

for w in docs['comb']:
  filtered_text.append(lemmatizer.lemmatize(w))
print(filtered_text[:1])
docs['filtered'] = filtered_text

embedding_model = SentenceTransformer("all-MiniLM-L6-v2")
umap_model = UMAP(n_neighbors=20, n_components=5, min_dist=0.0, metric='cosine')
hdbscan_model = HDBSCAN(min_cluster_size=40,metric='euclidean', cluster_selection_method='eom', prediction_data=True)
vectorizer_model = CountVectorizer()
ctfidf_model = ClassTfidfTransformer()

docs.head()

topic_model= BERTopic(language = "multilingual", top_n_words = 10, n_gram_range = (1,2), calculate_probabilities=True, verbose=True,
  embedding_model=embedding_model,
  umap_model=umap_model,
  hdbscan_model=hdbscan_model,
  vectorizer_model=vectorizer_model,
  ctfidf_model=ctfidf_model,
  nr_topics = "auto"
          )
topics, probs = topic_model.fit_transform(docs['filtered'])

# topic_model.update_topics(list(docs['filtered']), n_gram_range=(1, 2), top_n_words = 10)

hierarchical_topics = topic_model.hierarchical_topics(docs['filtered'])

hierarchical_topics

topic_model.visualize_hierarchy(hierarchical_topics=hierarchical_topics)

tree = topic_model.get_topic_tree(hierarchical_topics)
print(tree)

representative_docs = topic_model.get_representative_docs()
representative_docs

docs['Topic'] = topics

docs.head()

freq = topic_model.get_topic_info()
freq

topic_model.get_topic(0)

topic_model.visualize_hierarchy(top_n_topics=50)

topic_model.visualize_heatmap(n_clusters=20, width=1000, height=1000)

# from sklearn.metrics.pairwise import cosine_similarity
# sim_matrix = cosine_similarity(topic_model.c_tf_idf_)
# df = pd.DataFrame(sim_matrix, columns=topic_model.topic_labels_.values(), index=topic_model.topic_labels_.values())
# df
# # distance_matrix = cosine_similarity(np.array(topic_model.topic_embeddings)[1:, :])

topic_model.visualize_barchart(top_n_topics=len(freq))

topic_model.visualize_topics()

topic_model.save("my_model")

test_docs = "adidas кроссовки"
predicted_topics, predicted_probs = topic_model.transform(test_docs)
print(f'Predicted topic is {predicted_topics}, and the probability is {np.round(predicted_probs,2)}')

new_review ="adidas кроссовки"
similar_topics, similarity = topic_model.find_topics(new_review, top_n=5)
print(f'The most similar child topic is {similar_topics}, and the similarities is {np.round(similarity,2)}')


import pandas as pd
from db_funcs import create_tables
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base

from funcs_filters import *
from funcs_plotsettings import *
from funcs_plots import *

def backend(price_input_1, price_input_2, year_input_1, year_input_2,
            mileage_input_1, mileage_input_2, power_input_1, power_input_2,
            fuel_input, brand_input, in_0_0, in_0_1, in_0_2, in_0_3, in_1_0,
            in_1_1, in_1_2, in_1_3, link_search_in):

    Base = declarative_base()
    Offers, MyOffers, engine = create_tables(Base)
    Session = sessionmaker(bind=engine)
    session = Session()
    output = session.query(MyOffers).all()
    session.close()
    # brands_list = [off.brand for off in output]
    df = pd.DataFrame({"Price": [int(off.price) for off in output],
                       "Year": [off.prod_year for off in output],
                       "Mileage": [off.mileage for off in output],
                       "Power": [int(off.power) for off in output],
                       "Fuel": [off.fuel for off in output],
                       "Brand": [off.brand for off in output],
                       "Longitude": [off.longitude for off in output],
                       "Latitude": [off.latitude for off in output],
                       "Link": [off.link for off in output]})
    df = df[df["Power"] > 0]

    # df = pd.read_csv("offers.csv", sep=';')

    min_price = min(df["Price"])
    max_price = max(df["Price"])
    min_year = min(df["Year"])
    max_year = max(df["Year"])
    min_mil = int(min(df["Mileage"]))
    max_mil = int(max(df["Mileage"]))
    min_power = int(min(df["Power"]))
    max_power = int(max(df["Power"]))

    # I. Filters
    # 1. Price
    price_output = "Cena: "
    price_style_dict = {"width": "15%", "position": "absolute", "left": "1%",
                        "top": "2%", "height": "10%", "font-size": 18,
                        "font-family": "Cambria"}
    error_message = "Minimalna cena jest większa od maksymalnej"

    price_output, price_min_limit, price_max_limit \
        = input_hint(price_output, price_style_dict, price_input_1,
                     price_input_2, min_price, max_price, error_message, "zł",
                     1)

    ph_1_1 = "Min. cena (od {})".format(min_price)
    ph_1_2 = "Max. cena (od {})".format(max_price)
    ph_2_1 = "Min. rok produkcji (od {})".format(min_year)
    ph_2_2 = "Max. rok produkcji (od {})".format(max_year)
    ph_3_1 = "Min. przebieg (od {})".format(min_mil)
    ph_3_2 = "Max. przebieg (od {})".format(max_mil)
    ph_4_1 = "Min. moc (od {})".format(min_power)
    ph_4_2 = "Max. moc (od {})".format(max_power)

    # 2. Production year
    year_output = "Rok produkcji: "
    year_style_dict = {"width": "15%", "position": "absolute", "left": "18%",
                       "top": "2%", "height": "10%", "font-size": 18,
                       "font-family": "Cambria"}
    error_message = "Minimalny rok produkcji jest większy od maksymalnego"

    year_output, year_min_limit, year_max_limit \
        = input_hint(year_output, year_style_dict, year_input_1,
                     year_input_2, min_year, max_year, error_message, "", 0)

    # 3. Mileage
    mileage_output = "Przebieg: "
    mileage_style_dict = {"width": "15%", "position": "absolute",
                          "left": "37%", "top": "2%", "height": "10%",
                          "font-size": 18, "font-family": "Cambria"}
    error_message = "Minimalny przebieg jest większy od maksymalnego"

    mileage_output, mileage_min_limit, mileage_max_limit \
        = input_hint(mileage_output, mileage_style_dict, mileage_input_1,
                     mileage_input_2, min_mil, max_mil, error_message, "km",
                     1)

    # 4. Power
    power_output = "Moc: "
    power_style_dict = {"width": "15%", "position": "absolute",
                        "left": "56%", "top": "2%", "height": "10%",
                        "font-size": 18, "font-family": "Cambria"}
    error_message = "Minimalna moc jest większa od maksymalnej"

    power_output, power_min_limit, power_max_limit \
        = input_hint(power_output, power_style_dict, power_input_1,
                     power_input_2, min_power, max_power, error_message, "KM",
                     1)

    # 5. Fuel
    fuel_output = "Paliwo:"

    # 6. Brand
    brand_output = "Marka: "
    if brand_input:
        brand_output += brand_input

    # 7. Number of offers
    if price_input_1:
        df = df[df["Price"] >= int(price_input_1)]
    if price_input_2:
        df = df[df["Price"] <= int(price_input_2)]
    if year_input_1:
        df = df[df["Year"] >= int(year_input_1)]
    if year_input_2:
        df = df[df["Year"] <= int(year_input_2)]
    if mileage_input_1:
        df = df[df["Mileage"] >= int(mileage_input_1)]
    if mileage_input_2:
        df = df[df["Mileage"] <= int(mileage_input_2)]
    if power_input_1:
        df = df[df["Power"] >= int(power_input_1)]
    if power_input_2:
        df = df[df["Power"] <= int(power_input_2)]

    df = df[df["Fuel"].isin(fuel_input)]
    if brand_input != "wszystkie":
        df = df[df["Brand"] == brand_input]

    n_of_offs = str(len(df))
    n_of_offs_out = "Wybrano ofert: " + n_of_offs

    # II. Plots preparation

    # 1. Visibility
    base_style_dict = {"left": "0%", "width": "60%"}
    style_0_1 = base_style_dict.copy()
    style_0_2 = base_style_dict.copy()
    style_0_3 = base_style_dict.copy()
    style_1_1 = base_style_dict.copy()
    style_1_2 = base_style_dict.copy()
    style_1_3 = base_style_dict.copy()

    # 2. Dropdown options
    numerical_labels = ["Cena", "Przebieg", "Moc"]
    numerical_values = ["Price", "Mileage", "Power"]
    categorical_labels = ["Rok", "Paliwo", "Marka"]
    categorical_values = ["Year", "Fuel", "Brand"]

    out_0_1, out_0_2, out_0_3, style_0_1, style_0_2, style_0_3 \
        = get_dropdown_options(in_0_0, style_0_1, style_0_2, style_0_3,
                               numerical_labels, categorical_labels,
                               numerical_values, categorical_values)
    out_1_1, out_1_2, out_1_3, style_1_1, style_1_2, style_1_3 \
        = get_dropdown_options(in_1_0, style_1_1, style_1_2, style_1_3,
                               numerical_labels, categorical_labels,
                               numerical_values, categorical_values)

    # III. Plots creation
    plot_type_1 = get_plot_type(in_0_0, in_0_1, in_0_2)
    plot_type_2 = get_plot_type(in_1_0, in_1_1, in_1_2)

    fig_1 = create_plot(plot_type_1, df, in_0_1, in_0_2, in_0_3)
    fig_2 = create_plot(plot_type_2, df, in_1_1, in_1_2, in_1_3)

    # IV. Link search
    if not link_search_in:
        link_search_out = ""
    else:
        try:
            link_search_out = df["Link"].loc[int(link_search_in)]
        except:
            link_search_out = "Błędny numer id"

    return [price_output, price_style_dict, ph_1_1, ph_1_2, year_output,
            year_style_dict, ph_2_1, ph_2_2, mileage_output,
            mileage_style_dict, ph_3_1, ph_3_2, power_output,
            power_style_dict, ph_4_1, ph_4_2, fuel_output, brand_output,
            n_of_offs_out, out_0_1, out_0_2, out_0_3, out_1_1, out_1_2,
            out_1_3, style_0_1, style_0_2, style_0_3, style_1_1, style_1_2,
            style_1_3, fig_1, fig_2, link_search_out]


import heapq

class Solution:
    """
    @param nums: A list of integers
    @return: the median of numbers
    """

    def medianII(self, nums):
        maxheap = []
        minheap = []
        result = []
        for num in nums:
            if maxheap and num > -maxheap[0]:
                heapq.heappush(minheap, num)
                if len(minheap) > len(maxheap):
                    heapq.heappush(maxheap, -heapq.heappop(minheap))

            else:
                heapq.heappush(maxheap, -num)
                if len(maxheap) - len(minheap) > 1:
                    heapq.heappush(minheap, -heapq.heappop(maxheap))
            result.append(-maxheap[0])
        return result

    def medianII(self, nums):
        max_heap, min_heap, output = [], [], []

        for num in nums:
            if len(max_heap) == len(min_heap):
                if max_heap and num > -max_heap[0]:
                    num = heapq.heappushpop(min_heap, num)
                heapq.heappush(max_heap, -num)
            else:
                if num < -max_heap[0]:
                    num = - heapq.heappushpop(max_heap, -num)
                heapq.heappush(min_heap, num)
            output.append(-max_heap[0])
        return output

class Solution:
    def __init__(self):
        self.maxheap = []
        self.minheap = []

    """
    @param nums: A list of integers
    @return: the median of numbers
    """

    def medianII(self, nums):
        res = []
        for num in nums:
            if not self.maxheap:
                heapq.heappush(self.maxheap, -num)
            else:
                if num > -self.maxheap[0]:
                    heapq.heappush(self.minheap, num)
                else:
                    heapq.heappush(self.maxheap, -num)

            while len(self.maxheap) - len(self.minheap) > 1:
                item = heapq.heappop(self.maxheap)
                heapq.heappush(self.minheap, -item)

            while len(self.minheap) - len(self.maxheap) > 0:
                item = heapq.heappop(self.minheap)
                heapq.heappush(self.maxheap, -item)

            res.append(-self.maxheap[0])

        return res


#!/usr/bin/env python
# a script for live monitoring CPU usage, network conn, volume, VPN conn, installed packages.
# required: psutil, netifaces, sockets, subprocess

import socket
import netifaces
from os import popen
import subprocess
import psutil  # For Cpu and ram usage, and storage
import distro  # Check distro name to Know How to count the installed packages
import time

# TODO: Pesentage option and number option for swap and memory
# WARNING: The ram function is giving unaccurate numbers !

# Network
def ping():  # checks if there is a connection
    try:
        socket.setdefaulttimeout(1)
        socket_obj = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        host = "1.1.1.1"
        port = 80
        server_addr = (host, port)
        socket_obj.connect(server_addr)
    except OSError as Err:
        return False
    else:
        socket_obj.close()
        return True

# Set network devices
Ethernet_device = "enp2s0"
Wifi_device = "wlp3s0"

def interface(eth=Ethernet_device, wifi=Wifi_device):  # returns the type of connection
    gateways = netifaces.gateways()
    if gateways["default"][2][1] == eth:
        return "Ethernet"
    elif gateways["default"][2][1] == wifi:
        return "Wifi"
    else:
        return None

def connection():  # combines the previous two functions
    if ping() is True:
        if interface() == "Ethernet":
            return "Ethernet: CONNECTED"
        elif interface() == "Wifi":
            return "Wi-fi: CONNECTED"
    elif ping() is False or interface() is None:
        return "NO CONNECTION !"

# VPN
def vpn_connection():
    r = popen("ip a | grep tun0 | grep inet | wc -l").readline()
    if r == 1:
        return "on"
    else:
        return "off"

# CPU
def cpu():
    return psutil.cpu_percent(interval=0.5)

# Ram and swap
def ram():
    return psutil.virtual_memory()[2]

def swap():
    return psutil.swap_memory()[3]

# Battery
def battery():
    if psutil.sensors_battery() is not None:
        return int(psutil.sensors_battery()[0])
    else:
        return "This Device Don't have a Battery"

# VOLUME
def check_vol():
    cmd = "amixer get Master | awk -F'[][]' 'END{ print $2}'"
    process = subprocess.run(cmd, stdout=subprocess.PIPE, shell=True)
    return process.stdout.strip().decode("ascii")

# count installed packages
# Check distro name :
def count_pkg():  # rpm -qa for fedora  # dpkg -l for Debian # pacman -Q
    if distro.id() in ['fedora', 'rhel', 'centos', 'opensuse']:
        default_cmd = "rpm -qa"
    elif distro.id() in ['ubuntu', 'debian']:
        default_cmd = "dpkg -l"
    elif distro.id() in ['arch']:
        default_cmd = "pacman -Q"
    else:
        return "Unknown"
    return str(len(popen(default_cmd).readlines()))

def main():
    # can be used in a while loop to live monitor data.
    print(
        " CPU: {}%".format(cpu()),
        "|",
        " RAM: {}%".format(ram()),
        "|",
        "易SWAP: {}%".format(swap()),
        "|",
        " Packages: {}".format(count_pkg()),
        "|",
        "嬨VPN: {}".format(vpn_connection()),
        "|",
        "墳VOL: {}".format(check_vol()),
        "|",
        "  " + connection(),
        "|",
        " BAT: {}%".format(battery()),
        # flush=True,
        # end="",
    )
    time.sleep(0.3)

if __name__ == "__main__":
    main()


import pyb
from pyb import Timer

class HBridge:
    """ Control à H-Bridge. Also support PWM to control the speed (between 0 to 100%) """
    PWM_FREQ = 100  # Frequency 100 Hz
    (UNDEFINED,HALT,FORWARD,BACKWARD) = (-1, 0,1,2)

    def __init__( self, input_pins, pwm = None ):
        """:param input_pins: tuple with input1 and input 2 pins
           :param pwm: dic with pin, timer and channel """
        self.speed = 0
        self.state = HBridge.UNDEFINED

        # Init HBridge control pins
        self.p_input1 = pyb.Pin( input_pins[0], pyb.Pin.OUT_PP )
        self.p_input2 = pyb.Pin( input_pins[1], pyb.Pin.OUT_PP )

        # Init PWM Control for speed control (without PWM, the L293D's
        #   Enable pin must be place to HIGH level)
        self.has_pwm = (pwm != None)
        if self.has_pwm:
            self._timer = pyb.Timer( pwm['timer'], freq=self.PWM_FREQ )
            self._channel = self._timer.channel( pwm['channel'], Timer.PWM, pin=pwm['pin'], pulse_width_percent=100 )

        self.halt()

    def set_speed( self, speed ):
        if not(0 <= speed <= 100):
            raise ValueError( 'invalid speed' )
        # Use PWM speed ?
        if self.has_pwm:
            self._channel.pulse_width_percent( speed )
            self.speed = speed
        else:
            # Non PWM
            self.speed = 0 if speed == 0 else 100
            if self.speed == 0 and self.state != HBridge.HALT:
                self.halt() # force motor to stop by manipulating input1 & input2

    def halt( self ):
        self.p_input1.low()
        self.p_input2.low()
        self.state = HBridge.HALT # Do not invert ...
        self.set_speed( 0 )       #    thoses 2 lines

    def forward(self, speed = 100 ):
        # reconfigure HBridge
        if self.state != HBridge.FORWARD :
            self.halt()
            self.p_input1.low()
            self.p_input2.high()
            self.state = HBridge.FORWARD
        # Set speed
        self.set_speed( speed )

    def backward(self, speed = 100 ):
        # reconfigure HBridge
        if self.state != HBridge.BACKWARD:
            self.halt()
            self.p_input1.high()
            self.p_input2.low()
            self.state = HBridge.BACKWARD
        # Set speed
        self.set_speed( speed )

class DualHBridge():
    """ Control 2 x H-Bridges on L293D to control the 2 motors of a
        robotic plateforme """

    def __init__( self, mot1_pins, mot1_pwm, mot2_pins, mot2_pwm, derivative_fix=0 ):
        """:param mot1_pins: tuple with input1 and input 2 pins
           :param mot1_pwm: dic with pin, timer and channel. Use None for no PWM.
           :param mot2_pins: tuple with input3 and input 4 pins
           :param mot2_pwm: dic with pin, timer and channel. Use None for no PWM.
           :param derivative_fix: use +3 to speedup right motor when forward() does bend on the right.
                                 use -3 to slow down the right motor when forward() bend on the left."""
        if (derivative_fix != 0) and ( (mot1_pwm == None) or (mot2_pwm == None) ):
            raise EValueError( 'derivative_fix needs PWM control to work' )
        self.motor1 = HBridge( mot1_pins, mot1_pwm )
        self.motor2 = HBridge( mot2_pins, mot2_pwm )
        self.derivative_fix = derivative_fix

    def forward( self, speed = 100, speed_2 = None ):
        if speed_2 == None: # not different speed for motor 2?
            speed_2 = speed
        #adjust speed with derivative_fix
        if self.derivative_fix > 0:
            speed = speed - self.derivative_fix
            speed = 0 if speed < 0 else speed
        elif self.derivative_fix < 0:
            speed_2 = speed_2 - abs(self.derivative_fix)
            speed_2 = 0 if speed_2 < 0 else speed_2

        self.motor1.forward( speed )
        self.motor2.forward( speed_2 )

    def backward( self, speed = 100, speed_2 = None ):
        if speed_2 == None: # not different speed for motor 2?
            speed_2 = speed
        #adjust speed with derivative_fix
        #adjust speed with derivative_fix
        if self.derivative_fix > 0:
            speed = speed - self.derivative_fix
            speed = 0 if speed < 0 else speed
        elif self.derivative_fix < 0:
            speed_2 = speed_2 - abs(self.derivative_fix)
            speed_2 = 0 if speed_2 < 0 else speed_2

        self.motor1.backward( speed )
        self.motor2.backward( speed_2 )

    def halt( self ):
        self.motor1.halt()
        self.motor2.halt()


"""
Написать функцию преобразующую в словарь словарей,
дальше нужно сделать сортировки внешнего словаря (по фамилиям) и внутреннего по именам.
Сортировка: сначала по фамилиям, потом по именам
"""

data = ("Иван Сергеев", "Инна Серова", "Петр Алексеев", "Илья Иванов", "Анна Савельева")
result = {}
finish_result = {}
for person in data:
    name, surname = person.split()
    data_surname = result.setdefault(surname[0], {})
    data_name = data_surname.setdefault(name[0], [])
    data_name.append(person)

for surname in sorted(result):
    finish_result[surname] = {}
    for name in sorted(result[surname]):
        finish_result[surname][name] = result[surname][name]

print(finish_result)


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

import os, json, datetime, re, urllib, time, argparse

import requests

from lxml import html

class ParserData():

    def __init__(self, **values):
        self.__values = values
        self.__values['type'] = self.type
        for attr in values:
            setattr(self, attr, values[attr])

    def __setattr__(self, attr, value):
        if not attr.startswith('_'):
            self.__values[attr] = value
        super().__setattr__(attr, value)

    def tojson(self):
        return json.dumps(self.__values, sort_keys=True)

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

class CountryData(ParserData):
    type = 'countrydata'

class DetailData(ParserData):
    type = 'detaildata'

class PopulationData(ParserData):
    type = 'populationdata'

class WOMParser():

    def __init__(self, url):
        self.url = url

    def parsenumber(self, text):
        if text is None:
            return text
        try:
            return int(text.replace(',',''))
        except ValueError:
            return text

    def parsecountries(self):
        r = requests.get(self.url)
        r.raise_for_status()
        page = html.fromstring(r.content)

        #table = page.xpath('//*[@id="main_table_countries_today"]/tbody[1]')[0]
        table = page.xpath('//*[@id="main_table_countries_yesterday"]/tbody[1]')[0]
        #country_element = table.xpath("//td[contains(., 'USA')]")[0]
        #print(html.tostring(country_element))
        #row = country_element.xpath("./..")[0]
        for row in table.xpath("tr"):
            #print(html.tostring(row))
            if row.attrib.get("class") in ["total_row_world row_continent", "total_row_world"]:
                continue
            links = row.xpath("td/a")
            if not links:
                continue
            country = links[0].text

            data = [self.parsenumber(td.text) for td in row.xpath("td")]

            baseidx = 1

            total_cases = data[baseidx+1]
            new_cases = data[baseidx+2]
            total_deaths = data[baseidx+3]
            new_deaths = data[baseidx+4]
            total_recovered = data[baseidx+5]
            active_cases = data[baseidx+6]
            new_recovered = data[baseidx+7]
            serious_critical = data[baseidx+8]
            total_per_1m = data[baseidx+9]
            total_deaths_per_1m = data[baseidx+10]
            total_tests = data[baseidx+11]
            total_tests_per_1m = data[baseidx+12]
            continent = data[baseidx+14]

            if len(links) > 1:
                population = self.parsenumber(links[1].text)
            else:
                population = data[baseidx+13]

            if False:
                print("Country: ", country)
                print("Total cases: ", total_cases)
                print("New cases: ", new_cases)
                print("Total deaths: ", total_deaths)
                print("New deaths: ", new_deaths)
                print("Active cases: ", active_cases)
                print("Total recovered: ", total_recovered)
                print("Serious, critical cases: ", serious_critical)

            countrydata = CountryData(
                country=country,
                total_cases = total_cases,
                new_cases = new_cases,
                total_deaths = total_deaths,
                new_deaths = new_deaths,
                active_cases = active_cases,
                total_recovered = total_recovered,
                #new_recovered = new_recovered,
                serious_critical = serious_critical,
                total_per_1m = total_per_1m,
                total_deaths_per_1m = total_deaths_per_1m,
                total_tests = total_tests,
                total_tests_per_1m = total_tests_per_1m,
                date = str(datetime.date.today()-datetime.timedelta(days=1)),
                continent = continent,
                population = population,
                #data = str(data),
            )
            yield countrydata

    def parsepopulation(self):
        r = requests.get(self.url)
        r.raise_for_status()
        page = html.fromstring(r.content)

        table = page.xpath('//*[@id="example2"]/tbody[1]')[0]
        for row in table.xpath("tr"):
            #print(html.tostring(row))
            if row.attrib.get("class") in ["total_row_world row_continent", "total_row_world"]:
                continue
            country_elem = row.xpath("td/a")
            if not country_elem:
                continue
            country = country_elem[0].text

            data = [self.parsenumber(td.text) for td in row.xpath("td")]

            population = data[2]

            if False:
                print("Country: ", country)
                print("Population: ", population)

            popdata = PopulationData(
                country=country,
                population=data[2]
            )
            yield popdata

    def parsedetails(self):
        r = requests.get(self.url)
        r.raise_for_status()
        page = html.fromstring(r.content)

        table = page.xpath('//*[@id="main_table_countries_yesterday"]/tbody[1]')[0]

        for row in table.xpath("tr"):
            if row.attrib.get("class") in ["total_row_world row_continent", "total_row_world"]:
                continue
            country_elem = row.xpath("td/a")
            if not country_elem:
                continue

            country = country_elem[0].text
            url = country_elem[0].attrib.get('href')
            (cases, deaths, active) = self.parsecountry(country, url)
            detaildata = DetailData(
                country=country,
                cases = cases,
                deaths = deaths,
                active = active,
            )

            yield detaildata

    def parsecountry(self, country, url):
        r = requests.get(urllib.parse.urljoin(self.url, url))
        page = html.fromstring(r.content)

        if False:
            script = page.xpath('//div[@id="graph-cases-daily"]/following-sibling::script[1]')
            if script:
                cases = self.parsescript(script[0], "Daily Cases")
            else:
                cases = []

            script = page.xpath('//div[@id="graph-deaths-daily"]/following-sibling::script[1]')
            if script:
                deaths = self.parsescript(script[0], "Daily Deaths")
            else:
                deaths = []
        else:
            script = page.xpath('//div[@class="tabbable-panel-cases"]/following-sibling::script[1]')
            if script:
                cases = self.parsescript(script[0], "Cases")
            else:
                cases = []

            script = page.xpath('//div[@class="tabbable-panel-deaths"]/following-sibling::script[1]')
            if script:
                deaths = self.parsescript(script[0], "Deaths")
            else:
                deaths = []

        script = page.xpath('//div[@id="graph-active-cases-total"]/following-sibling::script[1]')
        if script:
            active = self.parsescript(script[0], "Currently Infected")
        else:
            active = []

        return cases, deaths, active

    def parsescript(self, elem, attribute):
        lines = elem.text.split('\n')

        try:
            xaxis = lines.index('        xAxis: {')
        except ValueError:
            xaxis = lines.index('            xAxis: {')
        datevalues = self.parsevalues(lines[xaxis+1])
        dates = self.parsedates(datevalues)
        try:
            vidx = lines.index("            name: '%s'," % attribute)
        except ValueError:
            vidx = lines.index("                name: '%s'," % attribute)

        values = self.parsevalues(lines[vidx+3])
        #print(dates)
        #print(values)
        return list(zip(dates, values))

    def parsevalues(self, line):
        r = re.compile(r".*(\[.*?\]).*")
        v = r.match(line)
        parsed = json.loads(v.group(1))
        return parsed

    def parsedates(self, datevalues):
        dates = []

        baseyear = 2020
        prevd = None

        for text in datevalues:
            ts = time.strptime(text, '%b %d')
            d = datetime.date(baseyear, ts.tm_mon, ts.tm_mday)
            if prevd and prevd > d:
                baseyear += 1
                d = datetime.date(baseyear, ts.tm_mon, ts.tm_mday)
            prevd = d
            dates.append(d.strftime('%Y-%m-%d'))
        return dates

def main():
    p = argparse.ArgumentParser()
    p.add_argument(
        "-s",
        "--stdout",
        action="store_true",
        dest="write_stdout",
        default=False,
        help="output to stdout",
    )
    p.add_argument(
        "-C", "--csv", action="store_true", dest="csv", default=False, help="csv mode"
    )
    p.add_argument(
        "-f",
        "--outputfile",
        action="store",
        dest="outputfile",
        default=None,
        help="outputfile name",
    )
    p.add_argument(
        "-o",
        "--overwrite",
        action="store_true",
        dest="overwrite",
        default=False,
        help="overwrite existing outputfile",
    )
    p.add_argument("dataset", choices=['countries', 'details', 'population'])

    options = p.parse_args()
    dataset = options.dataset

    cov_url = 'https://www.worldometers.info/coronavirus/'
    pop_url = 'https://www.worldometers.info/world-population/population-by-country/'

    if options.outputfile:
        outputfile = options.outputfile
    else:
        datestr = datetime.date.today().strftime("%Y%m%d")
        outputfile = "%s-%s.json" % (dataset, datestr)

    if dataset == 'countries':
        parser = WOMParser(cov_url)
        parsermethod = parser.parsecountries

    elif dataset == 'details':
        parser = WOMParser(cov_url)
        parsermethod = parser.parsedetails

    elif dataset == 'population':
        parser = WOMParser(pop_url)
        parsermethod = parser.parsepopulation

    else:
        usage()
        return

    if parsermethod:
        if options.write_stdout:
            for event in parsermethod():
                print(event.tojson())

        else:
            if os.path.exists(outputfile) and os.path.getsize(outputfile) > 0 and not options.overwrite:
                print("%s exists" % outputfile)
                return

            with open(outputfile, 'w') as fp:
                for event in parsermethod():
                    print(event.tojson(), file=fp)

if __name__ == "__main__":
    main()


import datetime
from celery import Celery
from ..models.declarative_base import Base, engine, session
from ..models.models import *
import random

celery_app = Celery('health', broker='redis://localhost:6379/0')

celery_app.conf.task_queues = {
    'team': {
        'exchange': 'team',
        'exchange_type': 'direct',
        'routing_key': 'team',
    },
}

@celery_app.task(name='tasks.health_check', queue='team')
def check(message):

    random_number = random.random()

    if random_number <= 0.9:
        celery_app.send_task('tasks.health_response', args=['team', message], queue='monitor')
    else:
        failure = ServiceRegistration(requestId = message, timestamp=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), serviceName='team')
        session.add(failure)
        session.commit()
        session.close()


from __future__ import absolute_import, print_function, division
from __future__ import unicode_literals

import math
import os
import time

import tensorflow as tf

from utils.utils import deco_print, log_summaries_from_dict, get_results_for_epoch

class PrintSampleHook(tf.train.SessionRunHook):

    def __init__(self, every_steps, model):
        super(PrintSampleHook).__init__()
        self._timer = tf.train.SecondOrStepTimer(every_steps = every_steps)
        self._iter_count = 0
        self._global_step = None
        self._model = model

        # Using only the 1st GPU
        output_tensors = model.get_output_tensors(0)
        self._fetches = [model.get_data_layer(0).input_tensors, output_tensors]

    def begin(self):
        self._iter_count = 0
        self._global_step = tf.train.get_global_step()

    def before_run(self, run_context):
        if self._timer.should_trigger_for_step(self._iter_count):
            return tf.train.SessionRunArgs([self._fetches, self._global_step])
        return tf.train.SessionRunArgs([[], self._global_step])

    def after_run(self, run_context, run_values):
        results, step = run_values.results
        self._iter_count = step

        if not results:
            return
        self._timer.update_last_triggered_step(self._iter_count - 1)

        input_values, output_values = results
        dict_to_log = self._model.maybe_print_logs(input_values, output_values, step)

        if self._model.params["save_summaries_steps"] and dict_to_log:
            log_summaries_from_dict(
                    dict_to_log,
                    self._model.params["logdir"],
                    step)

class PrintLossAndTimeHook(tf.train.SessionRunHook):

    def __init__(self, every_steps, model):
        super(PrintLossAndTimeHook).__init__()
        self._timer = tf.train.SecondOrStepTimer(every_steps = every_steps)
        self._every_steps = every_steps
        self._iter_count = 0
        self._global_step = None
        self._model = model
        self._fetches = [model.loss]
        self._last_time = time.time()

    def begin(self):
        self._iter_count = 0
        self._global_step = tf.train.get_global_step()

    def before_run(self, run_context):
        if self._timer.should_trigger_for_step(self._iter_count):
            return tf.train.SessionRunArgs([self._fetches, self._global_step])
        return tf.train.SessionRunArgs([[], self._global_step])

    def after_run(self, run_context, run_values):
        results, step = run_values.results
        self._iter_count = step

        if not results:
            return

        self._timer.update_last_triggered_step(self._iter_count - 1)

        if self._model.steps_in_epoch is None:
            deco_print("Global step {}:".format(step), end = " ")
        else:
            deco_print("Epoch {}, global step {}:".format(step // self._model.steps_in_epoch, step), end = " ")

        loss = results[0]

        deco_print("Train loss: {:.4f}".format(loss), offset = 4)

        tm = (time.time() - self._last_time) / self._every_steps
        m, s = divmod(tm, 60)
        h, m = divmod(m ,60)

        deco_print("time per step = {}:{:02}:{:.3f}".format(int(h), int(m), s), start = " ")

        self._last_time = time.time()


"""
    Advent of Code 2021
    Day 02: Dive!
"""

import pytest

def parse_input(file_name):
    with open(file_name, "r", encoding="ascii") as data_file:
        data = []
        for line in data_file.readlines():
            command, value = line.split()
            data.append((command, int(value)))
        return data

def day02_part1(data):
    hpos = 0
    depth = 0
    for cmd, val in data:
        if cmd == "forward":
            hpos += val
        else:
            depth += val if cmd == "down" else -val
    return hpos * depth

def day02_part2(data):
    hpos = 0
    depth = 0
    aim = 0
    for cmd, val in data:
        if cmd == "forward":
            hpos += val
            depth += aim * val
        else:
            aim += val if cmd == "down" else -val
    return hpos * depth

@pytest.fixture(autouse=True, name="test_data")
def fixture_test_data():
    return parse_input("data/day02_test.txt")

def test_day02_part1(test_data):
    assert day02_part1(test_data) == 150

def test_day02_part2(test_data):
    assert day02_part2(test_data) == 900

if __name__ == "__main__":
    input_data = parse_input("data/day02.txt")

    print("Day 02 Part 1:")
    print(day02_part1(input_data))  # Correct answer is 1451208

    print("Day 02 Part 2:")
    print(day02_part2(input_data))  # Correct answer is 1620141160


from entities.game_console import Game
from entities.round_console import Round, Subround
from entities.tournament_console import Tournament
from utils.template_data import EXAMPLE_PLAYERS, EXAMPLE_WORDS
from typing import List, Optional
from collections import Counter
from typing import Counter as CounterT

VERSION_INTERFACE: str = "0.0.3"
HELP: str = """
Доступные команды:
quit, exit
version
help
add word
delete word
add players
delete players
template fill (broken)
add round
add subround
checkout round
checkout subround
checkout game
set game results
split to games
conclude subround result
conclude round result
get k best
"""

if __name__ == "__main__":
    current_tournament: Tournament = Tournament(0)
    current_round: Optional[Round] = None
    current_subround: Optional[Subround] = None
    current_game: Optional[Game] = None
    print("Система организации турниров по шляпе, локальный командный интерфейс")

    while True:
        command: str = input(">>> ")
        if command == "quit" or command == "exit":
            break
        elif command == "version":
            print(f"Версия интерфейса: {VERSION_INTERFACE}")
            print(f"Версия класса: {current_tournament.VERSION}")
        elif command == "help":
            print(HELP)
        elif command == "add word":
            word: str = input("Введите слово:\n")
            difficulty: int = int(input("Введите сложность:\n"))
            result, word_id = current_tournament.add_word(
                word, difficulty
            )  # FIXME: No type annotation
            if result:
                print(f"Слово добавлено, его uid: {word_id}")
            else:
                print(f"Слово уже есть в базе")
        elif command == "delete word":
            word: str = input("Введите слово:\n")
            result: bool = current_tournament.delete_word(word)
            if result:
                print(f"Слово удалено")
            else:
                print(f"Слова нет в базе")
        elif command == "add players":
            first_player: str = input("Введите имя и фамилию первого игрока:\n")
            second_player: str = input("Введите имя и фамилию второго игрока:\n")
            result: bool = current_tournament.add_play_pair(first_player, second_player)
            if result:
                print("Пара игроков успешно добавлена")
            else:
                print("Пара игроков уже участвует")
        elif command == "delete players":
            first_player: str = input("Введите имя и фамилию первого игрока:\n")
            second_player: str = input("Введите имя и фамилию второго игрока:\n")
            result: bool = current_tournament.delete_play_pair(
                first_player, second_player
            )
            if result:
                print("Пара игроков успешно удалена")
            else:
                print("Пары игроков нет в списке участников")
        elif command == "template fill":
            for word, diff in EXAMPLE_WORDS:
                current_tournament.add_word(word, diff)
            for play_1, play_2 in EXAMPLE_PLAYERS:
                current_tournament.add_play_pair(play_1, play_2)
            print("Добавлены слова и игроки для примера")
        elif command == "add round":
            players_uid: List[int] = list(
                map(int, input("Введите uid пар участников нового раунда:\n").split())
            )
            difficulty: int = int(input("Введите ожидаемую сложность раунда:\n"))
            name: str = input("Введите название раунда:\n")
            current_tournament.create_round(players_uid, difficulty, name)
            print("Раунд создан")
        elif command == "checkout round":
            round_id: int = int(input("Введите номер раунда:\n"))
            if round_id >= len(current_tournament.rounds):
                print("Нет раунда с таким номером")
            else:
                current_round = current_tournament.rounds[round_id]
                print("Раунд успешно выбран")
        elif command == "add subround":
            if current_round is None:
                print("Сначала выберите раунд")
                continue
            players_uid: List[int] = list(
                map(
                    int, input("Введите uid пар участников нового подраунда:\n").split()
                )
            )
            words_amount: int = int(input("Укажите, сколько слов из банка взять:\n"))
            result: bool = current_round.add_subround(players_uid, words_amount)
            if result:
                print("Подраунд успешно создан")
            else:
                print("Что-то пошло не так")
        elif command == "checkout subround":
            if current_round is None:
                print("Сначала выберите раунд")
                continue
            subround_id: int = int(input("Введите номер подраунда:\n"))
            if subround_id >= len(current_round.subrounds):
                print("Нет подраунда с таким номером")
            else:
                current_subround = current_round.subrounds[subround_id]
                print("Подраунд успешно выбран")
        elif command == "split to games":
            if current_subround is None:
                print("Сначала выберите подраунд")
                continue
            games_amount: int = int(
                input("Укажите, на сколько игр разбить подраунд:\n")
            )
            result: bool = current_subround.split_players_to_games(games_amount)
            if result:
                print("Подраунд успешно разбит на игры")
            else:
                print("Что-то пошло не так")
        elif command == "checkout game":
            if current_subround is None:
                print("Сначала выберите подраунд")
                continue
            game_id: int = int(input("Введите номер игры:\n"))
            if game_id >= len(current_subround.games):
                print("Нет игры с таким номером")
            else:
                current_game = current_subround.games[game_id]
                print("Игра успешно выбрана")
        elif command == "set game results":
            if current_game is None:
                print("Сначала выберите игру")
                continue
            game_results: CounterT[int] = Counter(current_game.participants_uid)
            for player_uid in current_game.participants_uid:
                game_results[player_uid] = int(
                    input(
                        f"Enter result for pair {current_tournament.uid_to_play_pair[player_uid]}:\n"
                    )
                )

            result: bool = current_game.conclude_result(game_results)
            if result:
                print("Итоги успешно подведены")
            else:
                print("Произошла какая-то ошибка при подведении итогов")
        elif command == "conclude subround result":
            result: bool = current_subround.conclude_results()
            if result:
                print("Итоги подраунда успешно подведены")
            else:
                print("Подведение итогов не прошло успешно")
        elif command == "conclude round result":
            result: bool = current_round.conclude_results()
            if result:
                print("Итоги раунда успешно подведены")
            else:
                print("Подведение итогов не прошло успешно")
        elif command == "get k best":
            k: int = int(input("Укажите число для взятия:\n"))
            result: Optional[List[int]] = current_round.get_top_n(k)
            if result:
                for uid in result:
                    print(current_tournament.uid_to_play_pair[uid])
            else:
                print("Что-то пошло не так")


from datetime import datetime

from django.http import JsonResponse
from django.shortcuts import redirect, get_object_or_404
from django.urls import reverse
from django.db.models import Q
from django.contrib.auth.mixins import PermissionRequiredMixin, LoginRequiredMixin
from django.utils.http import urlencode
from django.views.generic import ListView, CreateView, DetailView, DeleteView, UpdateView

from advert.models import Advert
from advert.forms import AdvertForm, SearchForm

class AdvertListView(ListView):
    model = Advert
    template_name = 'adverts/list.html'
    context_object_name = 'ads'
    paginate_related_by = 4
    paginate_related_orphans = 0

    def get(self, request, **kwargs):
        self.form = SearchForm(request.GET)
        self.search_data = self.get_search_data()
        return super(AdvertListView, self).get(request, **kwargs)

    def get_queryset(self):
        queryset = super().get_queryset()

        if self.search_data:
            queryset = queryset.filter(
                Q(title__icontains=self.search_data)
            )
        return queryset.filter(moderated=True).order_by('-post_date')

    def get_search_data(self):
        if self.form.is_valid():
            return self.form.cleaned_data['search_value']
        return None

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['adverts'] = Advert.objects.all().filter(moderated=True).order_by('-post_date')
        context['search_form'] = self.form

        if self.search_data:
            context['query'] = urlencode({'search_value': self.search_data})
        return context

class AdvertCreateView(LoginRequiredMixin, CreateView):
    model = Advert
    template_name = 'adverts/create.html'
    form_class = AdvertForm

    def form_valid(self, form):
        ad = form.save(commit=False)
        ad.author = self.request.user
        ad.save()
        return redirect('advert_list')

class ApprovalListView(PermissionRequiredMixin, ListView):
    model = Advert
    template_name = 'adverts/approval.html'
    context_object_name = 'ads'
    paginate_related_by = 4
    paginate_related_orphans = 0
    permission_required = 'advert.approve'

    def get(self, request, **kwargs):
        self.form = SearchForm(request.GET)
        self.search_data = self.get_search_data()
        return super(ApprovalListView, self).get(request, **kwargs)

    def get_queryset(self):
        queryset = super().get_queryset()

        if self.search_data:
            queryset = queryset.filter(
                Q(title__icontains=self.search_data)
            )
        return queryset.filter(moderated=False).order_by('created_date')

    def get_search_data(self):
        if self.form.is_valid():
            return self.form.cleaned_data['search_value']
        return None

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['adverts'] = Advert.objects.all().filter(moderated=False).order_by('created_date')
        context['search_form'] = self.form

        if self.search_data:
            context['query'] = urlencode({'search_value': self.search_data})
        return context

    def has_permission(self):
        return super().has_permission()

class AdvertDetailView(DetailView):
    model = Advert
    template_name = 'adverts/detail.html'
    context_object_name = 'ad'

class AdvertUpdateView(PermissionRequiredMixin, UpdateView):
    model = Advert
    template_name = 'adverts/update.html'
    form_class = AdvertForm
    context_object_name = 'ad'

    def form_valid(self, form):
        ad = form.save(commit=False)
        ad.author = self.request.user
        ad.moderated = False
        ad.save()
        return redirect('advert_list')

    def has_permission(self):
        return self.get_object().author == self.request.user

class AdvertDeleteView(PermissionRequiredMixin, DeleteView):
    model = Advert
    template_name = 'adverts/delete.html'

    def get_success_url(self):
        return reverse('advert_list')

    def has_permission(self):
        return self.get_object().author == self.request.user

class ApprovalAdvertDetailView(PermissionRequiredMixin, DetailView):
    model = Advert
    template_name = 'adverts/approval_detail.html'
    context_object_name = 'ad'
    permission_required = 'advert.approve'

    def has_permission(self):
        return super().has_permission()

def approve(request, *args, **kwargs):
    if request.is_ajax and request.method == "POST":
        ad = get_object_or_404(Advert, pk=list(dict(request.POST).keys())[0])
        ad.moderated = True
        ad.published_at = datetime.now()
        ad.save()
        return JsonResponse({'message': 'Success!'}, status=200)
    return JsonResponse({"error": ""}, status=400, safe=False)

def reject(request, *args, **kwargs):
    if request.is_ajax and request.method == "POST":
        ad = get_object_or_404(Advert, pk=list(dict(request.POST).keys())[0])
        ad.rejected = True
        ad.save()
        return JsonResponse({'message': 'Rejected'}, status=200)
    return JsonResponse({"error": ""}, status=400, safe=False)


import re
from django import template
from decimal import Decimal

register = template.Library()

@register.filter
def match(value, regex):
    """
    Tests a regular expression against the start of a value.

    Especially useful in if statements::

        {% if value|match:"regex" %}

Some text

        {% endif %}
    """
    return re.match(regex, value)

@register.filter
def subtract(value, arg):
  return value - arg


from telas import *
from persistencia import *

def menu():
    Metodos.limparTela()
    print("-------- Sistema de Motos --------")
    print("1 - Cadastrar Moto")
    print("2 - Editar Moto")
    print("3 - Excluir Moto")
    print("4 - Selecionar Moto")
    print("5 - Listar Motos")
    print("6 - Sair")
    print("----------------------------------")
    opcao = int(input("Digite a opção desejada: "))
    return opcao

while True:
    opcao = menu()

    if opcao == 1:
        moto = Metodos.cadastrarMoto()
        criar(moto)
        Metodos.exibirMsg("Moto cadastrada com sucesso!")
    elif opcao == 2:
        moto = Metodos.editarMoto()
        editar(moto)
        Metodos.exibirMsg("Moto editada com sucesso!")
    elif opcao == 3:
        Metodos.limparTela()
        id = Metodos.excluirMoto()
        excluir(id)
        Metodos.exibirMsg("Moto excluída com sucesso!")
    elif opcao == 4:
        id = Metodos.selecionarMoto()
        moto = selecionar(id)
        if moto == None:
            Metodos.exibirMsg("Moto não encontrada!")
        else:
            Metodos.exibirMoto(moto)
            Metodos.travarTela()
    elif opcao == 5:
        motos = selecionar_todos()
        Metodos.exibirMotos(motos)
    elif opcao == 6:
        break