Initial import
[experiments/season_math_test.git] / season_math_test.py
1 #!/usr/bin/env python
2 #
3 # season_math_test.py - convert from month to season and vice versa
4 #
5 # Copyright (C) 2015  Antonio Ospite <ao2@ao2.it>
6 #
7 # This program is free software. It comes without any warranty, to
8 # the extent permitted by applicable law. You can redistribute it
9 # and/or modify it under the terms of the Do What The Fuck You Want
10 # To Public License, Version 2, as published by Sam Hocevar. See
11 # http://sam.zoy.org/wtfpl/COPYING for more details.
12
13 # The code assumes meteorological seasons.
14 # See http://en.wikipedia.org/wiki/Season
15 #
16 # Default seasons order is the one for the Northern hemisphere.
17
18 seasons_norhtern = ["Winter", "Spring", "Summer", "Fall"]
19 seasons_southern = ["Summer", "Fall", "Winter", "Spring"]
20
21 seasons = seasons_norhtern
22
23 months = ["January", "February", "March", "April", "May", "June", "July",
24           "August", "September", "October", "November", "December"]
25
26
27 def season(month):
28     if month < 0 or month > 11:
29         raise ValueError("Invalid month %d" % month)
30
31     # Rotate forward by one month because Winter starts in December,
32     # and group by three months because each season is three months long.
33     return ((month + 1) % 12) // 3
34
35
36 def first_month_of_season(season):
37     if season < 0 or season > 3:
38         raise ValueError("Invalid season %d" % season)
39
40     # Expand by three because a new season starts every 3 months,
41     # and rotate back by one month because December is the first
42     # month of Winter
43     return (season * 3 - 1) % 12
44
45
46 def test(function):
47     try:
48         eval(function)
49     except Exception as e:
50         print e.message
51
52
53 if __name__ == "__main__":
54     test("season(-1)")
55     test("season(12)")
56     test("first_month_of_season(-1)")
57     test("first_month_of_season(4)")
58
59     print
60
61     for i, m in enumerate(months):
62         j = season(i)
63         print "Month: %s. (%d) \tSeason: %s (%d)" % (m[0:3], i, seasons[j], j)
64
65     print
66
67     for i, s in enumerate(seasons):
68         j = first_month_of_season(i)
69         print "Season: %s (%d)\tFirst month: %s (%d)" % (s, i, months[j], j)