Coverage for src/meshpy/four_c/yaml_dumper.py: 86%

22 statements  

« 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"""Utility stuff to dump data to a yaml file.""" 

23 

24import numpy as _np 

25import yaml as _yaml 

26 

27from meshpy.four_c.function import Function as _Function 

28 

29 

30class MeshPyDumper(_yaml.SafeDumper): 

31 """Dumper for MeshPy input files.""" 

32 

33 def ignore_aliases(self, data): 

34 """Don't alias shared objects.""" 

35 return True 

36 

37 

38def function_representer(dumper, data: _Function): 

39 """Define how the Function object should be dumped. 

40 

41 Since the only time we have the function object in the list to dump 

42 is when it is referred to in a BC, we only have to return the global 

43 index here. 

44 """ 

45 if data.i_global is None: 

46 raise IndexError("The function does not have a global index!") 

47 return dumper.represent_int(data.i_global) 

48 

49 

50MeshPyDumper.add_representer(_Function, function_representer) 

51 

52 

53def numpy_float_representer(dumper, value): 

54 """Converter for numpy float to yaml.""" 

55 return dumper.represent_float(float(value)) 

56 

57 

58def numpy_int_representer(dumper, value): 

59 """Converter for numpy int to yaml.""" 

60 return dumper.represent_int(int(value)) 

61 

62 

63def numpy_bool_representer(dumper, value): 

64 """Converter for numpy bool to yaml.""" 

65 return dumper.represent_bool(bool(value)) 

66 

67 

68MeshPyDumper.add_representer(_np.float32, numpy_float_representer) 

69MeshPyDumper.add_representer(_np.float64, numpy_float_representer) 

70MeshPyDumper.add_representer(_np.int32, numpy_int_representer) 

71MeshPyDumper.add_representer(_np.int64, numpy_int_representer) 

72MeshPyDumper.add_representer(_np.bool_, numpy_bool_representer)