From b2b4e40e5333b3182d0a57052640e8a7ecae8619 Mon Sep 17 00:00:00 2001 From: Martin Date: Tue, 15 Sep 2015 12:13:46 +0200 Subject: Added utility functions to util.py --- python/util.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'python') 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 prec 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'])) -- cgit v1.2.1