Add user-defined methods to Python classes.
Project description
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)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file scattr-0.0.1.tar.gz.
File metadata
- Download URL: scattr-0.0.1.tar.gz
- Upload date:
- Size: 2.1 kB
- Tags: Source
- Uploaded using 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
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d920b3b56342a9abea603068844572f95c437943f0f5c4c991d7284540a6d6ae
|
|
| MD5 |
dfce05d527dc10eef5aa56aa11d667fb
|
|
| BLAKE2b-256 |
a828a6c69e338098553d2566636687e968046c6a704700487ce885b84b32e395
|
File details
Details for the file scattr-0.0.1-py3-none-any.whl.
File metadata
- Download URL: scattr-0.0.1-py3-none-any.whl
- Upload date:
- Size: 2.3 kB
- Tags: Python 3
- Uploaded using 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
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fdfbb75f73cb97af032aaa9bda8dac3f4f1c102194d757155fbcee7806530b01
|
|
| MD5 |
a610ef63a448a73baa456ef4e12239b9
|
|
| BLAKE2b-256 |
388bd36fc549f998ff9745ab3cbc247f575f826448d26cde4963bb73df655883
|