Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- smoosh module allowing for smoosh levels specified by number of o's
- >>> import smoosh
- >>> list(smoosh.smoosh([1,[2,[3,[4]]]]))
- [1, 2, [3, [4]]]
- >>> list(smoosh.smooosh([1,[2,[3,[4]]]]))
- [1, 2, 3, [4]]
- >>> list(smoosh.smoooosh([1,[2,[3,[4]]]]))
- [1, 2, 3, 4]
- """
- from collections.abc import Iterable
- from functools import partial
- import re
- def _flatten(iterable, depth):
- for item in iterable:
- if not isinstance(item, Iterable) or depth == 0:
- yield item
- else:
- yield from _flatten(item, depth-1)
- def __getattr__(name):
- if re.search(r'^smoo+sh$', name):
- return partial(_flatten, depth=name.count('o')-1)
- raise AttributeError(f"module {__name__} has no attribute {name}")
- __all__ = [f'smoo{"o" * i}sh' for i in range(20)]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement