0

This is my scenario. I have a package that consists of several modules. They all import from settings.py. Some of the variable, though, depend on user input.

...
# some CONSTANTS
...
PROJECT_DIR = Path(os.path.abspath(__file__)).parent.ancestor(1)
SCRIPT_DIR = PROJECT_DIR.child('scripts')
data_input = DATA_ROOT.child('input')
input_root = data_input.child(options.root_input) # the options object holds some user input

# then use input_root to get an instance of class Countries
from countries import Countries
country_object = Countries(input_root)

there are several modules that need the country_object. So importing them from settings would be the cleanest solutions.

So I was reading up on dependency injection and I think this is what comes in handy here. But I find it difficult to wrap my around it so how would one use dependency injection to inject the options object into a module?

1 Answer 1

1

When it comes to patterns there are two philosophies, make your problem fit the pattern and make the pattern fit your problem. I follow the latter. So this would be my adaption of the dependency injection pattern to your problem:

class UserCountry(object):
     def __init__(self):be populated by user data
         self.Country = None

     def set_input_root(self, input_root):
         self.input_root = input_root # <-- this is a list/dict etc that I assume will 

     def __call__(self):
         if self.Country:
             return self.Country
         else:
             # Select country
             self.Country = Country
             return self.Country

in settings.py:

 user_country = UserCountries()

when input_root is defined:

settings.user_country.set_input_root(input_root) 

in other modules:

 settings.user_country() # gives you the Country object
Sign up to request clarification or add additional context in comments.

5 Comments

But settings.py does not know anything about input_root. So I can't call UserCountries() from within settings. I could call it from my main program but the problem remains as other modules also don't know about root_input.
so when is input_root set?
@LarsVegas I updated my answer, but you do realize that you won't know input_root, before you know input root...
it depends on user input, so when the main program is invoked. But the main program already imports from settings. Other modules than the main program will import settings as well. A module might use another module of the package as well outside of the main program. So input_root and country-instance must be valid and accessible globally during execution of the main program.
Have you seen my updated version? As long as set_input_root is set in the main program before the actual country is needed anywhere you should be fine. You may want to add a default and also save it to the database, if sessions are persistent.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.