Reputation: 2472
I have a mongo wrapper with hooks to a timer class, basically, every time a collection is updated or saved it spawns a timer which in turn executes a given function when it expires. My question is, what would be the pythonic way to specify those functions? My thought was to simply add them to the collection wrapper like this:
class TestCollection(Collection):
__name__ = 'test_collection'
__database__ = 'test'
__primary_key__ = 'field_1'
post_delete = 'call_this_func_with_getattr_after_delete'
expire = 'also_call_this_with_getattr_when_timer_expires'
field_1 = Key()
field_2 = Key()
field_3 = Key()
Then I can just add the logic on my timer class to run the specified function when expired and the same for my mongo wrapper. This could also be achieved in different ways (class Meta, mangled attribute names, etc...) but I just wanted to know the general consensus when doing something like this.
Upvotes: 1
Views: 79
Reputation: 71545
Don't store names you have to look up when you cab just store references to callables directly. Any function, method, or an instance of a class with a __call__
method, is an object just like anything else, and can be stored in your expired
attribute.
Upvotes: 1