Besides the p.h and p.b accessors, ipy_leo contains at least one other
nifty feature, the PosList class.

I have shamelessly stolen the essential idea from that class, and
extended it.  Here is the present version of g.posList::

class posList(list):
    '''A subclass of list that defines a select method.'''
    def __init__ (self,c,aList=None):
        self.c = c
        list.__init__(self) # Init the base class
        if aList is None:
            for p in c.allNodes_iter():
                self.append(p.copy())
        else:
            for p in aList:
                self.append(p.copy())

    def dump (self,sort=False,verbose=False):
        if verbose: return g.listToString(self,sort=sort)
        else: return g.listToString([p.h for p in self],sort=sort)

    def select(self,pat,regex=False,removeClones=True):
        '''Return a new posList containing all positions
        in self that match the given pattern.'''
        c = self.c ; aList = []
        if regex:
            for p in self:
               if re.match(pat,p.h):
                   aList.append(p.copy())
        else:
            for p in self:
               if p.h.find(pat) != -1:
                   aList.append(p.copy())
        if removeClones:
            aList = self.removeClones(aList)
        return posList(c,aList)

    def removeClones(self,aList):
        seen = {} ; aList2 = []
        for p in aList:
            if p.v.t not in seen:
                seen[p.v.t] = p.v.t
                aList2.append(p)
        return aList2

As you can see, this is substantially larger than the original.  I had
my reasons:

1. Unlike the original, g.posList has a ctor.  This allows the class
to support the following convention.  If the ctor contains a list,
that list becomes the underlying contents (self) of posList.
Otherwise, the list is inited to all the nodes of c's tree.  At
present, the ctor requires a 'c' argument.  That's not essential, but
it might be error-prone to make both args optional.

2. Unlike the original, the select method does not start with all the
nodes in c's outline, but only with the nodes in 'self'.  I think this
is a cute improvement.  It allows successive calls to select to narrow
the found nodes.  See the example below.  And it should speed up
subsequent searches.

3. The select method support two new keyword arguments: regex and
removeClones.  If regex is False, a plain search is used.  If
removeClones is true, select removes cloned nodes from the list.

4. g.posList supports a dump method, which is quite handy, at least
for testing.

test.leo now contains a node with the following body text:

aList = g.posList(c) # Start with all positions
print len(aList)
aList2 = aList.select('plugin',regex=False,removeClones=True)
print aList2.dump(sort=True)
aList3 = aList2.select('http')
print aList3.dump()

This is early days for this important function.  All suggestions
welcome.

Edward
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/leo-editor?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to