blob: d43337d4508dde220cb65c75110daf2fd8f2e939 [file] [log] [blame]
Paul Ganssle4c0ba122018-06-07 20:07:21 +01001Exercises
2=========
3
4It is often useful to work through some examples in order to understand how a module works; on this page, there are several exercises of varying difficulty that you can use to learn how to use ``dateutil``.
5
6If you are interested in helping improve the documentation of ``dateutil``, it is recommended that you attempt to complete these exercises with no resources *other than dateutil's documentation*. If you find that the documentation is not clear enough to allow you to complete these exercises, open an issue on the `dateutil issue tracker <https://github.com/dateutil/dateutil/issues>`_ to let the developers know what part of the documentation needs improvement.
7
8
9.. contents:: Table of Contents
10 :backlinks: top
11 :local:
12
Paul Ganssle404af912018-06-07 20:09:47 +010013
14Martin Luther King Day
15--------------------------------
16
17
18 `Martin Luther King, Jr Day <https://en.wikipedia.org/wiki/Martin_Luther_King_Jr._Day>`_ is a US holiday that occurs every year on the third Monday in January?
19
20 How would you generate a `recurrence rule <../rrule.html>`_ that generates Martin Luther King Day, starting from its first observance in 1986?
21
22
23**Test Script**
24
25To solve this exercise, copy-paste this script into a document, change anything between the ``--- YOUR CODE ---`` comment blocks.
26
27.. raw:: html
28
29 <details>
30
31.. code-block:: python3
32
33 # ------- YOUR CODE -------------#
34 from dateutil import rrule
35
36 MLK_DAY = <<YOUR CODE HERE>>
37
38 # -------------------------------#
39
40 from datetime import datetime
41 MLK_TEST_CASES = [
42 ((datetime(1970, 1, 1), datetime(1980, 1, 1)),
43 []),
44 ((datetime(1980, 1, 1), datetime(1989, 1, 1)),
45 [datetime(1986, 1, 20),
46 datetime(1987, 1, 19),
47 datetime(1988, 1, 18)]),
48 ((datetime(2017, 2, 1), datetime(2022, 2, 1)),
49 [datetime(2018, 1, 15, 0, 0),
50 datetime(2019, 1, 21, 0, 0),
51 datetime(2020, 1, 20, 0, 0),
52 datetime(2021, 1, 18, 0, 0),
53 datetime(2022, 1, 17, 0, 0)]
54 ),
55 ]
56
57 def test_mlk_day():
58 for (between_args, expected) in MLK_TEST_CASES:
59 assert MLK_DAY.between(*between_args) == expected
60
61 if __name__ == "__main__":
62 test_mlk_day()
63 print('Success!')
64
65
66.. raw:: html
67
68 </details>
69
70