Pytemp 0.4¶
Pytemp is a commandline temperature converter written in Python. You will need Python installed on your system to be able to run it.
Python can be obtained from https://www.python.org.
The program should be self explanitory, have a look at the source code. If you find any bugs then let me know. Suggestions on how to improve pytemp are welcome.
Python Highlight Example¶
1#!/usr/bin/python
2#######################
3# Python Temperature Converter - Author: Matthew Tunstall 2006
4# -------------------------------------------------------------------------------
5# Aim: To take a command line temperature value and convert it to another scale
6# Usage: pytemp -c -f 24 #Convert 24 degrees C to F
7#######################
8# Copyright (C) 2006 Matthew Tunstall
9#
10# This program is free software; you can redistribute it and/or
11# modify it under the terms of the GNU General Public License
12# as published by the Free Software Foundation; either version 2
13# of the License, or (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program; if not, write to the Free Software
22# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23#######################
24usage ="""
25PyTemp - Python Temperature Converter
26-------------------------------------
27USAGE: pytemp.py [from units] [to units] [value]
28
29Units:
30------
31-c or -C = Celsius
32-f or -F = Fahrenheit
33-k or -K = Kelvin
34
35Example:
36--------
37To convert 20 Celsius to Fahrenheit
38pytemp.py -c -f 20
39"""
40#######################
41# Revision History
42# ----------------
43# 0.4 - Release Date [10/9/2006]
44# 0.4 - Rewrite of codebase to clean and optimise
45# 0.3 - Working bug free code
46# 0.2 - Working code with some error checking
47# 0.1 - Initial Code Outline
48#######################
49bugemail = "bugs@deltanova.co.uk # Email address for bug reports
50import sys # To gain access to the argv command to access command line args.
51error="""
52Something unexpected has occured for this message to be displayed.
53Please submit a bug report to: """ + bugemail +"\n"
54#######################
55def abort(x): # Abort routine to exit the program when error detected
56 print usage
57 print x
58 print "Application Terminating"
59 sys.exit()
60#######################
61# Calculation Definitions
62#######################
63def f2c(temp):
64 fromScale = "Fahrenheit"
65 toScale = "Celsius"
66 temp = temp - 32
67 x = 5.0/9.0
68 temp = temp*x
69 return temp, fromScale, toScale
70#######################
71def c2f(temp):
72 fromScale = "Celsius"
73 toScale = "Fahrenheit"
74 x = 9.0/5.0
75 temp = temp*x
76 temp = temp + 32
77 return temp, fromScale, toScale
78#######################
79def f2k(temp):
80 fromScale = "Fahrenheit"
81 toScale = "Kelvin"
82 x,y,z = f2c(temp) # y & z take the fromScale and toScale values returned by f2c()
83 x = x + 273.15
84 return x, fromScale, toScale
85#######################
86def c2k(temp):
87 fromScale = "Celsius"
88 toScale = "Kelvin"
89 temp = temp + 273.15
90 return temp, fromScale, toScale
91#######################
92def k2c(temp):
93 fromScale = "Kelvin"
94 toScale = "Celsius"
95 temp = temp - 273.15
96 return temp, fromScale, toScale
97#######################
98def k2f(temp):
99 fromScale = "Kelvin"
100 toScale = "Fahrenheit"
101 x,w,v = k2c(temp) #w & v take the fromScale and toScale values returned by k2c()
102 print x
103 y = 9.0/5.0
104 temp = (x*y)+32
105 return temp, fromScale, toScale
106########################
107# Conversion Calculations #
108# ------------------------------------------------------------------------------#
109# From To Fahrenheit To Celsius To Kelvin #
110# Fahrenheit(F) F (F-32)*(5/9) (F-32)*(5/9)+273.15 #
111# Celsius(C) (C*(9/5))+32 C C+273.15 #
112# Kelvin(K) (K-273.15)*(9/5)+32 K-273.15 K #
113########################
114def getargs():
115 args = sys.argv[1:] # Get command line args but strip the first arg(the file name).
116 argtest = [] # An empty list. Compare with args to check for command line arguments
117 if (args == argtest):
118 abort("No arguments supplied")
119 else: # If Arguments found
120 numargs = len(args)
121 if (numargs == 3):
122 x = args
123 elif (numargs < 3):
124 abort("Too few arguments supplied.")
125 elif (numargs > 3):
126 abort("Too many arguments supplied.")
127 else:
128 abort(error)
129 return x
130#######################
131def lowerargs(args): # Take the arguments and convert to lowercase to aid processing.
132 b = []
133 for x in args:
134 x = x.lower()
135 b.append(x)
136 return b
137#######################
138def tofloat(x): # Attempt to convert the input to a float
139 try:
140 x = float(x)
141 except ValueError:
142 y = 0
143 else:
144 y = 1
145 return y
146#######################
147def validtest1(args):
148 pos =[] # Position log, allows us to see which arg(s) validated
149 for a in args:
150 pos.append(tofloat(a))
151 poschk = [0,0,1] # Indicates the ideal result, only the last arg will convert to a float
152 if poschk == pos:
153 args[2]=float(args[2]) # Make float conversion on last arg permanent
154 else:
155 abort("Invalid Set of Arguments")
156 return args
157#######################
158def validtest2(args):
159 argchk = ["-c","-f","-k"]
160 testlist = [] # Create a test list to compare with argchk
161 # Only the first two values need checking, we have already checked the third
162 testlist.append(args[0])
163 testlist.append(args[1])
164 if args[0]==args[1]:
165 abort("No point in converting to the same scale")
166 x = 0
167 for item in testlist:
168 if item in argchk:
169 x = x + 1
170 if x != 2:
171 abort("Invalid Set of Arguments")
172#######################
173def processargs(args):
174 if args[0] == '-c':
175 if args[1] == '-f':
176 temp,f,t = c2f(args[2])
177 else:
178 temp,f,t = c2k(args[2])
179 elif args[0] == '-f':
180 if args[1] == '-k':
181 temp,f,t = f2k(args[2])
182 else:
183 temp,f,t = f2c(args[2])
184 elif args[0] == '-k':
185 if args[1] == '-c':
186 temp,f,t = k2c(args[2])
187 else:
188 temp,f,t = k2f(args[2])
189 else:
190 abort("Ooops, something has gone wrong")
191 print str(args[2]) + ' ' + f + ' equals ' + str(temp) + " " + t
192#######################
193# Main Program Loop
194args = getargs() #Get arguments from the command line
195args=lowerargs(args) #Convert args to lowercase
196args = validtest1(args)
197#The program will abort from inside validtest2 if failed validation means it cannot proceed.
198validtest2(args)
199#Program will resume here if validtest2 passed.
200processargs(args)
201#######################