from oauthlib.oauth2.rfc6749.errors import InvalidGrantError, TokenExpiredError
from requests.exceptions import HTTPError

from flask import redirect, url_for, Response, session, logging

from flask_dance.consumer import OAuth2ConsumerBlueprint, oauth_error

from .auth import Auth

class OktaOAuth2ConsumerBlueprint(OAuth2ConsumerBlueprint):
    """
    This subclass makes access_denied and invalid_token oauth_errors accessible.
    """
    def __init__(self, app, name, import_name, **kwargs):
        super().__init__(name, import_name, **kwargs)
        self.access_denied = False
        self.invalid_token = False
        self.logger = logging.create_logger(app)

    @oauth_error.connect
    def auth_error(self, error, error_description, error_uri):
        self.logger.error(f"{error}: {error_description} {error_uri}")
        if error == 'access_denied':
            self.access_denied = True
        if error == 'invalid_token':
            self.invalid_token = True

class OktaOAuth(Auth):
    def __init__(self, app, additional_scopes=None):
        super().__init__(app)
        self.base_url = app.server.config['OKTA_BASE_URL']
        self.logger = logging.create_logger(app.server)
        self.okta = OktaOAuth2ConsumerBlueprint(
            app=app.server,
            name='okta',
            import_name=__name__,
            client_id=app.server.config['OKTA_OAUTH_CLIENT_ID'],
            client_secret=app.server.config['OKTA_OAUTH_CLIENT_SECRET'],
            base_url=self.base_url,
            token_url=f'{self.base_url}/token',
            authorization_url=f'{self.base_url}/authorize',
            scope=['openid', 'email', 'profile'] + (additional_scopes if additional_scopes else []),
            # by default a login redirects to the root page
        )
        app.server.register_blueprint(self.okta, url_prefix="/login")

    def is_authorized(self):
        if not self.okta.session.authorized:
            return False
        try:
            resp = self.okta.session.get(f'{self.base_url}/userinfo')
            resp.raise_for_status()
            if resp.ok:
                json_response = resp.json()
                session['user_name'] = json_response.get('name')
                session['user_nickname'] = json_response.get('nickname')
                session['user_given_name'] = json_response.get('given_name')
                session['user_middle_name'] = json_response.get('middle_name')
                session['user_family_name'] = json_response.get('family_name')
                session['user_profile'] = json_response.get('profile')
                session['user_zoneinfo'] = json_response.get('zoneinfo')
                session['user_updated_at'] = json_response.get('updated_at')
                session['user_locale'] = json_response.get('locale')
                session['user_email'] = json_response.get('email')
                session['user_email_verified'] = json_response.get('email_verified')
                session['user_address'] = json_response.get('address')
                session['user_preferred_username'] = json_response.get('preferred_username')
                session['user_phone_number'] = json_response.get('phone_number')
                return True
        except HTTPError:
            # resp.status_code == 401 Happens e.g. when a user token was revoked.
            return False
        except (InvalidGrantError, TokenExpiredError):
            # no clue which other errors require this behavior, add when required for now raise
            return False

    def login_request(self):
        if self.okta.access_denied:
            # by setting self.okta.access_denied to False we allow a new auth dance when a user manually reloads
            # if the user is assigned to this application in the meantime, a reload is sufficient to load the page.
            self.okta.access_denied = False
            return Response(status=403)
        return redirect(url_for("okta.login"))

    def auth_wrapper(self, f):
        def wrap(*args, **kwargs):
            if self.is_authorized():
                response = f(*args, **kwargs)
            else:
                return self.login_request()
            return response
        return wrap

    def index_auth_wrapper(self, original_index):
        def wrap(*args, **kwargs):
            if self.is_authorized():
                return original_index(*args, **kwargs)
            else:
                return self.login_request()
        return wrap


import os
import tarfile
from simpletransformers.classification import ClassificationModel
import re
from bs4 import BeautifulSoup

def unpack_model(model_name=''):
    tar = tarfile.open(f"{model_name}.tar.gz", "r:gz")
    tar.extractall()
    tar.close()

unpack_model('amazonforum-bert-categclass')

# define hyperparameter
train_args ={"reprocess_input_data": True,
            "overwrite_output_dir": True,
            "fp16":False,
            "num_train_epochs": 4}

# Create a ClassificationModel
model = ClassificationModel(
    "bert", "outputs/",
    num_labels=11,
    args=train_args,
    use_cuda=False
)

REPLACE_BY_SPACE_RE = re.compile('[/(){}\[\]\'\“\”\’\|@,;]')

def clean_text(text):
    """
        text: a string
        return: modified initial string
    """
    text = BeautifulSoup(text, "lxml").text # HTML decoding
    text = text.lower() # lowercase text
    text = REPLACE_BY_SPACE_RE.sub(' ', text) # replace REPLACE_BY_SPACE_RE symbols by space in text
    return text

class_list = ['Fulfillment By Amazon', 'Selling on Amazon',
    'Amazon Marketplace Web Service (MWS)',
    'Amazon Sponsored Products', 'Account Health', 'Global Selling',
    'Amazon Pay', 'Groups',
    'Health,Safety,Sustainability,Security & Compliance',
    'Login With Amazon', 'Amazon Custom']

def predict(post):
    predictions, raw_outputs = model.predict([clean_text(post)])
    return class_list[predictions[0]]


from tournamentClasses import *
import openpyxl

wb = openpyxl.load_workbook('Final NCAA Tournament Spreadsheet (For Data).xlsx', read_only=False, data_only=True)

data_ws = wb.get_sheet_by_name('Complete Stats')
tables_ws = wb.get_sheet_by_name('Individual data sets')

categories = {cat_name: num_types for cat_name, num_types in
              [(cell1[0].value, cell2[0].value) for cell1, cell2 in zip(tables_ws['A2:A9'], tables_ws['B2:B9'])]}
games = []
first_i_ls = []

for i in range(2, 6081):
    if not (isinstance(data_ws.cell(row=i, column=1).value, (int, type(None))) or not (i - 1 not in first_i_ls)):
        first_i_ls.append(i)
        games.append(GameData(i, data_ws))

del (categories[None])

for cat_name in categories.keys():
    print(cat_name)
    name_row = None
    row_i = 9
    pairs = []
    while not name_row:
        if tables_ws.cell(row=row_i, column=1).value == cat_name:
            name_row = row_i
        row_i += 1
    names = [str(tables_ws.cell(row=i, column=2).value) for i in
             range(name_row + 2, name_row + 2 + categories[cat_name])]
    for row_num, row_i in enumerate(range(name_row + 2, name_row + 2 + categories[cat_name])):
        cat1_name = names[row_num]
        for col_num, col_i in enumerate(range(4 + row_num, 3 + categories[cat_name])):
            cat2_name = names[col_num + row_num + 1]
            pairs.append(Pair(cat1_name, cat2_name))
    for game in games:
        if (game.team1[cat_name] != game.team2[cat_name]) and (game.team1[cat_name] in names) and (
                    game.team2[cat_name] in names):
            if Pair((game.team1[cat_name]), game.team2[cat_name]) in pairs:
                pairs[pairs.index(Pair((game.team1[cat_name]), game.team2[cat_name]))].update(game, cat_name)
            else:
                game.switch_sides()
                if Pair((game.team1[cat_name]), game.team2[cat_name]) in pairs:
                    pairs[pairs.index(Pair((game.team1[cat_name]), game.team2[cat_name]))].update(game, cat_name)
                else:
                    print("ERROR!!!!!!!!!! (womp womp)")
        elif not (game.team1[cat_name] in names) or not(
                    game.team2[cat_name] in names):
            print("Missing name")
            print(game.team1[cat_name], game.team2[cat_name])
    for row_num, row_i in enumerate(range(name_row + 2, name_row + 2 + categories[cat_name])):
        cat1_name = names[row_num]
        for col_num, col_i in enumerate(range(4 + row_num, 3 + categories[cat_name])):
            cat2_name = names[col_num + row_num + 1]
            pair = pairs[pairs.index(Pair(cat1_name, cat2_name))]
            tables_ws.cell(row=row_i, column=col_i).value = "{}-{}".format(pair.cat_a_wins,
                                                                           pair.total_games - pair.cat_a_wins)
    for pair in pairs:
        pair.switch_sides()
    for row_num, row_i in enumerate(range(name_row + 2, name_row + 2 + categories[cat_name])):
        cat1_name = names[row_num]
        for col_num, col_i in enumerate(range(3, 3 + row_num)):
            cat2_name = names[col_num]
            pair =  pairs[pairs.index(Pair(cat1_name, cat2_name))]
            tables_ws.cell(row=row_i, column=col_i).value = "{}-{}".format(pair.cat_a_wins,
                                                                           pair.total_games - pair.cat_a_wins)

    wb.save('Final NCAA Tournament Spreadsheet (For Output).xlsx')
    print('Done with {}'.format(cat_name))


import numpy
import math
import random
import MHDCoordSys
from scipy.spatial.transform import Rotation as R

verbose = 0
verbose = 0
def log(input):
    if verbose:
        print(input)

class MHDRectPM:
    def __init__(self,csIn,LxIn,LyIn,LzIn,JIn):
        #self.Lx = LxIn
        #self.Ly = LyIn
        #self.Lz = LzIn
        self.J = JIn
        if 1 == 1:
            self.a = LxIn
            self.b = LyIn
            self.h = LzIn
        else:
            self.a = LyIn
            self.b = LzIn
            self.h = LxIn
        self.muZero = 4.0 * 3.141592653 * 1.0e-7
        self.K = self.muZero * self.J / (4.0 * 3.1415926535)

        self.x = -.14
        self.y = 0.001
        self.z = -0.001

        self.dBxDx = .1
        self.dByDx = .1
        self.dBxDy = .1
        self.dByDy = .1

        self.sigmaBx = 0.293
        self.sigmaBy = 0.164
        self.sigmaBz = 0.207

        xOld = .1
        yOld = .1

        self.xMin = -1e5
        self.xMax = 1e5
        self.yMin = -1e5
        self.yMax = 1e5
        self.zMin = 0
        self.zMax = 1e5

        self.cs = csIn

        self.orientationType = 1

        self.verbose = 0

        self.posVect = numpy.array([[1.0],[2.0],[1.5]],numpy.double)

    def gammaP(self,g1,g2,g3,zP0):
        gOut = 1.0

        numerator = math.sqrt(g1 * g1 + g2 * g2 + (g3 - zP0) * (g3 - zP0)) - g2
        denominator = math.sqrt(g1 * g1 + g2 * g2 + (g3 - zP0) * (g3 - zP0)) + g2
        gOut = math.log(numerator / denominator)

        return gOut

    def phiP(self,p1,p2,p3,zP0):
        numerator = p1 * (p3 - zP0)
        denominator = p2 * math.sqrt(p1 * p1 + p2 * p2 + (p3 - zP0) * (p3 - zP0))
        if (denominator != 0.0):
            pOut = math.atan(numerator / denominator)
        else:
            pOut = 0.0
        return pOut

    def BxP(self,xP,yP,zP):
        BxPH = self.gammaP(self.a - xP, yP, zP, self.h) + self.gammaP(self.a - xP, self.b - yP, zP, self.h) - self.gammaP(xP, yP, zP, self.h) - self.gammaP(xP,self.b - yP,zP,self.h)
        BxP0 = self.gammaP(self.a - xP, yP, zP, 0.0) + self.gammaP(self.a - xP, self.b - yP, zP, 0.0) - self.gammaP(xP, yP, zP, 0.0) - self.gammaP(xP, self.b - yP, zP, 0.0)
        BxPout = - self.K / 2.0 * (BxPH - BxP0)
        return BxPout

    def ByP(self,xP,yP,zP):
        ByPH = self.gammaP(self.b - yP,xP,zP,self.h)
        ByPH+= self.gammaP(self.b - yP,self.a - xP,zP,self.h)
        ByPH-= self.gammaP(yP,xP,zP,self.h)
        ByPH-= self.gammaP(yP,self.a - xP,zP,self.h)

        ByP0 = self.gammaP(self.b - yP,xP,zP,0.0)
        ByP0+= self.gammaP(self.b - yP,self.a - xP,zP,0.0)
        ByP0-= self.gammaP(yP,xP,zP,0.0)
        ByP0-= self.gammaP(yP,self.a - xP,zP,0.0)

        ByPout = - self.K / 2.0 * (ByPH - ByP0)
        return ByPout

    def BzP(self,xP,yP,zP):
        BzPH = self.phiP(yP,self.a - xP,zP,self.h)
        BzPH+=self.phiP(self.b - yP,self.a - xP,zP,self.h)
        BzPH+=self.phiP(xP,self.b - yP,zP,self.h)
        BzPH+=self.phiP(self.a - xP,self.b - yP,zP,self.h)
        BzPH+=self.phiP(self.b - yP,xP,zP,self.h)
        BzPH+=self.phiP(yP,xP,zP,self.h)
        BzPH+=self.phiP(self.a - xP,yP,zP,self.h)
        BzPH+=self.phiP(xP,yP,zP,self.h)

        BzP0 = self.phiP(yP,self.a - xP,zP,0.0)
        BzP0+=self.phiP(self.b - yP,self.a - xP,zP,0.0)
        BzP0+=self.phiP(xP,self.b - yP,zP,0.0)
        BzP0+=self.phiP(self.a - xP,self.b - yP,zP,0.0)
        BzP0+=self.phiP(self.b - yP,xP,zP,0.0)
        BzP0+=self.phiP(yP,xP,zP,0.0)
        BzP0+=self.phiP(self.a - xP,yP,zP,0.0)
        BzP0+=self.phiP(xP,yP,zP,0.0)

        BzPout = - self.K * (BzPH - BzP0)
        return BzPout

    def getUpdatedJacobian(self,xP,yP,zP,delta):
        # Xprime B field partial derivatives
        B0 = self.BxP(xP,yP,zP)
        B1 = self.BxP(xP+delta, yP, zP)
        P_BxP_P_xP = (B1-B0)/delta

        B0 = self.BxP(xP, yP, zP)
        B1 = self.BxP(xP, yP+delta, zP)
        P_BxP_P_yP = (B1 - B0) / delta

        B0 = self.BxP(xP, yP, zP)
        B1 = self.BxP(xP, yP, zP+delta)
        P_BxP_P_zP = (B1 - B0) / delta

        # Yprime B field partial derivatives
        B0 = self.ByP(xP, yP, zP)
        B1 = self.ByP(xP+delta, yP, zP)
        P_ByP_P_xP = (B1 - B0) / delta

        B0 = self.ByP(xP, yP, zP)
        B1 = self.ByP(xP, yP+delta, zP)
        P_ByP_P_yP = (B1 - B0) / delta

        B0 = self.ByP(xP, yP, zP)
        B1 = self.ByP(xP, yP, zP+delta)
        P_ByP_P_zP = (B1 - B0) / delta

        # Zprime B field partial derivatives
        B0 = self.BzP(xP, yP, zP)
        B1 = self.BzP(xP+delta, yP, zP)
        P_BzP_P_xP = (B1 - B0) / delta

        B0 = self.BzP(xP, yP, zP)
        B1 = self.BzP(xP, yP+delta, zP)
        P_BzP_P_yP = (B1 - B0) / delta

        B0 = self.BzP(xP, yP, zP)
        B1 = self.BzP(xP, yP, zP+delta)
        P_BzP_P_zP = (B1 - B0) / delta

        jac = numpy.array([[P_BxP_P_xP, P_BxP_P_yP, P_BxP_P_zP],[P_ByP_P_xP, P_ByP_P_yP, P_ByP_P_zP],[P_BzP_P_xP, P_BzP_P_yP, P_BzP_P_zP]])

        return jac

    def updatePosition(self,Bx,By,Bz,guessX,guessY,guessZ):
        guessXP = guessX#*-1.0
        guessYP = guessY#*-1.0
        guessZP = guessZ
        posPrime = numpy.array([[guessXP], [guessYP], [guessZP]], numpy.double)

        #Bx *= -1.0
        #By *= -1.0

        BxPG = self.BxP(guessXP, guessYP, guessZP)
        ByPG = self.ByP(guessXP, guessYP, guessZP)
        BzPG = self.BzP(guessXP, guessYP, guessZP)

        log('xPG:%s yPG:%s zPG:%s'%(guessXP,guessYP,guessZP))

        log('BxPG:%s ByPG:%s BzPG:%s'%(BxPG,ByPG,BzPG))
        log('BxP:%s ByP:%s BzP:%s' % (Bx, By, Bz))

        objFunction = numpy.array([[BxPG-Bx],[ByPG-By],[BzPG-Bz]],numpy.double)

        BPErrorNorm = numpy.linalg.norm(objFunction)
        log('BPErrorNorm:%s'%BPErrorNorm)

        trialPos = 0
        validPosFlag = 0
        errorThreshold = 0.000001

        log('begin nonlinear equation solve')

        while (trialPos<=10000):
            log('trialPos loop begin:%i'%trialPos)
            iterations = 0
            while iterations<10:
                log('iterations loop begin:%i'%iterations)

                # If pos > 4m away from sensor, guess new poss
                if numpy.linalg.norm(posPrime)>4.0:
                    guessXP = (random.random() - 0.5) * .20
                    guessYP = (random.random() - 0.5) * .20
                    guessZP = (random.random() - 0.0) * .20
                    posPrime = numpy.array([[guessXP], [guessYP], [guessZP]], numpy.double)

                log('posPrime: %s '%str(posPrime))

                jac = self.getUpdatedJacobian(guessXP,guessYP,guessZP,.000001)
                log('jacobian: %s'%str(jac))

                invJac = numpy.linalg.inv(jac)
                log('inverse jac: %s'%str(invJac))

                delta = invJac.dot(objFunction)
                log('delta: %s'%str(delta))

                posPrime = numpy.subtract(posPrime,delta)
                log('updatedPos:%s'%posPrime)

                guessXP = posPrime[0,0]
                guessYP = posPrime[1,0]
                guessZP = posPrime[2,0]

                if guessZP < 0.0:
                    guessZP*=1.0

                BxPG = self.BxP(guessXP, guessYP, guessZP)
                ByPG = self.ByP(guessXP, guessYP, guessZP)
                BzPG = self.BzP(guessXP, guessYP, guessZP)
                log('BxPG:%s ByPG:%s BzPG:%s' % (BxPG, ByPG, BzPG))
                log('BxP:%s ByP:%s BzP:%s' % (Bx, By, Bz))
                objFunction = numpy.array([[BxPG - Bx], [ByPG - By], [BzPG - Bz]], numpy.double)
                BPErrorNorm = numpy.linalg.norm(objFunction)
                log('error norm:%f'%BPErrorNorm)

                x = posPrime[0, 0]
                y = posPrime[1, 0]
                z = posPrime[2, 0]

                if (BPErrorNorm < errorThreshold):

                    validPosFlag = 1
                    break                                       #break iterations loop
                iterations+=1
            #proceed
            log('validPosFlag:%i'%validPosFlag)
            if validPosFlag == 1:
                guessXP = posPrime[0,0]
                guessYP = posPrime[1,0]
                guessZP = posPrime[2,0]

                self.x = guessXP
                self.y = guessYP
                self.z = guessZP

                self.posVect = numpy.array([[self.x],[self.y],[self.z]],numpy.double)
                break                                           #break trialpos loop

            else:
                trialPos+=1
                guessXP = (random.random() - 0.5) * .20
                guessYP = (random.random() - 0.5) * .20
                guessZP = (random.random() - 0.0) * .20

                posPrime = numpy.array([[guessXP], [guessYP], [guessZP]], numpy.double)

        if validPosFlag == 0:
            log('ERROR: could not resolve positon')
        else:
            return [self.x,self.y,self.z]

    def getBP(self,xyzIn):
        bPMag = numpy.array([self.BxP(xyzIn[0],xyzIn[1],xyzIn[2]),self.ByP(xyzIn[0],xyzIn[1],xyzIn[2]),self.BzP(xyzIn[0],xyzIn[1],xyzIn[2])], numpy.double)
        return bPMag

#Test
if 1 == 0:
    #standard home depot magnet used
    #zP = -x
    #xP = -y
    #yP = z

    if 1 == 0:
        Lx = .047
        Ly = .0215
        Lz = .0097
        testMag = MHDRectPM(0.047,0.0215,0.0097,1.25e11)

        xP = Lx/2.0 + .2
        yP = Ly/2.0+ .2
        zMin = 0.0
        zMax = 0.2

        for i in range(0,5,1):
            z = zMin + (zMax-zMin)/4 * i
            print ('test value BxP:%f ByP:%s BzP:%s z:%s'%(testMag.BxP(xP,yP,z),testMag.ByP(xP,yP,z),testMag.BzP(xP,yP,z),z))

    if 1 == 1:
        testMag = MHDRectPM(0.047, 0.0215, 0.0097, 1.25e11)
        bxMeasured = -38.1
        byMeasured = 33.4
        bzMeasured = -.505309
        bM = numpy.array([bxMeasured, byMeasured, bzMeasured], numpy.double)

        xMeasured = -.1719
        yMeasured = 0.06330
        zMeasured = .0030  # (.0456/10)
        posM = numpy.array([xMeasured, yMeasured, zMeasured], numpy.double)
        print('posM:%s' % posM)

        globalToPrime = ['yz', [90, 90]]
        r = R.from_euler(globalToPrime[0], globalToPrime[1], degrees=True)

        posPM = r.apply(posM)
        print('posPM:%s' % posPM)

        bPM = r.apply(bM)
        print('bM:%s' % bM)
        print('bPM:%s' % bPM)

        bxPMeasured = bPM[0]
        byPMeasured = bPM[1]
        bzPMeasured = bPM[2]

        xPMeasured = posPM[0]
        yPMeasured = posPM[1]
        zPMeasured = posPM[2]

        res = testMag.updatePosition(bxPMeasured,byPMeasured,bzPMeasured,xPMeasured,yPMeasured,zPMeasured)

        print('result:%s'%res)

        print('actual xPM:%s yPM:%s zPM:%s'%(xPMeasured,yPMeasured,zPMeasured))
        print('calc   xPM:%s yPM:%s zPM:%s'%(res[0],res[1],res[2]))

        xP = numpy.array([-.72,.005,.126],numpy.double)

        #bXPCalc = numpy.array([testMag.BxP(xP[0],xP[1],xP[2]),testMag.ByP(xP[0],xP[1],xP[2]),testMag.BzP(xP[0],xP[1],xP[2])], numpy.double)
        #print('bXPCalc:%s'%bXPCalc)

        #print(testMag.BzP(0.0,0.0))

#scratch secion
if 1 == 0:
    arr = numpy.array([[1.0],[2.0],[1.5]],numpy.double)

    print(arr)


vowels = "aiueo"
ip_str = "Halo nama saya sopan setiawan, saya sedang belajar python"
ip_str = ip_str.casefold()
count = {}.fromkeys(vowels, 0)

for i in ip_str:
    if i in count:
        count[i] += 1

print(count)


'''
输入：
身高、体重、年龄、性别
处理数据：
1、计算体脂率  BMI=体重（kg）/(身高*身高)（米）
体脂率=1.2*BMI+0.23*年龄-5.4-10.8*性别（男：1 女：0）
2、判定是否在正常范围内
正常范围：男15%~18%，女25%~28%
输出：
告诉用户是否正常
'''
height=float(input("请输入身高（m）:"))

weight=float(input("请输入体重（kg）:"))
age=int(input("请输入年龄:"))
sex=int(input("请输入性别:（男：1 女：0）"))

# 容错处理
# 优先级not->and->or
# if 0<height<3 and 0<age<100 and 0<weight<150 and (sex==1 or sex==0):
if not (0<height<3 and 0<age<100 and 0<weight<150 and (sex==1 or sex==0)):
    print("数据不满足要求，程序退出")
    exit()

BMI=weight/(height*height)
TZL=1.2*BMI+0.23*age-5.4-10.8*sex
TZL/=100

# 区分男女
if sex==1:
    result = 0.15 < TZL < 0.18
elif sex==0:
    result = 0.25 < TZL < 0.28

# minNum=0.15+0.10*(1-sex)
# maxNum=0.18+0.10*(1-sex)
#
# result=minNum<TZL<maxNum

# print("你的体脂率是%f" % TZL)
# print("你的体脂率是否符合标准",result)

if sex==1:
    wenhao="先生你好"
    minNum=0.15
    maxNum=0.18
elif sex==0:
    wenhao="女士你好"
    minNum = 0.25
    maxNum = 0.28
if result:
    notice="恭喜你，身体健康，请继续保持"
else:
    if TZL>maxNum:
        notice="请注意，你的身体不正常，偏胖"
    else:
        notice="请注意，你的身体不正常，偏瘦"

print(wenhao,notice)

test1计算体脂率-优化案例.py


# A very loud TCP port scanner

# Import socket module for port scanning functionality
import socket
# Import datetime class for timestamps
from datetime import datetime
# Import scapy for packet manipulation
from scapy.all import *
import sys

# Close application if script is not running as root
if not os.geteuid() == 0:
    sys.exit('Root is required')

# Prompt user for host
host = input('Enter the hostname or IP address of the target: ')
scanType = input('Choose scan type: TCP or SYN (stealthy): ')

# Check given host is online
def pingHost(host):
    if scanType == 'TCP':
        while True:
            try:
                # Resolve host using socket
                socket.gethostbyname(host)
                host = socket.gethostbyname(host)
                break
            except Exception:
                # If unable to connect, re-prompt
                print('Unable to connect to', host)
                host = input('Enter the hostname or IP address of the target: ')
    elif scanType == 'SYN':
        while True:
            try:
                # Craft ICMP packet to ping host using Scapy
                packet = sr1(IP(dst=host)/ICMP(), timeout=5)
                break
            except Exception:
                # If unable to connect, re-prompt
                print('Unable to connect to', host)
                host = input('Enter another host: ')
    else:
        sys.exit('Invalid scan type provided, exiting...')

pingHost(host)

# Define port range. If empty, revert to 1-1023 as default (these ports can only be opened by privileged users)
portRange = input(
    'Define port range that you want to scan (leave blank for well-known ports 1-1023): '
)
portRange = portRange if portRange else '1-1023'

while True:
    # Format the port range for passing through to socket
    portRange = portRange.split('-')
    startPort = int(portRange[0])
    endPort = int(portRange[1])

    # Check port range is valid
    if startPort >= 0 and startPort < endPort:
        if endPort > startPort and endPort <= 65535:
            portRange = portRange
            break
    else:
        portRange = input('Please enter a valid port range: ')

# Output scan start date/time
print('Started scan on', datetime.now().strftime(f'%d-%m-%Y at %H:%M:%S'))

# Output scan host
print('Scan report for', host)

# Craft packet
# sendPacket = sr1(IP(dst=host) / TCP(dport=(startPort, endPort)))

# print(sendPacket)

if scanType == 'TCP':
    for port in range(startPort, endPort + 1):
        # Establish socket for IPv4 TCP scanning
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        # Set timeout on socket connection
        s.settimeout(1)

        try:
            # Open socket TCP connection
            connection = s.connect_ex((host, port))
            # If connection successful, return open port
            if connection == 0:
                print(datetime.now().strftime('%H:%M:%S') +
                      f': Port {port} is open')
        # Handle socket exceptions
        except socket.error:
            print('Connection attempt unsuccessful')
        except socket.gaierror:
            print('Unable to connect to host')
        except socket.timeout:
            print('Connection attempt timed out')
elif scanType == 'SYN':
    print('Handle SYN scan here')

print('Scan completed on', datetime.now().strftime(f'%d-%m-%Y at %H:%M:%S'))


"""Provides factory function for DownloadStrategies"""
from .bulk import BulkDownloadStrategy
from .classic import ClassicDownloadStrategy
from .full import FullDownloadStrategy

_strategies = {
    'classic': ClassicDownloadStrategy,
    'bulk': BulkDownloadStrategy,
    'full': FullDownloadStrategy,
}

# For lack of a better place for now...
def create_download_strategy(name, log, params):
    """Get a DownloadStrategy by name.

    Args:
        name (str): The name of the strategy to create
        log (Logger): The logger instance to use for logging
        params (dict): Additional parameters to pass to the logging strategy

    Returns:
        DownloadStrategy: The download strategy instance, or None
    """
    strategy = _strategies.get(name, None)
    if strategy is not None:
        return strategy(log=log, params=params)

    return None


import random
import time
import pygame
import numpy as np
import os

SCALE = 500
size = 2
colour = (0,0,255)
STEP_SIZE = 0.5
# STEP_SIZE = 1
RANDOM_MOVEMENT = False

def normalize(v):
    norm = np.linalg.norm(v)
    if norm == 0:
       return v
    return v / norm

class Point():
    def __init__(self, dim=2, coord=None):
        self.pos = []
        if coord is None:
            for i in range(dim):
                self.pos.append(SCALE*random.random())
        else:
            for i in range(len(coord)):
                self.pos.append(coord[i])

        self.pos = np.array(self.pos)
        self.new_pos = np.copy(self.pos)

        self.circle = pygame.draw.circle(screen, colour, (int(self.pos[0]),
            int(self.pos[1])), size, 0)
        self.following = None

def init(n=4, coords=None):
    '''
    returns n points.
    '''
    points = []
    for i in range(n):
        if coords:
            points.append(Point(coord=coords[i]))
        else:
            points.append(Point())

    # adding the following code.
    for i in range(n):
        idx = (i+1) % n
        points[i].following = points[idx]

    return points

def update(points):
    '''
    '''
    for i, p in enumerate(points):
        # TODO: Normalize this??
        dif = normalize(p.following.pos - p.pos)
        # print("diff = ", dif)
        p.new_pos += dif*STEP_SIZE

        if RANDOM_MOVEMENT:
            if random.randint(0,2) % 2 == 0:
                p.new_pos[0] += SCALE*random.random()
                p.new_pos[1] += SCALE*random.random()
            else:
                p.new_pos[0] -= SCALE*random.random()
                p.new_pos[1] -= SCALE*random.random()

    # now that all points positions have been updated, correct pos variable to
    # the new pos.
    for i, p in enumerate(points):
        p.pos = np.copy(p.new_pos)
        # print("for point {}, pos = {}".format(i, p.pos))

def draw_points(points):
    '''
    @points: list of point objects which need to be drawn on the screen.
    '''
    for p in points:
         p.circle = pygame.draw.circle(screen, colour, (int(p.pos[0]),
             int(p.pos[1])), size, 0)

pygame.init()
screen = pygame.display.set_mode((640, 480))
clock = pygame.time.Clock()

# coords = [(200.00,200.00), (400.00,400.00), (200.00, 400.00), (400.00, 200.00)]
# coords = [(200.00,200.00), (200.00, 400.00), (400.00, 400.00), (400.00,200.00)]
# points = init(n=len(coords), coords=coords)
# points = init()
points = init(n=100)
file_num = 0

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            break
    # clock.tick(80)

    # FFS figure out how to save frames.
    # image = cam.get_image()
    # filename = "Snaps/%04d.png" % file_num
    # pygame.image.save(image, filename)

    screen.fill(pygame.Color("black"))
    draw_points(points)
    update(points)
    pygame.display.flip()

# os.system("avconv -r 8 -f image2 -i Snaps/%04d.png -y -qscale 0 -s 640x480 -aspect 4:3 result.avi")


import os
import re
import shutil

workspace = '/media/vitale232/monster/mcfaddin_original/2015/p26r40'
server_workspace = (
    '/media/o/OpenET/projects/mcfaddin_original/2015/p26r40/zonal_stats')
landsat_re = re.compile(
    '^(LT4|LT5|LE7|LC8)(\d{3})(\d{3})(\d{4})(\d{3})')

print('Copying files from {} to {}'.format(workspace, server_workspace))
for directory in os.listdir(workspace):
    if not landsat_re.match(directory):
        continue
    image_ws = os.path.join(workspace, directory)
    for variable in ['ET24', 'ETRF', 'RN', 'G', 'H']:
        variable_dir = os.path.join(image_ws, variable)
        if not os.path.isdir(variable_dir):
            continue
        for raster in os.listdir(variable_dir):
            # if not re.match(
            #     '\w+_\d{3}_(mean|cov|std_dev|median)_\d{2}-\d{2}.img$', raster):
            if not re.match('\w+_\d{3}_mean_\d{2}-\d{2}.img$', raster):
                continue
            source_raster_path = os.path.join(variable_dir, raster)
            dest_raster_path = os.path.join(server_workspace, raster)

            print('   cp {} {}'.format(source_raster_path, dest_raster_path))
            if not os.path.isfile(dest_raster_path):
                shutil.copyfile(source_raster_path, dest_raster_path)


# -*- coding: utf-8 -*-
from datetime import datetime
import json
import logging

import pandas as pd
from colorama import Fore
from openpyxl import load_workbook
from openpyxl.styles import Alignment, PatternFill
from tqdm import tqdm

from directory import cellAlph

URL_FO = 'http://indicators.miccedu.ru/monitoring/2019/_vpo/material.php?type=1&id=4'
ID = 'an'
HEADERS = {
    'user-agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
                  'Chrome/81.0.4044.129 Safari/537.36 OPR/68.0.3618.63',
    'accept': '*/*'}

logging.basicConfig(filename="vkbot.log", level=logging.INFO)
logging.info("Start Analika: " + str(datetime.now()))

def parse_toExcel():
    try:
        with open('src/cityAndRegions.json', encoding="utf-8") as json_file:
            data = json.load(json_file)
        intIdAll = []
        arrIdCity = []
        listFileExcel = []
        linkList = []
        for x in data:
            link = str(data[x]['link'])
            intIdAll.append(int(link.split('id=')[1]))
            linkList.append(str(data[x]['link']))
        for i in range(len(intIdAll)):
            if intIdAll[i] / 10000 < 1:
                arrIdCity.append(intIdAll[i])
        with tqdm(total=len(linkList)) as pbar:
            for r in range(len(linkList)):
                url = linkList[r]
                table = pd.read_html(url, decimal=',', thousands='.')[6]
                table.to_excel('src/excel/fo/fo' + '_' + str(url.split('id=')[1]) + '.xlsx', encoding='cp1251')
                listFileExcel.append('src/excel/fo/fo' + '_' + str(url.split('id=')[1]) + '.xlsx')
                formatCell('src/excel/fo/fo' + '_' + str(url.split('id=')[1]) + '.xlsx')
                pbar.update(1)
        return listFileExcel
    except Exception as e:
        logging.error(str(datetime.now()) + " " + str(e) + str(" : ") + str(url))

def formatCell(filename):
    wb1 = load_workbook(filename)
    sheet_ranges = wb1['Sheet1']
    column_c = sheet_ranges['C']
    column_d = sheet_ranges['D']
    column_e = sheet_ranges['E']
    column_f = sheet_ranges['F']
    column_g = sheet_ranges['G']
    column_h = sheet_ranges['H']
    column_i = sheet_ranges['I']
    column_j = sheet_ranges['J']
    column_k = sheet_ranges['K']
    column_l = sheet_ranges['L']
    wrapAndWidth(filename)

def wrapAndWidth(filename):
    # ширина ячейки = 60
    wb2 = load_workbook(filename)
    sheet_ranges3 = wb2['Sheet1']
    for i in range(len(cellAlph)):
        sheet_ranges3.column_dimensions[cellAlph[i]].width = 60
    # авто-перенос по словам
    for rows in sheet_ranges3.iter_rows(min_row=1, min_col=1):
        for cell in rows:
            if cell.value is not None:
                cell.alignment = Alignment(wrap_text=True)
    # удалить, всё что идет до названия города (г.)
    wb2.save(filename)
    wb4 = load_workbook(filename)
    sheet_ranges4 = wb4['Sheet1']
    column_c4 = sheet_ranges4['C']
    for i in range(len(column_c4)):
        if column_c4[i].value == '' and column_c4[i + 1].value == '':
            pass
        elif column_c4[i].value is not None:
            s = column_c4[i].value
            s1 = s.split('г.')[0]
            s2 = s1.split(' в ')[0]
            column_c4[i].value = s2
    stylesCell(sheet_ranges4)
    wb4.save(filename)

def stylesCell(sheet):
    redFill = PatternFill(start_color='FFFF0000',
                          end_color='FFFF0000',
                          fill_type='solid')
    sheet['C1'].fill = redFill
    sheet['B1'].value = '    '
    sheet['B2'].value = '    '
    sheet['C1'].value = '    '
    sheet['C2'].value = '    '
    # sheet.unmerge_cells(start_row=1, start_column=4, end_row=1, end_column=12)
    # sheet.merge_cells(start_row=1, start_column=1, end_row=1, end_column=12)
    # sheet['A1'].value = 'Заголовок'

parse_toExcel()


import os
import datetime

from pymongo import MongoClient

BASE_DIR = os.path.dirname(os.path.realpath(__file__))

mongo = MongoClient("mongodb://localhost:27017")
db = mongo['aircode']
col = db['shopnt_prod']

today = datetime.date.today()
day_fr = datetime.date(2020,12,20)

prod_list = list(col.find({'reg_date':{'$gt':str(day_fr)}},{'_id':False}))

with open(os.path.join(BASE_DIR, f'newly_onDB_{str(day_fr)}_{str(today)}.tsv'),'w') as f:
    columns = 'prod_id\tprod_name\tprice\tscore\tscore_persons\timg_url\treg_date\tdetail_img_url\tbot_ctg\ttop_ctg\tdesc\tdetail_img_url\n'
    f.write(columns)
    for prod in prod_list:
        for i in prod.values():
            f.write(f'{i}\t')
        f.write('\n')


from machine import UART, RTC
from mbus.reader import MBusReader
import time
import ubinascii
import uasyncio_fast as asyncio

class MBusUART:
    """Wrapper for a HW UART connected to an MBus bus (via a TSS721 IC).\nProvides the user with a nice interface for reading/writing, and also provides the possibility to signal collisions on the bus.\n"""
    def __init__(self,baudrate=2400,timeout=350,callback=lambda x:print(x),debug=False):
        self.callback = callback
        self.uart = UART(2, baudrate)
        self.uart.init(baudrate,bits=8,parity=0,stop=1,timeout=timeout)
        self.mbus_reader = MBusReader(self.uart)
        self.mbus_reader.set_debug(debug)
        self.rtc = RTC()

    def set_parity(self,parity):
        self.uart.init(parity=parity)

    def send_collision(self):
        self.log_debug_string("debug","sent collision")
        self.set_parity(1)
        self.send_telegram(bytearray([0x00]))
        self.set_parity(0)

    def send_ack(self):
        self.log_debug_string("debug","sent ACK")
        self.send_telegram(bytearray([0xE5]))

    def get_time(self):
        return "%02u:%02u:%02u (%d)" % self.rtc.datetime()[4:8]

    async def read_telegram(self):
        while True:
            telegram = await self.mbus_reader.read_telegram()
            if telegram != None:
                ticks = time.ticks_ms()
                self.log_incoming(telegram)
                self.callback(telegram,ticks)
            await asyncio.sleep(0)

    def log_incoming(self, data_bytes):
        print("[{}][master <] - {}".format(self.get_time(),ubinascii.hexlify(data_bytes)))

    def log_outgoing(self, data_bytes):
        print("[{}][slave  >] - {}".format(self.get_time(),ubinascii.hexlify(data_bytes)))

    def log_debug(self,name,data_bytes):
        print("[{}][{}   ] - {}".format(self.get_time(),name, ubinascii.hexlify(data_bytes)))

    def log_debug_string(self,name,debug_string):
        print("[{}][{}   ] - {}".format(self.get_time(),name, debug_string))

    def send_telegram(self, data, ticks=None):
        len_data = len(data)
        if ticks:
           send_ticks = time.ticks_ms()
           time_diff = send_ticks - ticks
        self.uart.write(data)
        if ticks:
           send_ticks = time.ticks_ms()
           time_diff = send_ticks - ticks
        self.log_outgoing(data)
        # Since the MBus transceiver echoes all transmitted data on the RX line..
        # .. we have to get rid of the echoed data before continuing reading
        duplicated_bytes = bytearray([])
        while (len(duplicated_bytes) != len_data):
            read_bytes = self.uart.read(len_data - len(duplicated_bytes))
            if read_bytes == None:
                continue
            duplicated_bytes.extend(read_bytes)
        self.log_debug("echo  ",duplicated_bytes)
        if ticks:
            self.log_debug_string("debug","The reply was sent to the master after {} milliseconds".format(time_diff))
        print("\r\n")


from django.conf.urls import url, include

from ecommerce import views
from tests.views import (
    TestAddToCart, TestChangeCount, TestFlushCart, TestRemoveFromCart)

from pages.models import Page

app_name = 'tests'

test_url = [
    url(r'^cart-add/$', TestAddToCart.as_view(), name='cart_add'),
    url(r'^cart-change/$', TestChangeCount.as_view(), name='cart_set_count'),
    url(r'^cart-flush/$', TestFlushCart.as_view(), name='cart_flush'),
    url(r'^cart-remove/$', TestRemoveFromCart.as_view(), name='cart_remove'),
]

urlpatterns = [
    url(r'^catalog/', include('catalog.urls')),
    url(r'^shop/', include(test_url)),
    url(r'^shop/(?P<page>order)/$', views.OrderPage.as_view(),
        name=Page.CUSTOM_PAGES_URL_NAME),
]


# Automatic detection line of support/resistance
# https://towardsdatascience.com/detection-of-price-support-and-resistance-levels-in-python-baedc44c34c9

import retriever.StockDataRetriever as retriever
import pandas as pd
import numpy as np
import matplotlib.dates as mpl_dates
import matplotlib as plt
from mplfinance.original_flavor import candlestick_ohlc

class LevelDetector:
    def __init__(self, stock_data_retriever):
        # Grab S&P 500 daily data
        self.stockDataRetriever = stock_data_retriever
        self.df = self.stockDataRetriever.data
        self.df['Date'] = pd.to_datetime(self.df.index)
        self.df['Date'] = self.df['Date'].apply(mpl_dates.date2num)
        self.df = self.df.loc[:, ['Date', 'Open', 'High', 'Low', 'Close', 'Volume']]
        self.levels = []
        self.support_levels = []
        self.resistance_levels = []
        # Cleaning up noise; if a level is near another one, it will be discarded
        # Near is defined as less than the average candle size on the chart
        self.s = np.mean(self.df['High'] - self.df['Low'])
        self.find_levels()

    # These two functions identify 4-candles fractals
    @staticmethod
    def is_support(df, i):
        return df['Low'][i] < df['Low'][i - 1] < df['Low'][i - 2] and df['Low'][i] < df['Low'][i + 1] < df['Low'][i + 2]

    @staticmethod
    def is_resistance(df, i):
        return df['High'][i] > df['High'][i - 1] > df['High'][i - 2] and df['High'][i] > df['High'][i + 1] >\
               df['High'][i + 2]

    # Returns false if it is near some previously discovered key level
    def is_far_from_level(self, l):
        return np.sum([abs(l - x) < self.s for x in self.levels]) == 0

    # Finds all support and resistance levels and populates them to the class variables
    def find_levels(self):
        df = self.df
        # Create list that contains the support and resistance levels.
        # Each level is a tuple whose first element is the index of the signal candle
        # and the second element is price value.

        for i in range(2, df.shape[0] - 2):
            if LevelDetector.is_support(df, i):
                l = df['Low'][i]
                if self.is_far_from_level(l):
                    self.levels.append((i, l))
                    self.support_levels.append((i, l))
            elif LevelDetector.is_resistance(df, i):
                l = df['High'][i]
                if self.is_far_from_level(l):
                    self.levels.append((i, l))
                    self.resistance_levels.append((i, l))

    # Private method to plot price and key levels together
    def __plot_all(self, support_levels, resistance_levels):
        df = self.df

        # Plotting environment
        plt.rcParams['figure.figsize'] = [12, 7]
        plt.rc('font', size=14)

        fig = plt.figure()
        ax1 = plt.subplot()

        candlestick_ohlc(ax1, df.values, width=0.6, colorup='green', colordown='red', alpha=0.8)

        date_format = mpl_dates.DateFormatter('%d %b %Y')
        ax1.xaxis.set_major_formatter(date_format)
        fig.autofmt_xdate()
        fig.tight_layout()

        for level in support_levels:
            plt.hlines(level[1], xmin=df['Date'][level[0]], xmax=max(df['Date']), colors='blue')
        for level in resistance_levels:
            plt.hlines(level[1], xmin=df['Date'][level[0]], xmax=max(df['Date']), colors='orange')

        plt.show()

    def get_support_levels(self):
        return self.support_levels

    # Return the prices for all support levels (excludes the date that the support level happened)
    def get_support_level_prices(self):
        return sorted([support_level[1] for support_level in self.get_support_levels()])

    def get_resistance_levels(self):
        return self.resistance_levels

    def plot_all_levels(self):
        self.__plot_all(self.support_levels, self.resistance_levels)

    def plot_support_levels(self):
        self.__plot_all(self.support_levels, [])

    def plot_resistance_levels(self):
        self.__plot_all([], self.resistance_levels)

if __name__ == '__main__':
    supportDetector = LevelDetector(retriever.StockDataRetriever("CNC", "2020-06-01", "2021-07-15"))
    supportDetector.get_support_level_prices()
    supportDetector.plot_all_levels()

    # print(supportDetector.getsupport_levels())
    # supportDetector.plotsupport_levels()


__author__ = 'marin'

from tkinter import *
import numpy as np
import dataset
import dataset_square
import ANN

root = Tk()

root.title("Simple Graph")

root.resizable(0,0)

global points
global gesture

points = []

train = False

if not train:
    nn = ANN.ANN(dataset.M * 2, 20, 10, 5)
    X, y = dataset.get()
    nn.fit(X, y, 'm')
    # print(nn.predict(X))

def drag(event):
    points.append([event.x, event.y])
    c.create_oval(event.x-1, event.y-1, event.x+1, event.y+1, fill="black")
def handle_gesture(gesture):
    dataset.handle_gesture(gesture)
def release(event):
    global points
    global gesture
    gesture = np.array(points, dtype='float64')
    handle_gesture(gesture)
    points = []

def release_predict(event):
    global points
    global gesture
    global nn
    gesture = np.array(points, dtype='float64')
    x = dataset.handle_gesture(gesture, False)
    points = []
    pred = nn.predict(x)
    print(str(dataset.symbols[np.argmax(pred)]) + ' ' + str(pred))

def reset(event):
    c.delete("all")

# exit(0)

c = Canvas(root, bg="white", width=300, height= 300)

c.configure(cursor="crosshair")

c.pack()

if train is True:
    c.bind("<B1-Motion>", drag)

    c.bind('<ButtonRelease-1>', release)

    c.bind("<Button-1>", reset)
else:
    c.bind("<B1-Motion>", drag)

    c.bind('<ButtonRelease-1>', release_predict)

    c.bind("<Button-1>", reset)

root.mainloop()


from m_url.lib.cnn_lstm import lstmbidrpred
from m_url.lib.utility.url_data_loader import load_url_data
import numpy as np

def main():
    data_dir_path = './data'
    model_dir_path = './models'
    predictor = lstmbidrpred()
    np_load_old = np.load
    np.load = lambda *a,**k: np_load_old(*a, allow_pickle=True, **k)
    predictor.load_model(model_dir_path) #loading the model
    url_data = load_url_data(data_dir_path)
    np.load = np_load_old
    count = 0

    '''for url, label in zip(url_data['text'], url_data['label']):
        print(url)
        predicted_label = predictor.predict(url)
        print('predicted: ' + str(predicted_label) + ' actual: ' + str(label))
        count += 1
        if count > 20:
            break
    '''

    url_inp=input("\nEnter the URL:")#asking user to put a url to be judged
    fin=predictor.predict(url_inp)#getting the result of user input url
    if fin ==0:
        print("\nIts SAFE!!\n")
    else:
        print("\nThe website seems to be malicious!!\n")

if __name__ == '__main__':
    main()


# pylint: disable=line-too-long
r"""
Definition for 2D (orientated system)
-------------------------------------

The angles $\theta$ and $\phi$ define the orientation of the axis of the
cylinder. The angle $\Psi$ is defined as the orientation of the major
axis of the ellipse with respect to the vector $Q$. A gaussian polydispersity
can be added to any of the orientation angles, and also for the minor
radius and the ratio of the ellipse radii.

.. figure:: img/elliptical_cylinder_geometry.png

   Elliptical cylinder geometry $a = r_\text{minor}$
   and $\nu = r_\text{major} / r_\text{minor}$ is the *axis_ratio*.

The function calculated is

.. math::

    I(\vec q)=\frac{1}{V_\text{cyl}}\int{d\psi}\int{d\phi}\int{
        p(\theta,\phi,\psi)F^2(\vec q,\alpha,\psi)\sin(\alpha)d\alpha}

with the functions

.. math::

    F(q,\alpha,\psi) = 2\frac{J_1(a)\sin(b)}{ab}

where

.. math::

    a = qr'\sin(\alpha)

    b = q\frac{L}{2}\cos(\alpha)

    r'=\frac{r_{minor}}{\sqrt{2}}\sqrt{(1+\nu^{2}) + (1-\nu^{2})cos(\psi)}

and the angle $\psi$ is defined as the orientation of the major axis of the
ellipse with respect to the vector $\vec q$. The angle $\alpha$ is the angle
between the axis of the cylinder and $\vec q$.

Definition for 1D (no preferred orientation)
--------------------------------------------

The form factor is averaged over all possible orientation before normalized
by the particle volume

.. math::

    P(q) = \text{scale}  <F^2> / V

To provide easy access to the orientation of the elliptical cylinder, we
define the axis of the cylinder using two angles $\theta$, $\phi$ and $\Psi$
(see :ref:`cylinder orientation <cylinder-angle-definition>`). The angle
$\Psi$ is the rotational angle around its own long_c axis.

All angle parameters are valid and given only for 2D calculation; ie, an
oriented system.

.. figure:: img/elliptical_cylinder_angle_definition.png

    Definition of angles for oriented elliptical cylinder, where axis_ratio is drawn >1,
    and angle $\Psi$ is now a rotation around the axis of the cylinder.

.. figure:: img/elliptical_cylinder_angle_projection.png

    Examples of the angles for oriented elliptical cylinders against the
    detector plane, with $\Psi$ = 0.

The $\theta$ and $\phi$ parameters to orient the cylinder only appear in the model when fitting 2d data.
On introducing "Orientational Distribution" in the angles, "distribution of theta" and "distribution of phi" parameters will
appear. These are actually rotations about the axes $\delta_1$ and $\delta_2$ of the cylinder, the $b$ and $a$ axes of the
cylinder cross section. (When $\theta = \phi = 0$ these are parallel to the $Y$ and $X$ axes of the instrument.)
The third orientation distribution, in $\psi$, is about the $c$ axis of the particle. Some experimentation may be required to
understand the 2d patterns fully. (Earlier implementations had numerical integration issues in some circumstances when orientation
distributions passed through 90 degrees, such situations, with very broad distributions, should still be approached with care.)

NB: The 2nd virial coefficient of the cylinder is calculated based on the
averaged radius $(=\sqrt{r_\text{minor}^2 * \text{axis ratio}})$ and length
values, and used as the effective radius for $S(Q)$ when $P(Q)*S(Q)$ is applied.

Validation
----------

Validation of our code was done by comparing the output of the 1D calculation
to the angular average of the output of the 2D calculation over all possible
angles.

In the 2D average, more binning in the angle $\phi$ is necessary to get the
proper result. The following figure shows the results of the averaging by
varying the number of angular bins.

.. figure:: img/elliptical_cylinder_averaging.png

    The intensities averaged from 2D over different numbers of bins and angles.

References
----------

L A Feigin and D I Svergun, *Structure Analysis by Small-Angle X-Ray and
Neutron Scattering*, Plenum, New York, (1987) [see table 3.4]

Authorship and Verification
----------------------------

* **Author:**
* **Last Modified by:**
* **Last Reviewed by:**  Richard Heenan - corrected equation in docs **Date:** December 21, 2016

"""

from numpy import pi, inf, sqrt, sin, cos

name = "elliptical_cylinder"
title = "Form factor for an elliptical cylinder."
description = """
    Form factor for an elliptical cylinder.
    See L A Feigin and D I Svergun, Structure Analysis by Small-Angle X-Ray and Neutron Scattering, Plenum, New York, (1987).
"""
category = "shape:cylinder"

# pylint: disable=bad-whitespace, line-too-long
#             ["name", "units", default, [lower, upper], "type","description"],
parameters = [["radius_minor",     "Ang",        20.0,  [0, inf],    "volume",      "Ellipse minor radius"],
              ["axis_ratio",   "",          1.5,   [1, inf],    "volume",      "Ratio of major radius over minor radius"],
              ["length",      "Ang",        400.0, [1, inf],    "volume",      "Length of the cylinder"],
              ["sld",         "1e-6/Ang^2", 4.0,   [-inf, inf], "sld",         "Cylinder scattering length density"],
              ["sld_solvent", "1e-6/Ang^2", 1.0,   [-inf, inf], "sld",         "Solvent scattering length density"],
              ["theta",       "degrees",    90.0,  [-360, 360], "orientation", "cylinder axis to beam angle"],
              ["phi",         "degrees",    0,     [-360, 360], "orientation", "rotation about beam"],
              ["psi",         "degrees",    0,     [-360, 360], "orientation", "rotation about cylinder axis"]]

# pylint: enable=bad-whitespace, line-too-long

source = ["lib/polevl.c", "lib/sas_J1.c", "lib/gauss76.c", "lib/gauss20.c",
          "elliptical_cylinder.c"]

demo = dict(scale=1, background=0, radius_minor=100, axis_ratio=1.5, length=400.0,
            sld=4.0, sld_solvent=1.0, theta=10.0, phi=20, psi=30,
            theta_pd=10, phi_pd=2, psi_pd=3)

def ER(radius_minor, axis_ratio, length):
    """
        Equivalent radius
        @param radius_minor: Ellipse minor radius
        @param axis_ratio: Ratio of major radius over minor radius
        @param length: Length of the cylinder
    """
    radius = sqrt(radius_minor * radius_minor * axis_ratio)
    ddd = 0.75 * radius * (2 * radius * length
                           + (length + radius) * (length + pi * radius))
    return 0.5 * (ddd) ** (1. / 3.)

def random():
    import numpy as np
    # V = pi * radius_major * radius_minor * length;
    V = 10**np.random.uniform(3, 9)
    length = 10**np.random.uniform(1, 3)
    axis_ratio = 10**np.random.uniform(0, 2)
    radius_minor = np.sqrt(V/length/axis_ratio)
    Vf = 10**np.random.uniform(-4, -2)
    pars = dict(
        #background=0, sld=0, sld_solvent=1,
        scale=1e9*Vf/V,
        length=length,
        radius_minor=radius_minor,
        axis_ratio=axis_ratio,
    )
    return pars

q = 0.1
# april 6 2017, rkh added a 2d unit test, NOT READY YET pull #890 branch assume correct!
qx = q*cos(pi/6.0)
qy = q*sin(pi/6.0)

tests = [
    [{'radius_minor': 20.0, 'axis_ratio': 1.5, 'length':400.0}, 'ER', 79.89245454155024],
    [{'radius_minor': 20.0, 'axis_ratio': 1.2, 'length':300.0}, 'VR', 1],

    # The SasView test result was 0.00169, with a background of 0.001
    [{'radius_minor': 20.0, 'axis_ratio': 1.5, 'sld': 4.0, 'length':400.0,
      'sld_solvent':1.0, 'background':0.0},
     0.001, 675.504402],
#    [{'theta':80., 'phi':10.}, (qx, qy), 7.88866563001 ],
]


from notebook.notebookapp import NotebookApp, flags
from traitlets import Unicode

from .config import NteractConfig

nteract_flags = dict(flags)
nteract_flags['dev'] = (
    {'NteractConfig': {'asset_url':'http://localhost:8080/'}},
    "Start nteract in dev mode, running off your source code, with hot reloads."
)

class NteractApp(NotebookApp):
    """Application for runing nteract on a jupyter notebook server.

    """
    default_url = Unicode('/nteract/edit',
                          help="nteract's default starting location")

    classes = [*NotebookApp.classes, NteractConfig]
    flags = nteract_flags

main = launch_new_instance = NteractApp.launch_instance

if __name__ == '__main__':
    main()


from log_x import LogX
import inspect
import os
import sqlite3 as db
import sys

Log = LogX(__name__)

class db_exception(Exception):
    def __init__(self, info):
        self.info = info

    def __str__(self):
        return self.info

class table_base(object):

    INT = 'integer'
    TEXT = 'text'
    REAL = 'real'

    CLN_FLG_NOT_NULL =  0x01
    CLN_FLG_UNIQUE   =  0x02
    CLN_FLG_PRIMARY  =  0x04

    def __init__(self):
        self.entry_lst = []
        self.create_table()

    def sql_str(self):
        sql_str = 'create table %s (%s)' % \
                  (self.__tablename__, ', '.join(self.entry_lst))
        Log.debug('  ..would create <table:%s> with <SQL:%s>' %
                  (self.__tablename__, sql_str))
        return sql_str

    def add_tb_entry(self, entry, type, cln_mode=0):
        # set default mode
        if cln_mode is 0:
            cln_mode = self.CLN_FLG_NOT_NULL

        flg_not_null = ''
        flg_unique = ''
        flg_primary = ''

        if cln_mode & self.CLN_FLG_NOT_NULL:
            flg_not_null = ' not null'
        if cln_mode & self.CLN_FLG_UNIQUE:
            flg_unique = ' unique'
        if cln_mode & self.CLN_FLG_PRIMARY:
            flg_primary = ' primary key'

        self.entry_lst.append('%s %s%s%s%s' % (entry, type, flg_primary,
                                               flg_unique, flg_not_null))

    def set_default(self, entry_name, dft_value):
        i = 0
        for entry in self.entry_lst:
            if entry.split(' ')[0] == entry_name:
                self.entry_lst[i] = '%s default %s' % (entry, dft_value)
                return
            i += 1
        msg = 'Not found <key:%s>' % entry_name
        Log.fatal(msg)
        raise db_exception(msg)

    def set_check(self, entry_name, chk_rule):
        i = 0
        for entry in self.entry_lst:
            if entry.split(' ')[0] == entry_name:
                self.entry_lst[i] = '%s check(%s)' % (entry, chk_rule)
                return
            i += 1
        msg = 'Not found <key:%s>' % entry_name
        Log.fatal(msg)
        raise db_exception(msg)

from db.main_db import tb_stastics,\
                       tb_hosts,\
                       tb_commands, \
                       tb_results
class db_handler(object):
    '''
    Usage:
        1:initialize database with all registered table classes that need to
            be written in '__tables__'. And there is one import thing is this
            class need to be imported below, or else Exception would be
            reported due to loop importing.
        2:this class provides all public interface to database. <put/get>
            @tb_hosts:      record hosts need to be accessed
            @tb_commands:   record commands need to be executed
            @tb_results:    record the results returned from the remote host
            @tb_stastics:   record the stastics of <host, commands, results>
    '''
    __tables__ = [
            tb_stastics,
            tb_hosts,
            tb_commands,
            tb_results,
            ## self-defined below
            ]

    def __init__(self, db_name, is_replace=False):
        if is_replace and os.access(db_name, os.F_OK):
            os.unlink(db_name)

        self.conn = db.connect(db_name)
        self.cursor = self.conn.cursor()

        self._create_tables()
        self._init_tb_stastics()

    def _init_tb_stastics(self):
        self.cursor.execute("insert into %s (id, nhosts, ncommands, nresults) "
                            "values (0, 0, 0, 0)" % (tb_stastics.__tablename__))

    def _create_tables(self):

        # check if table is already created
        c = self.cursor
        c.execute('select tbl_name from sqlite_master '
                            'where type=\'table\'')
        existed_tables = c.fetchall()

        for table in self.__tables__:
            table_name = table.__tablename__
            if (unicode(table_name), ) in existed_tables:
                Log.info('table %s is already existed' % table_name)
                continue

            c.execute(table().sql_str())
            Log.debug('result is %s' % str(c.fetchall()))

    def commit(self):
        self.conn.commit()

    ## =======================================================================
    #   used for public interface
    def put_host(self, host, status):
        c = self.cursor
        c.fetchall()

        c.execute('select nhosts from %s where id=0' %
                  (tb_stastics.__tablename__))
        cur_nhosts = c.fetchone()[0]
        c.execute('insert into %s (id, hostname, status) values (%d, "%s", %d)' %
                  (tb_hosts.__tablename__, cur_nhosts+1, host, status))
        c.execute('update %s set nhosts=%d where id=0' %
                           (tb_stastics.__tablename__, cur_nhosts+1))

        c.fetchall()

    def put_command(self, command):
        c = self.cursor
        c.fetchall()

        c.execute('select ncommands from %s where id=0' %
                  (tb_stastics.__tablename__))
        cur_ncmds = c.fetchone()[0]
        c.execute('insert into %s (id, command) values (%d, "%s")' %
                  (tb_commands.__tablename__, cur_ncmds+1, command))
        c.execute('update %s set ncommands=%d where id=0' %
                           (tb_stastics.__tablename__, cur_ncmds+1))

        c.fetchall()

    def put_result(self, host, cmd, status, result):
        c = self.cursor
        c.fetchall()

        c.execute('select nresults from %s where id=0' %
                  (tb_stastics.__tablename__))
        cur_nresults = c.fetchone()[0]

        # get host id
        c.execute('select id from %s where hostname="%s"' %
                  (tb_hosts.__tablename__, host))
        host_id = c.fetchone()[0]

        # get command id
        c.execute('select id from %s where command="%s"' %
                  (tb_commands.__tablename__, cmd))
        cmd_id = c.fetchone()[0]

        c.execute('insert into %s (id, host, cmd, status, result) values '
                  '(%d, %d, %d, %d, "%s")' %
                  (tb_results.__tablename__, cur_nresults+1, host_id, cmd_id,
                   status, result))
        c.execute('update %s set nresults=%d where id=0' %
                           (tb_stastics.__tablename__, cur_nresults+1))

        c.fetchall()

    def get_hosts(self):
        c = self.cursor
        c.execute('select * from %s' % (tb_hosts.__tablename__))
        return c.fetchall()

    def get_cmds(self):
        c = self.cursor
        c.execute('select * from %s' % (tb_commands.__tablename__))
        return c.fetchall()

    def get_results(self):
        c = self.cursor
        c.execute('select * from %s' %
                  (tb_results.__tablename__))
        return c.fetchall()


import os
import time
import subprocess
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import curve_fit

from os import environ

pp = "./build/release/examples/"

graphs = [
    "graph_alexnet",
    "graph_mobilenet",
    "graph_resnet50",
]

cp = ["--target=NEON", "--threads=4"]
gp = ["--target=CL"]
sm = ["taskset", "-c", "0-3"]
bg = ["taskset", "-c", "4-7"]

targets = {
    "cpu_big": lambda g: bg + [pp + g] + cp,
    "cpu_small": lambda g: sm + [pp + g] + cp,
    "gpu": lambda g: [pp + g] + gp
}

T = 5
dt = 0.1

temp_func = lambda t, q0, qi, tau: q0 + qi * (1 - np.exp(- t / tau))

def get_temp():
    with open('/sys/class/thermal/thermal_zone0/temp') as f:
        temp = int(f.readline())
        return temp

def plot_temp(graph, target, x, y, popt, i):
    fig = plt.figure()
    plt.plot(x, y)
    plt.plot(x, temp_func(x, *popt),
        'r-',
        label='fit: Q0=%5.3f, Qinf=%5.3f, Tau=%5.3f' % tuple(popt))
    plt.axhline(65000, linestyle='--', color='r')
    plt.xlabel('Time')
    plt.ylabel('Temperature')
    plt.title('Execution ' + graph + " " + target)
    plt.legend(loc='best')
    plt.savefig('temp_plots/' + graph + "_" + target + "_" + str(i) + ".png")
    plt.close(fig)

def get_params(graph, target, x, y, i):
    try:
        popt, _ = curve_fit(temp_func, x, y, bounds=(0, np.inf))
        print('> Q0=%5.3f, Qinf=%5.3f, Tau=%5.3f' % tuple(popt))
        plot_temp(graph, target, x, y, popt, i)
        return popt
    except:
        print("> Bad Graph")
        return []

def find_params(graph, target, cmd, i):
    x = []
    y = []
    t = 0
    time.sleep(T)
    p = subprocess.Popen(cmd, env=env)
    while p.poll() == None:
        x.append(t)
        y.append(get_temp())
        time.sleep(dt)
        t += dt
    x = np.array(x)
    y = np.array(y)
    return get_params(graph, target, x, y, i)

if __name__ == "__main__":
    env = dict(os.environ)
    env['LD_LIBRARY_PATH'] = './build/release'
    RCs = {}
    for graph in graphs:
        print(graph)
        for target in targets:
            print(target)
            for i in range(5):
                RCs[(graph, target)] = find_params(graph, target, targets[target](graph), i)


import csv
import openpyxl
from . import models
from zipfile import ZipFile
from django.core.files import File
import re
import io
from django.core.files.base import ContentFile
import threading
from . import ocr2
import xlsxwriter
import json
import traceback
from . import serializers
from websocket import alarms
from .constants import SALE_DOCS_ASSIGN_SEQUENCE_KEY
from sequences import get_next_value
from core import logging
logger = logging.getLogger(__name__)

def doc_key(name):
    return f'{name.lower()}_doc'

def correct_date(sep: str = '/'):
    logger.info('Correct transaction dates')
    sales = models.Sale.objects.filter(transaction_date__contains=sep)
    for s in sales:
        parts = s.transaction_date.split(sep)
        mm = f'{int(parts[0]):02}'
        dd = f'{int(parts[1]):02}'
        new_date = f"{parts[2]}-{mm}-{dd} 00:00:00"
        print(f'{s.transaction_date} => {new_date}')
        s.transaction_date = new_date
        s.save()

def update_date_fmt(file: str = 'dates.csv'):
    with open(file) as f:
        reader = csv.DictReader(f)
        for row in reader:
            update(row)

def update(row):
    d, m, y = tuple(row['the_date'].split('/'))
    y = y.split(' ')[0]
    dt = f'{y}-{int(m):02d}-{int(d):02d} 00:00:00'
    s = models.Sale.objects.get(pk=row['id'])
    if s:
        s.transaction_date = dt
        s.save()
        print('Updated sale: ', s.sales_order)

def docs_schema():
    return [
        {'id': 1, 'name': models.Document.DOC_C2, 'letter': models.Document.LETTER_C2, 'key': doc_key(
            models.Document.DOC_C2), 'regex': '[ ]{0,1}(TZ\w{13,})[\({ ]', 'params': {'x': 500, 'y': 400, 'h': 400, 'w': 1400, 'threshold': 220}, 'mandatory': True},
        # {'id': 2, 'name': models.Document.DOC_ASSESSMENT, 'prefix': 'C ', 'letter': models.Document.LETTER_ASSESSMENT, 'key': doc_key(models.Document.DOC_ASSESSMENT), 'regex': '[CcGS]{1,}[ ]{0,}(\d{2,} \d{2}/\d{2}/\d{4})', 'params': {
        #     'x': 700, 'y': 20, 'h': 500, 'w': 800, 'threshold': 230, 'zoom': 1.1}, 'mandatory': True, 'replace': '\d{2}/\d{2}/'},
        {'id': 2, 'name': models.Document.DOC_ASSESSMENT, 'letter': models.Document.LETTER_ASSESSMENT, 'key': doc_key(models.Document.DOC_ASSESSMENT), 'regex': '([CcGS]{1}[ ]{0,}\d{2,} \d{2}/\d{2}/\d{4})', 'params': {
            'x': 700, 'y': 20, 'h': 500, 'w': 800, 'threshold': 230, 'zoom': 1.1}, 'mandatory': True},
        {'id': 3, 'name': models.AggregateDocument.DOC_ASSESSMENT_KG, 'letter': models.AggregateDocument.LETTER_ASSESSMENT_KG, 'key': doc_key(models.AggregateDocument.DOC_ASSESSMENT_KG), 'regex': '(T[Z2]{1}\w{2}[-\d]+) [\d\/]+$', 'params': {
            'x': 1100, 'y': 100, 'h': 400, 'w': 600, 'threshold': 230, 'zoom': 1.0}, 'mandatory': True},
        {'id': 4, 'name': models.AggregateDocument.DOC_RELEASE_NOTE, 'letter': models.AggregateDocument.LETTER_RELEASE_NOTE, 'key': doc_key(models.AggregateDocument.DOC_RELEASE_NOTE), 'regex': '(T[Z2]{1}\w{2}[-\d]+)$', 'params': {
            'x': 400, 'y': 300, 'h': 600, 'w': 800, 'threshold': 230, 'zoom': 1.0}, 'mandatory': True},
        {'id': 5, 'name': models.Document.DOC_EXIT, 'letter': models.Document.LETTER_EXIT, 'key': doc_key(
            models.Document.DOC_EXIT), 'regex': 'N[bior\.]+ ([\dXxK/]+) ([IT+])ssue|Bon de sortie[\w °]{1,} ([\dXxK/]+)[\n ]{0,}', 'params': {'x': 0, 'y': 100, 'h': 2000, 'w': 1200, 'threshold': 236, 'zoom': 1.0}, 'mandatory': True}

    ]

def import_sales(batch):
    excel_file = batch.file_in
    wb = openpyxl.load_workbook(excel_file)
    ws = wb.active
    i = 0
    rows = []
    for row in ws.values:
        if i:
            res = {'TRANS DATE': '', 'CUSTOMER': '', 'DELIVERY NOTE': '', 'VEH#': '',
                   'TAX INVOICE': '', 'SO#': '', 'PRODUCT': '', 'QTY9TONS': '', 'VALUE': '', 'DESTINATION': '', 'STATUS': '', 'DETAILS': ''}
            dict = {}
            try:
                dict['transaction_date'] = row[0]
                res['TRANS DATE'] = row[0]
                dict['customer_name'] = row[1]
                res['CUSTOMER'] = row[1]
                dict['delivery_note'] = row[2]
                res['DELIVERY NOTE'] = row[2]
                dict['vehicle_number'] = row[3]
                res['VEH#'] = row[3]
                dict['tax_invoice'] = row[4]
                res['TAX INVOICE'] = row[4]
                dict['sales_order'] = row[5]
                res['SO#'] = row[5]
                dict['product_name'] = row[6]
                res['PRODUCT'] = row[6]
                dict['quantity'] = row[7]
                res['QTY9TONS'] = row[7]
                dict['total_value'] = row[8]
                res['VALUE'] = row[8]
                dict['destination'] = row[9]
                res['DESTINATION'] = row[9]
                dict['vehicle_number_trailer'] = row[10] if len(row) > 10 else None
                res['VEH# TRAILER'] = row[10] if len(row) > 10 else None
                models.Sale.objects.create(**dict)
                res['STATUS'] = 'Success'
                res['DETAILS'] = json.dumps({'errors': []})
                rows.append(res)
            except Exception as e:
                logger.debug(e)
                err_msg = str(e)
                if 'UNIQUE' in err_msg:
                    err_msg = 'Duplicate record'
                res['STATUS'] = 'Fail'
                res['DETAILS'] = json.dumps({'errors': [{'message': err_msg}]})
                rows.append(res)
        else:
            if len(row) < 2:
                logger.debug('Not enough columns')
                break
        i += 1

    write_out(batch, rows, headers=['TRANS DATE', 'CUSTOMER', 'DELIVERY NOTE', 'VEH#',
                                    'TAX INVOICE', 'SO#', 'PRODUCT', 'QTY9TONS', 'VALUE', 'DESTINATION', 'STATUS', 'DETAILS', 'VEH# TRAILER'])
    data = serializers.BatchSerializer(batch).data
    logger.debug(data)
    alarms.trigger(batch.user, {'file_in': batch.file_in.url, 'file_out': batch.file_out.url})
    logger.debug('Completed processing the upload - sales')
    correct_date()

def map_error(error):
    res = {}
    key = error['name']
    res[key] = error['message']
    return res

def read_entries(zip, row, docs_list, profile):
    so, qty, val = (row[0], row[1], row[2])
    sale = models.Sale.objects.filter(sales_order=so.strip()).first() if so else None
    if sale:
        truck = 'trailer' if sale.quantity >= models.TRUCK_THRESHOLD else 'head'
        doc_entries = filter(lambda x: so in x.filename, docs_list)
        errors, docs = ([], [])
        for doc_entry in doc_entries:
            with zip.open(doc_entry, 'r') as file:
                filename = doc_entry.filename.split("/")[1]
                doc_type = filename[0]
                d = None
                try:
                    d = next(x for x in docs_schema() if doc_type == x['letter'])
                except Exception as e:
                    logger.debug(e)
                    res = {'sale': sale, 'result': -1, 'errors': [{'sale': 'Sales order number is not valid'}]}
                    errors.append({
                        'key': 'Unknown',
                        'name': "Unknown",
                        'message': "Not valid document or invalid prefix",
                        'mandatory': True
                    })
                    continue

                args, name, regex, pdf_data = (d['params'], d['name'], d['regex'], io.BytesIO(file.read()))
                logger.debug(name, f'"{regex}"')
                # ref_number = ocr.new_extract_from_file(regex, pdf_data, **args)
                ref_number = ocr2.extract_ref_number(pdf_data, regex,  **args)
                logger.debug("Resulting Ref No: ", ref_number)
                error = None
                if ref_number:
                    if 'corrections' in d:
                        ref_number = ocr2.apply_corrections(ref_number, d['corrections'])
                    if 'replace' in d:
                        ref_number = re.sub(d['replace'], '', ref_number)
                    prefix = d.get('prefix', '')
                    ref_number = f'{prefix}{ref_number}'
                    logger.debug(ref_number)
                    duplicate = models.Document.objects.filter(
                        ref_number=ref_number, doc_type=name, truck=truck).first()
                    if duplicate:
                        error = f'Duplicate {name} document with ref# {ref_number}; existing document attached to sale: {duplicate.sale.sales_order}'
                    else:
                        docs.append({
                            'ref_number': ref_number,
                            'file': File(pdf_data, name=filename),
                            'sale': sale,
                            'doc_type': name,
                            'truck': truck,
                            'user': profile.user
                        })
                else:
                    error = f'Invalid {name} document'
                if error:
                    errors.append({
                        'key': d['key'],
                        'name': name,
                        'message': error,
                        'mandatory': d['mandatory']
                    })

        if len(list(filter(lambda x: x['mandatory'], errors))):
            logger.debug('Errors: ', errors)
        else:
            logger.debug('Docs: ', docs)
            sale.agent = profile.agent
            sale.quantity2 = qty
            sale.total_value2 = val
            sale.assign_no = sale.assign_no if sale.assign_no else get_next_value(SALE_DOCS_ASSIGN_SEQUENCE_KEY)
            sale.save()
            for doc in docs:
                models.Document.objects.create(**doc)
        return {'sale': sale, 'result': 0, 'errors': list(map(map_error, errors))}
    else:
        return {'sale': sale, 'result': -1, 'errors': [{'sale': 'Sales order number is not valid'}]}

def cell(i, j):
    char = "A"
    char = chr(ord(char[0]) + j - 1)
    return f'{char}{i}'

def write_out(batch, rows, headers):
    output = io.BytesIO()
    workbook = xlsxwriter.Workbook(output)
    main = workbook.add_worksheet("Result")
    logger.debug(rows[0] if len(rows) else rows, headers)

    for j, col in enumerate(headers, start=1):
        main.write(f'{cell(1, j)}', col)

    for i, row in enumerate(rows, start=2):
        for j, col in enumerate(row, start=1):
            main.write(f'{cell(i, j)}', row[col])

    workbook.close()
    xlsx_data = output.getvalue()
    batch.file_out = File(output, name=f'Result_{batch.id}.xlsx')
    # batch.file_out.save()
    batch.status = 1
    batch.save()

def import_docs(batch):
    profile = batch.user.profile
    logger.debug('User Profile: ', profile)
    # return
    rows = []
    with ZipFile(batch.file_in, 'r') as zip:
        docs_list = []
        excel = None
        for entry in zip.infolist():
            if '/' in entry.filename:
                docs_list.append(entry)
            else:
                excel = entry
        sales = []
        if excel:
            with zip.open(excel, 'r') as file:
                ws = openpyxl.load_workbook(ContentFile(file.read())).active
                i = 0

                for row in ws.values:
                    logger.debug("Data ........", row)
                    if i and len(row) >= 3:
                        logger.debug(row)
                        rec = {}
                        try:
                            rec['SO#'] = row[0]
                            rec['Quantity'] = row[1]
                            rec['Volume'] = row[2]
                            res = read_entries(zip, row, docs_list, profile)
                            rec['Status'] = 'Completed' if res['result'] == 0 else 'Failed'
                            rec['Detail'] = json.dumps({'errors': res['errors']})
                        except Exception as e:
                            logger.debug("Error", e)
                            traceback.logger_exc()
                            rec['Status'] = 'Failed'
                            rec['Detail'] = json.dumps({'errors': [{'message': f'Exception: {e}'}]})
                        rows.append(rec)
                    i += 1

    headers = ['SO#', 'Quantity', 'Volume', 'Status', 'Detail']
    write_out(batch, rows, headers=headers)
    data = serializers.BatchSerializer(batch).data
    logger.debug(data)
    alarms.trigger(batch.user, {'file_in': batch.file_in.url, 'file_out': batch.file_out.url})
    logger.debug('Completed processing the upload - docs')

def sales_import_async(batch):
    t = threading.Thread(target=import_sales, args=(batch,), kwargs={})
    t.setDaemon(True)
    t.start()

def docs_import_async(batch):
    t = threading.Thread(target=import_docs, args=(batch,), kwargs={})
    t.setDaemon(True)
    t.start()


import argparse
import os
from ray import tune
from toolbox.cooperative_exploration.train import train
from toolbox.dece.dece import DECETrainer
from toolbox.dece.utils import *
from toolbox.marl import MultiAgentEnvWrapper

os.environ['OMP_NUM_THREADS'] = '1'

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("--exp-name", type=str, default="0106-dece-only-tnb")
    parser.add_argument("--num-gpus", type=int, default=8)
    parser.add_argument("--num-seeds", type=int, default=3)
    parser.add_argument("--test", action="store_true")
    args = parser.parse_args()
    exp_name = args.exp_name

    assert os.getenv("OMP_NUM_THREADS") == '1'

    walker_config = {
        "use_vtrace": False,
        ONLY_TNB: True,
        REPLAY_VALUES: False,
        DELAY_UPDATE: tune.grid_search([True, False]),

        "env": MultiAgentEnvWrapper,
        "env_config": {
            "env_name": "Walker2d-v3",
            "num_agents": 5
        },

        # should be fixed
        "kl_coeff": 1.0,
        "num_sgd_iter": 20,
        "lr": 0.0001,
        'sample_batch_size': 256,
        'sgd_minibatch_size': 4096,
        'train_batch_size': 65536,
        "num_gpus": 0.45,
        "num_cpus_per_worker": 0.5,
        "num_cpus_for_driver": 0.5,
        "num_envs_per_worker": 16,
        'num_workers': 8,
    }
    train(
        extra_config=walker_config,
        trainer=DECETrainer,
        env_name=walker_config['env_config']['env_name'],
        stop={"timesteps_total": int(5e7)},
        exp_name=exp_name,
        num_agents=walker_config['env_config']['num_agents'],
        num_seeds=args.num_seeds,
        num_gpus=args.num_gpus,
        test_mode=args.test
    )


import argparse

from parser.fetch import fetch_raw_pages
from parser.parser import parse_page
from parser.stat import calculate_word_frequency_stat, output_word_stat

def main(args):
    print('Parsing {}'.format(args.s))
    url = args.s
    pages = args.p
    pagination = args.pag
    raw_pages = fetch_raw_pages(url, pagination, pages)
    articles_info = []
    for raw_page in raw_pages:
        articles_info += parse_page(raw_page, title_class=args.tc,
                                    article_class=args.ac,
                                    article_block_class=args.ab,
                                    time_block_class=args.tb)
    word_stat = calculate_word_frequency_stat(articles_info)
    for idx in range(len(word_stat)):
        output_word_stat(word_stat[idx]['words'], word_stat[idx]['week'],
                         top_size=args.t)

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('-s', '-site', help='Site address. Example: '
                                            'https://habr.com/all/')
    parser.add_argument('--p', '--pages', type=int, nargs='?', help='Numer '
                                                                    'of '
                                                                    'pages. \
        Default 10', const=10, default=10)
    parser.add_argument('--t', '--top_num', type=int, nargs='?',
                        help='Numer of top words. Default 10', const=10,
                        default=10)
    parser.add_argument('--pag', '--pagination', nargs='?',
                        default='page{}/', help='Pagination uri. '
                                                'Example page{}/ or '
                                                '?page={} . Default '
                                                'page{}/')
    parser.add_argument('--tc', '--title_class', nargs='?',
                        default='post__title_link', help='CSS class of '
                                                         'title . Default: '
                                                         'post__title_link')
    parser.add_argument('--ac', '--article_class', nargs='?',
                        default='post__text', help='CSS class of '
                                                   'title . Default: '
                                                   'post__text')
    parser.add_argument('--ab', '--article_block', nargs='?',
                        default='post post_preview', help='CSS class of '
                                                          'article block . '
                                                          'Default: '
                                                          'post post_preview')
    parser.add_argument('--tb', '--time_block', nargs='?',
                        default='post__time', help='CSS class of '
                                                   'time block . '
                                                   'Default: '
                                                   'post__time')

    args = parser.parse_args()
    print(args)
    main(args)


"""
De functie reverse_tuple zou een tuple moeten teruggeven
dat dezelfde elementen bevat als de input tuple maar dan
in de omgekeerde volgorde. Helaas zijn er enkele bugs in
het programma geslopen. Kan jij ze vinden? Lees de errors
en gebruik de built-in debugger van Pycharm om de foutjes
te vinden en te corrigeren.
"""

def reverse_tuple(tuple):
    # Fout 1: Er was geen base case die een lege tuple afhandelde.
    if len(tuple) <= 1:
        return tuple
    else:
        # Fout 2: Het eerste element werd vooraan toegevoegd i.p.v. achteraan.
        # Fout 3: De syntax was incorrect voor tuples met lengte 1.
        # Fout 4: Er werd altijd de volledige tuple meegegeven als input aan de recursieve oproep.
        reversed_tuple = reverse_tuple(tuple[1:]) + (tuple[0],)
        return reversed_tuple

assert reverse_tuple(()) == ()
assert reverse_tuple((4,)) == (4,)
assert reverse_tuple((4, 6)) == (6, 4)


import pygame
import math
import random
from scipy.spatial import Delaunay
import numpy
from operator import itemgetter
import sort_cells as s_c

#setup

background_colour = (255,255,255)
(WIDTH, HEIGHT) = (1000, 600)
num_cells=random.randint(5,40)
Hooke_constant = 18
spring_damp = 4
pressure_coefficient = 12
drag_t = 0.003
drag_n = 0.003
t=0
FPS=30
deltat=1./FPS

screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()

def distance(a,b):
	return math.sqrt((a.x - b.x)**2 + (a.y - b.y)**2)

def sign(a):
	#return 1, -1, or 0
	if a>0:
		return 1
	elif a<0:
		return -1
	else:
		return 0

def did_collide(p_i, p_f, Edge):
	xdiff = (p_i[0] - p_f[0], Edge.nodes[0].x - Edge.nodes[1].x)
	ydiff = (p_i[1] - p_f[1], Edge.nodes[0].y - Edge.nodes[1].y)

	def det(a, b):
		return a[0] * b[1] - a[1] * b[0]

	div = det(xdiff, ydiff)
	if div == 0:
		return False
	else:
		d = (det((p_i[0],p_i[1]),(p_f[0],p_f[1])), det((Edge.nodes[0].x,Edge.nodes[0].y),(Edge.nodes[1].x,Edge.nodes[1].y)))
		x = det(d, xdiff) / div
		y = det(d, ydiff) / div
		if (Edge.nodes[0].x < x < Edge.nodes[1].x or Edge.nodes[1].x < x < Edge.nodes[0].x) and (Edge.nodes[0].y < y < Edge.nodes[1].y or Edge.nodes[1].y < y < Edge.nodes[0].y):
			return True

def elastic_collide(p_i,p_f,Edge):
	a = Edge.nodes[0]
	b = Edge.nodes[1]
	dist = distance(a,b)
	normal_dir = (-(b.y-a.y)/dist,(b.x-a.x)/dist)
	tangential_dir = (normal_dir[1],-normal_dir[0])
	v = (p_f[0]-p_i[0],p_f[1]-p_i[1])
	normal_v = (v[0]*normal_dir[0],v[1]*normal_dir[1])
	tangential_v = (v[0]*tangential_dir[0],v[1]*tangential_dir[1])
	return (p_i[0]+tangential_v[0]-normal_v[0],p_i[1]+tangential_v[1]-normal_v[1])

class cell:
	def __init__(self, parent, location, mass):
		self.x = location[0]
		self.y = location[1]
		self.forces = [0,0]
		self.v = [0,0]
		self.amplitude = 3
		self.frequency = 1.5
		self.offset = math.pi*2*(HEIGHT - self.y)/HEIGHT
		self.mass=9*random.random()+1
		self.colour = [255-255*self.mass/10,0,0]

	def draw(self):
		pygame.draw.circle(screen, self.colour, [int(self.x), int(self.y)], 10)

class edge:
	def __init__(self, a, b):
		self.nodes = [a,b]
		self.x = (a.x + b.x)/2
		self.y = (a.y + b.y)/2
		self.colour = [0,0,0]
		self.rest_length = distance(self.nodes[0],self.nodes[1])

	def draw(self,color):
		pygame.draw.line(screen, color, [self.nodes[0].x,self.nodes[0].y],[self.nodes[1].x,self.nodes[1].y], 3)

class chamber:
	def __init__(self, vertices, edges):
		if len(vertices)>len(edges):
			print("Error: Chamber edges < vertices: ", len(edges), " < ", len(vertices))
		elif len(vertices)<len(edges):
			print("Error: Chamber edges > vertices: ", len(edges), " > ", len(vertices))
		self.cells = vertices
		array=[]
		for Cell in self.cells:
			array.append([Cell.x,Cell.y])
		crt = s_c.Create_random_polygon(array)
		polygon_array = crt.main()
		reordered_cells=[0]*len(self.cells)
		for i in range(len(self.cells)):
			for j in range(len(self.cells)):
				if self.cells[i].x==polygon_array[j][0] and self.cells[i].y==polygon_array[j][1]:
					reordered_cells[j]=self.cells[i]
		self.cells=reordered_cells
		self.edges = edges
		self.signed_volume = self.compute_volume()
		self.initial_volume = math.fabs(self.signed_volume)
		(self.x,self.y)=self.compute_center()

	def compute_volume(self):
		a1=0.0
		a2=0.0
		for i in range(len(self.cells) - 1):
			a1+=self.cells[i].x*self.cells[i+1].y
			a2+=self.cells[i].y*self.cells[i+1].x
		a1+=self.cells[-1].x*self.cells[0].y
		a2+=self.cells[-1].y*self.cells[0].x
		return (a2-a1)/2

	def compute_center(self):
		x = 0.0
		y = 0.0
		for i in range(len(self.cells)-1):
			x += (self.cells[i].x+self.cells[i+1].x)*(self.cells[i].x*self.cells[i+1].y-self.cells[i].y*self.cells[i+1].x)
			y += (self.cells[i].y+self.cells[i+1].y)*(self.cells[i].x*self.cells[i+1].y-self.cells[i].y*self.cells[i+1].x)
		x += (self.cells[-1].x+self.cells[0].x)*(self.cells[-1].x*self.cells[0].y-self.cells[-1].y*self.cells[0].x)
		y += (self.cells[-1].y+self.cells[0].y)*(self.cells[-1].x*self.cells[0].y-self.cells[-1].y*self.cells[0].x)
		x = x/(-6*self.signed_volume)
		y = y/(-6*self.signed_volume)
		return int(x),int(y)

	def draw(self,color):
		pygame.draw.circle(screen, color, [self.x, self.y], 10)

def merge_chambers(parent,a,b,rem):
	#Something might be off here but I can't put my finger on it
	cells=[]
	edges=[]
	for Cell in a.cells:
		cells.append(Cell)
	for Cell in b.cells:
		if Cell not in cells:
			cells.append(Cell)
	for Edge in a.edges:
		if Edge not in edges:
			edges.append(Edge)
	for Edge in b.edges:
		if Edge not in edges:
			edges.append(Edge)
	removing = []
	for e in edges:
		if e.nodes[0] in rem.nodes and e.nodes[1] in rem.nodes and e not in removing:
			removing.append(e)
	for e in removing:
		edges.remove(e)
	print('Number of chambers before merge = ',len(parent.chambers))
	parent.chambers.remove(a)
	parent.chambers.remove(b)
	parent.chambers.append(chamber(cells,edges))
	print('Number of chambers after merge = ',len(parent.chambers))

class creature:

	def __init__(self):
		#layout some cells in the creature
		self.location = [WIDTH/2,HEIGHT/2] #location in space of the creature
		self.external_cells = [] #list of cells which are exposed to the environment
		self.cells = [] #list of cells which make up the creature
		self.points= []
		self.chambers = [] #list of pressure chambers inside the creature, each chamber is formed of edges
		self.edges = [] #list of edges in the creature, each edge connects 2 cells
		self.external_edges=[] #list of edges exposed to the environment
		self.edges2=[]

		for i in range(num_cells):
			self.cells.append(cell(self,
			(int(self.location[0] + random.randint(-200,200)),int(self.location[1] + random.randint(-200,200))),
			0.5+(1.5)*random.random()))

		#if the cells are too close move them a litte
		done = False
		counter = 0
		while not done:
			counter += 1
			found = False
			for Cell in self.cells:
				for Cell2 in self.cells:
					if distance(Cell, Cell2) <= 40 and Cell is not Cell2:
						Cell.x += random.randint(-10,10)
						Cell.y += random.randint(-10,10)
						found = True

			if not found or counter >= 100000:
				done = True
		self.x=0.0
		self.y=0.0
		#convert cells to points
		for Cell in self.cells:
			self.x+=Cell.x
			self.y+=Cell.y
			self.points.append([Cell.x,Cell.y])
		self.x/=len(self.cells)
		self.y/=len(self.cells)

		#build Delaunay Triangulization
		tri = Delaunay(self.points)
		for elm in tri.simplices:
			e1 = edge(self.cells[elm[0]],self.cells[elm[1]])
			e2 = edge(self.cells[elm[2]],self.cells[elm[1]])
			e3 = edge(self.cells[elm[0]],self.cells[elm[2]])
			e1_app=True
			e2_app=True
			e3_app=True
			for E in self.edges:
				if e1.nodes[0]==E.nodes[1] and e1.nodes[1]==E.nodes[0]:
					e1 = E
					e1_app=False
				if e2.nodes[0]==E.nodes[1] and e2.nodes[1]==E.nodes[0]:
					e2 = E
					e2_app=False
				if e3.nodes[0]==E.nodes[1] and e3.nodes[1]==E.nodes[0]:
					e3 = E
					e3_app=False
			if e1_app:
				self.edges.append(e1)
			if e2_app:
				self.edges.append(e2)
			if e3_app:
				self.edges.append(e3)
			vertices=(self.cells[elm[0]],self.cells[elm[1]],self.cells[elm[2]])
			self.chambers.append(chamber(vertices,(e1,e2,e3)))

		#This is the loop that converts Delaunay Triangulization to Gabriel Graph but the chambers get messed up

		for Edge in self.edges:
			radius = distance(Edge,Edge.nodes[1])
			removed = False
			for Cell in self.cells:
				if distance(Cell,Edge)<radius and Cell not in Edge.nodes:
					removed = True
					adj_chamb=[]
					for ch in self.chambers:
						if Edge.nodes[0] in ch.cells and Edge.nodes[1] in ch.cells:
							adj_chamb.append(ch)
					print('number of adjacent chambers = ',len(adj_chamb))
					if len(adj_chamb)==2:
						merge_chambers(self,adj_chamb[0],adj_chamb[1],Edge)
					break
			if not removed:
				self.edges2.append(Edge)
		self.edges=self.edges2

		#remove chambers which are external to the cell
		removing = []
		for ch in self.chambers:
			for ed in ch.edges:
				if ed not in self.edges and ch not in removing:
					removing.append(ch)
		for ch in removing:
			self.chambers.remove(ch)

		print("Number of chambers in the end =", len(self.chambers))

		#sort out external edges
		for Edge in self.edges:
			adj_chamb=0
			for ch in self.chambers:
				if Edge.nodes[0] in ch.cells and Edge.nodes[1] in ch.cells:
					adj_chamb+=1
			if adj_chamb<2:
				self.external_edges.append(Edge)

	def move(self):
		for Cell in self.cells:
			Cell.forces = [0,0]

		#Spring forces
		for Edge in self.edges:
			a=Edge.nodes[0]
			b=Edge.nodes[1]
			new_rest_length = ((1 + a.amplitude*math.sin(a.frequency*t + a.offset)
				+ b.amplitude*math.sin(b.frequency*t + b.offset)))*Edge.rest_length
			#work out it's current length
			current_length = distance(a,b)
			force_magnitude = Hooke_constant*(new_rest_length - current_length)
			force_direction = [(a.x - b.x)/(current_length), (a.y - b.y)/(current_length)]
			#add it onto the forces of each end
			a.forces[0] += force_direction[0]*force_magnitude-spring_damp*a.v[0]
			a.forces[1] += force_direction[1]*force_magnitude-spring_damp*a.v[1]
			b.forces[0] += -force_direction[0]*force_magnitude-spring_damp*b.v[0]
			b.forces[1] += -force_direction[1]*force_magnitude-spring_damp*b.v[1]

		#forces from chamber pressure
		for ch in self.chambers:
			ch.signed_volume = ch.compute_volume()
			ch.x,ch.y = ch.compute_center()
			force_magnitude = pressure_coefficient*(1 - math.fabs(ch.signed_volume)/ch.initial_volume)
			for Edge in ch.edges:
				force_direction1=[-(Edge.nodes[1].y-Edge.nodes[0].y),(Edge.nodes[1].x-Edge.nodes[0].x)]
				force_direction2=[-force_direction1[0],-force_direction1[1]]
				d1=(Edge.x+force_direction1[0]-ch.x)**2+(Edge.y+force_direction1[1]-ch.y)**2
				d2=(Edge.x+force_direction2[0]-ch.x)**2+(Edge.y+force_direction2[1]-ch.y)**2
				if d1<d2:
					force_direction=force_direction1
				else:
					force_direction=force_direction2
				Edge.nodes[0].forces[0] -= force_direction[0]*force_magnitude*0.5
				Edge.nodes[0].forces[1] -= force_direction[1]*force_magnitude*0.5
				Edge.nodes[1].forces[0] -= force_direction[0]*force_magnitude*0.5
				Edge.nodes[1].forces[1] -= force_direction[1]*force_magnitude*0.5

		#forces from external drag
		for Edge in self.external_edges:
			a=Edge.nodes[0]
			b=Edge.nodes[1]
			dist = distance(a,b)
			normal_dir = [-(b.y-a.y)/dist,(b.x-a.x)/dist]
			tangential_dir = [normal_dir[1],-normal_dir[0]]
			v = [(a.v[0]+b.v[0])/2,(a.v[1]+b.v[1])/2]
			normal_v = v[0]*normal_dir[0]+v[1]*normal_dir[1]
			tangential_v = v[0]*tangential_dir[0]+v[1]*tangential_dir[1]
			t_force_mag=drag_t*sign(tangential_v)*tangential_v*dist
			n_force_mag=drag_n*sign(normal_v)*normal_v*dist
			for Cell in Edge.nodes:
				Cell.forces[0]-=t_force_mag*tangential_dir[0]/2
				Cell.forces[1]-=t_force_mag*tangential_dir[1]/2
				Cell.forces[0]-=n_force_mag*normal_dir[0]/2
				Cell.forces[1]-=n_force_mag*normal_dir[1]/2

		self.x=0.0
		self.y=0.0
		for Cell in self.cells:
			Cell.v[0]+=Cell.forces[0]*deltat/Cell.mass
			Cell.v[1]+=Cell.forces[1]*deltat/Cell.mass
			p_i = (Cell.x,Cell.y)
			p_f = (Cell.x + Cell.forces[0]*.5*deltat**2/Cell.mass,Cell.y + Cell.forces[1]*.5*deltat**2/Cell.mass)
			'''for Edge in self.edges:
				if did_collide(p_i,p_f,Edge) and Cell not in Edge.nodes:
					p_f = elastic_collide(p_i,p_f,Edge)'''
			Cell.x = p_f[0]
			Cell.y = p_f[1]
			self.x+=Cell.x
			self.y+=Cell.y

		self.x/=len(self.cells)
		self.y/=len(self.cells)

	def draw(self):
		for Cell in self.cells:
			Cell.draw()
		for Edge in self.edges:
			Edge.draw([0,0,0])
		for Edge in self.external_edges:
			Edge.draw([0,255,0])
		for Ch in self.chambers:
			Ch.draw([200,0,200])

worm = creature()
startx=worm.x
starty=worm.y
running=True
while running:
	screen.fill(background_colour)
	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			running = False
		elif event.type == pygame.KEYDOWN:
			if event.key == pygame.K_ESCAPE:
				running = False
			if event.key == pygame.K_SPACE:
				worm = creature()
	worm.move()
	worm.draw()
	travel_dist=(worm.x-startx)**2+(worm.y-starty)**2
	pygame.display.update()
	t+=deltat
	clock.tick(FPS)
pygame.quit()


#!/usr/bin/env python3
"""
文件上传/下载脚本(到自己的VPS中)

使用方法:
    将该文件放到PATH中,并把如下信息加入zshrc:
    alias up='python myfile.py up'
    alias down='python myfile.py down'

    up filename即可
"""
import sys
import subprocess

UPLOAD_URL = "http://rockywu.me/up/"
DOWNLOAD_URL = "http://rockywu.me/down/"

def run_cmd(cmd):
    p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
    p.wait()
    out = p.stdout.readlines()
    return p.returncode, out

if __name__ == "__main__":
    if len(sys.argv) < 3:
        sys.exit(1)
    if sys.argv[1] in ("u", "up"):
        cmd = "curl --insecure -T {filename} {url}".format(filename=sys.argv[2], url=UPLOAD_URL)
    elif sys.argv[1] in ("d", "down"):
        cmd = "wget  --no-check-certificate {url}{filename}".format(filename=sys.argv[2], url=UPLOAD_URL)
    print("**" * 10 + DOWNLOAD_URL+sys.argv[2] + "**" * 10)
    run_cmd(cmd)


"""
Simple graph implementation
"""
import random

class Queue:
    def __init__(self):
        self.queue = []
    def enqueue(self, value):
        self.queue.append(value)
    def dequeue(self):
        if (self.size()) > 0:
            return self.queue.pop(0)
        else:
            return None
    def size(self):
        return len(self.queue)

class Stack:
    def __init__(self):
        self.stack = []
    def push(self, value):
        self.stack.append(value)
    def pop(self):
        if (self.size()) > 0:
            return self.stack.pop()
        else:
            return None
    def size(self):
        return len(self.stack)

class Graph:
    """Represent a graph as a dictionary of vertices mapping labels to edges."""
    def __init__(self):
        """
        Create an empty graph
        """
        self.vertices = {}
    def add_vertex(self, vertex_id):
        """
        Add an vertex to the graph
        """
        self.vertices[vertex_id] = Vertex(vertex_id)
    def add_edge(self, v1, v2):
        """
        Add an undirected edge to the graph
        """
        if v1 in self.vertices and v2 in self.vertices:
            self.vertices[v1].edges.add(v2)
            self.vertices[v2].edges.add(v1)
        else:
            raise IndexError("That vertex does not exist!")
    def add_directed_edge(self, v1, v2):
        """
        Add a directed edge to the graph
        """
        if v1 in self.vertices:
            self.vertices[v1].edges.add(v2)
        else:
            raise IndexError("That vertex does not exist!")
    def dft(self, starting_node, visited=None):
        if visited is None:
            visited = []
        visited.append(starting_node)
        print(starting_node)

        for node in self.vertices[starting_node].edges:
            if node not in visited:
                self.dft(node, visited)
        return visited
    def bft(self, starting_node):
        visited = []
        q = Queue()
        q.enqueue(starting_node)
        while q.size() > 0:
            dequeued = q.dequeue()
            visited.append(dequeued)
            print(dequeued)
            for edge in self.vertices[dequeued].edges:
                if edge not in visited:
                    q.enqueue(edge)
        return visited
    def dfs(self, starting_node, target=None):
        visited = []
        s = Stack()
        s.push(starting_node)
        while s.size() > 0:
            current = s.pop()
            if current not in visited:
                if current == target:
                    visited.append(current)
                    break
                visited.append(current)
                for next_vert in self.vertices[current].edges:
                    s.push(next_vert)
        print(visited)
        return visited

    def bfs(self, starting_node, target):
        visited = set()
        q = Queue()
        q.enqueue([starting_node])
        while q.size() > 0:
            node = q.dequeue()
            if node[-1] not in visited:
                if target == node[-1]:
                    return node
                visited.add(node[-1])
                for child in self.vertices[node[-1]].edges:
                    new_path = list(node)
                    new_path.append(child)
                    q.enqueue(new_path)
        return None

class Vertex:
    def __init__(self, vertex_id):
        self.id = vertex_id
        self.edges = set()


#!/usr/bin/env python
import numpy as np
import matplotlib.pyplot as plt
n  = 61, 61
vx = np.fromfile( 'tmp/out/vx', 'f' ).reshape( n )
vy = np.fromfile( 'tmp/out/vy', 'f' ).reshape( n )
vm = np.sqrt( vx * vx + vy * vy )
fig = plt.figure( figsize=(3, 3) )
ax = fig.add_subplot( 111 )
ax.imshow( vm, extent=(-3, 3, -3, 3), interpolation='nearest', vmax=1 )
ax.axis( 'image' )
fig.savefig( 'tmp/example.png', dpi=80 )


import string

def rot13(message):
    result = ''
    low = (string.ascii_lowercase)*2
    high = (string.ascii_uppercase)*2

    for i in message:
        if i.islower():
            index = low.find(i)
            result += low[index+13]
        elif i.isupper():
            index = high.find(i)
            result += high[index+13]
        else:
            result += i
    return result