Package: extendanything
ExtendAnything (WIP)Inject new functionality into Python objectsExtend 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 afeaturize()method and a different rule for predicting class labels withpredict().Here's how to modify the behavior of your existing scikit-learn classifier object with ExtendAnything:# pip install extendanythingfromextendanythingimportExtendAnything# Create the original instanceclf=sklearn.linear_model.LogisticRegression().fit(X_train,y_train)# Define a wrapper class with the functionality you want.classCustomClassifier(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.deffeaturize(self,df:pd.DataFrame)->np.ndarray:X=df.values# ...returnX# Replace the existing predict() with new logic:defpredict(self,X:np.ndarray)->np.ndarray:# For example, return the least likely class.# Here, classes_ and predict_proba come from the original model.returnself.classes_[np.argmin(self.predict_proba(X),axis=-1)]# Apply the wrapperwrapped_clf=CustomClassifier(clf)# Use standard scikit-learn classifier functionality# These calls are passed through without modificationclasses=wrapped_clf.classes_# Use new or modified functionality# These calls use your CustomClassifier's logicX_test=wrapped_clf.featurize(df_test)y_test=wrapped_clf.predict(X_test)What's happening here?You can think ofCustomClassifier(clf)as dynamically casting the scikit-learn classifier object to a subclass of your choice.Instantiate theCustomClassifierwrapper class by passing your existing object:super().__init__(model)setsself._inner = model.Get: Any unknown attribute accesses are passed through toself._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 withself._inner.It's pickle-able.LimitationsLogic defined in the base (wrapped) class can't access the derived (wrapper) class's overloaded methods.Example: If you callpredict()and that's not overloaded in your wrapper class, the call is passed through to your original wrapped instance. Ifpredict()callspredict_proba(), it will use the original object'spredict_proba(), _even if you created an overloadedpredict_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 modifyingself._innerdirectly. 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 anisinstancecheck).Indexing like[0]is not currently passed through to the inner instance. For now use.inner[0].DevelopmentSubmit PRs againstdevelopbranch, then make a release pull request tomaster.# Install requirementspipinstall--upgradepipwheel
pipinstall-rrequirements_dev.txt# Install local packagepipinstall-e.# Install pre-commitpre-commitinstall# Run testsmaketest# Run lintmakelint# bump version before submitting a PR against master (all master commits are deployed)bump2versionpatch# possible: major / minor / patch# also ensure CHANGELOG.md updatedTODOs: Configuring this templateCreate 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 TODOChangelog0.0.1First release on PyPI.