Community
    • 登入

    Regex] Rounding numbers python script does not run properly

    已排程 已置頂 已鎖定 已移動 Help wanted · · · – – – · · ·
    28 貼文 4 Posters 3.9k 瀏覽
    正在載入更多貼文
    • 從舊到新
    • 從新到舊
    • 最多點贊
    回覆
    • 在新貼文中回覆
    登入後回覆
    此主題已被刪除。只有擁有主題管理權限的使用者可以查看。
    • unfassbarstephanU
      unfassbarstephan
      最後由 編輯

      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

      EkopalypseE 1 條回覆 最後回覆 回覆 引用 0
      • EkopalypseE
        Ekopalypse @unfassbarstephan
        最後由 編輯

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

        Alan KilbornA 1 條回覆 最後回覆 回覆 引用 3
        • Alan KilbornA
          Alan Kilborn @Ekopalypse
          最後由 Alan Kilborn 編輯

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

          EkopalypseE 1 條回覆 最後回覆 回覆 引用 2
          • EkopalypseE
            Ekopalypse @Alan Kilborn
            最後由 Ekopalypse 編輯

            @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)))
            
            Alan KilbornA 1 條回覆 最後回覆 回覆 引用 2
            • Alan KilbornA
              Alan Kilborn @Ekopalypse
              最後由 Alan Kilborn 編輯

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

              EkopalypseE 1 條回覆 最後回覆 回覆 引用 2
              • EkopalypseE
                Ekopalypse @Alan Kilborn
                最後由 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 條回覆 最後回覆 回覆 引用 2
                • Alan KilbornA
                  Alan Kilborn
                  最後由 編輯

                  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 條回覆 最後回覆 回覆 引用 3
                  • Alan KilbornA
                    Alan Kilborn
                    最後由 編輯

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

                    Imgur

                    1 條回覆 最後回覆 回覆 引用 3
                    • unfassbarstephanU
                      unfassbarstephan
                      最後由 編輯

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

                      1 條回覆 最後回覆 回覆 引用 0
                      • unfassbarstephanU
                        unfassbarstephan
                        最後由 編輯

                        @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 條回覆 最後回覆 回覆 引用 0
                        • EkopalypseE
                          Ekopalypse @unfassbarstephan
                          最後由 編輯

                          @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 條回覆 最後回覆 回覆 引用 3
                          • Alan KilbornA
                            Alan Kilborn @unfassbarstephan
                            最後由 編輯

                            @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 條回覆 最後回覆 回覆 引用 2
                            • PeterJonesP
                              PeterJones
                              最後由 編輯

                              @Alan-Kilborn said:

                              Replace selected text with? :

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

                              Alan KilbornA 1 條回覆 最後回覆 回覆 引用 1
                              • Alan KilbornA
                                Alan Kilborn @PeterJones
                                最後由 編輯

                                @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 條回覆 最後回覆 回覆 引用 1
                                • Alan KilbornA
                                  Alan Kilborn
                                  最後由 編輯

                                  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 條回覆 最後回覆 回覆 引用 0
                                  • Alan KilbornA
                                    Alan Kilborn
                                    最後由 編輯

                                    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 條回覆 最後回覆 回覆 引用 1
                                    • EkopalypseE
                                      Ekopalypse @Alan Kilborn
                                      最後由 編輯

                                      @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 條回覆 最後回覆 回覆 引用 0
                                      • Alan KilbornA
                                        Alan Kilborn @Ekopalypse
                                        最後由 編輯

                                        @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 條回覆 最後回覆 回覆 引用 1
                                        • EkopalypseE
                                          Ekopalypse @Alan Kilborn
                                          最後由 編輯

                                          @Alan-Kilborn

                                          what about using a hidden scintilla instance?

                                          Alan KilbornA 2 條回覆 最後回覆 回覆 引用 0
                                          • Alan KilbornA
                                            Alan Kilborn @Ekopalypse
                                            最後由 編輯

                                            @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 條回覆 最後回覆 回覆 引用 0
                                            • 第一個貼文
                                              最後的貼文
                                            The Community of users of the Notepad++ text editor.
                                            Powered by NodeBB | Contributors