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

2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
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
 
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
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
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
 
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
 
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
 
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
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
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
 
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
 
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
 

Recently uploaded (20)

2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
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...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
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
 
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...
 
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...
 
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
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
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
 
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
 
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
 

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