Source

pythonwise / calc.py

#!/usr/bin/env python
'''Small calculator'''

# ======================================================#
# Copyright (c) Miki Tebeka <miki.tebeka@gmail.com>     #
# This file is under the GNU Public License (GPL), see  #
# http://www.gnu.org/copyleft/gpl.html for more details #
# ======================================================#

from __future__ import division # Detault to real devision
from math import * # Get all math goodies in global namespace
from optparse import OptionParser

description = 'Calculate math expression (e.g. calc \'7 * 29\' -> 203)'

def show_gui(text=''):
    import Tkinter as tk
    from operator import isNumberType

    root = tk.Tk()
    root.title('The Humble Calc')
    root.option_add('*font', 'Courier -40 bold')
    root.bind('<Escape>', lambda e: root.quit())
    expr = tk.Entry(root, width=40)
    expr.insert(0, text)
    expr.pack(side=tk.LEFT)
    tk.Label(root, text='=').pack(side=tk.LEFT)
    answer = tk.Label(root)
    answer.pack(side=tk.LEFT)
    expr.focus()

    def poll():
        s = expr.get()
        try:
            ans = eval(expr.get())
            if isNumberType(ans):
                answer['text'] = str(eval(s))
        except Exception, e:
            answer['text'] = '???'
        root.after(100, poll)

    poll()
    root.mainloop()

def main(argv=None):
    if argv is None:
        import sys
        argv = sys.argv

    from argparse import ArgumentParser


    parser = ArgumentParser(description=description)
    parser.add_argument('expression', help='math expression', default='',
                       nargs='?')
    parser.add_argument('-g', '--gui', help='run in gui mode',
        dest='gui', action='store_true', default=False)

    args = parser.parse_args(argv[1:])

    args.gui = (not sys.stdout.isatty()) or args.gui

    if args.gui:
        show_gui(args.expression)
    else:
        try:
            print(eval(args.expression or '0'))
        except Exception as e:
            raise SystemExit('error: {0}'.format(e))

if __name__ == '__main__':
    main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.