I need to run some Python 2.7 code under Python 2.6 and I was wondering how that could be automated.
Some specific simple changes are
sed -i -e 's/:,d/:d/g' -e 's/{0}/set([0])/g' foo.py
However, I also need to replace
with open(foo) as f, open(bar) as b:
...
with
with open(foo) as f:
with open(bar) as b:
...
and it is much less obvious for me (I need to get the indentation right and my sed-foo is not enough here).
Any suggestions?
No, don't use
sed. What you need is an IDE that understands refactoring or macros. For example, here's what I would do invim:Search for the
with A as a, with B as B:patternStart recording a macro
qaFind the comma
f,Replace it with a colon
r:Delete the space then enter a newline
lx<enter>Tab or use spaces to indent
Press
qto stop recordingThen you iterate through the searches and press
@ato replay the macro.