<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[PythonScript early return]]></title><description><![CDATA[<p dir="auto">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?</p>
]]></description><link>https://community.notepad-plus-plus.org/topic/22342/pythonscript-early-return</link><generator>RSS for Node</generator><lastBuildDate>Tue, 21 Apr 2026 01:22:38 GMT</lastBuildDate><atom:link href="https://community.notepad-plus-plus.org/topic/22342.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 05 Jan 2022 14:18:37 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to PythonScript early return on Wed, 05 Jan 2022 21:24:41 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://community.notepad-plus-plus.org/uid/22926">@paul-wormer</a> said in <a href="/post/72676">PythonScript early return</a>:</p>
<blockquote>
<p dir="auto">I see “os.sep”, shouldn’t one “import os”?</p>
</blockquote>
<p dir="auto">I requested a forum moderator to add the missing <code>import os</code> to the script listing above, and that was done…thanks Mr. Moderator!</p>
]]></description><link>https://community.notepad-plus-plus.org/post/72709</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/72709</guid><dc:creator><![CDATA[Alan Kilborn]]></dc:creator><pubDate>Wed, 05 Jan 2022 21:24:41 GMT</pubDate></item><item><title><![CDATA[Reply to PythonScript early return on Wed, 05 Jan 2022 18:04:35 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://community.notepad-plus-plus.org/uid/22926">@paul-wormer</a></p>
<p dir="auto">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.</p>
<p dir="auto">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.</p>
<p dir="auto">In the spirit of fun coding, if you find what you think is a better way of doing something, I say “go for it!”.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/72697</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/72697</guid><dc:creator><![CDATA[Alan Kilborn]]></dc:creator><pubDate>Wed, 05 Jan 2022 18:04:35 GMT</pubDate></item><item><title><![CDATA[Reply to PythonScript early return on Wed, 05 Jan 2022 17:02:22 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://community.notepad-plus-plus.org/uid/22926">@paul-wormer</a> Or even shorter:</p>
<pre><code>os.path.basename(__file__).rsplit('.')[0]
</code></pre>
]]></description><link>https://community.notepad-plus-plus.org/post/72691</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/72691</guid><dc:creator><![CDATA[Paul Wormer]]></dc:creator><pubDate>Wed, 05 Jan 2022 17:02:22 GMT</pubDate></item><item><title><![CDATA[Reply to PythonScript early return on Wed, 05 Jan 2022 16:55:34 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://community.notepad-plus-plus.org/uid/22926">@paul-wormer</a><br />
Next question:  I have been googling and found an alternative for finding a filename:</p>
<pre><code>os.path.basename(os.path.abspath(__file__)).rsplit('.')[0]
</code></pre>
<p dir="auto">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?</p>
]]></description><link>https://community.notepad-plus-plus.org/post/72690</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/72690</guid><dc:creator><![CDATA[Paul Wormer]]></dc:creator><pubDate>Wed, 05 Jan 2022 16:55:34 GMT</pubDate></item><item><title><![CDATA[Reply to PythonScript early return on Wed, 05 Jan 2022 15:34:04 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://community.notepad-plus-plus.org/uid/7377">@alan-kilborn</a> Just kidding. OK you helped me a lot, got wiser by the minute. I know now what to do, thank you again.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/72681</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/72681</guid><dc:creator><![CDATA[Paul Wormer]]></dc:creator><pubDate>Wed, 05 Jan 2022 15:34:04 GMT</pubDate></item><item><title><![CDATA[Reply to PythonScript early return on Wed, 05 Jan 2022 15:32:03 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://community.notepad-plus-plus.org/uid/22926">@paul-wormer</a> said in <a href="/post/72679">PythonScript early return</a>:</p>
<blockquote>
<p dir="auto">is wizardry</p>
</blockquote>
<p dir="auto">???</p>
<p dir="auto">No wizardry, just code.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/72680</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/72680</guid><dc:creator><![CDATA[Alan Kilborn]]></dc:creator><pubDate>Wed, 05 Jan 2022 15:32:03 GMT</pubDate></item><item><title><![CDATA[Reply to PythonScript early return on Wed, 05 Jan 2022 15:30:54 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://community.notepad-plus-plus.org/uid/22926">@paul-wormer</a><br />
So,</p>
<pre><code>import inspect, os 
inspect.getframeinfo(inspect.currentframe()).filename.split(os.sep)[-1].rsplit('.', 1)[0]
</code></pre>
<p dir="auto">is wizardry just to get the script name without extension?</p>
]]></description><link>https://community.notepad-plus-plus.org/post/72679</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/72679</guid><dc:creator><![CDATA[Paul Wormer]]></dc:creator><pubDate>Wed, 05 Jan 2022 15:30:54 GMT</pubDate></item><item><title><![CDATA[Reply to PythonScript early return on Wed, 05 Jan 2022 15:31:15 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://community.notepad-plus-plus.org/uid/22926">@paul-wormer</a> said in <a href="/post/72676">PythonScript early return</a>:</p>
<blockquote>
<p dir="auto">I see “os.sep”, shouldn’t one “import os”?</p>
</blockquote>
<p dir="auto">Yes, good catch.</p>
<p dir="auto">So the reason it doesn’t fail without it, for me, is that my <a href="http://startup.py" rel="nofollow ugc">startup.py</a> (or something <em>it</em> imports) must import os.</p>
<p dir="auto">I usually test all of my scripts in a “clean” setup before posting; this one seemed so simple, I guess I didn’t bother. :-(</p>
<p dir="auto">PythonScripts are a bit different from the normal use case for Python – here, everything previously done is “remembered”.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/72678</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/72678</guid><dc:creator><![CDATA[Alan Kilborn]]></dc:creator><pubDate>Wed, 05 Jan 2022 15:31:15 GMT</pubDate></item><item><title><![CDATA[Reply to PythonScript early return on Wed, 05 Jan 2022 15:23:08 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://community.notepad-plus-plus.org/uid/22926">@paul-wormer</a> I see “os.sep”, shouldn’t one “import os”?</p>
]]></description><link>https://community.notepad-plus-plus.org/post/72676</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/72676</guid><dc:creator><![CDATA[Paul Wormer]]></dc:creator><pubDate>Wed, 05 Jan 2022 15:23:08 GMT</pubDate></item><item><title><![CDATA[Reply to PythonScript early return on Wed, 05 Jan 2022 15:14:24 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://community.notepad-plus-plus.org/uid/7377">@alan-kilborn</a> OK thank you, you give me something to try out!</p>
]]></description><link>https://community.notepad-plus-plus.org/post/72671</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/72671</guid><dc:creator><![CDATA[Paul Wormer]]></dc:creator><pubDate>Wed, 05 Jan 2022 15:14:24 GMT</pubDate></item><item><title><![CDATA[Reply to PythonScript early return on Wed, 05 Jan 2022 15:13:28 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://community.notepad-plus-plus.org/uid/22926">@paul-wormer</a></p>
<p dir="auto">I should have added that the “inspect” stuff is there because when I write scripts that use either <code>notepad.messageBox()</code> or <code>notepad.prompt()</code> 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”.  My <code>mb()</code> function is a little helper function that “wraps” <code>notepad.messageBox()</code> and shows this.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/72670</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/72670</guid><dc:creator><![CDATA[Alan Kilborn]]></dc:creator><pubDate>Wed, 05 Jan 2022 15:13:28 GMT</pubDate></item><item><title><![CDATA[Reply to PythonScript early return on Wed, 05 Jan 2022 15:08:01 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://community.notepad-plus-plus.org/uid/22926">@paul-wormer</a>  Stupid question: I see that  ‘inspect’ is a Python module.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/72668</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/72668</guid><dc:creator><![CDATA[Paul Wormer]]></dc:creator><pubDate>Wed, 05 Jan 2022 15:08:01 GMT</pubDate></item><item><title><![CDATA[Reply to PythonScript early return on Wed, 05 Jan 2022 15:03:50 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://community.notepad-plus-plus.org/uid/7377">@alan-kilborn</a></p>
<pre><code>inspect.getframeinfo(inspect.currentframe()).filename.split(os.sep)[-1].rsplit('.', 1)[0]
</code></pre>
<p dir="auto">This is abracadabra to me. To begin with, where can I find the attributes of the inspect class?</p>
]]></description><link>https://community.notepad-plus-plus.org/post/72667</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/72667</guid><dc:creator><![CDATA[Paul Wormer]]></dc:creator><pubDate>Wed, 05 Jan 2022 15:03:50 GMT</pubDate></item><item><title><![CDATA[Reply to PythonScript early return on Wed, 05 Jan 2022 14:59:42 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://community.notepad-plus-plus.org/uid/7377">@alan-kilborn</a><br />
Yes this solution is very simple, I should have thought of it 😉.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/72666</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/72666</guid><dc:creator><![CDATA[Paul Wormer]]></dc:creator><pubDate>Wed, 05 Jan 2022 14:59:42 GMT</pubDate></item><item><title><![CDATA[Reply to PythonScript early return on Wed, 05 Jan 2022 21:18:57 GMT]]></title><description><![CDATA[<p dir="auto">Or you can take the class-based approach to script design, which I prefer.  Here’s my template for that:</p>
<pre><code class="language-z"># -*- 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()
</code></pre>
<p dir="auto">In this approach you can return early from any function, just like the non-class-based example from the previous post.</p>
<p dir="auto">–<br />
<em>moderator edit: added <code>import os</code> 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)</em></p>
]]></description><link>https://community.notepad-plus-plus.org/post/72665</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/72665</guid><dc:creator><![CDATA[Alan Kilborn]]></dc:creator><pubDate>Wed, 05 Jan 2022 21:18:57 GMT</pubDate></item><item><title><![CDATA[Reply to PythonScript early return on Wed, 05 Jan 2022 14:26:07 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://community.notepad-plus-plus.org/uid/22926">@paul-wormer</a></p>
<p dir="auto">Perhaps the simplest illustration of how to do it:</p>
<pre><code class="language-z">def main():
    # do some stuff
    x = 0
    if x == 0: return  # take an early exit
    # do more stuff

main()
</code></pre>
]]></description><link>https://community.notepad-plus-plus.org/post/72664</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/72664</guid><dc:creator><![CDATA[Alan Kilborn]]></dc:creator><pubDate>Wed, 05 Jan 2022 14:26:07 GMT</pubDate></item></channel></rss>