eta.util.sexpr
Utilities for handling S-expression lists.
Contains functions for parsing and manipulating S-expressions in Python, which are represented as recursively nested lists, with strings as “symbols”.
Some of this code is borrowed from the following repository: https://github.com/bitbanger/schemas/blob/master/pyschemas/sexpr.py
Functions
Find a substring with a balanced number of parentheses. |
|
Clean S-expressions from a LISP file by removing all commented lines and removing escape characters on symbols. |
|
Clean a string representation of an S-expression by removing newlines and standardizing whitespace. |
|
Compress quoted expressions that were split between multiple words during parsing. |
|
Convert any quoted word lists (i.e., single ' symbol followed by a list of symbols) to a single quoted string. |
|
Convert an S-expression list structure to a string representing a LISP formatted S-expression. |
|
Convert an S-expression list structure to a flattened string containing each of the symbols. |
|
Parse a string containing an S-expression (in LISP form) into a structured list. |
|
Read a list of S-expressions from a LISP file. |
|
Standardize the symbols within an S-expression by mapping to lowercase, unless enclosed in escape symbols. |
|
Write an S-expression to a LISP file. |
- clean_s_expr(s_expr)[source]
Clean a string representation of an S-expression by removing newlines and standardizing whitespace.
- standardize_symbols(s_expr)[source]
Standardize the symbols within an S-expression by mapping to lowercase, unless enclosed in escape symbols.
- convert_quotes(s_expr)[source]
Convert any quoted word lists (i.e., single ‘ symbol followed by a list of symbols) to a single quoted string.
- compress_quotes(s_expr)[source]
Compress quoted expressions that were split between multiple words during parsing.
- parse_s_expr(s_expr)[source]
Parse a string containing an S-expression (in LISP form) into a structured list.
- Parameters:
s_expr (str) – An S-expression in LISP form, e.g.,
(a (b c (d e)) '(f g h))
.- Returns:
A structured S-expression, i.e., a recursively nested list structure with string “symbols” as atoms. e.g.,
['a', ['b', 'c', ['d', 'e']], "f g h"]
- Return type:
s-expr
- list_to_s_expr(lst)[source]
Convert an S-expression list structure to a string representing a LISP formatted S-expression.
- Parameters:
lst (s-expr) – An S-expression in recursively nested list form, e.g.,
['a', ['b', ['c', 'd']], 'e']
.- Returns:
A LISP formatted string representation of the S-expression, e.g.,
(a (b (c d)) e)
.- Return type:
- list_to_str(lst)[source]
Convert an S-expression list structure to a flattened string containing each of the symbols.
- Parameters:
lst (s-expr) – An S-expression in recursively nested list form, e.g.,
['a', ['b', ['c', 'd']], 'e']
.- Returns:
A flattened string containing each of the symbols, e.g.,
"a b c d e"
.- Return type:
- clean_lisp(str)[source]
Clean S-expressions from a LISP file by removing all commented lines and removing escape characters on symbols.