473,406 Members | 2,439 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

how to exclude specific things when pickling?

If I gather correctly pickling an object will pickle its entire hierarchy,
but what if there are certain types of objects anywhere within the hierarchy
that I don't want included in the serialization? What do I do to exclude
them? Thanks.
Sep 14 '08 #1
5 1714
On Sep 14, 10:53 am, "inhahe" <inh...@gmail.comwrote:
If I gather correctly pickling an object will pickle its entire hierarchy,
but what if there are certain types of objects anywhere within the hierarchy
that I don't want included in the serialization? What do I do to exclude
them? Thanks.
If your class defines a __getstate__ method, it is expected to return
the pickled state of the entire class. You can for example del those
items from self.__dict__ that you don't want pickled and then return
dumps(self).
Sep 14 '08 #2
inhahe wrote:
If I gather correctly pickling an object will pickle its entire hierarchy,
but what if there are certain types of objects anywhere within the hierarchy
that I don't want included in the serialization? What do I do to exclude
them? Thanks.
Pickle uses the methods __getstate__, __setstate__ and __reduce__ /
__reduce_ex__ to access certain aspects of an object like e.g. the
state. You can find more information in the pickle docs.

Christian

Sep 14 '08 #3
Michael Palmer wrote:
If your class defines a __getstate__ method, it is expected to return
the pickled state of the entire class. You can for example del those
items from self.__dict__ that you don't want pickled and then return
dumps(self).
FYI:
__getstate__ is ignored when __reduce__ is available.

Christian

Sep 14 '08 #4
En Sun, 14 Sep 2008 12:06:44 -0300, Michael Palmer <m_********@yahoo.ca>
escribió:
On Sep 14, 10:53 am, "inhahe" <inh...@gmail.comwrote:
>If I gather correctly pickling an object will pickle its entire
hierarchy,
but what if there are certain types of objects anywhere within the
hierarchy
that I don't want included in the serialization? What do I do to
exclude
them? Thanks.

If your class defines a __getstate__ method, it is expected to return
the pickled state of the entire class. You can for example del those
items from self.__dict__ that you don't want pickled and then return
dumps(self).
note: __getstate__ should return the *values* to be pickled, not the
pickled state; else you end doing the work twice.
And "del those items from self.__dict__" isn't a good idea, in general;
I'd use a *copy* of __dict__ instead.

--
Gabriel Genellina

Sep 16 '08 #5
On Sep 14, 9:53*am, "inhahe" <inh...@gmail.comwrote:
If I gather correctly pickling an object will pickle its entire hierarchy,
but what if there are certain types of objects anywhere within the hierarchy
that I don't want included in the serialization? *What do I do to exclude
them? * Thanks.
Are you picturing a custom pickler object?
Sep 16 '08 #6

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

Similar topics

4
by: gong | last post by:
hi i would like to pickle a lambda; according to the library docs in 2.3, i believe this shouldnt be possible, since a lambda is not a function defined at the top level of a module (?) ...
176
by: Thomas Reichelt | last post by:
Moin, short question: is there any language combining the syntax, flexibility and great programming experience of Python with static typing? Is there a project to add static typing to Python? ...
2
by: Kirk Strauser | last post by:
I have a module that defines a Search class and a SearchResult class. I use these classes by writing other modules that subclass both of them as needed to interface with particular search engines....
4
by: Maxim | last post by:
Greetings, In my C# project, I'm using a third-party Opensource library... in source code form. I mean no assemblies, just pure source code. It allows me to add new application-specific...
4
by: Brad | last post by:
I'm not one to rant or flame....so please excuse me while I do so for this once. I've now spent a bit of time working with VS2005 beta 2 to see how it functions for web development, especially how...
3
by: Gabe Matteson | last post by:
How would I go about excluding a list of specific directories in a listbox from being queried using the following code? Any ideas? Thank you. Private Sub rAllStats(ByVal sDir As String) Try ...
1
by: uniko | last post by:
Hello, I have a table with multiple fields, which describe several products.I want to exclude all the records of a specific product when it appears at the beginning or at the end of the table....
10
by: Simon Brooke | last post by:
I'm struggling to understand what 'exclude-result-prefixes' does and is for; the language of the standard http://www.zvon.org/xxl/XSLTreference/W3C/xslt.html#literal-result-element is not...
3
by: ITSimTech | last post by:
I'm trying to learn how/do two things here: 1) If the user searches for "Data" ($searchtext = "Data") the output should also include the fourth record because Field1 contains "all". 2) But the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
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,...
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.