-
Notifications
You must be signed in to change notification settings - Fork 196
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
Make imports in Python consistent with the style guide #615
Comments
Hello and WelcomeImport AllI have crunched some numbers and code ... After the incident with the error, I have realised this. There are some files that should be reformatted first, for ease and to save revisions. For example, List of files that are imported in
So, app/model.py would have to go first. Not to forget files using All in all, there are 33 files. Import SomeAll modules imported via
|
That would be great, thanks! Let me take examples to better understand your proposal. https://github.com/google/personfinder/blob/master/app/view.py contains: from model import *
...
person = Person.get(self.repo, self.params.id) and https://github.com/google/personfinder/blob/master/app/create.py contains: from photo import create_photo, PhotoError
...
note_photo, note_photo_url = \
create_photo(self.params.note_photo, self) How does the code look like by applying your proposed change? What I imagined was: import model
...
person = model.Person.get(self.repo, self.params.id) import photo
...
note_photo, note_photo_url = \
photo.create_photo(self.params.note_photo, self)
There is no time limit or deadline, so take your time. And you don't need to do everything at once (and you probably shouldn't). It would probably be best to first send a pull request to only fix a single file (e.g., app/create.py) to check if the change matches our intention, and apply it to other files later. Just FYI I found a bit tricky example: photo, photo_url = create_photo(self.params.photo, self) In this case the variable must be also renamed e.g.: person_photo, person_photo_url = photo.create_photo(self.params.photo, self) To avoid collision with the module name. Something worth keeping in mind. |
What I meant was this : from model import *
...
person = Person.get(self.repo, self.params.id) This could be done in one of 2 ways:
import model
...
person = model.Person.get(self.repo, self.params.id))
Take a look at this file :
from model import Person,[Some Other Module],[Some Another Module],[Etc ...]
...
person = Person.get(self.repo, self.params.id))
Tommorrow, A quick question though, in view.py I see |
Thanks for the clarification!
This still violates our style guide:
So I would prefer to go with the first option even though it is more work.
It is true for this particular case, and you would also see a lot of unused imports like this unfortunately :( Feel free to (and you are welcomed to) clean up unused imports as well when you find them. |
The file that I promised. Ran the app with the altered code. I was able to enter a record.
Solved this error, I was able to view the record as well. See ERROR section. Script
At first I thought, I would have to search every class and function there is in model.py in the working file. Default = dir()
from model import *
New = dir()
for i in New:
if i not in Default:print(i) This is premitive, i know. But it's just to give you an idea. This would save much time Anyhow, It still needs work but it will greatly reduce the effort of going through the entire files. Few things on which I need some feedback : HELP1
Rename that ? 1 ] 2 ] 3] 4] One can not go any higher right? HELP2
1 ] import google.appengine.api 2 ] import google.appengine.api.datastore_errors Import only one perticular thing In both ways above the datastore_errors would have to be renamed 3] Leave it as it is . Again Style code: Not for importing functions. ERRORFull :
File "/home/***/Documents/GitHub/personfinder/app/view.py", line 164, in get
admin=users.is_current_user_admin(),
NameError: global name 'users' is not defined This was because, HELP 3Read ERROR section first. So, now the 2 In this perticular case, But maybe in others we could use utils.users to avoid additional import. So, which way do we go ? |
To be clear, you mean when including APPENGINE_DIR on the path, same as when you run the app? Could you let me know what version of the SDK you're using (it should be the first line of output if you run
IMHO this is the sort of situation where maybe it's not worth following the style guide (_('user-facing') string seems more readable than the alternatives), but I'll yield to Hiroshi. If we do want to stick to the style guide, one option to minimize the length of the calls would be to import the translation module like this: |
I agree that we should consider this as an exception and leave it as is, because I believe it's a common pattern for this particular module. Sorry I forgot to mention that earlier. |
I updated my earlier comment. Maybe take a look at that for other things. Devlopment EnvironmentFor convenience, I am showing just the directory name here, Python2 + pipsUsing python2 env setup using miniconda conda set up is in a directory G SDKGoogle SDK v240.0.0 Installed the app engine python and extras.
Trying something new here, nut here's what I did before. cd to the personfinder. > py2
import google # works fine but Nothing in dir(google).
import google.appimage #ERROR : No module named `appimage` G App Enginerelease: "1.9.85" prepended the PATH with > py2
import google #ERROR : No module named `google`
import google.appimage #ERROR : No module named `google` Run personfinderprepended the PATH with
prepended the PATH with env APPENGINE_DIR="appengine" tools/gae run app --port=8000 That works fine. |
I have an Important question.
Do I just commit to the main branch, can I even do that ? |
You can just send pull requests. We can review and merge them into the master branch. |
Okay. Doing that. I am Removing the first comment I made and using it as a sort of TODO board and a Progress bar for this Issue.
|
In Model.py
|
https://cloud.google.com/appengine/docs/standard/python/refdocs/google.appengine.ext.db?hl=en It is sometimes unclear if the symbol is a package/module or a variable. But I believe you can consult documentation or read code of the library to distinguish them. |
Our current code base has many imports in Python not following this style guide:
https://github.com/google/styleguide/blob/gh-pages/pyguide.md#22-imports
These cases should be fixed to import packages/modules instead.
The text was updated successfully, but these errors were encountered: