Skip to content

Commit c5544a4

Browse files
committed
Website build
1 parent 0138555 commit c5544a4

File tree

3 files changed

+87
-10
lines changed

3 files changed

+87
-10
lines changed

8_inheritance.html

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -798,16 +798,53 @@ <h2><span class="section-number">8.6. </span>Exercises<a class="headerlink" href
798798
<p>In implementing element validation, the builtin function <a class="reference external" href="https://docs.python.org/3/library/functions.html#sorted" title="(in Python v3.9)"><code class="xref py py-func docutils literal notranslate"><span class="pre">sorted()</span></code></a>
799799
is likely to be useful.</p>
800800
</div>
801+
</div></div><div class="proof proof-type-exercise" id="id5">
802+
803+
<div class="proof-title">
804+
<span class="proof-type">Exercise 8.2</span>
805+
806+
</div><div class="proof-content">
807+
<p>The objective of this exercise is to create subclasses of the built-in
808+
<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#set" title="(in Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">set</span></code></a> class which are only valid for values which pass a certain
809+
test. For example, one might have a set which can only contain integers.</p>
810+
<ol class="arabic simple">
811+
<li><p>In this week’s repository, create a package called <code class="xref py py-mod docutils literal notranslate"><span class="pre">sets</span></code> containing a
812+
module <code class="xref py py-obj docutils literal notranslate"><span class="pre">verified_sets</span></code>. Create a subclass of the inbuilt <a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#set" title="(in Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">set</span></code></a>,
813+
<code class="xref py py-class docutils literal notranslate"><span class="pre">sets.verified_sets.VerifiedSet</span></code>. <code class="xref py py-class docutils literal notranslate"><span class="pre">VerifiedSet</span></code> will itself
814+
be the parent of other classes which have particular verification rules.</p></li>
815+
<li><p>Give <code class="xref py py-class docutils literal notranslate"><span class="pre">VerifiedSet</span></code> a method <code class="xref py py-meth docutils literal notranslate"><span class="pre">_verify()</span></code> which takes a single
816+
value. In the case of <code class="xref py py-class docutils literal notranslate"><span class="pre">VerifiedSet</span></code>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">_verify()</span></code> should
817+
unconditionally raise <a class="reference external" href="https://docs.python.org/3/library/exceptions.html#NotImplementedError" title="(in Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a>. Subclasses of
818+
<code class="xref py py-class docutils literal notranslate"><span class="pre">VerifiedSet</span></code> will override this method to do something more useful.</p></li>
819+
<li><p>For each <a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#set" title="(in Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">set</span></code></a> method which adds items to the set,
820+
<code class="xref py py-class docutils literal notranslate"><span class="pre">VerifiedSet</span></code> will need to have its own version which calls
821+
<code class="xref py py-meth docutils literal notranslate"><span class="pre">_verify()</span></code> on each item, before calling the appropriate superclass
822+
method in order to actually insert the value(s). The methods which add
823+
items to a set are <a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#frozenset.add" title="(in Python v3.9)"><code class="xref py py-meth docutils literal notranslate"><span class="pre">add()</span></code></a>, <a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#frozenset.update" title="(in Python v3.9)"><code class="xref py py-meth docutils literal notranslate"><span class="pre">update()</span></code></a>, and
824+
<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#frozenset.symmetric_difference_update" title="(in Python v3.9)"><code class="xref py py-meth docutils literal notranslate"><span class="pre">symmetric_difference_update()</span></code></a>.</p></li>
825+
<li><p>For those methods which create a new set, <code class="xref py py-class docutils literal notranslate"><span class="pre">VerifiedSet</span></code> will also
826+
need to <a class="reference internal" href="3_objects.html#term-instantiate"><span class="xref std std-term">instantiate</span></a> a new object, so that the method returns a subclass of
827+
<code class="xref py py-class docutils literal notranslate"><span class="pre">VerifiedSet</span></code> instead of a plain <a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#set" title="(in Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">set</span></code></a>. The methods to which
828+
this applies are <a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#frozenset.union" title="(in Python v3.9)"><code class="xref py py-meth docutils literal notranslate"><span class="pre">union()</span></code></a>, <a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#frozenset.intersection" title="(in Python v3.9)"><code class="xref py py-meth docutils literal notranslate"><span class="pre">intersection()</span></code></a>,
829+
<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#frozenset.difference" title="(in Python v3.9)"><code class="xref py py-meth docutils literal notranslate"><span class="pre">difference()</span></code></a>, <a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#frozenset.symmetric_difference" title="(in Python v3.9)"><code class="xref py py-meth docutils literal notranslate"><span class="pre">symmetric_difference()</span></code></a>, and
830+
<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#frozenset.copy" title="(in Python v3.9)"><code class="xref py py-meth docutils literal notranslate"><span class="pre">copy()</span></code></a>.</p></li>
831+
<li><p>Create a subclass of <code class="xref py py-class docutils literal notranslate"><span class="pre">VerifiedSet</span></code> called <code class="xref py py-class docutils literal notranslate"><span class="pre">IntSet</span></code> in which
832+
only integers (i.e. instances of <a class="reference external" href="https://docs.python.org/3/library/numbers.html#numbers.Integral" title="(in Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">numbers.Integral</span></code></a>) are allowed.
833+
On encountering a non-integer <code class="xref py py-meth docutils literal notranslate"><span class="pre">IntSet._verify()</span></code> should raise
834+
<a class="reference external" href="https://docs.python.org/3/library/exceptions.html#TypeError" title="(in Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">TypeError</span></code></a> with an error message of the following form. For example
835+
if an attempt were made to add a string to the set, the message would be
836+
“IntSet expected an integer, got a str.”.</p></li>
837+
<li><p>Create a subclass of <code class="xref py py-class docutils literal notranslate"><span class="pre">VerifiedSet</span></code> called <code class="xref py py-class docutils literal notranslate"><span class="pre">UniqueSet</span></code> into
838+
which values can only be added if they are not already in the set. You
839+
should create a new exception <code class="xref py py-class docutils literal notranslate"><span class="pre">UniquenessError</span></code>, a subclass of
840+
<a class="reference external" href="https://docs.python.org/3/library/exceptions.html#KeyError" title="(in Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">KeyError</span></code></a>. <code class="xref py py-class docutils literal notranslate"><span class="pre">UniqueSet._verify</span></code> should raise this if an
841+
operation would add a duplicate value to the <code class="xref py py-class docutils literal notranslate"><span class="pre">UniqueSet</span></code>.</p></li>
842+
</ol>
801843
</div></div><div class="admonition note">
802844
<p class="admonition-title">Note</p>
803845
<p>Quiz exercise giving a bunch complicated inheritance pattern and asking what
804846
various things print.</p>
805847
</div>
806-
<div class="admonition note">
807-
<p class="admonition-title">Note</p>
808-
<p>One exercise will be to implement another family of groups by importing and
809-
inheriting from <a class="reference internal" href="example_code.html#example_code.groups.Group" title="example_code.groups.Group"><code class="xref py py-class docutils literal notranslate"><span class="pre">Group</span></code></a>.</p>
810-
</div>
811848
</div>
812849
</div>
813850

_sources/8_inheritance.rst.txt

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -707,12 +707,52 @@ Obtain the :doc:`skeleton code for these exercises from GitHub classroom <not_re
707707
In implementing element validation, the builtin function :func:`sorted`
708708
is likely to be useful.
709709

710+
711+
.. proof:exercise::
712+
713+
The objective of this exercise is to create subclasses of the built-in
714+
:class:`set` class which are only valid for values which pass a certain
715+
test. For example, one might have a set which can only contain integers.
716+
717+
1. In this week's repository, create a package called :mod:`sets` containing a
718+
module `verified_sets`. Create a subclass of the inbuilt :class:`set`,
719+
:class:`sets.verified_sets.VerifiedSet`. :class:`VerifiedSet` will itself
720+
be the parent of other classes which have particular verification rules.
721+
722+
2. Give :class:`VerifiedSet` a method :meth:`_verify` which takes a single
723+
value. In the case of :class:`VerifiedSet`, :meth:`_verify` should
724+
unconditionally raise :class:`NotImplementedError`. Subclasses of
725+
:class:`VerifiedSet` will override this method to do something more useful.
726+
727+
3. For each :class:`set` method which adds items to the set,
728+
:class:`VerifiedSet` will need to have its own version which calls
729+
:meth:`_verify` on each item, before calling the appropriate superclass
730+
method in order to actually insert the value(s). The methods which add
731+
items to a set are :meth:`~frozenset.add`, :meth:`~frozenset.update`, and
732+
:meth:`~frozenset.symmetric_difference_update`.
733+
734+
4. For those methods which create a new set, :class:`VerifiedSet` will also
735+
need to :term:`instantiate` a new object, so that the method returns a subclass of
736+
:class:`VerifiedSet` instead of a plain :class:`set`. The methods to which
737+
this applies are :meth:`~frozenset.union`, :meth:`~frozenset.intersection`,
738+
:meth:`~frozenset.difference`, :meth:`~frozenset.symmetric_difference`, and
739+
:meth:`~frozenset.copy`.
740+
741+
5. Create a subclass of :class:`VerifiedSet` called :class:`IntSet` in which
742+
only integers (i.e. instances of :class:`numbers.Integral`) are allowed.
743+
On encountering a non-integer :meth:`IntSet._verify` should raise
744+
:class:`TypeError` with an error message of the following form. For example
745+
if an attempt were made to add a string to the set, the message would be
746+
"IntSet expected an integer, got a str.".
747+
748+
6. Create a subclass of :class:`VerifiedSet` called :class:`UniqueSet` into
749+
which values can only be added if they are not already in the set. You
750+
should create a new exception :class:`UniquenessError`, a subclass of
751+
:class:`KeyError`. :class:`UniqueSet._verify` should raise this if an
752+
operation would add a duplicate value to the :class:`UniqueSet`.
753+
710754
.. note::
711755

712756
Quiz exercise giving a bunch complicated inheritance pattern and asking what
713757
various things print.
714758

715-
.. note::
716-
717-
One exercise will be to implement another family of groups by importing and
718-
inheriting from :class:`~example_code.groups.Group`.

0 commit comments

Comments
 (0)