Automatic fields pickling

Category: Python - Miscellaneous

A 'fields' class is a class that acts like a struct full of fields, e.g.:

<pre>
class Titles:
def __init__(self, name): self.name = name
def dr(self): return "Dr. " + self.name
def mr(self): return "Mr. " + self.name
</pre>

Once an instance is constructed, the return value of x.dr() or x.mr() doesn't change.

I sometimes have 'fields' classes that I need to pickle (e.g. they go over a wire) but they contain unpickleable members, like file objects or tracebacks.

This code adds picklers to such classes that automatically 'flattens' the instances on pickling, saving the return values and making the unpickled instance return the saved values. Date: 22 January, 2012


Shortcuts

Homepage: http://code.activestate.com/recipes/456363-automatic-fields-pickling/?in=lang-python

Developer: Ori Peleg

License: Python License

Operating System: Windows

Add a Comment

all are required fields

     
What do you think of this resource?

Select Your Rate:

Votes:0

 

Related Scripts Download

Free Online Personal Finance Software, Multi Currency, Online Expense Tracking, Budget Planner with special features like shortcut, multi-level and multiple tagging, batch transactions, built with silverlight.

developer Developer: Kel
license License: Freeware
operating systems Operating System: Linux, Unix, Windows


Python does not support assignment in if and while statements such as "if (x=func()):".

developer Developer: harish anand
license License: Freeware
operating systems Operating System: Windows


A function to solve simultaneous equations in two variables.

developer Developer: Anand B Pillai
license License: Freeware
operating systems Operating System: Windows


If you need


to define named tuple subclasses (including reusable abstract ones), adding/overriding some methods, in a convenient way;
to have the named tuple ABC (abstract base class) for isinstance/issubclass tests;
or simply would like to define your named tuple classes in a class-syntax-based and DRY way (without repeating type names.

developer Developer: Jan Kaliszewski
license License: Freeware
operating systems Operating System: Windows


Reads or Creates Windows shortcuts (links).

developer Developer: Louis RIVIERE
license License: Freeware
operating systems Operating System: Windows


This recipe is another example of how to use the winreg module.

developer Developer: Stephen Chappell
license License: Python License
operating systems Operating System: Windows


The code presented below is a short example of how
to use winreg.

developer Developer: Stephen Chappell
license License: Python License
operating systems Operating System: Windows


This is a container class, suitable for use to answer the question "How do I pass a reference to a variable to a function", abusing some of the methods to get a syntax that's easier to use than more conventional containers.

developer Developer: Mike Meyer
license License: Python License
operating systems Operating System: Windows


You have files named with funky characters lying around in your
filesystem.

developer Developer: Micah Elliott
license License: Python License
operating systems Operating System: Windows