Blog Roll

Using Python strings and dictionaries to create video embedding templates

Kyran Dale

Sat, 27 Feb 2010 13:31:02 +0000

All Pythonistas will recognize the % operator used to format strings:

print "%s world from %s!"%('hello', 'showmedo')

This is similar to its ‘C’ antecedent but, as you would expect and probably know, Python allows containers other than tuples (in the case containing the ‘hello’ and ’showmedo’ strings) to be used in the string formatting operation.

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.

We start with our text-file template, one long string with no pesky line-breaks:

<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"><param name="m    ovie" value="http://showmedo.com/static/flowplayer/flowplayer-3.1.5.swf" /><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param  [...]   &quot;%(url)s&quot;,&quot;title&quot;:&quot;%(title)s&quot;,&quot;baseUrl&quot;:&quot;http://showmedo.com&quot;,&quot;autoPlay&quot;:false,&quot;autoBuffering&quot;:true}],    &quot;plugins&quot;:{&quot;controls&quot;:{&quot;url&quot;:&quot;http://showmedo.com/static/flowplayer/flowplayer.controls-3.1.5.swf&quot;,&quot;playlist&quot;:true}}}' /></object><br />

Note the variables scattered through the string starting with %(width)d. We’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 ‘Video’ class and in this context ’self’ refers to the video instance in question:

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

We open the ‘embed_video.txt’ file (using a little Python os magic to establish a relative directory path) and read its contents, storing them in the ‘embed_template’ string. We then use the ‘%’ operator to assign values to the special (of the form %(foo)x) variables in the template, using a Python dictionary.

The resulting ‘embed_str’ string defines a flash

which can be used to embed Showmedo videos in blogs, articles and the like:

<object width="425" height="344" id="_672" data="http://showmedo.com/static/flowplayer/flowplayer-3.1.5.swf" type="application/x-shockwave-flash"><param name="movie" value="http://showmedo.com/static/flowplayer/flowplayer-3.1.5.swf" /><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="flashvars" value='config={&quot;key&quot;:&quot;#$824e5316466b69d76dc&quot;,&quot;logo&quot;:{&quot;url&quot;:&quot;http://showmedo.com/static/images/showmedo_logo_vp.png&quot;,&quot;fullscreenOnly&quot;:false,&quot;top&quot;:20,&quot;right&quot;:20,&quot;opacity&quot;:0.5,&quot;displayTime&quot;:0,&quot;linkUrl&quot;:&quot;http://showmedo.com&quot;},&quot;clip&quot;:{&quot;baseUrl&quot;:&quot;http://showmedo.com&quot;,&quot;autoPlay&quot;:false,&quot;autoBuffering&quot;:true},&quot;playlist&quot;:[{&quot;url&quot;:&quot;http://showmedovideos4.com/ShowMeDos/2450010.flv&quot;,&quot;title&quot;:&quot;Scientific Computing Using SAGE: Introduction&quot;,&quot;baseUrl&quot;:&quot;http://showmedo.com&quot;,&quot;autoPlay&quot;:false,&quot;autoBuffering&quot;:true}],&quot;plugins&quot;:{&quot;controls&quot;:{&quot;url&quot;:&quot;http://showmedo.com/static/flowplayer/flowplayer.controls-3.1.5.swf&quot;,&quot;playlist&quot;:true}}}' /></object>

Related posts:

  1. Testing Flowplayer embedding script
  2. Taming Flowplayer Pt. 1
  3. ShowMeDo: Learn Django: Create a Wiki in 20 minutes (1 video)

Python to the Rescue

Kyran Dale

Thu, 25 Feb 2010 14:59:26 +0000

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 the health of, among other things, the Python Turbogears process running the site died. Now this script isn’t needed too often <fingers crossed> but if the site falls down and we’re literally asleep then it avoids more than a minute or sos downtime. The crontab line in question looks like this:

*/1 * * * * source /.bashrc && /usr/local/bin/python2.5 /home/showmedo/showmedo/cron_start_showmedo.py > /var/log/cron_log

This line tells cron to run cron_start_showmedo.py every minute and the python process will check the health of Showmedo’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 here 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:

  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)

Not pretty, but at five lines quick and succinct. The line 4 while loop sets things running in perpetuity, line 5 uses Python’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.

The script sat duty while I waited for our cron-daemon to return and I slept sounder in my bed as a result.

Related posts:

  1. A Little Downtime :(
  2. Using non-Python files with py2exe
  3. Optimising Genshi Imports

A Little Downtime :(

Kyran Dale

Mon, 22 Feb 2010 10:34:16 +0000

Having to wear my sysadmin hat a little too often this week. I can honestly state that at least one of the bugs wasn’t my fault. At the moment our cron-jobs (timed unix processes) are failing to engage. So when the site falls down, there’s no handy cron-restart. Should be easy to track down and lots of fun :0

Related posts:

  1. Python to the Rescue
  2. Site outages – sorry for the downtime
  3. Christmas wishes

New flash-player being debuted

Kyran Dale

Fri, 08 Jan 2010 22:24:21 +0000

It’s been much more work than anticipated (a definite screencast or three in there somewhere) but we’ve finally moved to the open-source flowplayer Chief benefits are:

  • much cleaner jquery-like API
  • far greater control over the video-player area. Which means…
  • innovative ways of directing users to new content and…
  • with luck much more interactivity with the video-player

p.s. the commenting on-site is getting a much-needed overhaul. With a bit of cookie-magic we should be able to get some dialogues going and vanquish those nasty anonymous comments (mea culpa).

 

Related posts:

  1. New ShowMeDos: Interactive Flash
  2. Site update
  3. Showmedo Relaunching

Python Beginners Completed! – File Input/Output

Kyran Dale

Fri, 16 Oct 2009 08:54:56 +0000

Finally we’ve finished the Python Beginners über-series in our Club. In total the Club has 118 videos over 15 series all focused on teaching new Python skills. Now we’re done with Python beginners we’ll be expanding our scope.

In just under 30 minutes in the File I/O Tutorial I cover reading text files, writing textfiles, using binary files (with Python Imaging Library as the example) and persisting data with the pickle module.

Now that we’re finished with the raw episodes and our Learning Paths are usable I’m tying together all the Python videos (free+Club) into paths-of-learning that teach you about topics including:

  • Starting to Program with Python
  • Getting Python Installed
  • Walking through fully-written programs (long step-by-step series)
  • Python development environments and tools
  • Python GUI development
  • Django
  • Web-application development

The learning-paths, and learning how to use them effectively, are very much a work in progress. As ever we welcome any and all constructive feedback.

Once we’re done with the Python learning paths we’ll start to tie together all the other topics in ShowMeDo (heck – we have well over 1000 screencast tutorials now!) to make learning much easier.

Related posts:

  1. Python Beginners – Common Variables and Containers Series Completed
  2. 15 New Learning Paths (mostly for Pythonistas)
  3. Get Going with Python 2: Python Development Environments (IDEs) part 1

Python 411 podcast interview – thanks Ron!

Kyran Dale

Thu, 28 May 2009 19:41:16 +0000

Ron Stephens, creator of the rather excellent Python411 podcast series was good enough to interview Kyran and myself a couple of weeks back: An interview with the founders of ShowMeDo: May 25, 2009

We cover the history of how we started ShowMeDo with Python screencasts, Kyran’s innovative Learning Paths (which caught MIT’s eye), the special learn-Python-quickly tutorials in our Club, our authors and the 1,000 educational screencasts we’ve built between us to date.

We also discuss how you can share your own knowledge with 100,000 global viewers and end with a light chat about Python 3.0.

Ron has a long list of podcast interviews, check them out if you want to know about topics like Django, Python Visualisation and the Python Learning Foundation.  He also lists many Python tutorials across the web, they are great short-cuts to find what you need.

Related posts:

  1. Ubuntu UK Podcast interviews ShowMeDo
  2. Python Advocacy Podcast (Python411)
  3. 15 New Learning Paths (mostly for Pythonistas)

Python Beginners – Club Collection Takes Shape

Kyran Dale

Tue, 05 May 2009 01:25:59 +0000

Have finally got round to giving the Club videos a bit of presentational structure following Ian’s last file I/O series in his huge Beginner Programming With Python.

Ian’s final series in Beginners brings the club total to 118 videos, 15 video-series and over 10 hours of Pythonic video demonstration <phew>.

Ian’s beginner-sets were conceived as a whole, from introducing the look and feel of Python to covering, in quite a bit of detail, the general Python programming elements. With Lucas Holland’s introduction to the Python standard libraries we have a nice round number of 50 videos, setting clubbers up nicely for the other club videos and some of the intermediate/advanced Showmedo Python series.

Related posts:

  1. New Club videos for “Batteries Included” series on The Python Standard Library
  2. New Club series for Python Beginners – Common Variables and Containers
  3. New Club Series for Python Beginners – Loops and Iteration

15 New Learning Paths (mostly for Pythonistas)

Kyran Dale

Fri, 10 Apr 2009 18:17:47 +0000

We’ve published 15 new Learning Paths to help you build your knowledge of Python, C and Screencasting.  The Paths mix free and Club content in a guided journey, pulling out exactly the right episodes and series to help you complete your knowledge for a particular subject.

Popular Paths include:

If you like the idea of these guided tutorials, please give us feedback and help us to spread the word by blogging and tweeting (@showmedo).

Related posts:

  1. Python Tutorials via Learning Paths
  2. Python Beginners Completed! – File Input/Output
  3. Huge screencast series ‘Developing emol!’ for new Pythonistas

Showmedo Relaunching

Kyran Dale

Tue, 10 Mar 2009 02:41:03 +0000

Frantic scenes aboard the SS Showmedo as we move to a new server and introduce a much-changed site. According to google analytics about one third of our regular audience are currently lost in cyberspace as various DNS caches around the globe send them off to the old site or some weird fusion of old and new.

Moving server has been as stressful as I imagined, probably the reason for all those ‘moving servers is stressful’ blog entries around the web. We’re currently a little rickety and scared to crank up the engines for fear of shaking the ship apart. But, thanks to some caching magic, we seem to be serving pages and videos.

The new site design is the result of finally having a big chunk of hours to dedicate to Showmedo, rather than cadging them after hours from the day-job. I think things are a deal cleaner and more professional, allowing for my being rather an accidental web-developer. The feedback has been pretty positive, which is always nice.

Our new initiative viz web-education is Learning-paths. I’ll be blogging a little about this as soon as I manage to get the server software installed. For all you linux users out there used to the simple pleasures of ‘apt-get install’ or ‘yum’ or ‘rpm’ or any one of the new-fangled ways of getting software onto your *buntu, opensuse, fedora, etc. box, be very thankful; I have been plunged back a couple of decades or so to library-dependency hell and it is a head-trip indeed.

Related posts:

  1. We’re moving to a new host this weekend
  2. Python Beginners Completed! – File Input/Output
  3. 15 New Learning Paths (mostly for Pythonistas)

Kudos and Thanks to Our Authors

What's New
Checkout Our New Video Page
We've cleaned things up a little, aiming at making things easier to find and the site a more pleasant experience.
Showmedo Introduces Learning-paths
We're stepping up a gear with our new learning-paths. By structuring Showmedo's content we hope to make the site a more effective learning centre.
Showmedo's Notice Boards
!!COMING VERY SOON!! To encourage collaborative education and increase community feedback, we've introduced notice-boards. These allow requests for new learning-paths to be posted and subsequent responses followed.
Showmedo's Stats Page
Our authors are the true heros of Showmedo. Here we start gathering some info on their contributions. Kudos.
Our New Monthly Competition
To celebrate the introduction of Learning-paths we're offering a prize of $100 for the best learning-path this month. The judges will be drawn from existing star authors.

Welcome to Showmedo!

Showmedo is about learning and (Free and) Open-source software (FOSS). We were inspired to start Showmedo by watching some very effective web video-tutorials/screencasts. These convinced us that web-videos can be a great way to quickly and efficiently acquire knowledge. It can even be fun, or at least painless. For some things there is no substitute to seeing it done.

Incoming Video Tutorials

John Graves A demonstration of a webcam gesture-based game like Pong(R). Note that scoring and sound [...]
yazkicom Django da blog hazırlıycaz django da basit bir blog uygulaması hazırlıyacağız .. Bunun iç [...]
unpingco SAGE is an open-source project to integrate the best open-source scientific packages unde [...]
John Graves Even if you could not attend PyCon 2010 in Atlanta, you can still find useful slides and [...]
Fred Chevitarese How to create Komodo Edit shortcuts
Kenny X Learn how to make games with Python's amazing pygame library! It's much more simple than [...]
Tim Harper How to install dragonfly
luispedro This is an example of using Jug to perform a simple task.
FlashKidGotSuspended A simple tutorial on how I fixed my video lag and problems in Ubuntu 9.10 Karmic Koala Fi [...]
gasto Esta serie de vídeos deberían de guiar al usuario a usar Blender desde el inicio. Blender [...]
Jurgis Pralgauskis Žaismingos programavimo aplinkos Scratch (http://scratch.mit.edu) pagrindai. Learn how to [...]