22
33from string import digits , ascii_uppercase
44
5- from six .moves import input
6-
75MINUS_SIGN = '-'
86ZERO = '0'
97
@@ -43,7 +41,7 @@ class InvalidInternalValueError(BaseConvError):
4341 _value = 'Invalid Internal Value Error'
4442
4543
46- def _ValidNum (value ):
44+ def _valid_num (value ):
4745 '''Return a string representing the number given validated to
4846 standard notation:
4947 no leading zeros (with optional minus sign)
@@ -79,7 +77,7 @@ def _ValidNum(value):
7977 return ZERO
8078
8179
82- def _ValidBas (value ):
80+ def _valid_bas (value ):
8381 '''Returns an int with a value of 2 or above or
8482 raises InvalidBaseError exception.
8583 '''
@@ -90,62 +88,62 @@ def _ValidBas(value):
9088
9189 try :
9290 # nominal case
93- intValue = int (value )
91+ int_value = int (value )
9492 except ValueError :
9593 raise InvalidBaseError
9694 else :
97- if MINIMUM_BASE <= intValue :
95+ if MINIMUM_BASE <= int_value :
9896 # nominal case
99- return intValue
97+ return int_value
10098 else :
10199 raise InvalidBaseError
102100
103101
104- def _ValToChar ( inputNum ):
102+ def _val_to_char ( input_num ):
105103 try :
106- inputNum = int (inputNum )
104+ input_num = int (input_num )
107105 except ValueError :
108106 raise InvalidInternalValueError
109107
110108 # Negative indexes count backwards from the end of the list
111- if inputNum < 0 :
109+ if input_num < 0 :
112110 raise InvalidInternalValueError
113111
114112 try :
115- return ALLOWED_SYMBOLS [inputNum ]
113+ return ALLOWED_SYMBOLS [input_num ]
116114 except IndexError :
117115 raise InvalidInternalValueError
118116
119117
120- def IntoDec ( inNum , inBas ):
118+ def into_dec ( in_num , in_bas ):
121119 '''Returns an int.
122120 '''
123- inNum = _ValidNum ( inNum )
124- inBas = _ValidBas ( inBas )
121+ in_num = _valid_num ( in_num )
122+ in_bas = _valid_bas ( in_bas )
125123 try :
126- return int (inNum , inBas )
124+ return int (in_num , in_bas )
127125 except ValueError :
128126 raise InvalidInputBaseError
129127
130128
131- def FromDec ( inNum , outBas ):
129+ def from_dec ( in_num , out_bas ):
132130 '''Is an error for inNum to not be an integer.
133131 '''
134132 try :
135- inNum = int (_ValidNum ( inNum ))
133+ in_num = int (_valid_num ( in_num ))
136134 except ValueError :
137135 raise InvalidNumberError
138136
139- outBas = _ValidBas ( outBas )
137+ out_bas = _valid_bas ( out_bas )
140138
141- minused = inNum < 0
139+ minused = in_num < 0
142140 if minused :
143- inNum *= - 1
141+ in_num *= - 1
144142
145143 values = []
146- while (0 < inNum ):
147- values .append (_ValToChar ( inNum % outBas ))
148- inNum //= outBas
144+ while (0 < in_num ):
145+ values .append (_val_to_char ( in_num % out_bas ))
146+ in_num //= out_bas
149147
150148 value = '' .join (reversed (values ))
151149
@@ -155,48 +153,44 @@ def FromDec(inNum, outBas):
155153 return ZERO
156154
157155
158- def BasCalc ( inNum , inBas = DEFAULT_BASE , outBas = DEFAULT_BASE ):
156+ def bas_calc ( in_num , in_bas = DEFAULT_BASE , out_bas = DEFAULT_BASE ):
159157 '''Given a number and its current base, returns the number with a
160158 new specified base.
161159 If a base is not given it is assumed to be base %d.
162160 ''' % (DEFAULT_BASE )
163- return FromDec ( IntoDec ( inNum , inBas ), outBas )
161+ return from_dec ( into_dec ( in_num , in_bas ), out_bas )
164162
165163
166- def _CommandLine (args ):
164+ def _command_line (args ):
167165 if 1 < len (args ):
168166 for arg in args [1 :]:
169167 if arg in ('-h' , '--help' ):
170168 print ('This program converts integers which may be signed ' +
171169 'between any two number bases %d and over.\n ' +
172170 'Inputs as follows:\n ' +
173- 'inNum = the Input Number\n ' +
174- 'inBas = the Input Base\n ' +
175- 'outBas = the Output Base' % (MINIMUM_BASE ))
176- break
177- elif arg in ('-t' , '--test' ):
178- import test
179- test .RunTests ()
171+ 'in_num = the Input Number\n ' +
172+ 'in_bas = the Input Base\n ' +
173+ 'out_bas = the Output Base' % (MINIMUM_BASE ))
180174 break
181175 else :
182- print (BasCalc (* args [1 :]))
176+ print (bas_calc (* args [1 :]))
183177 else :
184178 print ('Base Converter' )
185- exitVals = ('q' , 'quit' )
179+ exit_vals = ('q' , 'quit' )
180+ exit_prompt = '\n Enter any of the following values to exit: %s\n or press return to continue: ' % str (exit_vals )
186181 while True :
187182 try :
188183 print ('Output Number: ' +
189- BasCalc (input ('\n Enter an Input Number: ' ).strip (),
190- input ('Enter an Input Base: ' ).strip (),
191- input ('Enter an Output Base: ' ).strip ()))
184+ bas_calc (
185+ input ('\n Enter an Input Number: ' ).strip (),
186+ input ('Enter an Input Base: ' ).strip (),
187+ input ('Enter an Output Base: ' ).strip ()))
192188 except (BaseConvError , ValueError ) as e :
193189 print ('Error: ' , e )
194- if input ('\n Enter any of the following values to exit: %s\n ' +
195- 'or press return to continue: ' %
196- (str (exitVals ))).strip ().lower () in exitVals :
190+ if input (exit_prompt ).strip ().lower () in exit_vals :
197191 break
198192
199193
200194if __name__ == "__main__" :
201195 from sys import argv
202- _CommandLine (argv )
196+ _command_line (argv )
0 commit comments