-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
gh-127011: Add __str__ and __repr__ to ConfigParser #127014
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 10 commits
b3ce3f3
d603aa8
91db566
7f3633d
5cd1d1f
5255025
dc71b39
c59d4fa
d5e944f
0883b52
d76eec4
433432b
acd31ae
43b7ff3
79e4892
913c2a4
e60ffe4
0ed179a
726b152
70fb9a5
38809c9
081c340
50838d5
2560eb9
cb53ff2
155b8ef
1a03c72
acfa969
8415943
ee219a6
63d58e0
9b107e2
50fdb2b
6427a0d
d547497
3817877
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -682,6 +682,7 @@ def __init__(self, defaults=None, dict_type=_default_dict, | |
| if defaults: | ||
| self._read_defaults(defaults) | ||
| self._allow_unnamed_section = allow_unnamed_section | ||
| self._loaded_files = [] | ||
|
|
||
| def defaults(self): | ||
| return self._defaults | ||
|
|
@@ -750,6 +751,7 @@ def read(self, filenames, encoding=None): | |
| if isinstance(filename, os.PathLike): | ||
| filename = os.fspath(filename) | ||
| read_ok.append(filename) | ||
| self._loaded_files.extend(read_ok) | ||
| return read_ok | ||
|
|
||
| def read_file(self, f, source=None): | ||
|
|
@@ -1040,6 +1042,35 @@ def __iter__(self): | |
| # XXX does it break when underlying container state changed? | ||
| return itertools.chain((self.default_section,), self._sections.keys()) | ||
|
|
||
| def __str__(self): | ||
| config_dict = { | ||
| section: {key: value for key, value in self.items(section, raw=True)} | ||
| for section in self.sections() | ||
| } | ||
| return str(config_dict) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this implementation leaves too much risk for confusion. With this, you can have Personally, I don't see the benefit in defining
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @brianschubert Based on your input, I plan to make it fallback to repr for string representations and add a new API, to_dict, which could be useful for scenarios like dumping the configuration into JSON and several other use cases. I’m waiting for others to agree with you before proceeding.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Leave a new API to another PR, let's just focus on IMO, recommending
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
exactly.
Done
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @ZeroIntensity , should I create an issue for adding to_dict ? |
||
|
|
||
|
|
||
| def __repr__(self): | ||
| init_params = { | ||
| "defaults": self._defaults if self._defaults else None, | ||
| "dict_type": type(self._dict).__name__, | ||
| "allow_no_value": self._allow_no_value, | ||
| "delimiters": self._delimiters, | ||
| "strict": self._strict, | ||
| "default_section": self.default_section, | ||
| "interpolation": type(self._interpolation).__name__, | ||
| } | ||
| init_params = {k: v for k, v in init_params.items() if v is not None} | ||
|
|
||
| state_summary = { | ||
| "loaded_files": self._loaded_files if hasattr(self, '_loaded_files') else "(no files loaded)", | ||
|
Agent-Hellboy marked this conversation as resolved.
Outdated
|
||
| "sections": len(self._sections), | ||
| } | ||
|
|
||
| return (f"<{self.__class__.__name__}(" | ||
| f"params={init_params}, " | ||
| f"state={state_summary})>") | ||
|
|
||
| def _read(self, fp, fpname): | ||
| """Parse a sectioned configuration file. | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| The ``__str__`` and ``__repr__`` methods have been added to the :class:`configparser.RawConfigParser` |
Uh oh!
There was an error while loading. Please reload this page.