Skip to content

Commit

Permalink
#56 Add custom dictionary factory
Browse files Browse the repository at this point in the history
  • Loading branch information
astropenguin committed Aug 1, 2023
1 parent 34d1391 commit df8aac2
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion azely/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ def wrapper(*args: Any, **kwargs: Any) -> Any:
tab = doc.setdefault(table, {})

if update or (query not in tab):
tab[query] = asdict(func(*args, **kwargs))
obj = func(*args, **kwargs)
tab[query] = asdict(obj, dict_factory=dropna)

if tab is not doc.last_item():
tab.add(nl())
Expand All @@ -53,6 +54,11 @@ def wrapper(*args: Any, **kwargs: Any) -> Any:
return wrapper # type: ignore


def dropna(items: list[tuple[str, Any]]) -> dict[str, Any]:
"""Dictionary factory that drops N/A items."""
return dict((k, v) for k, v in items if v is not None)


def rename(func: TCallable, key: str) -> TCallable:
"""Update the name field of a dataclass object."""
signature = Signature.from_callable(func)
Expand Down

0 comments on commit df8aac2

Please sign in to comment.