#!/usr/bin/env python
# encoding: utf-8

"""
@version: v1.0
@author: BaoChengCai
@contact: baochengcai@lanjingren.com
@site: http://www.meipian.cn
@software: PyCharm
@file: inception.py
@time: 2019/3/19 10:45 AM
"""
# from __future__ import unicode_literals
import os,re
os.environ.update({"DJANGO_SETTINGS_MODULE": "SqlAudit.settings"})
from app.work_order.models import WorkSheet
from app.instance.action import InceptionClass
from app.work_order.models import InceptionRes
from .sendmail import sendMail
from django.contrib.auth.models import User

class Inception_main:
    """
    inception 执行程序
    """
    def __init__(self,work_id):
        """
        初始化
        """
        self.work_id = int(work_id)
        self.inceptionres = InceptionRes.objects.all()
        self.worksheet = WorkSheet.objects.all()

    def readWorksheet(self):
        """
        读取任务列表
        instance 实例地址
        db_name 数据库名称
        db_port 数据库端口
        db_sql  需要执行的SQL
        exec_way 执行方式，0为自动执行，1为手动执行
        exec_timepoint 执行时段，0为立即执行，1为凌晨2点后执行
        :return: work_status =2 的执行任务内容
        """
        worksheetInfo = self.worksheet.filter(work_id=self.work_id, work_status=2).values('work_id','instance','db_name','db_port','db_sql','exec_way','exec_timepoint').first()
        return worksheetInfo

    def sendAuthorEmaiL(self):
        """
        发送邮件功能，调用task的异步任务
        :return:
        """
        author = self.worksheet.filter(work_id=self.work_id).values('author').first()
        author_email = User.objects.filter(username=author['author']).values('email').first()
        author_email = author_email['email']
        sendmail = sendMail("SQL执行完成通知","
你有一个SQL工单执行完成，请登录SQL审核系统查看。

请点击: http://sqlaudit.meipian.cn/#/task/mytask/
",author_email)
        return sendmail

    def execInception(self):
        """
        使用inception执行SQL语句
        :return:
        """
        try:
            worksheetInfo = self.readWorksheet()
            print('worksheet: ',worksheetInfo)
            inc = InceptionClass()
            print(inc)
            param = {
                'instance_host': worksheetInfo['instance'],
                'instance_port': worksheetInfo['db_port'],
                'dbname': worksheetInfo['db_name'],
                'sql': re.sub(r'`','',worksheetInfo['db_sql']),
                'execute': 1
            }
            print ('inception start----------------------')
            data = inc.CheckSql(**param)
            print('inc_res: ',data)
            print('inception end-------------------------')

            for row in data:
                del row['ID']
                row['work_id'] = worksheetInfo['work_id']
                row['exec_status'] = int(row['errlevel']) > 0 and -1 or 1
                self.inceptionres.create(**row)
            self.worksheet.filter(work_id=worksheetInfo['work_id']).update(work_status=3) # 修改执行状态，执行完成
            print('sendmail: ',self.sendAuthorEmaiL())
            result = data
        except Exception as e:
            result = e
        return result

def runInc(work_id):
    """
    执行函数
    :return: inception的执行结果
    """
    inceptionMain = Inception_main(work_id)
    return inceptionMain.execInception()

if __name__ == '__main__':
    inceptionMain = Inception_main()
    inceptionMain.readWorksheet()


# Artık dersler daha komplike olacağı için pencere classı üretilecek
import sys
from PyQt5 import QtWidgets

class Pencere(QtWidgets.QWidget):
    def __init__(self):

        super().__init__()  # burada artık miras aldığım Qwidgetı çalıştırmış oldum
        self.init_ui()  # bunu yaparak aslında her işimi init ui içerisinde yapacak olduğumu programa bildiriyorum

    def init_ui(self):

        self.yazi_alani = QtWidgets.QLabel("Wattage Calculator")
        self.calculateButton = QtWidgets.QPushButton("Click me!")
        self.say = 0  # butona ne kadar tıklandığını saymak adına ekliyorum

        verticalAllignment = QtWidgets.QVBoxLayout()
        verticalAllignment.addWidget(self.yazi_alani)
        verticalAllignment.addWidget(self.calculateButton)
        verticalAllignment.addStretch()

        horizantalAllignment = QtWidgets.QHBoxLayout()
        horizantalAllignment.addLayout(verticalAllignment)
        horizantalAllignment.addStretch()

        self.setGeometry(500, 500, 500, 500)
        self.setLayout(
            horizantalAllignment
        )  # diğer aşamalarda nasıl ki pencerenin içerisine atıyorsak burda da self.setLayout yaparak pencerenin içerisine dahil ediyoruz
        self.calculateButton.clicked.connect(
            self.click
        )  # bu butona tıklandığında parantez içerisindeki bir fonksiyona bağlan demiş oluyorum
        self.show()

    def click(self):
        self.say += 1
        self.yazi_alani.setText("İşlem Gerçekleşti" + str(self.say) + "defa tıklandı")

# Bunları görebilmek için öncelikle uygulama oluşturmamız gerekiyor
app = QtWidgets.QApplication(sys.argv)  # sys.argv  argümanları yollamak için
pencere = Pencere()
sys.exit(app.exec_())  # uygulamayı döngüye sokmak için app.exec_() kullanıyoruz


from math import sqrt
import random
import dendrogram

class bicluster:
    def __init__(self,vec,left=None,right=None,dist=0.0,id=None):
        self.left = left
        self.right = right
        self.vec = vec
        self.id = id
        self.distance = dist

def readfile(filename):
    with open(filename) as file:
        lines=[line for line in file]
        # First line is the column titles
        colnames=lines[0].strip().split('\t')[1:]
        rownames=[]
        data=[]
        for line in lines[1:]:
            p=line.strip().split('\t')
            # First column in each row is the rowname
            rownames.append(p[0])
            # The data for this row is the remainder of the row
            data.append([float(x) for x in p[1:]])
        return rownames,colnames,data

# Formes calcular distancies
##########################################################################

def pearson(v1, v2):
    # Simple sums
    sum1 = sum(v1)
    sum2 = sum(v2)
    # Sums of the squares
    sum1Sq = sum([pow(v,2) for v in v1])
    sum2Sq = sum([pow(v,2) for v in v2])
    # Sum of the products
    pSum = sum([v1[i]*v2[i] for i in range(len(v1))])
    # Calculate r (Pearson score)
    num = pSum-(sum1*sum2/len(v1))
    den = sqrt((sum1Sq-pow(sum1,2)/len(v1))*(sum2Sq-pow(sum2,2)/len(v1)))
    if den==0: return 0
    return 1.0-num/den

def euclidean(v1, v2):
    suma = 0
    for i in range(len(v1)):
        suma += (v1[i]-v2[i])**2
    return sqrt(suma)

def euclideansquared(v1, v2):
    suma = 0
    for i in range(len(v1)):
        suma += (v1[i]-v2[i])**2
    return suma

def manhattan(v1, v2):
    suma = 0
    for i in range(len(v1)):
        suma += abs(v1[i]-v2[i])
    return suma

def tchebychev(v1, v2):
    value = 0
    for i in range(len(v1)):
        value = max(value, abs((v1[i]-v2[i])))
    return value

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

def hcluster(rows, distance=pearson):
    distances = {}  # stores the distances for efficiency
    currentclustid = -1  # all except the original items have a negative id
    # Clusters are initially just the rows
    clust = [bicluster(rows[i], id=i) for i in range(len(rows))]
    while len(clust) > 1:  # stop when there is only one cluster left
        lowestpair = (0, 1)
        closest = distance(clust[0].vec, clust[1].vec)
        # loop through every pair looking for the smallest distance
        for i in range(len(clust)):
            for j in range(i+1, len(clust)):

                # distances is the cache of distance calculations
                if (clust[i].id, clust[j].id) not in distances:
                    distances[(clust[i].id, clust[j].id)] = distance(clust[i].vec, clust[j].vec)
                    d = distances[(clust[i].id, clust[j].id)]
                if d < closest:
                    closest = d
                    lowestpair = (i, j)
        # calculate the average of the two clusters
        mergevec = [(clust[lowestpair[0]].vec[i]+clust[lowestpair[1]].vec[i])/2.0
        for i in range(len(clust[0].vec))]

        # create the new cluster
        newcluster=bicluster(mergevec,left=clust[lowestpair[0]],
        right=clust[lowestpair[1]],
        dist=closest,id=currentclustid)

        # cluster ids that werent in the original set are negative
        currentclustid-=1
        del clust[lowestpair[1]]
        del clust[lowestpair[0]]
        clust.append(newcluster)
        return clust[0]

def kcluster(rows, distance=pearson, k=4, r=5):
    # Determine the minimum and maximum values for each point
    ranges = [(min([row[i] for row in rows]),
    max([row[i] for row in rows])) for i in range(len(rows[0]))]

    bestTotalDistance = 0.0
    bestTimeMatches = None

    for time in range(r):
        # Create k randomly placed centroids
        clusters = [[random.random()*(ranges[i][1]-ranges[i][0])+ranges[i][0]
        for i in range(len(rows[0]))] for j in range(k)]
        lastmatches = None
        lasttotaldistance = 0.0

        for t in range(100):
            bestmatches = [[] for i in range(k)]

            # Find which centroid is the closest for each row
            for j in range(len(rows)):
                row = rows[j]
                bestmatch = 0
                for i in range(k):
                    d = distance(clusters[i], row)
                    if d < distance(clusters[bestmatch], row): bestmatch = i
                bestmatches[bestmatch].append(j)

            # If the results are the same as last time, done
            if bestmatches == lastmatches: break
            lastmatches = bestmatches

            # Move the centroids to the average of their members
            for i in range(k):
                avgs = [0.0]*len(rows[0])
                if len(bestmatches[i]) > 0:
                    for rowid in bestmatches[i]:
                        for m in range(len(rows[rowid])):
                            avgs[m] += rows[rowid][m]
                    for j in range(len(avgs)):
                        avgs[j] /= len(bestmatches[i])
                    clusters[i] = avgs

            # Distance of the elements to their centroids
            distances = [[] for _ in range(k)]
            for i in range(k):
                distances[i] = 0.0
                if len(bestmatches[i]) > 0:
                    for rowid in bestmatches[i]:
                        distances[i] += euclideansquared(clusters[i], rows[rowid])
            totaldistance = 0.0
            for dist in distances:
                totaldistance += dist
            totaldistance = totaldistance / k

            lasttotaldistance = totaldistance

        # Restarting polices
        # print("\nDistància actual" + str(lasttotaldistance))       # Used to see the evolution of the distances
        # print("Millor" + str(bestTotalDistance))
        if time == 0:
            bestTotalDistance = lasttotaldistance
            bestTimeMatches = lastmatches
        else:
            # We want to keep only the bestMatches Of AllTime
            if bestTotalDistance > lasttotaldistance:
                bestTotalDistance = lasttotaldistance
                bestTimeMatches = lastmatches

    return bestTimeMatches, bestTotalDistance       # returns the final value of the distance

def printclust(clust, labels=None, n=0):
    # indent to make hirarchy layout
    # utilitzar la funcio de dentogram
    pass


#!/usr/bin/env python
import os
from os import path
from setuptools import setup, Extension
from setuptools.command.install import install
from setuptools.command.develop import develop
from setuptools.command.build_ext import build_ext
from setuptools.command.test import test
import subprocess
from copy import deepcopy

def parse_path_environ(key):
    env = os.environ.get(key)
    if not env:
        return None
    return env.split(':')

MOZJS_INCLUDE_DIRS = parse_path_environ('MOZJS_INCLUDE_DIRS')
MOZJS_LIB_DIRS = parse_path_environ('MOZJS_LIB_DIRS')
MOZJS_PYTHON = os.environ.get('MOZJS_PYTHON', 'python2.7')
MOZJS_PYTHON = subprocess.check_output(['which', MOZJS_PYTHON])

MOZJS = 'temp/build'
INCLUDE_DIRS = MOZJS_INCLUDE_DIRS or [path.join(MOZJS, 'include/mozjs-31/')]
LIB_DIRS = MOZJS_LIB_DIRS or [path.join(MOZJS, 'lib'),]

def find_sources():
    return [
        os.path.join(d, f)
            for (d, dpath, files) in os.walk('src')
                for f in files if f.endswith('.cpp')
    ]

class CustomBuildExt(build_ext):
    user_options = build_ext.user_options + [
        ('static', None, 'create with static linking',),
        ('mozjs', None, 'download and build mozjs'),]

    def initialize_options(self):
        self.mozjs = None
        self.static = None
        build_ext.initialize_options(self)

    def finalize_options(self):
        build_ext.finalize_options(self)
        self.pyjs_ext = next(ext for ext in self.extensions if ext.name == 'py_js')

        self.pyjs_ext.languages = 'c++'
        self.pyjs_ext.library_dirs = LIB_DIRS
        self.pyjs_ext.include_dirs = INCLUDE_DIRS
        self.pyjs_ext.libraries = ['z', 'm', 'dl']
        if self.static:
            self.pyjs_ext.extra_objects = [path.join(MOZJS, 'lib/libmozjs-31.a')]

    def run(self):
        if self.mozjs:
            self.run_mozjs()

        js_config = JSConfig(MOZJS)
        self.pyjs_ext.extra_compile_args = js_config.get_cflags()
        build_ext.run(self)

    def run_mozjs(self):
        subprocess.check_call(['bash', 'setup.sh', '--download'])

        env = deepcopy(os.environ)
        env['PYTHON'] = MOZJS_PYTHON
        resp = subprocess.Popen(['bash', 'setup.sh', '--build'], env=env).wait()
        if resp != 0:
            raise Exception("There is an exception in --build step")
        subprocess.check_call(['bash', 'setup.sh', '--install'])

def read(fname):
    return open(os.path.join(os.path.dirname(__file__), fname)).read()

class JSConfig(object):
#TODO:
# search global
    def __init__(self, dirname):
        self.f = path.join(dirname, 'bin/js-config')
        if not path.exists(self.f):
            raise Exception("Should js-config path exists")

    def get_cflags(self):
        return subprocess.check_output([self.f, '--cflags']).split()

setup(name='py_js',
      cmdclass={
          'build_ext': CustomBuildExt
      },
      version='1.0a3',
      description='Python-javascript bridge',
      long_description=read('README.rst'),
      url="https://github.com/new-mind/pyjs",
      author='jiojiajiu',
      author_email='jiojiajiu@gmail.com',
      license='MIT',
      ext_modules=[Extension('py_js', sources=find_sources()) ],
      test_suite="tests",
      classifiers=[
        'Development Status :: 3 - Alpha',
        'Intended Audience :: Developers',
        'Topic :: Software Development :: Build Tools',
        'License :: OSI Approved :: MIT License',
        'Programming Language :: Python :: 2',
        'Programming Language :: Python :: 2.6',
        'Programming Language :: Python :: 2.7'
      ],
      keywords='javascript development spidermonkey')


__author__ = 'Chloe'

'''
Run this file on the box which you would like the
client to run on. Mongo needs to be running first.
This initialises the Mongo Collections

Python3
'''

from pymongo import MongoClient

class PopulateMongo:

    _connection_string = 'mongodb://localhost:27017/'

    def __init__(self, connection_string=None):
        if connection_string:
            self.client = MongoClient(connection_string)
        else:
            self.client = MongoClient(self._connection_string)
        self.db = self.client['CoventryMakerspace']
        self.makers_collection = self.db['Makers']
        self.checkin_collection = self.db['Checkin']

    def populate_db(self):
        maker = {'device_id': "1234",
                  'name': 'TestMaker'}

        checkin = {'device_id': "1234",
                   "x_pos": "1.1234",
                   "y_pos": "1234",
                   "checkin": "True"}
        self.makers_collection.insert(maker)
        self.checkin_collection.insert(checkin)

    def test_db(self):
        result = self.makers_collection.find_one({'name': 'TestMaker'})
        print(result)
        result = self.checkin_collection.find_one({'device_id': '1234'})
        print(result)
        print("The DB creation was a success!")

populate = PopulateMongo()
populate.populate_db()
populate.test_db()


"""

This script finds the best parameter through grid search
on a subset of training data, and then train a full
model using all training data along with the best
parameters. Then the model is use to make prediction on
test data.

"""
from __future__ import print_function
import sys, os

def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)

# eprint("1..............\n")
# eprint(os.path.realpath(__file__))
# eprint(os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), '../src/libsvm-3.21/python'))

sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), '../src/libsvm-3.21/python'))
from svmutil import *

sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), '../src/libsvm-3.21/tools'))
from grid import *

# import train and test data
yTrain, xTrain = svm_read_problem(sys.argv[1])
yTest, xTest = svm_read_problem(sys.argv[2])

# grid search for best parameter on a subset of training data
rate, grid = find_parameters(sys.argv[5], '-s 0 -t 2 -v 5 -out null')

# train linear SVM
prob = svm_problem(yTrain, xTrain)
param = svm_parameter(" ".join(['-s 0 -t 2 -c', str(grid['c']), '-g', str(grid['g'])]))
m = svm_train(prob, param)

# prediction on test data
p_label, p_acc, p_val = svm_predict(yTest, xTest, m)

# save model
svm_save_model(sys.argv[3], m)

# output confident score
predout = open(sys.argv[4],'w')
predout.write('\n'.join(map(str,p_val)))
predout.close()


# -*- coding: utf-8 -*-
"""
general functions and fitting.
(a half-decent language would have those functions already).
Python is worth what it costs.
"""

from itertools import product
import sys  # command line argument, print w/o newline, version
import numpy as np

NSmplRst = 2
NGrp = 212
NPad = 45
NADC = 7
NColInBlock = 32
NRowInBlock = NADC  # 7
NCol = NColInBlock * NPad  # 32*45=1440, including RefCol
NRow = NADC * NGrp  # 212*7=1484
NbitPerPix = 15

iGn = 0
iCrs = 1
iFn = 2
NGnCrsFn = 3
iSmpl = 0
#
ERRint16 = -256  # negative value usable to track Gn/Crs/Fn from missing pack
ERRDLSraw = 65535
# forbidden uint16, usable to track "pixel" from missing pack

# percival-specific data reordering functions

# from P2M manual
N_xcolArray = 4
N_ncolArray = 8
N_col_block = N_xcolArray * N_ncolArray
colArray = np.arange(N_col_block).reshape((N_xcolArray,
                                           N_ncolArray)).transpose()
colArray = np.fliplr(colArray)  # in the end it is colArray[ix,in]

# this gives the (iADC,iCol) indices of a pixel in a Rowblk,
# given its sequence in the streamout
ADCcolArray_1DA = []
for i_n, i_adc, i_x in product(range(N_ncolArray),
                               range(NADC)[::-1],
                               range(N_xcolArray)):
    ADCcolArray_1DA.append((i_adc, colArray[i_n, i_x]))

# to use this:  for ipix in range(32*7):
# (ord_ADC,ord_col)=ADCcolArray_1DA[ipix]
ADCcolArray_1DA = np.array(ADCcolArray_1DA)

iG = 0
iH0 = np.arange(21+1, 0+1-1, -1)
iH1 = np.arange(22+21+1, 22+0+1-1, -1)
iP2M_ColGrp = np.append(np.array(iG), iH0)
iP2M_ColGrp = np.append(iP2M_ColGrp, iH1)

def printcol(string, colour):
    ''' write in colour (red/green/orange/blue/purple) '''
    white = '\033[0m'  # white (normal)
    if (colour == 'black'):
        out_color = '\033[30m'  # black
    elif (colour == 'red'):
        out_color = '\033[31m'  # red
    elif (colour == 'green'):
        out_color = '\033[32m'  # green
    elif (colour == 'orange'):
        out_color = '\033[33m'  # orange
    elif (colour == 'blue'):
        out_color = '\033[34m'  # blue
    elif (colour == 'purple'):
        out_color = '\033[35m'  # purple
    else:
        out_color = '\033[30m'
    print(out_color+string+white)
    sys.stdout.flush()

def convert_hex_byteSwap_2nd(data2convert_Ar):
    ''' interpret the ints in an array as 16 bits.
        byte-swap them: (byte0,byte1) => (byte1,byte0)
    '''
    by0 = np.mod(data2convert_Ar, 2**8).astype('uint16')
    by1 = data2convert_Ar//(2**8)
    by1 = by1.astype('uint16')
    data_ByteSwapped_Ar = (by0 * (2**8)) + by1
    data_ByteSwapped_Ar = data_ByteSwapped_Ar.astype('uint16')
    return (data_ByteSwapped_Ar)

def convert_uint_2_bits_Ar(in_intAr, Nbits):
    ''' convert (numpyarray of uint => array of Nbits bits)
        for many bits in parallel
    '''
    inSize_T = in_intAr.shape
    in_intAr_flat = in_intAr.flatten()
    out_NbitAr = np.zeros((len(in_intAr_flat), Nbits), dtype=bool)
    for iBits in range(Nbits):
        out_NbitAr[:, iBits] = (in_intAr_flat >> iBits) & 1
    out_NbitAr = out_NbitAr.reshape(inSize_T+(Nbits,))
    return out_NbitAr

def convert_britishBits_Ar(british_bit_array):
    ''' Conversion of bits defined by RAL to normal bit Conversion:
            0=>1 , 1=>0

         Example:
            >>> convert_britishBis_Ar(1)
                0
    '''
    return 1 - british_bit_array

def convert_bits_2_uint16_Ar(bitarray):
    """ Convert (numpyarray of [... , ... , n_bits] to
        array of [... , ... ](int) """
    shape = bitarray.shape
    n_bits = shape[-1]
    out = np.zeros(shape[:-1], dtype='uint16')
    for ibit in range(n_bits):
        out = (out << 1) | bitarray[..., n_bits-ibit-1]
    return out

def decode_dataset_8bit(arr_in, bit_mask, bit_shift):
    """ Masks out bits and shifts """
    arr_out = np.bitwise_and(arr_in, bit_mask)
    arr_out = np.right_shift(arr_out, bit_shift)
    arr_out = arr_out.astype(np.uint8)
    return arr_out

def aggregate_to_GnCrsFn(raw_dset):
    """Extracts the coarse, fine and gain bits.
    0   1   2   3   4   5   6   7   8   9   10  11  12  13  14  15
    -   B0  B1  F0  F1  F2  F3  F4  F5  F6  F7  C0  C1  C2  C3  C4
    """
    coarse_adc = decode_dataset_8bit(arr_in=raw_dset,
                                     bit_mask=0x1F,
                                     bit_shift=0)  # 0x1F -> 0000000000011111
    fine_adc = decode_dataset_8bit(arr_in=raw_dset,
                                   bit_mask=0x1FE0,
                                   bit_shift=5)  # 0x1FE0 -> 0001111111100000
    gain_bits = decode_dataset_8bit(arr_in=raw_dset,
                                    bit_mask=0x6000,
                                    bit_shift=5+8)  # 0x6000->0110000000000000
    return coarse_adc, fine_adc, gain_bits

# P2M specific descrambling
def reorder_pixels_GnCrsFn_par(disord_6DAr, NADC, NColInRowBlk):
    """ P2M pixel reorder for a image.
        (NImg,NSmplrst, NGrp,NDataPads,NADC*NColInRowBlk,3),disordered
          ((NImg,NSmplrst, NGrp,NDataPads,NADC,NColInRowBlk,3),ordered
    """
    (aux_NImg,
     aux_NSmplRst,
     aux_NGrp,
     aux_NPads,
     aux_pixInBlk,
     auxNGnCrsFn) = disord_6DAr.shape

    output_shape = (aux_NImg,
                    aux_NSmplRst,
                    aux_NGrp,
                    aux_NPads,
                    NADC,
                    NColInRowBlk,
                    auxNGnCrsFn)
    ord_7DAr = np.zeros(output_shape, dtype='uint8')
    aux_pixOrd_padDisord_7DAr = np.zeros(output_shape, dtype='uint8')
    # pixel reorder inside each block
    for ipix in range(NADC*NColInRowBlk):
        (ord_ADC, ord_Col) = ADCcolArray_1DA[ipix]
        aux_pixOrd_padDisord_7DAr[...,
                                  ord_ADC, ord_Col, :] = disord_6DAr[..., ipix, :]
    ord_7DAr[...] = aux_pixOrd_padDisord_7DAr[..., iP2M_ColGrp[:], :, :, :]
    return ord_7DAr

def descramble_to_crs_fn_gn(scrmblShot,
                            refColH1_0_Flag,
                            cleanMemFlag,
                            verboseFlag):
    """
    Descramble content of 1 odinDAQ-acquired (raw) shout.
    Return content in Gn/Crs/Fn format.

    Here is how data are scrambled:

    1a) the chips send data out interleaving RowGroups
        (7row x (32 Col x 45 pads) ) from Sample/Reset, as:
        Smpl, Smpl,   Smpl, Rst, Smpl, Rst, ... , Smpl, Rst,   Rst, Rst
    1b) the position of pixels (pix0/1/2 in the next point) is mapped to
        the (7row x 32 Col) block according to the adc_cols lost
    1c) inside a (7row x 32 Col) block, the data are streamed out as:
        bit0-of-pix0, bit0-of-pix1, bit0-of-pix2, ... , bit0-of-pix1, ...
    1d) all the data coming from the sensor are bit-inverted: 0->1; 1->0
    2a) the mezzanine takes the data coming from each (7 x 32) block,
        and add a "0" it in front of every 15 bits:
        xxx xxxx xxxx xxxx  yyy ... => 0xxx xxxx xxxx xxxx 0yyy ...
    2b) the mezzanine takes the data coming from a 44x (7 x 32) blocks
        (this corresponds to a complete RowCrp, ignoring RefCol )
        and interleaves 32 bits at a time:
        32b-from-pad1, 32b-from-pad2, ... , 32b-from-pad44,
        next-32b-from-pad1, next-32b-from-pad2, ...
    2c) the mezzanine packs the bits in Bytes, and the Bytes in UDPackets
        4 UDPackets contain all the bits of a 44x (7 x 32) Rowgrp.
        A complete image (Smpl+Rst) = 1696 UDPackets
        each UDPack has 4928Byte of information, and 112Byte of header.
        each UDPack is univocally identified by the header:
        - which Img (frame) the packet belongs to
        - datatype: whether it is part of a Smpl/Rst (respect. 1 or 0)
        - subframenumber (0 or 1)
        - packetnumber (0:423), which determines the RowGrp the
        packet's data goes into
        there are 1696 packets in an image; a packet is identified by the
        triplet (datatype,subframenumber,packetnumber)
    3a) the packets are sent from 2 mezzanine links (some on one link, some
        on the other), and are saved to 2 h5 files by odinDAQ
    4a) OdinDAQ byte-swaps every 16-bit sequence (A8 B8 becomes B8 A8)
    5a) OdinDAQ rearranges the 4 quarters of each rowblock
    6a) OdinDAQ seems to invert Smpl and Rst

    Args:
        dataSmpl_fl0,dataRst_fl0,dataSmpl_fl1,dataRst_fl1
        swapSmplRstFlag,seqModFlag, refColH1_0_Flag,verboseFlag,cleanMemFlag

    Returns:
        descrambled array (DLSraw format)
    """
    out_dscrmbl = np.copy(scrmblShot)
    scrmblShot = np.asarray(scrmblShot)
    NColInBlk = NColInBlock
    NDataPad = NPad - 1

    # convert to (1-img,row, col) because easier to integrate
    print(scrmblShot.shape)
    (auxNRow, auxNCol) = scrmblShot.shape
    scrmblShot = scrmblShot.reshape((1, auxNRow, auxNCol))
    NImg = 1

    # solving 4a DAQ-scrambling: byte swap in hex (By0,By1) => (By1,By0)
    # N.B.: possible swipe between Smpl and Rst
    if verboseFlag:
        printcol("solving DAQ-scrambling: byte-swapping and Smpl-Rst-swapping",
                 'blue')
    scrmblShot_byteSwap = convert_hex_byteSwap_2nd(scrmblShot)
    if cleanMemFlag:
        del scrmblShot

    # solving DAQ-scrambling: "pixel" reordering
    if verboseFlag:
        printcol("solving DAQ-scrambling: reordering subframes", 'blue')

    def convert_odin_daq_2_mezzanine(shot_in):
        ' descrambles the OdinDAQ-scrambling '
        (aux_n_img, aux_nrow, aux_ncol) = shot_in.shape
        aux_reord = shot_in.reshape((aux_n_img,
                                     NGrp,
                                     NADC,
                                     2,
                                     aux_ncol//2))
        aux_reord = np.transpose(aux_reord, (0, 1, 3, 2, 4))
        aux_reord = aux_reord.reshape((aux_n_img,
                                      NGrp,
                                      2,
                                      2,
                                      NADC*aux_ncol//4))
        aux_reordered = np.ones((aux_n_img,
                                 NGrp,
                                 4,
                                 NADC*aux_ncol//4), dtype='uint16') * ERRDLSraw
        aux_reordered[..., 0, :] = aux_reord[..., 0, 0, :]
        aux_reordered[..., 1, :] = aux_reord[..., 1, 0, :]
        aux_reordered[..., 2, :] = aux_reord[..., 0, 1, :]
        aux_reordered[..., 3, :] = aux_reord[..., 1, 1, :]
        aux_reordered = aux_reordered.reshape((aux_n_img,
                                               NGrp*NADC,
                                               aux_ncol))
        return aux_reordered

    data2srcmbl_noRefCol = np.ones((NImg,
                                    NSmplRst,
                                    NRow,
                                    auxNCol), dtype='uint16') * ERRDLSraw
    data2srcmbl_noRefCol[:, iSmpl, :, :] = convert_odin_daq_2_mezzanine(scrmblShot_byteSwap)
    if cleanMemFlag:
        del scrmblShot_byteSwap

    data2srcmbl_noRefCol = data2srcmbl_noRefCol.reshape((NImg,
                                                         NSmplRst,
                                                         NGrp,
                                                         NADC,
                                                         auxNCol))

    # track missing packets:
    # False==RowGrp OK; True== packet(s) missing makes rowgroup moot
    # (1111 1111 1111 1111 instead of 0xxx xxxx xxxx xxxx)
    missingRowGrp_Tracker = data2srcmbl_noRefCol[..., 0, 0] == ERRDLSraw
    # ---
    # descramble proper
    if verboseFlag:
        printcol("solving mezzanine&chip-scrambling: pixel descrambling",
                 'blue')
    multiImgWithRefCol = np.zeros((NImg,
                                   NSmplRst,
                                   NGrp,
                                   NPad,
                                   NADC*NColInBlk,
                                   NGnCrsFn), dtype='int16')
    #
    # refCol
    multiImgWithRefCol[..., 0, :, :] = ERRint16
    #
    # descrambling
    data2srcmbl_noRefCol = data2srcmbl_noRefCol.reshape((NImg,
                                                         NSmplRst,
                                                         NGrp,
                                                         NADC*auxNCol//(NDataPad*2),
                                                         NDataPad,
                                                         2))
    # 32bit = 2"pix" from 1st pad, 2"pix" from 2nd pad, etc.
    data2srcmbl_noRefCol = np.transpose(data2srcmbl_noRefCol,
                                        (0, 1, 2, 4, 3, 5)).reshape((NImg,
                                                                     NSmplRst,
                                                                     NGrp,
                                                                     NDataPad,
                                                                     NADC*auxNCol//NDataPad))
    # (NSmplRst,NGrp,NDataPad,NADC*aux_NCol//NDataPad)
    theseImg_bitted = convert_uint_2_bits_Ar(data2srcmbl_noRefCol, 16)[..., -2::-1].astype('uint8')
    # n_smplrst,n_grp,n_data_pads,n_adc*aux_ncol//n_data_pads,15bits
    if cleanMemFlag:
        del data2srcmbl_noRefCol
    theseImg_bitted = theseImg_bitted.reshape((NImg,
                                               NSmplRst,
                                               NGrp,
                                               NDataPad,
                                               NbitPerPix,
                                               NADC*NColInBlk))
    theseImg_bitted = np.transpose(theseImg_bitted, (0, 1, 2, 3, 5, 4))
    # (NImg, n_smplrst,n_grp,n_data_pads,NPixsInRowBlk,15)
    theseImg_bitted = convert_britishBits_Ar(theseImg_bitted).reshape((NImg,
                                                                       NSmplRst*NGrp*NDataPad*NADC*NColInBlk,
                                                                       NbitPerPix))

    theseImg_bitted = theseImg_bitted.reshape((NImg*NSmplRst*NGrp*NDataPad*NADC*NColInBlk,
                                               NbitPerPix))
    (aux_coarse, aux_fine, aux_gain) = aggregate_to_GnCrsFn(convert_bits_2_uint16_Ar(theseImg_bitted[:, ::-1]))
    multiImgWithRefCol[..., 1:, :, iGn] = aux_gain.reshape((NImg,
                                                            NSmplRst,
                                                            NGrp,
                                                            NDataPad,
                                                            NADC*NColInBlk))
    multiImgWithRefCol[..., 1:, :, iCrs] = aux_coarse.reshape((NImg,
                                                               NSmplRst,
                                                               NGrp,
                                                               NDataPad,
                                                               NADC*NColInBlk))
    multiImgWithRefCol[..., 1:, :, iFn] = aux_fine.reshape((NImg,
                                                            NSmplRst,
                                                            NGrp,
                                                            NDataPad,
                                                            NADC*NColInBlk))
    if cleanMemFlag:
        del aux_gain
        del aux_coarse
        del aux_fine
        del theseImg_bitted

    # reorder pixels and pads
    if verboseFlag:
        printcol("solving chip-scrambling: pixel reordering", 'blue')
    multiImg_Grp_dscrmbld = reorder_pixels_GnCrsFn_par(multiImgWithRefCol,
                                                       NADC,
                                                       NColInBlk)
    if cleanMemFlag:
        del multiImgWithRefCol

    # add error tracking
    if verboseFlag:
        printcol("lost packet tracking", 'blue')
    multiImg_Grp_dscrmbld = multiImg_Grp_dscrmbld.astype('int16')
    # -256 upto 255
    for iImg in range(NImg):
        for iGrp in range(NGrp):
            for iSmplRst in range(NSmplRst):
                if (missingRowGrp_Tracker[iImg, iSmplRst, iGrp]):
                    multiImg_Grp_dscrmbld[iImg, iSmplRst, iGrp, ...] = ERRint16

    # also err tracking for ref col
    multiImg_Grp_dscrmbld[..., 0, :, :, :] = ERRint16
    if refColH1_0_Flag:
        if verboseFlag:
            printcol("moving RefCol data to G", 'blue')
        multiImg_Grp_dscrmbld[..., 0, :, :, :] = multiImg_Grp_dscrmbld[..., 44, :, :, :]
        multiImg_Grp_dscrmbld[..., 44, :, :, :] = ERRint16
    # ---
    #
    # reshaping as an Img array: (NImg,Smpl/Rst,n_grp,n_adc,n_pad,NColInBlk,Gn/Crs/Fn)
    if verboseFlag:
        printcol("reshaping as an Img array", 'blue')
    dscrmbld_GnCrsFn = np.transpose(multiImg_Grp_dscrmbld,
                                    (0, 1, 2, 4, 3, 5, 6)).astype('int16').reshape((NImg,
                                                                                NSmplRst,
                                                                                NGrp*NADC,
                                                                                NPad*NColInBlk,
                                                                                NGnCrsFn))
    if cleanMemFlag:
        del multiImg_Grp_dscrmbld
    # that's all folks
#    out_dscrmbl = dscrmbld_GnCrsFn[0, 0, :, 32:, iCrs].astype('uint16')
#    return out_dscrmbl

    coarse = dscrmbld_GnCrsFn[0, 0, :, 32:, iCrs].astype('uint16')
    fine = dscrmbld_GnCrsFn[0, 0, :, 32:, iFn].astype('uint16')
    gain = dscrmbld_GnCrsFn[0, 0, :, 32:, iGn].astype('uint16')

    return (coarse, fine, gain)


import socket
import sys
import json
import threading
from threading import Thread, Lock
import time
from block import Transaction, BlockChain, BallotNum

PID = int(sys.argv[1])
print("Process id: ", PID)

IP = "127.0.0.1"
BUFFER_SIZE = 1024
CLIENTS = []
LAMPORT = 0
BALANCE = 10
MAJORITY = 1
SEQ_NUM = 0
BALLOT_NUM = BallotNum()
ACCEPT_NUM = BallotNum()
ACCEPT_VAL = []
ACK_COUNT = 0
ACCEPT_COUNT = 0
MAX_ACK_NUM = BallotNum()
MAX_ACK_VAL = []
isLeader = False
transaction_log = []

PORT = 5000+PID
clientConn = {}
pidConn = {}

def sendMessage(msg, conn):
    time.sleep(5)
    conn.sendall(msg.encode('utf-8'))

def incrementAckCount():
	global ACK_COUNT
	mutex = Lock()
	mutex.acquire()
	ACK_COUNT += 1
	mutex.release()
	print ('ACK_COUNT: ' + str(ACK_COUNT))

def incrementAcceptCount():
	global ACCEPT_COUNT
	mutex = Lock()
	mutex.acquire()
	ACCEPT_COUNT += 1
	mutex.release()
	print ('ACCEPT_COUNT: ' + str(ACCEPT_COUNT))

def sendPrepare():
    BALLOT_NUM.num += 1
    BALLOT_NUM.pid = PID
    SEQ_NUM = chain.getLastSeqNum() + 1
    data = {
        'type': 'prepare',
        'ballot': BALLOT_NUM.toJSON(),
        'seq_num': SEQ_NUM
    }
    message = json.dumps(data)
    for client in CLIENTS:
        threading.Thread(target = sendMessage, args = (message, client,)).start()
    print ('Prepare message sent to clients')

def processMessage(pid, data):
    global BALLOT_NUM
    global ACCEPT_NUM
    global ACCEPT_VAL
    global MAX_ACK_NUM
    global MAX_ACK_VAL
    global ACK_COUNT
    global ACCEPT_COUNT
    print ('Message from client ' + str(pid))
    data = json.loads(data)
    if data['type'] == 'prepare':
        ballotNum = BallotNum.load(data['ballot'])
        if ballotNum.isHigher(BALLOT_NUM):
            BALLOT_NUM = ballotNum
            val = []
            for aval in ACCEPT_VAL:
                val.append(aval.toJSON())
            data = {
                'type': 'ack',
                'ballot': BALLOT_NUM.toJSON(),
                'seq_num': chain.getLastSeqNum(),
                'accept_ballot': ACCEPT_NUM.toJSON(),
                'accept_val': val
            }
            message = json.dumps(data)
            threading.Thread(target = sendMessage, args = (message, pidConn[pid],)).start()
            print ('Ack message sent to client '+str(pid))

    elif data['type'] == 'ack':
        #  check for majority and send accept to followers
        incrementAckCount()
        acceptBallot = BallotNum.load(data['accept_ballot'])
        acceptVal = data['accept_val']
        if len(acceptVal) != 0 and acceptBallot.isHigher(MAX_ACK_NUM):
            MAX_ACK_NUM = acceptBallot
            MAX_ACK_VAL = acceptVal[:]

        time.sleep(5)
        if ACK_COUNT >= MAJORITY:
            log = []
            if len(MAX_ACK_VAL) != 0:
                log = MAX_ACK_VAL[:]
            else:
                for val in transaction_log:
                    log.append(val.toJSON())

            data = {
                'type': 'accept',
                'ballot': BALLOT_NUM.toJSON(),
                'seq_num': SEQ_NUM,
                'value': log
            }
            message = json.dumps(data)
            # for client in CLIENTS:
            threading.Thread(target = sendMessage, args = (message, pidConn[pid],)).start()
            print ('Accept message sent to followers')
            ACK_COUNT = 0

    elif data['type'] == 'accept':
        ballotNum = BallotNum.load(data['ballot'])
        if ballotNum.isHigher(BALLOT_NUM):
            BALLOT_NUM = ballotNum
            ACCEPT_NUM = ballotNum
            val = []
            for aval in transaction_log:
                val.append(aval.toJSON())
            ACCEPT_VAL = [ Transaction.load(val) for val in data['value'] ]
            data = {
                'type': 'accepted',
                'ballot': BALLOT_NUM.toJSON(),
                'seq_num': chain.getLastSeqNum(),
                'value': val
            }
            message = json.dumps(data)
            threading.Thread(target = sendMessage, args = (message, pidConn[pid],)).start()
            print ('Accepted message sent to client '+str(pid))

    elif data['type'] == 'accepted':
        # do stuff and relay message to leader
        incrementAcceptCount()
        for aval in data['value']:
            transaction_log.append(Transaction.load(aval))

        time.sleep(5)
        if ACCEPT_COUNT >= MAJORITY:
            val = []
            for aval in transaction_log:
                val.append(aval.toJSON())
            data = {
                'type': 'commit',
                'ballot': BALLOT_NUM.toJSON(),
                'seq_num': SEQ_NUM,
                'value': val
            }
            message = json.dumps(data)
            # for client in CLIENTS:
            threading.Thread(target = sendMessage, args = (message, pidConn[pid],)).start()
            print ('Decide message sent to followers')
            ACCEPT_COUNT = 0
            chain.printChain()

    elif data['type'] == 'commit':
        print ('Decide message from leader')
        val = []
        for aval in data['value']:
            val.append(Transaction.load(aval))
        chain.append(data['seq_num'], val)
        transaction_log.clear()
        chain.printChain()

def getBalance(pid):
    amount = chain.getBalance(pid)
    for log in transaction_log:
        amount -= log.amount
    return amount

def processInput(data):
    dataList = data.split(',')
    if dataList[0] == 'transfer':
        # Update getBalance to get balance
        receiver = int(dataList[1])
        amount = int(dataList[2])
        amountBefore = getBalance(PID)
        if amountBefore >= amount and PID != receiver:
            transaction_log.append(Transaction(PID, receiver, amount))
            print("SUCCESS")
            print("Balance before: $"+str(amountBefore))
            print("Balance after: $"+str(amountBefore-amount))
        else:
            # Run Paxos
            sendPrepare()

    elif dataList[0] == 'balance':
        if len(dataList) == 1:
            dataList.append(str(PID))
        print("Balance: $"+str(getBalance(int(dataList[1]))))

def listenToClient(pid, conn):
    with conn:
        while True:
            try:
                data = conn.recv(BUFFER_SIZE).decode('utf-8')
                if not data:
                    break
                processMessage(pid, data)
            except socket.error:
                print ("Socket error in receiving message")
                break
        if conn in CLIENTS:
            CLIENTS.remove(conn)

def createServer(pid):
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        s.bind(('', PORT))
        print("socket binded to %s" %(PORT))
        s.listen(1)
        print("socket successfully created")
    except socket.error as err:
        print("socket creation failed with error %s" %(err))

    while True:
        conn, addr = s.accept()
        data = conn.recv(BUFFER_SIZE).decode('utf-8')
        if not data:
            break
        dataList = data.split(',')
        if dataList[0] == 'pid':
            clientConn[conn] = int(dataList[1])
            pidConn[int(dataList[1])] = conn
            print('Accepted connection from client ', dataList[1])
        CLIENTS.append(conn)
        print("#clients connected: ", len(CLIENTS))
        threading.Thread(target = listenToClient,args = (int(dataList[1]),conn,)).start()

def connectToClient(pid, ip, port):
    c_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    c_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    c_socket.connect((ip, port))
    c_socket.sendall(("pid,"+str(PID)).encode('utf-8'))
    CLIENTS.append(c_socket)
    clientConn[c_socket] = pid
    pidConn[pid] = c_socket
    threading.Thread(target = listenToClient,args = (pid,c_socket,)).start()

if __name__ == "__main__":
    # Reading the client configurations
    f = open(sys.argv[2], 'r')
    configList = f.readlines()
    config = configList[PID-1].strip('\n').split(',')
    if len(config) != 3:
        print("Incorrect configuration")
        sys.exit()
    IP = config[0]
    PORT = int(config[1])
    BALANCE = int(config[2])

    # Creating server to listen for connections
    server_thread = threading.Thread(target = createServer,args = (PID,))
    server_thread.start()

    # Connect to existing clients
    for i in range(1, PID):
        clientConfig = configList[i-1].strip('\n').split(',')
        connectToClient(i, clientConfig[0], int(clientConfig[1]))
    print("#clients connected: ", len(CLIENTS))
    print("Balance: $"+str(BALANCE))

    # Listen for client inputs
    chain = BlockChain(BALANCE)

    while True:
        message = input()
        processInput(message)


#!/usr/local/bin/python
# The previous line (which must be the first one to work) makes the script self-executing,
# assuming that the system has the Python interpreter at path /usr/local/bin/python.

# This wants to be run in Python 3.

# Reference Pre-Processor
# Given: A reference file
#        An integer horizon, which tells us how far to look ahead if we do look ahead
# Return: A suffix array (either with short strings, or with the BWT)
#
# by Moyaccercchi, 17th of June 2015
#
# version 11:
# added hash support

import collections

fo = open('in.txt', 'r')

referencepath = fo.readline().strip()
horizon = int(fo.readline().strip())
useBWT = int(fo.readline().strip())

fo.close()

def loadTextFromFile(filepath):

    fhandle = open(filepath, 'r')

    lastread = fhandle.readline().strip()

    if (filepath[-6:].lower() == '.fasta'):

        # We ignore the first read, as it is a comment line.
        res = ''
        origres = lastread

        while (lastread != ""):
            lastread = fhandle.readline().strip()
            res += lastread
            origres += '\n' + lastread

    else:

        # STPU files
        res = lastread
        origres = lastread

    fhandle.close()

    return (res, origres)

# we flatten a given reference string by choosing the first option from each bubble;
# however, we could instead also choose the second (or in fact a random) option,
# or ideally put N characters to indicate that there is a character,
# but it is unknown, so that the later steps do NOT count it as an error
# to align against it either way
# (of course, completely ideal would be to keep track of the graph itself,
# or in this case the bubbles, and use ALL the information later on in the chain,
# not ever discarding any of it)

# also, we are now having nested graphs, so we need to check if we are
# in fact encountering a nested one here, and do this recursively, which is
# why this is a function now rather than just a while iteration

def flattenreference(flat_reference):

    # find the first "(", so e.g. GA(GC(T|C)A|CAGG)T(G|A)C
    #                               ^

    i = flat_reference.find("(");

    if i > -1:

        # find what is before the first "(", so e.g. GA(GC(T|C)A|CAGG)T(G|A)C
        #                                            ^^

        flat_before = flat_reference[:i];

        # we would like to find what is the first alternative, so e.g.
        # GA(GC(T|C)A|CAGG)T(G|A)C
        #    ^^^^^^^^
        #
        # to do so, in version 8 of this script, we simply used
        # flat_first = flat_reference[i+1:flat_reference.find("|")];
        #
        # however, this is not sufficient anymore, as it would find the very first "|",
        # and in our example give us GA(GC(T|C)A|CAGG)T(G|A)C (where we are missing all ° chars)
        #                               ^^^^°°°°
        # so instead we first make a recursive call to this function itself, which will give us:
        # GC(T|C)A|CAGG)T(G|A)C => GCTA|CAGG)T(G|A)C => GCTA|CAGG)TGC

        flatter_reference = flattenreference(flat_reference[i+1:]);

        # flat_first is now the the start of the returned value, e.g.
        #
        # GCTA|CAGG)TGC = GA(GC(T|C)A|CAGG)T(G|A)C
        # ^^^^               ^^ ^   ^

        flat_first = flatter_reference[:flatter_reference.find("|")];

        # find what is after the ")" closing the first "(", so e.g.
        # GCTA|CAGG)TGC = GA(GC(T|C)A|CAGG)T(G|A)C
        #           ^^^                    ^ ^   ^

        flat_after = flatter_reference[flatter_reference.find(")")+1:];

        # take all the parts together to flatten one layer, so e.g.
        # (left is what is actually happening, right is what is conceptually happening)
        #
        # GA(GC(T|C)A|CAGG)T(G|A)C + GCTA|CAGG)TGC = GA(GC(T|C)A|CAGG)T(G|A)C
        # ^^                         ^^^^      ^^^   ^^ ^^ ^   ^      ^ ^   ^
        # GA                         GCTA      TGC = GA GC T   A      T G   C
        # =>                                         =>
        # GAGCTATGC                                = GAGCTATGC

        flat_reference = flat_before + flat_first + flat_after;

    return flat_reference;

def getbubblemidlen(reference, offset):

    # in v9, snipmidlen is more involved (it is the length of any path through the graph,
    # so we go for the first option always and keep track of that)

    curpos = offset + 1;
    nestedness = 0;

    snipmidlen = 0;

    addtomidlen = [];

    # A(GT(CT|TA)G|CAA(A|G)A)T
    #   ^^ ^^ xx ^ xxx x x x
    #
    # 0 11 22 22 1 111 2 2 1 0 nestedness
    #
    #   11 11 11 1 000 0 0 0   addtomidlen[0 = nestedness-1]
    #      11 00       1 0     addtomidlen[1 = nestedness-1]
    #   11 11 00 1 000 0 0 0   prod(addtomidlen) = addtomidlen[0] * .. * addtomidlen[nestedness - 1]

    while (nestedness > 0) or (reference[curpos] != '|'):

        if (reference[curpos] == '('):

            nestedness += 1
            addtomidlen.append(1)

        elif (reference[curpos] == ')'):

            nestedness -= 1
            del addtomidlen[-1]

        elif (reference[curpos] == '|'):

            addtomidlen[nestedness-1] = 0

        else:

            doadd = True;

            for atm in addtomidlen:
                if atm == 0:
                    doadd = False
                    break

            if doadd:
                snipmidlen += 1

        curpos += 1

    return snipmidlen

def getbubbleend(reference, offset):

    lr = len(reference)

    # in v9, snipend also is more involved (it is the end of the bubble,
    # but as bubbles can now be nested, it actually needs to keep track of
    # nesting levels while searching for the ending ")" that belongs to the opening "(",
    # where previously it could just be assumed that the first ")" belongs to the first "(")
    snipstarts = 1
    curloc = offset + 1

    while snipstarts > 0:
        nextstart = reference.find("(", curloc)
        nextend = reference.find(")", curloc)

        if nextstart == -1:
            nextstart = lr
        if nextstart < nextend:
            snipstarts += 1
            curloc = nextstart + 1
        else:
            snipstarts -= 1
            curloc = nextend + 1

    return curloc - 1

def generate_suffix_array(referencepath, horizon):

    orighorizon = horizon

    ret = collections.defaultdict(lambda: '')

    reference, out_reference = loadTextFromFile(referencepath)

    if (referencepath[-6:].lower() == '.fasta'):

        # string reference in FASTA

        for i in range(0, len(reference)):
            ret[reference[i:i+horizon]] += str(i) + ","

    else:

        if '(' in reference:

            # (multi-bubble, nested) graph reference in STPU

            # first, start by generating a changed out_reference in which we
            # replace bubbles by characters of equal length to keep a unified index system
            # (based on i_absolute)

            out_reference = flattenreference(out_reference);

            # actually do the preprocessing of the reference

            i = 0;
            i_absolute = 0;
            lr = len(reference)

            # keeps track of the sniplengths we have encountered, to be able to counteract them later
            sniplens = []
            snipmidlens = []

            # initiate the horizon by adding the length of the SNIP - 1 to it for each '(' before
            # the last character of the first read

            rf = reference[0:horizon-1]
            jumpedover = 0

            while '(' in rf:

                snipbeg = rf.find("(")

                # version 8: for non-nested bubbles only
                # snipmidlen = reference.find('|', snipbeg + jumpedover) - (1 + snipbeg + jumpedover)
                # snipend = reference.find(")", snipbeg + jumpedover) - jumpedover

                # version 9: for nested bubbles too
                snipmidlen = getbubblemidlen(reference, snipbeg + jumpedover)
                snipend = getbubbleend(reference, snipbeg + jumpedover) - jumpedover

                # this is not the real length of the snip, but rather the length - 1
                sniplen = snipend - snipbeg

                sniplens.append(sniplen)
                snipmidlens.append(snipmidlen)

                if (snipend+1 > len(rf)):
                    rf = reference[snipend+horizon-len(rf):horizon+sniplen-1]
                else:
                    rf = rf[snipend+1:] + reference[horizon-1:horizon+sniplen-1]

                jumpedover += snipend+1

                horizon += sniplen

            # to get virtual reads all the way to the end, go for:
            # while i < lr:

            # to only get virtual reads of the full specified length, go for:
            while i < lr-horizon+1:

                # imagine the following reads as [ ] and the following reference:
                #
                # ...AGAGA(T|C)AGAGA...
                #    [   ] <- read AGAGA without graph
                #
                # ...AGAGA(T|C)AGAGA...
                #     [   ] <- read GAGA(, find '(' in last position, expand by 4
                #     [       ] <- read GAGAT and GAGAC
                #
                # ...AGAGA(T|C)AGAGA...
                #      [       ] <- read AGATA and AGACA
                #
                # ...AGAGA(T|C)AGAGA...
                #       [       ] <- read GATAG and GACAG
                #
                # ...AGAGA(T|C)AGAGA...
                #        [       ] <- read ATAGA and ACAGA
                #
                # ...AGAGA(T|C)AGAGA...
                #         [       ] <- read TAGAG and CAGAG
                #
                # instead of i+1, do i+5 (or i+4 and then i+1 due to the loop)
                # also, unexpand
                #
                # ...AGAGA(T|C)AGAGA...
                #              [   ] <- read AGAGA, unexpanded

                # now let us have a look at reads containing several two-SNIPs:
                #
                # ...AGAGA(T|C)A(T|C)GAGA...
                #    [   ] <- read AGAGA without graph
                #
                # ...AGAGA(T|C)A(T|C)GAGA...
                #     [   ] <- read GAGA(, find '(' in last position, expand by 4
                #     [       ] <- read GAGAT and GAGAC
                #
                # ...AGAGA(T|C)A(T|C)GAGA...
                #      [       ] <- read AGATA and AGACA
                #
                # ...AGAGA(T|C)A(T|C)GAGA...
                #       [       ] <- read GATA( and GACA(, find '(' in last position, expand by 4 more
                #       [           ] <- read GATAT, GATAC, GACAT and GACAC
                #
                # ...AGAGA(T|C)A(T|C)GAGA...
                #        [           ] <- read ATATG, ATACG, ACATG and ACACG
                #
                # ...AGAGA(T|C)A(T|C)GAGA...
                #         [           ] <- read TATGA, TACGA, CATGA and CACGA
                #
                # instead of i+1, do i+5 (or i+4 and then i+1 due to the loop)
                # also, unexpand
                #
                # ...AGAGA(T|C)A(T|C)GAGA...
                #              [       ] <- read ATGAG and ACGAG

                # And what about nested bubbles?
                #
                # ...AGAGA(T(T|C)|CG)AGAGA...
                #    [   ] <- read AGAGA without graph
                #
                # ...AGAGA(T(T|C)|CG)AGAGA...
                #     [   ] <- read GAGA(, find '(' in last position, expand until the ')' that closes it
                #     [             ] <- read GAGATT, GAGATC and GAGACG,
                #                        reduce to GAGAT, GAGAT and GAGAC,
                #                        remove multiples to find GAGAT and GAGAC
                #
                # ...AGAGA(T(T|C)|CG)AGAGA...
                #      [             ] <- read AGATTA, AGATCA and AGACGA,
                #                         reduce to AGATT, AGATC and AGACG, remove nothing
                #
                # ...AGAGA(T(T|C)|CG)AGAGA...
                #       [             ] <- read GATTAG, GATCAG and GACGAG,
                #                          reduce to GATTA, GATCA and GACGA, remove nothing
                #
                # ...AGAGA(T(T|C)|CG)AGAGA...
                #        [             ] <- read ATTAGA, ATCAGA and ACGAGA,
                #                           reduce to ATTAG, ATCAG and ACGAG, remove nothing
                #
                # ...AGAGA(T(T|C)|CG)AGAGA...
                #         [             ] <- read TTAGAG, TCAGAG and CGAGAG,
                #                            reduce to TTAGA, TCAGA and CGAGA, remove nothing
                #
                # find '(' in first read pos => jump over entire read,
                # fast-adding all reduced variants, that is
                # TTAGAG, TCAGAG and CGAGAG were given
                # TTAGA , TCAGA  and CGAGA  were already added
                #  TAGAG,  CAGAG and  GAGAG are added in the fast-adding round
                #
                #
                # ...AGAGA(T(T|C)|CG)AGAGA...
                #                    [   ] <- read AGAGA

                rf = reference[i:i+horizon]

                if rf[len(rf)-1] == '(':

                    # the last character of the read, which is where we encountered the '(' character
                    snipbeg = i+horizon-1

                    # in version 8, it was enough to do the following:
                    # snipmidlen = reference.find("|", snipbeg) - (1 + snipbeg);
                    # snipend = reference.find(")", snipbeg)

                    # however, now we have to also consider nested bubbles, so instead we do:
                    snipmidlen = getbubblemidlen(reference, snipbeg)
                    snipend = getbubbleend(reference, snipbeg)

                    # again, this is the length of the snip - 1
                    sniplen = snipend - snipbeg
                    sniplens.append(sniplen)
                    snipmidlens.append(snipmidlen)

                    # (analysis) print ("\nnext:")
                    # (analysis) print ("snipbeg:")
                    # (analysis) print (snipbeg)
                    # (analysis) print ("snipmidlen:")
                    # (analysis) print (snipmidlen)
                    # (analysis) print ("snipend:")
                    # (analysis) print (snipend)
                    # (analysis) print ("sniplen:")
                    # (analysis) print (sniplen)
                    # (analysis) print ("rf old:")
                    # (analysis) print (rf)
                    # (analysis) print ("horizon old:")
                    # (analysis) print (horizon)

                    horizon += sniplen
                    rf = reference[i:i+horizon]

                    # (analysis) print ("rf new:")
                    # (analysis) print (rf)
                    # (analysis) print ("horizon new:")
                    # (analysis) print (horizon)

                if '(' in rf:

                    # this virtual read contains at least one SNIP
                    rfs = [rf]

                    while len(rfs) > 0:

                        rfsnew = []

                        for rfl in rfs:

                            # in version 8 it was enough to do
                            # snipbeg = rfl.find("(");

                            # however, we now have to consider nested bubbles as well

                            # we don't go for the first '(' that we encounter,
                            # but instead for the first '(' that belongs to an inner bubble,
                            # where an "inner bubble" is any bubble that contains no nested bubbles
                            #
                            # e.g. AG(CTAG|G(C(G|T)T|TAA))C(G|T), which represents
                            #      AG CTAG                C G
                            #      AG      G C G   T      C G
                            #      AG      G C   T T      C G
                            #      AG      G         TAA  C G
                            #      AG CTAG                C   T
                            #      AG      G C G   T      C   T
                            #      AG      G C   T T      C   T
                            #      AG      G         TAA  C   T
                            #
                            # in this example, there are two inner bubbles:
                            #      AG(CTAG|G(C(G|T)T|TAA))C(G|T)
                            #                 °°°°°        °°°°°
                            #
                            # More formally, a bubble is an inner bubble iff
                            # after its '(' a ')' follows sooner than another '('.

                            # (analysis) print('rfl', rfl)

                            snipbeg = rfl.find("(");

                            isInnerBubble = False;

                            while not isInnerBubble:

                                if snipbeg < 0:
                                    # no bubble? that's good enough!
                                    isInnerBubble = True
                                else:
                                    snipnextbeg = rfl.find("(", snipbeg+1)
                                    if snipnextbeg < 0:
                                        # no bubble start follows? then this is an inner one!
                                        isInnerBubble = True
                                    else:
                                        snipend = rfl.find(")", snipbeg)
                                        if snipend < snipnextbeg:
                                            # the end is nigh - nigher than the next opening, that is,
                                            # so this is an inner one!
                                            isInnerBubble = True
                                        else:
                                            # another round? then we should update snipbeg
                                            snipbeg = snipnextbeg

                            if snipbeg > -1:

                                snipmidlen = rfl.find("|", snipbeg) - (1 + snipbeg);
                                snipend = rfl.find(")", snipbeg);

                                # we add the virtual read with the first choice and with the
                                # second choice for the first SNIP in it, e.g.
                                # AG(C|T)G(C|T)A -> AGCG(C|T)A, AGTG(C|T)A

                                beforesnip = rfl[:snipbeg]
                                aftersnip = rfl[snipend+1:len(rfl)]

                                snipstart = snipbeg+1;
                                while (snipstart < snipend):
                                    rfsnew.append(beforesnip + rfl[snipstart:snipstart+snipmidlen] + aftersnip)
                                    snipstart += snipmidlen + 1

                            else:

                                # all SNIPs have been replaced by choices from them
                                if not ("," + str(i_absolute) + "," in "," + ret[rfl[:orighorizon]]):
                                    ret[rfl[:orighorizon]] += str(i_absolute) + ","

                                # position [1]:
                                # if a SNIP is in the first position, then add all possible reads
                                # for the following positions as well, all the way through the SNIP
                                if rf[0] == '(':
                                    # we start at 1, as we already added 0:orighorizon+0 two lines above
                                    for lc in range(1, snipmidlens[0]):
                                        # we here check if the location has already been returned for this
                                        # exact virtual read, which seems ridiculous, but actually makes sense,
                                        # considering the following example:
                                        # A(ACCCCCG|GCCCCCA)G
                                        # if we have a virtual read length of 5 (orighorizon == 5),
                                        # then when we get to 'CCCCC' in the second path, it has already
                                        # been added for the first path - and in exactly the same absolute location!
                                        if not ("," + str(i_absolute + lc) + "," in "," + ret[rfl[lc:orighorizon+lc]]):
                                            ret[rfl[lc:orighorizon+lc]] += str(i_absolute + lc) + ","

                        rfs = rfsnew[:]

                else:

                    # this virtual read contains no SNIP, so just use it purely
                    ret[rf[:orighorizon]] += str(i_absolute) + ","

                if rf[0] == '(':
                    sniplen = sniplens.pop(0)
                    snipmidlen = snipmidlens.pop(0)
                    if (sniplen < 0):
                        sniplen = 0
                    horizon -= sniplen

                    i += sniplen + 1
                    i_absolute += snipmidlen

                else:

                    i += 1
                    i_absolute += 1

        else:

            # string reference in STPU

            for i in range(0, len(reference)):
                ret[reference[i:i+horizon]] += str(i) + ","

    fref = open('outref.txt', 'w')

    fref.write(out_reference)

    fref.close()

    return ret

def dicttoadjacency(ourdict):

    ret = []

    for fromnode, tonode in ourdict.items():
        ret.append(fromnode + ' -> ' + tonode[:-1])

    return '\n'.join(sorted(ret))

def getAllCyclicStrings(instr):

    ret = []

    for i in range(0, len(instr)):
        ret.append(instr[i:len(instr)] + instr[0:i])

    return ret

if (useBWT):

    reference, out_reference = loadTextFromFile(referencepath)

    allCyclicStrings = getAllCyclicStrings(reference + '$')

    allCyclicStringsandIndicies = []

    for i in range(0, len(allCyclicStrings)):
        allCyclicStringsandIndicies.append([allCyclicStrings[i], i])

    allCyclicStringsandIndicies.sort()

    # Burrows-Wheeler Transform

    bwt = ''

    for i in range(0, len(allCyclicStringsandIndicies)):
        bwt += allCyclicStringsandIndicies[i][0][len(allCyclicStringsandIndicies[i][0]) - 1]

    fo = open('out.txt', 'w')

    fo.write(bwt)

    fo.close()

    # Suffix Array

    sa = ''

    for i in range(0, len(allCyclicStringsandIndicies)):
        sa += str(allCyclicStringsandIndicies[i][1]) + '\n'

    fo = open('sa.txt', 'w')

    fo.write(sa)

    fo.close()

else:

    res = dicttoadjacency(generate_suffix_array(referencepath, horizon))

    fo = open('out.txt', 'w')

    fo.write(res)

    fo.close()


import pandas as pd

from regla import RuleReportColumn, RuleReporter, RuleReportType, RuleContext, RuleOption
from hazel import GoogleAdsReporter as HazelReporter
from typing import Dict
from .google_ads_context import GoogleAdsContext, GoogleAdsOption, add_report_time

class GoogleAdsReporter(RuleReporter):
  @property
  def entity_granularity(self) -> str:
    if self.reportType is RuleReportType.campaign:
      return 'campaign'
    elif self.reportType is RuleReportType.adGroup:
      return 'ad_group'
    else:
      raise ValueError('Unsupported google ads report type', self.reportType)

  @property
  def rule_column_map(self) -> Dict[RuleReportColumn, str]:
    return {
      RuleReportColumn.campaign_id: 'campaign_id',
      RuleReportColumn.impressions: 'metrics_impressions',
      RuleReportColumn.clicks: 'metrics_clicks',
      RuleReportColumn.conversions: 'metrics_conversions',
    }

  def time_granularity(self, granularity: str) -> str:
    return granularity.lower()

  def _map_rule_columns(self, report: pd.DataFrame):
    report.rename(lambda s: s.replace('#', '_'), axis='columns', inplace=True)
    report[RuleReportColumn.spend.value] = report['metrics_cost_micros'].apply(lambda c: c / 1000000 if not pd.isna(c) else c) if 'metrics_cost_micros' in report.columns else None
    add_report_time(
      report=report,
      time_column=RuleReportColumn.date.value
    )
    assert report.customer_currency_code.unique() == ['USD'], 'Only accounts using USD currency are currently supported'

    super()._map_rule_columns(report=report)
    if self.context[RuleContext.rule_options.value][GoogleAdsOption.use_optimized_conversions.value] and 'campaign_selective_optimization_conversion_actions' in report.columns:
      location = report.loc[report.campaign_selective_optimization_conversion_actions.notna()]
      report.loc[location.index, RuleReportColumn.conversions.value] = location.selected_conversions

  def _getRawReport(self, startDate, endDate, granularity, api, campaign, adGroupIDs):
    reporter = HazelReporter(api=api)
    report = reporter.get_performance_report(
      start_date=startDate,
      end_date=endDate,
      entity_granularity=self.entity_granularity,
      time_granularity=self.time_granularity(granularity=granularity),
      entity_ids=[campaign[GoogleAdsContext.campaign_id.value]]
    )
    if self.context[RuleContext.rule_options.value][GoogleAdsOption.use_optimized_conversions.value]:
      report = reporter.add_selected_conversions(
        report=report,
        start_date=startDate,
        end_date=endDate,
        entity_granularity=self.entity_granularity,
        time_granularity=self.time_granularity(granularity=granularity)
      )
    return report

  def _filterByLastActionDate(self, report, historyCollection):
    if not self.context[RuleContext.rule_options.value][RuleOption.dynamic_window.value]:
      return
    return super()._filterByLastActionDate(
      report=report,
      historyCollection=historyCollection
    )


#!/usr/bin/python
import driver
import time

from webapp.createedit.tasks import set_shipment_info, set_order_info
from webapp.login.tasks import login

driver = driver.get_driver()

destination_address = {
    "first_name": "James",
    "last_name": "Livermont",
    "company_name": "Scurri",
    "email": "jlivermont@scurri.com",
    "country": "Ireland",
    "postcode": "Y35 1923",
    "phone": "091 291 1901",
    "address1": "The Bulling",
    "address2": "Innovation House",
    "county": "WEXFORD",
    "city": "Wexford"
}

login(driver, "ezlabel", "ezlabel")
set_shipment_info(driver, **destination_address)
set_order_info(driver, order_number="1232932")

#time.sleep(5)

#driver.quit()


'''
6 - Faça um algoritmo que leia um nº inteiro e mostre uma mensagem indicando se este número é par ou ímpar,
e se é positivo ou negativo
'''

def checar_sinal(num):
    if num >= 0:
        print("Positivo")
    else:
        print("Negativo")

def checar_paridade(num):
    if num%2 == 0:
        print("Par")
    else:
        print("Ímpar")

temp=int(input("Digite um numero para verificação: "))

checar_sinal(temp)
checar_paridade(temp)


from conans import ConanFile, CMake, tools
import os
import platform

class BlackmagicConan(ConanFile):
    name = 'blackmagic'

    source_version = '12.0'
    package_version = '0'
    version = '%s-%s' % (source_version, package_version)

    build_requires = (
        'llvm/5.0.2-1@vuo/stable',
        'macos-sdk/11.0-0@vuo/stable',
    )
    settings = 'os', 'compiler', 'build_type', 'arch'
    url = 'https://www.blackmagicdesign.com/support/'
    description = 'Video capture and playback'
    generators = 'cmake'
    build_dir = '_build'
    exports_sources = 'CMakeLists.txt'

    def requirements(self):
        if platform.system() == 'Linux':
            self.requires('patchelf/0.10pre-1@vuo/stable')

        if platform.system() == 'Darwin':
            pf = 'Mac'
            self.libext = 'dylib'
        elif platform.system() == 'Linux':
            pf = 'Linux'
            self.libext = 'so'
        else:
            raise Exception('Unknown platform "%s"' % platform.system())

        self.platformDir = 'Blackmagic DeckLink SDK 12.0/%s' % pf

    def source(self):
        tools.mkdir('include')
        tools.mkdir('lib')
        tools.get('https://sw.blackmagicdesign.com/DeckLink/v12.0/Blackmagic_DeckLink_SDK_12.0.zip?Key-Pair-Id=APKAJTKA3ZJMJRQITVEA&Signature=NNP8rztcVRymb8dLddJqX3J4+SZ4d0i4FH1G9tt58W2BnCnKH6OLxxKCZByNH6kG7KC2V+7Sdl2yCFVonz1673qFBjnwwFNXboEz/8yn7s/mMa6t9GFdw7+Fjc9n3VG1inT0aPpfPc92A/XnAMlbzHeAmrnF6jdmQAo/++TpIZ4aRHI7ypqcRJ77hPopi32HzrjTwxzxVqcU4nKPZR1ErFtPIbUenozZLM6MdcqkDNElvqyXIwLfczNgVhvwuZutF+3Z2Vwq4K1etTyYPprWeuU3LBsH6SbO3QLUr4/pp/s/MzUIOWB5Kc2BN5F/jllM1Tw5JghHQ7Bd6TWefl/2Kw==&Expires=1611438177',
            sha256='89b34b05ae1fbe209129de767606300583bcce4266d9476293a43fe2e881fcf5',
            filename='sdk.zip')

        self.run('mv "%s/include"/*.h include' % self.platformDir)
        # Exclude older header versions.
        self.run('rm include/*_v[0-9]*.h')

    def build(self):
        cmake = CMake(self)
        cmake.definitions['CONAN_DISABLE_CHECK_COMPILER'] = True
        cmake.definitions['CMAKE_BUILD_TYPE'] = 'Release'
        cmake.definitions['CMAKE_C_COMPILER'] = '%s/bin/clang' % self.deps_cpp_info['llvm'].rootpath
        cmake.definitions['CMAKE_C_FLAGS'] = '-Oz'
        cmake.definitions['CMAKE_SKIP_BUILD_RPATH'] = True
        cmake.definitions['CMAKE_BUILD_WITH_INSTALL_NAME_DIR'] = True
        cmake.definitions['CMAKE_INSTALL_NAME_DIR'] = '@rpath'
        cmake.definitions['CMAKE_OSX_ARCHITECTURES'] = 'x86_64;arm64'
        cmake.definitions['CMAKE_OSX_DEPLOYMENT_TARGET'] = '10.11'
        cmake.definitions['CMAKE_OSX_SYSROOT'] = self.deps_cpp_info['macos-sdk'].rootpath
        cmake.definitions['BLACKMAGIC_SDK_DIR'] = self.platformDir

        tools.mkdir(self.build_dir)
        with tools.chdir(self.build_dir):
            cmake.configure(source_dir='..', build_dir='.', args=['--no-warn-unused-cli'])
            cmake.build()
            self.run('install_name_tool -change @rpath/libc++.dylib /usr/lib/libc++.1.dylib lib/libblackmagic.dylib')

    def package(self):
        self.copy('*.h', src='include', dst='include')
        self.copy('libblackmagic.%s' % self.libext, src='%s/lib' % self.build_dir, dst='lib')

    def package_info(self):
        self.cpp_info.libs = ['blackmagic']


import glob

# Read in cars and notcars filenames
# Big set
#image_filenames_all = glob.glob('*/*/*.png')
#training_image_type = 'png'
# Small set
image_filenames_all = glob.glob('course/*/*/*.jpeg')
training_image_type = 'jpeg'

cars = []
notcars = []
for image_filename in image_filenames_all:
    if 'non-vehicles' in image_filename:
        notcars.append(image_filename)
    elif 'vehicles' in image_filename:
        cars.append(image_filename)

# Equalize the samples if different, or limit if necessary.
sample_size = max(len(cars), len(notcars))
cars = cars[0:sample_size]
notcars = notcars[0:sample_size]

print('Number of samples used: ', sample_size)

# Classifier parameters
# Parameters of the features selected.
# HOG
color_space = 'YUV' #'YUV' #  Can be RGB, HSV, LUV, HLS, YUV, YCrCb
orient          = 11 #9 # HOG orientations
pix_per_cell    = 16 #8  # HOG pixels per      (pix_per_cell, pix_per_cell)
cell_per_block  = 2     # HOG cells per block (cell_per_block, cell_per_block)
hog_channel     = 'ALL' # Can be 0, 1, 2, or "ALL"
# Spatial binning
spatial_size = (16, 16) # Spatial binning dimensions
# Color histogram
hist_bins = 16    # Number of histogram bins

spatial_feat = False # Spatial features on or off
hist_feat = True # Histogram features on or off
hog_feat = True # HOG features on or off

video_image_size = (720, 1280, 3)

# Search parameters
combinations = {}

combinations['color'] = [ (0, 0,   255),
                          (0, 255,  0),
                          (255, 255, 0)]

combinations['window_size'] = [ (60, 60),
                                (140, 140),
                                (230, 230)]

combinations['overlap'] = [ (0.0, )*2,
                            (0.4, )*2,
                            (0.5, 0.7)]

combinations['y_limit'] = [(360, 550),
                           (350, 650),
                           (380, 720)]

combinations['num_window'] = list(range(0, len(combinations['color']) ))

# Heatmap
heatmap_threshold = 2
max_heat = 6

image_files = ['test_images/test1.jpg',
               'test_images/test2.jpg',
               'test_images/test3.jpg',
               'test_images/test4.jpg',
               'test_images/test5.jpg',
               'test_images/test6.jpg']

if __name__ == '__main__':

    # If this module is called directly, train the classifier and check the
    # features selected.

    import classifier as cs
    import pickle
    import search_and_classify as sc

    print('Training SVM classifier')
    cs.check_datasets()

    svc, X_scaler = cs.train_classifier(   cars,
                                        notcars,
                                        color_space = color_space,
                                        spatial_size = spatial_size,
                                        hist_bins = hist_bins,
                                        orient = orient,
                                        pix_per_cell = pix_per_cell,
                                        cell_per_block = cell_per_block,
                                        hog_channel = hog_channel,
                                        spatial_feat = spatial_feat,
                                        hist_feat = hist_feat,
                                        hog_feat = hog_feat)

    pickle.dump( (svc, X_scaler), open( "trained_svc.p", "wb" ) )

    cs.test_hog_features()

    sc.plot_grid()
    sc.test_search_and_classify()


def partition(no ,l ,r):
    p = no[l]
    i, j= 0, r
    while True:
        while i <=r :
             if not no[i] <= p:
                 break
             i += 1
        while j >= 0:
            if not no[j] >= p:
                break
            j -= 1
        if i>j :
            break
        #print(i,'=i and j=',j)
        no[i], no[j]= no[j],no[i]
    no[l], no[j] = no[j], no[l]
    return j

def qs(no, l, r):
    if l < r:
        s = partition(no, l, r)
        qs(no, l, s - 1)
        qs(no, s + 1, r)

no1 = list(map(int, input().split()))
qs(no1, 0, len(no1) - 1)
print(*no1)


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

# Python 对那些需要刻意隐藏在类内部的特性有自己的命名规范：由连续的两个下划线开头 (__)

class Duck():
	def __init__(self, input_name):
		self.__name = input_name
	@property
	def name(self):
		print('inside the getter')
		return self.__name
	@name.setter
	def name(self, input_name):
		print('inside the setter')
		self.__name = input_name

fowl = Duck('Howard')
print(fowl.name)
fowl.name = 'Donald'
print(fowl.name)

# 现在已经无法在外部访问 __name 特性
# print(fowl.__name) # 报错

# 这种命名规范并没有把特性变为私有，但 Python 确实将它的名字重整
print(fowl._Duck__name)
# 并没有得到 inside the getter, 成功绕过了 getter 方法


import requests
from requests.auth import HTTPBasicAuth
import json
from testrail import *
from unittest import TestCase
import unittest
import datetime
from precondition_test_case import A_PreconditionTestCase
from base_test_case import TestData
from base_test_case import BaseTestCase
import uuid

class Z_VideosTestCase(BaseTestCase):
    sensor1_mac = "5C:31:3E:43:BB:2E"

    def test_18_01_post_video_to_action(self):
        TestData.case_id = "617506"

        url = self.form_url_api("/v2/UserActions")
        timestamp = "2018-01-02T10:47:51"
        body = self.createBaseballAction(TestData.student1_userquipment1_id, TestData.student1_id, timestamp = timestamp, sensor_id=self.sensor1_mac)
        headers = {"Authorization":"Bearer "+TestData.student1_token, 'Content-Type':'application/json'}

        response = requests.post(url=url, data=body, headers=headers)
        self.print_response_status(response=response)
        TestData.action_id = response.json()['Id']
        self.assert_Equal(response.status_code, 201)

        url = self.form_url_api("/v2/useractions/"+TestData.action_id+"/videos")
        body = json.dumps({
  "OriginalFileName": "video.mp4",
  "InputS3Key": "123",
  "CameraSource": "iphone",
  "Metadata": [
    {
      "Name": "string",
      "Value": "string"
    }
  ]
})
        headers = {"Authorization":"Bearer "+TestData.student1_token, 'Content-Type':'application/json'}
        response = requests.post(url=url, data=body, headers=headers)
        self.print_response_status(response=response)
        self.assert_Equal(response.status_code, 201)

    def test_18_02_get_user_sensor_by_coach(self):
        TestData.case_id = "617507"
        url = "/v2/useractions/"+TestData.action_id+"/videos"
        response = self.get_from_API(url=url, token=TestData.student1_token)
        self.assert_Equal(response.status_code, 200)
        self.assert_Equal(response.json()["Count"], 1)

    @classmethod
    def tearDownClass(cls):
            url=cls.form_url_api(cls, "/v2/useractions/"+TestData.action_id)
            headers = {"Authorization":"Bearer "+TestData.student1_token, 'Content-Type':'application/json'}

            response = requests.delete(url=url, headers=headers)
            cls.print_response_status(cls, response=response)


"""Compile samples that are infeasible or difficult by svg compilation.
"""

import datetime
from pathlib import Path
from fontTools import fontBuilder
from fontTools import ttLib
from fontTools.colorLib import builder as colorBuilder
from fontTools.pens.ttGlyphPen import TTGlyphPen
from fontTools.ttLib.tables._g_l_y_f import Glyph
import sys
from typing import Any, Mapping, NamedTuple, Optional
from fontTools.ttLib.tables import otTables as ot
from nanoemoji.colors import css_colors, Color
from fontTools.misc.transform import Transform

_UPEM = 1000
_ASCENT = 950
_DESCENT = 250
_FAMILY = "More COLR v1 Samples"
_STYLE = "Regular"
_PALETTE = {}  # <3 mutable globals

class SampleGlyph(NamedTuple):
    glyph_name: str
    accessor: str
    advance: int
    glyph: Glyph
    colr: Optional[Mapping[str, Any]] = None

def _cpal(color_str):
    color = Color.fromstring(color_str).to_ufo_color()
    if color not in _PALETTE:
        _PALETTE[color] = len(_PALETTE)
    return _PALETTE[color]

def _sample_sweep():
    glyph_name = "sweep"

    pen = TTGlyphPen(None)
    pen.moveTo((100, 500))
    pen.qCurveTo((500, 1000), (900, 500))
    pen.qCurveTo((500, 0), (100, 500))
    pen.closePath()

    colr = {
        "Format": ot.PaintFormat.PaintGlyph,
        "Glyph": glyph_name,
        "Paint": {
            "Format": ot.PaintFormat.PaintSweepGradient,
            "ColorLine": {
                "ColorStop": [
                    (0.0, _cpal("red")),
                    (0.5, _cpal("yellow")),
                    (1.0, _cpal("red")),
                ]
            },
            "centerX": 500,
            "centerY": 500,
            "startAngle": 0,
            "endAngle": 360,
        },
    }

    return SampleGlyph(
        glyph_name=glyph_name, accessor="c", advance=_UPEM, glyph=pen.glyph(), colr=colr
    )

def _sample_colr_glyph():
    glyph_name = "transformed_sweep"
    # Paint the sweep shifted and rotated
    colr = {
        "Format": ot.PaintFormat.PaintTranslate,
        "dx": 250,
        "dy": 0,
        "Paint": {
            "Format": ot.PaintFormat.PaintRotate,
            "centerX": _UPEM / 2,
            "centerY": _UPEM / 2,
            "angle": 60,
            "Paint": {
                "Format": ot.PaintFormat.PaintColrGlyph,
                "Glyph": "sweep",
            },
        },
    }

    pen = TTGlyphPen(None)
    pen.moveTo((0, 0))
    pen.lineTo((_UPEM, _UPEM))
    pen.endPath()

    return SampleGlyph(
        glyph_name=glyph_name, accessor="t", advance=_UPEM, glyph=pen.glyph(), colr=colr
    )

def _sample_composite_colr_glyph():
    glyph_name = "composite_colr_glyph"
    # Scale down the sweep and use it to cut a hole in the sweep
    # Transforms combine f(g(x)); build up backwards
    t = Transform(dx=-500, dy=-500)  # move to origin
    t = Transform(xx=0.75, yy=0.75).transform(t)
    t = Transform(dx=500, dy=500).transform(t)
    t = tuple(t)

    colr = {
        "Format": ot.PaintFormat.PaintComposite,
        "CompositeMode": "SRC_OUT",
        "SourcePaint": {
            "Format": ot.PaintFormat.PaintColrGlyph,
            "Glyph": "sweep",
        },
        "BackdropPaint": {
            "Format": ot.PaintFormat.PaintTransform,
            "Paint": {
                "Format": ot.PaintFormat.PaintColrGlyph,
                "Glyph": "sweep",
            },
            "Transform": t,
        },
    }

    pen = TTGlyphPen(None)
    pen.moveTo((0, 0))
    pen.lineTo((_UPEM, _UPEM))
    pen.endPath()

    return SampleGlyph(
        glyph_name=glyph_name, accessor="o", advance=_UPEM, glyph=pen.glyph(), colr=colr
    )

def _gradient_stops_repeat(first_stop, second_stop, accessor_char):
    glyph_name = f"linear_repeat_{first_stop}_{second_stop}"

    pen = TTGlyphPen(None)
    pen.moveTo((100, 250))
    pen.lineTo((100, 950))
    pen.lineTo((900, 950))
    pen.lineTo((900, 250))
    pen.closePath()

    colr = {
        "Format": ot.PaintFormat.PaintGlyph,
        "Glyph": glyph_name,
        "Paint": {
            "Format": ot.PaintFormat.PaintLinearGradient,
            "ColorLine": {
                "ColorStop": [
                    (first_stop, _cpal("red")),
                    (second_stop, _cpal("blue")),
                ],
                "Extend": ot.ExtendMode.REPEAT,
            },
            "x0": 100,
            "y0": 250,
            "x1": 900,
            "y1": 250,
            "x2": 100,
            "y2": 300,
        },
    }

    return SampleGlyph(
        glyph_name=glyph_name,
        accessor=accessor_char,
        advance=_UPEM,
        glyph=pen.glyph(),
        colr=colr,
    )

def main():
    assert len(sys.argv) == 2
    build_dir = Path(sys.argv[1])
    build_dir.mkdir(exist_ok=True)

    out_file = (build_dir / _FAMILY.replace(" ", "")).with_suffix(".ttf")

    version = datetime.datetime.now().isoformat()
    names = {
        "familyName": _FAMILY,
        "styleName": _STYLE,
        "uniqueFontIdentifier": " ".join((_FAMILY, version)),
        "fullName": " ".join((_FAMILY, _STYLE)),
        "version": version,
        "psName": "-".join((_FAMILY.replace(" ", ""), _STYLE)),
    }

    glyphs = [
        SampleGlyph(glyph_name=".notdef", accessor="", advance=600, glyph=Glyph()),
        SampleGlyph(glyph_name=".null", accessor="", advance=0, glyph=Glyph()),
        _sample_sweep(),
        _sample_colr_glyph(),
        _sample_composite_colr_glyph(),
        _gradient_stops_repeat(0, 1, "p"),
        _gradient_stops_repeat(0.2, 0.8, "q"),
        _gradient_stops_repeat(0, 1.5, "r"),
        _gradient_stops_repeat(0.5, 1.5, "s"),
    ]

    fb = fontBuilder.FontBuilder(_UPEM)
    fb.setupGlyphOrder([g.glyph_name for g in glyphs])
    fb.setupCharacterMap(
        {ord(g.accessor): g.glyph_name for g in glyphs if len(g.accessor) == 1}
    )
    fb.setupGlyf({g.glyph_name: g.glyph for g in glyphs})
    fb.setupHorizontalMetrics({g.glyph_name: (_UPEM, g.glyph.xMin) for g in glyphs})
    fb.setupHorizontalHeader(ascent=_ASCENT, descent=-_DESCENT)
    fb.setupOS2(sTypoAscender=_ASCENT, usWinAscent=_ASCENT, usWinDescent=_DESCENT)
    fb.setupNameTable(names)
    fb.setupPost()

    fb.font["head"].xMin = 0
    fb.font["head"].yMin = -_DESCENT
    fb.font["head"].xMax = _UPEM
    fb.font["head"].yMax = _ASCENT

    fb.font["OS/2"].fsType = 0
    fb.font["OS/2"].version = 4
    fb.font["OS/2"].fsSelection |= 1 << 7

    fb.font["hhea"].advanceWidthMax = _UPEM

    fb.font["COLR"] = colorBuilder.buildCOLR(
        {g.glyph_name: g.colr for g in glyphs if g.colr}
    )
    fb.font["CPAL"] = colorBuilder.buildCPAL([list(_PALETTE)])

    fb.save(out_file)
    print(f"Wrote {out_file}")

if __name__ == "__main__":
    main()


'''
    Extract spatial and hog feature functions
'''
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import matplotlib.gridspec as gridspec
import glob
import numpy as np
import cv2
from skimage.feature import hog

# Convert RGB to specific color type
def convert_color(img, color_space):
    if color_space == 'HSV':
        feature_image = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
    elif color_space == 'LUV':
        feature_image = cv2.cvtColor(img, cv2.COLOR_RGB2LUV)
    elif color_space == 'HLS':
        feature_image = cv2.cvtColor(img, cv2.COLOR_RGB2HLS)
    elif color_space == 'YUV':
        feature_image = cv2.cvtColor(img, cv2.COLOR_RGB2YUV)
    elif color_space == 'YCrCb':
        feature_image = cv2.cvtColor(img, cv2.COLOR_RGB2YCrCb)
    else:
        feature_image = img
    return feature_image

def convertTo255(img):
    img_cvt = np.array([x*255 for x in img]).astype(np.uint8)
    return img_cvt

def ContrastEnhanceRGB(img):
    # Convert to YUV
    img_yuv = convert_color(img, 'YUV')
    img_yuv[:,:,0] = cv2.equalizeHist(img_yuv[:,:,0])

    # convert  back to RGB format
    img_en = cv2.cvtColor(img_yuv, cv2.COLOR_YUV2RGB)
    img_en = cv2.blur(img_en,(5,5))
    return img_en

# Define a function to return HOG features and visualization
# Note img is 1 channel only: could be a type of RGB, HLS, YCrCb....
def get_hog_features(img, orient, pix_per_cell, cell_per_block, hog_vis=False, feature_vec=True):
    # Call with two outputs if vis==True
    if hog_vis == True:
        features, hog_image = hog(img, orientations=orient,
                                  pixels_per_cell=(pix_per_cell, pix_per_cell),
                                  cells_per_block=(cell_per_block, cell_per_block),
                                  transform_sqrt=True,
                                  visualise=hog_vis, feature_vector=feature_vec)
        return features, hog_image
    # Otherwise call with one output
    else:
        features = hog(img, orientations=orient,
                       pixels_per_cell=(pix_per_cell, pix_per_cell),
                       cells_per_block=(cell_per_block, cell_per_block),
                       transform_sqrt=True,
                       visualise=hog_vis, feature_vector=feature_vec)
        return features

# Define a function to compute binned color features
def bin_spatial(img, size=(32, 32), bin_vis=False):
    # image resize
    img_resize = cv2.resize(img, size)
    # Create the feature vector
    features = img_resize.ravel()
    if bin_vis:
        return features, img_resize
    else:
        # Return the feature vector
        return features

# Define a function to compute color histogram features
def color_hist(img, nbins=32, bins_range=(0, 256)):
    # Compute the histogram of the color channels separately
    channel1_hist = np.histogram(img[:,:,0], bins=nbins, range=bins_range)
    channel2_hist = np.histogram(img[:,:,1], bins=nbins, range=bins_range)
    channel3_hist = np.histogram(img[:,:,2], bins=nbins, range=bins_range)
    # Concatenate the histograms into a single feature vector
    hist_features = np.concatenate((channel1_hist[0], channel2_hist[0], channel3_hist[0]))
    # Return the individual histograms, bin_centers and feature vector
    return hist_features

# Define a function to extract features from a list of images
# Have this function call bin_spatial() and color_hist()
def extract_features(imgs, color_space, spatial_size=(32, 32), hist_bins=32, orient=9,
                     pix_per_cell=8, cell_per_block=2, hog_channel=0, spatial_feat=True,
                     hist_feat=True, hog_feat=True):
    # Create a list to append feature vectors to
    features = []
    # Iterate through the list of images
    for file in imgs:
        file_features = []
        # Read in each one by one
        # Using matplotlib
        #   jpg will be 0-255
        #   png will be 0-1.0
        image = mpimg.imread(file)
        img = np.copy(image)

        # I am using 0-255 scale through out
        # convert png 0-1.0 scale to 0-255
        img = convertTo255(img)

        # enhance contrast
        #img = ContrastEnhanceRGB(img)

        if spatial_feat == True:
            # convert to specific color type for spatial features
            feature_image = convert_color(img, color_space['spatial'])
            spatial_features = bin_spatial(feature_image, size=spatial_size)
            file_features.append(spatial_features)
        if hist_feat == True:
            # convert to specific color type for histogram features
            feature_image = convert_color(img, color_space['hist'])
            hist_features = color_hist(feature_image, nbins=hist_bins)
            file_features.append(hist_features)
        if hog_feat == True:
            # convert to specific color type for hog features
            feature_image = convert_color(img, color_space['hog'])
            # Call get_hog_features() with vis=False, feature_vec=True
            if hog_channel == 'ALL':
                hog_features = []
                for channel in range(feature_image.shape[2]):
                    hog_features.append(get_hog_features(feature_image[:,:,channel],
                                        orient, pix_per_cell, cell_per_block,
                                        hog_vis=False, feature_vec=True))
                hog_features = np.ravel(hog_features)
            else:
                hog_features = get_hog_features(feature_image[:,:,hog_channel], orient,
                            pix_per_cell, cell_per_block, hog_vis=False, feature_vec=True)
            # Append the new feature vector to the features list
            file_features.append(hog_features)

        # combine all feature spaces
        features.append(np.concatenate(file_features))
    # Return list of feature vectors
    return features

def MultiplePlots(grid, images, titles):
    nrow = grid[0]
    ncol = grid[1]
    plt.figure(figsize=(ncol+1, nrow+1))
    gs = gridspec.GridSpec(nrow, ncol,
             wspace=0.2, hspace=0.2,
             top=1.-0.2/(nrow+1), bottom=0.2/(nrow+1),
             left=0.2/(ncol+1), right=1-0.2/(ncol+1))

    for i in range(nrow):
        for j in range(ncol):
            ax = plt.subplot(gs[i,j])
            img = images[i][j]
            title = titles[i][j]
            ax.imshow(img)
            ax.set_title(title)
            xticks = np.arange(0, img.shape[1], 10)
            yticks = np.arange(0, img.shape[0], 10)
            ax.set_xticks(xticks)
            ax.set_yticks(yticks)
            ax.set_xticklabels(xticks)
            ax.set_yticklabels(yticks)
    plt.show()

def test_bin_spatial(img, color_space, spatial_size):
    # convert to specific color type for spatial features
    feature_image = convert_color(img, color_space['spatial'])
    # extract features
    spatial_features_ch0, spatial_image_ch0 = bin_spatial(feature_image[:,:,0], size=spatial_size, bin_vis=True)
    spatial_features_ch1, spatial_image_ch1 = bin_spatial(feature_image[:,:,1], size=spatial_size, bin_vis=True)
    spatial_features_ch2, spatial_image_ch2 = bin_spatial(feature_image[:,:,2], size=spatial_size, bin_vis=True)
    # Grid plot
    grid = [3,2]
    images = [[feature_image[:,:,0],spatial_image_ch0],
              [feature_image[:,:,1],spatial_image_ch1],
              [feature_image[:,:,2],spatial_image_ch2]]
    titles = [[color_space['spatial'] + ' CH-0',color_space['spatial'] + ' CH-0 FEATURE'],
              [color_space['spatial'] + ' CH-1',color_space['spatial'] + ' CH-1 FEATURE'],
              [color_space['spatial'] + ' CH-2',color_space['spatial'] + ' CH-2 FEATURE']]
    MultiplePlots(grid, images, titles)

def test_hog_feature(img, color_space, orient, pix_per_cell, cell_per_block):
    # convert to specific color type for hog features
    feature_image = convert_color(img, color_space['hog'])
    # Call get_hog_features() with vis=False, feature_vec=True
    hog_features_ch0, hog_image_ch0 = get_hog_features(feature_image[:,:,0], orient, pix_per_cell, cell_per_block, hog_vis=True, feature_vec=True)
    hog_features_ch1, hog_image_ch1 = get_hog_features(feature_image[:,:,1], orient, pix_per_cell, cell_per_block, hog_vis=True, feature_vec=True)
    hog_features_ch2, hog_image_ch2 = get_hog_features(feature_image[:,:,2], orient, pix_per_cell, cell_per_block, hog_vis=True, feature_vec=True)

    # Grid Plot
    grid = [3,2]
    images = [[feature_image[:,:,0],hog_image_ch0],
              [feature_image[:,:,1],hog_image_ch1],
              [feature_image[:,:,2],hog_image_ch2]]

    titles = [[color_space['hog'] + ' CH-0',color_space['hog'] + ' CH-0 HOG'],
              [color_space['hog'] + ' CH-1',color_space['hog'] + ' CH-1 HOG'],
              [color_space['hog'] + ' CH-2',color_space['hog'] + ' CH-2 HOG']]

    MultiplePlots(grid, images, titles)

if __name__ == '__main__':
    feature_color = {'hog':'YCrCb','spatial':'HSV','hist':'YCrCb'} #HLS,HSV,LUV,RGB,YCrCb
    orient = 8                  # HOG orientations
    pix_per_cell = 8            # HOG pixels per cell
    cell_per_block = 2          # HOG cells per block
    hog_channel = "ALL"         # HOG channel, can be 0, 1, 2, or "ALL"
    spatial_size = (32, 32)     # Spatial binning dimensions
    hist_bins = 32              # Number of histogram bins
    spatial_feat = True         # Spatial features on or off
    hist_feat = True            # Histogram features on or off
    hog_feat = True             # HOG features on or off

    # read test images
    vehicle = glob.glob('../TrainTestData/Initial_GKU/vehicles/GTI_MiddleClose/image*.png')
    notvehicle = glob.glob('../TrainTestData/Initial_GKU/non_vehicles/Extras/extra*.png')
    # randomly pick one img
    vehicle_indx = 100#np.random.randint(1,len(vehicle))
    notvehicle_indx = 100#np.random.randint(1,len(notvehicle))
    vehicle_img = mpimg.imread(vehicle[vehicle_indx])
    notvehicle_img = mpimg.imread(notvehicle[notvehicle_indx])
    # I am using 255 scale through out the project
    vehicle_img = convertTo255(vehicle_img)
    notvehicle_img = convertTo255(notvehicle_img)
    plt.figure()
    plt.subplot(1,2,1)
    plt.imshow(vehicle_img)
    plt.title('Car')
    plt.subplot(1,2,2)
    plt.imshow(notvehicle_img)
    plt.title('Not car')
    plt.show()

    # show result: car
    test_hog_feature(vehicle_img, feature_color, orient, pix_per_cell, cell_per_block)
    test_bin_spatial(vehicle_img, feature_color, spatial_size)
    # show result: not car
    test_hog_feature(notvehicle_img, feature_color, orient, pix_per_cell, cell_per_block)
    test_bin_spatial(notvehicle_img, feature_color, spatial_size)