fix name
[maegirls] / maegirls / src / algo.py
1 #!/usr/bin/env python
2 # coding=UTF-8
3
4 # Copyright (C) 2010 Stefanos Harhalakis
5 #
6 # This file is part of maegirls.
7 #
8 # maegirls is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # maegirls is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with maegirls.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 # $Id: 0.py 2265 2010-02-21 19:16:26Z v13 $
22
23 __version__ = "$Id: 0.py 2265 2010-02-21 19:16:26Z v13 $"
24
25 import time
26
27 def today():
28     t=time.time() - time.timezone
29     day=int(t/86400)
30
31     return(day)
32
33 # Defaults for a girl
34 defaults={
35     'cycle':        28,         # Cycle length
36     'menstr':       5,          # Duration of menstruation
37     'ovday':        -14,        # When ovulation happens. Positive: This number
38                                 # of days after the start of the cycle.
39                                 # Negative: This number of days before the end
40                                 # of the cycle.
41     'day0':         today()
42     }
43
44 class Algo(object):
45     def __init__(self):
46         t=today()
47         self.setReference(t, defaults)
48
49     # set day "day" as the reference day of starting period cycle
50     # day is int(time.time()/86400)
51     # cycle is the cycle width in days, starting from 0
52     def setReference(self, day, cfg):
53         self.cfg=cfg
54         self.refday=day
55
56     # Convert an arbitary day to a day in cycle
57     def dayInCycle(self, day):
58         d=(day-self.refday) % self.cfg['cycle']
59         if d<0:
60             d+=self.cycleLength()
61
62         return(d)
63
64     def cycleLength(self):
65         return(self.cfg['cycle'])
66
67     def currentDayInCycle(self):
68         d=today()
69         return(self.dayInCycle(d))
70
71     def menstruationDuration(self):
72         return(self.cfg['menstr'])
73
74     def ovulationDay(self):
75         return(self.cfg['ovday'])
76
77     # Return:
78     # ret={
79     #   'status':   ['ok', 'red']
80     #   'day':      day in status
81     #   'len':      length of status
82     #   }
83     #
84     # e.g.  ret={'status':'red', 'day':0, 'len':5}
85     #       means that it is in red, in the first of 5
86     def status(self, day):
87         d=self.dayInCycle(day)
88
89         cycle=self.cycleLength()
90
91         #ovbefore=14
92         ovday=self.ovulationDay()
93         if ovday>0:
94             ovstart=ovday-3
95             ovmid=ovday
96             ovend=ovday+1
97         else:
98             ovstart=cycle+ovday-3
99             ovmid=cycle+ovday
100             ovend=cycle+ovday+1
101
102         menstr=self.menstruationDuration()
103
104         if d<menstr:
105             # Red
106             ret={
107                 'status':   'red',
108                 'day':      d,
109                 'len':      menstr,
110                 }
111         elif d>=cycle-7:
112             ret={
113                 'status':   'blue',
114                 'day':      7+d-cycle,
115                 'len':      7,
116                 }
117         elif d>=ovstart and d<=ovmid:
118             ret={
119                 'status':   'green',
120                 'day':      d-ovstart,
121                 'len':      4,
122                 }
123         elif d>ovmid and d<=ovend:
124             ret={
125                 'status':   'green',
126                 'day':      2,
127                 'len':      4,
128                 }
129         else:
130             ret={
131                 'status':   'ok',
132                 'day':      d,
133                 'len':      cycle
134                 }
135
136         return(ret)
137
138
139 # vim: set ts=8 sts=4 sw=4 noet formatoptions=r ai nocindent:
140