def __init__( self, true: str = DEFAULT_TRUE_STR, false: str = DEFAULT_FALSE_STR, align_right: bool = False, min_len: Union[int, Auto] = AUTO, max_len: Union[int, Auto] = AUTO, including_framing: bool = False, crop: str = SHORT_CROP_SUFFIX, fill: str = FILL_CHAR, prefix: str = '', suffix: str = '', default: str = DEFAULT_STR, ): max_len = Auto.acquire( max_len, max(len(true), len(false), len(default), len(crop), Auto.acquire(min_len, 0))) min_len = Auto.acquire(min_len, max_len) self._true = true self._false = false super().__init__( min_len=min_len, max_len=max_len, including_framing=including_framing, fill=fill, crop=crop, prefix=prefix, suffix=suffix, align_right=align_right, default=default, )
def set_items(self, items: Iterable, inplace: bool, count: Optional[int] = None) -> Optional[Native]: if inplace: self.set_data(items, inplace=True) if Auto.is_defined(count): self._set_count(count, inplace=True) else: if Auto.is_defined(count): return self.make_new(items, count=count) else: return self.make_new(items)
def __init__(self, name: Name, value: Union[Value, Auto] = AUTO, update: bool = False): if update or not self._is_initialized(): name = get_name(name) if self._auto_value: value = Auto.acquire(value, name) self.name = name self.value = value
def get_context(self) -> Parent: parent = self.get_parent() if Auto.is_defined(parent, check_name=False): if parent.is_context(): return parent elif hasattr(parent, 'get_context'): return parent.get_context()
def fold_lists( list_items: Array, key_fields: Array, list_fields: Array, skip_missing: bool = False, item_type: Optional[ItemType] = None, ) -> Union[Row, Record]: if list_items: if not Auto.is_defined(item_type): first_item = list_items[0] item_type = ItemType.detect(first_item) if item_type == ItemType.Record: return fold_records(list_items, key_fields, list_fields, skip_missing=skip_missing) elif item_type in (ItemType.Row, ItemType.StructRow): return fold_rows(list_items, key_fields, list_fields, skip_missing=skip_missing) else: raise ValueError( 'fold_lists(): expected Record, Row or StructRow, got {}'. format(item_type)) elif not skip_missing: raise ValueError('fold_lists(): list_items must be non-empty')
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 __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 get_str_count(self, default: str = '(iter)') -> str: if hasattr(self, 'get_count'): count = self.get_count() else: count = None if Auto.is_defined(count): return str(count) else: return default
def func(item) -> Union[dict, tuple]: detected_type = item_type if not Auto.is_defined(detected_type): detected_type = ItemType.detect(item) return gr.fold_lists(item, keys, values, skip_missing=skip_missing, item_type=detected_type)
def get_records(self, columns: AutoColumns = AUTO) -> Generator: if Auto.is_defined(columns): available_columns = self.get_columns() for r in self.get_rows(): yield { k: v for k, v in zip(available_columns, r) if k in columns } else: yield from self.get_items_of_type(item_type=ItemType.Record)
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 get_cropped(self, line: str) -> str: max_len = self.get_max_total_len() if Auto.is_defined(max_len): if max_len < len(line): crop_str = self.get_crop_str() crop_len = max_len - len(crop_str) if crop_len > 0: line = line[:crop_len] + crop_str else: line = crop_str[:-crop_len] return line
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_enumerated_items(self, item_type=AUTO) -> Generator: if item_type == 'Any' or item_type == 'Any' or not Auto.is_defined( item_type): items = self.get_items() elif hasattr(self, 'get_items_of_type'): items = self.get_items_of_type(item_type) else: items = self.get_items() if hasattr(self, 'get_item_type'): received_item_type = self.get_item_type() assert item_type == received_item_type, '{} != {}'.format( item_type, received_item_type) for n, i in enumerate(items): yield n, i
def __init__( self, align_right: bool = False, min_len: Count = AUTO, max_len: Count = AUTO, including_framing: bool = False, crop: str = CROP_SUFFIX, fill: str = FILL_CHAR, prefix: str = '', suffix: str = '', default: str = DEFAULT_STR, ): if Auto.is_defined(max_len): assert len( crop ) <= max_len, 'Expected len(crop) <= max_len, got len({}) > {}'.format( crop, max_len) self._min_len = min_len self._max_len = max_len self._crop = crop self._fill = fill self._align_right = align_right self._prefix = prefix self._suffix = suffix self._default = default if including_framing: framing_len = self.get_framing_len() if Auto.is_defined(max_len): assert max_len >= framing_len, 'Expected max_len >= framing_len, got {}<{}'.format( max_len, framing_len) self._max_len -= framing_len if Auto.is_defined(min_len): if min_len > framing_len: self._min_len -= framing_len else: self._min_len = 0
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 _assert_is_appropriate_child( cls, obj, msg: Union[str, Auto] = AUTO, skip_missing: bool = False, ) -> None: if not cls._is_appropriate_child(obj, skip_missing=skip_missing): if not Auto.is_defined(msg): template = '{}._assert_is_appropriate_child({}): Expected child as {} instance, got {}' expected_class_names = [ c.__name__ for c in cls.get_child_obj_classes() ] msg = template.format(cls.__name__, obj, ', '.join(expected_class_names), type(obj)) raise TypeError(msg)
def set_context(self, context: ContextInterface, reset: bool = True, inplace: bool = True) -> Native: if inplace: if self._is_stub_instance(context): return self elif self._has_context_as_source(): if reset: assert isinstance(context, ContextInterface) assert isinstance(context, AbstractNamed) self.set_source(context) elif Auto.is_defined(self.get_source()): self.get_source().set_context(context, reset=reset) return self.put_into_context(check=not reset) else: contextual = self.set_outplace(context=context) return self._assume_native(contextual)
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 take(self, count: Union[int, bool] = 1, inplace: bool = False) -> Optional[Native]: if (count and isinstance(count, bool) ) or not Auto.is_defined(count): # True, None, AUTO return self elif isinstance(count, int): if count > 0: items = self._get_first_items(count) elif count < 0: items = self._get_last_items(-count) else: # count in (0, False) items = list() result_count = None if self.is_in_memory(): if not is_in_memory(items): items = list(items) if self._has_count_attribute(): result_count = len(items) return self.set_items(items, count=result_count, inplace=inplace) else: raise TypeError( 'Expected count as int or boolean, got {}'.format(count))
def get_items(self, item_type: Union[ItemType, Auto] = AUTO) -> Iterable: if Auto.is_defined(item_type): return self.get_items_of_type(item_type) else: return self.get_stream_data()
def is_defined(obj, check_name: bool = True) -> bool: return Auto.is_defined(obj, check_name=check_name)
def get_str_count(self, default: str = '(iter)') -> str: count = self.get_count() if Auto.is_defined(count): return str(count) else: return default
def delayed_acquire(current, func: Callable, *args, **kwargs): return Auto.delayed_acquire(current, func, *args, **kwargs)
def simple_acquire(current, default): return Auto.simple_acquire(current, default)
def acquire(current, default, delayed=False, *args, **kwargs): return Auto.acquire(current, default, delayed=delayed, *args, **kwargs)
def get_max_value_len(self, or_min: bool = True) -> Count: max_value_len = self._max_len if Auto.is_auto(max_value_len): return self.get_min_value_len(or_max=False) if or_min else None else: return max_value_len
def get_count_repr(self, default: str = '<iter>') -> str: count = self.get_str_count() if not Auto.is_defined(count): count = default return '{} items'.format(count)
def is_defined(self) -> bool: return Auto.is_defined(self.get_name())
get_name, get_names, get_generated_name, DEFAULT_RANDOM_LEN, get_list, get_optional_len, update, is_in_memory, is_generator, get_str_from_args_kwargs, get_str_from_annotation, ) from ..functions.primary.text import str_to_bool, is_absolute_path, is_mask, is_formatter NOT_USED = None # deprecated DEFAULT_VALUE = Auto.get_value() DefaultArgument = Auto DEFAULT = AUTO def apply(func: Callable, *args, **kwargs): return func(*args, **kwargs) def is_defined(obj, check_name: bool = True) -> bool: return Auto.is_defined(obj, check_name=check_name) def simple_acquire(current, default):