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

Using an interface as a parameter

You can define methods that take an interface as a parameter. Your interface defines a contract and your methods will accept as parameter any objects whose class implements that interface. This is in fact one of the most common and useful ways to use an interface. interface Test { public void test(); //define the interface } class Tester { public void runTest(Test t) { t.test(); } // method with interface as param } MyTest class will implement this interface:...

April 20, 2017 · 1 min · Rezha Julio