Skip to content

Commit ddfd1d2

Browse files
committed
FEAT: implemented Session.union, .difference and .intersection (closes #22)
1 parent f5bfd59 commit ddfd1d2

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

doc/source/api.rst

+10
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,16 @@ Exploring
762762
Session.items
763763
Session.summary
764764

765+
Combining
766+
---------
767+
768+
.. autosummary::
769+
:toctree: _generated/
770+
771+
Session.union()
772+
Session.intersection()
773+
Session.difference()
774+
765775
Copying
766776
-------
767777

doc/source/changes/version_0_34.rst.inc

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ Backward incompatible changes
1818
New features
1919
^^^^^^^^^^^^
2020

21+
* implemented :py:obj:`Session.union()`, :py:obj:`Session.intersection()` and :py:obj:`Session.difference()` to combine
22+
several sessions into one using set operations (closes :issue:`22`).
23+
2124
* added a feature (see the :ref:`miscellaneous section <misc>` for details). It works on :ref:`api-axis` and
2225
:ref:`api-group` objects.
2326

larray/core/session.py

+87
Original file line numberDiff line numberDiff line change
@@ -1388,6 +1388,93 @@ def apply(self, func, *args, **kwargs) -> 'Session':
13881388
kind = kwargs.pop('kind', Array)
13891389
return Session([(k, func(v, *args, **kwargs) if isinstance(v, kind) else v) for k, v in self.items()])
13901390

1391+
def union(self, other):
1392+
"""Returns session with the (set) union of this session objects with other objects.
1393+
1394+
In other words, the new session will contain objects from this session then those of other whose name is not
1395+
present in this session. Objects relative order will be kept intact, but only unique names will be kept.
1396+
Objects from this session will be before objects from other.
1397+
1398+
Parameters
1399+
----------
1400+
other : Session
1401+
other session
1402+
1403+
Returns
1404+
-------
1405+
Session
1406+
1407+
Examples
1408+
--------
1409+
>>> arr1, arr2, arr3 = ndtest((2, 2)), ndtest(3), ndtest((3, 2))
1410+
>>> s1 = Session(arr1=arr1, arr2=arr2, arr3=arr3)
1411+
>>> s2 = Session(arr2=arr2 + 1, arr4=arr3 + 1)
1412+
>>> s1.union(s2)
1413+
Session(arr1, arr2, arr3, arr4)
1414+
>>> s2.union(s1)
1415+
Session(arr2, arr4, arr1, arr3)
1416+
"""
1417+
self_keys = set(self.keys())
1418+
res = self.copy()
1419+
res._update_from_iterable([(k, v) for k, v in other.items() if k not in self_keys])
1420+
return res
1421+
1422+
def intersection(self, other):
1423+
"""Returns session with the (set) intersection of this session objects with other objects.
1424+
1425+
In other words, the new session will contain objects from this session if there is an object with the same
1426+
name in other. Objects relative order will be kept intact, but only unique names will be kept.
1427+
1428+
Parameters
1429+
----------
1430+
other : Session or sequence of names
1431+
other names
1432+
1433+
Returns
1434+
-------
1435+
Session
1436+
1437+
Examples
1438+
--------
1439+
>>> arr1, arr2, arr3 = ndtest((2, 2)), ndtest(3), ndtest((3, 2))
1440+
>>> s1 = Session(arr1=arr1, arr2=arr2, arr3=arr3)
1441+
>>> s2 = Session(arr2=arr2 + 1, arr3=arr3 + 1)
1442+
>>> s1.intersection(s2)
1443+
Session(arr2, arr3)
1444+
>>> s1.intersection(['arr2', 'arr1'])
1445+
Session(arr1, arr2)
1446+
"""
1447+
other_keys = set(other.keys() if isinstance(other, Session) else other)
1448+
return self[[k for k in self.keys() if k in other_keys]]
1449+
1450+
def difference(self, other):
1451+
"""Returns session with the (set) difference of this session objects and other objects.
1452+
1453+
In other words, the new session will contain objects from this session if there is no object with the same
1454+
name in other. Objects relative order will be kept intact, but only unique names will be kept.
1455+
1456+
Parameters
1457+
----------
1458+
other : Session or sequence of names
1459+
other names
1460+
1461+
Returns
1462+
-------
1463+
Session
1464+
1465+
Examples
1466+
--------
1467+
>>> arr1, arr2, arr3 = ndtest((2, 2)), ndtest(3), ndtest((3, 2))
1468+
>>> s1 = Session(arr1=arr1, arr2=arr2, arr3=arr3)
1469+
>>> s2 = Session(arr2=arr2 + 1, arr3=arr3 + 1)
1470+
>>> s1.difference(s2)
1471+
Session(arr1)
1472+
>>> s1.difference(['arr2', 'arr1'])
1473+
Session(arr3)
1474+
"""
1475+
other_keys = set(other.keys() if isinstance(other, Session) else other)
1476+
return self[[k for k in self.keys() if k not in other_keys]]
1477+
13911478
def summary(self, template=None) -> str:
13921479
"""
13931480
Returns a summary of the content of the session.

0 commit comments

Comments
 (0)