[Tutor] Continue Matching after First Match

Tom Tucker 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...


Oh yes,

> 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.
> http://kodos.sourceforge.net/



Kodos is an excellent regex tool.  I use it regularly to verify regex
strings.

> Anyways, any suggestions to fix the below code?
> <snip>
>
> Have you had a look at the python-ldap package?
>
> http://python-ldap.sourceforge.net/



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)
>     lp.parse()
>     for user in lp.users:
>         uid = user[0][0]
>         cn = user[1][0]
>         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)
> directory.parse()
> for dn, entry in directory.all_records:
>     print entry['uid'][0]
>     print entry['cn'][0]
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070521/ac0b755e/attachment.htm 


More information about the Tutor mailing list