LDAP/LDIF Parsing

Bruno Desthuilliers bruno.42.desthuilliers at wtf.websiteburo.oops.com
Fri Feb 2 08:19:16 EST 2007


Hallvard B Furuseth a écrit :
> Bruno Desthuilliers writes:
>> class LdapObject(object):
>>    (...)
>>    def __getattr__(self, name):
>>      try:
>>        data = self._record[name]
>>      except KeyError:
>>        raise AttributeError(
>>          "object %s has no attribute %s" % (self, name)
>>         )
> 
> Note that LDAP attribute descriptions may be invalid Python
> attribute names.  E.g.
>     {...
>      'title;lang-en': ['The Boss']
>      'title;lang-no': ['Sjefen']}
> So you'd have to call getattr() explicitly to get at all the attributes
> this way.

Yeps, true. Another solution would be to add a __getitem__ method 
pointing to the same implementation, ie:

     __getitem__ = __getattr__

>>      else:
>>        # all LDAP attribs are multivalued by default,
>>        # even when the schema says they are monovalued
>>        if len(data) == 1:
>>           return data[0]
>>        else:
>>           return data[:]
> 
> IMHO, this just complicates the client code since the client needs to
> inserts checks of isinstance(return value, list) all over the place.
> Better to have a separate method which extracts just the first value of
> an attribute, if you want that.

Most of the times, in a situation such as the one described by the OP, 
one knows by advance if a given LDAP attribute will be used as 
monovalued or multivalued. Well, this is at least my own experience...




More information about the Python-list mailing list