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 in vim
:
Search for the with A as a, with B as B:
pattern
Start recording a macro qa
Find 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 q
to stop recording
Then you iterate through the searches and press @a
to replay the macro.