Skip to content

Commit

Permalink
Use yapf for code formatting
Browse files Browse the repository at this point in the history
Black is restrictive as a matter of design/feature and the [driving
philosophy](psf/black#2156 (comment))
seems to favor very limited changes to Black over addressing
extraordinarily bad formatting.

yapf is not perfect as it requires a commenting pattern to readable
formats for long logical and mathematical expressions. It's far more
flexible and succeeds at generating maintainable code in cases that
Black cannot.
  • Loading branch information
gar1t committed Apr 16, 2023
1 parent d5a5265 commit 7929837
Show file tree
Hide file tree
Showing 103 changed files with 565 additions and 496 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/check-source-code.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pylint black
pip install pylint yapf
pip install -r requirements.txt
pip install -r guild/tests/requirements.txt
Expand All @@ -32,4 +32,4 @@ jobs:
- name: Check source code format
if: ${{ success() || failure() }}
run: |
python -m black --diff --check setup.py guild examples
python -m yapf --diff -r setup.py tools.py guild examples
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ README.html: README.md
markdown_py README.md > README.html

format-code:
@echo Formatting guild code
@black setup.py tools.py guild examples
@echo Formating Guild source code
@yapf -ri setup.py tools.py guild examples

coverage-check:
if [ -z "$(TESTS)" ]; then \
Expand Down
2 changes: 0 additions & 2 deletions examples/bias/gen_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
--help` for command help.
"""


import argparse
import os

Expand Down Expand Up @@ -81,7 +80,6 @@ class TFDVSchemaDisplay(object):
Patch is unapplied when the context manager exits.
"""

def __init__(self):
self._display_save = None
self.display_obj = []
Expand Down
3 changes: 1 addition & 2 deletions examples/detectron2/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ def get_parser():
"{}: {} in {:.2f}s".format(
path,
"detected {} instances".format(len(predictions["instances"]))
if "instances" in predictions
else "finished",
if "instances" in predictions else "finished",
time.time() - start_time,
)
)
Expand Down
1 change: 0 additions & 1 deletion examples/detectron2/predictor.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ class AsyncPredictor:
Because rendering the visualization takes considerably amount of time,
this helps improve throughput when rendering videos.
"""

class _StopToken:
pass

Expand Down
1 change: 0 additions & 1 deletion examples/dvc/eval_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ def plot_contours(ax, clf, xx, yy, **params):

X, y = load_data()


models = (
joblib.load("model-1.joblib"),
joblib.load("model-2.joblib"),
Expand Down
2 changes: 0 additions & 2 deletions examples/dvc/train_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@
svm.SVC(kernel="poly", degree=3, gamma="auto", C=C),
)


X, y = load_data()


for i, m in enumerate(models):
print("Saving model-%i.joblib" % (i + 1))
m.fit(X, y)
Expand Down
4 changes: 3 additions & 1 deletion examples/erlang-plugin/erlang_guild/plugins/escript.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ def __init__(self, script_path):
operations={
script_name: {
"exec": exec_spec,
"sourcecode": {"dest": "."},
"sourcecode": {
"dest": "."
},
"flags": flagdefs,
},
},
Expand Down
5 changes: 4 additions & 1 deletion examples/pytorch-lightning/mnist.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ def add_model_specific_args(parent_parser):
args = parser.parse_args()

transform = transforms.Compose(
[transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,))]
[
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,)),
]
)

mnist_train = MNIST(os.getcwd(), train=True, download=True, transform=transform)
Expand Down
2 changes: 1 addition & 1 deletion examples/tensorboard/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

"""Sample data exhibiting audio summaries, via a waveform generator."""

import inspect
Expand All @@ -25,7 +26,6 @@

tf.compat.v1.disable_eager_execution()


FLAGS = flags.FLAGS

flags.DEFINE_string(
Expand Down
4 changes: 2 additions & 2 deletions examples/tensorboard/custom_scalar.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
x_train, y_train = x[:train_size], y[:train_size]
x_test, y_test = x[train_size:], y[train_size:]

logdir = os.getenv("LOGDIR") or "logs/scalars/" + datetime.now().strftime(
"%Y%m%d-%H%M%S"
logdir = (
os.getenv("LOGDIR") or ("logs/scalars/" + datetime.now().strftime("%Y%m%d-%H%M%S"))
)
file_writer = tf.summary.create_file_writer(logdir + "/metrics")
file_writer.set_as_default()
Expand Down
5 changes: 3 additions & 2 deletions examples/tensorboard/graphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@
train_images = train_images / 255.0

# Define the Keras TensorBoard callback.
logdir = os.getenv("TRAIN_LOGDIR") or "logs/fit/" + datetime.now().strftime(
"%Y%m%d-%H%M%S"
logdir = (
os.getenv("TRAIN_LOGDIR")
or ("logs/fit/" + datetime.now().strftime("%Y%m%d-%H%M%S"))
)
tensorboard_callback = keras.callbacks.TensorBoard(log_dir=logdir)

Expand Down
1 change: 0 additions & 1 deletion examples/tensorboard/hparams.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
logdir = os.getenv("LOGDIR") or "logs/image/" + datetime.now().strftime("%Y%m%d-%H%M%S")
tensorboard_callback = keras.callbacks.TensorBoard(log_dir=logdir)


model.fit(
x_train,
y_train,
Expand Down
16 changes: 9 additions & 7 deletions examples/tensorboard/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@
img = np.reshape(train_images[0], (-1, 28, 28, 1))

# Sets up a timestamped log directory.
logdir = os.getenv("TRAIN_DATA_LOGDIR") or "logs/train_data/" + datetime.now().strftime(
"%Y%m%d-%H%M%S"
logdir = (
os.getenv("TRAIN_DATA_LOGDIR")
or ("logs/train_data/" + datetime.now().strftime("%Y%m%d-%H%M%S"))
)
# Creates a file writer for the log directory.
file_writer = tf.summary.create_file_writer(logdir)
Expand All @@ -60,8 +61,9 @@
images = np.reshape(train_images[0:25], (-1, 28, 28, 1))
tf.summary.image("25 training data examples", images, max_outputs=25, step=0)

logdir = os.getenv("PLOTS_LOGDIR") or "logs/plots/" + datetime.now().strftime(
"%Y%m%d-%H%M%S"
logdir = (
os.getenv("PLOTS_LOGDIR")
or ("logs/plots/" + datetime.now().strftime("%Y%m%d-%H%M%S"))
)
file_writer = tf.summary.create_file_writer(logdir)

Expand Down Expand Up @@ -104,7 +106,6 @@ def image_grid():
with file_writer.as_default():
tf.summary.image("Training data", plot_to_image(figure), step=0)


model = keras.models.Sequential(
[
keras.layers.Flatten(input_shape=(28, 28)),
Expand Down Expand Up @@ -149,8 +150,9 @@ def plot_confusion_matrix(cm, class_names):
return figure


logdir = os.getenv("IMAGE_LOGDIR") or "logs/image/" + datetime.now().strftime(
"%Y%m%d-%H%M%S"
logdir = (
os.getenv("IMAGE_LOGDIR")
or ("logs/image/" + datetime.now().strftime("%Y%m%d-%H%M%S"))
)
# Define the basic TensorBoard callback.
tensorboard_callback = keras.callbacks.TensorBoard(log_dir=logdir)
Expand Down
4 changes: 2 additions & 2 deletions examples/tensorboard/loss_scalar.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
x_train, y_train = x[:train_size], y[:train_size]
x_test, y_test = x[train_size:], y[train_size:]

logdir = os.getenv("LOGDIR") or "logs/scalars/" + datetime.now().strftime(
"%Y%m%d-%H%M%S"
logdir = (
os.getenv("LOGDIR") or ("logs/scalars/" + datetime.now().strftime("%Y%m%d-%H%M%S"))
)
tensorboard_callback = keras.callbacks.TensorBoard(log_dir=logdir)

Expand Down
19 changes: 7 additions & 12 deletions examples/tensorboard/pr_curve.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

"""Create sample PR curve summary data.
We have 3 classes: R, G, and B. We generate colors within RGB space from 3
Expand Down Expand Up @@ -152,22 +153,16 @@ def start_runs(logdir, steps, run_name, thresholds, mask_every_other_prediction=
# tail of the normal distribution.
examples = tf.concat([true_reds, true_greens, true_blues], axis=0)
probabilities_colors_are_red = (
1
- red_predictor.cdf(
tf.norm(tensor=examples - tf.constant([255.0, 0, 0]), axis=1)
)
1 - red_predictor
.cdf(tf.norm(tensor=examples - tf.constant([255.0, 0, 0]), axis=1))
) * 2
probabilities_colors_are_green = (
1
- green_predictor.cdf(
tf.norm(tensor=examples - tf.constant([0, 255.0, 0]), axis=1)
)
1 - green_predictor
.cdf(tf.norm(tensor=examples - tf.constant([0, 255.0, 0]), axis=1))
) * 2
probabilities_colors_are_blue = (
1
- blue_predictor.cdf(
tf.norm(tensor=examples - tf.constant([0, 0, 255.0]), axis=1)
)
1 - blue_predictor
.cdf(tf.norm(tensor=examples - tf.constant([0, 0, 255.0]), axis=1))
) * 2

predictions = (
Expand Down
6 changes: 3 additions & 3 deletions examples/tensorboard/projector.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@
)

# Set up a logs directory, so Tensorboard knows where to look for files
log_dir = os.getenv("LOGDIR") or "logs/imdb-example/" + datetime.now().strftime(
"%Y%m%d-%H%M%S"
log_dir = (
os.getenv("LOGDIR")
or ("logs/imdb-example/" + datetime.now().strftime("%Y%m%d-%H%M%S"))
)
if not os.path.exists(log_dir):
os.makedirs(log_dir)
Expand All @@ -65,7 +66,6 @@
for unknown in range(1, encoder.vocab_size - len(encoder.subwords)):
f.write("unknown #{}\n".format(unknown))


# Save the weights we want to analyse as a variable. Note that the first
# value represents any unknown word, which is not in the metadata, so
# we will remove that value.
Expand Down
5 changes: 3 additions & 2 deletions examples/tensorboard/projector2.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
vectors = np.array([[0, 0, 1], [0, 1, 0], [1, 0, 0], [1, 1, 1]])
metadata = ['001', '010', '100', '111'] # labels

log_dir = os.getenv("LOGDIR") or "logs/projector/" + datetime.now().strftime(
"%Y%m%d-%H%M%S"
log_dir = (
os.getenv("LOGDIR")
or ("logs/projector/" + datetime.now().strftime("%Y%m%d-%H%M%S"))
)
with SummaryWriter(log_dir) as writer:
writer.add_embedding(vectors, metadata)
10 changes: 5 additions & 5 deletions guild/_lex.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,7 @@ def token(self):
if not self.lexoptimize:
if newtok.type not in self.lextokens_all:
raise LexError(
"%s:%d: Rule '%s' returned an unknown token type '%s'"
% (
"%s:%d: Rule '%s' returned an unknown token type '%s'" % (
func.__code__.co_filename,
func.__code__.co_firstlineno,
func.__name__,
Expand Down Expand Up @@ -417,10 +416,11 @@ def token(self):
self.lexpos = lexpos
newtok = self.lexerrorf(tok)
if lexpos == self.lexpos:
# Error method didn't change text position at all. This is an error.
# Error method didn't change text position at
# all. This is an error.
raise LexError(
"Scanning error. Illegal character '%s'"
% (lexdata[lexpos]),
"Scanning error. Illegal character '%s'" %
(lexdata[lexpos]),
lexdata[lexpos:],
)
lexpos = self.lexpos
Expand Down
23 changes: 9 additions & 14 deletions guild/_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,7 @@ def _run_test(name, fail_fast, force):
_log_test_not_found(name)
return False
if (
not force
and os.getenv("FORCE_TEST") != "1"
not force and os.getenv("FORCE_TEST") != "1"
and front_matter_skip_test(filename)
):
_log_skipped_test(name)
Expand Down Expand Up @@ -362,7 +361,6 @@ class Checker(doctest.OutputChecker):
environment variable to `1`.
"""

def check_output(self, want, got, optionflags):
if optionflags & STRICT:
optionflags -= optionflags & doctest.ELLIPSIS
Expand Down Expand Up @@ -538,7 +536,7 @@ def _parse_example(self, m, name, lineno):
source_lines = m.group("source").split("\n")
_check_prompt_blank(source_lines, indent, name, lineno)
self._check_prefix(source_lines[1:], " " * indent + ">", name, lineno)
source = "\n".join([sl[indent + 2 :] for sl in source_lines])
source = "\n".join([sl[indent + 2:] for sl in source_lines])

want = m.group("want")
want_lines = want.split("\n")
Expand Down Expand Up @@ -766,7 +764,8 @@ def _filter_ignored(paths, ignore):
if isinstance(ignore, str):
ignore = [ignore]
return [
p for p in paths if not any((fnmatch.fnmatch(p, pattern) for pattern in ignore))
p for p in paths
if not any((fnmatch.fnmatch(p, pattern) for pattern in ignore))
]


Expand Down Expand Up @@ -813,8 +812,7 @@ def cat_json(*parts):
def dir(path=".", ignore=None):
return sorted(
[
name
for name in os.listdir(path)
name for name in os.listdir(path)
if ignore is None or not any((fnmatch.fnmatch(name, p) for p in ignore))
]
)
Expand Down Expand Up @@ -887,7 +885,6 @@ class Project:
facility should be refactored to use the pattern described in
`guild/tests/test-template.md` using `use_project()`.
"""

def __init__(self, cwd, guild_home=None, env=None):
from guild import index as indexlib # expensive

Expand Down Expand Up @@ -990,8 +987,7 @@ def _filter_output(out, ignore):
ignore = [ignore]
return "\n".join(
[
line
for line in out.split("\n")
line for line in out.split("\n")
if all(s and s not in line for s in ignore)
]
)
Expand Down Expand Up @@ -1148,7 +1144,7 @@ def _strip_error_module(last_line):


def _strip_class_module(class_name):
return class_name[class_name.rfind(".") + 1 :]
return class_name[class_name.rfind(".") + 1:]


def _normlf(s):
Expand Down Expand Up @@ -1359,8 +1355,7 @@ def _strip_lines(out, patterns):
if isinstance(patterns, str):
patterns = [patterns]
stripped_lines = [
line
for line in out.split("\n")
line for line in out.split("\n")
if not any((re.search(p, line) for p in patterns))
]
return "\n".join(stripped_lines)
Expand All @@ -1376,7 +1371,7 @@ def _cut_line(line, to_cut):
cut_line = []
cols = line.split()
for i in to_cut:
cut_line.extend(cols[i : i + 1])
cut_line.extend(cols[i:i + 1])
return cut_line


Expand Down

0 comments on commit 7929837

Please sign in to comment.