I have a module for configuration, projectConfig
, to parse a sample sheet for a project:
class SampleSheetFields():
FIELD_1 = "field1"
FIELD_2 = "field2"
class SampleSheetFieldsOld():
FIELD_1 = "field_1"
FIELD_2 = "field_2"
I had been using the first class in other modules like this:
from projectConfig import SampleSheetFields as ssFields
class SomeClass
def __init__(self):
...
check(someContent, ssFields.FIELD_1)
The thing is I had developed my software using the reference to ssFields
quite a lot. At some point new specifications said that the software should also use sample sheets with different field names. The quickest way I found to achieve that, without messing too much with the code, was to add the class SampleSheetFieldsOld
in the projectConfig
and to make a conditional import in my modules:
class SomeClass:
def __init__(self, useOld):
if useOld:
from projectConfig import SampleSheetFieldsOld as ssFields
else:
from projectConfig import SampleSheetFields as ssFields
...
check(someContent, ssFields.FIELD_1)
Note that the mandatory fields which are used have the same name so there is no conflicting or missing field. The program works as expected.
My questions are:
- How bad is this practice, if it is bad; and
- How can I circumvent it to make a better and more sustainable code?