dbprocessing.DBstrings.DBformatter¶
- class dbprocessing.DBstrings.DBformatter[source]¶
String formatter extended/modified for DButils
Notes
As this is currently implemented,
re()
may not handle{{
and}}
properly, since regex expansion and basic formatting occur in two different steps, and thus{{
and}}
are already replaced with{
and}
in the second step.- __init__(*args, **kwargs)¶
Methods
assemble
(literal, field, format, conversion)Assembles components of a field specification
check_unused_args
(used_args, args, kwargs)convert_field
(value, conversion)expand_datetime
(kwargs)Expands datetime keyword into special keywords.
expand_format
(format_string[, kwargs])Add formatting codes to 'special' fields in format string.
format
(format_string, *args, **kwargs)Expand base format to handle datetime and special dbp keywords
format_field
(value, format_spec)get_field
(field_name, args, kwargs)get_value
(key, args, kwargs)parse
(format_string)re
(format_string, *args, **kwargs)Format with regexp for unspecified fields.
vformat
(format_string, args, kwargs)Attributes
- assemble(literal, field, format, conversion)[source]¶
Assembles components of a field specification
Converse of parse. Takes literal text, field name, format spec, and conversion and assembles into a full field spec.
- Parameters
- Returns
str
A full format spec that will parse into
literal
,field
,format
,conversion
Examples
>>> f = dbprocessing.DBstrings.DBformatter() >>> parsed = list(f.parse('The year is {Y}')) >>> parsed[0] ('The year is ', 'Y', '', None) >>> f.assemble(*parsed[0]) 'The year is {Y}'
- check_unused_args(used_args, args, kwargs)¶
- convert_field(value, conversion)¶
- expand_datetime(kwargs)[source]¶
Expands datetime keyword into special keywords. Helper function!
A single datetime keyword may be provided to
format()
; this function expands that datetime keyword into all the fields that may be provided by the datetime object and inserts those keywords intokwargs
.Examples
>>> kwargs = { 'datetime': datetime.datetime(2010, 1, 1) } >>> dbprocessing.DBstrings.DBformatter().expand_datetime(kwargs) >>> kwargs {'DATE': '20100101', 'H': 0, 'M': 0, 'MICRO': 0, 'MILLI': 0, 'S': 0, 'Y': 2010, 'b': 'Jan', 'd': 1, 'datetime': datetime.datetime(2010, 1, 1, 0, 0), 'j': 1, 'm': 1, 'y': 10}
- expand_format(format_string, kwargs=None)[source]¶
Add formatting codes to ‘special’ fields in format string.
Helper function!
For every field defined in
SPECIAL_FIELDS
, if there is no format spec nor conversion specified, replace it on the output with the full format spec inSPECIAL_FIELDS
.If the format spec/conversion is not provided or matches that in
SPECIAL_FIELDS
, and the field is not found inkwargs
, replace with the regular expression fromSPECIAL_FIELDS
.Everything else is returned verbatim.
- Parameters
- Returns
str
format_string
with the fields defined inSPECIAL_FIELDS
expanded to full format specifiers and replaced by regular expressions, as desired.
- format(format_string, *args, **kwargs)[source]¶
Expand base format to handle datetime and special dbp keywords
This is the top-level function to call.
- format_field(value, format_spec)¶
- get_field(field_name, args, kwargs)¶
- get_value(key, args, kwargs)¶
- parse(format_string)¶
- re(format_string, *args, **kwargs)[source]¶
Format with regexp for unspecified fields.
Similar to
format()
, but any fields which are not specified are replaced wth regular expressions according toSPECIAL_FIELDS
.
- vformat(format_string, args, kwargs)¶
- SPECIAL_FIELDS = {'??': ('{??}', '..'), '???': ('{???}', '...'), '????': ('{????}', '....'), 'APID': ('{APID:x}', '[\\da-fA-F]+'), 'DATE': ('{DATE}', '(19|2\\d)\\d\\d(0\\d|1[0-2])[0-3]\\d'), 'H': ('{H:02d}', '[0-2]\\d'), 'M': ('{M:02d}', '[0-6]\\d'), 'MICRO': ('{MICRO:03d}', '\\d{3}'), 'MILLI': ('{MILLI:03d}', '\\d{3}'), 'QACODE': ('{QACODE}', 'ok|ignore|problem'), 'S': ('{S:02d}', '[0-6]\\d'), 'VERSION': ('{VERSION}', '\\d+\\.\\d+\\.\\d+'), 'Y': ('{Y:04d}', '(19|2\\d)\\d\\d'), 'b': ('{b}', 'Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec'), 'd': ('{d:02d}', '[0-3]\\d'), 'datetime': ('{datetime}', '(19|2\\d)\\d\\d(0\\d|1[0-2])[0-3]\\d'), 'j': ('{j:03d}', '[0-3]\\d\\d'), 'm': ('{m:02d}', '(0\\d|1[0-2])'), 'mday': ('{mday:d}', '-?\\d+'), 'nn': ('{nn}', '\\d\\d'), 'nnn': ('{nnn}', '\\d\\d\\d'), 'nnnn': ('{nnnn}', '\\d\\d\\d\\d'), 'y': ('{y:02d}', '\\d\\d')}¶
indexed by field name; each element contains a fully-formatted representation of the field and a regular expression that should match it. (
dict
)
Release: 0.1.0 Doc generation date: Feb 10, 2022