Skip to content

Commit

Permalink
gh-218: add scala impl to a package
Browse files Browse the repository at this point in the history
  • Loading branch information
EgorOrachyov committed Aug 28, 2023
1 parent 19d6548 commit 1dedd95
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 27 deletions.
7 changes: 5 additions & 2 deletions python/example.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import pyspla

a = pyspla.Array.from_list(dtype=pyspla.INT, values=[-1, 0, 1])
b = pyspla.Array.generate(dtype=pyspla.INT, shape=10, dist=(-10, 10))

print(a)

b = pyspla.Array.generate(dtype=pyspla.INT, shape=10, dist=(-10, 10))
print(b)

s = pyspla.Scalar(value=10, dtype=pyspla.INT)
print(s)
22 changes: 1 addition & 21 deletions python/pyspla/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,23 +115,13 @@ def __init__(self, dtype=INT, shape=None, hnd=None, label=None):
def dtype(self):
"""
Type used for storage parametrization of this container.
Returns
-------
Type of stored values.
"""
return self._dtype

@property
def n_vals(self):
"""
Number of values in the array.
Returns
-------
Size of the array.
"""

n_values = ctypes.c_uint(0)
Expand All @@ -142,24 +132,14 @@ def n_vals(self):
def shape(self):
"""
2-Tuple with shape of array where second value is always 1.
Returns
-------
Size of array as a tuple.
"""

return self.n_vals, 1

@property
def empty(self):
"""
Checks if array is empty (has 0-size) and returns true.
Returns
-------
True if array is empty.
Checks if array is empty (has 0-size) and returns true. True if array is empty.
"""

return self.shape[0] == 0
Expand Down
99 changes: 95 additions & 4 deletions python/pyspla/scalar.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,19 @@

import ctypes

from .bridge import backend, check
from .type import INT
from .object import Object

class Scalar:

class Scalar(Object):
"""
Generalized statically-typed scalar primitive.
Attributes
----------
- type : `type` type of stored matrix elements
- dtype : `type` type of stored matrix elements
- shape : `2-tuple` shape of the scalar in form of two integers tuple (always 1x1)
Notes
Expand Down Expand Up @@ -69,5 +73,92 @@ class Scalar:
Meta-data additionally stored with each scalar value.
"""

def __init__(self):
pass
__slots__ = ["_dtype"]

def __init__(self, dtype=INT, value=None, hnd=None, label=None):
"""
Creates new scalar of desired type or retains existing C object.
Parameters
----------
dtype: optional: Type. default: INT.
Type of the scalar value.
value: optional: Any. default: None.
Optional value to store in scalar on creation.
hnd: optional: ctypes.c_void_p. default: None.
Optional handle to C object to retain in this scalar instance.
label: optional: str. default: None.
Optional debug label of the scalar.
"""

super().__init__(None, None)

self._dtype = dtype

if hnd is None:
hnd = ctypes.c_void_p(0)
check(backend().spla_Scalar_make(ctypes.byref(hnd), dtype._hnd))

super().__init__(label, hnd)
self.set(value)

@property
def dtype(self):
"""
Returns the type of stored value in the scalar.
"""

return self._dtype

@property
def shape(self):
"""
2-tuple shape of the storage. For scalar object it is always 1 by 1.
"""

return 1, 1

@property
def n_vals(self):
"""
Number of stored values in the scalar. Always 1.
"""

return 1

def set(self, value=None):
"""
Set the value stored in the scalar. If no value passed the default value is set.
Parameters
----------
value: optional: Any. default: None.
Optional value to store in scalar.
"""

check(self._dtype._scalar_set(self._hnd, self._dtype._c_type(value)))

def get(self):
"""
Read the value stored in the scalar.
Returns
-------
Value from scalar.
"""

value = self._dtype._c_type(0)
check(self._dtype._scalar_get(self._hnd, ctypes.byref(value)))
return self._dtype.to_py(value)

def __str__(self):
return str(self.get())

def __iter__(self):
return iter([self.get()])

0 comments on commit 1dedd95

Please sign in to comment.