Options to read from CSV files. Available options are: - column delimiters - include a subset of columns - types of each columns - whether the file contains a header
| 42 | |
| 43 | |
| 44 | class CSVOptions(object): |
| 45 | """Options to read from CSV files. |
| 46 | Available options are: |
| 47 | - column delimiters |
| 48 | - include a subset of columns |
| 49 | - types of each columns |
| 50 | - whether the file contains a header |
| 51 | """ |
| 52 | |
| 53 | def __init__(self) -> None: |
| 54 | # Field delimiter |
| 55 | self.delimiter = "," |
| 56 | |
| 57 | # If non-empty, indicates the names of columns from the CSV file that should |
| 58 | # be actually read and converted (in the list's order). |
| 59 | # Columns not in this list will be ignored. |
| 60 | self.include_columns = [] |
| 61 | # Optional per-column types (disabling type inference on those columns) |
| 62 | self.column_types = [] |
| 63 | # include_columns always contains id column for v, src id and dst id column for e |
| 64 | # if it contains and only contains those id columns, we suppose user actually want to |
| 65 | # read all other properties. (Otherwise they should specify at least one property) |
| 66 | self.force_include_all = False |
| 67 | |
| 68 | # If true, column names will be read from the first CSV row |
| 69 | # If false, column names will be of the form "f0", "f1"... |
| 70 | self.header_row = True |
| 71 | self.filetype = "CSV" |
| 72 | |
| 73 | def to_dict(self) -> Dict: |
| 74 | options = {} |
| 75 | options["delimiter"] = self.delimiter |
| 76 | options["header_row"] = self.header_row |
| 77 | if self.include_columns: |
| 78 | options["schema"] = ",".join(self.include_columns) |
| 79 | if self.column_types: |
| 80 | cpp_types = [utils.data_type_to_cpp(dt) for dt in self.column_types] |
| 81 | options["column_types"] = ",".join(cpp_types) |
| 82 | if self.force_include_all: |
| 83 | options["include_all_columns"] = self.force_include_all |
| 84 | options["filetype"] = self.filetype |
| 85 | return options |
| 86 | |
| 87 | def __str__(self) -> str: |
| 88 | return "&".join(["{}={}".format(k, v) for k, v in self.to_dict().items()]) |
| 89 | |
| 90 | def __repr__(self) -> str: |
| 91 | return self.__str__() |
| 92 | |
| 93 | |
| 94 | class Loader(object): |