From 2f772ba93d887a4c3b6cc5256168c7ab48e89409 Mon Sep 17 00:00:00 2001 From: Dan Colish Date: Mon, 14 Mar 2011 16:53:57 -0400 Subject: Use itertools izip and islice to speed up pairs_to_dict and other zips --- redis/client.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/redis/client.py b/redis/client.py index b091e23..2c7e404 100644 --- a/redis/client.py +++ b/redis/client.py @@ -2,7 +2,7 @@ import datetime import threading import time import warnings -from itertools import chain, imap +from itertools import chain, imap, islice, izip from redis.connection import ConnectionPool, Connection from redis.exceptions import ConnectionError, ResponseError, WatchError from redis.exceptions import RedisError, AuthenticationError @@ -75,7 +75,8 @@ def parse_info(response): def pairs_to_dict(response): "Create a dict given a list of key/value pairs" - return dict(zip(response[::2], response[1::2])) + return dict(izip(islice(response, None, None, 2), + islice(response, 1, None, 2))) def zset_score_pairs(response, **options): """ @@ -84,7 +85,8 @@ def zset_score_pairs(response, **options): """ if not response or not options['withscores']: return response - return zip(response[::2], map(float, response[1::2])) + return list(izip(islice(response, None, None, 2), + imap(float, islice(response, 1, None, 2)))) def int_or_none(response): if response is None: @@ -1300,7 +1302,7 @@ class Pipeline(Redis): "pipeline execution") # Run any callbacks for the commands run in the pipeline data = [] - for r, cmd in zip(response, commands): + for r, cmd in izip(response, commands): if not isinstance(r, Exception): if cmd[0] in self.RESPONSE_CALLBACKS: r = self.RESPONSE_CALLBACKS[cmd[0]](r, **cmd[2]) -- cgit v1.2.1