0

I'm a complete beginner in Python, and I'm trying to use the language to make scripts in a program called Modo.

I'm attempting to make a script that duplicates an item which has a suffixed number, and adds 1 to the suffix of the new duplicated item.

So for example; duplicate 'item_01', which will create 'item_01 (2)', then rename 'item_01 (2)' to 'item_02'.

I'm having trouble finding out how to get Python to take the '_01' part of the previous item's name, then add 1 to it and using the sum of that as the suffix for the new item name.

Also, this is my first question on this great site, so if additional information is needed, please let me know.

Alex
  • 3
  • 1
  • 1
    Could you include an example of the code you have tried so far? Where does 'item_01' come from in your example? A list, a text file? – TheoretiCAL Jan 11 '18 at 18:29
  • 1
    What should be the new name for `item_99`? What should be the new name for `item_23_42`? – Kevin Jan 11 '18 at 18:35
  • Your code is **essential** since we can see what you are trying to do, and so, we can tell you if it's a bad practice, which one is the good way to achieve what you're trying to do, and so on. – IMCoins Jan 11 '18 at 18:45
  • Between ` for code formatting. Select a piece of code, and press Ctrl+K. Also > if you want to quote some text. – IMCoins Jan 11 '18 at 18:53
  • 'Item_01' is just an example of a selected item in the program. So, for example, if my script is 1 line that's `duplicateItem()`, that's a command for Modo to run on whatever item is selected. An example of code to duplicate an item, then print its name would be: `duplicateItem() print(modo.Item().name)` I've got some python code in my script to 'split' the name at the '_', but I'm not sure where the suffix number goes or how to add to it and use that sum. – Alex Jan 11 '18 at 18:54

2 Answers2

1

I'm interpreting your question as "I have a string ending in a digit sequence, for example "item_01". I want to get a string with the same form as the original string, but with the digit incremented by one, for example "item_02"."

You could use re.sub to replace the digit sequence with a new one:

>>> import re
>>>
>>> s = "item_01"
>>> result = re.sub(
...     r"\d+$",                    #find all digits at the end of the string,
...     lambda m: str(              #replacing them with a string
...         int(m.group())+1        #equal to one plus the original value,
...     ).zfill(len(m.group())),    #with at least as much padding as the original value.
...     s
... )
>>>
>>> print(result)
item_02

In one line, that would be

result = re.sub(r"\d+$", lambda m: str(int(m.group())+1).zfill(len(m.group())), s)

Note that the resulting string may be longer than the original string, if the original value is all nines:

>>> re.sub(r"\d+$", lambda m: str(int(m.group())+1).zfill(len(m.group())), "item_99")
'item_100'

And it will only increment the digit sequence at the very end of the string, and not any intermediary sequences.

>>> re.sub(r"\d+$", lambda m: str(int(m.group())+1).zfill(len(m.group())), "item_23_42")
'item_23_43'

And if the string has no suffix digit sequence, it will simply return the original value unaltered.

>>> re.sub(r"\d+$", lambda m: str(int(m.group())+1).zfill(len(m.group())), "item_foobar")
'item_foobar'
Kevin
  • 74,910
  • 12
  • 133
  • 166
  • This resolves my issue, I only had to adapt `s = "item_01"` to my own function which is built around the syntax for working in Modo. I'm going to see if I can use the 'result' to name the new duplicate item, as well as seeing how I could repeat the script for many item duplicates. Thank you very much (and everyone else who took the time to help me). – Alex Jan 11 '18 at 19:29
0

Getting the name out of a variable is not something you do in Python.

What you want to achieve here, reading what you wrote,

I'm attempting to make a script that duplicates an item which has a suffixed number, and adds 1 to the suffix of the new duplicated item.

So for example; duplicate 'item_01', which will create 'item_01 (2)', then rename 'item_01 (2)' to 'item_02'.

would be more convenient such as below :

some_var = 1
some_other_var = some_var + 1

You could have a function doing "I am adding one to the parameter I received and I return the value" !

def add_one(var):
    return (var + 1)

some_var = 1
some_other_var = add_one(some_var)

If you want to "name" your variables, and be able to change them, even if I don't see why you would want to do this, I believe what you are looking for is a dict.

I am letting you look at the reference for the dictionnary though. :)

Community
  • 1
  • 1
IMCoins
  • 3,149
  • 1
  • 10
  • 25