# from more_itertools 10.2 def always_iterable(obj, base_type=(str, bytes)): """If *obj* is iterable, return an iterator over its items:: >>> obj = (1, 2, 3) >>> list(always_iterable(obj)) [1, 2, 3] If *obj* is not iterable, return a one-item iterable containing *obj*:: >>> obj = 1 >>> list(always_iterable(obj)) [1] If *obj* is ``None``, return an empty iterable: >>> obj = None >>> list(always_iterable(None)) [] By default, binary and text strings are not considered iterable:: >>> obj = 'foo' >>> list(always_iterable(obj)) ['foo'] If *base_type* is set, objects for which ``isinstance(obj, base_type)`` returns ``True`` won't be considered iterable. >>> obj = {'a': 1} >>> list(always_iterable(obj)) # Iterate over the dict's keys ['a'] >>> list(always_iterable(obj, base_type=dict)) # Treat dicts as a unit [{'a': 1}] Set *base_type* to ``None`` to avoid any special handling and treat objects Python considers iterable as iterable: >>> obj = 'foo' >>> list(always_iterable(obj, base_type=None)) ['f', 'o', 'o'] """ if obj is None: return iter(()) if (base_type is not None) and isinstance(obj, base_type): return iter((obj,)) try: return iter(obj) except TypeError: return iter((obj,))
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
__pycache__ | Folder | 0755 |
|
|
_vendor | Folder | 0755 |
|
|
command | Folder | 0755 |
|
|
compat | Folder | 0755 |
|
|
__init__.py | File | 359 B | 0644 |
|
_collections.py | File | 5.31 KB | 0644 |
|
_functools.py | File | 1.73 KB | 0644 |
|
_itertools.py | File | 1.42 KB | 0644 |
|
_log.py | File | 42 B | 0644 |
|
_macos_compat.py | File | 239 B | 0644 |
|
_modified.py | File | 2.35 KB | 0644 |
|
_msvccompiler.py | File | 19.18 KB | 0644 |
|
archive_util.py | File | 8.35 KB | 0644 |
|
bcppcompiler.py | File | 14.33 KB | 0644 |
|
ccompiler.py | File | 47.79 KB | 0644 |
|
cmd.py | File | 17.46 KB | 0644 |
|
config.py | File | 5.1 KB | 0644 |
|
core.py | File | 9.1 KB | 0644 |
|
cygwinccompiler.py | File | 11.67 KB | 0644 |
|
debug.py | File | 139 B | 0644 |
|
dep_util.py | File | 349 B | 0644 |
|
dir_util.py | File | 7.82 KB | 0644 |
|
dist.py | File | 49.78 KB | 0644 |
|
errors.py | File | 3.5 KB | 0644 |
|
extension.py | File | 9.97 KB | 0644 |
|
fancy_getopt.py | File | 17.4 KB | 0644 |
|
file_util.py | File | 7.76 KB | 0644 |
|
filelist.py | File | 13.33 KB | 0644 |
|
log.py | File | 1.17 KB | 0644 |
|
msvc9compiler.py | File | 29.42 KB | 0644 |
|
msvccompiler.py | File | 22.9 KB | 0644 |
|
spawn.py | File | 3.54 KB | 0644 |
|
sysconfig.py | File | 18.25 KB | 0644 |
|
text_file.py | File | 11.81 KB | 0644 |
|
unixccompiler.py | File | 15.33 KB | 0644 |
|
util.py | File | 17.67 KB | 0644 |
|
version.py | File | 12.34 KB | 0644 |
|
versionpredicate.py | File | 5.08 KB | 0644 |
|
zosccompiler.py | File | 6.43 KB | 0644 |
|