• Login
Community
  • Login

PythonScript early return

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
17 Posts 2 Posters 946 Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P
    Paul Wormer
    last edited by Jan 5, 2022, 2:18 PM

    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?

    A 1 Reply Last reply Jan 5, 2022, 2:26 PM Reply Quote 0
    • A
      Alan Kilborn @Paul Wormer
      last edited by Jan 5, 2022, 2:26 PM

      @paul-wormer

      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()
      
      P 1 Reply Last reply Jan 5, 2022, 2:59 PM Reply Quote 2
      • A
        Alan Kilborn
        last edited by PeterJones Jan 5, 2022, 9:18 PM Jan 5, 2022, 2:38 PM

        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: added import 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)

        P 1 Reply Last reply Jan 5, 2022, 3:03 PM Reply Quote 1
        • P
          Paul Wormer @Alan Kilborn
          last edited by Jan 5, 2022, 2:59 PM

          @alan-kilborn
          Yes this solution is very simple, I should have thought of it 😉.

          1 Reply Last reply Reply Quote 1
          • P
            Paul Wormer @Alan Kilborn
            last edited by Jan 5, 2022, 3:03 PM

            @alan-kilborn

            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?

            P 2 Replies Last reply Jan 5, 2022, 3:08 PM Reply Quote 0
            • P
              Paul Wormer @Paul Wormer
              last edited by Jan 5, 2022, 3:08 PM

              @paul-wormer Stupid question: I see that ‘inspect’ is a Python module.

              A 1 Reply Last reply Jan 5, 2022, 3:12 PM Reply Quote 0
              • A
                Alan Kilborn @Paul Wormer
                last edited by Alan Kilborn Jan 5, 2022, 3:13 PM Jan 5, 2022, 3:12 PM

                @paul-wormer

                I should have added that the “inspect” stuff is there because when I write scripts that use either notepad.messageBox() or notepad.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”. My mb() function is a little helper function that “wraps” notepad.messageBox() and shows this.

                P 1 Reply Last reply Jan 5, 2022, 3:14 PM Reply Quote 1
                • P
                  Paul Wormer @Alan Kilborn
                  last edited by Jan 5, 2022, 3:14 PM

                  @alan-kilborn OK thank you, you give me something to try out!

                  P 1 Reply Last reply Jan 5, 2022, 3:23 PM Reply Quote 1
                  • P
                    Paul Wormer @Paul Wormer
                    last edited by Jan 5, 2022, 3:23 PM

                    @paul-wormer I see “os.sep”, shouldn’t one “import os”?

                    A 2 Replies Last reply Jan 5, 2022, 3:29 PM Reply Quote 2
                    • A
                      Alan Kilborn @Paul Wormer
                      last edited by Alan Kilborn Jan 5, 2022, 3:31 PM Jan 5, 2022, 3:29 PM

                      @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”.

                      1 Reply Last reply Reply Quote 2
                      • P
                        Paul Wormer @Paul Wormer
                        last edited by Jan 5, 2022, 3:30 PM

                        @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?

                        A 1 Reply Last reply Jan 5, 2022, 3:32 PM Reply Quote 0
                        • A
                          Alan Kilborn @Paul Wormer
                          last edited by Jan 5, 2022, 3:32 PM

                          @paul-wormer said in PythonScript early return:

                          is wizardry

                          ???

                          No wizardry, just code.

                          P 1 Reply Last reply Jan 5, 2022, 3:34 PM Reply Quote 0
                          • P
                            Paul Wormer @Alan Kilborn
                            last edited by Jan 5, 2022, 3:34 PM

                            @alan-kilborn Just kidding. OK you helped me a lot, got wiser by the minute. I know now what to do, thank you again.

                            P 1 Reply Last reply Jan 5, 2022, 4:55 PM Reply Quote 2
                            • P
                              Paul Wormer @Paul Wormer
                              last edited by Jan 5, 2022, 4:55 PM

                              @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?

                              P 1 Reply Last reply Jan 5, 2022, 5:02 PM Reply Quote 1
                              • P
                                Paul Wormer @Paul Wormer
                                last edited by Jan 5, 2022, 5:02 PM

                                @paul-wormer Or even shorter:

                                os.path.basename(__file__).rsplit('.')[0]
                                
                                A 1 Reply Last reply Jan 5, 2022, 6:04 PM Reply Quote 0
                                • A
                                  Alan Kilborn @Paul Wormer
                                  last edited by Jan 5, 2022, 6:04 PM

                                  @paul-wormer

                                  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!”.

                                  1 Reply Last reply Reply Quote 0
                                  • M Michael Vincent referenced this topic on Jan 5, 2022, 9:03 PM
                                  • A
                                    Alan Kilborn @Paul Wormer
                                    last edited by Jan 5, 2022, 9:24 PM

                                    @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!

                                    1 Reply Last reply Reply Quote 2
                                    • A Alan Kilborn referenced this topic on Feb 4, 2023, 2:44 AM
                                    5 out of 17
                                    • First post
                                      5/17
                                      Last post
                                    The Community of users of the Notepad++ text editor.
                                    Powered by NodeBB | Contributors