from flask import Flask, render_template, redirect, url_for, flash, abort,request
from flask_sqlalchemy import SQLAlchemy
from flask_login import UserMixin
import json

app=Flask(__name__)

#connect to db
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///details.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)

#tabel creation
class Detail(UserMixin, db.Model):
    __tablename__ = "detail"
    id=db.Column(db.Integer,primary_key=True)
    firstName = db.Column(db.String(100))
    lastName = db.Column(db.String(100))
    email = db.Column(db.String(100), unique=True)
    mobile=db.Column(db.Integer,unique=True)
    address = db.Column(db.String(1000))

# db.create_all()

@app.route("/")
def main_page():
    return render_template("index.html")
@app.route("/customers",methods=["GET", "POST","PUT","DELETE"])
def get_page():
    posts = Detail.query.all()
    return render_template("get.html",inf=posts)
@app.route("/customers/post",methods=["GET", "POST","PUT","DELETE"])
def post_page():
    if request.method=="POST":
        new_detail=Detail(
            id=request.form["id"],
            firstName=request.form["firstName"],
            lastName=request.form["lastName"],
            email=request.form["email"],
            mobile=request.form["mobile"],
            address=request.form["address"]
        )
        db.session.add(new_detail)
        db.session.commit()
        return redirect(url_for("get_page"))
    return render_template("post.html")

@app.route("/customers/put",methods=["GET", "POST","PUT","DELETE"])
def put_page():
    if request.method=="POST":
        id=request.form["id"]
        tobe_updated=Detail.query.get(id)
        tobe_updated.firstName=request.form["firstName"]
        tobe_updated.lastName = request.form["lastName"]
        tobe_updated.email = request.form["email"]
        tobe_updated.mobile = request.form["mobile"]
        tobe_updated.address = request.form["address"]
        db.session.commit()
        return redirect(url_for("get_page"))
    return render_template("put.html")

@app.route("/customers/<int:id>",methods=["GET", "POST","PUT","DELETE"])
def get_page_details(id):
    inf=Detail.query.get(id)
    arr=[]
    arr.append({
            "id":inf.id,
            "firstName":inf.firstName,
            "lastName":inf.lastName,
            "email":inf.email,
            "mobile":inf.mobile,
            "address":inf.address
        })
    return json.dumps({"Detail":arr}),200,{"Content- Type":"Application/json"}

@app.route("/customers/getall",methods=["GET", "POST","PUT","DELETE"])
def get_all_page_details():
    info=Detail.query.all()
    arr=[]
    for inf in info:
        arr.append({
                "id":inf.id,
                "firstName":inf.firstName,
                "lastName":inf.lastName,
                "email":inf.email,
                "mobile":inf.mobile,
                "address":inf.address
            })
    return json.dumps({"Detail":arr}),200,{"Content- Type":"Application/json"}

@app.route("/customers/delete",methods=["GET", "POST","PUT","DELETE"])
def delete_page():

    if request.method=="POST":
        id=request.form["id"]
        id_to_delete=Detail.query.get(id)
        db.session.delete(id_to_delete)
        db.session.commit()
        return redirect(url_for("get_page"))

    return render_template("delete.html")

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


text = open('InputTxt/day11input.txt', 'r')

grid = {(x,y): int(point) for x, line in enumerate(text) for y, point in enumerate(line.strip())}

def adjacent(point):
    x = point[0]
    y = point[1]
    return filter(grid.get, [(x-1,y), (x+1,y), (x,y-1), (x,y+1),
                             (x-1,y-1), (x+1,y+1), (x+1, y-1), (x-1,y+1)])

answer = 0
for x in range(0,1000000000000000000000):
    for point in grid:
        grid[point] += 1
    flashes = {point for point in grid if grid[point] > 9}

    while flashes:
        point = flashes.pop()
        grid[point] = 0
        answer += 1
        for p in adjacent(point):
            grid[p] += 1
            if grid[p] > 9:
                flashes.add(p)
    if x == 99:
        print(answer)
    if sum(grid.values()) == 0:
        print(x+1)
        break


from PyQt5.QtWidgets import QWidget, QPushButton, QLabel, QGridLayout, QSizePolicy
from PyQt5.QtCore import pyqtSignal

class MainBar(QWidget):

    switch_to_create_client = pyqtSignal()
    switch_to_partners = pyqtSignal()
    switch_to_clients = pyqtSignal()
    switch_to_calendar = pyqtSignal()
    switch_exit = pyqtSignal()

    def __init__(self, parent):
        QWidget.__init__(self, parent=parent)
        self.setObjectName("DarkTopper")
        self.style().unpolish(self)
        self.style().polish(self)

        main_layout = QGridLayout()

        self.picture = QPushButton("Картинка типа", self)
        self.picture.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred)

        self.login = QPushButton("Логин типа", self)
        self.login.clicked.connect(self.emit_exit)
        self.login.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred)

        self.create = QPushButton("Создать", self)
        self.create.setObjectName("GreenButton")
        self.create.clicked.connect(self.emit_create_client)
        self.create.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred)

        self.clients = QPushButton("Клиенты", self)
        self.clients.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred)
        self.clients.clicked.connect(self.emit_clients)

        self.partners = QPushButton("Партнеры", self)
        self.partners.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred)
        self.partners.clicked.connect(self.emit_partners)

        self.calendar = QPushButton("Календарь", self)
        self.calendar.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred)
        self.calendar.clicked.connect(self.emit_calendar)

        main_layout.addWidget(self.picture, 0, 0, 1, 1)
        main_layout.addWidget(self.login, 0, 3, 1, 1)
        main_layout.addWidget(self.create, 1, 0, 1, 1)
        main_layout.addWidget(self.clients, 1, 1, 1, 1)
        main_layout.addWidget(self.partners, 1, 2, 1, 1)
        main_layout.addWidget(self.calendar, 1, 3, 1, 1)

        self.setLayout(main_layout)

    def set_login(self, val):
        self.login.setText(val)

    def get_partners_signal(self):
        return self.switch_to_partners

    def get_clients_signal(self):
        return self.switch_to_clients

    def get_calendar_signal(self):
        return self.switch_to_calendar

    def emit_partners(self):
        self.switch_to_partners.emit()

    def emit_clients(self):
        self.switch_to_clients.emit()

    def emit_calendar(self):
        self.switch_to_calendar.emit()

    def emit_create_client(self):
        self.switch_to_create_client.emit()

    def emit_exit(self):
        self.switch_exit.emit()


from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.properties import ObjectProperty

class Main(MDApp):
    in_class = ObjectProperty(None)

    def build(self):
        return Builder.load_file("styles/style.kv")

    def auth(self):
        if self.root.in_class.text == 'root':
            label = self.root.ids.show
            label.text = "Sucess"
        else:
            label = self.root.ids.show
            label.text = "Fail"

if __name__ == '__main__':
    Main().run()

# https://stackoverflow.com/questions/68368729/kivy-mysql-checking-if-a-username-and-password-exists-in-the-database-not-worki


def Four_Squares(N):
      M = [[] for T in range(N+1)]
         ## Element T of M will be populated with all pairs [a,b] such that a, b < sqrt(N) and a*a + b*b = T

      for a in range(0,int(sqrt(N//2)) + 1):
         for b in range(a,int(sqrt(N)) + 1):
            T = a*a + b*b
            if T <= N:
               M[T].append([a,b])
            else:
               break

      P = []
         ## P will be populated with all lists [a,b,c,d] such that 0 <= a <= b <= c <= d whose squares sum to N

      for i in range(0,N//2 + 1):
         if len(M[N-i]) == 0:
            break
         for j in range(0,len(M[i])):
            for k in range(0,len(M[N-i])):
               X = [M[i][j][0], M[i][j][1],
                    M[N-i][k][0], M[N-i][k][1]]
               if X[1] <= X[2]:
                  P.append(X)

      return P


from datetime import datetime
from flask import Flask, render_template, url_for, request,redirect,flash
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.sql.functions import func
from sqlalchemy import text
from random import shuffle

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///tournaments.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = 'True'
app.secret_key = 'queijo'
db = SQLAlchemy(app)

class Tournaments(db.Model):
    __tablename__ = 'tournaments'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(200), nullable=False)
    date_created = db.Column(db.DateTime, default=datetime.utcnow)

    def __repr__(self):
        return '<Tournament %r>' % self.id

class Matchs(db.Model):
    __tablename__ = 'matchs'
    id = db.Column(db.Integer, primary_key =True)
    match_number =  db.Column(db.Integer)
    Tournament_id = db.Column(db.Integer, db.ForeignKey('tournaments.id'),nullable = False)
    tournaments = db.relationship("Tournaments", backref = db.backref("tournaments",uselist = False))
    challenger_1 = db.Column(db.String(200),nullable = False)
    challenger_2 = db.Column(db.String(200),nullable = False)
    score_1 = db.Column(db.Integer)
    score_2 = db.Column(db.Integer)
    status = db.Column(db.String(10), nullable = False)

    def __repr__(self):
        return '<Match %r>' % self.id

class Challengers(db.Model):
    __tablename__ = 'challengers'
    id = db.Column(db.Integer, primary_key =True)
    name = db.Column(db.String(200),nullable = False)
    Tournament_id = db.Column(db.Integer, db.ForeignKey('tournaments.id'),nullable = False)
    tournaments2 = db.relationship("Tournaments", backref = db.backref("tournaments2",uselist = False))
   # matchs = db.relationship("Matchs", backref = db.backref("matchs",uselist = False))
   # Match_id = db.Column(db.Integer, db.ForeignKey('matchs.id'),nullable = False)
   # matchs = db.relationship("matchs", backref = db.backref("matchs",uselist = False))

    def __repr__(self):
        return '<Challenger %r>' % self.id

@app.route('/',methods=['POST','GET'])
def index():
    return render_template('index.html')

@app.route('/tournaments/', methods=['POST','GET'])
def tournaments():
    if request.method == 'POST':
        task_content = request.form.get('tournament')
        search = Tournaments.query.filter_by(name=str(task_content)).first()
        if search == None:
            new_task = Tournaments(name=str(task_content))
            try:
                db.session.add(new_task)
                db.session.commit()
                flash('Tournament created')
                return redirect('/tournaments/')
            except:
                return flash('There was an issue adding your task')
        else:
             return flash('Already exists')
    else:
            tasks = Tournaments.query.order_by(Tournaments.date_created).all()
            return  render_template('tournaments.html', Tournaments = tasks)

@app.route('/tournaments/<int:id>', methods = ['POST','GET'])
def tournament(id):
    if request.method == "GET":
        tasks = Matchs.query.filter_by(Tournament_id = id).all()
        if tasks != []:
            return render_template('tournament.html', Matchs = tasks)
        else:
            return flash("There are no matches yet")

@app.route('/tournaments/update/<int:id>')
def update_tournament(id):
    task = request.form.get("form")
    print(task)
    try:
        update_data = Tournaments.query.filter_by(id = id).all()
        db.session.commit()
        return redirect('/tournaments/')
    except:
        return flash("There was a problem deleting the tournament" )

@app.route('/tournaments/delete/<int:id>')
def delete_tournament(id):
    try:
        db.session.delete(Tournaments.query.get_or_404(id))
        db.session.commit()
        return redirect('/tournaments/')
    except:
        return flash("there was a problem deleting the tournament" )

@app.route('/matchs/', methods= ['POST','GET'])
def matchs():
    if request.method == 'GET':
        arc = Challengers.query.order_by(Challengers.Tournament_id).all()
        arm = Matchs.query.order_by(Matchs.match_number)
        sumc = 0
        summ = 0
        for item in arc:
            sumc += 1
        for item in arm:
            summ += 1
        if (sumc - summ) > 0:
            idt = Challengers.query.distinct(Challengers.Tournament_id)

            for id in idt:
                challengers  = Challengers.query.filter_by(Tournament_id = id.Tournament_id).all()
                if (sumc % 2) == 0:
                    cont = 1
                    while challengers != []:
                        shuffle(challengers)

                        number_1 = challengers[0].name
                        number_2 = challengers[1].name

                        check_1 = Matchs.query.filter_by(Tournament_id = id.Tournament_id, challenger_1 = number_1, challenger_2 = number_2).all()
                        check_2 = Matchs.query.filter_by(Tournament_id = id.Tournament_id, challenger_1 = number_2, challenger_2 = number_1).all()

                        if check_1 == [] and check_2 == []:
                            new_task = Matchs(match_number = cont,Tournament_id = id.Tournament_id, challenger_1 = number_1, challenger_2 = number_2, score_1 = 0, score_2 = 0, status = "on game")
                            try:
                                db.session.add(new_task)
                                db.session.commit()
                            except:
                                return flash("Error on matchmaking")
                        challengers.pop(0)
                        challengers.pop(0)

                        cont += 1
                    tasks = Matchs.query.order_by(Matchs.Tournament_id).all()
                    return render_template('matchs.html',Matchs = tasks)
                else:
                    if (sumc % 2) == 0:
                        cont = 1
                        while len(challengers) != 1:
                            shuffle(challengers)

                            number_1 = challengers[0].name
                            number_2 = challengers[1].name

                            check_1 = Matchs.query.filter_by(Tournament_id = id.Tournament_id, challenger_1 = number_1, challenger_2 = number_2).all()
                            check_2 = Matchs.query.filter_by(Tournament_id = id.Tournament_id, challenger_1 = number_2, challenger_2 = number_1).all()

                            if check_1 == [] and check_2 == []:
                                new_task = Matchs(match_number = cont,Tournament_id = id.Tournament_id, challenger_1 = number_1, challenger_2 = number_2, score_1 = 0, score_2 = 0, status = "on game")
                                try:
                                    db.session.add(new_task)
                                    db.session.commit()
                                except:
                                    return flash("Error on matchmaking")
                            challengers.pop(0)
                            challengers.pop(0)
                            cont += 1

                    tasks = Matchs.query.order_by(Matchs.Tournament_id).all()
                    return render_template('matchs.html',Matchs = tasks)

        else:
            tasks = Matchs.query.order_by(Matchs.Tournament_id).all()
            return render_template('matchs.html',Matchs = tasks)

@app.route('/tournaments/match/update/<int:id>')
def update_match(id):
    try:
        db.session.delete(Matchs.query.get_or_404(id))
        db.session.commit()
        return redirect('/matchs/')
    except:
        return flash("there was a problem deleting the match" )

@app.route('/tournaments/match/delete/<int:id>')
def delete_match(id):
    try:
        db.session.delete(Matchs.query.get_or_404(id))
        db.session.commit()
        return redirect('/matchs/')
    except:
        return flash("there was a problem deleting the match" )

@app.route('/challengers/', methods= ['POST','GET'])
def challenger():
    if request.method == 'POST':
        task_content = request.form.get('match')
    else:
        tasks = Challengers.query.order_by(Challengers.name).all()
        return render_template('challengers.html', Challengers = tasks)

@app.route('/tournaments/<int:id>/challenger/create', methods=['POST','GET'])
def join_tournament(id):
    if request.method == 'POST':
        task_content = request.form.get('challenger')
        print(task_content)
        search = Challengers.query.filter_by(name=str(task_content)).first()
        if search == None:
            new_task = Challengers(name=str(task_content),Tournament_id = id)
            try:
                db.session.add(new_task)
                db.session.commit()
                flash('Joined with sucess')
                return redirect('/tournaments/' )
            except:
                flash('There was an issue adding your task')
                return redirect('/tournaments/' )
        else:
             flash('Already exists')
             return redirect('/tournaments/' )
    else:
            tasks = Tournaments.query.order_by(Tournaments.date_created).all()
            return  render_template('tournaments.html', Tournaments = tasks)

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


import numpy as np
import matplotlib.pyplot as plt
x_cords=np.linspace(-100,100,500)
y_cords=np.linspace(-100,100,500)
points=[]
for y in y_cords:
    for x in x_cords:
        if ((x*0.03)**2+(y*0.03)**2-1)**3-(x*0.03)**2*(y*0.03)**3<=0:points.append({"x": x, "y": y})
hear_x=list(map(lambda point:point["x"],points))
hear_y=list(map(lambda point:point["y"],points))
plt.scatter(hear_x,hear_y,s=10,alpha=0.5,c=range(len(hear_x)),cmap="gist_rainbow")
plt.show()


# string queue
from collections import deque

def solution(msg):
    book = {'': 0}
    i = 1
    while i <= 26:
        book[chr(ord('A') + i - 1)] = i
        i += 1

    result = []
    msg = deque(msg)

    while msg:
        w = ''
        while msg and w in book:
            w += msg.popleft()

        if not msg:
            if w in book:
                result.append(book[w])
                break

        c = w[-1]
        w = w[:-1]
        result.append(book[w])
        book[w + c] = i
        i += 1
        msg.appendleft(c)

    return result

if __name__ == '__main__':
    print(solution('KAKAO'))


from django.urls import include, path
from rest_framework import routers

from api.views import (APISubscribtionCreateDelete, IngredientViewSet,
                       RecipeViewSet, SubscriptionViewSet, TagViewSet)

router_v1 = routers.DefaultRouter()
router_v1.register(r'tags', TagViewSet, basename='tags')
router_v1.register(r'ingredients', IngredientViewSet, basename='ingredients')
router_v1.register(r'recipes', RecipeViewSet, basename='recipes')
router_v1.register(r'users/subscriptions', SubscriptionViewSet,
                   basename='subscriptions')

urlpatterns = [
    path('users/<int:pk>/subscribe/', APISubscribtionCreateDelete.as_view(),
         name='api_subscribe'),
    path('', include(router_v1.urls)),
]


from app import app
from config import *
from flask import render_template, abort, jsonify
import os.path

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/csv/<module>/<metric>')
def csv(module=None, metric=None):
    if module is None or metric is None:
        abort(400)

    filePath = '{}/{}/{}.csv'.format(LOGS_DIR, module, metric)
    if not os.path.exists(filePath):
        abort(404)

    file = open(filePath, "r")
    content = file.read()
    file.close()

    response = app.response_class(
        response = content,
        status = 200,
        mimetype = 'text/csv'
    )

    return response

@app.route('/get_method_usages')
def method_usages():
    path = "{}/runtime".format(LOGS_DIR)
    methods = []
    if os.path.isdir(path):
        methods = [f[:-4] for f in os.listdir(path)]
    return jsonify(methods)


from typing import Tuple, Union, List, Generator, ByteString

from boefjes.job_models import BoefjeMeta
from PIL import Image
import io
import os
import docker
import logging
import tarfile

PLAYWRIGHT_IMAGE = "mcr.microsoft.com/playwright:v1.30.0-focal"
BROWSER = "chromium"

class TarStream(io.RawIOBase):
    """Wrapper around generator to feed tarfile.

    Based on:
    - https://stackoverflow.com/questions/39155958/how-do-i-read-a-tarfile-from-a-generator
    - https://stackoverflow.com/questions/6657820/how-to-convert-an-iterable-to-a-stream/6658949
    """

    def __init__(self, stream: Generator):
        """Store the generator in the TarStream class."""
        self.bytes_left = None
        self.stream = stream
        self.able_to_read = bool(stream)
        super().__init__()

    def reader(self) -> io.BufferedReader:
        """Return the bufferedreader for this TarStream."""
        return io.BufferedReader(self)

    def readable(self) -> bool:
        """Returns whether the generator stream is (still) readable."""
        return self.able_to_read

    def readinto(self, memory_view: ByteString) -> int:
        """Read the generator. Returns 0 when done. Output is stored in memory_view."""
        try:
            chunk = self.bytes_left or next(self.stream)
        except StopIteration:
            self.able_to_read = False
            return 0
        view_len = len(memory_view)
        output, self.bytes_left = chunk[:view_len], chunk[view_len:]
        outlen = len(output)
        memory_view[:outlen] = output
        return outlen

def get_file_from_container(container: docker.models.containers.Container, path: str) -> bytes:
    """Returns a file from a docker container."""
    try:
        stream, _ = container.get_archive(path)
    except docker.errors.NotFound:
        logging.warning(
            "[Webpage Capture] %s not found in container %s %s", path, container.short_id, container.image.tags
        )
        return None

    f = tarfile.open(mode="r|", fileobj=TarStream(stream).reader())
    tarobject = f.next()
    if tarobject.name == os.path.basename(path):
        return f.extractfile(tarobject).read()

def build_playwright_command(webpage: str, browser: str, tmp_path: str) -> str:
    """Returns playwright command including webpage, browser and locations for image, har and storage."""
    return " ".join(
        [
            "playwright screenshot",
            f"-b {browser}",
            "--full-page",
            f"--save-har={tmp_path}.har.zip",
            f"--save-storage={tmp_path}.json",
            webpage,
            f"{tmp_path}.png",
        ]
    )

def run_playwright(webpage: str, browser: str, tmp_path: str = "/tmp/tmp") -> Tuple[bytes]:
    """Run Playwright in Docker."""
    client = docker.from_env()
    res = client.containers.run(
        image=PLAYWRIGHT_IMAGE,
        command=[
            "/bin/sh",
            "-c",
            f"npx -y {build_playwright_command(webpage=webpage, browser=browser, tmp_path=tmp_path)}",
        ],
        detach=True,
    )
    res.wait()
    logging.warning("[Webpage Capture] HAR (.har.zip) and Storage (.json) files not working in 1.30.0.")
    image = Image.open(io.BytesIO(get_file_from_container(container=res, path=f"{tmp_path}.png")))
    har = get_file_from_container(container=res, path=f"{tmp_path}.har.zip")
    storage = get_file_from_container(container=res, path=f"{tmp_path}.json")
    res.remove()

    return image.tobytes(), har, storage

def run(boefje_meta: BoefjeMeta) -> List[Tuple[set, Union[bytes, str]]]:
    """Creates webpage and takes capture using Playwright container."""
    input_ = boefje_meta.arguments["input"]
    webpage = f"{input_['scheme']}://{input_['netloc']['name']}{input_['path']}"

    image_bytes, har_zip, storage_json = run_playwright(webpage=webpage, browser=BROWSER)

    return [
        (set("image/png"), image_bytes),
        (set("application/zip+json"), har_zip),
        (set("application/json"), storage_json),
    ]


#!/usr/bin/python
import sys
import string
import numpy as np
from Bio.PDB.DSSP import *
import glob
#print(glob.glob("/home/hlim/dssp/2ov*"))
#sys.exit()
outfile='./SeqID30_secondary_str_angle_statistics.txt'
'''
H	Alpha helix (4-12)
B	Isolated beta-bridge residue
E	Strand
G	3-10 helix
I	Pi helix
T	Turn
S	Bend
-	None

Relative accessibility=residue_accessibility / max(residue_accessibility)
'''
h_phi=[]
h_psi=[]
b_phi=[]
b_psi=[]
e_phi=[]
e_psi=[]
g_phi=[]
g_psi=[]
i_phi=[]
i_psi=[]
t_phi=[]
t_psi=[]
s_phi=[]
s_psi=[]
n_phi=[]
n_psi=[]
#########

ciffiles=glob.glob("/home/hlim/PDB/SeqID30/*.cif")
filecount=len(ciffiles)
count=0
for cif in ciffiles:

    f=string.replace(cif,'PDB/SeqID30','dssp')
    f=string.replace(f,'cif','dssp')
    try:
        dssp,keys=make_dssp_dict(f)
    except:
        continue
    keys=keys[1:-1] #Skip the first and last residue as their angles are half-missing
    for key in keys:

        ds=dssp[key]
        aa=str(ds[0])
        ss=str(ds[1])
        acc=float(ds[2])
        psi=float(ds[3])
        phi=float(ds[4])
        if (aa=='G') or (aa=='P'):
            #skip glycine or proline
            continue
        if ss=='H':
            h_phi.append(phi)
            h_psi.append(psi)
        elif ss=='B':
            b_phi.append(phi)
            b_psi.append(psi)
        elif ss=='E':
            e_phi.append(phi)
            e_psi.append(psi)
        elif ss=='G':
            g_phi.append(phi)
            g_psi.append(psi)
        elif ss=='I':
            i_phi.append(phi)
            i_psi.append(psi)
        elif ss=='T':
            t_phi.append(phi)
            t_psi.append(psi)
        elif ss=='S':
            s_phi.append(phi)
            s_psi.append(psi)
        else:
            n_phi.append(phi)
            n_psi.append(psi)
    count+=1
    if count % 10000 ==0:
        print("%s files done. Total %s files."%(str(count),str(filecount)))

S=open(outfile,"w")
S.write("StructureType\tmin.Psi\tmax.Psi\tmean.Psi\tstd.Psi\tmin.Phi\tmax.Phi\tmean.Phi\tstd.Phi\tCount\n")

S.write("AlphaHelix\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%s\n"%( np.amin(h_psi),np.amax(h_psi),np.mean(h_psi),np.std(h_psi),np.amin(h_phi),np.amax(h_phi),np.mean(h_phi),np.std(h_phi),str(len(h_psi))))
S.write("BetaBridge\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%s\n"%( np.amin(b_psi),np.amax(b_psi),np.mean(b_psi),np.std(b_psi),np.amin(b_phi),np.amax(b_phi),np.mean(b_phi),np.std(b_phi),str(len(b_psi))))
S.write("Strand\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%s\n"%( np.amin(e_psi),np.amax(e_psi),np.mean(e_psi),np.std(e_psi),np.amin(e_phi),np.amax(e_phi),np.mean(e_phi),np.std(e_phi),str(len(e_psi))))
S.write("3-10Helix\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%s\n"%( np.amin(g_psi),np.amax(g_psi),np.mean(g_psi),np.std(g_psi),np.amin(g_phi),np.amax(g_phi),np.mean(g_phi),np.std(g_phi),str(len(g_psi))))
S.write("PiHelix\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%s\n"%( np.amin(i_psi),np.amax(i_psi),np.mean(i_psi),np.std(i_psi),np.amin(i_phi),np.amax(i_phi),np.mean(i_phi),np.std(i_phi),str(len(i_psi))))
S.write("Turn\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%s\n"%( np.amin(t_psi),np.amax(t_psi),np.mean(t_psi),np.std(t_psi),np.amin(t_phi),np.amax(t_phi),np.mean(t_phi),np.std(t_phi),str(len(t_psi))))
S.write("Bend\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%s\n"%( np.amin(s_psi),np.amax(s_psi),np.mean(s_psi),np.std(s_psi),np.amin(s_phi),np.amax(s_phi),np.mean(s_phi),np.std(s_phi),str(len(s_psi))))
S.write("Others\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%s\n"%( np.amin(n_psi),np.amax(n_psi),np.mean(n_psi),np.std(n_psi),np.amin(n_phi),np.amax(n_phi),np.mean(n_phi),np.std(n_phi),str(len(n_psi))))


from slackclient import SlackClient
from private import SLACK_TOKEN

slack_client = SlackClient(SLACK_TOKEN)

if __name__ == "__main__":
    api_call = slack_client.api_call("users.list")
    if api_call.get('ok'):
        # retrieve all users so we can find our bot
        users = api_call.get('members')
        for user in users:
            if 'name' in user:
                print('{0:20}{1}'.format(user['name'], user.get('id')))
    else:
        print('Could not connecto to slack')


# -*- coding: utf-8 -*-
from rest_framework import serializers

from domains.models import Domain
from users.models import User
from .models import Task, Status, Code, Executor

class CodeSerializer(serializers.ModelSerializer):
    class Meta:
        model = Code
        fields = ('pk', 'code', 'name', 'comment')

class StatusSerializer(serializers.ModelSerializer):
    class Meta:
        model = Status
        fields = ('pk', 'status', 'comment')

class DomainTaskSerializer(serializers.ModelSerializer):
    class Meta:
        model = Domain
        fields = ('pk',)

    def to_representation(self, value):

        company_address = value.company.address if value.company else ''
        company_name = value.company.name if value.company else ''

        if value.use_custom_address:
            address = value.custom_company_address
        else:
            address = company_address

        ret = {
            "pk": value.pk,
            "name": value.name,
            "company_name": company_name,
            "use_custom_address": value.use_custom_address,
            "address": address
        }
        return ret

class ExecutorTaskSerializer(serializers.ModelSerializer):
    class Meta:
        model = Executor
        fields = ('pk', 'executor')

    def to_representation(self, value):
        ret = {
            "executor": value.executor.pk,
            "username": value.executor.username,
        }
        return ret

class TaskSerializer(serializers.ModelSerializer):
    domain = DomainTaskSerializer(required=False)
    status = StatusSerializer(required=False)
    code = CodeSerializer(required=False)
    executors = ExecutorTaskSerializer(required=False, many=True)
    notify = serializers.BooleanField(required=False)

    class Meta:
        model = Task
        fields = (
            "url", "pk", "title", "description", "domain", "status",
            "code", "executors", "notify"
        )

    def get_instance_from_pk(self, model, field):
        field_data = self.initial_data.get(field)
        if not field_data or not field_data.get('pk'):
            return None
        instance = model.objects.filter(pk=field_data.get('pk')).first()
        return instance or None

    def validate_code(self, obj):
        instance = self.get_instance_from_pk(Code, 'code')
        return instance or None

    def validate_status(self, obj):
        instance = self.get_instance_from_pk(Status, 'status')
        return instance or None

    def validate_domain(self, obj):
        instance = self.get_instance_from_pk(Domain, 'domain')
        return instance or None

    def validate_executors(self, obj):
        data = self.initial_data.get('executors', {})
        qs = User.objects.filter(pk__in=[x.get('pk') for x in data])
        return list(qs) or []

    def create(self, validated_data):
        executors = validated_data.pop('executors')
        notify = validated_data.pop('notify', False)

        instance = Task(**validated_data)
        instance.creator = self.context['request'].user
        instance.save()
        if executors:
            for executor in executors:
                Executor(task=instance, executor=executor).save()
        if notify:
            instance.notify_users()

        return instance

    def update(self, instance, validated_data):

        executors = validated_data.pop('executors')
        instance.executors.exclude(
            executor__pk__in=[x.pk for x in executors]
        ).delete()

        for executor in executors:
            instance.executors.get_or_create(executor=executor)

        for key in validated_data.keys():
            if key in ('executors', ):
                continue
            value = validated_data.get(key)
            try:
                if getattr(instance, key) != value:
                    setattr(instance, key, validated_data.get(key))
            except AttributeError:
                pass

        instance.save()
        notify = self.validated_data.get('notify', False)
        if notify:
            self.instance.notify_users()

        return instance


from io import BufferedReader
from argparse import FileType

from typed_argument_parser import TypedArgumentParser

class LnkParserArgumentParser(TypedArgumentParser):

    class Namespace:
        lnk_files: list[BufferedReader]
        system_encoding: str | None

    def __init__(self, *args, **kwargs):
        super().__init__(
            *args,
            **(
                dict(description='A parser for Shell Link (.LNK) files.') | kwargs
            )
        )

        self.add_argument(
            'lnk_files',
            help='The path of an LNK file to be parsed.',
            nargs='+',
            type=FileType('rb'),
            metavar='lnk_file'
        )

        self.add_argument(
            '--system-encoding',
            help=(
                'The default encoding on the system from which the LNK file originated. Defaults to that of the current'
                ' system.'
            )
        )


from datetime import timedelta

import django.forms as forms

from website.models.core_models import *

class OHSCreateForm(forms.ModelForm):
    class Meta:
        model = OfficeHourSession
        exclude = ['ohs_status', 'ohs_instructor']

    def __init__(self, *args, user, **kwargs):
        super().__init__(*args, **kwargs)
        self.instructor = Instructor.objects.get(instructor_user=user)

    def save(self, commit=True):
        self.instance.ohs_instructor = self.instructor
        return super().save(commit)

    def clean(self):
        self.cleaned_data['ohs_instructor'] = self.instructor
        cleaned_data = super().clean()
        self.__clean_times__(cleaned_data)
        self.__clean_registration__(cleaned_data)
        return cleaned_data

    def __clean_registration__(self, cleaned_data):
        class_obj = cleaned_data.get('ohs_class')
        if class_obj and self.instructor not in class_obj.get_instructors():
            self.add_error('ohs_instructor', 'You are not enrolled in this class as an instructor.')

    def __clean_times__(self, cleaned_data):
        start_time = cleaned_data.get('ohs_start_time')
        end_time = cleaned_data.get('ohs_end_time')
        if start_time and end_time:
            if end_time < start_time:
                self.add_error('ohs_end_time', 'The end time cannot be before the start time.')
            if start_time < timezone.now() - timedelta(minutes=15):
                self.add_error('ohs_start_time',
                               'The start time must occur at least 15 minutes before the current time.')
        else:
            if not start_time:
                self.add_error('ohs_start_time', 'The start time must be supplied.')
            if not end_time:
                self.add_error('ohs_end_time', 'The end time must be supplied.')


import subprocess
import time
import sys

addr1 = "www.itu.edu.tr"    #Türkiye
addr2 = "www.uni-lj.si"    #Slovenya
addr3 = "www.ut.ee"     #Estonya
addr4 = "www.tcd.ie"    #İrlanda
addr5 = "www.en.ru.is"  #İzlanda
addr6 = "www.uni.gl"    #Grönland
addr7 = "www.ualberta.ca"   #Kanada
addr8 = "www.otago.ac.nz"   #Yeni Zelanda
addr9 = "www.kyoto-u.ac.jp" #Japonya
addr10 = "www.harvard.edu"  #Abd

uni_list = [addr1, addr2, addr3, addr4, addr5, addr6, addr7, addr8, addr9, addr10]
results = {

}

timings = {

}

all_time = 0

def animation():
    animasyon = ". .. ..."
    while True:
        for karakter in animasyon:
            sys.stdout.write(karakter)
            sys.stdout.flush()
            time.sleep(0.5)
            sys.stdout.write('\b')
            sys.stdout.flush()

def commands():
    global all_time
    for i in range(len(uni_list)):
        current = uni_list[i]
        command = f"traceroute {current}"
        print(f"Çalıştırılan Komut: {command}")
        start_time = time.time()
        command_result = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
        end_time = time.time()
        total_time = start_time - end_time
        all_time += total_time
        print("\n\n\n\n")
        print(total_time)
        #print(command_result)
        results.update({current: command_result})
        i+=1

commands()
#print(results)
print(all_time)
#result = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)

#if the command is successfully executed
""" if (result.returncode == 0):
    print("Command execution successful.")
    print("Output:\n", result.stdout)
else:
    print("Couldnt execute command")
 """


# -*- coding: utf-8 -*-
# __author__ = 'corvin'

import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread('../data/contrast75.png', 0)
# flatten() 将数组变成一维
hist, bins = np.histogram(img.flatten(), 256, [0, 256])
# 计算累积分布图
cdf = hist.cumsum()

# 构建 Numpy 掩模数组， cdf 为原数组，当数组元素为 0 时，掩盖（计算时被忽略）
cdf_m = np.ma.masked_equal(cdf, 0)
cdf_m = (cdf_m, cdf_m.min()) * 255 / (cdf_m.max(), cdf_m.min())
# 对被掩盖的元素赋值，赋值为 0
cdf = np.ma.filled(cdf_m, 0).astype('uint8')
img2 = cdf[img]

# cv2.imshow("img2", img2)
# cv2.waitKey(0)

# flatten() 将数组变成一维
hist, bins = np.histogram(img2.flatten(), 256, [0, 256])
# 计算累积分布图
cdf = hist.cumsum()
cdf_normalized = cdf * hist.max() / cdf.max()

plt.plot(cdf_normalized, color='b')
plt.hist(img.flatten(), 256, [0, 256], color='r')
plt.xlim([0, 256])
plt.legend(('cdf', 'histogram'), loc='upper left')
plt.show()


''''
Crear una tupla con los meses del año, pide números al usuarios, si el número está entre 1
y la longitud máxima de la tupla, muestra el contenido de esa posición sino muestra un mensaje
de error. El programa termina cuando el usuario introduce un cero.
'''

meses = ("Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre")
opt = 1

while opt > 0:
    opt = int(input(f'''Ingresa un numero del 1 al {len(meses)}: '''))

    if opt >= 1 and opt <= int(len(meses)):
        print(meses[(opt-1)])
    else:
        print("Opcion no existe")


import numpy as np
import tensorflow as tf
from tensorflow import keras
from collections import defaultdict
import json
import pickle
import os
import time
import sys
import random
from tensorflow.keras.models import load_model
from scipy.stats import *
sys.path.append('..')
from sklearn.preprocessing import StandardScaler, Normalizer, LabelEncoder
from sklearn.decomposition import PCA
from sklearn.metrics import silhouette_score, davies_bouldin_score, pairwise_distances, calinski_harabasz_score
from sklearn.utils import check_X_y, _safe_indexing
from scipy.stats import wasserstein_distance, moment
from math import log, exp
import matplotlib.pyplot as plt
from augment import *
from computecomplexityfinal import *
import copy
import math

def complexityMargin(model, dataset, augment='standard', program_dir=None):

	'''
	Fuction to calculate margin summary measure on augmented data

	Parameters
	----------
	model : tf.keras.Model()
		The Keras model for which the complexity measure is to be computed
	dataset : tf.data.Dataset
		Dataset object from PGDL data loader
	augment : str, optional
		The type of augmentation to use ('standard', 'mixup', 'adverserial', 'adverserial+standard', 'mixup+standard')
	program_dir : str, optional
		The program directory to store and retrieve additional data

	Returns
	-------
	float
		complexity measure
	'''

	keras.backend.clear_session()

	np.random.seed(0)

	marginDistribution = {}
	C = CustomComplexityFinal(model, dataset, augment=augment, input_margin=False)
	for label in range(2, 3):
		marginDistribution[label], normwidth = C.computeMargins(top = label)

	score = 0

	def computeQuantiles(d):

		q1 = np.percentile(d, 25)
		q2 = np.percentile(d, 50)
		q3 = np.percentile(d, 75)

		iqr = q3 - q1

		if d[d < q1 - 1.5*iqr].size == 0:
			f_l = np.min(d)
		else:
			f_l = np.max(d[d < q1 - 1.5*iqr])

		if d[d > q3 + 1.5*iqr].size == 0:
			f_u = np.max(d)
		else:
			f_u = np.min(d[d > q3 + 1.5*iqr])

		ret = [f_l, q1, q2, q3, f_u]

		return np.array(ret)

	def computePercentiles(d):
		return np.array([np.percentile(d, p) for p in list(range(5, 95, 10))])

	def computeMoments(d):

		moments = [stats.moment(d, moment=ord)**(1/ord) for ord in range(1, 6)]

		moments[0] = np.mean(d)
		moments = np.nan_to_num(moments, nan=np.mean(moments))
		print(moments)
		return np.array(moments)

	for label in range(2, 3):
		for i, index in enumerate(list(marginDistribution[label].keys())):
			quantiles = np.mean(computeQuantiles(marginDistribution[label][index]))
			mean = np.nanmean(marginDistribution[label][index])
			score += mean/len(list(marginDistribution[label].keys()))

	return -score

def complexityNorm(model, dataset, program_dir=None):

	'''
	Function to calculate norm based complexity measures

	Parameters
	----------
	model : tf.keras.Model()
		The Keras model for which the complexity measure is to be computed
	dataset : tf.data.Dataset
		Dataset object from PGDL data loader
	program_dir : str, optional
		The program directory to store and retrieve additional data

	Returns
	-------
	float
		complexity measure
	'''

	C = CustomComplexity(model, dataset, metric='batch_variance', augment='standard')
	Norm = C.getNormComplexity(norm='fro')
	score = 0

	score += Norm
	params = np.sum([tf.keras.backend.count_params(p) for p in model.trainable_weights])
	print('Params:', params, model.layers[0].get_weights()[0].shape[-1])
	print('Final Score:', score, len(model.layers))

	return score

def complexityDB(model, dataset, program_dir=None, pool=True, use_pca = False, layer='initial', computeOver=400, batchSize=40):

	'''
	Function to calculate feature clustering based measures. Based on the sklearn implementation of DB Index.

	Parameters
	----------
	model : tf.keras.Model()
		The Keras model for which the complexity measure is to be computed
	dataset : tf.data.Dataset
		Dataset object from PGDL data loader
	program_dir : str, optional
		The program directory to store and retrieve additional data
	pool : bool, optional
		Whether to use max-pooling for dimensionality reduction, default True
	use_pca : bool, optional
		Whether to use PCA for dimensionality reduction, default False
	layer : str or int, optional
		Which layer to compute DB on. Either 'initial', for the first conv/pooling layer in the
		model, 'pre-penultimate' for the 3rd-from-last conv/pool layer, or an int indicating the
		layer. Defaults to 'initial'.

	Returns
	-------
	float
		complexity measure
	'''

	def check_number_of_labels(n_labels, n_samples):
	    """Check that number of labels are valid.
	    Parameters
	    ----------
	    n_labels : int
	        Number of labels
	    n_samples : int
	        Number of samples
	    """
	    if not 1 < n_labels < n_samples:
	        raise ValueError("Number of labels is %d. Valid values are 2 "
	                         "to n_samples - 1 (inclusive)" % n_labels)

	def db(X, labels):
		X, labels = check_X_y(X, labels)
		le = LabelEncoder()
		labels = le.fit_transform(labels)
		n_samples, _ = X.shape
		n_labels = len(le.classes_)
		check_number_of_labels(n_labels, n_samples)

		intra_dists = np.zeros(n_labels)
		centroids = np.zeros((n_labels, len(X[0])), dtype=float)
		for k in range(n_labels):
			cluster_k = _safe_indexing(X, labels == k)
			centroid = cluster_k.mean(axis=0)
			centroids[k] = centroid
			intra_dists[k] = np.average(pairwise_distances(
			    cluster_k, [centroid], metric='euclidean'))

		centroid_distances = pairwise_distances(centroids, metric='euclidean')

		if np.allclose(intra_dists, 0) or np.allclose(centroid_distances, 0):
			return 0.0

		centroid_distances[centroid_distances == 0] = np.inf
		combined_intra_dists = intra_dists[:, None] + intra_dists
		scores = np.max(combined_intra_dists / centroid_distances, axis=1)
		return np.mean(scores)

	tf.keras.backend.clear_session()
	db_score = {}
	C = CustomComplexityFinal(model, dataset, augment='mixup', computeOver=computeOver, batchSize=batchSize)
	it = iter(dataset.repeat(-1).batch(C.batchSize))
	batch=next(it)
	extractor = C.intermediateOutputs(batch=batch)
	if pool == True:
		max_pool = tf.keras.layers.MaxPooling2D(pool_size=(4, 4), strides=None, padding="valid", data_format=None)
	else:
		max_pool = tf.keras.layers.Lambda(lambda x: x + 0)
	layers = []

	layer_dict = {'initial': [0, 1, 2], 'pre-penultimate': [-3, -4, -5]}

	if isinstance(layer, str):
		for l in layer_dict[layer]:
			c = list(model.get_layer(index = l).get_config().keys())
			if 'strides' in c:
				layers.append(l)
			if len(layers) == 1:
				break
	else:
		for l in [layer]:
			c = list(model.get_layer(index = l).get_config().keys())
			if 'strides' in c:
				layers.append(l)
			if len(layers) == 1:
				break

	D = DataAugmentor(batchSize = C.batchSize)
	for l in layers:
		it = iter(dataset.repeat(-1).shuffle(5000, seed=1).batch(C.batchSize))
		for i in range(C.computeOver//C.batchSize):
			tf.keras.backend.clear_session()
			batch1 = next(it)
			# batch1 = (D.augment(batch1[0]), batch1[1])
			batch2 = next(it)
			# batch2 = (D.augment(batch2[0]), batch2[1])
			batch3 = next(it)
			# batch3 = (D.augment(batch3[0]), batch3[1])
			feature = np.concatenate((max_pool(extractor(batch1[0].numpy())[l]).numpy().reshape(batch1[0].shape[0], -1),
										max_pool(extractor(batch2[0].numpy())[l]).numpy().reshape(batch2[0].shape[0], -1),
										max_pool(extractor(batch3[0].numpy())[l]).numpy().reshape(batch3[0].shape[0], -1)), axis = 0)
			target = np.concatenate((batch1[1], batch2[1], batch3[1]), axis = 0)
			if use_pca == True:
				pca = PCA(n_components=25)
				feature = pca.fit_transform(feature)
			try:
				db_score[l] += db(feature, target)/(C.computeOver//C.batchSize)
			except Exception as e:
				db_score[l] = db(feature, target)/(C.computeOver//C.batchSize)

	score = np.mean(list(db_score.values()))

	return(score)

def complexityMixup(model, dataset, program_dir=None,
					computeOver=500, batchSize=50):

	'''
	Function to calculate label-wise Mixup based measure

	Parameters
	----------
	model : tf.keras.Model()
		The Keras model for which the complexity measure is to be computed
	dataset : tf.data.Dataset
		Dataset object from PGDL data loader
	program_dir : str, optional
		The program directory to store and retrieve additional data
	computeOver : int
		The number of samples over which to compute the complexity measure
	batchSize:
		The batch size

	Returns
	-------
	float
		complexity measure
	'''
	it = iter(dataset.repeat(-1).shuffle(5000, seed=1).batch(batchSize))
	batch = next(it)
	n_classes = 1+np.max(batch[1].numpy())
	batchSize = n_classes*10
	computeOver = batchSize*10
	tf.keras.backend.clear_session()
	it = iter(dataset.repeat(-1).batch(batchSize))
	N = computeOver//batchSize
	batches = [next(it) for i in range(N)]
	vr = []

	def intrapolateImages(img, alpha=0.5):
		temp = np.stack([img]*img.shape[0])
		tempT =  np.transpose(temp, axes = (1,0,2,3,4))
		ret = alpha*temp + (1-alpha)*tempT
		mask = np.triu_indices(img.shape[0], 1)
		return ret[mask]

	def choose(n, k):
	    """
	    A fast way to calculate binomial coefficients by Andrew Dalke (contrib).
	    """
	    if 0 <= k <= n:
	        ntok = 1
	        ktok = 1
	        for t in range(1, min(k, n - k) + 1):
	            ntok *= n
	            ktok *= t
	            n -= 1
	        return ntok // ktok
	    else:
	        return

	def veracityRatio(model, batches, label, version_loss=None, label_smoothing=0.1):
		ret = []
		lossObject = tf.keras.losses.SparseCategoricalCrossentropy(from_logits = True)
		for b in batches:
			img = b[0][b[1]==label]
			lbl = b[1][b[1]==label]
			int_img = intrapolateImages(img)
			int_lbl = np.stack([label]*int_img.shape[0])
			int_logits = model(int_img)
			if version_loss == 'log':
				logLikelihood = lossObject(int_lbl, int_logits)
				ret.append(logLikelihood)
			elif version_loss == 'cosine':
				int_preds = tf.nn.softmax(int_logits, axis = 1)
				target = tf.one_hot(int_lbl, int_preds.shape[-1]) * (1 - label_smoothing) + label_smoothing/2
				ret.append((tf.keras.losses.CosineSimilarity()(target, int_preds)+1)/2)
			elif version_loss == 'mse':
				int_preds = tf.nn.softmax(int_logits, axis = 1)
				target = tf.one_hot(int_lbl, int_preds.shape[-1]) #* (1 - label_smoothing) + label_smoothing/2
				ret.append(tf.keras.losses.MeanSquaredError()(target, int_preds))
			else:
				int_preds = tf.argmax(int_logits, axis=1)
				ret.append(np.sum(int_preds==label)/np.size(int_preds))
		return np.mean(ret)

	for l in range(n_classes):
		try:
			vr.append(veracityRatio(model, batches, l))
		except:
			pass

	return np.mean(vr)

def complexityManifoldMixup(model, dataset, program_dir=None,
							computeOver=1000, batchSize=50):

	'''
	Function to calculate Manifold Mixup based measure

	Parameters
	----------
	model : tf.keras.Model()
		The Keras model for which the complexity measure is to be computed
	dataset : tf.data.Dataset
		Dataset object from PGDL data loader
	program_dir : str, optional
		The program directory to store and retrieve additional data
	computeOver : int
		The number of samples over which to compute the complexity measure
	batchSize: int
		The batch size

	Returns
	-------
	float
		complexity measure
	'''

	it = iter(dataset.repeat(-1).batch(batchSize))
	N = computeOver//batchSize
	batches = [next(it) for i in range(N)]
	digress = []

	def intrapolateImages(img, alpha=0.5):
		temp = np.stack([img]*img.shape[0])
		tempT =  np.transpose(temp, axes = (1,0,2,3,4))
		ret = alpha*temp + (1-alpha)*tempT
		mask = np.triu_indices(img.shape[0], 1)
		return ret[mask]

	def multiplicativeNoise(img, std=2):
		return img*(tf.random.normal(mean=1., stddev=std, shape=img.shape, seed=1))

	def veracityRatio(model, batches, label, layer=0, version_loss=False):

		cloned_model = keras.models.clone_model(model, input_tensors=keras.Input(shape=(batches[0][0][0].shape)))
		cloned_model.set_weights(model.get_weights())
		if layer != 0:
			cloned_model.layers[layer-1].activation = keras.activations.linear
		for b in batches:
			ret = []
			img = b[0][b[1]==label]
			orig_logits = cloned_model(img)
			representation = cloned_model.layers[layer]._last_seen_input
			int_repr = intrapolateImages(representation)
			int_lbl = np.stack([label]*int_repr.shape[0])
			x = int_repr
			for i in range(layer, len(model.layers)):
				if i == layer and i != 0:
					x = keras.activations.relu(x)
					x = model.layers[i](x)
				else:
					x = model.layers[i](x)
			if version_loss:
				logLikelihood = lossObject(int_lbl, x)
				ret.append(logLikelihood/np.size(int_preds))
			else:
				int_preds = tf.argmax(x, axis=1)
				ret.append(np.sum(int_preds==label)/np.size(int_preds))
		return np.mean(ret)

	for l in range(10):
		try:
			digress.append(veracityRatio(model, batches, l, layer = 1))
		except:
			pass
	return np.mean(digress)

def complexityMixupSoft(model, dataset, program_dir=None,
					computeOver=1000, batchSize=100):

	'''
	Function to calculate Mixup based measure

	Parameters
	----------
	model : tf.keras.Model()
		The Keras model for which the complexity measure is to be computed
	dataset : tf.data.Dataset
		Dataset object from PGDL data loader
	program_dir : str, optional
		The program directory to store and retrieve additional data
	computeOver : int
		The number of samples over which to compute the complexity measure
	batchSize: int
		The batch size

	Returns
	-------
	float
		complexity measure
	'''

	it = iter(dataset.repeat(-1).shuffle(5000, seed=1).batch(batchSize))
	batch = next(it)
	n_classes = 1+np.max(batch[1].numpy())
	batchSize = n_classes*10
	computeOver = batchSize*10
	tf.keras.backend.clear_session()
	it = iter(dataset.repeat(-1).batch(batchSize))
	N = computeOver//batchSize
	batches = [next(it) for i in range(N)]
	np.random.seed(0)
	vr = []

	def intrapolateImages(img, lbl, alpha=0.5):
		indices = np.random.randint(0, img.shape[0], size = img.shape[0])
		img = img*alpha + img[indices]*(1-alpha)
		lbl = lbl*alpha + lbl[indices]*(1-alpha)
		return (tf.convert_to_tensor(img), tf.convert_to_tensor(lbl))

	def choose(n, k):
	    """
	    A fast way to calculate binomial coefficients by Andrew Dalke (contrib).
	    """
	    if 0 <= k <= n:
	        ntok = 1
	        ktok = 1
	        for t in range(1, min(k, n - k) + 1):
	            ntok *= n
	            ktok *= t
	            n -= 1
	        return ntok // ktok
	    else:
	        return

	def veracityRatio(model, batches, version_loss=None, label_smoothing=0.1):
		ret = []
		lossObject = tf.keras.losses.SparseCategoricalCrossentropy(from_logits = True)
		for b in batches:
			int_img, int_lbl = intrapolateImages(b[0].numpy(), b[1].numpy())
			int_logits = model(int_img)
			if version_loss == 'log':
				logLikelihood = lossObject(int_lbl, int_logits)
				ret.append(logLikelihood)
			elif version_loss == 'cosine':
				int_preds = tf.nn.softmax(int_logits, axis = 1)
				target = tf.one_hot(int_lbl, int_preds.shape[-1]) * (1 - label_smoothing) + label_smoothing/2
				ret.append((tf.keras.losses.CosineSimilarity()(target, int_preds)+1)/2)
			elif version_loss == 'mse':
				int_preds = tf.nn.softmax(int_logits, axis = 1)
				target = tf.one_hot(int_lbl, int_preds.shape[-1]) #* (1 - label_smoothing) + label_smoothing/2
				ret.append(tf.keras.losses.MeanSquaredError()(target, int_preds))
			else:
				int_preds = tf.argmax(int_logits, axis=1)
				ret.append(np.sum(int_preds==label)/np.size(int_preds))
		return np.mean(ret)

	vr.append(veracityRatio(model, batches, version_loss='log'))

	return np.mean(vr)


#!/usr/bin/python3
"""
Module contains class Base
"""

import json

class Base:
    """class contains
    Attributes:
        __nb_objects - a private attribute
    Methods:
        __init__(self, id=None)
        to_json_string(list_dictionaries)
        save_to_file(cls, list_objs)
        from_json_string(json_string)
        create(cls, **dictionary)
    """

    __nb_objects = 0

    def __init__(self, id=None):
        """initalizes class Base,
        sets id or increments class attribute and sets as id"""
        if id is not None:
            self.id = id
        else:
            Base.__nb_objects += 1
            self.id = Base.__nb_objects

    @staticmethod
    def to_json_string(list_dictionaries):
        """return the JSON string representation of list_dictionaries"""
        if list_dictionaries is None:
            return "[]"
        return json.dumps(list_dictionaries)

    @classmethod
    def save_to_file(cls, list_objs):
        """writes the JSON string representation of list_objs into a file"""
        objs = []
        if list_objs is not None:
            for o in list_objs:
                objs.append(cls.to_dictionary(o))
        filename = cls.__name__ + ".json"
        with open(filename, "w") as f:
            f.write(cls.to_json_string(objs))

    @staticmethod
    def from_json_string(json_string):
        """returns the list of the JSON string representation json_string"""
        if json_string is None:
            return []
        return json.loads(json_string)

    @classmethod
    def create(cls, **dictionary):
        """Returns an instance with all attributes already set"""
        if cls.__name__ == "Square":
            dummy = cls(1)
        if cls.__name__ == "Rectangle":
            dummy = cls(1, 1)
        dummy.update(**dictionary)
        return dummy


from django import forms
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm, PasswordChangeForm, PasswordResetForm, SetPasswordForm

from .models import User, Message, Friend, Good, Comment

# User signup auth
class SignupForm(UserCreationForm):
    """ユーザー登録用フォーム"""
    class Meta(UserCreationForm.Meta):
        model = User
        fields = ('userid', 'username', 'email', 'thumbnail', 'self_intro')
        widgets = {
            'userid': forms.TextInput(attrs={'class':'userid-create-form'}),
            'username': forms.TextInput(attrs={'class':'username-create-form'}),
            'email': forms.EmailInput(attrs={'class':'email-create-form'})
        }

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        for field in self.fields.values():
            field.widget.attrs['class'] = 'form-control'

    def clean_email(self):
        email = self.cleaned_data['email']
        # 仮登録で操作が停止している場合、再度登録するときに仮登録のデータを削除
        User.objects.filter(email=email, is_active=False).delete()
        return email

# User edit form
class UserEditForm(forms.ModelForm):
    """ユーザー情報編集用フォーム"""
    class Meta:
        model = User
        fields = ('username', 'email', 'thumbnail', 'self_intro')

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        for field in self.fields.values():
            field.widget.attrs['class'] = 'form-control'

# Password change
class MyPasswordChangeForm(PasswordChangeForm):
    """パスワード変更フォーム"""
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        for field in self.fields.values():
            field.widget.attrs['class'] = 'form-control'

# Password reset form
class MyPasswordResetForm(PasswordResetForm):
    """パスワード忘れた時のフォーム"""
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        for field in self.fields.values():
            field.widget.attrs['class'] = 'form-control'

class MySetPasswordForm(SetPasswordForm):
    """パスワード再設定用フォーム"""
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        for field in self.fields.values():
            field.widget.attrs['class'] = 'form-control'

# Emal change form
class EmailChangeForm(forms.ModelForm):
    class Meta:
        model = User
        fields = ('email',)

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        for field in self.fields.values():
            field.widget.attrs['class'] = 'form-control'

    def clean_email(self):
        email = self.cleaned_data['email']
        User.objects.filter(email=email, is_active=False).delete()
        return email

# Login form
class LoginForm(AuthenticationForm):
    def __init__(self, *args, **kargs):
        super().__init__(*args, **kargs)
        for field in self.fields.values():
            field.widget.attrs['class'] = 'form-control'
            field.widget.attrs['placeholder'] = field.label

# MessageForm
class MessageForm(forms.ModelForm):
    class Meta:
        model = Message
        fields = ('post_pic', 'content')
        widgets = {
            'content': forms.Textarea(attrs={'id':'comment-input','class':'post-form', 'placeholder':"コメントを入力", 'aria-label': "", 'aria-describedby':"basic-addon1"})
        }

# CommentForm
class CommentForm(forms.ModelForm):
    class Meta:
        model = Comment
        fields = ('reply',)
        widgets = {
            'reply': forms.Textarea(attrs={'id':'reply-input', 'class':'reply-form', 'placeholder':"コメントを入力"})
        }

class FriendForm(forms.ModelForm):
    class Meta:
        model = Friend
        fields = '__all__'

class GoodForm(forms.ModelForm):
    class Meta:
        model = Good
        fields = '__all__'

# search form
class SearchForm(forms.Form):
    search = forms.CharField(max_length=100)

# Post form
class PostForm(forms.Form):
    content = forms.CharField(max_length=150, widget=forms.Textarea)


from __future__ import absolute_import, division, print_function

# Standard library imports
import sys

# Third party imports
from qtpy.QtCore import Qt, Signal
from qtpy.QtWidgets import QHBoxLayout, QLabel, QProgressBar, QVBoxLayout

# Local imports
from anaconda_navigator.api.anaconda_api import AnacondaAPI
from anaconda_navigator.utils import constants as C
from anaconda_navigator.utils.styles import load_style_sheet
from anaconda_navigator.widgets import (ButtonNormal, ComboBoxBase,
                                        FrameTabContent, FrameTabFooter,
                                        FrameTabHeader, LabelBase,
                                        SpacerHorizontal, WidgetBase)
from anaconda_navigator.widgets.lists.apps import (ListItemApplication,
                                                   ListWidgetApplication)

# yapf: enable

# --- Custom widgets used with CSS styling
# -----------------------------------------------------------------------------
class ButtonHomeRefresh(ButtonNormal):
    """QFrame used for CSS styling refresh button inside the Home Tab."""

class ComboHomeEnvironment(ComboBoxBase):
    """Widget Used for CSS styling."""

class ButtonHomeChannels(ButtonNormal):
    """Widget Used for CSS styling."""

class LabelHome(LabelBase):
    """QLabel used for CSS styling the Home Tab label."""

# --- Main widget
# -----------------------------------------------------------------------------
class HomeTab(WidgetBase):
    """Home applications tab."""
    # name, prefix, sender
    sig_item_selected = Signal(object, object, object)

    # button_widget, sender
    sig_channels_requested = Signal(object, object)

    # application_name, command, prefix, leave_path_alone, sender
    sig_launch_action_requested = Signal(object, object, bool, object, object)

    # action, application_name, version, sender
    sig_conda_action_requested = Signal(object, object, object, object)

    # url
    sig_url_clicked = Signal(object)

    # TODO: Connect these signals to have more granularity
    # [{'name': package_name, 'version': version}...], sender
    sig_install_action_requested = Signal(object, object)
    sig_remove_action_requested = Signal(object, object)

    def __init__(self, parent=None):
        """Home applications tab."""
        super(HomeTab, self).__init__(parent)

        # Variables
        self._parent = parent
        self.api = AnacondaAPI()
        self.applications = None
        self.style_sheet = None
        self.app_timers = None
        self.current_prefix = None

        # Widgets
        self.list = ListWidgetApplication()
        self.button_channels = ButtonHomeChannels('Channels')
        self.button_refresh = ButtonHomeRefresh('Refresh')
        self.combo = ComboHomeEnvironment()
        self.frame_top = FrameTabHeader(self)
        self.frame_body = FrameTabContent(self)
        self.frame_bottom = FrameTabFooter(self)
        self.label_home = LabelHome('Applications on')
        self.label_status_action = QLabel('')
        self.label_status = QLabel('')
        self.progress_bar = QProgressBar()
        self.first_widget = self.combo

        # Widget setup
        self.setObjectName('Tab')
        self.progress_bar.setTextVisible(False)
        self.list.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)

        # Layout
        layout_top = QHBoxLayout()
        layout_top.addWidget(self.label_home)
        layout_top.addWidget(SpacerHorizontal())
        layout_top.addWidget(self.combo)
        layout_top.addWidget(SpacerHorizontal())
        layout_top.addWidget(self.button_channels)
        layout_top.addWidget(SpacerHorizontal())
        layout_top.addStretch()
        layout_top.addWidget(self.button_refresh)
        self.frame_top.setLayout(layout_top)

        layout_body = QVBoxLayout()
        layout_body.addWidget(self.list)
        self.frame_body.setLayout(layout_body)

        layout_bottom = QHBoxLayout()
        layout_bottom.addWidget(self.label_status_action)
        layout_bottom.addWidget(SpacerHorizontal())
        layout_bottom.addWidget(self.label_status)
        layout_bottom.addStretch()
        layout_bottom.addWidget(self.progress_bar)
        self.frame_bottom.setLayout(layout_bottom)

        layout = QVBoxLayout()
        layout.addWidget(self.frame_top)
        layout.addWidget(self.frame_body)
        layout.addWidget(self.frame_bottom)
        self.setLayout(layout)

        # Signals
        self.list.sig_conda_action_requested.connect(
            self.sig_conda_action_requested
        )
        self.list.sig_url_clicked.connect(self.sig_url_clicked)
        self.list.sig_launch_action_requested.connect(
            self.sig_launch_action_requested
        )
        self.button_channels.clicked.connect(self.show_channels)
        self.button_refresh.clicked.connect(self.refresh_cards)
        self.progress_bar.setVisible(False)

    # --- Setup methods
    # -------------------------------------------------------------------------
    def setup(self, conda_data):
        """Setup the tab content."""
        conda_processed_info = conda_data.get('processed_info')
        environments = conda_processed_info.get('__environments')
        applications = conda_data.get('applications')
        self.current_prefix = conda_processed_info.get('default_prefix')
        self.set_environments(environments)
        self.set_applications(applications)

    def set_environments(self, environments):
        """Setup the environments list."""
        # Disconnect to avoid triggering the signal when updating the content
        try:
            self.combo.currentIndexChanged.disconnect()
        except TypeError:
            pass

        self.combo.clear()
        for i, (env_prefix, env_name) in enumerate(environments.items()):
            self.combo.addItem(env_name, env_prefix)
            self.combo.setItemData(i, env_prefix, Qt.ToolTipRole)

        index = 0
        for i, (env_prefix, env_name) in enumerate(environments.items()):
            if self.current_prefix == env_prefix:
                index = i
                break

        self.combo.setCurrentIndex(index)
        self.combo.currentIndexChanged.connect(self._item_selected)

    def set_applications(self, applications):
        """Build the list of applications present in the current conda env."""
        apps = self.api.process_apps(applications, prefix=self.current_prefix)
        all_applications = []
        installed_applications = []
        not_installed_applications = []

        # Check if some installed applications are not on the apps dict
        # for example when the channel was removed.
        linked_apps = self.api.conda_linked_apps_info(self.current_prefix)
        missing_apps = [app for app in linked_apps if app not in apps]
        for app in missing_apps:
            apps[app] = linked_apps[app]

        for app_name in sorted(list(apps.keys())):
            app = apps[app_name]
            item = ListItemApplication(
                name=app['name'],
                description=app['description'],
                versions=app['versions'],
                command=app['command'],
                image_path=app['image_path'],
                prefix=self.current_prefix,
                needs_license=app.get('needs_license', False)
            )
            if item.installed:
                installed_applications.append(item)
            else:
                not_installed_applications.append(item)

        all_applications = installed_applications + not_installed_applications

        self.list.clear()
        for i in all_applications:
            self.list.addItem(i)
        self.list.update_style_sheet(self.style_sheet)

        self.set_widgets_enabled(True)
        self.update_status()

    # --- Other methods
    # -------------------------------------------------------------------------
    def current_environment(self):
        """Return the current selected environment."""
        env_name = self.combo.currentText()
        return self.api.conda_get_prefix_envname(env_name)

    def refresh_cards(self):
        """Refresh application widgets.

        List widget items sometimes are hidden on resize. This method tries
        to compensate for that refreshing and repainting on user demand.
        """
        self.list.update_style_sheet(self.style_sheet)
        self.list.repaint()
        for item in self.list.items():
            if not item.widget.isVisible():
                item.widget.repaint()

    def show_channels(self):
        """Emit signal requesting the channels dialog editor."""
        self.sig_channels_requested.emit(self.button_channels, C.TAB_HOME)

    def update_list(self, name=None, version=None):
        """Update applications list."""
        self.set_applications()
        self.label_status.setVisible(False)
        self.label_status_action.setVisible(False)
        self.progress_bar.setVisible(False)

    def update_versions(self, apps=None):
        """Update applications versions."""
        self.items = []

        for i in range(self.list.count()):
            item = self.list.item(i)
            self.items.append(item)
            if isinstance(item, ListItemApplication):
                name = item.name
                meta = apps.get(name)
                if meta:
                    versions = meta['versions']
                    version = self.api.get_dev_tool_version(item.path)
                    item.update_versions(version, versions)

    # --- Common Helpers (# FIXME: factor out to common base widget)
    # -------------------------------------------------------------------------
    def _item_selected(self, index):
        """Notify that the item in combo (environment) changed."""
        name = self.combo.itemText(index)
        prefix = self.combo.itemData(index)
        self.sig_item_selected.emit(name, prefix, C.TAB_HOME)

    @property
    def last_widget(self):
        """Return the last element of the list to be used in tab ordering."""
        if self.list.items():
            return self.list.items()[-1].widget

    def ordered_widgets(self, next_widget=None):
        """Return a list of the ordered widgets."""
        ordered_widgets = [
            self.combo,
            self.button_channels,
            self.button_refresh,
        ]
        ordered_widgets += self.list.ordered_widgets()

        return ordered_widgets

    def set_widgets_enabled(self, value):
        """Enable or disable widgets."""
        self.combo.setEnabled(value)
        self.button_channels.setEnabled(value)
        self.button_refresh.setEnabled(value)
        for item in self.list.items():
            item.button_install.setEnabled(value)
            item.button_options.setEnabled(value)

            if value:
                item.set_loading(not value)

    def update_items(self):
        """Update status of items in list."""
        if self.list:
            for item in self.list.items():
                item.update_status()

    def update_status(self, action='', message='', value=None, max_value=None):
        """Update the application action status."""
        self.label_status_action.setText(action)
        self.label_status.setText(message)

        if max_value is None and value is None:
            self.progress_bar.setVisible(False)
        else:
            self.progress_bar.setVisible(True)
            self.progress_bar.setMaximum(max_value)
            self.progress_bar.setValue(value)

    def update_style_sheet(self, style_sheet=None):
        """Update custom CSS style sheet."""
        if style_sheet is None:
            self.style_sheet = load_style_sheet()
        else:
            self.style_sheet = style_sheet

        self.list.update_style_sheet(style_sheet=self.style_sheet)
        self.setStyleSheet(self.style_sheet)

# --- Local testing
# -----------------------------------------------------------------------------
def local_test():  # pragma: no cover
    """Run local test."""
    from anaconda_navigator.utils.qthelpers import qapplication

    app = qapplication()
    widget = HomeTab()
    apps = {}
    for i in range(6):
        name = "Package {0}".format(i)
        application = dict(
            name=name,
            prefix='boom',
            description="Scientific PYthon Development EnviRonment",
            versions=[str(i), str(i + 1), str(i + 2)],
            command=None,
            image_path=None
        )
        apps[name] = application
    widget.setup(apps)
    widget.update_style_sheet()
    widget.showMaximized()
    sys.exit(app.exec_())

if __name__ == "__main__":  # pragma: no cover
    local_test()


import logging
import fsspec
import geopandas as gpd
from shapely.geometry import Polygon
from pyproj import Transformer

logging.getLogger('fiona').setLevel(logging.WARNING)

class Sentinel2Grid:

    def __init__(self):
        gpd.io.file.fiona.drvsupport.supported_drivers['KML'] = 'r'
        kml_file = 'simplecache::https://sentinel.esa.int/documents/247904/1955685/S2A_OPER_GIP_TILPAR_MPC__20151209T095117_V20150622T000000_21000101T000000_B00.kml'
        # commented because url was not reachable
        with fsspec.open(kml_file) as file:
            self.tiles_df = gpd.read_file(file, driver='KML')

    def get_codes(self, bbox, crs):

        coord_transformer = Transformer.from_crs(crs, "EPSG:4326", always_xy=True)
        coord_lower_left = coord_transformer.transform(bbox[1], bbox[0])
        coord_upper_right = coord_transformer.transform(bbox[3], bbox[2])
        lat_range = sorted([coord_lower_left[0], coord_upper_right[0]])
        lon_range = sorted([coord_lower_left[1], coord_upper_right[1]])

        polygon = Polygon([
            (lon_range[0], lat_range[0]),
            (lon_range[0], lat_range[1]),
            (lon_range[1], lat_range[1]),
            (lon_range[1], lat_range[0]),
            (lon_range[0], lat_range[0])
        ])

        #Too slow
        idx = self.tiles_df[self.tiles_df.intersects(polygon)]

        return idx['Name'].array


import uuid
from datetime import date, timedelta
from io import BytesIO

from django.conf import settings
from django.db import models
from django.template.loader import get_template
from django.utils import translation
from django.utils.text import slugify
from django.utils.timezone import now
from payments import PurchasedItem
from payments.models import BasePayment, PaymentStatus
from plans.contrib import get_user_language
from plans.models import Invoice, Order
from plans.signals import order_completed
from xhtml2pdf import pisa

from openwisp_users.mixins import OrgMixin

from . import settings as app_settings
from .mail import send_template_email

class Payment(BasePayment, OrgMixin):
    id = models.UUIDField(primary_key=True,
                          default=uuid.uuid4,
                          editable=False)
    variant = models.CharField(max_length=255,
                               default='default',
                               choices=app_settings.PAYMENT_VARIANT_CHOICES)
    order = models.ForeignKey('plans.Order',
                              on_delete=models.CASCADE)
    is_renewal = models.BooleanField(default=False)

    class Meta:
        app_label = 'plans'

    @property
    def plan_slug(self):
        if not self.order:
            return None
        return slugify(self.order.plan.name)

    @classmethod
    def payment_status_changed(cls, instance, **kwargs):
        if instance.status == PaymentStatus.CONFIRMED:
            user = instance.order.user
            days = instance.order.pricing.period
            user.userplan.expire = date.today() + timedelta(days=days)
            user.userplan.active = True
            user.userplan.save()
            # complete order (skip django-plans logic)
            order = instance.order
            order.completed = now()
            order.status = Order.STATUS.COMPLETED
            order.full_clean()
            order.save()
            order_completed.send(order)
            org = instance.organization
            from .utils import (get_or_create_paid_plan_radius_group,
                                get_or_create_temporary_radius_group)
            paid_group = get_or_create_paid_plan_radius_group(org, user.userplan.plan)
            temp_group = get_or_create_temporary_radius_group(org)
            RadiusUserGroup = user.radiususergroup_set.model
            try:
                rug = user.radiususergroup_set.get(group=temp_group)
            except RadiusUserGroup.DoesNotExist:
                user.radiususergroup_set.all().delete()
                RadiusUserGroup.objects.create(user=user, group=paid_group)
            else:
                rug.group = paid_group
                rug.full_clean()
                rug.save()

    def get_success_url(self):
        if not self.is_renewal:
            return app_settings.PAYMENT_SUCCESS_URL
        else:
            return app_settings.PAYMENT_RENEW_SUCESS_URL

    def get_failure_url(self):
        return app_settings.PAYMENT_FAILURE_URL

    def get_purchased_items(self):
        order = self.order
        yield PurchasedItem(name=str(order.plan),
                            sku=str(order.plan.id),
                            quantity=1,
                            price=order.amount,
                            currency=order.currency)

def send_invoice_by_email(self):
    if self.type != self.INVOICE_TYPES.INVOICE:
        return
    language_code = get_user_language(self.user)
    if language_code is not None:
        translation.activate(language_code)
    mail_context = {'user': self.user,
                    'invoice_type': self.get_type_display(),
                    'invoice_number': self.get_full_number(),
                    'order': self.order.id}
    if language_code is not None:
        translation.deactivate()
    attachments = [
        [self.get_invoice_pdf_filename(),
         self.generate_invoice_pdf().getvalue(),
         'application/pdf']
    ]
    send_template_email([self.user.email],
                        'mail/invoice_created_title.txt',
                        'mail/invoice_created_body.txt',
                        mail_context,
                        language_code,
                        attachments=attachments)

def get_invoice_pdf_filename(self):
    return 'invoice-{}.pdf'.format(self.full_number.replace('/', '-'))

def generate_invoice_pdf(self):
    template_path = 'invoice.html'
    context = {
        'logo_url': getattr(settings, 'PLANS_INVOICE_LOGO_URL', None),
        'invoice': self,
        'pdf': True
    }
    template = get_template(template_path)
    html = template.render(context)
    stream = BytesIO()
    pisa.CreatePDF(html, dest=stream)
    return stream

Invoice.send_invoice_by_email = send_invoice_by_email
Invoice.generate_invoice_pdf = generate_invoice_pdf
Invoice.get_invoice_pdf_filename = get_invoice_pdf_filename


import json
import os

import numpy as np
import torch
import torchvision

import colorama
colorama.init()

def read_data(train_data_dir, test_data_dir):
    '''parses data in given train and test data directories

    assumes:
    - the data in the input directories are .json files with
        keys 'users' and 'user_data'
    - the set of train set users is the same as the set of test set users

    Return:
        clients: list of non-unique client ids
        groups: list of group ids; empty list if none found
        train_data: dictionary of train data
        test_data: dictionary of test data
    '''
    clients = []
    groups = []
    train_data = {}
    test_data = {}

    train_files = os.listdir(train_data_dir)
    train_files = [f for f in train_files if f.endswith('.json')]
    for f in train_files:
        file_path = os.path.join(train_data_dir, f)
        with open(file_path, 'r') as inf:
            cdata = json.load(inf)
        clients.extend(cdata['users'])
        if 'hierarchies' in cdata:
            groups.extend(cdata['hierarchies'])
        train_data.update(cdata['user_data'])

    test_files = os.listdir(test_data_dir)
    test_files = [f for f in test_files if f.endswith('.json')]
    for f in test_files:
        file_path = os.path.join(test_data_dir, f)
        with open(file_path, 'r') as inf:
            cdata = json.load(inf)
        test_data.update(cdata['user_data'])

    clients = sorted(cdata['users'])

    return clients, groups, train_data, test_data

def load_mnist():
    np.random.seed(0)
    torch.manual_seed(10)
    train_data = torchvision.datasets.MNIST(root='../../data', train=True, download=True)
    test_data = torchvision.datasets.MNIST(root='../../data', train=False)

    train_data_local_dict = [dict({'x':[], 'y':[]}), dict({'x':[], 'y':[]}), dict({'x':[], 'y':[]}), dict({'x':[], 'y':[]}), dict({'x':[], 'y':[]})]
    test_data_local_dict = [dict({'x':[], 'y': []}), dict({'x':[], 'y':[]}), dict({'x':[], 'y':[]}), dict({'x':[], 'y':[]}), dict({'x':[], 'y':[]})]
    # test_data_global_dict = [dict({'x':[], 'y':[]})]
    for i in range(60000):
        if train_data[i][1] == 0 or train_data[i][1] == 1:
            train_data_local_dict[0]['x'].append(np.array(train_data[i][0]))
            train_data_local_dict[0]['y'].append(train_data[i][1])
        elif train_data[i][1] == 2 or train_data[i][1] == 3:
            train_data_local_dict[1]['x'].append(np.array(train_data[i][0]))
            train_data_local_dict[1]['y'].append(train_data[i][1])
        elif train_data[i][1] == 4 or train_data[i][1] == 5:
            train_data_local_dict[2]['x'].append(np.array(train_data[i][0]))
            train_data_local_dict[2]['y'].append(train_data[i][1])
        elif train_data[i][1] == 6 or train_data[i][1] == 7:
            train_data_local_dict[3]['x'].append(np.array(train_data[i][0]))
            train_data_local_dict[3]['y'].append(train_data[i][1])
        elif train_data[i][1] == 8 or train_data[i][1] == 9:
            train_data_local_dict[4]['x'].append(np.array(train_data[i][0]))
            train_data_local_dict[4]['y'].append(train_data[i][1])
    for i in range(10000):
        # test_data_global_dict[0]['x'].append(np.array(test_data[i][0]))
        # test_data_global_dict[0]['y'].append(test_data[i][1])

        if test_data[i][1] == 0 or test_data[i][1] == 1:
            test_data_local_dict[0]['x'].append(np.array(test_data[i][0]))
            test_data_local_dict[0]['y'].append(test_data[i][1])
        elif test_data[i][1] == 2 or test_data[i][1] == 3:
            test_data_local_dict[1]['x'].append(np.array(test_data[i][0]))
            test_data_local_dict[1]['y'].append(test_data[i][1])
        elif test_data[i][1] == 4 or test_data[i][1] == 5:
            test_data_local_dict[2]['x'].append(np.array(test_data[i][0]))
            test_data_local_dict[2]['y'].append(test_data[i][1])
        elif test_data[i][1] == 6 or test_data[i][1] == 7:
            test_data_local_dict[3]['x'].append(np.array(test_data[i][0]))
            test_data_local_dict[3]['y'].append(test_data[i][1])
        elif test_data[i][1] == 8 or test_data[i][1] == 9:
            test_data_local_dict[4]['x'].append(np.array(test_data[i][0]))
            test_data_local_dict[4]['y'].append(test_data[i][1])

    return train_data_local_dict, test_data_local_dict

def batch_data(data, batch_size):
    '''
    data is a dict := {'x': [numpy array], 'y': [numpy array]} (on one client)
    returns x, y, which are both numpy array of length: batch_size
    '''
    data_x = data['x']
    data_y = data['y']

    # randomly shuffle data
    np.random.seed(100)
    rng_state = np.random.get_state()
    np.random.shuffle(data_x)
    np.random.set_state(rng_state)
    np.random.shuffle(data_y)

    # loop through mini-batches
    batch_data = list()
    for i in range(0, len(data_x), batch_size):
        batched_x = data_x[i:i + batch_size]
        batched_y = data_y[i:i + batch_size]
        batched_x = torch.from_numpy(np.asarray(batched_x)).float()
        batched_y = torch.from_numpy(np.asarray(batched_y)).long()
        batch_data.append((batched_x, batched_y))
    return batch_data

def load_partition_data_mnist_by_device_id(batch_size,
                                           device_id,
                                           train_path="MNIST_mobile",
                                           test_path="MNIST_mobile"):
    train_path += '/' + device_id + '/' + 'train'
    test_path += '/' + device_id + '/' + 'test'
    return load_partition_data_mnist(batch_size)

def load_partition_data_mnist(batch_size):
    print(colorama.Fore.LIGHTGREEN_EX + "start load_partition_data_mnist in data_loader.py" + colorama.Style.RESET_ALL)

    # check file directory
    # print('current directory:', os.getcwd())
    # users, groups, train_data, test_data = read_data(train_path, test_path)
    #
    # if len(groups) == 0:
    #     groups = [None for _ in users]

    train_data, test_data = load_mnist()

    train_data_num = 0
    test_data_num = 0
    train_data_local_dict = dict()
    test_data_local_dict = dict()
    train_data_local_num_dict = dict()
    train_data_global = list()
    test_data_global = list()
    client_idx = 0
    for i in range(5):
        user_train_data_num = len(train_data[i]['x'])
        user_test_data_num = len(test_data[i]['x'])
        train_data_num += user_train_data_num
        test_data_num += user_test_data_num
        train_data_local_num_dict[client_idx] = user_train_data_num

        # transform to batches
        train_batch = batch_data(train_data[i], batch_size)
        test_batch = batch_data(test_data[i], batch_size)

        # each client takes data from train_data_local_dict[distribution]
        # distribution = {'0': (0, 1)} / {'1': (2, 3)} / {'2': (4, 5)}
        #                                {'3': (6, 7)} / {'4': (8, 9)}
        # batch_data from ==>
        # for (x, label) in enumerate(train_data_local_dict[distribution][batch]):

        # index using client index
        train_data_local_dict[client_idx] = train_batch
        test_data_local_dict[client_idx] = test_batch
        train_data_global += train_batch
        test_data_global += test_batch
        client_idx += 1
    client_num = client_idx
    class_num = 10

    print(colorama.Fore.LIGHTGREEN_EX + "finish load_partition_data_mnist in data_loader.py" + colorama.Style.RESET_ALL)

    return client_num, train_data_num, test_data_num, train_data_global, test_data_global, \
           train_data_local_num_dict, train_data_local_dict, test_data_local_dict, class_num


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

"""
    ***************************************************************************
    * Plugin name:   SgmHunting
    * Plugin type:   QGIS 3 plugin
    * Module:        Main
    * Description:   Main class
    * Specific lib:  None
    * First release: 2023-09-01
    * Last release:  2023-09-07
    * Copyright:     (C)2023 SIGMOE
    * Email:         em at sigmoe.fr
    * License:       GPL
    ***************************************************************************

    ***************************************************************************
    * This program is free software: you can redistribute it and/or modify
    * it under the terms of the GNU General Public License as published by
    * the Free Software Foundation, either version 3 of the License, or
    * (at your option) any later version.
    ***************************************************************************
"""

from qgis.PyQt.QtCore import  QCoreApplication
from qgis.PyQt.QtWidgets import QAction, QMenu
from qgis.PyQt.QtGui import QIcon
from qgis.core import QgsProject
from qgis.utils import iface

import os.path

from .sgm_hunting_globalvars import *
from .sgm_hunting_prepaparclot import PrepaParcLot
from .sgm_hunting_crerapport import ExportXl
from .sgm_hunting_conf import Conf

# Imports resources (for icons)
from . import sgm_hunting_rc

class SgmHunting:

    # Initialization
    def __init__(self, iface):
        self.iface = iface
        self.canvas = self.iface.mapCanvas()
        self.project = QgsProject.instance()
        self.selectiae_window = None

        # Initializes plugin directory
        self.plugin_dir = os.path.dirname(__file__)

    def initGui(self):
        '''
            Initialization of menu and toolbar
        '''
        # Adds specific menu to QGIS menu
        self.sgm_menu = QMenu(QCoreApplication.translate("Hunting", mnu_title_txt))
        self.iface.mainWindow().menuBar().insertMenu(self.iface.firstRightStandardMenu().menuAction(), self.sgm_menu)

        # Adds specific toolbar
        self.sgm_tb = self.iface.addToolBar(mnu_title_txt)
        self.sgm_tb.setObjectName("HuntingToolBar")

        # Creates actions
        self.action_prepaparclot = QAction(
            QIcon(r":sgm_hunting_prepaparclot"), mnu_fnc1_txt, self.iface.mainWindow())
        self.action_crerapport = QAction(
            QIcon(r":sgm_hunting_crerapport"), mnu_fnc2_txt, self.iface.mainWindow())
        self.action_conf = QAction(
            QIcon(r":sgm_hunting_conf"), mnu_fnc3_txt, self.iface.mainWindow())

        # Adds actions to the toolbar
        self.sgm_tb.addActions([self.action_prepaparclot,
                                self.action_crerapport,
                                self.action_conf
                                ])

        # Adds actions to the menu
        self.sgm_menu.addActions([  self.action_prepaparclot,
                                    self.action_crerapport,
                                    self.action_conf
                                    ])

        # Manages signals
        self.action_prepaparclot.triggered.connect(self.hunting_prepaparclot)
        self.action_crerapport.triggered.connect(self.hunting_crerapport)
        self.action_conf.triggered.connect(self.hunting_conf)

        # Gets the config
        self.huntingconf = Conf(self.iface, self.canvas, self.project, self.plugin_dir)

    def unload(self):
        '''
            Removes the plugin menu item and icon from QGIS GUI
        '''
        if self.sgm_menu != None:
            self.iface.mainWindow().menuBar().removeAction(self.sgm_menu.menuAction())
            self.sgm_menu.deleteLater()
            self.iface.mainWindow().removeToolBar(self.sgm_tb)
        else:
            self.iface.removePluginMenu("&Hunting", self.sgm_menu.menuAction())
            self.sgm_menu.deleteLater()

    def hunting_prepaparclot(self):
        '''
            Creates new layers to manage hunting lots
        '''
        self.prepaparclot = PrepaParcLot(self.iface, self.canvas, self.project, self.plugin_dir, self.huntingconf.old_params)
        self.prepaparclot.launch()

    def hunting_crerapport(self):
        '''
            Creates new report (Excel export)
        '''
        self.export_xl_rapport = ExportXl(self.iface, self.canvas, self.project, self.plugin_dir, self.huntingconf.old_params)
        self.export_xl_rapport.launch()

    def hunting_conf(self):
        '''
            Configuration
        '''
        self.huntingconf.reconf()