[issue36654] Add example to tokenize.tokenize
New submission from Windson Yang <wiwindson@outlook.com>:
The tokenize() generator requires one argument, readline, which must be a callable object which provides the same interface as the io.IOBase.readline() method of file objects. Each call to the function should return one line of input as bytes.
Add an example like this should be easier to understand: # example.py def foo: pass # tokenize_example.py import tokenize f = open('example.py', 'rb') token_gen = tokenize.tokenize(f.readline) for token in token_gen: # Something like this # TokenInfo(type=1 (NAME), string='class', start=(1, 0), end=(1, 5), line='class Foo:\n') # TokenInfo(type=1 (NAME), string='Foo', start=(1, 6), end=(1, 9), line='class Foo:\n') # TokenInfo(type=53 (OP), string=':', start=(1, 9), end=(1, 10), line='class Foo:\n') print(token) ---------- assignee: docs@python components: Documentation messages: 340467 nosy: Windson Yang, docs@python priority: normal severity: normal status: open title: Add example to tokenize.tokenize type: enhancement versions: Python 3.5, Python 3.6, Python 3.7, Python 3.8 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue36654> _______________________________________
Lisa Roach <lisaroach14@gmail.com> added the comment: This could be added to the examples section of the tokenize doc, would you want to make the PR Windson? ---------- nosy: +lisroach _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue36654> _______________________________________
Windson Yang <wiwindson@outlook.com> added the comment: Yes, I can make a PR for it. ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue36654> _______________________________________
Change by Windson Yang <wiwindson@outlook.com>: ---------- keywords: +patch pull_requests: +12871 stage: -> patch review _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue36654> _______________________________________
Serhiy Storchaka <storchaka+cpython@gmail.com> added the comment: I do not think a new example is needed. The existing example already demonstrates the use of file's readline method. If you need an example for opening a file, the tokenize module documentation is not an appropriate place for this. ---------- nosy: +serhiy.storchaka _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue36654> _______________________________________
Berker Peksag <berker.peksag@gmail.com> added the comment: New changeset 4b09dc79f4d08d85f2cc945563e9c8ef1e531d7b by Berker Peksag (Windson yang) in branch 'master': bpo-36654: Add examples for using tokenize module programmically (#12947) https://github.com/python/cpython/commit/4b09dc79f4d08d85f2cc945563e9c8ef1e5... ---------- nosy: +berker.peksag _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue36654> _______________________________________
Change by miss-islington <mariatta.wijaya+miss-islington@gmail.com>: ---------- pull_requests: +17570 pull_request: https://github.com/python/cpython/pull/18187 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue36654> _______________________________________
Change by miss-islington <mariatta.wijaya+miss-islington@gmail.com>: ---------- pull_requests: +17571 pull_request: https://github.com/python/cpython/pull/18188 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue36654> _______________________________________
Berker Peksag <berker.peksag@gmail.com> added the comment: New changeset 1cf0df4f1bcc38dfd70a152af20cf584de531ea7 by Berker Peksag (Miss Islington (bot)) in branch '3.8': bpo-36654: Add examples for using tokenize module programmatically (GH-18187) https://github.com/python/cpython/commit/1cf0df4f1bcc38dfd70a152af20cf584de5... ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue36654> _______________________________________
Berker Peksag <berker.peksag@gmail.com> added the comment: New changeset 6dbd843dedc9e05c0e3f4714294837f0a83deebe by Berker Peksag (Miss Islington (bot)) in branch '3.7': bpo-36654: Add examples for using tokenize module programmatically (GH-12947) https://github.com/python/cpython/commit/6dbd843dedc9e05c0e3f4714294837f0a83... ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue36654> _______________________________________
Berker Peksag <berker.peksag@gmail.com> added the comment: Wow, I managed to make typos in all three commits! PR 12947 has some discussion about why adding these examples would be a good idea as we now have two different APIs for unicode and bytes input. Thanks for the PR, Windson. ---------- resolution: -> fixed stage: patch review -> resolved status: open -> closed versions: +Python 3.9 -Python 3.5, Python 3.6 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue36654> _______________________________________
participants (5)
-
Berker Peksag -
Lisa Roach -
miss-islington -
Serhiy Storchaka -
Windson Yang