from keras.applications.vgg16 import VGG16
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input
import numpy as np

import time
import matplotlib.pyplot as plt

from keras.models import Sequential, load_model, Model
from keras.layers.convolutional import Conv2D, MaxPooling2D
from keras.layers import Activation, Flatten, Dense, Dropout, GlobalAveragePooling2D
from keras.layers.normalization import BatchNormalization
from keras.utils import np_utils

from keras.datasets import cifar10
from keras.utils import plot_model
from keras.callbacks import EarlyStopping, ModelCheckpoint

# If using TF(NHWC), set image dimensions order to TH (NCHW)
from keras import backend as K
if K.backend() == 'tensorflow':
    K.set_image_dim_ordering("th")

# Use GPU for Theano, comment to use CPU instead of GPU
# Tensorflow uses GPU by default
import os
os.environ["THEANO_FLAGS"] = "mode=FAST_RUN,device=gpu,floatX=float32"

(train_features, train_labels), (test_features, test_labels) = cifar10.load_data()
num_train, img_channels, img_rows, img_cols = train_features.shape
num_test, _, _, _ =test_features.shape
num_classes = len(np.unique(train_labels))

print ("Training X:", train_features.shape)
print ("Training Y:", train_labels.shape)
print (test_features.shape)
print (test_labels.shape)
print ("Number of class:", num_classes)

# Data pre-processing
train_X = train_features.astype('float32')/255
test_X = test_features.astype('float32')/255
# convert class labels to one-hot encoding
train_y = np_utils.to_categorical(train_labels, num_classes)
test_y = np_utils.to_categorical(test_labels, num_classes)

# plot model accuracy and loss
def plot_model_history(model_history, tofile='history.png'):
    fig, axs = plt.subplots(1,2,figsize=(15,5))
    # summarize history for accuracy
    axs[0].plot(range(1,len(model_history.history['acc'])+1),model_history.history['acc'])
    axs[0].plot(range(1,len(model_history.history['val_acc'])+1),model_history.history['val_acc'])
    axs[0].set_title('Model Accuracy')
    axs[0].set_ylabel('Accuracy')
    axs[0].set_xlabel('Epoch')
    axs[0].set_xticks(np.arange(1,len(model_history.history['acc'])+1),len(model_history.history['acc'])/10)
    axs[0].legend(['train', 'val'], loc='best')
    # summarize history for loss
    axs[1].plot(range(1,len(model_history.history['loss'])+1),model_history.history['loss'])
    axs[1].plot(range(1,len(model_history.history['val_loss'])+1),model_history.history['val_loss'])
    axs[1].set_title('Model Loss')
    axs[1].set_ylabel('Loss')
    axs[1].set_xlabel('Epoch')
    axs[1].set_xticks(np.arange(1,len(model_history.history['loss'])+1),len(model_history.history['loss'])/10)
    axs[1].legend(['train', 'val'], loc='best')
    plt.savefig(tofile)
    #plt.show()

# load base model VGG16
base_model = VGG16(weights='imagenet', include_top=False)

# add a global spatial average pooling layer
x = base_model.output
x = GlobalAveragePooling2D()(x)
# add a fully-connected layer
x = Dense(1024, activation='relu')(x)
# add a logistic layer
predictions = Dense(num_classes, activation='softmax')(x)

# final model
model = Model(inputs=base_model.input, outputs=predictions)

# freeze all vgg layers
for layer in base_model.layers:
    layer.trainable = False

# compile the model (should be done *after* setting layers to non-trainable)
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])

# Plot model to image file
plot_model(model, to_file="Model_VGG16_cifar.png", show_shapes=True)

# Set callback functions to early stop training and save the best model so far
early_Stopper = EarlyStopping(monitor='val_acc', patience=50, min_delta=0,mode='auto')
model_check_point = ModelCheckpoint(filepath='Model_VGG16_cifar.h5', monitor='val_acc', save_best_only=True)

# Train the model
start = time.time()
model_info = model.fit(train_X, train_y,
                       batch_size=128, nb_epoch=200,
                       validation_data = (test_X, test_y),
                       verbose=2,
                       callbacks=[early_Stopper, model_check_point])
end = time.time()

# plot model history
plot_model_history(model_info, tofile="cifar_train_history.png")

print ("Model took %0.2f seconds to train"%(end - start))

del model
# load best model
best_model = load_model("Model_VGG16_cifar.h5")

# Evaluate model on test data
score = best_model.evaluate(test_X, test_y, verbose=0)

print ("test loss:", score[0])
print ("test accuracy:", score[1])


import torch
from torchvision import transforms
import torch.nn.functional as F

from tqdm import tqdm
import os
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image

from utils import Config
from data import get_dataloader

def generate_category_txt(dataloaders, dataset_size, model, label_enc, id_to_category, device, save_path='category.txt'):
    result=open(save_path,'w')
    model=model.to(device)
    model.eval()
    test_set = open(os.path.join(Config['root_path'],'test_category_hw.txt'),'r').readlines()
    test_set = [file.strip() for file in test_set]
    print(len(test_set),dataset_size['test'])
    transform = transforms.Compose([
                transforms.CenterCrop(224),
                transforms.ToTensor(),
                transforms.Normalize([0.5,0.5,0.5],[0.5,0.5,0.5])
            ])
    with torch.set_grad_enabled(False):
        running_corrects=0
        for file in tqdm(test_set):
            img = Image.open(os.path.join(Config['root_path'],'images',file.strip()+'.jpg'))
            img = transform(img)
            img = img.to(device)
            img = img.unsqueeze(0)
            label = id_to_category[file.strip()]
            outputs = model(img)
            # print(outputs.shape)
            _, pred = torch.max(F.softmax(outputs, 1),1)

            if int(label_enc[pred.cpu().numpy()[0]])==int(label):
              running_corrects+= 1
            # print(f'{file}, {pred.cpu().numpy()[0]}, {label}\n')
            result.write(f'{file}, {label_enc[pred.cpu().numpy()[0]]}, {label}\n')
    epoch_acc=running_corrects/len(test_set)
    print(f'Testing completed, accuracy is {epoch_acc}')
    # torch.save({
    #   'model':model.to('cpu'),
    #   'label':label_enc.classes_
    # }, Config['checkpoint_file'])
    result.close()

if __name__=='__main__':
    dataloaders, classes, dataset_size, label_enc, id_to_category = get_dataloader(debug=Config['debug'], batch_size=Config['batch_size'], num_workers=Config['num_workers'])
    device=torch.device('cuda:0' if torch.cuda.is_available() and Config['use_cuda'] else 'cpu')
    saved_model=torch.load(Config['checkpoint_file'], map_location=device)
    model = saved_model['model']
    generate_category_txt(dataloaders, dataset_size, model, saved_model['label'], id_to_category, device)


from math import sqrt
from time import time

import numpy as np
import matplotlib.pyplot as plt

from skopt.benchmarks import branin
from skopt.dummy_opt import dummy_minimize
from skopt.gp_opt import gp_minimize

dimensions = [(-5.0, 10.0), (0.0, 15.0)]
best_dummy_scores = np.zeros((5, 200))
best_gp_scores = np.zeros((5, 200))
n_iterations = range(1, 201)

for random_state in range(5):
    print("Doing a random search for the minimum.")
    t = time()
    dummy_model = dummy_minimize(
        branin, dimensions, maxiter=200, random_state=random_state)
    print(time() - t)
    print("Best score obtained, %0.4f" % dummy_model.fun)

    print("Doing a gp-based search for the minimum")
    t = time()
    gp_model = gp_minimize(
        branin, dimensions, maxiter=200, random_state=random_state, n_start=1)
    print(time() - t)
    print("Best score obtained, %0.4f" % gp_model.fun)

    for j in range(1, 201):
        best_dummy_scores[random_state, j-1] = np.min(
            dummy_model.func_vals[:j])
        best_gp_scores[random_state, j-1] = np.min(
            gp_model.func_vals[:j])

mean_dummy_scores = np.mean(best_dummy_scores, axis=0)
mean_gp_scores = np.mean(best_gp_scores, axis=0)
err_dummy_scores = np.std(best_dummy_scores, axis=0) / sqrt(10)
err_gp_scores = np.std(best_gp_scores, axis=0) / sqrt(10)

print("Mean minimum value obtained after 200 iterations by dummy search "
      "across 5 random states.")
print("%0.4f" % mean_dummy_scores[-1])
print("Mean minimum value obtained after 200 iterations by gp-based search "
      "across 5 random states.")
print("%0.4f" % mean_gp_scores[-1])

plt.title("Minimum obtained at every iteration for branin")
plt.plot(n_iterations, mean_dummy_scores, label="Dummy search", color='red')
plt.plot(n_iterations, mean_gp_scores, label="GP search", color='green')
plt.fill_between(
    n_iterations, mean_dummy_scores - err_dummy_scores,
    mean_dummy_scores + err_dummy_scores, color='red', alpha=0.3)
plt.fill_between(
    n_iterations, mean_gp_scores - err_gp_scores,
    mean_gp_scores + err_gp_scores, color='green', alpha=0.3)
plt.legend(loc="best")
plt.xlabel("Number of iterations.")
plt.ylabel("Optimal value.")
plt.ylim([0, 5])
plt.show()


#!/usr/bin/env python3
import requests
import time
import smtplib
import os
import lxml
from bs4 import BeautifulSoup
from Utils import KeywordMatching
'''
1. Must be able to find products through keyword/product title
2. Must be able to find removed links/early links that are often made and removed before a release [sitemap.xml]
3. Must be able to show the quantity
4. Must be able to add to cart
5. Must be able to find variants and time stamp for when it was loaded
'''

class ShopifyWebpageMonitor():
    def __init__(self, url):
        self.url = url
        pass

    def alert(self):
        sender = 'fuck@niketalk.com'
        receiver = '---@gmail.com'
        message = """ From: From Person <from@fromdomain.com>
        To: To Person <to@todomain.com>
        Subject: SMTP e-mail test

        This is a test e-mail message.
        """
        try:
            smtpObj = smtplib.SMTP('localhost')
            smtpObj.sendmail(sender, receiver, message)
            print("Successfully sent email")
        except smtplib.SMTPException:
            print("Error: unable to send email")

    def monitor(self):
        header = {
            'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36'
                          '(KHTML, like Gecko) Chrome/56.0.2924.28 Safari/537.36'
        }
        try:
            old_map = {}
            while True:
                keyword_match = KeywordMatching()   # Initiate keyword match, Inside of while loop to update from changes

                r = requests.get(self.url + '/sitemap_products_1.xml', headers=header)
                if old_map:    # If the old_map exist, proceed
                    new_map = {}
                    soup = BeautifulSoup(r.text, 'xml')
                    for item in soup.find_all("url"):
                        title = None

                        for x in item.find_all("image"):
                            title = x.title.string

                        new_map[title] = item.find('loc').string

                    if self.compare_old_and_new_sitemap(old_map, new_map) is False:
                        new, removed = self.determine_dictionary_changes(old_map, new_map) # Returns two dictionaries of whats been added and whats been removed

                        # Do keyword matching to determine if its an item that is wanted
                        if keyword_match.contains_shoe_type(new.keys()):    # We only want the keys since this shows the title of the shoe
                            # TODO: Alert and add to cart
                            print()

                    # Update the old map with the current map
                    old_map = new_map

                else:           # Map out the current site map, this occurs when you first start the program
                    soup = BeautifulSoup(r.text, 'xml')
                    for item in soup.find_all("url"):
                        title = None

                        for x in item.find_all("image"):
                            title = x.title.string

                        old_map[title] = item.find('loc').string

                time.sleep(16)
        except KeyboardInterrupt:
            print("Exited monitor")

    def compare_old_and_new_sitemap(self, old_sitemap, new_sitemap):
        '''
        Quickly determines if there is any difference between the two sitemap dictionaries
        :param old_sitemap: Sitemap that was gathered previously
        :param new_sitemap: Sitemap that was gathered currently
        :return:
            True: If the old sitemap and the new sitemap are the same
            False: If the old sitemap and the new sitemap are different
        '''
        if old_sitemap == new_sitemap:
            print("They are the same")
            return True
        else:
            print("They are different")
            return False

    def determine_dictionary_changes(self, old_sitemap, new_sitemap):
        '''
        Determines what has been added to the new site map and what has been removed from the old sitemap
        :param old_sitemap
        :param new_sitemap
        :return:
            Dictionary of newly_added and removed
        '''
        newly_added = { k: new_sitemap[k] for k in set(new_sitemap) - set(old_sitemap) }
        removed = { k: old_sitemap[k] for k in set(old_sitemap) - set(new_sitemap) }
        return newly_added, removed

if __name__ == '__main__':
    go = ShopifyWebpageMonitor('https://www.trophyroomstore.com/')
    go.monitor()


# coding:utf-8
# 2019-1-10
# tf-record tool

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

from datetime import datetime
import os
import random
import sys
import threading

import numpy as np
import tensorflow as tf
import logging

__all__ = ['main']

def check_and_set_default_args(input_para):
    assert not (input_para['train_shards'] % input_para['num_threads'] and input_para['validation_shards'] % input_para['num_threads']), ("train_shards 和 validation_shards 必须是 num_threads 的公约数")

def _find_image_files(data_dir,
                    labels_file,
                    class_label_base):
    """
    return：
        filenames : list, data_dir下所有图片名
        texts : list, 父文件名
        labels : list, 每张图片对应的分类索引，下标从class_label_base开始
    """
    logging.info('处理的数据来源于 %s.' % data_dir)
    unique_labels = [l.strip() for l in tf.gfile.FastGFile(
        labels_file, 'r').readlines()] # 获得需要处理的文件列表

    labels, filenames, texts = [], [], []

    label_index = class_label_base

    # 各个文件夹下的图片
    for text in unique_labels:
        pic_file_pattern = '%s/%s/*' % (data_dir, text) # 匹配格式 `*` 通配符
        matching_files = []
        try:
            matching_files = tf.gfile.Glob(pic_file_pattern)
        except:
            pass
        labels.extend([label_index] * len(matching_files)) # 分类索引
        texts.extend([text] * len(matching_files)) # 父文件夹名
        filenames.extend(matching_files) # 图片名

        logging.info("第 %s 个分类，共处理文件 %s 个" % (
                label_index, len(labels)))
        label_index += 1

    # 打乱列表
    shuffled_index = [i for i in range(len(filenames))]
    random.seed(12345)
    random.shuffle(shuffled_index)

    filenames = [filenames[i] for i in shuffled_index]
    texts = [texts[i] for i in shuffled_index]
    labels = [labels[i] for i in shuffled_index]

    logging.info('在 %s 内, 共找到 %d 张图片,  %d 个标签' %
                 (data_dir, len(filenames), len(unique_labels)))

    return filenames, texts, labels

class ImageCoder(object):
    """
    将TensorFlow图像读取转换
    """
    def __init__(self):
        self._sess = tf.Session() # 创建Session处理图像

        # 将PNG转换为JPEG
        self._png_data = tf.placeholder(dtype=tf.string) # 读取图片未经过解码为字符串
        image = tf.image.decode_png(self._png_data, channels=3)
        self._png_to_jpeg = tf.image.encode_jpeg(image, format='rgb', quality=100)

        # JPEG解码
        self._decode_jpeg_data = tf.placeholder(dtype=tf.string)
        self._decode_jpeg = tf.image.decode_jpeg(self._decode_jpeg_data, channels=3)

    def png_to_jpeg(self, image_data):
        return self._sess.run(self._png_to_jpeg,
                              feed_dict={self._png_data: image_data})

    def decode_jpeg(self, image_data):
        image = self._sess.run(self._decode_jpeg,
                               feed_dict={self._decode_jpeg_data: image_data})
        assert len(image.shape) == 3
        assert image.shape[2] == 3, '图片不是三通道'
        return image

def _is_png(filename):
    """
    return bool
    """
    return '.png' in filename

def _process_image(filename, coder):
    """
    读取图片
    return:
        image_buffer: string, JPEG
        height: int
        width: int
    """
    with open(filename, 'rb') as f:
        image_data = f.read()

    if _is_png(filename):
        logging.info('%s 转换为JPEG格式' % filename)
        image_data = coder.png_to_jpeg(image_data)

    image = coder.decode_jpeg(image_data) # JPEG解码,解码后可获得图像属性
    assert len(image.shape) == 3
    assert image.shape[2] == 3, '图像不是三通道'

    height, width = image.shape[0], image.shape[1]

    return image_data, height, width

def _int64_feature(value):
    """
    转换为int64格式
    """
    if not isinstance(value, list):
        value = [value]
    return tf.train.Feature(int64_list=tf.train.Int64List(value=value))

def _bytes_feature(value):
    """
    转换为bytes
    """
    return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))

def _convert_to_example(filename,
                        image_buffer,
                        label,
                        text,
                        height,
                        width):
    """
    使用tf.train.Example生成 Example proto
    """
    colorspace = b'RGB'
    channels = 3
    image_format = b'JPEG'

    example = tf.train.Example(features=tf.train.Features(feature={
        'image/height': _int64_feature(height),
        'image/width': _int64_feature(width),
        'image/colorspace': _bytes_feature(colorspace),
        'image/channels': _int64_feature(channels),
        'image/class/label': _int64_feature(label),
        'image/class/text': _bytes_feature(str.encode(text)),
        'image/format': _bytes_feature(image_format),
        'image/filename': _bytes_feature(os.path.basename(str.encode(filename))),
        'image/encoded': _bytes_feature(image_buffer)}))

    return example

def _process_image_files_batch(coder,
                                thread_index,
                                ranges,
                                name,
                                filenames,
                                texts,
                                labels,
                                num_shards,
                                output_dir,
                                dataset_name):
    """
    每个线程的call函数
    """
    num_threads = len(ranges) # 计算线程数
    assert not num_shards % num_threads, 'num_shars必须能整除num_threads'
    num_shards_per_batch = int(num_shards / num_threads) # 每个线程需要处理的数据

    shard_ranges = np.linspace(
        ranges[thread_index][0],
        ranges[thread_index][1],
        num_shards_per_batch + 1).astype(int)
    num_files_in_thread = ranges[thread_index][1] - ranges[thread_index][0] # 计算当前线程需要处理的图像数量

    counter = 0
    for s in range(num_shards_per_batch):
        shard = thread_index * num_shards_per_batch + s # 计算当前图像处理批次
        output_filename = '%s_%s_%.5d-of-%.5d.tfrecord' % (dataset_name, name, shard, num_shards)

        output_file = os.path.join(output_dir, output_filename)

        writer = tf.python_io.TFRecordWriter(output_file)

        shard_counter = 0
        files_in_shard = np.arange(shard_ranges[s], shard_ranges[s + 1], dtype=int) # 获得当前处理的文件索引

        for i in files_in_shard:
            filename = filenames[i]
            label = labels[i]
            text = texts[i]

            image_buffer, height, width = _process_image(filename, coder)
            example = _convert_to_example(
                filename,
                image_buffer,
                label,
                text,
                height,
                width) # tf.train.Features 格式化数据
            writer.write(example.SerializeToString())
            shard_counter += 1
            counter += 1

            if not counter % 300:
                logging.info('%s [thread %d]: Processed %d of %d images in thread batch.' %
                             (datetime.now(), thread_index, counter, num_files_in_thread))
                sys.stdout.flush()
        writer.close()
        logging.info('%s [thread %d]: Wrote %d images to %s' %
                     (datetime.now(), thread_index, shard_counter, output_file))
        sys.stdout.flush()
        shard_counter = 0
    logging.info('%s [thread %d]: Wrote %d images to %d shards.' %
                 (datetime.now(), thread_index, counter, num_files_in_thread))
    sys.stdout.flush()

def _process_image_files(name,
                        filenames,
                        texts,
                        labels,
                        num_shards,
                        num_threads,
                        output_dir,
                        dataset_name):

    # 判断图片是否对应标签
    assert len(filenames) == len(texts)
    assert len(filenames) == len(labels)

    # 将列表分割为num_threads份
    # np.linspace(0, 20, 4 + 1).astype(np.int)
    # [ 0  5 10 15 20]
    spacing = np.linspace(0, len(filenames), num_threads + 1).astype(np.int)

    # 组合索引
    ranges = []
    for i in range(len(spacing) - 1):
        ranges.append([spacing[i], spacing[i + 1]])

    logging.info('开启 %s 个线程' % num_threads)
    sys.stdout.flush() # 刷新输出

    coord = tf.train.Coordinator() # 监控线程

    coder = ImageCoder() # 初始化图像处理类

    # 线程入队
    threads = []
    for thread_index in range(len(ranges)): # range(num_threads)
        args = (coder, thread_index, ranges, name, filenames, texts, labels, num_shards, output_dir, dataset_name)
        t = threading.Thread(target=_process_image_files_batch, args=args)
        t.start()
        threads.append(t)

    coord.join(threads)
    logging.info('%s : 完成 %d 图像的数据转换.' %
                (datetime.now(), len(filenames)))
    sys.stdout.flush()

def process_dataset(name,
                    directory,
                    num_shards,
                    labels_file,
                    num_threads,
                    output_dir,
                    dataset_name,
                    class_label_base):
    filenames, texts, labels = _find_image_files(directory, labels_file, class_label_base)
    _process_image_files(name, filenames, texts, labels, num_shards, num_threads, output_dir, dataset_name)

def main(input_para):
    logging.info('Saving results to %s' % input_para['output_dir'])
    process_dataset(
        'validation',
        input_para['validation_dir'],
        input_para['validation_shards'],
        input_para['labels_file'],
        input_para['num_threads'],
        input_para['output_dir'],
        input_para['dataset_name'],
        input_para['class_label_base'])

    process_dataset(
        'train',
        input_para['train_dir'],
        input_para['train_shards'],
        input_para['labels_file'],
        input_para['output_dir'],
        input_para['num_threads'],
        input_para['output_dir'],
        input_para['dataset_name'],
        input_para['class_label_base'])

    logging.info('%s : Finish!' % datetime.now())

if __name__ == '__main__':
    input_para = {

    }
    main(input_para)


import numpy as np
from common import create_harmonic, create_from_wav, am_mod, plot_spectrum, filt, interpolate, decimate
import matplotlib.pyplot as plt

Fcarrier = 200e3

# Read samples from wav file
Fs, t, audio_sig = create_from_wav('audio.wav', 1000)
print('Audio Fs =', Fs)

# Interpolate
interp_audio_sig = interpolate(audio_sig, INTERP_ORDER=20, NFIR=101)
Fs_after_interp = 20*Fs # New Fs after interpolation

print('Fs after interpolation =', Fs_after_interp)

# Make AM Modulation
am_mod_sig = am_mod(interp_audio_sig, Fc=Fcarrier, Fs=Fs_after_interp, mod_id=1)

# Plot signals in time domain

plt.figure()
plt.plot(am_mod_sig, color='C2', label='AM modulated signal')
plt.plot(interp_audio_sig/np.amax(np.absolute(interp_audio_sig)), color='C3', label='Modulation audio signal')
plt.xlabel('Indexes')
plt.ylabel('Signal Values')
plt.legend(loc='upper right')

# Plot fm modulated signal in frequency domain

plot_spectrum(am_mod_sig, Fs=Fs_after_interp, NFFT=8192, title='AM Signal Spectrum')

plt.show()


from RDD_Dfr import Data_dfr
from pyspark.sql import SparkSession,Row
from WriteToDB import WrToDB
from pyspark.sql.functions import split

DFR = Data_dfr.DataFrame
RDD = Data_dfr.DataFrame.rdd
sc = Data_dfr.sc

########################################################
#行业 以及其下级对上级行业的占比
########################################################

session=  SparkSession.builder.getOrCreate()#获取或创建

#重构

# mapRDD = RDD.map(lambda x:Row(
#         Jindustry1 =x.Jtype.split("_")[0],
#         Jindustry2 =x.Jtype.split("_")[1] if len(x.Jtype.split("_"))>1 else " ",
#         Jindustry3 =x.Jtype.split("_")[2] if len(x.Jtype.split("_"))>2 else " ",
#         )
#                  )
#
# industry1RDD = mapRDD.map(lambda x:(x[0],1)).reduceByKey(lambda x,y:x+y)

# finalResults = []
#
# for item1 in industry1RDD.collect():
#     # print(type(item),item[0])
#     industry2RDD = mapRDD.filter(lambda r:r[0]==item1[0]).map(lambda x: (x[1], 1)).reduceByKey(lambda x, y: x + y)
#     for item2  in industry2RDD.collect():
#             industry3RDD = mapRDD.filter(lambda r: r[1] == item2[0] and r[0]==item1[0] ).map(lambda x: (x[2], 1)).reduceByKey(lambda x, y: x + y)
#             for item3 in industry3RDD.collect():
#                     result=[item1[0],item2[0],item3[0],item2[1]/item1[1],item3[1]/item2[1]]
#                     finalResults.append(result)
#
# resultRDD = sc.parallelize(finalResults)
# resultRow = resultRDD.map(lambda p:Row(
#     Jindustry1 = p[0],
#     Jindustry2 =p[1],
#     Jindustry3 =p[2],
#     Ratio2to1 =p[3],
#     Ratio3to2 =p[4],
# ))
# resultDRF = session.createDataFrame(resultRow)
# # resultDRF.show()
# WrToDB(resultDRF,"Demand1_1_2","overwrite")

###############################################################################################################
DFR = DFR.withColumn('Jindustry1',split(DFR['Jtype'],'_')[0])
DFR = DFR.withColumn('Jindustry2',split(DFR['Jtype'],'_')[1])
DFR = DFR.withColumn('Jindustry3',split(DFR['Jtype'],'_')[2])
# DFR = DFR.withColumn('Jindustry2',split(DFR['Jtype'],'_')[1] if len(split(DFR['Jtype'],'_'))>1 else " ")
# DFR = DFR.withColumn('Jindustry3',split(DFR['Jtype'],'_')[2] if len(split(DFR['Jtype'],'_'))>2 else " ")

industry1DFR = DFR.groupBy("Jindustry1").count()
# count = industry1DFR.count()
finalResults = []
# count1 = 0
# count2 = 0
# count3 = 0

for item1 in industry1DFR.collect():
    industry2DFR = DFR.filter(DFR['Jindustry1']==item1.Jindustry1).groupBy('Jindustry2').count()
    count1 = item1[1]
    if(item1[0]==''):
        Jindustry1 = "其它"
    else:
        Jindustry1 = item1.Jindustry1
    result = [Jindustry1,"所有",count1]
    finalResults.append(result)

    for item2  in industry2DFR.collect():
        industry3DFR = DFR.filter(DFR['Jindustry1'] == item1.Jindustry1 ).filter(DFR['Jindustry2'] == item2.Jindustry2).groupBy('Jindustry3').count()
        count2 = item2[1]
        if (item2[0] == ''):
            Jindustry2 = "其它"
        else:
            Jindustry2 = item2.Jindustry2
        result = [Jindustry2,Jindustry1 , count2]
        finalResults.append(result)

        for item3 in industry3DFR.collect():
            count3 = item3[1]
            if (item3[0] == ''):
                Jindustry3 = "其它"
            else:
                Jindustry3 = item3.Jindustry3
            result = [Jindustry3, Jindustry2, count3]
            finalResults.append(result)

print(finalResults)
resultRDD = sc.parallelize(finalResults)
resultRow = resultRDD.map(lambda p:Row(
    JtypeNow = p[0],
    JtypeFather =p[1],
    count =p[2],
))
resultDRF = session.createDataFrame(resultRow).orderBy('JtypeFather','JtypeNow')
resultDRF.show()
WrToDB(resultDRF,"Demand1_1_2","overwrite")


# Copyright The PyTorch Lightning team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import torch
import torch.nn.functional as F
from torch.utils.data import DataLoader

import pytorch_lightning as pl
import tests.helpers.pipelines as tpipes
import tests.helpers.utils as tutils
from pytorch_lightning.callbacks import EarlyStopping
from pytorch_lightning.core import memory
from tests.helpers import BoringModel, RandomDataset
from tests.helpers.datamodules import ClassifDataModule
from tests.helpers.runif import RunIf
from tests.helpers.simple_models import ClassificationModel

PRETEND_N_OF_GPUS = 16

class CustomClassificationModelDP(ClassificationModel):

    def _step(self, batch, batch_idx):
        x, y = batch
        logits = self(x)
        return {'logits': logits, 'y': y}

    def training_step(self, batch, batch_idx):
        out = self._step(batch, batch_idx)
        loss = F.cross_entropy(out['logits'], out['y'])
        return loss

    def validation_step(self, batch, batch_idx):
        return self._step(batch, batch_idx)

    def test_step(self, batch, batch_idx):
        return self._step(batch, batch_idx)

    def validation_step_end(self, outputs):
        self.log('val_acc', self.valid_acc(outputs['logits'], outputs['y']))

    def test_step_end(self, outputs):
        self.log('test_acc', self.test_acc(outputs['logits'], outputs['y']))

@RunIf(min_gpus=2)
def test_multi_gpu_early_stop_dp(tmpdir):
    """Make sure DDP works. with early stopping"""
    tutils.set_random_master_port()

    dm = ClassifDataModule()
    model = CustomClassificationModelDP()

    trainer_options = dict(
        default_root_dir=tmpdir,
        callbacks=[EarlyStopping(monitor='val_acc')],
        max_epochs=50,
        limit_train_batches=10,
        limit_val_batches=10,
        gpus=[0, 1],
        accelerator='dp',
    )

    tpipes.run_model_test(trainer_options, model, dm)

@RunIf(min_gpus=2)
def test_multi_gpu_model_dp(tmpdir):
    tutils.set_random_master_port()

    trainer_options = dict(
        default_root_dir=tmpdir,
        max_epochs=1,
        limit_train_batches=10,
        limit_val_batches=10,
        gpus=[0, 1],
        accelerator='dp',
        progress_bar_refresh_rate=0,
    )

    model = BoringModel()

    tpipes.run_model_test(trainer_options, model)

    # test memory helper functions
    memory.get_memory_profile('min_max')

@RunIf(min_gpus=2)
def test_dp_test(tmpdir):
    tutils.set_random_master_port()

    dm = ClassifDataModule()
    model = CustomClassificationModelDP()
    trainer = pl.Trainer(
        default_root_dir=tmpdir,
        max_epochs=2,
        limit_train_batches=10,
        limit_val_batches=10,
        gpus=[0, 1],
        accelerator='dp',
    )
    trainer.fit(model, datamodule=dm)
    assert 'ckpt' in trainer.checkpoint_callback.best_model_path
    results = trainer.test(datamodule=dm)
    assert 'test_acc' in results[0]

    old_weights = model.layer_0.weight.clone().detach().cpu()

    results = trainer.test(model, datamodule=dm)
    assert 'test_acc' in results[0]

    # make sure weights didn't change
    new_weights = model.layer_0.weight.clone().detach().cpu()

    assert torch.all(torch.eq(old_weights, new_weights))

class ReductionTestModel(BoringModel):

    def train_dataloader(self):
        return DataLoader(RandomDataset(32, 64), batch_size=2)

    def val_dataloader(self):
        return DataLoader(RandomDataset(32, 64), batch_size=2)

    def test_dataloader(self):
        return DataLoader(RandomDataset(32, 64), batch_size=2)

    def add_outputs(self, output, device):
        output.update({
            "reduce_int": torch.tensor(device.index, dtype=torch.int, device=device),
            "reduce_float": torch.tensor(device.index, dtype=torch.float, device=device),
        })

    def training_step(self, batch, batch_idx):
        output = super().training_step(batch, batch_idx)
        self.add_outputs(output, batch.device)
        return output

    def validation_step(self, batch, batch_idx):
        output = super().validation_step(batch, batch_idx)
        self.add_outputs(output, batch.device)
        return output

    def test_step(self, batch, batch_idx):
        output = super().test_step(batch, batch_idx)
        self.add_outputs(output, batch.device)
        return output

    def training_epoch_end(self, outputs):
        assert outputs[0]["loss"].shape == torch.Size([])
        assert outputs[0]["reduce_int"].item() == 0  # mean([0, 1]) = 0
        assert outputs[0]["reduce_float"].item() == 0.5  # mean([0., 1.]) = 0.5

@RunIf(min_gpus=2)
def test_dp_training_step_dict(tmpdir):
    """ This test verifies that dp properly reduces dictionaries """
    model = ReductionTestModel()
    model.training_step_end = None
    model.validation_step_end = None
    model.test_step_end = None

    trainer = pl.Trainer(
        default_root_dir=tmpdir,
        max_epochs=1,
        limit_train_batches=1,
        limit_val_batches=1,
        limit_test_batches=1,
        gpus=2,
        accelerator='dp',
    )
    trainer.fit(model)


from application import app
from flask import Flask, render_template, request
import plasmapy
import astropy
import astropy.units as u
import plasmapy.formulary.parameters as pfp

# Thermal Pressure

def calculate_thermal_pressure(form):
    num1 = form['temp']
    num2 = form['density']

    unit1 = u.Unit(form['unitsT'])
    q1 = u.Quantity(num1, unit1)
    unit2 = u.Unit(form['unitsN'])
    q2 = u.Quantity(num2, unit2)
    sum = pfp.thermal_pressure(q1, q2)
    return sum

# Debye length

def calculate_debye_length(form):
    num1 = form['temp']
    num2 = form['density']

    unit1 = u.Unit(form['unitsT'])
    q1 = u.Quantity(num1, unit1)
    unit2 = u.Unit(form['unitsN'])
    q2 = u.Quantity(num2, unit2)
    sum = pfp.Debye_length(q1, q2)
    return sum

# Gyrofrequency

def calculate_gyrofrequency(form):
    mag_fld = form['mf_mag']
    mag_unit = form['unitsB']
    particle = form['particle']
    z = form['z']
    signed = form['signed']
    to_hz = form['to_hz']

    if mag_fld == None or mag_unit == 'select':
        return render_template('gyrofrequency.html', sum="Enter all required fields")

    # Gyrofrequency with only Magnetic Field and Particle
    b = u.Quantity(mag_fld, u.Unit(mag_unit))
    p = plasmapy.particles.Particle(particle)
    sum = pfp.gyrofrequency(b, p)

    # Gyrofrequency with B, particle, z

    # Gyrofrequency with B, particle, z, signed

    # Output if to_hz is true
    return sum

# Inertial length

def calculate_inertial_length(form):
    n = form['n']
    p = form['particle']

    n_quantity = u.Quantity(n, u.Unit(form['unitsN']))
    sum = pfp.inertial_length(n_quantity, p)
    return sum


# -*- coding: utf-8 -*-
"""
Here you will find redefined editor widgets
which install some sensitive modes
"""

import sys
from codeaide.base import *
from codeaide.main import *
import codeaide.modes

class BaseEditor(CodeAide):

    modes = (
        "AutoTyping",
        "WrapIndicator",
        "HighlightLine",
        "LineNumbers",  # installs Sidebar as a dependency
        "LineIcons",
        "LineStatus",
        "Occurrences",
        #"EmacsCore",
        "ParentheseMatcher",
        "Whitespace",
        "EditExtras",
        "Snippets",
        )
    require_modules = ()

    def __init__(self, parent=None):
        if not codeaide.modes.loaded:
            codeaide.modes.load_all(verbose=True)
        CodeAide.__init__(self, parent)
        for mod_name in self.require_modules:
            mod = __import__(mod_name, {}, {}, [])
        self.install(*self.modes)
        self.init_editor()

    def init_editor(self):
        pass

    @classmethod
    def create_window(cls, *args, **kwargs):
        edit = cls(*args, **kwargs)
        edit.resize(800,600)
        edit.show()
        return edit

    @classmethod
    def test(cls, *args, **kwargs):
        app = QApplication.instance() or QApplication(sys.argv)
        edit = cls.create_window(*args, **kwargs)
        return app.exec_()

class CodeEditor(BaseEditor):
    modes = BaseEditor.modes + ("AutoloadLanguage",)

    @classmethod
    def create_window(cls, *args, **kwargs):
        edit = super(CodeEditor, cls).create_window(*args, **kwargs)
        if len(sys.argv) == 2:
            filename = sys.argv[1]
        else: filename = __file__.rstrip("co")
        edit.load_buffer(filename)
        return edit

class PythonEditor(CodeEditor):
    modes = CodeEditor.modes + ("Python",)

class BaseConsole(BaseEditor):
    modes =  (
        "AutoTyping",
        #"WrapIndicator",
        #"LineNumbers",  # installs Sidebar as a dependency
        "ParentheseMatcher",
        "Whitespace",
        "Session"
        )

    @classmethod
    def create_window(cls, *args, **kwargs):
        edit = super(BaseConsole, cls).create_window(*args, **kwargs)
        return edit

class PythonConsole(BaseConsole):
    modes = BaseConsole.modes + ("PythonConsole",)

class QtScriptConsole(BaseConsole):
    modes = BaseConsole.modes + ("QtScriptConsole",)

class ShellConsole(BaseConsole):
    modes = BaseConsole.modes + ("Shell",)


import os
import sys
import json
import datetime
from os.path import expanduser

now = datetime.datetime.now()
nowdatetime = now.strftime('%Y%m%d-%H%M%S')

home = expanduser("~")

DIR = home + "/" + nowdatetime

#print(home)

def login(master_ip):
    os.system("cloudctl login -a https://%s:8443 --skip-ssl-validation" % master_ip)

def version_check():
    os.system("kubectl version -o json >> version.json")

    with open('version.json') as json_file:
        json_data = json.load(json_file)

        server_string = json_data["serverVersion"]
        server_version = server_string["minor"]

        os.system("rm -rf version.json")

    return server_version

def get_logs():

    os.system("mkdir -p %s" % DIR)

    os.system(
        "kubectl get images --all-namespaces >> %s/private_docker_registry.log" % DIR)
    os.system("cloudctl catalog charts -s >> %s/helm_chart.log" % DIR)
    os.system("kubectl get ns >> %s/namespaces.log" % DIR)
    os.system("kubectl get pod --all-namespaces >> %s/pod.log" % DIR)
    os.system("kubectl get ds --all-namespaces >> %s/daemonset.log" % DIR)
    os.system("kubectl get deploy --all-namespaces >> %s/deployment.log" % DIR)
    os.system("helm list --tls >> %s/helm_release.log" % DIR)
    os.system("kubectl get job --all-namespaces >> %s/job.log" % DIR)
    os.system("kubectl get cronjob --all-namespaces >> %s/cronjob.log" % DIR)
    os.system("kubectl get sts --all-namespaces >> %s/statefulset.log" % DIR)
    os.system("kubectl get rs --all-namespaces >> %s/replicaset.log" % DIR)
    os.system("kubectl get svc --all-namespaces >> %s/services.log" % DIR)
    os.system("kubectl get ing --all-namespaces >> %s/ingress.log" % DIR)
    os.system("kubectl get cm --all-namespaces >> %s/configmaps.log" % DIR)
    os.system("kubectl get hpa --all-namespaces >> %s/scaling_policies.log" % DIR)
    os.system("kubectl get secrets --all-namespaces >> %s/secrets.log" % DIR)

def count(log):

    file = DIR+"/"+log

    log_file = open(file, 'r')
    line = log_file.read().count("\n")

    log_file.close()

    return line

def status_check():
    version = version_check()
    daemonset()
    deployment(version)
    helm
    job
    statefulset(version)
    replicaset

def daemonset():
    os.system("kubectl get daemonset --all-namespaces -o json > daemonset.json")

    ds_error = 0

    with open('daemonset.json') as json_file:
        json_data = json.load(json_file)

        len_d = len(json_data['items'])

        print("\033[33m%55s\033[0m" % "============ CHECK  Daemonset ============" )

        for i in range(len_d):
            NAME = json_data['items'][i]['metadata']['name']
            DESIRED = json_data['items'][i]['status']['desiredNumberScheduled']
            CURRENT = json_data['items'][i]['status']['currentNumberScheduled']
            READY = json_data['items'][i]['status']['numberReady']
            if READY == 0:
                AVAILABLE = 0
            else:
                AVAILABLE = json_data['items'][i]['status']['numberAvailable']

            if DESIRED == CURRENT == READY == AVAILABLE:
                print("%-60s\033[32m" % NAME),
                print("%s\033[0m" % "READY")
            else:
                print("%-60s\033[31m" % NAME),
                print("%s\033[0m" % "NOT READY")
                ds_error = ds_error+1

    os.system("rm -rf daemonset.json")

    return ds_error

def deployment():
    os.system("kubectl get deployment --all-namespaces -o json > deployment.json")
    deploy_error = 0

    with open('deployment.json') as json_file:
        json_data = json.load(json_file)
        len_d = len(json_data['items'])

        print("\033[33m%55s\033[0m" % "============ CHECK  Deployment ============" )

        for i in range(len_d):
            NAME = json_data['items'][i]['metadata']['name']
            DESIRED = json_data['items'][i]['status']['replicas']
            CURRENT = json_data['items'][i]['status']['updatedReplicas']
            READY = json_data['items'][i]['status']['conditions'][0]['status']

            try:
                AVAILABLE = json_data['items'][i]['status']['availableReplicas']
            except:
                AVAILABLE = 0

            if DESIRED == CURRENT == AVAILABLE:
                print("%-60s\033[32m" % NAME),
                print("%s\033[0m" % "READY")
            else:
                print("%-60s\033[31m" % NAME),
                print("%s\033[0m" % "NOT READY")
                deploy_error = deploy_error+1

    os.system("rm -rf deployment.json")
    return deploy_error

# def helm():
# def job():
# def statefulset(version):
# def replicaset():

# def result()

# master_ip = raw_input("Cluster Master IP: ")
# login(master_ip)

# server_version = version_check()
#get_logs()

deployment()

# master_ip = raw_input("Cluster Master IP: ")

# os.system("cloudctl login -a https://%s:8443 --skip-ssl-validation" % master_ip)

# now = datetime.datetime.now()
# nowdatetime = now.strftime('%Y%m%d-%H%M%S')

# DIR = "~/zcp-verification/"+nowdatetime
# os.system("mkdir -p %s" % DIR)


from tensorflow import keras

class LearningRateScheduler(keras.callbacks.Callback):
  """Learning rate scheduler which sets the learning rate according to schedule.

  Arguments:
      schedule: a function that takes an epoch index
          (integer, indexed from 0) and current learning rate
          as inputs and returns a new learning rate as output (float).
  """
  def __init__(self, schedule):
    super(LearningRateScheduler, self).__init__()
    self.schedule = schedule

  def on_epoch_begin(self, epoch, logs=None):
    if not hasattr(self.model.optimizer, 'lr'):
      raise ValueError('Optimizer must have a "lr" attribute.')

    lr = float(keras.backend.get_value(self.model.optimizer.lr))
    scheduled_lr = self.schedule(epoch, lr)
    # Set the value back to the optimizer before this epoch starts
    keras.backend.set_value(self.model.optimizer.lr, scheduled_lr)


from EstruturasDeDados import Fila

def Menu():
	N = Fila()
	P = Fila()
	contN = 0
	contP = 0
	while True:
		opc = int(input('''
			1 - Senha Normal
			2 - Senha Prioridade
			3 - Atedimento
			Digite a opção desejada:\t'''))

		if opc == 1:
			contN += 1
			N.enfileirar(contN)
			print('Sua senha é a {}N'.format(contN))

		elif opc == 2:
			contP +=1
			P.enfileirar(contP)
			print('Sua senha é a {}P'.format(contP))

		elif opc == 3:
			if (P.vazia()) and (not N.vazia()):
				print('Atedimento á senha`{}N'.format(N.desenfileirar()))

			elif not P.vazia():
				print('Atedimento à senha{}N'.format(P.desenfileirar()))

			else:
				print('Sem senha a serem chamadas')

		else:
			print('Opção inválida')

Menu()


import numpy as np
import scipy
import MDAnalysis as mda

import matplotlib.pyplot as plt
from matplotlib.ticker import FixedLocator, FixedFormatter
from org_functions import labels_path, saving_data, read_data, Timer, wrap

def nr_fr_range(arr_ranges):
    nr_fr = arr_ranges[:,1] - arr_ranges[:,0]
    return nr_fr

def time_fr_to_fs(data_ez, nr_fr, ts_fs = 0.4):
    nr_fr_skip = np.abs(data_ez[0][0] - data_ez[0][1])

    time_fs = nr_fr * nr_fr_skip*ts_fs
    return time_fs

def zero_runs(a):
    # Create an array that is 1 where a is 0, and pad each end with an extra 0.
    iszero = np.concatenate(([0], np.equal(a, 0).view(np.int8), [0]))

    absdiff = np.abs(np.diff(iszero))

    # Runs start and end where absdiff is 1.
    ranges = np.where(absdiff == 1)[0].reshape(-1, 2)
    return ranges

def not_zero_runs(a):
    # Create an array that is 1 where a is 0, and pad each end with an extra 0.
    iszero = np.concatenate(([0], np.not_equal(a, 0).view(np.int8), [0]))

    absdiff = np.abs(np.diff(iszero))

    # Runs start and end where absdiff is 1.
    ranges_non_zero = np.where(absdiff == 1)[0].reshape(-1, 2)
    return ranges_non_zero

def id_runs(a,id_at):
    # Create an array that is 1 where a is 0, and pad each end with an extra 0.
    iszero = np.concatenate(([0], np.equal(a, id_at).view(np.int8), [0]))
    absdiff = np.abs(np.diff(iszero))
    # Runs start and end where absdiff is 1.
    ranges = np.where(absdiff == 1)[0].reshape(-1, 2)
    return ranges

def plot_res_time(res_times,res_times2 ):
    plt.figure(figsize=(6.26894*2,4.7747*1))
    fs = 18
    nr_bins = 20
    y_val = np.array(res_times.copy())/1000
    y_val_2 = np.array(res_times2.copy())/1000
    print('Average residence time with an oxygen', np.round(np.average(y_val),1))
    density, edges, patches = plt.hist(y_val,bins=nr_bins,
                                       histtype='step', label='Residence time with merging')#

    density2, edges2, patches2 = plt.hist(y_val_2, bins=edges,
                                           histtype='step', label='Residence time no/less merging')#

    plt.tick_params(labelsize=fs)
    plt.xlabel("Time [ps]", fontsize=fs)
    plt.ylabel("Counts", fontsize=fs)
    plt.tick_params(axis='x', labelsize=fs)
    plt.tick_params(axis='y', labelsize=fs)
    plt.legend(fontsize=fs-4)
    plt.show()

def plot_ox_id(ez_data, ox_id1,ox_id2, nr_ox_invol, t_st_fs):
    fig, (ax1, ax2) = plt.subplots(1,2, figsize=(6.26894*2,4.7747),sharey=True)
    fig.subplots_adjust(wspace=0)
    fs =18
    for i in range(0, nr_ox_invol):
        y = ox_id1[:,i].copy()
        y[y==0] = np.nan
        ax1.plot(ez_data[0].copy()*t_st_fs/1000,y, marker='o')

    for i in range(0, nr_ox_invol):
        y = ox_id2[:,i].copy()
        y[y==0] = np.nan
        ax2.plot(ez_data[0].copy()*t_st_fs/1000,y, marker='o')

    tit_x = 'Time [ps]'
    tit_y = 'Oxygen ID'
    ax1.set_ylabel(tit_y,fontsize=fs)
    for ax in (ax1,ax2):
        ax.set_xlabel(tit_x,fontsize=fs)

        ax.tick_params(axis='x', labelsize=fs)
        ax.tick_params(axis='y', labelsize=fs)

    ax1.set_title('Without merging', fontsize=fs)
    ax2.set_title('With merging',fontsize=fs)
    plt.tight_layout()
    plt.show()

def pt_rt(ez_data,nr_fr=1, merge_fr = 0,t_st_fs = 0.4, nr_tot_ox=128):
    less_merg = 5
    # selecting oxygen at id that are identified to interact with the excess proton
    appear_id = np.unique(np.concatenate([np.unique(ez_data[7].copy()[np.nonzero(ez_data[7].copy())]),
                                np.unique(ez_data[6][:,0].copy()[np.nonzero(ez_data[6][:,0].copy())]),
                               np.unique(ez_data[6][:,1].copy()[np.nonzero(ez_data[6][:,1].copy())])]))
    nr_fr_skip = np.abs(ez_data[0][0] - ez_data[0][1])

    nr_ox_invol = len(appear_id)
    resid_times = []
    resid_times_no_merg = []
    ox_id_timing = np.zeros([len(ez_data[0]),nr_ox_invol])
    ox_id_timing_cl = np.zeros([len(ez_data[0]),nr_ox_invol])
    for a in range(0, len(appear_id)):

        at_id = np.int(appear_id[a])
        eig_at = id_runs(ez_data[7],at_id)
        zund1_at = id_runs(ez_data[6][:,0],at_id)
        zund2_at = id_runs(ez_data[6][:,1],at_id)
        # combining ranges of frames from both eig-zund conformations in one array
        # array that gives ranges when this ox at was interactring with H+
        all_ranges = np.concatenate([eig_at,zund1_at,zund2_at])

        # array with info, when ox interacting =1 if ox not interacting =0
        #combined for both both eig-zund conformations
        ox_timing = np.zeros(len(ez_data[0]))
        ox_timing_no_merg = np.zeros(len(ez_data[0]))

        for p in all_ranges:
            for i in range(p[0],p[1],1):
                ox_timing[i] = 1
                ox_timing_no_merg[i] = 1
                ox_id_timing[i,a] = at_id
                ox_id_timing_cl[i,a] = at_id

        #filling gaps smaller than nr_zer with 1
        ox_zeros_ranges = zero_runs(ox_timing)
        for j in range(0,len(ox_zeros_ranges)):
            nr_zer = ox_zeros_ranges[j,1]-ox_zeros_ranges[j,0]
            if (nr_zer < merge_fr):
                for i in range(ox_zeros_ranges[j][0],ox_zeros_ranges[j][1],1):
                    ox_timing[i] = 1.
                    ox_id_timing[i,a] = at_id

        # remowing the les than merging times from the graph
        ox_non_zero_ranges = not_zero_runs(ox_id_timing[:,a])
        nr_non_zer = nr_fr_range(ox_non_zero_ranges)
        for g in range(0,len(ox_non_zero_ranges)):
            if (nr_non_zer[g] < merge_fr):
                for h in range(ox_non_zero_ranges[g][0], ox_non_zero_ranges[g][1]):
                    ox_id_timing[i,a] = 0

        # les merging runs
        with_at_rans = not_zero_runs(ox_timing)
        with_at_nr_fr = nr_fr_range(with_at_rans)
        ox_zeros_ranges2 = zero_runs(ox_timing_no_merg)
        for j in range(0,len(ox_zeros_ranges2)):
            nr_zer = ox_zeros_ranges2[j,1]-ox_zeros_ranges2[j,0]
            if (nr_zer < less_merg):
                for i in range(ox_zeros_ranges2[j][0],ox_zeros_ranges2[j][1],1):
                    ox_timing_no_merg[i] = 1.

        with_at_rans_no_merg = not_zero_runs(ox_timing_no_merg)
        with_at_nr_fr_no_merg = nr_fr_range(with_at_rans_no_merg)

        for k in range(0,len(with_at_nr_fr_no_merg)):
            resid_times_no_m = time_fr_to_fs(ez_data,with_at_nr_fr_no_merg[k])
            resid_times_no_merg.append(resid_times_no_m)

        # rejecting times that were shorter than merging criterium because
        #this times were already colected by some other residence time
        with_at_sel = with_at_nr_fr[with_at_nr_fr>merge_fr]
        for i in range(0,len(with_at_sel)):
            res_t_ps = time_fr_to_fs(ez_data,with_at_sel[i])
            resid_times.append(res_t_ps)

    plot_ox_id(ez_data,ox_id_timing_cl,ox_id_timing, nr_ox_invol , t_st_fs)
#     plot_ox_id(ez_data,ox_id_timing, nr_ox_invol , t_st_fs)
    print('Time limit to merge data:',time_fr_to_fs(ez_data,merge_fr),'fs',
          'and less merged data with limit: ', time_fr_to_fs(ez_data, less_merg),'fs')
    print('Minimum selested residence time:', min(resid_times),'fs')
    plot_res_time(resid_times, resid_times_no_merg)

    av_res_time = np.round(np.average(resid_times)/1000,1)
    sim_t= nr_fr*t_st_fs/1000

    hop_rate = np.round(len(resid_times)/sim_t,2)
    print('Rate of hops with merging:',hop_rate,'ps^(-1)')

    appear_id_aft_merg = np.unique(ox_id_timing[:,:].copy()[np.nonzero(ox_id_timing[:,:].copy())])
    print('Nr ox at detected before merging:',len(appear_id))
    print('Nr ox at detected after merging:',len(appear_id_aft_merg))
    participating_ox_perc = (len(appear_id_aft_merg)*100)/(nr_tot_ox*sim_t) # percent per ps
    return av_res_time, hop_rate,max(resid_times)/1000, participating_ox_perc,resid_times


# encoding=utf-8
import os
import time
from  utility.timeutility import TimeUtility
from  utility.fileutil import FileUtility
from storage.newsstorage import NewsStorage
from storage.cmtstorage  import CMTStorage
from dao.sqldao import SQLDAO
from utility.common  import Common
from utility import const
from configuration import constant
from configuration.environment.configure import SpiderConfigure
from log.spiderlog import Logger
########################################################################
class FileFormat:
    """"""
    DEFAULT_NEWS_FORMAT = u'{channel}\t{url}\t{title}\t{body}\t{comments}\t{cmtnum}\t{clicknum}\t{votenum}\t{fansnum}\t{pubtime}\t{crawlertime}\t{type}\t{query}\t\t\t'
    KEY_CMTLIST = 'commentlist'
    OUTPUTPATH = '{path}/{suffix}_{date}_{ts}'
    ERRORINFOPATH = '{path}/{suffix}_error_{date}_{ts}'
    #----------------------------------------------------------------------
    def __init__(self):
        self.url_beforenewsinfo_map = {SQLDAO.SPIDER_TABLE_NEWS_CMTNUM: {},
                                       SQLDAO.SPIDER_TABLE_NEWS_CLICKNUM: {},
                                       SQLDAO.SPIDER_TABLE_NEWS_VOTENUM: {},
                                       SQLDAO.SPIDER_TABLE_NEWS_FANSNUM: {}}
        self.url_beforenewsnum_map = {}
        self.url_curcmtcontent_map = {}
        self.url_curcmtnum_map = {}
        self.url_beforecmtnum_map = {}
        date = TimeUtility.getcurrentdate()
        path = os.path.join(SpiderConfigure.getconfig(const.SPIDER_STORAGE_DOMAIN,const.SPIDER_OUTPUT_PATH), date)
        suffix = SpiderConfigure.getconfig(const.SPIDER_STORAGE_DOMAIN,const.SPIDER_OUTPUT_FILENAME_SUFFIX)
        self.outputpath = FileFormat.OUTPUTPATH.format(path=path, suffix=suffix, date=date.replace('-', '_'), ts=int(time.time()))
        self.errorinfopath = FileFormat.ERRORINFOPATH.format(path=path, suffix=suffix, date=date.replace('-', '_'), ts=int(time.time()))
        self.pushpath = os.path.join(SpiderConfigure.getconfig(const.SPIDER_STORAGE_DOMAIN,const.SPIDER_PUSH_PATH_MASTER), date)
        if not FileUtility.exists(path):
            FileUtility.mkdirs(path)
        #if not FileUtility.exists(self.pushpath):
            #FileUtility.mkdirs(self.pushpath)

    #----------------------------------------------------------------------
    def dereplicate(self):
        #聚合相关信息后, 执行去重
        #获取本次所有key1=null且channel=201的所有url为urllist
        #遍历urllist中的url，且key1=null,channel=202的所有id为idlist
        #对idlist中的kye1打标记，该条记录不在输出
        idlist = []
        sql = 'SELECT url from news where key1 is null and channel=201'
        sqlf= 'SELECT id  from news where url=\"{url}\" and key1 is null and channel=202'
        results = SQLDAO.getinstance().execute(sql, find=True)
        for result in results:
            sql2 = sqlf.format(url=result[0])
            results2 = SQLDAO.getinstance().execute(sql2, find=True)
            if results2:
                Logger.getlogging().info('dereplicated url:\t{url}'.format(url=result[0]))
            for result2 in results2:
                idlist.append(result2[0])
        self.updatenewsflag(idlist)

    #----------------------------------------------------------------------
    def fileformat(self):
        self.aggregate_beforenewsinfo()
        self.aggregate_beforenewsnum()
        self.aggregate_curcomments()
        self.aggregate_curcmtnum()
        self.aggregate_beforecmtsnum()
        self.dereplicate()
        urllist = []
        idlist = []
        newscond = '{key} is null'.format(key=SQLDAO.SPIDER_TABLE_NEWS_KEY1)
        results = SQLDAO.getinstance().find(SQLDAO.SPIDER_TABLE_NEWS, where=newscond)
        for result in results:
            doc = SQLDAO.getdictdata(SQLDAO.SPIDER_TABLE_NEWS_KEYS, result)
            id = doc[SQLDAO.SPIDER_TABLE_NEWS_ID]
            url = doc[SQLDAO.SPIDER_TABLE_NEWS_URL].strip()
            try:
                urlmd5 = Common.md5(url)
                channel = doc.get(SQLDAO.SPIDER_TABLE_NEWS_CHANNEL, '201')
                title = doc.get(SQLDAO.SPIDER_TABLE_NEWS_TITLE, '')
                body = doc.get(SQLDAO.SPIDER_TABLE_NEWS_BODY, '')
                commentlist = self.url_curcmtcontent_map.get(urlmd5, [])
                comments = ' '.join(commentlist)
                pubtime = doc.get(SQLDAO.SPIDER_TABLE_NEWS_PUBLISH_DATE, TimeUtility.getintformtime(0))
                crawlertime = doc.get(SQLDAO.SPIDER_TABLE_NEWS_CREATE_DATE, TimeUtility.getintformtime(0))
                type = doc.get(SQLDAO.SPIDER_TABLE_NEWS_TYPE, '')
                query = doc.get(SQLDAO.SPIDER_TABLE_NEWS_QUERY, '')
                #评论量增量推送
                #      第一次推送全量：如果comments对应的内容没有被取过(key1没有标记1)，则应推送全量
                #                     此时如果news中cmtnum>0,则推送news中的cmtnum，否则推送comment中的cmtnum(已经聚合到url_curcmtnum_map中)
                #      第二次推送增量：如果comments对应的内容有取过(key1有部分标记1)，则应推送增量，推送comment中的cmtnum(已经聚合到url_curcmtnum_map中)
                cmtkey1flag = self.url_beforecmtnum_map.get(urlmd5, -1)
                if cmtkey1flag <= 0:
                    cmtnum = doc.get(SQLDAO.SPIDER_TABLE_NEWS_CMTNUM, -1)
                    if cmtnum < 0:
                        cmtnum = self.url_curcmtnum_map.get(urlmd5, 0)
                else:
                    cmtnum = self.url_curcmtnum_map.get(urlmd5, 0)
                #其他增量
                clicknum = doc.get(SQLDAO.SPIDER_TABLE_NEWS_CLICKNUM, -1)
                clicknum = self.increment(urlmd5, SQLDAO.SPIDER_TABLE_NEWS_CLICKNUM, clicknum)
                votenum = doc.get(SQLDAO.SPIDER_TABLE_NEWS_VOTENUM, -1)
                votenum = self.increment(urlmd5, SQLDAO.SPIDER_TABLE_NEWS_VOTENUM, votenum)
                fansnum = doc.get(SQLDAO.SPIDER_TABLE_NEWS_FANSNUM, -1)
                fansnum = self.increment(urlmd5, SQLDAO.SPIDER_TABLE_NEWS_FANSNUM, fansnum)
                string = FileFormat.DEFAULT_NEWS_FORMAT.format(channel=channel,
                                                               url=url,
                                                               title=self.strfilter(title),
                                                               body=self.strfilter(body),
                                                               comments=comments,
                                                               cmtnum=cmtnum,
                                                               clicknum=clicknum,
                                                               votenum=votenum,
                                                               fansnum=fansnum,
                                                               pubtime=TimeUtility.getinttime(pubtime),
                                                               crawlertime=crawlertime,
                                                               type=type,
                                                               query=self.strfilter(query))
                Logger.getlogging().info(u'{channel}\t{query}\t{url}'.format(channel=channel, query=query, url=url).encode(constant.CHARSET_UTF8))
                if not title:
                    FileUtility.writeline(self.errorinfopath, string.encode(constant.CHARSET_UTF8))
                else:
                    FileUtility.writeline(self.outputpath, string.encode(constant.CHARSET_UTF8))

                if id not in idlist:
                    idlist.append(id)
                if title and commentlist:
                    if url not in urllist:
                        urllist.append(url)
            except:
                Logger.getlogging().error(str(result))
                Logger.printexception()
        #已经提取过，则变更key1标记为1
        self.updatenewsflag(idlist)
        self.updatecommentsflag(urllist)
        ##推送数据到指定路径
        #if FileUtility.exists(self.outputpath):
            #FileUtility.copy(self.outputpath, self.pushpath)
    #----------------------------------------------------------------------
    def increment(self, urlmd5, key, curnum):
        #计算增量
        #1.当前某字段取值<=0,则表示该字段未取到值或者等于0,此时无法计算增量（不需要计算增量）
        #2.当前某字段取值>0, 则表示该字段正常取值，此时可以计算增量
        #      2.1如果该url是新url(key1没有标记1)，返回该值
        #      2.2如果该url是旧url(key1有部分标记1),则用当前量减去已经取过的量的最大值【因为增量始终是最大值之差】
        #      2.3在2.2情况下如何提取已取过url的最大值，首先是值是有效的，其次是按抓取时间排序，取最大抓取时间对应的值
        if curnum <= 0:
            return 0
        newskey1flag = self.url_beforenewsnum_map.get(urlmd5, -1)
        if newskey1flag > 0:
            before = self.url_beforenewsinfo_map[key].get(urlmd5, 0)
            curnum = curnum - before
        return curnum
    #----------------------------------------------------------------------
    def aggregate_curcomments(self):
        #汇总本次未推送的评论
        sqlf = 'SELECT {url},{content},{publish} from {table} where {key1} is null'
        sql = sqlf.format(table=SQLDAO.SPIDER_TABLE_COMMENTS,
                          url=SQLDAO.SPIDER_TABLE_COMMENTS_URL,
                          content=SQLDAO.SPIDER_TABLE_COMMENTS_CONTENT,
                          publish=SQLDAO.SPIDER_TABLE_COMMENTS_PUBLISH_DATE,
                          key1=SQLDAO.SPIDER_TABLE_COMMENTS_KEY1)
        cmtsresults = SQLDAO.getinstance().execute(sql, find=True)
        for cmtsresult in cmtsresults:
            urlmd5 = Common.md5(cmtsresult[0])
            content = self.strfilter(cmtsresult[1])
            publish = TimeUtility.getinttime(cmtsresult[2])
            if urlmd5 not in self.url_curcmtcontent_map:
                self.url_curcmtcontent_map[urlmd5] = []
            self.url_curcmtcontent_map[urlmd5].append(content + '_' + str(int(publish)))

    #----------------------------------------------------------------------
    def aggregate_curcmtnum(self):
        #计算本次未推送过的评论数量
        sqlf = 'SELECT {url},count(*) from {table} where {key1} is null group by {url}'
        sql = sqlf.format(table=SQLDAO.SPIDER_TABLE_COMMENTS,
                          url=SQLDAO.SPIDER_TABLE_COMMENTS_URL,
                          key1=SQLDAO.SPIDER_TABLE_COMMENTS_KEY1)
        results = SQLDAO.getinstance().execute(sql, find=True)
        for result in results:
            key = Common.md5(result[0].strip())
            if key not in self.url_curcmtnum_map:
                self.url_curcmtnum_map[key] = int(result[1])
    #----------------------------------------------------------------------
    def aggregate_beforecmtsnum(self):
        #计算本次之前已经推送过的数量
        #如果key1标记为1,则表示该url对应的comments已经推送过一部分；否则表示未推送过
        sqlf = 'SELECT {url},count(*) from {table} where {key1}=1 group by {url}'
        sql = sqlf.format(table=SQLDAO.SPIDER_TABLE_COMMENTS,
                          url=SQLDAO.SPIDER_TABLE_COMMENTS_URL,
                          key1=SQLDAO.SPIDER_TABLE_COMMENTS_KEY1)
        results = SQLDAO.getinstance().execute(sql, find=True)
        for result in results:
            key = Common.md5(result[0].strip())
            if key not in self.url_beforecmtnum_map:
                self.url_beforecmtnum_map[key] = int(result[1])

    #----------------------------------------------------------------------
    def aggregate_beforenewsinfo(self):
        #如何提取已取过url的最大值
        #1.首先值是有效的，并存储对应的值的抓取时间
        #2.其次按抓取时间排序，取最大抓取时间对应的值
        sqlf = 'SELECT {url},{createtime},{cmtnum}, {clicknum},{votenum},{fansnum} from {table} where  {key1}=1'
        sql = sqlf.format(table=SQLDAO.SPIDER_TABLE_NEWS,
                          url=SQLDAO.SPIDER_TABLE_NEWS_URL,
                          createtime=SQLDAO.SPIDER_TABLE_NEWS_CREATE_DATE,
                          cmtnum=SQLDAO.SPIDER_TABLE_NEWS_CMTNUM,
                          clicknum=SQLDAO.SPIDER_TABLE_NEWS_CLICKNUM,
                          votenum=SQLDAO.SPIDER_TABLE_NEWS_VOTENUM,
                          fansnum=SQLDAO.SPIDER_TABLE_NEWS_FANSNUM,
                          key1=SQLDAO.SPIDER_TABLE_NEWS_KEY1)
        results = SQLDAO.getinstance().execute(sql, find=True)
        cmtnumlist = {}
        clicknumlist = {}
        votenumlist = {}
        fansnumlist = {}
        for result in results:
            url = result[0].strip()
            urlmd5 = Common.md5(url)
            createtime = result[1]
            cmtnum = result[2]
            clicknum = result[3]
            votenum = result[4]
            fansnum = result[5]
            if urlmd5 not in cmtnumlist:
                cmtnumlist[urlmd5] = {}
            if urlmd5 not in clicknumlist:
                clicknumlist[urlmd5] = {}
            if urlmd5 not in votenumlist:
                votenumlist[urlmd5] = {}
            if urlmd5 not in fansnumlist:
                fansnumlist[urlmd5] = {}
            #存储有效的值(>0)及对应的抓取时间
            if cmtnum > 0:
                if cmtnumlist[urlmd5].get(str(createtime), 0) <= cmtnum:
                    cmtnumlist[urlmd5][str(createtime)] = cmtnum
            if clicknum > 0:
                if clicknumlist[urlmd5].get(str(createtime), 0) <= clicknum:
                    clicknumlist[urlmd5][str(createtime)] = clicknum
            if votenum > 0:
                if votenumlist[urlmd5].get(str(createtime), 0) <= votenum:
                    votenumlist[urlmd5][str(createtime)] = votenum
            if fansnum > 0:
                if fansnumlist[urlmd5].get(str(createtime), 0) <= fansnum:
                    fansnumlist[urlmd5][str(createtime)] = fansnum
        for urlmd5, value in cmtnumlist.iteritems():
            if not value:
                continue
            self.url_beforenewsinfo_map[SQLDAO.SPIDER_TABLE_NEWS_CMTNUM][urlmd5] = value[max(value)]
        for urlmd5, value in clicknumlist.iteritems():
            if not value:
                continue
            self.url_beforenewsinfo_map[SQLDAO.SPIDER_TABLE_NEWS_CLICKNUM][urlmd5] = value[max(value)]
        for urlmd5, value in votenumlist.iteritems():
            if not value:
                continue
            self.url_beforenewsinfo_map[SQLDAO.SPIDER_TABLE_NEWS_VOTENUM][urlmd5] = value[max(value)]
        for urlmd5, value in fansnumlist.iteritems():
            if not value:
                continue
            self.url_beforenewsinfo_map[SQLDAO.SPIDER_TABLE_NEWS_FANSNUM][urlmd5] = value[max(value)]

    #----------------------------------------------------------------------
    def aggregate_beforenewsnum(self):
        #计算url本次之前已经推送过的次数
        #如果key1标记为1,则表示该url对应的news id已经推送过；否则表示未推送过
        sqlf = 'SELECT {url},count(*) from {table} where {key1}=1 group by {url}'
        sql = sqlf.format(table=SQLDAO.SPIDER_TABLE_NEWS,
                          url=SQLDAO.SPIDER_TABLE_NEWS_URL,
                          key1=SQLDAO.SPIDER_TABLE_NEWS_KEY1)
        results = SQLDAO.getinstance().execute(sql, find=True)
        for result in results:
            key = Common.md5(result[0].strip())
            if key not in self.url_beforenewsnum_map:
                self.url_beforenewsnum_map[key] = int(result[1])
    #----------------------------------------------------------------------
    def updatenewsflag(self, idlist):
        if idlist:
            idlist = [item.encode(constant.CHARSET_UTF8) for item in idlist]
            sqlf = 'UPDATE {table} SET {key1}=1  WHERE {key1} is null and {id} in {idlist}'
            sql = sqlf.format(table=SQLDAO.SPIDER_TABLE_NEWS,
                              key1=SQLDAO.SPIDER_TABLE_NEWS_KEY1,
                              id=SQLDAO.SPIDER_TABLE_NEWS_ID,
                              idlist=tuple(idlist))
            Logger.getlogging().info('UPDATE news SET key1=1 where key1 is null')
            SQLDAO.getinstance().execute(sql)
    #----------------------------------------------------------------------
    def updatecommentsflag(self, urllist):
        if urllist:
            urllist = [item.encode(constant.CHARSET_UTF8) for item in urllist]
            sqlf = 'UPDATE {table} SET {key1}=1  WHERE {key1} is null and {url} in {urllist}'
            sql = sqlf.format(table=SQLDAO.SPIDER_TABLE_COMMENTS,
                              key1=SQLDAO.SPIDER_TABLE_COMMENTS_KEY1,
                              url=SQLDAO.SPIDER_TABLE_COMMENTS_URL,
                              urllist=tuple(urllist))
            Logger.getlogging().info('UPDATE comments SET key1=1 where key1 is null')
            SQLDAO.getinstance().execute(sql)

    #----------------------------------------------------------------------
    @staticmethod
    def strfilter(string):
        if not string:
            return ''
        replaces = ['\n', '\r', '\t', ' ']
        for replace in replaces:
            string = string.replace(replace, '')
        return string

if __name__ == '__main__':
    import sys
    reload(sys)
    Logger.getlogging().info('set encoding={charset}!'.format(charset=constant.CHARSET_UTF8))
    sys.setdefaultencoding(constant.CHARSET_UTF8)
    FileFormat().fileformat()
    Logger.getlogging().info('FINISH')


import re

re = re.compile(r'^\+5426115[0-9]{7}$')

while True:
    tel = input("Ingrese su numero de telefono: ")

    if re.match(tel):
        print("El número ingresado es válido")
        break
    print("Número no válido. Intente nuevamente")


# -*- coding: UTF-8 -*-
from django.shortcuts import render_to_response,Http404,HttpResponse
from django.template import RequestContext
from django.views.decorators.csrf import ensure_csrf_cookie
from order.models import Order,Order_goods,CustomerAddress
from cart.models import Cart_customer,Cart_goods
from shop.models import Goods_no, Goods_shoe, Shop,Goods_color_picture
from cart.util import checkSameShop
from django.views.decorators.http import require_POST,require_GET
from accounts.decorators import login_needed, active_required
import json

@ensure_csrf_cookie
@require_POST
@login_needed
@active_required
def confirmOrder(request):
    cart_goods_id_list = request.POST.get("cart_goods_id_list",'')
    try:
        cart_goods_id_arr = cart_goods_id_list.split(',')
    except ValueError:
        raise Http404

    uid = request.session.get('uid', None)
    assert uid

    try:
        cart = Cart_customer.objects.get(uid = uid)
    except Cart_customer.DoesNotExist:
        print(" Cart_customer.DoesNotExist")
        cart = Cart_customer(uid=uid)
        cart.save()
    try:
        goods_list = Cart_goods.objects.filter(cart_id = cart.id,goods_cart_type = 0)
    except Cart_goods.DoesNotExist:
        print(" Cart_goods.DoesNotExist")
        raise Http404

    shop_info = []
    total_money = 0
    for goods in goods_list:
        if str(goods.cart_goods_id) in cart_goods_id_arr:
            goods_tmp = {}
            goods_tmp['cart_goods_id'] = goods.cart_goods_id
            goods_tmp['goods_num'] = goods.goods_num
            goods_no = Goods_no.objects.get(goods_no = goods.goods_no)
            goods_tmp['goods_color'] = goods_no.goods_color
            goods_tmp['goods_size'] = goods_no.goods_size
            goods_tmp['goods_unit_price'] = goods_no.goods_price
            goods_shoe = Goods_shoe.objects.get(goods_id = goods_no.goods_id)
            goods_tmp['goods_title'] = goods_shoe.goods_title
            goods_tmp['goods_pic'] = goods_shoe.goods_pic_url
            shop_id = goods_shoe.shop_id
            goods_tmp['original_price'] = goods_shoe.goods_price
            goods_tmp['price'] = int(goods_tmp['goods_unit_price']) * int(goods_tmp['goods_num'])
            total_money += goods_tmp['price']
            shop_goods_info = checkSameShop(shop_info,shop_id)
            if shop_goods_info == None:
                shop = Shop.objects.get(shop_id = goods_shoe.shop_id)
                shop_info.append({'shop_id':shop.shop_id ,'shop_name': shop.shop_name,'goods':[goods_tmp]})
            else:
                shop_goods_info['goods'].append(goods_tmp)

    addressInfo = []
    try:
        addressList = CustomerAddress.objects.filter(customer_id = uid,status_type=0)
        for address in addressList:
            tmp = {}
            tmp['address_id'] = address.address_id
            tmp['receiver_name'] = address.receiver_name
            tmp['receiver_cellphone'] = address.receiver_cellphone
            tmp['address_school'] = address.address_school
            tmp['address_campus'] = address.address_campus
            tmp['spec_address'] = address.spec_address
            if request.ulexy_user.default_address_id != None and request.ulexy_user.default_address_id == address.address_id:
                tmp['is_default_address'] = True
            else:
                tmp['is_default_address'] = False
            addressInfo.append(tmp)
    except CustomerAddress.DoesNotExist:
        print("address not exist")

    print(addressInfo)
    return render_to_response('order/confirmOrder.html',{'step':2,'shop_info':shop_info,'total_money':total_money,'addressInfo':addressInfo},context_instance=RequestContext(request))

@require_POST
@login_needed
@active_required
def submitOrder(request):
    address_id = request.POST.get("address_id",'')
    cart_goods_list = request.POST.get("cart_goods_list",'')
    try:
        address_id = int(address_id)
    except ValueError:
        raise Http404

    uid = request.session.get('uid', None)
    if not uid:
        return HttpResponse(json.dumps({'status':'FAIL',"error":"用户未登录"},ensure_ascii = False))

    try:
        cart=Cart_customer.objects.get(uid=uid)
    except:
        return HttpResponse(json.dumps({'status':'FAIL',"error":"没有对应购物车信息"},ensure_ascii = False))

    cartGoodsList = Cart_goods.objects.filter(cart_id=cart.id ,goods_cart_type = 0)
    goodsInfo = []
    total_money = 0;
    for goods in cartGoodsList:
        if str(goods.cart_goods_id) in cart_goods_list:
            tmp = {}
            tmp['goods_no'] = goods.goods_no
            tmp['goods_num'] = goods.goods_num
            goods.goods_cart_type = 1;
            goods.save()

            goods_no = Goods_no.objects.get(goods_no = tmp['goods_no'])
            goods_no.goods_num_saled = goods_no.goods_num_saled + tmp['goods_num']
            tmp['goods_price'] = goods_no.goods_price
            goods_no.save()
            goodsInfo.append(tmp)
            total_money += tmp['goods_num'] * tmp['goods_price']
    order = Order(customer_id = uid, address_id = address_id,order_status=3,delivery_status=2,order_currency_value = total_money)
    order.save()

    for goods in goodsInfo:
        order_goods = Order_goods(order_id = order.order_id, goods_no = goods['goods_no'],goods_num = goods['goods_num'] )
        order_goods.save()

    return HttpResponse(json.dumps({'status':'SUCCESS',"order_id":order.order_id},ensure_ascii = False))

@require_GET
@login_needed
@active_required
def showOrder(request):
    order_id = request.GET.get("order_id","")
    assert order_id
    order_id = int(order_id)

    uid = request.session.get('uid', None)
    if not uid:
        return HttpResponse(json.dumps({'status':'FAIL',"error":"用户未登录"},ensure_ascii = False))

    try:
        order = Order.objects.get(customer_id = uid,order_id = order_id )
    except:
        raise Http404
    if request.META.get('HTTP_REFERER').endswith('/order/confirmOrder/'):
        return render_to_response('order/orderID.html',{'step':3,'order_id':order_id},context_instance=RequestContext(request))
    else:
        raise Http404

@require_GET
@login_needed
@active_required
def myOrder(request):
    uid = request.session.get('uid', None)
    orderList = Order.objects.filter(customer_id = uid).order_by("-order_date_generated")
    if len(orderList) > 10:
        orderList = orderList[0:10]
    order_info_list = []
    for order in orderList:
        tmp = {'shop_list':[],'goods_list':[],'money':order.order_currency_value,'status':order.order_status }
        order_id = order.order_id
        tmp['order_date'] = order.order_date_generated
        tmp['order_id'] = order_id
        order_goods_list = Order_goods.objects.filter(order_id = order_id)
        assert len(order_goods_list) != 0

        customerAddress = CustomerAddress.objects.get(address_id = order.address_id)
        tmp['address_receiver'] = customerAddress.receiver_name
        for order_goods in order_goods_list:
            goods_no = Goods_no.objects.get(goods_no = order_goods.goods_no)
            # goods_color_picture = Goods_color_picture.objects.get(goods_id = goods_no.goods_id,goods_color = goods_no.goods_color)
            tmp_goods = {}
            # tmp_goods['pic'] = goods_color_picture.goods_pic_url;
            goods_shoe = Goods_shoe.objects.get(goods_id = goods_no.goods_id)
            tmp_goods['pic'] = goods_shoe.goods_pic_url
            tmp_goods['title'] = "%s  %s %s" %(goods_shoe.goods_title,goods_no.goods_color,goods_no.goods_size)
            tmp['goods_list'].append(tmp_goods)
            shop_id = goods_shoe.shop_id
            if not alreadyIndict(tmp['shop_list'], shop_id):
                shop = Shop.objects.get(shop_id = shop_id)
                tmp_shop = {}
                tmp_shop['shop_name'] = shop.shop_name
                tmp_shop['shop_id'] = shop_id
                tmp_shop['shop_url'] = "/shop/%s/" %(shop_id)
                tmp['shop_list'].append(tmp_shop)
        order_info_list.append(tmp)
    print(order_info_list)
    return render_to_response('order/myOrder.html',{ "order_info_list":order_info_list },context_instance=RequestContext(request))

def alreadyIndict(shop_dict_list,shop_id):
    for shopInfo in shop_dict_list:
        if shopInfo['shop_id'] == shop_id:
            return True
    return False


import os
import re
from typing import List, Optional

import click
import dnnlib
import numpy as np
import PIL.Image
import legacy

import torch
import torch.nn as nn
import torchvision.models as models
from torch.utils.tensorboard import SummaryWriter

def get_batch(num_iter_G, batch_size_gen, G, device, label, truncation_psi, noise_mode, mean, std):
    img_batch = []
    w_batch = []
    for i in range(num_iter_G):
        z = torch.from_numpy(np.random.randn(batch_size_gen, G.z_dim)).to(device)
        ws = G.mapping(z, label, truncation_psi=truncation_psi) # (b,16,512) -> 512 vector is copied 16 times! (num_ws==16, w_dim=512)
        img = G.synthesis(ws, noise_mode=noise_mode) # (b,3,256,256)
        img = (img * 127.5 + 128).clamp(0, 255).to(torch.uint8).div(255.) # (b,3,256,256)
        img_batch.append(img) # list of (b,3,256,256)
        w_batch.append(ws[:,0]) # list of (b,512)

    img_batch = torch.cat(img_batch)
    inp_batch = (img_batch-mean)/std
    w_batch = torch.cat(w_batch)

    return img_batch, inp_batch, w_batch

def reconstruct_img(num_iter_G, batch_size_gen, G, output, num_ws, noise_mode):
    img_recon = []
    for i in range(num_iter_G):
        ws = output[i*batch_size_gen:(i+1)*batch_size_gen].unsqueeze(1).repeat(1, num_ws, 1) # (b,512) -> (b,16,512)
        img = G.synthesis(ws, noise_mode=noise_mode)
        img = (img * 127.5 + 128).clamp(0, 255).to(torch.uint8).div(255.)
        img_recon.append(img)
    img_recon = torch.cat(img_recon)
    return img_recon

@click.command()
@click.pass_context
@click.option('--network', 'network_pkl', help='Network pickle filename', required=True)
@click.option('--seed', type=int, help='Random seed', default=0, show_default=True)
@click.option('--trunc', 'truncation_psi', type=float, help='Truncation psi', default=1, show_default=True)
@click.option('--noise_mode', help='Noise mode', type=click.Choice(['const', 'random', 'none']), default='const', show_default=True)
@click.option('--outdir', help='Where to save the output images', type=str, required=True, metavar='DIR')
@click.option('--class', 'class_idx', type=int, help='Class label (unconditional if not specified)')
@click.option('--mode', type=int, help='training mode', default=1, show_default=True)
@click.option('--num_iters', type=int, help='total number of iterations', required=True)
@click.option('--iter_decay1', type=int, help='first decay point', required=True)
@click.option('--iter_decay2', type=int, help='second decay point', required=True)
@click.option('--batch_size', type=int, help='batch size', default=64, show_default=True)
@click.option('--batch_size_gen', type=int, help='batch size for generator', default=8, show_default=True)
@click.option('--lr', type=float, help='initial learning rate', default=0.0001, show_default=True)
def train_val(
    ctx: click.Context,
    network_pkl: str,
    seed: int,
    truncation_psi: float,
    noise_mode: str,
    outdir: str,
    class_idx: Optional[int],
    mode: int,
    num_iters: int,
    iter_decay1: int,
    iter_decay2: int,
    batch_size: int,
    batch_size_gen: int,
    lr: float,
):

    print('Loading networks from "%s"...' % network_pkl)
    device = torch.device('cuda')
    with dnnlib.util.open_url(network_pkl) as f:
        G = legacy.load_network_pkl(f)['G_ema'].to(device) # type: ignore

    os.makedirs(outdir, exist_ok=True)

    # Labels.
    label = torch.zeros([1, G.c_dim], device=device)
    if G.c_dim != 0:
        if class_idx is None:
            ctx.fail('Must specify class label with --class when using a conditional network')
        label[:, class_idx] = 1
    else:
        if class_idx is not None:
            print ('warn: --class=lbl ignored when running on an unconditional network')

    np.random.seed(seed)
    model = models.__dict__['resnet50'](pretrained=True)
    model.fc = nn.Linear(2048, 512)
    model.to(device)

    num_ws = 16
    mean = torch.tensor([0.485, 0.456, 0.406]).view(1,3,1,1).to(device)
    std = torch.tensor([0.229, 0.224, 0.225]).view(1,3,1,1).to(device)
    criterion_param = nn.MSELoss().to(device)
    criterion_recon = nn.MSELoss().to(device)
    optimizer = torch.optim.AdamW(model.parameters(), lr=lr)

    exp_name = '%d_%d-%d-%d_%g' % (mode, iter_decay1, iter_decay2, num_iters, lr)
    logger = SummaryWriter(os.path.join(outdir, exp_name))

    for i in range(1, num_iters+1):
        img_batch, inp_batch, w_batch = get_batch(batch_size // batch_size_gen, batch_size_gen, G, device, label, truncation_psi, noise_mode, mean, std)
        output = model(inp_batch) # (b,512)
        loss = criterion_param(output, w_batch)

        if mode == 2:
            img_recon = reconstruct_img(batch_size // batch_size_gen, batch_size_gen, G, output, num_ws, noise_mode) # (b,3,256,256)
            loss += criterion_recon(img_recon, img_batch)

        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        if i % 100:
            print('loss: ')
            train_writer.add_scalar('loss', model.loss, model.total_steps)

        import pdb; pdb.set_trace()

#----------------------------------------------------------------------------

if __name__ == "__main__":
    train_val() # pylint: disable=no-value-for-parameter


# -*- coding: utf-8 -*-
import src.dao.feedback

import src.pydev.config
import src.pydev.logger
import src.pydev.mysql

conf = src.pydev.config.read_ini('core.ini')
logger = src.pydev.logger.create('daoaccess')

def get_user_feedbacks(offset, limit):
    conn = src.pydev.mysql.connection()
    cursor = conn.cursor()
    try:
        feedback_infos = src.dao.feedback.get_user_feedbacks(cursor, offset, limit)
        return_info = {
            'is_success': True,
            'feedback_infos': feedback_infos
        }
    except Exception as e:
        logger.exception(e)
        return_info = {
            'is_success': False,
            'des': str(e)
        }
    cursor.close()
    conn.close()
    return return_info

def get_platform_feedbacks(user_id):
    conn = src.pydev.mysql.connection()
    cursor = conn.cursor()

    try:
        feedback_infos = src.dao.feedback.get_platform_feedbacks(cursor, user_id)
        return_info = {
            'is_success': True,
            'feedback_infos': feedback_infos
        }
    except Exception as e:
        logger.exception(e)
        return_info = {
            'is_success': False,
            'des': str(e)
        }
    cursor.close()
    conn.close()
    return return_info

def feedback_to_user(user_id, content):
    conn = src.pydev.mysql.connection()
    cursor = conn.cursor()
    try:
        rows_count = src.dao.feedback.insert_platform_return_feedback(cursor, user_id, content)
    except Exception as e:
        logger.exception(e)
        return {
            'is_success': False,
            'des': str(e)
        }
    else:
        cursor.close()
        conn.commit()
        conn.close()
        if rows_count:
            return {
                'is_success': True,
                'user_id': user_id,
                'content': content
            }
        else:
            return {
                'is_success': False
            }

def get_platform_and_user_feedbacks(user_id):
    conn = src.pydev.mysql.connection()
    cursor = conn.cursor()
    try:
        response = src.dao.feedback.get_platform_and_user_feedbacks(cursor, user_id)
        num = len(response)
    except Exception as e:
        logger.exception(e)
        return {
            'is_success': False,
            'des': str(e)
        }
    else:
        return {
            'is_success': True,
            'feedback_infos': response,
            'total_num': num
        }


from constants import SEARCH_BOX, SEARCH_BUTTON, SEARCH_RESULT_SELECTOR
from pages.base_page import BasePage

class SearchPage(BasePage):

    def search_by_value(self, search_text):
        self.wait_for_element(SEARCH_BOX)
        search_box = self.driver.find_element_by_css_selector(SEARCH_BOX)
        search_box.click()
        search_box.send_keys(search_text)
        self.driver.find_element_by_css_selector(SEARCH_BUTTON).click()
        self.wait_for_ajax()

    def verify_result_count(self):
        result_count_list = []
        results = self.driver.find_element_by_css_selector(SEARCH_RESULT_SELECTOR).text
        result_count_list.append(results)
        return result_count_list


# @file config.py
# @func module definitions for framework.
#       Is used to register, define & load modules
#       Starts the main-framework
# @auth Hessel van der Molen
#       hmolen.science@gmail.com
# @date 4 may 2012

#dictionairy for module
moduledict = {}
#show frameworks print's (1=show, 0=do not show)
VERBOSE = 1

####
## Register modules:
#######

moduledict["main"]          = "main_v1"
moduledict["globals"]       = [1, "globals"]
moduledict["motion"]        = [1, "motion_v1"]
moduledict["sonar"]         = [1,"sonar_v1"]
moduledict["tools"]         = [1, "tools_v3"]
moduledict["vision"]        = [1, "vision_v1"]
moduledict["behaviour"]     = [1, "behaviour_v1"]

###########################
# start & run framework
###########################
from framework import mframework
mframework.startUpFramework(moduledict, VERBOSE)


# -*- coding: utf-8 -*-
# Author:       xiewenzhe<xiewenzhe@kingsoft.com>
# Date:         2019/04/19
# FileName:     globalvalue_tool.py
# Description:  Tool for globalvalue

from buildtools import log_tool

# 全局变量放在这里
globalvalue = {}

def set_value(value_name, value):
    globalvalue[value_name] = value

def get_value(value_name):
    try:
        return globalvalue[value_name]
    except Exception as e:
        log_tool.show_error('GlobalValue {} not Exist!'.format(value_name))
        return None


import config
import learner
import particle
import haar_features as haar
import numpy as np
import utils
from utils import measureTime
import cv2
import particle_utils

class Meanshift(object):
    def __init__(self):
        self.kernelSize = 3
        self.kernelSigma = 2
        pass

    def start(self, image, target):
        # Initialize particles
        self.iterations = 0
        self.initial = image
        self.roi = target
        self.target = target
        self.setup()
        self.scale = 1
        self.valid = True
        self.kernelChanged = False
        self.pf = particle.ParticleFilter(
            target, 30, image.shape[:2])

    def setup(self):
        roi = self.roi
        image = self.initial
        image = cv2.GaussianBlur(image, (self.kernelSize, self.kernelSize), self.kernelSigma)
        hsv =  cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
        targetFrame = utils.cropImage(hsv, roi)
        mask = self.getMask(targetFrame)
        roi_hist = cv2.calcHist([targetFrame], [0], mask, [180], [0,180])
        self.initial_hist = roi_hist
        self.roi_hist = roi_hist

    def getMask(self, image):
        return cv2.inRange(image, np.array((0., 60.,32.)), np.array((180.,255.,255.)))

    def hist(self, hsv_image):
        mask = self.getMask(hsv_image)
        return cv2.calcHist([hsv_image], [0], mask, [180], [0,180])

    def next(self, image):
        self.image = image

        if self.kernelChanged:
            self.setup()

        term_crit = ( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 15, 1 )

        image = cv2.GaussianBlur(image, (self.kernelSize, self.kernelSize), self.kernelSigma)
        hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
        dst = cv2.calcBackProject(
            [hsv], [0], self.roi_hist, [0, 180], self.scale)

        ret, track_window = cv2.meanShift(dst, tuple(map(int, self.target)), term_crit)

        self.target = track_window

        window = utils.cropImage(hsv, track_window)
        targetFrame = utils.cropImage(hsv, track_window)
        window = targetFrame
        hist = self.hist(window)
        #self.roi_hist = (self.initial_hist + hist) / 2
        self.dst = dst

        self.iterations += 1

if __name__ == "__main__":
    camera = False

    algo = Meanshift()

    filename, target = utils.loadVideoFromFile("Vid_A_ball")
    if camera:
        capture = cv2.VideoCapture(0)
    else:
        capture = cv2.VideoCapture(filename)
    ret ,frame = capture.read()

    cv2.namedWindow("img2")
    particle_utils.initializeMeanshiftSlider(algo, "img2")

    algo.start(frame,  target)
    while(1):
        ret ,frame = capture.read()
        if ret == True:
            algo.next(frame)

            # Draw it on image
            x,y,w,h = algo.target
            cv2.rectangle(frame, (int(x),int(y)), (int(x+w),int(y+h)), 255,2)
            cv2.imshow('img2', frame)
            cv2.imshow('prob', algo.dst)

            k = cv2.waitKey(60) & 0xff
            if k == 27 or algo.iterations == 10000:
                img2.shape
                break

        else:
            break

    cv2.destroyAllWindows()
    cap.release()


import pandas as pd
import numpy as np
import yfinance as yf

class StockAnalysis(self,ticker,df):
    def __init__(self,ticker,df):
        self.ticker = ticker
        self.df = df

    def expected_return(self):
        self.df['returns'] = self.df.pct_change(1)
        daily_return = self.df['returns'].mean()
        annual_return = ((daily_return+1)**252-1)*100
        return annual_return

    def standard_deviation(self):
        self.df['returns'] = self.df.pct_change(1)
        daily_std = np.std(self.df['returns'])
        annual_std = np.sqrt(252)*daily_std*100
        return annual_std

    def sharpe_ratio(self):
        sharpe = self.expected_return()/self.standard_deviation()
        return sharpe


from xai.brain.wordbase.nouns._squeegee import _SQUEEGEE

#calss header
class _SQUEEGEES(_SQUEEGEE, ):
	def __init__(self,):
		_SQUEEGEE.__init__(self)
		self.name = "SQUEEGEES"
		self.specie = 'nouns'
		self.basic = "squeegee"
		self.jsondata = {}


#!/usr/bin/env python

import csv
import datetime

# pip install matplotlib
import matplotlib.pyplot as plt

def main():
    csv_file = './temperature_20190102_015358.csv'
    with open(csv_file, 'r') as file:
        reader = csv.DictReader(file)
        data_x = []
        data_y = []
        for row in reader:
            timestamp = datetime.datetime.strptime(
                row['timestamp'], '%Y%m%d_%H%M%S.%f'
            )
            temperature = int(row['temperature'])

            data_x.append(timestamp)
            data_y.append(temperature)

            # print(row)
            # print(timestamp, type(timestamp))
            # print(temperature, type(temperature))

    print(data_x)
    print(data_y)

    plt.plot(data_x, data_y)
    plt.title('Temperature - Time\n({})'.format(csv_file))
    plt.xlabel('Timestamp')
    plt.ylabel('Temperature')
    plt.show()

if __name__ == '__main__':
    main()


import warnings
warnings.filterwarnings(action='ignore', category=UserWarning, module='gensim')
from gensim.corpora import WikiCorpus
import argparse
import re
import glob
import os

# RE_TOKEN = re.compile(r"((\d+(,\d{3})*(\.\d+)?)|([^\W\d]+)|(\S))", re.UNICODE) # normal number
# RE_TOKEN = re.compile(r"(((-?\d+(,\d{3})*(\.\d+)?)\/(-?\d+(,\d{3})*(\.\d+)?))|(-?\d+(,\d{3})*(\.\d+)?)|([^\W\d]+)|(\S))") # number, negative, fraction
RE_TOKEN = re.compile(r"(((\w+-\d+)|(\w+-\w+)|((-?\d+(,\d{3})*(\.\d+)?)\/(-?\d+(,\d{3})*(\.\d+)?))|(-?\d+(,\d{3})*(\.\d+)?))|([^\W\d]+)|(\S))", re.UNICODE)

all_token_num = 0

def custom_tokenizer(text, token_min_len=1, token_max_len=20, lower=True):
	tokens = []
	for token in RE_TOKEN.findall(text):
		if isinstance(token, tuple):
			token = token[0]

		if len(token) > token_max_len or len(token) < token_min_len:
			pass

		else:
			if lower:
				token = token.lower()

			tokens.append(token)

	return tokens

def make_corpus(in_f, out_f):

	"""Convert Wikipedia xml dump file to text corpus"""
	global all_token_num
	output = open(out_f, 'w', encoding='utf-8')
	wiki = WikiCorpus(in_f, tokenizer_func=custom_tokenizer)

	i = 0
	for text in wiki.get_texts():
		all_token_num += len(text)
		output.write(bytes(' '.join(text), 'utf-8').decode('utf-8') + '\n')
		i = i + 1
		if (i % 10000 == 0):
			print('Processed ' + str(i) + ' articles, ' + str(all_token_num) + 'tokens in total')

	output.close()
	print('Processing complete!')

if __name__ == '__main__':
	parser = argparse.ArgumentParser()
	parser.add_argument('--input', type=str, help="wiki dumps file path")
	parser.add_argument('--output', type=str, help="processed file path")
	args = parser.parse_args()
	infile = args.input
	outfile = args.output
	if infile[-3:] == 'bz2':
		make_corpus(infile, outfile)

	elif os.path.isdir(infile):
		files = glob.glob(infile+'/*.bz2')
		print(files)
		for f in files:
			print('processing file: {}'.format(f), 'num of tokens now: {}'.format(all_token_num))
			make_corpus(f, f[:-3]+'txt')


# coding: utf-8

# In[2]:

import pandas as pd
import datetime
import struct
from google.cloud import bigtable
from google.cloud.bigtable import column_family
from google.cloud.bigtable import row_filters
import matplotlib.pyplot as plt
from sklearn.cluster import DBSCAN
import sklearn.utils
from sklearn.preprocessing import StandardScaler
import numpy as np

# In[3]:

df=pd.read_csv('loc.csv')

# In[4]:

df.head()

# In[5]:

df.shape

# In[6]:

df.drop(columns=[df.columns[0],
                 ],inplace=True)
df.head()

# In[7]:

df['Longitude'][0]

# In[8]:

from google.oauth2 import service_account

credentials = service_account.Credentials.from_service_account_file(
    'sih.json')

scoped_credentials = credentials.with_scopes(
    ['https://www.googleapis.com/auth/cloud-platform'])

# In[9]:

project_id=''
instance_id='accidentdb'
table_id='accident'

# In[10]:

client = bigtable.Client(project=project_id, admin=True,credentials=credentials)
instance = client.instance(instance_id)

# In[11]:

print('Creating the {} table.'.format(table_id))
table = instance.table(table_id)

# In[12]:

max_age_rule = column_family.MaxAgeGCRule(datetime.timedelta(days=1))

column_families = {'location': max_age_rule}
if not table.exists():
    table.create(column_families=column_families)
    print("Table Created")
else:
    print("Table {} already exists.".format(table_id))

# In[13]:

byte_data=struct.pack('>d', 0.75)

# In[14]:

print('Writing some gvalues to the table.')
column_family_id='location'
rows = []

for i in range(10000):
    row_key = 'Location{}'.format(i).encode()
    long=df['Longitude'][i]
    lat=df['Latitude'][i]
    row = table.direct_row(row_key)
    long_data=struct.pack('>d', long)
    lat_data=struct.pack('>d', lat)
    row.set_cell(column_family_id,
                 'Longitude',
                 long_data,
                 timestamp=datetime.datetime.utcnow())
    row.set_cell(column_family_id,
                 'Latitude',
                 lat_data,
                 timestamp=datetime.datetime.utcnow())
    rows.append(row)
    print('Successfully wrote row {}.'.format(row_key))
table.mutate_rows(rows)

# In[15]:

# $:cbt listinstances
# $:cbt -instance accidentdb count accident


class Solution:
    def leftSmaller(self, n, a):
        lst, stack = [], []
        for i in range(n):
            if len(stack) == 0:
                lst.append(-1)
            elif len(stack) > 0 and stack[-1] < a[i]:
                lst.append(stack[-1])
            elif len(stack) > 0 and stack[-1] >= a[i]:
                while (len(stack) > 0 and stack[-1] >= a[i]):
                    stack.pop()
                if len(stack) == 0:
                    lst.append(-1)
                else:
                    lst.append(stack[-1])
            stack.append(a[i])
        return lst

a = [1, 5, 0, 3, 4, 5]
n = len(a)

ans = Solution().leftSmaller(n, a)
print(ans)


from . import claymore_common

class Miner(claymore_common.Miner):
	def __init__(self, config):
		super().__init__(config)

		self.fullname = "Claymore's ZCash AMD GPU Miner %s" % self.version

	def args(self):
		args = [
			"-zpool", self.server,
			"-zwal", self.worker,
			"-zpsw", self.password,
		]
		return args + super().args()

	def paths(self):
		return {
			"windows": {
				"default": "12.6",
				"paths": {
					"12.6": "Claymore's ZCash AMD GPU Miner v12.6/ZecMiner64.exe",
					"12.5": "Claymore's ZCash AMD GPU Miner v12.5/ZecMiner64.exe",
				}
			},
			"linux": {
				"default": "12.6",
				"paths": {
					"12.6": "Claymore's ZCash AMD GPU Miner v12.6 - LINUX/zecminer64",
				}
			}
		}

	def env(self):
		return {
			"GPU_FORCE_64BIT_PTR": "1",
			"GPU_MAX_HEAP_SIZE": "100",
			"GPU_USE_SYNC_OBJECTS": "1",
			"GPU_MAX_ALLOC_PERCENT": "100",
			"GPU_SINGLE_ALLOC_PERCENT": "100"
		}