Pages

Saturday, January 3, 2015

Python: current time in milliseconds









import time
ms = int(round(time.time() * 1000))
print(ms)




Friday, January 2, 2015

Python: ignore all exception and proceed








try:
   ... ...
except:
   pass



Thursday, January 1, 2015

Python: overriding static method








class A:
    @staticmethod
    def m():
        print "A"

class B(A):
    @staticmethod
    def m():
        print "B"

a = A()
a.m() # A
b = B()
b.m() # B



Python: call constructor of the super class








  • Option 1:
    class A(object):
     def __init__(self):
       print "world"
    
    class B(A):
     def __init__(self):
       print "hello"
       super(B, self).__init__()
    
  • Option 2:
    class A: 
     def __init__(self): 
       print "world" 
    
    class B(A): 
     def __init__(self): 
       print "hello" 
       A.__init__(self)
    



Python: string equals ignore case








def equals_ignore_case(s1, s2):
    if s1 is None and s2 is None:
        return True
    if s1 is None and s2 is not None:
        return False
    if s1 is not None and s2 is None:
        return False
    return s1.lower() == s2.lower()