|
25 | 25 | from .daterange import DateRange |
26 | 26 | from .distributions import Normal, DataDistribution |
27 | 27 | from .nrange import NRange |
| 28 | +from .serialization import SerializableToDict |
28 | 29 | from .text_generators import TemplateGenerator |
29 | 30 | from .utils import ensure, coalesce_values |
30 | 31 | from .schema_parser import SchemaParser |
|
40 | 41 | RAW_VALUES_COMPUTE_METHOD] |
41 | 42 |
|
42 | 43 |
|
43 | | -class ColumnGenerationSpec(object): |
| 44 | +class ColumnGenerationSpec(SerializableToDict): |
44 | 45 | """ Column generation spec object - specifies how column is to be generated |
45 | 46 |
|
46 | 47 | Each column to be output will have a corresponding ColumnGenerationSpec object. |
@@ -119,7 +120,7 @@ def __init__(self, name, colType=None, minValue=0, maxValue=None, step=1, prefix |
119 | 120 | if EXPR_OPTION not in kwargs: |
120 | 121 | raise ValueError("Column generation spec must have `expr` attribute specified if datatype is inferred") |
121 | 122 |
|
122 | | - elif type(colType) == str: |
| 123 | + elif isinstance(colType, str): |
123 | 124 | colType = SchemaParser.columnTypeFromString(colType) |
124 | 125 |
|
125 | 126 | assert isinstance(colType, DataType), f"colType `{colType}` is not instance of DataType" |
@@ -299,6 +300,21 @@ def __init__(self, name, colType=None, minValue=0, maxValue=None, step=1, prefix |
299 | 300 | # set up the temporary columns needed for data generation |
300 | 301 | self._setupTemporaryColumns() |
301 | 302 |
|
| 303 | + def _toInitializationDict(self): |
| 304 | + """ Converts an object to a Python dictionary. Keys represent the object's |
| 305 | + constructor arguments. |
| 306 | + :return: Python dictionary representation of the object |
| 307 | + """ |
| 308 | + _options = self._csOptions.options.copy() |
| 309 | + _options["colName"] = _options.pop("name", self.name) |
| 310 | + _options["colType"] = _options.pop("type", self.datatype).simpleString() |
| 311 | + _options["kind"] = self.__class__.__name__ |
| 312 | + return { |
| 313 | + k: v._toInitializationDict() |
| 314 | + if isinstance(v, SerializableToDict) else v |
| 315 | + for k, v in _options.items() if v is not None |
| 316 | + } |
| 317 | + |
302 | 318 | def _temporaryRename(self, tmpName): |
303 | 319 | """ Create enter / exit object to support temporary renaming of column spec |
304 | 320 |
|
@@ -451,7 +467,7 @@ def setBaseColumnDatatypes(self, columnDatatypes): |
451 | 467 | assert type(columnDatatypes) is list, " `column_datatypes` parameter must be list" |
452 | 468 | ensure(len(columnDatatypes) == len(self.baseColumns), |
453 | 469 | "number of base column datatypes must match number of base columns") |
454 | | - self._baseColumnDatatypes = [].append(columnDatatypes) |
| 470 | + self._baseColumnDatatypes = columnDatatypes.copy() |
455 | 471 |
|
456 | 472 | def _setupTemporaryColumns(self): |
457 | 473 | """ Set up any temporary columns needed for test data generation. |
|
0 commit comments