本文整理汇总了Python中tinydb.queries.where函数的典型用法代码示例。如果您正苦于以下问题:Python where函数的具体用法?Python where怎么用?Python where使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了where函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: add
def add(fullname, password, comment, force, copy):
db = Database(config.path)
try:
login, name = split_fullname(fullname)
except ValueError:
message = 'invalid fullname syntax'
raise click.ClickException(click.style(message, fg='yellow'))
found = db.get((where("login") == login) & (where("name") == name))
if force or not found:
with Cryptor(config.path) as cryptor:
encrypted = cryptor.encrypt(password)
credential = dict(fullname=fullname,
name=name,
login=login,
password=encrypted,
comment=comment,
modified=datetime.now())
db.insert(credential)
if copy:
pyperclip.copy(password)
else:
message = "Credential {} already exists. --force to overwrite".format(
fullname)
raise click.ClickException(click.style(message, fg='yellow'))
开发者ID:drewdown99,项目名称:passpie,代码行数:26,代码来源:cli.py
示例2: search
def search(regex):
db = Database(config.path)
credentials = db.search(
where("name").matches(regex) |
where("login").matches(regex) |
where("comment").matches(regex))
credentials = sorted(credentials, key=lambda x: x["name"]+x["login"])
print_table(credentials)
开发者ID:drewdown99,项目名称:passpie,代码行数:8,代码来源:cli.py
示例3: test_all
def test_all():
query = where('followers').all(where('name') == 'don')
assert query({'followers': [{'name': 'don'}]})
assert not query({'followers': [{'name': 'don'}, {'name': 'john'}]})
query = where('followers').all(where('num').matches('\\d+'))
assert query({'followers': [{'num': '123'}, {'num': '456'}]})
assert not query({'followers': [{'num': '123'}, {'num': 'abc'}]})
开发者ID:AresTao,项目名称:tinydb,代码行数:8,代码来源:test_queries.py
示例4: test_hash
def test_hash():
d = {
where('key1') == 2: True,
where('key1').has('key2').has('key3'): True
}
assert (where('key1') == 2) in d
assert (where('key1').has('key2').has('key3')) in d
开发者ID:GeWu,项目名称:tinydb,代码行数:8,代码来源:test_queries.py
示例5: test_and
def test_and():
query = (
(where('val1') == 1) &
(where('val2') == 2)
)
assert_true(query({'val1': 1, 'val2': 2}))
assert_false(query({'val1': 1}))
assert_false(query({'val2': 2}))
assert_false(query({'val1': '', 'val2': ''}))
开发者ID:ktosiu,项目名称:tinydb-1,代码行数:9,代码来源:tests_queries.py
示例6: test_and
def test_and():
query = (
(where('val1') == 1) &
(where('val2') == 2)
)
assert query({'val1': 1, 'val2': 2})
assert not query({'val1': 1})
assert not query({'val2': 2})
assert not query({'val1': '', 'val2': ''})
开发者ID:GeWu,项目名称:tinydb,代码行数:9,代码来源:test_queries.py
示例7: search
def search(regex):
if config.search_automatic_regex and re.match("\w+", regex):
regex = ".*{}.*".format(regex)
db = Database(config.path)
credentials = db.search(
where("name").matches(regex) |
where("login").matches(regex) |
where("comment").matches(regex))
credentials = sorted(credentials, key=lambda x: x["name"] + x["login"])
print_table(credentials)
开发者ID:Atlantic777,项目名称:passpie,代码行数:10,代码来源:cli.py
示例8: test_has
def test_has():
query = where('key1').has('key2')
str(query) # This used to cause a bug...
assert query({'key1': {'key2': {'key3': 1}}})
assert query({'key1': {'key2': 1}})
assert not query({'key1': 3})
assert not query({'key1': {'key1': 1}})
assert not query({'key2': {'key1': 1}})
query = where('key1').has('key2') == 1
assert query({'key1': {'key2': 1}})
assert not query({'key1': {'key2': 2}})
# Nested has: key exists
query = where('key1').has('key2').has('key3')
assert query({'key1': {'key2': {'key3': 1}}})
# Not a dict
assert not query({'key1': 1})
assert not query({'key1': {'key2': 1}})
# Wrong key
assert not query({'key1': {'key2': {'key0': 1}}})
assert not query({'key1': {'key0': {'key3': 1}}})
assert not query({'key0': {'key2': {'key3': 1}}})
# Nested has: check for value
query = where('key1').has('key2').has('key3') == 1
assert query({'key1': {'key2': {'key3': 1}}})
assert not query({'key1': {'key2': {'key3': 0}}})
# Test special methods: regex matches
query = where('key1').has('value').matches(r'\d+')
assert query({'key1': {'value': '123'}})
assert not query({'key2': {'value': '123'}})
assert not query({'key2': {'value': 'abc'}})
# Test special methods: regex contains
query = where('key1').has('value').contains(r'\d+')
assert query({'key1': {'value': 'a2c'}})
assert not query({'key2': {'value': 'a2c'}})
assert not query({'key2': {'value': 'abc'}})
# Test special methods: nested has and regex matches
query = where('key1').has('x').has('y').matches(r'\d+')
assert query({'key1': {'x': {'y': '123'}}})
assert not query({'key1': {'x': {'y': 'abc'}}})
# Test special method: nested has and regex contains
query = where('key1').has('x').has('y').contains(r'\d+')
assert query({'key1': {'x': {'y': 'a2c'}}})
assert not query({'key1': {'x': {'y': 'abc'}}})
# Test special methods: custom test
query = where('key1').has('int').test(lambda x: x == 3)
assert query({'key1': {'int': 3}})
开发者ID:GeWu,项目名称:tinydb,代码行数:56,代码来源:test_queries.py
示例9: test_not
def test_not():
query = ~ (where('val1') == 1)
assert_true(query({'val1': 5, 'val2': 2}))
assert_false(query({'val1': 1, 'val2': 2}))
query = (
(~ (where('val1') == 1)) &
(where('val2') == 2)
)
assert_true(query({'val1': '', 'val2': 2}))
assert_true(query({'val2': 2}))
assert_false(query({'val1': 1, 'val2': 2}))
assert_false(query({'val1': 1}))
assert_false(query({'val1': '', 'val2': ''}))
开发者ID:ktosiu,项目名称:tinydb-1,代码行数:14,代码来源:tests_queries.py
示例10: test_not
def test_not():
query = ~ (where('val1') == 1)
assert query({'val1': 5, 'val2': 2})
assert not query({'val1': 1, 'val2': 2})
query = (
(~ (where('val1') == 1)) &
(where('val2') == 2)
)
assert query({'val1': '', 'val2': 2})
assert query({'val2': 2})
assert not query({'val1': 1, 'val2': 2})
assert not query({'val1': 1})
assert not query({'val1': '', 'val2': ''})
开发者ID:GeWu,项目名称:tinydb,代码行数:14,代码来源:test_queries.py
示例11: test_regex
def test_regex():
query = where('val').matches(r'\d{2}\.')
assert query({'val': '42.'})
assert not query({'val': '44'})
assert not query({'val': 'ab.'})
assert not query({'': None})
query = where('val').contains(r'\d+')
assert query({'val': 'ab3'})
assert not query({'val': 'abc'})
assert not query({'val': ''})
assert not query({'': None})
开发者ID:GeWu,项目名称:tinydb,代码行数:14,代码来源:test_queries.py
示例12: update
def update(fullname, name, login, password, comment):
db = Database(config.path)
credential = get_credential_or_abort(db, fullname)
values = credential.copy()
if any([name, login, password, comment]):
values["name"] = name if name else credential["name"]
values["login"] = login if login else credential["login"]
values["password"] = password if password else credential["password"]
values["comment"] = comment if comment else credential["comment"]
else:
values["name"] = click.prompt("Name", default=credential["name"])
values["login"] = click.prompt("Login", default=credential["login"])
values["password"] = click.prompt("Password",
hide_input=True,
default=credential["password"],
confirmation_prompt=True,
show_default=False,
prompt_suffix=" [*****]: ")
values["comment"] = click.prompt("Comment",
default=credential["comment"])
if values != credential:
values["fullname"] = make_fullname(values["login"], values["name"])
values["modified"] = datetime.now()
if values["password"] != credential["password"]:
with Cryptor(config.path) as cryptor:
values["password"] = cryptor.encrypt(password)
db = Database(config.path)
db.update(values, (where("fullname") == credential["fullname"]))
开发者ID:drewdown99,项目名称:passpie,代码行数:30,代码来源:cli.py
示例13: test_regex
def test_regex():
query = where('val').matches(r'\d{2}\.')
assert_true(query({'val': '42.'}))
assert_false(query({'val': '44'}))
assert_false(query({'val': 'ab.'}))
assert_false(query({'': None}))
开发者ID:ktosiu,项目名称:tinydb-1,代码行数:7,代码来源:tests_queries.py
示例14: get_credential_or_abort
def get_credential_or_abort(db, fullname):
try:
login, name = split_fullname(fullname)
query = (where("name") == name) & (where("login") == login)
except ValueError:
query = where('name') == fullname
credential = db.get(query)
if not credential:
message = "Credential '{}' not found".format(fullname)
raise click.ClickException(click.style(message, fg='red'))
elif db.count(query) > 1:
message = "Multiple matches for '{}'".format(fullname)
raise click.ClickException(click.style(message, fg='red'))
return credential
开发者ID:drewdown99,项目名称:passpie,代码行数:16,代码来源:cli.py
示例15: remove
def remove(fullname):
db = Database(config.path)
credential = get_credential_or_abort(db, fullname)
if credential:
click.confirm(
'Remove credential: {}'.format(click.style(fullname, 'yellow')),
abort=True
)
db.remove(where('fullname') == credential["fullname"])
开发者ID:breakingigloo,项目名称:passpie,代码行数:9,代码来源:cli.py
示例16: test_custom_with_params
def test_custom_with_params():
def test(value, minimum, maximum):
return minimum <= value <= maximum
query = where('val').test(test, 1, 10)
assert query({'val': 5})
assert not query({'val': 0})
assert not query({'val': 11})
assert not query({'': None})
开发者ID:hughes,项目名称:tinydb,代码行数:10,代码来源:test_queries.py
示例17: test_custom
def test_custom():
def test(value):
return value == 42
query = where('val').test(test)
assert query({'val': 42})
assert not query({'val': 40})
assert not query({'val': '44'})
assert not query({'': None})
开发者ID:GeWu,项目名称:tinydb,代码行数:10,代码来源:test_queries.py
示例18: remove
def remove(fullname, yes):
db = Database(config.path)
credentials = get_credential_or_abort(db, fullname, many=True)
if credentials:
if not yes:
creds = ', '.join([c['fullname'] for c in credentials])
click.confirm(
'Remove credentials: ({})'.format(
click.style(creds, 'yellow')),
abort=True
)
for credential in credentials:
db.remove(where('fullname') == credential['fullname'])
开发者ID:UtahDave,项目名称:passpie,代码行数:14,代码来源:cli.py
示例19: remove
def remove(fullname, yes):
db = Database(config.path)
credentials = get_credential_or_abort(db, fullname, many=True)
if credentials:
if not yes:
creds = ', '.join([c['fullname'] for c in credentials])
click.confirm(
'Remove credentials: ({})'.format(
click.style(creds, 'yellow')),
abort=True
)
for credential in credentials:
db.remove(where('fullname') == credential['fullname'])
fullnames = ', '.join(c['fullname'] for c in credentials)
message = 'Removed {}'.format(fullnames)
logger.debug(message)
repo = Repository(config.path)
repo.commit(message)
开发者ID:Atlantic777,项目名称:passpie,代码行数:20,代码来源:cli.py
示例20: test_any
def test_any():
query = where('followers').any(where('name') == 'don')
assert query({'followers': [{'name': 'don'}, {'name': 'john'}]})
assert not query({'followers': 1})
assert not query({})
query = where('followers').any(where('num').matches('\\d+'))
assert query({'followers': [{'num': '12'}, {'num': 'abc'}]})
assert not query({'followers': [{'num': 'abc'}]})
query = where('followers').any(['don', 'jon'])
assert query({'followers': ['don', 'greg', 'bill']})
assert not query({'followers': ['greg', 'bill']})
assert not query({})
query = where('followers').any([{'name': 'don'}, {'name': 'john'}])
assert query({'followers': [{'name': 'don'}, {'name': 'greg'}]})
assert not query({'followers': [{'name': 'greg'}]})
开发者ID:GeWu,项目名称:tinydb,代码行数:19,代码来源:test_queries.py
注:本文中的tinydb.queries.where函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论