Rajendra Dharmkar has Published 452 Articles

How to get the Python date object for last Wednesday?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 19-Feb-2020 09:33:33

681 Views

You can get the Python date object for last wednesday using some Python date math. Whatever the day of the week it is today, subtracting 2 from it and taking the modulus of the result by 7 will give us how back was wedenesday. examplefrom datetime import date from datetime import ... Read More

How to convert a datetime string to millisecond UNIX time stamp?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 19-Feb-2020 08:08:58

666 Views

You can get the current time in milliseconds in Python using the time module. You can get the time in seconds using time.time function(as a floating point value). To convert it to milliseconds, you need to multiply it with 1000 and round it off.  exampleimport time milliseconds = int(round(time.time() * 1000)) ... Read More

How to compare calendar.timegm() vs. time.mktime() in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 19-Feb-2020 07:55:26

637 Views

If not explicitly stated, the date time functions/modules in Python assume everything in Local time zone.time.mktime() assumes that the passed tuple is in local time, calendar.timegm() assumes it's in GMT/UTC.Depending on the interpretation the tuple represents a different time, so the functions return different values (seconds since the epoch are ... Read More

How to get the timing Execution Speed of Python Code?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 19-Feb-2020 07:44:46

2K+ Views

To measure 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 the ... Read More

How to find if 24 hrs have passed between datetimes in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 19-Feb-2020 07:44:15

2K+ Views

To find out if 24 hrs have passed between datetimes in Python, you will need to do some date math in Python. So if you have 2 datetime objects, you'll have to subtract them and then take the timedelta object you get as a result and use if for comparision. ... Read More

How to compare Python string formatting: % with .format?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 19-Feb-2020 07:33:31

185 Views

% can either take a variable or a tuple. So you'd have to be very explicit about what you want it to do. For example, if you try formatting such that −Examplemy_tuple = (1, 2, 3) "My tuple: %s" % my_tuple You'd expect it to give the output: My tuple: ... Read More

How to suspend the calling thread for few seconds in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 19-Feb-2020 07:08:19

325 Views

You can suspend the calling thread for a given time in Python using the sleep method from the time module. It accepts the number of seconds for which you want to put the calling thread on suspension for. Exampleimport time while(True):   print("Prints every 10 seconds")   time.sleep(10)OutputPrints every 10 seconds ... Read More

How regular expression grouping works in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 19-Feb-2020 06:54:55

548 Views

GroupingWe group part of a regular expression by surrounding it with parentheses. This is how we apply operators to the complete group instead of a single character.Capturing GroupsParentheses not only group sub-expressions but they also create backreferences. The part of the string matched by the grouped part of the regular ... Read More

How regular expression back references works in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 19-Feb-2020 06:12:06

580 Views

GroupingWe group part of a regular expression by enclosing it in a pair of parentheses. This way we apply operators to the group instead of a single character.Capturing Groups and BackreferencesParentheses not only group sub-expressions but they also create backreferences. The part of the string matched by the grouped part ... Read More

How to get current date and time in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 19-Feb-2020 05:41:03

1K+ Views

You can get the current date and time using multiple ways. The easiest way is to use the datetime module. It has a function, now, that gives the current date and time. exampleimport datetime now = datetime.datetime.now() print("Current date and time: ") print(str(now))OutputThis will give the output −2017-12-29 11:24:48.042720You can also ... Read More

Advertisements