469,635 Members | 1,952 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,635 developers. It's quick & easy.

Best way to have intermediate object description format

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

Nov 3 '06 #1
4 1106

jm*******@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
XML ?
YAML ?
JSON ?
ConfigParser module ?
CSV file ?
UML ?

I'm finding it hard to be more specific, given your original post. so
just squirted some data formats that are supported by multiple
languages and can be used to transfer data between them.

P.S. I don't know why I put XML first :-)
P.P.S. And UML seems to be about pretty diagrams rather than a textual
format, but no doubt, with the size of the companies behind it, there's
probably a textual format hidden in their too.
- Paddy.

Nov 3 '06 #2
Thanks paddy, Since the original language from which I am translating
does not support any of these formats, I will have to write one myself.
So, which one is easy to write out in a C like environment.

regards,
Suresh
Paddy wrote:
jm*******@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

XML ?
YAML ?
JSON ?
ConfigParser module ?
CSV file ?
UML ?

I'm finding it hard to be more specific, given your original post. so
just squirted some data formats that are supported by multiple
languages and can be used to transfer data between them.

P.S. I don't know why I put XML first :-)
P.P.S. And UML seems to be about pretty diagrams rather than a textual
format, but no doubt, with the size of the companies behind it, there's
probably a textual format hidden in their too.
- Paddy.
Nov 3 '06 #3
jm*******@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)
Nov 3 '06 #4

jm*******@no.spam.gmail.com wrote:
Thanks paddy, Since the original language from which I am translating
does not support any of these formats, I will have to write one myself.
So, which one is easy to write out in a C like environment.

regards,
Suresh
Paddy wrote:
jm*******@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
XML ?
YAML ?
JSON ?
ConfigParser module ?
CSV file ?
UML ?

I'm finding it hard to be more specific, given your original post. so
just squirted some data formats that are supported by multiple
languages and can be used to transfer data between them.

P.S. I don't know why I put XML first :-)
P.P.S. And UML seems to be about pretty diagrams rather than a textual
format, but no doubt, with the size of the companies behind it, there's
probably a textual format hidden in their too.
- Paddy.
Could you write out the data as a series of python dicts to file
ending in .py?,

If it is hard to write out the data in the language you are coming from
then it might be easier to stick to a simple format like CSV (comma
separated value/tab separated value), and then put more smarts in the
Python reader to reconstruct your objects.

- Paddy.

Nov 3 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by brianobush | last post: by
16 posts views Thread by D Witherspoon | last post: by
reply views Thread by Anonieko Ramos | last post: by
29 posts views Thread by gs | last post: by
1 post views Thread by Muchach | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.