天天看點

開始攻克下一本電子書PDF--Dive Into Python by Mark Pilgrim

<a href="http://woodpecker.org.cn/diveintopython3/">http://woodpecker.org.cn/diveintopython3/</a>

開始攻克下一本電子書PDF--Dive Into Python by Mark Pilgrim

同時推薦如下連結--

<a href="http://linuxtoy.org/archives/9-free-python-books.html">http://linuxtoy.org/archives/9-free-python-books.html</a>

copy自己測試的第一個sample:

suffixes = {1000:['kb','mb','gb','tb','pb','eb','zb','yb'],

            1024:['kib','mib','gib','tib','pib','eib','zib','yib']}

def approximate_size(size,a_kilobyte_is_1024_bytes=true):

    '''convert a file size to human-readeable form.

    keyword arguments:

    size -- file size in bytes

    a_kilobyte_is_1024_bytes --if true(default),use multiples of 1024

                               if false,use multiples of 1000

    returns:string

    '''

    if size &lt; 0:

        raise valueerror('number must be non-negative')

    multiple = 1024 if a_kilobyte_is_1024_bytes else 1000

    for suffix in suffixes[multiple]:

        size /= multiple

        if size &lt; multiple:

            return '{0:.1f} {1}'.format(size,suffix)

    raise valueerror('number too large')

if __name__ == '__main__':

    print(approximate_size(100,false))

    print(approximate_size(100))

開始攻克下一本電子書PDF--Dive Into Python by Mark Pilgrim