Posts

Showing posts from 2017

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);

Variable assignment in JavaScript

var num1 = 10; var num5 = 40; num1=num2=num3=num4=num5;   console.log("Value of num1", num1); console.log("Value of num2", num2); console.log("Value of num3", num3); console.log("Value of num4", num4); console.log("Value of num5", num5);    Question: What is the value of num1, num2, num3, num4 and num5? Answer: 40 Assignment is done from right to left.

Promise in Javascript

let promiseToFixSomething = new Promise(function(resolve, reject) { //Learning let isFixed = false; if (isFixed ) { resolve('Fixed!!'); } else { reject('Nah'); } }); promiseToFixSomething.then(function(fromResolve) { console.log('Fixed?' + fromResolve); }).catch(function(fromReject){ console.log('Fixed?' + fromReject); });

JS Call, Apply and Bind

. bind () when you want that function to later be called with a certain context( Returns function ) . call () or . apply () when you want to invoke the function immediately ( Returns value ) var object = {num:2}; var myFunction = function(arg1, arg2, arg3){      return this.num + arg1 + arg2 + arg3; }; console.log(myFunction.call(object, 1, 2, 3)); console.log(myFunction.apply(object, [1, 2, 3])); var bound = myFunction.bind(object); console.log(bound(1, 2, 3));

React - State v/s Props

Props and state are used to store dynamic information in   react. Dynamic information can change, by definition. But there is a difference. A React component should use props to store information that can be   changed,   but can only be changed by a different component. A React component should use the state to store information that the component itself can change.