Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: How to copy an instance without its cStringIO.StringO item ?

Reply
Thread Tools

Re: How to copy an instance without its cStringIO.StringO item ?

 
 
John Machin
Guest
Posts: n/a
 
      02-08-2009
On Feb 9, 1:01*am, Christian Heimes <li...@cheimes.de> wrote:
> Barak, Ron schrieb:
>
> > Hi,

>
> > I need to copy an instance of a class, where one of the items in the original is a cStringIO.StringO object.
> > I do not need the cStringIO.StringO in the target object.
> > The target instance is to be pickled.

>
> > Googling made me devise the following plan: do a deepcopy from the original to the target, and then delete the cStringIO.StringO object from the target.

>
> > However, trying to use copy.deepcopy produced: TypeError: object.__new__(cStringIO.StringO) is not safe, use cStringIO.StringO.__new__()

>
> > Is there a way to create a copy of the instance, sans the cStringIO.StringO ?

>
> > If I find no elegant way to do that, I thought of creating a "blank" target instance; then iterating over the __dict__ of the original, and manually copy the items to the target (while not copying the cStringIO.StringO to the target).

>
> > Can you suggest a better way ?

>
> You can overwrite some functions in order to omit attributes from a
> pickle. It's all documented athttp://docs.python.org/library/pickle.html#pickling-and-unpickling-no...


If there is no chance that some other thread could be accessing the
source object, can't the OP just do:
temp = source.stringio
del source.stringio
# make a pickle from source
source.stringio = temp
?



 
Reply With Quote
 
 
 
 
John Machin
Guest
Posts: n/a
 
      02-09-2009
On 9/02/2009 8:51 PM, Barak, Ron wrote:
> Hi John,
>
> Thanks for the suggestion.
>
> What I do now in preparation for pickling is:
>
> for_pickle_log_stream = LogStream(sac_log_file)
> for key in log_stream.__dict__:
> if key != "input_file":
> for_pickle_log_stream.__dict__[key] =
> log_stream.__dict__[key]
> del for_pickle_log_stream.__dict__["input_file"]
>
> (i.e., log_stream is the source instance, and for_pickle_log_stream is
> the target)
>
> So, it seems to be in agreement with your suggestion.


which was
"""
If there is no chance that some other thread could be accessing the
source object, can't the OP just do:
temp = source.stringio
del source.stringio
# make a pickle from source
source.stringio = temp
"""
which I regard as radically different to your approach; please explain
what you mean by "in agreement with".


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Its a bird, its a plane, its.. um, an Attribute based System? thunk Ruby 14 04-03-2010 10:08 AM
Its a bird, its a plane, its.. um, an Attribute based System? thunk Ruby 0 04-01-2010 10:25 PM
Its a bird, its a plane, no ummm, its a Ruide thunk Ruby 1 03-30-2010 11:10 AM
Accessing an instance via its memory address (instance at ...) Kent Johnson Python 4 11-13-2004 07:42 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57