Skip to content

Commit 6c3fdb9

Browse files
committed
Start of exercises.
1 parent 9f46223 commit 6c3fdb9

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed

doc/source/11_further_object-oriented_features.rst

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,40 @@ Glossary
540540
Exercises
541541
---------
542542

543+
Obtain the `skeleton code for these exercises from GitHub classroom
544+
<>`__.
545+
546+
.. proof:exercise::
547+
548+
The objective of this exercise is to write a :term:`decorator` which logs
549+
whenever the decorated function is called. This sort of decorator could be
550+
very useful in debugging code. Create the decorator in the
551+
`log_decorator.log_decorator` module and ensure it is importable as
552+
`log_decorator.log_call`. The decorator should be applicable to functions
553+
taking any combination of arguments.
554+
555+
The logging itself should be accomplished using
556+
the built-in `logging` module by calling :func:`logging.info` and passing
557+
the logging message.
558+
559+
The logging message should comprise the function name (accessible using the
560+
`__name__` attribute), followed by round brackets containing first the
561+
:func:`repr` of the positional arguments, followed by the key=value pairs
562+
the keyword arguments.
563+
564+
.. proof:exercise::
565+
566+
The :mod:`groups.groups` module in the skeleton code is the new version
567+
introduced above, using an :term:`abstract base class`. The
568+
`log_decorator.log_call` :term:`decorator` has been applied to the
569+
:math:`Group._validate` :term:`abstract method`. However, even once you
570+
have implemented this decorator, it never gets called. Your challenge is to
571+
modify :mod:`groups.groups` so that the decorator is called every time a
572+
subclass :meth:`_validate` method is called, but **without** moving or
573+
duplicating `@log_call`.
574+
575+
576+
543577
Exam preparation
544578
----------------
545579

doc/webgit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Subproject commit 980ab0a97fffdaab91555208aa27ee249e79c715
1+
Subproject commit 3ec91a3fbcd3791143806564c6f04affb354357f

example_code/addable.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from abc import ABC, abstractmethod
2+
3+
4+
class Addable(ABC):
5+
6+
@abstractmethod
7+
def __add__(self, other):
8+
return NotImplemented
9+
10+
@classmethod
11+
def __subclasshook__(cls, C):
12+
if cls is not Addable:
13+
return NotImplemented
14+
for B in C.__mro__:
15+
if "__add__" in B.__dict__:
16+
if B.__dict__["__add__"] is None:
17+
return NotImplemented
18+
return True
19+
return NotImplemented

example_code/groups_abc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def symbol(self):
6464

6565
@abstractmethod
6666
def _validate(self, value):
67-
"""Ensure that value is a legitimate element value in this group."""
67+
"""Ensure that value is a legitimate element value in this Group."""
6868
pass
6969

7070
@abstractmethod

0 commit comments

Comments
 (0)