From 3e0eed49c5c7e38c906e6c31b948d273e3fa91be Mon Sep 17 00:00:00 2001 From: Stephen Finucane Date: Wed, 18 May 2022 12:42:10 +0100 Subject: [PATCH] Defer loading PyYAML Yet another library that's slow to import and is totally optional. Defer loading this one also and speed up initial start time. Change-Id: Ic694b4d36dbf7ce87bc8fe9a2f8b0597719418a1 Signed-off-by: Stephen Finucane --- cliff/formatters/yaml_format.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/cliff/formatters/yaml_format.py b/cliff/formatters/yaml_format.py index 8b1e64db..71d4906b 100644 --- a/cliff/formatters/yaml_format.py +++ b/cliff/formatters/yaml_format.py @@ -13,8 +13,6 @@ """Output formatters using PyYAML. """ -import yaml - from . import base from cliff import columns @@ -25,6 +23,9 @@ class YAMLFormatter(base.ListFormatter, base.SingleFormatter): pass def emit_list(self, column_names, data, stdout, parsed_args): + # the yaml import is slow, so defer loading until we know we want it + import yaml + items = [] for item in data: items.append( @@ -36,6 +37,9 @@ class YAMLFormatter(base.ListFormatter, base.SingleFormatter): yaml.safe_dump(items, stream=stdout, default_flow_style=False) def emit_one(self, column_names, data, stdout, parsed_args): + # the yaml import is slow, so defer loading until we know we want it + import yaml + for key, value in zip(column_names, data): dict_data = { key: (value.machine_readable()