Summary Append to list only if that value is the next value expected.
""" Return codes can be wrong because errors happen. And the next return code can be reliant on this one. This module checks that a return code is expected next and that only then does it become the latest piece of a fulfilling order. """ __all__ = ['order_two_with_retry', 'wait_on_the_return_of_a_commandline'] import functools import operator import subprocess import sys import time import typing insert_object_at_the_end_of_the_list = list.append # optimization def if_the_list_is_empty(insertion: typing.Any, out: typing.List, expectation: typing.List) -> None: assert not out, 'the list is not empty' a = [insertion] if a == expectation[:1]: insert_object_at_the_end_of_the_list(out, insertion) def if_the_list_is_not_empty(insertion: typing.Any, out: typing.List, expectation: typing.List) -> None: a = out[::] insert_object_at_the_end_of_the_list(a, insertion) if a == expectation[:len(a)]: insert_object_at_the_end_of_the_list(out, insertion) else: out.clear() def lever(insertion: typing.Any, out: typing.List, expectation: typing.List) -> None: """Test equality (one).""" if not out: if_the_list_is_empty(insertion, out, expectation) else: if_the_list_is_not_empty(insertion, out, expectation) def order_two(insertionm: typing.Callable, insertionn: typing.Callable, out: typing.List, expectation: typing.List) -> None: lever(insertionm(), out, expectation) print(out) if not out: return lever(insertionn(), out, expectation) print(out) def order_two_with_retry(insertionm: typing.Callable, insertionn: typing.Callable, out: typing.List, expectation: typing.List) -> None: order_two(insertionm, insertionn, out, expectation) if not out: order_two(insertionm, insertionn, out, expectation) # Test equality (all). test_insertions_against_expectation = operator.eq def main(): insertionm = functools.partial(wait_on_the_return_of_a_commandline, 'python -c "import random; import sys; sys.exit(random.randrange(0, 1 + 1))"') insertionn = insertionm out =  expectation = [0, 0] order_two_with_retry(insertionm, insertionn, out, expectation) # This function can silence this module. # Reestablish noise with builtins.print def set_print(new_print): # noinspection PyShadowingBuiltins,PyGlobalUndefined global print # noinspection PyShadowingBuiltins print = new_print def filler(executingprocess): s = 0.4 while executingprocess.poll() is None: sys.stdout.write('\r. ') time.sleep(s) sys.stdout.write('\r.. ') time.sleep(s) sys.stdout.write('\r...') time.sleep(s) def wait_on_the_return_of_a_commandline(commandline): executingprocess = subprocess.Popen(commandline) filler(executingprocess) return executingprocess.returncode if __name__ == '__main__': main()
- Dunder all contains the two functions necessary to get the module running.
- In if_the_list... the new list is named a because a, b are the names of arguments to operators and I'm testing for equality (operator.eq(a, b)).
- The comment 'optimization' is there because that function does less steps then the next function for the same computation.
- out[::] is faster than list(out) on my computer; use of slices not to modify lists too soon and by accident (slices create new lists).
- If this returncode does not match the expected returncode, out is cleared because I want to try again, not ignore and keep going.
- order_two_with_retry retries once, and if I have an odd number, I should use lever for that. This is where the return code appears by calling a function or opening a subprocess.
- The alias to operator.eq: the name fits it into the module. Test for equality: out is a, expectation is b.
- main generates 0 or 1 from a subprocess randomly. It shows module usage.
- prints in order_two can be silenced by setting print to something
def print(*args, **kwargs): pass
- And finally, wait on subprocess exitcode with a console animation that repeats one dot, two, three.
I'm looking for tips on commenting code as I'm not sure how to do it or what the reader needs.