[Tutor] Continue Matching after First Match
tktucker at gmail.com
Mon May 21 19:52:21 CEST 2007
Alan & Martin,
Thanks for the feedback and suggestions. Kodos is a great tool. I use it
regularly to debug my regex mistakes. It can also be a excellent learning
tool to those unfamiliar with regular expressions. Thanks for the
Python-LDAP link and ldif example code.
On 5/20/07, Martin Walsh <mwalsh at groktech.org> wrote:
> Hi Tom,
> Tom Tucker wrote:
> > Why the cStringIO stuff? The input data shown below is collected from
> > os.popen. I was trying to find an easy way of matching my regex.
> Ah, ldap...
> Matching with a string seemed easier than looping through the ouput
> > collected. Hmm. Come to think of it, I guess I could match on the
> > first "^dn" catpure that output and then keep looping until "^cn:" is
> > seen. Then repeat.
> Honestly, I'm not very good with regular expressions -- and try to avoid
> them when possible. But in cases where they seem to be the best option,
> I have formed a heavy dependence on regex debuggers like kodos.
Kodos is an excellent regex tool. I use it regularly to verify regex
> Anyways, any suggestions to fix the below code?
> Have you had a look at the python-ldap package?
Thanks. I checked the Python module index page and
You could probably access ldap directly with python, if that's an
> option. Or, you could roll your own ldif parser (but make sure your data
> contains a newline between each dn, or the parser will choke with a
> 'ValueError: Two lines starting with dn: in one record.'):
> import ldif
> from cStringIO import StringIO
> class MyLDIF(ldif.LDIFParser):
> def __init__(self, inputfile):
> ldif.LDIFParser.__init__(self, inputfile)
> self.users = 
> def handle(self, dn, entry):
> self.users.append((entry['uid'], entry['cn']))
> raw = """\
> <snip your ldif example with newlines added between dns>
> if __name__ == '__main__':
> io = StringIO(raw)
> lp = MyLDIF(io)
> for user in lp.users:
> uid = user
> cn = user
> print uid
> print cn
> ... or ...
> You could also use ldif.LDIFRecordList directly without creating a
> custom parser class which would return a list of (dn, entry) tuples. The
> module author warns that 'It can be a memory hog!', and I can imagine
> this is true if you are working with a particularly large ldap directory.
> io = StringIO(raw)
> directory = ldif.LDIFRecordList(io)
> for dn, entry in directory.all_records:
> print entry['uid']
> print entry['cn']
> Tutor maillist - Tutor at python.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Tutor