字串 format

帶入 dict 的值, 如果用 format api, dictionary 要帶 ** 去展開 key-value
>>> m = {'a':1.1234, "b":"qq"}
>>> '%(a)1.1f %(a)s %(b)s' %m
'1.1 1.1234 qq'


>>> '{a:1.1f} {a:s} {b:s}'.format(m) # illegal, must use **m

Traceback (most recent call last):
  File "", line 1, in 
    '{a:1.1f} {a:s} {b:s}'.format(m)
KeyError: 'a'

>>> '{a:1.1f} {a:s} {b:s}'.format(**m) # illegal, a is float type

Traceback (most recent call last):
  File "", line 1, in 
    '{a:1.1f} {a:s} {b:s}'.format(**m)
ValueError: Unknown format code 's' for object of type 'float'

>>> '{a:1.1f} {b:s}'.format(**m)
'1.1 qq'

沒有留言:

張貼留言

Lessons Learned While Benchmarking vLLM with GPU

Recently, I benchmarked vLLM on a GPU to better understand how much throughput can realistically be expected in an LLM serving setup. One ...