This deserves to be blogged about :)

On Tue, Oct 01, 2013 at 02:28:27PM -0700, Toshio Kuratomi wrote:
>    In fedmsg_meta_fedora_infrastructure/pkgdb.py:
> 
>  > @@ -57,7 +57,11 @@ def subtitle(self, msg, **config):
>  >          elif 'pkgdb.package.update' in msg['topic']:
>  >              tmpl = self._(u"{agent} made some updates to {package}")
>  >              agent = msg['msg']['agent']
>  > -            package = msg['msg']['package']
>  > +            try:
>  > +                package = msg['msg']['package_listing']['package']['name']
>  > +            except KeyError:
>  > +                package = msg['msg']['package']
>  > +
> 
>    I should probably also add a counter example: If you're checking a cache
>    for a key value and if the key isn't present, then you're adding the value
>    to the cache, look before you leap is a more intuitive approach (and then
>    you just have to remember to use for i in dict to keep it fast).
> 
>    Also note two things: try; except: vs for i in dict isn't a big difference
>    and which is faster depends on the data that you're working with. Here's a
>    short script and some output;
> 
>  #!/usr/bin/python2 -tt
>  from __future__ import print_function
> 
>  import timeit
> 
>  MAX = 1000000
>  def setup_favor_forgiveness():
>      working = {}
>      for i in range(0, MAX):
>          working[i] = {'t': 0}
>      return working
> 
>  def setup_favor_look():
>      working = {}
>      return working
> 
>  def test_forgive(setup):
>      working = setup()
>      for i in range(0, MAX):
>          try:
>              working[i]['t'] += 1
>          except KeyError:
>              working[i] = {'t': 1}
> 
>  def test_leap(setup):
>      working = setup()
>      for i in range(0, MAX):
>          if i in working:
>              working[i]['t'] += 1
>          else:
>              working[i] = {'t': 1}
> 
>  print('Setup the working set to be as kind to try: except (ask forgiveness) as possible')
>  t = timeit.repeat(stmt='test_forgive(setup_favor_forgiveness)', setup='from __main__ import test_forgive, setup_favor_forgiveness', number=10, repeat=3)
>  print ('Ask Forgiveness: {}'.format(t))
> 
>  t = timeit.repeat(stmt='test_leap(setup_favor_forgiveness)', setup='from __main__ import test_leap, setup_favor_forgiveness', number=10, repeat=3)
>  print ('Look Before you Leap: {}'.format(t))
> 
>  print()
>  print('Setup the working set to be as kind to testing first (look before you leap) as possible')
>  t = timeit.repeat(stmt='test_forgive(setup_favor_look)', setup='from __main__ import test_forgive, setup_favor_look', number=10, repeat=3)
>  print ('Ask Forgiveness: {}'.format(t))
> 
>  t = timeit.repeat(stmt='test_leap(setup_favor_look)', setup='from __main__ import test_leap, setup_favor_look', number=10, repeat=3)
>  print ('Look Before you Leap: {}'.format(t))
> 
>    Setup the working set to be as kind to try: except (ask forgiveness) as
>    possible
>    Ask Forgiveness: [8.483088970184326, 8.197084903717041, 8.197520971298218]
>    Look Before you Leap: [8.844825983047485, 8.833319187164307,
>    8.838846921920776]
> 
>    Setup the working set to be as kind to testing first (look before you
>    leap) as possible
>    Ask Forgiveness: [19.554786205291748, 19.594372987747192,
>    19.589397192001343]
>    Look Before you Leap: [5.846503019332886, 5.843125104904175,
>    5.851045846939087]
> 
>    So -- from an optimization standpoint, try; except is faster when you
>    don't expect to invoke the exception very frequently. Luckily that matches
>    with what we want to accomplish here: We'd like the code to work with both
>    an old and a new server version. Once the server is updated in production
>    we should go down the exception handler code path again.
> 
>    -
>    Reply to this email directly or view it on GitHub.