Re: [Edu-sig] a Circle type with radius, area both properties (fixed)
Uh oh... I'm in the middle of teaching Session 10 and couldn't figure out why my radius for a smaller area was bigger. Couldn't be. I did my algebra wrong, duh. Here's the debugged version: # -*- coding: utf-8 -*- """ Created on Thu Oct 20 15:43:14 2016 @author: Kirby Urner toggle the import model_property on and off to see the example works the same either way. model_property contains a pure Python emulator of the built in property type. Related reading: https://mail.python.org/pipermail/edu-sig/2016-October/011548.html """ from model_property import Property as property import math class Circle: """setting either the radius or area attribute sets the other as a dependent value. Initialized with radius only, unit circle by default. """ def __init__(self, radius = 1): self.radius = radius @property def area(self): return self._area @property def radius(self): return self._radius @area.setter def area(self, value): self._area = value self._radius = math.sqrt(self._area / math.pi) #duh @radius.setter def radius(self, value): self._radius = value self._area = math.pi * (self._radius ** 2) def __repr__(self): return "Circle(radius = {})".format(self.radius) the_circle = Circle(5) print("the_circle:", the_circle) print("Area: ", the_circle.area) the_circle.area = 50 print("Radius when Area=50:", the_circle.radius)
I swap in the pure Python emulator for the property type as a part of explaining how these decorators work: On Thu, Oct 20, 2016 at 7:45 PM, kirby urner <kirby.urner@gmail.com> wrote:
from model_property import Property as property
# -*- coding: utf-8 -*- """ Created on Thu Oct 20 15:45:20 2016 @author: Python.org Emulates the built-in property type in pure Python """ class Property(object): "Emulate PyProperty_Type() in Objects/descrobject.c" def __init__(self, fget=None, fset=None, fdel=None, doc=None): self.fget = fget self.fset = fset self.fdel = fdel if doc is None and fget is not None: doc = fget.__doc__ self.__doc__ = doc def __get__(self, obj, objtype=None): if obj is None: return self if self.fget is None: raise AttributeError("unreadable attribute") return self.fget(obj) def __set__(self, obj, value): if self.fset is None: raise AttributeError("can't set attribute") self.fset(obj, value) def __delete__(self, obj): if self.fdel is None: raise AttributeError("can't delete attribute") self.fdel(obj) def getter(self, fget): return type(self)(fget, self.fset, self.fdel, self.__doc__) def setter(self, fset): return type(self)(self.fget, fset, self.fdel, self.__doc__) def deleter(self, fdel): return Property(self.fget, self.fset, fdel, self.__doc__)
On Thu, Oct 20, 2016 at 9:45 PM, kirby urner <kirby.urner@gmail.com> wrote:
https://docs.python.org/2/library/unittest.html if __name__ == "__main__": # import unittest unittest.main()
Yeah, seriously good advice. I've added unittests to the latest version, which I'll push to Github when the site once again becomes available. http://gizmodo.com/this-is-probably-why-half-the-internet-shut-down-today-17... Kirby class TestCircle(unittest.TestCase): def testArea(self): the_circle = Circle(1) self.assertEqual(the_circle.area, math.pi, "Uh oh") def testRadius(self): the_circle = Circle(1) the_circle.area = math.pi * 4 # power rule self.assertEqual(the_circle.radius, 2, "Uh oh") a = TestCircle() # the test suite suite = unittest.TestLoader().loadTestsFromModule(a) unittest.TextTestRunner().run(suite) # run the test suite
IIRC, many computer labs have no internet access (as a closed loop learning environment). But if it's possible to host something like DevPi as a proxy cache in order to support pip for package installation, nose-progressive and pytest-sugar add a progressbar and other useful features to the nose and pytest test runners: - https://westurner.org/wiki/awesome-python-testing#nose - https://westurner.org/wiki/awesome-python-testing#py-test Fortunately, I have CloudFlare DNS/CDN in front of my GitHub Pages sites, so these should still be online today while DNS for a number of sites is down. ... jupyter/nbgrader has some cool CI-like features for teachers and students: - https://wrdrd.com/docs/consulting/education-technology#nbgrader - https://github.com/jupyter/nbgrader On Friday, October 21, 2016, kirby urner <kirby.urner@gmail.com> wrote:
participants (2)
-
kirby urner
-
Wes Turner