Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Support adding callables to path#445

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
NextNext commit
Support adding callables to path
  • Loading branch information
@rewritten
Saverio Trioni authored andrewritten committedDec 1, 2021
commit2d25eb96650d738dba0881f7840e5e147831c832
7 changes: 7 additions & 0 deletionstests/test_queries.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -35,6 +35,13 @@ def test_path_and():
assert hash(query) != hash(where('value'))


def test_callable_in_path_with_map():
double = lambda x: x + x
query = Query().value.map(double) == 10
assert query({'value': 5})
assert not query({'value': 10})


def test_eq():
query = Query().value == 1
assert query({'value': 1})
Expand Down
19 changes: 18 additions & 1 deletiontinydb/queries.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -218,7 +218,10 @@ def runner(value):
try:
# Resolve the path
for part in self._path:
value = value[part]
if isinstance(part, str):
value = value[part]
else:
value = part(value)
except (KeyError, TypeError):
return False
else:
Expand DownExpand Up@@ -487,6 +490,20 @@ def noop(self) -> QueryInstance:
()
)

def map(self, fn: Callable[[Any], Any]) -> 'Query':
"""
Add a function to the query path. Similar to __getattr__ but for
arbitrary functions.
"""
query = type(self)()

# Now we add the callable to the query path ...
query._path = self._path + (fn,)

# ... and update the query hash
query._hash = ('path', query._path)

return query

def where(key: str) -> Query:
"""
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp