summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
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']))