Skip to content
This repository was archived by the owner on Aug 26, 2021. It is now read-only.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# 220-Advanced-Template
## Not for class use - Fork me!
# Python 220: online cohort April 2019
## Class repo for grading

This is the master repo from which an instructor will use a fork to create their class assignment repo.
Links for setting up git for this class:

PLEASE do not use this for your students' assignments.
- https://uwpce-pythoncert.github.io/PythonCertDevel220/modules/lesson00/git_setup.html
- https://uwpce-pythoncert.github.io/PythonCertDevel220/modules/lesson00/git_workflow.html
1 change: 1 addition & 0 deletions students/AlexW/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions students/jesse_miller/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
New dir
Empty file.
32 changes: 32 additions & 0 deletions students/jesse_miller/lesson01/activity/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Calculator

## Instructions

Your assignment is to complete testing and linting on the calculator from the lesson videos.

There's one new addition since the videos: I've separated the unit tests and the integration tests into two separate test files.

## Your Goals

1. `python -m unittest integration-test` should have no failures. Don't edit integration-test.py, edit your code to make it pass.
2. Add unit tests to unit-test.py such that `coverage run --source=calculator -m unittest unit-test; coverage report` shows 100% coverage.
3. All of the tests in unit-test.py should pass.
4. Satisfy the linter such that `pylint calculator` gives no errors and `flake8 calculator` gives no errors. See (PEP257)[https://www.python.org/dev/peps/pep-0257/] if you'd like more information about docstrings. There are quite a few docstrings to add, but for this exercise you don't need to get too creative: """ this method adds two numbers """ is sufficient.

## Bonus goal
One of our specs for calculator says the following:

```
The add, subtract, multiply, and divide methods shall both:
Return the result of the operation
Enter the result of the operation back into the calculator
This makes it possible to perform the following sequences of operations:
calculator.enter_number(2)
calculator.enter_number(3)
calculator.add() # Returns 5, and now 5 is now 'in the calculator'
calculator.enter_number(1)
calculator.subtract() # Returns 4 because 5 - 1 = 4
```

This feature is tested by our integration test, but it is not tested in our unit tests. Because this sequencing of operations is a defined feature of calculator, it would definitely be appropriate to test in the unit-test.CalculatorTests. Your bonus goal is to use *MagicMock* to test this method sequencing feature in isolation. Ie: without relying on the correctness of any particular operator we use to initialize our calculator.

18 changes: 18 additions & 0 deletions students/jesse_miller/lesson01/activity/calculator/adder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env python3
#pylint: disable=R0903
'''
Addition module for calculator
'''


class Adder():
'''
Addition class for a more complex calculator
'''

@staticmethod
def calc(operand_1, operand_2):
'''
This does the heavy lifting for adding
'''
return operand_1 + operand_2
62 changes: 62 additions & 0 deletions students/jesse_miller/lesson01/activity/calculator/calculator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/usr/bin/env python3
'''
Our calculator
'''
from exceptions import InsufficientOperands


class Calculator():
'''
Class base for the calculator
'''

def __init__(self, adder, subtracter, multiplier, divider):
self.adder = adder
self.subtracter = subtracter
self.multiplier = multiplier
self.divider = divider

self.stack = []

def enter_number(self, number):
'''
What it says on the tin. Enter a number.
'''
self.stack.insert(0, number)

def _do_calc(self, operator):
'''
Shortcut to operations on numbers. Keeps from having to repeat whole
functions.
'''
try:
result = operator.calc(self.stack[1], self.stack[0])
except IndexError:
raise InsufficientOperands

self.stack = [result]
return result

def add(self):
'''
Addition method
'''
return self._do_calc(self.adder)

def subtract(self):
'''
Subtraction method
'''
return self._do_calc(self.subtracter)

def multiply(self):
'''
Multiplication method
'''
return self._do_calc(self.multiplier)

def divide(self):
'''
Division method
'''
return self._do_calc(self.divider)
16 changes: 16 additions & 0 deletions students/jesse_miller/lesson01/activity/calculator/divider.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env python3
'''
Division module for calculator
'''
#pylint: disable=R0903
class Divider():
'''
Division class for a more complex calculator
'''

@staticmethod
def calc(operand_1, operand_2):
'''
This does the heavy lifting for division
'''
return operand_1 / operand_2
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env python3
class InsufficientOperands(Exception):
pass
16 changes: 16 additions & 0 deletions students/jesse_miller/lesson01/activity/calculator/multiplier.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env python3
'''
Multiplication module for calculator
'''
#pylint: disable=R0903
class Multiplier():
'''
Multiplying class for a more complex calculator
'''

@staticmethod
def calc(operand_1, operand_2):
'''
This does the heavy lifting for multiplying
'''
return operand_1 * operand_2
14 changes: 14 additions & 0 deletions students/jesse_miller/lesson01/activity/calculator/squarer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env python3
class Squarer(object):
'''
A simple squaring function
'''

@staticmethod
def calc(operand):
'''
calculating the square
'''
#return operand**2 # OLD
#return operand**operand # BAD
return operand*operand # This should work
16 changes: 16 additions & 0 deletions students/jesse_miller/lesson01/activity/calculator/subtracter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env python3
'''
Subtraction module for calculator
'''
#pylint: disable=R0903
class Subtracter():
'''
Addition class for a more complex calculator
'''

@staticmethod
def calc(operand_1, operand_2):
'''
This does the heavy lifting for subtracting
'''
return operand_1 - operand_2
109 changes: 109 additions & 0 deletions students/jesse_miller/lesson01/activity/calculator/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#!/usr/bin/env python3
#pylint: disable-all
from unittest import TestCase
from unittest.mock import MagicMock

from adder import Adder
from subtracter import Subtracter
from multiplier import Multiplier
from divider import Divider
from calculator import Calculator
from exceptions import InsufficientOperands

class AdderTests(TestCase):
def test_adding(self):
adder = Adder()
for i in range(-10, 10):
for j in range(-10, 10):
self.assertEqual(i + j, adder.calc(i, j))


class SubtractorTests(TestCase):
def test_subtracting(self):
subtracter = Subtracter()
for i in range(-10, 10):
for j in range(-10, 10):
self.assertEqual(i - j, subtracter.calc(i, j))


class MultiplierTests(TestCase):
def test_multiplying(self):
multiplier = Multiplier()
for i in range(-10, 10):
for j in range(-10, 10):
self.assertEqual(i * j, multiplier.calc(i, j))


class DividerTests(TestCase):
def test_divider(self):
divider = Divider()
try:
for i in range(-10, 10):
for j in range(-10, 10):
self.assertEqual(i / j, divider.calc(i, j))
except ZeroDivisionError:
return 0


class CalculatorTests(TestCase):

def setUp(self):
self.adder = Adder()
self.subtracter = Subtracter()
self.multiplier = Multiplier()
self.divider = Divider()

self.calculator = Calculator(self.adder, self.subtracter, \
self.multiplier, self.divider)


def test_insufficient_operands(self):
self.calculator.enter_number(0)

with self.assertRaises(InsufficientOperands):
self.calculator.add()


def test_adder_call(self):
self.adder.calc = MagicMock(return_value=0)

self.calculator.enter_number(1)
self.calculator.enter_number(2)
self.calculator.add()

self.adder.calc.assert_called_with(1, 2)


def test_subtracter_call(self):
self.subtracter.calc = MagicMock(return_value=0)

self.calculator.enter_number(1)
self.calculator.enter_number(2)
self.calculator.subtract()

self.subtracter.calc.assert_called_with(1, 2)


class ModuleTests(TestCase):

def test_module(self):
calculator = Calculator(Adder(), Subtracter(), Multiplier(), Divider())

calculator.enter_number(5)
calculator.enter_number(2)

calculator.multiply()

calculator.enter_number(46)

calculator.add()

calculator.enter_number(8)

calculator.divide()

calculator.enter_number(1)

result = calculator.subtract()

self.assertEqual(6, result)
17 changes: 17 additions & 0 deletions students/jesse_miller/lesson01/activity/squarer/squarer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env python3
'''
Squaring module for our calculator
'''
class Squarer():
'''
A simple squaring function
'''

@staticmethod
def calc(operand):
'''
calculating the square
'''
#return operand**2 # OLD
#return operand**operand # BAD
return operand*operand # This should work
41 changes: 41 additions & 0 deletions students/jesse_miller/lesson01/activity/squarer/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# test.py
from squarer import Squarer

class SquarerTest(object):

@staticmethod
def test_positive_numbers():

squares # {
1: 1,
2: 4,
3: 9,
12: 144,
100: 10000,
}

for num, square in squares.items():
result # Squarer.calc(num)

if result !# square:
print("Squared {} and got {} but expected {}".format(num, result, square))
@staticmethod
def test_negative_numbers():

squares # {
-1: 1,
-2: 4,
-3: 9,
-12: 144,
-100: 10000,
}

for num, square in squares.items():
result # Squarer.calc(num)

if result !# square:
print("Squared {} and got {} but expected {}".format(num, result, square))

if __name__ ## "__main__":
SquarerTest.test_positive_numbers()
SquarerTest.test_negative_numbers()
Loading