osdir.com

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

To check for empty string after a portion of the string in python 3.6


Dear Mr. Cameron

Thanks a lot for your quick responses, Could you please let me know
when the device is not enabled I get the error " I get the below error
saying "IndexError: list index out of range""

Code snippet:
cmd = "sudo hcitool scan"
res = self._helper.execute_cmd_output_string(cmd)
print("The value of res!!!!", res)
res = self._helper.execute_cmd_output_string(cmd).split("\n", 2)
print("the value!!!!!!!!!!!!", res)
print("the value!!!!!!!!!!!!", res[1])

if __name__ == "__main__":
    m = Bt()
    print(m.bluetooth_scan())


1. Output when device is enabled:
The value of res!!!! Scanning ...
64:A2:F9:06:63:79 OnePlus 6
the value!!!!!!!!!!!! ['Scanning ...', '\t64:A2:F9:06:63:79\tOnePlus 6']
the value!!!!!!!!!!!! 64:A2:F9:06:63:79 OnePlus 6
None

Process finished with exit code 0

2. Output when device is not enabled

When the device is not connected, I get the below error saying
"IndexError: list index out of range"
The value of res!!!! Scanning ...
Traceback (most recent call last):
the value!!!!!!!!!!!! ['Scanning ...']
  File "/home/srinivasan/Downloads/bt_tests/qa/test_library/Bt.py",
line 96, in <module>
    print(m.bluetooth_scan())
  File "/home/srinivasan/Downloads/bt_tests/qa/test_library/Bt.py",
line 74, in bluetooth_scan
    print("the value!!!!!!!!!!!!", res[1])
IndexError: list index out of range

Process finished with exit code 1

Could you please do the needful

Thanks in advance



















On Tue, Dec 4, 2018 at 3:09 AM Cameron Simpson <cs at cskk.id.au> wrote:
>
> Note: post returned to the tutor list.
>
> Please DO NOT cross post to multiple lists (i.e. tutor and python-list,
> as you have). This makes things difficult for people who are not on both
> lists. Pick a _single_ list, and use that.
>
> On 04Dec2018 02:46, srinivasan <srinivasan.rns at gmail.com> wrote:
> >Could you please help me, as am still learning python syntax, how can
> >I add conditional check for empty string after running "hcitool scan"
> >(ie., when there is no Bluetooth devices discovered) ie., after the
> >word  "Scanning..." , when there are no Bluetooth discover-able
> >devices and then return False in the below python code snippet?
> >
> >
> >Command:
> >~$ sudo hcitool scan
> >Scanning ...                              -------------------------->
> >When there are no BT devices
> >~$ sudo hcitool scan
> >Scanning ...
> >64:A2:F9:06:63:79 OnePlus 6 ---------------------------> When there
> >are BT devices
> >~$
> >
> >Python code snippet:
> >    def bluetooth_scan(self):
> >        """
> >            Start bluetooth scanning process.
> >
> >            :return: Return device info by mac address and name.
> >        """
> >        cmd = "sudo hciconfig hci0 up"
> >        self._helper.execute_cmd_return_code(cmd)
> >
> >        cmd = "sudo hcitool scan"
> >        res = self._helper.execute_cmd_output_string(cmd)
> >
> >        return res
>
> Well you document your function as returning device info by MAC. So part
> the output if "hcitool scan" and for MAC address and name and store that
> in a dictionary (key MAC, value the name). Return the dictionary.
>
> If the dictionary is empty, there were no devices.
>
> Not that like almost all collections, empty dictionaries are "false", so
> you can go:
>
>   bluetooth_devices = scanner.bluetooth_scan()
>   if not bluetooth_devices:
>     ... no devices found ...
>
> Cheers,
> Cameron Simpson <cs at cskk.id.au>