def rough_process(txt: str, cat: int) -> tuple:
    """
    :param cat: 1.fixSample1那种 2.fixSample2那种 ...
    """

    def not_empty(a):
        for i in a:
            if i != '\n' and i != ' ':
                return True
        else:
            return False

    def clean_head_tail(a: str):
        while a[0] == ' ' or a[0] == '\n':
            a = a[1:]
        while a[-1] == ' ' or a[-1] == '\n':
            a = a[:-1]
        return a

    _txt = open(txt, 'r', encoding="utf-8")
    fix_list = []
    location_list = []
    txt = _txt.read()
    for i in txt:
        if '°' in i:
            cat = 0
            break
    _txt.close()
    if cat == 1:
        txt = txt.split('\n')
        txt = [i for i in txt if not_empty(i)]
        for i in range(len(txt)):
            if i < len(txt) / 2:
                fix_list.append(clean_head_tail(txt[i]))
            else:
                location_list.append(clean_head_tail(txt[i]))
    elif cat == 2:
        txt = txt.replace('\n', ' ').split(' ')
        txt = [i for i in txt if i != '']
        for i in range(len(txt)):
            if i % 2 == 0:
                fix_list.append(txt[i])
            else:
                location_list.append(txt[i])
    elif cat == 3:
        txt = txt.split('\n')
        txt = [i for i in txt if i != '']
        for i in txt:
            if not not_empty(i):
                continue
            i = i.split()
            for j in range(1, len(i)):
                i[j] = i[j].replace('N', '')
                i[j] = i[j].replace('E', '')
            i = [j for j in i if j != '']
            fix_list.append(i[0])
            location_list.append('T'.join(i[1:]))
    else:
        print("文本类型错误")
    return fix_list, location_list

def location_process(loc_list: list):
    def cut(string: str, sep: list) -> list:
        baka = ['']
        timer = 0
        for iChar in string:
            if iChar in sep:
                baka.append('')
                timer += 1
            else:
                baka[timer] += iChar
        baka = [float(i) for i in baka if i != '']
        return baka.copy()

    back = []
    timer = -1
    for i in loc_list:
        if '°' in i:  # N29°18'45.1"E092°19'45.4" 度分秒
            mode = 0
        elif 'T' in i:
            mode = 2
        elif '.' in i:  # N3226.0E10456.0 度分？
            mode = 1
        else:  # N312047E1044856 度分秒？
            mode = -1

        if mode == 0:  # 感觉有点像脱了裤子放屁 算了
            i = cut(i, ['N', 'E', '°', '\'', '\"'])
            for j in range(6):
                k = j % 3
                if k == 0:
                    back.append(0)
                    timer += 1
                back[timer] += i[j] / pow(60, k)
        elif mode == 1:
            i = cut(i, ['N', 'E'])
            i = list(map(str, i))
            i = [[float(j[:-4]), float(j[-4:])] for j in i]
            for j in i:  # j:[32.0, 26.0]
                add = 0
                for k in range(2):  # j[k]:32.0
                    add += j[k] / (60 ** k)
                back.append(add)  # 怎么感觉上面的写法好奇怪
        elif mode == 2:
            for i in loc_list:
                i = i.split('T')
                i = list(map(float, i))
                la = i[0] + i[1] / 60
                long = i[2] + i[3] / 60
                back.append(la)
                back.append(long)
        elif mode == -1:
            i = cut(i, ['N', 'E'])
            i = [str(int(j)) for j in i]
            i = [[float(j[:-4]), float(j[-4:-2]), float(j[-2:])] for j in i]
            for j in i:  # j:[31.0, 30.0, 6.0]
                add = 0
                for k in range(3):  # j[k]:32.0 艹复制下来忘改成3了
                    add += j[k] / (60 ** k)
                back.append(add)  # 怎么感觉上面的写法好奇怪
    back = [str(round(i, 9)) for i in back]
    return back

def encoding(port: str, path: str, fix: list, loc: list):
    for i in range(len(loc)):  # 本来写的列表推导式 后面发现前面好像不行
        j = str(loc[i])
        j = j + (9 - len(j[j.index('.') + 1:])) * '0'
        fn = (3 if i % 2 == 0 else 4)
        j = (fn - len(j[:j.index('.')])) * ' ' + j
        loc[i] = j
    fix = [(5 - len(i)) * ' ' + i for i in fix]
    with open(path[:-3] + "fix", 'w') as f:
        for i in range(len(fix)):
            f.write(loc[2 * i + 0] + ' ')
            f.write(loc[2 * i + 1] + "  ")
            f.write(fix[i] + ' ')
            f.write(port.upper() + ' ')
            f.write(port[:2].upper() + ' ')
            f.write("4608073" + '\n')


import time
import os
import pyperclip
from PIL import ImageGrab
import pyimgur
# define the t to make the image name
t = time.localtime()
t = time.strftime("%y-%m-%d_%H_%M_%S", t)

CLIENT_ID = "713cacc415ed391"
title = "Uploaded with PyImgur"
image_path = "/Users/mac/Pictures"
# if not(os.path.exists(image_path)):
#    os.mkdir(image_path)
image_path = f"{image_path}/image-{t}.png"
image = ImageGrab.grabclipboard()
if image is not None:  # check if there's image in the clipboard
    image = image.save(image_path)
    im = pyimgur.Imgur(CLIENT_ID)
    uploaded_image = im.upload_image(image_path, title=title)
    # print(uploaded_image.title)
    # print(uploaded_image.link)
    # print(uploaded_image.type)
    link = uploaded_image.link
    result = f"![]({link})"
    os.remove(image_path)  # comment this line if you want to keep the image
else:
    link = "https://i.imgur.com/9HfL3bw.jpeg"
    result = f"![🤷🏻📷只好用一隻🦎]({link})"
# print(result)
pyperclip.copy(result)
spam = pyperclip.paste()  # save the result to system clipboard
print("Link Generated")


#!/usr/bin/env python3
import requests

def encrypt():
    url = "http://aes.cryptohack.org/bean_counter/encrypt/"
    rsp = requests.get(url)
    return rsp.json()['encrypted']

png_hdr = bytes([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52])
encrypted = bytes.fromhex(encrypt())

keystream = []
for i in range(len(png_hdr)):
    keystream.append(png_hdr[i] ^ encrypted[i])

print(keystream)

png = [0]*len(encrypted)
for i in range(len(encrypted)):
    png[i] = encrypted[i] ^ keystream[i%len(keystream)]

with open('bean_counter.png', 'wb') as fd:
    fd.write(bytes(png))


import argparse
from functools import partial
from glob import glob
from pathlib import Path
from time import perf_counter

import matplotlib
import matplotlib.pyplot as plt
import numpy as np
from tqdm.contrib.concurrent import process_map

import hgspy
import plotting
import tools
from strategies.config import Config
from strategies.static import hgs

matplotlib.use("Agg")  # Don't show plots

_SOLS_DIR = "solutions/"
_STATS_DIR = "statistics/"
_FIGS_DIR = "figures/"

def parse_args():
    parser = argparse.ArgumentParser()

    parser.add_argument("--seed", type=int, default=1)
    parser.add_argument("--num_procs", type=int, default=4)
    parser.add_argument("--config_loc", default="configs/analysis.toml")
    parser.add_argument(
        "--instance_pattern", default="instances/ORTEC-VRPTW-ASYM-*.txt"
    )
    parser.add_argument("--results_dir", type=str)
    parser.add_argument("--overwrite", action="store_true")

    group = parser.add_mutually_exclusive_group(required=True)
    group.add_argument("--max_runtime", type=float)
    group.add_argument("--max_iterations", type=int)
    group.add_argument("--phase", choices=["quali", "final"])

    return parser.parse_args()

def solve(
    loc: str,
    seed: int,
    config_loc: str,
    results_dir: str,
    max_runtime,
    max_iterations,
    phase,
    **kwargs,
):
    path = Path(loc)

    instance = tools.read_vrplib(path)
    start = perf_counter()

    if phase is not None:
        t_lim = tools.static_time_limit(tools.name2size(loc), phase)
        stop = hgspy.stop.MaxRuntime(t_lim)
    elif max_runtime is not None:
        stop = hgspy.stop.MaxRuntime(max_runtime)
    else:
        stop = hgspy.stop.MaxIterations(max_iterations)

    static_config = Config.from_file(config_loc).static()

    res = hgs(
        instance,
        hgspy.Config(seed=seed, **static_config.solver_params()),
        static_config.node_ops(),
        static_config.route_ops(),
        static_config.crossover_ops(),
        stop,
    )

    best = res.get_best_found()
    routes = [route for route in best.get_routes() if route]
    cost = best.cost()

    try:
        actual_cost = tools.validate_static_solution(instance, routes)
        is_ok = "Y"

        assert np.isclose(actual_cost, cost), "Could not validate objective."
    except AssertionError:
        is_ok = "N"

    finish = round(perf_counter() - start, 3)

    # Only save results for runs with feasible solutions and if results_dir
    # is a non-empty string
    if is_ok == "Y" and results_dir is not None:
        save_results(instance, res, results_dir, path.stem)

    stats = res.get_statistics()
    return (
        path.stem,
        is_ok,
        int(cost),
        res.get_iterations(),
        finish,
        len(stats.incumbents()),
    )

def save_results(instance, res, results_dir, inst_name):
    """
    Save the best solution, statistics and figures of results.
    - Solutions are stored as ``<results_dir>/solutions/<inst_name>.txt``.
    - Statistics are stored as ``<results_dir>/statistics/<inst_name>.csv``.
    - Figures are stored as ``<results_dir>/figures/<inst_name>.png``.
    """
    res_path = Path(results_dir)

    def make_path(subdir, extension):
        return (res_path / subdir / inst_name).with_suffix("." + extension)

    # Save best solutions
    best = res.get_best_found()
    sol_path = str(make_path(_SOLS_DIR, "txt"))
    best.export_cvrplib_format(sol_path, res.get_run_time())

    # Save statistics
    stats = res.get_statistics()
    stats_path = str(make_path(_STATS_DIR, "csv"))
    stats.to_csv(stats_path)

    # Save plots
    fig = plt.figure(figsize=(20, 12))
    gs = fig.add_gridspec(3, 2, width_ratios=(2 / 5, 3 / 5))

    plotting.plot_population(fig.add_subplot(gs[0, 0]), stats)
    plotting.plot_objectives(fig.add_subplot(gs[1, 0]), stats)
    plotting.plot_incumbents(fig.add_subplot(gs[2, 0]), stats)

    routes = best.get_routes()
    plotting.plot_instance(fig.add_subplot(gs[:, 1]), instance, routes)

    plt.tight_layout()
    plt.savefig(make_path(_FIGS_DIR, "png"))
    plt.close(fig)

def main():
    args = parse_args()

    # Make directories to save results
    if args.results_dir is not None:
        res_dir = Path(args.results_dir)
        res_dir.mkdir(exist_ok=args.overwrite)
        (res_dir / _SOLS_DIR).mkdir(exist_ok=args.overwrite)
        (res_dir / _STATS_DIR).mkdir(exist_ok=args.overwrite)
        (res_dir / _FIGS_DIR).mkdir(exist_ok=args.overwrite)

    func = partial(solve, **vars(args))
    func_args = sorted(glob(args.instance_pattern), key=tools.name2size)

    tqdm_kwargs = dict(max_workers=args.num_procs, unit="instance")
    data = process_map(func, func_args, **tqdm_kwargs)

    dtypes = [
        ("inst", "U37"),
        ("ok", "U1"),
        ("obj", int),
        ("iters", int),
        ("time", float),
        ("nb_improv", int),
    ]

    data = np.array(data, dtype=dtypes)

    headers = [
        "Instance",
        "OK",
        "Objective",
        "Iters. (#)",
        "Time (s)",
        "Improv. (#)",
    ]

    table = tools.tabulate(headers, data)

    print("\n", table, "\n", sep="")

    obj_all = data["obj"]
    obj_feas = data[data["ok"] == "Y"]["obj"]

    print(f"      Avg. objective: {obj_all.mean():.0f}", end=" ")
    print(f"(w/o infeas: {obj_feas.mean():.0f})" if obj_feas.size > 0 else "")

    print(f"     Avg. iterations: {data['iters'].mean():.0f}")
    print(f"   Avg. run-time (s): {data['time'].mean():.2f}")
    print(f"Avg. improving moves: {data['nb_improv'].mean():.1f}")
    print(f"        Total not OK: {np.count_nonzero(data['ok'] == 'N')}")

if __name__ == "__main__":
    main()


# Configuration file for the Sphinx documentation builder.

import io
import os
import re
from os.path import dirname as up

version_path = os.path.join(up(up(up(os.path.abspath(__file__)))), "pyfdb/version.py")

__version__ = re.search(
    r'__version__\s*=\s*[\'"]([^\'"]*)[\'"]', io.open(version_path, encoding="utf_8_sig").read()
).group(1)

# -- Project information

project = "Pyfdb"
copyright = "2021, ECMWF"
author = "ECMWF"

release = __version__

# -- General configuration

extensions = [
    "sphinx.ext.duration",
    "sphinx.ext.doctest",
    "sphinx.ext.autodoc",
    "sphinx.ext.autosummary",
    "sphinx.ext.intersphinx",
]

intersphinx_mapping = {
    "python": ("https://docs.python.org/3/", None),
    "sphinx": ("https://www.sphinx-doc.org/en/master/", None),
}
intersphinx_disabled_domains = ["std"]

templates_path = ["_templates"]

# -- Options for HTML output

html_theme = "sphinx_rtd_theme"

# -- Options for EPUB output
epub_show_urls = "footnote"


from tkinter import filedialog, Tk, Label, Entry, Button, Canvas
from pdf2docx import Converter
from PIL import Image, ImageTk

class PDFtoWordConverter:
    def __init__(self, master):
        self.master = master
        self.master.title("PDF to Word Converter")

        # Load logo and resize it to 200x200
        self.logo_path = "D:\Leaning programing\python\PDFtoWord\logo.jpg"
        logo_image = Image.open(self.logo_path)
        logo_image = logo_image.resize((200, 200), Image.ANTIALIAS)
        self.logo_image = ImageTk.PhotoImage(logo_image)

        # Display logo
        canvas = Canvas(self.master, width=200, height=200)
        canvas.create_image(0, 0, anchor='nw', image=self.logo_image)
        canvas.grid(row=0, column=0, columnspan=2, pady=10)

        # Input entry
        self.pdf_path_entry = Entry(self.master, width=40)
        self.pdf_path_entry.grid(row=1, column=0, padx=10, pady=10)

        # Browse button
        browse_button = Button(self.master, text="Browse", command=self.browse_pdf)
        browse_button.grid(row=1, column=1, pady=10)

        # Convert button
        convert_button = Button(self.master, text="Convert", command=self.convert_pdf_to_word)
        convert_button.grid(row=2, column=0, columnspan=2, pady=10)

    def browse_pdf(self):
        pdf_path = filedialog.askopenfilename(title="Select PDF file", filetypes=[("PDF files", "*.pdf")])
        self.pdf_path_entry.delete(0, "end")
        self.pdf_path_entry.insert(0, pdf_path)

    def convert_pdf_to_word(self):
        pdf_path = self.pdf_path_entry.get()

        if pdf_path:
            # Convert PDF to Word
            word_path = filedialog.asksaveasfilename(title="Save Word file", defaultextension=".docx", filetypes=[("Word files", "*.docx")])

            if word_path:
                self.convert_pdf_to_docx(pdf_path, word_path)
                print("Conversion successful. Word file saved at", word_path)
            else:
                print("You canceled saving the Word file.")
        else:
            print("You canceled selecting the PDF file.")

    @staticmethod
    def convert_pdf_to_docx(pdf_path, output_path):
        cv = Converter(pdf_path)
        cv.convert(output_path)
        cv.close()

if __name__ == "__main__":
    root = Tk()
    app = PDFtoWordConverter(root)
    root.mainloop()


#!python3

import json
import requests
import logging
import isodate
from datetime import timedelta

HEADERS = {'Content-type': 'application/json'}
LOGGER = logging.getLogger('REST')

def get(url, params=[]):
  "This sends a get to a URL and returns a json"
  response = requests.get(url, params=params, headers=HEADERS, timeout=2.0)
  if not response.ok:
    response.raise_for_status()
  jData = response.json()
  return jData

def post(data, url, params=[]):
  "This posts a json to a URL and returns a json"
  dataString = json.dumps(data, separators=(',', ':'))
  response = requests.post(url, data=dataString, params=params, headers=HEADERS, timeout=2.0)
  if (not response.ok):
    LOGGER.error("Request returned an invalid status. Text output : " + response.text)
    LOGGER.error("To reproduce : curl -X POST '" + url + "' --data '" + dataString + "' --header \"Content-Type:application/json\" with params " + str(params))
    response.raise_for_status()
  return response

def formatDatetime(d):
  "This formats a datetime in a format that can be understood on JAX RS side"
  string = d.strftime("%Y-%m-%dT%H:%M:%S")
  return "%s.%03dZ" % (string, d.microsecond / 1000.0)

def iso_to_seconds(time_iso):
  timedeltaObject = isodate.parse_duration(time_iso)
  seconds = (timedeltaObject / timedelta(seconds=1))
  return seconds


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

import string
import sys
import unicodedata
from collections import namedtuple

spectuple = namedtuple('SpecificPlatform', ['dist',
                                            'major_vers',
                                            'full_vers',
                                            'stability'])

def normalize_name(s):
    """In Python 2, replaces all non-alphanumeric characters with ``_``. In
    Python 3, uses the `unicode identifiers rules
    <https://docs.python.org/3/reference/lexical_analysis.html#identifiers>`_
    to determine which characters to replace.

    :type s: str
    :param s: normalize

    Returns a string.
    """
    if sys.version_info < (3, 0):
        valid = lambda x: x in string.ascii_letters + string.digits
    else:
        valid = lambda x: unicodedata.category(x) in ('Lu', 'Ll', 'Lt', 'Lm', 'Lo', 'Nl', 'Mn', 'Mc', 'Nd', 'Pc')
    s = list(s.lower())
    for i, c in enumerate(s):
        s[i:i+1] = c if valid(c) else '_'
    return ''.join(s)


import logging
import concurrent.futures
import time
import gc
import os

from shared.csv_editor import CSVEditor
from shared.imports import Importer

class Main:

    def __init__(self,name,chunk_size,max_workers,input_file,start_from,count_checkpoint=100,testing=False):
        self.name = name
        self.testing = testing
        self.chunk_size = chunk_size
        self.max_workers = max_workers
        self.input_file = input_file
        self.start_from = start_from
        self.count_checkpoint = count_checkpoint
        try: os.mkdir("logs")
        except: pass
        logging.basicConfig(filename=f'logs/main.log', level=logging.INFO, filemode='w', format='%(asctime)s - %(levelname)s - %(message)s', datefmt='%d-%m-%y %H:%M:%S')
        logging.info(f"{self.name} script started.")
        self.checker = Importer(self.name).checker
        self.run_chunk()

    def run_chunk(self):
        editor = CSVEditor(input_file=self.input_file,name=self.name)
        self.all_records = editor.input_records
        logging.info(f"{len(self.all_records)} emails to check.")

        for number in range(self.start_from,len(self.all_records),self.chunk_size):
            self.skipped_emails = []
            self.chunk_results = []
            self.chunk = []
            self.count = 0

            self.create_chunk(number)
            self.process_chunk()

            editor.save(results=self.chunk_results,skipped=self.skipped_emails)

            gc.collect()

            logging.info(f"Chunk completed. {len(self.chunk_results)} emails saved. {len(self.skipped_emails)} email skipped.")
            print(f"Chunk completed. {len(self.chunk_results)} emails saved. {len(self.skipped_emails)} email skipped.")

            if self.testing:
                break

    def create_chunk(self,index):
        for x in range(index,index+self.chunk_size):
            try:
                self.chunk.append(self.all_records[x])
            except Exception as exc:
                print(exc)
                break
        print(f"Scraping {index}-{index+self.chunk_size}.")
        logging.info(f"Scraping {index}-{index+self.chunk_size}.")

    def process_chunk(self):
        start = time.time()
        with concurrent.futures.ThreadPoolExecutor(max_workers=self.max_workers) as executor:
            futures = []
            for pair in self.chunk:
                futures.append(executor.submit(self.checker,pair))
            for future in concurrent.futures.as_completed(futures):
                r = future.result().response
                if "@" in str(r[0]):
                    self.chunk_results.append(r[0])
                if "@" in str(r[1]):
                   self.skipped_emails.append(r[1])
                self.count+=1
                if self.count%self.count_checkpoint==0:
                    print(f"{self.count} emails checked.")
                    logging.info(f"{self.count} emails checked.")
        end = time.time()

        logging.info(f"{self.count} emails checked.")
        logging.info(f"Total: {end-start}")

Main(start_from=0,chunk_size=200,max_workers=10,input_file="2324mayVSfeed",name="Amazon",testing=True)


import settings as s

def escape_path(path):
    return path.replace(":", "%3A").replace("/", "%2F")

class AccessPointNameConverter(object):
    ap_name_to_formal_name = {}
    ap_index_to_of_name = {}
    ap_of_name_to_index = {}

    def get_of_name_by_name(self, ap_name):
        if ap_name in self.ap_name_to_formal_name:
            return self.ap_name_to_formal_name[ap_name]
        else:
            ap_id = int(ap_name[len(s.AP_NAME_PREFIX):])
            formal_ap_name = "of:1" + hex(ap_id).split('x')[-1].zfill(15)
            self.ap_name_to_formal_name[ap_name] = formal_ap_name
            return formal_ap_name

    def get_id_by_of_name(self, ap_name):
        if ap_name not in self.ap_of_name_to_index:
            ap_index = int(ap_name.split(":1")[1], 16) - 1
            self.ap_index_to_of_name[ap_index] = ap_name
            self.ap_of_name_to_index[ap_name] = ap_index
        else:
            ap_index = self.ap_of_name_to_index[ap_name]
        return ap_index

    def get_of_name_by_index(self, ap_index):
        if ap_index in self.ap_index_to_of_name:
            return self.ap_index_to_of_name[ap_index]
        else:
            ap_name = "of:" + hex(int(ap_index + 1)).split('x')[-1].zfill(16)
            self.ap_index_to_of_name[ap_index] = ap_name
            return ap_name

if __name__ == '__main__':
    ap_name_converter = AccessPointNameConverter()


from django.shortcuts import render
from django.http import HttpResponse
from .models import Curso, Estudiante, Profesor
from django.template import Template, Context, loader
from AppClassBook.forms import CursoForm, EstudianteForm, ProfesorForm

# Create your views here.

def inicio(request):
     return render(request,"AppClassBook/inicio.html")

def cursos(request):

     if request.method=="POST":
          form=CursoForm(request.POST)
          if form.is_valid():
               informacion=form.cleaned_data
               nombre1=informacion["nombre"]
               camada1=informacion["camada"]

               curso1=Curso(nombre=nombre1,camada=camada1)
               curso1.save()
               return render(request,"AppClassBook/inicio.html")
     else:
          formulario=CursoForm()

     return render(request,"AppClassBook/cursos.html", {"form1":formulario})

def estudiantes(request):
     if request.method=="POST":
          form=EstudianteForm(request.POST)
          if form.is_valid():
               informacion1=form.cleaned_data
               nombre2=informacion1["nombre"]
               apellido1=informacion1["apellido"]
               libreta1=informacion1["libreta"]
               email1=informacion1["email"]

               estudiante1=Estudiante(nombre=nombre2,apellido=apellido1,libreta=libreta1,email=email1)
               estudiante1.save()
               return render(request,"AppClassBook/inicio.html")
     else:
          formulario1=EstudianteForm()

     return render(request,"AppClassBook/estudiantes.html", {"form2":formulario1})

def profesores(request):
     if request.method=="POST":
          form=ProfesorForm(request.POST)
          if form.is_valid():
               informacion2=form.cleaned_data
               nombre3=informacion2["nombre"]
               apellido2=informacion2["apellido"]
               email2=informacion2["email"]

               profesor1=Profesor(nombre=nombre3,apellido=apellido2,email=email2)
               profesor1.save()
               return render(request,"AppClassBook/inicio.html")
     else:
          formulario=ProfesorForm()

     return render(request,"AppClassBook/profesores.html", {"form3":formulario})

def BusquedaEstudiante(request):
     return render(request, "AppClassBook/BusquedaEstudiante.html")

def buscar(request):

     if request.GET["libreta"]:
          libreta=request.GET['libreta']
          estudiantes=Estudiante.objects.filter(libreta__icontains=libreta)
          return render(request, "AppClassBook/ResultadoBusquedaEst.html", {"estudiantes":estudiantes, "libreta":libreta })

     else:
          respuesta="No enviaste datos"
     return HttpResponse(respuesta)


from .asset import Asset
import pandas as pd
import numpy as np
from datetime import datetime, timedelta

class PreferredStock(Asset):
    def __init__(self,name,symbol,description,pvalue,dividend):
        super().__init__(name,symbol,description,pvalue,dividend)
        self.currentPrice = None
        self.type = 1    # Zero to identify common stock
        self.fixedDividend = None

    def getAssetInfo(self):
        return {'Name':self.name, 'Description':self.description, 'Par Value':self.pvalue, 'Dividend':self.fixedDividend,'Symbol':self.symbol}

    def getPrice(self,price):
        try:
            self.currentPrice = float(price)
            1/self.currentPrice
        except ValueError:
          print('Introduce a number to set price')
        except ZeroDivisionError:
          print('Price can not be Zero, use another value')

    def getFixedDividend(self,fixedDividend):
       try:
           self.fixedDividend = float(fixedDividend)
       except ValueError:
          print('Introduce a number to set fixed dividend')

    def getStockPE(self):
        try:
          return self.currentPrice/self.dividend
        except ZeroDivisionError:
          print ('PE set to Zero')
          return 0

    def getDividendYield(self):
        try:
            return self.fixedDividend*self.pvalue/self.currentPrice
        except ZeroDivisionError:
            print('Dividend is zero. There is no yield')

    def recordInfo(self,tradeInfo):
        if not hasattr(self,'state'):
            self.state = pd.DataFrame(tradeInfo,index=[1])

        else:
            self.state = self.state.append(pd.DataFrame(tradeInfo,index=[1]),ignore_index=True,sort=True)

    def getTradeInfo(self,quantity,price,indicator):
        trade = {'Time':datetime.now(),'Quantity':int(quantity), 'Price':price, 'Indicator':indicator,'Amount':quantity*price}
        try:
            1/price
            self.currentPrice = float(price)
        except ValueError:
          print('Introduce a number to set price')
        except ZeroDivisionError:
          print('Price can not be Zero, use another value')
        self.recordInfo(trade)

    def calculateVolumeWeightedStockPrice(self,minutes = 5):
        currentTime = datetime.now()
        deltaTime = timedelta(minutes=minutes)
        quantity = np.array(self.state['Quantity'][self.state['Time']>=currentTime-deltaTime].loc[:].values)
        price = np.array(self.state['Price'][self.state['Time']>=currentTime-deltaTime].loc[:].values)
        return np.average(price,weights=quantity)

    def getLastTrade(self):
        return self.state.iloc[-1]


# https://atcoder.jp/contests/abc002/tasks/abc002_4
# お互いに知っているかだから人づてはだめ→unionfindじゃない
# 全結合になっている領域が良い

import sys
read = sys.stdin.readline

def read_ints():
    return list(map(int, read().split()))

def iter_p_adic(p, length):
    '''
    連続して増加するp進数をリストとして返す。lengthはリストの長さ
    return
    ----------
    所望のp進数リストを次々返してくれるiterator
    '''
    from itertools import product
    tmp = [range(p)] * length
    return product(*tmp)

from collections import defaultdict
from itertools import combinations
# ビット全探索で愚直に確かめる
N, M = read_ints()
graph = defaultdict(lambda: [])
for _ in range(M):
    x, y = read_ints()
    graph[x - 1].append(y - 1)
    graph[y - 1].append(x - 1)

def g_candi(perm):
    candi = []
    for i, p in enumerate(perm):
        if p == 0:  # この国会議員は使わない
            continue
        candi.append(i)
    return candi

ans = 0
for perm in iter_p_adic(2, N):
    n = sum(perm)
    for a, b in combinations(g_candi(perm), 2):
        if b not in graph[a]:  # ng
            n = -1
            break
    ans = max(n, ans)
print(ans)


n, m=map(int, input().split())
not_listen=set()
not_look=set()
for i in range(n):
    x=input()
    not_listen.add(x)
for i in range(m):
    y=input()
    not_look.add(y)
res=sorted(list(not_listen & not_look))
print(len(res))
for i in res:
    print(i)


"""`ExportCableInstallation` and related processes."""

__author__ = "Jake Nunemaker"
__copyright__ = "Copyright 2020, National Renewable Energy Laboratory"
__maintainer__ = "Jake Nunemaker"
__email__ = "jake.nunemaker@nrel.gov"

from copy import deepcopy
from math import ceil

from marmot import process

from wisdem.orbit.core.logic import position_onsite
from wisdem.orbit.phases.install import InstallPhase
from wisdem.orbit.core.exceptions import InsufficientCable

from .common import SimpleCable as Cable
from .common import (
    lay_cable,
    bury_cable,
    dig_trench,
    pull_in_cable,
    landfall_tasks,
    lay_bury_cable,
    splice_process,
    terminate_cable,
    load_cable_on_vessel,
)

class ExportCableInstallation(InstallPhase):
    """Export Cable Installation Phase"""

    phase = "Export Cable Installation"
    capex_category = "Export System"

    #:
    expected_config = {
        "landfall": {"trench_length": "km (optional)"},
        "export_cable_install_vessel": "str | dict",
        "export_cable_bury_vessel": "str | dict (optional)",
        "export_cable_trench_vessel": "str (optional)",
        "site": {"distance": "km"},
        "plant": {"capacity": "MW"},
        "export_system": {
            "cable": {
                "linear_density": "t/km",
                "sections": [("length, km", "speed, km/h (optional)")],
                "number": "int (optional)",
            },
            "interconnection_distance": "km (optional); default: 3km",
            "interconnection_voltage": "kV (optional); default: 345kV",
        },
    }

    def __init__(self, config, weather=None, **kwargs):
        """
        Creates an instance of ExportCableInstallation.

        Parameters
        ----------
        config : dict
            Simulation specific configuration.
        weather : np.ndarray
            Weather profile at site.
        """

        super().__init__(weather, **kwargs)

        config = self.initialize_library(config, **kwargs)
        self.config = self.validate_config(config)

        self.initialize_port()
        self.extract_distances()
        self.setup_simulation(**kwargs)

    def setup_simulation(self, **kwargs):
        """
        Setup method for the ExportCableInstallation phase.
        - Extracts key inputs
        - Performs onshore infrastructure construction
        - Routes to specific setup scripts based on configured strategy.
        """

        depth = self.config["site"]["depth"]
        system = self.config["export_system"]
        self.free_cable_length = system.get("free_cable_length", depth / 1000)

        self.cable = Cable(system["cable"]["linear_density"])
        self.sections = system["cable"]["sections"]
        self.number = system["cable"].get("number", 1)

        self.initialize_installation_vessel()
        self.initialize_burial_vessel()
        self.initialize_trench_vessel()

        # Perform onshore construction
        onshore = kwargs.get("include_onshore_construction", True)
        if onshore:
            self.onshore_construction(**kwargs)

        # Perform cable installation
        install_export_cables(
            self.install_vessel,
            sections=self.sections,
            cable=self.cable,
            number=self.number,
            distances=self.distances,
            burial_vessel=self.bury_vessel,
            trench_vessel=self.trench_vessel,
            free_cable_length=self.free_cable_length,
            **kwargs,
        )

    @property
    def system_capex(self):
        """Returns total procurement cost of the array system."""

        return self.config["export_system"]["system_cost"]

    def extract_distances(self):
        """Extracts distances from input configuration or default values."""

        site = self.config["site"]["distance"]
        try:
            trench = self.config["landfall"]["trench_length"]

        except KeyError:
            trench = 1

        self.distances = {"site": site, "trench": trench}

    def onshore_construction(self, **kwargs):
        """
        Performs onshore construction prior to the installation of the export
        cable system.

        Parameters
        ----------
        construction_time : int | float
            Amount of time onshore construction takes.
            Default: 48h
        construction_rate : int | float
            Day rate of onshore construction.
            Default: 50000 USD/day
        """

        construction_time = kwargs.get("onshore_construction_time", 0.0)
        construction_cost = self.calculate_onshore_transmission_cost(**kwargs)

        if construction_time:
            _ = self.env.timeout(construction_time)
            self.env.run()

        self.env._submit_log(
            {
                "action": "Onshore Construction",
                "agent": "Onshore Construction",
                "duration": construction_time,
                "cost": construction_cost,
                "location": "Landfall",
            },
            level="ACTION",
        )

    def calculate_onshore_transmission_cost(self, **kwargs):
        """
        Calculates the cost of onshore transmission costs. From legacy
        OffshoreBOS model.
        """

        capacity = self.config["plant"]["capacity"]

        voltage = self.config["export_system"].get("interconnection_voltage", 345)
        distance = self.config["export_system"].get("interconnection_distance", 3)

        switchyard_cost = 18115 * voltage + 165944
        onshore_substation_cost = (0.165 * 1e6) * capacity  # From BNEF Tomorrow's Cost of Offshore Wind
        onshore_misc_cost = 11795 * capacity**0.3549 + 350000
        transmission_line_cost = (1176 * voltage + 218257) * (distance ** (1 - 0.1063))

        onshore_transmission_cost = (
            switchyard_cost + onshore_substation_cost + onshore_misc_cost + transmission_line_cost
        )

        return onshore_transmission_cost

    def initialize_installation_vessel(self):
        """Creates the export cable installation vessel."""

        # Vessel name and costs
        vessel_specs = self.config.get("export_cable_install_vessel", None)
        name = vessel_specs.get("name", "Export Cable Installation Vessel")

        vessel = self.initialize_vessel(name, vessel_specs)
        self.env.register(vessel)

        vessel.initialize()
        self.install_vessel = vessel

    def initialize_burial_vessel(self):
        """Creates the export cable burial vessel."""

        # Vessel name and costs
        vessel_specs = self.config.get("export_cable_bury_vessel", None)
        if vessel_specs is None:
            self.bury_vessel = None
            return

        name = vessel_specs.get("name", "Export Cable Burial Vessel")

        vessel = self.initialize_vessel(name, vessel_specs)
        self.env.register(vessel)

        vessel.initialize()
        self.bury_vessel = vessel

    def initialize_trench_vessel(self):
        """Creates the export cable trenching vessel."""

        # Vessel name and costs
        vessel_specs = self.config.get("export_cable_trench_vessel", None)
        if vessel_specs is None:
            self.trench_vessel = None
            return
        name = vessel_specs.get("name", "Export Cable Trench Vessel")

        vessel = self.initialize_vessel(name, vessel_specs)
        self.env.register(vessel)

        vessel.initialize()
        vessel.at_port = True
        vessel.at_site = False
        self.trench_vessel = vessel

    @property
    def detailed_output(self):
        """Detailed outputs of the export system installation."""

        outputs = {self.phase: {**self.agent_efficiencies}}

        return outputs

@process
def install_export_cables(
    vessel,
    sections,
    cable,
    number,
    distances,
    burial_vessel=None,
    trench_vessel=None,
    free_cable_length=None,
    **kwargs,
):
    """
    Simulation of the installation of export cables.

    Parameters
    ----------
    vessel : Vessel
        Cable installation vessel.
    sections : float
        Section lengths of the export cable.
    cable : SimpleCable | Cable
        Cable type to use.
    number : int
        Number of export cables.
    distances : dict
        Distances required for export cable installation simulation:
        site : int | float
            Distance between from the offshore substation and port. For
            simplicity, the cable landfall point is assumed to be at port.
        trench : int | float
            Trench length at landfall. Determines time required to tow the plow
            and pull-in cable (km).
    burial_vessel : Vessel
        Optional configuration for burial vessel. If configured, the
        installation vessel only lays the cable on the seafloor and this
        vessel will bury them at the end of the simulation.
    trench_vessel: Vessel
        Optional configuration for trenching vessel.  If configured, the
        trenching vessel travels along the cable route prior to arrival of
        the cable lay vessel and digs a trench.
    """

    ground_distance = -free_cable_length
    for s in sections:
        try:
            length, speed = s

        except TypeError:
            length = s

        ground_distance += length

    # Conduct trenching operations
    if trench_vessel is None:
        pass

    else:
        for _ in range(number):
            # Trenching vessel can dig a trench during inbound or outbound journey
            if trench_vessel.at_port:
                trench_vessel.at_port = False
                yield dig_export_cables_trench(trench_vessel, ground_distance, **kwargs)
                trench_vessel.at_site = True
            elif trench_vessel.at_site:
                trench_vessel.at_site = False
                yield dig_export_cables_trench(trench_vessel, ground_distance, **kwargs)
                trench_vessel.at_port = True

        # If the vessel finishes trenching at site, return to shore
        # TODO: replace with demobilization method
        if trench_vessel.at_site:
            trench_vessel.at_site = False
            yield trench_vessel.transit(ground_distance, **kwargs)
        trench_vessel.at_port = True

    for _ in range(number):
        vessel.cable_storage.reset()
        yield load_cable_on_vessel(vessel, cable, **kwargs)

        # At Landfall
        yield landfall_tasks(vessel, distances["trench"], **kwargs)

        for s in sections:
            splice_required = False
            try:
                length, speed = s
                if burial_vessel is None:
                    specs = {**kwargs, "cable_lay_bury_speed": speed}

                else:
                    specs = {**kwargs, "cable_lay_speed": speed}

            except TypeError:
                length = s
                specs = deepcopy(kwargs)

            remaining = length
            while remaining > 0:
                if splice_required:
                    yield splice_process(vessel, **kwargs)

                try:
                    section = vessel.cable_storage.get_cable(remaining)

                except InsufficientCable as e:
                    section = vessel.cable_storage.get_cable(e.current)

                if burial_vessel is None:
                    yield lay_bury_cable(vessel, section, **specs)

                else:
                    yield lay_cable(vessel, section, **specs)

                remaining -= ceil(section)
                if remaining > 0:
                    splice_required = True

                    yield vessel.transit(distances["site"])
                    vessel.cable_storage.reset()
                    yield load_cable_on_vessel(vessel, cable, **kwargs)
                    yield vessel.transit(distances["site"])

        # At Site
        yield position_onsite(vessel, **kwargs)
        yield pull_in_cable(vessel, **kwargs)
        yield terminate_cable(vessel, **kwargs)

        # Transit back to port
        yield vessel.transit(distances["site"])

    if burial_vessel is None:
        vessel.submit_debug_log(
            message="Export cable lay/burial process completed!",
            progress="Export System",
        )

    else:
        vessel.submit_debug_log(message="Export cable lay process completed!")
        bury_export_cables(burial_vessel, ground_distance, number, **kwargs)

@process
def bury_export_cables(vessel, length, number, **kwargs):
    """
    Simulation for the burial of export cables if configured.

    Parameters
    ----------
    vessel : Vessel
        Performing vessel.
    length : float
        Full length of an export cable.
    number : int
        Number of export cables.
    """

    for _ in range(number):
        yield bury_cable(vessel, length, **kwargs)

    vessel.submit_debug_log(
        message="Export cable burial process completed!",
        progress="Export System",
    )

@process
def dig_export_cables_trench(vessel, distance, **kwargs):
    """
    Simulation for digging a trench for the export cables (if configured).

    Parameters
    ----------
    vessel : Vessel
        Performing vessel.
    distance : int | float
        Distance along export cable route to dig trench for cable
    """

    yield position_onsite(vessel, site_position_time=2)
    yield dig_trench(vessel, distance, **kwargs)

    vessel.submit_debug_log(message="Export cable trench digging process completed!")


from collections import defaultdict
import heapq

class AllOne:
    def __init__(self):
        self.ctr = defaultdict(int)
        self.deleted = defaultdict(int)
        self.minheap = []
        self.maxheap = []

    def inc(self, key: str) -> None:
        self.deleted[(self.ctr[key], key)] += 1
        self.deleted[(-self.ctr[key], key)] += 1
        self.ctr[key] += 1
        heapq.heappush(self.minheap, (self.ctr[key], key))
        heapq.heappush(self.maxheap, (-self.ctr[key], key))

    def dec(self, key: str) -> None:
        if self.ctr[key] > 0:
            self.deleted[(self.ctr[key], key)] += 1
            self.deleted[(-self.ctr[key], key)] += 1
            self.ctr[key] -= 1
            heapq.heappush(self.minheap, (self.ctr[key], key))
            heapq.heappush(self.maxheap, (-self.ctr[key], key))

    def getMaxKey(self) -> str:
        while len(self.maxheap) > 0 and self.deleted[self.maxheap[0]] > 0:
            self.deleted[self.maxheap[0]] -= 1
            heapq.heappop(self.maxheap)
        if len(self.maxheap) > 0:
            return self.maxheap[0][1]
        return ""

    def getMinKey(self) -> str:
        while len(self.minheap) > 0 and self.deleted[self.minheap[0]] > 0:
            self.deleted[self.minheap[0]] -= 1
            heapq.heappop(self.minheap)
        if len(self.minheap) > 0:
            return self.minheap[0][1]
        return ""


# !/usr/bin/env python3
# -*- encoding: utf-8 -*-
'''
@项目 :  SignUper
@文件 :  Refresh.py
@时间 :  2021/11/07 08:03
@作者 :  will
@版本 :  1.0
@说明 :

'''
import json
import os
import time

import requests

from Config import Config
from Utils import traverse_dir_files, read_file, write_file

class Refresh(object):

    def __init__(self):
        pass

    def refresh(self):
        link_path_list = traverse_dir_files(Config.LINK_PATH)
        if link_path_list:
            strftime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
            print('********************' + strftime + '*******************')
            for link_file in link_path_list:
                self.load_link_file(link_file)
            print("********************************************************")

    def load_link_file(self, link_file):
        contnent = read_file(link_file)
        if contnent:
            json_links = json.loads(contnent)
            site_name = os.path.basename(link_file)[:-5]
            # 文件加载成功，开始更新订阅
            print(site_name + '链接文件加载成功，开始更新订阅文件....')
            # 下载订阅文件
            for name, link in json_links.items():
                # 构建订阅文件地址
                file_name = site_name + "_" + name
                file_path = Config.SUB_PATH + "/" + file_name
                # 下载订阅文件
                res = requests.get(link)
                if res.status_code == 200:
                    try:
                        # 写入文件
                        if write_file(file_path, res.text):
                            print(file_name + ":订阅更新成功!")
                    except Exception as e:
                        print(file_name + '订阅链接，下载失败！！！')
                        print(e)
                        pass
                else:
                    print(file_name + '订阅链接，请求失败！！！')

if __name__ == '__main__':
    rf = Refresh()
    rf.refresh()


import string

from stock_pandas import StockDataFrame

class SimonKDJIndicator():
    def __init__(self,
                 df: StockDataFrame,
                 k: string = "25",
                 d: string = "3",
                 j: string = "3"):
        self.df = df
        self.j = j
        self.d = d
        self.k = k

    def kdj_indicator(self) -> StockDataFrame:
        self.df.exec(f"kdj.j:{self.k},{self.d},{self.j}", create_column=True)
        self.df.exec(f"kdj.j:{self.k},{self.d},{self.j}", create_column=True)
        self.df.exec(f"kdj.j:{self.k},{self.d},{self.j}/20.0", create_column=True)
        self.df.exec(f"kdj.j:{self.k},{self.d},{self.j}\80.0", create_column=True)
        self.df.exec(f"kdj.j:{self.k},{self.d},{self.j}>=80.0", create_column=True)
        self.df.exec(f"kdj.j:{self.k},{self.d},{self.j}<=20.0", create_column=True)
        return self.df


import lapie
import pickle
import sys

def main():
    udb = sys.argv[1]
    # Get actual routed path using Tcl
    nets = lapie.list_nets(udb)
    routing = lapie.get_routing(udb, nets)

    # (source, sink) -> pips
    arc2pips = {}

    # Keep track of fanout - we'll need this later!
    wire_fanout = {}

    for net in sorted(nets):
        if net not in routing:
            continue
        route = routing[net]
        tree = {}
        # Construct route tree dst->src
        for pip in route.pips:
            tree[pip.node2] = pip.node1
        # Mapping node -> pin
        node2pin = {}
        for pin in route.pins:
            node2pin[pin.node] = (pin.cell, pin.pin)

        for rpin in route.pins:
            pin = (rpin.cell, rpin.pin)
            cursor = rpin.node
            if cursor not in tree:
                continue
            pin_route = []
            while True:
                wire_fanout[cursor] = wire_fanout.get(cursor, 0) + 1
                if cursor not in tree:
                    if cursor in node2pin:
                        # Found a complete (src, sink) route
                        pin_route.reverse()
                        arc2pips[(node2pin[cursor], pin)] = pin_route
                    break
                prev_wire = tree[cursor]
                pin_route.append((prev_wire, cursor))
                cursor = prev_wire
    with open(sys.argv[2], "wb") as pf:
        pickle.dump(dict(arc2pips=arc2pips, wire_fanout=wire_fanout), pf)

if __name__ == '__main__':
    main()


"""module: helper_functions
Author: Bill Duncan

This module contains a number of functions to assist with the querying and processing of tabular data.
See `init_graph` for details on functions that get attached to rdflib.Graph.
* `init_graph`: attaches custom functions an rdflib Graph and returns a new graph
* `add_spo`: shortcut for adding subject, predicate, object URIRefs to graph
* `add_spo`: shortcut for adding subject, predicate URIRefs and literal value to graph
* `sparql_results_to_df`: converts sparql results into a dataframe
* `sparql_query_to_df`: queries the graph and returns the results as a dataframe
* `add_table_metadata`: adds instances of tables and fields to graph
* `add_enums`: adds instances of enumerated values to graph
* `df_to_sql`: creates an sql query based on values in a dataframe (with specific columns)
* `add_df_to_graph`: adds a simple rdf transformation of a dataframe into the graph

"""
import pandas as pds
import itertools
from typing import Optional, List, Any
from rdflib import URIRef, BNode, Literal, Graph, Namespace, RDF, RDFS, OWL
from rdflib.plugins.sparql.processor import SPARQLResult

def init_graph(ontology:str =None, format='ttl') -> Graph:
    """attaches custom functions an rdflib Graph and returns a new graph"""

    # attach helper functions
    Graph.add_spo = add_spo
    Graph.add_spv = add_spv
    Graph.sparql_query_to_df = sparql_query_to_df
    Graph.add_table_metadata = add_table_metadata
    Graph.add_enums = add_enums
    Graph.add_df = add_df

    # intstantiate Graph
    graph = Graph()

    # parse ontology
    if ontology:
        return graph.parse(ontology, format=format)
    else:
        return graph

def add_spo(self: Graph, subj: Any, predicate: Any, obj: Any) -> Graph:
    """shortcut for adding subject, predicate, object URIRefs to graph"""

    self.add((URIRef(subj), URIRef(predicate), URIRef(obj)))
    return self

def add_spv(self: Graph, subj: Any, predicate: Any, val: Any) -> Graph:
    """shortcut for adding subject, predicate URIRefs and literal value to graph"""

    self.add((URIRef(subj), URIRef(predicate), Literal(val)))
    return self

def sparql_results_to_df(results: SPARQLResult, graph=Optional[Graph]) -> pds.DataFrame:
    """converts sparql results into a dataframe"""

    def set_value(x):
        if x is None:
            return None
        elif graph is not None:
            for n in graph.namespaces():
                # each is a tuple of form (<prefix>, URIRef(...))
                # e.g., ('dc', rdflib.term.URIRef('http://purl.org/dc/elements/1.1/'))
                if str(x).startswith(str(n[1])):
                    # replace uri with prefix
                    return str(x).replace(n[1], n[0])

            # if it makes it here, no replacements occurred
            return x.toPython()
        else:
            return x.toPython()

    return \
        pds.DataFrame(
            data=([set_value(x) for x in row] for row in results),
            columns=[str(x) for x in results.vars]
        )

def sparql_query_to_df(self: Graph, query: str, use_ns=True) -> pds.DataFrame:
    """queries the graph and returns the results as a dataframe"""

    results = self.query(query)
    if use_ns:
        return sparql_results_to_df(results, self)
    else:
        return sparql_results_to_df(results, None)

def add_table_metadata(self: Graph,
                        table: pds.DataFrame,
                        table_name: str,
                        table_ns: Namespace,
                        field_ns: Namespace,
                        property_ns: Namespace) -> Graph:
    """adds instances of tables and fields to graph"""

    # add table instance to graph
    table_uri = table_ns[f'/{table_name}']
    self.add_spo(table_uri, RDF.type, table_ns)
    self.add_spv(table_uri, RDFS.label, table_name)

    # add each of the tables fields to graph as instances and subclass of fields
    for field_name in table.columns:
        field_name = f'{table_name}.{field_name}' # prepend table name to field name
        uri = URIRef(field_ns[f'/{field_name}'])
        self.add_spo(uri, RDF.type, field_ns)
        self.add_spo(uri, property_ns.member_of, table_uri)
        self.add_spv(uri, RDFS.label, field_name)

        # *pun* the field as an owl class
        # note: field_ns[:-1] removes the last "/" from the uri
        self.add_spo(uri, RDF.type, OWL.Class)
        self.add_spo(uri, RDFS.subClassOf, field_ns[:-1])

    return self

def add_enums(self: Graph,
             enums: List,
             table_name: str,
             field_name: str,
             enum_ns: Namespace,
             base_ns: Namespace) -> Graph:
    """adds instances of enumerated values to graph"""

    field_name = f'{table_name}.{field_name}' # prepend table name to field name
    for enum in enums:
        # build uri
        uri = URIRef(enum_ns[f'/{field_name}#{enum}'])

        # add instances
        # Note: the literal value is added to the graph as well
        self.add_spo(uri, RDF.type, enum_ns)
        self.add_spv(uri, base_ns.has_value, enum)
        self.add_spv(uri, RDFS.label, f'{field_name} {enum}')

        # enums constrain values in fields, so add this informaton the graph
        field = base_ns[f'field/{field_name}']
        self.add_spo(uri, base_ns.defines_values_in, field)

    return self

def df_to_sql(df: pds.DataFrame) -> str:
    """creates an sql query based on values in a dataframe (with specific columns)"""
    # N.B.: This need to be refactored!!!

    # dict to hold info about the table and class the field represents
    # note: check for enum_value column
    if 'enum_value' in df.columns:
        columns = ['field_name', 'table_name', 'cls_names', 'enum_value']
    else:
        columns = ['field_name', 'table_name', 'cls_names']

    field_info_dict = \
        df[columns].drop_duplicates().set_index('field_name').to_dict(orient='index')

    # gather all table and field names in dataframe
    all_tables = set(df['table_name'])
    all_fields = set(df['field_name'])

    # dict to hold whether the table occurs in a 'from' clause or 'join' clause
    table_clause_dict = {'from': '', 'joins': []}
    for i, tbl in enumerate(all_tables):
        if i == 0:
            table_clause_dict['from'] = tbl
        else:
            table_clause_dict['joins'].append(tbl)

    # dict to hold just those tables that occur in 'join' clause
    # this dict just help build the sql query
    table_join_dict = \
        {tbl:{'joins': []} for tbl in all_tables if tbl != table_clause_dict['from']}

    # iterate over each class in the dataframe
    # b/c each field is part of the same group we know the reprenst the same type of thing
    # so, we can relate the fields in the join clause
    for group_name, group_df in df.groupby('cls_names'):
        # all fields in the same group represent the same type of thing
        # find the pairwise combinations of how each field relates to one another
        field_pairs = \
            list(itertools.combinations(group_df['field_name'], 2))

        # now for each pair add it to the appropriate join table
        for left_field, right_field in field_pairs:
            # fetch table names of fields
            left_table = field_info_dict[left_field]['table_name']
            right_table = field_info_dict[right_field]['table_name']

            # add pair as a join condition (e.g. patients.patient_id = procedures.patient_id)
            if left_table != table_clause_dict['from']:
                table_join_dict[left_table]['joins'].append((left_field, right_field))
            elif right_table != table_clause_dict['from']:
                table_join_dict[right_table]['joins'].append((left_field, right_field))

    # finally, build sql query
    sql = ""

    # SELECT ...
    # form the select list for the sql
    select_fields = \
        [f"  {field} as [{field} ({value['cls_names']})]\n"
        for field, value in field_info_dict.items()]

    select_fields = []
    for field, value in field_info_dict.items():
        # the "as [{field}" is needed to guarantee that the whole
        # field name (i.e., <table>.<field name>) is used in the results
        if 'cls_names' in value.keys():
            select_fields.append(f"  {field} as [{field} ({value['cls_names']})] \n")
        else:
            select_fields.append(f"  {field} as [{field}]\n)")

    sql = sql + f"select \n{'  ,'.join(select_fields)} \n"

    # FROM ... JOIN ...
    # add tables that data is retrieved from (note: there must be a 'from' clause)
    if len(table_clause_dict['from']) > 0:
        sql = sql + f"from {table_clause_dict['from']} \n"

        #  collect joined tables
        for tbl in table_clause_dict['joins']:
            join_fields = table_join_dict[tbl]['joins']
            if len(join_fields) > 0:
                sql = sql + f"inner join {tbl} on \n"

                # put an '=' between each pair of fields
                field_pairs = [f"{' = '.join(field_pair)}\n" for field_pair in join_fields]
                sql = sql + f"  {'  and '.join(field_pairs)}"

    # WHERE ...
    # check if enum value used for filter
    if 'enum_value' in df.columns:
        #
        print_where = True
        for idx, field_name, enum_value in df[['field_name', 'enum_value']].itertuples():
            if print_where and enum_value is not None and len(enum_value) > 0:
                sql = sql + f"where {field_name} = '{enum_value}' \n"
                print_where = False
            elif enum_value is not None and len(enum_value) > 0:
                sql = sql + f"and {field_name} = '{enum_value}' \n"

    # return sql query
    return sql

def add_df(self: Graph, df: pds.DataFrame, table_name, field_ns: Namespace, base_ns: Namespace) -> Graph:
    """adds a simple rdf transformation of a dataframe into the graph"""

    # add table to graph
    table_uri = base_ns[f'table/{table_name}']
    self.add_spo(table_name, RDF.type, base_ns.table)

    for row in df.itertuples(index=None):
        # add row as blank node to table
        row_uri = BNode()
        self.add_spo(row_uri, RDF.type, base_ns.row)
        self.add_spo(row_uri, base_ns.member_of, table_uri)

        # for each field in row
        for field_name, value in row._asdict().items():
            # add instance of field value using blank node
            field_value_uri = BNode()
            self.add_spo(field_value_uri, RDF.type, base_ns.field_value)
            self.add_spv(field_value_uri, base_ns.has_value, value)

            # relate row to field value using *punned* field name
            field_uri = field_ns[f'/{table_name}.{field_name}']
            self.add_spo(field_uri, RDF.type, OWL.ObjectProperty)
            self.add_spo(row_uri, field_uri, field_value_uri)

            # relate field value to field and field to row via 'member of'
            field_instance_uri = BNode()
            self.add_spo(field_instance_uri, RDF.type, field_uri)
            self.add_spo(field_instance_uri, base_ns.member_of, row_uri)
            self.add_spo(field_value_uri, base_ns.member_of, field_instance_uri)

    return self


#! /home/daim/plebdev/developing-cln-plugins/venv/bin/python

# copied from https://lnroom.live/2023-03-28-live-0001-understand-cln-plugin-mechanism-with-a-python-example/

import sys
import os
import json
import socket

myplugin_out="/tmp/plugin_out"
if os.path.isfile(myplugin_out):
    os.remove(myplugin_out)
def printout(s):
    with open(myplugin_out, "a") as output:
        output.write(s)

# getmanifest
request = sys.stdin.readline()
sys.stdin.readline() # "\n"

printout("GETMANIFEST\n")
printout(request)
printout("\n==================================================\n\n")

req_id = json.loads(request)["id"]

manifest = {
    "jsonrpc": "2.0",
    "id": req_id,
    "result": {
        "dynamic": True,
        "options": [{
            "name": "foo_opt",
            "type": "string",
            "default": "bar",
            "description": "description"
        }],
        "rpcmethods": [{
            "name": "myplugin",
            "usage": "",
            "description": "description"
        }]
    }
}

sys.stdout.write(json.dumps(manifest))
sys.stdout.flush()

# init
request = sys.stdin.readline()
sys.stdin.readline() # "\n"

printout("INIT\n")
printout(request)
printout("\n==================================================\n\n")


ND = 'NODATA'

def hexbyte(byte):
    def hexwrd(x):
        return '{:02X}'.format(x)
    return ' '.join(hexwrd(a) if a != ND else '**' for a in byte)

def asciibyte(byte):
    return ''.join(chr(a) if (a > 32 and a < 127) else '.' for a in byte)

def view(packet):
    b = []
    w = []
    words = []
    for a in packet:
        b.append(ord(a))
        if len(b) == 8:
            w.append(b)
            b = []
            if len(w) == 2:
                words.append(w)
                w = []
    # Pad any missing byts with 0 to make both words
    if b:
        while len(b) < 8:
            b.append(ND)
        w.append(b)
    if w:
        if len(w) == 1:
            w.append([ND] * 8)
        words.append(w)
    for word in words:
        hword, lword = word
        line_parts = [
            hexbyte(hword),
            '  ',
            hexbyte(lword),
            '    ',
            asciibyte(hword),
            '  ',
            asciibyte(lword)
        ]
        print(''.join(line_parts))

if __name__ == '__main__':
    with open('packet.txt', 'rb') as fp:
        packet = fp.read()
    view(packet)


from PySide import QtGui, QtCore

class SqueezeRow(QtGui.QWidget):
    LEFT,RIGHT = 0,1
    def __init__(self,widget,align=RIGHT):
        QtGui.QWidget.__init__(self)

        layout = QtGui.QHBoxLayout()
        self.setLayout(layout)

        layout.addStretch(1)

        layout.insertWidget(
            {
                self.LEFT:0,
                self.RIGHT:1
            }[align],
            widget,
            0
        )


import pandas as pd
import requests

class Hefesto():
    def __init__(self, path):

        # Import CSV using pandas:
        self.path = path
        self.df = pd.read_csv(path)

        # Create an object as dictonary to reduce duplicate calls:
        self.reg = dict()

    def get_uri(self, col, ont):

        # Column duplication to work in new column:
        self.df[col+"_uri"] = self.df.loc[:, col]

        # Loop throught new column to replace current value with API term:
        for i in self.df[col+"_uri"].index:
            term = self.df.at[i,col+"_uri"]
            if term in self.reg:
                self.df.at[i,col+"_uri"] = self.reg[term]

            else: # API call to OLS
                url= "http://www.ebi.ac.uk/ols/api/search?q="+ term +"&ontology=" + ont
                r = requests.get(url,headers= {"accept":"application/json"})
                data = r.json()
                # JSON navigation:
                data_iri = data["response"]["docs"][0]["iri"]
                # Attach new value to the Dataframe:
                self.reg[term] = data_iri
                self.df.at[i,col+"_uri"] = data_iri

        print(self.df)
        return self.df

    def get_label(self, col):

        # Column duplication to work in new column:
        self.df[col+"_label"] = self.df.loc[:, col]

        # Loop throught new column to replace current value with API term:
        for i in self.df[col+"_label"].index:
            term = self.df.at[i,col+"_label"]
            if term in self.reg:
                self.df.at[i,col+"_label"] = self.reg[term]

            else: # API call to OLS
                url= 'http://www.ebi.ac.uk/ols/api/terms?iri='+ term
                r = requests.get(url,headers= {"accept":"application/json"}) # API call to OLS
                data = r.json()
                # JSON navigation:
                data_label = data["_embedded"]["terms"][0]["label"]
                # Attach new value to the Dataframe:
                self.reg[term] = data_label
                self.df.at[i,col+"_label"] = data_label

        print(self.df)
        return self.df

    def replace_inf(self,col, key, value, duplicate):
        if duplicate == "True":
            # Column duplication to work in new column:
            self.df[col+"_dup"] = self.df.loc[:, col]
            self.df[col+"_dup"] = self.df[col+"_dup"].replace([key],value)
        else:
            self.df[col] = self.df[col].replace([key],value)
        print(self.df)
        return self.df

# trial

trial = Hefesto('/home/pabloalarconm/Desktop/EJP/MEDUSA/trialCDE/hefesto/DEPRECATED/trial_hefesto.csv')
trial.get_uri("output1","sio")
trial.get_label("output2")
trial.replace_inf("output1", "information content entity","information content procedure")


def copylist( x ):#a = [1,2,[3,4],[[5,6],8]]

    if type(x) != list:
        y = x
    else:
        y = list()
        for i in x:
            y.append(copylist(i))
    return y

exec(input().strip()) # do not remove this line


string = '''
'''
import numpy as np
r = []
for i in string.split('\n'):
    if 'test' in i:
        line = i.split(':  ')[-1].split(',')
        r.append([float(p) for p in line])
r = np.array(r)

for i in r[:,1]:print(i)

string = ''' {{}} &
		{\includegraphics[width=0.2\\textwidth]{photo/table_ctm_nn_city/{{}}-pm25-1.png}} &
        {\includegraphics[width=0.2\\textwidth]{photo/table_ctm_nn_city/{{}}-pm25-4.png}} &
        {\includegraphics[width=0.2\\textwidth]{photo/table_ctm_nn_city/{{}}-pm25-7.png}} &
        {\includegraphics[width=0.2\\textwidth]{photo/table_ctm_nn_city/{{}}-pm25-10.png}} \\\\
        \hline
        '''
def format_Lin(*r):
    s = string.split('{{}}')
    res = ''
    for i in range(len(s)):
        if i<len(r):
            res = res + s[i] + r[i]
        else:
            res = res+s[i]
    return res

for c1,c2 in [['TianJin','tj'],['ShiJiazhuang','sjz'],['NingBo','nb'],['LanZhou','lz'],\
              ['XiAn','xa'],['XinJiang','xj'],['SanYa','sy'],['HeFei','hf'],['ShangHai','sh'],\
              ['GuangZhou','gz'],['XiaMen','xm']]:
    print(format_Lin(c1,c2,c2,c2,c2))


import maya.cmds as cmds
import pymel.core as pm
import pymel.core.datatypes as dt
from tqdm import tqdm, trange

import attachedLocalSpace as als

import importlib
importlib.reload(als)

def connectMeshToTree(meshNode, treeNode, index):
    meshNode.outMesh.connect(treeNode.inMeshes[index].inMesh)
    meshNode.parent(0).worldMatrix.connect(treeNode.inMeshes[index].inWorldMatrix)

def connectMeshToDeform(treeNode,chainId,meshId,subIds):
    deformIdx = treeNode.deformedMeshes.get(s=True)
    curDeformMesh = treeNode.deformedMeshes[deformIdx]
    curDeformMesh.deformedChainId.set(chainId)
    curDeformMesh.deformedMeshId.set(meshId)
    curDeformMesh.deformedPointIds.set(subIds)

def connectRootFrame(rootJoint, treeNode, idx, rootMatrix):
    rootFrameAttr = treeNode.chains[idx].rootFrame
    rootP = dt.Point(rootJoint.getTranslation("world"))
    locP = rootP * rootMatrix
    rootFrameAttr.offset.set([locP.x, locP.y, locP.z])
    q = rootJoint.getRotation(quaternion=True) * rootJoint.getOrientation()
    q *= dt.Quaternion(rootMatrix)
    rootFrameAttr.rootQuat.set([q.x, q.y, q.z, q.w])

def connectFrame(joint, treeNode, idx, idx2):
    frame = treeNode.chains[idx].frames[idx2]
    frame.xOffset.set(joint.tx.get())
    q = joint.getRotation(quaternion=True) * joint.getOrientation()
    frame.frameQuat.set([q.x, q.y, q.z, q.w])

def connectJointHierarchyToTree(joint, treeNode, chainId, idx2):
    connectFrame(joint, treeNode, chainId, idx2)
    for subJoint in joint.getChildren():
        connectJointHierarchyToTree(subJoint, treeNode, chainId, idx2+1)

def inputChain(treeNode, rootJoint, chainId, rootMatrix):
    # input joints
    rootJointNode = pm.PyNode(rootJoint)
    rootMatrix = rootMatrix.inverse()
    connectRootFrame(rootJointNode, treeNode, chainId, rootMatrix)

    for subJoint in rootJointNode.getChildren():
        connectJointHierarchyToTree(subJoint, treeNode, chainId, 0)

def parentChainFrame(parentJoint, rootP):
    jointNodes = pm.ls(parentJoint,dag=True,type="joint")
    nearestDist = 1000000
    curDist = 0
    nearestId = 0
    idx = 0
    nearestJointNode = None
    for joint in jointNodes:
        p = pm.joint(joint,q=True,p=True)
        curDist = (p - rootP).length()
        if nearestDist > curDist:
            nearestDist = curDist
            nearestId = idx
            nearestJointNode = joint

        idx += 1

    return nearestJointNode, nearestId

def setTreeInputForDeformedMesh(treeNode, meshId, subPIds, rootJoint, chainId, parentJoint=None):
    rootMatrix = dt.Matrix()
    rootMatrix.setToIdentity()
    # input rootMeshes
    if parentJoint:
        rootP = cmds.xform(rootJoint, q=True, ws=True, t=True)
        rootP = dt.Point(rootP)
        parentFrameJoint, rootChainFrameId = parentChainFrame(parentJoint, rootP)
        treeNode.chains[chainId].trunkFrameId.set(rootChainFrameId)
        rootMatrix = pm.xform(parentFrameJoint, q=True, ws=True, m=True)
        rootMatrix = dt.Matrix(rootMatrix)

    # input chain
    inputChain(treeNode, rootJoint, chainId, rootMatrix)

    connectMeshToDeform(treeNode, chainId, meshId, subPIds)

def inputFoliages(treeNode, foliageData, rootMesh, meshId, rootMeshId):
    foliageNum = len(foliageData["pnt"])
    vIdList = [0] * foliageNum * 3
    uvList = [0] * foliageNum * 2
    axisList = [0] * foliageNum * 3
    radianList = [0] * foliageNum
    scaleList = [1] * foliageNum

    itersector = als.createMeshIntersector(rootMesh)

    print("Foliage process is beginning")

    # pbar = tqdm(range(foliageNum))
    for i in range(foliageNum):
        # pbar.set_description('Processing {}'.format(i))

        if i % 10000 == 1:
            print("Processing {:.2f}%".format(i/foliageNum*100))

        rootP = foliageData["pnt"][i]
        rootP = dt.Point(rootP)
        vIds, u, v = als.closestPolygonAndWeights(rootP, rootMesh, itersector)

        vId3 = i*3
        vIdList[vId3  ]=vIds[0]
        vIdList[vId3+1]=vIds[1]
        vIdList[vId3+2]=vIds[2]

        uvId2 = i*2
        uvList[uvId2  ]=u
        uvList[uvId2+1]=v

        qId3 = i*3
        axis = foliageData["rotAxis"][i]

        axisList[qId3  ] = axis[0]
        axisList[qId3+1] = axis[1]
        axisList[qId3+2] = axis[2]

        radianList[i] = foliageData["rotRadian"][i]

        scaleList[i] = foliageData["scale"][i]

    print("Foliage process is done")

    attachedPointAttr = treeNode.foliages
    attachedPointAttr.foliageMeshId.set(meshId)
    attachedPointAttr.foliageAttachedMeshId.set(rootMeshId)
    attachedPointAttr.foliageAttachedPointId.set(vIdList)
    attachedPointAttr.foliageAttachedWeight.set(uvList)
    attachedPointAttr.foliageScale.set(scaleList)
    attachedPointAttr.foliageRotAxis.set(axisList)
    attachedPointAttr.foliageRotRadian.set(radianList)

def skelTreeCreator(meshDataDict):
    if not cmds.pluginInfo("skelTree",q=True,l=True):
        cmds.loadPlugin("skelTree")

    treeNode = pm.PyNode(cmds.createNode('skelTreeCreator'))
    visNode = pm.PyNode(cmds.createNode('skelTreeVisualization'))

    visNode.noiseTrunkValueFreqSpeed.set([0.2,.1,0.05,0.1])
    visNode.noiseBranchValueFreqSpeed.set([0.2,.2,1])
    visNode.noiseLeafValueFreqSpeed.set([1,.2,1,2])
    visNode.windDirection.set([1,0,0])
    visNode.dispChainScale.set(0.1)

    visNode.dispEnableDeformedPoints.set(False)
    visNode.dispEnableDeformedMeshes.set(False)
    visNode.dispEnableChainBoxes.set(False)
    visNode.dispEnableChainLine.set(True)
    visNode.dispEnableAttachedPoint.set(False)
    visNode.dispEnableFoliages.set(False)

    cmds.expression(s="{}.time=frame*0.01;".format(visNode.name()),o=visNode.name(), ae=1, uc="all")

    # input meshes
    meshList = meshDataDict["mesh"]
    idx = 0
    for mesh in meshList:
        meshNode = pm.PyNode(mesh)
        connectMeshToTree(meshNode, treeNode, idx)
        idx += 1

    deformDataList = meshDataDict["deform"]

    # input deformed meshes and chains
    for meshData in deformDataList:
        if meshData[3]:
            rootMeshId = meshList.index(meshData[3])
            setTreeInputForDeformedMesh(treeNode, meshList.index(meshData[0]), meshData[1], meshData[2], meshDataDict["chain"].index(meshData[2]), meshDataDict["chain"][0])
        else:
            setTreeInputForDeformedMesh(treeNode, meshList.index(meshData[0]), meshData[1], meshData[2], meshDataDict["chain"].index(meshData[2]))

    treeNode.outSkelTreeData.connect(visNode.inSkelTreeData)

    if meshDataDict["foliage"]:
        # input foliages
        rootMeshId = 0
        foliageMeshId = 1
        rootMesh = meshList[rootMeshId]
        inputFoliages(treeNode, meshDataDict["foliage"], rootMesh, foliageMeshId, rootMeshId)

# skelTreeCreator({
#     "mesh":["pPlaneShape1", "pCylinderShape1", "pCylinderShape2","pCylinderShape3"],
#     "chain":["joint1","joint10","joint16"],
#     "deform":[
#         ["pCylinderShape1", "joint1", "pPlaneShape1"],
#         ["pCylinderShape2", "joint10", "pCylinderShape1"],
#         ["pCylinderShape3", "joint16", "pCylinderShape1"]
#     ]
# })

# skelTreeCreator({
#     "mesh":["pCylinderShape1"],
#     "chain":["joint1"],
#     "deform":[["pCylinderShape1","joint1",None]]
# })


# Django
from django.urls import path

# Views
from products import views

app_name = 'products'

urlpatterns = [
    path('', views.ProductsHomeView.as_view(), name='home'),
    path('negocios/', views.ProviderListView.as_view(), name='provider-list'),
    path('negocio/<int:pk>/', views.ProviderDetailView.as_view(), name='provider-detail'),
    path('<int:pk>/', views.ProductDetailView.as_view(), name='product-detail'),
    path('pedir/<int:product>/<number>/', views.wa_view, name='wa_view'),
]


from django.contrib import admin
from django.urls import path, include, re_path

from drf_yasg.views import get_schema_view
from drf_yasg import openapi

from rest_framework import permissions

from SimpleCRM.settings.base import VER

schema_view = get_schema_view(
   openapi.Info(
      title="Snippets API",
      default_version=f'v{VER}',
      description="Simple CRM API",
      contact=openapi.Contact(email="tino.ant.s@gmail.com"),
      license=openapi.License(name="MIT License"),
   ),
   public=True,
   permission_classes=(permissions.AllowAny,),
)

urlpatterns = [
    path('admin/', admin.site.urls),
    path(f'api/v{VER}/', include('core.urls')),
    re_path(r'^swagger(?P<format>\.json|\.yaml)$', schema_view.without_ui(cache_timeout=0), name='schema-json'),
    re_path(r'^swagger/$', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
    re_path(r'^redoc/$', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'),
]


import os
import re
import json
import random
import warnings
from pathlib import Path
import pandas as pd
import numpy as np
import spacy
from spacy.util import minibatch, compounding
from spacy.matcher import PhraseMatcher

warnings.filterwarnings("ignore")

class NER:
    def __init__(
        self,
        iter=100,
        dropout=0.5,
        min_batchsize=4.0,
        max_batchsize=32.0,
        compounding_coef=0.01,
        train_text_path=None,
        train_entities_path=None,
        test_text_path=None,
    ):
        self.iter = iter
        self.dropout = dropout
        self.min_batchsize = min_batchsize
        self.max_batchsize = max_batchsize
        self.compounding_coef = compounding_coef
        self.train_text_path = train_text_path
        self.train_entities_path = train_entities_path
        self.test_text_path = test_text_path

    def get_data(self, *args, **kwargs):
        """
        Get data from the text files and transform into a pandas dataframe
        parameters: None
        returns: dataframe
        """
        set_text = set(os.listdir(self.train_text_path))
        set_ent = set(os.listdir(self.train_entities_path))
        training_set = list(set_text.intersection(set_ent))

        self.data = pd.DataFrame(columns=["filename", "text"])
        self.data["filename"] = training_set
        data_text = []
        for file in self.data["filename"]:
            rec_text = []
            pattern = r"\d+,\d+,\d+,\d+,\d+,\d+,\d+,\d+,(.+)"
            with open(os.path.join(self.train_text_path, file)) as f:
                f.seek(0)
                lines = f.readlines()
                for line in lines:
                    rec_text += re.findall(pattern, line)
            data_text.append(" ".join([x.strip() for x in rec_text]))
        self.data["text"] = data_text
        ent_list = []
        for file in self.data["filename"]:
            with open(f"{self.train_entities_path}/{file}") as f:
                entity_dict = json.load(f)
                ent_list.append(entity_dict)
        self.data["entity_dictionary"] = ent_list

        return self.data

    def transform_data(self, data, *args, **kwargs):
        """
        Transform pandas dataframe to the spaCy compliant training data format
        parameters: DataFrame
        returns: List of text and entity tuples
        """
        training_data = []
        id_ent = []
        nlp_match = spacy.load("en_core_web_sm")
        matcher = PhraseMatcher(nlp_match.vocab)
        for index, row in self.data.iterrows():
            ent_dic = row["entity_dictionary"]
            ent = []
            phrases = list(ent_dic.values())
            patterns = [nlp_match.make_doc(phrase) for phrase in phrases]
            matcher.add("EntityList", None, *patterns)

            doc = nlp_match(row["text"])
            matches = matcher(doc)
            for match_id, start, end in matches:
                try:
                    span = doc[start:end]
                    if start > 0:
                        sb = doc[0:start]
                        start_index = len(sb.text) + 1
                    else:
                        start_index = 0
                    end_index = start_index + len(span.text)
                except:
                    pass

                for key, value in ent_dic.items():
                    if value == span.text:
                        ent_tup = (start_index, end_index, key)
                        ent.append(ent_tup)
            ent_set = {"company", "date", "total", "address"}
            detected_entities = set([key for start, end, key in ent])
            missed_entities = list(ent_set - detected_entities)
            if "total" in missed_entities:
                value = ent_dic["total"]
                if len(value) > 0:
                    catch_total = re.search(value, str(row["text"]).replace(",", ""))
                    ent_tup = (catch_total.span()[0], catch_total.span()[1], "total")
                    ent.append(ent_tup)
            if "date" in missed_entities:
                value = ent_dic["date"]
                if len(value) > 0:
                    catch_date = re.search(value, str(row["text"]))
                    if catch_date == None:
                        catch_date = re.search(
                            r"\d\d[-/]*\d\d[-/]*\d\d", str(row["text"])
                        )
                    try:
                        ent_tup = (catch_total.span()[0], catch_total.span()[1], "date")
                        ent.append(ent_tup)
                    except:
                        pass
            if "company" in missed_entities:
                value = ent_dic["company"]
                catch_company = re.search(value, str(row["text"]))
                if catch_company != None:
                    ent_tup = (
                        catch_company.span()[0],
                        catch_company.span()[1],
                        "company",
                    )
                    ent.append(ent_tup)
                else:
                    catch_company = re.search(value, str(row["text"]).replace(".", ""))
                    if catch_company != None:
                        ent_tup = (
                            catch_company.span()[0],
                            catch_company.span()[1],
                            "company",
                        )
                        ent.append(ent_tup)
            if "address" in missed_entities:
                try:
                    value = ent_dic["address"]
                    catch_address = re.search(value, str(row["text"]))
                    if catch_address != None:
                        ent_tup = (
                            catch_address.span()[0],
                            catch_address.span()[1],
                            "address",
                        )
                        ent.append(ent_tup)
                except:
                    pass
            id_ent.append(len(ent))
            entity_dictionary = {"entities": ent}
            train_tup = (row["text"], entity_dictionary)
            training_data.append(train_tup)

        return training_data

    def fit(self, train_text_path, train_entities_path, *args, **kwargs):
        """
        Fit a blank English language model from spaCy and save the model in the current directory
        parameters: None
        returns: None
        """
        self.train_text_path = train_text_path
        self.train_entities_path = train_entities_path
        data = self.get_data(self)
        training_data = self.transform_data(self, data)
        TRAIN_DATA = training_data
        output_dir = os.path.join(os.getcwd(), "model")

        nlp = spacy.blank("en")
        ner = nlp.create_pipe("ner")
        nlp.add_pipe(ner, last=True)
        for _, annotations in TRAIN_DATA:
            for ent in annotations.get("entities"):
                ner.add_label(ent[2])

        dropout = self.dropout
        min_batchsize = self.min_batchsize
        max_batchsize = self.max_batchsize
        compounding_coef = self.compounding_coef
        n_iter = self.iter

        nlp.begin_training()
        for itn in range(n_iter):
            random.shuffle(TRAIN_DATA)
            losses = {}
            batches = minibatch(
                TRAIN_DATA,
                size=compounding(min_batchsize, max_batchsize, compounding_coef),
            )
            for batch in batches:
                texts, annotations = zip(*batch)
                try:
                    nlp.update(
                        texts, annotations, drop=dropout, losses=losses,
                    )
                except:
                    pass
            print(f"{itn} Losses", losses)

        output_dir = Path(output_dir)
        if not output_dir.exists():
            output_dir.mkdir()
        nlp.to_disk(output_dir)

    def get_test_data(self, *args, **kwargs):
        """
        Fetch the data from the test directory and transform it into a pandas DataFrame
        returns: DataFrame
        """
        test_text_files = os.listdir(self.test_text_path)
        test_data = pd.DataFrame(columns=["filename", "text"])
        test_data["filename"] = test_text_files
        data_text = []
        for file in test_data["filename"]:
            rec_text = []
            pattern = r"\d+,\d+,\d+,\d+,\d+,\d+,\d+,\d+,(.+)"
            try:
                with open(os.path.join(self.test_text_path, file)) as f:
                    lines = f.readlines()
                    for line in lines:
                        rec_text += re.findall(pattern, line)
            except:
                pass
            data_text.append(" ".join([x.strip() for x in rec_text]))
        test_data["text"] = data_text
        return test_data

    def predict(self, test_text_path, reciept_text="", is_dir=True, *args, **kwargs):
        """
        Identify entities in a new text string
        """
        cwd = os.getcwd()
        nlp = spacy.load(os.path.join(cwd, "model"))
        train = os.listdir(self.train_entities_path)
        ent_list = []
        for file in train:
            with open(os.path.join(self.train_entities_path, file)) as f:
                entity_dict = json.load(f)
                ent_list.append(entity_dict)
        memory_dictionary = {"company": [], "address": [], "date": [], "total": []}
        for dictionary in ent_list:
            for key, value in dictionary.items():
                memory_dictionary[key].append(value)

        if is_dir:
            self.test_text_path = test_text_path
            test_data = self.get_test_data(self)
            for index, row in test_data.iterrows():
                op_dict = {"company": "", "date": "", "address": "", "total": ""}
                doc = nlp(row["text"])
                for ent in doc.ents:
                    op_dict[ent.label_] = ent.text

                for tag, tag_memory in memory_dictionary.items():
                    for tag_value in tag_memory:
                        if tag == "total":
                            pass
                        elif (re.search(tag_value, row["text"]) != None) and (
                            op_dict[tag] == ""
                        ):
                            op_dict[tag] = tag_value

                print("Entities: ", op_dict)
                op_dir = os.path.join(cwd, "output")
                if not os.path.isdir(op_dir):
                    os.mkdir(path=op_dir)
                json_object = json.dumps(op_dict, indent=4)
                with open(os.path.join(op_dir, f"{row['filename']}"), "w") as op:
                    op.write(json_object)
        else:
            doc = nlp(reciept_text)
            op_dict = {"company": "", "date": "", "address": "", "total": ""}
            for ent in doc.ents:
                op_dict[ent.label_] = ent.text
            for tag, tag_memory in memory_dictionary.items():
                for tag_value in tag_memory:
                    if tag == "total":
                        pass
                    elif (re.search(tag_value, row["text"]) != None) and (
                        op_dict[tag] == ""
                    ):
                        op_dict[tag] = tag_value
            print("Entities: ", op_dict)


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

import tensorflow as tf
from tensorflow import keras
import tushare as ts
import numpy as np
import pandas as pd
import os
import time
import sys
import random
from absl import app
from absl import flags
from datetime import datetime
sys.path.append("..")
from common import base_common
from common import np_common
from common.const_def import *
import tushare_data
import preprocess

FLAGS = flags.FLAGS

default_show_values =  [PPI_open,
                        PPI_close,
                        PPI_close_5_avg,
                        PPI_close_10_avg,
                        PPI_close_30_avg,
                        PPI_close_100_avg,
                        PPI_vol,
                        PPI_vol_10_avg,
                        PPI_vol_100_avg]

# reload(sys)
# sys.setdefaultencoding('utf-8')
# os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

# mpl.rcParams['font.sans-serif']=['SimHei'] #指定默认字体 SimHei为黑体
# mpl.rcParams['axes.unicode_minus']=False #用来正常显示负号

def PlotCondition(pp_data, data_name, data_value, input_color):
    if len(pp_data) == 0:
        return
    filter_data = pp_data[pp_data[data_name] == data_value]
    if len(filter_data) == 0:
        return
    xs = [datetime.strptime(d, '%Y%m%d').date() for d in filter_data['trade_date'].astype(str).values]
    plt.plot(xs, filter_data['close'].values, 'o', color=input_color, label='%s_%u' % (data_name, data_value), linewidth=1)

def ShowAStock_(ts_code, show_values=[PPI_open,
                                     PPI_close,
                                     PPI_close_5_avg,
                                     PPI_close_10_avg,
                                     PPI_close_30_avg,
                                     PPI_close_100_avg,
                                     PPI_vol,
                                     PPI_vol_10_avg,
                                     PPI_vol_100_avg]):
    plt, mdate, _, _, zhfont = base_common.ImportMatPlot()
    # current_date_int = int(base_common.CurrentDate())
    current_date_int = 20200403
    o_data_source = tushare_data.DataSource(current_date_int,
                                            '',
                                            '',
                                            1,
                                            0,
                                            current_date_int)
    o_data_source.DownloadStockData(ts_code)
    o_data_source.UpdateStockPPData(ts_code)
    pp_data = o_data_source.LoadStockPPData(ts_code)
    preprocess.PPRegionCompute(pp_data, PPI_close, PPI_close_5_avg, 500, len(pp_data), np.mean)
    stock_name = o_data_source.StockName(ts_code)
    if len(pp_data) == 0:
        return
    title = "%s - %s" % (ts_code, stock_name)
    title = unicode(title, "utf-8")
    fig1 = plt.figure(dpi=70,figsize=(32,10))
    ax1 = fig1.add_subplot(1,1,1)
    ax1.xaxis.set_major_formatter(mdate.DateFormatter('%Y-%m-%d'))
    plt.title(title, fontproperties=zhfont)
    plt.xlabel('date')
    plt.ylabel('price')
    date_str_arr = np.array(["%.0f" % x for x in pp_data[:,PPI_trade_date]])
    xs = [datetime.strptime(d, '%Y%m%d').date() for d in date_str_arr]
    plt.grid(True)

    vol_list = [PPI_vol,
                PPI_vol_5_avg,
                PPI_vol_10_avg,
                PPI_vol_30_avg,
                PPI_vol_100_avg]
    print(set(show_values) - set(vol_list))
    if len(set(show_values) - set(vol_list)) > 0:
        close_max = max(pp_data[:,PPI_close])
        vol_max = max(pp_data[:,PPI_vol])
        vol_ratio = 1.0 / vol_max * close_max / 2
    else:
        vol_ratio = 1.0
    for col_index in show_values:
        name = ''
        if col_index in vol_list:
            # plt.bar(xs, pp_data[:, col_index] * vol_ratio, 0.8)
            plt.plot(xs, pp_data[:, col_index] * vol_ratio, label=PPI_name[col_index], linewidth=1)
        else:
            plt.plot(xs, pp_data[:, col_index], label=PPI_name[col_index], linewidth=1)

    plt.gcf().autofmt_xdate()
    plt.legend()
    #plt.ylim([0,5])
    plt.show()
    plt.pause(1000)

def ShowAStockLowLevel(pp_data, show_obj, show_values, vol_bar=False):
    data_len = len(pp_data)
    if data_len == 0:
        return
    if len(set(show_values) - set(PPI_vol_list)) > 0:
        close_max = max(pp_data[:,PPI_close])
        close_min = min(pp_data[:,PPI_close])
        vol_max = max(pp_data[:,PPI_vol])
        vol_min = 0.0
        vol_range = vol_max - vol_min
        vol_show_range = (close_max - close_min) / 3
        vol_show_offset = close_min - vol_show_range
        show_obj.SetBaseLine(vol_show_offset)

        if vol_range < 1.0:
            vol_range = 1.0
        vol_ratio = vol_show_range / vol_range
    else:
        vol_ratio = 1.0
    data_list = []
    name_list = []
    bar_list = []
    for col_index in show_values:
        if col_index in PPI_vol_list:
            data_list.append((pp_data[:, col_index] - vol_min) * vol_ratio + vol_show_offset)
        else:
            data_list.append(pp_data[:, col_index])
        bar_list.append(vol_bar and (col_index == PPI_vol))
        name_list.append(PPI_name[col_index])
    return show_obj.RefreshAlign(pp_data[:,PPI_trade_date], data_list, bar_list, name_list)

# def ShowAStock(pp_data, ts_code, code_name):
#     title = "%s - %s" % (ts_code, code_name)
#     show = np_common.Show(title, True)
#     ShowAStockLowLevel(pp_data, show, show_values=[PPI_open])

# if __name__ == "__main__":
#     code_list = ['000001.SZ']
#     if len(sys.argv) > 1:
#         code_list = [sys.argv[1]]
#     for ts_code in code_list:
#         # ShowAStock(ts_code, [PPI_close, PPI_close_30_avg, PPI_vol_5_avg, PPI_vol_30_avg])
#         ShowAStock(ts_code, [PPI_close, PPI_close_5_avg, PPI_close_10_avg, PPI_close_30_avg, PPI_close_100_avg, PPI_vol_5_avg])

def RandShow(pp_data, show_obj, start_day_index):
    temp_index = start_day_index
    data_len = len(pp_data)
    if data_len == 0:
        return
    show_len = FLAGS.l
    if show_len > data_len:
        show_len = data_len
    while True:
        if (temp_index + show_len) > data_len:
            temp_index = data_len - show_len
        if temp_index < 0:
            temp_index = 0
        ret = ShowAStockLowLevel(pp_data[temp_index:temp_index+show_len], show_obj, [PPI_close, PPI_close_5_avg, PPI_vol], False)
        if 'left' == ret:
            temp_index += 1
        elif 'right' == ret:
            temp_index -= 1
        elif 'n' == ret:
            break

def main(argv):
    del argv
    random.seed(123)
    show = np_common.Show(True)
    if FLAGS.r:
        data_source = tushare_data.DataSource(20000101, '', '', 1, 20000101, 20200306)
        while True:
            code_index = random.randint(0, len(data_source.code_list) - 1)
            ts_code = data_source.code_list[code_index]
            print(ts_code)
            code_name = data_source.name_list[code_index]
            pp_data = data_source.LoadStockPPData(ts_code, True)
            if len(pp_data) == 0:
                return
            day_index = random.randint(0, len(pp_data) - 1)
            show.SetTitle('%s - %s' % (ts_code, code_name))
            RandShow(pp_data, show, day_index)

    else:
        data_source = tushare_data.DataSource(0, '', '', 1, 20000101, 20200603)
        ts_code = FLAGS.c
        code_name = data_source.StockName(ts_code)
        data_source.DownloadStockData(ts_code)
        data_source.UpdateStockPPData(ts_code)
        pp_data = data_source.LoadStockPPData(ts_code, True)
        show.SetTitle('%s - %s' % (ts_code, code_name))
        ShowAStockLowLevel(pp_data, show, [PPI_open, PPI_vol])

    # ShowAStock_(FLAGS.c)

if __name__ == "__main__":
    flags.DEFINE_boolean('r', False, 'rand show')
    flags.DEFINE_string('c', '000001.SZ', 'ts code')
    flags.DEFINE_integer('l', 100, 'rand test show data len')
    app.run(main)


# 음계 : https://www.acmicpc.net/problem/2920
# 문제 설명 : 1~8사이의 자연수 값을 랜덤으로 한 번씩 입력받는다.
# 입력된 값이 차례로 입력된 경우, ascending, 반대인 경우, descending, 이외는 mixed
# 68ms

import sys

if __name__ == '__main__' :
    input = sys.stdin.readline().rstrip()

    if(input == "1 2 3 4 5 6 7 8"):
        print("ascending")
    elif(input == "8 7 6 5 4 3 2 1"):
        print("descending")
    else:
        print("mixed")