{"action":"created","issue":{"url":"https://api.github.com/repos/vishnubob/python-midi/issues/60","labels_url":"https://api.github.com/repos/vishnubob/python-midi/issues/60/labels{/name}","comments_url":"https://api.github.com/repos/vishnubob/python-midi/issues/60/comments","events_url":"https://api.github.com/repos/vishnubob/python-midi/issues/60/events","html_url":"https://github.com/vishnubob/python-midi/issues/60","id":93024339,"number":60,"title":"Best way to deepcopy a track","user":{"login":"tleyden","id":296876,"avatar_url":"https://avatars.githubusercontent.com/u/296876?v=3","gravatar_id":"","url":"https://api.github.com/users/tleyden","html_url":"https://github.com/tleyden","followers_url":"https://api.github.com/users/tleyden/followers","following_url":"https://api.github.com/users/tleyden/following{/other_user}","gists_url":"https://api.github.com/users/tleyden/gists{/gist_id}","starred_url":"https://api.github.com/users/tleyden/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tleyden/subscriptions","organizations_url":"https://api.github.com/users/tleyden/orgs","repos_url":"https://api.github.com/users/tleyden/repos","events_url":"https://api.github.com/users/tleyden/events{/privacy}","received_events_url":"https://api.github.com/users/tleyden/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-07-04T15:59:04Z","updated_at":"2015-07-06T16:49:19Z","closed_at":null,"body":"I'm trying to get a deep copy of a track via:\r\n\r\n```\r\ncopy.deepcopy(track)\r\n```\r\n\r\nbut I'm getting this error:\r\n\r\n```\r\n  File \"/Users/tleyden/Development/climate_music/src/transformer.py\", line 52, in transform\r\n    track_copy = copy.deepcopy(track)\r\n  File \"/Users/tleyden/DevLibraries/anaconda/lib/python2.7/copy.py\", line 190, in deepcopy\r\n    y = _reconstruct(x, rv, 1, memo)\r\n  File \"/Users/tleyden/DevLibraries/anaconda/lib/python2.7/copy.py\", line 351, in _reconstruct\r\n    item = deepcopy(item, memo)\r\n  File \"/Users/tleyden/DevLibraries/anaconda/lib/python2.7/copy.py\", line 190, in deepcopy\r\n    y = _reconstruct(x, rv, 1, memo)\r\n  File \"/Users/tleyden/DevLibraries/anaconda/lib/python2.7/copy.py\", line 346, in _reconstruct\r\n    setattr(y, key, value)\r\n  File \"/Users/tleyden/DevLibraries/anaconda/lib/python2.7/site-packages/midi/events.py\", line 125, in set_velocity\r\n    self.data[1] = val\r\nAttributeError: data\r\n```\r\n\r\nAs an alternative, I'm writing my own kludge and just propagating the events I care about:\r\n\r\n```\r\ndef copy_track(track):\r\n    \"\"\"\r\n    copy.deepcopy() didn't work, so I hand rolled this as a workaround\r\n    \"\"\"\r\n    track_copy = midi.Track()\r\n    for event in track:\r\n        if isinstance(event, midi.NoteOnEvent):\r\n            on = midi.NoteOnEvent(tick=event.tick, velocity=event.velocity, pitch=event.pitch)\r\n            track_copy.append(on)\r\n        if isinstance(event, midi.NoteOffEvent):\r\n            off = midi.NoteOffEvent(tick=event.tick, velocity=event.velocity, pitch=event.pitch)\r\n            track_copy.append(off)\r\n     return track_copy\r\n```\r\n\r\nIf it's not feasible to make `copy.deepcopy()` work, is there a more elegant way of rolling my own?"},"comment":{"url":"https://api.github.com/repos/vishnubob/python-midi/issues/comments/118922669","html_url":"https://github.com/vishnubob/python-midi/issues/60#issuecomment-118922669","issue_url":"https://api.github.com/repos/vishnubob/python-midi/issues/60","id":118922669,"user":{"login":"tdhsmith","id":1791252,"avatar_url":"https://avatars.githubusercontent.com/u/1791252?v=3","gravatar_id":"","url":"https://api.github.com/users/tdhsmith","html_url":"https://github.com/tdhsmith","followers_url":"https://api.github.com/users/tdhsmith/followers","following_url":"https://api.github.com/users/tdhsmith/following{/other_user}","gists_url":"https://api.github.com/users/tdhsmith/gists{/gist_id}","starred_url":"https://api.github.com/users/tdhsmith/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tdhsmith/subscriptions","organizations_url":"https://api.github.com/users/tdhsmith/orgs","repos_url":"https://api.github.com/users/tdhsmith/repos","events_url":"https://api.github.com/users/tdhsmith/events{/privacy}","received_events_url":"https://api.github.com/users/tdhsmith/received_events","type":"User","site_admin":false},"created_at":"2015-07-06T16:49:19Z","updated_at":"2015-07-06T16:49:19Z","body":"I never really looked into this fully. From the discussion last time (#40), it seems I had tracked it to a known interaction between `deepcopy` using `property`/custom getters/setters, but I also seem to recall something about how `__slots__` worked affecting it. Hrm...\r\n\r\nSo I think fixing it is feasible, but no one ever got around to figuring it out.\r\n\r\nIn the meantime, I know you can simply provide custom functions for `__copy__`/`__deepcopy__` which will be used by them automatically. [Here's an SO thread on it.](http://stackoverflow.com/questions/1500718/what-is-the-right-way-to-override-the-copy-deepcopy-operations-on-an-object-in-p)"}}