armi.utils.iterables module

Module of utilities to help dealing with iterable objects in Python.

armi.utils.iterables.flatten(lst)[source]

Flattens an iterable of iterables by one level.

Examples

>>> flatten([[1,2,3,4],[5,6,7,8],[9,10]])
[1,2,3,4,5,6,7,8,9,10]
armi.utils.iterables.chunk(lst, n)[source]

Returns a generator object that yields lenght-n chunks of lst.

The last chunk may have a length less than n if n doesn’t divide len(lst).

Examples

>>> list(chunk([1,2,3,4,5,6,7,8,9,10], 4))
 [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10]]
armi.utils.iterables.split(a, n, padWith=())[source]

Split an iterable a into n sublists.

Parameters:
  • a (iterable) – The list to be broken into chunks

  • n (int) – The number of “even” chunks to break this into. There will be this many entries in the returned list no matter what. If len(a) < n, error unless padWith has been set. If padWithNones is true, then the output will be padded with lists containing a single None.

  • padWith (object, optional) – if n > len(a), then the result will be padded to length-n by appending padWith.

Returns:

chunked

Return type:

list[len=n] of lists

Examples

>>> split([1,2,3,4,5,6,7,8,9,10],4)
 [[1, 2, 3], [4, 5, 6], [7, 8], [9, 10]]
>>> split([0,1,2], 5, padWith=None)
 [[0], [1], [2], None, None]
armi.utils.iterables.unpackBinaryStrings(binaryRow)[source]

Unpacks a row of binary strings to a list of floats.

armi.utils.iterables.packBinaryStrings(valueDict)[source]

Converts a dictionary of lists of floats into a dictionary of lists of byte arrays.

armi.utils.iterables.unpackHexStrings(hexRow)[source]

Unpacks a row of binary strings to a list of floats.

armi.utils.iterables.packHexStrings(valueDict)[source]

Converts a dictionary of lists of floats into a dictionary of lists of hex values arrays.

class armi.utils.iterables.Sequence(seq=None)[source]

Bases: object

The Sequence class partially implements a list-like interface, supporting methods like append and extend and also operations like + and +=.

It also provides some convenience methods such as drop and select to support filtering, as well as a transform function to modify the sequence. Note that these methods return a “cloned” version of the iterator to support chaining, e.g.

>>> s = Sequence(range(1000000))
>>> tuple(s.drop(lambda i: i%2 == 0).select(lambda i: i < 20).transform(lambda i: i*10))
(10, 30, 50, 70, 90, 110, 130, 150, 170, 190)

This starts with a Sequence over 1 million elements (not stored in memory), drops the even elements, selects only those whose value is less than 20, and multiplies the resulting values by 10, all while loading only one element at a time into memory. It is only when tuple is called that the operations are performed.

drop, select, and transform act in-place, so the following is equivalent to the chained expression given above: >>> s = Sequence(range(1000000)) >>> s.drop(lambda i: i%2 == 0) <Sequence at 0x…> >>> s.select(lambda i: i < 20) <Sequence at 0x…> >>> s.transform(lambda i: i*10) <Sequence at 0x…> >>> tuple(s) (10, 30, 50, 70, 90, 110, 130, 150, 170, 190)

Note: that this class is intended for use with finite sequences. Don’t attempt to use with infinite generators. For instance, the following will not work:

>>> def counter():
...    i = 0
...    while True:
...        yield i
...        i += 1
...
>>> s = Sequence(counter()).select(lambda i: i < 10)
>>> tuple(s)  # DON'T DO THIS!

Although the result should be (0,1,2,3,4,5,6,7,8,9), the select method is not smart enough to know that it’s a terminal condition and will continue to check every number generated forever. One could remedy this by using the dropwhile and/or takewhile methods in the itertools module, but this has not been done.

Constructs a new Sequence object from an iterable. This also serves as a copy constructor if seq is an instance of Sequence.

copy()[source]

Return a new iterator that is a copy of self without consuming self.

select(pred)[source]

Keep only items for which pred(item) evaluates to True.

Note: returns self so it can be chained with other filters, e.g.,

newseq = seq.select(…).drop(…).transform(…)

drop(pred)[source]

Drop items for which pred(item) evaluates to True.

Note: returns self so it can be chained with other filters, e.g.,

newseq = seq.select(…).drop(…).transform(…)

transform(func)[source]

Apply func to this sequence.

extend(seq)[source]
append(item)[source]