Package: iterator-chain
iterator-chainChain together lazily computed modifications to iterators.One normally needs to do the followinglist(map(lambdaelement:element/3,filter(lambdaelement:element>32,map(lambdaelement:element*2,[5,78,12,26]))))Instead do thisiterator_chain.from_iterable([5,78,12,26])\.map(lambdaelement:element*2)\.filter(lambdaelement:element>32)\.map(lambdaelement:element/3).list()It allows the developer to read the code in a more natural fashion: from left to right and from top to bottom.  The
developer no longer needs to "unwrap" the functions to understand the logic.InstallIncludeiterator-chainin yourrequirements.txtfile and/or usepipto install it.$pipinstalliterator-chainAPIStart by importing the package.importiterator_chainStart the chainTo start the chain, use thefrom_iterableorfrom_iterable_parallelfunction.  They take an iterable.an_iterable=[5,78,12,26]chain=iterator_chain.from_iterable(an_iterable)parallel_chain=iterator_chain.from_iterable_parallel(an_iterable)FunctionArgumentsDescriptionfrom_iterable•iterable- An iterable to be used in the iterator chainStarts the iterator chain with the supplied iterable.  Chaining and terminating methods can now be called on the result.from_iterable_parallel•iterable- An iterable to be used in the iterator chain•chunksize- Keyword.  How big of chunks to split the iterator up across the parallel execution units.  If unspecified or None, the chunk size will start at 1 and send that many elements to each execution unit.  The chunk size will then increment in powers of two and send that many items to each execution unit.  This is repeated until the iterator is exhausted.  This value is used as the default chunksize for all the following parallel based methods.  A specific parallel based method's chunksize can be overrided by supplying thechunksizekeyword to that method.Starts the iterator chain with the supplied iterable.  Chaining and terminating methods can now be called on the result.  Certain chaining and terminating methods will occur in parallel.  Parallel means separate processes to get around Python's GIL.Continuing the chainFrom there, one can call a plethora of additional methods to modify the iterable passed in originally.  The methods are
outlined below.  The methods fall into one of two categories: chaining or terminating.Chaining methods apply some modification to the elements in the iterator, but keeps the chain alive.
This allows additional chaining methods to be subsequently called on the result.
Because modifications are lazily computed, none of the modifications from chaining methods are applied untilaftera terminating method is
called.Terminating methods also apply some modification, requests some information, or executes something on the elements in the iterator.  They stop the chaining by returning
an actual value.  This value will depend on all the previous chaining methods being executed first.Chaining methodsMethodArgumentsDescriptionmap•function- A function that takes a single argumentWill run thefunctionacross all the elements in the iterator.filter•function- A function that takes a single argumentWill run thefunctionon every element.functionshould return a truthy or falsy value.  On true, the element will stay; on false, the element will be removed.skip•number- An integerThenumbernumber of elements will be skipped over and effectively removed.distinctAny duplicates will be removed.limit•max_size- An integerThe iterator will stop aftermax_sizeelements.  Any elements afterward are effectively removed.flattenAny element that is an iterable itself will have its elements iterated over first before continuing with the remaining elements.  Strings (str) do not count as an iterable for this method.  Dictionaries flatten to its item tuples.sort•key- Keyword.  A function of one argument that is used to extract a comparison key from each element•cmp- Keyword.  A Python 2.x "cmp" function that takes two arguments•reverse- Keyword.  If set toTrue, the elements will be sorted in the reverse orderSorts the iterator based on the elements' values.  Usekeyorcmpto make a custom comparison.  Ifkeyis specified,cmpcannot be used.  This method is expensive because it must serialize all the values into a sequence.reverseReverses the iterator.  The last item will be first, and the first item will be last.  This method is expensive because it must serialize all the values into a list.Parallel VersionsMethodArgumentsDescriptionmap•function- A function that takes a single argument•chunksize- Keyword.  Overrides the chunksize supplied to the originalfrom_iterable_parallelWill run thefunctionacross all the elements in the iterator in parallel.filter•function- A function that takes a single argument•chunksize- Keyword.  Overrides the chunksize supplied to the originalfrom_iterable_parallelWill run thefunctionon every element in parallel.functionshould return a truthy or falsy value.  On true, the element will stay; on false, the element will be removed.Terminating methodsMethodArgumentsDescriptionlistSerializes the iterator chain into alistand returns it.countReturns the number of elements in the iterator.first•default- Keyword.  Any value.Returns just the first item in the iterator.  If the iterator is empty, thedefaultis returned.last•default- Keyword.  Any value.Returns just the last item in the iterator.  If the iterator is empty, thedefaultis returned.max•default- Keyword.  Any value.Returns the largest valued element in the iterator.  If the iterator is empty, thedefaultis returned.min•default- Keyword.  Any value.Returns the smallest valued element in the iterator.  If the iterator is empty, thedefaultis returned.sum•default- Keyword.  Any value.Sums all the elements in the iterator together.  If any of the elements are un-summable, thedefaultis returned.reduce•function- A function that takes two arguments•initial- Keyword.  Any value.Applies the function to two elements in the iterator cumulatively.  Subsequent calls tofunctionuses the previous return value fromfunctionas the first argument and the next element in the iterator as the second argument.  The final value is returned.  Ifinitialis present, it is placed before the items of the sequence in the calculation, and serves as a default when the sequence is empty.for_each•function- A function that takes one argument and returns nothingExecutesfunctionon every element in the iterator.  There is no return value.  If you are wanting to return a list of values based on the function, use.map(_function_).list().all_match•function- A function that takes one argument and returns a booleanReturnsTrueonly ifallthe elements returnTrueafter applying thefunctionto them.  Else returnsFalse.any_match•function- A function that takes one argument and returns a booleanReturnsTrueif just one element returnTrueafter applying thefunctionto it.  If all elements result inFalse,Falseis returned.none_match•function- A function that takes one argument and returns a booleanReturnsTrueonly ifallthe elements returnFalseafter applying thefunctionto them.  Else returnsTrue.Parallel VersionsMethodArgumentsDescriptionfor_each•function- A function that takes one argument and returns nothing•chunksize- Keyword.  Overrides the chunksize supplied to the originalfrom_iterable_parallelExecutesfunctionon every element in the iterator in parallel.  There is no return value.  If you are wanting to return a list of values based on the function, use.map(function).list().Examplesimportiterator_chainan_iterable=[5,78,12,26]iterator_chain.from_iterable(an_iterable)\#starts the chain.map(lambdaelement:element*2)\#multiplies every element by two: [10, 156, 24, 52].filter(lambdaelement:element>32)\#keeps any element greater than 32: [156, 52].map(lambdaelement:element/3)\#divides every element by three: [52.0, 17.333333333333332].list()#and finally returns a list of the result for later use in your application: [52.0, 17.333333333333332]