Set the point at which create_node and companion methods will insert into the graph. When used within a 'with' statement, this will temporary set the insert point and then restore it when the with statement exits:: with g.inserting_before(n): ... # insert
(self, n: Optional[Node] = None)
| 953 | |
| 954 | @compatibility(is_backward_compatible=True) |
| 955 | def inserting_before(self, n: Optional[Node] = None): |
| 956 | """Set the point at which create_node and companion methods will insert into the graph. |
| 957 | When used within a 'with' statement, this will temporary set the insert point and |
| 958 | then restore it when the with statement exits:: |
| 959 | |
| 960 | with g.inserting_before(n): |
| 961 | ... # inserting before node n |
| 962 | ... # insert point restored to what it was previously |
| 963 | g.inserting_before(n) # set the insert point permanently |
| 964 | |
| 965 | Args: |
| 966 | |
| 967 | n (Optional[Node]): The node before which to insert. If None this will insert before |
| 968 | the beginning of the entire graph. |
| 969 | |
| 970 | Returns: |
| 971 | A resource manager that will restore the insert point on ``__exit__``. |
| 972 | """ |
| 973 | if n is None: |
| 974 | return self.inserting_after(self._root) |
| 975 | assert n.graph == self, "Node to insert before is not in graph." |
| 976 | return _InsertPoint(self, n.prepend) |
| 977 | |
| 978 | @compatibility(is_backward_compatible=True) |
| 979 | def inserting_after(self, n: Optional[Node] = None): |