MCPcopy Index your code
hub / github.com/python/cpython / rand_format

Function rand_format

Modules/_decimal/tests/formathelper.py:238–262  ·  view source on GitHub ↗
(fill, typespec='EeGgFfn%')

Source from the content-addressed store, hash-verified

236# Generate random format strings
237# [[fill]align][sign][#][0][width][.precision][type]
238def 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.

Callers 1

test_formatFunction · 0.90

Calls 5

strFunction · 0.85
randrangeMethod · 0.80
choiceMethod · 0.80
sampleMethod · 0.45
replaceMethod · 0.45

Tested by 1

test_formatFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…