def get_struct_rows(self, rows, struct=AUTO, skip_bad_rows=False, skip_bad_values=False, verbose=True): struct = Auto.delayed_acquire(struct, self.get_struct) if isinstance(struct, StructInterface): # actual approach converters = struct.get_converters('str', 'py') for r in rows: converted_row = list() for value, converter in zip(r, converters): converted_value = converter(value) converted_row.append(converted_value) yield converted_row.copy() else: # deprecated approach for r in rows: if skip_bad_rows: try: yield apply_struct_to_row( r, struct, False, logger=self if verbose else None) except ValueError: self.log(['Skip bad row:', r], verbose=verbose) else: yield apply_struct_to_row(r, struct, skip_bad_values, logger=self if verbose else None)
def get_ordered_meta_names(self, meta: Union[dict, Auto] = AUTO) -> Generator: meta = Auto.delayed_acquire(meta, self.get_meta) args = getfullargspec(self.__init__).args for k in args: if k in meta: yield k for k in meta: if k not in args: yield k
def __init__(self, name: str, field_type: FieldType = FieldType.Any, properties=None): field_type = Auto.delayed_acquire(field_type, FieldType.detect_by_name, field_name=name) field_type = FieldType.get_canonic_type(field_type, ignore_missing=True) assert isinstance( field_type, FieldType), 'Expected FieldType, got {}'.format(field_type) self._type = field_type super().__init__(name=name, data=properties)
def get_demo_example( self, count: Count = DEFAULT_EXAMPLE_COUNT, filters: Columns = None, columns: Columns = None, as_dataframe: AutoBool = AUTO, ) -> Union[DataFrame, list, None]: as_dataframe = Auto.delayed_acquire(as_dataframe, get_use_objects_for_output) sm_sample = self.filter(*filters) if filters else self sm_sample = sm_sample.to_record_stream() if as_dataframe and hasattr(sm_sample, 'get_dataframe'): return sm_sample.get_dataframe(columns) elif hasattr(sm_sample, 'get_list'): return sm_sample.get_list()
def __init__( self, name: str = AUTO, source: Source = None, context: Context = None, check: bool = True, ): name = Auto.delayed_acquire(name, get_generated_name, self._get_default_name_prefix()) if Auto.is_defined(context): if Auto.is_defined(source): source.set_context(context) else: source = context super().__init__(name=name, source=source, check=check) if Auto.is_defined(self.get_context()): self.put_into_context(check=check)
def convert( cls, obj: Union[EnumItem, Name], default: Union[EnumItem, Auto, None] = AUTO, skip_missing: bool = False, ): assert cls.is_prepared(), 'DynamicEnum must be prepared before usage' if isinstance(obj, cls): return obj for string in cls._get_name_and_value(obj): instance = cls.find_instance(string) if instance: return instance default = Auto.delayed_acquire(default, cls.get_default) if default: return cls.convert(default) elif not skip_missing: raise ValueError('item {} is not an instance of DynamicEnum {}'.format(obj, cls.get_enum_name()))
def delayed_acquire(current, func: Callable, *args, **kwargs): return Auto.delayed_acquire(current, func, *args, **kwargs)
def get_meta_items(self, meta: Union[dict, Auto] = AUTO) -> Generator: meta = Auto.delayed_acquire(meta, self.get_meta) for k in self.get_ordered_meta_names(meta): yield k, meta[k]