Skip to content

easy way to handle random_seed-like paramaters #21

Description

@spisakt

In the numpy-universe stochastic functions's seed can be either fixed by setting it to an int, or deterministic behavior can be switched off by setting the seed to None.

My workaround to ensure the correct behavior is:

def my_keymaker(<the whole signature>, random_seed=None):
    if random_seed is None:
       random_seed = np.random.normal()
    return <usual key for all parameters>, random_seed

@cached(custom_key_maker=my_keymaker)
def function_with_long_signature(<the whole signature>, random_seed=None)

I understand, with this approach numpy (or equivalent) receives None (and not the random float generated in the keymaker) but at the same time, we force to have new hash every time random seed is None.

This approach seems to work nicely but doesn't look very elegant, especially if the function has a long signature...

Any more comfortable way to disable cache when certain parameters take certain values?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions