Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pydantic tip... #62

Closed
bbartling opened this issue Dec 26, 2022 · 0 comments
Closed

pydantic tip... #62

bbartling opened this issue Dec 26, 2022 · 0 comments

Comments

@bbartling
Copy link

Would anyone be able to give me a Pydantic tip at all?

Is it possible on a pydantic model to reference another class? For example below in the ReadRequestModel in point_type I am trying to figure out if its possible reference that only these "types of points" in a string format can be chosen:

# type-of-points
# just for reference
multiStateValue
multiStateInput
multiStateOutput
analogValue
analogInput
analogOutput
binaryValue
binaryInput
binaryOutput

And depending on what point_type is that depics what type of point_id can be chosen that I am trying to reference in the PointType class twice below that is obviously very wrong. Any tips greatly appreciated...

from typing import List, Literal, Optional
from pydantic import BaseModel


BOOLEAN_ACTION_MAPPING = Literal["active", "inactive"]


class ReadRequestModel(BaseModel):
    device_address: str
    point_type: PointType  <--- not correct
    point_id: PointType    <--- not correct


class PointType(BaseModel):
    multiStateValue: Optional[int]
    multiStateInput: Optional[int]
    multiStateOutput: Optional[int]
    analogValue: Optional[int]
    analogInput: Optional[int]
    analogOutput: Optional[int]
    binaryValue: Optional[BOOLEAN_ACTION_MAPPING]
    binaryInput: Optional[BOOLEAN_ACTION_MAPPING]
    binaryOutput: Optional[BOOLEAN_ACTION_MAPPING]


r = ReadRequestModel({'device_address': '12345:5',
                     'point_type': 'analogInput',
                      'point_id': 8})

print(r)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant