Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Static Public Attributes | Private Member Functions | Static Private Attributes
lib2to3.pytree.Base Class Reference
Inheritance diagram for lib2to3.pytree.Base:
Inheritance graph
[legend]
Collaboration diagram for lib2to3.pytree.Base:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def __new__
def __eq__
def __ne__
def clone
def post_order
def pre_order
def set_prefix
def get_prefix
def replace
def get_lineno
def changed
def remove
def next_sibling
def prev_sibling
def leaves
def depth
def get_suffix
def __str__

Public Attributes

 prefix
_PyObject_HEAD_EXTRA Py_ssize_t ob_refcnt
struct _typeobjectob_type

Static Public Attributes

 type = None
 parent = None
tuple children = ()
 was_changed = False
 was_checked = False

Private Member Functions

def _eq

Static Private Attributes

 __hash__ = None

Detailed Description

Abstract base class for Node and Leaf.

This provides some default functionality and boilerplate using the
template pattern.

A node may be a subnode of at most one parent.

Definition at line 32 of file pytree.py.


Member Function Documentation

def lib2to3.pytree.Base.__eq__ (   self,
  other 
)
Compare two nodes for equality.

This calls the method _eq().

Definition at line 55 of file pytree.py.

00055 
00056     def __eq__(self, other):
00057         """
00058         Compare two nodes for equality.
00059 
00060         This calls the method _eq().
00061         """
00062         if self.__class__ is not other.__class__:
00063             return NotImplemented
00064         return self._eq(other)

Here is the call graph for this function:

def lib2to3.pytree.Base.__ne__ (   self,
  other 
)
Compare two nodes for inequality.

This calls the method _eq().

Definition at line 67 of file pytree.py.

00067 
00068     def __ne__(self, other):
00069         """
00070         Compare two nodes for inequality.
00071 
00072         This calls the method _eq().
00073         """
00074         if self.__class__ is not other.__class__:
00075             return NotImplemented
00076         return not self._eq(other)

Here is the call graph for this function:

def lib2to3.pytree.Base.__new__ (   cls,
  args,
  kwds 
)
Constructor that prevents Base from being instantiated.

Definition at line 50 of file pytree.py.

00050 
00051     def __new__(cls, *args, **kwds):
00052         """Constructor that prevents Base from being instantiated."""
00053         assert cls is not Base, "Cannot instantiate Base"
00054         return object.__new__(cls)

Definition at line 236 of file pytree.py.

00236 
00237         def __str__(self):
00238             return str(self).encode("ascii")

def lib2to3.pytree.Base._eq (   self,
  other 
) [private]
Compare two nodes for equality.

This is called by __eq__ and __ne__.  It is only called if the two nodes
have the same type.  This must be implemented by the concrete subclass.
Nodes should be considered equal if they have the same structure,
ignoring the prefix string and other context information.

Reimplemented in lib2to3.pytree.Leaf, and lib2to3.pytree.Node.

Definition at line 77 of file pytree.py.

00077 
00078     def _eq(self, other):
00079         """
00080         Compare two nodes for equality.
00081 
00082         This is called by __eq__ and __ne__.  It is only called if the two nodes
00083         have the same type.  This must be implemented by the concrete subclass.
00084         Nodes should be considered equal if they have the same structure,
00085         ignoring the prefix string and other context information.
00086         """
00087         raise NotImplementedError

Here is the caller graph for this function:

Definition at line 164 of file pytree.py.

00164 
00165     def changed(self):
00166         if self.parent:
00167             self.parent.changed()
00168         self.was_changed = True

Here is the caller graph for this function:

def lib2to3.pytree.Base.clone (   self)
Return a cloned (deep) copy of self.

This must be implemented by the concrete subclass.

Reimplemented in lib2to3.pytree.Leaf, and lib2to3.pytree.Node.

Definition at line 88 of file pytree.py.

00088 
00089     def clone(self):
00090         """
00091         Return a cloned (deep) copy of self.
00092 
00093         This must be implemented by the concrete subclass.
00094         """
00095         raise NotImplementedError

def lib2to3.pytree.Base.depth (   self)

Definition at line 220 of file pytree.py.

00220 
00221     def depth(self):
00222         if self.parent is None:
00223             return 0
00224         return 1 + self.parent.depth()

Return the line number which generated the invocant node.

Definition at line 155 of file pytree.py.

00155 
00156     def get_lineno(self):
00157         """Return the line number which generated the invocant node."""
00158         node = self
00159         while not isinstance(node, Leaf):
00160             if not node.children:
00161                 return
00162             node = node.children[0]
00163         return node.lineno

Return the prefix for the node (see Leaf class).

DEPRECATED; use the prefix property directly.

Definition at line 122 of file pytree.py.

00122 
00123     def get_prefix(self):
00124         """
00125         Return the prefix for the node (see Leaf class).
00126 
00127         DEPRECATED; use the prefix property directly.
00128         """
00129         warnings.warn("get_prefix() is deprecated; use the prefix property",
00130                       DeprecationWarning, stacklevel=2)
00131         return self.prefix

Here is the call graph for this function:

Return the string immediately following the invocant node. This is
effectively equivalent to node.next_sibling.prefix

Definition at line 225 of file pytree.py.

00225 
00226     def get_suffix(self):
00227         """
00228         Return the string immediately following the invocant node. This is
00229         effectively equivalent to node.next_sibling.prefix
00230         """
00231         next_sib = self.next_sibling
00232         if next_sib is None:
00233             return ""
00234         return next_sib.prefix

Here is the call graph for this function:

Reimplemented in lib2to3.pytree.Leaf.

Definition at line 215 of file pytree.py.

00215 
00216     def leaves(self):
00217         for child in self.children:
00218             for x in child.leaves():
00219                 yield x

The node immediately following the invocant in their parent's children
list. If the invocant does not have a next sibling, it is None

Definition at line 183 of file pytree.py.

00183 
00184     def next_sibling(self):
00185         """
00186         The node immediately following the invocant in their parent's children
00187         list. If the invocant does not have a next sibling, it is None
00188         """
00189         if self.parent is None:
00190             return None
00191 
00192         # Can't use index(); we need to test by identity
00193         for i, child in enumerate(self.parent.children):
00194             if child is self:
00195                 try:
00196                     return self.parent.children[i+1]
00197                 except IndexError:
00198                     return None

Here is the call graph for this function:

Here is the caller graph for this function:

Return a post-order iterator for the tree.

This must be implemented by the concrete subclass.

Reimplemented in lib2to3.pytree.Leaf, and lib2to3.pytree.Node.

Definition at line 96 of file pytree.py.

00096 
00097     def post_order(self):
00098         """
00099         Return a post-order iterator for the tree.
00100 
00101         This must be implemented by the concrete subclass.
00102         """
00103         raise NotImplementedError

Return a pre-order iterator for the tree.

This must be implemented by the concrete subclass.

Reimplemented in lib2to3.pytree.Leaf, and lib2to3.pytree.Node.

Definition at line 104 of file pytree.py.

00104 
00105     def pre_order(self):
00106         """
00107         Return a pre-order iterator for the tree.
00108 
00109         This must be implemented by the concrete subclass.
00110         """
00111         raise NotImplementedError

Here is the caller graph for this function:

The node immediately preceding the invocant in their parent's children
list. If the invocant does not have a previous sibling, it is None.

Definition at line 200 of file pytree.py.

00200 
00201     def prev_sibling(self):
00202         """
00203         The node immediately preceding the invocant in their parent's children
00204         list. If the invocant does not have a previous sibling, it is None.
00205         """
00206         if self.parent is None:
00207             return None
00208 
00209         # Can't use index(); we need to test by identity
00210         for i, child in enumerate(self.parent.children):
00211             if child is self:
00212                 if i == 0:
00213                     return None
00214                 return self.parent.children[i-1]

Here is the call graph for this function:

Remove the node from the tree. Returns the position of the node in its
parent's children before it was removed.

Definition at line 169 of file pytree.py.

00169 
00170     def remove(self):
00171         """
00172         Remove the node from the tree. Returns the position of the node in its
00173         parent's children before it was removed.
00174         """
00175         if self.parent:
00176             for i, node in enumerate(self.parent.children):
00177                 if node is self:
00178                     self.parent.changed()
00179                     del self.parent.children[i]
00180                     self.parent = None
00181                     return i

Here is the call graph for this function:

Here is the caller graph for this function:

def lib2to3.pytree.Base.replace (   self,
  new 
)
Replace this node with a new one in the parent.

Definition at line 132 of file pytree.py.

00132 
00133     def replace(self, new):
00134         """Replace this node with a new one in the parent."""
00135         assert self.parent is not None, str(self)
00136         assert new is not None
00137         if not isinstance(new, list):
00138             new = [new]
00139         l_children = []
00140         found = False
00141         for ch in self.parent.children:
00142             if ch is self:
00143                 assert not found, (self.parent.children, self, new)
00144                 if new is not None:
00145                     l_children.extend(new)
00146                 found = True
00147             else:
00148                 l_children.append(ch)
00149         assert found, (self.children, self, new)
00150         self.parent.changed()
00151         self.parent.children = l_children
00152         for x in new:
00153             x.parent = self.parent
00154         self.parent = None

def lib2to3.pytree.Base.set_prefix (   self,
  prefix 
)
Set the prefix for the node (see Leaf class).

DEPRECATED; use the prefix property directly.

Definition at line 112 of file pytree.py.

00112 
00113     def set_prefix(self, prefix):
00114         """
00115         Set the prefix for the node (see Leaf class).
00116 
00117         DEPRECATED; use the prefix property directly.
00118         """
00119         warnings.warn("set_prefix() is deprecated; use the prefix property",
00120                       DeprecationWarning, stacklevel=2)
00121         self.prefix = prefix

Here is the call graph for this function:


Member Data Documentation

lib2to3.pytree.Base.__hash__ = None [static, private]

Definition at line 65 of file pytree.py.

tuple lib2to3.pytree.Base.children = () [static]

Reimplemented in lib2to3.pytree.Node.

Definition at line 46 of file pytree.py.

Definition at line 107 of file object.h.

struct _typeobject* _object::ob_type [inherited]

Definition at line 108 of file object.h.

lib2to3.pytree.Base.parent = None [static]

Definition at line 45 of file pytree.py.

Reimplemented in lib2to3.pytree.Leaf, and lib2to3.pytree.Node.

Definition at line 120 of file pytree.py.

lib2to3.pytree.Base.type = None [static]

Reimplemented in lib2to3.pytree.Leaf, and lib2to3.pytree.Node.

Definition at line 44 of file pytree.py.

Definition at line 47 of file pytree.py.

Definition at line 48 of file pytree.py.


The documentation for this class was generated from the following file: