structa.format
The structa.format
module contains various simple routines for “nicely”
formatting certain structures for output.
- structa.format.format_chars(chars, range_sep='-', list_sep='')[source]
Given a set of chars, returns a compressed string representation of all values in the set. For example:
>>> char_ranges({'a', 'b'}) 'ab' >>> char_ranges({'a', 'b', 'c'}) 'a-c' >>> char_ranges({'a', 'b', 'c', 'd', 'h'}) 'a-dh' >>> char_ranges({'a', 'b', 'c', 'd', 'h', 'i'}) 'a-dh-i'
range_sep and list_sep can be optionally specified to customize the strings used to separate ranges and lists of ranges respectively.
- structa.format.format_int(i)[source]
Reduce i by some appropriate power of 1000 and suffix it with an appropriate Greek qualifier (K for kilo, M for mega, etc). For example:
>>> format_int(0) '0' >>> format_int(10) '10' >>> format_int(1000) '1.0K' >>> format_int(1600) '1.6K' >>> format_int(2**32) '4.3G'
- structa.format.format_repr(self, **override)[source]
Returns a
repr()
style string for self in the formclass(name=value, name=value, ...)
.Note
At present, this function does not handle recursive structures unlike
reprlib.recursive_repr()
.
- structa.format.format_sample(value)[source]
Format a scalar value for output. The value can be a
str
,int
,float
,bool
,datetime
, orNone
.The result is a
str
containing a “nicely” formatted representation of the value. For example:>>> format_sample(1.0) '1' >>> format_sample(1.5) '1.5' >>> format_sample(200000000000) '200.0G' >>> format_sample(200000000000.0) '2e+11' >>> format_sample(None) 'null' >>> format_sample(False) 'false' >>> format_sample('foo') '"foo"' >>> format_sample(datetime.now()) '2021-08-16 14:05:04'