From d308b948968d290c4b9e003deb1378c259d71ca7 Mon Sep 17 00:00:00 2001 From: Haiwei Xu Date: Wed, 16 Dec 2015 18:23:59 +0900 Subject: [PATCH] Replace inner methods with lambda function Some inner methods can be simplified by using lambda function. Change-Id: I6e9286fe9346e35c3852e2bdb945783ac121d2b2 --- senlinclient/tests/unit/v1/test_shell.py | 38 ++++----------------- senlinclient/v1/shell.py | 43 ++++++++---------------- 2 files changed, 20 insertions(+), 61 deletions(-) diff --git a/senlinclient/tests/unit/v1/test_shell.py b/senlinclient/tests/unit/v1/test_shell.py index ab9f91b6..ef6fbd18 100644 --- a/senlinclient/tests/unit/v1/test_shell.py +++ b/senlinclient/tests/unit/v1/test_shell.py @@ -107,8 +107,6 @@ class ShellTest(testtools.TestCase): @mock.patch.object(utils, 'print_list') def test_do_profile_list(self, mock_print): client = mock.Mock() - short_id = mock.Mock() - sh._short_id = short_id profiles = mock.Mock() client.profiles.return_value = profiles fields = ['id', 'name', 'type', 'created_time'] @@ -116,27 +114,16 @@ class ShellTest(testtools.TestCase): 'show_deleted': False, 'limit': 20, 'marker': 'mark_id', - 'full_id': True - } - queries = { - 'show_deleted': False, - 'limit': 20, - 'marker': 'mark_id', } + queries = copy.deepcopy(args) formatters = {} args = self._make_args(args) + args.full_id = True sh.do_profile_list(client, args) client.profiles.assert_called_once_with(**queries) mock_print.assert_called_with(profiles, fields, formatters=formatters, sortby_index=1) - # short_id is requested - args.full_id = False - sh.do_profile_list(client, args) - formatters = {'id': short_id} - mock_print.assert_called_with(profiles, fields, formatters=formatters, - sortby_index=1) - @mock.patch.object(utils, 'nested_dict_formatter') @mock.patch.object(utils, 'print_dict') def test_show_profile(self, mock_print, mock_dict): @@ -357,15 +344,6 @@ class ShellTest(testtools.TestCase): mock_print.assert_called_with(webhooks, fields, formatters=formatters, sortby_index=1) - # short_id is requested - args.full_id = False - short_id = mock.Mock() - sh._short_id = short_id - formatters = {'id': short_id} - sh.do_webhook_list(client, args) - mock_print.assert_called_with(webhooks, fields, - formatters=formatters, - sortby_index=1) @mock.patch.object(utils, 'print_dict') def test_show_webhook(self, mock_print): @@ -457,7 +435,7 @@ class ShellTest(testtools.TestCase): 'show_deleted': True, 'limit': 20, 'marker': 'fake_id', - 'full_id': False + 'full_id': True } args = self._make_args(args) queries = { @@ -468,9 +446,7 @@ class ShellTest(testtools.TestCase): } policies = mock.Mock() client.policies.return_value = policies - short_id = mock.Mock() - sh._short_id = short_id - formatters = {'id': short_id} + formatters = {} sh.do_policy_list(client, args) client.policies.assert_called_once_with(**queries) mock_print.assert_called_once_with( @@ -603,10 +579,8 @@ class ShellTest(testtools.TestCase): args = self._make_args(args) clusters = mock.Mock() client.clusters.return_value = clusters - args.full_id = False - short_id = mock.Mock() - sh._short_id = short_id - formatters = {'id': short_id} + args.full_id = True + formatters = {} sh.do_cluster_list(client, args) client.clusters.assert_called_once_with(**queries) mock_print.assert_called_once_with(clusters, fields, diff --git a/senlinclient/v1/shell.py b/senlinclient/v1/shell.py index e34995a9..c4c1a2d1 100644 --- a/senlinclient/v1/shell.py +++ b/senlinclient/v1/shell.py @@ -69,10 +69,6 @@ def do_profile_type_show(sc, args): print(utils.format_output(pt)) -def _short_id(obj): - return obj.id[:8] - - # PROFILES @@ -97,7 +93,7 @@ def do_profile_list(sc, args=None): formatters = {} if not args.full_id: formatters = { - 'id': _short_id, + 'id': lambda x: x.id[:8], } utils.print_list(profiles, fields, formatters=formatters, sortby_index=1) @@ -268,7 +264,7 @@ def do_webhook_list(sc, args=None): formatters = {} if not args.full_id: formatters = { - 'id': _short_id, + 'id': lambda x: x.id[:8] } utils.print_list(webhooks, fields, formatters=formatters, sortby_index=1) @@ -378,7 +374,7 @@ def do_policy_list(sc, args=None): formatters = {} if not args.full_id: formatters = { - 'id': _short_id, + 'id': lambda x: x.id[:8] } utils.print_list(policies, fields, formatters=formatters, sortby_index=1) @@ -532,7 +528,7 @@ def do_cluster_list(sc, args=None): formatters = {} if not args.full_id: formatters = { - 'id': _short_id, + 'id': lambda x: x.id[:8] } utils.print_list(clusters, fields, formatters=formatters, sortby_index=sortby_index) @@ -663,8 +659,6 @@ def do_cluster_show(sc, args): help=_('Name or ID of cluster to nodes from.')) def do_cluster_node_list(sc, args): """List nodes from cluster.""" - def _short_physical_id(obj): - return obj.physical_id[:8] if obj.physical_id else '' queries = { 'cluster_id': args.id, @@ -683,8 +677,8 @@ def do_cluster_node_list(sc, args): if not args.full_id: formatters = { - 'id': _short_id, - 'physical_id': _short_physical_id, + 'id': lambda x: x.id[:8], + 'physical_id': lambda x: x.physical_id[:8] if x.physical_id else '' } else: formatters = {} @@ -871,7 +865,7 @@ def do_cluster_policy_list(sc, args): formatters = {} if not args.full_id: formatters = { - 'policy_id': _short_id, + 'policy_id': lambda x: x.id[:8] } utils.print_list(policies, fields, formatters=formatters, @@ -1009,11 +1003,6 @@ def do_cluster_policy_disable(sc, args): help=_('Print full IDs in list.')) def do_node_list(sc, args): """Show list of nodes.""" - def _short_cluster_id(obj): - return obj.cluster_id[:8] if obj.cluster_id else '' - - def _short_physical_id(obj): - return obj.physical_id[:8] if obj.physical_id else '' fields = ['id', 'name', 'status', 'cluster_id', 'physical_id', 'profile_name', 'created_time', 'updated_time'] @@ -1048,9 +1037,9 @@ def do_node_list(sc, args): if not args.full_id: formatters = { - 'id': _short_id, - 'cluster_id': _short_cluster_id, - 'physical_id': _short_physical_id, + 'id': lambda x: x.id[:8], + 'cluster_id': lambda x: x.cluster_id[:8] if x.cluster_id else '', + 'physical_id': lambda x: x.physical_id[:8] if x.physical_id else '' } else: formatters = {} @@ -1212,8 +1201,6 @@ def do_node_leave(sc, args): help=_('Print full IDs in list.')) def do_event_list(sc, args): """List events.""" - def _short_obj_id(obj): - return obj.obj_id[:8] if obj.obj_id else '' fields = ['id', 'timestamp', 'obj_type', 'obj_id', 'action', 'status', 'status_reason'] @@ -1241,8 +1228,8 @@ def do_event_list(sc, args): formatters = {} if not args.full_id: - formatters['id'] = _short_id - formatters['obj_id'] = _short_obj_id + formatters['id'] = lambda x: x.id[:8] + formatters['obj_id'] = lambda x: x.obj_id[:8] if x.obj_id else '' events = sc.events(**queries) utils.print_list(events, fields, formatters=formatters, @@ -1283,8 +1270,6 @@ def do_event_show(sc, args): help=_('Print full IDs in list.')) def do_action_list(sc, args): """List actions.""" - def _short_target(obj): - return obj.target[:8] if obj.target else '' def _fmt_depends_on(obj): if not obj.depends_on: @@ -1334,8 +1319,8 @@ def do_action_list(sc, args): 'depended_by': _fmt_depended_by } if not args.full_id: - formatters['id'] = _short_id - formatters['target'] = _short_target + formatters['id'] = lambda x: x.id[:8] + formatters['target'] = lambda x: x.target[:8] if x.target else '' utils.print_list(actions, fields, formatters=formatters, sortby_index=sortby_index)