Use format instead of % for string formatting

As part of keeping Marconi forward compatible, this change replaces
the usage of % for string formatting with .format.

Change-Id: Ie2b1c95f4527339953aa57cceb9b39b0988bef26
This commit is contained in:
Flavio Percoco 2013-09-10 14:50:48 +02:00
parent 98d5487e4c
commit 34f1d68f2e
8 changed files with 39 additions and 35 deletions

View File

@ -62,8 +62,8 @@ Example::
if width == 0 and height == 0 and (color == 'red' or
emphasis is None):
raise ValueError("I don't think so -- values are %s, %s" %
(width, height))
raise ValueError("I don't think so -- values are {0}, {1}".format(
width, height))
msg = ('this is a very long string that goes on and on and on and'
'on and on and on...')
@ -232,7 +232,7 @@ template string then do the replacement.
Example::
msg = _("Missing parameter: %s") % ("flavor",)
msg = _("Missing parameter: {0}").format("flavor",)
LOG.error(msg)
If you have multiple variables to place in the string, use keyword parameters.
@ -240,8 +240,8 @@ This helps our translators reorder parameters when needed.
Example::
msg = _("The server with id %(s_id)s has no key %(m_key)s")
LOG.error(msg % {"s_id": "1234", "m_key": "imageId"})
msg = _("The server with id {s_id} has no key {m_key}")
LOG.error(msg.format(s_id=1234", m_key=imageId"))
Creating Unit Tests

View File

@ -30,10 +30,10 @@ class TestClaims(base.FunctionalTestBase):
super(TestClaims, self).setUp()
self.queue = uuid.uuid1()
self.queue_url = ("%(url)s/%(version)s/queues/%(queue)s" %
{'url': self.cfg.marconi.url,
'version': self.cfg.marconi.version,
'queue': self.queue})
self.queue_url = ("{url}/{version}/queues/{queue}".format(
url=self.cfg.marconi.url,
version=self.cfg.marconi.version,
queue=self.queue))
self.client.put(self.queue_url)

View File

@ -29,10 +29,10 @@ class TestMessages(base.FunctionalTestBase):
super(TestMessages, self).setUp()
self.queue = uuid.uuid1()
self.queue_url = ("%(url)s/%(version)s/queues/%(queue)s" %
{'url': self.cfg.marconi.url,
'version': self.cfg.marconi.version,
'queue': self.queue})
self.queue_url = ("{url}/{version}/queues/{queue}".format(
url=self.cfg.marconi.url,
version=self.cfg.marconi.version,
queue=self.queue))
self.client.put(self.queue_url)

View File

@ -28,8 +28,8 @@ class TestInsertQueue(base.FunctionalTestBase):
def setUp(self):
super(TestInsertQueue, self).setUp()
self.base_url = '%s/%s' % (self.cfg.marconi.url,
self.cfg.marconi.version)
self.base_url = '{0}/{1}'.format(self.cfg.marconi.url,
self.cfg.marconi.version)
self.headers_response_empty = set(['location'])
self.client.set_base_url(self.base_url)
@ -138,8 +138,8 @@ class TestQueueMetaData(base.FunctionalTestBase):
def setUp(self):
super(TestQueueMetaData, self).setUp()
self.base_url = '%s/%s' % (self.cfg.marconi.url,
self.cfg.marconi.version)
self.base_url = '{0}/{1}'.format(self.cfg.marconi.url,
self.cfg.marconi.version)
self.queue_url = self.base_url + '/queues/{0}'.format(uuid.uuid1())
self.client.put(self.queue_url)
@ -187,8 +187,8 @@ class TestQueueMisc(base.FunctionalTestBase):
def setUp(self):
super(TestQueueMisc, self).setUp()
self.base_url = '%s/%s' % (self.cfg.marconi.url,
self.cfg.marconi.version)
self.base_url = '{0}/{1}'.format(self.cfg.marconi.url,
self.cfg.marconi.version)
self.client.set_base_url(self.base_url)

View File

@ -36,8 +36,9 @@ class ControllerBaseTest(testing.TestBase):
self.skipTest('No driver class specified')
if not issubclass(self.controller_class, self.controller_base_class):
self.skipTest('%s is not an instance of %s. Tests not supported' %
(self.controller_class, self.controller_base_class))
self.skipTest('{0} is not an instance of {1}. '
'Tests not supported'.format(
self.controller_class, self.controller_base_class))
self.driver = self.driver_class()
self.controller = self.controller_class(self.driver)
@ -570,7 +571,7 @@ def _insert_fixtures(controller, queue_name, project=None,
yield {
'ttl': ttl,
'body': {
'event': 'Event number %s' % n
'event': 'Event number {0}'.format(n)
}}
controller.post(queue_name, messages(),

View File

@ -193,8 +193,10 @@ class MongodbMessageTests(base.MessageControllerTest):
messages_per_queue = gc_threshold
nogc_messages_per_queue = gc_threshold - 1
projects = ['gc-test-project-%s' % i for i in range(num_projects)]
queue_names = ['gc-test-%s' % i for i in range(num_queues)]
projects = ['gc-test-project-{0}'.format(i)
for i in range(num_projects)]
queue_names = ['gc-test-{0}'.format(i) for i in range(num_queues)]
client_uuid = 'b623c53c-cf75-11e2-84e1-a1187188419e'
messages = [{'ttl': 0, 'body': str(i)}
for i in range(messages_per_queue)]

View File

@ -122,9 +122,9 @@ class QueueLifecycleBaseTest(base.TestBase):
def test_too_much_metadata(self):
self.simulate_put('/v1/queues/fizbat', '7e55e1a7e')
self.assertEquals(self.srmock.status, falcon.HTTP_201)
doc = '{"messages": {"ttl": 600}, "padding": "%s"}'
padding_len = self.wsgi_cfg.metadata_max_length - (len(doc) - 2) + 1
doc = doc % ('x' * padding_len)
doc = '{{"messages": {{"ttl": 600}}, "padding": "{pad}"}}'
padding_len = self.wsgi_cfg.metadata_max_length - (len(doc) - 10) + 1
doc = doc.format(pad='x' * padding_len)
self.simulate_put('/v1/queues/fizbat/metadata', '7e55e1a7e', body=doc)
self.assertEquals(self.srmock.status, falcon.HTTP_400)
@ -132,9 +132,9 @@ class QueueLifecycleBaseTest(base.TestBase):
def test_way_too_much_metadata(self):
self.simulate_put('/v1/queues/fizbat', '7e55e1a7e')
self.assertEquals(self.srmock.status, falcon.HTTP_201)
doc = '{"messages": {"ttl": 600}, "padding": "%s"}'
doc = '{{"messages": {{"ttl": 600}}, "padding": "{pad}"}}'
padding_len = self.wsgi_cfg.metadata_max_length * 100
doc = doc % ('x' * padding_len)
doc = doc.format(pad='x' * padding_len)
self.simulate_put('/v1/queues/fizbat/metadata', '7e55e1a7e', body=doc)
self.assertEquals(self.srmock.status, falcon.HTTP_400)
@ -144,9 +144,9 @@ class QueueLifecycleBaseTest(base.TestBase):
self.assertEquals(self.srmock.status, falcon.HTTP_201)
# Set
doc = '{"messages": {"ttl": 600}, "padding": "%s"}'
doc = '{{"messages": {{"ttl": 600}}, "padding": "{pad}"}}'
padding_len = self.wsgi_cfg.metadata_max_length - (len(doc) - 2)
doc = doc % ('x' * padding_len)
doc = doc.format(pad='x' * padding_len)
self.simulate_put('/v1/queues/fizbat/metadata', '480924', body=doc)
self.assertEquals(self.srmock.status, falcon.HTTP_204)

View File

@ -51,12 +51,13 @@ class ValidationTest(base.TestBase):
# Too long
metadata_size_uplimit = 64
doc_tmpl = '{"Dragon Torc":"%s"}'
doc_tmpl_ws = '{ "Dragon Torc" : "%s" }' # with whitespace
envelop_length = len(doc_tmpl % '')
doc_tmpl = '{{"Dragon Torc":"{0}"}}'
doc_tmpl_ws = '{{ "Dragon Torc" : "{0}" }}' # with whitespace
envelop_length = len(doc_tmpl.format(''))
for tmpl in doc_tmpl, doc_tmpl_ws:
doc = tmpl % ('0' * (metadata_size_uplimit - envelop_length + 1))
gen = '0' * (metadata_size_uplimit - envelop_length + 1)
doc = tmpl.format(gen)
self.simulate_put(self.queue_path + '/metadata',
self.project_id,
body=doc)