import os
import shutil
import numpy as np
import cv2
import random
import copy

from keras.models import Sequential
from keras.layers.core import Dense, Flatten, Dropout
import tensorflow as tf

def qpixmap_to_array(qtpixmap):
    # qpixmap转换成array
    img = qtpixmap.toImage()
    temp_shape = (img.height(), img.bytesPerLine() * 8 // img.depth())
    temp_shape += (4,)
    ptr = img.bits()
    ptr.setsize(img.byteCount())
    result = np.array(ptr, dtype=np.uint8).reshape(temp_shape)
    result = result[..., :3]

    return result

def img_to_candy(img):
    # 图片转换成灰色,灰色图片转换为轮廓
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    img_candy = cv2.Canny(img_gray, 100, 200)
    return img_candy

def create_model(input_shape, output_dim, hidden_layer: dict):
    convolutional_layer = hidden_layer.get("convolutional_layer")
    fully_connected_layer = hidden_layer.get("fully_connected_layer")
    # 创建模型
    model = Sequential()
    model.add(Flatten(input_shape=input_shape))

    if convolutional_layer is not None:
        # 待实现
        # 处理卷积层
        pass

    if fully_connected_layer is not None:
        # 处理全连接层
        for index, item in enumerate(fully_connected_layer):
            model.add(Dense(item, activation='relu'))
            model.add(Dropout(0.2))
        model.add(Dense(output_dim, activation='softmax'))

    return model

def model_mutation():
    # 模型变异-变异程度由低到高
    # 1.完全继承/复制-降低lr重训练
    # 2.完全继承/复制-并重训练
    # 3.数量不变,结构重排,并重训练
    # 4.降低5%结构,并重训练
    # 5.增加5%结构,并重训练
    pass

def arr_mutation_rearrange(arr_old: list):
    # 随机重排,比如[1,2,3]排列成[2,1,3]
    arr_new = copy.deepcopy(arr_old)
    random.shuffle(arr_new)
    return arr_new

def arr_mutation_merge(arr_old: list):
    # 合并,层数减少,如[1,2,3]=>[3,3]或[1,5]
    arr_new = copy.deepcopy(arr_old)
    length = len(arr_new)
    if length <= 1:
        return arr_new
    index1, index2 = random.sample(range(0, length), 2)
    arr_new[index1] = arr_new[index1] + arr_new[index2]
    del arr_new[index2]
    return arr_new

def arr_mutation_split(arr_old: list):
    # 分裂,层数增加,如如[3,4]=>[1,3,3]或[2,2,3]等
    arr_new = copy.deepcopy(arr_old)
    index_arr = []
    for i, val in enumerate(arr_new):
        if val > 1:
            index_arr.append(i)
    if len(index_arr) <= 0:
        # 数组中没有可以分裂的
        return arr_new
    index_random = random.sample(index_arr, 1)[0]
    val0 = arr_new[index_random]
    val1 = random.randint(1, val0 - 1)
    val2 = val0 - val1
    del arr_new[index_random]
    arr_new.insert(index_random, val2)
    arr_new.insert(index_random, val1)
    return arr_new

def arr_mutation_increase(arr_old: list):
    arr_new = copy.deepcopy(arr_old)

    length = len(arr_new)
    random_index = random.randint(0, length - 1)
    increase = int(arr_new[random_index] * 0.05)
    if increase == 0:
        increase = 1
    arr_new[random_index] = arr_new[random_index] + increase
    return arr_new

def arr_mutation_decrease(arr_old: list):
    arr_new = copy.deepcopy(arr_old)

    length = len(arr_new)
    random_index = random.randint(0, length - 1)
    decrease = int(arr_new[random_index] * 0.05)
    if decrease == 0:
        decrease = 1
    arr_new[random_index] = arr_new[random_index] - decrease
    if arr_new[random_index] <= 0:
        arr_new[random_index] = 1
    return arr_new

def hidden_layer_mutation(hidden_layer: dict):
    convolutional_layer = hidden_layer.get("convolutional_layer")
    fully_connected_layer = hidden_layer.get("fully_connected_layer")

    return [
        {
            "mutation_type": "origin",
            "convolutional_layer": convolutional_layer,
            "fully_connected_layer": copy.deepcopy(fully_connected_layer)
        },
        {
            "mutation_type": "mutations_rearrange",
            "convolutional_layer": convolutional_layer,
            "fully_connected_layer": arr_mutation_rearrange(fully_connected_layer)
        },
        {
            "mutation_type": "mutations_merge",
            "convolutional_layer": convolutional_layer,
            "fully_connected_layer": arr_mutation_merge(fully_connected_layer)
        },
        {
            "mutation_type": "mutations_split",
            "convolutional_layer": convolutional_layer,
            "fully_connected_layer": arr_mutation_split(fully_connected_layer)
        },
        {
            "mutation_type": "mutations_increase",
            "convolutional_layer": convolutional_layer,
            "fully_connected_layer": arr_mutation_increase(fully_connected_layer)
        },
        {
            "mutation_type": "mutations_decrease",
            "convolutional_layer": convolutional_layer,
            "fully_connected_layer": arr_mutation_decrease(fully_connected_layer)
        }
    ]

def model_save(model, model_path):
    # 模型保存
    if not os.path.exists(model_path):
        os.makedirs(model_path)
    tf.saved_model.save(model, model_path)

def model_load(model_path):
    # 模型加载
    if not os.path.exists(model_path):
        print(f"[{model_path}] is not exists ,exit")
        exit(-1)
    model = tf.saved_model.load(model_path)
    return model

def create_folder(folder_path):
    # 删除模型
    if not os.path.exists(folder_path):
        os.makedirs(folder_path)

def remove_folder(folder_path):
    # 删除模型
    if os.path.exists(folder_path):
        shutil.rmtree(folder_path)

if __name__ == '__main__':
    a = [100, 101, 102, 103, 104, 105, 106, 107, 108]
    a = [1, 2, 3]
    print(a)
    b = arr_mutation_merge(a)
    c = arr_mutation_split(a)
    print(b)
    print(c)


import cupy
import cupyx.scipy.fft

from cupy import _core
from cupy._core import _routines_math as _math
from cupy._core import fusion
from cupy.lib import stride_tricks

import numpy

_dot_kernel = _core.ReductionKernel(
    'T x1, T x2',
    'T y',
    'x1 * x2',
    'a + b',
    'y = a',
    '0',
    'dot_product'
)

def _choose_conv_method(in1, in2, mode):
    if in1.ndim != 1 or in2.ndim != 1:
        raise NotImplementedError('Only 1d inputs are supported currently')

    if in1.dtype.kind in 'bui' or in2.dtype.kind in 'bui':
        return 'direct'

    if _fftconv_faster(in1, in2, mode):
        return 'fft'

    return 'direct'

def _fftconv_faster(x, h, mode):
    """
    .. seealso:: :func: `scipy.signal._signaltools._fftconv_faster`

    """
    # TODO(Dahlia-Chehata): replace with GPU-based constants.
    return True

def convolve(a, v, mode='full'):
    """Returns the discrete, linear convolution of two one-dimensional sequences.

    Args:
        a (cupy.ndarray): first 1-dimensional input.
        v (cupy.ndarray): second 1-dimensional input.
        mode (str, optional): `valid`, `same`, `full`

    Returns:
        cupy.ndarray: Discrete, linear convolution of a and v.

    .. seealso:: :func:`numpy.convolve`

    """  # NOQA
    if a.size == 0:
        raise ValueError('a cannot be empty')
    if v.size == 0:
        raise ValueError('v cannot be empty')
    if v.ndim > 1:
        raise ValueError('v cannot be multidimensional array')
    if v.size > a.size:
        a, v = v, a
    a = a.ravel()
    v = v.ravel()

    method = _choose_conv_method(a, v, mode)
    if method == 'direct':
        out = _dot_convolve(a, v, mode)
    elif method == 'fft':
        out = _fft_convolve(a, v, mode)
    else:
        raise ValueError('Unsupported method')
    return out

def _fft_convolve(a1, a2, mode):

    offset = 0
    if a1.shape[-1] < a2.shape[-1]:
        a1, a2 = a2, a1
        offset = 1 - a2.shape[-1] % 2

    # if either of them is complex, the dtype after multiplication will also be
    if a1.dtype.kind == 'c' or a2.dtype.kind == 'c':
        fft, ifft = cupy.fft.fft, cupy.fft.ifft
    else:
        fft, ifft = cupy.fft.rfft, cupy.fft.irfft

    dtype = cupy.result_type(a1, a2)
    n1, n2 = a1.shape[-1], a2.shape[-1]
    out_size = cupyx.scipy.fft.next_fast_len(n1 + n2 - 1)
    fa1 = fft(a1, out_size)
    fa2 = fft(a2, out_size)
    out = ifft(fa1 * fa2, out_size)

    if mode == 'full':
        start, end = 0, n1 + n2 - 1
    elif mode == 'same':
        start = (n2 - 1) // 2 + offset
        end = start + n1
    elif mode == 'valid':
        start, end = n2 - 1, n1
    else:
        raise ValueError(
            'acceptable mode flags are `valid`, `same`, or `full`.')

    out = out[..., start:end]

    if dtype.kind in 'iu':
        out = cupy.around(out)

    return out.astype(dtype, copy=False)

def _dot_convolve(a1, a2, mode):

    offset = 0
    if a1.size < a2.size:
        a1, a2 = a2, a1
        offset = 1 - a2.size % 2

    dtype = cupy.result_type(a1, a2)
    n1, n2 = a1.size, a2.size
    a1 = a1.astype(dtype, copy=False)
    a2 = a2.astype(dtype, copy=False)

    if mode == 'full':
        out_size = n1 + n2 - 1
        a1 = cupy.pad(a1, n2 - 1)
    elif mode == 'same':
        out_size = n1
        pad_size = (n2 - 1) // 2 + offset
        a1 = cupy.pad(a1, (n2 - 1 - pad_size, pad_size))
    elif mode == 'valid':
        out_size = n1 - n2 + 1

    stride = a1.strides[0]
    a1 = stride_tricks.as_strided(a1, (out_size, n2), (stride, stride))
    output = _dot_kernel(a1, a2[::-1], axis=1)
    return output

def clip(a, a_min, a_max, out=None):
    """Clips the values of an array to a given interval.

    This is equivalent to ``maximum(minimum(a, a_max), a_min)``, while this
    function is more efficient.

    Args:
        a (cupy.ndarray): The source array.
        a_min (scalar, cupy.ndarray or None): The left side of the interval.
            When it is ``None``, it is ignored.
        a_max (scalar, cupy.ndarray or None): The right side of the interval.
            When it is ``None``, it is ignored.
        out (cupy.ndarray): Output array.

    Returns:
        cupy.ndarray: Clipped array.

    .. seealso:: :func:`numpy.clip`

    Notes
    -----
    When `a_min` is greater than `a_max`, `clip` returns an
    array in which all values are equal to `a_max`.
    """
    if fusion._is_fusing():
        return fusion._call_ufunc(_math.clip,
                                  a, a_min, a_max, out=out)

    # TODO(okuta): check type
    return a.clip(a_min, a_max, out=out)

# sqrt_fixed is deprecated.
# numpy.sqrt is fixed in numpy 1.11.2.
sqrt = sqrt_fixed = _core.sqrt

cbrt = _core.create_ufunc(
    'cupy_cbrt',
    ('e->e', 'f->f', 'd->d'),
    'out0 = cbrt(in0)',
    doc='''Elementwise cube root function.

    .. seealso:: :data:`numpy.cbrt`

    ''')

square = _core.create_ufunc(
    'cupy_square',
    ('b->b', 'B->B', 'h->h', 'H->H', 'i->i', 'I->I', 'l->l', 'L->L', 'q->q',
     'Q->Q', 'e->e', 'f->f', 'd->d', 'F->F', 'D->D'),
    'out0 = in0 * in0',
    doc='''Elementwise square function.

    .. seealso:: :data:`numpy.square`

    ''')

absolute = _core.absolute

fabs = _core.create_ufunc(
    'cupy_fabs',
    ('e->e', 'f->f', 'd->d'),
    'out0 = abs(in0)',
    doc='''Calculates absolute values element-wise.
    Only real values are handled.

    .. seealso:: :data:`numpy.fabs`

    ''')

_unsigned_sign = 'out0 = in0 > 0'
_complex_sign = '''
if (in0.real() == 0) {
  out0 = (in0.imag() > 0) - (in0.imag() < 0);
} else {
  out0 = (in0.real() > 0) - (in0.real() < 0);
}
'''
sign = _core.create_ufunc(
    'cupy_sign',
    ('b->b', ('B->B', _unsigned_sign), 'h->h', ('H->H', _unsigned_sign),
     'i->i', ('I->I', _unsigned_sign), 'l->l', ('L->L', _unsigned_sign),
     'q->q', ('Q->Q', _unsigned_sign), 'e->e', 'f->f', 'd->d',
     ('F->F', _complex_sign), ('D->D', _complex_sign)),
    'out0 = (in0 > 0) - (in0 < 0)',
    doc='''Elementwise sign function.

    It returns -1, 0, or 1 depending on the sign of the input.

    .. seealso:: :data:`numpy.sign`

    ''')

heaviside = _core.create_ufunc(
    'cupy_heaviside',
    ('ee->e', 'ff->f', 'dd->d'),
    '''
    if (isnan(in0)) {
        out0 = in0;
    } else if (in0 == 0) {
        out0 = in1;
    } else {
        out0 = (in0 > 0);
    }
    ''',
    doc='''Compute the Heaviside step function.

    .. seealso:: :data:`numpy.heaviside`

    '''
)

_float_preamble = '''
#ifndef NAN
#define NAN __int_as_float(0x7fffffff)
#endif
'''
_float_maximum = ('out0 = (isnan(in0) | isnan(in1)) ? out0_type(NAN) : '
                  'out0_type(max(in0, in1))')
maximum = _core.create_ufunc(
    'cupy_maximum',
    ('??->?', 'bb->b', 'BB->B', 'hh->h', 'HH->H', 'ii->i', 'II->I', 'll->l',
     'LL->L', 'qq->q', 'QQ->Q',
     ('ee->e', _float_maximum),
     ('ff->f', _float_maximum),
     ('dd->d', _float_maximum),
     ('FF->F', _float_maximum),
     ('DD->D', _float_maximum)),
    'out0 = max(in0, in1)',
    preamble=_float_preamble,
    doc='''Takes the maximum of two arrays elementwise.

    If NaN appears, it returns the NaN.

    .. seealso:: :data:`numpy.maximum`

    ''',
    cutensor_op=('OP_MAX', 1, 1), scatter_op='max')

_float_minimum = ('out0 = (isnan(in0) | isnan(in1)) ? out0_type(NAN) : '
                  'out0_type(min(in0, in1))')
minimum = _core.create_ufunc(
    'cupy_minimum',
    ('??->?', 'bb->b', 'BB->B', 'hh->h', 'HH->H', 'ii->i', 'II->I', 'll->l',
     'LL->L', 'qq->q', 'QQ->Q',
     ('ee->e', _float_minimum),
     ('ff->f', _float_minimum),
     ('dd->d', _float_minimum),
     ('FF->F', _float_minimum),
     ('DD->D', _float_minimum)),
    'out0 = min(in0, in1)',
    preamble=_float_preamble,
    doc='''Takes the minimum of two arrays elementwise.

    If NaN appears, it returns the NaN.

    .. seealso:: :data:`numpy.minimum`

    ''',
    cutensor_op=('OP_MIN', 1, 1), scatter_op='min')

fmax = _core.create_ufunc(
    'cupy_fmax',
    ('??->?', 'bb->b', 'BB->B', 'hh->h', 'HH->H', 'ii->i', 'II->I', 'll->l',
     'LL->L', 'qq->q', 'QQ->Q',
     ('ee->e', 'out0 = fmax(in0, in1)'),
     ('ff->f', 'out0 = fmax(in0, in1)'),
     ('dd->d', 'out0 = fmax(in0, in1)'),
     'FF->F', 'DD->D'),
    'out0 = max(in0, in1)',
    doc='''Takes the maximum of two arrays elementwise.

    If NaN appears, it returns the other operand.

    .. seealso:: :data:`numpy.fmax`

    ''')

fmin = _core.create_ufunc(
    'cupy_fmin',
    ('??->?', 'bb->b', 'BB->B', 'hh->h', 'HH->H', 'ii->i', 'II->I', 'll->l',
     'LL->L', 'qq->q', 'QQ->Q',
     ('ee->e', 'out0 = fmin(in0, in1)'),
     ('ff->f', 'out0 = fmin(in0, in1)'),
     ('dd->d', 'out0 = fmin(in0, in1)'),
     'FF->F', 'DD->D'),
    'out0 = min(in0, in1)',
    doc='''Takes the minimum of two arrays elementwise.

    If NaN appears, it returns the other operand.

    .. seealso:: :data:`numpy.fmin`

    ''')

_nan_to_num_preamble = '''
template <class T>
__device__ T nan_to_num(T x, T nan, T posinf, T neginf) {
    if (isnan(x))
        return nan;
    if (isinf(x))
        return x > 0 ? posinf : neginf;
    return x;
}

template <class T>
__device__ complex<T> nan_to_num(complex<T> x, T nan, T posinf, T neginf) {
    T re = nan_to_num(x.real(), nan, posinf, neginf);
    T im = nan_to_num(x.imag(), nan, posinf, neginf);
    return complex<T>(re, im);
}
'''

_nan_to_num = _core.create_ufunc(
    'cupy_nan_to_num_',
    ('????->?', 'bbbb->b', 'BBBB->B', 'hhhh->h', 'HHHH->H',
     'iiii->i', 'IIII->I', 'llll->l', 'LLLL->L', 'qqqq->q', 'QQQQ->Q',
     ('eeee->e',
      'out0 = nan_to_num(in0, in1, in2, in3)'),
     ('ffff->f',
      'out0 = nan_to_num(in0, in1, in2, in3)'),
     ('dddd->d',
      'out0 = nan_to_num(in0, in1, in2, in3)'),
     ('Ffff->F',
      'out0 = nan_to_num(in0, in1, in2, in3)'),
     ('Dddd->D',
      'out0 = nan_to_num(in0, in1, in2, in3)')),
    'out0 = in0',
    preamble=_nan_to_num_preamble,
    doc='''Elementwise nan_to_num function.

    .. seealso:: :func:`numpy.nan_to_num`

    ''')

def _check_nan_inf(x, dtype, neg=None):
    if dtype.char in 'FD':
        dtype = cupy.dtype(dtype.char.lower())
    if dtype.char not in 'efd':
        x = 0
    elif x is None and neg is not None:
        x = cupy.finfo(dtype).min if neg else cupy.finfo(dtype).max
    elif cupy.isnan(x):
        x = cupy.nan
    elif cupy.isinf(x):
        x = cupy.inf * (-1)**(x < 0)
    return cupy.asanyarray(x, dtype)

def nan_to_num(x, copy=True, nan=0.0, posinf=None, neginf=None):
    """Replace NaN with zero and infinity with large finite numbers (default
    behaviour) or with the numbers defined by the user using the `nan`,
    `posinf` and/or `neginf` keywords.

    .. seealso:: :func:`numpy.nan_to_num`

    """
    if not isinstance(x, cupy.ndarray):
        out = cupy.full((), x)
    else:
        out = cupy.empty_like(x) if copy else x
    dtype = out.dtype
    nan = _check_nan_inf(nan, dtype)
    posinf = _check_nan_inf(posinf, dtype, False)
    neginf = _check_nan_inf(neginf, dtype, True)
    return _nan_to_num(x, nan, posinf, neginf, out=out)

def real_if_close(a, tol=100):
    """If input is complex with all imaginary parts close to zero, return real
    parts.
    "Close to zero" is defined as `tol` * (machine epsilon of the type for
    `a`).

    .. warning::

            This function may synchronize the device.

    .. seealso:: :func:`numpy.real_if_close`
    """
    if not issubclass(a.dtype.type, cupy.complexfloating):
        return a
    if tol > 1:
        f = numpy.finfo(a.dtype.type)
        tol = f.eps * tol
    if cupy.all(cupy.absolute(a.imag) < tol):
        a = a.real
    return a

@cupy._util.memoize(for_each_device=True)
def _get_interp_kernel(is_complex):
    in_params = 'raw V x, raw U idx, '
    in_params += 'raw W fx, raw Y fy, U len, raw Y left, raw Y right'
    out_params = 'Z y'  # output dtype follows NumPy's

    if is_complex:
        preamble = 'typedef double real_t;\n'
    else:
        preamble = 'typedef Z real_t;\n'
    preamble += 'typedef Z value_t;\n'
    preamble += cupy._sorting.search._preamble  # for _isnan

    code = r'''
        U x_idx = idx[i] - 1;

        if ( _isnan<V>(x[i]) ) { y = x[i]; }
        else if (x_idx < 0) { y = left[0]; }
        else if (x[i] == fx[len - 1]) {
            // searchsorted cannot handle both of the boundary points,
            // so we must detect and correct ourselves...
            y = fy[len - 1];
        }
        else if (x_idx >= len - 1) { y = right[0]; }
        else {
            const Z slope = (value_t)(fy[x_idx+1] - fy[x_idx]) / \
                            ((real_t)fx[x_idx+1] - (real_t)fx[x_idx]);
            Z out = slope * ((real_t)x[i] - (real_t)fx[x_idx]) \
                    + (value_t)fy[x_idx];
            if (_isnan<Z>(out)) {
                out = slope * ((real_t)x[i] - (real_t)fx[x_idx+1]) \
                      + (value_t)fy[x_idx+1];
                if (_isnan<Z>(out) && (fy[x_idx] == fy[x_idx+1])) {
                    out = fy[x_idx];
                }
            }
            y = out;
        }
    '''
    return cupy.ElementwiseKernel(
        in_params, out_params, code, 'cupy_interp', preamble=preamble)

def interp(x, xp, fp, left=None, right=None, period=None):
    """ One-dimensional linear interpolation.

    Args:
        x (cupy.ndarray): a 1D array of points on which the interpolation
            is performed.
        xp (cupy.ndarray): a 1D array of points on which the function values
            (``fp``) are known.
        fp (cupy.ndarray): a 1D array containing the function values at the
            the points ``xp``.
        left (float or complex): value to return if ``x < xp[0]``. Default is
            ``fp[0]``.
        right (float or complex): value to return if ``x > xp[-1]``. Default is
            ``fp[-1]``.
        period (None or float): a period for the x-coordinates. Parameters
            ``left`` and ``right`` are ignored if ``period`` is specified.
            Default is ``None``.

    Returns:
        cupy.ndarray: The interpolated values, same shape as ``x``.

    .. note::
        This function may synchronize if ``left`` or ``right`` is not already
        on the device.

    .. seealso:: :func:`numpy.interp`

    """

    if xp.ndim != 1 or fp.ndim != 1:
        raise ValueError('xp and fp must be 1D arrays')
    if xp.size != fp.size:
        raise ValueError('fp and xp are not of the same length')
    if xp.size == 0:
        raise ValueError('array of sample points is empty')
    if not x.flags.c_contiguous:
        raise NotImplementedError('Non-C-contiguous x is currently not '
                                  'supported')
    x_dtype = cupy.common_type(x, xp)
    if not cupy.can_cast(x_dtype, cupy.float64):
        raise TypeError('Cannot cast array data from'
                        ' {} to {} according to the rule \'safe\''
                        .format(x_dtype, cupy.float64))

    if period is not None:
        # The handling of "period" below is modified from NumPy's

        if period == 0:
            raise ValueError("period must be a non-zero value")
        period = abs(period)
        left = None
        right = None

        x = x.astype(cupy.float64)
        xp = xp.astype(cupy.float64)

        # normalizing periodic boundaries
        x %= period
        xp %= period
        asort_xp = cupy.argsort(xp)
        xp = xp[asort_xp]
        fp = fp[asort_xp]
        xp = cupy.concatenate((xp[-1:]-period, xp, xp[0:1]+period))
        fp = cupy.concatenate((fp[-1:], fp, fp[0:1]))
        assert xp.flags.c_contiguous
        assert fp.flags.c_contiguous

    # NumPy always returns float64 or complex128, so we upcast all values
    # on the fly in the kernel
    out_dtype = 'D' if fp.dtype.kind == 'c' else 'd'
    output = cupy.empty(x.shape, dtype=out_dtype)
    idx = cupy.searchsorted(xp, x, side='right')
    left = fp[0] if left is None else cupy.array(left, fp.dtype)
    right = fp[-1] if right is None else cupy.array(right, fp.dtype)
    kern = _get_interp_kernel(out_dtype == 'D')
    kern(x, idx, xp, fp, xp.size, left, right, output)
    return output


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

from operator import __or__ as OR
from functools import reduce

import six
from django.conf import settings
try:
    from django.utils.encoding import force_unicode as force_text
except ImportError:
    from django.utils.encoding import force_text
from django.utils.translation import ugettext_lazy as _
from django.template.response import TemplateResponse
from django.contrib.admin import helpers
from django.contrib.admin.utils import model_ngettext

from celery import chain

from edw.admin.entity.forms import EntitiesUpdateTermsAdminForm

def update_terms(modeladmin, request, queryset, task, template=None):
    """
    ENG: Update terms for multiple entities
    RUS: Обновляет термины для нескольких объектов
    """
    CHUNK_SIZE = getattr(settings, 'EDW_UPDATE_TERMS_ACTION_CHUNK_SIZE', 100)
    opts = modeladmin.model._meta
    app_label = opts.app_label

    if request.POST.get('post'):
        form = EntitiesUpdateTermsAdminForm(request.POST)

        if form.is_valid():
            to_set = [x.id for x in form.cleaned_data['to_set']]
            to_unset = [x.id for x in form.cleaned_data['to_unset']]
            n = queryset.count()
            if n and (to_set or to_unset):
                i = 0
                tasks = []
                while i < n:
                    chunk = queryset[i:i + CHUNK_SIZE]
                    for obj in chunk:
                        obj_display = force_text(obj)
                        modeladmin.log_change(request, obj, obj_display)
                    tasks.append(task.si([x.id for x in chunk], to_set, to_unset))
                    i += CHUNK_SIZE

                chain(reduce(OR, tasks)).apply_async()

                modeladmin.message_user(request, _("Successfully proceed %(count)d %(items)s.") % {
                    "count": n, "items": model_ngettext(modeladmin.opts, n)
                })
            # Return None to display the change list page again.
            return None

    else:
        form = EntitiesUpdateTermsAdminForm()

    if len(queryset) == 1:
        objects_name = force_text(opts.verbose_name)
    else:
        objects_name = force_text(opts.verbose_name_plural)

    title = _("Update terms for multiple entities")
    context = {
        "title": title,
        'form': form,
        "objects_name": objects_name,
        'queryset': queryset,
        "opts": opts,
        "app_label": app_label,
        'action_checkbox_name': helpers.ACTION_CHECKBOX_NAME,
        'media': modeladmin.media,
        'action': 'update_terms',
    }
    # Display the confirmation page
    kwargs = {} if six.PY3 else {'current_app': modeladmin.admin_site.name}
    return TemplateResponse(request, template if template is not None else "edw/admin/base_actions/update_terms.html",
                            context, **kwargs)

update_terms.short_description = _("Modify terms for selected %(verbose_name_plural)s")


# PROBLEM:
# Given an array A of non-negative integers, return an array
# consisting of all the even elements of A, followed by all
# the odd elements of A.
# You may return any answer array that satisfies this condition.

# EXAMPLE:
# Input: [3,1,2,4]
# Output: [2,4,3,1]
# The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted.

from typing import List

class Solution:

    # APPROACH: COMBINE 2 LIST
    # - In this approach we can create and 'evens'
    # and 'odds' list. Then we can itterate through
    # the given input list and store numbers either
    # in the evens list or odds list based on whether
    # or not A[i] % 2 == 0. Then we can return the
    # list concatenated together.

    # Runtime: 72 ms
    # Memory: 14.8 MB
    # Faster than 98.15% of Python Submissions.
    def approach(self, A: List[int]) -> List[int]:

        evens = []
        odds = []

        for num in range(len(A)):
            if A[num] % 2 == 0:
                evens.append(A[num])
            else:
                odds.append(A[num])

        # - If you want, you can have
        # the lists sorted as well for
        # output clarity.
        # evens.sort()
        # odds.sort()
        return evens + odds

if __name__ == '__main__':
    solution = Solution()
    A = [3,1,2,4,7,8,9,15]
    print(solution.approach(A))


from utils.faker_factory import faker

from ..mails import BaseMailView

class OpportunityReminderCloseMailView(BaseMailView):
    """
    """
    template_name = 'mails/opportunity/opportunity_reminder_close.html'

    mandatory_mail_args = [
        'title',
        'created_by_name',
        'duedate_timedelta',
        'duedate',
        'public_url',
    ]

    section = 'opportunities'
    subject = '%(duedate_timedelta)s until opportunity closure'

    def get_mock_data(self, optional=True):
        mock_data = {
            'title': '[Role Name] for [Project Name]',
            'created_by_name': '[SDM Name]',
            'duedate_timedelta': '3 days',
            'duedate': '[May 29, 12AM]',
            'disable_notification_url': None,
            'public_url': '/{}'.format(faker.uri_path()),
        }
        return mock_data


import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import solve_ivp

def system_of_odes(t, y):
    # Define the system of second-order ODEs
    # y is an array of shape (2n,), where n is the number of equations

    # Compute coefficients

    n = int(len(y) / 2)
    y1 = y[:n]  # x,y
    y2 = y[n:]  # dxdt, dydt
    dy1_dt = y2

    dy2_dt = -0.001*y2 - 3*y1
    return np.concatenate([dy1_dt, dy2_dt])

# Define the initial conditions
initial_conditions = [1, 0]  # Initial values for y1 and y2
initial_derivatives = [0, 1]  # Initial values for the derivatives dy1/dt and dy2/dt
initial_state = np.concatenate([initial_conditions, initial_derivatives])

# Define the time span for the solution
time_span = (0, 5)  # Solve from t=0 to t=1

# Solve the system of ODEs
solution = solve_ivp(system_of_odes, time_span, initial_state)

breakpoint()
# Access the solution
t_values = solution.t  # Array of time values
n=2
y1_values = solution.y[:n]  # Array of y1 values
y2_values = solution.y[n:]  # Array of y2 values

# Plot the solution
plt.plot(solution.t, y1_values[0], label='y1')
plt.plot(solution.t, y2_values[0], label='y2')
plt.xlabel('Time')
plt.ylabel('Solution')
plt.title('Solution of the System of ODEs')
plt.legend()
plt.grid(True)
plt.show()


#!usr/bin/env python
# -*- coding:utf-8 -*-
"""
@author: admin
@file: main.py
@time: 2021/09/02
@desc:
"""
import time
import torch
from model import config
from model.data_process import PrepareData
from model.Transformer import make_model
from model.LabelSmoothing import LabelSmoothing
from model.opt import NoamOpt
from train_evaluate import train
from predict import predict

def main():
    # 数据预处理
    data = PrepareData(config.TRAIN_FILE, config.DEV_FILE)
    src_vocab = len(data.en_word_dict)
    tgt_vocab = len(data.cn_word_dict)
    # src_vocab 5493
    # tgt_vocab 2537
    print("src_vocab %d" % src_vocab)
    print("tgt_vocab %d" % tgt_vocab)

    # 初始化模型
    model = make_model(
        src_vocab,
        tgt_vocab,
        config.LAYERS,
        config.D_MODEL,
        config.D_FF,
        config.H_NUM,
        config.DROPOUT
    )

    # 训练
    print(">>>>>>> start train")
    train_start = time.time()
    criterion = LabelSmoothing(tgt_vocab, padding_idx=0, smoothing=0.0)
    optimizer = NoamOpt(config.D_MODEL, 1, 2000,
                        torch.optim.Adam(model.parameters(), lr=0, betas=(0.9, 0.98), eps=1e-9))

    train(data, model, criterion, optimizer)
    print(f"<<<<<<< finished train, cost {time.time() - train_start:.4f} seconds")

    # 预测
    # 加载模型
    model.load_state_dict(torch.load(config.SAVE_FILE))
    # 开始预测
    print(">>>>>>> start predict")
    evaluate_start = time.time()
    predict(data, model)
    print(f"<<<<<<< finished evaluate, cost {time.time() - evaluate_start:.4f} seconds")

if __name__ == '__main__':
    main()


#!/local/cluster/bin/python
#biopython take three regions, invert middle, then put together
import sys
from Bio import SeqIO

strain=sys.argv[1]
#include 50 bp margin so as not to interrupt att site in rotated genome
largestart=int(sys.argv[2]) + 50
largeend=int(sys.argv[3]) - 50
infile="../" + strain + ".gbk"
outfile=strain + ".noSI.gbk"

record = SeqIO.read(infile, "genbank")
firstpart = record[1:largestart]
endpart = record[largeend:len(record.seq)]
newrecord = firstpart + endpart
fw=open(outfile,'w')
SeqIO.write(newrecord,fw,"genbank")


"""
Simple BBS
簡単な掲示板

要件:
1. ページ上部に大きくSimple BBSと書かれている
2. Username と Messageを入力するフォームがある
3. 送信と書かれたスイッチがある
4. 入力された文字が掲示板に表示されていく（下段に追加されていく）
5. Username に何も入力されていない状態で送信された場合は名無しさんにする
6. Message に何も入力されていない状態で送信された場合は空欄にする

"""
import os

from flask import Flask, render_template, request

app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def index():
    # 初めてページが選択された時
    if request.method == 'GET':

        # コメントの読み込み
        documents = []
        if os.path.isfile("document.txt"):
            with open('document.txt', 'r') as file:
                line = file.readline()[:-1]
                while line:
                    line_list = line.split(',')
                    # print(line_list)
                    documents.append(line_list)
                    line = file.readline()[:-1]

        return render_template('BBS.html', documents=documents)

    # 送信がクリックされた時
    if request.method == 'POST':
        username = request.form['username']
        message = request.form['message']

        # usernameがないときは"名無しさん"に変更
        if username == '':
            username = '名無しさん'

        # コメントの書き込み
        with open('document.txt', mode='a') as file:
            file.write(f'{username},{message}\n')

        # コメントの読み込み
        with open('document.txt', 'r') as file:
            documents = []
            line = file.readline()[:-1]
            while line:
                line_list = line.split(',')
                # print(line_list)
                documents.append(line_list)
                line = file.readline()[:-1]

        return render_template('BBS.html', documents=documents)

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


from scipy.stats import zscore
from datetime import datetime as dt
import numpy as np
import pandas as pd

RAW_DIR = "raw/"
RAW_TRAIN_PATH = RAW_DIR + "raw_train_data.csv"
RAW_PREDICT_PATH = RAW_DIR + "raw_predict_data.csv"
CYCLE_AMOUNT_PATH = RAW_DIR + "cycle_amount.csv"

INPUT_DIR = "input/"
TRAIN_DATA_PATH = INPUT_DIR + "train_data.csv"
TEST_DATA_PATH = INPUT_DIR + "test_data.csv"
PREDICT_DATA_PATH = INPUT_DIR + "predict_data.csv"

TEST_PERCENTAGE = 0.1

AMOUNT_LOW_LIMIT = 80
AMOUNT_HIGH_LIMIT = 180

class WeatherDataGenerator:

  #CLOSED_HOURS = [ "22:00", "23:00", "0:00", "1:00", "2:00", "3:00", "4:00", "5:00" ]
  CLOSED_HOURS = [ "22:00", "23:00", "0:00", "1:00", "2:00", "3:00", "4:00", "5:00", "13:00", "14:00", "15:00", "19:00", "20:00", "21:00"  ]

  def __init__(self, raw_data=None, amount_data=None):
    self.weather_data = pd.DataFrame()
    self.raw_data = raw_data
    self.amount_data = amount_data

  def generate_data(self):
    self.__store_split_datetime()
    self.__store_real_values()
    self.__drop_closed_hours()
    self.__pivot_date_x_hour()
    self.__store_categolized_values()
    self.__store_label_values()
    self.__drop_invalid_label_values()

  def get_data(self):
    return self.weather_data

  def __store_split_datetime(self):
    print("Splitting datetime to date and hour...")
    # index 1, 2, 3 is used later
    self.weather_data = self.raw_data[0].apply(lambda datehour: pd.Series(datehour.split(" "), index=[0,4]))

  def __drop_closed_hours(self):
    print("Dropping closed hours columns...")
    drop_rows = self.weather_data.loc[self.weather_data[4].isin(self.CLOSED_HOURS)]
    self.weather_data.drop(drop_rows.index, inplace=True)

  def __store_real_values(self):
    print("Storing temprature and precipiation and wind speed...")
    for j in [ 1, 2, 3 ]:
    #for j in [ 1, 3 ]: # Passing wind speed
      self.weather_data[j] = self.raw_data[j]

  def __normalize_real_values(self):
    print("Normalizing real values...")
    # Normalize real_value columns
    for j in [ 1, 2, 3 ]:
    #for j in [ 1, 3 ]: # Passing wind speed
      # Regression problems doesn't need to be normalized?
      self.weather_data[j] = zscore(self.weather_data[j], axis=0)

  def __pivot_date_x_hour(self):
    print("Pivoting columns date x hour...")
    # Pivot data to date x hour
    self.weather_data = self.weather_data.pivot(index=0, columns=4)

  def __store_categolized_values(self):
    print("Appending categolized values...")
    # Append oter weathers and labels after pivot
    for l in self.weather_data.index:
      date = dt.strptime(l, "%Y/%m/%d")
      self.weather_data.loc[l, 5] = date.month
      self.weather_data.loc[l, 6] = date.weekday()

  def __store_label_values(self):
    # Reset indexes of self.weather_data as default interger, to match index of two dataframes
    self.weather_data.reset_index(drop=True, inplace=True)

    if self.amount_data is None:
      print("Skipping appending label values...")
    else:
      print("Appending label values...")
      self.weather_data[7] = self.amount_data[0]

  def __drop_invalid_label_values(self):
    print("Dropping invalid label values...")
    #if self.weather_data[7] is None:
    if self.amount_data is None:
      print("Skipping dropping invalid label values...")
    else:
      drop_rows = self.weather_data[(AMOUNT_LOW_LIMIT <= self.weather_data[7]) & (self.weather_data[7] <= AMOUNT_HIGH_LIMIT)]
      self.weather_data.drop(drop_rows.index, inplace=True)

def read_raw_data():
  print("Reading weather and cycle amount data...")
  # Adding 0 - 3 numbers as header names.
  raw_train_data_df = pd.read_csv(RAW_TRAIN_PATH, header=None, names=np.arange(4))
  raw_predict_data_df = pd.read_csv(RAW_PREDICT_PATH, header=None, names=np.arange(4))
  amount_data_df = pd.read_csv(CYCLE_AMOUNT_PATH, header=None)

  return raw_train_data_df, raw_predict_data_df, amount_data_df

def make_train_test_data(weather_df):
  print("Make train and test data by TEST_PERCENTAGE...")
  # Select random columns from whole weather data with directed percentage
  test_df = weather_df.sample(frac=TEST_PERCENTAGE)
  train_df = weather_df.drop(test_df.index.values)

  return train_df, test_df

def raw_to_weather():
  print('*********************************')
  print('Generating train and test data...')
  print('*********************************')
  raw_train_data_df, raw_predict_data_df, amount_data_df = read_raw_data()
  train_data_generator = WeatherDataGenerator(raw_train_data_df, amount_data_df)
  train_data_generator.generate_data()
  train_df, test_df = make_train_test_data(train_data_generator.get_data())

  print('*********************************')
  print('Saving train and test data...')
  print('*********************************')
  train_df.to_csv(TRAIN_DATA_PATH, header=None)
  test_df.to_csv(TEST_DATA_PATH, header=None)

  print('*********************************')
  print('Generating predict data...')
  print('*********************************')
  predict_data_generator = WeatherDataGenerator(raw_predict_data_df)
  predict_data_generator.generate_data()
  predict_df = predict_data_generator.get_data()

  print('*********************************')
  print('Saving predict data...')
  print('*********************************')
  predict_df.to_csv(PREDICT_DATA_PATH, header=None)

def run():
  raw_to_weather()

if __name__ == "__main__":
  run()


#!/usr/bin/env python

"""
Neato control program to make a robot follow a line (like a roadway) and react
to signs in its path.
"""

import rospy
from geometry_msgs.msg import Twist, PoseWithCovariance, Pose, Point, Vector3
from sensor_msgs.msg import LaserScan, Image
import math
import numpy as np
import cv2
from cv_bridge import CvBridge
import helper_functions as hp
import signal
import sys

##### GLOBAL SPEED CONSTANT #####
rotate_speed_limit = 0.3

##### GLOBAl STATE CONSTANTS #####
DRIVE = 0
STOP = 1
LOOK_BOTH_WAYS = 2

class Controller:
  def __init__(self):

    ##### ROS INITIALIZATION #####
    rospy.init_node('caribou')
    self.pub = rospy.Publisher('cmd_vel', Twist, queue_size=10)
    self.command = Twist()
    self.threshold = 0 # TODO: CHANGE THIS NUMBER
    self.bridge = CvBridge()
    rospy.Subscriber('/camera/image_raw', Image, self.react_to_image)

    ##### IMAGE SIZE #####
    self.win_size = (640,480)
    self.win_height_cropped = 480*0.9

    ##### SET STATE #####
    self.state = DRIVE

    ##### INITIALIZE WINDOWS #####
    cv2.namedWindow('set_bounds')
    cv2.namedWindow('bw_window_cropped')
    cv2.namedWindow('Output')

    ##### INITIALIZE SIFT #####
    self.sift = cv2.SIFT()
    self.bf = cv2.BFMatcher()
    self.past_descriptors = []

    ##### SIGN REACTION BEHAVIOR #####
    self.pause_duration = rospy.Duration(3)
    self.ignore_stop_sign_threshold = self.pause_duration + rospy.Duration(3)
    self.last_stop_sign = rospy.Time.now() - self.ignore_stop_sign_threshold

    ##### COLOR PARAMETERS (hand-tweaked) #####
    settings_file = open('settings.txt', 'r')
    self.grey_lb = int(settings_file.readline())
    self.grey_ub = int(settings_file.readline())
    self.red_lb = eval(settings_file.readline())
    self.red_ub = eval(settings_file.readline())
    settings_file.close()

    ##### CALIBRATION SLIDERS #####
    cv2.createTrackbar('grey l', 'set_bounds', self.grey_lb , 255,
        self.set_grey_lower)
    cv2.createTrackbar('grey u', 'set_bounds', self.grey_ub , 255,
        self.set_grey_upper)
    cv2.createTrackbar('B l', 'set_bounds', self.red_lb[0], 255,
      self.set_b_l)
    cv2.createTrackbar('B u', 'set_bounds', self.red_ub[0], 255,
      self.set_b_u)
    cv2.createTrackbar('G l', 'set_bounds', self.red_lb[1] ,255,
      self.set_g_l)
    cv2.createTrackbar('G u', 'set_bounds', self.red_ub[1], 255,
        self.set_g_u)
    cv2.createTrackbar('R l', 'set_bounds', self.red_lb[2], 255,
        self.set_r_l)
    cv2.createTrackbar('R u', 'set_bounds', self.red_ub[2], 255,
        self.set_r_u)

    ##### START OFF STOPPED #####
    self.stop()
    self.send()

  def set_grey_lower(self, val):
    """ Use sliders to set GREY lower bound. """
    self.grey_lb = val

  def set_grey_upper(self, val):
    """ Use sliders to set GREY upper bound. """
    self.grey_ub = val

  def set_b_l(self, val):
    """ Use sliders to set BLUE lower bound. """
    self.red_lb[0] = val

  def set_b_u(self, val):
    """ Use sliders to set BLUE upper bound. """
    self.red_ub[0] = val

  def set_g_l(self, val):
    """ Use sliders to set BLUE lower bound. """
    self.red_lb[1] = val

  def set_g_u(self, val):
    """ Use sliders to set GREEN upper bound. """
    self.red_ub[1] = val

  def set_r_l(self, val):
    """ Use sliders to set RED lower bound. """
    self.red_lb[2] = val

  def set_r_u(self, val):
    """ Use sliders to set RED upper bound. """
    self.red_ub[2] = val

  def react_to_image(self, msg):
    """
    Process image messages from ROS and stash them in an attribute called
    cv_image for subsequent processing

    Grabs image stream from camera, called cv_image, and processes the image for
    line following and sign detection
    """
    self.cv_image = self.bridge.imgmsg_to_cv2(msg, desired_encoding="bgr8")

    cv2.waitKey(5)
    if self.state == DRIVE:
      direction = hp.find_line(self.cv_image,
        (0, self.win_height_cropped), self.win_size,
        (self.grey_lb, self.grey_lb, self.grey_lb),
        (self.grey_ub, self.grey_ub, self.grey_ub),
        self.threshold)
      self.drive(direction)
      sign_test = hp.find_stop_sign(self.cv_image,
          tuple(self.red_lb), tuple(self.red_ub))
      if (sign_test and
          (rospy.Time.now() - self.ignore_stop_sign_threshold) >
          self.last_stop_sign):
        rospy.Timer(self.pause_duration,
            self.look_both_ways, oneshot=True)
        self.state = STOP

    elif self.state == STOP:
      self.stop()

    elif self.state == LOOK_BOTH_WAYS:
      gray = cv2.cvtColor(self.cv_image, cv2.COLOR_BGR2GRAY)
      kp, des = self.sift.detectAndCompute(gray, None)
      if len(self.past_descriptors) > 10:
        previous_des = self.past_descriptors.pop(0)
        matches = self.bf.knnMatch(des, previous_des, k=2)
        # Apply ratio test
        good_count = 0
        for m,n in matches:
          if m.distance < 0.75*n.distance:
            good_count += 1
        if good_count > 0.6*len(previous_des):
          self.state = DRIVE
      self.past_descriptors.append(des)

    cv2.imshow("Output", self.cv_image)
    cv2.waitKey(5)

  def look_both_ways(self, event):
    """ Callback function to set the robot's state to LOOK_BOTH_WAYS """
    self.last_stop_sign = rospy.Time.now()
    self.state = LOOK_BOTH_WAYS

  def drive(self, direction):
    """ Changes self.command in response to the direction inputed """
    if direction[1]:
      if direction[0] == 0:
        self.command.angular.z = 0
        self.command.linear.x = .1
      else:
        proportion = (float(direction[0]) / (640/2))
        self.command.angular.z = (min(proportion, rotate_speed_limit)
            if proportion > 0 else max(proportion, -rotate_speed_limit))
        self.command.linear.x = .1 * (1 - abs(proportion))
    else:
      self.stop()

  def stop(self):
    """ Sets self.command to stop all bot motion """
    self.command.linear.x = 0
    self.command.angular.z = 0

  def send(self):
    """ Publishes self.command to ROS """
    self.pub.publish(self.command)

  def signal_handler(self, signal, frame):
    """ Saves calibration settings to settings.txt file before closing """
    settings_file = open('settings.txt', 'w')
    settings_file.write(str(self.grey_lb) + '\n')
    settings_file.write(str(self.grey_ub) + '\n')
    settings_file.write(str(self.red_lb) + '\n')
    settings_file.write(str(self.red_ub) + '\n')
    settings_file.close()
    print('Exiting gracefully...')
    sys.exit(0)

controller = Controller()
signal.signal(signal.SIGINT, controller.signal_handler)

while not rospy.is_shutdown():
  controller.send()


''' CAS schema's for the roads '''
__name__      = "CASSchema.py"
__author__    = "COUTAND Bastien"
__date__      = "07.12.22"

from datetime import datetime
from pydantic import BaseModel, Field

class CASBase(BaseModel):
    '''
        CAS Schema
    '''
    cas_ip: str = Field(
        description='ip for the CAS'
    )

    cas_port: int = Field(
        description='port for the CAS'
    )

class CASCreate(CASBase):
    '''
        CAS schema for the creation of an CAS in the database.
    '''
    pass

class CASInDB(CASBase):
    '''
        CAS schema for the db
    '''
    id: int = Field(
        description='ID in the database of the CAS'
    )

    created_at: datetime = Field(
        default=datetime.utcnow,
        description='Date of the creation for an CAS'
    )

    class Config:
        orm_mode = True


# -*- coding: utf-8 -*-
"""
Created on Thu Jul  5 16:06:36 2018

@author: jose.molina
"""

# -*- coding: utf-8 -*-
"""
Created on Thu Jul  5 15:39:40 2018

@author: jose.molina
"""

from bs4 import BeautifulSoup
from selenium import webdriver
import requests
from xml.etree import ElementTree
from time import sleep

import pandas as pd
from dateutil.relativedelta import relativedelta

import re

def getvalueofnode(node):
    """ return node text or None """
    return node if node is not None else None

dfcols = ['nombre', 'link', 'overall_rating','ranking','rango_precio','num_opiniones','ops_exc','ops_muybueno','ops_normal','ops_malo','ops_pesimo','punt_servicio','punt_comida','punt_calprecio','direccion','ubicacion','telefono']
df_xml = pd.DataFrame(columns=dfcols)

url = 'https://www.tripadvisor.es/Restaurants-g187514-Madrid.html#EATERY_OVERVIEW_BOX'
browser = webdriver.Chrome(r'C:\Users\Jose.Molina\Downloads\WinPython\projects\tripadvisor\chromedriver.exe')
#'/home/josemolina/programs_python/geckodriver'
browser.implicitly_wait(10)
browser.get(url)

#li id="alphabetical"
alpha = browser.find_element_by_id('alphabetical')
alpha.click()
browser.implicitly_wait(10)

contador = 0
next = True
#cada vez que empieza el bucle se recorre una página entera
while next == True:
    html = BeautifulSoup(browser.page_source, 'html.parser')
    table = html.find_all('div',{'data-index': re.compile(r".*")})
    for row in table:
        item = row.find('div', class_='title')
        link = item.find('a')
        link ="https://www.tripadvisor.es"+link['href']
        browser.get(link)
        #print(link['href'])
        #elemento = browser.find_element_by_xpath('//a[@href="'+link['href']+'"]')
        #elemento.click()

        browser.get(browser.current_url)
        bar_html = BeautifulSoup(browser.page_source,'html.parser')
        #contenido a scrapear

        name = bar_html.find('h1',{'class':'heading_title'})
        rating = bar_html.find('span',{'class':'overallRating'})
        ranking = (bar_html.find('span',{'class':'header_popularity'})).find('span')
        print(ranking.text)
        precio = (bar_html.find('span',{'class':['ui_column',"is-6","price"]})).find('span')
        print(precio.text)
        #fin contenido a scrapear

        df_xml = df_xml.append(
            pd.Series([getvalueofnode(name.text), getvalueofnode(link), getvalueofnode(rating.text),getvalueofnode(ranking.text),getvalueofnode(precio.text),'num_opiniones','ops_exc','ops_muybueno','ops_normal','ops_malo','ops_pesimo','punt_servicio','punt_comida','punt_calprecio','direccion','ubicacion','telefono'], index=dfcols),
            ignore_index=True)
        contador += 1
        print(f'Contrato numero: {contador}')

        browser.execute_script('window.history.go(-1)')
    			#if (times == 0):
        browser.get(browser.current_url)
        nextpage = browser.find_element_by_css_selector('a.nav').click()

#    if class = disabled :
#        next = False
#    else:
#
#
#    try:
#        nextpage = browser.find_element_by_css_selector('a.nav').click()
##       nextpage = browser.execute_script(" ta.restaurant_filter.paginate(this.getAttribute('data-offset'));; ta.trackEventOnPage('STANDARD_PAGINATION', 'next', '2', 0); return false;")
#        if (nextpage):
#            nextpage.click()
#        else:
#            next = False
#    except:
#        next = False
#browser.close()
   # expediente = browser.get(link.get_attribute('href'))
    #expediente.click()
df_xml.to_excel("tripadvisor_restaurantes_madrid.xlsx", index = False)

            #coger id de ventana actual
            #main_window = browser.cur


from typing import List

class Solution:
    def findReplaceString(self, s: str, indices: List[int], sources: List[str], targets: List[str]) -> str:
        for i, source, target in sorted(list(zip(indices, sources, targets)), reverse=True):
            l = len(source)
            if s[i:i + l] == source:
                s = s[:i] + target + s[i + l:]
        return s


from typing import Optional, List

import torch
import uuid
from torch import nn
from supertransformerlib import Core

class DefaultParameterLayer(nn.Module):
    """
    A NTM extension layer designed to contain within it the default
    state for some sort of parameter and to be manipulatable to create,
    interpolate, and reset batch elements to as fine a granularity as is provided

    It also contains a unique id which identifies what parameter id
    it is corrolated with.
    """
    def __init__(self,
                 parameter: nn.Parameter
                 ):
        super().__init__()
        self.ident = str(uuid.uuid1())
        self.default_parameter = parameter

    @torch.jit.export
    def make_batch(self,
                   batch_shape: Core.StandardShapeType
                   ):
        """
        :param batch_shape: The shape of the batch, in terms of an int, a list of ints, or a 1d tensor
        :return: A batch consisting of a broadcasted defaults
        """
        broadcast_shape: List[int] = Core.standardize_shape(batch_shape, "batch_shape").tolist()
        expansion_length = len(broadcast_shape)
        broadcast_shape += [-1] * self.default_parameter.dim()
        defaults = self.default_parameter
        for _ in range(expansion_length):
            defaults = defaults.unsqueeze(0)
        tensor = defaults.expand(broadcast_shape)
        return tensor

    @torch.jit.export
    def reset_to_parameters(self,
                            reset_probability: torch.Tensor,
                            tensor: torch.Tensor) -> torch.Tensor:
        """
        A small helper method, this will accept a fully expanded tensor and
        it's unbroadcasted defaults, then perform linear interpolation between them using the
        reset probabilities. A value of 0 will mean do not reset, while 1
        means completely reset

        :param reset_probability: A float tensor of values between 0..1. The rank of this tensor can
                                  only be greater than or equal to the rank of parameter 'tensor', and
                                  the dimensions here must match the initial dimensions of 'tensor'
        :param tensor:          A data tensor which we wish to interpolate with.
        :return: An interpolated tensor between the tensor and the defaults, mediated by the reset probability
        """
        defaults = self.default_parameter
        reset_values = defaults.expand_as(tensor)
        while reset_probability.dim() < reset_values.dim():
            reset_probability = reset_probability.unsqueeze(-1)
        updated_tensor = tensor * (1 - reset_probability) + reset_values * reset_probability
        return updated_tensor

    @torch.jit.export
    def force_reset_to_defaults(self,
                                reset_mask: torch.Tensor,
                                tensor: torch.Tensor)->torch.Tensor:
        """
        Forces a reset to default where the reset mask is marked as true

        :param reset_mask: A mask which matches tensor's dimensions on the initial dimensions. Elements
                            marked true will be reset to defaults
        :param tensor: The tensor to reset
        :return: A tensor which has had elements replaced with the mask where appropriate
        """
        defaults = self.default_parameter
        reset_values = defaults.expand_as(tensor)
        while reset_mask.dim() < reset_values.dim():
            reset_mask = reset_mask.unsqueeze(-1)
        updated_tensor = torch.where(reset_mask, reset_values, tensor)
        return updated_tensor

def make_memory_parameter(
        memory_size: int,
        memory_width: int,
        ensemble_shape: Optional[Core.StandardShapeType] = None,
        dtype: Optional[torch.dtype] = None,
        device: Optional[torch.device] = None
        )->DefaultParameterLayer:

    """
    Creates a functional DefaultParameterLayer for representing a memory
    parameter, which is capable of handling resetting to defaults.
    """

    shape = [memory_size, memory_width]
    if ensemble_shape is not None:
        ensemble_shape_list: List[int] = Core.standardize_shape(ensemble_shape, "ensemble_shape").tolist()
        shape = ensemble_shape_list + shape

    parameter = torch.zeros(shape, dtype = dtype, device=device)
    torch.nn.init.kaiming_uniform_(parameter)
    parameter = nn.Parameter(parameter)
    return DefaultParameterLayer(parameter)

def make_weights_parameter(memory_size: int,
                           num_heads: int,
                           ensemble_shape: Optional[Core.StandardShapeType] = None,
                           dtype: Optional[torch.dtype] = None,
                           device: Optional[torch.device] = None
                           ) -> DefaultParameterLayer:
    """

    Creates a functional weights layer to contain the default weights
    values and to be responsible for resetting the weights.

    :param memory_size: The size of the built memory
    :param num_heads: The number of heads the memory will manage
    :param ensemble_shape: The shape of the ensemble, if used
    :param dtype: The dtype
    :param device: The device.
    :return:
    """
    shape = [num_heads, memory_size]
    if ensemble_shape is not None:
        ensemble_shape_list: List[int] = Core.standardize_shape(ensemble_shape, "ensemble_shape").tolist()
        shape = ensemble_shape_list + shape

    parameter = torch.zeros(shape, dtype = dtype, device=device)
    torch.nn.init.kaiming_uniform_(parameter)
    parameter = nn.Parameter(parameter)
    return DefaultParameterLayer(parameter)


# -*- coding: utf-8 -*-
#######################
# deploy.urls
#######################
"""
  1. 部署服务
  2. add DNS
  3. nginx设置
  4. config check
  5. 诊断
"""
from django.urls import path
from deploy import views

urlpatterns = [
    path('health/', views.health,name="health"),
    path('start/',views.deploy,name="deploy"),
    path('setNginx/<env>/<target>/<project>/',views.set_project_nginx,name="set_project_nginx"),
    path('addDNS/<env>/<project>/',views.add_project_dns,name="add_project_dns"),
    path('configCheck/<project>/',views.project_config_check,name="project_config_check"),
    path('diagnose/<project>/',views.diagnose_project,name="diagnose_project"),
]


import matplotlib.pyplot as plt

from moisture_tracers import plotdir
from moisture_tracers.plot.figures.fig2_satellite_comparison import make_plot

def main():
    start_time = "20200201"
    grid = "lagrangian_grid"
    resolutions = ["km1p1", "km2p2", "km4p4"]
    lead_times = [30, 36, 42, 48]
    make_plot(start_time, grid, resolutions, lead_times)
    plt.savefig(
        plotdir + "fig4_satellite_comparison_{}_{}.png".format(start_time, grid)
    )

if __name__ == "__main__":
    import warnings

    warnings.filterwarnings("ignore")

    main()


from app.issue_detector import IssueDetector
from app.support_detector import SupportDetector
import pandas as pd
from pathlib import Path
import sys
from pydantic import BaseModel, Field

class SupportScoreCalculator(BaseModel):
    timestamp: str = Field()
    issue_detector: IssueDetector = Field(default=IssueDetector())
    support_detector: SupportDetector = Field(default=SupportDetector())

    def calculate(self, messages: list):
        # メッセージ群からissue（困り事・質問）に関係するメッセージを取得
        issues = self.issue_detector.evaluate(messages)
        if not issues:
            print("no issue exitst")
            sys.exit(0)

        records = []
        for issue_id in issues:
            # idから実際のメッセージを取得
            issue_message = self._get_target_message(messages, issue_id)
            if not issue_message:
                print("targe issue not found")
                continue
            # issueメッセージと関連がありそうなメッセージ群を抽出
            refrences = self._get_reference_messages(messages, issue_id)
            if not refrences:
                print("no refrence message")
                continue
            # メッセージ群の中で解決に貢献したメッセージを取得
            answer_ids = self.support_detector.evaluate(issue_message, refrences)
            for answer_id in answer_ids:
                # idから実際のメッセージを取得
                answer = self._get_target_message(messages, answer_id)
                records.append({"q": issue_message, "a": answer})
        df = self._create_df(records)
        # 質問と回答バインド情報をcsv出力
        self._save_result(df, "qa")
        grouped_df = df.groupby("answer_user_id").agg(support_score=("answer_user_id", "size")).reset_index()
        self._save_result(grouped_df, "support_score")
        return grouped_df

    def _get_target_message(self, message_objects, message_id):
        for obj in message_objects:
            obj_id = obj["id"]
            if obj_id == message_id:
                return obj

    def _get_reference_messages(self, message_objects, issue_id):
        messages = []
        reference_ids = []
        for obj in message_objects:
            referenced_message = obj.get("referenced_message")
            if referenced_message:
                obj_id = obj.get("id")
                parent_id = referenced_message["id"]
                if parent_id == issue_id or parent_id in reference_ids:
                    messages.append(obj)
                    # レコードが生成順にソートされる前提。
                    reference_ids.append(obj_id)
        return messages

    def _create_df(self, records):
        # issue（困り事・質問文）のmessageId, issueメッセージを投稿したuserId, 回答のmessageId, 回答者のuserId, issue文, 回答文をcsvに出力する
        rows = []
        for record in records:
            row = {
                "issue_id": record["q"]["id"],
                "issue_user_id": record["q"]["author"]["id"],
                "answer_id": record["a"]["id"],
                "answer_user_id": record["a"]["author"]["id"],
                "issue_message": record["q"]["content"].replace("\n", "\\n"),
                "answer_message": record["a"]["content"].replace("\n", "\\n"),
            }
            rows.append(row)
        df = pd.DataFrame(rows)
        return df

    def _save_result(self, df, prefix: str):
        report_dir = Path("result") / "tmp"
        df.to_csv(report_dir / f"{prefix}_{self.timestamp}.csv", index=False)


import boto3
import os
import botocore
import logging

from agief_experiment import utils

class Cloud:

    # EC2 instances will be launched into this subnet (in a vpc)
    subnet_id = 'subnet-0b1a206e'

    # For ECS, which cluster to use
    cluster = 'default'

    # When creating EC2 instances, the root ssh key to use
    mainkeyname = 'nextpair'

    # For compute hosts, which the security group to use
    ec2_compute_securitygroup_id = 'sg-98d574fc'

    # AZ for all EC2 instances
    availability_zone = 'ap-southeast-2a'

    # Placement group for EC2 instances
    placement_group = 'MNIST-PGroup'

    # Unique, case-sensitive identifier you provide to ensure
    # client_token = 'this_is_the_client_token_la_la_34'

    # The idempotency of the request.
    network_interface_id = 'eni - b2acd4d4'

    def __init__(self):
        pass

    def sync_experiment(self, remote):
        """
        Sync experiment from this machine to remote machine
        """

        print("\n....... Use remote-sync-experiment.sh to "
              "rsync relevant folders.")

        cmd = ("../remote/remote-sync-experiment.sh " +
               remote.host_key_user_variables())
        utils.run_bashscript_repeat(cmd, 15, 6)

    def remote_download_output(self, prefix, host_node):
        """ Download /output/prefix folder from remote storage (s3) to remote machine.
        :param host_node:
        :param prefix:
        :type host_node: RemoteNode
        """

        print("\n....... Use remote-download-output.sh to copy /output files "
              "from s3 (typically input and data files) with "
              "prefix = " + prefix + ", to remote machine.")

        cmd = ("../remote/remote-download-output.sh " + " " + prefix +
               " " + host_node.host_key_user_variables())
        utils.run_bashscript_repeat(cmd, 15, 6)

    def remote_docker_launch_compute(self, host_node):
        """
        Assumes there exists a private key for the given
        ec2 instance, at keypath
        """

        print("\n....... Launch compute node in a docker container "
              "on a remote host.")

        commands = '''
            export VARIABLES_FILE={0}
            source {0}
            cd $AGI_HOME/bin/node_coordinator
            ./run-in-docker.sh -d
        '''.format(host_node.remote_variables_file)

        return utils.remote_run(host_node, commands)

    def ecs_run_task(self, task_name):
        """ Run task 'task_name' and return the Task ARN """

        print("\n....... Running task on ecs ")
        client = boto3.client('ecs')
        response = client.run_task(
            cluster=self.cluster,
            taskDefinition=task_name,
            count=1,
            startedBy='pyScript'
        )

        logging.debug("LOG: " + response)

        length = len(response['failures'])
        if length > 0:
            logging.error("Could not initiate task on AWS.")
            logging.error("reason = " + response['failures'][0]['reason'])
            logging.error("arn = " + response['failures'][0]['arn'])
            logging.error(" ----- exiting -------")
            exit(1)

        if len(response['tasks']) <= 0:
            logging.error("could not retrieve task arn when initiating task "
                          "on AWS - something has gone wrong.")
            exit(1)

        task_arn = response['tasks'][0]['taskArn']
        return task_arn

    def ecs_stop_task(self, task_arn):

        print("\n....... Stopping task on ecs ")
        client = boto3.client('ecs')

        response = client.stop_task(
            cluster=self.cluster,
            task=task_arn,
            reason='pyScript said so!'
        )

        logging.debug("LOG: " + response)

    def ec2_start_from_instanceid(self, instance_id):
        """
        Run the chosen instance specified by instance_id
        :return: the instance AWS public and private ip addresses
        """

        print("\n....... Starting ec2 (instance id " + instance_id + ")")
        ec2 = boto3.resource('ec2')
        instance = ec2.Instance(instance_id)
        response = instance.start()

        print("LOG: Start response: " + response)

        instance_id = instance.instance_id

        ips = self.ec2_wait_till_running(instance_id)
        return ips

    def ec2_start_from_ami(self, name, ami_id, min_ram):
        """
        :param name:
        :param ami_id: ami id
        :param min_ram: (integer), minimum ram to allocate to ec2 instance
        :return: ip addresses: public and private, and instance id
        """

        print("\n....... Launching ec2 from AMI (AMI id " + ami_id +
              ", with minimum " + str(min_ram) + "GB RAM)")

        # minimum size, 15GB on machine, leaves 13GB for compute
        instance_type = None
        ram_allocated = 8
        if min_ram < 6:
            instance_type = 'm4.large'      # 8
            ram_allocated = 8
        elif min_ram < 13:
            instance_type = 'r3.large'      # 15.25
            ram_allocated = 15.25
        elif min_ram < 28:
            instance_type = 'r3.xlarge'     # 30.5
            ram_allocated = 30.5
        else:
            logging.error("cannot create an ec2 instance with that much RAM")
            exit(1)

        print("\n............. RAM to be allocated: " + str(ram_allocated) +
              " GB RAM")

        ec2 = boto3.resource('ec2')
        subnet = ec2.Subnet(self.subnet_id)

        # Set the correct Logz.io token in EC2
        logzio_token = os.getenv("AGI_LOGZIO_TOKEN")
        user_data = '''
        #!/bin/sh
        echo export AGI_LOGZIO_TOKEN=%s >> /etc/environment
        ''' % (logzio_token)

        instance = subnet.create_instances(
            DryRun=False,
            ImageId=ami_id,
            MinCount=1,
            MaxCount=1,
            KeyName=self.mainkeyname,

            SecurityGroupIds=[
                self.ec2_compute_securitygroup_id,
            ],
            InstanceType=instance_type,
            Placement={
                'AvailabilityZone': self.availability_zone,
                # 'GroupName': self.placement_group,
                'Tenancy': 'default'                # | 'dedicated' | 'host',
            },
            Monitoring={
                'Enabled': False
            },
            DisableApiTermination=False,
            InstanceInitiatedShutdownBehavior='terminate',      # | 'stop'
            # ClientToken=self.client_token,
            AdditionalInfo='started by run-framework.py',
            # IamInstanceProfile={
            #     'Arn': 'string',
            #     'Name': 'string'
            # },
            EbsOptimized=False,
            UserData=user_data
        )

        instance_id = instance[0].instance_id

        logging.debug("Instance launched %s", instance_id)

        # set name
        response = ec2.create_tags(
            DryRun=False,
            Resources=[
                instance_id,
            ],
            Tags=[
                {
                    'Key': 'Name',
                    'Value': name
                },
            ]
        )

        logging.debug("Set Name tag on instanceid: %s", instance_id)
        logging.debug("Response is: %s", response)

        ips = self.ec2_wait_till_running(instance_id)
        return ips, instance_id

    def ec2_wait_till_running(self, instance_id):
        """
        :return: the instance AWS public and private ip addresses
        """

        ec2 = boto3.resource('ec2')
        instance = ec2.Instance(instance_id)

        print("wait_till_running for instance: ", instance)

        instance.wait_until_running()

        ip_public = instance.public_ip_address
        ip_private = instance.private_ip_address

        print("Instance is up and running ...")
        self.print_ec2_info(instance)

        return {'ip_public': ip_public, 'ip_private': ip_private}

    def ec2_stop(self, instance_id):
        print("\n...... Closing ec2 instance (instance id " +
              str(instance_id) + ")")
        ec2 = boto3.resource('ec2')
        instance = ec2.Instance(instance_id)

        self.print_ec2_info(instance)

        response = instance.stop()

        print("stop ec2: ", response)

    def remote_upload_runfilename_s3(self, host_node, prefix, dest_name):
        cmd = ("../remote/remote-upload-runfilename.sh " + " " + prefix +
               " " + dest_name +
               host_node.host_key_user_variables())
        try:
            utils.run_bashscript_repeat(cmd, 3, 3)
        except Exception as e:
            logging.error("Remote Upload Failed for this file")
            logging.error("Exception: %s", e)

    def remote_upload_output_s3(self, host_node, prefix, no_compress,
                                csv_output):
        cmd = "../remote/remote-upload-output.sh " + prefix + " "
        cmd += host_node.host_key_user_variables() + " "
        cmd += str(no_compress) + " " + str(csv_output)
        utils.run_bashscript_repeat(cmd, 3, 3)

    def upload_folder_s3(self, bucket_name, key, source_folderpath):

        if not os.path.exists(source_folderpath):
            logging.warning("folder does not exist, cannot upload: " +
                            source_folderpath)
            return

        if not os.path.isdir(source_folderpath):
            logging.warning("path is not a folder, cannot upload: " +
                            source_folderpath)
            return

        for root, dirs, files in os.walk(source_folderpath):
            for f in files:
                filepath = os.path.join(source_folderpath, f)
                filekey = os.path.join(key, f)

                self.upload_file_s3(bucket_name, filekey, filepath)

    @staticmethod
    def upload_file_s3(bucket_name, key, source_filepath):

        try:
            if os.stat(source_filepath).st_size == 0:
                logging.warning("file is empty, cannot upload: " +
                                source_filepath)
                return
        except OSError:
            logging.warning("file does not exist, cannot upload: " +
                            source_filepath)
            return

        s3 = boto3.resource('s3')

        exists = True
        try:
            s3.meta.client.head_bucket(Bucket=bucket_name)
        except botocore.exceptions.ClientError as e:
            # If a client error is thrown, then check that it was a 404 error.
            # If it was a 404 error, then the bucket does not exist.
            error_code = int(e.response['Error']['Code'])
            if error_code == 404:
                exists = False

        if not exists:
            logging.warning("s3 bucket " + bucket_name +
                            " does not exist, creating it now.")
            s3.create_bucket(Bucket=bucket_name)

        print(" ... file = " + source_filepath + ", to bucket = " +
              bucket_name + ", key = " + key)
        response = s3.Object(bucket_name=bucket_name,
                             key=key).put(Body=open(source_filepath, 'rb'))

        logging.debug("Response = : ", response)

    @staticmethod
    def print_ec2_info(instance):
        print("Instance details.")
        print(" -- Public IP address is: ", instance.public_ip_address)
        print(" -- Private IP address is: ", instance.private_ip_address)
        print(" -- id is: ", str(instance.instance_id))


from django.shortcuts import render

# Create your views here.
def func(request, num1, num2):
    if num2 != 0:
        div = num1 / num2
    else:
        div = '계산할 수 없습니다.'

    context = {
        'num1' : num1,
        'num2' : num2,
        'sub' : num1 - num2,
        'mul' : num1 * num2,
        'div' : div
    }
    return render(request, 'calculators/calculator.html', context)


import dotenv
import openai
import os

dotenv.load_dotenv('../.env')
openai.api_key = os.environ["OPENAI_API_KEY"]

response = openai.ChatCompletion.create(
    model="gpt-3.5-turbo",
    messages=[
      {"role": "user", "content": "tell me about gpt for social good"}
    ]
)

print(response["choices"][0]["message"]["content"])


from pydantic import BaseModel
import json
import requests
import Console
import config

HTTP_PREFIX = "http://"
HOST = config.server_address + "/internal"

class DownloadFileFromAgentInputType(BaseModel):
    ip_address: str
    file_path: str

class ListFilesFromAgentInputType(BaseModel):
    ip_address: str
    dir_path: str

class MonitorClipboardOnAgentInputType(BaseModel):
    ip_address: str
    duration: int

class GenerateFirstCodeForAgentInputType(BaseModel):
    ip_address: str

class DisconnectAgentInputType(BaseModel):
    ip_address: str

def download_file_from_agent(input: DownloadFileFromAgentInputType):

    data = json.dumps(input.__dict__)

    response = requests.post(url=HTTP_PREFIX+HOST+"/downloadFile", data=data)

    if response.status_code != 200:
        Console.console.print(f"Could not schedule downloading file from agent {input.ip_address}", style="error")
        Console.console.print(response.json()['detail'], style="error")
    else:
        Console.console.print(f'Task for downloading file from an agent scheduled successfully with id:'
                              f' {response.json()["command_id"]}', style="success")

    return response.json()

def list_files_from_agent(input: ListFilesFromAgentInputType):

    data = json.dumps(input.__dict__)

    response = requests.post(url=HTTP_PREFIX+HOST+"/listFiles", data=data)

    if response.status_code != 200:
        Console.console.print(f"Could not schedule listing files from agent {input.ip_address}", style="error")
        Console.console.print(response.json()['detail'], style="error")
    else:
        Console.console.print(f'Task for listing files from an agent scheduled successfully with id:'
                              f' {response.json()["command_id"]}',
                              style="success")

    return response.json()

def monitor_clipboard_on_agent(input: MonitorClipboardOnAgentInputType):

    data = json.dumps(input.__dict__)

    response = requests.post(url=HTTP_PREFIX+HOST+"/monitorClipboard", data=data)

    if response.status_code != 200:
        Console.console.print(f"Could not schedule monitoring clipboard on agent {input.ip_address}", style="error")
        Console.console.print(response.json()['detail'], style="error")
    else:
        Console.console.print(f'Task for monitoring clipboard on agent scheduled successfully with id:'
                              f' {response.json()["command_id"]}',
                              style="success")

    return response.json()

def generate_first_code_for_agent(input: GenerateFirstCodeForAgentInputType):

    data = json.dumps(input.__dict__)

    response = requests.post(url=HTTP_PREFIX+HOST+"/generateAgentCode", data=data)

    if response.status_code == 200:
        Console.console.print('Code generated successfully', style="success")

    return response.json()['code']

def disconnect_agent(input: DisconnectAgentInputType):

    data = json.dumps(input.__dict__)

    response = requests.post(url=HTTP_PREFIX+HOST+"/disconnectAgent", data=data)

    if response.status_code != 200:
        Console.console.print(f"Could not schedule disconnecting agent {input.ip_address}", style="error")
        Console.console.print(response.json()['detail'], style="error")
    else:
        Console.console.print(f'Task for disconnecting agent scheduled successfully with id:'
                              f' {response.json()["command_id"]}',
                              style="success")

    return response.json()

def list_agents():

    response = requests.get(url=HTTP_PREFIX+HOST+"/agents")

    if response.status_code == 200:
        return response.json()['agents']

    return None


# -*- coding: utf-8 -*-
"""
Created on Thu Apr 15 18:08:59 2021

@author: Chris
"""

#%% Imports

from PIL import ImageGrab
import win32gui
import numpy as np
import time
import cv2

#%% Get the ID of the Emulator window

# List to hold window information
window = []
# Name of the window to find
window_name = "Super Street Fighter II (USA) - Snes9x 1.60"

# Function to extract emulator window information
# Given a window and checks if its visible and if it matches the required name
# IF the above are true then we add the information to the window list
def winEnumHandler(hwnd, ctx, window_name=window_name):
    if win32gui.IsWindowVisible(hwnd):
        print(win32gui.GetWindowText(hwnd))
        if win32gui.GetWindowText(hwnd) == window_name:
            window.append(hwnd)
            window.append(win32gui.GetWindowText(hwnd))

# Function to get the screen
# Uses the enumerate windows function from wn32gui with our handler to get the
# correct window.
win32gui.EnumWindows(winEnumHandler, None)

#%% Window streaming

# Pixelwise relative corrections for the window bounding box
screen_correction = np.array([-8,-51,8,8])

# Loop to capture the window
while True:
    try:
        # Get the start time
        start_time = time.time()

        # Get the bounding box for the window
        bbox = np.array(win32gui.GetWindowRect(window[0]))
        # Correct the window size
        bbox = tuple(bbox - screen_correction)
        # Get the screen capture
        screen_grab = np.array(ImageGrab.grab(bbox))

        # Prints the time it took to collect the screenshot
        print(f"loop took {time.time()-start_time} seconds")

        # Reset the start time for the next loop
        start_time=time.time()

        # Display the image in a new window
        cv2.imshow("window", cv2.cvtColor(screen_grab, cv2.COLOR_BGR2RGB))
        # Checks to see if window should be closed and loop stopped
        if cv2.waitKey(25) & 0xFF == ord('q'):
            cv2.destroyAllWindows()
            break

    except Exception as e:
        print("error", e)
# %%


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Filename : kalao_pump_switch.sh
# @Date : 2023-01-12-14-12
# @Project: KalAO-ICS
# @AUTHOR : Janis Hagelberg
"""
kalao_pump_switch.py is part of the KalAO Instrument Control Software it is a maintenance script used to switch
the water cooling pump from on to off and opposite.
(KalAO-ICS).
"""

from kalao.plc import temperature_control
import argparse

if __name__ == "__main__":
    parser = argparse.ArgumentParser()

    parser.add_argument('--on', help='Turn pump ON', action='store_true')
    parser.add_argument('--off', dest='on', action='store_false')
    parser.set_defaults(feature=False)
    args = parser.parse_args()

    #if temperature_control.pump_status() == 'ON':
    if args.on:
        print("Switching pump ON")
        temperature_control.pump_on()
    else:
        print("Switching pump OFF")
        temperature_control.pump_off()


import discord
from discord.ext import commands
import time
import json
import os

if not os.path.isfile('config.json'):
    exit

f = open('config.json')
data = json.load(f)
TOKEN = data['token']
servers = data['servers']
message = data['message']
delay = int(data['delay'])
for i in range(len(servers)):
    servers[i] = int(servers[i])

bot = discord.Client(token=TOKEN)

@bot.event
async def on_guild_channel_create(channel):
    if channel.guild.id in servers:
        print(channel, "had just been created")
        time.sleep(delay)
        await channel.send(message)
bot.run(TOKEN)


import sys
import argparse

from lettuce.bin import main as lettuce_main
from lettuce import world
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from salad.steps.everything import *
from salad.terrains.everything import *

BROWSER_CHOICES = [browser.lower()
                   for browser in DesiredCapabilities.__dict__.keys()
                   if not browser.startswith('_')]
BROWSER_CHOICES.sort()
DEFAULT_BROWSER = 'firefox'

class store_driver_and_version(argparse.Action):
    drivers = BROWSER_CHOICES

    def __call__(self, parser, namespace, values, option_string=None):
        driver_info = values.split('-')
        if driver_info[0] not in self.drivers:
            args = {'driver': driver_info[0],
                    'choices': ', '.join(map(repr, self.drivers))}
            message = 'invalid choice: %(driver)r (choose from %(choices)s)'
            raise argparse.ArgumentError(self, message % args)
        setattr(namespace, self.dest, driver_info[0])
        if len(driver_info) > 1:
            setattr(namespace, 'version', driver_info[1])
        if len(driver_info) > 2:
            setattr(namespace, 'platform', driver_info[2].replace('_', ' '))

def main(args=sys.argv[1:]):
    parser = argparse.ArgumentParser(prog="Salad",
                                     description=("BDD browswer-automation "
                                                  "made tasty."))

    parser.add_argument('--browser', default=DEFAULT_BROWSER,
                        action=store_driver_and_version, metavar='BROWSER',
                        help=('Browser to use. Options: %s Default is %s.' %
                              (BROWSER_CHOICES, DEFAULT_BROWSER)))

    parser.add_argument('--remote-url',
                        help='Selenium server url for remote browsers')

    parser.add_argument('--name',
                        help=('Give your job a name so it '
                              'can be identified on saucelabs'))

    parser.add_argument('--timeout',
                        help=("Set the saucelabs' idle-timeout for the job"))

    (parsed_args, leftovers) = parser.parse_known_args()
    world.drivers = [parsed_args.browser]
    world.remote_url = parsed_args.remote_url
    world.remote_capabilities = {}

    if 'version' in parsed_args:
        world.remote_capabilities['version'] = parsed_args.version

    if 'platform' in parsed_args:
        world.remote_capabilities['platform'] = parsed_args.platform

    name = _get_current_timestamp() + " -  "
    if not parsed_args.name:
        name += "unnamed job"
    else:
        name += parsed_args.name
    world.remote_capabilities['name'] = name

    if not parsed_args.timeout:
        world.remote_capabilities['idle-timeout'] = 120
    else:
        world.remote_capabilities['idle-timeout'] = parsed_args.timeout

    lettuce_main(args=leftovers)

def _get_current_timestamp():
    from time import strftime
    import datetime
    return datetime.datetime.strftime(datetime.datetime.now(),
                                      '%d.%m.%Y %H:%M')

if __name__ == '__main__':
    main()


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

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('ventas', '0048_auto_20160928_0742'),
    ]

    operations = [
        migrations.AlterField(
            model_name='comanda',
            name='area_encargada',
            field=models.CharField(max_length=3, verbose_name=b'Area Encargada', choices=[(b'COC', b'Cocina'), (b'BAR', b'Barra')]),
        ),
        migrations.AlterField(
            model_name='comanda',
            name='estado_comanda',
            field=models.CharField(max_length=3, verbose_name=b'Estado Comanda', choices=[(b'PEN', b'Pendiente'), (b'PRO', b'Procesada'), (b'CAN', b'Cancelada')]),
        ),
        migrations.AlterField(
            model_name='comanda',
            name='fecha_hora_pedido_comanda',
            field=models.DateTimeField(verbose_name=b'Fecha/hora Pedido Comanda'),
        ),
        migrations.AlterField(
            model_name='comanda',
            name='fecha_hora_procesamiento_comanda',
            field=models.DateTimeField(null=True, verbose_name=b'Fecha/hora Procesamiento Comanda', blank=True),
        ),
        migrations.AlterField(
            model_name='comanda',
            name='producto_a_elaborar',
            field=models.ForeignKey(verbose_name=b'Producto a Elaborar', to='stock.ProductoCompuesto'),
        ),
        migrations.AlterField(
            model_name='venta',
            name='apertura_caja',
            field=models.ForeignKey(default=1, verbose_name=b'Apertura de Caja', to='ventas.AperturaCaja', help_text=b'Se asigna dependiendo del usuario logueado y de si posee una Apertura de Caja vigente.'),
        ),
        migrations.AlterField(
            model_name='venta',
            name='numero_factura_venta',
            field=models.ForeignKey(related_name='numero_factura', default=1, verbose_name=b'Numero de Factura de la Venta', to='bar.FacturaVenta', help_text=b'El Numero de Factura se asigna al momento de confirmarse la Venta.'),
        ),
    ]


import numpy as np
import math

import torch
import torch.nn as nn

class Upper(nn.Module):  # Upper: Mutual Information Contrastive Learning Upper Bound
    '''
        This class provides the Upper bound estimation to I(X,Y)
        Method:
            forward() :      provides the estimation with input samples
            loglikeli() :   provides the log-likelihood of the approximation q(Y|X) with input samples
        Arguments:
            x_dim, y_dim :         the dimensions of samples from X, Y respectively
            hidden_size :          the dimension of the hidden layer of the approximation network q(Y|X)
            x_samples, y_samples : samples from X and Y, having shape [sample_size, x_dim/y_dim]
    '''
    def __init__(self, x_dim, y_dim, hidden_size):
        super(Upper, self).__init__()
        # p_mu outputs mean of q(Y|X)
        self.p_mu = nn.Sequential(nn.Linear(x_dim, hidden_size//2),
                                       nn.ReLU(),
                                       nn.Linear(hidden_size//2, y_dim))
        # p_logvar outputs log of variance of q(Y|X)
        self.p_logvar = nn.Sequential(nn.Linear(x_dim, hidden_size//2),
                                       nn.ReLU(),
                                       nn.Linear(hidden_size//2, y_dim),
                                       nn.Tanh())

    def get_mu_logvar(self, x_samples):
        mu = self.p_mu(x_samples)
        logvar = self.p_logvar(x_samples)
        return mu, logvar

    def forward(self, x_samples, y_samples):
        mu, logvar = self.get_mu_logvar(x_samples)

        # log of conditional probability of positive sample pairs
        positive = - (mu - y_samples)**2 /2./logvar.exp()

        prediction_1 = mu.unsqueeze(1)          # shape [nsample,1,dim]
        y_samples_1 = y_samples.unsqueeze(0)    # shape [1,nsample,dim]

        # log of conditional probability of negative sample pairs
        negative = - ((y_samples_1 - prediction_1)**2).mean(dim=1)/2./logvar.exp()

        return (positive.sum(dim = -1) - negative.sum(dim = -1)).mean()

    def loglikeli(self, x_samples, y_samples): # unnormalized loglikelihood
        mu, logvar = self.get_mu_logvar(x_samples)
        return (-(mu - y_samples)**2 /logvar.exp()-logvar).sum(dim=1).mean(dim=0)

    def learning_loss(self, x_samples, y_samples):
        return - self.loglikeli(x_samples, y_samples)

class Lower(nn.Module):
    def __init__(self, x_dim, y_dim, hidden_size):
        super(Lower, self).__init__()
        self.F_func = nn.Sequential(nn.Linear(x_dim + y_dim, hidden_size),
                                    nn.ReLU(),
                                    nn.Linear(hidden_size, 1))

    def forward(self, x_samples, y_samples):
        # shuffle and concatenate
        sample_size = y_samples.shape[0]

        x_tile = x_samples.unsqueeze(0).repeat((sample_size, 1, 1))
        y_tile = y_samples.unsqueeze(1).repeat((1, sample_size, 1))

        T0 = self.F_func(torch.cat([x_samples,y_samples], dim = -1))
        T1 = self.F_func(torch.cat([x_tile, y_tile], dim = -1))-1.  #shape [sample_size, sample_size, 1]

        lower_bound = T0.mean() - (T1.logsumexp(dim = 1) - np.log(sample_size)).exp().mean()
        return lower_bound

    def learning_loss(self, x_samples, y_samples):
        return -self.forward(x_samples, y_samples)


import os
import csv
from decimal import Decimal
from forex_python.bitcoin import BtcConverter
from forex_python.converter import CurrencyRates
from plugin import plugin, require

FILE_PATH = os.path.abspath(os.path.dirname(__file__))

@require(network=True)
@plugin('currencyconv')
class Currencyconv():
    """
    Convert an amount of money from a currency to another.
    -- Type currencyconv, press enter and follow the instructions!
    """

    def __call__(self, jarvis, s):
        currencies = self.find_currencies()

        amount = jarvis.input_number('Enter an amount: ')
        from_currency = self.get_currency(jarvis, 'Enter from which currency: ', currencies)
        to_currency = self.get_currency(jarvis, 'Enter to which currency: ', currencies)

        self.currencyconv(jarvis, amount, from_currency, to_currency)

    def currencyconv(self, jarvis, amount, fr, to):
        """
        currencyconv converts the given amount to another currency
        using fore-python
        """

        b = BtcConverter(force_decimal=True)
        c = CurrencyRates(force_decimal=True)

        if (to == "BTC"):
            result = b.convert_to_btc(Decimal(amount), fr)
        elif (fr == "BTC"):
            result = b.convert_btc_to_cur(Decimal(amount), to)
        else:
            result = c.convert(fr, to, Decimal(amount))
        outputText = str(amount) + " " + fr + \
            " are equal to " + str(result) + " " + to
        jarvis.say(outputText)

    def find_currencies(self):
        """
        find_currency creates a dict with the inputs that forex-python accepts
        """

        with open(os.path.join(FILE_PATH, "../data/currencies.csv"), mode='r') as infile:
            reader = csv.reader(infile)
            mydict = {r.upper(): row[2] for row in reader for r in row[0:3]}
        return mydict

    def get_currency(self, jarvis, prompt, currencies):
        """
        get_currency checks if the input the user gave is valid based
        on the dictionary of find_currencies
        """

        while True:
            c = jarvis.input(prompt).upper()
            if c in currencies:
                return currencies[c]
            elif c == "show help".upper():
                print(', '.join(set(currencies.values())))
                prompt = 'Please enter a valid country or currency: '
                continue
            elif c == "try again".upper():
                prompt = 'Please enter a valid country or currency: '
                continue
            else:
                prompt = 'Type -show help- to see valid currencies '\
                         'or -try again- to continue: '


import matplotlib.pyplot as plt
import pdb
import numpy as np
import csv
import time

def PlotDemo1(a, b):
    a1 = []
    b1 = []
    a1 = a
    b1 = b
    fig  = plt.figure()
    ax = fig.add_subplot(1,1,1)
    ax.plot(a,b)
    plt.show()

def PlotDemo(a,zero):
    a1 = []
    b1 = []
    a1 = a
    fig  = plt.figure()
    ax = fig.add_subplot(1,1,1)
    ax.plot(a)
    ax.plot(zero)
    plt.show()

def Run(symbol):
    csv_reader = csv.reader(open("E:/MyGit/PythonStock/获取当数据并画分时图/data/"+time.strftime('%Y%m%d')+"/"+symbol+".csv",'r', encoding='utf-8'))
    #pdb.set_trace()
    PICES1 = [row[4] for row in csv_reader]
    csv_reader1 = csv.reader(open("E:/MyGit/PythonStock/获取当数据并画分时图/data/"+time.strftime('%Y%m%d')+"/"+symbol+".csv",'r', encoding='utf-8'))
    TIME1 = [row[10] for row in csv_reader1]
    csv_reader2 = csv.reader(open("E:/MyGit/PythonStock/获取当数据并画分时图/data/"+time.strftime('%Y%m%d')+"/"+symbol+".csv",'r', encoding='utf-8'))
    yesterday = [row[3] for row in csv_reader2][2]

    TIME2 = [row[4] for row in csv_reader]
    PICES3 = [row[5] for row in csv_reader]
    TIME3 = [row[6] for row in csv_reader]
    PICES4 = [row[7] for row in csv_reader]
    TIME4 = [row[8] for row in csv_reader]
    PICES5 = [row[9] for row in csv_reader]
    TIME5 = [row[10] for row in csv_reader]

    print(PICES1)
    print(TIME1)
    print( yesterday)
    print(TIME2)
    print(PICES3)
    print(TIME3)
    print(PICES4)
    print(TIME4)
    print(PICES5)
    print(TIME5)
    #PlotDemo1()
    #res = [x-1 for x in PICES1]
    a = PICES1.remove('当前价格')
    b = TIME1.remove('时间')
    print(PICES1)
    res = list(map(float,PICES1))
    res = [x-float(yesterday) for x in res]
    res = [x/float(yesterday)*100 for x in res]
    zero = [0 for i in range(len(res))]
    print(zero)
    print(TIME1)
    PlotDemo(res,zero)

if __name__ == '__main__':
    Run("300736")

获取当日数据并画分时图/画出走势图.py