Next, Function or Method ?

While in Python 2 it was possible to use both the function next() and the .next() method to iterate over the resulting values of a generator, the later has been removed with the introduction of Python 3. Consider the sample generator: def sample_generator(): yield "a" yield "b" yield "c" In Python 2: a = sample_generator() print(next(a)) # prints 'a' print(a.next()) # prints 'b' But in Python 3: print(next(a)) # prints 'a' print(a....

April 26, 2017 · 1 min · Rezha Julio

Generator Expressions

Generator expressions are a high performance and memory efficient generalization of list comprehensions and generators. Imagine we want to sum up all even number ranging from 1 to 100. Using list comprehension: even_sum = sum([x for x in range(1, 100) if x % 2 == 0]) print(even_sum) #2450 This will prove inefficient in the case of a large range because it first creates a list, it iterates over it and then returns the sum....

April 24, 2017 · 1 min · Rezha Julio

Yield Keyword

The yield keyword is fundamental to the creation of generators. Consider the following generator function: def createGenerator(): print('Initial call') yield '1' print('Second call') yield '2' a = createGenerator() Calling the createGenerator() function will create a generator object stored as a. Note that the code inside the generator function will not be run yet. print(next(a)) # Initial call # 1 The first time the generator object is iterated over (in a loop or with next()), the function code will be run from the start until the first yield....

April 23, 2017 · 1 min · Rezha Julio

How to Build Nginx with Google Pagespeed Support

Nginx (engine-x) is an open source and high-performance HTTP server, reverse proxy and IMAP/POP3 proxy server. The outstanding features of Nginx are stability, a rich feature set, simple configuration and low memory consumption. This tutorial shows how to build a Nginx .deb package for Ubuntu 16.04 from source that has Google PageSpeed module compiled in. PageSpeed is a web server module developed by Google to speed up the website response time, optimize the returned HTML and reduce the page load time....

April 22, 2017 · 3 min · Rezha Julio

What are Generators?

Generators are special functions that implement or generate iterators. Generators are functions which behave like iterators, but can have better performance characteristics. These include: Creating values on demand, resulting in lower memory consumption. The values returned are lazily generated. Hence, it is not necessary to wait until all the values in a list are generated before using them. However, the set of generated values can only be used once. Generators look like normal functions, but instead of the return statement they make use of the yield statement....

April 21, 2017 · 1 min · Rezha Julio