Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

3
Deserializing to different schema from same queue?
Post Body

I have a message queue, with messages that have a well defined schema. Up until very recently, there has been only one message type, and therefore I've only needed one schema.

A recent change to the queue now allows for multiple different messages, with different schemas on the same queue.

Up to now, I've been handling the messages like:

for msg in queue:
    loaded_msg = MessageSchema.Schema().load(msg)  # if the message doesn't meet the schema, that's bad, and the test should fail immediately
    # do stuff

Now that there are two, this fails, as the new schema isn't a sub, or superset of the current model.

My current approach to handle this is:

for msg in queue:
    try:
        loaded_msg = MessageSchema.Schema().load(msg)
    except ValidationError as e:
        try:
            _ = NewMessageSchema.schema().load(msg)
            continue    # Don't care about NewMessages, but it should still match one of the schemas
        except ValidationError:
            raise e  # message doesn't meet either schema, this is bad
    # do stuff

Obviously, this isn't scalable if new message schemas are allowed on the queue. What's the best way to handle it?

Author
Account Strength
100%
Account Age
5 years
Verified Email
Yes
Verified Flair
No
Total Karma
42,492
Link Karma
11,134
Comment Karma
30,467
Profile updated: 2 days ago
Posts updated: 4 months ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
8 months ago