1
0
Fork 0

Moved read_json_file() function outside of class JsonImporter

master
Meliurwen 3 years ago
parent d568ad20e4
commit 0bfca02a61
  1. 30
      src/pyctbn/legacy/utility/json_importer.py

@ -10,6 +10,19 @@ import pandas as pd
from .abstract_importer import AbstractImporter from .abstract_importer import AbstractImporter
def read_json_file(file_path) -> typing.List:
"""Reads the JSON file in the path self.filePath.
:return: The contents of the json file
:rtype: List
"""
with open(file_path) as f:
data = json.load(f)
if (isinstance(data,list)):
return data
else:
return [data]
class JsonImporter(AbstractImporter): class JsonImporter(AbstractImporter):
"""Implements the abstracts methods of AbstractImporter and adds all the necessary methods to process and prepare """Implements the abstracts methods of AbstractImporter and adds all the necessary methods to process and prepare
@ -52,7 +65,7 @@ class JsonImporter(AbstractImporter):
self._df_samples_list = None self._df_samples_list = None
self._array_indx = None self._array_indx = None
super(JsonImporter, self).__init__(file_path) super(JsonImporter, self).__init__(file_path)
self._raw_data = self.read_json_file() self._raw_data = read_json_file(file_path)
def import_data(self, indx: int = 0) -> None: def import_data(self, indx: int = 0) -> None:
"""Implements the abstract method of :class:`AbstractImporter`. """Implements the abstract method of :class:`AbstractImporter`.
@ -101,21 +114,6 @@ class JsonImporter(AbstractImporter):
""" """
return self.one_level_normalizing(raw_data, self._array_indx, self._variables_label) return self.one_level_normalizing(raw_data, self._array_indx, self._variables_label)
def read_json_file(self) -> typing.List:
"""Reads the JSON file in the path self.filePath.
:return: The contents of the json file
:rtype: List
"""
with open(self._file_path) as f:
data = json.load(f)
if (isinstance(data,list)):
return data
else:
return [data]
def one_level_normalizing(self, raw_data: typing.List, indx: int, key: str) -> pd.DataFrame: def one_level_normalizing(self, raw_data: typing.List, indx: int, key: str) -> pd.DataFrame:
"""Extracts the one-level nested data in the list ``raw_data`` at the index ``indx`` at the key ``key``. """Extracts the one-level nested data in the list ``raw_data`` at the index ``indx`` at the key ``key``.