1

I want to structure a property of an object which should be read from an external system (in this case, it's a blockchain, but could be any other system).

What is the best practice to make the property read-only and have its value taken from the external system? Is the @property pattern usable?

__tablename__ = "example"

id = db.Column(db.Integer, primary_key=True, autoincrement=True)
...

@property
def status(self):
...?

Thanks

Adding some details:

  • is it ok to use http calls or anyways asynch methods inside the property?
  • it would be very nice to save the result of the external call and the time it was taken, something like "latest_checked_state" "latest_check_datetime"
T. Rossi
  • 465
  • 1
  • 6
  • 23
  • 2
    Yes a `@property` Sounds like it might be a good fit and are read only unless you define a setter. Can you elaborate on how you want the property to behave? For example, should the property access the external resource on each access, or cache the result of first access? If cached, do you want the cached value to expire along with sqlalchemy column attributes on commit etc? – SuperShoot Aug 25 '19 at 02:49
  • thanks for the reply, I'd like to cache the result along with the last time it was checked from the external system. This object in the external system has only two states, once the final state is reached it won't change anymore. I've added some description of my doubts in the question, thanks again – T. Rossi Aug 25 '19 at 12:26
  • So unless you really need to tie your property into the expiration/refresh cycle of the sqlalchemy instrumented attributes, your solution here isn't constrained by sqlalchemy at all and you should be able to use whichever attribute caching paradigm suits you best. See https://stackoverflow.com/questions/4037481/caching-attributes-of-classes-in-python for some examples. – SuperShoot Aug 25 '19 at 21:54

0 Answers0