Skip to content

Instantly share code, notes, and snippets.

@parente
Last active March 7, 2023 11:50
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save parente/b6ee0efe141822dfa18b6feeda0a45e5 to your computer and use it in GitHub Desktop.
Save parente/b6ee0efe141822dfa18b6feeda0a45e5 to your computer and use it in GitHub Desktop.
Jupyter Tidbit: IPython's ! returns an SList

Summary

IPython shell assignment (the ! operator) evaluates a command using the local shell (e.g., bash) and returns a string list (IPython.utils.text.SList). An SList is a list-like object containing "chunks" of stdout and stderr, properties for accessing those chunks in different forms, and convenience methods for operating on them.

Example

Binder

The SList.ipynb notebook below uses SList properties to access the output of a shell command as a list-like of strings, a newline-separated string, a space-separated string, and a list of pathlib.Path objects. The notebook then uses the SList.fields() and SList.grep() methods to extract columns from and search command output.

Why is this useful?

You can take advantage of the properties and methods of an SList to transform shell output into forms more amenable to further operations in Python.

For more information

See the IPython documentation about Shell Assignment for examples of executing shell commands with optional Python values as inputs. See the IPython documentation about String Lists for additional demontrations of the utility of SLists.

Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@cupdike
Copy link

cupdike commented Dec 3, 2021

Thanks, this was useful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment