The time module


In [1]:
import time

In [2]:
time.time()


Out[2]:
1524142398.178439

In [3]:
time.time()


Out[3]:
1524142398.5505207

In [4]:
for i in range(3):
    print('Tick')
    time.sleep(1)
    print('Tock')
    time.sleep(1)


Tick
Tock
Tick
Tock
Tick
Tock

In [5]:
time.sleep(5)

Rounding Numbers


In [6]:
now = time.time()

In [7]:
now


Out[7]:
1524142902.661013

In [8]:
round(now, 2)


Out[8]:
1524142902.66

In [9]:
round(now, 4)


Out[9]:
1524142902.661

In [10]:
round(now)


Out[10]:
1524142903

The datetime module


In [2]:
import datetime

In [12]:
datetime.datetime.now()


Out[12]:
datetime.datetime(2018, 4, 19, 10, 55, 35, 729952)

In [13]:
dt = datetime.datetime.now()

In [14]:
dt.year, dt.month, dt.day


Out[14]:
(2018, 4, 19)

In [15]:
dt.hour, dt.minute, dt.second


Out[15]:
(10, 55, 59)

In [16]:
datetime.datetime.fromtimestamp(1000000)


Out[16]:
datetime.datetime(1970, 1, 12, 10, 46, 40)

In [17]:
datetime.datetime.fromtimestamp(time.time())


Out[17]:
datetime.datetime(2018, 4, 19, 11, 1, 19, 564442)

In [18]:
halloween2015 = datetime.datetime(2015, 10, 31, 0 , 0, 0)

In [19]:
newyears2016 = datetime.datetime(2016, 1, 1, 0, 0, 0)

In [20]:
oct31_2015 = datetime.datetime(2015, 10, 31, 0, 0, 0)

In [21]:
halloween2015 == oct31_2015


Out[21]:
True

In [22]:
halloween2015 > newyears2016


Out[22]:
False

In [23]:
newyears2016 > halloween2015


Out[23]:
True

In [24]:
newyears2016 != oct31_2015


Out[24]:
True

The timedelta Data Type


In [25]:
delta = datetime.timedelta(days=11, hours=10, minutes=9, seconds=8)

In [26]:
delta.days, delta.seconds, delta.microseconds


Out[26]:
(11, 36548, 0)

In [27]:
delta.total_seconds()


Out[27]:
986948.0

In [28]:
11*24*3600 + 10*3600 + 9*60 + 8


Out[28]:
986948

In [29]:
str(delta)


Out[29]:
'11 days, 10:09:08'

In [30]:
dt = datetime.datetime.now()

In [31]:
dt


Out[31]:
datetime.datetime(2018, 4, 19, 12, 19, 27, 425635)

In [33]:
thousandDays = datetime.timedelta(days=1000)

In [34]:
dt + thousandDays


Out[34]:
datetime.datetime(2021, 1, 13, 12, 19, 27, 425635)

In [35]:
oct21st = datetime.datetime(2015, 10, 21, 16, 29, 0)

In [36]:
aboutThirtyYears = datetime.timedelta(days=365*30)

In [37]:
oct21st


Out[37]:
datetime.datetime(2015, 10, 21, 16, 29)

In [38]:
oct21st - aboutThirtyYears


Out[38]:
datetime.datetime(1985, 10, 28, 16, 29)

In [39]:
oct21st - (2 * aboutThirtyYears)


Out[39]:
datetime.datetime(1955, 11, 5, 16, 29)

Pausing Until a Specific Date


In [45]:
halloween2016 = datetime.datetime(2016, 10, 31, 0, 0, 0)

In [46]:
while datetime.datetime.now() < halloween2016:
    time.sleep(1)

Converting datetime Objects into Strings


In [40]:
oct21st = datetime.datetime(2015, 10, 21, 16, 29, 0)

In [42]:
oct21st.strftime('%Y/%m/%d %H:%M:%S')


Out[42]:
'2015/10/21 16:29:00'

In [43]:
oct21st.strftime('%I:%M %p')


Out[43]:
'04:29 PM'

In [44]:
oct21st.strftime('%B of %y')


Out[44]:
'October of 15'

Converting Strings into datetime Objects


In [47]:
datetime.datetime.strptime('October 21, 2015', '%B %d, %Y')


Out[47]:
datetime.datetime(2015, 10, 21, 0, 0)

In [48]:
datetime.datetime.strptime('2015/10/21 16:29:00', '%Y/%m/%d %H:%M:%S')


Out[48]:
datetime.datetime(2015, 10, 21, 16, 29)

In [49]:
datetime.datetime.strptime("October of '15", "%B of '%y")


Out[49]:
datetime.datetime(2015, 10, 1, 0, 0)

In [50]:
datetime.datetime.strptime("November of '63", "%B of '%y")


Out[50]:
datetime.datetime(2063, 11, 1, 0, 0)

Multithreading

Passing Arguments to the Thread’s Target Function


In [51]:
print("Cats", "Dogs", "Frogs", sep=" & ")


Cats & Dogs & Frogs

In [3]:
import threading

In [55]:
threadObj = threading.Thread(
    target=print, args=["Cats", "Dogs", "Frogs"], kwargs={"sep":" & "})

In [56]:
threadObj.start()


Cats & Dogs & Frogs

In [4]:
import requests

Launching other Programs from Python


In [5]:
import subprocess

In [6]:
subprocess.Popen("/usr/bin/gnome-calculator")


Out[6]:
<subprocess.Popen at 0x7f597c27ad30>

In [7]:
calcProc = subprocess.Popen("/usr/bin/gnome-calculator")

In [9]:
calcProc.poll() == None


Out[9]:
True

In [10]:
calcProc.wait()


Out[10]:
0

In [11]:
calcProc.poll()


Out[11]:
0

Passing Command Line Arguments to Popen()


In [14]:
subprocess.Popen(["/usr/bin/gedit", "/home/dario/temp.txt"])


Out[14]:
<subprocess.Popen at 0x7f596de7b400>

Running Other Python Scripts


In [16]:
subprocess.Popen(["/usr/bin/python3", "hello.py"])


Out[16]:
<subprocess.Popen at 0x7f596de7bb70>

Opening Files with Default Applications


In [17]:
fileObj = open("hello.txt", "w")

In [18]:
fileObj.write("Hello world!!!")


Out[18]:
14

In [19]:
fileObj.close()

In [23]:
subprocess.Popen(["gedit", "hello.txt"], shell=True)


Out[23]:
<subprocess.Popen at 0x7f596dce0710>

In [ ]: