Skip to content
Open
Show file tree
Hide file tree
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
103 changes: 40 additions & 63 deletions cupy_xarray/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ def get_keywords():
git_refnames = "$Format:%d$"
git_full = "$Format:%H$"
git_date = "$Format:%ci$"
keywords = {"refnames": git_refnames, "full": git_full, "date": git_date}
return keywords
return {"refnames": git_refnames, "full": git_full, "date": git_date}
Copy link
Author

Choose a reason for hiding this comment

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

Function get_keywords refactored with the following changes:



class VersioneerConfig:
Expand Down Expand Up @@ -96,18 +95,18 @@ def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False,
if e.errno == errno.ENOENT:
continue
if verbose:
print("unable to run %s" % dispcmd)
print(f"unable to run {dispcmd}")
print(e)
return None, None
else:
if verbose:
print("unable to find command, tried %s" % (commands,))
print(f"unable to find command, tried {commands}")
return None, None
stdout = process.communicate()[0].strip().decode()
if process.returncode != 0:
if verbose:
print("unable to run %s (error)" % dispcmd)
print("stdout was %s" % stdout)
print(f"unable to run {dispcmd} (error)")
print(f"stdout was {stdout}")
Comment on lines -99 to +109
Copy link
Author

Choose a reason for hiding this comment

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

Function run_command refactored with the following changes:

return None, process.returncode
return stdout, process.returncode

Expand All @@ -131,8 +130,9 @@ def versions_from_parentdir(parentdir_prefix, root, verbose):
root = os.path.dirname(root) # up a level

if verbose:
print("Tried directories %s but none started with prefix %s" %
(str(rootdirs), parentdir_prefix))
print(
f"Tried directories {rootdirs} but none started with prefix {parentdir_prefix}"
)
Comment on lines -134 to +135
Copy link
Author

Choose a reason for hiding this comment

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

Function versions_from_parentdir refactored with the following changes:

raise NotThisMethod("rootdir doesn't start with parentdir_prefix")


Expand All @@ -148,17 +148,14 @@ def git_get_keywords(versionfile_abs):
with open(versionfile_abs, "r") as fobj:
for line in fobj:
if line.strip().startswith("git_refnames ="):
mo = re.search(r'=\s*"(.*)"', line)
if mo:
keywords["refnames"] = mo.group(1)
if mo := re.search(r'=\s*"(.*)"', line):
keywords["refnames"] = mo[1]
if line.strip().startswith("git_full ="):
mo = re.search(r'=\s*"(.*)"', line)
if mo:
keywords["full"] = mo.group(1)
if mo := re.search(r'=\s*"(.*)"', line):
keywords["full"] = mo[1]
if line.strip().startswith("git_date ="):
mo = re.search(r'=\s*"(.*)"', line)
if mo:
keywords["date"] = mo.group(1)
if mo := re.search(r'=\s*"(.*)"', line):
keywords["date"] = mo[1]
Comment on lines -151 to +158
Copy link
Author

Choose a reason for hiding this comment

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

Function git_get_keywords refactored with the following changes:

except OSError:
pass
return keywords
Expand Down Expand Up @@ -202,9 +199,9 @@ def git_versions_from_keywords(keywords, tag_prefix, verbose):
# "stabilization", as well as "HEAD" and "master".
tags = {r for r in refs if re.search(r'\d', r)}
if verbose:
print("discarding '%s', no digits" % ",".join(refs - tags))
print(f"""discarding '{",".join(refs - tags)}', no digits""")
if verbose:
print("likely tags: %s" % ",".join(sorted(tags)))
print(f'likely tags: {",".join(sorted(tags))}')
Comment on lines -205 to +204
Copy link
Author

Choose a reason for hiding this comment

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

Function git_versions_from_keywords refactored with the following changes:

for ref in sorted(tags):
# sorting will prefer e.g. "2.0" over "2.0rc1"
if ref.startswith(tag_prefix):
Expand All @@ -215,7 +212,7 @@ def git_versions_from_keywords(keywords, tag_prefix, verbose):
if not re.match(r'\d', r):
continue
if verbose:
print("picking %s" % r)
print(f"picking {r}")
return {"version": r,
"full-revisionid": keywords["full"].strip(),
"dirty": False, "error": None,
Expand All @@ -236,10 +233,7 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, runner=run_command):
expanded, and _version.py hasn't already been rewritten with a short
version string, meaning we're inside a checked out source tree.
"""
GITS = ["git"]
if sys.platform == "win32":
GITS = ["git.cmd", "git.exe"]

GITS = ["git.cmd", "git.exe"] if sys.platform == "win32" else ["git"]
Comment on lines -239 to +236
Copy link
Author

Choose a reason for hiding this comment

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

Function git_pieces_from_vcs refactored with the following changes:

This removes the following comments ( why? ):

# maybe improved later

# GIT_DIR can interfere with correct operation of Versioneer.
# It may be intended to be passed to the Versioneer-versioned project,
# but that should not change where we get our version from.
Expand All @@ -251,7 +245,7 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, runner=run_command):
hide_stderr=True)
if rc != 0:
if verbose:
print("Directory %s not under git control" % root)
print(f"Directory {root} not under git control")
raise NotThisMethod("'git rev-parse --git-dir' returned error")

# if there is a tag matching tag_prefix, this yields TAG-NUM-gHEX[-dirty]
Expand All @@ -269,11 +263,7 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, runner=run_command):
raise NotThisMethod("'git rev-parse' failed")
full_out = full_out.strip()

pieces = {}
pieces["long"] = full_out
pieces["short"] = full_out[:7] # maybe improved later
pieces["error"] = None

pieces = {"long": full_out, "short": full_out[:7], "error": None}
branch_name, rc = runner(GITS, ["rev-parse", "--abbrev-ref", "HEAD"],
cwd=root)
# --abbrev-ref was added in git-1.6.3
Expand Down Expand Up @@ -324,26 +314,23 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, runner=run_command):
mo = re.search(r'^(.+)-(\d+)-g([0-9a-f]+)$', git_describe)
if not mo:
# unparsable. Maybe git-describe is misbehaving?
pieces["error"] = ("unable to parse git-describe output: '%s'"
% describe_out)
pieces["error"] = f"unable to parse git-describe output: '{describe_out}'"
return pieces

# tag
full_tag = mo.group(1)
full_tag = mo[1]
if not full_tag.startswith(tag_prefix):
if verbose:
fmt = "tag '%s' doesn't start with prefix '%s'"
print(fmt % (full_tag, tag_prefix))
pieces["error"] = ("tag '%s' doesn't start with prefix '%s'"
% (full_tag, tag_prefix))
print(f"tag '{full_tag}' doesn't start with prefix '{tag_prefix}'")
pieces["error"] = f"tag '{full_tag}' doesn't start with prefix '{tag_prefix}'"
return pieces
pieces["closest-tag"] = full_tag[len(tag_prefix):]

# distance: number of commits since tag
pieces["distance"] = int(mo.group(2))
pieces["distance"] = int(mo[2])

# commit: short hex revision ID
pieces["short"] = mo.group(3)
pieces["short"] = mo[3]

else:
# HEX: no tags
Expand All @@ -363,9 +350,7 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, runner=run_command):

def plus_or_dot(pieces):
"""Return a + if we don't already have one, else return a ."""
if "+" in pieces.get("closest-tag", ""):
return "."
return "+"
return "." if "+" in pieces.get("closest-tag", "") else "+"
Comment on lines -366 to +353
Copy link
Author

Choose a reason for hiding this comment

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

Function plus_or_dot refactored with the following changes:



def render_pep440(pieces):
Expand All @@ -382,14 +367,12 @@ def render_pep440(pieces):
if pieces["distance"] or pieces["dirty"]:
rendered += plus_or_dot(pieces)
rendered += "%d.g%s" % (pieces["distance"], pieces["short"])
if pieces["dirty"]:
rendered += ".dirty"
else:
# exception #1
rendered = "0+untagged.%d.g%s" % (pieces["distance"],
pieces["short"])
if pieces["dirty"]:
rendered += ".dirty"
if pieces["dirty"]:
rendered += ".dirty"
Comment on lines -385 to +375
Copy link
Author

Choose a reason for hiding this comment

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

Function render_pep440 refactored with the following changes:

return rendered


Expand All @@ -409,17 +392,15 @@ def render_pep440_branch(pieces):
rendered += ".dev0"
rendered += plus_or_dot(pieces)
rendered += "%d.g%s" % (pieces["distance"], pieces["short"])
if pieces["dirty"]:
rendered += ".dirty"
Comment on lines -412 to -413
Copy link
Author

Choose a reason for hiding this comment

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

Function render_pep440_branch refactored with the following changes:

else:
# exception #1
rendered = "0"
if pieces["branch"] != "master":
rendered += ".dev0"
rendered += "+untagged.%d.g%s" % (pieces["distance"],
pieces["short"])
if pieces["dirty"]:
rendered += ".dirty"
if pieces["dirty"]:
rendered += ".dirty"
return rendered


Expand Down Expand Up @@ -474,13 +455,13 @@ def render_pep440_post(pieces):
if pieces["dirty"]:
rendered += ".dev0"
rendered += plus_or_dot(pieces)
rendered += "g%s" % pieces["short"]
rendered += f'g{pieces["short"]}'
else:
# exception #1
rendered = "0.post%d" % pieces["distance"]
if pieces["dirty"]:
rendered += ".dev0"
rendered += "+g%s" % pieces["short"]
rendered += f'+g{pieces["short"]}'
Comment on lines -477 to +464
Copy link
Author

Choose a reason for hiding this comment

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

Function render_pep440_post refactored with the following changes:

return rendered


Expand All @@ -499,17 +480,15 @@ def render_pep440_post_branch(pieces):
if pieces["branch"] != "master":
rendered += ".dev0"
rendered += plus_or_dot(pieces)
rendered += "g%s" % pieces["short"]
if pieces["dirty"]:
rendered += ".dirty"
rendered += f'g{pieces["short"]}'
else:
# exception #1
rendered = "0.post%d" % pieces["distance"]
if pieces["branch"] != "master":
rendered += ".dev0"
rendered += "+g%s" % pieces["short"]
if pieces["dirty"]:
rendered += ".dirty"
rendered += f'+g{pieces["short"]}'
if pieces["dirty"]:
rendered += ".dirty"
Comment on lines -502 to +491
Copy link
Author

Choose a reason for hiding this comment

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

Function render_pep440_post_branch refactored with the following changes:

return rendered


Expand All @@ -525,13 +504,11 @@ def render_pep440_old(pieces):
rendered = pieces["closest-tag"]
if pieces["distance"] or pieces["dirty"]:
rendered += ".post%d" % pieces["distance"]
if pieces["dirty"]:
rendered += ".dev0"
else:
# exception #1
rendered = "0.post%d" % pieces["distance"]
if pieces["dirty"]:
rendered += ".dev0"
if pieces["dirty"]:
rendered += ".dev0"
Comment on lines -528 to +511
Copy link
Author

Choose a reason for hiding this comment

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

Function render_pep440_old refactored with the following changes:

return rendered


Expand Down Expand Up @@ -604,7 +581,7 @@ def render(pieces, style):
elif style == "git-describe-long":
rendered = render_git_describe_long(pieces)
else:
raise ValueError("unknown style '%s'" % style)
raise ValueError(f"unknown style '{style}'")
Comment on lines -607 to +584
Copy link
Author

Choose a reason for hiding this comment

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

Function render refactored with the following changes:


return {"version": rendered, "full-revisionid": pieces["long"],
"dirty": pieces["dirty"], "error": None,
Expand Down
17 changes: 8 additions & 9 deletions cupy_xarray/accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,22 +113,21 @@ def __init__(self, ds):

@property
def is_cupy(self):
return all([da.cupy.is_cupy for da in self.ds.data_vars.values()])
return all(da.cupy.is_cupy for da in self.ds.data_vars.values())
Copy link
Author

Choose a reason for hiding this comment

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

Function CupyDatasetAccessor.is_cupy refactored with the following changes:


def as_cupy(self):
data_vars = {var: da.as_cupy() for var, da in self.ds.data_vars.items()}
return Dataset(data_vars=data_vars, coords=self.ds.coords, attrs=self.ds.attrs)

def as_numpy(self):
if self.is_cupy:
data_vars = {var: da.cupy.as_numpy() for var, da in self.ds.data_vars.items()}
return Dataset(
data_vars=data_vars,
coords=self.ds.coords,
attrs=self.ds.attrs,
)
else:
if not self.is_cupy:
return self.ds.as_numpy()
data_vars = {var: da.cupy.as_numpy() for var, da in self.ds.data_vars.items()}
return Dataset(
data_vars=data_vars,
coords=self.ds.coords,
attrs=self.ds.attrs,
)
Comment on lines -123 to +130
Copy link
Author

Choose a reason for hiding this comment

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

Function CupyDatasetAccessor.as_numpy refactored with the following changes:



# Attach the `as_cupy` methods to the top level `Dataset` and `Dataarray` objects.
Expand Down
Loading