You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<p>The objective of this exercise is to create subclasses of the built-in
808
+
<aclass="reference external" href="https://docs.python.org/3/library/stdtypes.html#set" title="(in Python v3.9)"><codeclass="xref py py-class docutils literal notranslate"><spanclass="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
+
<olclass="arabic simple">
811
+
<li><p>In this week’s repository, create a package called <codeclass="xref py py-mod docutils literal notranslate"><spanclass="pre">sets</span></code> containing a
812
+
module <codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">verified_sets</span></code>. Create a subclass of the inbuilt <aclass="reference external" href="https://docs.python.org/3/library/stdtypes.html#set" title="(in Python v3.9)"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">set</span></code></a>,
be the parent of other classes which have particular verification rules.</p></li>
815
+
<li><p>Give <codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">VerifiedSet</span></code> a method <codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">_verify()</span></code> which takes a single
816
+
value. In the case of <codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">VerifiedSet</span></code>, <codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">_verify()</span></code> should
<codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">VerifiedSet</span></code> will override this method to do something more useful.</p></li>
819
+
<li><p>For each <aclass="reference external" href="https://docs.python.org/3/library/stdtypes.html#set" title="(in Python v3.9)"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">set</span></code></a> method which adds items to the set,
820
+
<codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">VerifiedSet</span></code> will need to have its own version which calls
821
+
<codeclass="xref py py-meth docutils literal notranslate"><spanclass="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 <aclass="reference external" href="https://docs.python.org/3/library/stdtypes.html#frozenset.add" title="(in Python v3.9)"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">add()</span></code></a>, <aclass="reference external" href="https://docs.python.org/3/library/stdtypes.html#frozenset.update" title="(in Python v3.9)"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">update()</span></code></a>, and
<li><p>For those methods which create a new set, <codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">VerifiedSet</span></code> will also
826
+
need to <aclass="reference internal" href="3_objects.html#term-instantiate"><spanclass="xref std std-term">instantiate</span></a> a new object, so that the method returns a subclass of
827
+
<codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">VerifiedSet</span></code> instead of a plain <aclass="reference external" href="https://docs.python.org/3/library/stdtypes.html#set" title="(in Python v3.9)"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">set</span></code></a>. The methods to which
<li><p>Create a subclass of <codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">VerifiedSet</span></code> called <codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">IntSet</span></code> in which
832
+
only integers (i.e. instances of <aclass="reference external" href="https://docs.python.org/3/library/numbers.html#numbers.Integral" title="(in Python v3.9)"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">numbers.Integral</span></code></a>) are allowed.
833
+
On encountering a non-integer <codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">IntSet._verify()</span></code> should raise
834
+
<aclass="reference external" href="https://docs.python.org/3/library/exceptions.html#TypeError" title="(in Python v3.9)"><codeclass="xref py py-class docutils literal notranslate"><spanclass="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 <codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">VerifiedSet</span></code> called <codeclass="xref py py-class docutils literal notranslate"><spanclass="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 <codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">UniquenessError</span></code>, a subclass of
840
+
<aclass="reference external" href="https://docs.python.org/3/library/exceptions.html#KeyError" title="(in Python v3.9)"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">KeyError</span></code></a>. <codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">UniqueSet._verify</span></code> should raise this if an
841
+
operation would add a duplicate value to the <codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">UniqueSet</span></code>.</p></li>
842
+
</ol>
801
843
</div></div><divclass="admonition note">
802
844
<pclass="admonition-title">Note</p>
803
845
<p>Quiz exercise giving a bunch complicated inheritance pattern and asking what
804
846
various things print.</p>
805
847
</div>
806
-
<divclass="admonition note">
807
-
<pclass="admonition-title">Note</p>
808
-
<p>One exercise will be to implement another family of groups by importing and
0 commit comments