• python 实现 byte 可视化转换


    python 实现 byte 可视化转换

    制作命令行工具过程中,因需要展示文件大小,所以造了个轮子,实现了以下byte可视化转换。

    如果哪位仁兄知道有现成的轮子,劳烦留言告知一声。

    以下是代码实现。

    def unit_conversion(size):
        ratio = 2 ** 10
        units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
        level = 0
        size = int(size)
        if size < 0:
            try:
                raise ValueError
            except ValueError as e:
                print('Please enter the correct parameters', repr(e))
        for unit in units:
            if size > ratio:
                size = size / ratio
                level += 1
                continue
            else:
                size = '%.1f %s' % (size, unit)
                return size
    
        size = '这个大小多少有点过分'
        return size
    
    
    # 以下是测试部分
    test0 = unit_conversion(230)
    test1 = unit_conversion(1500)
    test2 = unit_conversion(2823230)
    test3 = unit_conversion(8231283128)
    test4 = unit_conversion(10239841023123)
    test5 = unit_conversion(502938748172301283017)
    test6 = unit_conversion(923619287389872013891231)
    test7 = unit_conversion(72379812387401293812847821631246)
    
    print(f'test0 : {test0}')
    print(f'test1 : {test1}')
    print(f'test2 : {test2}')
    print(f'test3 : {test3}')
    print(f'test4 : {test4}')
    print(f'test5 : {test5}')
    print(f'test6 : {test6}')
    print(f'test7 : {test7}')
    
    test0 : 230.0 B
    test1 : 1.5 KB
    test2 : 2.7 MB
    test3 : 7.7 GB
    test4 : 9.3 TB
    test5 : 436.2 EB
    test6 : 782.3 ZB
    test7 : 这个大小多少有点过分
    
  • 相关阅读:
    学了axure的感受
    axure的功能
    PS的应用
    day15-1 模块
    day14-2 模块详解
    day14-1 模块定义,导入
    day13-1 Json & pickle 数据序列化
    day12-3 内置方法
    day12-2 内置方法
    day12-1 迭代器和生成器
  • 原文地址:https://www.cnblogs.com/shu-sheng/p/13646858.html
Copyright © 2020-2023  润新知