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_bytes(value: bytes, print_options: _PrintOptions) -> LogBuffer: ''' Returns a LogBuffer containing a string representation of the bytes value. Args: value (bytes): The bytes value print_options (_PrintOptions): Output options Returns: LogBuffer: a LogBuffer ''' bytes_length = len(value) buff = LogBuffer(_maximum_data_output_width) buff.no_break_append('(') if type(value) == bytes: buff.no_break_append('bytes') elif type(value) == bytearray: buff.no_break_append('bytearray') if bytes_length >= _minimum_output_length: buff.no_break_append(' ') buff.no_break_append(_length_format.format(bytes_length)) buff.no_break_append(')[') is_multi_lines = bytes_length >= _bytes_count_in_line if is_multi_lines: buff.line_feed() buff.up_nest() chars = '' count = 0 for element in value: if count != 0 and count % _bytes_count_in_line == 0: if is_multi_lines: buff.no_break_append('| ') buff.no_break_append(chars) buff.line_feed() chars = '' if (count >= print_options.bytes_limit): buff.no_break_append(_limit_string) break buff.no_break_append('{:02X} '.format(element)) chars += chr(element) if element >= 0x20 and element <= 0x7E else '.' count += 1 if is_multi_lines: # padding full_length = 3 * _bytes_count_in_line current_length = buff.length if current_length == 0: current_length = full_length buff.no_break_append(' ' * (full_length - current_length)) buff.no_break_append('| ') buff.no_break_append(chars) if is_multi_lines: buff.line_feed() buff.down_nest() buff.no_break_append(']') return buff