Skip to content

Commit b1471ef

Browse files
updating rendered materials
1 parent 682aebf commit b1471ef

17 files changed

Lines changed: 1627 additions & 34 deletions

File tree

‎_sources/class_schedule/lesson04.rst.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Exercises:
3636

3737
../exercises/exceptions_lab.rst
3838
../exercises/except_exercise.rst
39-
../exercises/unit_testing.rst
39+
../exercises/unit_testing/unit_testing.rst
4040
../exercises/dict_lab.rst
4141
../exercises/mailroom/mailroom_with_dicts.rst
4242
../exercises/trigrams/trigrams.rst

‎_sources/class_schedule/lesson08.rst.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Exercises:
3838
.. toctree::
3939
:maxdepth: 1
4040

41-
../exercises/circle_class.rst
41+
../exercises/circle/circle_class.rst
4242

4343
Optional Exercise:
4444
------------------
Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
1+
.. _exercise_circle_class:
2+
3+
#####################
4+
Circle Class Exercise
5+
#####################
6+
7+
Circle Class
8+
============
9+
10+
The ultimate circle....
11+
12+
13+
Goal:
14+
-----
15+
16+
The goal is to create a class that represents a simple circle.
17+
18+
A Circle can be defined by either specifying the radius or the diameter,
19+
and the user can query the circle for either its radius or diameter.
20+
21+
Other abilities of a Circle instance:
22+
23+
* Compute the circle's area.
24+
* Print the circle and get something nice.
25+
* Be able to add two circles together.
26+
* Be able to compare two circles to see which is bigger.
27+
* Be able to compare to see if they are are equal.
28+
* (follows from above) be able to put them in a list and sort them.
29+
30+
31+
You will use:
32+
33+
- properties.
34+
- a bunch of "magic methods".
35+
- a classmethod.
36+
37+
General Instructions:
38+
---------------------
39+
40+
1. For each step, write a couple of unit tests that test the new features.
41+
42+
2. Run these tests (and they will fail the first time)
43+
44+
3. Add the code required for your tests to pass.
45+
46+
47+
Step 1:
48+
-------
49+
50+
Create class called ``Circle`` -- it's signature should look like::
51+
52+
c = Circle(the_radius)
53+
54+
The radius is a required parameter (can't have a circle without one!)
55+
56+
The resulting circle should have an attribute for the radius::
57+
58+
c.radius
59+
60+
So you can do:
61+
62+
.. code-block:: python
63+
64+
>> c = Circle(4)
65+
>> print(c.radius)
66+
4
67+
68+
Remember: tests first!
69+
70+
Step 2:
71+
-------
72+
73+
Add a "diameter" property, so the user can get the diameter of the circle:
74+
75+
.. code-block:: python
76+
77+
>> c = Circle(4)
78+
>> print(c.diameter)
79+
8
80+
81+
Step 3:
82+
-------
83+
84+
Set up the diameter property so that the user can set the diameter of the circle:
85+
86+
.. code-block:: python
87+
88+
>> c = Circle(4)
89+
>> c.diameter = 2
90+
>> print c.diameter
91+
2
92+
>> print c.radius
93+
1
94+
95+
**NOTE** that the radius has changed!
96+
97+
**Important:** Do not store both the radius and the diameter as attributes! If you do that, they could get out of sync. So store only one (the radius), and have the other calculated "on the fly" by the property.
98+
99+
Step 4:
100+
--------
101+
102+
Add an ``area`` property so the user can get the area of the circle:
103+
104+
.. code-block:: python
105+
106+
>> c = Circle(2)
107+
>> print(c.area)
108+
12.566370
109+
110+
(``pi`` can be found in the math module).
111+
112+
The user should not be able to set the area:
113+
114+
.. code-block:: python
115+
116+
>> c = Circle(2)
117+
>> c.area = 42
118+
AttributeError
119+
120+
Step 5:
121+
-------
122+
123+
Add an "alternate constructor" that lets the user create a Circle directly
124+
with the diameter:
125+
126+
.. code-block:: python
127+
128+
>> c = Circle.from_diameter(8)
129+
>> print(c.diameter)
130+
8
131+
>> print(c.radius)
132+
4
133+
134+
Hint: This is a good use case for a ``classmethod``
135+
136+
Step 6:
137+
-------
138+
139+
Every class should have a nice way to print it out...
140+
141+
Add ``__str__`` and ``__repr__`` methods to your Circle class.
142+
143+
Now you can print it:
144+
145+
.. code-block:: ipython
146+
147+
In [2]: c = Circle(4)
148+
149+
In [3]: print(c)
150+
Circle with radius: 4.000000
151+
152+
In [4]: repr(c)
153+
Out[4]: 'Circle(4)'
154+
155+
In [5]: d = eval(repr(c))
156+
157+
In [6]: d
158+
Out[6]: Circle(4)
159+
160+
Step 7:
161+
--------
162+
163+
Add some of the numeric protocol to your Circle:
164+
165+
You should be able to add two circles:
166+
167+
.. code-block:: ipython
168+
169+
In [7]: c1 = Circle(2)
170+
171+
In [8]: c2 = Circle(4)
172+
173+
In [9]: c1 + c2
174+
Out[9]: Circle(6)
175+
176+
and multiply one by a number:
177+
178+
.. code-block:: ipython
179+
180+
In [16]: c2 * 3
181+
Out[16]: Circle(12)
182+
183+
(what happens with ``3 * c2`` ? -- can you fix that?)
184+
185+
186+
187+
Step 8:
188+
--------
189+
190+
Add the ability to compare two circles:
191+
192+
.. code-block:: ipython
193+
194+
In [10]: c1 > c2
195+
Out[10]: False
196+
197+
In [11]: c1 < c2
198+
Out[11]: True
199+
200+
In [12]: c1 == c2
201+
Out[12]: False
202+
203+
In [13]: c3 = Circle(4)
204+
205+
In [14]: c2 == c3
206+
Out[14]: True
207+
208+
209+
Once the comparing is done, you should be able to sort a list of circles:
210+
211+
.. code-block:: ipython
212+
213+
In [18]: print circles
214+
[Circle(6), Circle(7), Circle(8), Circle(4), Circle(0), Circle(2), Circle(3), Circle(5), Circle(9), Circle(1)]
215+
216+
In [19]: circles.sort()
217+
218+
In [20]: print circles
219+
[Circle(0), Circle(1), Circle(2), Circle(3), Circle(4), Circle(5), Circle(6), Circle(7), Circle(8), Circle(9)]
220+
221+
**NOTE:** make sure to write unit tests for all of this! Ideally before writing the code.
222+
223+
Step 8: Optional Features:
224+
--------------------------
225+
226+
* See if you can make "reflected" numerics do the right thing:
227+
228+
.. code-block:: python
229+
230+
a_circle * 3 == 3 * a_circle
231+
232+
* What else makes sense: division? others?
233+
234+
* Add the "augmented assignment" operators, where they make sense:
235+
236+
.. code-block:: python
237+
238+
a_circle += another_circle
239+
240+
a_circle *= 2
241+
242+
* Look through all the "magic methods" and see what makes sense for circles.
243+
244+
245+
Step 9: Subclassing!
246+
--------------------
247+
248+
You've got a circle already -- what if you needed a Sphere? They have a fair bit in common -- both defined by a radius, same relationship of radius to diameter, etc.
249+
250+
So we can get a pretty useful Sphere class by simply subclassing Circle, and adding and changing a couple things.
251+
252+
* Create a ``Sphere`` Class that subclasses ``Circle``.
253+
254+
* Override the ``__str__`` and ``__repr__`` methods to be appropriate for Spheres.
255+
256+
* Create a ``volume`` property that returns the volume (hint: volume of a sphere is: 4/3 pi r^3).
257+
258+
* Override the area property so that it either computes the surface area of a sphere (what's the formula for that???), or have it raise an exception: maybe ``NotImplementedError``.
259+
260+
Make sure to write some tests -- maybe ahead of time! -- that confirm that all this works. And the other things like addition, and sorting...
261+
262+
Check that the ``Sphere.from_diameter()`` alternate constructor actually creates a Sphere! (you DO NOT have to write a new classmethod for that!) -- pretty cool, eh?

‎_sources/exercises/index.rst.txt‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ Lesson 6: Unit Testing and Advanced Argument Passing
7171
.. toctree::
7272
:maxdepth: 1
7373

74-
unit_testing.rst
74+
unit_testing/unit_testing.rst
7575
args_kwargs_lab.rst
7676
mailroom/mailroom_with_tests.rst
7777

@@ -93,7 +93,7 @@ Lesson 8: Properties and Magic Methods
9393
.. toctree::
9494
:maxdepth: 1
9595

96-
circle_class.rst
96+
circle/circle_class.rst
9797

9898
Lesson 9: Multiple Inheritance
9999
------------------------------

0 commit comments

Comments
 (0)