Skip to content

Commit

Permalink
Use f-strings where possible (#762)
Browse files Browse the repository at this point in the history
Semi-automated using ikamensh/flynt
  • Loading branch information
akx authored Dec 18, 2023
1 parent fd2661f commit bc9c607
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 76 deletions.
7 changes: 1 addition & 6 deletions papermill/adl.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,7 @@ def listdir(self, url):
"""Returns a list of the files under the specified path"""
(store_name, path) = self._split_url(url)
adapter = self._create_adapter(store_name)
return [
"adl://{store_name}.azuredatalakestore.net/{path_to_child}".format(
store_name=store_name, path_to_child=path_to_child
)
for path_to_child in adapter.ls(path)
]
return [f"adl://{store_name}.azuredatalakestore.net/{path_to_child}" for path_to_child in adapter.ls(path)]

def read(self, url):
"""Read storage at a given url"""
Expand Down
6 changes: 1 addition & 5 deletions papermill/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@
def print_papermill_version(ctx, param, value):
if not value:
return
print(
"{version} from {path} ({pyver})".format(
version=papermill_version, path=__file__, pyver=platform.python_version()
)
)
print(f"{papermill_version} from {__file__} ({platform.python_version()})")
ctx.exit()


Expand Down
2 changes: 1 addition & 1 deletion papermill/clientwrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def execute(self, **kwargs):
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

with self.setup_kernel(**kwargs):
self.log.info("Executing notebook with kernel: %s" % self.kernel_name)
self.log.info(f"Executing notebook with kernel: {self.kernel_name}")
self.papermill_execute_cells()
info_msg = self.wait_for_reply(self.kc.kernel_info())
self.nb.metadata['language_info'] = info_msg['content']['language_info']
Expand Down
4 changes: 2 additions & 2 deletions papermill/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ def __str__(self):
# when called with str(). In order to maintain compatibility with previous versions which
# passed only the message to the superclass constructor, __str__ method is implemented to
# provide the same result as was produced in the past.
message = "\n" + 75 * "-" + "\n"
message += 'Exception encountered at "In [%s]":\n' % str(self.exec_count)
message = f"\n{75 * '-'}\n"
message += f'Exception encountered at "In [{self.exec_count}]":\n'
message += "\n".join(self.traceback)
message += "\n"
return message
Expand Down
10 changes: 4 additions & 6 deletions papermill/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ def execute_notebook(
input_path = parameterize_path(input_path, path_parameters)
output_path = parameterize_path(output_path, path_parameters)

logger.info("Input Notebook: %s" % get_pretty_path(input_path))
logger.info("Output Notebook: %s" % get_pretty_path(output_path))
logger.info(f"Input Notebook: {get_pretty_path(input_path)}")
logger.info(f"Output Notebook: {get_pretty_path(output_path)}")
with local_file_io_cwd():
if cwd is not None:
logger.info(f"Working directory: {get_pretty_path(cwd)}")
Expand Down Expand Up @@ -169,13 +169,11 @@ def prepare_notebook_metadata(nb, input_path, output_path, report_mode=False):
ERROR_STYLE = 'style="color:red; font-family:Helvetica Neue, Helvetica, Arial, sans-serif; font-size:2em;"'

ERROR_MESSAGE_TEMPLATE = (
'<span ' + ERROR_STYLE + '>'
"An Exception was encountered at '<a href=\"#papermill-error-cell\">In [%s]</a>'."
'</span>'
f"<span {ERROR_STYLE}>An Exception was encountered at '<a href=\"#papermill-error-cell\">In [%s]</a>'.</span>"
)

ERROR_ANCHOR_MSG = (
'<span id="papermill-error-cell" ' + ERROR_STYLE + '>'
f'<span id="papermill-error-cell" {ERROR_STYLE}>'
'Execution using papermill encountered an exception here and stopped:'
'</span>'
)
Expand Down
8 changes: 4 additions & 4 deletions papermill/inspection.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
def _open_notebook(notebook_path, parameters):
path_parameters = add_builtin_parameters(parameters)
input_path = parameterize_path(notebook_path, path_parameters)
logger.info("Input Notebook: %s" % get_pretty_path(input_path))
logger.info(f"Input Notebook: {get_pretty_path(input_path)}")

with local_file_io_cwd():
return load_notebook_node(input_path)
Expand Down Expand Up @@ -78,14 +78,14 @@ def display_notebook_help(ctx, notebook_path, parameters):
if type_repr == "None":
type_repr = "Unknown type"

definition = " {}: {} (default {})".format(p["name"], type_repr, p["default"])
definition = f" {p['name']}: {type_repr} (default {p['default']})"
if len(definition) > 30:
if len(p["help"]):
param_help = "".join((definition, "\n", 34 * " ", p["help"]))
param_help = f"{definition}\n{34 * ' '}{p['help']}"
else:
param_help = definition
else:
param_help = "{:<34}{}".format(definition, p["help"])
param_help = f"{definition:<34}{p['help']}"
click.echo(param_help)
else:
click.echo(
Expand Down
6 changes: 3 additions & 3 deletions papermill/iorw.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ def get_handler(self, path, extensions=None):

if extensions:
if not fnmatch.fnmatch(os.path.basename(path).split('?')[0], '*.*'):
warnings.warn("the file is not specified with any extension : " + os.path.basename(path))
elif not any(fnmatch.fnmatch(os.path.basename(path).split('?')[0], '*' + ext) for ext in extensions):
warnings.warn(f"the file is not specified with any extension : {os.path.basename(path)}")
elif not any(fnmatch.fnmatch(os.path.basename(path).split('?')[0], f"*{ext}") for ext in extensions):
warnings.warn(f"The specified file ({path}) does not end in one of {extensions}")

local_handler = None
Expand Down Expand Up @@ -380,7 +380,7 @@ def read(self, path):
repo_id = splits[4]
ref_id = splits[6]
sub_path = '/'.join(splits[7:])
repo = self._get_client().get_repo(org_id + '/' + repo_id)
repo = self._get_client().get_repo(f"{org_id}/{repo_id}")
content = repo.get_contents(sub_path, ref=ref_id)
return content.decoded_content

Expand Down
9 changes: 4 additions & 5 deletions papermill/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def __init__(
self.etag = etag
if last_modified:
try:
self.last_modified = last_modified.isoformat().split('+')[0] + '.000Z'
self.last_modified = f"{last_modified.isoformat().split('+')[0]}.000Z"
except ValueError:
self.last_modified = last_modified
self.storage_class = storage_class
Expand Down Expand Up @@ -158,14 +158,13 @@ def _bucket_name(self, bucket):
return self._clean(bucket).split('/', 1)[0]

def _clean(self, name):
if name.startswith('s3n:'):
name = 's3:' + name[4:]
name = self._clean_s3(name)
if self._is_s3(name):
return name[5:]
return name

def _clean_s3(self, name):
return 's3:' + name[4:] if name.startswith('s3n:') else name
return f"s3:{name[4:]}" if name.startswith('s3n:') else name

def _get_key(self, name):
if isinstance(name, Key):
Expand Down Expand Up @@ -346,7 +345,7 @@ def cat(
if err:
raise Exception
else:
raise AwsError('Failed to fully read [%s]' % source.name)
raise AwsError(f'Failed to fully read [{source.name}]')

if undecoded:
assert encoding is not None # only time undecoded is set
Expand Down
68 changes: 34 additions & 34 deletions papermill/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,17 @@ def augment_execute_kwargs(self, **new_kwargs):
kwargs.update(new_kwargs)
return kwargs

@patch(cli.__name__ + '.execute_notebook')
@patch(f"{cli.__name__}.execute_notebook")
def test_parameters(self, execute_patch):
self.runner.invoke(papermill, self.default_args + ['-p', 'foo', 'bar', '--parameters', 'baz', '42'])
execute_patch.assert_called_with(**self.augment_execute_kwargs(parameters={'foo': 'bar', 'baz': 42}))

@patch(cli.__name__ + '.execute_notebook')
@patch(f"{cli.__name__}.execute_notebook")
def test_parameters_raw(self, execute_patch):
self.runner.invoke(papermill, self.default_args + ['-r', 'foo', 'bar', '--parameters_raw', 'baz', '42'])
execute_patch.assert_called_with(**self.augment_execute_kwargs(parameters={'foo': 'bar', 'baz': '42'}))

@patch(cli.__name__ + '.execute_notebook')
@patch(f"{cli.__name__}.execute_notebook")
def test_parameters_file(self, execute_patch):
extra_args = ['-f', self.sample_yaml_file, '--parameters_file', self.sample_json_file]
self.runner.invoke(papermill, self.default_args + extra_args)
Expand All @@ -133,20 +133,20 @@ def test_parameters_file(self, execute_patch):
)
)

@patch(cli.__name__ + '.execute_notebook')
@patch(f"{cli.__name__}.execute_notebook")
def test_parameters_yaml(self, execute_patch):
self.runner.invoke(
papermill,
self.default_args + ['-y', '{"foo": "bar"}', '--parameters_yaml', '{"foo2": ["baz"]}'],
)
execute_patch.assert_called_with(**self.augment_execute_kwargs(parameters={'foo': 'bar', 'foo2': ['baz']}))

@patch(cli.__name__ + '.execute_notebook')
@patch(f"{cli.__name__}.execute_notebook")
def test_parameters_yaml_date(self, execute_patch):
self.runner.invoke(papermill, self.default_args + ['-y', 'a_date: 2019-01-01'])
execute_patch.assert_called_with(**self.augment_execute_kwargs(parameters={'a_date': '2019-01-01'}))

@patch(cli.__name__ + '.execute_notebook')
@patch(f"{cli.__name__}.execute_notebook")
def test_parameters_empty(self, execute_patch):
# "#empty" ---base64--> "I2VtcHR5"
with tempfile.TemporaryDirectory() as tmpdir:
Expand All @@ -171,7 +171,7 @@ def test_parameters_empty(self, execute_patch):
)
)

@patch(cli.__name__ + '.execute_notebook')
@patch(f"{cli.__name__}.execute_notebook")
def test_parameters_yaml_override(self, execute_patch):
self.runner.invoke(
papermill,
Expand All @@ -184,15 +184,15 @@ def test_parameters_yaml_override(self, execute_patch):
)
)

@patch(cli.__name__ + '.execute_notebook', side_effect=nbclient.exceptions.DeadKernelError("Fake"))
@patch(f"{cli.__name__}.execute_notebook", side_effect=nbclient.exceptions.DeadKernelError("Fake"))
def test_parameters_dead_kernel(self, execute_patch):
result = self.runner.invoke(
papermill,
self.default_args + ['--parameters_yaml', '{"foo": "bar"}', '-y', '{"foo": ["baz"]}'],
)
assert result.exit_code == 138

@patch(cli.__name__ + '.execute_notebook')
@patch(f"{cli.__name__}.execute_notebook")
def test_parameters_base64(self, execute_patch):
extra_args = [
'--parameters_base64',
Expand All @@ -203,26 +203,26 @@ def test_parameters_base64(self, execute_patch):
self.runner.invoke(papermill, self.default_args + extra_args)
execute_patch.assert_called_with(**self.augment_execute_kwargs(parameters={'foo': 1, 'bar': 2}))

@patch(cli.__name__ + '.execute_notebook')
@patch(f"{cli.__name__}.execute_notebook")
def test_parameters_base64_date(self, execute_patch):
self.runner.invoke(papermill, self.default_args + ['--parameters_base64', 'YV9kYXRlOiAyMDE5LTAxLTAx'])
execute_patch.assert_called_with(**self.augment_execute_kwargs(parameters={'a_date': '2019-01-01'}))

@patch(cli.__name__ + '.execute_notebook')
@patch(f"{cli.__name__}.execute_notebook")
def test_inject_input_path(self, execute_patch):
self.runner.invoke(papermill, self.default_args + ['--inject-input-path'])
execute_patch.assert_called_with(
**self.augment_execute_kwargs(parameters={'PAPERMILL_INPUT_PATH': 'input.ipynb'})
)

@patch(cli.__name__ + '.execute_notebook')
@patch(f"{cli.__name__}.execute_notebook")
def test_inject_output_path(self, execute_patch):
self.runner.invoke(papermill, self.default_args + ['--inject-output-path'])
execute_patch.assert_called_with(
**self.augment_execute_kwargs(parameters={'PAPERMILL_OUTPUT_PATH': 'output.ipynb'})
)

@patch(cli.__name__ + '.execute_notebook')
@patch(f"{cli.__name__}.execute_notebook")
def test_inject_paths(self, execute_patch):
self.runner.invoke(papermill, self.default_args + ['--inject-paths'])
execute_patch.assert_called_with(
Expand All @@ -234,42 +234,42 @@ def test_inject_paths(self, execute_patch):
)
)

@patch(cli.__name__ + '.execute_notebook')
@patch(f"{cli.__name__}.execute_notebook")
def test_engine(self, execute_patch):
self.runner.invoke(papermill, self.default_args + ['--engine', 'engine-that-could'])
execute_patch.assert_called_with(**self.augment_execute_kwargs(engine_name='engine-that-could'))

@patch(cli.__name__ + '.execute_notebook')
@patch(f"{cli.__name__}.execute_notebook")
def test_prepare_only(self, execute_patch):
self.runner.invoke(papermill, self.default_args + ['--prepare-only'])
execute_patch.assert_called_with(**self.augment_execute_kwargs(prepare_only=True))

@patch(cli.__name__ + '.execute_notebook')
@patch(f"{cli.__name__}.execute_notebook")
def test_kernel(self, execute_patch):
self.runner.invoke(papermill, self.default_args + ['-k', 'python3'])
execute_patch.assert_called_with(**self.augment_execute_kwargs(kernel_name='python3'))

@patch(cli.__name__ + '.execute_notebook')
@patch(f"{cli.__name__}.execute_notebook")
def test_language(self, execute_patch):
self.runner.invoke(papermill, self.default_args + ['-l', 'python'])
execute_patch.assert_called_with(**self.augment_execute_kwargs(language='python'))

@patch(cli.__name__ + '.execute_notebook')
@patch(f"{cli.__name__}.execute_notebook")
def test_set_cwd(self, execute_patch):
self.runner.invoke(papermill, self.default_args + ['--cwd', 'a/path/here'])
execute_patch.assert_called_with(**self.augment_execute_kwargs(cwd='a/path/here'))

@patch(cli.__name__ + '.execute_notebook')
@patch(f"{cli.__name__}.execute_notebook")
def test_progress_bar(self, execute_patch):
self.runner.invoke(papermill, self.default_args + ['--progress-bar'])
execute_patch.assert_called_with(**self.augment_execute_kwargs(progress_bar=True))

@patch(cli.__name__ + '.execute_notebook')
@patch(f"{cli.__name__}.execute_notebook")
def test_no_progress_bar(self, execute_patch):
self.runner.invoke(papermill, self.default_args + ['--no-progress-bar'])
execute_patch.assert_called_with(**self.augment_execute_kwargs(progress_bar=False))

@patch(cli.__name__ + '.execute_notebook')
@patch(f"{cli.__name__}.execute_notebook")
def test_log_output(self, execute_patch):
self.runner.invoke(papermill, self.default_args + ['--log-output'])
execute_patch.assert_called_with(
Expand All @@ -279,61 +279,61 @@ def test_log_output(self, execute_patch):
)
)

@patch(cli.__name__ + '.execute_notebook')
@patch(f"{cli.__name__}.execute_notebook")
def test_log_output_plus_progress(self, execute_patch):
self.runner.invoke(papermill, self.default_args + ['--log-output', '--progress-bar'])
execute_patch.assert_called_with(**self.augment_execute_kwargs(log_output=True, progress_bar=True))

@patch(cli.__name__ + '.execute_notebook')
@patch(f"{cli.__name__}.execute_notebook")
def test_no_log_output(self, execute_patch):
self.runner.invoke(papermill, self.default_args + ['--no-log-output'])
execute_patch.assert_called_with(**self.augment_execute_kwargs(log_output=False))

@patch(cli.__name__ + '.execute_notebook')
@patch(f"{cli.__name__}.execute_notebook")
def test_log_level(self, execute_patch):
self.runner.invoke(papermill, self.default_args + ['--log-level', 'WARNING'])
# TODO: this does not actually test log-level being set
execute_patch.assert_called_with(**self.augment_execute_kwargs())

@patch(cli.__name__ + '.execute_notebook')
@patch(f"{cli.__name__}.execute_notebook")
def test_start_timeout(self, execute_patch):
self.runner.invoke(papermill, self.default_args + ['--start-timeout', '123'])
execute_patch.assert_called_with(**self.augment_execute_kwargs(start_timeout=123))

@patch(cli.__name__ + '.execute_notebook')
@patch(f"{cli.__name__}.execute_notebook")
def test_start_timeout_backwards_compatibility(self, execute_patch):
self.runner.invoke(papermill, self.default_args + ['--start_timeout', '123'])
execute_patch.assert_called_with(**self.augment_execute_kwargs(start_timeout=123))

@patch(cli.__name__ + '.execute_notebook')
@patch(f"{cli.__name__}.execute_notebook")
def test_execution_timeout(self, execute_patch):
self.runner.invoke(papermill, self.default_args + ['--execution-timeout', '123'])
execute_patch.assert_called_with(**self.augment_execute_kwargs(execution_timeout=123))

@patch(cli.__name__ + '.execute_notebook')
@patch(f"{cli.__name__}.execute_notebook")
def test_report_mode(self, execute_patch):
self.runner.invoke(papermill, self.default_args + ['--report-mode'])
execute_patch.assert_called_with(**self.augment_execute_kwargs(report_mode=True))

@patch(cli.__name__ + '.execute_notebook')
@patch(f"{cli.__name__}.execute_notebook")
def test_no_report_mode(self, execute_patch):
self.runner.invoke(papermill, self.default_args + ['--no-report-mode'])
execute_patch.assert_called_with(**self.augment_execute_kwargs(report_mode=False))

@patch(cli.__name__ + '.execute_notebook')
@patch(f"{cli.__name__}.execute_notebook")
def test_version(self, execute_patch):
self.runner.invoke(papermill, ['--version'])
execute_patch.assert_not_called()

@patch(cli.__name__ + '.execute_notebook')
@patch(cli.__name__ + '.display_notebook_help')
@patch(f"{cli.__name__}.execute_notebook")
@patch(f"{cli.__name__}.display_notebook_help")
def test_help_notebook(self, display_notebook_help, execute_path):
self.runner.invoke(papermill, ['--help-notebook', 'input_path.ipynb'])
execute_path.assert_not_called()
assert display_notebook_help.call_count == 1
assert display_notebook_help.call_args[0][1] == 'input_path.ipynb'

@patch(cli.__name__ + '.execute_notebook')
@patch(f"{cli.__name__}.execute_notebook")
def test_many_args(self, execute_patch):
extra_args = [
'-f',
Expand Down Expand Up @@ -535,4 +535,4 @@ def test_stdout_file(tmpdir):
assert not err

with open(str(stdout_file)) as fp:
assert fp.read() == secret + '\n'
assert fp.read() == f"{secret}\n"
Loading

0 comments on commit bc9c607

Please sign in to comment.