Пример #1
0
    def _receive_packet(self):
        while True:
            robots_list = db.table('robots').all()
            for id in range(6):
                updated_robot = {
                    "info": {
                        "id": id,
                        "name": "Robot {}".format(id)
                    },
                    "supply": {
                        "batt": random.random() * 100,
                        "voltage": random.random() * 16,
                        "current": random.random() * 5,
                        "power": random.random()
                    },
                    "com": {
                        "time_since_last_packet": 2
                    }
                }

                if any(robot['info']['name'] == updated_robot['info']['name']
                       for robot in robots_list):
                    sio.emit('robots_update', updated_robot)
                    with transaction(db.table('robots')) as robots:
                        robots.update(
                            updated_robot,
                            where('info').name == updated_robot['info']
                            ['name'])
                else:
                    sio.emit('robots_insert', updated_robot)
                    with transaction(db.table('robots')) as robots:
                        robots.insert(updated_robot)

                time.sleep(1)
    def save(self):
        results = self.db().search(Query().command == self.command)
        doc_ids_to_delete = [sfx.doc_id for sfx in results]
        if doc_ids_to_delete:
            from tinyrecord import transaction

            with transaction(self.db()) as tr:
                tr.remove(doc_ids=doc_ids_to_delete)

        print(f"Creating New SFX Request: {self.doc()}")
        from tinyrecord import transaction

        with transaction(self.db()) as tr:
            tr.insert(self.doc())
        return self.doc()
Пример #3
0
def test_remove_eids(db):
    eid = db.insert({'x': 1})

    with transaction(db) as tr:
        tr.remove(eids=[eid])

    assert not db.get(eid=eid)
Пример #4
0
def collect(testid=''):
    if request.headers['Content-Type'].startswith(
            'application/x-www-form-urlencoded'):
        try:
            db = app.config['db']
            payload = json.loads(request.form['sessionJSON'])
            payload = casting.cast_recursively(payload)
            insert = casting.json_to_dict(payload)

            collection = db.table(payload['trials'][0]['testId'])
            with transaction(collection):
                inserted_ids = collection.insert_multiple(insert)
            print(inserted_ids)

            return jsonify({
                'error':
                False,
                'message':
                "Saved as ids %s" % ','.join(map(str, inserted_ids))
            })
        except Exception as e:
            return jsonify({
                'error': True,
                'message': "An error occurred: %s" % str(e)
            })
    else:
        return "415 Unsupported Media Type", 415
Пример #5
0
def stop_scan(scan_id):
    res = {"page": "stopscan"}
    scan_id = str(scan_id)

    item = table.search(Query().scan_id == scan_id)

    # todo: use this.scans and nessus_scan_id
    if not item:
        res.update({"status": "error", "reason": "scan_id '{}' not found".format(scan_id)})
        return jsonify(res)

    if item[0]['options']['action'] == 'scan':
        this.nessscan.action(
            action="scans/"+str(item[0]["nessscan_id"])+"/stop",
            method="POST")

    if this.nessscan.res != {}:
        res.update({"status": "error", "reason": this.nessscan.res['error']})
        return jsonify(res)
    with transaction(table) as tr:
        tr.update({
            "status": "STOPPED",
            "finished_at": int(time.time() * 1000)},
            where('scan_id') == scan_id)

    res.update({"status": "success", "scan": item[0]})
    return jsonify(res)
Пример #6
0
 def update(self, uuid: str, data: dict):
     with transaction(self.table) as tr:
         record = self.table.get(where('uuid') == uuid)
         if record:
             tr.update(data, where('uuid') == uuid)
         else:
             raise NoResultFound('object do not exist')
Пример #7
0
def test_remove_eids(db):
    eid = db.insert({'x': 1})

    with transaction(db) as tr:
        tr.remove(eids=[eid])

    assert not db.get(eid=eid)
Пример #8
0
 def delete(self, uuid: str):
     with transaction(self.table) as tr:
         record = self.table.get(where('uuid') == uuid)
         if record:
             tr.remove(where('uuid') == uuid)
         else:
             raise NoResultFound('object do not exist')
Пример #9
0
def add_to_list(name, value, force=False):
	global db
	with db_lock:
		ListQ = Query()

		lst = [ ]

		table = db.table(LIST_TABLE)
		ans = table.get(ListQ.name == name)
		if ans:
			lst = ans['value']

		res = len(lst)

		if value not in lst or force:
			lst.append(value)
		else:
			res = -1

		with transaction(table) as tr:
			if ans:
				tr.update({'value':lst}, ListQ.name == name)
			else:
				tr.insert({'name':name,'value':lst})

	return res
Пример #10
0
def add_to_list(name, value, force=False):
    global db
    with db_lock:
        ListQ = Query()

        lst = []

        table = db.table(LIST_TABLE)
        ans = table.get(ListQ.name == name)
        if ans:
            lst = ans['value']

        res = len(lst)

        if value not in lst or force:
            lst.append(value)
        else:
            res = -1

        with transaction(table) as tr:
            if ans:
                tr.update({'value': lst}, ListQ.name == name)
            else:
                tr.insert({'name': name, 'value': lst})

    return res
Пример #11
0
def insertfew():
    from storage.tiny import tinydb_model
    global globalCounter, threadLock
    time.sleep(3)
    while True:
        # pwm_table = db.tinydb.table('pwm')
        pwm = tinydb_model.Pwm()
        pwm.id = globalCounter
        # pwm_table = pwm.coll.table
        pwm.name = 'boiler {}'.format(random.randint(1, 100))
        pwm.frequency = random.randint(10, 800)
        pwm.duty_cycle = random.randint(1, 1000000)
        pwm.gpio_pin_code = random.randint(1, 40)
        pwm.host_name = 'pizero1'
        try:
            # with transaction(pwm_table) as tr:
            #    res = tr.insert(pwm.dictx())
            #    print res
            with transaction(pwm.coll.table):
                # res = pwm.coll.insert_one(pwm.dictx(), bypass_document_validation=True)
                # print res.inserted_id
                pass
            with threadLock:
                globalCounter += 1
            print(globalCounter)
        except Exception as ex:
            print(ex)
Пример #12
0
def nsd_from_best(pairs_in, db, thresh, delta, exclude_best):
    pvalmat, orig_scores, docs = load_pairs_in(pairs_in)
    graph = mk_nsd_graph(pvalmat, thresh)
    for idx, (score, doc) in enumerate(zip(orig_scores, docs)):
        logger.info("%s %s %s", idx, doc, score)
    max_score = max(orig_scores)
    if exclude_best:
        max_score = max((score for score in orig_scores if score < max_score))
    max_scores = [
        idx for idx, score in enumerate(orig_scores)
        if score + delta > max_score
    ]
    logger.info("max_scores: %s", max_scores)
    nsd_from_max = set(max_scores) | {
        other_idx
        for idx in max_scores for other_idx in graph[idx]
    }
    logger.info("nsd_from_max: %s", nsd_from_max)
    max_guesses = [docs[idx] for idx in max_scores]
    nsd_from_max_guesses = [docs[idx] for idx in nsd_from_max]

    with transaction(db) as tr:
        tr.insert({
            "type": "highlight-guesses",
            "guesses": nsd_from_max_guesses,
            "max": max_guesses,
        })
Пример #13
0
def test_atomicity(db):
    with raises(ValueError):
        with transaction(db) as tr:
            tr.insert({})
            tr.insert({'x': 1})
            tr.update({'x': 2}, where('x') == 1)
            raise ValueError
    assert len(db) == 0
Пример #14
0
def test_update(db):
    eid = db.insert({'x': 1})

    with transaction(db) as tr:
        tr.update({'x': 2}, where('x') == 1)
        tr.update({'x': 3}, eids=[eid])

    assert db.get(where('x') == 3).eid == eid
Пример #15
0
def test_atomicity(db):
    with raises(ValueError):
        with transaction(db) as tr:
            tr.insert({})
            tr.insert({'x': 1})
            tr.update({'x': 2}, where('x') == 1)
            raise ValueError
    assert len(db) == 0
Пример #16
0
def set_credentials(creds_file):
    with get_credentials_table() as table:
        exists = get_credentials(table)
        with transaction(table) as tr:
            if exists:
                tr.update({'path': creds_file}, where('path').exists())
            else:
                tr.insert({'path': creds_file})
Пример #17
0
 def replace(self, structure):
     highest_id = self._get_highest_id()
     rmList = list(range(0,highest_id + 1))
     trans = DB_PRODUCT_STRUCTURE()
     with transaction(trans) as tr:
         tr.insert(structure)
         tr.remove(doc_ids=rmList)
     return structure
Пример #18
0
def test_update(db):
    eid = db.insert({'x': 1})

    with transaction(db) as tr:
        tr.update({'x': 2}, where('x') == 1)
        tr.update({'x': 3}, eids=[eid])

    assert db.get(where('x') == 3).eid == eid
Пример #19
0
def db_insert(idn, latitude, longitude, timestamp):
    with transaction(db_points) as tr:
        tr.insert({
            'id': idn,
            'lat': latitude,
            'lng': longitude,
            'time': timestamp
        })
Пример #20
0
def increment_values():
    for i in range(100):
        with transaction(table) as tr:

            def f(doc):
                doc['value'] += 1

            tr.update_callable(f, where('name') == 'test')
Пример #21
0
def set_credentials(creds_file):
    table = TinyDB(DB_FILE, create_dirs=True).table('credentials')
    exists = get_credentials()

    with transaction(table) as tr:
        if exists:
            tr.update({'path': creds_file}, where('path').exists())
        else:
            tr.insert({'path': creds_file})
Пример #22
0
def test_remove_doc_ids(db):
    doc_id = db.insert({'x': 1})
    other_doc_id = db.insert({'x': 4})

    with transaction(db) as tr:
        tr.remove(doc_ids=[doc_id])

    assert not db.get(doc_id=doc_id)
    assert db.get(doc_id=other_doc_id)
Пример #23
0
def test_remove_doc_ids(db):
    doc_id = db.insert({'x': 1})
    other_doc_id = db.insert({'x': 4})

    with transaction(db) as tr:
        tr.remove(doc_ids=[doc_id])

    assert not db.get(doc_id=doc_id)
    assert db.get(doc_id=other_doc_id)
Пример #24
0
def test_update(db):
    doc_id = db.insert({'x': 1})
    other_doc_id = db.insert({'x': 4})

    with transaction(db) as tr:
        tr.update({'x': 2}, where('x') == 1)
        tr.update({'x': 3}, doc_ids=[doc_id])

    assert db.get(where('x') == 3).doc_id == doc_id
    assert db.get(where('x') == 4).doc_id == other_doc_id
Пример #25
0
def test_update_callable(db):
    [db.insert({'x': {'u': 10}}) for i in range(5)]

    with transaction(db) as tr:
        def function(t):
            t['x']['u'] = 1
        tr.update_callable(function, lambda x: True)

    assert len(db) == 5
    assert all(x['x']['u'] == 1 for x in db.search(lambda x: True))
Пример #26
0
def test_update(db):
    doc_id = db.insert({'x': 1})
    other_doc_id = db.insert({'x': 4})

    with transaction(db) as tr:
        tr.update({'x': 2}, where('x') == 1)
        tr.update({'x': 3}, doc_ids=[doc_id])

    assert db.get(where('x') == 3).doc_id == doc_id
    assert db.get(where('x') == 4).doc_id == other_doc_id
Пример #27
0
def clear_list(name):
	global db
	ListQ = Query()

	with db_lock:
		table = db.table(LIST_TABLE)

		with transaction(table) as tr:
			tr.update({'value': [ ]}, ListQ.name == name)

		set_variable(name, [ ], table=LIST_TABLE)
Пример #28
0
    def set_value(self, field, value):
        def _update_that_value():
            def transform(doc):
                doc[field] = value

            return transform

        from tinyrecord import transaction

        with transaction(self.db()) as tr:
            tr.update_callable(_update_that_value(), Query().user == self.user)
Пример #29
0
def proc_score(exp, db, measures, guess, gold, **kwargs):
    result = exp.info()
    result["measures"] = measures
    result["guess"] = guess
    result["gold"] = gold
    result["time"] = time()
    result.update(kwargs)

    with transaction(db) as tr:
        tr.insert(result)
    return measures
Пример #30
0
    def _update_value(self, field, amount=1):
        def _update_that_value():
            def transform(doc):
                doc[field] = doc[field] + amount

            return transform

        from tinyrecord import transaction

        with transaction(self.db()) as tr:
            tr.update_callable(_update_that_value(), Query().version == self.version)
Пример #31
0
    def set_value_by_id(cls, doc_id, field, value):
        def _update_that_value():
            def transform(doc):
                doc[field] = value

            return transform

        from tinyrecord import transaction

        with transaction(cls.db()) as tr:
            tr.update_callable(_update_that_value(), doc_ids=[doc_id])
Пример #32
0
 def create(self, data: dict, max_retries: int = 10) -> dict:
     with transaction(self.table) as tr:
         while max_retries > 0:
             uuid = uuid4()
             if not self.table.contains(where('uuid') == str(uuid)):
                 data.update(uuid=str(uuid))
                 tr.insert(data)
                 return data
             else:
                 max_retries -= 1
         raise StorageError('could not set unique UUID')
Пример #33
0
def clear_list(name):
    global db
    ListQ = Query()

    with db_lock:
        table = db.table(LIST_TABLE)

        with transaction(table) as tr:
            tr.update({'value': []}, ListQ.name == name)

        set_variable(name, [], table=LIST_TABLE)
Пример #34
0
def set_variable(name, value, table=VAR_TABLE):
    global db

    with db_lock:
        Variable = Query()

        table = db.table(VAR_TABLE)
        with transaction(table) as tr:
            if table.contains(Variable.name == name):
                tr.update({'value': value}, Variable.name == name)
            else:
                tr.insert({'name': name, 'value': value})
Пример #35
0
    def pop_all_off(self):
        from tinyrecord import transaction

        all_effects = self.all()

        doc_ids_to_delete = [sfx.doc_id for sfx in all_effects]
        if doc_ids_to_delete:
            with transaction(self.db()) as tr:
                tr.remove(doc_ids=doc_ids_to_delete)
            return all_effects
        else:
            return []
Пример #36
0
    def _find_or_create_cube_bet(self):
        result = self.db().get(Query().user == self.user)

        if result:
            return result

        success(f"Creating New Cube Bet: {self.doc()}")
        from tinyrecord import transaction

        with transaction(self.db()) as tr:
            tr.insert(self.doc())
        return self.doc()
Пример #37
0
def test_update_callable(db):
    [db.insert({'x': {'u': 10}}) for i in range(5)]

    with transaction(db) as tr:

        def function(t):
            t['x']['u'] = 1

        tr.update_callable(function, lambda x: True)

    assert len(db) == 5
    assert all(x['x']['u'] == 1 for x in db.search(lambda x: True))
Пример #38
0
    def save(self):
        bet = self.db().get(Query().user == self.user)

        if bet:
            self.set_value("duration", self._duration)
        else:
            success(f"Creating New Cube Bet: {self.doc()}")
            from tinyrecord import transaction

            with transaction(self.db()) as tr:
                tr.insert(self.doc())
        return self.doc()
Пример #39
0
def set_variable(name, value, table=VAR_TABLE):
	global db

	with db_lock:
		Variable = Query()

		table = db.table(VAR_TABLE)
		with transaction(table) as tr:
			if table.contains(Variable.name == name):
				tr.update({'value':value}, Variable.name == name)
			else:
				tr.insert({'name':name,'value':value})
Пример #40
0
def remove_from_list(name, val):
	lst = get_list(name)
	lst.remove(val)

	global db
	with db_lock:
		ListQ = Query()

		table = db.table(LIST_TABLE)
		with transaction(table) as tr:
			tr.update({'value': lst}, ListQ.name == name)

	set_variable(name, lst, table=LIST_TABLE)
Пример #41
0
def test_remove(db):
    [db.insert({}) for i in range(10)]
    anything = lambda x: True
    db.search(anything)

    with transaction(db) as tr:
        tr.remove(anything)

    assert not db._query_cache
    assert len(db) == 0

    db.insert({})
    assert db.get(anything).eid == 11
Пример #42
0
def get_subreddit_samples(category, subreddit, posts):
    for post in posts:
        print(subreddit + ': ' + post.url)
        if post.is_self:
            content = post.selftext
        else:
            content = get_link_content(post.url)
        if content:
            with transaction(reddit_db) as tr:
                tr.insert({
                    'url': post.url,
                    'text': content,
                    'label': category,
                })
Пример #43
0
def test_single_commit_per_transaction(repo, db):
    db.insert({'a': 1})
    # 2 commits so far, one for intializations, one for adding insert

    commit = repo[db._storage._refname]
    parent_commit = repo[commit.parents[0]]

    assert parent_commit.parents == []

    # create transction with 3 inserts, this should return into a single
    # new commit
    with transaction(db.table('_default')) as t:
        t.insert({'b': 2})
        t.insert({'c': 3})
        t.insert({'d': 4})

    latest = repo[db._storage._refname]
    assert latest.parents == [commit.id]
Пример #44
0
 def _update_words(self, updates):
     self._up_to_date = False
     words = tuple(updates.keys())
     new_words = []
     known_words = []
     for word in words:
         known = word in self
         if known:
             known_words.append(word)
         else:
             new_words.append(word)
     with transaction(self._lex) as tr:
         for word in known_words:
             tr.update({'lex': updates[word]}, where('word') == word)
         for word in new_words:
             tr.insert({'word': word, 'lex': updates[word]})
     for word in words:
         if word in self._cache:
             self._cache[word]['known'] = True
Пример #45
0
def add_to_leaderboard(user, score, leaderboard_name='rooms'):
	global db

	name = user.name
	if user.pet:
		pet = user.get_pet()
		name += ' и {0} {1}'.format(pet.name, pet.real_name)

	with db_lock:
		table = db.table(leaderboard_name)
		doc = {
			'uid': user.uid,
			'name': name,
			'score': score
		}
		if hasattr(user, 'death_reason'):
			doc['death_reason'] = user.death_reason


		with transaction(table) as tr:
			tr.insert(doc)
Пример #46
0
async def get_subreddit_samples(category, subreddit, posts):
    """
    Asynchronously gather text content of posts and store in database.

    :param category: training label
    :param subreddit: subreddit name
    :param posts: list of reddit posts
    """
    for post in posts:
        print(subreddit + ': ' + post.url)
        if post.is_self:
            content = post.selftext
        else:
            content = await get_link_content(post.url)
        if content:
            with transaction(reddit_db) as tr:
                tr.insert({
                    'url': post.url,
                    'text': content,
                    'label': category,
                })
Пример #47
0
def test_insert(db):
    with transaction(db) as tr:
        tr.insert({})

    assert len(db) == 1
    assert db._last_id == 1
Пример #48
0
def test_abort(db):
    with transaction(db) as tr:
        tr.insert({})
        abort()

    assert len(db) == 0
Пример #49
0
def increment_values():
    for i in range(100):
        with transaction(table) as tr:
            def f(doc):
                doc['value'] += 1
            tr.update_callable(f, where('name') == 'test')
Пример #50
0
 def callback():
     with transaction(db) as tr:
         tr.insert({})
         tr.insert({})
Пример #51
0
def test_insert_multiple(db):
    with transaction(db) as tr:
        tr.insert_multiple({} for x in range(5))

    assert len(db) == 5