Python代码整理 2012

判断当前文件是否被直接执行,还是被当作模块加载

if __name__ == "__main__":
    main()

将工作目录(current work path)切换到当前脚本文件所在的目录

os.chdir(os.path.dirname(os.path.abspath(os.path.join(os.getcwd(), __file__))))


设置默认字符串编码方式

reload(sys)
sys.setdefaultencoding('utf-8') 

写文件

def saveToFile(string, filePath):
    f = open(filePath, "wb")    # "a" for append
    f.write(string)
    f.close()


遍历文件夹

def scanDir(path):
    list = os.listdir(path)
    for item in list:
        filepath = os.path.join(path, item)
        print filepath


重定向输出

class NoneOutput:  
    def __init__(self):  
        pass
    def write(self, s):
        pass
    def flush(self):  
        pass

logfile = open(os.path.join("./", "updatescan.log"), "w", 0)
errfile = NoneOutput()
sys.stdout = logfile
sys.stderr = errfile

#restore
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__


打印堆栈信息

def traceback():
    import traceback
    return traceback.format_exc()

md5和sha1哈希算法

def hash_sha1(content):
    import hashlib
    hash = hashlib.sha1(content)
    return hash.hexdigest()
    
def hash_sha1_file(path):
    import hashlib
    hash = hashlib.sha1()
    file = open(path, "rb")
    hash.update(file.read())
    file.close()
    return hash.hexdigest()
    
def hash_md5(content):
    import hashlib
    hash = hashlib.md5(content)
    return hash.hexdigest()

def hash_md5_file(path):
    import hashlib
    hash = hashlib.md5()
    file = open(path, "rb")
    hash.update(file.read())
    file.close()
    return hash.hexdigest()

size,datetime,time 转换为便于阅读的字符串

def readableDatetime(date = datetime.datetime.now(), format="%Y-%m-%d %H:%M:%S"):
    return date.strftime(format)
    
def readableTime(time_second):
    units = ["sec", "min", "hour", "day"]
    scale = [60, 60, 24]
    t = float(time_second)
    for i in xrange(len(units)):
        if i < len(scale):
            s = scale[i]
        if not s or t < s:
            return "%.1f%s" % (t, units[i])
        t /= s

def readableSize(size):
    units = ["B", "K", "M", "G", "T"]
    size = float(size)
    for u in units:
        if size < 1024:
            return "%.1f%s" % (size, u)
        size /= 1024.0
 
  

限定函数执行时间

def timeout(timeout_second, func, args=(), kwargs={}, default=None):
    if args == None:
        args = ()
    if kwargs == None:
        kwargs = {}
    import threading
    class InterruptableThread(threading.Thread):
        def __init__(self):
            threading.Thread.__init__(self)
            self.result = default
        def run(self):
            self.result = func(*args, **kwargs)
    it = InterruptableThread()
    it.start()
    it.join(timeout_second)
    if it.isAlive():
        it._Thread__stop()
        return it.result
    else:
        return it.result

下载文件

def downloadFile(url, path, urlCallback = None, timelimit = None):
    '''callback(blockCount, blockSize, totalSize)'''
    import urllib
    import urllib2
    import httplib
    import os
    global last
    last = 0
    result = True
    try:
        mkdirIfNotExist(path)
        class AppURLopener(urllib.FancyURLopener):
            version = USER_AGENT
        urllib._urlopener = AppURLopener()
        if None == timelimit or timelimit <= 0:
            result = urllib.urlretrieve(url, path, urlCallback)
        else:
            result = timeout(timelimit, urllib.urlretrieve, (url, path, urlCallback))
        if None == result:
            print >>sys.stderr, "ERROR: Download timeout %s, url=%s" % (readableTime(timelimit), url)
            result = False
        #filename, info = urllib.urlretrieve(url, path, urlCallback)
    except urllib2.URLError, e:
        if hasattr(e, "code"):
            print >>sys.stderr, "ERROR: Url open failed, %s" % (str(e.code))
            result = False
        elif hasattr(e, "reason"):
            print >>sys.stderr, "ERROR: Url open failed, %s" % (str(e.reason))
            result = False
    except httplib.BadStatusLine, e:
        print >>sys.stderr, "ERROR: Url open failed, BadStatusLine error"
        result = False
    except Exception, e:
        print >>sys.stderr, "ERROR: Url open failed, unknown error,", e
        #print >>sys.stderr, traceback()
        result = False
    finally:
        if not result and os.path.exists(path):
            os.remove(path)
    return result

下载文件前获取文件大小

def requestFileSize(url):
    import urllib2
    import httplib
    import os
    import sys
    
    try:
        headers={'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'}
        #headers = {'User-Agent':USER_AGENT}
        request = urllib2.Request(url, headers = headers)
        response = urllib2.urlopen(request, timeout = 5)
    except urllib2.URLError, e:
        if hasattr(e, "code"):
            #print >>sys.stderr, "ERROR: Url open failed, %s, %s" % (str(e.code), url)
            return (None, str(e.code))
        elif hasattr(e, "reason"):
            #print >>sys.stderr, "ERROR: Url open failed, %s, %s" % (str(e.reason), url)
            return (None, str(e.reason))
    except httplib.BadStatusLine, e:
        #print >>sys.stderr, "ERROR: Url open failed, BadStatusLine error"
        return (None, "BadStatusLine")
    except:
        #print >>sys.stderr, "ERROR: Url open failed, unknown error"
        return (None, "Unknown")


发送邮件

EMAIL_CONFIG = {
    'mail_server':'smtp.163.com',
    'mail_port':25,
    'from_name':'[email protected]',
    'from_pwd':'123456',
    'to_name_list':[
       '[email protected]', 
    ],
}

def send_email(subject, text, attachments=None, toNames=None):
    import os
    import smtplib
    import mimetypes
    from email.mime.text import MIMEText  
    from email.mime.multipart import MIMEMultipart  
    from email.mime.image import MIMEImage
    try:
        msg = MIMEMultipart()
        
        msg['From'] = EMAIL_CONFIG['from_name']
        if toNames != None:
            msg['To'] = ','.join(toNames)
        else:
            msg['To'] = ','.join(EMAIL_CONFIG['to_name_list'])
        msg['Subject'] = subject
        
        txt = MIMEText(text)
        msg.attach(txt)
        
        if attachments != None:
            for file in attachments:
                #path = os.path.join(os.getcwd(), file)
                path = file
                ctype, encoding = mimetypes.guess_type(path)  
                if ctype is None or encoding is not None:  
                    ctype = 'application/octet-stream'  
                maintype, subtype = ctype.split('/', 1)  
                att = MIMEImage((lambda f: (f.read(), f.close()))(open(path, 'rb'))[0], _subtype = subtype)  
                att.add_header('Content-Disposition', 'attachment', filename = path)  
                msg.attach(att)
        
        server_mail = smtplib.SMTP()
        server_mail.connect(EMAIL_CONFIG['mail_server'], EMAIL_CONFIG['mail_port'])
        server_mail.login(EMAIL_CONFIG['from_name'],EMAIL_CONFIG['from_pwd'])
        server_mail.sendmail(EMAIL_CONFIG['from_name'], EMAIL_CONFIG['to_name_list'], msg.as_string())
        server_mail.quit()
        print 'send mail Ok'
    except Exception ,ex:
        print 'send email Error:'+str(ex)
        print traceback()
        return False
    return True


多线程工作

def run():
    import threading
    import Queue
    global threads
    taskQueue = Queue.Queue()
    exitEvent = threading.Event()
    syncLock = threading.Semaphore()
    total = queryTasks()
    for i in range(threadCount):
        thread_id = len(threads)
        th = threading.Thread(target=downloadThread, args=(thread_id,))
        threads.append(th)
        th.start()
        time.sleep(.1)
    taskQueue.join()
    exitEvent.set()
    for th in threads:
        th.join()

def downloadThread(thread_id):
    global taskQueue
    global syncLock
    global lastPingTime
    while not exitEvent.is_set():
        try:
            task = taskQueue.get(True, 1)
        except:
            time.sleep(1)
            continue
        try:
            # DO SOME WORK
            syncLock.acquire()
            try:
                # DO SOME SYNC THING
            finally:
                syncLock.release()
            # DO SOME WORK

        except Exception, e:
            #util.output("%s" % e, sys.stderr)
            #util.output(util.traceback(), sys.stderr)
        finally:
            taskQueue.task_done()   # important
            time.sleep(1)


解压文件

def extractFile(filePath, innerFile, targetDir):    #Linux
    import os
    targetFile = os.path.join(targetDir, innerFile)
    r = os.system('unzip -o "%s" "%s" -d "%s"' % (filePath, innerFile, targetDir))
    return r == 0

def extractAllFiles(filePath, targetDir = None):    #Linux
    from zipfile import ZipFile
    import os
    if targetDir == None:
        name, ext = os.path.splitext(filePath)
        dirname = os.path.dirname(filePath)
        targetDir = os.path.join(dirname, name)
    mkdirIfNotExist(targetDir, True)
    os.popen('unzip -o "%s" -d "%s"' % (filePath, targetDir))
    return targetDir
def extractFileWithPythonZip(filePath, innerFile, targetDir): import os targetFile = os.path.join(targetDir, innerFile) mkdirIfNotExist(targetDir, targetFile) from zipfile import ZipFile try: zip = ZipFile(filePath) if not zip: #print "ERROR: Zip file not found path=%s" % (filePath) return False zip.extract(innerFile, targetDir) except Exception, e: #print "ERROR: Unzip failed! path=%s, %s" % (filePath, e) return False if not os.path.exists(targetFile): return False return targetFile
def unzipTest(filePath):
    import os
    import platform
    
    def _unzipTestLinux(path):
        return os.system('unzip -t "%s"' % (filePath))
        
    def _unzipTestPython(path):
        from zipfile import ZipFile
        try:
            zip = ZipFile(path)
            if not zip:
                return False
        except:
            return False
        return True
    
    if platform.system() == "Linux":
        r = _unzipTestLinux(filePath)
        if r != 0:
            return _unzipTestPython(filePath)
        return r == 0
    else:
        return _unzipTestPython(filePath)
 
  

字符串编码转换

def encode_default(coding="utf-8"):
    import sys
    reload(sys)
    sys.setdefaultencoding(coding)
    
def code_unicode(s):
    if not s:
        return None
    if isinstance(s, unicode):
        return s
    charsets = ["utf-8", "gbk",  "big5", "windows-1252", "ascii"]
    for charset in charsets:
        try:
            s = s.decode(charset)
        except:
            pass
        else:
            break
    else:
        return None
    return s

def code_utf8(s):
    s = code_unicode(s)
    if not s:
        return None
    try:
        s = s.encode("utf-8")
    except:
        return None
    return s


你可能感兴趣的:(代码整理)