Skip to content

Commit 3db428c

Browse files
committed
Add a new 'css' format, which further abbreviates base 1000 suffixes (e.g. 'k' instead of ' kb')
1 parent 0cf08b4 commit 3db428c

3 files changed

Lines changed: 40 additions & 13 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@ Default base if it is not clear what the unit is (i.e. if it is not 'mib' or 'me
4848
- format type: `[hH][size_format][^exponent]`
4949
- `h`: Base 1000
5050
- `H`: Base 1024
51-
- `size_format`: `c | cs | cv | e | ev | s | sv`
51+
- `size_format`: `c | cs | css | cv | e | ev | s | sv`
5252
- `c`: Commonly used case-sensitive suffixes
5353
- `cs`: Commonly used abbreviated case-sensitive suffixes
54+
- `css`: Similar to `cs` but even more abbreviated `Base 1000` (e.g. `k` instead of ` kb`)
5455
- `cv`: Commonly used verbose case-sensitive suffixes
5556
- `e`: IEC suffixes
5657
- `ev`: IEC verbose suffixes

hfilesize/hfilesize.py

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,31 @@ class Format:
5959
],
6060
}
6161

62+
casing_shorter = {
63+
1024: [
64+
'',
65+
'K',
66+
'M',
67+
'G',
68+
'T',
69+
'P',
70+
'E',
71+
'Z',
72+
'Y',
73+
],
74+
1000: [
75+
'',
76+
'k',
77+
'm',
78+
'g',
79+
't',
80+
'p',
81+
'e',
82+
'z',
83+
'y',
84+
],
85+
}
86+
6287
casing_verbose = {
6388
1024: [
6489
(' byte', ' bytes'),
@@ -284,26 +309,27 @@ def __format__(self, fmt):
284309
'''
285310
format specification:
286311
format type: [hH][size_format][^exponent]
287-
size_format: c | cs | cv | e | ev | s | sv
288-
exponent: integer
312+
size_format: c | cs | css | cv | e | ev | s | sv
313+
exponent: integer
289314
290315
base is required sometimes if no exponent is specified
291316
always specifying the base gives a shorter format specification
292317
'''
293318
# is it an empty format or not a special format for the size class
294-
matches = re.search(r'([hH])(?:(c|cs|cv|e|ev|s|sv)?(?:\^(\d+))?)?$', fmt)
319+
matches = re.search(r'([hH])(?:(c|cs|css|cv|e|ev|s|sv)?(?:\^(\d+))?)?$', fmt)
295320
if not matches:
296321
return int(self).__format__(fmt)
297322
fmt_type, size_fmt, exponent = matches.groups()
298323
size_fmt = {
299-
None: Format.casing,
300-
'c': Format.casing,
301-
'cs': Format.casing_short,
302-
'cv': Format.casing_verbose,
303-
'e': Format.iec,
304-
'ev': Format.iec_verbose,
305-
's': Format.si,
306-
'sv': Format.si_verbose,
324+
None: Format.casing,
325+
'c': Format.casing,
326+
'cs': Format.casing_short,
327+
'css': Format.casing_shorter,
328+
'cv': Format.casing_verbose,
329+
'e': Format.iec,
330+
'ev': Format.iec_verbose,
331+
's': Format.si,
332+
'sv': Format.si_verbose,
307333
}[size_fmt]
308334
if fmt_type=='h':
309335
base = 1000

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
setup(
1818
name='hfilesize',
19-
version='0.1.0',
19+
version='0.2.0',
2020
license='GPLv3',
2121
description='Human Readable File Sizes',
2222
long_description=long_description,

0 commit comments

Comments
 (0)