Python standard library: First half (Python learning memo ⑧)

OS interface

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')

File wildcard

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

Command line arguments

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

Error output redirect and program termination

Use the sys module stdin, stdout, stderr

stderr is useful for displaying messages even when stdout is redirected

String pattern matching

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

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/

Internet access

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()

Date and time


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

Performance measurement

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.

quality management

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

Python standard library: First half (Python learning memo ⑧)
Python standard library: second half (Python learning memo ⑨)
Python class (Python learning memo ⑦)
Python & Machine Learning Study Memo ②: Introduction of Library
Python exception handling (Python learning memo ⑥)
Python control syntax, functions (Python learning memo ②)
Input / output with Python (Python learning memo ⑤)
Interval scheduling learning memo ~ by python ~
"Scraping & machine learning with Python" Learning memo
(python) Deep Learning Library Chainer Basics Basics
Python memo
python memo
Python memo
python memo
python learning
Python memo
Python memo
Python memo
Python & Machine Learning Study Memo: Environment Preparation
Python numbers, strings, list types (Python learning memo ①)
[Learning memo] Basics of class by python
Python data structure and operation (Python learning memo ③)
[Python] First data analysis / machine learning (Kaggle)
Python & Machine Learning Study Memo ③: Neural Network
Python & Machine Learning Study Memo ④: Machine Learning by Backpropagation
Python & Machine Learning Study Memo ⑥: Number Recognition
Python 3.6 email library
[Python] Memo dictionary
[Personal memo] julia --Using Python library with julia using PyCall
LPIC201 learning memo
First time python
[Python] Learning Note 1
Python ast library
First Python 3 ~ First comparison ~
python beginner memo (9.2-10)
Python learning notes
Django Learning Memo
Python & Machine Learning Study Memo ⑤: Classification of irises
python beginner memo (9.1)
python learning output
[Python] Standard input
Python learning site
★ Memo ★ Python Iroha
First time python
Python lexical / parsing library (2014.11 first survey, 2019.10 partial addition)
Python Deep Learning
Python 3 operator memo
Not surprisingly known !? Python standard library functions groupby
Python Library notes
Python learning (supplement)
First Python ~ Coding 2 ~
Try Juniper JUNOS PyEz (python library) Memo 1 ~ PyEz Overview ~
Deep learning × Python
[Memo] Machine learning
[My memo] python
Python3 metaclass memo
[Python] Basemap memo
progate Python learning memo (updated from time to time)
Python beginner memo (2)
python learning notes
[Python] Numpy memo