python标准库学习8

简介:

使用sys重定向输出

import  sys
import  string
 
class  Redirect:
 
     def  _ _init_ _( self , stdout):
         self .stdout =  stdout
 
     def  write( self , s):
         self .stdout.write(string.lower(s))
 
# redirect standard output (including the print statement)
# 重定向标准输出(包括print语句)
old_stdout =  sys.stdout
sys.stdout =  Redirect(sys.stdout)
 
print  "HEJA SVERIGE" ,
print  "FRISKT HUM\303\226R"
 
# restore standard output
# 恢复标准输出
sys.stdout =  old_stdout
 
print  "M\303\205\303\205\303\205\303\205L!"
 
heja sverige friskt hum\ 303 \ 266r
M\ 303 \ 205 \ 303 \ 205 \ 303 \ 205 \ 303 \ 205L !

  

使用sys模块退出程序

import  sys
 
print  "hello"
 
sys.exit( 1 )
 
print  "there"
 
hello

  注意 sys.exit 并不是立即退出. 而是引发一个 SystemExit 异常. 这意味着你可以在主程序中捕获对 sys.exit 的调用

捕获sys.exit调用

import  sys
 
print  "hello"
 
try :
     sys.exit( 1 )
except  SystemExit:
     pass
 
print  "there"
 
hello
there

  如果准备在退出前自己清理一些东西(比如删除临时文件), 你可以配置一个 "退出处理函数"(exit handler), 它将在程序退出的时候自动被调用

另一种捕获sys.exit调用的方法

import  sys
 
def  exitfunc():
     print  "world"
 
sys.exitfunc =  exitfunc
 
print  "hello"
sys.exit( 1 )
print  "there"  # never printed # 不会被 print
 
hello
world

  在 Python 2.0 以后, 你可以使用 atexit 模块来注册多个退出处理函数.

 atexit 模块允许你注册一个或多个终止函数(暂且这么叫), 这些函数将在解释器终止前被自动调用.

调用 register 函数, 便可以将函数注册为终止函数,你也可以添加更多的参数, 这些将作为 exit 函数的参数传递.

使用 atexit 模块

import  atexit
 
def  exit( * args):
     print  "exit" , args
 
# register two exit handler
atexit.register(exit)
atexit.register(exit, 1 )
atexit.register(exit, "hello" , "world" )
 
exit ( 'hello' , 'world' )
exit ( 1 ,)
exit ()

  

time 模块提供了一些处理日期和一天内时间的函数. 它是建立在 C 运行时库的简单封装.

给定的日期和时间可以被表示为浮点型(从参考时间, 通常是 1970.1.1 到现在经过的秒数. 即 Unix 格式), 或者一个表示时间的 struct (类元组).

使用 time 模块获取当前时间

import  time
 
now =  time.time()
 
print  now, "seconds since" , time.gmtime( 0 )[: 6 ]
print
print  "or in other words:"
print  "- local time:" , time.localtime(now)
print  "- utc:" , time.gmtime(now)
 
937758359.77  seconds since ( 1970 , 1 , 1 , 0 , 0 , 0 )
 
or  in  other words:
-  local time: ( 1999 , 9 , 19 , 18 , 25 , 59 , 6 , 262 , 1 )
-  utc: ( 1999 , 9 , 19 , 16 , 25 , 59 , 6 , 262 , 0 )

  

使用 time 模块格式化时间输出

import  time
 
now =  time.localtime(time.time())
 
print  time.asctime(now)
print  time.strftime( "%y/%m/%d %H:%M" , now)
print  time.strftime( "%a %b %d" , now)
print  time.strftime( "%c" , now)
print  time.strftime( "%I %p" , now)
print  time.strftime( "%Y-%m-%d %H:%M:%S %Z" , now)
 
# do it by hand...
year, month, day, hour, minute, second, weekday, yearday, daylight =  now
print  "%04d-%02d-%02d"  %  (year, month, day)
print  "%02d:%02d:%02d"  %  (hour, minute, second)
print  ( "MON" , "TUE" , "WED" , "THU" , "FRI" , "SAT" , "SUN" )[weekday], yearday
 
Sun Oct  10  21 : 39 : 24  1999
99 / 10 / 10  21 : 39
Sun Oct  10
Sun Oct  10  21 : 39 : 24  1999
09  PM
1999 - 10 - 10  21 : 39 : 24  CEST
1999 - 10 - 10
21 : 39 : 24
SUN 283

  在一些平台上, time 模块包含了 strptime 函数, 它的作用与 strftime 相反. 给定一个字符串和模式, 它返回相应的时间对象

使用 time.strptime 函数解析时间

import  time
 
# make sure we have a strptime function!
# 确认有函数 strptime
try :
     strptime =  time.strptime
except  AttributeError:
     from  strptime import  strptime
 
print  strptime( "31 Nov 00" , "%d %b %y" )
print  strptime( "1 Jan 70 1:30pm" , "%d %b %y %I:%M%p" )

  

strptime 不完全实现

import  re
import  string
 
MONTHS =  [ "Jan" , "Feb" , "Mar" , "Apr" , "May" , "Jun" , "Jul" , "Aug" ,
           "Sep" , "Oct" , "Nov" , "Dec" ]
 
SPEC =  {
     # map formatting code to a regular expression fragment
     "%a" : "(?P<weekday>[a-z]+)" ,
     "%A" : "(?P<weekday>[a-z]+)" ,
     "%b" : "(?P<month>[a-z]+)" ,
     "%B" : "(?P<month>[a-z]+)" ,
     "%C" : "(?P<century>\d\d?)" ,
     "%d" : "(?P<day>\d\d?)" ,
     "%D" : "(?P<month>\d\d?)/(?P<day>\d\d?)/(?P<year>\d\d)" ,
     "%e" : "(?P<day>\d\d?)" ,
     "%h" : "(?P<month>[a-z]+)" ,
     "%H" : "(?P<hour>\d\d?)" ,
     "%I" : "(?P<hour12>\d\d?)" ,
     "%j" : "(?P<yearday>\d\d?\d?)" ,
     "%m" : "(?P<month>\d\d?)" ,
     "%M" : "(?P<minute>\d\d?)" ,
     "%p" : "(?P<ampm12>am|pm)" ,
     "%R" : "(?P<hour>\d\d?):(?P<minute>\d\d?)" ,
     "%S" : "(?P<second>\d\d?)" ,
     "%T" : "(?P<hour>\d\d?):(?P<minute>\d\d?):(?P<second>\d\d?)" ,
     "%U" : "(?P<week>\d\d)" ,
     "%w" : "(?P<weekday>\d)" ,
     "%W" : "(?P<weekday>\d\d)" ,
     "%y" : "(?P<year>\d\d)" ,
     "%Y" : "(?P<year>\d\d\d\d)" ,
     "%%" : "%"
}
 
class  TimeParser:
     def  _ _init_ _( self , format ):
         # convert strptime format string to regular expression
         format  =  string.join(re.split( "(?:\s|%t|%n)+" , format ))
         pattern =  []
         try :
             for  spec in  re.findall( "%\w|%%|." , format ):
                 if  spec[ 0 ] = =  "%" :
                     spec =  SPEC[spec]
                 pattern.append(spec)
         except  KeyError:
             raise  ValueError, "unknown specificer: %s"  %  spec
         self .pattern =  re. compile ( "(?i)"  +  string.join(pattern, ""))
     def  match( self , daytime):
         # match time string
         match =  self .pattern.match(daytime)
         if  not  match:
             raise  ValueError, "format mismatch"
         get =  match.groupdict().get
         tm =  [ 0 ] *  9
         # extract date elements
         y =  get( "year" )
         if  y:
             y =  int (y)
             if  y < 68 :
                 y =  2000  +  y
             elif  y < 100 :
                 y =  1900  +  y
             tm[ 0 ] =  y
         m =  get( "month" )
         if  m:
             if  m in  MONTHS:
                 m =  MONTHS.index(m) +  1
             tm[ 1 ] =  int (m)
         d =  get( "day" )
         if  d: tm[ 2 ] =  int (d)
         # extract time elements
         h =  get( "hour" )
         if  h:
             tm[ 3 ] =  int (h)
         else :
             h =  get( "hour12" )
             if  h:
                 h =  int (h)
                 if  string.lower(get( "ampm12" , " ")) == " pm":
                     h =  h +  12
                 tm[ 3 ] =  h
         m =  get( "minute" )
         if  m: tm[ 4 ] =  int (m)
         s =  get( "second" )
         if  s: tm[ 5 ] =  int (s)
         # ignore weekday/yearday for now
         return  tuple (tm)
 
def  strptime(string, format = "%a %b %d %H:%M:%S %Y" ):
     return  TimeParser( format ).match(string)
 
if  _ _name_ _ = =  "_ _main_ _" :
     # try it out
     import  time
     print  strptime( "2000-12-20 01:02:03" , "%Y-%m-%d %H:%M:%S" )
     print  strptime(time.ctime(time.time()))
 
( 2000 , 12 , 20 , 1 , 2 , 3 , 0 , 0 , 0 )
( 2000 , 11 , 15 , 12 , 30 , 45 , 0 , 0 , 0 )

  

使用 time 模块将本地时间元组转换为时间值(整数)

import  time
 
t0 =  time.time()
tm =  time.localtime(t0)
 
print  tm
 
print  t0
print  time.mktime(tm)
 
( 1999 , 9 , 9 , 0 , 11 , 8 , 3 , 252 , 1 )
936828668.16
936828668.0

  

将 UTC 时间元组转换为时间值(整数)

import  time
 
def  _d(y, m, d, days = ( 0 , 31 , 59 , 90 , 120 , 151 , 181 , 212 , 243 , 273 , 304 , 334 , 365 )):
     # map a date to the number of days from a reference point
     return  (((y -  1901 ) * 1461 ) / 4  +  days[m - 1 ] +  d +
         ((m > 2  and  not  y %  4  and  (y %  100  or  not  y %  400 )) and  1 ))
 
def  timegm(tm, epoch = _d( 1970 , 1 , 1 )):
     year, month, day, h, m, s =  tm[: 6 ]
     assert  year > =  1970
     assert  1  < =  month < =  12
     return  (_d(year, month, day) -  epoch) * 86400  +  h * 3600  +  m * 60  +  s
 
t0 =  time.time()
tm =  time.gmtime(t0)
 
print  tm
 
print  t0
print  timegm(tm)
 
( 1999 , 9 , 8 , 22 , 12 , 12 , 2 , 251 , 0 )
936828732.48
936828732

  

使用 time 模块评价算法

import  time
 
def  procedure():
     time.sleep( 2.5 )
 
# measure process time
t0 =  time.clock()
procedure()
print  time.clock() -  t0, "seconds process time"
 
# measure wall time
t0 =  time.time()
procedure()
print  time.time() -  t0, "seconds wall time"
 
0.0  seconds process time
2.50903499126  seconds wall time

  

使用 types 模块

import  types
 
def  check( object ):
     print  object ,
 
     if  type ( object ) is  types.IntType:
         print  "INTEGER" ,
     if  type ( object ) is  types.FloatType:
         print  "FLOAT" ,
     if  type ( object ) is  types.StringType:
         print  "STRING" ,
     if  type ( object ) is  types.ClassType:
         print  "CLASS" ,
     if  type ( object ) is  types.InstanceType:
         print  "INSTANCE" ,
     print
 
check( 0 )
check( 0.0 )
check( "0" )
 
class  A:
     pass
 
class  B:
     pass
 
check(A)
check(B)
 
a =  A()
b =  B()
 
check(a)
check(b)
 
0  INTEGER
0.0  FLOAT
0  STRING
A CLASS
B CLASS
<A instance at 796960 > INSTANCE
<B instance at 796990 > INSTANCE

  

使用 gc 模块收集循环引用垃圾

import  gc
 
# create a simple object that links to itself
class  Node:
 
     def  _ _init_ _( self , name):
         self .name =  name
         self .parent =  None
         self .children =  []
 
     def  addchild( self , node):
         node.parent =  self
         self .children.append(node)
 
     def  _ _repr_ _( self ):
         return  "<Node %s at %x>"  %  ( repr ( self .name), id ( self ))
 
# set up a self-referencing structure
root =  Node( "monty" )
 
root.addchild(Node( "eric" ))
root.addchild(Node( "john" ))
root.addchild(Node( "michael" ))
 
# remove our only reference
del  root
 
print  gc.collect(), "unreachable objects"
print  gc.collect(), "unreachable objects"
 
12  unreachable objects
0  unreachable objects

  


==============================================================================
本文转自被遗忘的博客园博客,原文链接:http://www.cnblogs.com/rollenholt/archive/2011/11/27/2264909.html,如需转载请自行联系原作者
相关文章
|
1天前
|
开发者 Python
Python学习九:file操作
这篇文章是关于Python文件操作的详细教程,包括文件的打开、读写、关闭,以及文件备份脚本的编写和文件定位操作。
|
1天前
|
Java C# Python
Python学习七:面向对象编程(中)
这篇文章是关于Python面向对象编程的中级教程,涵盖了析构函数、对象的三大特征(封装、继承、多态)、类属性与实例属性、以及类方法与静态方法的对比。
10 2
|
1天前
|
设计模式 安全 JavaScript
Python学习八:面向对象编程(下):异常、私有等
这篇文章详细介绍了Python面向对象编程中的私有属性、私有方法、异常处理及动态添加属性和方法等关键概念。
|
1天前
|
存储 Java 编译器
Python学习三:学习python的 变量命名规则,算数、比较、逻辑、赋值运算符,输入与输出。
这篇文章是关于Python编程语言中变量命名规则、基本数据类型、算数运算符、比较运算符、逻辑运算符、赋值运算符以及格式化输出与输入的详细教程。
Python学习三:学习python的 变量命名规则,算数、比较、逻辑、赋值运算符,输入与输出。
|
1天前
|
资源调度 前端开发 JavaScript
Python学习二:Python包管理器pip
这篇文章介绍了Python包管理器pip的基本概念、基本操作、如何更改下载源为国内镜像以加速下载,以及如何指定安装包的位置。
Python学习二:Python包管理器pip
|
4天前
|
Shell Python
Python 的 os 库的应用实例
Python 的 os 库的应用实例
12 3
|
1天前
|
PyTorch 测试技术 算法框架/工具
Python中Thop库的常见用法和代码示例
肆十二在B站分享了关于THOP(Torch-OpCounter)的实战教学视频。THOP是一个用于计算PyTorch模型操作数和计算量的工具,帮助开发者评估模型复杂度和性能。本文介绍了THOP的安装、使用方法及基本用例,包括如何计算模型的FLOPs和参数量。
5 0
|
1天前
|
算法 数据可视化 计算机视觉
Python中医学图像处理常用的库
在Python中,医学图像处理常用的库包括:ITK(及其简化版SimpleITK)、3D Slicer、Pydicom、Nibabel、MedPy、OpenCV、Pillow和Scikit-Image。这些库分别擅长图像分割、配准、处理DICOM和NIfTI格式文件、图像增强及基础图像处理等任务。选择合适的库需根据具体需求和项目要求。
6 0
|
1天前
|
SQL 关系型数据库 MySQL
Python中Pymysql库的常见用法和代码示例
`pymysql` 是一个用于连接 MySQL 数据库的 Python 库,支持 SQL 查询的执行和结果处理。通过 `pip install pymysql` 安装后,可使用 `connect()` 方法建立连接,`cursor()` 创建游标执行查询,包括数据的增删改查,并通过 `commit()` 和 `rollback()` 管理事务,最后需关闭游标和连接以释放资源。
9 0
|
1天前
|
计算机视觉 Python
Python中Pillow库的常见用法和代码示例
Pillow是Python中广泛使用的图像处理库,支持丰富的图像操作功能,包括但不限于打开、保存、缩放、裁剪、旋转、调色等。本文通过一系列示例介绍Pillow的基本用法,涵盖图像的加载与显示、尺寸调整、裁剪与旋转、亮度调整、格式转换、滤镜应用、图像合成及像素级操作等。首先需通过`pip install pillow`安装库,随后可通过导入`PIL.Image`等模块开始图像处理任务。无论是初学者还是进阶用户,都能从Pillow提供的强大功能中获益。
5 0