Skip to content

Commit

Permalink
gh-218: minor docs fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
EgorOrachyov committed Aug 28, 2023
1 parent 413eaa7 commit 7016b3e
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 33 deletions.
2 changes: 1 addition & 1 deletion python/example.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import pyspla

print(pyspla.Array.generate(dtype=pyspla.FLOAT, shape=3, dist=[100, 300]).to_list())
print(pyspla.Array.generate(dtype=pyspla.FLOAT, shape=3, dist=[100, 300]).to_list())
121 changes: 92 additions & 29 deletions python/pyspla/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,20 @@ def __init__(self, dtype=INT, shape=0, hnd=None, label=None):
"""
Creates new array of specified type and shape.
:param dtype: Type parametrization of a storage
:param shape: Size of the array
:param hnd: Optional native handle to retain
:param label: Optional label to assign
Parameters
----------
dtype: Type.
Type parametrization of a storage
shape: optional: int. default: 0.
Size of the array
hnd: optional: ctypes.c_void_p. default: None.
Optional native handle to retain
label: optional: str. default: None.
Optional label to assign
"""

super().__init__(None, None)
Expand All @@ -103,7 +113,10 @@ def dtype(self):
"""
Type used for storage parametrization of this container.
:return: Type of stored values
Returns
-------
Type of stored values.
"""
return self._dtype

Expand All @@ -112,7 +125,10 @@ def shape(self):
"""
2-Tuple with shape of array where second value is always 1.
:return: Size of array as a tuple
Returns
-------
Size of array as a tuple.
"""

n_values = ctypes.c_uint(0)
Expand All @@ -124,7 +140,10 @@ def empty(self):
"""
Checks if array is empty (has 0-size) and returns true.
:return: True if array is empty
Returns
-------
True if array is empty.
"""

return self.shape[0] == 0
Expand All @@ -133,9 +152,14 @@ def set(self, index, value):
"""
Set value at specified index.
:param index: Index at which value to set
:param value: Value to set, must be convertible to destination type
:return:
Parameters
----------
index: int.
Index at which value to set
value: any.
Value to set, must be convertible to destination type
"""

check(self._dtype._array_set(self._hnd, ctypes.c_uint(index), self._dtype._c_type(value)))
Expand All @@ -144,29 +168,38 @@ def get(self, index):
"""
Get value at specified index.
:param index: Index at which to get value
:return: Value at specified index
Parameters
----------
index: int.
Index at which to get value
Returns
-------
Value at specified index.
"""

value = self._dtype._c_type(0)
check(self._dtype._array_get(self._hnd, ctypes.c_uint(index), ctypes.byref(value)))
return self._dtype._to_python(value)
return self._dtype.to_py(value)

def resize(self, shape=0):
"""
Resizes array to new size with desired num of values specified as shape.
:param shape: New array capacity
:return:
Parameters
----------
shape: optional: int. default: 0.
New array capacity
"""

check(backend().spla_Array_resize(self._hnd, ctypes.c_uint(shape)))

def clear(self):
"""
Clears array removing all elements, so it has 0 values.
:return:
"""

check(backend().spla_Array_clear(self._hnd))
Expand All @@ -175,27 +208,43 @@ def to_list(self):
"""
Read array data as a python list of values.
:return: List with values stored in the array
Returns
-------
List with values stored in the array
"""

values = list()
value = self._dtype._c_type(0)

for i in range(self.shape[0]):
check(self._dtype._array_get(self._hnd, ctypes.c_uint(i), ctypes.byref(value)))
values.append(self._dtype._to_python(value))
values.append(self._dtype.to_py(value))

return values

@classmethod
def from_list(cls, values, dtype=INT, shape=None):
"""
Creates new array of desired type and shape and fills its content with `values` data.
Creates new array of desired type and shape and fills
its content with `values` data.
Parameters
----------
values: List.
List with values to fill array
dtype: Type.
Type of the array stored value
shape: optional: int. default: None.
Optional size of array, by default inferred from `values`
Returns
-------
:param values: List with values to fill array
:param dtype: Type of the array stored value
:param shape: Optional size of array, by default inferred from `values`
:return: Created array filled with values
Created array filled with values.
"""

if shape is None:
Expand All @@ -212,11 +261,25 @@ def generate(cls, dtype=INT, shape=0, seed=None, dist=(0, 1)):
Creates new array of desired type and shape and fills its content
with random values, generated using specified distribution.
:param dtype: Type of values array will have
:param shape: Size of the array (number of values)
:param seed: Optional seed to randomize generator
:param dist: Optional distribution for uniform generation of values
:return: Created array filled with values
Parameters
----------
dtype: Type.
Type of values array will have
shape: optional: int. default: 0.
Size of the array (number of values)
seed: optional: int. default: None.
Optional seed to randomize generator
dist: optional: tuple. default: [0,1].
Optional distribution for uniform generation of values
Returns
-------
Created array filled with values.
"""

array = Array(dtype=dtype, shape=shape)
Expand Down
13 changes: 13 additions & 0 deletions python/pyspla/object.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ class Object:
__slots__ = ["_hnd", "_label"]

def __init__(self, label, hnd):
"""
Creates a base spla object class instance with common attributes.
Parameters
----------
label: optional: str. default: None.
Object name for debugging.
hnd: optional: ctypes.c_void_p. default: None.
Object native void* handle to a C counterpart.
"""

self._hnd = hnd
self._label = label

Expand Down
14 changes: 11 additions & 3 deletions python/pyspla/type.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ class Type:
_matrix_set = None
_hnd = None

@classmethod
def get_code(cls):
return cls._code

@classmethod
def to_py(cls, value):
pass


class INT(Type):
"""Spla integral INT-32 type."""
Expand All @@ -76,7 +84,7 @@ def _setup(cls):
cls._hnd = backend().spla_Type_int()

@classmethod
def _to_python(cls, value):
def to_py(cls, value):
return int(value.value)


Expand All @@ -100,7 +108,7 @@ def _setup(cls):
cls._hnd = backend().spla_Type_uint()

@classmethod
def _to_python(cls, value):
def to_py(cls, value):
return int(value.value)


Expand All @@ -124,7 +132,7 @@ def _setup(cls):
cls._hnd = backend().spla_Type_float()

@classmethod
def _to_python(cls, value):
def to_py(cls, value):
return float(value.value)


Expand Down

0 comments on commit 7016b3e

Please sign in to comment.