import collections.abc import functools # from jaraco.functools 3.5 def pass_none(func): """ Wrap func so it's not called if its first param is None >>> print_text = pass_none(print) >>> print_text('text') text >>> print_text(None) """ @functools.wraps(func) def wrapper(param, *args, **kwargs): if param is not None: return func(param, *args, **kwargs) return wrapper # from jaraco.functools 4.0 @functools.singledispatch def _splat_inner(args, func): """Splat args to func.""" return func(*args) @_splat_inner.register def _(args: collections.abc.Mapping, func): """Splat kargs to func as kwargs.""" return func(**args) def splat(func): """ Wrap func to expect its parameters to be passed positionally in a tuple. Has a similar effect to that of ``itertools.starmap`` over simple ``map``. >>> import itertools, operator >>> pairs = [(-1, 1), (0, 2)] >>> _ = tuple(itertools.starmap(print, pairs)) -1 1 0 2 >>> _ = tuple(map(splat(print), pairs)) -1 1 0 2 The approach generalizes to other iterators that don't have a "star" equivalent, such as a "starfilter". >>> list(filter(splat(operator.add), pairs)) [(0, 2)] Splat also accepts a mapping argument. >>> def is_nice(msg, code): ... return "smile" in msg or code == 0 >>> msgs = [ ... dict(msg='smile!', code=20), ... dict(msg='error :(', code=1), ... dict(msg='unknown', code=0), ... ] >>> for msg in filter(splat(is_nice), msgs): ... print(msg) {'msg': 'smile!', 'code': 20} {'msg': 'unknown', 'code': 0} """ return functools.wraps(func)(functools.partial(_splat_inner, func=func))
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
__pycache__ | Folder | 0755 |
|
|
command | Folder | 0755 |
|
|
__init__.py | File | 359 B | 0644 |
|
_collections.py | File | 5.18 KB | 0644 |
|
_functools.py | File | 1.73 KB | 0644 |
|
_log.py | File | 43 B | 0644 |
|
_macos_compat.py | File | 239 B | 0644 |
|
_modified.py | File | 2.35 KB | 0644 |
|
_msvccompiler.py | File | 19.16 KB | 0644 |
|
archive_util.py | File | 8.37 KB | 0644 |
|
bcppcompiler.py | File | 14.38 KB | 0644 |
|
ccompiler.py | File | 47.5 KB | 0644 |
|
cmd.py | File | 17.44 KB | 0644 |
|
config.py | File | 4.8 KB | 0644 |
|
core.py | File | 9.18 KB | 0644 |
|
cygwinccompiler.py | File | 11.64 KB | 0644 |
|
debug.py | File | 139 B | 0644 |
|
dep_util.py | File | 349 B | 0644 |
|
dir_util.py | File | 7.88 KB | 0644 |
|
dist.py | File | 49 KB | 0644 |
|
errors.py | File | 3.5 KB | 0644 |
|
extension.py | File | 10.03 KB | 0644 |
|
fancy_getopt.py | File | 17.48 KB | 0644 |
|
file_util.py | File | 8.02 KB | 0644 |
|
filelist.py | File | 13.39 KB | 0644 |
|
log.py | File | 1.17 KB | 0644 |
|
msvc9compiler.py | File | 29.48 KB | 0644 |
|
msvccompiler.py | File | 23.02 KB | 0644 |
|
py38compat.py | File | 217 B | 0644 |
|
py39compat.py | File | 1.92 KB | 0644 |
|
spawn.py | File | 3.41 KB | 0644 |
|
sysconfig.py | File | 18.48 KB | 0644 |
|
text_file.py | File | 11.8 KB | 0644 |
|
unixccompiler.py | File | 15.24 KB | 0644 |
|
util.py | File | 17.68 KB | 0644 |
|
version.py | File | 12.65 KB | 0644 |
|
versionpredicate.py | File | 5.08 KB | 0644 |
|