def read_data_set( cls, dirpath: str, filesystem: FsWrapperBase = FsPathlibWrapper()) -> "DataSet": """Reads a DataSet from disk. A DataSet is given by a directory containing a collection of files, each of which represents a PublisherDataSet. The name associated to the DataSet object is the last component of the dirpath. Args: dirpath: Directory containing the PublisherDataSets that comprise this DataSet. filesystem: The filesystem object that manages all file operations. Returns: The DataSet object representing the contents of this directory. """ pdf_list = [] for filepath in sorted(filesystem.glob(dirpath, "*")): if filesystem.is_file(filepath): with filesystem.open(filepath) as file: try: pdf = PublisherData.read_publisher_data(file) pdf.name = str(filepath) pdf_list.append(pdf) except (ValueError, RuntimeError) as e: raise RuntimeError( "In publisher file {}".format(filepath)) from e return cls(pdf_list, filesystem.name(dirpath))
def test_read_and_write_publisher_data(self): pdf = PublisherData([(1, 0.01), (2, 0.02), (1, 0.04)], "test") with TemporaryDirectory() as d: filename = join(d, "pdf_data") pdf_file = open(filename, "w") pdf.write_publisher_data(pdf_file) pdf_file.close() new_file = open(filename) new_pdf = PublisherData.read_publisher_data(new_file) self.assertEqual(new_pdf.max_impressions, 3) self.assertEqual(new_pdf.max_spend, 0.04) self.assertEqual(new_pdf.max_reach, 2) new_file.close()