There are some edge cases that this module does not cover, and rather than recreating the wheel I would like to discuss a method to support datetime.strftime directives.
The basic strategy I am imagining would be to preprocess the given string to replace these directives with appropriate format definitions from a hard-coded table so that they are loaded into the named set. These values can then be used to set a datetime on the named set after everything is parsed.
Walkthrough example:
FMT_STR="string with {stuff}, {}, and strftime directives like %Y, %d, and %b"
parse(FMT_STR, "string with myStuff, also_this, and strftime directives like 2018, 03 and Feb").named
>> {
"stuff": "myStuff",
"__Y": 2018,
"__b": "Feb",
"__d": 3,
"__datetime": datetime(2018, 2, 3)
}
In the above example the format string would be pre-parsed into something like :
"string with {stuff}, {}, and strftime directives like {:4d}, {:2d}, and {:3w}"
using a mapping like:
map = {
"%Y": "{:4d}",
"%d": "{:2d}",
"%b": "{:3w}"
}
for directive, fmt in map.items():
string = string.replace(directive, fmt)
Does this seem reasonable? I may try an implementation unless there are potential issues with this I am overlooking.
There are some edge cases that this module does not cover, and rather than recreating the wheel I would like to discuss a method to support
datetime.strftimedirectives.The basic strategy I am imagining would be to preprocess the given string to replace these directives with appropriate format definitions from a hard-coded table so that they are loaded into the
namedset. These values can then be used to set a datetime on thenamedset after everything is parsed.Walkthrough example:
In the above example the format string would be pre-parsed into something like :
using a mapping like:
Does this seem reasonable? I may try an implementation unless there are potential issues with this I am overlooking.