Python

Note: Configuration from this KB can be applied through this Ansible playbook.

Python shell autocomplete

Taken from here.

$ cat ~/.pythonrc

# ~/.pythonrc
# enable syntax completion
try:
    import readline
except ImportError:
    print("Module readline not available.")
else:
    import rlcompleter
    readline.parse_and_bind("tab: complete")

Python IDE

Long story short, lets go VIM + terminal ;-). Vim plugin jedi-vim for python autocomplete installation can be found here.

Python debugger pdb

To start pdb use either:

  • breakpoint() - directly in the code and then just run it ./xyz.py
  • python -m pdb xyz.py - run python script in debugger

pdb commands

# view all variable names
dir()

# Pretty print variable content
pp <variable>

# Print variable content
p <variable>

# Print the source code around current line
l

# Print the source code around until current line
ll

# Step - execute current line
s

# Continue program execution
c

# Execute until return from current func
r

# Quit
q

Auto-generate imports

Tool used is pyflyby and content is taken from here.

  • installation: pip3 install pyflyby
  • basic usage: tidy-imports <filename>.py
  • vim on-save:
    cat ~/.vimrc
    ...
    " autogenerate import for python files on save
    autocmd bufwritepost *.py execute  "silent !tidy-imports --quiet --replace-star-imports --action REPLACE " . bufname("%")
    ...
    

Simple web server

python -m http.server 8080