Skip to content

Commit 30226ad

Browse files
committed
fix larray-project#203 : creation functions raise an error when called as func(axis1, axis2, ...) instead of func([axis1, axis2], ...)
1 parent b5b33c1 commit 30226ad

2 files changed

Lines changed: 29 additions & 5 deletions

File tree

‎doc/source/changes/version_0_22.rst.inc‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,6 @@ Fixes
178178
* fixed group sum over boolean arrays (closes :issue:`194`).
179179

180180
* fixed set_labels when inplace=True
181+
182+
* fixed array creation functions not raising an exception when called with wrong syntax func(axis1, axis2, ...)
183+
instead of func([axis1, axis2, ...]). (closes :issue:`203`)

‎larray/core.py‎

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
import re
9292
import sys
9393
import warnings
94+
import functools
9495

9596
try:
9697
import builtins
@@ -10130,12 +10131,23 @@ def read_sas(filepath, nb_index=None, index_col=None, na=np.nan, sort_rows=False
1013010131
return df_aslarray(df, sort_rows=sort_rows, sort_columns=sort_columns, fill_value=na)
1013110132

1013210133

10134+
def _check_axes_argument(func):
10135+
@functools.wraps(func)
10136+
def wrapper(*args, **kwargs):
10137+
if len(args) > 1 and isinstance(args[1], (int, Axis)):
10138+
raise ValueError("If you want to pass several axes or dimension lengths to {}, "
10139+
"you must pass them as a list (using []) or tuple (using()).".format(func.__name__))
10140+
return func(*args, **kwargs)
10141+
return wrapper
10142+
10143+
10144+
@_check_axes_argument
1013310145
def zeros(axes, title='', dtype=float, order='C'):
1013410146
"""Returns an array with the specified axes and filled with zeros.
1013510147
1013610148
Parameters
1013710149
----------
10138-
axes : int, tuple of int or tuple/list/AxisCollection of Axis
10150+
axes : int, tuple of int, Axis or tuple/list/AxisCollection of Axis
1013910151
Collection of axes or a shape.
1014010152
title : str, optional
1014110153
Title.
@@ -10206,12 +10218,13 @@ def zeros_like(array, title='', dtype=None, order='K'):
1020610218
return LArray(np.zeros_like(array, dtype, order), array.axes, title)
1020710219

1020810220

10221+
@_check_axes_argument
1020910222
def ones(axes, title='', dtype=float, order='C'):
1021010223
"""Returns an array with the specified axes and filled with ones.
1021110224
1021210225
Parameters
1021310226
----------
10214-
axes : int, tuple of int or tuple/list/AxisCollection of Axis
10227+
axes : int, tuple of int, Axis or tuple/list/AxisCollection of Axis
1021510228
Collection of axes or a shape.
1021610229
title : str, optional
1021710230
Title.
@@ -10274,12 +10287,13 @@ def ones_like(array, title='', dtype=None, order='K'):
1027410287
return LArray(np.ones_like(array, dtype, order), axes, title)
1027510288

1027610289

10290+
@_check_axes_argument
1027710291
def empty(axes, title='', dtype=float, order='C'):
1027810292
"""Returns an array with the specified axes and uninitialized (arbitrary) data.
1027910293
1028010294
Parameters
1028110295
----------
10282-
axes : int, tuple of int or tuple/list/AxisCollection of Axis
10296+
axes : int, tuple of int, Axis or tuple/list/AxisCollection of Axis
1028310297
Collection of axes or a shape.
1028410298
title : str, optional
1028510299
Title.
@@ -10343,12 +10357,13 @@ def empty_like(array, title='', dtype=None, order='K'):
1034310357
return LArray(np.empty_like(array.data, dtype, order), array.axes, title)
1034410358

1034510359

10360+
# We cannot use @_check_axes_argument here because an integer fill_value would be considered as an error
1034610361
def full(axes, fill_value, title='', dtype=None, order='C'):
1034710362
"""Returns an array with the specified axes and filled with fill_value.
1034810363
1034910364
Parameters
1035010365
----------
10351-
axes : int, tuple of int or tuple/list/AxisCollection of Axis
10366+
axes : int, tuple of int, Axis or tuple/list/AxisCollection of Axis
1035210367
Collection of axes or a shape.
1035310368
fill_value : scalar or LArray
1035410369
Value to fill the array
@@ -10381,6 +10396,9 @@ def full(axes, fill_value, title='', dtype=None, order='C'):
1038110396
BE | 0 | 1
1038210397
FO | 0 | 1
1038310398
"""
10399+
if isinstance(fill_value, Axis):
10400+
raise ValueError("If you want to pass several axes or dimension lengths to full, "
10401+
"you must pass them as a list (using []) or tuple (using()).")
1038410402
if dtype is None:
1038510403
dtype = np.asarray(fill_value).dtype
1038610404
res = empty(axes, title, dtype, order)
@@ -10638,6 +10656,7 @@ def array_or_full(a, axis, initial):
1063810656
return res
1063910657

1064010658

10659+
@_check_axes_argument
1064110660
def ndrange(axes, start=0, title='', dtype=int):
1064210661
"""Returns an array with the specified axes and filled with increasing int.
1064310662
@@ -10710,6 +10729,7 @@ def ndrange(axes, start=0, title='', dtype=int):
1071010729
return LArray(data.reshape(axes.shape), axes, title)
1071110730

1071210731

10732+
@_check_axes_argument
1071310733
def ndtest(shape, start=0, label_start=0, title='', dtype=int):
1071410734
"""Returns test array with given shape.
1071510735
@@ -10719,7 +10739,7 @@ def ndtest(shape, start=0, label_start=0, title='', dtype=int):
1071910739
1072010740
Parameters
1072110741
----------
10722-
shape : int, tuple or list
10742+
shape : int, tuple/list of int
1072310743
Shape of the array to create. An int can be used directly for one
1072410744
dimensional arrays.
1072510745
start : int or float, optional
@@ -10856,6 +10876,7 @@ def diag(a, k=0, axes=(0, 1), ndim=2, split=True):
1085610876
return a.points[indexer]
1085710877

1085810878

10879+
@_check_axes_argument
1085910880
def labels_array(axes, title=''):
1086010881
"""Returns an array with specified axes and the combination of
1086110882
corresponding labels as values.

0 commit comments

Comments
 (0)