Skip to content

Updated .format() to f-strings #30027

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

Merged
merged 4 commits into from
Dec 5, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions pandas/io/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ def _parse_tfoot_tr(self, table):
def _setup_build_doc(self):
raw_text = _read(self.io)
if not raw_text:
raise ValueError("No text parsed from document: {doc}".format(doc=self.io))
raise ValueError(f"No text parsed from document: {self.io}")
return raw_text

def _build_doc(self):
Expand Down Expand Up @@ -616,8 +616,8 @@ def _build_xpath_expr(attrs) -> str:
if "class_" in attrs:
attrs["class"] = attrs.pop("class_")

s = [f"@{k}={repr(v)}" for k, v in attrs.items()]
return "[{expr}]".format(expr=" and ".join(s))
s = " and ".join([f"@{k}={repr(v)}" for k, v in attrs.items()])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you end up making another pass through this file, can you do this in two lines? i.e. keep s as it is now and do the join in a separate step

return f"[{s}]"


_re_namespace = {"re": "http://exslt.org/regular-expressions"}
Expand Down Expand Up @@ -846,7 +846,8 @@ def _parser_dispatch(flavor):


def _print_as_set(s) -> str:
return "{" + "{arg}".format(arg=", ".join(pprint_thing(el) for el in s)) + "}"
arg = ", ".join(pprint_thing(el) for el in s)
return f"{{{arg}}}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this adding a layer of braces?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you might be able to get rid of the pprint_thing, not sure

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The double brace is how to add a literal brace in an f-string, so looks OK



def _validate_flavor(flavor):
Expand All @@ -871,10 +872,8 @@ def _validate_flavor(flavor):

if not flavor_set & valid_flavors:
raise ValueError(
"{invalid} is not a valid set of flavors, valid "
"flavors are {valid}".format(
invalid=_print_as_set(flavor_set), valid=_print_as_set(valid_flavors)
)
f"{_print_as_set(flavor_set)} is not a valid set of flavors, valid "
f"flavors are {_print_as_set(valid_flavors)}"
)
return flavor

Expand All @@ -898,11 +897,11 @@ def _parse(flavor, io, match, attrs, encoding, displayed_only, **kwargs):
elif hasattr(io, "seekable") and not io.seekable():
# if we couldn't rewind it, let the user know
raise ValueError(
"The flavor {} failed to parse your input. "
f"The flavor {flav} failed to parse your input. "
"Since you passed a non-rewindable file "
"object, we can't rewind it to try "
"another parser. Try read_html() with a "
"different flavor.".format(flav)
"different flavor."
)

retained = caught
Expand Down