#🔒 how to auto parse properties pydantic

31 messages · Page 1 of 1 (latest)

narrow ice
#

im makeing models and i want to auto convert isodate str to datetime and ect how do i do that?

compact lynxBOT
#

@narrow ice

Python help channel opened

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.

shut bobcat
narrow ice
#

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

shut bobcat
#

ye you dont have to write an init

narrow ice
#

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

shut bobcat
#

you can rely on Pydantic’s built-in parsing

narrow ice
#

it sees by default that i want a date and its an x formatted timestamp?

shut bobcat
#

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.

narrow ice
#

and what about Enum s

shut bobcat
#

i think it also support enum?

narrow ice
shut bobcat
#

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

narrow ice
steep inlet
#

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.

narrow ice
#

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?

steep inlet
narrow ice
#

how do i do that?

steep inlet
# narrow ice 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

compact lynxBOT
#
Python help channel closed

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.