diff options
author | Martin <martin@leo.fri1.uni-lj.si> | 2015-09-15 12:13:46 +0200 |
---|---|---|
committer | Martin <martin@leo.fri1.uni-lj.si> | 2015-09-15 12:13:46 +0200 |
commit | b2b4e40e5333b3182d0a57052640e8a7ecae8619 (patch) | |
tree | ef1d83e04ba376000e333957dffa714aeacb69f5 | |
parent | a49a44fc4b06f2675de3a4f7cbc9ac4e477f1c28 (diff) |
Added utility functions to util.py
-rw-r--r-- | python/util.py | 22 |
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'])) |