mercredi 21 avril 2021

How to keep a global state in memory of an object?

I'm trying to build an application that will receive a lot of messages that may be repeated in any order, how can i instantiate a class or keep the data in memory to avoid processing the same information more than once? example:

The messages:

{"product": {"name": "iphone", "active": True}}
{"price": {"amount": 100}}
{"product": {"name": "iphone", "active": True}}

When processing the messages I need to generate an error saying that the "product was already activated", I've tried to use the Singleton pattern but I don't think that it will work because I'm reading the message in a loop and i didn't figure out how to store the "product" information somewhere in a global state and check if the state is None and active = True.

The loop:

for message in messages:
    product = Product(message)  # serialize the dict into a class, this is the first message

    # On the second loop I should get an "string" saying that the product was already activated.

How can I store a information in a global state?

Aucun commentaire:

Enregistrer un commentaire