Ndarray-listener’s documentation

Date:Mar 13, 2019
Version:1.1.2

Install

Installation is as simple as:

pip install ndarray-listener

Usage

class ndarray_listener.ndl[source]

Examples

A scalar is stored as a zero-dimensional array much like a NumPy scalar:

>>> from __future__ import print_function
>>> from ndarray_listener import ndl
>>> from numpy import atleast_1d
>>>
>>> class Watcher(object):
...     def __init__(self, msg):
...         self._msg = msg
...
...     def __call__(self, value):
...         print(self._msg + " called with %s" % str(value))
...
>>> scalar = ndl(-0.5)
>>>
>>> you0 = Watcher("First guy")
>>> you1 = Watcher("Second guy")
>>>
>>> scalar.talk_to(you0)
>>> scalar.itemset(-1.0)
First guy called with -1.0
>>> s0 = scalar.copy()
>>> s0.itemset(-0.5)
First guy called with -0.5
>>> s0.talk_to(you1)
>>> scalar.itemset(0.0)
First guy called with 0.0
Second guy called with 0.0
>>>
>>> s1 = atleast_1d(scalar)
>>> s1[0] = 1.0
First guy called with [1.]
Second guy called with [1.]

One-dimension arrays are also supported:

>>> from ndarray_listener import ndl
>>> from numpy import atleast_1d
>>> from numpy import set_printoptions
>>>
>>> set_printoptions(precision=2, suppress=True)
>>>
>>> vector = ndl([-0.5, 0.1])
>>>
>>> you0 = Watcher("First guy")
>>> you1 = Watcher("Second guy")
>>>
>>> vector.talk_to(you0)
>>>
>>> vector[0] = 0.0
First guy called with [0.  0.1]
>>> vector[:] = 1.0
First guy called with [1. 1.]
>>>
>>> v0 = vector.copy()
>>> v0.itemset(0, 1.1)
First guy called with [1.1 1. ]
>>>
>>> v0.itemset(1, 2.2)
First guy called with [1.1 2.2]
>>>
>>> v1 = v0.ravel()
>>>
>>> v1.talk_to(you1)
>>> vector[-1] = 9.9
First guy called with [1.  9.9]
Second guy called with [1.  9.9]
itemset(*args)[source]

Insert scalar into an array (scalar is cast to array’s dtype, if possible)

There must be at least 1 argument, and define the last argument as item. Then, a.itemset(*args) is equivalent to but faster than a[args] = item. The item should be a scalar value and args must select a single item in the array a.

Parameters:*args (Arguments) – If one argument: a scalar, only used in case a is of size 1. If two arguments: the last argument is the value to be set and must be a scalar, the first argument specifies a single array element location. It is either an int or a tuple.

Notes

Compared to indexing syntax, itemset provides some speed increase for placing a scalar into a particular location in an ndarray, if you must do this. However, generally this is discouraged: among other problems, it complicates the appearance of the code. Also, when using itemset (and item) inside a loop, be sure to assign the methods to a local variable to avoid the attribute look-up at each loop iteration.

Examples

>>> x = np.random.randint(9, size=(3, 3))
>>> x
array([[3, 1, 7],
       [2, 8, 3],
       [8, 5, 3]])
>>> x.itemset(4, 0)
>>> x.itemset((2, 2), 9)
>>> x
array([[3, 1, 7],
       [2, 0, 3],
       [8, 5, 9]])
class ndarray_listener.float64[source]

Examples

>>> from ndarray_listener import ndl, float64
>>>
>>> print(float64(1.5))
1.5
>>> print(ndl(1.5))
1.5
talk_to(me)[source]

Not implemented.

Array-scalars are immutable.

Comments and bugs

You can get the source and open issues on Github.