Snuggs is a Python library that provides s-expressions for Numpy, allowing users to define and evaluate array computations using a Lisp-like syntax. It is currently at version 1.4.7 and appears to have a stable, though not frequently updated, release cadence, with the last release in September 2019.
pip install snuggsVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates basic arithmetic operations, array creation using `asarray`, and evaluating expressions with a local context. The `snuggs.eval()` function is central to executing s-expressions.
Replace `snuggs.eval(expression, kwd_dict={'key': value})` with `snuggs.eval(expression, key=value)`.For performance-critical array computations, consider using libraries explicitly designed for optimized numerical operations or vectorized NumPy code directly, rather than relying on Snuggs for performance gains.
For order-dependent context, create an `OrderedDict` and pass its items as keyword arguments: `ctx = OrderedDict((('a', np.array([5, 5])), ('b', np.array([2, 2])))); snuggs.eval('(- (read 1) (read 2))', **ctx)`.pip install snuggs
Define the operator using `snuggs_instance.add_op(name, function)` before evaluating expressions that use it.
Example:
```python
import snuggs
import numpy as np
s = snuggs.Snuggs()
s.add_op('add', lambda *args: np.add(*args)) # Define 'add' operator
s.eval_expression('(add 1 2)')
```Ensure the s-expression string follows correct S-expression syntax, including proper parenthesization and structure.
Example:
```python
import snuggs
s = snuggs.Snuggs()
# Incorrect: s.eval_expression('add 1 2')
s.add_op('add', lambda a, b: a + b) # Assuming 'add' is defined
s.eval_expression('(add 1 2)') # Corrected with parentheses
```Adjust the s-expression to provide the correct number of arguments required by the operator's underlying function.
Example:
```python
import snuggs
s = snuggs.Snuggs()
s.add_op('sum_two_numbers', lambda a, b: a + b)
# Incorrect (too many arguments): s.eval_expression('(sum_two_numbers 1 2 3)')
s.eval_expression('(sum_two_numbers 1 2)') # Corrected to provide two arguments
```