Would your future self thank you?

Would your future self thank you?

One day I was listening to a podcast where the host interviews a businessman. At the end of the interview the host asked, “What are the things you do that make you successful?“. The businessman answered: “The reason I am successful is not because of what I do today. I owe everything to 2011 me. All his hard work, his grit, and his networking efforts paid off.

That got me thinking: am I thankful for past me? Do my actions decades ago produce something good today?

I thank 2012 me. I accepted a job from something that I have not tried before, and I was not really expecting much. It turns out to be work that boosted my income and still something I enjoy even after seven years. read more

Shadow Boxing

Shadow Boxing

You may have heard people talk about bad things that happened to someone else. “Poor guy, he lost his job“, “She was diagnosed with breast cancer“, or “They lost their child” are some of the examples. When this happens we may feel shocked and sad for the other person. We may even think “Thank God that did not happen to me!“.

But what if it happened to you?

Shadow boxing is an important technique used in combat sports, especially boxing. It does not involve any equipment, only yourself. Typically you do this with an imaginary opponent, or you can just do it in front of a mirror (so your enemy is you). This can even be used to “simulate” an upcoming fight against an opponent as you imagine him standing in front of you. read more

Rubyist’s intro to Python web development

I have been using Ruby professionally for more than a decade now. Until recently, I haven’t explored much outside of the Ruby and Rails community. That changed however after I completed a course in Foundations of Data Science. This made me curious about Python and how to build applications using it.

Python and Ruby have many similarities. Both are interpreted, high-level programming languages. Python also has support for Object-Oriented Programming and Functional Programming. In terms of syntax, they have a similar look and feel, aside from some fundamental differences such as Python being indent-driven.

You may find this article to be very similar to the Ruby on Rails guide I posted years ago. This is not accidental since my goal is to introduce Python web application development to someone who is already familiar in the Ruby space.

Installing Python

The very first step is to install Python itself in your computer. I recommend using pyenv to manage your Python versions. pyenv is a Python version manager, like rbenv. In fact, pyenv is a fork of rbenv and is re-purposed for Python. To install pyenv:

curl https://pyenv.run | bash

After installing, update your login shell configuration by adding the following, e.g. in ~/.zshrc

export PATH="/home/user/.pyenv/bin:$PATH" eval "$(pyenv init -)" eval "$(pyenv virtualenv-init -)"

Now we are ready to use pyenv to install a specific Python version. But first, we need to install some dependencies so we can compile Python from source:

sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev \
libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev \
xz-utils tk-dev libffi-dev liblzma-dev python-openssl

Then, we can easily install a Python version, like 3.6.8 in this example:

pyenv install 3.6.8

If you are having trouble installing Python, it could be related to the OpenSSL version installed in your machine.

On Debian stretch (and Ubuntu bionic), libssl-dev is OpenSSL 1.1.x,
but support for that was only added in Python 2.7.13, 3.5.3 and 3.6.0.
To install earlier versions, you need to replace libssl-dev with
libssl1.0-dev. This is being tracked in
https://github.com/pyenv/pyenv/issues/945.

Once Python has been installed, you can opt to set the version (e.g 3.6.8) as your global version. This makes the Python executable available to all terminal sessions:

pyenv global 3.6.8

Package Management

pip is Python’s package manager, like rubygems or npm. If you installed Python, pip should also be available for you. If for some reason it is not installed, you can install it using the guide here.

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py $ python get-pip.py

Application-specific Packages

In Ruby/Rails we use the awesome library Bundler to handle application package management. We manage the packages using a Gemfile, and it gets converted into Gemfile.lock.

pipenv is similar to bundler, but its functionality extends beyond package management in the application. In this article we will use it similar to bundler so it will handle all the application package dependencies. To install pipenv, just use pip!

pip install pipenv

To specify the application packages, pipenv uses a Pipfile. An example is given below:

[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]

[packages]
requests = "*"
flask = "*"
python-dotenv = "*"
flask-sqlalchemy = "*"
flask-migrate = "*"

[requires]
python_version = "3.6" read more

Rubocop + vim

Rubocop + vim

Code linters such as Rubocop ensure consistent, clean code throughout your application. If all developers are using the same linter configuration, then you can be sure that any code that you encounter is organized in the same way regardless of the author.

One challenge in using linters is that it should have immediate feedback, or else it may disrupt the coding flow of the user. For example, you can run the linter manually after you make your changes, but as this is a manual process it can be easily forgotten.

Commit Hooks

To solve this, we can use pre-commit hooks (such as in git) to automatically run the linter script whenever we try to commit our changes. In this manner, it is an automated process and so will not be skipped in development.

For example, here is a git pre-commit hook for Rubocop (from
https://gist.github.com/mpeteuil/6147292 and
https://github.com/grosser/dotfiles/pull/7 )

#!/usr/bin/env ruby
#
# put this file into your path and use `<file> install` to add a new hook
# or use it as a binary to check changed files

require 'shellwords'

if ARGV == ["install"]
exec "ln", "-sf", __FILE__, ".git/hooks/pre-commit"
else
raise unless ARGV == []
end

changed = `git status --porcelain`.
split("\n").
map { |l| l.split(" ", 2) }.
select { |status, _| ["A", "AM", "M"].include?(status) }.
map { |_, file| file.delete('"') }

exit if changed.empty?

result = `bundle exec rubocop --force-exclusion #{changed.shelljoin}`
puts result unless $?.success?
exit $?.exitstatus read more

Why Boxing?

Why Boxing?

I admit, I am not a very active person. I would rather read a book or play a game than take a walk outside. But I have a “hobby” that I was doing for years now: boxing. Even though I don’t have other physical activities, boxing helps me to keep a reasonably healthy body.

So what do I like about boxing?

You are on the same ground as the greatest

What I mean is: you have the same basic equipment as the all-time greats. All boxers started with the same equipment and the same routine that you are doing in the gym.

Wherever you go, every boxing gym has these three tools: the speed ball, the double end, and the punching bag. read more