Path: csiph.com!optima2.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!newsfeed.straub-nv.de!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Cecil Westerhof Newsgroups: comp.lang.python Subject: Is this a good way to work with init and exception Date: Sun, 19 Jul 2015 12:35:16 +0200 Organization: Decebal Computing Lines: 42 Message-ID: <87380kzb8b.fsf@Equus.decebal.nl> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="528adfd6ad074c92fdc6a7f8fb9e23d8"; logging-data="23218"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/2+9NbTg3y74svQ7KtsrdAYb2RVJdmjSo=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) X-Homepage: http://www.decebal.nl/ Cancel-Lock: sha1:MtIFgqVXoCFMiH7Cdtf2Sc5IjXc= sha1:3AG0KAGtDqLbCgbu1H+QvSnaxSg= Xref: csiph.com comp.lang.python:94111 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