Best way to modify code without breaking stuff.

Carl Banks pavlovevidence at gmail.com
Wed Jun 4 12:57:50 CEST 2008


On Jun 4, 3:25 am, Jesse Aldridge <JesseAldri... at gmail.com> wrote:
> I've got a module that I use regularly.  I want to make some extensive
> changes to this module but I want all of the programs that depend on
> the module to keep working while I'm making my changes.  What's the
> best way to accomplish this?

If I'm understanding you correctly: you want to load the old module
when running code normally, but want to use a new module when
developing, but is has to have the same name?

Here's what you could do:

1. Rename "whatever.py" to "oldwhatever.py".

2. Copy "oldwhatever.py" to "newwhatever.py", and make your extensive
changes there.

3. Create a new "whatever.py" with code that imports all the symbols
from the old or new module depending on which module you want to use.
For instance, you could use an environment variable to choose which
one:

if os.environ.get("USENEWMODULE") == "yes":
    from newwhatever import *
else:
    from oldwhatever import *

Or, you could set a flag in some sort of configuration module and
check that:

import config
if config.use_new_module:
    from newwhatever import *
else
    from oldwhatever import *


Carl Banks



More information about the Python-list mailing list