Created
July 8, 2015 22:16
-
-
Save tmerr/6f3620c7ab88acd828c4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def subs2(expr, substitutions): | |
""" | |
extends subs to conveniently support arrays | |
subs2(['a', [3,4]]) becomes subs([('a_0', 3), ('a_1', 4)]) | |
""" | |
subslist = [] | |
for name, val in substitutions: | |
if hasattr(val, '__getitem__') and not isinstance(val, str): | |
for i, vali in enumerate(val): | |
indexedname = '{}_{}'.format(name, i) | |
subslist.append((indexedname, vali)) | |
else: | |
subslist.append((name, val)) | |
return expr.subs(subslist) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment