XML Considered Harmful

Michael F. Stemper michael.stemper at gmail.com
Tue Sep 28 13:53:49 EDT 2021


On 28/09/2021 10.53, Stefan Ram wrote:
> "Michael F. Stemper" <michael.stemper at gmail.com> writes:
>> Well, I could continue to hard-code the data into one of the test
>> programs
> 
>    One can employ a gradual path from a program with hardcoded
>    data to an entity sharable by different programs.
> 
>    When I am hurried to rush to a working program, I often
>    end up with code that contains configuration data spread
>    (interspersed) all over the code. For example:

>    1st step: give a name to all the config data:

>    2nd: move all config data to the top of the source code,
>    directly after all the import statements:

>    3rd: move all config data to a separate "config.py" module:
> 
> import ...
> import config
> ...
> 
> ...
> open( config.project_directory + "data.txt" )
> ...
> 
>> but that would mean that every time that I wanted to look
>> at a different scenario, I'd need to modify a program.
> 
>    Now you just have to modify "config.py" - clearly separated
>    from the (rest of the) "program".

Well, that doesn't really address what format to store the data
in. I was going to write a module that would read data from an
XML file:

import EDXML
gens = EDXML.GeneratorsFromXML( "gendata1.xml" )
fuels = EDXML.FuelsFromXML( "fueldata3.xml" )

(Of course, I'd really get the file names from command-line arguments.)

Then I read a web page that suggested use of XML was a poor idea,
so I posted here asking for a clarification and alternate suggestions.

One suggestion was that I use YAML, in which case, I'd write:

import EDfromYAML
gens = EDfromYAML( "gendata1.yaml" )
fuels = EDXML.FuelsFromYAML( "fueldata3.yaml" )

>> And when I discover anomalous behavior, I'd need to copy the
>> hard-coded data into another program.
> 
>    Now you just have to import "config.py" from the other program.

This sounds like a suggestion that I hard-code the data into a
module. I suppose that I could have half-a-dozen modules with
different data sets and ln them as required:

$ rm GenData.py* FuelData.py*
$ ln gendata1.py GenData.py
$ ln fueldata3.py FuelData.py

It seems to me that a more thorough separation of code and data
might be useful.

-- 
Michael F. Stemper
The name of the story is "A Sound of Thunder".
It was written by Ray Bradbury. You're welcome.


More information about the Python-list mailing list