Skip to main content

Monapy

Python Library for declarative programming.

Binder - simple monad implementation.

Binder is binding functions to chain. The result of the previous function is passed to a next function as positional argument. Right arrows bind functions into the chain. Left arrows set positional argument for last function in chain.

Examples:
>>> from monapy import Binder
>>> binder = Binder() >> range >> map << ( lambda i: i * 10 )
>>> list( binder( 10 ) )
[0, 10, 20, 30, 40, 50, 60, 70, 80, 90]
>>> binder >> filter << ( lambda i: not i % 20 ) >> list
>>> binder( 10 )
[0, 20, 40, 60, 80]

Package Step

Step is a functional unit, a step is binding with other step for making chain steps. Class Step must be implemented, method 'make' is main that take value and generate iterator. First step take value and generating iterator, that iteratively by one values is passed other step, this process continue while to the last step in the chain. Bindings define which step the value is passed to. Steps may be bind by next binding methods: bind, loop_bind, or_bind.

Bind

The values (in iterator) is passed to the next step.

Examples:
>>> from monapy import Step
>>> chain1 = Step() >> Step()
>>> chain2 = Step().bind( Step() )
>>> chain3 = StepChain( [ Step(), Step() ] )
>>> chain4 = Step() >> Step() >> Step()

Loop

This bind make loop, first step's values (in iterator) is passed to second step and out of this chain, second step by each value is generating values (iterator), and this values is passed to first step, then all repeat until empty iterator from second step.

(iter) --> value ------> First Step --> (iter) --> value ------>
                    ^                                       |
                    |                                       |
                     -- value <-- (iter) <-- Second Step <--
Examples:
>>> from monapy import Step
>>> chain1 = Step() << Step()
>>> chain2 = Step().loop( Step() )
>>> chain3 = LoopStep( Step(), Step() )

Or-Bind

It like 'or' logical expression, a first non-empty iterator is passed to out of this chain.

Examples:
>>> from monapy import Step
>>> chain1 = Step() | Step()
>>> chain2 = Step().or_bind( Step() )
>>> chain3 = OrChain( [ Step(), Step() ] )
>>> chain4 = Step() | Step() | Step()

Internal structure of chain

To view, how steps binded, call 'tree' method and print result.

Examples:
>>> chain = Step() >> ~( Step() >> Step() >> Step() ) << Step() >> ( Step() | Step() | Step() )
>>> print( chain.tree() )
StepChain(3)
   |__Step()
   |__LoopStep()
   |     |__StepChain(3)
   |     |     |__Step()
   |     |     |__Step()
   |     |     |__Step()
   |     |
   |     |_<< Step()
   |
   |__OrChain(3)
          |__Step()
          |__Step()
          |__Step()
Detailed tree
>>> print( chain.tree( full=True ) )
StepChain(3)
   |__Step()
   |__LoopStep()
   |     |__UnitedSteps()
   |     |     |__StepChain(3)
   |     |            |__Step()
   |     |            |__Step()
   |     |            |__Step()
   |     |
   |     |_<< Step()
   |
   |__OrChain(3)
          |__Step()
          |__Step()
          |__Step()

Combining steps

Sometimes need to make a separate sub-chain, for this exists '~' expression.

Examples:
>>> from monapy import Step
>>> chain1 = ~( Step() >> Step() >> Step() ) << Step()
>>> print( chain1.tree() )
LoopStep()
   |__StepChain(3)
   |     |__Step()
   |     |__Step()
   |     |__Step()
   |
   |_<< Step()

>>> chain2 = Step() >> Step() >> Step() << Step()
>>> print( chain2.tree() )
StepChain(3)
   |__Step()
   |__Step()
   |__LoopStep()
          |__Step()
          |_<< Step()

Show UnitedSteps in tree

>>> print( chain1.tree( show_union=True ) )
LoopStep()
   |__UnitedSteps()
   |     |__StepChain(3)
   |            |__Step()
   |            |__Step()
   |            |__Step()
   |
   |_<< Step()

Run chain

For run chain call 'make' method, that return iterator.

Examples:
>>> from monapy import Step
>>> chain = Step() >> Step() << Step() >> ( Step() | Step() | Step() )
>>> for val in chain.make( value ):
>>>     print( val )

>>> for val in chain( value ):
>>>     print( val )

Packing values in standard data structures

4 structures supported: tuple, list, dict, set. Values pack the corresponding steps, to create this step need bind the chain with corresponding structure of steps, or call a corresponding class. From each step, one value is taken and pack into structure.

Pack in tuple

It's like 'zip' function, if every step returns a value then those values ​​are packed into a tuple, otherwise no packing happens and values ​​aren't returned.

Examples:
>>> from monapy import Step
>>> chain1 = Step() >> ( Step(), Step(), Step() )
>>> chain2 = TupleStep( ( Step(), Step(), Step() ) )
>>> chain3 = to_step( ( Step(), Step(), Step() ) )
Pack in list
Examples:
>>> from monapy import Step
>>> chain1 = Step() >> [ Step(), Step(), Step() ]
>>> chain2 = ListStep( [ Step(), Step(), Step() ] )
>>> chain3 = to_step( [ Step(), Step(), Step() ] )
Pack in dict
Examples:
>>> from monapy import Step
>>> chain1 = Step() >> { 'key1': Step(), 'key2': Step(), 'key3': Step() }
>>> chain2 = DictStep( { 'key1': Step(), 'key2': Step(), 'key3': Step() } )
>>> chain3 = to_step( { 'key1': Step(), 'key2': Step(), 'key3': Step() } )
Pack in set
Examples:
>>> from monapy import Step
>>> chain1 = Step() >> { Step(), Step(), Step() }
>>> chain2 = SetStep( { Step(), Step(), Step() } )
>>> chain3 = to_step( { Step(), Step(), Step() } )

Step class implementation

Class Step has two methods to implement, it's 'make' and 'make_all'. The 'make' method takes a value and generates an iterator, that is passed to the next step. This method takes one positional argument to get the value, and also accepts named arguments, that are used to pass settings for all steps in the chain. The method 'make_all' is taking an iterator of values that it passes to the 'make' method, it could be used for paralleling.

Metadata

Release files for monapy 0.7.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for monapy 0.7.0
File Interpreter ABI Platform Actions
monapy-0.7.0-py3-none-any.whl Python 3 none any Details for monapy-0.7.0-py3-none-any.whl

Release files / monapy-0.7.0-py3-none-any.whl

Download URL monapy-0.7.0-py3-none-any.whl
Size 8.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6867d44a4bc59f2ba9489456a18b5088e22756d3826a74857a4edc4da3c54ba6
BLAKE2b-256 checksum
How to use checksums
fe4e54f818ec408966e229d6c755e4661ec2fa0134717f705caf1b038e7e797f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.2

Security for monapy 0.7.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page