def get_output(self, output: AutoOutput = AUTO) -> Optional[Class]: if Auto.is_defined(output) and not isinstance(output, LoggingLevel): return output if hasattr(self, 'get_logger'): logger = self.get_logger() if Auto.is_defined(logger): return logger elif Auto.is_auto(output): return print
def get_str_count(self, default: Optional[str] = '(iter)') -> Optional[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 get_err_msg(cls, scale: Union[str, DynamicEnum] = '{}', available_scales=AUTO): list_available_scales = Auto.delayed_acquire(available_scales, cls.get_enum_items) if Auto.is_defined(list_available_scales): str_available_scales = ' or '.join(map(str, list_available_scales)) else: str_available_scales = '{}' return 'Expected time-scale {}, got {}'.format(str_available_scales, scale)
def get_date_from_month_abs( month_abs: int, min_date: Union[Date, Auto] = AUTO, as_iso_date: bool = True, ) -> Date: if Auto.is_defined(min_date): min_year = get_year_from_date(min_date) else: min_year = get_min_year() year_delta = int((month_abs - 1) / MONTHS_IN_YEAR) year_no = min_year + year_delta month_no = month_abs - year_delta * MONTHS_IN_YEAR cur_date = get_date_from_year_and_month(year=year_no, month=month_no, as_iso_date=as_iso_date) return cur_date
def get_meta_records(self) -> Generator: init_defaults = self._get_init_defaults() for key, value in self.get_meta_items(): actual_type = type(value).__name__ expected_type = self._get_init_types().get(key) if hasattr(expected_type, '__name__'): expected_type = expected_type.__name__ else: expected_type = str(expected_type).replace('typing.', '') default = init_defaults.get(key) yield dict( key=key, value=self._get_value_repr(value), default=self._get_value_repr(default), actual_type=actual_type, expected_type=expected_type or '-', defined='-' if value == default else '+' if Auto.is_defined(value) else 'x', )
def get_data_description( self, count: int = DEFAULT_ROWS_COUNT, title: Optional[str] = 'Data:', max_len: int = JUPYTER_LINE_LEN, ) -> Generator: if title: yield title if hasattr(self, 'get_data_caption'): yield self.get_data_caption() if hasattr(self, 'get_data'): data = self.get_data() if data: shape_repr = self.get_shape_repr() if shape_repr: yield 'First {count} data items from {shape}:'.format(count=count, shape=shape_repr) if isinstance(data, dict): records = map( lambda i: dict(key=i[0], value=i[1], defined='+' if Auto.is_defined(i[1]) else '-'), data.items(), ) yield from self._get_columnar_lines( records, columns=DICT_DESCRIPTION_COLUMNS, count=count, max_len=max_len, ) elif isinstance(data, Iterable): for n, item in enumerate(data): if n >= count: break line = ' - ' + str(item) yield line[:max_len] elif isinstance(data, DescribeMixin) or hasattr(data, 'get_meta_description'): for line in data.get_meta_description(): yield line else: line = str(data) yield line[:max_len] else: yield '(data attribute is empty)' else: yield '(data attribute not found)'
def get_monday_date(d: Date, as_iso_date: AutoBool = AUTO) -> Date: cur_date = get_date(d) if not Auto.is_defined(as_iso_date): as_iso_date = is_iso_date(d) monday_date = cur_date + timedelta(days=-cur_date.weekday()) return to_date(monday_date, as_iso_date)
def get_count_repr(self, default: str = '<iter>') -> str: count = self.get_str_count(default=default) if not Auto.is_defined(count): count = default return '{} items'.format(count)