def test_parse_kwargs_string(self): self.dict_equal( _parse_kwargs_string('year=1950 ; style=jazz', year=int, style=str), {'year': 1950, 'style': 'jazz'}) self.dict_equal( _parse_kwargs_string(' like_it=True ', like_it=lambda x: x == 'True'), {'like_it': True})
def test_parse_kwargs_string(): assert _parse_kwargs_string('year=1950 ; style=jazz', year=int, style=str) == { 'year': 1950, 'style': 'jazz' } assert _parse_kwargs_string(' like_it=True ', like_it=lambda x: x == 'True') == { 'like_it': True }
def test_parse_kwargs_string(self): self.dict_equal( _parse_kwargs_string('year=1950 ; style=jazz', year=int, style=str), { 'year': 1950, 'style': 'jazz' }) self.dict_equal( _parse_kwargs_string(' like_it=True ', like_it=lambda x: x == 'True'), {'like_it': True})
def naturalbitrate(bps, kwargs_string=None): """ Return a human readable representation of a bit rate taking `bps` as the rate in bits/s. See documentation of :func:`pytoolbox.humanize.naturalbitrate` for further examples. Output:: 16487211.33|naturalbitrate -> 16.5 Mb/s 16487211.33|naturalbitrate:'format={value:.0f} {unit}' -> 16 Mb/s 16487211.33|naturalbitrate:'format={sign}{value:.0f} {unit}; scale=1' -> 16487 kb/s -16487211.33|naturalbitrate:'format={sign}{value:.0f} {unit}' -> -16 Mb/s None|naturalbitrate -> (empty string) (empty string)|naturalbitrate -> (empty string) """ if bps in (None, string_if_invalid): return string_if_invalid return humanize.naturalbitrate( bps, **_parse_kwargs_string(kwargs_string, format=str, scale=int))
def naturalfilesize(the_bytes, kwargs_string=None): """ Return a human readable representation of a *file* size taking `the_bytes` as the size in bytes. See documentation of :func:`pytoolbox.humanize.naturalfilesize` for further examples. Output:: 16487211.33|naturalfilesize -> 15.7 MB 16487211.33|naturalfilesize:'system=si' -> 16.5 MiB 16487211.33|naturalfilesize:'format={value:.0f} {unit}; system=gnu' -> 16 M 16487211.33|naturalfilesize:'format={sign}{value:.0f} {unit}; scale=1' -> 16101 kB -16487211.33|naturalfilesize:'format={sign}{value:.0f} {unit}' -> -16 MB None|naturalfilesize -> (empty string) (empty string)|naturalfilesize -> (empty string) """ if the_bytes in (None, string_if_invalid): return string_if_invalid return humanize.naturalfilesize( the_bytes, **_parse_kwargs_string(kwargs_string, format=str, scale=int, system=str))
def test_parse_kwargs_string_value_error(self): with self.raises(ValueError): _parse_kwargs_string(' a_number=yeah', a_number=int)
def test_parse_kwargs_string_key_error(self): with self.raises(KeyError): _parse_kwargs_string(' pi=3.1416; ru=2', pi=float)