Skip to content

Commit 10150a0

Browse files
committed
extract option values in LArray.__str__() instead of LArray.as_table() and table2str()
1 parent 38ffbc4 commit 10150a0

File tree

3 files changed

+16
-28
lines changed

3 files changed

+16
-28
lines changed

larray/core/array.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
from larray.util.misc import (table2str, size2str, basestring, izip, rproduct, ReprString, duplicates,
6666
float_error_handler_factory, _isnoneslice, light_product, unique_list, common_type,
6767
renamed_to, deprecate_kwarg, LHDFStore, lazy_attribute)
68-
from larray.util.options import OPTIONS, DISPLAY_MAXLINES, DISPLAY_EDGEITEMS
68+
from larray.util.options import OPTIONS, DISPLAY_MAXLINES, DISPLAY_EDGEITEMS, DISPLAY_WIDTH, DISPLAY_PRECISION
6969

7070

7171
def all(values, axis=None):
@@ -2278,8 +2278,9 @@ def __str__(self):
22782278
elif not len(self):
22792279
return 'LArray([])'
22802280
else:
2281-
table = list(self.as_table())
2282-
return table2str(table, 'nan', keepcols=self.ndim - 1)
2281+
table = list(self.as_table(OPTIONS[DISPLAY_MAXLINES], OPTIONS[DISPLAY_EDGEITEMS]))
2282+
return table2str(table, 'nan', maxwidth=OPTIONS[DISPLAY_WIDTH], keepcols=self.ndim - 1,
2283+
precision=OPTIONS[DISPLAY_PRECISION])
22832284
__repr__ = __str__
22842285

22852286
def __iter__(self):
@@ -2288,20 +2289,20 @@ def __iter__(self):
22882289
def __contains__(self, key):
22892290
return any(key in axis for axis in self.axes)
22902291

2291-
def as_table(self, maxlines=None, edgeitems=None, light=False, wide=True, value_name='value'):
2292+
def as_table(self, maxlines=-1, edgeitems=5, light=False, wide=True, value_name='value'):
22922293
r"""
22932294
Generator. Returns next line of the table representing an array.
22942295
22952296
Parameters
22962297
----------
22972298
maxlines : int, optional
2298-
Maximum number of lines to show. If 0 all lines are shown.
2299-
See :py:obj:`set_options` for default value.
2299+
Maximum number of lines to show. If negative all lines are shown.
2300+
Defaults to -1.
23002301
edgeitems : int, optional
23012302
If number of lines to display is greater than `maxlines`,
23022303
only the first and last `edgeitems` lines are displayed.
23032304
Only active if `maxlines` is not 0.
2304-
See :py:obj:`set_options` for default value.
2305+
Defaults to 5.
23052306
light: bool, optional
23062307
Whether or not printing the array in the same way as a pandas DataFrame with a MultiIndex
23072308
(see example below). Defaults to False.
@@ -2351,12 +2352,6 @@ def as_table(self, maxlines=None, edgeitems=None, light=False, wide=True, value_
23512352
if not self.ndim:
23522353
return
23532354

2354-
# get default options
2355-
if maxlines is None:
2356-
maxlines = OPTIONS[DISPLAY_MAXLINES]
2357-
if edgeitems is None:
2358-
edgeitems = OPTIONS[DISPLAY_EDGEITEMS]
2359-
23602355
# ert unit geo\time 2012 2011 2010
23612356
# NEER27 I05 AT 101.41 101.63 101.63
23622357
# NEER27 I05 AU 134.86 125.29 117.08
@@ -2432,7 +2427,7 @@ def dump(self, header=True, wide=True, value_name='value'):
24322427
# flatten all dimensions except the last one
24332428
return self.data.reshape(-1, self.shape[-1]).tolist()
24342429
else:
2435-
return list(self.as_table(maxlines=0, wide=wide, value_name=value_name))
2430+
return list(self.as_table(maxlines=-1, wide=wide, value_name=value_name))
24362431

24372432
# XXX: should filter(geo=['W']) return a view by default? (collapse=True)
24382433
# I think it would be dangerous to make it the default

larray/util/misc.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,21 +133,14 @@ def get_min_width(table, index):
133133
return max(longest_word(row[index]) for row in table)
134134

135135

136-
def table2str(table, missing, summarize=True, maxwidth=None, numedges='auto', sep=' ', cont='...',
136+
def table2str(table, missing, summarize=True, maxwidth=200, numedges='auto', sep=' ', cont='...',
137137
keepcols=0, precision=None):
138138
"""
139139
table is a list of lists
140140
:type table: list of list
141141
"""
142142
if not table:
143143
return ''
144-
145-
from larray.util.options import OPTIONS, DISPLAY_WIDTH, DISPLAY_PRECISION
146-
if maxwidth is None:
147-
maxwidth = OPTIONS[DISPLAY_WIDTH]
148-
if precision is None:
149-
precision = OPTIONS[DISPLAY_PRECISION]
150-
151144
numcol = max(len(row) for row in table)
152145
# pad rows that have too few columns
153146
for row in table:

larray/util/options.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@
2020
}
2121

2222

23+
def _integer(value):
24+
if not isinstance(value, int):
25+
raise ValueError("Expected integer")
26+
27+
2328
def _positive_integer(value):
2429
if not (isinstance(value, int) and value > 0):
2530
raise ValueError("Expected positive integer")
2631

2732

28-
def _non_negative_integer(value):
29-
if not (isinstance(value, int) and value >= 0):
30-
raise ValueError("Expected non-negative integer")
31-
32-
3333
def _positive_integer_or_none(value):
3434
if value is None:
3535
return
@@ -40,7 +40,7 @@ def _positive_integer_or_none(value):
4040
_VALIDATORS = {
4141
DISPLAY_PRECISION: _positive_integer_or_none,
4242
DISPLAY_WIDTH: _positive_integer,
43-
DISPLAY_MAXLINES: _non_negative_integer,
43+
DISPLAY_MAXLINES: _integer,
4444
DISPLAY_EDGEITEMS: _positive_integer,
4545
}
4646

0 commit comments

Comments
 (0)