Reputation: 108
So basically I'm trying to generate different caseIDs, but to avoid duplicates I'm trying to re-run the function if the case is already in the database.
Here's some code:
async def openuseless():
with open("caseids.json", "r") as f:
caseids = json.load(f)
return caseids
async def addcaseidtodb(caseid):
e = openuseless()
e[str(caseid)] = {}
with open("caseids.json", "w") as f:
json.dump(e, f)
return True
async def id_generator(size=10, chars=string.ascii_uppercase + string.digits):
x = ''.join(random.choice(chars) for _ in range(size))
existing = openuseless()
for caseid in existing:
if caseid == x:
# repeat function
print("e")
else:
await addcaseidtodb(x)
The questions is: How would I repeat the function from if caseid == x?
Upvotes: 0
Views: 42
Reputation: 54193
The general pattern for this is
def some_function():
while True:
# do things
if not repeat_condition:
break # or return or etc
You'll see this a lot for input validation.
def get_integer(prompt="Enter an integer: "):
while True:
user_input = input(prompt)
try:
result = int(user_input)
except ValueError:
print("Invalid input -- must be an integer")
return result
Upvotes: 1