konch (GitHub) is a command-line and configuration utility for your Python shells, whether it’s built-in, IPython, or BPython. It can help customize them.

1   konch

Its configuration file .konchrc has four elements to customize:

context
what’s loaded automatically when shell is ready.
shell
the default shell.
banner
the greeting text.
prompt
the prompt text.

It looks for .konchrc in current working directory, you can initialize one by running konch init. The command-line tool also has some other options to override. The configuration can also host multiple named configurations.

2   calculator

A few years back, I tried to make the built-in Python shell to work like a calcualtor. Now with konch, I could have the following .konchrc:


# -*- coding: utf-8 -*-
import konch
from math import *

# from https://github.com/sloria/konch/issues/2
context = dict(locals())
for k in ('konch', 'context', 'fname', 'locals_', 'globals_'):
if k in context:
del context[k]

konch.config({
'context': context,
'prompt': 'do the math > ',
})

This configuration is a bit of tricky since I want to load all mathematical function or constants without manually listing one by one. Normally, the context is either a dict or list, for example:


import math
context = {
'sin': math.sin
}

# or
context = [math.sin]

Either can work, but list method has its limitation, every item must have __name__ attribute. For math.pi, it a float and it doesn’t have such attribute. You will get an error if you put it into the context list.

The shell looks like, with konch 0.3.2:


$ konch
2.7.5 (default, Dec 5 2013, 16:57:38)
[GCC 4.7.3]

"Hooray for the magic conches!"

Context:
pow: <built-in function pow>
fsum: <built-in function fsum>
[snip]
pi: 3.141592653589793
log10: <built-in function log10>
[snip]
cos: <built-in function cos>
e: 2.718281828459045
[snip]

do the math > sin(pi/2)
1.0
do the math > pi
3.141592653589793
do the math > log(e)
1.0
do the math > 1 + 1 * 3
4
do the math >

You can see the banner just below the version numbers, I didn’t change that part, the text is provided by konch.speak(). It talks randomly, but always with the word “conch.”

The text is followed by the context list, that’s what you can immediately to use without needing to import on your own or every time you start the shell. konch will load those for you, if you write the configuration right.

Then the prompt, since it’s a calculator, I changed it to a proper one.

3   thought

Frankly, I don’t use any Python shell often, but I like this konch, and I can see why the author, Steven Loria, developed it. It would increase the productivity if you need to start a shell with libraries every single time you use the shell. It will save a lot of time. And if you also have different settings for various projects, konch certainly is a good solution.