Coverage for presidio_structured / data / data_reader.py: 71%
17 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-03-29 08:58 +0000
« prev ^ index » next coverage.py v7.13.1, created at 2026-03-29 08:58 +0000
1"""Helper data classes, mostly simple wrappers to ensure consistent user interface."""
3import json
4from abc import ABC, abstractmethod
5from pathlib import Path
6from typing import Any, Dict, Union
8import pandas as pd
11class ReaderBase(ABC):
12 """
13 Base class for data readers.
15 This class should not be instantiated directly, instead init a subclass.
16 """
18 @abstractmethod
19 def read(self, path: Union[str, Path], **kwargs) -> Any:
20 """
21 Extract data from file located at path.
23 :param path: String defining the location of the file to read.
24 :return: The data read from the file.
25 """
26 pass
29class CsvReader(ReaderBase):
30 """
31 Reader for reading csv files.
33 Usage::
35 reader = CsvReader()
36 data = reader.read(path="filepath.csv")
38 """
40 def read(self, path: Union[str, Path], **kwargs) -> pd.DataFrame:
41 """
42 Read csv file to pandas dataframe.
44 :param path: String defining the location of the csv file to read.
45 :return: Pandas DataFrame with the data read from the csv file.
46 """
47 return pd.read_csv(path, **kwargs)
50class JsonReader(ReaderBase):
51 """
52 Reader for reading json files.
54 Usage::
56 reader = JsonReader()
57 data = reader.read(path="filepath.json")
59 """
61 def read(self, path: Union[str, Path], **kwargs) -> Dict[str, Any]:
62 """
63 Read json file to dict.
65 :param path: String defining the location of the json file to read.
66 :return: dictionary with the data read from the json file.
67 """
68 with open(path) as f:
69 data = json.load(f, **kwargs)
70 return data