The scattr Python package
The scattr package has one function, scattr,
that provides an easy way to
Set Class ATTRibutes of classes derived
from pre-existing Python classes.
The scattr function takes
- a class object and
- the name of a helper script
and returns a subclass called
SubClassAttributesthat contains the methods defined in the helper script.
Essentially, this is an easy way to add user-defined functions to classes.
Class variables, method and classes can be added dynamically:
import pandas as pd
import math
# add pi to a pandas DataFrame
pd.DataFrame.pi = math.pi
The scattr package provides a cleaner way to
add new attributes, because it creates a new subclass and
uses method and class defined in a separate helper script.
The scattr function only adds
callable objects(methods and classes), not variables.
Pandas DataFrame example
import pandas as pd
from scattr import scattr
# create a new class that inherits from pd.DataFrame
# and includes methods defined in a 'helper.py' file
ScattrFrame = scattr(cls=pd.DataFrame, src='helper')
# instantiate the new class
df = ScattrFrame(data=pd.read_csv('risk_factors_cervical_cancer.csv'))
# test methods added from helper file
df.say_hi()
# test CowClass added from helper file
df.CowClass.say_moo()
# test method from parent class
df.head(n=1)
# confirm that df is an instance of pd.DataFrame and PydyFrame
isinstance(df, (pd.DataFrame, ScattrFrame))
# confirm that ScattrFrame is a subclass of pd.DataFrame
issubclass(ScattrFrame, pd.DataFrame)
Release files for scattr 0.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| scattr-0.0.1.tar.gz | 2.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| scattr-0.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 4.4 kB
Release files / scattr-0.0.1.tar.gz
| Download URL | scattr-0.0.1.tar.gz |
|---|---|
| Size | 2.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d920b3b56342a9abea603068844572f95c437943f0f5c4c991d7284540a6d6ae
|
|
BLAKE2b-256 checksum How to use checksums |
a828a6c69e338098553d2566636687e968046c6a704700487ce885b84b32e395
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.12.1 pkginfo/1.4.2 requests/2.18.4 setuptools/39.1.0 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.5
|
Release files / scattr-0.0.1-py3-none-any.whl
| Download URL | scattr-0.0.1-py3-none-any.whl |
|---|---|
| Size | 2.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
fdfbb75f73cb97af032aaa9bda8dac3f4f1c102194d757155fbcee7806530b01
|
|
BLAKE2b-256 checksum How to use checksums |
388bd36fc549f998ff9745ab3cbc247f575f826448d26cde4963bb73df655883
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.12.1 pkginfo/1.4.2 requests/2.18.4 setuptools/39.1.0 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.5
|