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

balanced_substr

Find a substring with a balanced number of parentheses.

clean_lisp

Clean S-expressions from a LISP file by removing all commented lines and removing escape characters on symbols.

clean_s_expr

Clean a string representation of an S-expression by removing newlines and standardizing whitespace.

compress_quotes

Compress quoted expressions that were split between multiple words during parsing.

convert_quotes

Convert any quoted word lists (i.e., single ' symbol followed by a list of symbols) to a single quoted string.

list_to_s_expr

Convert an S-expression list structure to a string representing a LISP formatted S-expression.

list_to_str

Convert an S-expression list structure to a flattened string containing each of the symbols.

parse_s_expr

Parse a string containing an S-expression (in LISP form) into a structured list.

read_lisp

Read a list of S-expressions from a LISP file.

standardize_symbols

Standardize the symbols within an S-expression by mapping to lowercase, unless enclosed in escape symbols.

write_lisp

Write an S-expression to a LISP file.

balanced_substr(s)[source]

Find a substring with a balanced number of parentheses.

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:

str

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:

str

clean_lisp(str)[source]

Clean S-expressions from a LISP file by removing all commented lines and removing escape characters on symbols.

read_lisp(fname)[source]

Read a list of S-expressions from a LISP file.

Parameters:

fname (str) – The LISP file to read from.

Returns:

A list of S-expressions, in recursively nested list form.

Return type:

list[s-expr]

write_lisp(fname, sexpr)[source]

Write an S-expression to a LISP file.

Parameters:
  • fname (str) – The LISP file to write to.

  • sexpr (s-expr) – An S-expression, in recursively nested list form, to write to the file in LISP format.