Skip to content

Commit

Permalink
black: apply black to iio_readdev.py & iio_writedev.py
Browse files Browse the repository at this point in the history
Use black, the python formatter on iio_readdev.py & iio_writedev.py

Signed-off-by: Robin Getz <[email protected]>
  • Loading branch information
rgetz committed May 18, 2020
1 parent 3a02317 commit 7b112c7
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 37 deletions.
67 changes: 49 additions & 18 deletions bindings/python/examples/iio_readdev.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Arguments:

def __init__(self):
"""Arguments class constructor."""
self.parser = argparse.ArgumentParser(description='iio_readdev')
self.parser = argparse.ArgumentParser(description="iio_readdev")
self._add_parser_arguments()
args = self.parser.parse_args()

Expand All @@ -43,21 +43,48 @@ def __init__(self):

def _add_parser_arguments(self):
self.parser.add_argument(
'-n', '--network', type=str, metavar='', help='Use the network backend with the provided hostname.')
"-n",
"--network",
type=str,
metavar="",
help="Use the network backend with the provided hostname.",
)
self.parser.add_argument(
'-u', '--uri', type=str, metavar='', help='Use the context with the provided URI.')
"-u",
"--uri",
type=str,
metavar="",
help="Use the context with the provided URI.",
)
self.parser.add_argument(
'-b', '--buffer-size', type=int, metavar='', help='Size of the capture buffer. Default is 256.')
"-b",
"--buffer-size",
type=int,
metavar="",
help="Size of the capture buffer. Default is 256.",
)
self.parser.add_argument(
'-s', '--samples', type=int, metavar='', help='Number of samples to capture, 0 = infinite. Default is 0.')
"-s",
"--samples",
type=int,
metavar="",
help="Number of samples to capture, 0 = infinite. Default is 0.",
)
self.parser.add_argument(
'-T', '--timeout', type=int, metavar='', help='Buffer timeout in milliseconds. 0 = no timeout')
"-T",
"--timeout",
type=int,
metavar="",
help="Buffer timeout in milliseconds. 0 = no timeout",
)
self.parser.add_argument(
'-a', '--auto', action='store_true',
help='Scan for available contexts and if only one is available use it.'
"-a",
"--auto",
action="store_true",
help="Scan for available contexts and if only one is available use it.",
)
self.parser.add_argument('device', type=str, nargs=1)
self.parser.add_argument('channel', type=str, nargs='*')
self.parser.add_argument("device", type=str, nargs=1)
self.parser.add_argument("channel", type=str, nargs="*")


class ContextBuilder:
Expand All @@ -81,12 +108,12 @@ def _timeout(self):
def _auto(self):
contexts = iio.scan_contexts()
if len(contexts) == 0:
raise Exception('No IIO context found.\n')
raise Exception("No IIO context found.\n")
if len(contexts) == 1:
uri, _ = contexts.popitem()
self.ctx = iio.Context(_context=uri)
else:
print('Multiple contexts found. Please select one using --uri!')
print("Multiple contexts found. Please select one using --uri!")
for uri, _ in contexts.items():
print(uri)
sys.exit(0)
Expand Down Expand Up @@ -117,7 +144,7 @@ def create(self):
else:
self._default()
except FileNotFoundError:
raise Exception('Unable to create IIO context!\n')
raise Exception("Unable to create IIO context!\n")

self._timeout()

Expand All @@ -144,7 +171,7 @@ def _device(self):
self.dev = self.ctx.find_device(self.arguments.device_name)

if self.dev is None:
raise Exception('Device %s not found!' % self.arguments.device_name)
raise Exception("Device %s not found!" % self.arguments.device_name)

return self

Expand All @@ -165,7 +192,7 @@ def create(self):
buffer = iio.Buffer(self.dev, self.arguments.buffer_size)

if buffer is None:
raise Exception('Unable to create buffer!\n')
raise Exception("Unable to create buffer!\n")

return buffer

Expand Down Expand Up @@ -193,8 +220,12 @@ def read(self):
samples = self.buffer.read()

if self.arguments.num_samples > 0:
sys.stdout.buffer.write(samples[:min(self.arguments.num_samples, len(samples))])
self.arguments.num_samples -= min(self.arguments.num_samples, len(samples))
sys.stdout.buffer.write(
samples[: min(self.arguments.num_samples, len(samples))]
)
self.arguments.num_samples -= min(
self.arguments.num_samples, len(samples)
)

if self.arguments.num_samples == 0:
break
Expand All @@ -210,5 +241,5 @@ def main():
reader.read()


if __name__ == '__main__':
if __name__ == "__main__":
main()
69 changes: 50 additions & 19 deletions bindings/python/examples/iio_writedev.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Arguments:

def __init__(self):
"""Arguments class constructor."""
self.parser = argparse.ArgumentParser(description='iio_writedev')
self.parser = argparse.ArgumentParser(description="iio_writedev")
self._add_parser_arguments()
args = self.parser.parse_args()

Expand All @@ -45,23 +45,51 @@ def __init__(self):

def _add_parser_arguments(self):
self.parser.add_argument(
'-n', '--network', type=str, metavar='', help='Use the network backend with the provided hostname.')
"-n",
"--network",
type=str,
metavar="",
help="Use the network backend with the provided hostname.",
)
self.parser.add_argument(
'-u', '--uri', type=str, metavar='', help='Use the context with the provided URI.')
"-u",
"--uri",
type=str,
metavar="",
help="Use the context with the provided URI.",
)
self.parser.add_argument(
'-b', '--buffer-size', type=int, metavar='', help='Size of the capture buffer. Default is 256.')
"-b",
"--buffer-size",
type=int,
metavar="",
help="Size of the capture buffer. Default is 256.",
)
self.parser.add_argument(
'-s', '--samples', type=int, metavar='', help='Number of samples to capture, 0 = infinite. Default is 0.')
"-s",
"--samples",
type=int,
metavar="",
help="Number of samples to capture, 0 = infinite. Default is 0.",
)
self.parser.add_argument(
'-T', '--timeout', type=int, metavar='', help='Buffer timeout in milliseconds. 0 = no timeout')
"-T",
"--timeout",
type=int,
metavar="",
help="Buffer timeout in milliseconds. 0 = no timeout",
)
self.parser.add_argument(
'-a', '--auto', action='store_true',
help='Scan for available contexts and if only one is available use it.'
"-a",
"--auto",
action="store_true",
help="Scan for available contexts and if only one is available use it.",
)
self.parser.add_argument(
'-c', '--cyclic', action='store_true', help='Use cyclic buffer mode.')
self.parser.add_argument('device', type=str, nargs=1)
self.parser.add_argument('channel', type=str, nargs='*')
"-c", "--cyclic", action="store_true", help="Use cyclic buffer mode."
)
self.parser.add_argument("device", type=str, nargs=1)
self.parser.add_argument("channel", type=str, nargs="*")


class ContextBuilder:
Expand All @@ -85,12 +113,12 @@ def _timeout(self):
def _auto(self):
contexts = iio.scan_contexts()
if len(contexts) == 0:
raise Exception('No IIO context found.\n')
raise Exception("No IIO context found.\n")
if len(contexts) == 1:
uri, _ = contexts.popitem()
self.ctx = iio.Context(_context=uri)
else:
print('Multiple contexts found. Please select one using --uri!')
print("Multiple contexts found. Please select one using --uri!")
for uri, _ in contexts.items():
print(uri)
sys.exit(0)
Expand Down Expand Up @@ -121,7 +149,7 @@ def create(self):
else:
self._default()
except FileNotFoundError:
raise Exception('Unable to create IIO context!\n')
raise Exception("Unable to create IIO context!\n")

self._timeout()

Expand All @@ -148,7 +176,7 @@ def _device(self):
self.dev = self.ctx.find_device(self.arguments.device_name)

if self.dev is None:
raise Exception('Device %s not found!' % self.arguments.device_name)
raise Exception("Device %s not found!" % self.arguments.device_name)

return self

Expand All @@ -169,7 +197,7 @@ def create(self):
buffer = iio.Buffer(self.dev, self.arguments.buffer_size, self.arguments.cyclic)

if buffer is None:
raise Exception('Unable to create buffer!\n')
raise Exception("Unable to create buffer!\n")

return buffer

Expand Down Expand Up @@ -197,8 +225,11 @@ def write(self):
num_samples = self.arguments.num_samples

while app_running:
bytes_to_read = len(self.buffer) if num_samples == 0 \
bytes_to_read = (
len(self.buffer)
if num_samples == 0
else min(len(self.buffer), num_samples * self.device.sample_size)
)
write_len = bytes_to_read
data = []

Expand All @@ -210,7 +241,7 @@ def write(self):
data.extend(read_data)

if self.buffer.write(bytearray(data)) == 0:
raise Exception('Unable to push buffer!')
raise Exception("Unable to push buffer!")

self.buffer.push()

Expand All @@ -231,5 +262,5 @@ def main():
writer.write()


if __name__ == '__main__':
if __name__ == "__main__":
main()

0 comments on commit 7b112c7

Please sign in to comment.