Basically, I have a lot of Python classes (representing our database schema) that look something like this:
from foo import xyz, b, c
class bar(object):
x = xyz()
y = b()
z = c()
...and I want to change it to this:
from foo import b, c
from baz import foobar
class bar(object):
x = foobar()
y = b()
z = c()
Essentially, I just want to replace all instances of xyz with foobar. It's acceptable to me to leave the import of a in, so this would also be fine:
from foo import a, b, c
from baz import foobar
class bar(object):
x = foobar()
y = b()
z = c()
It seems trivial to do a sed s/xyz/foobar/ on this, but then I'd still have to go back and change the import statements. I'm fine with doing some manual work, but I'd like to learn new ways to minimize the amount of it.
So how would you do this change? Is there anything I can do with sed to do this? Or rope (I don't see anything obvious that would help me here)?
sed s/a/m would be disasterous since bar would be changed to bmr.
If the variable names are truly short and/or non-unique, non-regex-able, then perhaps the easiest thing to do would be to insert
Then you don't have to change any other code further down in the file.
You could use sed to change
to
though
would work too, since the last import wins.