Welcome, guest | Sign In | My Account | Store | Cart

Eli Criffield python@zendo.net avalible at http://eli.criffield.net/sum/ a better better command line calculator,

put this in a file, run it, cut and past a bunch of numbers (don't worry about the $ signs and commas) hit enter and it adds them up use / * - + like normal

added features over just using python as a command line calculator:

  • numbers can include ,s and $s or anything really and it still works this allows you to cut and paste things like $1,123.65 and it still works
  • add by default! (unless you run with -x) cut and paste a bunch of numbers hit enter and it'll add them all up
  • command line history and readline, up arrow recalls your history even between sessions
  • _ is the last answer, just like in python
Python, 62 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python
# Eli Criffield <python@zendo.net>
# avalible at http://eli.criffield.net/sum/
# a better better command line calculator,
#
# put this in a file, run it, cut and past a bunch of numbers (don't
# worry about the $ signs and commas) hit enter and it adds them up
# use / * -  + like normal
#
# added features over just using python as a command line calculator
# * numbers can include ,s and $s or anything really and it still works
#   this allows you to cut and paste things like $1,123.65 and it still works
# * add by default! (unless you run with -x)
#   cut and paste a bunch of numbers hit enter and it'll add them all up
# * command line history and readline, up arrow recalls your history
#   even between sessions
# * _ is the last answer, just like in python

import readline
import re
import os
import sys
import atexit

histfile = os.path.join(os.environ["HOME"], ".sumhist")
try:
    readline.read_history_file(histfile)
except IOError:
    pass
atexit.register(readline.write_history_file, histfile)

# if ran with -x don't add by default
if len(sys.argv) > 1:
   if sys.argv[1] == "-x":
      nodefsum = True
   else:
      nodefsum = False
else:
   nodefsum = False

del os, atexit, readline, sys, histfile

while 1 :
   try:
      __equation = raw_input("> ")
   except EOFError:
       break
   # substitue out anything not equation like
   __equation = re.sub('([a-zA-Z]|\,|\$|\`|\!|\@|\#|\$|\&|\{|\}|\[|\]|\~|\?|\<|\>|\|)',"",__equation)

   if nodefsum:
      __equation = re.sub('(\s+)'," ",__equation)
   else:
      __equation = re.sub('(\s+)',"+",__equation)
      __equation = re.sub('\+$',"",__equation)

   try:
      _ = eval(__equation)
   except:
      print "ERROR:%s"%__equation
   else:
      print _

This doen't really qualify as a hole program, but i use it every day, it just makes for one nice command line calculator.