Skip to content
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

CLN: String formatting % -> f-strings #29518

Merged
merged 11 commits into from
Nov 18, 2019
Merged

CLN: String formatting % -> f-strings #29518

merged 11 commits into from
Nov 18, 2019

Conversation

alimcmaster1
Copy link
Member

@alimcmaster1 alimcmaster1 commented Nov 10, 2019

Old style % formatting to f-strings

@@ -600,7 +600,7 @@ def _format_native_types(self, na_rep="NaT", date_format=None, **kwargs):
if date_format:
formatter = lambda dt: dt.strftime(date_format)
else:
formatter = lambda dt: "%s" % dt
formatter = lambda dt: f"{dt}"
Copy link
Member

Choose a reason for hiding this comment

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

would str(dt) be clearer here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure - agree

@@ -84,7 +84,7 @@ def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False):
return None
else:
if verbose:
print("unable to find command, tried %s" % (commands,))
Copy link
Member

Choose a reason for hiding this comment

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

i think this file is auto-generated

Copy link
Member Author

Choose a reason for hiding this comment

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

reverted

@@ -2187,7 +2187,7 @@ def to_parquet(
compression=compression,
index=index,
partition_cols=partition_cols,
**kwargs
**kwargs,
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 a black version thing?

Copy link
Member Author

Choose a reason for hiding this comment

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

Weirdly using 19.3b0 locally seems to add this for me - I've reverted

Copy link
Member Author

@alimcmaster1 alimcmaster1 Nov 11, 2019

Choose a reason for hiding this comment

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

Black code check in CI fail without this - i've added this back in

Copy link
Member

Choose a reason for hiding this comment

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

Personally, I find this quite odd that you have to add a comma at the end.

Copy link
Member Author

Choose a reason for hiding this comment

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

Related to this - #29607

@jbrockmendel
Copy link
Member

looks like black complaints

@alimcmaster1
Copy link
Member Author

@jbrockmendel - ready to re-review whenever you have a moment

@@ -960,14 +960,13 @@ def __repr__(self):
data = self._format_data()
attrs = self._format_attrs()
space = self._format_space()

prepr = (",%s" % space).join("%s=%s" % (k, v) for k, v in attrs)
prepr = f",{space}".join(f"{k}={v}" for k, v in attrs)
Copy link
Member

Choose a reason for hiding this comment

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

let's try to minimize what we execute in f-space (better name for this?)

@@ -1121,13 +1120,13 @@ def _summary(self, name=None):
tail = self[-1]
if hasattr(tail, "format") and not isinstance(tail, str):
tail = tail.format()
index_summary = ", %s to %s" % (pprint_thing(head), pprint_thing(tail))
index_summary = f", {pprint_thing(head)} to {pprint_thing(tail)}"
Copy link
Member

Choose a reason for hiding this comment

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

pprint_thing may be unnecessary here now that we're py3-only. can you take a look

pandas/core/series.py Outdated Show resolved Hide resolved
@@ -429,7 +429,7 @@ def __call__(self):
).format(estimate=estimate, dmin=dmin, dmax=dmax, arg=self.MAXTICKS * 2)
)

freq = "%dL" % self._get_interval()
freq = f"{self._get_interval()}L"
Copy link
Member

Choose a reason for hiding this comment

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

call get_intterval outside

Copy link
Member Author

Choose a reason for hiding this comment

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

fair point - done

@@ -496,7 +496,7 @@ def _format_attrs(self):
if attrib == "freq":
freq = self.freqstr
if freq is not None:
freq = "'%s'" % freq
freq = f"'{freq}'"
Copy link
Member

Choose a reason for hiding this comment

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

do the single quotes become unnecessary with a {freq:r} or something like that? This is a pretty common pattern, but we aren't very consistent about when we do or dont use the quotes (i sometimes use ticks like for markdown)

Copy link
Member Author

Choose a reason for hiding this comment

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

Assuming you mean {freq!r} here - updated. Agree doesn't seem to be much consistency

@jbrockmendel
Copy link
Member

A handful of comments, general rule about minimizing the code that is executed inside f-space. If its viable to have the black-version-related stuff not in this diff that'll make it a little easier. cc @gfyoung for another look

@gfyoung
Copy link
Member

gfyoung commented Nov 15, 2019

Looks pretty good! I agree with the comments here.

@alimcmaster1
Copy link
Member Author

Updated as per comments and green cc. @jbrockmendel and @gfyoung

@jreback jreback added this to the 1.0 milestone Nov 18, 2019
@jreback jreback merged commit c236491 into pandas-dev:master Nov 18, 2019
@jreback
Copy link
Contributor

jreback commented Nov 18, 2019

thanks @alimcmaster1

Reksbril pushed a commit to Reksbril/pandas that referenced this pull request Nov 18, 2019
proost pushed a commit to proost/pandas that referenced this pull request Dec 19, 2019
proost pushed a commit to proost/pandas that referenced this pull request Dec 19, 2019
@alimcmaster1 alimcmaster1 deleted the mcmali-pyup branch December 25, 2019 20:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants