Closed mixilchenko closed 2 years ago
@lebrice could you please take a look. What do you think about custom postprocessors?
Hey @mixilchenko , why not just use the type
argument to field
? Doesn't that already do what you want?
from dataclasses import dataclass
def date_from_string(v: str) -> dt.date:
... # whatever conversion you want to do
from simple_parsing.helpers import field
class Foo:
before: pd.date = field(type=date_from_string)
Thanks @lebrice! I didn't find this. I think it can close some of my needs.
But I've found at least one con in this approach
It writes on --help
--before date_from_string
or
--before <lambda>
instead of real type
--before datetime.date
@mixilchenko then you can pass in the metavar
you want, the same way!
How to use dates with simple parsing
Since python builtins
dt.date
anddt.datetime
do not support string parsing in constructor, the only way to use dates now ispd.Timestamp
classBut someone doesn't want to install pandas as dependency and dates support could be helpful
Datetimes can be specified in different formats:
Some problems and possible solutions
dt.date
has nostrptime
methodpostprocessor should check that type is not
dt.datetime
subclass and do something likeDatetime is often used with timezones and
strptime
doesn't handle them correctly. Also timezones have different implementations and which one to use during postprocessingYou can see that named timezone and timezone with shift are not the same things since there are lots of regions with daylight saving time. This problem can be solved with custom postprocessor (see below)
There are lots of
dt.date
anddt.datetime
subclasses (eg pendulum and arrow)pendulum
andarrow
libraries have theirparse
andget
methods respectively to parse strings. We can implement optional argumentpostprocessor
of typeOptional[Callable[[str], T]]
forfield
. This would provide vast range of possibilities forsimple_parsing
users. The basic usage can be