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

Add a snippet for the if __name__ == '__main__': block #2242

Closed
d3r3kk opened this issue Jul 24, 2018 · 6 comments
Closed

Add a snippet for the if __name__ == '__main__': block #2242

d3r3kk opened this issue Jul 24, 2018 · 6 comments
Assignees
Labels
feature-request Request for new features or functionality

Comments

@d3r3kk
Copy link

d3r3kk commented Jul 24, 2018

We have all sorts of useful snippets in our extension, but I've noticed that we have not added one for the ubiquitous block:

if __name__ == '__main__':

Proposal to add the snippet with the type-ahead identifier __main__.

Acceptance Criteria:

  1. Open a new editor window
  2. Create a new file helloworld.py
  3. Add the following code:
1|  import sys
2|  
3|  def hello_world():
4|      print('Hi there!')
5|      print(f'Python version {sys.version_info}')
6|  
7|
  1. On line 7, begin type __main

Expected Result

Popup with a snippet for the dunder name equals dunder main appears. Pressing Enter will accept the snippet and insert code such as:

6|
7|  if __name__ == '__main__':
8|      $
9|

...where the $ is where the cursor is left such that the user can add whatever call(s) are necessary.

EDIT: Acceptance criteria altered to ensure cursor is placed optimally.

@d3r3kk d3r3kk added feature-request Request for new features or functionality feature-* labels Jul 24, 2018
@DonJayamanne
Copy link

DonJayamanne commented Jul 24, 2018

Instead of pass that's where the cursor should be placed.

@need47
Copy link

need47 commented Jul 26, 2018

I have a similar snippet when typing if. I only have the official python extension installed.

untitled

@d3r3kk
Copy link
Author

d3r3kk commented Jul 26, 2018

Yes, just saw this, thank you for showing us!

image

Seems that we already have this one defined. Closing the issue.

@d3r3kk d3r3kk closed this as completed Jul 26, 2018
@brettcannon
Copy link
Member

I would actually say that's not the best snippet since there's no expectation to define a main() function.

@d3r3kk
Copy link
Author

d3r3kk commented Jul 31, 2018

I agree actually, because the code that comes back is this:

image

Note the trailing ) at the end of the call to main())...

@brettcannon
Copy link
Member

I personally just want:

if __name__ == "__main__":
    ${1}

Or whatever is required to have VS Code start the cursor indented in the if block.

rsnk96 added a commit to rsnk96/vscode-python that referenced this issue Oct 13, 2018
Added snippet to check if program is being run directly or imported without a separate `main()` function (microsoft#2242)
@lock lock bot locked as resolved and limited conversation to collaborators Nov 20, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature-request Request for new features or functionality
Projects
None yet
Development

No branches or pull requests

4 participants