<?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>ShowMeDo Blogs the World &#187; Python tips and tricks</title>
	<atom:link href="http://blog.showmedo.com/category/python-tips-and-tricks/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.showmedo.com</link>
	<description>Building the site, promoting the videos, sharing the knowledge</description>
	<lastBuildDate>Tue, 03 Aug 2010 14:57:04 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using Python  strings and dictionaries to create video embedding templates</title>
		<link>http://blog.showmedo.com/showmedo-front-page/using-python-strings-and-dictionaries-to-create-video-embedding-templates/</link>
		<comments>http://blog.showmedo.com/showmedo-front-page/using-python-strings-and-dictionaries-to-create-video-embedding-templates/#comments</comments>
		<pubDate>Sat, 27 Feb 2010 13:31:02 +0000</pubDate>
		<dc:creator>wizzy</dc:creator>
				<category><![CDATA[Building a Website]]></category>
		<category><![CDATA[Python tips and tricks]]></category>
		<category><![CDATA[Showmedo Front-page]]></category>

		<guid isPermaLink="false">http://blog.showmedo.com/showmedo-front-page/using-python-strings-and-dictionaries-to-create-video-embedding-templates/</guid>
		<description><![CDATA[<p>All Pythonistas will recognize the % operator used to format strings:</p>
print "%s world from %s!"%('hello', 'showmedo')
<p>This is similar to its &#8216;C&#8217; antecedent but, as you would expect and probably know, Python allows containers other than tuples (in the case containing the &#8216;hello&#8217; and &#8217;showmedo&#8217; strings) to be used in the string formatting operation.</p>
<p>The use of [...]


Related posts:<ol><li><a href='http://blog.showmedo.com/python-showmedos/testing-flowplayer-embedding-script/' rel='bookmark' title='Permanent Link: Testing Flowplayer embedding script'>Testing Flowplayer embedding script</a></li>
<li><a href='http://blog.showmedo.com/news/new-scribus-video-published/' rel='bookmark' title='Permanent Link: New Scribus Video Published'>New Scribus Video Published</a></li>
<li><a href='http://blog.showmedo.com/news/congratulations-for-dais-40th-scribus-video/' rel='bookmark' title='Permanent Link: Congratulations for Dai&#8217;s 40th Scribus Video!'>Congratulations for Dai&#8217;s 40th Scribus Video!</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>All Pythonistas will recognize the % operator used to format strings:</p>
<pre>print "%s world from %s!"%('hello', 'showmedo')</pre>
<p>This is similar to its &#8216;C&#8217; antecedent but, as you would expect and probably know, Python allows containers other than tuples (in the case containing the &#8216;hello&#8217; and &#8217;showmedo&#8217; strings) to be used in the string formatting operation.</p>
<p>The use of a dictionary rather than tuple to format the string provides a very handy little templating mechanism, used recently in the addition of video embedding strings to the site.</p>
<p>We start with our text-file template, one long string with no pesky line-breaks:</p>
<pre>&lt;object width="%(width)d" height="%(height)d" id="%(id)s" data="http://showmedo.com/static/flowplayer/flowplayer-3.1.5.swf" type="application/x-shockwave-flash"&gt;&lt;param name="m    ovie" value="http://showmedo.com/static/flowplayer/flowplayer-3.1.5.swf" /&gt;&lt;param name="allowfullscreen" value="true" /&gt;&lt;param name="allowscriptaccess" value="always" /&gt;&lt;param  [...]   &amp;quot;%(url)s&amp;quot;,&amp;quot;title&amp;quot;:&amp;quot;%(title)s&amp;quot;,&amp;quot;baseUrl&amp;quot;:&amp;quot;http://showmedo.com&amp;quot;,&amp;quot;autoPlay&amp;quot;:false,&amp;quot;autoBuffering&amp;quot;:true}],    &amp;quot;plugins&amp;quot;:{&amp;quot;controls&amp;quot;:{&amp;quot;url&amp;quot;:&amp;quot;http://showmedo.com/static/flowplayer/flowplayer.controls-3.1.5.swf&amp;quot;,&amp;quot;playlist&amp;quot;:true}}}' /&gt;&lt;/object&gt;&lt;br /&gt;</pre>
<p>Note the variables scattered through the string starting with %(width)d. We&#8217;ll be using these, together with a Python dictionary, to create our video-specific embedding string. The method that does the legwork is shown below. It is a member of the &#8216;Video&#8217; class and in this context &#8217;self&#8217; refers to the video instance in question:</p>
<pre>
def get_embed_string(self, width=425, height=344):
    t_dir = os.path.join(os.path.dirname(__file__),'templates/')
    embed_template = open(t_dir + 'embed_video.txt').read()
    embed_str = embed_template%dict(
                                  width=width,
                                  height=height,
                                  url=movieDirectory() + "%s"%self.get_flv_file_name()
                                  title=self.title,
                                  id='_%d'%self.id
                                  )
    return embed_str</pre>
<p>We open the &#8216;embed_video.txt&#8217; file (using a little Python os magic to establish a relative directory path) and read its contents, storing them in the &#8216;embed_template&#8217; string. We then use the &#8216;%&#8217; operator to assign values to the special (of the form %(foo)x) variables in the template, using a Python dictionary.</p>
<p>The resulting &#8216;embed_str&#8217; string defines a flash
<div class="youtube-video"><object> which can be used to embed Showmedo videos in blogs, articles and the like:</p>
<pre>
&lt;object width=&quot;425&quot; height=&quot;344&quot; id=&quot;_672&quot; data=&quot;http://showmedo.com/static/flowplayer/flowplayer-3.1.5.swf&quot; type=&quot;application/x-shockwave-flash&quot;&gt;&lt;param name=&quot;movie&quot; value=&quot;http://showmedo.com/static/flowplayer/flowplayer-3.1.5.swf&quot; /&gt;&lt;param name=&quot;allowfullscreen&quot; value=&quot;true&quot; /&gt;&lt;param name=&quot;allowscriptaccess&quot; value=&quot;always&quot; /&gt;&lt;param name=&quot;flashvars&quot; value='config={&amp;quot;key&amp;quot;:&amp;quot;#$824e5316466b69d76dc&amp;quot;,&amp;quot;logo&amp;quot;:{&amp;quot;url&amp;quot;:&amp;quot;http://showmedo.com/static/images/showmedo_logo_vp.png&amp;quot;,&amp;quot;fullscreenOnly&amp;quot;:false,&amp;quot;top&amp;quot;:20,&amp;quot;right&amp;quot;:20,&amp;quot;opacity&amp;quot;:0.5,&amp;quot;displayTime&amp;quot;:0,&amp;quot;linkUrl&amp;quot;:&amp;quot;http://showmedo.com&amp;quot;},&amp;quot;clip&amp;quot;:{&amp;quot;baseUrl&amp;quot;:&amp;quot;http://showmedo.com&amp;quot;,&amp;quot;autoPlay&amp;quot;:false,&amp;quot;autoBuffering&amp;quot;:true},&amp;quot;playlist&amp;quot;:[{&amp;quot;url&amp;quot;:&amp;quot;http://showmedovideos4.com/ShowMeDos/2450010.flv&amp;quot;,&amp;quot;title&amp;quot;:&amp;quot;Scientific Computing Using SAGE: Introduction&amp;quot;,&amp;quot;baseUrl&amp;quot;:&amp;quot;http://showmedo.com&amp;quot;,&amp;quot;autoPlay&amp;quot;:false,&amp;quot;autoBuffering&amp;quot;:true}],&amp;quot;plugins&amp;quot;:{&amp;quot;controls&amp;quot;:{&amp;quot;url&amp;quot;:&amp;quot;http://showmedo.com/static/flowplayer/flowplayer.controls-3.1.5.swf&amp;quot;,&amp;quot;playlist&amp;quot;:true}}}' /&gt;&lt;/object&gt;
</pre>
<div class="zemanta-pixie"><img class="zemanta-pixie-img" alt="" src="http://img.zemanta.com/pixy.gif?x-id=3597d4cb-7343-8165-8acc-df1eef48b8d4" /></div>
<p></object></div>


<p>Related posts:<ol><li><a href='http://blog.showmedo.com/python-showmedos/testing-flowplayer-embedding-script/' rel='bookmark' title='Permanent Link: Testing Flowplayer embedding script'>Testing Flowplayer embedding script</a></li>
<li><a href='http://blog.showmedo.com/news/new-scribus-video-published/' rel='bookmark' title='Permanent Link: New Scribus Video Published'>New Scribus Video Published</a></li>
<li><a href='http://blog.showmedo.com/news/congratulations-for-dais-40th-scribus-video/' rel='bookmark' title='Permanent Link: Congratulations for Dai&#8217;s 40th Scribus Video!'>Congratulations for Dai&#8217;s 40th Scribus Video!</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.showmedo.com/showmedo-front-page/using-python-strings-and-dictionaries-to-create-video-embedding-templates/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python to the Rescue</title>
		<link>http://blog.showmedo.com/showmedo-front-page/python-to-the-rescue/</link>
		<comments>http://blog.showmedo.com/showmedo-front-page/python-to-the-rescue/#comments</comments>
		<pubDate>Thu, 25 Feb 2010 14:59:26 +0000</pubDate>
		<dc:creator>wizzy</dc:creator>
				<category><![CDATA[Building a Website]]></category>
		<category><![CDATA[Kyran's thought for the day]]></category>
		<category><![CDATA[Python tips and tricks]]></category>
		<category><![CDATA[Showmedo Front-page]]></category>

		<guid isPermaLink="false">http://blog.showmedo.com/showmedo-front-page/python-to-the-rescue/</guid>
		<description><![CDATA[<p>Showmedo caught some flak from the recent Westhost (our main server-provider) troubles. Catch this pretty interesting thread for the gory details. We were actually pretty lucky as the main site remained standing (not the case for many others) but did manage to lose our cron daemon. As a result some restart scripts which check on [...]


Related posts:<ol><li><a href='http://blog.showmedo.com/showmedo-front-page/a-little-downtime/' rel='bookmark' title='Permanent Link: A Little Downtime :('>A Little Downtime :(</a></li>
<li><a href='http://blog.showmedo.com/news/using-non-python-files-with-py2exe/' rel='bookmark' title='Permanent Link: Using non-Python files with py2exe'>Using non-Python files with py2exe</a></li>
<li><a href='http://blog.showmedo.com/news/optimising-genshi-imports/' rel='bookmark' title='Permanent Link: Optimising Genshi Imports'>Optimising Genshi Imports</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Showmedo caught some flak from the recent Westhost (our main server-provider) troubles. Catch <a href="http://www.webhostingtalk.com/showthread.php?t=358323">this</a> pretty interesting thread for the gory details. We were actually pretty lucky as the main site remained standing (not the case for many others) but did manage to lose our <a href="http://en.wikipedia.org/wiki/Cron">cron daemon</a>. As a result some restart scripts which check on the health of, among other things, the Python <a href="http://turbogears.org/">Turbogears</a> process running the site died. Now this script isn&#8217;t needed too often &lt;fingers crossed&gt; but if the site falls down and we&#8217;re literally asleep then it avoids more than a minute or sos downtime. The crontab line in question looks like this:</p>
<pre>*/1 * * * * source /.bashrc &#038;&#038; /usr/local/bin/python2.5 /home/showmedo/showmedo/cron_start_showmedo.py &gt; /var/log/cron_log</pre>
<p>This line tells cron to run cron_start_showmedo.py every minute and the python process will check the health of Showmedo&#8217;s main process, our memcache and mysql servers and a few other things and restart them if anything has fallen over. Having lost cron, I needed a quick hack to perform these duties and found some very cool advice <a href="http://stackoverflow.com/questions/696839/how-do-i-write-a-bash-script-to-restart-a-process-if-it-dies">here</a> at Slashdot. For various reasons the recommended bash-scripts seemed to be dying on me, and lacking the time or will to try debugging on fairly foreign turf, I hacked a little Python substitute to do much the same:</p>
<pre>
  1 import time
  2 import subprocess
  3
  4 while 1:
  5     subprocess.Popen(['/usr/local/bin/python2.5', '/home/showmedo/showmedo/cron_start_showmedo.py'])
  6     time.sleep(60)
</pre>
<p>Not pretty, but at five lines quick and succinct. The line 4 while loop sets things running in perpetuity, line 5 uses Python&#8217;s newish (and pretty powerful) subprocess module call the cron_start_showmedo.py module and line 6 takes a 60 second break before continuing the loop.</p>
<p>The script sat duty while I waited for our cron-daemon to return and I slept sounder in my bed as a result.</p>


<p>Related posts:<ol><li><a href='http://blog.showmedo.com/showmedo-front-page/a-little-downtime/' rel='bookmark' title='Permanent Link: A Little Downtime :('>A Little Downtime :(</a></li>
<li><a href='http://blog.showmedo.com/news/using-non-python-files-with-py2exe/' rel='bookmark' title='Permanent Link: Using non-Python files with py2exe'>Using non-Python files with py2exe</a></li>
<li><a href='http://blog.showmedo.com/news/optimising-genshi-imports/' rel='bookmark' title='Permanent Link: Optimising Genshi Imports'>Optimising Genshi Imports</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.showmedo.com/showmedo-front-page/python-to-the-rescue/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Taming Flowplayer Pt. 1</title>
		<link>http://blog.showmedo.com/kyrans-thought-for-the-day/taming-flowplayer-pt-1/</link>
		<comments>http://blog.showmedo.com/kyrans-thought-for-the-day/taming-flowplayer-pt-1/#comments</comments>
		<pubDate>Sun, 17 Jan 2010 07:43:14 +0000</pubDate>
		<dc:creator>wizzy</dc:creator>
				<category><![CDATA[Building a Website]]></category>
		<category><![CDATA[Kyran's thought for the day]]></category>
		<category><![CDATA[Python tips and tricks]]></category>

		<guid isPermaLink="false">http://blog.showmedo.com/?p=620</guid>
		<description><![CDATA[<p>Getting Flowplayer up and running on the site was not quite as smooth as the nice web-site and clean API suggested. I&#8217;ll be documenting the whole gory process in the &#8216;Building a Website (with Python, jquery etc..)&#8217; series, but in the meantime I&#8217;ll mention the hideous hackery needed to get over the final hurdle that [...]


Related posts:<ol><li><a href='http://blog.showmedo.com/showmedo-front-page/using-python-strings-and-dictionaries-to-create-video-embedding-templates/' rel='bookmark' title='Permanent Link: Using Python  strings and dictionaries to create video embedding templates'>Using Python  strings and dictionaries to create video embedding templates</a></li>
<li><a href='http://blog.showmedo.com/news/new-flash-player-being-debuted/' rel='bookmark' title='Permanent Link: New flash-player being debuted'>New flash-player being debuted</a></li>
<li><a href='http://blog.showmedo.com/python-showmedos/testing-flowplayer-embedding-script/' rel='bookmark' title='Permanent Link: Testing Flowplayer embedding script'>Testing Flowplayer embedding script</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Getting Flowplayer up and running on the site was not quite as smooth as the nice <a href="http://flowplayer.org">web-site</a> and clean API suggested. I&#8217;ll be documenting the whole gory process in the &#8216;Building a Website (with Python, jquery etc..)&#8217; series, but in the meantime I&#8217;ll mention the hideous hackery needed to get over the final hurdle that is Internet Explorer 7.</p>
<p>Somewhere along the line I was looking to pass a web-address, as part of an html overlay, into the flash-player. This has the standard form:</p>
<p><code>"http://showmedo.com/videotutorials/video?name=1470110&amp;fromSeriesID=147"</code></p>
<p>Now if one tries to pass that string into a flashplayer, all hell will break loose. Some of those symbols are going to be interpreted as controls rather than nice, neutral characters. For this reason they need to be &#8216;escaped&#8217;, and <a href="http://turbogears.org">Turbogears</a> provides a nice little function &#8220;quote_plus&#8221;, via Python&#8217;s urllib, to do just this. The result is this rather scary looking string:</p>
<p><code>"http%3A%2F%2Fshowmedo.com</code>%2Fvideotutorials%2Fvideo%3Fname%3D1470110%26fromSeriesID%3D147&#8243;</p>
<p>This string makes it through the flash-player in all the browsers we&#8217;re interested in at Showmedo and produces a clickable flash-overlay which, strangely enough, takes one through to the page in question. Er, except (and twas ever thus), IE7 (not 8 &lt;sigh&gt;), where, when all the escape-artistry has taken place, we find ourselves directed to this non-existent page:</p>
<p><code>"http://showmedo.com/http://showmedo.com/videotutorials/video?name=1470110&amp;fromSeriesID=147"</code></p>
<p>which is a very poorly URL indeed.</p>
<p>Now I&#8217;m guessing that somewhere at the heart of this is the issue of absolute and relative web-addresses.  The second &#8216;http://&#8230;&#8217; should be interpreted as an absolute address, but those unicode escape-characters have confused the system, making it treat the URL as relative, thus adding another root-url.</p>
<p>If I had time to explore this horror I would probably spend it doing something else, but in these time-starved times I just did a little bit of playing around and added a hack-function which undoes the &#8216;:&#8217; and &#8216;/&#8217; escapes of &#8220;quote_plus&#8221;, producing this:</p>
<p><code>"http://showmedo.com</code>/videotutorials/video%3Fname%3D1470110%26fromSeriesID%3D147&#8243;</p>
<p>Which makes it through the browser minefield, but, as seems so often the case with this messed up web-development world, at the cost of some serious inelegance.</p>


<p>Related posts:<ol><li><a href='http://blog.showmedo.com/showmedo-front-page/using-python-strings-and-dictionaries-to-create-video-embedding-templates/' rel='bookmark' title='Permanent Link: Using Python  strings and dictionaries to create video embedding templates'>Using Python  strings and dictionaries to create video embedding templates</a></li>
<li><a href='http://blog.showmedo.com/news/new-flash-player-being-debuted/' rel='bookmark' title='Permanent Link: New flash-player being debuted'>New flash-player being debuted</a></li>
<li><a href='http://blog.showmedo.com/python-showmedos/testing-flowplayer-embedding-script/' rel='bookmark' title='Permanent Link: Testing Flowplayer embedding script'>Testing Flowplayer embedding script</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.showmedo.com/kyrans-thought-for-the-day/taming-flowplayer-pt-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

