SlideShare a Scribd company logo
1 of 13
Download to read offline
doctest
Jenny JS Liang (jsliang)
PyConTW 2013
It all began with MoSQL…(thank you
Mosky!)
● http://mosql.mosky.tw/
● 2013年的春節,我在GitHub上看到
Mosky開始著手進行MoSQL……
● 3月的Taipei.py聚會,我問 Mosky:「為
什麼妳的MoSQL文件可以寫得這麼
快?」
● Mosky:「因為我用doctest +
Sphinx!」
A simple sample…
# add.py
def add(a, b):
"""
This is an addition function.
>>> add(1,2)
3
>>> add("Hello", "World")
'HelloWorld'
"""
return a+b
$ python -m doctest -v add.py
Trying:
add(1,2)
Expecting:
3
ok
…
1 items passed all tests:
2 tests in add.add
2 tests in 2 items.
2 passed and 0 failed.
Test passed.
Multilines
# multilines.py
def multilines(input_str):
"""
>>> input_str = '''1st
... 2nd
... 3rd'''
>>> multilines(input_str)
1st
2nd
3rd
"""
print input_str
$ python -m doctest -v 
multilines.py
Trying:
input_str = '''1st
2nd
3rd'''
Expecting nothing
ok
Trying:
prnt_multilines(input_str)
...
Test passed.
Handling blank lines…
# blank.py
def blankline(line1, line2):
"""
Print "line1nnline2".
>>> blankline("Hello", "World")
Hello
<BLANKLINE>
World
"""
print line1
print ""
print line2
$ python -m doctest -v 
blank.py
Trying:
blankline("Hello", "World")
Expecting:
Hello
<BLANKLINE>
World
ok
...
Test passed.
Checking Examples in Docstrings
End your module with the following code:
if __name__ == "__main__":
import doctest
doctest.testmod()
and execute:
$ python <module_name>.py
Autodocumenting
with Sphinx
Module Structure
● mymodule/
○ __init__.py
○ add.py
○ blank.py
○ multilines.py
Make sure mymodule is accessible:
$ export PYTHONPATH=$PYTHONPATH:
/path/to/
mymodule
Modify index.rst (use ReST)
Welcome to mymodule's documentation!
====================================
.. toctree::
:maxdepth: 2
.. automodule:: mymodule.add
:members:
.. automodule:: mymodule.blank
:members:
Generating documentation with
Sphinx
1. $ pip install sphinx
2. $ sphinx-quickstart
3. Follow the instructions. When you see
Please indicate if you want to use one of the
following Sphinx extensions:
> autodoc: automatically insert
docstrings from modules (y/N) [n]: y
select "y"!
4. modify index.rst (see the previous slide)
5. $ make html
More information of Sphinx...
● Sphinx http://sphinx-doc.org/
● reStructuredText http://docutils.sourceforge.
net/rst.html
● sphinx.ext.autodoc – Include documentation
from docstrings http://sphinx-doc.
org/ext/autodoc.html
○ .. automodule::
○ .. autoclass::
○ .. autoexception::
○ ...
References
● Python doctest
○ http://docs.python.org/3/library/doctest.html
● Documenting Your Project Using Sphinx
○ http://pythonhosted.
org/an_example_pypi_project/sphinx.html
● sphinx.ext.autodoc – Include documentation
from docstrings
○ http://sphinx-doc.org/ext/autodoc.html
● Minimal Sphinx setup for autodocumenting
Python modules
○ http://scienceoss.com/minimal-sphinx-setup-for-
autodocumenting-python-modules/

More Related Content

Viewers also liked

影像好日子隨手拍
影像好日子隨手拍 影像好日子隨手拍
影像好日子隨手拍 小華 黃
 
Time Series Estimation of Gas Furnace Data in IMPL and CPLEX Industrial Model...
Time Series Estimation of Gas Furnace Data in IMPL and CPLEX Industrial Model...Time Series Estimation of Gas Furnace Data in IMPL and CPLEX Industrial Model...
Time Series Estimation of Gas Furnace Data in IMPL and CPLEX Industrial Model...Alkis Vazacopoulos
 
Презентація команди Васильківського НВК № 4
Презентація команди Васильківського НВК № 4Презентація команди Васильківського НВК № 4
Презентація команди Васильківського НВК № 408600 Vasilkov
 
Emerging Mobile Trends & Behaviors for 2013
Emerging Mobile Trends & Behaviors for 2013Emerging Mobile Trends & Behaviors for 2013
Emerging Mobile Trends & Behaviors for 2013Paul Booth
 
Human Development
Human DevelopmentHuman Development
Human DevelopmentEnival
 
Gcse sci-f-b1
Gcse sci-f-b1Gcse sci-f-b1
Gcse sci-f-b1opsonise
 
Three point bend deflector slide presentation
Three point bend deflector slide presentationThree point bend deflector slide presentation
Three point bend deflector slide presentationschomp
 
Arthst2 mat techniques_porcelainlacqueredwood_final
Arthst2 mat techniques_porcelainlacqueredwood_finalArthst2 mat techniques_porcelainlacqueredwood_final
Arthst2 mat techniques_porcelainlacqueredwood_finalall_abby
 
Improve Yield Accounting by including Density Measurements Explicitly
Improve Yield Accounting by including Density Measurements ExplicitlyImprove Yield Accounting by including Density Measurements Explicitly
Improve Yield Accounting by including Density Measurements ExplicitlyAlkis Vazacopoulos
 
Thong diep cho cuoc song
Thong diep cho cuoc songThong diep cho cuoc song
Thong diep cho cuoc songlethao1491
 
Karate Training in Universal City
Karate Training in Universal CityKarate Training in Universal City
Karate Training in Universal Citykaratedojo2
 
Latihan bab6 cikgugeog
Latihan bab6 cikgugeogLatihan bab6 cikgugeog
Latihan bab6 cikgugeogKila Shakila
 

Viewers also liked (20)

影像好日子隨手拍
影像好日子隨手拍 影像好日子隨手拍
影像好日子隨手拍
 
Time Series Estimation of Gas Furnace Data in IMPL and CPLEX Industrial Model...
Time Series Estimation of Gas Furnace Data in IMPL and CPLEX Industrial Model...Time Series Estimation of Gas Furnace Data in IMPL and CPLEX Industrial Model...
Time Series Estimation of Gas Furnace Data in IMPL and CPLEX Industrial Model...
 
Dsl public
Dsl publicDsl public
Dsl public
 
Intropdf
IntropdfIntropdf
Intropdf
 
Презентація команди Васильківського НВК № 4
Презентація команди Васильківського НВК № 4Презентація команди Васильківського НВК № 4
Презентація команди Васильківського НВК № 4
 
Emerging Mobile Trends & Behaviors for 2013
Emerging Mobile Trends & Behaviors for 2013Emerging Mobile Trends & Behaviors for 2013
Emerging Mobile Trends & Behaviors for 2013
 
Human Development
Human DevelopmentHuman Development
Human Development
 
Mem bhee1
Mem bhee1Mem bhee1
Mem bhee1
 
Big europian cities
Big europian citiesBig europian cities
Big europian cities
 
Gcse sci-f-b1
Gcse sci-f-b1Gcse sci-f-b1
Gcse sci-f-b1
 
Manual arranque dual
Manual arranque dualManual arranque dual
Manual arranque dual
 
Three point bend deflector slide presentation
Three point bend deflector slide presentationThree point bend deflector slide presentation
Three point bend deflector slide presentation
 
Arthst2 mat techniques_porcelainlacqueredwood_final
Arthst2 mat techniques_porcelainlacqueredwood_finalArthst2 mat techniques_porcelainlacqueredwood_final
Arthst2 mat techniques_porcelainlacqueredwood_final
 
Af cat agricola
Af cat agricolaAf cat agricola
Af cat agricola
 
Improve Yield Accounting by including Density Measurements Explicitly
Improve Yield Accounting by including Density Measurements ExplicitlyImprove Yield Accounting by including Density Measurements Explicitly
Improve Yield Accounting by including Density Measurements Explicitly
 
Thong diep cho cuoc song
Thong diep cho cuoc songThong diep cho cuoc song
Thong diep cho cuoc song
 
Karate Training in Universal City
Karate Training in Universal CityKarate Training in Universal City
Karate Training in Universal City
 
4524
45244524
4524
 
El eclipse
El eclipseEl eclipse
El eclipse
 
Latihan bab6 cikgugeog
Latihan bab6 cikgugeogLatihan bab6 cikgugeog
Latihan bab6 cikgugeog
 

Similar to [PyConTW 2013] doctest

Writing Commits for You, Your Friends, and Your Future Self
Writing Commits for You, Your Friends, and Your Future SelfWriting Commits for You, Your Friends, and Your Future Self
Writing Commits for You, Your Friends, and Your Future SelfAll Things Open
 
Test First Teaching
Test First TeachingTest First Teaching
Test First TeachingSarah Allen
 
Lecture for Bootstrap and flask in Python
Lecture for Bootstrap and flask in PythonLecture for Bootstrap and flask in Python
Lecture for Bootstrap and flask in PythonNaoki Watanabe
 
Polyglot automation - QA Fest - 2015
Polyglot automation - QA Fest - 2015Polyglot automation - QA Fest - 2015
Polyglot automation - QA Fest - 2015Iakiv Kramarenko
 
QA Fest 2015. Яков Крамаренко. Polyglot automation
QA Fest 2015. Яков Крамаренко. Polyglot automation QA Fest 2015. Яков Крамаренко. Polyglot automation
QA Fest 2015. Яков Крамаренко. Polyglot automation QAFest
 
ES3-2020-P3 TDD Calculator
ES3-2020-P3 TDD CalculatorES3-2020-P3 TDD Calculator
ES3-2020-P3 TDD CalculatorDavid Rodenas
 
node.js, javascript and the future
node.js, javascript and the futurenode.js, javascript and the future
node.js, javascript and the futureJeff Miccolis
 
Joker'15 Java straitjackets for MongoDB
Joker'15 Java straitjackets for MongoDBJoker'15 Java straitjackets for MongoDB
Joker'15 Java straitjackets for MongoDBAlexey Zinoviev
 
From Java to Kotlin - The first month in practice
From Java to Kotlin - The first month in practiceFrom Java to Kotlin - The first month in practice
From Java to Kotlin - The first month in practiceStefanTomm
 
Test First Teaching and the path to TDD
Test First Teaching and the path to TDDTest First Teaching and the path to TDD
Test First Teaching and the path to TDDSarah Allen
 
Data processing with celery and rabbit mq
Data processing with celery and rabbit mqData processing with celery and rabbit mq
Data processing with celery and rabbit mqJeff Peck
 
Intro to JavaScript Testing
Intro to JavaScript TestingIntro to JavaScript Testing
Intro to JavaScript TestingRan Mizrahi
 
Nodejs functions & modules
Nodejs functions & modulesNodejs functions & modules
Nodejs functions & modulesmonikadeshmane
 
Node.js - JavaScript Chicago Meetup
Node.js - JavaScript Chicago MeetupNode.js - JavaScript Chicago Meetup
Node.js - JavaScript Chicago Meetuphugs
 

Similar to [PyConTW 2013] doctest (20)

Writing Commits for You, Your Friends, and Your Future Self
Writing Commits for You, Your Friends, and Your Future SelfWriting Commits for You, Your Friends, and Your Future Self
Writing Commits for You, Your Friends, and Your Future Self
 
Easy automation.py
Easy automation.pyEasy automation.py
Easy automation.py
 
Test First Teaching
Test First TeachingTest First Teaching
Test First Teaching
 
Lecture for Bootstrap and flask in Python
Lecture for Bootstrap and flask in PythonLecture for Bootstrap and flask in Python
Lecture for Bootstrap and flask in Python
 
Spock
SpockSpock
Spock
 
Polyglot automation - QA Fest - 2015
Polyglot automation - QA Fest - 2015Polyglot automation - QA Fest - 2015
Polyglot automation - QA Fest - 2015
 
QA Fest 2015. Яков Крамаренко. Polyglot automation
QA Fest 2015. Яков Крамаренко. Polyglot automation QA Fest 2015. Яков Крамаренко. Polyglot automation
QA Fest 2015. Яков Крамаренко. Polyglot automation
 
ES3-2020-P3 TDD Calculator
ES3-2020-P3 TDD CalculatorES3-2020-P3 TDD Calculator
ES3-2020-P3 TDD Calculator
 
node.js, javascript and the future
node.js, javascript and the futurenode.js, javascript and the future
node.js, javascript and the future
 
Database connectivity in python
Database connectivity in pythonDatabase connectivity in python
Database connectivity in python
 
Joker'15 Java straitjackets for MongoDB
Joker'15 Java straitjackets for MongoDBJoker'15 Java straitjackets for MongoDB
Joker'15 Java straitjackets for MongoDB
 
Testing with PostgreSQL
Testing with PostgreSQLTesting with PostgreSQL
Testing with PostgreSQL
 
50 Ways To Love Your Project
50 Ways To Love Your Project50 Ways To Love Your Project
50 Ways To Love Your Project
 
From Java to Kotlin - The first month in practice
From Java to Kotlin - The first month in practiceFrom Java to Kotlin - The first month in practice
From Java to Kotlin - The first month in practice
 
Test First Teaching and the path to TDD
Test First Teaching and the path to TDDTest First Teaching and the path to TDD
Test First Teaching and the path to TDD
 
Data processing with celery and rabbit mq
Data processing with celery and rabbit mqData processing with celery and rabbit mq
Data processing with celery and rabbit mq
 
Intro to JavaScript Testing
Intro to JavaScript TestingIntro to JavaScript Testing
Intro to JavaScript Testing
 
Nodejs functions & modules
Nodejs functions & modulesNodejs functions & modules
Nodejs functions & modules
 
Node.js - JavaScript Chicago Meetup
Node.js - JavaScript Chicago MeetupNode.js - JavaScript Chicago Meetup
Node.js - JavaScript Chicago Meetup
 
Lightning talk- testing
Lightning talk- testingLightning talk- testing
Lightning talk- testing
 

Recently uploaded

Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 

Recently uploaded (20)

Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 

[PyConTW 2013] doctest

  • 1. doctest Jenny JS Liang (jsliang) PyConTW 2013
  • 2. It all began with MoSQL…(thank you Mosky!) ● http://mosql.mosky.tw/ ● 2013年的春節,我在GitHub上看到 Mosky開始著手進行MoSQL…… ● 3月的Taipei.py聚會,我問 Mosky:「為 什麼妳的MoSQL文件可以寫得這麼 快?」 ● Mosky:「因為我用doctest + Sphinx!」
  • 3. A simple sample… # add.py def add(a, b): """ This is an addition function. >>> add(1,2) 3 >>> add("Hello", "World") 'HelloWorld' """ return a+b $ python -m doctest -v add.py Trying: add(1,2) Expecting: 3 ok … 1 items passed all tests: 2 tests in add.add 2 tests in 2 items. 2 passed and 0 failed. Test passed.
  • 4. Multilines # multilines.py def multilines(input_str): """ >>> input_str = '''1st ... 2nd ... 3rd''' >>> multilines(input_str) 1st 2nd 3rd """ print input_str $ python -m doctest -v multilines.py Trying: input_str = '''1st 2nd 3rd''' Expecting nothing ok Trying: prnt_multilines(input_str) ... Test passed.
  • 5. Handling blank lines… # blank.py def blankline(line1, line2): """ Print "line1nnline2". >>> blankline("Hello", "World") Hello <BLANKLINE> World """ print line1 print "" print line2 $ python -m doctest -v blank.py Trying: blankline("Hello", "World") Expecting: Hello <BLANKLINE> World ok ... Test passed.
  • 6. Checking Examples in Docstrings End your module with the following code: if __name__ == "__main__": import doctest doctest.testmod() and execute: $ python <module_name>.py
  • 8.
  • 9. Module Structure ● mymodule/ ○ __init__.py ○ add.py ○ blank.py ○ multilines.py Make sure mymodule is accessible: $ export PYTHONPATH=$PYTHONPATH: /path/to/ mymodule
  • 10. Modify index.rst (use ReST) Welcome to mymodule's documentation! ==================================== .. toctree:: :maxdepth: 2 .. automodule:: mymodule.add :members: .. automodule:: mymodule.blank :members:
  • 11. Generating documentation with Sphinx 1. $ pip install sphinx 2. $ sphinx-quickstart 3. Follow the instructions. When you see Please indicate if you want to use one of the following Sphinx extensions: > autodoc: automatically insert docstrings from modules (y/N) [n]: y select "y"! 4. modify index.rst (see the previous slide) 5. $ make html
  • 12. More information of Sphinx... ● Sphinx http://sphinx-doc.org/ ● reStructuredText http://docutils.sourceforge. net/rst.html ● sphinx.ext.autodoc – Include documentation from docstrings http://sphinx-doc. org/ext/autodoc.html ○ .. automodule:: ○ .. autoclass:: ○ .. autoexception:: ○ ...
  • 13. References ● Python doctest ○ http://docs.python.org/3/library/doctest.html ● Documenting Your Project Using Sphinx ○ http://pythonhosted. org/an_example_pypi_project/sphinx.html ● sphinx.ext.autodoc – Include documentation from docstrings ○ http://sphinx-doc.org/ext/autodoc.html ● Minimal Sphinx setup for autodocumenting Python modules ○ http://scienceoss.com/minimal-sphinx-setup-for- autodocumenting-python-modules/