• Login
Community
  • Login

Regex] Rounding numbers python script does not run properly

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
28 Posts 4 Posters 2.9k 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.
  • U
    unfassbarstephan
    last edited by Aug 23, 2019, 7:14 AM

    Hi,

    unfortunately i can’t comment on the thread https://notepad-plus-plus.org/community/topic/15100/regex-rounding-numbers , so i’m asking my question here.

    First of all I want to thank @PeterJones for the nice Python script for rounding numbers in Notepad++.

    However, the script does not run correctly for unknown reasons. The following series of numbers (coordinates):

    5494577.00001803319901228 5650290.99997399840503931, 5494577.00001803319901228 5650288.99997399933636189, 5494575.00001802947372198 5650288.99997399933636189, 5494575.00001802947372198 5650286.99997399840503931, 5494573.00001803319901228 5650286.99997399840503931

    is converted to:

    5494577.0000 5650290.99997399840503931, 5494577.0000 5650288.99997399933636189, 5494575.0000 5650288.99997399933636189, 5494575.0000 5650286.99997399840503931, 5494573.0000 5650286.99997399840503931

    Unfortunately I could not fix the error in the script. What is exactly the problem?

    Thank you in advance!

    Sincerly,

    Stephan

    E 1 Reply Last reply Aug 23, 2019, 11:59 AM Reply Quote 0
    • E
      Ekopalypse @unfassbarstephan
      last edited by Aug 23, 2019, 11:59 AM

      @unfassbarstephan

      the issue might be the recalculation of the next start point.
      May I propose a different approach?

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

      Just this line, nothing else.
      rereplace takes care that the next start position
      is calculated according to the replaced text.

      A 1 Reply Last reply Aug 23, 2019, 12:12 PM Reply Quote 3
      • A
        Alan Kilborn @Ekopalypse
        last edited by Alan Kilborn Aug 23, 2019, 12:13 PM Aug 23, 2019, 12:12 PM

        @Ekopalypse

        When I try the one-liner on the user’s data, I get this as a result (I don’t think it is the intent?):

        5494577.0 5650291.0, 5494577.0 5650289.0, 5494575.0 5650289.0, 5494575.0 5650287.0, 5494573.0 5650287.0

        But maybe it is…it would have been nice if the OP had said exactly what they were trying to achieve!

        E 1 Reply Last reply Aug 23, 2019, 12:14 PM Reply Quote 2
        • E
          Ekopalypse @Alan Kilborn
          last edited by Ekopalypse Aug 23, 2019, 12:15 PM Aug 23, 2019, 12:14 PM

          @Alan-Kilborn

          if it is really needed to show 3 digit zeros then a slight modification will do it. :-)

          editor1.rereplace("-*\d+\.\d{4,}", lambda m: '{:0.3f}'.format(round(float(m.group(0)), 3)))
          
          A 1 Reply Last reply Aug 23, 2019, 12:17 PM Reply Quote 2
          • A
            Alan Kilborn @Ekopalypse
            last edited by Alan Kilborn Aug 23, 2019, 12:19 PM Aug 23, 2019, 12:17 PM

            @Ekopalypse

            Ah. The .0 or .000 seems like extra noise to me, but maybe it is required for OP’s need (often wish people would explain their need better).

            But, very nice one-liner Eko! It’s a clip-and-saver.

            A switch from editor to editor1 ? Methinks you might be too focused on which view is active. I pretty much ignore editor1 and editor2 in my programming unless I really need them. :)

            E 1 Reply Last reply Aug 23, 2019, 12:22 PM Reply Quote 2
            • E
              Ekopalypse @Alan Kilborn
              last edited by Ekopalypse Aug 23, 2019, 12:22 PM Aug 23, 2019, 12:22 PM

              @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
              • A
                Alan Kilborn
                last edited by Aug 23, 2019, 3:10 PM

                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
                • A
                  Alan Kilborn
                  last edited by Aug 23, 2019, 6:04 PM

                  …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
                  • U
                    unfassbarstephan
                    last edited by Aug 23, 2019, 9:21 PM

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

                    1 Reply Last reply Reply Quote 0
                    • U
                      unfassbarstephan
                      last edited by Aug 24, 2019, 10:11 AM

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

                      E A 2 Replies Last reply Aug 24, 2019, 12:31 PM Reply Quote 0
                      • E
                        Ekopalypse @unfassbarstephan
                        last edited by Aug 24, 2019, 12:31 PM

                        @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
                        • A
                          Alan Kilborn @unfassbarstephan
                          last edited by Aug 24, 2019, 12:36 PM

                          @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
                          • P
                            PeterJones
                            last edited by Aug 24, 2019, 10:51 PM

                            @Alan-Kilborn said:

                            Replace selected text with? :

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

                            A 1 Reply Last reply Aug 24, 2019, 11:57 PM Reply Quote 1
                            • A
                              Alan Kilborn @PeterJones
                              last edited by Aug 24, 2019, 11:57 PM

                              @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
                              • A
                                Alan Kilborn
                                last edited by Aug 26, 2019, 6:06 PM

                                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 ?

                                E 1 Reply Last reply Aug 26, 2019, 7:02 PM Reply Quote 0
                                • A
                                  Alan Kilborn
                                  last edited by Aug 26, 2019, 6:23 PM

                                  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
                                  • E
                                    Ekopalypse @Alan Kilborn
                                    last edited by Aug 26, 2019, 7:02 PM

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

                                    A 1 Reply Last reply Aug 26, 2019, 7:08 PM Reply Quote 0
                                    • A
                                      Alan Kilborn @Ekopalypse
                                      last edited by Aug 26, 2019, 7:08 PM

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

                                      E 1 Reply Last reply Aug 26, 2019, 7:10 PM Reply Quote 1
                                      • E
                                        Ekopalypse @Alan Kilborn
                                        last edited by Aug 26, 2019, 7:10 PM

                                        @Alan-Kilborn

                                        what about using a hidden scintilla instance?

                                        A 2 Replies Last reply Aug 26, 2019, 7:13 PM Reply Quote 0
                                        • A
                                          Alan Kilborn @Ekopalypse
                                          last edited by Aug 26, 2019, 7:13 PM

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

                                          E 2 Replies Last reply Aug 26, 2019, 7:18 PM Reply Quote 0
                                          10 out of 28
                                          • First post
                                            10/28
                                            Last post
                                          The Community of users of the Notepad++ text editor.
                                          Powered by NodeBB | Contributors