Skip to content

delfick/python-chain

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

python-chain

Class for making any object have a chainable API.

Why?

Because chained APIs are good. And returning self from every function is bad.

example

# 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

Chain Class

Chain(obj, **options)

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

Chain().<attribute>

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.

Chain().__call__

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.

Chain Internals

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.

store(name)

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.

retrieve(name)

Will return the value stored under the name instead of the chain.

get_stored()

Returns dictionary of all named stored values

promote_value()

Will make the last value on the object become proxied by the chain

previous_proxy()

Will remove the current proxied object and replace it with the one before it.

replace_proxy(obj)

Will set the proxy to obj. Note that this will not record the current proxy as a previous proxy

name_proxy(name)

Will give the current proxied object a name for later retrieval

restore_proxy(name)

Restores the proxy that was given the provided name. Will keep a record of current proxy, so that previous_proxy still works.

setattr(key, value)

Will call setattr(obj, key, value) where obj is the proxied object, before returning the chain

call_proxy()

Will make it so the next time you call the Chain, the proxy itself is called

tap(action)

Will call action with the last value on the chain, before returning the chain. This won't change the current value on the chain.

exit()

Returns the object being proxied.

license

WTFPL

About

Class for making any object have a chainable api

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published