def xmro(iterable): ''' Extract classes in the :term:`method resolution order` of classes or objects in :term:`class`\es in :term:`iterable`. :argument iterable: :term:`iterable` :return: :term:`iterator` of :term:`class`\es >>> from blade.xfilter import xmro >>> class stooges: ... name = 'moe' ... age = 40 >>> class stoog2(stooges): ... name = 'larry' ... age = 50 >>> class stoog3(stoog2): ... name = 'curly' ... age = 60 ... class stoog4: ... name = 'beastly' ... age = 969 >>> results = list(xmro([stoog3])) >>> stooges in results True >>> stoog3 in results True >>> stoog2 in results True ''' return xmerge(xmap(getmro, iterable))
def xmembers(iterable, test, inverse=False): ''' Collect values shallowly from classes or objects in :term:`class`\es in :term:`iterable` matched by `test`. :argument iterable: :term:`iterable` :argument test: filtering :func:`callable` :keyword bool invert: collect items in :term:`iterable` that `test` is :data:`False` rather than :data:`True` for :return: :term:`iterator` of values >>> from blade.xfilter import xmembers >>> class stooges: ... name = 'moe' ... age = 40 >>> class stoog2: ... name = 'larry' ... age = 50 >>> class stoog3: ... name = 'curly' ... age = 60 ... class stoog4: ... name = 'beastly' ... age = 969 >>> list(xmembers([stoog3], lambda x: not x[0].startswith('__'))) # doctest: +SKIP [('age', 60), ('name', 'curly'), ('stoog4', stoog3.stoog4)] ''' return xfilter(xmerge(xmap(members, iterable)), test, inverse)
def xcopy(iterable): ''' Duplicate each items in `iterable`. :argument iterable: :term:`iterable` >>> from blade.xmap import xcopy >>> list(xcopy([[1, [2, 3]], [4, [5, 6]]])) [[1, [2, 3]], [4, [5, 6]]] ''' return xmap(deepcopy, iterable)
def xall(iterable, test): ''' Discover if `test` is :data:`True` for **all** items in :term:`iterable`. :argument iterable: :term:`iterable` :argument test: filtering :func:`callable` :return: :func:`bool` >>> from blade.xcmp import xall >>> xall([2, 4, 6, 8], lambda x: x % 2 == 0) True ''' return all(xmap(test, iterable))
def xkeyvalue(iterable, callable=None, keys=False, values=False): ''' Run `callable` on incoming :term:`mapping` things. :argument iterable: :term:`iterable` :argument callable: mapped :func:`callable` :keyword bool keys: collect mapping keys only :keyword bool values: collect mapping values only >>> from blade.xmap import xkeyvalue >>> # filter items >>> list(xkeyvalue( ... [dict([(1, 2), (2, 3), (3, 4)]), dict([(1, 2), (2, 3), (3, 4)])], ... lambda x, y: x * y)) [2, 6, 12, 2, 6, 12] >>> # mapping keys only >>> list(xkeyvalue( ... [dict([(1, 2), (2, 3), (3, 4)]), dict([(1, 2), (2, 3), (3, 4)])], ... keys=True ... )) [1, 2, 3, 1, 2, 3] >>> # mapping values only >>> list(xkeyvalue( ... [dict([(1, 2), (2, 3), (3, 4)]), dict([(1, 2), (2, 3), (3, 4)])], ... values=True ... )) [2, 3, 4, 2, 3, 4] ''' if keys: callable = identity if callable is None else callable return xmap(callable, xmerge(xmap(keyz, iterable))) elif values: callable = identity if callable is None else callable return xmap(callable, xmerge(xmap(valuez, iterable))) callable = dualidentity if callable is None else callable return starmap(callable, xmerge(xmap(items, iterable)))