Format Functions
Format functions have the same signature as <built-in function format>.
These can be used to override the class variable
format_func in a subclass
or on an instance.
Boolean Values
- monotable.plugin.boolean(bool_value: bool, format_spec: str = 'T,F') str
Format function that formats the boolean values to user’s strings.
The format_spec is a string
'true-truth-value,false-truth-value'of the true and false truth value strings joined by comma where true-truth-value is returned when bool_value evaluates to logical True. The default value for argument format_spec above is a good example. If fspec or !fspec is rendered check for an incorrect format_spec.
Python Formatting Function Adapters
The Python 3 documentation links below show how to write the format spec for the monotable’s adapters to Python formatting functions.
function |
Python 3 Documentation |
|---|---|
sformat |
|
mformat |
same as sformat |
tformat |
|
pformat |
- monotable.plugin.sformat(value: Any, format_spec: str = '') str
Format function adapter to str.format().
Please keep in mind that only a single replacement field can be used.
- monotable.plugin.mformat(mapping: Mapping[str, Any], format_spec: str = '') str
Format function that selects values from a dictionary.
In the format_spec use references to keyword arguments described by Python Standard Library Format String Syntax that are keys in mapping.
For d = dict(key1=value1, key2=value2, …) A call to mformat(d, format_spec) behaves like calling: format_spec.format(key1=value1, key2=value2, …).
Example:
>>> format_spec = '{key1:.2f} {key2:}!' >>> print(format_spec.format(key1=25.9456, key2='spam')) 25.95 spam!
>>> from monotable.plugin import mformat >>> format_spec = '{key1:.2f} {key2:}!' # same as above >>> d = {'key1': 25.9456, 'key2': 'spam'} >>> print(mformat(d, format_spec)) 25.95 spam!
The keys must be strings but need not be valid python identifiers as shown here with a key that begins with a digit and a key containing a ‘-‘.
>>> from monotable.plugin import mformat >>> format_spec = '{0key1:.2f} {key-2:}!' # same as above >>> d = {'0key1': 25.9456, 'key-2': 'spam'} >>> print(mformat(d, format_spec)) 25.95 spam!
- monotable.plugin.pformat(value: Any, format_spec: str = '') str
Format function adapter to percent operator %.
The exact number % replacements in the printf-style format spec must be satisfied by items from value.
- monotable.plugin.tformat(value: Mapping[str, Any], format_spec: str = '') str
Format function adapter to string.Template.substitute().
Units Format Functions
These functions change the units of numeric values. They multiply or divide by a floating point number. The format_spec should be appropriate for type float.
- monotable.plugin.thousands(numeric_value: complex, format_spec: str = '') str
Format function that divides by 1.0e3.
- monotable.plugin.millions(numeric_value: complex, format_spec: str = '') str
Format function that divides by 1.0e6.
- monotable.plugin.billions(numeric_value: complex, format_spec: str = '') str
Format function that divides by 1.0e9.
- monotable.plugin.trillions(numeric_value: complex, format_spec: str = '') str
Format function that divides by 1.0e12.
- monotable.plugin.milli(numeric_value: complex, format_spec: str = '') str
Format function that multiplies by 1.0e3.
- monotable.plugin.micro(numeric_value: complex, format_spec: str = '') str
Format function that multiplies by 1.0e6.
- monotable.plugin.nano(numeric_value: complex, format_spec: str = '') str
Format function that multiplies by 1.0e9.
- monotable.plugin.pico(numeric_value: complex, format_spec: str = '') str
Format function that multiplies by 1.0e12.
- monotable.plugin.kibi(numeric_value: complex, format_spec: str = '') str
Format function that divides by 1024.
- monotable.plugin.mebi(numeric_value: complex, format_spec: str = '') str
Format function that divides by 1024^2.
- monotable.plugin.gibi(numeric_value: complex, format_spec: str = '') str
Format function that divides by 1024^3.
- monotable.plugin.tebi(numeric_value: complex, format_spec: str = '') str
Format function that divides by 1024^4.
References
Please refer to Wikipedia articles Unit_Prefix and Binary_Prefix.