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])

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])

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

snuggs-1.2.0.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

snuggs-1.2.0-py2-none-any.whl (5.5 kB view details)

Uploaded Python 2

File details

Details for the file snuggs-1.2.0.tar.gz.

File metadata

  • Download URL: snuggs-1.2.0.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for snuggs-1.2.0.tar.gz
Algorithm Hash digest
SHA256 2affdcbc62a9943ceb292d447e3fd4f278a175dafa7a76f74eb9ec8dc10473a9
MD5 86aa9b319092dbb87c40a3f2e218809e
BLAKE2b-256 843b3729317302bbd315cdf946dbcd0a9b1ba714fa4e7e7cdae69980a3d4674f

See more details on using hashes here.

File details

Details for the file snuggs-1.2.0-py2-none-any.whl.

File metadata

File hashes

Hashes for snuggs-1.2.0-py2-none-any.whl
Algorithm Hash digest
SHA256 72fe7992612401a0e84635321e226e2473fabc8f603c6c8091d44cabcaa2e270
MD5 292bd11839624b9596320779704cdc5d
BLAKE2b-256 86e7219aaa87cbbd1bbca16fa37bee94a4068779c0225075f182de5045fffe78

See more details on using hashes here.

Release history Release notifications | RSS feed

1.4.7

2 files

1.4.6

2 files

1.4.5

2 files

1.4.4

2 files

1.4.3

2 files

1.4.2

2 files

1.4.1

3 files

1.4.0

3 files

1.3.1

2 files

1.3.0

2 files

This release

1.2.0 This release

2 files

1.1.0

3 files

1.0

1 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