Giter VIP home page Giter VIP logo

pythonrc's Introduction

lonetwin's pimped-up pythonrc

What is this ?

This is a python script intended to improve on the default Python interactive shell experience.

Unlike ipython, bpython or any of the many other options out there, this is not designed to be used as a separate interactive environment. The intent is to keep it as a single file and use it as any other rcfile. This script relies solely on the standard python library and will always remain that way.

Demo

demo

Usage

The pythonrc file will be executed when the Python interactive shell is started, if $PYTHONSTARTUP is in your environment and points to the file.

You could also simply make the file executable and call it directly.

Additionally, this file will in turn, execute a virtual env specific rc file [1] if it exists, for the current session, enabling you to pre-populate sessions specific to virtual environments.

Features

The file creates an InteractiveConsole instance and executes it. This instance provides:

  • execution history
  • colored prompts and pretty printing
  • auto-indentation
  • intelligent tab completion [2]
    • without preceding text four spaces
    • with preceding text
      • names in the current namespace
      • for objects, their attributes/methods
      • for strings with a /, pathname completion
      • module name completion in an import statement
  • edit the session or a file in your $EDITOR (the \e command)
    • without no arguments, opens your $EDITOR with the session hstory
    • with filename argument, opens the file in your $EDITOR
    • with object as an argument, opens the source code for the object in $EDITOR
  • list the source code for objects when available (the \l command)
  • temporary escape to $SHELL or ability to execute a shell command and capturing the output in to the _ variable (the ! command)
  • convenient printing of doc stings (the ? command) and search for entries in online docs (the ?? command)
  • auto-execution of a virtual env specific (.venv_rc.py) file at startup

If you have any other good ideas please feel free to submit pull requests or issues.

Configuration

The code attempts to be easy to read and modify to suit personal preferences as easily as possible. You can change any of the commands or the options like the path to the history file, its size etc in the config dict at the top of the rc file. For instance, if you prefer to set the default edit command to %edit instead of the default \e, you just have to change the entry in the config dict.

Note that, the init_readline() method also reads your .inputrc file if it exists. This allows you to share the same readline behavior as all other tools that use readline. For instance, in my personal ~/.inputrc I have the following:

# - when performing completion in the middle of a word, do not insert characters
# from the completion that match characters after point in the word being
# completed
set skip-completed-text on

# - displays possible completions using different colors according to file type.
set colored-stats on

# - show completed prefix in a different color
set colored-completion-prefix on

# - jump temporarily to matching open parenthesis
set blink-matching-paren on

set expand-tilde on
set history-size -1
set history-preserve-point on

"\e[A": history-search-backward
"\e[B": history-search-forward

A little history

Ever since around 2005, I've been obsessed with tweaking my python interactive console to have it behave the way I prefer. Despite multiple attempts I've failed to embrace ipython on the command line because some of ipython's approach just don't fit my head. Additionally, ipython is a full environment and I just need some conveniences added to the default environment. This is why I started maintaining my own pythonrc. I started eventually sharing it as a gist back in 2014 and now about 38 revisions later, I think it might just make sense to set it up as a project so that I can accept pull requests, bug reports or suggestions in case somebody bothers to use it and contribute back.

Known Issue

The console is not __main__. The issue was first reported by @deeenes in the gist I used to maintain. In essence, this code fails:

>>> import timeit
>>>
>>> def getExecutionTime():
...     t = timeit.Timer("sayHello()", "from __main__ import sayHello")
...     return t.timeit(2)
...
>>> def sayHello():
...     print("Hello")
...
>>> print(getExecutionTime())
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "<console>", line 3, in getExecutionTime
  File "/usr/lib64/python2.7/timeit.py", line 202, in timeit
    timing = self.inner(it, self.timer)
  File "<timeit-src>", line 3, in inner
ImportError: cannot import name sayHello
>>>

There are two possible workarounds for this:

  • When within the console, if you have to reference local names via __main__, remember to do it via __main__.pymp.locals instead, something like (for the example above):

    ...
    def getExecutionTime():
        t = timeit.Timer("sayHello()", "from __main__ import pymp; sayHello = pymp.locals['sayHello']")
    ...
    
  • Or in the pythonrc file, change the initialization of ImprovedConsole to accept locals(). That is something like this:

    pymp = ImprovedConsole(locals=locals())
    

    Although the downside of this is, doing it will pollute your console namespace with everything in the pythonrc file.

[1]Named .venv_rc.py by default, but like almost everything else, is configurable
[2]Since python 3.4 the default interpreter also has tab completion enabled however it does not do pathname completion

pythonrc's People

Contributors

lonetwin avatar cmcaine avatar jdormit avatar

Stargazers

Jorge Gomez avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.