sourcery_analytics.extractors¶
Extract nodes from various sources according to conditions.
- sourcery_analytics.extractors.extract_methods(item: Union[str, NodeNG, Path], /) Iterator[FunctionDef] ¶
Extracts methods from the input.
- Parameters
item – source code, node, or path to file or directory
- Returns
An iterable of all the function definition nodes in the item
>>> [method.name for method in extract_methods('''def foo(): pass''')] ['foo']
- sourcery_analytics.extractors.extract(item: Union[str, NodeNG, Path], /, condition: Optional[Callable[[NodeNG], bool]] = None, function: Optional[Callable[[NodeNG], Optional[E]]] = None) Iterator[E] ¶
Extracts from
item
according tocondition
ORfunction
.- Parameters
item – source code, node, or path to file or directory
condition – a condition on a node
function – a function over a node, returning None for unextracted nodes
- Returns
If
condition
is specified, an iterable of nodes satisfying the condition. Iffunction
is specified, an iterable of not-None results of the function.- Raises
ValueError – if both function and condition are specified
Examples
>>> from sourcery_analytics.conditions import is_name >>> [ ... name.name ... for name in extract('''def add(x, y): x + y''', condition=is_name) ... ] ['x', 'y'] >>> list( ... extract( ... '''def add(x, y): x + y''', ... function=lambda node: node.name if hasattr(node, "name") else None, ... ) ... ) ['add', 'x', 'y', 'x', 'y']
- class sourcery_analytics.extractors.Extractor(visitor: ~sourcery_analytics.visitors.Visitor[~typing.Optional[~sourcery_analytics.extractors.E]] = <sourcery_analytics.visitors.IdentityVisitor object>, manager: ~astroid.manager.AstroidManager = <astroid.manager.AstroidManager object>)¶
Bases:
Generic
[E
]Extracts results by walking a tree with the provided visitor.
The visitor should return either an object of type
E
(for instance a node) or None. Theextract
method will return only the not-None values.By default, the extractor will return every node in the tree.
Examples
>>> source = ''' ... def one(): ... return 1 ... def two(): ... return 2 ... ''' >>> from sourcery_analytics.conditions import is_method, is_const >>> methods = Extractor.from_condition(is_method).extract(source) >>> list(m.name for m in methods) ['one', 'two'] >>> const_value_extractor = Extractor.from_function( ... lambda node: node.value if is_const(node) else None ... ) >>> const_values = const_value_extractor.extract(source) >>> list(const_values) [1, 2]
- manager: AstroidManager = <astroid.manager.AstroidManager object>¶
- classmethod from_condition(condition: Callable[[NodeNG], bool], sub_visitor: Visitor[E] = IdentityVisitor()) Extractor[E] ¶
Construct a node extractor from a condition on a node.