Copy a copy of "cool" every day
Copy-Copy object
Purpose: provide some functions to copy objects using the semantics of a shortest copy or a deep copy.
The copy module includes two functions: copy () and deepcopy (), which are used to copy existing objects.
1. Shallow copy
The shallow copy created by copy () is a new container that fills in references to the original object content. When a shortest copy of the list object is created, a new list is constructed and the elements of the original object are appended to the list.
1 import copy 2 3 class MyClass(object): 4 def __init__(self,name): 5 self.name = name 6 def __cmp__(self,other): 7 return cmp(self.name,other.name) 8 9 a = MyClass('a')10 my_lt = [a]11 dup = copy.copy(my_lt) 12 print ' my_lt:',my_lt13 print ' dup:',dup14 print ' dup is my_lt:',(dup is my_lt)15 print ' dup == my_lt:',(dup == my_lt)16 print 'dup[0] is my_lt[0]:',(dup[0] is my_lt[0])17 print 'dup[0] == my_lt[0]:',(dup[0] == my_lt[0])
Running result:
_ Cmp _ () is called when comparing class instances
For a shortest copy, the Myclass instance is not copied, so the reference in the dup list will point to the same object in my_lt.
2. Deep copy
The deep copy created by deepcopy () is a new container that fills in copies of the source object content. to create a deep copy of a list, a new list is formed. Copy the elements of the original list and append these copies to the new list.
1 import copy 2 3 class MyClass(object): 4 def __init__(self,name): 5 self.name = name 6 def __cmp__(self,other): 7 return cmp(self.name,other.name) 8 9 a = MyClass('a')10 my_lt = [a]11 dup = copy.deepcopy(my_lt) 12 print ' my_lt:',my_lt13 print ' dup:',dup14 print ' dup is my_lt:',(dup is my_lt)15 print ' dup == my_lt:',(dup == my_lt)16 print 'dup[0] is my_lt[0]:',(dup[0] is my_lt[0])17 print 'dup[0] == my_lt[0]:',(dup[0] == my_lt[0])
Running result:
3. Custom Replication
You can use special methods _ copy _ () and _ deepcopy _ () to control how to create a copy.
·Call _ copy _ () without providing any parameters. This will return a shortest copy of the object.
·Call _ deepcopy _ () and provide a northern dictionary, which returns a deep copy of the object. All member attributes that require deep replication must be passed to copy. deepcopy () together with the memo dictionary to control recursion.
For example:
1 import copy 2 3 class MyClass(object): 4 def __init__(self,name): 5 self.name = name 6 def __cmp__(self,other): 7 return cmp(self.name,other.name) 8 def __copy__(self): 9 print '__copy__()'10 return MyClass(self.name)11 def __deepcopy__(self,memo):12 print '__deepcopy__(%s)' % str(memo)13 return MyClass(copy.deepcopy(self.name, memo,))14 15 a = MyClass('a')16 sc = copy.copy(a)17 dc = copy.deepcopy(a)
Running result:
The memo dictionary is used to trace replicated values to avoid infinite recursion.
4. recursion in deep copies
To avoid the possibility of copying recursive data structures, deepcopy () uses a dictionary to track replicated objects. Pass the dictionary of this song into the _ deepcopy _ () method, so that the method can also be checked.
The following example shows an interconnected data structure (1) which can help prevent recursion by implementing the _ deepcopy _ () method.
Figure 1
1 class Graph(object): 2 3 def __init__(self,name,connections): 4 self.name = name 5 self.connections = connections 6 def add_connections(self,other): 7 self.connections.append(other) 8 9 def __repr__(self):10 return 'Graph(name=%s,id=%s)' % (self.name,id(self))11 def __deepcopy__(self,memo):12 print '\nCalling __deepcopy__ for %r' % self13 if self in memo:14 existing = memo.get(self)15 print ' Already copied to %r' % existing16 return existing17 print ' Memo dictionary'18 pprint.pprint(memo,indent=4,width=40)19 dup = Graph(copy.deepcopy(self.name, memo),[])20 print ' Copying to new object %s' % dup21 memo[self] = dup22 for c in self.connections:23 dup.add_connections(copy.deepcopy(c, memo))24 return dup25 26 root = Graph('root',[])27 a = Graph('a',[root])28 b = Graph('b',[a,root])29 root.add_connections(a)30 root.add_connections(b)31 32 dup = copy.deepcopy(root)
The Graph class contains some basic directed Graph methods. You can initialize a Graph instance based on a list of connected and existing nodes. The add_connection () method is used to establish a two-way understanding deepcopy. This _ deepcopy _ () method is also used to print the message to show how it gets called,
Manage the dictionary content as needed. Instead of copying the entire Link List, it creates a new list and appends copies of each connection to this list. This ensures that the memorandum dictionary is updated when each node is copied to avoid recursive issues or redundant node copies. As before, the Copied object is returned.
There are several loops in 1, but using the memorandum dictionary to process recursion can avoid stack overflow errors caused by traversal. When copying the root node root, enter the following:
The second time you encounter a root node, you are copying node a ,__ deepcopy _ () to detect recursion. instead of creating a new object, the existing value in the memo dictionary is reused.
'Everyday yige' in the "cool music box" Software'
I vomit blood *-*. I am joking. This is impossible. This is not recorded. If you like it, you can only add these songs to your own list every day. After that, copy the song name to your computer ............. or you can download it directly.
I don't have these records .........
Have a good time
How Can I copy the lyrics when I copy cool dog music from my computer?
Cool has a "Copy MP3" tool (cool 7 in the panel of the "tool"), click set in the upper right corner, check the brake copy lyrics, then you can use this tool to copy music to a mobile device and copy the lyrics at the same time ..
Hope to help you ~~