|
16 | 16 | _range_to_slice, _seq_group_to_name, _translate_group_key_hdf, remove_nested_groups)
|
17 | 17 | from larray.util.oset import *
|
18 | 18 | from larray.util.misc import (basestring, PY2, unicode, long, duplicates, array_lookup2, ReprString, index_by_id,
|
19 |
| - renamed_to, common_type, LHDFStore, lazy_attribute, _isnoneslice, unique_multi) |
| 19 | + renamed_to, common_type, LHDFStore, lazy_attribute, _isnoneslice, unique_multi, Product) |
20 | 20 |
|
21 | 21 |
|
22 | 22 | class Axis(ABCAxis):
|
@@ -1472,6 +1472,73 @@ def __dir__(self):
|
1472 | 1472 | def __iter__(self):
|
1473 | 1473 | return iter(self._list)
|
1474 | 1474 |
|
| 1475 | + # TODO: move a few doctests to unit tests |
| 1476 | + def iter_labels(self, axes=None, ascending=True): |
| 1477 | + r"""Returns a view of the axes labels. |
| 1478 | +
|
| 1479 | + Parameters |
| 1480 | + ---------- |
| 1481 | + axes : int, str or Axis or tuple of them, optional |
| 1482 | + Axis or axes along which to iterate and in which order. Defaults to None (all axes in the order they are |
| 1483 | + in the collection). |
| 1484 | + ascending : bool, optional |
| 1485 | + Whether or not to iterate the axes in ascending order (from start to end). Defaults to True. |
| 1486 | +
|
| 1487 | + Returns |
| 1488 | + ------- |
| 1489 | + Sequence |
| 1490 | + An object you can iterate (loop) on and index by position. |
| 1491 | +
|
| 1492 | + Examples |
| 1493 | + -------- |
| 1494 | +
|
| 1495 | + >>> from larray import ndtest |
| 1496 | + >>> axes = ndtest((2, 2)).axes |
| 1497 | + >>> axes |
| 1498 | + AxisCollection([ |
| 1499 | + Axis(['a0', 'a1'], 'a'), |
| 1500 | + Axis(['b0', 'b1'], 'b') |
| 1501 | + ]) |
| 1502 | + >>> axes.iter_labels()[0] |
| 1503 | + (a.i[0], b.i[0]) |
| 1504 | + >>> for index in axes.iter_labels(): |
| 1505 | + ... print(index) |
| 1506 | + (a.i[0], b.i[0]) |
| 1507 | + (a.i[0], b.i[1]) |
| 1508 | + (a.i[1], b.i[0]) |
| 1509 | + (a.i[1], b.i[1]) |
| 1510 | + >>> axes.iter_labels(ascending=False)[0] |
| 1511 | + (a.i[1], b.i[1]) |
| 1512 | + >>> for index in axes.iter_labels(ascending=False): |
| 1513 | + ... print(index) |
| 1514 | + (a.i[1], b.i[1]) |
| 1515 | + (a.i[1], b.i[0]) |
| 1516 | + (a.i[0], b.i[1]) |
| 1517 | + (a.i[0], b.i[0]) |
| 1518 | + >>> axes.iter_labels(('b', 'a'))[0] |
| 1519 | + (b.i[0], a.i[0]) |
| 1520 | + >>> for index in axes.iter_labels(('b', 'a')): |
| 1521 | + ... print(index) |
| 1522 | + (b.i[0], a.i[0]) |
| 1523 | + (b.i[0], a.i[1]) |
| 1524 | + (b.i[1], a.i[0]) |
| 1525 | + (b.i[1], a.i[1]) |
| 1526 | + >>> axes.iter_labels('b')[0] |
| 1527 | + (b.i[0],) |
| 1528 | + >>> for index in axes.iter_labels('b'): |
| 1529 | + ... print(index) |
| 1530 | + (b.i[0],) |
| 1531 | + (b.i[1],) |
| 1532 | + """ |
| 1533 | + axes = self if axes is None else self[axes] |
| 1534 | + if not isinstance(axes, AxisCollection): |
| 1535 | + axes = (axes,) |
| 1536 | + # we need .i because Product uses len and [] on axes and not iter; and [] creates LGroup and not IGroup |
| 1537 | + p = Product([axis.i for axis in axes]) |
| 1538 | + if not ascending: |
| 1539 | + p = p[::-1] |
| 1540 | + return p |
| 1541 | + |
1475 | 1542 | def __getattr__(self, key):
|
1476 | 1543 | try:
|
1477 | 1544 | return self._map[key]
|
|
0 commit comments