473,756 Members | 9,662 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to marshal objects to readable files?

Hi list:
I just want to marshal objects (instance of custom classes)to a human
*READABEL *file/string, and also, I want unmarshal it back. in xml
format or any other format.
Any advice? Which lib should I use?
Thanks a lot.
Sep 14 '08 #1
4 1935
On Sep 14, 10:28*am, nielinjie <nielin...@gmai l.comwrote:
Hi list:
I just want to marshal objects (instance of custom classes)to a human
*READABEL *file/string, and also, I want unmarshal it back. in xml
format or any other format.
Any advice? Which lib should I use?
Thanks a lot.
Nielinjie,

There is no generic way; an object can contain a reference to another
object.

You can get started with ob.__dict__ as follows:
>>class A: pass
....
>>a= A()
a.b= 0
a.c= 'abc'
a.__dict__
{'c': 'abc', 'b': 0}

But the only way TMK (to my knowledge) to retrieve the contents is
eval, which is very hackish.

The PyYAML package produces the following (continued):
>>b= A()
b.d= 'efg'
b.e= 1.2
a.d= b
b.parent= a
print yaml.dump( a )
&id001 !!python/object:__main__ .A
b: 0
c: abc
d: !!python/object:__main__ .A
d: efg
e: 1.2
parent: *id001
>>print yaml.dump( b )
&id001 !!python/object:__main__ .A
d: efg
e: 1.2
parent: !!python/object:__main__ .A
b: 0
c: abc
d: *id001
>>yaml.load( yaml.dump( a ) )
<__main__.A instance at 0x00D098A0>
>>_.__dict__
{'c': 'abc', 'b': 0, 'd': <__main__.A instance at 0x00D09788>}

It caches IDs to reference circular references, and writes nested
objects in place. As you can see, you don't need to dump both 'a' and
'b'; 'a' contains 'b' already. You do need the types of the objects
already living in the same namespace as when you stored them.

If it's not human readable enough for your use, please write an
example of what you want your output to look like. Abstract is ok.

PyYAML is available at http://pyyaml.org/ . Download from
http://pyyaml.org/wiki/PyYAML . I've only skimmed it though.
Sep 14 '08 #2
En Sun, 14 Sep 2008 15:09:52 -0300, Aaron "Castironpi " Brady
<ca********@gma il.comescribió:
On Sep 14, 10:28*am, nielinjie <nielin...@gmai l.comwrote:
>Hi list:
I just want to marshal objects (instance of custom classes)to a human
*READABEL *file/string, and also, I want unmarshal it back. in xml
format or any other format.
Any advice? Which lib should I use?

The PyYAML package produces the following (continued):
>>>print yaml.dump( a )
&id001 !!python/object:__main__ .A
b: 0
c: abc
d: !!python/object:__main__ .A
d: efg
e: 1.2
parent: *id001
JSON is another format, much simpler and probably more suited for human
reading. But it is somewhat limited on what it can represent. There are a
few Python implementations , maybe the most used is simplejson, which comes
bundled with Python 2.6 as the json module.

--
Gabriel Genellina

Sep 14 '08 #3
On Sep 14, 11:28 am, nielinjie <nielin...@gmai l.comwrote:
Hi list:
I just want to marshal objects (instance of custom classes)to a human
*READABEL *file/string, and also, I want unmarshal it back. in xml
format or any other format.
Any advice? Which lib should I use?
Thanks a lot.
There is module pickle in the standard library. It's output is not all
that bad (in text mode, that is). If you don't like it, it would still
be a good starting point for a custom serializer - it does all the
tree walking for you, so you would only have to customize the strings
it writes and reads back in.

Sep 16 '08 #4
On Sep 14, 8:28*am, nielinjie <nielin...@gmai l.comwrote:
Hi list:
I just want to marshal objects (instance of custom classes)to a human
*READABEL *file/string, and also, I want unmarshal it back. in xml
format or any other format.
Any advice? Which lib should I use?
Thanks a lot.
I think YAML is the solution most targeted for your needs. To quote
the website: "YAML is a human friendly data serialization standard for
all programming languages."

http://yaml.org/

If you're willing to sacrifice some readability for truly robust
serialization, try pickle, which isn't horribly cryptic.

If you want simplicity and AJAX interoperabilit y, consider JSON.

If you consider Python itself to be human readable, and if your
security/performance situation does not preclude the use of eval(),
consider the PrettyPrinter module.

Sep 16 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4725
by: Mountain Bikn' Guy | last post by:
It is known that one cannot pass arguments as ref or out in a marshal-by-reference class. My problem is that I have a C DLL (and C# wrapper) that I need to isolate in an AppDomain and then I need to interact with many objects in that DLL and wrapper by reference. (So the classes inherit from MBRO.) While my app is running, I need to obtain info from objects in the 2nd app domain frequently/speedily and I need update the GUI; and I need to...
3
1941
by: Tom | last post by:
I think I'm still a little rough on the principle and understanding of Marshal by value and Marshal by reference after reading various materials. my understanding of Marshal by value is that the object is copied and passed by value out of the application domain. So does that mean the ENTIRE server object is copied to the client side ? thus all calculations are done on the client side ? ie. if client/server
0
2694
by: Johannes Unfried | last post by:
Problem Best practice needed to marshal STL data from managed code to unmanaged code & vice vers Details managed code is written in managed C++ & accesses the unmanaged code (i.e. lives in a mixed mode DLL unmanaged code is written in unmanaged C++ & resides in an unmanaged DL Product used is VS.NET 200 Please tell me what is the best practice for getting and setting data fro unmanaged to managed part when I have a class with...
4
6869
by: Michael McGarry | last post by:
Hi, I am using the marshal module in python to save a data structure to a file. It does not appear to be portable. The data is saved on a Linux machine. Loading that same data on a Mac gives me the bad marshal data message. Is this data really not portable or I am doing something wrong here. I thought the whole point of using Python and similar cross platform scripting languages to write once run everywhere, does that not hold
2
2006
by: jason | last post by:
hello everyone, i have had a suppot ticket open with microsoft for some time to investigate a memory leak issue we are experiencing on our production web servers. the web servers host both ASP classic and ASP.NET applications, and the leak is caused, at least in part, by the ASP.NET appliactions according to our support engineer. this is what he says: After a bit of research I was able to determine more on this stack. The
0
2509
by: metaperl | last post by:
A Comparison of Python Class Objects and Init Files for Program Configuration ============================================================================= Terrence Brannon bauhaus@metaperl.com http://www.livingcosmos.org/Members/sundevil/python/articles/a-comparison-of-python-class-objects-and-init-files-for-program-configuration/view
5
1993
by: Anurag | last post by:
I have been chasing a problem in my code since hours and it bolis down to this import marshal marshal.dumps(str(123)) != marshal.dumps(str("123")) Can someone please tell me why? when str(123) == str("123") or are they different?
10
1572
by: bkustel | last post by:
I'm stuck on a problem where I want to use marshal for serialization (yes, yes, I know (c)Pickle is normally recommended here). I favor marshal for speed for the types of data I use. However it seems that marshal.dumps() for large objects has a quadratic performance issue which I'm assuming is that it grows its memory buffer in constant increments. This causes a nasty slowdown for marshaling large objects. I thought I would get around...
0
2736
by: xrxst32 | last post by:
Hello there, I have some doubts about the best practice for using COM automation, the Runtime Callable Wrapper (RCW) and Marshal.ReleaseComObject. So the question is/are: Do I need to release each COM object explicit by a call to Marshal.ReleaseComObject, does the RCW take care of that or does it leaks unmanaged resources?
0
9482
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9292
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10062
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9901
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9878
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9728
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8733
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6551
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5322
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.