Where to set default data - where received, or where used

Chris Angelico rosuav at gmail.com
Tue Jun 12 18:14:50 EDT 2012


On Tue, Jun 12, 2012 at 4:37 AM, Dennis Carachiola <dncarac at gmail.com> wrote:
> Here's my question.  I could do this by creating the dictionary with
> the default values, then read the file into it.  Or I could use a
> 'get' with default values at the location in the program where those
> values are used.

Both options have their recommendations. As others have said, setting
your defaults in one place has advantages of coherence; but setting
them at point of read keeps all the code using it together. You can
have an entirely dumb I/O submodule that feeds smart other-modules.
Take your pick based on what you're doing.

What I would definitely suggest, though, is making a structured config
file. (You could "cheat" by importing it as a module and making it
simply Python code.) Provide a template config file with lots of
explanatory comments, and (crucially) every config entry given,
commented out, and set to its default.

# Configures the default and maximum flurble percentages
# Flurblization increases from the default until either the maximum is
# reached, or max_sort_memory is exceeded, whichever comes first.
#flurble_default = 10
#flurble_max = 30

Having all your defaults in one place makes it easier to produce this
sort of file; in fact, you could have your documentation there as
well. It does become a little more maintenance work, though. It's
possible to have a hybrid, where you run a preprocessor over your code
that analyzes it, collects all config entries, and builds your config
file parser... but that may be beyond the scope of your project.

Anything you can imagine can be done in code. It's just a question of
how much work. :)

Chris Angelico



More information about the Python-list mailing list