This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (pypi.python.org).
Help us improve Python packaging - Donate today!
Project Description

doctest (and recently manuel) provide hooks for using custom doctest parsers. zc.customdoctests helps to leverage this to support other languages, such as JavaScript:

js> function double (x) {
...     return x*2;
... }
js> double(2)
4

And with manuel, it facilitates doctests that mix multiple languages, such as Python, JavaScript, and sh.

Detailed documentation

Custom doctest parsers

zc.customdoctests provides a little bit of help with creating custom doctest parsers that work pretty muct like regular doctests, but that use an alternate means of evaluating examples. To use it, you call zc.customdoctests.DocTestParser and pass any of the following options:

ps1

The first-line prompt, which defaultd to '>>>'.

This must be a regular expression that matches exactly 3 characters.

(Note that you can’t override the second-line prompt.)

comment_prefix
The comment prefix regular expression, which defaults to ‘#’.
transform
A function used to transform example source, which defaults to a no-operation function.

The js module provides support for using JavaScript in doctests using python-spidermonkey. It provides some examples of defining custom doctest parsers.

Javascript and Python-Spidermonkey support

To wire this up, you’d use something like:

import doctest, zc.customdoctests.js

test_suite = doctest.DocTestSuite(
    parser=zc.customdoctests.js.parser,
    setUp=zc.customdoctests.js.spidermonkeySetUp)

Or, with manuel:

test_suite = manuel.testing.TestSuite(
    manuel.doctest.Manuel(parser=zc.customdoctests.js.parser) +
    manuel.doctest.Manuel(parser=zc.customdoctests.js.eq_parser) +
    manuel.doctest.Manuel() +
    manuel.capture.Manuel(),
    'spidermonkey.txt',
    setUp=zc.customdoctests.js.spidermonkeySetUp)

Note that zc.customdoctests doesn’t require spidermonkey, so you need to install spidermonkey seperately if you want to use it.

An advantage of using manuel is that you can use multiple parsers in the same document. In the example, above, 2 javascript example syntaxes (described below) as well as the standard doctest syntax are supported. This document is run with manuel to allow all 3 syntaxes.

For the rest of this document, we’ll show examples of JavaScript doctests as well as helper APIs used to support JavaScript and to integrate JavaScript and Python.

Javascript doctests use a “js>” prompt (as used in rhino and the spidermonkey interpreter):

js> 2 +
... 'Hi world' // doctest: +ELLIPSIS
u'2Hi...

Assignments return values. This can generate annoying output in doctests:

js> ob = {a: 1, b: 2}
[object Object]

If you’re using manuel, you can avoid this by using js!:

js! x = 3

which suppresses expression values.

load and print functions (similar to those found in rhino) are provided. For example, given a javascript file, double.js:

function double (x) {
    return x*2;
}

We can load the file:

js> load('double.js')
js> double(10)
20

We can print values:

js> print('Hi')
Hi

A python object provides access to the open function and the os module:

js> python.os.path.exists('double.js')
True

js! f = python.open('double.js')
js> print(f.read())
function double (x) {
    return x*2;
}
<BLANKLINE>

js> f.close()

If you’re using manuel, you can intermix Python and and JavaScript examples and there are a number of APIs to facilitate using Python and JavaScript together.

There’s an add_js_global function to copy data from Python:

>>> add_js_global('y', 1)

js> y
1

There’s also a js object that provides attribute access to js globals:

>>> js.x
3

>>> js.z = 4

js> z
4

You can also call this to run JS code without returning the resulting value:

>>> js('a = x + y')

js> a
4

Changelog

1.0.1 (2013-02-14)

  • Fixed ReStructuredText errors on the PyPI page.

1.0.0 (2013-02-13)

  • Added Python 3.3 support.
  • Cleanup setup.py, add tox.ini and manifest.

0.1.0 (2011-05-19)

  • Initial release
Release History

Release History

1.0.1

This version

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

1.0.0

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.1.0

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

Download Files

Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
zc.customdoctests-1.0.1.zip (15.8 kB) Copy SHA256 Checksum SHA256 Source Feb 14, 2013

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting