Skip to content

Instantly share code, notes, and snippets.

@mindey
Last active April 11, 2016 23:32
Show Gist options
  • Save mindey/10fb5b9a0f63fe372e86ce6c4e60aea6 to your computer and use it in GitHub Desktop.
Save mindey/10fb5b9a0f63fe372e86ce6c4e60aea6 to your computer and use it in GitHub Desktop.

Often, we would like to use "Python" as calculator. However, "ipython" command loads much slower than "R". Here is a solution -- let's define an "S" command:

Install http://mpmath.org/'s pack globally.

sudo pip install mpmath

Create initialization script ~/.S.py with following content, for 50 digits of precision:

from mpmath import *
mp.dps = 50; mp.pretty = True

Create a ~/.bashrc or ~/.zshrc alias:

alias S="python -i ~/.S.py"

Just run S command, and you can use many things. Optionally, I'd recommend adding more things to ~/.S.py, but most things, keep in mind, that even importing something as light as like sympy, will slow down the loading time.

Play around:

http://fredrikj.net/blog/2011/03/100-mpmath-one-liners-for-pi/

Autocompletion:

For autocompletion, you can use jedi by adding these lines to ~/.S.py:

try:
    from jedi.utils import setup_readline
    setup_readline()
except ImportError:
    # Fallback to the stdlib readline completer if it is installed.
    # Taken from http://docs.python.org/2/library/rlcompleter.html
    print("Jedi is not installed, falling back to readline")
    try:
        import readline
        import rlcompleter
        readline.parse_and_bind("tab: complete")
    except ImportError:
        print("Readline is not installed either. No tab completion is enabled.")

http://jedi.jedidjah.ch/en/latest/docs/usage.html#tab-completion-in-the-python-shell.

So, it loads much faster than bpython, ipython, ptpython. If you just need to do basic math, it may be what you need. :)

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