A collection of advanced code for the Python 3.x built-in function vars
.
Since the return value of vars
is a dictionary, it can be copied with ʻitems () and dictionary comprehension. The element of ʻitems ()
(here ʻitem`) is a tuple of keys and values.
import sys
from pprint import pp
dict1 = {item[0]: item[1] for item in vars(sys).items()}
pp(dict1)
output
{'__name__': 'sys',
'__doc__': 'This module provides access to some objects ...,
...
Note: The return value of the vars
built-in function is dictionary type (<class'dict'>
). You must use vars (sys) .items ()
to enumerate the elements (tuples of keys and values). When for item in vars (sys)
is set, it is treated as vars (sys) .keys ()
, and a dictionary with the first character of the key as the key and the second character as the value is created.
import sys
from pprint import pp
dict1 = {item[0]: type(item[1]) for item in vars(sys).items()}
pp(dict1)
output
{'__name__': <class 'str'>,
'__doc__': <class 'str'>,
'__package__': <class 'str'>,
'__loader__': <class 'type'>,
...
import sys
from pprint import pp
set1 = {type(value).__name__ for value in vars(sys).values()}
pp(set1)
output
{'ModuleSpec',
'NoneType',
'SimpleNamespace',
'TextIOWrapper',
'bool',
'builtin_function_or_method',
'dict',
'flags',
'float_info',
'function',
'hash_info',
'int',
'int_info',
'list',
'str',
'thread_info',
'tuple',
'type',
'version_info'}
--The type name can be obtained with type (...) .__ name__
.
--{e for e in v}
is a set comprehension.
import sys
from collections import Counter
from pprint import pp
counter = Counter((type(value).__name__ for value in vars(sys).values()))
pp(counter)
output
Counter({'builtin_function_or_method': 42,
'str': 16,
'TextIOWrapper': 6,
'int': 5,
'list': 5,
'dict': 3,
'tuple': 2,
'NoneType': 2,
'type': 1,
'ModuleSpec': 1,
'float_info': 1,
'int_info': 1,
'hash_info': 1,
'version_info': 1,
'SimpleNamespace': 1,
'flags': 1,
'thread_info': 1,
'bool': 1,
'function': 1})
import sys
import types
from pprint import pp
dict1 = {item[0]: item[1] for item in vars(sys).items()
if isinstance(item[1], types.BuiltinFunctionType)}
pp(dict1)
output
{'addaudithook': <built-in function addaudithook>,
'audit': <built-in function audit>,
'breakpointhook': <built-in function breakpointhook>,
'callstats': <built-in function callstats>,
'_clear_type_cache': <built-in function _clear_type_cache>,
...
import sys
import types
from pprint import pp
dict1 = {item[0]: item[1] for item in vars(sys).items()
if not isinstance(item[1], types.BuiltinFunctionType)}
pp(dict1)
output
{'__name__': 'sys',
'__doc__': 'This module provides ...',
'__package__': '',
'__loader__': <class '_frozen_importlib.BuiltinImporter'>,
'__spec__': ModuleSpec(name='sys', loader=<class '_frozen_importlib.BuiltinImporter'>),
...
import sys
import types
from pprint import pp
from typing import Optional
def str_truncate_singleline(s, width: int, linesep: Optional[str]):
s = str(s)
if not isinstance(width, int):
raise TypeError()
addsEllipsis: bool = False
if linesep is not None:
i: int = s.find(linesep)
if i != -1:
s = s[0:i]
addsEllipsis = True
if len(s) + len("...") > width:
return s[0:width-len("...")] + "..."
return s + "..." if addsEllipsis else s
dict1 = {item[0]: str_truncate_singleline(item[1], 50, "\n")
for item in vars(sys).items()
if not isinstance(item[1], types.BuiltinFunctionType)}
pp(dict1)
Recommended Posts