@@ -1388,6 +1388,93 @@ def apply(self, func, *args, **kwargs) -> 'Session':
1388
1388
kind = kwargs .pop ('kind' , Array )
1389
1389
return Session ([(k , func (v , * args , ** kwargs ) if isinstance (v , kind ) else v ) for k , v in self .items ()])
1390
1390
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
+
1391
1478
def summary (self , template = None ) -> str :
1392
1479
"""
1393
1480
Returns a summary of the content of the session.
0 commit comments