Good description for daily use:
One time I was in the ocean and a wave crashed on top of me and took my breath away as it pulled me deeper into the ocean. Just as I started to recover my breath, another wave dropped on top of me and extracted much of my remaining energy and pulled me even deeper into the ocean. Just as I started to recover, yet another wave crashed down on top of me. The more I would fight the waves and the ocean, the more energy I drained. I seriously wondered if I would die at that moment. I couldn’t breath, my body ached and I was terrified I was going to drown.
Being close to death helped me focus on the only thing that could save me, which was conserving my energy and using the waves not fighting them.
Install and Configure
Usually python2 is pre-installed, need to install python3
, can refer this blog
Install Python 3.7 on centos 7
This installation will not disturb original python2
pre-installed (it is the dependency of some other packages).
1 | curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py |
then you can use pip3
to install other packages, otherwise pip
is still use python2
.
Make python command execute python3, you can use alias.
Installing packages using pip and virtual environments
Chapter 1 Introduction
Install ipython
or ipython3
, powerful mixed interactive shell:
https://ipython.org/index.html#
1 | pip install ipython |
then run it as
1 | ipython |
Python variables use dynamic typing. In practice, this means reassigned to values of different types or classes
Built-in Functions
- range Use spaces instead of tabs to indent.
Functions
functions can be object, can put it in the list.
1 | functions = [double, triple] |
Wrapping Functions with Decorators, there are some other python command line build tools. click
package:
1 | pip install click |
1 | '''A example of using the click package to develop a command line tool''' |
call it:
1 | $ python simple_cli.py Sue |
lambda function, just like java
use lambda to create comparator for priority queue.
1 | sorted(items, key=lambda item: item[1]) |
RE package
The re
module uses \
to delineate special character for matching, for example \.
, \n
, etc. To avoid confusion with regular string escape sequences, raw
strings are recommended in defining regular expressions. Raw strings are prepended with a r
before the first quotation mark.
Similar as grep
:
1 | import re |
Lazy Evaluation
This will have footprint in memory, generate values as needed, will not take much memory.
Create generator, using ()
instead of []
(list comprehension)
1 | gen_o_nums = (x for x in range(100)) |
More IPYTHON features
Using IPython To Run Unix Shell Commands, add !
before command, but sometime does not need (default setting):
1 | In [2]: !ls -l |
can assign to a variable:
1 | In [6]: res = !df -h | head -n 7 |
magic commands:
1 | ## enter bash |
Make IPython import shell alias
1 | #!/usr/bin/env python |
Drop this code in ~/.ipython/profile_default/ipython_config.py
, just like .bashrc
and .vimrc
, the configuration file for ipython.
How to import shell functions in .bashrc
? Or wirte python function instead.
Chapter 2 Automating Text and Files
In the DevOps world, you are continually parsing, searching, and changing the text in files, whether it’s searching application logs or propagating configuration files.
read regular file:
1 | ## don't need to close explicitly |
or use
1 | ## this will parse lines by `\n` |
Different operating systems use different escaped characters to represent line-endings. Unix systems use \n
and Windows systems use \r\n
. Python converts these to \n
when you open a file as text. If you are opening a binary file, such as a jpeg image, you are likely to corrupt the data by this conversion if you open it as text. You can, however, read binary files by appending a b
to mode:
1 | file_path = 'bookofdreamsghos00lang.pdf' |
write file:
1 | content='''export a=123 |
The open
function creates the file if it does not already exist and overwrites if it does. if want to append, use a
mode instead of w
. For binary file use bw
or ba
.
JSON
1 | import json |
actually you can use data pretty printer:
1 | import pprint |
YAML
The most commonly used library for parsing YAML files in Python is PyYAML
. It is not in the Python Standard Library, but you can install it using pip:
1 | pip install pyyaml |
XML
Historically, many web systems used XML to transport data. One use is for RSS
feeds. RSS (Really Simple Syndication)
feeds are used to track and notify users of updates to websites. These feeds have been used to track the publication of articles from various sources. RSS uses XML formatted pages. Python offers the xml library for dealing with XML documents. It maps the XML documents hierarchical structure to a tree-like data structure.
1 | import xml.etree.ElementTree as ET |
CSV
data stored as comma-separated values.
1 | In [16]: import csv |
pandas
packages is mainstay to do data science work.
Pandas has many more methods for analyzing and manipulating table like data, and there are many books on its use. You should be aware that it is available if you need to do data analysis.
Search Text
One widely used format is the Common Log Format (CLF)
. A variety of log analysis tools can understand this format:
1 | <IP Address> <Client Id> <User Id> <Time> <Request> <Status> <Size> |
Just give some examples:
1 | line = '127.0.0.1 - swills [13/Nov/2019:14:43:30 -0800] "GET /assets/234 HTTP/1.0" 200 2326' |
Note: Python automatically allocates and frees memory.The Python garbage collector can be controlled using the
gc
package, though this is rarely needed.
For large files. If the files contain data that can be processed one line at a time, the task is easy with Python. You can read one line at a time, process the line, and then move to the next. The lines are removed from memory automatically by Python’s garbage collector, freeing up memory.
1 | In [23]: with open('big-data.txt', 'r') as source_file: |
Chapter 3 Command Line
Python offers tools for interacting with systems and shells. You should become familiar with the sys
, os
, and subprocess
modules, as are all essential tools.
1 | import sys |
The most common usage of the os module is to get settings from environment variables.
1 | import os |
With subprocess
you can run your favorite shell command or other command line software and collect its output from within Python. For the majority of use-cases, you should use the subprocess.run
function to spawn processes
1 | import subprocess |
Creating Command Line Tools
Invoke python script usually by:
1 | python xx.py |
or you can eliminate python by adding #!/usr/bin/env python
(or python3
) at first line of the script, then chmod
the script to executable:
1 | ./xx.py |
The simplest and most basic way to process arguments from the command line is to use the argv attribute of the sys module
:
1 | #!/usr/bin/env python |
This is not enough, we need argument parser! Luckily there are modules and packages designed for the creation of command line tools. These packages provide frameworks to design the user interface for your module when running in a shell. Three popular solutions argparse
, click
, and fire
. All three include ways to design required arguments, optional flags, and means to display help documentation. The first, argparse, is part of the Python standard library, and the other two are third-party packages that need to be installed separately (using pip).
argparse
这个有专门的tutorial,大概看了一下,it does take much work on your part but you get lots of control.
Automatically generates help and usage messages and issues errors when users give the program invalid arguments.
1 | #!/usr/bin/env python |
You can also define sub-commands, like git stash ...
.
click
It uses Python Function Decorators
to bind the command line interface directly with your functions.
Python decorators are a special syntax for functions take other functions as arguments. Python functions are objects, so any function can take a function as an argument. The decorator syntax provides a clean and easy way to do this.
1 | #!/usr/bin/env python |
Please refer to click documents.
fire
for example:
1 | #!/usr/bin/env python |
An exciting feature of fire is the ability to enter an interactive mode easily. By using the --interactive
flag, fire opens an IPython shell with the object and functions of your script available:
1 | ./fire_example.py <command> <args> -- --interactive |
Overall, We recommend click
for most use cases. It balances ease and control. In the case of complex interfaces where you want to separate the UI code from business logic, argparse
is the way to go. Moreover, if you need to access code that does not have a command line interface quickly, fire
is right for you.
Implementing plugins
Once you’ve implemented your applications command line user interface you might want to consider a plugin system. Plugins are pieces of code supplied by the user of your program to extend functionality.
A key part of any plugin system is plugin discover. Your program needs to know what plugins are available to load and run. Create a file named add_plugins.py
1 | #!/usr/bin/env python |
then you can wirte modules for example: module1.py
and put it in sys.path
directory. If you run ./add_plugins.py find_and_run_plugins module
, then it will search, load and run the module1.py
module.
Turbocharging Python with Command Line Tools
Here are the raw ingredients that will be used to make several solutions:
- Click Framework
- Python CUDA Framework
- Numba Framework
- Scikit-learn Machine Learning Framework
These are tools to speed up the performance.
Chapter 4 Useful Linux Utilities
This chapter will go through some common patterns in the shell and will include some useful Python commands that should enhance the ability to interact with a machine.
As a seasoned performance engineer once said, it depends on what is measured and how.
disk utility
If we had to work in an isolated environment with a server that doesn’t have access to the internet or that we don’t control and therefore can’t install packages, we would have to say that the dd
tool can provide help.
This will get thoughput of the new device, for example, the throughput is 1.4GB/s
1 | dd if=/dev/zero of=<new device> count=10 bs=100M |
how to get IOPS, update every 1 second:
1 | iostat -d <device> 1 |
Other common test tool is fio
, you may need to install this package. It can help clarify the performance behavior of a device in a read-heavy or write-heavy environment (and even adjust the percentages of reads vs. writes).
network utility
- ssh tunneling (ssh port forwarding)
For example, the server hello.com can only access by ssh with port 3345 and not exposed,let’s forward hello.com:3345 to a local port in my machine: https://www.youtube.com/watch?v=AtuAdk4MwWw
1 | ssh -f -L 12333:hello.com:3345 root@hello.com -N |
-f
means run in bachground
-L
is forwarding rule
-N
means don’t get into remote shell
root@hello.com
is the username and address of that server
This tech can also be used to bypass firewall for some ports. Then we can access localhost:12333 to access the server. 疑问:如果port已经被firewall block了,ssh是怎么连接上的呢?