from __future__ import print_function, division
from warnings import warn
import numpy as np
from nilmtk.disaggregate import Disaggregator
from keras.layers import Conv1D, Dense, Dropout, Reshape, Flatten, Bidirectional, GRU
import os
import pickle
import pandas as pd
import numpy as np
from collections import OrderedDict
from keras.optimizers import SGD
from keras.models import Sequential, load_model
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from keras.callbacks import ModelCheckpoint
import keras.backend as K
import random
import sys
import json
from .util import *

random.seed(10)
np.random.seed(10)

class SequenceLengthError(Exception):
    pass

class ApplianceNotFoundError(Exception):
    pass

class WindowGRU(Disaggregator):

    def __init__(self, params):
        """
        Parameters to be specified for the model
        """

        self.MODEL_NAME = "WindowGRU"
        self.models = OrderedDict()
        self.chunk_wise_training = params.get('chunk_wise_training', False)
        self.sequence_length = params.get('sequence_length', 99)
        self.n_epochs = params.get('n_epochs', 30)
        self.batch_size = params.get('batch_size', 1024)
        self.mains_mean = params.get('mains_mean', 1800)
        self.mains_std = params.get('mains_std', 600)
        self.appliance_params = params.get('appliance_params', {})
        self.save_model_path = params.get('save-model-path', None)
        self.load_model_path = params.get('pretrained-model-path', None)
        self.models = OrderedDict()
        if self.load_model_path:
            self.load_model()
        if self.sequence_length % 2 == 0:
            print("Sequence length should be odd!")
            raise (SequenceLengthError)

    def partial_fit(self, train_main, train_appliances, do_preprocessing=True,
                    **load_kwargs):

        # If no appliance wise parameters are provided, then copmute them using the first chunk
        if len(self.appliance_params) == 0:
            self.set_appliance_params(train_appliances)

        print("...............GRU partial_fit running...............")
        # Do the pre-processing, such as  windowing and normalizing

        if do_preprocessing:
            train_main, train_appliances = self.call_preprocessing(
                train_main, train_appliances, 'train')

        train_main = pd.concat(train_main, axis=0)
        train_main = train_main.values.reshape((-1, self.sequence_length, 1))

        new_train_appliances = []
        for app_name, app_df in train_appliances:
            app_df = pd.concat(app_df, axis=0)
            app_df_values = app_df.values.reshape((-1, 1))
            new_train_appliances.append((app_name, app_df_values))
        train_appliances = new_train_appliances

        for appliance_name, power in train_appliances:
            # Check if the appliance was already trained. If not then create a new model for it
            if appliance_name not in self.models:
                print("First model training for ", appliance_name)
                self.models[appliance_name] = self.return_network()
            # Retrain the particular appliance
            else:
                print("Started Retraining model for ", appliance_name)

            model = self.models[appliance_name]
            if train_main.size > 0:
                # Sometimes chunks can be empty after dropping NANS
                if len(train_main) > 10:
                    # Do validation when you have sufficient samples
                    filepath = 'GRU-temp-weights-' + str(random.randint(0, 100000)) + '.h5'
                    checkpoint = ModelCheckpoint(filepath, monitor='val_loss', verbose=1, save_best_only=True,
                                                 mode='min')
                    train_x, v_x, train_y, v_y = train_test_split(train_main, power, test_size=.15, random_state=10)
                    model.fit(train_x, train_y, validation_data=[v_x, v_y], epochs=self.n_epochs,
                              callbacks=[checkpoint], batch_size=self.batch_size)
                    model.load_weights(filepath)
        if self.save_model_path:
            self.save_model()

    def load_model(self):
        print("Loading the model using the pretrained-weights")
        model_folder = self.load_model_path
        if os.path.exists(os.path.join(model_folder, "model.json")):
            with open(os.path.join(model_folder, "model.json"), "r") as f:
                model_string = f.read().strip()
                params_to_load = json.loads(model_string)

            self.sequence_length = int(params_to_load['sequence_length'])
            self.mains_mean = params_to_load['mains_mean']
            self.mains_std = params_to_load['mains_std']
            self.appliance_params = params_to_load['appliance_params']

            for appliance_name in self.appliance_params:
                self.models[appliance_name] = self.return_network()
                self.models[appliance_name].load_weights(os.path.join(model_folder, appliance_name + ".h5"))

    def save_model(self):
        if (os.path.exists(self.save_model_path) == False):
            os.makedirs(self.save_model_path)
        params_to_save = {}
        params_to_save['appliance_params'] = self.appliance_params
        params_to_save['sequence_length'] = self.sequence_length
        params_to_save['mains_mean'] = self.mains_mean
        params_to_save['mains_std'] = self.mains_std
        for appliance_name in self.models:
            print("Saving model for ", appliance_name)
            self.models[appliance_name].save_weights(os.path.join(self.save_model_path, appliance_name + ".h5"))

        with open(os.path.join(self.save_model_path, 'model.json'), 'w') as file:
            file.write(json.dumps(params_to_save, cls=NumpyEncoder))

    def disaggregate_chunk(self, test_main_list, model=None, do_preprocessing=True):

        if model is not None:
            self.models = model

        # Preprocess the test mains such as windowing and normalizing

        if do_preprocessing:
            test_main_list = self.call_preprocessing(
                test_main_list, submeters_lst=None, method='test')

        test_predictions = []
        for test_main in test_main_list:
            # if(test_main.shape[0] < 100):
            #     continue
            test_main = test_main.values
            test_main = test_main.reshape((-1, self.sequence_length, 1))
            disggregation_dict = {}
            for appliance in self.models:
                prediction = self.models[appliance].predict(test_main, batch_size=self.batch_size)
                prediction = self.appliance_params[appliance]['mean'] + prediction * self.appliance_params[appliance][
                    'std']
                valid_predictions = prediction.flatten()
                valid_predictions = np.where(valid_predictions > 0, valid_predictions, 0)
                df = pd.Series(valid_predictions)
                disggregation_dict[appliance] = df
            results = pd.DataFrame(disggregation_dict, dtype='float32')
            test_predictions.append(results)
        return test_predictions

    def return_network(self):
        '''Creates the GRU architecture described in the paper
        '''
        model = Sequential()
        # 1D Conv
        model.add(Conv1D(16, 4, activation='relu', input_shape=(self.sequence_length, 1), padding="same", strides=1))
        # Bi-directional GRUs
        model.add(Bidirectional(GRU(64, activation='relu',
                                    return_sequences=True), merge_mode='concat'))
        model.add(Dropout(0.5))
        model.add(Bidirectional(GRU(128, activation='relu',
                                    return_sequences=False), merge_mode='concat'))
        model.add(Dropout(0.5))
        # Fully Connected Layers
        model.add(Dense(128, activation='relu'))
        model.add(Dropout(0.5))
        model.add(Dense(1, activation='linear'))
        model.compile(loss='mse', optimizer='adam')
        return model

    def call_preprocessing(self, mains_lst, submeters_lst, method):

        if method == 'train':
            mains_df_list = []
            for mains in mains_lst:
                new_mains = mains.values.flatten()
                n = self.sequence_length
                units_to_pad = n - 1
                new_mains = np.pad(new_mains, (units_to_pad, 0), 'constant', constant_values=(0, 0))
                new_mains = np.array([new_mains[i:i + n] for i in range(len(new_mains) - n + 1)])
                new_mains = (new_mains - self.mains_mean) / self.mains_std
                mains_df_list.append(pd.DataFrame(new_mains))

            appliance_list = []
            for app_index, (app_name, app_df_list) in enumerate(submeters_lst):
                if app_name in self.appliance_params:
                    app_mean = self.appliance_params[app_name]['mean']
                    app_std = self.appliance_params[app_name]['std']
                else:
                    print("Parameters for ", app_name, " were not found!")
                    raise ApplianceNotFoundError()

                processed_appliance_dfs = []

                for app_df in app_df_list:
                    new_app_readings = app_df.values.reshape((-1, 1))
                    # This is for choosing windows
                    new_app_readings = (new_app_readings - app_mean) / app_std
                    # Return as a list of dataframe
                    processed_appliance_dfs.append(pd.DataFrame(new_app_readings))
                appliance_list.append((app_name, processed_appliance_dfs))
            return mains_df_list, appliance_list

        else:
            mains_df_list = []

            for mains in mains_lst:
                new_mains = mains.values.flatten()
                n = self.sequence_length
                # units_to_pad = n // 2
                # new_mains = np.pad(new_mains, (units_to_pad, units_to_pad), 'constant', constant_values=(0, 0))
                units_to_pad = n - 1
                new_mains = np.pad(new_mains, (units_to_pad, 0), 'constant', constant_values=(0, 0))
                new_mains = np.array([new_mains[i:i + n] for i in range(len(new_mains) - n + 1)])
                new_mains = (new_mains - self.mains_mean) / self.mains_std
                mains_df_list.append(pd.DataFrame(new_mains))
            return mains_df_list

    def set_appliance_params(self, train_appliances):
        # Find the parameters using the first
        for (app_name, df_list) in train_appliances:
            l = np.array(pd.concat(df_list, axis=0))
            app_mean = np.mean(l)
            app_std = np.std(l)
            if app_std < 1:
                app_std = 100
            self.appliance_params.update({app_name: {'mean': app_mean, 'std': app_std}})
        print(self.appliance_params)


import json
import logging
import os
import six

from contextlib import contextmanager
from threading import Lock as _Lock

from pony.orm import db_session

from iot.const import FIXTURES_DIR

try:
    from json import JSONDecodeError
except ImportError:
    JSONDecodeError = ValueError

log = logging.getLogger('iottalk')

def is_posix():
    return os.name.startswith('posix')

def is_win():
    return os.name.startswith('nt')

@contextmanager
def suppress(*args):
    '''
    Inspire by py34+, contextlib.suppress

    >>> with suppress(OSError):
    ...     raise OSError()

    >>> with suppress(OSError, KeyboardInterrupt):
    ...     raise KeyboardInterrupt()
    '''
    try:
        yield
    except args:
        pass

def load_json_from_file(filename):
    '''
    :return: the json decoded object if load successfully.
             None if loading failed
    '''
    with open(filename, 'r') as f:
        try:
            return json.load(f)
        except ValueError as err:
            log.error('Loading fixture {!r} failed', *err.args)

def load_fixtures(fixtures, db):
    '''
    Load all fixtures to db from the given file list.

    The fixture is an json file contain some db records.
    It should have following schema::

        {
            'table_name': {
                'tuple_primary_key1': {
                    'field 1': '...',
                    'field 2': '...'
                }
            }
        }

    :param fixtures: the file name list
    :type fixtures: iterable
    :param db: the pony db instance
    '''
    for filename in fixtures:
        fixture = load_json_from_file(filename)
        if fixture is None:
            continue

        with db_session:
            for table in fixture.keys():
                obj = getattr(db, table, None)
                if obj is None:
                    log.warning('The table {!r} not supported'.format(table))
                    continue

                for id_, attrs in fixture[table].items():
                    obj(id=id_, **attrs)

        log.info('fixture {!r} loaded successfully'.format(filename))

def load_dev_fixtures(db):
    '''
    Load all ``fixtures/dev-*`` into database. We will invoke
    :py:func:`~utils.load_fixtures`.

    :param db: the pony db instance
    '''
    load_fixtures(sorted(
        os.path.join(FIXTURES_DIR, f)
        for f in os.listdir(FIXTURES_DIR)
            if os.path.isfile(os.path.join(FIXTURES_DIR, f))
                and f.startswith('dev-')),
        db)

if six.PY2:
    from Queue import Queue

    class SimpleLock(object):
        def __init__(self):
            self._q = Queue(maxsize=1)
            self._q.put(True)

        def acquire(self, block=True, timeout=None):
            return self._q.get(block, timeout)

        def release(self):
            self._q.put(True)

        def __enter__(self):
            return self.acquire()

        def __exit__(self, exc_type, exc_value, traceback):
            self.release()

else:  # python3+
    SimpleLock = _Lock


import itertools
from unittest import TestCase

from pytsa import sa_int, sa_number, sa_string, sa_type

class TestMultipleRules(TestCase):

    def test_multiple_rules_case(self):

        @sa_int('a', gt=-4, lte=4.0)
        @sa_string('b', starts_with='ab', is_lower=True)
        @sa_type('c')
        @sa_number('d', gte=-5, lte=6.5, allow_none=True)
        def _test(a, b, *, c=int, d):
            return

        correct_a = [4, 0, -3]
        incorrect_a = [-4, 5, 2.2, None]
        correct_b = ['abcd', 'ab', 'ab\t3']
        incorrect_b = ['bcd', 'AB', '', None]
        correct_c = [int, str, type(int)]
        incorrect_c = ['int', 'bool', None]
        correct_d = [-5.0, 6.5, None, 0]
        incorrect_d = ['3', -7.2, 11]

        # Test all possible combinations

        # - All correct
        for a, b, c, d in itertools.product(correct_a, correct_b, correct_c, correct_d):
            _test(a, b, **{'c': c, 'd': d})

        # - A Incorrect
        for a, b, c, d in itertools.product(incorrect_a, correct_b, correct_c, correct_d):
            with self.assertRaises(Exception):
                _test(a, b, **{'c': c, 'd': d})

        # - B Incorrect
        for a, b, c, d in itertools.product(correct_a, incorrect_b, correct_c, correct_d):
            with self.assertRaises(Exception):
                _test(a, b, **{'c': c, 'd': d})

        # - C Incorrect
        for a, b, c, d in itertools.product(correct_a, correct_b, incorrect_c, correct_d):
            with self.assertRaises(Exception):
                _test(a, b, **{'c': c, 'd': d})

        # - D Incorrect
        for a, b, c, d in itertools.product(correct_a, correct_b, correct_c, incorrect_d):
            with self.assertRaises(Exception):
                _test(a, b, **{'c': c, 'd': d})

        # - All Incorrect
        for a, b, c, d in itertools.product(incorrect_a, incorrect_b, incorrect_c, incorrect_d):
            with self.assertRaises(Exception):
                _test(a, b, **{'c': c, 'd': d})


import numpy as np
import pickle
from datetime import datetime

sleep_start=0
sleep_end=7
###############################################################################
######################## Find features #######################################
###############################################################################
f = open('reduced.pckl', 'rb')
[v,p,df] = pickle.load(f)
f.close()

#   1. promotion_average_discount: promotion weighted by hours
df['promotion_price'] = np.zeros(len(df))
df['promotion_rebate'] = np.zeros(len(df))
df['promotion_discount'] = np.zeros(len(df))
df['promotion_item'] = np.zeros(len(df))
df['promotion_hour'] = np.zeros(len(df))
for i in range(0,len(p)):
    print(i)
    S=(p['start_time'][i]-df['date'][0]).days
    E=(p['end_time'][i]-df['date'][0]).days
    SH=p['start_time'][i].hour
    EH=p['end_time'][i].hour
    tmpp=p['promotion_price'][i]
    tmpr=p['rebate'][i]
    dis=p['discount_percent'][i]
    duration=E-S

    if S==365:
        continue

    if duration==0:
        df['promotion_item'][S]+=1
        if SH>sleep_end:
            df['promotion_hour'][S]+=(EH-SH)
            df['promotion_price'][S]+=tmpp*(EH-SH)
            df['promotion_rebate'][S]+=tmpr*(EH-SH)
            df['promotion_discount'][S]+=dis*(EH-SH)
        else:
            df['promotion_hour'][S]+=(EH-sleep_end)
            df['promotion_price'][S]+=tmpp*(EH-sleep_end)
            df['promotion_rebate'][S]+=tmpr*(EH-sleep_end)
            df['promotion_discount'][S]+=dis*(EH-sleep_end)

    elif duration==1:
        df['promotion_item'][S]+=1
        if SH>sleep_end:
            df['promotion_hour'][S]+=24-SH
            df['promotion_price'][S]+=tmpp*(24-SH)
            df['promotion_rebate'][S]+=tmpr*(24-SH)
            df['promotion_discount'][S]+=dis*(24-SH)
        else:
            df['promotion_hour'][S]+=24-sleep_end
            df['promotion_price'][S]+=tmpp*(24-sleep_end)
            df['promotion_rebate'][S]+=tmpr*(24-sleep_end)
            df['promotion_discount'][S]+=dis*(24-sleep_end)
        if EH>sleep_end and E<365:
            df['promotion_hour'][E]+=(EH-sleep_end)
            df['promotion_item'][E]+=1
            df['promotion_price'][E]+=tmpp*(EH-sleep_end)
            df['promotion_rebate'][E]+=tmpr*(EH-sleep_end)
            df['promotion_discount'][E]+=dis*(EH-sleep_end)
    else:
        df['promotion_item'][S:E]+=1
        if SH>sleep_end:
            df['promotion_hour'][S]+=24-SH
            df['promotion_price'][S]+=tmpp*(24-SH)
            df['promotion_rebate'][S]+=tmpr*(24-SH)
            df['promotion_discount'][S]+=dis*(24-SH)
        else:
            df['promotion_hour'][S]+=(24-sleep_end)
            df['promotion_price'][S]+=tmpp*(24-sleep_end)
            df['promotion_rebate'][S]+=tmpr*(24-sleep_end)
            df['promotion_discount'][S]+=dis*(24-sleep_end)
        df['promotion_hour'][S+1:E]+=(24-sleep_end)
        df['promotion_price'][S+1:E]+=tmpp*(24-sleep_end)
        df['promotion_rebate'][S+1:E]+=tmpr*(24-sleep_end)
        df['promotion_discount'][S+1:E]+=dis*(24-sleep_end)
        if EH>sleep_end and E<365:
            df['promotion_hour'][E]+=(EH-sleep_end)
            df['promotion_item'][E]+=1
            df['promotion_price'][E]+=tmpp*(EH-sleep_end)
            df['promotion_rebate'][E]+=tmpr*(EH-sleep_end)
            df['promotion_discount'][E]+=dis*(EH-sleep_end)

for i in range(0,len(df)):
    if df['promotion_hour'].iloc[i]==0:
        df['promotion_price'].iloc[i]=0
        df['promotion_rebate'].iloc[i]=0
        df['promotion_discount'].iloc[i]=0
    else:
        df['promotion_price'].iloc[i]=df['promotion_price'].iloc[i]/df['promotion_hour'].iloc[i]
        df['promotion_rebate'].iloc[i]=df['promotion_rebate'].iloc[i]/df['promotion_hour'].iloc[i]
        df['promotion_discount'].iloc[i]=df['promotion_discount'].iloc[i]/df['promotion_hour'].iloc[i]

f = open('df_promotion.pckl', 'wb')
pickle.dump(df, f)
f.close()

f = open('df_promotion.pckl', 'rb')
df = pickle.load(f)
f.close()
print(df)

#   2. vounchers
#       -hourly
df['voucher_value'] = np.zeros(len(df))                         # time weighted average voucher value
df['voucher_min'] = np.zeros(len(df))                           # time weighted average voucher minimum price
df['voucher_discount'] = np.zeros(len(df))                      # time weighted average voucher discount
df['voucher_total'] = np.zeros(len(df))                          # time weighted average voucher number
df['voucher_hour']= np.zeros(len(df))
df['voucher_number']= np.zeros(len(df))                         # number of vouchers per day
df['voucher_value_n'] = np.zeros(len(df))                          # number weighted average voucher value
df['voucher_min_n'] = np.zeros(len(df))                          # number weighted average voucher value
df['voucher_discount_n']= np.zeros(len(df))

#Assume people sleep during 00:00-06:00
i=0
while(i<len(v)):
    print(i)
    S=(v['start_time'].iloc[i]-df['date'][0]).days
    E=(v['end_time'].iloc[i]-df['date'][0]).days
    if S==365:
        i+=1
        continue
    SH=v['start_time'].iloc[i].hour
    EH=v['end_time'].iloc[i].hour
    duration=E-S

    j=i
    va,dis,mp,ul,uv,d,m,vv=0,0,0,0,0,0,0,0
    while(j<len(v) and v['mark_date'].iloc[j]==v['mark_date'].iloc[i]):
        va+=v['value'].iloc[j]
        dis+=v['discount'].iloc[j]
        mp+=v['min_price'].iloc[j]
        ul+=v['usage_limit'].iloc[j]
        uv+=ul*va
        d+=v['discount'].iloc[j]*v['usage_limit'].iloc[j]
        m+=v['min_price'].iloc[j]*v['usage_limit'].iloc[j]
        vv+=v['value'].iloc[j]*v['usage_limit'].iloc[j]
        j+=1
    if duration==0:
        df['voucher_number'][S]+=ul
        df['voucher_value_n'][S]+=vv
        df['voucher_min_n'][S]+=m
        df['voucher_discount_n'][S]+=d

        if SH>sleep_end:
            df['voucher_hour'][S]+=(EH-SH)
            df['voucher_value'][S]+=va*(EH-SH)
            df['voucher_discount'][S]+=dis*(EH-SH)
            df['voucher_min'][S]+=mp*(EH-SH)
            df['voucher_total'][S]+=ul*(EH-SH)
        else:
            df['voucher_hour'][S]+=(EH-sleep_end)
            df['voucher_value'][S]+=va*(EH-sleep_end)
            df['voucher_discount'][S]+=dis*(EH-sleep_end)
            df['voucher_min'][S]+=mp*(EH-sleep_end)
            df['voucher_total'][S]+=ul*(EH-sleep_end)
    elif duration==1:
        if SH>sleep_end:
            df['voucher_hour'][S]+=24-SH
            df['voucher_value'][S]+=va*(24-SH)
            df['voucher_discount'][S]+=dis*(24-SH)
            df['voucher_min'][S]+=mp*(24-SH)
            df['voucher_total'][S]+=ul*(24-SH)
        else:
            df['voucher_hour'][S]+=24-sleep_end
            df['voucher_value'][S]+=va*(24-sleep_end)
            df['voucher_discount'][S]+=dis*(24-sleep_end)
            df['voucher_min'][S]+=mp*(24-sleep_end)
            df['voucher_total'][S]+=ul*(24-sleep_end)
        if EH>sleep_end and E<365:
            df['voucher_number'][E]+=ul
            df['voucher_hour'][E]+=EH-sleep_end
            df['voucher_value'][E]+=va*(EH-sleep_end)
            df['voucher_discount'][E]+=dis*(EH-sleep_end)
            df['voucher_min'][E]+=mp*(EH-sleep_end)
            df['voucher_total'][E]+=ul*(EH-sleep_end)
            df['voucher_value_n'][S:E+1]+=vv
            df['voucher_min_n'][S:E+1]+=m
            df['voucher_discount_n'][S:E+1]+=d
            df['voucher_number_n'][S:E+1]+=ul
        elif EH<=sleep_end and E<365:
            df['voucher_value_n'][S:E]+=vv
            df['voucher_min_n'][S:E]+=m
            df['voucher_discount_n'][S:E]+=d
            df['voucher_number_n'][S:E]+=ul
    else:
        df['voucher_number'][S]+=ul
        if SH>sleep_end:
            df['voucher_hour'][S]+=24-SH
            df['voucher_value'][S]+=va*(24-SH)
            df['voucher_discount'][S]+=dis*(24-SH)
            df['voucher_min'][S]+=mp*(24-SH)
            df['voucher_total'][S]+=ul*(24-SH)
        else:
            df['voucher_hour'][S]+=24-sleep_end
            df['voucher_value'][S]+=va*(24-sleep_end)
            df['voucher_discount'][S]+=dis*(24-sleep_end)
            df['voucher_min'][S]+=mp*(24-sleep_end)
            df['voucher_total'][S]+=ul*(24-sleep_end)
        df['voucher_number'][S+1:E]+=ul
        df['voucher_hour'][S+1:E]+=24-sleep_end
        df['voucher_value'][S+1:E]+=va*(24-sleep_end)
        df['voucher_discount'][S+1:E]+=dis*(24-sleep_end)
        df['voucher_min'][S+1:E]+=mp*(24-sleep_end)
        df['voucher_total'][S+1:E]+=ul*(24-sleep_end)

        if EH>sleep_end and E<365:
            df['voucher_number'][E]+=ul
            df['voucher_hour'][E]+=EH-sleep_end
            df['voucher_value'][E]+=va*(EH-sleep_end)
            df['voucher_discount'][E]+=dis*(EH-sleep_end)
            df['voucher_min'][E]+=mp*(EH-sleep_end)
            df['voucher_total'][E]+=ul*(EH-sleep_end)
            df['voucher_value_n'][S:E+1]+=vv
            df['voucher_min_n'][S:E+1]+=m
            df['voucher_discount_n'][S:E+1]+=d
            df['voucher_number_n'][S:E+1]+=ul
        elif EH<=sleep_end and E<365:
            df['voucher_value_n'][S:E]+=vv
            df['voucher_min_n'][S:E]+=m
            df['voucher_discount_n'][S:E]+=d
            df['voucher_number_n'][S:E]+=ul
    i=j
df['weekday']=list(map(lambda x:x.weekday(),df['date']))
df['voucher_value']=list(map(lambda x,y:x/y,df['voucher_value'],df['voucher_hour']))
df['voucher_discount']=list(map(lambda x,y:x/y,df['voucher_discount'],df['voucher_hour']))
df['voucher_min']=list(map(lambda x,y:x/y,df['voucher_min'],df['voucher_hour']))
df['voucher_total']=list(map(lambda x,y:x/y,df['voucher_total'],df['voucher_hour']))
df['voucher_value_n']=list(map(lambda x,y:x/y,df['voucher_value_n'],df['voucher_number_n']))
df['voucher_min_n']=list(map(lambda x,y:x/y,df['voucher_min_n'],df['voucher_number_n']))
df['voucher_discount_n']=list(map(lambda x,y:x/y,df['voucher_discount_n'],df['voucher_number_n']))
#f = open('features_pv.pckl', 'wb')
#pickle.dump(df, f)
#f.close()
#
#df.to_csv('features.csv')
#
#f = open('features_pv.pckl', 'rb')
#df = pickle.load(f)
#f.close()
#
#   3. Public holidays and Special Events
feature=df[['voucher_value_n','voucher_min_n','voucher_discount_n','voucher_number','weekday','voucher_money','voucher_total','voucher_min','voucher_value','voucher_discount','promotion_discount','promotion_rebate','promotion_price','promotion_hour','promotion_item']]
feature['event']=np.ones(len(feature))    # nomal days
feature['event'][(datetime(2017,1,1)-datetime(2017,1,1)).days:(datetime(2017,1,3)-datetime(2017,1,1)).days]=2  # New Year's Day
feature['event'][(datetime(2017,1,26)-datetime(2017,1,1)).days:(datetime(2017,1,31)-datetime(2017,1,1)).days]=0 # Chinese New Year Delivery Stop
#feature['event'][(datetime(2017,2,27)-datetime(2017,1,1)).days:(datetime(2017,3,1)-datetime(2017,1,1)).days]=2
feature['event'][(datetime(2017,4,3)-datetime(2017,1,1)).days:(datetime(2017,4,5)-datetime(2017,1,1)).days]=2 # Tomb Sweeping Day, Children's Day
#feature['event'][(datetime(2017,5,29)-datetime(2017,1,1)).days:(datetime(2017,5,31)-datetime(2017,1,1)).days]=2
#feature['event'][(datetime(2017,10,4)-datetime(2017,1,1)).days:(datetime(2017,10,5)-datetime(2017,1,1)).days]=2
feature['event'][(datetime(2017,10,9)-datetime(2017,1,1)).days:(datetime(2017,10,11)-datetime(2017,1,1)).days]=2 # National Day Holiday
feature['event'][(datetime(2017,11,8)-datetime(2017,1,1)).days:(datetime(2017,11,12)-datetime(2017,1,1)).days]=3 # 11.11
feature['event'][(datetime(2017,12,11)-datetime(2017,1,1)).days:(datetime(2017,12,12)-datetime(2017,1,1)).days]=3 # 12.12

#   4. Promotion events of the previous day
feature_lag=feature.shift(1)
feature_lag.iloc[0]=feature_lag.iloc[1]
feature_lag['weekday'].iloc[0]=5

feature[['voucher_value_n_l','voucher_min_n_l','voucher_discount_n_l','voucher_number_l','weekday_l','voucher_money_l','voucher_total_l','voucher_min_l','voucher_value_l','voucher_discount_l','promotion_discount_l','promotion_rebate_l','promotion_price_l','promotion_hour_l','promotion_item_l','event_l']]=feature_lag

#   5. Day of the month
feature['monthday']=np.zeros(len(feature))
for i in range(0,len(feature)):
    feature['monthday'][i]=df['date'][i].day

f = open('feature_all.pckl', 'wb')
pickle.dump(feature, f)
f.close()


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

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html
from dspider import poster, items
post_router = {
    items.InvestorSituationItem:poster.InvestorSituationItemPoster
}

class DspiderPipeline(object):
    def process_item(self, item, spider):
        post_router[item.__class__](item).post()
        return item


# exit using 'q'
from IITB_Video_Analytics.summary.keyclipwriter import KeyClipWriter
from imutils.video import VideoStream
import argparse
import datetime
import imutils
import time
import cv2
import numpy as np

ap = argparse.ArgumentParser()
ap.add_argument("-o", "--output", required=True,
	help="path to output directory")
ap.add_argument("-p", "--picamera", type=int, default=-1,
	help="whether or not the Raspberry Pi camera should be used")
ap.add_argument("-f", "--fps", type=int, default=20,
	help="FPS of output video")
ap.add_argument("-c", "--codec", type=str, default="MJPG",
	help="codec of output video")
ap.add_argument("-b", "--buffer-size", type=int, default=32,
	help="buffer size of video clip writer")
args = vars(ap.parse_args())

# haarcascade_frontalface_alt.xml works the best with good accuracy among all other frontal face cascades
face_cascade = cv2.CascadeClassifier('IITB_Video_Analytics/haarcascade_frontalface_alt.xml')

# eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')

cap = cv2.VideoCapture("IITB_Video_Analytics/Videos/Style Tip- Wearing the right colours to suit your skin tone - The Style Hanger.mp4")

# initialize key clip writer and the consecutive number of
# frames that have *not* contained any action
kcw = KeyClipWriter(bufSize=args["buffer_size"])
consecFrames = 0

while cap.isOpened():
    ret, img = cap.read()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.2, 2)
    updateConsecFrames = True
    # returns initial coordinates and width and height of faces
    for (x,y,w,h) in faces:
        cv2.rectangle(img, (x,y), (x+w, y+h), (255,0,0), 2)

        consecFrames = 0

        # roi_gray = gray[y:y+h, x:x+w]
        # roi_color = img[y:y+h, x:x+w]
        # eyes = eye_cascade.detectMultiScale(roi_gray)
        # for (ex,ey,ew,eh) in eyes:
            # cv2.rectangle(roi_color, (ex, ey), (ex+ew, ey+eh), (0,255,0), 2)

        # if we are not already recording, start recording
        if not kcw.recording:
            timestamp = datetime.datetime.now()
            p = "{}/{}.avi".format(args["output"],
                timestamp.strftime("%Y%m%d-%H%M%S"))
            kcw.start(p, cv2.VideoWriter_fourcc(*args["codec"]),
                args["fps"])

    # otherwise, no action has taken place in this frame, so
    # increment the number of consecutive frames that contain
    # no action
    if updateConsecFrames:
        consecFrames += 1

    # update the key frame clip buffer
    kcw.update(img)

    # if we are recording and reached a threshold on consecutive
    # number of frames with no action, stop recording the clip
    if kcw.recording and consecFrames == args["buffer_size"]:
        kcw.finish()

    # show the frame
    cv2.imshow("Frame", img)
    key = cv2.waitKey(1) & 0xFF

    # if the `q` key was pressed, break from the loop
    if key == ord("q"):
        break

# if we are in the middle of recording a clip, wrap it up
if kcw.recording:
    kcw.finish()

cap.release()
cv2.destroyAllWindows()


__author__ = 'baio'

import unittest
from w6.events_gens.equity_3_down_market_2_up import find_events as evt
from w6.events_gens.equity_5_dollars_actual_close import  find_events as evt_5_dollars_actual_close
from w6.strategy_gens.event_date_buy_100_sell_5_days_later import generate_strategy as strat
from w6.market_simulator import build_market
from w6 import strategy_maker
from w6 import market_analyzer
import datetime as dt

import QSTK.qstkstudy.EventProfiler as ep

import QSTK.qstkutil.DataAccess as da

import numpy as np

class TestSequenceFunctions(unittest.TestCase):

    @unittest.skip("demonstrating skipping")
    def test_analise_market_sim(self):
        market_analyzer.analise_market("../data/values_5_dollar_events.csv", "SPY", "../data/5_dollar_events.png")

    @unittest.skip("demonstrating skipping")
    def test_build_market_sim(self):
        build_market(50000, "../data/orders_5_dollar_events.csv", "../data/values_5_dollar_events.csv")

    def test_write_strategy_equity_3_down_market_2_up_buy_100_aapl(self):
        dt_start = dt.datetime(2008, 1, 1)
        dt_end = dt.datetime(2009, 12, 31)
        df_events, d_data = strategy_maker.find_events(dt_start, dt_end, 'SPY', ['AAPL'], evt)
        strategy_maker.write_strategy("orders1.csv", df_events, strat)

    @unittest.skip("demonstrating skipping")
    def test_equity_sp_5_dollars_actual_close(self):
        dt_start = dt.datetime(2008, 1, 1)
        dt_end = dt.datetime(2009, 12, 31)
        dataobj = da.DataAccess('Yahoo')
        ls_symbols = dataobj.get_symbols_from_list('sp5002012')
        df_events, d_data = strategy_maker.find_events(dt_start, dt_end, 'SPY', ls_symbols, evt_5_dollars_actual_close)
        strategy_maker.write_strategy("../data/orders_5_dollar_events.csv", df_events, strat)

    @unittest.skip("demonstrating skipping")
    def test_gen_strategy_equity_3_down_market_2_up_aapl(self):
        dt_start = dt.datetime(2008, 1, 1)
        dt_end = dt.datetime(2009, 12, 31)
        df_events, d_data = strategy_maker.find_events(dt_start, dt_end, 'SPY', ['AAPL'], evt)
        strategy_maker.generate_strategy(df_events, strat)

    @unittest.skip("demonstrating skipping")
    def test_equity_3_down_market_2_up_aapl(self):
        dt_start = dt.datetime(2008, 1, 1)
        dt_end = dt.datetime(2009, 12, 31)
        df_events, d_data = strategy_maker.find_events(dt_start, dt_end, 'SPY', ['AAPL'], evt)
        ep.eventprofiler(df_events, d_data, i_lookback=20, i_lookforward=20,
                         s_filename='test_equity_3_down_market_2_up_aapl_fast.pdf', b_market_neutral=True, b_errorbars=True,
                         s_market_sym='SPY')

    @unittest.skip("demonstrating skipping")
    def test_equity_3_down_market_2_up(self):
        dt_start = dt.datetime(2008, 1, 1)
        dt_end = dt.datetime(2009, 12, 31)
        dataobj = da.DataAccess('Yahoo')
        ls_symbols = dataobj.get_symbols_from_list('sp5002012')
        strategy_maker.find_events(dt_start, dt_end, 'SPY', ls_symbols, evt)
        ep.eventprofiler(df_events, d_data, i_lookback=20, i_lookforward=20,
                         s_filename='test_equity_3_down_market_2_up.pdf', b_market_neutral=True, b_errorbars=True,
                         s_market_sym='SPY')

if __name__ == '__main__':
    unittest.main()


import QRCodeScanner
import time

TB = 0
LID = 1
FID = 2
SID = 3

class Receiver:
    def __init__(self, linkID):
        self.linkID = linkID
        self.scanner = QRCodeScanner.QRCodeScanner()
        self.scanner.start()
        self.currentID = None
        self.currentPacket = ''
        self.terminalFlag = False
        self.lastSeg = ''
        self.timeStart = 0
        self.timeEnd = 0
        self.nextSegNum = 0

    def receive(self):
        while True:
            try:
                segment = self.scanner.getCurrentSegment()

                if (segment != None):  ##prevent frames with no QR from being processed
                    print("Recevier: ", segment)
                    if (segment != self.lastSeg):  ##prevent duplicate QRs from being processed
                        self.lastSeg = segment

                        headerMetrics = self.processHeader(segment)
                        tB = headerMetrics[TB]
                        lID = headerMetrics[LID]
                        fID = headerMetrics[FID]
                        sID = headerMetrics[SID]

                        if lID != self.linkID:
                            pass

                        else:
                            validFrame = self.checkFrame(fID, sID, tB)
                            if validFrame == True:
                                self.currentID = fID
                                self.currentFrame += segment[2:].strip('Z')

                                if self.checkTerminal(tB) == True:
                                    self.serialForward()
                                    break

                                self.nextSegNum += 1

            except:
                pass

            time.sleep(.08)

    def serialForward(self):
        print("Packet Received: ", self.currentFrame)
        self.timeEnd = time.time()
        self.calcSpeed()
        self.flagFinish = True

    def calcSpeed(self):
        time = self.timeEnd - self.timeStart
        print(time)
        speed = len(self.currentFrame) / 2
        print("Transmit Speed (Bps): ", speed)

    def newFrame(self):

        self.currentFrame = ''
        self.nextSeg = 0
        self.lastSeg = ''
        self.timeStart = time.time()

    def processHeader(self, segment):
        header = segment[:6]
        linkByte = header[:2]
        frameByte = header[2:4]
        segByte = header[4:]
        (terminalBit, linkNum) = self.processLinkByte(linkByte)

        frameNum = int(frameByte,16)
        segNum = int(segByte,16)
        return (terminalBit, linkNum, frameNum, segNum)

    def processLinkByte(self, linkByte):
        binary = bin(int(linkByte,16))[2:]
        terminalBit = binary[0]
        linkBin = binary[1:]
        linkNum = int(linkBin,2)
        return (terminalBit, linkNum)

    def checkFrame(self, frameID, segID):
        if self.terminalFlag == True:
            self.newFrame()

        if (frameID == self.currentFrame) or (self.currentFrame == None):
            if self.nextSegNum == segID:
                return True
            else:
                return False
        else:
            return False

    def checkTerminal(self, terminalBit):
        if terminalBit == '1':
            self.terminalFlag == True
            return True
        elif terminalBit == '0':
            return False

receiver = Receiver()
receiver.receive()


# coding=utf-8
import sys
import json
import logging
import datetime
from project.utils.connect_mongodb import ConnectMongo
from project.models.schemas import Player, Tournament
from mongoengine import *
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)

#collect all fi player ids to a list X
#check each tournament X
#check if tournament individual player is on the finnish player list
#check what were the round ratings for the players
#if found ratings where higher than previous highest save the info

logging.info('Starting country highest rated analytics script')
ConnectMongo()

all_pdga_numbers = []
all_country_players = Player.objects.filter(country="united states").only("pdga_number")
for country_player in all_country_players:
    all_pdga_numbers.append(country_player.pdga_number)

all_tournaments = Tournament.objects.filter().only("players", "tournament_id")

highest_round_rating = 0
player_id = 0
tournament_id = 0

dict = {}

same_round_rating = []

logging.info(f'Highest round rating for Finland: {str(highest_round_rating)} for player {str(player_id)} in tournament with ID: {str(tournament_id)}')

for count, tournament in enumerate(all_tournaments):
    logging.info(f'Checking tournament number {str(count)}')
    logging.info(f'Tournament ID is {str(tournament.tournament_id)}')
    found_country_players = []
    for tour_player in tournament.players:
        if tour_player in all_pdga_numbers:
            found_country_players.append(tour_player)

    if len(found_country_players) > 0:
        t = Tournament.objects.filter(tournament_id=tournament.tournament_id).first()

        logging.info(f'Found {str(len(found_country_players))} Finnish players in this tournament')
        for div in t.divisions:
            for p in div.players:
                if p.pdga_number_1 in found_country_players:
                    for r in p.rounds:
                        if r.round_rating is not None:
                            if len(dict.keys()) < 11:
                                dict[r.round_rating] = {"tournament": t.tournament_name, "player_name": p.full_name_1, "pdga_number": p.pdga_number_1, "year": str(t.tournament_start).split('-')[0], "round_number": str(r.round_number)}
                            elif r.round_rating < 1500:
                                dict[r.round_rating] = {"tournament": t.tournament_name, "player_name": p.full_name_1, "pdga_number": p.pdga_number_1, "year": str(t.tournament_start).split('-')[0], "round_number": str(r.round_number)}
                                lowest_key = sorted(dict)[0]
                                highest_key = sorted(dict)[-1]
                                dict.pop(lowest_key)
                                print(json.dumps(dict[highest_key], indent=4))

                            # if r.round_rating == highest_round_rating:
                            #     same_round_rating.append(p.pdga_number_1)
                            #     logging.info('New same highest round rating found.')
                            # elif r.round_rating > highest_round_rating and r.round_rating < 1400:
                            #     tournament_id = t.tournament_id
                            #     player_id = p.pdga_number_1
                            #     highest_round_rating = r.round_rating
                            #     same_round_rating = []
                            #     logging.info(f'Highest round rating for Finland: {str(highest_round_rating)} for player {str(player_id)} in tournament with ID: {str(tournament_id)}')

print(json.dumps(dict, indent=4))


import asyncio
import tempfile
import shutil
import os
from torba.testcase import AsyncioTestCase
from lbrynet.conf import Config
from lbrynet.extras.daemon.storage import SQLiteStorage
from lbrynet.blob.blob_manager import BlobFileManager

class TestBlobfile(AsyncioTestCase):
    async def test_create_blob(self):
        blob_hash = "7f5ab2def99f0ddd008da71db3a3772135f4002b19b7605840ed1034c8955431bd7079549e65e6b2a3b9c17c773073ed"
        blob_bytes = b'1' * ((2 * 2 ** 20) - 1)

        loop = asyncio.get_event_loop()
        tmp_dir = tempfile.mkdtemp()
        self.addCleanup(lambda: shutil.rmtree(tmp_dir))

        storage = SQLiteStorage(Config(), os.path.join(tmp_dir, "lbrynet.sqlite"))
        blob_manager = BlobFileManager(loop, tmp_dir, storage)

        await storage.open()
        await blob_manager.setup()

        # add the blob on the server
        blob = blob_manager.get_blob(blob_hash, len(blob_bytes))
        self.assertEqual(blob.get_is_verified(), False)
        self.assertNotIn(blob_hash, blob_manager.completed_blob_hashes)

        writer = blob.open_for_writing()
        writer.write(blob_bytes)
        await blob.finished_writing.wait()
        self.assertTrue(os.path.isfile(blob.file_path), True)
        self.assertEqual(blob.get_is_verified(), True)
        self.assertIn(blob_hash, blob_manager.completed_blob_hashes)


'''s3.py'''
import os

import boto3
from cloudwedge.utils.logger import get_logger

# Setup logger
LOGGER = get_logger('util.s3')

RESOURCE_S3 = None

def s3_save_object(session, bucket: str, key: str, content=None):
    '''Save the contents to the given target object'''

    global RESOURCE_S3

    if not RESOURCE_S3:
        RESOURCE_S3 = session.resource('s3')

    s3_object = RESOURCE_S3.Object(bucket, key)

    try:
        s3_object.put(Body=content)

        return s3_object.key

    except Exception as err:
        raise err


# Copyright (C) 2019 Google Inc.
# Licensed under http://www.apache.org/licenses/LICENSE-2.0 <see LICENSE file>

"""Module contains BeforeFlushHandleable mixin."""

from sqlalchemy import event
from sqlalchemy.orm import session

class BeforeFlushHandleable(object):
  """A mixin for handling before_flush sqlalchemy events.

  Used if you need to handle object in a before flush state.
  e.g if needed to validate object when all fields are filled.
  """

  # pylint: disable=too-few-public-methods
  def handle_before_flush(self):
    """Override with custom handling"""
    raise NotImplementedError()

  @classmethod
  def _loop_handler(cls, alchemy_session, flush_context, objects):
    """loop thought all object in session and call handler"""
    # pylint: disable=unused-argument

    for obj in alchemy_session:
      if isinstance(obj, BeforeFlushHandleable):
        obj.handle_before_flush()

# pylint: disable=protected-access
event.listen(session.Session, 'before_flush',
             BeforeFlushHandleable._loop_handler)


import logging
from functools import partial
import numpy

from . import evaluation
from .. import quadrature

def find_interior_point(
        distribution,
        parameters=None,
        cache=None,
        iterations=1000,
        retall=False,
        seed=None,
):
    """
    Find interior point of the distribution where forward evaluation is
    guarantied to be both ``distribution.fwd(xloc) > 0`` and
    ``distribution.fwd(xloc) < 1``.

    Args:
        distribution (Dist): Distribution to find interior on.
        parameters (Optional[Dict[Dist, numpy.ndarray]]): Parameters for the
            distribution.
        cache (Optional[Dict[Dist, numpy.ndarray]]): Memory cache for the
            location in the evaluation so far.
        iterations (int): The number of iterations allowed to be performed
        retall (bool): If provided, lower and upper bound which guaranties that
            ``distribution.fwd(lower) == 0`` and
            ``distribution.fwd(upper) == 1`` is returned as well.
        seed (Optional[int]): Fix random seed.

    Returns:
        numpy.ndarray: An input array with shape ``(len(distribution),)`` which
        is guarantied to be on the interior of the probability distribution.

    Example:
        >>> distribution = chaospy.MvNormal([1, 2, 3], numpy.eye(3)+.03)
        >>> midpoint, lower, upper = find_interior_point(
        ...     distribution, retall=True, seed=1234)
        >>> print(lower.T)
        [[-64. -64. -64.]]
        >>> print(numpy.around(midpoint, 4).T)
        [[  0.6784 -33.7687 -19.0182]]
        >>> print(upper.T)
        [[16. 16. 16.]]
        >>> distribution = chaospy.Uniform(1000, 1010)
        >>> midpoint, lower, upper = find_interior_point(
        ...     distribution, retall=True, seed=1234)
        >>> print(numpy.around(lower, 4))
        [[-1.]]
        >>> print(numpy.around(midpoint, 4))
        [[1009.8873]]
        >>> print(numpy.around(upper, 4))
        [[1024.]]
    """
    random_state = numpy.random.get_state()
    numpy.random.seed(seed)

    forward = partial(evaluation.evaluate_forward, cache=cache,
                      distribution=distribution, parameters=parameters)

    dim = len(distribution)
    upper = numpy.ones((dim, 1))
    for _ in range(100):
        indices = forward(x_data=upper) < 1
        if not numpy.any(indices):
            break
        upper[indices] *= 2

    lower = -numpy.ones((dim, 1))
    for _ in range(100):
        indices = forward(x_data=lower) > 0
        if not numpy.any(indices):
            break
        lower[indices] *= 2

    for _ in range(iterations):

        rand = numpy.random.random(dim)
        proposal = (rand*lower.T + (1-rand)*upper.T).T
        evals = forward(x_data=proposal)

        indices0 = evals > 0
        indices1 = evals < 1

        range_ = numpy.random.choice(dim, size=dim, replace=False)

        upper_ = numpy.where(indices1, upper, evals)
        for idx in range_:
            if upper.flatten()[idx] == upper_.flatten()[idx]:
                continue
            if numpy.all(forward(x_data=upper_) == 1):
                upper = upper_
                break
            upper_[idx] = upper[idx]

        lower_ = numpy.where(indices0, lower, evals)
        for idx in range_:
            if lower.flatten()[idx] == lower_.flatten()[idx]:
                continue
            if numpy.all(forward(x_data=lower_) == 0):
                lower = lower_
                break
            lower_[idx] = lower[idx]

        if numpy.all(indices0 & indices1):
            break

    else:
        if retall:
            return proposal, lower, upper
        return proposal
        raise evaluation.DependencyError(
            "Too many iterations required to find interior point.")

    numpy.random.set_state(random_state)
    if retall:
        return proposal, lower, upper
    return proposal

def approximate_inverse(
        distribution,
        qloc,
        parameters=None,
        cache=None,
        iterations=100,
        tol=1e-5,
        seed=None,
):
    """
    Calculate the approximation of the inverse Rosenblatt transformation.

    Uses forward Rosenblatt, probability density function (derivative) and
    boundary function to apply hybrid Newton-Raphson and binary search method.

    Args:
        distribution (Dist): Distribution to estimate inverse Rosenblatt.
        qloc (numpy.ndarray): Input values. All values must be on [0,1] and
            ``qloc.shape == (dim,size)`` where dim is the number of dimensions
            in distribution and size is the number of values to calculate
            simultaneously.
        parameters (Optional[Dict[Dist, numpy.ndarray]]): Parameters for the
            distribution.
        cache (Optional[Dict[Dist, numpy.ndarray]]): Memory cache for the
            location in the evaluation so far.
        iterations (int): The number of iterations allowed to be performed
        tol (float): Tolerance parameter determining convergence.
        seed (Optional[int]): Fix random seed.

    Returns:
        numpy.ndarray: Approximation of inverse Rosenblatt transformation.

    Example:
        >>> distribution = chaospy.Normal(1000, 10)
        >>> qloc = numpy.array([[0.1, 0.2, 0.9]])
        >>> print(numpy.around(approximate_inverse(
        ...     distribution, qloc, seed=1234), 4))
        [[ 987.1845  991.5838 1012.8155]]
        >>> print(numpy.around(distribution.inv(qloc), 4))
        [[ 987.1845  991.5838 1012.8155]]
    """
    logger = logging.getLogger(__name__)
    logger.debug("init approximate_inverse: %s", distribution)

    # lots of initial values:
    xloc, xlower, xupper = find_interior_point(
        distribution, cache=cache, parameters=parameters, retall=True, seed=seed)
    xloc = (xloc.T * numpy.zeros(qloc.shape).T).T
    xlower = (xlower.T + numpy.zeros(qloc.shape).T).T
    xupper = (xupper.T + numpy.zeros(qloc.shape).T).T
    uloc = numpy.zeros(qloc.shape)
    ulower = -qloc
    uupper = 1-qloc
    indices = numpy.ones(qloc.shape[-1], dtype=bool)

    for idx in range(2*iterations):

        # evaluate function:
        uloc[:, indices] = (evaluation.evaluate_forward(
            distribution, xloc, cache=cache, parameters=parameters)-qloc)[:, indices]

        # convergence criteria:
        indices[indices] = numpy.any(numpy.abs(xupper-xlower) > tol, 0)[indices]
        logger.debug(
            "iter: %s : %s : %s (%s)",
            numpy.mean(xlower, -1),
            numpy.mean(xloc, -1),
            numpy.mean(xupper, -1),
            numpy.mean(indices),
        )
        if not numpy.any(indices):
            break

        # narrow down lower boundary:
        ulower[:, indices] = numpy.where(uloc <= 0, uloc, ulower)[:, indices]
        xlower[:, indices] = numpy.where(uloc <= 0, xloc, xlower)[:, indices]

        # narrow down upper boundary:
        uupper[:, indices] = numpy.where(uloc >= 0, uloc, uupper)[:, indices]
        xupper[:, indices] = numpy.where(uloc >= 0, xloc, xupper)[:, indices]

        # Newton increment every second iteration:
        xloc_ = numpy.inf
        if idx % 2 == 0:
            derivative = evaluation.evaluate_density(
                distribution, xloc, cache=cache, parameters=parameters)[:, indices]
            derivative = numpy.where(derivative, derivative, numpy.inf)

            xloc_ = xloc[:, indices] - uloc[:, indices] / derivative

        # use binary search if Newton increment is outside bounds:
        xloc[:, indices] = numpy.where(
            (xloc_ < xupper[:, indices]) & (xloc_ > xlower[:, indices]),
            xloc_, 0.5*(xupper+xlower)[:, indices])

    else:
        logger.warning(
            "Too many iterations required to estimate inverse.")
        logger.info("{} out of {} did not converge.".format(
            numpy.sum(indices), len(indices)))
    logger.debug("end approximate_inverse: %s", distribution)
    return xloc

def approximate_moment(
        dist,
        K,
        retall=False,
        control_var=None,
        rule="F",
        order=1000,
        **kws
):
    """
    Approximation method for estimation of raw statistical moments.

    Args:
        dist : Dist
            Distribution domain with dim=len(dist)
        K : numpy.ndarray
            The exponents of the moments of interest with shape (dim,K).
        control_var : Dist
            If provided will be used as a control variable to try to reduce
            the error.
        acc (:py:data:typing.Optional[int]):
            The order of quadrature/MCI
        sparse : bool
            If True used Smolyak's sparse grid instead of normal tensor
            product grid in numerical integration.
        rule : str
            Quadrature rule
            Key     Description
            ----    -----------
            "G"     Optimal Gaussian quadrature from Golub-Welsch. Slow for
                    high order.
            "E"     Gauss-Legendre quadrature
            "C"     Clenshaw-Curtis quadrature. Exponential growth rule is
                    used when sparse is True to make the rule nested.

            Monte Carlo Integration
            Key     Description
            ----    -----------
            "H"     Halton sequence
            "K"     Korobov set
            "L"     Latin hypercube sampling
            "M"     Hammersley sequence
            "R"     (Pseudo-)Random sampling
            "S"     Sobol sequence

        antithetic (:py:data:typing.Optional[numpy.ndarray]):
            List of bool. Represents the axes to mirror using antithetic
            variable during MCI.
    """
    dim = len(dist)
    shape = K.shape
    size = int(K.size/dim)
    K = K.reshape(dim, size)

    if dim > 1:
        shape = shape[1:]

    X, W = quadrature.generate_quadrature(order, dist, rule=rule, **kws)

    grid = numpy.mgrid[:len(X[0]), :size]
    X = X.T[grid[0]].T
    K = K.T[grid[1]].T
    out = numpy.prod(X**K, 0)*W

    if control_var is not None:

        Y = control_var.ppf(dist.fwd(X))
        mu = control_var.mom(numpy.eye(len(control_var)))

        if (mu.size == 1) and (dim > 1):
            mu = mu.repeat(dim)

        for d in range(dim):
            alpha = numpy.cov(out, Y[d])[0, 1]/numpy.var(Y[d])
            out -= alpha*(Y[d]-mu)

    out = numpy.sum(out, -1)
    return out

def approximate_density(
        dist,
        xloc,
        parameters=None,
        cache=None,
        eps=1.e-7
):
    """
    Approximate the probability density function.

    Args:
        dist : Dist
            Distribution in question. May not be an advanced variable.
        xloc : numpy.ndarray
            Location coordinates. Requires that xloc.shape=(len(dist), K).
        eps : float
            Acceptable error level for the approximations
        retall : bool
            If True return Graph with the next calculation state with the
            approximation.

    Returns:
        numpy.ndarray: Local probability density function with
            ``out.shape == xloc.shape``. To calculate actual density function,
            evaluate ``numpy.prod(out, 0)``.

    Example:
        >>> distribution = chaospy.Normal(1000, 10)
        >>> xloc = numpy.array([[990, 1000, 1010]])
        >>> print(numpy.around(approximate_density(distribution, xloc), 4))
        [[0.0242 0.0399 0.0242]]
        >>> print(numpy.around(distribution.pdf(xloc), 4))
        [[0.0242 0.0399 0.0242]]
    """
    if parameters is None:
        parameters = dist.prm.copy()
    if cache is None:
        cache = {}

    xloc = numpy.asfarray(xloc)
    lo, up = numpy.min(xloc), numpy.max(xloc)
    mu = .5*(lo+up)
    eps = numpy.where(xloc < mu, eps, -eps)*xloc

    floc = evaluation.evaluate_forward(
        dist, xloc, parameters=parameters.copy(), cache=cache.copy())
    for d in range(len(dist)):
        xloc[d] += eps[d]
        tmp = evaluation.evaluate_forward(
            dist, xloc, parameters=parameters.copy(), cache=cache.copy())
        floc[d] -= tmp[d]
        xloc[d] -= eps[d]

    floc = numpy.abs(floc / eps)
    return floc


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

from tableaux2D import *
from string import *

# lecture/ecriture de fichier pgm binaire

def creer_fichier(l,h,nom,type):
    file = open(nom,'w')
    file.write(type + '\n')
    file.write(str(l) + ' ' + str(h) + '\n')
    file.write('255 \n')
    return file

def creer_image_pgm_binaire(t, nom):
    h = len(t)
    l = len(t[0])
    file =creer_fichier(l,h,nom,'P5')
    for i in range(h):
        for j in range(l):
            file.write('%c'%t[i][j]) #transforme un entier entre 0 et 255 en octet
    file.close()

def lire_fichier_pgm_binaire (nom):
    f = open (nom)

  #lecture du "magic number" P5
    mode = f.readline()

  #lecture des dimensions (il faut sauter les éventuelles lignes de commentaire)
    dim = f.readline()
    while dim[0] == '#':
        dim = f.readline()
    t_dim = split(dim)
    largeur = int(t_dim[0])
    hauteur = int(t_dim[1])

  # création du tableau image
    t_image = creer_tableau(hauteur, largeur, 0)

  # lecture de la valeur maximale d'un niveau de gris (255)
    val_max = f.readline()

    for i in range(hauteur):
        for j in range(largeur):
            t_image[i][j] = ord(f.read(1)) # transforme un octet en entier

    f.close()
    return t_image


#This Document Made for String without String And helps to understand What is Object

class MyString:
    str1 = []
    str_len1 = 0
    def __init__(self, str):
        self.str1 = str
        i =0
        for a in self.str1:
            i = i+1
        self.str_len1 = i

    def cal_len(self):
        return self.str_len1

    def println_str(self):
        for i in self.str1:
            print(i, end='') #End substitue \n to ''
        print(end = '\n')

    def Append_str(self, a):
        self.str1 = self.str1+a
        i =0
        for a in self.str1:
            i = i+1
        self.str_len1 = i

    def Insert_str(self, str, loc):
        i=0
        for a in self.str1:
            i = i+1
            if(i==loc):
                break
        str_temp = self.str1[i:]
        self.str1 = self.str1[:i]+str
        self.str1 = self.str1+str_temp
        for a in self.str1:
            i = i+1
        self.str_len1 = i

if __name__ == '__main__':
    print(dict(String))
    String1 = MyString("hello")

    print("String is {string} and Length is {Length}".format(string= String1.str1, Length = String1.str_len1))
    print("Type of Mystring is {Type}".format(Type = type(MyString.str1)))
    String1.println_str()
    String1.Append_str(" World")
    print("String is {string} and Length is {Length}".format(string=String1.str1, Length=String1.str_len1))
    String1.Insert_str(" Beautiful ", 5)
    print("String is {string} and Length is {Length}".format(string=String1.str1, Length=String1.str_len1))


import string
from Nested import Nested
from Tkinter import *

class IfStatement(Nested):
    def __init__(self, x, y, condition="", do=[], width=100, height=25):
        self.x, self.y = x, y
        self.width = width
        self.height = height
        self.condition = condition
        self.do = do
        self.depth = 0
        self.selected = False
        v1 = StringVar()
        self.conditionInput = Entry(width=28,textvariable=v1)
        v1.set(self.condition)

    def draw(self, canvas):
        outlineColor = "black"
        if(self.selected):
            outlineColor = "red"
        canvas.create_rectangle(self.x-self.width,self.y-self.height, self.x+self.width, self.y+self.height, fill="CadetBlue1", outline=outlineColor)
        canvas.create_text(self.x-self.width+5, self.y-self.height+5, text="if: ", anchor="nw", font="Arial 10 bold")
        canvas.create_window(self.x-self.width+25, self.y-self.height+1, window=self.conditionInput, anchor="nw")
        canvas.create_line(self.x-self.width, self.y-self.height+20, self.x+self.width, self.y-self.height+20, width="1")
        canvas.create_text(self.x-self.width+5, self.y-self.height+25, text="do", anchor="nw", font="Arial 10 bold")
        for i in xrange(len(self.do)):
            self.do[i].draw(canvas)

    def __repr__(self):
        string = "IfStatement(x=" + repr(self.x) + ",y=" + repr(self.y) + ",condition=" + repr(self.condition)
        string += ",do=" + repr(self.do) + ",width=" + repr(self.width) + ",height=" + repr(self.height) + ")"
        return string

    def __str__(self):
        self.condition = self.conditionInput.get()
        s = "if (" + self.condition + "):\n"
        for i in xrange(len(self.do)):
            self.do[i].depth = self.depth + 1
            s += self.depth*"    " +str(self.do[i])
            s += "\n" if (i != len(self.do) - 1) else ""
        return s


'''Crie um programa que leia vários números inteiros pelo teclado. O programa só vai parar quando o
usuário digitar o valor 999, que é a condição de parada. No final, mostre quantos números foram digitados
e qual foi a soma entre eles. (desconsiderando o flag).'''

s = cont = 0
while True:
    num = int(input('Digite um valor: '))
    if num != 999:
        s +=num
        cont += 1
    else:
        break
print(f'A soma dos {cont} digitados, é {s} ')


import const
from sklearn.metrics import f1_score
from sklearn.metrics import accuracy_score
import collections
from sklearn.model_selection import KFold
import numpy as np
from matplotlib import pyplot as plt
import pandas as pd
# import time

def evaluation(y_predict, y_true, method):
    """
    return evaluation result
    """
    if method == const.f1:
        # macro, unweighted avg of each class f1
        return f1_score(y_true, y_predict, average='macro')
    elif method == const.f1_micro:
        # micro, weighted avg of each class f1
        return f1_score(y_true, y_predict, average='micro')
    elif method == const.accuracy:
        return accuracy_score(y_true, y_predict)
    else:
        raise ValueError(f"{method} not supported yet")

def single_set_evaluation(train_x, test_x, train_y, test_y, learners, evaluation_methods):
    """
    return 2 dicts, training and testing , key is method, and value is a list, the list contains each learner result.
    """
    for incident in learners:
        incident.fit(train_x, train_y)
    train_y = train_y.values
    test_y = test_y.values
    learners_test_outputs = []
    learners_train_outputs = []
    for incident in learners:
        test_output = incident.predict(test_x)
        train_output = incident.predict(train_x)
        learners_test_outputs.append(test_output)
        learners_train_outputs.append(train_output)

    test_scores = collections.defaultdict(list)
    train_scores = collections.defaultdict(list)
    for method in evaluation_methods:
        for output in learners_test_outputs:
            test_score = evaluation(output, test_y, method)
            test_scores[method].append(test_score)
        for output in learners_train_outputs:
            train_score = evaluation(output, train_y, method)
            train_scores[method].append(train_score)
    return train_scores, test_scores

def compare_learners(data_x, data_y, learners, num_fold=3, split_seed=None, evaluation_methods=const.f1,
                     shuffle=True):
    """
    return 2 lists, train list and test list, each element is a fold (evaluation) result (each element is a dict)
    learners: list of sklearn learners, it can be the same type learner but with hyperparams
    evaluation_methods: list of learning method
    """

    if not isinstance(learners, list):
        learners = [learners]
    # if not isinstance(evaluation_methods, list):
    #     evaluation_methods = [evaluation_methods]
    kf = KFold(n_splits=num_fold, shuffle=shuffle, random_state=split_seed)
    test_fold_score = []
    train_fold_score = []
    for train_idx, test_idx in kf.split(data_y):
        train_x = data_x.iloc[train_idx]
        train_y = data_y.iloc[train_idx]
        test_x = data_x.iloc[test_idx]
        test_y = data_y.iloc[test_idx]
        train_result, test_result = single_set_evaluation(train_x, test_x, train_y, test_y,
                                                          learners, evaluation_methods)
        test_fold_score.append(test_result)
        train_fold_score.append(train_result)
    return train_fold_score, test_fold_score

def learning_curve(data_x, data_y, learner, test_ratio: list, evaluation_methods=const.f1, shuffle=True):
    """
    test_ratio is a list of different test ratio to generate different # of testing cases vs training cases
    """
    learner = [learner]  # to fit single_set_evaluation function format
    test_ratio = sorted(test_ratio, reverse=True)  # we want less training cases at the beginning
    data_length = data_x.shape[0]
    idx = np.arange(data_length)
    test_fold_score = []
    train_fold_score = []
    if shuffle:
        np.random.shuffle(idx)

    for tr in test_ratio:
        train_length = int(np.round(data_length*(1-tr)))
        train_idx = idx[: train_length]
        test_idx = idx[train_length:]
        train_x = data_x.iloc[train_idx]
        # try:
        #     train_y = data_y.iloc[train_idx]
        # except:
        #     pass
        train_y = data_y.iloc[train_idx]
        test_x = data_x.iloc[test_idx]
        test_y = data_y.iloc[test_idx]
        # start = time.time()
        train_result, test_result = single_set_evaluation(train_x, test_x, train_y, test_y,
                                                          learner, evaluation_methods)
        # print(f"{type(learner[0])}: {time.time()-start}")
        test_fold_score.append(test_result)
        train_fold_score.append(train_result)
    return train_fold_score, test_fold_score

def avg_score(train_score: list, test_score: list):
    """
    input is 2 lists, train list and test list, each element is a dict, which is a fold (evaluation) result

    each method has avg train score, test score among folds
    return 2 dicts, key is method, value is 1 list, a list
    """
    fold_num = len(train_score)
    avg_train = {}
    avg_test = {}
    evaluation_methods = train_score[0].keys()

    for method in evaluation_methods:
        train_learners_score_list = []
        test_learners_score_list = []
        for i in range(fold_num):
            train_learners_score_list.append(train_score[i][method])
            test_learners_score_list.append(test_score[i][method])
        train_avg = np.average(train_learners_score_list, axis=0)
        test_avg = np.average(test_learners_score_list, axis=0)
        avg_train[method] = train_avg
        avg_test[method] = test_avg

    return avg_train, avg_test

def learner_compare_graphing(train_score, test_score, x_list, evaluation_methods, topic):
    avg_train, avg_test = avg_score(train_score, test_score)

    consolidate = {}
    for eva_method in evaluation_methods:
        train_result = avg_train[eva_method]
        test_result = avg_test[eva_method]
        both_result = np.array([train_result, test_result])
        both_result = pd.DataFrame(both_result.transpose(), columns=[const.train, const.test])
        consolidate[eva_method] = both_result
    plot_result(consolidate, topic, x_list, [const.train, const.test])

def plot_result(scores, topic, x_points=None, label=None, save=True):

    for key, value in scores.items():
        ploting_individual_method(value, topic + f'_{key}', x_points, label, save)

def ploting_individual_method(scores, title, x_points=None, label=None, save=True):
    if x_points is None:
        x_points = np.arrange(len(scores))
    plt.plot(x_points, scores)
    plt.legend(label)
    plt.title(title)
    if save:
        plt.savefig(title + ".png")
    else:
        plt.show()
    plt.close()

def build_learning_score(scores, evaluation_methods):
    """
    consolidate to {method1: score list, method2: score list}
    """
    consolidate_dict = {}
    for method in evaluation_methods:
        consolidate_dict[method] = []
    for score in scores:
        for method in evaluation_methods:
            consolidate_dict[method].append(score[method][0])

    return consolidate_dict

def learning_curve_graphing(train_score, test_score, x_list, evaluation_methods, topic):
    consolidate = {}
    for eva_method in evaluation_methods:
        train_result = train_score[eva_method]
        test_result = test_score[eva_method]
        both_result = np.array([train_result, test_result])
        both_result = pd.DataFrame(both_result.transpose(), columns=[const.train, const.test])
        consolidate[eva_method] = both_result
    plot_result(consolidate, topic, x_list, [const.train, const.test])


import sys
sys.stdin = open('input.txt','r')

N, M = map(int,input().split())
pole, height = [], 0
for a in range(N):
    l = list(map(int,input().split()))
    if max(l) > height : height = max(l)
    pole.append(l)

dx, dy = [0,1,0,-1], [1,0,-1,0]
def melt():
    global pole
    melted_temp = [[0]*M for _ in range(N)]
    bool = False
    for i in range(N):
        for j in range(M):
            if pole[i][j] :
                bool = True
                cntzero = 0
                for d in range(4):
                    if pole[i+dy[d]][j+dx[d]] == 0: cntzero += 1
                melted_temp[i][j] = pole[i][j] - cntzero
                melted_temp[i][j] = 0 if melted_temp[i][j] < 0 else melted_temp[i][j]
    if not bool : return False
    else : pole = [ a[:] for a in melted_temp ]

def bfs(i,j):
    q = [(i,j)]
    while q :
        cur = q.pop()
        cy, cx = cur[0], cur[1]
        for d in range(4):
            ny, nx = cy+dy[d] , cx+dx[d]
            if pole[ny][nx] and visited[ny][nx] == 0 :
                q.append((ny,nx))
                visited[ny][nx] = 1

res = 0
for y in range(1,900):
    if melt() == False : break
    else: pass

    visited = [[0]*M for _ in range(N)]
    cnt, run = 0, True
    for i in range(N):
        for j in range(M):
            if i == N-1 and j == M-1 :
                run = False
                break
            if pole[i][j] and not visited[i][j] :
                visited[i][j] = 1
                bfs(i,j)
                cnt += 1
        if not run: break
    if cnt > 1 :
        res = y
        break
print(res)


#!/usr/bin/python

import os
import typing

from pdf_conversion.documents.file_extensions import SupportedDocTypes

class DocumentInfo:
    """
    Stores basic information about source PDF file, and all conversion info (type, time elapsed during the
    conversion process, resulting intermediary and final image locations)
    """
    def __init__(self, file_spec: str, conversion_dir: str = None) -> None:
        """
        Constructor for DocumentInfo
        :param file_spec: File spec (path and filename) of original document
        :param conversion_dir: File path to store converted documents
        """
        self.filespec = os.path.abspath(file_spec)
        self.file_dir = os.path.abspath(conversion_dir) or os.path.split(self.filespec)[0]
        self.filename = self.filespec.split(os.path.sep)[-1]
        self.doc_type = self.filename.split('.')[-1].lower()
        self.files = []
        self.conversion_duration = 0

    def get_format_types(self) -> typing.List[str]:
        """
        Get the list of formats the source exists: pdf, tif, webp, etc.

        :return: List of unique file format types

        """
        formats = list(set([filename.split('.')[-1] for filename in self.files]))
        formats.extend([self.filename.split('.')[-1]])
        return formats

    def _return_list_of_filespecs_of_file_format(self, file_format: SupportedDocTypes) -> typing.List[str]:
        """
        Return a list of all file specs matching the desired file format.

        :param file_format: The file format to list

        :return: List of files matching the file format (extension)

        """
        return [file_ for file_ in self.files if file_.lower().endswith(file_format.value.lower())]

    @property
    def tiff(self) -> typing.List[str]:
        """
        Return all file specs that are in the TIFF format.

        :return: List of file specs for TIFF images.

        """
        return self._return_list_of_filespecs_of_file_format(SupportedDocTypes.TIFF)

    @property
    def webp(self) -> typing.List[str]:
        """
        Return all file specs that are in the webp format.

        :return: List of file specs for webp images.

        """
        return self._return_list_of_filespecs_of_file_format(SupportedDocTypes.WEBP)

    def document_status(self) -> str:
        output = f"SOURCE DOCUMENT: {self.filespec}\n"
        output += f"LIST OF TIFFs:\n{self.tiff}\n"
        output += f"LIST OF WEBPs:\n{self.webp}\n"
        output += f"CONVERSION DURATION: {self.conversion_duration:0.4f} seconds\n"
        output += f"CREATED DOC FORMATS: {self.get_format_types()}\n"
        return output


import datetime as dt
import fnmatch
import logging
import os
import shutil
import tempfile

from .core import Namespace
from .time import datetime_iso

log = logging.getLogger(__name__)

def to_mb(size_in_bytes):
    return size_in_bytes / (1 << 20)

def to_gb(size_in_bytes):
    return size_in_bytes / (1 << 30)

def normalize_path(path):
    return os.path.realpath(os.path.expanduser(path))

def split_path(path):
    dir, file = os.path.split(path)
    base, ext = os.path.splitext(file)
    return Namespace(dirname=dir, filename=file, basename=base, extension=ext)

def path_from_split(split, real_path=True):
    return os.path.join(os.path.realpath(split.dirname) if real_path else split.dirname,
                        split.basename)+split.extension

def dir_of(caller_file, rel_to_project_root=False):
    abs_path = os.path.realpath(os.path.dirname(caller_file))
    if rel_to_project_root:
        project_root = os.path.normpath(os.path.join(os.path.realpath(os.path.dirname(__file__)), '..', '..'))
        return os.path.relpath(abs_path, start=project_root)
    else:
        return abs_path

def list_all_files(paths, include=None, exclude=None):
    """
    :param paths: the directories to look into.
    :param include: a UNIX path pattern for the files to be included in the result list
    :param exclude: a UNIX path pattern for the files to be excluded from the result list
    """
    all_files = []
    paths = paths if isinstance(paths, list) else [paths]
    for path in paths:
        # path = normalize_path(path)
        if os.path.isdir(path):
            for root_dir, sub_dirs, files in os.walk(path):
                for name in files:
                    all_files.append(os.path.join(root_dir, name))
        elif os.path.isfile(path):
            all_files.append(path)
        else:
            log.warning("Skipping file `%s` as it doesn't exist.", path)

    if include is not None:
        include = include if isinstance(include, list) else [include]
        included = []
        for pattern in include:
            included.extend(fnmatch.filter(all_files, pattern))
        all_files = [file for file in all_files if file in included]

    if exclude is not None:
        exclude = exclude if isinstance(exclude, list) else [exclude]
        excluded = []
        for pattern in exclude:
            excluded.extend(fnmatch.filter(all_files, pattern))
        all_files = [file for file in all_files if file not in excluded]

    return all_files

def touch(path, as_dir=False):
    path = normalize_path(path)
    if not os.path.exists(path):
        dirname, basename = (path, '') if as_dir else os.path.split(path)
        if not os.path.exists(dirname):
            os.makedirs(dirname, exist_ok=True)
        if basename:
            open(path, 'a').close()
    os.utime(path, times=None)

def backup_file(file_path):
    src_path = os.path.realpath(file_path)
    if not os.path.isfile(src_path):
        return
    p = split_path(src_path)
    mod_time = dt.datetime.utcfromtimestamp(os.path.getmtime(src_path))
    dest_name = ''.join([p.basename, '_', datetime_iso(mod_time, date_sep='', time_sep=''), p.extension])
    dest_dir = os.path.join(p.dirname, 'backup')
    touch(dest_dir, as_dir=True)
    dest_path = os.path.join(dest_dir, dest_name)
    shutil.copyfile(src_path, dest_path)
    log.debug('File `%s` was backed up to `%s`.', src_path, dest_path)

class TmpDir:

    def __init__(self):
        self.tmp_dir = None

    def __enter__(self):
        self.tmp_dir = tempfile.mkdtemp()
        return self.tmp_dir

    def __exit__(self, *args):
        shutil.rmtree(self.tmp_dir)
        # pass


"""mysite URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/3.0/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.conf import settings
from django.conf.urls import handler404
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path, include
from home import views

urlpatterns = [

    path('admin/', admin.site.urls),
    path('ckeditor/', include('ckeditor_uploader.urls')),
    path('', include('home.urls')),
    path('home/', include('home.urls')),
    path('', include('blog.urls')),
    path('content/', include('content.urls')),
    path('user/', include('user.urls')),

    path('hakkimizda/', views.hakkimizda, name='hakkimizda'),
    path('hizmetlerimiz/', views.hizmetlerimiz, name='hizmetlerimiz'),
    path('projelerimiz/', views.projelerimiz, name='projelerimiz'),
    path('blog/', views.blog, name='blog'),
    path('iletisim/', views.iletisim, name='iletisim'),
    path('referanslarimiz/', views.referanslar, name='referanslarimiz'),
    path('sss/', views.faq, name='faq'),

    path('user_profile/', views.signup_view, name='signup_view'),
    path('signup/', views.signup_view, name='signup_view'),
    path('login/', views.login_view, name='login_view'),
    path('logout/', views.logout_view, name='logout_view'),
    path('error/', views.error, name='error'),

    path('blog/<slug:slug>/<int:id>/', views.blog_detail, name='blog_detail'),

    path('content/<slug:slug>/<int:id>/', views.contentdetail, name='contentdetail'),
    path('menu/<int:id>/', views.menu, name='menu'),

]

if settings.DEBUG: #new
    urlpatterns += static(settings.MEDIA_URL,document_root = settings.MEDIA_ROOT)


import os, zipfile, PyPDF2

def merge(files):
    pdfWriter = PyPDF2.PdfFileWriter()
    for pdf in files:
        pdfReader = PyPDF2.PdfFileReader(pdf)
        for page in range(pdfReader.numPages):
            pdfWriter.addPage(pdfReader.getPage(page))
    pdfOutput = open('output.pdf', 'wb')
    pdfWriter.write(pdfOutput)
    pdfOutput.close()

def split(pdf, rangeList, pageList):

    try:
        os.remove('new.zip')
    except FileNotFoundError:
        pass

    pdfReader = PyPDF2.PdfFileReader(pdf)
    downloadZip = zipfile.ZipFile('new.zip', 'a')
    added = False
    for lst in rangeList:
        pdfWriter = PyPDF2.PdfFileWriter()
        if lst[0] <= pdfReader.numPages and lst[1] <= pdfReader.numPages:
            added = True
            for pageNo in range(lst[0]-1, lst[1]):
                pdfWriter.addPage(pdfReader.getPage(pageNo))
            pdfOutput = open('temp/output' + str(lst[0]) + str(lst[1]) + '.pdf', 'wb')
            pdfWriter.write(pdfOutput)
            pdfOutput.close()
            downloadZip.write(pdfOutput.name, compress_type=zipfile.ZIP_DEFLATED)

    for page in pageList:
        pdfWriter = PyPDF2.PdfFileWriter()
        if page <= pdfReader.numPages:
            added = True
            pdfWriter.addPage(pdfReader.getPage(page-1))
        pdfOutput = open('temp/output' + str(page) + '.pdf', 'wb')
        pdfWriter.write(pdfOutput)
        pdfOutput.close()
        downloadZip.write(pdfOutput.name, compress_type=zipfile.ZIP_DEFLATED)

    downloadZip.close()
    for pdf in os.listdir('temp'):
        os.remove('temp/' + pdf)

    return added

def remove(pdf, matches):
    pdfReader = PyPDF2.PdfFileReader(pdf)
    pdfWriter = PyPDF2.PdfFileWriter()
    i=0
    removed = False
    for pageNo in range(pdfReader.numPages):
        if i < len(matches) and matches[i] == pageNo + 1:
            i += 1
            removed = True
            continue
        pdfWriter.addPage(pdfReader.getPage(pageNo))
    pdfOutput = open('output.pdf', 'wb')
    pdfWriter.write(pdfOutput)
    pdfOutput.close()
    return removed

def rotate(pdf, degree):
    pdfReader = PyPDF2.PdfFileReader(pdf)
    pdfWriter = PyPDF2.PdfFileWriter()
    for pageNo in range(pdfReader.numPages):
        page = pdfReader.getPage(pageNo)
        page.rotateClockwise(degree)
        pdfWriter.addPage(page)
    pdfOutput = open('output.pdf', 'wb')
    pdfWriter.write(pdfOutput)
    pdfOutput.close()

def watermark(pdf, watermark_file, typ):
    pdfReader = PyPDF2.PdfFileReader(pdf)
    watermarkReader = PyPDF2.PdfFileReader(watermark_file)
    pdfWriter = PyPDF2.PdfFileWriter()
    pageObj = watermarkReader.getPage(0)
    if typ == 'first':
        page = pdfReader.getPage(0)
        page.mergePage(pageObj)
        pdfWriter.addPage(page)
        for pageNo in range(1, pdfReader.getNumPages()):
            pdfWriter.addPage(pdfReader.getPage(pageNo))
    elif typ == 'all':
        for pageNo in range(pdfReader.getNumPages()):
            page = pdfReader.getPage(pageNo)
            page.mergePage(pageObj)
            pdfWriter.addPage(page)
    pdfOutput = open('output.pdf', 'wb')
    pdfWriter.write(pdfOutput)
    pdfOutput.close()

def encrypt(pdf, password):
    pdfReader = PyPDF2.PdfFileReader(pdf)
    pdfWriter = PyPDF2.PdfFileWriter()
    for pageNo in range(pdfReader.getNumPages()):
        pdfWriter.addPage(pdfReader.getPage(pageNo))
    pdfWriter.encrypt(password)
    pdfOutput = open('output.pdf', 'wb')
    pdfWriter.write(pdfOutput)
    pdfOutput.close()


import numpy as np
import cv2
class BinaryMaskProvider:
    def Get(self, img, kernel_size = 40):
        '''
        Turn the RGB image into grayscale before
        applying an Otsu threshold to obtain a
        binary segmentation
        '''

        blurred_img = cv2.GaussianBlur(img,(1,1),0)
        if (len(blurred_img.shape) > 2): #if many chanells
            blurred_img = cv2.cvtColor(blurred_img, cv2.COLOR_RGBA2GRAY)
        ret, otsu = cv2.threshold(blurred_img, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)

        kernel = np.ones((kernel_size,kernel_size),np.uint8)
        closed_mask = cv2.morphologyEx(otsu, cv2.MORPH_CLOSE, kernel)
        return closed_mask


import sys
import re

def main(inputFileName):
 #   string = "11.22.33.44:2343"
    pattern = "(\d+)\.(\d+)\.(\d+)\.(\d+):(\d+)(.*)"

    with open(inputFileName) as f:
        line_list = f.readlines()

    for string in line_list:
        obj = re.match(pattern, string)
        if obj:
            if obj.group(6):
                print(string[:-1], "- Invalid Port Number")
                continue
            val1, val2, val3, val4, port = obj.group(1,2,3,4,5)
            val1, val2, val3, val4, port = int(val1), int(val2), int(val3), int(val4), int(port)
            if val1 > 255 or val2 > 255 or val3 > 255 or val4 > 255:
                print(string[:-1], "- Invalid IP Address")
            elif port > 32767:
                print(string[:-1], "- Invalid Port Number")
            elif port > 1024:
                print(string[:-1], "- Valid")
            else:
                print(string[:-1], "- Valid (root privileges required)")

        else: # handles mismatch of format (including negative sign)
            print(string[:-1], "- Invalid IP Address")

if __name__ == "__main__":
    if len(sys.argv) != 2:
        print("Sorry. Must have 2 arguments")
    else:
        main(sys.argv[1])


import cv2
import numpy as np

def canny(image):
    gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
    blur = cv2.GaussianBlur(gray, (5,5), 0)
    canny = cv2.Canny(blur, 50, 150)
    return canny

def region_of_interest(image):
    height = image.shape[0]
    polygons = np.array([
        [(200, height), (1100, height), (550, 250)]
    ])#get the polygon array
    mask = np.zeros_like(image)#make a black mask of the size of the image
    cv2.fillPoly(mask, polygons, 255)
    masked_image = cv2.bitwise_and(image, mask)
    return masked_image

def display_lines(image, lines):
    line_image = np.zeros_like(image)
    if lines is not None:
        for line in lines:
            x1, y1, x2, y2 = line.reshape(4)
            cv2.line(line_image, (x1, y1), (x2, y2), (255, 0, 0), 10)
    return line_image
'''
def display_lines(image, lines):
    line_image = np.zeros_like(image)
    if lines is not None:
        for line in lines:
            x1,y1,x2,y2=line.reshape(4)
            cv2.line(line_image,(x1,y1),(x2,y2),(255,0,0),10)
            #its a BGR color, hence blue, 10 is the line thickness
    return line_image
'''
image = cv2.imread('test_image.jpg')
lane_image = np.copy(image)
canny = canny(lane_image)
cropped_image = region_of_interest(canny)#get a image of the road(cropped for the road part)
#using hough transformation
lines = cv2.HoughLinesP(cropped_image, 2, np.pi/180, 100, np.array([]), minLineLength = 40, maxLineGap = 5),
#2 as size of the 'bins', 100 as the threshold of the votes
#a line has to atleast has 40 pixels and the gap between segments should not exceed 5 pixels
line_image = display_lines(lane_image, lines)
cv2.imshow('result', line_image)
cv2.waitKey(0)


#
#  Make a SOAP request with Python
#

from zeep import Client
from zeep.helpers import serialize_object
import requests
import re

#
#  APPROACH 1: Using `zeep` module
#
wsdl = 'http://dneonline.com/calculator.asmx?wsdl'

client = Client(wsdl)

response = serialize_object(
    client.service.Add(intA=5, intB=7)
)

print(response)

#
#  APPROACH 2: Using raw XML
#

xml = """
    <?xml version=\'1.0\' encoding=\'utf-8\'?>
    <soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
    <soap-env:Body>
    <ns0:Add xmlns:ns0="http://tempuri.org/">
    <ns0:intA>5</ns0:intA>
    <ns0:intB>7</ns0:intB>
    </ns0:Add>
    </soap-env:Body>
    </soap-env:Envelope>
"""

xml = re.sub(r'\n\s*', '', xml)

r = requests.post(wsdl, data=xml, headers={'Content-Type': 'text/xml'})

print(r.text)


import io
import sys
import unittest

import ex0

class ExerciseTests(unittest.TestCase):

    @classmethod
    def setUpClass(self):
        self.file = io.StringIO()
        sys.stdout = self.file
        sys.stderr = self.file

    @classmethod
    def tearDownClass(self):
        # I could do something with self.file here...
        sys.stdout = sys.__stdout__
        sys.stderr = sys.__stderr__

    def test_success(self):
        ex0.displayASCII()


#反射说简单点 --> 就是利用字符串的形式去对象(模块)中操作(寻找/检查/删除/设置)成员。

import commons
def run():
    #利用字符串的形式去对象(模块)中操作(寻找/检查/删除/设置)成员 反射
    imp = input('请输入一个方法:')   #比如login
    # delattr()  删除内存里面的方法
    # setattr()  #设置内存里面的方法
    if hasattr(commons,imp):  #判断有没有这个方法
       fuc = getattr(commons,imp)  #获得这个方法
       fuc()   #会调用 commons文件里面的方法
    else:
        print('404')

run()


from django.conf.urls import url

from .views import table_department, create_comment, post_like, comment_like

urlpatterns = [
    url(r'comment/(?P<id>\d+)', create_comment, name='comment'),
    url(r'comment_like/(?P<id>\d+)', comment_like, name='comment_like'),
    url(r'post_like/(?P<id>\d+)', post_like, name='post_like'),
    url(r'^(?P<id>\d+)', table_department, name='forum'),

]


import argparse

from . import DOIT_CONFIG

parser = argparse.ArgumentParser(prog="qpub")
parser.add_argument(
    "-a",
    "--actions",
    choices="configure docs list test install all".split(),
    default="all",
    type=str,
    required=False,
    help="the kind of actions you want to run",
)

def load_tasks(a="all"):
    all = a == "all"
    object = {}

    if all or a == "configure":
        from . import configure

        object.update(vars(configure))
    if all or a == "docs":
        from . import docs

        object.update(vars(docs))
    if all or a == "test":
        from . import test

        object.update(vars(test))
    if all or a == "install":
        from . import install

        object.update(vars(install))
    if all or a == "lint":
        from . import lint

        object.update(vars(lint))

    import doit

    class Reporter(doit.reporter.ConsoleReporter):
        def execute_task(self, task):
            self.outstream.write("MyReporter --> %s\n" % task.title())

    DOIT_CONFIG["reporter"] = Reporter

    return {
        **{k: v for k, v in object.items() if k.startswith("task_")},
        "DOIT_CONFIG": DOIT_CONFIG,
    }

def main():
    from .__init__ import main

    ns, args = parser.parse_known_args()

    if not args:
        args = ["list"]
    main(load_tasks(ns.actions), argv=args, raises=True)

if __name__ == "__main__":
    main()


import threading

import docker
from kazoo.client import KazooClient
import kazoo.exceptions
import os
import socket
import json
import docker.errors
import requests.exceptions

end = False

def sync(docker_client: docker.Client, zk: KazooClient, node_path: str, advertise_name: str):
    events = docker_client.events()
    for event_str in events:
        if end:
            break
        event = json.loads(event_str.decode('utf-8'))

        if 'Type' in event and event['Type'] == 'container' and 'id' in event:
            container_id = event['id']
            container_zk_path = "{}/{}".format(node_path, container_id)

            if 'Action' in event:
                if event['Action'] == 'destroy':
                    update_data(zk, container_zk_path, {}, advertise_name)
                else:
                    try:
                        container = docker_client.inspect_container(container_id)
                        update_data(zk, container_zk_path, container, advertise_name)
                    except docker.errors.APIError:
                        pass
                    except requests.exceptions.HTTPError:
                        pass

def main():
    global end

    root_zk_path = os.environ.get("ZK_BASE", '')
    if not root_zk_path:
        root_zk_path = '/leftstache/hive'
    root_zk_path += "/containers"

    zk_connection_string = os.environ.get("ZK_HOSTS", None)
    if not zk_connection_string:
        zk_connection_string = "localhost:2181"
    advertise_name = os.environ.get("ADVERTISE_NAME", None)
    if not advertise_name:
        advertise_name = socket.gethostname()

    docker_client = docker.from_env()
    zk = KazooClient(hosts=zk_connection_string)
    zk.start()
    try:
        try:
            zk.create("{}".format(root_zk_path), makepath=True)
        except kazoo.exceptions.NodeExistsError:
            pass

        thread = threading.Thread(target=sync, args=[docker_client, zk, root_zk_path, advertise_name])
        thread.start()

        containers = docker_client.containers(all=True)
        for container in containers:
            container = docker_client.inspect_container(container)
            container_zk_path = "{}/{}".format(root_zk_path, container['Id'])
            update_data(zk, container_zk_path, container, advertise_name)

        thread.join()
    except KeyboardInterrupt:
        print("shutting down")
        end = True
        try:
            # Force the thread to read an event so it can terminate
            container = docker_client.create_container("busybox")
            docker_client.remove_container(container)
        finally:
            docker_client.close()
    finally:
        zk.stop()

def update_data(zk: KazooClient, path: str, data: dict, advertise_name: str):
    if not data:
        try:
            print("deleting", path)
            zk.delete(path, recursive=True)
            print("deleted", path)
        except kazoo.exceptions.NoNodeError:
            print("path doesn't exist to delete", path)
    else:
        data = json.dumps({'node': advertise_name, 'data': data})
        data = data.encode('utf-8')
        print("setting", path)
        try:
            zk.create(path, data, makepath=True, ephemeral=True)
            print("created", path)
        except kazoo.exceptions.NodeExistsError:
            zk.set(path, data)
            print("updated", path)

if __name__ == "__main__":
    main()


from model.article import Article

class NewsExtractor:

    def __init__(self, link_collector, content_extractor):
        self.link_collector = link_collector
        self.content_extractor = content_extractor

    def extract(self, count):
        links = self.link_collector.collect(count)

        articles = []
        for link in links:
            content = self.content_extractor.extract(link.url)
            articles.append(Article(link.title, link.url, content.snippet))

        return articles


import torch
import torch.nn as nn
import torch.nn.functional as F

def DotScorer(dec_out, enc_outs, **kwargs):
    """
    Score for query decoder state and the ith encoder state is given
    by their dot product.
    """
    return torch.bmm(enc_outs.transpose(0, 1), dec_out.unsqueeze(2)).squeeze(2)

class GeneralScorer(nn.Module):
    """
    Inserts a linear projection to the query state before the dot product
    """
    def __init__(self, dim):
        super(GeneralScorer, self).__init__()

        self.W_a = nn.Linear(dim, dim, bias=False)

    def forward(self, dec_out, enc_outs, **kwargs):
        return DotScorer(self.W_a(dec_out), enc_outs)

class BahdanauScorer(nn.Module):
    """
    Projects both query decoder state and encoder states to an attention space.
    The scores are computed by a dot product with a learnable param v_a after
    transforming the sum of query decoder state and encoder state with a tanh.

    `score(a_i_j) = a_v \dot tanh(W_s @ h_s_j + W_t @ h_t_i)`
    """
    def __init__(self, hid_dim1, att_dim, hid_dim2=None):
        super(BahdanauScorer, self).__init__()
        # params
        hid_dim2 = hid_dim2 or hid_dim1
        self.W_s = nn.Linear(hid_dim1, att_dim, bias=False)
        self.W_t = nn.Linear(hid_dim2, att_dim, bias=True)
        self.v_a = nn.Parameter(torch.Tensor(att_dim, 1))
        self.v_a.data.uniform_(-0.05, 0.05)
        self.v_a.custom = True  # don't overwrite initialization

    def project_enc_outs(self, enc_outs):
        """
        mapping: (seq_len x batch x hid_dim) -> (seq_len x batch x att_dim)

        Returns:
        --------
        torch.Tensor (seq_len x batch x att_dim),
            Projection of encoder output onto attention space
        """
        seq_len, batch, hid_dim = enc_outs.size()
        return self.W_s(enc_outs.view(-1, hid_dim)).view(seq_len, batch, -1)

    def forward(self, dec_out, enc_outs, enc_att=None):
        if enc_att is None:
            # (seq_len x batch x att dim)
            enc_att = self.project_enc_outs(enc_outs)
        # (batch x att_dim)
        dec_att = self.W_t(dec_out)
        # (batch x seq_len x att_dim)
        dec_enc_att = F.tanh(enc_att + dec_att[None, :, :])
        # (batch x seq_len x att_dim) * (1 x att_dim x 1) -> (batch x seq_len)
        return (dec_enc_att.transpose(0, 1) @ self.v_a[None, :, :]).squeeze(2)

class Attention(nn.Module):
    """
    Global attention implementing the three scorer modules from Luong 15.

    Parameters:
    -----------
    - hid_dim: int, dimensionality of the query vector
    - att_dim: (optional) int, dimensionality of the attention space (only
        used by the bahdanau scorer). If not given it will default to hid_dim.
    - scorer: str, one of ('dot', 'general', 'bahdanau')
    - hid_dim2: (optional), int, dimensionality of the key vectors (optionally
        used by the bahdanau scorer if given)
    """
    def __init__(self, hid_dim, att_dim=None, scorer='general', hid_dim2=None):
        super(Attention, self).__init__()

        if scorer != 'bahdanau' and att_dim is not None and hid_dim != hid_dim:
            raise ValueError("Global attention requires attention size "
                             "equal to Encoder/Decoder hidden size")
        if scorer == 'bahdanau:' and att_dim is None:
            att_dim = hid_dim

        # Scorer
        if scorer.lower() == 'dot':
            self.scorer = DotScorer
        elif scorer.lower() == 'general':
            self.scorer = GeneralScorer(hid_dim)
        elif scorer.lower() == 'bahdanau':
            self.scorer = BahdanauScorer(hid_dim, att_dim, hid_dim2=hid_dim2)
        else:
            raise ValueError(
                "`scorer` must be one of ('dot', 'general', 'bahdanau')"
                " but got {}".format(scorer))

        # Output layer (Luong 15. eq (5))
        self.linear_out = nn.Linear(
            hid_dim * 2, hid_dim, bias=scorer.lower() == 'bahdanau')

    def forward(self, dec_out, enc_outs, enc_att=None, mask=None):
        """
        Parameters:
        -----------

        - dec_out: torch.Tensor(batch_size x hid_dim)
        - enc_outs: torch.Tensor(seq_len x batch_size x hid_dim)
        - enc_att: (optional), torch.Tensor(seq_len x batch_size x att_dim)
        """
        # (batch x seq_len)
        weights = self.scorer(dec_out, enc_outs, enc_att=enc_att)

        if mask is not None:
            # weights = weights * mask.float()
            weights.data.masked_fill_(1 - mask.data, -float('inf'))

        weights = F.softmax(weights, dim=1)

        # (eq 7)
        context = weights.unsqueeze(1).bmm(enc_outs.transpose(0, 1)).squeeze(1)
        # (eq 5) linear out combining context and hidden
        context = F.tanh(self.linear_out(torch.cat([context, dec_out], 1)))

        return context, weights


"""
description:
Demonstration of the cursor mechanism in the myListIter module.
author: ben k steele <bks@cs.rit.edu>
"""

import myListIter as module

def demo_cursor():
    """
    Demonstrate use of cursor to iterate through a linked list.
    """
    lst =  module.createEmptyList()
    for item in ['a', 'b', 'c']:
	    module.append( lst, item )
    print( 'list is', module.toString( lst ) )

    # reset the cursor to the start of the list
    module.reset( lst )
    print( 'iterating through list using the cursor...' )
    while module.hasNext( lst ):
	    print( 'next element is', module.next( lst ) )

demo_cursor()