The Python range() Function

Python’s built-in range function is handy when you need to perform an action a specific number of times. As an experienced Pythonista, you’ve most likely used it before. But what does it do? By the end of this course, you’ll: Understand how the Python range function works Know how the implementations differ in Python 2 and Python 3 Have seen a number of hands-on range() examples Be equipped to work around some of its limitations Let’s get cracking! [ Improve Your Python With 🐍 Python Tricks 💌 – Get a…

Read More

GitHub webhook payload as a cloud function

Back in 2014, I wrote a post describing a simple payload server for GitHub webhooks, using Python 3. That server could be deployed to any VPS listening on a custom port. Now it’s 2019, and deploying servers to VPSs doesn’t make me feel hip enough. All the cool kids are into serverless now, so I decided to rewrite the same payload server in Go and deploy it as a Google Cloud Function. This brief post can serve as a basic tutorial on how to do it. I assume you already…

Read More
Programming 

GeoIP service as a cloud function

In this post I’ll go through a complete code sample that deploys a simple service as a Google Cloud Function (GCF). The service uses built-in capabilities of the GCF runtime to serve GeoIP (a.k.a. GeoLocation) information about its clients, letting dynamic web pages know where their visitors are coming from. The complete code sample is available here. Free GeoIP information from GCP Since client-side JS doesn’t have easy access to the visitor’s IP address, the traditional approach to find GeoIP information is to invoke a server endpoint; done properly, this…

Read More

PostgreSQL 11’s Support for SQL Standard GROUPS and EXCLUDE Window Function Clauses

Exciting discovery when playing around with PostgreSQL 11! New SQL standard window function clauses have been supported. If you want to play with this, you can do so very easily using docker: docker pull postgres:11 docker run –name POSTGRES11 -e POSTGRES_PASSWORD=postgres -d postgres:11 docker run -it –rm –link POSTGRES11:postgres postgres psql -h postgres -U postgres See also: https://hub.docker.com/r/_/postgres The frame clause When working with window functions, in some cases you want to add the optional frame clause. For example, to get a sliding average over your data, you will write:…

Read More

How to Write a Multiplication Aggregate Function in SQL

Everyone knows the SQL SUM() aggregate function (and many people also know its window function variant). When querying the Sakila database, we can get the daily revenue (using PostgreSQL syntax): WITH p AS ( SELECT CAST (payment_date AS DATE) AS date, amount FROM payment ) SELECT date, SUM (amount) AS daily_revenue, SUM (SUM (amount)) OVER (ORDER BY date) AS cumulative_revenue FROM p GROUP BY date ORDER BY date The result will look something like this: date |daily_revenue |cumulative_revenue ———–|————–|——————- 2005-05-24 |29.92 |29.92 2005-05-25 |573.63 |603.55 2005-05-26 |754.26 |1357.81 2005-05-27 |685.33…

Read More

MySQL CURRENT_TIMESTAMP() Function

This tutorial explains how to use the MySQL CURRENT_TIMESTAMP() function with the help of examples. By using it, you can convert or display the current date and time. The output format is either ‘YYYY-MM-DD HH:MM: SS’ format or YYYYMMDDHHMMSS, which depends on the context of the calling function, whether it is numeric or string. The function NOW() and CURRENT_TIMESTAMP() are equivalent of the CURRENT_TIMESTAMP. Please note that examples shown here will give results depending upon the present date. MySQL CURRENT_TIMESTAMP() This function provides a date and time value formatted in…

Read More

Using the Python zip() Function for Parallel Iteration

Python’s zip() function creates an iterator that will aggregate elements from two or more iterables. You can use the resulting iterator to quickly and consistently solve common programming problems, like creating dictionaries. In this tutorial, you’ll discover the logic behind the Python zip() function and how you can use it to solve real-world problems. By the end of this tutorial, you’ll learn: How zip() works in both Python 3 and Python 2 How to use the Python zip() function for parallel iteration How to create dictionaries on the fly using…

Read More

The Separator and End Arguments of the Python Print Function

Everybody knows the print function in Python. It prints a string to the shell–and makes the computation of a Python program explicit to the programmer. But what other arguments can be used to format the output? There are two other arguments: sep and end. Consider the following example: a = ‘hello’ b = ‘world’ print(a, b, sep=’ Python ‘, end=’!’) The print function has several parameters which you can use to format the output. The parameter sep indicates the separator which is printed between the objects. By default sep is…

Read More

Calling an Oracle Function with PL/SQL BOOLEAN Type from SQL

One of the most wanted features in the Oracle database is the BOOLEAN type. The SQL standard specified it a while ago, and RDBMS like PostgreSQL show how powerful it can be, e.g. when using the EVERY() aggregate function. The PL/SQL language already has support for boolean types. We can write: CREATE OR REPLACE FUNCTION number_to_boolean (i NUMBER) RETURN BOOLEAN IS BEGIN RETURN NOT i = 0; END number_to_boolean; / CREATE OR REPLACE FUNCTION boolean_to_number (b BOOLEAN) RETURN NUMBER IS BEGIN RETURN CASE WHEN b THEN 1 WHEN NOT b…

Read More