-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |