Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 14040

How to convert Python dataclass to dictionary of string literal?

$
0
0

Given a dataclass like below:

class MessageHeader(BaseModel):    message_id: uuid.UUID    def dict(self, **kwargs):        return json.loads(self.json())

I would like to get a dictionary of string literal when I call dict on MessageHeaderThe desired outcome of dictionary is like below:

{'message_id': '383b0bfc-743e-4738-8361-27e6a0753b5a'}

I want to avoid using 3rd party library like pydantic& I do not want to use json.loads(self.json()) as there are extra round trips

Is there any better way to convert a dataclass to a dictionary with string literal like above?


Viewing all articles
Browse latest Browse all 14040

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>