[Tutor] functions first?

Steven D'Aprano steve at pearwood.info
Wed Jan 28 01:10:10 CET 2015


On Tue, Jan 27, 2015 at 08:39:17AM -0800, Alex Kleider wrote:

> I use the docopt module to collect command line options and then 
> configparser to read a file.
> Some of the values (such as a port number, for example) must then be 
> adjusted.  An example is
> a port number which I want to convert from "5022" to ":5022" if it's a 
> non standard port or
> to "" if it is the standard "22" so it can then be used as a string 
> format parameter.
> Perhaps I should be doing this in the same place as I set up the string 
> rather than where
> I populate the 'config' and 'PARAMS' dictionaries?


I see the basic problem as this:

- you have config settings (command line options, config files) 
  which by their nature are always strings
- furthermore they are strings in a format designed for human use
  rather than machine use
- by the time your function uses them, they need to be converted
  from human-format to machine-format.

That last step might involve a type conversion like int(s), trimming 
whitespace, or more complex operations.

The most obvious way to do them is to apply the operation after reading 
the config value but before storing it in the params dict. If that 
works, I wouldn't add complexity where it isn't needed.

If it doesn't work for you, I think you need to explain what the problem 
is before we can suggest a better design.


-- 
Steve


More information about the Tutor mailing list