#!/usr/bin/env python3
import sqlite3
import os

class CRUD:

    columns = {
    "osaccuracy" : "osaccuracy integer",
    "argument" : "argument text",
    "conf" : "conf text",
    "cpe" : "cpe text",
    "date" : "date datetime",
    "discoverd" : "discovered integer",
    "elapsed" : "elapsed real",
    "entries" : "entries integer",
    "end" : "end datetime",
    "extrainfo" : "extrainfo text",
    "filename" : "filename text",
    "fds" : "fds datetime",
    "hosts" : "hosts integer",
    "host" : "host text",
    "hardvendor" : "hardvendor text",
    "id" : "id integer primary key not null",
    "ip" : "ip text",
    "lds" : "lds datetime",
    "mac" : "mac text",
    "name" : "name text",
    "osvendor" : "osvendor text",
    "osfamily" : "osfamily text",
    "openports" : "openports integer",
    "scanfinished" : "scanfinished integer",
    "osgeneration" : "osgeneration text",
    "port" : "port integer",
    "product" : "product text",
    "protocol" : "protocol text",
    "reason" : "reason text",
    "scan" : "scan integer",
    "state" : "state text",
    "scanned" : "scanned integer",
    "service" : "service text",
    "start" : "datetime",
    "uphosts" : "uphosts integer",
    "version" : "version text",
    }

    def __init__(self, properties,logger):
        self.logger=logger
        self.properties = properties
        self.dir = self.properties.getProjectDirectory()
        self.createDatabaseConnection()
        self.createTables()

    def createDatabaseConnection(self):
        uptime = self.properties.getProperty("database", "uptime.name")
        meta = self.properties.getProperty("database","meta.name")
        self.uptimepath = "%s/%s" % (self.dir, uptime)
        self.metapath = "%s/%s" % (self.dir, meta)

    def createTables(self):
        self.createTableMAC()
        self.createTableIp()
        self.createTableHosts()
        self.createTableSoftvendors()
        self.createTableHardvendors()
        self.createTablePorts()
        self.createTableOsInfo()
        self.createTableProtocolls()
        self.createTableServices()
        self.createTableArguments()
        self.createTableSweepscans()
        self.createTableUptimes()
        self.createTableScan()
        self.createTableScanports()
        self.createTableUPS()

    def deleteAll(self):
        os.system("rm %s %s" % (self.uptimepath, self.metapath))

#------------------- MAC --------------------------

    def createTableMAC(self):
        mac = [self.columns["id"],self.columns["mac"],self.columns["fds"],self.columns["lds"],self.columns["scanned"],self.columns["discoverd"]]
        self.createTableIfNotExists(self.metapath,"macaddress",mac)

    def updateMacDiscovered(self,mac):
        conn=sqlite3.connect(self.metapath)
        cursor=conn.cursor()
        cursor.execute("update macaddress set discovered=discovered+1 where mac=?", (mac,))
        conn.commit()
        conn.close()

    def updateMacScanned(self,mac,date):
        conn=sqlite3.connect(self.metapath)
        cursor=conn.cursor()
        cursor.execute("update macaddress set scanned=scanned+1, lds=? where mac=?", (date,mac))
        conn.commit()
        conn.close()

    def insertIntoMacs(self,mac,fds,lds,scanned,discovered):
        conn=sqlite3.connect(self.metapath)
        cursor=conn.cursor()
        cursor.execute("insert into macaddress(mac,fds,lds,scanned,discovered) values(?,?,?,?,?)",(mac,fds,lds,scanned,discovered))
        id=cursor.lastrowid
        conn.commit()
        conn.close()
        return id

    def selectAllFromMacByMac(self,mac):
        conn=sqlite3.connect(self.metapath)
        cursor=conn.execute("select * from macaddress where mac=?",(mac,))
        row = cursor.fetchone()
        if row == None:
            conn.close()
            return None
        else:
            result = {"id":row[0],"mac":row[1],"fds":row[2],"lds":row[3],"scanned":row[4],"discovered":row[5]}
            conn.close()
            return result

#------------------- IP --------------------------

    def createTableIp(self):
        ip = [self.columns["id"],self.columns["ip"],self.columns["fds"],self.columns["lds"],self.columns["scanned"],self.columns["discoverd"]]
        self.createTableIfNotExists(self.metapath,"ipaddress",ip)

    def updateIpDiscovered(self,ip):
        conn=sqlite3.connect(self.metapath)
        cursor=conn.cursor()
        cursor.execute("update ipaddress set discovered=discovered+1 where ip=?", (ip,))
        conn.commit()
        conn.close()

    def updateIpScanned(self,ip,date):
        conn=sqlite3.connect(self.metapath)
        cursor=conn.cursor()
        cursor.execute("update ipaddress set scanned=scanned+1,lds=? where ip=?", (date,ip))
        conn.commit()
        conn.close()

    def insertIntoIps(self,ip,fds,lds,scanned,discovered):
        conn=sqlite3.connect(self.metapath)
        cursor=conn.cursor()
        cursor.execute("insert into ipaddress(ip,fds,lds,scanned,discovered) values(?,?,?,?,?)",(ip,fds,lds,scanned,discovered))
        id=cursor.lastrowid
        conn.commit()
        conn.close()
        return id

    def selectAllFromIpsByIp(self,ip):
        conn=sqlite3.connect(self.metapath)
        cursor=conn.execute("select * from ipaddress where ip=?",(ip,))
        row = cursor.fetchone()
        if row == None:
            conn.close()
            return None
        else:
            result = {"id":row[0],"ip":row[1],"fds":row[2],"lds":row[3],"scanned":row[4],"discovered":row[5]}
            conn.close()
            return result

#------------------- HOSTS --------------------------
    def createTableHosts(self):
        host = [self.columns["id"],self.columns["host"],self.columns["fds"],self.columns["lds"],self.columns["scanned"],self.columns["discoverd"]]
        self.createTableIfNotExists(self.metapath,"hostnames",host)

    def updateHostnameDiscovered(self,hostname):
        conn=sqlite3.connect(self.metapath)
        cursor=conn.cursor()
        cursor.execute("update hostnames set discovered=discovered+1 where host=?", (hostname,))
        conn.commit()
        conn.close()

    def updateHostnameScanned(self,hostname,date):
        conn=sqlite3.connect(self.metapath)
        cursor=conn.cursor()
        cursor.execute("update hostnames set scanned=scanned+1,lds=? where host=?", (date,hostname))
        conn.commit()
        conn.close()

    def insertIntoHostnames(self,hostname,fds,lds,scanned,discovered):
        conn=sqlite3.connect(self.metapath)
        cursor=conn.cursor()
        cursor.execute("insert into hostnames(host,fds,lds,scanned,discovered) values(?,?,?,?,?)",(hostname,fds,lds,scanned,discovered))
        id=cursor.lastrowid
        conn.commit()
        conn.close()
        return id

    def selectAllFromHostnameByHostname(self,hostname):
        conn=sqlite3.connect(self.metapath)
        cursor=conn.execute("select * from hostnames where host=?",(hostname,))
        row = cursor.fetchone()
        if row == None:
            conn.close()
            return None
        else:
            result = {"id":row[0],"host":row[1],"fds":row[2],"lds":row[3],"scanned":row[4],"discovered":row[5]}
            conn.close()
            return result

#------------------- Softvendors --------------------------
    def createTableSoftvendors(self):
        vendor = [self.columns["id"],self.columns["osvendor"],self.columns["fds"],self.columns["lds"],self.columns["scanned"]]
        self.createTableIfNotExists(self.metapath,"softvendors",vendor)

    def updateSoftvendorScanned(self,softvendor,date):
        conn=sqlite3.connect(self.metapath)
        cursor=conn.cursor()
        cursor.execute("update softvendors set scanned=scanned+1,lds=? where osvendor=?", (date,softvendor))
        conn.commit()
        conn.close()

    def insertIntoSoftvendor(self,softvendor,fds,lds,scanned):
        conn=sqlite3.connect(self.metapath)
        cursor=conn.cursor()
        cursor.execute("insert into softvendors(osvendor,fds,lds,scanned) values(?,?,?,?)",(softvendor,fds,lds,scanned))
        id=cursor.lastrowid
        conn.commit()
        conn.close()
        return id

    def selectAllFromSoftvendorBySoftvendor(self,softvendor):
        conn=sqlite3.connect(self.metapath)
        cursor=conn.execute("select * from softvendors where osvendor=?",(softvendor,))
        row = cursor.fetchone()
        if row == None:
            conn.close()
            return None
        else:
            result = {"id":row[0],"osvendor":row[1],"fds":row[2],"lds":row[3],"scanned":row[4]}
            conn.close()
            return result

#------------------- Hardvendors --------------------------
    def createTableHardvendors(self):
        vendor = [self.columns["id"],self.columns["hardvendor"],self.columns["fds"],self.columns["lds"],self.columns["scanned"]]
        self.createTableIfNotExists(self.metapath,"hardvendors",vendor)

    def updateHardvendorScanned(self,hardvendor,date):
        conn=sqlite3.connect(self.metapath)
        cursor=conn.cursor()
        cursor.execute("update hardvendors set scanned=scanned+1,lds=? where hardvendor=?", (date,hardvendor))
        conn.commit()
        conn.close()

    def insertIntoHardvendor(self,hardvendor,fds,lds,scanned):
        conn=sqlite3.connect(self.metapath)
        cursor=conn.cursor()
        cursor.execute("insert into hardvendors(hardvendor,fds,lds,scanned) values(?,?,?,?)",(hardvendor,fds,lds,scanned))
        id=cursor.lastrowid
        conn.commit()
        conn.close()
        return id

    def selectAllFromHardvendorByHardvendor(self,hardvendor):
        conn=sqlite3.connect(self.metapath)
        cursor=conn.execute("select * from hardvendors where hardvendor=?",(hardvendor,))
        row = cursor.fetchone()
        if row == None:
            conn.close()
            return None
        else:
            result = {"id":row[0],"hardvendor":row[1],"fds":row[2],"lds":row[3],"scanned":row[4]}
            conn.close()
            return result

#------------------- OsInfo --------------------------
    def createTableOsInfo(self):
        osinfo = [self.columns["id"],self.columns["osvendor"],self.columns["osfamily"],self.columns["osgeneration"],
                  self.columns["fds"],self.columns["lds"],self.columns["scanned"]]
        self.createTableIfNotExists(self.metapath,"osinfo",osinfo)

    def updateOsInfo(self,osvendor,osfamily,osgeneration,date):
        conn=sqlite3.connect(self.metapath)
        cursor=conn.cursor()
        cursor.execute("update osinfo set scanned=scanned+1,lds=? where osvendor=? and osfamily=? and osgeneration=?", (date,osvendor,osfamily,osgeneration))
        conn.commit()
        conn.close()

    def insertIntoOsInfo(self,osvendor,osfamily,osgeneration,fds,lds,scanned):
        conn=sqlite3.connect(self.metapath)
        cursor=conn.cursor()
        cursor.execute("insert into osinfo(osvendor,osfamily,osgeneration,fds,lds,scanned) values(?,?,?,?,?,?)",(osvendor,osfamily,osgeneration,fds,lds,scanned))
        id=cursor.lastrowid
        conn.commit()
        conn.close()
        return id

    def selectAllFromOsInfo(self,osvendor,osfamily,osgeneration):
        conn=sqlite3.connect(self.metapath)
        cursor=conn.execute("select * from osinfo where osvendor=? and osfamily=? and osgeneration=?",(osvendor,osfamily,osgeneration))
        row = cursor.fetchone()
        if row == None:
            conn.close()
            return None
        else:
            result = {"id":row[0],"osvendor":row[1],"osfamily":row[2],"osgeneration":row[3],"fds":row[4],"lds":row[5],"scanned":row[6]}
            conn.close()
            return result

#------------------- Ports --------------------------
    def createTablePorts(self):
        port = [self.columns["id"],self.columns["port"],self.columns["fds"],self.columns["lds"],self.columns["scanned"]]
        self.createTableIfNotExists(self.metapath,"ports",port)

    def updatePort(self,port,date):
        conn=sqlite3.connect(self.metapath)
        cursor=conn.cursor()
        cursor.execute("update ports set scanned=scanned+1,lds=? where port=? ", (date,port))
        conn.commit()
        conn.close()

    def insertIntoPorts(self,port,fds,lds,scanned):
        conn=sqlite3.connect(self.metapath)
        cursor=conn.cursor()
        cursor.execute("insert into ports(port,fds,lds,scanned) values(?,?,?,?)",(port,fds,lds,scanned))
        id=cursor.lastrowid
        conn.commit()
        conn.close()
        return id

    def selectAllFromPorts(self,port):
        conn=sqlite3.connect(self.metapath)
        cursor=conn.execute("select * from ports where port=?",(port,))
        row = cursor.fetchone()
        if row == None:
            conn.close()
            return None
        else:
            result = {"id":row[0],"port":row[1],"fds":row[2],"lds":row[3],"scanned":row[4]}
            conn.close()
            return result

#------------------- Services --------------------------
    def createTableServices(self):
        service = [self.columns["id"],self.columns["service"],self.columns["version"],self.columns["product"],self.columns["fds"],self.columns["lds"],self.columns["scanned"]]
        self.createTableIfNotExists(self.metapath,"services",service)

    def updateService(self,service,version,product,date):
        conn=sqlite3.connect(self.metapath)
        cursor=conn.cursor()
        cursor.execute("update services set scanned=scanned+1,lds=? where service=? and version=? and product=?", (date,service,version,product))
        conn.commit()
        conn.close()

    def insertIntoService(self,service,version,product,fds,lds,scanned):
        conn=sqlite3.connect(self.metapath)
        cursor=conn.cursor()
        cursor.execute("insert into services(service,version,product,fds,lds,scanned) values(?,?,?,?,?,?)",(service,version,product,fds,lds,scanned))
        id=cursor.lastrowid
        conn.commit()
        conn.close()
        return id

    def selectAllFromService(self,service,version,product):
        conn=sqlite3.connect(self.metapath)
        cursor=conn.execute("select * from services where service=? and version=? and product=?",(service,version,product))
        row = cursor.fetchone()
        if row == None:
            conn.close()
            return None
        else:
            result = {"id":row[0],"service":row[1],"version":row[2],"product":row[3],"fds":row[4],"lds":row[5],"scanned":row[6]}
            conn.close()
            return result

#------------------- Protocols --------------------------
    def createTableProtocolls(self):
        protocol = [self.columns["id"],self.columns["protocol"],self.columns["fds"],self.columns["lds"],self.columns["scanned"]]
        self.createTableIfNotExists(self.metapath,"protocols",protocol)

#------------------- Arguments --------------------------
    def createTableArguments(self):
        argument = [self.columns["id"],self.columns["argument"],self.columns["fds"],self.columns["lds"],self.columns["scanned"]]
        self.createTableIfNotExists(self.metapath,"arguments",argument)

#------------------- Sweepscan --------------------------
    def createTableSweepscans(self):
        sweepscan = [self.columns["id"],self.columns["uphosts"],self.columns["hosts"],self.columns["date"],self.columns["elapsed"],self.columns["argument"]]
        self.createTableIfNotExists(self.metapath,"sweepscans",sweepscan)

    def selectAllFromSweepscan(self,id):
        conn=sqlite3.connect(self.metapath)
        cursor=conn.execute("select * from sweepscans where id=?",(id,))
        row = cursor.fetchone()
        if row == None:
            conn.close()
            return None
        else:
            result = {"id":row[0],"uphosts":row[1],"hosts":row[2],"date":row[3],"elapsed":row[4],"argument":row[5]}
            conn.close()
            return result

    def insertIntoSweepscans(self,uphosts,hosts,date,elapsed,argument):
        conn=sqlite3.connect(self.metapath)
        cursor=conn.cursor()
        cursor.execute("insert into sweepscans(uphosts,hosts,date,elapsed,argument) values(?,?,?,?,?)", (uphosts,hosts,date,elapsed,argument))
        idscan=cursor.lastrowid
        conn.commit()
        conn.close()
        return idscan

#------------------- Uptimes --------------------------
    def createTableUptimes(self):
        uptime = [self.columns["id"],self.columns["filename"],self.columns["start"],self.columns["end"],self.columns["entries"]]
        self.createTableIfNotExists(self.metapath,"uptimes",uptime)

#------------------- Scan --------------------------
    def createTableScan(self):
        scan = [self.columns["id"],self.columns["mac"],self.columns["ip"],self.columns["host"],self.columns["date"],self.columns["argument"],
                self.columns["elapsed"],self.columns["openports"],self.columns["scanfinished"],self.columns["osvendor"],self.columns["osfamily"],
                self.columns["osgeneration"],self.columns["osaccuracy"],self.columns["hardvendor"]]
        self.createTableIfNotExists(self.metapath,"scans",scan)

    def insertIntoScan(self,mac,ip,host,date,arguments,elapsed,openports,scanfinished,osvendor,osfamily,osgeneration,osaccuracy,hardvendor):
        conn=sqlite3.connect(self.metapath)
        cursor=conn.cursor()
        cursor.execute("insert into scans(mac,ip,host,date,argument,elapsed,openports,scanfinished,osvendor,osfamily,osgeneration,osaccuracy,hardvendor) values(?,?,?,?,?,?,?,?,?,?,?,?,?)",
                       (mac,ip,host,date,arguments,elapsed,openports,scanfinished,osvendor,osfamily,osgeneration,osaccuracy,hardvendor))
        idscan=cursor.lastrowid
        conn.commit()
        conn.close()
        return idscan

    def selectAllFromScan(self,id):
        conn=sqlite3.connect(self.metapath)
        cursor=conn.execute("select * from scans where id=?",(id,))
        row = cursor.fetchone()
        if row == None:
            conn.close()
            return None
        else:
            result = {"id":row[0],"mac":row[1],"ip":row[2],"host":row[3],"date":row[4],"arguments":row[5],
                      "elapsed":row[6],"openports":row[7],"scanfinished":row[8],"osvendor":row[9],"osfamily":row[10],
                      "osgeneration":row[11],"osaccuracy":row[12],"hardvendor":row[13]}
            conn.close()
            return result

#------------------- ScanPorts --------------------------
    def createTableScanports(self):
        scanport = [self.columns["id"],self.columns["scan"],self.columns["port"],self.columns["conf"],self.columns["reason"],self.columns["cpe"],
                    self.columns["state"],self.columns["protocol"],self.columns["version"],self.columns["name"],self.columns["product"],
                    self.columns["extrainfo"]]
        self.createTableIfNotExists(self.metapath,"scanports",scanport)

    def insertIntoScanport(self,scan,port,conf,reason,cpe,state,protocol,version,name,product,extrainfo):
        conn=sqlite3.connect(self.metapath)
        cursor=conn.cursor()
        cursor.execute("insert into scanports(scan,port,conf,reason,cpe,state,protocol,version,name,product,extrainfo) values(?,?,?,?,?,?,?,?,?,?,?)",
                       (scan,port,conf,reason,cpe,state,protocol,version,name,product,extrainfo))
        id=cursor.lastrowid
        conn.commit()
        conn.close()
        return id

    def selectAllFromScanportByScan(self,scan):
        conn=sqlite3.connect(self.metapath)
        cursor=conn.execute("select * from scanports where scan=?",(scan,))
        row = cursor.fetchone()
        if row == None:
            conn.close()
            return None
        else:
            result = {"id":row[0],"scan":row[1],"port":row[2],"conf":row[3],"reason":row[4],"cpe":row[5],
                      "state":row[6],"protocol":row[7],"version":row[8],"name":row[9],"product":row[10],
                      "extrainfo":row[11]}
            conn.close()
            return result

#------------------- UPS --------------------------
    def createTableUPS(self):
        up = [self.columns["id"],self.columns["ip"],self.columns["host"],self.columns["mac"],self.columns["date"],self.columns["scan"]]
        self.createTableIfNotExists(self.uptimepath,"ups",up)

    def selectAllFromUphostsById(self,id):
        conn=sqlite3.connect(self.uptimepath)
        cursor=conn.execute("select * from ups where id=?",(id,))
        row = cursor.fetchone()
        if row == None:
            conn.close()
            return None
        else:
            result = {"id":row[0],"ip":row[1],"host":row[2],"mac":row[3],"date":row[4],"scan":row[5]}
            conn.close()
            return result

    def insertIntoUphosts(self,ip,host,mac,date,scan):
        conn=sqlite3.connect(self.uptimepath)
        cursor=conn.cursor()
        cursor.execute("insert into ups(ip,host,mac,date,scan) values(?,?,?,?,?)",(ip,host,mac,date,scan))
        idscan=cursor.lastrowid
        conn.commit()
        conn.close()
        return idscan

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

    def createTableIfNotExists(self, path,tablename,columnitems):
        conn = sqlite3.connect(path)
        columns = ",".join(columnitems)
        command = "create table if not exists %s(%s);" % (tablename,columns)
        conn.execute(command)
        conn.close()


#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Tue Oct  3
This is auxilary class for tsne visualization project.
This class objective is extracting features from images.

@author: esoroush
"""
import os, pickle

from keras.engine import  Model
from keras_vggface.vggface import VGGFace
from keras.preprocessing import image
from keras_vggface import utils

import numpy as np
import tensorflow as tf
from PIL import Image

supported_feature_extraction = ['inception', 'raw', 'vggfaces']
# TODO: This class should be standalone and independent with tf.app.flags
# Google inception pre-trained network
class FeatureExtractor(object):
    def __init__(self, image_names):
        self.image_names = image_names
        self.no_of_images = len(image_names)
        self.image_path = image_names[0].split('/')[-2]
        self.supported_feature_extraction = supported_feature_extraction
    def extract_feature(self):
        if tf.flags.FLAGS.feature_extraction == 'raw':
            features = self.raw_features()
        elif tf.flags.FLAGS.feature_extraction == 'inception':
            features = self.inception_features()
        elif tf.flags.FLAGS.feature_extraction == 'vggfaces':
            features = self.vggfaces_features()
        else:
            print('Currently the supported methods for feature extraction are: {}'.format(self.supported_feature_extraction))
            return
        return features
    def inception_features(self):
        print('using Inception network for feature extraction')
        import sys, tarfile
        from six.moves import urllib
        model_dir = os.path.join(os.environ['HOME'], '.tensorflow/models')
        DATA_URL = 'http://download.tensorflow.org/models/image/imagenet/inception-2015-12-05.tgz'
        def create_graph():

            """Creates a graph from saved GraphDef file and returns a saver."""
            # Creates graph from saved graph_def.pb.
            with tf.gfile.FastGFile(os.path.join(
              model_dir, 'classify_image_graph_def.pb'), 'rb') as f:
                graph_def = tf.GraphDef()
                graph_def.ParseFromString(f.read())
                _ = tf.import_graph_def(graph_def, name='')

        def run_inference_on_image(image):

            """Runs forward path on an image.
            Args:
            image: Image file name.

            Returns:
            off the shelf 2048 feature vector
            """
            if not tf.gfile.Exists(image):
                tf.logging.fatal('File does not exist %s', image)
            image_data = tf.gfile.FastGFile(image, 'rb').read()

            with tf.Session() as sess:
            # Some useful tensors:
            # 'softmax:0': A tensor containing the normalized prediction across
            #   1000 labels.
            # 'pool_3:0': A tensor containing the next-to-last layer containing 2048
            #   float description of the image.
            # 'DecodeJpeg/contents:0': A tensor containing a string providing JPEG
            #   encoding of the image.
            # Runs the softmax tensor by feeding the image_data as input to the graph.
                pool3 = sess.graph.get_tensor_by_name('pool_3:0')
                features = sess.run(pool3,
                                       {'DecodeJpeg/contents:0': image_data})
                return features

        def maybe_download_and_extract():
            """Download and extract model tar file."""
            dest_directory = model_dir
            if not os.path.exists(dest_directory):
                os.makedirs(dest_directory)
            filename = DATA_URL.split('/')[-1]
            filepath = os.path.join(dest_directory, filename)
            if not os.path.exists(filepath):
                def _progress(count, block_size, total_size):
                    sys.stdout.write('\r>> Downloading %s %.1f%%' % (
                          filename, float(count * block_size) / float(total_size) * 100.0))
                    sys.stdout.flush()
                filepath, _ = urllib.request.urlretrieve(DATA_URL, filepath, _progress)
                print()
                statinfo = os.stat(filepath)
                print('Successfully downloaded', filename, statinfo.st_size, 'bytes.')
            tarfile.open(filepath, 'r:gz').extractall(dest_directory)

        maybe_download_and_extract()
        # Creates graph from saved GraphDef.
        create_graph()
        feature_filename = '%s-feature-inception-%d.p'%(
                self.image_path, self.no_of_images)
        if os.path.exists(feature_filename):
            with open(feature_filename, 'rb') as f:
                features, self.image_names = pickle.load(f)
        else:
            features = np.zeros([self.no_of_images, 2048])
            for i in range(self.no_of_images):
                print('image name: %s index: %d/%d' %(
                        self.image_names[i], i, self.no_of_images))
                features[i, :] = run_inference_on_image(image=self.image_names[i]).squeeze()
            with open(feature_filename, 'wb') as f:
                pickle.dump((features, self.image_names), f)
        return features

    # raw image pixels resized to 100x100
    def raw_features(self):
        print('using raw method for feature extraction')
        features = np.zeros([self.no_of_images, 100*100])
        for i, name in enumerate(self.image_names):
            features[i, :] = np.asarray(Image.open(name).resize((100, 100)).
                    convert('L')).reshape(-1,)

    # vgg face pretrained network
    def vggfaces_features(self):
        print('using vggfaces network for feature extraction')
        # Convolution Features
        features = np.zeros([self.no_of_images, 2048])
    #    vgg_model_conv = VGGFace(include_top=False, input_shape=(224, 224, 3), pooling='avg') # pooling: None, avg or max
        # FC7 Features
        vgg_model = VGGFace() # pooling: None, avg or max
        out = vgg_model.get_layer('fc7').output
        vgg_model_fc7 = Model(vgg_model.input, out)

        feature_filename = '%s-feature-vggfaces-%d.p'%(
                self.image_path, self.no_of_images)
        if os.path.exists(feature_filename):
            with open(feature_filename, 'rb') as f:
                features, self.image_names = pickle.load(f)
        else:
            features = np.zeros([self.no_of_images, 4096])
            for i, name in enumerate(self.image_names):
                img = image.load_img(name, target_size=(224, 224))
                x = image.img_to_array(img)
                x = np.expand_dims(x, axis=0)
                x = utils.preprocess_input(x)
                print('image name: %s progress: %d/%d'%(
                        name, i+1, self.no_of_images))
                features[i, :] = vgg_model_fc7.predict(x)
            with open(feature_filename, 'wb') as f:
                pickle.dump((features, self.image_names), f)
        return features


from datetime import timedelta, datetime, tzinfo
from typing import Optional, Sequence, Type, Any, Tuple, Dict, List

import pandas as pd

from feathub.common.exceptions import FeathubException
from feathub.common.types import to_numpy_dtype
from feathub.dsl.ast import ExprAST
from feathub.dsl.expr_parser import ExprParser
from feathub.feature_views.feature import Feature
from feathub.feature_views.sliding_feature_view import SlidingFeatureView
from feathub.feature_views.transforms.agg_func import AggFunc
from feathub.feature_views.transforms.sliding_window_transform import (
    SlidingWindowTransform,
)
from feathub.processors.constants import EVENT_TIME_ATTRIBUTE_NAME
from feathub.processors.local.aggregation_utils import AGG_FUNCTIONS
from feathub.processors.local.ast_evaluator.local_ast_evaluator import LocalAstEvaluator
from feathub.processors.local.time_utils import append_unix_time_column
from feathub.processors.type_utils import cast_dataframe_dtype

# TODO: Unify common classes across all processors.
class AggregationFieldDescriptor:
    """
    Descriptor of a field computed by aggregation.
    """

    def __init__(
        self,
        field_name: str,
        field_data_type: Type,
        expr: str,
        agg_func: AggFunc,
        window_size: timedelta,
        filter_expr: Optional[str],
        limit: Optional[int],
    ) -> None:
        self.field_name = field_name
        self.field_data_type = field_data_type
        self.expr = expr
        self.agg_func = agg_func
        self.window_size = window_size
        self.filter_expr = filter_expr
        self.limit = limit

    @staticmethod
    def from_feature(feature: Feature) -> "AggregationFieldDescriptor":
        transform = feature.transform
        if not (isinstance(transform, SlidingWindowTransform)):
            raise FeathubException(
                f"Cannot convert {feature} to AggregationFieldDescriptor."
            )
        return AggregationFieldDescriptor(
            feature.name,
            to_numpy_dtype(feature.dtype),
            transform.expr,
            transform.agg_func,
            transform.window_size,
            transform.filter_expr,
            transform.limit,
        )

class SlidingWindowDescriptor:
    """
    Descriptor of a sliding window.
    """

    def __init__(
        self,
        step_size: timedelta,
        group_by_keys: Sequence[str],
    ) -> None:
        self.step_size = step_size
        self.group_by_keys = group_by_keys

    @staticmethod
    def from_sliding_window_transform(
        sliding_window_agg: SlidingWindowTransform,
    ) -> "SlidingWindowDescriptor":
        return SlidingWindowDescriptor(
            sliding_window_agg.step_size,
            sliding_window_agg.group_by_keys,
        )

    def __eq__(self, other: object) -> bool:
        return (
            isinstance(other, self.__class__)
            and self.step_size == other.step_size
            and self.group_by_keys == other.group_by_keys
        )

    def __hash__(self) -> int:
        return hash(
            (
                self.step_size,
                tuple(self.group_by_keys),
            )
        )

def evaluate_sliding_window(
    input_df: pd.DataFrame,
    feature_view: SlidingFeatureView,
    window_descriptor: SlidingWindowDescriptor,
    agg_descriptors: List[AggregationFieldDescriptor],
    tz: tzinfo,
    parser: ExprParser,
    ast_evaluator: LocalAstEvaluator,
) -> pd.DataFrame:
    """
    Evaluate the sliding window on the input DataFrame.
    """
    df_copy = input_df.copy()
    append_unix_time_column(
        df_copy,
        feature_view.get_resolved_source().timestamp_field,
        feature_view.get_resolved_source().timestamp_format,
        tz,
    )

    agg_df = df_copy.sort_values(by=[EVENT_TIME_ATTRIBUTE_NAME])
    if len(window_descriptor.group_by_keys) > 0:
        agg_df = agg_df.groupby(by=window_descriptor.group_by_keys).apply(
            lambda df: _sliding_window_func(
                df=df,
                sliding_window_descriptor=window_descriptor,
                agg_field_descriptors=agg_descriptors,
                tz=tz,
                parser=parser,
                ast_evaluator=ast_evaluator,
            )
        )
    else:
        agg_df = _sliding_window_func(
            df=agg_df,
            sliding_window_descriptor=window_descriptor,
            agg_field_descriptors=agg_descriptors,
            tz=tz,
            parser=parser,
            ast_evaluator=ast_evaluator,
        )

    agg_df = agg_df.reset_index(drop=True)

    # Compute the timestamp field with the given timestamp format from event
    # time(window time).
    if feature_view.timestamp_field is not None:
        if feature_view.timestamp_format == "epoch":
            agg_df[feature_view.timestamp_field] = agg_df[
                EVENT_TIME_ATTRIBUTE_NAME
            ].transform(lambda unix_time: int(unix_time))
        elif feature_view.timestamp_format == "epoch_millis":
            agg_df[feature_view.timestamp_field] = agg_df[
                EVENT_TIME_ATTRIBUTE_NAME
            ].transform(lambda unix_time: int(unix_time * 1000))
        else:
            agg_df[feature_view.timestamp_field] = agg_df[
                EVENT_TIME_ATTRIBUTE_NAME
            ].transform(
                lambda unix_time: datetime.fromtimestamp(unix_time).strftime(
                    feature_view.timestamp_format
                )[:-3]
            )

    agg_df = agg_df.drop([EVENT_TIME_ATTRIBUTE_NAME], axis=1)
    return agg_df

def _sliding_window_func(
    df: pd.DataFrame,
    sliding_window_descriptor: SlidingWindowDescriptor,
    agg_field_descriptors: Sequence[AggregationFieldDescriptor],
    tz: tzinfo,
    parser: ExprParser,
    ast_evaluator: LocalAstEvaluator,
) -> Optional[pd.DataFrame]:

    if df.shape[0] <= 0:
        return None

    df_copy = df.copy()

    # We assign row base on the local timestamp millis instead of unix time so that the
    # windows are aligned with 1970-01-01 00:00:00 at the current time zone.
    df_copy[EVENT_TIME_ATTRIBUTE_NAME] = df_copy[EVENT_TIME_ATTRIBUTE_NAME].transform(
        lambda t: int((t + _get_utc_offset_seconds(t, tz)) * 1000)
    )

    first_row = df_copy.iloc[0]
    keys_dict = {k: first_row[k] for k in sliding_window_descriptor.group_by_keys}
    agg_field_names = [d.field_name for d in agg_field_descriptors]

    filter_ast_map: Dict[AggregationFieldDescriptor, ExprAST] = {
        descriptor: parser.parse(descriptor.filter_expr)
        for descriptor in agg_field_descriptors
        if descriptor.filter_expr is not None
    }
    agg_result_dtypes = {
        agg_field_descriptor.field_name: agg_field_descriptor.field_data_type
        for agg_field_descriptor in agg_field_descriptors
    }
    step_size_millis = int(sliding_window_descriptor.step_size.total_seconds() * 1000)

    for agg_field_descriptor in agg_field_descriptors:
        expr_node = parser.parse(agg_field_descriptor.expr)
        df_copy[agg_field_descriptor.field_name] = df_copy.apply(
            lambda r: ast_evaluator.eval(expr_node, r), axis=1
        )

    res_df = pd.DataFrame()
    idx_map: Dict[AggregationFieldDescriptor, Tuple[int, int]] = {}
    cur_window_end = _get_first_window_end_time(
        first_row[EVENT_TIME_ATTRIBUTE_NAME], sliding_window_descriptor.step_size
    )

    while True:
        agg_res: Dict[str, Any] = {}
        all_reach_end = True
        for agg_field_descriptor in agg_field_descriptors:
            agg_func = AGG_FUNCTIONS.get(agg_field_descriptor.agg_func, None)
            if agg_func is None:
                raise RuntimeError(
                    f"Unsupported agg function {agg_field_descriptor.agg_func}."
                )

            left_idx, right_idx = idx_map.get(agg_field_descriptor, (0, 0))

            # Advance left idx
            while (
                right_idx < df_copy.shape[0]
                and df_copy.iloc[right_idx][EVENT_TIME_ATTRIBUTE_NAME] < cur_window_end
            ):
                right_idx += 1

            # Advance right idx
            window_size_millis = int(
                agg_field_descriptor.window_size.total_seconds() * 1000
            )
            while (
                left_idx < right_idx
                and df_copy.iloc[left_idx][EVENT_TIME_ATTRIBUTE_NAME]
                < cur_window_end - window_size_millis
            ):
                left_idx += 1

            if left_idx >= df_copy.shape[0] and right_idx >= df_copy.shape[0]:
                # All rows are processed for this aggregation descriptor
                rows_in_window = df_copy.iloc[0:0]
            else:
                all_reach_end = False
                rows_in_window = df_copy.iloc[left_idx:right_idx]

            if agg_field_descriptor in filter_ast_map and rows_in_window.shape[0] > 0:
                # Filter the rows in the window
                rows_in_window = rows_in_window[
                    rows_in_window.apply(
                        lambda r: ast_evaluator.eval(
                            filter_ast_map.get(agg_field_descriptor), r
                        ),
                        axis=1,
                    )
                ]

            limit = agg_field_descriptor.limit
            if limit is not None and limit < rows_in_window.shape[0]:
                # Limit the rows in the window
                rows_in_window = rows_in_window.iloc[-limit:]
            agg_res[agg_field_descriptor.field_name] = agg_func(
                rows_in_window[agg_field_descriptor.field_name].to_list()
            )

            # Update indexes
            idx_map[agg_field_descriptor] = (left_idx, right_idx)

        last_row_agg_res = None
        if res_df.shape[0] > 0:
            last_row_agg_res = dict(res_df.iloc[-1][agg_field_names])

        if agg_res != last_row_agg_res:
            row = pd.DataFrame(
                data={
                    **keys_dict,
                    **{agg_field: [agg_val] for agg_field, agg_val in agg_res.items()},
                    EVENT_TIME_ATTRIBUTE_NAME: [cur_window_end - 1],
                }
            )
            res_df = res_df.append(cast_dataframe_dtype(row, agg_result_dtypes))

        if all_reach_end:
            break

        cur_window_end += step_size_millis

    # Convert local timestamp mills back to unix time
    res_df[EVENT_TIME_ATTRIBUTE_NAME] = res_df[EVENT_TIME_ATTRIBUTE_NAME].transform(
        lambda t: t / 1000.0 - _get_utc_offset_seconds(t / 1000.0, tz)
    )
    res_df.index = res_df[EVENT_TIME_ATTRIBUTE_NAME]
    return res_df

def _get_utc_offset_seconds(unix_time: float, zone: tzinfo) -> float:
    return zone.utcoffset(datetime.fromtimestamp(unix_time)).total_seconds()

def _get_first_window_end_time(
    row_time: int,
    step_size: timedelta,
) -> int:
    step_size_millis = int(step_size.total_seconds() * 1000)
    first_window_end = row_time - row_time % step_size_millis + step_size_millis
    return first_window_end


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
sys.path.append(sys.path[0]+'/..')
import pandas as pd
import progressbar
import re
from bs4 import BeautifulSoup
from utils.request import get_request

# 验证城市办公楼盘获取有效性
def validation(city_dct):
    # 定义关键字
    msg = '办公楼盘'
    # 初始化有效城市字典
    effective_cities = {}
    # 初始化progressbar
    # 定义进度条的显示样式
    widgets = ['有效验证中 : ',
            progressbar.Percentage(),
            " ",
            progressbar.Bar(),
            " ",
            progressbar.ETA()]
    # 创建进度条并开始运行
    pbar = progressbar.ProgressBar(maxval=len(city_dct),
                                widgets=widgets).start()
    i = 0
    for city,abbr in city_dct.items():
        url = 'https://%s.anjuke.com/'%abbr
        content = get_request(url, anonymous=True)
        i += 1
        pbar.update(i)
        navigation_info = re.search(r'<a(.*?)>%s</a>'%msg, content)
        if navigation_info:
            url = re.search(r'href=(.*?)>', navigation_info.group(0)).group(1)
            effective_cities[city] = {'abbr':abbr, 'url':url}
    # 结束进度条
    pbar.finish()
    return effective_cities

if __name__ == '__main__':
    # 初始化城市链接页
    url = 'https://www.anjuke.com/sy-city.html'
    # 城市列表
    content = BeautifulSoup(get_request(url,True), features='lxml')
    cities = content.find('div', class_='letter_city').find_all('a')
    # 城市缩写清洗
    city_map = {}
    for city in cities:
        city_name = city.text.strip()
        city_abbr = city.attrs['href'].replace('https://','').replace('.anjuke.com','')
        city_map[city_name] = city_abbr
    # 安居客中会把小城市的缩写归类到附近大城市，例如：临安，富阳归为杭州
    # 因此需要对city_map进行清洗，仅保留大城市的对应关系
    # Step1 统计具有重复缩写城市的名称集合
    # city_df = pd.DataFrame.from_dict(city_map,
    #                                  orient='index',
    #                                  columns = ['abbr']).reset_index().rename(columns = {'index':'city'}) # 字典转dataframe
    # tmp = city_df.groupby('abbr')['city'].agg(['count', list]).reset_index() # 统计abbr重复个数及对应城市列表
    # tmp.where(tmp['count'] != 1).dropna() # 仅观测count不为1的城市缩写

    # Step2 观测发现，一下城市冗余
    duplicated_cities = ['巴州', '农安', '阿坝州', '阿坝州', '大邑', '金堂', '铜梁', '丰都',
                        '长寿', '巢湖', '涪陵', '南川', '永川', '綦江', '黔江', '万州', '江津',
                        '合川', '宁乡', '普兰店', '肇源', '长乐', '连江', '平潭', '白沙县',
                        '儋州市', '澄迈县', '定安', '琼中', '屯昌', '文昌市', '淳安', '富阳',
                        '临安', '桐庐', '肥东', '肥西', '庐江', '长丰', '龙门', '平阴',
                        '济阳', '商河', '宜良', '文安', '永登', '榆中', '汝阳', '当涂',
                        '宾阳', '横县', '宁海', '新建', '即墨', '胶南', '晋安', '陵水',
                        '保亭', '东方市', '上虞', '无极', '辛集', '元氏', '辽中', '新民',
                        '乐亭', '滦县', '周至', '户县', '蓝田', '丰县', '睢宁', '江都',
                        '中牟', '巩义']
    # 安居客涵盖的所有城市
    city_dct = {key: city_map[key] for key in city_map.keys() if key not in duplicated_cities}
    # 可直接通过导航栏中的 "办公楼盘" 获取写字楼的城市
    effective_cities = validation(city_dct)
    # 上述字典转DataFrame
    city_df = pd.DataFrame.from_dict(city_dct,
                                    orient='index',
                                    columns=['abbr']).reset_index().rename(columns = {'index':'city'}) # 字典转dataframe
    effective_cities_df = pd.DataFrame.from_dict(effective_cities,
                                             orient='index').reset_index() # 字典转dataframe
    # 输出保存
    city_df.to_csv('%s/../data/01_cities.csv'%sys.path[0], encoding='utf-8', index=False)
    effective_cities_df.to_csv('%s/../data/02_effective_cities.csv'%sys.path[0], encoding='utf-8', index=False)

03安居客全国写字楼信息/code/01-城市信息获取.py

01-城市信息获取.py


from .game import Game
from .cards import Deck
import numpy as np
import pandas as pd
import pickle
from tqdm.notebook import tqdm

def runSimulations(numberOfPlayers: int, numberOfSimulations: int, append: dict = None, verbose: bool = True) -> dict:
    """Run simulations of games

    Parameters
    ----------
    numberOfPlayers : int
        Number of players in each game
    numberOfSimulations : int
        Number of simulations of the game
    append : dict, optional
        Whether results are to be added to existing results, by default None
    verbose : bool, optional
        Whether debugging information is to be printed, by default True

    Returns
    -------
    dict
        Results for each possible hand
    """

    if append is None:
        simulations = {item:np.array([0,0,0]) for item in Game.HAND_RANKINGS.keys()}
    else:
        simulations = append

    if(verbose):
        if append is None:
            print(f"Running {numberOfSimulations} simulations for a game of {numberOfPlayers} players")
        else:
            print(f"Adding {numberOfSimulations} simulations for a game of {numberOfPlayers} players")

    for _ in tqdm(range(numberOfSimulations)):
        game = Game(numberOfPlayers)
        res = game.simulateGame()
        for item in res:
            simulations[item]=np.add(simulations[item],res[item])

    return simulations

def saveRaw(results: dict, numberOfPlayers: int, verbose: bool = True):
    """Save the dictionary of results

    Parameters
    ----------
    results : dict
        Results for each hand
    numberOfPlayers : int
        Number of players in the game
    verbose : bool, optional
        Whether debugging information is to be printed, by default True
    """

    fileName = 'results/winrate_'+str(numberOfPlayers)+"_raw.pickle"
    with open(fileName, 'wb') as handle:
        pickle.dump(results, handle, protocol=pickle.HIGHEST_PROTOCOL)

    if(verbose):
        print(f"Raw data file saved at {fileName}!")

def clean(results: dict, numberOfPlayers: int, verbose: bool = True) -> pd.DataFrame:
    """Cleans the results and converts it to a DataFrame

    Parameters
    ----------
    results : dict
        Results for each hand
    numberOfPlayers : int
        Number of players in the game
    verbose : bool, optional
        Whether debugging information is to be printed, by default True

    Returns
    -------
    pd.DataFrame
        Cleaned DataFrame with the results for each hand
    """

    names = list(Game.HAND_NAMES.values())

    cleaned = pd.DataFrame.from_dict(results,orient='index', columns = ['wins','ties','total_games'])
    cleaned.index.name = 'hand'
    cleaned.insert(0, 'name', names)
    cleaned['winrate'] = cleaned['wins']/cleaned['total_games']
    cleaned['winrate'] = round(cleaned['winrate'], 4)
    cleaned.index = [", ".join(list(item)) for item in cleaned.index]

    if(verbose):
        print("Converted dictionary to DataFrame and cleaned it!")

    return cleaned

def saveCleaned(results: pd.DataFrame, numberOfPlayers: int, verbose: bool = True):
    """Save the cleaned results to a CSV file

    Parameters
    ----------
    results : pd.DataFrame
        Cleaned DataFrame with the results for each hand
    numberOfPlayers : int
        Number of players in the game
    verbose : bool, optional
        Whether debugging information is to be printed, by default True
    """

    fileName = 'results/winrate_'+str(numberOfPlayers)+"_cleaned.csv"
    results.to_csv(fileName)

    if(verbose):
        print(f"Cleaned data file saved at {fileName}!")

def loadRaw(numberOfPlayers: int, verbose: bool = True) -> dict:
    """Load the dictionary of results

    Parameters
    ----------
    numberOfPlayers : int
        Number of players in the game
    verbose : bool, optional
        Whether debugging information is to be printed, by default True

    Returns
    -------
    dict
        Dictionary with results for each hand
    """

    fileName = 'results/winrate_'+str(numberOfPlayers)+"_raw.pickle"
    with open(fileName, 'rb') as handle:
        res = pickle.load(handle)

    if(verbose):
        print(f"Data loaded from {fileName}!")

    return res

def simulationsRun(numberOfPlayers: int, verbose: bool = True):
    """Get the number of simulations that have been run for games with a certain number of players

    Parameters
    ----------
    numberOfPlayers : int
        Number of players in the game
    verbose : bool, optional
        Whether debugging information is to be printed or returned, by default True

    Returns
    -------
    [type]
        [description]
    """

    df = pd.read_csv('results/winrate_'+str(numberOfPlayers)+"_cleaned.csv")
    num = int(df['total_games'].sum()/numberOfPlayers)

    if(verbose):
        print(f"{num} simulations have been run for {numberOfPlayers} players")
    else:
        return num


from __future__ import annotations

from typing import TYPE_CHECKING, Union

from discord import ButtonStyle, Interaction, Member, User
from discord.ui import Button, button

from .View import FroggeView

if TYPE_CHECKING:
    pass
################################################################################

__all__ = ("ConfirmCancelView",)

################################################################################
class ConfirmCancelView(FroggeView):

    def __init__(self, owner: Union[Member, User], *args, **kwargs):
        super().__init__(owner, *args, close_on_complete=True, **kwargs)

    @button(
        style=ButtonStyle.success,
        label="Confirm",
        disabled=False,
        row=0
    )
    async def confirm(self, btn: Button, interaction: Interaction):
        self.value = True
        self.complete = True

        await interaction.response.edit_message()
        await self.stop()  # type: ignore

    @button(
        style=ButtonStyle.danger,
        label="Cancel",
        disabled=False,
        row=0
    )
    async def cancel(self, btn: Button, interaction: Interaction):
        self.value = False
        self.complete = True

        await interaction.response.edit_message()
        await self.stop()  # type: ignore

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


'''
My Utils

Written by Z. Duputel, September 2013
'''

############################################################################
#
#                  W phase source inversion package
#                               -------------
#
#        Main authors: Zacharie Duputel, Luis Rivera and Hiroo Kanamori
#
# (c) California Institute of Technology and Universite de Strasbourg / CNRS
#                                  April 2013
#
#    Neither the name of the California Institute of Technology (Caltech)
#    nor the names of its contributors may be used to endorse or promote
#    products derived from this software without specific prior written
#    permission
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program 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 General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
############################################################################

# Externals
from os.path import exists,isdir
from numpy   import ndarray
import shutil
import os
import re

def grep(chaine, file):
    """
    Returns lines in file matching chaine
    """
    out = [];
    template = re.compile(chaine)
    fid  = open(file, 'r')
    for line in fid:
        if template.match(line):
            out.append(line)
    fid.close()

    # All done
    return(out)

def rm(ifiles):
    '''
    Remove file(s) or directory(ies)
    Args:
         ifiles:  file, directory or list of files, directories
    '''

    if type(ifiles)!=list and type(ifiles)!=ndarray:
        ifiles = [ifiles]
    for ifile in ifiles:
        if exists(ifile):
            if isdir(ifile):
                shutil.rmtree(ifile)
            else:
                os.remove(ifile)

    # All done
    return

def mkdir(idir,pflag=False):
    '''
    Create directory
    Args:
         idir:   directory to be created
         pflag: (optional, default=False)
               False: will     remove directory if exists
               True:  will NOT remove directory if exists
    '''

    if pflag and not exists(idir):
        os.mkdir(idir)
    else:
        rm(idir)
        os.mkdir(idir)

    # All done
    return

class parseConfigError(Exception):
    """
    Raised if the config file is incorrect
    """
    pass

def parseConfig(cfg_file):
    '''
    Parse my config files and returns a config dictionary
    Args:
         cfg_file: configuration filename
    '''

    # Check if cfg_file exists
    assert exists(cfg_file), 'Cannot read %s: No such file'%(cfg_file)

    # Fill the config dictionary
    config = {}
    try:
        config_lines = open(cfg_file, 'r').readlines()
        for line in config_lines:
            if line.find('#')==0:
                continue
            if line.rstrip():
                key,value = line.strip().split(':')
                key   = key.strip()
                value = value.strip()
                if key in config:
                    config[key] = [config[key]]
                    config[key].append(value)
                else:
                    config[key]=value
    except:
        raise parseConfigError('Incorrect format in %s!\n'%cfg_file)

    # All done
    return config


'''
Usando as funções criadas nos exercícios 13, 14 e 15. Crie uma composição de funções que resolva os 3 casos:

EX:
f(x) -> Union[ID, goiabada, queijo, romeu e julieta]
f(3) -> ‘queijo’
f(5) -> ‘goiabada’
f(15) -> ‘romeu e julieta’
f(19) -> 19
'''

def Union(id):
    if ((id % 5) == 0) and ((id % 3) == 0):
        return 'Romeu e Julieta'
    elif (id % 5) == 0:
        return 'Goiabada'
    elif (id % 3) == 0:
        return 'queijo'
    else:
        return id

id = int(input('Digite um número: '))

print(Union(id))


class Joueur:

    def __init__(self, nom, age, taille, poste) -> None:
        self.nom = nom
        self.age = age
        self.taille = taille
        self.poste = poste

    def __str__(self) -> str:
        return f"{self.nom}: {self.age} {self.taille} {self.poste}"

class VoleyPlayer(Joueur):
    def __init__(self, nom, age, taille, poste, sex) -> None:
        super().__init__(nom, age, taille, poste)
        self.sex = sex

class Equipe:
    def __init__(self, nom, joueurs=None) -> None:
        self.nom = nom
        if joueurs is None:
            self.joueurs = []
        else:
            self.joueurs = joueurs

    def add_joueur(self, joueur):
        if isinstance(joueur ,Joueur):
            if joueur not in self.joueurs:
                self.joueurs.append(joueur)
        return self

    def remove_joueur(self, joueur):
       if isinstance(joueur ,Joueur):
            for i in range(len(self.joueurs)) :
                if joueur == self.joueurs[i]:
                    del self.joueurs[i]
                    return self

    def moyenne_age(self):
        sum = 0
        for joueur in self.joueurs:
            sum += joueur.age
        return sum / len(self.joueurs)

    def joueur_plus_grand(self):
        max = self.joueurs[0]
        for joueur in self.joueurs:
            if joueur.taille > max.taille:
                max = joueur
        return max

    def joueur_plus_petit(self):
        min = self.joueurs[0]
        for joueur in self.joueurs:
            if joueur.taille < min.taille:
                min = joueur
        return min

    def joueur_au_poste(self, poste):
        for joueur in self.joueurs:
            if joueur.poste == poste:
                return joueur
        return None

j1 = Joueur("a", 20, 150, "a1")
j2 = Joueur("b", 25, 160, "b1")
j3 = Joueur("c", 30, 180, "c1")

e = Equipe("shit", [j1])

e.add_joueur(j2).add_joueur(j3)

for j in e.joueurs:
    print(j)
print("*" *10)
print(e.moyenne_age())
print(e.joueur_plus_grand())
print(e.joueur_plus_petit())
print(e.joueur_au_poste("c1"))

print("*" *10)

e.remove_joueur(j3).remove_joueur(j2)

for j in e.joueurs:
    print(j)

print("*" *10)
e2 = Equipe("Salamieh")
ibrahim = VoleyPlayer("hendawi", 30, 180, "attackeur", "t")
ali = VoleyPlayer("hamwai", 28, 150, "passeur", "t")

e2.add_joueur(ibrahim).add_joueur(ali)

for j in e2.joueurs:
    print(j)

print("*" *10)
print(e2.moyenne_age())
print(e2.joueur_plus_grand())
print(e2.joueur_plus_petit())
print(e2.joueur_au_poste("passeur"))


import bge
from bge.logic import expandPath, globalDict
from scripts.data_loader import load_data

load_data()

def parent_to_group(cont):
	""" Parents the current object to the dupli group object (usually an empty).

	Useful to transform the dupli group object instead of searching group members to perform the transform. For better results, make sure the current object is parent of all the other objects in the group. """

	# Basic
	own = cont.owner

	# Sensors
	s_sensor = cont.sensors[0].positive

	if s_sensor:

		if own.groupObject != None:
			own.setParent(own.groupObject)

def is_group_instance(obj):
	""" Checks if the current object is a dupli group instance.

	This function avoids repetitions of conditions on other functions. """

	if 'groupObject' in dir(obj):

		if obj.groupObject != None:
			return True

		else:
			return False

	else:
		return False


#!/usr/local/bin/python3

#Print  newline,  word,  and byte counts for each FILE, and a total line if more than one FILE is
#  specified.  With no FILE, or when FILE is -, read standard input.  A word is  a  non-zero-length
# sequence  of characters delimited by white space.  The options below may be used to select which
# counts are printed, always in the following order: newline, word, character, byte, maximum  line
# length.
#       -m, --chars -   print the character counts
#       -l, --lines     print the newline counts
#      --files0-from=F  -  read  input  from  the  files specified by NUL-terminated names in file F; If F is - then
              #read names from standard input
#       -L, --max-line-length -    print the length of the longest line
#       -w, --words -   print the word counts
#       --help display this help and exit

# We will assume that all arguments taken in are beginning with -
# First argument is the file name that is running

from sys import argv

options=[]
z=""
x=""
files=[]
readLine=[]
longestLine=0
wordCount={"words":0, "chars":0,"lines":0}
totalCount={"words":0, "chars":0,"lines":0}

def printOut(fileDetails, file) :
    formatting=""
    arguements=""
    if options==[] :
        formatting="%5i%5i%5i%16s"
        print(formatting % (fileDetails["lines"], fileDetails["words"], fileDetails["chars"], file))
        return
    if "-l" in options :
        formatting+="%5s"
        arguements=str(fileDetails["lines"])
    if "-w" in options :
        formatting+="%5s"
        arguements=str(fileDetails["words"])
    if "-m" in options :
        formatting+="%5s"
        arguements=str(fileDetails["chars"])
    if "-L" in options :
        formatting+="%5s"
        arguements+=str(longestLine)
    if formatting !="" :
        arguements+=" "+fileName
        print(formatting % (arguements))
    else :
         print("Please enter valid arguments :  File name(s), -m, -l, -L, -w")
    return

if len (argv)==1 :
    print("Please enter valid arguments :  File name(s), -m, -l, -L, -w")
    exit

for arg in argv:
    if arg[0]!="-":
        files.append(arg)
    else :
        options.append(arg)
del files[0]

try :
    for fileName in files:
        theFile=open(fileName,"r")
        while readLine!="" :
            readLine=theFile.readline()
            wordCount["lines"]+=1
            for words in readLine.split() :
                wordCount["words"]+=1
                wordCount["chars"]+=len(words)
                if longestLine<len(words) :
                    longestLine=len(words)
        printOut(wordCount, fileName)
        for items in wordCount :
            totalCount[items]+=wordCount[items]
            wordCount[items]=0
        theFile.close
    if len(files)>1 :
        printOut(totalCount, "total")

# Here is the files list is greater than one print the total library

except FileNotFoundError :
    print (fileName, "not Found - Please enter a valid file name!")
except IOError :
    print ("Error with file!!")
except TypeError :
    print ("Please enter valid arguments :  File name(s), -m, -l, -L, -w")
except EOFError :
    theFile.close


from cred.Cred import Cred
import mysql.connector as ms
from mysql_connection import mysql_connection

def logintable(mycon,cur):
    sql='''
    create table login(
    first_name varchar(50) Not Null,
    last_name varchar(50),
    username varchar(50) primary key,
    password varchar(50)
    )
    '''
    cur.execute(sql)
    mycon.commit()

def email(mycon,cur):
    sql ='''
    create table mails(
    s_no int primary key auto_increment,
    mail_date varchar(10) not null,
    sender varchar(50) not null,
    receiver varchar(50) not null,
    subject varchar(50),
    text varchar(500)
    )
    '''
    cur.execute(sql)
    mycon.commit()

def main():
    mycon,cur = mysql_connection()
    logintable(mycon,cur)
    email(mycon,cur)
    print("Table Created Successfully")

if(__name__ == "__main__"):
    main()


import random
# WHILE LOOP
random_number = random.randint(1, 3)
limit = int(input('How many time try: '))
tried = 0

while tried < limit:
  entry = int(input('Your guess: '))
  tried += 1

  if entry == random_number:
    print("You won")
    break
else:
  print('Sorry, you didn\'t guess right')

# FOR LOOP
# pattern

line = 5
for i in range(5):
  for j in range(i+1):
    print("*", end='')
  print()


import sys

def boj_2920(input):
    diff = input[0]-input[1]
    for i in range(2, len(input)-1):
        if input[i]-input[i+1] != diff:
            return "mixed"

    return "ascending" if diff == -1 else "descending"

if __name__ == "__main__":
    input = list(map(int, sys.stdin.readline().split()))
    output = boj_2920(input)
    print(output)


import numpy as np
import pyGLM.gabor as gabor
from scipy.stats import poisson
from os.path import join

"""
simulates a coupled GLM model
author: Niru Maheswaranathan
4:31 PM Nov 13, 2013
"""

def f_df(theta, data, params):
    """
    log-likelihood objective and gradient, assuming Poisson noise

    function [fval grad] = f_df(theta, data, params)
    % Computes the Poisson log-likelihood objective and gradient
    % for the generalized linear model (GLM)
    """

    ## get out parameters
    ds = params['stim_dim']
    dh = params['hist_dim']
    N  = params['numNeurons']
    M = data['x'].shape[0]

    # offset for numerical stability
    epsilon = 1e-20

    ## simulate the model at theta
    rhat, expu = simulate(theta, params, data)
    rdt  = rhat*params['dt']                        # rdt is: M by N

    ## compute objective value (negative log-likelihood)
    fval = np.sum(rdt - data['n']*np.log(rdt + epsilon)) / M

    ## compute gradient
    grad = dict()

    # temporarily store different in rate vs. observed spike count (used for gradient computations)
    #rateDiff = (rdt - data['n']).T                  # rateDiff is: N by M  (used for exponential nonlinearity)
    rateDiff = ((params['dt'] - data['n']/rhat)*expu).T                  # rateDiff is: N by M  (used for soft linear rectifying nonlinearity)

    # gradient for stimulus parameters
    grad['w'] = rateDiff.dot(data['x']).T / M       # grad['w'] is: ds by N

    # gradient for the offset term
    grad['b'] = np.sum(rateDiff.T, axis=0) / M                 # grad['b'] is:  1 by N

    # gradient for history terms
    grad['h'] = np.zeros((dh,N))                    # grad['h'] is: dh by N

    # gradient for coupling terms
    nprev = np.vstack(( np.zeros((1,N)), data['n'][1:,:] )) # (nprev is: M by N)
    grad['k'] = rateDiff.dot(nprev).T / M           # grad['k'] is:  N by N

    # for each neuron
    for nrnIdx in range(N):

        # cross-correlate rate vector
        spkCountArray = np.squeeze(data['n'][:,nrnIdx])
        rateDiffArray = np.squeeze(rateDiff[nrnIdx,:])

        # at each time offset
        for delta in np.arange(1,dh+1):

            # average product of spike counts and rateDiff at each point in time
            grad['h'][dh-delta,nrnIdx] = np.sum( spkCountArray[:-delta] * rateDiffArray[delta:] , axis=0) / M

    # check for nans
    gradCheck(grad)

    return fval, grad

def setParameters(n = 10, ds = 1024, dh = 50, m = 1000, dt = 0.1):

    """
    Parameters
    ----------
    n : number of neurons
    ds: dimension of the stimulus
    dh: dimension of the coupling filters
    m : minibatch size (number of training examples)

    """

    # define the parameters dictionary
    params = {'numNeurons': n, 'stim_dim': ds, 'hist_dim': dh, 'numSamples': m, 'dt': dt, 'alpha': 0.1}

    # nonlinearity
    params['f'] = logexp # (soft-linear-rectifying)
    #params['f'] = np.exp # exponential

    return params

def generateModel(params, filterType='gabor'):

    """

    Generates model parameters
    --------------------------

    Returns a theta dictionary:
    theta['w']:    stimulus filters for the n neurons, (ds x N) matrix
    theta['b']:    stimulus offset  for the n neurons, ( 1 x N) matrix
    theta['h']:    history  filters for the n neurons, (dh x N) matrix
    theta['k']:    coupling filters for the n neurons, ( N x N) matrix

    """

    ## get out parameters
    ds = params['stim_dim']
    dh = params['hist_dim']
    N  = params['numNeurons']
    M  = params['numSamples']

    ## store model as dictionary
    theta = {}

    ## build the filters:

    # stimulus filters for each of n neurons
    if filterType is 'random':
        theta['w'] = np.random.randn(ds, N)

    elif filterType is 'sinusoid':
        theta['w'] = np.zeros((ds, N))
        for nrnIdx in range(N):
            theta['w'][:,nrnIdx] = np.sin( np.linspace(0,2*np.pi,ds) + 2*np.pi*np.random.rand() )

    elif filterType is 'gabor':
        theta['w'] = np.zeros((ds, N))
        for nrnIdx in range(N):

            # random parameters
            mySigma = np.random.rand()*0.6 + 0.1
            myFreq  = np.random.rand()*5
            myPhase = np.random.rand()*2*np.pi

            # build filter
            theta['w'][:,nrnIdx] = gabor.buildGabor(ds, sigma=mySigma, freq=myFreq, phase=myPhase)

    else:
        print('WARNING: unrecognized filter type. Using random values instead.')
        theta['w'] = 0.2*np.random.randn(ds, N)

    # pick out two pools
    fracInh = 0.2
    numInh = np.ceil( fracInh * N )
    numExc = N - numInh

    # normalize filters
    #theta['w'] = theta['w'] / np.linalg.norm( theta['w'], axis=0 )
    theta['w'] = 1.5*theta['w'] / np.sqrt(np.sum(theta['w']**2, axis=0))

    # offset (scalar)
    #theta['b'] = -1*np.ones((1,N)) + 0.25*np.random.randn(1,N)
    theta['b'] = np.zeros((1,N))

    theta['b'][0,:numInh] = 0.15*np.ones((1,numInh))
    theta['b'][0,numInh:] = -5 + 3*np.random.rand(1,numExc)

    # history (self-coupling) filters for each of n neurons
    theta['h'] = np.zeros( (dh,N) )

    # random decay (linear)
    #theta['h'][:,:numInh] = -0.2*np.sort( np.random.rand( dh, numInh ), axis=0 )
    #theta['h'][:,numInh:] = -0.5*np.sort( np.random.rand( dh, numExc ), axis=0 )

    # exponential decay
    tau = np.linspace(1,0,dh).reshape( (dh,1) )
    theta['h'][:,:numInh] = -0.2*np.exp(-10*tau.dot( np.ones( (1, numInh) )))
    theta['h'][:,numInh:] = -0.5*np.exp(-5*tau.dot(  np.ones( (1, numExc) )))

    # inhibitory connections
    inhK = -5.5*np.random.rand(N, numInh) / numInh

    # excitatory (sparse) connections
    excK = 10*np.random.rand(N, numExc) / (numExc * params['alpha'])
    temp =    np.random.rand(N, numExc)
    excK[temp >= params['alpha']] = 0

    # coupling filters - stored as a (n by n) matrix
    theta['k'] = np.hstack(( inhK, excK )).T
    theta['k'] -= np.diag(np.diag(theta['k']))

    return theta

def logexp(u):
    #for stability
    epsilon = 1e-6

    output = np.log( 1 + np.exp( u )) + epsilon
    output[u > 50] = u[u > 50]
    return output

def loadExternalData(stimFile, ratesFile, shapes, baseDir='.'):

    data = dict()

    # load stimuli
    data['x'] = np.memmap(join(baseDir, stimFile), dtype='uint8', mode='r', shape=tuple(shapes['stimSlicedShape']))

    # load rates
    data['n'] = np.memmap(join(baseDir, ratesFile), dtype='uint8', mode='r', shape=tuple(shapes['rateShape']))[40:,:]

    return data

def generateData(theta, params):

    """
    Generates stimuli and draws spike counts from the model
    -------------------------------------------------------
    """

    ## get out parameters

    # constants
    ds = params['stim_dim']
    dh = params['hist_dim']
    N  = params['numNeurons']
    M  = params['numSamples']

    # nonlinearity
    f = params['f']

    # model parameters
    w = theta['w']
    b = theta['b']
    h = theta['h']
    k = theta['k']

    # offset for numerical stability
    epsilon = 1e-20

    ## store output in a dictionary
    data = {}

    # output
    data['n'] = np.zeros( (M, N) )                   # spike history
    data['r'] = np.zeros( (M, N) )                   # spike history

    # generate stimuli
    #data['x'] = 0.2*np.random.randn(M, ds)         # Gaussian white noise
    data['x'] = genPinkNoise(M, np.sqrt(ds), params['dt']).reshape( (M, ds) )

    # compute stimulus projection
    #u = w.T.dot(data['x'].T).T            # (u is: M by N)
    u = data['x'].dot(w)

    # the initial rate (no history)
    data['n'][0,:] = poisson.rvs( f( u[0,:] + b ) )
    data['r'][0,:] = f( u[0,:] + b )

    # the next rate (one history point)
    data['n'][1,:] = poisson.rvs( f( u[1,:] + data['n'][0,:]*h[-1,:] + b ) )
    data['r'][1,:] = f( u[1,:] + data['n'][0,:]*h[-1,:] + b )

    # simulate the model (in time)
    for j in np.arange(2,M):

        # store output
        v = np.zeros((1,N))

        # compute history weights
        if j < dh+1:
            v = np.sum( data['n'][:j,:]*h[-j:] , axis=0)
            # for each neuron
            #for nrnIdx in range(N):
                #n1 = np.squeeze(data['n'][0:j,nrnIdx])
                #n2 = np.squeeze(h[:,nrnIdx])
                #v[0,nrnIdx] = np.correlate( n1 , n2 , 'valid' )[0]

        else:
            # for each neuron
            v = np.sum(data['n'][j-dh:j,:]*(h), axis=0)

        # print out contributions
        #print('stim: %g\thistory: %g\tcoupling: %g\tbias: %g'%(np.linalg.norm(u[j,:]), np.linalg.norm(v), np.linalg.norm(data['n'][j-1,:].dot(k)), np.linalg.norm(b)))

        # compute model firing rate
        r = f( u[j,:] + v + b + data['n'][j-1,:].dot(k) ) + epsilon
        data['r'][j,:] = r.copy()

        # cap spike count
        maxVal = 10
        r[r > maxVal] = maxVal

        # draw spikes
        spikes = poisson.rvs(r)

        # store
        data['n'][j,:] = spikes

    return data

def simulate(theta, params, data):

    """
    Simulates the output of the GLM given true stimulus/response
    ------------------------------------------------------------
    """

    ## get out parameters

    # constants
    ds = params['stim_dim']
    dh = params['hist_dim']
    N  = params['numNeurons']
    M = data['x'].shape[0]

    # nonlinearity
    f = params['f']

    # model parameters
    w = theta['w']
    b = theta['b']
    h = theta['h']
    k = theta['k']

    # get stimuli, offset, and spike counts
    x = data['x']           # stimuli       (x is: M by ds)
    n = data['n']           # spike counts  (n is: M by N)

    # make sure we have a reasonable number of samples
    if M < dh:
        print('Error: minibatch size is too small (smaller than history term)')

    # compute stimulus projection for the n neurons     # (u is: M by N)
    #u = w.T.dot(x.T).T
    u = x.dot(w)

    # compute coupling projection
    kappa = np.vstack(( np.zeros((1,N)), n.dot(k)[1:,:] )) # (kappa is: M by N)

    # predicted rates
    rates = np.zeros( (M,N) )
    expu  = np.zeros( (M,N) )

    # compute history terms                             # (uh is: M by N)
    for nrnIdx in range(N):
        v = np.reshape( np.correlate(n[:,nrnIdx], h[:,nrnIdx], 'full')[0:n.shape[0]-1], (M-1,1) )
        v = np.vstack(( np.zeros((1,1)) , v ))

        # input to the nonlinearity
        linearOutput = u[:,nrnIdx] + np.squeeze(v) + b[0,nrnIdx] + kappa[:,nrnIdx]
        arrayCheck(linearOutput, 'linear output')

        # store exp(linearOtput) for the gradient
        expu[:,nrnIdx]  = np.exp(linearOutput) / ( 1 + np.exp(linearOutput) )
        expu[linearOutput > 50, nrnIdx] = 1

        arrayCheck(expu[:,nrnIdx], 'exp(u) / (1 + exp(u)) for neuron %g'%(nrnIdx))

        # response of the n neurons (stored as an n by m matrix)
        rates[:,nrnIdx] = f( linearOutput )
        arrayCheck(rates[:,nrnIdx], 'rhat for neuron %g'%(nrnIdx))

    return rates, expu

def visualizeNetwork(theta, params, data):

    print('\n====================================')
    print('===== Simulated GLM Properties =====')

    print('\nSpike Counts:')
    print('mean: ', np.mean(data['n']))
    print('var.: ', np.var(data['n']))

    print('\nRates:')
    print('mean: ', np.mean(data['r']))
    print('var.: ', np.var(data['r']))
    print('*** %g percent of rates are over the limit. ***'%(100*np.mean(data['r']>10)))

    print('\n====================================\n')

def genPinkNoise(t, n, dt):

    noise = np.random.randn(t,n,n)

    tc = np.arange(t).reshape(( -1,1,1 )) * dt
    xc = np.arange(n).reshape(( 1,-1,1 ))
    yc = np.arange(n).reshape(( 1,1,-1 ))

    tc -= np.mean(tc)
    xc -= np.mean(xc)
    yc -= np.mean(yc)

    radii = np.sqrt( tc**2 + xc**2 + yc**2 )

    # normalize
    noise /= np.fft.fftshift(radii)

    # generate via ifft
    stim = np.real(np.fft.ifftn(noise))
    stim /= np.sqrt(np.mean(stim**2))

    return stim

def gradCheck(grad):
    for key in grad.keys():
        arrayCheck(grad[key], key)

def arrayCheck(arr, name):
    if ~np.isfinite(arr).all():
        print('**** WARNING: Found non-finite value in ' + name + ' (%g percent of the values were bad)'%(np.mean(np.isfinite(arr))))

if __name__=="__main__":

    print('Initializing parameters...')
    p = setParameters(n = 100, ds = 256, dh = 10, m = 1e4)

    print('Generating model...')
    theta = generateModel(p, 'gabor')

    print('Simulating model...')
    data = generateData(theta, p)
    visualizeNetwork(theta, p, data)

    from matplotlib.pylab import *

    # plot connections
    figure(7)
    clf()
    imshow(theta['k'])
    colorbar()
    draw()

    # plot rasters
    figure(6)
    clf()
    imshow(np.log(1 + data['n'].T))
    colorbar()
    draw()

    figure(8)
    clf()
    imshow(np.log(data['r'].T))
    colorbar()
    draw()

    show()

    print('Evaluating objective...')
    fval, grad = f_df(theta, data, p)

    print('Objective: %g'%(fval))

    for key in grad.keys():
        print('grad[' + key + ']: %g'%(np.linalg.norm(grad[key])))


import json
import sys
from flask import Flask, request, jsonify

from pathfinding import Network

app = Flask(__name__)

def pathfinding(req):

    cap = req['cap']
    pickpool = req['pickpool']

    graph = req['graph']
    nodes = graph['nodes']
    edges = graph['edges']

    network = Network(nodes, edges, pickpool, cap)
    return json.dumps(network.solve())

@app.route('/')
def hello_world():
    return 'Es funzt!'

@app.route('/pathfinding', methods=['POST'])
def packaging():
    answer = pathfinding(request.json)
    return answer

if __name__ == '__main__':

    if(len(sys.argv) == 2):
        with open(sys.argv[1]) as json_file:
            data = json.load(json_file)
        answer = pathfinding(data)
        print(answer)
    else:
        app.run(host='0.0.0.0')


# -*- encoding: utf-8 -*-

import os
from welcome import welcome
from createMX.createmx import newmxmail
from deleteMX.deletemx import deletemx_mail
from createEX.createex import createex_mail
from deleteEX.deleteex import deleteex_mail
from redirectMail.redirectmail import redirectMail

def main():
    while True:
        os.system("clear")
        print(welcome("OVHApp"))
        print("\nChoisissez la fonction que vous souhaitez utiliser : ")
        print("""
        1 : Créer une boite mail - MXPlan
        2 : Supprimer une boite mail - MXPlan
        3 : Créer une boite mail - Exchange
        4 : Supprimer une boite mail - Exchange
        5 : Mettre en place une redirection
        0 : Exit"""
              )
        choice = input("\nEntrez votre choix : ")

        if choice == '1':
            newmxmail()
        elif choice == '2' :
            delmxmail()
        elif choice == '3' :
            newexmail()
        elif choice == '4' :
            delexmail()
        elif choice == '5' :
            redirectMail()
        elif choice == '0':
            exit()
        os.system("clear")

if __name__ == "__main__":
    main()


# -*- coding: utf-8 -*-
"""
Application setup script

To build package:
python3 setup.py sdist bdist_wheel clean

To run tests in an virtualenv:
python3 setup.py test

To run tests directly with verbose output:
python3 -m pytest -vv
"""

import sys
from setuptools import find_packages, setup
from updatewatch import __description__, __program__, __version__

def require_pylint():
    """Determine whether pytest-pylint is a necessary test dependency."""
    import getopt
    import os
    import shlex

    try:
        addopts = shlex.split(os.environ.get('PYTEST_ADDOPTS', '')) + \
            [v for _, v in getopt.getopt(sys.argv[2:], '', ['addopts='])[0]]
    except getopt.GetoptError:
        return False

    return '--pylint' in addopts

SETUP_REQUIRES = ['pytest-runner'] if \
    {'ptr', 'pytest', 'test'}.intersection(sys.argv) else []
INSTALL_REQUIRES = ['appdirs', 'keyring', 'PyYAML']
TESTS_REQUIRE = ['minimock', 'pytest', 'pytest-cov', 'pytest-datafiles']

# install standalone mock if necessary (Python 3.2 and below)
try:
    __import__('unittest.mock')
except ImportError:
    TESTS_REQUIRE.append('mock')

# install pytest-pylint if pytest is called with --pylint
if require_pylint():
    TESTS_REQUIRE.append('pytest-pylint')

setup(
    name=__program__,
    version=__version__,
    description=__description__,
    author='Brian Beffa',
    author_email='brbsix@gmail.com',
    url='https://github.com/brbsix/updatewatch',
    license='GPLv3',
    packages=find_packages(),
    setup_requires=SETUP_REQUIRES,
    install_requires=INSTALL_REQUIRES,
    tests_require=TESTS_REQUIRE,
    extras_require={'testing': TESTS_REQUIRE},
    entry_points={
        'console_scripts': ['updatewatch=updatewatch.updatewatch:main'],
    },
    keywords=['monitor', 'notify', 'updates'],
    classifiers=[
        'Development Status :: 4 - Beta',
        'Environment :: Console',
        'Intended Audience :: Developers',
        'Intended Audience :: End Users/Desktop',
        'License :: OSI Approved :: GNU General Public License v3 (GPLv3)',
        'Natural Language :: English',
        'Operating System :: POSIX',
        'Operating System :: POSIX :: Linux',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.4',
        'Programming Language :: Python :: 3.5',
        'Programming Language :: Python :: 3.6',
        'Programming Language :: Python :: 3 :: Only',
        'Topic :: Utilities',
    ]
)


###################################################
#EXTERNAL MODULES
###################################################
from util import *

###################################################
#VARIABLES
###################################################
datadir="DB/"

#THIS SCRIPT IS DESIGNED TO BE RUN IN IPYTHON
qip=1
if len(argv)<2:
    qip=0
    argv+=['1']

if argv[1]=='1':
    ######################################################################
    # LEE LA BASE DE DATOS DE RVcatf
    ######################################################################
    print("Reading RVCat...")
    RV=pd.read_csv(datadir+"RVCatf.csv")
    cats=['hip','tycho2_id']
    for cname in cats:
        RV[cname]=RV[cname].fillna('')
        RV[cname]=RV[cname].map(str)
    dfstr=RV.select_dtypes(['object'])
    RV[dfstr.columns]=dfstr.apply(lambda x: x.str.strip())
    RV['hip']=RV['hip'].apply(lambda x:x.replace('.0',''))
    print("Number of RV objects: %d"%len(RV))

def uniqCat(RV):
    RVCat=pd.DataFrame()
    for col in "hip","tycho2_id":
        sel=RV[RV[col]!=''][[col,"eRV"]].sort_values([col,"eRV"])
        print("Number of total entries for %s: %d"%(col,len(sel)))
        index=sel[col].drop_duplicates().index
        uniq=RV.ix[index]
        print("Number of uniq entries for %s: %d"%(col,len(uniq)))
        RVCat=RVCat.append(uniq)

    RVCat.to_csv(datadir+"RVCat.csv",index=False)
    print("Total number of uniq objects:%d"%len(RVCat))

if not qip:uniqCat(RV)