Python basics summary

  sonic0002        2013-09-23 10:04:42       11,296        0    

Python is recommended by many people as the first language to be learned now. Even in some universities, it is the primary language to be taught to CS/CE students. To master this languages, we should understand some basics of it. Here we summarize some basics about this language.

These basics include : string replacement with regular expression, traverse a directory, sort a list, remove duplication, dictionary ordering, dictionary, list, string conversion, date object manipulation, command line parameter parsing(getopt), formatted output, Call system commands or scripts with Python, Python file I/O.

1. String replacement with regular expression

Problem : Replace overview.gif with other string in line.

>>> line = '<IMG ALIGN="middle" SRC=\'#\'" /span>
>>> mo=re.compile(r'(?<=SRC=)"([\w+\.]+)"',re.I)  

>>> mo.sub(r'"\1****"',line)
'<IMG ALIGN="middle" SRC=\'#\'" /span> 

>>> mo.sub(r'replace_str_\1',line)
'<IMG ALIGN="middle" replace_str_overview.gif BORDER="0" ALT="">'< /span> 

>>> mo.sub(r'"testetstset"',line)
'<IMG ALIGN="middle" SRC=\'#\'" /span>

Note : \1 maps to the matched data.

2. Traverse a directory

Sometimes we need to traverse a directory to find some files we need, we can use os.walk to achieve this.

import os  
fileList = []  
rootdir = "/data" 
for root, subFolders, files in os.walk(rootdir):  
if '.svn' in subFolders: subFolders.remove('.svn')  # Filter some specified directories 
for file in files:  
  if file.find(".t2t") != -1:# Find files with specified extension name 
      file_dir_path = os.path.join(root,file)  
      fileList.append(file_dir_path)  
 
print fileList 

3. list sort

If each item in a list is a tuple, we need to sort the list according to a specified column in the tuple. We ca refer to below method. In the following example, we sort the list according to the 2nd and 3rd column and it's reverse sorted.

>>> a = [('2011-03-17', '2.26', 6429600, '0.0'), ('2011-03-16', '2.26', 12036900, '-3.0'), ('2011-03-15', '2.33', 15615500,'-19.1')]  
>>> print a[0][0]  
2011-03-17 
>>> b = sorted(a, key=lambda result: result[1],reverse=True)  
>>> print b  
[('2011-03-15', '2.33', 15615500, '-19.1'), ('2011-03-17', '2.26', 6429600, '0.0'), ('2011-03-16', '2.26', 12036900, '-3.0')]  
>>> c = sorted(a, key=lambda result: result[2],reverse=True)  
>>> print c  
[('2011-03-15', '2.33', 15615500, '-19.1'), ('2011-03-16', '2.26', 12036900, '-3.0'), ('2011-03-17', '2.26', 6429600, '0.0')] 

4. Remove list duplication

Below method can be used to remove duplication in a list:

>>> lst= [(1,'sss'),(2,'fsdf'),(1,'sss'),(3,'fd')]  
>>> set(lst)  
set([(2, 'fsdf'), (3, 'fd'), (1, 'sss')])  
>>>  
>>> lst = [1, 1, 3, 4, 4, 5, 6, 7, 6]  
>>> set(lst)  
set([1, 3, 4, 5, 6, 7]) 

5. Dictionary sort

Generally, we will sort a dictionary by its key. but if we want to sort the dictionary by its value, then we can use:

>>> from operator import itemgetter  
>>> aa = {"a":"1","sss":"2","ffdf":'5',"ffff2":'3'}  
>>> sort_aa = sorted(aa.items(),key=itemgetter(1))  
>>> sort_aa  
[('a', '1'), ('sss', '2'), ('ffff2', '3'), ('ffdf', '5')] 

6. Dictionary,list,string conversion

Below example is to convert a dictionary to a string

>>> params = {"server":"mpilgrim", "database":"master", "uid":"sa", "pwd":"secret"}  
>>> ["%s=%s" % (k, v) for k, v in params.items()]  
['server=mpilgrim', 'uid=sa', 'database=master', 'pwd=secret']  
>>> ";".join(["%s=%s" % (k, v) for k, v in params.items()])  
'server=mpilgrim;uid=sa;database=master;pwd=secret' 

Below is to convert a string to a dictionary

>>> a = 'server=mpilgrim;uid=sa;database=master;pwd=secret' 
>>> aa = {}  
>>> for i in a.split(';'):aa[i.split('=',1)[0]] = i.split('=',1)[1]  
...   
>>> aa  
{'pwd': 'secret', 'database': 'master', 'uid': 'sa', 'server': 'mpilgrim'} 

7. Date manipulation

Convert datetime to string

>>> import datetime  
>>> datetime.datetime.now().strftime("%Y-%m-%d %H:%M")  
  '2011-01-20 14:05' 

Datetime comparison

>>> import time  
>>> t1 = time.strptime('2011-01-20 14:05',"%Y-%m-%d %H:%M")  
>>> t2 = time.strptime('2011-01-20 16:05',"%Y-%m-%d %H:%M")  
>>> t1 > t2  
  False 
>>> t1 < t2  
  True 

Datetime difference

>>> datetime.datetime.now().strftime("%Y-%m-%d %H:%M")  
  '2011-01-20 15:02' 
>>> (datetime.datetime.now() - datetime.timedelta(hours=8)).strftime("%Y-%m-%d %H:%M")  
  '2011-01-20 07:03' 

Convert string to datetime

>>> endtime=datetime.datetime.strptime('20100701',"%Y%m%d")  
>>> type(endtime)  
  <type 'datetime.datetime'>  
>>> print endtime  
  2010-07-01 00:00:00 

8. Command line argument parsing

When we write some monitoring scripts, we may need to pass different arguments for different conditions. In Python, we can use getopt to parse command line arguments.

#!/usr/bin/env python  
# -*- coding: utf-8 -*-  
import sys,os,getopt  
def usage():  
print '''''  
Usage: analyse_stock.py [options...]  
Options:   
-e : Exchange Name   
-c : User-Defined Category Name  
-f : Read stock info from file and save to db  
-d : delete from db by stock code  
-n : stock name  
-s : stock code  
-h : this help info  
test.py -s haha -n "HA Ha"   
''' 
 
try:  
opts, args = getopt.getopt(sys.argv[1:],'he:c:f:d:n:s:')  
except getopt.GetoptError:  
usage()  
sys.exit()  
if len(opts) == 0:  
usage()  
sys.exit()  
 
for opt, arg in opts:   
if opt in ('-h', '--help'):  
  usage()  
  sys.exit()  
elif opt == '-d':  
  print "del stock %s" % arg  
elif opt == '-f':  
  print "read file %s" % arg  
elif opt == '-c':  
  print "user-defined %s " % arg  
elif opt == '-e':  
  print "Exchange Name %s" % arg  
elif opt == '-s':  
  print "Stock code %s" % arg  
elif opt == '-n':  
  print "Stock name %s" % arg  
 
sys.exit()  

9. Formatted output

9.1 Formatted string output

Print part of the string,  3 characters will be printed out below:

>>> str="abcdefg" 
>>> print "%.3s" % str  
  abc  

Print the string with fixed width

>>> str="abcdefg" 
>>> print "%10s" % str  
     abcdefg  

Print part of string with fixed width

>>> str="abcdefg" 
>>> print "%10.3s" % str  
         abc  

Float number with fixed decimal point

>>> import fpformat  
>>> a= 0.0030000000005 
>>> b=fpformat.fix(a,6)  
>>> print b  
  0.003000 

Round float number

>>> from decimal import *  
>>> a ="2.26" 
>>> b ="2.29" 
>>> c = Decimal(a) - Decimal(b)  
>>> print c  
  -0.03 
>>> c / Decimal(a) * 100 
  Decimal('-1.327433628318584070796460177')  
>>> Decimal(str(round(c / Decimal(a) * 100, 2)))  
  Decimal('-1.33') 

9.2 Number conversion

Sometimes we need to convert binary data to decimal data.

>>> num = 10 
>>> print "Hex = %x,Dec = %d,Oct = %o" %(num,num,num)  
  Hex = a,Dec = 10,Oct = 12 

10. Python to call system commands or scripts

Use os.system() to call system commands, cannot get output and returned value

>>> import os  
>>> os.system('ls -l /proc/cpuinfo')  
>>> os.system("ls -l /proc/cpuinfo")  
  -r--r--r-- 1 root root 0  3月 29 16:53 /proc/cpuinfo  
  0 

Use os.popen() to call system commands, can get output but cannot get returned value

>>> out = os.popen("ls -l /proc/cpuinfo")  
>>> print out.read()  
  -r--r--r-- 1 root root 0  3月 29 16:59 /proc/cpuinfo  

Use commands.getstatusoutput() to call system commands, can get output and returned value

>>> import commands  
>>> commands.getstatusoutput('ls /bin/ls')  
  (0, '/bin/ls') 

11. Use Python to capture Ctrl+C, Ctrl+D events

Sometimes we may need to capture key events.

try:   
    do_some_func()  
except KeyboardInterrupt:  
    print "User Press Ctrl+C,Exit" 
except EOFError:  
    print "User Press Ctrl+D,Exit" 

12. Python file I/O

Read file to a list at once.

track_file = "track_stock.conf"   
fd = open(track_file)  
content_list = fd.readlines()  
fd.close()  
for line in content_list:  
    print line  

Read file line by line. A bit slow.

fd = open(file_path)  
fd.seek(0)  
title = fd.readline()  
keyword = fd.readline()  
uuid = fd.readline()  
fd.close()  

Source : http://wangwei007.blog.51cto.com/68019/1131610/

SUMMARY  PYTHON 

       

  RELATED


  0 COMMENT


No comment for this article.



  RANDOM FUN

Spurious wakeup without spinlock