The code is Python, but it's more an algorithmic question I can't solve.
I use an API which defines a set of instances, each one is reachable by its Name
property:
print(APIClass[a_name].Name)
>>> 'a_name'
print(APIClass[another_one].Name)
>>> 'another_one'
I want (and I'm allowed) to rename a specific instance, but I want to prevent any duplicate (which leads to an API crash). If I rename the second in my example to 'a_name'
, I have first to rename the first one, eg:
APIClass[a_name].Name = 'a_name_old'
print(APIClass[a_name_old].Name)
>>> 'a_name_old'
and it's OK (APIClass[another_name] = 'a_name'
). But I have to check first that no one else has a_name_old
as property, and so on.
Should I populate a set with the Name
properties, then rename each item, and then rename the instances? But in this case (I tried it), how to be sure that a 'new' name will not be the same that an old one? (in this case, my tries failed when renaming instances) Please note I don't want to remove any duplicates, just rename them.
EDIT:
The 'APIClass' are provided, I'm only a user and I can't modify them (by adding methods, or so on). I can only modify instances. So I want to modify the 'Name' property of one of them, but I have to check (and rename if needed) the other ones. If I rename one, in my opinion I have to re-check (and re-rename) the remainings. So I could do it with a recursive algorithm, but I thought this is not efficient (for memory eg).