Adding to a module's __dict__?

Dave Angel davea at
Tue Mar 2 22:23:34 CET 2010

Terry Reedy wrote:
> On 3/2/2010 11:18 AM, John Posner wrote:
>> On 3/2/2010 10:19 AM, Roy Smith wrote:
>>> Somewhat sadly, in my case, I can't even machine process the header
>>> file. I don't, strictly speaking, have a header file. What I have is
>>> a PDF which documents what's in the header file, and I'm manually re-
>>> typing the data out of that. Sigh.
> There are Python modules to read/write pdf.
>> Here's an idea, perhaps too obvious, to minimize your keystrokes:
>> 1. Create a text file with the essential data:
>> XYZ_FOO 0 The foo property
>> XYZ_BAR 1 The bar property
>> XYZ_BAZ 2 reserved for future use
>> 2. Use a Python script to convert this into the desired code:
>> declare('XYZ_FOO', 0, "The foo property")
>> declare('XYZ_BAR', 1, "The bar property")
>> declare('XYZ_BAZ', 2, "reserved for future use")
>> Note:
>>  >>> s
>> 'XYZ_FOO 0 The foo property'
>>  >>> s.split(None, 2)
>> ['XYZ_FOO', '0', 'The foo property']
> Given that set of triples is constant, I would think about having the 
> Python script do the computation just once, instead of with every 
> inport. In other words, the script should *call* the declare function 
> and then write out the resulting set of dicts either to a .py or 
> pickle file.
> tjr
There have been lots of good suggestions in this thread.  Let me give 
you my take:

1) you shouldn't want to clutter up the global dictionary of your main 
processing module.  There's too much risk of getting a collision, either 
with the functions you write, or with some builtin.  That's especially 
true if you might later want to use a later version of that pdf file.  
Easiest solution for your purposes, make it a separate module.  Give it 
a name like defines, and in your main module, you use

import defines
print  defines.XYZ_FOO

And if that's too much typing, you can do:
import defines as I
print  I.XYZ_FOO

Next problem is to parse that pdf file.  One solution is to use a pdf 
library.  But another is to copy/paste it into a text file, and parse 
that.   Assuming it'll paste, and that the lines you want are 
recognizable (eg. they all begin as  #define), the parsing should be 
pretty easy.  The results of the parsing is a file

Now, if the pdf ever changes, rerun your parsing program.  But don't run 
it every time your application runs.

If the pdf file were changing often, then I'd have a different answer:
2) define an empty class, just as a placeholder, and make one instance I
      Populate a class instance I with    setattrib() calls, but access 
it with direct syntax, same as our first example.


More information about the Python-list mailing list