0

I want a function to set the random seeds for several known packages.I'd like to create a library function which calls:

tf.set_random_seed(seed)
np.random.seed(seed)
random.seed(seed)

, but only if actually needed. In some cases the caller will be part of a program that uses numpy and in others it won't.

I want a single function, in its own importable file, which will set the random seed of various packages, but not to import the package unless used by the caller.

I can easily work-around this by inserting the method in each caller's file. But, I'm curious if there is a way to do what I want to do.

Can I somehow query the calling function and determine "In your scope, has numpy been imported"? If so, this function would call np.random.seed.

Robert Lugg
  • 1,090
  • 2
  • 12
  • 40

1 Answers1

2

@Luke DeLuccia pointed me in the correct direction. For future readers, The code might look something like this:

import sys
def set_pseudoseeds(seed):
    # random
    try:
        module = sys.modules['random']
    except KeyError:
        pass
    else:
        module.seed(seed)
    ...

Thanks Luke!

Robert Lugg
  • 1,090
  • 2
  • 12
  • 40