Posts

Why to use get(key) instead of [key] for Python Dictionary?

Accessing value from a dictionary using square brackets will raise an exception if there is no value mapped to that key, here is an example >>> dict = {'key1':1,'key2':2} >>> dict['key1'] 1 >>> dict['key3'] Traceback (most recent call last): File "", line 1, in dict['key3'] KeyError: 'key3' On the other hand, 'get' will return the default value if there is no value mapped to that key, the default value is 'None' by default >>> dict = {'key1':1,'key2':2} >>> dict['key1'] 1 >>> a = dict.get('key3) >>> print a 0 To set a custom default value >>> a = dict.get('key3', 'MyCustomValue')

Checkout specific folder in git

git checkout -- path/to/folder

psql: could not connect to server

If you come across an error like this in POSTGRESQL: psql: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"? Then, use the following command to solve the issue: sudo chmod a+w /var/run/postgresql

GIT : Praise or Blame

Git has a very useful blame feature which allows you to check which developer was responsible for changing a particular piece of code: $ git blame -L5,10 app.css

GIT : Show Changed Words Instead of Whole Lines

This works with git diff and git show git diff --word-diff Or git diff --color --word-diff myFile.js

Copy object to clipboard from javascript console

Use copy() in your chrome or firefox browser to copy the entire object onto your clipboard. For example: copy(myObject);

ES6 Destructuring and Template strings

var obj = { name:"X", surname: "Y" } var { name, surname } = obj; console.log("name", name); console.log("surname", surname); var values = ["Hey", "Hi", "Ssup"]; var [ x, y, z ] = values; console.log("x", x); console.log("y", y); console.log("y", z); // Template strings using backtics var str = `Value of x is ${x} and vallue of y is ${y}`; console.log(str);