import meegkit
from meegkit.asr import ASR, clean_windows
from meegkit.utils.matrix import sliding_window
import numpy as np

from mne_bids import make_bids_basename, read_raw_bids, write_raw_bids

from os.path import join, exists
from os import makedirs

import mne

def apply_zapline(raw, fline, picks = "eeg", nremove=1):
    sfreq = raw.info["sfreq"]
    ix = mne.channel_indices_by_type(raw.info, picks)["eeg"]
    data = np.transpose(raw._data[ix,:])
   # data = np.expand_dims(data, 2)

    denoised_data, artifacts = meegkit.dss.dss_line(data, fline, sfreq, nremove=nremove)

    raw._data[ix,:] = np.transpose(denoised_data)

    return raw

def asr(X, sfreq, ref_maxbadchannels = 0.075, ref_tolerances = [-3.5, 5.5], ref_wndlen = 1):

    asr = ASR(method='euclid', sfreq = sfreq)

    nchan = X.shape[0]

    print(X.shape, nchan)

    data = X * 1000000

    asr.fit(data)
    print("ASR calibrated.")

    # Apply filter using sliding (non-overlapping) windows
    X = sliding_window(data, window=int(sfreq), step=int(sfreq))
    Y = np.zeros_like(X)
    for i in range(X.shape[1]):
        Y[:, i, :] = asr.transform(X[:, i, :])

    #raw = X.reshape(nchan, -1)  # reshape to (n_chans, n_times)
    clean = Y.reshape(nchan, -1)

    print(clean.shape)
    print("ASR finished. Now comparing signals...")

    sample_mask = np.sum(np.abs(data - clean), 0) < 1e-10

    # find latency of regions
    retain_data_intervals = np.reshape( np.argwhere(np.diff([False] + sample_mask + [False])), 2,[])
    retain_data_intervals[:,1] = retain_data_intervals[:,1]-1

    # reject regions
    new_data = data[:, retain_data_intervals]

    return new_data / 1000000

def cut_raw_blocks(raw, events, eventinfo, begin_event, end_event):
    sfreq = raw.info["sfreq"]

    begin_event_id = eventinfo[begin_event]
    end_event_id = eventinfo[end_event]

    begin_idx =  np.squeeze(events [np.where(events[:,2] == end_event_id), 0] )
    end_idx = np.squeeze(events[np.where(events[:,2] == begin_event_id), 0])

    if begin_idx[0] > end_idx[0]:
        end_idx = end_idx[1:]

    if len(begin_idx) is not len(end_idx):
        min_len = min([len(begin_idx), len(end_idx)])
        begin_idx = begin_idx[0:min_len]
        end_idx = end_idx[0:min_len]

    print(*["Annotating from {b} s to {e} s.".format(b=b/ sfreq,e=e/ sfreq) for b,e in zip(begin_idx, end_idx)])

    annot = mne.Annotations(begin_idx / sfreq, (end_idx - begin_idx) / sfreq, "bad_nonblock")
    raw.set_annotations(annot)

    return raw

def get_derivative_file_name(bids_root_path, subject, pipeline_name, extension="", make_dir = True, **kwargs):

    derivivite_root_path = join(bids_root_path, "derivatives", pipeline_name)
    save_dir = join(derivivite_root_path, "sub-"+subject)
    bids_basename = make_bids_basename(subject=subject, **kwargs)

    if make_dir and not exists(save_dir):
        makedirs(save_dir)

    return join(save_dir, bids_basename) + extension

def get_derivative_report_file_name(bids_root_path, subject, pipeline_name, extension=".html", make_dir = True, **kwargs):

    derivivite_root_path = join(bids_root_path, "derivatives", pipeline_name)
    save_dir = join(derivivite_root_path)
    bids_basename = make_bids_basename(subject=subject, **kwargs)

    if make_dir and not exists(save_dir):
        makedirs(save_dir)

    return join(save_dir, bids_basename) + extension

def get_mean_amplitudes(evokeds, window, picks = "all"):

    means = []
    if isinstance(evokeds, list):
        for i, evoked in enumerate(evokeds):
            evoked = evoked.copy().pick(picks)

            _window = np.arange(evoked.time_as_index(window[0]),
                                evoked.time_as_index(window[1]))

            data = evoked.data[:, _window]
            mean = data.mean()
            means.append(mean)
    else:
        return get_mean_amplitudes([evokeds], window, picks)

    return means

def write_set(fname, raw):
    """Export raw to EEGLAB .set file."""
    import numpy as np
    from numpy.core.records import fromarrays
    from scipy.io import savemat
    data = raw.get_data() * 1e6  # convert to microvolts
    fs = raw.info["sfreq"]
    times = raw.times
    ch_names = raw.info["ch_names"]
    chanlocs = fromarrays([ch_names], names=["labels"])
    events = fromarrays([raw.annotations.description,
                         raw.annotations.onset * fs + 1,
                         raw.annotations.duration * fs],
                        names=["type", "latency", "duration"])
    savemat(fname, dict(EEG=dict(data=data,
                                 setname=fname,
                                 nbchan=data.shape[0],
                                 pnts=data.shape[1],
                                 trials=1,
                                 srate=fs,
                                 xmin=times[0],
                                 xmax=times[-1],
                                 chanlocs=chanlocs,
                                 event=events,
                                 icawinv=[],
                                 icasphere=[],
                                 icaweights=[])),
            appendmat=False)


#!/usr/bin/python2.7
# -*- coding: utf-8 -*-
"""
name.py: description

Created by Scott Feister on Fri Oct 14 21:42:19 2016
"""
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.mlab as mlab
import math
import numpy as np

if __name__ == "__main__":
    zmin=-10
    zmax=10
    xmin=-20
    xmax=10
    npts=900
    nx = nz = npts
    xgv = np.linspace(xmin, xmax, nx)
    zgv = np.linspace(zmin, zmax, nz)
    X, Z = np.meshgrid(xgv, zgv)
    D = np.zeros_like(X)

    mu = -5
    sigma = 3
    ct = (np.abs(Z) < 1 + 10*mlab.normpdf(X, mu, sigma))
    D[ct] = 1.0
    clabel="Density"
    fig = plt.figure(1)
    plt.clf()
    ax = plt.subplot(111)
    im = ax.pcolorfast((xmin, xmax), (zmin, zmax), D, cmap='viridis')
    cbar = fig.colorbar(im, label=clabel)


# --- Do not remove these libs ---
import freqtrade.vendor.qtpylib.indicators as qtpylib
import numpy as np
import talib.abstract as ta
import pandas_ta as pta

from freqtrade.persistence import Trade
from freqtrade.strategy.interface import IStrategy
from pandas import DataFrame, Series, DatetimeIndex, merge
from datetime import datetime, timedelta
from freqtrade.strategy import merge_informative_pair, CategoricalParameter, DecimalParameter, IntParameter, stoploss_from_open
from functools import reduce
from technical.indicators import RMI, zema

# --------------------------------
def EWO(dataframe, ema_length=5, ema2_length=35):
    df = dataframe.copy()
    ema1 = ta.EMA(df, timeperiod=ema_length)
    ema2 = ta.EMA(df, timeperiod=ema2_length)
    emadif = (ema1 - ema2) / df['low'] * 100
    return emadif

# Williams %R
def williams_r(dataframe: DataFrame, period: int = 14) -> Series:
    """Williams %R, or just %R, is a technical analysis oscillator showing the current closing price in relation to the high and low
        of the past N days (for a given N). It was developed by a publisher and promoter of trading materials, Larry Williams.
        Its purpose is to tell whether a stock or commodity market is trading near the high or the low, or somewhere in between,
        of its recent trading range.
        The oscillator is on a negative scale, from −100 (lowest) up to 0 (highest).
    """

    highest_high = dataframe["high"].rolling(center=False, window=period).max()
    lowest_low = dataframe["low"].rolling(center=False, window=period).min()

    WR = Series(
        (highest_high - dataframe["close"]) / (highest_high - lowest_low),
        name=f"{period} Williams %R",
        )

    return WR * -100

class BB_RPB_TSL_RNG(IStrategy):
    '''
        BB_RPB_TSL
        @author jilv220
        Simple bollinger brand strategy inspired by this blog  ( https://hacks-for-life.blogspot.com/2020/12/freqtrade-notes.html )
        RPB, which stands for Real Pull Back, taken from ( https://github.com/GeorgeMurAlkh/freqtrade-stuff/blob/main/user_data/strategies/TheRealPullbackV2.py )
        The trailing custom stoploss taken from BigZ04_TSL from Perkmeister ( modded by ilya )
        I modified it to better suit my taste and added Hyperopt for this strategy.
    '''

    ##########################################################################

    # Hyperopt result area

    # buy space
    buy_params = {
        ##
        "buy_btc_safe": -289,
        "buy_btc_safe_1d": -0.05,
        ##
        "buy_threshold": 0.003,
        "buy_bb_factor": 0.999,
        "buy_bb_delta": 0.025,
        "buy_bb_width": 0.095,
        ##
        "buy_cci": -116,
        "buy_cci_length": 25,
        "buy_rmi": 49,
        "buy_rmi_length": 17,
        "buy_srsi_fk": 32,
        ##
        "buy_closedelta": 12.148,
        "buy_ema_diff": 0.022,
        ##
        "buy_adx": 20,
        "buy_fastd": 20,
        "buy_fastk": 22,
        "buy_ema_cofi": 0.98,
        "buy_ewo_high": 4.179,
        ##
        "buy_ema_high_2": 1.087,
        "buy_ema_low_2": 0.970,
        ##
    }

    # sell space
    sell_params = {
        "pHSL": -0.178,
        "pPF_1": 0.019,
        "pPF_2": 0.065,
        "pSL_1": 0.019,
        "pSL_2": 0.062,
        "sell_btc_safe": -389,
        "base_nb_candles_sell": 24,
        "high_offset": 0.991,
        "high_offset_2": 0.997
    }

    # really hard to use this
    minimal_roi = {
        "0": 0.10,
    }

    # Optimal timeframe for the strategy
    timeframe = '5m'
    inf_1h = '1h'

    # Disabled
    stoploss = -0.99

    # Custom stoploss
    use_custom_stoploss = True
    use_sell_signal = True

    process_only_new_candles = True

    ############################################################################

    ## Buy params

    is_optimize_dip = False
    buy_rmi = IntParameter(30, 50, default=35, optimize= is_optimize_dip)
    buy_cci = IntParameter(-135, -90, default=-133, optimize= is_optimize_dip)
    buy_srsi_fk = IntParameter(30, 50, default=25, optimize= is_optimize_dip)
    buy_cci_length = IntParameter(25, 45, default=25, optimize = is_optimize_dip)
    buy_rmi_length = IntParameter(8, 20, default=8, optimize = is_optimize_dip)

    is_optimize_break = False
    buy_bb_width = DecimalParameter(0.05, 0.2, default=0.15, optimize = is_optimize_break)
    buy_bb_delta = DecimalParameter(0.025, 0.08, default=0.04, optimize = is_optimize_break)

    is_optimize_local_dip = False
    buy_ema_diff = DecimalParameter(0.022, 0.027, default=0.025, optimize = is_optimize_local_dip)
    buy_bb_factor = DecimalParameter(0.990, 0.999, default=0.995, optimize = False)
    buy_closedelta = DecimalParameter(12.0, 18.0, default=15.0, optimize = is_optimize_local_dip)

    is_optimize_ewo = False
    buy_rsi_fast = IntParameter(35, 50, default=45, optimize = False)
    buy_rsi = IntParameter(15, 30, default=35, optimize = False)
    buy_ewo = DecimalParameter(-6.0, 5, default=-5.585, optimize = is_optimize_ewo)
    buy_ema_low = DecimalParameter(0.9, 0.99, default=0.942 , optimize = is_optimize_ewo)
    buy_ema_high = DecimalParameter(0.95, 1.2, default=1.084 , optimize = is_optimize_ewo)

    is_optimize_ewo_2 = False
    buy_ema_low_2 = DecimalParameter(0.96, 0.978, default=0.96 , optimize = is_optimize_ewo_2)
    buy_ema_high_2 = DecimalParameter(1.05, 1.2, default=1.09 , optimize = is_optimize_ewo_2)

    is_optimize_cofi = False
    buy_ema_cofi = DecimalParameter(0.96, 0.98, default=0.97 , optimize = is_optimize_cofi)
    buy_fastk = IntParameter(20, 30, default=20, optimize = is_optimize_cofi)
    buy_fastd = IntParameter(20, 30, default=20, optimize = is_optimize_cofi)
    buy_adx = IntParameter(20, 30, default=30, optimize = is_optimize_cofi)
    buy_ewo_high = DecimalParameter(2, 12, default=3.553, optimize = is_optimize_cofi)

    is_optimize_btc_safe = False
    buy_btc_safe = IntParameter(-300, 50, default=-200, optimize = is_optimize_btc_safe)
    buy_btc_safe_1d = DecimalParameter(-0.075, -0.025, default=-0.05, optimize = is_optimize_btc_safe)
    buy_threshold = DecimalParameter(0.003, 0.012, default=0.008, optimize = is_optimize_btc_safe)

    # Buy params toggle
    buy_is_dip_enabled = CategoricalParameter([True, False], default=True, space='buy', optimize=False, load=True)
    buy_is_break_enabled = CategoricalParameter([True, False], default=True, space='buy', optimize=False, load=True)

    ## Sell params
    sell_btc_safe = IntParameter(-400, -300, default=-365, optimize = True)
    base_nb_candles_sell = IntParameter(5, 80, default=sell_params['base_nb_candles_sell'], space='sell', optimize=True)
    high_offset          = DecimalParameter(0.95, 1.1, default=sell_params['high_offset'], space='sell', optimize=True)
    high_offset_2        = DecimalParameter(0.99, 1.5, default=sell_params['high_offset_2'], space='sell', optimize=True)

    ## Trailing params

    # hard stoploss profit
    pHSL = DecimalParameter(-0.200, -0.040, default=-0.08, decimals=3, space='sell', load=True)
    # profit threshold 1, trigger point, SL_1 is used
    pPF_1 = DecimalParameter(0.008, 0.020, default=0.016, decimals=3, space='sell', load=True)
    pSL_1 = DecimalParameter(0.008, 0.020, default=0.011, decimals=3, space='sell', load=True)

    # profit threshold 2, SL_2 is used
    pPF_2 = DecimalParameter(0.040, 0.100, default=0.080, decimals=3, space='sell', load=True)
    pSL_2 = DecimalParameter(0.020, 0.070, default=0.040, decimals=3, space='sell', load=True)

    ############################################################################

    def informative_pairs(self):

        informative_pairs = [("BTC/USDT", "5m")]

        return informative_pairs

    ############################################################################

    ## Custom Trailing stoploss ( credit to Perkmeister for this custom stoploss to help the strategy ride a green candle )
    def custom_stoploss(self, pair: str, trade: 'Trade', current_time: datetime,
                        current_rate: float, current_profit: float, **kwargs) -> float:

        # hard stoploss profit
        HSL = self.pHSL.value
        PF_1 = self.pPF_1.value
        SL_1 = self.pSL_1.value
        PF_2 = self.pPF_2.value
        SL_2 = self.pSL_2.value

        # For profits between PF_1 and PF_2 the stoploss (sl_profit) used is linearly interpolated
        # between the values of SL_1 and SL_2. For all profits above PL_2 the sl_profit value
        # rises linearly with current profit, for profits below PF_1 the hard stoploss profit is used.

        if (current_profit > PF_2):
            sl_profit = SL_2 + (current_profit - PF_2)
        elif (current_profit > PF_1):
            sl_profit = SL_1 + ((current_profit - PF_1) * (SL_2 - SL_1) / (PF_2 - PF_1))
        else:
            sl_profit = HSL

        # Only for hyperopt invalid return
        if (sl_profit >= current_profit):
            return -0.99

        return stoploss_from_open(sl_profit, current_profit)

    ############################################################################

    def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:

        assert self.dp, "DataProvider is required for multiple timeframes."

        # Bollinger bands (hyperopt hard to implement)
        bollinger2 = qtpylib.bollinger_bands(qtpylib.typical_price(dataframe), window=20, stds=2)
        dataframe['bb_lowerband2'] = bollinger2['lower']
        dataframe['bb_middleband2'] = bollinger2['mid']
        dataframe['bb_upperband2'] = bollinger2['upper']

        bollinger3 = qtpylib.bollinger_bands(qtpylib.typical_price(dataframe), window=20, stds=3)
        dataframe['bb_lowerband3'] = bollinger3['lower']
        dataframe['bb_middleband3'] = bollinger3['mid']
        dataframe['bb_upperband3'] = bollinger3['upper']

        ### BTC protection

        # BTC info
        inf_tf = '5m'
        informative = self.dp.get_pair_dataframe('BTC/USDT', timeframe=inf_tf)
        informative_past = informative.copy().shift(1)                                                                                # Get recent BTC info

        # BTC 5m dump protection
        informative_past_source = (informative_past['open'] + informative_past['close'] + informative_past['high'] + informative_past['low']) / 4        # Get BTC price
        informative_threshold = informative_past_source * self.buy_threshold.value                                                                       # BTC dump n% in 5 min
        informative_past_delta = informative_past['close'].shift(1) - informative_past['close']                                                          # should be positive if dump
        informative_diff = informative_threshold - informative_past_delta                                                                                # Need be larger than 0
        dataframe['btc_threshold'] = informative_threshold
        dataframe['btc_diff'] = informative_diff

        # BTC 1d dump protection
        informative_past_1d = informative.copy().shift(288)
        informative_past_source_1d = (informative_past_1d['open'] + informative_past_1d['close'] + informative_past_1d['high'] + informative_past_1d['low']) / 4
        dataframe['btc_5m'] = informative_past_source
        dataframe['btc_1d'] = informative_past_source_1d

        ### Other checks

        dataframe['bb_width'] = ((dataframe['bb_upperband2'] - dataframe['bb_lowerband2']) / dataframe['bb_middleband2'])
        dataframe['bb_delta'] = ((dataframe['bb_lowerband2'] - dataframe['bb_lowerband3']) / dataframe['bb_lowerband2'])
        dataframe['bb_bottom_cross'] = qtpylib.crossed_below(dataframe['close'], dataframe['bb_lowerband3']).astype('int')

        # CCI hyperopt
        for val in self.buy_cci_length.range:
            dataframe[f'cci_length_{val}'] = ta.CCI(dataframe, val)

        dataframe['cci'] = ta.CCI(dataframe, 26)
        dataframe['cci_long'] = ta.CCI(dataframe, 170)

        # RMI hyperopt
        for val in self.buy_rmi_length.range:
            dataframe[f'rmi_length_{val}'] = RMI(dataframe, length=val, mom=4)
        #dataframe['rmi'] = RMI(dataframe, length=8, mom=4)

        # SRSI hyperopt ?
        stoch = ta.STOCHRSI(dataframe, 15, 20, 2, 2)
        dataframe['srsi_fk'] = stoch['fastk']
        dataframe['srsi_fd'] = stoch['fastd']

        # BinH
        dataframe['closedelta'] = (dataframe['close'] - dataframe['close'].shift()).abs()

        # SMA
        dataframe['sma_15'] = ta.SMA(dataframe, timeperiod=15)
        dataframe['sma_30'] = ta.SMA(dataframe, timeperiod=30)

        # CTI
        dataframe['cti'] = pta.cti(dataframe["close"], length=20)

        # EMA
        dataframe['ema_8'] = ta.EMA(dataframe, timeperiod=8)
        dataframe['ema_12'] = ta.EMA(dataframe, timeperiod=12)
        dataframe['ema_13'] = ta.EMA(dataframe, timeperiod=13)
        dataframe['ema_16'] = ta.EMA(dataframe, timeperiod=16)
        dataframe['ema_26'] = ta.EMA(dataframe, timeperiod=26)
        dataframe['hma_50'] = qtpylib.hull_moving_average(dataframe['close'], window=50)
        dataframe['ema_100'] = ta.EMA(dataframe, timeperiod=100)
        dataframe['sma_9'] = ta.SMA(dataframe, timeperiod=9)

        # RSI
        dataframe['rsi'] = ta.RSI(dataframe, timeperiod=14)
        dataframe['rsi_fast'] = ta.RSI(dataframe, timeperiod=4)
        dataframe['rsi_slow'] = ta.RSI(dataframe, timeperiod=20)

        # Elliot
        dataframe['EWO'] = EWO(dataframe, 50, 200)

        # Cofi
        stoch_fast = ta.STOCHF(dataframe, 5, 3, 0, 3, 0)
        dataframe['fastd'] = stoch_fast['fastd']
        dataframe['fastk'] = stoch_fast['fastk']
        dataframe['adx'] = ta.ADX(dataframe)

        # Williams %R
        dataframe['r_14'] = williams_r(dataframe, period=14)

        # Volume
        dataframe['volume_mean_4'] = dataframe['volume'].rolling(4).mean().shift(1)

        # Calculate all ma_sell values
        for val in self.base_nb_candles_sell.range:
            dataframe[f'ma_sell_{val}'] = ta.EMA(dataframe, timeperiod=val)

        return dataframe

    def populate_buy_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:

        conditions = []
        dataframe.loc[:, 'buy_tag'] = ''

        if self.buy_is_dip_enabled.value:

            is_dip = (
                (dataframe[f'rmi_length_{self.buy_rmi_length.value}'] < self.buy_rmi.value) &
                (dataframe[f'cci_length_{self.buy_cci_length.value}'] <= self.buy_cci.value) &
                (dataframe['srsi_fk'] < self.buy_srsi_fk.value)
            )

            #conditions.append(is_dip)

        if self.buy_is_break_enabled.value:

            is_break = (

                (   (dataframe['bb_delta'] > self.buy_bb_delta.value)                                   #"buy_bb_delta": 0.025 0.036
                    &                                                                                #"buy_bb_width": 0.095 0.133
                    (dataframe['bb_width'] > self.buy_bb_width.value)
                )
                &
                (dataframe['closedelta'] > dataframe['close'] * self.buy_closedelta.value / 1000 ) &    # from BinH
                (dataframe['close'] < dataframe['bb_lowerband3'] * self.buy_bb_factor.value)
            )
            #conditions.append(is_break)

        is_local_uptrend = (                                                                            # from NFI next gen

                (dataframe['ema_26'] > dataframe['ema_12']) &
                (dataframe['ema_26'] - dataframe['ema_12'] > dataframe['open'] * self.buy_ema_diff.value) &
                (dataframe['ema_26'].shift() - dataframe['ema_12'].shift() > dataframe['open'] / 100) &
                (dataframe['close'] < dataframe['bb_lowerband2'] * self.buy_bb_factor.value) &
                (dataframe['closedelta'] > dataframe['close'] * self.buy_closedelta.value / 1000 )
            )

        is_ewo = (                                                                                # from SMA offset
                (dataframe['rsi_fast'] < self.buy_rsi_fast.value) &
                (dataframe['close'] < dataframe['ema_8'] * self.buy_ema_low.value) &
                (dataframe['EWO'] > self.buy_ewo.value) &
                (dataframe['close'] < dataframe['ema_16'] * self.buy_ema_high.value) &
                (dataframe['rsi'] < self.buy_rsi.value)
            )

        is_ewo_2 = (
                (dataframe['rsi_fast'] < self.buy_rsi_fast.value) &
                (dataframe['close'] < dataframe['ema_8'] * self.buy_ema_low_2.value) &
                (dataframe['EWO'] > self.buy_ewo_high.value) &
                (dataframe['close'] < dataframe['ema_16'] * self.buy_ema_high_2.value) &
                (dataframe['rsi'] < self.buy_rsi.value)
            )

        is_cofi = (
                (dataframe['open'] < dataframe['ema_8'] * self.buy_ema_cofi.value) &
                (qtpylib.crossed_above(dataframe['fastk'], dataframe['fastd'])) &
                (dataframe['fastk'] < self.buy_fastk.value) &
                (dataframe['fastd'] < self.buy_fastd.value) &
                (dataframe['adx'] > self.buy_adx.value) &
                (dataframe['EWO'] > self.buy_ewo_high.value)
            )

        # NFI quick mode

        is_nfi_32 = (
                (dataframe['rsi_slow'] < dataframe['rsi_slow'].shift(1)) &
                (dataframe['rsi_fast'] < 46) &
                (dataframe['rsi'] > 19) &
                (dataframe['close'] < dataframe['sma_15'] * 0.942) &
                (dataframe['cti'] < -0.86)
            )

        is_nfi_33 = (
                (dataframe['close'] < (dataframe['ema_13'] * 0.978)) &
                (dataframe['EWO'] > 8) &
                (dataframe['cti'] < -0.88) &
                (dataframe['rsi'] < 32) &
                (dataframe['r_14'] < -98.0) &
                (dataframe['volume'] < (dataframe['volume_mean_4'] * 2.5))
            )

        # is_btc_safe = (

        #         (dataframe['btc_diff'] > self.buy_btc_safe.value)
        #        &(dataframe['btc_5m'] - dataframe['btc_1d'] > dataframe['btc_1d'] * self.buy_btc_safe_1d.value)
        #        &(dataframe['volume'] > 0)           # Make sure Volume is not 0
        #     )

        is_BB_checked = is_dip & is_break

        #print(dataframe['btc_5m'])
        #print(dataframe['btc_1d'])
        #print(dataframe['btc_5m'] - dataframe['btc_1d'])
        #print(dataframe['btc_1d'] * -0.025)
        #print(dataframe['btc_5m'] - dataframe['btc_1d'] > dataframe['btc_1d'] * -0.025)

        ## condition append
        conditions.append(is_BB_checked)          # ~1.7 89%
        dataframe.loc[is_BB_checked, 'buy_tag'] += 'bb '

        conditions.append(is_local_uptrend)       # ~3.84 90.2%
        dataframe.loc[is_local_uptrend, 'buy_tag'] += 'local uptrend '

        conditions.append(is_ewo)                 # ~2.26 93.5%
        dataframe.loc[is_ewo, 'buy_tag'] += 'ewo '

        conditions.append(is_ewo_2)               # ~3.68 90.3%
        dataframe.loc[is_ewo_2, 'buy_tag'] += 'ewo2 '

        conditions.append(is_cofi)                # ~3.21 90.8%
        dataframe.loc[is_cofi, 'buy_tag'] += 'cofi '

        conditions.append(is_nfi_32)              # ~2.43 91.3%
        dataframe.loc[is_nfi_32, 'buy_tag'] += 'nfi 32 '

        conditions.append(is_nfi_33)              # ~0.11 100%
        dataframe.loc[is_nfi_33, 'buy_tag'] += 'nfi 33 '

        if conditions:
            dataframe.loc[reduce(lambda x, y: x | y, conditions), 'buy' ] = 1

        return dataframe

    def populate_sell_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        conditions = []

        conditions.append(
            (
                (dataframe['close'] > dataframe['sma_9'])&
                (dataframe['close'] > (dataframe[f'ma_sell_{self.base_nb_candles_sell.value}'] * self.high_offset_2.value)) &
                (dataframe['rsi']>50)&
                (dataframe['volume'] > 0)&
                (dataframe['rsi_fast'] > dataframe['rsi_slow'])

            )
            |
            (
                (dataframe['sma_9'] > (dataframe['sma_9'].shift(1) + dataframe['sma_9'].shift(1)*0.005 )) &
                (dataframe['close'] < dataframe['hma_50'])&
                (dataframe['close'] > (dataframe[f'ma_sell_{self.base_nb_candles_sell.value}'] * self.high_offset.value)) &
                (dataframe['volume'] > 0)&
                (dataframe['rsi_fast']>dataframe['rsi_slow'])
            )

        )

        if conditions:
            dataframe.loc[
                reduce(lambda x, y: x | y, conditions),
                'sell'
            ]=1

        return dataframe


from BurstCube.NoahSim import burstutils
from BurstCube.NoahSim import NoahCube
#this obeys the correct format, but my path is off I think for it to work on my local system.
import numpy as np
from astropy.tests.helper import pytest

@pytest.fixture(scope='module')
def create_sky(request):
        testsim = NoahCube.Sky(1, 500)

        return testsim

@pytest.fixture(scope='module')
def create_BurstCube(request):

        testcube = NoahCube.BurstCube(1000, 45)

        return testcube

def test_det():

        tc = NoahCube.BurstCube(1000,45)

        result = [[0.78539816339744828, 0],
                  [0.78539816339744828, 1.5707963267948966],
                  [0.78539816339744828, 3.141592653589793],
                  [0.78539816339744828, 4.71238898038469]]

        dets = [getattr(tc, 'det{}'.format(letter))
                for letter in ['A', 'B', 'C', 'D']]

        np.testing.assert_allclose(dets, result, 1e-16)

# def test_fastcube():
#        testsim = GRBgenerator.Sky(1, 500)
#        testcube = fastcube.FastCube(1000, 45)
#        testresponse = testcube.response2GRB(testsim, samples=10,
#                                             test=True, talk=False)

        # leaving this a little more room for error
        # since I'm messing up one detector.
#        np.testing.assert_allclose(testresponse[0], 6.0, 1.0)


#!/usr/bin/python3

from gpiozero import Button

button = Button(2)

try:
    while True:
        if button.is_pressed:
            print("Button is pressed")
        else:
            print("Button is not pressed")
except KeyboardInterrupt:
    print("interrupted!")
    button.close()


import os
import shutil
import cv2 as cv
from skimage.measure import compare_ssim
from tqdm import tqdm

def compareKeyFrame(frame1, frame2):
    return compare_ssim(frame1, frame2, multichannel=True)

    pass

def compareVideoFrame(video_path1, video_path2):
    cap1 = cv.VideoCapture(video_path1)

    VIDEO1_FPS = cap1.get(cv.CAP_PROP_FPS)
    VIDEO1_WIDTH = cap1.get(cv.CAP_PROP_FRAME_WIDTH)
    VIDEO1_HEIGHT = cap1.get(cv.CAP_PROP_FRAME_HEIGHT)
    VIDEO1_FRAME_COUNT = cap1.get(cv.CAP_PROP_FRAME_COUNT)

    VIDEO1_TOTAL_TIME = VIDEO1_FRAME_COUNT / VIDEO1_FPS

    cap2 = cv.VideoCapture(video_path2)
    VIDEO2_FPS = cap2.get(cv.CAP_PROP_FPS)
    VIDEO2_WIDTH = cap2.get(cv.CAP_PROP_FRAME_WIDTH)
    VIDEO2_HEIGHT = cap2.get(cv.CAP_PROP_FRAME_HEIGHT)
    VIDEO2_FRAME_COUNT = cap2.get(cv.CAP_PROP_FRAME_COUNT)

    VIDEO2_TOTAL_TIME = VIDEO2_FRAME_COUNT / VIDEO2_FPS

    if VIDEO1_WIDTH != VIDEO2_WIDTH or VIDEO1_HEIGHT != VIDEO2_HEIGHT:
        return False

    if abs(VIDEO1_TOTAL_TIME - VIDEO2_TOTAL_TIME) > 10:
        return False

    frame_count = 0
    key_frames = []
    for index in range(10):
        if index * (int(VIDEO1_FPS / 2)) < VIDEO1_FRAME_COUNT or index * (int(VIDEO2_FPS / 2)) < VIDEO2_FRAME_COUNT:
            key_frames.append(index * (int(VIDEO1_FPS / 2)))
        else:
            break
    score = 0
    while cap1.isOpened() or cap2.isOpened():
        suc1, frame1 = cap1.read()
        suc2, frame2 = cap2.read()
        if frame_count in key_frames:
            single_score = compareKeyFrame(frame1, frame2)
            score += single_score
        if frame_count > key_frames[len(key_frames) - 1]:
            break

        frame_count += 1

    score_avg = score / len(key_frames)
    cap1.release()
    cap2.release()
    print('\nscore_avg: ', score_avg)
    if score_avg > 0.5:
        return True
    else:
        return False

VIDEO_EXTENSIONS = [
    'mpg', 'mp4'
]

def is_video_file(filename):
    return any(filename.endswith(extension) for extension in VIDEO_EXTENSIONS)

def make_dataset(dir):
    videos = []
    assert os.path.isdir(dir), '%s is not a valid directory' % dir
    for root, _, fnames in sorted(os.walk(dir)):
        for fname in fnames:
            if is_video_file(fname):
                path = os.path.join(root, fname)
                videos.append(path)
    return videos

def findSourceVideo(Q_path, K_paths):
    Q_video_list = []
    K_video_list = []

    if isinstance(Q_path, list):
        for item in Q_path:
            Q_video_list.extend(make_dataset(item))
    else:
        Q_video_list.extend(make_dataset(Q_path))

    if isinstance(K_paths, list):
        for item in K_paths:
            K_video_list.extend(make_dataset(item))

    with open("q_to_k.txt", "w+") as f:
        for q_item in tqdm(Q_video_list):
            for k_item in K_video_list:
                if compareVideoFrame(q_item, k_item):
                    f.write(q_item)
                    f.write(":")
                    f.write(k_item)
                    f.write("\n")
                    print(" find source video:\n" + "q:" + q_item + "\n" + "k:" + k_item)
                    break
    pass

def copyTargetVideoFile():
    q_and_k_dict = {}
    with open("q_to_k.txt", "r") as f:
        while True:
            line = f.readline()
            if line:
                line = line.replace("\n", "")
                q_and_k = line.split(":")
                if len(q_and_k) == 2:
                    q_video = q_and_k[0]
                    k_video = q_and_k[1]
                    q_and_k_dict[q_video] = k_video
            else:
                break

    print(q_and_k_dict)
    pass

if __name__ == '__main__':
    Q_path = ['/home/letmesleep/data/test/q_folder']

    K_paths = ['/home/letmesleep/data/test/k_folder/k1',
               '/home/letmesleep/data/test/k_folder/k2',
               '/home/letmesleep/data/test/k_folder/k3'
               ]

    findSourceVideo(Q_path, K_paths)

    copyTargetVideoFile()


import asyncio
import logging

import pytest

import lightbus
from lightbus import BusNode

logger = logging.getLogger(__name__)

@pytest.fixture
def redis_rpc_transport(new_redis_pool, server, loop):
    """Get a redis transport backed by a running redis server."""
    return lightbus.RedisRpcTransport(redis_pool=new_redis_pool(maxsize=10000))

@pytest.fixture
def redis_result_transport(new_redis_pool, server, loop):
    """Get a redis transport backed by a running redis server."""
    return lightbus.RedisResultTransport(redis_pool=new_redis_pool(maxsize=10000))

@pytest.fixture
def redis_event_transport(new_redis_pool, server, loop):
    """Get a redis transport backed by a running redis server."""
    return lightbus.RedisEventTransport(redis_pool=new_redis_pool(maxsize=10000),
                                        consumer_group_prefix='test_cg', consumer_name='test_consumer')

@pytest.fixture
def redis_schema_transport(new_redis_pool, server, loop):
    """Get a redis transport backed by a running redis server."""
    logger.debug('Loop: {}'.format(id(loop)))
    return lightbus.RedisSchemaTransport(redis_pool=new_redis_pool(maxsize=10000))

@pytest.fixture
def bus(loop, redis_rpc_transport, redis_result_transport, redis_event_transport, redis_schema_transport):
    """Get a redis transport backed by a running redis server."""
    return lightbus.create(
        rpc_transport=redis_rpc_transport,
        result_transport=redis_result_transport,
        event_transport=redis_event_transport,
        schema_transport=redis_schema_transport,
        loop=loop,
    )

@pytest.fixture(name='fire_dummy_events')
def fire_dummy_events_fixture(bus):
    async def fire_dummy_events(total, initial_delay=0.1):
        await asyncio.sleep(initial_delay)
        for x in range(0, total):
            await bus.my.dummy.my_event.fire_async(field=x)
        logger.warning('TEST: fire_dummy_events() completed')
    return fire_dummy_events


#Based on  awesome script (https://www.kaggle.com/ogrellier/good-fun-with-ligthgbm) and parameters (https://www.kaggle.com/ashishpatel26/home-credit-default-analysis/notebook)

import pandas as pd
import numpy as np

from sklearn.metrics import roc_auc_score
from sklearn.model_selection import KFold

import xgboost as xgb
from xgboost import XGBClassifier

import gc

from sklearn.preprocessing import MinMaxScaler, LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, roc_auc_score, confusion_matrix
from sklearn.feature_selection import VarianceThreshold

print('Importing data...')
data = pd.read_csv('../input/application_train.csv')
test = pd.read_csv('../input/application_test.csv')
prev = pd.read_csv('../input/previous_application.csv')
buro = pd.read_csv('../input/bureau.csv')
buro_balance = pd.read_csv('../input/bureau_balance.csv')
credit_card  = pd.read_csv('../input/credit_card_balance.csv')
POS_CASH  = pd.read_csv('../input/POS_CASH_balance.csv')
payments = pd.read_csv('../input/installments_payments.csv')
lgbm_submission = pd.read_csv('../input/sample_submission.csv')

#Separate target variable
y = data['TARGET']
del data['TARGET']

#One-hot encoding of categorical features in data and test sets
categorical_features = [col for col in data.columns if data[col].dtype == 'object']

one_hot_df = pd.concat([data,test])
one_hot_df = pd.get_dummies(one_hot_df, columns=categorical_features)

data = one_hot_df.iloc[:data.shape[0],:]
test = one_hot_df.iloc[data.shape[0]:,]

#Pre-processing buro_balance
print('Pre-processing buro_balance...')
buro_grouped_size = buro_balance.groupby('SK_ID_BUREAU')['MONTHS_BALANCE'].size()
buro_grouped_max = buro_balance.groupby('SK_ID_BUREAU')['MONTHS_BALANCE'].max()
buro_grouped_min = buro_balance.groupby('SK_ID_BUREAU')['MONTHS_BALANCE'].min()

buro_counts = buro_balance.groupby('SK_ID_BUREAU')['STATUS'].value_counts(normalize = False)
buro_counts_unstacked = buro_counts.unstack('STATUS')
buro_counts_unstacked.columns = ['STATUS_0', 'STATUS_1','STATUS_2','STATUS_3','STATUS_4','STATUS_5','STATUS_C','STATUS_X',]
buro_counts_unstacked['MONTHS_COUNT'] = buro_grouped_size
buro_counts_unstacked['MONTHS_MIN'] = buro_grouped_min
buro_counts_unstacked['MONTHS_MAX'] = buro_grouped_max

buro = buro.join(buro_counts_unstacked, how='left', on='SK_ID_BUREAU')

#Pre-processing previous_application
print('Pre-processing previous_application...')
#One-hot encoding of categorical features in previous application data set
prev_cat_features = [pcol for pcol in prev.columns if prev[pcol].dtype == 'object']
prev = pd.get_dummies(prev, columns=prev_cat_features)
avg_prev = prev.groupby('SK_ID_CURR').mean()
cnt_prev = prev[['SK_ID_CURR', 'SK_ID_PREV']].groupby('SK_ID_CURR').count()
avg_prev['nb_app'] = cnt_prev['SK_ID_PREV']
del avg_prev['SK_ID_PREV']

#Pre-processing buro
print('Pre-processing buro...')
#One-hot encoding of categorical features in buro data set
buro_cat_features = [bcol for bcol in buro.columns if buro[bcol].dtype == 'object']
buro = pd.get_dummies(buro, columns=buro_cat_features)
avg_buro = buro.groupby('SK_ID_CURR').mean()
avg_buro['buro_count'] = buro[['SK_ID_BUREAU', 'SK_ID_CURR']].groupby('SK_ID_CURR').count()['SK_ID_BUREAU']
del avg_buro['SK_ID_BUREAU']

#Pre-processing POS_CASH
print('Pre-processing POS_CASH...')
le = LabelEncoder()
POS_CASH['NAME_CONTRACT_STATUS'] = le.fit_transform(POS_CASH['NAME_CONTRACT_STATUS'].astype(str))
nunique_status = POS_CASH[['SK_ID_CURR', 'NAME_CONTRACT_STATUS']].groupby('SK_ID_CURR').nunique()
nunique_status2 = POS_CASH[['SK_ID_CURR', 'NAME_CONTRACT_STATUS']].groupby('SK_ID_CURR').max()
POS_CASH['NUNIQUE_STATUS'] = nunique_status['NAME_CONTRACT_STATUS']
POS_CASH['NUNIQUE_STATUS2'] = nunique_status2['NAME_CONTRACT_STATUS']
POS_CASH.drop(['SK_ID_PREV', 'NAME_CONTRACT_STATUS'], axis=1, inplace=True)

#Pre-processing credit_card
print('Pre-processing credit_card...')
credit_card['NAME_CONTRACT_STATUS'] = le.fit_transform(credit_card['NAME_CONTRACT_STATUS'].astype(str))
nunique_status = credit_card[['SK_ID_CURR', 'NAME_CONTRACT_STATUS']].groupby('SK_ID_CURR').nunique()
nunique_status2 = credit_card[['SK_ID_CURR', 'NAME_CONTRACT_STATUS']].groupby('SK_ID_CURR').max()
credit_card['NUNIQUE_STATUS'] = nunique_status['NAME_CONTRACT_STATUS']
credit_card['NUNIQUE_STATUS2'] = nunique_status2['NAME_CONTRACT_STATUS']
credit_card.drop(['SK_ID_PREV', 'NAME_CONTRACT_STATUS'], axis=1, inplace=True)

#Pre-processing payments
print('Pre-processing payments...')
avg_payments = payments.groupby('SK_ID_CURR').mean()
avg_payments2 = payments.groupby('SK_ID_CURR').max()
avg_payments3 = payments.groupby('SK_ID_CURR').min()
del avg_payments['SK_ID_PREV']

#Join data bases
print('Joining databases...')
data = data.merge(right=avg_prev.reset_index(), how='left', on='SK_ID_CURR')
test = test.merge(right=avg_prev.reset_index(), how='left', on='SK_ID_CURR')

data = data.merge(right=avg_buro.reset_index(), how='left', on='SK_ID_CURR')
test = test.merge(right=avg_buro.reset_index(), how='left', on='SK_ID_CURR')

data = data.merge(POS_CASH.groupby('SK_ID_CURR').mean().reset_index(), how='left', on='SK_ID_CURR')
test = test.merge(POS_CASH.groupby('SK_ID_CURR').mean().reset_index(), how='left', on='SK_ID_CURR')

data = data.merge(credit_card.groupby('SK_ID_CURR').mean().reset_index(), how='left', on='SK_ID_CURR')
test = test.merge(credit_card.groupby('SK_ID_CURR').mean().reset_index(), how='left', on='SK_ID_CURR')

data = data.merge(right=avg_payments.reset_index(), how='left', on='SK_ID_CURR')
test = test.merge(right=avg_payments.reset_index(), how='left', on='SK_ID_CURR')

data = data.merge(right=avg_payments2.reset_index(), how='left', on='SK_ID_CURR')
test = test.merge(right=avg_payments2.reset_index(), how='left', on='SK_ID_CURR')

data = data.merge(right=avg_payments3.reset_index(), how='left', on='SK_ID_CURR')
test = test.merge(right=avg_payments3.reset_index(), how='left', on='SK_ID_CURR')

#Remove features with many missing values
print('Removing features with more than 80% missing...')
test = test[test.columns[data.isnull().mean() < 0.80]]
data = data[data.columns[data.isnull().mean() < 0.80]]

from lightgbm import LGBMClassifier
import gc

gc.enable()

folds = KFold(n_splits=5, shuffle=True, random_state=546789)
oof_preds = np.zeros(data.shape[0])
sub_preds = np.zeros(test.shape[0])

feature_importance_df = pd.DataFrame()

feats = [f for f in data.columns if f not in ['SK_ID_CURR']]

for n_fold, (trn_idx, val_idx) in enumerate(folds.split(data)):
    trn_x, trn_y = data[feats].iloc[trn_idx], y.iloc[trn_idx]
    val_x, val_y = data[feats].iloc[val_idx], y.iloc[val_idx]

    clf = LGBMClassifier(
        # n_estimators=10000,
        # num_leaves=30,
        # colsample_bytree=.8,
        # subsample=.9,
        # max_depth=7,
        # reg_alpha=.1,
        # reg_lambda=.1,
        # min_split_gain=.01
        n_estimators=10000,
        learning_rate=0.03,
        num_leaves=34,
        colsample_bytree=0.9,
        subsample=0.8,
        max_depth=8,
        reg_alpha=.1,
        reg_lambda=.1,
        min_split_gain=.01,
        min_child_weight=375,
        silent=-1,
        verbose=-1,
        )

    clf.fit(trn_x, trn_y,
            eval_set= [(trn_x, trn_y), (val_x, val_y)],
            eval_metric='auc', verbose=100, early_stopping_rounds=100  #30
           )

    oof_preds[val_idx] = clf.predict_proba(val_x, num_iteration=clf.best_iteration_)[:, 1]
    sub_preds += clf.predict_proba(test[feats], num_iteration=clf.best_iteration_)[:, 1] / folds.n_splits

    fold_importance_df = pd.DataFrame()
    fold_importance_df["feature"] = feats
    fold_importance_df["importance"] = clf.feature_importances_
    fold_importance_df["fold"] = n_fold + 1
    feature_importance_df = pd.concat([feature_importance_df, fold_importance_df], axis=0)

    print('Fold %2d AUC : %.6f' % (n_fold + 1, roc_auc_score(val_y, oof_preds[val_idx])))
    del clf, trn_x, trn_y, val_x, val_y
    gc.collect()

print('Full AUC score %.6f' % roc_auc_score(y, oof_preds))

test['TARGET'] = sub_preds

test[['SK_ID_CURR', 'TARGET']].to_csv('1submission.csv', index=False)

# Plot feature importances
cols = feature_importance_df[["feature", "importance"]].groupby("feature").mean().sort_values(
    by="importance", ascending=False)[:50].index

best_features = feature_importance_df.loc[feature_importance_df.feature.isin(cols)];


import sympy as sym
import numpy as np

# returns True if number in an integer
def is_integer(num):
    num = 2 * num
    if num % 2 == 0:
        return True
    else:
        return False

# finds all unique prime factors of a number
def prime_factors(x):
    p_factors = []
    for i in range(1, (x//2) + 1):
        if sym.isprime(i) and x % i == 0:
            p_factors.append(i)
    return p_factors

# expresses a number as its prime factors
def p_factn(x):
    factorisation = []
    p_factors = prime_factors(x)
    for i in p_factors:
        stop = False
        while not stop:
            if is_integer(x/i):
                factorisation.append(int(i))
                x = x/i
            else:
                stop = True
    return factorisation

# calculates the lowest common multiple of any number of given integers
def LCM(*args):
    primes = []
    non_primes = []

    # splits input sequence into primes and non-primes
    for i in args:
        if sym.isprime(i):
            primes.append(i)
        elif not sym.isprime(i) and i != 1:
            non_primes.append(i)

    # expresses non-primes as a product of prime factors
    factorised_non_primes = []
    for i in non_primes:
        x = p_factn(i)
        factorised_non_primes.append(x)

    # Removes unique primes from factorised_non_primes output from original primes list
    unique_primes = set()
    for i in factorised_non_primes:
        for j in i:
            unique_primes.add(j)
    unique_primes = list(unique_primes)
    for i in unique_primes:
        primes.remove(i)

    # creates dictionary with unique prime keys and values equal to its largest exponent from the factorised_non_primes output
    exponents = []
    for i in unique_primes:
        x = 0
        for j in factorised_non_primes:
            y = j.count(i)
            if y > x:
                x = y
        exponents.append(x)
    exponents = dict(zip(unique_primes,exponents))

    # calculates lowest common multiple
    lcm = 1
    for i in unique_primes:
        lcm = lcm * i ** (int(exponents[i]))
    lcm = lcm * np.prod(primes)
    return lcm

seq = list(range(1,21))
print(LCM(*seq))


__author__ = 'Murphy'
import unittest
import datetime, time
from Methods.LoginTools import LoginTools
from Methods.MemcacheTools import MemcacheTools
from Methods.WebDriverTools import WebDriverTools
from Methods.OtherTools import OtherTools
from config import app
from time import sleep

class Case087(unittest.TestCase):
    testCaseID = "Case087"
    projectName = "上海世贸皇家艾美"
    buzName = "能耗值查询"
    now = 'None'
    url = "http://%s" % app.config['SERVERIP']
    project = (395, 'shsmhjam', ['能源管理'], '#indexMain')

    def setUp(self):
        self.start = datetime.datetime.now()
        self.startTime = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()).split(" ")[-1]
        MemcacheTools.setMemTime(self.testCaseID,{"startTime":self.startTime})
        lg = LoginTools()
        self.driver = lg.InitialChrome(self.url,self.testCaseID)
        self.driver = lg.login(self.driver)
        self.tools = WebDriverTools()

    def tearDown(self):
        text=str([x[1] for x in self._outcome.errors if x[1]!=None])
        if "Exception" in text or "AssertionError" in text or self.errors!=[]:
            WebDriverTools.get_pic(self.driver, self.testCaseID)
        self.start = str((datetime.datetime.now() - self.start).seconds)
        self.start = self.start + "s"
        self.now = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()).split(" ")[-1]
        self.driver.quit()
        MemcacheTools.setMemTime(self.testCaseID,{'start':self.startTime,'end':self.now})

    def Test(self):
        driver = self.driver
        self.errors = []
        self.tools.enterProject(driver, self.project[0], self.project[1], self.errors)
        sleep(3)
        self.tools.enterPage(driver, self.project[2], self.project[3], self.project[1], timeout=180)
        self.GetEnergyValue(driver)

        OtherTools.raiseError(self.errors)
        print('上海世贸皇家艾美酒店，检查负值，测试完成！')

    def GetEnergyValue(self, driver):
        strTodayTotalEnergy = driver.find_element_by_xpath("//div[@id='threeItem1']/div/div/p[1]/span").text
        strEnergy0 = driver.find_element_by_xpath("//div[@id='legendData00']/div[2]/div[3]/span").text
        strEnergy1 = driver.find_element_by_xpath("//div[@id='legendData01']/div[2]/div[3]/span").text
        strEnergy2 = driver.find_element_by_xpath("//div[@id='legendData02']/div[2]/div[3]/span").text
        strEnergy3 = driver.find_element_by_xpath("//div[@id='legendData03']/div[2]/div[3]/span").text
        strEnergy4 = driver.find_element_by_xpath("//div[@id='legendData04']/div[2]/div[3]/span").text
        strEnergy5 = driver.find_element_by_xpath("//div[@id='legendData05']/div[2]/div[3]/span").text
        strEnergy6 = driver.find_element_by_xpath("//div[@id='legendData06']/div[2]/div[3]/span").text
        strEnergy7 = driver.find_element_by_xpath("//div[@id='legendData07']/div[2]/div[3]/span").text
        strEnergy8 = driver.find_element_by_xpath("//div[@id='legendData08']/div[2]/div[3]/span").text
        strEnergy9 = driver.find_element_by_xpath("//div[@id='legendData09']/div[2]/div[3]/span").text
        strEnergy10 = driver.find_element_by_xpath("//div[@id='legendData10']/div[2]/div[3]/span").text
        strEnergy11 = driver.find_element_by_xpath("//div[@id='legendData11']/div[2]/div[3]/span").text
        strEnergy12 = driver.find_element_by_xpath("//div[@id='legendData12']/div[2]/div[3]/span").text
        strEnergy13 = driver.find_element_by_xpath("//div[@id='legendData13']/div[2]/div[3]/span").text
        strEnergy14 = driver.find_element_by_xpath("//div[@id='legendData14']/div[2]/div[3]/span").text
        strLastDayEnergy = driver.find_element_by_id('lastDayEnergyValue').text

        todayTotalEnergy = self.ParseEnergyValue(strTodayTotalEnergy)
        energy0 = self.ParseEnergyValue(strEnergy0)
        energy1 = self.ParseEnergyValue(strEnergy1)
        energy2 = self.ParseEnergyValue(strEnergy2)
        energy3 = self.ParseEnergyValue(strEnergy3)
        energy4 = self.ParseEnergyValue(strEnergy4)
        energy5 = self.ParseEnergyValue(strEnergy5)
        energy6 = self.ParseEnergyValue(strEnergy6)
        energy7 = self.ParseEnergyValue(strEnergy7)
        energy8 = self.ParseEnergyValue(strEnergy8)
        energy9 = self.ParseEnergyValue(strEnergy9)
        energy10 = self.ParseEnergyValue(strEnergy10)
        energy11 = self.ParseEnergyValue(strEnergy11)
        energy12 = self.ParseEnergyValue(strEnergy12)
        energy13 = self.ParseEnergyValue(strEnergy13)
        energy14 = self.ParseEnergyValue(strEnergy14)
        lastDayEnergy = self.ParseEnergyValue(strLastDayEnergy)

        self.CheckValue(todayTotalEnergy)
        self.CheckValue(energy0)
        self.CheckValue(energy1)
        self.CheckValue(energy2)
        self.CheckValue(energy3)
        self.CheckValue(energy4)
        self.CheckValue(energy5)
        self.CheckValue(energy6)
        self.CheckValue(energy7)
        self.CheckValue(energy8)
        self.CheckValue(energy9)
        self.CheckValue(energy10)
        self.CheckValue(energy11)
        self.CheckValue(energy12)
        self.CheckValue(energy13)
        self.CheckValue(energy14)
        self.CheckValue(lastDayEnergy)

        sleep(3)

    def ParseEnergyValue(self, value):
        if 'NaN'in value or "None" in value or "Null" in value:
            return False
        else:
            value = str(value)
            value = value.replace(',', '')
            value = value.replace(' ', '')
            value = value.replace('kWh', '')
            if value=="0.0":
                return 0
            else:
                return int(value)

    def CheckValue(self, value):
        if isinstance(value,int):
            if value < 0:
                self.errors.append('数值：' + str(value) + ' 小于0！')
        else:
            self.errors.append('数值：' + str(value) + ' 返回数据values为空！')

if __name__ == "__main__":
    suit = unittest.TestSuite()
    suit.addTest(Case087('Test'))
    runner = unittest.TextTestRunner()
    runner.run(suit)


from flask import jsonify, views, request

from app import db
from models import Department, Location, Country

class DepartmentApi(views.MethodView):

    def get(self, country_id=None):
        if not country_id:
            return jsonify([a.serialize for a in Department.query.all()])
        else:
            departmentsCountry = db.session.query(Department,Location,Country)\
                .filter(Country.country_id == Location.country_id)\
                .filter(Department.location_id == Location.location_id)\
                .filter(Country.country_id == country_id)\
                .all()

            json = []
            for department, location, country in departmentsCountry:
                dict = {}
                dict.update(department.serialize)
                dict.update(location.serialize)
                dict.update(country.serialize)
                json.append(dict)

            return jsonify(json)

    def post(self):
        if request.json:
            data = request.json
        else:
            data = request.form

        if(len(str(data.get('department_id'))) == 0
          or int(data.get('department_id')) == 0
          or len(data.get('department_name')) == 0):

            return jsonify({'message': 'Informe o Dpt ID e Dpt Name.'}), 409

        departmentID = Department.query.filter_by(department_id=data.get('department_id')).first()
        managerID = None

        if data.get('manager_id') != None:
            managerID = Department.query.filter_by(manager_id=data.get('manager_id')).first()

        if departmentID:
            return jsonify({'message': 'Dpt ID já cadastrado.'}), 409

        if managerID:
            return jsonify({'message': 'Manager ID já cadastrado.'}), 409

        department = Department().department_form_data(data)

        db.session.add(department)

        try:
            db.session.commit()
            return jsonify(
                {'message': 'Department inserido.'})
        except Exception as e:
            return jsonify({'message': 'Não foi possivel executar a operação.'}), 500

    def put(self):
        if request.json:
            data = request.json
        else:
            data = request.form

        if (len(str(data.get('department_id'))) == 0
                or int(data.get('department_id')) == 0
                or len(data.get('department_name')) == 0):
            return jsonify({'message': 'Informe o Dpt ID e Dpt Name.'}), 409

        editedDepartment = Department.query.filter_by(department_id=data.get('department_id')).first()
        editedDepartment.department_name = data.get('department_name')
        editedDepartment.manager_id = data.get('manager_id')
        editedDepartment.location_id = data.get('location_id')

        db.session.add(editedDepartment)

        try:
            db.session.commit()
            return jsonify(
                {'message': 'Department atualizado.'})
        except Exception as e:
            return jsonify({'message': 'Não foi possivel executar a operação.'}), 500

    def delete(self, id):
        Department.query.filter_by(department_id=id).delete()

        try:
            db.session.commit()
            return jsonify(
                {'message': 'Department removido.'})
        except Exception as e:
            return jsonify({'message': 'Não foi possivel executar a operação.'}), 500


import time
import gym
import numpy as np
import decimal

env = gym.make('CartPole-v0')
q_table = [] #Where each line of this Q-table is state, action 1, action 2, Q-value 1, Q-value 2
#Learning parameters
learning_rate = 0.1
gamma = 0.99
r_init = 20

t = 0
i_episode = 0
benchmark = 75

while i_episode < 1000:
    if i_episode % 500 == 0:
        print(q_table)
        print(len(q_table))
    observation = env.reset()
    observation = [round(observation[i],3) for i in range(len(observation))]
    observation = [observation[2],observation[3]]
    for t in range(2000): #For each timestep t in episode i_episode...
        found = False
        env.render()
        #Search for state in Q-table and choose action based on action there
        for i in range(len(q_table)):
            if np.array_equal(q_table[i][0],observation):
                found = True
                if q_table[i][3] == q_table[i][4]:
                    action = env.action_space.sample()
                else:
                    #action = (np.argmax([q_table[i][3],q_table[i][4]]) - 3) #Action is either 0 or 1
                    action = 1
                    q_t = q_table[i][action+3] #Q-value of the action at state s_t
                s_t = i
        else: #If not in Q-table, sample random action
            if found == False:
                action = env.action_space.sample()
                q_table.append([observation,0,1,r_init,r_init])
                #print("Added %s to the Q-table" % action)
                s_t = len(q_table)-1
                a_t = q_table[s_t][action+3]
        found = False
        observation, reward, done, info = env.step(action) #Take action and observe reward
        observation = [round(observation[i],3) for i in range(len(observation))]
        observation = [observation[2],observation[3]]
        #Find location of new state s_t+1 (s_tt) in Q-table
        for j in range(len(q_table)):
            if np.array_equal(q_table[j][0],observation):
                found = True
                s_tt = j
                #Q-value updated towards landing state's argmax Q-value
                q_table[s_t][action+3] += learning_rate * ((reward + (gamma * max(q_table[s_tt][3],q_table[s_tt][4]))) - q_table[s_t][action+1])
        else: #If this new state has not been visited before
            if found == False:
                q_table[s_t][action+3] += learning_rate * ((reward + (gamma * r_init)) - q_table[s_t][action+1])
        if done:
            t += t
            print("Episode {} finished after {} timesteps".format(i_episode,t))
            i_episode += 1
            break


# This file is part of the GBI project.
# Copyright (C) 2013 Omniscale GmbH & Co. KG <http://omniscale.com>
#
# 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 re
import urllib

from werkzeug.exceptions import NotFound
from flask import (render_template, Blueprint, flash,
    redirect, url_for, request, current_app, jsonify, g)

# XXX olt: do not import from flask.ext, makes trouble with pyinstaller
from flaskext.babel import gettext as _

from geobox.lib.couchdb import CouchFileBox
from geobox.lib.file_validation import get_file_information
from geobox.web.forms import UploadForm, ImportGeoJSONEdit
from geobox.model import VectorImportTask, User, GBIServer
from geobox.lib.server_logging import send_task_logging
from .vector import prepare_geojson_form

boxes = Blueprint('boxes', __name__)

@boxes.route("/box/<box_name>", methods=["GET", "POST"])
def files(box_name, user_id=None):
    is_consultant = current_app.config.geobox_state.user.is_consultant
    if (box_name == 'file' and not is_consultant):
        raise NotFound()
    if ((box_name == 'download' or box_name == 'upload') and is_consultant):
        raise NotFound()
    form = UploadForm()
    import_form = ImportGeoJSONEdit()
    import_form = prepare_geojson_form(import_form)

    couch_box = get_couch_box_db(box_name)

    host = request.headers.get('Host').split(':')[0]
    couch = CouchFileBox(
        'http://%s:%s' % (
            host,
            current_app.config.geobox_state.config.get('couchdb', 'port')
        ), couch_box)
    if form.validate_on_submit():
        file = request.files['file']
        overwrite = True if request.form.get('overwrite') == 'true' else False
        if file:
            data = get_file_information(file)
            if data:
                couch.store_file(data, overwrite=overwrite)
                flash(_('upload success'), 'success')
            else:
                flash(_('file type not allowed'), 'error')

    files = couch.all_files()
    for f in files:
        f['download_link'] = couchid_to_link(f['id'],
                                             couch_url=couch.couch_url)

    return render_template("boxes/%s.html" % box_name, form=form, files=files,
                           box_name=box_name, import_form=import_form)

def couchid_to_link(filename, couch_url):
    if isinstance(filename, unicode):
        filename = filename.encode('utf-8')
    return "%s/%s/file" % (couch_url, urllib.quote(filename))

def get_couch_box_db(box_name):
    app_state = current_app.config.geobox_state
    if box_name == 'download':
        couch_box_name = app_state.config.get('couchdb', 'download_box')
    elif box_name == 'upload':
        couch_box_name = app_state.config.get('couchdb', 'upload_box')
    elif box_name == 'file':
        couch_box_name = app_state.config.get('couchdb', 'file_box')
    else:
        raise NotFound()
    gbi_server = GBIServer.current_home_server(g.db)
    if gbi_server is None:
        return couch_box_name
    return '%s_%s' % (gbi_server.prefix, couch_box_name)

@boxes.route("/box/<box_name>/check_file", methods=["POST"])
def check_file_exists(box_name):

    couch_box = get_couch_box_db(box_name)
    couch = CouchFileBox('http://127.0.0.1:%s' % current_app.config.geobox_state.config.get('couchdb', 'port'), couch_box)
    existing_doc = couch.get(request.form['filename'])

    if existing_doc:
        return jsonify(existing=True)
    return jsonify(existing=False)

@boxes.route("/box/<box_name>/import/<id>", methods=["POST"])
def import_json(box_name, id):
    layer = request.form.get('layers', False)
    new_layer = request.form.get('name', False)
    file_name = request.form.get('file_name', False)
    title = None

    if (layer and new_layer) or (not layer and not new_layer):
        flash(_('please select new layer or current layer to import'), 'error')
        return redirect(url_for('.files', box_name=box_name))
    if layer:
        layer = layer
    else:
        title = new_layer
        layer = 'local_vector_' + re.sub(r'[^a-z0-9]*', '',  new_layer.lower())
        if layer == 'local_vector_':
            flash(_('None of the characters used for the layer is allowed'))
            return redirect(url_for('.files', box_name=box_name))

    task = VectorImportTask(
        db_name=layer,
        title=title,
        file_name=file_name,
        type_ = 'geojson',
        source = get_couch_box_db(box_name)
    )
    g.db.add(task)
    g.db.commit()
    flash(_("file will be imported"), 'success')
    return redirect(url_for('tasks.list'))

@boxes.route('/box/<box_name>/delete/<id>/<rev>', methods=["GET", "POST"])
def delete_file(box_name, id, rev):
    couch_box = get_couch_box_db(box_name)
    couch = CouchFileBox('http://127.0.0.1:%s' % current_app.config.geobox_state.config.get('couchdb', 'port'), couch_box)
    couch.delete(id, rev)
    flash(_("file deleted"), 'success')
    return redirect(url_for("boxes.files", box_name=box_name))


# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals

from collections import defaultdict
import fnmatch
import json
import os
from os.path import join, dirname, isfile, isdir
import re
import sys

import requests

CHANNEL_NAME = "main"
CHANNEL_ALIAS = "https://repo.anaconda.com/pkgs"
SUBDIRS = (
    "noarch",
    "linux-32",
    "linux-64",
    "linux-aarch64",
    "linux-armv6l",
    "linux-armv7l",
    "linux-ppc64le",
    "osx-64",
    "win-32",
    "win-64",
)

REMOVALS = {
    "noarch": (),
    "linux-ppc64le": [],
    "osx-64": [],
    "win-32": ["nomkl-*"],
    "win-64": ["nomkl-*"],
    "any": {
        # early efforts on splitting numpy recipe did not pin numpy-base exactly.
        #     These led to bad builds (built against newest numpy)
        "numpy-*1.11.3-*_6.tar.bz2",
        "numpy-*1.14.3-*_2.tar.bz2",
        # missing blas deps that make solver able to choose wrong things.
        #    numpy-base is pinned exactly, but this effectively defeated the
        #    variant being able to distinguish between openblas and mkl.
        "numpy-1.14.5-py35h28100ab_0.tar.bz2",
        # early vs2017 packages used wrong version numbers
        "vs2017_win-*-15.5.2*",
        "vs2017*h590f102_0*",
        "vs2017*hb4ce483_0*"
    }
}

REVOKED = {
    "linux-64": [
        # build _0 were built against an invalid CuDNN library version
        "tensorflow-base-1.9.0-gpu_py35h9f529ab_0.tar.bz2",
        "tensorflow-base-1.9.0-gpu_py36h9f529ab_0.tar.bz2",
        "tensorflow-base-1.9.0-gpu_py27h9f529ab_0.tar.bz2",
        # compilers with wrong dependencies (missing impl)
        "g*_linux-64-7.2.0-24.tar.bz2",
        ],
    "linux-32": [
        # build _0 were built against an invalid CuDNN library version
        "tensorflow-base-1.9.0-gpu_py35h9f529ab_0.tar.bz2",
        "tensorflow-base-1.9.0-gpu_py36h9f529ab_0.tar.bz2",
        "tensorflow-base-1.9.0-gpu_py27h9f529ab_0.tar.bz2",
        # compilers with wrong dependencies (missing impl)
        "g*_linux-32-7.2.0-24.tar.bz2",
        ],
    "linux-ppc64le": [],
    "osx-64": [],
    "win-32": [
        "spyder-kernels-1.0.1-*_0"
    ],
    "win-64": [
        "spyder-kernels-1.0.1-*_0"
    ],
    "any": []
}

BLAS_USING_PKGS = {"numpy", "numpy-base", "scipy", "numexpr", "scikit-learn", "libmxnet"}

def _replace_vc_features_with_vc_pkg_deps(fn, record, instructions):
    python_vc_deps = {
        '2.6': 'vc 9.*',
        '2.7': 'vc 9.*',
        '3.3': 'vc 10.*',
        '3.4': 'vc 10.*',
        '3.5': 'vc 14.*',
        '3.6': 'vc 14.*',
        '3.7': 'vc 14.*',
    }
    vs_runtime_deps = {
        9: 'vs2008_runtime',
        10: 'vs2010_runtime',
        14: 'vs2015_runtime',
    }
    record_name = record['name']
    if record_name == 'python':
        # remove the track_features key
        if 'track_features' in record:
            instructions["packages"][fn]['track_features'] = None
        # add a vc dependency
        if not any(d.startswith('vc') for d in record['depends']):
            depends = record['depends']
            depends.append(python_vc_deps[record['version'][:3]])
            instructions["packages"][fn]['depends'] = depends
    elif record_name == "vs2015_win-64":
        # remove the track_features key
        if 'track_features' in record:
            instructions["packages"][fn]['track_features'] = None
    elif record['name'] == "yasm":
        # remove vc from the features key
        vc_version = _extract_and_remove_vc_feature(record)
        if vc_version:
            instructions["packages"][fn]['features'] = record.get('features') or ""
            # add a vs20XX_runtime dependency
            if not any(d.startswith('vs2') for d in record['depends']):
                depends = record['depends']
                depends.append(vs_runtime_deps[vc_version])
                instructions["packages"][fn]['depends'] = depends
    elif record_name == "git":
        # remove any vc dependency, as git does not depend on a specific one
        depends = [dep for dep in record['depends'] if not dep.startswith('vc ')]
        if len(depends) != (record['depends']):
            instructions["packages"][fn]['depends'] = depends
    elif 'vc' in record.get('features', ''):
        # remove vc from the features key
        vc_version = _extract_and_remove_vc_feature(record)
        if vc_version:
            instructions["packages"][fn]['features'] = record.get('features') or ""
            # add a vc dependency
            if not any(d.startswith('vc') for d in record['depends']):
                instructions["packages"][fn]['depends'] = record["depends"] + ['vc %d.*' % vc_version]
    # elif record["name"] == "vc" and "vc" not in record.get("track_features", ""):
    #     tf = record.get("track_features", "") + " vc" + record["version"]
    #     instructions["packages"][fn]["track_features"] = tf.strip()
    # elif record.get("track_features", "").startswith("vc"):
    #     vc_feats = (f for f in record["track_features"].split() if f.startswith("vc"))
    #     for feat in vc_feats:
    #         xtractd = record["track_features"] = _extract_track_feature(record, feat)
    #         instructions["packages"][fn]["track_features"] = xtractd

def _apply_namespace_overrides(fn, record, instructions):
    record_name = record["name"]
    namespace_in_name_set = {
        "python-crfsuite",
        "python-daemon",
        "python-dateutil",
        "python-editor",
        "python-engineio",
        "python-gflags",
        "python-ldap",
        "python-memcached",
        "python-ntlm",
        "python-rapidjson",
        "python-slugify",
        "python-snappy",
        "python-socketio",
        "python-sybase",
        "python-utils",
    }
    namespace_overrides = {
        "boost": "python",
        "ninja": "global",
        "numpy-devel": "python",
        "texlive-core": "global",
        "keras": "python",
        "keras-gpu": "python",
        "git": "global",
        "python-javapackages-cos7-ppc64le": "global",
        "anaconda": "python",
        "conda-env": "python",
        "tensorflow": "python",
        "tensorflow-gpu": "python",
        "xcb-proto": "global",
        "mxnet": "python",
    }
    if record_name in namespace_in_name_set and not record.get('namespace_in_name'):
        # set the namespace_in_name field
        instructions["packages"][fn]['namespace_in_name'] = True
    if namespace_overrides.get(record_name):
        # explicitly set namespace
        instructions["packages"][fn]['namespace'] = namespace_overrides[record_name]

def _fix_linux_runtime_bounds(fn, record, instructions):
    linux_runtime_re = re.compile(r"lib(\w+)-ng\s(?:>=)?([\d\.]+\d)(?:$|\.\*)")
    if any(dep.split()[0] in ("libgcc-ng", "libstdcxx-ng", "libgfortran-ng")
           for dep in record.get('depends', [])):
        deps = []
        for dep in record['depends']:
            match = linux_runtime_re.match(dep)
            if match:
                dep = "lib{}-ng >={}".format(match.group(1), match.group(2))
                if match.group(1) == "gfortran":
                    # this is adding an upper bound
                    lower_bound = int(match.group(2)[0])
                    # ABI break at gfortran 8
                    if lower_bound < 8:
                        dep += ",<8.0a0"
            deps.append(dep)
        instructions["packages"][fn]["depends"] = deps

def _fix_nomkl_features(fn, record, instructions):
    if "nomkl" == record["features"]:
        del record['features']
        if not any(d.startswith("blas ") for d in record["depends"]):
            instructions["packages"][fn]["depends"] = record['depends'] + ["blas * openblas"]
    elif "nomkl" in record["features"]:
        # remove nomkl feature
        record['features'].remove('nomkl')
        if not any(d.startswith("blas ") for d in record["depends"]):
            instructions["packages"][fn]["depends"] = record['depends'] + ["blas * openblas"]
            instructions["packages"][fn]["features"] = record['features']

def _patch_repodata(repodata, subdir):
    index = repodata["packages"]
    instructions = {
        "patch_instructions_version": 1,
        "packages": defaultdict(dict),
        "revoke": [],
        "remove": [],
    }

    if subdir == "noarch":
        instructions["external_dependencies"] = {
            "util-linux": "global:util-linux",  # libdap4, pynio
            "meld3": "python:meld3",  # supervisor
            "msys2-conda-epoch": "global:msys2-conda-epoch",  # anaconda
        }

    for fn, record in index.items():
        if (any(fnmatch.fnmatch(fn, rev) for rev in REVOKED.get(subdir, [])) or
                 any(fnmatch.fnmatch(fn, rev) for rev in REVOKED.get("any", []))):
            instructions['revoke'].append(fn)
        if (any(fnmatch.fnmatch(fn, rev) for rev in REMOVALS.get(subdir, [])) or
                 any(fnmatch.fnmatch(fn, rev) for rev in REMOVALS.get("any", []))):
            instructions['remove'].append(fn)
        _apply_namespace_overrides(fn, record, instructions)
        if fn.startswith("numba-0.36.1") and record.get('timestamp') != 1512604800000:
            # set a specific timestamp
            instructions["packages"][fn]['timestamp'] = 1512604800000

        # strip out pyobjc stuff from twisted  (maybe Kale understands this one?)
        #
        # 2018/09/10: we're not sure why this one was necessary.  Commenting until we understand the need for it.
        #
        # if record["name"] == "twisted" and any(dep.startswith("pyobjc-") for dep in record.get("constrains", ())):
        #         instructions["packages"][fn]['constrains'] = [dep for dep in record["constrains"]
        #                                                       if not dep.startswith("pyobjc-")]

        if "features" in record:
            _fix_nomkl_features(fn, record, instructions)

        # this was a not-very-successful approach at fixing features
        blas_req_feature = record.get('requires_features', {}).get("blas")
        if blas_req_feature:
            if not any(dep.startswith("blas ") for dep in record['depends']):
                record['depends'].append("blas * %s" % blas_req_feature)
                instructions["packages"][fn]["depends"] = record['depends']
            # del record["requires_features"]["blas"]
            # instructions["packages"][fn]["requires_features"] = record["requires_features"]

        if record.get("track_features"):
            for feat in record["track_features"].split():
                if feat.startswith(("rb2", "openjdk")):
                    xtractd = record["track_features"] = _extract_track_feature(record, feat)
                    instructions["packages"][fn]["track_features"] = xtractd

        # reset dependencies for nomkl to the blas metapkg and remove any
        #      track_features (these are attached to the metapkg instead)
        if record['name'] == 'nomkl' and not subdir.startswith("win-"):
            record['depends'] = ["blas * openblas"]
            if 'track_features' in record:
                instructions["packages"][fn]["track_features"] = None

        if record['name'] == 'conda-env':
            if not any(d.startswith('python') for d in record['depends']):
                instructions["packages"][fn]["namespace"] = "python"

        if record['name'] == 'openblas-devel' and not any(d.startswith('blas ') for d in record['depends']):
            record["depends"].append("blas * openblas")
            instructions["packages"][fn]["depends"] = record["depends"]

        if record['name'] == 'mkl-devel' and not any(d.startswith('blas') for d in record['depends']):
            record["depends"].append("blas * mkl")
            instructions["packages"][fn]["depends"] = record["depends"]

        if record['name'] == 'pyqt'and record['version'] == '5.9.2':
            # pyqt needs an upper limit of sip, build 2 has this already
            if 'sip >=4.19.4' in record['depends']:
                sip_index = record['depends'].index('sip >=4.19.4')
                record['depends'][sip_index]= 'sip >=4.19.4,<=4.19.8'
                instructions["packages"][fn]["depends"] = record["depends"]

        if fn == 'cupti-9.0.176-0.tar.bz2':
            # depends in package is set as cudatoolkit 9.*, should be 9.0.*
            instructions["packages"][fn]["depends"] = ['cudatoolkit 9.0.*']

        # add in blas mkl metapkg for mutex behavior on packages that have just mkl deps
        if (record['name'] in BLAS_USING_PKGS and not
                   any(dep.split()[0] == "blas" for dep in record['depends'])):
            if any(dep.split()[0] == 'mkl' for dep in record['depends']):
                record["depends"].append("blas * mkl")
            elif any(dep.split()[0] in ('openblas', "libopenblas") for dep in record['depends']):
                record["depends"].append("blas * openblas")
            instructions["packages"][fn]["depends"] = record["depends"]

        # some of these got hard-coded to overly restrictive values
        if record['name'] in ('scikit-learn', 'pytorch'):
            new_deps = []
            for dep in record['depends']:
                if dep.startswith('mkl 2018'):
                    if not any(_.startswith('mkl >') for _ in record['depends']):
                        new_deps.append("mkl >=2018.0.3")
                elif dep == 'nccl':
                    # pytorch was built with nccl 1.x
                    new_deps.append('nccl <2')
                else:
                    new_deps.append(dep)
            record["depends"] = new_deps
            instructions["packages"][fn]["depends"] = record["depends"]

        if subdir.startswith("win-"):
            _replace_vc_features_with_vc_pkg_deps(fn, record, instructions)

        elif subdir.startswith("linux-"):
            _fix_linux_runtime_bounds(fn, record, instructions)

    instructions['remove'].sort()
    instructions['revoke'].sort()
    return instructions

def _extract_and_remove_vc_feature(record):
    features = record.get('features', '').split()
    vc_features = tuple(f for f in features if f.startswith('vc'))
    if not vc_features:
        return None
    non_vc_features = tuple(f for f in features if f not in vc_features)
    vc_version = int(vc_features[0][2:])  # throw away all but the first
    if non_vc_features:
        record['features'] = ' '.join(non_vc_features)
    else:
        del record['features']
    return vc_version

def _extract_feature(record, feature_name):
    features = record.get('features', '').split()
    features.remove(feature_name)
    return " ".join(features)

def _extract_track_feature(record, feature_name):
    features = record.get('track_features', '').split()
    features.remove(feature_name)
    return " ".join(features)

def do_hotfixes(base_dir):
    # Step 1. Collect initial repodata for all subdirs.
    repodatas = {}
    for subdir in SUBDIRS:
        repodata_path = join(base_dir, subdir, 'repodata-clone.json')
        if isfile(repodata_path):
            with open(repodata_path) as fh:
                repodatas[subdir] = json.load(fh)
        else:
            repodata_url = "/".join((CHANNEL_ALIAS, CHANNEL_NAME, subdir, "repodata.json"))
            response = requests.get(repodata_url)
            response.raise_for_status()
            repodatas[subdir] = response.json()
            if not isdir(dirname(repodata_path)):
                os.makedirs(dirname(repodata_path))
            with open(repodata_path, 'w') as fh:
                json.dump(repodatas[subdir], fh, indent=2, sort_keys=True, separators=(',', ': '))

    # Step 2. Create all patch instructions.
    patch_instructions = {}
    for subdir in SUBDIRS:
        instructions = _patch_repodata(repodatas[subdir], subdir)
        patch_instructions_path = join(base_dir, subdir, "patch_instructions.json")
        with open(patch_instructions_path, 'w') as fh:
            json.dump(instructions, fh, indent=2, sort_keys=True, separators=(',', ': '))
        patch_instructions[subdir] = instructions

def main():
    base_dir = join(dirname(__file__), CHANNEL_NAME)
    do_hotfixes(base_dir)

if __name__ == "__main__":
    sys.exit(main())


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

###############################################################################
#  Copyright Kitware Inc.
#
#  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.
###############################################################################

from enum import Enum

from girder.constants import AccessType
from girder.models.folder import Folder as FolderModel
from girder.models.model_base import ValidationException

class Study(FolderModel):
    class State(Enum):
        ACTIVE = 'active'
        COMPLETE = 'complete'

    def initialize(self):
        super(Study, self).initialize()
        # TODO: add indexes

        self._filterKeys[AccessType.READ].clear()
        self.exposeFields(level=AccessType.READ, fields=[
            '_id', 'name', 'description', 'created', 'creatorId', 'updated'
        ])
        self.summaryFields = ['_id', 'name', 'updated']
        self.prefixSearchFields = ['lowerName', 'name']

    def loadStudyCollection(self):
        # assumes collection has been created by provision_utility
        # TODO: cache this value
        return self.model('collection').findOne({'name': 'Annotation Studies'})

    def createStudy(self, name, creatorUser, featureset, annotatorUsers,
                    segmentations):
        # this may raise a ValidationException if the name already exists
        studyFolder = self.createFolder(
            parent=self.loadStudyCollection(),
            name=name,
            description='',
            parentType='collection',
            public=None,
            creator=creatorUser
        )
        studyFolder = self.copyAccessPolicies(
            src=self.loadStudyCollection(),
            dest=studyFolder,
            save=False
        )
        studyFolder = self.setUserAccess(
            doc=studyFolder,
            user=creatorUser,
            # TODO: make admin
            level=AccessType.READ,
            save=False
        )
        # "setMetadata" will always save
        studyFolder = self.setMetadata(
            folder=studyFolder,
            metadata={
                'featuresetId': featureset['_id']
            }
        )

        for annotatorUser in annotatorUsers:
            self.addAnnotator(studyFolder, annotatorUser, creatorUser,
                              segmentations)

        return studyFolder

    def addAnnotator(self, study, annotatorUser, creatorUser,
                     segmentations=None):
        Annotation = self.model('annotation', 'isic_archive')
        if not segmentations:
            segmentations = self.getSegmentations(study)

        if self.childAnnotations(
                study=study,
                annotatorUser=annotatorUser).count():
            raise ValidationException(
                'Annotator user is already part of the study.')

        annotatorFolder = self.createFolder(
            parent=study,
            name='%(login)s (%(firstName)s %(lastName)s)' % annotatorUser,
            description='',
            parentType='folder',
            public=True,
            creator=creatorUser
        )
        # study creator accesses will already have been copied to this
        # sub-folder
        # TODO: all users from the study don't need access; this should be
        # changed and migrated
        self.setUserAccess(
            doc=annotatorFolder,
            user=annotatorUser,
            # TODO: make write
            level=AccessType.READ,
            save=True
        )
        self.setMetadata(
            folder=annotatorFolder,
            metadata={
                'userId': annotatorUser['_id']
            }
        )

        for segmentation in segmentations:
            Annotation.createAnnotation(
                study, segmentation, creatorUser, annotatorFolder)

    def addSegmentation(self, study, segmentation, creatorUser):
        Folder = self.model('folder')
        Annotation = self.model('annotation', 'isic_archive')
        for annotatorFolder in Folder.find({'parentId': study['_id']}):
            Annotation.createAnnotation(
                study, segmentation, creatorUser, annotatorFolder)

    def getFeatureset(self, study):
        return self.model('featureset', 'isic_archive').load(
            study['meta']['featuresetId'], exc=True)

    def getAnnotators(self, study):
        Folder = self.model('folder')
        User = self.model('user')
        annotatorFolders = Folder.find({'parentId': study['_id']})
        return User.find({
            '_id': {'$in': annotatorFolders.distinct('meta.userId')}
        })

    def getSegmentations(self, study):
        Annotation = self.model('annotation', 'isic_archive')
        Segmentation = self.model('segmentation', 'isic_archive')
        segmentationIds = Annotation.find(
            {'meta.studyId': study['_id']}).distinct('meta.segmentationId')
        return Segmentation.find({'_id': {'$in': segmentationIds}})

    def getImages(self, study):
        Annotation = self.model('annotation', 'isic_archive')
        Image = self.model('image', 'isic_archive')
        imageIds = Annotation.find({
            'meta.studyId': study['_id']}).distinct('meta.imageId')
        return Image.find({'_id': {'$in': imageIds}})

    def childAnnotations(self, study=None, annotatorUser=None,
                         segmentation=None, imageItem=None, state=None,
                         **kwargs):
        Annotation = self.model('annotation', 'isic_archive')
        query = dict()
        if study:
            query['meta.studyId'] = study['_id']
        if annotatorUser:
            query['meta.userId'] = annotatorUser['_id']
        if segmentation:
            query['meta.segmentationId'] = segmentation['_id']
        if imageItem:
            query['meta.imageId'] = imageItem['_id']
        if state:
            if state == self.State.ACTIVE:
                query['meta.stopTime'] = None
            elif state == self.State.COMPLETE:
                query['meta.stopTime'] = {'$ne': None}
            else:
                raise ValueError('"state" must be an instance of State')
        return Annotation.find(query, **kwargs)

    def _findQueryFilter(self, query, annotatorUser, state):
        studyQuery = {
            'parentId': self.loadStudyCollection()['_id']
        }
        if query:
            studyQuery.update(query)
        if state or annotatorUser:
            annotations = self.childAnnotations(
                annotatorUser=annotatorUser,
                state=state
            )
            studyQuery.update({
                '_id': {'$in': annotations.distinct('meta.studyId')}
            })
        return studyQuery

    def list(self, user=None, limit=0, offset=0, sort=None):
        """
        Return a paginated list of studies that a user may access.
        """
        cursor = self.find({}, sort=sort)
        return self.filterResultsByPermission(
            cursor=cursor, user=user, level=AccessType.READ, limit=limit,
            offset=offset)

    def find(self, query=None, annotator_user=None, state=None, **kwargs):
        studyQuery = self._findQueryFilter(query, annotator_user, state)
        return super(Study, self).find(studyQuery, **kwargs)

    def findOne(self, query=None, annotator_user=None, state=None, **kwargs):
        studyQuery = self._findQueryFilter(query, annotator_user, state)
        return super(Study, self).findOne(studyQuery, **kwargs)

    def validate(self, doc, **kwargs):
        # TODO: implement
        return super(Study, self).validate(doc, **kwargs)


#!/usr/bin/env python3

# --- Modules --- #
import drawBot as dB
from pathlib import Path
from collections import defaultdict

# --- Constants --- #
DICT_FOLDER = Path('dictionaries')
PRECISION = 2

# --- Objects & Methods --- #
def calcWordsWidth(words, fontName):
    dB.font(fontName, 1)
    width_2_words = defaultdict(set)
    for eachWord in words:
        txtWdt, txtHgt = dB.textSize(eachWord)
        width_2_words[round(txtWdt, PRECISION)].add(eachWord)
    return width_2_words

# --- Variables --- #
fontName = 'Skia'
language = 'italian'

# --- Instructions --- #
if __name__ == '__main__':
    from loadWords_v4 import loadWords
    words = loadWords(DICT_FOLDER / f'{language}.txt')
    width_2_words = calcWordsWidth(words, fontName)
    print(f'words: {len(words)}')
    # words: 16458
    print(f'groups: {len(width_2_words)}')
    # groups: 573
    print(f'words/groups ratio: {len(words)/len(width_2_words):.3f}')
    # words/groups ratio: 28.723


# coding=utf-8
from django.contrib.auth.decorators import login_required
from django.contrib.auth.decorators import permission_required
from django.contrib.messages.views import SuccessMessageMixin
from django.core.urlresolvers import reverse
from django.views.generic.edit import UpdateView

from modules.contract_manage.intern.intern_forms import ContractInternForm
from modules.contract_manage.models import *
from modules.share_module.formater import *
from modules.share_module.permissionMixin import class_view_decorator

@class_view_decorator(login_required)
@class_view_decorator(permission_required('contract_manage.change_contractintern', raise_exception=True))
class ContractInternUpdate(SuccessMessageMixin, UpdateView):
	model = ContractIntern
	form_class = ContractInternForm
	template_name = "contract_edit.html"
	success_message = u"%(name)s 成功修改"

	def get_context_data(self, **kwargs):
		context = super(ContractInternUpdate, self).get_context_data(**kwargs)
		context["form_content"] = u"合同签订-实习生"
		referrer = self.request.META.get('HTTP_REFERER', "")
		context["referrer"] = referrer
		return context

	def get_success_url(self):
		self.url = reverse('contract:list_intern', args=())
		referrer = self.request.POST.get("referrer", "")
		if referrer:
			self.url = referrer
		_addanother = self.request.POST.get("_addanother", "")
		if _addanother:
			self.url = reverse('contract:add_intern')
		return self.url

	def form_valid(self, form):
		receive_address = self.request.POST.get("receive_address", "")  # 指定通知接收地址
		address = self.request.POST.get("address", "")  # 住址
		deadline = self.request.POST.get("deadline", "")  # 合同期限
		start_date = self.request.POST.get("start_date", "")  # 合同开始时间
		end_date = self.request.POST.get("end_date", "")  # 合同结束时间

		# 计算-指定通知接收地址
		if not receive_address:
			if address:
				form.instance.receive_address = address

		# 计算-试用期
		form.instance.probation = deadline

		# 计算-合同结束时间
		if not end_date:
			if deadline and start_date:
				start_date_datetime = datetime.datetime.strptime(start_date, "%Y/%m/%d")

				if deadline == "1":  # 24个月
					end_date = add_months(start_date_datetime, 24)
				if deadline == "2":  # 36个月
					end_date = add_months(start_date_datetime, 36)
				form.instance.end_date = end_date
		return super(ContractInternUpdate, self).form_valid(form)


from django.db import models
from django.contrib.auth.models import User

class Article(models.Model):
	article_title = models.CharField(max_length=200)
	article_text  = models.TextField()
	article_date  = models.DateTimeField()
	article_likes = models.IntegerField(default=0)
	article_author = models.ForeignKey(User)

	def __str__(self):
		return self.article_title

	class Meta():
		db_table = 'article'
		ordering = ["article_date"]

class Comments(models.Model):
	comments_text 	 = models.TextField(verbose_name='')
	comments_article = models.ForeignKey(Article)
	comments_author = models.ForeignKey(User)

	class Meta():
		db_table = 'comments'


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

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('comite', '0002_resguardo_activo'),
    ]

    operations = [
        migrations.AlterModelOptions(
            name='producto',
            options={'ordering': ['clave']},
        ),
        migrations.AlterModelOptions(
            name='resguardo',
            options={'ordering': ['recibe']},
        ),
        migrations.RemoveField(
            model_name='producto',
            name='estado',
        ),
        migrations.AddField(
            model_name='producto',
            name='usado',
            field=models.IntegerField(default=0, verbose_name=b'Usado', blank=True),
            preserve_default=False,
        ),
    ]


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

import re
import time
import traceback
import requests
from requests import Response,Request
from requests.exceptions import (InvalidSchema, InvalidURL, MissingSchema,
                                 RequestException)

absolute_http_url_regexp = re.compile(r"^https?://", re.I)

class ApiResponse(Response):

    def raise_for_status(self):
        if hasattr(self, "error") and self.error:
            raise self.error
        Response.raise_for_status(self)

class HttpSession(requests.Session):

    def __init__(self):
        super(HttpSession, self).__init__()
        self.request_meta = dict()

    def _build_url(self,path):
        if absolute_http_url_regexp.match(path):
            return path
        else:
            return "%s%s" % (self.base_url, path)

    def request(self, url, method, **kwargs):
        """
        Constructs and sends a :py:class:`requests.Request`.
        Returns :py:class:`requests.Response` object.

        :param method: method for the new :class:`Request` object.
        :param url: URL for the new :class:`Request` object.
        :param params: (optional) Dictionary or bytes to be sent in the query string for the :class:`Request`.
        :param data: (optional) Dictionary or bytes to send in the body of the :class:`Request`.
        :param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.
        :param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`.
        :param files: (optional) Dictionary of ``'filename': file-like-objects`` for multipart encoding upload.
        :param auth: (optional) Auth tuple or callable to enable Basic/Digest/Custom HTTP Auth.
        :param timeout: (optional) How long in seconds to wait for the server to send data before giving up, as a float,
            or a (`connect timeout, read timeout <user/advanced.html#timeouts>`_) tuple.
        :type timeout: float or tuple
        :param allow_redirects: (optional) Set to True by default.
        :type allow_redirects: bool
        :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.
        :param stream: (optional) whether to immediately download the response content. Defaults to ``False``.
        :param verify: (optional) if ``True``, the SSL cert will be verified. A CA_BUNDLE path can also be provided.
        :param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair.
        """

        # set up pre_request hook for attaching meta data to the request object
        self.request_meta["method"] = method
        self.request_meta["start_time"] = time.time()

        response = self._send_request_safe_mode(method, url, **kwargs)

        # record the consumed time
        self.request_meta["end_time"] = time.time()

        # get the length of the content, but if the argument stream is set to True, we take
        # the size from the content-length header, in order to not trigger fetching of the body
        if kwargs.get("stream", False):
            self.request_meta["content_size"] = int(response.headers.get("content-length") or 0)
        else:
            self.request_meta["content_size"] = len(response.content or b"")

        self.request_meta["content_type"] = response.headers.get("Content-Type")
        self.request_meta["status_code"] = response.status_code
        try:
            response.raise_for_status()
            self.request_meta["status"] = "success"
        except RequestException as e:
            self.request_meta["status"] = "error"
            self.request_meta["message"] = traceback.format_exc()

        return response

    def _send_request_safe_mode(self, method, url, **kwargs):
        """
        Send an HTTP request, and catch any exception that might occur due to connection problems.

        Safe mode has been removed from requests 1.x.
        """
        try:
            return requests.Session.request(self, method, url, **kwargs)
        except (MissingSchema, InvalidSchema, InvalidURL):
            raise
        except RequestException as e:
            r = ApiResponse()
            r.status_code = 0
            r.error = e
            r.request = Request(method, url).prepare()
            return r