lazysloth


Namelazysloth JSON
Version 0.0.6 PyPI version JSON
download
home_pageNone
SummaryA Python library for lazy and immutable variables.
upload_time2024-03-21 18:18:58
maintainerNone
docs_urlNone
authorNone
requires_python>=3.10
licenseNone
keywords lazy immutable variables
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # lazysloth: Lazy and Immutable Variable Library for Python

`lazysloth` is a Python library designed to facilitate the use of lazy initialization patterns and immutable variables in Python applications. It provides two main functionalities through its `LazyVariable` and `ImmutableVariable` classes, allowing developers to defer expensive computations until absolutely necessary and to create variables that cannot be modified after initialization.

## Status

### WIP

Super early. At the time of writing this, this is not production-ready or anything close to that. This is a pet project I started 2 days ago. Hopefully, it will be production-ready in a few weeks.

## Why

Too many things in Python evaluate at the time the code is loaded to memory, from global variables to the default values of function arguments. Sometimes, it's great; sometimes, it creates bugs that might be hard to find the first time or two before it's been burned into your experience. You can work around it with a slight refactor, which often involves wrapping some code in a function, a class, or a dictionary. I found myself writing these too many times in too many different projects, so I decided to write it once.

This time, my need came from wanting control over a singleton global variable holding the config.

I have a `my_config.py` file where I load my configuration, usually from files and environment variables. 

```python
from lazysloth import LazyVariable
from configlib import loader

settings = LazyVariable(loader.load_files, load_files=['settings.yml', 'settings.toml'])
```

```python
from my_config import settings
from my_cli import get_current_run_cli_arguments



settings._kwargs['load_files'].append(
  get_current_run_cli_arguments()['--config-override-file']
)
```





## Installation

```bash
pip install lazysloth
```

Replace `lazysloth` with the correct package name if published on PyPI or provide specific instructions if the library is to be installed directly from a source repository.

## Features

- **LazyVariable:** Enables lazy initialization of variables, where the value is only computed upon the first access.
- **ImmutableVariable:** Wraps values in an immutable container, preventing changes after initialization.

## Quick Start

Here's a quick example to get started with `lazysloth`, demonstrating the lazy loading behavior:

```python
from lazysloth import LazyVariable

def expensive_computation():
    print("Expensive computation is now being executed...")
    return {"data": 42}

# Initialize a LazyVariable with the expensive computation.
lazy_var = LazyVariable(expensive_computation)

print("LazyVariable has been initialized.")
print("Accessing the variable to trigger computation...")

# The expensive computation is executed only upon this access.
print(lazy_var.data)  # Outputs: Expensive computation is now being executed... \n 42

```

## Usage

```python
def expensive_operation():
    print("Performing an expensive operation that takes time...")
    return "Result of the operation"

lazy_operation = LazyVariable(expensive_operation)

print("LazyVariable initialized, but expensive operation hasn't run yet.")
# At this point, the expensive operation has not been executed.

# Accessing the LazyVariable to trigger the expensive operation.
result = lazy_operation()
print(f"Operation result: {result}")
# Now, the expensive operation is executed, and its result is accessed.

```

## Features

- **LazyVariable:** Enables lazy initialization of variables, where the value is only computed upon the first access.
- **ImmutableVariable:** Wraps values in an immutable container, preventing changes after initialization.

### LazyVariable

`LazyVariable` is intended for situations where a variable's initialization is computationally expensive and not always necessary. The variable's value is computed only on the first access.

#### Initialization

```python
lazy_var = LazyVariable(initializer_function, *args, **kwargs)
```

- `initializer_function`: A callable that returns the value to be lazily initialized.
- `*args` and `**kwargs`: Arguments passed to the `initializer_function`.

#### Access

Access attributes or items of the lazily initialized value as if `LazyVariable` were the value itself:

```python
result = lazy_var.attribute
result = lazy_var['item']
```

#### Callable Support

If the lazily initialized value is callable, `LazyVariable` can be called directly:

```python
result = lazy_var(*args, **kwargs)
```

### ImmutableVariable

`ImmutableVariable` ensures that the wrapped value cannot be modified after initialization. It's useful for creating read-only data structures.

- `value`: The value to be made immutable.

#### Access

Access the immutable value's attributes or items directly:

```python
result = immutable_var.attribute
result = immutable_var['item']
```

Attempting to modify the wrapped value results in an `AttributeError`.

## Advanced Usage

- **Resetting a `LazyVariable`:** If your use case requires re-initializing a `LazyVariable`, you can extend its functionality to support resetting.

## Contributing

Contributions to `lazysloth` are welcome! Please refer to the contributing guidelines for more information on how to submit pull requests, report issues, or request features.

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "lazysloth",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.10",
    "maintainer_email": null,
    "keywords": "lazy, immutable, variables",
    "author": null,
    "author_email": "Omer Holzinger <omer@omerholz.com>",
    "download_url": "https://files.pythonhosted.org/packages/c3/b7/4064050901d311b8e37bb0c1653eba6da701fbbc08dbb0c975bc85b88917/lazysloth-0.0.6.tar.gz",
    "platform": null,
    "description": "# lazysloth: Lazy and Immutable Variable Library for Python\n\n`lazysloth` is a Python library designed to facilitate the use of lazy initialization patterns and immutable variables in Python applications. It provides two main functionalities through its `LazyVariable` and `ImmutableVariable` classes, allowing developers to defer expensive computations until absolutely necessary and to create variables that cannot be modified after initialization.\n\n## Status\n\n### WIP\n\nSuper early. At the time of writing this, this is not production-ready or anything close to that. This is a pet project I started 2 days ago. Hopefully, it will be production-ready in a few weeks.\n\n## Why\n\nToo many things in Python evaluate at the time the code is loaded to memory, from global variables to the default values of function arguments. Sometimes, it's great; sometimes, it creates bugs that might be hard to find the first time or two before it's been burned into your experience. You can work around it with a slight refactor, which often involves wrapping some code in a function, a class, or a dictionary. I found myself writing these too many times in too many different projects, so I decided to write it once.\n\nThis time, my need came from wanting control over a singleton global variable holding the config.\n\nI have a `my_config.py` file where I load my configuration, usually from files and environment variables. \n\n```python\nfrom lazysloth import LazyVariable\nfrom configlib import loader\n\nsettings = LazyVariable(loader.load_files, load_files=['settings.yml', 'settings.toml'])\n```\n\n```python\nfrom my_config import settings\nfrom my_cli import get_current_run_cli_arguments\n\n\n\nsettings._kwargs['load_files'].append(\n  get_current_run_cli_arguments()['--config-override-file']\n)\n```\n\n\n\n\n\n## Installation\n\n```bash\npip install lazysloth\n```\n\nReplace `lazysloth` with the correct package name if published on PyPI or provide specific instructions if the library is to be installed directly from a source repository.\n\n## Features\n\n- **LazyVariable:** Enables lazy initialization of variables, where the value is only computed upon the first access.\n- **ImmutableVariable:** Wraps values in an immutable container, preventing changes after initialization.\n\n## Quick Start\n\nHere's a quick example to get started with `lazysloth`, demonstrating the lazy loading behavior:\n\n```python\nfrom lazysloth import LazyVariable\n\ndef expensive_computation():\n    print(\"Expensive computation is now being executed...\")\n    return {\"data\": 42}\n\n# Initialize a LazyVariable with the expensive computation.\nlazy_var = LazyVariable(expensive_computation)\n\nprint(\"LazyVariable has been initialized.\")\nprint(\"Accessing the variable to trigger computation...\")\n\n# The expensive computation is executed only upon this access.\nprint(lazy_var.data)  # Outputs: Expensive computation is now being executed... \\n 42\n\n```\n\n## Usage\n\n```python\ndef expensive_operation():\n    print(\"Performing an expensive operation that takes time...\")\n    return \"Result of the operation\"\n\nlazy_operation = LazyVariable(expensive_operation)\n\nprint(\"LazyVariable initialized, but expensive operation hasn't run yet.\")\n# At this point, the expensive operation has not been executed.\n\n# Accessing the LazyVariable to trigger the expensive operation.\nresult = lazy_operation()\nprint(f\"Operation result: {result}\")\n# Now, the expensive operation is executed, and its result is accessed.\n\n```\n\n## Features\n\n- **LazyVariable:** Enables lazy initialization of variables, where the value is only computed upon the first access.\n- **ImmutableVariable:** Wraps values in an immutable container, preventing changes after initialization.\n\n### LazyVariable\n\n`LazyVariable` is intended for situations where a variable's initialization is computationally expensive and not always necessary. The variable's value is computed only on the first access.\n\n#### Initialization\n\n```python\nlazy_var = LazyVariable(initializer_function, *args, **kwargs)\n```\n\n- `initializer_function`: A callable that returns the value to be lazily initialized.\n- `*args` and `**kwargs`: Arguments passed to the `initializer_function`.\n\n#### Access\n\nAccess attributes or items of the lazily initialized value as if `LazyVariable` were the value itself:\n\n```python\nresult = lazy_var.attribute\nresult = lazy_var['item']\n```\n\n#### Callable Support\n\nIf the lazily initialized value is callable, `LazyVariable` can be called directly:\n\n```python\nresult = lazy_var(*args, **kwargs)\n```\n\n### ImmutableVariable\n\n`ImmutableVariable` ensures that the wrapped value cannot be modified after initialization. It's useful for creating read-only data structures.\n\n- `value`: The value to be made immutable.\n\n#### Access\n\nAccess the immutable value's attributes or items directly:\n\n```python\nresult = immutable_var.attribute\nresult = immutable_var['item']\n```\n\nAttempting to modify the wrapped value results in an `AttributeError`.\n\n## Advanced Usage\n\n- **Resetting a `LazyVariable`:** If your use case requires re-initializing a `LazyVariable`, you can extend its functionality to support resetting.\n\n## Contributing\n\nContributions to `lazysloth` are welcome! Please refer to the contributing guidelines for more information on how to submit pull requests, report issues, or request features.\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "A Python library for lazy and immutable variables.",
    "version": "0.0.6",
    "project_urls": {
        "Homepage": "https://github.com/omerholz/lazysloth",
        "Repository": "https://github.com/omerholz/lazysloth"
    },
    "split_keywords": [
        "lazy",
        " immutable",
        " variables"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "493f873f4e5e00222490e5e08e5051c632f46f66f9e46309296d821b1c68f1c3",
                "md5": "ad1284efd3814c7cd1462d1b42abc3a6",
                "sha256": "cd09da312025759d35c61b29338a213cf0ef5cd65e2d08c05dec0f5a9211cc95"
            },
            "downloads": -1,
            "filename": "lazysloth-0.0.6-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "ad1284efd3814c7cd1462d1b42abc3a6",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10",
            "size": 6059,
            "upload_time": "2024-03-21T18:18:56",
            "upload_time_iso_8601": "2024-03-21T18:18:56.791224Z",
            "url": "https://files.pythonhosted.org/packages/49/3f/873f4e5e00222490e5e08e5051c632f46f66f9e46309296d821b1c68f1c3/lazysloth-0.0.6-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c3b74064050901d311b8e37bb0c1653eba6da701fbbc08dbb0c975bc85b88917",
                "md5": "c54e8f79f678647201d3b8eef1a81e86",
                "sha256": "1eecb4237c07eca2ba0905490ddab28b241958d4213087afefe7e2bd6b6c6d1b"
            },
            "downloads": -1,
            "filename": "lazysloth-0.0.6.tar.gz",
            "has_sig": false,
            "md5_digest": "c54e8f79f678647201d3b8eef1a81e86",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10",
            "size": 9399,
            "upload_time": "2024-03-21T18:18:58",
            "upload_time_iso_8601": "2024-03-21T18:18:58.487320Z",
            "url": "https://files.pythonhosted.org/packages/c3/b7/4064050901d311b8e37bb0c1653eba6da701fbbc08dbb0c975bc85b88917/lazysloth-0.0.6.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-03-21 18:18:58",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "omerholz",
    "github_project": "lazysloth",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "lazysloth"
}
        
Elapsed time: 0.22114s