pyquibbler.quiby

pyquibbler.quiby(func: Callable | None = None, lazy: bool | None = None, pass_quibs: bool = False, is_random: bool = False, is_graphics: bool | None = False, is_file_loading: bool = False, create_quib: bool | None = None, _quibify_even_if_quibbler_not_initialized: bool = False, **kwargs) Callable[[...], Quib]

Convert a regular function into a quiby function.

Converts any function func to a function that can work on quib arguments (“quiby” function). When such quiby function is called, it creates a function quib that implement the original function.

For classes, applies quiby behavior to all methods and properties (instance methods, class methods, static methods, and properties).

quiby can also be used as a decorator of user functions or classes, either directly, or as a function with parameter specification (see examples).

Parameters:
  • func (Callable or Type) – The function or class to convert.

  • lazy (bool or None, default None) – Indicates whether the created function quib evaluates immediately (lazy=False), or only when its value is requested (lazy=True). When lazy=None (default), the function is evaluated immediately only if it is a declared graphics function (is_graphics=True).

  • pass_quibs (bool, default False) – Indicates whether the function should be called with quib arguments (pass_quibs=True), or with the values of the quib arguments (pass_quibs=False, default).

  • is_random (bool, default False) – Indicates a random function. Random functions are automatically cached and can be invalidated centrally to re-randomize (see reset_random_quibs).

  • is_graphics (bool or None, default: False) – Specifies whether the function creates graphics. If True, the function will be re-evaluated upon any upstream quib changes (according to the quib’s graphics_update property), and any graphics it creates will be redrawn. is_graphics=None will search for graphics during the call to the function and the quib will automatically be defined as a graphics quib if any graphics was created.

  • is_file_loading (bool, default: False) – Indicates whether the function’s returned value depends on reading of an external file. File-loading functions can be invalidated centrally to re-load (see reset_file_loading_quibs).

  • create_quib (bool or None, default: None) – Controls when to create a quib: - True: Always create a quib. - False: Never create a quib, return the normal function output. - None: Automatic (default) - create a quib only if arguments contain quibs or lazy is explicitly set.

Returns:

a quiby function, or a quiby decorator (if function is not specified)

Return type:

Callable

Examples

>>> a = iquib(2)
>>> b = quiby(str)(a)
>>> b.get_value()
'2'
>>> @quiby
... def display_variable(name: str, x:int):
...     return f'{name} = {x}'
...
>>> num = iquib(7)
>>> display_num = display_variable('number', num)
>>> display_num.get_value()
'number = 7'
>>> @quiby(pass_quibs=False)  # default for all methods in the class
... class MyClass:
...     def __init__(self, x, y, z):
...         self.x = x  # quib
...         self.y = y  # quib
...
...     def get_x(self):
...         return self.x  # Gets value (x.get_value())
...
...     @quiby(pass_quibs=True)  # override for this method only
...     def get_y(self):
...         return self.y  # Gets proxy quib (proxy(y))
...
>>> @quiby(is_random=True)
... def sum_of_dice(n: int):
...     dice = np.random.randint(1, 6, n)
...     return np.sum(dice)
...
>>> n = iquib(2)
>>> sum_dice = sum_of_dice(n)
>>> sum_dice.get_value()
17
>>> reset_random_quibs()
>>> sum_dice.get_value()
27

Note

If Quibbler has not been initialized, quiby will simply return the unmodified function, not a quiby function. This allows checking how your code works without quibs.