Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

An example using Pythonpy and Bash

Alexander Böhn edited this page Apr 24, 2018 · 1 revision

Using Pythonpy and Bash together: defining the pyls command

This example illustrates how Pythonpy can be easily dovetailed with scripted shell commands – in this case, a function written for version 4.0+ of GNU Bash – opening up the richness of Python’s runtime data model to script and command-line tool authors.

We define a command to list both the member items and the attributes of any Python object we can name – that is to say, any Python object we can describe to the Pythonpy py command as a valid argument. Since we’re using a command-line tool to list all these subordinates of something from the Python world, we’ll call our command pyls (a simple portmanteau of the py and ls commands) and define it as a Bash function:

function pyls() {
    if [ "$1" ]; then
        attribute_count=$(py "len(dir(${1}))")
        item_count=$(py "getattr(${1}, '__len__', lambda *a, **k: 0)()")
        what_is_it=$(py "type(${1}).__name__")
        echo -n "> Python ${what_is_it}${1}” has ${item_count} member sub-items"
        [ "${item_count}" -eq "0" ] && echo "" || echo ":"
        py ${1} | py -x '"+ %s" % x'
        echo -n "> Python ${what_is_it}${1}” has ${attribute_count} member attributes"
        [ "${attribute_count}" -eq "0" ] && echo "" || echo ":"
        py "sorted(dir(${1}))" | /usr/local/bin/columns -W `tput cols`
    fi
}

The first and last lines above declare the Bash function by name, and define the block of code that constitutes the functions’ body, starting with function pyls() { and ending with }. Unlike most languages, the arguments to the function are not specified in the typical C-language function-signature style, with parameter names between the parentheses in the function; rather, Bash function arguments are specified and passed using specially reserved variable names. In our function, we are only concerned with the first argument (referenced through the special variable $1), which we assume to be populated with the Python object we want to list, E.G.:

$ pyls 'sys.path'

… In the function body, the first three lines execute Python statements, using Pythonpy commands, and assign their values to local variables:

attribute_count=$(py "len(dir(${1}))")                            # the number of attributes
item_count=$(py "getattr(${1}, '__len__', lambda *a, **k: 0)()")  # the number of items (E.G. in a list)
what_is_it=$(py "type(${1}).__name__")                            # the name of the class of the object in question

… Note in particular the arguments to py to retrieve the item count, in the second line. In line one, we can safely call len() on the return value of dir(), as it is guaranteed to always be a list – but in line two, if we call len() on something without a length (say, a function object)

Clone this wiki locally

Morty Proxy This is a proxified and sanitized view of the page, visit original site.