Day 17 Frequently-used modules
一 hashlib module
Python hashlib hashing function takes variable length of bytes and converts it into a fixed length sequence. This is a one way function. That means, you hash a message, you get a fixed length sequence. But you cannot get the original message from those fixed length sequence.
This hash function accepts sequence of bytes and returns 128 bit hash value, usually used to check data integrity but has security issues. Functions associated : encode() : Converts the string into bytes to be acceptable by hash function. digest() : Returns the encoded data in byte format.
hash coding
A hash function is any function that can be used to map data of arbitrary size to fixed-size values. The values returned by a hash function are called hash values, hash codes, digests, or simply hashes.
二 subprocess module
The subprocess module present in Python(both 2.x and 3.x) is used to run new applications or programs through Python code by creating new processes. It also helps to obtain the input/output/error pipes as well as the exit codes of various commands.
https://www.runoob.com/w3cnote/python3-subprocess.html
https://queirozf.com/entries/python-3-subprocess-examples
三 os&sys module
os module
The OS module in python provides functions for interacting with the operating system. OS, comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. The os and os.path modules include many functions to interact with the file system.
https://www.geeksforgeeks.org/os-module-python-examples/
sys module
The python sys module provides functions and variables which are used to manipulate different parts of the Python Runtime Environment. It lets us access system-specific parameters and functions.
sys.path
This function shows the PYTHONPATH set in the current system. It is an environment variable that is a search path for all the python modules.
sys.environ
https://www.nylas.com/blog/making-use-of-environment-variables-in-python/
四 configparser module
Python ConfigParser
ConfigParser is a Python class which implements a basic configuration language for Python programs. It provides a structure similar to Microsoft Windows INI files. ConfigParser allows to write Python programs which can be customized by end users easily.
The configuration file consists of sections followed by key/value pairs of options. The section names are delimited with [] characters. The pairs are separated either with : or =. Comments start either with # or with ;.
In the first example, we read configuration data from a file.
db.ini
[mysql]
host = localhost
user = user7
passwd = s$cret
db = ydb
[postgresql]
host = localhost
user = user8
passwd = mypwd$7
db = testdb
We have two sections of configuration data.
reading_from_file.py
#!/usr/bin/env python3
import configparser
config = configparser.ConfigParser()
config.read('db.ini')
host = config['mysql']['host']
user = config['mysql']['user']
passwd = config['mysql']['passwd']
db = config['mysql']['db']
print('MySQL configuration:')
print(f'Host: {host}')
print(f'User: {user}')
print(f'Password: {passwd}')
print(f'Database: {db}')
host2 = config['postgresql']['host']
user2 = config['postgresql']['user']
passwd2 = config['postgresql']['passwd']
db2 = config['postgresql']['db']
print('PostgreSQL configuration:')
print(f'Host: {host2}')
print(f'User: {user2}')
print(f'Password: {passwd2}')
print(f'Database: {db2}')