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

SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentationphoebematthew05
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 

Recently uploaded (20)

SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort ServiceHot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentation
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 

[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/