0

I have a class somewhat like following:

from joblib import Memory
import time


def find_static_methods(cls):
    # to be implemented
    pass


class A:

    def __init__(self, cache_path: str):
        self._memory = Memory(cache_path, verbose=0)
        self._methods = {}
        for name, method in find_static_methods(A):
            self._methods[name] = self._memory.cache(method)

    def __getattribute__(self, item):
        if item in self._methods:
            return self._methods[item]
        return super(A, self).__getattribute__(item)

    @staticmethod
    def method1(a: int, b: int):
        time.sleep(3)
        return a + b

I'm trying to memoize method1 using joblib.Memory. But I don't know the cache_path in advance. Please help me with the implementation of find_static_methods here.

Pushkar Nimkar
  • 394
  • 3
  • 11

1 Answers1

2

Here is another way:

A_attrs = A.__dict__
for k, v in A_attrs.items():
    if isinstance(v, staticmethod):
        print(k)
trsvchn
  • 8,033
  • 3
  • 23
  • 30
  • Not sure whether this works: I tried it on `QtCore.QObject` (PyQt5) and it said that it had no `staticmethod`s, which is not the case. The PyQt5 package may be doing strange things though. – mike rodent Aug 22 '21 at 11:22