Skip to content

Commit

Permalink
CI: Add pyupgrade to pre-commit config. (#294)
Browse files Browse the repository at this point in the history
  • Loading branch information
csadorf authored Feb 22, 2022
1 parent 648630a commit ba358e1
Show file tree
Hide file tree
Showing 14 changed files with 50 additions and 53 deletions.
6 changes: 6 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,9 @@ repos:
rev: 0.13.0
hooks:
- id: check-github-workflows

- repo: https://github.com/asottile/pyupgrade
rev: v2.31.0
hooks:
- id: pyupgrade
args: [--py37-plus]
2 changes: 1 addition & 1 deletion aiidalab_widgets_base/codes.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ def __init__(self, path_to_root="../", **kwargs):
btn_setup_code,
self._setup_code_out,
]
super(AiiDACodeSetup, self).__init__(children, **kwargs)
super().__init__(children, **kwargs)

@validate("input_plugin")
def _validate_input_plugin(self, proposal):
Expand Down
6 changes: 2 additions & 4 deletions aiidalab_widgets_base/computational_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -853,10 +853,8 @@ def __init__(self, path_to_root="../", **kwargs):
# Code plugin.
self.input_plugin = ipw.Dropdown(
options=sorted(
[
(ep.name, ep)
for ep in plugins.entry_point.get_entry_points("aiida.calculations")
]
(ep.name, ep)
for ep in plugins.entry_point.get_entry_points("aiida.calculations")
),
description="Code plugin:",
layout=LAYOUT,
Expand Down
12 changes: 6 additions & 6 deletions aiidalab_widgets_base/computers.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def __init__(self, **kwargs):
btn_setup_ssh,
self._setup_ssh_out,
]
super(SshComputerSetup, self).__init__(children, **kwargs)
super().__init__(children, **kwargs)

@staticmethod
def _ssh_keygen():
Expand Down Expand Up @@ -297,8 +297,8 @@ def _send_pubkey(hostname, username, password, proxycmd=""):
from pexpect import TIMEOUT

timeout = 10
print("Sending public key to {}... ".format(hostname), end="")
str_ssh = "ssh-copy-id {}@{}".format(username, hostname)
print(f"Sending public key to {hostname}... ", end="")
str_ssh = f"ssh-copy-id {username}@{hostname}"
if proxycmd:
str_ssh += ' -o "ProxyCommand ssh -q -Y ' + proxycmd + ' netcat %h %p\n"'
child = pexpect.spawn(str_ssh)
Expand All @@ -314,7 +314,7 @@ def _send_pubkey(hostname, username, password, proxycmd=""):
timeout=timeout,
) # final
except TIMEOUT:
print("Exceeded {} s timeout".format(timeout))
print(f"Exceeded {timeout} s timeout")
return False

possible_output = {
Expand Down Expand Up @@ -736,11 +736,11 @@ def __init__(self, **kwargs):
ipw.HBox([btn_setup_comp, btn_test]),
ipw.HBox([self._setup_comp_out, self._test_out]),
]
super(AiidaComputerSetup, self).__init__(children, **kwargs)
super().__init__(children, **kwargs)

def _configure_computer(self):
"""Create AuthInfo."""
print("Configuring '{}'".format(self.label))
print(f"Configuring '{self.label}'")
sshcfg = parse_sshconfig(self.hostname)
authparams = {
"compress": True,
Expand Down
14 changes: 7 additions & 7 deletions aiidalab_widgets_base/databases.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def __init__(self, title="", **kwargs):
self.query_message,
ipw.HBox([self.drop_structure, self.link]),
]
super(CodQueryWidget, self).__init__(children=children, **kwargs)
super().__init__(children=children, **kwargs)

@staticmethod
def _query(idn=None, formula=None):
Expand Down Expand Up @@ -106,7 +106,7 @@ def _on_click_query(self, change): # pylint: disable=unused-argument
)
structures.append(entry_add)

self.query_message.value += "{} structures found".format(len(structures) - 1)
self.query_message.value += f"{len(structures) - 1} structures found"
self.drop_structure.options = structures

def _on_select_structure(self, change):
Expand Down Expand Up @@ -462,29 +462,29 @@ def _reset(self, _=None):

def clean_up_database(self, database, plugin):
for domain in list(database.keys()):
for computer in list(database[domain].keys() - set(["default"])):
for computer in list(database[domain].keys() - {"default"}):
for code in list(
database[domain][computer].keys()
- set(["computer-configure", "computer-setup"])
- {"computer-configure", "computer-setup"}
):
if plugin != database[domain][computer][code]["input_plugin"]:
del database[domain][computer][code]
# If no codes remained that correspond to the chosen plugin, remove the computer.
if (
len(
database[domain][computer].keys()
- set(["computer-configure", "computer-setup"])
- {"computer-configure", "computer-setup"}
)
== 0
):
del database[domain][computer]
# If no computers remained - remove the domain.
if len(database[domain].keys() - set(["default"])) == 0:
if len(database[domain].keys() - {"default"}) == 0:
del database[domain]
# Making sure the 'default' key still points to an existing computer.
elif database[domain]["default"] not in database[domain]:
database[domain]["default"] = sorted(
database[domain].keys() - set(["default"])
database[domain].keys() - {"default"}
)[0]
return database

Expand Down
4 changes: 2 additions & 2 deletions aiidalab_widgets_base/elns.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def connect_to_eln(eln_instance=None, **kwargs):
# assuming that the connection can only be established to the ELNs
# with the stored configuration.
try:
with open(ELN_CONFIG, "r") as file:
with open(ELN_CONFIG) as file:
config = json.load(file)
except (FileNotFoundError, json.JSONDecodeError, KeyError):
return (
Expand Down Expand Up @@ -235,7 +235,7 @@ def write_to_config(self, config):

def get_config(self):
try:
with open(ELN_CONFIG, "r") as file:
with open(ELN_CONFIG) as file:
return json.load(file)
except (FileNotFoundError, json.JSONDecodeError, KeyError):
return {}
Expand Down
6 changes: 3 additions & 3 deletions aiidalab_widgets_base/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ class ExportButtonWidget(Button):
def __init__(self, process, **kwargs):
self.process = process
if "description" not in kwargs:
kwargs["description"] = "Export workflow ({})".format(self.process.id)
kwargs["description"] = f"Export workflow ({self.process.id})"
if "layout" not in kwargs:
kwargs["layout"] = {}
kwargs["layout"]["width"] = "initial"
super(ExportButtonWidget, self).__init__(**kwargs)
super().__init__(**kwargs)
self.on_click(self.export_aiida_subgraph)

def export_aiida_subgraph(self, change=None): # pylint: disable=unused-argument
Expand All @@ -42,7 +42,7 @@ def export_aiida_subgraph(self, change=None): # pylint: disable=unused-argument
link.click();
document.body.removeChild(link);
""".format(
payload=payload, filename="export_{}.aiida".format(self.process.id)
payload=payload, filename=f"export_{self.process.id}.aiida"
)
)
display(javas)
16 changes: 8 additions & 8 deletions aiidalab_widgets_base/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def show_selected_input(self, change=None):
clear_output()
if change["new"]:
selected_input = self.process.inputs[change["new"]]
self.info.value = "PK: {}".format(selected_input.id)
self.info.value = f"PK: {selected_input.id}"
display(viewer(selected_input))


Expand Down Expand Up @@ -247,7 +247,7 @@ def show_selected_output(self, change=None):
clear_output()
if change["new"]:
selected_output = self.process.outputs[change["new"]]
self.info.value = "PK: {}".format(selected_output.id)
self.info.value = f"PK: {selected_output.id}"
display(viewer(selected_output))


Expand Down Expand Up @@ -278,7 +278,7 @@ def __init__(
self.followers.append(
ipw.VBox(
[
ipw.HTML("<h2><b>{}</b></h2>".format(follower.title)),
ipw.HTML(f"<h2><b>{follower.title}</b></h2>"),
follower,
]
)
Expand Down Expand Up @@ -347,7 +347,7 @@ def update(self):
elif isinstance(self.process, (CalcFunctionNode, WorkFunctionNode)):
string = get_process_function_report(self.process)
else:
string = "Nothing to show for node type {}".format(self.process.__class__)
string = f"Nothing to show for node type {self.process.__class__}"
self.value = string.replace("\n", "<br/>")


Expand Down Expand Up @@ -375,7 +375,7 @@ def calc_info(self, node):
"""Return a string with the summary of the state of a CalculationNode."""

if not isinstance(node, ProcessNode):
raise TypeError("Unknown type: {}".format(type(node)))
raise TypeError(f"Unknown type: {type(node)}")

process_state = node.process_state.value.capitalize()
pk = """<a#space#href={0}aiidalab-widgets-base/notebooks/process.ipynb?id={1}#space#target="_blank">{1}</a>""".format(
Expand All @@ -387,10 +387,10 @@ def calc_info(self, node):
node.process_label, pk, process_state, node.exit_status
)
else:
string = "{}<{}> {}".format(node.process_label, pk, process_state)
string = f"{node.process_label}<{pk}> {process_state}"

if isinstance(node, WorkChainNode) and node.stepper_state_info:
string += " [{}]".format(node.stepper_state_info)
string += f" [{node.stepper_state_info}]"
return string


Expand Down Expand Up @@ -655,7 +655,7 @@ def update(self, _=None):
if self.description_contains:
dataf = dataf[dataf.Description.str.contains(self.description_contains)]

self.output.value = "{} processes shown".format(len(dataf))
self.output.value = f"{len(dataf)} processes shown"

# Add HTML links.
dataf["PK"] = dataf["PK"].apply(
Expand Down
6 changes: 3 additions & 3 deletions aiidalab_widgets_base/structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def user_modifications(source_structure): # pylint: disable=unused-argument

else:
structure_node = self.structure_node.store()
self.output.value = "Stored in AiiDA [{}]".format(structure_node)
self.output.value = f"Stored in AiiDA [{structure_node}]"

def undo(self, _):
"""Undo modifications."""
Expand Down Expand Up @@ -594,10 +594,10 @@ def search(self, _=None):
matches = sorted(matches, reverse=True, key=lambda n: n.ctime)

options = OrderedDict()
options["Select a Structure ({} found)".format(len(matches))] = False
options[f"Select a Structure ({len(matches)} found)"] = False

for mch in matches:
label = "PK: {}".format(mch.id)
label = f"PK: {mch.id}"
label += " | " + mch.ctime.strftime("%Y-%m-%d %H:%M")
label += " | " + mch.get_extra("formula")
label += " | " + mch.node_type.split(".")[-2]
Expand Down
10 changes: 4 additions & 6 deletions aiidalab_widgets_base/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def predefine_settings(obj, **kwargs):
if hasattr(obj, key):
setattr(obj, key, value)
else:
raise AttributeError("'{}' object has no attirubte '{}'".format(obj, key))
raise AttributeError(f"'{obj}' object has no attirubte '{key}'")


def get_ase_from_file(fname, format=None): # pylint: disable=redefined-builtin
Expand All @@ -34,14 +34,12 @@ def get_ase_from_file(fname, format=None): # pylint: disable=redefined-builtin
else:
traj = read(fname, format=format, index=":")
if not traj:
print(("Could not read any information from the file {}".format(fname)))
print(f"Could not read any information from the file {fname}")
return False
if len(traj) > 1:
print(
(
"Warning: Uploaded file {} contained more than one structure. Selecting the first one.".format(
fname
)
"Warning: Uploaded file {} contained more than one structure. Selecting the first one.".format(
fname
)
)
return traj[0]
Expand Down
14 changes: 6 additions & 8 deletions aiidalab_widgets_base/viewers.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ def viewer(obj, downloadable=True, **kwargs):
Returns the object itself if the viewer wasn't found."""
if not isinstance(obj, Node): # only working with AiiDA nodes
warnings.warn(
"This viewer works only with AiiDA objects, got {}".format(type(obj))
)
warnings.warn(f"This viewer works only with AiiDA objects, got {type(obj)}")
return obj

try:
Expand Down Expand Up @@ -146,7 +144,7 @@ def __init__(self, parameter, downloadable=True, **kwargs):
# this is used to setup table's appearance using CSS
if downloadable:
payload = base64.b64encode(dataf.to_csv(index=False).encode()).decode()
fname = "{}.csv".format(parameter.pk)
fname = f"{parameter.pk}.csv"
to_add = """Download table in csv format: <a download="{filename}"
href="data:text/csv;base64,{payload}" target="_blank">{title}</a>"""
self.value += to_add.format(filename=fname, payload=payload, title=fname)
Expand Down Expand Up @@ -356,9 +354,9 @@ def _observe_cell(self, _=None):
self.cell.lengths()[2]
)

self.cell_alpha.value = "&alpha;: {:.4f}".format(self.cell.angles()[0])
self.cell_beta.value = "&beta;: {:.4f}".format(self.cell.angles()[1])
self.cell_gamma.value = "&gamma;: {:.4f}".format(self.cell.angles()[2])
self.cell_alpha.value = f"&alpha;: {self.cell.angles()[0]:.4f}"
self.cell_beta.value = f"&beta;: {self.cell.angles()[1]:.4f}"
self.cell_gamma.value = f"&gamma;: {self.cell.angles()[2]:.4f}"
else:
self.cell_a.value = "<i><b>a</b></i>:"
self.cell_b.value = "<i><b>b</b></i>:"
Expand Down Expand Up @@ -1103,7 +1101,7 @@ def __init__(self, bands, **kwargs):
x_data = [plot_info["x"] for i in range(len(y_data))]
labels = plot_info["labels"]
# Create the figure
plot = figure(y_axis_label="Dispersion ({})".format(bands.units))
plot = figure(y_axis_label=f"Dispersion ({bands.units})")
plot.multi_line(
x_data, y_data, line_width=2, line_color="red"
) # pylint: disable=too-many-function-args
Expand Down
4 changes: 1 addition & 3 deletions aiidalab_widgets_base/wizard.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ def icons(cls):
"""Return the icon set and return animated icons based on the current time stamp."""
t = time()
return {
key: item
if isinstance(item, str)
else item[int((t * len(item) % len(item)))]
key: item if isinstance(item, str) else item[int(t * len(item) % len(item))]
for key, item in cls.ICONS.items()
}

Expand Down
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
copyright_year_string = (
current_year
if current_year == copyright_first_year
else "{}-{}".format(copyright_first_year, current_year)
else f"{copyright_first_year}-{current_year}"
)
copyright = "{}, {}. All rights reserved".format(
copyright_year_string, copyright_owners
Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf8 -*-
"""This file is required for editable installs of the package."""
from setuptools import setup

Expand Down

0 comments on commit ba358e1

Please sign in to comment.