Python & Perl                         PY Generators     Generator Factories, Generator Objects,              Generator Lif...
Outline   ●   OOP Background: Factory Design Pattern   ●   Generator Construction   ●   Generator Factories   ●   Generato...
OOP Background               Factory Design Patternwww.youtube.com/vkedco      www.vkedco.blogspot.com
Design Patterns   ●   In OOP, design patterns are referred to       standardized ways of capturing specify       types of ...
Factory Design Pattern   ●   Factory is an OOP design pattern that       generalizes the constructor concept   ●   In some...
Generatorswww.youtube.com/vkedco         www.vkedco.blogspot.com
Motivation   ●   Generators    provide    for     very     succinct       computational patterns       They are useful bot...
Definition   ●   Let us use this definition: a generator is an object       that iterates over some data in a lazy fashion...
Generators as Memory Conservation Devices   ●   You can think of       generators    as   memory       conservation device...
Generator Construction   ●   In Python, there are two ways to construct       generators:         – generator  factories  ...
Generator Factorieswww.youtube.com/vkedco      www.vkedco.blogspot.com
Generator Factories   ●   Every Python function that has the keyword       yield in its body defines a generator factory  ...
Example   ●   Why is gen_factory_1234 a generator factory?   ●   Because on each invocation it creates a new generator obj...
Generator Objectswww.youtube.com/vkedco      www.vkedco.blogspot.com
Generator Objects   ●   Unlike functions that return values, generators       can be construed as value sequences that yie...
Range Generator   ●   Here is a more generic way of defining a       generator factory that yields each number in       a ...
Generator Objects Lifetime   ●   Generator objects have only one lifetime:       once a generator object goes over its dat...
Examples      ## two generators defined over the same data      >>> gen1 = gen_factory_range(10, 15)      >>> gen2 = gen_f...
Reading & References  ●   http://en.wikipedia.org/wiki/Factory_method_pattern  ●   www.python.orgwww.youtube.com/vkedco   ...
Upcoming SlideShare
Loading in …5
×

Python & Perl: PY Generators, Generator Construction, Generator Factories, Generator Objects, Generator Lifetime

638 views
575 views

Published on

Python & Perl: PY Generators, Generator Construction, Generator Factories, Generator Objects, Generator Lifetime

Published in: Software
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
638
On SlideShare
0
From Embeds
0
Number of Embeds
354
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Python & Perl: PY Generators, Generator Construction, Generator Factories, Generator Objects, Generator Lifetime

  1. 1. Python & Perl PY Generators Generator Factories, Generator Objects, Generator Lifetimes Vladimir Kulyukinwww.youtube.com/vkedco www.vkedco.blogspot.com
  2. 2. Outline ● OOP Background: Factory Design Pattern ● Generator Construction ● Generator Factories ● Generator Objectswww.youtube.com/vkedco www.vkedco.blogspot.com
  3. 3. OOP Background Factory Design Patternwww.youtube.com/vkedco www.vkedco.blogspot.com
  4. 4. Design Patterns ● In OOP, design patterns are referred to standardized ways of capturing specify types of behavior ● Design patterns typically evolve bottom-up: after repeating the same task for a few times, a software engineer or a team of software engineers may notice a sequence of actions that may be worth reusing – and a design pattern is bornwww.youtube.com/vkedco www.vkedco.blogspot.com
  5. 5. Factory Design Pattern ● Factory is an OOP design pattern that generalizes the constructor concept ● In some circumstances, it is conceptually easier or more efficient to create a class whose objects produce other types of objects ● A factory class has a method for constructing each type of object it is designed to constructwww.youtube.com/vkedco www.vkedco.blogspot.com
  6. 6. Generatorswww.youtube.com/vkedco www.vkedco.blogspot.com
  7. 7. Motivation ● Generators provide for very succinct computational patterns They are useful both in research and systems programming ● They are still relatively under-used: probably because we as programmers feel safer and more confident with for-loops and iterators ● And … PY generators are beautifulwww.youtube.com/vkedco www.vkedco.blogspot.com
  8. 8. Definition ● Let us use this definition: a generator is an object that iterates over some data in a lazy fashion ● This definition has several implications: – Generators are iterators – Generators are associated with specific data – Generators are lazy: they yield data items one at a time and without necessarily storing all data items in memorywww.youtube.com/vkedco www.vkedco.blogspot.com
  9. 9. Generators as Memory Conservation Devices ● You can think of generators as memory conservation devices ● They allow you to iterate over a set (or several sets) of objects without having to store each and every one of those objects in memory ● You cannot do the same with iterables, such as lists, tuples, or dictionarieswww.youtube.com/vkedco www.vkedco.blogspot.com
  10. 10. Generator Construction ● In Python, there are two ways to construct generators: – generator factories – generator expressions ● Generator factories are used when more complex generators are required ● Generator expressions (comprehensions) are used for simpler generatorswww.youtube.com/vkedco www.vkedco.blogspot.com
  11. 11. Generator Factorieswww.youtube.com/vkedco www.vkedco.blogspot.com
  12. 12. Generator Factories ● Every Python function that has the keyword yield in its body defines a generator factory ● For example: def gen_factory_1234(): yield 1 yield 2 yield 3 yield 4www.youtube.com/vkedco www.vkedco.blogspot.com
  13. 13. Example ● Why is gen_factory_1234 a generator factory? ● Because on each invocation it creates a new generator object >>> g1 = gen_factory_1234() >>> g1.next() 1 >>> g1.next() 2 >>> g1.next() 3 >>> g1.next() 4 >>> g1.next() ## no more objects to generator so an exception is thrown StopIteration Exceptionwww.youtube.com/vkedco www.vkedco.blogspot.com
  14. 14. Generator Objectswww.youtube.com/vkedco www.vkedco.blogspot.com
  15. 15. Generator Objects ● Unlike functions that return values, generators can be construed as value sequences that yield values and remember the point at which the last value is yielded ● On the next invocation, a generator pick up from the point at which the previous value was yielded ● When there are no more values to yield, StopIteration is raisedwww.youtube.com/vkedco www.vkedco.blogspot.com
  16. 16. Range Generator ● Here is a more generic way of defining a generator factory that yields each number in a range def gen_factory_range(lower, upper): for i in xrange(lower, upper+1): yield iwww.youtube.com/vkedco www.vkedco.blogspot.com
  17. 17. Generator Objects Lifetime ● Generator objects have only one lifetime: once a generator object goes over its data (if the number of data items is finite), it cannot be restarted/reset ● You can create as many generator objects coupled to the same data as you want ● Generator objects can be consumed by standard PY sequence constructors and processorswww.youtube.com/vkedco www.vkedco.blogspot.com
  18. 18. Examples ## two generators defined over the same data >>> gen1 = gen_factory_range(10, 15) >>> gen2 = gen_factory_range(10, 15) ## two generator objects consumed by zip constructor >>> zip(gen1, gen2) [(10, 10), (11, 11), (12, 12), (13, 13), (14, 14), (15, 15)] >>> gen3 = gen_factory_range(10, 15) ## generator object consumed by list constructor >>> list(gfr3) [10, 11, 12, 13, 14, 15] ## anonymous generator object consumed by sum >>> sum(gen_factory_range(1, 5)) 15www.youtube.com/vkedco www.vkedco.blogspot.com
  19. 19. Reading & References ● http://en.wikipedia.org/wiki/Factory_method_pattern ● www.python.orgwww.youtube.com/vkedco www.vkedco.blogspot.com

×