Is this a good way to work with init and exception

Cecil Westerhof Cecil at decebal.nl
Sun Jul 19 06:35:16 EDT 2015


I am using libturpial to post things on Twitter. But sometimes I get a
ServiceOverCapacity exception. So I wrote the following code.

======================================================================
class InitAlreadyDoneError(Exception):
    pass


##### Functions
def init(max_tries = 5, wait_time = 60):
    global _core

    if _core != None:
        raise InitAlreadyDoneError
    tries = 0
    while True:
        try:
            _core = Core()
            break
        except ServiceOverCapacity:
            tries += 1
            sys.stderr.write('Tried to init _core it {0} times\n'.format(tries))
            sys.stderr.flush()
            if tries >= max_tries:
                raise
            time.sleep(wait_time)
======================================================================

Is this the correct way to work user defined exceptions, or should I
also define a default message?

I use this in the following way:
    import twitterDecebal
    twitterDecebal.init()

Because you can not give parameters with an import as far as I can
see. Is this a good way to do this, or is there a better way?

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof



More information about the Python-list mailing list