summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorAleš Smodiš <aless@guru.si>2015-09-15 12:19:59 +0200
committerAleš Smodiš <aless@guru.si>2015-09-15 12:19:59 +0200
commite49caeec12b36742044d1e306de382f54995576c (patch)
tree6c53a5f73b1285b253627ed5b3fa58eddb11ffb1 /python
parentfc26c19e57faa7b22e76954e1d5243c82374125e (diff)
parent9df933ebc3ade50628a26af691693254ed1daa22 (diff)
Merge branch 'master' of ssh://212.235.189.51:22122/codeq-server
Diffstat (limited to 'python')
-rw-r--r--python/util.py22
1 files changed, 22 insertions, 0 deletions
diff --git a/python/util.py b/python/util.py
index 3101edf..390b831 100644
--- a/python/util.py
+++ b/python/util.py
@@ -12,6 +12,28 @@ def has_token_sequence(code, sequence):
return True
return False
+def almost_equal(a, b, prec=1):
+ """ Compares values a and b using at most <code>prec</code> decimal values. """
+ return int(a*10**prec) == int(b*10**prec)
+
+def string_almost_equal(s, a, prec=1):
+ """ Searches string s for a value that is almost equal to a.
+
+ Args:
+ s (str): string to search
+ a (float): value to find in string
+
+ Returns:
+ bool: True if successful, else False
+ """
+ for v in s.split():
+ try:
+ if almost_equal(float(v), a, prec):
+ return True
+ except:
+ pass
+ return False
+
if __name__ == '__main__':
print(has_token_sequence('x + y >= 0', ['>=', '0']))
print(has_token_sequence('x + y > 0', ['>=', '0']))