import copy
from operator import index
import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler
from process_data import *
# from input_process import *
import json
import os
import torch
from models.brits import BRITSModel as BRITS
from models.brits_i import BRITSModel as BRITS_I
import utils
import data_loader
import matplotlib.pyplot as plt
from sklearn.experimental import enable_iterative_imputer
from sklearn.impute import KNNImputer, IterativeImputer

RNN_HID_SIZE = 64
IMPUTE_WEIGHT = 0.3
LABEL_WEIGHT = 1

folder = './json/test/'
if not os.path.exists(folder):
    os.makedirs(folder)
fs = open(folder+'json', 'w')

real_values = None

mean = []
std = []

# features_impute = [features.index('MEAN_AT'), features.index('AVG_REL_HUMIDITY')]

def _graph_bar_diff_multi(GT_values, result_dict, title, x, xlabel, ylabel, i, drop_linear=False):
    plot_dict = {}
    plt.figure(figsize=(16,9))
    for key, value in result_dict.items():
      plot_dict[key] = np.abs(GT_values) - np.abs(value)
    # ind = np.arange(prediction.shape[0])
    x = np.array(x)
    width = 0.3
    pos = 0
    remove_keys = ['GT']
    if drop_linear:
      remove_keys.append('LinearInterp')
    for key, value in plot_dict.items():
        if key not in remove_keys:
          plt.bar(x + pos + 1, value, width, label = key)
          pos += width

    plt.xlabel(xlabel)
    plt.ylabel(ylabel)
    plt.title(title)
    plt.axis([0, 100, -2, 3])

    plt.legend(loc='best')
    plt.savefig(f'diff_imgs/result-diff-{i}.png', dpi=300)
    plt.close()
    # plt.show()
    return

def parse_delta(masks, dir_):
    if dir_ == 'backward':
        masks = masks[::-1]

    deltas = []

    for h in range(masks.shape[0]):
        if h == 0:
            deltas.append(np.ones(len(features)))
        else:
            deltas.append(np.ones(len(features)) + (1 - masks[h]) * deltas[-1])

    return np.array(deltas)

def draw_data_trend(df_t, df_c, f, interp_name, i):
    plt.figure(figsize=(32,9))
    plt.title(f)
    ax = plt.subplot(121)
    ax.set_title(f+' original')
    plt.plot(np.arange(df_t.shape[0]), df_t)
    ax = plt.subplot(122)
    ax.set_title(f+' imputed by '+interp_name)
    plt.plot(np.arange(df_c.shape[0]), df_c)
    plt.savefig(f"subplots/{f}-{interp_name}-{i}.png", dpi=300)
    plt.close()

def parse_rec(values, masks, evals, eval_masks, dir_):
    deltas = parse_delta(masks, dir_)

    # only used in GRU-D
    forwards = pd.DataFrame(values).fillna(method='ffill').fillna(0.0).to_numpy()

    rec = {}

    rec['values'] = np.nan_to_num(values).tolist()
    rec['masks'] = masks.astype('int32').tolist()
    # imputation ground-truth
    rec['evals'] = np.nan_to_num(evals).tolist()
    rec['eval_masks'] = eval_masks.astype('int32').tolist()
    rec['forwards'] = forwards.tolist()
    rec['deltas'] = deltas.tolist()

    return rec

def unnormalize(X, mean, std, feature_idx=-1):
    if feature_idx == -1:
        return (X * std) + mean
    else:
        return (X * std[feature_idx]) + mean[feature_idx]

def parse_id(x, y):
    # data = pd.read_csv('./raw/{}.txt'.format(id_))
    # accumulate the records within one hour
    # data['Time'] = data['Time'].apply(lambda x: to_time_bin(x))

    evals = x
    # print('x: ', x)

    # merge all the metrics within one hour
    # for h in range(48):
    #     evals.append(parse_data(data[data['Time'] == h]))
    evals = (evals - mean) / std
    # print('eval: ', evals)
    # print('eval shape: ', evals.shape)
    shp = evals.shape

    # idx1 = np.where(~np.isnan(evals[:,features_impute[0]]))[0]
    # idx2 = np.where(~np.isnan(evals[:,features_impute[1]]))[0]
    # idx1 = idx1 * len(features) + features_impute[0]
    # idx2 = idx2 * len(features) + features_impute[1]
    # exit()

    evals = evals.reshape(-1)
    # randomly eliminate 10% values as the imputation ground-truth
    # print('not null: ',np.where(~np.isnan(evals)))
    # indices = np.concatenate((idx1, idx2)).tolist()
    # indices = np.random.choice(indices, len(indices) // 10)

    # global real_values
    # real_values = evals[indices]

    values = evals.copy()
    # values[indices] = np.nan

    masks = ~np.isnan(values)

    eval_masks = (~np.isnan(values)) ^ (~np.isnan(evals))

    evals = evals.reshape(shp)
    values = values.reshape(shp)

    masks = masks.reshape(shp)
    eval_masks = eval_masks.reshape(shp)
    label = y.tolist() #out.loc[int(id_)]
    # print(f'rec y: {list(y)}')
    rec = {'label': label}

    # prepare the model for both directions
    rec['forward'] = parse_rec(values, masks, evals, eval_masks, dir_='forward')
    rec['backward'] = parse_rec(values[::-1], masks[::-1], evals[::-1], eval_masks[::-1], dir_='backward')
    # for key in rec.keys():
    #     print(f"{key}: {type(rec[key])}")# and {rec[key].shape}")
    rec = json.dumps(rec)

    fs.write(rec + '\n')

train_df = pd.read_csv("ColdHardiness_Grape_Merlot_2.csv")
print('Now train')
train_modified_df, train_dormant_seasons = preprocess_missing_values(train_df, is_dormant=False, is_year=True)
train_season_df, train_season_array, train_max_length = get_seasons_data(train_modified_df, train_dormant_seasons, is_dormant=False, is_year=True)
train_season_df = train_season_df.drop(train_season_array[-1], axis=0)
train_season_df = train_season_df.drop(train_season_array[-2], axis=0)
train_season_df = train_season_df.drop(train_season_array[-3], axis=0)
# train_season_df = train_season_df.drop(train_season_array[-4], axis=0)
mean, std = get_mean_std(train_season_df, features)

normalized_season_df = train_season_df[features].copy()
# print(f"norm: {normalized_season_df.shape}, mean: {mean.shape}, std: {std.shape}")
normalized_season_df = (normalized_season_df - mean) /std

mice_impute = IterativeImputer(random_state=0, max_iter=20)
mice_impute.fit(normalized_season_df[features])

print('Now test')
test_df = pd.read_csv("ColdHardiness_Grape_Merlot_2.csv")
modified_df, dormant_seasons = preprocess_missing_values(test_df, is_dormant=False, is_year=True)
season_df, season_array, max_length = get_seasons_data(modified_df, dormant_seasons, is_dormant=False, is_year=True)
# print('season array: ', len(season_array), '\n', season_array)

test_normalized_df = season_df[features].copy()
test_normalized_df = (test_normalized_df - mean) /std
test_imputed_mice = mice_impute.transform(test_normalized_df[features])
test_imputed = unnormalize(test_imputed_mice, mean, std)

print(f"{test_df.iloc[11824]}")

mice_df = test_df.copy()
mice_df[features] = np.round(test_imputed, 2)
mice_df.iloc[11824:] = test_df.iloc[11824:]
mice_df.to_csv('ColdHardiness_Grape_Merlot_imputed_yearly_mice.csv', index=False)

X, Y = split_XY(season_df, max_length, season_array)

print('X: ', X.shape)
zero_pads = []
for i in range(X.shape[0]):
    indices = np.where(~X[i].any(axis=1))[0]
    # print('zero pads: ', indices, '\n\n')
    zero_pads.append(indices)
    parse_id(X[i], Y[i])
fs.close()

model_brits = BRITS(rnn_hid_size=RNN_HID_SIZE, impute_weight=IMPUTE_WEIGHT, label_weight=LABEL_WEIGHT)

if os.path.exists('./model_BRITS.model'):
    model_brits.load_state_dict(torch.load('./model_BRITS.model'))

model_brits.eval()

print('season array loader: ', len(season_array))
val_iter = data_loader.get_loader(batch_size=len(season_array), filename=folder + 'json', shuffle=False)

imputed_array_brits = None
for idx, data in enumerate(val_iter):
    data = utils.to_var(data)
    ret = model_brits.run_on_batch(data, None)
    eval_ = ret['evals'].data.cpu().numpy()
    print('idx: ', idx, ' eval: ', eval_.shape)
    imputation_brits = ret['imputations'].data.cpu().numpy()
    imputation_brits = unnormalize(imputation_brits, mean, std)
    print(f"brits imputed seasons: {len(imputation_brits)}")
    for i in range(imputation_brits.shape[0]):
        # print(imputation_brits[i])
        without_paddings = np.delete(imputation_brits[i], zero_pads[i], 0)
        if imputed_array_brits is None:
            imputed_array_brits = np.round(without_paddings, 2)
        else:
            imputed_array_brits = np.concatenate((imputed_array_brits, np.round(without_paddings, 2)), axis=0)
brits_df = test_df.copy()
brits_df[features] = imputed_array_brits
brits_df.iloc[11824:] = test_df.iloc[11824:]
print(f"test: {test_df.iloc[12088]}")
print(f"brits: {brits_df.iloc[12088]}")
brits_df.to_csv('ColdHardiness_Grape_Merlot_imputed_yearly_brits.csv', index=False)

# evals = np.array(evals)
# imputations_brits = np.array(imputations_brits)

# imputations_brits_I = np.array(imputations_brits_I)
# print("imp len: ", len(imputations_brits))
# result_dict = {
#     # 'knn': X_knn_imputed,
#     'brits': imputations_brits,
#     'brits_I': imputations_brits_I
# }

# limit = 100
# start = 0

# for i in range(int(len(imputations_brits)/limit)):
#     end = start + limit + 1
#     result_dict = {
#         'brits': imputations_brits[start:end],
#         'brits_I': imputations_brits_I[start:end]
#     }
#     _graph_bar_diff_multi(evals[start:end], result_dict, "Difference of the different imputations from GT", np.arange(len(imputations_brits[start:end])), "# of missing values from observed data", "imputed value difference form GT", i)
#     start = end

# start = 0

# for i in range(int(len(imputations_brits)/limit)):
#     end = start+limit+1
#     x_range = np.arange(len(imputations_brits[start:end]))
#     plt.figure(figsize=(16,9))
#     plt.plot(x_range, imputations_brits[start:end], 'r', label='BRITS')
#     plt.plot(x_range, imputations_brits_I[start:end], 'b', label='BRITS_I')
#     plt.plot(x_range, evals[start:end], 'g', label='Ground Truth')
#     plt.title('GT vs Bidirectional RNN based imputations')
#     plt.xlabel('# of missing values from observed data')
#     plt.ylabel('Actual/imputed values (normalized)')
#     plt.legend()
#     plt.savefig(f'plot_imgs/gt-imputed-{i}.png', dpi=300)
#     plt.close()
#     start = end


def solve(index):
    global answer, arr
    if index == len(num):
        temp = [0] + arr + [0]
        SUM = 0
        for i in range(len(temp) - 1):
            SUM += MAP[temp[i]][temp[i + 1]]
        answer = min(answer, SUM)
        return
    for i in range(len(num)):
        if not visited[num[i]]:
            arr.append(num[i])
            visited[num[i]] = True
            solve(index + 1)
            arr.pop()
            visited[num[i]] = False

TC = int(input())
for tc in range(TC):
    N = int(input())
    MAP = [list(map(int, input().split())) for _ in range(N)]
    num = range(1, N)
    visited = [False for _ in range(N)]
    arr = []
    answer = 0xfffff
    solve(0)
    print('#{} {}'.format(tc + 1, answer))


import numpy as np
import matplotlib.pyplot as plt
from waterfilling import divide_spectrum, power_allocation, sort_steps, rearrange_powers
from modulators.piM_DPSK import PiMDPSK
from ber_controller import create_modulators, get_curves, ber_controller
from bandpass import temporal_bandpass_filter

# Old implementation

def encode_data(modulators, data, return_psd=True):
    signals = []
    for modulator in modulators:
        signals.append(modulator.modulate(data))
    signal = np.zeros(len(signals[0]))

    for s in signals:
        signal += s
    psds = []
    if return_psd:
        for s in signals:
            psd = np.abs(np.fft.fft(s))[:int(len(s)/2)]
            psd /= np.max(psd)
            psds.append(psd)
        return signal, psds
    else:
        return signal

def decode_signal(modulators, carrier_frequencies, signal):
    data = []
    for mod in modulators:
        data.append(mod.demodulate(signal))
    return data

def ber(data_in, data_out):
    bers = []
    for data in data_out:
        num_errors = np.sum(np.abs(np.subtract(np.array(data_in), np.array(data))))
        bers.append(num_errors / len(data))  # bit error rate
    return bers

class Multicarrier:
    """Implements multi-carrier modulation by combining the single-carrier
       modulator classes"""
    def __init__(self, bandwidth, num_carriers, symbol_rate, fs, power_budget, frequency_axis, noise_spectrum, transfer_function):
        self.bandwidth = bandwidth
        self.num_carriers = num_carriers
        self.symbol_rate = symbol_rate
        self.fs = fs
        self.mod = []

        self.power_budget = power_budget
        self.f = frequency_axis
        self.n = noise_spectrum
        self.h = transfer_function
        self.subcarrier_powers()
        self.subcarrier_modulators()
        self.subcarrier_bandwidth = bandwidth/num_carriers

        self.modulators = []
        self.rates = []
        filenames = ['plotters/data/pi4dpsk_j5.csv', 'plotters/data/pi4dpsk.csv',
                     'plotters/data/pi8dpsk_j5.csv', 'plotters/data/pi8dpsk.csv',
                     'plotters/data/pi16dpsk_j5.csv', 'plotters/data/pi16dpsk.csv',
                     'plotters/data/pi32dpsk_j5.csv', 'plotters/data/pi32dpsk.csv']
        self.curves = get_curves(filenames)

    def subcarrier_powers(self):
        self.channel_noise_ratio = self.h/self.n
        inverse_cnr = 1/self.channel_noise_ratio
        self.f_steps, self.inverse_cnr_steps = divide_spectrum(self.f, inverse_cnr, self.num_carriers)
        sorted_step_depths, indices = sort_steps(self.inverse_cnr_steps)
        self.power_levels = power_allocation(self.power_budget, sorted_step_depths)
        self.power_levels = rearrange_powers(self.power_levels, indices)
        #self.power_levels = self.power_budget/self.num_carriers*np.ones(self.num_carriers)

    def subcarrier_modulators(self):
        for k, fr in enumerate(self.f_steps):
            carrier_amplitude = np.sqrt(2 * self.power_levels[k])
            self.mod.append(create_modulators(self.symbol_rate, fr, carrier_amplitude, self.fs))

    def modulate_packet(self, data, eb_n0_per_band, packet_duration):
        mods = []
        data_per_carrier = []
        s = []
        for j, eb_n0 in enumerate(eb_n0_per_band):
            mod_idx = ber_controller(eb_n0, 10**(-6), self.curves)  # select modulation level
            mods.append(self.mod[j][mod_idx])
        for j, mod in enumerate(mods):
            effective_rate = mod.data_rate
            if mod.coding:
                effective_rate *= (26/31)
            data_size = int(effective_rate*packet_duration)//1560 * 1560
            if len(data) >= data_size:
                subcarrier_data = data[:data_size]
                data_per_carrier.append(subcarrier_data)
                s.append(mod.modulate(subcarrier_data))
                data = data[data_size:]
        signal = np.zeros(np.max([len(s_i) for s_i in s]))
        for j, s_i in enumerate(s):
            signal[:len(s_i)] += s_i
            signal_power = mods[j].carrier_amplitude**2/2
            bit_energy = signal_power/mods[j].data_rate
            n0 = bit_energy/eb_n0_per_band[j]
            noise = np.sqrt(n0 * mods[j].fs / 2) * np.random.standard_normal(len(s_i))
            carrier_spacing = self.bandwidth/self.num_carriers
            in_band_noise = temporal_bandpass_filter(noise, mods[j].fs, mods[j].carrier_frequency-carrier_spacing/2, mods[j].carrier_frequency+carrier_spacing/2)
            plt.plot(np.fft.fft(in_band_noise))
            signal[:len(s_i)] += in_band_noise
        plt.title('noise')
        plt.show()

        return signal, data, data_per_carrier, mods

    def demodulate_packet(self, signal, mods):
        data_per_carrier = []
        for mod in mods:
            detected_data = mod.demodulate(signal)
            data_per_carrier.append(detected_data)
        return data_per_carrier

if __name__ == '__main__':
    datarate = 50000                # data rate [bits/s]
    fs = 1000000                    # ADC/DAC sampling frequency [Hz]
    mod_index = 1                   # modulation index
    m = 32
    P = 50
    number_of_bits = 20000

    total_bandwidth = 200000
    num_carriers = 20

    f = np.linspace(0, total_bandwidth, 2*total_bandwidth+1)
    h = np.abs(1/(1 + 1j*(f**2)/20000/10**6))
    spec = 1.5+0.05*np.random.randn(len(f)) + 10000/(f+2000) + f**2/10**6

    bits = np.random.randint(2, size=number_of_bits)  # array of data bits to be sent

    bandwidth_per_carrier = total_bandwidth/num_carriers
    carrier_frequencies = np.linspace(bandwidth_per_carrier/2, total_bandwidth-bandwidth_per_carrier/2, num_carriers)
    f_bands, noise_bands = divide_spectrum(f, spec, num_carriers)
    f_bands, h_bands = divide_spectrum(f, h, num_carriers)
    inverse_cnr = 1 / h_bands
    power_levels = power_allocation(P, inverse_cnr)

    modulators = []
    for i, f in enumerate(carrier_frequencies):
        carrier_amplitude = np.sqrt(2*power_levels[i])
        modulators.append(PiMDPSK(datarate, f, carrier_amplitude, fs, mod_index, m))

    signal, psds = encode_data(modulators, bits)
    signal = np.clip(signal, -20, 20)
    signal_psd = np.abs(np.fft.fft(signal))
    signal_psd /= np.max(signal_psd)
    f_axis = np.linspace(0, modulators[0].fs/2, int(len(signal)/2))
    plt.figure(figsize=(6, 3))
    for psd in psds:
        plt.plot(f_axis/1000, 10*np.log10(psd), alpha=0.5)
    plt.xlim([0, 200])
    plt.title("PSD of OFDM-carriers")
    plt.xlabel("Frequency [kHz]")
    plt.ylabel("Signal energy [dB]")
    plt.ylim([-20, 5])
    plt.tight_layout()
    plt.savefig("plotters/plots/multicarrier.pdf", bbox_inches='tight', dpi=600)
    plt.show()
    data_decoded = decode_signal(modulators, carrier_frequencies, signal)

    bit_errors = ber(bits, data_decoded)
    print(bit_errors)


from ws.rsspull.main import rsspull
import os
import pkg_resources
import shutil

confdir = 'work'
if not os.path.exists(confdir):
    os.mkdir(confdir)
maildir = os.path.join(confdir, 'Maildir')
if not os.path.exists(maildir):
    os.mkdir(maildir)
shutil.copy(pkg_resources.resource_filename(
    'ws.rsspull.tests', 'fixture/feeds.opml'), confdir)
shutil.copy(pkg_resources.resource_filename(
    'ws.rsspull.tests', 'fixture/config'), confdir)
rsspull(confdir)


import sys
from heapq import heappush, heappop
sys.stdin = open("input.txt", "r")
input = sys.stdin.readline

lt, rt = [], []
n = int(input())
for _ in range(n):
    x = int(input())
    if len(lt) == len(rt):
        heappush(lt, -x)
    else:
        heappush(rt, x)
    if rt and -lt[0] > rt[0]:
        l, r = -heappop(lt), heappop(rt)
        heappush(rt, l)
        heappush(lt, -r)
    print(-lt[0])


from setuptools import setup, find_packages

from os import path
this_directory = path.abspath(path.dirname(__file__))
with open(path.join(this_directory, 'README.md'), encoding='utf-8') as f:
    long_description = f.read()

setup(name='brain-pred-toolbox',
      version='2.3.2',
      long_description=long_description,
      long_description_content_type='text/markdown',
      description='The Brain Predictability toolbox (BPt) is a ' +
      'Python based machine learning library designed to work with ' +
      'a range of neuroimaging data. Warning: Not actively maintained ' +
      'as of 11/30/22.',
      url='http://github.com/sahahn/BPt',
      author='Sage Hahn',
      author_email='sahahn@euvm.edu',
      license='MIT',
      packages=find_packages(),
      python_requires=">=3.8",
      install_requires=[
          'scikit-learn>=1.1.0,<1.1.4',
          'numpy>=1.21,<1.24',
          'scipy>=1.2,<2.0',
          'pandas>=1.1.5,<1.6',
          'matplotlib>=3.2.2,<3.7',
          'seaborn>=0.9,<0.13',
          'scikit-image>=0.16',
          'tqdm>=4.51',
          'nevergrad>=0.5.0,<0.6',
          'Ipython',
          'joblib>=1,<2',
          'loky',
          'threadpoolctl>=3'
      ],
      extras_require={
          'extra': ['lightgbm>=3.3.0',
                    'nilearn>=0.9',
                    'python-docx',
                    'mvlearn',
                    'imblearn',
                    'xgboost>=1.4',
                    'bp-neurotools'],
       },
      test_suite='pytest',
      tests_require=['pytest', 'coverage'],
      zip_safe=False)


# Keithley_2230.py driver for Keithley 2230 power supply
#
# Sam Hile <samhile@gmail.com>
# Daniel Widmann <>
# Gabriele de Boo <ggdeboo@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

from instrument import Instrument
import visa
import types
import logging
import numpy
import re

import qt

def bool_to_str(val):
    '''
    Function to convert boolean to 'ON' or 'OFF'
    '''
    if val == True:
        return "ON"
    else:
        return "OFF"

class Keithley_2230(Instrument):
    '''
    This is the driver for the Keithley 2230 Multimeter

    Usage:
    Initialize with
    <name> = instruments.create('<name>', 'Keithley_2230',
        address='<GBIP address>',
        reset=<bool>)
    '''

    def __init__(self, name, address, reset=False):
        '''
        Initializes the Keithley_2230, and communicates with the wrapper.

        Input:
            name (string)           : name of the instrument
            address (string)        : GPIB address
            reset (bool)            : resets to default values
        Output:
            None
        '''
        # Initialize wrapper functions
        logging.info('Initializing instrument Keithley_2230')
        Instrument.__init__(self, name, tags=['physical', 'measure'])

        # Add some global constants
        self._address = address
        self._visainstrument = visa.instrument(self._address)

        # Add parameters to wrapper
        self.add_parameter('voltage',
            flags=Instrument.FLAG_GETSET,
            units='V', minval=0, maxval=30, type=types.FloatType,
            channels=(1,2,3))
        self.add_parameter('voltage_reading',
            flags=Instrument.FLAG_GET,
            units='V', minval=0, maxval=30, type=types.FloatType,
            channels=(1,2,3))
        self.add_parameter('current',
            flags=Instrument.FLAG_GETSET,
            units='A', minval=0, maxval=5, type=types.FloatType,
            channels=(1,2,3))
        self.add_parameter('current_reading',
            flags=Instrument.FLAG_GET,
            units='A', minval=0, maxval=5, type=types.FloatType,
            channels=(1,2,3))
        self.add_parameter('active',
            flags=Instrument.FLAG_GETSET,
            type=types.BooleanType, channels=(1,2,3))
#        self.add_parameter('remote_status',
#            flags=Instrument.FLAG_GETSET,
#            type=types.BooleanType)
        # Add functions to wrapper

        self.set_parameter_bounds('voltage3', 0, 6)
        self.set_parameter_bounds('current1', 0, 1.5)
        self.set_parameter_bounds('current2', 0, 1.5)

        self.add_function('reset')
        self.add_function('get_all')
        self.add_function('set_remote')

        if reset:
            self.reset()
        else:
            pass#self.get_all()

# --------------------------------------
#           functions
# --------------------------------------

    def reset(self):
        '''
        Resets instrument to default values

        Input:
            None

        Output:
            None
        '''
        logging.debug('Resetting instrument')
        self._visainstrument.write('*RST')
        self.get_all()

    def get_all(self):
        '''
        Reads all relevant parameters from instrument

        Input:
            None

        Output:
            None
        '''
        logging.info('Get all relevant data from device')
        self.get_active1()
        self.get_active2()
        self.get_active3()
        self.get_voltage1()
        self.get_voltage2()
        self.get_voltage3()
        self.get_voltage_reading1()
        self.get_voltage_reading2()
        self.get_voltage_reading3()
        self.get_current_reading1()
        self.get_current_reading2()
        self.get_current_reading3()

########

    def do_get_voltage(self,channel):
        '''
        Get voltage for a specified channel
            in: ch - channel number
            out: voltage [Volts]
        '''
        logging.debug('Getting voltage from instrument')
        self._visainstrument.write('INST:SEL CH%d' % channel)
        return self._visainstrument.ask('SOUR:VOLTAGE?')

    def do_get_voltage_reading(self,channel):
        '''
        Get voltage for a specified channel
            in: ch - channel number
            out: voltage [Volts]
        '''
        logging.debug('Getting voltage from instrument')
        return self._visainstrument.ask('MEAS:VOLT? CH%d' % channel)

    def do_set_voltage(self,v,channel):
        '''
        Get voltage for a specified channel
            in: ch - channel number
                v - voltage [Volts]
            out: none
        '''
        logging.debug('Sending voltage to instrument')
        self._visainstrument.write('INST:SEL CH%d' % channel)
        self._visainstrument.write('SOUR:VOLT %.3fV' % v)
#        self._visainstrument.write('SOUR:APP CH%d,%fV' % (channel,v))
        return

    def do_get_current(self, channel):
        '''
        Get current for a specified channel
            in: ch - channel number
            out: current [A]
        '''
        logging.debug(__name__ + ': Getting current from instrument')
        self._visainstrument.write('INST:SEL CH%d' % channel)
        return self._visainstrument.ask('SOUR:CURR?')

    def do_set_current(self, I, channel):
        '''
        Set current for a specified channel
            in: ch - channel number
            out: current [A]
        '''
        logging.debug(__name__ + ': Setting current to %.3f' % I)
        self._visainstrument.write('INST:SEL CH%d' % channel)
        self._visainstrument.write('SOUR:CURR %.3fA' % I)
        return

    def do_get_current_reading(self, channel):
        '''
        Get current reading for a specified channel
            in: ch - channel number
            out:
        '''
        logging.debug(__name__ + ': Getting current from instrument')
        return self._visainstrument.ask('MEAS:CURR? CH%d' % channel)

    def do_get_active(self,channel):
        '''
        Get voltage for a specified channel
            in: ch - channel number
            out: active_status [Boolean]
        '''
        logging.debug('Getting active status from instrument')
        self._visainstrument.write('INST:SEL CH%d' % channel)
        answer = self._visainstrument.ask('CHAN:OUTP?')
        if answer == '1':
            return True
        elif answer =='0':
            return False
        else:
            logging.warning(__name__ + ': responded with unexpected answer: %s' % answer)

    def do_set_active(self,a,channel):
        '''
        Get voltage for a specified channel
            in: ch - channel number
                a - active_status [Boolean]
            out: none
        '''
        logging.debug('Sending active status to instrument')
        self._visainstrument.write('INST:SEL CH%d' % channel)
        self._visainstrument.write('CHAN:OUTP %s' % bool_to_str(a))
        return

    def set_remote(self):
        self._visainstrument.write('SYST:REM')


from cs50 import SQL
from flask import Flask, flash, redirect, render_template, request, session
from flask_session import Session
from tempfile import mkdtemp
from werkzeug.security import check_password_hash, generate_password_hash
from helper import apology, login_required, calculate_distance, lookup

# Configure application
app = Flask(__name__)

# Configure session to use filesystem (instead of signed cookies)
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)

@app.after_request
def after_request(response):
    """Ensure responses aren't cached"""
    response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
    response.headers["Expires"] = 0
    response.headers["Pragma"] = "no-cache"
    return response

# Configure CS50 Library to use SQLite database
db = SQL("sqlite:///balloon.db")

@app.route("/")
@login_required
def index():
    """Show portfolio of stocks"""
    rows = db.execute("select * from users where id=?" , session.get("user_id"))
    username  = rows[0]["username"]
    cash=rows[0]["caapital"]
    size = rows[0]["size"]

    return render_template("index.html" , username=username,cash=cash, size=size)

@app.route("/select" , methods=["POST" , "GET"])
@login_required
def select():
    if request.method == "POST":
        fcity = request.form["from"]
        tcity = request.form["to"]

        if lookup(fcity) is None:
            return apology("must provide source city" , 400)
        if lookup(tcity) is None:
            return apology("must provide destination city" , 400)

        session["fcity"] = fcity
        session["tcity"] = tcity

        rows = db.execute("Select * from users where id=?" , session.get("user_id"))
        size = rows[0]["size"]

        #Calculate distance and cost between start city and selected city
        distance = calculate_distance(float(lookup(fcity)['latitude']), float(lookup(fcity)['longitude']),
                                       float(lookup(tcity)['latitude']), float(lookup(tcity)['longitude']))
        session["distance"] = distance

        if size == "small":
            if distance > 300:
                return render_template("between.html", f=fcity, t=tcity)
            else:
                old_cash = db.execute("select caapital from users where id=?" , session.get("user_id"))[0]["caapital"]
                new_cash = (int)(old_cash) - (int)(distance)
                if new_cash < 0:
                    return render_template("cash.html")
                db.execute('insert into history ("from", "to", distance, capital, size, user_id) values(?,?,?,?,?,?)' , fcity, tcity, (int)(distance), (int)(distance), size, session.get("user_id"))
                db.execute("update users set caapital=? where id=?" , new_cash , session.get("user_id"))
                return redirect("/")

        if size == "big":
            if distance > 700:
                return render_template("/between.html", f=fcity, t=tcity)
            else:
                old_cash = db.execute("select caapital from users where id=?" , session.get("user_id"))[0]["caapital"]
                new_cash = (int)(old_cash) - (int)(distance)
                if new_cash < 0:
                    return render_template("cash.html")
                db.execute('insert into history ("from", "to", distance, capital, size, user_id) values(?,?,?,?,?,?)' , fcity, tcity, (int)(distance), (int)(distance), size, session.get("user_id"))
                db.execute("update users set caapital=? where id=?" , new_cash , session.get("user_id"))
                return redirect("/")

        return redirect("/")
    else:
        return render_template("select.html")

@app.route("/between" , methods=["POST" , "GET"])
@login_required
def between():
    if request.method == "POST":
        between = request.form["between"]

        if lookup(between) is None:
            return apology("must provide between city" , 400)

        distance1 = calculate_distance(float(lookup(session["fcity"])['latitude']), float(lookup(session["fcity"])['longitude']),
                                        float(lookup(between)['latitude']), float(lookup(between)['longitude']))

        distance2 = calculate_distance(float(lookup(between)['latitude']), float(lookup(between)['longitude']),
                                        float(lookup(session["fcity"])['latitude']), float(lookup(session["fcity"])['longitude']))

        if distance1 > session["distance"]:
            return render_template("between.html", f=session["fcity"], t=session["tcity"], message="That city is farther than destination city. Choose another one")
        else:
            rows = db.execute("Select * from users where id=?" , session.get("user_id"))
            size = rows[0]["size"]

            if size == "small":
                if distance1 < 300:
                    if distance2 < 300:
                        old_cash = db.execute("select caapital from users where id=?" , session.get("user_id"))[0]["caapital"]
                        new_cash = (int)(session["distance"]) + 10
                        if new_cash > old_cash:
                            return render_template("cash.html")
                        db.execute('insert into history ("from", "to", distance, capital, size, user_id) values(?,?,?,?,?,?)' , session["fcity"], session["tcity"],(int)(session["distance"]), new_cash, size, (int)(session.get("user_id")))
                        db.execute("update users set caapital=? where id=?" , old_cash - new_cash , session.get("user_id"))
                        return redirect("/")
                    else:
                        return render_template("between.html" , f=between , t=session["tcity"] , message="Next parts are harder, i couldn't find a way to get all data from that API to show, sorry for my easy submission")
                else:
                    return render_template("between.html" , f=between , t=session["tcity"] , message="Next parts are harder (if i randomly will take Almaty->New York) , it takes a long time to do , sorry for my easy submission")

            if size == "big":
                if distance1 < 700:
                    if distance2 < 700:
                        old_cash = db.execute("select caapital from users where id=?" , session.get("user_id"))[0]["caapital"]
                        new_cash = (int)(session["distance"]) + 10
                        if new_cash > old_cash:
                            return render_template("cash.html")
                        db.execute('insert into history ("from", "to", distance, capital, size, user_id) values(?,?,?,?,?,?)' , session["fcity"], session["tcity"],(int)(session["distance"]), new_cash, size, (int)(session.get("user_id")))
                        db.execute("update users set caapital=? where id=?" , old_cash - new_cash , session.get("user_id"))
                        return redirect("/")
                    else:
                        return render_template("between.html" , f=between , t=session["tcity"] , message="Next parts are harder, i couldn't find a way to get all data from that API to show, sorry for my easy submission")
                else:
                    return render_template("between.html" , f=between , t=session["tcity"] , message="Next parts are harder (if i randomly will take Almaty->New York) , it takes a long time to do , sorry for my easy submission")

    else:
        return render_template("between.html")

@app.route("/cash" ,methods=["POST" , "GET"])
@login_required
def cash():
    if request.method == "POST":
        cash = request.form["amount"]
        old_cash = db.execute("select caapital from users where id=?" , session.get("user_id"))[0]["caapital"]
        new_cash = (int)(old_cash) + (int)(cash)
        db.execute("update users set caapital=? where id=?" , new_cash , session.get("user_id"))
        return render_template("select.html")
    else:
        return render_template("cash.html")

@app.route("/history")
@login_required
def history():
    # Retrieve all transactions for the current user
    rows = db.execute("SELECT * FROM history WHERE user_id =?",session.get("user_id"))
    items = []

    items = [{
            'from' : "Source",
            'to' : "Destination",
            'distance' : "Distance" ,
            'capital' : "Capital",
            'size' : "Size"
    }]

    # Format the date and time of each transaction
    for row in rows:
        items.append(row)

    return render_template("history.html" , items=items)

@app.route("/register", methods=["POST", "GET"])
def register():
    if request.method == "POST":
        username = request.form["username"]
        passw = request.form["password"]
        conf = request.form["confirmation"]
        if passw != conf:
            return apology("passwords must match", 400)
        capital = request.form["capital"]
        size = request.form["balloon_size"]
        rows = db.execute("SELECT * FROM users WHERE username = :username",
                          username=username)

        # Ensure username is not already taken
        if len(rows) != 0:
            return apology("username already taken", 400)

        # Insert new user into database
        db.execute("INSERT INTO users (username, hash , caapital , size) VALUES (:username, :hash, :caapital, :size )",
                   username=request.form.get("username"),
                   hash=generate_password_hash(passw), caapital=capital, size=size)

        # # Redirect user to login page
        return redirect("/login")
    else:
        return render_template("register.html")

@app.route("/login", methods=["GET", "POST"])
def login():
    """Log user in"""

    # Forget any user_id
    session.clear()

    # User reached route via POST (as by submitting a form via POST)
    if request.method == "POST":

        # Query database for username
        rows = db.execute("SELECT * FROM users WHERE username = ?", request.form.get("username"))

        # Ensure username exists and password is correct
        if len(rows) != 1 or not check_password_hash(rows[0]["hash"], request.form.get("password")):
            return apology("invalid username and/or password" , 400)

        # Remember which user has logged in
        session["user_id"] = rows[0]["id"]
        session["username"] = rows[0]["username"]

        # Redirect user to home page
        return redirect("/")

    # User reached route via GET (as by clicking a link or via redirect)
    else:
        return render_template("login.html")

@app.route("/logout")
def logout():
    # Forget any user_id
    session.clear()

    # Redirect user to login form
    return redirect("/")


from django.conf.urls import url
from django.views.generic import TemplateView
from .views import IndexView, NotFoundView

app_name = 'personal'
urlpatterns = [
    url(
        r'^$',
        IndexView.as_view(),
        name='index'
    ),
    url(
        r'',
        NotFoundView.as_view(),
        name='notfound'
    )
]


from telegram.ext import Updater, CallbackContext, CommandHandler, MessageHandler, Filters, Handler
from telegram.ext.dispatcher import run_async, DispatcherHandlerStop, Dispatcher
from telegram import Update, User, Message, ParseMode
from telegram.error import BadRequest
import requests_html
import requests
import json
import logging

#Enter API-KEY here
updater = Updater("API-KEY", use_context=True)
dispatcher = updater.dispatcher

logging.basicConfig(filename="shipping.log", format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)

#Tracking Function for E-Kart Logistics
@run_async
def ekart(update: Update, context: CallbackContext):
    if update.message!=None:
        trackingID = (update.message.text).split()[1]
        data = []
        session = requests_html.HTMLSession()
        response = session.get("https://ekartlogistics.com/track/"+str(trackingID)+"/")
        for selector in response.html.xpath('//div[@id="no-more-tables"][1]/table/tbody'):
            data.append(selector.text)

        context.bot.send_message(chat_id=update.effective_chat.id, text="*Shipping Status: *\n\n`Latest Status: "+data[0]+"`\n\n*Tracking Info:*\n\n`"+data[1]+"`", reply_to_message_id=update.message.message_id, parse_mode=ParseMode.MARKDOWN)

#Tracking Function for Pitney Bowes
@run_async
def pitneyb(update: Update, context: CallbackContext):
    if update.message!=None:
        trackingID = (update.message.text).split()[1]
        response = requests.get("https://parceltracking.pb.com/ptsapi/track-packages/"+trackingID)
        jsonData = json.loads(response.text)
        try:
            currentStatusData = [
                'Status: '+jsonData['currentStatus']['packageStatus'],
                'Last Updated: '+jsonData['currentStatus']['eventDate']+' '+jsonData['currentStatus']['eventTime'],
                'Description: '+jsonData['currentStatus']['eventDescription'],
                'Location: '+jsonData['currentStatus']['eventLocation']['city']+", "+jsonData['currentStatus']['eventLocation']['countyOrRegion']+' - '+jsonData['currentStatus']['eventLocation']['postalOrZipCode']
            ]
        except KeyError:
            currentStatusData = [
                'Status: '+jsonData['currentStatus']['packageStatus'],
                'Last Updated: '+jsonData['currentStatus']['eventDate']+' '+jsonData['currentStatus']['eventTime'],
                'Description: '+jsonData['currentStatus']['eventDescription'],
                'Location: '+jsonData['currentStatus']['eventLocation']['city']+", "+jsonData['currentStatus']['eventLocation']['countyOrRegion']+' - '
            ]
        currentStatusData = "\n".join(currentStatusData)

        history = []
        for x in jsonData['scanHistory']['scanDetails']:
            try:
                history.append([
                'Status: '+x['packageStatus'],
                'Last Updated: '+x['eventDate']+' '+x['eventTime'],
                'Description: '+x['eventDescription'],
                'Location: '+x['eventLocation']['city']+", "+x['eventLocation']['countyOrRegion']+' - '+x['eventLocation']['postalOrZipCode']
            ])
            except KeyError:
                history.append([
                'Status: '+x['packageStatus'],
                'Last Updated: '+x['eventDate']+' '+x['eventTime'],
                'Description: '+x['eventDescription'],
            ])

        historyData = []
        for i in range(len(history)):
            historyData.append("\n".join(history[i]))

        historyData = "\n\n".join(historyData)

        context.bot.send_message(chat_id=update.effective_chat.id, text="*Shipping Status: *\n\n`Latest Status:\n"+currentStatusData+"`\n\n*Tracking Info:*\n\n`"+historyData+"`", reply_to_message_id=update.message.message_id, parse_mode=ParseMode.MARKDOWN)

#Tracking Function for Canada Post
@run_async
def canadapost(update: Update, context: CallbackContext):
    if update.message!=None:
        trackingID = (update.message.text).split()[1]
        response = requests.get("https://www.canadapost.ca/trackweb/rs/track/json/package/"+trackingID+"/detail")
        jsonData = json.loads(response.text)
        status = jsonData['status']

        history = []
        for x in jsonData['events']:
            history.append([
                'Date: '+ x['datetime']['date'] + x['datetime']['time'] + x['datetime']['zoneOffset'],
                'Location: '+ x['locationAddr']['city'] + ", " + x['locationAddr']['regionCd'] + " (" + x['locationAddr']['countryCd'] + ")",
                'Description: '+ x['descEn']
            ])
        currentStatusData = history[0]
        currentStatusData = "\n".join(currentStatusData)
        del history[0]

        historyData = []
        for i in range(len(history)):
            historyData.append("\n".join(history[i]))

        historyData = "\n\n".join(historyData)

        context.bot.send_message(chat_id=update.effective_chat.id, text="*Shipping Status: *\n\n`Latest Status:\n"+currentStatusData+"`\n\n*Tracking Info:*\n\n`"+historyData+"`", reply_to_message_id=update.message.message_id, parse_mode=ParseMode.MARKDOWN)

#Bot Start Message /start
@run_async
def start(update: Update, context: CallbackContext):
    context.bot.sendChatAction(update.effective_chat.id, "typing")
    cmd_msg = context.bot.send_message(chat_id=update.effective_chat.id, text="Hey there! I'm Shipping Info Bot!\nI can provide you latest tracking info on your package.\n\nUse the following commands to access your package tracking info.")

def main():

    start_handler = CommandHandler("start", start)
    dispatcher.add_handler(start_handler)

    #Command handler for E-Kart Logistics
    ekart_handler = CommandHandler("ekart", ekart)
    dispatcher.add_handler(ekart_handler)

    #Command handler for Pitney Bowes
    pitneyb_handler = CommandHandler("pitneyb", pitneyb)
    dispatcher.add_handler(pitneyb_handler)

    #Command handler for Canada Post
    canadapost_handler = CommandHandler("canadapost", canadapost)
    dispatcher.add_handler(canadapost_handler)

    updater.start_polling()
    updater.idle()

if __name__ == "__main__":

    print(" _____ _     _             _            _____       __     ______       _   \n")
    print("/  ___| |   (_)           (_)          |_   _|     / _|    | ___ \     | |  \n")
    print("\ `--.| |__  _ _ __  _ __  _ _ __   __ _ | | _ __ | |_ ___ | |_/ / ___ | |_ \n")
    print(" `--. \ '_ \| | '_ \| '_ \| | '_ \ / _` || || '_ \|  _/ _ \| ___ \/ _ \| __|\n")
    print("/\__/ / | | | | |_) | |_) | | | | | (_| || || | | | || (_) | |_/ / (_) | |_ \n")
    print("\____/|_| |_|_| .__/| .__/|_|_| |_|\__, \___/_| |_|_| \___/\____/ \___/ \__|\n")
    print("              | |   | |             __/ |                                   \n")
    print("              |_|   |_|            |___/                                    ")

    main()


import Rhino
import rhinoscriptsyntax as rs
import time

#Load Grasshopper plugin as gh
gh = Rhino.RhinoApp.GetPlugInObject("Grasshopper")

### This is how you find and print methods in grasshopper
#for func in dir(gh):
#    if not func.startswith('_'): print func

#SetSliderValue("GUID", Number)
#BakeDataInObject("GUID")

rs.EnableRedraw(True)

#set working directory
workingDir = rs.BrowseForFolder(None, "Pick a folder to use as a working directory")
rs.WorkingFolder(workingDir)

#create variable for file naming
num = 0

for i in range(1,3,1):
    #BaseSize Slider = i
    gh.SetSliderValue("adf54977-006a-4877-9e67-81d736ff07de", i)
    for j in range(0,3,1):
        #Rotation Slider = j
        gh.SetSliderValue("061dd51d-3d85-4a32-8aaa-0e6ba809ed75", j)
        for k in range (3,5,1):
            #Number of Sides Slider = k
            gh.SetSliderValue("b46e0b0e-643d-4b3a-8a96-c6d4e120e4dd", k)
            gh.RunSolver("AutoTower.gh")
            baked = gh.BakeDataInObject("72628cf1-020e-425d-9746-6f323f8882f8")
            transVect = (12*i, 12*j, 12*(k-2))
            rs.MoveObject(baked, transVect)

            #convert to string and add file name
            strNum = str(num)
            filename = "myfileTest" + strNum + ".3dm"

            #call a bunch of rhino cammands to do a selection and export
            rs.Command("_SelNone", True)
            rs.Command("_SelLast", True)
            rs.Command("_-Export " + filename + " _Enter", True)
            rs.Command("_SelNone", True)

            #incriment the file name

            num = num + 1

Python Tutorial Files/Automating a Tower Massing.py


import sys, os
sys.path.insert(1, os.path.split(os.path.split(sys.path[0])[0])[0])
import pickle as pkl
import numpy as np
from sklearn import preprocessing
from sklearn.utils import shuffle
from classes.neural_net import NeuralNetwork

def unpickle(file):
    fo = open(file, 'rb')
    dict = pkl.load(fo, encoding='latin1')
    fo.close()
    return dict

le = preprocessing.LabelEncoder()
le.classes_ = unpickle(sys.path[0] + '/batches.meta')['label_names']

train_images = None
train_labels = []

test_images = None
test_labels = []
for i in range(1, 6):
    data = unpickle(sys.path[0] + '/data_batch_'+str(i))
    if train_images is None:
        train_images = data['data']
    else:
        train_images = np.vstack((train_images, data['data']))

    train_labels += data['labels']

train_images = train_images.reshape(-1, 3, 32, 32)
train_images = train_images.astype(np.float128)

mean_image= np.mean(train_images, axis=0)
train_images -= mean_image
std = np.std(train_images, axis=0)
train_images /= std

train_images = train_images.astype(np.float32)

data = unpickle(sys.path[0] + '/test_batch')
test_images = data['data'].reshape(-1, 3, 32, 32)
test_images = test_images.astype(np.float128)

test_images -= mean_image
test_images /= std

test_images = test_images.astype(np.float32)

test_labels = data['labels']

lr = 1e-4
l2_reg = 8e-6
learning_rate_decay = np.float32(96e-2)
batch_size = 1

cnn = NeuralNetwork(train_images.shape[1:],
                    [
                        {'type': 'conv', 'k': 16, 'u_type': 'nag', 'f': 5, 's': 1, 'p': 2},
                        {'type': 'pool', 'method': 'average'},
                        {'type': 'conv', 'k': 20, 'u_type': 'nag', 'f': 5, 's': 1, 'p': 2},
                        {'type': 'pool', 'method': 'average'},
                        {'type': 'conv', 'k': 20, 'u_type': 'nag', 'f': 5, 's': 1, 'p': 2},
                        {'type': 'pool', 'method': 'average'},
                        {'type': 'output', 'k': len(le.classes_), 'u_type': 'adam'}
                    ]
                    , lr, l2_reg=l2_reg)

train_images, train_labels = shuffle(train_images, train_labels)
for i in range(60000000):
    start = i * batch_size % len(train_images)
    end = start + batch_size

    if start == 0 and i != 0:
        cnn.epoch_count += 1
        train_images, train_labels = shuffle(train_images, train_labels)
        print('{} epoch finish. learning rate is {}'.format(str(cnn.epoch_count), str(cnn.lr)))
        cnn.lr *= learning_rate_decay

        loss, acc = cnn.predict(train_images[:4000], train_labels[:4000])
        print('training acc:{}'.format(acc))
        print('training loss:{}'.format(loss))

        test_loss, test_acc = cnn.predict(test_images[:10000], test_labels[:10000])
        print('test acc:{}'.format(test_acc))
        print('test loss:{}'.format(test_loss))

    cnn.t += 1
    loss, acc = cnn.epoch(train_images[start:end], train_labels[start:end])

    # print(loss)
    # print(acc)


from __future__ import absolute_import
from __future__ import print_function
from __future__ import division

from six.moves import xrange

from tensorforce.core import Agent
from tensorforce.core.memories import ReplayMemory

class MemoryAgent(Agent):

    default_config = dict(
        batch_size=32,
        memory_capacity=1000000,
        update_frequency=4,
        first_update=10000,
        repeat_update=1
    )

    def __init__(self, config):
        config.default(MemoryAgent.default_config)
        super(MemoryAgent, self).__init__(config)

        self.batch_size = config.batch_size
        self.memory = ReplayMemory(capacity=config.memory_capacity, states_config=config.states, actions_config=config.actions)
        self.update_frequency = config.update_frequency
        self.first_update = config.first_update
        self.repeat_update = config.repeat_update

    def observe(self, state, action, reward, terminal):
        if self.unique_state:
            state = dict(state=state)
        if self.unique_action:
            action = dict(action=action)

        self.memory.add_experience(state=state, action=action, reward=reward, terminal=terminal, internal=self.internals)

        if self.timestep >= self.first_update and self.timestep % self.update_frequency == 0:
            for _ in xrange(self.repeat_update):
                batch = self.memory.get_batch(batch_size=self.batch_size)
                self.model.update(batch=batch)


import sys, time, os, pathlib, subprocess
from PyQt5 import QtGui, QtWidgets, QtCore

sys.path.append(str(pathlib.Path(__file__).resolve().parents[1]))
from assembling.saving import list_dayfolder, get_TSeries_folders
from assembling.tools import find_matching_CaImaging_data
from misc.folders import FOLDERS, python_path, python_path_suite2p_env
from Ca_imaging.preprocessing import PREPROCESSING_SETTINGS
from Ca_imaging.redcell_gui import run as RunRedCellGui
from Ca_imaging.zstack.cellID_gui import run as RunZstackGui

class MainWindow(QtWidgets.QMainWindow):

    def __init__(self, app,
                 args=None,
                 parent=None):
        """
        sampling in Hz
        """
        super(MainWindow, self).__init__()
        self.app, self.args, self.CHILDREN_PROCESSES = app, args, []
        self.folder=''

        self.setGeometry(350, 470, 300, 350)
        # adding a "quit" and "load" keyboard shortcuts
        self.quitSc = QtWidgets.QShortcut(QtGui.QKeySequence('Ctrl+Q'), self)
        self.quitSc.activated.connect(self.quit)
        self.loadSc = QtWidgets.QShortcut(QtGui.QKeySequence('Ctrl+O'), self)
        self.loadSc.activated.connect(self.load_imaging)

        self.setWindowTitle('Ca-Preprocessing')

        self.process_script = os.path.join(str(pathlib.Path(__file__).resolve().parents[1]),
                                           'Ca_imaging',  'preprocessing.py')

        HEIGHT = 0

        HEIGHT += 10
        QtWidgets.QLabel("Root Folder:", self).move(10, HEIGHT)
        self.folderI = QtWidgets.QComboBox(self)
        self.folderI.setMinimumWidth(150)
        self.folderI.move(100, HEIGHT)
        self.folderI.activated.connect(self.update_setting)
        self.folderI.addItems(FOLDERS.keys())

        HEIGHT += 40
        self.load = QtWidgets.QPushButton('Upload (set of) TSeries \u2b07', self)
        self.load.setFont(QtGui.QFont("Arial", 8, QtGui.QFont.Bold))
        self.load.clicked.connect(self.load_imaging)
        self.load.setMinimumWidth(200)
        self.load.move(50, HEIGHT)

        HEIGHT += 40
        QtWidgets.QLabel("=> Setting :", self).move(10, HEIGHT)
        self.cbc = QtWidgets.QComboBox(self)
        self.cbc.setMinimumWidth(150)
        self.cbc.move(100, HEIGHT)
        self.cbc.activated.connect(self.update_setting)
        self.cbc.addItems(['automated']+list(PREPROCESSING_SETTINGS.keys()))

        HEIGHT += 40
        QtWidgets.QLabel("   delay ?", self).move(10, HEIGHT)
        self.delayBox = QtWidgets.QComboBox(self)
        self.delayBox.setMinimumWidth(150)
        self.delayBox.move(100, HEIGHT)
        self.delayBox.addItems(['None', '10s', '10min', '30min',
                                '1h', '2h', '3h', '5h', '10h', '1d', '2d'])

        HEIGHT +=40
        self.gen = QtWidgets.QPushButton('-=- Run -=-', self)
        self.gen.setFont(QtGui.QFont("Arial", 8, QtGui.QFont.Bold))
        self.gen.clicked.connect(self.run)
        self.gen.setMinimumWidth(200)
        self.gen.move(50, HEIGHT)

        HEIGHT +=40
        self.gen = QtWidgets.QPushButton(' open Suite2p ', self)
        self.gen.setFont(QtGui.QFont("Arial", 8, QtGui.QFont.Bold))
        self.gen.clicked.connect(self.open_suite2p)
        self.gen.setMinimumWidth(200)
        self.gen.move(50, HEIGHT)

        HEIGHT +=40
        self.gen = QtWidgets.QPushButton('red-cell selection GUI ', self)
        self.gen.setFont(QtGui.QFont("Arial", 8, QtGui.QFont.Bold))
        self.gen.clicked.connect(self.red_cell_selection)
        self.gen.setMinimumWidth(200)
        self.gen.move(50, HEIGHT)

        HEIGHT +=40
        self.gen = QtWidgets.QPushButton('Zstack cell selection GUI ', self)
        self.gen.setFont(QtGui.QFont("Arial", 8, QtGui.QFont.Bold))
        self.gen.clicked.connect(self.zstack_selection)
        self.gen.setMinimumWidth(200)
        self.gen.move(50, HEIGHT)

        self.CMDS = []
        self.show()

    def update_setting(self):
        self.CMDS = []
        print('command set is reset !')

    def build_cmd(self, folder, key):
        # delay = ''
        # if self.delayBox.currentText()!='None':
        #     delay = 'sleep %s; ' % self.delayBox.currentText()
        return '%s %s --CaImaging_folder "%s" --setting_key %s -v' % (python_path_suite2p_env,
                                                                      self.process_script, folder, key)

    def find_suite2p_settings(self, folder):
        settings = None
        if self.cbc.currentText()=='automated':
            potential_settings = folder.split('-')[-2]
            if potential_settings in list(PREPROCESSING_SETTINGS.keys()):
                settings = potential_settings
                print('using "%s" setting for: %s' % (settings, folder))
            else:
                print('settings not found for', folder)
        else:
            settings = self.cbc.currentText()
        return settings

    def load_imaging(self):

        self.folder = QtWidgets.QFileDialog.getExistingDirectory(self,\
                                    "Choose datafolder",
                                    FOLDERS[self.folderI.currentText()])
        if self.folder!='':
            if ('TSeries' in str(self.folder)):
                settings = self.find_suite2p_settings(str(self.folder))
                if settings is not None:
                    self.CMDS.append(self.build_cmd(self.folder, self.cbc.currentText()))
                else:
                    print('settings note recognized !')
            else:
                folders = get_TSeries_folders(self.folder, limit_to_subdirectories=False)
                for f in folders:
                    settings = self.find_suite2p_settings(str(f))
                    if settings is not None:
                        self.CMDS.append(self.build_cmd(f, settings))

            for cmd in self.CMDS:
                print('"%s" added to command set' % cmd)

        if len(self.CMDS)==0:
            print('\n /!\ no "TSeries" folder found or added, set of command is empty ! \n')

    def run(self):
        if self.delayBox.currentText()!='None':
            # delaying the run by a delay
            delay = eval(self.delayBox.currentText().replace('s', '').replace('m', '*60').replace('h', '*3600').replace('d', '*86400'))
            print('sleeping for %is' % delay)
            time.sleep(delay)
            print('sleeping time over !')
            # ----------------------------------------------------
            self.CMDS = [] # we overwrite the previous commands
            # looping over folder after the delay (in case the transfer was done)
            folders = get_TSeries_folders(self.folder, limit_to_subdirectories=False)
            for f in folders:
                settings = self.find_suite2p_settings(str(f))
                if settings is not None:
                    self.CMDS.append(self.build_cmd(f, settings))
        elif len(self.CMDS)==0:
            print('\n /!\ set of command is empty /!\  \n')

        # running commands
        for cmd in self.CMDS:
            p = subprocess.Popen(cmd, shell=True)
            print('"%s" launched as a subprocess' % cmd)

    def open_suite2p(self):
        p = subprocess.Popen('%s -m suite2p' % python_path_suite2p_env,
                             shell=True,
                             stdout=subprocess.PIPE,
                             stderr=subprocess.STDOUT)

    def red_cell_selection(self):
        self.CHILDREN_PROCESSES.append(RunRedCellGui(self.app, self.args))

    def zstack_selection(self):
        self.CHILDREN_PROCESSES.append(RunZstackGui(self.app, self.args))

    def quit(self):
        QtWidgets.QApplication.quit()

def run(app, args=None, parent=None):
    return MainWindow(app,
                      args=args,
                      parent=parent)

if __name__=='__main__':
    app = QtWidgets.QApplication(sys.argv)
    main = run(app)
    sys.exit(app.exec_())


from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as _

from applications.enums import AdditionalInfoUserReason

def validate_additional_info_user_reasons(
    input_reasons_list, allow_empty_list=True
) -> None:
    """
    Validate input_reasons_list as a list of unique values from AdditionalInfoUserReason.
    The list may be empty only if allow_empty_list is True.

    :raises ValidationError: If input_reasons_list is not a list of unique values from
                             AdditionalInfoUserReason, or if allow_empty_list is False
                             and input_reasons_list is an empty list.
    """
    if not isinstance(input_reasons_list, list):
        raise ValidationError(
            _("additional_info_user_reasons must be a list, was %(type)s")
            % {"type": type(input_reasons_list)}
        )

    if input_reasons_list == [] and not allow_empty_list:
        raise ValidationError(
            "additional_info_user_reasons must be non-empty, was empty"
        )

    valid_reasons_set = set(AdditionalInfoUserReason.values)

    try:
        input_reasons_set = set(input_reasons_list)
    except TypeError:
        raise ValidationError(
            _("Invalid type input values for additional_info_user_reasons")
        )

    if not input_reasons_set.issubset(valid_reasons_set):
        raise ValidationError(
            _(
                "additional_info_user_reasons must contain only values from "
                "AdditionalInfoUserReason, contained invalid values %(invalid_values)s"
            )
            % {"invalid_values": sorted(input_reasons_set - valid_reasons_set)}
        )

    if len(input_reasons_list) != len(input_reasons_set):
        raise ValidationError(
            "additional_info_user_reasons must contain unique values, contained duplicates"
        )


"""Tests for Protocol API temperature module contexts."""
import pytest
from decoy import Decoy, matchers

from opentrons.legacy_broker import LegacyBroker
from opentrons.hardware_control.modules import TemperatureStatus
from opentrons.protocols.api_support.types import APIVersion
from opentrons.protocols.api_support.util import APIVersionError
from opentrons.protocol_api import MAX_SUPPORTED_VERSION, TemperatureModuleContext
from opentrons.protocol_api.core.common import ProtocolCore, TemperatureModuleCore
from opentrons.protocol_api.core.core_map import LoadedCoreMap

@pytest.fixture
def mock_core(decoy: Decoy) -> TemperatureModuleCore:
    """Get a mock module implementation core."""
    return decoy.mock(cls=TemperatureModuleCore)

@pytest.fixture
def mock_protocol_core(decoy: Decoy) -> ProtocolCore:
    """Get a mock protocol implementation core."""
    return decoy.mock(cls=ProtocolCore)

@pytest.fixture
def mock_core_map(decoy: Decoy) -> LoadedCoreMap:
    """Get a mock LoadedCoreMap."""
    return decoy.mock(cls=LoadedCoreMap)

@pytest.fixture
def mock_broker(decoy: Decoy) -> LegacyBroker:
    """Get a mock command message broker."""
    return decoy.mock(cls=LegacyBroker)

@pytest.fixture
def api_version() -> APIVersion:
    """Get an API version to apply to the interface."""
    return MAX_SUPPORTED_VERSION

@pytest.fixture
def subject(
    api_version: APIVersion,
    mock_core: TemperatureModuleCore,
    mock_protocol_core: ProtocolCore,
    mock_core_map: LoadedCoreMap,
    mock_broker: LegacyBroker,
) -> TemperatureModuleContext:
    """Get a temperature module context with its dependencies mocked out."""
    return TemperatureModuleContext(
        core=mock_core,
        protocol_core=mock_protocol_core,
        core_map=mock_core_map,
        broker=mock_broker,
        api_version=api_version,
    )

def test_set_temperature(
    decoy: Decoy,
    mock_core: TemperatureModuleCore,
    mock_broker: LegacyBroker,
    subject: TemperatureModuleContext,
) -> None:
    """It should set and wait for the temperature via the core."""
    subject.set_temperature(42.0)

    decoy.verify(
        mock_broker.publish(
            "command",
            matchers.DictMatching(
                {
                    "$": "before",
                    "name": "command.TEMPDECK_SET_TEMP",
                    "payload": matchers.DictMatching({"celsius": 42.0}),
                }
            ),
        ),
        mock_core.set_target_temperature(42.0),
        mock_core.wait_for_target_temperature(),
        mock_broker.publish(
            "command",
            matchers.DictMatching({"$": "after"}),
        ),
    )

@pytest.mark.parametrize("api_version", [APIVersion(2, 3)])
def test_start_set_temperature(
    decoy: Decoy,
    mock_core: TemperatureModuleCore,
    mock_broker: LegacyBroker,
    subject: TemperatureModuleContext,
) -> None:
    """It should set the target temperature via the core."""
    subject.start_set_temperature(42.0)

    decoy.verify(
        mock_broker.publish(
            "command",
            matchers.DictMatching(
                {
                    "$": "before",
                    "name": "command.TEMPDECK_SET_TEMP",
                    "payload": matchers.DictMatching({"celsius": 42.0}),
                }
            ),
        ),
        mock_core.set_target_temperature(42.0),
        mock_broker.publish("command", matchers.DictMatching({"$": "after"})),
    )

    decoy.verify(
        mock_core.wait_for_target_temperature(), ignore_extra_args=True, times=0
    )

@pytest.mark.parametrize("api_version", [APIVersion(2, 2)])
def test_start_set_temperature_api_version_low(
    decoy: Decoy, mock_broker: LegacyBroker, subject: TemperatureModuleContext
) -> None:
    """It should reject if API version is lower than 2.3."""
    with pytest.raises(APIVersionError) as exc_info:
        subject.start_set_temperature(42.0)

    decoy.verify(
        mock_broker.publish(
            "command",
            matchers.DictMatching({"$": "after", "error": exc_info.value}),
        ),
    )

@pytest.mark.parametrize("api_version", [APIVersion(2, 3)])
def test_await_temperature(
    decoy: Decoy,
    mock_core: TemperatureModuleCore,
    mock_broker: LegacyBroker,
    subject: TemperatureModuleContext,
) -> None:
    """It should wait for a specified target temperature."""
    subject.await_temperature(42.0)

    decoy.verify(
        mock_broker.publish(
            "command",
            matchers.DictMatching(
                {
                    "$": "before",
                    "name": "command.TEMPDECK_AWAIT_TEMP",
                    "payload": matchers.DictMatching({"celsius": 42.0}),
                }
            ),
        ),
        mock_core.wait_for_target_temperature(42.0),
        mock_broker.publish("command", matchers.DictMatching({"$": "after"})),
    )

@pytest.mark.parametrize("api_version", [APIVersion(2, 2)])
def test_await_temperature_api_version_low(
    decoy: Decoy, mock_broker: LegacyBroker, subject: TemperatureModuleContext
) -> None:
    """It should reject if API version is lower than 2.3."""
    with pytest.raises(APIVersionError) as exc_info:
        subject.await_temperature(42.0)

    decoy.verify(
        mock_broker.publish(
            "command",
            matchers.DictMatching({"$": "after", "error": exc_info.value}),
        ),
    )

def test_deactivate(
    decoy: Decoy,
    mock_core: TemperatureModuleCore,
    mock_broker: LegacyBroker,
    subject: TemperatureModuleContext,
) -> None:
    """It should deactivate the heater."""
    subject.deactivate()

    decoy.verify(
        mock_broker.publish(
            "command",
            matchers.DictMatching(
                {"$": "before", "name": "command.TEMPDECK_DEACTIVATE"}
            ),
        ),
        mock_core.deactivate(),
        mock_broker.publish("command", matchers.DictMatching({"$": "after"})),
    )

def test_get_current_temperature(
    decoy: Decoy, mock_core: TemperatureModuleCore, subject: TemperatureModuleContext
) -> None:
    """It should get the current temperature from the core."""
    decoy.when(mock_core.get_current_temperature()).then_return(42.0)

    result = subject.temperature

    assert result == 42.0

def test_get_target_temperature(
    decoy: Decoy, mock_core: TemperatureModuleCore, subject: TemperatureModuleContext
) -> None:
    """It should get the target temperature from the core."""
    decoy.when(mock_core.get_target_temperature()).then_return(42.0)

    result = subject.target

    assert result == 42.0

@pytest.mark.parametrize("api_version", [APIVersion(2, 3)])
def test_get_status(
    decoy: Decoy, mock_core: TemperatureModuleCore, subject: TemperatureModuleContext
) -> None:
    """It should get the target temperature from the core."""
    decoy.when(mock_core.get_status()).then_return(TemperatureStatus.HEATING)

    result = subject.status

    assert result == "heating"

@pytest.mark.parametrize("api_version", [APIVersion(2, 2)])
def test_get_status_version_low(subject: TemperatureModuleContext) -> None:
    """It should reject if API version is lower than 2.3."""
    with pytest.raises(APIVersionError):
        subject.status

def test_serial_number(
    decoy: Decoy,
    mock_core: TemperatureModuleCore,
    subject: TemperatureModuleContext,
) -> None:
    """It should get the module's unique serial number."""
    decoy.when(mock_core.get_serial_number()).then_return("abc-123")
    result = subject.serial_number
    assert result == "abc-123"


from django.conf import settings
from django.db import models
from django.contrib import admin
from django.urls import include, path, re_path
from drf_yasg import openapi
from drf_yasg.views import get_schema_view
from rest_framework import permissions
from attachments.views import media_view

if settings.DEBUG:
    docs_permissions = ()
else:
    docs_permissions = (permissions.IsAuthenticated,)

schema_view = get_schema_view(
   openapi.Info(
      title="OPEN API",
      default_version='v1'
   ),
   public=False,
   permission_classes=docs_permissions,
)

urlpatterns = [
    re_path(r'^swagger(?P<format>\.json|\.yaml)$',
            schema_view.without_ui(cache_timeout=0), name='schema-json'),
    path('docs/', schema_view.with_ui('swagger', cache_timeout=0),
         name='schema-swagger-ui'),
    path('admin/', admin.site.urls),
    path('api/attachments/', include('attachments.urls')),
    re_path(r'^media/attachments/(?P<path>.*)(/?)?', media_view),
    #path('accounts/', include('accounts.urls')),
]

if settings.DEBUG:
    from django.conf.urls.static import static
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


import atexit
from functools import partial
import os
from subprocess import check_call

from setuptools import setup
from setuptools.command.install import install

package_name = 'pbcmd'

def _post_install(inst=None):
    print('~~~ post-install ~~~')
    if inst is not None:
        install_aliases = os.path.join(inst.install_scripts, 'install_aliases.sh')
        print('post-install scripts: ', install_aliases)
        check_call(install_aliases)

class PostInstall(install):

    def __init__(self, *args, **kwargs):
        super(PostInstall, self).__init__(*args, **kwargs)
        atexit.register(partial(_post_install, self))

setup(
    name=package_name,
    version='0.0.0',
    packages=[package_name],
    data_files=[
        ('share/ament_index/resource_index/packages',
            ['resource/' + package_name]),
        ('share/' + package_name, ['package.xml']),
    ],
    install_requires=['setuptools'],
    zip_safe=True,
    maintainer='anderson',
    maintainer_email='anderson@mbari.org',
    description='replicates pbcmd for physical MBARI Power Buoy',
    license='Apache 2.0',
    tests_require=['pytest'],
    entry_points={
        'console_scripts': [
            'pbcmd = pbcmd.pbcmd:main'
        ],
    },
    scripts=[
        'scripts/install_aliases.sh',
        'scripts/uninstall_aliases.sh'
    ],
    cmdclass={'install': PostInstall}
)


import pymongo
from uncomtrade import comtrade_api
from uncomtrade.notification import notify
import time
import logging

logging.basicConfig(filename='com_trade.log', level=logging.DEBUG)

INTERVAL_BETWEEN_REQUESTS = 2
MINUTE = 60
MINUTES_BETWEEN_409_REQUESTS = 3

COMMODITY = "TOTAL"

client = pymongo.MongoClient()
COLLECTION = client.comtrade.total

country_dict = list(client.comtrade.country_codes.find({}, {"id": 1, "_id": 0}))
country_codes = [code["id"] for code in country_dict if "all" not in code["id"]]

VPN_STATUS = "on"  # !!!!! TURN THE FREAKING VPN OFF !!!!!!

LOGO = r"""
 #     # #     #     #####
 #     # ##    #    #     #  ####  #    # ##### #####    ##   #####  ######
 #     # # #   #    #       #    # ##  ##   #   #    #  #  #  #    # #
 #     # #  #  #    #       #    # # ## #   #   #    # #    # #    # #####
 #     # #   # #    #       #    # #    #   #   #####  ###### #    # #
 #     # #    ##    #     # #    # #    #   #   #   #  #    # #    # #
  #####  #     #     #####   ####  #    #   #   #    # #    # #####  ######

                                            (c) Luca Verginer
"""

print(LOGO)

def is_in_db(country_cd, com_code, year_check):
    return True if COLLECTION.find_one({"rtCode": int(country_cd), "cmdCode": com_code, "yr": year_check}) else False

for year in range(1975, 2015):
    logging.info("-*"*10)
    logging.info(msg="Starting Year: {year}".format(year=year))
    logging.info("-*"*10)
    i = len(country_codes)-1

    # i = START_POSITION
    while i >= 0:
        country = country_codes[i]
        if is_in_db(country, COMMODITY, year):
            logging.debug("already in DB, country: {country}, year: {year} at position, {pos}".format(
                country=country,
                year=year,
                pos=i)
            )
            i -= 1
            continue

        time.sleep(INTERVAL_BETWEEN_REQUESTS)

        json_data = comtrade_api.get_commodity_data(country, COMMODITY, year)

        if type(json_data) is list and json_data:
            try:
                COLLECTION.insert_many(json_data)
                logging.info(msg="inserted country: {country} at position {position} using insert_many".format(
                    country=country,
                    position=i)
                )
                i -= 1
            except pymongo.errors.BulkWriteError:
                for element in json_data:
                    COLLECTION.insert_one(element)
                logging.debug(msg="inserted country: {country} at position {position} using insert_one".format(
                    country=country,
                    position=i)
                )
                i -= 1
                continue
        elif type(json_data) is list and not json_data:
            res = COLLECTION.insert_one({"rtCode": int(country), "cmdCode": COMMODITY, "yr": year, "pfCode": "S1"})
            logging.debug("inserted empty entry in MongoDB at (Object_ID) {id} at position {pos}".format(id=str(res.inserted_id), pos=i))
            i -= 1
        elif json_data == 409:

            message = "409 code received at: {country} at position {position},sleeping for {sleep}".format(
                country=country,
                sleep=MINUTES_BETWEEN_409_REQUESTS,
                position=i)
            logging.warning(msg=message)

            notify(title="UN Comtrade Quota Limit reached",
                   subtitle="Please change proxy",
                   message=message,
                   sound='bell')

            if VPN_STATUS is "on":
                VPN_STATUS = comtrade_api.swithch_vpn("off")
            else:
                VPN_STATUS = comtrade_api.swithch_vpn("on")

            for minute in range(MINUTES_BETWEEN_409_REQUESTS):
                logging.debug("{minute} minute(s) of {max_min} passed.".format(
                    minute=minute,
                    max_min=MINUTES_BETWEEN_409_REQUESTS))
                time.sleep(MINUTE)

            logging.warning(msg="resuming requests")
            continue

        elif json_data == 500:
            logging.warning(msg="500 code received at: {country} at position {position}".format(
                country=country,
                position=i
            ))
            continue

        else:
            logging.debug(msg="skipped for reason: {reason}".format(reason=json_data))
            i -= 1
            continue


# 无序数组里有99个重复的数，范围1——100，缺失一个1-100的整数

def findLostNum(arr):
    sum = 0
    for i in range(1, 101):
        sum += i
    arrSum = 0
    for i in arr:
        arrSum += i
    return sum - arrSum

# 扩展1：一个无序数组里有若干个正整数，范围是1-100，其中99各个整数都是出现了偶数次
# 只有1个整数出现了奇数次，找到这个数
# 解法
# 异或：偶数次的数异或必为0
#       奇数次的数异或为自身

# 扩展2：一个无序数组里有若干个正整数，范围是1-100，其中98各个整数都是出现了偶数次
# 有2个整数出现了奇数次，找到这个2个数

def findNum(arr):
    xorRes = 0
    for i in arr:
        xorRes ^= i
    if xorRes == 0:
        return None, None
    separator = 1
    while xorRes & separator == 0:
        separator <<= 1
    resA = 0
    resB = 0
    for i in arr:
        if i & separator == 0:
            resA ^= i
        else:
            resB ^= i
    return resA, resB

def fibo(x):
    f0 = 0
    f1 = 1
    if x == 0:
        return f0
    if x == 1:
        return f1
    res = 0
    if x >= 2:
        for i in range(x-1):
            res = f0 + f1
            f0 = f1
            f1 = res
    return res

def printFibo(n):
    f1 = 1
    f2 = 1
    fiboList = []
    for i in range(n):
        fiboList.append(f1)
        res = f1+f2
        f1 = f2
        f2 = res
    return fiboList

if __name__ == "__main__":
    arr = [4,1,2,2,5,1,4,3]
    a, b = findNum(arr)
    print(a, b)
    print(fibo(3))
    print(fibo(4))
    print(fibo(5))
    print(printFibo(5))

11.寻找缺失的整数.py


#!/usr/bin/env python3
"""
http://adventofcode.com/day/20

Part 1
------
To keep the Elves busy, Santa has them deliver some presents
by hand, door-to-door. He sends them down a street with infinite
houses numbered sequentially: 1, 2, 3, 4, 5, and so on.

Each Elf is assigned a number, too, and delivers presents to houses
based on that number:

  - The first Elf (number 1) delivers presents to every house: 1, 2,
     3, 4, 5, ....
  - The second Elf (number 2) delivers presents to every second house:
     2, 4, 6, 8, 10, ....
  - Elf number 3 delivers presents to every third house: 3, 6, 9, 12,
     15, ....

There are infinitely many Elves, numbered starting with 1. Each Elf
delivers presents equal to ten times his or her number at each house.

So, the first nine houses on the street end up like this:

  - House 1 got 10 presents.
  - House 2 got 30 presents.
  - House 3 got 40 presents.
  - House 4 got 70 presents.
  - House 5 got 60 presents.
  - House 6 got 120 presents.
  - House 7 got 80 presents.
  - House 8 got 150 presents.
  - House 9 got 130 presents.

The first house gets 10 presents: it is visited only by Elf 1, which
delivers 1 * 10 = 10 presents. The fourth house gets 70 presents, because
it is visited by Elves 1, 2, and 4, for a total of 10 + 20 + 40 = 70 presents.

What is the lowest house number of the house to get at least as many
presents as the number in your puzzle input?

Part 2
------
The Elves decide they don't want to visit an infinite number of houses.
Instead, each Elf will stop after delivering presents to 50 houses. To
make up for it, they decide to deliver presents equal to eleven times their
number at each house.

With these changes, what is the new lowest house number of the house to
get at least as many presents as the number in your puzzle input?
"""

from __future__ import print_function, unicode_literals
from math import sqrt
import os
import sys

INFILE = 'inputs/input20.txt'

def divisors(n):
    """
    http://stackoverflow.com/a/171779
    """
    divisors = []
    for i in range(1, int(sqrt(n) + 1)):
        if n % i == 0:
            yield i

            if i * i != n:
                divisors.append(n / i)

    for divisor in reversed(divisors):
        yield divisor

def main():
    input = None

    p1house = None
    p2house = None

    with open(INFILE) as f:
        lines = f.read().splitlines()
        input = int(lines[0].strip())

    for house in range(1, input + 1):
        if not p1house:
            presents = sum(list(divisors(house))) * 10
            if presents >= input:
                p1house = house

        if not p2house:
            divs = divisors(house)
            presents = sum([d for d in divs if house / d <= 50]) * 11
            if presents >= input:
                p2house = house

        if p1house and p2house:
            break

    msg = '[Python]  Puzzle 20-1: {}'
    print(msg.format(p1house))

    msg = '[Python]  Puzzle 20-2: {}'
    print(msg.format(p2house))

if __name__ == '__main__':
    main()


from Dispositivos import agregar
from HiloDemonio import empezarMonitorearAgentes
from os import system
from Reporte import generarReporte

def menu():
    try:
        print("[1] Agregar dispositivo")
        print("[2] Generar Reporte")
        opcion = int(input())
        return opcion
    except KeyboardInterrupt:
        print("Se interrumpió  el flujo.")

if __name__ == '__main__':
    empezarMonitorearAgentes()
    while 1:
        input("Presione la tecla Enter para continuar.\n")
        system("clear")
        opcion = menu()

        if (opcion == 1):
            print("Nombre del HOST o la dirección IP: ")
            host = input()
            print("Nombre de la comunidad: ")
            comunidad = input()

            agregar(comunidad, host)

        elif (opcion == 2):
            print("Escribe el host a la dirección  IP del dispositivo a monitorizar.\n")
            host = input()
            generarReporte(host)


import time
print("De 1 a 100 com while:")
i=1
vect =[]
while(i <=100):
    print(i)
    i += 1
    time.sleep(0.20)

print("De 100 a 1 com for:")
for j in range(100,0,-1):
    vect.append(j)
print(vect)


import os
import shutil
import numpy as np
import torch
import torch.nn as nn
from tqdm import tqdm

import config
from models.recommender_model import Recommender
from models.hred import HRED
from batch_loaders.batch_loader import DialogueBatchLoader
from batch_loaders.reddit_batch_loader import RedditBatchLoader
import test_params
from utils import create_dir

def train(model, batch_loader, nb_epochs, patience, save_path):
    def save_model(val_loss, best_loss, epoch, patience_count):
        # Save model
        is_best = val_loss < best_loss
        best_loss = min(best_loss, val_loss)
        save_checkpoint({
            "params": model.params,
            "epoch": epoch,
            "state_dict": model.state_dict(),
            "best_loss": best_loss,
        }, is_best, save_path)

        # Patience
        if is_best:
            patience_count = 0
        else:
            patience_count += 1
        return best_loss, patience_count

    # set word2id in batchloader from encoder
    batch_loader.set_word2id(model.encoder.word2id)
    epoch = 0
    patience_count = 0
    best_loss = 1e10
    n_train_batches = batch_loader.n_batches["train"]

    training_losses = []
    criterion = nn.NLLLoss()
    optimizer = torch.optim.Adam(filter(lambda p: p.requires_grad, model.parameters()))
    while epoch < nb_epochs:
        model.train()
        losses = []
        for i in tqdm(range(n_train_batches)):
            batch = batch_loader.load_batch(subset="train")
            # dummy_output = torch.Tensor(np.ones(target.data.shape))
            # dialogue and movie inputs are handled by gensen, so no need to send them to GPU
            if model.cuda_available:
                batch["dialogue"] = batch["dialogue"].cuda()
                batch["target"] = batch["target"].cuda()
                batch["senders"] = batch["senders"].cuda()

            optimizer.zero_grad()
            loss = model.train_iter(batch, criterion=criterion)
            optimizer.step()
            # keep losses in memory
            losses.append(loss)

            # intra epoch evaluations for long trainings
            if i > 0 and i % 10000 == 0:
                # Evaluate
                val_loss = model.evaluate(batch_loader=batch_loader, criterion=criterion)
                print('Epoch : {} Validation Loss : {}'.format(epoch + float(i) / n_train_batches, val_loss))
                # Write logs
                with open(os.path.join(save_path, "logs"), "a+") as f:
                    text = "EPOCH {} : losses {} {} \n". \
                        format(epoch + float(i) / n_train_batches, np.mean(losses), val_loss)
                    f.write(text)

                best_loss, patience_count = save_model(
                    val_loss, best_loss, epoch + float(i) / n_train_batches, patience_count)
                if patience_count >= patience:
                    print("Early stopping, {} epochs without best".format(patience_count))
                    return
        else:
            epoch += 1
            print('Epoch : {} Training Loss : {}'.format(epoch, np.mean(losses)))
            training_losses.append(np.mean(losses))

            # Evaluate
            val_loss = model.evaluate(batch_loader=batch_loader, criterion=criterion)

            print('Epoch : {} Validation Loss : {}'.format(epoch, val_loss))
            print('--------------------------------------------------------------')

            # Write logs
            with open(os.path.join(save_path, "logs"), "a+") as f:
                text = "EPOCH {} : losses {} {} \n". \
                    format(epoch, training_losses[-1], val_loss)
                f.write(text)

            best_loss, patience_count = save_model(val_loss, best_loss, epoch, patience_count)
            if patience_count >= patience:
                print("Early stopping, {} epochs without best".format(patience_count))
                return

    print("Training done.")
    return False

def save_checkpoint(state, is_best, path):
    torch.save(state, os.path.join(path, "checkpoint"))
    if is_best:
        shutil.copy(os.path.join(path, "checkpoint"), os.path.join(path, "model_best"))

def explore_params(params_seq, data="movie_dialogue", hred=False):
    """

    :param params_seq: sequence of tuples (save_folder, model_params, train_params)
    :return:
    """
    if hred:
        model_class = HRED
        sources = "dialogue"
    else:
        model_class = Recommender
        sources = "dialogue movie_occurrences movieIds_in_target"
    for (save_path, params, train_params) in params_seq:
        print("Saving in {} with parameters : {}, {}".format(save_path, params, train_params))
        create_dir(save_path)

        if data == "movie_dialogue_pretrained":
            # pre train on reddit data set
            batch_loader = RedditBatchLoader(batch_size=train_params["batch_size"])
            model = model_class(train_vocab=batch_loader.train_vocabulary, n_movies=batch_loader.n_movies,
                                params=params)
            train(
                model, batch_loader=batch_loader,
                nb_epochs=train_params["nb_epochs"], patience=20, save_path=save_path + "/reddit"
            )

            # train on our DB.
            batch_loader = DialogueBatchLoader(
                sources=sources,
                batch_size=train_params["batch_size"]
            )
            train(
                model, batch_loader=batch_loader,
                nb_epochs=train_params["nb_epochs"], patience=train_params["patience"], save_path=save_path
            )
        elif data == "movie_dialogue":
            # Just train on our DB
            batch_loader = DialogueBatchLoader(
                sources=sources,
                batch_size=train_params["batch_size"]
            )
            model = model_class(train_vocab=batch_loader.train_vocabulary, n_movies=batch_loader.n_movies,
                                params=params)
            train(
                model,
                batch_loader=batch_loader,
                nb_epochs=train_params["nb_epochs"],
                patience=train_params["patience"],
                save_path=save_path
            )
        elif data == "increasing_data_size":
            for size in [1000, 2000, 4000, 6000, -1]:
                batch_loader = DialogueBatchLoader(
                    sources=sources,
                    batch_size=train_params["batch_size"],
                    training_size=size
                )
                model = model_class(
                    train_vocab=batch_loader.train_vocabulary,
                    n_movies=batch_loader.n_movies,
                    params=params,
                )
                train(
                    model,
                    batch_loader=batch_loader,
                    nb_epochs=train_params["nb_epochs"],
                    patience=train_params["patience"],
                    save_path=save_path + "/{}training".format(size)
                )

        else:
            raise ValueError(
                "data parameter expected to be 'movie_dialogue' or 'movie_dialogue_pretrained'. Got '{}' instead".
                    format(data))

if __name__ == '__main__':
    params_seq = [(config.RECOMMENDER_MODEL, test_params.recommender_params, test_params.train_recommender_params)]
    explore_params(params_seq, data="movie_dialogue")


"""
에누리 크롤링 코드
"""
#Django import
import os
import sys

#프로젝트 절대경로
sys.path.append('D:\Capstone_Design\config')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings')

# 웹프레임워크
import django
django.setup()
from note_book_service.models import Prod, Prod_property, Prod_img

# 셀레니움 import
from selenium import webdriver
import chromedriver_autoinstaller
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# BeautifulSoup import
from bs4 import BeautifulSoup
import time

class enuri:
    # 크롬 드라이버 초기화
    def init_driver(self):
        chromedriver_autoinstaller.install()
        driver = webdriver.Chrome()

        # 브라우저 내부 대기
        driver.implicitly_wait(5)

        # url 접근
        driver.get('http://www.enuri.com/list.jsp?cate=0404')

        return driver

    # bs4 초기화
    def init_bs4(self, driver):
        # 페이지 소스 가져오기
        source = driver.page_source
        # bs4 초기화
        soup = BeautifulSoup(source, 'html.parser')
        return soup

    # 버튼 클릭
    def button_click(self, driver):
        comparison_price = "#listBodyDiv > div.list-body > div.list-body-cont > div.list-filter > div.list-filter-top > ul > li:nth-child(2) > a"
        WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.CSS_SELECTOR, comparison_price))).click()
        time.sleep(1)
        new_prod = "#listBodyDiv > div.list-body > div.list-body-cont > div.list-filter > div.list-filter-bot > ul > li:nth-child(4)"
        WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.CSS_SELECTOR, new_prod))).click()
        time.sleep(1)

    # 크롤링 함수
    def enuri_crolling(self, driver, spage, lpage):

        lpage /= 10
        last = 0
        count = 0
        # 마지막 페이지까지 반복
        while last != lpage:
            soup = self.init_bs4(driver)
            time.sleep(1)
            # 상품 리스트에 저장
            notebook_list = soup.select('li.prodItem')
            time.sleep(1)

            for item_list in notebook_list:
                # 상품명, 제조사 가격, 상품 등록일 저장
                model_id = item_list.attrs["data-model-origin"]

                try:
                    Prod.objects.get(prod_id=model_id)
                    print("PASS")
                    break
                except:
                    company = item_list.select_one('li.item__etc--brand > a').text.strip()
                    name = item_list.select_one('div.item__model > a').text.strip()
                    price = item_list.select_one('div.opt--price > span').text.strip().replace(",", "")
                    reg_date = item_list.select_one('li.item__etc--date').text.strip()
                    # 리뷰 평점 및 개수 인덱싱 후 저장
                    # review_count = item_list.select_one('li.item__etc--score').text.split()

                    # 페이지 카운트 추가
                    count += 1

                    print(model_id, company, name, price, reg_date)

                    #============================ 기본 정보 DB저장=============================================
                    try:
                        Prod(prod_id=model_id, prod_company=company, prod_name=name, prod_price=price,
                             prod_reg_date=reg_date[-7:]).save()
                    except:
                        print("Error : Skip")
                    '''
                    if not review_count:
                        Prod(prod_id=model_id, prod_company=company, prod_name=name, prod_price=price,
                             prod_reg_date=reg_date).save()
                    else:
                        try:
                            Prod(prod_id=model_id, prod_company=company, prod_name=name, prod_price=price,
                                 prod_reg_date=reg_date, prod_review_grade=review_count[0],
                                 # prod_review_count=review_count[1].strip('('')').replace(',', '')).save()
                        except:
                            Prod(prod_id=model_id, prod_company=company, prod_name=name, prod_price=price,
                                 prod_reg_date=reg_date, prod_review_grade=review_count[0],
                                 # prod_review_count=review_count[1].strip('('')')).save()
                    '''

            print('===============================================')
            print('크롤링 횟수 : ', count, "회")
            print('===============================================')
            # 페이지 변수 증가
            spage += 1

            # 페이지 넘기기
            nextPage = 'div.paging__inner > a:nth-child({})'.format(spage)
            nextbutton = '#listBodyDiv > div.list-body > div.list-body-cont > div.goods-list > div.paging > div > button.paging__btn--next > i'
            if spage == 11:
                # 페이지 버튼 클릭
                WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.CSS_SELECTOR, nextbutton))).click()
                spage = 1
                last += 1
            else:
                WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.CSS_SELECTOR, nextPage))).click()

            # BeautifulSoup 값 삭제
            del soup

            # 3초간 대기
            time.sleep(3)

    def enuri_croll(self):
        driver = self.init_driver()

        self.button_click(driver)
        time.sleep(4)

        # 크롤링 Start(드라이버, 초기페이지, 마지막 페이지)
        self.enuri_crolling(driver, 1, 60)

        # 브라우저 종료
        print("크롤링이 끝났습니다.")
        driver.close()

if __name__=='__main__':
    start_time = time.time()
    result = enuri()
    result.enuri_croll()


import scrapy
from pyquery import PyQuery as pq
import json
from ..items import HongheItem
import re
import requests

class HhSpider(scrapy.Spider):
    name = 'hh'

    def start_requests(self):
        url = 'http://tuku.hh.cn/list1/15-16'
        yield scrapy.Request(url, callback=self.get_id_one_class)

    def get_id_one_class(self, response):
        html = response.text
        url = 'http://tuku.hh.cn/respository'
        info_url = 'http://tuku.hh.cn/respository/info'
        dics = re.findall(r'"sub_respository":\[(.*?)\]};', html)
        dics_list = dics[0].replace('],"old_photo":[', ',').replace('},{', '}@{').split('@')
        for dic in dics_list:
            item = HongheItem()
            dic = json.loads(dic)
            i_d = dic['id']
            item['one_class'] = dic['name']
            count_data = {
                'i': str(i_d),
                't': '3',
                'callback': 'jQuery111204507538242328981_1618236090086',
            }
            hd = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36'}
            h = requests.post(info_url, data=count_data, headers=hd).text
            d_count = json.loads(h.split('(')[1].split(')')[0])
            count = int(d_count['c_n'])
            for i in range(0, count + 20, 20):
                data = {
                    'id': str(i_d),
                    'start': str(i),
                    'cnt': '20',
                    'callback': 'jQuery111205435798931123299_1618229472875'
                }
                yield scrapy.FormRequest(url, formdata=data, meta={'item': item}, callback=self.parse, dont_filter=True)

    def parse(self, response, **kwargs):
        j = response.text
        d_str = json.loads(j.split('(', 1)[1].rsplit(')', 1)[0])
        for d in d_str['data']:
            item = response.meta['item']
            two_href = 'http://tuku.hh.cn/' + d['href']
            item['two_class'] = d['title']
            yield scrapy.Request(two_href, meta={'item': item}, callback=self.detail_parse)

    def detail_parse(self, response):
        html = response.text
        datas_list = re.findall(r'var data=\[(.*)\];', html)
        dict_list = datas_list[0].replace('},{', '}@{').split('@')
        for d in dict_list:
            d_data = json.loads(d)
            item = response.meta['item']
            item['img_name'] = d_data['description']
            item['img_url'] = 'http://tuku.hh.cn/' + d_data['file'].replace('\\', '')
            yield item