import numpy as np
import math
import argparse
import os
import gc
from PIL import Image
import time
import tensorflow as tf
from keras import backend as K
from keras.layers import Input
from keras.models import Model
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import Conv3D
from keras.layers.convolutional import MaxPooling2D
from keras.layers import Conv2D, MaxPooling2D, GlobalAveragePooling2D, Dropout, Activation, Average
from keras.layers.core import Activation
from keras.layers.core import Dense
from keras.layers.core import Flatten
from keras.layers.core import Reshape
from keras.layers import Embedding
from keras.layers import concatenate
import itertools as it
import random
import scipy.io as sio
gc.enable()

def create_cnn(width, height, depth): #CNN design modelled after googlenet inception based modules
	# initialize the input shape and channel dimension, assuming
    # The width and height of the input images in pixels
    # define the model input
    inputShape = (width, height, depth)
    input_image = Input(shape=inputShape, name='input_image')
    #each block represents a single inception module
    conv1 = Conv2D(16, kernel_size=(3, 3), strides=1, data_format="channels_last", padding='same', activation='relu')(input_image)
    conv1 = Conv2D(32, kernel_size=(3, 3), strides=1, data_format="channels_last", padding='same', activation='relu')(conv1)
    # Start of first inception module
    intconv1_1 = Conv2D(64, kernel_size=(1, 1), strides=1, data_format="channels_last", padding='same', activation='relu')(conv1)
    intconv1_1 = Conv2D(16, kernel_size=(1, 1), strides=1, data_format="channels_last", padding='same', activation='relu')(intconv1_1)

    intconv1_2 = Conv2D(64, kernel_size=(3, 3), strides=1, data_format="channels_last", padding='same', activation='relu')(conv1)
    intconv1_2 = Conv2D(16, kernel_size=(1, 1), strides=1, data_format="channels_last", padding='same', activation='relu')(intconv1_2)

    intconv1_3 = Conv2D(64, kernel_size=(5, 5), strides=1, data_format="channels_last", padding='same', activation='relu')(conv1)
    intconv1_3 = Conv2D(16, kernel_size=(1, 1), strides=1, data_format="channels_last", padding='same', activation='relu')(intconv1_3)

    intconv1_4 = MaxPooling2D(pool_size=(3, 3), strides=1, data_format="channels_last", padding='same')(conv1)

    intconv1 = concatenate([intconv1_1, intconv1_2, intconv1_3, intconv1_4])  #end of first inception module

    conv2 = Conv2D(32, kernel_size=(3, 3), strides=1, data_format="channels_last", padding='same', activation='relu')(intconv1)
    conv2 = MaxPooling2D(pool_size=(3, 3), strides=2, data_format="channels_last", padding='same')(conv2)
    # Start of Second inception module
    intconv2_1 = Conv2D(64, kernel_size=(1, 1), strides=1, data_format="channels_last", padding='same', activation='relu')(conv2)
    intconv2_1 = Conv2D(16, kernel_size=(1, 1), strides=1, data_format="channels_last", padding='same', activation='relu')(intconv2_1)

    intconv2_2 = Conv2D(64, kernel_size=(3, 3), strides=1, data_format="channels_last", padding='same', activation='relu')(conv2)
    intconv2_2 = Conv2D(16, kernel_size=(1, 1), strides=1, data_format="channels_last", padding='same', activation='relu')(intconv2_2)

    intconv2_3 = Conv2D(64, kernel_size=(5, 5), strides=1, data_format="channels_last", padding='same', activation='relu')(conv2)
    intconv2_3 = Conv2D(16, kernel_size=(1, 1), strides=1, data_format="channels_last", padding='same', activation='relu')(intconv2_3)

    intconv2_4 = MaxPooling2D(pool_size=(3, 3), strides=1, data_format="channels_last", padding='same')(conv2)

    intconv2 = concatenate([intconv2_1, intconv2_2, intconv2_3, intconv2_4])     # End of second inception module

    conv3 = Conv2D(32, kernel_size=(3, 3), strides=1, data_format="channels_last", padding='same', activation='relu')(intconv2)
    conv3 = MaxPooling2D(pool_size=(3, 3), strides=2, data_format="channels_last", padding='same')(conv3)
    # Start of third inception module
    intconv3_1 = Conv2D(64, kernel_size=(1, 1), strides=1, data_format="channels_last", padding='same', activation='relu')(conv3)
    intconv3_1 = Conv2D(16, kernel_size=(1, 1), strides=1, data_format="channels_last", padding='same', activation='relu')(intconv3_1)

    intconv3_2 = Conv2D(64, kernel_size=(3, 3), strides=1, data_format="channels_last", padding='same', activation='relu')(conv3)
    intconv3_2 = Conv2D(16, kernel_size=(1, 1), strides=1, data_format="channels_last", padding='same', activation='relu')(intconv3_2)

    intconv3_3 = Conv2D(64, kernel_size=(5, 5), strides=1, data_format="channels_last", padding='same', activation='relu')(conv3)
    intconv3_3 = Conv2D(16, kernel_size=(1, 1), strides=1, data_format="channels_last", padding='same', activation='relu')(intconv3_3)

    intconv3_4 = MaxPooling2D(pool_size=(3, 3), strides=1, data_format="channels_last", padding='same')(conv3)

    intconv3 = concatenate([intconv3_1, intconv3_2, intconv3_3, intconv3_4])     # End of Third inception module

    conv4 = Conv2D(32, kernel_size=(3, 3), strides=1, data_format="channels_last", padding='same', activation='relu')(intconv3)
    conv4 = MaxPooling2D(pool_size=(3, 3), strides=2, data_format="channels_last", padding='same')(conv4)
    # Start of Fourth inception module
    intconv4_1 = Conv2D(64, kernel_size=(1, 1), strides=1, data_format="channels_last", padding='same', activation='relu')(conv4)
    intconv4_1 = Conv2D(16, kernel_size=(1, 1), strides=1, data_format="channels_last", padding='same', activation='relu')(intconv4_1)

    intconv4_2 = Conv2D(64, kernel_size=(3, 3), strides=1, data_format="channels_last", padding='same', activation='relu')(conv4)
    intconv4_2 = Conv2D(16, kernel_size=(1, 1), strides=1, data_format="channels_last", padding='same', activation='relu')(intconv4_2)

    intconv4_3 = Conv2D(64, kernel_size=(5, 5), strides=1, data_format="channels_last", padding='same', activation='relu')(conv4)
    intconv4_3 = Conv2D(16, kernel_size=(1, 1), strides=1, data_format="channels_last", padding='same', activation='relu')(intconv4_3)

    intconv4_4 = MaxPooling2D(pool_size=(3, 3), strides=1, data_format="channels_last", padding='same')(conv4)

    intconv4 = concatenate([intconv4_1, intconv4_2, intconv4_3, intconv4_4])     # Start of fourth inception module

    conv5 = Conv2D(32, kernel_size=(3, 3), strides=1, data_format="channels_last", padding='same', activation='relu')(intconv4)
    conv5 = MaxPooling2D(pool_size=(3, 3), strides=2, data_format="channels_last", padding='same')(conv5)
    # Start of fifth inception module
    intconv5_1 = Conv2D(64, kernel_size=(1, 1), strides=1, data_format="channels_last", padding='same', activation='relu')(conv5)
    intconv5_1 = Conv2D(16, kernel_size=(1, 1), strides=1, data_format="channels_last", padding='same', activation='relu')(intconv5_1)

    intconv5_2 = Conv2D(64, kernel_size=(3, 3), strides=1, data_format="channels_last", padding='same', activation='relu')(conv5)
    intconv5_2 = Conv2D(16, kernel_size=(1, 1), strides=1, data_format="channels_last", padding='same', activation='relu')(intconv5_2)

    intconv5_3 = Conv2D(64, kernel_size=(5, 5), strides=1, data_format="channels_last", padding='same', activation='relu')(conv5)
    intconv5_3 = Conv2D(16, kernel_size=(1, 1), strides=1, data_format="channels_last", padding='same', activation='relu')(intconv5_3)

    intconv5_4 = MaxPooling2D(pool_size=(3, 3), strides=1, data_format="channels_last", padding='same')(conv5)

    intconv5 = concatenate([intconv5_1, intconv5_2, intconv5_3, intconv5_4])    # End of fifth inception module

    conv6 = Conv2D(32, kernel_size=(3, 3), strides=1, data_format="channels_last", padding='same', activation='relu')(intconv5)

    flat = Flatten()(conv6) # flatten conv layers into dense layer

    dense = Dense(512, activation='relu')(flat)
    main_output = Dense(196, activation='softmax')(dense) #196 outputs as dataset has 196 classes
    model = Model(inputs=[input_image], outputs=[main_output])
    return model

def save_image(filename, norm_arr): # function for saving image, used for debugging and testing only
    img = (norm_arr.copy()).astype(np.uint8)

    res = Image.fromarray(img).convert('L')
    res.save(filename)

def load_data(folder_path, mat_name): #function for loading test and training data
    os.chdir(folder_path)
    mat = sio.loadmat(str(mat_name) +".mat", squeeze_me=True)
    os.chdir('..')
    idx_name = mat['annotations']
    return idx_name

def load_meta(folder_path, mat_name): #function for loading test and metadata
    os.chdir(folder_path)
    mat = sio.loadmat(str(mat_name) +".mat", squeeze_me=True)
    os.chdir('..')
    idx_name = mat['class_names']
    return idx_name

def load_imgs(folder_path, img_name, width, height): #function for loading and resizing images
    os.chdir(folder_path)
    img = Image.open(str(img_name))
    os.chdir('..')
    return img

def filter_dataset( dataset): #filtering dataset for testing data
    filtering = np.zeros( (len(dataset) ), dtype=np.float)
    for idx in range(len(dataset)):
        test_flag  = dataset[idx][6]
        if test_flag == 1 :
            filtering[idx] = True
        else:
            filtering[idx] = False
    return filtering

def jpg_crop_grey_arr(image_name, width, height): #function for loading and resizing images
    with Image.open(str(image_name)) as img:
        lower_x = train_info[0][0]
        upper_x = train_info[0][2]
        lower_y = train_info[0][1]
        upper_y = train_info[0][3]
        bound_x = upper_x - lower_x
        bound_y = upper_y - lower_y
        img = np.array(img)
        if img.ndim < 3: #for normalizing images that only have one channel
            img_new = np.zeros( (np.size(img,0),np.size(img,1),3), dtype=np.float)
            img_new[:,:,0] = img [:,:]
            img_new[:,:,1] = img [:,:]
            img_new[:,:,2] = img [:,:]
            img = img_new
        else :
            pass

        if upper_y >= np.size(img, 0) : #for filtering images with incorrect boundary conditions
            img_crop = img
        elif upper_x >= np.size(img, 1) :
            img_crop = img
        else :
            img_crop = np.zeros( (bound_y,bound_x,3), dtype=np.float)
            for y_pos in range (bound_y):
                for x_pos in range (bound_x):
                    img_crop[y_pos, x_pos, 0:2] = img[ lower_y + y_pos , lower_x + x_pos, 0:2]
        img_crop = (img_crop.copy()).astype(np.uint8)
        img_crop = Image.fromarray(img_crop)
        img_crop = img_crop.resize((width,height), Image.ANTIALIAS) #resize to standard resolution
        img_crop = np.array(img_crop)
    return img_crop

def aug_data(raw_arr): # augmentation of training data to improve training performance
    res_LR = np.flip(raw_arr, 1) #flips left right

    res_90 = np.rot90(raw_arr, 1) #90 rotation
    res_90LR = np.flip(res_90, 1) #90 + left right flip

    res_180 = np.rot90(raw_arr, 2) #180 rotation
    res_LR180 = np.flip(res_180, 1) #180 + left right flip

    res_270 = np.rot90(raw_arr, 3) #90 rotation
    res_270LR = np.flip(res_270, 1) #90 + left right flip
    res = np.stack((raw_arr, res_90, res_90LR, res_180,res_LR,res_LR180, res_270, res_270LR), axis = 0 )
    return res

def data_load(folder_path, width, height): # function for loading images
    os.chdir(folder_path)
    img_arr = jpg_crop_grey_arr(train_info[0][5], width, height)      # apply greyscale and cropping
    img_arr_stack = np.reshape(img_arr, (1,128,128,3))
    for file_idx in range (2,len(train_info)):
        img_arr = jpg_crop_grey_arr(train_info[file_idx][5], width, height)
        img_arr = np.reshape(img_arr, (1,128,128,3))
        img_arr_stack = np.concatenate((img_arr_stack,img_arr), axis = 0 )
    os.chdir('..')
    return img_arr

def class_load(dataset): # function for loading class dataset
    train_class = np.zeros((len(dataset)), dtype=np.float)
    for file_idx in range (len(dataset)):
        train_class[file_idx] = dataset[file_idx][4]
    return train_class

def test_data_process(folder_path, batch_size, width, height, filtering): # function to load test and train images and classes
    os.chdir(folder_path)
    test_class = np.zeros((8041), dtype=np.int) #number of testing images specified
    train_class = np.zeros((8144), dtype=np.int) #number of training images specified
    test_class_count = 0
    train_class_count = 0
    for file_idx in range (len(test_info)):
        if filtering[file_idx] == True:
            test_class[test_class_count] = test_info[file_idx][5]
            test_class_count = test_class_count +1
        else:
            train_class[train_class_count] = test_info[file_idx][5]
            train_class_count = train_class_count +1
    test_img_stack = np.zeros( (8041,128,128,3) , dtype=np.float) # creating and sorting test and training class datasets
    train_img_stack = np.zeros((8144,128,128,3), dtype=np.float)
    train_count = 0
    test_count = 0
    for file_idx in range (len(test_info)):
        if filtering[file_idx] == True:
            text = test_info[file_idx][0][:0] + test_info[file_idx][0][8:]
            test_img_stack[test_count, :, :, :] = jpg_crop_grey_arr(text, width, height)
            test_count = test_count + 1
        else :
            text = test_info[file_idx][0][:0] + test_info[file_idx][0][8:]
            train_img_stack[train_count,:,:,:] = jpg_crop_grey_arr(text, width, height)
            train_count = train_count + 1
    os.chdir('..')
    return train_img_stack, test_img_stack, train_class, test_class

def hot_encode(dataset, num_classes): #function for hot encoding the dataset the sparse categorical format for softmax activation
    data_classes = np.zeros( (len(dataset), num_classes) , dtype=np.int)
    for idx in range(len(dataset)):
        data_classes[idx, dataset[idx]-1 ] = 1
    return data_classes

def data_gen(dataset, classes, num_classes): #generator for network input #generator improves efficency and RAM management since GPU training is used
    while True:
        class_set = np.zeros((8, num_classes), dtype=np.int)
        for idx in range(len(dataset)):
            img_arr = dataset[idx, : , : , :]
            img_arr = aug_data(img_arr)
            for class_idx in range(8) :
                class_set[class_idx, :] = classes[idx, :]
            yield img_arr, class_set

def data_reduction(dataset): #reduces images to one channel
    reduc_dataset = np.zeros((len(dataset),128,128,1), dtype=np.float)
    reduc_dataset[:,:,:,0] = dataset [:,:,:, 0]
    return reduc_dataset

if __name__ == "__main__":
    ap = argparse.ArgumentParser()
    ap.add_argument('-f', '--filepath', required=True, help="specify filepath")
    args = ap.parse_args()
    os.chdir(args.filepath)
    #create network
    model = create_cnn(128, 128, 1)
    model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
    model.summary()
    #loading training data
    train_info = load_data('car_devkit', 'cars_train_annos')
    print(np.array(train_info).shape)
    print(len(train_info[1] ))

    #loading testing data
    test_info = load_data('car_devkit', 'cars_annos')
    filtering = filter_dataset(test_info)
    print(len(test_info))
    print(len(filtering))

    #loading meta data
    meta_info = load_meta('car_devkit', 'cars_meta')
    print(len(meta_info))

    #loading and processing training and testing image dataset and classes
    print('dataset 1 processing')
    (train_dataset, test_dataset, train_classes, test_classes)= test_data_process('car_ims', 4, 128, 128, filtering)
    print(np.array(train_dataset).shape)
    print(np.array(test_dataset).shape)
    print(np.array(train_classes).shape)
    print(np.array(test_classes).shape)

    train_classes = hot_encode(train_classes, len(meta_info)) #hot encode class dataset for network training
    print(np.array(train_classes).shape)
    test_classes = hot_encode(test_classes,len(meta_info))
    print(np.array(test_classes).shape)

    test_dataset = data_reduction(test_dataset)
    train_dataset = data_reduction(train_dataset)
    print('final')
    print(np.array(train_dataset).shape)
    print(np.array(test_dataset).shape)

    # save_image(str('test')+".jpg", new_img)

    gc.collect()
    #training network
    model.fit_generator(data_gen(train_dataset,train_classes,len(meta_info)), steps_per_epoch=len(train_classes), epochs=100, validation_data=(test_dataset[:1000], test_classes[:1000]), max_queue_size=100, workers=1, use_multiprocessing=False, verbose = 1)
    gc.collect()
    #testing network on evaluation dataset, img number 1001 to 8041 of the testing dataset
    score = model.evaluate(test_dataset[1001:], test_classes[1001:], verbose=1)
    print('\n', 'Test accuracy:', score[1])


import logging
import logging.handlers
import LIMS2DB.classes as lclasses
import LIMS2DB.utils as lutils
import multiprocessing as mp
import statusdb.db as sdb
try:
	import queue as Queue
except ImportError:
	import Queue
import genologics_sql.tables as gt

from genologics.entities import Process
from genologics.config import BASEURI, USERNAME, PASSWORD
from genologics.lims import *

from genologics_sql.utils import *

def processWSUL(options, queue, logqueue):
    mycouch = sdb.Couch()
    mycouch.set_db("worksets")
    mycouch.connect()
    view = mycouch.db.view('worksets/name')
    mylims = Lims(BASEURI, USERNAME, PASSWORD)
    work=True
    procName = mp.current_process().name
    proclog = logging.getLogger(procName)
    proclog.setLevel(level=logging.INFO)
    mfh = QueueHandler(logqueue)
    mft = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    mfh.setFormatter(mft)
    proclog.addHandler(mfh)

    while work:
        #grabs project from queue
        try:
            ws_id = queue.get(block=True, timeout=3)
            proclog.info("Starting work on {}".format(ws_id))
        except Queue.Empty:
            work = False
            proclog.info("exiting gracefully")
            break
        else:
            wsp = Process(mylims, id=ws_id)
            if not wsp.date_run:
                continue
            lc = lclasses.LimsCrawler(mylims, wsp)
            lc.crawl()
            try:
                ws = lclasses.Workset(mylims,lc, proclog)
            except NameError:
                continue

            #If there is already a workset with that name in the DB
            if len(view[ws.obj['name']].rows) == 1:
                remote_doc=view[ws.obj['name']].rows[0].value
                #remove id and rev for comparison
                doc_id = remote_doc.pop('_id')
                doc_rev = remote_doc.pop('_rev')
                if remote_doc != ws.obj:
                    #if they are different, though they have the same name, upload the new one
                    ws.obj=lutils.merge(ws.obj, remote_doc)
                    ws.obj['_id'] = doc_id
                    ws.obj['_rev'] = doc_rev
                    mycouch.db[doc_id] = ws.obj
                    proclog.info("updating {0}".format(ws.obj['name']))
                else:
                    proclog.info("not modifying {0}".format(ws.obj['name']))
            elif len(view[ws.obj['name']].rows) == 0:
                #it is a new doc, upload it
                mycouch.save(ws.obj)
                proclog.info("saving {0}".format(ws.obj['name']))
            else:
                proclog.warn("more than one row with name {0} found".format(ws.obj['name']))
            #signals to queue job is done
            queue.task_done()

def masterProcess(options,wslist, mainlims, logger):
    worksetQueue = mp.JoinableQueue()
    logQueue = mp.Queue()
    childs = []
    procs_nb = 1;
    #Initial step : order worksets by date:
    logger.info("ordering the workset list")
    orderedwslist = sorted(wslist, key=lambda x:x.date_run)
    logger.info("done ordering the workset list")
    if len(wslist) < options.procs:
        procs_nb = len(wslist)
    else:
        procs_nb = options.procs

    #spawn a pool of processes, and pass them queue instance
    for i in range(procs_nb):
        p = mp.Process(target=processWSUL, args=(options,worksetQueue, logQueue))
        p.start()
        childs.append(p)
    #populate queue with data
    #CHEATING
    if options.queue:
        worksetQueue.put(options.queue)
        orderedwslist=[]
    for ws in orderedwslist:
        worksetQueue.put(ws.id)

    #wait on the queue until everything has been processed
    notDone=True
    while notDone:
        try:
            log = logQueue.get(False)
            logger.handle(log)
        except Queue.Empty:
            if not stillRunning(childs):
                notDone = False
                break

def stillRunning(processList):
    ret = False
    for p in processList:
        if p.is_alive():
            ret = True

    return ret

def masterProcessSQL(args ,wslist, logger):
    worksetQueue = mp.JoinableQueue()
    logQueue = mp.Queue()
    childs = []
    procs_nb = 1;
    if len(wslist) < args.procs:
        procs_nb = len(wslist)
    else:
        procs_nb = args.procs

    #spawn a pool of processes, and pass them queue instance
    for i in range(procs_nb):
        p = mp.Process(target=processWSULSQL, args=(args,worksetQueue, logQueue))
        p.start()
        childs.append(p)
    #populate queue with data
    for ws in wslist:
        worksetQueue.put(ws.processid)

    #wait on the queue until everything has been processed
    notDone=True
    while notDone:
        try:
            log = logQueue.get(False)
            logger.handle(log)
        except Queue.Empty:
            if not stillRunning(childs):
                notDone = False
                break

def processWSULSQL(args, queue, logqueue):
    work=True
    session=get_session()
    with open(args.conf) as conf_file:
        conf=yaml.load(conf_file, Loader=yaml.SafeLoader)
    couch=lutils.setupServer(conf)
    db=couch["worksets"]
    procName = mp.current_process().name
    proclog = logging.getLogger(procName)
    proclog.setLevel(level=logging.INFO)
    mfh = QueueHandler(logqueue)
    mft = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    mfh.setFormatter(mft)
    proclog.addHandler(mfh)

    while work:
        #grabs project from queue
        try:
            ws_id = queue.get(block=True, timeout=3)
            proclog.info("Starting work on {}".format(ws_id))
        except Queue.Empty:
            work = False
            proclog.info("exiting gracefully")
            break
        else:
            step=session.query(gt.Process).filter(gt.Process.processid == int(ws_id)).one()
            ws=lclasses.Workset_SQL(session, proclog, step)
            doc={}
            for row in db.view('worksets/lims_id')[ws.obj['id']]:
                doc=db.get(row.id)
            if doc:
                final_doc=lutils.merge(ws.obj, doc)
            else:
                final_doc=ws.obj
            #clean possible name duplicates
            for row in db.view('worksets/name')[ws.obj['name']]:
                doc=db.get(row.id)
                if doc['id'] != ws.obj['id']:
                    proclog.warning("Duplicate name {} for worksets {} and {}".format(doc['name'], doc['id'], final_doc['id']))
                    db.delete(doc)
            db.save(final_doc)
            proclog.info("updating {0}".format(ws.obj['name']))
            queue.task_done()

class QueueHandler(logging.Handler):
    """
    This handler sends events to a queue. Typically, it would be used together
    with a multiprocessing Queue to centralise logging to file in one process
    (in a multi-process application), so as to avoid file write contention
    between processes.

    This code is new in Python 3.2, but this class can be copy pasted into
    user code for use with earlier Python versions.
    """

    def __init__(self, queue):
        """
        Initialise an instance, using the passed queue.
        """
        logging.Handler.__init__(self)
        self.queue = queue

    def enqueue(self, record):
        """
        Enqueue a record.

        The base implementation uses put_nowait. You may want to override
        this method if you want to use blocking, timeouts or custom queue
        implementations.
        """
        self.queue.put_nowait(record)

    def prepare(self, record):
        """
        Prepares a record for queuing. The object returned by this method is
        enqueued.

        The base implementation formats the record to merge the message
        and arguments, and removes unpickleable items from the record
        in-place.

        You might want to override this method if you want to convert
        the record to a dict or JSON string, or send a modified copy
        of the record while leaving the original intact.
        """
        # The format operation gets traceback text into record.exc_text
        # (if there's exception data), and also puts the message into
        # record.message. We can then use this to replace the original
        # msg + args, as these might be unpickleable. We also zap the
        # exc_info attribute, as it's no longer needed and, if not None,
        # will typically not be pickleable.
        self.format(record)
        record.msg = record.message
        record.args = None
        record.exc_info = None
        return record

    def emit(self, record):
        """
        Emit a record.

        Writes the LogRecord to the queue, preparing it for pickling first.
        """
        try:
            self.enqueue(self.prepare(record))
        except Exception:
            self.handleError(record)


# This is the script where we select some random products from our website and get their prices ( for a specific store of course )

import keys
import pymysql
import pymysql.cursors
import urllib2
import re
import random

# first we need to formualte the url

# We need to get the popular subcategories from our DB and for that we need to connect to our db

class Item:

	item_id = ""
	mspPrice = 0
	store = ""

	def __init__(self, item_id, mspPrice, store):
		self.item_id = item_id
		self.mspPrice = int(mspPrice)
		self.store = store

	def setUrl(self, url):
		self.url = url

	def getStorePrice(self, regex, logFile, exceptLogFile):

		try:
			data = urllib2.urlopen(self.url)
			dataString = data.read()
			priceSearchString = regex
			priceMatches = re.findall(priceSearchString,dataString)

		except ( urllib2.HTTPError, urllib2.URLError, urllib2.httplib.IncompleteRead ) as e:
			# print(e)
			exceptLogFile.write(self.url)
			exceptLogFile.write(str(e))
		try:
			retval = priceMatches[0]
		except:
			# print (self.url + " is messed up")
			logFile.write(self.url + " is messed up\n")
			# print(priceMatches)
			return 0

			# TODO We need to clean the price obtained. We can copy the iCrawler Regex here I guess.

			# Remove the comma
			# retVal = retVal.replace(",","")
		retVal = priceMatches[0].replace(",","")
		retVal = re.sub('/[^0-9.]+/','',retVal)
		retVal = retVal.split(".",1)
		retVal = retVal[0]
		retVal = int(retVal)
		# print(retVal)
		return retVal
			#return priceMatches[0]

class StoreQA:

	store = ""
	subcategories = []

	def __init__(self, store):
		self.store = store
		self.getPopSubCat()

	def getPopSubCat(self):
		store = self.store
		cacheFile = open(keys.docRoot + "popSubCatCache/" + store + '.cache','r')
		cache = cacheFile.readlines()
		for line in cache:
			lineArray = line.split(":::")
			temp = {}
			temp['category'] = lineArray[0]
			temp['subcategory'] = lineArray[1].rstrip("\n")
			self.subcategories.append(temp)
		return self.subcategories

	def getProducts(self, logFile, exceptLogFile):
		itemsList = []
		for subCat in self.subcategories:
			# print(subCat)
			url = "http://www.mysmartprice.com/" + subCat['category'] + "/" + subCat['subcategory'] + "?subcategory=" + subCat['subcategory'] + "&property=store:" + self.store
			# print(url)
			logFile.write(url + "\n")
			try:
				dump = urllib2.urlopen(url)
			except ( urllib2.HTTPError, urllib2.URLError, urllib2.httplib.IncompleteRead ) as e:
				# print(e)
				exceptLogFile.write(str(e))
				continue
			dumpString = dump.read()
			itemIdSearchString = "prdct-item prdct-item--nc\" data-mspid=\"([\d]*)\""
			priceSearchString = "
\n₹\n([\d]*)\n
"
			itemIdMatches = re.findall(itemIdSearchString,dumpString)
			priceMatches = re.findall(priceSearchString,dumpString)
			noOfItems = itemIdMatches.__len__()
			noOfPrices = priceMatches.__len__()

			if noOfItems == noOfPrices:
				for i in range(0,noOfItems//10):
					j = random.randint(0,noOfItems-1)
					temp = (itemIdMatches[j],priceMatches[j][1])
					itemsList.append(temp)
				'''for i in range(0,noOfItems):
					temp = (itemIdMatches[i],priceMatches[i][1])
					itemsList.append(temp)
					'''
			else:
				continue
			# exit()
			# return itemsList
		return itemsList

class StoreRegex:

	priceRegex = {}

	def __init__(self):
		fp = open(keys.docRoot + "storeRegex.txt",'r')
		data = fp.readlines()
		# print(data)
		noOfLines = data.__len__()
		for i in range(0,noOfLines):
			line = data[i]
			line = line.rstrip('\n')
			# print(line)
			array = line.split(":::")
			self.priceRegex[array[0]] = array[1]

	def getStoreRegex(self,store):
		return self.priceRegex[store]

'''
kap = StoreRegex()
print(kap.getStoreRegex("jabong"))

trial = Item("52835269",1495)
print(trial.getStorePrice(kap.getStoreRegex("jabong")))

test = StoreQA("jabong")
test.getPopSubCat()
test.getProducts()

new = StoreQA("flipkart")
new.getPopSubCat()
'''


from fastapi import FastAPI, Depends
from sqlalchemy.orm import Session
from src.schema.schemas import Serie
from src.infra.sqlalchemy.config.database import get_db, criar_bd
from src.infra.sqlalchemy.repositorios.serie import RepositorioSerie

criar_bd()

app = FastAPI()

@app.post('/series')
def cadastra_series(serie: Serie, db: Session = Depends(get_db)):
    serie_cadastrada = RepositorioSerie(db).cadastrar(serie)
    return serie_cadastrada

@app.get('/series')
def listar_series(db: Session = Depends(get_db)):
    serie = RepositorioSerie(db).listar()
    return serie

@app.get('/series/{serie_id}')
def obter_serie(serie_id: int, db: Session = Depends(get_db)):
    serie = RepositorioSerie(db).obter(serie_id)
    return serie

@app.put('/series/{titulo}')
def listar_titulo(titulo: str, db: Session = Depends(get_db)):
    series_titulo = RepositorioSerie(db).exibir_titulo(titulo)
    return series_titulo

@app.delete('/series/{serie_id}')
def obter_serie(serie_id: int, db: Session = Depends(get_db)):
    RepositorioSerie(db).remover(serie_id)
    return {"msg": "Removido com sucesso"}


# main function
def func(inp):
    p_c_i = 0
    print(inp[0], end="")

    for c_c_i in range(1, len(inp)):
        if(inp[c_c_i] != inp[p_c_i]):
            print(inp[c_c_i], end="")
            p_c_i = c_c_i
    print()

if __name__ == '__main__':
    test = int(input())
    # print(test)
    while(test > 0):
        a = input()
        func(a)
        test -= 1


import tkinter as tk

data = 0

root = tk.Tk()
root.title("Test Subject")
root.geometry("400x400")

msg = '''
1. The game is played on a grid that's 3 squares by 3 squares.

2. You are X, your friend is O. Players take turns putting their marks in empty squares.

3. The first player to get 3 of her marks in a row (up, down, across, or diagonally) is the winner.

4. When all 9 squares are full, the game is over. If no player has 3 marks in a row, the game ends in a tie.

5. Open the Game Status menu for a detailed and updated score board
'''

def click():
	#global pop
	global txt
	pop = tk.Toplevel(root)
	pop.resizable(0,0)
	txt = tk.Label(pop,text=msg)
	txt.pack()

def do():
	global data
	global txt
	data += 1
	txt.config(text=data)

bt = tk.Button(root,text="Click me",command=click)
bt.pack()

bt1 = tk.Button(root,text="F me",command=do)
bt1.pack()

#print(msg)

root.mainloop()


#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# File   : test.py
# Author : Bowen Pan
# Email  : panbowen0607@gmail.com
# Date   : 09/25/2018
#
# Distributed under terms of the MIT license.

"""

"""
from utils import Foo
from models import VPNModel
from datasets import OVMDataset
from opts import parser
from transform import *
import torchvision
import torch
from torch import nn
import os
import time
import cv2
import dominate
from dominate.tags import *

mean_rgb = [0.485, 0.456, 0.406]
std_rgb = [0.229, 0.224, 0.225]

def main():
    global args, web_path, best_prec1
    best_prec1 = 0
    args = parser.parse_args()
    network_config = Foo(
        encoder=args.encoder,
        decoder=args.decoder,
        fc_dim=args.fc_dim,
        num_views=args.n_views,
        num_class=94,
        transform_type=args.transform_type,
        output_size=args.label_resolution,
    )

    val_dataset = OVMDataset(args.data_root, args.eval_list,
                         transform=torchvision.transforms.Compose([
                             Stack(roll=True),
                             ToTorchFormatTensor(div=True),
                             GroupNormalize(mean_rgb, std_rgb)
                         ]),
                         n_views=network_config.num_views, input_size=args.input_resolution,
                         label_size=args.segSize, use_mask=args.use_mask, use_depth=args.use_depth, is_train=False)

    val_loader = torch.utils.data.DataLoader(
        val_dataset, batch_size=args.batch_size,
        num_workers=args.num_workers, shuffle=False,
        pin_memory=True
    )

    mapper = VPNModel(network_config)
    mapper = nn.DataParallel(mapper.cuda())

    if args.weights:
        if os.path.isfile(args.weights):
            print(("=> loading checkpoint '{}'".format(args.weights)))
            checkpoint = torch.load(args.weights)
            args.start_epoch = checkpoint['epoch']
            mapper.load_state_dict(checkpoint['state_dict'])
            print(("=> loaded checkpoint '{}' (epoch {})"
                  .format(args.evaluate, checkpoint['epoch'])))
        else:
            print(("=> no checkpoint found at '{}'".format(args.weights)))

    criterion = nn.NLLLoss(weight=None, size_average=True)
    eval(val_loader, mapper, criterion)

    web_path = os.path.join(args.visualize, args.store_name)
    if os.path.isdir(web_path):
        pass
    else:
        os.makedirs(web_path)

    with dominate.document(title=web_path) as web:
        for step in range(len(val_loader)):
            if step % args.print_freq == 0:
                h2('Step {}'.format(step*args.batch_size))
                with table(border = 1, style = 'table-layout: fixed;'):
                    with tr():
                        for i in range(args.test_views):
                            path = 'Step-{}-{}.png'.format(step * args.batch_size, i)
                            with td(style='word-wrap: break-word;', halign='center', valign='top'):
                                img(style='width:128px', src=path)
                        path = 'Step-{}-pred.png'.format(step * args.batch_size)
                        with td(style='word-wrap: break-word;', halign='center', valign='top'):
                            img(style='width:128px', src=path)
                        path = 'Step-{}-gt.png'.format(step * args.batch_size)
                        with td(style='word-wrap: break-word;', halign='center', valign='top'):
                            img(style='width:128px', src=path)

    with open(os.path.join(web_path, 'index.html'), 'w') as fp:
        fp.write(web.render())

def eval(val_loader, mapper, criterion):
    batch_time = AverageMeter()
    data_time = AverageMeter()
    losses = AverageMeter()
    top1 = AverageMeter()
    top5 = AverageMeter()

    mapper.eval()

    end = time.time()

    web_path = os.path.join(args.visualize, args.store_name)
    if os.path.isdir(web_path):
        pass
    else:
        os.makedirs(web_path)

    prec_stat = {}
    for i in range(args.num_class):
        prec_stat[str(i)] = {'intersec': 0, 'union': 0, 'all': 0}

    with open('./metadata/colormap_coarse.csv') as f:
        lines = f.readlines()
    cat = []
    for line in lines:
        line = line.rstrip()
        cat.append(line)
    cat = cat[1:]
    label_dic = {}
    for i, value in enumerate(cat):
        key = str(i)
        label_dic[key] = [int(x) for x in value.split(',')[1:]]

    for step, (rgb_stack, target, rgb_origin, OverMaskOrigin) in enumerate(val_loader):
        data_time.update(time.time() - end)
        with torch.no_grad():
            input_rgb_var = torch.autograd.variable(rgb_stack).cuda()
            _, output = mapper(x=input_rgb_var, return_feat=True)
        target_var = target.cuda()
        target_var = target_var.view(-1)
        upsample = output.view(-1, args.label_resolution, args.label_resolution, args.num_class).transpose(3,2).transpose(2,1).contiguous()
        upsample = nn.functional.upsample(upsample, size=args.segSize, mode='bilinear', align_corners=False)
        upsample = nn.functional.softmax(upsample, dim=1)
        output = torch.log(upsample.transpose(1,2).transpose(2,3).contiguous().view(-1, args.num_class))
        _, pred = upsample.data.topk(1, 1, True, True)
        pred = pred.squeeze(1)
        loss = criterion(output, target_var)
        losses.update(loss.data[0], input_rgb_var.size(0))
        prec_stat = count_mean_accuracy(output.data, target_var.data, prec_stat)
        prec1 = accuracy(output.data, target_var.data, topk=(1,))[0]

        batch_time.update(time.time() - end)
        end = time.time()

        if step % args.print_freq == 0:
            output = ('Test: [{0}/{1}]\t'
                    'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
                    'Data {data_time.val:.3f} ({data_time.avg:.3f})\t'
                    'Loss {loss.val:.4f} ({loss.avg:.4f})\t'
                    'Prec@1 {top1.val:.3f} ({top1.avg:.3f})'.format(
                step + 1, len(val_loader), batch_time=batch_time,
                data_time=data_time, loss=losses, top1=top1))
            print(output)

            pred = np.uint8(pred.cpu()[0])
            predMask = np.uint8(np.zeros((args.segSize, args.segSize, 3)))
            for i, _ in enumerate(pred):
                for j, _ in enumerate(pred[0]):
                    key = str(pred[i][j])
                    predMask[i,j] = label_dic[key]
            predMask = cv2.resize(predMask[:, :, ::-1], (256, 256), interpolation=cv2.INTER_NEAREST)
            cv2.imwrite(os.path.join(web_path, 'Step-{}-pred.png'.format(step * args.batch_size, i)), predMask)

            gtMask = OverMaskOrigin[0].cpu().numpy()
            gtMask = cv2.resize(gtMask, (256, 256), interpolation=cv2.INTER_NEAREST)
            cv2.imwrite(os.path.join(web_path, 'Step-{}-gt.png'.format(step * args.batch_size, i)), gtMask)

            rgb = rgb_origin.cpu().numpy()[0]
            for i in range(args.test_views):
                cv2.imwrite(os.path.join(web_path, 'Step-{}-{}.png'.format(step * args.batch_size, i)), cv2.resize(rgb[(i + args.view_bias) % 8], (256, 256), interpolation=cv2.INTER_NEAREST))

    sum_acc = 0
    counted_cat = 0
    sum_iou = 0
    for key in prec_stat:
        if int(prec_stat[key]['all']) != 0:
            acc = prec_stat[key]['intersec'] / (prec_stat[key]['all'] + 1e-10)
            iou = prec_stat[key]['intersec'] / (prec_stat[key]['union'] + 1e-10)
            sum_acc += acc
            sum_iou += iou
            counted_cat += 1
    mean_acc = sum_acc / counted_cat
    mean_iou = sum_iou / counted_cat
    output = ('Testing Results: Prec@1 {top1.avg:.3f} Mean Prec@1 {meantop:.3f} Mean IoU {meaniou:.3f} Loss {loss.avg:.5f}'
              .format(top1=top1, loss=losses, meantop=mean_acc, meaniou=mean_iou))
    print(output)
    output_best = '\nBest Prec@1 of: %.3f' % (best_prec1)
    print(output_best)

    return top1.avg

class AverageMeter(object):
    """Computes and stores the average and current value"""
    def __init__(self):
        self.reset()

    def reset(self):
        self.val = 0
        self.avg = 0
        self.sum = 0
        self.count = 0

    def update(self, val, n=1):
        self.val = val
        self.sum += val * n
        self.count += n
        self.avg = self.sum / self.count

def count_mean_accuracy(output, target, prec_stat):
    _, pred = output.topk(1, 1, True, True)
    pred = pred.squeeze(1)
    for key in prec_stat.keys():
        label = int(key)
        pred_map = np.uint8(pred.cpu().numpy() == label)
        target_map = np.uint8(target.cpu().numpy() == label)
        intersection_t = pred_map * (pred_map == target_map)
        union_t = pred_map + target_map - intersection_t
        prec_stat[key]['intersec'] += np.sum(intersection_t)
        prec_stat[key]['union'] += np.sum(union_t)
        prec_stat[key]['all'] += np.sum(target_map)
    return prec_stat

def accuracy(output, target, topk=(1,)):
    """Computes the precision@k for the specified values of k"""
    maxk = max(topk)
    batch_size = target.size(0)

    _, pred = output.topk(maxk, 1, True, True)
    pred = pred.t()
    correct = pred.eq(target.view(1, -1).expand_as(pred))

    res = []
    for k in topk:
        correct_k = correct[:k].view(-1).float().sum(0)
        res.append(correct_k.mul_(100.0 / batch_size))
    return res

if __name__=='__main__':
    main()


# -*- coding: UTF-8 -*-

import os.path, sys, time
etc = {}

etc['debug'] = True
etc['error'] = False
etc['servs'] = 'AL/1.0.%s' % int(time.time())

etc['root_path'] = sys.path[0]
etc['login_url'] = '/login'
etc['xsrf_cookies'] = True
etc['cookie_secret'] = 'cookie_secret'
etc['template_path'] = os.path.join(etc['root_path'], 'app', 'views', '')


import logging
log = logging.getLogger()
log.setLevel('INFO')
def expValue(exp):
    global index
    result = termValue(exp)
    if index >= len(exp):
        return result
    more = True
    while more:
        op = exp[index]
        if op in '+-':
            index += 1
            value = termValue(exp)
            if op == '+':
                result += value
                if index>=len(exp):
                    more = False
            else:
                result -= value
                if index>=len(exp):
                    more = False
        else:
            more = False
    return result
def factorValue(exp):
    global index
    result = 0
    c = exp[index]
    if c == '(':
        index += 1
        result = expValue(exp)
        index += 1
    else:
        while c.isdigit():
            result = 10 * result + int(c) - 0
            index += 1
            if index>=len(exp):
                break
            c = exp[index]
            if exp[index]=='-' or exp[index]=='+':
                return result
    return result
def termValue(exp):
    global index
    result = factorValue(exp)
    if index >= len(exp):
        return result
    while True:
        if index >= len(exp):
            break
        op = exp[index]
        if op in '*/':
            index += 1
            value = factorValue(exp)
            if op == '*':
                result *= value
            else:
                result /= value
        else:
            break
    return result
def GetExpValue(L,P,H,exp):
    global index
    index = 0
    if not isinstance(L,str):
        L = str(int(L))
    if not isinstance(P, str):
        P = str(int(P))
    if not isinstance(H, str):
        H = str(int(H))
    log.info('EXP1= '+str(exp))
    EXP = exp.replace('L',L).replace('P',P).replace('H',H)
    result = expValue(EXP)
    log.info('result= '+str(result))
    return result
def main():
    GetExpValue(200, 540, 2100, 'L-18--1-20')


import sys
from ..utils.helpers import VariablesGenerator
from ..utils.snippet import snippet
from ..utils.tree import get_node_position, find, insert_at
from .. import ast
from .base import BaseNodeTransformer
from typing import Callable, Dict, List, Optional, Set, Union

class _ScopeTransformer(ast.NodeTransformer):
    """
    Renames objects so they use scope dictionaries, this is called by
    _RawTransformer.transform() after running its own transformations.
    """

    def __init__(self, scope: Dict[str, str], tree: ast.AST) -> None:
        self.scope = scope
        self.tree = tree

    def visit_Name(self, node: ast.Name) -> Union[ast.Name, ast.Subscript]:
        if node.id not in self.scope:
            return node

        value = ast.Name(id=self.scope[node.id], ctx=ast.Load())

        # Python 2's unicode strings typically use either 2 or 4 bytes, use
        # 8-bit strings (bytes in Python 3) if possible.
        try:
            s = ast.Bytes(s=node.id.encode('ascii')) # type: ast.AST
        except UnicodeEncodeError:
            s = ast.Str(s=node.id)

        subscript = ast.Subscript(value=value, slice=ast.Index(value=s),
                                  ctx=getattr(node, 'ctx', None) or ast.Load())
        return subscript

    def _assign_later(self, node: ast.AST, name: str) -> str:
        pos = get_node_position(self.tree, node)
        temp_name = VariablesGenerator.generate('temp_' + name)
        assign = ast.Assign(targets=[ast.Name(id=name, ctx=ast.Store())],
                            value=ast.Name(id=temp_name, ctx=ast.Load()))
        pos.holder.insert(pos.index + 1, assign)
        delete = ast.Delete(targets=[ast.Name(id=temp_name,
                                              ctx=ast.Del())])
        pos.holder.insert(pos.index + 2, delete)

        return temp_name

    def visit_FunctionDef(self, node: Union[ast.FunctionDef, ast.ClassDef]) \
            -> Union[ast.FunctionDef, ast.ClassDef]:
        if node.name in self.scope:
            node.name = self._assign_later(node, node.name)

        # Don't call generic_visit() here.
        return node

    visit_ClassDef = visit_FunctionDef

    # Used in import and import from statements.
    def visit_alias(self, node: ast.alias) -> ast.alias:
        name = node.asname or node.name
        if name and name in self.scope:
            node.asname = self._assign_later(node, name)

        return self.generic_visit(node) # type: ignore

class _RawTransformer(ast.NodeTransformer):
    def __init__(self, node: Union[ast.FunctionDef, ast.ClassDef],
            callbacks: List[Callable], *, parent_name: Optional[str] = None,
            parent_scope: Optional[Dict[str, str]] = None) -> None:
        self.parent_name = parent_name
        self.name = None # type: Optional[str]
        self.node = node
        self.is_class = isinstance(node, ast.ClassDef) # type: bool
        self.tree_changed = False # type: bool
        self._callbacks = callbacks
        if parent_scope is None:
            parent_scope = {}
        self.parent_scope = parent_scope # type: Dict[str, str]
        self.scope = {} # type: Dict[str, str]

    @classmethod
    def transform(cls, node: Union[ast.FunctionDef, ast.ClassDef],
            parent: Union[ast.FunctionDef, ast.ClassDef],
            callbacks: List[Callable], *, parent_name: Optional[str] = None,
            parent_scope: Optional[Dict[str, str]] = None) -> '_RawTransformer':
        self = cls(node, callbacks, parent_name=parent_name,
                   parent_scope=parent_scope)
        for n in node.body:
            self.visit(n)

        if not self.tree_changed:
            return self

        scopetransformer = _ScopeTransformer(self.scope, node)
        for n in node.body:
            scopetransformer.visit(n)

        if not self.parent_name or isinstance(parent, ast.ClassDef):
            return self

        # Add the scope variable
        if not parent_name:
            name = ast.Name(id=self.parent_name, ctx=ast.Store())
            assign = ast.Assign(targets=[name],
                                value=ast.Dict(keys=[], values=[]))

            i = 0
            if parent.body and isinstance(parent.body[0], ast.Expr) and \
                    isinstance(parent.body[0].value, ast.Str):
                i = 1
            self._callbacks.append(lambda : parent.body.insert(i, assign))

        return self

    def visit_FunctionDef(self, node: Union[ast.FunctionDef, ast.ClassDef]) \
            -> Union[ast.FunctionDef, ast.ClassDef]:
        # Classes are different.
        if self.is_class:
            name, scope = self.parent_name, self.parent_scope
        else:
            name, scope = self.name, self.scope

        transformer = self.transform(node, self.node, self._callbacks,
                                     parent_name=name, parent_scope=scope)
        if transformer.tree_changed:
            self.tree_changed = True
            self.name = transformer.parent_name

        # Don't call generic_visit(), that would iterate over the nodes inside
        #   the function.
        return node

    visit_ClassDef = visit_FunctionDef

    def visit_Nonlocal(self, node: ast.Nonlocal) -> ast.Nonlocal:
        self.tree_changed = True
        if self.parent_name is None:
            self.parent_name = VariablesGenerator.generate('scope')

        for name in node.names:
            if name in self.parent_scope:
                scope = self.parent_scope[name]
            else:
                scope = self.parent_name
                self.parent_scope[name] = scope
            self.scope[name] = scope

        position = get_node_position(self.node, node)
        self._callbacks.append(lambda : position.holder.remove(node))

        return node

class NonlocalStatementTransformer(BaseNodeTransformer):
    """Compiles:
        def outer():
            x = 1
            def inner():
                nonlocal x
                x = 2
            inner()
            print(x)
    To
        def outer():
            _py_backwards_scope_0 = {}
            _py_backwards_scope_0['x'] = 1
            def inner():
                _py_backwards_scope_0['x'] = 2
            inner()
            print(_py_backwards_scope_0['x'])

    """
    target = (2, 7)

    def visit_FunctionDef(self, node: ast.FunctionDef) -> ast.FunctionDef:
        callbacks = [] # type: List[Callable]
        transformer = _RawTransformer.transform(node, node, callbacks)
        if transformer.tree_changed:
            self._tree_changed = True
        for callback in reversed(callbacks):
            callback()
        return node  # type: ignore

    def visit_Nonlocal(self, node: ast.Nonlocal):
        exc = SyntaxError('nonlocal outside function')
        exc.ast_node = node # type: ignore
        raise exc


import torch
from collections import *

def modelTransform(modeDir):
    """
    将image上的snet模型转化为用于thunderNet微调的backbone
    """
    snet=torch.load(modeDir)
    backbone=OrderedDict()
    for key,value in snet["state_dict"].items():
        newkey=key.replace("shuffleNet.","")
        backbone[newkey]=value
    torch.save({"state_dict":backbone},"snet146.pth.tar")

if __name__=="__main__":
    modeDir="../SNet146_12.pth.tar"
    modelTransform(modeDir)


# coding: utf8
import pygame
import network
import re
import time
import barres
from threading import Thread

BLACK    = (   0,   0,   0)
WHITE    = ( 255, 255, 255)

def main():

######################FONCTIONS###################################
    def move(char):
        if char == 0 : _char = bar0
        if char == 1 : _char = bar1

        if pygame.key.get_pressed()[pygame.K_UP] : _char.speed = -5
        elif pygame.key.get_pressed()[pygame.K_DOWN] : _char.speed = 5
        else : _char.speed = 0

    def commandes():
        _started = False
        while True :
            comm = input(">>")

            if comm == "help" or comm == "h" :
                print("Aide de la console de PongOnline !!!!!! \n\
                Pour devenir hôte d'un serveur de jeu : hosting,\n\
                Pour se connecter à un server distant : connect")

            if comm == "hosting" :
                print("Préparation du serveur...")
                time.sleep(1)

                while not _started :
                    try :

                        ip = re.split(':',input("Entrez votre ip (par défaut localhost:6660) : "))
                        if ip[0] == '':
                            host = "localhost"
                        else :
                            host = ip[0]
                        if len(ip) == 2 :
                            port = int(ip[1])
                        elif len(ip) > 2 :
                            int('a')
                        else :
                            port = 6660

                        print("Mise en place du serveur : "+host+":"+str(port))
                        servThread = Thread(target=network.serv, args=(host, port, char, bar0, bar1, balle))
                        servThread.setDaemon(True)
                        servThread.start()
                        _started   = True

                    except :
                        print("Veuillez entrer une IP valide")

                print("Attente de connexions.")
                break

            if comm == "connect" :
                while not _started :
                    try :
                        ip   = re.split(':',input("Entrez l'ip du serveur (port par défaut 6660) : "))
                        host = ip[0]
                        if len(ip) == 2 :
                            port = int(ip[1])
                        elif len(ip) > 2 :
                            int('a')
                        else :
                            port = 6660

                        print("Tentative de connexion à "+host+":"+str(port))
                        clientThread = Thread(target=network.client, args=(host, port, char, bar0, bar1, balle))
                        clientThread.setDaemon(True)
                        clientThread.start()
                        _started   = True

                    except :
                        print("Veuillez entrer une IP valide")
######################FONCTIONS###################################

    pygame.init()

    size   = [1000, 500]
    screen = pygame.display.set_mode(size)

    pygame.display.set_caption("Pong Online")

    done   = False

    clock  = pygame.time.Clock()

    balle      = barres.Balle()
    balleGroup = pygame.sprite.Group()
    balleGroup.add(balle)

    bar0       = barres.Barre(0,balle)
    bar1       = barres.Barre(1,balle)
    barreGroup = pygame.sprite.Group()
    barreGroup.add(bar0)
    barreGroup.add(bar1)

    font       = pygame.font.Font(None, 36)

    while not done :
        for event in pygame.event.get(): # User did something
            if event.type == pygame.QUIT: # If user clicked close
                done = True # Flag that we are done so we exit this loop
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    char = 0
                    done = True
                if event.key == pygame.K_RIGHT:
                    char = 1
                    done = True

        text     = font.render("Gauche ou Droite", True, WHITE)
        text_r   = text.get_rect()
        text_pos = [500-(text_r.width/2),250-(text_r.height/2)]

        screen.fill(BLACK)

        screen.blit(text, text_pos)

        pygame.display.flip()

        clock.tick(60)

    t          = Thread(target = commandes)
    t.setDaemon(True)
    t.start()

    game_type = "ndef"
    game_started = False
    done = False

    while not done:

        for event in pygame.event.get(): # User did something
            if event.type == pygame.QUIT: # If user clicked close
                done = True # Flag that we are done so we exit this loop
        if pygame.key.get_pressed()[pygame.K_SPACE] and not game_started :
            balle.engageo()
            game_started = True

        move(char)
        barreGroup.update()
        if game_started :
            balleGroup.update()

        if balle.rect.left <= 0 : balle.engageo(bar1)
        if balle.rect.right >= 1000 : balle.engageo(bar0)

        score0     = font.render(str(int(bar0.points)), True, WHITE)
        score0_r   = score0.get_rect()
        score0_pos = [200-score0_r.width,40-score0_r.height]
        score1     = font.render(str(int(bar1.points)), True, WHITE)
        score1_r   = score1.get_rect()
        score1_pos = [800-score1_r.width,40-score1_r.height]

        screen.fill(BLACK)
        barreGroup.draw(screen)
        balleGroup.draw(screen)
        screen.blit(score0, score0_pos)
        screen.blit(score1, score1_pos)

        pygame.display.flip()

        clock.tick(60)

    pygame.quit()

if __name__ == "__main__":
    main()


# Создайте собственный класс-исключение, обрабатывающий ситуацию деления на нуль. Проверьте его работу на данных,
# вводимых пользователем. При вводе пользователем нуля в качестве делителя программа должна корректно обработать
# эту ситуацию и не завершиться с ошибкой.

class OwnZeroDivision(ZeroDivisionError):
    def __init__(self, text):
        self._text = text

try:
    input_devisor = int(input('Введите делимое: '))
    input_devided = int(input('Введите делитель: '))
    if input_devided == 0:
        raise OwnZeroDivision('Деление на 0 невозможно')
except ValueError:
    print('Вы ввели не число')
except OwnZeroDivision as err:
    print(err)
else:
    print(f'Данные введены верно, результат деленя: {input_devisor / input_devided}')


def adaptationModel(stimulus, name, options):
	Gmax  = float(options[0]);
	Gmin  = float(options[1]);
	Spl   = float(options[2]);
	Sp    = float(options[3]);
	Sm    = float(options[4]);
	Sml   = float(options[5]);
	flag1 = options[6];
	flag2 = options[7];

	stLen = stimulus.__len__();
	res = zeros(stLen, FloatType);

	if name == 'trilinear':
		if flag1 == 0:
			arrayLogic = (Sp < stimulus);
		else:
			res[stimulus >= Spl]=Gmax;
			arrayLogic = (stimulus > Sp)&(stimulus < Spl);

		res[arrayLogic] = (Gmax/(Spl - Sp))*(stimulus[arrayLogic]-Sp);

		res[(stimulus >= Sm)&(stimulus <= Sp)] = 0;

		if flag2 == 0:
			arrayLogic = (stimulus < Sm);
		else:
			res[stimulus <= Sml]=Gmin;
			arrayLogic = (stimulus > Sml)&(stimulus < Sm);

		res[arrayLogic] = (Gmin/(Sml - Sm))*(stimulus[arrayLogic]-Sm);

	elif name ==  'japan':

		res[stimulus >= Spl]=Gmax;

		arrayLogic = (stimulus > Sp)&(stimulus < Spl);
		res[arrayLogic] = (Gmax/2)*(np.sin(pi*((stimulus[arrayLogic]-Sp)/(Spl - Sp)-0.5))+1);

		res[(stimulus >= Sm)&(stimulus <= Sp)] = 0;

		res[stimulus <= Sml]=Gmin;

		arrayLogic = (stimulus > Sml)&(stimulus < Sm);
		res[arrayLogic] = (Gmin/2)*(np.sin(pi*((stimulus[arrayLogic]-Sm)/(Sml - Sm)-0.5))+1);

	return res

if curveAtInstant:
	adaptationValues = smoothedMagnitude;
else:
	adaptationValues = adaptationModel(smoothedMagnitude, growthModelName, adaptationCurve);

# multiplies adaptation magnitude by node normals (smoothed)
adaptationMat = array([smoothedVectorMat[multI,]*adaptationValues[multI] for multI in range(adaptationValues.__len__())]);

# multiplies by rotation matrix of orientation of assembly
adaptationMat = dot(rotationOnVector(deg2rad(angleRot), rotationVector), adaptationMat.T).T;

# Ideia: meter if smoothing == 1: valor na matrix smoothedMat = ao valor dos resultados! (mesmo que raio = 0)

pointer1 = 0;
initialPartNodes = initialPart.nodes;
for node in initialRA.sets['GR'].nodes:
	label = node.label;
	initialPart.editNode(nodes=[initialPartNodes.getFromLabel(label=label)],
				offset1=adaptationMat[pointer1,0],
				offset2=adaptationMat[pointer1,1],
				offset3=adaptationMat[pointer1,2]);
	pointer1 += 1;

# Realigns surface region assembly
if os.path.isfile(inputName+'.rot'):
	file = open(inputName+'.rot', 'r');
	fileLines = file.readlines();
	prevAngle = float(fileLines[0]);
	prevRotVec =  eval(fileLines[1]);
	file.close();

	initialRA.rotate((initialRA.instances.keys()[0], ), axisPoint=(0.0, 0.0, 0.0), axisDirection=prevRotVec, angle=-prevAngle);
	os.remove(inputName+'.rot');


import unittest

import boto3
import mock

from alerts.send_new_relic_messages_to_sqs import (
    cache_known_violations,
    read_known_violations,
    send_violations
)

class TestSendNewRelicMessagesToSQS(unittest.TestCase):

    def test_cache_known_violations(self):
        with mock.patch('__builtin__.open', create=True) as mock_open:
            mock_open.return_value = mock.MagicMock(spec=file)
            cache_known_violations('/some/file.json', [12345, '23456'])

        file_handle = mock_open.return_value.__enter__.return_value
        file_handle.writelines.assert_called_with(['12345\n', '23456\n'])

    def test_read_known_violations_existing(self):
        with mock.patch('__builtin__.open', create=True) as mock_open:
            mock_open.return_value = mock.MagicMock(spec=file)
            file_handle = mock_open.return_value.__enter__.return_value
            file_handle.readlines.return_value = ['12345\n', '23456\n']
            known_violations = read_known_violations('/some/file.json')

        self.assertEqual([12345, 23456], known_violations)

    def test_read_known_violations_nonexisting(self):
        with mock.patch('__builtin__.open', create=True) as mock_open:
            mock_open.side_effect = IOError()
            known_violations = read_known_violations('/some/file.json')
        self.assertEqual([], known_violations)

    def test_send_violations(self):
        mock_boto3_client = mock.MagicMock(boto3.session.Session.client)()
        mock_boto3_client.send_message.return_value = {
            'ResponseMetadata': {'HTTPStatusCode': 200}
        }
        send_violations(mock_boto3_client,
                        'http://queue',
                        ['test violation message'])
        mock_boto3_client.send_message.assert_called_once_with(
            MessageBody='test violation message',
            QueueUrl='http://queue')

    def test_send_violations_not200(self):
        mock_boto3_client = mock.MagicMock(boto3.session.Session.client)()
        mock_boto3_client.send_message.return_value = {
            'ResponseMetadata': {'HTTPStatusCode': 400}
        }
        with self.assertRaises(SystemExit):
            send_violations(mock_boto3_client,
                            'http://queue',
                            ['test violation message'])

    def test_send_violations_dryrun(self):
        mock_boto3_client = mock.MagicMock(boto3.session.Session.client)()
        send_violations(mock_boto3_client,
                        'http://queue',
                        ['test violation message'],
                        dryrun=True)
        mock_boto3_client.send_message.assert_not_called()


#   Coyright 2017-2019 Nativepython Authors
#
#   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 typed_python.python_ast as python_ast

import nativepython
import nativepython.native_ast as native_ast
from nativepython.expression_conversion_context import ExpressionConversionContext
from nativepython.type_wrappers.none_wrapper import NoneWrapper
from nativepython.typed_expression import TypedExpression
from nativepython.conversion_exception import ConversionException
from typed_python import OneOf

NoneExprType = NoneWrapper()

typeWrapper = lambda t: nativepython.python_object_representation.typedPythonTypeToTypeWrapper(t)

class FunctionOutput:
    pass

class FunctionConversionContext(object):
    """Helper function for converting a single python function."""

    def __init__(self, converter, identity, ast_arg, statements, input_types, output_type, free_variable_lookup):
        self.converter = converter
        self.identity = identity
        self._ast_arg = ast_arg
        self._argnames = None
        self._statements = statements
        self._input_types = input_types
        self._output_type = output_type
        self._varname_to_type = {}
        self._free_variable_lookup = free_variable_lookup
        self._temp_let_var = 0
        self._temp_stack_var = 0
        self._temp_iter_var = 0
        self._typesAreUnstable = False
        self._functionOutputTypeKnown = False
        self._star_args_name = None
        self._native_args = None

        self._constructInitialVarnameToType()

    def convertToNativeFunction(self):
        body_native_expr, controlFlowReturns = self.convert_function_body(self._statements)
        assert not controlFlowReturns

        if self._star_args_name is not None:
            body_native_expr = self.construct_starargs_around(body_native_expr, self._star_args_name)

        body_native_expr = self.construct_stackslots_around(
            body_native_expr, self._argnames, self._star_args_name
        )
        return_type = self._varname_to_type.get(FunctionOutput, None)

        if return_type is None:
            return (
                native_ast.Function(
                    args=self._native_args,
                    body=native_ast.FunctionBody.Internal(body=body_native_expr),
                    output_type=native_ast.Void
                ),
                return_type
            )

        if return_type.is_pass_by_ref:
            return (
                native_ast.Function(
                    args=(
                        (('.return', return_type.getNativeLayoutType().pointer()),)
                        + tuple(self._native_args)
                    ),
                    body=native_ast.FunctionBody.Internal(body=body_native_expr),
                    output_type=native_ast.Void
                ),
                return_type
            )
        else:
            return (
                native_ast.Function(
                    args=self._native_args,
                    body=native_ast.FunctionBody.Internal(body=body_native_expr),
                    output_type=return_type.getNativeLayoutType()
                ),
                return_type
            )

    def _constructInitialVarnameToType(self):
        input_types = self._input_types

        if self._ast_arg.vararg is not None:
            self._star_args_name = self._ast_arg.vararg.val.arg

        if self._star_args_name is None:
            if len(input_types) != len(self._ast_arg.args):
                raise ConversionException(
                    "Expected %s arguments but got %s" % (len(self._ast_arg.args), len(input_types))
                )
        else:
            if len(input_types) < len(self._ast_arg.args):
                raise ConversionException(
                    "Expected at least %s arguments but got %s" %
                    (len(self._ast_arg.args), len(input_types))
                )

        self._native_args = []
        for i in range(len(self._ast_arg.args)):
            self._varname_to_type[self._ast_arg.args[i].arg] = input_types[i]
            if not input_types[i].is_empty:
                self._native_args.append((self._ast_arg.args[i].arg, input_types[i].getNativePassingType()))

        self._argnames = [a.arg for a in self._ast_arg.args]

        if self._star_args_name is not None:
            star_args_count = len(input_types) - len(self._ast_arg.args)

            for i in range(len(self._ast_arg.args), len(input_types)):
                self._native_args.append(
                    ('.star_args.%s' % (i - len(self._ast_arg.args)),
                        input_types[i].getNativePassingType())
                )

            starargs_type = native_ast.Struct(
                [('f_%s' % i, input_types[i+len(self._ast_arg.args)])
                    for i in range(star_args_count)]
            )

            self._varname_to_type[self._star_args_name] = starargs_type

        if self._output_type is not None:
            self._varname_to_type[FunctionOutput] = typeWrapper(self._output_type)

        self._functionOutputTypeKnown = FunctionOutput in self._varname_to_type

    def typesAreUnstable(self):
        return self._typesAreUnstable

    def resetTypeInstabilityFlag(self):
        self._typesAreUnstable = False

    def markTypesAreUnstable(self):
        self._typesAreUnstable = True

    def let_varname(self):
        self._temp_let_var += 1
        return "letvar.%s" % (self._temp_let_var-1)

    def stack_varname(self):
        self._temp_stack_var += 1
        return "stackvar.%s" % (self._temp_stack_var-1)

    def upsizeVariableType(self, varname, new_type):
        if self._varname_to_type.get(varname) is None:
            if new_type is None:
                return

            self._varname_to_type[varname] = new_type
            self.markTypesAreUnstable()
            return

        existingType = self._varname_to_type[varname].typeRepresentation

        if existingType == new_type.typeRepresentation:
            return

        if hasattr(existingType, '__typed_python_category__') and \
                existingType.__typed_python_category__ == 'OneOf':
            if new_type.typeRepresentation in existingType.Types:
                return

        final_type = OneOf(new_type.typeRepresentation, existingType)

        self.markTypesAreUnstable()

        self._varname_to_type[varname] = typeWrapper(final_type)

    def generateAssignmentExpr(self, varname, val_to_store):
        """Ensure we have appropriate storage allocated for 'varname', and assign 'val_to_store' to it."""
        subcontext = val_to_store.context

        self.upsizeVariableType(varname, val_to_store.expr_type)
        slot_ref = subcontext.named_var_expr(varname)

        # convert the value to the target type now that we've upsized it
        val_to_store = val_to_store.convert_to_type(slot_ref.expr_type)

        assert val_to_store is not None, "We should always be able to upsize"

        if slot_ref.expr_type.is_pod:
            slot_ref.convert_copy_initialize(val_to_store)
            subcontext.pushEffect(subcontext.isInitializedVarExpr(varname).expr.store(native_ast.trueExpr))
        else:
            with subcontext.ifelse(subcontext.isInitializedVarExpr(varname)) as (true_block, false_block):
                with true_block:
                    slot_ref.convert_assign(val_to_store)
                with false_block:
                    slot_ref.convert_copy_initialize(val_to_store)
                    subcontext.pushEffect(
                        subcontext.isInitializedVarExpr(varname).expr.store(native_ast.trueExpr)
                    )

    def convert_statement_ast(self, ast):
        if ast.matches.Assign or ast.matches.AugAssign:
            if ast.matches.Assign:
                assert len(ast.targets) == 1
                op = None
                target = ast.targets[0]
            else:
                target = ast.target
                op = ast.op

            if target.matches.Name and target.ctx.matches.Store:
                varname = target.id

                if varname not in self._varname_to_type:
                    self._varname_to_type[varname] = None

                subcontext = ExpressionConversionContext(self)

                val_to_store = subcontext.convert_expression_ast(ast.value)

                if val_to_store is None:
                    return subcontext.finalize(None), False

                if op is not None:
                    if varname not in self._varname_to_type:
                        raise NotImplementedError()
                    else:
                        slot_ref = subcontext.named_var_expr(varname)
                        val_to_store = slot_ref.convert_bin_op(op, val_to_store)

                        if val_to_store is None:
                            return subcontext.finalize(None), False

                self.generateAssignmentExpr(varname, val_to_store)

                return subcontext.finalize(None).with_comment("Assign %s" % (varname)), True

            if target.matches.Subscript and target.ctx.matches.Store:
                assert target.slice.matches.Index

                subcontext = ExpressionConversionContext(self)

                slicing = subcontext.convert_expression_ast(target.value)
                if slicing is None:
                    return subcontext.finalize(None), False

                index = subcontext.convert_expression_ast(target.slice.value)

                if slicing is None:
                    return subcontext.finalize(None), False

                val_to_store = subcontext.convert_expression_ast(ast.value)

                if val_to_store is None:
                    return subcontext.finalize(None), False

                if op is not None:
                    val_to_store = slicing.convert_getitem(index).convert_bin_op(op, val_to_store)
                    if val_to_store is None:
                        return subcontext.finalize(None), False

                slicing.convert_setitem(index, val_to_store)

                return subcontext.finalize(None), True

            if target.matches.Attribute and target.ctx.matches.Store:
                subcontext = ExpressionConversionContext(self)

                slicing = subcontext.convert_expression_ast(target.value)
                attr = target.attr

                val_to_store = subcontext.convert_expression_ast(ast.value)
                if val_to_store is None:
                    return subcontext.finalize(None), False

                if op is not None:
                    input_val = slicing.convert_attribute(attr)
                    if input_val is None:
                        return subcontext.finalize(None), False

                    val_to_store = input_val.convert_bin_op(op, val_to_store)
                    if val_to_store is None:
                        return subcontext.finalize(None), False

                slicing.convert_set_attribute(attr, val_to_store)

                return subcontext.finalize(None), True

        if ast.matches.Return:
            subcontext = ExpressionConversionContext(self)

            if ast.value is None:
                e = subcontext.convert_expression_ast(
                    python_ast.Expr.Num(n=python_ast.NumericConstant.None_())
                )
            else:
                e = subcontext.convert_expression_ast(ast.value)

            if e is None:
                return subcontext.finalize(None), False

            if not self._functionOutputTypeKnown:
                if self._varname_to_type.get(FunctionOutput) is None:
                    self.markTypesAreUnstable()
                    self._varname_to_type[FunctionOutput] = e.expr_type
                else:
                    self.upsizeVariableType(FunctionOutput, e.expr_type)

            if e.expr_type != self._varname_to_type[FunctionOutput]:
                e = e.convert_to_type(self._varname_to_type[FunctionOutput])

            if e is None:
                return subcontext.finalize(None), False

            if e.expr_type.is_pass_by_ref:
                returnTarget = TypedExpression(
                    subcontext,
                    native_ast.Expression.Variable(name=".return"),
                    self._varname_to_type[FunctionOutput],
                    True
                )

                returnTarget.convert_copy_initialize(e)

                subcontext.pushTerminal(
                    native_ast.Expression.Return(arg=None)
                )
            else:
                subcontext.pushTerminal(
                    native_ast.Expression.Return(arg=e.nonref_expr)
                )

            return subcontext.finalize(None), False

        if ast.matches.Expr:
            subcontext = ExpressionConversionContext(self)

            result_expr = subcontext.convert_expression_ast(ast.value)

            return subcontext.finalize(result_expr), result_expr is not None

        if ast.matches.If:
            cond_context = ExpressionConversionContext(self)
            cond = cond_context.convert_expression_ast(ast.test)
            if cond is None:
                return cond_context.finalize(None), False
            cond = cond.toBool()
            if cond is None:
                return cond_context.finalize(None), False

            if cond.expr.matches.Constant:
                truth_val = cond.expr.val.truth_value()

                branch, flow_returns = self.convert_statement_list_ast(ast.body if truth_val else ast.orelse)

                return cond.expr + branch, flow_returns

            true, true_returns = self.convert_statement_list_ast(ast.body)
            false, false_returns = self.convert_statement_list_ast(ast.orelse)

            return (
                native_ast.Expression.Branch(
                    cond=cond_context.finalize(cond.nonref_expr), true=true, false=false
                ),
                true_returns or false_returns
            )

        if ast.matches.Pass:
            return native_ast.nullExpr, True

        if ast.matches.While:
            cond_context = ExpressionConversionContext(self)

            cond = cond_context.convert_expression_ast(ast.test)

            if cond is None:
                return cond_context.finalize(None), False
            cond = cond.toBool()
            if cond is None:
                return cond_context.finalize(None), False

            true, true_returns = self.convert_statement_list_ast(ast.body)

            false, false_returns = self.convert_statement_list_ast(ast.orelse)

            return (
                native_ast.Expression.While(
                    cond=cond_context.finalize(cond.nonref_expr), while_true=true, orelse=false
                ),
                true_returns or false_returns
            )

        if ast.matches.Try:
            raise NotImplementedError()

        if ast.matches.For:
            if not ast.target.matches.Name:
                raise NotImplementedError("Can't handle multi-variable loop expressions")

            target_var_name = ast.target.id

            # create a variable to hold the iterator, and instantiate it there
            iter_varname = target_var_name + ".iter." + str(ast.line_number)

            iterator_setup_context = ExpressionConversionContext(self)
            to_iterate = iterator_setup_context.convert_expression_ast(ast.iter)
            if to_iterate is None:
                return iterator_setup_context.finalize(to_iterate), False
            iterator_object = to_iterate.convert_method_call("__iter__", (), {})
            if iterator_object is None:
                return iterator_setup_context.finalize(iterator_object), False
            self.generateAssignmentExpr(iter_varname, iterator_object)

            cond_context = ExpressionConversionContext(self)
            iter_obj = cond_context.named_var_expr(iter_varname)
            next_ptr, is_populated = iter_obj.convert_next()  # this conversion is special - it returns two values

            with cond_context.ifelse(is_populated.nonref_expr) as (if_true, if_false):
                with if_true:
                    self.generateAssignmentExpr(target_var_name, next_ptr)

            true, true_returns = self.convert_statement_list_ast(ast.body)

            false, false_returns = self.convert_statement_list_ast(ast.orelse)

            return (
                iterator_setup_context.finalize(None) >>
                native_ast.Expression.While(
                    cond=cond_context.finalize(is_populated), while_true=true, orelse=false
                ),
                true_returns or false_returns
            )

        if ast.matches.Raise:
            raise NotImplementedError()

        raise ConversionException("Can't handle python ast Statement.%s" % ast._which)

    def convert_function_body(self, statements):
        return self.convert_statement_list_ast(statements, toplevel=True)

    def convert_statement_list_ast(self, statements, toplevel=False):
        exprAndReturns = []
        for s in statements:
            expr, controlFlowReturns = self.convert_statement_ast(s)
            exprAndReturns.append((expr, controlFlowReturns))

            if not controlFlowReturns:
                break

        if not exprAndReturns or exprAndReturns[-1][1]:
            flows_off_end = True
        else:
            flows_off_end = False

        if toplevel and flows_off_end:
            flows_off_end = False
            if not self._functionOutputTypeKnown:
                if self._varname_to_type.get(FunctionOutput) is None:
                    self._varname_to_type[FunctionOutput] = NoneWrapper()
                    self.markTypesAreUnstable()
                else:
                    self.upsizeVariableType(FunctionOutput, NoneWrapper())

            exprAndReturns.append(
                self.convert_statement_ast(
                    python_ast.Statement.Return(
                        value=None, filename="", line_number=0, col_offset=0
                    )
                )
            )

        seq_expr = native_ast.Expression.Sequence(
            vals=[expr for expr, _ in exprAndReturns]
        )

        return seq_expr, flows_off_end

    def construct_stackslots_around(self, expr, argnames, stararg_name):
        to_add = []
        destructors = []

        for name in argnames:
            if name is not FunctionOutput and name != stararg_name:
                if name not in self._varname_to_type:
                    raise ConversionException("Couldn't find a type for argument %s" % name)
                slot_type = self._varname_to_type[name]

                if slot_type.is_empty:
                    # we don't need to generate a stackslot for this value. Whenever we look it up
                    # we'll simply make a void expression
                    pass
                elif slot_type is not None:
                    context = ExpressionConversionContext(self)

                    if slot_type.is_pod:
                        # we can just copy this into the stackslot directly. no destructor needed
                        context.pushEffect(
                            native_ast.Expression.Store(
                                ptr=native_ast.Expression.StackSlot(
                                    name=name,
                                    type=slot_type.getNativeLayoutType()
                                ),
                                val=(
                                    native_ast.Expression.Variable(name=name) if not slot_type.is_pass_by_ref else
                                    native_ast.Expression.Variable(name=name).load()
                                )
                            )
                        )
                        context.pushEffect(
                            context.isInitializedVarExpr(name).expr.store(native_ast.trueExpr)
                        )
                    else:
                        # need to make a stackslot for this variable
                        # the argument will be a pointer because it's POD
                        var_expr = context.inputArg(slot_type, name)

                        slot_expr = context.named_var_expr(name)

                        slot_type.convert_copy_initialize(context, slot_expr, var_expr)

                        context.pushEffect(
                            context.isInitializedVarExpr(name).expr.store(native_ast.trueExpr)
                        )

                    to_add.append(context.finalize(None))

        for name in self._varname_to_type:
            if name is not FunctionOutput and name != stararg_name:
                context = ExpressionConversionContext(self)

                if self._varname_to_type[name] is not None:
                    slot_expr = context.named_var_expr(name)

                    with context.ifelse(context.isInitializedVarExpr(name)) as (true, false):
                        with true:
                            slot_expr.convert_destroy()

                    destructors.append(
                        native_ast.Teardown.Always(
                            expr=context.finalize(None).with_comment("Cleanup for variable %s" % name)
                        )
                    )

                    if name not in argnames:
                        # this is a variable in the function that we assigned to. we need to ensure that
                        # the initializer flag is zero
                        context = ExpressionConversionContext(self)
                        context.pushEffect(context.isInitializedVarExpr(name).expr.store(native_ast.falseExpr))
                        to_add.append(context.finalize(None))

        if to_add:
            expr = native_ast.Expression.Sequence(
                vals=to_add + [expr]
            )

        if destructors:
            expr = native_ast.Expression.Finally(
                teardowns=destructors,
                expr=expr
            )

        return expr

    def construct_starargs_around(self, res, star_args_name):
        args_type = self._varname_to_type[star_args_name]

        stararg_slot = self.named_var_expr(star_args_name)

        return (
            args_type.convert_initialize(
                self,
                stararg_slot,
                [
                    TypedExpression(
                        native_ast.Expression.Variable(".star_args.%s" % i),
                        args_type.element_types[i][1]
                    )
                    for i in range(len(args_type.element_types))]
            ).with_comment("initialize *args slot") + res
        )


from django.test import TestCase

print('hello'[0:150])

from bs4 import BeautifulSoup
s1 = '
hello
123'
# html.parser为解析器
soup = BeautifulSoup(s1,'html.parser')
print(soup.text)

# xss攻击验证：验证是否含有非法标签script
s2 = '
hello
123'
soup = BeautifulSoup(s2,'html.parser')
print(soup.find_all())

for tag in soup.find_all():
    print(tag.name)
    # 过滤
    if tag.name == 'script':
        tag.decompose()    # 删除该标签

print(str(soup))


import sys

filename = sys.argv[1]

with open(filename, 'rb') as f, open('output', 'w') as out:
    char = f.read(1)
    while char != '':
        asci = ord(char)
        asci = str(asci)
        out.write('&#' + asci)
        char = f.read(1)


#!/usr/local/bin/python3

import fileinput

# N up to 10^6

for i, n in enumerate(list(fileinput.input())[1:]):
    n = int(n)
    if n == 0:
        result = 'INSOMNIA'
    else:
        letters = set()
        x = 0
        while True:
            x += n
            letters = letters.union(str(x))
            if len(letters) == 10:
                break
        result = x
    print('Case #{}: {}'.format(i + 1, result))


import pygame
from pygame.locals import *
import sys
import time

WINDOW_WIDTH = 400
WINDOW_HEIGHT = 720

DEFAULT_FPS = 60
DEFAULT_DELAY = 1.0/DEFAULT_FPS # 0.016666666666666666

if __name__ == '__main__':
    pygame.init()
    window = pygame.display.set_mode((WINDOW_WIDTH,WINDOW_HEIGHT))

    font = pygame.font.Font('font/happy.ttf',44)
    text = font.render('de', True, (255, 0, 0))
    window.blit(text, (10, 10))

    # 玩家车辆
    car = pygame.image.load('img/car.png')
    x = 300
    y = 400

    # 画三条竖线
    points1 = [(10, 0), (10, WINDOW_HEIGHT)]
    points2 = [(WINDOW_WIDTH/2, 0), (WINDOW_WIDTH/2, WINDOW_HEIGHT)]
    points3 = [(WINDOW_WIDTH-10, 0), (WINDOW_WIDTH-10, WINDOW_HEIGHT)]

    while True:
        # 填充背景
        window.fill((0,0,0))
        # 渲染竖线
        pygame.draw.lines(window, (0, 255, 0), 0, points1, 1)
        pygame.draw.lines(window, (0, 255, 0), 0, points2, 1)
        pygame.draw.lines(window, (0, 255, 0), 0, points3, 1)

        # 显示车辆
        window.blit(car, (x, y))
        # 渲染
        pygame.display.flip()
        events = pygame.event.get()
        for e in events:
            if e.type == QUIT:
                pygame.quit()
                sys.exit(0)
            if e.type == KEYDOWN:
                if e.key == K_LEFT:
                    print('left')
                    x -= 100
                if e.key == K_RIGHT:
                    print('right')
                    x += 100


#!/usr/bin/env python
__author__ = 'Sergei F. Kliver'
import os
import argparse
from RouToolPa.Parsers.LAST import CollectionLast
from RouToolPa.Collections.General import IdList, SynDict
from RouToolPa.Routines import DrawingRoutines

parser = argparse.ArgumentParser()

parser.add_argument("-i", "--input_last_tab", action="store", dest="input_last_tab", required=True,
                    help="File with LAST output in tab format")
parser.add_argument("-o", "--output_prefix", action="store", dest="output_prefix", required=True,
                    help="Prefix of output files")
parser.add_argument("--format", action="store", dest="format", default="tab",
                    help="Format of LAST alignments. Allowed: tab(default), tab_mismap")
parser.add_argument("-w", "--white_id_file", action="store", dest="white_id_file",
                    help="File with scaffold ids from white list or corresponding comma-separated list."
                         "NOTE: filtering is done BEFORE renaming by synonyms!")
parser.add_argument("-b", "--black_id_file", action="store", dest="black_id_file",
                    help="File with scaffold ids from black list or corresponding comma-separated list."
                         "NOTE: filtering is done BEFORE renaming by synonyms!")

parser.add_argument("-s", "--syn_file", action="store", dest="syn_file",
                    help="File with scaffold id synonyms")
parser.add_argument("--syn_file_key_column", action="store", dest="syn_file_key_column",
                    default=0, type=int,
                    help="Column(0-based) with key(current id) for scaffolds in synonym file")
parser.add_argument("--syn_file_value_column", action="store", dest="syn_file_value_column",
                    default=1, type=int,
                    help="Column(0-based) with value(synonym id) for scaffolds in synonym file synonym")

parser.add_argument("-l", "--label", action="store", dest="label",
                    help="Label for genome(both X and Y axis)")

parser.add_argument("-t", "--title", action="store", dest="title",
                    help="Title of dot plot")
parser.add_argument("-e", "--extensions", action="store", dest="extensions", type=lambda x: x.split(","),
                    default=["png", ],
                    help="Comma-separated list of extensions for histogram files. Default: png")

parser.add_argument("-d", "--dpi", action="store", dest="dpi", type=int, default=400,
                    help="DPI of figure. Default: 400")
parser.add_argument("-f", "--figsize", action="store", dest="figsize", type=lambda s: map(int, s.split(",")),
                    default=(12, 12),
                    help="Size of figure in inches(two comma-separated ints). Default: 12,12")
parser.add_argument("-a", "--antialiasing", action="store_true", dest="antialiasing", default=False,
                    help="Enable antialiasing. Use this option only for small sequences, i.e segments of chromosomes ")

parser.add_argument("--linewidth", action="store", dest="linewidth", type=float,
                    default=0.01,
                    help="Width of alignment lines. Default: 0.01")
parser.add_argument("--gridwidth", action="store", dest="gridwidth", type=float,
                    default=1,
                    help="Width of grid lines. Default: 1")
parser.add_argument("--scaffold_label_fontsize", action="store", dest="scaffold_label_fontsize", type=float,
                    default=13,
                    help="Fontsize for scaffold labels. Default: 13")
parser.add_argument("--grid_color", action="store", dest="grid_color", default='black',
                    help="Color of grid lines. Default: 'black'")
parser.add_argument("--hide_grid", action="store_true", dest="hide_grid", default=False,
                    help="Hide grid. Default: False")
parser.add_argument("--bar_color", action="store", dest="bar_color", default='grey',
                    help="Color of bars. Default: 'grey'")
parser.add_argument("--same_strand_color", action="store", dest="same_strand_color", default='blue',
                    help="Color of alignment line if query and target are in same strand. Default: 'blue'")
parser.add_argument("--diff_strand_color", action="store", dest="diff_strand_color", default='red',
                    help="Color of alignment line if query and target are in different strand. Default: 'red'")

parser.add_argument("--target_scaffold_labels_angle", action="store", dest="target_scaffold_labels_angle",
                    default=45, type=int,
                    help="Angle for labels of target scaffolds. Default: 45")
parser.add_argument("--query_scaffold_labels_angle", action="store", dest="query_scaffold_labels_angle",
                    default=0, type=int,
                    help="Angle for labels of query scaffolds. Default: 0")
parser.add_argument("--hide_labels", action="store_true", dest="hide_labels", default=False,
                    help="Hide labels of scaffolds. Default: False")

parser.add_argument("--bottom_offset", action="store", dest="bottom_offset",
                    default=0.1, type=float,
                    help="Bottom offset for subplot. Default: 0.1")
parser.add_argument("--top_offset", action="store", dest="top_offset",
                    default=0.9, type=float,
                    help="Top offset for subplot. Default: 0.9")
parser.add_argument("--left_offset", action="store", dest="left_offset",
                    default=0.1, type=float,
                    help="Left offset for subplot. Default: 0.1")
parser.add_argument("--right_offset", action="store", dest="right_offset",
                    default=0.9, type=float,
                    help="Right offset for subplot. Default: 0.9")
parser.add_argument("--x_axis_visible", action="store_true", dest="x_axis_visible",
                    default=False,
                    help="Make X axis visible. Default: False")
parser.add_argument("--y_axis_visible", action="store_true", dest="y_axis_visible",
                    default=False,
                    help="Make Y axis visible. Default: False")

args = parser.parse_args()

if args.white_id_file:
    white_list = IdList(filename=args.white_id_file) if os.path.isfile(args.white_id_file) else IdList(args.white_id_file.split(","))
else:
    white_list = IdList()

if args.black_id_file:
    black_list = IdList(filename=args.black_id_file) if os.path.isfile(args.black_id_file) else IdList(args.black_id_file.split(","))
else:
    black_list = IdList()

syn_dict = SynDict(filename=args.syn_file,
                   key_index=args.syn_file_key_column,
                   value_index=args.syn_file_value_column)

if white_list and syn_dict:
    syn_white_list = IdList([syn_dict[entry] for entry in white_list])
elif white_list:
    syn_white_list = white_list
else:
    syn_white_list = IdList()

last_collection = CollectionLast(args.input_last_tab,
                                 target_white_list=white_list,
                                 target_black_list=black_list,
                                 query_white_list=white_list,
                                 query_black_list=black_list,
                                 query_syn_dict=syn_dict,
                                 target_syn_dict=syn_dict,
                                 format=args.format
                                 )

last_collection.write("%s.syn.tab" % args.output_prefix)

DrawingRoutines.draw_dot_plot_per_scaffold_from_last_self_alignment(last_collection,
                                                                    output_prefix=args.output_prefix,
                                                                    extension_list=args.extensions,
                                                                    scaffold_black_list=(),
                                                                    scaffold_white_list=syn_white_list,
                                                                    scaffold_reverse_list=(),
                                                                    figsize=args.figsize, dpi=args.dpi,
                                                                    grid_color=args.grid_color,
                                                                    bar_color=args.bar_color,
                                                                    same_strand_color=args.same_strand_color,
                                                                    diff_strand_color=args.diff_strand_color,
                                                                    title=args.title,
                                                                    label=args.label,
                                                                    linewidth=args.linewidth,
                                                                    gridwidth=args.gridwidth,
                                                                    antialiased_lines=args.antialiasing,
                                                                    scaffold_label_fontsize=args.scaffold_label_fontsize,
                                                                    target_scaffold_labels_angle=args.target_scaffold_labels_angle,
                                                                    query_scaffold_labels_angle=args.query_scaffold_labels_angle,
                                                                    show_grid=not args.hide_grid,
                                                                    show_labels=not args.hide_labels,
                                                                    top_offset=args.top_offset,
                                                                    bottom_offset=args.bottom_offset,
                                                                    left_offset=args.left_offset,
                                                                    right_offset=args.right_offset,
                                                                    x_axis_visible=args.x_axis_visible,
                                                                    y_axis_visible=args.y_axis_visible
                                                                    )