Commits

Erik Grinaker committed 333b28b

added module constants for default parser and calendar

Comments (0)

Files changed (9)

 
 * Added Date.delta() and DateTime.delta() for difference calculations
 * Added support for julian day numbers
+* Added chrono.DEFAULT_CALENDAR for setting default calendar
+* Added chrono.DEFAULT_PARSER for setting default parser
 
 2010-02-06: 0.2.0
 =================

chrono/__init__.py

 from .datetime import DateTime
 from .error import *
 from .time import Time
+
+DEFAULT_CALENDAR = calendar.ISOCalendar
+DEFAULT_PARSER = parser.CommonParser
 
 from __future__ import absolute_import
 
-from . import calendar as calendarmod
 from . import error
 from . import formatter
-from . import parser as parsermod
 from . import utility
 
+import chrono
 import datetime
 import time
 
 
     Valid values for *date* can be:
 
-    * string: parses date from a string using the given parser (by default
+    * string: parses date from a string using the given parser (defaults
+      to the value of :attr:`chrono.DEFAULT_PARSER`, normally
       :class:`chrono.parser.CommonParser`)
     * **True**: sets the date to the current date
     * integer: assumes input is a UNIX timestamp, sets date accordingly
     If both *date* and keywords are specified, *date* takes precedence.
 
     *parser* determines which parser to use for parsing dates from strings.
-    By default :class:`chrono.parser.CommonParser` is used, which supports
-    the most common date and time formats. See :mod:`chrono.parser` for
-    a list of available parsers.
+    By default the value of :attr:`chrono.DEFAULT_PARSER` is used - normally
+    :class:`chrono.parser.CommonParser`, which supports the most common date
+    and time formats. See :mod:`chrono.parser` for a list of available parsers.
 
     *calendar* determines which calendar to use for calendar operations.
-    By default :class:`chrono.calendar.ISOCalendar` is used, see
+    By default the value of :attr:`chrono.DEFAULT_CALENDAR` is used - normally
+    :class:`chrono.calendar.ISOCalendar`. See
     :mod:`chrono.calendar` for a list of available calendars.
     """
 
 
     def __init__(self, date=None, parser=None, calendar=None, **kwargs):
 
-        self.parser = parser or parsermod.CommonParser
-        self.calendar = calendar or calendarmod.ISOCalendar
+        self.parser = parser or chrono.DEFAULT_PARSER
+        self.calendar = calendar or chrono.DEFAULT_CALENDAR
 
         if isinstance(date, str):
             self.set_string(date)
     def set_string(self, string):
         """
         Sets the date from a string, parsed with the parser set in
-        :attr:`chrono.Date.parser`, by default
+        :attr:`chrono.Date.parser` - by default the parser set in
+        :attr:`chrono.DEFAULT_PARSER`, normally
         :class:`chrono.parser.CommonParser`.
 
         Raises :exc:`chrono.error.ParseError` for invalid input format,

chrono/datetime.py

 
 from __future__ import absolute_import
 
-from . import calendar as calendarmod
 from . import clock
 from . import date
 from . import error
 from . import formatter
-from . import parser as parsermod
 from . import time
 from . import utility
 
+import chrono
 import datetime as datetimemod
 import time as timemod
 
 
     Valid values for *datetime* can be:
 
-    * string: parses date/time from a string using the set parser,
-      :class:`chrono.parser.CommonParser` by default
+    * string: parses date/time from a string using the set parser (defaults
+      to the value of :attr:`chrono.DEFAULT_PARSER`, normally
+      :class:`chrono.parser.CommonParser`)
     * **True**: sets the date/time to the current date
     * integer: assumes input is a UNIX timestamp, sets date/time accordingly
     * :class:`chrono.DateTime`: sets date/time from another DateTime object
     precedence.
 
     *parser* determines which parser to use for parsing dates and times
-    from strings. By default :class:`chrono.parser.CommonParser` is used,
-    which supports the most common date and time formats. See
-    :mod:`chrono.parser` for a list of available parsers.
+    from strings. By default the value of :attr:`chrono.DEFAULT_PARSER` is
+    used - normally :class:`chrono.parser.CommonParser`, which supports the
+    most common date and time formats. See :mod:`chrono.parser` for a list of
+    available parsers.
 
     *calendar* determines which calendar to use for calendar operations.
-    By default :class:`chrono.calendar.ISOCalendar` is used, see
+    By default the value of :attr:`chrono.DEFAULT_CALENDAR` is used - normally
+    :class:`chrono.calendar.ISOCalendar`. See
     :mod:`chrono.calendar` for a list of available calendars.
     """
 
 
     def __init__(self, datetime=None, parser=None, calendar=None, **kwargs):
 
-        self.parser = parser or parsermod.CommonParser
-        self.calendar = calendar or calendarmod.ISOCalendar
+        self.parser = parser or chrono.DEFAULT_PARSER
+        self.calendar = calendar or chrono.DEFAULT_CALENDAR
 
         if isinstance(datetime, str):
             self.set_string(datetime)
 
     def set_string(self, string):
         """
-        Sets the datetime from a string.
+        Sets the datetime from a string, parsed with the parser set in
+        :attr:`chrono.DateTime.parser` - by default the parser set in
+        :attr:`chrono.DEFAULT_PARSER`, normally
+        :class:`chrono.parser.CommonParser`.
 
         Raises :exc:`chrono.error.ParseError` for invalid input format,
         :exc:`TypeError` for invalid input type, and an appropriate
 from . import clock
 from . import error
 from . import formatter
-from . import parser as parsermod
 from . import utility
 
+import chrono
 import datetime
 import time as timemod
 
 
     Valid values for *time* can be:
 
-    * string: parses time from a string, see :class:`chrono.parser.ISOParser`
-      for valid formats
+    * string: parses time from a string using the given parser  (defaults
+      to the value of :attr:`chrono.DEFAULT_PARSER`, normally
+      :class:`chrono.parser.CommonParser`)
     * **True**: sets the time to the current time
     * :class:`chrono.Time`: sets time from another Time object
     * :class:`datetime.datetime`: sets time from a :class:`datetime.datetime`
     If both *time* and keywords are specified, *time* takes precedence.
 
     *parser* determines which parser to use for parsing times from strings.
-    By default :class:`chrono.parser.CommonParser` is used, which supports the
-    most common time formats. See :mod:`chrono.parser` for a list of available
-    parsers.
+    By default the value of :attr:`chrono.DEFAULT_PARSER` is used - normally
+    :class:`chrono.parser.CommonParser`, which supports the most common date
+    and time formats. See :mod:`chrono.parser` for a list of available parsers.
     """
 
     hour = None
 
     def __init__(self, time=None, parser=None, **kwargs):
 
-        self.parser = parser or parsermod.CommonParser
+        self.parser = parser or chrono.DEFAULT_PARSER
 
         if isinstance(time, str):
             self.set_string(time)
     def set_string(self, string):
         """
         Sets the time from a string, parsed with the parser set in
-        :attr:`chrono.Date.parser`, by default
+        :attr:`chrono.Date.parser` - by default the parser set in
+        :attr:`chrono.DEFAULT_PARSER`, normally
         :class:`chrono.parser.CommonParser`.
 
         Raises :exc:`chrono.error.ParseError` for invalid input format,

doc/source/usage.rst

 Dates can be specified using either ISO, US, or european formats. Lists
 of valid formats are available in the :mod:`chrono.parser` documentation.
 
-By default, python-chrono uses the parser :class:`chrono.parser.CommonParser`,
+By default, python-chrono uses the parser set in
+:attr:`chrono.DEFAULT_PARSER` - normally :class:`chrono.parser.CommonParser`,
 which accepts the most commonly used date formats. The notable exceptions
 are formats without separators (which for example can be interpreted as
 either US or european dates), and unusual separators such as . in US dates
 (which is the standard separator in Europe). In order to parse such formats,
-you need to pass the proper parser to :class:`chrono.Date`.
+you need to either set another default in :attr:`chrono.DEFAULT_PARSER`, or
+pass the proper parser to :class:`chrono.Date`.
 
 Date parsing is done simply by instantiating a :class:`chrono.Date` object,
 passing the date string to be parsed as input. Once instantiated, the
  * Weeks start on Sunday
  * The first week of a year is the week which contains January 1st
 
-By default, the ISO calendar is used. As can be seen above, this only
+By default the calendar set in :attr:`chrono.DEFAULT_CALENDAR` is used,
+normally :class:`chrono.calendar.ISOCalendar`. To use another calendar,
+either set it as the default in :attr:`chrono.DEFAULT_CALENDAR`, or pass
+the proper calendar to :class:`chrono.Date`. As can be seen above, this only
 affects functionality related to week numbers or week days.
 
 :class:`chrono.Date` has a number of methods for retreiving calendar-related

tests/test_date.py

 
 class Date__initTest(unittest.TestCase):
 
+    def setUp(self):
+        unittest.TestCase.setUp(self)
+
+        self.default_calendar = chrono.DEFAULT_CALENDAR
+        self.default_parser = chrono.DEFAULT_PARSER
+
+    def tearDown(self):
+        unittest.TestCase.tearDown(self)
+
+        chrono.DEFAULT_CALENDAR = self.default_calendar
+        chrono.DEFAULT_PARSER = self.default_parser
+
     def test_calendar(self):
         "Date.__init__() takes calendar as input"
 
 
         self.assertEqual(chrono.Date().calendar, chrono.calendar.ISOCalendar)
 
+    def test_calendar_default_custom(self):
+        "Date.__init__() uses chrono.DEFAULT_CALENDAR"
+
+        chrono.DEFAULT_CALENDAR = chrono.calendar.USCalendar
+
+        self.assertEqual(chrono.Date().calendar, chrono.calendar.USCalendar)
+
     def test_date(self):
         "Date.__init__() with Date instance parameter copies attributes"
 
     def test_parser_default(self):
         "Date.__init__() defaults to CommonParser"
 
-        d = chrono.Date("2009-07-23")
+        self.assertEqual(chrono.Date().parser, chrono.parser.CommonParser)
 
-        self.assertEqual(d.get(), (2009, 7, 23))
-        self.assertEqual(d.parser, chrono.parser.CommonParser)
+    def test_parser_default_custom(self):
+        "Date.__init__() uses chrono.DEFAULT_PARSER"
+
+        chrono.DEFAULT_PARSER = chrono.parser.ISOParser
+
+        self.assertEqual(chrono.Date().parser, chrono.parser.ISOParser)
 
     def test_string(self):
         "Date.__init__() parses strings"

tests/test_datetime.py

 
 class DateTime__initTest(unittest.TestCase):
 
+    def setUp(self):
+        unittest.TestCase.setUp(self)
+
+        self.default_calendar = chrono.DEFAULT_CALENDAR
+        self.default_parser = chrono.DEFAULT_PARSER
+
+    def tearDown(self):
+        unittest.TestCase.tearDown(self)
+
+        chrono.DEFAULT_CALENDAR = self.default_calendar
+        chrono.DEFAULT_PARSER = self.default_parser
+
     def test_calendar(self):
         "DateTime.__init__() takes calendar as input"
 
             chrono.DateTime().calendar, chrono.calendar.ISOCalendar
         )
 
+    def test_calendar_default_custom(self):
+        "DateTime.__init__() uses chrono.DEFAULT_CALENDAR"
+
+        chrono.DEFAULT_CALENDAR = chrono.calendar.USCalendar
+
+        self.assertEqual(
+            chrono.DateTime().calendar, chrono.calendar.USCalendar
+        )
+
     def test_date(self):
         "DateTime.__init__() with Date parameter copies attributes"
 
     def test_parser_default(self):
         "DateTime.__init__() defaults to CommonParser"
 
-        d = chrono.DateTime("2009-07-23 16:27:43")
+        self.assertEqual(chrono.DateTime().parser, chrono.parser.CommonParser)
 
-        self.assertEqual(d.get(), (2009, 7, 23, 16, 27, 43))
-        self.assertEqual(d.parser, chrono.parser.CommonParser)
+    def test_parser_default_custom(self):
+        "DateTime.__init__() uses chrono.DEFAULT_PARSER"
+
+        chrono.DEFAULT_PARSER = chrono.parser.ISOParser
+
+        self.assertEqual(chrono.DateTime().parser, chrono.parser.ISOParser)
 
     def test_string(self):
         "DateTime.__init__() parses strings"

tests/test_time.py

 
 class Time__initTest(unittest.TestCase):
 
+    def setUp(self):
+        unittest.TestCase.setUp(self)
+
+        self.default_calendar = chrono.DEFAULT_CALENDAR
+        self.default_parser = chrono.DEFAULT_PARSER
+
+    def tearDown(self):
+        unittest.TestCase.tearDown(self)
+
+        chrono.DEFAULT_CALENDAR = self.default_calendar
+        chrono.DEFAULT_PARSER = self.default_parser
+
     def test_datetime_datetime(self):
         "Time.__init__() copies time from datetime.datetime objects"
 
     def test_parser_default(self):
         "Time.__init__() defaults to CommonParser"
 
-        t = chrono.Time("16:27:43")
+        self.assertEqual(chrono.Time().parser, chrono.parser.CommonParser)
 
-        self.assertEqual(t.get(), (16, 27, 43))
-        self.assertEqual(t.parser, chrono.parser.CommonParser)
+    def test_parser_default_custom(self):
+        "Time.__init__() uses chrono.DEFAULT_PARSER"
+
+        chrono.DEFAULT_PARSER = chrono.parser.ISOParser
+
+        self.assertEqual(chrono.Time().parser, chrono.parser.ISOParser)
 
     def test_string(self):
         "Time.__init__() parses strings using the parser"
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.