Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding circle and lambda labs #104

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions students/JohnRudolph/session8/circle_class.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/usr/bin/env python
from math import pi

######################################################
############## CIRCLE CLASS EXERCISE ##############
######################################################

class Circle(object):
'''
Defines a circle object which takes
A radius or diameter as an input
And performs some operations
'''

#init takes radius or diameter as an input
def __init__(self, **kwargs):
if [x for x in kwargs if x not in ['radius', 'diameter']]:
raise AttributeError('radius or diameter is required')
#if radius then set radius
if 'radius' in kwargs:
self._radius = kwargs['radius']
#if diameter then set radius
if'diameter' in kwargs:
self._radius = kwargs['diameter']/2

@property
def radius(self):
return self._radius

@property
def diameter(self):
return self.radius*2

@property
def area(self):
return self._radius**2*pi

@radius.setter
def radius(self, radius):
self._radius = radius

@diameter.setter
def diameter(self, diameter):
self._radius = diameter/2

def __repr__(self):
return 'Circle({})'.format(self.radius)

def __str__(self):
return 'Circle with radius: {}'.format(self.radius)
#set __add__ method to create new circle class with added radius values
def __add__(self, other):
add_radius= self.radius + other.radius
return Circle(radius= add_radius)
#set __mul__ method to create new circle class with multiplied radius
def __mul__(self, num):
return Circle(radius=(self.radius*num))
#set comparison operators
def __gt__(self, other):
return self.radius > other.radius

def __lt__(self, other):
return self.radius < other.radius

def __eq__(self, other):
return self.radius == other.radius
139 changes: 139 additions & 0 deletions students/JohnRudolph/session8/test_circle_class.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import circle_class as cc
import pytest
from math import pi

def test_radius():
'''
test that radius can be set on init
and called via get property
'''
radius = 4
c = cc.Circle(radius=radius)
assert radius == c.radius

def test_diameter():
'''
test that diameter is set when radius is provided
'''
radius = 4
c = cc.Circle(radius=radius)
assert radius*2 == c.diameter

def test_set_diameter():
'''
test that a new diameter can be set based on set property
test that radius is reset when diameter is set
'''
radius = 4
c = cc.Circle(radius=radius)
new_diameter = 2
c.diameter = new_diameter
assert c.diameter == new_diameter
assert c.radius == 1

def test_area():
'''
test that an area attribute is set when Circle is init
'''
radius = 4
c = cc.Circle(radius=radius)
area = (c.radius**2*pi)
assert c.area == area

def test_area_error():
'''
Check if setting area raises an Attribute Error
'''
radius = 4
c = cc.Circle(radius=radius)
with pytest.raises(AttributeError):
c.area = 2

def test_alternate_constructor():
'''
Check if setting area raises an Attribute Error
'''
diameter = 8
c = cc.Circle(diameter = diameter)
assert c.radius == (diameter/2)

def test_str_method():
'''
Check if __str__ method works as intended
'''
radius = 4
c = cc.Circle(radius = radius)
s = 'Circle with radius: {}'.format(radius)
assert s == str(c)

def test_repr_method():
'''
Check if __str__ method works as intended
'''
radius = 4
c = cc.Circle(radius=radius)
s = 'Circle({})'.format(radius)
d = repr(c)
assert s == d

def test_add_method():
'''
Check if 2 circles can be added
'''
radius = 4
diameter = 10
c1 = cc.Circle(radius = radius)
c2 = cc.Circle(diameter= diameter)
c3 = c1 + c2
assert c3.radius == radius + diameter/2

def test_mult_method():
'''
Check if 2 circles can be added
'''
radius = 4
num = 3
c1 = cc.Circle(radius = radius)
c2 = c1*num
assert c2.radius == radius*num

def test_gt_method():
'''
Check if circle1 radius greater than circle2 radius
'''
radius1 = 4
radius2 = 6
c1 = cc.Circle(radius=radius1)
c2 = cc.Circle(radius=radius2)
assert (c2 > c1) == True

def test_lt_method():
'''
Check if circle1 radius less than circle2 radius
'''
radius1 = 4
radius2 = 6
c1 = cc.Circle(radius=radius1)
c2 = cc.Circle(radius=radius2)
assert (c1 < c2) == True

def test_eq_method():
'''
Check if circle1 radius less than circle2 radius
'''
radius1 = 6
radius2 = 6
c1 = cc.Circle(radius=radius1)
c2 = cc.Circle(radius=radius2)
assert (c1 == c2) == True

def test_sort_method():
'''
Test of a list of circle objects is sortable
'''
c1, c2, c3 = cc.Circle(radius=4), cc.Circle(radius=7), cc.Circle(radius=2)
circles = [c1, c2, c3]
sort_circles = [c3, c1, c2]
for i,n in zip(sorted(circles), sort_circles):
assert i.radius == n.radius

Loading