extendanything


Nameextendanything JSON
Version 0.0.1 PyPI version JSON
download
home_pagehttps://github.com/maximz/extendanything
SummaryWrap Instance
upload_time2023-04-12 00:49:57
maintainer
docs_urlNone
authorMaxim Zaslavsky
requires_python>=3.8
licenseMIT license
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # ExtendAnything (WIP)

[![](https://img.shields.io/pypi/v/extendanything.svg)](https://pypi.python.org/pypi/extendanything)
[![CI](https://github.com/maximz/extendanything/actions/workflows/ci.yaml/badge.svg?branch=master)](https://github.com/maximz/extendanything/actions/workflows/ci.yaml)
[![](https://img.shields.io/badge/docs-here-blue.svg)](https://extendanything.maximz.com)
[![](https://img.shields.io/github/stars/maximz/extendanything?style=social)](https://github.com/maximz/extendanything)


## Inject new functionality into Python objects

Extend any already-created Python object with new functionality, or replace its inner logic.

Suppose you trained a scikit-learn classifier and want to inject custom logic. The classifier instance already exists, and now you want it to have a `featurize()` method and a different rule for predicting class labels with `predict()`.

Here's how to modify the behavior of your existing scikit-learn classifier object with ExtendAnything:

```python
# pip install extendanything
from extendanything import ExtendAnything

# Create the original instance
clf = sklearn.linear_model.LogisticRegression().fit(X_train, y_train)

# Define a wrapper class with the functionality you want.
class CustomClassifier(ExtendAnything):
    # The constructor accepts the existing instance you want to wrap.
    def __init__(self, model):
        super().__init__(model)

    # Add functionality: introduce a brand new method.
    def featurize(self, df: pd.DataFrame) -> np.ndarray:
        X = df.values # ...
        return X

    # Replace the existing predict() with new logic:
    def predict(self, X: np.ndarray) -> np.ndarray:
        # For example, return the least likely class.
        # Here, classes_ and predict_proba come from the original model.
        return self.classes_[np.argmin(self.predict_proba(X), axis=-1)]

# Apply the wrapper
wrapped_clf = CustomClassifier(clf)

# Use standard scikit-learn classifier functionality
# These calls are passed through without modification
classes = wrapped_clf.classes_

# Use new or modified functionality
# These calls use your CustomClassifier's logic
X_test = wrapped_clf.featurize(df_test)
y_test = wrapped_clf.predict(X_test)
```

## What's happening here?

You can think of `CustomClassifier(clf)` as dynamically casting the scikit-learn classifier object to a subclass of your choice.

* Instantiate the `CustomClassifier` wrapper class by passing your existing object:`super().__init__(model)` sets `self._inner = model`.

* Get: Any unknown attribute accesses are passed through to `self._inner`. This means you can access the original object's attributes and methods, without defining them explicitly in the wrapper class.

* Set: Setting attributes detaches them, rather than modifying the inner instance.

* You can access the original object's functionality with `self._inner`.

* It's pickle-able.

## Limitations

* Logic defined in the base (wrapped) class can't access the derived (wrapper) class's overloaded methods.
  * Example: If you call `predict()` and that's not overloaded in your wrapper class, the call is passed through to your original wrapped instance. If `predict()` calls `predict_proba()`, it will use the original object's `predict_proba()`, _even if you created an overloaded `predict_proba()` in your wrapper class.

* Modifying attributes on the wrapped instance detaches those instance attributes, rather than modifying the inner instance.
  * Future accesses will hit the detached version belonging to the wrapper, not to the inner instance.
  * If needed, you can propogate changes to the inner instance by modifying `self._inner` directly. Attribute modifications directly on the base inner instance will be visible through the outer wrapped class's attributes, unless those attributes have already been detached by being modified on the outer instance.

* The final wrapped instance is not an official subclass of the original class. (It will not pass an `isinstance` check).

* Indexing like `[0]` is not currently passed through to the inner instance. For now use `.inner[0]`.

## Development

Submit PRs against `develop` branch, then make a release pull request to `master`.

```bash
# Install requirements
pip install --upgrade pip wheel
pip install -r requirements_dev.txt

# Install local package
pip install -e .

# Install pre-commit
pre-commit install

# Run tests
make test

# Run lint
make lint

# bump version before submitting a PR against master (all master commits are deployed)
bump2version patch # possible: major / minor / patch

# also ensure CHANGELOG.md updated
```

## TODOs: Configuring this template

Create a Netlify site for your repository, then turn off automatic builds in Netlify settings.

Add these CI secrets: `PYPI_API_TOKEN`, `NETLIFY_AUTH_TOKEN` (Netlify user settings - personal access tokens), `DEV_NETLIFY_SITE_ID`, `PROD_NETLIFY_SITE_ID` (API ID from Netlify site settings)

Set up Codecov at TODO


# Changelog

## 0.0.1

* First release on PyPI.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/maximz/extendanything",
    "name": "extendanything",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "",
    "keywords": "",
    "author": "Maxim Zaslavsky",
    "author_email": "maxim@maximz.com",
    "download_url": "https://files.pythonhosted.org/packages/c8/e5/e98ebcfdc79cc9bc12fd6083e8d5fc6b92a9a09c9d3a1bab20dc55d1eccc/extendanything-0.0.1.tar.gz",
    "platform": null,
    "description": "# ExtendAnything (WIP)\n\n[![](https://img.shields.io/pypi/v/extendanything.svg)](https://pypi.python.org/pypi/extendanything)\n[![CI](https://github.com/maximz/extendanything/actions/workflows/ci.yaml/badge.svg?branch=master)](https://github.com/maximz/extendanything/actions/workflows/ci.yaml)\n[![](https://img.shields.io/badge/docs-here-blue.svg)](https://extendanything.maximz.com)\n[![](https://img.shields.io/github/stars/maximz/extendanything?style=social)](https://github.com/maximz/extendanything)\n\n\n## Inject new functionality into Python objects\n\nExtend any already-created Python object with new functionality, or replace its inner logic.\n\nSuppose you trained a scikit-learn classifier and want to inject custom logic. The classifier instance already exists, and now you want it to have a `featurize()` method and a different rule for predicting class labels with `predict()`.\n\nHere's how to modify the behavior of your existing scikit-learn classifier object with ExtendAnything:\n\n```python\n# pip install extendanything\nfrom extendanything import ExtendAnything\n\n# Create the original instance\nclf = sklearn.linear_model.LogisticRegression().fit(X_train, y_train)\n\n# Define a wrapper class with the functionality you want.\nclass CustomClassifier(ExtendAnything):\n    # The constructor accepts the existing instance you want to wrap.\n    def __init__(self, model):\n        super().__init__(model)\n\n    # Add functionality: introduce a brand new method.\n    def featurize(self, df: pd.DataFrame) -> np.ndarray:\n        X = df.values # ...\n        return X\n\n    # Replace the existing predict() with new logic:\n    def predict(self, X: np.ndarray) -> np.ndarray:\n        # For example, return the least likely class.\n        # Here, classes_ and predict_proba come from the original model.\n        return self.classes_[np.argmin(self.predict_proba(X), axis=-1)]\n\n# Apply the wrapper\nwrapped_clf = CustomClassifier(clf)\n\n# Use standard scikit-learn classifier functionality\n# These calls are passed through without modification\nclasses = wrapped_clf.classes_\n\n# Use new or modified functionality\n# These calls use your CustomClassifier's logic\nX_test = wrapped_clf.featurize(df_test)\ny_test = wrapped_clf.predict(X_test)\n```\n\n## What's happening here?\n\nYou can think of `CustomClassifier(clf)` as dynamically casting the scikit-learn classifier object to a subclass of your choice.\n\n* Instantiate the `CustomClassifier` wrapper class by passing your existing object:`super().__init__(model)` sets `self._inner = model`.\n\n* Get: Any unknown attribute accesses are passed through to `self._inner`. This means you can access the original object's attributes and methods, without defining them explicitly in the wrapper class.\n\n* Set: Setting attributes detaches them, rather than modifying the inner instance.\n\n* You can access the original object's functionality with `self._inner`.\n\n* It's pickle-able.\n\n## Limitations\n\n* Logic defined in the base (wrapped) class can't access the derived (wrapper) class's overloaded methods.\n  * Example: If you call `predict()` and that's not overloaded in your wrapper class, the call is passed through to your original wrapped instance. If `predict()` calls `predict_proba()`, it will use the original object's `predict_proba()`, _even if you created an overloaded `predict_proba()` in your wrapper class.\n\n* Modifying attributes on the wrapped instance detaches those instance attributes, rather than modifying the inner instance.\n  * Future accesses will hit the detached version belonging to the wrapper, not to the inner instance.\n  * If needed, you can propogate changes to the inner instance by modifying `self._inner` directly. Attribute modifications directly on the base inner instance will be visible through the outer wrapped class's attributes, unless those attributes have already been detached by being modified on the outer instance.\n\n* The final wrapped instance is not an official subclass of the original class. (It will not pass an `isinstance` check).\n\n* Indexing like `[0]` is not currently passed through to the inner instance. For now use `.inner[0]`.\n\n## Development\n\nSubmit PRs against `develop` branch, then make a release pull request to `master`.\n\n```bash\n# Install requirements\npip install --upgrade pip wheel\npip install -r requirements_dev.txt\n\n# Install local package\npip install -e .\n\n# Install pre-commit\npre-commit install\n\n# Run tests\nmake test\n\n# Run lint\nmake lint\n\n# bump version before submitting a PR against master (all master commits are deployed)\nbump2version patch # possible: major / minor / patch\n\n# also ensure CHANGELOG.md updated\n```\n\n## TODOs: Configuring this template\n\nCreate a Netlify site for your repository, then turn off automatic builds in Netlify settings.\n\nAdd these CI secrets: `PYPI_API_TOKEN`, `NETLIFY_AUTH_TOKEN` (Netlify user settings - personal access tokens), `DEV_NETLIFY_SITE_ID`, `PROD_NETLIFY_SITE_ID` (API ID from Netlify site settings)\n\nSet up Codecov at TODO\n\n\n# Changelog\n\n## 0.0.1\n\n* First release on PyPI.\n",
    "bugtrack_url": null,
    "license": "MIT license",
    "summary": "Wrap Instance",
    "version": "0.0.1",
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "7cd39433a4066d511a66b51ef87cac92265166e9865f6b05b53d03cb0af88ee4",
                "md5": "4172301538a847946d217418b91e5d89",
                "sha256": "25eeed23cf2f651e772dca77bd79eeb9529df675ddabf4c158f3b419ebc444b6"
            },
            "downloads": -1,
            "filename": "extendanything-0.0.1-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "4172301538a847946d217418b91e5d89",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": ">=3.8",
            "size": 6124,
            "upload_time": "2023-04-12T00:49:54",
            "upload_time_iso_8601": "2023-04-12T00:49:54.391353Z",
            "url": "https://files.pythonhosted.org/packages/7c/d3/9433a4066d511a66b51ef87cac92265166e9865f6b05b53d03cb0af88ee4/extendanything-0.0.1-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c8e5e98ebcfdc79cc9bc12fd6083e8d5fc6b92a9a09c9d3a1bab20dc55d1eccc",
                "md5": "7b6cf431b35acc2757e3d3ca3abe72ec",
                "sha256": "e8411bdd140aac1264f5a2f7ba409a7930552330e58dbf7fd81669ec9875c2d5"
            },
            "downloads": -1,
            "filename": "extendanything-0.0.1.tar.gz",
            "has_sig": false,
            "md5_digest": "7b6cf431b35acc2757e3d3ca3abe72ec",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 12175,
            "upload_time": "2023-04-12T00:49:57",
            "upload_time_iso_8601": "2023-04-12T00:49:57.672735Z",
            "url": "https://files.pythonhosted.org/packages/c8/e5/e98ebcfdc79cc9bc12fd6083e8d5fc6b92a9a09c9d3a1bab20dc55d1eccc/extendanything-0.0.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-04-12 00:49:57",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "maximz",
    "github_project": "extendanything",
    "lcname": "extendanything"
}
        
Elapsed time: 0.34759s