<?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>schmichael&#039;s blog &#187; json</title>
	<atom:link href="http://blog.schmichael.com/tag/json/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.schmichael.com</link>
	<description>good good study, day day up</description>
	<lastBuildDate>Wed, 16 May 2012 23:59:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Handy Python Progress for JSON module</title>
		<link>http://blog.schmichael.com/2009/04/26/handy-python-progress-for-json-module/</link>
		<comments>http://blog.schmichael.com/2009/04/26/handy-python-progress-for-json-module/#comments</comments>
		<pubDate>Sun, 26 Apr 2009 23:26:04 +0000</pubDate>
		<dc:creator>Michael Schurter</dc:creator>
				<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[json]]></category>

		<guid isPermaLink="false">http://michael.susens-schurter.com/blog/?p=642</guid>
		<description><![CDATA[I&#8217;ve been spending a good deal of time the past couple of days processing large JSON files to try and fix some corrupted data (long story, short version: my fault). While JSON is a fast file format to work with, &#8230; <a href="http://blog.schmichael.com/2009/04/26/handy-python-progress-for-json-module/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been spending a good deal of time the past couple of days processing large JSON files to try and fix some corrupted data (long story, short version: my fault).  While JSON is a fast file format to work with, processing > 50 MB of any data format takes some time.</p>
<p>So to give myself some idea of what was going on, I whipped up a small progress bar for Python 2.6&#8242;s <a href="http://docs.python.org/library/json.html">json</a> module (works on <a href="http://pypi.python.org/pypi/simplejson">simplejson</a> if you&#8217;re still using 2.4/2.5):</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;
<span style="color: #ff7700;font-weight:bold;">class</span> JsonProgress<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">count</span> = <span style="color: #ff4500;">0</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__call__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, obj<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">count</span> += <span style="color: #ff4500;">1</span>
        <span style="color: #dc143c;">sys</span>.<span style="color: black;">stdout</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\r</span>%8d&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: #008000;">self</span>.<span style="color: black;">count</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> obj</pre></div></div>

<p>And then use it as the object_hook when loading JSON:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">f = <span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'foo.json'</span><span style="color: black;">&#41;</span>
foo = json.<span style="color: black;">load</span><span style="color: black;">&#40;</span>f, object_hook=JsonProgress<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;">print</span> <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\r</span>Done&quot;</span> <span style="color: #808080; font-style: italic;"># \r in the next line erases the progress output</span></pre></div></div>

<p>Although JsonProgress is a poor name since its also useful in generic list comprehensions:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">progress = JsonProgress<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
foo = <span style="color: black;">&#91;</span>progress<span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> bar<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;">\n</span>Done&quot;</span> <span style="color: #808080; font-style: italic;"># \n prints a newline so the progress output is kept</span></pre></div></div>

<p>Obviously this is a performance hit, but still quite handy for personal use when you just want to know that <em>something</em> is happening.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.schmichael.com/2009/04/26/handy-python-progress-for-json-module/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

