def get_table_by_file_id(self, file_id, format='csv') -> Table: """Get a table that has been saved to Box in csv or JSON format. `Args`: file_id: str The Box file_id of the table to be retrieved. format: str Format in which Table has been saved; for now, only 'csv' or 'json'. `Returns`: Table A Parsons Table. """ if format not in self.ALLOWED_FILE_FORMATS: raise ValueError( f'Format argument to upload_table() must be in one ' f'of {self.ALLOWED_FILE_FORMATS}; found "{format}"') # Temp file will be around as long as enclosing process is running, # which we need, because the Table we return will continue to use it. output_file_name = create_temp_file() with open(output_file_name, 'wb') as output_file: self.client.file(file_id).download_to(output_file) if format == 'csv': return Table.from_csv(output_file_name) elif format == 'json': return Table.from_json(output_file_name) else: raise SystemError(f'Got (theoretically) impossible ' f'format option "{format}"') # pragma: no cover
def test_to_from_json_line_delimited_compressed(self): path = 'tmp/test.json.gz' self.tbl.to_json(path, line_delimited=True) result_tbl = Table.from_json(path, line_delimited=True) assert_matching_tables(self.tbl, result_tbl) os.remove(path)
def test_to_from_json_compressed(self): path = 'tmp/test.json.gz' self.tbl.to_json(path) result_tbl = Table.from_json(path) assert_matching_tables(self.tbl, result_tbl) os.remove(path)
def test_to_from_temp_json_compressed(self): path = self.tbl.to_json(temp_file_compression='gzip') result_tbl = Table.from_json(path) assert_matching_tables(self.tbl, result_tbl)
def test_to_from_temp_json(self): path = self.tbl.to_json() result_tbl = Table.from_json(path) assert_matching_tables(self.tbl, result_tbl)