Skip to content

Commit

Permalink
pep8-naming and mypy checks (and drop python2 support)
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronRobson committed May 28, 2021
1 parent 102b026 commit cabbef3
Show file tree
Hide file tree
Showing 10 changed files with 197 additions and 179 deletions.
19 changes: 10 additions & 9 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
# Download and cache dependencies
- restore_cache:
keys:
- v1-dependencies-{{ checksum "requirements.txt" }}
- v1-dependencies-{{ checksum "requirements.txt" }}-{{ checksum "dev-requirements.txt"}}-{{ checksum "tests/requirements.txt"}}
# fallback to using the latest cache if no exact match is found
- v1-dependencies-

Expand All @@ -25,23 +25,24 @@ jobs:
command: |
python3 -m venv venv
. venv/bin/activate
pip install -r requirements.txt
make install-packages
- save_cache:
paths:
- ./venv
key: v1-dependencies-{{ checksum "requirements.txt" }}
key: v1-dependencies-{{ checksum "requirements.txt" }}-{{ checksum "dev-requirements.txt"}}-{{ checksum "tests/requirements.txt"}}

- run:
name: run checks
command: |
. venv/bin/activate
make check
# run tests!
# this example uses Django's built-in test-runner
# other common Python testing frameworks include pytest and nose
# https://pytest.org
# https://nose.readthedocs.io
- run:
name: run tests
command: |
. venv/bin/activate
python -m unittest discover
make test
- store_artifacts:
path: test-reports
Expand Down
8 changes: 3 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ language: python

matrix:
include:
- python: "2.7"
- python: pypy
- python: "3.5"
- python: "3.6"
- python: "3.7"
Expand All @@ -18,14 +16,14 @@ matrix:
allow_failures:
- python: "3.10"
- python: nightly
- python: pypy3

install:
- pip install -r requirements.txt
- pip install flake8
- make install-packages
- pip install python-coveralls

script:
- flake8 .
- make check
- nosetests
- coverage run --source=. -m unittest discover

Expand Down
20 changes: 18 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,26 @@
.PHONY: all
all: check test

install-packages:
pip3 install --upgrade \
-r dev-requirements.txt \
-r requirements.txt \
-r tests/requirements.txt

.PHONY: check
check:
check: lint typecheck

.PHONY: lint
lint:
flake8 .

.PHONY: typecheck
typecheck:
mypy .

.PHONY: test
test:
test: unittest

.PHONY: unittest
unittest:
python3 -m unittest
78 changes: 36 additions & 42 deletions baseconv.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

from string import digits, ascii_uppercase

from six.moves import input

MINUS_SIGN = '-'
ZERO = '0'

Expand Down Expand Up @@ -43,7 +41,7 @@ class InvalidInternalValueError(BaseConvError):
_value = 'Invalid Internal Value Error'


def _ValidNum(value):
def _valid_num(value):
'''Return a string representing the number given validated to
standard notation:
no leading zeros (with optional minus sign)
Expand Down Expand Up @@ -79,7 +77,7 @@ def _ValidNum(value):
return ZERO


def _ValidBas(value):
def _valid_bas(value):
'''Returns an int with a value of 2 or above or
raises InvalidBaseError exception.
'''
Expand All @@ -90,62 +88,62 @@ def _ValidBas(value):

try:
# nominal case
intValue = int(value)
int_value = int(value)
except ValueError:
raise InvalidBaseError
else:
if MINIMUM_BASE <= intValue:
if MINIMUM_BASE <= int_value:
# nominal case
return intValue
return int_value
else:
raise InvalidBaseError


def _ValToChar(inputNum):
def _val_to_char(input_num):
try:
inputNum = int(inputNum)
input_num = int(input_num)
except ValueError:
raise InvalidInternalValueError

# Negative indexes count backwards from the end of the list
if inputNum < 0:
if input_num < 0:
raise InvalidInternalValueError

try:
return ALLOWED_SYMBOLS[inputNum]
return ALLOWED_SYMBOLS[input_num]
except IndexError:
raise InvalidInternalValueError


def IntoDec(inNum, inBas):
def into_dec(in_num, in_bas):
'''Returns an int.
'''
inNum = _ValidNum(inNum)
inBas = _ValidBas(inBas)
in_num = _valid_num(in_num)
in_bas = _valid_bas(in_bas)
try:
return int(inNum, inBas)
return int(in_num, in_bas)
except ValueError:
raise InvalidInputBaseError


def FromDec(inNum, outBas):
def from_dec(in_num, out_bas):
'''Is an error for inNum to not be an integer.
'''
try:
inNum = int(_ValidNum(inNum))
in_num = int(_valid_num(in_num))
except ValueError:
raise InvalidNumberError

outBas = _ValidBas(outBas)
out_bas = _valid_bas(out_bas)

minused = inNum < 0
minused = in_num < 0
if minused:
inNum *= -1
in_num *= -1

values = []
while (0 < inNum):
values.append(_ValToChar(inNum % outBas))
inNum //= outBas
while (0 < in_num):
values.append(_val_to_char(in_num % out_bas))
in_num //= out_bas

value = ''.join(reversed(values))

Expand All @@ -155,48 +153,44 @@ def FromDec(inNum, outBas):
return ZERO


def BasCalc(inNum, inBas=DEFAULT_BASE, outBas=DEFAULT_BASE):
def bas_calc(in_num, in_bas=DEFAULT_BASE, out_bas=DEFAULT_BASE):
'''Given a number and its current base, returns the number with a
new specified base.
If a base is not given it is assumed to be base %d.
''' % (DEFAULT_BASE)
return FromDec(IntoDec(inNum, inBas), outBas)
return from_dec(into_dec(in_num, in_bas), out_bas)


def _CommandLine(args):
def _command_line(args):
if 1 < len(args):
for arg in args[1:]:
if arg in ('-h', '--help'):
print('This program converts integers which may be signed ' +
'between any two number bases %d and over.\n' +
'Inputs as follows:\n' +
'inNum = the Input Number\n' +
'inBas = the Input Base\n' +
'outBas = the Output Base' % (MINIMUM_BASE))
break
elif arg in ('-t', '--test'):
import test
test.RunTests()
'in_num = the Input Number\n' +
'in_bas = the Input Base\n' +
'out_bas = the Output Base' % (MINIMUM_BASE))
break
else:
print(BasCalc(*args[1:]))
print(bas_calc(*args[1:]))
else:
print('Base Converter')
exitVals = ('q', 'quit')
exit_vals = ('q', 'quit')
exit_prompt = '\nEnter any of the following values to exit: %s\nor press return to continue: ' % str(exit_vals)
while True:
try:
print('Output Number: ' +
BasCalc(input('\nEnter an Input Number: ').strip(),
input('Enter an Input Base: ').strip(),
input('Enter an Output Base: ').strip()))
bas_calc(
input('\nEnter an Input Number: ').strip(),
input('Enter an Input Base: ').strip(),
input('Enter an Output Base: ').strip()))
except (BaseConvError, ValueError) as e:
print('Error: ', e)
if input('\nEnter any of the following values to exit: %s\n' +
'or press return to continue: ' %
(str(exitVals))).strip().lower() in exitVals:
if input(exit_prompt).strip().lower() in exit_vals:
break


if __name__ == "__main__":
from sys import argv
_CommandLine(argv)
_command_line(argv)
Loading

0 comments on commit cabbef3

Please sign in to comment.