Feature or enhancement
Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
Links to previous discussion of this feature:
collections.namedtuple provides a couple useful helper functions, which are not present in PyStructSequence, its C API. Even though its documentation does not promise collections.namedtuple compatibility, it refers to it as a "named tuple".
|
The structseq helper is considered an internal CPython implementation |
|
detail. Docs for modules using structseqs should call them |
|
"named tuples" (be sure to include a space between the two |
|
words and add a link back to the term in Docs/glossary.rst). |
IMO, it'd be worth to increase compatibility between these two named tuple instances, and also make the extra functionality available in PyStructSequence.
Many times already, I have had the need for some of these helpers, with them being unavailable. An example use-case would be converting sys.version_info or sys.flags to a dictionary.
Here's a list of the collections.namedtuple helpers that are not available in PyStructSequence:
_fields
-
Tuple of strings listing the field names. Useful for introspection and for creating new named tuple types from existing named tuples
_asdict
-
Return a new dict which maps field names to their corresponding values.
_field_defaults (would return an empty dict, if implemented)
-
Tuple of strings listing the field names. Useful for introspection and for creating new named tuple types from existing named tuples.
_replace
-
Return a new instance of the named tuple replacing specified fields with new values:
_make
-
Class method that makes a new instance from an existing sequence or iterable.
Proposal:
>>> import sys
>>> sys.version_info._fields
('major', 'minor', 'micro', 'releaselevel', 'serial')
>>> sys.version_info._asdict()
{'major': 3, 'minor': 13, 'micro': 0, 'releaselevel': 'alpha', 'serial': 0}
>>> sys.flags._fields
('debug', 'inspect', 'interactive', 'optimize', 'dont_write_bytecode', 'no_user_site', 'no_site', 'ignore_environment', 'verbose', 'bytes_warning', 'quiet', 'hash_randomization', 'isolated', 'dev_mode', 'utf8_mode', 'warn_default_encoding', 'safe_path', 'int_max_str_digits')
>>> sys.flags._asdict()
{'debug': 0, 'inspect': 0, 'interactive': 0, 'optimize': 0, 'dont_write_bytecode': 0, 'no_user_site': 0, 'no_site': 0, 'ignore_environment': 0, 'verbose': 0, 'bytes_warning': 0, 'quiet': 0, 'hash_randomization': 1, 'isolated': 0, 'dev_mode': False, 'utf8_mode': 0, 'warn_default_encoding': 0, 'safe_path': False, 'int_max_str_digits': 4300}
Linked PRs
Feature or enhancement
Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
Links to previous discussion of this feature:
collections.namedtupleprovides a couple useful helper functions, which are not present inPyStructSequence, its C API. Even though its documentation does not promisecollections.namedtuplecompatibility, it refers to it as a "named tuple".cpython/Objects/structseq.c
Lines 4 to 7 in b62a760
IMO, it'd be worth to increase compatibility between these two named tuple instances, and also make the extra functionality available in
PyStructSequence.Many times already, I have had the need for some of these helpers, with them being unavailable. An example use-case would be converting
sys.version_infoorsys.flagsto a dictionary.Here's a list of the
collections.namedtuplehelpers that are not available inPyStructSequence:_fields_asdict_field_defaults(would return an empty dict, if implemented)_replace_makeProposal:
Linked PRs
PyStructSequencecompatible withnamedtuple#108648