I'm looking for the most pythonic way of trying a command, catching if an error occurs and retrying by running a preparatory command and then the original command. Specifically in my case I'm looking to write a table to a database, catch if a "schema does not exist" error is thrown, then trying to create a schema and retrying the write table. Then, if the write table errors again I don't want to catch it.
So far I have (schematically):
try:
write_to_table(table, schema)
except sqlalchemy.exc.ProgrammingError:
create_schema(schema)
write_to_table(table, schema)
This does what I want, but seems a bit off somehow, maybe because I'm duplicating write_to_table().
So what's the most pythonic way of doing the above?
P.S. When I say I'd like to retry, I do NOT want something like this: How to retry after exception?