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
  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
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/python
#######################
# Python Temperature Converter - Author: Matthew Tunstall 2006
# -------------------------------------------------------------------------------
# Aim: To take a command line temperature value and convert it to another scale
# Usage: pytemp -c -f 24  #Convert 24 degrees C to F
#######################
# Copyright (C) 2006  Matthew Tunstall
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#######################
usage ="""
PyTemp - Python Temperature Converter
-------------------------------------
USAGE: pytemp.py [from units] [to units] [value]

Units:
------
-c or -C = Celsius
-f or -F = Fahrenheit
-k or -K = Kelvin

Example:
--------
To convert 20 Celsius to Fahrenheit
pytemp.py -c -f 20
"""
#######################
# Revision History
# ----------------
# 0.4 - Release Date [10/9/2006]
# 0.4 - Rewrite of codebase to clean and optimise
# 0.3 - Working bug free code
# 0.2 - Working code with some error checking
# 0.1 - Initial Code Outline
#######################
bugemail = "bugs@deltanova.co.uk            # Email address for bug reports
import sys                                                          # To gain access to the argv command to access command line args.
error="""
Something unexpected has occured for this message to be displayed.
Please submit a bug report to: """ + bugemail +"\n"
#######################
def abort(x):                                   # Abort routine to exit the program when error detected
    print usage
    print x
    print "Application Terminating"
    sys.exit()
#######################
# Calculation Definitions
#######################
def f2c(temp):
    fromScale = "Fahrenheit"
    toScale = "Celsius"
    temp = temp - 32
    x = 5.0/9.0
    temp = temp*x
    return temp, fromScale, toScale
#######################
def c2f(temp):
    fromScale = "Celsius"
    toScale = "Fahrenheit"
    x = 9.0/5.0
    temp = temp*x
    temp = temp + 32
    return temp, fromScale, toScale
#######################
def f2k(temp):
    fromScale = "Fahrenheit"
    toScale = "Kelvin"
    x,y,z = f2c(temp) # y & z take the fromScale and toScale values returned by f2c()
    x = x + 273.15
    return x, fromScale, toScale
#######################
def c2k(temp):
    fromScale = "Celsius"
    toScale = "Kelvin"
    temp = temp + 273.15
    return temp, fromScale, toScale
#######################
def k2c(temp):
    fromScale = "Kelvin"
    toScale = "Celsius"
    temp = temp - 273.15
    return temp, fromScale, toScale
#######################
def k2f(temp):
    fromScale = "Kelvin"
    toScale = "Fahrenheit"
    x,w,v = k2c(temp) #w & v take the fromScale and toScale values returned by k2c()
    print x
    y = 9.0/5.0
    temp = (x*y)+32
    return temp, fromScale, toScale
########################
# Conversion Calculations                                                       #
# ------------------------------------------------------------------------------#
# From                              To Fahrenheit                   To Celsius              To Kelvin           #
# Fahrenheit(F)             F                                               (F-32)*(5/9)    (F-32)*(5/9)+273.15 #
# Celsius(C)                (C*(9/5))+32                    C                               C+273.15            #
# Kelvin(K)                 (K-273.15)*(9/5)+32             K-273.15                K                   #
########################
def getargs():
    args = sys.argv[1:]                                             # Get command line args but strip the first arg(the file name).
    argtest = []                                                    # An empty list. Compare with args to check for command line arguments
    if (args == argtest):
        abort("No arguments supplied")
    else:                                                                   # If Arguments found
        numargs = len(args)
        if (numargs == 3):
            x = args
        elif (numargs < 3):
            abort("Too few arguments supplied.")
        elif (numargs > 3):
            abort("Too many arguments supplied.")
        else:
            abort(error)
    return x
#######################
def lowerargs(args): # Take the arguments and convert to lowercase to aid processing.
    b = []
    for x in args:
        x = x.lower()
        b.append(x)
    return b
#######################
def tofloat(x):                             # Attempt to convert the input to a float
    try:
        x = float(x)
    except ValueError:
        y = 0
    else:
        y = 1
    return y
#######################
def validtest1(args):
    pos =[]                                         # Position log, allows us to see which arg(s) validated
    for a in args:
        pos.append(tofloat(a))
    poschk = [0,0,1]        # Indicates the ideal result, only the last arg will convert to a float
    if poschk == pos:
        args[2]=float(args[2]) # Make float conversion on last arg permanent
    else:
        abort("Invalid Set of Arguments")
    return args
#######################
def validtest2(args):
    argchk = ["-c","-f","-k"]
    testlist = [] # Create a test list to compare with argchk
        # Only the first two values need checking, we have already checked the third
        testlist.append(args[0])
    testlist.append(args[1])
    if args[0]==args[1]:
        abort("No point in converting to the same scale")
    x = 0
    for item in testlist:
        if item in argchk:
            x = x + 1
    if x != 2:
        abort("Invalid Set of Arguments")
#######################
def processargs(args):
    if args[0] == '-c':
        if args[1] == '-f':
            temp,f,t = c2f(args[2])
        else:
            temp,f,t = c2k(args[2])
    elif args[0] == '-f':
        if args[1] == '-k':
            temp,f,t = f2k(args[2])
        else:
            temp,f,t = f2c(args[2])
    elif args[0] == '-k':
        if args[1] == '-c':
            temp,f,t = k2c(args[2])
        else:
            temp,f,t = k2f(args[2])
    else:
        abort("Ooops, something has gone wrong")
    print str(args[2]) + ' ' + f + ' equals ' + str(temp) + " " + t
#######################
# Main Program Loop
args = getargs()                            #Get arguments from the command line
args=lowerargs(args)                        #Convert args to lowercase
args = validtest1(args)
#The program will abort from inside validtest2 if failed validation means it cannot proceed.
validtest2(args)
#Program will resume here if validtest2 passed.
processargs(args)
#######################