summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorTimotej Lazar <timotej.lazar@araneo.org>2015-08-31 15:53:16 +0200
committerTimotej Lazar <timotej.lazar@araneo.org>2015-08-31 15:53:16 +0200
commitc499c2108fd86a4ed696d4e02143f3b725bc8a22 (patch)
treea0bec091f4d5c87f97f9263067e640598f95774d /python
parent9d4350983336a837c2fc65f377f3527d4714858e (diff)
Add a Python utility module
Diffstat (limited to 'python')
-rw-r--r--python/util.py17
1 files changed, 17 insertions, 0 deletions
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']))