Python 3: use six.iteritems and six.string_types

six.iteritems() replaces dictionary.iteritems() on Python 2 and
dictionary.items() on Python 3.

six.string_types replaces basestring() in Python 2 and str
in Python 3.

Change-Id: Ia18510d167df35caec83626718010228e2140bc0
This commit is contained in:
Yassine Lamgarchal 2014-01-10 11:13:21 +01:00
parent 47de9cfd89
commit 02b00b6226
4 changed files with 11 additions and 9 deletions

View File

@ -162,8 +162,8 @@ class HTTPClient(object):
:returns: Dictionary with encoded headers'
names and values
"""
to_str = strutils.safe_encode
return dict([(to_str(h), to_str(v)) for h, v in headers.iteritems()])
return dict((strutils.safe_encode(h), strutils.safe_encode(v))
for h, v in six.iteritems(headers))
def _http_request(self, url, method, **kwargs):
"""Send an http request with the specified characteristics.
@ -178,7 +178,7 @@ class HTTPClient(object):
kwargs['headers'].setdefault('X-Auth-Token', self.auth_token)
if self.identity_headers:
for k, v in self.identity_headers.iteritems():
for k, v in six.iteritems(self.identity_headers):
kwargs['headers'].setdefault(k, v)
self.log_curl_request(method, url, kwargs)

View File

@ -20,6 +20,8 @@ import os
import sys
import uuid
import six
if os.name == 'nt':
import msvcrt
else:
@ -63,7 +65,7 @@ def schema_args(schema_getter, omit=[]):
kwargs))
else:
properties = schema.get('properties', {})
for name, property in properties.iteritems():
for name, property in six.iteritems(properties):
if name in omit:
continue
param = '--' + name.replace('_', '-')
@ -123,7 +125,7 @@ def print_dict(d, max_column_width=80):
pt = prettytable.PrettyTable(['Property', 'Value'], caching=False)
pt.align = 'l'
pt.max_width = max_column_width
[pt.add_row(list(r)) for r in d.iteritems()]
[pt.add_row(list(r)) for r in six.iteritems(d)]
print(strutils.safe_encode(pt.get_string(sortby='Property')))

View File

@ -83,7 +83,7 @@ class ImageManager(base.Manager):
# headers will be encoded later, before the
# request is sent.
def to_str(value):
if not isinstance(value, basestring):
if not isinstance(value, six.string_types):
return str(value)
return value
@ -162,7 +162,7 @@ class ImageManager(base.Manager):
owner = qp.pop('owner', None)
for param, value in six.iteritems(qp):
if isinstance(value, basestring):
if isinstance(value, six.string_types):
# Note(flaper87) Url encoding should
# be moved inside http utils, at least
# shouldn't be here.

View File

@ -57,11 +57,11 @@ class Controller(object):
tags_url_params = []
for tag in tags:
if isinstance(tag, basestring):
if isinstance(tag, six.string_types):
tags_url_params.append({'tag': strutils.safe_encode(tag)})
for param, value in six.iteritems(filters):
if isinstance(value, basestring):
if isinstance(value, six.string_types):
filters[param] = strutils.safe_encode(value)
url = '/v2/images?%s' % urllib.urlencode(filters)