Skip to main content

· 4 min read

What I learned and understand so far...

1. Divide and Conquer

This stay on the top as it's the most important principle, for instances when we are being provided with a business case with huge list of requirements, we started to confuse ourselves and often the solution came up straight away potentially not the best solution. The idea of divide is to decompose a given problem into two or more similar, sub-problems, which then we conquer by composing solutions to the give problem.

2. Increase cohesion

A good object oriented design must be loosely coupled and highly cohesive. This design principle have been created based on the idea of Loose coupling and high cohesion. Don't mixed up with Cohesion and Coupling, as cohesion refers to what a class, module or function can do. Low cohesion would means that it does great variety of action being unfocused on what it should do. Another way to think of this is to have something that could not be broken down further, and should be doing only one thing.

Example of Low Cohesion:


Example of High Cohesion:


3. Reduce coupling

This refers to how related or dependent of two classes / modules. Low coupled would mean that changing something in major should not affect the other. High coupling would increase the difficulties of code maintenance, a change of module usually forces a ripple effect of changes in other modules. The lesser dependencies the better

4. Increase abstract

Abstraction is one of the key principles behind many of the OO design principles such as below and is the hardest in my opinion. The idea is to have a simplified version of something technical and the goal is to reduce complexity.

  • Inheritance
  • Polymorphism
  • Composition
  • Benefits of abstraction
  • Code is easy to understand
  • Manages change and the effect of change
  • Creates cohesive code – it finds common fields
  • Create loose coupling

abstraction is the ability to view a complex operation in a simplified form. A class interface provides an abstraction of the implementation that’s hidden behind the interface

Some examples below:





You will just lock down the attributes like x, y, areas

5. Increase re-usability

Think of writing codes as re-usable as possible, the challenge part could be keeping the balance on spending more time to design and write a general function. I see this as a investment of time to reduce the future time needed to go back and understand the piece of code.

6. Design for flexibility

Think ahead and anticipate that the fact of there will be a change in the future, as and when the business grows, requirement changes and more. A-lot of time when comes to an end of project, I realised that I have to add some features and based on the way I have written my codes, that's not possible. Ending up spending more time to re-write and re-design the whole program.

7. Anticipate Obsolescence

  • Avoid early release and version of software
  • Use software from reputable companies
  • Use as few external dependencies as possible
  • Avoid poorly documented or maintained projects

8. Design for portability / scalability

Think ahead about possibility to have your program in other platform. Things can get harder when your client based grows and you do not have the ability to scale your program or port to another platform.

9. Design for testability

This is important especially for larger code based project. Design the code in a way that you are able to test the code. Think of test case, functional test, unit test, and find out a way to test the code.

10. Design defensively

Idiot proof your code. Good error messages, handling all the invalid input, handling wrong output.

· 4 min read

Trying to understand what is refactoring, follow the guide from Real Python.

1. Functions That Should Be Objects

Without reading the code, you will not know if it will modify the original image or create a new image."
def load_image(path):
with open(path, "rb") as file:
fb = file.load()
image = img_lib.parse(fb)
return image

def crop_image(image, width, height):
return image

def get_image_thumbnail(image, resolution=100):
return image

To call the codes:

from imagelib import load_image, crop_image, get_image_thumbnail

image = load_image('~/face.jpg')
image = crop_image(image, 400, 500)
thumb = get_image_thumbnail(image)

Symptoms of code using functions that could be refactored into classes:

Similar arguments across functions
Higher number of Halstead h2 unique operands (All the variables and constants are considered operands)
Mix of mutable and immutable functions
Functions spread across multiple Python files

Here is a refactored version of those 3 functions, where the following happens:

.__init__() replaces load_image().
crop() becomes a class method.
get_image_thumbnail() becomes a property.

The thumbnail resolution has become a class property, so it can be changed globally or on that particular instance:
class Image(object):
thumbnail_resolution = 100

def __init__(self, path):

def crop(self, width, height):

def thumbnail(self):
return thumb

This is how the refactored example would look:

from imagelib import Image

image = Image('~/face.jpg')
image.crop(400, 500)
thumb = image.thumbnail

In the resulting code, we have solved the original problems:

It is clear that thumbnail returns a thumbnail since it is a property, and that it doesn’t modify the instance.
The code no longer requires creating new variables for the crop operation.

2. Objects That Should Be Functions

Here are some tell-tale signs of incorrect use of classes:

Classes with 1 method (other than .__init__())
Classes that contain only static methods
authentication class
class Authenticator(object):
def __init__(self, username, password):
self.username = username
self.password = password

def authenticate(self):
# Do something
return result

It would make more sense to just have a simple function named authenticate() that takes username and password as arguments:
def authenticate(username, password):
# Do something
return result

3. Converting “Triangular” Code to Flat Code

These are the symptoms of highly nested code:

A high cyclomatic complexity because of the number of code branches
A low Maintainability Index because of the high cyclomatic complexity relative to the number of lines of code
def contains_errors(data):
if isinstance(data, list):
for item in data:
if isinstance(item, str):
if item == "error":
return True
return False

Refactor this function by “returning early”

def contains_errors(data):
if not isinstance(data, list):
return False
return data.count("error") > 0

Another technique to reduce nesting by list comprehension

Common practise to create list, loop through and check for criteria.

results = []
for item in iterable:
if item == match:

Replace with:

results = [item for item in iterable if item == match]

4. Handling Complex Dictionaries With Query Tools

It does have one major side-effect: when dictionaries are highly nested, the code that queries them becomes nested too.

data = {
"network": {
"lines": [
"name.en": "Ginza",
"": "銀座線",
"color": "orange",
"number": 3,
"sign": "G"
"name.en": "Marunouchi",
"": "丸ノ内線",
"color": "red",
"number": 4,
"sign": "M"

If you wanted to get the line that matched a certain number, this could be achieved in a small function:

def find_line_by_number(data, number):
matches = [line for line in data if line['number'] == number]
if len(matches) > 0:
return matches[0]
raise ValueError(f"Line {number} does not exist.")
find_line_by_number(data["network"]["lines"], 3)

There are third party tools for querying dictionaries in Python. Some of the most popular are JMESPath, glom, asq, and flupy.

import jmespath"network.lines", data)
[{'name.en': 'Ginza', '': '銀座線',
'color': 'orange', 'number': 3, 'sign': 'G'},
{'name.en': 'Marunouchi', '': '丸ノ内線',
'color': 'red', 'number': 4, 'sign': 'M'}]

If you wanted to get the line number for every line, you could do this:

> > >"network.lines[*].number", data)
> > > [3, 4]

You can provide more complex queries, like a == or <. The syntax is a little unusual for Python developers, so keep the documentation handy for reference.

Find the line with the number 3

> > >"network.lines[?number==`3`]", data)
> > > [{'name.en': 'Ginza', '': '銀座線', 'color': 'orange', 'number': 3, 'sign': 'G'}]

· 8 min read

Enable the ssh-agent service

To enable SSH Agent automatically on Windows, start PowerShell as an Administrator and run the following commands:

# Make sure you're running as an Administrator
Set-Service ssh-agent -StartupType Automatic
Start-Service ssh-agent
Get-Service ssh-agent

Adding ssh keys

Run these commands in a terminal window within Visual Studio Code.

Show keys managed by the ssh-agent

ssh-add -l

Add a ssh key


For git, add a system environment variable or use a temporary setting in a PowerShell terminal of VSCode.


If you add the line $env:GIT_SSH="C:\Windows\System32\OpenSSH\ssh.exe" to your Powershell profile the environment variable will always be used.

Using SSH Keys in Visual Studio Code on Windows

Visual Studio Code is Microsoft’s open-source code editor for Windows, OS X and Linux. Nicely, VS Code has built-in support for Git and support for Python through an extension, making it a useful for scientific development. Using VS Code on Windows is somewhat frustrated, however, if you want to work with a Git repository that was cloned using SSH. Thankfully, I found a workable solution using PuTTY and Git for Windows, such that VS Code transparently works with password-protected SSH keys. Below, I detailed how I got it working in as complete a detail as reasonable, but you may have already done some or even many of these steps. If so, the procedure is actually fairly simple, and consists of pointing Git (and hence VS Code) to use PuTTY and Pageant instead of the SSH version that ships with Git for Windows.

First, though, a disclaimer. These steps worked on my Windows 10 installation, but may not work on yours. If you find that this is the case, let me know, and I’ll try and update accordingly.

Step 0. Install Required Software

Before we get into things, we’ll need a bit of software. In particular, we’ll need:


Do not install PuTTY from its official homepage, as this will download PuTTY over an insecure connection. This guide will cover how to download PuTTY securely.

For much of this, we can use the Chocolatey package manager for Windows to save some grief, so let’s start by installing that. If you already have Chocolatey, please skip this step. (If you aren’t sure, try running choco from PowerShell.) Run PowerShell as administrator, then run the following command to download and install Chocolatey:

PS> Set-ExecutionPolicy -Scope Process RemoteSigned
PS> iwr -UseBasicParsing | iex

Once this is done, close and reopen PowerShell (again as administrator). This will make choco available as a command. Now we can use it to install Git and OpenSSH (as above, we will not install PuTTY using Chocolatey, as it will download PuTTY from its official homepage using an insecure connection). Run the following PowerShell commands to install Git and OpenSSH:

PS> choco install git
PS> choco install win32-openssh

We’ll finish up by downloading the version of PuTTY that ships with WinSCP, since that version is delivered via HTTPS and not insecure HTTP. In particular, use this link to download PuTTY, then run the installer once you’ve downloaded it.

Step 1. Setup Private Keys

Once everything is installed, we now need to make sure that you have an SSH private key and that this key is registered with your Git hosting service (for instance, GitHub or Bitbucket). If you already have keys and have registered them with your hosting provider, please skip on ahead.

In any case, to generate keys, we’ll again use PowerShell:


Simply follow the prompts to make yourself a new public/private key pair, making sure to choose a long (~40 character) passphrase. This passphrase provides much of the entropy for your key, such that it should be much longer than a typical password. Never type your passphrase into a remote password prompt— the passphrase is used to unlock your key locally on your machine, and should never be sent over the network. If a website asks you for your SSH passphrase, you are probably being scammed.

By default, the new keys will be located in C:\Users\<username>\.ssh\id_rsaandC:\Users\<username>\.ssh\ As the names suggest, the first of these is theprivatekey and should not be shared with anyone. The other is the public key, and serves to identify yourself to others. Follow the instructions for GitHub or Bitbucket (for Bitbucket, make sure to follow the Linux and OS X instructions, even from Windows) to upload yourpublickey to your hosting provider.

Step 2. Set up SSH Agent

Next, we’ll make sure that your private key is setup in an SSH agent. This will securely remember your passphrase within a given session, so that you don’t have to type it in every time you use it. In particular, we’ll configure Pageant, since this is installed with PuTTY, and works well with a variety of command-line and GUI tools for Windows— most notably, with VS Code.

Pageant must be run at startup in order to be useful, so we’ll begin by adding it to the startup folder now. In Windows Explorer (Windows 8.1 and earlier) or in File Explorer (Windows 10 and later), go to the folder C:\Users\<username>\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup. Right-click inside this folder and select New → Shortcut. From there, browse to C:\Program Files (x86)\PuTTY and select pageant.exe.

Next, we need to import your new key into PuTTY/Pageant.

  1. Run PuTTYgen from the Start Menu and select File → Load Key....
  2. From there, navigate to C:\Users\<username>\.ssh\ and select id_rsa (the private key). You may have to drop down the file types selector in the dialog box to see this, as PuTTYgen defaults to filtering out everything but files ending in *.ppk. Once selected, you’ll be prompted by PuTTY to unlock your key by typing in your passphrase. Do so, and PuTTYgen will show the corresponding public key.
  3. Select File → Save private key to export your private key in PuTTY, rather than OpenSSH, format. I suggest saving it as id_rsa.ppk in the same folder as id_rsa, but this is up to you. Just be sure that to save it in a folder that only you can read, and that is not synchronized using Dropbox, OneDrive, Google Drive or similar.

Finally, run Pageant from the Start Menu (in the future, this will be handled automatically by the shortcut we created above). This will add a new icon to your system tray. It may be hidden by the arrow; if so, click the arrow to make all fo the system tray icons visible. Right-click on Pageant and select Add Key. Browse to where you saved id_rsa.ppk and select it. You’ll be prompted to unlock your key. Upon doing so, your unlocked key will then be made available in Pageant until you log out or quit Pageant.

Step 3. Add SSH Server Fingerprints

Despite the name, this is a short step. Whenever you log into an SSH server, PuTTY will check that the server’s fingerprint is correct. This is a short cryptographic string identifying that server, such that checking the fingerprint helps against man-in-the-middle attacks. If you haven’t logged into a server with PuTTY before, however, it has no idea how to check the fingerprint, and will fail to login. Since VS Code ignores these errors, Git support will silently fail unless you first attempt to log into the SSH server offered by your Git host. To do so, we’ll use PowerShell one last time. Run one of the following commands below, depending on which hosting provider you use.

PS > & 'C:\Program Files (x86)\PuTTY\plink.exe' [email protected]
PS > & 'C:\Program Files (x86)\PuTTY\plink.exe' [email protected]

In either case, you’ll be prompted to add the server’s fingerprint to the registry. If you are confident that your traffic is not being intercepted, selectyat this prompt. Neither GitHub nor Bitbucket actually allows logins via SSH, so you’ll get an error, but this is OK: you’ve gotten far enough to see the server’s fingerprint, and that’s all we needed. To check, you can run the commands above again, and note that you are no longer prompted to add the fingerprint, but instead fail immediately.

Step 4. Configure Environment Variables

We’re almost done. All that’s left is to point Git for Windows at PuTTY and Pageant, rather than its own built-in SSH client. Since VS Code uses Git for Windows, this will ensure that VS Code does what we want.

  1. Right-click on My Computer or This PC in Windows/File Explorer, and select Properties.
  2. From there, click Advanced system settings in the sidebar to the left. On the Advanced tab, press the Environment Variables... button at the bottom.
  3. Finally, click New... on the user variables pane (top), and add a new variable named GIT_SSH with value C:\Program Files (x86)\PuTTY\plink.exe.
  4. You may want to use Browse File... in this dialog box to make sure you get the path correct.
  5. Once done, press OK to add the variable,OK again to close the Environment Variables dialog, then OK a third time to close System Properties.
  6. Finally, close the System window.

· 2 min read

Since the task was to simply use another branch instead of master, you can simply remove master branch completely or rename it to let's say - legacy, then take another branch and rename it to master. That's it. Here are actual commands that you might need to execute to achieve the goal locally and on GitHub:

git branch -m master legacy               # rename local master to legacy
git checkout legacy
git branch -m another_branch master # another_branch will be our new master

Locally we are done now. However you cannot simply remove master branch on GitHub. You need to take another branch as default first. This can be done in repository Settings > Default Branch. Once you do this, you can proceed:

git push origin :master                   # remove master on GitHub
git push origin master # push out our new master branch
git push origin legacy # push our legacy branch too

Then go back to Settings > Default Branch and switch default branch back to master. Additionally you can remove all extra branches that you might have created during migration process.

Alternatively, if you are looking to save all your actions in history, check a correct answer here.

· One min read
hugo server --noHTTPCache --disableFastRender
hugo server --noHTTPCache --disableFastRender --ignoreCache

Create Post

hugo new post/create-virtual-environment-in-linux/

Install Hugo

curl -s \
| grep browser_download_url \
| grep Linux-64bit.deb \
| grep -v extended \
| cut -d '"' -f 4 \
| wget -i -

curl -s \
| grep browser_download_url \
| grep extended_0.81.0_Linux-64bit.deb \
| cut -d '"' -f 4 \
| wget -i -