[Pymoul-svn] SF.net SVN: pymoul: [243] pymoul/trunk/src/moul/time
Status: Alpha
Brought to you by:
tiran
|
From: <ti...@us...> - 2007-03-08 12:57:43
|
Revision: 243
http://pymoul.svn.sourceforge.net/pymoul/?rev=243&view=rev
Author: tiran
Date: 2007-03-08 04:57:43 -0800 (Thu, 08 Mar 2007)
Log Message:
-----------
Added unit tests for gira
More implementation details
Modified Paths:
--------------
pymoul/trunk/src/moul/time/edergira.py
Added Paths:
-----------
pymoul/trunk/src/moul/time/tests/test_edergira.py
Modified: pymoul/trunk/src/moul/time/edergira.py
===================================================================
--- pymoul/trunk/src/moul/time/edergira.py 2007-03-08 02:42:59 UTC (rev 242)
+++ pymoul/trunk/src/moul/time/edergira.py 2007-03-08 12:57:43 UTC (rev 243)
@@ -18,12 +18,19 @@
"""pyMoul Ede Gira day cycle
Based on BrettM's information
+
+>>> from pytz import timezone
+>>> MST = timezone('MST')
+>>> sunset = datetime(2007, 3, 8, 4, 30, 0, tzinfo=MST)
+>>> egdc = EderGiraDayCalculator()
+>>> eg = egdc.getDayInfo(sunset)
+>>> eg.state, eg.light, eg.hour
+(<dusk>, 0.75, 7.25)
"""
__author__ = "Christian Heimes"
__version__ = "$Id"
__revision__ = "$Revision"
-
from datetime import datetime
from datetime import timedelta
@@ -31,11 +38,136 @@
from moul.time.utils import td2sec
from moul.time.utils import utcnow
-delta = timedelta(days=0, hours=10, minutes=0) # 10h
-base_dt = datetime(2007, 3, 8, 9, 15, 0) # noon
+class Invariant(object):
+ __slots__ = ('_var',)
+ def __new__(cls, var):
+ self = object.__new__(cls)
+ if not isinstance(var, basestring):
+ raise TypeError(type(var))
+ self._var = var
+ return self
+
+ def __repr__(self):
+ return '<%s>' % self._var
+
+ def __str__(self):
+ return str(self._var)
+
+ def __unicode__(self):
+ return unicode(self._var)
+
+NIGHT = Invariant('night')
+DAWN = Invariant('dawn')
+DAY = Invariant('day')
+DUSK = Invariant('dusk')
+
+class EderGiraDayCalculator(object):
+ """Eder Gira day cycle calculator
+ """
+ def __init__(self):
+ self._basedt = datetime(2007, 3, 8, 4, 15, 0, tzinfo=UTC) # midnight
+ self._hours = 10
+ self._delta = timedelta(days=0, hours=self._hours)
+ self._deltasec = self._hours * 3600
+
+ def getLastMidnight(self, dt=None):
+ """Get last midnight in relation to dt (or now)
+ """
+ if dt is None:
+ dt = utcnow()
+ cycles = td2sec(dt - self._basedt) / self._deltasec
+ return self._basedt + (int(cycles) * self._delta)
+
+ def getDayInfo(self, dt=None):
+ """Get day info object for dt (or now)
+ """
+ if dt is None:
+ dt = utcnow()
+ sec = td2sec(dt - self._basedt)
+ return EderGiraDay(round(sec / 3600.0, 2) % self._hours)
+
class EderGiraDay(object):
- """Eder Gira day cycle class
+ """An Eder Gira day representation
+
+ >>> eg = EderGiraDay(1.25)
+ >>> eg.state, eg.light, eg.hour
+ (<night>, 0.0, 1.25)
+ >>> int(eg)
+ 1
+ >>> float(eg)
+ 1.25
+
+ >>> eg = EderGiraDay(2.5)
+ >>> eg.state, eg.light, eg.hour
+ (<dawn>, 0.5, 2.5)
+
+ >>> eg = EderGiraDay(5.0)
+ >>> eg.state, eg.light, eg.hour
+ (<day>, 1.0, 5.0)
+
+ >>> eg = EderGiraDay(7.875)
+ >>> eg.state, eg.light, eg.hour
+ (<dusk>, 0.13, 7.875)
+
+ >>> eg = EderGiraDay(9.5)
+ >>> eg.state, eg.light, eg.hour
+ (<night>, 0.0, 9.5)
+
+ >>> eg = EderGiraDay(10.0)
+ Traceback (most recent call last):
+ ...
+ ValueError: 10.0
"""
- def __init__(self):
- pass
+ def __init__(self, eghour):
+ if not 0 <= eghour < 10:
+ raise ValueError(eghour)
+ self._eghour = eghour
+
+ @property
+ def state(self):
+ """State: night, dawn, day, dusk
+ """
+ eg = self._eghour
+ if eg < 2.0:
+ return NIGHT
+ elif 2.0 <= eg < 3.0:
+ return DAWN
+ elif 3.0 <= eg < 7.0:
+ return DAY
+ elif 7.0 <= eg < 8.0:
+ return DUSK
+ elif 8.0 <= eg < 10:
+ return NIGHT
+ else:
+ raise ValueError(eg)
+
+ @property
+ def light(self):
+ """Light level between 0.0 and 1.0
+ """
+ state = self.state
+ if state is NIGHT:
+ return 0.0
+ elif state is DAWN:
+ level = self._eghour % 1
+ return round(level, 2)
+ elif state is DAY:
+ return 1.0
+ elif state is DUSK:
+ level = self._eghour % 1
+ return round(1-level, 2)
+ else:
+ raise ValueError(state)
+
+ @property
+ def hour(self):
+ """Hour between 0.00 and 9.99
+ """
+ return self._eghour
+
+ def __int__(self):
+ return int(self._eghour)
+
+ def __float__(self):
+ return float(self._eghour)
Added: pymoul/trunk/src/moul/time/tests/test_edergira.py
===================================================================
--- pymoul/trunk/src/moul/time/tests/test_edergira.py (rev 0)
+++ pymoul/trunk/src/moul/time/tests/test_edergira.py 2007-03-08 12:57:43 UTC (rev 243)
@@ -0,0 +1,37 @@
+# pyMoul - Python interface to Myst Online URU Live
+# Copyright (C) 2007 Christian Heimes <christian (at) cheimes (dot) de>
+
+# This program is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by the
+# Free Software Foundation; either version 2 of the License, or (at your
+# option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+# more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc., 59
+# Temple Place, Suite 330, Boston, MA 02111-1307 USA
+#
+"""moul.time.edergira unit tests
+"""
+__author__ = "Christian Heimes"
+__version__ = "$Id: test_dni.py 122 2007-02-02 17:34:06Z tiran $"
+__revision__ = "$Revision: 122 $"
+
+import os
+import unittest
+from doctest import DocTestSuite
+
+import moul.time.edergira
+
+
+def test_suite():
+ return unittest.TestSuite((
+ DocTestSuite('moul.time.edergira'),
+ ))
+
+if __name__ == '__main__':
+ unittest.main(defaultTest="test_suite")
Property changes on: pymoul/trunk/src/moul/time/tests/test_edergira.py
___________________________________________________________________
Name: svn:keywords
+ 'Id Revision'
Name: svn:eol-style
+ native
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|