xpybuild.utils.flatten

Utility functions for normalizing and flattening deeply nested lists.

flatten

xpybuild.utils.flatten.flatten(input) → List[source]

Return the input flattened to an array.

input: any variable composed of lists/generators/tuples, strings, lambda functions or other objects, nested arbitrarily.

Empty strings and None items are removed.

Returns a list of strings or other objects depth 1.

>>> flatten('hi')
['hi']
>>> flatten(['hi', 'ho', 'hum'])
['hi', 'ho', 'hum']
>>> flatten(['hi', ['ho', ['hum'] ] ])
['hi', 'ho', 'hum']
>>> flatten(['hi', ('ho', ('hum') ) ])
['hi', 'ho', 'hum']
>>> flatten(3)
[3]
>>> flatten( (x + 1) for x in [1,2,3])
[2, 3, 4]
>>> flatten(lambda: '3')
['3']
>>> flatten(['hi', lambda: 'ho', 'hum'])
['hi', 'ho', 'hum']
>>> flatten(None)
[]

getStringList

xpybuild.utils.flatten.getStringList(stringOrListOfStrings) → List[str][source]

Return a list of strings, either identical to the input (if it’s already a list), or with the input wrapped in a new sequence (if it’s a string), or an empty sequence (if its None).

>>> getStringList('abc')
['abc']
>>> getStringList(['abc', 'def'])
['abc', 'def']
>>> getStringList(('abc', 'def'))
['abc', 'def']
>>> getStringList(tuple(['abc', 'def']))
['abc', 'def']
>>> getStringList([['abc', 'def']])
['abc', 'def']
>>> getStringList(None)
[]
>>> getStringList(5)
Traceback (most recent call last):
...
ValueError: The specified value must be a list of strings: "5"