(fill, typespec='EeGgFfn%')
| 236 | # Generate random format strings |
| 237 | # [[fill]align][sign][#][0][width][.precision][type] |
| 238 | def rand_format(fill, typespec='EeGgFfn%'): |
| 239 | active = sorted(random.sample(range(7), random.randrange(8))) |
| 240 | have_align = 0 |
| 241 | s = '' |
| 242 | for elem in active: |
| 243 | if elem == 0: # fill+align |
| 244 | s += fill |
| 245 | s += random.choice('<>=^') |
| 246 | have_align = 1 |
| 247 | elif elem == 1: # sign |
| 248 | s += random.choice('+- ') |
| 249 | elif elem == 2 and not have_align: # zeropad |
| 250 | s += '0' |
| 251 | elif elem == 3: # width |
| 252 | s += str(random.randrange(1, 100)) |
| 253 | elif elem == 4: # thousands separator |
| 254 | s += ',' |
| 255 | elif elem == 5: # prec |
| 256 | s += '.' |
| 257 | s += str(random.randrange(100)) |
| 258 | elif elem == 6: |
| 259 | if 4 in active: c = typespec.replace('n', '') |
| 260 | else: c = typespec |
| 261 | s += random.choice(c) |
| 262 | return s |
| 263 | |
| 264 | # Partially brute force all possible format strings containing a thousands |
| 265 | # separator. Fall back to random where the runtime would become excessive. |
searching dependent graphs…