ToDonePy Helper Functions

Function: itemsetter

helpers.itemsetter.itemsetter(*items: int) → Callable[[List, Any], None]

Return a callable object that sets item from its operand

This is essentially the opposite of operator.itemgetter. If only one position is specified, the resulting callable will set that item. If multiple positions are specified, it sets all items

Parameters

*items (int) – The indices to be set. Remember, Python is 0-indexed

Returns

Callable[[List, Any], None] – A function that will set the indices specified in items to a given value.

Examples

>>> x = ['a', 'b', 'c']
>>> f = itemsetter(2)
>>> f(x, 'z')
>>> print(x)
['a', 'b', 'z']

Function: external_command

helpers.external_command.external_command(args: List[str]) → subprocess.CompletedProcess

Make a generic command line call

Any command line call can be made. Pass the respective components as individual strings. Roughly speaking, anywhere there is a space, break it into a new component. See the documentation on subprocess.run for advanced use cases.

Note

If run in a situation where the user was providing a dynamic input, there are obvious security risks. In the app, however, the user cannot provide their own input, which I believe sufficiently mitigates the risk in this use case. Obviously, if you adopt and use this function elsewhere, take care to check your inputs!

Parameters

*args (List[str]) – The parts of the external command

Returns

subprocess.CompletedProcess – If successful. This contains a number of useful attributes, including returncode and stdout.

Raises
  • OSError – If unsuccessful. This will be thrown if the command found in args[0] cannot be found on the OS

  • subprocess.CalledProcessError – If the called command returns a non-zero exit status

Examples

The results of a successful command ccan be queried like so:

>>> results = external_command(['echo', 'hello'])
>>> results.returncode
0