PythonScript early return
-
Usually my scripts start with checks on applicability etc. If certain conditions do not hold or errors are detected the script writes an error msg and takes an early return. I searched for a way to return from a PythonScript - other than at the bottom - but couldnât find it. Is there a way to return from a PythonScript?
-
Perhaps the simplest illustration of how to do it:
def main(): # do some stuff x = 0 if x == 0: return # take an early exit # do more stuff main()
-
Or you can take the class-based approach to script design, which I prefer. Hereâs my template for that:
# -*- coding: utf-8 -*- from __future__ import print_function from Npp import * import inspect import os #------------------------------------------------------------------------------- class TEST(object): def __init__(self): self.this_script_name = inspect.getframeinfo(inspect.currentframe()).filename.split(os.sep)[-1].rsplit('.', 1)[0] def run(self): self.mb('Hello from TEST !') def mb(self, msg, flags=0, title=''): # a message-box function return notepad.messageBox(msg, title if title else self.this_script_name, flags) #------------------------------------------------------------------------------- if __name__ == '__main__': TEST().run()
In this approach you can return early from any function, just like the non-class-based example from the previous post.
â
moderator edit: addedimport os
to make it work for default setups (since this script is being referenced by other posts, Alan wanted this to be a âworkingâ script for future userâs copy/paste, and the moderator agreed) -
@alan-kilborn
Yes this solution is very simple, I should have thought of it đ. -
inspect.getframeinfo(inspect.currentframe()).filename.split(os.sep)[-1].rsplit('.', 1)[0]
This is abracadabra to me. To begin with, where can I find the attributes of the inspect class?
-
@paul-wormer Stupid question: I see that âinspectâ is a Python module.
-
I should have added that the âinspectâ stuff is there because when I write scripts that use either
notepad.messageBox()
ornotepad.prompt()
functions, these bring up a dialog box, and take a parameter to specify the title bar; I like to put the script name there (typically), and rather than hardcode its name, I fetch it at runtime using âinspectâ. Mymb()
function is a little helper function that âwrapsânotepad.messageBox()
and shows this. -
@alan-kilborn OK thank you, you give me something to try out!
-
@paul-wormer I see âos.sepâ, shouldnât one âimport osâ?
-
@paul-wormer said in PythonScript early return:
I see âos.sepâ, shouldnât one âimport osâ?
Yes, good catch.
So the reason it doesnât fail without it, for me, is that my startup.py (or something it imports) must import os.
I usually test all of my scripts in a âcleanâ setup before posting; this one seemed so simple, I guess I didnât bother. :-(
PythonScripts are a bit different from the normal use case for Python â here, everything previously done is ârememberedâ.
-
@paul-wormer
So,import inspect, os inspect.getframeinfo(inspect.currentframe()).filename.split(os.sep)[-1].rsplit('.', 1)[0]
is wizardry just to get the script name without extension?
-
-
@alan-kilborn Just kidding. OK you helped me a lot, got wiser by the minute. I know now what to do, thank you again.
-
@paul-wormer
Next question: I have been googling and found an alternative for finding a filename:os.path.basename(os.path.abspath(__file__)).rsplit('.')[0]
This does not require importing inspect. I donât doubt that you are aware of this solution, and since you are not using it, it must have its drawbacks. What are they?
-
@paul-wormer Or even shorter:
os.path.basename(__file__).rsplit('.')[0]
-
So, often when I post scripts here, I take something existing that I have (working for a need specific to me), and strip some things out. Thatâs the case here.
As weâre really trying to focus on Notepad++ and/or PythonScript specific stuff here, diving deeper into the ways of âinspectâ or other such things really isnât appropriate, as it is really not related to either.
In the spirit of fun coding, if you find what you think is a better way of doing something, I say âgo for it!â.
-
Michael Vincent
-
@paul-wormer said in PythonScript early return:
I see âos.sepâ, shouldnât one âimport osâ?
I requested a forum moderator to add the missing
import os
to the script listing above, and that was doneâŠthanks Mr. Moderator! -
Alan Kilborn