8000 QueryList: Better object handling by tony · Pull Request #415 · vcs-python/libvcs · GitHub
[go: up one dir, main page]

Skip to content

QueryList: Better object handling #415

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 18, 2022
Merged
Changes from 1 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
Prev Previous commit
Next Next commit
docs(query_list): More detailed docs for keygetter
  • Loading branch information
tony committed Sep 18, 2022
commit 133d4eb6b6769f7978be72e6217d63b1e3a4989e
49 changes: 46 additions & 3 deletions src/libvcs/_internal/query_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,53 @@ def keygetter(
) -> Union[None, Any, str, list[str], Mapping[str, str]]:
"""Fetch values in objects and keys, deeply.

>>> keygetter({ "foods": { "breakfast": "cereal" } }, "foods__breakfast")
'cereal'
>>> keygetter({ "foods": { "breakfast": "cereal" } }, "foods")
**With dictionaries**:

>>> keygetter({ "menu": { "breakfast": "cereal" } }, "menu")
{'breakfast': 'cereal'}

>>> keygetter({ "menu": { "breakfast": "cereal" } }, "menu__breakfast")
'cereal'

**With objects**:

>>> from typing import Optional
>>> from dataclasses import dataclass, field

>>> @dataclass()
... class Menu:
... fruit: list[str] = field(default_factory=list)
... breakfast: Optional[str] = None


>>> @dataclass()
... class Restaurant:
... place: str
... city: str
... state: str
... menu: Menu = field(default_factory=Menu)


>>> restaurant = Restaurant(
... place="Largo",
... city="Tampa",
... state="Florida",
... menu=Menu(
... fruit=["banana", "orange"], breakfast="cereal"
... )
... )

>>> restaurant
Restaurant(place='Largo',
city='Tampa',
state= 577E 'Florida',
menu=Menu(fruit=['banana', 'orange'], breakfast='cereal'))

>>> keygetter(restaurant, "menu")
Menu(fruit=['banana', 'orange'], breakfast='cereal')

>>> keygetter(restaurant, "menu__breakfast")
'cereal'
"""
try:
sub_fields = path.split("__")
Expand Down
0