Coverage for src/meshpy/core/container.py: 90%
20 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-28 04:21 +0000
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-28 04:21 +0000
1# The MIT License (MIT)
2#
3# Copyright (c) 2018-2025 MeshPy 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
23sets in one object."""
26class ContainerBase(dict):
27 """A base class for containers to be used in MeshPy."""
29 def append(self, key, item):
30 """Append item to this container and check if the item is already in
31 the list corresponding to key."""
33 type_ok = False
34 for item_type in self.item_types:
35 if isinstance(item, item_type):
36 type_ok = True
37 break
38 if not type_ok:
39 raise TypeError(
40 f"You tried to add an item of type {type(item)}, but only types derived "
41 + f"from {self.item_types} can be added"
42 )
43 if key not in self.keys():
44 self[key] = []
45 else:
46 if item in self[key]:
47 raise ValueError("The item is already in this container!")
48 self[key].append(item)
50 def extend(self, container):
51 """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)