Coverage for C: \ Users \ peaco \ OneDrive \ Documents \ GitHub \ mt_metadata \ mt_metadata \ transfer_functions \ io \ jfiles \ metadata \ birrp_block.py: 76%

29 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-01-10 00:11 -0800

1# ===================================================== 

2# Imports 

3# ===================================================== 

4from typing import Annotated 

5 

6from pydantic import Field, field_validator 

7 

8from mt_metadata.base import MetadataBase 

9 

10 

11# ===================================================== 

12class BirrpBlock(MetadataBase): 

13 filnam: Annotated[ 

14 str, 

15 Field( 

16 default="", 

17 description="File name of data block", 

18 alias=None, 

19 json_schema_extra={ 

20 "units": None, 

21 "required": True, 

22 "examples": ["hx.dat"], 

23 }, 

24 ), 

25 ] 

26 

27 nskip: Annotated[ 

28 int, 

29 Field( 

30 default=None, 

31 description="number of points to skip", 

32 alias=None, 

33 json_schema_extra={ 

34 "units": None, 

35 "required": True, 

36 "examples": ["0"], 

37 }, 

38 ), 

39 ] 

40 

41 nread: Annotated[ 

42 int, 

43 Field( 

44 default=None, 

45 description="number of points to read", 

46 alias=None, 

47 json_schema_extra={ 

48 "units": None, 

49 "required": True, 

50 "examples": ["10000"], 

51 }, 

52 ), 

53 ] 

54 

55 ncomp: Annotated[ 

56 int, 

57 Field( 

58 default=0, 

59 description="number of components in file", 

60 alias=None, 

61 json_schema_extra={ 

62 "units": None, 

63 "required": True, 

64 "examples": ["4"], 

65 }, 

66 ), 

67 ] 

68 

69 indices: Annotated[ 

70 list[int] | int | str, 

71 Field( 

72 default_factory=list, 

73 description="index values to use", 

74 alias=None, 

75 json_schema_extra={ 

76 "units": None, 

77 "required": True, 

78 "examples": ["[1, 2]"], 

79 }, 

80 ), 

81 ] 

82 

83 @field_validator("indices", mode="before") 

84 @classmethod 

85 def validate_indices(cls, value): 

86 """Ensure indices is a list of integers or a single integer.""" 

87 # Handle None or empty values 

88 if value is None: 

89 return [] 

90 

91 # Handle float values (convert to int first) 

92 if isinstance(value, float): 

93 return [int(value)] 

94 

95 if isinstance(value, str): 

96 # If it's a string, try to parse it as a list 

97 try: 

98 return [ 

99 int(i) for i in value.strip("[]").split(",") if i.strip().isdigit() 

100 ] 

101 except ValueError: 

102 raise ValueError(f"Invalid string format for indices: {value}") 

103 if isinstance(value, int): 

104 return [value] 

105 if isinstance(value, list): 

106 try: 

107 return [int(i) for i in value] 

108 except ValueError: 

109 raise ValueError(f"Invalid list format for indices: {value}") 

110 

111 # If we get here, the type is unsupported 

112 raise ValueError(f"Unsupported type for indices: {type(value)}, value: {value}")