<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>zerodivisible.com</title>
	<atom:link href="http://zerodivisible.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://zerodivisible.com</link>
	<description></description>
	<lastBuildDate>Sat, 14 Apr 2012 09:37:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Python + Redis: TypeError: an integer is required.</title>
		<link>http://zerodivisible.com/2012/04/python-redis-typeerror-an-integer-is-required/</link>
		<comments>http://zerodivisible.com/2012/04/python-redis-typeerror-an-integer-is-required/#comments</comments>
		<pubDate>Sat, 14 Apr 2012 09:29:35 +0000</pubDate>
		<dc:creator>zeroDi</dc:creator>
				<category><![CDATA[gotchas]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[redis]]></category>
		<category><![CDATA[redis-py]]></category>
		<category><![CDATA[TypeError]]></category>

		<guid isPermaLink="false">http://zerodivisible.com/?p=44</guid>
		<description><![CDATA[Today, while trying to write a simple python script to work with Redis (using the redis-py client), I had stumbled across a silly, yet time consuming error. I had written simple RedisConnector class, which is working as a wrapper around the &#8230; <a href="http://zerodivisible.com/2012/04/python-redis-typeerror-an-integer-is-required/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Today, while trying to write a simple python script to work with Redis (using the <a title="redis-py" href="https://github.com/andymccurdy/redis-py" target="_blank">redis-py</a> client), I had stumbled across a silly, yet time consuming error.</p>
<p>I had written simple <code>RedisConnector</code> class, which is working as a wrapper around the official redis-py client:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> redis
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> RedisConnector<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
&nbsp;
   <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, host=<span style="color: #483d8b;">'localhost'</span>, port=<span style="color: #483d8b;">'6379'</span><span style="color: black;">&#41;</span>:
      <span style="color: #008000;">super</span><span style="color: black;">&#40;</span>RedisConnector, <span style="color: #008000;">self</span><span style="color: black;">&#41;</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
      <span style="color: #008000;">self</span>._host = host
      <span style="color: #008000;">self</span>._port = port
      <span style="color: #008000;">self</span>.<span style="color: black;">r</span> = <span style="color: #008000;">False</span>
      <span style="color: #008000;">self</span>.<span style="color: black;">connect</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
   <span style="color: #ff7700;font-weight:bold;">def</span> connect<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
      <span style="color: #008000;">self</span>.<span style="color: black;">r</span> = redis.<span style="color: black;">StrictRedis</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">host</span>, <span style="color: #008000;">self</span>.<span style="color: black;">port</span>, db=<span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
      <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">self</span>.<span style="color: black;">r</span> <span style="color: #66cc66;">!</span>= <span style="color: #008000;">False</span>:
         <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">True</span>
      <span style="color: #ff7700;font-weight:bold;">else</span>:
         <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">False</span></pre></div></div>

<div>As it appears to be ok, there is one and very confusing error there, which will block you from doing any communication with redis. <code>self._port </code>needs to be an integer, not a string, otherwise you will be getting an error:</p>
<pre>TypeError: an integer is required</pre>
</div>
<div>The correct version of this snippet looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> redis
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> RedisConnector<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
&nbsp;
   <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, host=<span style="color: #483d8b;">'localhost'</span>, port=<span style="color: #ff4500;">6379</span><span style="color: black;">&#41;</span>:
      <span style="color: #008000;">super</span><span style="color: black;">&#40;</span>RedisConnector, <span style="color: #008000;">self</span><span style="color: black;">&#41;</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
      <span style="color: #008000;">self</span>._host = host
      <span style="color: #008000;">self</span>._port = port
      <span style="color: #008000;">self</span>.<span style="color: black;">r</span> = <span style="color: #008000;">False</span>
      <span style="color: #008000;">self</span>.<span style="color: black;">connect</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
   <span style="color: #ff7700;font-weight:bold;">def</span> connect<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
      <span style="color: #008000;">self</span>.<span style="color: black;">r</span> = redis.<span style="color: black;">StrictRedis</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">host</span>, <span style="color: #008000;">self</span>.<span style="color: black;">port</span>, db=<span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
      <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">self</span>.<span style="color: black;">r</span> <span style="color: #66cc66;">!</span>= <span style="color: #008000;">False</span>:
         <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">True</span>
      <span style="color: #ff7700;font-weight:bold;">else</span>:
         <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">False</span></pre></div></div>

</div>
]]></content:encoded>
			<wfw:commentRss>http://zerodivisible.com/2012/04/python-redis-typeerror-an-integer-is-required/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

