Implemented correct copying, which actually copies python objects. Hopefully this works.

This commit is contained in:
Ozbolt Menegatti 2020-01-25 21:44:32 +01:00
parent 1f829386d2
commit 27409b6f63

View File

@ -1,6 +1,43 @@
empty_doc = __new__(DOMParser()).parseFromString("<empty />", "text/xml")
empty_doc = __new__(DOMParser()).parseFromString("<empty />", "text/xml").firstChild
def copy(parent):
if type(parent) is None:
console.log("Copying non-transcrypt object, I think, this could go fairly wrong")
return parent
elif type(parent) is dict:
result = {}
for k, v in parent.items():
result[k] = copy(v)
return result
elif type(parent) is list:
return [copy(i) for i in parent]
elif type(parent) is int or type(parent) is float or type(parent) is str or parent is None:
return parent
else:
# some fancy object it looks like
result = __pragma__ ('js', "{{}};")
console.log(parent)
proto = Object.getPrototypeOf(parent)
Object.setPrototypeOf(result, proto)
result["__class__"] = parent.__class__
for key in Object.getOwnPropertyNames(parent):
if key == "__class__":
continue
val = getattr(parent, key)
result[key] = copy(val)
return result
class Editable:
def make_copy(self):
# makes an internal copy of self
@ -9,8 +46,9 @@ class Editable:
if self._copy is not None:
self._copy = None
# make new copy
self._copy = JSON.parse(JSON.stringify(self))
# make new data only copy
self._copy = copy(self)
def copy(self):
# return internal copy of self
@ -22,7 +60,8 @@ class Editable:
def overwrite_with_copy(self):
for key, value in dict(self._copy).items():
if key == "_copy":
# skip functions and _copy
if key == "_copy" or type(value) is None:
continue
setattr(self, key, value)