Some of the wonders of python development

Posted by straycat on Tue, 10 Sep 2019 15:07:20 +0200

This article serves as a record of some of the tricks encountered in the use of python, some of which are written by myself, some of which are built-in functions of python, and some of which are taken from the Internet. In this record, only for backup and forgetting when easy to find.
This article will continue to be updated to record only a few commonly used but never memorable codes or modules.

Console operation

Console does not flip back

os.system('pause')

Get the console size

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

Input and output control

Solve the problem of Chinese scrambling in input prompts

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

Format output

print a.prettify()

Accept multi-line input

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

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

Standard Input and Output

sys.stdout.write("input") standard input
 sys.stdout.flush() refresh buffer

The function of print is similar to that of sys.stdout.write, because print in 2.x by default specifies output to standard output (sys.stdout).

If you are still confused in the world of programming, you can join our Python Learning button qun: 784758214 to see how our predecessors learned. Exchange of experience. From basic Python script to web development, crawler, django, data mining, zero-base to actual project data are sorted out. To every Python buddy! Share some learning methods and small details that need attention. Click to join us. python learner gathering place

Color control

Console color control (for windows)

WConio.textcolor(WConio.YELLOW)
print "yellow"
WConio.textcolor(WConio.BLUE)
print "blue"

Output color control (full platform)

red = '\033[1;31m'
green = '\033[1;32m'
yellow = '\033[1;33m'
white = '\033[1;37m'
reset = '\033[0m'
print red+"color is red"+reset
print green+"color is green"+reset

Progress bar control

Scheme 1

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 two

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

Option three

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 uses can use the progressbar module.

system operation

system information

Get python installation path

What I don't know in the process of learning can be added to me?
python learning communication deduction qun, 784758214
 There are good learning video tutorials, development tools and e-books in the group.
Share with you the current talent needs of python enterprises and how to learn python from zero foundation, and what to learn
from distutils.sysconfig import get_python_lib
print get_python_lib

Get the current python version

sys.version_info
sys.version

Get the current time

c=time.ctime()
#Custom Format Output
ISOTIMEFORMAT='%Y-%m-%d %X'
time.strftime( ISOTIMEFORMAT, time.localtime() )

View System Environment Variables

os.environ["PATH"]

Getting System Disk

os.popen("wmic VOLUME GET Name")

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

os.path.realpath(__file__)

Line terminators used by the current platform

os.linesep

Get terminal size

rows, columns = os.popen('stty size', 'r').read().split()
#After Python 3, os can be used
os.get_termial_size()

Exit procedure

  • return: Returns the value of the function and exits the function.
  • exit(): Exit directly.
  • sys.exit(): Raises a SystemExit exception, and if no error is caught, the python program exits directly; after the exception is caught, additional cleanup can be done.
  • sys.exit(0): For normal exit, the others (1-127) are abnormal and can throw abnormal things for capture. (Usually used to exit programs in the main thread)
  • os._exit(0): Quit the python program directly and the code will not execute afterwards. (Usually used to exit programs in threads)

Network operation

Domain name resolution to ip

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

Get server version information

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

File operation

open function, using wb, rb instead of w, r

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

This is compatible with Python 2/3.
Output all file names in a directory

def search(paths):
    if os.path.isdir(paths):  #If it is a directory
          files=os.listdir(paths)  #List all files in the directory
          for i in files:
               i=os.path.join(paths,i)  #Constructing File Path
               search(i)           #recursion
          elif os.path.isfile(paths): #If it is a document
               print paths   #Output file name

File search

import glob
 print glob.glob(r"E:/*.txt")# returns a list
 Finding files uses only three matches: "*", "?", "[]"“
”*” Match 0 or more characters;
” Match a single character.
” [] "Matches characters within a specified range, such as: [0-9] matches numbers.

Find the path to the folder with the specified name

def search(paths,file_name,tag,lists):
    if os.path.isdir(paths):  #If it is a 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 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)  #Constructing File Path
                    search(path_new,file_name,tag,lists)    #recursion
            except: #Error reporting when encountering a special directory name
                pass
    elif os.path.isfile(paths): #If it is a document
        pass
    return lists

Data manipulation

Judging data type

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

String

String Derivation

a="True"
b=a if a=="True" else "False"
>>>print b
True

format method splicing strings and variables


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

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

String inversion

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

Change the initial letter of a string to uppercase

info = 'ssfef'
print info.capitalize()
print info.title()

Returns a string in the middle and fills it with spaces to a new string of length width.

"center string".center(width) #Width is set to the console width, and the output string can be controlled in the middle.

List all the letters

print string.ascii_uppercase all capital letters
 print string. ascii_lowercase all lowercase letters
 print string.ascii_letters all letters (including case)

List (list)

List removal

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

Judge that the list is empty

a=[]
if not a:

List operation

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

Addition of single list elements

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

Multiple list elements are added separately

list1 = ['a', 'b', 'c', 'd']
list2 = ['p', 'q', 'r', 's']
>>> for x, y in zip(list1,list2):  
        print x, y
ap
bq
cr
ds

Converting nested lists to single lists

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

Addition of elements in a list

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

Generate a-z list of strings

map(chr,range(97,123))

List replication

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

List inference

if+else Coordination List Resolution

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

Multilayer nested list

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

Generate a generator and call the next method to reduce memory overhead.

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

Dictionary derivation

Change key and value positions

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

Dictionary operation (dict)

Screen out key with duplicate values

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)

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 sorting by key.

Dictionary Value Judgment

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

Module operation

When importing a module, set properties or methods that allow only imports.

fb.py:
-----------------------
__all__=["a","b"]
a="123"
c="2345"
def b():
    print "123"
-----------------------
from fb import *
//Variables defined in all can be imported, a and b() can be imported, c can not. If _all_ is not defined, all can be imported.

Import packages in superior directories

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

Import modules in external directories

The _init_ py file needs to be created in the target directory with arbitrary content.


Adding Module Properties

Sometimes in the source code, we need to write our own name and version information, which can be defined in the way of _name_.
a.py:
#! -*- coding:utf-8 -*-
__author__="nMask"

Then when we import the module a, we can output dir(a).

What I don't know in the process of learning can be added to me?
python learning communication deduction qun, 784758214
 There are good learning video tutorials, development tools and e-books in the group.
Share with you the current talent needs of python enterprises and how to learn python from zero foundation, and what to learn
>>> import p
>>> print dir(p)
['__author__', '__builtins__', '__doc__', '__file__', '__name__', '__package__']
>>> print p.__author__
nmask

Dynamic loading of all modules in a directory

//Catalog:
---test
   ----a.py
   ----b.py
---c.py
c.py Import test All modules below:
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": ##Eliminate unnecessary documents
            import_string = "import path+"."+i+"
            exec import_string #Execution string content

Function operation

eval/exec

def test(content):
    print content
exec("test('abc')")

Output: abc
Note: Exc function has no return value

def test(content):
    return content
print eval("test('abc')")

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

Decorator function

Output current time decorator

def current_time(aclass):
    def wrapper():
        print "[Info]NowTimeis:",time.ctime()
        return aclass()
    return wrapper

itertools iterator

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

reduce function

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

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

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

Function timeout settings

@ Update in 2017.05.27
Setting the timeout time of a function's execution with signal

import time
import signal

def test(i):
    time.sleep(0.999)#Simulate timeout
    print "%d within time"%(i)
    return i

def fuc_time(time_out):
    # This is the function timeout control. Replace the following test function with 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 timeout
        temp = test(1) #The function settings section returns data normally if it does not time out.
        return temp
    except AssertionError:
        print "%d timeout"%(i)# Error reporting if timeout occurs

if __name__ == '__main__':
    for i in range(1,10):
        fuc_time(1)

Function error retry

Using retrying Module to Realize Function Error Reporting Function

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 finish until random.randint returns 5, otherwise it will continue to be re-executed. Please search for more usage of this module.

Program operation

@ Update in 2017.05.27

Ctrl+C Exit Procedure

ctrl+c exit program is realized by signal.

What I don't know in the process of learning can be added to me?
python Learning Exchange Button qun,784758214
//There are good learning video tutorials, development tools and e-books in the group.
//Share with you the current talent needs of python enterprises and how to learn python from zero foundation, and what to learn
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 demonstration, we do a loop that won't jam the machine.
while 1:
    time.sleep(10)
# When you press Ctrl-C, you should output a paragraph and exit.

Program restart

Using os.execl Method to Realize Program Restart

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 in order to achieve the goal of self-restart.
if __name__ == "__main__":
     print 'start...'
     print u"3 Seconds later,The program will end...".encode("utf8")
     time.sleep(3)
     restart_program()

Time wall

@ Create this article on April 19, 2017
@ 2017.04.24 Add eval/exec function
@ 2017.05.27 Increase program operation, function timeout, function error retry
@ 2017.08.24 Add format splicing strings and variable, string derivation

Topics: Python Programming Web Development Django