Monday, May 28, 2018

How to merge a branch in git

Follow the commands below to do the auto merge the branch in git:

$ git checkout -b {new-branch-name}
$ git merge --no-ff {old-branch-name}
$ git push -u origin {new-branch-name}

Friday, May 25, 2018

Concatenating and combining lists in Python

CODE:
[4, None, 'foo'] + [7, 8, (2, 3)]

or

x = [4, None, 'foo']
x.extend([7, 8, (2, 3)])
print(x)


OUTPUT:
[4, None, 'foo', 7, 8, (2, 3)]



Ternary expressions in Python

A ternary expression in Python allows you to combine an if-else block that produces
a value into a single line or expression. The syntax for this in Python is:

value = true-expr if condition else false-expr

Here, true-expr and false-expr can be any Python expressions. It has the identical
effect as the more verbose:

if condition:
    value = true-expr
else:
    value = false-expr


This is a more concrete example:

CODE:
x = 5
'Non-negative' if x >= 0 else 'Negative'

OUTPUT:
'Non-negative'

What is pass in Python

pass is the "no-op" statement in Python. It can be used in blocks where no action is to
be taken (or as a placeholder for code not yet implemented); it is only required
because Python uses whitespace to delimit blocks:

Eg:

if x < 0:
    print('negative!')
elif x == 0:
    # TODO: put something smart here
    pass
else:
    print('positive!')

How to chain comparisons in Python

CODE:
print(4 > 3 > 2 > 1)


OUTPUT:
True


What are the datetime format specifications (ISO C89 compatible) in Python

Below are the Datetime format specification (ISO C89 compatible) in Python:




Why triple quotes (" " ") are used in Python

For multiline strings with line breaks, you can use triple quotes, either ''' or " " ":

c = """
This is a long string that
spans multiple lines
"""

Standard Python scalar types

Below are the standard Python scalar types:

Type: Description
None: The Python "null" value (only one instance of the None object exists)
str: String type; holds Unicode (UTF-8 encoded) strings
bytes: Raw ASCII bytes (or Unicode encoded as bytes)
float: Double-precision (64-bit) floating-point number (note there is no separate double type)
bool: A True or False value
int: Arbitrary precision signed integer





How to check/get/set an attribute or a method in Python (Reflection)

Python allows an easy way to check whether an object contains/has an attribute or method with the specified name as follows:

1) getattr:
Gets the attribute/method with the specified name, usage as follows:
usage: getattr(obj, 'method/attribute name')
eg: 
a='RK'; 
getattr(a, 'split')


2) hasattr:
Checks whether object has the attribute/method with the specified name, usage as follows:

usage: hasattr(obj, 'method/attribute name')
eg: 
a='RK'; 
hasattr(a, 'split')


3) setattr:
Sets the attribute/method with the specified name, usage as follows:
usage: setattr(obj, 'method/attribute name', value)
eg: 
a=Employee(23)
setattr(a, 'id', 45)


Python assignments are by references

When assigning a variable (or name) in Python, you are creating a reference, have a look at below:

CODE:
a=[1,2]
b=a
print("Before append: ", b)
b.append(3)
print("Shows a & b are same references, after append to b, a: ", a)



OUTPUT:

Before append:  [1, 2]
Shows a & b are same references, after append to b, a:  [1, 2, 3]