Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32.3k
gh-134082: Modernize docstrings instring.Formatter
#134083
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes from1 commit
9ef4e56
9aa1a87
b3815b5
3a3de36
b2a33a7
ffc59ef
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
string.Formatter
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -264,22 +264,18 @@ def _vformat(self, format_string, args, kwargs, used_args, recursion_depth, | ||
return ''.join(result), auto_arg_index | ||
def get_value(self, key, args, kwargs): | ||
if isinstance(key, int): | ||
return args[key] | ||
else: | ||
return kwargs[key] | ||
def check_unused_args(self, used_args, args, kwargs): | ||
pass | ||
def format_field(self, value, format_spec): | ||
return format(value, format_spec) | ||
def convert_field(self, value, conversion): | ||
# do any conversion on the resulting object | ||
if conversion is None: | ||
@@ -292,34 +288,33 @@ def convert_field(self, value, conversion): | ||
return ascii(value) | ||
raise ValueError("Unknown conversion specifier {0!s}".format(conversion)) | ||
def parse(self, format_string): | ||
""" | ||
Return an iterable that contains tuples of the form | ||
(literal_text, field_name, format_spec, conversion). | ||
*literal_text* can be zero length and *field_name* | ||
can be None, in which case nothing is formatted. | ||
picnixz marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
If *field_name* is not None, it is looked up and | ||
formatted with *format_spec* and *conversion*. | ||
""" | ||
return _string.formatter_parser(format_string) | ||
def get_field(self, field_name, args, kwargs): | ||
"""Find the object referenced by a given field name. | ||
The field name *field_name* can be for instance "0.name" | ||
or "lookup[3]". The *args* and *kwargs* arguments are | ||
passed to get_value(). | ||
""" | ||
first, rest = _string.formatter_field_name_split(field_name) | ||
obj = self.get_value(first, args, kwargs) | ||
# loop through the rest of the field_name, doing | ||
# getattr or getitem as needed | ||
for is_attr, i in rest: | ||
if is_attr: | ||
obj = getattr(obj, i) | ||
else: | ||
obj = obj[i] | ||
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. why remove empty lines that provide «breathing space» and make it easy to follow the code? 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. Mmh, I shouldn't have removed those ones indeed. My bad. I however think that too many empty lines hinder the flow but I should have left them out in this case. I'll try to keep in mind. Note that from what I saw in more modern code, we tend to avoid too many blank lines in general except in long functions where logical sections are split. 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 agree with@picnixz. PEP 8 says to use a single blank line to separate methods and two blank lines to separate top-level functions and classes (there are also exceptions).https://peps.python.org/pep-0008/#blank-lines We don't usually make changes solely to meet PEP 8 requirements, but in this case I think it was justified. | ||
return obj, first |
Uh oh!
There was an error while loading.Please reload this page.