from django.shortcuts import render,redirect,get_object_or_404
from django.contrib.sites.shortcuts import get_current_site
from django.contrib.auth.models import User,Permission
from django.utils.encoding import force_bytes,force_text
from django.utils.http import urlsafe_base64_encode,urlsafe_base64_decode
from django.template.loader import render_to_string
from django.contrib.auth import login
from django.views.generic import ListView, DetailView
from django import forms
from django_filters.views import FilterView
from django_tables2.views import SingleTableMixin
from .tables import CategoriaTable,PerfilTable,GastoTable
from .filters import CategoriaFilter,PerfilFilter,GastoFilter
from .models import Categorias,Perfiles,Gastos
from .forms import GastoForm,CategoriaForm,PerfilForm,SignUpForm
from .tokens import account_activation_token
from django.core.mail import send_mail

from .functions import obtenerPerfiles,formatear_informacion_del_perfil,envia_email_confirmacion,obtener_gasto_mas_caro,obtener_gasto_mas_barato,obtener_gasto_total_y_mensual

def index(request):
    p = obtenerPerfiles(request.user)
    perfiles = None
    if p != None:
        perfiles = formatear_informacion_del_perfil(p)
    return render(request, 'mainGastos/index.html',{'perfiles':perfiles})

def cuenta_activada(request):
    return render(request, 'registration/account_activation_email_success.html')

#CRUD de categorías
def categoria_list(request):
    table = CategoriaTable(Categorias.objects.all().filter(usuario=request.user))
    mensajeNoCategorias = False
    notTheOwner = False

    if not Categorias.objects.filter(usuario=request.user):
        mensajeNoCategorias = True
    return render(request, "crud/categoria/listar_categoria.html", {
        "table": table,
        "mensajeNoCategorias":mensajeNoCategorias
    })

def categoria_view(request, pk, template_name='crud/categoria/detalle_categoria.html'):
    categoria = get_object_or_404(Categorias, pk=pk)
    return render(request, template_name, {'object':categoria})

def categoria_create(request, template_name='crud/categoria/categoria_create_form.html'):
    form = CategoriaForm(request.POST or None)
    if form.is_valid():
        f = form.save(commit=False)
        f.usuario = request.user
        f.save()
        return redirect('listar_categoria')
    return render(request, template_name, {'form':form})

def categoria_update(request, pk, template_name='crud/categoria/categoria_form.html'):
    categoria= get_object_or_404(Categorias, pk=pk)
    form = CategoriaForm(request.POST or None, instance=categoria)
    isOwner = False
    userOfCategory = Categorias.objects.get(id=pk).usuario
    if (request.user == userOfCategory):
        isOwner = True
    if form.is_valid():
        form.save()
        return redirect('listar_categoria')
    return render(request, template_name, {'form':form,'isOwner':isOwner})

def categoria_delete(request, pk, template_name='crud/categoria/categoria_confirm_delete.html'):
    categoria= get_object_or_404(Categorias, pk=pk)
    isOwner = False
    userOfCategory = Categorias.objects.get(id=pk).usuario
    if (request.user == userOfCategory):
        isOwner = True
    if request.method=='POST':
        categoria.delete()
        return redirect('listar_categoria')
    return render(request, template_name, {'object':categoria,'isOwner':isOwner})

#CRUD de perfiles

def perfil_list(request):
    table = PerfilTable(Perfiles.objects.all().filter(usuario=request.user))
    mensajeNoPerfiles = False

    if not Perfiles.objects.filter(usuario=request.user):
        mensajeNoPerfiles = True
    return render(request, "crud/perfil/listar_perfil.html", {
        "table": table,
        "mensajeNoPerfiles":mensajeNoPerfiles
    })

    return render(request, "crud/perfil/listar_perfil.html", {
        "table": table
    })

def perfil_view(request, pk, template_name='crud/perfil/detalle_perfil.html'):
    perfil = get_object_or_404(Perfiles, pk=pk)
    return render(request, template_name, {'object':perfil})

def perfil_create(request, template_name='crud/perfil/perfil_create_form.html'):
    form = PerfilForm(request.POST or None)
    if form.is_valid():
        f = form.save(commit=False)
        f.usuario = request.user
        f.save()
        return redirect('listar_perfil')
    return render(request, template_name, {'form':form})

def perfil_update(request, pk, template_name='crud/perfil/perfil_form.html'):
    perfil= get_object_or_404(Perfiles, pk=pk)
    form = PerfilForm(request.POST or None, instance=perfil)
    isOwner = False
    userOfProfile = Perfiles.objects.get(id=pk).usuario
    if (request.user == userOfProfile):
        isOwner = True
    if form.is_valid():
        form.save()
        return redirect('listar_perfil')
    return render(request, template_name, {'form':form,'isOwner':isOwner})

def perfil_delete(request, pk, template_name='crud/perfil/perfil_confirm_delete.html'):
    perfil= get_object_or_404(Perfiles, pk=pk)
    isOwner = False
    userOfProfile = Perfiles.objects.get(id=pk).usuario
    if (request.user == userOfProfile):
        isOwner = True
    if request.method=='POST':
        perfil.delete()
        return redirect('listar_perfil')
    return render(request, template_name, {'object':perfil,'isOwner':isOwner})

#CRUD de gastos

def gasto_list(request,plk):
    table = GastoTable(Gastos.objects.all().filter(fk_id_perfil=plk))
    nombre_perfil = Perfiles.objects.get(id=plk)
    mensajeNoGastos = False

    if not Gastos.objects.filter(fk_id_perfil=nombre_perfil):
        mensajeNoGastos = True
    return render(request, "crud/gasto/listar_gasto.html", {
        "table": table,
        "perfilN":plk,
        "nombre_perfil":nombre_perfil,
        "mensajeNoGastos":mensajeNoGastos
    })

def gasto_view(request, pk,plk, template_name='crud/gasto/detalle_gasto.html'):
    gasto = get_object_or_404(Gastos, pk=pk)
    return render(request, template_name, {'object':gasto})

def gasto_create(request,plk, template_name='crud/gasto/gasto_create_form.html'):
    perfil = Perfiles.objects.get(id=plk)
    user = perfil.usuario
    existenCategorias = False
    categorias = Categorias.objects.filter(usuario=user)
    if(categorias):
        existenCategorias = True
    form = GastoForm(request.POST or None)
    isOwner = False
    if (request.user == user):
        isOwner = True
    if form.is_valid():
        f = form.save(commit=False)
        f.fk_id_categoria = Categorias.objects.get(id=request.POST['categoria'])
        f.fk_id_perfil = perfil
        f.usuario = request.user
        f.save()
        return redirect('listar_gasto', plk)
    return render(request, template_name, {'existenCategorias':existenCategorias,'categorias':categorias,'form':form,'isOwner':isOwner})

def gasto_update(request, pk,plk, template_name='crud/gasto/gasto_form.html'):
    gasto= get_object_or_404(Gastos, pk=pk)
    form = GastoForm(request.POST or None, instance=gasto)
    isOwner = False
    userOfGasto = Gastos.objects.get(id=pk).fk_id_categoria.usuario
    if (request.user == userOfGasto):
        isOwner = True
    if form.is_valid():
        form.save()
        return redirect('listar_gasto', plk)
    return render(request, template_name, {'form':form,'isOwner':isOwner})

def gasto_delete(request, pk,plk, template_name='crud/gasto/gasto_confirm_delete.html'):
    gasto= get_object_or_404(Gastos, pk=pk)
    isOwner = False
    userOfGasto = Gastos.objects.get(id=pk).fk_id_categoria.usuario
    if (request.user == userOfGasto):
        isOwner = True
    if request.method=='POST':
        gasto.delete()
        return redirect('listar_gasto', plk)
    return render(request, template_name, {'object':gasto,'isOwner':isOwner})

#Registro de Usuario
def signup(request):
    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if form.is_valid():
            user = form.save(commit=False)
            user.is_active = False
            user.save()
            current_site = get_current_site(request)
            envia_email_confirmacion(user,current_site,urlsafe_base64_encode(force_bytes(user.pk)),account_activation_token.make_token(user))
            return redirect('account_activation_sent')
    else:
        form = SignUpForm()
    return render(request, 'registration/signup.html', {'form': form})

def account_activation_sent(request):
    return render(request, 'registration/account_activation_sent.html')

def activate(request, uidb64, token):
    try:
        uid = force_text(urlsafe_base64_decode(uidb64))
        user = User.objects.get(pk=uid)
    except (TypeError, ValueError, OverflowError, User.DoesNotExist):
        user = None

    if user is not None and account_activation_token.check_token(user, token):
        user.is_active = True
        user.profile.email_confirmed = True

        #Añadir permisos de edicion y borrado de perfiles,categorias y gastos
        permisoEditaPerfiles = Permission.objects.get(name='Can change perfiles')
        permisoBorraPerfiles = Permission.objects.get(name='Can delete perfiles')
        permisoEditaCategorias = Permission.objects.get(name='Can change categorias')
        permisoBorraCategorias = Permission.objects.get(name='Can delete categorias')
        permisoEditaGastos = Permission.objects.get(name='Can change gastos')
        permisoBorraGastos = Permission.objects.get(name='Can delete gastos')

        user.user_permissions.add(permisoEditaPerfiles)
        user.user_permissions.add(permisoBorraPerfiles)
        user.user_permissions.add(permisoEditaCategorias)
        user.user_permissions.add(permisoBorraCategorias)
        user.user_permissions.add(permisoEditaGastos)
        user.user_permissions.add(permisoBorraGastos)

        user.save()
        login(request, user)
        return redirect('c_activada')
    else:
        return render(request, 'registration/account_activation_invalid.html')

#Resumen de un perfil
def resumen(request,plk):
    perfil = Perfiles.objects.get(id=plk)

    isOwner = False
    userOfProfile = perfil.usuario
    if (request.user == userOfProfile):
        isOwner = True

    try:
        informacion = {}
        informacion['nombre_perfil'] = perfil.nombre
        informacion['gasto_total'] = obtener_gasto_total_y_mensual(perfil)[1]
        informacion['mas_caro'] = obtener_gasto_mas_caro(perfil)
        informacion['mas_barato'] = obtener_gasto_mas_barato(perfil)
        return render(request, 'mainGastos/resumen.html',{'isOwner':isOwner,'informacion': informacion})
    except:
        return render(request, 'mainGastos/resumen.html',{'isOwner':isOwner,'informacion': None,'plk':plk})

    #if not Gastos.objects.filter(fk_id_perfil=nombre_perfil):
    #    mensajeNoGastos = True
    #return render(request, "crud/gasto/listar_gasto.html", {
    #    "table": table,
    #    "perfilN":plk,
    #    "nombre_perfil":nombre_perfil,
    #    "mensajeNoGastos":mensajeNoGastos
    #})


from django.test import TestCase
from selenium import webdriver
from microinsurance.models import InsuranceProvider, Microinsurance, Company, Branch
import datetime

class MicroinsuranceAdmin(TestCase):

    def test_saving_new_insurace_provider(self):

        provider = InsuranceProvider()
        provider.insurance_provider_name = 'Manulife'
        provider.date_created = datetime.datetime.now().strftime('%Y-%m-%d %H:%M')
        provider.save()

        all_providers_in_database = InsuranceProvider.objects.all()
        self.assertEquals(len(all_providers_in_database), 1)
        only_provider_in_the_database = all_providers_in_database[0]
        self.assertEquals(provider, only_provider_in_the_database)

        self.assertEquals(only_provider_in_the_database.insurance_provider_name, 'Manulife')
        self.assertEquals(only_provider_in_the_database.date_created.strftime('%Y-%m-%d'), datetime.datetime.now().strftime('%Y-%m-%d'))

    def test_saving_new_microinsurance(self):

        provider = InsuranceProvider()
        provider.insurance_provider_name = 'Manulife'
        provider.date_created = datetime.datetime.now().strftime('%Y-%m-%d %H:%M')
        provider.save()

        all_providers_in_database = InsuranceProvider.objects.all()
        self.assertEquals(len(all_providers_in_database), 1)
        only_provider_in_the_database = all_providers_in_database[0]
        self.assertEquals(provider, only_provider_in_the_database)

        self.assertEquals(only_provider_in_the_database.insurance_provider_name, 'Manulife')
        self.assertEquals(only_provider_in_the_database.date_created.strftime('%Y-%m-%d'), datetime.datetime.now().strftime('%Y-%m-%d'))

        microinsurance = Microinsurance()
        microinsurance.product_name = 'StudentCARE'
        microinsurance.base_price = 25.00
        microinsurance.retail_price = 30.00
        microinsurance.insurance_limit = 5
        microinsurance.age_requirement = 18
        microinsurance.age_limit = 65
        microinsurance.date_created = datetime.datetime.now().strftime('%Y-%m-%d %H:%M')

        provider_data = InsuranceProvider.objects.get(insurance_provider_name = 'Manulife')

        microinsurance.insurance_provider_name = provider_data
        microinsurance.effectivity_date = datetime.datetime.now().strftime('%Y-%m-%d')
        microinsurance.save()

        all_microinsurance_in_database = Microinsurance.objects.all()
        self.assertEquals(len(all_microinsurance_in_database), 1)
        only_microinsurance_in_the_database = all_microinsurance_in_database[0]
        self.assertEquals(microinsurance, only_microinsurance_in_the_database)

        self.assertEquals(only_microinsurance_in_the_database.product_name, 'StudentCARE')
        self.assertEquals(only_microinsurance_in_the_database.base_price, 25.00)
        self.assertEquals(only_microinsurance_in_the_database.effectivity_date.strftime('%Y-%m-%d'), datetime.datetime.now().strftime('%Y-%m-%d'))
        self.assertEquals(only_microinsurance_in_the_database.insurance_provider_name, provider_data)

    def test_saving_new_branch(self):

        company = Company()
        company.company_name = 'Bideco'
        company.date_created = datetime.datetime.now().strftime('%Y-%m-%d %H:%M')
        company.active = True
        company.save()

        all_company_in_database = Company.objects.all()
        self.assertEquals(len(all_company_in_database), 1)
        only_company_in_database = all_company_in_database[0]

        self.assertEquals(only_company_in_database.company_name, 'Bideco')
        self.assertEquals(only_company_in_database.date_created.strftime('%Y-%m-%d %H:%M'), datetime.datetime.now().strftime('%Y-%m-%d %H:%M'))
        self.assertEquals(only_company_in_database.active, True)

        branch = Branch()
        branch.branch_name = 'Binangonan branch'
        branch.branch_address = 'Lopez Jaena St., Libid, Binangonan, Rizal'
        company_data = Company.objects.get(company_name = 'Bideco')
        branch.company_name = company_data
        branch.save()

        all_branch_in_database = Branch.objects.all()
        self.assertEquals(len(all_branch_in_database), 1)
        only_branch_in_database = all_branch_in_database[0]

        self.assertEquals(only_branch_in_database.branch_name, 'Binangonan branch')
        self.assertEquals(only_branch_in_database.branch_address, 'Lopez Jaena St., Libid, Binangonan, Rizal')
        self.assertEquals(only_branch_in_database.company_name, company_data)


# Copyright (C) 2020-2023 by SCICO Developers
# All rights reserved. BSD 3-clause License.
# This file is part of the SCICO package. Details of the copyright and
# user license can be found in the 'LICENSE' file distributed with the
# package.

"""Linear operator utility functions."""

# Needed to annotate a class method that returns the encapsulating class;
# see https://www.python.org/dev/peps/pep-0563/
from __future__ import annotations

from typing import Optional, Union

import scico.numpy as snp
from scico.operator._operator import Operator
from scico.random import randn
from scico.typing import PRNGKey

from ._linop import LinearOperator

def power_iteration(A: LinearOperator, maxiter: int = 100, key: Optional[PRNGKey] = None):
    """Compute largest eigenvalue of a diagonalizable :class:`.LinearOperator`.

    Compute largest eigenvalue of a diagonalizable
    :class:`.LinearOperator` using power iteration.

    Args:
        A: :class:`.LinearOperator` used for computation. Must be
            diagonalizable.
        maxiter: Maximum number of power iterations to use.
        key: Jax PRNG key. Defaults to ``None``, in which case a new key
            is created.

    Returns:
        tuple: A tuple (`mu`, `v`) containing:

            - **mu**: Estimate of largest eigenvalue of `A`.
            - **v**: Eigenvector of `A` with eigenvalue `mu`.

    """
    v, key = randn(shape=A.input_shape, key=key, dtype=A.input_dtype)
    v = v / snp.linalg.norm(v)

    for i in range(maxiter):
        Av = A @ v
        normAv = snp.linalg.norm(Av)
        if normAv == 0.0:  # Assume that ||Av|| == 0 implies A is a zero operator
            mu = 0.0
            v = Av
            break
        mu = snp.sum(v.conj() * Av) / snp.linalg.norm(v) ** 2
        v = Av / normAv
    return mu, v

def operator_norm(A: LinearOperator, maxiter: int = 100, key: Optional[PRNGKey] = None):
    r"""Estimate the norm of a :class:`.LinearOperator`.

    Estimate the operator norm
    `induced <https://en.wikipedia.org/wiki/Matrix_norm#Matrix_norms_induced_by_vector_norms>`_
    by the :math:`\ell_2` vector norm, i.e. for :class:`.LinearOperator`
    :math:`A`,

    .. math::
       \| A \|_2 &= \max \{ \| A \mb{x} \|_2 \, : \, \| \mb{x} \|_2 \leq 1 \} \\
                 &= \sqrt{ \lambda_{ \mathrm{max} }( A^H A ) }
                 = \sigma_{\mathrm{max}}(A) \;,

    where :math:`\lambda_{\mathrm{max}}(B)` and
    :math:`\sigma_{\mathrm{max}}(B)` respectively denote the
    largest eigenvalue of :math:`B` and the largest singular value of
    :math:`B`. The value is estimated via power iteration, using
    :func:`power_iteration`, to estimate
    :math:`\lambda_{\mathrm{max}}(A^H A)`.

    Args:
        A: :class:`.LinearOperator` for which operator norm is desired.
        maxiter: Maximum number of power iterations to use. Default: 100
        key: Jax PRNG key. Defaults to ``None``, in which case a new key
            is created.

    Returns:
        float: Norm of operator :math:`A`.

    """
    return snp.sqrt(power_iteration(A.H @ A, maxiter, key)[0].real)

def valid_adjoint(
    A: LinearOperator,
    AT: LinearOperator,
    eps: Optional[float] = 1e-7,
    x: Optional[snp.Array] = None,
    y: Optional[snp.Array] = None,
    key: Optional[PRNGKey] = None,
) -> Union[bool, float]:
    r"""Check whether :class:`.LinearOperator` `AT` is the adjoint of `A`.

    Check whether :class:`.LinearOperator` :math:`\mathsf{AT}` is the
    adjoint of :math:`\mathsf{A}`. The test exploits the identity

    .. math::
      \mathbf{y}^T (A \mathbf{x}) = (\mathbf{y}^T A) \mathbf{x} =
      (A^T \mathbf{y})^T \mathbf{x}

    by computing :math:`\mathbf{u} = \mathsf{A}(\mathbf{x})` and
    :math:`\mathbf{v} = \mathsf{AT}(\mathbf{y})` for random
    :math:`\mathbf{x}` and :math:`\mathbf{y}` and confirming that

    .. math::
      \frac{| \mathbf{y}^T \mathbf{u} - \mathbf{v}^T \mathbf{x} |}
      {\max \left\{ | \mathbf{y}^T \mathbf{u} |,
       | \mathbf{v}^T \mathbf{x} | \right\}}
      < \epsilon \;.

    If :math:`\mathsf{A}` is a complex operator (with a complex
    `input_dtype`) then the test checks whether :math:`\mathsf{AT}` is
    the Hermitian conjugate of :math:`\mathsf{A}`, with a test as above,
    but with all the :math:`(\cdot)^T` replaced with :math:`(\cdot)^H`.

    Args:
        A: Primary :class:`.LinearOperator`.
        AT: Adjoint :class:`.LinearOperator`.
        eps: Error threshold for validation of :math:`\mathsf{AT}` as
           adjoint of :math:`\mathsf{AT}`. If ``None``, the relative
           error is returned instead of a boolean value.
        x: If not the default ``None``, use the specified array instead
           of a random array as test vector :math:`\mb{x}`. If specified,
           the array must have shape `A.input_shape`.
        y: If not the default ``None``, use the specified array instead
           of a random array as test vector :math:`\mb{y}`. If specified,
           the array must have shape `AT.input_shape`.
        key: Jax PRNG key. Defaults to ``None``, in which case a new key
           is created.

    Returns:
      Boolean value indicating whether validation passed, or relative
      error of test, depending on type of parameter `eps`.
    """

    if x is None:
        x, key = randn(shape=A.input_shape, key=key, dtype=A.input_dtype)
    else:
        if x.shape != A.input_shape:
            raise ValueError("Shape of x array not appropriate as an input for operator A")
    if y is None:
        y, key = randn(shape=AT.input_shape, key=key, dtype=AT.input_dtype)
    else:
        if y.shape != AT.input_shape:
            raise ValueError("Shape of y array not appropriate as an input for operator AT")

    u = A(x)
    v = AT(y)
    yTu = snp.sum(y.conj() * u)  # type: ignore
    vTx = snp.sum(v.conj() * x)  # type: ignore
    err = snp.abs(yTu - vTx) / max(snp.abs(yTu), snp.abs(vTx))
    if eps is None:
        return err
    return float(err) < eps

def jacobian(F: Operator, u: snp.Array, include_eval: Optional[bool] = False) -> LinearOperator:
    """Construct Jacobian linear operator for a general operator.

    For a specified :class:`.Operator`, construct a corresponding
    Jacobian :class:`LinearOperator`, the application of which is
    equivalent to multiplication by the Jacobian of the
    :class:`.Operator` at a specified input value.

    The implementation of this function is based on :meth:`.Operator.jvp`
    and :meth:`.Operator.vjp`, which are themselves based on
    :func:`jax.jvp` and :func:`jax.vjp`. For reasons of computational
    efficiency, these functions return the value of the :class:`.Operator`
    evaluated at the specified point in addition to the requested
    Jacobian-vector product. If the `include_eval` parameter of this
    function is ``True``, the constructed :class:`LinearOperator` returns
    a :class:`.BlockArray` output, the first component of which is the
    result of the :class:`.Operator` evaluation, and the second component
    of which is the requested Jacobian-vector product. If `include_eval`
    is ``False``, then the :class:`.Operator` evaluation computed by
    :func:`jax.jvp` and :func:`jax.vjp` are discarded.

    Args:
        F: :class:`.Operator` of which the Jacobian is to be computed.
        u: Input value of the :class:`.Operator` at which the Jacobian is
           to be computed.
        include_eval: Flag indicating whether the result of evaluating
           the :class:`.Operator` should be included (as the first
           component of a :class:`.BlockArray`) in the output of the
           Jacobian :class:`LinearOperator` constructed by this function.

    Returns:
      A :class:`LinearOperator` capable of computing Jacobian-vector
      products.
    """
    if include_eval:
        Fu, G = F.vjp(u, conjugate=True)

        def adj_fn(v):
            return snp.blockarray((Fu, G(v)))

        def eval_fn(v):
            return snp.blockarray(F.jvp(u, v))

    else:
        adj_fn = F.vjp(u, conjugate=True)[1]

        def eval_fn(v):
            return F.jvp(u, v)[1]

    return LinearOperator(
        F.input_shape,
        output_shape=F.output_shape,
        eval_fn=eval_fn,
        adj_fn=adj_fn,
        input_dtype=F.input_dtype,
        output_dtype=F.output_dtype,
    )


import pytest

import dpnp as inp

import numpy

def vvsort(val, vec, size):
    for i in range(size):
        imax = i
        for j in range(i + 1, size):
            if numpy.abs(val[imax]) < numpy.abs(val[j]):
                imax = j

        temp = val[i]
        val[i] = val[imax]
        val[imax] = temp

        for k in range(size):
            temp = vec[k, i]
            vec[k, i] = vec[k, imax]
            vec[k, imax] = temp

def test_cholesky():
    a = numpy.array([[[1, -2], [2, 5]]])
    ia = inp.array(a)
    result = inp.linalg.cholesky(ia)
    expected = numpy.linalg.cholesky(a)
    numpy.testing.assert_array_equal(expected, result)

def test_det():
    arrays = [
        [[0, 0], [0, 0]],
        [[1, 2], [1, 2]],
        [[1, 2], [3, 4]],
        [[[1, 2], [3, 4]], [[1, 2], [2, 1]], [[1, 3], [3, 1]]],
        [[[[1, 2], [3, 4]], [[1, 2], [2, 1]]], [[[1, 3], [3, 1]], [[0, 1], [1, 3]]]]
    ]
    for array in arrays:
        a = numpy.array(array)
        ia = inp.array(a)
        result = inp.linalg.det(ia)
        expected = numpy.linalg.det(a)
        numpy.testing.assert_allclose(expected, result)

@pytest.mark.parametrize("type",
                         [numpy.float64, numpy.float32, numpy.int64, numpy.int32],
                         ids=['float64', 'float32', 'int64', 'int32'])
@pytest.mark.parametrize("size",
                         [2, 4, 8, 16, 300])
def test_eig_arange(type, size):
    a = numpy.arange(size * size, dtype=type).reshape((size, size))
    symm_orig = numpy.tril(a) + numpy.tril(a, -1).T + numpy.diag(numpy.full((size,), size * size, dtype=type))
    symm = symm_orig
    dpnp_symm_orig = inp.array(symm)
    dpnp_symm = dpnp_symm_orig

    dpnp_val, dpnp_vec = inp.linalg.eig(dpnp_symm)
    np_val, np_vec = numpy.linalg.eig(symm)

    # DPNP sort val/vec by abs value
    vvsort(dpnp_val, dpnp_vec, size)

    # NP sort val/vec by abs value
    vvsort(np_val, np_vec, size)

    # NP change sign of vectors
    for i in range(np_vec.shape[1]):
        if np_vec[0, i] * dpnp_vec[0, i] < 0:
            np_vec[:, i] = -np_vec[:, i]

    numpy.testing.assert_array_equal(symm_orig, symm)
    numpy.testing.assert_array_equal(dpnp_symm_orig, dpnp_symm)

    assert (dpnp_val.dtype == np_val.dtype)
    assert (dpnp_vec.dtype == np_vec.dtype)
    assert (dpnp_val.shape == np_val.shape)
    assert (dpnp_vec.shape == np_vec.shape)

    numpy.testing.assert_allclose(dpnp_val, np_val, rtol=1e-05, atol=1e-05)
    numpy.testing.assert_allclose(dpnp_vec, np_vec, rtol=1e-05, atol=1e-05)

def test_eigvals():
    arrays = [
        [[0, 0], [0, 0]],
        [[1, 2], [1, 2]],
        [[1, 2], [3, 4]]
    ]
    for array in arrays:
        a = numpy.array(array)
        ia = inp.array(a)
        result = inp.linalg.eigvals(ia)
        expected = numpy.linalg.eigvals(a)
        numpy.testing.assert_allclose(expected, result, atol=0.5)

def test_matrix_rank():
    arrays = [
        [0, 0],
        # [0, 1],
        [1, 2],
        [[0, 0], [0, 0]],
        # [[1, 2], [1, 2]],
        # [[1, 2], [3, 4]],
    ]
    tols = [None]
    for array in arrays:
        for tol in tols:
            a = numpy.array(array)
            ia = inp.array(a)
            result = inp.linalg.matrix_rank(ia, tol=tol)
            expected = numpy.linalg.matrix_rank(a, tol=tol)
            numpy.testing.assert_array_equal(expected, result)


import sys
from decimal import Decimal
from datetime import datetime, timedelta

def printlist(lst, name, **kw):
    import numpy as np
    mean = float(np.mean(lst,0))
    std = np.std(lst)
    stdstr = '%8.3f' % std if std < 10000 else '%8.5g' % std
    sovm = 100*std / mean if mean > 0 else float('nan')
    last = float(lst[-1])
    lstd = 100*(last - mean) / std if std > 0 else float('nan')
    print("{name:10s}={mean:10.5g}, σ={std:10.4g} ({sovm:9.3g}%) [{last:9.2f} ({lstd:7.3g}%)]".format(**locals()), **kw)

def to_dhms(tdelta):
    days = tdelta.days
    hr, sec = divmod(tdelta.seconds, 3600)
    mn, sec = divmod(sec, 60)
    return (days, hr, mn, sec)

def interval_str(days, hr, mn, sec):
    return (('%2dd ' % days) if days else '') + ('%2d:%02d:%02d'% (hr, mn, sec))

class Progress:
    def __init__(self, total):
        self.start = datetime.now()
        self.total = total

    def get_eta(self, i):
        """Returns (end time, time left) as datetime, timedelta objects."""
        now = datetime.now()
        tused = now - self.start

        # timedeltas cannot be multiplied by floats (?), so we get out the
        #seconds, use that, and make it back into a dt
        usedsecs = 24*60*60 * tused.days + tused.seconds + (1e-6 * tused.microseconds)
        tleft = (timedelta(0, (usedsecs) * ((self.total - i) / i)) if i > 0
                else timedelta(0, 0))

        return now + tleft, tleft

    def eta_strs(self, i, timefmt = '(%a %b %d, %H:%M:%S)'):
        """Returns (time left, total time, endtime) as strs"""
        endtime, tleft = self.get_eta(i)
        interval = to_dhms(tleft)
        totinterval = to_dhms(endtime - self.start)

        return (interval_str(*interval), interval_str(*totinterval),
                    endtime.strftime(timefmt))

    def eta_str(self, i, timefmt = '(%a %b %d, %H:%M:%S)'):
        ss = self.eta_strs(i, timefmt=timefmt)
        return 'Ends in ({} / {}) on {}'.format(*ss)

def errprint(*args, **kw):
    kwargs = {'file':sys.stderr}
    kwargs.update(kw)
    print(*args, **kwargs)
    kwargs['file'].flush()

def filefinder(dir, *names, regexp=None, matchall=True, types=Decimal, ext = 'tsv.gz', **args):
    """
    dir             : Directory to look in
    names           : keywords to look for / store into
    regexp          : Define the regular expression for groups (default: #NAME##VALUE#-#NAME##VALUE#....ext)
    matchall        : Match the entire expression
    types           : What types to apply (default: all Decimal)
    ext             : File extension (ignores everything else) (default: 'tsv.gz')
    args            : Like names, but add some constants (such as T=1) for all fs
    """
    import re, fpath
    from collections import namedtuple

    names = [(n + '0' if n in 'f' else n) for n in names]
    FoundFile = namedtuple('FoundFile', names + ['f'])

    try:
        iter(types)
    except TypeError:
        types = [types] * len(names)

    dir = fpath.Dir(dir)
    children = [f for f in dir.children() if f[-1][(-len(ext)-1):] == '.' + ext]

    if regexp is None:
        regexp='-'.join([n + r'(\F)' for n in names])
    regexp = regexp.replace(r'\F', r'(?:[0-9]*\.?[0-9]*)|(?:inf)')
    regexp = regexp.replace(r'\G', r'(?:[0-9]*\.?[0-9]*)|(?:inf)|(?:[0-9]*\.?[0-9]*e[-+0-9]+)')

    regex = re.compile(regexp)
    matches = [(list(regex.finditer(f[-1])), f) for f in children]
    badmatches = [f for ms,f in matches if len(ms) == 0]

    if badmatches and matchall:
        raise ValueError('%s could not match %s' % (regexp, badmatches[0][-1]))
    matches = [(ms[0],f) for ms,f in matches if len(ms) > 0]

    groups = sorted([list(zip(names, [t(m or 'nan') for t,m in zip(types,mtch.groups())]))
        + [('f',f)] for mtch,f in matches])

    pdicts = [dict(lst) for lst in groups]
    for p in pdicts:
        p.update(args)
    return [FoundFile(**d) for d in pdicts]

def filefinder2(dir, *names, delim='_', matchall=True, types=Decimal, ext = 'tsv.gz'):
    """
    Find files based on a standard naming scheme of name value [delim name value...] (literal .) ext

    dir             : Directory to look in
    names           : keywords to look for / store into
    delim           : Delimiter separating file names
    matchall        : Match every file with the given extension
    types           : What types to apply (default: all Decimal). May be a single type or list of types
    ext             : File extension (ignores everything else) (default: 'tsv.gz')
    """
    import re, pathlib
    from collections import namedtuple
    FoundFile = namedtuple('FoundFile', list(names) + ['fname'])

    try:
        iter(types)
    except TypeError:
        types = [types] * len(names)

    dir = pathlib.Path(dir)
    children = [f for f in dir.iterdir() if f.name[(-len(ext)-1):] == '.' + ext]
    #print(len(children), [f[-1][(-len(ext)-1):] for f in dir.children()])

    def handlechild(c):
        cutf = c.name[:(-len(ext)-1)]
        splits = cutf.split(delim)
        if not len(splits) == len(names):
            raise ValueError("%r splits into %d instead of %d" %
                (cutf, len(splits), len(names)))
        ns, valstrs = zip(*[(v[:len(n)], v[len(n):]) for v,n in zip(splits,names)])
        m=0

        nspace = dict()
        nspace['fname'] = c
        #nspace['cutf'] = cutf
        for n,v,n0,t in zip(ns, valstrs, names, types):
            m += 1
            if not n == n0:
                raise ValueError("%r does not match %r in %dth arg %r in %r" % (
                n, n0, m, n+v, cutf))
            try:
                value = t(v)
            except:
                raise ValueError("%r not converted by %r in %dth arg %r in %r" % (
                v, t, m, n+v, cutf))
            nspace[n0] = value
        return FoundFile(**nspace)

    def trycatch(c):
        try:
            return handlechild(c)
        except ValueError as e:
            return None

    if matchall:
        nspaces = [handlechild(c) for c in children]
    else:
        nspaces = [trycatch(c) for c in children]
        nspaces = [n for n in nspaces if n is not None]

    return sorted(nspaces)

def groupfiles(dlst, key):
    bigdict = dict()
    for d in dlst:
        curval = getattr(d, key)
        keylist = bigdict.get(curval, [])
        keylist.append(d)
        bigdict[curval] = keylist
    return bigdict

def groupby(dlst, groupkey, sortby=None):
    if sortby is None:
        return sorted(groupfiles(dlst, groupkey).items())
    else:
        return sorted(groupfiles(dlst, groupkey).items(), key=sortby)


import numpy as np

from keras.models import Sequential, load_model
from keras.layers import Dense, Activation, Dropout
import spacy

# from fantom_util.feature_extraction.spacy_extractor import spacy_model
from fantom_util.file_io_util import file_from_s3

# from fantom_util.models import Model

class RootNodeClassifierMLP:
    def __init__(self):
        weights_path = "weights_rnc_mlp.hdf5"
        file_from_s3("SOME_AWS_BUCKET", weights_path, weights_path)
        self.special_list = [
            ["what", "who", "when", "how", "where"],
            ["why", "whether", "which"],
            ["yes", "no", "sure", "course"],
            ["about"],
            ["want", "wanna", "like", "love", "hate"],
            ["chat", "talk"],
            [
                "he",
                "she",
                "it",
                "them",
                "that",
                "this",
                "those",
                "us",
                "his",
                "her",
                "their",
            ],
            ["I", "you"],
            ["else", "another", "again", "change"],
            ["something"],
            ["stop"],
            ["hi", "hello"],
        ]
        self.min_tok_pos = 83
        self.max_tok_pos = 100

        self.nlp = spacy.load("en_core_web_lg")
        # self.nlp = spacy_model()
        self.data_width = 32

        self.model = Sequential()
        self.model.add(Dense(20, activation="relu", input_shape=(self.data_width,)))
        self.model.add(Dropout(0.5))
        self.model.add(Dense(20, activation="relu"))
        self.model.add(Dropout(0.5))
        self.model.add(Dense(1, activation="sigmoid"))
        self.model.load_weights(weights_path)
        self.model.compile(
            optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"]
        )

    def prepare_data(self, utterances):
        X_data = np.zeros((len(utterances), self.max_tok_pos - self.min_tok_pos + 2))
        Y_data = np.zeros(len(utterances))
        X_additional_data = np.zeros((len(utterances), len(self.special_list) + 1))
        # X_data
        for sample_nr in range(len(utterances)):
            line_doc = self.nlp(utterances[sample_nr])
            if "tell me" == str(utterances[sample_nr])[0:7]:
                X_additional_data[sample_nr, -2] += 1.0
            for token in line_doc:
                try:
                    X_data[sample_nr, token.pos - self.min_tok_pos] += 1.0
                    for list_nr in range(len(self.special_list)):
                        if token.orth in self.special_list[list_nr]:
                            X_additional_data[sample_nr, list_nr] += 1.0
                    if not token.is_stop:
                        X_additional_data[sample_nr, -1] += 1.0
                except:
                    pass
        X_data = np.concatenate((X_data, X_additional_data), axis=1)
        return X_data

    def predict_string(self, utterance):
        return float(self.predict_list([utterance])[0][0])

    def predict_list(self, list_of_utterances):
        return self.model.predict(x=self.prepare_data(list_of_utterances))

def main():
    rnc_mlp = RootNodeClassifierMLP()
    print(
        rnc_mlp.predict_list(
            ["hi", "Let's chat about football", "my name is trevor", "do you like him"]
        )
    )
    print((rnc_mlp.predict_string("I would like to talk about chickens")))

if __name__ == "__main__":
    main()


from nltk.corpus import conll2002
from sklearn.feature_extraction import DictVectorizer
from sklearn.linear_model import Perceptron
from sklearn.ensemble import AdaBoostClassifier, RandomForestClassifier
from sklearn.metrics import precision_recall_fscore_support
from sklearn.linear_model import LogisticRegression
from sklearn.svm import LinearSVC
import time
import string
import re
from nltk.stem import WordNetLemmatizer
import numpy as np

lemmatizer = WordNetLemmatizer()

def getnames(text):
    return open(text, 'r').read().split()

names = set(getnames('sp_name_out.txt'))

def load_wordvec(file):
    wordvec = {}
    with open(file) as f:
        for line in f:
            word = line.split()[0]
            vec = line.split()[1:]
            for value in vec:
                try:
                    value = float(value)
                except BaseException:
                    value = 0.0
            wordvec[word] = vec
    return wordvec

vec = load_wordvec("wiki.es.vec")

def wordshape(word):
    output = ''
    for i in range(len(word)):
        if word[i].isupper():
            output += 'X'
        elif word[i].islower():
            output += 'x'
        elif re.match(r'\d', word[i]):
            output += 'd'
        else:
            output += word[i]
    return output

def getfeats(word, POS, o):
    o = str(o)
    features = [
        (o + 'word', word),
        (o + 'POS', POS)
    ]
    return features

def word2features(sent, i):
    features = []
    for o in [-3, -2, -1, 0, 1, 2, 3]:
        if i+o >= 0 and i+o < len(sent):
            word = sent[i+o][0]
            POS = sent[i+o][1]
            featlist = getfeats(word, POS, o)
            features.extend(featlist)

    # punctuation = set(sent[i][0]) & set(string.punctuation)
    # features.append(('PREFIX_1', sent[i][0][:1]))
    # features.append(('SUFFIX_1', sent[i][0][-1:]))
    # features.append(('PREFIX_2', sent[i][0][:2]))
    # features.append(('SUFFIX_2', sent[i][0][-2:]))
    features.append(('PREFIX_3', sent[i][0][:3]))
    features.append(('SUFFIX_3', sent[i][0][-3:]))
    features.append(('SHAPE', wordshape(sent[i][0])))
    # features.append(('LENGTH', len(sent[i][0])))
    #features.append(('PUNCTUATION', len(punctuation)))
    # features.append(('NAMES', sent[i][0] in names))
    return dict(features)

if __name__ == "__main__":
    t = time.time()
    train_sents = list(conll2002.iob_sents('esp.train'))
    dev_sents = list(conll2002.iob_sents('esp.testa'))
    test_sents = list(conll2002.iob_sents('esp.testb'))
    train_sents = train_sents + dev_sents

    train_feats = []
    train_labels = []

    for sent in train_sents:
        for i in range(3):
            sent.insert(0, ('SSS', 'None', 'None'))
            sent.append(('EEE', 'None', 'None'))
        for i in range(len(sent)):
            feats = word2features(sent,i)
            train_feats.append(feats)
            train_labels.append(sent[i][-1])

    vectorizer = DictVectorizer()
    X_train = vectorizer.fit_transform(train_feats)
    for sent in train_sents:
        for i in range(len(sent)):
            try:
                X_train[i, :] += sent[i][0]
            except KeyError:
                try:
                    X_train[i, :] += vec[lemmatizer.lemmatize(sent[i][0])]
                except KeyError:
                    X_train[i, :] = np.zeros((1, 50))

    model = LogisticRegression(C=10)
    # model = LinearSVC(C=1)
    # model = Perceptron(verbose=1)
    model.fit(X_train, train_labels)

    test_feats = []
    test_labels = []

    # switch to test_sents for your final results
    for sent in test_sents:
        for i in range(len(sent)):
            feats = word2features(sent,i)
            test_feats.append(feats)
            test_labels.append(sent[i][-1])

    X_test = vectorizer.transform(test_feats)
    for sent in test_sents:
        for i in range(len(sent)):
            try:
                X_test[i, :] += sent[i][0]
            except KeyError:
                try:
                    X_test[i, :] += vec[lemmatizer.lemmatize(sent[i][0])]
                except KeyError:
                    X_test[i, :] = np.zeros((1, 50))
    y_pred = model.predict(X_test)

    j = 0
    print("Writing to results.txt")
    with open("results.txt", "w") as out:
        for sent in test_sents:
            for i in range(len(sent)):
                word = sent[i][0]
                gold = sent[i][-1]
                pred = y_pred[j]
                j += 1
                out.write("{}\t{}\t{}\n".format(word,gold,pred))
        out.write("\n")
    print(time.time() - t)
    print("Now run: python conlleval.py results.txt")


# -*- coding: utf-8 -*-
"""
Created on Sun Feb 09 19:49:25 2020

@author: ashwin_menon
"""
#Made by Ashwin Menon 101703120
import setuptools

with open("README.md", "r") as fh:
    long_description = fh.read()

setuptools.setup(
    name="fill_missing_values_ashwin", # Replace with your own username
    version="0.0.3",
    author="ashwin_menon",
    author_email="ashwin.s.menon1999@gmail.com",
    description="A small package for filling missing values in the given dataset",
    long_description="",
    long_description_content_type="text/markdown",
    url="https://github.com/ash-1999/fill_missing_values_ashwin",
    download_url="https://github.com/ash-1999/fill_missing_values_ashwin/archive/0.0.3.tar.gz",
    packages=["fill_missing_values_ashwin"],
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    python_requires='>=3.6',
)


import requests
import json
from io import StringIO
from Bio import Seq
from Bio import SeqIO
from Bio import SeqFeature
import sys
import uuid

sbol_type_table = {
    "CDS": "cds",
    "regulatory": "promoter" #promoter is actually a subclass of regulatory
}

def get_sequence_and_features(block, allblocks):
    features = block["sequence"]["features"]
    for f in features:
        f["block_id"] = block["id"]

    if len(block["components"])==0:
        return { "sequence": block["sequence"]["sequence"], "features": features}
    else:
        seq = ""
        for i in range(0, len(block["components"])):
            block_id = block["components"][i]
            bl = allblocks[block_id]
            pos = len(seq)
            output = get_sequence_and_features(bl, allblocks)
            seq += output["sequence"]

            features.append( {
                "block_id": block_id,
                "start" : pos,
                "end" : len(seq),
                "type" : "block",
                "parent_block" : block["id"] + "," + str(i),
            })

            for feature in output["features"]:
                features.append( {
                    "block_id": feature["block_id"],
                    "start" : feature["start"] + pos,
                    "end" : feature["end"] + pos,
                    "type" : feature["type"],
                    "parent_block" : feature.get("parent_block",None)
                })
        return { "sequence": seq, "features": features }

def project_to_genbank(filename, project, allblocks):
    blocks = project["components"]
    seq_obj_lst = []

    for bid in blocks:
        block = allblocks.get(bid)
        if not block:
            continue

        output = get_sequence_and_features(block, allblocks)
        seq = output["sequence"]
        features = output["features"]

        seq_obj = SeqIO.SeqRecord(Seq.Seq(seq,Seq.Alphabet.DNAAlphabet()),block["id"])
        for feature in features:
            sf = SeqFeature.SeqFeature()
            sf.type = feature["type"]
            sf.location = SeqFeature.FeatureLocation(feature["start"],feature["end"])
            sf.qualifiers["block_id"] = feature.get("block_id","")
            sf.qualifiers["parent_block"] = feature.get("parent_block",None)
            seq_obj.features.append(sf)
        seq_obj_lst.append(seq_obj)

    SeqIO.write(seq_obj_lst, open(filename, "w"), "genbank")

def create_block_json(id, sequence, features):
    return {
        "id": id,
        "metadata" : {
            "authors": [],
            "tags": {},
            "version": ""
        },
        "rules": {},
        "components": [],
        "sequence" : {
            "sequence": sequence,
            "features": features
        }
      }

def genbank_to_block_helper(gb, convert_features=False):
    all_blocks = {}
    block_parents = {}
    block_start_pos = {}
    features = []
    newblocks = 0
    sequence = str(gb.seq)
    #root_id = str(uuid.uuid4())
    root_id = gb.id

    #Collect all the neccessary metadata
    root_block = create_block_json(root_id, sequence, [])
    root_block["metadata"]["description"] = gb.description
    root_block["metadata"]["name"] = gb.name
    for annot in gb.annotations:
        try:
            json.dumps(gb.annotations[annot])
            root_block["metadata"][annot] = gb.annotations[annot]
        except:
            pass

    all_blocks[root_id] = root_block
    #all_blocks[gb.id] = root_block

    for f in gb.features:
        qualifiers = f.qualifiers
        start = f.location.start.position
        end = f.location.end.position

        feature = {}
        for q in qualifiers:
            if q != "block_id" and q != "parent_block":
                try:
                    json.dumps(qualifiers[q][0])
                    feature[q] = qualifiers[q][0]
                except:
                    pass
        feature["start"] = start
        feature["end"] = end
        feature["strand"] = f.location.strand
        feature["type"] = f.type

        sbol_type = sbol_type_table.get(f.type)

        if "block_id" in qualifiers:
            block_id = qualifiers["block_id"][0]
            if f.type == 'source':
                continue

            if f.type == "block":
                child_block = create_block_json(block_id, sequence[start:end], [])
                if qualifiers.get("parent_block",None):
                    words = qualifiers["parent_block"][0].split(",")
                    block_parents[block_id] = {
                        "id" : words[0],
                        "index" : int(words[1])
                    }
                else:
                    block_parents[block_id] = {
                        "id" : root_id,
                        "index" : newblocks
                    }
                    newblocks += 1
                child_block["metadata"]["tags"]["sbol"] = sbol_type
                all_blocks[block_id] = child_block
            else:
                feature["block_id"] = block_id
                features.append(feature)
        else:
            if sbol_type and convert_features:
                block_id = str(uuid.uuid4())
                child_block = create_block_json(block_id, sequence[start:end], [])
                child_block["metadata"]["tags"]["sbol"] = sbol_type
                all_blocks[block_id] = child_block
                block_parents[block_id] = {
                    "id" : root_id,
                    "index" : newblocks
                }
                newblocks += 1
            else:
                feature["block_id"] = root_id
                features.append(feature)

    for block_id in block_parents:
        parent = block_parents[ block_id ]
        if block_id in all_blocks and parent["id"] in all_blocks:
            all_blocks[ parent["id"] ]["components"].insert(int(parent["index"]), block_id)
            all_blocks[ parent["id"] ]["sequence"]["sequence"] = ""

    for feature in features:
        block_id = feature["block_id"]
        del feature["block_id"]
        if block_id in all_blocks:
            all_blocks[ block_id ]["sequence"]["features"].append(feature)
    #del all_blocks[gb.id]
    return { "block": all_blocks[root_id], "blocks": all_blocks }

def genbank_to_project(filename, convert_features=False):
    project = {
      'id': str(uuid.uuid4()),
      'metadata': {
        'authors': [],
        'tags': {},
        'version': '',
      },
      'components': [],
    };
    blocks_array = []
    all_blocks = {}
    generator = SeqIO.parse(open(filename,"r"),"genbank")
    for record in generator:
        gb = record
        one_block = genbank_to_block_helper(gb, convert_features)
        project["components"].append(one_block["block"]["id"])
        for bid in one_block["blocks"]:
            all_blocks[bid] = one_block["blocks"][bid]
    return { "project": project, "blocks": all_blocks }


# -*- coding: utf-8 -*-
# @Time : 2019/11/26 下午1:15
# @Author : kimihiro
# @File : __init__.py.py
# @Software: PyCharm
from cfg_pull_service.get_config_data import ApolloClient
from cfg_update_service.update_config import UpdateConfigService
from config import Configs

class UpdateConfigApis:
    def __init__(self, ap_conf: dict = Configs.DEV_APO, conf_info: list = Configs.CONF_INFO):
        self.ap_conf = ap_conf
        self.conf_info = conf_info

    def _get_c_data(self) -> dict:
        apollo_client = ApolloClient(**self.ap_conf)
        return apollo_client.get_config()

    def auto_update_conf(self):
        c_data = self._get_c_data()
        if c_data:
            for info in self.conf_info:
                up_ser = UpdateConfigService(**info)
                up_ser.update(c_data)

if __name__ == "__main__":
    a = UpdateConfigApis()
    a.auto_update_conf()


from asyncio import ensure_future, get_event_loop
from os import path
from signal import SIGINT, SIGTERM

from whalesong import Whalesong
from whalesong.driver_chromium import WhalesongDriver
from whalesong.managers.stream import Stream

class GetLiveLocations:

    def __init__(self, print_fn=print, loop=None):
        self._print_fn = print_fn
        self._driver = Whalesong(
            driver=WhalesongDriver(profile=path.join(path.dirname(__file__), 'profile-chromium'),
                                   headless=True,
                                   loop=loop),
            loop=loop
        )

    @property
    def loop(self):
        return self._driver.loop

    def echo(self, txt):
        self._print_fn(txt)

    async def check_stream(self):
        stream = await self._driver.stream.get_model()
        self.echo("Stream: {}".format(stream.stream))
        self.echo("State: {}".format(stream.state))

    async def monitor_stream(self):
        self.echo('Monitor stream')
        live_loc_it = None
        new_message_monitor = None
        message_ack_monitor = None

        async for evt in self._driver.stream.monitor_field('stream'):
            self.echo('Stream value: {}'.format(evt['value']))

            if evt['value'] == Stream.Stream.CONNECTED:

                if live_loc_it is None:
                    live_loc_it = self._driver.live_locations.get_items()
                    ensure_future(self.list_live_locations(live_loc_it))

                if new_message_monitor is None:
                    new_live_locations = self._driver.live_locations.monitor_add()
                    ensure_future(self.monitor_new_live_locations(new_live_locations))

            else:
                if live_loc_it is not None:
                    live_loc_it = None
                    await self._driver.cancel_iterators()

                if new_message_monitor is not None:
                    self._driver.stop_monitor(new_message_monitor)
                    new_message_monitor = None

                if message_ack_monitor is not None:
                    self._driver.stop_monitor(message_ack_monitor)
                    message_ack_monitor = None

    async def list_live_locations(self, it):
        self.echo('List live locations')
        async for live_location in it:
            self.echo('Live location: {}'.format(live_location))
            ensure_future(self.monitor_live_location(live_location))

        self.echo('List messages finished')

    async def monitor_new_live_locations(self, it):
        self.echo('Monitor live locations')
        async for live_location in it:
            self.echo('New live location: {}'.format(live_location))
            ensure_future(self.monitor_live_location(live_location))

    async def monitor_live_location(self, live_location):
        self._driver.live_locations[live_location.id].subscribe()

        for participant in live_location.participants:
            ensure_future(self.monitor_participant(live_location, participant))

        async for participant in self._driver.live_locations[live_location.id].participants.monitor_add():
            ensure_future(self.monitor_participant(live_location, participant))

    async def monitor_participant(self, live_location, participant):
        async for new_part in self._driver.live_locations[live_location.id] \
                .participants[participant.id].monitor_model():
            self.echo('Participant at live location `{}` now are in {}@{}'.format(live_location.id,
                                                                                new_part.lat,
                                                                                new_part.lng))

    async def start(self):
        await self._driver.start()

        loop = get_event_loop()
        loop.add_signal_handler(SIGINT, lambda *args: ensure_future(self._driver.stop()))
        loop.add_signal_handler(SIGTERM, lambda *args: ensure_future(self._driver.stop()))

        ensure_future(self.check_stream()),
        ensure_future(self.monitor_stream())

        await self._driver.wait_until_stop()

if __name__ == '__main__':
    monitor = GetLiveLocations()
    monitor.loop.run_until_complete(monitor.start())


import Q1_C1815697 as c
import matplotlib.pyplot as plt
import numpy as np

def count_wins(num_of_races, file_name = 'sailors_values.csv'):
    name_list = list(c.read_sailor_data(file_name).keys())
    #Creates a dictionary with the names as keys and empty lists as values
    results = {key:0 for key in name_list}
    for i in range(0, num_of_races):
        race_result = c.calculate_finishing_order(c.generate_performances(c.read_sailor_data(file_name)))
        for index, value in enumerate(race_result):
            results[value] += (index + 1)
    return sorted(results.items(), key=lambda keyvalue: keyvalue[1])

def average_modelling(num_of_models, race_result):
    output = {}
    for tuple in race_result:
        #Creates average score output and stores as tuple
        output[tuple[0]] = tuple[1] / num_of_models
    #Returns sorted avg results
    return sorted(output.items(), key=lambda keyvalue: keyvalue[1])

num_of_races = int(input("Number of models: "))
results = count_wins(num_of_races)
#Collects names and results from model
names = [value[0] for value in results]
totals = [value[1] for value in results]
#Calculates avg finsishing position based on the results
averages = average_modelling(num_of_races, results)
avg_values = [value for key, value in averages]

def plot_bar(x_data, y_data, title, x_label, y_label):
    index = np.arange(len(x_data))
    bar_list = plt.bar(index, y_data)
    plt.xlabel(x_label, fontsize=10)
    plt.ylabel(y_label, fontsize=10)
    plt.xticks(index, x_data, fontsize=10)
    plt.title(title, fontsize=15)
    x = -0.05 #Set X value for bar count data
    for value in y_data:
        plt.text(x, value * 0.5, value, fontsize=8)
        x += 1

fig_size = plt.rcParams["figure.figsize"] = [6.4, 4.8]
figure = plt.gcf()
plt.subplot(2, 1, 1)
plot_bar(names, totals, "Total Scores", "Names", "Total Score")
plt.subplot(2, 1, 2)
plot_bar(names, avg_values, "Average Position", "Names", "Average Positions")
plt.tight_layout()
plt.show()


import torch
import random
import math
import time
from torch.autograd import Variable
from .Plotter import Plotter
from Env.Environment import Environment
import numpy as np
import os
# if gpu is to be used
use_cuda = torch.cuda.is_available()
FloatTensor = torch.cuda.FloatTensor if use_cuda else torch.FloatTensor
LongTensor = torch.cuda.LongTensor if use_cuda else torch.LongTensor
ByteTensor = torch.cuda.ByteTensor if use_cuda else torch.ByteTensor
Tensor = FloatTensor

class Improver(object):
    def __init__(self, net, memory_size, memory, shared):
        self.net = net  # Deep Net
        self.memory = memory
        self.shared = shared  # shared resources, {'memory', 'SENT_FLAG', 'weights'}
        #self.env = env
        self.plotter = Plotter(folder='DQN/plot/cartpole_simple/exp4')
        self.steps_done = 0

        # hyperparameters:
        self.EPS_START = 1.
        self.EPS_END = 0.05
        self.EPS_DECAY = 50   # DECAY larger: slower
        self.MEMORY_SIZE = memory_size

    def behavior_policy(self, state):
        # We can add epislon here to decay the randomness
        # We store the tensor of size 1x1
        sample = random.random()
        eps_threshold = self.EPS_END + (self.EPS_START - self.EPS_END) * \
                        math.exp(-1. * self.steps_done / self.EPS_DECAY)
        #print('threshold: {}' . format(eps_threshold))

        if sample > eps_threshold:
            return self.policy(state)
        else:
            #return LongTensor([[self.env.action_space.sample()]])
            return self.env.action_space.sample()
    def policy(self, state):
        # return tensor of size 1x1
        res = self.net(Variable(state, volatile=True).type(FloatTensor)).data
        #print('policy values: {}, {}' . format(res[0][0], res[0][1]))
        #print('policy value: {}' . format(res.max(1)[0][0]))
        return res.max(1)[1][0]
        #return res.max(1)[1].view(1,1)
        #return self.net(Variable(state, volatile=True).type(FloatTensor))\
        #                .data.max(1)[1].view(1,1)

    def save_checkpoint(self, state, filename='save/checkpoint_improver.pth.tar'):
        torch.save(state, filename)

    def run(self):
        POPULATE_FLAG = True
        #MEMORY_SIZE = self.shared['memory'].capacity
        MEMORY_SIZE = self.MEMORY_SIZE
        #POPULATE_MAX = MEMORY_SIZE
        POPULATE_MAX = 1
        populate_num = 0
        #PLOT_FREQ = int(MEMORY_SIZE/32)
        PLOT_FREQ = 20
        R = 0. # for plotting
        step = 0
        Repi = 0
        self.env = Environment()
        self.env.set_all()
        self.env.reset()
        last_time = time.time()
        prev_plot = self.steps_done
        pretrain = True
        #PRETRAIN_MAX = self.MEMORY_SIZE // 2
        PRETRAIN_MAX = 2000

        if os.path.isfile('save/checkpoint_improver.pth.tar'):
            print("=> loading checkpoint '{}'".format('save/checkpoint_improver.pth.tar'))
            checkpoint = torch.load('save/checkpoint_improver.pth.tar')
            print("=> loaded checkpoint '{}'"
                  .format('save/checkpoint_improver.pth.tar'))
        else:
            print("=> no checkpoint found at '{}'".format('save/checkpoint_improver.pth.tar'))

        while True:
            if POPULATE_FLAG:
                if pretrain:
                    if populate_num == PRETRAIN_MAX:
                        pretrain = False
                        self.shared['SENT_FLAG'] = 0
                        POPULATE_FLAG = False
                else:
                    if populate_num == POPULATE_MAX:
                        # reset populate num
                        self.shared['SENT_FLAG'] = 0
                        POPULATE_FLAG = False
            else:
                if self.shared['SENT_FLAG']:
                    # evaluator sent the weights
                    print('copying...')
                    self.net.load_state_dict(self.shared['weights'])
                    POPULATE_FLAG = True
                    populate_num = 0
                    # after evaluating the policy for one round, set the epislon smaller
                    next_step = self.steps_done + 1
                    if next_step != 0:  # loop to back
                        # then set eps_threshold to a small value
                        self.steps_done = next_step
            self.save_checkpoint({
                'state_dict': self.net.state_dict(),
            })
            #print('loop took {0} seconds' . format(time.time()-last_time))

            state = self.env.get_state()  # this is to avoid time delay
            last_time = time.time()
            # 0.003s
            state_torch = torch.from_numpy(state).type(FloatTensor) # convert to torch and normalize
            state_torch = state_torch.unsqueeze(0).type(FloatTensor)
            action = self.behavior_policy(state_torch)

            next_state, r, done = self.env.step(action)  # 0.03s

            if len(self.memory) == MEMORY_SIZE:
                self.memory.pop(0)
            self.memory.append((state, [action], [r], \
                                        next_state, [1-done])
                            )
            #print(len(self.memory))

            # 0.001s
            if POPULATE_FLAG:
                populate_num += 1
            # plot the average reward in PLOT_FREQ episodes
            Repi += r
            if done:
                step += 1
                R += Repi
                Repi = 0. # reset episode reward
            if step and self.steps_done > prev_plot:
                print('average rewards after {} train: {}' . format(self.steps_done-prev_plot, R/step))
                self.plotter.plot_train_rewards(R/step)
                # 2-3s
                R = 0.
                step = 0
                prev_plot = self.steps_done

        self.plotter.terminate()


import numpy as np
import os
import glob
import scipy.ndimage as image

# Get data
place = os.getcwd()
pattern_path = place + "/patterns/"

data = glob.glob(os.path.join(pattern_path, "*.jpg"))

patterns = []
targets = []
for path in data:
    #images = [image.imread(path, mode="RGB").astype(np.float) for path in data]
    img1 = image.imread(path, mode="RGB").astype(np.float)
    a = path.replace("pattern", "target")
    img2 = image.imread(a, mode="RGB").astype(np.float)
    patterns.append(img1)
    targets.append(img2)

patterns = np.array(patterns)
targets = np.array(targets)

np.savez("numpy_data", patterns=patterns, targets=targets)


# This script runs in the Regression Test CI.
# We build the current commit and compare it with the Master branch.
# If there is a diference of 10% in regression on any query the build breaks.
import statistics
import time
import subprocess
import math

def truncate(number, decimals=0):
    """
    Returns a value truncated to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer.")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more.")
    elif decimals == 0:
        return math.trunc(number)

    factor = 10.0 ** decimals
    return math.trunc(number * factor) / factor

def install_duck_master():
    output = subprocess.run(["pip", "install", "duckdb","--pre"], capture_output=True)
    if output.stderr:
        print ("pip install duckdb --pre FAILED")
        assert(0)

def uninstall_duck():
    output = subprocess.run(["pip", "uninstall", "-y","duckdb"], capture_output=True)
    if output.stderr:
        print ("pip uninstall -y duckdb FAILED")
        assert(0)

def install_duck_current():
    output = subprocess.run(["make","release","BUILD_PYTHON=1", "GEN=ninja"], capture_output=True)
    if output.stderr:
        print (" make release BUILD_PYTHON=1 GEN=ninja FAILED")
        assert(0)

def run_tpch_query(duckdb_conn,query_number):
    query_result = []
    for i in range(5):
        query = duckdb_conn.execute("select query from tpch_queries() where query_nr="+str(query_number)).fetchone()[0]
        start_time = time.time()
        result = duckdb_conn.execute(query)
        total_time = time.time() - start_time
        query_result.append(total_time)
    return statistics.median(query_result)

def run_tpch(repetitions):
    import duckdb
    duckdb_conn = duckdb.connect()
    duckdb_conn.execute("CALL dbgen(sf=1);")
    result_list = []
    for i in range (1,23):
        query_result_list = []
        for j in range(repetitions):
            query_result_list.append(run_tpch_query(duckdb_conn,i))
        result_list.append(query_result_list)
    return result_list

def run_benchmark(install_function,repetitions):
    install_function()
    result_list = run_tpch(repetitions)
    uninstall_duck()
    return result_list

# We want to run the regression tests 3x if a query fails (i.e., is slower than the one in the master these 3 times then it fails)
def regression_test(threshold):
    repetitions = 3
    master_time = run_benchmark(install_duck_master,repetitions)
    current_time = run_benchmark(install_duck_current,repetitions)

    # If the regression status is true, there was no regression
    regression_status = True
    for i in range(len(master_time)):
        # Query Ok means that in all runs at least once it finished below the threshold
        query_ok = False
        # Query Faster means that is always finished faster than the master (using threshold)
        query_faster = True
        for j in range (repetitions):
            if current_time[i][j] <= master_time[i][j] * (1+threshold):
                query_ok = True
            if current_time[i][j] > master_time[i][j] * (1-threshold):
                query_faster = False

        if not query_ok:
                regression_status = False
                print("Q"+ str(i+1) + " slow ("+ str(truncate(current_time[i][0],2)) + " vs " + str(truncate(master_time[i][0],2)) + "). ")
        if query_faster:
                print("Q"+ str(i+1) + " fast ("+ str(truncate(current_time[i][0],2)) + " vs " + str(truncate(master_time[i][0],2)) + "). ")

    if not regression_status:
        assert(0)

regression_test(0.1)


# import pytest
from click.testing import CliRunner
from mlencrypt_research import cli

# @pytest.mark.skip(reason="takes too long")
def test_single():
    runner = CliRunner()
    for ur in [
        'random-same',
        # 'random-different-A-B-E',
        # 'random-different-A-B',
        'hebbian',
        # assume that anti_hebbian and random_walk if hebbian works:
        # 'anti_hebbian',
        # 'random_walk',
    ]:
        for attack in [
            # 'none',  # assume that none works if geometric works
            'geometric',
            'probabilistic'
        ]:
            result = runner.invoke(
                cli.cli,
                ['single', '-ur', ur, '-a', attack],
                catch_exceptions=False
            )
            assert result.exit_code == 0
            assert not result.exception
            assert result.exit_code == 0

            result_b = runner.invoke(
                cli.cli,
                ['single', '-ur', ur, '-a', attack, '-b'],
                catch_exceptions=False
            )
            assert result_b.exit_code == 0
            assert not result_b.exception
            assert result_b.exit_code == 0

            with runner.isolated_filesystem():
                result_tb = runner.invoke(
                    cli.cli,
                    ['single', '-ur', ur, '-a', attack, '-tb'],
                    catch_exceptions=False
                )
                assert result_tb.exit_code == 0
                assert not result_tb.exception
                assert result_tb.exit_code == 0


import json
import tkinter as tk
from tkinter import font  as tkfont
from tkinter import messagebox
from tkinter import ttk
from Gui import *

class EditPlayer(tk.Frame):

        def __init__(self, parent, controller):
            tk.Frame.__init__(self, parent)
            self.controller = controller
            self.StartCount = 3
            self.Title = tk.Label(self,text = "Edit player details" ,font = controller.title_font)
            self.lblPlayer = ttk.Label(self,text = "Player: ")
            self.txtPlayer = ttk.Entry(self)
            self.getPlayerButton = tk.Button(self,text = "Get Players",command = self.GetPlayers)
            self.EditPlayerButton = tk.Button(self,text = "Edit Players",command = self.Edit_Players)
            self.BackButton= tk.Button(self, text="Back",command=lambda:self.BackButtonRun(controller))

            self.Title.config(background="#f4f8ff",fg = "#485e82",pady="5")
            self.Title.grid(row =0,column =0,columnspan = 4)
            self.lblPlayer.grid(row =1,column =0)
            self.txtPlayer.grid(row =1,column =1)
            self.getPlayerButton.grid(row =1,column =2)
            self.EditPlayerButton.grid(row =1,column =3)
            self.BackButton.grid(row =1,column = 4)

        def BackButtonRun(self,controller):
            global PagesViewed
            PagesViewed.pop()
            controller.show_frame(PagesViewed[-1])

        def GetPlayers(self):
            self.orderedList = []

            self.players = SystemToolKit.readFile("players.json")
            print(self.players)

            for i ,j in enumerate(self.players):
                self.orderedList.append(j)
                self.txtFirstName = ttk.Entry(self)
                self.txtLastName = ttk.Entry(self)
                self.txtPhoneNumber = ttk.Entry(self)
                self.txtAddress = ttk.Entry(self)
                self.txtPostcode = ttk.Entry(self)
                self.txtDateOfBirth = ttk.Entry(self)
                self.txtFirstName.grid(row = self.StartCount+i, column  =0 )
                self.txtLastName.grid(row = self.StartCount+i,column = 1)
                self.txtPhoneNumber.grid(row = self.StartCount+i, column  =2 )
                self.txtAddress.grid(row = self.StartCount + i, column  =3 )
                self.txtPostcode.grid(row = self.StartCount + i, column  =4 )
                self.txtDateOfBirth.grid(row = self.StartCount + i, column  =5 )
                self.txtFirstName.insert(0,self.players[j]["First name"])
                self.txtLastName.insert(0,self.players[j]["Last name"])
                self.txtPhoneNumber.insert(0,self.players[j]["Phone number"])
                self.txtAddress.insert(0,self.players[j]["Address"])
                self.txtPostcode.insert(0,self.players[j]["Post code"])
                self.txtDateOfBirth.insert(0,self.players[j]["Date of Birth"])
            self.orderedList = list(reversed(self.orderedList))

        def Edit_Players(self):

                count = 0

                data = []

                for i,j in enumerate(self.grid_slaves()):

                    if int(j.grid_info()["row"]) >= self.StartCount:

                        try:

                            data.append(j.get())
                            if len(data) == 6 :
                                data =  list(reversed(data))
                                self.players[self.orderedList[count]]["First name"] = data[0]
                                self.players[self.orderedList[count]]["Last name"] = data[1]
                                self.players[self.orderedList[count]]["Phone number"] = data[2]
                                self.players[self.orderedList[count]]["Address"] = data[3]
                                self.players[self.orderedList[count]]["Post code"] = data[4]
                                self.players[self.orderedList[count]]["Date of Birth"] = data[5]
                                count +=1
                                data = []

                        except :AttributeError

                with open('players.json', 'w') as fp:
                    json.dump(self.players,fp)

Controlled assesment 4/Controlled assesment 4/EditPlayer.py


#	-*- coding:utf-8 -*-
import xlwt
import xlrd
import csv
import re
import pickle

class subGraph():
	def __init__(self, graph, articleId, sentenceId, annotation):
		self.graph = graph
		self.articleId = articleId
		self.sentenceId = sentenceId
		self.annotation = annotation

	def __repr__(self):
		return "{}\t{}\t{}\t{}".format(self.articleId, self.sentenceId, self.graph, self.annotation)

def main():
	subgraphFilePath = "./PART-amr-release-1.0-training-proxy-subgraph.txt"
	annotationFilePath = "./annotation.xlsx"
	lino = 0
	subGraphs = []

	subgraph = open(subgraphFilePath,'r').read().split('-'*50+'\n')[2:]
	annotation = xlrd.open_workbook(annotationFilePath).sheets()[0]
	maxrows = annotation.nrows
	for s in subgraph:
		line = s.split('\n')
		index = re.search("id\s(\S+)\.(\d+)",line[0])	#/d can only match one digit
		aid = index.group(1)
		sid = index.group(2)
		for i in range(len(line)):
			if line[i].startswith('#'):
				continue
			else:
				break
		sg = '\n'.join(line[i:])
		if not sg:
			continue
		sg = sg.split('\n\n')[:-1]

		for e in sg:
			r = re.search("\((\d+)\)",e)
			if not r:
				print('sb')
			ind = int(r.group(1)) 	# a integer
			graph = e[r.end(1)+1:]
			try:
				while lino < maxrows and annotation.row(lino)[1].value != aid+'.'+sid:
					#print(annotation.row(lino)[1].value)
					lino += 1

				i = 1
				while lino + i < maxrows and ind != annotation.row(lino+i)[2].value:
					#print(annotation.row(lino+i)[2].value)
					i += 1
				if annotation.row(lino+i)[3].value == 'y' or annotation.row(lino+i)[3].value == 'yes':
					y = 1.0
				else:
					y = 0.0
			except IndexError:
				print(aid+'.'+sid)
			subGraphs.append(subGraph(graph,aid,sid,y))
	# print(subGraphs)
	with open('./tuple.pkl','wb') as p:		#the output file need to be open as wb if list
		pickle.dump(subGraphs,p)

if __name__ == '__main__':
	main()


from django.shortcuts import render,redirect,reverse
from util.myutil import authuser
from .form import SelfInfoForms
from django.http import JsonResponse,HttpResponse
from blogs.models import User, Category, Keyword, PTArticle
from verify.models import User as YH
from person.models import Article,Comment
from django.core.paginator import Paginator
from django.db import models
import os

# Create your views here.

@authuser
def index(request):
    if request.method == 'GET':
        email = request.session.get('email', None)
        obj = User.objects.filter(email=email).values('username', 'img').first()
        return render(request, "person/index.html", {'email': email, 'obj': obj})

# 个人资料管理
@authuser
def pim(request):
    email = request.session.get('email', None)
    if request.method == 'GET':
        obj = User.objects.filter(email=email).values('username', 'password', 'email', 'tel', 'img').first()
        # form = SelfInfoForms(obj)
        return render(request, "person/pim.html", {'email': email, 'obj': obj})
    if request.is_ajax():
        img = request.FILES.get('img', None)
        # name = os.path.join("media", 'user_img', img.name)
        if img:
            with open('upload/user_img/'+img.name, 'wb+') as f:
                for item in img.chunks():
                    f.write(item)
        return JsonResponse({'status': 'ok', 'src': '/'+os.path.join("media", 'user_img', img.name)})
    if request.method == 'POST':
        username = request.POST.get('username')
        tel = request.POST.get('tel')
        img = request.FILES.get('img', None)
        print(username)
        # img_name = os.path.join("upload", 'user_img', '') + img.name
        # if img:
        #     with open(img_name, 'wb+') as f:
        #         for item in img.chunks():
        #             f.write(item)
        if img:
            with open('upload/user_img/%s' % img.name, 'wb+') as destination:
                for chunk in img.chunks():
                    destination.write(chunk)
            User.objects.filter(email=email).update(username=username, tel=tel, img='/user_img/%s' % img.name)
        return redirect(reverse("person:pim"))

@authuser
def lianjie(request):
    if request.method == 'GET':
        email = request.session.get('email', None)
        return render(request, "person/index.html", {'email': email})

@authuser
def admin(request):
    if request.method == 'GET':
        email = request.session.get('email', None)
        return render(request, "person/index.html", {'email': email})

@authuser
def addArticle(request):
    email = request.session.get('email', None)
    if request.method == 'GET':
        types = Category.getALL()
        keys = Keyword.objects.all()
        return render(request, "person/addArticle.html", {'email': email, 'types': types, 'keys': keys})
    else:
        title = request.POST.get('title', None)
        category = request.POST.get('types', None)
        keys = request.POST.get('keys', None)
        con = request.POST.get('con', None)
        u_id = YH.objects.get(email=email)
        obj = Article(title=title, con=con, c_id=int(category), k_id=int(keys), a_id=int(u_id.id))
        obj.save()
        return redirect(reverse("person:index"))

@authuser
def myArticle(request):
    email = request.session.get('email', None)
    if request.method == 'GET':
        u_id = YH.objects.get(email=email)
        article = Article.objects.filter(a_id=int(u_id.id))
        paginator = Paginator(article, 2)  # Show 25 contacts per page
        page = request.GET.get('page')
        contacts = paginator.get_page(page)
        uName = User.objects.filter(email=email).values('username', 'img').first()
        return render(request, "person/myArticle.html", {'email': email, 'article': article, 'uName': uName, 'contacts': contacts})

def showArticle(request, item_id):
    email = request.session.get('email', None)
    if request.method == 'GET':
        u_id = YH.objects.get(email=email)
        first = Article.objects.filter(a_id=int(u_id.id)).first()
        last = Article.objects.filter(a_id=int(u_id.id)).last()
        article = Article.objects.get(id=item_id)
        article_id = Article.objects.filter(a_id=int(u_id.id))
        demo_id = []
        for item in article_id:
            demo_id.append(item.id)
        if str(first.id) == str(item_id):
            last_article = {'title': '到顶了，已无文章------', 'id': item_id}
        else:
            last_article = Article.objects.get(id=demo_id[demo_id.index(item_id)-1])
        if str(last.id) == str(item_id):
            next_article = {'title': '到底了，已无文章------', 'id': item_id}
        else:
            next_article = Article.objects.get(id=demo_id[demo_id.index(item_id)+1])
        uName = User.objects.filter(email=email).values('username', 'img').first()
        pl = Comment.objects.filter(article_id=item_id).order_by('-id')
        for i in pl:
            uname = User.objects.filter(id=i.users_id).first()
            i.users_id = uname
        return render(request, "person/showArticle.html", {'email': email, 'article': article, 'uName': uName, 'last_article': last_article, 'next_article': next_article, 'item_id': item_id, "pl": pl})

# 文章管理程序
def manageArticle(request):
    email = request.session.get("email", None)
    if request.method == 'GET':
        u_id = YH.objects.get(email=email)
        article = Article.objects.filter(a_id=int(u_id.id))
        return render(request, 'person/manageArticle.html', {"article": article})
    if request.is_ajax():
        del_arr = request.POST.getlist('del_arr', None)
        new_del = []
        for item in del_arr:
            new_del.append(int(item))
            Article.objects.filter(id=int(item)).delete()
        return JsonResponse({'status': 'ok'})


from django.conf.urls import include, url
from django.views.decorators.csrf import csrf_exempt
from rest_framework import routers

from .views import CampaignViewSet, DigitalSpotplanViewSet, OfflineSpotplanViewSet

router = routers.DefaultRouter()
router.register('campaign', CampaignViewSet, base_name='campaign')
router.register(
    'campaign/(?P<campaign_pk>\d+)/digital-spotplans',
    DigitalSpotplanViewSet,
    base_name='digital-spotplan')
router.register(
    'campaign/(?P<campaign_pk>\d+)/digital-spotplans/(?P<spotplan_pk>\d+)',
    DigitalSpotplanViewSet,
    base_name='digital-spotplan')
router.register(
    'campaign/(?P<campaign_pk>\d+)/offline-spotplans',
    OfflineSpotplanViewSet,
    base_name='offline-spotplan')

urlpatterns = router.urls
#  + [url('digital-campaign/upload$',CampaignUploadViewSet.as_view({'post':'create'}))]


import pygame
from pygame.locals import MOUSEBUTTONDOWN, MOUSEBUTTONUP, KEYDOWN, MOUSEMOTION, K_SPACE, K_LEFT, K_RIGHT, K_UP, K_DOWN
import glm
from glm import vec2, vec3
import math

from constants import BACKGROUND_COLOR
from objects import CelestialObject, VelocityArrow
from inputs import Inputs, Button
from scene import CelestialScene

from typing import TYPE_CHECKING
if TYPE_CHECKING:
    from main import App

class State:
    """
    State base class
    - Derive from this class and override methods for each state
    """
    def __init__(self, app, **kwargs):
        self.app: App = app
        self.name = ''

        self.suspended = False
        self.terminated = False

        self.scene = None

        if kwargs:
            raise ValueError(f"Some kwargs not consumed: {kwargs}")

    def update(self, delta_time):
        self.scene.update(delta_time)

    @property
    def scene_bg(self):
        return self.scene.background

    # @property
    # def sprites(self):
    #     return self.scene.content

class MenuState(State):
    """
    Menu State Class
    """
    def __init__(self, app, **kwargs):
        super().__init__(app, **kwargs)

    def update(self, delta_time):
        pass

class DrawState(State):
    """
    Draw State Class
    """
    def __init__(self, app, **kwargs):
        super().__init__(app, **kwargs)

        self.scene = CelestialScene(app)

        self.__static_input_funcs = []
        self.__dynamic_input_funcs = {}
        self.__build_inputs()

        self.curr_celestial = None
        self.curr_velo_arrow = None

        self.paused = False

    def __build_inputs(self):
        """
        Private function to bind inputs to functions
        """
        inputs = Inputs()

        inputs.register("new_object", Button(MOUSEBUTTONDOWN, 1))
        inputs.register("update", Button(MOUSEMOTION, 0))
        inputs.register("kill_all_objects", Button(KEYDOWN, K_SPACE))

        inputs.register("mleft", Button(KEYDOWN, K_LEFT))
        inputs.register("mright", Button(KEYDOWN, K_RIGHT))
        inputs.register("mup", Button(KEYDOWN, K_UP))
        inputs.register("mdown", Button(KEYDOWN, K_DOWN))
        inputs.register("zoomin", Button(MOUSEBUTTONDOWN, 4))
        inputs.register("zoomout", Button(MOUSEBUTTONDOWN, 5))

        self.app.inputs = inputs

        # Store functions to maintain weakrefs
        self.__dynamic_input_funcs["temp"] = self.app.inputs.inputs["new_object"].on_press(self.__new_object_stage1)

        # Add all the inputs that do not change
        #
        #
        # TODO: Needs a restructure so not referring down to scene
        #
        #
        self.__static_input_funcs.append(self.app.inputs.inputs["kill_all_objects"].on_press(self.scene.kill_all_objects))
        # self.__static_input_funcs.append(self.app.inputs.inputs["kill_all_objects"].on_press(self.__reset_new_object_stage))
        self.__static_input_funcs.append(self.app.inputs.inputs["mleft"].on_press_repeat(self.scene.move_cam_left, 0))
        self.__static_input_funcs.append(self.app.inputs.inputs["mright"].on_press_repeat(self.scene.move_cam_right, 0))
        self.__static_input_funcs.append(self.app.inputs.inputs["mup"].on_press_repeat(self.scene.move_cam_up, 0))
        self.__static_input_funcs.append(self.app.inputs.inputs["mdown"].on_press_repeat(self.scene.move_cam_down, 0))
        self.__static_input_funcs.append(self.app.inputs.inputs["zoomout"].on_press(self.scene.move_cam_out))
        self.__static_input_funcs.append(self.app.inputs.inputs["zoomin"].on_press(self.scene.move_cam_in))

    def __reset_new_object_stage(self):
        self.__dynamic_input_funcs["temp"] = self.app.inputs.inputs["new_object"].on_press(self.__new_object_stage1)
        self.__dynamic_input_funcs["temp2"] = None

    def __new_object_stage1(self):
        """
        Private function to create a new celestial object
        """
        if not self.curr_celestial:
            self.paused = True
            # Replace functions for next stage
            self.__dynamic_input_funcs["temp"] = self.app.inputs.inputs["new_object"].on_press_repeat(self.__new_object_stage1_cont, 0)
            self.__dynamic_input_funcs["temp2"] = self.app.inputs.inputs["new_object"].on_release(self.__new_object_stage2)

            center = pygame.mouse.get_pos()
            self.curr_celestial = CelestialObject(center) #set radius to 0 so the initializer will set PLANET_MIN_RADIUS

    def __new_object_stage1_cont(self):
        """
        Private function to update celestial body radius
        """
        if self.curr_celestial:
            center = self.curr_celestial.rect.center
            center_v = vec3(center[0], center[1], 0)
            curpos = pygame.mouse.get_pos()
            curpos_v = vec3(curpos[0], curpos[1], 0)
            self.curr_celestial.radius = math.floor(glm.distance(center_v, curpos_v))

    def __new_object_stage2(self):
        """
        Private function to complete creating a new celestial object
        """
        if self.curr_celestial:
            # Replace functions for next stage
            self.__dynamic_input_funcs["temp"] = self.app.inputs.inputs["update"].always(self.__new_object_stage2_cont)
            self.__dynamic_input_funcs["temp2"] = self.app.inputs.inputs["new_object"].on_press(self.__new_object_stage3)

            center = self.curr_celestial.rect.center
            center_v = vec3(center[0], center[1], 0)
            curpos = pygame.mouse.get_pos()
            curpos_v = vec3(curpos[0], curpos[1], 0)
            self.curr_celestial.radius = math.floor(glm.distance(center_v, curpos_v))
            self.setting_velocity = True

            center = self.curr_celestial.rect.center
            self.curr_velo_arrow = VelocityArrow(center)
            # self.scene.transient_objs.append(self.curr_velo_arrow)

    def __new_object_stage2_cont(self):
        """
        Private function to update velocity arrow
        """
        if self.curr_celestial and self.curr_velo_arrow:
            self.curr_velo_arrow.arrow_end = pygame.mouse.get_pos()

    def __new_object_stage3(self):
        """
        Private function to complete celestial body with velocity
        """
        if self.curr_celestial and self.curr_velo_arrow:
            # Replace functions to start over
            self.__dynamic_input_funcs["temp"] = self.app.inputs.inputs["new_object"].on_press(self.__new_object_stage1)
            self.__dynamic_input_funcs["temp2"] = None

            vc = self.curr_velo_arrow.velocity_component
            vel = glm.vec3(vc.x, -vc.y, 0)
            self.curr_celestial.velocity = vel

            # Add celestial to the scene with add_new_celestial function to have world_offset applied
            self.scene.add_new_celestial(self.curr_celestial)

            #Clean up
            self.curr_celestial = None # completed drawing the celestial
            self.curr_velo_arrow.dead = True
            self.curr_velo_arrow = None
            self.paused = False

    def update(self, delta_time):
        if not self.paused:
            # Update the current celestial being drawn
            if self.curr_celestial:
                self.curr_celestial.update(delta_time)

            # Update the scene
            self.scene.update(delta_time)

    def draw(self):
        # Blit currently drawing celestial
        if self.curr_celestial:
            self.app.screen.blit(self.curr_celestial.image, (self.curr_celestial.rect.x, self.curr_celestial.rect.y))

        if self.curr_velo_arrow:
            self.curr_velo_arrow.draw(self.app.screen)

        # Draw teh scene
        self.scene.draw(self.app.screen)


from panda3d.core import *
from toontown.toonbase.ToonBaseGlobal import *
from direct.gui.DirectGui import *
from panda3d.core import *
from direct.interval.IntervalGlobal import *
from direct.fsm import ClassicFSM, State
from direct.fsm import State
from direct.fsm import StateData
from toontown.toontowngui import TTDialog
from toontown.toonbase import ToontownGlobals
from toontown.toonbase import TTLocalizer
from direct.showbase import PythonUtil

class GolfKart(StateData.StateData):

    def __init__(self, safeZone, parentFSM, doneEvent, golfCourse):
        StateData.StateData.__init__(self, doneEvent)
        self.golfCourse = golfCourse
        self.fsm = ClassicFSM.ClassicFSM('GolfKart', [
            State.State('start',
                        self.enterStart,
                        self.exitStart, [
                            'requestBoard',
                            'trolleyHFA',
                            'trolleyTFA']),
            State.State('trolleyHFA',
                        self.enterTrolleyHFA,
                        self.exitTrolleyHFA, [
                            'final']),
            State.State('trolleyTFA',
                        self.enterTrolleyTFA,
                        self.exitTrolleyTFA, [
                            'final']),
            State.State('requestBoard',
                        self.enterRequestBoard,
                        self.exitRequestBoard, [
                            'boarding']),
            State.State('boarding',
                        self.enterBoarding,
                        self.exitBoarding, [
                            'boarded']),
            State.State('boarded',
                        self.enterBoarded,
                        self.exitBoarded, [
                            'requestExit',
                            'trolleyLeaving',
                            'final']),
            State.State('requestExit',
                        self.enterRequestExit,
                        self.exitRequestExit, [
                            'exiting',
                            'trolleyLeaving']),
            State.State('trolleyLeaving',
                        self.enterTrolleyLeaving,
                        self.exitTrolleyLeaving, [
                            'final']),
            State.State('exiting',
                        self.enterExiting,
                        self.exitExiting, [
                            'final']),
            State.State('final',
                        self.enterFinal,
                        self.exitFinal, [
                            'start'])],
            'start', 'final')
        self.parentFSM = parentFSM
        return None

    def load(self):
        self.parentFSM.getStateNamed('golfKartBlock').addChild(self.fsm)
        self.buttonModels = loader.loadModel('phase_3.5/models/gui/inventory_gui')
        self.upButton = self.buttonModels.find('**//InventoryButtonUp')
        self.downButton = self.buttonModels.find('**/InventoryButtonDown')
        self.rolloverButton = self.buttonModels.find('**/InventoryButtonRollover')

    def unload(self):
        self.parentFSM.getStateNamed('trolley').removeChild(self.fsm)
        del self.fsm
        del self.parentFSM
        self.buttonModels.removeNode()
        del self.buttonModels
        del self.upButton
        del self.downButton
        del self.rolloverButton

    def enter(self):
        self.fsm.enterInitialState()
        if base.localAvatar.hp > 0:
            messenger.send('enterGolfKartOK_%d' % self.golfCourse)
            self.fsm.request('requestBoard')
        else:
            self.fsm.request('trolleyHFA')
        return None

    def exit(self):
        self.ignoreAll()
        return None

    def enterStart(self):
        return None

    def exitStart(self):
        return None

    def enterTrolleyHFA(self):
        self.noTrolleyBox = TTDialog.TTGlobalDialog(message=TTLocalizer.TrolleyHFAMessage, doneEvent='noTrolleyAck', style=TTDialog.Acknowledge)
        self.noTrolleyBox.show()
        base.localAvatar.b_setAnimState('neutral', 1)
        self.accept('noTrolleyAck', self.__handleNoTrolleyAck)

    def exitTrolleyHFA(self):
        self.ignore('noTrolleyAck')
        self.noTrolleyBox.cleanup()
        del self.noTrolleyBox

    def enterTrolleyTFA(self):
        self.noTrolleyBox = TTDialog.TTGlobalDialog(message=TTLocalizer.TrolleyTFAMessage, doneEvent='noTrolleyAck', style=TTDialog.Acknowledge)
        self.noTrolleyBox.show()
        base.localAvatar.b_setAnimState('neutral', 1)
        self.accept('noTrolleyAck', self.__handleNoTrolleyAck)

    def exitTrolleyTFA(self):
        self.ignore('noTrolleyAck')
        self.noTrolleyBox.cleanup()
        del self.noTrolleyBox

    def __handleNoTrolleyAck(self):
        ntbDoneStatus = self.noTrolleyBox.doneStatus
        if ntbDoneStatus == 'ok':
            doneStatus = {}
            doneStatus['mode'] = 'reject'
            messenger.send(self.doneEvent, [doneStatus])
        else:
            self.notify.error('Unrecognized doneStatus: ' + str(ntbDoneStatus))

    def enterRequestBoard(self):
        return None

    def handleRejectBoard(self):
        doneStatus = {}
        doneStatus['mode'] = 'reject'
        messenger.send(self.doneEvent, [doneStatus])

    def exitRequestBoard(self):
        return None

    def enterBoarding(self, nodePath):
        camera.wrtReparentTo(nodePath)
        heading = PythonUtil.fitDestAngle2Src(camera.getH(nodePath), 180)
        self.cameraBoardTrack = LerpPosHprInterval(camera, 1.5, Point3(0, 18, 8), Point3(heading, -10, 0))
        self.cameraBoardTrack.start()
        return None

    def exitBoarding(self):
        self.ignore('boardedTrolley')
        return None

    def enterBoarded(self):
        self.enableExitButton()
        return None

    def exitBoarded(self):
        self.cameraBoardTrack.finish()
        self.disableExitButton()
        return None

    def enableExitButton(self):
        self.exitButton = DirectButton(relief=None, text=TTLocalizer.TrolleyHopOff, text_fg=(1, 1, 0.65, 1), text_pos=(0, -0.23), text_scale=0.8, image=(self.upButton, self.downButton, self.rolloverButton), image_color=(1, 0, 0, 1), image_scale=(20, 1, 11), pos=(0, 0, 0.8), scale=0.15, command=lambda self = self: self.fsm.request('requestExit'))
        return

    def disableExitButton(self):
        self.exitButton.destroy()

    def enterRequestExit(self):
        messenger.send('trolleyExitButton')
        return None

    def exitRequestExit(self):
        return None

    def enterTrolleyLeaving(self):
        self.acceptOnce('playMinigame', self.handlePlayMinigame)
        self.acceptOnce('playGolf', self.handlePlayGolf)
        return None

    def handlePlayMinigame(self, zoneId, minigameId):
        base.localAvatar.b_setParent(ToontownGlobals.SPHidden)
        doneStatus = {}
        doneStatus['mode'] = 'minigame'
        doneStatus['zoneId'] = zoneId
        doneStatus['minigameId'] = minigameId
        messenger.send(self.doneEvent, [doneStatus])

    def handlePlayGolf(self, zoneId, courseId):
        base.localAvatar.b_setParent(ToontownGlobals.SPHidden)
        doneStatus = {}
        doneStatus['mode'] = 'golfcourse'
        doneStatus['zoneId'] = zoneId
        doneStatus['courseId'] = courseId
        messenger.send(self.doneEvent, [doneStatus])

    def exitTrolleyLeaving(self):
        self.ignore('playMinigame')
        taskMgr.remove('leavingCamera')
        return None

    def enterExiting(self):
        return None

    def handleOffTrolley(self):
        doneStatus = {}
        doneStatus['mode'] = 'exit'
        messenger.send(self.doneEvent, [doneStatus])
        return None

    def exitExiting(self):
        return None

    def enterFinal(self):
        return None

    def exitFinal(self):
        return None


from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from feed.views import service_landing, temp_landing

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', temp_landing, name="landing"),
    path('feed/', include('feed.urls')),
    path('chat/', include('chat.urls')),
    path('accounts/', include('accounts.urls')),
    path('group/', include('group.urls')),
    path('mapsurfing/', include('mapsurfing.urls')),
    path('reservation/', include('reservation.urls'))
]

urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [
        ('newsbot', '0002_dictentry'),
    ]

    operations = [
        migrations.AddField(
            model_name='articleexample',
            name='bias_class',
            field=models.IntegerField(default=5),
            preserve_default=False,
        ),
    ]


'''
N 이하의 소수리스트를 찾는다.
N을 큰 소수부터 안 나눠질 때까지 나눈다.

'''

T = int(input())
from collections import defaultdict
import sys
input = sys.stdin.readline

def prime(N):
    is_prime = [0]*(N+1)
    primes =[]
    for i in range(2,N+1):
        if is_prime[i]:
            continue
        else:
            for j in range(i*i,N+1,i):
                is_prime[j] = 1

    for i in range(2,N+1):
        if not is_prime[i]:
            primes.append(i)

    return primes

for _ in range(T):
    N = int(input())
    primes = prime(N)

    record = defaultdict(int)

    for p in primes:
        if N < p:
            break

        while N >= p:
            if N%p == 0:
                N//=p
                record[p]+=1
            else:
                break

    for k,v in record.items():
        print(k,v)


from scipy.misc import imread
from scipy.ndimage import gaussian_filter
import numpy as np

data = imread(BIN_FILE)   # returns a numpy array
red = data[:,:,0]    # keep only red channel

homer = {
    "max": float(np.max(red)),
    "min": float(np.min(red)),
    "mean": float(np.mean(red))
}    # this is the return value of the script, must specify floats

blurred = gaussian_filter(red, 10)
write_file('blurred_homer.png', blurred)

Experiments in ipython notebooks/qatrack/py34/qaTrack_homer_test.py