======
snuggs
======
.. image:: https://travis-ci.org/mapbox/snuggs.svg?branch=master
:target: https://travis-ci.org/mapbox/snuggs
.. image:: https://coveralls.io/repos/mapbox/snuggs/badge.svg
:target: https://coveralls.io/r/mapbox/snuggs
Snuggs are s-expressions for Numpy
.. code-block:: python
>>> snuggs.eval("(+ (asarray 1 1) (asarray 2 2))")
array([3, 3])
Syntax
======
Snuggs wraps Numpy in expressions with the following syntax:
.. code-block::
expression = "(" (operator | function) *arg ")"
arg = expression | name | number | string
Examples
========
Addition of two numbers
-----------------------
.. code-block:: python
import snuggs
snuggs.eval('(+ 1 2)')
# 3
Multiplication of a number and an array
---------------------------------------
Arrays can be created using ``asarray``.
.. code-block:: python
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.
.. code-block:: python
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:
.. code-block:: python
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.
.. code-block:: python
snuggs.eval("(mean (asarray 1 2 4))")
# 2.3333333333333335
.. code-block:: python
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``.
.. code-block:: python
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 <https://github.com/pydata/numexpr>`__ (multithreading,
elimination of temporary data, etc) are currently available.
If you're looking to combine Numpy with a more complete Lisp, see
`Hy <https://github.com/hylang/hy>`__:
.. code-block:: clojure
=> (import numpy)
=> (* 2 (.asarray numpy [1 2 3]))
array([2, 4, 6])
Raw data
{
"_id": null,
"home_page": "https://github.com/mapbox/snuggs",
"name": "snuggs",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "",
"author": "Sean Gillies",
"author_email": "sean@mapbox.com",
"download_url": "https://files.pythonhosted.org/packages/93/19/0d11ab370735dde61076a0e41644e5593821776e69e3b0344626cfa0e56a/snuggs-1.4.7.tar.gz",
"platform": "",
"description": "======\nsnuggs\n======\n\n.. image:: https://travis-ci.org/mapbox/snuggs.svg?branch=master\n :target: https://travis-ci.org/mapbox/snuggs\n\n.. image:: https://coveralls.io/repos/mapbox/snuggs/badge.svg\n :target: https://coveralls.io/r/mapbox/snuggs\n\nSnuggs are s-expressions for Numpy\n\n.. code-block:: python\n\n >>> snuggs.eval(\"(+ (asarray 1 1) (asarray 2 2))\")\n array([3, 3])\n\nSyntax\n======\n\nSnuggs wraps Numpy in expressions with the following syntax:\n\n.. code-block::\n\n expression = \"(\" (operator | function) *arg \")\"\n arg = expression | name | number | string\n\nExamples\n========\n\nAddition of two numbers\n-----------------------\n\n.. code-block:: python\n\n import snuggs\n snuggs.eval('(+ 1 2)')\n # 3\n\nMultiplication of a number and an array\n---------------------------------------\n\nArrays can be created using ``asarray``.\n\n.. code-block:: python\n\n snuggs.eval(\"(* 3.5 (asarray 1 1))\")\n # array([ 3.5, 3.5])\n\nEvaluation context\n------------------\n\nExpressions can also refer by name to arrays in a local context.\n\n.. code-block:: python\n\n snuggs.eval(\"(+ (asarray 1 1) b)\", b=np.array([2, 2]))\n # array([3, 3])\n\nThis local context may be provided using keyword arguments (e.g.,\n``b=np.array([2, 2])``), or by passing a dictionary that stores\nthe keys and associated array values. Passing a dictionary, specifically\nan ``OrderedDict``, is important when using a function or operator that\nreferences the order in which values have been provided. For example,\nthe ``read`` function will lookup the `i-th` value passed:\n\n.. code-block:: python\n\n ctx = OrderedDict((\n ('a', np.array([5, 5])),\n ('b', np.array([2, 2]))\n ))\n snuggs.eval(\"(- (read 1) (read 2))\", ctx)\n # array([3, 3])\n\nFunctions and operators\n=======================\n\nArithmetic (``* + / -``) and logical (``< <= == != >= > & |``) operators are\navailable. Members of the ``numpy`` module such as ``asarray()``, ``mean()``,\nand ``where()`` are also available.\n\n.. code-block:: python\n\n snuggs.eval(\"(mean (asarray 1 2 4))\")\n # 2.3333333333333335\n\n.. code-block:: python\n\n snuggs.eval(\"(where (& tt tf) 1 0)\",\n tt=numpy.array([True, True]),\n tf=numpy.array([True, False]))\n # array([1, 0])\n\nHigher-order functions\n======================\n\nNew in snuggs 1.1 are higher-order functions ``map`` and ``partial``.\n\n.. code-block:: python\n\n snuggs.eval(\"((partial * 2) 2)\")\n # 4\n\n snuggs.eval('(asarray (map (partial * 2) (asarray 1 2 3)))')\n # array([2, 4, 6])\n\nPerformance notes\n=================\n\nSnuggs makes simple calculator programs possible. None of the optimizations\nof, e.g., `numexpr <https://github.com/pydata/numexpr>`__ (multithreading,\nelimination of temporary data, etc) are currently available.\n\nIf you're looking to combine Numpy with a more complete Lisp, see\n`Hy <https://github.com/hylang/hy>`__:\n\n.. code-block:: clojure\n\n => (import numpy)\n => (* 2 (.asarray numpy [1 2 3]))\n array([2, 4, 6])\n\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Snuggs are s-expressions for Numpy",
"version": "1.4.7",
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"md5": "3e86bc664526e0aaf579d47a56b5e34e",
"sha256": "988dde5d4db88e9d71c99457404773dabcc7a1c45971bfbe81900999942d9f07"
},
"downloads": -1,
"filename": "snuggs-1.4.7-py3-none-any.whl",
"has_sig": false,
"md5_digest": "3e86bc664526e0aaf579d47a56b5e34e",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 5370,
"upload_time": "2019-09-18T18:54:49",
"upload_time_iso_8601": "2019-09-18T18:54:49.829832Z",
"url": "https://files.pythonhosted.org/packages/cc/0e/d27d6e806d6c0d1a2cfdc5d1f088e42339a0a54a09c3343f7f81ec8947ea/snuggs-1.4.7-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"md5": "74265bada7a08e9d80be9497dfbe47e7",
"sha256": "501cf113fe3892e14e2fee76da5cd0606b7e149c411c271898e6259ebde2617b"
},
"downloads": -1,
"filename": "snuggs-1.4.7.tar.gz",
"has_sig": false,
"md5_digest": "74265bada7a08e9d80be9497dfbe47e7",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 8196,
"upload_time": "2019-09-18T18:54:51",
"upload_time_iso_8601": "2019-09-18T18:54:51.598801Z",
"url": "https://files.pythonhosted.org/packages/93/19/0d11ab370735dde61076a0e41644e5593821776e69e3b0344626cfa0e56a/snuggs-1.4.7.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2019-09-18 18:54:51",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "mapbox",
"github_project": "snuggs",
"travis_ci": true,
"coveralls": false,
"github_actions": false,
"lcname": "snuggs"
}