A package for creating chainable method calls in Python
Project description
pyChainable
pyChainable 是一个 Python 包,允许您创建可链式调用的方法,同时保持对原始值的操作能力。
安装
使用以下命令安装:
pip install -r requirements.txt
python -m build
pip install dist/pyChainable-0.1.1-py3-none-any.whl
或者
pip install pyChainable
使用
方法链式调用
@chainable 装饰器允许您创建可链式调用的方法。
from pychain import chainable
class MyClass:
def __init__(self):
self.value = 0
@chainable
def add(self, num):
self.value += num
return self.value
@chainable
def multiply(self, num):
self.value *= num
return self.value
obj = MyClass()
result = obj.add(1).add(2).multiply(3)
print(result)
函数链式调用
@pipeline 装饰器允许将多个函数数链接在一起,以便在执行时按顺序调用它们。上一个函数的返回值将作为下一个函数的参数。
class TestClass:
@pipeline
def add_one(self, x: int) -> int:
return x + 1
@pipeline
def add_two(self, x: int) -> int:
return x + 2
test = TestClass()
test.add_one(2).add_two().add_two().add_two().add_two().add_two()
print(test)
字符串操作同样可用
from pychain.pipeline import PipelineResult
class StrTest:
@pipeline
def add(self, s : str) -> str:
return s + '.'
@pipeline
def sp(self, s: str) -> str:
return s + ','
getString = StrTest()
res3 : str = test3.add("word").sp().add().sp().add()
print(res3)
print(res3.split(','))
矩阵运算
@pipeline 可以结合dataclass使用,以实现矩阵运算的链式调用。
from dataclasses import dataclass
@dataclass(slots=True)
class Matrix:
a : float
b : float
c : float
d : float
@pipeline
def rotate(self, x, y):
return (self.a * x + self.b * y, self.c * x + self.d * y)
@pipeline
def scale(self, x, y, factor: float):
return (x * factor, y * factor)
m = Matrix(0, 1, -1, 0)
res = m.rotate(2,3).scale(factor=2)
res
# continue with other operations
res2 = res.scale(factor=1/2)
res2
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
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 pychainable-0.1.2.tar.gz.
File metadata
- Download URL: pychainable-0.1.2.tar.gz
- Upload date:
- Size: 5.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b947bfcf4829b2fabad777208b53b54922152a3053e2f69ab34189b0a8489c10
|
|
| MD5 |
e0a30396b4d7343e4d52ec7acf513004
|
|
| BLAKE2b-256 |
af28334050f3af52d2786a2f902437e920dbe4ce90a28d237aee3241a7242806
|
File details
Details for the file pychainable-0.1.2-py3-none-any.whl.
File metadata
- Download URL: pychainable-0.1.2-py3-none-any.whl
- Upload date:
- Size: 5.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a6cfb56820a3e75c5ac13676b339cad8d15e700dba11e5a6bc4e386ea6159e35
|
|
| MD5 |
aed80a4216e251beca623aa0c382891a
|
|
| BLAKE2b-256 |
5d6477c5bbebb60e615f6fe96c39a44563dd3f929d08fd25cbaa9e030b26961a
|
File details
Details for the file pyChainable-0.1.2-py3-none-any.whl.
File metadata
- Download URL: pyChainable-0.1.2-py3-none-any.whl
- Upload date:
- Size: 5.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3a68f4e6da94979769a872b282de4fe9015d2a9bef88a8aca189946fffda408e
|
|
| MD5 |
6b8786341310d8ca232f1ab028836e7e
|
|
| BLAKE2b-256 |
ea2991c7408f51dd4456a2fdd51df3f4117c3e619dab28d78aafadff9af3ce4c
|