b3j0f.utils.chaining module¶
This module aims to provide tools to chaining of calls.
It is inspired from method chaining pattern in embedding objects to chain methods calls in a dedicated Chaining object. Such method calls return the Chaining object itself, allowing multiple calls to object methods to be invoked in a concise statement.
-
class
b3j0f.utils.chaining.
Chaining
(content)[source]¶ Bases:
object
Class which permits to process chaining of routines/attr such as call chaining in javascript.
In order to chain calls, start to embed an object, then call embedded object methods which returns all the Chaining object. Finally, if you want to get back method results, use Chaining.__getitem__ method where the index corresponds to the order of calls.
Example: >>> chaining = Chaining('example').upper().capitalize() >>> chaining[0] 'EXAMPLE' >>> chaining[1] 'Example' >>> chaining[-1] 'example.' >>> chaining[:] ['EXAMPLE', 'Example', 'example.'] >>> chaining._ 'example' >>> chaining.__iadd__('.').__iadd__('!') >>> chaining._ 'example.!' >>> chaining[:] ['EXAMPLE', 'Example', 'example.', None, None]
-
CONTENT
= u'_'¶ content attribute name
-
RESULTS
= u'___'¶ chained method results attribute name
-
-
class
b3j0f.utils.chaining.
ListChaining
(*content)[source]¶ Bases:
b3j0f.utils.chaining.Chaining
Apply chaining on a list of objects.
- According to content length, chaining results are saved in a list where
- values are call result or exception if an exeception occured.
Example: >>> chaining = ListChaining('example', 'test').upper().capitalize() >>> chaining[0] ['EXAMPLE', 'TEST'] >>> chaining[1] ['Example', 'Test'] >>> chaining[-1] ['Example', 'Test'] >>> chaining._ ['example', 'test'] >>> chaining += '.' >>> chaining._ ['example.', 'test.'] >>> chaining[2] [None, None] >>> chaining[:] [['EXAMPLE', 'TEST'], ['Example', 'Test'], [None, None]]