import csv
import csv
from os.path import exists
import networkx as nx
from functools import partial

def find_nearest(points, coord):
    dist = lambda s, key: (s[0] - points[key][0]) ** 2 + \
                           (s[1] - points[key][1]) ** 2
    return min(points, key=partial(dist, coord))

class UserDistance:
    def __init__(self):
        self.network = nx.Graph()
        self.__nodes = {}
        self.__loc = {}
        self.__keywords = {}
        self.relRemoved = 0

        #
        # INSTRUCTIONS:
        # Update below path names then run
        #
        self.loadRoadEdges(path="./Datasets/RoadNetworks/california_edge.csv")
        self.loadRoadNodes(path="./Datasets/RoadNetworks/california_node.csv")

        self.loadSocialLoc(path="./Datasets/SocialNetworks/gowalla_loc.csv")
        self.loadSocialKeywords(path="./Datasets/SocialNetworks/gowalla_keywords.csv")
        self.loadSocialRel(path="./Datasets/SocialNetworks/gowalla_rel.csv", name="./Datasets/SocialNetworks/gowalla_n_rel.csv")
        print(str(self.relRemoved) + ' relationships were removed as user(s) could not be found in location dataset')
        self.relRemoved = 0
        print('GOWALLA DONE')
        self.loadSocialLoc(path="./Datasets/SocialNetworks/foursquare_loc.csv")
        self.loadSocialKeywords(path="./Datasets/SocialNetworks/foursquare_keywords.csv")
        self.loadSocialRel(path="./Datasets/SocialNetworks/foursquare_rel.csv", name="./Datasets/SocialNetworks/foursquare_n_rel.csv")
        print(str(self.relRemoved) + ' relationships were removed as user(s) could not be found in location dataset')
        self.relRemoved = 0
        print('FOURSQUARE DONE')

    # Reads edge file from path.
    # dict = {
    #    "edge_id": [start_id, end_id, weight],
    # }
    # noinspection PyShadowingBuiltins
    def loadRoadEdges(self, path=None):

        if path is not None and exists(path):
            dict = {}
            with open(path, 'r') as csvfile:
                reader = csv.reader(csvfile, delimiter=',', quotechar='|')
                next(reader)
                for row in reader:
                    edge_id = row[0]
                    start_id = row[1]
                    end_id = row[2]
                    weight = row[3]
                    if edge_id in dict:
                        raise Exception(f"Error: Duplicate value in {path}")
                    else:
                        self.network.add_edge(float(start_id), float(end_id), weight=float(weight))

    # Reads node file from path. This is super awful but it's the fastest way to do things. This is what it returns:
    # dict = {
    #    "node_id":
    #    [
    #       [lat, lon],
    #       [lat, lon]
    #    ]
    # }
    # noinspection PyShadowingBuiltins
    def loadRoadNodes(self, path=None):
        if path is not None and exists(path):
            dict = {}
            # noinspection SpellCheckingInspection
            with open(path, 'r') as csvfile:
                reader = csv.reader(csvfile, delimiter=',', quotechar='|')
                next(reader)
                for row in reader:
                    node_id = row[0]
                    lat = row[1]
                    lon = row[2]
                    if node_id in dict:
                        raise Exception(f"Error: Duplicate value in {path}")
                    else:
                        self.network.add_node(float(node_id))
                        dict[node_id] = (float(lat), float(lon))
            self.__nodes = dict
        else:
            self.__nodes = None

    # Reads rel file from path. This is super awful but it's the fastest way to do things. This is what it returns:
    # dict = {
    #    "user_id":
    #    [
    #       [rel_user_id, weight],
    #       [rel_user_id, weight]
    #    ]
    # }
    # noinspection PyShadowingBuiltins
    def loadSocialRel(self, path=None, name='out.csv'):
        if 1 == 1:
            dict = {}
            with open(path, 'r') as readObj, \
                    open(name, 'w', newline='') as writeObj:
                reader = csv.reader(readObj, delimiter=',', quotechar='|')
                writer = csv.writer(writeObj)

                # Add header to output file
                writer.writerow(['user_id','rel_user_id','weight','distance','keywords'])
                next(reader)
                for row in reader:
                    user_id = row[0]
                    rel_user_id = row[1]
                    weight = row[2]

                    # Ensure users are in loc datasets
                    if(self.__loc.get(user_id, 0) == 0):
                        self.relRemoved = self.relRemoved + 1
                        continue
                    if(self.__loc.get(rel_user_id, 0) == 0):
                        self.relRemoved = self.relRemoved + 1
                        continue

                    usrA = find_nearest(self.__nodes, (float(self.__loc[user_id][0][0]), float(self.__loc[user_id][0][1])))
                    usrB = find_nearest(self.__nodes, (float(self.__loc[rel_user_id][0][0]), float(self.__loc[rel_user_id][0][1])))
                    commonKeywords= len(set(self.__keywords[user_id]) & set(self.__keywords[rel_user_id]))
                    row.append(nx.dijkstra_path_length(self.network, source=float(usrA), target=float(usrB)))
                    row.append(commonKeywords)
                    writer.writerow(row)

    # Reads loc file from path.
    # dict = {
    #    "user_id":
    #    [
    #       [lat_pos, lon_pos],
    #       [lat_pos, lon_pos]
    #    ]
    # }
    # noinspection SpellCheckingInspection,PyShadowingBuiltins
    def loadSocialLoc(self, path=None):
        if 1 == 1:
            dict = {}
            with open(path, 'r') as csvfile:
                reader = csv.reader(csvfile, delimiter=',', quotechar='|')
                next(reader)
                for row in reader:
                    user_id = row[0]
                    lat_pos = row[1]
                    lon_pos = row[2]
                    if user_id in dict:
                        dict[user_id] = dict[user_id] + [lat_pos, lon_pos]
                    else:
                        dict[user_id] = [[lat_pos, lon_pos]]
            self.__loc = dict
        else:
            self.__loc = None

    def loadSocialKeywords(self, path=None):
        if 1 == 1:
            dict = {}
            with open(path, 'r') as csvfile:
                reader = csv.reader(csvfile, delimiter=',', quotechar='|')
                next(reader)
                for row in reader:
                    user_id = row[0]
                    keyword = row[1]
                    if user_id in dict:
                        dict[user_id] = dict[user_id] + [keyword]
                    else:
                        dict[user_id] = [keyword]
            self.__keywords = dict
        else:
            self.__keywords = None

def main():
    test = UserDistance()

if __name__ == '__main__':
    main()


import pymongo
from pymongo import errors

from Domain.Team import Team
from Domain.TeamBudget import TeamBudget

class MongoTeamDB:

    HOST = "mongodb://localhost"
    PORT = 27017
    DB = "SoccerLeagueManagerDB"
    TEAM_COLLECTION = "Teams"

    def __init__(self):
        """ Constructor for MongoTeamDB """

        try:
            client = pymongo.MongoClient(self.HOST, self.PORT)
            self.__db = client[self.DB]
            self.__teams_collection = self.__db[self.TEAM_COLLECTION]

        except errors.ConnectionFailure as err:
            raise ConnectionError("Connection Lost\n" + str(err))

    def add(self, team):

        if self.is_name_taken(team.name):
            raise ValueError("Could not add team, {0} already taken".format(team.name))

        team_budget_dict = team.budget_manager.__dict__

        team_dict = team.__dict__

        team_dict.pop('_Team__budget_manager')

        team_dict['_Team__budget_manager'] = team_budget_dict

        self.__teams_collection.insert_one(team_dict)

    def delete(self, team_name):

        if not self.is_name_taken(team_name):
            raise ValueError("Team Doesnt exist")

        query = {
            '_Team__name': team_name
        }

        self.__teams_collection.delete_one(query)

    def get(self, team_name):

        if not self.is_name_taken(team_name):
            raise ValueError("Could not get team {0}, team does not exist".format(team_name))

        query = {
            '_Team__name': team_name
        }

        return self.team_dict_to_object(self.__teams_collection.find_one(query))

    def get_all(self):

        teams = self.__teams_collection.find()

        res = []

        for team in teams:
            res.append(self.team_dict_to_object(team))

        return res

    def update(self, team):

        old_team = self.get(team.name)

        self.delete(team.name)

        try:
            self.add(team)
        except Exception as err:
            self.add(old_team)
            raise err

    def is_name_taken(self, team_name):

        query = {
            '_Team__name': team_name
        }

        if self.__teams_collection.find_one(query) is None:
            return False
        return True

    def team_dict_to_object(self, team_dict):
        team_budget_dict = team_dict['_Team__budget_manager']
        team_budget = TeamBudget(team_budget_dict['_TeamBudget__transactions'],
                                 team_budget_dict['_TeamBudget__income_transactions'],
                                 team_budget_dict['_TeamBudget__expanses_transactions'],
                                 team_budget_dict['_TeamBudget__current_balance'])
        return Team(team_dict['_Team__name'], team_dict['_Team__team_members'], team_dict['_Team__stadium'],
                    team_dict['_Team__upcoming_games'], team_dict['_Team__past_games'],
                    team_dict['_Team__leagues'], team_dict['_Team__owners'],
                    team_dict['_Team__managers'], team_dict['_Team__is_open'], team_budget)

    def reset_db(self):

        self.__teams_collection.drop()


import codecs
import encodings
import io
from functools import lru_cache
from typing import List, Optional, Tuple

# 3rd party
import emoji
import tokenize_rt  # type: ignore

__all__ = ["decode", "register"]

__author__: str = "Dominic Davis-Foster"
__copyright__: str = "2021 Dominic Davis-Foster"
__license__: str = "MIT License"
__version__: str = "0.1.0"
__email__: str = "dominic@davis-foster.co.uk"

tokenize_rt._string_prefixes = frozenset("bfrug")

utf_8 = encodings.search_function("utf8")
assert utf_8 is not None

class IncrementalDecoder(codecs.BufferedIncrementalDecoder):

	def _buffer_decode(
			self,
			input,  # noqa: A002  # pylint: disable=redefined-builtin
			errors,
			final,
			):  # pragma: no cover

		if final:
			return decode(input, errors)
		else:
			return '', 0

class StreamReader(utf_8.streamreader):  # type: ignore
	# decode is deferred to support better error messages
	_stream = None
	_decoded = False

	@property
	def stream(self):
		if not self._decoded:
			text, _ = decode(self._stream.read())  # type: ignore
			self._stream = io.BytesIO(text.encode("UTF-8"))
			self._decoded = True
		return self._stream

	@stream.setter
	def stream(self, stream):
		self._stream = stream
		self._decoded = False

def _is_g(token):
	prefix, _ = tokenize_rt.parse_string_literal(token.src)
	return 'g' in prefix.lower()

def _make_gstring(tokens):
	new_tokens = []

	for i, token in enumerate(tokens):
		if token.name == "STRING" and _is_g(token):
			prefix, s = tokenize_rt.parse_string_literal(token.src)

			for q in ('"' * 3, "'" * 3, '"', "'"):
				if s.startswith(q):
					s = s[len(q):len(s) - len(q)]
					break
			else:
				raise AssertionError("unreachable")

			parts = [q, s, q]

			if 'r' in prefix.lower():
				parts = [s.replace('\\', "\\\\") for s in parts]

			parts = [emoji.emojize(s) for s in parts]
			token = token._replace(src=''.join(parts))

		new_tokens.append(token)

	return new_tokens

def decode(
		input: bytes,  # noqa: A002  # pylint: disable=redefined-builtin
		errors: str = "strict",
		) -> Tuple[str, int]:
	"""
	Decode the given source as UTF-8 and convert emoji.

	:param input:
	:param errors:
	"""

	u, length = utf_8.decode(input, errors)  # type: ignore[union-attr]
	tokens = tokenize_rt.src_to_tokens(u)

	to_replace: List[Tuple[int, Optional[int]]] = []
	start: Optional[int] = None
	end: Optional[int] = None
	seen_f: Optional[bool] = None

	for i, token in enumerate(tokens):
		if start is None:
			if token.name == "STRING":
				start, end = i, i + 1
				seen_f = _is_g(token)
		elif token.name == "STRING":
			end = i + 1
			seen_f |= _is_g(token)
		elif token.name not in tokenize_rt.NON_CODING_TOKENS:
			if seen_f:
				to_replace.append((start, end))
			start = end = seen_f = None

	for start, end in reversed(to_replace):
		tokens[start:end] = _make_gstring(tokens[start:end])

	return tokenize_rt.tokens_to_src(tokens), length

# codec api

codec_map = {
		name: codecs.CodecInfo(
				name=name,
				encode=utf_8.encode,
				decode=decode,
				incrementalencoder=utf_8.incrementalencoder,
				incrementaldecoder=IncrementalDecoder,
				streamreader=StreamReader,
				streamwriter=utf_8.streamwriter,
				)
		for name in ("emoji-strings", "emoji_strings", "gstrings")
		}

@lru_cache(1)
def register():  # pragma: no cover  # noqa: D103
	codecs.register(codec_map.get)

register()


import logging
import json
import os
import pyodbc
import azure.functions as func

QUERY = 'SELECT TOP 1 author, quote FROM quotes ORDER BY newid()'

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')
    # connection_string = 'DRIVER={ODBC Driver 17 for SQL Server};SERVER=<server>.database.windows.net;PORT=1433;DATABASE=<database>;UID=<user>;PWD=<password>'
    connection_string = os.environ['CONNECTION_STRING']

    try:
        conn = pyodbc.connect(connection_string, timeout=3)

        cur = conn.cursor()
        author, quote = cur.execute(QUERY).fetchone()

        cur.close()
        conn.close()
    except pyodbc.OperationalError:
        author, quote = '503 Service Unavailable', 'Včera jsme měli párty a pili jsme těžko...'

    resp = {"author": author, "quote": quote}

    return func.HttpResponse(
        json.dumps(resp),
        headers={"Content-type": "application/json"},
        status_code=200
    )


import os
import sys
from sqlalchemy import Column, ForeignKey, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from sqlalchemy import create_engine
from eralchemy2 import render_er

Base = declarative_base()

class Person(Base):
    __tablename__ = 'person'
    id = Column(Integer, primary_key=True)
    username = Column(String(50), unique=True, nullable=False)
    email = Column(String(100))
    favorite_characters = relationship('People', secondary="favorite_characters")
    favorite_planets = relationship('Planet', secondary="favorite_planets")
    favorite_vehicles = relationship('Vehicle', secondary="favorite_vehicles")

class Favorite_characters(Base):
    __tablename__ = 'favorite_characters'
    character_id = Column(Integer, ForeignKey('people.id'), primary_key=True)
    person_id = Column(Integer, ForeignKey('person.id'), primary_key=True)

class Favorite_planets(Base):
    __tablename__ = 'favorite_planets'
    planet_id = Column(Integer, ForeignKey('planets.id'), primary_key=True)
    person_id = Column(Integer, ForeignKey('person.id'), primary_key=True)

class favorite_vehicles(Base):
    __tablename__ = 'favorite_vehicles'
    vechile_id = Column(Integer, ForeignKey('vehicles.id'), primary_key=True)
    person_id = Column(Integer, ForeignKey('person.id'), primary_key=True)

class People(Base):
    __tablename__ = 'people'
    id = Column(Integer, primary_key=True)
    name = Column(String(200))
    height = Column(Integer)
    mass = Column(Integer)
    hair_color = Column(String(100))
    skin_color = Column(String(100))
    eye_color = Column(String(100))
    birth_year = Column(String(100))
    gender = Column(String(10))

class Planet(Base):
    __tablename__ = 'planets'
    id = Column(Integer, primary_key=True)
    name = Column(String(200))
    rotation_period = Column(Integer)
    orbital_period = Column(Integer)
    diameter = Column(Integer)
    climate = Column(String(200))
    gravity = Column(String(200))
    terrain = Column(String(200))
    surface_water = Column(String(200))
    population = Column(Integer)

class Vehicle(Base):
    __tablename__ = 'vehicles'
    id = Column(Integer, primary_key=True)
    name = Column(String(200))
    model = Column(String(200))
    manufacturer = Column(String(200))
    cost_in_credits = Column(Integer)
    length = Column(Integer)
    crew = Column(Integer)
    passengers = Column(Integer)
    cargo_capacity = Column(Integer)
    consumables = Column(String(100))
    vehicle_class = Column(String(100))

## Draw from SQLAlchemy base
render_er(Base, 'diagram.png')


import pandas as pd
import logging
import numpy as np
import openpyxl
import os

from pathlib import Path

import tkinter as tk

from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

from . import PEAKS_IMG_PATH

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

## IMPORT DF FROM EXCEL ##

def init_core_folders(project_path):
    core_folders = ["Log", "Output"]
    for folder in core_folders:
        if not os.path.exists(os.path.join(project_path, folder)):
            os.makedirs(os.path.join(project_path, folder))

def read_clean_excel(excel_path, sheet_name=None):

    if sheet_name is None:
        # Get all sheet names of excel_path
        wb = openpyxl.load_workbook(excel_path)
        sheet_names = wb.sheetnames

        i = 0
        while True:
            sheet_name = sheet_names[i]
            if "summary" not in sheet_name.lower():
                break
            i += 1

    # load into df_whole, header = false
    df_whole = pd.read_excel(excel_path, sheet_name=sheet_name, header=None)

    # if first row contain a cell with "scorer", remove it
    for cell in df_whole.iloc[0]:
        if "scorer" in str(cell).lower():
            df_whole = df_whole.drop(0)
            break

    # reset index
    df_whole = df_whole.reset_index(drop=True)

    # Combine the content of row 1 and row 2 into one row, f"{content of row 1} {content of row 2}", if content of row 2 is NaN, then just use content of row 1
    df_whole.iloc[0] = df_whole.iloc[0].fillna("")

    # iterate through cell in row 0
    for i, cell in enumerate(df_whole.iloc[0]):
        cell_below = df_whole.iloc[1][i]
        if cell_below is np.nan:
            df_whole.iloc[0][i] = cell
        else:
            df_whole.iloc[0][i] = f"{cell}_{cell_below}"

    # drop row 1
    df_whole = df_whole.drop(1)

    # make row 0 as header
    df_whole.columns = df_whole.iloc[0]

    # drop row 0
    df_whole = df_whole.drop(0)

    # reset index
    df_whole = df_whole.reset_index(drop=True)

    return df_whole

## MAKE DF FOR SAVING ##

def get_coordinates(values, positions):
    size = len(positions)
    cc = [values[positions[i]] for i in range(size)]
    return cc

def make_df(xvalues, yvalues, maxima, minima):

    xMaxima = get_coordinates(xvalues, maxima)
    yMaxima = get_coordinates(yvalues, maxima)
    xMinima = get_coordinates(xvalues, minima)
    yMinima = get_coordinates(yvalues, minima)

    maxima_result =  {xMaxima[i]: yMaxima[i] for i in range(len(xMaxima))}
    maxima_result = dict(sorted(maxima_result.items()))

    minima_result =  {xMinima[i]: yMinima[i] for i in range(len(xMinima))}
    minima_result = dict(sorted(minima_result.items()))

    df_max = pd.DataFrame(list(maxima_result.items()),columns = ['X_maxima', 'Y_maxima'])
    df_max['X_maxima'] = df_max['X_maxima'].astype(int)
    df_max['Y_maxima'] = df_max['Y_maxima'].astype(float)

    df_min = pd.DataFrame(list(minima_result.items()),columns = ['X_minima', 'Y_minima'])
    df_min['X_minima'] = df_min['X_minima'].astype(int)
    df_min['Y_minima'] = df_min['Y_minima'].astype(float)

    return df_max, df_min

def append_df_to_excel(filepath, df, sheet_name='Sheet1', startcol=None, startrow=None, col_sep = 0, row_sep = 0,
                       truncate_sheet=False, DISPLAY = False,
                       **to_excel_kwargs):
    # Excel file doesn't exist - saving and exiting
    if not os.path.isfile(filepath):
        logger.info("Excel file doesn't exist - directly export using df.to_excel")
        df.to_excel(
            filepath,
            sheet_name=sheet_name,
            startcol=startcol if startcol is not None else 0,
            startrow=startrow if startrow is not None else 0,
            **to_excel_kwargs)

        wb = openpyxl.load_workbook(filepath)
        ws = wb[sheet_name]
        row_0 = ws[1]
        logger.debug(f"Header: {row_0}")

        return

    logger.info("Excel file exists - appending using openpyxl")

    if 'engine' in to_excel_kwargs:
        to_excel_kwargs.pop('engine')

    writer = pd.ExcelWriter(filepath, engine='openpyxl', mode='a', if_sheet_exists='overlay')

    # try to open an existing workbook
    writer.workbook = openpyxl.load_workbook(filepath)

    # get the last col in the existing Excel sheet
    # if it was not specified explicitly
    if startcol is None and sheet_name in writer.workbook.sheetnames:
        startcol = writer.workbook[sheet_name].max_column + col_sep

    if startrow is None and sheet_name in writer.workbook.sheetnames:
        startrow = writer.workbook[sheet_name].max_row + row_sep

    if startcol is None:
        startcol = 0

    if startrow is None:
        startrow = 0

    logger.debug(f'In file: {os.path.basename(filepath)}')
    logger.debug(f'Appended to column: {startcol}, row: {startrow}')

    try:
        row_0 = writer.workbook[sheet_name][1]
        logger.debug(f"Header: {row_0}")
    except:
        logger.debug(f"Sheet {sheet_name} doesn't exist")

    # remove df headers if they exist
    if startrow != 0:
        # take the first row
        first_row = df.iloc[0].astype(str)
        # check if any cell in the first row contains a letter
        has_letter = first_row.str.contains('[a-zA-Z]').any()
        if has_letter:
            df = df.iloc[1:, :]

    # write the dataframe to the existing sheet
    df.to_excel(writer, sheet_name, startcol=startcol, startrow=startrow, **to_excel_kwargs)

    # close workbook
    writer.close()

## DRAW PLOT ##

def draw_plot(xvalues, yvalues, maxima, minima):

    xMaxima = get_coordinates(xvalues, maxima)
    yMaxima = get_coordinates(yvalues, maxima)
    xMinima = get_coordinates(xvalues, minima)
    yMinima = get_coordinates(yvalues, minima)

    # Create figure and subplot
    fig = Figure(figsize=(20, 10))
    plot1 = fig.add_subplot(111)

    # Plot main data
    plot1.plot(xvalues, yvalues, label='Data')

    # Plot maxima points
    plot1.plot(xMaxima, yMaxima, 'ro', label='Maxima')

    # Plot minima points
    plot1.plot(xMinima, yMinima, 'bo', label='Minima')

    # Add a legend
    plot1.legend()

    return fig

def get_core(given_file):

    status = "raw"

    if "_filtered" in given_file.stem:
        status = "filtered"

    if "DLC" in given_file.stem:
        core = given_file.stem.split("DLC")[0]
        core = " ".join(core.split(" ")[-2:])
    else:
        core = given_file.stem
        status = "filtered"

    return core, status

def key_value_swap(given_dict):
    # check if all values are unique
    if not len(given_dict.values()) == len(set(given_dict.values())):
        raise ValueError("Values are not unique, can't swap keys and values")

    return {v: k for k, v in given_dict.items()}

def draw_peaks(master, given_name, given_values, mode="display"):

    assert mode in ["display", "save"]

    xvalues, yvalues, maxima, minima = given_values

    figure = draw_plot(xvalues, yvalues, maxima, minima)

    def save_pictures(figure):
        output_path = os.path.join(PEAKS_IMG_PATH, f"{given_name}.png")
        Path(output_path).parent.mkdir(parents=True, exist_ok=True)
        if os.path.exists(output_path):
            logger.info(f"{output_path} already exists, skip saving")
            return
        figure.savefig(output_path, dpi=300)
        logger.info(f"Saved peaks plot to {output_path}")

    if mode=="display":
        # Make a top level window
        TOP = tk.Toplevel(master)
        TOP.title("Peaks")
        TOP.geometry("800x400")

        # Make a canvas
        CANVAS = FigureCanvasTkAgg(figure, master=TOP)
        CANVAS.draw()
        CANVAS.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)

        # make a button on top right corner to save the figure
        BTN_save_figure = tk.Button(TOP, text="Save", command=lambda: save_pictures(figure))
        BTN_save_figure.pack(side=tk.RIGHT)

    if mode=="save":
        save_pictures(figure)

def excel_polisher(input_excel_path):

    pass


import os,random,time,hashlib
from django.http import JsonResponse
from .. import models
from django.shortcuts import render,HttpResponse
from django.urls import reverse
from django.core.paginator import Paginator
from django.contrib.auth.hashers import make_password, check_password

#列表显示
def index(request):
    # 查询所有会员数据
    data = models.Users.objects.filter()

    #接收查询条件
    select = request.GET.get('select',None)
    select_text=request.GET.get('select_text',None)
    # 判断是否有查询条件
    if select:
        if select=='select_all':
            data=data.all()
        elif select == 'select_nikename':
            data=data.filter(nikename__icontains=select_text)
        elif select == 'select_sex':
            if select_text=='女':
                data = data.filter(sex__icontains=0)
            elif select_text=='男':
                data = data.filter(sex__icontains=1)
        elif select == 'select_phone':
            data=data.filter(phone__icontains=select_text)
        elif select == 'select_address':
            data=data.filter(homeaddress__icontains=select_text)
        elif select == 'select_usertype':
            data=data.filter(usertype__icontains=select_text)

    #实例化分页对象(分页的集合,每页显示多少条数据)
    p=Paginator(data,5)
    #接收当前页码数 1为默认值
    index_p=request.GET.get('page',1)
    #显示当前页的数据
    newdata=p.page(index_p)
    #将当前页装入字典返回到网页 将查询条件也存入newdata
    contex={'data':newdata,'select':select,'select_text':select_text}
    return render(request,'myadmin/meber/meberindex.html',contex)

#会员注册
def meberadd(request):
    if request.method=='GET':
        return render(request, 'myadmin/meber/meberAdd.html')
    else:
        try:
            # 接收表单数据
            data = request.POST.dict()
            #删除token
            data.pop('csrfmiddlewaretoken')
            #密码加密
            # data['password']=password_md5(data['password'])
            data['password'] = make_password(data['password'])
            #接收图片
            file=request.FILES.get('face',None)
            if file:
                # 将图片路径保存
                filename = img_upload(file)
                # 存进字典
                data['face'] = filename
            else:
                # #没有上传头像则设置默认头像
                # data['face']='/static/myadmin/images/face/default_face.jpg'
                data.pop('face')
            #验证手机号码是否存在
            # 验证
            obj = models.Users.objects.filter(phone=data['phone']).count()
            # 判断
            if obj:
                url = reverse('meber_add')
                return HttpResponse(f'<script>alert("手机号码已注册!");location.href="' + url + '" </script>')
            # 存进数据库
            obj = models.Users(**data)
            # 数据库提交
            obj.save()
            url=reverse('meber_index')
            return HttpResponse(f'<script>alert("注册成功");location.href="' + url + '" </script>')
        except:
            url=reverse('meber_add')
            return HttpResponse(f'<script>alert("注册失败");location.href="' + url + '" </script>')

#会员修改
def meberupdate(request):
    if request.method=='GET':
        id=request.GET.get('id')
        obj=models.Users.objects.get(id=id)
        conte={'obj':obj}
        return render(request,'myadmin/meber/meberUpdate.html',conte)
    elif request.method=="POST":
        # 接收表单数据
        data = request.POST.dict()
        # 删除token
        data.pop('csrfmiddlewaretoken')
        # 密码加密
        data['password'] = make_password(data['password'])
        # 接收图片
        file = request.FILES.get('face', None)
        if file:
            # 将图片路径保存
            filename = img_upload(file)
            # 存进字典
            data['face'] = filename
        else:
            data.pop('face')
        #数据库进行更新
        try:
            obj=models.Users.objects.filter(id=data['id'])
            obj.update(**data)
            url = reverse('meber_index')
            return HttpResponse(f'<script>alert("修改成功");location.href="' + url + '" </script>')
        except:
            url =reverse('meber_update')
            urlname=f"{url}+?id={{ i.id }}"
            return HttpResponse(f'<script>alert("修改失败");location.href="' + urlname + '" </script>')

#会员删除
def meberdelete(request):
    try:
        id=request.GET.get('id')
        obj=models.Users.objects.get(id=id)
        try:
            if obj.face!='/static/myadmin/images/face/default_face.jpg':
                os.remove('.'+obj.face)
        except:
            return JsonResponse({'code': 2, 'msg': '头像删除失败'})
        obj.delete()
        return JsonResponse({'code': 0, 'msg': '删除成功'})
    except:
        return JsonResponse({'code': 1, 'msg': '删除失败'})
#封装MD5加密
def password_md5(msg):
    hl = hashlib.md5()
    hl.update(msg.encode(encoding='utf-8'))
    return hl.hexdigest()
#封装图片上传到服务器
def img_upload(file):

    if file:  # 假如file有数据的话则
        # 处理上传文件
        zh = file.name.split('.').pop()  # 扩展名 获取
        name = str(random.randint(10000, 99999) + time.time()) + '.' + zh
        try:
            with open(f'./static/myadmin/images/face/{name}', 'wb+') as fp:  # 打开或者创建一个图片 再用write把网页获取到的图片存放到创建的图片中
                # fp.write(file)  # 存放到创建的图片中
                #分块写入
                for chunk in file.chunks():
                    fp.write(chunk)
                filename = f'/static/myadmin/images/face/{name}'
                return filename
        except:
            return False


import pytest

@pytest.fixture()
def client(test_directory):
    import os
    from parrot_api.core.server import create_server
    app = create_server(spec_dir=os.path.join(test_directory, 'mocks/example_app/schemas'))
    return app.app.test_client()

def test_package_apis(test_directory):
    from parrot_api.core.server import create_server
    import os
    import shutil
    new_package_dir = os.path.join(test_directory, '../parrot_api/example_package')
    if os.path.isdir(new_package_dir):
        shutil.rmtree(new_package_dir)
    shutil.copytree(os.path.join(test_directory, 'mocks/example_package/'), new_package_dir)
    client = create_server(
        spec_dir=os.path.join(test_directory, os.path.join(test_directory, 'mocks/example_app/schemas')),

    ).test_client()
    js = dict(head=1)
    assert client.post('/package/v1/echo', json=js).json() == js
    shutil.rmtree(new_package_dir)

@pytest.fixture()
def client(test_directory):
    import os
    from parrot_api.core.server import create_server
    app = create_server(spec_dir=os.path.join(test_directory, 'mocks/async_app/schemas'))
    return app.test_client()

async def test_async_template_home(client):
    resp = client.get('/status/v1/healthcheck')
    assert resp.status_code == 200

async def test_async_template_api_ui(client):
    resp = client.get('v1/ui/')
    assert resp.status_code == 200

async def test_async_hello(client):
    resp = client.get('/v1/hello', headers=dict(Authorization='Bearer test'))
    assert resp.status_code == 200


from __future__ import absolute_import, division, print_function

import unittest

from datashape import coercion_cost, dshape, dshapes, error
from datashape.tests import common
from datashape.py2help import xfail

class TestCoercion(common.BTestCase):

    def test_coerce_ctype(self):
        a, b, c = dshapes('float32', 'float32', 'float64')
        self.assertLess(coercion_cost(a, b), coercion_cost(a, c))
        a, b, c = dshapes('uint64', 'uint64', 'int64')
        self.assertLess(coercion_cost(a, b), coercion_cost(a, c))
        a, b, c = dshapes('int64', 'int64', 'uint64')
        self.assertLess(coercion_cost(a, b), coercion_cost(a, c))
        a, b, c = dshapes('float64', 'float64', 'complex[float32]')
        self.assertLess(coercion_cost(a, b), coercion_cost(a, c))
        a, b, c = dshapes('int16', 'float64', 'complex[float32]')
        self.assertLess(coercion_cost(a, b), coercion_cost(a, c))
        a, b, c = dshapes('int8', 'float64', 'complex[float32]')
        self.assertLess(coercion_cost(a, b), coercion_cost(a, c))

    def test_coerce_ctype_float_vs_complex(self):
        # int -> float32 is preferred over int -> complex[float32]
        a, b, c = dshapes('int32', 'float32', 'complex[float32]')
        self.assertLess(coercion_cost(a, b), coercion_cost(a, c))
        # int -> float64 is preferred over int -> complex[float64]
        a, b, c = dshapes('int32', 'float64', 'complex[float64]')
        self.assertLess(coercion_cost(a, b), coercion_cost(a, c))
        # int -> float64 is preferred over int -> complex[float32]
        a, b, c = dshapes('int32', 'float64', 'complex[float32]')
        self.assertLess(coercion_cost(a, b), coercion_cost(a, c))

    def test_coerce_numeric(self):
        a, b = dshapes('float32', 'float64')
        self.assertGreater(coercion_cost(a, b), 0)

    def test_coercion_transitivity(self):
        a, b, c = dshapes('int8', 'complex128', 'float64')
        self.assertGreater(coercion_cost(a, b), coercion_cost(a, c))

    @xfail(reason='This is something that needs to be handled by overloading')
    def test_coerce_typevars(self):
        a, b, c = dshapes('10 * 11 * float32', 'X * Y * float64',
                          '10 * Y * float64')
        self.assertGreater(coercion_cost(a, b), coercion_cost(a, c))

    @xfail(reason='This is something that needs to be handled by overloading')
    def test_coerce_constrained_typevars(self):
        a, b, c = dshapes('10 * 10 * float32', 'X * Y * float64',
                          'X * X * float64')
        self.assertGreater(coercion_cost(a, b), coercion_cost(a, c))

    def test_coerce_broadcasting(self):
        a, b, c = dshapes('10 * 10 * float32', '10 * Y * Z * float64',
                          'X * Y * float64')
        self.assertGreater(coercion_cost(a, b), coercion_cost(a, c))

    def test_coerce_broadcasting2(self):
        a, b, c = dshapes('10 * 10 * float32', '1 * 10 * 10 * float32',
                          '10 * 10 * float32')
        self.assertGreater(coercion_cost(a, b), coercion_cost(a, c))

    def test_coerce_broadcasting3(self):
        a, b, c = dshapes('10 * 10 * float32', '10 * 10 * 10 * float32',
                          '1 * 10 * 10 * float32')
        self.assertGreater(coercion_cost(a, b), coercion_cost(a, c))

    @xfail(reason='implements has not been implemented in the new parser')
    def test_coerce_traits(self):
        a, b, c = dshapes('10 * 10 * float32', '10 * X * A : floating',
                          '10 * X * float32')
        self.assertGreater(coercion_cost(a, b), coercion_cost(a, c))

    def test_coerce_dst_ellipsis(self):
        a, b, c = dshapes('10 * 10 * float32', 'X * ... * float64',
                          'X * Y * float64')
        self.assertGreater(coercion_cost(a, b), coercion_cost(a, c))

    @xfail(reason='not dealing with ellipsis in the src of a coercion')
    def test_coerce_src_ellipsis(self):
        a, b, c = dshapes('10 * ... * float32', 'X * Y * float64',
                          'X * ... * float64')
        self.assertGreater(coercion_cost(a, b), coercion_cost(a, c))

    def test_allow_anything_to_bool(self):
        # The cost should be large
        min_cost = coercion_cost(dshape('int8'), dshape('complex[float64]'))
        for ds in ['int8', 'int16', 'int32', 'int64', 'uint8', 'uint16',
                   'uint32', 'uint64', 'float32', 'float64',
                   'complex[float32]', 'complex[float64]']:
            self.assertGreater(coercion_cost(dshape(ds), dshape('bool')),
                               min_cost)

class TestCoercionErrors(unittest.TestCase):

    def test_downcast(self):
        a, b = dshapes('float32', 'int32')
        self.assertRaises(error.CoercionError, coercion_cost, a, b)

    def test_disallow_bool_to_anything(self):
        for ds in ['int8', 'int16', 'int32', 'int64', 'uint8', 'uint16',
                   'uint32', 'uint64', 'float32', 'float64',
                   'complex[float32]', 'complex[float64]']:
            self.assertRaises(error.CoercionError, coercion_cost,
                              dshape('bool'), dshape(ds))

if __name__ == '__main__':
    unittest.main()


import os

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.pool import NullPool

class SessionFactory(object):
    @staticmethod
    def get_session():
        var_name = "CONN_STRING"

        return SessionFactory._get_session(var_name)

    @staticmethod
    def _get_session(var_name):
        conn_string = os.environ.get(var_name)

        if not conn_string:
            conn_string = "postgresql+psycopg2://admin:admin@localhost:5433/postgres"

        engine = create_engine(
            conn_string,
            poolclass=NullPool,
            connect_args={
                "connect_timeout": int(os.environ.get("DB_CONN_TIMEOUT", 10))
            },
        )
        Session = sessionmaker(bind=engine)
        session = Session()
        return session


# -*- coding: utf-8 -*-
"""
Created on Sat Dec  5 03:01:00 2020

@author: senol
"""

def get_index_positions(list_of_elems, element):
    ''' Returns the indexes of all occurrences of give element in
    the list- listOfElements '''
    index_pos_list = []
    index_pos = 0
    while True:
        try:
            # Search for item in list from indexPos to the end of list
            index_pos = list_of_elems.index(element, index_pos)
            # Add the index position in list
            index_pos_list.append(index_pos)
            index_pos += 1
        except ValueError as e:
            break
    return index_pos_list

def fix_link(sentence):
    sentence_list = list(sentence)
    space_list = get_index_positions(sentence_list, " ")
    for i in space_list:
        sentence_list[i] = "%20"

    new_sentence = "".join(sentence_list)
    new_sentence = str(new_sentence)

    return new_sentence

#%%
import speech_recognition as sr
import webbrowser
import time
from sys import exit
from gtts import gTTS
from playsound import playsound
import random
import os

r = sr.Recognizer()

def record(ask = False):
    with sr.Microphone() as source:
        if ask:
            speak(ask)

        audio = r.listen(source)
        voice = ''
        try:
            voice = r.recognize_google(audio)

        except sr.UnknownValueError:
            speak("Sorry! I did't understand. Could you repeat that please.")

        except sr.RequestError:
            speak("System is not working.")

        return voice

def response(voice):
    if 'Translate' in voice:
        sentence = record('what do you want to translate')
        url_sentence = fix_link(sentence)
        url = "https://translate.google.com/?sl=en&tl=tr&text="+url_sentence+"&op=translate"
        webbrowser.get().open(url)

    if 'dictionary' in voice:
        sentence = record('which word do you want to know')
        url_sentence = fix_link(sentence)
        url = "https://tureng.com/tr/turkce-ingilizce/"+url_sentence
        webbrowser.get().open(url)

    if 'search' in voice:
        sentence = record('What do you want to search in Google?')
        url_sentence = fix_link(sentence)
        url = "https://www.google.com/search?q="+url_sentence
        webbrowser.get().open(url)

    if 'thank you' in voice:
        speak("Good Bye")
        exit()

def speak(string):
    tts = gTTS(string)
    rand = random.randint(1, 100)
    file = 'translate-audio-'+str(rand)+".mp3"
    tts.save(file)
    playsound(file)
    os.remove(file)

speak("How can I help you.")
time.sleep(1)
while 1:
    voice = record()
    print(voice)
    response(voice)


from fastapi import FastAPI
from starlette.middleware.cors import CORSMiddleware

from payment_mock.api.endpoints.health import router as health_router
from payment_mock.api.endpoints.payment_mock import router as prescriptions_router

def create_app():
    app = FastAPI(title="Partner Payment Mock")

    # Middlewares
    app.add_middleware(
        CORSMiddleware,
        allow_origins=["*"],
        allow_credentials=True,
        allow_methods=["*"],
        allow_headers=["*"],
    )

    # Define routers
    app.include_router(health_router, tags=["health"], prefix="/health")
    app.include_router(
        prescriptions_router, tags=["payment_login_mock"], prefix=""
    )

    return app

app = create_app()


import copy
import numpy as np
import torch

import geometry_util
import encoder_decoder_core

def order_cost(encoder, re_encoded_points, decoded_angles, integration_resamples):
    nearest_angular_distances, nearest_end, nearest_matches = geometry_util.closest_points_periodic(
        decoded_angles)
    nearest_re_encoded = torch.gather(re_encoded_points, -2,
                                      torch.tile(torch.unsqueeze(nearest_matches, -1), [re_encoded_points.size(-1)]))
    euclid_dist = torch.sqrt(torch.sum(torch.square(nearest_re_encoded - re_encoded_points), dim=-1) + 1e-13)
    model_arclengths = \
    encoder.minimum_straight_line_distance(decoded_angles, nearest_end, n_points_integrate=integration_resamples)[1]

    return torch.mean((model_arclengths - euclid_dist) / euclid_dist)

def train(data, n_circular_dimensions, n_linear_dimensions, device, encoder_hidden_dim=1500, encoder_n_hidden=1, decoder_hidden_dim=1500,
          decoder_n_hidden=1, integration_resamples=20, n_points_compare=20,
          batch_size=50, n_training_iterations=3000, loss_stop_thresh=1e-4, decoder_weight=1, order_red_weight=1, scrambling_weight=1):

    embedded_dim = np.shape(data)[1] # we will give the NN points on a ring in 2D as input
    encoder_net = encoder_decoder_core.Encoder1D(embedded_dim, n_circular_dimensions, n_linear_dimensions, encoder_hidden_dim, encoder_n_hidden).to(device)
    decoder_net = encoder_decoder_core.Decoder1D(embedded_dim, n_circular_dimensions, n_linear_dimensions, decoder_hidden_dim, decoder_n_hidden).to(device)

    random_weights = torch.nn.Parameter((torch.ones(n_circular_dimensions + n_linear_dimensions) * -4).to(device))
    params = list(encoder_net.parameters()) + list(decoder_net.parameters()) + [random_weights]

    opt = torch.optim.Adam(params)

    sample_range = np.arange(start=0, stop=np.shape(data)[0], step=1)
    best_loss = np.inf
    best_encoder = copy.deepcopy(encoder_net)
    best_decoder = copy.deepcopy(decoder_net)
    for i in range(n_training_iterations):
        samples = np.array([data[np.random.choice(sample_range, size=n_points_compare, replace=False), :] for i in range(batch_size)])
        data_samples = torch.tensor(samples, dtype=torch.get_default_dtype()).to(device)
        opt.zero_grad()
        decoded_angles = decoder_net(data_samples)
        random_shifts = torch.rand(decoded_angles.size()).to(device) * 2 * np.pi
        saturated_weights = torch.sigmoid(random_weights)
        weight_cost = -1 * torch.sum(saturated_weights)
        scaled_shifts = random_shifts * saturated_weights
        scrambled_angles = decoded_angles + scaled_shifts
        scrambled_angles = torch.atan2(torch.sin(scrambled_angles), torch.cos(scrambled_angles))
        re_encoded_points = encoder_net(scrambled_angles)
        decoder_loss = torch.mean(torch.square(re_encoded_points - data_samples))

        rolled_scrambled_angles = torch.roll(scrambled_angles, 1, dims=-2)
        angular_distances, end_remap = geometry_util.minimum_periodic_distance(
            scrambled_angles, rolled_scrambled_angles)
        angular_distances = torch.sqrt(1e-13 + torch.sum(torch.square((scrambled_angles - end_remap) * (1 - saturated_weights)), dim=-1))
        model_distances = encoder_net.model_length(scrambled_angles, end_remap, integration_resamples)[1]
        normed_angular_distance = angular_distances / torch.mean(angular_distances)
        normed_model_distance = model_distances / torch.mean(model_distances)
        distance_cost = torch.mean(torch.square(normed_angular_distance - normed_model_distance))

        norm_loss = order_cost(encoder_net, re_encoded_points, scrambled_angles, integration_resamples)

        loss = (decoder_weight * decoder_loss) + distance_cost + (norm_loss * order_red_weight) + (scrambling_weight * weight_cost)
        loss.backward()
        opt.step()

        if loss < best_loss:
            best_loss = loss
            best_encoder = copy.deepcopy(encoder_net)
            best_decoder = copy.deepcopy(decoder_net)
            print("iteration: {}, decoding loss: {}, distance cost: {}, order reduction: {}, scrambling weights: {}".format(i, decoder_loss, distance_cost, norm_loss, saturated_weights))

    print('done')
    return best_encoder, best_decoder, saturated_weights


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

import copy
import itertools
import pathlib
from typing import Union, List, NoReturn

from ase import Atoms
from ase.io import read, write

from GDPy.core.variable import Variable
from GDPy.core.operation import Operation
from GDPy.core.register import registers
from GDPy.worker.worker import AbstractWorker
from GDPy.data.array import AtomsNDArray
from GDPy.selector.selector import AbstractSelector, load_cache
from GDPy.selector.composition import ComposedSelector

@registers.variable.register
class SelectorVariable(Variable):

    def __init__(self, selection: List[dict], directory="./", *args, **kwargs) -> None:
        """"""
        selection = copy.deepcopy(selection)
        selectors = []
        for params in selection:
            method = params.pop("method", None)
            selector = registers.create("selector", method, convert_name=True, **params)
            selectors.append(selector)
        nselectors = len(selectors)
        if nselectors > 1:
            selector = ComposedSelector(selectors)
        else:
            selector = selectors[0]

        super().__init__(initial_value=selector, directory=directory)

        return

@registers.operation.register
class select(Operation):

    cache_fname = "selected_frames.xyz"

    def __init__(
        self, structures, selector, directory="./", *args, **kwargs
    ):
        """"""
        super().__init__(input_nodes=[structures, selector], directory=directory)

        return

    @Operation.directory.setter
    def directory(self, directory_) -> NoReturn:
        """"""
        super(select, select).directory.__set__(self, directory_)

        return

    def forward(self, structures: AtomsNDArray, selector: AbstractSelector) -> AtomsNDArray:
        """"""
        super().forward()
        selector.directory = self.directory

        cache_fpath = self.directory/self.cache_fname
        if not cache_fpath.exists():
            new_frames = selector.select(structures)
            write(cache_fpath, new_frames)
        else:
            markers = load_cache(selector.info_fpath)
            structures.markers = markers
            new_frames = read(cache_fpath, ":")
        self._print(f"nframes: {len(new_frames)}")

        self.status = "finished"

        return structures

def run_selection(
    param_file: Union[str,pathlib.Path], structure: Union[str,dict],
    directory: Union[str,pathlib.Path]="./", potter: AbstractWorker=None
) -> None:
    """Run selection with input selector and input structures.
    """
    directory = pathlib.Path(directory)
    if not directory.exists():
        directory.mkdir(parents=True, exist_ok=False)

    from GDPy.utils.command import parse_input_file
    params = parse_input_file(param_file)

    selector = SelectorVariable(
        params["selection"], directory=directory, pot_worker=potter
    ).value
    selector.directory = directory

   # - read structures
    from GDPy.builder import create_builder
    builder = create_builder(structure)
    frames = builder.run() # -> List[Atoms]

    # TODO: convert to a bundle of atoms?
    data = AtomsNDArray(frames)

    # -
    selected_frames = selector.select(data)

    from ase.io import read, write
    write(directory/"selected_frames.xyz", selected_frames)

    return

if __name__ == "__main__":
    ...


from scipy.integrate import quad
from numpy import *
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm

N = 256
T = 50

def S(t):
    return sin(2 * pi * t / T)

plt.figure()
plt.title(' Гармоническое колебание', size=12)
y = [S(t) for t in arange(0, 100, 1)]
x = [t for t in arange(0, 100, 1)]
plt.plot(x, y)
plt.grid()

def w(a, b):
    f = lambda t: (1 / a ** 0.5) * exp(-0.5 * ((t - b) / a) ** 2) * (((t - b) / a) ** 2 - 1) * S(t)
    r = quad(f, -N, N)
    return round(r[0], 3)

x = arange(1, 50, 1)
y = arange(1, 50, 1)
z = array([w(i, j) for j in y for i in x])
X, Y = meshgrid(x, y)
Z = z.reshape(49, 49)
fig = plt.figure("Вейвлет- спектр: гармонического колебания")
ax = Axes3D(fig)
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.jet)
ax.set_xlabel(' Масштаб:a')
ax.set_ylabel('Задержка: b')
ax.set_zlabel('Амплитуда ВП: $ N_{ab}$')
plt.figure("2D-график для z = w (a,b)")
plt.title('Плоскость ab с цветовыми областями ВП', size=12)
plt.contourf(X, Y, Z, 100)
plt.show()


import pycuda.driver as cuda
import pycuda.autoinit
from pycuda.compiler import SourceModule
import numpy
import matplotlib.pyplot as plt
import time
import math

###x = numpy.linspace(0, 2*numpy.pi, 10)
#Prepared data
x = numpy.linspace(-10, 10, 10000000)
y = numpy.arctan(x)

#######################################
############## CUDA ###################
#######################################
x = x.astype(numpy.float32)
y = y.astype(numpy.float32)

#Create interval for interpolation
##interval = numpy.asarray([0, 2*numpy.pi])
interval = numpy.asarray([-10, 10])
min_el = numpy.min(interval)
max_el = numpy.max(interval)

#To transfer data to allocate memory on the device
x_gpu = cuda.mem_alloc(x.nbytes)
y_gpu = cuda.mem_alloc(y.nbytes)

#To transfer the data to the GPU
cuda.memcpy_htod(x_gpu, x)
cuda.memcpy_htod(y_gpu, y)

#Kernel module on C
mod = SourceModule("""
  #include <stdio.h>
  __global__ void linear_interpolation(float *x, float *y, const float min_el, const float max_el)
  {
    int idx = threadIdx.x + blockDim.x*blockIdx.x;
    int jdy = threadIdx.y + blockDim.y*blockIdx.y;
    x[idx] = y[idx]-x[idx]/(max_el-min_el)*1+x[idx];
  }
  """)

#To get the kernel function
func = mod.get_function("linear_interpolation")
start_timer_1 = time.time()
func(x_gpu, y_gpu, numpy.float32(min_el), numpy.float32(max_el), block=(1000,1,1))
x_int = numpy.empty_like(x)
cuda.memcpy_dtoh(x_int, x_gpu)

stop_timer_1 = time.time()
timer_1 = stop_timer_1 - start_timer_1
print('CUDA: ', timer_1, 'sec.')

########################################
######## NUMPY INTERPOLATION ###########
########################################

xvals = numpy.linspace(0, 2*numpy.pi, 10000000)
start_timer_2 = time.time()
yinterp = numpy.interp(xvals,x,y)
stop_timer_2 = time.time()
timer_2 = stop_timer_2 - start_timer_2
print('NUMPY: ', timer_2, 'sec.')

########################################
####### BASE PYTHON INTERPOLATION ######
########################################

start_timer_3 = time.time()
x_list = numpy.array(x).tolist()
y_list = []
interval_list = [-10, 10]
x_iterp = []
for i in x_list:
    y_element = math.atan(i)
    y_list.append(y_element)

for j in range(len(x_list)):
    x_int_elem = (y_list[j] - x_list[j])/(interval_list[1] - interval_list[0])*1 + x_list[j]
    x_iterp.append(x_int_elem)
stop_timer_3 = time.time()
timer_3 = stop_timer_3 - start_timer_3
print('PYTHON BASE: ', timer_3, 'sec.')

#######################################
############### END ###################
#######################################

#x_l = [1, 2, 3]
#x_label = ['CUDA', 'NUMPY', 'BASE PYTHON']
#y_label = [timer_1, timer_2, timer_3]
#plt.axis([1, 3, 0., 1])
#plt.grid(True)
#plt.bar(x_l,y_label, align='center')
#plt.ylabel('Time, sec.')
#plt.title('Comparison CUDA vs Python and Numpy')
#plt.xticks(x_l, x_label)
#plt.show()


import glob
import json
import os
import tempfile
from unittest import mock

import apache_beam as beam
import pytest

from apache_beam.testing import test_pipeline
from apache_beam.testing import util as btest_util

from klio_core.proto import klio_pb2

from klio.transforms import core
from tests.unit import conftest

# NOTE: All of the Klio IO transforms use _KlioIOCounter, which instantiates a
# KlioContext object.  Since all IO transforms use the _KlioIOCounter, we just
# patch on the module level instead of within each and every test function.
patcher = mock.patch.object(core.RunConfig, "get", conftest._klio_config)
patcher.start()
from klio.transforms import io as io_transforms  # NOQA E402

patcher.stop()

HERE = os.path.abspath(os.path.join(os.path.abspath(__file__), os.path.pardir))
FIXTURE_PATH = os.path.join(HERE, os.path.pardir, "fixtures")

def assert_expected_klio_msg_from_file(element):
    message = klio_pb2.KlioMessage()
    message.ParseFromString(element)
    assert message.data.element is not None
    assert isinstance(message.data.element, bytes)

@mock.patch.object(core.RunConfig, "get", conftest._klio_config)
def test_read_from_file():
    file_path = os.path.join(FIXTURE_PATH, "elements_text_file.txt")

    exp_element_count = 0
    with open(file_path, "r") as f:
        exp_element_count = len(f.readlines())

    transform = io_transforms.KlioReadFromText(file_path)
    with test_pipeline.TestPipeline() as p:
        (
            p
            | "Read" >> transform
            | beam.Map(assert_expected_klio_msg_from_file)
        )

    assert transform._reader._REQUIRES_IO_READ_WRAP is False

    actual_counters = p.result.metrics().query()["counters"]
    assert 1 == len(actual_counters)
    assert exp_element_count == actual_counters[0].committed
    assert "KlioReadFromText" == actual_counters[0].key.metric.namespace
    assert "kmsg-read" == actual_counters[0].key.metric.name

def test_write_to_file():
    file_path_read = os.path.join(FIXTURE_PATH, "elements_text_file.txt")

    with tempfile.TemporaryDirectory() as tmp_path:
        with test_pipeline.TestPipeline() as p:
            (
                p
                | io_transforms.KlioReadFromText(file_path_read)
                | io_transforms.KlioWriteToText(tmp_path)
            )
        # WriteToText will shard files so we iterate through each
        # file in the directory
        write_results = []
        exp_element_count = 0
        for file_name in glob.glob(tmp_path + "*"):
            if os.path.isfile(os.path.join(tmp_path, file_name)):
                with open(file_name, "rb") as f:
                    lines = f.readlines()
                    exp_element_count = len(lines)
                    write_results.extend(lines)
        with open(file_path_read, "rb") as fr:
            read_results = fr.readlines()
        assert write_results == read_results

    actual_counters = p.result.metrics().query()["counters"]
    assert 2 == len(actual_counters)

    read_counter = actual_counters[0]
    write_counter = actual_counters[1]
    assert exp_element_count == read_counter.committed
    assert "KlioReadFromText" == read_counter.key.metric.namespace
    assert "kmsg-read" == read_counter.key.metric.name

    assert exp_element_count == write_counter.committed
    assert "KlioWriteToText" == write_counter.key.metric.namespace
    assert "kmsg-write" == write_counter.key.metric.name

def _expected_avro_kmsgs():
    expected_records = [
        {
            "username": "miguno",
            "tweet": "Rock: Nerf paper, scissors is fine.",
            "timestamp": 1366150681,
        },
        {
            "username": "BlizzardCS",
            "tweet": "Works as intended.  Terran is IMBA.",
            "timestamp": 1366154481,
        },
    ]
    expected_kmsgs = []
    for record in expected_records:
        message = klio_pb2.KlioMessage()
        message.version = klio_pb2.Version.V2
        message.metadata.intended_recipients.anyone.SetInParent()
        message.data.element = bytes(json.dumps(record).encode("utf-8"))
        expected_kmsgs.append(message)
    return expected_kmsgs

def assert_expected_klio_msg_from_avro(element):
    expected_kmsgs = _expected_avro_kmsgs()
    message = klio_pb2.KlioMessage()
    message.ParseFromString(element)
    assert message in expected_kmsgs

def test_read_from_avro():
    file_pattern = os.path.join(FIXTURE_PATH, "twitter.avro")

    transform = io_transforms.KlioReadFromAvro(file_pattern=file_pattern)
    with test_pipeline.TestPipeline() as p:
        p | transform | beam.Map(assert_expected_klio_msg_from_avro)

    assert transform._reader._REQUIRES_IO_READ_WRAP is True

    actual_counters = p.result.metrics().query()["counters"]
    assert 1 == len(actual_counters)
    assert 2 == actual_counters[0].committed
    assert "KlioReadFromAvro" == actual_counters[0].key.metric.namespace
    assert "kmsg-read" == actual_counters[0].key.metric.name

def assert_expected_klio_msg_from_avro_write(element):
    file_path_read = os.path.join(FIXTURE_PATH, "elements_text_file.txt")
    with open(file_path_read, "rb") as fr:
        expected_elements = fr.read().splitlines()
    message = klio_pb2.KlioMessage()
    message.ParseFromString(element)
    assert message.data.element in expected_elements

def test_write_to_avro():

    file_path_read = os.path.join(FIXTURE_PATH, "elements_text_file.txt")
    exp_element_count = 0
    with open(file_path_read, "r") as f:
        exp_element_count = len(f.readlines())

    with tempfile.TemporaryDirectory() as tmp_path:
        with test_pipeline.TestPipeline() as p:

            p | io_transforms.KlioReadFromText(
                file_path_read
            ) | io_transforms.KlioWriteToAvro(file_path_prefix=tmp_path)

        files = glob.glob(tmp_path + "*")
        assert len(files) > 0
        assert (
            os.path.isfile(os.path.join(tmp_path, file_name))
            for file_name in files
        )

        with test_pipeline.TestPipeline() as p2:
            p2 | io_transforms.KlioReadFromAvro(
                file_pattern=(tmp_path + "*")
            ) | beam.Map(assert_expected_klio_msg_from_avro_write)

    actual_counters = p.result.metrics().query()["counters"]
    assert 2 == len(actual_counters)

    read_counter = actual_counters[0]
    write_counter = actual_counters[1]

    assert exp_element_count == read_counter.committed
    assert "KlioReadFromText" == read_counter.key.metric.namespace
    assert "kmsg-read" == read_counter.key.metric.name

    assert exp_element_count == write_counter.committed
    assert "KlioWriteToAvro" == write_counter.key.metric.namespace
    assert "kmsg-write" == write_counter.key.metric.name

def test_avro_io_immutability():

    initial_data_path = os.path.join(FIXTURE_PATH, "twitter.avro")

    with tempfile.TemporaryDirectory() as tmp_path:
        with test_pipeline.TestPipeline() as p:

            p | io_transforms.KlioReadFromAvro(
                initial_data_path
            ) | io_transforms.KlioWriteToAvro(
                file_path_prefix=tmp_path, num_shards=0
            )

        with test_pipeline.TestPipeline() as p2:

            p2 | io_transforms.KlioReadFromAvro(
                file_pattern=tmp_path + "*"
            ) | beam.Map(assert_expected_klio_msg_from_avro)

def test_bigquery_mapper_generate_klio_message():

    mapper = io_transforms._KlioReadFromBigQueryMapper()
    message = mapper._generate_klio_message()

    assert message.version == klio_pb2.Version.V2
    assert (
        message.metadata.intended_recipients.WhichOneof("recipients")
        == "anyone"
    )

@pytest.mark.parametrize(
    "klio_message_columns,row,expected",
    (
        (["one_column"], {"a": "A", "b": "B", "one_column": "value"}, "value"),
        (
            ["a", "b"],
            {"a": "A", "b": "B", "c": "C"},
            json.dumps({"a": "A", "b": "B"}),
        ),
        (None, {"a": "A", "b": "B"}, json.dumps({"a": "A", "b": "B"})),
    ),
)
def test_bigquery_mapper_map_row_element(klio_message_columns, row, expected):
    mapper = io_transforms._KlioReadFromBigQueryMapper(
        klio_message_columns=klio_message_columns
    )

    actual = mapper._map_row_element(row)

    assert actual == expected

def _generate_kmsg(element):
    message = klio_pb2.KlioMessage()
    message.version = klio_pb2.Version.V2
    message.metadata.intended_recipients.anyone.SetInParent()
    message.data.element = bytes(json.dumps(element), "utf-8")
    return message.SerializeToString()

def test_read_from_bigquery(monkeypatch):
    read_from_bq = io_transforms.KlioReadFromBigQuery()
    monkeypatch.setattr(read_from_bq, "_reader", beam.Map(lambda x: x))

    table_data = [{"entity_id": 1}, {"entity_id": 2}, {"entity_id": 3}]
    expected_pcoll = [_generate_kmsg(k) for k in table_data]
    with test_pipeline.TestPipeline() as p:
        act_pcoll = p | beam.Create(table_data) | read_from_bq
        btest_util.assert_that(act_pcoll, btest_util.equal_to(expected_pcoll))

    actual_counters = p.result.metrics().query()["counters"]
    assert 1 == len(actual_counters)
    assert len(table_data) == actual_counters[0].committed
    assert "KlioReadFromBigQuery" == actual_counters[0].key.metric.namespace
    assert "kmsg-read" == actual_counters[0].key.metric.name

def _generate_kmsg_with_payload(element):
    message = klio_pb2.KlioMessage()
    message.version = klio_pb2.Version.V2
    message.metadata.intended_recipients.anyone.SetInParent()
    message.data.element = bytes(str(element["entity_id"]), "utf-8")
    message.data.payload = bytes(json.dumps(element), "utf-8")
    return message.SerializeToString()

def test_write_to_bigquery(monkeypatch):
    write_to_bq = io_transforms.KlioWriteToBigQuery(
        project="test-project", dataset="test-dataset", table="test-table"
    )
    monkeypatch.setattr(write_to_bq, "_writer", beam.Map(lambda x: x))

    table_data = [{"entity_id": 1}, {"entity_id": 2}, {"entity_id": 3}]
    with test_pipeline.TestPipeline() as p:
        act_pcoll = (
            p
            | beam.Create(table_data)
            | beam.Map(_generate_kmsg_with_payload)
            | write_to_bq
        )
        btest_util.assert_that(act_pcoll, btest_util.equal_to(table_data))

    actual_counters = p.result.metrics().query()["counters"]
    assert 1 == len(actual_counters)
    assert len(table_data) == actual_counters[0].committed
    assert "KlioWriteToBigQuery" == actual_counters[0].key.metric.namespace
    assert "kmsg-write" == actual_counters[0].key.metric.name


"""
    st_innocent/run.py
"""

import os
import sys
import uvicorn
import argparse
import threading
import subprocess
from server.config import settings
from server.logger import CustomLogger, logger as server_logger

def client_dev():
    """
    Start the client application in dev mode.
    """

    client_logger = CustomLogger.make_logger("client")
    client_logger.info("Starting next.js client.")
    with subprocess.Popen(
        "cd client && yarn run dev",
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        encoding="utf-8",
        shell=True,
        env=os.environ,
    ) as proc:
        while proc.poll() is None:
            out = proc.stdout.readline()
            err = proc.stderr.readline()
            out = out.replace("\n", "")
            err = err.replace("\n", "")
            sys.stdout.flush()
            sys.stderr.flush()
            if len(err) > 0:
                client_logger.error(err)
            elif len(out) > 0:
                client_logger.info(out)

def admin_dev():
    """
    Start the admin application in dev mode.
    """
    admin_logger = CustomLogger.make_logger("admin")
    admin_logger.info("Starting admin app.")
    with subprocess.Popen(
        "cd admin && yarn run start",
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        encoding="utf-8",
        shell=True,
        env=os.environ,
    ) as proc:
        while proc.poll() is None:
            out = proc.stdout.readline()
            err = proc.stderr.readline()
            out = out.replace("\n", "")
            err = err.replace("\n", "")
            sys.stdout.flush()
            sys.stderr.flush()
            if len(err) > 0:
                admin_logger.error(err)
            elif len(out) > 0:
                admin_logger.info(out)

def server_dev():
    """
    Start the server in dev mode.
    """
    server_logger.info("Starting fastapi/uvicorn server.")
    uvicorn.run(
        "server.main:app",
        host=settings.host,
        port=int(settings.server_port),
        log_level="debug" if settings.mode == "dev" else "info",
        reload=True,
    )

if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        prog="St. Innocent's Webapp",
        description="FastAPI server / Next.js client / CRA Admin",
        epilog="run.py: command line utilities",
    )
    parser.add_argument("-b", "--build", action="store_true", help="production build")
    parser.add_argument("-s", "--server", action="store_true", help="only uvicorn")
    parser.add_argument("-a", "--admin", action="store_true", help="only cra admin")
    parser.add_argument("-d", "--dev", action="store_true", help="development run")
    args = parser.parse_args()

    if args.dev:
        admin = threading.Thread(target=admin_dev, args=())
        client = threading.Thread(target=client_dev, args=())
        client.start()
        admin.start()
        server_dev()
    if args.server:
        server_dev()
    if args.admin:
        admin_dev()
    else:
        server_logger.info(f"{args} not supported.")


from django.db import models
from bs4 import BeautifulSoup
import requests

def do_if_sub(table, sub_tag):
	for part in table:
		leg = part.find_all(sub_tag)
		if len(leg) > 1:
			array = []
			for splinter in leg:
				if sub_tag == 'a':
					array.append(splinter['href'])
				if sub_tag != 'a':
					try:
						array.append(splinter.text)
					except:
						array.append(splinter)
			return array
		if len(leg) == 1:
			try:
				return leg.text
			except:
				return leg

def do_no_sub(table, main_tag):
	if len(table) > 1:
		array = []
		for item in table:
			if main_tag == 'a':
				array.append(item['href'])
			if main_tag != 'a':
				try:
					array.append(item.text)
				except:
					array.append(item)
		return array
	if len(table) == 1:
		try:
			return table.text
		except:
			return table

def page(hyper, main_tag=None, sub_tag=None, class_name=None, id_name=None):
	req_headers = {
			'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
			'accept-encoding': 'gzip, deflate, br',
			'accept-language': 'en-US,en;q=0.8',
			'upgrade-insecure-requests': '1',
			'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36'
		}
	source = requests.get(hyper,headers=req_headers).text
	soup = BeautifulSoup(source, 'lxml')
	if class_name == '' and id_name == '':
		table = soup.find_all(main_tag)
		if sub_tag == '':
			return do_no_sub(table, main_tag)
		if sub_tag != '':
			return do_if_sub(table, sub_tag)

	if class_name and id_name == '':
		table = soup.find_all(main_tag, class_=class_name)
		if sub_tag == '':
			return do_no_sub(table, main_tag)
		if sub_tag != '':
			return do_if_sub(table, sub_tag)

	if class_name == '' and id_name:
		table = soup.find(main_tag, id=id_name)
		if sub_tag == 'a':
			array = []
			leg = table.find_all(sub_tag)
			for splinter in leg:
				array.append(splinter['href'])
			return array
		if sub_tag != 'a':
			return [table.text]

class URL(models.Model):
	main_tag = models.CharField(max_length=7, default='div')
	sub_tag = models.CharField(max_length=7, default='a')
	class_name = models.CharField(max_length=100, default='')
	id_name = models.CharField(max_length=100, default='')
	url = models.URLField(max_length=200)
	get_date = models.DateTimeField('date scrapped')
	def __str__(self):
		return self.url

	def get_data(self):
		return page(self.url, self.main_tag, self.sub_tag, self.class_name, self.id_name)


import os
import torch
from matplotlib import pyplot as plt
from tqdm import tqdm

def weights_init(net, init_type='normal', init_gain=0.01, path=None):
    """权重初始化"""

    def init_func(m):
        # 卷积层权重
        if m.__class__.__name__ == 'Conv2d':
            if init_type == 'normal':
                torch.nn.init.normal_(m.weight.data, 0.0, init_gain)
            elif init_type == 'xavier':
                torch.nn.init.xavier_normal_(m.weight.data, gain=init_gain)
        # 归一层
        elif m.__class__.__name__ == 'BatchNorm2d':
            torch.nn.init.normal_(m.weight.data, 1.0, 0.02)
            torch.nn.init.constant_(m.bias.data, 0.0)

    if path and os.path.exists(path):
        print(f'initialize network with path')
        net.load_state_dict(torch.load(path))
    else:
        print(f'initialize network with {init_type} type')
        net.apply(init_func)

def update_log(root, loc_loss, conf_loss):
    with open(root, 'r', encoding='utf-8') as f:
        loc_loss_list = [float(ll) for ll in f.readline().split()]
        conf_loss_list = [float(cl) for cl in f.readline().split()]

    loc_loss_list.append(loc_loss)
    conf_loss_list.append(conf_loss)

    with open(root, 'w', encoding='utf-8') as f:
        f.writelines(' '.join([f'{ll:.3f}' for ll in loc_loss_list]) + '\n')
        f.writelines(' '.join([f'{cl:.3f}' for cl in conf_loss_list]))

def train_epoch(net, train_iter, loss, updater, device, mode='train'):
    loc_loss = 0
    conf_loss = 0
    count = 0
    for X, y in tqdm(train_iter):
        if mode == 'train':
            net.train()
        else:
            net.eval()
        X, y = X.to(device), y.to(device)
        output = net(X)
        loss_l, loss_c = loss(output, y)
        all_loss = loss_l + loss_c

        # 反向传播
        if mode == 'train':
            updater.zero_grad()
            all_loss.backward()
            updater.step()

        loc_loss += loss_l.item()
        conf_loss += loss_c.item()
        count += 1

    return loc_loss / count, conf_loss / count

def train(net, train_iter, num_epoch, loss, updater, updater_scheduler, device, net_param_path, log_path,
          train_threshold=None):
    net.to(device)

    for epoch in range(num_epoch):
        # train
        train_loc_loss, train_conf_loss = train_epoch(net, train_iter, loss, updater, device, mode='train')
        print(f'{epoch + 1}, {train_loc_loss:.3f}, {train_conf_loss:.3f}')
        updater_scheduler.step(train_loc_loss + train_conf_loss)

        # eval
        # val_loc_loss, val_conf_loss = train_epoch(net, train_iter, updater, device, mode='eval')
        # print(f'{epoch + 1}, {val_loc_loss:.3f}, {val_conf_loss:.3f}')

        if train_threshold is None:
            torch.save(net.state_dict(), net_param_path)
            update_log(log_path, train_loc_loss, train_conf_loss)
            print('save model param and log')
            train_threshold = train_conf_loss
        elif train_conf_loss < train_threshold:
            torch.save(net.state_dict(), net_param_path)
            update_log(log_path, train_loc_loss, train_conf_loss)
            print('save model param and log')
            train_threshold = train_conf_loss

def set_figsize(figsize=(3.5, 2.5)):
    """设置matplotlib的图表大小。"""
    plt.rcParams['figure.figsize'] = figsize

def set_axes(axes, xlabel, ylabel, xlim, ylim, xscale, yscale, legend):
    """设置matplotlib的轴。"""
    axes.set_xlabel(xlabel)
    axes.set_ylabel(ylabel)
    axes.set_xscale(xscale)
    axes.set_yscale(yscale)
    axes.set_xlim(xlim)
    axes.set_ylim(ylim)
    if legend:
        axes.legend(legend)
    axes.grid()

def plot(X, Y=None, xlabel=None, ylabel=None, legend=None, xlim=None, ylim=None, xscale='linear', yscale='linear',
         fmts=('-', 'm--', 'g-.', 'r:'), figsize=(3.5, 2.5), axes=None, tpye=0):
    """绘制数据点。"""
    if legend is None:
        legend = []

    # 设置画布大小
    set_figsize(figsize)
    # 移动坐标轴
    axes = axes if axes else plt.gca()

    # 如果 `X` 有一个轴，输出True
    def has_one_axis(X):
        return hasattr(X, "ndim") and X.ndim == 1 or isinstance(X, list) and not hasattr(X[0], "__len__")

    if has_one_axis(X):
        X = [X]
    if Y is None:
        X, Y = [[]] * len(X), X
    elif has_one_axis(Y):
        Y = [Y]
    if len(X) != len(Y):
        X = X * len(Y)
    # 清除当前活动轴
    axes.cla()
    for x, y, fmt in zip(X, Y, fmts):
        if len(x):
            axes.plot(x, y, fmt)
        else:
            axes.plot(y, fmt)
    set_axes(axes, xlabel, ylabel, xlim, ylim, xscale, yscale, legend)
    if not tpye:
        plt.show()

def plot_log(size=300):
    with open(f'../logs/m_mask_ssd{size}.log', 'r', encoding='utf-8') as f:
        loc_loss_list = [float(ll) for ll in f.readline().split()]
        conf_loss_list = [float(cl) for cl in f.readline().split()]
    num_epoch = len(loc_loss_list)
    print(num_epoch)
    plot(X=[range(1, num_epoch + 1)], Y=[loc_loss_list, conf_loss_list], xlabel='epoch', ylabel='loss',
         xlim=[1, num_epoch], legend=['loc_loss', 'conf_loss'], figsize=(6, 4))

if __name__ == '__main__':
    plot_log(300)
    # plot_log(512)


#!/usr/bin/env python3

"""
Script de prueba para generar la distrubución de alfa-Rossi a partir de la
simulación con PTRAC en modo lista sin tener en cuenta las coincidencias
accidentales.
"""

import numpy as np
import sys

sys.path.append('/home/pablo/CinePy/')
from modules.alfa_rossi_procesamiento import alfa_rossi_procesamiento
from modules.io_modules import read_PTRAC_CAP_bin
from modules.alfa_rossi_escritura import escribe_datos_promedio
from modules.simulacion_modules import lee_nps_entrada, \
                                       separa_en_historias_sin_accidentales

if __name__ == '__main__':

    archivo_entrada = 'in_sdef'
    archivo_n = 'in_sdef_n.p'

    # -------------------------------------------------------------------------
    # Pre-procesamiento
    # -------------------------------------------------------------------------
    # Se lee la cantidad de eventos de fuente del archivo de entrada de MCNP
    nps = lee_nps_entrada(archivo_entrada)
    # Se leen los datos en binario
    print('Leyendo arhvo de neutrones....')
    datos_n, header_n = read_PTRAC_CAP_bin(archivo_n)
    # Se separan por historias
    print('Separando datos por hisotrias....')
    times_out = separa_en_historias_sin_accidentales(datos_n)

    # -------------------------------------------------------------------------
    # Procesamiento
    # -------------------------------------------------------------------------
    # Duración de cada bin (discretización del tau) [s]
    dt_s = 2e-4
    # Máximo bin analizado (mayor tau) [s]
    dtmax_s = 2e-2
    # Nombre de la carpeta donde se guardarán los resultados
    nombre_carpeta = 'resultados_arossi'
    tb = 1
    # Procesamiento de los datos
    dat_pro = alfa_rossi_procesamiento(times_out, dt_s, dtmax_s, tb, 'all')

    # En este caso la cantidad de historias lo determina la simulación
    Nhist = np.shape(dat_pro[0])[0]
    # Se escribe sólo el promedio
    # Si se usan más detectores, se debe crear un nombre para cada detector
    archivo_out = [archivo_n]
    escribe_datos_promedio(dat_pro, archivo_out, Nhist, dt_s, dtmax_s, tb,
                           nombre_carpeta)


#!/bin/bash
# pylint: disable=invalid-name

"""Demonstrates reference counting."""

from typing import Any, List, Optional

import sys

def main() -> None:
    """Performs the main task of the script."""

    a: Optional[List[Any]] = []
    b = a
    c = b

    # [] is referenced by a, b, c and getrefcount's argument
    print(f"a has {sys.getrefcount(a)} ref(s)")

    c = None

    print(f"c is now {c}")

    # [] is referenced by a, b and getrefcount's argument
    print(f"a has {sys.getrefcount(a)} ref(s)")

if __name__ == "__main__":
    main()


import json
from logging import log
import os
from nltk.util import pr
import config
import logging
from bs4 import BeautifulSoup

class EntityResolution:

    def __init__(self, label_file_dir):
        if os.path.exists(config.LABEL_RESOLUTION_KV):
            with open(config.LABEL_RESOLUTION_KV, "r") as f:
                self.entity_label_dict = json.load(f)
                self.stored_dict = True
        else:
            self.stored_dict = False
            self.label_soups = []
            label_files = os.listdir(label_file_dir)
            for label_file in label_files:
                if label_file.endswith(".xml"):
                    with open(label_file_dir + label_file) as label:
                        self.label_soups.append(BeautifulSoup(label, config.BS_PARSER_XBRL))

    def get_label_text(self, xbrl_id):
        if self.stored_dict:
            return self.entity_label_dict[xbrl_id]
        else:
            xbrl_id_converted = config.PREFIX_GAAP + config.LABEL_PREFIX_ID_SEPARATOR + xbrl_id.split(config.XBRL_PREFIX_ID_SEPARATOR)[1]
            labels = set()
            for soup in self.label_soups:
                for tag in soup.find_all(name="labelarc", attrs={"xlink:from": lambda x: x and x.lower()==xbrl_id_converted.lower()}):
                    label_tag = soup.find(attrs={"id": tag["xlink:to"]})
                    if not "documentation" in label_tag["xlink:role"]:
                        labels.add(label_tag.text)
            if len(labels) == 0:
                logging.warning("There was no plain text label found for: " + xbrl_id)
            return sorted(labels, key=len)