Convert a localeconv-style grouping into a (possibly infinite) iterable of integers representing group lengths.
(grouping)
| 6267 | return result |
| 6268 | |
| 6269 | def _group_lengths(grouping): |
| 6270 | """Convert a localeconv-style grouping into a (possibly infinite) |
| 6271 | iterable of integers representing group lengths. |
| 6272 | |
| 6273 | """ |
| 6274 | # The result from localeconv()['grouping'], and the input to this |
| 6275 | # function, should be a list of integers in one of the |
| 6276 | # following three forms: |
| 6277 | # |
| 6278 | # (1) an empty list, or |
| 6279 | # (2) nonempty list of positive integers + [0] |
| 6280 | # (3) list of positive integers + [locale.CHAR_MAX], or |
| 6281 | |
| 6282 | from itertools import chain, repeat |
| 6283 | if not grouping: |
| 6284 | return [] |
| 6285 | elif grouping[-1] == 0 and len(grouping) >= 2: |
| 6286 | return chain(grouping[:-1], repeat(grouping[-2])) |
| 6287 | elif grouping[-1] == _locale.CHAR_MAX: |
| 6288 | return grouping[:-1] |
| 6289 | else: |
| 6290 | raise ValueError('unrecognised format for grouping') |
| 6291 | |
| 6292 | def _insert_thousands_sep(digits, spec, min_width=1): |
| 6293 | """Insert thousands separators into a digit string. |
no test coverage detected
searching dependent graphs…