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:
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:
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.
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:
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.
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
Video showing how to remotely participate in the PyCon 2010 conference in Atlanta by watching the live note taking of some thoughtful and friendly participants using EtherPad. Go to https://twitter.com/pyconpads to find pads. (Author: John Graves from Auckland, New Zealand has other videos at http://bit.ly/openallure and an Open Source project http://openallureds.org )
This little script, appearing below Showmedo’s video-boxes should allow easy embedding of the videos in blogs, articles and the like. As ever, avoid the WYSIWYG editors or the pristine HTML is likely to be horribly mangled.
If everything goes according to plan, you should be seeing a Showmedo video below (The first part of Chris Perkins’ rather wonderful series on Agile Development Tools in Python):
In this tutorial screencast you will learn how to install pygame on Linux, Mac, and Windows. I explain what you can find on http://pygame.org/download.shtml and how to find out if pygame wasn’t installed correctly.
One of the nice things about the web and coding in general is the occasional little distraction thrown up in the space of toolsets, coding-practice and the like. Today those eddies in cyberspace have brought fonts to the fore, after a while in which I really hadn’t given it too much thought. But the fact is, for someone hacking code all day a good font, while maybe not as critical as a good editor (VIM btw), is important. In the same way as buying a good bed is important, you’ll have been staring at this font for a good few life years, when time comes to cash in your programming chips. So is worth some consideration.
I generally go for a good, reliable mono-space default which is less likely to go horribly wrong (VIM sometime gets confused with widths) and is cross-platform and easy. Andale Mono tends to do the job, without feeling particularly classy. Anyway, the Consolas font crops up a lot in various programming contexts, e.g. Slashdot, Reddit, Stackoverflow.
So my curiosity has finally been piqued and I’m going to give it a try. I certainly don’t have the balls to try a proportional font, even if readability is improved by 18% or so. Here is today’s Slashdot thread, that undoubtedly gets to the heart of the issue. And a nice post from Hive-Logic about the top ten programming fonts, which suggests I should give Inconsolata a go. Which I probably will. The accompanying Reddit thread, that undoubtedly gets to the heart of the issue.
I’ll buzz back with an opinion when I’ve had time to absorb the changes. Exciting times…
Oh, and for linux bods such as myself, here’s a little hack to get them into VIM.
Getting Flowplayer up and running on the site was not quite as smooth as the nice web-site and clean API suggested. I’ll be documenting the whole gory process in the ‘Building a Website (with Python, jquery etc..)’ series, but in the meantime I’ll mention the hideous hackery needed to get over the final hurdle that is Internet Explorer 7.
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:
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 ‘escaped’, and Turbogears provides a nice little function “quote_plus”, via Python’s urllib, to do just this. The result is this rather scary looking string:
This string makes it through the flash-player in all the browsers we’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 <sigh>), where, when all the escape-artistry has taken place, we find ourselves directed to this non-existent page:
Now I’m guessing that somewhere at the heart of this is the issue of absolute and relative web-addresses. The second ‘http://…’ 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.
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 ‘:’ and ‘/’ escapes of “quote_plus”, producing this:
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.
If this gets through, the soon to be awesomeness of this blog will be seeping through to twitter automatically. Or crashing something somewhere. Going to keep on writing to hit the 140 limit – and see what it does with that.
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).
In this final segment, we highlight concepts important for new scientific Python programmers, especially those coming from a Matlab environment. We discuss effective use of Python dictionaries and the timeit profiling module. We also discuss the defaultdict object for quick and easy database creation useful for keeping track of data in multiple parameter runs.
How to instantiate the previously created class ( Foo ) is shown, using IDLE (the Integrated Development Environment , that comes with the standard CPython distribution). 2 referrers are created to understand that up until all referrers are deleted, the Python garbage collector may claim the associated instance (destruct it, deallocate it).
variables in Python are simple names associated to an instance (it could be a value). Examples are shown to enforce this concept.
OpenOffice’s Support section gives many links which include free material, courses and books.
If you have questions the most active forum seems to be the independent OpenOffice Forum and although a little bit less active, the official forum should be your other port of call.
Now let’s look at a native document – OpenOffice works with .odt files (OpenDocument Text). Normally these are zipped structures that contain xml and binary data – here we look at what’s inside an .odt file.
If you’re curious, you can poke inside your own .odt files using my instructions.
You’ll find more tips at at 3monkeyweb.
An early hurdle when switching to OpenOffice will be the question of whether you can still share documents with MS Office users. Of course you can! Here we edit a document in both MS Word 2003 and OpenOffice 3.1 Writer to show that all formatting is preserved.
In just over 2 minutes we run you through the installation of OpenOffice 3.1 on Windows XP. We also show you how you can download a version of OpenOffice for Windows, Mac or Linux in different languages.
Visit OpenOffice.org to get started.
In this series we help you move from Microsoft Word (2003 is our example) to OpenOffice 3.1. We show you how to use both Word and Office to edit the same document and how to do all the usual functions in OpenOffice, as well as where to go to get your questions answered.
Visit OpenOffice.org to get started, see the live download counter and read more background at WikiPedia.
In this segment, we continue with the case study of robust regression methods, but here we show how to use generators and the parallel computing features of IPython to accelerate the computation.
In this case study, we pull together everything we have learned so far in order to compute the least MEDIAN squared estimate of a line given data with outliers present. This segment describes the fundamental problem.
In this segment, we discuss the push, pull, and push_function, features of IPython that allow finer control over individual namespaces of remote nodes and over the computations that run on them.
In this segment, we discuss the map feature to distribute computations across the available nodes, how to use targets to specify which nodes to use, and how to use blocking to avoid waiting for long computations to finish.
IPython provides excellent interactive parallel computing tools. In this segment, we discuss how to set up remote computational engines using ipcluster and how to use special magic commands in IPython to use these remote engines.
Here we show how to run the IPython interpreter on the Windows PC (as usual), but connect to and interact with remote computational engines.