Skip to content

Commit 24ca36e

Browse files
authored
Add scripts/ & benchmarks/ to directories checked by scripts/pylint (#1047)
This adds `./scripts` and `./benchmarks` to directories where the linter is run by `scripts/lint_all.sh`. Additional changes in a couple of other files are part of this PR because running the linter on those files resulted in linting errors that the changes fix.
1 parent 6a3d2ab commit 24ca36e

2 files changed

Lines changed: 35 additions & 25 deletions

File tree

scripts/format_ipynb.py

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,36 @@
1313
# limitations under the License.
1414
# ==============================================================================
1515
"""Format notebook code cells using yapf google style."""
16+
1617
import glob
1718
import nbformat
1819
import yapf
1920

20-
# Must be run from the top level of the `TFQuantum` repo.
21-
NOTEBOOKS = glob.glob("docs/tutorials/*.ipynb")
22-
for fname in NOTEBOOKS:
23-
nb = nbformat.read(fname, as_version=nbformat.NO_CONVERT)
24-
all_cells = nb.get('cells')
25-
for i, cell in enumerate(all_cells):
26-
if cell.get('cell_type') != 'code':
27-
continue
28-
lines = cell.get('source')
29-
# This will safely skip over cells containing !% magic
30-
try:
31-
fmt_lines = yapf.yapf_api.FormatCode(''.join(lines),
32-
style_config="google")[0]
33-
except (SyntaxError, yapf.yapflib.errors.YapfError):
34-
continue
35-
# google style always adds an EOF newline; undo this.
36-
all_cells[i]['source'] = fmt_lines[:-1]
37-
38-
nb['cells'] = all_cells
39-
nbformat.write(nb, fname, version=nbformat.NO_CONVERT)
21+
22+
def format_notebooks():
23+
"""Format tutorial notebooks.
24+
25+
This must be run from the top level of the repository."""
26+
27+
for fname in glob.glob("docs/tutorials/*.ipynb"):
28+
nb = nbformat.read(fname, as_version=nbformat.NO_CONVERT)
29+
all_cells = nb.get('cells')
30+
for i, cell in enumerate(all_cells):
31+
if cell.get('cell_type') != 'code':
32+
continue
33+
lines = cell.get('source')
34+
# This will safely skip over cells containing !% magic
35+
try:
36+
fmt_lines = yapf.yapf_api.FormatCode(''.join(lines),
37+
style_config="google")[0]
38+
except (SyntaxError, yapf.yapflib.errors.YapfError):
39+
continue
40+
# google style always adds an EOF newline; undo this.
41+
all_cells[i]['source'] = fmt_lines[:-1]
42+
43+
nb['cells'] = all_cells
44+
nbformat.write(nb, fname, version=nbformat.NO_CONVERT)
45+
46+
47+
if __name__ == "__main__":
48+
format_notebooks()

scripts/lint_all.sh

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
#!/bin/bash
22
# Copyright 2020 The TensorFlow Quantum Authors. All Rights Reserved.
3-
#
3+
#
44
# Licensed under the Apache License, Version 2.0 (the "License");
55
# you may not use this file except in compliance with the License.
66
# You may obtain a copy of the License at
7-
#
7+
#
88
# http://www.apache.org/licenses/LICENSE-2.0
9-
#
9+
#
1010
# Unless required by applicable law or agreed to in writing, software
1111
# distributed under the License is distributed on an "AS IS" BASIS,
1212
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
# ==============================================================================
16-
echo "Checking for lint in python code...";
17-
linting_outputs=$(pylint --rcfile .pylintrc ./tensorflow_quantum);
16+
echo "Checking for lint in Python code..."
17+
linting_outputs=$(pylint --rcfile .pylintrc \
18+
./benchmarks ./scripts ./tensorflow_quantum)
1819
exit_code=$?
1920
if [ "$exit_code" == "0" ]; then
2021
echo "Python linting complete!";

0 commit comments

Comments
 (0)