Skip to content

Commit cabbef3

Browse files
committed
pep8-naming and mypy checks (and drop python2 support)
1 parent 102b026 commit cabbef3

File tree

10 files changed

+197
-179
lines changed

10 files changed

+197
-179
lines changed

.circleci/config.yml

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
# Download and cache dependencies
1717
- restore_cache:
1818
keys:
19-
- v1-dependencies-{{ checksum "requirements.txt" }}
19+
- v1-dependencies-{{ checksum "requirements.txt" }}-{{ checksum "dev-requirements.txt"}}-{{ checksum "tests/requirements.txt"}}
2020
# fallback to using the latest cache if no exact match is found
2121
- v1-dependencies-
2222

@@ -25,23 +25,24 @@ jobs:
2525
command: |
2626
python3 -m venv venv
2727
. venv/bin/activate
28-
pip install -r requirements.txt
28+
make install-packages
2929
3030
- save_cache:
3131
paths:
3232
- ./venv
33-
key: v1-dependencies-{{ checksum "requirements.txt" }}
33+
key: v1-dependencies-{{ checksum "requirements.txt" }}-{{ checksum "dev-requirements.txt"}}-{{ checksum "tests/requirements.txt"}}
34+
35+
- run:
36+
name: run checks
37+
command: |
38+
. venv/bin/activate
39+
make check
3440
35-
# run tests!
36-
# this example uses Django's built-in test-runner
37-
# other common Python testing frameworks include pytest and nose
38-
# https://pytest.org
39-
# https://nose.readthedocs.io
4041
- run:
4142
name: run tests
4243
command: |
4344
. venv/bin/activate
44-
python -m unittest discover
45+
make test
4546
4647
- store_artifacts:
4748
path: test-reports

.travis.yml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ language: python
55

66
matrix:
77
include:
8-
- python: "2.7"
9-
- python: pypy
108
- python: "3.5"
119
- python: "3.6"
1210
- python: "3.7"
@@ -18,14 +16,14 @@ matrix:
1816
allow_failures:
1917
- python: "3.10"
2018
- python: nightly
19+
- python: pypy3
2120

2221
install:
23-
- pip install -r requirements.txt
24-
- pip install flake8
22+
- make install-packages
2523
- pip install python-coveralls
2624

2725
script:
28-
- flake8 .
26+
- make check
2927
- nosetests
3028
- coverage run --source=. -m unittest discover
3129

Makefile

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,26 @@
33
.PHONY: all
44
all: check test
55

6+
install-packages:
7+
pip3 install --upgrade \
8+
-r dev-requirements.txt \
9+
-r requirements.txt \
10+
-r tests/requirements.txt
11+
612
.PHONY: check
7-
check:
13+
check: lint typecheck
14+
15+
.PHONY: lint
16+
lint:
817
flake8 .
918

19+
.PHONY: typecheck
20+
typecheck:
21+
mypy .
22+
1023
.PHONY: test
11-
test:
24+
test: unittest
25+
26+
.PHONY: unittest
27+
unittest:
1228
python3 -m unittest

baseconv.py

Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
from string import digits, ascii_uppercase
44

5-
from six.moves import input
6-
75
MINUS_SIGN = '-'
86
ZERO = '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 = '\nEnter any of the following values to exit: %s\nor press return to continue: ' % str(exit_vals)
186181
while True:
187182
try:
188183
print('Output Number: ' +
189-
BasCalc(input('\nEnter an Input Number: ').strip(),
190-
input('Enter an Input Base: ').strip(),
191-
input('Enter an Output Base: ').strip()))
184+
bas_calc(
185+
input('\nEnter 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('\nEnter 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

200194
if __name__ == "__main__":
201195
from sys import argv
202-
_CommandLine(argv)
196+
_command_line(argv)

0 commit comments

Comments
 (0)