<?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>code.random() &#187; python</title>
	<atom:link href="http://code.pui.ch/category/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://code.pui.ch</link>
	<description>Random code-snippets and tips</description>
	<lastBuildDate>Sun, 04 Apr 2010 21:42:18 +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>Python: Print list of dicts as ascii table</title>
		<link>http://code.pui.ch/2010/02/27/python-print-list-of-dicts-as-ascii-table/</link>
		<comments>http://code.pui.ch/2010/02/27/python-print-list-of-dicts-as-ascii-table/#comments</comments>
		<pubDate>Sat, 27 Feb 2010 10:55:35 +0000</pubDate>
		<dc:creator>philipp.keller</dc:creator>
				<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://code.pui.ch/?p=17</guid>
		<description><![CDATA[Sometimes I want to print list of dicts as an ascii table, like this:

&#124; Programming Language &#124; Language Type &#124; Years of Experience &#124;
+----------------------+---------------+---------------------+
&#124; python               &#124; script        &#124;       [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes I want to print list of dicts as an ascii table, like this:</p>
<pre>
| Programming Language | Language Type | Years of Experience |
+----------------------+---------------+---------------------+
| python               | script        |                   4 |
| php                  | script        |                   5 |
| java                 | compiled      |                  11 |
| assember             | compiled      |                  15 |
</pre>
<p>I searched on Google - but without luck.</p>
<p>That's what I came up with - it's not particularly nice but it does the job:</p>
<div class="igBar"><span id="lpython-3"><a href="#" onclick="javascript:showPlainTxt('python-3'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PYTHON:</span>
<div id="python-3">
<div class="python">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #ff7700;font-weight:bold;">def</span> table_print<span style="color: black;">&#40;</span>data, title_row<span style="color: black;">&#41;</span>:</div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #483d8b;">""</span><span style="color: #483d8b;">"data: list of dicts, </span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #483d8b;">&nbsp; &nbsp; &nbsp; &nbsp;title_row: e.g. [('name', 'Programming Language'), ('type', 'Language Type')]</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #483d8b;">&nbsp; &nbsp; "</span><span style="color: #483d8b;">""</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; max_widths = <span style="color: black;">&#123;</span><span style="color: black;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; data_copy = <span style="color: black;">&#91;</span><span style="color: #008000;">dict</span><span style="color: black;">&#40;</span>title_row<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span> + <span style="color: #008000;">list</span><span style="color: black;">&#40;</span>data<span style="color: black;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> col <span style="color: #ff7700;font-weight:bold;">in</span> data_copy<span style="color: black;">&#91;</span><span style="color: #ff4500;color:#800000;">0</span><span style="color: black;">&#93;</span>.<span style="color: black;">keys</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:</div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; max_widths<span style="color: black;">&#91;</span>col<span style="color: black;">&#93;</span> = <span style="color: #008000;">max</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span><span style="color: #008000;">str</span><span style="color: black;">&#40;</span>row<span style="color: black;">&#91;</span>col<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> row <span style="color: #ff7700;font-weight:bold;">in</span> data_copy<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; cols_order = <span style="color: black;">&#91;</span>tup<span style="color: black;">&#91;</span><span style="color: #ff4500;color:#800000;">0</span><span style="color: black;">&#93;</span> <span style="color: #ff7700;font-weight:bold;">for</span> tup <span style="color: #ff7700;font-weight:bold;">in</span> title_row<span style="color: black;">&#93;</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">def</span> custom_just<span style="color: black;">&#40;</span>col, value<span style="color: black;">&#41;</span>:</div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">type</span><span style="color: black;">&#40;</span>value<span style="color: black;">&#41;</span> == <span style="color: #008000;">int</span>:</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>value<span style="color: black;">&#41;</span>.<span style="color: black;">rjust</span><span style="color: black;">&#40;</span>max_widths<span style="color: black;">&#91;</span>col<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">else</span>:</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> value.<span style="color: black;">ljust</span><span style="color: black;">&#40;</span>max_widths<span style="color: black;">&#91;</span>col<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> row <span style="color: #ff7700;font-weight:bold;">in</span> data_copy:</div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; row_str = <span style="color: #483d8b;">" | "</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span>custom_just<span style="color: black;">&#40;</span>col, row<span style="color: black;">&#91;</span>col<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> col <span style="color: #ff7700;font-weight:bold;">in</span> cols_order<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">"| %s |"</span> % row_str</div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> data_copy.<span style="color: black;">index</span><span style="color: black;">&#40;</span>row<span style="color: black;">&#41;</span> == <span style="color: #ff4500;color:#800000;">0</span>:</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; underline = <span style="color: #483d8b;">"-+-"</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'-'</span> * max_widths<span style="color: black;">&#91;</span>col<span style="color: black;">&#93;</span> <span style="color: #ff7700;font-weight:bold;">for</span> col <span style="color: #ff7700;font-weight:bold;">in</span> cols_order<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'+-%s-+'</span> % underline </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Use it like that:</p>
<div class="igBar"><span id="lpython-4"><a href="#" onclick="javascript:showPlainTxt('python-4'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PYTHON:</span>
<div id="python-4">
<div class="python">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">data = <span style="color: black;">&#91;</span><span style="color: #008000;">dict</span><span style="color: black;">&#40;</span>name=<span style="color: #483d8b;">'python'</span>, <span style="color: #008000;">type</span>=<span style="color: #483d8b;">'script'</span>, years_experience=<span style="color: #ff4500;color:#800000;">4</span><span style="color: black;">&#41;</span>,</div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #008000;">dict</span><span style="color: black;">&#40;</span>name=<span style="color: #483d8b;">'php'</span>, <span style="color: #008000;">type</span>=<span style="color: #483d8b;">'script'</span>, years_experience=<span style="color: #ff4500;color:#800000;">5</span><span style="color: black;">&#41;</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #008000;">dict</span><span style="color: black;">&#40;</span>name=<span style="color: #483d8b;">'java'</span>, <span style="color: #008000;">type</span>=<span style="color: #483d8b;">'compiled'</span>, years_experience=<span style="color: #ff4500;color:#800000;">11</span><span style="color: black;">&#41;</span>,</div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #008000;">dict</span><span style="color: black;">&#40;</span>name=<span style="color: #483d8b;">'assember'</span>, <span style="color: #008000;">type</span>=<span style="color: #483d8b;">'compiled'</span>, years_experience=<span style="color: #ff4500;color:#800000;">15</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: black;">&#93;</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">titles = <span style="color: black;">&#91;</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'name'</span>, <span style="color: #483d8b;">'Programming Language'</span><span style="color: black;">&#41;</span>, </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: black;">&#40;</span><span style="color: #483d8b;">'type'</span>, <span style="color: #483d8b;">'Language Type'</span><span style="color: black;">&#41;</span>, </div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: black;">&#40;</span><span style="color: #483d8b;">'years_experience'</span>, <span style="color: #483d8b;">'Years of Experience'</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">table_print<span style="color: black;">&#40;</span>data, titles<span style="color: black;">&#41;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>It will produce the table printed above. It's not fancy - the only 'smart' thing it does is <strong>right-adjusting integers, strings are left-adjusted</strong>.</p>
<p>P.S. no, I don't have 15 years of experience of Assembler - I just know it since 15 years - it's one of the first programming languages I learned - and I even wrote a text editor with it - then I learned that's probably not the best language to write an editor <img src='http://code.pui.ch/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://code.pui.ch/2010/02/27/python-print-list-of-dicts-as-ascii-table/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>dealing with &#8220;MySQL backend does not support timezone-aware datetimes&#8221;</title>
		<link>http://code.pui.ch/2009/12/30/dealing-with-mysql-backend-does-not-support-timezone-aware-datetimes/</link>
		<comments>http://code.pui.ch/2009/12/30/dealing-with-mysql-backend-does-not-support-timezone-aware-datetimes/#comments</comments>
		<pubDate>Wed, 30 Dec 2009 22:27:00 +0000</pubDate>
		<dc:creator>philipp.keller</dc:creator>
				<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://code.pui.ch/?p=16</guid>
		<description><![CDATA[When fetching events from an iCal feed and saving this into a database I got
MySQL backend does not support timezone-aware datetimes
This did the trick for me:
Install pytz (download here)
PLAIN TEXT
PYTHON:




import pytz


that_datetime_in_utc.astimezone&#40;pytz.timezone&#40;'Europe/Zurich'&#41;&#41;.replace&#40;tzinfo=None&#41; 






Caution: I don't really understand what I'm writing here - it feels like those posters in PHP forums who explain  'how to ...' [...]]]></description>
			<content:encoded><![CDATA[<p>When <a href="http://code.pui.ch/2009/12/29/fetch-publicly-available-google-calendar-data-with-python/">fetching events from an iCal feed and saving this into a database</a> I got</p>
<blockquote><p>MySQL backend does not support timezone-aware datetimes</p></blockquote>
<p>This did the trick for me:</p>
<p>Install <a href="http://pytz.sourceforge.net/">pytz</a> (<a href="http://sourceforge.net/projects/pytz/">download here</a>)</p>
<div class="igBar"><span id="lpython-6"><a href="#" onclick="javascript:showPlainTxt('python-6'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PYTHON:</span>
<div id="python-6">
<div class="python">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #ff7700;font-weight:bold;">import</span> pytz</div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">that_datetime_in_utc.<span style="color: black;">astimezone</span><span style="color: black;">&#40;</span>pytz.<span style="color: black;">timezone</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Europe/Zurich'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>.<span style="color: black;">replace</span><span style="color: black;">&#40;</span>tzinfo=<span style="color: #008000;">None</span><span style="color: black;">&#41;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p><strong>Caution</strong>: I don't really understand what I'm writing here - it feels like those posters in PHP forums who explain  'how to ...' and then trying to explain something they have no clue of, but well.. <img src='http://code.pui.ch/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://code.pui.ch/2009/12/30/dealing-with-mysql-backend-does-not-support-timezone-aware-datetimes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fetch publicly available google calendar data with python</title>
		<link>http://code.pui.ch/2009/12/29/fetch-publicly-available-google-calendar-data-with-python/</link>
		<comments>http://code.pui.ch/2009/12/29/fetch-publicly-available-google-calendar-data-with-python/#comments</comments>
		<pubDate>Tue, 29 Dec 2009 20:31:18 +0000</pubDate>
		<dc:creator>philipp.keller</dc:creator>
				<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://code.pui.ch/?p=15</guid>
		<description><![CDATA[I tried accessing the google data api with python - that api seems either overly complicated or just not suited for just grabbing events from a public google calendar.
This worked for me:

find out ical address (subscribe to the calendar, other calendars->arrow down->calendar settings)
install icalendar

Then this is possible:
PLAIN TEXT
PYTHON:




from icalendar import Calendar


import urllib


ics = urllib.urlopen&#40;'http://www.google.com/calendar/ical/fchppllvcaupb6fgguigobkfj4@group.calendar.google.com/public/basic.ics'&#41;.read&#40;&#41;


ical=Calendar.from_string&#40;ics&#41;


for vevent [...]]]></description>
			<content:encoded><![CDATA[<p>I tried <a href="http://code.google.com/apis/calendar/data/1.0/developers_guide_python.html">accessing the google data api with python</a> - that api seems either overly complicated or just not suited for just grabbing events from a public google calendar.</p>
<p>This worked for me:</p>
<ol>
<li>find out ical address (subscribe to the calendar, other calendars->arrow down->calendar settings)</li>
<li>install <a href="http://codespeak.net/icalendar/">icalendar</a></li>
</ol>
<p>Then this is possible:</p>
<div class="igBar"><span id="lpython-8"><a href="#" onclick="javascript:showPlainTxt('python-8'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PYTHON:</span>
<div id="python-8">
<div class="python">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #ff7700;font-weight:bold;">from</span> icalendar <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">Calendar</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">urllib</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">ics = <span style="color: #dc143c;">urllib</span>.<span style="color: black;">urlopen</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'http://www.google.com/calendar/ical/fchppllvcaupb6fgguigobkfj4@group.calendar.google.com/public/basic.ics'</span><span style="color: black;">&#41;</span>.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">ical=<span style="color: #dc143c;">Calendar</span>.<span style="color: black;">from_string</span><span style="color: black;">&#40;</span>ics<span style="color: black;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #ff7700;font-weight:bold;">for</span> vevent <span style="color: #ff7700;font-weight:bold;">in</span> ical.<span style="color: black;">subcomponents</span>:</div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> vevent.<span style="color: black;">name</span> != <span style="color: #483d8b;">"VEVENT"</span>:</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">continue</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; title = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>vevent.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'SUMMARY'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; description = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>vevent.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'DESCRIPTION'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; location = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>vevent.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'LOCATION'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; start = vevent.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'DTSTART'</span><span style="color: black;">&#41;</span>.<span style="color: black;">dt</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #808080; font-style: italic;"># a datetime</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; end = vevent.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'DTEND'</span><span style="color: black;">&#41;</span>.<span style="color: black;">dt</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #808080; font-style: italic;"># a datetime </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://code.pui.ch/2009/12/29/fetch-publicly-available-google-calendar-data-with-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Send javascript errors by mail</title>
		<link>http://code.pui.ch/2007/10/06/send-javascript-errors-by-mail/</link>
		<comments>http://code.pui.ch/2007/10/06/send-javascript-errors-by-mail/#comments</comments>
		<pubDate>Sat, 06 Oct 2007 12:33:42 +0000</pubDate>
		<dc:creator>philipp.keller</dc:creator>
				<category><![CDATA[django]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://code.pui.ch/2007/10/06/send-javascript-errors-by-mail/</guid>
		<description><![CDATA[I'm running a Django-powered site for a closed user group and added a bit of JavaScript magic here and there (mainly Prototype and Tooltip).
Now Django sends me a mail whenever a 404 or 500 error occurs. But when one of my users encounters a JavaScript-Error, I'm not informed. I thought anyone in the web has [...]]]></description>
			<content:encoded><![CDATA[<p>I'm running <a href="http://extranet.icoc.ch">a Django-powered site for a closed user group</a> and added a bit of JavaScript magic here and there (mainly <a href="http://www.prototypejs.org/">Prototype</a> and <a href="http://codylindley.com/Javascript/219/finding-a-javascript-tool-tip-script">Tooltip</a>).</p>
<p>Now Django sends me a mail whenever a 404 or 500 error occurs. But when one of my users encounters a JavaScript-Error, I'm not informed. I thought anyone in the web has solved this problem but didn't find anything, so here's my take: Just send any error using Ajax (here: using <a href="http://www.prototypejs.org/learn/introduction-to-ajax">Prototypes Ajax abstraction</a>) to the server</p>
<div class="igBar"><span id="ljavascript-11"><a href="#" onclick="javascript:showPlainTxt('javascript-11'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-11">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000066;">onerror</span> = Extranet.<span style="color: #006600;">mailError</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> mailError<span style="color: #66cc66;">&#40;</span>msg, url, line<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #003366; font-weight: bold;">var</span> postBody = <span style="color: #3366CC;">'url='</span> + url + <span style="color: #3366CC;">'&amp;line='</span> + line + <span style="color: #3366CC;">'&amp;message='</span> + escape<span style="color: #66cc66;">&#40;</span>msg<span style="color: #66cc66;">&#41;</span> + <span style="color: #3366CC;">'&amp;useragent='</span> + escape<span style="color: #66cc66;">&#40;</span>navigator.<span style="color: #006600;">userAgent</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #3366CC;">'&amp;user='</span> + escape<span style="color: #66cc66;">&#40;</span>user_name<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #003366; font-weight: bold;">var</span> myAjax = <span style="color: #003366; font-weight: bold;">new</span> Ajax.<span style="color: #006600;">Request</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'/api/jserror/'</span>, <span style="color: #66cc66;">&#123;</span>method: <span style="color: #3366CC;">'post'</span>, postBody: postBody<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p><code>user_name</code> is a JavaScript variable holding the Django username (so I know whom I can inform when the error is fixed).</p>
<p>On the server side, I just send me mails containing the JavaScript error message, the username and the user agent:</p>
<div class="igBar"><span id="lpython-12"><a href="#" onclick="javascript:showPlainTxt('python-12'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PYTHON:</span>
<div id="python-12">
<div class="python">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #ff7700;font-weight:bold;">def</span> jserror<span style="color: black;">&#40;</span>request<span style="color: black;">&#41;</span>:</div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">core</span>.<span style="color: black;">mail</span> <span style="color: #ff7700;font-weight:bold;">import</span> mail_admins</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; omit_messages = <span style="color: black;">&#91;</span><span style="color: #483d8b;">'pointerobj is not defined'</span>, <span style="color: #483d8b;">'tipobj is not defined'</span>, <span style="color: #483d8b;">'ns6 is not defined'</span>, <span style="color: #483d8b;">'enabletip is not defined'</span><span style="color: black;">&#93;</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> request.<span style="color: black;">POST</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'message'</span>, <span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #ff7700;font-weight:bold;">in</span> omit_messages:</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; message = <span style="color: #483d8b;">""</span><span style="color: #483d8b;">"url: %s (%s)</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #483d8b;">%s</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #483d8b;">user-agent: %s</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #483d8b;">username: %s</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #483d8b;">"</span><span style="color: #483d8b;">""</span> % <span style="color: black;">&#40;</span>request.<span style="color: black;">POST</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'url'</span>, <span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span>, request.<span style="color: black;">POST</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'line'</span>, <span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span>, request.<span style="color: black;">POST</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'message'</span>, <span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span>, request.<span style="color: black;">POST</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'useragent'</span>, <span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span>, request.<span style="color: black;">POST</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'user'</span>, <span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; mail_admins<span style="color: black;">&#40;</span><span style="color: #483d8b;">"javascript error"</span>, urldecode<span style="color: black;">&#40;</span>message<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> HttpResponse<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Yeah, that's all very trivial but I wonder what other solutions exist for this problem...</p>
]]></content:encoded>
			<wfw:commentRss>http://code.pui.ch/2007/10/06/send-javascript-errors-by-mail/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Django: Serve big files via fcgid</title>
		<link>http://code.pui.ch/2007/10/03/django-serve-big-files-via-fcgid/</link>
		<comments>http://code.pui.ch/2007/10/03/django-serve-big-files-via-fcgid/#comments</comments>
		<pubDate>Wed, 03 Oct 2007 07:14:28 +0000</pubDate>
		<dc:creator>philipp.keller</dc:creator>
				<category><![CDATA[django]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://code.pui.ch/2007/10/03/django-serve-big-files-via-fcgid/</guid>
		<description><![CDATA[I've got a django project running which requires you to login to access files.
That means that I have to serve the files via python, like this:
PLAIN TEXT
PYTHON:




@login_required


def download&#40;request, filename&#41;:


&#160; &#160; # ... some code specific to my site ...


&#160; &#160; response = HttpResponse&#40;mimetype=postUpload.mimetype&#41;


&#160; &#160; response&#91;'Content-Disposition'&#93; = "attachment; filename=" + original_filename


&#160; &#160; response&#91;'Content-Length'&#93; = os.path.getsize&#40;filename_path&#41;


&#160; &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>I've got a <a href="http://extranet.icoc.ch">django project</a> running which requires you to login to access files.<br />
That means that I have to serve the files via python, like this:</p>
<div class="igBar"><span id="lpython-15"><a href="#" onclick="javascript:showPlainTxt('python-15'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PYTHON:</span>
<div id="python-15">
<div class="python">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">@login_required</div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #ff7700;font-weight:bold;">def</span> download<span style="color: black;">&#40;</span>request, filename<span style="color: black;">&#41;</span>:</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># ... some code specific to my site ...</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; response = HttpResponse<span style="color: black;">&#40;</span>mimetype=postUpload.<span style="color: black;">mimetype</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; response<span style="color: black;">&#91;</span><span style="color: #483d8b;">'Content-Disposition'</span><span style="color: black;">&#93;</span> = <span style="color: #483d8b;">"attachment; filename="</span> + original_filename</div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; response<span style="color: black;">&#91;</span><span style="color: #483d8b;">'Content-Length'</span><span style="color: black;">&#93;</span> = <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">getsize</span><span style="color: black;">&#40;</span>filename_path<span style="color: black;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; response.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #008000;">open</span><span style="color: black;">&#40;</span>filename_path<span style="color: black;">&#41;</span>.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> response </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>The problem: If the download of a file exceeded 5 minutes (big files and/or low bandwidth) the download was canceled on the server side by a timeout. This Apache configuration for mod_fcgid solved the problem (see <a href="http://fastcgi.coremail.cn/doc.htm">mod_fcgid documentation for BusyTimeout</a>)</p>
<div class="igBar"><span id="lcode-16"><a href="#" onclick="javascript:showPlainTxt('code-16'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-16">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;IfModule mod_fcgid.<span style="">c</span>&gt;</div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; BusyTimeout <span style="color:#800000;color:#800000;">1200</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;/IfModule&gt; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>The problem was that the apache module scanned every minute for processes that run for more than BusyTimeout seconds. These processes are potentially in bad health (infinite loop et al.) and have to be killed. Not so with my processes (since I know what I'm doing..). The setting of the busy timeout to 1200 seconds now lets my processes run for a maximum of one hour.</p>
<p>As this setting can't by overwritten in a htaccess file by default I needed to bug <a href="http://www.citrin.ch/">my web hosting provider</a> with the request, which was handled in 24 hours, so thanks for that one!</p>
<p>PS: If you know of another way how to serve protected static files via a single sign on (no HTTP basic auth), please let me know.</p>
]]></content:encoded>
			<wfw:commentRss>http://code.pui.ch/2007/10/03/django-serve-big-files-via-fcgid/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Python: Sort a list of dicts by dict-key</title>
		<link>http://code.pui.ch/2007/07/23/python-sort-a-list-of-dicts-by-dict-key/</link>
		<comments>http://code.pui.ch/2007/07/23/python-sort-a-list-of-dicts-by-dict-key/#comments</comments>
		<pubDate>Mon, 23 Jul 2007 13:52:29 +0000</pubDate>
		<dc:creator>philipp.keller</dc:creator>
				<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://code.pui.ch/2007/07/23/python-sort-a-list-of-dicts-by-dict-key/</guid>
		<description><![CDATA[I always forget that one and end up searching for half an hour:
PLAIN TEXT
PYTHON:




&#62;&#62;&#62; list = &#91; dict&#40;a=1,b=2,c=3&#41;,


...&#160; &#160;dict&#40;a=2,b=2,c=2&#41;,


...&#160; &#160;dict&#40;a=3,b=2,c=1&#41;&#93;


&#62;&#62;&#62; list.sort&#40;key=operator.itemgetter&#40;'c'&#41;&#41;


&#62;&#62;&#62; list


&#91;&#123;'a': 3, 'c': 1, 'b': 2&#125;, &#123;'a': 2, 'c': 2, 'b': 2&#125;, &#123;'a': 1, 'c': 3, 'b': 2&#125;&#93;


&#62;&#62;&#62; list.sort&#40;key=operator.itemgetter&#40;'a'&#41;&#41;


&#62;&#62;&#62; list


&#91;&#123;'a': 1, 'c': 3, 'b': 2&#125;, &#123;'a': 2, 'c': 2, 'b': 2&#125;, &#123;'a': 3, 'c': [...]]]></description>
			<content:encoded><![CDATA[<p>I always forget that one and end up searching for half an hour:</p>
<div class="igBar"><span id="lpython-18"><a href="#" onclick="javascript:showPlainTxt('python-18'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PYTHON:</span>
<div id="python-18">
<div class="python">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&gt;&gt;&gt; <span style="color: #008000;">list</span> = <span style="color: black;">&#91;</span> <span style="color: #008000;">dict</span><span style="color: black;">&#40;</span>a=<span style="color: #ff4500;color:#800000;">1</span>,b=<span style="color: #ff4500;color:#800000;">2</span>,c=<span style="color: #ff4500;color:#800000;">3</span><span style="color: black;">&#41;</span>,</div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">...&nbsp; &nbsp;<span style="color: #008000;">dict</span><span style="color: black;">&#40;</span>a=<span style="color: #ff4500;color:#800000;">2</span>,b=<span style="color: #ff4500;color:#800000;">2</span>,c=<span style="color: #ff4500;color:#800000;">2</span><span style="color: black;">&#41;</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">...&nbsp; &nbsp;<span style="color: #008000;">dict</span><span style="color: black;">&#40;</span>a=<span style="color: #ff4500;color:#800000;">3</span>,b=<span style="color: #ff4500;color:#800000;">2</span>,c=<span style="color: #ff4500;color:#800000;">1</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&gt;&gt;&gt; <span style="color: #008000;">list</span>.<span style="color: black;">sort</span><span style="color: black;">&#40;</span>key=<span style="color: #dc143c;">operator</span>.<span style="color: black;">itemgetter</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'c'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&gt;&gt;&gt; <span style="color: #008000;">list</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: black;">&#91;</span><span style="color: black;">&#123;</span><span style="color: #483d8b;">'a'</span>: <span style="color: #ff4500;color:#800000;">3</span>, <span style="color: #483d8b;">'c'</span>: <span style="color: #ff4500;color:#800000;">1</span>, <span style="color: #483d8b;">'b'</span>: <span style="color: #ff4500;color:#800000;">2</span><span style="color: black;">&#125;</span>, <span style="color: black;">&#123;</span><span style="color: #483d8b;">'a'</span>: <span style="color: #ff4500;color:#800000;">2</span>, <span style="color: #483d8b;">'c'</span>: <span style="color: #ff4500;color:#800000;">2</span>, <span style="color: #483d8b;">'b'</span>: <span style="color: #ff4500;color:#800000;">2</span><span style="color: black;">&#125;</span>, <span style="color: black;">&#123;</span><span style="color: #483d8b;">'a'</span>: <span style="color: #ff4500;color:#800000;">1</span>, <span style="color: #483d8b;">'c'</span>: <span style="color: #ff4500;color:#800000;">3</span>, <span style="color: #483d8b;">'b'</span>: <span style="color: #ff4500;color:#800000;">2</span><span style="color: black;">&#125;</span><span style="color: black;">&#93;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&gt;&gt;&gt; <span style="color: #008000;">list</span>.<span style="color: black;">sort</span><span style="color: black;">&#40;</span>key=<span style="color: #dc143c;">operator</span>.<span style="color: black;">itemgetter</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'a'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&gt;&gt;&gt; <span style="color: #008000;">list</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: black;">&#91;</span><span style="color: black;">&#123;</span><span style="color: #483d8b;">'a'</span>: <span style="color: #ff4500;color:#800000;">1</span>, <span style="color: #483d8b;">'c'</span>: <span style="color: #ff4500;color:#800000;">3</span>, <span style="color: #483d8b;">'b'</span>: <span style="color: #ff4500;color:#800000;">2</span><span style="color: black;">&#125;</span>, <span style="color: black;">&#123;</span><span style="color: #483d8b;">'a'</span>: <span style="color: #ff4500;color:#800000;">2</span>, <span style="color: #483d8b;">'c'</span>: <span style="color: #ff4500;color:#800000;">2</span>, <span style="color: #483d8b;">'b'</span>: <span style="color: #ff4500;color:#800000;">2</span><span style="color: black;">&#125;</span>, <span style="color: black;">&#123;</span><span style="color: #483d8b;">'a'</span>: <span style="color: #ff4500;color:#800000;">3</span>, <span style="color: #483d8b;">'c'</span>: <span style="color: #ff4500;color:#800000;">1</span>, <span style="color: #483d8b;">'b'</span>: <span style="color: #ff4500;color:#800000;">2</span><span style="color: black;">&#125;</span><span style="color: black;">&#93;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://code.pui.ch/2007/07/23/python-sort-a-list-of-dicts-by-dict-key/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Python: Find out cpu time of a certain process</title>
		<link>http://code.pui.ch/2007/06/08/python-find-out-cpu-time-of-a-certain-process/</link>
		<comments>http://code.pui.ch/2007/06/08/python-find-out-cpu-time-of-a-certain-process/#comments</comments>
		<pubDate>Fri, 08 Jun 2007 09:52:52 +0000</pubDate>
		<dc:creator>philipp.keller</dc:creator>
				<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://code.pui.ch/2007/06/08/python-find-out-cpu-time-of-a-certain-process/</guid>
		<description><![CDATA[To find out how many percentage a certain process uses the cpu:
PLAIN TEXT
PYTHON:




import os, time


&#160;


# find out the pid by username. 


# &#34;-o pid h&#34; omits the header and just prints the pid


pid = os.popen&#40;'ps -U my_user_name -o pid h'&#41;.read&#40;&#41;.strip&#40;&#41;


&#160;


# 14th column is utime, 15th column is stime: 


# The time the process has been [...]]]></description>
			<content:encoded><![CDATA[<p>To find out how many percentage a certain process uses the cpu:</p>
<div class="igBar"><span id="lpython-20"><a href="#" onclick="javascript:showPlainTxt('python-20'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PYTHON:</span>
<div id="python-20">
<div class="python">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>, <span style="color: #dc143c;">time</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;"># find out the pid by username. </span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;"># &quot;-o pid h&quot; omits the header and just prints the pid</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">pid = <span style="color: #dc143c;">os</span>.<span style="color: black;">popen</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'ps -U my_user_name -o pid h'</span><span style="color: black;">&#41;</span>.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;"># 14th column is utime, 15th column is stime: </span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;"># The time the process has been scheduled in user/kernel mode</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;"># The time value is in jiffies. One jiffie is appox 1/100 second</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;"># see man proc for more info</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #dc143c;">stat</span> = <span style="color: #dc143c;">os</span>.<span style="color: black;">popen</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'cat /proc/%s/stat'</span> % pid<span style="color: black;">&#41;</span>.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">cpu_time1=<span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">stat</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;color:#800000;">14</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> + <span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">stat</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;color:#800000;">15</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">time1=<span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #dc143c;">time</span>.<span style="color: black;">sleep</span><span style="color: black;">&#40;</span><span style="color: #ff4500;color:#800000;">1</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #dc143c;">stat</span> = <span style="color: #dc143c;">os</span>.<span style="color: black;">popen</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'cat /proc/%s/stat'</span> % pid<span style="color: black;">&#41;</span>.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">cpu_time2=<span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">stat</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;color:#800000;">14</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> + <span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">stat</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;color:#800000;">15</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">time2=<span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span><span style="color: #008000;">float</span><span style="color: black;">&#40;</span>cpu_time2 - cpu_time1<span style="color: black;">&#41;</span> / <span style="color: black;">&#40;</span>time2 - time1<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">"%"</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>I don't know though if the number is accurate.<br />
What is "cpu time" anyway? It's the time the process is running (using the cpu for 100%) divided by the time the process is laid asleep by the scheduler.<br />
Then, <a href="http://www.ecos.sourceware.org/ml/systemtap/2005-q4/msg00185.html">jiffies seem to be not a safe number</a> for time measurements.<br />
But for relative measurements it should do the trick.</p>
]]></content:encoded>
			<wfw:commentRss>http://code.pui.ch/2007/06/08/python-find-out-cpu-time-of-a-certain-process/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>add bandwidth to a file download in python</title>
		<link>http://code.pui.ch/2007/02/25/add-bandwidth-to-a-file-download-in-python/</link>
		<comments>http://code.pui.ch/2007/02/25/add-bandwidth-to-a-file-download-in-python/#comments</comments>
		<pubDate>Sun, 25 Feb 2007 01:51:05 +0000</pubDate>
		<dc:creator>philipp.keller</dc:creator>
				<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://code.pui.ch/2007/02/25/add-bandwidth-to-a-file-download-in-python/</guid>
		<description><![CDATA[Another short code snippet I came up when writing the module download script
The following class keeps a download under a certain ratio and prints live download stats (KB/s).

PLAIN TEXT
PYTHON:




class BandwidthFile:


&#160; &#160; """


&#160; &#160; limits the file download to a certain amount of bytes per second and prints download stats


&#160; &#160; """


&#160; &#160; def __init__&#40;self, filename, [...]]]></description>
			<content:encoded><![CDATA[<p>Another short code snippet I came up when writing the <a href="http://code.pui.ch/2007/02/18/turn-demoscene-modules-into-mp3s/">module download script</a></p>
<p>The following class keeps a download under a certain ratio and prints live download stats (KB/s).<br />
<span id="more-8"></span></p>
<div class="igBar"><span id="lpython-23"><a href="#" onclick="javascript:showPlainTxt('python-23'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PYTHON:</span>
<div id="python-23">
<div class="python">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #ff7700;font-weight:bold;">class</span> BandwidthFile:</div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #483d8b;">""</span><span style="color: #483d8b;">"</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #483d8b;">&nbsp; &nbsp; limits the file download to a certain amount of bytes per second and prints download stats</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #483d8b;">&nbsp; &nbsp; "</span><span style="color: #483d8b;">""</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, filename, filesize, bytes_per_second = <span style="color: #ff4500;color:#800000;">20000</span>, mode = <span style="color: #483d8b;">"wb"</span><span style="color: black;">&#41;</span>:</div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">self</span>.<span style="color: black;">fp</span> = <span style="color: #008000;">open</span><span style="color: black;">&#40;</span>filename, <span style="color: #483d8b;">"wb"</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">self</span>.<span style="color: black;">bytecounter</span> = <span style="color: #ff4500;color:#800000;">0</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">self</span>.<span style="color: black;">started</span> = <span style="color: #dc143c;">datetime</span>.<span style="color: #dc143c;">datetime</span>.<span style="color: black;">now</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">self</span>.<span style="color: black;">bytes_per_second</span> = bytes_per_second</div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">self</span>.<span style="color: black;">filesize</span> = filesize</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">self</span>.<span style="color: black;">filename</span> = filename</div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__del__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:</div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">self</span>.<span style="color: black;">fp</span>.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">def</span> write<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, bytestring<span style="color: black;">&#41;</span>:</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">self</span>.<span style="color: black;">bytecounter</span> += <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>bytestring<span style="color: black;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; seconds_since_started = <span style="color: black;">&#40;</span><span style="color: #dc143c;">datetime</span>.<span style="color: #dc143c;">datetime</span>.<span style="color: black;">now</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> - <span style="color: #008000;">self</span>.<span style="color: black;">started</span><span style="color: black;">&#41;</span>.<span style="color: black;">seconds</span> + <span style="color: #ff4500;color:#800000;">1</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">bytecounter</span> / seconds_since_started<span style="color: black;">&#41;</span>&gt; <span style="color: #008000;">self</span>.<span style="color: black;">bytes_per_second</span>:</div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; seconds_since_started = <span style="color: black;">&#40;</span><span style="color: #dc143c;">datetime</span>.<span style="color: #dc143c;">datetime</span>.<span style="color: black;">now</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> - <span style="color: #008000;">self</span>.<span style="color: black;">started</span><span style="color: black;">&#41;</span>.<span style="color: black;">seconds</span> + <span style="color: #ff4500;color:#800000;">1</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #dc143c;">time</span>.<span style="color: black;">sleep</span><span style="color: black;">&#40;</span><span style="color: #ff4500;color:#800000;">0</span>.<span style="color: #ff4500;color:#800000;">1</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">self</span>.<span style="color: black;">fp</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span>bytestring<span style="color: black;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <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;">"<span style="color: #000099; font-weight: bold;">\r</span>downloading %s (%i/%i KB, %i KB/s)"</span> % <span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">filename</span>, <span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">bytecounter</span> / <span style="color: #ff4500;color:#800000;">1024</span><span style="color: black;">&#41;</span>, <span style="color: black;">&#40;</span><span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">filesize</span><span style="color: black;">&#41;</span> / <span style="color: #ff4500;color:#800000;">1024</span><span style="color: black;">&#41;</span>, <span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">bytecounter</span> /seconds_since_started / <span style="color: #ff4500;color:#800000;">1024</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #dc143c;">sys</span>.<span style="color: black;">stdout</span>.<span style="color: black;">flush</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>To use the class with ftp:</p>
<div class="igBar"><span id="lpython-24"><a href="#" onclick="javascript:showPlainTxt('python-24'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PYTHON:</span>
<div id="python-24">
<div class="python">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">ftplib</span> <span style="color: #ff7700;font-weight:bold;">import</span> FTP</div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">ftp = FTP<span style="color: black;">&#40;</span><span style="color: #483d8b;">"ftp.modland.com"</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">filesize = ftp.<span style="color: black;">size</span><span style="color: black;">&#40;</span>remote_filename<span style="color: black;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ;background-color: #ffd;"">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">f = BandwidthFile<span style="color: black;">&#40;</span>local_filename, ftp_filesize<span style="color: black;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ;background-color: #ffd;"">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">ftp.<span style="color: black;">retrbinary</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'RETR '</span>+remote_filename, f.<span style="color: black;">write</span><span style="color: black;">&#41;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>The status line looks like this:</p>
<pre>
downloading /home/phred/modules/Skaven/beyond the network.it (95/4411 KB, 19 KB/s)
</pre>
<p>If you have suggestions how to enhance the code snippet feel free to leave a comment!</p>
]]></content:encoded>
			<wfw:commentRss>http://code.pui.ch/2007/02/25/add-bandwidth-to-a-file-download-in-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>set timeout for a shell command in python</title>
		<link>http://code.pui.ch/2007/02/19/set-timeout-for-a-shell-command-in-python/</link>
		<comments>http://code.pui.ch/2007/02/19/set-timeout-for-a-shell-command-in-python/#comments</comments>
		<pubDate>Sun, 18 Feb 2007 23:04:06 +0000</pubDate>
		<dc:creator>philipp.keller</dc:creator>
				<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://code.pui.ch/2007/02/19/set-timeout-for-a-shell-command-in-python/</guid>
		<description><![CDATA[I wanted to run a shell command in python without knowing if the shell command is going to exit within reasonable time (adplay that was, sometimes it simply hangs).
Update: the "task" module of Rob Hooft seems to solve this exact problem. At the time I wrote this, the python.net website was down. I leave my [...]]]></description>
			<content:encoded><![CDATA[<p>I wanted to run a shell command in python without knowing if the shell command is going to exit within reasonable time (<a href="http://adplug.sourceforge.net/">adplay</a> that was, sometimes it simply hangs).</p>
<p><strong>Update:</strong> <a href="http://www.python.net/crew/hooft/">the "task" module of Rob Hooft</a> seems to solve this exact problem. At the time I wrote this, the python.net website was down. I leave my solution here just for archive purpose.</p>
<p><span id="more-7"></span></p>
<div class="igBar"><span id="lpython-27"><a href="#" onclick="javascript:showPlainTxt('python-27'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PYTHON:</span>
<div id="python-27">
<div class="python">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #ff7700;font-weight:bold;">def</span> timeout_command<span style="color: black;">&#40;</span>command, timeout<span style="color: black;">&#41;</span>:</div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #483d8b;">""</span><span style="color: #483d8b;">"call shell-command and either return its output or kill it</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #483d8b;">&nbsp; &nbsp; if it doesn't normally exit within timeout seconds and return None"</span><span style="color: #483d8b;">""</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">subprocess</span>, <span style="color: #dc143c;">datetime</span>, <span style="color: #dc143c;">os</span>, <span style="color: #dc143c;">time</span>, <span style="color: #dc143c;">signal</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; start = <span style="color: #dc143c;">datetime</span>.<span style="color: #dc143c;">datetime</span>.<span style="color: black;">now</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ;background-color: #ffd;"">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; process = <span style="color: #dc143c;">subprocess</span>.<span style="color: black;">Popen</span><span style="color: black;">&#40;</span>command, stdout=<span style="color: #dc143c;">subprocess</span>.<span style="color: black;">PIPE</span>, stderr=<span style="color: #dc143c;">subprocess</span>.<span style="color: black;">PIPE</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">while</span> process.<span style="color: black;">poll</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #008000;">None</span>:</div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #dc143c;">time</span>.<span style="color: black;">sleep</span><span style="color: black;">&#40;</span><span style="color: #ff4500;color:#800000;">0</span>.<span style="color: #ff4500;color:#800000;">1</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; now = <span style="color: #dc143c;">datetime</span>.<span style="color: #dc143c;">datetime</span>.<span style="color: black;">now</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>now - start<span style="color: black;">&#41;</span>.<span style="color: black;">seconds</span>&gt; timeout:</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ;background-color: #ffd;"">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #dc143c;">os</span>.<span style="color: black;">kill</span><span style="color: black;">&#40;</span>process.<span style="color: black;">pid</span>, <span style="color: #dc143c;">signal</span>.<span style="color: black;">SIGKILL</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ;background-color: #ffd;"">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #dc143c;">os</span>.<span style="color: black;">waitpid</span><span style="color: black;">&#40;</span>-<span style="color: #ff4500;color:#800000;">1</span>, <span style="color: #dc143c;">os</span>.<span style="color: black;">WNOHANG</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">None</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> process.<span style="color: black;">stdout</span>.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<div class="igBar"><span id="lpython-28"><a href="#" onclick="javascript:showPlainTxt('python-28'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PYTHON:</span>
<div id="python-28">
<div class="python">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&gt;&gt;&gt; output = timeout_command<span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">"sleep"</span>, <span style="color: #483d8b;">"10"</span><span style="color: black;">&#93;</span>, <span style="color: #ff4500;color:#800000;">2</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #008000;">None</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&gt;&gt;&gt; output = timeout_command<span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">"sleep"</span>, <span style="color: #483d8b;">"1"</span><span style="color: black;">&#93;</span>, <span style="color: #ff4500;color:#800000;">2</span><span style="color: black;">&#41;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>The process can be killed when it has run for too long (the <code>os.waitpid</code> waits for the kill to end and avoids defunct-processes) and furthermore the Popen'ed process' printed is caught and returned if it doesn't timeout. However, <code>subprocess.Popen</code> is called with a list as argument. That means, that the command isn't passed to a shell and furthermore you can just call one command with options, nothing more.</p>
]]></content:encoded>
			<wfw:commentRss>http://code.pui.ch/2007/02/19/set-timeout-for-a-shell-command-in-python/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Turn demoscene modules into mp3s</title>
		<link>http://code.pui.ch/2007/02/18/turn-demoscene-modules-into-mp3s/</link>
		<comments>http://code.pui.ch/2007/02/18/turn-demoscene-modules-into-mp3s/#comments</comments>
		<pubDate>Sun, 18 Feb 2007 17:39:11 +0000</pubDate>
		<dc:creator>philipp.keller</dc:creator>
				<category><![CDATA[demoscene]]></category>
		<category><![CDATA[modules]]></category>
		<category><![CDATA[music]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://code.pui.ch/2007/02/18/turn-demoscene-modules-into-mp3s/</guid>
		<description><![CDATA[
tunes of purple motion turned into mp3s are playing in itunes
If you were part of the demoscene in your former life, if you were and still are fond of modules (those sound files with the mod, xm, s3m, it, ... ending), if you are a linux user and if you still want to listen to [...]]]></description>
			<content:encoded><![CDATA[<div style="float: right; margin-left: 2em"><img src="/wp-content/upload/itunes_purple_motion.png" alt="iTunes screenshot of purple motions tracks"/><br />
tunes of purple motion turned into mp3s are playing in itunes</div>
<p>If you were part of the demoscene in your former life, if you were and still are fond of modules (those sound files with the mod, xm, s3m, it, ... ending), if you are a linux user and if you still want to listen to this music on your computer without doing all the tweaks of installing (or even compiling) music player plugins for itunes or amarok or if you simply want to listen to Purple Motions tunes on your mp3 player then this little tutorial is for you. If not, then you won't have read that far anyway..</p>
<div style="clear: right">&nbsp;</div>
<p><span id="more-6"></span></p>
<h2>short version for the impatient</h2>
<p>download <a href="/wp-content/upload/downloadmod.py">downloadmod.py</a> and <a href="/wp-content/upload/mod2mp3.py">mod2mp3.py</a> into /usr/local/bin/</p>
<div class="igBar"><span id="lcode-30"><a href="#" onclick="javascript:showPlainTxt('code-30'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-30">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">sudo apt-get install xmp adplay unrar lame</div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">mkdir ~/modules/</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">downloadmod.<span style="">py</span> ~/modules/ <span style="color:#CC0000;">"Purple Motion"</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">mod2mp3.<span style="">py</span> ~/modules/ </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<h3>First, get those modules</h3>
<p>The first task is to get those modules onto your computer (as you most certainly deleted them - either per accident or when you were in needed of some space on your hard drive back when hard drives where small and expensive)</p>
<p>Afaik the most complete module resource is <a href="ftp://ftp.modland.com">modland</a>. The crux is that it's just an ftp site with deep directory structure and without a search facility. All it has got is a complete list of all the modules in a RAR file (which is kept up to date by a cron job) that holds a text file with all the available modules and their path.</p>
<p>To download all modules of a certain artist, I wrote a little <a href="/wp-content/upload/downloadmod.py">python script</a> that downloads the module list, unrars it, caches it for further search requests, searches for the artist and downloads all modules of that particular artist.</p>
<p><code>downloadmod.py ~/modules/ &quot;Michael Land&quot;</code> downloads all Adlib songs of Michael Land and places them into ~/modules/Michael Land/ and writes some artist/album meta information into those newly created directories.</p>
<p>Alternatively you can adapt a quick and dirty <a href="/wp-content/upload/downloadmod.sh">bash function</a>. It uses wget and is quite messy but it should do the task as well. You have to download and unpack the module list before you call the shell function.</p>
<h3>Then, convert those modules into mp3 files</h3>
<p>To convert the modules into mp3 files you usually take a module player and call with some special parameters in order to &quot;play&quot; the module into a wav-File and convert that wav into an mp3 file.</p>
<p>On linux, I'd suggest to install this module players (both exist as debian packages and work without hassle on my ubuntu installation)</p>
<ul>
<li><a href="http://xmp.sourceforge.net/">xmp</a> is *nix &quot;native&quot;</li>
<li><a href="http://adplug.sourceforge.net/">adplay</a> supports many adlib formats</li>
</ul>
<p>I wrote a <a href="/wp-content/upload/mod2mp3.py">python script</a> that converts the downloaded modules into mp3s using the just mentioned module players. You need at least xmp or adplay. If you install both, you'd be able to play about 90% of all modules.</p>
<p>Just call <code>mod2mp3.py</code> with your root module directory as argument and the script creates a mp3 file for each module that is convertible and isn't already converted. If you downloaded the modules with my download script, this script will use the meta information of the first script to write the id3 tags of the mp3s.</p>
<p>If everything worked, you now should have mp3 files with correct id3 tags that can be imported into your amarok/itunes library.</p>
<h3>What about us windows users?</h3>
<p>Sorry guys. I've got to let you down. One whole evening I tried to get any module player writing it's output to a wave file from the command line. I tried <a href="http://www.modplug.com/playerinfo.html">modplug</a> and <a href="http://www.winamp.com">Winamp</a> with a <a href="http://www.winamp.com/plugins/details.php?id=132367">module player plugin</a> but without luck. If you succeed, let me know. Up to then, just take a linux box, copy/download all modules there and let the box convert your modules over night and import it back into your iTunes player. That's what I did.</p>
]]></content:encoded>
			<wfw:commentRss>http://code.pui.ch/2007/02/18/turn-demoscene-modules-into-mp3s/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
