def generateWeights(graph, weightFile, param):
    pdb = Base(weightFile)
    pdb.create('pair', 'node1', 'node2', 'TS02', 'TS05', 'TS08')
    pdb.create_index('pair')

    sortedNodes = sorted(graph.nodes())
    for node in sortedNodes:
        others = sorted(set(n for n in sortedNodes if n > node))
        for other in others:
            if graph.has_edge(node, other):
                informations = list(
                    edge
                    for n1, n2, edge in graph.edges([node, other], data=True)
                    if ((n1 == node and n2 == other) or (
                        n1 == other and n2 == node)))
                timesofLinks = []
                for info in informations:
                    timesofLinks.append(int(info['time']))

                total_publications = len(informations)
                k = int(param.t0_) - max(timesofLinks)
                decayfunction02 = (1 - 0.2)**k
                decayfunction05 = (1 - 0.5)**k
                decayfunction08 = (1 - 0.8)**k

                pdb.insert(
                    str(node) + ';' + str(other), node, other,
                    (total_publications * decayfunction02),
                    (total_publications * decayfunction05),
                    (total_publications * decayfunction08))

    pdb.commit()
    return pdb
예제 #2
0
def generateWeights(graph, weightFile, param):
    pdb = Base(weightFile)
    pdb.create('pair', 'node1', 'node2', 'FI')
    pdb.create_index('pair')

    sortedNodes = sorted(graph.nodes())
    for node in sortedNodes:
        others = sorted(set(n for n in sortedNodes if n > node))
        for other in others:
            if graph.has_edge(node, other):
                informations = list(
                    edge
                    for n1, n2, edge in graph.edges([node, other], data=True)
                    if ((n1 == node and n2 == other) or (
                        n1 == other and n2 == node)))

                total_publications = len(informations)
                bagNode1 = list(
                    eval(edge['keywords'])
                    for n1, n2, edge in graph.edges([node], data=True)
                    if (n1 != other and n2 != other))
                bagNode2 = list(
                    eval(edge['keywords'])
                    for n1, n2, edge in graph.edges([other], data=True)
                    if (n1 != node and n2 != node))
                jc = get_jacard_domain(bagNode1, bagNode2)

                pdb.insert(
                    str(node) + ';' + str(other), node, other,
                    (total_publications * (jc + 1)))

    pdb.commit()
    return pdb
def generateWeights(graph, weightFile, param):
    pdb = Base(weightFile)
    pdb.create('pair', 'node1', 'node2','TS02','TS05','TS08', 'JC')
    pdb.create_index('pair')
    
    sortedNodes = sorted(graph.nodes())
    for node in sortedNodes:
        others = sorted(set(n for n in sortedNodes if n > node))
        for other in others:
            if graph.has_edge(node, other):
                informations = list(edge for n1, n2, edge in graph.edges([node, other], data=True) if ((n1 == node and n2 == other) or (n1 == other and n2 == node)) )
                timesofLinks = []
                for info in informations:
                    timesofLinks.append(int(info['time']))
        
                
                bagNode1 = list(eval(edge['keywords']) for n1, n2, edge in graph.edges([node], data=True) if (n1 != other and n2 != other)  )
                bagNode2 = list(eval(edge['keywords']) for n1, n2, edge in graph.edges([other], data=True) if (n1 != node and n2 != node)  )
                
                
                total_publications = len(informations)   
                k =  int(param.t0_)  - max(timesofLinks)
                decayfunction02 = (1 - 0.2) ** k
                decayfunction05 = (1 - 0.5) ** k
                decayfunction08 = (1 - 0.8) ** k
                
                pdb.insert(str(node) + ';' + str(other),node,other,(total_publications * decayfunction02) , (total_publications * decayfunction05) , (total_publications * decayfunction08), get_jacard_domain(bagNode1, bagNode2) ) 
                 
    pdb.commit()            
    return pdb
def generateWeights(graph, weightFile, param):
    pdb = Base(weightFile)
    pdb.create('pair', 'node1', 'node2','FTI01','FTI02','FTI03','FTI04','FTI05','FTI06','FTI07','FTI08','FTI09')
    pdb.create_index('pair')
    
    sortedNodes = sorted(graph.nodes())
    for node in sortedNodes:
        others = sorted(set(n for n in sortedNodes if n > node))
        for other in others:
            if graph.has_edge(node, other):
                informations = list(edge for n1, n2, edge in graph.edges([node, other], data=True) if ((n1 == node and n2 == other) or (n1 == other and n2 == node)) )
                timesofLinks = []
                for info in informations:
                    timesofLinks.append(int(info['time']))
                
                total_publications = len(informations)   
                k =  int(param.t0_)  - max(timesofLinks)
                FTI01 = total_publications * (0.1**k)
                FTI02 = total_publications * (0.2**k)
                FTI03 = total_publications * (0.3**k)
                FTI04 = total_publications * (0.4**k)
                FTI05 = total_publications * (0.5**k)
                FTI06 = total_publications * (0.6**k)
                FTI07 = total_publications * (0.7**k)
                FTI08 = total_publications * (0.8**k)
                FTI09 = total_publications * (0.9**k)
                 
                
 
                pdb.insert(str(node) + ';' + str(other),node,other, FTI01, FTI02, FTI03, FTI04, FTI05, FTI06, FTI07, FTI08, FTI09 ) 
                 
    pdb.commit()            
    return pdb
def run_class_configuration(request):
    if request.method == 'POST':
        db = Base('backendDB.pdl')
        if db.exists():
            db.open()
        else: 
            db.create('Type','Log', 'Run', 'Prefix','Rule','Threshold', 'TimeStamp', 'Status')

        configuration_json = json.loads(request.body)
        print configuration_json
        log = configuration_json["log"]
        prefix = configuration_json['prefix']
        rule = configuration_json['rule']
        threshold = configuration_json['threshold']

        # Encode the file.
        encoding.encode(log, prefix)
        for encodingMethod in configuration_json['encoding']:
            for clustering in configuration_json['clustering']:
                for classification in configuration_json['classification']:
                    django_rq.enqueue(tasks.classifierTask, log,
                                      prefix, encodingMethod, clustering, classification, rule, threshold)
                    run = classification + '_' + encodingMethod + '_' + clustering
                    records = [r for r in db if r['Run'] == run and r['Prefix'] == str(prefix) and r['Log'] == log and r['Rule'] == rule and r['Threshold'] == str(threshold)]
                    print records
                    if not records:
                        db.insert("Classification", log, run, str(prefix), rule, str(threshold), time.strftime("%b %d %Y %H:%M:%S", time.localtime()), 'queued')
                    else:
                        db.update(records[0], TimeStamp=time.strftime("%b %d %Y %H:%M:%S", time.localtime()), Status= 'queued')
        db.commit()        
    return HttpResponse("YOLO")
예제 #6
0
    def do_Post (self, result, request, args):
        def transfomType(x):
            if isinstance(x, unicode): return str(x)
            else: return x
    
        requestBody = args['requestContent']
        
        #######    Replace this section by your logic   #######
        vTestId = transfomType(json.loads(requestBody)['testId'])
        vTestMessage = transfomType(json.loads(requestBody)['testMessage'])
    
        responseCode = 200 #ResponseCode.Ok
 
       
        db = Base('database_service6.pdl')
        db.create('testId', 'testMessage', mode="open")
        db.insert(testId = vTestId, testMessage = vTestMessage)
        db.commit()

        result = []

        responseBody = json.dumps(result, sort_keys=True, indent=4, separators=(',', ': '))
        #######    Replace this section by your logic   #######


        request.setResponseCode(responseCode)
        resp = utils.serviceResponse(responseCode, responseBody)
        
        return resp
예제 #7
0
def calculatingWeights(graph, nodesnotLinked, database, calculatingFile):
    pdb = Base(calculatingFile)
    pdb.create('node1', 'node2', 'WCNFI','WAAFI')
    pdb.create_index('node1', 'node2')
                
    element = 0
    qtyofNodesToProcess = len(nodesnotLinked)
    for pair in nodesnotLinked:
        element = element+1
        FormatingDataSets.printProgressofEvents(element, qtyofNodesToProcess, "Calculating features for nodes not liked: ")
        neighbors_node1 = all_neighbors(graph, pair[0])
        neighbors_node2 = all_neighbors(graph, pair[1])
        len_neihbors_node1 = len(neighbors_node1)
        len_neihbors_node2 = len(neighbors_node2)
        CommonNeigbors = neighbors_node1.intersection(neighbors_node2)
        WCNFI = 0;
        WAAFI = 0;
        
        for cn in CommonNeigbors:
            item = get_partOfWeightCalculating(graph, database, pair, cn)
            WCNFI = WCNFI + item['WCN'];
            WAAFI = WAAFI + item['WAA'];
        pdb.insert(str(pair[0]), str(pair[1]), WCNFI, WAAFI )   
    pdb.commit()
    return pdb;
예제 #8
0
class Dao_in_memory():
    
    def __init__(self, region_name):
        self.db = Base(region_name, save_to_file=False)
        self.db.create('address', 'band', 'latency', 'resources', mode="override")
    
    def add_blocks(self, blocks):
        
        for block in blocks:
            self.db.insert(address=block.get('address'), band=block.get('band'),
                       latency=block.get('latency'), resources=block.get('resources'))
    
    def update_blocks(self, blocks):
        for block in blocks:
            try:
                old_record = self.db(address=block.get('address'))[0]
                self.db.update(old_record, band=block.get('band'),
                       latency=block.get('latency'), resources=block.get('resources'))
            except IndexError:
                print ('Not present')
            
    def get_block(self, address):
        
        try:
            block = self.db(address=address)[0]
            return block
        except IndexError:
            return None
        
    def get_all_blocks(self):
        
        return self.db
        
    
        
예제 #9
0
class Data:
    def __init__(self):
        self.db = Base(':memory:')
        self.db.create('container_manager', 'network_plugin',
                       'operating_system')

    def set(self, container_manager, network_plugin, operating_system):
        self.db.insert(container_manager=container_manager,
                       network_plugin=network_plugin,
                       operating_system=operating_system)
        self.db.commit()

    def exists(self, container_manager, network_plugin, operating_system):
        return len((self.db("container_manager") == container_manager)
                   & (self.db("network_plugin") == network_plugin)
                   & (self.db("operating_system") == operating_system)) > 0

    def jinja(self):
        template = env.get_template('table.md.j2')
        container_engines = list(self.db.get_unique_ids('container_manager'))
        network_plugins = list(self.db.get_unique_ids("network_plugin"))
        operating_systems = list(self.db.get_unique_ids("operating_system"))

        container_engines.sort()
        container_engines.reverse(
        )  # reverse sort container_engines to get Docker first in the list
        network_plugins.sort()
        operating_systems.sort()

        return template.render(container_engines=container_engines,
                               network_plugins=network_plugins,
                               operating_systems=operating_systems,
                               exists=self.exists)

    def markdown(self):
        out = ''
        for container_manager in self.db.get_unique_ids('container_manager'):
            # Prepare the headers
            out += "# " + container_manager + "\n"
            headers = '|OS / CNI| '
            underline = '|----|'
            for network_plugin in self.db.get_unique_ids("network_plugin"):
                headers += network_plugin + ' | '
                underline += '----|'
            out += headers + "\n" + underline + "\n"
            for operating_system in self.db.get_unique_ids("operating_system"):
                out += '| ' + operating_system + ' | '
                for network_plugin in self.db.get_unique_ids("network_plugin"):
                    if self.exists(container_manager, network_plugin,
                                   operating_system):
                        emoji = ':white_check_mark:'
                    else:
                        emoji = ':x:'
                    out += emoji + ' | '
                out += "\n"

        pprint(self.db.get_unique_ids('operating_system'))
        pprint(self.db.get_unique_ids('network_plugin'))
        return out
예제 #10
0
def save_in_db(comic_url):
    db = Base(os.path.join(SCRIPT_DIR, 'comics.db'))
    db.create('comic', 'time', mode="open")

    db.insert(comic=comic_url, time=datetime.datetime.now())
    db.commit()

    return len(db(comic=comic_url)) == 1
예제 #11
0
class YahoourlsearcherPipeline(object):
    def open_spider(self, spider):

        filename = "urls_log.txt"
        self.log_target = codecs.open(filename, 'a+', encoding='utf-8')
        self.log_target.truncate()

        self.db = Base('URL_database.pdl')
        self.db.create('url', 'date', mode="open")
        self.log_target.write("***New url scraping session started at: "+ str(datetime.datetime.strftime(datetime.datetime.now(), ' %Y-%m-%d %H:%M:%S ')) + " ***" +"\n")
        print("***New url scraping session started at: "+ str(datetime.datetime.strftime(datetime.datetime.now(), ' %Y-%m-%d %H:%M:%S ')) + " ***" +"\n")
        self.log_target.write("*** Total url in the Database BEFORE new search: "+ str(len(self.db)) + " ***" + "\n")


        dispatcher.connect(self.spider_closed, signals.spider_closed)


    def process_item(self, item, spider):
        self.db.insert(url=item['url'],
                       date=item['date']
                       )
        self.log_target.write(item['url'] + "  " + item['date'] + "\n")
        self.db.commit()
        return item

    def spider_closed(self, spider):
        url_structure = []
        print ("End of database")
        i = 1
        for r in self.db:
            #print (str(r["url"]) + " " + str(r["date"]) + " \n")
            url_structure.append(url_date(r["url"],r["date"]))
            i += 1
        print (str(i) + "Url in the DB \n")
        self.log_target.write("Session ends at: "+ str(datetime.datetime.strftime(datetime.datetime.now(), ' %Y-%m-%d %H:%M:%S ')) + "\n")
        print ("Session ends at: "+ str(datetime.datetime.strftime(datetime.datetime.now(), ' %Y-%m-%d %H:%M:%S ')) + "\n")
        self.log_target.write("*** Total url in the Database AFTER the search: "+ str(len(self.db)) + " ***" + "\n")

        print ("Elementi presenti nel database: "+ str(len(self.db)) + " in struttura: " + str(len(url_structure)))
        all_record = []
        for r in self.db:
            all_record.append(r)
        self.db.delete(all_record)
        print ("Elementi presenti nel database: "+ str(len(self.db)))

        #set qui
        url_structure = {x.url: x for x in url_structure}.values()


        for any_url in url_structure:
            self.db.insert(any_url.url, any_url.date)


        print ("Elementi presenti nel database: "+ str(len(self.db)))
        self.db.commit()
        self.log_target.write("--- After SET operation: "+ str(len(self.db)) + " --- " + "\n" + "\n" + "\n" + "\n")

        self.log_target.close()
예제 #12
0
class DBPipeline(object):
    def __init__(self):

        #Creating log file
        filename = "session_log.txt"
        self.log_target = codecs.open(filename, 'a+', encoding='utf-8')
        self.log_target.truncate()
        self.log_target.write("***New session started at: "+ str(datetime.datetime.strftime(datetime.datetime.now(), ' %Y-%m-%d %H:%M:%S ')) + " ***" +"\n")

        #Creating database for items
        self.db = Base('QuestionThreadExtracted.pdl')
        self.db.create('uid', 'type', 'author', 'title', 'text', 'date_time',
                       'tags', 'views', 'answers', 'resolve', 'upvotes', 'url', mode="open")

        #Some data for the log file
        self.number_of_questions = 0
        self.number_of_answers = 0
        self.last_id=0
        dispatcher.connect(self.spider_closed, signals.spider_closed)

 
    def process_item(self, item, spider):

        self.db.insert(uid=item['uid'],
                       type=item['type'],
                       author=item['author'],
                       title=item['title'],
                       text=item['text'],
                       date_time=item['date_time'],
                       tags=item['tags'],
                       views=item['views'],
                       answers=item['answers'],
                       resolve=item['resolve'],
                       upvotes=item['upvotes'],
                       url=item['url']
                       )
        #Count questions and answers
        if "question" in item['type']:
            self.number_of_questions+=1
            if self.last_id<item['uid']:
                self.last_id=item['uid']
        else:
            self.number_of_answers+=1


        self.db.commit()
        return item

    def spider_closed(self, spider):
        self.log_target.write("Questions founded: "+ str(self.number_of_questions) + "\n")
        self.log_target.write("Answers founded: "+ str(self.number_of_answers) + "\n")
        self.log_target.write("Last UID: "+str(self.last_id) + "\n" + "\n")


        self.log_target.write("***Session End at: "+ str(datetime.datetime.strftime(datetime.datetime.now(), ' %Y-%m-%d %H:%M:%S ')) + " ***" +"\n")

        self.log_target.close()
예제 #13
0
def save_in_db(channel, count):
    db = Base(os.path.join(SCRIPT_DIR, f'{channel}_members.db'))
    db.create('members', 'time', mode="open")
    len_db = len(db)
    count_previous = db[len_db - 1]['members'] if len_db else 0
    if count != count_previous:
        db.insert(members=count, time=datetime.datetime.now())
        db.commit()
        return True
예제 #14
0
def save_in_db(channel, count, date):
    db = Base(os.path.join(SCRIPT_DIR, f'{channel}_members.db'))
    db.create('members', 'time', mode="open")
    variant = db[0]['members'].split()[1]
    value = f"{count} {variant}"
    already_present = [r for r in db if r['members'] == value]
    if not already_present:
        db.insert(members=value, time=date)
        db.commit()
        return True
예제 #15
0
class DBPipeline(object):
    # Pipeline to write an Item in the database
    def open_spider(self, spider):
        # Creation of DB
        self.db = Base(spider.database)
        self.db.create('uid',
                       'type',
                       'author',
                       'title',
                       'text',
                       'date_time',
                       'tags',
                       'views',
                       'answers',
                       'resolve',
                       'upvotes',
                       'url',
                       mode="override")
        dispatcher.connect(self.spider_closed, signals.spider_closed)

    def process_item(self, item, spider):
        # Writing of the item
        self.db.insert(uid=item['uid'],
                       type=item['type'],
                       author=item['author'],
                       title=item['title'],
                       text=item['text'],
                       date_time=item['date_time'],
                       tags=item['tags'],
                       views=item['views'],
                       answers=item['answers'],
                       resolve=item['resolve'],
                       upvotes=item['upvotes'],
                       url=item['url'])

        self.db.commit()
        return item

    def spider_closed(self, spider):
        # Number of items saved, shown at the end
        i = 0
        j = 0
        for r in self.db:

            if r["type"] == "question":
                i += 1
            else:
                j += 1

        print('Number of questions and answers found:')
        print(str(i) + ' questions \n')
        print(str(j) + ' answers \n')
예제 #16
0
def generateWeights(graph, weightFile, param):
    pdb = Base(weightFile)
    pdb.create('pair', 'node1', 'node2', 'FTI01', 'FTI02', 'FTI03', 'FTI04',
               'FTI05', 'FTI06', 'FTI07', 'FTI08', 'FTI09')
    pdb.create_index('pair')

    sortedNodes = sorted(graph.nodes())
    for node in sortedNodes:
        others = sorted(set(n for n in sortedNodes if n > node))
        for other in others:
            if graph.has_edge(node, other):
                informations = list(
                    edge
                    for n1, n2, edge in graph.edges([node, other], data=True)
                    if ((n1 == node and n2 == other) or (
                        n1 == other and n2 == node)))
                timesofLinks = []
                for info in informations:
                    timesofLinks.append(int(info['time']))

                total_publications = len(informations)
                k = int(param.t0_) - max(timesofLinks)

                decayfunction = (0.8)**k
                bagNode1 = list(
                    eval(edge['keywords'])
                    for n1, n2, edge in graph.edges([node], data=True)
                    if (n1 != other and n2 != other))
                bagNode2 = list(
                    eval(edge['keywords'])
                    for n1, n2, edge in graph.edges([other], data=True)
                    if (n1 != node and n2 != node))

                jc = get_jacard_domain(bagNode1, bagNode2)

                FTI01 = total_publications * (decayfunction * (1 / (0.1**jc)))
                FTI02 = total_publications * (decayfunction * (1 / (0.2**jc)))
                FTI03 = total_publications * (decayfunction * (1 / (0.3**jc)))
                FTI04 = total_publications * (decayfunction * (1 / (0.4**jc)))
                FTI05 = total_publications * (decayfunction * (1 / (0.5**jc)))
                FTI06 = total_publications * (decayfunction * (1 / (0.6**jc)))
                FTI07 = total_publications * (decayfunction * (1 / (0.7**jc)))
                FTI08 = total_publications * (decayfunction * (1 / (0.8**jc)))
                FTI09 = total_publications * (decayfunction * (1 / (0.9**jc)))

                pdb.insert(
                    str(node) + ';' + str(other), node, other, FTI01, FTI02,
                    FTI03, FTI04, FTI05, FTI06, FTI07, FTI08, FTI09)

    pdb.commit()
    return pdb
예제 #17
0
def generateWeights(graph, weightFile, param):
    pdb = Base(weightFile)
    pdb.create('pair', 'node1', 'node2', 'CTS02', 'CTS05', 'CTS08')
    pdb.create_index('pair')

    sortedNodes = sorted(graph.nodes())
    for node in sortedNodes:
        others = sorted(set(n for n in sortedNodes if n > node))
        for other in others:
            if graph.has_edge(node, other):
                informations = list(
                    edge
                    for n1, n2, edge in graph.edges([node, other], data=True)
                    if ((n1 == node and n2 == other) or (
                        n1 == other and n2 == node)))
                timesofLinks = []
                for info in informations:
                    timesofLinks.append(int(info['time']))

                bagNode1 = list(
                    eval(edge['keywords'])
                    for n1, n2, edge in graph.edges([node], data=True)
                    if (n1 != other and n2 != other))
                bagNode2 = list(
                    eval(edge['keywords'])
                    for n1, n2, edge in graph.edges([other], data=True)
                    if (n1 != node and n2 != node))

                total_publications = len(informations)
                k = int(param.t0_) - max(timesofLinks)

                jc = get_jacard_domain(bagNode1, bagNode2)
                decayfunction = (0.6)**k
                decayfunction02 = (1 - 0.7)**jc
                decayfunction05 = (1 - 0.8)**jc
                decayfunction08 = (1 - 0.9)**jc

                CTS02 = total_publications * (decayfunction *
                                              (1 / decayfunction02))
                CTS05 = total_publications * (decayfunction *
                                              (1 / decayfunction05))
                CTS08 = total_publications * (decayfunction *
                                              (1 / decayfunction08))

                pdb.insert(
                    str(node) + ';' + str(other), node, other, CTS02, CTS05,
                    CTS08)

    pdb.commit()
    return pdb
예제 #18
0
def convertcsv2db(csvpath, dbpath): #Converts a CSV file to a PyDBLite database
    db = Base(dbpath)
    try:
        csvfile = open(csvpath, 'rb')
    except csv.Error:
        print "Could not open CSV file at " + csvpath + "\n"
    reader = csv.reader(csvfile)
    header = reader.next()
    try:
        db.create(*header)
    except IOError:
        print "Existing DB at " + dbpath + "\n"
    for row in reader:
        db.insert(*row)
    db.commit()
예제 #19
0
def new_repo(db_path):
    all_items = list_queue()
    pbs_id = []
    for line in all_items:
        pbs_id.append(str(re.search(r'\d+', line).group()))

    job_db = Base(os.path.join(db_path, 'PBS_job_database.pdl'))
    job_db.create('PBS_id', 'uniq_id', 'work_dir')

    for ele in pbs_id:
        ele_dir = init_work_dir(ele)
        ele_id = re.findall(r'\d+', ele_dir)[-1]
        job_db.insert(PBS_id=ele, work_dir=ele_dir, uniq_id=ele_id)
    job_db.commit()
    return job_db
예제 #20
0
def generate_finalResult(order,topRank, TestGraph, FileNameResult ):
    pdb = Base(FileNameResult)
    pdb.create('node1', 'node2', 'value', 'sucesso','topRank')
    pdb.create_index('node1', 'node2')
 
    indice = 0 
    for nodeToCheck in order:
        indice = indice+1
        isTopRank = (indice <= topRank)
        if (TestGraph.has_edge(nodeToCheck['node1'],nodeToCheck['node2'])):
            pdb.insert(str(nodeToCheck['node1']), nodeToCheck['node2'],nodeToCheck['value'] , True, isTopRank  ) 
        else:   
            pdb.insert(str(nodeToCheck['node1']), nodeToCheck['node2'],nodeToCheck['value'] , False, isTopRank  ) 
    
    pdb.commit()
    return pdb
예제 #21
0
class Posts:
    def __init__(self, filename, erase_db):
        self.db = Base(filename)
        self.db.create('author', 'content', 'date', mode="override" if erase_db else "open")

    def addPost(self, post):
        """ Persist a Post object in db and returns auto-generated id. """
        post.id = self.db.insert(author = post.author, content = post.content, date = post.date)
        self.db.commit()
        return post.id

    def getPost(self, id):
        """ Get a post by its id. Returns a Post object or None if id is not found. """
        db_entry = self.db[id] if id in self.db else None
        return self.__createPost(db_entry) if db_entry is not None else None

    def getPosts(self, from_date = None, to_date = None, author = None):
        """ Get all posts matching optionally provided conditions. Returns a list (can be empty). """
        iterator = self.db.filter()
        if from_date is not None:
            iterator = iterator & (self.db("date") > from_date)
        if to_date is not None:
            iterator = iterator & (self.db("date") < to_date)
        if author is not None:
            iterator = iterator & (self.db("author") == author)

        return [self.__createPost(db_entry) for db_entry in iterator]

    def getPostsCount(self):
        """ Get total number of posts in db. """
        return len(self.db)

    def __createPost(self, db_entry):
        return Post(db_entry['author'], db_entry['content'], db_entry['date'], db_entry['__id__'])
def generate_finalResult(order,topRank, TestGraph, FileNameResult ):
    pdb = Base(FileNameResult)
    pdb.create('node1', 'node2', 'value', 'sucesso','topRank')
    pdb.create_index('node1', 'node2')
 
    indice = 0 
    for nodeToCheck in order:
        indice = indice+1
        isTopRank = (indice <= topRank)
        if (TestGraph.has_edge(nodeToCheck['node1'],nodeToCheck['node2'])):
            pdb.insert(str(nodeToCheck['node1']), nodeToCheck['node2'],nodeToCheck['value'] , True, isTopRank  ) 
        else:   
            pdb.insert(str(nodeToCheck['node1']), nodeToCheck['node2'],nodeToCheck['value'] , False, isTopRank  ) 
    
    pdb.commit()
    return pdb
예제 #23
0
def generateWeights(graph, weightFile, param):
    pdb = Base(weightFile)
    pdb.create('pair', 'node1', 'node2','FI')
    pdb.create_index('pair')
    
    sortedNodes = sorted(graph.nodes())
    for node in sortedNodes:
        others = sorted(set(n for n in sortedNodes if n > node))
        for other in others:
            if graph.has_edge(node, other):
                informations = list(edge for n1, n2, edge in graph.edges([node, other], data=True) if ((n1 == node and n2 == other) or (n1 == other and n2 == node)) )
                        
                total_publications = len(informations)   
                pdb.insert(str(node) + ';' + str(other),node,other, total_publications ) 
                 
    pdb.commit()            
    return pdb
예제 #24
0
파일: tmp.py 프로젝트: LucasFLima/Mestrado
 def checkCondition(cls, result):
     if result == "error":
         return "erro"
     else:
         
         db = Base('database_service1.pdl')
         db.create('cod', 'message', mode="open")
         db.insert(cod='1', message='valid')
         db.insert(cod='2', message='not valid')
         db.commit()
         #for rec in (db("age") > 30):
         for rec in db:
             print rec["cod"] +' '+ rec["message"]
         
         
         
         return "ok"
예제 #25
0
def load_repo(db_path):
    job_db = Base(os.path.join(db_path, 'PBS_job_database.pdl'))
    if job_db.exists():
        job_db.open()
        print("PBS_database found. Loading...\n")

        all_items = list_queue()
        pbs_id = []
        for line in all_items:
            pbs_id.append(str(re.search(r'\d+', line).group()))
        for ele in pbs_id:
            ele_dir = init_work_dir(ele)
            ele_id = re.findall(r'\d+', ele_dir)[-1]
            if len(job_db(PBS_id=ele)) == 0:
                job_db.insert(PBS_id=ele, work_dir=ele_dir, uniq_id=ele_id)
    else:
        job_db = new_repo(db_path)
    job_db.commit()
def calculatingWeights(graph, nodesnotLinked, database, calculatingFile):
    pdb = Base(calculatingFile)
    pdb.create('node1', 'node2', 'cnWTS02', 'cnWTS05', 'cnWTS08', 'aaWTS02',
               'aaWTS05', 'aaWTS08')
    pdb.create_index('node1', 'node2')

    element = 0
    qtyofNodesToProcess = len(nodesnotLinked)
    for pair in nodesnotLinked:
        element = element + 1
        FormatingDataSets.printProgressofEvents(
            element, qtyofNodesToProcess,
            "Calculating features for nodes not liked: ")
        neighbors_node1 = all_neighbors(graph, pair[0])
        neighbors_node2 = all_neighbors(graph, pair[1])
        len_neihbors_node1 = len(neighbors_node1)
        len_neihbors_node2 = len(neighbors_node2)
        CommonNeigbors = neighbors_node1.intersection(neighbors_node2)
        CNWts02Feature = 0
        CNWts05Feature = 0
        CNWts08Feature = 0
        AAWts02Feature = 0
        AAWts05Feature = 0
        AAWts08Feature = 0
        CNWJCFeature = 0
        AAWJCFeature = 0

        for cn in CommonNeigbors:
            item = get_partOfWeightCalculating(graph, database, pair, cn)
            CNWts02Feature = CNWts02Feature + item['cnWts02']
            CNWts05Feature = CNWts05Feature + item['cnWts05']
            CNWts08Feature = CNWts08Feature + item['cnWts08']
            AAWts02Feature = AAWts02Feature + item['aaWts02']
            AAWts05Feature = AAWts05Feature + item['aaWts05']
            AAWts08Feature = AAWts08Feature + item['aaWts08']
            #CNWJCFeature = CNWJCFeature + item['cnWJC'];
            #AAWJCFeature = AAWJCFeature + item['aaWJC'];

        pdb.insert(str(pair[0]), str(pair[1]), CNWts02Feature, CNWts05Feature,
                   CNWts08Feature, AAWts02Feature, AAWts05Feature,
                   AAWts08Feature)
    pdb.commit()
    return pdb
예제 #27
0
class DBPipeline(object):
    # Pipeline to write an Item in the database
    def open_spider(self, spider):
        # Creation of DB
        self.db = Base(spider.database)
        self.db.create('uid', 'type', 'author', 'title', 'text', 'date_time',
                       'tags', 'views', 'answers', 'resolve', 'upvotes', 'url',
                       mode="override")
        dispatcher.connect(self.spider_closed, signals.spider_closed)

    def process_item(self, item, spider):
        # Writing of the item
        self.db.insert(uid=item['uid'],
                       type=item['type'],
                       author=item['author'],
                       title=item['title'],
                       text=item['text'],
                       date_time=item['date_time'],
                       tags=item['tags'],
                       views=item['views'],
                       answers=item['answers'],
                       resolve=item['resolve'],
                       upvotes=item['upvotes'],
                       url=item['url']
                       )

        self.db.commit()
        return item

    def spider_closed(self, spider):
        # Number of items saved, shown at the end
        i = 0
        j = 0
        for r in self.db:

            if r["type"] == "question":
                i += 1
            else:
                j += 1

        print ('Number of questions and answers found:')
        print (str(i) + ' questions \n')
        print (str(j) + ' answers \n')
def run_configuration(request):
    if request.method == 'POST':
        db = Base('backendDB.pdl')
        # db.create('Type','Log', 'Run', 'Prefix','Rule','Threshold', 'TimeStamp', 'Status', mode="override")
        if db.exists():
            db.open()
        else: 
            db.create('Type','Log', 'Run', 'Prefix','Rule','Threshold', 'TimeStamp', 'Status')


        configuration_json = json.loads(request.body)
        print configuration_json
        log = configuration_json["log"]
        prefix = configuration_json['prefix']
        # Encode the file.
        encoding.encode(log, prefix)
        
        for encodingMethod in configuration_json['encoding']:
            for clustering in configuration_json['clustering']:
                for regression in configuration_json['regression']:
                    django_rq.enqueue(tasks.regressionTask, log,
                                      prefix, encodingMethod, clustering, regression)
                    run = regression + '_' + encodingMethod + '_' + clustering
                    records = [r for r in db if r['Run'] == run and r['Prefix'] == str(prefix) and r['Log'] == log]
                    # for r in db:
                    #     if (r['Run'] == run) and (r['Prefix'] == str(prefix)) and (r['Log'] == log):
                    #         records.append(r)
                    print records
                    if not records:
                        db.insert("Regression", log, run, str(prefix),"NaN","NaN", time.strftime("%b %d %Y %H:%M:%S", time.localtime()), 'queued')
                    else:
                        db.update(records[0], TimeStamp=time.strftime("%b %d %Y %H:%M:%S", time.localtime()), Status= 'queued')
                    # if run in df['Run'].unique():
                    #     df.loc[df.Run == run, 'TimeStamp'] = time.strftime("%b %d %Y %H:%M:%S", time.localtime())
                    #     df.loc[df.Run == run, 'Status'] = "queued"
                    # else: 
                    #     df.loc[df.shape[0]] = [run, time.strftime("%b %d %Y %H:%M:%S", time.localtime()), 'queued']
        # print df
        # print df['Run'] 
        # df.to_csv('core_results_queue/' + log + '/' + str(prefix) + '/reg_queueStatus.csv', sep=',',header=writeHeader, mode='w+', index=False)
        db.commit()
    return HttpResponse("YOLO")
def initialize_database():
    print("Initializing database!")
    db = Base('block_chain.pdl')
    if (db.exists()):
        print("Delete block chain database")
        db.delete
    db.create('index',
              'timestamp',
              'hash',
              'previousHash',
              'data',
              mode="override")
    genesisData = "It all begins here!"
    timestamp = datetime.now()
    index = 0
    hash = calculateHash(index, "", timestamp, genesisData)
    genesisBlock = Block(index, str(timestamp), hash, None, genesisData)
    db.insert(genesisBlock.index, genesisBlock.timestamp, genesisBlock.hash,
              genesisBlock.previousHash, genesisBlock.data)
    db.commit()
예제 #30
0
def get_gbq_query_results(gbq_service, query, prj_id='ace-amplifier-455', index_col=None,
                          col_order=None, cache=True, cache_path='', db=None):
    
    # set_trace()
    if cache:
        if db is None:
            db = Base(cache_path).create('query', 'result_df', mode='open')
            # db.create_index('query')
        final_df = extract(db, query)
        if len(final_df) > 0:
            # print 'extracted from cache ' + db.path
            return final_df

    df = gbq.read_gbq(query, prj_id)

    if cache:
        db.insert(query, df)
        db.commit()
        print 'saved to cache ' + db.path

    return df
def calculatingWeights(graph, nodesnotLinked, database, calculatingFile):
    pdb = Base(calculatingFile)
    pdb.create('node1', 'node2', 'cnWTS02','cnWTS05','cnWTS08', 'aaWTS02', 'aaWTS05', 'aaWTS08')
    pdb.create_index('node1', 'node2')
                
    element = 0
    qtyofNodesToProcess = len(nodesnotLinked)
    for pair in nodesnotLinked:
        element = element+1
        FormatingDataSets.printProgressofEvents(element, qtyofNodesToProcess, "Calculating features for nodes not liked: ")
        neighbors_node1 = all_neighbors(graph, pair[0])
        neighbors_node2 = all_neighbors(graph, pair[1])
        len_neihbors_node1 = len(neighbors_node1)
        len_neihbors_node2 = len(neighbors_node2)
        CommonNeigbors = neighbors_node1.intersection(neighbors_node2)
        CNWts02Feature = 0;
        CNWts05Feature = 0;
        CNWts08Feature = 0;
        AAWts02Feature = 0;
        AAWts05Feature = 0;
        AAWts08Feature = 0;
        CNWJCFeature = 0;
        AAWJCFeature = 0;
        
        for cn in CommonNeigbors:
            item = get_partOfWeightCalculating(graph, database, pair, cn)
            CNWts02Feature = CNWts02Feature + item['cnWts02'];
            CNWts05Feature = CNWts05Feature + item['cnWts05'];
            CNWts08Feature = CNWts08Feature + item['cnWts08'];
            AAWts02Feature = AAWts02Feature + item['aaWts02'];
            AAWts05Feature = AAWts05Feature + item['aaWts05'];
            AAWts08Feature = AAWts08Feature + item['aaWts08'];
            #CNWJCFeature = CNWJCFeature + item['cnWJC'];
            #AAWJCFeature = AAWJCFeature + item['aaWJC'];
        
            
        pdb.insert(str(pair[0]), str(pair[1]), CNWts02Feature, CNWts05Feature, CNWts08Feature, AAWts02Feature, AAWts05Feature, AAWts08Feature  )   
    pdb.commit()
    return pdb;
예제 #32
0
class Rates:
    """ Persistence layer for exchange rates. """
    def __init__(self, filename, erase_db):
        self.__db = Base(filename)
        self.__db.create('currency',
                         'rate',
                         mode="override" if erase_db else "open")
        self.__db.create_index('currency')

    def setRate(self, currency, rate):
        """ Persist a currency's exchange rate. """
        assert rate > 0.0

        records = self.__db._currency[currency]

        if len(records) > 0:
            assert len(
                records
            ) == 1  # We never expect several exchange rates for the same currency
            self.__db.update(records[0], rate=rate)
        else:
            self.__db.insert(currency=currency, rate=rate)

        self.__db.commit()

    def getRate(self, currency):
        """ Get the exchange rate with EUR for the provided currency or None if it is not found.
            An exchange rate for currency CUR is Value(EUR) / Value(CUR): 1 EUR = rate(CUR) CUR <=> 1/rate(CUR) EUR = 1 CUR.
        """
        records = self.__db._currency[currency]
        return records[0]['rate'] if len(records) > 0 else None

    def getAllRates(self):
        """ Get all known exchange rates as a dict. """
        return [(r['currency'], r['rate']) for r in self.__db]

    def getRatesCount(self):
        """ Get total number of exchange rates in db. """
        return len(self.__db)
예제 #33
0
def get_results_db(clear_cache=False, skip=[]):
    cache_file = 'cache/results.pdl'
    db = Base(cache_file)

    if clear_cache or not db.exists() or os.path.getmtime(cache_file) < os.path.getmtime(results_dir):
        warnings.warn('Rebuilding results cache...')
        columns = set()
        rows = []
        p = pathlib.Path(results_dir)
        for config_file in p.glob('*.config'):
            with config_file.open() as config_fh:
                settings_hash = config_file.stem
                row = json.loads(config_fh.read())
            if settings_hash in skip:
                continue
            row['hash'] = settings_hash
            tests_count = analyze.count(config_file.parent, settings_hash)
            row['iostat_cpu'], len_cpu_values = analyze.iostat_cpu(config_file.parent, settings_hash)
            row['iperf_result'], len_iperf_values = getattr(analyze, row['iperf_name'])(config_file.parent, settings_hash, row)
            if tests_count != len_cpu_values or tests_count != len_iperf_values:
                raise analyze.AnalysisException('For test {}, mismatch in cardinality of tests between count ({}), iostat ({}) and iperf ({})'.format(settings_hash, tests_count, len_cpu_values, len_iperf_values), settings_hash)
            if len_iperf_values > 0:
                min_fairness = row['iperf_result']['fairness'][0] - row['iperf_result']['fairness'][1]
                if min_fairness < (1 - 1 / (2 * row['parallelism'])):
                    warnings.warn('For test {}, fairness has a critical value: {}.'.format(settings_hash, row['iperf_result']['fairness']), RuntimeWarning)
            columns = columns | set(row.keys())
            rows.append(row)

        db.create(*columns, mode='override')
        for r in rows:
            db.insert(**r)
        db.commit()
        warnings.warn('Results cache built.')
    else:
        warnings.warn('Reusing results cache.')
        db.open()

    return db
예제 #34
0
class Rates:
    """ Persistence layer for exchange rates. """

    def __init__(self, filename, erase_db):
        self.__db = Base(filename)
        self.__db.create('currency', 'rate', mode="override" if erase_db else "open")
        self.__db.create_index('currency')

    def setRate(self, currency, rate):
        """ Persist a currency's exchange rate. """
        assert rate > 0.0

        records = self.__db._currency[currency]

        if len(records) > 0:
            assert len(records) == 1 # We never expect several exchange rates for the same currency
            self.__db.update(records[0], rate = rate)
        else:
            self.__db.insert(currency = currency, rate = rate)

        self.__db.commit()

    def getRate(self, currency):
        """ Get the exchange rate with EUR for the provided currency or None if it is not found.
            An exchange rate for currency CUR is Value(EUR) / Value(CUR): 1 EUR = rate(CUR) CUR <=> 1/rate(CUR) EUR = 1 CUR.
        """
        records = self.__db._currency[currency]
        return records[0]['rate'] if len(records) > 0 else None

    def getAllRates(self):
        """ Get all known exchange rates as a dict. """
        return [(r['currency'], r['rate']) for r in self.__db]

    def getRatesCount(self):
        """ Get total number of exchange rates in db. """
        return len(self.__db)
예제 #35
0
파일: s0-monitor.py 프로젝트: kulhos/elmer
#parser.add_argument("-f","--file", type=open, help="config file")

#args = parser.parse_args()
#if args.file:
#    CONFIG_FILE = args.log

GPIO.setmode(GPIO.BCM)     # set up BCM GPIO numbering
GPIO.setup(GPIO_PIN, GPIO.IN, pull_up_down=GPIO_PULL)    # set GPIO25 as input (button)

db.create('timestamp','event','value', mode="open")

print "Existing records:"
for r in db:
    print r

db.insert(time.time(), "elmer",1)
db.commit()

global counter
global timeStamp
counter=0
timeStamp = time.time()
global history
history = {}

headers = {'user-agent': 'smrz-elmer/0.0.1', 'Content-type': 'application/x-www-form-urlencoded'};
URL = "http://www.netfort.cz/smrz/s_elmer.php";

stopped = Event()

def impulseCount(channel):
예제 #36
0
class Inventory:

    def __init__(self, session):
        self.session = session
        self.client = session.resource("ec2")
        self.create_database()

    def create_database(self):
        self.db = Base("test.pdl")
        if not self.db.exists():
            self.db.create("resource", "name", "env")
        self.db.open()

    def fetch_instance(self, id):
        try:
            instances = self.client.instances.filter(InstanceIds=[id])
            for instance in instances:
                tags = {t["Key"]: t["Value"] for t in instance.tags}
                env = tags["Env"] if "Env" in tags else tags["Environment"]
                self.db.insert(resource=id, name=tags["Name"], env=env)
        except Exception as e:
            print(e)
            self.db.insert(resource=id, name=id, env="")

    def get_instance(self, id):
        instance = self.db(resource=id)
        if not instance:
            self.fetch_instance(id)
            instance = self.db(resource=id)

        return instance[0] if instance else None

    def fetch_network_interface(self, id):
        c = self.client.meta.client
        try:
            data = c.describe_network_interfaces(NetworkInterfaceIds=[id])
            nif = data["NetworkInterfaces"][0]
            if "Attachment" in nif:
                instance = self.get_instance(nif["Attachment"]["InstanceId"])
                if instance:
                    self.db.insert(resource=id,
                                   name=instance["name"],
                                   env=instance["env"])
        except Exception as e:
            print(e)
            self.db.insert(resource=id,
                           name=id,
                           env="")

    def add_group(self, id, name):
        self.db.insert(resource=id, name=name, env="")

    def get_network_interface(self, id):
        nif = self.db(resource=id)
        if not nif:
            self.fetch_network_interface(id)
            nif = self.db(resource=id)
        return nif[0] if nif else None

    def fetch_security_group(self, id):
        c = self.client.meta.client
        try:
            groups = c.describe_security_groups(GroupIds=[id])
            for group in groups["SecurityGroups"]:
                self.db.insert(resource=id, name=group["GroupName"])
        except Exception as e:
            print(e)

    def get_security_group(self, id):
        group = self.db(resource=id)
        if not group:
            self.fetch_security_group(id)
            group = self.db(resource=id)
        return group[0] if group else None

    def get_resource_name(self, id):
        resource = self.db(resource=id)
        return resource[0]["name"] if resource else None
예제 #37
0
def process(bot, chat_id, parsedCommand, messageText, currentMessage, update, instanceAge):
    def sendText(givenText, replyingMessageID=0, keyboardLayout=[]):
        if not chatInstanceArray[chat_id]['adminDisable']:
            atbSendFunctions.sendText(bot, chat_id, givenText, replyingMessageID, keyboardLayout)

    def sendPhoto(imageName):
        atbSendFunctions.sendPhoto(bot, chat_id, "images/" + imageName)

    def sendSticker(stickerName):
        atbSendFunctions.sendSticker(bot, chat_id, "stickers/" + stickerName)

    def sendAudio(audioName):
        atbSendFunctions.sendAudio(bot, chat_id, "audio/" + audioName)

    def sendVideo(videoName):
        atbSendFunctions.sendVideo(bot, chat_id, "videos/" + videoName)

    def passSpamCheck(timeDelay=15):
        return atbMiscFunctions.spamCheck(chat_id, currentMessage.date, timeDelay)

    try:
        chatInstanceArray[chat_id]['checking'] = True
    except Exception:
        chatInstanceArray[chat_id] = {'checking': True, 'adminDisable': False, 'spamTimestamp': 0, 'shottyTimestamp': 0, 'shottyWinner': "", 'checkingVehicles': False, 'whoArray': []}

    try:
        #commands go here, in this if-elif block. Python doesn't have switch statements.
        if parsedCommand == "/mom": #sends "MOM GET THE CAMERA"
            sendText("MOM GET THE CAMERA")

        elif atbMiscFunctions.isMoom(parsedCommand): #sends M {random number of Os} M
            if passSpamCheck(): #use this to prevent spamming of a command
                response = "M"
                for i in range(0, random.randint(3, 75)):
                    response += "O"
                sendText(response + "M")

        elif parsedCommand == "/swag":
            sendText("swiggity swag, what\'s in the bag?")

        elif parsedCommand == "/worms":
            if passSpamCheck():
                response = "hey man can I borrow your "
                if len(messageText) > len("/worms "):
                    response += messageText[len("/worms "):]
                else:
                    response += "worms"
                sendText(response)

        elif parsedCommand == "/shh" or parsedCommand == "/shhh":
            if passSpamCheck():
                sendPhoto("shhh.jpg")

        elif parsedCommand == "/father":
            if (random.randint(0, 1)):
                sendText("You ARE the father!")
            else:
                sendText("You are NOT the father!")

        elif parsedCommand == "/rip":   #sends "I can't believe that [name (defaults to sender's name)] is f*****g dead."
            if passSpamCheck():
                response = "I can't believe that "

                while "my " in messageText:
                    messageText = messageText.replace("my ", currentMessage.from_user.first_name + "\'s ", 1)

                if len(messageText) > len("/rip "):
                    if messageText[len("/rip "):] == "me":
                        response += currentMessage.from_user.first_name
                    else:
                        response += messageText[len("/rip "):]
                else:
                    response += currentMessage.from_user.first_name
                response += " is f*****g dead."
                sendText(response)

        elif parsedCommand == "/rips":   #sends "I can't believe that [name (defaults to sender's name)] is f*****g dead."
            if passSpamCheck():
                response = "I can't believe that "

                while "my " in messageText:
                    messageText = messageText.replace("my ", currentMessage.from_user.first_name + "\'s ", 1)

                if len(messageText) > len("/rip "):
                    if messageText[len("/rip "):] == "me":
                        response += currentMessage.from_user.first_name
                    else:
                        response += messageText[len("/rip "):]
                else:
                    response += currentMessage.from_user.first_name
                response += " are f*****g dead."
                sendText(response)

        elif parsedCommand == "/scrub":
            checkingStats = False
            try:
                if currentMessage.text.lower().split()[1] == "stats":
                    db = Base('chatStorage/scrub.pdl') #The path to the DB
                    db.create('username', 'name', 'counter', mode="open")
                    K = list()
                    for user in db:
                        K.append(user)
                    sortedK = sorted(K, key=lambda x: int(x['counter']), reverse=True)
                    outputString = "SCRUBBIEST LEADERBOARD:\n"
                    for user in sortedK:
                        pluralString = " SCRUB POINT"
                        if not(int(user['counter']) == 1):
                            pluralString += "S"
                        pluralString += "\n"
                        outputString += user['name'].upper() + ": " + str(user['counter']) + pluralString
                    sendText(outputString)
                    checkingStats = True
            except IndexError:
                pass

            if not checkingStats and (currentMessage.from_user.id == 169883788 or currentMessage.from_user.id == 44961843):
                db = Base('chatStorage/scrub.pdl')
                db.create('username', 'name', 'counter', mode="open")

                userWasFound = False
                valueSuccessfullyChanged = False

                try:
                    pointsAdded = float(currentMessage.text.lower().split()[1])
                except (IndexError, ValueError):
                    pointsAdded = 1

                for user in db:
                    if int(user['username']) == currentMessage.reply_to_message.from_user.id:
                        db.update(user, counter=int(user['counter']) + pointsAdded)
                        valueSuccessfullyChanged = True
                        userWasFound = True
                db.commit()

                if not userWasFound:
                    db.insert(currentMessage.reply_to_message.from_user.id, currentMessage.reply_to_message.from_user.first_name, pointsAdded)
                    db.commit()

                if valueSuccessfullyChanged or not userWasFound:
                    sendText("Matt Gomez awarded " + str(pointsAdded) + " scrub point(s) to " + currentMessage.reply_to_message.from_user.first_name + ".")

            elif not checkingStats:
                sendText("AdamTestBot, powered by ScrubSoft (C)")

        elif parsedCommand == "/hiss":
            checkingStats = False
            try:
                if currentMessage.text.lower().split()[1] == "stats":
                    db = Base('chatStorage/hiss.pdl')
                    db.create('username', 'name', 'counter', mode="open")
                    K = list()
                    for user in db:
                        K.append(user)
                    sortedK = sorted(K, key=lambda x: int(x['counter']), reverse=True)
                    outputString = "Hiss Leaderboard:\n"
                    for user in sortedK:
                        pluralString = " hiss"
                        if not(int(user['counter']) == 1):
                            pluralString += "es"
                        pluralString += "\n"
                        outputString += user['name'] + ": " + str(user['counter']) + pluralString
                    sendText(outputString)
                checkingStats = True
            except IndexError:
                pass

            if not checkingStats and (currentMessage.from_user.id == 122526873 or currentMessage.from_user.id == 44961843):
                db = Base('chatStorage/hiss.pdl')
                db.create('username', 'name', 'counter', mode="open")

                userWasFound = False
                valueSuccessfullyChanged = False

                for user in db:
                    if int(user['username']) == currentMessage.reply_to_message.from_user.id:
                        db.update(user, counter=int(user['counter']) + 1)
                        valueSuccessfullyChanged = True
                        userWasFound = True
                db.commit()

                if not userWasFound:
                    db.insert(currentMessage.reply_to_message.from_user.id, currentMessage.reply_to_message.from_user.first_name, 1)
                    db.commit()

                if valueSuccessfullyChanged or not userWasFound:
                    sendText("Robyn hissed at " + currentMessage.reply_to_message.from_user.first_name + ".")

        elif parsedCommand == "/water":
            if passSpamCheck():
                if (random.randint(0, 1) == 0):
                    sendSticker("water.webp")
                else:
                    sendSticker("hoboken_water.webp")

        elif parsedCommand == "/sysinfo":
            if passSpamCheck():
                cpu = []
                for x in range(3):
                    cpu.append(psutil.cpu_percent(interval=1))
                cpuavg = round(sum(cpu) / float(len(cpu)), 1)
                memuse = psutil.virtual_memory()[2]
                diskuse = psutil.disk_usage('/')[3]
                sendText("The CPU uasge is " + str(cpuavg) + "%, the memory usage is " + str(memuse) + "%, and " + str(diskuse) + "% of the disk has been used.")

        elif parsedCommand == "/grill":
            if passSpamCheck():
                sendPhoto("grill.jpg")

        elif parsedCommand == "/pants":
            if passSpamCheck():
                sendText("Shit! I can't find my pants.")

        elif parsedCommand == "/broken":
            if passSpamCheck():
                if len(messageText) > len("/broken "):
                    message = str(currentMessage.from_user.username) + " says: @magomez96 my " + messageText[len("/broken "):] + " is broken!"
                else:
                    message = str(currentMessage.from_user.username) + " says: @magomez96 my shit is broken!"
                sendText(message)

        elif parsedCommand == "/quote":
            if passSpamCheck(5):
                try:
                    sendText(atbQuote.getQuoteAt(chat_id, int(messageText.split()[1])))
                except:
                    sendText(atbQuote.getQuote(chat_id))

        elif parsedCommand == "/quotefrom":
            print("\n" + messageText[len("/quotefrom "):])
            if passSpamCheck(5):
                sendText(atbQuote.getQuoteFrom(chat_id, messageText[len("/quotefrom "):]))

        elif parsedCommand == "/quoteremove":
            if currentMessage.from_user.username == "AdamZG" or currentMessage.from_user.username == "magomez96" or currentMessage.from_user.username == "Peribot":
                if atbQuote.quoteRemove(chat_id, int(messageText.split()[1])):
                    sendText("Quote " + messageText.split()[1] + " removed")
                else:
                    sendText("That quote doesn't exist or you never added any quotes")

        elif parsedCommand == "/quoteadd":
            if currentMessage.reply_to_message == None and messageText == "/quoteadd":
                sendText("Try replying to a message with this command to add it to the quote list")
            else:
                userLastName = ""
                try:
                    userLastName = " " + currentMessage.from_user.last_name
                except:
                    pass
                try:
                    replyUserLastName = ""
                    try:
                        replyUserLastName = " " + currentMessage.reply_to_message.from_user.last_name
                    except:
                        replyUserLastName = ""
                    quote_resp = atbQuote.quoteAdd(chat_id, '"' + currentMessage.reply_to_message.text + '"', (currentMessage.reply_to_message.from_user.first_name + replyUserLastName).strip())
                    sendText(quote_resp)
                except(Exception):
                    quoteParse = currentMessage.text.split("-")
                    quote = "-".join(quoteParse[:-1])
                    quote = quote[len("/quoteadd "):].strip()
                    quote_resp = atbQuote.quoteAdd(chat_id, quote, quoteParse[-1].strip(), (currentMessage.from_user.first_name + userLastName).strip())
                    sendText(quote_resp)

        elif parsedCommand == "/quotelegacy":
            if passSpamCheck(5):
                sendText(atbQuote.getQuoteLegacy(chat_id))

        elif parsedCommand == "/pogo":
            def getPokeInfo():
                start = time.time()
                try:
                    nf = urlopen("https://pgorelease.nianticlabs.com/plfe/", timeout = 3)
                    page = nf.read()
                    end = time.time()
                    nf.close()
                except TimeoutError:
                    end=time.time()
                    rTime = round((end - start) * 1000)
                    if (rTime < 800):
                        sendText("Pokémon GO is UP\n{}ms Response Time".format(rTime))
                    elif (rTime >= 800 and rTime < 3000):
                        sendText("Pokémon GO's servers are struggling\n{}ms Response Time".format(rTime))
                    elif (rTime >= 3000):
                        sendText("Pokémon GO is DOWN\n{}ms Response Time".format(rTime))
                except Exception as e:
                    sendText("Pokémon GO's servers are really not doing well\nHere's what I got back\n" + e.__str__())
            
            myThread = Thread(target=getPokeInfo)
            myThread.start()

        elif parsedCommand == "/discourse":
            if passSpamCheck():
                if (random.randint(0, 1) == 0):
                    sendPhoto("discourse.jpg")
                else:
                    sendText("http://thediscour.se")

        #this command should go last:
        elif parsedCommand == "/community": #add your command to this list
            response = "/mom - get the camera\n"
            response += "/mooom (any number of \'o\'s) - call for help\n"
            response += "/swag - more memes\n"
            response += "/worms - can I borrow them?\n"
            response += "/shh(h) - here, be relaxed\n"
            response += "/father - are you the father?\n"
            response += "/rip(s) (something) - I can't believe they're dead! (The s is for plural dead things)\n"
            response += "/hiss stats - see how many time Robyn has hissed at people\n"
            response += "/scrub or /scrub stats - see who sponsors me or how many times Matt Gomez has called you a scrub\n"
            response += "/water - does this water look brown to you?\n"
            response += "/sysinfo - Gets server performance info.\n"
            response += "/grill - I'm a George Foreman grill!\n"
            response += "/pants - Pants?\n"
            response += "/broken - Tell Matt Gomez your stuff is broken.\n"
            response += "/quote - Pulls a random quote from a list. Reply to a message with /quoteadd to add one!\n"
            response += "/pogo - Want to know if Pokémon GO's servers are up?\n"
            response += "/discourse - Break in case of spicy discourse"
            sendText(response)

        else:
            return False

        return True
    except Exception:
        print(traceback.format_exc())
        return False
예제 #38
0
from pydblite import Base
db = Base('test.pydb')
db.create('name', 'age', 'size')

db.insert(name='homer', age=23, size=1.84)
db.insert('homer', 23, 1.84)
db.commit()
db.exists()
예제 #39
0
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
from pydblite import Base

requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
#config = dump(safe_load(open("/root/vrops/vrops.yaml", 'r')))
config = json.loads(open("/opt/prometheus/vrops_exporter/vrops.json").read())

print "Connecting to vROPs host: ", config["server"]["hostname"]
vcops = nagini.Nagini(host=config["server"]["hostname"],
                      user_pass=(config["server"]["user"],
                                 config["server"]["pwd"]))
db = Base('/tmp/vrops.pd1')
db.create('type', 'object', 'id', 'site', mode="override")

for d_id in vcops.get_resources(resourceKind="Datacenter")['resourceList']:
    #if d_id['resourceKey']['name'] in [site for site in config["datacenter"] ]:
    #print d_id['identifier'],d_id['resourceKey']['name']
    for d_r in vcops.get_relationships(
            id=d_id['identifier'], relationshipType='CHILD')['resourceList']:
        if d_r['resourceKey']['resourceKindKey'] in [
                x["kind"] for x in config["metrics"]
        ]:
            db.insert(type=d_r['resourceKey']['resourceKindKey'],
                      object=d_r['resourceKey']['name'],
                      id=d_r['identifier'],
                      site=d_id['resourceKey']['name'])
            #print d_r['resourceKey']['resourceKindKey'], d_r['resourceKey']['name'], d_r['identifier'], d_id['resourceKey']['name']

db.commit()
예제 #40
0
def handleLikes(isLiking, currentMessage):
    try:
        db = Base('chatStorage/likes/likes.pdl') #The path to the database
        db.create('userID', 'likes', 'dislikes', 'history', 'liked', mode="open")  # Create a new DB if one doesn't exist. If it does, open it
        #get disliked comment ID
        likedID = currentMessage.reply_to_message.message_id
        likeReceivedID = currentMessage.reply_to_message.from_user.id
        #check if likedID is in user's liked history
        passedCheck = True
        isLikingSelf = int(likeReceivedID) == int(currentMessage.from_user.id)
        if isLikingSelf:
            passedCheck = False
        if passedCheck:
            if db(userID=currentMessage.from_user.id):
                liker = db(userID=currentMessage.from_user.id).pop()
            else:
                liker = ""
            if db(userID=likeReceivedID):
                liked = db(userID=likeReceivedID).pop()
            else:
                liked = ""
            if liker and likedID not in liker['history'] and liked:
                hist = liker['history']
                hist += " " + likedID
                db.update(liker, history=hist)
                lik = liker['liked']
                tmpLikes = liked['likes']
                tmpDis = liked['dislikes']
                if isLiking:
                    tmpLikes = int(tmpLikes) + 1
                    db.update(liked, likes=tmpLikes)
                    lik += " " + "t"
                else:
                    tmpDis = int(tmpDis) + 1
                    db.update(liked, dislikes=tmpDis)
                    lik += " " + "f"
                db.update(liker, liked=lik)
            elif liker and not liked:
                hist = liker['history']
                hist += " " + likedID
                db.update(liker, history=hist)
                lik = liker['liked']
                if isLiking:
                    db.insert(likeReceivedID, 1, 0, "", "")
                    lik += " " + "t"
                else:
                    db.insert(likeReceivedID, 0, 1, "", "")
                    lik += " " + "f"
                db.update(liker, liked=lik)
            elif not liker and liked:
                if isLiking:
                    tmpLikes = liked['likes']
                    db.insert(currentMessage.from_user.id, 0, 0, likedID, 't')
                    tmpLikes = int(tmpLikes) + 1
                    db.update(liked, likes=tmpLikes)
                else:
                    tmpDis = liked['dislikes']
                    db.insert(currentMessage.from_user.id, 0, 0, likedID, 'f')
                    tmpDis = int(tmpDis) + 1
                    db.update(liked, dislikes=tmpDis)
            elif not liker and not liked:
                if isLiking:
                    db.insert(currentMessage.from_user.id, 0, 0, likedID, 't')
                    db.insert(likeReceivedID, 1, 0, "", "")
                else:
                    db.insert(currentMessage.from_user.id, 0, 0, likedID, 'f')
                    db.insert(likeReceivedID, 0, 1, "", "")
            db.commit()
    except Exception:
        print traceback.format_exc()
예제 #41
0
from pydblite import Base
db = Base('Thai db.pdl')
db.create('ภาค','เพศ','ว่างงาน','ไตรมาส','ปี')
"""ภาคเหนือ เพศชาย"""
db.insert(ภาค='เหนือ', เพศ='ชาย', ว่างงาน='0.4', ไตรมาส='1', ปี='2556')
db.insert(ภาค='เหนือ', เพศ='ชาย', ว่างงาน='0.5', ไตรมาส='2', ปี='2556')
db.insert(ภาค='เหนือ', เพศ='ชาย', ว่างงาน='0.5', ไตรมาส='3', ปี='2556')
db.insert(ภาค='เหนือ', เพศ='ชาย', ว่างงาน='0.7', ไตรมาส='4', ปี='2556')
db.insert(ภาค='เหนือ', เพศ='ชาย', ว่างงาน='0.6', ไตรมาส='1', ปี='2557')
db.insert(ภาค='เหนือ', เพศ='ชาย', ว่างงาน='0.7', ไตรมาส='2', ปี='2557')
db.insert(ภาค='เหนือ', เพศ='ชาย', ว่างงาน='0.8', ไตรมาส='3', ปี='2557')
db.insert(ภาค='เหนือ', เพศ='ชาย', ว่างงาน='0.7', ไตรมาส='4', ปี='2557')
db.insert(ภาค='เหนือ', เพศ='ชาย', ว่างงาน='0.9', ไตรมาส='1', ปี='2558')
db.insert(ภาค='เหนือ', เพศ='ชาย', ว่างงาน='0.7', ไตรมาส='2', ปี='2558')
db.insert(ภาค='เหนือ', เพศ='ชาย', ว่างงาน='0.7', ไตรมาส='3', ปี='2558')
db.insert(ภาค='เหนือ', เพศ='ชาย', ว่างงาน='0.9', ไตรมาส='4', ปี='2558')
"""ภาคเหนือ เพศหญิง"""
db.insert(ภาค='เหนือ', เพศ='หญฺิง', ว่างงาน='0.5', ไตรมาส='1', ปี='2556')
db.insert(ภาค='เหนือ', เพศ='หญิง', ว่างงาน='0.6', ไตรมาส='2', ปี='2556')
db.insert(ภาค='เหนือ', เพศ='หญิง', ว่างงาน='0.6', ไตรมาส='3', ปี='2556')
db.insert(ภาค='เหนือ', เพศ='หญิง', ว่างงาน='0.6', ไตรมาส='4', ปี='2556')
db.insert(ภาค='เหนือ', เพศ='หญิง', ว่างงาน='0.8', ไตรมาส='1', ปี='2557')
db.insert(ภาค='เหนือ', เพศ='หญิง', ว่างงาน='1.1', ไตรมาส='2', ปี='2557')
db.insert(ภาค='เหนือ', เพศ='หญิง', ว่างงาน='0.7', ไตรมาส='3', ปี='2557')
db.insert(ภาค='เหนือ', เพศ='หญิง', ว่างงาน='0.5', ไตรมาส='4', ปี='2557')
db.insert(ภาค='เหนือ', เพศ='หญิง', ว่างงาน='0.7', ไตรมาส='1', ปี='2558')
db.insert(ภาค='เหนือ', เพศ='หญิง', ว่างงาน='0.9', ไตรมาส='2', ปี='2558')
db.insert(ภาค='เหนือ', เพศ='หญิง', ว่างงาน='0.8', ไตรมาส='3', ปี='2558')
db.insert(ภาค='เหนือ', เพศ='หญิง', ว่างงาน='0.5', ไตรมาส='4', ปี='2558')

"""---------------------------------------------------------------"""
예제 #42
0
파일: db.py 프로젝트: srgevs93/rsoi2
from pathlib import Path
from pydblite import Base

if not Path('database').exists():
    Path('database').mkdir()

client = Base('database/client.pdl')
if client.exists():
    client.open()
else:
    client.create('secret', 'redirect_uri', 'name')

authorization_code = Base('database/authorization_code.pdl')
if authorization_code.exists():
    authorization_code.open()
else:
    authorization_code.create('user_id', 'code', 'expire_time')

token = Base('database/token.pdl')
if token.exists():
    token.open()
else:
    token.create('user_id', 'access', 'expire_time', 'refresh')

if len(client) == 0:
    client.insert(secret='test_secret', redirect_uri='http://example.com', name='app1')
    client.commit()
def process(bot, chat_id, parsedCommand, messageText, currentMessage, update,
            instanceAge):
    def sendText(givenText,
                 replyingMessageID=0,
                 keyboardLayout=[]
                 ):  # A simple wrapper for botSendFunctions.sendText()
        botSendFunctions.sendText(bot, chat_id, givenText, replyingMessageID,
                                  keyboardLayout)

    gameRecords = Base(
        "chatStorage/records.pdl"
    )  # The following two lines create a DB to map gameID's to group chat ID's and player ID's
    gameRecords.create("gameID",
                       "groupChatID",
                       "memberUsernames",
                       "memberUserIDs",
                       "memberChatIDs",
                       "memberPoints",
                       "creator",
                       "creatorChatID",
                       "started",
                       mode="open")
    gameRecords.create_index(
        "gameID")  # Create a index to make selections by gameID
    gameRecords.create_index(
        "groupChatID")  # Create a index to make selections by groupChatID
    gameRecords.commit()  # Save changes to disk

    try:
        try:
            chatInstances[chat_id]['checking'] = True
        except Exception:
            chatInstances[chat_id] = {'checking': True}
        print("Processing command " + messageText)
        if parsedCommand == "/help" or parsedCommand == "/start":  # The default command Telegram sends to a bot is /start
            s = "This is the Cards Against Huge Manatees Bot\n"
            s += "To create a game type /newgame in a group chat\n"
            s += "To join a game type /join [gameID] in a private chat with the bot\n"
            s += "To start the game after everyone has joined type /startgame in the group chat.\n"
            s += "A new black card will appear in the group chat and your white cards will appear\n"
            s += "in your private chat. The judge will choose the winner of each round.\n"
            s += "To end the game type /quit in the group chat\n"
            s += "To add a custom black card type /addblackcard the card text here To insert a blank use 5 underscores\n"
            s += "To add a custom white card type /addwhitecard the card text here"

            sendText(s)
        elif parsedCommand == "/newgame":
            ident = game.generate()  # Generate a game ID
            gameRecords.insert(ident, str(chat_id), "", "", "", "",
                               currentMessage.from_user.id, "",
                               0)  # Make a new database record skeleton
            gameRecords.commit()
            sendText("The game ID is " + ident + " Please type /join " +
                     ident + " in a private chat with the bot."
                     )  # Send the ID to the group chat
        elif parsedCommand == "/join":
            rec = gameRecords._gameID[currentMessage.text.upper().split()
                                      [1]]  # Get the DB record by GameID
            if not rec:
                sendText("Game ID not found.")
                return
            rec = rec[-1]
            if rec['started']:  # If the game has already started they can't join.
                sendText("The game has already started. Sorry.")
                return
            if rec['groupChatID'] != str(chat_id):
                memberChats = rec['memberChatIDs']
                memberIDs = rec['memberUserIDs']
                memberNames = rec['memberUsernames']
                points = rec['memberPoints']
                if str(chat_id) not in memberChats:
                    if str(currentMessage.from_user.id) == str(rec['creator']):
                        gameRecords.update(rec, creatorChatID=chat_id)
                        gameRecords.commit()
                        sendText("You are the judge of game " +
                                 str(rec['gameID']))
                        return
                    memberChats += str(
                        chat_id
                    ) + " "  # String to list and back for the database.
                    memberIDs += str(currentMessage.from_user.id) + " "
                    memberNames += str(
                        currentMessage.from_user.first_name) + " "
                    points += "0 "
                    gameRecords.update(
                        rec,
                        memberUsernames=memberNames,
                        memberUserIDs=memberIDs,
                        memberChatIDs=memberChats,
                        memberPoints=points
                    )  # On every join update the database record
                    gameRecords.commit()
                    sendText("You have successfully joined the game " +
                             str(rec['gameID']))
                else:
                    sendText("You have already joined the game.")
            else:
                sendText(
                    "Please type this command in a private chat with the bot.")
        elif parsedCommand == "/startgame":
            try:
                rec = gameRecords._groupChatID[str(chat_id)]
                rec = rec[-1]
                if not rec['started']:
                    game.initGameEnv(rec['gameID'])
                    game.playGame(bot, rec['gameID'])
                    gameRecords.update(rec, started=1)
                    gameRecords.commit()
                else:
                    sendText("Game already started.")
            except Exception:
                traceback.format_exc()
                sendText("Error. No game record for this chat found.")
        elif parsedCommand == "/ans":
            game.getAnswers(bot, currentMessage, chat_id)
        elif parsedCommand == "/win":
            game.winner(bot, currentMessage, chat_id)
        elif parsedCommand == "/quit":
            game.endGame(bot, currentMessage, chat_id)
        elif parsedCommand == "/addblackcard":
            admin.customBlackCard(bot, currentMessage, chat_id)
        elif parsedCommand == "/addwhitecard":
            admin.customWhiteCard(bot, currentMessage, chat_id)
        elif parsedCommand[0] == "/":  # Error handling
            sendText("Not a valid command")

    except Exception:
        print(traceback.format_exc())
class SignalDB:
    def __init__(self, name, path='./', mode="open"):
        '''mode can be 'override' '''

        name_sig = name + '_sig.pdl'
        name_sig = join(path, name_sig)
        self.db_sig = Base(name_sig)
        self.db_sig.create('signal_bundle',
                           'signals',
                           'timestamps',
                           'name',
                           'labels',
                           'md5',
                           'sublabel',
                           mode=mode)
        self.db_sig.open()

    def commit(self):
        self.db_sig.commit()

    def tohash(self, data):
        md5 = hashlib.md5(pickle.dumps(data)).hexdigest()
        return md5

    def findld(self, ld):
        md5 = self.tohash(ld.signal_bundle.signals[0])
        recarr = [r for r in (self.db_sig('md5') == md5)]
        if len(recarr) > 1:
            print 'duplicate signal bundles'
        elif len(recarr) == 1:
            r = recarr[0]
            sb = r['signal_bundle']
            labels = r['labels']
            name = r['name']
            ld_out = LabeledData(sb)
            ld_out.labels = labels
            return ld_out
        else:
            print "signal doesn't currently exist"
            return None

    def add_labeleddata(self, ld, overwrite=False):
        md5 = self.tohash(ld.signal_bundle.signals[0])
        recarr = [r for r in (self.db_sig('md5') == md5)]
        if len(recarr) > 1:
            print 'duplicate signal bundles'
        if not recarr:
            self.db_sig.insert(signal_bundle = ld.signal_bundle,\
                               signals = ld.signal_bundle.signals,\
                               timestamps = ld.signal_bundle.timestamps,\
                               name = ld.signal_bundle.name, \
                               labels = ld.labels,\
                               md5 = md5)

        else:
            rec = recarr[0]
            print rec['__id__']
            if overwrite == False:
                for idx, (reclabel,
                          ldlabel) in enumerate(zip(rec['labels'], ld.labels)):
                    if reclabel == '':
                        rec['labels'][idx] = ldlabel
            else:
                for idx, (reclabel,
                          ldlabel) in enumerate(zip(rec['labels'], ld.labels)):
                    if ldlabel != '':
                        rec['labels'][idx] = ldlabel

            # print rec['__id__'],rec['labels']
            self.db_sig.update(rec, labels=rec['labels'])

    def get_labeleddata(self):
        ld_arr = []
        # signal_bundle, timestamps = None, label = "")
        for r in self.db_sig:
            sb = r['signal_bundle']
            labels = r['labels']
            name = r['name']
            ld = LabeledData(sb)
            ld.labels = labels
            ld_arr.append(ld)
        return ld_arr
예제 #45
0
class DataBase():
    def __init__(self, mode):
        self.db = Base("./mlfv_hosts.pdl")
        self.db.create('ip',
                       'port',
                       'libs',
                       'cpu',
                       'mem',
                       'net',
                       'runs',
                       mode=mode)

    def insert_reg(self, r):
        if (len(r) < 6):
            print("Bad insertion")
            return False
        if self.db.exists():
            print self.db.insert(ip=r['ip'],
                                 port=r['port'],
                                 libs=r['libs'],
                                 cpu=r['cpu'],
                                 mem=r['mem'],
                                 net=r['net'],
                                 runs=0)
            self.db.commit()
            return True

    def remove_reg(self, r):
        if self.db.exists():
            for i in self.db:
                if i['ip'] == r['ip'] and i['port'] == r['port']:
                    self.db.delete(i)
                    self.db.commit()
                    print str(i['ip']) + " removed\n"
                    return True
        return False

    def print_all(self):
        if self.db.exists():
            for r in self.db:
                print r

    def get_less_runs(self):
        if self.db.exists():
            less_runs = sys.maxsize
            for r in self.db:
                if (r['runs'] < less_runs):
                    new_host = r
                    less_runs = r['runs']
            new_host['runs'] += 1
            self.db.commit()
            return new_host

    def get_hosts_cpu_mem(self, cpu, mem):
        print "cpu", cpu, mem
        ret = []
        if self.db.exists():
            for r in (self.db("cpu") >= cpu) & (self.db("mem") >= mem):
                ret.append([
                    r['ip'], r['port'], r['libs'], r['cpu'], r['mem'], r['net']
                ])
            #ret = [r for r in self.db if r['cpu'] >= cpu and r['mem'] >= mem]
            return ret
        else:
            print("Error: no client!")
            return None

    def get_registers_values(self):
        if self.db.exists():
            l = []
            for r in self.db:
                l.append([
                    r['ip'], r['port'], r['libs'], r['cpu'], r['mem'], r['net']
                ])
            a = np.array(l, dtype=object)
            return a
예제 #46
0
def calculatingWeights(graph, nodesnotLinked, database, calculatingFile):
    pdb = Base(calculatingFile)
    pdb.create('node1', 'node2', 'WCNFTI01', 'WCNFTI02', 'WCNFTI03',
               'WCNFTI04', 'WCNFTI05', 'WCNFTI06', 'WCNFTI07', 'WCNFTI08',
               'WCNFTI09', 'WAAFTI01', 'WAAFTI02', 'WAAFTI03', 'WAAFTI04',
               'WAAFTI05', 'WAAFTI06', 'WAAFTI07', 'WAAFTI08', 'WAAFTI09')
    pdb.create_index('node1', 'node2')

    element = 0
    qtyofNodesToProcess = len(nodesnotLinked)
    for pair in nodesnotLinked:
        element = element + 1
        FormatingDataSets.printProgressofEvents(
            element, qtyofNodesToProcess,
            "Calculating features for nodes not liked: ")
        neighbors_node1 = all_neighbors(graph, pair[0])
        neighbors_node2 = all_neighbors(graph, pair[1])
        len_neihbors_node1 = len(neighbors_node1)
        len_neihbors_node2 = len(neighbors_node2)
        CommonNeigbors = neighbors_node1.intersection(neighbors_node2)
        WCNFTI01 = 0
        WCNFTI02 = 0
        WCNFTI03 = 0
        WCNFTI04 = 0
        WCNFTI05 = 0
        WCNFTI06 = 0
        WCNFTI07 = 0
        WCNFTI08 = 0
        WCNFTI09 = 0

        WAAFTI01 = 0
        WAAFTI02 = 0
        WAAFTI03 = 0
        WAAFTI04 = 0
        WAAFTI05 = 0
        WAAFTI06 = 0
        WAAFTI07 = 0
        WAAFTI08 = 0
        WAAFTI09 = 0

        for cn in CommonNeigbors:
            item = get_partOfWeightCalculating(graph, database, pair, cn)
            WCNFTI01 = WCNFTI01 + item['WCN'][0]
            WCNFTI02 = WCNFTI02 + item['WCN'][1]
            WCNFTI03 = WCNFTI03 + item['WCN'][2]
            WCNFTI04 = WCNFTI04 + item['WCN'][3]
            WCNFTI05 = WCNFTI05 + item['WCN'][4]
            WCNFTI06 = WCNFTI06 + item['WCN'][5]
            WCNFTI07 = WCNFTI07 + item['WCN'][6]
            WCNFTI08 = WCNFTI08 + item['WCN'][7]
            WCNFTI09 = WCNFTI09 + item['WCN'][8]

            WAAFTI01 = WAAFTI01 + item['WAA'][0]
            WAAFTI02 = WAAFTI02 + item['WAA'][1]
            WAAFTI03 = WAAFTI03 + item['WAA'][2]
            WAAFTI04 = WAAFTI04 + item['WAA'][3]
            WAAFTI05 = WAAFTI05 + item['WAA'][4]
            WAAFTI06 = WAAFTI06 + item['WAA'][5]
            WAAFTI07 = WAAFTI07 + item['WAA'][6]
            WAAFTI08 = WAAFTI08 + item['WAA'][7]
            WAAFTI09 = WAAFTI09 + item['WAA'][8]

        pdb.insert(
            str(pair[0]),
            str(pair[1]),
            WCNFTI01,
            WCNFTI02,
            WCNFTI02,
            WCNFTI03,
            WCNFTI04,
            WCNFTI05,
            WCNFTI06,
            WCNFTI07,
            WCNFTI08,
            WCNFTI09,
            WAAFTI01,
            WAAFTI02,
            WAAFTI02,
            WAAFTI03,
            WAAFTI04,
            WAAFTI05,
            WAAFTI06,
            WAAFTI07,
            WAAFTI08,
            WAAFTI09,
        )
    pdb.commit()
    return pdb
예제 #47
0
def process(bot, chat_id, parsedCommand, messageText, currentMessage, update, instanceAge):
    def sendText(givenText, replyingMessageID=0, keyboardLayout=[]):
        if not chatInstanceArray[chat_id]['adminDisable']:
            atbSendFunctions.sendText(bot, chat_id, givenText, replyingMessageID, keyboardLayout)

    def sendPhoto(imageName):
        atbSendFunctions.sendPhoto(bot, chat_id, "images/"+ imageName)

    def sendSticker(stickerName):
        atbSendFunctions.sendSticker(bot, chat_id, "stickers/"+ stickerName)

    def passSpamCheck():
        return atbMiscFunctions.spamCheck(chat_id, currentMessage.date)


    try:
        chatInstanceArray[chat_id]['checking'] = True
    except Exception:
        chatInstanceArray[chat_id] = {'checking': True, 'adminDisable': False, 'spamTimestamp': 0, 'shottyTimestamp': 0, 'shottyWinner': "", 'checkingVehicles': False, 'whoArray': []}


    try:
        #commands go here, in this if-elif block. Python doesn't have switch statements.
        if parsedCommand == "/mom": #sends "MOM GET THE CAMERA"
            sendText("MOM GET THE CAMERA")

        elif atbMiscFunctions.isMoom(parsedCommand): #sends M {random number of Os} M
            if passSpamCheck(): #use this to prevent spamming of a command
                response = "M"
                for i in range(0, random.randint(3, 75)):
                    response += "O"
                sendText(response + "M")

        elif parsedCommand == "/swag":
            sendText("swiggity swag, what\'s in the bag?")

        elif parsedCommand == "/worms":
            if passSpamCheck():
                response = "hey man can I borrow your "
                if len(messageText) > len("/worms "):
                    response += messageText[len("/worms "):]
                else:
                    response += "worms"
                sendText(response)

        elif parsedCommand == "/shh" or parsedCommand == "/shhh":
            if passSpamCheck():
                sendPhoto("shhh.jpg")

        elif parsedCommand == "/father":
            if (random.randint(0, 1)):
                sendText("You ARE the father!")
            else:
                sendText("You are NOT the father!")

        elif parsedCommand == "/rip":   #sends "I can't believe that [name (defaults to sender's name)] is f*****g dead."
            if passSpamCheck():
                response = "I can't believe that "
                if len(messageText) > len("/rip "):
                    if (messageText[len("/rip "):] == "me"):
                        response += currentMessage.from_user.first_name
                    else:
                        response += messageText[len("/rip "):]
                else:
                    response += currentMessage.from_user.first_name
                response += " is f*****g dead."
                sendText(response)

        elif parsedCommand == "/scrub":
            checkingStats = False
            try:
                if currentMessage.text.lower().split()[1] == "stats":
                    db = Base('chatStorage/scrub.pdl') #The path to the DB
                    db.create('username', 'name', 'counter', mode="open")
                    K = list()
                    for user in db:
                        K.append(user)
                    sortedK = sorted(K, key=lambda x: int(x['counter']), reverse=True)
                    outputString = "SCRUBBIEST LEADERBOARD:\n"
                    for user in sortedK:
                        pluralString = " SCRUB POINT"
                        if not(int(user['counter']) == 1):
                            pluralString += "S"
                        pluralString += "\n"
                        outputString += user['name'].upper() + ": " + str(user['counter']) + pluralString
                    sendText(outputString)
                    checkingStats = True
            except IndexError:
                pass

            if not checkingStats and (currentMessage.from_user.id == 169883788 or currentMessage.from_user.id == 44961843):
                db = Base('chatStorage/scrub.pdl')
                db.create('username', 'name', 'counter', mode="open")

                userWasFound = False
                valueSuccessfullyChanged = False

                for user in db:
                    if int(user['username']) == currentMessage.reply_to_message.from_user.id:
                        db.update(user, counter=int(user['counter']) + 1)
                        valueSuccessfullyChanged = True
                        userWasFound = True
                db.commit()

                if not userWasFound:
                    db.insert(currentMessage.reply_to_message.from_user.id, currentMessage.reply_to_message.from_user.first_name, 1)
                    db.commit()

                if valueSuccessfullyChanged or not userWasFound:
                    sendText("Matt Gomez awarded a scrub point to " + currentMessage.reply_to_message.from_user.first_name + ".")

            elif not checkingStats:
                sendText("AdamTestBot, powered by ScrubSoft (C)")

        elif parsedCommand == "/hiss":
            checkingStats = False
            try:
                if currentMessage.text.lower().split()[1] == "stats":
                    db = Base('chatStorage/hiss.pdl')
                    db.create('username', 'name', 'counter', mode="open")
                    K = list()
                    for user in db:
                        K.append(user)
                    sortedK = sorted(K, key=lambda x: int(x['counter']), reverse=True)
                    outputString = "Hiss Leaderboard:\n"
                    for user in sortedK:
                        pluralString = " hiss"
                        if not(int(user['counter']) == 1):
                            pluralString += "es"
                        pluralString += "\n"
                        outputString += user['name'] + ": " + str(user['counter']) + pluralString
                    sendText(outputString)
                checkingStats = True
            except IndexError:
                pass

            if not checkingStats and (currentMessage.from_user.id == 122526873 or currentMessage.from_user.id == 44961843):
                db = Base('chatStorage/hiss.pdl')
                db.create('username', 'name', 'counter', mode="open")

                userWasFound = False
                valueSuccessfullyChanged = False

                for user in db:
                    if int(user['username']) == currentMessage.reply_to_message.from_user.id:
                        db.update(user, counter=int(user['counter']) + 1)
                        valueSuccessfullyChanged = True
                        userWasFound = True
                db.commit()
                
                if not userWasFound:
                    db.insert(currentMessage.reply_to_message.from_user.id, currentMessage.reply_to_message.from_user.first_name, 1)
                    db.commit()

                if valueSuccessfullyChanged or not userWasFound:
                    sendText("Robyn hissed at " + currentMessage.reply_to_message.from_user.first_name + ".")

        elif parsedCommand == "/water":
            if (random.randint(0, 1) == 0):
                sendSticker("water.webp")
            else:
                sendSticker("hoboken_water.webp")

        #this command should go last:
        elif parsedCommand == "/community": #add your command to this list
            response = "/mom - get the camera\n"
            response += "/mooom (any number of \'o\'s) - call for help\n"
            response += "/swag - more memes\n"
            response += "/worms - can I borrow them?\n"
            response += "/shh(h) - here, be relaxed\n"
            response += "/father - are you the father?\n"
            response += "/rip (something) - I can't believe they're dead!\n"
            response += "/hiss stats - see how many time Robyn has hissed at people\n"
            response += "/scrub or /scrub stats - see who sponsors me or how many times Matt Gomez has called you a scrub\n"
            response += "/water - does this water look brown to you?"
            sendText(response)

        else:
            return False

        return True
    except Exception:
        print traceback.format_exc()
        return False
예제 #48
0
# -*- coding: utf-8 -*-
from pydblite import Base
'''
序列化采用cPickle
'''

db = Base("test.db", save_to_file=False)

if db.exists():
    db.open()
else:
    db.create("name", "age")

db.insert("bob", 10)
index = db.insert(name="alice", age=20)

print db[index] # 按照主键访问record

record = db[1]

db.update(record, name="dellian")

#db.delete(record)

# db.records (所有记录)

# query
for r in db("age") > 10:
    print r

예제 #49
0
#   become too convoluted because you have to create too much
#   to bridge that gap, then move to another DB library.

import pydblite

from pydblite import Base

# seems that only one field per db
db = Base('test.pdl')  #creates database/single-table
db.create('name', 'age', 'size', mode='override')  #creates fields
# mode='open', opens if the db exists and creates if not
# mode='override', overides existing db with new one, use for testing
# implicit __id__ value included to ensure primary_key

#both insert
db.insert(name='billy', age=17, size=1.5)  #by keywords
db.insert('elegia', 83, 2.1)  #by position
db.insert('jeremy', 702, 0.2)

db.commit()  #F****N IMPORTANT DONT FORGET!!!!!!!!!!!!!!!! <----------------

# commits changes
# using open() to pull values from dick, nullifying uncommited changes


#iterate over all records
def fp():  #full print
    for r in db:
        print(r)
    print('#######')
예제 #50
0
    user.create('login', 'password_hash', 'name', 'email', 'phone')

sailors = Base('db/sailors.pdl')
if sailors.exists():
    sailors.open()
else:
    sailors.create('firstname', 'lastname', 'speciality', 'hiredate', 'ship_empl')

ships = Base('db/ships.pdl')
if ships.exists():
    ships.open()
else:
    ships.create('name', 'type', 'country')

if len(ships) == 0:
    ships.insert('Dwight D. Eisenhower', 'Aircraft carrier', 'USA')
    ships.insert('Carl Vinson', 'Aircraft carrier', 'GB')
    ships.insert('Udaloy', 'Destroyer', 'RUS')
    ships.insert('Kirov', 'Battlecruiser', 'RUS')
    ships.commit()

if len(sailors) == 0:
    sailors.insert('Valisyi', 'Bykov', 'Chief cook', datetime.strptime('2013-09-28 20:30:55.78200', "%Y-%m-%d %H:%M:%S.%f"), 4)
    sailors.insert('Yaroslav', 'Galych', 'Seaman', datetime.strptime('2013-09-28 20:30:55.78200', "%Y-%m-%d %H:%M:%S.%f"), 4)
    sailors.insert('Cavin', 'Lanister', 'Boatswain', datetime.strptime('2013-09-28 20:30:55.78200', "%Y-%m-%d %H:%M:%S.%f"), 1)
    sailors.insert('Mark', 'Brown', 'Physician', datetime.strptime('2013-09-28 20:30:55.78200', "%Y-%m-%d %H:%M:%S.%f"), 2)
    sailors.insert('Nick', 'Carroll', 'Seaman', datetime.strptime('2013-09-28 20:30:55.78200', "%Y-%m-%d %H:%M:%S.%f"), 2)
    sailors.insert('Eugene', 'Crownsberg', datetime.strptime('2013-09-28 20:30:55.78200', "%Y-%m-%d %H:%M:%S.%f"), 3)
    sailors.insert('Ulrich', 'Bloodaxe', 'Captain', datetime.strptime('2013-09-28 20:30:55.78200', "%Y-%m-%d %H:%M:%S.%f"), 3)
    sailors.commit()
def calculatingWeights(graph, nodesnotLinked, database, calculatingFile):
    pdb = Base(calculatingFile)
    pdb.create('node1', 'node2', 'WCNFTI01','WCNFTI02', 'WCNFTI03','WCNFTI04','WCNFTI05','WCNFTI06','WCNFTI07','WCNFTI08','WCNFTI09','WAAFTI01','WAAFTI02', 'WAAFTI03','WAAFTI04','WAAFTI05','WAAFTI06','WAAFTI07','WAAFTI08','WAAFTI09')
    pdb.create_index('node1', 'node2')
                
    element = 0
    qtyofNodesToProcess = len(nodesnotLinked)
    for pair in nodesnotLinked:
        element = element+1
        FormatingDataSets.printProgressofEvents(element, qtyofNodesToProcess, "Calculating features for nodes not liked: ")
        neighbors_node1 = all_neighbors(graph, pair[0])
        neighbors_node2 = all_neighbors(graph, pair[1])
        len_neihbors_node1 = len(neighbors_node1)
        len_neihbors_node2 = len(neighbors_node2)
        CommonNeigbors = neighbors_node1.intersection(neighbors_node2)
        WCNFTI01 = 0;
        WCNFTI02 = 0;
        WCNFTI03 = 0;
        WCNFTI04 = 0;
        WCNFTI05 = 0;
        WCNFTI06 = 0;
        WCNFTI07 = 0;
        WCNFTI08 = 0;
        WCNFTI09 = 0;
        
        WAAFTI01 = 0;
        WAAFTI02 = 0;
        WAAFTI03 = 0;
        WAAFTI04 = 0;
        WAAFTI05 = 0;
        WAAFTI06 = 0;
        WAAFTI07 = 0;
        WAAFTI08 = 0;
        WAAFTI09 = 0;
        
        
        for cn in CommonNeigbors:
            item = get_partOfWeightCalculating(graph, database, pair, cn)
            WCNFTI01 = WCNFTI01 + item['WCN'][0];
            WCNFTI02 = WCNFTI02 + item['WCN'][1];
            WCNFTI03 = WCNFTI03 + item['WCN'][2];
            WCNFTI04 = WCNFTI04 + item['WCN'][3];
            WCNFTI05 = WCNFTI05 + item['WCN'][4];
            WCNFTI06 = WCNFTI06 + item['WCN'][5];
            WCNFTI07 = WCNFTI07 + item['WCN'][6];
            WCNFTI08 = WCNFTI08 + item['WCN'][7];
            WCNFTI09 = WCNFTI09 + item['WCN'][8];
            
            WAAFTI01 = WAAFTI01 + item['WAA'][0];
            WAAFTI02 = WAAFTI02 + item['WAA'][1];
            WAAFTI03 = WAAFTI03 + item['WAA'][2];
            WAAFTI04 = WAAFTI04 + item['WAA'][3];
            WAAFTI05 = WAAFTI05 + item['WAA'][4];
            WAAFTI06 = WAAFTI06 + item['WAA'][5];
            WAAFTI07 = WAAFTI07 + item['WAA'][6];
            WAAFTI08 = WAAFTI08 + item['WAA'][7];
            WAAFTI09 = WAAFTI09 + item['WAA'][8];
            
        pdb.insert(str(pair[0]), str(pair[1]), WCNFTI01, WCNFTI02,  WCNFTI02,
                   WCNFTI03,WCNFTI04,WCNFTI05,WCNFTI06,WCNFTI07,WCNFTI08,WCNFTI09,
                   WAAFTI01, WAAFTI02,  WAAFTI02,
                   WAAFTI03,WAAFTI04,WAAFTI05,WAAFTI06,WAAFTI07,WAAFTI08,WAAFTI09,
                    
                    )   
    pdb.commit()
    return pdb;