Welcome, guest | Sign In | My Account | Store | Cart

A set of simple functions to represent bytes or seconds in a human readable form.

Python, 87 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
def size(bytes=0):
    kbyte = 1024
    mbyte = (kbyte**2)
    gbyte = (kbyte**3)
    tbyte = (kbyte**4)
    pbyte = (kbyte**5)
    ebyte = (kbyte**6)
    zbyte = (kbyte**7)
    
    if bytes < kbyte: 
        retv = '%dB' % int(bytes)
        return unicode('%9s' % retv)
    elif bytes >= kbyte and bytes < mbyte:
        retv = '%04.02fKB' % (float(bytes) / float(kbyte))
        return unicode('%9s' % retv)
    elif bytes >= mbyte and bytes < gbyte:
        retv = '%04.02fMB' % (float(bytes) / float(mbyte))
        return unicode('%9s' % retv)
    elif bytes >= gbyte and bytes < tbyte:
        retv = '%04.02fGB' % (float(bytes) / float(gbyte))
        return unicode('%9s' % retv)
    elif bytes >= tbyte and bytes < pbyte:
        retv = '%04.02fTB' % (float(bytes) / float(tbyte))
        return unicode('%9s' % retv)
    elif bytes >= pbyte and bytes < ebyte:
        retv = '%04.02fPB' % (float(bytes) / float(pbyte))
        return unicode('%9s' % retv)
    elif bytes >= ebyte and bytes < zbyte:
        retv = '%04.02fEB' % (float(bytes) / float(ebyte))
        return unicode('%9s' % retv)
    else:
        retv = '%04.02fZB' % (float(bytes) / float(zbyte))
        return unicode('%9s' % retv)
        
def time(seconds=0):
    # These are for convenience
    minute = 60
    hour   = (minute**2)
    day    = (hour*24)
    week   = (day*7)
    month  = (week*4)
    year   = (month*12)
    
    secs, mins, hrs, days, weeks, months, years = 0, 0, 0, 0, 0, 0, 0
    
    if seconds > year:
        years   = (seconds / year)
        tmp     = (seconds % year)
        seconds = tmp
    if seconds > month:
        months  = (seconds / month)
        tmp     = (seconds % month)
        seconds = tmp
    if seconds > week:
        weeks   = (seconds / week)
        tmp     = (seconds % week)
        seconds = tmp
    if seconds > day:
        days    = (seconds / day)
        tmp     = (seconds % day)
        seconds = tmp
    if seconds > hour:
        hrs     = (seconds / hour)
        tmp     = (seconds % hour)
        seconds = tmp
    if seconds > minute:
        mins    = (seconds / minute)
        secs    = (seconds % minute)
    if seconds < minute:
        secs   = seconds

    if years != 0:
        return unicode('%4dy%2dm%1dw%1dd %02d:%02d:%02d' % (
            years, months, weeks, days, hrs, mins, secs
        ))
    if months != 0:
        return unicode('%2dm%1dw%1dd %02d:%02d:%02d' % (
            months, weeks, days, hrs, mins, secs
        ))
    if weeks != 0:
        return unicode('%1dw%1dd %02d:%02d:%02d' % (
            weeks, days, hrs, mins, secs
        ))
    if days != 0:
        return unicode('%1dd %02d:%02d:%02d' % (days, hrs, mins, secs))
        
    return unicode('%02d:%02d:%02d' % (hrs, mins, secs))
Created by Mike 'Fuzzy' Partin on Wed, 16 Jul 2014 (BSD)
Python recipes (4591)
Mike 'Fuzzy' Partin's recipes (12)

Required Modules

  • (none specified)

Other Information and Tasks