Skip to content

Commit

Permalink
Add a new method, fix bytes_iter
Browse files Browse the repository at this point in the history
  • Loading branch information
pudo committed Feb 8, 2021
1 parent d275501 commit 50c8208
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
2 changes: 2 additions & 0 deletions banal/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ def bytes_iter(obj) -> Iterable[bytes]:
elif isinstance(obj, (date, datetime)):
yield _bytes_str(obj.isoformat())
elif is_mapping(obj):
if None in obj:
yield from bytes_iter(obj.pop(None))
for key in sorted(obj.keys()):
for out in chain(bytes_iter(key), bytes_iter(obj[key])):
yield out
Expand Down
34 changes: 32 additions & 2 deletions banal/lists.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from typing import List, Any
from collections.abc import Sequence
from typing import List, Set, Any, TypeVar, Generator
from collections.abc import Sequence, Iterable

T = TypeVar("T")


def is_sequence(obj: Any) -> bool:
Expand Down Expand Up @@ -31,6 +33,34 @@ def ensure_list(obj: Any) -> List:
return [o for o in obj]


def chunked_iter(
iterable: Iterable[T], batch_size: int = 500
) -> Generator[List[T], None, None]:
"""Pick `batch_size` items from an iterable and treat them as a batch list."""
batch = list()
for item in iterable:
batch.append(item)
if len(batch) >= batch_size:
yield batch
batch = list()
if len(batch) > 0:
yield batch


def chunked_iter_sets(
iterable: Iterable[T], batch_size: int = 500
) -> Generator[Set[T], None, None]:
"""Pick `batch_size` items from an iterable and treat them as a batch set."""
batch = set()
for item in iterable:
batch.add(item)
if len(batch) >= batch_size:
yield batch
batch = set()
if len(batch) > 0:
yield batch


def first(lst: Sequence) -> Any:
"""Return the first non-null element in the list, or None."""
for item in ensure_list(lst):
Expand Down

0 comments on commit 50c8208

Please sign in to comment.