<?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>Guy Rutenberg &#187; Projects</title>
	<atom:link href="http://www.guyrutenberg.com/category/projects/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.guyrutenberg.com</link>
	<description>Keeping track of what I do</description>
	<lastBuildDate>Sat, 14 Jan 2012 11:30:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Solving Sudoku using Python and Prolog</title>
		<link>http://www.guyrutenberg.com/2011/12/01/solving-sudoku-using-python-and-prolog/</link>
		<comments>http://www.guyrutenberg.com/2011/12/01/solving-sudoku-using-python-and-prolog/#comments</comments>
		<pubDate>Thu, 01 Dec 2011 20:39:22 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[Prolog]]></category>
		<category><![CDATA[Sudoku]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/?p=911</guid>
		<description><![CDATA[Two weeks ago, I add came up with an interesting algorithm for solving Hidato which basically involves decomposing the board the grid (can be square, hexagonal or any other shape), into classes of pieces and then arranging them (maybe I&#8217;ll write a detailed post on it in the future). So while pondering whether it would [...]]]></description>
			<content:encoded><![CDATA[<p>Two weeks ago, I add came up with an interesting algorithm for solving <a href="http://en.wikipedia.org/wiki/Hidato">Hidato</a> which basically involves decomposing the board the grid (can be square, hexagonal or any other shape), into classes of pieces and then arranging them (maybe I&#8217;ll write a detailed post on it in the future). So while pondering whether it would be interesting enough to go forward and actually implementing the algorithm compared to the work it would require, I started thinking what will be the simplest way to solve such puzzles, as opposed to efficient.</p>
<p>At first I&#8217;ve looked at general purpose constraint solvers, and decided to tackle Sudoku instead as it&#8217;s a bit simple to define in terms of constraints. I considered several libraries but in the end I&#8217;ve settled on plainly using Prolog. I chose Prolog because as a logic programming language, constraints are its bread and butter. I although kind of liked it as I haven&#8217;t done anything in Prolog for quite a few years.</p>
<p>Describing Sudoku in terms of constraints is extremely simple. You need to state that every cell is in a given range and that all rows, columns and sub-grid contain different integers. As mangling with lists in prolog isn&#8217;t fun, I&#8217;ve wrote a python program that outputs all the prolog statements with hardcoded references to the variables which build-up the board. It&#8217;s ugly but dead simple. The script gets the dimensions of the sub-grid.<br />
<span id="more-911"></span></p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
&nbsp;
VAR_TEMPLATE = <span style="color: #483d8b;">&quot;G%02d%02d&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> puzzle_declaration<span style="color: black;">&#40;</span>x,y<span style="color: black;">&#41;</span>:
    lines = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</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>x<span style="color: #66cc66;">*</span>y<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">vars</span> = <span style="color: #008000;">map</span><span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">lambda</span> j: VAR_TEMPLATE <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>i,j<span style="color: black;">&#41;</span>, <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>x<span style="color: #66cc66;">*</span>y<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        lines.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'['</span> + <span style="color: #483d8b;">', '</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: #008000;">vars</span><span style="color: black;">&#41;</span>+ <span style="color: #483d8b;">']'</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;puzzle(&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'  '</span> + <span style="color: #483d8b;">',<span style="color: #000099; font-weight: bold;">\n</span>  '</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>lines<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;):-&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> constraints_range<span style="color: black;">&#40;</span>x,y<span style="color: black;">&#41;</span>:
    <span style="color: #008000;">vars</span> = <span style="color: black;">&#91;</span>VAR_TEMPLATE <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>i,j<span style="color: black;">&#41;</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>x<span style="color: #66cc66;">*</span>y<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> j <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>x<span style="color: #66cc66;">*</span>y<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>Vars = [&quot;</span> + <span style="color: #483d8b;">', '</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: #008000;">vars</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">&quot;],&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>Vars ins 1..%d,&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>x<span style="color: #66cc66;">*</span>y<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> constraints_diff<span style="color: black;">&#40;</span>x,y<span style="color: black;">&#41;</span>:
    <span style="color: #808080; font-style: italic;"># rows</span>
    rows = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</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>x<span style="color: #66cc66;">*</span>y<span style="color: black;">&#41;</span>:
        row = <span style="color: black;">&#91;</span>VAR_TEMPLATE <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>i,j<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> j <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>x<span style="color: #66cc66;">*</span>y<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
        rows.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;all_different([&quot;</span> + <span style="color: #483d8b;">', '</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>row<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">&quot;])&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
    columns = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> j <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>x<span style="color: #66cc66;">*</span>y<span style="color: black;">&#41;</span>:
        column = <span style="color: black;">&#91;</span>VAR_TEMPLATE <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>i,j<span style="color: black;">&#41;</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>x<span style="color: #66cc66;">*</span>y<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
        columns.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;all_different([&quot;</span> + <span style="color: #483d8b;">', '</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>column<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">&quot;])&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
    rects = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</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>x<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">for</span> j <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>y<span style="color: black;">&#41;</span>:
            rect = <span style="color: black;">&#91;</span>VAR_TEMPLATE <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>k+y<span style="color: #66cc66;">*</span>i,l+x<span style="color: #66cc66;">*</span>j<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> k <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>y<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> l <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
            rects.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;all_different([&quot;</span> + <span style="color: #483d8b;">', '</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>rect<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">&quot;])&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\t</span>'</span> + <span style="color: #483d8b;">',<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\t</span>'</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>rows<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">&quot;,&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\t</span>'</span> + <span style="color: #483d8b;">',<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\t</span>'</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>columns<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">&quot;,&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\t</span>'</span> + <span style="color: #483d8b;">',<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\t</span>'</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>rects<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">&quot;.&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> generate_puzzle<span style="color: black;">&#40;</span>x,y<span style="color: black;">&#41;</span>:
    puzzle_declaration<span style="color: black;">&#40;</span>x,y<span style="color: black;">&#41;</span>
    constraints_range<span style="color: black;">&#40;</span>x,y<span style="color: black;">&#41;</span>
    constraints_diff<span style="color: black;">&#40;</span>x,y<span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__==<span style="color: #483d8b;">&quot;__main__&quot;</span>:
    x = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
    y = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
    generate_puzzle<span style="color: black;">&#40;</span>x,y<span style="color: black;">&#41;</span></pre></div></div>

<p>For example running the script with <code>3 3</code> as arguments generates a regular 9&#215;9 Sudoku solver.</p>

<div class="wp_syntax"><div class="code"><pre class="prolog" style="font-family:monospace;"><span style="color: #339933;">:-</span> use_module<span style="color: #009900;">&#40;</span>library<span style="color: #009900;">&#40;</span>clpfd<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>
&nbsp;
puzzle<span style="color: #009900;">&#40;</span>
  <span style="color: #009900;">&#91;</span>G0000<span style="color: #339933;">,</span> G0001<span style="color: #339933;">,</span> G0002<span style="color: #339933;">,</span> G0003<span style="color: #339933;">,</span> G0004<span style="color: #339933;">,</span> G0005<span style="color: #339933;">,</span> G0006<span style="color: #339933;">,</span> G0007<span style="color: #339933;">,</span> G0008<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
  <span style="color: #009900;">&#91;</span>G0100<span style="color: #339933;">,</span> G0101<span style="color: #339933;">,</span> G0102<span style="color: #339933;">,</span> G0103<span style="color: #339933;">,</span> G0104<span style="color: #339933;">,</span> G0105<span style="color: #339933;">,</span> G0106<span style="color: #339933;">,</span> G0107<span style="color: #339933;">,</span> G0108<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
  <span style="color: #009900;">&#91;</span>G0200<span style="color: #339933;">,</span> G0201<span style="color: #339933;">,</span> G0202<span style="color: #339933;">,</span> G0203<span style="color: #339933;">,</span> G0204<span style="color: #339933;">,</span> G0205<span style="color: #339933;">,</span> G0206<span style="color: #339933;">,</span> G0207<span style="color: #339933;">,</span> G0208<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
  <span style="color: #009900;">&#91;</span>G0300<span style="color: #339933;">,</span> G0301<span style="color: #339933;">,</span> G0302<span style="color: #339933;">,</span> G0303<span style="color: #339933;">,</span> G0304<span style="color: #339933;">,</span> G0305<span style="color: #339933;">,</span> G0306<span style="color: #339933;">,</span> G0307<span style="color: #339933;">,</span> G0308<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
  <span style="color: #009900;">&#91;</span>G0400<span style="color: #339933;">,</span> G0401<span style="color: #339933;">,</span> G0402<span style="color: #339933;">,</span> G0403<span style="color: #339933;">,</span> G0404<span style="color: #339933;">,</span> G0405<span style="color: #339933;">,</span> G0406<span style="color: #339933;">,</span> G0407<span style="color: #339933;">,</span> G0408<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
  <span style="color: #009900;">&#91;</span>G0500<span style="color: #339933;">,</span> G0501<span style="color: #339933;">,</span> G0502<span style="color: #339933;">,</span> G0503<span style="color: #339933;">,</span> G0504<span style="color: #339933;">,</span> G0505<span style="color: #339933;">,</span> G0506<span style="color: #339933;">,</span> G0507<span style="color: #339933;">,</span> G0508<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
  <span style="color: #009900;">&#91;</span>G0600<span style="color: #339933;">,</span> G0601<span style="color: #339933;">,</span> G0602<span style="color: #339933;">,</span> G0603<span style="color: #339933;">,</span> G0604<span style="color: #339933;">,</span> G0605<span style="color: #339933;">,</span> G0606<span style="color: #339933;">,</span> G0607<span style="color: #339933;">,</span> G0608<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
  <span style="color: #009900;">&#91;</span>G0700<span style="color: #339933;">,</span> G0701<span style="color: #339933;">,</span> G0702<span style="color: #339933;">,</span> G0703<span style="color: #339933;">,</span> G0704<span style="color: #339933;">,</span> G0705<span style="color: #339933;">,</span> G0706<span style="color: #339933;">,</span> G0707<span style="color: #339933;">,</span> G0708<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
  <span style="color: #009900;">&#91;</span>G0800<span style="color: #339933;">,</span> G0801<span style="color: #339933;">,</span> G0802<span style="color: #339933;">,</span> G0803<span style="color: #339933;">,</span> G0804<span style="color: #339933;">,</span> G0805<span style="color: #339933;">,</span> G0806<span style="color: #339933;">,</span> G0807<span style="color: #339933;">,</span> G0808<span style="color: #009900;">&#93;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">:-</span>
	Vars <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>G0000<span style="color: #339933;">,</span> G0001<span style="color: #339933;">,</span> G0002<span style="color: #339933;">,</span> G0003<span style="color: #339933;">,</span> G0004<span style="color: #339933;">,</span> G0005<span style="color: #339933;">,</span> G0006<span style="color: #339933;">,</span> G0007<span style="color: #339933;">,</span> G0008<span style="color: #339933;">,</span> G0100<span style="color: #339933;">,</span> G0101<span style="color: #339933;">,</span> G0102<span style="color: #339933;">,</span> G0103<span style="color: #339933;">,</span> G0104<span style="color: #339933;">,</span> G0105<span style="color: #339933;">,</span> G0106<span style="color: #339933;">,</span> G0107<span style="color: #339933;">,</span> G0108<span style="color: #339933;">,</span> G0200<span style="color: #339933;">,</span> G0201<span style="color: #339933;">,</span> G0202<span style="color: #339933;">,</span> G0203<span style="color: #339933;">,</span> G0204<span style="color: #339933;">,</span> G0205<span style="color: #339933;">,</span> G0206<span style="color: #339933;">,</span> G0207<span style="color: #339933;">,</span> G0208<span style="color: #339933;">,</span> G0300<span style="color: #339933;">,</span> G0301<span style="color: #339933;">,</span> G0302<span style="color: #339933;">,</span> G0303<span style="color: #339933;">,</span> G0304<span style="color: #339933;">,</span> G0305<span style="color: #339933;">,</span> G0306<span style="color: #339933;">,</span> G0307<span style="color: #339933;">,</span> G0308<span style="color: #339933;">,</span> G0400<span style="color: #339933;">,</span> G0401<span style="color: #339933;">,</span> G0402<span style="color: #339933;">,</span> G0403<span style="color: #339933;">,</span> G0404<span style="color: #339933;">,</span> G0405<span style="color: #339933;">,</span> G0406<span style="color: #339933;">,</span> G0407<span style="color: #339933;">,</span> G0408<span style="color: #339933;">,</span> G0500<span style="color: #339933;">,</span> G0501<span style="color: #339933;">,</span> G0502<span style="color: #339933;">,</span> G0503<span style="color: #339933;">,</span> G0504<span style="color: #339933;">,</span> G0505<span style="color: #339933;">,</span> G0506<span style="color: #339933;">,</span> G0507<span style="color: #339933;">,</span> G0508<span style="color: #339933;">,</span> G0600<span style="color: #339933;">,</span> G0601<span style="color: #339933;">,</span> G0602<span style="color: #339933;">,</span> G0603<span style="color: #339933;">,</span> G0604<span style="color: #339933;">,</span> G0605<span style="color: #339933;">,</span> G0606<span style="color: #339933;">,</span> G0607<span style="color: #339933;">,</span> G0608<span style="color: #339933;">,</span> G0700<span style="color: #339933;">,</span> G0701<span style="color: #339933;">,</span> G0702<span style="color: #339933;">,</span> G0703<span style="color: #339933;">,</span> G0704<span style="color: #339933;">,</span> G0705<span style="color: #339933;">,</span> G0706<span style="color: #339933;">,</span> G0707<span style="color: #339933;">,</span> G0708<span style="color: #339933;">,</span> G0800<span style="color: #339933;">,</span> G0801<span style="color: #339933;">,</span> G0802<span style="color: #339933;">,</span> G0803<span style="color: #339933;">,</span> G0804<span style="color: #339933;">,</span> G0805<span style="color: #339933;">,</span> G0806<span style="color: #339933;">,</span> G0807<span style="color: #339933;">,</span> G0808<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
	Vars ins <span style="color: #800080;">1</span><span style="color: #339933;">..</span><span style="color: #800080;">9</span><span style="color: #339933;">,</span>
	all_different<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>G0000<span style="color: #339933;">,</span> G0001<span style="color: #339933;">,</span> G0002<span style="color: #339933;">,</span> G0003<span style="color: #339933;">,</span> G0004<span style="color: #339933;">,</span> G0005<span style="color: #339933;">,</span> G0006<span style="color: #339933;">,</span> G0007<span style="color: #339933;">,</span> G0008<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	all_different<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>G0100<span style="color: #339933;">,</span> G0101<span style="color: #339933;">,</span> G0102<span style="color: #339933;">,</span> G0103<span style="color: #339933;">,</span> G0104<span style="color: #339933;">,</span> G0105<span style="color: #339933;">,</span> G0106<span style="color: #339933;">,</span> G0107<span style="color: #339933;">,</span> G0108<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	all_different<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>G0200<span style="color: #339933;">,</span> G0201<span style="color: #339933;">,</span> G0202<span style="color: #339933;">,</span> G0203<span style="color: #339933;">,</span> G0204<span style="color: #339933;">,</span> G0205<span style="color: #339933;">,</span> G0206<span style="color: #339933;">,</span> G0207<span style="color: #339933;">,</span> G0208<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	all_different<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>G0300<span style="color: #339933;">,</span> G0301<span style="color: #339933;">,</span> G0302<span style="color: #339933;">,</span> G0303<span style="color: #339933;">,</span> G0304<span style="color: #339933;">,</span> G0305<span style="color: #339933;">,</span> G0306<span style="color: #339933;">,</span> G0307<span style="color: #339933;">,</span> G0308<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	all_different<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>G0400<span style="color: #339933;">,</span> G0401<span style="color: #339933;">,</span> G0402<span style="color: #339933;">,</span> G0403<span style="color: #339933;">,</span> G0404<span style="color: #339933;">,</span> G0405<span style="color: #339933;">,</span> G0406<span style="color: #339933;">,</span> G0407<span style="color: #339933;">,</span> G0408<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	all_different<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>G0500<span style="color: #339933;">,</span> G0501<span style="color: #339933;">,</span> G0502<span style="color: #339933;">,</span> G0503<span style="color: #339933;">,</span> G0504<span style="color: #339933;">,</span> G0505<span style="color: #339933;">,</span> G0506<span style="color: #339933;">,</span> G0507<span style="color: #339933;">,</span> G0508<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	all_different<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>G0600<span style="color: #339933;">,</span> G0601<span style="color: #339933;">,</span> G0602<span style="color: #339933;">,</span> G0603<span style="color: #339933;">,</span> G0604<span style="color: #339933;">,</span> G0605<span style="color: #339933;">,</span> G0606<span style="color: #339933;">,</span> G0607<span style="color: #339933;">,</span> G0608<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	all_different<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>G0700<span style="color: #339933;">,</span> G0701<span style="color: #339933;">,</span> G0702<span style="color: #339933;">,</span> G0703<span style="color: #339933;">,</span> G0704<span style="color: #339933;">,</span> G0705<span style="color: #339933;">,</span> G0706<span style="color: #339933;">,</span> G0707<span style="color: #339933;">,</span> G0708<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	all_different<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>G0800<span style="color: #339933;">,</span> G0801<span style="color: #339933;">,</span> G0802<span style="color: #339933;">,</span> G0803<span style="color: #339933;">,</span> G0804<span style="color: #339933;">,</span> G0805<span style="color: #339933;">,</span> G0806<span style="color: #339933;">,</span> G0807<span style="color: #339933;">,</span> G0808<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	all_different<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>G0000<span style="color: #339933;">,</span> G0100<span style="color: #339933;">,</span> G0200<span style="color: #339933;">,</span> G0300<span style="color: #339933;">,</span> G0400<span style="color: #339933;">,</span> G0500<span style="color: #339933;">,</span> G0600<span style="color: #339933;">,</span> G0700<span style="color: #339933;">,</span> G0800<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	all_different<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>G0001<span style="color: #339933;">,</span> G0101<span style="color: #339933;">,</span> G0201<span style="color: #339933;">,</span> G0301<span style="color: #339933;">,</span> G0401<span style="color: #339933;">,</span> G0501<span style="color: #339933;">,</span> G0601<span style="color: #339933;">,</span> G0701<span style="color: #339933;">,</span> G0801<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	all_different<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>G0002<span style="color: #339933;">,</span> G0102<span style="color: #339933;">,</span> G0202<span style="color: #339933;">,</span> G0302<span style="color: #339933;">,</span> G0402<span style="color: #339933;">,</span> G0502<span style="color: #339933;">,</span> G0602<span style="color: #339933;">,</span> G0702<span style="color: #339933;">,</span> G0802<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	all_different<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>G0003<span style="color: #339933;">,</span> G0103<span style="color: #339933;">,</span> G0203<span style="color: #339933;">,</span> G0303<span style="color: #339933;">,</span> G0403<span style="color: #339933;">,</span> G0503<span style="color: #339933;">,</span> G0603<span style="color: #339933;">,</span> G0703<span style="color: #339933;">,</span> G0803<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	all_different<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>G0004<span style="color: #339933;">,</span> G0104<span style="color: #339933;">,</span> G0204<span style="color: #339933;">,</span> G0304<span style="color: #339933;">,</span> G0404<span style="color: #339933;">,</span> G0504<span style="color: #339933;">,</span> G0604<span style="color: #339933;">,</span> G0704<span style="color: #339933;">,</span> G0804<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	all_different<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>G0005<span style="color: #339933;">,</span> G0105<span style="color: #339933;">,</span> G0205<span style="color: #339933;">,</span> G0305<span style="color: #339933;">,</span> G0405<span style="color: #339933;">,</span> G0505<span style="color: #339933;">,</span> G0605<span style="color: #339933;">,</span> G0705<span style="color: #339933;">,</span> G0805<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	all_different<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>G0006<span style="color: #339933;">,</span> G0106<span style="color: #339933;">,</span> G0206<span style="color: #339933;">,</span> G0306<span style="color: #339933;">,</span> G0406<span style="color: #339933;">,</span> G0506<span style="color: #339933;">,</span> G0606<span style="color: #339933;">,</span> G0706<span style="color: #339933;">,</span> G0806<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	all_different<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>G0007<span style="color: #339933;">,</span> G0107<span style="color: #339933;">,</span> G0207<span style="color: #339933;">,</span> G0307<span style="color: #339933;">,</span> G0407<span style="color: #339933;">,</span> G0507<span style="color: #339933;">,</span> G0607<span style="color: #339933;">,</span> G0707<span style="color: #339933;">,</span> G0807<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	all_different<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>G0008<span style="color: #339933;">,</span> G0108<span style="color: #339933;">,</span> G0208<span style="color: #339933;">,</span> G0308<span style="color: #339933;">,</span> G0408<span style="color: #339933;">,</span> G0508<span style="color: #339933;">,</span> G0608<span style="color: #339933;">,</span> G0708<span style="color: #339933;">,</span> G0808<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	all_different<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>G0000<span style="color: #339933;">,</span> G0001<span style="color: #339933;">,</span> G0002<span style="color: #339933;">,</span> G0100<span style="color: #339933;">,</span> G0101<span style="color: #339933;">,</span> G0102<span style="color: #339933;">,</span> G0200<span style="color: #339933;">,</span> G0201<span style="color: #339933;">,</span> G0202<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	all_different<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>G0003<span style="color: #339933;">,</span> G0004<span style="color: #339933;">,</span> G0005<span style="color: #339933;">,</span> G0103<span style="color: #339933;">,</span> G0104<span style="color: #339933;">,</span> G0105<span style="color: #339933;">,</span> G0203<span style="color: #339933;">,</span> G0204<span style="color: #339933;">,</span> G0205<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	all_different<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>G0006<span style="color: #339933;">,</span> G0007<span style="color: #339933;">,</span> G0008<span style="color: #339933;">,</span> G0106<span style="color: #339933;">,</span> G0107<span style="color: #339933;">,</span> G0108<span style="color: #339933;">,</span> G0206<span style="color: #339933;">,</span> G0207<span style="color: #339933;">,</span> G0208<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	all_different<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>G0300<span style="color: #339933;">,</span> G0301<span style="color: #339933;">,</span> G0302<span style="color: #339933;">,</span> G0400<span style="color: #339933;">,</span> G0401<span style="color: #339933;">,</span> G0402<span style="color: #339933;">,</span> G0500<span style="color: #339933;">,</span> G0501<span style="color: #339933;">,</span> G0502<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	all_different<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>G0303<span style="color: #339933;">,</span> G0304<span style="color: #339933;">,</span> G0305<span style="color: #339933;">,</span> G0403<span style="color: #339933;">,</span> G0404<span style="color: #339933;">,</span> G0405<span style="color: #339933;">,</span> G0503<span style="color: #339933;">,</span> G0504<span style="color: #339933;">,</span> G0505<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	all_different<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>G0306<span style="color: #339933;">,</span> G0307<span style="color: #339933;">,</span> G0308<span style="color: #339933;">,</span> G0406<span style="color: #339933;">,</span> G0407<span style="color: #339933;">,</span> G0408<span style="color: #339933;">,</span> G0506<span style="color: #339933;">,</span> G0507<span style="color: #339933;">,</span> G0508<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	all_different<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>G0600<span style="color: #339933;">,</span> G0601<span style="color: #339933;">,</span> G0602<span style="color: #339933;">,</span> G0700<span style="color: #339933;">,</span> G0701<span style="color: #339933;">,</span> G0702<span style="color: #339933;">,</span> G0800<span style="color: #339933;">,</span> G0801<span style="color: #339933;">,</span> G0802<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	all_different<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>G0603<span style="color: #339933;">,</span> G0604<span style="color: #339933;">,</span> G0605<span style="color: #339933;">,</span> G0703<span style="color: #339933;">,</span> G0704<span style="color: #339933;">,</span> G0705<span style="color: #339933;">,</span> G0803<span style="color: #339933;">,</span> G0804<span style="color: #339933;">,</span> G0805<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	all_different<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>G0606<span style="color: #339933;">,</span> G0607<span style="color: #339933;">,</span> G0608<span style="color: #339933;">,</span> G0706<span style="color: #339933;">,</span> G0707<span style="color: #339933;">,</span> G0708<span style="color: #339933;">,</span> G0806<span style="color: #339933;">,</span> G0807<span style="color: #339933;">,</span> G0808<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span></pre></div></div>

<p>Again, not so pretty, but it&#8217;s python-generated hence simple as hell. Now solving is easily by passing integers instead of the variables. You can see below a prolog solution that demos solving a Sudoku. Another nice thing, is that if there isn&#8217;t a unique solution, it will be output say which cells have definite value, and output the constraints that hold for the rest of the cells.</p>
<p>I&#8217;ve tested it with both regular size Sudokus and mini-size (6&#215;6), and it seems to preform pretty fast.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">?- puzzle(
  [G0000, G0001,     1,     3, G0004, G0005, G0006, G0007,     8],
  [G0100, G0101,     7,     5,     6, G0105, G0106,     4, G0108],
  [G0200,     3, G0202, G0203,     7,     8,     6, G0207, G0208],
  [G0300, G0301,     4,     2, G0304, G0305, G0306, G0307,     6],
  [G0400,     9, G0402, G0403, G0404, G0405, G0406,     5, G0408],
  [    1, G0501, G0502, G0503, G0504,     5,     9, G0507, G0508],
  [G0600, G0601, G0602,     1,     2, G0605, G0606,     6, G0608],
  [G0700,     1, G0702, G0703,     3,     7,     5, G0707, G0708],
  [G0800, G0801, G0802, G0803, G0804,     9,     3, G0807, G0808]
).
&nbsp;
G0000 = 5,
G0001 = 6,
G0004 = 4,
G0005 = 2,
G0006 = 7,
G0007 = G0100, G0100 = 9,
G0101 = 8,
G0105 = 1,
G0106 = 2,
G0108 = 3,
G0200 = 4,
G0202 = 2,
G0203 = 9,
G0207 = 1,
G0208 = 5,
G0300 = 7,
G0301 = 5,
G0304 = 9,
G0305 = 3,
G0306 = 1,
G0307 = G0400, G0400 = 8,
G0402 = 3,
G0403 = 7,
G0404 = 1,
G0405 = 6,
G0406 = 4,
G0408 = G0501, G0501 = 2,
G0502 = 6,
G0503 = 4,
G0504 = 8,
G0507 = 3,
G0508 = 7,
G0600 = 3,
G0601 = 7,
G0602 = 5,
G0605 = 4,
G0606 = 8,
G0608 = 9,
G0700 = 6,
G0702 = 9,
G0703 = 8,
G0707 = 2,
G0708 = 4,
G0800 = 2,
G0801 = 4,
G0802 = 8,
G0803 = 6,
G0804 = 5,
G0807 = 7,
G0808 = 1.</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2011/12/01/solving-sudoku-using-python-and-prolog/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>passha &#8211; A hashapass variant</title>
		<link>http://www.guyrutenberg.com/2011/09/17/passha-a-hashapass-variant/</link>
		<comments>http://www.guyrutenberg.com/2011/09/17/passha-a-hashapass-variant/#comments</comments>
		<pubDate>Sat, 17 Sep 2011 14:06:42 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[Projects]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/?p=880</guid>
		<description><![CDATA[I like the idea of hashapass, but I&#8217;m unwilling to use an online tool, as I fear that someday it might be compromised. So I wrote down my own variant of hashapass. It uses slightly longer passwords, and sha256 as the hash function. #! /usr/bin/python &#160; &#34;&#34;&#34; passha.py - Generate passwords from a master password [...]]]></description>
			<content:encoded><![CDATA[<p>I like the idea of <a href="http://hashapass.com">hashapass</a>, but I&#8217;m unwilling to use an online tool, as I fear that someday it might be compromised. So I wrote down my own variant of hashapass. It uses slightly longer passwords, and sha256 as the hash function.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#! /usr/bin/python</span>
&nbsp;
<span style="color: #483d8b;">&quot;&quot;&quot;
passha.py - Generate passwords from a master password and a parameter.
&nbsp;
Based on hashapass (http://hashapass.com)
&quot;&quot;&quot;</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">hmac</span>
<span style="color: #ff7700;font-weight:bold;">import</span> hashlib
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> main<span style="color: black;">&#40;</span>passwd, param<span style="color: black;">&#41;</span>:
    hm = <span style="color: #dc143c;">hmac</span>.<span style="color: black;">HMAC</span><span style="color: black;">&#40;</span>passwd, param, hashlib.<span style="color: black;">sha256</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> hm.<span style="color: black;">digest</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">encode</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;base64&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span>:<span style="color: #ff4500;">10</span><span style="color: black;">&#93;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__==<span style="color: #483d8b;">&quot;__main__&quot;</span>:
    <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">getpass</span>
    passwd = <span style="color: #dc143c;">getpass</span>.<span style="color: #dc143c;">getpass</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    param = <span style="color: #008000;">raw_input</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Parameter: &quot;</span><span style="color: black;">&#41;</span>
    main<span style="color: black;">&#40;</span>passwd, param<span style="color: black;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2011/09/17/passha-a-hashapass-variant/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>CSS Compactor &#8211; Reduces CSS File Size</title>
		<link>http://www.guyrutenberg.com/2011/01/16/css-compactor-reduces-css-file-size/</link>
		<comments>http://www.guyrutenberg.com/2011/01/16/css-compactor-reduces-css-file-size/#comments</comments>
		<pubDate>Sun, 16 Jan 2011 19:57:09 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[CSS]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/?p=809</guid>
		<description><![CDATA[This is a script I wrote back 2006 that reduces the file size for CSS files by removing unnecessary whitespace and comments. It&#8217;s also capable of taking such compacted CSS file, and re-indent it to make it readable. For example it would take the following CSS: /* sample css */ * &#123; margin: 0px; padding: [...]]]></description>
			<content:encoded><![CDATA[<p>This is a script I wrote back 2006 that reduces the file size for CSS files by removing unnecessary whitespace and comments. It&#8217;s also capable of taking such compacted CSS file, and re-indent it to make it readable. For example it would take the following CSS:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/* sample css */</span>
<span style="color: #00AA00;">*</span> <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* define style for the logo */</span>
<span style="color: #cc00cc;">#header</span> <span style="color: #6666ff;">.logo</span> <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span>
    <span style="color: #808080; font-style: italic;">/* another comment */</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>and turn it into:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #00AA00;">*</span><span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span><span style="color: #933;">0px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span><span style="color: #933;">0px</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span><span style="color: #cc00cc;">#header</span> <span style="color: #6666ff;">.logo</span><span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span><span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>which is an equivalent but much shorter CSS code. It can also reindent it back to:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #00AA00;">*</span><span style="color: #00AA00;">&#123;</span>
	 <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span><span style="color: #00AA00;">;</span>
	 <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #cc00cc;">#header</span> .logo<span style="color: #00AA00;">&#123;</span>
	 <span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p><span id="more-809"></span><br />
As I said the script is very old, and was written around the time I learned how to use python, so it is far from perfect. But despite that, it does what it was written for and it&#8217;s useful, so I deemed it worth to release it. You can download it from here: <a href="/wp-content/uploads/2011/01/csscompactor-0.4.tar.gz">csscompactor-0.4.tar.gz</a>.</p>
<p>The code is released under the GPL, so feel free to modify it. If there will be enough interest in it, I&#8217;ll probably re-write so it would have prettier and more efficient code.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2011/01/16/css-compactor-reduces-css-file-size/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Building CookieJar out of Firefox&#8217;s cookies.sqlite</title>
		<link>http://www.guyrutenberg.com/2010/11/27/building-cookiejar-out-of-firefoxs-cookies-sqlite/</link>
		<comments>http://www.guyrutenberg.com/2010/11/27/building-cookiejar-out-of-firefoxs-cookies-sqlite/#comments</comments>
		<pubDate>Sat, 27 Nov 2010 15:19:55 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[Firefox]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/?p=788</guid>
		<description><![CDATA[Firefox 3 started to store it&#8217;s cookies in a SQLite database instead of the old plain-text cookie.txt. While Python&#8217;s cookielib module could read the old cookie.txt file, it doesn&#8217;t handle the new format. The following python snippet takes a CookieJar object and the path to Firefox cookies.sqlite (or a copy of it) and fills the [...]]]></description>
			<content:encoded><![CDATA[<p>Firefox 3 started to store it&#8217;s cookies in a SQLite database instead of the old plain-text <code>cookie.txt</code>. While Python&#8217;s cookielib module could read the old cookie.txt file, it doesn&#8217;t handle the new format. The following python snippet takes a <code>CookieJar</code> object and the path to Firefox <code>cookies.sqlite</code> (or a copy of it) and fills the <code>CookieJar</code> with the cookies from <code>cookies.sqlite</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> sqlite3
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">cookielib</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> get_cookies<span style="color: black;">&#40;</span>cj, ff_cookies<span style="color: black;">&#41;</span>:
    con = sqlite3.<span style="color: black;">connect</span><span style="color: black;">&#40;</span>ff_cookies<span style="color: black;">&#41;</span>
    cur = con.<span style="color: black;">cursor</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    cur.<span style="color: black;">execute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;SELECT host, path, isSecure, expiry, name, value FROM moz_cookies&quot;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> item <span style="color: #ff7700;font-weight:bold;">in</span> cur.<span style="color: black;">fetchall</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
        c = <span style="color: #dc143c;">cookielib</span>.<span style="color: #dc143c;">Cookie</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>, item<span style="color: black;">&#91;</span><span style="color: #ff4500;">4</span><span style="color: black;">&#93;</span>, item<span style="color: black;">&#91;</span><span style="color: #ff4500;">5</span><span style="color: black;">&#93;</span>,
            <span style="color: #008000;">None</span>, <span style="color: #008000;">False</span>,
            item<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>, item<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>.<span style="color: black;">startswith</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'.'</span><span style="color: black;">&#41;</span>, item<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>.<span style="color: black;">startswith</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'.'</span><span style="color: black;">&#41;</span>,
            item<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>, <span style="color: #008000;">False</span>,
            item<span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span>,
            item<span style="color: black;">&#91;</span><span style="color: #ff4500;">3</span><span style="color: black;">&#93;</span>, item<span style="color: black;">&#91;</span><span style="color: #ff4500;">3</span><span style="color: black;">&#93;</span>==<span style="color: #483d8b;">&quot;&quot;</span>,
            <span style="color: #008000;">None</span>, <span style="color: #008000;">None</span>, <span style="color: black;">&#123;</span><span style="color: black;">&#125;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">print</span> c
        cj.<span style="color: black;">set_cookie</span><span style="color: black;">&#40;</span>c<span style="color: black;">&#41;</span></pre></div></div>

<p>It works well for me, except that apperantly Firefox doesn&#8217;t save session cookies to the disk at all.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2010/11/27/building-cookiejar-out-of-firefoxs-cookies-sqlite/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>spass-2.0 &#8211; Secure Password Generator</title>
		<link>http://www.guyrutenberg.com/2010/08/21/spass-2-0-secure-password-generator/</link>
		<comments>http://www.guyrutenberg.com/2010/08/21/spass-2-0-secure-password-generator/#comments</comments>
		<pubDate>Sat, 21 Aug 2010 11:44:14 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[spass]]></category>
		<category><![CDATA[c/c++]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/?p=717</guid>
		<description><![CDATA[This is a complete rewrite of my secure password generator. The new version uses my a true random number generator (and here). The major change was using the new true random number generator in order to ensure strong passwords. Less significant changes include an easy way to specify password&#8217;s strips, and some calling convention changes. [...]]]></description>
			<content:encoded><![CDATA[<p>This is a complete rewrite of my <a href="/2008/05/04/spass-11-secure-password-generator/">secure password generator</a>. The new version uses my a <a href="/2010/08/13/statistical-tests-for-my-audio-based-random-number-generator/">true random number generator</a> (and <a href="/2010/05/14/audio-based-true-random-number-generator-poc/">here</a>).</p>
<p>The major change was using the new true random number generator in order to ensure strong passwords. Less significant changes include an easy way to specify password&#8217;s strips, and some calling convention changes.</p>
<p>Usage examples:</p>

<div class="wp_syntax"><div class="code"><pre class="txt" style="font-family:monospace;">$ ./spass
E5pT35Fg
$ ./spass -l 14
R$tfOm4g_yRQ2J
$ ./spass -s 0-9a-f -l 32
8b5f14a1eeaabe58c2878ab5416a9ebb</pre></div></div>

<p>Download the tarball <a href="/wp-content/uploads/2010/08/spass-2.0.tar.bz2"><code>spass-2.0.tar.bz2</code></a>. The program depends on <a href="http://www.boost.org/">Boost</a>&#8216;s program_options (it was tested against version 1.37 and 1.42 and should work with other versions too).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2010/08/21/spass-2-0-secure-password-generator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Statistical Tests for My Audio Based Random Number Generator</title>
		<link>http://www.guyrutenberg.com/2010/08/13/statistical-tests-for-my-audio-based-random-number-generator/</link>
		<comments>http://www.guyrutenberg.com/2010/08/13/statistical-tests-for-my-audio-based-random-number-generator/#comments</comments>
		<pubDate>Thu, 12 Aug 2010 21:37:25 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[c/c++]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/?p=699</guid>
		<description><![CDATA[In May I&#8217;ve written about a way to generate random number from audio noise. Basically it went like this: Get audio sample from the microphone. Push the least significant bit to a buffer. Repeat steps 1-2 until the buffer is full (buffer size == block size for the hash function). Apply the hash function on [...]]]></description>
			<content:encoded><![CDATA[<p>In May I&#8217;ve written about a <a href="/2010/05/14/audio-based-true-random-number-generator-poc/"> way to generate random number from audio noise</a>. Basically it went like this:</p>
<ol>
<li> Get audio sample from the microphone.</li>
<li>Push the least significant bit to a buffer.</li>
<li>Repeat steps 1-2 until the buffer is full (buffer size == block size for the hash function).</li>
<li>Apply the hash function on the buffer.</li>
<li>Get random bits from the digest.</li>
</ol>
<p>In order to continue developing this random number generator (RNG), I&#8217;ve written a C++ class that simplifies working with it.<br />
<span id="more-699"></span></p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #ff0000; font-style: italic;">/*
 * Copyright (C) 2010  Guy Rutenberg
 * http://www.guyrutenberg.com
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see &lt;http://www.gnu.org/licenses/&gt;.
 */</span>
&nbsp;
<span style="color: #339900;">#include &lt;cstdio&gt;</span>
<span style="color: #339900;">#include &quot;md5.h&quot;</span>
&nbsp;
<span style="color: #0000ff;">class</span> Grandom <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
		Grandom<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">virtual</span> ~Grandom<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #ff0000; font-style: italic;">/**
		 * Generate a random dword
		 */</span>
		<span style="color: #0000ff;">uint32_t</span> operator<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">private</span><span style="color: #008080;">:</span>
		<span style="color: #0000ff;">void</span> gather_entropy<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">void</span> get_block<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
		<span style="color: #0000ff;">FILE</span><span style="color: #000040;">*</span> m_dsp_fd<span style="color: #008080;">;</span>
		<span style="color: #0000ff;">uint32_t</span> m_index<span style="color: #008080;">;</span>
		<span style="color: #0000ff;">union</span> <span style="color: #008000;">&#123;</span>
			<span style="color: #0000ff;">char</span> digest<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">16</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
			<span style="color: #0000ff;">uint32_t</span> v<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">4</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span> m_buffer<span style="color: #008080;">;</span>
&nbsp;
		md5_ctx m_md5_ctx<span style="color: #008080;">;</span>
		<span style="color: #0000ff;">uint32_t</span> m_block<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">512</span><span style="color: #000040;">/</span><span style="color: #0000dd;">32</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p>The <code>Grandom</code> class uses MD5 as the hash function, 16-bit samples and 44.1KHz sampling rate. This configuration should work well on any modern sound card. The full implementation is available in <a href="/wp-content/uploads/2010/08/grandom-1.0.tar.bz2"><code>grandom-1.0.tar.bz2</code></a>, which is a small test app.</p>
<p>To have some evidence for the quality of the random number generation I&#8217;ve tested it against <a href="http://csrc.nist.gov/groups/ST/toolkit/rng/documentation_software.html">NIST&#8217;s statistical test suite</a>. I&#8217;ve tested 320 streams of 1M bits each. The results showed that the random number generator passed all of the tests but one (one of the non-overlapping pattern test, although it was very close to pass). If you wan to see the results, take a look at the <a href="/wp-content/uploads/2010/08/finalAnalysisReport.txt">final analysis report</a>.</p>
<p>Overall, <code>Grandom</code> seems to be a good way to generate small amounts of high quality random numbers.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2010/08/13/statistical-tests-for-my-audio-based-random-number-generator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Audio Based True Random Number Generator POC</title>
		<link>http://www.guyrutenberg.com/2010/05/14/audio-based-true-random-number-generator-poc/</link>
		<comments>http://www.guyrutenberg.com/2010/05/14/audio-based-true-random-number-generator-poc/#comments</comments>
		<pubDate>Fri, 14 May 2010 12:18:14 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/?p=678</guid>
		<description><![CDATA[Few days ago I came up with an idea to create a true random number generator based on noise gathered from a cheap microphone attached to my computer. Tests showed that when sampling the microphone, the least significant bit behaves pretty randomly. This lead me to think it might be good source for gathering entropy [...]]]></description>
			<content:encoded><![CDATA[<p>Few days ago I came up with an idea to create a true random number generator based on noise gathered from a cheap microphone attached to my computer. Tests showed that when sampling the microphone, the least significant bit behaves pretty randomly. This lead me to think it might be good source for gathering entropy for a true random number generator.<br />
<span id="more-678"></span><br />
The base design was to gather the noise from the microphone than apply a process that will make in more uniform and refine its randomness. After some design iterations I came up with a process based on applying a hash function to the noise. Each iteration involves filling block of the hash function from the least significant bits of the microphone output and applying the hash. Each iteration outputs the current hash digest. Assuming the hash function is uniform, this will output a uniformly distributed blocks of bits. Furthermore, because there the previous state of the hash function influences the next digest computation, the process accumulates entropy that can smooth out potentially less random blocks. Because for all commonly used hash function the block size is much larger than the digest size the output can tell much about the current state or any future or past state. This also holds true even if someone can find all pre-images of the hash function as the amount of possible states will be too big.</p>
<p>I&#8217;ve built a Python proof of concept (using md5 as a hash function) suitable for Linux.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> hashlib
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">struct</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> GRandom:
    <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: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">audio</span> = <span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;/dev/dsp&quot;</span>,<span style="color: #483d8b;">&quot;rb&quot;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: #008000;">hash</span> = hashlib.<span style="color: #dc143c;">md5</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> get_raw_block<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        buffer = <span style="color: #008000;">self</span>.<span style="color: black;">audio</span>.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: #008000;">hash</span>.<span style="color: black;">block_size</span><span style="color: #66cc66;">*</span><span style="color: #ff4500;">8</span><span style="color: black;">&#41;</span>
        <span style="color: #dc143c;">bytes</span> = <span style="color: #dc143c;">struct</span>.<span style="color: black;">unpack</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;%iB&quot;</span><span style="color: #66cc66;">%</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>buffer<span style="color: black;">&#41;</span>, buffer<span style="color: black;">&#41;</span>
&nbsp;
        longs = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</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: #008000;">self</span>.<span style="color: #008000;">hash</span>.<span style="color: black;">block_size</span>/<span style="color: #ff4500;">4</span><span style="color: black;">&#41;</span>:
            temp = <span style="color: #ff4500;">0</span>
            <span style="color: #ff7700;font-weight:bold;">for</span> b <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #dc143c;">bytes</span><span style="color: black;">&#91;</span>i<span style="color: #66cc66;">*</span><span style="color: #ff4500;">32</span>:<span style="color: black;">&#40;</span>i+<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">*</span><span style="color: #ff4500;">32</span><span style="color: black;">&#93;</span>:
                temp = <span style="color: black;">&#40;</span>temp <span style="color: #66cc66;">&lt;&lt;</span> <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span> ^ <span style="color: black;">&#40;</span>b <span style="color: #66cc66;">&amp;</span> <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
            longs.<span style="color: black;">append</span><span style="color: black;">&#40;</span>temp<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #dc143c;">struct</span>.<span style="color: black;">pack</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;%iI&quot;</span><span style="color: #66cc66;">%</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>longs<span style="color: black;">&#41;</span>, <span style="color: #66cc66;">*</span>longs<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> get_block<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: #008000;">hash</span>.<span style="color: black;">update</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">get_raw_block</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>.<span style="color: #008000;">hash</span>.<span style="color: black;">digest</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>The amount of generated bits per second is given by (sample rate)*(digest size)/(block size). So for 8KHz (default) sampling rate and md5 we&#8217;ll get a theoretical speed of 2000b/s. SHA type hashes have higher digest to block size ration thus may result in higher speeds. Another source of speed up may be to change the sample rate of the microphone. But setting it too high may have negative effects on the entropy. The code may get a considerable performance gain by porting it to c/c++, as it uses both bit manipulations and calculates hashes. Anyways, even the Python implementation&#8217;s speed allows us it be used for many cases where true randomness is required, such as generating passwords.</p>
<p><strong>Update 2010-08-07</strong> I&#8217;ve corrected the speed calculation.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2010/05/14/audio-based-true-random-number-generator-poc/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Hash Puppy 0.2</title>
		<link>http://www.guyrutenberg.com/2010/03/31/hash-puppy-0-2/</link>
		<comments>http://www.guyrutenberg.com/2010/03/31/hash-puppy-0-2/#comments</comments>
		<pubDate>Tue, 30 Mar 2010 21:15:06 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[Hash Puppy]]></category>
		<category><![CDATA[QT]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/?p=656</guid>
		<description><![CDATA[This is an update for my simple easy-to-use checksum calculator. It supports md4, md5, and sha1 hash functions. I wrote the project as a way to experience and learn Qt. Changes since the previous version (Hash Puppy 0.1) include ability to abort a checksum calculation and improved GUI responsiveness. Also there were other minor tweaks [...]]]></description>
			<content:encoded><![CDATA[<p>This is an update for my simple easy-to-use checksum calculator. It supports md4, md5, and sha1 hash functions. I wrote the project as a way to experience and learn Qt.<br />
<img src="http://www.guyrutenberg.com/wp-content/uploads/2010/03/hashpuppy-0.2.png" alt="" title="hashpuppy-0.2" width="565" height="205" class="aligncenter size-full wp-image-661" /><br />
Changes since the previous version (<a href="/2009/05/29/hash-puppy-a-qt-checksum-calculator/">Hash Puppy 0.1</a>) include ability to abort a checksum calculation and improved GUI responsiveness. Also there were other minor tweaks to make Hash Puppy easier to use.<br />
<span id="more-656"></span><br />
The software is available under the GPL license.<br />
Source code: <a href="/wp-content/uploads/2010/03/hashpuppy-0.2.tar.gz">hashpuppy-0.2.tar.gz</a><br />
Binary for Windows: <a href="/wp-content/uploads/2010/03/hashpuppy-0.2.zip">hashpuppy-0.2.zip</a> (Compiled against Qt 4.6.2).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2010/03/31/hash-puppy-0-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Improved FTP Backup for WordPress</title>
		<link>http://www.guyrutenberg.com/2010/02/28/improved-ftp-backup-for-wordpress/</link>
		<comments>http://www.guyrutenberg.com/2010/02/28/improved-ftp-backup-for-wordpress/#comments</comments>
		<pubDate>Sun, 28 Feb 2010 06:38:44 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[Bash]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[backup]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/?p=637</guid>
		<description><![CDATA[This script backups both the database and files of a WordPress blog into a remote FTP server (while keeping a local copy). It&#8217;s an update of my WordPress Backup to FTP script. The main changes are auto-detecting database settings and better support for caching plugins (specifically WP-Cache). The new version makes it easier to backup [...]]]></description>
			<content:encoded><![CDATA[<p>This script backups both the database and files of a WordPress blog into a remote FTP server (while keeping a local copy). It&#8217;s an update of my <a href="/2009/01/06/wordpress-backup-to-ftp/">WordPress Backup to FTP</a> script. The main changes are auto-detecting database settings and better support for caching plugins (specifically WP-Cache). The new version makes it easier to backup multiple WordPress blogs to the same FTP server.<br />
<span id="more-637"></span><br />
Usage is pretty simple after a short initial configuration. First, save the the script and make it executable:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">chmod</span> +x wp-backup</pre></div></div>

<p>(assuming you saved it under the name <code>wp-backup</code>). After saving it edit the file with your favorite editor and set the 5 configuration variable to whatever is appropriate for you. <code>BACKUP_DIR</code> is the folder to save the local backups to. <code>FTP_HOST</code>, <code>FTP_USER</code>, <code>FTP_PASS</code> control the FTP host, username and password, respectively, for the remote backup server. <code>FTP_BACKUP_DIR</code> sets the folder on the FTP server to save the remote backup to.</p>
<p>Now that the initial configuration is done, all you need to do is execute the script and give the path to the blog as an argument. For example:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>wp-backup <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>someuser<span style="color: #000000; font-weight: bold;">/</span>myblog</pre></div></div>

<p>And that it, the script will backup your files (excluding cache) and database to both a local and remote locations. This allows using the same script to backup multiple WordPress blogs, unlike the previous script which had to be modified for each blog.</p>
<p>And now the script itself:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Copyright 2008, 2010 Guy Rutenberg &lt;http://www.guyrutenberg.com/contact-me&gt;</span>
<span style="color: #666666; font-style: italic;"># WordPress FTP backup 2.0</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># Easily backup wordpress instances via ftp.</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># Change Log:</span>
<span style="color: #666666; font-style: italic;"># ===========</span>
<span style="color: #666666; font-style: italic;"># 2.0:</span>
<span style="color: #666666; font-style: italic;">#  - Auto-detect database settings.</span>
<span style="color: #666666; font-style: italic;">#  - Exclude cache data from backups.</span>
&nbsp;
<span style="color: #007800;">BACKUP_DIR</span>=
<span style="color: #007800;">FTP_HOST</span>=
<span style="color: #007800;">FTP_USER</span>=
<span style="color: #007800;">FTP_PASS</span>=
<span style="color: #007800;">FTP_BACKUP_DIR</span>=
&nbsp;
<span style="color: #666666; font-style: italic;"># end of configuration - you probably don't need to touch anything bellow</span>
&nbsp;
<span style="color: #007800;">PROG</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">basename</span> <span style="color: #ff0000;">&quot;$0&quot;</span><span style="color: #000000; font-weight: bold;">`</span>
print_usage <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;USAGE: <span style="color: #007800;">${PROG}</span> [options] BLOG_ROOT&quot;</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Backup a WordPress blog&quot;</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
print_help <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>  <span style="color: #7a0874; font-weight: bold;">&#123;</span>
    print_usage
    <span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #cc0000; font-style: italic;">&lt;&lt; EOF
&nbsp;
Options:
    -h, --help          show this help message and exit
&nbsp;
EOF</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #007800;">TEMP</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">getopt</span> <span style="color: #660033;">-o</span> h <span style="color: #660033;">--long</span> <span style="color: #7a0874; font-weight: bold;">help</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$PROG</span>&quot;</span> <span style="color: #660033;">--</span> <span style="color: #ff0000;">&quot;$@&quot;</span><span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">$?</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>; <span style="color: #000000; font-weight: bold;">then</span>
    print_usage
    <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">eval</span> <span style="color: #000000; font-weight: bold;">set</span> <span style="color: #660033;">--</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$TEMP</span>&quot;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #c20cb9; font-weight: bold;">true</span> ; <span style="color: #000000; font-weight: bold;">do</span>
    <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #ff0000;">&quot;$1&quot;</span> <span style="color: #000000; font-weight: bold;">in</span>
        <span style="color: #660033;">-h</span><span style="color: #000000; font-weight: bold;">|</span>--help<span style="color: #7a0874; font-weight: bold;">&#41;</span> print_help; <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000; font-weight: bold;">;;</span>
        --<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">shift</span>; <span style="color: #7a0874; font-weight: bold;">break</span><span style="color: #000000; font-weight: bold;">;;</span>
    <span style="color: #000000; font-weight: bold;">esac</span>
<span style="color: #000000; font-weight: bold;">done</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-z</span> <span style="color: #ff0000;">&quot;$1&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #000000; font-weight: bold;">then</span>
 print_usage <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>stderr
 <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #007800;">BLOG_DIR</span>=<span style="color: #007800;">$1</span>
<span style="color: #007800;">DB_NAME</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;&lt;?php require_once(<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #007800;">${BLOG_DIR}</span>/wp-config.php<span style="color: #000099; font-weight: bold;">\&quot;</span>); echo DB_NAME;&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> php<span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #007800;">DB_USER</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;&lt;?php require_once(<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #007800;">${BLOG_DIR}</span>/wp-config.php<span style="color: #000099; font-weight: bold;">\&quot;</span>); echo DB_USER;&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> php<span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #007800;">DB_PASS</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;&lt;?php require_once(<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #007800;">${BLOG_DIR}</span>/wp-config.php<span style="color: #000099; font-weight: bold;">\&quot;</span>); echo DB_PASSWORD;&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> php<span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #007800;">DB_HOST</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;&lt;?php require_once(<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #007800;">${BLOG_DIR}</span>/wp-config.php<span style="color: #000099; font-weight: bold;">\&quot;</span>); echo DB_HOST;&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> php<span style="color: #000000; font-weight: bold;">`</span>
&nbsp;
<span style="color: #007800;">BLOG_DIR</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">dirname</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$BLOG_DIR</span>&quot;</span><span style="color: #000000; font-weight: bold;">`/`</span><span style="color: #c20cb9; font-weight: bold;">basename</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$BLOG_DIR</span>&quot;</span><span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #007800;">BACKUP_DIR</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">dirname</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$BACKUP_DIR</span>&quot;</span><span style="color: #000000; font-weight: bold;">`/`</span><span style="color: #c20cb9; font-weight: bold;">basename</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$BACKUP_DIR</span>&quot;</span><span style="color: #000000; font-weight: bold;">`</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;dumping database... &quot;</span>
<span style="color: #007800;">DUMP_NAME</span>=<span style="color: #800000;">${DB_NAME}</span>-$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">date</span> +<span style="color: #000000; font-weight: bold;">%</span>Y<span style="color: #000000; font-weight: bold;">%</span>m<span style="color: #000000; font-weight: bold;">%</span>d<span style="color: #7a0874; font-weight: bold;">&#41;</span>.sql.bz2
mysqldump <span style="color: #660033;">--user</span>=<span style="color: #800000;">${DB_USER}</span> <span style="color: #660033;">--password</span>=<span style="color: #800000;">${DB_PASS}</span> <span style="color: #660033;">--host</span>=<span style="color: #800000;">${DB_HOST}</span> \
 <span style="color: #660033;">--databases</span> <span style="color: #800000;">${DB_NAME}</span> \
 <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">bzip2</span> <span style="color: #660033;">-c</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #800000;">${BACKUP_DIR}</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #800000;">${DUMP_NAME}</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;$?&quot;</span> <span style="color: #660033;">-ne</span> <span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
	<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;failed!&quot;</span>
	<span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span>
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;done&quot;</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;Creating tarball... &quot;</span>
<span style="color: #007800;">TAR_NAME</span>=<span style="color: #800000;">${BLOG_DIR##*/}</span>-$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">date</span> +<span style="color: #000000; font-weight: bold;">%</span>Y<span style="color: #000000; font-weight: bold;">%</span>m<span style="color: #000000; font-weight: bold;">%</span>d<span style="color: #7a0874; font-weight: bold;">&#41;</span>.tar.bz2
<span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-cjf</span> <span style="color: #800000;">${BACKUP_DIR}</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #800000;">${BLOG_DIR##*/}</span>-$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">date</span> +<span style="color: #000000; font-weight: bold;">%</span>Y<span style="color: #000000; font-weight: bold;">%</span>m<span style="color: #000000; font-weight: bold;">%</span>d<span style="color: #7a0874; font-weight: bold;">&#41;</span>.tar.bz2 <span style="color: #660033;">--exclude</span> cache <span style="color: #800000;">${BLOG_DIR}</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;$?&quot;</span> <span style="color: #660033;">-ne</span> <span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
	<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;failed!&quot;</span>
	<span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">2</span>
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;done&quot;</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;Uploading SQL dump and tarball to FTP... &quot;</span>
lftp <span style="color: #660033;">-u</span> <span style="color: #800000;">${FTP_USER}</span>,<span style="color: #800000;">${FTP_PASS}</span> <span style="color: #800000;">${FTP_HOST}</span> <span style="color: #cc0000; font-style: italic;">&lt;&lt;EOF
cd &quot;${FTP_BACKUP_DIR}&quot;
put &quot;${BACKUP_DIR}/${DUMP_NAME}&quot;
put &quot;${BACKUP_DIR}/${TAR_NAME}&quot;
&nbsp;
EOF</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;$?&quot;</span> <span style="color: #660033;">-ne</span> <span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
	<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;failed!&quot;</span>
	<span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">3</span>
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;done&quot;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2010/02/28/improved-ftp-backup-for-wordpress/feed/</wfw:commentRss>
		<slash:comments>43</slash:comments>
		</item>
		<item>
		<title>An Early Release of the New cssrtl.py-2.0</title>
		<link>http://www.guyrutenberg.com/2009/09/20/an-early-release-of-the-new-cssrtl-py-2-0/</link>
		<comments>http://www.guyrutenberg.com/2009/09/20/an-early-release-of-the-new-cssrtl-py-2-0/#comments</comments>
		<pubDate>Sun, 20 Sep 2009 10:00:50 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[cssrtl.py]]></category>
		<category><![CDATA[RTL]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/?p=578</guid>
		<description><![CDATA[It has been three years since I&#8217;ve released the original version of cssrtl.py (and two since it&#8217;s re-release). The old version did a nice job, but experience gained during that time led me to write from scratch a new version. I&#8217;ve detailed more than a month ago, the basic principles and ideas that guided me [...]]]></description>
			<content:encoded><![CDATA[<p>It has been three years since I&#8217;ve released the <a href="/2007/12/28/convert-css-layout-to-rtl-cssrtlpy/">original version</a> of <code>cssrtl.py</code> (and two since it&#8217;s re-release). The old version did a nice job, but experience gained during that time led me to write from scratch a new version. I&#8217;ve detailed more than a month ago, the <a href="/2009/08/05/designing-a-better-a-css-rtl-convertor/">basic principles and ideas</a> that guided me to design a better tool to help adapting CSS files from left-to-right to right-to-left.</p>
<p>The guidelines weren&#8217;t just empty words, they were written while working on the <a href="/2009/08/15/rtl-and-hebrew-adaptation-of-the-fusion-wordpress-theme/">Hebrew adaptation to the Fusion theme</a> and in the same time writing a new proof-of-concept version of <code>cssrtl.py</code>. The original intent was to release a more mature version of that code when it will be completed. However, due to the apparent shortage of time in the present and foreseeable future, I can&#8217;t see myself complete the project any time soon. So following the &#8220;release early&#8221; mantra, I&#8217;ve decided to release the code as-is. As I said, the code is in working state, but not polished, so it may be of benefit but may contain bugs. If you find any bugs or have any suggestions, I would be glad to hear.<br />
<span id="more-578"></span></p>
<h3>Download</h3>
<p>You can download the new version from here: <a href="/wp-content/uploads/2009/09/cssrtl.py-2.0.tar.bz2">cssrtl.py-2.0.tar.bz2</a>. The code is available under the GPLv2 or any later version.</p>
<h3>Usage</h3>
<p>The new version works by creating a seperate css &#8220;fix&#8221; file that fixes the directionality of the css styles.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">./cssrtl.py &lt; main.css &gt; main-rtl.css</pre></div></div>

<p>Afterward, just link the <code>main-rtl.css</code> after <code>main.css</code> in your HTML files.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2009/09/20/an-early-release-of-the-new-cssrtl-py-2-0/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.861 seconds -->

