Coverage for /home/runner/work/pycax/pycax/pycax/datasets/datasets.py: 100%

21 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2024-01-26 00:27 +0000

1""" 

2ca/tables/ API endpoints. These are the available data sets with meta data, e.g. name and id. 

3""" 

4 

5import pandas as pd 

6 

7from pycax.caxutils import build_api_url, cax_baseurl, cax_GET 

8 

9def get(args={}, **kwargs): 

10 """ 

11 Get the table of data sets 

12 

13 :param args: [dict] a dictionary of query parameters. The default is no parameters which returns the full table. 

14 :return: A Tables Response of the JSON 

15 

16 Usage:: 

17 

18 from pycax import tables 

19 query = tables.get() 

20 res = query.execute() 

21 query.to_pandas() 

22 """ 

23 url = cax_baseurl + "ca/tables" 

24 

25 # returns a DatasetsResponse object 

26 return DatasetsResponse(url, args) 

27 

28def getdf(args={}, **kwargs): 

29 """ 

30 Make query for the data sets and return a pandas DataFrame 

31 

32 :param args: [dict] a dictionary of query parameters. The default is no parameters which returns the full table. 

33 :return: A pandas DataFrame 

34 

35 Usage:: 

36 

37 from pycax import tables 

38 datasets.getdf() 

39 """ 

40 query = get(args=args, **kwargs) 

41 query.execute() 

42 

43 return query.to_pandas() 

44 

45class DatasetsResponse: 

46 """ 

47 An CAX Datasets Response Object 

48 """ 

49 

50 def __init__(self, url, args): 

51 """ 

52 Initialise the object parameters 

53 """ 

54 # public members 

55 self.data = None 

56 self.api_url = build_api_url(url, args) 

57 

58 # private members 

59 self.__args = args 

60 self.__url = url 

61 

62 def execute(self, **kwargs): 

63 """ 

64 Execute or fetch the data based on the query 

65 """ 

66 out = cax_GET( 

67 self.__url, self.__args, **kwargs 

68 ) 

69 self.data = out 

70 return self.data 

71 

72 def to_pandas(self): 

73 """ 

74 Convert the results into a pandas DataFrame 

75 """ 

76 return pd.DataFrame(self.data["tables"])