Leaves fall down, the wind sways those years old

This article is used as a record, some of the tricks encountered in the process of using python, some of the code is written by myself, some are Python built-in functions, and some are taken from the Internet. In this record, it is easy to find only for backup and forgetting.
This article will continue to be updated, and the content is limited to recording some code or modules that are commonly used but never remembered.

Console operation

The console does not flash back

1
os.system('pause')

Get console size

1
rows, columns = os.popen('stty size', 'r').read().split()

Input and output control

Solve the problem of input prompt Chinese garbled

1
Raw_input(unicode('Please enter text', 'utf-8').encode('gbk'))

Formatted output

1
print a.prettify()

Accept multiple lines of input

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
text=""
while 1:
data=raw_input(">>")
if data.strip()=="stop":
break
text+="%s\n" % data
print text
---------------------------
>>1
>>2
>>3
>>stop
1
2
3

Peer output

1
2
Print '%s' % a,
Print '%s \r' % a

Standard input and output

1
2
Sys.stdout.write("input") standard input
Sys.stdout.flush() flush buffer

The function of print is similar to sys.stdout.write, because the default for print in 2.x is to assign output to standard output (sys.stdout).

Color Control

Console color control (for windows)

1
2
3
4
WConio.textcolor(WConio.YELLOW)
print "yellow"
WConio.textcolor(WConio.BLUE)
print "blue"

Output color control (full platform)

1
2
3
4
5
6
7
red = '\033[1;31m'
green = '\033[1;32m'
yellow = '\033[1;33m'
white = '\033[1;37m'
print red+"color is red"+reset
print green+"color is green"+reset

Progress bar control

Option One

1
2
3
4
5
6
7
8
from __future__ import division
import sys,time
j = '#'
for i in range(1,61):
j += '#'
sys.stdout.write(str(int((i/60)*100))+'% ||'+j+'->'+"\r")
sys.stdout.flush()
time.sleep(0.1)

Option II

1
2
3
4
5
6
import sys
import time
for i in range(1,61):
sys.stdout.write('#'+'->'+"\b\b")
sys.stdout.flush()
time.sleep(0.5)

third solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from progressbar import *
import time
import os
Rows, columns = os.popen('stty size', 'r').read().split() #Get console size
console_width=int(columns)
total = 10
progress = ProgressBar()
def test():
'''
Progress bar function, recording progress
'''
for i in progress(range(total)):
test2 ()
def test2():
'''
Execution function, output result
'''
content="nMask'Blog is http://thief.one"
sys.stdout.write("\r"+content+" "*(console_width-len(content)))
time.sleep(1)
sys.stdout.flush()
test()

More advanced usage can use the progressbar module.

System operation

system message

Get the python installation path

1
2
from distutils.sysconfig import get_python_lib
print get_python_lib

Get the current python version

1
2
sys.version_info
sys.version

Get current time

1
2
c=time.ctime()
time.strftime( ISOTIMEFORMAT, time.localtime() )

View system environment variables

1
they sent ["PATH"]

Get system disk

1
os.popen("wmic VOLUME GET Name")

Get the current path (including the current py file name)

1
os.path.realpath(__file__)

The line terminator used by the current platform

1
os.linesep

Get the terminal size

1
2
3
rows, columns = os.popen('stty size', 'r').read().split()
#python3 exists later can use os
os.get_termial_size()

exit the program

  • return: returns the value of the function and exits the function.
  • exit(): Exit directly.
  • sys.exit(): Raises a SystemExit exception. If there is no catch error, the python program exits directly; after catching the exception, you can do some extra cleanup.
  • sys.exit(0): Normal exit, others (1-127) are abnormal, and can throw exceptions for capture. (usually used to exit the program in the main thread)
  • os._exit(0): Exit the python program directly, and the subsequent code will not be executed. (usually used to exit the program in the thread)

Network operation

Domain name resolution to ip

1
ip= socket.getaddrinfo(domain,'http')[0][4][0]

Get server version information

1
2
3
sUrl = 'http://www.163.com'
sock = urllib2.urlopen(sUrl)
sock.headers.values()

File Operations

Open function, use wb, rb instead of w, r

1
2
with open("test.txt","wr") as w:
w.write("test")

This way of writing is compatible with python2/3.
Output all file names in a directory

1
2
3
4
5
6
7
8
def search(paths):
If os.path.isdir(paths): #if is the directory
Files=os.listdir(paths) #List all files in the directory
for i in files:
i=os.path.join(paths,i) #Build file path
Search(i) #recursive
Elif os.path.isfile(paths): #if it is a file
Print paths #output filename

File lookup

1
2
3
4
5
6
import glob
Print glob.glob(r"E:/*.txt") #returns a list
Finding files uses only three match characters: "*", "?", "[]"
"*" matches 0 or more characters;
"?" matches a single character;
"[] "matches characters within the specified range, such as: [0-9] matches numbers.

Find the path to the folder with the specified name

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
def search(paths,file_name,tag,lists):
If os.path.isdir(paths): #if is the directory
If file_name==tag: #if the directory name is tag
Lists.append(paths) #Add this path to the list
Else: #if the directory name is not a tag
try:
Files_list=os.listdir(paths) #List all files in the directory
for file_name in files_list:
Path_new=os.path.join(paths,file_name) #Build file path
Except: # will get an error when encountering a special directory name
pass
Elif os.path.isfile(paths): #if it is a file
pass
return lists

Data manipulation

Judge data type

1
isinstance("123",(int,long,float,complex)

String (string)

String derivation

1
2
3
4
a="True"
b=a if a=="True" else "False"
>>>print b
True

Format method splicing strings and variables

1
2
3
4
5
6
7
a="{test} abc {test2}".format(test="123",test2="456")
>>>>print a
123 abc 456
or:
a="{},{}".format(1,2)
>>>>>print a
1,2

Remove the number after the decimal point

1
2
a=1.21311
b=Int(math.floor(a))

String inversion

1
2
>>> a = "codementor"
>>> a[::-1]

The first letter of the string is capitalized

1
2
3
info = 'ssfef'
print info.capitalize()
print info.title()

Returns a string centered and padded with a space to a new string of length width.

1
"center string".center(width) #width is set to the console width, which controls the output string to be centered.

List all letters

1
2
3
Print string.ascii_uppercase all uppercase letters
Print string. ascii_lowercase all lowercase letters
Print string.ascii_letters All letters (including capitalization)

list (list)

List deduplication

1
2
ids = [1,4,3,3,4,2,3,4,5,6,1]
ids = list(set(ids))

The judgment list is empty

1
2
a=[]
if not a:

List operation

1
2
3
4
a=[1,2,3]
b=[3,4,5]
Set(a)&set(b) and
Set(a)|set(b) or

Add single list elements

1
2
3
a = ["Code", "mentor", "Python", "Developer"]
>>> print " ".join(a)
Code mentor Python Developer

Add multiple list elements separately

1
2
3
4
5
6
7
8
list1 = ['a', 'b', 'c', 'd']
list2 = ['p', 'q', 'r', 's']
>>> for x, y in zip(list1,list2):
print x, y
you
bq
cr
ds

Convert nested lists to a single list

1
2
3
4
a = [[1, 2], [3, 4], [5, 6]]
>>> import itertools
>>> list(itertools.chain.from_iterable(a))
[1, 2, 3, 4, 5, 6]

Adding elements within the list

1
2
a=[1,2,3] (number)
sum(a)

Generate a list of strings for a-z

1
map(chr,range(97,123))

List copy

1
2
3
a=[1,2,3]
b=a
When operating on b, it will affect the content of a, because sharing a memory pointer, b = a [:] This is a separate copy.

List Derivation

If+else with list analysis

1
[i if i >5 else -i for i in range(10)]

Multi-level nested list

1
2
3
4
a=[[1,2],[3,4]]
b=[for j in i for i in a]
print b
[1,2,3,4]

Generating a generator and calling the next method can reduce memory overhead.

1
a=(i else i+1 for i in b if i==1)

Dictionary Derivation

Replace key and value locations

1
2
dict={"a":1,"b":2}
b={value:key for key value in dict.items()}

Dictionary operation (dict)

Filter out duplicate keys

1
2
3
4
5
6
list1=self.dict_ip.items()
ddict=defaultdict(list)
for k,v in list1:
ddict[v].append(k)
list2=[(i,ddict[i]) for i in ddict if len(ddict[i])>1]
dict_ns=dict(list2)

Dictionary sorting (py2)

1
2
file_dict={"a":1,"b":2,"c":3}
File_dict_new=sorted(file_dict.iteritems(), key=operator.itemgetter(1), reverse=True) ## dictionary sorting, reverse=True from high to low, itemgetter(1) means sorting by value, 0 means pressing key Sort.

Dictionary value judgment

1
2
3
b={"a":1}
a=b.get("a","") #If there is no a, return ""
c=a if a else 0 # If a is present, return a, otherwise return 0

Module operation

When importing a module, set only the properties or methods that are allowed to be imported.

1
2
3
4
5
6
7
8
9
fb.py:
-----------------------
__all__=["a","b"]
a="123"
c="2345"
def b():
-----------------------
from fb import *
You can import variables defined in __all__, a and b() can be imported, c can't. If you don't define __all__ then all can be imported.

Import the package under the parent directory

1
2
sys.path.append("..")
from spider.spider_ import spider_

Import modules under external directory

1
Need to create a __init__.py file in the target directory, the content is casual.

Add module properties

1
2
3
4
Sometimes in the source code, we need to write our own name and version introduction information, which can be defined by __name__.
a.py:
#! -*- coding:utf-8 -*-
__author__="nMask"

Then when we import a module, we can output dir(a) to see

1
2
3
4
5
>>> import p
>>> print dir (p)
['__author__', '__builtins__', '__doc__', '__file__', '__name__', '__package__']
>>> print p.__author__
nmask

Dynamically load all modules in a directory

1
2
3
4
5
6
7
8
9
10
11
table of Contents:
---test
----a.py
----b.py
---c.py
C.py imports all the modules under test:
for path in ["test"]:
for i in list(set([os.path.splitext(i)[0] for i in os.listdir("./"+path)])):
If i!="__init__" and i!=".DS_Store": ##Exclude unnecessary files
import_string = "import path+"."+i+"
Exec import_string #Execute the contents of the string

Function operation

eval/exec

1
2
def test(content):
print content

Output: abc
Description: The exec function has no return value.

1
2
def test(content):
return content

Output: abc
Description: The eval function has a return value

Decorator function

Output current time decorator

1
2
3
4
5
def current_time(aclass):
def wrapper():
print "[Info]NowTimeis:",time.ctime()
return a class()
return wrapper

itertoolsiterator

1
2
3
p=product(["a","b","c","d"],repeat=2)
----
[("a","a"),("b","b")......]

reduce function

The result of this function execution is passed to the next time.

1
2
3
4
def test(a,b):
return a+b
reduce(test,range(10))
Result: from 0+1+2...+9

enumerate function

Enter list elements and serial numbers

1
2
3
n=["a","b","c"]
for i,m in enumerate(n):
print(i,m)

Function timeout setting

Use signal to set the timeout for a function to execute

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import time
import signal
def test(i):
Time.sleep(0.999)# simulate timeout situation
print "%d within time"%(i)
return i
def fuc_time(time_out):
# This is the function timeout control, replacing the following test function as a function that may have an unknown error deadlock.
def handler(signum, frame):
raise AssertionError
try:
signal.signal(signal.SIGALRM, handler)
Signal.alarm(time_out)#time_out is the timeout period
Temp = test(1) #function setting part, if it does not time out, it will return data normally.
return temp
except AssertionError:
Print "%d timeout"%(i)# timeout error
if __name__ == '__main__':
for i in range(1,10):
fuc_time(1)

Function error retry

Using the retrying module to implement the function error retry function

1
2
3
4
5
6
7
8
import random
from retrying import retry
@retry
def have_a_try():
if random.randint(0, 10) != 5:
raise Exception('It's not 5!')
print 'It's 5!'

If we run the have_a_try function, it will not end until random.randint returns 5, otherwise it will be re-executed. Please search for more usage of this module.

Program operation

Ctrl+C to exit the program

Use signal to implement ctrl+c to exit the program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import signal
import sys
import time
def handler(signal_num,frame):
print "\nYou Pressed Ctrl-C."
sys.exit(signal_num)
signal.signal(signal.SIGINT, handler)
# Normally you can start your own program.
# Here for the demonstration, we make a loop that won't kill the machine.
while 1:
time.sleep(10)
# When you press Ctrl-C, you should output a paragraph and exit.

Program self-restart

Use os.execl method to realize program self-restart

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import time
import sys
import os
def restart_program():
python = sys.executable
print "info:", os.execl (python, python, * sys.argv)
The #os.execl method will replace its own process to achieve the purpose of self-restart.
if __name__ == "__main__":
print 'start...'
Print u" After 3 seconds, the program will end...".encode("utf8")
time.sleep(3)
restart_program()

@2017.04.19Create this article
@2017.04.24Add eval/exec function
@2017.05.27 increase program operation, function timeout, function error retry
@2017.08.24Add format splicing string with variables, string derivation

本文标题:

文章作者:nmask

发布时间:2017年04月19日 - 10:04

最后更新:2019年08月16日 - 15:08

原始链接:https://thief.one/2017/04/19/01/en/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

nmask wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
坚持原创技术分享,您的支持将鼓励我继续创作!

热门文章推荐: