Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python2....
File: copy.py
"""Generic (shallow and deep) copying operations.
[0] Fix | Delete
[1] Fix | Delete
Interface summary:
[2] Fix | Delete
[3] Fix | Delete
import copy
[4] Fix | Delete
[5] Fix | Delete
x = copy.copy(y) # make a shallow copy of y
[6] Fix | Delete
x = copy.deepcopy(y) # make a deep copy of y
[7] Fix | Delete
[8] Fix | Delete
For module specific errors, copy.Error is raised.
[9] Fix | Delete
[10] Fix | Delete
The difference between shallow and deep copying is only relevant for
[11] Fix | Delete
compound objects (objects that contain other objects, like lists or
[12] Fix | Delete
class instances).
[13] Fix | Delete
[14] Fix | Delete
- A shallow copy constructs a new compound object and then (to the
[15] Fix | Delete
extent possible) inserts *the same objects* into it that the
[16] Fix | Delete
original contains.
[17] Fix | Delete
[18] Fix | Delete
- A deep copy constructs a new compound object and then, recursively,
[19] Fix | Delete
inserts *copies* into it of the objects found in the original.
[20] Fix | Delete
[21] Fix | Delete
Two problems often exist with deep copy operations that don't exist
[22] Fix | Delete
with shallow copy operations:
[23] Fix | Delete
[24] Fix | Delete
a) recursive objects (compound objects that, directly or indirectly,
[25] Fix | Delete
contain a reference to themselves) may cause a recursive loop
[26] Fix | Delete
[27] Fix | Delete
b) because deep copy copies *everything* it may copy too much, e.g.
[28] Fix | Delete
administrative data structures that should be shared even between
[29] Fix | Delete
copies
[30] Fix | Delete
[31] Fix | Delete
Python's deep copy operation avoids these problems by:
[32] Fix | Delete
[33] Fix | Delete
a) keeping a table of objects already copied during the current
[34] Fix | Delete
copying pass
[35] Fix | Delete
[36] Fix | Delete
b) letting user-defined classes override the copying operation or the
[37] Fix | Delete
set of components copied
[38] Fix | Delete
[39] Fix | Delete
This version does not copy types like module, class, function, method,
[40] Fix | Delete
nor stack trace, stack frame, nor file, socket, window, nor array, nor
[41] Fix | Delete
any similar types.
[42] Fix | Delete
[43] Fix | Delete
Classes can use the same interfaces to control copying that they use
[44] Fix | Delete
to control pickling: they can define methods called __getinitargs__(),
[45] Fix | Delete
__getstate__() and __setstate__(). See the documentation for module
[46] Fix | Delete
"pickle" for information on these methods.
[47] Fix | Delete
"""
[48] Fix | Delete
[49] Fix | Delete
import types
[50] Fix | Delete
import weakref
[51] Fix | Delete
from copy_reg import dispatch_table
[52] Fix | Delete
[53] Fix | Delete
class Error(Exception):
[54] Fix | Delete
pass
[55] Fix | Delete
error = Error # backward compatibility
[56] Fix | Delete
[57] Fix | Delete
try:
[58] Fix | Delete
from org.python.core import PyStringMap
[59] Fix | Delete
except ImportError:
[60] Fix | Delete
PyStringMap = None
[61] Fix | Delete
[62] Fix | Delete
__all__ = ["Error", "copy", "deepcopy"]
[63] Fix | Delete
[64] Fix | Delete
def copy(x):
[65] Fix | Delete
"""Shallow copy operation on arbitrary Python objects.
[66] Fix | Delete
[67] Fix | Delete
See the module's __doc__ string for more info.
[68] Fix | Delete
"""
[69] Fix | Delete
[70] Fix | Delete
cls = type(x)
[71] Fix | Delete
[72] Fix | Delete
copier = _copy_dispatch.get(cls)
[73] Fix | Delete
if copier:
[74] Fix | Delete
return copier(x)
[75] Fix | Delete
[76] Fix | Delete
copier = getattr(cls, "__copy__", None)
[77] Fix | Delete
if copier:
[78] Fix | Delete
return copier(x)
[79] Fix | Delete
[80] Fix | Delete
reductor = dispatch_table.get(cls)
[81] Fix | Delete
if reductor:
[82] Fix | Delete
rv = reductor(x)
[83] Fix | Delete
else:
[84] Fix | Delete
reductor = getattr(x, "__reduce_ex__", None)
[85] Fix | Delete
if reductor:
[86] Fix | Delete
rv = reductor(2)
[87] Fix | Delete
else:
[88] Fix | Delete
reductor = getattr(x, "__reduce__", None)
[89] Fix | Delete
if reductor:
[90] Fix | Delete
rv = reductor()
[91] Fix | Delete
else:
[92] Fix | Delete
raise Error("un(shallow)copyable object of type %s" % cls)
[93] Fix | Delete
[94] Fix | Delete
return _reconstruct(x, rv, 0)
[95] Fix | Delete
[96] Fix | Delete
[97] Fix | Delete
_copy_dispatch = d = {}
[98] Fix | Delete
[99] Fix | Delete
def _copy_immutable(x):
[100] Fix | Delete
return x
[101] Fix | Delete
for t in (type(None), int, long, float, bool, str, tuple,
[102] Fix | Delete
frozenset, type, xrange, types.ClassType,
[103] Fix | Delete
types.BuiltinFunctionType, type(Ellipsis),
[104] Fix | Delete
types.FunctionType, weakref.ref):
[105] Fix | Delete
d[t] = _copy_immutable
[106] Fix | Delete
for name in ("ComplexType", "UnicodeType", "CodeType"):
[107] Fix | Delete
t = getattr(types, name, None)
[108] Fix | Delete
if t is not None:
[109] Fix | Delete
d[t] = _copy_immutable
[110] Fix | Delete
[111] Fix | Delete
def _copy_with_constructor(x):
[112] Fix | Delete
return type(x)(x)
[113] Fix | Delete
for t in (list, dict, set):
[114] Fix | Delete
d[t] = _copy_with_constructor
[115] Fix | Delete
[116] Fix | Delete
def _copy_with_copy_method(x):
[117] Fix | Delete
return x.copy()
[118] Fix | Delete
if PyStringMap is not None:
[119] Fix | Delete
d[PyStringMap] = _copy_with_copy_method
[120] Fix | Delete
[121] Fix | Delete
def _copy_inst(x):
[122] Fix | Delete
if hasattr(x, '__copy__'):
[123] Fix | Delete
return x.__copy__()
[124] Fix | Delete
if hasattr(x, '__getinitargs__'):
[125] Fix | Delete
args = x.__getinitargs__()
[126] Fix | Delete
y = x.__class__(*args)
[127] Fix | Delete
else:
[128] Fix | Delete
y = _EmptyClass()
[129] Fix | Delete
y.__class__ = x.__class__
[130] Fix | Delete
if hasattr(x, '__getstate__'):
[131] Fix | Delete
state = x.__getstate__()
[132] Fix | Delete
else:
[133] Fix | Delete
state = x.__dict__
[134] Fix | Delete
if hasattr(y, '__setstate__'):
[135] Fix | Delete
y.__setstate__(state)
[136] Fix | Delete
else:
[137] Fix | Delete
y.__dict__.update(state)
[138] Fix | Delete
return y
[139] Fix | Delete
d[types.InstanceType] = _copy_inst
[140] Fix | Delete
[141] Fix | Delete
del d
[142] Fix | Delete
[143] Fix | Delete
def deepcopy(x, memo=None, _nil=[]):
[144] Fix | Delete
"""Deep copy operation on arbitrary Python objects.
[145] Fix | Delete
[146] Fix | Delete
See the module's __doc__ string for more info.
[147] Fix | Delete
"""
[148] Fix | Delete
[149] Fix | Delete
if memo is None:
[150] Fix | Delete
memo = {}
[151] Fix | Delete
[152] Fix | Delete
d = id(x)
[153] Fix | Delete
y = memo.get(d, _nil)
[154] Fix | Delete
if y is not _nil:
[155] Fix | Delete
return y
[156] Fix | Delete
[157] Fix | Delete
cls = type(x)
[158] Fix | Delete
[159] Fix | Delete
copier = _deepcopy_dispatch.get(cls)
[160] Fix | Delete
if copier:
[161] Fix | Delete
y = copier(x, memo)
[162] Fix | Delete
else:
[163] Fix | Delete
try:
[164] Fix | Delete
issc = issubclass(cls, type)
[165] Fix | Delete
except TypeError: # cls is not a class (old Boost; see SF #502085)
[166] Fix | Delete
issc = 0
[167] Fix | Delete
if issc:
[168] Fix | Delete
y = _deepcopy_atomic(x, memo)
[169] Fix | Delete
else:
[170] Fix | Delete
copier = getattr(x, "__deepcopy__", None)
[171] Fix | Delete
if copier:
[172] Fix | Delete
y = copier(memo)
[173] Fix | Delete
else:
[174] Fix | Delete
reductor = dispatch_table.get(cls)
[175] Fix | Delete
if reductor:
[176] Fix | Delete
rv = reductor(x)
[177] Fix | Delete
else:
[178] Fix | Delete
reductor = getattr(x, "__reduce_ex__", None)
[179] Fix | Delete
if reductor:
[180] Fix | Delete
rv = reductor(2)
[181] Fix | Delete
else:
[182] Fix | Delete
reductor = getattr(x, "__reduce__", None)
[183] Fix | Delete
if reductor:
[184] Fix | Delete
rv = reductor()
[185] Fix | Delete
else:
[186] Fix | Delete
raise Error(
[187] Fix | Delete
"un(deep)copyable object of type %s" % cls)
[188] Fix | Delete
y = _reconstruct(x, rv, 1, memo)
[189] Fix | Delete
[190] Fix | Delete
memo[d] = y
[191] Fix | Delete
_keep_alive(x, memo) # Make sure x lives at least as long as d
[192] Fix | Delete
return y
[193] Fix | Delete
[194] Fix | Delete
_deepcopy_dispatch = d = {}
[195] Fix | Delete
[196] Fix | Delete
def _deepcopy_atomic(x, memo):
[197] Fix | Delete
return x
[198] Fix | Delete
d[type(None)] = _deepcopy_atomic
[199] Fix | Delete
d[type(Ellipsis)] = _deepcopy_atomic
[200] Fix | Delete
d[int] = _deepcopy_atomic
[201] Fix | Delete
d[long] = _deepcopy_atomic
[202] Fix | Delete
d[float] = _deepcopy_atomic
[203] Fix | Delete
d[bool] = _deepcopy_atomic
[204] Fix | Delete
try:
[205] Fix | Delete
d[complex] = _deepcopy_atomic
[206] Fix | Delete
except NameError:
[207] Fix | Delete
pass
[208] Fix | Delete
d[str] = _deepcopy_atomic
[209] Fix | Delete
try:
[210] Fix | Delete
d[unicode] = _deepcopy_atomic
[211] Fix | Delete
except NameError:
[212] Fix | Delete
pass
[213] Fix | Delete
try:
[214] Fix | Delete
d[types.CodeType] = _deepcopy_atomic
[215] Fix | Delete
except AttributeError:
[216] Fix | Delete
pass
[217] Fix | Delete
d[type] = _deepcopy_atomic
[218] Fix | Delete
d[xrange] = _deepcopy_atomic
[219] Fix | Delete
d[types.ClassType] = _deepcopy_atomic
[220] Fix | Delete
d[types.BuiltinFunctionType] = _deepcopy_atomic
[221] Fix | Delete
d[types.FunctionType] = _deepcopy_atomic
[222] Fix | Delete
d[weakref.ref] = _deepcopy_atomic
[223] Fix | Delete
[224] Fix | Delete
def _deepcopy_list(x, memo):
[225] Fix | Delete
y = []
[226] Fix | Delete
memo[id(x)] = y
[227] Fix | Delete
for a in x:
[228] Fix | Delete
y.append(deepcopy(a, memo))
[229] Fix | Delete
return y
[230] Fix | Delete
d[list] = _deepcopy_list
[231] Fix | Delete
[232] Fix | Delete
def _deepcopy_tuple(x, memo):
[233] Fix | Delete
y = []
[234] Fix | Delete
for a in x:
[235] Fix | Delete
y.append(deepcopy(a, memo))
[236] Fix | Delete
d = id(x)
[237] Fix | Delete
try:
[238] Fix | Delete
return memo[d]
[239] Fix | Delete
except KeyError:
[240] Fix | Delete
pass
[241] Fix | Delete
for i in range(len(x)):
[242] Fix | Delete
if x[i] is not y[i]:
[243] Fix | Delete
y = tuple(y)
[244] Fix | Delete
break
[245] Fix | Delete
else:
[246] Fix | Delete
y = x
[247] Fix | Delete
memo[d] = y
[248] Fix | Delete
return y
[249] Fix | Delete
d[tuple] = _deepcopy_tuple
[250] Fix | Delete
[251] Fix | Delete
def _deepcopy_dict(x, memo):
[252] Fix | Delete
y = {}
[253] Fix | Delete
memo[id(x)] = y
[254] Fix | Delete
for key, value in x.iteritems():
[255] Fix | Delete
y[deepcopy(key, memo)] = deepcopy(value, memo)
[256] Fix | Delete
return y
[257] Fix | Delete
d[dict] = _deepcopy_dict
[258] Fix | Delete
if PyStringMap is not None:
[259] Fix | Delete
d[PyStringMap] = _deepcopy_dict
[260] Fix | Delete
[261] Fix | Delete
def _deepcopy_method(x, memo): # Copy instance methods
[262] Fix | Delete
return type(x)(x.im_func, deepcopy(x.im_self, memo), x.im_class)
[263] Fix | Delete
_deepcopy_dispatch[types.MethodType] = _deepcopy_method
[264] Fix | Delete
[265] Fix | Delete
def _keep_alive(x, memo):
[266] Fix | Delete
"""Keeps a reference to the object x in the memo.
[267] Fix | Delete
[268] Fix | Delete
Because we remember objects by their id, we have
[269] Fix | Delete
to assure that possibly temporary objects are kept
[270] Fix | Delete
alive by referencing them.
[271] Fix | Delete
We store a reference at the id of the memo, which should
[272] Fix | Delete
normally not be used unless someone tries to deepcopy
[273] Fix | Delete
the memo itself...
[274] Fix | Delete
"""
[275] Fix | Delete
try:
[276] Fix | Delete
memo[id(memo)].append(x)
[277] Fix | Delete
except KeyError:
[278] Fix | Delete
# aha, this is the first one :-)
[279] Fix | Delete
memo[id(memo)]=[x]
[280] Fix | Delete
[281] Fix | Delete
def _deepcopy_inst(x, memo):
[282] Fix | Delete
if hasattr(x, '__deepcopy__'):
[283] Fix | Delete
return x.__deepcopy__(memo)
[284] Fix | Delete
if hasattr(x, '__getinitargs__'):
[285] Fix | Delete
args = x.__getinitargs__()
[286] Fix | Delete
args = deepcopy(args, memo)
[287] Fix | Delete
y = x.__class__(*args)
[288] Fix | Delete
else:
[289] Fix | Delete
y = _EmptyClass()
[290] Fix | Delete
y.__class__ = x.__class__
[291] Fix | Delete
memo[id(x)] = y
[292] Fix | Delete
if hasattr(x, '__getstate__'):
[293] Fix | Delete
state = x.__getstate__()
[294] Fix | Delete
else:
[295] Fix | Delete
state = x.__dict__
[296] Fix | Delete
state = deepcopy(state, memo)
[297] Fix | Delete
if hasattr(y, '__setstate__'):
[298] Fix | Delete
y.__setstate__(state)
[299] Fix | Delete
else:
[300] Fix | Delete
y.__dict__.update(state)
[301] Fix | Delete
return y
[302] Fix | Delete
d[types.InstanceType] = _deepcopy_inst
[303] Fix | Delete
[304] Fix | Delete
def _reconstruct(x, info, deep, memo=None):
[305] Fix | Delete
if isinstance(info, str):
[306] Fix | Delete
return x
[307] Fix | Delete
assert isinstance(info, tuple)
[308] Fix | Delete
if memo is None:
[309] Fix | Delete
memo = {}
[310] Fix | Delete
n = len(info)
[311] Fix | Delete
assert n in (2, 3, 4, 5)
[312] Fix | Delete
callable, args = info[:2]
[313] Fix | Delete
if n > 2:
[314] Fix | Delete
state = info[2]
[315] Fix | Delete
else:
[316] Fix | Delete
state = None
[317] Fix | Delete
if n > 3:
[318] Fix | Delete
listiter = info[3]
[319] Fix | Delete
else:
[320] Fix | Delete
listiter = None
[321] Fix | Delete
if n > 4:
[322] Fix | Delete
dictiter = info[4]
[323] Fix | Delete
else:
[324] Fix | Delete
dictiter = None
[325] Fix | Delete
if deep:
[326] Fix | Delete
args = deepcopy(args, memo)
[327] Fix | Delete
y = callable(*args)
[328] Fix | Delete
memo[id(x)] = y
[329] Fix | Delete
[330] Fix | Delete
if state is not None:
[331] Fix | Delete
if deep:
[332] Fix | Delete
state = deepcopy(state, memo)
[333] Fix | Delete
if hasattr(y, '__setstate__'):
[334] Fix | Delete
y.__setstate__(state)
[335] Fix | Delete
else:
[336] Fix | Delete
if isinstance(state, tuple) and len(state) == 2:
[337] Fix | Delete
state, slotstate = state
[338] Fix | Delete
else:
[339] Fix | Delete
slotstate = None
[340] Fix | Delete
if state is not None:
[341] Fix | Delete
y.__dict__.update(state)
[342] Fix | Delete
if slotstate is not None:
[343] Fix | Delete
for key, value in slotstate.iteritems():
[344] Fix | Delete
setattr(y, key, value)
[345] Fix | Delete
[346] Fix | Delete
if listiter is not None:
[347] Fix | Delete
for item in listiter:
[348] Fix | Delete
if deep:
[349] Fix | Delete
item = deepcopy(item, memo)
[350] Fix | Delete
y.append(item)
[351] Fix | Delete
if dictiter is not None:
[352] Fix | Delete
for key, value in dictiter:
[353] Fix | Delete
if deep:
[354] Fix | Delete
key = deepcopy(key, memo)
[355] Fix | Delete
value = deepcopy(value, memo)
[356] Fix | Delete
y[key] = value
[357] Fix | Delete
return y
[358] Fix | Delete
[359] Fix | Delete
del d
[360] Fix | Delete
[361] Fix | Delete
del types
[362] Fix | Delete
[363] Fix | Delete
# Helper for instance creation without calling __init__
[364] Fix | Delete
class _EmptyClass:
[365] Fix | Delete
pass
[366] Fix | Delete
[367] Fix | Delete
def _test():
[368] Fix | Delete
l = [None, 1, 2L, 3.14, 'xyzzy', (1, 2L), [3.14, 'abc'],
[369] Fix | Delete
{'abc': 'ABC'}, (), [], {}]
[370] Fix | Delete
l1 = copy(l)
[371] Fix | Delete
print l1==l
[372] Fix | Delete
l1 = map(copy, l)
[373] Fix | Delete
print l1==l
[374] Fix | Delete
l1 = deepcopy(l)
[375] Fix | Delete
print l1==l
[376] Fix | Delete
class C:
[377] Fix | Delete
def __init__(self, arg=None):
[378] Fix | Delete
self.a = 1
[379] Fix | Delete
self.arg = arg
[380] Fix | Delete
if __name__ == '__main__':
[381] Fix | Delete
import sys
[382] Fix | Delete
file = sys.argv[0]
[383] Fix | Delete
else:
[384] Fix | Delete
file = __file__
[385] Fix | Delete
self.fp = open(file)
[386] Fix | Delete
self.fp.close()
[387] Fix | Delete
def __getstate__(self):
[388] Fix | Delete
return {'a': self.a, 'arg': self.arg}
[389] Fix | Delete
def __setstate__(self, state):
[390] Fix | Delete
for key, value in state.iteritems():
[391] Fix | Delete
setattr(self, key, value)
[392] Fix | Delete
def __deepcopy__(self, memo=None):
[393] Fix | Delete
new = self.__class__(deepcopy(self.arg, memo))
[394] Fix | Delete
new.a = self.a
[395] Fix | Delete
return new
[396] Fix | Delete
c = C('argument sketch')
[397] Fix | Delete
l.append(c)
[398] Fix | Delete
l2 = copy(l)
[399] Fix | Delete
print l == l2
[400] Fix | Delete
print l
[401] Fix | Delete
print l2
[402] Fix | Delete
l2 = deepcopy(l)
[403] Fix | Delete
print l == l2
[404] Fix | Delete
print l
[405] Fix | Delete
print l2
[406] Fix | Delete
l.append({l[1]: l, 'xyz': l[2]})
[407] Fix | Delete
l3 = copy(l)
[408] Fix | Delete
import repr
[409] Fix | Delete
print map(repr.repr, l)
[410] Fix | Delete
print map(repr.repr, l1)
[411] Fix | Delete
print map(repr.repr, l2)
[412] Fix | Delete
print map(repr.repr, l3)
[413] Fix | Delete
l3 = deepcopy(l)
[414] Fix | Delete
import repr
[415] Fix | Delete
print map(repr.repr, l)
[416] Fix | Delete
print map(repr.repr, l1)
[417] Fix | Delete
print map(repr.repr, l2)
[418] Fix | Delete
print map(repr.repr, l3)
[419] Fix | Delete
class odict(dict):
[420] Fix | Delete
def __init__(self, d = {}):
[421] Fix | Delete
self.a = 99
[422] Fix | Delete
dict.__init__(self, d)
[423] Fix | Delete
def __setitem__(self, k, i):
[424] Fix | Delete
dict.__setitem__(self, k, i)
[425] Fix | Delete
self.a
[426] Fix | Delete
o = odict({"A" : "B"})
[427] Fix | Delete
x = deepcopy(o)
[428] Fix | Delete
print(o, x)
[429] Fix | Delete
[430] Fix | Delete
if __name__ == '__main__':
[431] Fix | Delete
_test()
[432] Fix | Delete
[433] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function