diff --git a/bindings/python/examples/iio_readdev.py b/bindings/python/examples/iio_readdev.py index 8a77e008c..8ef1049d9 100755 --- a/bindings/python/examples/iio_readdev.py +++ b/bindings/python/examples/iio_readdev.py @@ -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() @@ -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: @@ -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) @@ -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() @@ -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 @@ -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 @@ -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 @@ -210,5 +241,5 @@ def main(): reader.read() -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/bindings/python/examples/iio_writedev.py b/bindings/python/examples/iio_writedev.py index 6ad235389..1c0b3fa81 100755 --- a/bindings/python/examples/iio_writedev.py +++ b/bindings/python/examples/iio_writedev.py @@ -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() @@ -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: @@ -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) @@ -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() @@ -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 @@ -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 @@ -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 = [] @@ -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() @@ -231,5 +262,5 @@ def main(): writer.write() -if __name__ == '__main__': +if __name__ == "__main__": main()