<?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>Desi Penguin’s Blog &#187; Code</title>
	<atom:link href="http://desipenguin.com/techblog/category/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://desipenguin.com/techblog</link>
	<description>Open source, Tech Tips, Reviews, Tutorials and more …</description>
	<lastBuildDate>Tue, 29 Jun 2010 16:38:10 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Importing CSV file data into sqlite3</title>
		<link>http://desipenguin.com/techblog/2009/07/13/importing-csv-file-data-into-sqlite3/</link>
		<comments>http://desipenguin.com/techblog/2009/07/13/importing-csv-file-data-into-sqlite3/#comments</comments>
		<pubDate>Mon, 13 Jul 2009 15:41:16 +0000</pubDate>
		<dc:creator>Mandar Vaze</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Hack]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[Comma-separated values]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[SQLite]]></category>
		<category><![CDATA[sqlite3]]></category>
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://desipenguin.com/techblog/?p=327</guid>
		<description><![CDATA[



Image via Wikipedia



I was trying to import the data from CSV file into sqlite3 database. Ideally this should be very simple task, with following the steps given in the sqlite tutorial. It is a matter of calling the sqlite command with separator argument, followed by an import operation, as listed below.

sqlite3 test.db  "create table [...]]]></description>
			<content:encoded><![CDATA[<div class="zemanta-img" style="margin: 1em; display: block;">
<div>
<dl class="wp-caption alignleft" style="width: 223px;">
<dt class="wp-caption-dt"><a href="http://commons.wikipedia.org/wiki/Image:SQLite_Logo_4.png"><img title="The :en:SQLite logo as of 2007-12-15" src="http://upload.wikimedia.org/wikipedia/commons/thumb/1/19/SQLite_Logo_4.png/300px-SQLite_Logo_4.png" alt="The :en:SQLite logo as of 2007-12-15" width="213" height="63" /></a></dt>
<dd class="wp-caption-dd zemanta-img-attribution" style="font-size: 0.8em;">Image via <a href="http://commons.wikipedia.org/wiki/Image:SQLite_Logo_4.png">Wikipedia</a></dd>
</dl>
</div>
</div>
<p>I was trying to import the data from <a class="zem_slink" title="Comma-separated values" rel="wikipedia" href="http://en.wikipedia.org/wiki/Comma-separated_values">CSV</a> file into <a class="zem_slink" title="SQLite" rel="homepage" href="http://www.sqlite.org/">sqlite3</a> database. Ideally this should be very simple task, with following the steps given in the sqlite tutorial. It is a matter of calling the sqlite command with separator argument, followed by an import operation, as listed below.</p>
<blockquote>
<pre>sqlite3 test.db  "create table t1 (t1key INTEGER PRIMARY KEY,data TEXT);"
sqlite3 -separator , test.db ".import some.csv t1"</pre>
</blockquote>
<p>Except that main attribute of my CSV file was that it could contain single records with embedded comma. I was hoping that sqlite3 would be smart enough to detect that the fields were enclosed within double quotes and then separate by comma. But I soon realized that only a code specifically dealing with CSV would know about this.  As we can see in the example above, the import is a generic code and as a user I listed comma as a separator.</p>
<p>My Data looked something like this :</p>
<blockquote>
<pre>"1","data1"
"2","data2,data3"</pre>
</blockquote>
<p>So like  any *nix geek would do, I tried  providing double quote <strong>and</strong> comma as a separator. To my surprise it worked very well. I though separator would take only single character, and I had provided two (three?). Anyway, important thing to remember is to escape the single quote with a <a class="zem_slink" title="Backslash" rel="wikipedia" href="http://en.wikipedia.org/wiki/Backslash">backslash</a> (I didn&#8217;t try it without the backslash, may be that would work too)</p>
<p>So here is the syntax that worked :</p>
<blockquote>
<pre>sqlite3 -separator \", test.db ".import mydata.csv mytbl"</pre>
</blockquote>
<p><em>Update : Turns out <a href="http://code.google.com/p/sqlite-manager/downloads/list" target="_blank">SQLite Manager</a> is much better solution after all.  It is an </em><em><a id="project_summary_link" style="text-decoration: none; color: #000000;" href="http://code.google.com/p/sqlite-manager/">Extension for Firefox and other apps to manage any sqlite database.</a></em><em> Not only it took care of above situation, it also handled empty cells as well where the command line failed with following error message : </em></p>
<blockquote>
<pre><em>line 4: expected 3 columns of data but found 2</em></pre>
</blockquote>
<p><em>Data with missing cells : Notice two successive commas :</em></p>
<blockquote>
<pre><em>"1","data1","data2"
"2","data3,data4","data5"
"3",,"data6"</em></pre>
</blockquote>
<div class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><a class="zemanta-pixie-a" title="Reblog this post [with Zemanta]" href="http://reblog.zemanta.com/zemified/037f00cf-f2be-47cc-89d4-863c249a4f99/"><img class="zemanta-pixie-img" style="border: medium none; float: right;" src="http://img.zemanta.com/reblog_c.png?x-id=037f00cf-f2be-47cc-89d4-863c249a4f99" alt="Reblog this post [with Zemanta]" /></a><span class="zem-script more-related pretty-attribution"><script src="http://static.zemanta.com/readside/loader.js" type="text/javascript"></script></span></div>
]]></content:encoded>
			<wfw:commentRss>http://desipenguin.com/techblog/2009/07/13/importing-csv-file-data-into-sqlite3/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Reading CSV files in IronPython</title>
		<link>http://desipenguin.com/techblog/2009/07/03/reading-csv-files-in-ironpython/</link>
		<comments>http://desipenguin.com/techblog/2009/07/03/reading-csv-files-in-ironpython/#comments</comments>
		<pubDate>Fri, 03 Jul 2009 16:13:12 +0000</pubDate>
		<dc:creator>Mandar Vaze</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[windows]]></category>
		<category><![CDATA[Development Tools]]></category>
		<category><![CDATA[IronPython]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://desipenguin.com/techblog/?p=294</guid>
		<description><![CDATA[



Image via Wikipedia



This is in continuation with my previous blog post :
To get IronPython to use Standard Python Modules,  one needs to add  the following two lines to C:\IronPython-2.0.1\Lib\site.py :
import sys
sys.path.append(r"C:\Python25\Lib")
While this works for most part, it doesn’t help if you are using Python extensions written in C. More about my specific problems in [...]]]></description>
			<content:encoded><![CDATA[<div class="zemanta-img" style="margin: 1em; display: block;">
<div>
<dl class="wp-caption alignright" style="width: 123px;">
<dt class="wp-caption-dt"><a href="http://en.wikipedia.org/wiki/Image:CsvDelimited001.svg"><img title="stylized depiction of a csv text file" src="http://upload.wikimedia.org/wikipedia/en/thumb/3/38/CsvDelimited001.svg/113px-CsvDelimited001.svg.png" alt="stylized depiction of a csv text file" width="113" height="113" /></a></dt>
<dd class="wp-caption-dd zemanta-img-attribution" style="font-size: 0.8em;">Image via <a href="http://en.wikipedia.org/wiki/Image:CsvDelimited001.svg">Wikipedia</a></dd>
</dl>
</div>
</div>
<p>This is in continuation with my <a href="http://desipenguin.com/techblog/2009/06/23/ironpython/" target="_blank">previous</a> blog post :</p>
<blockquote><p><span>To get <a class="zem_slink" title="IronPython" rel="homepage" href="http://www.codeplex.com/IronPython">IronPython</a> to use Standard <a class="zem_slink" title="Python (programming language)" rel="homepage" href="http://www.python.org/">Python</a> Modules,  one needs to</span> add  the following two lines to <em>C:\IronPython-2.0.1\Lib\site.py</em> :</p>
<pre>import sys
sys.path.append(r"C:\Python25\Lib")</pre>
<p>While this works for most part, it doesn’t help if you are using Python extensions written in C. More about my specific problems in another post. But there is an open source project <a href="http://code.google.com/p/ironclad/" target="_blank">IronClad</a> to deal specifically with this issue. In the meantime, you can check  <a href="http://ironpython.codeplex.com/Wiki/View.aspx?title=Differences" target="_blank">the differences between IronPython and CPython</a></p></blockquote>
<p>Reading (and writing to) <a class="zem_slink" title="Comma-separated values" rel="wikipedia" href="http://en.wikipedia.org/wiki/Comma-separated_values">CSV</a> file is critical part of my program, while in stadard python it was as easy as &#8220;import csv&#8221;, the same thing took some efforts to get it working in IronPython. I got the following error for my import statement</p>
<pre>Error on line 7 in csv.py
from functools import reduce</pre>
<p>I also tried using <a class="zem_slink" title="ActivePython" rel="wikipedia" href="http://en.wikipedia.org/wiki/ActivePython">ActiveState Python</a> 2.5.2.2 (which I already had from few months ago, didn&#8217;t feel like downloading the latest version till the problem was fixed) But that didn&#8217;t help either. With ActiveState, I got the same error on the same line, except this time it was for _csv.</p>
<p>To Quote from <a href="http://www.ironpython.info/index.php/Reading_CSV_Files" target="_blank">IronPython Cookbook</a> :</p>
<blockquote><p>For some reason the Python standard library <a class="external text" title="http://docs.python.org/lib/module-csv.html" href="http://docs.python.org/lib/module-csv.html">csv module</a> is written in C, which means that it isn&#8217;t available to IronPython.</p></blockquote>
<p>The cookbook points to a third party library called <a href="http://www.codeproject.com/KB/database/CsvReader.aspx" target="_blank">A Fast Csv Reader</a> . The cookbook has a nice example of how to use the said DLL with your IronPython Program.</p>
<p>It wasn&#8217;t clear to me as to why I had to register at Code Project to download this binary since it is provided under <a href="http://en.wikipedia.org/wiki/MIT_License" target="_blank">MIT Open Source License</a>. But whom am I gonna complain to ? Beggars can&#8217;t be choosers <img src='http://desipenguin.com/techblog/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>
<div class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><a class="zemanta-pixie-a" title="Reblog this post [with Zemanta]" href="http://reblog.zemanta.com/zemified/97b6f3b5-f068-4b14-accd-9a79a802c586/"><img class="zemanta-pixie-img" style="border: medium none; float: right;" src="http://img.zemanta.com/reblog_b.png?x-id=97b6f3b5-f068-4b14-accd-9a79a802c586" alt="Reblog this post [with Zemanta]" /></a><span class="zem-script more-related pretty-attribution"><script src="http://static.zemanta.com/readside/loader.js" type="text/javascript"></script></span></div>
]]></content:encoded>
			<wfw:commentRss>http://desipenguin.com/techblog/2009/07/03/reading-csv-files-in-ironpython/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python : Fun with subprocess.stdin</title>
		<link>http://desipenguin.com/techblog/2009/01/13/fun-with-python-subprocessstdin/</link>
		<comments>http://desipenguin.com/techblog/2009/01/13/fun-with-python-subprocessstdin/#comments</comments>
		<pubDate>Tue, 13 Jan 2009 06:38:08 +0000</pubDate>
		<dc:creator>Mandar Vaze</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://desipenguin.wordpress.com/?p=129</guid>
		<description><![CDATA[As part of some automation work,  I needed to execute a program which requires each command to be explictely validated by the user by expliciltely selecting &#8220;y&#8221; or &#8220;n&#8221; , on the command line. Since I was processing a large number of files, I decided to use python script.
I used the subprocess module of python [...]]]></description>
			<content:encoded><![CDATA[<p>As part of some automation work,  I needed to execute a program which requires each command to be explictely validated by the user by expliciltely selecting &#8220;y&#8221; or &#8220;n&#8221; , on the command line. Since I was processing a large number of files, I decided to use python script.</p>
<p>I used the subprocess module of python to invoke the external program. It was easy to capture the output sent on stdout. In order to send &#8216;<em>y</em>&#8216; on stdin for each iteration,  I tried sending &#8216;<em>y</em>&#8216; on stdin, but that would not work.  The script would hang. After discussing this, with more experienced python programmer, it was suggested that one possible reason why the script hangs is may be because the stdin buffer wasn&#8217;t <em>flushed</em>.  Both of us were not sure how to do that. Then it was discussed that when we run the external program from command line, we not only type &#8216;<em>y</em>&#8216; as response, but we also hit <em>Enter </em>there after, which results in <em>flushing </em>the stdin buffer. So may be that is what I ought to try.</p>
<p>To my surprise, it worked. So the solution was to pass &#8216;<em>y/n</em>&#8216; instead of single &#8216;<em>y</em>&#8216;</p>
<p>Here is how my code looked like :<br />
<code><br />
import subprocess<br />
cmd = "...." # The command you wish to execute<br />
proc = subprocess.Popen(cmd,<br />
                       stdin=subprocess.PIPE,<br />
                       stdout=subprocess.PIPE)<br />
print proc.communicate('Y\n')[0]<br />
</code></p>
]]></content:encoded>
			<wfw:commentRss>http://desipenguin.com/techblog/2009/01/13/fun-with-python-subprocessstdin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Debugging Python Scripts</title>
		<link>http://desipenguin.com/techblog/2008/12/24/debugging-python-scripts/</link>
		<comments>http://desipenguin.com/techblog/2008/12/24/debugging-python-scripts/#comments</comments>
		<pubDate>Wed, 24 Dec 2008 01:57:06 +0000</pubDate>
		<dc:creator>Mandar Vaze</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[cross platform]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://desipenguin.wordpress.com/?p=108</guid>
		<description><![CDATA[Found a great way to debug python scripts interactively using pdb aka Python Debugger. It is similar to gdb used on *nix.
Essentially you import pdb in the beginning of the script, and wherever you need to start debugging, add following statement :
pdb.set_trace()
Now you execute the script from command line, and execution will stop where you [...]]]></description>
			<content:encoded><![CDATA[<p>Found a great way to debug python scripts interactively using pdb aka Python Debugger. It is similar to gdb used on *nix.<br />
Essentially you <code>import pdb</code> in the beginning of the script, and wherever you need to start debugging, add following statement :<br />
<code>pdb.set_trace()</code><br />
Now you execute the script from command line, and execution will stop where you have added set_trace() call. You are presented with pdb prompt. There after it is similar to gdb commands.</p>
<p><span id="more-108"></span>Here is the quick list of pdb commands</p>
<ul>
<li>n : Execute next line</li>
<li>s : Step inside a module</li>
<li>p : Print the value of a variable</li>
<li>c : Continue</li>
<li>l : List. Shows stack trace</li>
<li>q : Quit (Most important command <img src='http://desipenguin.com/techblog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  )</li>
</ul>
<p>For details, check this <a href="http://www.ferg.org/papers/debugging_in_python.html" target="_blank">site</a></p>
<h3>But what if I can&#8217;t debug interactively ?</h3>
<p>Typically, you come across this problem when you are running the script on a server. Even if the script worked OK in test environment, in live environment something goes wrong, because the script is executed by server, and you can not control the arguments that are being passed to the script. In this scenario, you need to resort to age-old logging technique</p>
<p>Here are few line of code you need to add to your script. Modify as per your needs (especially paths)<br />
Earlier in your script (as early as possible) add following lines :<br />
<code><br />
from time import strftime<br />
f = open('D:/temp/log.txt', 'r+')<br />
f.write("-- Begin Log at %s --n" % strftime("%Y-%m-%d %H:%M:%S"))<br />
</code></p>
<p>At the end of your script (as late as possible)  add following lines :<br />
<code><br />
f.write("-- End Log at %s --n" % strftime("%Y-%m-%d %H:%M:%S"))<br />
f.close()<br />
</code></p>
<p>Your basic logging infrastructure is ready. Now you can add <code>f.write()</code> lines through out your code, as and where needed.</p>
<p><span style="color:#808080;">Note : I&#8217;m part of pythondev group on twitter.  Visit the <a href="http://twittgroups.com/group/pythondev" target="_blank">website </a>for details.</span></p>
]]></content:encoded>
			<wfw:commentRss>http://desipenguin.com/techblog/2008/12/24/debugging-python-scripts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>My First Open Source Contribution</title>
		<link>http://desipenguin.com/techblog/2008/12/17/my-first-open-source-contribution/</link>
		<comments>http://desipenguin.com/techblog/2008/12/17/my-first-open-source-contribution/#comments</comments>
		<pubDate>Wed, 17 Dec 2008 16:53:53 +0000</pubDate>
		<dc:creator>Mandar Vaze</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[contribution]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[subversion]]></category>

		<guid isPermaLink="false">http://desipenguin.wordpress.com/?p=80</guid>
		<description><![CDATA[SVN Plot creates variety of graphs by parsing the Subversion repository.  The Project is created by Nitin Bhide. Now he happens to be my colleague. As part of learning Python, I&#8217;ve contributed to svnplot, hosted on Google Code.
Here are my changes  
Note : I&#8217;m part of pythondev group on twitter.  Visit the website for [...]]]></description>
			<content:encoded><![CDATA[<p>SVN Plot creates variety of graphs by parsing the Subversion repository.  The Project is created by <a href="http://nitinbhide.blogspot.com/" target="_blank">Nitin Bhide</a>. Now he happens to be my colleague. As part of learning Python, I&#8217;ve contributed to <a href="http://code.google.com/p/svnplot" target="_blank">svnplot</a>, hosted on Google Code.</p>
<p><a href="http://code.google.com/p/svnplot/source/detail?r=25" target="_blank">Here </a>are my changes <img src='http://desipenguin.com/techblog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><span style="color:#808080;">Note : I&#8217;m part of pythondev group on twitter.  Visit the <a href="http://twittgroups.com/group/pythondev" target="_blank">website </a>for details.</span></p>
]]></content:encoded>
			<wfw:commentRss>http://desipenguin.com/techblog/2008/12/17/my-first-open-source-contribution/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
