0

I'm currently working on some code for one of my classes which is shown right here (changed names/addresses to hide names).

# coding=utf:8
#————————————————————————Attendance Checker Start————————————————————————#
import bluetooth
import time

#-----Function Definition Start-----#
def student_check(index):
    result = bluetooth.lookup_name(blue_address_list[index], timeout=1)

    if (result is not None):
        return True
    else:
        return False
#-----Function Definition End-----#


#————————Defined Dictionary Start————————#
blue_student_list = ['Name1', 'Name2', 'Name3', 'Name4',
                  'Name5', 'Name6', 'Name7', 'Name8',
                  'Name9']
blue_address_list = ['Address1', 'Address2', 'Address3', 'Address4', 'Address5', 'Address6', 'Address7', 'Address8', 'Address9']
#—————————Defined Dictionary End—————————#

#———————————————Print Method Start———————————————#

print ' '
time.sleep(1)
print 'Checking who is here on ' + time.strftime('%b %d, %Y', time.gmtime())
print ' '
time.sleep(1)

for i in range(0, len(blue_address_list)):
    if (student_check(i)):
        print blue_student_list[i] + ': Present '
    else:
        print blue_student_list[i] + ': Absent '

print 'Script Completed'

#————————————————Print Method End————————————————#

#—————————————————————————Attendance Checker End—————————————————————————#

My issue is when the script starts, I get this output.

Checking who is here on Feb 24, 2016

Name1: Present
Name2: Absent
Name3: Absent
Name4: Absent
Name5: Absent
Name6: Absent
Name7: Absent
Name8: Absent
Name9: Absent

Script Completed

My issue with this is not them being absent. I have another device paired under Name7 that says absent no matter what. I believe it actually checks on the first one and fails the rest once one goes absent. The reason I think this is because they all go absent at the exact same time, it doesn't have any delay unlike the first which has a delay when checking for the nearby device.

Dylan
  • 823
  • 2
  • 9
  • 33
  • Don't use `!= None`; use `is not None`. – zondo Feb 25 '16 at 15:06
  • Changed the code to that, still no luck. @zondo – Dylan Feb 25 '16 at 15:07
  • 1
    `blue_address_list.__sizeof__()` returns the size of the list in memory, in bytes. Perhaps you want `range(0, len(blue_address_list))` or a dictionary that maps addresses to names, so you can pass the address directly into `student_check` and then use the address to look up the name when displaying the results. – Simon Fraser Feb 25 '16 at 15:14
  • That is what I actually am using, I just accidentally forgot to change this when I pasted it in. This was a copy of my older code. @SimonFraser – Dylan Feb 25 '16 at 15:17
  • It may be an error in pybluez with devices that don't exist. You could try using `discover_devices` to see what's nearby, and then iterate over those to see if any of them are addresses you care about. That way you know that you would only use `lookup_name` on devices that can respond. – Simon Fraser Feb 25 '16 at 15:25
  • I actually figured it out in the comments in the answer below. @SimonFraser – Dylan Feb 25 '16 at 15:27
  • I saw, just not until after I'd posted that comment :) – Simon Fraser Feb 25 '16 at 15:29

1 Answers1

1

__sizeof__ returns the internal size in bytes for the given object, not the number of items. I think you should use len(blue_address_list) instead of it.

Edit: increasing timeout will solve problem, seems bluetooth.lookup_name can not respond on given time.

FatmaT
  • 255
  • 1
  • 9
  • I actually pasted in my older code that didn't have that fixed. Updated Q now. @FatmaT – Dylan Feb 25 '16 at 15:19
  • did you try to increase timeout that might be reason. – FatmaT Feb 25 '16 at 15:22
  • That was the issue, I increased the timeout at "result = bluetooth.lookup_name(blue_address_list[index], timeout=1)" and set the timeout to 5 and now it's fixed. Thanks! Edit your answer and I'll mark it solved. @FatmaT – Dylan Feb 25 '16 at 15:25
  • you're welcome. Friendly reminder "result !=None" is not pythonic, "result is not None" is much better ;) – FatmaT Feb 25 '16 at 15:49
  • I also forgot to edit that out when pasting, whoops. I'll fix that real quick. @FatmaT – Dylan Feb 25 '16 at 15:50