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

Discussion in 'Python' started by John Machin, Feb 8, 2009.

  1. John Machin

    John Machin Guest

    On Feb 9, 1:01 am, Christian Heimes <> 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
    ?
    John Machin, Feb 8, 2009
    #1
    1. Advertising

  2. John Machin

    John Machin Guest

    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".
    John Machin, Feb 9, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kent Johnson
    Replies:
    4
    Views:
    392
    Terry Hancock
    Nov 13, 2004
  2. thunk
    Replies:
    1
    Views:
    292
    thunk
    Mar 30, 2010
  3. thunk
    Replies:
    0
    Views:
    457
    thunk
    Apr 1, 2010
  4. thunk
    Replies:
    14
    Views:
    600
    thunk
    Apr 3, 2010
  5. Kedar Mhaswade
    Replies:
    1
    Views:
    116
    Gary Wright
    Jan 25, 2011
Loading...

Share This Page