Coverage for src/beamme/core/container.py: 90%

20 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-07-28 15:20 +0000

1# The MIT License (MIT) 

2# 

3# Copyright (c) 2018-2026 BeamMe Authors 

4# 

5# Permission is hereby granted, free of charge, to any person obtaining a copy 

6# of this software and associated documentation files (the "Software"), to deal 

7# in the Software without restriction, including without limitation the rights 

8# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 

9# copies of the Software, and to permit persons to whom the Software is 

10# furnished to do so, subject to the following conditions: 

11# 

12# The above copyright notice and this permission notice shall be included in 

13# all copies or substantial portions of the Software. 

14# 

15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 

16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 

17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 

18# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 

19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 

20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 

21# THE SOFTWARE. 

22"""This module implements containers to manage boundary conditions and geometry sets in 

23one object.""" 

24 

25# This comment avoids docformatter's issue: https://github.com/PyCQA/docformatter/issues/350 

26 

27 

28class ContainerBase(dict): 

29 """A base class for containers.""" 

30 

31 def append(self, key, item): 

32 """Append item to this container and check if the item is already in the list 

33 corresponding to key.""" 

34 type_ok = False 

35 for item_type in self.item_types: 

36 if isinstance(item, item_type): 

37 type_ok = True 

38 break 

39 if not type_ok: 

40 raise TypeError( 

41 f"You tried to add an item of type {type(item)}, but only types derived " 

42 + f"from {self.item_types} can be added" 

43 ) 

44 if key not in self.keys(): 

45 self[key] = [] 

46 else: 

47 if item in self[key]: 

48 raise ValueError("The item is already in this container!") 

49 self[key].append(item) 

50 

51 def extend(self, container): 

52 """Add all items of another container to this container.""" 

53 if not isinstance(container, self.__class__): 

54 raise TypeError( 

55 f"Only containers of type {self.__class__} can be merged here, you tried " 

56 + f"add {type(container)}" 

57 ) 

58 for key, items in container.items(): 

59 for item in items: 

60 self.append(key, item)