Remove element.Element indexing ability, client to use element.Element.children
authorNick Downing <nick@ndcode.org>
Tue, 29 Jan 2019 04:21:47 +0000 (15:21 +1100)
committerNick Downing <nick@ndcode.org>
Tue, 29 Jan 2019 04:22:13 +0000 (15:22 +1100)
children.sed [new file with mode: 0644]
skel/element.py

diff --git a/children.sed b/children.sed
new file mode 100644 (file)
index 0000000..4b3a830
--- /dev/null
@@ -0,0 +1,28 @@
+s/in \(self\|_ast\|action\|i\|j\|k\|l\)\[[^\]]*\]\[[^\]]*\]\[[^\]]*\]$/&.children/
+s/in \(self\|_ast\|action\|i\|j\|k\|l\)\[[^\]]*\]\[[^\]]*\]$/&.children/
+s/in \(self\|_ast\|action\|i\|j\|k\|l\)\[[^\]]*\]$/&.children/
+s/in \(self\|_ast\|action\|i\|j\|k\|l\)$/&.children/
+s/\(in \(self\|_ast\|action\|i\|j\|k\|l\)\[[^\]]*\]\[[^\]]*\]\[[^\]]*\]\):/\1.children:/g
+s/\(in \(self\|_ast\|action\|i\|j\|k\|l\)\[[^\]]*\]\[[^\]]*\]\):/\1.children:/g
+s/\(in \(self\|_ast\|action\|i\|j\|k\|l\)\[[^\]]*\]\):/\1.children:/g
+s/\(in \(self\|_ast\|action\|i\|j\|k\|l\)\):/\1.children:/g
+s/\(in \(self\|_ast\|action\|i\|j\|k\|l\)\[[^\]]*\]\[[^\]]*\]\[[^\]]*\]\)\]/\1.children]/g
+s/\(in \(self\|_ast\|action\|i\|j\|k\|l\)\[[^\]]*\]\[[^\]]*\]\)\]/\1.children]/g
+s/\(in \(self\|_ast\|action\|i\|j\|k\|l\)\[[^\]]*\]\)\]/\1.children]/g
+s/\(in \(self\|_ast\|action\|i\|j\|k\|l\)\)\]/\1.children]/g
+s/\(len(\(self\|_ast\|action\|i\|j\|k\|l\)\[[^\]]*\]\[[^\]]*\]\[[^\]]*\]\))/\1.children)/g
+s/\(len(\(self\|_ast\|action\|i\|j\|k\|l\)\[[^\]]*\]\[[^\]]*\]\))/\1.children)/g
+s/\(len(\(self\|_ast\|action\|i\|j\|k\|l\)\[[^\]]*\]\))/\1.children)/g
+s/\(len(\(self\|_ast\|action\|i\|j\|k\|l\)\))/\1.children)/g
+s/\(\(self\|_ast\|action\|i\|j\|k\|l\)\[[^\]]*\]\[[^\]]*\]\[[^\]]*\]\)\[/\1.children[/g
+s/\(\(self\|_ast\|action\|i\|j\|k\|l\)\[[^\]]*\]\[[^\]]*\]\)\[/\1.children[/g
+s/\(\(self\|_ast\|action\|i\|j\|k\|l\)\[[^\]]*\]\)\[/\1.children[/g
+s/\(\(self\|_ast\|action\|i\|j\|k\|l\)\)\[/\1.children[/g
+s/\(\(self\|_ast\|action\|i\|j\|k\|l\)\[[^\]]*\]\[[^\]]*\]\[[^\]]*\]\)\.append/\1.children.append/g
+s/\(\(self\|_ast\|action\|i\|j\|k\|l\)\[[^\]]*\]\[[^\]]*\]\)\.append/\1.children.append/g
+s/\(\(self\|_ast\|action\|i\|j\|k\|l\)\[[^\]]*\]\)\.append/\1.children.append/g
+s/\(\(self\|_ast\|action\|i\|j\|k\|l\)\)\.append/\1.children.append/g
+s/\(\(self\|_ast\|action\|i\|j\|k\|l\)\[[^\]]*\]\[[^\]]*\]\[[^\]]*\]\)\.extend/\1.children.extend/g
+s/\(\(self\|_ast\|action\|i\|j\|k\|l\)\[[^\]]*\]\[[^\]]*\]\)\.extend/\1.children.extend/g
+s/\(\(self\|_ast\|action\|i\|j\|k\|l\)\[[^\]]*\]\)\.extend/\1.children.extend/g
+s/\(\(self\|_ast\|action\|i\|j\|k\|l\)\)\.extend/\1.children.extend/g
index 1711d7d..4ea7a4a 100644 (file)
 import xml.etree.ElementTree
 
 class Element:
-  #def __init__(self, tag, attrib={}, **extra):
-  #  if not isinstance(attrib, dict):
-  #    raise TypeError("attrib must be dict, not %s" % (
-  #      attrib.__class__.__name__,))
-  #  attrib = attrib.copy()
-  #  attrib.update(extra)
-  #  self.tag = tag
-  #  self.attrib = attrib
-  #  self.children = []
-
-  #def __repr__(self):
-  #  return "<%s %r at %#x>" % (self.__class__.__name__, self.tag, id(self))
-
-  #def makeelement(self, tag, attrib):
-  #  return self.__class__(tag, attrib)
-
-  #def copy(self):
-  #  elem = self.makeelement(self.tag, self.attrib)
-  #  elem.text = self.text
-  #  elem.tail = self.tail
-  #  elem[:] = self
-  #  return elem
-
-  def __len__(self):
-    return len(self.children)
-
-  #def __bool__(self):
-  #  warnings.warn(
-  #    "The behavior of this method will change in future versions.  "
-  #    "Use specific 'len(elem)' or 'elem is not None' test instead.",
-  #    FutureWarning, stacklevel=2
-  #    )
-  #  return len(self.children) != 0 # emulate old behaviour, for now
-
-  def __getitem__(self, index):
-    return self.children[index]
-
-  def __setitem__(self, index, element):
-    # if isinstance(index, slice):
-    #   for elt in element:
-    #     assert iselement(elt)
-    # else:
-    #   assert iselement(element)
-    self.children[index] = element
-
-  def __delitem__(self, index):
-    del self.children[index]
-
-  def append(self, subelement):
-    #self._assert_is_element(subelement)
-    self.children.append(subelement)
-
-  def extend(self, elements):
-    #for element in elements:
-    #  self._assert_is_element(element)
-    self.children.extend(elements)
-
-  def insert(self, index, subelement):
-    #self._assert_is_element(subelement)
-    self.children.insert(index, subelement)
-
-  #def _assert_is_element(self, e):
-  #  # Need to refer to the actual Python implementation, not the
-  #  # shadowing C implementation.
-  #  if not isinstance(e, _Element_Py):
-  #    raise TypeError('expected an Element, not %s' % type(e).__name__)
-
-  #def remove(self, subelement):
-  #  # assert iselement(element)
-  #  self.children.remove(subelement)
-
-  #def getchildren(self):
-  #  warnings.warn(
-  #    "This method will be removed in future versions.  "
-  #    "Use 'list(elem)' or iteration over elem instead.",
-  #    DeprecationWarning, stacklevel=2
-  #    )
-  #  return self.children
-
-  #def find(self, path, namespaces=None):
-  #  return ElementPath.find(self, path, namespaces)
-
-  #def findtext(self, path, default=None, namespaces=None):
-  #  return ElementPath.findtext(self, path, default, namespaces)
-
-  #def findall(self, path, namespaces=None):
-  #  return ElementPath.findall(self, path, namespaces)
-
-  #def iterfind(self, path, namespaces=None):
-  #  return ElementPath.iterfind(self, path, namespaces)
-
-  #def clear(self):
-  #  self.attrib.clear()
-  #  self.children = []
-  #  self.text = self.tail = None
-
-  #def get(self, key, default=None):
-  #  return self.attrib.get(key, default)
-
-  #def set(self, key, value):
-  #  self.attrib[key] = value
-
-  #def keys(self):
-  #  return self.attrib.keys()
-
-  #def items(self):
-  #  return self.attrib.items()
-
-  #def iter(self, tag=None):
-  #  if tag == "*":
-  #    tag = None
-  #  if tag is None or self.tag == tag:
-  #    yield self
-  #  for e in self.children:
-  #    yield from e.iter(tag)
-
-  ## compatibility
-  #def getiterator(self, tag=None):
-  #  # Change for a DeprecationWarning in 1.4
-  #  warnings.warn(
-  #    "This method will be removed in future versions.  "
-  #    "Use 'elem.iter()' or 'list(elem.iter())' instead.",
-  #    PendingDeprecationWarning, stacklevel=2
-  #  )
-  #  return list(self.iter(tag))
-
-  #def itertext(self):
-  #  tag = self.tag
-  #  if not isinstance(tag, str) and tag is not None:
-  #    return
-  #  t = self.text
-  #  if t:
-  #    yield t
-  #  for e in self:
-  #    yield from e.itertext()
-  #    t = e.tail
-  #    if t:
-  #      yield t
-
   def __init__(self, tag = 'Element', text = '', children = []):
     self.tag = tag
     self.tail = None
@@ -167,8 +28,8 @@ class Element:
     element = self.visited[0]
     if len(self.text[0]):
       element.text = self.text[0]
-    for i in range(len(self)):
-      child = self[i]
+    for i in range(len(self.children)):
+      child = self.children[i]
       if child.visited is None:
         child_element = xml.etree.ElementTree.Element(child.tag)
         child.visited = (child_element, -1, True)
@@ -229,7 +90,7 @@ def serialize(value, fout, encoding = 'unicode'):
   while i < len(todo):
     node = todo[i]
     node.visited = None
-    todo.extend(node[:])
+    todo.extend(node.children)
     i += 1
 
 def deserialize(fin, factory = Element, encoding = 'unicode'):
@@ -248,7 +109,7 @@ def deserialize(fin, factory = Element, encoding = 'unicode'):
         ref_list.extend([None] * (ref + 1 - len(ref_list)))
       ref_list[ref] = node
     children = [factory(i.tag) for i in element]
-    node[:] = children
+    node.children = children
     node.text = (
       ['' if element.text is None else element.text] +
       ['' if j.tail is None else j.tail for j in element]
@@ -260,19 +121,19 @@ def deserialize(fin, factory = Element, encoding = 'unicode'):
   return ref_list[0]
 
 def get_text(root, i):
-  assert len(root.text) == len(root) + 1
+  assert len(root.text) == len(root.children) + 1
   return root.text[i]
 
 def set_text(root, i, text):
-  assert len(root.text) == len(root) + 1
+  assert len(root.text) == len(root.children) + 1
   root.text[i] = text
 
 def to_text(root):
-  assert len(root.text) == len(root) + 1
+  assert len(root.text) == len(root.children) + 1
   return ''.join(
     [
       j
-      for i in range(len(root))
+      for i in range(len(root.children))
       for j in [root.text[i], to_text(root[i])]
     ] +
     [root.text[-1]]
@@ -281,10 +142,10 @@ def to_text(root):
 def concatenate(children, factory = Element, *args, **kwargs):
   root = factory(*args, **kwargs)
   for child in children:
-    assert len(root.text) == len(root) + 1
-    assert len(child.text) == len(child) + 1
+    assert len(root.text) == len(root.children) + 1
+    assert len(child.text) == len(child.children) + 1
     root.text[-1] += child.text[0]
-    root.extend(child[:])
+    root.children.extend(child.children)
     root.text.extend(child.text[1:])
-  assert len(root.text) == len(root) + 1
+  assert len(root.text) == len(root.children) + 1
   return root