added IndentedTextImporter, compatible docstrings, & nose tests#155
Open
LionKimbro wants to merge 4 commits intoc0fec0de:masterfrom
Open
added IndentedTextImporter, compatible docstrings, & nose tests#155LionKimbro wants to merge 4 commits intoc0fec0de:masterfrom
LionKimbro wants to merge 4 commits intoc0fec0de:masterfrom
Conversation
The tests are now shorter, more direct, and do not rely on the presentation system.
|
I would be interested in this. Some features I would want:
Here sample implementations: def from_indented(file, indent=' ', node_factory=anytree.Node):
"""
node_factory receives one argument and should create a node
indent should be " " if exactly 4 spaces are used
"""
# Each line consists of indent and code
pattern = re.compile(rf"^(?P<prefix>({re.escape(indent)})*)(?P<code>.*)")
root = node_factory()
stack = [root]
for line in file:
match = pattern.match(line)
prefix, code = match['prefix'], match['code']
depth = len(prefix) // len(indent)
node = node_factory(code)
node.parent = stack[depth]
# Place node as last item on index depth + 1
del stack[depth + 1:]
stack.append(node)
return root
def to_indented(root, file, indent=" ", depth=0, formatter=str):
"""
formatter is how the node should be displayed, maybe lambda x: x.name is also a good default
formatter should be the inverse of node_factory in the previous function
depth is the beginning level, normally 0 meaning no indent for first depth-level
root is not included in the output
"""
for child in root.children:
file.write(indent * depth + formatter(child) + '\n')
to_indented(child, file, indent=indent, depth=depth + 1, formatter=formatter) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.