Most Pythonic way to store (small) configuration

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu Aug 6 03:33:44 EDT 2015


On Thursday 06 August 2015 10:07, Chris Angelico wrote:

> On Thu, Aug 6, 2015 at 9:43 AM, Tim Chase <python.list at tim.thechases.com>
> wrote:
>> Significant whitespace?  Not usually simple (just stuck touching a
>> project where someone committed with tons of trailing whitespaces.
>> grumble), so strip 'em off as if they're an error condition.  I've
>> never had a config-file where I wanted leading/trailing whitespace as
>> significant.
> 
> If you're configuring a prompt, sometimes you need to be able to
> include a space at the end of it.

"Sometimes"? What sort of filthy perv doesn't separate their prompts from 
the user input with at least one space? Disgusting, I call it.

    this is a prompt and this isn't

versus

    this is a promptand this isn't

Come the revolution, anyone who writes the second will be taken out and 
shot.

The right solution to that is to have the display function add a space to 
the end of the prompt if there isn't already one, that way you don't need to 
care about putting a space at the end of the prompt yourself.


> Since trailing whitespace on a line
> in the file itself is a bad idea, you need some way of marking it.

I'm partial to one of two schemes:

- strings need to be quoted, so leading and trailing spaces are easy: 

  key = " this has both leading and trailing spaces "

- strings don't need to be quoted, and spaces are significant *except* 
  at  the ends of the string:

  key = this has no leading or trailing spaces

  If you need spaces at the end, use an escape sequence:

  key = \sthis has both leading and trailing spaces\s

say, or ^SP or \N{SPACE} or whatever floats your boat. Unless your 
requirements are limited to only printable ASCII strings, you're going to 
need to provide some sort of escape sequence anyway, to allow (say) 
newlines.


-- 
Steve




More information about the Python-list mailing list