本文整理汇总了Python中tinydb.queries.where函数的典型用法代码示例。如果您正苦于以下问题:Python where函数的具体用法?Python where怎么用?Python where使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了where函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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'))
示例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)
示例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'}]})
示例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
示例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': ''}))
示例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': ''})
示例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)
示例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}})
示例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': ''}))
示例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': ''})
示例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})
示例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"]))
示例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}))
示例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
示例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"])