Rajendra Dharmkar has Published 452 Articles

How to compare Python DateTime with Javascript DateTime?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 12-Jun-2020 11:25:08

628 Views

The datetime in Javascript and in Python have 2 major differences. First one being the meaning of the month argument.The month in Javascript is expected between 0-11 while in Python it is expected to be between 1-12. So the following tuple actually represents 2 different dates in Python and in ... Read More

How do I get time of a Python program's execution?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 12-Jun-2020 08:43:33

362 Views

To measure time time of a program's execution, either use time.clock() or time.time() functions. The python docs state that this function should be used for benchmarking purposes. Exampleimport time t0= time.clock() print("Hello") t1 = time.clock() - t0 print("Time elapsed: ", t1 - t0) # CPU seconds elapsed (floating point)OutputThis will give ... Read More

How to measure time with high-precision in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 12-Jun-2020 08:40:43

4K+ Views

To measure time with high precision, either use time.clock() or time.time() functions. The python docs state that this function should be used for benchmarking purposes.Exampleimport time t0= time.clock() print("Hello") t1 = time.clock() - t0 print("Time elapsed: ", t1 - t0) # CPU seconds elapsed (floating point)OutputThis will give the output ... Read More

How to prepare a Python date object to be inserted into MongoDB?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 12-Jun-2020 08:39:05

1K+ Views

You can use the pymongo library in Python to connect to a MongoDB database and use it to insert, update, delete, etc objects in Python. The library supports Python datetime objects out of the box and you dont need to do anything special to insert dates in Mongo using PyMongo.Examplefrom ... Read More

How to catch IOError Exception in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 12-Jun-2020 08:08:32

7K+ Views

IOError ExceptionIt is an error raised when an input/output operation fails, such as the print statement or the open() function when trying to open a file that does not exist. It is also raised for operating system-related errors.If the given code is written in a try block, it raises an ... Read More

How to catch ArithmeticError Exception in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 12-Jun-2020 08:07:42

853 Views

ArithmeticError Exception is the base class for all errors that occur for numeric calculations. It is the base class for those built-in exceptions like: OverflowError, ZeroDivisionError, FloatingPointErrorWe can catch the exception in given code as followsExampleimport sys try: 7/0 except ArithmeticError as e: print e print sys.exc_type print 'This is ... Read More

How to catch ImportError Exception in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 12-Jun-2020 07:42:03

3K+ Views

ImportError is raised when a module, or member of a module, cannot be imported. There are a two conditions where an ImportError might be raised.If a module does not exist.Exampleimport sys try:     from exception import myexception except Exception as e:     print e     print sys.exc_typeOutputNo ... Read More

How to use range in Python regular expression?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 12-Jun-2020 07:38:30

2K+ Views

Range in Regular ExpressionsRanges of characters can be indicated by giving two characters and separating them by a '-', for example [a-z] will match any lowercase ASCII letter, [0-5][0-9] will match all the two-digits numbers from 00 to 59.If - is escaped (e.g. [a\-z]) or if it’s placed as the ... Read More

Are Python Exceptions runtime errors?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 12-Jun-2020 07:24:00

738 Views

All python exceptions are not runtime errors, some are syntax errors as well.If you run the given code, you get the following output.File "C:/Users/TutorialsPoint1/~.py", line 4 else: ^ SyntaxError: invalid syntaxWe see that it is syntax error and not a runtime error.Errors or inaccuracies in a program are often called ... Read More

How to get timer ticks at a given time in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 12-Jun-2020 06:32:00

2K+ Views

To get the timer ticks at a given time, use the clock. The python docs state that this function should be used for benchmarking purposes. Exampleimport time t0= time.clock() print("Hello") t1 = time.clock() - t0 print("Time elapsed: ", t1 - t0) # CPU seconds elapsed (floating point)OutputThis will give the output ... Read More

Advertisements