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

Barak, Ron Ron.Barak at lsi.com
Sun Feb 8 11:17:34 EST 2009


Hi John,

Maybe you encountered the below issue in your Python programming, and you may offer advise ?

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 copy/deepcopy from the original to the target, and then delete the cStringIO.StringO object from the target.

However, trying to use copy.deepcopy produced (see full traceback below my signature): 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).

Trying that, I did:

            for_pickle_log_stream = LogStream(sac_log_file)
            for key in log_stream.__dict__:
                if key != "input_file":
                    for_pickle_log_stream[key] = log_stream[key]

But that gave me:

Traceback (most recent call last):
  File "c:\Documents and Settings\rbarak\rbarak_devel\LogManager\./failover_pickle_demo02.py", line 393, in <module>
    for_pickle_log_stream[key] = log_stream[key]
AttributeError: FailoverMatches instance has no attribute '__getitem__'

Can you suggest a better way ?



Another question: Do you have an example of creating a __getitem__ method for my class?
The class includes the following:

log_stream.__dict__:
{   'db': [   {   'DPM': 'iSW-00-090',
                  'end_time': '2009 Dec 15 16:17:24',
                  'event': 'Volume failover',
                  'event_duration': 0L,
                  'first_line_pos': 12453,
                  'last_line_pos': 12871,
                  'rec_num': 0,
                  'start_time': '2009 Dec 15 16:17:24',
                  'volume_name': 'Domain 5:DVol1_1'},
              {   'DPM': 'iSW-00-090',
                  'end_time': '2009 Dec 15 16:17:24',
                  'event': 'Volume failover',
                  'event_duration': 0L,
                  'first_line_pos': 13409,
                  'last_line_pos': 13827,
                  'rec_num': 1,
                  'start_time': '2009 Dec 15 16:17:24',
                  'volume_name': 'Domain 5:DVol1_2'}],
    'end_timestamp': 1260886645L,
    'file_pointer': 58180,
    'filename_array': [   u'C:\\Documents and Settings\\rbarak\\rbarak_devel\\LogManager\\logs_3_vol\\sac.log.367lines.gz'],
    'input_file': <cStringIO.StringO object at 0x01B3D700>,
    'last_line_offset': 58181,
    'log_stream_array': [<LogStream.LogStream instance at 0x01B87350>]}

(the red item is not needed in the target object).

Thanks,
Ron.

Traceback (most recent call last):
  File "c:\Documents and Settings\rbarak\rbarak_devel\LogManager\./failover_pickle_demo02.py", line 388, in <module>
    for_pickle_log_stream = copy.deepcopy(log_stream)
  File "c:\Python25\lib\copy.py", line 162, in deepcopy
    y = copier(x, memo)
  File "c:\Python25\lib\copy.py", line 291, in _deepcopy_inst
    state = deepcopy(state, memo)
  File "c:\Python25\lib\copy.py", line 162, in deepcopy
    y = copier(x, memo)
  File "c:\Python25\lib\copy.py", line 254, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "c:\Python25\lib\copy.py", line 189, in deepcopy
    y = _reconstruct(x, rv, 1, memo)
  File "c:\Python25\lib\copy.py", line 322, in _reconstruct
    y = callable(*args)
  File "c:\Python25\lib\copy_reg.py", line 92, in __newobj__
    return cls.__new__(cls, *args)
TypeError: object.__new__(cStringIO.StringO) is not safe, use cStringIO.StringO.__new__()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090208/fdd4f617/attachment.html>


More information about the Python-list mailing list