Community
    • 登入

    pythonscript: any ready pyscript to replace one huge set of regex/ phrases with others?

    已排程 已置頂 已鎖定 已移動 Notepad++ & Plugin Development
    58 貼文 10 Posters 26.5k 瀏覽
    正在載入更多貼文
    • 從舊到新
    • 從新到舊
    • 最多點贊
    回覆
    • 在新貼文中回覆
    登入後回覆
    此主題已被刪除。只有擁有主題管理權限的使用者可以查看。
    • Alan KilbornA
      Alan Kilborn @Eko palypse
      最後由 編輯

      @Eko-palypse

      Wow. WOW. I find I cannot reproduce my earlier results. It seems to be working consistently now (duplicating your results). I guess I have egg on my face and sorry for the false alarm; unless it takes some sort of special sequence of actions to get into a weird mode! I did restart N++ an hour ago so I supposed that is a possible occurrence.

      One thing I am seeing now that the editor.rereplace() is doing “nothing”:

      I use the LocationNavigate plugin for its ability to specially mark changed lines (wish there was a better/more-current solution to that, btw!). When I run the scripted replace, although visually no text changes, the plugin does mark every line where \w+ matches. Basically this means all lines besides empty ones got “changed”. So…not really sure what under-the-hood voodoo magic is happening when using \U (and probably others like it) with a scripted replace, but it sure seems like SOMETHING interesting might be happening. If what is happening is that the \U is being ignored and abc is simply being replaced by abc perhaps that is not all that interesting. :(

      Eko palypseE 1 條回覆 最後回覆 回覆 引用 1
      • Eko palypseE
        Eko palypse @Alan Kilborn
        最後由 編輯

        @Alan-Kilborn

        :-) … live is interesting, isn’t it … what is true now might be false in the next minute :-)

        From modify callback I see that there is a replace but it just ignores the \U

        {'code': 2008, 'annotationLinesAdded': 0, 'text': 'this', 'modificationType': 1048576, 'token': 0, 'linesAdded': 0, 'length': 4, 'foldLevelPrev': 0, 'position': 0, 'line': 0, 'foldLevelNow': 0}
        {'code': 2008, 'annotationLinesAdded': 0, 'text': 'is', 'modificationType': 1048576, 'token': 0, 'linesAdded': 0, 'length': 2, 'foldLevelPrev': 0, 'position': 5, 'line': 0, 'foldLevelNow': 0}
        {'code': 2008, 'annotationLinesAdded': 0, 'text': 'some', 'modificationType': 1048576, 'token': 0, 'linesAdded': 0, 'length': 4, 'foldLevelPrev': 0, 'position': 8, 'line': 0, 'foldLevelNow': 0}
        {'code': 2008, 'annotationLinesAdded': 0, 'text': 'text', 'modificationType': 1048576, 'token': 0, 'linesAdded': 0, 'length': 4, 'foldLevelPrev': 0, 'position': 13, 'line': 0, 'foldLevelNow': 0}
        
        1 條回覆 最後回覆 回覆 引用 3
        • chcgC
          chcg
          最後由 編輯

          Added the script as https://github.com/bruderstein/PythonScript/blob/master/scripts/Samples/Multiples_SR.py

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

            @guy038

            Since you pointed out that the Pythonscript editor.rereplace() way of working has some distinct advantages (less bugs?) over the Notepad++ interactive Replace dialog, how about a little script for making testing the differences even easier (and less cryptic than typing a one-liner in the PS console):

            if editor.getSelectionEmpty():
                p1 = editor.getCurrentPos()
                p2 = editor.getTextLength() - 1
            else:
                p1 = editor.getSelectionStart()
                p2 = editor.getSelectionEnd()
            s = notepad.prompt('Enter search regex:', '', '')
            if s != None and len(s) > 0:
                r = notepad.prompt('Enter replace regex:', '', '')
                if r != None:
                    editor.beginUndoAction()
                    editor.rereplace(s, r, 0, p1, p2)
                    editor.endUndoAction()
            

            If a selection is active when running the script, it acts like N++'s “Replace All…with In Selection ticked”. Otherwise, it acts like a normal N++'s “Replace All” acting on text from caret downward to EOF.

            Not rocket science, but then again neither was the original script way above. :)

            1 條回覆 最後回覆 回覆 引用 2
            • V S RawatV
              V S Rawat
              最後由 編輯

              how can I download all script at bruderstein/PythonScript/scripts/Samples/ as a single zip file?

              Thanks.

              Meta ChuhM Eko palypseE 2 條回覆 最後回覆 回覆 引用 0
              • Meta ChuhM
                Meta Chuh moderator @V S Rawat
                最後由 Meta Chuh 編輯

                @V-S-Rawat

                how can I download all script at bruderstein/PythonScript/scripts/Samples/ as a single zip file?

                you can do that by either downloading the whole master repo from >>> here <<< and extract PythonScript/scripts/Samples/,

                … or by downloading them manually one by one, then package them into another zip and commit this zip to david @bruderstein , with the request for publishing, so that you can re-download it 😉

                1 條回覆 最後回覆 回覆 引用 2
                • Eko palypseE
                  Eko palypse @V S Rawat
                  最後由 Eko palypse 編輯

                  … or by writing a python script doing it
                  @V-S-Rawat I leave the implementation of the compressing function to you ;-)

                  import urllib
                  import re
                  
                  sample_url = "https://github.com/bruderstein/PythonScript/tree/master/scripts/Samples"
                  raw_url = "https://raw.githubusercontent.com/bruderstein/PythonScript/master/scripts/Samples/"
                  save_dir = r'D:\xyz\{}'
                  
                  _f = urllib.urlopen(sample_url)
                  data = _f.read()
                  links = re.findall('href="(.*?\.py)"', data)
                  for link in links:
                      file = link.rpartition('/')[2]
                      urllib.urlretrieve(raw_url + file, save_dir.format(urllib.unquote(file)))
                  
                  V S RawatV 1 條回覆 最後回覆 回覆 引用 4
                  • V S RawatV
                    V S Rawat @Eko palypse
                    最後由 編輯

                    @Eko-palypse

                    This script will run from within npp and will access net and download all *.py on that page?

                    Quite powerful. I never thought such a thing is even possible.

                    :-)

                    Thanks.

                    Eko palypseE 1 條回覆 最後回覆 回覆 引用 1
                    • Eko palypseE
                      Eko palypse @V S Rawat
                      最後由 編輯

                      @V-S-Rawat

                      Yes, but since it does not use any Notepad ++ objects, it can also be run outside of npp.
                      The ecosystem of Python is really and I mean really huge.
                      If you are thinking about a problem and how you can solve it, there is most likely already a Python module available that is made for it.
                      The only little downer is that PythonScript has not implemented pip or it can not be easily implemented (as far as I understand it), which means you have to, if you want to rely on external modules, do some manual work to make it happen within pythonscript plugin.

                      V S RawatV 1 條回覆 最後回覆 回覆 引用 3
                      • V S RawatV
                        V S Rawat @Eko palypse
                        最後由 編輯

                        That brings another bouncer to me.
                        @Eko-palypse said:

                        Yes, but since it does not use any Notepad ++ objects, it can also be run outside of npp.

                        You mean same npp pyscript can be run from outside of npp?

                        how? I only have pyscript plugin installed, and it seems it had installed a huge set of files. Are those files sufficient to run npp pyscript from outside npp? How? which exe file to run?

                        or do I have to download python afresh and install that to be able to run python out of npp?

                        And if so, can I think remove pyscript plugin and still be able to run pyscript from within npp?
                        because that would mean one python installed by pyscript within npp, and another python installed directly in programfiles. Why keep both, there may be version clashes between them.

                        The ecosystem of Python is really and I mean really huge.
                        If you are thinking about a problem and how you can solve it, there is most likely already a Python module available that is made for it.

                        yeah, I was also thinking that across the years that python has been popular and widely used, a huge repository of pyscripts must already have been made. But I find very less repositories very less number of scripts then I thought should have been there. Seems I didn’t not reach major pyscript repositories.

                        Thanks.

                        Eko palypseE Alan KilbornA 2 條回覆 最後回覆 回覆 引用 0
                        • Eko palypseE
                          Eko palypse @V S Rawat
                          最後由 編輯

                          @V-S-Rawat

                          Yes, this particular script may run outside npp if you have a local
                          python installation and then by calling python scriptname.py

                          Theoretically, you can also uninstall the plugin and remote control
                          npp and scintilla with the local python installation.
                          But then you have to program everything yourself
                          what the pythonscript plugin otherwise provides to you.
                          The callbacks could be a bigger challenge.

                          A central repo for scripts does not exist, as far as I know,
                          but a central system for installing modules does.

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

                            @V-S-Rawat

                            Just to say a bit more, adding onto what Eko said:

                            If you have Pythonscript installed with N++, it is run from a python.dll. If you have no other external Python installed (which would be a python.exe file and of course everything that goes along with it) then you cannot run a .py file outside of N++, even if that .py file isn’t using anything related to N++ (e.g. editor. and notepad. object function calls). Conversely, as pointed out above, you can run such a .py file from inside N++.

                            1 條回覆 最後回覆 回覆 引用 2
                            • Gokul GopalakrishnanG
                              Gokul Gopalakrishnan @guy038
                              最後由 Gokul Gopalakrishnan 編輯

                              @guy038 I’m getting this following error when I try to run this script.
                              Traceback (most recent call last):
                              File “C:\Program Files\Notepad++\plugins\PythonScript\scripts\Samples\Multiples_SR.py”, line 96, in <module>
                              (s,r,e) = line[1:].rstrip(‘\n’).split(line[0])
                              ValueError: too many values to unpack

                              SR_List:
                              ‘%(\hpc-nas.noise.server01.net)%(/Volumes)%’,

                              M 1 條回覆 最後回覆 回覆 引用 1
                              • M
                                Mayonnaisu @Gokul Gopalakrishnan
                                最後由 編輯

                                @Gokul-Gopalakrishnan @guy038
                                Me too…

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

                                  @Mayonnaisu

                                  If you get an error message like ValueError: too many values to unpack, then this is a strong indication that you have generated too many values :-) - I mean, what is wrong with this message?
                                  Consider the following: “(a, b) = ‘A B C’.split()”
                                  From the code, you expect the split to generate TWO results that you want to put into variables a and b, but that’s not the case, is it?
                                  So your real data does not match your code logic.

                                  M 1 條回覆 最後回覆 回覆 引用 3
                                  • M
                                    Mayonnaisu @Ekopalypse
                                    最後由 編輯

                                    @Ekopalypse
                                    I see that’s how it is. So I just need to delete the (third) variable that I don’t need. Thanks for the information!

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

                                      @Mayonnaisu

                                      The error message indicates that you need more variables.
                                      If you need less, then the error message would be slightly different, something like need more than 2 values to unpack.

                                      M 1 條回覆 最後回覆 回覆 引用 1
                                      • M
                                        Mayonnaisu @Ekopalypse
                                        最後由 編輯

                                        @Ekopalypse
                                        I see… 😅

                                        1 條回覆 最後回覆 回覆 引用 0
                                        • 第一個貼文
                                          最後的貼文
                                        The Community of users of the Notepad++ text editor.
                                        Powered by NodeBB | Contributors