Community
    • Login

    Regex] Rounding numbers python script does not run properly

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    28 Posts 4 Posters 2.8k 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.
    • EkopalypseE
      Ekopalypse @Alan Kilborn
      last edited by Ekopalypse

      @Alan-Kilborn

      :-D I constantly keep forgetting this, yes, it should be editor, thx for the head up.

      editor.rereplace("-*\d+\.\d{4,}", lambda m: '{:0.3f}'.format(round(float(m.group(0)), 3)))
      

      Yes, to me it looks awkward too but as you said, it might be in the interested of the OP.

      1 Reply Last reply Reply Quote 2
      • Alan KilbornA
        Alan Kilborn
        last edited by

        Just for fun (I say that a lot), here’s a version that does a prompted replace with preview (wouldn’t it be great if N++ itself offered a preview to its [regex] replacement?). Strange but I’ve never coded this kind of thing before for N++; one more thing to stash in the bag-of-tricks…

        def main():
        
            while True:
                match_list = []
                editor.research("-*\d+\.\d{4,}", lambda m: match_list.append(m.span(0)), 0, editor.getCurrentPos(), editor.getTextLength(), 1)
                if len(match_list) == 0: return
                editor.setSelection(match_list[0][1], match_list[0][0]); editor.scrollCaret()
                repl_text = '{:0.3f}'.format(round(float(editor.getSelText()), 3))
                result = notepad.messageBox('Replace selected text with\r\n\r\n{}\r\n\r\n???'.format(repl_text), '', MESSAGEBOXFLAGS.YESNOCANCEL)
                if result == MESSAGEBOXFLAGS.RESULTCANCEL: return
                if result == MESSAGEBOXFLAGS.RESULTYES: editor.replaceSel(repl_text)
        
        main()
        

        Imgur

        1 Reply Last reply Reply Quote 3
        • Alan KilbornA
          Alan Kilborn
          last edited by

          …still having fun with this…so much can be done to make interactive replace much better than what N++ can do; sample:

          Imgur

          1 Reply Last reply Reply Quote 3
          • unfassbarstephanU
            unfassbarstephan
            last edited by

            Wow, thank you dir your help! I will try this approach.

            1 Reply Last reply Reply Quote 0
            • unfassbarstephanU
              unfassbarstephan
              last edited by

              @Alan-Kilborn said:

              …still having fun with this…so much can be done to make interactive replace much better than what N++ can do; sample:

              The one-line-solution works like a charm.

              Would you also publish your code for the interactive replace? That would be great.

              Short question: What does “???” mean?

              EkopalypseE Alan KilbornA 2 Replies Last reply Reply Quote 0
              • EkopalypseE
                Ekopalypse @unfassbarstephan
                last edited by

                @unfassbarstephan

                Short question: What does “???” mean?

                This is Alan’s wasteful use of the question mark,
                for which it is also used here, to ask a question. :-D

                1 Reply Last reply Reply Quote 3
                • Alan KilbornA
                  Alan Kilborn @unfassbarstephan
                  last edited by

                  @unfassbarstephan said:

                  Would you also publish your code for the interactive replace?

                  I did…for your specific case. I’m still working on a generic version; may never finish it… :(

                  @Ekopalypse said:

                  Alan’s wasteful use of the question mark

                  Wasteful? It doesn’t seem right without it; I did consider it this way as well, maybe better?:

                  Replace selected text with? :

                  1 Reply Last reply Reply Quote 2
                  • PeterJonesP
                    PeterJones
                    last edited by

                    @Alan-Kilborn said:

                    Replace selected text with? :

                    Maybe Would you like to replace the selected text with the following text?

                    Alan KilbornA 1 Reply Last reply Reply Quote 1
                    • Alan KilbornA
                      Alan Kilborn @PeterJones
                      last edited by

                      @PeterJones

                      Well, Pythonscript sizes the dialog box according to contents, so I was trying to keep it as narrow and short as possible to avoid covering too much of the Notepad++ main window. But…I’ve already added more info to make it taller, so why not wider as well? :)

                      1 Reply Last reply Reply Quote 1
                      • Alan KilbornA
                        Alan Kilborn
                        last edited by

                        Would you also publish your code for the interactive replace?

                        I’m still working on a generic version; may never finish it

                        Follow-up:

                        There may not be a reasonable way to script this (the “generic version”); here’s why: There doesn’t seem to be a way to do a “boost-ish” replacement option on a Python string. It’s only possible to do it on in-place text.

                        What I want to do in the code is pull a copy of the matched text into a Python string and then do a Notepad++ -like replacement operation on that. Sure I can do a Python “re” replacement on it, but that isn’t always going to be equivalent.

                        Digging deeper, I see there is an open issue on this kind of thing in the Pythonscript code pages, here. But it hasn’t been acted on so it isn’t a reality.

                        If anyone has thoughts on this, I’m all ears. @Ekopalypse ?

                        EkopalypseE 1 Reply Last reply Reply Quote 0
                        • Alan KilbornA
                          Alan Kilborn
                          last edited by

                          Ugh. Clearly I meant this Pythonscript issue: https://github.com/bruderstein/PythonScript/issues/101 in the previous post, but it is too late for an edit. :(

                          1 Reply Last reply Reply Quote 1
                          • EkopalypseE
                            Ekopalypse @Alan Kilborn
                            last edited by

                            @Alan-Kilborn

                            one idea might be to search for matches and replace only those which are
                            in the same range as the selected text. Something like

                            if start_pos_selected text <= match_start and end_pos_selected_text >= match_end:
                                replaceSelText
                            

                            Or did I misunderstood your goal? :-(

                            Alan KilbornA 1 Reply Last reply Reply Quote 0
                            • Alan KilbornA
                              Alan Kilborn @Ekopalypse
                              last edited by

                              @Ekopalypse said:

                              did I misunderstood your goal?

                              Maybe that part. :)

                              To be able to offer a preview of the yet-to-be-done replacement, I need to know what that replacement will be. I can’t do that without actually doing the replacement. Catch-22. I could do it and then undo it, I suppose, but that gets messy. Mostly it should be OK, but sometimes with regexes Boost can differ from Python’s re.

                              EkopalypseE 1 Reply Last reply Reply Quote 1
                              • EkopalypseE
                                Ekopalypse @Alan Kilborn
                                last edited by

                                @Alan-Kilborn

                                what about using a hidden scintilla instance?

                                Alan KilbornA 2 Replies Last reply Reply Quote 0
                                • Alan KilbornA
                                  Alan Kilborn @Ekopalypse
                                  last edited by

                                  @Ekopalypse said:

                                  what about using a hidden scintilla instance?

                                  That also sounds “messy” (or “involved”) but if you have some snippet to show me it is not so bad…?

                                  EkopalypseE 2 Replies Last reply Reply Quote 0
                                  • EkopalypseE
                                    Ekopalypse @Alan Kilborn
                                    last edited by

                                    @Alan-Kilborn

                                    I used it here.

                                    1 Reply Last reply Reply Quote 2
                                    • EkopalypseE
                                      Ekopalypse @Alan Kilborn
                                      last edited by

                                      @Alan-Kilborn

                                      just be careful about notepad.destroyScintilla(self.hidden_scintilla)
                                      Currently npp crashes when using this and then request another buffer.
                                      There is already a merged fix but only available on next release.

                                      Alan KilbornA 1 Reply Last reply Reply Quote 2
                                      • Alan KilbornA
                                        Alan Kilborn @Ekopalypse
                                        last edited by

                                        @Ekopalypse

                                        This is the issue, I assume: https://github.com/notepad-plus-plus/notepad-plus-plus/issues/4487

                                        EkopalypseE 1 Reply Last reply Reply Quote 1
                                        • EkopalypseE
                                          Ekopalypse @Alan Kilborn
                                          last edited by

                                          @Alan-Kilborn

                                          correct, that is the one.

                                          1 Reply Last reply Reply Quote 0
                                          • Alan KilbornA
                                            Alan Kilborn @Ekopalypse
                                            last edited by

                                            @Ekopalypse said:

                                            what about using a hidden scintilla instance?

                                            So, like this: hidden = notepad.createScintilla()

                                            That is actually a nice solution. The hidden instance’s entire text can be set to the matched text from the editor.research(). Then the hidden.rereplace() can be called and the result of the replacement read out into a Python string variable. Thanks, Eko; clearly I would not have thought of this technique without your pointer.

                                            just be careful about notepad.destroyScintilla()

                                            This is not a problem as I will do this near the top of the script, and never do a “destroy”:

                                            try:
                                                hidden
                                            except NameError:
                                                hidden = notepad.createScintilla()
                                            

                                            and never do a “destroy”

                                            Alan KilbornA EkopalypseE 2 Replies Last reply Reply Quote 2
                                            • First post
                                              Last post
                                            The Community of users of the Notepad++ text editor.
                                            Powered by NodeBB | Contributors