Mercurial > public > mercurial-scm > hg-stable
diff mercurial/util.py @ 35940:72de5c504833
py3: factor out helpers to apply string conversion recursively
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Sat, 27 Jan 2018 13:33:31 +0900 |
parents | 2384523cee4d |
children | 15c8c4ac5d9c |
line wrap: on
line diff
--- a/mercurial/util.py Sat Jan 27 13:14:06 2018 +0900 +++ b/mercurial/util.py Sat Jan 27 13:33:31 2018 +0900 @@ -183,6 +183,39 @@ def safehasattr(thing, attr): return getattr(thing, attr, _notset) is not _notset +def _rapply(f, xs): + if xs is None: + # assume None means non-value of optional data + return xs + if isinstance(xs, (list, set, tuple)): + return type(xs)(_rapply(f, x) for x in xs) + if isinstance(xs, dict): + return type(xs)((_rapply(f, k), _rapply(f, v)) for k, v in xs.items()) + return f(xs) + +def rapply(f, xs): + """Apply function recursively to every item preserving the data structure + + >>> def f(x): + ... return 'f(%s)' % x + >>> rapply(f, None) is None + True + >>> rapply(f, 'a') + 'f(a)' + >>> rapply(f, {'a'}) == {'f(a)'} + True + >>> rapply(f, ['a', 'b', None, {'c': 'd'}, []]) + ['f(a)', 'f(b)', None, {'f(c)': 'f(d)'}, []] + + >>> xs = [object()] + >>> rapply(pycompat.identity, xs) is xs + True + """ + if f is pycompat.identity: + # fast path mainly for py2 + return xs + return _rapply(f, xs) + def bytesinput(fin, fout, *args, **kwargs): sin, sout = sys.stdin, sys.stdout try: