Lines Matching full:format
44 format_string = "%.{}f".format(INT_MAX + 1)
49 format_string = "%{}f".format(sys.maxsize + 1)
128 def test(value, format, expected): argument
130 self.assertEqual(value.__format__(format), expected)
131 self.assertEqual(value.__format__(format + 's'), expected)
160 self.assertEqual(''.format(), '')
161 self.assertEqual('a'.format(), 'a')
162 self.assertEqual('ab'.format(), 'ab')
163 self.assertEqual('a{{'.format(), 'a{')
164 self.assertEqual('a}}'.format(), 'a}')
165 self.assertEqual('{{b'.format(), '{b')
166 self.assertEqual('}}b'.format(), '}b')
167 self.assertEqual('a{{b'.format(), 'a{b')
171 self.assertEqual("My name is {0}".format('Fred'), "My name is Fred")
172 self.assertEqual("My name is {0[name]}".format(dict(name='Fred')),
174 self.assertEqual("My name is {0} :-{{}}".format('Fred'),
178 self.assertEqual("The year is {0.year}".format(d),
233 self.assertEqual(''.format(), '')
234 self.assertEqual('abc'.format(), 'abc')
235 self.assertEqual('{0}'.format('abc'), 'abc')
236 self.assertEqual('{0:}'.format('abc'), 'abc')
237 self.assertEqual('X{0}'.format('abc'), 'Xabc')
238 self.assertEqual('{0}X'.format('abc'), 'abcX')
239 self.assertEqual('X{0}Y'.format('abc'), 'XabcY')
240 self.assertEqual('{1}'.format(1, 'abc'), 'abc')
241 self.assertEqual('X{1}'.format(1, 'abc'), 'Xabc')
242 self.assertEqual('{1}X'.format(1, 'abc'), 'abcX')
243 self.assertEqual('X{1}Y'.format(1, 'abc'), 'XabcY')
244 self.assertEqual('{0}'.format(-15), '-15')
245 self.assertEqual('{0}{1}'.format(-15, 'abc'), '-15abc')
246 self.assertEqual('{0}X{1}'.format(-15, 'abc'), '-15Xabc')
247 self.assertEqual('{{'.format(), '{')
248 self.assertEqual('}}'.format(), '}')
249 self.assertEqual('{{}}'.format(), '{}')
250 self.assertEqual('{{x}}'.format(), '{x}')
251 self.assertEqual('{{{0}}}'.format(123), '{123}')
252 self.assertEqual('{{{{0}}}}'.format(), '{{0}}')
253 self.assertEqual('}}{{'.format(), '}{')
254 self.assertEqual('}}x{{'.format(), '}x{')
257 self.assertEqual("{0[foo-bar]}".format({'foo-bar':'baz'}), 'baz')
258 self.assertEqual("{0[foo bar]}".format({'foo bar':'baz'}), 'baz')
259 self.assertEqual("{0[ ]}".format({' ':3}), '3')
261 self.assertEqual('{foo._x}'.format(foo=C(20)), '20')
262 self.assertEqual('{1}{0}'.format(D(10), D(20)), '2010')
263 self.assertEqual('{0._x.x}'.format(C(D('abc'))), 'abc')
264 self.assertEqual('{0[0]}'.format(['abc', 'def']), 'abc')
265 self.assertEqual('{0[1]}'.format(['abc', 'def']), 'def')
266 self.assertEqual('{0[1][0]}'.format(['abc', ['def']]), 'def')
267 self.assertEqual('{0[1][0].x}'.format(['abc', [D('def')]]), 'def')
270 self.assertEqual('{0:.3s}'.format('abc'), 'abc')
271 self.assertEqual('{0:.3s}'.format('ab'), 'ab')
272 self.assertEqual('{0:.3s}'.format('abcdef'), 'abc')
273 self.assertEqual('{0:.0s}'.format('abcdef'), '')
274 self.assertEqual('{0:3.3s}'.format('abc'), 'abc')
275 self.assertEqual('{0:2.3s}'.format('abc'), 'abc')
276 self.assertEqual('{0:2.2s}'.format('abc'), 'ab')
277 self.assertEqual('{0:3.2s}'.format('abc'), 'ab ')
278 self.assertEqual('{0:x<0s}'.format('result'), 'result')
279 self.assertEqual('{0:x<5s}'.format('result'), 'result')
280 self.assertEqual('{0:x<6s}'.format('result'), 'result')
281 self.assertEqual('{0:x<7s}'.format('result'), 'resultx')
282 self.assertEqual('{0:x<8s}'.format('result'), 'resultxx')
283 self.assertEqual('{0: <7s}'.format('result'), 'result ')
284 self.assertEqual('{0:<7s}'.format('result'), 'result ')
285 self.assertEqual('{0:>7s}'.format('result'), ' result')
286 self.assertEqual('{0:>8s}'.format('result'), ' result')
287 self.assertEqual('{0:^8s}'.format('result'), ' result ')
288 self.assertEqual('{0:^9s}'.format('result'), ' result ')
289 self.assertEqual('{0:^10s}'.format('result'), ' result ')
290 self.assertEqual('{0:10000}'.format('a'), 'a' + ' ' * 9999)
291 self.assertEqual('{0:10000}'.format(''), ' ' * 10000)
292 self.assertEqual('{0:10000000}'.format(''), ' ' * 10000000)
294 # format specifiers for user defined type
295 self.assertEqual('{0:abc}'.format(C()), 'abc')
298 self.assertEqual('{0!s}'.format('Hello'), 'Hello')
299 self.assertEqual('{0!s:}'.format('Hello'), 'Hello')
300 self.assertEqual('{0!s:15}'.format('Hello'), 'Hello ')
301 self.assertEqual('{0!s:15s}'.format('Hello'), 'Hello ')
302 self.assertEqual('{0!r}'.format('Hello'), "'Hello'")
303 self.assertEqual('{0!r:}'.format('Hello'), "'Hello'")
304 self.assertEqual('{0!r}'.format(F('Hello')), 'F(Hello)')
307 self.assertEqual('{0}'.format({}), '{}')
308 self.assertEqual('{0}'.format([]), '[]')
309 self.assertEqual('{0}'.format([1]), '[1]')
310 self.assertEqual('{0}'.format(E('data')), 'E(data)')
311 self.assertEqual('{0:d}'.format(G('data')), 'G(data)')
312 self.assertEqual('{0!s}'.format(G('data')), 'string is data')
314 msg = 'object.__format__ with a non-empty format string is deprecated'
316 self.assertEqual('{0:^10}'.format(E('data')), ' E(data) ')
317 self.assertEqual('{0:^10s}'.format(E('data')), ' E(data) ')
318 self.assertEqual('{0:>15s}'.format(G('data')), ' string is data')
320 self.assertEqual("{0:date: %Y-%m-%d}".format(I(year=2007,
326 self.assertEqual("{0}".format(J(10)), "20")
329 # string format specifiers
330 self.assertEqual('{0:}'.format('a'), 'a')
332 # computed format specifiers
333 self.assertEqual("{0:.{1}}".format('hello world', 5), 'hello')
334 self.assertEqual("{0:.{1}s}".format('hello world', 5), 'hello')
335 self.assertEqual("{0:.{precision}s}".format('hello world', precision=5), 'hello')
336 …self.assertEqual("{0:{width}.{precision}s}".format('hello world', width=10, precision=5), 'hello …
337 …self.assertEqual("{0:{width}.{precision}s}".format('hello world', width='10', precision='5'), 'hel…
340 self.assertRaises(ValueError, '{'.format)
341 self.assertRaises(ValueError, '}'.format)
342 self.assertRaises(ValueError, 'a{'.format)
343 self.assertRaises(ValueError, 'a}'.format)
344 self.assertRaises(ValueError, '{a'.format)
345 self.assertRaises(ValueError, '}a'.format)
346 self.assertRaises(IndexError, '{0}'.format)
347 self.assertRaises(IndexError, '{1}'.format, 'abc')
348 self.assertRaises(KeyError, '{x}'.format)
349 self.assertRaises(ValueError, "}{".format)
350 self.assertRaises(ValueError, "{".format)
351 self.assertRaises(ValueError, "}".format)
352 self.assertRaises(ValueError, "abc{0:{}".format)
353 self.assertRaises(ValueError, "{0".format)
354 self.assertRaises(IndexError, "{0.}".format)
355 self.assertRaises(ValueError, "{0.}".format, 0)
356 self.assertRaises(IndexError, "{0[}".format)
357 self.assertRaises(ValueError, "{0[}".format, [])
358 self.assertRaises(KeyError, "{0]}".format)
359 self.assertRaises(ValueError, "{0.[]}".format, 0)
360 self.assertRaises(ValueError, "{0..foo}".format, 0)
361 self.assertRaises(ValueError, "{0[0}".format, 0)
362 self.assertRaises(ValueError, "{0[0:foo}".format, 0)
363 self.assertRaises(KeyError, "{c]}".format)
364 self.assertRaises(ValueError, "{{ {{{0}}".format, 0)
365 self.assertRaises(ValueError, "{0}}".format, 0)
366 self.assertRaises(KeyError, "{foo}".format, bar=3)
367 self.assertRaises(ValueError, "{0!x}".format, 3)
368 self.assertRaises(ValueError, "{0!}".format, 0)
369 self.assertRaises(ValueError, "{0!rs}".format, 0)
370 self.assertRaises(ValueError, "{!}".format)
371 self.assertRaises(IndexError, "{:}".format)
372 self.assertRaises(IndexError, "{:s}".format)
373 self.assertRaises(IndexError, "{}".format)
376 self.assertRaises(ValueError, "{0[0]x}".format, [None])
377 self.assertRaises(ValueError, "{0[0](10)}".format, [None])
380 self.assertRaises(TypeError, '{0[{1}]}'.format, 'abcdefg', 4)
383 self.assertRaises(ValueError, "{0:{1:{2}}}".format, 'abc', 's', '')
384 self.assertRaises(ValueError, "{0:{1:{2:{3:{4:{5:{6}}}}}}}".format,
387 # string format spec errors
388 self.assertRaises(ValueError, "{0:-s}".format, '')
389 self.assertRaises(ValueError, format, "", "-")
390 self.assertRaises(ValueError, "{0:=s}".format, '')
393 format_string = ".{}f".format(sys.maxsize + 1)
395 result = format(2.34, format_string)
398 format_string = "{}f".format(sys.maxsize + 1)
400 result = format(2.34, format_string)
403 format_string = "{{{}:.6f}}".format(sys.maxsize + 1)
405 result = format_string.format(2.34)
414 self.assertEqual('{}'.format(10), '10')
415 self.assertEqual('{:5}'.format('s'), 's ')
416 self.assertEqual('{!r}'.format('s'), "'s'")
417 self.assertEqual('{._x}'.format(C(10)), '10')
418 self.assertEqual('{[1]}'.format([1, 2]), '2')
419 self.assertEqual('{[a]}'.format({'a':4, 'b':2}), '4')
420 self.assertEqual('a{}b{}c'.format(0, 1), 'a0b1c')
422 self.assertEqual('a{:{}}b'.format('x', '^10'), 'a x b')
423 self.assertEqual('a{:{}x}b'.format(20, '#'), 'a0x14b')
426 self.assertRaises(ValueError, '{}{1}'.format, 1, 2)
427 self.assertRaises(ValueError, '{1}{}'.format, 1, 2)
428 self.assertRaises(ValueError, '{:{1}}'.format, 1, 2)
429 self.assertRaises(ValueError, '{0:{}}'.format, 1, 2)
432 self.assertEqual('{f}{}'.format(4, f='test'), 'test4')
433 self.assertEqual('{}{f}'.format(4, f='test'), '4test')
434 self.assertEqual('{:{f}}{g}{}'.format(1, 3, g='g', f=2), ' 1g3')
435 self.assertEqual('{f:{}}{}{g}'.format(2, 4, f=1, g='g'), ' 14g')
439 self.assertRaises(OverflowError, '{0:c}'.format, -1)
440 self.assertRaises(OverflowError, '{0:c}'.format, 256)
473 return 'Success, self.__rmod__({!r}) was called'.format(other)