From: Bartosz Golaszewski Date: Thu, 5 Sep 2019 15:00:24 +0000 (+0200) Subject: bindings: python: examples: check if '__main__' is set X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=8fc50f32cd9670f88ee9faa1ea5d97aa043599fa;p=qemu-gpiodev%2Flibgpiod.git bindings: python: examples: check if '__main__' is set The examples are programs meant to be executed directly and not imported. Use the standard "if __name__ == '__main__':" before any code. Signed-off-by: Bartosz Golaszewski --- diff --git a/bindings/python/examples/gpiodetect.py b/bindings/python/examples/gpiodetect.py index f539f04..9318f51 100755 --- a/bindings/python/examples/gpiodetect.py +++ b/bindings/python/examples/gpiodetect.py @@ -11,8 +11,9 @@ import gpiod -for chip in gpiod.ChipIter(): - print('{} [{}] ({} lines)'.format(chip.name(), - chip.label(), - chip.num_lines())) - chip.close() +if __name__ == '__main__': + for chip in gpiod.ChipIter(): + print('{} [{}] ({} lines)'.format(chip.name(), + chip.label(), + chip.num_lines())) + chip.close() diff --git a/bindings/python/examples/gpiofind.py b/bindings/python/examples/gpiofind.py index 30b8e2b..8505ba0 100755 --- a/bindings/python/examples/gpiofind.py +++ b/bindings/python/examples/gpiofind.py @@ -12,9 +12,10 @@ import gpiod import sys -line = gpiod.find_line(sys.argv[1]) -if line is None: - sys.exit(1) +if __name__ == '__main__': + line = gpiod.find_line(sys.argv[1]) + if line is None: + sys.exit(1) -print('{} {}'.format(line.owner().name(), line.offset())) -line.owner().close() + print('{} {}'.format(line.owner().name(), line.offset())) + line.owner().close() diff --git a/bindings/python/examples/gpioget.py b/bindings/python/examples/gpioget.py index bc7645b..bdff23d 100755 --- a/bindings/python/examples/gpioget.py +++ b/bindings/python/examples/gpioget.py @@ -12,18 +12,19 @@ import gpiod import sys -if len(sys.argv) < 3: - raise TypeError('usage: gpioget.py ...') +if __name__ == '__main__': + if len(sys.argv) < 3: + raise TypeError('usage: gpioget.py ...') -with gpiod.Chip(sys.argv[1]) as chip: - offsets = [] - for off in sys.argv[2:]: - offsets.append(int(off)) + with gpiod.Chip(sys.argv[1]) as chip: + offsets = [] + for off in sys.argv[2:]: + offsets.append(int(off)) - lines = chip.get_lines(offsets) - lines.request(consumer=sys.argv[0], type=gpiod.LINE_REQ_DIR_IN) - vals = lines.get_values() + lines = chip.get_lines(offsets) + lines.request(consumer=sys.argv[0], type=gpiod.LINE_REQ_DIR_IN) + vals = lines.get_values() - for val in vals: - print(val, end=' ') - print() + for val in vals: + print(val, end=' ') + print() diff --git a/bindings/python/examples/gpioinfo.py b/bindings/python/examples/gpioinfo.py index f9d91bf..de4b6f3 100755 --- a/bindings/python/examples/gpioinfo.py +++ b/bindings/python/examples/gpioinfo.py @@ -11,21 +11,22 @@ import gpiod -for chip in gpiod.ChipIter(): - print('{} - {} lines:'.format(chip.name(), chip.num_lines())) +if __name__ == '__main__': + for chip in gpiod.ChipIter(): + print('{} - {} lines:'.format(chip.name(), chip.num_lines())) - for line in gpiod.LineIter(chip): - offset = line.offset() - name = line.name() - consumer = line.consumer() - direction = line.direction() - active_state = line.active_state() + for line in gpiod.LineIter(chip): + offset = line.offset() + name = line.name() + consumer = line.consumer() + direction = line.direction() + active_state = line.active_state() - print('\tline {:>3}: {:>18} {:>12} {:>8} {:>10}'.format( - offset, - 'unnamed' if name is None else name, - 'unused' if consumer is None else consumer, - 'input' if direction == gpiod.Line.DIRECTION_INPUT else 'output', - 'active-low' if active_state == gpiod.Line.ACTIVE_LOW else 'active-high')) + print('\tline {:>3}: {:>18} {:>12} {:>8} {:>10}'.format( + offset, + 'unnamed' if name is None else name, + 'unused' if consumer is None else consumer, + 'input' if direction == gpiod.Line.DIRECTION_INPUT else 'output', + 'active-low' if active_state == gpiod.Line.ACTIVE_LOW else 'active-high')) - chip.close() + chip.close() diff --git a/bindings/python/examples/gpiomon.py b/bindings/python/examples/gpiomon.py index 2615f4a..1c56176 100755 --- a/bindings/python/examples/gpiomon.py +++ b/bindings/python/examples/gpiomon.py @@ -12,35 +12,36 @@ import gpiod import sys -def print_event(event): - if event.type == gpiod.LineEvent.RISING_EDGE: - evstr = ' RISING EDGE' - elif event.type == gpiod.LineEvent.FALLING_EDGE: - evstr = 'FALLING EDGE' - else: - raise TypeError('Invalid event type') - - print('event: {} offset: {} timestamp: [{}.{}]'.format(evstr, - event.source.offset(), - event.sec, event.nsec)) - -if len(sys.argv) < 3: - raise TypeError('usage: gpiomon.py ...') - -with gpiod.Chip(sys.argv[1]) as chip: - offsets = [] - for off in sys.argv[2:]: - offsets.append(int(off)) - - lines = chip.get_lines(offsets) - lines.request(consumer=sys.argv[0], type=gpiod.LINE_REQ_EV_BOTH_EDGES) - - try: - while True: - ev_lines = lines.event_wait(sec=1) - if ev_lines: - for line in ev_lines: - event = line.event_read() - print_event(event) - except KeyboardInterrupt: - sys.exit(130) +if __name__ == '__main__': + def print_event(event): + if event.type == gpiod.LineEvent.RISING_EDGE: + evstr = ' RISING EDGE' + elif event.type == gpiod.LineEvent.FALLING_EDGE: + evstr = 'FALLING EDGE' + else: + raise TypeError('Invalid event type') + + print('event: {} offset: {} timestamp: [{}.{}]'.format(evstr, + event.source.offset(), + event.sec, event.nsec)) + + if len(sys.argv) < 3: + raise TypeError('usage: gpiomon.py ...') + + with gpiod.Chip(sys.argv[1]) as chip: + offsets = [] + for off in sys.argv[2:]: + offsets.append(int(off)) + + lines = chip.get_lines(offsets) + lines.request(consumer=sys.argv[0], type=gpiod.LINE_REQ_EV_BOTH_EDGES) + + try: + while True: + ev_lines = lines.event_wait(sec=1) + if ev_lines: + for line in ev_lines: + event = line.event_read() + print_event(event) + except KeyboardInterrupt: + sys.exit(130) diff --git a/bindings/python/examples/gpioset.py b/bindings/python/examples/gpioset.py index efc11f2..3e7b121 100755 --- a/bindings/python/examples/gpioset.py +++ b/bindings/python/examples/gpioset.py @@ -12,18 +12,19 @@ import gpiod import sys -if len(sys.argv) < 3: - raise TypeError('usage: gpioset.py = ...') +if __name__ == '__main__': + if len(sys.argv) < 3: + raise TypeError('usage: gpioset.py = ...') -with gpiod.Chip(sys.argv[1]) as chip: - offsets = [] - values = [] - for arg in sys.argv[2:]: - arg = arg.split('=') - offsets.append(int(arg[0])) - values.append(int(arg[1])) + with gpiod.Chip(sys.argv[1]) as chip: + offsets = [] + values = [] + for arg in sys.argv[2:]: + arg = arg.split('=') + offsets.append(int(arg[0])) + values.append(int(arg[1])) - lines = chip.get_lines(offsets) - lines.request(consumer=sys.argv[0], type=gpiod.LINE_REQ_DIR_OUT) - lines.set_values(values) - input() + lines = chip.get_lines(offsets) + lines.request(consumer=sys.argv[0], type=gpiod.LINE_REQ_DIR_OUT) + lines.set_values(values) + input()