1 | """ |
---|
2 | Number Helpers |
---|
3 | """ |
---|
4 | # Last synced with Rails copy at Revision 4537 on Aug 19th, 2006. |
---|
5 | import re |
---|
6 | |
---|
7 | def number_to_phone(number, area_code=False, delimiter="-", extension=""): |
---|
8 | """ |
---|
9 | Formats a ``number`` into a US phone number string. |
---|
10 | |
---|
11 | The area code can be surrounded by parentheses by setting ``area_code`` to True; default is False |
---|
12 | The delimiter can be set using ``delimiter`` default is "-" |
---|
13 | |
---|
14 | Examples:: |
---|
15 | |
---|
16 | >>> number_to_phone(1235551234) |
---|
17 | 123-555-1234 |
---|
18 | >>> number_to_phone(1235551234, area_code=True) |
---|
19 | (123) 555-1234 |
---|
20 | >>> number_to_phone(1235551234, delimiter=" ") |
---|
21 | 123 555 1234 |
---|
22 | >>> number_to_phone(1235551234, area_code=True, extension=555) |
---|
23 | (123) 555-1234 x 555 |
---|
24 | """ |
---|
25 | if area_code: |
---|
26 | number = re.sub(r'([0-9]{3})([0-9]{3})([0-9]{4})', r'(\1) \2%s\3' % delimiter, str(number)) |
---|
27 | else: |
---|
28 | number = re.sub(r'([0-9]{3})([0-9]{3})([0-9]{4})', r'\1%s\2%s\3' % (delimiter, delimiter), str(number)) |
---|
29 | if extension and str(extension).strip(): |
---|
30 | number += " x %s" % extension |
---|
31 | return number |
---|
32 | |
---|
33 | def number_to_currency(number, unit="$", precision=2, separator=".", delimiter=","): |
---|
34 | """ |
---|
35 | Formats a ``number`` into a currency string. |
---|
36 | |
---|
37 | ``number`` |
---|
38 | Indicates the level of precision |
---|
39 | ``unit`` |
---|
40 | Sets the currency type |
---|
41 | ``separator`` |
---|
42 | Used to set what the unit separation should be |
---|
43 | ``delimiter`` |
---|
44 | The delimiter can be set using the +delimiter+ key; default is "," |
---|
45 | |
---|
46 | Examples:: |
---|
47 | |
---|
48 | >>> number_to_currency(1234567890.50) |
---|
49 | $1,234,567,890.50 |
---|
50 | >>> number_to_currency(1234567890.506) |
---|
51 | $1,234,567,890.51 |
---|
52 | >>> number_to_currency(1234567890.50, unit="£", separator=",", delimiter="") |
---|
53 | £1234567890,50 |
---|
54 | """ |
---|
55 | if precision < 1: |
---|
56 | separator = "" |
---|
57 | parts = number_with_precision(number, precision).split('.') |
---|
58 | num = unit + number_with_delimiter(parts[0], delimiter) |
---|
59 | if len(parts) > 1: |
---|
60 | num += separator + parts[1] |
---|
61 | return num |
---|
62 | |
---|
63 | def number_to_percentage(number, precision=3, separator="."): |
---|
64 | """ |
---|
65 | Formats a ``number`` as into a percentage string. |
---|
66 | |
---|
67 | ``number`` |
---|
68 | Contains the level of precision |
---|
69 | ``separator`` |
---|
70 | The unit separator to be used |
---|
71 | |
---|
72 | Examples:: |
---|
73 | |
---|
74 | >>> number_to_percentage(100) |
---|
75 | 100.000% |
---|
76 | >>> number_to_percentage(100, precision=0) |
---|
77 | 100% |
---|
78 | >>> number_to_percentage(302.0574, precision=2) |
---|
79 | 302.06% |
---|
80 | """ |
---|
81 | number = number_with_precision(number, precision) |
---|
82 | parts = number.split('.') |
---|
83 | if len(parts) < 2: |
---|
84 | return parts[0] + "%" |
---|
85 | else: |
---|
86 | return parts[0] + separator + parts[1] + "%" |
---|
87 | |
---|
88 | def number_to_human_size(size): |
---|
89 | """ |
---|
90 | Returns a formatted-for-humans file size. |
---|
91 | |
---|
92 | Examples:: |
---|
93 | |
---|
94 | >>> number_to_human_size(123) |
---|
95 | 123 Bytes |
---|
96 | >>> number_to_human_size(1234) |
---|
97 | 1.2 KB |
---|
98 | >>> number_to_human_size(12345) |
---|
99 | 12.1 KB |
---|
100 | >>> number_to_human_size(1234567) |
---|
101 | 1.2 MB |
---|
102 | >>> number_to_human_size(1234567890) |
---|
103 | 1.1 GB |
---|
104 | """ |
---|
105 | if size == 1: |
---|
106 | return "1 Byte" |
---|
107 | elif size < 1024: |
---|
108 | return "%d Bytes" % size |
---|
109 | elif size < (1024**2): |
---|
110 | return "%.1f KB" % (size / 1024.00) |
---|
111 | elif size < (1024**3): |
---|
112 | return "%.1f MB" % (size / 1024.00**2) |
---|
113 | elif size < (1024**4): |
---|
114 | return "%.1f GB" % (size / 1024.00**3) |
---|
115 | elif size < (1024**5): |
---|
116 | return "%.1f TB" % (size / 1024.00**4) |
---|
117 | else: |
---|
118 | return "" |
---|
119 | |
---|
120 | human_size = number_to_human_size |
---|
121 | |
---|
122 | def number_with_delimiter(number, delimiter=","): |
---|
123 | """ |
---|
124 | Formats a ``number`` with a ``delimiter``. |
---|
125 | |
---|
126 | Example:: |
---|
127 | |
---|
128 | >>> number_with_delimiter(12345678) |
---|
129 | 12,345,678 |
---|
130 | """ |
---|
131 | return re.sub(r'(\d)(?=(\d\d\d)+(?!\d))', r'\1%s' % delimiter, str(number)) |
---|
132 | |
---|
133 | def number_with_precision(number, precision=3): |
---|
134 | """ |
---|
135 | Formats a ``number`` with a level of ``precision``. |
---|
136 | |
---|
137 | Example:: |
---|
138 | |
---|
139 | >>> number_with_precision(111.2345) |
---|
140 | 111.235 |
---|
141 | """ |
---|
142 | formstr = '%01.' + str(precision) + 'f' |
---|
143 | return formstr % number |
---|
144 | |
---|
145 | __all__ = ['number_to_phone', 'number_to_currency', 'number_to_percentage','number_with_delimiter', |
---|
146 | 'number_with_precision', 'number_to_human_size', 'human_size'] |
---|