Community

    • Login
    • Search
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search

    PythonScript early return

    Help wanted · · · – – – · · ·
    2
    17
    203
    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.
    • Alan Kilborn
      Alan Kilborn @Paul Wormer last edited by Alan Kilborn

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

      Paul Wormer 1 Reply Last reply Reply Quote 1
      • Paul Wormer
        Paul Wormer @Alan Kilborn last edited by

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

        Paul Wormer 1 Reply Last reply Reply Quote 1
        • Paul Wormer
          Paul Wormer @Paul Wormer last edited by

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

          Alan Kilborn 2 Replies Last reply Reply Quote 2
          • Alan Kilborn
            Alan Kilborn @Paul Wormer last edited by Alan Kilborn

            @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
            • Paul Wormer
              Paul Wormer @Paul Wormer last edited by

              @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 1 Reply Last reply Reply Quote 0
              • Alan Kilborn
                Alan Kilborn @Paul Wormer last edited by

                @paul-wormer said in PythonScript early return:

                is wizardry

                ???

                No wizardry, just code.

                Paul Wormer 1 Reply Last reply Reply Quote 0
                • Paul Wormer
                  Paul Wormer @Alan Kilborn last edited by

                  @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 1 Reply Last reply Reply Quote 2
                  • Paul Wormer
                    Paul Wormer @Paul Wormer last edited by

                    @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 1 Reply Last reply Reply Quote 1
                    • Paul Wormer
                      Paul Wormer @Paul Wormer last edited by

                      @paul-wormer Or even shorter:

                      os.path.basename(__file__).rsplit('.')[0]
                      
                      Alan Kilborn 1 Reply Last reply Reply Quote 0
                      • Alan Kilborn
                        Alan Kilborn @Paul Wormer last edited by

                        @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
                        • Referenced by  Michael Vincent Michael Vincent 
                        • Alan Kilborn
                          Alan Kilborn @Paul Wormer last edited by

                          @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
                          • Referenced by  Alan Kilborn Alan Kilborn 
                          • First post
                            Last post
                          Copyright © 2014 NodeBB Forums | Contributors