1

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.