Python Generators - Talk at PySthlm meetup #15

14
Python Generators PySthlm Meetup #15 – Python Best Practices – Oct 31, 2013 Samuel Lampa twitter.com/smllmp

description

Describing how you can use python generator functions to process a file without loading it all in memory before starting

Transcript of Python Generators - Talk at PySthlm meetup #15

Page 1: Python Generators - Talk at PySthlm meetup #15

Python Generators

PySthlm Meetup #15 – Python Best Practices – Oct 31, 2013

Samuel Lampatwitter.com/smllmp

Page 2: Python Generators - Talk at PySthlm meetup #15

The “normal” (bad) way

Page 3: Python Generators - Talk at PySthlm meetup #15

The “normal” (bad) way

New temporary data structures created and kept in memory

== BAD!!!

Page 4: Python Generators - Talk at PySthlm meetup #15

Using Generator function

Page 5: Python Generators - Talk at PySthlm meetup #15

Using Generator function

A yield (“return”) for every iteration, so it can

be used immediately without temp data structure

== GOOD!

Page 6: Python Generators - Talk at PySthlm meetup #15

Using Generator function

A yield (“return”) for every iteration, so it can

be used immediately without temp data structure

== GOOD!

So, this is the generator function

Page 7: Python Generators - Talk at PySthlm meetup #15

Using Generator function

A yield (“return”) for every iteration, so it can

be used immediately without temp data structure

== GOOD!

So, this is the generator function … which can be

iterated over, like this ...

Page 8: Python Generators - Talk at PySthlm meetup #15
Page 9: Python Generators - Talk at PySthlm meetup #15

Using list-comprehension syntax

Page 10: Python Generators - Talk at PySthlm meetup #15

Using list-comprehension syntax

Here, the generator functionIs defined in one row

Page 11: Python Generators - Talk at PySthlm meetup #15

Chaining generator functions

Page 12: Python Generators - Talk at PySthlm meetup #15

Chaining generator functions

Page 13: Python Generators - Talk at PySthlm meetup #15

Creds and more info:

● David Beazleys Python Generator intro:

→dabeaz.com/generatorsDavid Beazley on twitter: @dabeaz

Page 14: Python Generators - Talk at PySthlm meetup #15

Thank you!

Samuel LampaG+: gplus.to/samltwitter: @smllmp

Blog: saml.rilspace.org

Developer @ www.uppmax.uu.sewww.farmbio.uu.sewww.rilpartner.se