def _to_string_refrection(value: object, print_options: _PrintOptions) -> LogBuffer: ''' Returns a LogBuffer containing a string representation of the value with reflection. Args: value (bytes): The value to append print_options (_PrintOptions): Output options Returns: LogBuffer: a LogBuffer ''' buff = LogBuffer(_maximum_data_output_width) buff.append(_get_type_name(value)) body_buff = _to_string_refrection_body(value, print_options) is_multi_lines = body_buff.is_multi_lines or buff.length + body_buff.length > _maximum_data_output_width buff.no_break_append('{') if is_multi_lines: buff.line_feed() buff.up_nest() buff.append_buffer(None, body_buff) if is_multi_lines: if buff.length > 0: buff.line_feed() buff.down_nest() buff.no_break_append('}') return buff
def _to_string_iterable(values: object, print_options: _PrintOptions) -> LogBuffer: ''' Returns a LogBuffer containing a string representation of the iterable value. Args: value (object): The iterable value to append print_options (_PrintOptions): Output options Returns: LogBuffer: a LogBuffer ''' open_char = '{' # set, frozenset, dict close_char = '}' if isinstance(values, list): # list open_char = '[' close_char = ']' elif isinstance(values, tuple): # tuple open_char = '(' close_char = ')' buff = LogBuffer(_maximum_data_output_width) buff.append(_get_type_name(values, len(values))) buff.no_break_append(open_char) body_buff = _to_string_iterable_body(values, print_options) # 1.1.0 if open_char == '(' and len(values) == 1: # A tuple with 1 element body_buff.no_break_append(',') #### is_multi_lines = body_buff.is_multi_lines or buff.length + body_buff.length > _maximum_data_output_width if is_multi_lines: buff.line_feed() buff.up_nest() buff.append_buffer(None, body_buff) if is_multi_lines: buff.line_feed() buff.down_nest() buff.no_break_append(close_char) return buff
def _to_string_refrection_body(value: object, print_options: _PrintOptions) -> LogBuffer: ''' Returns a LogBuffer containing the body of a string representation of the value with reflection. Args: value (bytes): The value to append print_options (_PrintOptions): Output options Returns: LogBuffer: a LogBuffer ''' buff = LogBuffer(_maximum_data_output_width) members = [] try: base_members = inspect.getmembers( value, lambda v: not inspect.isclass(v) and (print_options.output_method or not inspect.ismethod(v) ) and not inspect.isbuiltin(v)) members = [ m for m in base_members if (not m[0].startswith('__') or not m[0].endswith('__')) and ( print_options.output_private or not m[0].startswith('_')) ] except BaseException as ex: buff.append(str(ex)) return buff was_multi_lines = False index = 0 for member in members: if index > 0: buff.no_break_append(', ') name = member[0] value = member[1] member_buff = LogBuffer(_maximum_data_output_width) member_buff.append(name) member_buff.append_buffer(_key_value_separator, _to_string(None, value, print_options)) if index > 0 and (was_multi_lines or member_buff.is_multi_lines): buff.line_feed() buff.append_buffer(None, member_buff) was_multi_lines = member_buff.is_multi_lines index += 1 return buff
def _to_string_iterable_body(values: object, print_options: _PrintOptions) -> LogBuffer: ''' Returns a LogBuffer containing the body of a string representation of the iterable value. Args: value (object): The iterable value to append print_options (_PrintOptions): Output options Returns: LogBuffer: a LogBuffer ''' buff = LogBuffer(_maximum_data_output_width) was_multi_lines = False index = 0 for element in values: if index > 0: buff.no_break_append(', ') if index >= print_options.collection_limit: buff.append(_limit_string) break element_buff = LogBuffer(_maximum_data_output_width) if isinstance(values, dict): # dictionary element_buff = _to_string_key_value(element, values[element], print_options) else: # list, set, frozenset or tuple element_buff = _to_string(None, element, print_options) if index > 0 and (was_multi_lines or element_buff.is_multi_lines): buff.line_feed() buff.append_buffer(None, element_buff) was_multi_lines = element_buff.is_multi_lines index += 1 # 1.1.0 if isinstance(values, dict) and len(values) == 0: buff.no_break_append(':') #### return buff
def _to_string_key_value(key: object, value: object, print_options: _PrintOptions) -> LogBuffer: ''' Returns a LogBuffer containing a string representation of the the key and value. Args: key (object): The key related to the value value (object): The value print_options (_PrintOptions): Output options Returns: LogBuffer: a LogBuffer ''' buff = LogBuffer(_maximum_data_output_width) key_buff = _to_string(None, key, print_options) value_buff = _to_string(None, value, print_options) buff.append_buffer(None, key_buff).append_buffer(_key_value_separator, value_buff) return buff
def _to_string(name: str, value: object, print_options: _PrintOptions) -> LogBuffer: ''' Returns a LogBuffer containing a string representation of the the name and value. Args: name (str): The name related to the value value (object): The value print_options (_PrintOptions): Output options Returns: LogBuffer: a LogBuffer ''' buff = LogBuffer(_maximum_data_output_width) separator = None if name is not None: buff.append(name) separator = _varname_value_separator if value is None: # None buff.no_break_append(separator).append('None') elif isinstance(value, str): # str value_buff = _to_string_str(value, print_options) buff.append_buffer(separator, value_buff) elif isinstance(value, bytes) or isinstance(value, bytearray): # bytes value_buff = _to_string_bytes(value, print_options) buff.append_buffer(separator, value_buff) elif isinstance(value, int) or isinstance(value, float) or \ isinstance(value, datetime.date) or isinstance(value, datetime.time) or \ isinstance(value, datetime.datetime): # int, float, datetime.date, datetime.time, datetime.datetime buff.no_break_append(separator).append(str(value)) elif isinstance(value, list) or \ isinstance(value, set) or isinstance(value, frozenset) or \ isinstance(value, tuple) or \ isinstance(value, dict): # list, set, frozenset, tuple, dict value_buff = _to_string_iterable(value, print_options) buff.append_buffer(separator, value_buff) else: has_str, has_repr = _has_str_repr_method(value) value_buff = LogBuffer(_maximum_data_output_width) if not print_options.force_reflection and (has_str or has_repr): # has __str__ or __repr__ method if has_repr: value_buff.append('repr(): ') value_buff.no_break_append(repr(value)) else: value_buff.append('str(): ') value_buff.no_break_append(str(value)) buff.append_buffer(separator, value_buff) else: # use refrection if any(map(lambda obj: value is obj, _reflected_objects)): # cyclic reference value_buff.no_break_append(_cyclic_reference_string) elif len(_reflected_objects) > print_options.reflection_nest_limit: # over reflection level limitation value_buff.no_break_append(_limit_string) else: _reflected_objects.append(value) value_buff = _to_string_refrection(value, print_options) _reflected_objects.pop() buff.append_buffer(separator, value_buff) return buff