A better PostgreSQL CLI experience with few psqlrc tweaks

From psql man page:

psql attempts to read and execute commands from the system-wide startup file (psqlrc) and then the user's personal startup file (~/.psqlrc), after connecting to the database but before accepting normal commands. These files can be used to set up the client and/or the server to taste, typically with \set and SET commands.

Like with any dotfiles we can utilize psqlrc to customize psql behavior, look and feel. First, let’s create psqlrc. We have three options:

Continue Reading...

Simplifying and extending Jekyll CLI capabilities using Jtasks

When I started using Jekyll, serving a site was as simple as short jekyll serve command. After creating a few first posts I discovered drafts feature. Now serving a site command became jekyll serve --drafts. Then Jekyll 3 was released and I have figured out that GitHub Pages not necessarily switched to building hosted sites using the latest Jekyll release (now GitHub Pages supports Jekyll 3, but migration took a few months). I’m hosting this blog at GitHub, so I had to keep my development environment in align with environment used by GitHub. Otherwise, developing with Jekyll 3 locally and then pushing results to GitHub’s build server on Jekyll 2.4 may result in compatibility issues. Luckily, keeping environment in alignment with GitHub can be easily achieved by using gh-pages gem.

Continue Reading...

Jekyll development environment via Vagrant and PuPHPet

It happened that I developed at both Linux and Windows machines. And as you may know, installing, building and maintaining development environments in such case is always a burden. Common solution is to keep your environment inside a virtual machine (VM). This prevents from polluting the hostbox with unnecessary software. Most importantly, we can set up our VM to mimic the production environment as closely as possible.

The traditional scenario of maintaining a development VM introduces a set of routines which we prefer to avoid. First, a lot of initial setup. Second, we can easily damage our setup by making some minor changes. Third, it is difficult to share pre-configured VMs because they occupy a couple of gigabytes. This is a time consuming and error-prone process…

Continue Reading...

Python solutions for the Project Euler (problems 1-10)

pic tag

Once I have found Project Euler’s website and started writing scripts to solve proposed problems. I’m not a regular visitor there, but over time a small collection of solutions has emerged. So, I decided to put results in order, push them to GitHub and share here a Python solutions for the first ten problems.

I think given examples may be helpful for Python beginners as mathematical nature of the problems assists in showing pure Python in action on the most abstract use cases. Solutions below contain examples of utilizing following Python concepts: generators, lambda functions, slices, list and set comprehensions.

Spoiler Warning

Subsequent sections provide ready-made solutions. It is highly recommended to read the next sections only if the subject problems are already solved by you. Otherwise it may cost you lots of inspirations and “Aha!” moments.

Real learning is an active process and seeing how it is done is a long way from experiencing that epiphany of discovery.

Continue Reading...

How to implement Tags at Jekyll website

ear tag

Recently I have finished adding tagging infrastructure into this website. That’s was a bit tricky since Jekyll does not fully support this feature from the box. It is possible to add tags into the post’s YAML front matter and then access them using page varialbes via page.tags. However, there are no built-in means to generate tags page which collects all tags from the posts, sorts them alphabetically and builds a list of the posts assigned to every tag. Let’s take a look at one possible way to implement described functionality.

Continue Reading...

Digging around factorial function

Evolution of a Python programmer is a brilliant example of Python code produced by people with different backgrounds to solve a mostly simple problem of finding a factorial of the given number. Unfortunately it lacks solution which might be hypothetically produced by mathematician. Let’s add some entropy to the Universe and implement a weird factorial calculation using exponential function and logarithms:

analytical expression

The series in the exponent can be represented as a list comprehension:

[math.log(k) for k in range(1, x + 1)]
Continue Reading...

Python zip( ) Fu

In functional programming iterating over few things simultaneously is called “zip”. Python has built-in zip function for this. From the docs:

zip(*iterables)

Make an iterator that aggregates elements from each of the iterables.

Returns an iterator of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables. The iterator stops when the shortest input iterable is exhausted. With a single iterable argument, it returns an iterator of 1-tuples. With no arguments, it returns an empty iterator.

As an example, lets consider iteration over two lists:

>>> colors = ['red', 'green', 'blue']
>>> vals = [55, 89, 144, 233]
>>> for col, val in zip(colors, vals):
...     print(col, val)
...
('red', 55)
('green', 89)
('blue', 144)
Continue Reading...