Edit this page on our live server and create a PR by running command !create-pr in the console panel

SoS functions

  • Difficulty level: easy
  • Time need to lean: 5 minutes or less
  • Key points:
    • get_output gets output of command
    • expand_pattern expand pattern from lists of variables

Function get_output

Function get_output(cmd) returns the output of command (decoded in UTF-8), which is a shortcut for subprocess.check_output(cmd, shell=True).decode().

In [1]:
Out[1]:
'/bin/ls\n'

This function also accepts two options show_command=False, and prompt='$ ' that can be useful in case you would like to present the command that produce the output. For example,

In [2]:
$ which ls
/bin/ls

Function expand_pattern

Function expand_pattern expands a string to multiple ones using items of variables quoted between { }. For example,

output: expand_pattern('{a}_{b}.txt')

is equivalent to

output: [f'{x}_{y}.txt' for x,y in zip(a, b)]

if a and b are sequences of the same length. For example,

In [3]:
Out[3]:
["Bob's salary is 200", "John's salary is 300"]

The sequences should have the same length

In [4]:
ExecuteError: [0]: 
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
script_283448644140210882 in <module>
      salary = [200]
----> expand_pattern("{name}'s salary is {salary}")
      

ValueError: Variables in output pattern should have the same length (other=2, len(salary)=1)

An exception is made for variables of simple non-sequence types, in which case they are repeated in all expanded items

In [5]:
Out[5]:
["Bob's salary is 200", "John's salary is 200"]