sexpdata is a simple S-expression parser/serializer. It has simple load and dump functions like pickle, json or PyYAML module.
>>> from sexpdata import loads, dumps
>>> loads('("a" "b")')
['a', 'b']
>>> print(dumps(['a', 'b']))
("a" "b")
You can install sexpdata from PyPI:
pip install sexpdata
Links:
sexpdata is licensed under the terms of the BSD 2-Clause License. See the source code for more information.
Load object from S-expression stored in filelike.
| Parameters: | filelike – A text stream object. |
|---|
See loads() for valid keyword arguments.
>>> import io
>>> fp = io.StringIO()
>>> sexp = [Symbol('a'), Symbol('b')] # let's dump and load this object
>>> dump(sexp, fp)
>>> _ = fp.seek(0)
>>> load(fp) == sexp
True
Load object from S-expression string.
| Parameters: |
|
|---|
>>> loads("(a b)")
[Symbol('a'), Symbol('b')]
>>> loads("a")
Symbol('a')
>>> loads("(a 'b)")
[Symbol('a'), Quoted(Symbol('b'))]
>>> loads("(a '(b))")
[Symbol('a'), Quoted([Symbol('b')])]
>>> loads('''
... ;; This is a line comment.
... ("a" "b") ; this is also a comment.
... ''')
['a', 'b']
>>> loads('''
... # This is a line comment.
... ("a" "b") # this is also a comment.
... ''', line_comment='#')
['a', 'b']
nil is converted to an empty list by default. You can use keyword argument nil to change what symbol must be interpreted as nil:
>>> loads("nil")
[]
>>> loads("null", nil='null')
[]
>>> loads("nil", nil=None)
Symbol('nil')
t is converted to True by default. You can use keyword argument true to change what symbol must be converted to True.:
>>> loads("t")
True
>>> loads("#t", true='#t')
True
>>> loads("t", true=None)
Symbol('t')
No symbol is converted to False by default. You can use keyword argument false to convert a symbol to False.
>>> loads("#f")
Symbol('#f')
>>> loads("#f", false='#f')
False
>>> loads("nil", false='nil', nil=None)
False
Write obj as an S-expression into given stream filelike.
| Parameters: |
|
|---|
See dumps() for valid keyword arguments.
>>> import io
>>> fp = io.StringIO()
>>> dump([Symbol('a'), Symbol('b')], fp)
>>> print(fp.getvalue())
(a b)
Convert python object into an S-expression.
| Parameters: |
|
|---|
Basic usage:
>>> print(dumps(['a', 'b']))
("a" "b")
>>> print(dumps(['a', 'b'], str_as='symbol'))
(a b)
>>> print(dumps(dict(a=1, b=2)))
(:a 1 :b 2)
>>> print(dumps([None, True, False, ()]))
(() t () ())
>>> print(dumps([None, True, False, ()],
... none_as='null', true_as='#t', false_as='#f'))
(null #t #f ())
>>> print(dumps(('a', 'b')))
("a" "b")
>>> print(dumps(('a', 'b'), tuple_as='array'))
["a" "b"]
More verbose usage:
>>> print(dumps([Symbol('a'), Symbol('b')]))
(a b)
>>> print(dumps(Symbol('a')))
a
>>> print(dumps([Symbol('a'), Quoted(Symbol('b'))]))
(a 'b)
>>> print(dumps([Symbol('a'), Quoted([Symbol('b')])]))
(a '(b))
Alias of obj[0].
>>> car(loads('(a . b)'))
Symbol('a')
>>> car(loads('(a b)'))
Symbol('a')
cdr-like function.
>>> cdr(loads('(a . b)'))
Symbol('b')
>>> cdr(loads('(a b)'))
[Symbol('b')]
>>> cdr(loads('(a . (b))'))
[Symbol('b')]
>>> cdr(loads('(a)'))
[]
>>> cdr(loads('(a . nil)'))
[]