Skip to content

Commit

Permalink
add demos for issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Erotemic committed Sep 30, 2021
1 parent afaf99e commit 687b748
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
14 changes: 14 additions & 0 deletions dev/demo_dynamic_analysis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""
CommandLine:
xdoctest = ~/code/xdoctest/dev/demo_dynamic_analysis.py
"""


def func() -> None:
r''' Dynamic doctest
>>> %s
%s
'''
return

func.__doc__ %= ('print(1)', '1')
66 changes: 66 additions & 0 deletions dev/demo_usage_with_logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# -*- coding: utf-8 -*-
"""
demo_usage_with_logger.py
Script to demo a workaround to [Issue111]_.
CommandLine:
# Run with xdoctest runner
xdoctest ~/code/xdoctest/dev/demo_usage_with_logger.py
# Run with pytest runner
pytest -s --xdoctest --xdoctest-verbose=3 ~/code/xdoctest/dev/demo_usage_with_logger.py
# Run with builtin main
python ~/code/xdoctest/dev/demo_usage_with_logger.py
References:
.. [Issue111] https://github.com/Erotemic/xdoctest/issues/111
"""
import logging
import sys


class StreamHandler2(logging.StreamHandler):
def __init__(self, _getstream=None):
"""
Initialize the handler.
If stream is not specified, sys.stderr is used.
"""
logging.Handler.__init__(self)
if _getstream is None:
_getstream = lambda: sys.stderr # NOQA
self._getstream = _getstream
self.__class__.stream = property(lambda self: self._getstream())

def setStream(self, stream):
raise NotImplementedError


handler = StreamHandler2(lambda: sys.stdout)

_log = logging.getLogger('mylog')
_log.setLevel(logging.INFO)
_log.addHandler(handler)

_log.info('hello')
_log.info('hello hello')


def func_with_doctest():
"""
Example:
>>> _log.info('greetings from my doctest')
greetings from my doctest
"""


def main():
import xdoctest
xdoctest.doctest_callable(func_with_doctest)


if __name__ == '__main__':
main()

0 comments on commit 687b748

Please sign in to comment.