Readers API
src.readers ¶
Reader initialization and registration.
__all__ = ['BaseReader', 'SQLiteReader', 'CSVReader', 'XMLReader'] module-attribute ¶
BaseReader ¶
Bases: ABC
Abstract base class for all file format readers.
This class provides core functionality for reading diabetes device data files and automatic reader selection based on file types. It handles timestamp processing, data validation, and resource management.
Source code in src/readers/base.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 | |
file_path = path instance-attribute ¶
file_config = file_config instance-attribute ¶
register(file_type: FileType) -> Callable[[Type[T]], Type[T]] classmethod ¶
Register a reader class for a specific file type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_type | FileType | FileType enum value to associate with the reader | required |
Returns:
| Name | Type | Description |
|---|---|---|
Callable | Callable[[Type[T]], Type[T]] | Decorator function that registers the reader class |
Source code in src/readers/base.py
get_reader_for_format(fmt: DeviceFormat, file_path: Path) -> BaseReader classmethod ¶
Get appropriate reader instance for the detected format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fmt | DeviceFormat | Detected device format specification | required |
file_path | Path | Path to the data file | required |
Returns:
| Type | Description |
|---|---|
BaseReader | Instance of appropriate reader class |
Raises:
| Type | Description |
|---|---|
ReaderError | If no reader is registered for the file type |
Source code in src/readers/base.py
__init__(path: Path, file_config: FileConfig) ¶
initialise reader with file path and configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path | Path | Path to the data file | required |
file_config | FileConfig | Configuration for the file format | required |
Raises:
| Type | Description |
|---|---|
ValueError | If file does not exist |
Source code in src/readers/base.py
__enter__() ¶
__exit__(exc_type, exc_val, exc_tb) ¶
read_table(table_structure: TableStructure) -> Optional[TableData] abstractmethod ¶
Read and process a single table according to its structure.
This method must be implemented by each specific reader.
read_all_tables() -> Dict[str, TableData] ¶
Read and process all tables defined in the file configuration.
Source code in src/readers/base.py
detect_timestamp_format(series: pd.Series) -> Tuple[TimestampType, dict] ¶
Detect timestamp format using a small deterministic heuristic.
Strategy (simple & fast):
- sample up to 50 non-null values
- detect numeric epochs first
- look for obvious ISO-like markers (T, Z, timezone offsets)
- for hyphen/slash date starts try explicit day-first formats if day>12 found
- attempt a compact list of explicit formats with a high-acceptance threshold
- fallback to pandas inference (utc=True) if explicit formats fail
Returns (TimestampType, parse_kwargs) where parse_kwargs is suitable for passing to pd.to_datetime (e.g., {'format': ..., 'utc': True} or {'dayfirst': True,'utc':True}).
Source code in src/readers/base.py
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 | |
CSVReader ¶
Bases: BaseReader
Reads and processes CSV files according to the provided format configuration.
Source code in src/readers/csv.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | |
__init__(path: Path, file_config: FileConfig) ¶
read_table(table_structure: TableStructure) -> Optional[TableData] ¶
Read and process a single table according to its structure.
For CSV files, we treat each file as a single table, reading all data at once and caching it for subsequent operations if needed.
Source code in src/readers/csv.py
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | |
SQLiteReader ¶
Bases: BaseReader
Reads and processes SQLite files according to the provided format configuration.
Source code in src/readers/sqlite.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | |
engine property ¶
Lazy initialisation of database engine.
__init__(path: Path, file_config: FileConfig) ¶
read_table(table_structure: TableStructure) -> Optional[TableData] ¶
Read and process a single table according to its structure.
Source code in src/readers/sqlite.py
XMLReader ¶
Bases: BaseReader
Reads and processes XML files according to the provided format configuration.
Source code in src/readers/xml.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | |
__init__(path: Path, file_config: FileConfig) ¶
read_table(table_structure: TableStructure) -> Optional[TableData] ¶
Read and process a single table according to its structure.
For XML files, each table is expected to be contained within elements matching the table name or a configured xpath.