Help - Classes and attributes

R

rh0dium

Hi all,

I believe I am having a fundamental problem with my class and I can't
seem to figure out what I am doing wrong. Basically I want a class
which can do several specific ldap queries. So in my code I would have
multiple searches. But I can't figure out how to do it without it
barfing..

The error is straightforward ..

LDAP Version 2.0.8
Traceback (most recent call last):
File "./ldap-nsc.py", line 62, in ?
l.search()
File "./ldap-nsc.py", line 40, in search
ldap_result_id = l.search_s(baseDN, searchScope, searchAttrs,
retrieveAttrs)
AttributeError: NSCLdap instance has no attribute 'search_s'


The code is also I believe straight forward..

import ldap

class NSCLdap:

def __init__(self,server="sc-ldap.nsc.com"):
who=""; cred=""
self.server=server
try:
print "LDAP Version", ldap.__version__
l=ldap.open(server)
l.simple_bind_s(who, cred)
l.protocol_version=ldap.VERSION3
except ldap.LDAPError, error_message:
print "Couldn't Connect to %s %s " %
(server,error_message)

def search(self, baseDN="o=nsc.com",
retrieveAttrs=None,searchAttrs="cn=*klass*" ):
searchScope = ldap.SCOPE_SUBTREE
try:
ldap_result_id = l.search_s(baseDN, searchScope,
searchAttrs, retrieveAttrs)
result_set = []
while 1:
result_type, result_data = l.result(ldap_result_id, 0)
if (result_data == []):
break
else:
## here you don't have to append to a list
## you could do whatever you want with the
individual entry
## The appending to list is just for
illustration.
if result_type == ldap.RES_SEARCH_ENTRY:
result_set.append(result_data)
print result_set
except ldap.LDAPError, error_message:
print "Errors on Search %s " % error_message

def setBaseDN(self, baseDN="o=nsc.com"):
return baseDN

if __name__ == '__main__':

l = NSCLdap()
l.search()


I would love some pointers - clearly my code thinks that search_s is an
attribute of my class but it's not..

TIA
 
J

John Machin

rh0dium said:
Hi all,

I believe I am having a fundamental problem with my class and I can't
seem to figure out what I am doing wrong. Basically I want a class
which can do several specific ldap queries. So in my code I would have
multiple searches. But I can't figure out how to do it without it
barfing..

The error is straightforward ..

LDAP Version 2.0.8
Traceback (most recent call last):
File "./ldap-nsc.py", line 62, in ?
l.search()
File "./ldap-nsc.py", line 40, in search
ldap_result_id = l.search_s(baseDN, searchScope, searchAttrs,
retrieveAttrs)
AttributeError: NSCLdap instance has no attribute 'search_s'


The code is also I believe straight forward..

import ldap

class NSCLdap:

def __init__(self,server="sc-ldap.nsc.com"):
who=""; cred=""
self.server=server
try:
print "LDAP Version", ldap.__version__
l=ldap.open(server)
l.simple_bind_s(who, cred)
l.protocol_version=ldap.VERSION3
except ldap.LDAPError, error_message:
print "Couldn't Connect to %s %s " %
(server,error_message)

def search(self, baseDN="o=nsc.com",
retrieveAttrs=None,searchAttrs="cn=*klass*" ):
searchScope = ldap.SCOPE_SUBTREE
try:

If you had bothered to do some elementary debugging, like "print
repr(l)" here, just before the exception-triggering statement, ....
ldap_result_id = l.search_s(baseDN, searchScope,
searchAttrs, retrieveAttrs)
result_set = []
while 1:
result_type, result_data = l.result(ldap_result_id, 0)
if (result_data == []):
break
else:
## here you don't have to append to a list
## you could do whatever you want with the
individual entry
## The appending to list is just for
illustration.
if result_type == ldap.RES_SEARCH_ENTRY:
result_set.append(result_data)
print result_set
except ldap.LDAPError, error_message:
print "Errors on Search %s " % error_message

def setBaseDN(self, baseDN="o=nsc.com"):
return baseDN

if __name__ == '__main__':

l = NSCLdap()
l.search()


I would love some pointers - clearly my code thinks that search_s is an
attribute of my class but it's not..


You are confusing the bejaysus out of yourself and your audience by
using "l" as a name (1) at all (2) to represent two *different* things,
one in script-global scope -- l = NSCLdap() -- and one in the __init__
method of your class -- l=ldap.open(server).

Use two different sensible names; then your real problem should become
apparent -- unless of course in the meantime some wally thinks it a good
idea to prevent your attaining a clue yourself by spoon-feeding you.
 
C

Christopher Subich

rh0dium said:
Hi all,

I believe I am having a fundamental problem with my class and I can't
seem to figure out what I am doing wrong. Basically I want a class
which can do several specific ldap queries. So in my code I would have
multiple searches. But I can't figure out how to do it without it
barfing.. [snip]
File "./ldap-nsc.py", line 40, in search
ldap_result_id = l.search_s(baseDN, searchScope, searchAttrs,
retrieveAttrs)
AttributeError: NSCLdap instance has no attribute 'search_s'


The code is also I believe straight forward..

You're going to kick yourself when you see the mistake.
import ldap

class NSCLdap:

def __init__(self,server="sc-ldap.nsc.com"):
who=""; cred=""
self.server=server
try:
print "LDAP Version", ldap.__version__
l=ldap.open(server)
^^^^^^^^^^^^^^^^^^
[big snip]
if __name__ == '__main__':

l = NSCLdap()
l.search()
I would love some pointers - clearly my code thinks that search_s is an
attribute of my class but it's not..

Ah, but l -is- an instance of your class. You want l to refer to the
ldap connection, but you forgot do assign it to self.l -- in __init__,
you assign l to simply a local variable, which goes poof as soon as
__init__ returns. You forgot the self.l throughout both __init__ and
search.

You get the slighty misleading traceback because there is an "l" defined
-- it just happens to be the one in globals(), the l = NSCLdap() that
got assigned when you imported/ran the module.

Replace l = NSCLdap() with q = NSCLdap() (and l.search with q.search),
and you'll get a NameError instead.
 
B

Bruno Desthuilliers

rh0dium a écrit :
Hi all,

I believe I am having a fundamental problem with my class and I can't
seem to figure out what I am doing wrong. Basically I want a class
which can do several specific ldap queries. So in my code I would have
multiple searches. But I can't figure out how to do it without it
barfing..

The error is straightforward ..

LDAP Version 2.0.8
Traceback (most recent call last):
File "./ldap-nsc.py", line 62, in ?
l.search()
File "./ldap-nsc.py", line 40, in search
ldap_result_id = l.search_s(baseDN, searchScope, searchAttrs,
retrieveAttrs)
AttributeError: NSCLdap instance has no attribute 'search_s'


The code is also I believe straight forward..

import ldap

class NSCLdap:

def __init__(self,server="sc-ldap.nsc.com"):
who=""; cred=""
self.server=server
try:
print "LDAP Version", ldap.__version__
l=ldap.open(server)
l.simple_bind_s(who, cred)
l.protocol_version=ldap.VERSION3
except ldap.LDAPError, error_message:
print "Couldn't Connect to %s %s " %
(server,error_message)

And then you throw away the ldap connection...

def search(self, baseDN="o=nsc.com",
retrieveAttrs=None,searchAttrs="cn=*klass*" ):
searchScope = ldap.SCOPE_SUBTREE
try:
ldap_result_id = l.search_s(baseDN, searchScope,
searchAttrs, retrieveAttrs)

Now where is this 'l' coming from ?
result_set = []
while 1:
result_type, result_data = l.result(ldap_result_id, 0)
if (result_data == []):
break
else:
## here you don't have to append to a list
## you could do whatever you want with the
individual entry
## The appending to list is just for
illustration.
if result_type == ldap.RES_SEARCH_ENTRY:
result_set.append(result_data)
print result_set
except ldap.LDAPError, error_message:
print "Errors on Search %s " % error_message

def setBaseDN(self, baseDN="o=nsc.com"):
return baseDN

Err... this code is not 'setting' anything.
if __name__ == '__main__':

l = NSCLdap()
l.search()


I would love some pointers - clearly my code thinks that search_s is an
attribute of my class but it's not..

try with this instead :
q = NSCLdap()
q.search()


May I suggest a somewhat corrected version ?

class NSCLdap(object):
def __init__(self,
server="sc-ldap.nsc.com",
baseDN="o=nsc.com",
who=None,
cred=None):
self.server = server
self.baseDN = baseDN
if who is None:
self.who = ""
else:
self.who = who
if cred is None:
self.cred = ""
else:
self.cred = cred
self.connection = None

def connect(self):
try:
print "LDAP Version", ldap.__version__
self.connection = ldap.open(server)
self.connection.simple_bind_s(self.who, self.cred)
self.connection.protocol_version=ldap.VERSION3

except ldap.LDAPError, error_message:
# I would not catch this. It's the caller's
# responsabilitie to handle this IMHO
print >> sys.stderr, "Couldn't Connect to %s %s " %
(server,error_message)

def search(self,
baseDN=None,
searchScope=ldap.SCOPE_SUBTREE,
retrieveAttrs=None,
searchAttrs="cn=*klass*" ):

cnx = self.connection
if baseDN is None:
baseDN = self.baseDN

try:
ldap_result_id = cnx.search_s(baseDN,
searchScope,
searchAttrs,
retrieveAttrs)
result_set = []
while True:
result_type, result_data =cnx.result(ldap_result_id, 0)
#if (result_data == []):
if not result_data:
break
## here you don't have to append to a list
## you could do whatever you want with the
## individual entry
## The appending to list is just for
## illustration.
if result_type == ldap.RES_SEARCH_ENTRY:
result_set.append(result_data)
print result_set
except ldap.LDAPError, error_message:
print >> sys.stderr, "Errors on Search %s " % error_message

if __name__ == '__main__':
truc = NSCLdap()
truc.search()
 
R

rh0dium

I knew it had to be something obvious - thanks so much!!


John said:
rh0dium said:
Hi all,

I believe I am having a fundamental problem with my class and I can't
seem to figure out what I am doing wrong. Basically I want a class
which can do several specific ldap queries. So in my code I would have
multiple searches. But I can't figure out how to do it without it
barfing..

The error is straightforward ..

LDAP Version 2.0.8
Traceback (most recent call last):
File "./ldap-nsc.py", line 62, in ?
l.search()
File "./ldap-nsc.py", line 40, in search
ldap_result_id = l.search_s(baseDN, searchScope, searchAttrs,
retrieveAttrs)
AttributeError: NSCLdap instance has no attribute 'search_s'


The code is also I believe straight forward..

import ldap

class NSCLdap:

def __init__(self,server="sc-ldap.nsc.com"):
who=""; cred=""
self.server=server
try:
print "LDAP Version", ldap.__version__
l=ldap.open(server)
l.simple_bind_s(who, cred)
l.protocol_version=ldap.VERSION3
except ldap.LDAPError, error_message:
print "Couldn't Connect to %s %s " %
(server,error_message)

def search(self, baseDN="o=nsc.com",
retrieveAttrs=None,searchAttrs="cn=*klass*" ):
searchScope = ldap.SCOPE_SUBTREE
try:

If you had bothered to do some elementary debugging, like "print
repr(l)" here, just before the exception-triggering statement, ....
ldap_result_id = l.search_s(baseDN, searchScope,
searchAttrs, retrieveAttrs)
result_set = []
while 1:
result_type, result_data = l.result(ldap_result_id, 0)
if (result_data == []):
break
else:
## here you don't have to append to a list
## you could do whatever you want with the
individual entry
## The appending to list is just for
illustration.
if result_type == ldap.RES_SEARCH_ENTRY:
result_set.append(result_data)
print result_set
except ldap.LDAPError, error_message:
print "Errors on Search %s " % error_message

def setBaseDN(self, baseDN="o=nsc.com"):
return baseDN

if __name__ == '__main__':

l = NSCLdap()
l.search()


I would love some pointers - clearly my code thinks that search_s is an
attribute of my class but it's not..


You are confusing the bejaysus out of yourself and your audience by
using "l" as a name (1) at all (2) to represent two *different* things,
one in script-global scope -- l = NSCLdap() -- and one in the __init__
method of your class -- l=ldap.open(server).

Use two different sensible names; then your real problem should become
apparent -- unless of course in the meantime some wally thinks it a good
idea to prevent your attaining a clue yourself by spoon-feeding you.
 
R

rh0dium

Thanks Bruno!!

Very much appreciated the modifications!!


Bruno said:
rh0dium a écrit :
Hi all,

I believe I am having a fundamental problem with my class and I can't
seem to figure out what I am doing wrong. Basically I want a class
which can do several specific ldap queries. So in my code I would have
multiple searches. But I can't figure out how to do it without it
barfing..

The error is straightforward ..

LDAP Version 2.0.8
Traceback (most recent call last):
File "./ldap-nsc.py", line 62, in ?
l.search()
File "./ldap-nsc.py", line 40, in search
ldap_result_id = l.search_s(baseDN, searchScope, searchAttrs,
retrieveAttrs)
AttributeError: NSCLdap instance has no attribute 'search_s'


The code is also I believe straight forward..

import ldap

class NSCLdap:

def __init__(self,server="sc-ldap.nsc.com"):
who=""; cred=""
self.server=server
try:
print "LDAP Version", ldap.__version__
l=ldap.open(server)
l.simple_bind_s(who, cred)
l.protocol_version=ldap.VERSION3
except ldap.LDAPError, error_message:
print "Couldn't Connect to %s %s " %
(server,error_message)

And then you throw away the ldap connection...

def search(self, baseDN="o=nsc.com",
retrieveAttrs=None,searchAttrs="cn=*klass*" ):
searchScope = ldap.SCOPE_SUBTREE
try:
ldap_result_id = l.search_s(baseDN, searchScope,
searchAttrs, retrieveAttrs)

Now where is this 'l' coming from ?
result_set = []
while 1:
result_type, result_data = l.result(ldap_result_id, 0)
if (result_data == []):
break
else:
## here you don't have to append to a list
## you could do whatever you want with the
individual entry
## The appending to list is just for
illustration.
if result_type == ldap.RES_SEARCH_ENTRY:
result_set.append(result_data)
print result_set
except ldap.LDAPError, error_message:
print "Errors on Search %s " % error_message

def setBaseDN(self, baseDN="o=nsc.com"):
return baseDN

Err... this code is not 'setting' anything.
if __name__ == '__main__':

l = NSCLdap()
l.search()


I would love some pointers - clearly my code thinks that search_s is an
attribute of my class but it's not..

try with this instead :
q = NSCLdap()
q.search()


May I suggest a somewhat corrected version ?

class NSCLdap(object):
def __init__(self,
server="sc-ldap.nsc.com",
baseDN="o=nsc.com",
who=None,
cred=None):
self.server = server
self.baseDN = baseDN
if who is None:
self.who = ""
else:
self.who = who
if cred is None:
self.cred = ""
else:
self.cred = cred
self.connection = None

def connect(self):
try:
print "LDAP Version", ldap.__version__
self.connection = ldap.open(server)
self.connection.simple_bind_s(self.who, self.cred)
self.connection.protocol_version=ldap.VERSION3

except ldap.LDAPError, error_message:
# I would not catch this. It's the caller's
# responsabilitie to handle this IMHO
print >> sys.stderr, "Couldn't Connect to %s %s " %
(server,error_message)

def search(self,
baseDN=None,
searchScope=ldap.SCOPE_SUBTREE,
retrieveAttrs=None,
searchAttrs="cn=*klass*" ):

cnx = self.connection
if baseDN is None:
baseDN = self.baseDN

try:
ldap_result_id = cnx.search_s(baseDN,
searchScope,
searchAttrs,
retrieveAttrs)
result_set = []
while True:
result_type, result_data =cnx.result(ldap_result_id, 0)
#if (result_data == []):
if not result_data:
break
## here you don't have to append to a list
## you could do whatever you want with the
## individual entry
## The appending to list is just for
## illustration.
if result_type == ldap.RES_SEARCH_ENTRY:
result_set.append(result_data)
print result_set
except ldap.LDAPError, error_message:
print >> sys.stderr, "Errors on Search %s " % error_message

if __name__ == '__main__':
truc = NSCLdap()
truc.search()
 
R

rh0dium

Hi

I really like your approach but when do you actually get connected??
You never call the method connect?
 
B

Bruno Desthuilliers

rh0dium a écrit :
Hi

I really like your approach but when do you actually get connected??
You never call the method connect?

oops :(

(snip whole code)
BTW, you'd better let exceptions propagate from connect(), and catch'em
in the calling code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top