[Chicago] April Talk Topic Python 3.2 Features

Brian Ray brianhray at gmail.com
Fri Mar 9 15:56:21 CET 2012


Does anybody want to present on
this<http://docs.python.org/py3k/whatsnew/3.2.html>at our April
meeting (at Groupon.com)? Is this a good topic (can I get a
+1)? If I can not convince someone else on presenting I am sure I can
probably do something for 20-30 minutes.

I had some food for thought (my example code and notes) while reviewing
3.2.  I am not a 3 expert at all and have been stuck in the 2.X world for
ages.

I found: 2 things I like and 2 things I do not quite understand (my advance
apologies if your email reader does not support html):

*Like 1)* concurrent.futures seems pretty neat. However I had to realize
(doh) how exceptions are caught and be sure to get those with
future.exceptions(). See my commented out 1/0 below to raise an exception
in the body of a future *thing*

import concurrent.futures
import time
import random

def _(): time.sleep(random.randint(0,9)) # ; 1/0

pool_size = random.randint(5,15)
print('printing {s} items finding'
      ' squares between 1 and {s}'.format(s=pool_size))

# Callback when each is done
def cb(future):
    if future.exception() is not None:
        print('generated an exception: %s' % future.exception())
    else:
        print(future.result())

# A simple runner based on pool_size
with concurrent.futures.ThreadPoolExecutor(max_workers=pool_size) as e:
    for i in range(pool_size):
        i += 1
        e.submit(lambda x: _() or (x,x**2), i).add_done_callback(cb)


*Like 2) *Caching with functools lru_cache


import functools


@functools.lru_cache(maxsize=300)
def function_that_does_heavy_lifting(same_thing):
    print( "should only print once for {}".format(same_thing) )
    return same_thing

print(function_that_does_heavy_lifting("beer"))
print(function_that_does_heavy_lifting("beer"))
print(function_that_does_heavy_lifting("juice"))
print(function_that_does_heavy_lifting("beer"))
print(function_that_does_heavy_lifting("juice"))
print(function_that_does_heavy_lifting("juice"))


should only print once for beer
beer
beer
should only print once for juice
juice
beer
juice
juice

*Question 1) * Why does {} have 0?


table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
print('Jack: {0[Jack]:d}; Sjoerd: {0[Sjoerd]:d}; '
           'Dcab: {0[Dcab]:d}'.format(table))

Jack: 4098; Sjoerd: 4127; Dcab: 8637678

*Question 2)* what is the difference between nonlocal and global?

def scope_test():
    def do_local():
        spam = "local spam"
    def do_nonlocal():
        nonlocal spam
        spam = "nonlocal spam"
    def do_global():
        global spam
        spam = "global spam"

    spam = "test spam"
    do_local()
    print("After local assignment:", spam)
    do_nonlocal()
    print("After nonlocal assignment:", spam)
    do_global()
    print("After global assignment:", spam)

scope_test()
print("In global scope:", spam)

FYI this <http://docs.python.org/py3k/tutorial/classes.html> says """The
global <http://docs.python.org/py3k/reference/simple_stmts.html#global>statement
can be used to indicate that particular variables live in the
global scope and should be rebound there; the
nonlocal<http://docs.python.org/py3k/reference/simple_stmts.html#nonlocal>statement
indicates that particular variables live in an enclosing scope
and should be rebound there.""" However, I do not understand yet fully what
that means.


Cheers, Brian

-- 
Brian Ray
@brianray
(773) 669-7717
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/chicago/attachments/20120309/63fca839/attachment-0001.html>


More information about the Chicago mailing list