Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #94114

Re: Is this a good way to work with init and exception

Path csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'none:': 0.05; 'tries': 0.05; 'correct.': 0.07; 'exception.': 0.07; 'raises': 0.07; 'see.': 0.07; 'cc:addr:python-list': 0.09; '"run': 0.09; 'appropriate.': 0.09; 'exceptions,': 0.09; 'global,': 0.09; 'implies': 0.09; 'imports': 0.09; 'throw': 0.09; 'valueerror': 0.09; 'way:': 0.09; 'skip:= 70': 0.10; 'exception': 0.13; 'def': 0.13; 'appropriate': 0.14; '#####': 0.16; 'construct.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'hierarchy': 0.16; 'redundant.': 0.16; 'runtimeerror': 0.16; 'sorts': 0.16; 'subclassing': 0.16; 'subject:exception': 0.16; 'true:': 0.16; '{0}': 0.16; 'wrote:': 0.16; '"you': 0.18; 'basically': 0.18; 'try:': 0.18; 'together.': 0.20; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'saying': 0.22; "aren't": 0.22; 'explicit': 0.22; 'init': 0.22; 'logical': 0.22; 'pass': 0.22; 'code.': 0.23; 'decide': 0.23; 'defined': 0.23; 'wrote': 0.23; 'second': 0.24; 'import': 0.24; 'header:In-Reply- To:1': 0.24; "doesn't": 0.26; 'error': 0.27; 'define': 0.27; 'parameters': 0.27; 'message-id:@mail.gmail.com': 0.27; 'correct': 0.28; 'looks': 0.29; 'once,': 0.29; 'way?': 0.29; 'raise': 0.29; 'strongly': 0.30; "i'd": 0.31; 'operations': 0.31; 'probably': 0.31; 'post': 0.31; 'especially': 0.32; 'maybe': 0.33; 'run': 0.33; 'class': 0.33; "i'll": 0.33; 'case,': 0.34; 'except': 0.34; 'add': 0.34; 'received:google.com': 0.35; 'possible,': 0.35; 'something': 0.35; 'sometimes': 0.35; 'but': 0.36; 'skip:i 20': 0.36; 'should': 0.36; 'there': 0.36; 'subject:work': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'things': 0.38; 'doing': 0.38; "won't": 0.38; 'someone': 0.38; 'does': 0.39; 'where': 0.40; 'subject:with': 0.40; 'called': 0.40; 'your': 0.60; 'default': 0.61; 'back': 0.62; 'more': 0.63; 'different': 0.63; 'here': 0.66; 'jul': 0.72; 'cecil': 0.84; 'chrisa': 0.84; "it'd": 0.84; 'subject:good': 0.84; 'westerhof': 0.84; 'subject:this': 0.85; 'absolutely': 0.88; 'to:none': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=Dt4DFo039sZWzSviiLb9HyUieMf/ux+yaKGVDvgSo+Q=; b=yQInoqMqoowzmaSjcrDDonF9w26oTo1yiQ+R6viEdh9EZC/5ntYkrlTtP5LsdVVjCv zcAW4T/NGxp70ECuvZ0a7fib5kSxakCRgYQ837PqaJOGQnU1fe17GFYQ9anHcy8PRvZE 1R0eP3VQomJzBclIkYOdlhWn9YcwtSIrH5XXPGUis4td8+oDyV+Oa4cA9I7I+839lHbe lg4r7ySkDDr0SMRue1avxpKcjiVzCFWfOYRIdzhI0t8rBdCZc63J+FsyJR0G9MqGqJy/ rZUsnHxdFbrLlFPu554ODZ/OMPmQwr2uLs3Xcyi8r1qR79JM0D+3ndpi/UpvOfRL7fMT mG7A==
MIME-Version 1.0
X-Received by 10.50.136.194 with SMTP id qc2mr7876704igb.6.1437310766839; Sun, 19 Jul 2015 05:59:26 -0700 (PDT)
In-Reply-To <87380kzb8b.fsf@Equus.decebal.nl>
References <87380kzb8b.fsf@Equus.decebal.nl>
Date Sun, 19 Jul 2015 22:59:26 +1000
Subject Re: Is this a good way to work with init and exception
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.711.1437310769.3674.python-list@python.org> (permalink)
Lines 71
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1437310769 news.xs4all.nl 2844 [2001:888:2000:d::a6]:60179
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:94114

Show key headers only | View raw


Reordering/interleaving your post to respond to different parts together.

On Sun, Jul 19, 2015 at 8:35 PM, Cecil Westerhof <Cecil@decebal.nl> wrote:
> 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

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

I'd start by looking through the exception hierarchy for something
appropriate to subclass. In this case, you're basically saying "run
init() exactly once, and if you run it a second time, I'll throw back
an error", which probably doesn't have any logical match, so directly
subclassing Exception would be correct. But you might decide that
subclassing ValueError or RuntimeError is more appropriate.

> ##### Functions
> def init(max_tries = 5, wait_time = 60):
>     global _core
>
>     if _core != None:
>         raise InitAlreadyDoneError

This is where I'd add a message, if you want one. But it looks to me
as if there's never going to be any other place that raises this, so
the message would be redundant. InitAlreadyDoneError implies "you
called init() after someone else called init()".

(Side point: It might be a neat courtesy to let people call init
again, or maybe a try_init() that won't error out if already
initialized.)

>     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)
> ======================================================================
>
>
> 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?

Parameterized imports aren't possible, correct. What I'd look at here
is a more explicit instantiation. Something like:

import twitterDecebal
twitter = twitterDecebal.twitterDecebal(5, 60)

Especially since it's something that does a ton of network operations
and all sorts of sleeps and timeouts, I would strongly recommend NOT
doing this on import, even if you could. If you don't absolutely
_need_ it to be global, it'd be cleanest to make it a class that you
construct.

ChrisA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Is this a good way to work with init and exception Cecil Westerhof <Cecil@decebal.nl> - 2015-07-19 12:35 +0200
  Re: Is this a good way to work with init and exception Chris Angelico <rosuav@gmail.com> - 2015-07-19 22:59 +1000
    Re: Is this a good way to work with init and exception Cecil Westerhof <Cecil@decebal.nl> - 2015-07-19 18:46 +0200
      Re: Is this a good way to work with init and exception Chris Angelico <rosuav@gmail.com> - 2015-07-20 04:11 +1000
        Re: Is this a good way to work with init and exception Cecil Westerhof <Cecil@decebal.nl> - 2015-07-19 21:10 +0200
          Re: Is this a good way to work with init and exception Chris Angelico <rosuav@gmail.com> - 2015-07-20 07:08 +1000
            Re: Is this a good way to work with init and exception Cecil Westerhof <Cecil@decebal.nl> - 2015-07-20 00:19 +0200
              Re: Is this a good way to work with init and exception Chris Angelico <rosuav@gmail.com> - 2015-07-20 08:40 +1000
                Re: Is this a good way to work with init and exception Cecil Westerhof <Cecil@decebal.nl> - 2015-07-20 01:27 +0200

csiph-web