[CalendarServer-changes] [14695] PyCalendar/trunk/src/pycalendar

source_changes at macosforge.org source_changes at macosforge.org
Wed Apr 22 08:58:34 PDT 2015


Revision: 14695
          http://trac.calendarserver.org//changeset/14695
Author:   cdaboo at apple.com
Date:     2015-04-22 08:58:34 -0700 (Wed, 22 Apr 2015)
Log Message:
-----------
Performance optimizations.

Modified Paths:
--------------
    PyCalendar/trunk/src/pycalendar/datetime.py
    PyCalendar/trunk/src/pycalendar/period.py
    PyCalendar/trunk/src/pycalendar/timezone.py

Added Paths:
-----------
    PyCalendar/trunk/src/pycalendar/tests/test_period.py

Modified: PyCalendar/trunk/src/pycalendar/datetime.py
===================================================================
--- PyCalendar/trunk/src/pycalendar/datetime.py	2015-04-21 22:54:54 UTC (rev 14694)
+++ PyCalendar/trunk/src/pycalendar/datetime.py	2015-04-22 15:58:34 UTC (rev 14695)
@@ -50,38 +50,50 @@
 
     def __init__(self, year=None, month=None, day=None, hours=None, minutes=None, seconds=None, tzid=None, utcoffset=None):
 
-        self.mYear = 1970
-        self.mMonth = 1
-        self.mDay = 1
-
-        self.mHours = 0
-        self.mMinutes = 0
-        self.mSeconds = 0
-
-        self.mDateOnly = False
-
-        self.mTZUTC = False
-        self.mTZID = None
-        self.mTZOffset = None
-
-        self.mPosixTimeCached = False
-        self.mPosixTime = 0
-
         if (year is not None) and (month is not None) and (day is not None):
+
             self.mYear = year
             self.mMonth = month
             self.mDay = day
+
             if (hours is not None) and (minutes is not None) and (seconds is not None):
                 self.mHours = hours
                 self.mMinutes = minutes
                 self.mSeconds = seconds
+                self.mDateOnly = False
             else:
+                self.mHours = 0
+                self.mMinutes = 0
+                self.mSeconds = 0
                 self.mDateOnly = True
+
             if tzid:
                 self.mTZUTC = tzid.getUTC()
                 self.mTZID = tzid.getTimezoneID()
+            else:
+                self.mTZUTC = False
+                self.mTZID = None
+            self.mTZOffset = None
 
+        else:
+            self.mYear = 1970
+            self.mMonth = 1
+            self.mDay = 1
 
+            self.mHours = 0
+            self.mMinutes = 0
+            self.mSeconds = 0
+
+            self.mDateOnly = False
+
+            self.mTZUTC = False
+            self.mTZID = None
+            self.mTZOffset = None
+
+        self.mPosixTimeCached = False
+        self.mPosixTime = 0
+
+
     def duplicate(self):
         other = DateTime(self.mYear, self.mMonth, self.mDay, self.mHours, self.mMinutes, self.mSeconds)
 
@@ -194,76 +206,31 @@
             return 1
         # If either are date only, then just do date compare
         if self.mDateOnly or comp.mDateOnly:
-            if self.mYear == comp.mYear:
-                if self.mMonth == comp.mMonth:
-                    if self.mDay == comp.mDay:
-                        return 0
-                    else:
-                        if self.mDay < comp.mDay:
-                            return -1
-                        else:
-                            return 1
-                else:
-                    if self.mMonth < comp.mMonth:
-                        return -1
-                    else:
-                        return 1
-            else:
-                if self.mYear < comp.mYear:
-                    return -1
-                else:
-                    return 1
+            c = cmp(self.mYear, comp.mYear)
+            if c == 0:
+                c = cmp(self.mMonth, comp.mMonth)
+                if c == 0:
+                    c = cmp(self.mDay, comp.mDay)
+            return c
 
         # If they have the same timezone do simple compare - no posix calc
         # needed
         elif (Timezone.same(self.mTZUTC, self.mTZID, comp.mTZUTC, comp.mTZID)):
-            if self.mYear == comp.mYear:
-                if self.mMonth == comp.mMonth:
-                    if self.mDay == comp.mDay:
-                        if self.mHours == comp.mHours:
-                            if self.mMinutes == comp.mMinutes:
-                                if self.mSeconds == comp.mSeconds:
-                                    return 0
-                                else:
-                                    if self.mSeconds < comp.mSeconds:
-                                        return -1
-                                    else:
-                                        return 1
-                            else:
-                                if self.mMinutes < comp.mMinutes:
-                                    return -1
-                                else:
-                                    return 1
-                        else:
-                            if self.mHours < comp.mHours:
-                                return -1
-                            else:
-                                return 1
-                    else:
-                        if self.mDay < comp.mDay:
-                            return -1
-                        else:
-                            return 1
-                else:
-                    if self.mMonth < comp.mMonth:
-                        return -1
-                    else:
-                        return 1
-            else:
-                if self.mYear < comp.mYear:
-                    return -1
-                else:
-                    return 1
+            c = cmp(self.mYear, comp.mYear)
+            if c == 0:
+                c = cmp(self.mMonth, comp.mMonth)
+                if c == 0:
+                    c = cmp(self.mDay, comp.mDay)
+                    if c == 0:
+                        c = cmp(self.mHours, comp.mHours)
+                        if c == 0:
+                            c = cmp(self.mMinutes, comp.mMinutes)
+                            if c == 0:
+                                c = cmp(self.mSeconds, comp.mSeconds)
+            return c
+
         else:
-            posix1 = self.getPosixTime()
-            posix2 = comp.getPosixTime()
-            if posix1 == posix2:
-                return 0
-            else:
-                if posix1 < posix2:
-                    return -1
-                else:
-                    return 1
+            return cmp(self.getPosixTime(), comp.getPosixTime())
 
 
     def compareDate(self, comp):

Modified: PyCalendar/trunk/src/pycalendar/period.py
===================================================================
--- PyCalendar/trunk/src/pycalendar/period.py	2015-04-21 22:54:54 UTC (rev 14694)
+++ PyCalendar/trunk/src/pycalendar/period.py	2015-04-22 15:58:34 UTC (rev 14695)
@@ -28,26 +28,28 @@
 
         if end is not None:
             self.mEnd = end
-            self.mDuration = self.mEnd - self.mStart
+            self.mDuration = None
             self.mUseDuration = False
         elif duration is not None:
             self.mDuration = duration
-            self.mEnd = self.mStart + self.mDuration
+            self.mEnd = None
             self.mUseDuration = True
         else:
             self.mEnd = self.mStart.duplicate()
-            self.mDuration = Duration()
+            self.mDuration = None
             self.mUseDuration = False
 
 
     def duplicate(self):
-        other = Period(start=self.mStart.duplicate(), end=self.mEnd.duplicate())
-        other.mUseDuration = self.mUseDuration
+        if self.mUseDuration:
+            other = Period(start=self.mStart.duplicate(), duration=self.mDuration.duplicate())
+        else:
+            other = Period(start=self.mStart.duplicate(), end=self.mEnd.duplicate())
         return other
 
 
     def __hash__(self):
-        return hash((self.mStart, self.mEnd,))
+        return hash((self.mStart, self.getEnd(),))
 
 
     def __repr__(self):
@@ -59,7 +61,7 @@
 
 
     def __eq__(self, comp):
-        return self.mStart == comp.mStart and self.mEnd == comp.mEnd
+        return self.mStart == comp.mStart and self.getEnd() == comp.getEnd()
 
 
     def __gt__(self, comp):
@@ -68,7 +70,7 @@
 
     def __lt__(self, comp):
         return self.mStart < comp.mStart  \
-            or (self.mStart == comp.mStart) and self.mEnd < comp.mEnd
+            or (self.mStart == comp.mStart) and self.getEnd() < comp.getEnd()
 
 
     @classmethod
@@ -79,21 +81,24 @@
 
 
     def parse(self, data, fullISO=False):
-        splits = data.split('/', 1)
-        if len(splits) == 2:
-            start = splits[0]
-            end = splits[1]
+        try:
+            splits = data.split('/', 1)
+            if len(splits) == 2:
+                start = splits[0]
+                end = splits[1]
 
-            self.mStart.parse(start, fullISO)
-            if end[0] == 'P':
-                self.mDuration.parse(end)
-                self.mUseDuration = True
-                self.mEnd = self.mStart + self.mDuration
+                self.mStart.parse(start, fullISO)
+                if end[0] == 'P':
+                    self.mDuration = Duration.parseText(end)
+                    self.mUseDuration = True
+                    self.mEnd = None
+                else:
+                    self.mEnd.parse(end, fullISO)
+                    self.mUseDuration = False
+                    self.mDuration = None
             else:
-                self.mEnd.parse(end, fullISO)
-                self.mUseDuration = False
-                self.mDuration = self.mEnd - self.mStart
-        else:
+                raise ValueError
+        except IndexError:
             raise ValueError
 
 
@@ -146,10 +151,14 @@
 
 
     def getEnd(self):
+        if self.mEnd is None:
+            self.mEnd = self.mStart + self.mDuration
         return self.mEnd
 
 
     def getDuration(self):
+        if self.mDuration is None:
+            self.mDuration = self.mEnd - self.mStart
         return self.mDuration
 
 
@@ -159,11 +168,15 @@
 
     def setUseDuration(self, use):
         self.mUseDuration = use
+        if self.mUseDuration and self.mDuration is None:
+            self.getDuration()
+        elif not self.mUseDuration and self.mEnd is None:
+            self.getEnd()
 
 
     def isDateWithinPeriod(self, dt):
         # Inclusive start, exclusive end
-        return dt >= self.mStart and dt < self.mEnd
+        return dt >= self.mStart and dt < self.getEnd()
 
 
     def isDateBeforePeriod(self, dt):
@@ -173,17 +186,17 @@
 
     def isDateAfterPeriod(self, dt):
         # Exclusive end
-        return dt >= self.mEnd
+        return dt >= self.getEnd()
 
 
     def isPeriodOverlap(self, p):
         # Inclusive start, exclusive end
-        return not (self.mStart >= p.mEnd or self.mEnd <= p.mStart)
+        return not (self.mStart >= p.getEnd() or self.getEnd() <= p.mStart)
 
 
     def adjustToUTC(self):
         self.mStart.adjustToUTC()
-        self.mEnd.adjustToUTC()
+        self.getEnd().adjustToUTC()
 
 
     def describeDuration(self):

Added: PyCalendar/trunk/src/pycalendar/tests/test_period.py
===================================================================
--- PyCalendar/trunk/src/pycalendar/tests/test_period.py	                        (rev 0)
+++ PyCalendar/trunk/src/pycalendar/tests/test_period.py	2015-04-22 15:58:34 UTC (rev 14695)
@@ -0,0 +1,64 @@
+##
+#    Copyright (c) 2007-2013 Cyrus Daboo. All rights reserved.
+#
+#    Licensed under the Apache License, Version 2.0 (the "License");
+#    you may not use this file except in compliance with the License.
+#    You may obtain a copy of the License at
+#
+#        http://www.apache.org/licenses/LICENSE-2.0
+#
+#    Unless required by applicable law or agreed to in writing, software
+#    distributed under the License is distributed on an "AS IS" BASIS,
+#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#    See the License for the specific language governing permissions and
+#    limitations under the License.
+##
+
+from pycalendar.datetime import DateTime
+from pycalendar.duration import Duration
+from pycalendar.period import Period
+import unittest
+
+class TestPeriod(unittest.TestCase):
+
+    test_data = (
+        "20000101T000000Z/20000101T010000Z",
+        "20000101T000000Z/PT1H",
+    )
+
+    def testParseGenerate(self):
+
+        for result in TestPeriod.test_data:
+            period = Period.parseText(result)
+            self.assertEqual(period.getText(), result)
+
+
+    def testParseBad(self):
+
+        test_bad_data = (
+            "",
+            "ABC",
+            "20000101T000000Z",
+            "20000101T000000Z/",
+            "20000101T000000Z/P",
+            "20000101T000000Z/2000",
+        )
+        for data in test_bad_data:
+            self.assertRaises(ValueError, Period.parseText, data)
+
+
+    def testSetUseDuration(self):
+
+        p1 = Period(
+            start=DateTime(2000, 1, 1, 0, 0, 0),
+            end=DateTime(2000, 1, 1, 1, 0, 0),
+        )
+        p1.setUseDuration(True)
+        self.assertTrue(p1.getText(), "20000101T000000/PT1H")
+
+        p2 = Period(
+            start=DateTime(2000, 1, 1, 0, 0, 0),
+            duration=Duration(hours=1),
+        )
+        p2.setUseDuration(False)
+        self.assertTrue(p2.getText(), "20000101T000000/20000101T010000")

Modified: PyCalendar/trunk/src/pycalendar/timezone.py
===================================================================
--- PyCalendar/trunk/src/pycalendar/timezone.py	2015-04-21 22:54:54 UTC (rev 14694)
+++ PyCalendar/trunk/src/pycalendar/timezone.py	2015-04-22 15:58:34 UTC (rev 14695)
@@ -27,6 +27,7 @@
     """
 
     sDefaultTimezone = None
+    UTCTimezone = None
 
     def __init__(self, utc=None, tzid=None):
 
@@ -127,3 +128,4 @@
             return TimezoneDatabase.getTimezoneDescriptor(self.mTimezone, dt)
 
 Timezone.sDefaultTimezone = Timezone()
+Timezone.UTCTimezone = Timezone(utc=True)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20150422/dfb689b7/attachment-0001.html>


More information about the calendarserver-changes mailing list