Convert a localeconv-style grouping into a (possibly infinite) iterable of integers representing group lengths.
(grouping)
| 6293 | return result |
| 6294 | |
| 6295 | def _group_lengths(grouping): |
| 6296 | """Convert a localeconv-style grouping into a (possibly infinite) |
| 6297 | iterable of integers representing group lengths. |
| 6298 | |
| 6299 | """ |
| 6300 | # The result from localeconv()['grouping'], and the input to this |
| 6301 | # function, should be a list of integers in one of the |
| 6302 | # following three forms: |
| 6303 | # |
| 6304 | # (1) an empty list, or |
| 6305 | # (2) nonempty list of positive integers + [0] |
| 6306 | # (3) list of positive integers + [locale.CHAR_MAX], or |
| 6307 | |
| 6308 | from itertools import chain, repeat |
| 6309 | if not grouping: |
| 6310 | return [] |
| 6311 | elif grouping[-1] == 0 and len(grouping) >= 2: |
| 6312 | return chain(grouping[:-1], repeat(grouping[-2])) |
| 6313 | elif grouping[-1] == _locale.CHAR_MAX: |
| 6314 | return grouping[:-1] |
| 6315 | else: |
| 6316 | raise ValueError('unrecognised format for grouping') |
| 6317 | |
| 6318 | def _insert_thousands_sep(digits, spec, min_width=1): |
| 6319 | """Insert thousands separators into a digit string. |
no test coverage detected