[ovs-dev] [python 23/31] ovs.json: Optimize __dump_string().

Reid Price reid at nicira.com
Wed Aug 24 07:18:53 UTC 2011


On Tue, Aug 23, 2011 at 2:05 PM, Ben Pfaff <blp at nicira.com> wrote:

> Suggested-by: Reid Price <reid at nicira.com>
> ---
>  python/ovs/json.py |   10 +---------
>  1 files changed, 1 insertions(+), 9 deletions(-)
>
> diff --git a/python/ovs/json.py b/python/ovs/json.py
> index 67470fc..6362830 100644
> --- a/python/ovs/json.py
> +++ b/python/ovs/json.py
> @@ -28,15 +28,7 @@ for i in range(32):
>         escapes[i] = u"\\u%04x" % i
>
>  def __dump_string(stream, s):
> -    stream.write(u"\"")
> -    for c in s:
> -        x = ord(c)
> -        escape = escapes.get(x)
> -        if escape:
> -            stream.write(escape)
> -        else:
> -            stream.write(c)
> -    stream.write(u"\"")
> +    stream.write(u'"%s"' % ''.join([escapes.get(ord(c), c) for c in s]))
>
You can drop the [] here as well.  This will use an iterator instead,
which avoids the construction of an intermediate list.

   stream.write(u'"%s"' % ''.join(escapes.get(ord(c), c) for c in s))

Another thing in passing, could use the unicode cast here, which
ends up being about the same number of characters

   stream.write(unicode(''.join(escapes.get(ord(c), c) for c in s)))


>  def to_stream(obj, stream, pretty=False, sort_keys=True):
>     if obj is None:
> --
> 1.7.4.4
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openvswitch.org/pipermail/ovs-dev/attachments/20110824/a88156c6/attachment-0003.html>


More information about the dev mailing list