Welcome, guest | Sign In | My Account | Store | Cart

For those of you wondering how use "utility_mill" (my previous recipe), check out "hard_test()" and "soft_test()" in this example usage. The code in the lower sections generates test data and is only useful if trying to understand the "expressions" recipe initially ported from C#. The hard test shows how easy it is to execute a utility that you know the name of. The most recent version is used, but this can be inefficient. The soft test runs the same utility once a minute but only updates the version once an hour. This can saves the Utility Mill some extra work that may not be necessary.

Python, 182 lines
  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
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import expressions
import utility_mill
from test import support

################################################################################

def hard_test():
    while True:
        test = generate_test()
        with support.captured_stdout() as out:
            try:
                expressions.run(test, {})
            except Exception as err:
                print(err.args[0])
        val = out.getvalue()[:-1]
        try:
            out = utility_mill.run_latest('Expression_Evaluator', INPUT=test)
        except:
            pass
        else:
            print('PASS' if val == out else 'FAIL')

################################################################################

def soft_test():
    # test once a minute (softer)
    import time
    # we use some smart variables
    FAIL = 5
    NAME = 'Expression_Evaluator'
    # enter the main program code
    while True:
        version = get_version(FAIL, NAME)
        for minute in range(60):
            test = generate_test()
            with support.captured_stdout() as out:
                try:
                    expressions.run(test, {})
                except Exception as err:
                    print(err.args[0])
            val = out.getvalue()[:-1]
            out = get_results(FAIL, NAME, version, INPUT=test)
            print('PASS' if val == out else 'FAIL')
            time.sleep(60)

def get_version(fail, name):
    for attempt in range(fail):
        try:
            return utility_mill.get_version(name)
        except:
            pass
    raise SystemExit(1)

def get_results(fail, name, version, **query):
    for attempt in range(fail):
        try:
            return utility_mill.get_results(name, version, query)
        except:
            pass
    raise SystemExit(2)

################################################################################

import random
SR = random.SystemRandom()
LIB = '!"#$%&\'()*+,-./0123456789:<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~'
OP = '=', '+', '-', '*', '/', '//', '\\', '%', '**', '^', 'and', '&&', '&', 'or', '||', '|', '==', '!=', '>', '<', '>=', '=>', '<=', '=<'
MAX = 5

################################################################################

def generate_test():
    variables = set()
    required_lines = SR.randint(0, MAX)
    lines = []
    while len(lines) != required_lines:
        create_line(lines, variables)
    return '\n'.join(lines)

def create_line(lines, variables):
    required_expressions = SR.randint(0, MAX)
    expressions = []
    while len(expressions) != required_expressions:
        create_expression(expressions, variables)
    lines.append(';'.join(expressions))

def create_expression(expressions, variables):
    choice = SR.choice(['assign', 'comment', 'empty', 'print'])
    if choice == 'assign':
        tokens = create_assign(variables)
        variables.add('_')
    elif choice == 'comment':
        tokens = create_comment()
    elif choice == 'empty':
        tokens = []
    else:
        tokens = pad(create_evaluation(variables))
        variables.add('_')
    expressions.append(''.join(tokens))

def create_assign(variables):
    required_variables = SR.randint(1, MAX)
    new_variables = []
    while len(new_variables) != required_variables:
        create_variable(new_variables)
    tokens = []
    for variable in new_variables:
        tokens.append(variable)
        tokens.append('=')
    evaluation = create_evaluation(variables)
    variables.update(new_variables)
    tokens.extend(evaluation)
    padded_tokens = pad(tokens)
    return padded_tokens

def create_variable(new_variables):
    required_length = SR.randint(1, MAX)
    variable = ''
    while not valid_variable(variable):
        variable = []
        while len(variable) != required_length:
            variable.append(SR.choice(LIB))
        variable = ''.join(variable)
    new_variables.append(variable)

def valid_variable(variable):
    if not variable:
        return False
    if variable[0] == '#':
        return False
    try:
        float(variable)
        return False
    except:
        pass
    if variable in OP:
        return False
    return True

def create_evaluation(variables):
    required_tokens = SR.randint(1, MAX)
    tokens = []
    while len(tokens) != required_tokens:
        if SR.randint(0, 1):
            tokens.append(repr(SR.randint(1, 10)))
        elif variables:
            tokens.append(SR.choice(list(variables)))
    token_list = []
    for token in tokens:
        token_list.append(token)
        token_list.append(SR.choice(OP[1:]))
    return token_list[:-1]

def pad(tokens):
    token_list = []
    for token in tokens:
        token_list.append(token)
        token_list.append(create_whitespace())
    return token_list[:-1]

def create_whitespace():
    required_length = SR.randint(1, MAX)
    whitespace = []
    while len(whitespace) != required_length:
        if SR.randint(0, 1):
            whitespace.append(' ')
        else:
            whitespace.append('\t')
    return ''.join(whitespace)

def create_comment():
    required_length = SR.randint(1, MAX)
    tokens = ['#']
    while len(tokens) != required_length:
        create_variable(tokens)
    padded_tokens = pad(tokens)
    return padded_tokens

################################################################################
    
if __name__ == '__main__':
    soft_test()