Merge ~remyleone/cloud-init:dict_litteral into cloud-init:master

Proposed by Rémy Léone
Status: Work in progress
Proposed branch: ~remyleone/cloud-init:dict_litteral
Merge into: cloud-init:master
Diff against target: 130 lines (+13/-25)
7 files modified
cloudinit/config/cc_phone_home.py (+2/-4)
cloudinit/distros/net_util.py (+1/-2)
cloudinit/netinfo.py (+1/-3)
cloudinit/sources/DataSourceAzure.py (+1/-2)
cloudinit/sources/DataSourceEc2.py (+1/-2)
cloudinit/sources/DataSourceGCE.py (+2/-3)
cloudinit/sources/helpers/openstack.py (+5/-9)
Reviewer Review Type Date Requested Status
Scott Moser Needs Fixing
Server Team CI bot continuous-integration Needs Fixing
Review via email: mp+340238@code.launchpad.net

Commit message

Change some dict creation and population to literal.

This will provide a small performance improvement and shorter code.

To post a comment you must log in.
Revision history for this message
Scott Moser (smoser) wrote :

I'm curious, is there any benefit to this other than shorter code?

Revision history for this message
Rémy Léone (remyleone) wrote :

Yes, it does. Faster execution

Le 1 mars 2018 8:00 PM, "Scott Moser" <email address hidden> a écrit :

> I'm curious, is there any benefit to this other than shorter code?
>
> --
> https://code.launchpad.net/~remy-leone/cloud-init/+git/
> cloud-init/+merge/340238
> You are the owner of ~remy-leone/cloud-init:dict_litteral.
>

Revision history for this message
Scott Moser (smoser) :
review: Approve
Revision history for this message
Scott Moser (smoser) wrote :

$ python3 -m timeit "all_keys = {}; all_keys['instance_id'] = 9; all_keys['hostname'] = 'foo'; all_keys['fqdn'] = 'my.fqdn'"
10000000 loops, best of 3: 0.146 usec per loop
$ python3 -m timeit "all_keys = {'instance_id': 9, 'hostname': 'foo', 'fqdn': 'my.fqdn'};"
10000000 loops, best of 3: 0.109 usec per loop

Revision history for this message
Server Team CI bot (server-team-bot) wrote :

FAILED: Continuous integration, rev:
https://jenkins.ubuntu.com/server/job/cloud-init-ci/808/
Executed test runs:
    FAILED: Checkout

Click here to trigger a rebuild:
https://jenkins.ubuntu.com/server/job/cloud-init-ci/808/rebuild

review: Needs Fixing (continuous-integration)
Revision history for this message
Server Team CI bot (server-team-bot) wrote :

FAILED: Continuous integration, rev:
https://jenkins.ubuntu.com/server/job/cloud-init-ci/809/
Executed test runs:
    SUCCESS: Checkout
    FAILED: Unit & Style Tests

Click here to trigger a rebuild:
https://jenkins.ubuntu.com/server/job/cloud-init-ci/809/rebuild

review: Needs Fixing (continuous-integration)
Revision history for this message
Scott Moser (smoser) wrote :

fix the flake8 issues per c-i and we can pull.

flake8 runtests: commands[0] | /var/lib/jenkins/slaves/torkoal/workspace/cloud-init-ci/.tox/flake8/bin/python -m flake8 cloudinit/ tests/ tools/
cloudinit/config/cc_phone_home.py:98:80: E501 line too long (89 > 79 characters)
cloudinit/sources/DataSourceGCE.py:165:80: E501 line too long (82 > 79 characters)
cloudinit/sources/DataSourceAzure.py:339:80: E501 line too long (81 > 79 characters)
cloudinit/sources/DataSourceEc2.py:539:80: E501 line too long (91 > 79 characters)

You can verify locally with:
 tox -e flake8

or run all the c-i with
 tox

review: Needs Fixing
Revision history for this message
Scott Moser (smoser) wrote :

I'm going to mark this work in progress.
Please fix it up and set it back to 'Needs Review'.
Thanks.

Unmerged commits

80ce710... by =?utf-8?b?UsOpbXkgTMOpb25l?= <email address hidden>

Fix dict creation that could be litteral

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/cloudinit/config/cc_phone_home.py b/cloudinit/config/cc_phone_home.py
2index 878069b..d838707 100644
3--- a/cloudinit/config/cc_phone_home.py
4+++ b/cloudinit/config/cc_phone_home.py
5@@ -95,10 +95,8 @@ def handle(name, cfg, cloud, log, args):
6 if post_list == "all":
7 post_list = POST_LIST_ALL
8
9- all_keys = {}
10- all_keys['instance_id'] = cloud.get_instance_id()
11- all_keys['hostname'] = cloud.get_hostname()
12- all_keys['fqdn'] = cloud.get_hostname(fqdn=True)
13+ all_keys = {'instance_id': cloud.get_instance_id(), 'hostname': cloud.get_hostname(),
14+ 'fqdn': cloud.get_hostname(fqdn=True)}
15
16 pubkeys = {
17 'pub_key_dsa': '/etc/ssh/ssh_host_dsa_key.pub',
18diff --git a/cloudinit/distros/net_util.py b/cloudinit/distros/net_util.py
19index 1ce1aa7..d9d827c 100644
20--- a/cloudinit/distros/net_util.py
21+++ b/cloudinit/distros/net_util.py
22@@ -113,8 +113,7 @@ def translate_network(settings):
23 dev_name = dev
24 if not dev_name:
25 continue
26- iface_info = {}
27- iface_info['ipv6'] = {}
28+ iface_info = {'ipv6': {}}
29 if len(iface_details) >= 3:
30 proto_type = iface_details[2].strip().lower()
31 # Seems like this can be 'loopback' which we don't
32diff --git a/cloudinit/netinfo.py b/cloudinit/netinfo.py
33index 993b26c..f447bad 100644
34--- a/cloudinit/netinfo.py
35+++ b/cloudinit/netinfo.py
36@@ -87,9 +87,7 @@ def netdev_info(empty=""):
37 def route_info():
38 (route_out, _err) = util.subp(["netstat", "-rn"], rcs=[0, 1])
39
40- routes = {}
41- routes['ipv4'] = []
42- routes['ipv6'] = []
43+ routes = {'ipv4': [], 'ipv6': []}
44
45 entries = route_out.splitlines()[1:]
46 for line in entries:
47diff --git a/cloudinit/sources/DataSourceAzure.py b/cloudinit/sources/DataSourceAzure.py
48index 4bcbf3a..da81ad1 100644
49--- a/cloudinit/sources/DataSourceAzure.py
50+++ b/cloudinit/sources/DataSourceAzure.py
51@@ -336,8 +336,7 @@ class DataSourceAzure(sources.DataSource):
52 if len(missing):
53 LOG.warning("Did not find files, but going on: %s", missing)
54
55- metadata = {}
56- metadata['public-keys'] = key_value or pubkeys_from_crt_files(fp_files)
57+ metadata = {'public-keys': key_value or pubkeys_from_crt_files(fp_files)}
58 return metadata
59
60 def _get_data(self):
61diff --git a/cloudinit/sources/DataSourceEc2.py b/cloudinit/sources/DataSourceEc2.py
62index 21e9ef8..bb331fb 100644
63--- a/cloudinit/sources/DataSourceEc2.py
64+++ b/cloudinit/sources/DataSourceEc2.py
65@@ -536,8 +536,7 @@ def convert_ec2_metadata_network_config(network_md, macs_to_nics=None,
66 nic_metadata = macs_metadata.get(mac)
67 if not nic_metadata:
68 continue # Not a physical nic represented in metadata
69- nic_cfg = {'type': 'physical', 'name': nic_name, 'subnets': []}
70- nic_cfg['mac_address'] = mac
71+ nic_cfg = {'type': 'physical', 'name': nic_name, 'subnets': [], 'mac_address': mac}
72 if (nic_name == fallback_nic or nic_metadata.get('public-ipv4s') or
73 nic_metadata.get('local-ipv4s')):
74 nic_cfg['subnets'].append({'type': 'dhcp4'})
75diff --git a/cloudinit/sources/DataSourceGCE.py b/cloudinit/sources/DataSourceGCE.py
76index 2da34a9..4c91191 100644
77--- a/cloudinit/sources/DataSourceGCE.py
78+++ b/cloudinit/sources/DataSourceGCE.py
79@@ -162,9 +162,8 @@ def read_md(address=None, platform_check=True):
80 if address is None:
81 address = MD_V1_URL
82
83- ret = {'meta-data': None, 'user-data': None,
84- 'success': False, 'reason': None}
85- ret['platform_reports_gce'] = platform_reports_gce()
86+ ret = {'meta-data': None, 'user-data': None, 'success': False, 'reason': None,
87+ 'platform_reports_gce': platform_reports_gce()}
88
89 if platform_check and not ret['platform_reports_gce']:
90 ret['reason'] = "Not running on GCE."
91diff --git a/cloudinit/sources/helpers/openstack.py b/cloudinit/sources/helpers/openstack.py
92index 26f3168..7900572 100644
93--- a/cloudinit/sources/helpers/openstack.py
94+++ b/cloudinit/sources/helpers/openstack.py
95@@ -215,30 +215,26 @@ class BaseReader(object):
96 util.load_json, root_types=(dict, list) + six.string_types)
97
98 def datafiles(version):
99- files = {}
100- files['metadata'] = (
101+ files = {'metadata': (
102 # File path to read
103 self._path_join("openstack", version, 'meta_data.json'),
104 # Is it required?
105 True,
106 # Translator function (applied after loading)
107 util.load_json,
108- )
109- files['userdata'] = (
110+ ), 'userdata': (
111 self._path_join("openstack", version, 'user_data'),
112 False,
113 lambda x: x,
114- )
115- files['vendordata'] = (
116+ ), 'vendordata': (
117 self._path_join("openstack", version, 'vendor_data.json'),
118 False,
119 load_json_anytype,
120- )
121- files['networkdata'] = (
122+ ), 'networkdata': (
123 self._path_join("openstack", version, 'network_data.json'),
124 False,
125 load_json_anytype,
126- )
127+ )}
128 return files
129
130 results = {

Subscribers

People subscribed via source and target branches