473,805 Members | 2,055 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1742
On Sep 14, 10:53 am, "inhahe" <inh...@gmail.c omwrote:
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_********@yah oo.ca>
escribió:
On Sep 14, 10:53 am, "inhahe" <inh...@gmail.c omwrote:
>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.c omwrote:
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
2529
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 (?) however, upon google searching for "python lambda pickle" i find 2 posts, one including gvr, which apparently demonstrate that this was being attempted and even suggest that it is feasible. has this become available yet, or will it be soon?
176
8209
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? Thank you, -- greetz tom
2
2259
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. My problem is that Search defines a method (called automatically by __del__) to save its results between invocations: def _saveresults(self): self._oldresults = self._results file = open(self._storefile(), 'w')
4
7138
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 functionality to that library, finetune it, and see how things work. Well, the problem is that the library is quite huge, so the application's size gets not really acceptable - I'm developping for an
4
2893
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 our current extensive number of .Net 1.1 web apps convert to it. After a week's time I dont's mind the converted app code that was broken and had to be changed, due to deprecation, new framework classes (some of which have names identical to...
3
1303
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 Dim objFolder As New System.IO.DirectoryInfo(sDir) Dim objFile As System.IO.FileInfo() = objFolder.GetFiles
1
1441
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. Let's say, that the table is similar to the following, I want to exclude all products A from the beginning and the end. I have been told that I should use cursors but i am not very sure how to do it. thank you all in advance for your answers. e.g. A ...
10
8534
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 exactly easy to follow. What I assumed it was supposed to do is mark specific prefixes as not to be printed in the generated document, but that does not work with any of the XSL tools I'm currently working with (Xalan 1.2.2 and 2.7.0, xsltproc...
3
1719
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 output of this same search should also exclude any records where Field1 contains "info" ($searchtext = "info"). You can see by the textsearch template that I have 3 case conversion variables that can be used to to include/exclude "All", "all", "ALL",...
0
9716
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
9596
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
10360
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...
0
10105
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
9185
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...
1
7646
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6876
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
5542
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.