Skip to content

Add a new 'css' format, which further abbreviates base 1000 suffixes #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ Default base if it is not clear what the unit is (i.e. if it is not 'mib' or 'me
- format type: `[hH][size_format][^exponent]`
- `h`: Base 1000
- `H`: Base 1024
- `size_format`: `c | cs | cv | e | ev | s | sv`
- `size_format`: `c | cs | css | cv | e | ev | s | sv`
- `c`: Commonly used case-sensitive suffixes
- `cs`: Commonly used abbreviated case-sensitive suffixes
- `css`: Similar to `cs` but even more abbreviated `Base 1000` (e.g. `k` instead of ` kb`)
- `cv`: Commonly used verbose case-sensitive suffixes
- `e`: IEC suffixes
- `ev`: IEC verbose suffixes
Expand Down
30 changes: 28 additions & 2 deletions hfilesize/hfilesize.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,31 @@ class Format:
],
}

casing_shorter = {
1024: [
'',
'K',
'M',
'G',
'T',
'P',
'E',
'Z',
'Y',
],
1000: [
'',
'k',
'm',
'g',
't',
'p',
'e',
'z',
'y',
],
}

casing_verbose = {
1024: [
(' byte', ' bytes'),
Expand Down Expand Up @@ -281,21 +306,22 @@ def __format__(self, fmt):
'''
format specification:
format type: [hH][size_format][^exponent]
size_format: c | cs | cv | e | ev | s | sv
size_format: c | cs | css | cv | e | ev | s | sv
exponent: integer

base is required sometimes if no exponent is specified
always specifying the base gives a shorter format specification
'''
# is it an empty format or not a special format for the size class
matches = re.search(r'([hH])(?:(c|cs|cv|e|ev|s|sv)?(?:\^(\d+))?)?$', fmt)
matches = re.search(r'([hH])(?:(c|cs|css|cv|e|ev|s|sv)?(?:\^(\d+))?)?$', fmt)
if not matches:
return int(self).__format__(fmt)
fmt_type, size_fmt, exponent = matches.groups()
size_fmt = {
None: Format.casing,
'c': Format.casing,
'cs': Format.casing_short,
'css': Format.casing_shorter,
'cv': Format.casing_verbose,
'e': Format.iec,
'ev': Format.iec_verbose,
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

setup(
name='hfilesize',
version='0.1.0',
version='0.2.0',
license='MIT',
description='Human Readable File Sizes',
long_description=long_description,
Expand Down