<br>Alan & Martin,<br><br>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.
<br><br><div><span class="gmail_quote">On 5/20/07, <b class="gmail_sendername">Martin Walsh</b> <<a href="mailto:mwalsh@groktech.org">mwalsh@groktech.org</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi Tom,<br><br>Tom Tucker wrote:<br> > Why the cStringIO stuff? The input data shown below is collected from<br>> os.popen. I was trying to find an easy way of matching my regex.<br><br>Ah, ldap...</blockquote><div>
<br>Oh yes, <br></div><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">> Matching with a string seemed easier than looping through the ouput
<br>> collected. Hmm. Come to think of it, I guess I could match on the<br>> first "^dn" catpure that output and then keep looping until "^cn:" is<br>> seen. Then repeat.<br><br>Honestly, I'm not very good with regular expressions -- and try to avoid
<br>them when possible. But in cases where they seem to be the best option,<br>I have formed a heavy dependence on regex debuggers like kodos.<br><a href="http://kodos.sourceforge.net/">http://kodos.sourceforge.net/</a></blockquote>
<div><br><br>Kodos is an excellent regex tool. I use it regularly to verify regex strings.<br></div><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
> Anyways, any suggestions to fix the below code?<br><snip><br><br>Have you had a look at the python-ldap package?<br><br><a href="http://python-ldap.sourceforge.net/">http://python-ldap.sourceforge.net/</a></blockquote>
<div><br><br>Thanks. I checked the Python module index page and <br></div><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">You could probably access ldap directly with python, if that's an
<br>option. Or, you could roll your own ldif parser (but make sure your data<br>contains a newline between each dn, or the parser will choke with a<br>'ValueError: Two lines starting with dn: in one record.'):<br>
<br>import ldif<br>from cStringIO import StringIO<br><br>class MyLDIF(ldif.LDIFParser):<br> def __init__(self, inputfile):<br> ldif.LDIFParser.__init__(self, inputfile)<br> self.users = []<br><br> def handle(self, dn, entry):
<br> self.users.append((entry['uid'], entry['cn']))<br><br>raw = """\<br><snip your ldif example with newlines added between dns><br>"""<br><br>if __name__ == '__main__':
<br> io = StringIO(raw)<br> lp = MyLDIF(io)<br> lp.parse()<br> for user in lp.users:<br> uid = user[0][0]<br> cn = user[1][0]<br> print uid<br> print cn<br><br>... or ...<br><br>You could also use
ldif.LDIFRecordList directly without creating a<br>custom parser class which would return a list of (dn, entry) tuples. The<br>module author warns that 'It can be a memory hog!', and I can imagine<br>this is true if you are working with a particularly large ldap directory.
<br><br>io = StringIO(raw)<br>directory = ldif.LDIFRecordList(io)<br>directory.parse()<br>for dn, entry in directory.all_records:<br> print entry['uid'][0]<br> print entry['cn'][0]<br><br>_______________________________________________
<br>Tutor maillist - <a href="mailto:Tutor@python.org">Tutor@python.org</a><br><a href="http://mail.python.org/mailman/listinfo/tutor">http://mail.python.org/mailman/listinfo/tutor</a><br></blockquote></div><br>