def extract_node(code, module_name=''):
 def _extract(node):
  if isinstance(node, nodes.Discard):
   return node.value
  else:
   return node
 requested_lines = []
 for (idx, line) in enumerate(code.splitlines()):
  if line.strip().endswith(_STATEMENT_SELECTOR):
   requested_lines.append((idx + 1))
 tree = build_module(code, module_name=module_name)
 extracted = []
 if requested_lines:
  for line in requested_lines:
   extracted.append(_find_statement_by_line(tree, line))
 extracted.extend(_extract_expressions(tree))
 if (not extracted):
  extracted.append(tree.body[(-1)])
 extracted = [_extract(node) for node in extracted]
 if (len(extracted) == 1):
  return extracted[0]
 else:
  return extracted

def extract_node ( code , module_name = '' ) : def _extract ( node ) : if isinstance ( node , nodes . Discard ) : return node . value else : return node requested_lines = [ ] for ( idx , line ) in enumerate ( code . splitlines ( ) ) : if line . strip ( ) . endswith ( _STATEMENT_SELECTOR ) : requested_lines . append ( ( idx + 1 ) ) tree = build_module ( code , module_name = module_name ) extracted = [ ] if requested_lines : for line in requested_lines : extracted . append ( _find_statement_by_line ( tree , line ) ) extracted . extend ( _extract_expressions ( tree ) ) if ( not extracted ) : extracted . append ( tree . body [ ( - 1 ) ] ) extracted = [ _extract ( node ) for node in extracted ] if ( len ( extracted ) == 1 ) : return extracted [ 0 ] else : return extracted
parses some python code as a module and extracts a designated ast node .