Skip to main content
https://travis-ci.org/mapbox/snuggs.svg?branch=master https://coveralls.io/repos/mapbox/snuggs/badge.svg

Snuggs are s-expressions for Numpy

>>> snuggs.eval("(+ (asarray 1 1) (asarray 2 2))")
array([3, 3])

Syntax

Snuggs wraps Numpy in expressions with the following syntax:

expression = "(" (operator | function) *arg ")"
arg = expression | name | number | string

Examples

Addition of two numbers

import snuggs
snuggs.eval('(+ 1 2)')
# 3

Multiplication of a number and an array

Arrays can be created using asarray.

snuggs.eval("(* 3.5 (asarray 1 1))")
# array([ 3.5,  3.5])

Evaluation context

Expressions can also refer by name to arrays in a local context.

snuggs.eval("(+ (asarray 1 1) b)", b=np.array([2, 2]))
# array([3, 3])

This local context may be provided using keyword arguments (e.g., b=np.array([2, 2])), or by passing a dictionary that stores the keys and associated array values. Passing a dictionary, specifically an OrderedDict, is important when using a function or operator that references the order in which values have been provided. For example, the read function will lookup the i-th value passed:

ctx = OrderedDict((
    ('a', np.array([5, 5])),
    ('b', np.array([2, 2]))
))
snuggs.eval("(- (read 1) (read 2))", ctx)
# array([3, 3])

Functions and operators

Arithmetic (* + / -) and logical (< <= == != >= > & |) operators are available. Members of the numpy module such as asarray(), mean(), and where() are also available.

snuggs.eval("(mean (asarray 1 2 4))")
# 2.3333333333333335
snuggs.eval("(where (& tt tf) 1 0)",
    tt=numpy.array([True, True]),
    tf=numpy.array([True, False]))
# array([1, 0])

Higher-order functions

New in snuggs 1.1 are higher-order functions map and partial.

snuggs.eval("((partial * 2) 2)")
# 4

snuggs.eval('(asarray (map (partial * 2) (asarray 1 2 3)))')
# array([2, 4, 6])

Performance notes

Snuggs makes simple calculator programs possible. None of the optimizations of, e.g., numexpr (multithreading, elimination of temporary data, etc) are currently available.

If you’re looking to combine Numpy with a more complete Lisp, see Hy:

=> (import numpy)
=> (* 2 (.asarray numpy [1 2 3]))
array([2, 4, 6])

Release files for snuggs 1.4.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for snuggs 1.4.7
File Size Uploaded
snuggs-1.4.7.tar.gz 8.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for snuggs 1.4.7
File Interpreter ABI Platform
snuggs-1.4.7-py3-none-any.whl Python 3 none any Details

Total release size: 13.6 kB

Release files / snuggs-1.4.7.tar.gz

Download URL snuggs-1.4.7.tar.gz
Size 8.2 kB
Tags Source
SHA-256 checksum
How to use checksums
501cf113fe3892e14e2fee76da5cd0606b7e149c411c271898e6259ebde2617b
BLAKE2b-256 checksum
How to use checksums
93190d11ab370735dde61076a0e41644e5593821776e69e3b0344626cfa0e56a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.36.0 CPython/3.6.7

Release files / snuggs-1.4.7-py3-none-any.whl

Download URL snuggs-1.4.7-py3-none-any.whl
Size 5.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
988dde5d4db88e9d71c99457404773dabcc7a1c45971bfbe81900999942d9f07
BLAKE2b-256 checksum
How to use checksums
cc0ed27d6e806d6c0d1a2cfdc5d1f088e42339a0a54a09c3343f7f81ec8947ea
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.36.0 CPython/3.6.7

Release history Release notifications | RSS feed

This release

1.4.7 This release

2 release files

1.4.6

2 release files

1.4.5

2 release files

1.4.4

2 release files

1.4.3

2 release files

1.4.2

2 release files

1.4.1

3 release files

1.4.0

3 release files

1.3.1

2 release files

1.3.0

2 release files

1.2.0

2 release files

1.1.0

3 release files

1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page