<?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>Mad Python</title>
	<atom:link href="http://blog.madpython.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.madpython.com</link>
	<description>Watch out.. he's angry</description>
	<lastBuildDate>Thu, 08 Mar 2012 16:29:46 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Fuzzy Mathematics with FuzzPy (Part 2)</title>
		<link>http://blog.madpython.com/2010/11/03/fuzzy-mathematics-with-fuzzpy-part-2/</link>
		<comments>http://blog.madpython.com/2010/11/03/fuzzy-mathematics-with-fuzzpy-part-2/#comments</comments>
		<pubDate>Wed, 03 Nov 2010 13:00:24 +0000</pubDate>
		<dc:creator>Xavier</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[tech]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[CS]]></category>
		<category><![CDATA[engineering]]></category>
		<category><![CDATA[mathematics]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[scripting]]></category>

		<guid isPermaLink="false">http://blog.madpython.com/?p=187</guid>
		<description><![CDATA[In the first part of this post, we explored the foundations of fuzzy sets and fuzzy graphs, and discussed how you can use FuzzPy, a general fuzzy mathematics library for python, to work with these types. Today, we will expand &#8230; <a href="http://blog.madpython.com/2010/11/03/fuzzy-mathematics-with-fuzzpy-part-2/">Continue reading <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href='http://blog.madpython.com/2010/10/17/fuzzy-mathematics-with-fuzzpy-part-1/' rel='bookmark' title='Fuzzy Mathematics with FuzzPy (Part 1)'>Fuzzy Mathematics with FuzzPy (Part 1)</a></li>
<li><a href='http://blog.madpython.com/2010/08/07/algorithms-in-python-binary-exponentiation/' rel='bookmark' title='Algorithms in Python: Binary exponentiation'>Algorithms in Python: Binary exponentiation</a></li>
<li><a href='http://blog.madpython.com/2010/07/17/algorithms-in-python-binary-operations/' rel='bookmark' title='Algorithms in Python: Binary Operations'>Algorithms in Python: Binary Operations</a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p>In the <a href="http://blog.madpython.com/2010/10/17/fuzzy-mathematics-with-fuzzpy-part-1/" onclick="return TrackClick('http%3A%2F%2Fblog.madpython.com%2F2010%2F10%2F17%2Ffuzzy-mathematics-with-fuzzpy-part-1%2F','first+part+of+this+post')">first part of this post</a>, we explored the foundations of fuzzy sets and fuzzy graphs, and discussed how you can use FuzzPy, a general fuzzy mathematics library for python, to work with these types. Today, we will expand a bit on those foundations by learning about fuzzy numbers, and creating visualizations with FuzzPy.</p>
<h2>Fuzzy Numbers</h2>
<p>Think of a fuzzy number as a number whose actual value is uncertain. We do not have its value, but we have a set of estimations, each with an associated degree of likelihood, or <code>grade</code>. If you remember the definition of a fuzzy set, you will remember that the structure of a fuzzy set is very similar to what I just described, so it only makes sense to use a fuzzy set to represent what is known about a fuzzy number.</p>
<p>Effectively, a fuzzy number is a fuzzy subset of the real line <img src="//s0.wp.com/latex.php?latex=%5Cmathbb%7BR%7D&#038;bg=ffffff&#038;fg=000&#038;s=0" alt="&#92;mathbb{R}" title="&#92;mathbb{R}" class="latex" />, where the value of each member of the set is an estimated value of the fuzzy number, and the <img src="//s0.wp.com/latex.php?latex=%5Cmu&#038;bg=ffffff&#038;fg=000&#038;s=0" alt="&#92;mu" title="&#92;mu" class="latex" />-value of the member is its grade.</p>
<div id="attachment_271" class="wp-caption alignright" style="width: 310px"><a href="http://blog.madpython.com/wp-content/uploads/2010/11/triangular.png" onclick="return TrackClick('http%3A%2F%2Fblog.madpython.com%2Fwp-content%2Fuploads%2F2010%2F11%2Ftriangular.png','Triangular+Fuzzy+Number')"><img class="size-medium wp-image-271" title="Triangular Fuzzy Number" src="http://blog.madpython.com/wp-content/uploads/2010/11/triangular-300x225.png" alt="triangular_fuzzy_number" width="300" height="225" /></a><p class="wp-caption-text">Triangular Fuzzy Number</p></div>
<p>One can represent the estimated values as well as their associated <img src="//s0.wp.com/latex.php?latex=%5Cmu&#038;bg=ffffff&#038;fg=000&#038;s=0" alt="&#92;mu" title="&#92;mu" class="latex" />-values in the form of a Cartesian coordinate system, with the Y-axis representing <img src="//s0.wp.com/latex.php?latex=%5Cmu&#038;bg=ffffff&#038;fg=000&#038;s=0" alt="&#92;mu" title="&#92;mu" class="latex" />-values, and the X-axis representing estimated values.<br />
We can then see that certain patterns emerge in the shape of the visualization, and these patterns are used to distinguish between the different types of fuzzy numbers:</p>
<ul>
<li><strong>Polygonal</strong> sets are represented by an arbitrary set of segments on the plane.</li>
<li><strong>Trapezoidal</strong> sets contain two kernels (estimations whose <img src="//s0.wp.com/latex.php?latex=%5Cmu&#038;bg=ffffff&#038;fg=000&#038;s=0" alt="&#92;mu" title="&#92;mu" class="latex" />-value is exactly 1.0), and two X-intercepts.</li>
<li><strong>Triangular</strong> sets contain exactly one kernel, and two X-intercepts</li>
<li><strong>Gaussian</strong> sets have <img src="//s0.wp.com/latex.php?latex=%5Cmu&#038;bg=ffffff&#038;fg=000&#038;s=0" alt="&#92;mu" title="&#92;mu" class="latex" />-values determined by a normal distribution, thus providing a smoother distribution.</li>
</ul>
<p>There are a few additional types as well, but these are the four basic types, and are all supported out of the box by FuzzPy.</p>
<p>Because the points of interest in each type are different, instanciating a new <code>FuzzyNumber</code> in FuzzPy is done differently depending on the type. Let&#8217;s instantiate objects for each of these types using FuzzPy, and we&#8217;ll play a bit with these objects in a moment. Make sure you have FuzzPy installed first (<code>easy_install -U fuzzpy</code>) and save the following code in <code>fuzzynums.py</code>:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> fuzz
&nbsp;
<span style="color: #808080; font-style: italic;"># Instantiating a PolynomialFuzzyNumber is done by providing</span>
<span style="color: #808080; font-style: italic;"># a list of (value, mu) tuple to the constructor:</span>
polygonal <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span>fuzz.<span style="color: black;">PolygonalFuzzyNumber</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0.0</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0.0</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> \
    <span style="color: black;">&#40;</span><span style="color: #ff4500;">0.25</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">1.0</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">1.0</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0.5</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">1.3</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">1.0</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">1.8</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0.0</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
&nbsp;
polygonal +<span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span>fuzz.<span style="color: black;">PolygonalFuzzyNumber</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0.0</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0.0</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> \
    <span style="color: black;">&#40;</span><span style="color: #ff4500;">1.0</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0.5</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">3.0</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">1.0</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">4.0</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">1.0</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">6.0</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0.0</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># A trapezoidal needs to be given a tuple containing the values</span>
<span style="color: #808080; font-style: italic;"># of the kernels (mu = 1.0) and a tuple containing the values</span>
<span style="color: #808080; font-style: italic;"># of the support (mu = 0.0)</span>
trapezoidal <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span>fuzz.<span style="color: black;">TrapezoidalFuzzyNumber</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1.0</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">2.0</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> \
    <span style="color: black;">&#40;</span><span style="color: #ff4500;">0.3</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">3.0</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
&nbsp;
trapezoidal +<span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span>fuzz.<span style="color: black;">TrapezoidalFuzzyNumber</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1.0</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">6.0</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> \
    <span style="color: black;">&#40;</span><span style="color: #ff4500;">0.5</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">10.0</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># A triangular set needs to be provided the value of the</span>
<span style="color: #808080; font-style: italic;"># kernel, and a tuple containing the values of the support.</span>
triangular <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span>fuzz.<span style="color: black;">TriangularFuzzyNumber</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1.0</span><span style="color: #66cc66;">,</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">0.0</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">3.0</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
triangular +<span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span>fuzz.<span style="color: black;">TriangularFuzzyNumber</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">4.0</span><span style="color: #66cc66;">,</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">0.0</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">4.5</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Gaussian fuzzy sets simply need the mean, and standard</span>
<span style="color: #808080; font-style: italic;"># derivation for the estimated value.</span>
gaussian <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span>fuzz.<span style="color: black;">GaussianFuzzyNumber</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1.0</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0.2</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
gaussian +<span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span>fuzz.<span style="color: black;">GaussianFuzzyNumber</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">12.0</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">1.0</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span></pre></td></tr></table></div>

<p>We can now fire up the python interpreter and work with our objects:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #66cc66;">&gt;&gt;&gt;</span> <span style="color: #808080; font-style: italic;"># Polygonal Union:</span>
... <span style="color: #ff7700;font-weight:bold;">print</span> polygonal<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> | polygonal<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
PolygonalFuzzyNumber: kernel <span style="color: black;">&#91;</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0.25</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0.25</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">1.3</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">1.3</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> \
<span style="color: black;">&#40;</span><span style="color: #ff4500;">3.0</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">4.0</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> support <span style="color: black;">&#91;</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0.0</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">6.0</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
&nbsp;
<span style="color: #66cc66;">&gt;&gt;&gt;</span> <span style="color: #808080; font-style: italic;"># Polygonal Intersection:</span>
... <span style="color: #ff7700;font-weight:bold;">print</span> polygonal<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> &amp; polygonal<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
PolygonalFuzzyNumber: kernel <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> support <span style="color: black;">&#91;</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0.0</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">6.0</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
&nbsp;
<span style="color: #66cc66;">&gt;&gt;&gt;</span> <span style="color: #808080; font-style: italic;"># Trapezoidal Addition:</span>
... <span style="color: #ff7700;font-weight:bold;">print</span> trapezoidal<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> + trapezoidal<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
TrapezoidalFuzzyNumber: kernel <span style="color: black;">&#40;</span><span style="color: #ff4500;">2.0</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">8.0</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> support <span style="color: black;">&#40;</span><span style="color: #ff4500;">0.8000000</span>\
<span style="color: #ff4500;">0000000004</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">13.0</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&gt;&gt;&gt;</span> <span style="color: #808080; font-style: italic;"># Gaussian Addition</span>
... <span style="color: #ff7700;font-weight:bold;">print</span> gaussian<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> + gaussian<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
GaussianFuzzyNumber: kernel <span style="color: black;">&#40;</span><span style="color: #ff4500;">13.0</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">13.0</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> support <span style="color: black;">&#40;</span><span style="color: #ff4500;">4.85663149</span>\
<span style="color: #ff4500;">07018668</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">21.143368509298135</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>The class inheritance chain for FuzzPy&#8217;s FuzzyNumber derived classes looks like this:</p>
<div id="attachment_253" class="wp-caption aligncenter" style="width: 598px"><a href="http://blog.madpython.com/wp-content/uploads/2010/11/fuzzy_number_inheritance.png" onclick="return TrackClick('http%3A%2F%2Fblog.madpython.com%2Fwp-content%2Fuploads%2F2010%2F11%2Ffuzzy_number_inheritance.png','Fuzzy+Number+Class+Inheritance')"><img class="size-full wp-image-253" title="Fuzzy Number Class Inheritance" src="http://blog.madpython.com/wp-content/uploads/2010/11/fuzzy_number_inheritance.png" onclick="return TrackClick('http%3A%2F%2Fblog.madpython.com%2Fwp-content%2Fuploads%2F2010%2F11%2Ffuzzy_number_inheritance.png','Fuzzy+Number+Class+Inheritance')" alt="fuzzy_number_class_inheritance.png" width="588" height="249" /></a><p class="wp-caption-text">Fuzzy Number Class Inheritance</p></div>
<h2>Visualizations</h2>
<p>FuzzPy ships with a small visualization framework that is able to create visualizations for almost all FuzzPy types, and encode them in a variety of formats. In most cases, creating and storing a visualization is a trivial operation:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> fuzzynums <span style="color: #ff7700;font-weight:bold;">import</span> *
<span style="color: #ff7700;font-weight:bold;">from</span> fuzz.<span style="color: black;">visualization</span> <span style="color: #ff7700;font-weight:bold;">import</span> VisManager
&nbsp;
gaussian <span style="color: #66cc66;">=</span> fuzz.<span style="color: black;">GaussianFuzzyNumber</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1.0</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0.2</span><span style="color: black;">&#41;</span>
plugin <span style="color: #66cc66;">=</span> VisManager.<span style="color: black;">create_backend</span><span style="color: black;">&#40;</span>gaussian<span style="color: black;">&#41;</span>
<span style="color: black;">&#40;</span>vis_format<span style="color: #66cc66;">,</span> vis_data<span style="color: black;">&#41;</span> <span style="color: #66cc66;">=</span> plugin.<span style="color: black;">visualize</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">with</span> <span style="color: black;">&#40;</span><span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;visualization.%s&quot;</span> % vis_format<span style="color: #66cc66;">,</span> <span style="color: #483d8b;">&quot;wb&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">as</span> fp:
    fp.<span style="color: black;">write</span><span style="color: black;">&#40;</span>vis_data<span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>In this example, we passed our <code>FuzzyNumber</code> instance to the <code>VisManager.create_backend()</code> method, which is a plugin factory method, and it returned the first plugin it could find that can be used to create a visualization of our <code>FuzzyNumber</code>.</p>
<p>The plugin&#8217;s <code>visualize()</code> method was called, and returned a tuple containing the file format of the visualization, and its payload. We then created a file using the returned format as extension, and stored the payload in that file.</p>
<p>You can also force the VisManager object to use a certain plugin if you do not want to rely on the default plugin picked for the type of object to visualize:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="python" style="font-family:monospace;">plugin <span style="color: #66cc66;">=</span> VisManager.<span style="color: black;">create_backend</span><span style="color: black;">&#40;</span>gaussian<span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'num_gnuplot'</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>Similarly, a plugin&#8217;s <code>visualize()</code> method can accept arguments to customize the final output. For example, the following code would tell the plugin to use the &#8216;gif&#8217; image format:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: black;">&#40;</span>vis_format<span style="color: #66cc66;">,</span> vis_data<span style="color: black;">&#41;</span> <span style="color: #66cc66;">=</span> plugin.<span style="color: black;">visualize</span><span style="color: black;">&#40;</span>format<span style="color: #66cc66;">=</span><span style="color: #483d8b;">&quot;gif&quot;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>You will need to have <a href="http://www.graphviz.org/" onclick="return TrackClick('http%3A%2F%2Fwww.graphviz.org%2F','Graphviz')">Graphviz</a> installed for graph visualizations, and <a href="http://www.gnuplot.info/" onclick="return TrackClick('http%3A%2F%2Fwww.gnuplot.info%2F','Gnuplot')">Gnuplot</a> for fuzzy number visualizations.</p>
<p>Now that you know how to use all the FuzzPy types and how to create visualizations for your data, you can start using FuzzPy in your own project, or get involved in the development process. Check out the project&#8217;s <a href="http://github.com/ezod/fuzzpy" onclick="return TrackClick('http%3A%2F%2Fgithub.com%2Fezod%2Ffuzzpy','GitHub')">GitHub</a> page for more information.</p>
<div id="attachment_274" class="wp-caption alignleft" style="width: 221px"><a href="http://blog.madpython.com/wp-content/uploads/2010/11/fuzzy_directed_graph.png" onclick="return TrackClick('http%3A%2F%2Fblog.madpython.com%2Fwp-content%2Fuploads%2F2010%2F11%2Ffuzzy_directed_graph.png','Fuzzy+Digraph+Visualization')"><img class="size-medium wp-image-274" title="Fuzzy Digraph Visualization" src="http://blog.madpython.com/wp-content/uploads/2010/11/fuzzy_directed_graph-211x300.png" alt="fuzzy_digraph" width="211" height="300" /></a><p class="wp-caption-text">Fuzzy Digraph Visualization</p></div>
<div id="attachment_272" class="wp-caption alignright" style="width: 310px"><a href="http://blog.madpython.com/wp-content/uploads/2010/11/polygonal.png" onclick="return TrackClick('http%3A%2F%2Fblog.madpython.com%2Fwp-content%2Fuploads%2F2010%2F11%2Fpolygonal.png','Polygonal+Fuzzy+Number')"><img class="size-medium wp-image-272" title="Polygonal Fuzzy Number" src="http://blog.madpython.com/wp-content/uploads/2010/11/polygonal-300x225.png" alt="polygonal_fuzzy_number" width="300" height="225" /></a><p class="wp-caption-text">Polygonal Fuzzy Number</p></div>
<p><a class="a2a_button_reddit" href="http://www.addtoany.com/add_to/reddit?linkurl=http%3A%2F%2Fblog.madpython.com%2F2010%2F11%2F03%2Ffuzzy-mathematics-with-fuzzpy-part-2%2F&amp;linkname=Fuzzy%20Mathematics%20with%20FuzzPy%20%28Part%202%29" title="Reddit" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/reddit.png" width="16" height="16" alt="Reddit"/></a><a class="a2a_button_delicious" href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fblog.madpython.com%2F2010%2F11%2F03%2Ffuzzy-mathematics-with-fuzzpy-part-2%2F&amp;linkname=Fuzzy%20Mathematics%20with%20FuzzPy%20%28Part%202%29" title="Delicious" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/delicious.png" width="16" height="16" alt="Delicious"/></a><a class="a2a_button_google_bookmarks" href="http://www.addtoany.com/add_to/google_bookmarks?linkurl=http%3A%2F%2Fblog.madpython.com%2F2010%2F11%2F03%2Ffuzzy-mathematics-with-fuzzpy-part-2%2F&amp;linkname=Fuzzy%20Mathematics%20with%20FuzzPy%20%28Part%202%29" title="Google Bookmarks" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/google.png" width="16" height="16" alt="Google Bookmarks"/></a><a class="a2a_button_facebook" href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fblog.madpython.com%2F2010%2F11%2F03%2Ffuzzy-mathematics-with-fuzzpy-part-2%2F&amp;linkname=Fuzzy%20Mathematics%20with%20FuzzPy%20%28Part%202%29" title="Facebook" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/facebook.png" width="16" height="16" alt="Facebook"/></a><a class="a2a_button_stumbleupon" href="http://www.addtoany.com/add_to/stumbleupon?linkurl=http%3A%2F%2Fblog.madpython.com%2F2010%2F11%2F03%2Ffuzzy-mathematics-with-fuzzpy-part-2%2F&amp;linkname=Fuzzy%20Mathematics%20with%20FuzzPy%20%28Part%202%29" title="StumbleUpon" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/stumbleupon.png" width="16" height="16" alt="StumbleUpon"/></a><a class="a2a_button_linkedin" href="http://www.addtoany.com/add_to/linkedin?linkurl=http%3A%2F%2Fblog.madpython.com%2F2010%2F11%2F03%2Ffuzzy-mathematics-with-fuzzpy-part-2%2F&amp;linkname=Fuzzy%20Mathematics%20with%20FuzzPy%20%28Part%202%29" title="LinkedIn" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/linkedin.png" width="16" height="16" alt="LinkedIn"/></a><a class="a2a_button_twitter" href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fblog.madpython.com%2F2010%2F11%2F03%2Ffuzzy-mathematics-with-fuzzpy-part-2%2F&amp;linkname=Fuzzy%20Mathematics%20with%20FuzzPy%20%28Part%202%29" title="Twitter" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.madpython.com%2F2010%2F11%2F03%2Ffuzzy-mathematics-with-fuzzpy-part-2%2F&amp;title=Fuzzy%20Mathematics%20with%20FuzzPy%20%28Part%202%29" id="wpa2a_2">Share</a></p><div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href='http://blog.madpython.com/2010/10/17/fuzzy-mathematics-with-fuzzpy-part-1/' rel='bookmark' title='Fuzzy Mathematics with FuzzPy (Part 1)'>Fuzzy Mathematics with FuzzPy (Part 1)</a></li>
<li><a href='http://blog.madpython.com/2010/08/07/algorithms-in-python-binary-exponentiation/' rel='bookmark' title='Algorithms in Python: Binary exponentiation'>Algorithms in Python: Binary exponentiation</a></li>
<li><a href='http://blog.madpython.com/2010/07/17/algorithms-in-python-binary-operations/' rel='bookmark' title='Algorithms in Python: Binary Operations'>Algorithms in Python: Binary Operations</a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.madpython.com/2010/11/03/fuzzy-mathematics-with-fuzzpy-part-2/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Fuzzy Mathematics with FuzzPy (Part 1)</title>
		<link>http://blog.madpython.com/2010/10/17/fuzzy-mathematics-with-fuzzpy-part-1/</link>
		<comments>http://blog.madpython.com/2010/10/17/fuzzy-mathematics-with-fuzzpy-part-1/#comments</comments>
		<pubDate>Sun, 17 Oct 2010 22:43:15 +0000</pubDate>
		<dc:creator>Xavier</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[tech]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[CS]]></category>
		<category><![CDATA[engineering]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[scripting]]></category>

		<guid isPermaLink="false">http://blog.madpython.com/?p=165</guid>
		<description><![CDATA[FuzzPy is a python library that exposes specialized datatypes to deal with a wide range of fuzzy number types, fuzzy sets, and fuzzy graphs. Binary operations against each type, such as set unions and intersections, can be performed using some &#8230; <a href="http://blog.madpython.com/2010/10/17/fuzzy-mathematics-with-fuzzpy-part-1/">Continue reading <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href='http://blog.madpython.com/2010/11/03/fuzzy-mathematics-with-fuzzpy-part-2/' rel='bookmark' title='Fuzzy Mathematics with FuzzPy (Part 2)'>Fuzzy Mathematics with FuzzPy (Part 2)</a></li>
<li><a href='http://blog.madpython.com/2010/08/07/algorithms-in-python-binary-exponentiation/' rel='bookmark' title='Algorithms in Python: Binary exponentiation'>Algorithms in Python: Binary exponentiation</a></li>
<li><a href='http://blog.madpython.com/2010/07/17/algorithms-in-python-binary-operations/' rel='bookmark' title='Algorithms in Python: Binary Operations'>Algorithms in Python: Binary Operations</a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p><a href="http://github.com/ezod/fuzzpy" onclick="return TrackClick('http%3A%2F%2Fgithub.com%2Fezod%2Ffuzzpy','FuzzPy')">FuzzPy</a> is a python library that exposes specialized datatypes to deal with a wide range of fuzzy number types, fuzzy sets, and fuzzy graphs. Binary operations against each type, such as set unions and intersections, can be performed using some of python&#8217;s native binary operators (<code>|</code>, <code>&#038;</code>, <code>==</code>), or specialized methods if you wish to deviate from the default functions for these computations. </p>
<p>FuzzPy also provides several helper methods such as kernel and neighbors isolation, alpha-cuts, cardinality testing, shortest-path computation, and minimum spanning trees. A powerful visualization framework also allows you to quickly create and save visualizations for your data.</p>
<p>In this post, we will examine fuzzy sets and fuzzy graphs, and see how one can use FuzzPy to work with these types, providing examples for each. In the next post of this series, we will examine the different types of fuzzy numbers, generate visualizations, and explore advanced concepts such as automatic fuzzification, and importing graph data from fuzzy adjacency matrices.</p>
<h2>Fuzzy Sets</h2>
<p>A fuzzy set is simply a set in which each element has an associated degree of membership into the set. This membership value is called <img src="//s0.wp.com/latex.php?latex=%5Cmu&#038;bg=ffffff&#038;fg=000&#038;s=0" alt="&#92;mu" title="&#92;mu" class="latex" />-value of the element. A <img src="//s0.wp.com/latex.php?latex=%5Cmu&#038;bg=ffffff&#038;fg=000&#038;s=0" alt="&#92;mu" title="&#92;mu" class="latex" />-value of zero indicates that the element does not belong to the set, while any value between zero (exclusive) and one (inclusive), indicates that the element is, to a certain degree, a set member.</p>
<p>Creating a fuzzy set with FuzzPy is just a matter of creating a new <code>FuzzySet</code> object, then either calling the <code>add(FuzzyElement(value, mu))</code> method for each element to import, or using the <code>update(elements)</code> method, providing it with a list of <code>FuzzyElement(value, <img src="//s0.wp.com/latex.php?latex=%5Cmu&#038;bg=ffffff&#038;fg=000&#038;s=0" alt="&#92;mu" title="&#92;mu" class="latex" />-value)</code> tuples for bulk import.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> fuzz <span style="color: #ff7700;font-weight:bold;">import</span> FuzzySet<span style="color: #66cc66;">,</span> FuzzyElement
&nbsp;
<span style="color: #808080; font-style: italic;"># Create two lists of FuzzyElements</span>
elements_a <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0.3</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0.5</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">3</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0.7</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">4</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0.9</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
elements_b <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">3</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0.8</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">6</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0.6</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0.4</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">8</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0.2</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
&nbsp;
elements_a <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span>FuzzyElement<span style="color: black;">&#40;</span>x<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> x<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> elements_a<span style="color: black;">&#93;</span>
elements_b <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span>FuzzyElement<span style="color: black;">&#40;</span>x<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> x<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> elements_b<span style="color: black;">&#93;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Create two fuzzy sets</span>
set_a <span style="color: #66cc66;">=</span> FuzzySet<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
set_b <span style="color: #66cc66;">=</span> FuzzySet<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
set_a.<span style="color: black;">update</span><span style="color: black;">&#40;</span>elements_a<span style="color: black;">&#41;</span>
set_b.<span style="color: black;">update</span><span style="color: black;">&#40;</span>elements_b<span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>We&#8217;ve just created a couple fuzzy sets with four elements each. Notice that two of the elements are common to both <code>set_a</code> and <code>set_b</code> (albeit with different <img src="//s0.wp.com/latex.php?latex=%5Cmu&#038;bg=ffffff&#038;fg=000&#038;s=0" alt="&#92;mu" title="&#92;mu" class="latex" />-values). We can now perform several operations against these sample sets. Save the code above in a file called <code>example.py</code> and fire up the python interpreter to test fuzzy set functionality against our sets:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #66cc66;">&gt;&gt;&gt;</span> <span style="color: #ff7700;font-weight:bold;">from</span> example <span style="color: #ff7700;font-weight:bold;">import</span> *
<span style="color: #66cc66;">&gt;&gt;&gt;</span> <span style="color: #808080; font-style: italic;"># Set Difference</span>
...
<span style="color: #66cc66;">&gt;&gt;&gt;</span> set_a - set_b
FuzzySet<span style="color: black;">&#40;</span><span style="color: black;">&#91;</span>FuzzyElement<span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0.500000</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> FuzzyElement<span style="color: black;">&#40;</span><span style="color: #ff4500;">4</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0.900000</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> set_b - set_a
FuzzySet<span style="color: black;">&#40;</span><span style="color: black;">&#91;</span>FuzzyElement<span style="color: black;">&#40;</span><span style="color: #ff4500;">8</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0.200000</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> FuzzyElement<span style="color: black;">&#40;</span><span style="color: #ff4500;">6</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0.600000</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
<span style="color: #808080; font-style: italic;"># Set Union</span>
...
<span style="color: #66cc66;">&gt;&gt;&gt;</span> set_a | set_b
FuzzySet<span style="color: black;">&#40;</span><span style="color: black;">&#91;</span>FuzzyElement<span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0.400000</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> FuzzyElement<span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0.500000</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> FuzzyElement<span style="color: black;">&#40;</span><span style="color: #ff4500;">3</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0.800000</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> FuzzyElement<span style="color: black;">&#40;</span><span style="color: #ff4500;">4</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0.900000</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> FuzzyElement<span style="color: black;">&#40;</span><span style="color: #ff4500;">6</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0.600000</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> FuzzyElement<span style="color: black;">&#40;</span><span style="color: #ff4500;">8</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0.200000</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
<span style="color: #808080; font-style: italic;"># Set Intersection</span>
...
<span style="color: #66cc66;">&gt;&gt;&gt;</span> set_a &amp; set_b
FuzzySet<span style="color: black;">&#40;</span><span style="color: black;">&#91;</span>FuzzyElement<span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0.300000</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> FuzzyElement<span style="color: black;">&#40;</span><span style="color: #ff4500;">3</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0.700000</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<h2>Fuzzy graphs</h2>
<p>In a fuzzy graph or digraph, each vertice and each edge can be associated with a <img src="//s0.wp.com/latex.php?latex=%5Cmu&#038;bg=ffffff&#038;fg=000&#038;s=0" alt="&#92;mu" title="&#92;mu" class="latex" />-value. The <img src="//s0.wp.com/latex.php?latex=%5Cmu&#038;bg=ffffff&#038;fg=000&#038;s=0" alt="&#92;mu" title="&#92;mu" class="latex" />-value of a vertice indicates the vertice&#8217;s degree of membership into the graph, while an edge or arc&#8217;s <img src="//s0.wp.com/latex.php?latex=%5Cmu&#038;bg=ffffff&#038;fg=000&#038;s=0" alt="&#92;mu" title="&#92;mu" class="latex" />-value indicates the degree of connectivity between the head and tail vertices. </p>
<p>The creation of fuzzy graphs is also pretty straightforward: create a set or fuzzy set (see above), then feed that set to the fuzz.Graph constructor, along with the <code>directed</code> boolean value to specify whether you&#8217;d like to create a graph or a digraph. You can then use the <code>fuzz.Graph.connect(head, tail, mu)</code> method to add edges to the graph:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> fuzz
&nbsp;
<span style="color: #808080; font-style: italic;"># Create two sets of vertices</span>
set_a <span style="color: #66cc66;">=</span> <span style="color: #008000;">set</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">2</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">3</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">4</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">5</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">6</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">7</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">8</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">9</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">10</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
set_b <span style="color: #66cc66;">=</span> <span style="color: #008000;">set</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">11</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">12</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">13</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">14</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">15</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">16</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">17</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">18</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">19</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">20</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Create our graph objects</span>
graph_a <span style="color: #66cc66;">=</span> fuzz.<span style="color: black;">FuzzyGraph</span><span style="color: black;">&#40;</span>viter<span style="color: #66cc66;">=</span>set_a<span style="color: #66cc66;">,</span> directed<span style="color: #66cc66;">=</span><span style="color: #008000;">False</span><span style="color: black;">&#41;</span>
graph_b <span style="color: #66cc66;">=</span> fuzz.<span style="color: black;">FuzzyGraph</span><span style="color: black;">&#40;</span>viter<span style="color: #66cc66;">=</span>set_b<span style="color: #66cc66;">,</span> directed<span style="color: #66cc66;">=</span><span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Create some arcs in graph_a</span>
graph_a.<span style="color: black;">connect</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">3</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0.5</span><span style="color: black;">&#41;</span>
graph_a.<span style="color: black;">connect</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">3</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">6</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0.06</span><span style="color: black;">&#41;</span>
graph_a.<span style="color: black;">connect</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">8</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0.2</span><span style="color: black;">&#41;</span>
graph_a.<span style="color: black;">connect</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">4</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0.9</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># And some in graph_b</span>
graph_b.<span style="color: black;">connect</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">11</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">19</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0.9</span><span style="color: black;">&#41;</span>
graph_b.<span style="color: black;">connect</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">14</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">12</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0.3</span><span style="color: black;">&#41;</span>
graph_b.<span style="color: black;">connect</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">15</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">14</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0.5</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>Save the code above in a file named <code>graphs.py</code> and fire up your python interpreter so we can experiment with graph operations:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #66cc66;">&gt;&gt;&gt;</span> <span style="color: #ff7700;font-weight:bold;">from</span> graphs <span style="color: #ff7700;font-weight:bold;">import</span> *
<span style="color: #66cc66;">&gt;&gt;&gt;</span> <span style="color: #808080; font-style: italic;"># Retrieve the mu value of the edge between 1 and 3</span>
... 
<span style="color: #66cc66;">&gt;&gt;&gt;</span> graph_a.<span style="color: black;">mu</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">3</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
<span style="color: #ff4500;">0.5</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> <span style="color: #808080; font-style: italic;"># Retrieve an alpha-cut of graph_b against mu-value of 0.5</span>
... 
<span style="color: #66cc66;">&gt;&gt;&gt;</span> graph_b.<span style="color: black;">alpha</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0.5</span><span style="color: black;">&#41;</span>
Graph<span style="color: black;">&#40;</span>viter <span style="color: #66cc66;">=</span> <span style="color: #008000;">set</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">11</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">12</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">13</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">14</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">15</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">16</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">17</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">18</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">19</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">20</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> eiter <span style="color: #66cc66;">=</span> <span style="color: #008000;">set</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">15</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">14</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">11</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">19</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> directed <span style="color: #66cc66;">=</span> <span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> <span style="color: #808080; font-style: italic;"># Is graph_a a subgraph of graph_b? (no)</span>
... <span style="color: black;">graph_a</span>.<span style="color: black;">issubgraph</span><span style="color: black;">&#40;</span>graph_b<span style="color: black;">&#41;</span>
<span style="color: #008000;">False</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> <span style="color: #808080; font-style: italic;"># Detect adjacent (connected) vertices</span>
...
<span style="color: #66cc66;">&gt;&gt;&gt;</span> graph_a.<span style="color: black;">adjacent</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>
<span style="color: #008000;">False</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> graph_a.<span style="color: black;">adjacent</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span>
<span style="color: #008000;">True</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> <span style="color: #808080; font-style: italic;"># Find all the neighbours of the vertex with value 3</span>
... 
<span style="color: #66cc66;">&gt;&gt;&gt;</span> graph_a.<span style="color: black;">neighbors</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span>
<span style="color: #008000;">set</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">6</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> <span style="color: #808080; font-style: italic;"># Find the shortest path to all vertices using 6 as origin</span>
... 
<span style="color: #66cc66;">&gt;&gt;&gt;</span> graph_a.<span style="color: black;">dijkstra</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">6</span><span style="color: black;">&#41;</span>
<span style="color: black;">&#123;</span><span style="color: #ff4500;">1</span>: <span style="color: #ff4500;">3</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">2</span>: <span style="color: #008000;">None</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">3</span>: <span style="color: #ff4500;">6</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">4</span>: <span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">5</span>: <span style="color: #008000;">None</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">6</span>: <span style="color: #008000;">None</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">7</span>: <span style="color: #008000;">None</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">8</span>: <span style="color: #008000;">None</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">9</span>: <span style="color: #008000;">None</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">10</span>: <span style="color: #008000;">None</span><span style="color: black;">&#125;</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> <span style="color: #808080; font-style: italic;"># Find the minimum spanning tree of graph_a</span>
... 
<span style="color: #66cc66;">&gt;&gt;&gt;</span> graph_a.<span style="color: black;">minimum_spanning_tree</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
Graph<span style="color: black;">&#40;</span>viter <span style="color: #66cc66;">=</span> <span style="color: #008000;">set</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">2</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">3</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">4</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">5</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">6</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">7</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">8</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">9</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">10</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> eiter <span style="color: #66cc66;">=</span> <span style="color: #008000;">set</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">8</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">4</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">3</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">6</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> directed <span style="color: #66cc66;">=</span> <span style="color: #008000;">False</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>There are a few more operations available for both crisp and fuzzy graphs/digraphs. You should consult the <a href="http://packages.python.org/fuzzpy/" onclick="return TrackClick('http%3A%2F%2Fpackages.python.org%2Ffuzzpy%2F','API+doc')">API doc</a> for a comprehensive list.</p>
<p>You should also consult the <a href="http://github.com/ezod/fuzzpy/tree/master/examples/" onclick="return TrackClick('http%3A%2F%2Fgithub.com%2Fezod%2Ffuzzpy%2Ftree%2Fmaster%2Fexamples%2F','examples')">examples</a> provided as part of the module on GitHub to see FuzzPy in action.</p>
<p><a class="a2a_button_reddit" href="http://www.addtoany.com/add_to/reddit?linkurl=http%3A%2F%2Fblog.madpython.com%2F2010%2F10%2F17%2Ffuzzy-mathematics-with-fuzzpy-part-1%2F&amp;linkname=Fuzzy%20Mathematics%20with%20FuzzPy%20%28Part%201%29" title="Reddit" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/reddit.png" width="16" height="16" alt="Reddit"/></a><a class="a2a_button_delicious" href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fblog.madpython.com%2F2010%2F10%2F17%2Ffuzzy-mathematics-with-fuzzpy-part-1%2F&amp;linkname=Fuzzy%20Mathematics%20with%20FuzzPy%20%28Part%201%29" title="Delicious" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/delicious.png" width="16" height="16" alt="Delicious"/></a><a class="a2a_button_google_bookmarks" href="http://www.addtoany.com/add_to/google_bookmarks?linkurl=http%3A%2F%2Fblog.madpython.com%2F2010%2F10%2F17%2Ffuzzy-mathematics-with-fuzzpy-part-1%2F&amp;linkname=Fuzzy%20Mathematics%20with%20FuzzPy%20%28Part%201%29" title="Google Bookmarks" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/google.png" width="16" height="16" alt="Google Bookmarks"/></a><a class="a2a_button_facebook" href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fblog.madpython.com%2F2010%2F10%2F17%2Ffuzzy-mathematics-with-fuzzpy-part-1%2F&amp;linkname=Fuzzy%20Mathematics%20with%20FuzzPy%20%28Part%201%29" title="Facebook" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/facebook.png" width="16" height="16" alt="Facebook"/></a><a class="a2a_button_stumbleupon" href="http://www.addtoany.com/add_to/stumbleupon?linkurl=http%3A%2F%2Fblog.madpython.com%2F2010%2F10%2F17%2Ffuzzy-mathematics-with-fuzzpy-part-1%2F&amp;linkname=Fuzzy%20Mathematics%20with%20FuzzPy%20%28Part%201%29" title="StumbleUpon" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/stumbleupon.png" width="16" height="16" alt="StumbleUpon"/></a><a class="a2a_button_linkedin" href="http://www.addtoany.com/add_to/linkedin?linkurl=http%3A%2F%2Fblog.madpython.com%2F2010%2F10%2F17%2Ffuzzy-mathematics-with-fuzzpy-part-1%2F&amp;linkname=Fuzzy%20Mathematics%20with%20FuzzPy%20%28Part%201%29" title="LinkedIn" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/linkedin.png" width="16" height="16" alt="LinkedIn"/></a><a class="a2a_button_twitter" href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fblog.madpython.com%2F2010%2F10%2F17%2Ffuzzy-mathematics-with-fuzzpy-part-1%2F&amp;linkname=Fuzzy%20Mathematics%20with%20FuzzPy%20%28Part%201%29" title="Twitter" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.madpython.com%2F2010%2F10%2F17%2Ffuzzy-mathematics-with-fuzzpy-part-1%2F&amp;title=Fuzzy%20Mathematics%20with%20FuzzPy%20%28Part%201%29" id="wpa2a_4">Share</a></p><div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href='http://blog.madpython.com/2010/11/03/fuzzy-mathematics-with-fuzzpy-part-2/' rel='bookmark' title='Fuzzy Mathematics with FuzzPy (Part 2)'>Fuzzy Mathematics with FuzzPy (Part 2)</a></li>
<li><a href='http://blog.madpython.com/2010/08/07/algorithms-in-python-binary-exponentiation/' rel='bookmark' title='Algorithms in Python: Binary exponentiation'>Algorithms in Python: Binary exponentiation</a></li>
<li><a href='http://blog.madpython.com/2010/07/17/algorithms-in-python-binary-operations/' rel='bookmark' title='Algorithms in Python: Binary Operations'>Algorithms in Python: Binary Operations</a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.madpython.com/2010/10/17/fuzzy-mathematics-with-fuzzpy-part-1/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Algorithms in Python: Binary exponentiation</title>
		<link>http://blog.madpython.com/2010/08/07/algorithms-in-python-binary-exponentiation/</link>
		<comments>http://blog.madpython.com/2010/08/07/algorithms-in-python-binary-exponentiation/#comments</comments>
		<pubDate>Sun, 08 Aug 2010 03:10:01 +0000</pubDate>
		<dc:creator>Xavier</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[tech]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[CS]]></category>
		<category><![CDATA[engineering]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[scripting]]></category>

		<guid isPermaLink="false">http://blog.madpython.com/?p=142</guid>
		<description><![CDATA[The typical approach to exponentiation of a base b by an exponent n is to repeatedly multiply the base by itself, as such: This is easy to compute for small values but quickly chokes with very large values. A faster &#8230; <a href="http://blog.madpython.com/2010/08/07/algorithms-in-python-binary-exponentiation/">Continue reading <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href='http://blog.madpython.com/2010/07/17/algorithms-in-python-binary-operations/' rel='bookmark' title='Algorithms in Python: Binary Operations'>Algorithms in Python: Binary Operations</a></li>
<li><a href='http://blog.madpython.com/2010/07/10/algorithms-in-python-base-expansion/' rel='bookmark' title='Algorithms in Python: Base Expansion'>Algorithms in Python: Base Expansion</a></li>
<li><a href='http://blog.madpython.com/2010/10/17/fuzzy-mathematics-with-fuzzpy-part-1/' rel='bookmark' title='Fuzzy Mathematics with FuzzPy (Part 1)'>Fuzzy Mathematics with FuzzPy (Part 1)</a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p>The typical approach to exponentiation of a base <em>b</em> by an exponent <em>n</em> is to repeatedly multiply the base by itself, as such: <img src="//s0.wp.com/latex.php?latex=b%5En+%3D+%5Cprod_%7Bi%3D1%7D%5E%7Bn%7D+b&#038;bg=ffffff&#038;fg=000&#038;s=0" alt="b^n = &#92;prod_{i=1}^{n} b" title="b^n = &#92;prod_{i=1}^{n} b" class="latex" /></p>
<p>This is easy to compute for small values but quickly chokes with very large values.</p>
<p>A faster approach involves converting the exponent into base 2, then multiplying the running total and squaring the base each time a 1-bit is encountered. The python implementation looks like this:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> binary_exponent<span style="color: black;">&#40;</span>base<span style="color: #66cc66;">,</span> exponent<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;<span style="color: #000099; font-weight: bold;">\</span>
    Binary Exponentiation
&nbsp;
    Instead of computing the exponentiation in the traditional way,
    convert the exponent to its reverse binary representation.
&nbsp;
    Each time a 1-bit is encountered, we multiply the running total by
    the base, then square the base.
    &quot;&quot;&quot;</span>
    <span style="color: #808080; font-style: italic;"># Convert n to base 2, then reverse it.</span>
    exponent <span style="color: #66cc66;">=</span> bin<span style="color: black;">&#40;</span>exponent<span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span>:<span style="color: black;">&#93;</span><span style="color: black;">&#91;</span>::-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
&nbsp;
    result <span style="color: #66cc66;">=</span> <span style="color: #ff4500;">1</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>exponent<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> exponent<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span> <span style="color: #66cc66;">==</span> <span style="color: #483d8b;">'1'</span>:
            result *<span style="color: #66cc66;">=</span> base
        base *<span style="color: #66cc66;">=</span> base
    <span style="color: #ff7700;font-weight:bold;">return</span> result</pre></td></tr></table></div>

<p>Similarly, the same approach can be taken to perform modular exponentiation against very large exponents:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> modular_exponent<span style="color: black;">&#40;</span>base<span style="color: #66cc66;">,</span> exponent<span style="color: #66cc66;">,</span> mod<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;<span style="color: #000099; font-weight: bold;">\</span>
    Modular exponentiation through binary decomposition.
&nbsp;
    We use the same technique as for the binary exponentiation above in
    order to find the modulo of our very large exponent and an arbitrary
    integer mod.
    &quot;&quot;&quot;</span>
    exponent <span style="color: #66cc66;">=</span> bin<span style="color: black;">&#40;</span>exponent<span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span>:<span style="color: black;">&#93;</span><span style="color: black;">&#91;</span>::-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
&nbsp;
    x <span style="color: #66cc66;">=</span> <span style="color: #ff4500;">1</span>
    power <span style="color: #66cc66;">=</span> base % mod
    <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>exponent<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> exponent<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span> <span style="color: #66cc66;">==</span> <span style="color: #483d8b;">'1'</span>:
            x <span style="color: #66cc66;">=</span> <span style="color: black;">&#40;</span>x * power<span style="color: black;">&#41;</span> % mod
        power <span style="color: #66cc66;">=</span> <span style="color: black;">&#40;</span>power ** <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span> % mod
    <span style="color: #ff7700;font-weight:bold;">return</span> x</pre></td></tr></table></div>

<p>The optimization is especially visible for the modulo operation, since we are working on smaller numbers with each iteration, whereas for simple exponentiation, the CPython interpreter can already optimize the operation.<br />
The speedup for modular exponentiation on my system was in the order of 72x the speed of a simple <img src="//s0.wp.com/latex.php?latex=%28a%5En%29+%5Cmod%7Bm%7D&#038;bg=ffffff&#038;fg=000&#038;s=0" alt="(a^n) &#92;mod{m}" title="(a^n) &#92;mod{m}" class="latex" /> whereas the the computation of the exponentiation took about the same time in both cases.</p>
<p><a class="a2a_button_reddit" href="http://www.addtoany.com/add_to/reddit?linkurl=http%3A%2F%2Fblog.madpython.com%2F2010%2F08%2F07%2Falgorithms-in-python-binary-exponentiation%2F&amp;linkname=Algorithms%20in%20Python%3A%20Binary%20exponentiation" title="Reddit" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/reddit.png" width="16" height="16" alt="Reddit"/></a><a class="a2a_button_delicious" href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fblog.madpython.com%2F2010%2F08%2F07%2Falgorithms-in-python-binary-exponentiation%2F&amp;linkname=Algorithms%20in%20Python%3A%20Binary%20exponentiation" title="Delicious" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/delicious.png" width="16" height="16" alt="Delicious"/></a><a class="a2a_button_google_bookmarks" href="http://www.addtoany.com/add_to/google_bookmarks?linkurl=http%3A%2F%2Fblog.madpython.com%2F2010%2F08%2F07%2Falgorithms-in-python-binary-exponentiation%2F&amp;linkname=Algorithms%20in%20Python%3A%20Binary%20exponentiation" title="Google Bookmarks" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/google.png" width="16" height="16" alt="Google Bookmarks"/></a><a class="a2a_button_facebook" href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fblog.madpython.com%2F2010%2F08%2F07%2Falgorithms-in-python-binary-exponentiation%2F&amp;linkname=Algorithms%20in%20Python%3A%20Binary%20exponentiation" title="Facebook" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/facebook.png" width="16" height="16" alt="Facebook"/></a><a class="a2a_button_stumbleupon" href="http://www.addtoany.com/add_to/stumbleupon?linkurl=http%3A%2F%2Fblog.madpython.com%2F2010%2F08%2F07%2Falgorithms-in-python-binary-exponentiation%2F&amp;linkname=Algorithms%20in%20Python%3A%20Binary%20exponentiation" title="StumbleUpon" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/stumbleupon.png" width="16" height="16" alt="StumbleUpon"/></a><a class="a2a_button_linkedin" href="http://www.addtoany.com/add_to/linkedin?linkurl=http%3A%2F%2Fblog.madpython.com%2F2010%2F08%2F07%2Falgorithms-in-python-binary-exponentiation%2F&amp;linkname=Algorithms%20in%20Python%3A%20Binary%20exponentiation" title="LinkedIn" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/linkedin.png" width="16" height="16" alt="LinkedIn"/></a><a class="a2a_button_twitter" href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fblog.madpython.com%2F2010%2F08%2F07%2Falgorithms-in-python-binary-exponentiation%2F&amp;linkname=Algorithms%20in%20Python%3A%20Binary%20exponentiation" title="Twitter" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.madpython.com%2F2010%2F08%2F07%2Falgorithms-in-python-binary-exponentiation%2F&amp;title=Algorithms%20in%20Python%3A%20Binary%20exponentiation" id="wpa2a_6">Share</a></p><div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href='http://blog.madpython.com/2010/07/17/algorithms-in-python-binary-operations/' rel='bookmark' title='Algorithms in Python: Binary Operations'>Algorithms in Python: Binary Operations</a></li>
<li><a href='http://blog.madpython.com/2010/07/10/algorithms-in-python-base-expansion/' rel='bookmark' title='Algorithms in Python: Base Expansion'>Algorithms in Python: Base Expansion</a></li>
<li><a href='http://blog.madpython.com/2010/10/17/fuzzy-mathematics-with-fuzzpy-part-1/' rel='bookmark' title='Fuzzy Mathematics with FuzzPy (Part 1)'>Fuzzy Mathematics with FuzzPy (Part 1)</a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.madpython.com/2010/08/07/algorithms-in-python-binary-exponentiation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Algorithms in Python: Binary Operations</title>
		<link>http://blog.madpython.com/2010/07/17/algorithms-in-python-binary-operations/</link>
		<comments>http://blog.madpython.com/2010/07/17/algorithms-in-python-binary-operations/#comments</comments>
		<pubDate>Sat, 17 Jul 2010 19:08:22 +0000</pubDate>
		<dc:creator>Xavier</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[tech]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[CS]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://blog.madpython.com/?p=131</guid>
		<description><![CDATA[Today I&#8217;d like to demonstrate a simple implementation of Kenneth Rosen&#8216;s binary addition and multiplication algorithms as outlined in &#34;Discrete Mathematics and its Applications&#34;. Both algorithms are very simple and work the same way we&#8217;ve all learned to do decimal &#8230; <a href="http://blog.madpython.com/2010/07/17/algorithms-in-python-binary-operations/">Continue reading <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href='http://blog.madpython.com/2010/08/07/algorithms-in-python-binary-exponentiation/' rel='bookmark' title='Algorithms in Python: Binary exponentiation'>Algorithms in Python: Binary exponentiation</a></li>
<li><a href='http://blog.madpython.com/2010/07/10/algorithms-in-python-base-expansion/' rel='bookmark' title='Algorithms in Python: Base Expansion'>Algorithms in Python: Base Expansion</a></li>
<li><a href='http://blog.madpython.com/2010/10/17/fuzzy-mathematics-with-fuzzpy-part-1/' rel='bookmark' title='Fuzzy Mathematics with FuzzPy (Part 1)'>Fuzzy Mathematics with FuzzPy (Part 1)</a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p>Today I&#8217;d like to demonstrate a simple implementation of <a href="http://highered.mcgraw-hill.com/sites/0072880082/information_center_view0/about_the_author.html" onclick="return TrackClick('http%3A%2F%2Fhighered.mcgraw-hill.com%2Fsites%2F0072880082%2Finformation_center_view0%2Fabout_the_author.html','Kenneth+Rosen')" target="_blank">Kenneth Rosen</a>&#8216;s binary addition and multiplication algorithms as outlined in &quot;<a href="http://www.amazon.com/Discrete-Mathematics-Applications-Kenneth-Rosen/dp/0071244743/ref=sr_1_2?ie=UTF8&#038;s=books&#038;qid=1279392073&#038;sr=8-2" onclick="return TrackClick('http%3A%2F%2Fwww.amazon.com%2FDiscrete-Mathematics-Applications-Kenneth-Rosen%2Fdp%2F0071244743%2Fref%3Dsr_1_2%3Fie%3DUTF8%26s%3Dbooks%26qid%3D1279392073%26sr%3D8-2','Discrete+Mathematics+and+its+Applications')" target="_blank">Discrete Mathematics and its Applications</a>&quot;. Both algorithms are very simple and work the same way we&#8217;ve all learned to do decimal additions and multiplications by hand in grade-school.</p>
<p>As usual, I&#8217;ve also provided a unit-test suite for each algorithm.</p>
<p><strong>Please note:</strong> This is only intended to demonstrate the algorithms. If you are actually trying to do some real work with binary numbers in Python, note that the language itself provides a highly optimized implementation of binary numbers and related operations. Here are a few examples:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># Decimal to binary conversion:</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> bin<span style="color: black;">&#40;</span><span style="color: #ff4500;">15</span><span style="color: black;">&#41;</span>
<span style="color: #483d8b;">'0b1111'</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Binary to decimal conversion:</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0b1111</span><span style="color: black;">&#41;</span>
<span style="color: #483d8b;">'15'</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Binary addition:</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> bin<span style="color: black;">&#40;</span><span style="color: #ff4500;">0b1111</span> + <span style="color: #ff4500;">0b10011</span><span style="color: black;">&#41;</span>
<span style="color: #483d8b;">'0b100010'</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Binary multiplication:</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> bin<span style="color: black;">&#40;</span><span style="color: #ff4500;">0b1111</span> * <span style="color: #ff4500;">0b10011</span><span style="color: black;">&#41;</span>
<span style="color: #483d8b;">'0b100011101'</span></pre></td></tr></table></div>

<h2>Binary Addition</h2>
<p>We will use strings in this example to represent sequences of bits. The algorithm operates right-to-left, maintaining a carry each time it adds two 1-valued bits.</p>
<h3>Code:</h3>

<div class="wp_syntax"><table><tr><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #483d8b;">&quot;&quot;&quot;Simple binary operation algorithm.&quot;&quot;&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">math</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># a and b should be string representation of the binary components</span>
<span style="color: #808080; font-style: italic;"># You could easily extend this to overload the + operator for binaries,</span>
<span style="color: #808080; font-style: italic;"># but this is already built into python.</span>
<span style="color: #ff7700;font-weight:bold;">def</span> binary_addition<span style="color: black;">&#40;</span>bin_a<span style="color: #66cc66;">,</span> bin_b<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;Performs a binary addition against two provided binary numbers&quot;&quot;&quot;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># Pad the components so they are of equal length</span>
    max_length <span style="color: #66cc66;">=</span> <span style="color: #008000;">max</span><span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>bin_a<span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>bin_b<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    bin_a<span style="color: #66cc66;">,</span> bin_b <span style="color: #66cc66;">=</span> bin_a.<span style="color: black;">zfill</span><span style="color: black;">&#40;</span>max_length<span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> bin_b.<span style="color: black;">zfill</span><span style="color: black;">&#40;</span>max_length<span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
    carry <span style="color: #66cc66;">=</span> <span style="color: #ff4500;">0</span>
    result <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">''</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># Note that we work from right to left</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span> max_length<span style="color: black;">&#41;</span><span style="color: black;">&#91;</span>::-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>:
        tmp <span style="color: #66cc66;">=</span> <span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">math</span>.<span style="color: black;">floor</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #008000;">int</span><span style="color: black;">&#40;</span>bin_a<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> + <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>bin_b<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> + carry<span style="color: black;">&#41;</span>/<span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        res <span style="color: #66cc66;">=</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span><span style="color: #008000;">int</span><span style="color: black;">&#40;</span>bin_a<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> + <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>bin_b<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> + carry - <span style="color: #ff4500;">2</span>*tmp<span style="color: black;">&#41;</span>
        result +<span style="color: #66cc66;">=</span> res
        carry <span style="color: #66cc66;">=</span> tmp
&nbsp;
    result <span style="color: #66cc66;">=</span> <span style="color: black;">&#40;</span>result + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>carry<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span>::-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">try</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> result<span style="color: black;">&#91;</span>result.<span style="color: black;">index</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'1'</span><span style="color: black;">&#41;</span>:<span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">ValueError</span><span style="color: #66cc66;">,</span> ex:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">'0'</span></pre></td></tr></table></div>

<h3>Unit Test:</h3>

<div class="wp_syntax"><table><tr><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #483d8b;">&quot;&quot;&quot;Unit test for binary_addition.py&quot;&quot;&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">unittest</span>
<span style="color: #ff7700;font-weight:bold;">from</span> binary_addition <span style="color: #ff7700;font-weight:bold;">import</span> binary_addition
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> TestBinaryAddition<span style="color: black;">&#40;</span><span style="color: #dc143c;">unittest</span>.<span style="color: black;">TestCase</span><span style="color: black;">&#41;</span>:
&nbsp;
    <span style="color: #483d8b;">&quot;&quot;&quot;Comparing python binary addition against our own&quot;&quot;&quot;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> test_add_same_length<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
&nbsp;
        <span style="color: #483d8b;">&quot;&quot;&quot;Adding 2 binary numbers of the same length&quot;&quot;&quot;</span>
&nbsp;
        bin_a <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">'001001101'</span>
        bin_b <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">'011011010'</span>
&nbsp;
        <span style="color: #008000;">self</span>._compare_additions<span style="color: black;">&#40;</span>bin_a<span style="color: #66cc66;">,</span> bin_b<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> test_add_b_larger<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
&nbsp;
        <span style="color: #483d8b;">&quot;&quot;&quot; B has more digits than A &quot;&quot;&quot;</span>
&nbsp;
        bin_a <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">'01000101'</span>
        bin_b <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">'1110010110101011101101011101000101'</span>
&nbsp;
        <span style="color: #008000;">self</span>._compare_additions<span style="color: black;">&#40;</span>bin_a<span style="color: #66cc66;">,</span> bin_b<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> test_add_b_smaller<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
&nbsp;
        <span style="color: #483d8b;">&quot;&quot;&quot;B has less digits than A&quot;&quot;&quot;</span>
&nbsp;
&nbsp;
        bin_a <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">'1110001001001001001101101011000101'</span>
        bin_b <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">'0110010'</span>
&nbsp;
        <span style="color: #008000;">self</span>._compare_additions<span style="color: black;">&#40;</span>bin_a<span style="color: #66cc66;">,</span> bin_b<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> _compare_additions<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: #66cc66;">,</span> bin_a<span style="color: #66cc66;">,</span> bin_b<span style="color: black;">&#41;</span>:
&nbsp;
        <span style="color: #483d8b;">&quot;&quot;&quot;Compares the python implementation against ours&quot;&quot;&quot;</span>
&nbsp;
        bin_add <span style="color: #66cc66;">=</span> binary_addition<span style="color: black;">&#40;</span>bin_a<span style="color: #66cc66;">,</span> bin_b<span style="color: black;">&#41;</span>
        py_add <span style="color: #66cc66;">=</span> bin<span style="color: black;">&#40;</span><span style="color: #008000;">eval</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;0b%s&quot;</span> % bin_a<span style="color: black;">&#41;</span> + <span style="color: #008000;">eval</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;0b%s&quot;</span> % bin_b<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
        algo_res <span style="color: #66cc66;">=</span> bin_add<span style="color: black;">&#91;</span>bin_add.<span style="color: black;">index</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'1'</span><span style="color: black;">&#41;</span>:<span style="color: black;">&#93;</span>
        py_res <span style="color: #66cc66;">=</span> py_add<span style="color: black;">&#91;</span>py_add.<span style="color: black;">index</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'1'</span><span style="color: black;">&#41;</span>:<span style="color: black;">&#93;</span>
&nbsp;
        <span style="color: #008000;">self</span>.<span style="color: black;">assertEqual</span><span style="color: black;">&#40;</span>py_res<span style="color: #66cc66;">,</span> algo_res<span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #483d8b;">'__main__'</span> <span style="color: #66cc66;">==</span> __name__:
    <span style="color: #dc143c;">unittest</span>.<span style="color: black;">main</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<h2>Binary Multiplication</h2>
<p>We are still using strings to represent bit sequences. We are still working from right to left, shifting to the left every number from the first number that needs to be multiplied by 1 in the second number. We then append the result to a list that we sum at the end, using the previous algorithm.</p>
<h3>Code:</h3>

<div class="wp_syntax"><table><tr><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #483d8b;">&quot;&quot;&quot;Simple Binary multiplication algorithm&quot;&quot;&quot;</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> binary_addition <span style="color: #ff7700;font-weight:bold;">import</span> binary_addition
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> binary_multiplication<span style="color: black;">&#40;</span>bin_a<span style="color: #66cc66;">,</span> bin_b<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;Multiplies two binary numbers by using python lists to
    easily shift digits around&quot;&quot;&quot;</span>
&nbsp;
    temp_result <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
    result <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">&quot;0&quot;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># Remove any unnecessary padding</span>
    bin_a <span style="color: #66cc66;">=</span> bin_a<span style="color: black;">&#91;</span>bin_a.<span style="color: black;">index</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'1'</span><span style="color: black;">&#41;</span>:<span style="color: black;">&#93;</span>
    bin_b <span style="color: #66cc66;">=</span> bin_b<span style="color: black;">&#91;</span>bin_b.<span style="color: black;">index</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'1'</span><span style="color: black;">&#41;</span>:<span style="color: black;">&#93;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>bin_b<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span>::-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> bin_b<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span> <span style="color: #66cc66;">==</span> <span style="color: #483d8b;">'1'</span>:
            temp_result.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;%s%s&quot;</span> % <span style="color: black;">&#40;</span>bin_a<span style="color: #66cc66;">,</span> <span style="color: #483d8b;">&quot;0&quot;</span> * <span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>bin_b<span style="color: black;">&#41;</span>-i-<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            temp_result.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;0&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">for</span> val <span style="color: #ff7700;font-weight:bold;">in</span> temp_result:
        result <span style="color: #66cc66;">=</span> binary_addition<span style="color: black;">&#40;</span><span style="color: #008000;">str</span><span style="color: black;">&#40;</span>result<span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>val<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">return</span> result</pre></td></tr></table></div>

<h3>Unit Test:</h3>

<div class="wp_syntax"><table><tr><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #483d8b;">&quot;&quot;&quot;Unit test for binary_addition.py&quot;&quot;&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">unittest</span>
<span style="color: #ff7700;font-weight:bold;">from</span> binary_multiplication <span style="color: #ff7700;font-weight:bold;">import</span> binary_multiplication
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> TestBinaryAddition<span style="color: black;">&#40;</span><span style="color: #dc143c;">unittest</span>.<span style="color: black;">TestCase</span><span style="color: black;">&#41;</span>:
&nbsp;
    <span style="color: #483d8b;">&quot;&quot;&quot;Comparing python binary multiplication against our own&quot;&quot;&quot;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> test_add_same_length<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
&nbsp;
        <span style="color: #483d8b;">&quot;&quot;&quot;Multiplying 2 binary numbers of the same length&quot;&quot;&quot;</span>
&nbsp;
        bin_a <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">'001001101'</span>
        bin_b <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">'011011010'</span>
&nbsp;
        <span style="color: #008000;">self</span>._compare_multiplications<span style="color: black;">&#40;</span>bin_a<span style="color: #66cc66;">,</span> bin_b<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> test_simple<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
&nbsp;
        <span style="color: #483d8b;">&quot;&quot;&quot;Multiplying 2 simple binary numbers&quot;&quot;&quot;</span>
        bin_a <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">'110'</span>
        bin_b <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">'101'</span>
&nbsp;
        <span style="color: #008000;">self</span>._compare_multiplications<span style="color: black;">&#40;</span>bin_a<span style="color: #66cc66;">,</span> bin_b<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> test_add_b_larger<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
&nbsp;
        <span style="color: #483d8b;">&quot;&quot;&quot;B has more digits than A&quot;&quot;&quot;</span>
&nbsp;
        bin_a <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">'010'</span>
        bin_b <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">'111001011000101'</span>
&nbsp;
        <span style="color: #008000;">self</span>._compare_multiplications<span style="color: black;">&#40;</span>bin_a<span style="color: #66cc66;">,</span> bin_b<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> test_add_b_smaller<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
&nbsp;
        <span style="color: #483d8b;">&quot;&quot;&quot; B has less digits than A &quot;&quot;&quot;</span>
&nbsp;
        bin_a <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">'111001011000101'</span>
        bin_b <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">'010'</span>
&nbsp;
        <span style="color: #008000;">self</span>._compare_multiplications<span style="color: black;">&#40;</span>bin_a<span style="color: #66cc66;">,</span> bin_b<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> _compare_multiplications<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: #66cc66;">,</span> bin_a<span style="color: #66cc66;">,</span> bin_b<span style="color: black;">&#41;</span>:
&nbsp;
        <span style="color: #483d8b;">&quot;&quot;&quot;Compares the python implementation against ours&quot;&quot;&quot;</span>
&nbsp;
        bin_mult <span style="color: #66cc66;">=</span> binary_multiplication<span style="color: black;">&#40;</span>bin_a<span style="color: #66cc66;">,</span> bin_b<span style="color: black;">&#41;</span>
        py_mult <span style="color: #66cc66;">=</span> bin<span style="color: black;">&#40;</span><span style="color: #008000;">eval</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;0b%s&quot;</span> % bin_a<span style="color: black;">&#41;</span> * <span style="color: #008000;">eval</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;0b%s&quot;</span> % bin_b<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
        algo_res <span style="color: #66cc66;">=</span> bin_mult<span style="color: black;">&#91;</span>bin_mult.<span style="color: black;">index</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'1'</span><span style="color: black;">&#41;</span>:<span style="color: black;">&#93;</span>
        py_res <span style="color: #66cc66;">=</span> py_mult<span style="color: black;">&#91;</span>py_mult.<span style="color: black;">index</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'1'</span><span style="color: black;">&#41;</span>:<span style="color: black;">&#93;</span>
&nbsp;
        <span style="color: #008000;">self</span>.<span style="color: black;">assertEqual</span><span style="color: black;">&#40;</span>py_res<span style="color: #66cc66;">,</span> algo_res<span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #483d8b;">'__main__'</span> <span style="color: #66cc66;">==</span> __name__:
    <span style="color: #dc143c;">unittest</span>.<span style="color: black;">main</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p><a class="a2a_button_reddit" href="http://www.addtoany.com/add_to/reddit?linkurl=http%3A%2F%2Fblog.madpython.com%2F2010%2F07%2F17%2Falgorithms-in-python-binary-operations%2F&amp;linkname=Algorithms%20in%20Python%3A%20Binary%20Operations" title="Reddit" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/reddit.png" width="16" height="16" alt="Reddit"/></a><a class="a2a_button_delicious" href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fblog.madpython.com%2F2010%2F07%2F17%2Falgorithms-in-python-binary-operations%2F&amp;linkname=Algorithms%20in%20Python%3A%20Binary%20Operations" title="Delicious" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/delicious.png" width="16" height="16" alt="Delicious"/></a><a class="a2a_button_google_bookmarks" href="http://www.addtoany.com/add_to/google_bookmarks?linkurl=http%3A%2F%2Fblog.madpython.com%2F2010%2F07%2F17%2Falgorithms-in-python-binary-operations%2F&amp;linkname=Algorithms%20in%20Python%3A%20Binary%20Operations" title="Google Bookmarks" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/google.png" width="16" height="16" alt="Google Bookmarks"/></a><a class="a2a_button_facebook" href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fblog.madpython.com%2F2010%2F07%2F17%2Falgorithms-in-python-binary-operations%2F&amp;linkname=Algorithms%20in%20Python%3A%20Binary%20Operations" title="Facebook" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/facebook.png" width="16" height="16" alt="Facebook"/></a><a class="a2a_button_stumbleupon" href="http://www.addtoany.com/add_to/stumbleupon?linkurl=http%3A%2F%2Fblog.madpython.com%2F2010%2F07%2F17%2Falgorithms-in-python-binary-operations%2F&amp;linkname=Algorithms%20in%20Python%3A%20Binary%20Operations" title="StumbleUpon" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/stumbleupon.png" width="16" height="16" alt="StumbleUpon"/></a><a class="a2a_button_linkedin" href="http://www.addtoany.com/add_to/linkedin?linkurl=http%3A%2F%2Fblog.madpython.com%2F2010%2F07%2F17%2Falgorithms-in-python-binary-operations%2F&amp;linkname=Algorithms%20in%20Python%3A%20Binary%20Operations" title="LinkedIn" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/linkedin.png" width="16" height="16" alt="LinkedIn"/></a><a class="a2a_button_twitter" href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fblog.madpython.com%2F2010%2F07%2F17%2Falgorithms-in-python-binary-operations%2F&amp;linkname=Algorithms%20in%20Python%3A%20Binary%20Operations" title="Twitter" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.madpython.com%2F2010%2F07%2F17%2Falgorithms-in-python-binary-operations%2F&amp;title=Algorithms%20in%20Python%3A%20Binary%20Operations" id="wpa2a_8">Share</a></p><div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href='http://blog.madpython.com/2010/08/07/algorithms-in-python-binary-exponentiation/' rel='bookmark' title='Algorithms in Python: Binary exponentiation'>Algorithms in Python: Binary exponentiation</a></li>
<li><a href='http://blog.madpython.com/2010/07/10/algorithms-in-python-base-expansion/' rel='bookmark' title='Algorithms in Python: Base Expansion'>Algorithms in Python: Base Expansion</a></li>
<li><a href='http://blog.madpython.com/2010/10/17/fuzzy-mathematics-with-fuzzpy-part-1/' rel='bookmark' title='Fuzzy Mathematics with FuzzPy (Part 1)'>Fuzzy Mathematics with FuzzPy (Part 1)</a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.madpython.com/2010/07/17/algorithms-in-python-binary-operations/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Algorithms in Python: Base Expansion</title>
		<link>http://blog.madpython.com/2010/07/10/algorithms-in-python-base-expansion/</link>
		<comments>http://blog.madpython.com/2010/07/10/algorithms-in-python-base-expansion/#comments</comments>
		<pubDate>Sun, 11 Jul 2010 00:53:49 +0000</pubDate>
		<dc:creator>Xavier</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[tech]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[CS]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://blog.madpython.com/?p=111</guid>
		<description><![CDATA[I felt it would be helpful to folks interested in Python and studying algorithms, to review some commonly studied algorithms in Computer Science by providing a small description and a Python implementation of each algorithm. This week, we&#8217;ll cover an &#8230; <a href="http://blog.madpython.com/2010/07/10/algorithms-in-python-base-expansion/">Continue reading <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href='http://blog.madpython.com/2010/08/07/algorithms-in-python-binary-exponentiation/' rel='bookmark' title='Algorithms in Python: Binary exponentiation'>Algorithms in Python: Binary exponentiation</a></li>
<li><a href='http://blog.madpython.com/2010/07/17/algorithms-in-python-binary-operations/' rel='bookmark' title='Algorithms in Python: Binary Operations'>Algorithms in Python: Binary Operations</a></li>
<li><a href='http://blog.madpython.com/2010/10/17/fuzzy-mathematics-with-fuzzpy-part-1/' rel='bookmark' title='Fuzzy Mathematics with FuzzPy (Part 1)'>Fuzzy Mathematics with FuzzPy (Part 1)</a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p>I felt it would be helpful to folks interested in Python and studying algorithms, to review some commonly studied algorithms in Computer Science by providing a small description and a Python implementation of each algorithm.</p>
<p>This week, we&#8217;ll cover an introductory algorithm for converting from one numeral base to another. Here is the python code:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #483d8b;">&quot;&quot;&quot;Simple implementation of a base expansion algorithm&quot;&quot;&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">math</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> base_expand<span style="color: black;">&#40;</span>base<span style="color: #66cc66;">,</span> val<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;This simple function performs a base-expansion from decimal
    using moduli and a translation table. The translation table is
    a clear limitation here, in that it implies the maximum base
    is 36.&quot;&quot;&quot;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>base <span style="color: #66cc66;">&lt;</span> <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">or</span> <span style="color: black;">&#40;</span>base <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">36</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">raise</span> BaseOutOfBoundsError<span style="color: black;">&#40;</span>base<span style="color: black;">&#41;</span>
&nbsp;
    trans_table <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'</span>
    res <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">''</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">while</span> val <span style="color: #66cc66;">!=</span> <span style="color: #ff4500;">0</span>:
        res +<span style="color: #66cc66;">=</span> trans_table<span style="color: black;">&#91;</span><span style="color: black;">&#40;</span><span style="color: #008000;">int</span><span style="color: black;">&#40;</span>val % base<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
        val <span style="color: #66cc66;">=</span> <span style="color: #dc143c;">math</span>.<span style="color: black;">floor</span><span style="color: black;">&#40;</span>val / base<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> res<span style="color: black;">&#91;</span>::-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> BaseOutOfBoundsError<span style="color: black;">&#40;</span><span style="color: #008000;">Exception</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;Base must be between 2 and 36&quot;&quot;&quot;</span>
    <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><span style="color: #66cc66;">,</span> val<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">val</span> <span style="color: #66cc66;">=</span> val
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__str__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>Invalid base: %s. Base must be (x | x &gt; 1; x &lt; 37)&quot;</span> % \
            <span style="color: #008000;">self</span>.<span style="color: black;">val</span></pre></td></tr></table></div>

<p>Here is a unit-test for base_expand.py:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #483d8b;">&quot;&quot;&quot;Unit tests for base_expansion.py&quot;&quot;&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">unittest</span>
<span style="color: #ff7700;font-weight:bold;">from</span> base_expansion <span style="color: #ff7700;font-weight:bold;">import</span> base_expand<span style="color: #66cc66;">,</span> BaseOutOfBoundsError
&nbsp;
<span style="color: #808080; font-style: italic;"># Unit tests for base_expansion.py</span>
<span style="color: #ff7700;font-weight:bold;">class</span> TestVals<span style="color: black;">&#40;</span><span style="color: #dc143c;">unittest</span>.<span style="color: black;">TestCase</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;Test suite&quot;&quot;&quot;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> test_known_values<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;&quot;Testing against known values&quot;&quot;&quot;</span>
&nbsp;
        vals <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span><span style="color: black;">&#123;</span><span style="color: #483d8b;">'val'</span>: <span style="color: #ff4500;">8</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'base'</span>: <span style="color: #ff4500;">2</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'expect'</span>: <span style="color: #483d8b;">'1000'</span><span style="color: black;">&#125;</span><span style="color: #66cc66;">,</span>
            <span style="color: black;">&#123;</span><span style="color: #483d8b;">'val'</span>: <span style="color: #ff4500;">915652</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'base'</span>: <span style="color: #ff4500;">16</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'expect'</span>: <span style="color: #483d8b;">'DF8C4'</span><span style="color: black;">&#125;</span><span style="color: #66cc66;">,</span>
            <span style="color: black;">&#123;</span><span style="color: #483d8b;">'val'</span>: <span style="color: #ff4500;">256</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'base'</span>: <span style="color: #ff4500;">10</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'expect'</span>: <span style="color: #483d8b;">'256'</span><span style="color: black;">&#125;</span><span style="color: #66cc66;">,</span>
            <span style="color: black;">&#123;</span><span style="color: #483d8b;">'val'</span>: <span style="color: #ff4500;">88189</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'base'</span>: <span style="color: #ff4500;">8</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'expect'</span>: <span style="color: #483d8b;">'254175'</span><span style="color: black;">&#125;</span><span style="color: black;">&#93;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">for</span> val <span style="color: #ff7700;font-weight:bold;">in</span> vals:
            <span style="color: #008000;">self</span>.<span style="color: black;">assertEqual</span><span style="color: black;">&#40;</span>val<span style="color: black;">&#91;</span><span style="color: #483d8b;">'expect'</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> \
                base_expand<span style="color: black;">&#40;</span>val<span style="color: black;">&#91;</span><span style="color: #483d8b;">'base'</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> val<span style="color: black;">&#91;</span><span style="color: #483d8b;">'val'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> test_invalid_base<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;&quot; Testing invalid bases &quot;&quot;&quot;</span>
&nbsp;
        bases <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">37</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">50</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">100</span><span style="color: black;">&#93;</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> base <span style="color: #ff7700;font-weight:bold;">in</span> bases:
            <span style="color: #008000;">self</span>.<span style="color: black;">assertRaises</span><span style="color: black;">&#40;</span>BaseOutOfBoundsError<span style="color: #66cc66;">,</span> base_expand<span style="color: #66cc66;">,</span> \
                base<span style="color: #66cc66;">,</span> <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #483d8b;">'__main__'</span> <span style="color: #66cc66;">==</span> __name__:
    <span style="color: #dc143c;">unittest</span>.<span style="color: black;">main</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p><a class="a2a_button_reddit" href="http://www.addtoany.com/add_to/reddit?linkurl=http%3A%2F%2Fblog.madpython.com%2F2010%2F07%2F10%2Falgorithms-in-python-base-expansion%2F&amp;linkname=Algorithms%20in%20Python%3A%20Base%20Expansion" title="Reddit" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/reddit.png" width="16" height="16" alt="Reddit"/></a><a class="a2a_button_delicious" href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fblog.madpython.com%2F2010%2F07%2F10%2Falgorithms-in-python-base-expansion%2F&amp;linkname=Algorithms%20in%20Python%3A%20Base%20Expansion" title="Delicious" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/delicious.png" width="16" height="16" alt="Delicious"/></a><a class="a2a_button_google_bookmarks" href="http://www.addtoany.com/add_to/google_bookmarks?linkurl=http%3A%2F%2Fblog.madpython.com%2F2010%2F07%2F10%2Falgorithms-in-python-base-expansion%2F&amp;linkname=Algorithms%20in%20Python%3A%20Base%20Expansion" title="Google Bookmarks" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/google.png" width="16" height="16" alt="Google Bookmarks"/></a><a class="a2a_button_facebook" href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fblog.madpython.com%2F2010%2F07%2F10%2Falgorithms-in-python-base-expansion%2F&amp;linkname=Algorithms%20in%20Python%3A%20Base%20Expansion" title="Facebook" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/facebook.png" width="16" height="16" alt="Facebook"/></a><a class="a2a_button_stumbleupon" href="http://www.addtoany.com/add_to/stumbleupon?linkurl=http%3A%2F%2Fblog.madpython.com%2F2010%2F07%2F10%2Falgorithms-in-python-base-expansion%2F&amp;linkname=Algorithms%20in%20Python%3A%20Base%20Expansion" title="StumbleUpon" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/stumbleupon.png" width="16" height="16" alt="StumbleUpon"/></a><a class="a2a_button_linkedin" href="http://www.addtoany.com/add_to/linkedin?linkurl=http%3A%2F%2Fblog.madpython.com%2F2010%2F07%2F10%2Falgorithms-in-python-base-expansion%2F&amp;linkname=Algorithms%20in%20Python%3A%20Base%20Expansion" title="LinkedIn" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/linkedin.png" width="16" height="16" alt="LinkedIn"/></a><a class="a2a_button_twitter" href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fblog.madpython.com%2F2010%2F07%2F10%2Falgorithms-in-python-base-expansion%2F&amp;linkname=Algorithms%20in%20Python%3A%20Base%20Expansion" title="Twitter" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.madpython.com%2F2010%2F07%2F10%2Falgorithms-in-python-base-expansion%2F&amp;title=Algorithms%20in%20Python%3A%20Base%20Expansion" id="wpa2a_10">Share</a></p><div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href='http://blog.madpython.com/2010/08/07/algorithms-in-python-binary-exponentiation/' rel='bookmark' title='Algorithms in Python: Binary exponentiation'>Algorithms in Python: Binary exponentiation</a></li>
<li><a href='http://blog.madpython.com/2010/07/17/algorithms-in-python-binary-operations/' rel='bookmark' title='Algorithms in Python: Binary Operations'>Algorithms in Python: Binary Operations</a></li>
<li><a href='http://blog.madpython.com/2010/10/17/fuzzy-mathematics-with-fuzzpy-part-1/' rel='bookmark' title='Fuzzy Mathematics with FuzzPy (Part 1)'>Fuzzy Mathematics with FuzzPy (Part 1)</a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.madpython.com/2010/07/10/algorithms-in-python-base-expansion/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Django Context Processors Best Practice</title>
		<link>http://blog.madpython.com/2010/04/07/django-context-processors-best-practice/</link>
		<comments>http://blog.madpython.com/2010/04/07/django-context-processors-best-practice/#comments</comments>
		<pubDate>Thu, 08 Apr 2010 00:59:39 +0000</pubDate>
		<dc:creator>Xavier</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[tech]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[Web Frameworks]]></category>
		<category><![CDATA[convention]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://blog.madpython.com/?p=61</guid>
		<description><![CDATA[In this post, i&#8217;ll show you a simple trick to ensure your Django context-processors don&#8217;t break when Django 1.2 (and possibly future releases as well) is released. If you&#8217;re not familiar with the concept of context-processors, I&#8217;ll also explain what &#8230; <a href="http://blog.madpython.com/2010/04/07/django-context-processors-best-practice/">Continue reading <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss yarpp-related-none'>

No related posts.
</div>
]]></description>
				<content:encoded><![CDATA[<p>In this post, i&#8217;ll show you a simple trick to ensure your Django context-processors don&#8217;t break when Django 1.2 (and possibly future releases as well) is released.</p>
<p>If you&#8217;re not familiar with the concept of context-processors, I&#8217;ll also explain what those are and how they work, so don&#8217;t panic. Finally, i&#8217;ll also spend a minute explaining why I consider this to be best-practice.</p>
<h2>What are template context processors?</h2>
<p>Django&#8217;s context processors are a facility that allows you to provide data and callbacks to your templates.</p>
<p>You can do so in one of two ways:</p>
<ol>
<li><strong>On an individual request basis</strong>: by passing a custom <code>Context</code> value to your <code>render_to_response()</code> call</li>
<li><strong>Globally</strong>: by creating a <em>context processor method</em> that accepts a <code>HttpRequest</code> object as input, and returns a payload or callback, <strong>then</strong> registering the context processor in your <code>settings.py</code>, <strong>then</strong> providing your <code>render_to_response()</code> call with the built-in <code>RequestContext</code> attribute instead of your own (you can always extend <code>RequestContext</code> to add more data on an individual request basis of course).</li>
</ol>
<p>If that approach for passing data to templates sounded absurd and obfuscated to you, you&#8217;re not alone. The complexity involved in such a simple operation is unwarranted and counter-productive, but every system has its shortcomings.</p>
<h2>The Issue</h2>
<p>If you follow the <a href="http://docs.djangoproject.com/en/dev/ref/templates/api/#id1" onclick="return TrackClick('http%3A%2F%2Fdocs.djangoproject.com%2Fen%2Fdev%2Fref%2Ftemplates%2Fapi%2F%23id1','Django+documentation')">Django documentation</a> or <a href="http://djangobook.com/en/2.0/" onclick="return TrackClick('http%3A%2F%2Fdjangobook.com%2Fen%2F2.0%2F','The+Django+Book')">The Django Book</a>&#8216;s approach to configuring your own custom context-processors, you&#8217;ll notice that you are encouraged to add to your settings.py a hardcoded list of built-in context processors. If you follow that approach, your context-processors declaration will look like this:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="python" style="font-family:monospace;">TEMPLATE_CONTEXT_PROCESSORS <span style="color: #66cc66;">=</span> <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;django.contrib.auth.context_processors.auth&quot;</span><span style="color: #66cc66;">,</span>
<span style="color: #483d8b;">&quot;django.core.context_processors.debug&quot;</span><span style="color: #66cc66;">,</span>
<span style="color: #483d8b;">&quot;django.core.context_processors.i18n&quot;</span><span style="color: #66cc66;">,</span>
<span style="color: #483d8b;">&quot;django.core.context_processors.media&quot;</span><span style="color: #66cc66;">,</span>
<span style="color: #483d8b;">&quot;django.contrib.messages.context_processors.messages&quot;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>But if you pay close attention to <a href="http://docs.djangoproject.com/en/dev/ref/settings/#template-context-processors" onclick="return TrackClick('http%3A%2F%2Fdocs.djangoproject.com%2Fen%2Fdev%2Fref%2Fsettings%2F%23template-context-processors','the+development+version%22s+documentation')">the development version&#8217;s documentation</a>, you&#8217;ll notice a couple of interesting notes:</p>
<blockquote><p><strong>Changed in Django Development version: </strong>&#8220;django.contrib.messages.context_processors.messages&#8221; was added to the default. For more information, see the messages documentation.<br />
<strong>Changed in Django Development version</strong>: The auth context processor was moved in this release from its old location django.core.context_processors.auth to django.contrib.auth.context_processors.auth.</p></blockquote>
<p>This is a red flag! The Django dev team would like you to use hardcoded values that reference to classes that will no longer exist once the next version is released, and that omit new processors that you are likely to require.</p>
<h2>The Solution</h2>
<p>Obviously Django has access to its own default settings, so there must be a way to simply <em>extend</em> the defaults instead of overriding them with hardcoded values. You just need to dig around a bit in <a href="http://code.djangoproject.com/browser/django/trunk/django/conf/global_settings.py#L186" onclick="return TrackClick('http%3A%2F%2Fcode.djangoproject.com%2Fbrowser%2Fdjango%2Ftrunk%2Fdjango%2Fconf%2Fglobal_settings.py%23L186','the+Django+source+code')">the Django source code</a> to find exactly how. I&#8217;ll save you some digging:</p>
<p>Add this at the top of your settings.py file:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> django.<span style="color: black;">conf</span>.<span style="color: black;">global_settings</span> <span style="color: #ff7700;font-weight:bold;">as</span> DEFAULT_SETTINGS</pre></td></tr></table></div>

<p>Then extend the default context processors:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="python" style="font-family:monospace;">TEMPLATE_CONTEXT_PROCESSORS <span style="color: #66cc66;">=</span> DEFAULT_SETTINGS.<span style="color: black;">TEMPLATE_CONTEXT_PROCESSORS</span> + <span style="color: black;">&#40;</span>
<span style="color: #483d8b;">&quot;myapp.context_processors.example&quot;</span><span style="color: #66cc66;">,</span>
<span style="color: #483d8b;">&quot;myapp.context_processors.other_example&quot;</span><span style="color: #66cc66;">,</span>
<span style="color: #808080; font-style: italic;"># etc...</span>
<span style="color: black;">&#41;</span></pre></td></tr></table></div>

<h2>Why is this a best-practice approach?</h2>
<p>In software-engineering, you want to ensure maximum (reasonable) future interoperability of all your components, and there is really no component as important as your actual development framework, Django or otherwise.</p>
<p>Using hardcoded values that are already defined somewhere not only breaks <acronym title="Don't Repeat Yourself">DRY</acronym>, but it also introduces possible breakage on framework upgrades. I know you are diligent and always read the release notes before upgrading critical components, and I know you use a staging environment to test those changes, but by actively looking out for this kind of traps, you have just saved yourself some debugging time and some head-scratching.</p>
<p>Happy coding!</p>
<p><a class="a2a_button_reddit" href="http://www.addtoany.com/add_to/reddit?linkurl=http%3A%2F%2Fblog.madpython.com%2F2010%2F04%2F07%2Fdjango-context-processors-best-practice%2F&amp;linkname=Django%20Context%20Processors%20Best%20Practice" title="Reddit" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/reddit.png" width="16" height="16" alt="Reddit"/></a><a class="a2a_button_delicious" href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fblog.madpython.com%2F2010%2F04%2F07%2Fdjango-context-processors-best-practice%2F&amp;linkname=Django%20Context%20Processors%20Best%20Practice" title="Delicious" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/delicious.png" width="16" height="16" alt="Delicious"/></a><a class="a2a_button_google_bookmarks" href="http://www.addtoany.com/add_to/google_bookmarks?linkurl=http%3A%2F%2Fblog.madpython.com%2F2010%2F04%2F07%2Fdjango-context-processors-best-practice%2F&amp;linkname=Django%20Context%20Processors%20Best%20Practice" title="Google Bookmarks" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/google.png" width="16" height="16" alt="Google Bookmarks"/></a><a class="a2a_button_facebook" href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fblog.madpython.com%2F2010%2F04%2F07%2Fdjango-context-processors-best-practice%2F&amp;linkname=Django%20Context%20Processors%20Best%20Practice" title="Facebook" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/facebook.png" width="16" height="16" alt="Facebook"/></a><a class="a2a_button_stumbleupon" href="http://www.addtoany.com/add_to/stumbleupon?linkurl=http%3A%2F%2Fblog.madpython.com%2F2010%2F04%2F07%2Fdjango-context-processors-best-practice%2F&amp;linkname=Django%20Context%20Processors%20Best%20Practice" title="StumbleUpon" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/stumbleupon.png" width="16" height="16" alt="StumbleUpon"/></a><a class="a2a_button_linkedin" href="http://www.addtoany.com/add_to/linkedin?linkurl=http%3A%2F%2Fblog.madpython.com%2F2010%2F04%2F07%2Fdjango-context-processors-best-practice%2F&amp;linkname=Django%20Context%20Processors%20Best%20Practice" title="LinkedIn" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/linkedin.png" width="16" height="16" alt="LinkedIn"/></a><a class="a2a_button_twitter" href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fblog.madpython.com%2F2010%2F04%2F07%2Fdjango-context-processors-best-practice%2F&amp;linkname=Django%20Context%20Processors%20Best%20Practice" title="Twitter" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.madpython.com%2F2010%2F04%2F07%2Fdjango-context-processors-best-practice%2F&amp;title=Django%20Context%20Processors%20Best%20Practice" id="wpa2a_12">Share</a></p><div class='yarpp-related-rss yarpp-related-none'>
<p>No related posts.</p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.madpython.com/2010/04/07/django-context-processors-best-practice/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Penguicon 7.0</title>
		<link>http://blog.madpython.com/2009/05/06/penguicon-70/</link>
		<comments>http://blog.madpython.com/2009/05/06/penguicon-70/#comments</comments>
		<pubDate>Thu, 07 May 2009 02:11:28 +0000</pubDate>
		<dc:creator>Xavier</dc:creator>
				<category><![CDATA[Events]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[tech]]></category>
		<category><![CDATA[arduino]]></category>
		<category><![CDATA[convention]]></category>
		<category><![CDATA[engineering]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[sci-fi]]></category>

		<guid isPermaLink="false">http://blog.madpython.com/?p=47</guid>
		<description><![CDATA[Our little gang from the Windsor Unix Users Group just got back from Penguicon 7.0 on Sunday night. As always, it was so much fun we hardly managed to get any sleep, and we all came home with a lot &#8230; <a href="http://blog.madpython.com/2009/05/06/penguicon-70/">Continue reading <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href='http://blog.madpython.com/2009/03/07/extending-postgresql-with-python/' rel='bookmark' title='Extending PostgreSQL with Python'>Extending PostgreSQL with Python</a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p>Our little gang from the <a href="http://www.wuug.org" onclick="return TrackClick('http%3A%2F%2Fwww.wuug.org','Windsor+Unix+Users+Group')">Windsor Unix Users Group</a> just got back from <a href="http://www.penguicon.org" onclick="return TrackClick('http%3A%2F%2Fwww.penguicon.org','PenguiCon')" onclick="return TrackClick('http%3A%2F%2Fwww.penguicon.org','Penguicon+7.0')">Penguicon 7.0</a> on Sunday night. As always, it was so much fun we hardly managed to get any sleep, and we all came home with a lot of really fun memories. </p>
<p>I didn&#8217;t get around to sit down and come up with a write-up about the experience as i&#8217;ve been working on other projects every evening this week, but I finally have a minute to do so now.</p>
<p>As far as the convention event itself went, there is definitely a lot of good and a few notable logistical oversights that are worth mentioning, but i&#8217;m not really interested in addressing either on here, as I&#8217;m really more interested in sharing my impression of the technical panels I&#8217;ve attended.</p>
<p>I have to say, I&#8217;m very excited about every single one of the panels I&#8217;ve been able to attend this year. They were all highly informative, and the speakers very motivated and passionate about their material. Here&#8217;s a short summary of the events I was especially excited about:</p>
<ul>
<li><strong>Neural Networks</strong><br />
This panel was held by <em>Dr. Stanley C. Mortel</em>. It explained the basic concepts behind the idea of building and training neural computation networks. It was a very abstract fly-by course, which I feel is a very appropriate way to introduce this type of material. There was a second part of this panel available the next day but we weren&#8217;t fortunate enough to attend it.
</li>
<li>
<strong>Reading by Will Wheaton</strong><br />
<em>Just kidding  <img src='http://blog.madpython.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </em>
</li>
<li>
<strong>Beginning PyGame Programming</strong><br />
This tutorial by <em>Craig Maloney</em> was my first real introduction to PyGame. Craig had a nice little demo environment all set up and ready for the presentation. He flew pretty quickly through many of the concepts behind PyGame while writing a Pong demo. Although he went through the material pretty quickly, I&#8217;m very interested in learning more about the platform as a result, so it&#8217;s safe to say the tutorial was a success as far as I&#8217;m concerned.
</li>
<li>
<strong>Open Hardware with Arduino</strong><br />
The speaker for this talk was <em>W Craig Trader</em>, and I have to say Craig was not only extremely knowledgeable about the Arduino (and obviously several other) platform, but also pretty excited about it, and since microcontrolers isn&#8217;t something I&#8217;ve ever bothered to learn anything about, I really didn&#8217;t expect to get so excited about the talk. The Arduino platform appears to be very accessible technically and financially, and also pretty powerful. Craig did an amazing job showing us the strengths and weaknesses of the platform and getting our whole group pretty excited to play with it!
</li>
<li>
<strong>High Performance PHP</strong><br />
This talk held by <em>Rasmus Lerdorf</em>, the creator of PHP and an infrastructure architect at Yahoo! Inc., took us through several performance optimization techniques for PHP apps, although many of the concepts featured in the talk could easily be applied to any apache-based app. It was very refreshing to finally see someone as experienced and well-rounded as him go through the tribulations of identifying and addressing performance bottlenecks in PHP apps. I was very interested in both the individual techniques highlighted during the talk, as well as the problem-solving process of a highly experienced software engineer, so that talk was the highlight of the con for me.
</li>
</ul>
<p>Unfortunately, due to very unfortunate logistical shortcomings, our group was unable to attend a lot of the panels we were looking forward to check out, but despite this, the con was a resounding success in every aspect you can think of. I&#8217;ve met some really cool and interesting people, learned a lot of very exciting stuff that will be guiding some of my personal research for months to come, learned some technical concepts that will directly impact my work performance, and had way too much fun.</p>
<p>The next stop on our list will probably be PyOhio. We had a chance to chat with <a href="http://catherinedevlin.blogspot.com/" onclick="return TrackClick('http%3A%2F%2Fcatherinedevlin.blogspot.com%2F','Catherine+Devlin')">Catherine Devlin</a>, a fellow Pythonista &#038; Oracle geek from IntelliTech Systems, who told us about it, and Aaron and I are looking into putting together a talk proposal for the con, if we can come up with it before the deadline, and we already have some pretty interesting ideas, so I&#8217;m looking forward to it. I&#8217;d also really love to attend PyCon 2010 in Atlanta!</p>
<p>If you&#8217;re interested in attending a convention where Linux and FOSS enthusiasts get a chance to have fun with the Sci-Fi crowd for a weekend of fun,  <a href="http://www.penguicon.org" onclick="return TrackClick('http%3A%2F%2Fwww.penguicon.org','PenguiCon')" onclick="return TrackClick('http%3A%2F%2Fwww.penguicon.org','Penguicon+7.0')">PenguiCon</a> is definitely for you!!</p>
<p><a class="a2a_button_reddit" href="http://www.addtoany.com/add_to/reddit?linkurl=http%3A%2F%2Fblog.madpython.com%2F2009%2F05%2F06%2Fpenguicon-70%2F&amp;linkname=Penguicon%207.0" title="Reddit" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/reddit.png" width="16" height="16" alt="Reddit"/></a><a class="a2a_button_delicious" href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fblog.madpython.com%2F2009%2F05%2F06%2Fpenguicon-70%2F&amp;linkname=Penguicon%207.0" title="Delicious" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/delicious.png" width="16" height="16" alt="Delicious"/></a><a class="a2a_button_google_bookmarks" href="http://www.addtoany.com/add_to/google_bookmarks?linkurl=http%3A%2F%2Fblog.madpython.com%2F2009%2F05%2F06%2Fpenguicon-70%2F&amp;linkname=Penguicon%207.0" title="Google Bookmarks" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/google.png" width="16" height="16" alt="Google Bookmarks"/></a><a class="a2a_button_facebook" href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fblog.madpython.com%2F2009%2F05%2F06%2Fpenguicon-70%2F&amp;linkname=Penguicon%207.0" title="Facebook" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/facebook.png" width="16" height="16" alt="Facebook"/></a><a class="a2a_button_stumbleupon" href="http://www.addtoany.com/add_to/stumbleupon?linkurl=http%3A%2F%2Fblog.madpython.com%2F2009%2F05%2F06%2Fpenguicon-70%2F&amp;linkname=Penguicon%207.0" title="StumbleUpon" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/stumbleupon.png" width="16" height="16" alt="StumbleUpon"/></a><a class="a2a_button_linkedin" href="http://www.addtoany.com/add_to/linkedin?linkurl=http%3A%2F%2Fblog.madpython.com%2F2009%2F05%2F06%2Fpenguicon-70%2F&amp;linkname=Penguicon%207.0" title="LinkedIn" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/linkedin.png" width="16" height="16" alt="LinkedIn"/></a><a class="a2a_button_twitter" href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fblog.madpython.com%2F2009%2F05%2F06%2Fpenguicon-70%2F&amp;linkname=Penguicon%207.0" title="Twitter" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.madpython.com%2F2009%2F05%2F06%2Fpenguicon-70%2F&amp;title=Penguicon%207.0" id="wpa2a_14">Share</a></p><div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href='http://blog.madpython.com/2009/03/07/extending-postgresql-with-python/' rel='bookmark' title='Extending PostgreSQL with Python'>Extending PostgreSQL with Python</a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.madpython.com/2009/05/06/penguicon-70/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Extending PostgreSQL with Python</title>
		<link>http://blog.madpython.com/2009/03/07/extending-postgresql-with-python/</link>
		<comments>http://blog.madpython.com/2009/03/07/extending-postgresql-with-python/#comments</comments>
		<pubDate>Sat, 07 Mar 2009 22:56:31 +0000</pubDate>
		<dc:creator>Xavier</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[tech]]></category>
		<category><![CDATA[unix]]></category>
		<category><![CDATA[postgres]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[scripting]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://blog.madpython.com/?p=40</guid>
		<description><![CDATA[One of the features I enjoy the most about PostgreSQL is the ability to write stored procedures in C, Perl, TCL, PgSQL, and yes&#8230; obviously also in Python. I&#8217;ve been using this feature since 7.4, so any recent version of &#8230; <a href="http://blog.madpython.com/2009/03/07/extending-postgresql-with-python/">Continue reading <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href='http://blog.madpython.com/2009/03/06/fixing-custom-sequences-in-postgresql/' rel='bookmark' title='Fixing custom sequences in PostgreSQL'>Fixing custom sequences in PostgreSQL</a></li>
<li><a href='http://blog.madpython.com/2009/03/01/ahh-those-cool-little-cli-tools/' rel='bookmark' title='Ahh those cool little CLI tools&#8230;'>Ahh those cool little CLI tools&#8230;</a></li>
<li><a href='http://blog.madpython.com/2009/03/02/apache2-shortcuts/' rel='bookmark' title='Apache2 shortcuts'>Apache2 shortcuts</a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p>One of the features I enjoy the most about PostgreSQL is the ability to write stored procedures in C, Perl, TCL, PgSQL, and yes&#8230; obviously also in Python. I&#8217;ve been using this feature since 7.4, so any recent version of PostgreSQL is pretty much guaranteed to support it, but you&#8217;ll need to have the pl/python procedural language contrib module installed. Once it&#8217;s installed, you can activate it for your current database using the following query:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">PROCEDURAL</span> <span style="color: #993333; font-weight: bold;">LANGUAGE</span> plpythonu;</pre></td></tr></table></div>

<p>Once the language bindings have been activated, you can start writing your stored procedures in python, however you should really read up on the following subjects first:</p>
<ul>
<li><a href="http://www.postgresql.org/docs/8.3/interactive/sql-createfunction.html" onclick="return TrackClick('http%3A%2F%2Fwww.postgresql.org%2Fdocs%2F8.3%2Finteractive%2Fsql-createfunction.html','CREATE+FUNCTION+statement+syntax')" target="_blank">CREATE FUNCTION statement syntax</a></li>
<li><a href="http://www.postgresql.org/docs/8.3/interactive/plpython-funcs.html" onclick="return TrackClick('http%3A%2F%2Fwww.postgresql.org%2Fdocs%2F8.3%2Finteractive%2Fplpython-funcs.html','PL%2FPython+Implementation+Documentation')" target="_blank">PL/Python Implementation Documentation</a></li>
</ul>
<p>As an example, I&#8217;ve written a little <acronym title="Stored Procedure">SP</acronym> in PL/Python to provide support for <acronym title="Perl-Compatible Regular Expressions">PCRE</acronym> since the stock distribution of PostgreSQL only supports LIKE/SIMILAR, and POSIX Style Regular Expressions.</p>
<p>Let&#8217;s create our Python language binding, and create a standard text storage table:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">-- Activate PL/Python</span>
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">PROCEDURAL</span> <span style="color: #993333; font-weight: bold;">LANGUAGE</span> plpythonu;
&nbsp;
<span style="color: #808080; font-style: italic;">-- Create a plain text-storage table</span>
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> text_storage
<span style="color: #66cc66;">&#40;</span>
  id serial <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  payload <span style="color: #993333; font-weight: bold;">CHARACTER</span> <span style="color: #993333; font-weight: bold;">VARYING</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">128</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">CONSTRAINT</span> text_storage_pkey <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">WITH</span> <span style="color: #66cc66;">&#40;</span>OIDS<span style="color: #66cc66;">=</span><span style="color: #993333; font-weight: bold;">FALSE</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">TABLE</span> text_storage OWNER <span style="color: #993333; font-weight: bold;">TO</span> xavier;
&nbsp;
<span style="color: #808080; font-style: italic;">-- Let's throw in an index on the payload field for good measure</span>
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">INDEX</span> txt_payload_idx
  <span style="color: #993333; font-weight: bold;">ON</span> text_storage
  <span style="color: #993333; font-weight: bold;">USING</span> btree
  <span style="color: #66cc66;">&#40;</span>payload<span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p>Let&#8217;s now populate our table with some junk data:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> text_storage <span style="color: #66cc66;">&#40;</span>payload<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'hello, world'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> text_storage <span style="color: #66cc66;">&#40;</span>payload<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'the quick brown fox, blah blah blah'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> text_storage <span style="color: #66cc66;">&#40;</span>payload<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'PCREs in Postgres'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> text_storage <span style="color: #66cc66;">&#40;</span>payload<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'All hail Python!'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> text_storage <span style="color: #66cc66;">&#40;</span>payload<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Hello, test data!'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> text_storage <span style="color: #66cc66;">&#40;</span>payload<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Python would like to say Hello!'</span><span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p>And now we can go ahead and create our Python SP itself:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="plsql" style="font-family:monospace;"><span style="color: #00F;">CREATE</span> <span style="color: #00F;">OR</span> <span style="color: #000;">REPLACE</span> <span style="color: #00F;">FUNCTION</span> pcre<span style="color: #00F;">&#40;</span>text<span style="color: #00F;">,</span> text<span style="color: #00F;">&#41;</span>
  RETURNS <span style="color: #00F;">INTEGER</span> <span style="color: #00F;">AS</span>
$BODY$import re
&nbsp;
regex  <span style="color: #00F;">=</span> args<span style="color: #00F;">&#91;</span><span style="color: #800;">0</span><span style="color: #00F;">&#93;</span>
in_str <span style="color: #00F;">=</span> args<span style="color: #00F;">&#91;</span><span style="color: #800;">1</span><span style="color: #00F;">&#93;</span>
&nbsp;
compiled <span style="color: #00F;">=</span> re<span style="color: #00F;">.</span>compile<span style="color: #00F;">&#40;</span>regex<span style="color: #00F;">&#41;</span>
&nbsp;
<span style="color: #00F;">IF</span> compiled<span style="color: #00F;">.</span>search<span style="color: #00F;">&#40;</span>in_str<span style="color: #00F;">&#41;</span><span style="color: #00F;">:</span>
	<span style="color: #00F;">RETURN</span> <span style="color: #800;">1</span>
<span style="color: #00F;">ELSE</span><span style="color: #00F;">:</span>
	<span style="color: #00F;">RETURN</span> <span style="color: #800;">0</span>$BODY$
  LANGUAGE <span style="color: #F00;">'plpythonu'</span> VOLATILE
  COST <span style="color: #800;">100</span><span style="color: #00F;">;</span></pre></td></tr></table></div>

<p>As you can see, our PCRE matching system is extremely simple, yet pretty powerful. We import Python&#8217;s built-in <strong>re</strong> module, compile the specified regex argument, then attempt to match it against the other argument. Here&#8217;s a usage example on our test table:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> id<span style="color: #66cc66;">,</span> payload <span style="color: #993333; font-weight: bold;">FROM</span> text_storage <span style="color: #993333; font-weight: bold;">WHERE</span> pcre<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'[H|h]ello'</span><span style="color: #66cc66;">,</span> payload<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span>;
 id <span style="color: #66cc66;">|</span>             payload             
<span style="color: #808080; font-style: italic;">----+---------------------------------</span>
  <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">|</span> hello<span style="color: #66cc66;">,</span> world
  <span style="color: #cc66cc;">5</span> <span style="color: #66cc66;">|</span> Hello<span style="color: #66cc66;">,</span> test <span style="color: #993333; font-weight: bold;">DATA</span>!
  <span style="color: #cc66cc;">6</span> <span style="color: #66cc66;">|</span> Python would <span style="color: #993333; font-weight: bold;">LIKE</span> <span style="color: #993333; font-weight: bold;">TO</span> say Hello!
<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span> <span style="color: #993333; font-weight: bold;">ROWS</span><span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>As always, feel free to suggest any improvements.</p>
<p><a class="a2a_button_reddit" href="http://www.addtoany.com/add_to/reddit?linkurl=http%3A%2F%2Fblog.madpython.com%2F2009%2F03%2F07%2Fextending-postgresql-with-python%2F&amp;linkname=Extending%20PostgreSQL%20with%20Python" title="Reddit" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/reddit.png" width="16" height="16" alt="Reddit"/></a><a class="a2a_button_delicious" href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fblog.madpython.com%2F2009%2F03%2F07%2Fextending-postgresql-with-python%2F&amp;linkname=Extending%20PostgreSQL%20with%20Python" title="Delicious" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/delicious.png" width="16" height="16" alt="Delicious"/></a><a class="a2a_button_google_bookmarks" href="http://www.addtoany.com/add_to/google_bookmarks?linkurl=http%3A%2F%2Fblog.madpython.com%2F2009%2F03%2F07%2Fextending-postgresql-with-python%2F&amp;linkname=Extending%20PostgreSQL%20with%20Python" title="Google Bookmarks" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/google.png" width="16" height="16" alt="Google Bookmarks"/></a><a class="a2a_button_facebook" href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fblog.madpython.com%2F2009%2F03%2F07%2Fextending-postgresql-with-python%2F&amp;linkname=Extending%20PostgreSQL%20with%20Python" title="Facebook" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/facebook.png" width="16" height="16" alt="Facebook"/></a><a class="a2a_button_stumbleupon" href="http://www.addtoany.com/add_to/stumbleupon?linkurl=http%3A%2F%2Fblog.madpython.com%2F2009%2F03%2F07%2Fextending-postgresql-with-python%2F&amp;linkname=Extending%20PostgreSQL%20with%20Python" title="StumbleUpon" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/stumbleupon.png" width="16" height="16" alt="StumbleUpon"/></a><a class="a2a_button_linkedin" href="http://www.addtoany.com/add_to/linkedin?linkurl=http%3A%2F%2Fblog.madpython.com%2F2009%2F03%2F07%2Fextending-postgresql-with-python%2F&amp;linkname=Extending%20PostgreSQL%20with%20Python" title="LinkedIn" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/linkedin.png" width="16" height="16" alt="LinkedIn"/></a><a class="a2a_button_twitter" href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fblog.madpython.com%2F2009%2F03%2F07%2Fextending-postgresql-with-python%2F&amp;linkname=Extending%20PostgreSQL%20with%20Python" title="Twitter" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.madpython.com%2F2009%2F03%2F07%2Fextending-postgresql-with-python%2F&amp;title=Extending%20PostgreSQL%20with%20Python" id="wpa2a_16">Share</a></p><div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href='http://blog.madpython.com/2009/03/06/fixing-custom-sequences-in-postgresql/' rel='bookmark' title='Fixing custom sequences in PostgreSQL'>Fixing custom sequences in PostgreSQL</a></li>
<li><a href='http://blog.madpython.com/2009/03/01/ahh-those-cool-little-cli-tools/' rel='bookmark' title='Ahh those cool little CLI tools&#8230;'>Ahh those cool little CLI tools&#8230;</a></li>
<li><a href='http://blog.madpython.com/2009/03/02/apache2-shortcuts/' rel='bookmark' title='Apache2 shortcuts'>Apache2 shortcuts</a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.madpython.com/2009/03/07/extending-postgresql-with-python/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Fixing custom sequences in PostgreSQL</title>
		<link>http://blog.madpython.com/2009/03/06/fixing-custom-sequences-in-postgresql/</link>
		<comments>http://blog.madpython.com/2009/03/06/fixing-custom-sequences-in-postgresql/#comments</comments>
		<pubDate>Sat, 07 Mar 2009 00:52:47 +0000</pubDate>
		<dc:creator>Xavier</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[tech]]></category>
		<category><![CDATA[unix]]></category>
		<category><![CDATA[postgres]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[scripting]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://blog.madpython.com/?p=35</guid>
		<description><![CDATA[PostgreSQL provides a mechanism called sequences, which I believe is extracted from ANSI-SQL92, though I&#8217;m too lazy to check, which are basically stateful counters that provide some helper functions. The primary use of sequences in PostgreSQL and Oracle, is to &#8230; <a href="http://blog.madpython.com/2009/03/06/fixing-custom-sequences-in-postgresql/">Continue reading <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href='http://blog.madpython.com/2009/03/07/extending-postgresql-with-python/' rel='bookmark' title='Extending PostgreSQL with Python'>Extending PostgreSQL with Python</a></li>
<li><a href='http://blog.madpython.com/2009/03/02/apache2-shortcuts/' rel='bookmark' title='Apache2 shortcuts'>Apache2 shortcuts</a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p>PostgreSQL provides a mechanism called sequences, which I believe is extracted from ANSI-SQL92, though I&#8217;m too lazy to check, which are basically stateful counters that provide some helper functions. The primary use of sequences in PostgreSQL and Oracle, is to implement auto-incrementing counters as a table field.</p>
<p>PostgreSQL will automatically create a sequence when you use the &#8220;SERIAL&#8221; datatype for your field, and will take care of assigning the default value of your field as the result of the nextval() call on the sequence, so most of the time you don&#8217;t need to interact directly with sequences.</p>
<p>Where things can get hairy however, is when you back up your data using pg_dump or any other mechanism, and restore that data in a table that is already populated. A common scenario for example, is populating a table that already has some recent data, with some older data you&#8217;ve been storing in archival. The opposite is true if you are trying to archive data from a table to a backup database for example.</p>
<p>Whenever you manually have to provide a value for the field assigned to a sequence, you are pretty much guaranteed to break the sequence unless you take the time to nextval() your sequence until it is in sync. This is a real problem, as pg_dump does not include sequence synchronization in its output.</p>
<p>The quickest way to synchronize a sequence, based on my observations, is to run the following query, taking care to replace the name of the sequence [SEQ] and the name of the associated table [TABLE] and field [FIELD]:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">SETVAL</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>SEQ<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">COALESCE</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">&#91;</span><span style="color: #993333; font-weight: bold;">FIELD</span><span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #66cc66;">&#91;</span><span style="color: #993333; font-weight: bold;">TABLE</span><span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #66cc66;">&#91;</span><span style="color: #993333; font-weight: bold;">FIELD</span><span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">DESC</span> <span style="color: #993333; font-weight: bold;">LIMIT</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">+</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>This will fetch the highest value of [FIELD] in [TABLE], increment it by 1, and synchronize the sequence [SEQ] to the new value.</p>
<p>I&#8217;ve also written the following little Python script that will look for any <strong><em>non-system</em></strong> sequence in the specified database, and use this method to repair it. Let me know if it works out for you, or if you&#8217;d like to suggest some improvements.</p>
<p><strong>Requirements</strong>: Python 2.5+, PsycoPG2 Python module (python-psycopg2)</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/env python</span>
<span style="color: #808080; font-style: italic;"># -*- coding: utf-8 -*-</span>
&nbsp;
&nbsp;
<span style="color: #808080; font-style: italic;"># Standard imports</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span>
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">optparse</span> <span style="color: #ff7700;font-weight:bold;">import</span> OptionParser
&nbsp;
<span style="color: #808080; font-style: italic;"># psycopg2 import</span>
<span style="color: #ff7700;font-weight:bold;">try</span>:
    <span style="color: #ff7700;font-weight:bold;">import</span> psycopg2
<span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">ImportError</span><span style="color: #66cc66;">,</span> e:
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'You must install the python module named &quot;psycopg2&quot; in order to use this module.'</span>
    <span style="color: #dc143c;">sys</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">EX_SOFTWARE</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> PgRepairman:
    <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><span style="color: #66cc66;">,</span> options<span style="color: #66cc66;">,</span> <span style="color: #dc143c;">parser</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">options</span> <span style="color: #66cc66;">=</span> options
        <span style="color: #ff7700;font-weight:bold;">try</span>:
            dsn <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">&quot;dbname=%s host=%s user=%s&quot;</span> % <span style="color: black;">&#40;</span>options.<span style="color: black;">db</span><span style="color: #66cc66;">,</span> options.<span style="color: black;">host</span><span style="color: #66cc66;">,</span> options.<span style="color: #dc143c;">user</span><span style="color: black;">&#41;</span>
            dsn +<span style="color: #66cc66;">=</span> <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;&quot;</span> <span style="color: #66cc66;">!=</span> options.<span style="color: black;">passwd</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;password=%s&quot;</span> % options.<span style="color: black;">password</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">or</span> <span style="color: #483d8b;">&quot;&quot;</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">conn</span> <span style="color: #66cc66;">=</span> psycopg2.<span style="color: black;">connect</span><span style="color: black;">&#40;</span>dsn<span style="color: black;">&#41;</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">curs</span> <span style="color: #66cc66;">=</span> <span style="color: #008000;">self</span>.<span style="color: black;">conn</span>.<span style="color: black;">cursor</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">Exception</span><span style="color: #66cc66;">,</span> e:
            <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;ERROR - %s&quot;</span> % e
            <span style="color: #dc143c;">sys</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
    <span style="color: #808080; font-style: italic;"># Returns a dict for a psycopg2 row object</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> _to_dict<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: #66cc66;">,</span> desc<span style="color: #66cc66;">,</span> res<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">dict</span><span style="color: black;">&#40;</span><span style="color: #008000;">zip</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span>x<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> <span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> desc<span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> res<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
    <span style="color: #808080; font-style: italic;"># Attempt to locate all custom sequences</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> findSequences<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        seq_query <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">&quot;&quot;&quot;
        SELECT pc1.relname AS seq, pc2.relname AS table, c.attname AS field 
        FROM pg_depend, pg_class pc1, pg_class pc2, pg_attribute c 
        WHERE pc1.oid = pg_depend.objid 
            AND pc2.oid = pg_depend.refobjid 
            AND c.attnum = pg_depend.refobjsubid 
            AND c.attrelid = pc2.oid 
            AND pc1.relkind = 'S' 
            AND pc1.relname NOT LIKE 'pg_toast%%'
        &quot;&quot;&quot;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">try</span>:
            <span style="color: #008000;">self</span>.<span style="color: black;">print_verbose</span><span style="color: black;">&#40;</span>seq_query<span style="color: black;">&#41;</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">curs</span>.<span style="color: black;">execute</span><span style="color: black;">&#40;</span>seq_query<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">Exception</span><span style="color: #66cc66;">,</span> e:
            <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;[ERROR] - %s&quot;</span> % e
            <span style="color: #dc143c;">sys</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
&nbsp;
        desc <span style="color: #66cc66;">=</span> <span style="color: #008000;">self</span>.<span style="color: black;">curs</span>.<span style="color: black;">description</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> row <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">self</span>.<span style="color: black;">curs</span>.<span style="color: black;">fetchall</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
            <span style="color: #ff7700;font-weight:bold;">yield</span> <span style="color: #008000;">self</span>._to_dict<span style="color: black;">&#40;</span>desc<span style="color: #66cc66;">,</span> row<span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
    <span style="color: #808080; font-style: italic;"># Increment the key value to the value of the sequence + 1</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> fixSequences<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">for</span> seq <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">self</span>.<span style="color: black;">findSequences</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
            <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Fixing sequence %s in table %s&quot;</span> % <span style="color: black;">&#40;</span>seq<span style="color: black;">&#91;</span><span style="color: #483d8b;">'seq'</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> seq<span style="color: black;">&#91;</span><span style="color: #483d8b;">'table'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
            fix_query <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">&quot;SELECT setval('%s', COALESCE((SELECT %s FROM %s ORDER BY %s DESC LIMIT 1), 0)+1)&quot;</span> % <span style="color: black;">&#40;</span>seq<span style="color: black;">&#91;</span><span style="color: #483d8b;">'seq'</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> seq<span style="color: black;">&#91;</span><span style="color: #483d8b;">'field'</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> seq<span style="color: black;">&#91;</span><span style="color: #483d8b;">'table'</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> seq<span style="color: black;">&#91;</span><span style="color: #483d8b;">'field'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">try</span>:
                <span style="color: #008000;">self</span>.<span style="color: black;">print_verbose</span><span style="color: black;">&#40;</span>fix_query<span style="color: black;">&#41;</span>
                <span style="color: #008000;">self</span>.<span style="color: black;">curs</span>.<span style="color: black;">execute</span><span style="color: black;">&#40;</span>fix_query<span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">Exception</span><span style="color: #66cc66;">,</span> e:
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;[WARNING] - %s&quot;</span> % e
                <span style="color: #ff7700;font-weight:bold;">pass</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> print_verbose<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: #66cc66;">,</span> msg<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span><span style="color: #008000;">True</span> <span style="color: #66cc66;">==</span> <span style="color: #008000;">self</span>.<span style="color: black;">options</span>.<span style="color: black;">verbose</span><span style="color: black;">&#41;</span>:
            <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;[DEBUG] - %s&quot;</span> % msg
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__<span style="color: #66cc66;">==</span><span style="color: #483d8b;">'__main__'</span>:
    usage       <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">&quot;Usage: %prog &lt;options&gt; [-v --verbose] [-u --username | -p --password <span style="color: #000099; font-weight: bold;">\ </span>-o --host | -d --database]&quot;</span>
    version     <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">&quot;%prog v1.0<span style="color: #000099; font-weight: bold;">\n</span>Distributed under the LGPL2 License&quot;</span>
    description <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">&quot;Increments all sequences in a PostgreSQL database&quot;</span>
    <span style="color: #dc143c;">parser</span> <span style="color: #66cc66;">=</span> OptionParser<span style="color: black;">&#40;</span>usage<span style="color: #66cc66;">=</span>usage<span style="color: #66cc66;">,</span> version<span style="color: #66cc66;">=</span>version<span style="color: #66cc66;">,</span> description<span style="color: #66cc66;">=</span>description<span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">parser</span>.<span style="color: black;">add_option</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;-v&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">&quot;--verbose&quot;</span><span style="color: #66cc66;">,</span> action<span style="color: #66cc66;">=</span><span style="color: #483d8b;">&quot;store_true&quot;</span><span style="color: #66cc66;">,</span> dest<span style="color: #66cc66;">=</span><span style="color: #483d8b;">&quot;verbose&quot;</span><span style="color: #66cc66;">,</span> default<span style="color: #66cc66;">=</span><span style="color: #008000;">False</span><span style="color: #66cc66;">,</span> <span style="color: #008000;">help</span><span style="color: #66cc66;">=</span><span style="color: #483d8b;">&quot;Enable extra output&quot;</span><span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">parser</span>.<span style="color: black;">add_option</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;-o&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">&quot;--host&quot;</span><span style="color: #66cc66;">,</span> action<span style="color: #66cc66;">=</span><span style="color: #483d8b;">&quot;store&quot;</span><span style="color: #66cc66;">,</span> dest<span style="color: #66cc66;">=</span><span style="color: #483d8b;">&quot;host&quot;</span><span style="color: #66cc66;">,</span> default<span style="color: #66cc66;">=</span><span style="color: #483d8b;">&quot;127.0.0.1&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #008000;">help</span><span style="color: #66cc66;">=</span><span style="color: #483d8b;">&quot;Database hostname/IP&quot;</span><span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">parser</span>.<span style="color: black;">add_option</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;-u&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">&quot;--username&quot;</span><span style="color: #66cc66;">,</span> action<span style="color: #66cc66;">=</span><span style="color: #483d8b;">&quot;store&quot;</span><span style="color: #66cc66;">,</span> dest<span style="color: #66cc66;">=</span><span style="color: #483d8b;">&quot;user&quot;</span><span style="color: #66cc66;">,</span> default<span style="color: #66cc66;">=</span><span style="color: #483d8b;">&quot;postgres&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #008000;">help</span><span style="color: #66cc66;">=</span><span style="color: #483d8b;">&quot;Database Username&quot;</span><span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">parser</span>.<span style="color: black;">add_option</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;-p&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">&quot;--password&quot;</span><span style="color: #66cc66;">,</span> action<span style="color: #66cc66;">=</span><span style="color: #483d8b;">&quot;store&quot;</span><span style="color: #66cc66;">,</span> dest<span style="color: #66cc66;">=</span><span style="color: #483d8b;">&quot;passwd&quot;</span><span style="color: #66cc66;">,</span> default<span style="color: #66cc66;">=</span><span style="color: #483d8b;">&quot;&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #008000;">help</span><span style="color: #66cc66;">=</span><span style="color: #483d8b;">&quot;Database Password&quot;</span><span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">parser</span>.<span style="color: black;">add_option</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;-d&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">&quot;--database&quot;</span><span style="color: #66cc66;">,</span> action<span style="color: #66cc66;">=</span><span style="color: #483d8b;">&quot;store&quot;</span><span style="color: #66cc66;">,</span> dest<span style="color: #66cc66;">=</span><span style="color: #483d8b;">&quot;db&quot;</span><span style="color: #66cc66;">,</span> default<span style="color: #66cc66;">=</span><span style="color: #483d8b;">&quot;template1&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #008000;">help</span><span style="color: #66cc66;">=</span><span style="color: #483d8b;">&quot;Database Name&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">try</span>:
        <span style="color: black;">&#40;</span>options<span style="color: #66cc66;">,</span> args<span style="color: black;">&#41;</span> <span style="color: #66cc66;">=</span> <span style="color: #dc143c;">parser</span>.<span style="color: black;">parse_args</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        obj <span style="color: #66cc66;">=</span> PgRepairman<span style="color: black;">&#40;</span>options<span style="color: #66cc66;">,</span> <span style="color: #dc143c;">parser</span><span style="color: black;">&#41;</span>
        obj.<span style="color: black;">fixSequences</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">KeyboardInterrupt</span><span style="color: #66cc66;">,</span> e:
        <span style="color: #dc143c;">sys</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>You can also download the script <a href="http://blog.madpython.com/wp-content/uploads/2009/03/pg_sequences.py" onclick="return TrackClick('http%3A%2F%2Fblog.madpython.com%2Fwp-content%2Fuploads%2F2009%2F03%2Fpg_sequences.py','here')">here</a>.</p>
<p><a class="a2a_button_reddit" href="http://www.addtoany.com/add_to/reddit?linkurl=http%3A%2F%2Fblog.madpython.com%2F2009%2F03%2F06%2Ffixing-custom-sequences-in-postgresql%2F&amp;linkname=Fixing%20custom%20sequences%20in%20PostgreSQL" title="Reddit" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/reddit.png" width="16" height="16" alt="Reddit"/></a><a class="a2a_button_delicious" href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fblog.madpython.com%2F2009%2F03%2F06%2Ffixing-custom-sequences-in-postgresql%2F&amp;linkname=Fixing%20custom%20sequences%20in%20PostgreSQL" title="Delicious" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/delicious.png" width="16" height="16" alt="Delicious"/></a><a class="a2a_button_google_bookmarks" href="http://www.addtoany.com/add_to/google_bookmarks?linkurl=http%3A%2F%2Fblog.madpython.com%2F2009%2F03%2F06%2Ffixing-custom-sequences-in-postgresql%2F&amp;linkname=Fixing%20custom%20sequences%20in%20PostgreSQL" title="Google Bookmarks" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/google.png" width="16" height="16" alt="Google Bookmarks"/></a><a class="a2a_button_facebook" href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fblog.madpython.com%2F2009%2F03%2F06%2Ffixing-custom-sequences-in-postgresql%2F&amp;linkname=Fixing%20custom%20sequences%20in%20PostgreSQL" title="Facebook" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/facebook.png" width="16" height="16" alt="Facebook"/></a><a class="a2a_button_stumbleupon" href="http://www.addtoany.com/add_to/stumbleupon?linkurl=http%3A%2F%2Fblog.madpython.com%2F2009%2F03%2F06%2Ffixing-custom-sequences-in-postgresql%2F&amp;linkname=Fixing%20custom%20sequences%20in%20PostgreSQL" title="StumbleUpon" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/stumbleupon.png" width="16" height="16" alt="StumbleUpon"/></a><a class="a2a_button_linkedin" href="http://www.addtoany.com/add_to/linkedin?linkurl=http%3A%2F%2Fblog.madpython.com%2F2009%2F03%2F06%2Ffixing-custom-sequences-in-postgresql%2F&amp;linkname=Fixing%20custom%20sequences%20in%20PostgreSQL" title="LinkedIn" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/linkedin.png" width="16" height="16" alt="LinkedIn"/></a><a class="a2a_button_twitter" href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fblog.madpython.com%2F2009%2F03%2F06%2Ffixing-custom-sequences-in-postgresql%2F&amp;linkname=Fixing%20custom%20sequences%20in%20PostgreSQL" title="Twitter" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.madpython.com%2F2009%2F03%2F06%2Ffixing-custom-sequences-in-postgresql%2F&amp;title=Fixing%20custom%20sequences%20in%20PostgreSQL" id="wpa2a_18">Share</a></p><div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href='http://blog.madpython.com/2009/03/07/extending-postgresql-with-python/' rel='bookmark' title='Extending PostgreSQL with Python'>Extending PostgreSQL with Python</a></li>
<li><a href='http://blog.madpython.com/2009/03/02/apache2-shortcuts/' rel='bookmark' title='Apache2 shortcuts'>Apache2 shortcuts</a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.madpython.com/2009/03/06/fixing-custom-sequences-in-postgresql/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Apache2 shortcuts</title>
		<link>http://blog.madpython.com/2009/03/02/apache2-shortcuts/</link>
		<comments>http://blog.madpython.com/2009/03/02/apache2-shortcuts/#comments</comments>
		<pubDate>Mon, 02 Mar 2009 15:59:50 +0000</pubDate>
		<dc:creator>Xavier</dc:creator>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[tech]]></category>
		<category><![CDATA[unix]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[sysadmin]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://blog.madpython.com/?p=29</guid>
		<description><![CDATA[I&#8217;ve been maintaining and managing Apache servers for over 10 years now, but for some reason, I never bothered to RTFM when it came to enabling/disabling modules and site configs in apache2.. As it turns out, you don&#8217;t have to &#8230; <a href="http://blog.madpython.com/2009/03/02/apache2-shortcuts/">Continue reading <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href='http://blog.madpython.com/2009/03/07/extending-postgresql-with-python/' rel='bookmark' title='Extending PostgreSQL with Python'>Extending PostgreSQL with Python</a></li>
<li><a href='http://blog.madpython.com/2009/03/06/fixing-custom-sequences-in-postgresql/' rel='bookmark' title='Fixing custom sequences in PostgreSQL'>Fixing custom sequences in PostgreSQL</a></li>
<li><a href='http://blog.madpython.com/2009/03/01/ahh-those-cool-little-cli-tools/' rel='bookmark' title='Ahh those cool little CLI tools&#8230;'>Ahh those cool little CLI tools&#8230;</a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p>I&#8217;ve been maintaining and managing Apache servers for over 10 years now, but for some reason, I never bothered to RTFM when it came to enabling/disabling modules and site configs in apache2.. As it turns out, you don&#8217;t have to manually create symlinks from mods_available to mods_enabled and sites_available to sites_enabled, as Apache2 includes a handful of shortcut scripts to do this work for you&#8230; Doh!</p>
<p>To enable a module in your apache2 config, instead of doing the old</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ln</span> <span style="color: #660033;">-sf</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>apache2<span style="color: #000000; font-weight: bold;">/</span>mods_available<span style="color: #000000; font-weight: bold;">/</span>mod_rewrite.conf <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>apache2<span style="color: #000000; font-weight: bold;">/</span>mods_enabled<span style="color: #000000; font-weight: bold;">/</span>mod_rewrite.conf
<span style="color: #c20cb9; font-weight: bold;">ln</span> <span style="color: #660033;">-sf</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>apache2<span style="color: #000000; font-weight: bold;">/</span>mods_available<span style="color: #000000; font-weight: bold;">/</span>mod_rewrite.load <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>apache2<span style="color: #000000; font-weight: bold;">/</span>mods_enabled<span style="color: #000000; font-weight: bold;">/</span>mod_rewrite.load</pre></td></tr></table></div>

<p>Next time, just do:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;">a2enmod rewrite <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>apache2 restart</pre></td></tr></table></div>

<p>To disable this module, try</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;">a2dismod rewrite <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>apache2 restart</pre></td></tr></table></div>

<p>Similarly, to enable a site config, try</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;">a2ensite myVhost.com <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>apache2 restart</pre></td></tr></table></div>

<p> and to disable it, obviously, try</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;">a2dissite myVhost.com <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>apache2 restart</pre></td></tr></table></div>

<p>Finally, if you&#8217;d like to lint through your Apache2 config files before issuing a restart which might be responsible for some downtime if it doesn&#8217;t work, try</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">apache2ctl configtest</span></pre></td></tr></table></div>

<p><a class="a2a_button_reddit" href="http://www.addtoany.com/add_to/reddit?linkurl=http%3A%2F%2Fblog.madpython.com%2F2009%2F03%2F02%2Fapache2-shortcuts%2F&amp;linkname=Apache2%20shortcuts" title="Reddit" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/reddit.png" width="16" height="16" alt="Reddit"/></a><a class="a2a_button_delicious" href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fblog.madpython.com%2F2009%2F03%2F02%2Fapache2-shortcuts%2F&amp;linkname=Apache2%20shortcuts" title="Delicious" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/delicious.png" width="16" height="16" alt="Delicious"/></a><a class="a2a_button_google_bookmarks" href="http://www.addtoany.com/add_to/google_bookmarks?linkurl=http%3A%2F%2Fblog.madpython.com%2F2009%2F03%2F02%2Fapache2-shortcuts%2F&amp;linkname=Apache2%20shortcuts" title="Google Bookmarks" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/google.png" width="16" height="16" alt="Google Bookmarks"/></a><a class="a2a_button_facebook" href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fblog.madpython.com%2F2009%2F03%2F02%2Fapache2-shortcuts%2F&amp;linkname=Apache2%20shortcuts" title="Facebook" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/facebook.png" width="16" height="16" alt="Facebook"/></a><a class="a2a_button_stumbleupon" href="http://www.addtoany.com/add_to/stumbleupon?linkurl=http%3A%2F%2Fblog.madpython.com%2F2009%2F03%2F02%2Fapache2-shortcuts%2F&amp;linkname=Apache2%20shortcuts" title="StumbleUpon" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/stumbleupon.png" width="16" height="16" alt="StumbleUpon"/></a><a class="a2a_button_linkedin" href="http://www.addtoany.com/add_to/linkedin?linkurl=http%3A%2F%2Fblog.madpython.com%2F2009%2F03%2F02%2Fapache2-shortcuts%2F&amp;linkname=Apache2%20shortcuts" title="LinkedIn" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/linkedin.png" width="16" height="16" alt="LinkedIn"/></a><a class="a2a_button_twitter" href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fblog.madpython.com%2F2009%2F03%2F02%2Fapache2-shortcuts%2F&amp;linkname=Apache2%20shortcuts" title="Twitter" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.madpython.com%2F2009%2F03%2F02%2Fapache2-shortcuts%2F&amp;title=Apache2%20shortcuts" id="wpa2a_20">Share</a></p><div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href='http://blog.madpython.com/2009/03/07/extending-postgresql-with-python/' rel='bookmark' title='Extending PostgreSQL with Python'>Extending PostgreSQL with Python</a></li>
<li><a href='http://blog.madpython.com/2009/03/06/fixing-custom-sequences-in-postgresql/' rel='bookmark' title='Fixing custom sequences in PostgreSQL'>Fixing custom sequences in PostgreSQL</a></li>
<li><a href='http://blog.madpython.com/2009/03/01/ahh-those-cool-little-cli-tools/' rel='bookmark' title='Ahh those cool little CLI tools&#8230;'>Ahh those cool little CLI tools&#8230;</a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.madpython.com/2009/03/02/apache2-shortcuts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
