I need to have my title as the keyname the problem is this could cause problems with duplicate keynames, how can i check if it exists and add -1 to the end if it does, or add -2 to the end if -1 exists.
keyName = "hello"
duplicates = Entry.get_by_key_name(keyName)
if duplicates:
keyName = keyName+("-1")
How do i loop through adding 1 until I find a unique name?
any help is much appreciated J
keyName = "hello"
testName = keyName
suffix = 0
while Entry.get_by_key_name(testName):
suffix += 1
testName = "%s-%d" % (keyName, suffix)
keyName = testName
A different way to think about the problem:
from itertools import imap, dropwhile, count
def make_name(i):
stem = "foo"
return stem if i == 0 else "{0}-{1}".format(stem, i)
def in_universe(name):
return bool(Entry.get_by_key_name(name))
seq = dropwhile(in_universe, imap(make_name, count()))
keyName = seq.next()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With