summaryrefslogtreecommitdiff
path: root/python/util.py
blob: 8c1a0c7564515a6de53f57039115b6a7e70c3e4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/python

import io
import re
from tokenize import tokenize, TokenError

def get_tokens(code):
    """ Gets a list of tokens. """
    try:
        stream = io.BytesIO(code.encode('utf-8'))
        return [t.string for t in tokenize(stream.readline) if t.string]
    except TokenError:
        return []

# Check if tokens contain a sequence of tokens (given as a list of strings).
def has_token_sequence(tokens, sequence):
    for i in range(len(tokens)-len(sequence)+1):
        if tokens[i:i+len(sequence)] == sequence:
            return True
    return False

def almost_equal(a, b, prec):
    """ Compares values a and b 
    using prec if value < 1 or 
    prec most significant numbers otherwise. 
    """
    return abs(a-b) <= max(abs(a), abs(b), 1) * 10**(-prec)

def get_numbers(s):
    """ Extracts numbers from string s. """
    str_vals = re.findall(r'''
        [-+]? # optional sign
        (?:
             (?: \d* \. \d+ ) # .1 .12 .123 etc 9.1 etc 98.1 etc
             |
             (?: \d+ \.? ) # 1. 12. 123. etc 1 12 123 etc
        )
        # followed by optional exponent part if desired
        (?: [Ee] [+-]? \d+ ) ?
        ''', s, re.VERBOSE)
    return [float(v) for v in str_vals]

def string_almost_equal(s, a, prec=3):
    """ Searches string s for a value that is almost equal to a. """
    for v in get_numbers(s):
        if almost_equal(v, a, prec):
                return True
    return False

def string_contains_number(s, a):
    """ Searches string s for a value that is equal to a. """
    return a in get_numbers(s)


def get_exception_desc(exc):
    # if have an exception!
    if exc:
        if 'EOFError' in exc:
            return [{'id':'eof_error'}]
        if 'timed out' in exc:
            return [{'id':'timed_out'}]
        if 'sandbox violation' in exc:
            return [{'id': 'sandbox_violation'}]
        if 'NameError' in exc:
            return [{'id':'name_error', 'args': {'message': exc}}]
        elif 'TypeError' in exc:
            return [{'id':'type_error', 'args': {'message': exc}}]
        else:
            return [{'id':'error', 'args': {'message': exc}}]
    return None

if __name__ == '__main__':
    print(has_token_sequence(get_tokens('x + y >= 0'), ['>=', '0']))
    print(has_token_sequence(get_tokens('x + y > 0'), ['>=', '0']))