python 一些函数

查看python模块的help:

例如查看redis-python 模块的help:

>>>import redis

>>>help(redis)

就会有redis模块的所有接口信息和描述了

 

自定义一个常量类

  const.py

class _const(object):
    class ConstError(TypeError): pass

    def __init__(self):
        self.__setattr__('LOG_FILE_PATH', '/var/log/')
        self.__setattr__('DEFAULT_LOGFILENAME', 'log.txt')

    def __setattr__(self, name, value):
        if self.__dict__.has_key(name):
            raise self.ConstError, "Can't rebind const(%s)" % name
        self.__dict__[name] = value
    def __delattr__(self, name):
        if name in self.__dict__:
            raise self.ConstError, "Can't unbind const(%s)" % name
        raise NameError, name

import sys
sys.modules[__name__] = _const();

 

使用常量类:

test.py

import os, sys, const

if __name__ == '__main__' :
    print (const.LOG_FILE_PATH)

 

 

 

 时间处理:

         有3个类:date、time、datetime,各自擅长的领域通过名字就知道

         废话不多说,开始正题:

         1.已知一个格式的日期字符串,转成另外一种:

         

#!/bin/env python
import datetime
import traceback
import sys

try:
    d1 = datetime.datetime.strptime('20140304', '%Y%m%d')
except Exception as e:
    print 'format is wrong'
    sys.exit()

print d1
print d1.strftime('%Y--%m--%d')

           

 根据相对日期进行加减:

today=datetime.datetime.now()                                            
oneday=datetime.timedelta(days=1)                                               
yesterday=today - oneday                                              
yesterday_str=yesterday.strftime('%m-%d')

 

 

 

 

 

 

 

你可能感兴趣的:(python)