Community
    • Login

    renumbering/incremental

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    45 Posts 7 Posters 6.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.
    • Alan KilbornA
      Alan Kilborn
      last edited by Alan Kilborn

      The script does what I think was what was wanted; it renumbers into order, with gaps in the numbering, all the Nx fields that occur at start-of-line, where “x” is some number of digits – and the important part: it does this across some folder hierarchy of possibly a large number of files.

      If someone wanted to keep the core logic, but change the specific search and replacement, here’s what someone would do:

      Search the code for the text USER MODS; you’d find some sections that look like this:

      # vvvvvvvvvv---------- USER MODS REQUIRED BELOW ----------vvvvvvvvvv
      # ^^^^^^^^^^---------- USER MODS REQUIRED ABOVE ----------^^^^^^^^^^
      

      In between those lines are what you’d change for customization.

      In the first case, you’d change what is being looked for, e.g.:

      self.search_regex = r'^N\d+'  # search for N followed by some number of digits (but must be at start-of-line)
      

      Change what is between the ' characters for the regular expression you’ve tested independent of the script.

      In the second case, you’d find this:

      def return_replacement_text_func(m):
          ....
      

      and that’s where you’d put the replacement function you developed for the one file case.

      For our scenario under discussion in this thread, that function looks like this:

      def return_replacement_text_func(m):
          # replace with N and a minimum of 2 digits, e.g. N01, N90, N300
          repl_text = 'N{0:02}'.format(renumbering[CURRENT])      
          renumbering[CURRENT] += renumbering[INCREMENT]
          return repl_text
      

      However, for this scenario, we need to remember some things between runs of this function – e.g., what’s our current count value – so we have some code that appears just above the definition of return_replacement_text_func, to allow that “memory” to happen:

      renumbering = [ 1, 1 ]  # default to re-numbering starting with 1 and incrementing by 1; in the general case
      CURRENT = 0; INCREMENT = 1  # indexes into the re-numbering list
      renumbering[INCREMENT] = 10  # override our default increment-by and change it to 10, for our specific need
      

      The replacement will do the first replacement using the text N01, the second with N11, third with N21, and so on.

      Now, this all may seem very complicated if you were trying to write it from nothing, but maybe it isn’t so bad to read and follow the logic, or adapt to similar need.

      Scott RaskinS 1 Reply Last reply Reply Quote 2
      • Scott RaskinS
        Scott Raskin @Alan Kilborn
        last edited by

        @Alan-Kilborn This looks great and I will be doing some testing on it today. First thing I noticed was I needed to change this line: renumbering = [ 1, 1 ] to renumbering = [ 10, 1 ], as it has to start at 10 (or more, but I figured 10 was a good # to use, as if you remember from my other post, I need to edit some lines. So I really appreciate you making it easy, as that was very simple to find and edit. The next part I need to figure out is, where to have it do this (or do I justdo it separately. Its only 2 operations if I need to do separately and it will change 10k files so its worth it either way. The other post I had done we came up with

        replace: \A(?-s).\R.\R.\R.\R.\R.\R.*\R
        with : O0001 ;N01 ;N02 ;N03 ;N04 G94 G53 G56 T0000 ;N05 G92 X0.6820 Z2.1505 ;N06 G59 ;

        So right now, If I run your script, followed by that search/replace it seems it does all I need to do. Just wondering if that is something python allows.

        This is all amazing, and even with you being modest, you still put a good deal of effort into this and it is much appreciated

        Scott RaskinS Alan KilbornA 3 Replies Last reply Reply Quote 2
        • Scott RaskinS
          Scott Raskin @Scott Raskin
          last edited by

          @Alan-Kilborn Sorry for breaking this into two messages: I also see where you have the self.search_regex and def retuen_replacement_text_func(m) I would be l;looking to make any future code modifications, so I can probably try adding that \A(?-s).\R.\R.\R.\R.\R.\R.*\R into the first part and the replacement into the second I just dont want to screw up the code. I will be back at the factory in a few hours to see.
          Thanks again

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

            @Scott-Raskin said in renumbering/incremental:

            First thing I noticed was I needed to change this line: renumbering = [ 1, 1 ] to renumbering = [ 10, 1 ], as it has to start at 10

            You could change it the way you state, but I’d suggest this as cleaner:

            renumbering[CURRENT] = 10
            

            inserted above this line in the script:

            renumbering[INCREMENT] = 10
            

            The “current” value is the starting value, and also the “running” value as the replacement moves along (that’s why I didn’t want to call it “starting”). Expressing one’s self in code isn’t always straightforward.

            The “increment” value is well-named, but the fact that the INCREMENT tag in the code is 1 and NEVER should be changed, well, this does seem odd (it is used for a different purpose than one might think). Expressing one’s self in code isn’t always straightforward.

            If you just think in terms of:

            • if I want to change the starting value, I need to set renumbering[CURRENT] = something

            • if I want to change the incrementing value, I need to set renumbering[INCREMENT] = something

            and forget about the rest…for this specific application.


            This is all amazing, and even with you being modest, you still put a good deal of effort into this and it is much appreciated

            Not a problem. Part of the goal was somewhat of a generic solution (as you’ve probably noticed), so that the next time a question like this comes up (and it will), there is a model of a solution for it.

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

              @Scott-Raskin said :

              …run your script, followed by that search/replace it seems it does all I need to do.

              Its only 2 operations if I need to do separately and it will change 10k files so its worth it either way

              The script is rather dedicated to its renumbering/incrementing purpose, but it is just code so you could change it to go beyond that.

              As the search/replace op you mention is just a normal one (no special math/renumber/increment stuff), you could just include an editor.rereplace(search,replace) function call right after this line:

              replacements_made_in_this_file = self.perform_custom_replace_in_one_file(first_time_thru)
              

              Note however that this would invalidate the script’s results reporting (at completion, telling you number of replacements made, etc.). Not a big deal if you trust the script, mainly I put that stuff in to be general purpose, and to give a user a sanity check if they know about how many files should be matched via filespec/filter, about how many replaces should have been done, etc.

              If I were you, I’d keep it as two separate and distinct operations, running of a script, and then a follow-up Replace in Files op.

              Scott Raskin 0S 1 Reply Last reply Reply Quote 1
              • Scott Raskin 0S
                Scott Raskin 0 @Alan Kilborn
                last edited by

                @Alan-Kilborn tried this
                self.print(‘making replacements in’, pathname)
                replacements_made_in_this_file = self.perform_custom_replace_in_one_file(first_time_thru)
                editor.rereplace(\A(?-s).\R.&\R.\R.\R.\R.\R.\R.*\R,O0001 ;N0001 ;N0002 ;N0003 ;N0005 G94 G53 G56 T0000 ;N0010 G92 X0.6820 Z2.1505 ;
                ) self.print(‘{} replacements made in current file’.format(replacements_made_in_this_file))

                and the script wont even run now. Maybe youre right and 2 ops is the best

                Scott RaskinS Alan KilbornA 2 Replies Last reply Reply Quote 0
                • Scott RaskinS
                  Scott Raskin @Scott Raskin 0
                  last edited by

                  @Alan-Kilborn copy/paste made it look weird for some reason in the post but that search/replace was all one line in the script

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

                    @Scott-Raskin said in renumbering/incremental:

                    copy/paste made it look weird for some reason in the post

                    If you want it to appear verbatim in a posting, you have to format it correctly.

                    Add lines with 3 backticks at the start and bottom of your “block”:

                    ```
                    one
                    two
                    three
                    ```

                    Do that and it will appear like this:

                    one
                    two
                    three
                    

                    Alternatively, select the text to format and press the “code” button in the “toolbar” to get the same effect:

                    3b9761d1-033d-44e5-a085-6496b5910c8b-image.png

                    More info on FORMATTING posts is in the FAQ entry concerning it.

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

                      @Scott-Raskin-0

                      Concerning:

                      Maybe youre right and 2 ops is the best

                      I think so…especially due to your newness to the scripting world. As you’ve found, it is very easy to screw up something to the point where a previously-running script now won’t run. (This is often even easy for scripting veterans to do.)

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

                        @Scott-Raskin

                        Are you making a “macro” out of your second op, the standard Replace in Files operation?

                        If so, then it really becomes less painful as a separate operation; you don’t have to fill in all the fields in the UI each time, you just pick the “macro” from the Macro menu to run it.

                        So your workflow becomes:

                        • run the script
                        • run the macro

                        Downside of a macro: For a Replace in Files macro, the folder is “hardcoded”.

                        Scott Raskin 0S 1 Reply Last reply Reply Quote 1
                        • Scott Raskin 0S
                          Scott Raskin 0 @Alan Kilborn
                          last edited by

                          @Alan-Kilborn so sorry, we were closed for a bit and Im just getting back. I will try those both this week, thank you so much for all of your help!

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

                            If you’ve used a script in this thread, you might want to double check your copy of it for a bug I’ve discovered.
                            Look to previous postings in this topic thread where the script has been changed – find the text moderator edit (2024-Jan-14).
                            There’s a link there that describes the bug in more detail, and shows what needs to be changed in an old copy (or you can simply grab a copy of the current version).

                            1 Reply Last reply Reply Quote 1
                            • First post
                              Last post
                            The Community of users of the Notepad++ text editor.
                            Powered by NodeBB | Contributors