Skip to content

globals

langroid/utils/globals.py

GlobalState

Bases: BaseModel

A base Pydantic model for global states.

get_instance() classmethod

Get the global instance of the specific subclass.

Returns:

Type Description
GlobalState

The global instance of the subclass.

Source code in langroid/utils/globals.py
@classmethod
def get_instance(cls: Type["GlobalState"]) -> "GlobalState":
    """
    Get the global instance of the specific subclass.

    Returns:
        The global instance of the subclass.
    """
    if cls._instance is None:
        cls._instance = cls()
    return cls._instance

set_values(**kwargs) classmethod

Set values on the global instance of the specific subclass.

Parameters:

Name Type Description Default
**kwargs Dict[str, Any]

The fields and their values to set.

{}
Source code in langroid/utils/globals.py
@classmethod
def set_values(cls: Type[T], **kwargs: Dict[str, Any]) -> None:
    """
    Set values on the global instance of the specific subclass.

    Args:
        **kwargs: The fields and their values to set.
    """
    instance = cls.get_instance()
    for key, value in kwargs.items():
        setattr(instance, key, value)

get_value(name) classmethod

Retrieve the value of a specific field from the global instance.

Parameters:

Name Type Description Default
name str

The name of the field to retrieve.

required

Returns:

Name Type Description
str Any

The value of the specified field.

Source code in langroid/utils/globals.py
@classmethod
def get_value(cls: Type[T], name: str) -> Any:
    """
    Retrieve the value of a specific field from the global instance.

    Args:
        name (str): The name of the field to retrieve.

    Returns:
        str: The value of the specified field.
    """
    instance = cls.get_instance()
    return getattr(instance, name)