本文整理汇总了Python中tinydb.where方法的典型用法代码示例。如果您正苦于以下问题:Python tinydb.where方法的具体用法?Python tinydb.where怎么用?Python tinydb.where使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tinydb
的用法示例。
在下文中一共展示了tinydb.where方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_log
# 需要导入模块: import tinydb [as 别名]
# 或者: from tinydb import where [as 别名]
def get_log(database, log_id):
"""
Get the log with the given ID as a dictionary. The log and the
log number (the identity of this log in the database) are
both returned.
:param database: Database connection
:type database: tinydb.database.TinyDB
:param log_id: log ID
:type log_id: str or unicode
:return: log number and log, or None if no log exists with
the given ID.
:rtype: (int, dict)
:raises DatabaseError if there are problems in connecting to the
database
"""
try:
results = database.search(where('unique_id').matches(log_id))
except Exception as exception:
raise DatabaseError("Query error", exception)
if len(results) > 0:
return (results[0].eid, dict(results[0]))
else:
return None
示例2: get_filediffs
# 需要导入模块: import tinydb [as 别名]
# 或者: from tinydb import where [as 别名]
def get_filediffs(database, log_number):
"""
Get the 'filediffs' entry for the log with the given log number.
The log_number is returned via get_log.
:param database: Database connection
:type database: tinydb.database.TinyDB
:param log_number: log number
:type log_id: str or unicode
:return: 'filediffs' entry or None if none
:rtype: dict
:raises DatabaseError if there are problems in connecting to the
database
"""
try:
diffs = database.table("filediffs")
results = diffs.search(where("run_id") == log_number)
except Exception as exception:
raise DatabaseError("Query error", exception)
if len(results) > 0:
return dict(results[0])
else:
return None
示例3: get_ports
# 需要导入模块: import tinydb [as 别名]
# 或者: from tinydb import where [as 别名]
def get_ports(port, like=False):
"""
This function creates the SQL query depending on the specified port and
the --like option.
:param port: the specified port
:param like: the --like option
:return: all ports matching the given ``port``
:rtype: list
"""
where_field = "port" if port.isdigit() else "name"
db = get_database()
if like:
ports = db.search(where(where_field).search(port))
else:
ports = db.search(where(where_field) == port)
ports = merge_protocols(ports)
return [Port(**port) for port in ports] # flake8: noqa (F812)
示例4: delete_many
# 需要导入模块: import tinydb [as 别名]
# 或者: from tinydb import where [as 别名]
def delete_many(self, query):
"""
Removes all items matching the mongo query
:param query: dictionary representing the mongo query
:return: DeleteResult
"""
items = self.find(query)
result = [
self.table.remove(where(u'_id') == item[u'_id'])
for item in items
]
if query == {}:
# need to reset TinyDB's index for docs order consistency
self.table._last_id = 0
return DeleteResult(raw_result=result)
示例5: get_record_from_db
# 需要导入模块: import tinydb [as 别名]
# 或者: from tinydb import where [as 别名]
def get_record_from_db(id, dbfile):
db = TinyDB(dbfile, storage=serializer)
res = db.search(where('unique_id') == id)
return res
示例6: test_serializer
# 需要导入模块: import tinydb [as 别名]
# 或者: from tinydb import where [as 别名]
def test_serializer(tmpdir):
path = str(tmpdir.join('db.json'))
serializer = SerializationMiddleware(JSONStorage)
serializer.register_serializer(DateTimeSerializer(), 'TinyDate')
db = TinyDB(path, storage=serializer)
date = datetime(2000, 1, 1, 12, 0, 0)
db.insert({'date': date})
db.insert({'int': 2})
assert db.count(where('date') == date) == 1
assert db.count(where('int') == 2) == 1
示例7: test_serializer_recursive
# 需要导入模块: import tinydb [as 别名]
# 或者: from tinydb import where [as 别名]
def test_serializer_recursive(tmpdir):
path = str(tmpdir.join('db.json'))
serializer = SerializationMiddleware(JSONStorage)
serializer.register_serializer(DateTimeSerializer(), 'TinyDate')
db = TinyDB(path, storage=serializer)
date = datetime(2000, 1, 1, 12, 0, 0)
datenow = datetime.utcnow()
dates = [{'date': date, 'hp': 100}, {'date': datenow, 'hp': 1}]
data = {'dates': dates, 'int': 10}
db.insert(data)
db.insert({'int': 2})
assert db.count(where('dates').any(where('date') == date)) == 1
assert db.count(where('int') == 2) == 1
示例8: set_credentials
# 需要导入模块: import tinydb [as 别名]
# 或者: from tinydb import where [as 别名]
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})
示例9: delete_result
# 需要导入模块: import tinydb [as 别名]
# 或者: from tinydb import where [as 别名]
def delete_result(self, result):
"""
Remove the specified result from the database, based on its id.
"""
self.db.table('results').remove(where('meta')['id'] ==
result['meta']['id'])
#############
# Utilities #
#############
示例10: delete_one
# 需要导入模块: import tinydb [as 别名]
# 或者: from tinydb import where [as 别名]
def delete_one(self, query):
"""
Deletes one document from the collection
:param query: dictionary representing the mongo query
:return: DeleteResult
"""
item = self.find_one(query)
result = self.table.remove(where(u'_id') == item[u'_id'])
return DeleteResult(raw_result=result)
示例11: test_database_credential_with_fullname_does_a_db_where_with_split_fullname
# 需要导入模块: import tinydb [as 别名]
# 或者: from tinydb import where [as 别名]
def test_database_credential_with_fullname_does_a_db_where_with_split_fullname(mocker):
config = {
'path': 'path',
'extension': '.pass',
}
db = Database(config)
mocker.patch('passpie.database.split_fullname', return_value=('login', 'name'))
mock_get = mocker.patch.object(db, 'get', return_value=[{}])
result = db.credential('login@name')
assert db.get.called
assert result == [{}]
db.get.assert_called_once_with((where('login') == 'login') & (where('name') == 'name'))
示例12: test_database_remove_uses_table_remove_credential_from_database
# 需要导入模块: import tinydb [as 别名]
# 或者: from tinydb import where [as 别名]
def test_database_remove_uses_table_remove_credential_from_database(mocker):
config = {
'path': 'path',
'extension': '.pass',
}
db = Database(config)
mocker.patch.object(db, 'table', mocker.MagicMock())
mocker.patch('passpie.database.make_fullname', return_value='login@name')
db.remove(fullname='login@name')
assert db.table().remove.called
db.table().remove.assert_called_once_with((where('fullname') == 'login@name'))
示例13: remove
# 需要导入模块: import tinydb [as 别名]
# 或者: from tinydb import where [as 别名]
def remove(self, fullname):
self.table().remove(where('fullname') == fullname)
示例14: __init__
# 需要导入模块: import tinydb [as 别名]
# 或者: from tinydb import where [as 别名]
def __init__(self):
self.base_url = 'https://query.yahooapis.com/v1/public/yql'
self.param_string = ('?q={}&format=json&env='
'store://datatables.org/alltableswithkeys')
self.query = ('select Symbol,Ask,Bid '
'from yahoo.finance.quotes where symbol in ("{}")')
示例15: test_update
# 需要导入模块: import tinydb [as 别名]
# 或者: from tinydb import where [as 别名]
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