🔍 Code Extractor

function disable_constant

Maturity: 43

A context manager that temporarily disables the 'constant' attribute on all parameters of a Parameterized object, restoring their original constant states upon exit.

File:
/tf/active/vicechatdev/patches/util.py
Lines:
1621 - 1634
Complexity:
simple

Purpose

This function is used to temporarily allow modification of parameters that are normally marked as constant (immutable) on a Parameterized object. It's particularly useful when you need to programmatically update parameters that should normally be protected from changes, such as during initialization, configuration updates, or testing scenarios. The context manager ensures that the original constant states are always restored, even if an exception occurs.

Source Code

def disable_constant(parameterized):
    """
    Temporarily set parameters on Parameterized object to
    constant=False.
    """
    params = parameterized.param.objects('existing').values()
    constants = [p.constant for p in params]
    for p in params:
        p.constant = False
    try:
        yield
    finally:
        for (p, const) in zip(params, constants):
            p.constant = const

Parameters

Name Type Default Kind
parameterized - - positional_or_keyword

Parameter Details

parameterized: A Parameterized object (from the param library) whose parameters will have their constant attribute temporarily set to False. This should be an instance of a class that inherits from param.Parameterized and has parameters defined with the param library.

Return Value

This is a context manager (generator function) that yields control to the calling code. It doesn't return a specific value but provides a context where all parameters on the parameterized object have constant=False. Upon exiting the context (either normally or via exception), all parameters are restored to their original constant states.

Dependencies

  • param

Required Imports

from contextlib import contextmanager
import param

Usage Example

import param
from contextlib import contextmanager

@contextmanager
def disable_constant(parameterized):
    params = parameterized.param.objects('existing').values()
    constants = [p.constant for p in params]
    for p in params:
        p.constant = False
    try:
        yield
    finally:
        for (p, const) in zip(params, constants):
            p.constant = const

class MyConfig(param.Parameterized):
    value = param.Number(default=10, constant=True)
    name = param.String(default='test', constant=True)

config = MyConfig()

# This would normally raise an error because value is constant
# config.value = 20  # Error!

# But with disable_constant, we can modify it temporarily
with disable_constant(config):
    config.value = 20
    config.name = 'modified'
    print(f"Inside context: {config.value}, {config.name}")

# After exiting, parameters are constant again
print(f"After context: {config.value}, {config.name}")
# Trying to modify now would raise an error again

Best Practices

  • Always use this function as a context manager (with 'with' statement) to ensure proper cleanup of parameter states
  • Be cautious when using this in multi-threaded environments as it modifies shared parameter state
  • Only use this when you have a legitimate need to modify constant parameters; respect the constant designation in normal code
  • Ensure the parameterized object passed is actually a param.Parameterized instance to avoid AttributeErrors
  • Keep the code within the context block minimal to reduce the window where constant parameters are modifiable
  • This function modifies all parameters on the object; if you only need to modify specific parameters, consider a more targeted approach

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function set_curdoc 47.4% similar

    A context manager that temporarily sets the current Bokeh document (curdoc) in the application state, ensuring it is properly cleaned up after use.

    From: /tf/active/vicechatdev/patches/server.py
  • function dynamicmap_memoization 46.5% similar

    A context manager that temporarily controls memoization behavior of a callable object based on the state of associated streams, disabling memoization when transient streams are triggering.

    From: /tf/active/vicechatdev/patches/spaces.py
  • function unlocked 45.4% similar

    A context manager that temporarily unlocks a Bokeh Document and dispatches ModelChangedEvents to all connected WebSocket clients during the context execution.

    From: /tf/active/vicechatdev/patches/server.py
  • function guard_execution 42.3% similar

    A decorator factory that prevents rapid repeated execution of a function by enforcing a cooldown period between calls.

    From: /tf/active/vicechatdev/CDocs/__init__.py
  • class Config_v4 37.4% similar

    A configuration class that manages global HoloViews behavior settings, including deprecation warnings, default colormaps, and rendering parameters.

    From: /tf/active/vicechatdev/patches/util.py
← Back to Browse