File: build_expressions.py

package info (click to toggle)
python-astor 0.8.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 256 kB
  • sloc: python: 2,348; makefile: 4
file content (241 lines) | stat: -rwxr-xr-x 7,586 bytes parent folder | download | duplicates (2)
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""
Part of the astor library for Python AST manipulation.

License: 3-clause BSD

Copyright (c) 2015 Patrick Maupin

This module generates a lot of permutations of Python
expressions, and dumps them into a python module
all_expr_x_y.py (where x and y are the python version tuple)
as a string.

This string is later used by check_expressions.

This module takes a loooooooooong time to execute.

"""

import sys
import collections
import itertools
import textwrap
import ast
import astor

all_operators = (
            # Selected special operands
            '3 -3 () yield',
            # operators with one parameter
            'yield lambda_: not + - ~ $, yield_from',
            # operators with two parameters
            'or and == != > >= < <= in not_in is is_not '
            '| ^ & << >> + - * / % // @ ** for$in$ $($) $[$] . '
            '$,$ ',
            # operators with 3 parameters
            '$if$else$ $for$in$'
        )


select_operators = (
            # Selected special operands -- remove
            # some at redundant precedence levels
            '-3',
            # operators with one parameter
            'yield lambda_: not - ~ $,',
            # operators with two parameters
            'or and == in is '
            '| ^ & >> - % ** for$in$ $($) . ',
            # operators with 3 parameters
            '$if$else$  $for$in$'
        )


def get_primitives(base):
    """Attempt to return formatting strings for all operators,
       and selected operands.
       Here, I use the term operator loosely to describe anything
       that accepts an expression and can be used in an additional
       expression.
    """

    operands = []
    operators = []
    for nparams, s in enumerate(base):
        s = s.replace('%', '%%').split()
        for s in (x.replace('_', ' ') for x in s):
            if nparams and '$' not in s:
                assert nparams in (1, 2)
                s = '%s%s$' % ('$' if nparams == 2 else '', s)
            assert nparams == s.count('$'), (nparams, s)
            s = s.replace('$', ' %s ').strip()

            # Normalize the spacing
            s = s.replace(' ,', ',')
            s = s.replace(' . ', '.')
            s = s.replace(' [ ', '[').replace(' ]', ']')
            s = s.replace(' ( ', '(').replace(' )', ')')
            if nparams == 1:
                s = s.replace('+ ', '+')
                s = s.replace('- ', '-')
                s = s.replace('~ ', '~')

            if nparams:
                operators.append((s, nparams))
            else:
                operands.append(s)
    return operators, operands


def get_sub_combinations(maxop):
    """Return a dictionary of lists of combinations suitable
       for recursively building expressions.

       Each dictionary key is a tuple of (numops, numoperands),
       where:

            numops is the number of operators we
            should build an expression for

            numterms is the number of operands required
            by the current operator.

        Each list contains all permutations of the number
        of operators that the recursively called function
        should use for each operand.
    """
    combo = collections.defaultdict(list)
    for numops in range(maxop+1):
        if numops:
            combo[numops, 1].append((numops-1,))
        for op1 in range(numops):
            combo[numops, 2].append((op1, numops - op1 - 1))
            for op2 in range(numops - op1):
                combo[numops, 3].append((op1, op2, numops - op1 - op2 - 1))
    return combo


def get_paren_combos():
    """This function returns a list of lists.
       The first list is indexed by the number of operands
       the current operator has.

       Each sublist contains all permutations of wrapping
       the operands in parentheses or not.
    """
    results = [None] * 4
    options = [('%s', '(%s)')]
    for i in range(1, 4):
        results[i] = list(itertools.product(*(i * options)))
    return results


def operand_combo(expressions, operands, max_operand=13):
    op_combos = []
    operands = list(operands)
    operands.append('%s')
    for n in range(max_operand):
        this_combo = []
        op_combos.append(this_combo)
        for i in range(n):
            for op in operands:
                mylist = ['%s'] * n
                mylist[i] = op
                this_combo.append(tuple(mylist))
    for expr in expressions:
        expr = expr.replace('%%', '%%%%')
        for op in op_combos[expr.count('%s')]:
            yield expr % op


def build(numops=2, all_operators=all_operators, use_operands=False,
          # Runtime optimization
          tuple=tuple):
    operators, operands = get_primitives(all_operators)
    combo = get_sub_combinations(numops)
    paren_combos = get_paren_combos()
    product = itertools.product
    try:
        izip = itertools.izip
    except AttributeError:
        izip = zip

    def recurse_build(numops):
        if not numops:
            yield '%s'
        for myop, nparams in operators:
            myop = myop.replace('%%', '%%%%')
            myparens = paren_combos[nparams]
            # print combo[numops, nparams]
            for mycombo in combo[numops, nparams]:
                # print mycombo
                call_again = (recurse_build(x) for x in mycombo)
                for subexpr in product(*call_again):
                    for parens in myparens:
                        wrapped = tuple(x % y for (x, y)
                                        in izip(parens, subexpr))
                        yield myop % wrapped
    result = recurse_build(numops)
    return operand_combo(result, operands) if use_operands else result


def makelib():
    parse = ast.parse
    dump_tree = astor.dump_tree

    def default_value(): return 1000000, ''
    mydict = collections.defaultdict(default_value)

    allparams = [tuple('abcdefghijklmnop'[:x]) for x in range(13)]
    alltxt = itertools.chain(build(1, use_operands=True),
                             build(2, use_operands=True),
                             build(3, select_operators))

    yieldrepl = list(('yield %s %s' % (operator, operand),
                      'yield %s%s' % (operator, operand))
                     for operator in '+-' for operand in '(ab')
    yieldrepl.append(('yield[', 'yield ['))
    # alltxt = itertools.chain(build(1), build(2))
    badexpr = 0
    goodexpr = 0
    silly = '3( 3.( 3[ 3.['.split()
    for expr in alltxt:
        params = allparams[expr.count('%s')]
        expr %= params
        try:
            myast = parse(expr)
        except:
            badexpr += 1
            continue
        goodexpr += 1
        key = dump_tree(myast)
        expr = expr.replace(', - ', ', -')
        ignore = [x for x in silly if x in expr]
        if ignore:
            continue
        if 'yield' in expr:
            for x in yieldrepl:
                expr = expr.replace(*x)
        mydict[key] = min(mydict[key], (len(expr), expr))
    print(badexpr, goodexpr)

    stuff = [x[1] for x in mydict.values()]
    stuff.sort()

    lineend = '\n'.encode('utf-8')
    with open('all_expr_%s_%s.py' % sys.version_info[:2], 'wb') as f:
        f.write(textwrap.dedent('''
            # AUTOMAGICALLY GENERATED!!!  DO NOT MODIFY!!
            #
            all_expr = """
            ''').encode('utf-8'))
        for item in stuff:
            f.write(item.encode('utf-8'))
            f.write(lineend)
        f.write('"""\n'.encode('utf-8'))


if __name__ == '__main__':
    makelib()