Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Beautiful Python Idioms

Beautiful Python Idioms

Wrap Up of Ned Batchelder's and Raymond Hettinger's conferences at PYCON US 2013 for the first Paris.py meeting, holding in Paris June, 12th 2013.

Olivier Hervieu

June 12, 2013
Tweet

More Decks by Olivier Hervieu

Other Decks in Programming

Transcript

  1. WHO AM I? olivier hervieu software python developer at tinyclues

    my twitter (junk) account: @ _ o l i v i e r _
  2. WHY THIS PRESENTATION? inspired by Raymond Hettinger's conference at Pycon

    inspired by Ned Batchelder's conference at Pycon 90 minutes of conferences in 10!
  3. LOOPING OVER NUMBERS bad: even worse: better: f o r

    i i n r a n g e ( 6 ) : p r i n t i * * 2 f o r i i n [ 0 , 1 , 2 , 3 , 4 , 5 ] : p r i n t i * * 2 f o r i i n x r a n g e ( 6 ) : p r i n t i * * 2
  4. LOOPING OVER COLLECTIONS evil things: f o r i i

    n x r a n g e ( l e n ( c o l o r s ) ) : p r i n t c o l o r s [ i ] f o r i i n x r a n g e ( l e n ( c o l o r s ) - 1 , - 1 , - 1 ) : p r i n t c o l o r s [ i ] c o l o r s = [ ' b l u e ' , ' r e d ' , ' y e l l o w ' , ' p u r p l e ' ] f o r i i n x r a n g e ( l e n ( c o l o r s ) ) : p r i n t i , c o l o r s [ i ]
  5. please consider (and use!) the following: f o r c

    o l o r i n c o l o r s : p r i n t c o l o r f o r c o l o r i n r e v e r s e d ( c o l o r s ) : p r i n t c o l o r c o l o r s = [ ' b l u e ' , ' r e d ' , ' y e l l o w ' , ' p u r p l e ' ] f o r i , c o l o r i n e n u m e r a t e ( c o l o r s ) : p r i n t i , c o l o r
  6. LOOPING OVER TWO (OR MORE) COLLECTIONS dumb version: and what

    you really want to do: the generator way: see also: i z i p _ l o n g e s t in i t e r t o o l s f i r s t n a m e s = [ ' A l i c e ' , ' B o b ' , ' J o h n ' ] l a s t n a m e s = [ ' D u p o n t ' , ' S l e i g h ' , ' D o e ' ] a g e s = [ 2 4 , 6 3 , 3 3 ] z i p p e d _ d a t a = [ ] n = m i n ( l e n ( f i r s t n a m e s ) , l e n ( l a s t n a m e s ) , l e n ( a g e s ) ) f o r i i n r a n g e ( n ) : z i p p e d _ d a t a . a p p e n d ( ( f i r s t n a m e s [ i ] , l a s t n a m e s [ i ] , a g e s [ i ] ) ) z i p ( f i r s t n a m e s , l a s t n a m e s , a g e s ) i z i p ( f i s t n a m e s , l a s t n a m e s , a g e s )
  7. CREATING DICTIONARIES FROM PAIRS f i r s t n

    a m e s = [ ' A l i c e ' , ' B o b ' , ' J o h n ' ] a g e s = [ 2 4 , 6 3 , 3 3 ] d = d i c t ( i z i p ( f i r s t n a m e s , a g e s ) ) # { ' A l i c e ' : 2 4 , ' B o b ' : 6 3 , ' J o h n ' : 3 3 } d = d i c t ( e n u m e r a t e ( f i r s t n a m e s ) ) # { 0 : ' A l i c e ' , 1 : ' B o b ' , 2 : ' J o h n ' }
  8. MASTERING LIST/SET/DICT COMPREHENSION or is equivalent to: s u m

    = 0 f o r i i n x r a n g e ( 1 0 ) : s u m + = i * * 2 p r i n t s u m p o w _ 2 = [ ] f o r i i n x r a n g e ( 1 0 ) : p o w _ 2 . a p p e n d ( i * * 2 ) p r i n t s u m ( p o w _ 2 ) p r i n t s u m ( [ i * * 2 f o r i i n x r a n g e ( 1 0 ) ] ) p r i n t s u m ( i * * 2 f o r i i n x r a n g e ( 1 0 ) )
  9. c o l o r s = [ ' b

    ' , ' r ' , ' g ' , ' g ' , ' r ' , ' r ' , ' r ' , N o n e ] u n i q u e _ c o l o r s = { c o l o r f o r c o l o r i n c o l o r s i f c o l o r } p o w _ 2 _ m a p = { k : k * * 2 f o r k i n x r a n g e ( 1 0 0 0 0 ) }
  10. READING FILES the < 2 . 6 way: you should

    use: f = o p e n ( ' d a t a . t x t ' ) t r y : d a t a = f . r e a d ( ) f i n a l l y : f . c l o s e ( ) w i t h o p e n ( ' d a t a . t x t ' ) a s f : d a t a = f . r e a d ( )
  11. CONTEXT MANAGER probably my favorite use case scenario: avoiding the

    t r y / e x c e p t / p a s s pattern t r y : o s . r e m o v e ( ' s o m e f i l e . t m p ' ) e x c e p t O S E r r o r : p a s s f r o m c o n t e x t l i b i m p o r t c o n t e x t m a n a g e r @ c o n t e x t m a n a g e r d e f i g n o r e d ( * e x c e p t i o n s ) : t r y : y i e l d e x c e p t e x c e p t i o n s : p a s s w i t h i g n o r e d ( O S E r r o r ) : o s . r e m o v e ( ' s o m e f i l e . t m p ' )
  12. I M P O R T I N F I

    N I T Y i t e r t o o l s . c o u n t ( ) i t e r t o o l s . c y c l e ( )
  13. POWERFUL METHODS common methods come with handful parameters: t a

    l l _ b u i l d i n g s = { " E m p i r e S t a t e " : 3 8 1 , " S e a r s T o w e r " : 4 4 2 , " B u r j K h a l i f a " : 8 2 8 , " T a i p e i 1 0 1 " : 5 0 9 , } p r i n t m a x ( t a l l _ b u i l d i n g s . v a l u e s ( ) ) p r i n t m a x ( t a l l _ b u i l d i n g s . i t e m s ( ) , k e y = l a m b d a b : b [ 1 ] ) # ( ' B u r j K h a l i f a ' , 8 2 8 ) p r i n t m a x ( t a l l _ b u i l d i n g s , k e y = t a l l _ b u i l d i n g s . g e t ) # ' B u r j K h a l i f a '
  14. c o l o r s = [ ' r

    e d ' , ' g r e e n ' , ' b l u e ' , ' y e l l o w ' ] f o r c o l o r i n s o r t e d ( c o l o r s ) : p r i n t c o l o r f o r c o l o r i n s o r t e d ( c o l o r s , r e v e r s e = T r u e ) : p r i n t c o l o r d e f c o m p a r e _ l e n g t h ( c 1 , c 2 ) : i f l e n ( c 1 ) < l e n ( c 2 ) : r e t u r n - 1 i f l e n ( c 1 ) > l e n ( c 2 ) : r e t u r n 1 r e t u r n 0 p r i n t s o r t e d ( c o l o r s , c m p = c o m p a r e _ l e n g t h ) p r i n t s o r t e d ( c o l o r s , k e y = l e n )
  15. FOR/ELSE STATEMENTS use the e l s e statement to

    detect if a b r e a k has been encountered: d e f f i n d ( s e q , t a r g e t ) : f o u n d = F a l s e f o r i , v a l u e i n e n u m e r a t e ( s e q ) : i f v a l u e = = t g t : f o u n d = T r u e b r e a k i f n o t f o u n d : r e t u r n - 1 r e t u r n i d e f f i n d ( s e q , t a r g e t ) : f o r i , v a l u e i n e n u m e r a t e ( s e q ) : i f v a l u e = = t g t : b r e a k e l s e : r e t u r n - 1 r e t u r n i
  16. naive implementation: c o l o r s = [

    ' r e d ' , ' g r e e n ' , ' r e d ' , ' b l u e ' , ' g r e e n ' , ' r e d ' ] d = { } f o r c o l o r i n c o l o r s : i f c o l o r n o t i n d : d [ c o l o r ] = 0 d [ c o l o r ] + = 1
  17. slightly better: d = { } f o r c

    o l o r i n c o l o r s : d [ c o l o r ] = d . g e t ( c o l o r , 0 ) + 1
  18. in limbo: f r o m c o l l

    e c t i o n s i m p o r t d e f a u l t d i c t d = d e f a u l t d i c t ( i n t ) f o r c o l o r i n c o l o r s : d [ c o l o r ] + = 1
  19. python guru: f r o m c o l l

    e c t i o n s i m p o r t C o u n t e r C o u n t e r ( c o l o r s )
  20. GENERATORS d e f e v e n s (

    s t r e a m ) : f o r n i n s t r e a m : i f n % 2 = = 0 : y i e l d n f o r n i n e v e n s ( x r a n g e ( 1 0 0 0 0 ) ) : d o _ s o m e t h i n g ( n )
  21. NAMEDTUPLE c l a s s B o o k

    O r d e r ( o b j e c t ) d e f _ _ i n i t _ _ ( s e l f , n a m e , q u a n t i t y ) : s e l f . n a m e = n a m e s e l f . q u a n t i t y = q u a n t i t y f r o m c o l l e c t i o n i m p o r t n a m e d t u p l e B o o k O r d e r = n a m e d t u p l e ( ' B o o k O r d e r ' , [ ' n a m e ' , ' q u a n t i t y ' ] )
  22. POWERFUL ITERATION CONTINUE A FUNCTION UNTIL A SENTINEL VALUE IS

    REACHED everybody write this: write this instead: b l o c k s = [ ] w h i l e T r u e : b l o c k = f . r e a d ( 3 2 ) i f b l o c k = = ' ' : b r e a k b l o c k s . a p p e n d ( b l o c k ) b l o c k s = [ ] f o r b l o c k i n i t e r ( p a r t i a l ( f . r e a d , 3 2 ) , ' ' ) : b l o c k s . a p p e n d ( b l o c k )
  23. FIND ELEMENTS n e x t ( ( i f

    o r i i n x r a n g e ( 1 0 0 0 ) i f i > 1 0 a n d i % 2 ) , 0 )
  24. ABSTRACTING ITERATION thanks to generators, simplify your code: f =

    o p e n ( " m y _ c o n f i g . i n i " ) f o r l i n e i n f : l i n e = l i n e . s t r i p ( ) i f l i n e . s t a r t s w i t h ( ' # ' ) : # A c o m m e n t l i n e , s k i p i t . c o n t i n u e i f n o t l i n e : # A b l a n k l i n e , s k i p i t . c o n t i n u e # A n i n t e r e s t i n g l i n e . d o _ s o m e t h i n g ( l i n e )
  25. d e f i n t e r e s

    t i n g _ l i n e s ( f ) : f o r l i n e i n f : l i n e = l i n e . s t r i p ( ) i f l i n e . s t a r t s w i t h ( ' # ' ) : c o n t i n u e i f n o t l i n e : c o n t i n u e y i e l d l i n e w i t h o p e n ( " m y _ c o n f i g . i n i " ) a s f : f o r l i n e i n i n t e r e s t i n g _ l i n e s ( f ) : d o _ s o m e t h i n g ( l i n e )
  26. USING DECORATORS the web cache example: powered re-usability with decorators!

    d e f w e b _ l o o k u p ( u r l , s a v e d = { } ) : i f u r l i n s a v e d : r e t u r n s a v e d [ u r l ] p a g e = u r l l i b . u r l o p e n ( u r l ) . r e a d ( ) s a v e d [ u r l ] = p a g e r e t u r n p a g e @ c a c h e d e f w e b _ l o o k u p ( u r l ) : r e t u r n u r l l i b . u r l o p e n ( u r l ) . r e a d ( ) d e f c a c h e ( f u n c ) : s a v e d = { } @ w r a p s ( f u n c ) d e f n e w f u n c ( * a r g s ) : i f a r g s i n s a v e d : r e t u r n n e w f u n c ( * a r g s ) r e s u l t = f u n c ( * a r g s ) s a v e d [ a r g s ] = r e s u l t r e t u r n r e s u l t r e t u r n n e w f u n c
  27. EXPLICIT IS BETTER THAN IMPLICIT is better than: and: is

    better than: f i r s n a m e , l a s t n a m e , a g e = [ " J o h n " , " D o e " , 3 3 ] w h o a m i = [ " J o h n " , " D o e " , 3 3 ] f i r s t a n a m e = w h o m a i [ 0 ] l a s t n a m e = w h o a m i [ 1 ] a g e = w h o a m i [ 2 ] t w i t t e r _ s e a r c h ( ' @ o b a m a ' , r e t w e e t s = F a l s e , n u m t w e e t s = 2 0 , p o p u l a r = T r u e ) t w i t t e r _ s e a r c h ( ' @ o b a m a ' , F a l s e , 2 0 , T r u e )
  28. UNLIMITED DEFAULT DICT i n f i n i t

    e _ d e f a u l t d i c t = l a m b d a : d e f a u l t d i c t ( i n f i n i t e _ d e f a u l t d i c t )
  29. MATRIX TRANSPOSITION WITH ZIP a = [ [ 1 ,

    2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] t r _ a = [ l i s t ( i ) f o r i i n z i p ( * a ) ]