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

Python type-string parser. The code evolved from a post in python-list on 11/22/05 by Fredrik Lundh on a dictionary parser. It parses a type-string to their type objects for all basic types. Raises SyntaxError and SemanticError on failures.

Supported types: * containers: defaultdict, deque, dict, list, tuple, set * basic types: Decimal, bool, float, int, long, str * None type

REQUIRES PYTHON >= 2.5

Python, 209 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
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
import cStringIO
import tokenize

from collections import defaultdict, deque
from decimal import Decimal
from functools import partial

__version__ = "1.1"
__author__ = "Florian Leitner"

__all__ = ["parse", "parseOne"]

# For defining the default type for the defaultdict as to not use eval(type):
# TODO: better method?
TYPES = {
    "int": int,
    "long": long,
    "float": float,
    "Decimal": Decimal,
    "bool": bool,
    "str": str,
    "list": list,
    "tuple": tuple,
    "set": set,
    "deque": deque,
    "dict": dict,
    "defaultdict": defaultdict,
    "None": None
}

def _error(tok, msg):
    raise SyntaxError("%s token(%s, '%s') at %s = from/to [row, col]"
                      % (msg, tokenize.tok_name[tok[0]],
                         tok[1], str(tok[2:4])))

_fail = partial(_error, msg="malformed expression")

def _parse(token, src):
    try:
        return _dispatcher[token[0]](token, src)
    except KeyError:
        _error(token, "unknown token type")

def _parseBool(token, src):
    # bool
    return token[1] == "True"

def _parseDecimal(token, src):
    # Decimal("number")
    _tests(src, (('(', 'open'),), "malformed Decimal")
    token = src.next()
    if token[0] != tokenize.STRING:
        _error(token, "malformed Decimal string")
    out = Decimal(token[1][1:-1])
    _tests(src, ((')', 'close'),), "malformed Decimal")
    return out

def _parseDefaultdict(token, src):
    # defaultdict(<type 'type'>, {...})
    tests = (('(', 'open'), ('<', 'typeopen'), ('type', 'typestring'))
    _tests(src, tests, "malformed defaultdict")
    token = src.next()
    try:
        dicttype = TYPES[token[1][1:-1]]
    except KeyError:
        _error(token, "unknown defaultdict type")
    tests = (('>', 'typeclose'), (',', 'typesep'))
    _tests(src, tests, "malformed defaultdict")
    dd = _parse(src.next(), src)
    if type(dd) != dict:
        _error(src.next(), "malformed defaultdict dict '%s' - next" % str(dd))
    out = defaultdict(dicttype, dd)
    _tests(src, ((')', 'close'),), "malformed defaultdict")
    return out

def _parseName(token, src):
    try:
        return _dispatchName[token[1]](token, src)
    except KeyError:
        _fail(token)

def _parseNone(token, src):
    # None
    return None

def _parseNumber(token, src):
    # int, float, long
    try:
        if token[1][-1] == "L":
            return long(token[1], 0)
        return int(token[1], 0) 
    except ValueError:
        return float(token[1])

def _parseOp(token, src):
    if token[1] == "{":
        # {dict}
        out = {}
        token = src.next()
        while token[1] != "}":
            key = _parse(token, src)
            _tests(src, ((':', 'separator'),), "malformed dictionary")
            value = _parse(src.next(), src)
            out[key] = value
            token = src.next()
            if token[1] == ",":
                token = src.next()
        return out
    elif token[1] in ("[", "("):
        # [list], (tuple)
        container = list if token[1] == "[" else tuple
        out = []
        token = src.next()
        while token[0] != tokenize.OP and token[1] not in ("]", ")"):
            out.append(_parse(token, src))
            token = src.next()
            if token[1] == ",":
                token = src.next()
        return container(out)
    else:
        _fail(token)

def _parsePass(token, src):
    # just continue...
    return _parse(src.next(), src)

def _parseSet(token, src):
    # set([...]), deque([...])
    container = set if token[1] == "set" else deque
    name = token[1]
    _tests(src, (('(', 'open'),), "malformed %s" % name)
    lst = _parse(src.next(), src)
    if type(lst) != list:
        _error(src.next(), "malformed %s list '%s' - next" % (name, str(lst)))
    _tests(src, ((')', 'close'),), "malformed %s" % name)
    return container(lst)

def _parseString(token, src):
    # str
    return token[1][1:-1].decode("string-escape")

def _tests(src, tests, base_msg):
    for sym, msg in tests:
        token = src.next()
        if token[1] != sym:
            _error(token, "%s %s" % (base_msg, msg))
    return


def _tokenize(source):
    src = cStringIO.StringIO(source).readline
    return tokenize.generate_tokens(src)

_dispatcher = {
    tokenize.ENDMARKER: _parsePass,
    tokenize.INDENT: _parsePass,
    tokenize.NAME: _parseName,
    tokenize.NL: _parsePass,
    tokenize.NEWLINE: _parsePass,
    tokenize.NUMBER: _parseNumber,
    tokenize.OP: _parseOp,
    tokenize.STRING: _parseString,
}

_dispatchName = {
    "set": _parseSet,
    "deque": _parseSet,
    "defaultdict": _parseDefaultdict,
    "Decimal": _parseDecimal,
    "None": _parseNone,
    "True": _parseBool,
    "False": _parseBool,
}

def parse(source):
    """ Parses a type string to their type objects for all basic types.
        
        Yields [nested] type objects.
        
        Raises SyntaxError and SemanticError on failures.
        
        Supported types:
        * containers: defaultdict, deque, dict, list, tuple, set
        * basic types: Decimal, bool, float, int, long, str
        * None type
    """
    src = _tokenize(source)
    for token in src:
        yield _parse(token, src)

def parseOne(source):
    """ Parses only the next type object from source and returns it together
        with the remaining [possibly empty] string as an object, string tuple.
    """
    src = _tokenize(source)
    obj = _parse(src.next(), src)
    try:
        next = src.next()
    except StopIteration:
        rest = ""
    else:
        rest = source.split('\n')[next[2][0] - 1:]
        if len(rest) == 0:
            rest = ""
        else:
            rest[0] = rest[0][next[2][1]:]
            rest = '\n'.join(rest)
    finally:
        return obj, rest

The question is, naturally, why do you (we) need this? Well, I do not like eval(source) as the alternative solution to parse default __repr__ strings of python data types as much, it can be kind of dangerous when you do not know where the string comes from. This little module allows parsing of arbitrary typestrings without having to fear "remote code execution".

Example usage:

typestring = '\nDecimal("1") True\n1.1\t123L "string" None defaultdict(, {(1,2):\t[3,4]})\ndeque([1])' for obj in parse(typestring): # do something with obj...

Version 1.1: added support to return the remaining string when using parseOne and another dispatcher for the names so to have not so many ugly if - elif - else statements and a slightly faster code;