#🔒 how to auto parse properties pydantic
31 messages · Page 1 of 1 (latest)
@narrow ice
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
could uou specify about the etc* stuff?or you might find this helpful https://stackoverflow.com/questions/969285/how-do-i-translate-an-iso-8601-datetime-string-into-a-python-datetime-object
i want to define a pydantic field in a way that i dont have to write an init and it would parse my date to datetime
ye you dont have to write an init
but originaly its an int or str
class Absence(BaseModel):
creationTime: datetime = Field(alias="KeszitesDatuma", frozen=True, default=None)
like how do i make this work?
aoriginaly in the json i want to parse thats an isodate str
you can rely on Pydantic’s built-in parsing
it sees by default that i want a date and its an x formatted timestamp?
its been long time since i have use but
yes Pydantic handles various date and time formats however, if the input is in a timestamp format (like Unix timestamps — which are seconds or milliseconds since the epoch), Pydantic won't automatically convert that to a datetime.
and what about Enum s
i think it also support enum?
i also have unix timestamps how do i convert that?
Pydantic will map strings (or integers, if it's an int enum) to enum values directly if the input matches one of the values. But if you need custom logic to convert certain values to specific enum members like converting non-standard values or specific numbers to enum, you can use a custom validator.So i reccom you to search up the internet a bit cause am really not sure sorry :P
https://docs.pydantic.dev/latest/concepts/conversion_table/#__tabbed_1_2
how do i make ints be interpeted as minutes for timedelta
Looking at my old projects, I have used updated: DT.datetime = Field(strict=False) for that
strict=False makes it bold enough to parse timestamps as datetimes, IIRC
EDIT: wait, I can't read. Obviously it's the opposite and I was disabling some unnecessary conversions. But yeah, just datetime should work.
its in the conversion table ye i just found this
but for timedelta
it says interpeted as seconds
but i got minutes how do i do that?
For that you'd need to write a custom validator
how do i do that?
e.g. like this:
class Response(BaseModel):
duration: timedelta
@field_validator("duration")
def dur_as_mins(cls, v):
v *= 60
return v
res = Response(duration=5) # Response(duration=datetime.timedelta(seconds=300))
this assumes that the field is always a number; if it can be several types you'd need to check the type in the validator
thx
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.