diff options
author | Martin Možina <martin.mozina@fri.uni-lj.si> | 2016-09-27 15:19:27 +0200 |
---|---|---|
committer | Martin Možina <martin.mozina@fri.uni-lj.si> | 2016-09-27 15:19:27 +0200 |
commit | 82b23da6a519d4bd0923ffd2d9ec5c8e6d8f7a2f (patch) | |
tree | c828d4bf57aca00937455c5f1e61b56c2c4a97cf /python | |
parent | d1287f85ce6cca7cd8b63bd5fe0d191e93b8a983 (diff) |
Added python util functions.
Diffstat (limited to 'python')
-rw-r--r-- | python/util.py | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/python/util.py b/python/util.py index 5cde278..9339a8e 100644 --- a/python/util.py +++ b/python/util.py @@ -28,6 +28,13 @@ def get_tokens(code): except TokenError: return [] +def all_tokens(code): + try: + stream = io.BytesIO(code.encode('utf-8')) + return [t for t in tokenize(stream.readline)] + except TokenError: + return [] + # Check if tokens contain a sequence of tokens (given as a list of strings). def has_token_sequence(tokens, sequence): for i in range(len(tokens)-len(sequence)+1): @@ -85,6 +92,22 @@ def get_exception_desc(exc): return [{'id':'error', 'args': {'message': exc}}] return None + +def has_comprehension(code): + """ Searches code for comprehensions and generators. """ + for l in code.split('\n'): + if 'for' in l and ':' not in l: + return True + return False + +def has_loop(code): + """ Searches code for loops (for and while), but skips comprehensions and + generators. """ + for l in code.split('\n'): + if ('for' in l and ':' in l) or ('while' in l and ':' in l): + return True + return False + if __name__ == '__main__': print(has_token_sequence(get_tokens('x + y >= 0'), ['>=', '0'])) print(has_token_sequence(get_tokens('x + y > 0'), ['>=', '0'])) |