Skip to main content

A package for creating chainable method calls in Python

Project description

pyChainable

PyPI version Python versions License: MIT CI

中文文档 | English Documentation

pyChainable 是一个 Python 包,允许您创建可链式调用的方法,同时保持对原始值的操作能力。

安装

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)  # 9

函数链式调用

@pipeline 装饰器允许将多个函数链接在一起,以便在执行时按顺序调用它们。上一个函数的返回值将作为下一个函数的参数。

from pychain import 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)  # 12

字符串操作

from pychain import pipeline

class StrTest:
    @pipeline
    def add(self, s: str) -> str:
        return s + '.'

    @pipeline
    def sp(self, s: str) -> str:
        return s + ','

getString = StrTest()
res = getString.add("word").sp().add().sp().add()
print(res)            # word.,.,.,
print(res.split(',')) # ['word.', '.', '.,']

矩阵运算

@pipeline 可以结合 dataclass 使用,以实现矩阵运算的链式调用。

from dataclasses import dataclass
from pychain import pipeline

@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)
# continue with other operations
res2 = res.scale(factor=0.5)

License

MIT

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

pychainable-0.1.3.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pychainable-0.1.3-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file pychainable-0.1.3.tar.gz.

File metadata

  • Download URL: pychainable-0.1.3.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pychainable-0.1.3.tar.gz
Algorithm Hash digest
SHA256 7e8d5d84bcdd9c6f17de3f0dc75ae9a6c0d4d13688d73dce5deacd7bed9daf13
MD5 46765ef8b3c2409bb91e90b65c143e27
BLAKE2b-256 8a8352530e3c23b11e49bc8cc1075c932541cbb31c66c0975e6637686445e9da

See more details on using hashes here.

File details

Details for the file pychainable-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: pychainable-0.1.3-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.13.12

File hashes

Hashes for pychainable-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 ea427f1681e3aece7d1a2ebba76dfa4c945bd6d34a66b47d4439d6c6e3e3e31d
MD5 808861fa19de207e32e27b362a0d4e74
BLAKE2b-256 ab990cb9a1eb1babab1cd1cf4bdac6ff46cc2185ebd50b8ffe903eb413b7339e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page