Search or compai problem

John Machin sjmachin at lexicon.net
Fri Aug 18 18:55:45 EDT 2006


Gallagher, Tim (NE) wrote:
> I am new to python and I want to compare 2 strings, here is my code:
> [start]
>
> import active_directory
> import re
>
> lstUsers = []

Using "lst" or "l" as a variable name is bad news in any language; with
many fonts they are too easily misread as "1st" or "1" respectively.

> users = active_directory.root()
> for user in users.search ("sn='gallagher'"):

**** Insert here:
        print type(user.samAccountName), repr(user.samAccountName)
**** that may indicate where your problem *starts*
**** Then read the documentation for the active_directory module, in
particular what it says about the attributes of the objects in the
sequence returned by users.search.


>     lstUsers.append(user.samAccountName)
>
> print "----------------------------------------"
> lstUsers.sort()
>
> ## Printing out what is inside of the arrar(list)

What is "arrar(list)" ??

**** Here insert this code:
print lstUsers
**** Look at the elements in the list; do you see ..., 'None', ... or
do you see ..., None, ...

> x = 0
> while x < len(lstUsers):

*Please* consider using a "for" statement:

for item in lstUsers:
    do_something_with(item)

>     if re.compile(lstUsers[x]).match("None",0):

1. Python or not, using regular expressions to test for equality is
extreme overkill. Use
    if lstUsers[x] == "None":
2. Python or not, it is usual to do
    re.compile(constant pattern).match(variable_input)
not the other way around.
3. The second arg to match defaults to 0, so you can leave it out.


>         print "Somthing here"
>
>     x = x + 1
>
> [/end]
>
> When I do the:
>     if re.compile(lstUsers[x]).match("None",0):
>         print "Somthing here"
>
> Some of the items in lstUsers[x] are the word None.
>  I am not sure why I cant do this
>
> I want to compare lstUsers[x] to the word "None", how can I do this.

You  *have* compared lstUsers[x] to the word "None" -- with the re
sledgehammer, but you've done it. Maybe what's in there is *not* the
string "None" :-)

HTH,
John




More information about the Python-list mailing list