Skip to content

Commit

Permalink
Merge pull request #18 from Bchass/minor_improvements
Browse files Browse the repository at this point in the history
Minor improvements
  • Loading branch information
Bchass authored May 31, 2024
2 parents 6d147fd + 5af0c03 commit 35b509c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 31 deletions.
2 changes: 0 additions & 2 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
[If there's an existing issue related to this pull request, please reference it here.]

## Checklist
- [ ] I have read the CONTRIBUTING document.
- [ ] My code follows the code style of this project.
- [ ] My changes pass all existing automated tests.
- [ ] I have added tests to cover my changes.
- [ ] All new and existing tests passed.
Expand Down
42 changes: 13 additions & 29 deletions tinynumpy/tinynumpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,39 +133,27 @@ def squeeze_strides(s):


def _shape_from_object(obj):

shape = []
# todo: make more efficient, use len() etc
def _shape_from_object_r(index, element, axis):
try:
def _shape_from_object_r(element, axis):
if isinstance(element, list):
for i, e in enumerate(element):
_shape_from_object_r(i, e, axis+1)
_shape_from_object_r(e, axis + 1)
while len(shape) <= axis:
shape.append(0)
l = i + 1
s = shape[axis]
if l > s:
shape[axis] = l
except TypeError:
pass

_shape_from_object_r(0, obj, 0)
shape[axis] = max(shape[axis], len(element))

shape = []
_shape_from_object_r(obj, 0)
return tuple(shape)


def _assign_from_object(array, obj):
key = []
# todo: make more efficient, especially the try-except
def _assign_from_object_r(element):
try:
def _assign_from_object_r(element, indicies):
if isinstance(element, list):
for i, e in enumerate(element):
key.append(i)
_assign_from_object_r(e)
key.pop()
except TypeError:
array[tuple(key)] = element

_assign_from_object_r(obj)
_assign_from_object_r(e, indicies + [i])
else:
array[tuple(indicies)] = element
_assign_from_object_r(obj, [])


def _increment_mutable_key(key, shape):
Expand Down Expand Up @@ -442,7 +430,6 @@ def reshape(X,shape):
assert isinstance(shape, tuple) or isinstance(shape, list)
return X.reshape(shape)

## The class

class ndarray(object):
""" ndarray(shape, dtype='float64', buffer=None, offset=0,
Expand Down Expand Up @@ -1166,7 +1153,6 @@ def flags(self):
OWNDATA=(self._base is None),
WRITEABLE=True, # todo: fix this
ALIGNED=c_cont, # todo: different from contiguous?
UPDATEIFCOPY=False, # We don't support this feature
)

## Methods - managemenet
Expand Down Expand Up @@ -1392,8 +1378,6 @@ def tolist(self):
return comp




class nditer:
def __init__(self, array):
self.array = array
Expand Down

0 comments on commit 35b509c

Please sign in to comment.