opan.utils.decorate

Custom decorators defined for Open Anharmonic.

All of these decorators were built using the class form per the exposition here extlink.

Decorators

class opan.utils.decorate.arraysqueeze(*args)

Converts selected arguments to squeezed np.arrays

Pre-applies an np.asarray(...).squeeze() conversion to all positional arguments according to integer indices passed, and to any keyword arguments according to any strings passed.

Each int argument passed instructs the decorator to convert the corresponding positional argument in the function definition.

Each str argument passed instructs the decorator to convert the corresponding keyword argument.

str parameters corresponding to keyword arguments absent in a particular function call and positional/optional argument indices beyond the range of the actual *args of the decorated function are ignored.

Warning

Likely fragile with optional arguments; needs to be tested.

Parameters:*args (int or str) – Arguments to convert to squeezed np.array.
class opan.utils.decorate.kwargfetch(*args, **kwargs)

Fetch a missing keyword argument with a custom callable & arguments

This decorator implements a form of non-persistent memoization for use in networks of inter-related and/or nested functions, where:

  • External users may have reason to call any of the functions directly
  • Most or all of the functions call one or more of the same specific “supporting” functions that potentially represent significant computational overhead
  • Calls with identical function arguments are not likely to recur in typical use by external users, and thus fully persistent memoization would in general be a waste of memory

The memoization is implemented via injection of a specific keyword argument into a call to the wrapped function, where the inserted value is obtained from a call in turn to a specified callable using arguments drawn from the wrapped call. If the target keyword argument is already present in the wrapped call, no action is taken.

Note

The API description below is wholly non-intuitive and likely impossible to follow. The examples provided in the User’s Guide will probably be much more illuminating.

Parameters:
  • args[0]str – Name of the keyword argument to be injected into the call to the wrapped function
  • args[1]callable() – Object to call to generate value to be injected into the target keyword argument (args[0])
  • args[2..n]int or str – Indicate which positional (int) and keyword (str) parameters of the wrapped function call are to be passed to the callable() of args[1] as positional parameters, in the order provided within args[2..n]
  • kwargsint or str – Indicate which positional (int) and keyword (str) parameters of the wrapped function call are to be passed to the callable() of args[1] as keyword parameters, where the keys indicated here in kwargs are those used in the call to args[1]