Skip to content
Snippets Groups Projects
Commit dbb9bc95 authored by bey's avatar bey
Browse files

Add at method to find item on specific line. Add test for at method. Update TODO list.

parent 497a55f5
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -2,7 +2,6 @@
 
### Important
 
- raise an exception on .find/.find_all if the identifier given doesn't exists
- .help() seems really slow on big piece of code (for example RedBaron("baron/grammator.py").read())("dict")[0].help() is suuuuuuuuuuuuuuuuper slow)
- .at() return the first item starting at line X
- .rename() (name -> value, def/class -> name)
Loading
Loading
Loading
Loading
@@ -212,6 +212,16 @@ class GenericNodesUtils(object):
else:
return None
 
def at(self, line_no):
if line_no > self.absolute_bounding_box.bottom_right.line or \
line_no < self.absolute_bounding_box.top_left.line:
raise IndexError()
path = Path.from_baron_path(self, baron.path.position_to_path(self.fst(), (line_no, 1)))
node = path.node if path else None
if node is not None and hasattr(node, 'type') and node.type == 'endl':
return node.next
return node
def _string_to_node_list(self, string, parent, on_attribute):
return NodeList.from_fst(baron.parse(string), parent=parent, on_attribute=on_attribute)
 
Loading
Loading
@@ -885,6 +895,7 @@ class Node(GenericNodesUtils):
'has_render_key',
'get_absolute_bounding_box_of_attribute',
'find_by_position',
'at',
'parse_code_block',
'parse_decorators',
'from_fst',
Loading
Loading
# -*- coding:Utf-8 -*-
"""Test for at method"""
import pytest
import redbaron
from redbaron import RedBaron, DecoratorNode, AssignmentNode, DefNode, EndlNode
redbaron.DEBUG = True
fst = RedBaron("""\
@deco
def foo(a, b):
c = a + b
e = 1
""")
def test_at():
assert isinstance(fst.at(1), DecoratorNode) is True
assert isinstance(fst.at(2), RedBaron) is True
assert isinstance(fst.at(3), DefNode) is True
assert isinstance(fst.at(4), AssignmentNode) is True
assert isinstance(fst.at(5), AssignmentNode) is True
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment