deepcopy alternative?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • none

    #1

    deepcopy alternative?

    I have a very complex data structure which is basically a class object
    containing (sometimes many) other class objects, function references,
    ints, floats, etc. The man for the copy module states pretty clearly
    that it will not copy methods or functions. I've looked around for a
    while (prob just using the wrong keywords) and haven't found a good
    solution. As a workaround I've been using cPickle, loads(dumps(obj ))
    which is incredibly slow (~8 sec for a 1000 elem list).

    I believe the only thing stopping me from doing a deepcopy is the
    function references, but I'm not sure. If so is there any way to
    transform a string into a function reference(w/o eval or exec)?

    Example
    from copy import deepcopy
    a = ClassObj([ClassObj([3.2, 'str', funcRef]), 4, ClassObj[funcRef])
    b = deepcopy(a)
    TypeError: function() takes at least 2 arguments (0 given)

    All I want is a deepcopy of the list. Any suggestions would be appreciated.

    Jack Trades

    PS: If the answer to this is in __getstate__() or __setstate__() is
    there some reference to how these should be implemented besides the
    library reference?
  • Szabolcs Nagy

    #2
    Re: deepcopy alternative?

    I believe the only thing stopping me from doing a deepcopy is the
    function references, but I'm not sure. If so is there any way to
    transform a string into a function reference(w/o eval or exec)?
    what's your python version?
    for me deepcopy(lambda :1) does not work in py2.4 but it works in py2.5
    (in py2.4 i tried to override __deepcopy__ but it had no effect)

    Comment

    • Gabriel Genellina

      #3
      Re: deepcopy alternative?

      En Mon, 29 Jan 2007 20:17:11 -0300, <"none <"@bag.python.o rgescribió:
      I have a very complex data structure which is basically a class object
      containing (sometimes many) other class objects,
      What are "class objects"? instances of a given class, or a class itself?
      function references,
      ints, floats, etc. The man for the copy module states pretty clearly
      that it will not copy methods or functions. I've looked around for a
      while (prob just using the wrong keywords) and haven't found a good
      solution. As a workaround I've been using cPickle, loads(dumps(obj ))
      which is incredibly slow (~8 sec for a 1000 elem list).
      >
      I believe the only thing stopping me from doing a deepcopy is the
      function references, but I'm not sure. If so is there any way to
      transform a string into a function reference(w/o eval or exec)?
      What do you mean by "function reference"? A module-level function works
      fine both with pickle and copy. Methods do not; but it's not common to
      take methods out of a class and put them inside a tuple, by example...
      >
      Example
      from copy import deepcopy
      a = ClassObj([ClassObj([3.2, 'str', funcRef]), 4, ClassObj[funcRef])
      b = deepcopy(a)
      TypeError: function() takes at least 2 arguments (0 given)
      Please provide a *running* example. I've added some definitions, corrected
      some syntax errors, and this full example works for me:

      --- cut ---
      from copy import deepcopy

      class ClassObj(object ):
      def __init__(self, args):
      self.data = args

      def funcRef(x):
      print x

      a = ClassObj([ClassObj([3.2, 'str', funcRef]), 4, ClassObj([funcRef])])
      b = deepcopy(a)
      print a is b
      a.data.append(' hello')
      print len(a.data), len(b.data)
      --- cut ---
      PS: If the answer to this is in __getstate__() or __setstate__() is
      there some reference to how these should be implemented besides the
      library reference?
      Until you provide more info on your problem it's hard to tell.


      --
      Gabriel Genellina

      Comment

      • none

        #4
        Re: deepcopy alternative?

        Szabolcs Nagy wrote:
        >I believe the only thing stopping me from doing a deepcopy is the
        >function references, but I'm not sure. If so is there any way to
        >transform a string into a function reference(w/o eval or exec)?
        >
        what's your python version?
        for me deepcopy(lambda :1) does not work in py2.4 but it works in py2.5
        (in py2.4 i tried to override __deepcopy__ but it had no effect)
        >
        Thanks that fixed the problem real quick :)

        Jack Trades

        Comment

        Working...