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

1"""Helper data classes, mostly simple wrappers to ensure consistent user interface.""" 

2 

3import json 

4from abc import ABC, abstractmethod 

5from pathlib import Path 

6from typing import Any, Dict, Union 

7 

8import pandas as pd 

9 

10 

11class ReaderBase(ABC): 

12 """ 

13 Base class for data readers. 

14 

15 This class should not be instantiated directly, instead init a subclass. 

16 """ 

17 

18 @abstractmethod 

19 def read(self, path: Union[str, Path], **kwargs) -> Any: 

20 """ 

21 Extract data from file located at path. 

22 

23 :param path: String defining the location of the file to read. 

24 :return: The data read from the file. 

25 """ 

26 pass 

27 

28 

29class CsvReader(ReaderBase): 

30 """ 

31 Reader for reading csv files. 

32 

33 Usage:: 

34 

35 reader = CsvReader() 

36 data = reader.read(path="filepath.csv") 

37 

38 """ 

39 

40 def read(self, path: Union[str, Path], **kwargs) -> pd.DataFrame: 

41 """ 

42 Read csv file to pandas dataframe. 

43 

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) 

48 

49 

50class JsonReader(ReaderBase): 

51 """ 

52 Reader for reading json files. 

53 

54 Usage:: 

55 

56 reader = JsonReader() 

57 data = reader.read(path="filepath.json") 

58 

59 """ 

60 

61 def read(self, path: Union[str, Path], **kwargs) -> Dict[str, Any]: 

62 """ 

63 Read json file to dict. 

64 

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