### Installation of lazyregistry Source: https://github.com/milkclouds/lazyregistry/blob/main/README.md Commands to install the lazyregistry library using popular Python package managers. ```bash pip install lazyregistry uv add "lazyregistry" ``` -------------------------------- ### Quick Start with Registry Source: https://github.com/milkclouds/lazyregistry/blob/main/README.md Demonstrates how to initialize a Registry and register objects using both lazy import strings and direct object references. ```python from lazyregistry import Registry registry = Registry(name="plugins") # Register by import string (lazy - imported on access) registry["json"] = "json:dumps" # Register by instance (immediate - already imported) import pickle registry["pickle"] = pickle.dumps # Import happens here serializer = registry["json"] ``` -------------------------------- ### Plugin System with Decorator Registration in Python Source: https://context7.com/milkclouds/lazyregistry/llms.txt Provides an example of building an extensible plugin architecture using decorator-based registration with lazy loading. A global plugin registry is defined to manage plugins, enabling dynamic loading and management of extensions. ```python from lazyregistry import Registry # Create a global plugin registry PLUGINS: Registry[str, type] = Registry(name="plugins") ``` -------------------------------- ### Plugin System Implementation Source: https://github.com/milkclouds/lazyregistry/blob/main/README.md Shows how to create an extensible plugin architecture using a decorator-based registration pattern with the Registry class. ```python from lazyregistry import Registry PLUGINS = Registry(name="plugins") def plugin(name: str): def decorator(cls): PLUGINS[name] = cls return cls return decorator @plugin("uppercase") class UppercasePlugin: def process(self, text: str) -> str: return text.upper() ``` -------------------------------- ### Optimizing Imports with LazyRegistry Source: https://github.com/milkclouds/lazyregistry/blob/main/README.md Compares traditional eager imports with the lazy approach provided by the library, demonstrating how to defer heavy module loading until the moment of access. ```python from lazyregistry import Registry registry = Registry(name="components") registry.register("a", "heavy_module_1:ClassA") # ClassA is only imported here component = registry["a"] ``` -------------------------------- ### Registry and Namespace Usage Source: https://github.com/milkclouds/lazyregistry/blob/main/README.md Demonstrates basic dictionary-style registration and the use of Namespaces to organize multiple registries. It supports both immediate object assignment and lazy-loading via import strings. ```python from lazyregistry import Registry, NAMESPACE # Basic Registry registry = Registry() registry["key"] = "module:object" # Lazy registry["key2"] = actual_object # Immediate # Namespace usage NAMESPACE["models"]["bert"] = "transformers:BertModel" model = NAMESPACE["models"]["bert"] ``` -------------------------------- ### Configuring LazyImportDict Source: https://github.com/milkclouds/lazyregistry/blob/main/README.md Shows how to customize the behavior of the base LazyImportDict class. Users can toggle auto-import string conversion and eager loading settings. ```python from lazyregistry.registry import LazyImportDict registry = LazyImportDict() registry.auto_import_strings = True registry.eager_load = False registry["key"] = "module:object" ``` -------------------------------- ### Run Test Suite with Coverage Source: https://github.com/milkclouds/lazyregistry/blob/main/README.md Command to execute the project test suite using uv and pytest, including coverage reporting to ensure code quality. ```bash uv run pytest tests/ --cov=lazyregistry --cov-report=term-missing ``` -------------------------------- ### Pretrained Model Registration Source: https://github.com/milkclouds/lazyregistry/blob/main/README.md Illustrates using PretrainedMixin and AutoRegistry to manage models. It supports decorator-based registration, direct assignment, and bulk updates for automatic type detection from configuration files. ```python from lazyregistry.pretrained import PretrainedMixin, AutoRegistry class AutoModel(AutoRegistry): registry = NAMESPACE["models"] config_class = PretrainedConfig type_key = "model_type" @AutoModel.register_module("bert") class BertModel(BaseModel): config_class = BertConfig AutoModel.registry["gpt2"] = GPT2Model loaded = AutoModel.from_pretrained("./path") ``` -------------------------------- ### Update Registry with String References Source: https://github.com/milkclouds/lazyregistry/blob/main/README.md Demonstrates how to register models using string references to defer actual module imports until the registry is accessed. This pattern helps in avoiding heavy dependency loading at startup. ```python AutoModel.registry.update({ "bert": "mypackage.bert:BertModel", "gpt2": "mypackage.gpt2:GPT2Model", }) ``` -------------------------------- ### Combining with lazy-loader Source: https://github.com/milkclouds/lazyregistry/blob/main/README.md Shows how to integrate lazy-loader with lazyregistry to achieve full package-level lazy imports, maintaining IDE support via TYPE_CHECKING blocks. ```python from typing import TYPE_CHECKING if TYPE_CHECKING: from .auto import AutoModel else: import lazy_loader as lazy __getattr__, __dir__, __all__ = lazy.attach( __name__, submod_attrs={"auto": ["AutoModel"]} ) ``` -------------------------------- ### Using ImportString for Lazy Loading Source: https://github.com/milkclouds/lazyregistry/blob/main/README.md Demonstrates the use of the ImportString class to represent an import path that is only resolved when the load() method is called. ```python from lazyregistry import ImportString # Create an import string import_str = ImportString("json:dumps") # Load the object when needed func = import_str.load() func({"key": "value"}) ``` -------------------------------- ### Pretrained Model Registration Source: https://github.com/milkclouds/lazyregistry/blob/main/README.md Illustrates the HuggingFace-style save/load pattern using AutoRegistry and PretrainedMixin for managing model configurations and lazy loading. ```python from lazyregistry import NAMESPACE from lazyregistry.pretrained import AutoRegistry, PretrainedConfig, PretrainedMixin class BertConfig(PretrainedConfig): model_type: str = "bert" hidden_size: int = 768 class BaseModel(PretrainedMixin): config_class = PretrainedConfig class AutoModel(AutoRegistry): registry = NAMESPACE["models"] config_class = PretrainedConfig type_key = "model_type" @AutoModel.register_module("bert") class BertModel(BaseModel): config_class = BertConfig # Register directly AutoModel.registry["gpt2"] = "transformers:GPT2Model" ``` -------------------------------- ### Registry with Lazy Import Support in Python Source: https://context7.com/milkclouds/lazyregistry/llms.txt Illustrates the usage of the Registry class for managing components with automatic lazy import. Strings assigned to the registry are converted to ImportString and loaded on demand, while direct object assignments are loaded immediately. Supports eager loading and standard dictionary operations. ```python from lazyregistry import Registry # Create a named registry serializers = Registry(name="serializers") # Register by import string (lazy - imported on first access)serializers["json"] = "json:dumps" serializers["base64_encode"] = "base64:b64encode" # Register direct objects (immediate - no lazy loading) import pickle serializers["pickle"] = pickle.dumps # Access triggers lazy import for string-registered items json_dumps = serializers["json"] # json module imported here result = json_dumps({"name": "test"}) print(result) # '{"name": "test"}' # Enable eager loading for critical components serializers.eager_load = True serializers["yaml"] = "yaml:safe_dump" # Imported immediately # Dict-style operations work as expected serializers.update({ "toml_parse": "tomllib:loads", "csv_write": "csv:writer" }) print(list(serializers.keys())) # ['json', 'base64_encode', 'pickle', 'yaml', 'toml_parse', 'csv_write'] ``` -------------------------------- ### Namespace for Organizing Registries in Python Source: https://context7.com/milkclouds/lazyregistry/llms.txt Demonstrates the use of the Namespace class for organizing multiple isolated registries. Registries within a namespace are created on-demand when accessed, providing a structured way to manage different sets of components. ```python from lazyregistry import NAMESPACE, Namespace # Use the global NAMESPACE singleton NAMESPACE["models"]["bert"] = "transformers:BertModel" NAMESPACE["models"]["gpt2"] = "transformers:GPT2Model" NAMESPACE["tokenizers"]["wordpiece"] = "tokenizers:Tokenizer" # Each registry is independent print(list(NAMESPACE["models"].keys())) # ['bert', 'gpt2'] print(list(NAMESPACE["tokenizers"].keys())) # ['wordpiece'] # Create custom namespaces for isolation my_namespace = Namespace() my_namespace["plugins"]["processor"] = "myapp.plugins:Processor" my_namespace["handlers"]["api"] = "myapp.handlers:APIHandler" # Access items through the namespace Processor = my_namespace["plugins"]["processor"] ``` -------------------------------- ### Save and Load Models with AutoRegistry Source: https://context7.com/milkclouds/lazyregistry/llms.txt Demonstrates how to save a model configuration and state to a directory and reload it using AutoModel. This pattern relies on the presence of a config.json file to infer the model type. ```python config = BertConfig(hidden_size=1024) model = BertModel(config=config) with tempfile.TemporaryDirectory() as tmpdir: model.save_pretrained(tmpdir) # AutoModel.from_pretrained detects type from config.json loaded = AutoModel.from_pretrained(tmpdir) print(f"Auto-detected type: {type(loaded).__name__}") print(f"Config: {loaded.config}") ``` -------------------------------- ### Plugin Manager for Executing Registered Plugins in Python Source: https://context7.com/milkclouds/lazyregistry/llms.txt Implements a `PluginManager` class with static methods to execute registered plugins. `execute` runs a single plugin, while `pipeline` chains multiple plugins. `available` returns a sorted list of registered plugin names. It relies on the `PLUGINS` dictionary populated by the `@plugin` decorator. ```python class PluginManager: @staticmethod def execute(plugin_name: str, text: str) -> str: plugin_class = PLUGINS[plugin_name] return plugin_class().process(text) @staticmethod def pipeline(text: str, *plugin_names: str) -> str: result = text for name in plugin_names: result = PluginManager.execute(name, result) return result @staticmethod def available() -> list[str]: return sorted(PLUGINS.keys()) # Usage examples: print(PluginManager.available()) # ['capitalize', 'reverse', 'uppercase'] print(PluginManager.execute("uppercase", "hello")) # 'HELLO' print(PluginManager.pipeline("hello", "uppercase", "reverse")) # 'OLLEH' ``` -------------------------------- ### Lazy Loading with ImportString in Python Source: https://context7.com/milkclouds/lazyregistry/llms.txt Demonstrates how to use ImportString for deferred loading of Python objects. ImportString represents an import path and only performs the actual import when its load() method is called, allowing for graceful handling of import errors. ```python from lazyregistry import ImportString # Create import strings for deferred loading json_dumps = ImportString("json:dumps") pickle_loads = ImportString("pickle:loads") # Nothing is imported yet - load() triggers the actual import dumps_func = json_dumps.load() loads_func = pickle_loads.load() # Use the loaded functions result = dumps_func({"key": "value", "count": 42}) print(result) # '{"key": "value", "count": 42}' # Handle import errors gracefully try: bad_import = ImportString("nonexistent_module:function") bad_import.load() except ImportError as e: print(f"Import failed: {e}") # ImportFailedError with details ``` -------------------------------- ### Pydantic-based Configuration for Pretrained Models in Python Source: https://context7.com/milkclouds/lazyregistry/llms.txt Demonstrates defining model-specific configurations using `PretrainedConfig`, a Pydantic base class. `BertConfig` and `GPT2Config` inherit from it, defining their unique parameters like `model_type`, `hidden_size`, etc. It shows how to create, serialize to JSON, and deserialize from JSON with type validation. ```python from lazyregistry.pretrained import PretrainedConfig class BertConfig(PretrainedConfig): model_type: str = "bert" hidden_size: int = 768 num_layers: int = 12 attention_heads: int = 12 class GPT2Config(PretrainedConfig): model_type: str = "gpt2" hidden_size: int = 768 num_layers: int = 12 vocab_size: int = 50257 # Create and serialize configs config = BertConfig(hidden_size=1024, num_layers=24) print(config.model_dump_json(indent=2)) # Validate and load from JSON json_str = '{"model_type": "bert", "hidden_size": 512, "num_layers": 6, "attention_heads": 8}' loaded_config = BertConfig.model_validate_json(json_str) print(loaded_config.hidden_size) ``` -------------------------------- ### Handle Import Failures in Lazy Registries Source: https://context7.com/milkclouds/lazyregistry/llms.txt Shows how to catch specific import errors when accessing lazily registered components. It demonstrates the use of ImportFailedError and the broader LazyRegistryError hierarchy. ```python registry = Registry(name="test") registry["bad"] = "nonexistent_module:SomeClass" try: _ = registry["bad"] except ImportFailedError as e: print(f"Import failed: {e}") print(f"Original error: {e.__cause__}") try: _ = registry["bad"] except LazyRegistryError as e: print(f"Registry error: {e}") ``` -------------------------------- ### Mixin for Saving and Loading Pretrained Models in Python Source: https://context7.com/milkclouds/lazyregistry/llms.txt Introduces `PretrainedMixin`, a class providing `save_pretrained` and `from_pretrained` methods. Models inheriting from this mixin can serialize their configuration (defined by `config_class`) to a JSON file and be reloaded from a directory. It facilitates model persistence and sharing. ```python from pathlib import Path import tempfile from lazyregistry.pretrained import PretrainedConfig, PretrainedMixin class MyModelConfig(PretrainedConfig): model_type: str = "my_model" hidden_size: int = 256 dropout: float = 0.1 class MyModel(PretrainedMixin): config_class = MyModelConfig def __init__(self, *args, config, **kwargs): super().__init__(*args, config=config, **kwargs) self.hidden_size = config.hidden_size # Create and save a model config = MyModelConfig(hidden_size=512, dropout=0.2) model = MyModel(config=config) with tempfile.TemporaryDirectory() as tmpdir: model.save_pretrained(tmpdir) print(f"Saved config to: {tmpdir}/config.json") config_path = Path(tmpdir) / "config.json" print(config_path.read_text()) loaded = MyModel.from_pretrained(tmpdir) print(f"Loaded hidden_size: {loaded.config.hidden_size}") print(f"Loaded dropout: {loaded.config.dropout}") ``` -------------------------------- ### Implement Custom State Persistence with PretrainedMixin Source: https://context7.com/milkclouds/lazyregistry/llms.txt Extends PretrainedMixin to handle custom model state, such as vocabulary files, alongside standard configuration. This allows for complex objects to be serialized and deserialized using the save_pretrained and from_pretrained interface. ```python class Tokenizer(PretrainedMixin): config_class = TokenizerConfig def save_pretrained(self, save_directory: PathLike) -> None: super().save_pretrained(save_directory) save_path = Path(save_directory) vocab_file = save_path / "vocab.txt" sorted_vocab = sorted(self.vocab.items(), key=lambda x: x[1]) vocab_file.write_text("\n".join(word for word, _ in sorted_vocab)) @classmethod def from_pretrained(cls, pretrained_path: PathLike, **kwargs: Any): config_file = Path(pretrained_path) / cls.config_filename config = cls.config_class.model_validate_json(config_file.read_text()) vocab_file = Path(pretrained_path) / "vocab.txt" vocab = {} if vocab_file.exists(): words = vocab_file.read_text().strip().split("\n") vocab = {word: idx for idx, word in enumerate(words)} return cls(config=config, vocab=vocab, **kwargs) ``` -------------------------------- ### Custom Lazy Import Dictionary with LazyImportDict in Python Source: https://context7.com/milkclouds/lazyregistry/llms.txt Shows how to create custom registry implementations using LazyImportDict. This class provides fine-grained control over lazy import behavior, including options for automatically importing strings and deferring loading until access. ```python from lazyregistry.registry import LazyImportDict # Create a custom lazy dict components = LazyImportDict() # Configure behavior components.auto_import_strings = True # Auto-convert strings to ImportString (default) components.eager_load = False # Defer loading until access (default) # Register components components["encoder"] = "json:JSONEncoder" components["decoder"] = "json:JSONDecoder" # Access triggers import EncoderClass = components["encoder"] encoder = EncoderClass() print(encoder.encode({"a": 1})) # '{"a": 1}' # Disable auto-conversion for raw strings raw_dict = LazyImportDict() raw_dict.auto_import_strings = False raw_dict["key"] = "json:dumps" # Stored as plain string, not ImportString print(raw_dict["key"]) # 'json:dumps' (string, not function) ``` -------------------------------- ### Auto-loader Registry for Dynamic Model Instantiation in Python Source: https://context7.com/milkclouds/lazyregistry/llms.txt Presents `AutoRegistry`, a registry that automatically detects and instantiates model classes based on their `model_type` from configuration files. It supports multiple registration methods: decorator (`@AutoModel.register_module`), direct assignment, and bulk updates. This enables flexible loading of different model architectures. ```python import tempfile from lazyregistry import NAMESPACE from lazyregistry.pretrained import AutoRegistry, PretrainedConfig, PretrainedMixin class BertConfig(PretrainedConfig): model_type: str = "bert" hidden_size: int = 768 class GPT2Config(PretrainedConfig): model_type: str = "gpt2" hidden_size: int = 768 class BaseModel(PretrainedMixin): config_class = PretrainedConfig class AutoModel(AutoRegistry): registry = NAMESPACE["models"] config_class = PretrainedConfig type_key = "model_type" @AutoModel.register_module("bert") class BertModel(BaseModel): config_class = BertConfig @AutoModel.register_module("gpt2") class GPT2Model(BaseModel): config_class = GPT2Config # Example usage (assuming models are saved and loaded): # config = BertConfig(hidden_size=1024) # model = AutoModel.from_pretrained(config) # print(isinstance(model, BertModel)) # True ``` -------------------------------- ### Plugin Registration Decorator Factory in Python Source: https://context7.com/milkclouds/lazyregistry/llms.txt Defines a decorator factory `plugin` for registering classes as plugins. It stores the decorated class in a global `PLUGINS` dictionary, keyed by a provided name. This enables a simple plugin system where classes can be registered and later accessed by their registered name. ```python PLUGINS = {} def plugin(name: str): def decorator(cls: type) -> type: PLUGINS[name] = cls return cls return decorator @plugin("uppercase") class UppercasePlugin: def process(self, text: str) -> str: return text.upper() @plugin("reverse") class ReversePlugin: def process(self, text: str) -> str: return text[::-1] @plugin("capitalize") class CapitalizePlugin: def process(self, text: str) -> str: return text.capitalize() ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.