This module contains some language agnostic helper functions and classes for generating content with codenode.
This module is installed automatically alongside codenode.
pip install 0xf0f-codenode
pip install git+https://github.com/0xf0f/codenode
- codenode_utilities.PartitionedNode
- codenode_utilities.joined
- codenode_utilities.node_transformer
- codenode_utilities.prefixer
- codenode_utilities.suffixer
- codenode_utilities.auto_coerce_patch
class PartitionedNode: ...A node with three separate sections: a header, an indented body and a footer.
Keeps track of child nodes using a list, which is yielded as the default body.
Has convenience methods for adding children and dumping output using the default Writer type.
class PartitionedNode: def header(self) -> 'Iterable': ...Starting section of node.
class PartitionedNode: def body(self) -> 'Iterable': ...Middle section of node. Yields children by default.
class PartitionedNode: def footer(self) -> 'Iterable': ...Ending section of node.
class PartitionedNode: def add_child(self, node: 'T') -> 'T': ...Add a node to this node's children.
node: Node to add.
Added node.
class PartitionedNode: def add_children(self, nodes: typing.Iterable[T]) -> typing.Iterable[T]: ...Add multiple nodes to this node's children.
nodes: Nodes to add.
The added nodes
class PartitionedNode: def dump(self, stream, *, indentation=' ', newline='\n', depth=0, debug=False): ...Process and write out this node to a stream.
stream: An object with a 'write' method.
indentation: String used for indents in the output.
newline: String used for newlines in the output.
depth: Base depth (i.e. number of indents) to start at.
debug: If True, will print out extra info when an error occurs to give a better idea of which node caused it.
class PartitionedNode: def dumps(self, *, indentation=' ', newline='\n', depth=0, debug=False): ...Process and write out this node as a string.
indentation: String used for indents in the output.
newline: String used for newlines in the output.
depth: Base depth (i.e. number of indents) to start at.
debug: If True, will print out extra info when an error occurs to give a better idea of which node caused it.
String representation of node.
children: Node in the body section.
def joined(nodes, *, start='', separator=newline, end=newline): ...yields a starting node, then a sequence of nodes with a separator between each one, then an ending node
nodes: sequence of nodes in the middle
start: node at the start
separator: node repeated between middle nodes
end: ending node
iterable consisting of starting node, middle nodes with separators, and an ending node
def node_transformer(func): ...decorator for creating functions that are used to recursively transform node trees.
func: transformer function applied to each node
a function which recursively applies the transformer function to each node in the tree.
def prefixer(prefix: str): ...Returns a node transformer that adds a string to the start of every line in the output of a node.
prefix: String to place at the start of lines.
A function that takes a node as an argument, along with a function to convert a node to a string (i.e. codenode.dumps). It calls this function with the given node, then returns new nodes containing each line in the string along with the prefix at the start.
def suffixer(suffix: str, dumps=lambda node: codenode.dumps(node)): ...Returns a node transformer that adds a string to the end of every line in the output of a node. Padding is added automatically to align all suffixes to the end of the longest line.
suffix: String to place at the end of lines.
A function that takes a node as an argument, along with a function to convert a node to a string (i.e. codenode.dumps). It calls this function with the given node, then returns new nodes containing each line in the string along with the suffix at the end.
def auto_coerce_patch(writer_type: T, coerce=str) -> T: ...Returns an altered version of a writer type that will automatically convert unprocessable nodes to another type.
writer_type: Base writer type.
coerce: Callable used to convert nodes. str by default.
Descendant writer type that will convert unprocessable nodes.