Visitar URL original
bpo-36654: add example to generate token from another file by Windsooon · Pull Request #12947 · python/cpython · GitHub
The Wayback Machine - https://web.archive.org/web/20221223112653/https://github.com/python/cpython/pull/12947
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-36654: add example to generate token from another file #12947

Merged
merged 4 commits into from Jan 25, 2020

Conversation

Windsooon
Copy link
Contributor

@Windsooon Windsooon commented Apr 25, 2019 •

Add example to generate token from another file.

https://bugs.python.org/issue36654

Copy link
Contributor

@eamanu eamanu left a comment

This example confuse me, because the file example.py does not exist. Maybe you need to do something like hello.py

Doc/library/tokenize.rst Outdated Show resolved Hide resolved
Example of tokenizing from another file::

import tokenize
f = open('example.py', 'rb')
Copy link
Member

@berkerpeksag berkerpeksag Apr 27, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using tokenize.open() would be better as it automatically detect the encoding of the file:

encoding, lines = detect_encoding(buffer.readline)

Copy link
Member

@berkerpeksag berkerpeksag Apr 27, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I agree with Emmanuel that using the existing hello.py as an example would make the example easier to follow.

Copy link
Contributor Author

@Windsooon Windsooon Apr 28, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found the tokenize function already call detect_encoding.

Copy link
Member

@berkerpeksag berkerpeksag Apr 28, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just realized that tokenize.open() returns a text stream, so passing it to tokenize.tokenize() wouldn't work.

We can use tokenize.generate_tokens() to demonstrate how the str API works:

import tokenize

with tokenize.open('hello.py') as f:
    token_gen = tokenize.generate_tokens(f.readline)
    for token in token_gen:
        print(token)

Then fallback to your example to show the usage of the bytes API:

import tokenize

with open('hello.py', 'rb') as f:
    token_gen = tokenize.tokenize(f.readline)
    for token in token_gen:
        print(token)

@bedevere-bot
Copy link

bedevere-bot commented Apr 27, 2019

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@matrixise
Copy link
Member

matrixise commented Sep 11, 2019

Hi @Windsooon

Could you update your PR with the last comment of @berkerpeksag and with the with statement?

Thank you

matrixise
matrixise previously requested changes Sep 11, 2019
Copy link
Member

@matrixise matrixise left a comment

Thanks for your contribution but you have to update your PR with the recommendations of @berkerpeksag

@Windsooon
Copy link
Contributor Author

Windsooon commented Sep 11, 2019

Hello, @matrixise, I would love to update the example if needed. However, Serhiy Storchaka didn't agree with it

I do not think a new example is needed. The existing example already demonstrates the use of file's readline method.

https://bugs.python.org/issue36654

@berkerpeksag
Copy link
Member

berkerpeksag commented Sep 12, 2019

I think Serhiy's comment was about the current form of the PR. In my last comment, I was proposing to add examples for both bytes and unicode APIs of the tokenize module.

As a core developer, even I missed that generate_token() was made public. I'm pretty sure more people would like to know that the module now has an API to pass unicode input.

@csabella
Copy link
Contributor

csabella commented Jan 12, 2020

@Windsooon, any updates? Thanks!

@Windsooon
Copy link
Contributor Author

Windsooon commented Jan 12, 2020

@Windsooon
Copy link
Contributor Author

Windsooon commented Jan 13, 2020

Hi, @csabella. I just updated the PR base on @berkerpeksag 's suggestion.

@csabella csabella requested a review from berkerpeksag Jan 13, 2020
Copy link
Member

@berkerpeksag berkerpeksag left a comment

Looks pretty good to me, thank you!

@berkerpeksag berkerpeksag dismissed matrixise’s stale review Jan 25, 2020

Comments have been addressed.

@berkerpeksag berkerpeksag merged commit 4b09dc7 into python:master Jan 25, 2020
@bedevere-bot
Copy link

bedevere-bot commented Jan 25, 2020

@berkerpeksag: Please replace # with GH- in the commit message next time. Thanks!

@miss-islington
Copy link
Contributor

miss-islington commented Jan 25, 2020

Thanks @Windsooon for the PR, and @berkerpeksag for merging it 🌮🎉.. I'm working now to backport this PR to: 3.7, 3.8.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jan 25, 2020
…honGH-12947)

(cherry picked from commit 4b09dc7)

Co-authored-by: Windson yang <wiwindson@outlook.com>
@bedevere-bot
Copy link

bedevere-bot commented Jan 25, 2020

GH-18187 is a backport of this pull request to the 3.8 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jan 25, 2020
…honGH-12947)

(cherry picked from commit 4b09dc7)

Co-authored-by: Windson yang <wiwindson@outlook.com>
@bedevere-bot
Copy link

bedevere-bot commented Jan 25, 2020

GH-18188 is a backport of this pull request to the 3.7 branch.

berkerpeksag pushed a commit that referenced this pull request Jan 25, 2020
…H-12947)

(cherry picked from commit 4b09dc7)

Co-authored-by: Windson yang <wiwindson@outlook.com>
shihai1991 pushed a commit to shihai1991/cpython that referenced this pull request Jan 31, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir skip news
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants