#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""beta plane barotropic vorticity model.

This script uses a pseudospectral method to solve the barotropic vorticity
equation in two dimensions

    D/Dt[ω] = 0                                                             (1)

where ω = ξ + f.  ξ is local vorticity ∇ × u and f is global rotation.

Assuming an incompressible two-dimensional flow u = (u, v),
the streamfunction ψ = ∇ × (ψ êz) can be used to give (u,v)

    u = ∂/∂y[ψ]         v = -∂/∂x[ψ]                                        (2)

and therefore local vorticity can be given as a Poisson equation

    ξ = ∆ψ                                                                  (3)

where ∆ is the laplacian operator.  Since ∂/∂t[f] = 0 equation (1) can be
written in terms of the local vorticity

        D/Dt[ξ] + u·∇f = 0
    =>  D/Dt[ξ] = -vβ                                                       (4)

using the beta-plane approximation f = f0 + βy.  This can be written entirely
in terms of the streamfunction and this is the form that will be solved
numerically.

    D/Dt[∆ψ] = +β ∂/∂x[ψ]                                                   (5)

The spectral method defines ψ as a Fourier sum

    ψ = Σ A(t) exp(i (kx + ly))

and as such spatial derivatives can be calculated analytically

    ∂/∂x[ψ] = ikψ       ∂/∂y[ψ] = ilψ

The pseudospectral method will use the analytic derivatives to calculate
values for (u, v) which will then be used to evaluate nonlinear terms.

References:
* This code was developed based on a MATLAB script bvebb.m
  (Original source Dr. James Kent & Prof. John Thuburn)
* And the GFDL documentation for spectral barotropic models
  found here
  [http://www.gfdl.noaa.gov/cms-filesystem-action/user_files/pjp/barotropic.pdf]
* McWilliams Initial Condition inspired by pyqg [https://github.com/pyqg/pyqg]

Downloaded from github.com/jamesp/shallowwater
Modified for DAPPER by Patrick N. Raanes
"""

import matplotlib.pyplot as plt
import numpy as np

from numpy import pi, cos, sin
from numpy.fft import fftshift, fftfreq
from numpy.fft.fftpack import rfft2, irfft2

from tools.utils import Timer
from tools.misc import validate_int

### Configuration
nx = 256
ny = 256                        # numerical resolution
Lx = 1.0
Ly = 1.0                        # domain size [m]
ubar = 0.00                     # background zonal velocity  [m/s]
beta = 8.0                      # beta-plane f = f0 + βy     [1/s 1/m]
n_diss = 2.0                    # Small-scale dissipation of the form ∆^2n_diss,
                                # such that n_diss = 2 would be a ∆^4 hyperviscosity.
tau = 0.3                       # coefficient of dissipation
                                # (smaller => more dissipation)

#Poorly determined coefficients for forcing and dissipation
#r_rayleigh = (1./50000.)/np.sqrt(10.)
r_rayleigh = (1./500.)/np.sqrt(10.)
forcing_amp_factor=100.0/np.sqrt(1.)

### Function Definitions
def ft(phi):
    """Go from physical space to spectral space."""
    return rfft2(phi, axes=(-2, -1))

def ift(psi):
    """Go from spectral space to physical space."""
    return irfft2(psi, axes=(-2,-1))

def courant_number(psix, psiy, dx, dt):
    """Calculate the Courant Number given the velocity field and step size."""
    maxu = np.max(np.abs(psiy))
    maxv = np.max(np.abs(psix))
    maxvel = maxu + maxv
    return maxvel*dt/dx

def grad(phit):
    """Returns the spatial derivatives of a Fourier-transformed variable.
    Returns (∂/∂x[F[φ]], ∂/∂y[F[φ]]) i.e. (ik F[φ], il F[φ])"""
    global ik, il
    phixt = ik*phit        # d/dx F[φ] = ik F[φ]
    phiyt = il*phit        # d/dy F[φ] = il F[φ]
    return (phixt, phiyt)

def velocity(psit):
    """Returns the velocity field (u, v) from F[ψ]."""
    psixt, psiyt = grad(psit)
    psix = ift(psixt)    # v =   ∂/∂x[ψ]
    psiy = ift(psiyt)    # u = - ∂/∂y[ψ]
    return (-psiy, psix)

def spectral_variance(phit):
    global nx, ny
    var_density = 2.0 * np.abs(phit)**2 / (nx*ny)
    var_density[:,0] /= 2
    var_density[:,-1] /= 2
    return var_density.sum()

def anti_alias(phit):
    """Set the coefficients of wavenumbers > k_mask to be zero."""
    k_mask = (8./9.)*(nk+1)**2.
    phit[(np.abs(ksq/(dk*dk)) >= k_mask)] = 0.0

def high_wn_filter(phit):
    """Applies the high wavenumber filter of smith et al 2002"""
    filter_dec = -np.log(1.+2.*pi/nk)/((nk-kcut)**filter_exp)
    filter_idx = np.abs(ksq/(dk*dk)) >= kcut**2.
    phit[filter_idx] *= exp(filter_dec*(np.sqrt(ksq[filter_idx]/(dk*dk))-kcut)**filter_exp)

def forcing_spatial_mask(phi):
   """TODO: Make spatial mask for forcing"""
   phi[idx_sp_mask] *= 0.

_prhs, _pprhs  = 0.0, 0.0  # previous two right hand sides
def adams_bashforth(zt, rhs, dt):
    """Take a single step forward in time using Adams-Bashforth 3."""
    global step, _prhs, _pprhs
    #print("step:",step)
    if step is 0:
        # forward euler
        dt1 = dt
        dt2 = 0.0
        dt3 = 0.0
    elif step is 1:
        # AB2 at step 2
        dt1 = 1.5*dt
        dt2 = -0.5*dt
        dt3 = 0.0
    else:
        # AB3 from step 3 on
        dt1 = 23./12.*dt
        dt2 = -16./12.*dt
        dt3 = 5./12.*dt

    newzt = zt + dt1*rhs + dt2*_prhs + dt3*_pprhs
    _pprhs = _prhs
    _prhs  = rhs
    return newzt

## SETUP

### Physical Domain
dx = Lx / nx
dy = Ly / ny
#dt = 0.4 * 16.0 / nx          # choose an initial dt. This will change
                              # as the simulation progresses to maintain
                              # numerical stability
# Time params
t            = 0.0
tmax         = 10000
dt           = 0.1
PLOT_EVERY_S = 0.2
tplot        = t + PLOT_EVERY_S
FIRST        = True

# Make meshgrid of y coordinates
y = np.linspace(0, Ly, num=ny)
y_arr = np.flipud(np.tile(y,(nx,1)).transpose())

### Spectral Domain
nl = validate_int(ny)
nk = validate_int(nx/2 + 1)
dk = 2.0*pi/Lx
dl = 2.0*pi/Ly
# calculate the wavenumbers [1/m]
# The real FT has half the number of wavenumbers in one direction:
# FT_x[real]    -> complex : 1/2 as many complex numbers needed as real signal
# FT_y[complex] -> complex : After the first transform has been done the signal
# is complex, therefore the transformed domain in second dimension is same size
# as it is in euclidean space.
# Therefore FT[(nx, ny)] -> (nx/2, ny)
# The 2D Inverse transform returns a real-only domain (nx, ny)
k = dk*np.arange(0, nk)     [np.newaxis, :]
l = dl*fftfreq(nl, d=1.0/nl)[:, np.newaxis]

ksq = k**2 + l**2               # squared wavenums (yields laplacian)
ksq[ksq == 0] = 1.0             # avoid divide by zero - set ksq = 1 at zero wavenum
rksq = 1.0 / ksq                # reciprocal 1/(k^2 + l^2)

ik = 1j*k                       # wavenumber mul. imaginary unit is useful
il = 1j*l                       # for calculating derivatives

## Dissipation & Spectral Filters
# Use ∆^2n_diss hyperviscosity to diffuse at small scales
# (i.e. n_diss = 2 would be ∆^4)
# use the x-dimension for reference scale values
nu = ((Lx/(np.floor(nx/3)*2.0*pi))**(2*n_diss))/tau

# High wavenumber filter coefficients.
# Any waves with wavenumber below kcut are not dissipated at all.
filter_exp = 8.
kcut = 30.

# Spectral Filter as per [Arbic and Flierl, 2003]
wvx = np.sqrt((k*dx)**2 + (l*dy)**2)
spectral_filter = exp(-23.6*(wvx-0.65*pi)**4)
spectral_filter[wvx <= 0.65*pi] = 1.0

# ζ and F[ζ] arrays
z  = np.zeros((ny, nx), dtype=np.float64)
zt = np.zeros((nl, nk), dtype=np.complex128)

### Diagnostic arrays
time_arr       = np.zeros(1)
tot_energy_arr = np.zeros(1)

amp = 636

def integrator(xt,t0,dt_tot):

  t = t0

  nsteps = validate_int(dt_tot/dt)
  if nsteps < 10:
    raise Warning('dt_tot not long enough to properly bootstrap\
    multiSTEP (adams-bashford) integrator\
    (and relying solely on Euler is too unstable).')

  global step, _prhs, _pprhs
  step = _prhs = _pprhs = 0

  for k in range(nsteps):
    # calculate derivatives in spectral space
    psit         = -rksq * xt # F[ψ] = - F[ζ] / (k^2 + l^2)
    psixt, psiyt = grad(psit)
    zxt, zyt     = grad(xt)

    # transform back to physical space for pseudospectral part
    z[:] = ift(xt)
    psix = ift(psixt)
    psiy = ift(psiyt)
    zx   = ift(zxt)
    zy   = ift(zyt)

    # Non-linear: calculate the Jacobian in real space
    # and then transform back to spectral space
    jac  = psix * zy - psiy * zx + ubar * zx
    jact = ft(jac)

    # apply forcing in spectral space by exciting certain wavenumbers
    # (could also apply some real-space forcing and then convert
    # into spectral space before adding to rhs)
    rstream     = np.random.RandomState(int(t*10000))
    forcet      = np.zeros_like(ksq, dtype=complex)
    idx         = (14 < np.sqrt(ksq)/dk) & (np.sqrt(ksq)/dk < 20)
    numr        = ksq.shape[0]*ksq.shape[1]
    r_amp       = rstream.rand(numr).reshape(ksq.shape)[idx]
    r_phs       = rstream.rand(numr).reshape(ksq.shape)[idx]
    forcet[idx] = 0.5*amp*(r_amp - 0.5)*exp(1j*2.*pi*r_phs)

    c = 999

    # take a timestep and diffuse
    rhs   = -jact + beta*psixt - xt*r_rayleigh + forcet
    xt[:] = adams_bashforth(xt, rhs, dt)

    #anti_alias
    anti_alias(xt)

    #high_wavenumber_filter
    high_wn_filter(xt)

    t    += dt
    step += 1

    global tplot, time_arr, tot_energy_arr, FIRST
    if t > tplot and __name__ == "__main__":
        with Timer('Diagnostics'):
          urms=np.sqrt(np.mean(psix**2 + psiy**2))
          rhines_scale = np.sqrt(urms/beta)
          tot_energy=0.5*urms**2.
          epsilon = 2 * r_rayleigh * tot_energy
          l_epsilon = (epsilon / beta**3.)**(1./5.)

          time_arr=np.append(time_arr,t)
          tot_energy_arr=np.append(tot_energy_arr,tot_energy)

          force=ift(forcet)

        if FIRST:
          #plt.ion() # plot in realtime
          plt.figure(figsize=(15, 12))
          #plt.clf()
          FIRST = False

          with Timer('vorticity (FIRST)'):
            axv = plt.subplot2grid((2,3), (0, 0), colspan=2, rowspan=2)
            vh = plt.imshow(z, extent=[0, Lx, 0, Ly], cmap=plt.cm.YlGnBu)
            plt.xlabel('x')
            plt.ylabel('y')
            zmax = np.max(np.abs(z))
            plt.clim(-zmax,zmax)
            plt.colorbar(orientation='horizontal')
            plt.title('Rel. vorticity at {:.2f}s dt={:.2f}'.format(t, dt))

          with Timer('Forcing'):
            plt.subplot2grid((2,3), (0, 2))
            fh = plt.imshow(force, extent=[0, Lx, 0, Ly], cmap=plt.cm.YlGnBu)
            plt.xlabel('x')
            plt.ylabel('y')
            forcemax = np.max(np.abs(force))
            plt.clim(-forcemax,forcemax)
            plt.colorbar(orientation='horizontal')
            plt.title('Forcing')

          with Timer('Energy'):
            axl = plt.subplot2grid((2,3), (1, 2))
            lh = plt.plot(time_arr, tot_energy_arr)[0]
            plt.xlabel('Time')
            plt.ylabel('Total Energy')

            plt.subplots_adjust(wspace=0.4,left=0.05,right=0.97)
        else:
          with Timer('Update'):
            vh.set_data(z)
            axv.set_title('Rel. vorticity at {:.2f}s dt={:.2f}'.format(t, dt))
            fh.set_data(force)
            lh.set_data(time_arr, tot_energy_arr)
            axl.set_xlim(right=t)
            #axl.set_ylim((np.min(tot_energy_arr), np.max(tot_energy_arr)))
            axl.set_ylim((0.0000043, 0.00000435))
            axl.ticklabel_format(style='sci',useOffset=4e-6,axis='y')

        plt.pause(0.01)
        print('[{:5d}] {:.2f} Max z: {:2.2f} c={:.2f} dt={:.2f} rh_s={:.3f} l_eps={:.3f} ratio={:2.2f} urms={:2.2f}'.format(
            step, t, np.max(z), c, dt, rhines_scale, l_epsilon, rhines_scale/l_epsilon, urms))
        tplot = t + PLOT_EVERY_S

  return

if __name__ == "__main__":
  z = np.load('mods/Barotropic/z0_1.npz')['z']
  z = z[:ny,:nx]
  zt[:] = ft (z)

  integrator(zt,t,tmax)

### Initial Condition
#   # The McWilliams Initial Condition from [McWilliams - J. Fluid Mech. (1984)]
#   ck   = np.zeros_like(ksq)
#   ck   = np.sqrt(ksq + (1.0 + (ksq/36.0)**2))**-1
#   piit = rand(*ksq.shape)*ck + 1j*rand(*ksq.shape)*ck
#
#   pii  = ift(piit)
#   pii  = pii - pii.mean()
#   piit = ft(pii)
#   KE   = spectral_variance(piit*np.sqrt(ksq)*spectral_filter)
#
#   qit  = -ksq * piit / np.sqrt(KE)
#   qi   = ift(qit)
#   z[:] = qi
#
#   # calc a reasonable forcing amplitude
#   amp = forcing_amp_factor * np.max(np.abs(qi))
#
#   # initialise the transformed ζ
#   zt[:] = ft (z)
#   anti_alias (zt)
#   z[:]  = ift(zt)
#
#   # initialise the storage arrays
#   time_arr[0]=t
#
#   psit = -rksq * zt         # F[ψ] = - F[ζ] / (k^2 + l^2)
#   psixt, psiyt = grad(psit)
#   psix = ift(psixt)
#   psiy = ift(psiyt)
#
#   urms              = np.sqrt(np.mean(psix**2 + psiy**2))
#   tot_energy        = 0.5*urms**2.
#   tot_energy_arr[0] = tot_energy


import json
import os
import subprocess
import sys

from utils import listRecursive

def remote_1(args):
    scriptDir = "/computation/enigma_scripts"
    scriptName = "metaanalysis_AllComps2020.R"
    RScriptDir = "/usr/bin/Rscript"

    baseDir = args["state"]["baseDirectory"]
    transferDir = args["state"]["transferDirectory"]
    outputDir = args["state"]["outputDirectory"]

    site_list = os.path.join(outputDir, "site_list.txt")

    with open(site_list, 'w') as fh:
        for site in args["input"]:
            fh.write('%s\n' % site)

    regr_args = [
        RScriptDir,
        os.path.join(scriptDir, scriptName), baseDir, outputDir, site_list
    ]

    subprocess.call(regr_args,
                    stdout=subprocess.DEVNULL,
                    stderr=subprocess.DEVNULL)

    # Copying local results to transfer directory
    os.system("cp -rf " + baseDir + "/* " + transferDir)

    # Copying meta analysis results to transfer directory
    os.system("cp -rf " + outputDir + "/* " + transferDir)

    computation_output = {
        "output": {},
        # should be a list of files created -ross
        "success": True
    }
    return json.dumps(computation_output)

if __name__ == '__main__':

    parsed_args = json.loads(sys.stdin.read())
    phase_key = list(listRecursive(parsed_args, 'computation_phase'))

    if 'local_1' in phase_key:
        computation_output = remote_1(parsed_args)
        sys.stdout.write(computation_output)
    else:
        raise ValueError("Error occurred at Remote")


import cv2

cap = cv2.VideoCapture(0)

def getImg(display=False,size=[960, 480]):
    _, img = cap.read()
    cap.set(cv2.CAP_PROP_FPS,60)#test
    fps = int(cap.get(60)) #j
    img = cv2.resize(img, (size[0], size[1]))
    if display:
        cv2.imshow('IMG', img)
    return img

if __name__ == '__main__':
    i = 1
    while True:
        img = getImg(True)
        if i<200:
            i+=1
            cv2.imwrite('/home/pi/auto_car/LaneDetection/vid/vid'+str(i)+'.png', img)
            print(i)
        if cv2.waitKey(1) and 0xFF == ord('q'):
            break


from tornado.options import define, options
import tornado.concurrent
from trending_handler import IndexHandler, RepositoryHandler, RepositoryLanguageHandler \
    , DeveloperHandler, DeveloperLanguageHandler

define('port', default=8123, type=int)

app = tornado.web.Application([
    (r'/', IndexHandler),
    (r'/repo/', RepositoryHandler),
    (r'/repo', RepositoryHandler),
    (r'/repo/(.+)', RepositoryLanguageHandler),
    (r'/developer/', DeveloperHandler),
    (r'/developer', DeveloperHandler),
    (r'/developer/(.+)', DeveloperLanguageHandler),
])

if __name__ == '__main__':
    tornado.options.parse_command_line()
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.current().start()


__author__ = 'Vladimir Bolshakov <vovanbo@gmail.com>'

import json
import copy
from collections import OrderedDict
from typing import Callable, List, MutableMapping, Optional, Union

import ttree.utils
from ttree.common import ASCIIMode, TraversalMode
from ttree.exceptions import (
    NodeNotFound, MultipleRoots, DuplicatedNode, LinkPastRootNode, LoopError
)
from .node import Node

class Tree(OrderedDict):
    """
    The Tree object defines the tree-like structure based on
    :class:`Node` objects. A new tree can be created from scratch without any
    parameter or a shallow/deep copy of another tree. When ``deepcopy=True``,
    a deepcopy operation is performed on feeding ``tree`` parameter and
    *more memory is required to create the tree*.
    """
    def __init__(self, tree: 'Tree' = None, deepcopy: bool = False):
        """Initiate a new tree or copy another tree with a shallow or
        deepcopy copy.
        """
        super(Tree, self).__init__()

        #: id of the root node
        self.root = None

        if tree is not None:
            if not isinstance(tree, Tree):
                raise TypeError('Tree instance is required.')

            self.root = tree.root
            self.__merge_tree(tree, deepcopy)

    def __str__(self) -> str:
        return ttree.utils.print_tree(self, ascii_mode='simple')

    def __getitem__(self, item):
        try:
            return super(Tree, self).__getitem__(item)
        except KeyError:
            raise NodeNotFound(f"Node '{item}' is not in the tree")

    def __setitem__(self, key, value, **kwargs):
        super(Tree, self).__setitem__(key, value, **kwargs)
        if isinstance(value, Node):
            value._tree = self

    def __delitem__(self, key, **kwargs):
        if key in self:
            self[key]._tree = None
        super(Tree, self).__delitem__(key, **kwargs)

    def __merge_tree(self, other: 'Tree', deepcopy: bool = False):
        if deepcopy:
            for node_id in other:
                self[node_id] = copy.deepcopy(other[node_id])
        else:
            self.update(other)

    @property
    def paths_to_leaves(self):
        """
        Use this property to get the identifiers allowing to go from the root
        nodes to each leaf.
        Return a list of list of identifiers, root being not omitted.

        For example:

        .. code-block:: text

            Harry
            |___ Bill
            |___ Jane
            |    |___ Diane
            |         |___ George
            |              |___ Jill
            |         |___ Mary
            |    |___ Mark

        Result:

        .. code-block:: python3

            [['harry', 'jane', 'diane', 'mary'],
             ['harry', 'jane', 'mark'],
             ['harry', 'jane', 'diane', 'george', 'jill'],
             ['harry', 'bill']]
        """
        return [[n for n in self.rsearch(l.id)][::-1] for l in self.leaves()]

    def add_node(self, node: Node, parent: Node = None):
        """
        Add a new node to tree.

        Add a new node object to the tree and make the parent as the root
        by default.
        """
        if not isinstance(node, Node):
            raise TypeError('First parameter must be instance of Node.')

        if node.id in self:
            raise DuplicatedNode(f"Node with ID '{node.id}' "
                                 f"is already exists in tree.")

        pid = parent.id if isinstance(parent, Node) else parent

        if pid is None:
            if self.root is not None:
                raise MultipleRoots('A tree takes one root merely.')

            self.root = node.id
        elif pid not in self:
            raise NodeNotFound(f"Parent node '{pid}' is not in the tree")

        self[node.id] = node
        if pid in self:
            self[pid].add_child(node.id)
        self[node.id].parent = pid

    def children(self, node_id) -> List[Node]:
        """
        Return the children (Node) list of ``node_id``.

        Empty list is returned if ``node_id`` does not exist.
        """
        return [self[i] for i in self.is_branch(node_id)]

    def create_node(self, *args, parent=None, node_cls=Node, **kwargs):
        """
        Create a new node and add it to this tree.

        If ``id`` is absent, a UUID will be generated automatically.
        """
        if not issubclass(node_cls, Node):
            raise ValueError('node_cls must be a subclass of Node.')

        node = node_cls(*args, **kwargs)
        self.add_node(node, parent)
        return node

    def depth(self, node=None) -> int:
        """
        Get the maximum level of this tree or the level of the given node

        .. note ::

            The parameter is node instance rather than node_identifier.

        :param ~ttree.Node node:
        :return: Depth level
        """
        result = 0
        if node is None:
            # Get maximum level of this tree
            for leaf in self.leaves():
                level = self.level(leaf.id)
                result = level if level >= result else result
        else:
            # Get level of the given node
            node_id = node.id if isinstance(node, Node) else node

            if node_id not in self:
                raise NodeNotFound(f"Node '{node_id}' is not in the tree")

            result = self.level(node_id)
        return result

    def expand_tree(self, node_id=None,
                    mode: Union[TraversalMode, str] = TraversalMode.DEPTH,
                    filtering: Callable[[Node], bool] = None,
                    key=None, reverse: bool = False):
        """
        Traverse the tree nodes with different modes.

        ``node_id`` refers to the expanding point to start; ``mode`` refers
        to the search mode (Tree.DEPTH, Tree.WIDTH). ``filter`` refers to the
        function of one variable to act on the :class:`Node` object.
        In this manner, the traversing will not continue to following children
        of node whose condition does not pass the filter. ``key``, ``reverse``
        are present to sort :class:Node objects at the same level.

        Python generator. Loosly based on an algorithm from
        'Essential LISP' by John R. Anderson, Albert T. Corbett, and
        Brian J. Reiser, page 239-241

        UPDATE: the @filtering function is performed on Node object during
        traversing. In this manner, the traversing will not continue to
        following children of node whose condition does not pass the filter.

        UPDATE: the @key and @reverse are present to sort nodes at each
        level.
        """
        node_id = self.root if node_id is None else node_id

        if node_id not in self:
            raise NodeNotFound(f"Node '{node_id}' is not in the tree")

        if filtering is not None and not callable(filtering):
            raise TypeError('Filtering must be callable.')

        mode = mode if isinstance(mode, TraversalMode) else TraversalMode(mode)

        if filtering is not None and not filtering(self[node_id]):
            return

        yield node_id
        queue = (self[i] for i in self[node_id].children)

        if filtering is not None:
            queue = filter(filtering, queue)

        if mode in (TraversalMode.DEPTH, TraversalMode.WIDTH):
            queue = sorted(queue, key=key, reverse=reverse)
            while queue:
                yield queue[0].id
                expansion = sorted(
                    filter(filtering, (self[i] for i in queue[0].children)),
                    key=key, reverse=reverse
                )

                if mode is TraversalMode.DEPTH:
                    queue = expansion + queue[1:]  # depth-first
                elif mode is TraversalMode.WIDTH:
                    queue = queue[1:] + expansion  # width-first

        elif mode is TraversalMode.ZIGZAG:
            # Suggested by Ilya Kuprik (ilya-spy@ynadex.ru).
            stack_fw = []
            queue = list(queue)
            queue.reverse()
            stack = stack_bw = queue
            direction = False
            while stack:
                expansion = filter(filtering,
                                   (self[i] for i in stack[0].children))
                yield stack.pop(0).id
                expansion = list(expansion)
                if direction:
                    expansion.reverse()
                    stack_bw = expansion + stack_bw
                else:
                    stack_fw = expansion + stack_fw
                if not stack:
                    direction = not direction
                    stack = stack_fw if direction else stack_bw

    def is_branch(self, node_id):
        """
        Get the children (only sons) list of the node with ID == node_id.

        Empty list is returned if node_id does not exist
        """
        if node_id is None:
            raise ValueError("First parameter can't be None")

        return self[node_id].children

    def leaves(self, node_id=None):
        """Get leaves from given node."""
        if node_id is None:
            return [n for n in self.values() if n.is_leaf]

        return [self[n] for n in self.expand_tree(node_id) if self[n].is_leaf]

    def level(self, node_id, filtering: Callable[[Node], bool] = None):
        """
        Get the node level in this tree.
        The level is an integer starting with '0' at the root.
        In other words, the root lives at level '0';

        Update: @filtering params is added to calculate level passing
        exclusive nodes.
        """
        return len([n for n in self.rsearch(node_id, filtering)]) - 1

    def link_past_node(self, node_id):
        """
        Remove a node and link its children to its parent.

        Root is not allowed.

        For example, if we have a -> b -> c and delete node b, we are left
        with a -> c
        """
        if self.root == node_id:
            raise LinkPastRootNode('Cannot link past the root node, '
                                   'delete it with remove_node()')

        # Get the parent of the node we are linking past
        parent = self[self[node_id].parent]

        # Set the children of the node to the parent
        for child in self[node_id].children:
            self[child].parent = parent.id

        # Link the children to the parent
        parent.children += self[node_id].children
        # Delete the node
        parent.remove_child(node_id)
        del self[node_id]

    def move_node(self, source, destination):
        """
        Move node (source) from its parent to another parent (destination).
        """
        if source not in self or destination not in self:
            raise NodeNotFound

        if self.is_ancestor(source, destination):
            raise LoopError

        parent = self[source].parent
        self[parent].remove_child(source)
        self[destination].add_child(source)
        self[source].parent = destination

    def is_ancestor(self, ancestor, grandchild) -> bool:
        parent = self[grandchild].parent
        child = grandchild

        while parent is not None:
            if parent == ancestor:
                return True

            child = self[child].parent
            parent = self[child].parent

        return False

    def parent(self, node_id) -> Optional[Node]:
        """
        Obtain specific node's parent (Node instance).

        Return None if the parent is None or does not exist in the tree.
        """
        pid = self[node_id].parent
        if pid is None or pid not in self:
            return None

        return self[pid]

    def paste(self, node_id, new_tree: 'Tree', deepcopy: bool = False):
        """
        Paste a new tree to an existing tree, with ``node_id`` becoming the
        parent of the root of this new tree.

        Update: add @deepcopy of pasted tree.
        """
        if not isinstance(new_tree, Tree):
            raise TypeError('Instance of Tree is required as '
                            '"new_tree" parameter.')

        if node_id is None:
            raise ValueError('First parameter can not be None')

        if node_id not in self:
            raise NodeNotFound(f"Node '{node_id}' is not in the tree")

        set_joint = set(new_tree) & set(self)  # joint keys
        if set_joint:
            # TODO: a deprecated routine is needed to avoid exception
            raise ValueError(f'Duplicated nodes {list(set_joint)} exists.')

        self.__merge_tree(new_tree, deepcopy)

        self[node_id].add_child(new_tree.root)
        self[new_tree.root].parent = node_id

    def remove_node(self, node_id) -> int:
        """
        Remove a node indicated by 'id'; all the successors are
        removed as well.

        Return the number of removed nodes.
        """
        if node_id is None:
            return 0

        parent = self[node_id].parent

        removed = [n for n in self.expand_tree(node_id)]
        for id_ in removed:
            del self[id_]

        # Update its parent info
        if parent:
            self[parent].remove_child(node_id)

        return len(removed)

    def remove_subtree(self, node_id) -> 'Tree':
        """
        Return a subtree deleted from this tree. If node_id is None, an
        empty tree is returned.
        For the original tree, this method is similar to
        `remove_node(self,node_id)`, because given node and its children
        are removed from the original tree in both methods.
        For the returned value and performance, these two methods are
        different:

            `remove_node` returns the number of deleted nodes;
            `remove_subtree` returns a subtree of deleted nodes;

        You are always suggested to use `remove_node` if your only to
        delete nodes from a tree, as the other one need memory
        allocation to store the new tree.
        """
        subtree = Tree()
        if node_id is None:
            return subtree

        subtree.root = node_id
        parent = self[node_id].parent
        self[node_id].parent = None  # reset root parent for the new tree

        removed = [n for n in self.expand_tree(node_id)]
        for id_ in removed:
            subtree[id_] = self.pop(id_)

        # Update its parent info
        self[parent].remove_child(node_id)
        return subtree

    def rsearch(self, node_id, filtering: Callable[[Node], bool] = None):
        """
        Search the tree from ``node_id`` to the root along links reservedly.

        Parameter ``filter`` refers to the function of one variable to act
        on the :class:`Node` object.
        """
        if node_id is None:
            return

        if node_id not in self:
            raise NodeNotFound(f"Node '{node_id}' is not in the tree")

        if filtering is not None and not callable(filtering):
            raise TypeError('Filtering must be a callable.')

        current = node_id
        while current is not None:
            if filtering is None or filtering(self[current]):
                yield current

            # subtree() hasn't update the parent
            current = self[current].parent if self.root != current else None

    def save2file(self, filename, node_id=None, id_hidden=True,
                  filtering=None, key=None, reverse=False,
                  ascii_mode=ASCIIMode.ex, data_property=None):
        """
        Save the tree into file for offline analysis.

        :param filename: Export file name
        :param node_id: Traversal root node ID
        :param id_hidden: Is ID hidden?
        :param filtering: Filtering callable
        :param key: Sorting key callable
        :param reverse: Reverse mode?
        :param ascii_mode: ASCII mode
        :param data_property: Data property name
        """
        with open(filename, 'ab') as fp:
            ttree.utils.print_tree(
                self, node_id, id_hidden, filtering, key, reverse,
                ascii_mode, data_property, func=lambda n: fp.write(n + b'\n')
            )

    def print(self, node_id=None, id_hidden=True, filtering=None,
              key=None, reverse=False, ascii_mode=ASCIIMode.ex,
              data_property=None):
        """
        Print the tree structure in hierarchy style.

        You have three ways to output your tree data, i.e., stdout with
        ``print()``, plain text file with ``save2file()``, and json string
        with ``to_json()``. The former two use the same backend to generate
        a string of tree structure in a text graph.

        :param node_id: refers to the expanding point to start
        :param id_hidden: refers to hiding the node ID when printing
        :param filtering: refers to the function of one variable to act on the
            :class:`Node` object. In this manner, the traversing will not
            continue to following children of node whose condition does
            not pass the filter.
        :param key: are present to sort :class:`Node` object in the same level
        :param reverse: reverse mode
        :param ascii_mode: ASCII mode
        :param data_property: refers to the property on the node data object
            to be printed.
        """
        try:
            ttree.utils.print_tree(
                self, node_id, id_hidden, filtering,
                key, reverse, ascii_mode, data_property,
                func=print
            )
        except NodeNotFound:
            print('Tree is empty')

    def siblings(self, node_id) -> List[Node]:
        """
        Return the siblings of given ``node_id``.

        If ``node_id`` is root or there are no siblings, an empty list
        is returned.
        """
        siblings = []

        if node_id != self.root:
            pid = self[node_id].parent
            siblings = [self[c] for c in self[pid].children if c != node_id]

        return siblings

    def size(self, level: int = None) -> int:
        """
        Get the number of nodes of the whole tree if @level is not
        given. Otherwise, the total number of nodes at specific level
        is returned.

        @param level The level number in the tree. It must be between
        [0, tree.depth].

        Otherwise, InvalidLevelNumber exception will be raised.
        """
        if level is None:
            return len(self)

        if not isinstance(level, int):
            raise TypeError(f"Level should be an integer instead "
                            f"of '{type(level)}'")

        return len(
            [node for node in self.values()
             if self.level(node.id) == level]
        )

    def subtree(self, node_id) -> 'Tree':
        """
        Return a shallow COPY of subtree with node_id being the new root.
        If node_id is None, return an empty tree.
        If you are looking for a deepcopy, please create a new tree
        with this shallow copy,

        e.g.
            new_tree = Tree(t.subtree(t.root), deep=True)

        This line creates a deep copy of the entire tree.
        """
        result = self.__class__()
        if node_id is None:
            return result

        if node_id not in self:
            raise NodeNotFound(f"Node '{node_id}' is not in the tree")

        result.root = node_id
        for subtree_node in self.expand_tree(node_id):
            result[self[subtree_node].id] = self[subtree_node]

        return result

    def to_dict(self, node_id=None, key=None, sort=True, reverse=False,
                with_data=False) -> MutableMapping:
        """transform self into a dict"""

        node_id = self.root if node_id is None else node_id
        node_tag = self[node_id].tag
        result = {node_tag: {'children': []}}
        if with_data:
            result[node_tag]['data'] = self[node_id].data

        if self[node_id].expanded:
            queue = (self[i] for i in self[node_id].children)
            if sort:
                sort_options = {'reverse': reverse}
                if key is not None:
                    sort_options['key'] = key
                queue = sorted(queue, **sort_options)

            result[node_tag]['children'] = [
                self.to_dict(n.id, with_data=with_data,
                             sort=sort, reverse=reverse)
                for n in queue
            ]

            if not result[node_tag]['children']:
                result = (
                    self[node_id].tag
                    if not with_data
                    else {node_tag: {'data': self[node_id].data}}
                )

        return result

    def to_json(self, with_data=False, sort=True, reverse=False) -> str:
        """Return the json string corresponding to self"""
        return json.dumps(
            self.to_dict(with_data=with_data, sort=sort, reverse=reverse)
        )


import sys
import time

try:
    from unittest2.runner import TextTestRunner
except ImportError:
    from unittest import TextTestRunner

from .result import _XMLTestResult

try:
    # Removed in Python 3
    from cStringIO import StringIO
except ImportError:
    from io import StringIO

class _DelegateIO(object):
    """
    This class defines an object that captures whatever is written to
    a stream or file.
    """

    def __init__(self, delegate):
        self._captured = StringIO()
        self.delegate = delegate

    def write(self, text):
        self._captured.write(text)
        self.delegate.write(text)

    def __getattr__(self, attr):
        try:
            return getattr(self._captured, attr)
        except AttributeError:
            return getattr(self.delegate, attr)

    def reset(self):
        self._captured = StringIO()

class XMLTestRunner(TextTestRunner):
    """
    A test runner class that outputs the results in JUnit like XML files.
    """
    def __init__(self, output='.', outsuffix=None, stream=sys.stderr,
                 descriptions=True, verbosity=1, elapsed_times=True,
                 failfast=False, encoding=None):
        TextTestRunner.__init__(self, stream, descriptions, verbosity,
                                failfast=failfast)
        self.verbosity = verbosity
        self.output = output
        self.encoding = encoding
        if outsuffix:
            self.outsuffix = outsuffix
        else:
            self.outsuffix = time.strftime("%Y%m%d%H%M%S")
        self.elapsed_times = elapsed_times

    def _make_result(self):
        """
        Creates a TestResult object which will be used to store
        information about the executed tests.
        """
        return _XMLTestResult(
            self.stream, self.descriptions, self.verbosity, self.elapsed_times
        )

    def _patch_standard_output(self):
        """
        Replaces stdout and stderr streams with string-based streams
        in order to capture the tests' output.
        """
        sys.stdout = _DelegateIO(sys.stdout)
        sys.stderr = _DelegateIO(sys.stderr)

    def _restore_standard_output(self):
        """
        Restores stdout and stderr streams.
        """
        sys.stdout = sys.stdout.delegate
        sys.stderr = sys.stderr.delegate

    def run(self, test):
        """
        Runs the given test case or test suite.
        """
        try:
            # Prepare the test execution
            self._patch_standard_output()
            result = self._make_result()
            if hasattr(test, 'properties'):
                # junit testsuite properties
                result.properties = test.properties

            # Print a nice header
            self.stream.writeln()
            self.stream.writeln('Running tests...')
            self.stream.writeln(result.separator2)

            # Execute tests
            start_time = time.time()
            test(result)
            stop_time = time.time()
            time_taken = stop_time - start_time

            # Print results
            result.printErrors()
            self.stream.writeln(result.separator2)
            run = result.testsRun
            self.stream.writeln("Ran %d test%s in %.3fs" % (
                run, run != 1 and "s" or "", time_taken)
            )
            self.stream.writeln()

            expectedFails = unexpectedSuccesses = skipped = 0
            try:
                results = map(len, (result.expectedFailures,
                                    result.unexpectedSuccesses,
                                    result.skipped))
            except AttributeError:
                pass
            else:
                expectedFails, unexpectedSuccesses, skipped = results

            # Error traces
            infos = []
            if not result.wasSuccessful():
                self.stream.write("FAILED")
                failed, errored = map(len, (result.failures, result.errors))
                if failed:
                    infos.append("failures={0}".format(failed))
                if errored:
                    infos.append("errors={0}".format(errored))
            else:
                self.stream.write("OK")

            if skipped:
                infos.append("skipped={0}".format(skipped))
            if expectedFails:
                infos.append("expected failures={0}".format(expectedFails))
            if unexpectedSuccesses:
                infos.append("unexpected successes={0}".format(unexpectedSuccesses))

            if infos:
                self.stream.writeln(" ({0})".format(", ".join(infos)))
            else:
                self.stream.write("\n")

            # Generate reports
            self.stream.writeln()
            self.stream.writeln('Generating XML reports...')
            result.generate_reports(self)
        finally:
            self._restore_standard_output()

        return result


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

from django.db import models, migrations
import image_cropping.fields

class Migration(migrations.Migration):

    dependencies = [
        ('auth2', '0003_auto_20151110_1304'),
    ]

    operations = [
        migrations.AlterField(
            model_name='user',
            name=b'cropping',
            field=image_cropping.fields.ImageRatioField('avatar', '70x70', hide_image_field=False, size_warning=False, allow_fullsize=False, free_crop=False, adapt_rotation=False, help_text='Note that the preview above will only be updated after you submit the form.', verbose_name='cropping'),
        ),
        migrations.AlterField(
            model_name='user',
            name='groups',
            field=models.ManyToManyField(related_query_name='user', related_name='user_set', to='auth.Group', blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', verbose_name='groups'),
        ),
        migrations.AlterField(
            model_name='user',
            name='is_active',
            field=models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active'),
        ),
        migrations.AlterField(
            model_name='user',
            name='is_staff',
            field=models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status'),
        ),
        migrations.AlterField(
            model_name='user',
            name='is_superuser',
            field=models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status'),
        ),
    ]


# coding: utf-8
import re
from dictionaries.wiktionary_package.slovoforms_utils import remove_stress
from wikt.commons.checkers.verification import BaseChecker
from wikt.tasks.wikifiers.headers_levels import header_levels

class BaseHeadersChecker(BaseChecker):
    def check(self, page, **kwargs):
        # print u'→', page.title
        return True  # temporary disabled

        if page.title.startswith(u'Шаблон:') or page.title.startswith(u'Категория:'):
            return True
        content = page.content
        headers = re.findall("(?P<before>=+)(?P<header>[^=]+)(?P<after>=+)\n",
                             content)
        # headers = re.findall("(\n|^)(?P<before>=+)(?P<header>[^=]+)(?P<after>=+)(\n|$)",
        #                      content)
        self.errors = list()
        for before, header, after in headers:
        # for t, before, header, after, t in headers:
            error = self.check_header(page, before, header, after)
            if error:
                # print
                # print page.title
                # print error
                self.errors.append(error)
        return not self.errors

    def check_header(self, page, before, header, after):
        raise NotImplementedError

    def get_display(self, page, **kwargs):
        title = page.title#.decode('utf-8')
        errors = map(lambda x: ' ' + x, self.errors)
        errors = '\n'.join(errors)
        return "'''[[%s|%s]]'''\n%s" % (title, title, errors)

class HeadersSyntaxChecker(BaseHeadersChecker):
    def __init__(self, **kwargs):
        slug = 'headers_syntax'
        title = u'Синтаксис заголовков'
        super(HeadersSyntaxChecker, self).__init__(slug, title, **kwargs)

    def check_header(self, page, before, header, after):
        title = page.title
        line = "%s%s%s" % (before, header, after)
        if before != after:
            # print u"→", title
            # print line
            return line
        return

class HeadersLevelsChecker(BaseHeadersChecker):
    def __init__(self, **kwargs):
        slug = 'headers_levels'
        title = u'Уровни заголовков'
        super(HeadersLevelsChecker, self).__init__(slug, title, **kwargs)

    def check_header(self, page, before, header, after):
        title = page.title
        line = "%s%s%s" % (before, header, after)
        header = header.strip()
        if before != after:
            return  # это другая ошибка (syntax)
        need_level = header_levels.get(header, 0)
        level = len(before)
        if need_level and level > 1:
            if need_level != level:
                # пропустим иерогливы, морфемы и прочее
                content = page.content
                skips = [
                    u'= {{-Hani-}} =', u'= {{-hani-}} =', u'= {{-hanzi-',
                    u'= {{-mul-}} =', u'= {{-INT-}} =', u'= {{-Zmth-}} =',
                    u'{{suffix ', u'{{morph', u'== Буква ==',
                    u'Словообразовательная единица']
                for skip in skips:
                    if skip in content:
                        return
                # print u"→", title
                # print line
                return line
        return

class HeadersUnknownChecker(BaseHeadersChecker):
    def __init__(self, **kwargs):
        slug = 'headers_unknown'
        title = u'Неизвестные заголовки'
        super(HeadersUnknownChecker, self).__init__(slug, title, **kwargs)

    def check_header(self, page, before, header, after):

        morphs = [
            u"прилагательное",
            u"наречие",
            u"существительное",
            u"междометие",
            u"частица",
            u"глагол",
            u"местоимение",
            u"причастие",
            u"деепричастие",
            u"союз",
            u"числительное",
            u"предлог",
        ]
        morphs += map(lambda x: x.capitalize(), list(morphs))

        title = page.title#.decode('utf-8')
        # print title
        line = "%s%s%s" % (before, header, after)
        header = header.strip()
        if before != after:
            return  # это другая ошибка (syntax)
        need_level = header_levels.get(header)
        level = len(before)
        if level > 2:
            # if level == 4:
            #     hs = header.strip()
            #     if hs and len(hs) > 1 and hs[0] == '-' or hs[-1:] == '-':
            #         return  # это для подразделов блока "Родственные слова"
            if not need_level:
                # print
                # print title
                # print line
                return line
        elif level == 2:
            if need_level:
                return  # это другая ошибка (level)
            if header in morphs:
                return
            if u'{{заголовок|' in header:
                return  # использования шаблона [{заголовок}} — норм
            rtitle = title.replace('$', '\$').replace(')', '\)').\
                replace('(', '\(').replace('*', '\*').replace('.', '\.').\
                replace('?', '\?')
            pattern = u'^%s(?P<num> [IV]+)?( \((?P<morph>[а-я]+)\))?$' % rtitle
            # print pattern
            # todo: liquide: == существительное II ==
            # todo: list: ==list IV(глагол)==
            cleaned_header = remove_stress(header)
            latin_replacer = {
                u'ā': u'a',
                u'ē': u'e',
                u'ī': u'i',
                u'ō': u'o',
                u'ū': u'u',
            }
            for key, value in latin_replacer.items():
                cleaned_header = cleaned_header.replace(key, value)
            m = re.match(pattern, cleaned_header, flags=re.UNICODE)
            if not m:
                # print
                # print title
                # print line
                return line
            morph = m.group('morph')
            if morph:
                if morph not in morphs:
                    # print
                    # print title
                    # print line
                    return line
        return

class JoinedHeadersUnknownChecker(HeadersUnknownChecker):
    def __init__(self, **kwargs):
        slug = 'headers_unknown_joined'
        title = u'Неизвестные заголовки (резюме)'
        super(HeadersUnknownChecker, self).__init__(slug, title, **kwargs)

    def check(self, page, **kwargs):
        if page.title in [u"Викисловарь:Список слов, которые должны быть в каждом Викисловаре"]:
            return list()
        content = page.content
        headers = re.findall("(?P<before>=+)(?P<header>[^=]+)(?P<after>=+)\n",
                             content)
        self.errors = list()
        for before, header, after in headers:
            error = self.check_header(page, before, header, after)
            if error:
                # print
                # print page.title
                # print error
                self.errors.append(error)
        return self.errors


'''
@Author: your name
@Date: 2020-05-27 13:33:49
@LastEditTime: 2020-06-09 12:51:11
@LastEditors: Please set LastEditors
@Description: In User Settings Edit
@FilePath: /model-building/recommend/xdeepfm/inputs.py
'''

import tensorflow as tf
from collections import namedtuple
import copy
SparseFeature = namedtuple('SparseFeature', [
                           'feature_name', 'vocab_size', 'embedding_dim', 'field_id', 'num_fields'])
DenseFeature = namedtuple('DenseFeature', ['feature_name'])

SparseFeature.__new__.__defaults__ = (False, None, 4, None, None)
DenseFeature.__new__.__defaults__ = (False,)

def build_input_placeholder(feature_columns):
    input_placeholders_dict = {}
    for feature in feature_columns:
        if isinstance(feature, SparseFeature):
            h = tf.placeholder(dtype=tf.float32, shape=[
                               None, feature.embedding_dim])
        elif isinstance(feature, DenseFeature):
            h = tf.placeholder(dtype=tf.float32, shape=[None, 1])
        else:
            raise ValueError('unknown')
        input_placeholders_dict[feature.feature_name] = h
    return input_placeholders_dict

def build_embedding_matrix_dict(sparse_feature_columns, target='dnn'):
    if target == 'linear':
        sfc = copy.deepcopy(sparse_feature_columns)
        for i, fc in enumerate(sfc):
            sfc[i] = sfc[i]._replace(embedding_dim=1)
    elif target != 'dnn':
        raise ValueError('target参数只能是linear或者dnn, 接收到%s' % target)

    embedding_matrix_dict = {}
    for feature in sparse_feature_columns:
        embedding_matrix_dict[feature.feature_name] = tf.get_variable(
            name=feature.feature_name+'_embedding_matrix_%s' % target,
            shape=[feature.vocab_size, feature.embedding_dim],
            initializer=tf.random_normal_initializer(),
            dtype=tf.float32
        )
    return embedding_matrix_dict

def build_field_embedding_matrix_dict(sparse_feature_columns):
    embedding_matrix_dict = {}
    for feature in sparse_feature_columns:
        if not isinstance(feature, SparseFeature):
            raise ValueError('只有sparse_feature才能建立embedding矩阵')
        embedding_matrix_dict[feature.feature_name] = {}
        for field in range(feature.num_fields):
            embedding_matrix_dict[feature.feature_name][field] = tf.get_variable(
                name=feature.feature_name+'_field%d'%field+'_embedding_matrix',
                shape=[feature.vocab_size, feature.embedding_dim],
                initializer=tf.random_normal_initializer(),
                dtype=tf.float32
            )
    return embedding_matrix_dict

# test = SparseFeature(embedding_dim=4)
# print(test.embedding_dim)

def input_from_feature_columns(inputs, feature_columns, target='dnn', field=False):
    dense_feature_columns = [fc for fc in feature_columns if isinstance(fc, DenseFeature)]
    sparse_feature_columns = [fc for fc in feature_columns if isinstance(fc, SparseFeature)]

    dense_value_list = [inputs[fc.feature_name] for fc in dense_feature_columns]
    if field:
        embedding_matrix_dict = build_field_embedding_matrix_dict(sparse_feature_columns)
        return dense_value_list, embedding_matrix_dict

    embedding_matrix_dict = build_embedding_matrix_dict(sparse_feature_columns, target)
    sparse_embedding_list = [tf.nn.embedding_lookup(embedding_matrix_dict[fc.feature_name], inputs[fc.feature_name]) for fc in sparse_feature_columns]

    return dense_value_list, sparse_embedding_list


#! /usr/bin/env python
#  -*- coding: utf-8 -*-
#
# Copyright 2020 Autoware Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sys
import os
import rospy
from PySide.QtGui import *
from PySide.QtCore import *
from ui.ui_set_run_dialog import Ui_set_run_dialog
from controller.vehicle_manual_controller_iv import VehicleManualControllerIv
from lock_manager import LockManager

LOCK_PATH = '/tmp/set_run_dialog.lock'

class SetRunDialog(QDialog):
    def __init__(self, parent=None):
        super(SetRunDialog, self).__init__(parent)
        self.ui = Ui_set_run_dialog()
        self.ui.setupUi(self)

        self.ui.start_button.clicked.connect(self.start_button_clicked)
        self.ui.stop_button.clicked.connect(self.stop_button_clicked)

        # Init UI value
        self.ui.linear_edit.setText('0.0')
        self.ui.angular_edit.setText('0.0')

        self.vehicle_manual_controller = VehicleManualControllerIv()

        # Set window to stay top
        self.setWindowFlags(Qt.WindowStaysOnTopHint)

    def start_button_clicked(self):
        # Get value from UI
        try:
            linear_kmph = float(self.ui.linear_edit.text())
            steer_angle = float(self.ui.angular_edit.text())
        except ValueError:
            QMessageBox.warning(None, 'Warning', 'Input correct value!', QMessageBox.Ok)
            return
        # Set value
        self.vehicle_manual_controller.set_velocity_kmph(linear_kmph, steer_angle)
        # Transfer focus
        self.ui.stop_button.setFocus()

    def stop_button_clicked(self):
        # Set value
        self.vehicle_manual_controller.set_velocity_kmph(0.0, 0.0)
        # Transfer focus
        self.ui.start_button.setFocus()

    def closeEvent(self, event):
        # Stop vehicle for sure
        self.vehicle_manual_controller.stop_now()

if __name__=='__main__':
    rospy.init_node('set_run_dialog')

    app = QApplication(sys.argv)

    lock_manager = LockManager(LOCK_PATH)
    if not lock_manager.get_lock():
        QMessageBox.warning(None, 'Warning', 'Another same process is running. If not, please delete lock file [ %s ].' % lock_manager.get_lock_path(), QMessageBox.Ok)
        sys.exit(1)

    win = SetRunDialog()
    win.show()
    ret = app.exec_()
    lock_manager.release_lock()
    sys.exit(ret)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
#   for use with Python 3

#	text_buffer
#
#	This program is free software; you can redistribute it and/or modify
#	it under the terms of the GNU General Public License as published by
#	the Free Software Foundation; either version 2 of the License, or
#	(at your option) any later version.
#
#	This program is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNimport sys, getoptESS FOR A PARTICULAR PURPOSE.  See the
#	GNU General Public License for more details.
#
#	You should have received a copy of the GNU General Public License
#	along with this program; if not, write to the Free Software
#	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#	MA 02110-1301, USA.

# Standard library imports
from datetime import datetime
from shutil import copyfile
from sys import exit as sys_exit
import os

# Third party imports
# None

# Local application imports
from utility import pr,make_time_text,send_by_ftp
from buffer_log import class_buffer_log

class class_text_buffer(object):
	# Rotating Buffer Class
	# Initiate with just the size required Parameter
	# Get data with just a position in buffer Parameter
	def __init__(self,headings,config):
		#initialization
		self.__config = config
		print(" Buffer Init for : ",self.__config.prog_name," with a size of : ",self.__config.text_buffer_length, " and  width of : ", len(headings) + 1, " including time stamp")
		if not os.path.exists('log'):
		    os.makedirs('log')

		self.__source_ref = 0 # a number used to control prevention repeat messages
		self.__width = len(headings) + 1
		self.line_values = ["not set"]*(len(headings))
		self.__dta = [ [ None for di in range(self.__width+1) ] for dj in range(self.__config.text_buffer_length+1) ]
		self.__size = 0
		self.__posn = self.__config.text_buffer_length-1

#**********************************************
		#self.__headings = ["Time"]
		#for hdg_ind in range(0,self.__width-1):
		#	#print(hdg_ind,headings[hdg_ind])
		#	self.__headings.append(headings[hdg_ind])
		#print(self.__headings)
#**************************************************
		self.__headings = headings

		self.__pr_values = ["text"] * self.__width

		self.__html_filename = config.prog_name + "_log.html"
		self.__html_filename_save_as = config.prog_path + self.__html_filename
		self.__www_filename = config.local_dir_www +  self.__html_filename
		self.__ftp_creds = config.ftp_creds_filename
		self.__send_html_count = 0
		if self.__config.log_buffer_flag:
			self.__send_log_count = 0
			self.__log = class_buffer_log(config)

	def size(self):
		return self.__config.text_buffer_length

	def update_buffer(self,values,appnd,ref):
		#append a line of info at the current position plus 1
		# print("Update Buffer appnd and ref are : ",appnd,ref)
		if appnd + (self.__source_ref != ref):
			#we adding message and incrementing posn
			if self.__size < self.__config.text_buffer_length-1 :
				self.__size += 1
			if self.__posn == self.__config.text_buffer_length-1:
				# last insert was at the end so go back to beginning@@
				self.__posn = 0
			else:
				# first increment insert position by one
				self.__posn += 1
				# then insert a line full of values
			self.__source_ref = ref
		else:
			self.__source_ref = ref
		if len(values) > self.__width :
			print("Width Error for :",self.__config.prog_name, len(values) , self.__width, values)
			sys.exit()
		for i in range(0,len(values)):
			self.__dta[self.__posn][i] = values[i]

		#print("Buffer updated and log buffer flag is : ",self.__config.log_buffer_flag)
		if self.__config.log_buffer_flag:
			self.__log.log_to_file(self.__headings,values)

			#self.__log.copy_log_to_www(False)
			#send log file to website configevery ten scans

				# *************************
			#if self.__send_log_count > 10:

				#print("Sending log file by FTP")

			#	self.__log.send_log_by_ftp(False,self.__config.log_directory,self.__config.ftp_timeout)
			#	self.__send_log_count = 0
			#else:
				#print("not FTP This Time count is : ",self.__send_log_count)
			#	self.__send_log_count += 1

	def get_line_dta(self, key):
		#return stored element from position relative to current insert position in buffer
		line_dta = [" - "]*self.__width
		if (self.__posn-key) > -1:
			# need to take values from arlogea before current insert position
			for i in range(self.__width):
				line_dta[i] = self.__dta[self.__posn-key][i]
			return(line_dta)
		else:
			# need to take values from after current insert position
			for i in range(self.__width):
				#following two lines used too debug the calc to get the lower part of status file
				#print("That Calc key,self.__size,self.__config.text_buffer_length, self.__posn-key,key sum",
				 #  key,self.__size,self.__config.text_buffer_length, self.__posn-key,(self.__posn-key),self.(self.__config.text_buffer_length + (self.__posn-key))
				line_dta[i] = self.__dta[self.__config.text_buffer_length + (self.__posn-key)][i]
			return(line_dta)

	def get_dta(self):
		# get all the data inserted so far, or the whole buffer
		all_data = [ [ None for di in range(self.__width+1) ] for dj in range(self.__config.text_buffer_length+1) ]
		for ind in range(0,self.__size):
			line_dta = self.get_line_dta(ind)
			# Following line for debug data from Buffer
			# print("get_dta >>>",ind,line_dta)
			for i in range(len(line_dta)):
				all_data[ind][i] = line_dta[i]
		return(all_data)

	def just_log(self,appnd,ref,log_time,refresh_interval):
		self.__log.log_to_file(self.__headings,self.line_values)

	def pr(self,appnd,ref,log_time,refresh_interval):
		here = "buffer.pr for " + self.__config.prog_name
		make_values = [" -- "]*self.__width
		prtime = datetime.now()
		for_screen = log_time.strftime('%d/%m/%Y %H:%M:%S')
		# following alternative will show more resolution for fractions of a second
		# for_screen = log_time.strftime('%d/%m/%Y %H:%M:%S.%f')
		make_values[0] = for_screen
		file_start = """
<meta http-equiv="refresh" content="""
		file_start = file_start + str(refresh_interval)
		file_start = file_start + """ />

Rotating Buffer Display
"""
		tbl_start = """

"""
		tbl_start_line = """
"""
		tbl_end_line = """
"""
		tbl_start_col = """"""
		tbl_end_col= """	"""
		tbl_end = """

"""
		file_end = """

"""
		try:
			for i in range(0,self.__width -1):
				make_values[i+1] = str(self.line_values[i])
				for_screen = for_screen + " " + str(self.line_values[i])
		except:
			print("Error in make values in ...buffer.pr for : ",self.__config.prog_name)
			print("i,values,len(self.line_value>s),self.__width",i,self.line_values,len(self.line_values),self.__width)
			sys_exit()

		# print to screen and to status log and update html file

		if appnd:
			print("    appending : " + self.__config.prog_name + " : " + for_screen)
		else:
			print("not appending : " + self.__config.prog_name + " : " + for_screen)

		self.update_buffer(make_values,appnd,ref)
		with open(self.__html_filename,'w') as htmlfile:
			htmlfile.write(file_start)
			htmlfile.write("
" + self.__html_filename + " : " + make_time_text(datetime.now())  + "
\n
")
			htmlfile.write(tbl_start + tbl_start_line)
			for ind in range(0,len(self.__headings)):
				htmlfile.write(tbl_start_col + self.__headings[ind] + tbl_end_col)
			htmlfile.write(tbl_end_line)
			buffer_dta = self.get_dta()
			for ind in range(self.__size):
				htmlfile.write(tbl_start_line)
				for i in range(self.__width):
					htmlfile.write(tbl_start_col + str(buffer_dta[ind][i]) + tbl_end_col)
				htmlfile.write(tbl_end_line)
			htmlfile.write(tbl_end)
			htmlfile.write(file_end)
		copyfile(self.__html_filename, self.__www_filename)

		# To debug FTP change end of following line to " = True"
		if self.__send_html_count >= 3:
			FTP_dbug_flag = False
			ftp_result = send_by_ftp(FTP_dbug_flag,self.__ftp_creds, self.__html_filename_save_as, self.__html_filename,"",self.__config.ftp_timeout)
			for pres_ind in range(0,len(ftp_result)):
				pr(FTP_dbug_flag,here, str(pres_ind) + " : ", ftp_result[pres_ind])
			self.__send_html_count = 0
		else:
			self.__send_html_count += 1
		return


#!/usr/bin/python
# -*- coding: utf-8 -*-
from __future__ import print_function
import argparse
from collections import defaultdict
import json
import logging
import os
import subprocess
import sys

from qald.sparql_utils import Query, QueryManager, GetQueriesResults
from utils.evaluation import compute_f1_score
from utils.priority_queue import PriorityQueue
from utils.tree_tools import tree_or_string, get_top, IsString
from utils.tools_generic_transducer import TiburonToStanford

parser = argparse.ArgumentParser()
parser.add_argument(
  "infile", nargs='?', type=argparse.FileType('r'), default=sys.stdin)
parser.add_argument(
  "--kgen", nargs='?', type=int, default="10",
  help="Maximum number of queries to be generated.")
parser.add_argument(
  "--cores", nargs='?', type=int, default="1",
  help="Number of cores for multiprocessing.")
parser.add_argument(
  "--kout", nargs='?', type=int, default="10",
  help="N-best maximum number of trees (queries) to output as result.")
args = parser.parse_args()

logging.basicConfig(level=logging.DEBUG)

TIBURON = '../tiburon/tiburon'
temp_fname = 'temp.wrtg'
temp_fname_err = 'temp.wrtg.err'

def rename_vars(tree, var_index=0, var_pref = '?p'):
  if IsString(tree):
    return tree
  for p in tree.treepositions('leaves'):
    if '[]' in tree[p]:
      reverse_op = '!' if tree[p].startswith('!') else ''
      tree[p] = reverse_op + var_pref + str(var_index)
      var_index += 1
  return tree

def is_plausible_tree(tree):
  """
  Returns True if there are more than one predicate variables.
  """
  if IsString(tree):
    return False
  preds = set(l for l in tree.leaves() if l.lstrip('!').startswith('?p'))
  return len(preds) < 2

def filterout_trees(trees):
  return (tree for tree in trees if is_plausible_tree(tree))

def generate_trees(grammar, kgen):
  with open(temp_fname, 'w') as fout:
    for p in grammar:
      fout.write(p + '\n')
  # with open(os.devnull, 'w') as FNULL:
  with open(temp_fname_err, 'w') as FNULL:
    output = subprocess.check_output(
      [TIBURON,
       temp_fname,
       '--semiring', 'real',
       '-n',
       '-k', str(kgen)],
      stderr=FNULL)
  trees = [tl.split(' # ')[0] for tl in output.split('\n') if tl.strip()]
  trees = [tree_or_string(TiburonToStanford(t)) for t in trees]
  trees = [rename_vars(t) for t in trees]
  trees = filterout_trees(trees)
  return trees

def normalize_answer(answer):
  """
  Normalize answers from Freebase. At the moment,
  only URIs are shortened with the prefix "fb:".
  """
  return answer.replace('http://rdf.freebase.com/ns/', 'fb:')

def integrate_results(query, results):
  """
  results is a list of results:
  [[answer, var1 inst, var2 inst, ...], ...]
  This function integrates all answers for each
  combination of variable instantiations.
  """
  integration = defaultdict(list)
  for result in results:
    answer = normalize_answer(result[0])
    integration[tuple(result[1:])].append(answer)
  d = {'ldcsc' : str(query.ldcsc)}
  d['results'] = []
  for instantiations, answers in integration.items():
    var_inst = dict(zip(query.query_vars, instantiations))
    d['results'].append({'instantiations' : var_inst, 'answers' : answers})
  return d

def execute_trees(trees, query_manager, ncores=1):
  queries = [Query.fromldcsc(tree, ['?p']) for tree in trees]
  queries_results = GetQueriesResults(queries, query_manager, ncores)
  exec_results = []
  for query, results in zip(queries, queries_results):
    exec_result = integrate_results(query, results)
    exec_results.append(exec_result)
  return exec_results

def get_best_execs(execs, gold_answers, kout=10):
  """
  Given query executions (which are dictionaries with the following entries):
  ldcsc: constituent representation of l-DCS tree.
  instantiations: a map from variable name to predicate/entity URIs.
  answers: a list of entities (which are the answer retrieved).
  It returns the kout best executions.
  gold_answers is a set with the gold answers for the question.
  """
  queue = PriorityQueue(kout, reverse=True)
  for execution in execs:
    if execution:
      for result in execution['results']:
        answers = set(result['answers'])
        f1_score = compute_f1_score(answers, gold_answers)
        exec_instantiation = {
          'ldcsc' : execution['ldcsc'],
          'instantiations' : result['instantiations'],
          'answers' : result['answers']}
        queue.Push(f1_score, exec_instantiation)
  scored_execs = queue.GetSortedScoresAndItems()
  execs = []
  for score, execution in scored_execs:
    execution['score'] = score
    execs.append(execution)
  return execs

def index_grammar(grammar):
  """
  Creates a dictionary where:
    keys are RHSs of productions,
    values are tuples ([production], weight), that is,
      a tuple where the first element is a list of productions,
      and the second element is a weight (float).
  """
  index = dict()
  for production in grammar:
    if '->' not in production:
      continue
    lhs, rhs_weight = production.split(' -> ')
    if '#' in rhs_weight:
      rhs, weight_str = rhs_weight.split(' # ')
      weight = float(weight_str)
    else:
      rhs = rhs_weight
      weight = 1.0
    rhs_stan = TiburonToStanford(rhs)
    if rhs in index:
      index[rhs_stan][0].append('{0} -> {1}'.format(lhs, rhs))
      index[rhs_stan][1] = max(index[rhs_stan][1], weight)
    else:
      index[rhs_stan] = [['{0} -> {1}'.format(lhs, rhs)], weight]
  return index

def get_wprods_from_grammar_index(grammar_index):
  wprods = ['start']
  for rhs, (prods, weight) in grammar_index.items():
    for prod in prods:
      wprods.append('{0} # {1}'.format(prod, weight))
  return wprods

def estimate_wrtg_weights(
    grammar, gold_answers, query_manager, cores=1, batch_size=10):
  grammar_rhs_index = index_grammar(grammar)
  trees = list(generate_trees(grammar, batch_size))
  execs = execute_trees(trees, query_manager, cores)
  for tree, ex in zip(trees, execs):
    productions = grammar_rhs_index.get(str(tree), None)
    if productions is not None:
      if ex and ex['results']:
        for result in ex['results']:
          answers = set(result['answers'])
          f1_score = compute_f1_score(answers, gold_answers)
          productions[-1] = f1_score
      else:
        productions[-1] = 0.0
  grammar = get_wprods_from_grammar_index(grammar_rhs_index)
  return grammar

num_restarts = 0
batch_size = 1000

score = 0.0
total_examples = 0
with QueryManager() as query_manager:
  for example_str in args.infile:
    total_examples += 1
    example = json.loads(example_str.strip())
    gold_answers = set(example['answer_entities'])
    grammar = example.get('grammar', None)
    if grammar is None:
      print('x', end='', file=sys.stderr)
      continue
    # from pudb import set_trace; set_trace()
    for i in range(num_restarts):
      logging.info('Restart {0}'.format(i))
      grammar = estimate_wrtg_weights(
        grammar, gold_answers, query_manager, args.cores, batch_size)
    trees = generate_trees(grammar, args.kgen)
    execs = execute_trees(trees, query_manager, args.cores)
    best_execs = get_best_execs(execs, gold_answers, args.kout)
    example['executions'] = best_execs
    example['grammar'] = grammar
    if best_execs:
      score += best_execs[0]['score']
    print(json.dumps(example))
    print('.', end='', file=sys.stderr)

print('Averaged F1-score: {0}'.format(score / total_examples), file=sys.stderr)


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

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('telegram', '0002_auto_20160805_1413'),
    ]

    operations = [
        migrations.AddField(
            model_name='telegramuserstate',
            name='abuse',
            field=models.CharField(default='', verbose_name='Жалоба', max_length=1000),
        ),
    ]


import os
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
from flask_openid import OpenID
from config import basedir, Config
from flask_whooshalchemyplus import index_all
from flask_mail import Mail
from flask_moment import Moment
from flask_babel import Babel

app = Flask(__name__)
app.config.from_object(Config)
db = SQLAlchemy(app)
# 设置存储临时文件
oid = OpenID(app, os.path.join(basedir, 'tmp'))
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login'
index_all(app)
mail = Mail(app)
moment = Moment(app)
babel = Babel(app)

from app import routes, models, errors
if not app.debug:
    import logging
    from logging.handlers import SMTPHandler, RotatingFileHandler

    # send email
    if app.config['MAIL_SERVER']:
        credentials = None
        if app.config['MAIL_USERNAME'] or app.config['MAIL_PASSWORD']:
            credentials = (app.config['MAIL_USERNAME'], app.config['MAIL_PASSWORD'])
        secure = None
        if app.config['MAIL_USE_TLS']:
            secure = ()
        mail_handler = SMTPHandler(
            mailhost=(app.config['MAIL_SERVER'], app.config['MAIL_PORT']),
            fromaddr='no-reply@' + app.config['MAIL_SERVER'],
            toaddrs=app.config['ADMINS'], subject='Microblog Failure',
            credentials=credentials, secure=secure)
        mail_handler.setLevel(logging.ERROR)
        app.logger.addHandler(mail_handler)

    # 记录错误日志

    if not os.path.exists('logs'):
        os.mkdir('logs')
    file_handler = RotatingFileHandler(os.path.join(basedir, 'logs/microblog.log'), maxBytes=10240, backupCount=10)
    file_handler.setFormatter(logging.Formatter('%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]'))
    file_handler.setLevel(logging.INFO)
    app.logger.addHandler(file_handler)

    app.logger.setLevel(logging.INFO)
    # app.logger.info('Microblog startup')

if __name__ == '__name__':
    app.run()


import pandas as pd
import pandas_profiling
from pandas_profiling.utils.cache import cache_file
import sys
import shutil

def main():
    df = pd.read_csv(sys.stdin)
    ########### insert your code here. This example creates a pandas-profile of the data
    profile = df.profile_report(correlations={
             "pearson": {"calculate": False},
             "spearman": {"calculate": False},
             "kendall": {"calculate": False},
             "phi_k": {"calculate": False},
             "cramers": {"calculate": False},
         })

    profile.to_file("/home/acc/index.html")
    ###########

    with open("/home/acc/index.html", "r") as f:
        shutil.copyfileobj(f, sys.stdout)

if __name__ == "__main__":
    main()


#!/usr/bin/env python3
#

import threading
import time

def run(n):
    semaphore.acquire()
    print(f"{threading.current_thread().getName()} get semaphore, num: {n} ")
    time.sleep(1.5)
    semaphore.release()

if __name__ == '__main__':
    semaphore = threading.BoundedSemaphore(5)
    t_jobs = []

    for i in range(22):
        t = threading.Thread(target=run, args=(i,))
        t.start()
        t_jobs.append(t)

    for t in t_jobs:
        t.join()

    # while threading.active_count() != 1:
    #     pass
    # else:
    #
    #     print('---- all threads done ----')
    #     print(threading.active_count())

    if threading.active_count() == 1:
        print('---- all threads done ----')


import numba as nb
from numba import prange
from models.AHNFunctions import CreateLinearCompound
from models.AHNFunctions import DataInMolecules
from models.AHNFunctions import ComputeMoleculeParameters
import numpy as np

def PiranhaFeastOptimizer(D, NP, R, b, c, nFES, Lower, Upper, x, y, n):
    #Initialize parameters
    R_matrix = np.zeros((R,D))
    R_score = np.full((R), float("inf"))
    R_rad = np.full((R), 0.10)
    V = np.random.rand(NP,D)
    eval_flag = True  # evaluations flag
    evaluations = 0  # evaluations counter
    P = np.zeros((NP, D))
    P_score = np.zeros((NP, 1))

    C = CreateLinearCompound(n)
    def Fun(D, sol, x, y, C):
        #@nb.jit(nopython=True, parallel=True)
        def evaluate(D, sol, x, y, C):  # sol is the possible solution
            # Reshape the particle for matching the prototype structure
            sol = np.asarray(sol).reshape((n, x.shape[1]))
            # Split training data over molecules
            SigmaSplitX, SigmaSplitY = DataInMolecules(x, y, sol)
            # initialize procedure to fit molecular parameters
            error = np.zeros((n + 1, np.size(y, axis=1)))
            omega = C
            H = [[0] for i in range(n)]
            # Find molecular parameters
            for j in list(range(0, n)):
                Xi = SigmaSplitX[j]
                Yi = SigmaSplitY[j]
                if Xi.any():
                    [H[j], error[j][:]] = ComputeMoleculeParameters(Xi, Yi, int(omega[j]))
            #ahn = {'H': H, 'Pi': sol, 'n': n, 'C': C}
            fitness = np.sum(error)
            return fitness
        fitness = evaluate(D=D, sol=sol, x=x, y=y, C=C)
        return fitness

    #@nb.jit(nopython=True, parallel=False)
    def initialization(D, NP, Lower, Upper, P):
        #P = np.zeros((NP, D))
        #P_score = np.zeros((NP, 1))
        """Initialize positions."""
        for i in range(0, NP):
            for j in range(0, D):
                P[i, j] = np.random.rand() * (Upper[j] - Lower[j]) + Lower[j]
        return P

    #@nb.jit(nopython=True, parallel=False)
    def eval_true(evaluations, nFES, eval_flag):
        """Check evaluations."""
        if evaluations == nFES:
            eval_flag = False
        return eval_flag

    #@nb.jit(nopython=True, parallel=False)
    def bounds(D, Lower, Upper, position):
        """Keep it within bounds."""
        for i in range(D):
            if position[i] < Lower[i]:
                position[i] = Lower[i]
            if position[i] > Upper[i]:
                position[i] = Upper[i]
        return position

    """
    def attack(R, P, b, V):
        V_actual = V + np.random.rand() * b * (R - P)
        P_actual = P + V_actual
        return V_actual, P_actual

    def navigate(c, d, R):
        t=np.random.uniform(-1,1)
        M = d*np.exp(c*t)*np.cos(2*np.pi*t) + R
        return M
    """
    @nb.jit(nopython=True, parallel=True)
    def actual_pos(NP, P, V, R_matrix, R_rad, c, Fit, R_score):
        for i in prange(NP):
            #dist_matrix = Fit[i] - R_score #np.asarray([Fit - R_score[i] for i in range(len(R_matrix))])
            dist_matrix = np.asarray([np.linalg.norm(P[i] - R_matrix[j]) for j in prange(len(R_matrix))])  # Distancia entre pirañas y presas
            R_ind = np.argmin(dist_matrix)  # Identifica la presa mas cercana a la piraña i
            if (dist_matrix[R_ind] < R_rad[R_ind]) and (dist_matrix[R_ind] != 0):
                V[i] = V[i] + np.random.rand()*b*(R_matrix[R_ind] - P[i])
                P[i] = P[i] + V[i]
                #V[i], P[i] = attack(R=R_matrix[R_ind], P=P[i], b=b, V=V[i])
                R_rad[R_ind] = R_rad[R_ind] * (0.90 ** (R_ind + 1))
            else:
                acum = np.sum(dist_matrix)
                Prob_nj = np.divide(acum, dist_matrix)#np.asarray([(acum / d) for d in dist_matrix])
                acum = np.sum(Prob_nj)
                Prob_nj = np.divide(Prob_nj, acum)#Prob_nj/acum
                #Prob_nj = [w / acum for w in Prob_nj]
                #selectR = np.random.choice(a=list(range(len(R_matrix))), p=Prob_nj)
                selectR = np.searchsorted(np.cumsum(Prob_nj), np.random.rand(1))[0]
                t = np.random.uniform(-1,1)
                P[i] = dist_matrix[selectR] * np.exp(c*t)*np.cos(2*np.pi*t) + R_matrix[selectR]
                #P[i] = navigate(c=c, d=dist_matrix[selectR], R=R_matrix[selectR])

        return P, V
    """
    #@nb.jit(nopython=True, parallel=True)
    def actual_pos2(NP, P, V, R_matrix, R_rad, c, Fit, R_score):
        for i in range(NP):
            R_ind = 0
            for j in prange(len(R_score)):
                if (Fit[i] < np.multiply(R_score[j],1+R_rad[j])):
                    R_ind = j
                    break

            #dist_matrix = Fit[i] - R_score #np.asarray([Fit - R_score[i] for i in range(len(R_matrix))])
            #dist_matrix = np.asarray([np.linalg.norm(P[i] - R_matrix[j]) for j in range(len(R_matrix))])  # Distancia entre pirañas y presas
            #R_ind = np.argmax(dist_matrix)  # Identifica la presa mas cercana a la piraña i
            if R_ind != 0:
                V[i] = V[i] + np.random.rand()*b*(R_matrix[R_ind] - P[i])
                P[i] = P[i] + V[i]
                #V[i], P[i] = attack(R=R_matrix[R_ind], P=P[i], b=b, V=V[i])
                R_rad[R_ind] = (R_rad[R_ind]) * (0.90 ** (R_ind + 1))
            else:
                dist_matrix = np.asarray([np.linalg.norm(P[i] - R_matrix[j]) for j in range(len(R_matrix))])
                acum = np.sum(dist_matrix)
                Prob_nj = np.divide(acum, dist_matrix)#np.asarray([(acum / d) for d in dist_matrix])
                acum = np.sum(Prob_nj)
                Prob_nj = np.divide(Prob_nj, acum)#Prob_nj/acum
                #Prob_nj = [w / acum for w in Prob_nj]
                #selectR = np.random.choice(a=list(range(len(R_matrix))), p=Prob_nj)
                selectR = np.searchsorted(np.cumsum(Prob_nj), np.random.rand(1))[0]
                t = np.random.uniform(-1,1)
                P[i] = dist_matrix[selectR] * np.exp(c*t)*np.cos(2*np.pi*t) + R_matrix[selectR]
                #P[i] = navigate(c=c, d=dist_matrix[selectR], R=R_matrix[selectR])

        return P, V
    """
    def move(R_matrix, R_score, R_rad, V, eval_flag, evaluations, c, P, P_score):
        P = initialization(D=D, NP=NP, Lower=Lower, Upper=Upper, P=P)   #Inicialización
        #P_score[i] = Fun(D=D, sol=P[i], x=x, y=y, C=C)
        while eval_flag is not False:
            for i in range(NP):
                P[i] = bounds(D=D, Lower=Lower, Upper=Upper, position=P[i]) #Valida limites
                eval_flag = eval_true(evaluations=evaluations, nFES=nFES, eval_flag=eval_flag)
                if eval_flag is not True:
                    break
                Fit = Fun(D=D, sol=P[i], x=x, y=y, C=C) #Fitness de la solucion
                P_score[i] = Fit
                evaluations = evaluations + 1
                ####    Identifica presas
                for z in range(len(R_matrix)):
                    if Fit <= R_score[z]:
                        R_score[z] = Fit
                        R_matrix[z] = P[i]
                        R_rad[z] = 0.10
                        break
                #### Si es la primera evaluacion, ordenar R
                if evaluations == (len(R_matrix)):
                    ind = np.argsort(R_score)
                    R_score = R_score[ind]
                    R_matrix = R_matrix[ind]
                    R_rad = R_rad[ind]
                """
                dist_matrix = np.asanyarray([np.linalg.norm(P[i] - R_matrix[j]) for j in range(len(R_matrix))])   #Distancia entre pirañas y presas
                R_ind = np.argmin(dist_matrix) # Identifica la presa mas cercana a la piraña i
                if dist_matrix[R_ind] < R_rad[R_ind]:
                    V[i], P[i] = attack(R=R_matrix[R_ind], P=P[i], b=b, V=V[i])
                    R_rad[R_ind] = R_rad[R_ind]*0.95**(R_ind+1)

                else:
                    acum = sum(dist_matrix)
                    Prob_nj = np.asanyarray([(acum/d) for d in dist_matrix])
                    acum = sum(Prob_nj)
                    Prob_nj = [w / acum for w in Prob_nj]
                    selectR = np.random.choice(a=list(range(len(R_matrix))), p=Prob_nj)
                    P[i] = navigate(c=c, d=dist_matrix[selectR], R=R_matrix[selectR])
                """
            P, V = actual_pos(NP=NP, P=P, V=V, R_matrix=R_matrix, R_rad=R_rad, c=c, Fit=P_score, R_score=R_score)
        Best_pray = R_score[0]

        return Best_pray

    def run(R_matrix, R_score, R_rad, V, eval_flag, evaluations, c, P, P_score):
        return move(R_matrix, R_score, R_rad, V, eval_flag, evaluations, c, P, P_score)

    Best_pray = run(R_matrix, R_score, R_rad, V, eval_flag, evaluations, c, P, P_score)

    return Best_pray


"""
AJAX Zipcode example
"""

from time import sleep
from flask import Flask, request

app = Flask(__name__)

@app.route("/getplace", methods=["GET"])
def getplace():
    POSTCODES = {
        "0107": "Oslo",
        "0506": "Oslo",
        "4090": "Hafrsfjord",
        "4033": "Stavanger",
        "4014": "Stavanger",
        "4050": "Sola",
        "4056": "Tananger"
    }
    sleep(1)
    postcode = request.args.get("postcode", None)
    # look up corresponding place or return empty string
    if postcode and (postcode in POSTCODES):
        return POSTCODES[postcode]
    return ""

@app.route("/")
def index():
    return app.send_static_file("zipcode.html")

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


# -*- coding: utf-8 -*-
"""
Created on Wed 01/15/2020

@author: suterr
"""

import Dominion
import random
import testUtility
from collections import defaultdict

#Get player names
player_names = ["*Ryan", "*Bill"]

#number of curses and victory cards
if len(player_names)>2:
    nV=12
else:
    nV=8
nC = -10 + 10 * len(player_names)

box = testUtility.GetBoxes(nV)

#Pick 10 cards from box to be in the supply.
supply = testUtility.pickCards(box, player_names, nV, nC)

#initialize the trash
trash = []

#Costruct the Player objects
players = testUtility.makePlayers(player_names)

#Play the game
testUtility.playGame(supply, players,trash)

#Final score
testUtility.finalScore([Dominion.Player("Ryan")])


# This is the basic tool
# If code is reused, put it in a function
# Python 3.6.5
# Usage <rulefile> <folder>
import sys
# Import the yara library
import yara
# Import the glob module for recursivly finding files
import glob

"""
def scanHitCallback():
    # What to do when we find a match?
    return
"""

def getFileList(folder):
    globString = folder + "/**/*.php"
    fileList = glob.glob(globString, recursive=True)
    return fileList

def scanFile(compiledRules, fileToScan):
    # Scan a single file
    # Add the callback later

    # Get match
    match = compiledRules.match(fileToScan)
    # Check if it actually got a match
    if(len(match) > 0):
        print("File: %s matches: %s" % (fileToScan, match[0]))
        return True
    else:
        return False

def compileRules(pathToRules):
    rules = yara.compile(pathToRules)
    return rules

def scanner(rules, folder):
    scanList = getFileList(folder)
    print("List of files: %s" % scanList)
    print()
    print("Scanning...")
    scannerMatchedFiles = []
    for f in scanList:
        if(scanFile(rules, f)):
            scannerMatchedFiles.append(f)
    return scannerMatchedFiles

def init(arguments):
    print("--Malware scanner--")
    print()
    # Check if enough arguments are supplied
    # We want at least 3 (script, rules, folder)
    if(len(arguments) == 3):
        rules = compileRules(arguments[1])
        folder = arguments[2]
        # Scan the directory, returns the list of matched files
        matchedFiles = scanner(rules, folder)

        print()
        print("Done Scanning")

        print("-"*10)
        print("Number of matched files: %d" % len(matchedFiles))
        print("All matched files: %s" % matchedFiles)
    else:
        print("Usage: %s %s %s", arguments[0], "<rulefile>", "<folder>")
        exit()

    return

if(__name__ == '__main__'):
    init(sys.argv)
    exit()


from fs_helpers import *
from tweaks import address_to_offset

SILENT_BGM_IDS = [
  0x0000,
]

EXTRA_BGM_INFOS = [
  0x00441B00, # Grandma's theme
  0x00330000, # Unused Tetra theme
  #0x00190105, # Miniboss theme (sounds wrong)
  #0x001A0605, # Miniboss theme (sounds wrong)
  0x00030101, # Gohma phase 1
  0x00100101, # Gohma phase 2
  0x0041020A, # Phantom Ganon (at Forsaken Fortress, with intro)
  0x0047180A, # Phantom Ganon (in Ganon's Tower, no intro)
  0x01150704, # Helmaroc chase up the tower
  0x004C0809, # Molgera
  0x004D2000, # Ganondorf fight
]

def randomize_bgm(self):
  dol_data = self.get_raw_file("sys/main.dol")
  stage_bgm_info_list_start = 0x8039C30C
  island_bgm_info_list_start = 0x8039C4F0

  valid_bgm_infos = []
  for spot_index in range(1, 0x78+1):
    bgm_info = read_u32(dol_data, address_to_offset(stage_bgm_info_list_start + spot_index*4))
    if ((bgm_info & 0xFFFF0000) >> 16) in SILENT_BGM_IDS:
      # Don't include silent BGMs
      continue
    if bgm_info in valid_bgm_infos:
      # Don't include the exact same BGM info more than once
      continue
    valid_bgm_infos.append(bgm_info)
  for island_num in range(1, 49+1):
    bgm_info = read_u32(dol_data, address_to_offset(island_bgm_info_list_start + island_num*4))
    if ((bgm_info & 0xFFFF0000) >> 16) in SILENT_BGM_IDS:
      # Don't include silent BGMs
      continue
    if bgm_info in valid_bgm_infos:
      # Don't include the exact same BGM info more than once
      continue
    valid_bgm_infos.append(bgm_info)

  valid_bgm_infos += EXTRA_BGM_INFOS

  for spot_index in range(1, 0x78+1):
    bgm_info = self.rng.choice(valid_bgm_infos)
    write_u32(dol_data, address_to_offset(stage_bgm_info_list_start + spot_index*4), bgm_info)
    #print("Setting spot ID %d to BGM info %08X" % (spot_index, bgm_info))
  for island_num in range(1, 49+1):
    bgm_info = self.rng.choice(valid_bgm_infos)
    write_u32(dol_data, address_to_offset(island_bgm_info_list_start + island_num*4), bgm_info)
    #print("Setting island number %d to BGM info %08X" % (island_num, bgm_info))


# uncompyle6 version 3.7.4
# Python bytecode 3.7 (3394)
# Decompiled from: Python 3.6.9 (default, Apr 18 2020, 01:56:04)
# [GCC 8.4.0]
# Embedded file name: /marrow/package/release.py
# Compiled at: 2019-04-29 12:25:50
# Size of source mod 2**32: 610 bytes
"""Release information."""
from collections import namedtuple
version_info = namedtuple('version_info', ('major', 'minor', 'micro', 'releaselevel',
                                           'serial'))(2, 0, 1, 'final', 1)
version = '.'.join([str(i) for i in version_info[:3]]) + (version_info.releaselevel[0] + str(version_info.serial) if version_info.releaselevel != 'final' else '')
author = namedtuple('Author', ['name', 'email'])('Alice Bevan-McGregor', 'alice@gothcandy.com')
description = 'A collection of utilities for resolving object names, names to objects, and managing plugins/extensions.'
url = 'https://github.com/marrow/package/'


from tornado.web import url
from .handlers import PuzzlesListHandler, SinglePuzzleHandler
from .helpers import get_api_url_prefix

def initialize_routes(schema):
    url_prefix = get_api_url_prefix(schema)
    routes = [
        url(url_prefix + r"/", PuzzlesListHandler),
        url(url_prefix + r"/(?P<id>[0-9a-zA-Z]+)", SinglePuzzleHandler),
    ]
    return routes