Skip to main content

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.0-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)

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.1.tar.gz (5.3 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.1-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pychainable-0.1.1.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.1

File hashes

Hashes for pychainable-0.1.1.tar.gz
Algorithm Hash digest
SHA256 517ff6d9e509b860013ba54f1514d599b8e32d33e00790c5c1942a87a1ce341e
MD5 2970447eb45d5e30107ab3bc74e48dc8
BLAKE2b-256 f840a8c6e00b44c3c84177b2485f8492a22aaf5e3db703a72c9f244ae3bb4fe8

See more details on using hashes here.

File details

Details for the file pyChainable-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: pyChainable-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.1

File hashes

Hashes for pyChainable-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 17f02b273a10a3fe079e4af9b80472782488daf59451d1e99738192dee19c8b1
MD5 d87b6057ead17c29d60aec983c60c3c6
BLAKE2b-256 864a6df3fe49d05bdc70b19d30583908c2357cdfa0ae30287fc67138d659de3b

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