# HG changeset patch # User Erik Grinaker # Date 1268069027 -25200 # Node ID e0a34d0b4e612520a02a19e18ab4e3cbcd536a21 # Parent 2ed4905221deb5353760b52d8570832f2baff0f9 use doctest for documentation code examples diff -r 2ed4905221deb5353760b52d8570832f2baff0f9 -r e0a34d0b4e612520a02a19e18ab4e3cbcd536a21 .hgignore --- a/.hgignore Mon Mar 08 23:19:59 2010 +0700 +++ b/.hgignore Tue Mar 09 00:23:47 2010 +0700 @@ -1,5 +1,6 @@ ^build/ ^dist/ +^doc/doctest/ ^doc/doctrees/ ^doc/html/ ^MANIFEST$ diff -r 2ed4905221deb5353760b52d8570832f2baff0f9 -r e0a34d0b4e612520a02a19e18ab4e3cbcd536a21 HACKING --- a/HACKING Mon Mar 08 23:19:59 2010 +0700 +++ b/HACKING Tue Mar 09 00:23:47 2010 +0700 @@ -7,6 +7,7 @@ * Update NEWS file * ./setup.py test * ./setup.py test -p python3.1 + * ./setup.py doctest * hg commit -m "prepare version x.y.z" && hg push * Generate the source distribution diff -r 2ed4905221deb5353760b52d8570832f2baff0f9 -r e0a34d0b4e612520a02a19e18ab4e3cbcd536a21 NEWS --- a/NEWS Mon Mar 08 23:19:59 2010 +0700 +++ b/NEWS Tue Mar 09 00:23:47 2010 +0700 @@ -8,6 +8,10 @@ * Added chrono.DEFAULT_CALENDAR for setting default calendar * Added chrono.DEFAULT_PARSER for setting default parser +Bugfixes: + +* Fixed some errors in the documentation code examples + 2010-02-06: 0.2.0 ================= diff -r 2ed4905221deb5353760b52d8570832f2baff0f9 -r e0a34d0b4e612520a02a19e18ab4e3cbcd536a21 doc/source/conf.py --- a/doc/source/conf.py Mon Mar 08 23:19:59 2010 +0700 +++ b/doc/source/conf.py Tue Mar 09 00:23:47 2010 +0700 @@ -22,6 +22,7 @@ # extensions extensions = [ "sphinx.ext.autodoc", + "sphinx.ext.doctest", ] # document info @@ -32,3 +33,10 @@ pygments_style = "sphinx" html_theme = "default" html_show_sourcelink = False + +# doctest setup +doctest_global_setup = """ +import chrono +import datetime +import time +""" diff -r 2ed4905221deb5353760b52d8570832f2baff0f9 -r e0a34d0b4e612520a02a19e18ab4e3cbcd536a21 doc/source/usage.rst --- a/doc/source/usage.rst Mon Mar 08 23:19:59 2010 +0700 +++ b/doc/source/usage.rst Tue Mar 09 00:23:47 2010 +0700 @@ -36,6 +36,8 @@ attributes :attr:`chrono.Date.year`, :attr:`chrono.Date.month`, and :attr:`chrono.Date.day` will contain the respective date parts:: +.. doctest:: + >>> date = chrono.Date("2009-07-23") >>> date.year 2009 @@ -46,6 +48,8 @@ To retrieve all the attributes at once, use :meth:`chrono.Date.get`:: +.. doctest:: + >>> date = chrono.Date("2009-07-23") >>> date.get() (2009, 7, 23) @@ -53,6 +57,8 @@ The default :class:`chrono.parser.CommonParser` parser handles most normal date formats, such as:: +.. doctest:: + >>> # ISO dates >>> chrono.Date("2009-07-23").get() (2009, 7, 23) @@ -63,6 +69,7 @@ >>> # european dates >>> chrono.Date("23.07.2009").get() + (2009, 7, 23) >>> # ISO week dates >>> chrono.Date("2009-W32").get() @@ -79,6 +86,8 @@ In order to parse all valid date formats for a region, you can pass the proper parser class to :class:`chrono.Date`:: +.. doctest:: + >>> # US dates with two-digit year and no separator >>> chrono.Date("072309", chrono.parser.USParser).get() (2009, 7, 23) @@ -92,17 +101,23 @@ of :exc:`chrono.error.DateError` (such as :exc:`chrono.error.MonthError`) if the date was parsed properly but contained an invalid date value:: +.. doctest:: + >>> date = chrono.Date("xyz") - chrono.error.ParseError: Invalid ISO date value 'xyz' + Traceback (most recent call last): + ParseError: Invalid ISO date value 'xyz' >>> date = chrono.Date("2009-13-27") - chrono.error.MonthError: Month '13' not in range 1-12 + Traceback (most recent call last): + MonthError: Month '13' not in range 1-12 You can also pass a range of non-string inputs to the class, which will be handled according to the object type:: +.. doctest:: + >>> # boolean True indicates the current date - >>> chrono.Date(True).get() + >>> chrono.Date(True).get() # doctest: +SKIP (2010, 1, 23) >>> # integers are interpreted as UNIX timestamps @@ -110,7 +125,7 @@ (2010, 1, 17) >>> # fetch data from time.struct_time objects - >>> chrono.Date(time.localtime()).get() + >>> chrono.Date(time.localtime()).get() # doctest: +SKIP (2010, 1, 23) >>> # fetch data from datetime.date objects @@ -123,6 +138,8 @@ To parse date strings without instantiating a :class:`chrono.Date` object, you can use the parser classes directly:: +.. doctest:: + >>> # parses all supported ISO date formats >>> chrono.parser.ISOParser.parse_date("2009-07-23") (2009, 7, 23) @@ -163,6 +180,8 @@ :class:`chrono.Date` has a number of methods for retreiving calendar-related information about about a date, such as:: +.. doctest:: + >>> # week that contains the date >>> chrono.Date("2009-07-23").week() (2009, 30) @@ -182,11 +201,13 @@ To use the US calendar instead, pass the :class:`chrono.calendar.USCalendar` class to :class:`chrono.Date`:: +.. doctest:: + >>> # US week containing date >>> chrono.Date("2009-07-23", calendar=chrono.calendar.USCalendar).week() (2009, 30) - >>> US weekday of the date + >>> # US weekday of the date >>> chrono.Date("2009-07-23", calendar=chrono.calendar.USCalendar).weekday() 5 @@ -197,6 +218,8 @@ instantiate a :class:`chrono.Date` object, you can use the underlying calendar class directly:: +.. doctest:: + >>> chrono.calendar.ISOCalendar.yeardays(2008) 366 @@ -220,6 +243,8 @@ Here are some examples:: +.. doctest:: + >>> # adding days to a date >>> date = chrono.Date("2009-07-26") >>> date.day += 10 @@ -264,6 +289,8 @@ takes a string containing substitution variables of the form ``$name`` or ``${name}``, and replaces them with actual values:: +.. doctest:: + >>> # full human-readable date >>> chrono.Date("2009-07-23").format("$weekdayname $day. $monthname $year") 'Thursday 23. July 2009' @@ -281,6 +308,8 @@ Date comparisons can be done using the normal Python comparison operators: ``==``, ``!=``, ``>``, and ``<``:: +.. doctest:: + >>> chrono.Date("2009-07-31") == chrono.Date(year = 2009, month = 7, day = 31) True @@ -295,6 +324,8 @@ :class:`time.struct_time` or :class:`datetime.date` objects, and any other value that :class:`chrono.Date` is able to process:: +.. doctest:: + >>> # string with ISO date >>> chrono.Date("2009-07-31") == "2009-07-31" True @@ -312,5 +343,5 @@ False >>> # datetime.date objects - >>> chrono.Date("2009-07-31") < datetime.date(2009, 2, 17) + >>> chrono.Date("2009-07-31") >= datetime.date(2009, 2, 17) True diff -r 2ed4905221deb5353760b52d8570832f2baff0f9 -r e0a34d0b4e612520a02a19e18ab4e3cbcd536a21 setup.cfg --- a/setup.cfg Mon Mar 08 23:19:59 2010 +0700 +++ b/setup.cfg Tue Mar 09 00:23:47 2010 +0700 @@ -3,5 +3,11 @@ build-dir = doc/ fresh-env = True +[doctest] +builder = doctest +build-dir = doc/doctest/ +fresh-env = True +source-dir = doc/source/ + [sdist] formats = bztar,zip diff -r 2ed4905221deb5353760b52d8570832f2baff0f9 -r e0a34d0b4e612520a02a19e18ab4e3cbcd536a21 setup.py --- a/setup.py Mon Mar 08 23:19:59 2010 +0700 +++ b/setup.py Tue Mar 09 00:23:47 2010 +0700 @@ -34,9 +34,10 @@ if "sdist" in sys.argv and not "build_sphinx" in sys.argv: sys.argv.insert(1, "build_sphinx") -if "build_sphinx" in sys.argv: +if "build_sphinx" in sys.argv or "doctest" in sys.argv: from sphinx.setup_command import BuildDoc cmdclass["build_sphinx"] = BuildDoc + cmdclass["doctest"] = BuildDoc class Test(Command):