S-expression Library for Python is a library for Python to parse, read and manipulate S-expression data structures such as lisp programs, lisp data, netlists like EDIF and KiCAD, etc.
This library was tested with 0644.edf EDIF file (12MB).
>>> from sxprlib import *
>>> x = sxparse("(prices (apple 3.6) (orange 2.5))")
>>> x
Cons(Symbol('prices'), Cons(Cons(Symbol('apple'), Cons(3.6, NIL)), Cons(Cons(Symbol('orange'), Cons(2.5, NIL)), NIL)))
>>> print(x)
(prices (apple 3.6) (orange 2.5))
>>> sxpprint(x)
(prices
(apple 3.6)
(orange 2.5))
>>> with sxopen('sxsample.txt') as f:
... for s in f:
... sxpprint(s)
...
(prices
(apple 3.6)
(orange 2.5)
(banana 1.4))
(netlist
(netname "VCC"
(node 1
(ref "U1")
(pin 1))
(node 2
(ref "R1")
(pin 2)))
(netname "GND"
(node 3
(ref "U1")
(pin 4))
(node 4
(ref "R1")
(pin 1))))
(a b c d)
>>> print(x)
(prices (apple 3.6) (orange 2.5))
>>> print(car(x))
prices
>>> print(cdr(x))
((apple 3.6) (orange 2.5))
>>> print(x[2])
(orange 2.5)
>>> for v in x:
... print(v)
...
prices
(apple 3.6)
(orange 2.5)
>>> x
Cons(Symbol('prices'), Cons(Cons(Symbol('apple'), Cons(3.6, NIL)), Cons(Cons(Symbol('orange'), Cons(2.5, NIL)), NIL)))
>>> print(x)
(prices (apple 3.6) (orange 2.5))
>>> l = sx2py(x)
>>> l
['prices', ['apple', 3.6], ['orange', 2.5]]
>>> y = py2sx(l)
>>> y
Cons(Symbol('prices'), Cons(Cons(Symbol('apple'), Cons(3.6, NIL)), Cons(Cons(Symbol('orange'), Cons(2.5, NIL)), NIL)))
>>> z = Cons(Symbol("abc"), 2)
>>> print(z)
(abc . 2)
>>> z.cdr = Cons(3, NIL)
>>> print(z)
(abc 3)
>>> w = mklist(10, z, String("xyz"))
>>> print(w)
(10 (abc 3) "xyz")
S-expression Library for Python provides the following features:
- S-expression Primitives
- Text String Parser (sxparse function and SxprStringReader class)
- File Reader (sxopen function and SxprFileReader class)
- Standard Lisp Predicates such as null, consp, listp, atom, etc.
- Standard Lisp Functions such as car, cdr, cons, append, etc.
- Utilities such as Converters (py2sx, sx2py) and Pretty-Print (sxpprint)
- Option Switches to change parser behaviors
- Python 3
- Place "sxprlib.py" and "ratcomplex.py" in the same directory where your program source is there.
- Import "sxprlib.py" in your python code. Please see API Reference for details.
- Kitanokitsune
This library is released under the MIT license.
Copyright (c) 2023 Kitanokitsune
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.