Best way to have intermediate object description format

Larry Bates larry.bates at websafe.com
Fri Nov 3 17:11:02 CET 2006


jm.suresh at no.spam.gmail.com wrote:
> Hi,
>  Is there any standard text format for storing data of object oriented
> nature.
>  The text file should be readable.
> 
> That is, Is there any better way than having to write out a file like
> this from the original place and read it in python and process it.
> 
> #----------------------------
> world = World(name='MyWorld')
> world.objects.append(Box(color='red'))
> world.objects.append(Circle(color='green'))
> world.someProp = "123"
> #-----------------------------
> 
> Thanks.
> Suresh
> 
While it is a little hard to determine your exact use case, I'll
try.  I have an application that dynamically creates objects from
.INI configuration file.  I use ConfigParser to process it.  The
entries are something like the following:

[world_MyWorld]
object_001=Box(color='red')
object_002=Circle(color='green')
property_someProp=123
property_someOtherProp=XYZ

I then read using ConfigParser and use list comprehensions to isolate
what I'm looking for in the file. Code not tested, but should serve as
and example and I think you will get the idea.  Note: Please don't get
too caught up in "premature optimization".  I use this to process .INI
files with thousands of lines and it goes through the process in fractions
of a second.

-Larry


Sample Code (written completely from my memory):

INI=ConfigParser.ConfigParser()
INI.read(inifilepath)
#
# Get a list of the world sections in the .INI file
#
world_sections=[section for section in INI.sections()
                if section.beginswith('world')
.
. Get lists of any other objects/sections here
.
#
# Create a list to store your instances of world_objects
#
world_instances=[]
#
# Loop over all the sections in the .INI file that create world objects
#
for section in world_sections:
    #
    # Isolate the name of this world object
    #
    name=section.split('_')[1]
    obj=World(name=name)
    #
    # Create a list to store world instance objects here
    #
    world_objects=[object for object in INI.options(section)
                   if object.startswith('object')]
    #
    # Loop over all the instance objects defined and append them here
    #
    for object in world_objects:
        #
        # Use eval to create object then append it.  Note: eval is
        # dangerous if you don't control the .INI file.  Untrusted .INI
        # file could have eval doing BAD things.
        #
        obj.append(eval(object))
    #
    # Create a list to store world instance properties here
    #
    world_properties=[object for object in INI.options(section)
                   if object.startswith('property')]
    #
    # Loop over all the instance objects defined and append them here
    #
    for property in world_properties:
        value=INI.get(section, property)
        property_name=property.split('_')[1]
        #
        # Use setattr to set the property
        #
        setattr(obj, property_name, value)

    #
    # Append this world object onto the list
    #
    world_instances.append(obj)



More information about the Python-list mailing list