Best way to have intermediate object description format

J

jm.suresh

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
 
P

Paddy

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.
 
J

jm.suresh

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,
 
L

Larry Bates

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)
 
P

Paddy

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,
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top