Package lepl :: Package support :: Module list
[hide private]
[frames] | no frames]

Module list

source code

Support for S-expression ASTs using subclasses of Python's list class.

The general support works with any nested iterables (except strings).

Classes [hide private]
  List
Extend a list for use in ASTs.
Functions [hide private]
 
clone_iterable(type_, items)
Clone a class that wraps data in an AST.
source code
 
sexpr_fold(per_list=None, per_item=None, exclude=lambda x: isinstance(x,basestring))
We need some kind of fold-like procedure for generalising operations on arbitrarily nested iterables.
source code
 
sexpr_to_tree(list_)
Generate a tree using the same "trick" as GraphStr.
source code
 
sexpr_throw(node)
Raise an error, if one exists in the results (AST trees are traversed).
source code
Variables [hide private]
  clone_sexpr = sexpr_fold()
Clone a set of listed iterables.
  count_sexpr = sexpr_fold(per_list= lambda type_, items: sum(it...
Count the number of value nodes in an AST.
  join = lambda items:
Flatten a list of lists by one level, so [[1],[2, [3]]] becomes [1,2,[3]].
  sexpr_flatten = sexpr_fold(per_list= lambda type_, items: join...
Flatten a list completely, so [[1],[2, [3]]] becomes [1,2,3]
  _fmt = {}
  sexpr_to_str = sexpr_fold(per_list= lambda type_, items: fmt(_...
A flat representation of nested lists (a set of constructors).
Function Details [hide private]

sexpr_fold(per_list=None, per_item=None, exclude=lambda x: isinstance(x,basestring))

source code 

We need some kind of fold-like procedure for generalising operations on arbitrarily nested iterables. We can't use a normal fold because Python doesn't have the equivalent of cons, etc; this tries to be more Pythonic (see comments later).

We divide everything into iterables ("lists") and atomic values ("items"). per_list is called with a generator over the (transformed) top-most list, in order. Items (ie atomic values) in that list, when requested from the generator, will be processed by per_item; iterables will be processed by a separate call to per_list (ie recursively).

So this is more like a recursive map than a fold, but with Python's mutable state and lack of typing it appears to be equally powerful. Note that per_list is passed the previous type, which can be used for dispatching operations.

sexpr_to_tree(list_)

source code 

Generate a tree using the same "trick" as GraphStr.

The initial fold returns a function (str, str) -> list(str) at each level.

sexpr_throw(node)

source code 
Raise an error, if one exists in the results (AST trees are traversed). Otherwise, the results are returned (invoke with >>).

Variables Details [hide private]

count_sexpr

Count the number of value nodes in an AST.

(Note that size(List) gives the number of entries in that list, counting each sublist as "1", while this descends embedded lists, counting their non-iterable contents.

Value:
sexpr_fold(per_list= lambda type_, items: sum(items), per_item= lambda\
 item: 1)

join

Flatten a list of lists by one level, so [[1],[2, [3]]] becomes [1,2,[3]].

Note: this will only work correctly if all entries are lists.

Value:
lambda items:

sexpr_flatten

Flatten a list completely, so [[1],[2, [3]]] becomes [1,2,3]
Value:
sexpr_fold(per_list= lambda type_, items: join(items), per_item= lambd\
a item: [item])

sexpr_to_str

A flat representation of nested lists (a set of constructors).
Value:
sexpr_fold(per_list= lambda type_, items: fmt(_fmt.get(type_, '{0}([{1\
}])'), type_.__name__, ','.join(items)), per_item= lambda item: repr(i\
tem))