Archive

Archive for the ‘Uncategorized’ Category

Python: string.split() vs re.split()

While both string and regular expression in python has split() method, string.split() can split using only one separator.


def split_with_string():
    s = "how-r-you."
    assert s.split("-") == ['how', 'r', 'you.'] #True

On the other re.split() can split using regular expression. When you are using, you can build any complex separator or combine multiple separators.

def split_with_re():
    import re
    s = "how-r-you."
    assert re.split(r'[-|.]') == ['how', 'r', 'you', ''] #True

Categories: Uncategorized

Python ‘in’ operator with Iterator

I found use of in operator with iterator very useful. It would help write functional style code more easily.

list = [1,2,3,4]
list = iter(list)
assert 1 in list == True 

Explanation: iterator is matched until value 1 is matched, which is matched in the first place (index 0). list.next() would return 2 in this case.

list = [1,2,3,4]
list = iter(list)
assert 5 in list == False
list.next() # Throws exception

Explanation: 5 does not occur in the list. The list is all traversed to find occurence of 5. list.next() would raise StopIteration exception

Now lets see how to use ‘in’ operator with iterator to do something useful. Following function is written to check if the second argument is a subsequence of first argument.

def IS_word_subSequence_of_s(s,word):
 startIdx = 0
  for char in word:
    startIdx = s.find(char, startIdx)
    if startIdx == -1:
     return False
    else:
     startIdx += 1
 return True

Instead I found this code snippet [1] that very nicely use functional style to do the same thing.


def isSubsequence(s,x):
        it = iter(s)
        return all(c in it for c in x)

reference:
[1] https://discuss.leetcode.com/topic/80887/short-python-solutions

Categories: Uncategorized

Preserving states across function call in Python

Usually functions in Python is not state preserving. That means functions do not save values between calls. But you can make it happen in the following way

 

# this function remember total from last calls
def summation(value):
    summation.total = getattr(summation,"total",0) + value
    return summation.total 

When execute:

summation(1) == 1 # true

summation(2) == 3 # true

summation(3) == 6 # true

 

Saving states across function call might be useful. For example, here I am trying to accumulate sum over a list of numbers.

# clear last total value

summation.total = 0

[summation(x) for x in [1,2,3]] == [1,3,6] # true

 

Categories: python, Uncategorized

OpenStack Austin summit in pictures

 

Heading to OpenStack summit

 

DSC03544.JPG

 

@ Austin Convention Center entrance.

 

DSC03520.JPG

 

@ Austin Convention Center entrance.

 

DSC03558

 

OpenStack crowd.

 

20160425_111058

 

Brand yourself with OpenStack

 

DSC03548.JPG

 

Under OpenStack umbrella. 

 

DSC03524

 

Youngest OpenStack enthusiast

 

20160425_194202

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Keynotes

 

20160425_105048

 

Break out sessions.

 

20160425_140521.jpg

 

@ Break.

 

20160425_170206.jpg

 

@MarketPlace

 

DSC03525

 

Marketplace Theater.

 

DSC03535.JPG

 

ORACLE @ marketplace

 

DSC03538.JPG

 

ubuntu @ marketplace

 

DSC03541.JPG

 

vmware @marketplace

 

DSC03530

 

Mirantis @marketplace

 

DSC03543.JPG

 

 

Categories: openstack, Uncategorized

Hard link vs soft link

The following figure explains hardlink, softlink and    their association with inode. The figure is adopted from [1]

 

 

 

hardlink-vs-softlink

 

 

Refs:

[1] http://stackoverflow.com/questions/185899/what-is-the-difference-between-a-symbolic-link-and-a-hard-link

Categories: Uncategorized, Unix

su vs sudo command

The command su (short for substitute user) is used to switch user in UNIX environment. When we issue su command  with/without a username, it switches to root or otherwise user mentioned.

 

On the other hand, sudo command run a single command with root privilege. By leveraging sudo, the system discourages to switch to root user when it is not absolutely necessary.

 

We can su with -c  to achieve the same as sudo command.

 

 

#Prosunjit Biswas#

 

Categories: Uncategorized, Unix

Load python script into python command line

execfile(“/path/to/file”)

# Assuming ‘myscript.py’ is in the current directory

execfile(“myscript.py”)

 

Refs:

[1] http://stackoverflow.com/questions/5280178/how-do-i-load-a-file-into-the-python-console

Categories: python, Uncategorized