🔍 Code Extractor

function updating

Maturity: 25

A decorator function that temporarily sets an object's `_updating` flag to True during the execution of a wrapped method, then restores the original flag value.

File:
/tf/active/vicechatdev/panel_edits/custom_tabulator.py
Lines:
3 - 11
Complexity:
simple

Purpose

This decorator is used to track when an object is in the process of being updated. It's commonly used in UI frameworks like Panel to prevent recursive updates or to signal that an object is currently undergoing changes. The decorator ensures the `_updating` flag is properly restored even if an exception occurs during the wrapped function's execution.

Source Code

def updating(fn):
    def wrapped(self, *args, **kwargs):
        updating = self._updating
        self._updating = True
        try:
            fn(self, *args, **kwargs)
        finally:
            self._updating = updating
    return wrapped

Parameters

Name Type Default Kind
fn - - positional_or_keyword

Parameter Details

fn: The function to be wrapped/decorated. Expected to be a method that takes 'self' as its first parameter, followed by any number of positional and keyword arguments. This should be a method of a class that has a '_updating' attribute.

Return Value

Returns a wrapped function that, when called, will execute the original function with the _updating flag management logic. The wrapped function returns None (implicitly), as the original function's return value is not captured or returned.

Dependencies

  • panel

Required Imports

import panel as pn

Usage Example

python
import panel as pn

class MyComponent:
    def __init__(self):
        self._updating = False
    
    @updating
    def update_value(self, new_value):
        # This method will execute with _updating=True
        print(f"Updating flag: {self._updating}")
        self.value = new_value
        print(f"Value updated to: {self.value}")
    
    def check_updating(self):
        return self._updating

# Usage
component = MyComponent()
print(f"Before update: {component.check_updating()}")  # False
component.update_value(42)  # Prints: Updating flag: True
print(f"After update: {component.check_updating()}")  # False

Best Practices

  • Ensure the class using this decorator has a '_updating' attribute initialized before calling any decorated methods
  • This decorator does not return the wrapped function's return value, so it should only be used with methods that don't need to return values
  • The decorator uses a try-finally block to ensure the _updating flag is always restored, even if an exception occurs
  • This pattern is useful for preventing recursive updates in reactive UI frameworks
  • Consider checking the '_updating' flag in other methods to conditionally skip operations during updates

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function guard_execution 49.9% 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
  • function transaction 48.2% similar

    A decorator function that wraps another function to provide database transaction management capabilities, currently implemented as a placeholder for future transaction handling.

    From: /tf/active/vicechatdev/CDocs/controllers/__init__.py
  • function with_lock_v1 47.4% similar

    A decorator that implements reentrant locking for async methods, allowing the same task to acquire the lock multiple times without deadlocking.

    From: /tf/active/vicechatdev/rmcl/items.py
  • function disable_constant 47.3% similar

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

    From: /tf/active/vicechatdev/patches/util.py
  • function async_action 47.1% similar

    A decorator function that marks another function as asynchronous by adding an 'is_async' attribute, while preserving the original function's metadata.

    From: /tf/active/vicechatdev/CDocs/controllers/__init__.py
← Back to Browse