Like 2 Chainz but better!
Project description
nchainz - Automatic Method Chaining in Python
A metaclass for automatic method chaining. (Only for methods that return None
, obviously)
from nchainz import Chainz
class A(metaclass=Chainz):
def has_return_value(self):
return 4
def this_is_chainable(self):
print("hello")
# implicitly returns self
a = A()
assert a = a.this_is_chainable().this_is_chainable()
assert a.has_return_value() == 4
What is Method Chaining?
Method chaining describes the Syntax of not having to assign objects between methods which are changing the state.
For example, in JS one can just chain the array transformations like
[1,2,3,4,5,6].filter(x => x % 2 == 0).map(x => x * x).find(x => x > 30)
How to do it manually?
Pretty easy. You just return self
. Here is an example:
class MyNum:
def __init__(self, x):
self.x = x
def inc(self):
self.x += 1
return self
three = MyNum(3)
six = three.inc().inc().inc()
assert three.x+3 == six.x
Install
pip install nchainz
Use
Just use the Chainz
metaclass:
from nchainz import Chainz
class MyClass(metaclass=Chainz):
...
Why? Like seriously, Why?
I had my 5 minutes, I am sorry.
Further reading
It's such a great read, you should really read it. (Written for Python 2)
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
nchainz-1.0.0.tar.gz
(3.3 kB
view details)
Built Distribution
File details
Details for the file nchainz-1.0.0.tar.gz
.
File metadata
- Download URL: nchainz-1.0.0.tar.gz
- Upload date:
- Size: 3.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.14 CPython/3.7.13 Linux/5.15.48-gentoo-dist
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8e5a4d44b8fd29d90d0bb9383b7633ce0c7b409fa059310e5c1196c1acffb341 |
|
MD5 | d5322e5eb36945f1c6bb55e29ab26b34 |
|
BLAKE2b-256 | 3152d725264b2a963200e37367a46f65142b9dc4879c05bccede66dfcd5a61f1 |
File details
Details for the file nchainz-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: nchainz-1.0.0-py3-none-any.whl
- Upload date:
- Size: 3.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.14 CPython/3.7.13 Linux/5.15.48-gentoo-dist
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9330d460ed3fbc9f0bce40656925cabceb33bb2ec6d87b269daef7a0c545ac67 |
|
MD5 | fde4294b08448fb8394daaadf60dfa64 |
|
BLAKE2b-256 | 2f102db6709a90995a693bc978157516f93bbbaf22e1c07253ffbe51d3c9a9d2 |