Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

BOFH excuse #123: user to computer ratio too high.


comp / comp.lang.python / Re: Serializing pydantic enums

SubjectAuthor
o Re: Serializing pydantic enumsLarry Martell

1
Subject: Re: Serializing pydantic enums
From: Larry Martell
Newsgroups: comp.lang.python
Date: Wed, 29 May 2024 22:29 UTC
References: 1 2 3 4
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!fu-berlin.de!uni-berlin.de!not-for-mail
From: larry.martell@gmail.com (Larry Martell)
Newsgroups: comp.lang.python
Subject: Re: Serializing pydantic enums
Date: Wed, 29 May 2024 15:29:28 -0700
Lines: 54
Message-ID: <mailman.71.1717021808.2909.python-list@python.org>
References: <CACwCsY5xAJrFMvXQquLvrMrtgZk28-2UPnoQEwgH=8CtFxvRdg@mail.gmail.com>
<CAJQBtg=f3t9-TVeyrJVd2Y27uymSuMw1mJ9OWVUXN7BK8MLcUg@mail.gmail.com>
<CACwCsY7Ze-2n2Gusu=6QKftCfbzQGaaSXJz5vwLErEi6q6xmAA@mail.gmail.com>
<CACwCsY4Sa9+1UJKfh52Z8m7_Rc+JMNgq+MP4Sw2d-PD4mGrQBw@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Trace: news.uni-berlin.de J057e2FTQNxk88CRZYBXHw+GeXgtK/bJ0bgJhiuQbqdg==
Cancel-Lock: sha1:bjz0osDFx7yVmdkpqAZpPy1oouo= sha256:U7fX1WSMsueAiCA7MiuG+Om+pR2bgeWcBH/9VjPjKkY=
Return-Path: <larry.martell@gmail.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=gmail.com header.i=@gmail.com header.b=OqiY96mn;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.002
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'this:': 0.03; 'argument':
0.04; 'cc:addr:python-list': 0.09; 'json': 0.09; 'recurring':
0.09; 'situation,': 0.09; 'typeerror:': 0.09; '***': 0.16; '2024':
0.16; 'cc:name:python': 0.16; 'model:': 0.16; 'passing.': 0.16;
'recall': 0.16; 'wrote:': 0.16; 'python': 0.16; 'uses': 0.19;
"aren't": 0.19; 'implement': 0.19; 'tue,': 0.19;
'cc:addr:python.org': 0.20; 'maybe': 0.22; 'cannot': 0.25;
'cc:2**0': 0.25; 'tried': 0.26; 'object': 0.26; "isn't": 0.27;
'this?': 0.29; 'seem': 0.31; 'takes': 0.31; 'official': 0.32;
'guess': 0.32; 'objects': 0.32; 'python-list': 0.32; 'message-
id:@mail.gmail.com': 0.32; 'but': 0.32; 'same': 0.34; 'header:In-
Reply-To:1': 0.34; 'received:google.com': 0.34;
'from:addr:gmail.com': 0.35; 'missing': 0.37; 'special': 0.37;
"it's": 0.37; 'class': 0.37; 'adding': 0.39; 'this,': 0.39;
'wed,': 0.39; 'method': 0.61; 'true': 0.63; 'extend': 0.64;
'right': 0.68; 'too.': 0.70; 'left': 0.83; 'handled': 0.84
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20230601; t=1717021806; x=1717626606; darn=python.org;
h=content-transfer-encoding:cc:to:subject:message-id:date:from
:in-reply-to:references:mime-version:from:to:cc:subject:date
:message-id:reply-to;
bh=qQdmqwK8lw5aiJk+bnung5EIrEn2tT0PsjGVaFXlAZE=;
b=OqiY96mn04OmenZZmENAU0yJEp8AV5JSn1Q6RrhLgLqjx19zREu1RjkzsvrBoKsqlf
x9q2XMatFQrJUmmkpTVWp++xUMdI3t/oXlvlCr7wcp6Fr6qnQdn3nczTFmAWWyx45t2+
ZJYbzf9mnJRVw2IqJFdsM6kA6QnQSTMBh1BE3i28kYeDdVPCCFNClvO0BAorHnbSbvyN
k0FO5kwwxD3td5A3b6h1AArj84eedKp4QgRhXebuiXMcoP1hqzTd2mHwcgXg2Fil25S0
cXTUsEAxh1enpWzOT1J3NWF8hYyJkkQdhv0iq5wtvfBTO0rVqaPXAxywWWceteazvO04
M+hQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20230601; t=1717021806; x=1717626606;
h=content-transfer-encoding:cc:to:subject:message-id:date:from
:in-reply-to:references:mime-version:x-gm-message-state:from:to:cc
:subject:date:message-id:reply-to;
bh=qQdmqwK8lw5aiJk+bnung5EIrEn2tT0PsjGVaFXlAZE=;
b=OXxLhWNTG9xfbGbLYqUJzFZeeZ8wpS344qumk7adB8KHpYTuelvOD25LrMtIQCRCAt
XIpPby6RZlEOeENcANwSo9j/sfzi+uGzoqOI6gGQZQqZ1vh+IqV45fR4Tnj3MCJ4ZD1M
RbYqgqaWD2nNF499Zm9p54PPV5f7F8qKU6fbSFeN1ji38JO/SZePkNxLCeKz9OK5+62W
t+oeWG/yGVxF4ipQe7UVex2ZGwnV7Y/iJEV+7DnZ6zyeFpB4ZTEMhcg3o1PJLotgPwxF
faL2v0zzHRkitbQoLSEnaFJYh4NS6fb+4sLWtsQkdXlMjUwizzJ9Vz5Kt5dmo20/0lAN
yzoQ==
X-Gm-Message-State: AOJu0Yz1TQzC/Q735VGzwbL8C1aQE5tf+ZfRRqC+/trx8igKC/x3V8Wu
Pr9CqBmmocPzJl6WfkezfRfkAdMqkcvt/ByK/Lv3q0yzILWn7Y+oL7qWOqNth+acLVZoub7pXM4
e8njv9QSmXCvehKBIAgJnwwXOfA==
X-Google-Smtp-Source: AGHT+IGy9FuO3y9BrxKbYwYr497xqlpV6fIwMV+rePvQBB3Ajh2pqlZyWkg41J7Lcn//YJVAq8PkAap5s+QLJixod1A=
X-Received: by 2002:a05:6902:1b09:b0:de1:2206:a87e with SMTP id
3f1490d57ef6-dfa5a5ba87bmr765623276.13.1717021805995; Wed, 29 May 2024
15:30:05 -0700 (PDT)
In-Reply-To: <CACwCsY7Ze-2n2Gusu=6QKftCfbzQGaaSXJz5vwLErEi6q6xmAA@mail.gmail.com>
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <CACwCsY4Sa9+1UJKfh52Z8m7_Rc+JMNgq+MP4Sw2d-PD4mGrQBw@mail.gmail.com>
X-Mailman-Original-References: <CACwCsY5xAJrFMvXQquLvrMrtgZk28-2UPnoQEwgH=8CtFxvRdg@mail.gmail.com>
<CAJQBtg=f3t9-TVeyrJVd2Y27uymSuMw1mJ9OWVUXN7BK8MLcUg@mail.gmail.com>
<CACwCsY7Ze-2n2Gusu=6QKftCfbzQGaaSXJz5vwLErEi6q6xmAA@mail.gmail.com>
View all headers

On Wed, May 29, 2024 at 12:27 PM Larry Martell <larry.martell@gmail..com> wrote:
>
> On Tue, May 28, 2024 at 11:46 AM Left Right via Python-list
> <python-list@python.org> wrote:
> >
> > Most Python objects aren't serializable into JSON. Pydantic isn't
> > special in this sense.
> >
> > What can you do about this? -- Well, if this is a one-of situation,
> > then, maybe just do it by hand?
> >
> > If this is a recurring problem: json.dumps() takes a cls argument that
> > will be used to do the serialization. Extend json.JSONEncoder and
> > implement the encode() method for the encoder class you are passing. I
> > believe that the official docs have some information about this too.
>
> Yeah, I know I can do this, but I seem to recall reading that pydantic
> handled serialization. Guess not.

Actually it's as simple as adding this to any model that uses an enum model:

class Config:
use_enum_values = True

> > On Tue, May 28, 2024 at 2:50 PM Larry Martell via Python-list
> > <python-list@python.org> wrote:
> > >
> > > Just getting started with pydantic. I have this example code:
> > >
> > > class FinishReason(Enum):
> > > stop = 'stop'
> > >
> > > class Choice(BaseModel):
> > > finish_reason: FinishReason = Field(...)
> > >
> > >
> > > But I cannot serialize this:
> > >
> > > json.dumps(Choice(finish_reason=FinishReason.stop).dict())
> > > *** TypeError: Object of type FinishReason is not JSON serializable
> > >
> > >
> > > I get the object not the value:
> > >
> > > (Pdb) Choice(finish_reason=FinishReason.stop)
> > > Choice(finish_reason=<FinishReason.stop: 'stop'>)
> > >
> > >
> > > Also tried it with .value, same result.
> > >
> > > What am I missing here?

1

rocksolid light 0.9.8
clearnet tor