Class for making any object have a chainable API.
Because chained APIs are good. And returning self from every function is bad.
# Import chain
from chain import Chain
# Simple case
(Chain(square)
.set_left_corner(3)
.set_right_corner(4)
.make_3d(length=4)
.some_other_awesome_square_function()
)
# Equivalent to
# square.set_left_corner(3)
# square.set_right_corner(4)
# square.make_3d(length=4)
# square.some_other_awesome_square_function()
# Storing values
values = (
Chain(myobj)
.do_task1()
.do_task2()
.chain_store("task2_value")
.do_task3()
.do_task4()
.chain_store("task4_value")
.do_task5()
.chain_get_stored()
)
# values will be {"task2_value" : <value from do_task2>, "task4_value" : <value from do_task4>}
# Equivalent to
# values = {}
# myobj.do_task1()
# values['task2_value'] = myobj.do_task2()
# myobj.do_task3()
# values['task4_value'] = myobj.do_task4()
# myobj.to_task5()
# Promoting and demoting results as the proxy
thingValue = (
Chain(myobj)
.something()
.do_task3()
.chain_promote_value()
.stuff()
.do_aThing()
.chain_store("thing")
.chain_previous_proxy()
.do_task4
.chain_retrieve("thing")
)
# Equivalent to
# myobj.something()
# task3Value = myobj.do_task3()
# task3Value.stuff()
# stored_thingValue = task3Value.do_aThing()
# myobj.do_task4()
# thingValue = stored_thingValue
Creates a Chain object where "obj" is the object being proxied. Options control how the chain works.
options are as follows
- strict_proxy : Default is True : Will make the chain complain if you attempt to access an attribute on the proxy that doesn't exist
When you access something on a chain, the chain will record what you have just accessed and return the chain again.
If the attribute you accessed starts with 'chain_' then the value being accessed will be from the chain. Any other value will be access from the object the chain is currently proxying.
Calling the chain will call the last value that was accessed and store it's value. Everything except some special chain commands will make sure the chain itself is returned.
Methods on the chain itself can be access by accessing 'chain_<attr>' on the Chain object where <attr> is the attribute on the internals that you want to access.
Will store the last value accessed (whether it be via attribute access or calling the chain) under a name for later retrieval. returns the chain.
Will return the value stored under the name instead of the chain.
Returns dictionary of all named stored values
Will make the last value on the object become proxied by the chain
Will remove the current proxied object and replace it with the one before it.
Will set the proxy to obj. Note that this will not record the current proxy as a previous proxy
Will give the current proxied object a name for later retrieval
Restores the proxy that was given the provided name. Will keep a record of current proxy, so that previous_proxy still works.
Will call setattr(obj, key, value) where obj is the proxied object, before returning the chain
Will make it so the next time you call the Chain, the proxy itself is called
Will call action with the last value on the chain, before returning the chain. This won't change the current value on the chain.
Returns the object being proxied.
WTFPL