Functions that interact with the operating system
import os
os.getcwd() #Get current directory
os.system('mkdir today') #Command execution in the system shell
The shutil
module is useful for managing files and directories
import shutil
shutil.copyfile('data.db', 'archive.db')
shuti.move('/build/executables', 'installdir')
The glob module wildcards the directory and returns a list of files
import glob
glob.glob('*.py')
#.A list of files ending in py is returned
Arguments passed when executing the command of python are stored in sys.argv
import sys
print(sys.argv)
#Get argument list
#The 0th element is the file name
Other modules that handle arguments are as follows
Use the sys
module stdin, stdout, stderr
stderr
is useful for displaying messages even when stdout
is redirected
Regular expressions etc. can be implemented using the re
module
import re
print(re.findall(r'\bf[a-z]*', 'whitch foot or hand fell fastest'))
# ['foot', 'fell', 'fastest']
print(re.sub(r'(\b[a-z]+) \1', r'\1', 'cat in the the hat'))
# cat in the hat
math module
import math
math.cos(math.pi / 4)
# 0.7071067811865476
math.log(1024, 2)
# 10.0
random module
import random
random.choice(['apple', 'pear', 'banana'])
#Random choice from the list
# 'apple'
random.sample(range(100), 10)
# range(100)Extract 10 from(No duplication)
# [48, 5, 42, 15, 23, 78, 55, 72, 39, 1]
random.random()
#Random floating point number
# 0.2785335302723758
random.randrange(6)
# range(6)Randomly selected integer from
# 0
statistics module-find statistics
import statistics
data = [2.75, 1.75, 1.25, 0.25, 0.5, 1.25, 3.5]
statistics.mean(data) #average
# 1.6071428571428572
statistics.median(data) #Median
# 1.25
statistics.variance(data) #Distributed
# 1.3720238095238095
See SciPy project for other numerical modules
https://www.scipy.org/
from urllib.request import urlopen
with urlopen('http://tycho.usno.navy.mil/cgi-bin/timer.pl') as response:
for line in response:
line = line.decode('utf-8') #Decode binary data into text
if 'EST' in line or 'EDT' in line: #Find Eastern Standard Time
print(line)
import smtplib
server = smtplib.SMTP('localhost')
server.sendmail('[email protected]', '[email protected]',
"""To: [email protected]
From: [email protected]
Beware the Ideas of March.
"""
)
server.quit()
from datetime import date
now = date.today()
print(now)
# 2019-12-09
print(now.strftime("%m-%d-%y. %d %b %Y is a %A on the %d day of %B."))
# 12-09-19. 09 Dec 2019 is a Monday on the 09 day of December.
birthday = date(1964, 7, 31)
age = now - birthday
print(age.days)
# 20219
Let's measure the performance difference in exchanging variables
from timeit import Timer
time1 = Timer('t=a; a=b; b=t', 'a=1; b=2').timeit()
time2 = Timer('a,b = b,a', 'a=1; b=2').timeit()
print(time1)
# 0.020502762
print(time2)
# 0.018866841999999995
The profile
and pstats
modules are also suitable for measuring large code blocks.
Write the test when you write the function and run the test during development
The doctest
module scans the module and validates the tests embedded in the docstring.
The test lists common calls and their results in docstring
def average(values):
"""Calculate the arithmetic mean from a list of numbers
>>> print(average([20, 30, 70]))
40.0
"""
return sum(values) / len(values)
import doctest
doctest.testmod()
#Automatically validate embedded tests
The ʻunittest` module can have a more comprehensive set of tests in a separate file
import unittest
from doctest_sample import average
class TestStatisticalFunctions(unittest.TestCase):
def test_average(self):
self.assertEqual(average([20, 30, 70]), 40.0)
self.assertEqual(round(average([1, 5, 7]), 1), 4.3)
with self.assertRaises(ZeroDivisionError):
average([])
with self.assertRaises(TypeError):
average(20, 30, 70)
unittest.main()
Recommended Posts