Show More
@@ -95,8 +95,8 b' def extract_symbols(code, symbols):' | |||
|
95 | 95 | |
|
96 | 96 | class A: pass''' |
|
97 | 97 | |
|
98 | >>> extract_symbols(code, 'A,b') | |
|
99 | (["class A: pass", "def b(): return 42"], []) | |
|
98 | >>> extract_symbols(code, 'A,b,z') | |
|
99 | (["class A: pass", "def b(): return 42"], ['z']) | |
|
100 | 100 | """ |
|
101 | 101 | try: |
|
102 | 102 | py_code = ast.parse(code) |
@@ -107,10 +107,12 b' def extract_symbols(code, symbols):' | |||
|
107 | 107 | marks = [(getattr(s, 'name', None), s.lineno) for s in py_code.body] |
|
108 | 108 | code = code.split('\n') |
|
109 | 109 | |
|
110 | # construct a dictionary with elements | |
|
111 | # {'symbol_name': (start_lineno, end_lineno), ...} | |
|
112 | end = len(code) | |
|
113 | 110 | symbols_lines = {} |
|
111 | ||
|
112 | # we already know the start_lineno of each symbol (marks). | |
|
113 | # To find each end_lineno, we traverse in reverse order until each | |
|
114 | # non-blank line | |
|
115 | end = len(code) | |
|
114 | 116 | for name, start in reversed(marks): |
|
115 | 117 | while not code[end - 1].strip(): |
|
116 | 118 | end -= 1 |
@@ -118,7 +120,10 b' def extract_symbols(code, symbols):' | |||
|
118 | 120 | symbols_lines[name] = (start - 1, end) |
|
119 | 121 | end = start - 1 |
|
120 | 122 | |
|
121 | # fill a list with chunks of codes for each symbol | |
|
123 | # Now symbols_lines is a map | |
|
124 | # {'symbol_name': (start_lineno, end_lineno), ...} | |
|
125 | ||
|
126 | # fill a list with chunks of codes for each requested symbol | |
|
122 | 127 | blocks = [] |
|
123 | 128 | not_found = [] |
|
124 | 129 | for symbol in symbols.split(','): |
General Comments 0
You need to be logged in to leave comments.
Login now