summaryrefslogtreecommitdiff
path: root/redis/utils.py
diff options
context:
space:
mode:
authorKenneth Reitz <me@kennethreitz.com>2012-05-18 15:02:24 -0700
committerKenneth Reitz <me@kennethreitz.com>2012-05-18 15:02:24 -0700
commite57bbd1278631f89395102652193b53f9826d5cd (patch)
treed6749c35e48cce0b4c5b1188e9201e0380d9e07e /redis/utils.py
parent2eb518a7c53702168f5e987c1741512ac5e9b4d7 (diff)
downloadredis-py-e57bbd1278631f89395102652193b53f9826d5cd.tar.gz
redis.from_url
Diffstat (limited to 'redis/utils.py')
-rw-r--r--redis/utils.py27
1 files changed, 27 insertions, 0 deletions
diff --git a/redis/utils.py b/redis/utils.py
new file mode 100644
index 0000000..4f116a7
--- /dev/null
+++ b/redis/utils.py
@@ -0,0 +1,27 @@
+from urlparse import urlparse
+
+from .client import Redis
+
+DEFAULT_DATABASE_ID = 0
+
+def from_url(url, db=None):
+ """Returns an active Redis client generated from the given database URL.
+
+ Will attempt to extract the database id from the path url fragment, if
+ none is provided.
+ """
+
+ url = urlparse(url)
+
+ # Attempt to resolve database id.
+ if db is None:
+ try:
+ db = int(url.path.replace('/'))
+ except (AttributeError, ValueError):
+ db = DEFAULT_DATABASE_ID
+
+ return Redis(
+ host=url.hostname,
+ port=url.port,
+ db=db,
+ password=url.password) \ No newline at end of file