From c499c2108fd86a4ed696d4e02143f3b725bc8a22 Mon Sep 17 00:00:00 2001 From: Timotej Lazar Date: Mon, 31 Aug 2015 15:53:16 +0200 Subject: Add a Python utility module --- python/util.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 python/util.py (limited to 'python') diff --git a/python/util.py b/python/util.py new file mode 100644 index 0000000..3101edf --- /dev/null +++ b/python/util.py @@ -0,0 +1,17 @@ +#!/usr/bin/python + +import io +from tokenize import tokenize + +# Check if code contains a sequence of tokens (given as a list of strings). +def has_token_sequence(code, sequence): + stream = io.BytesIO(code.encode('utf-8')) + tokens = [t.string for t in tokenize(stream.readline) if t.string] + for i in range(len(tokens)-len(sequence)+1): + if tokens[i:i+len(sequence)] == sequence: + return True + return False + +if __name__ == '__main__': + print(has_token_sequence('x + y >= 0', ['>=', '0'])) + print(has_token_sequence('x + y > 0', ['>=', '0'])) -- cgit v1.2.1