ShowMeDo – Free Video Tutorials – Python, Java, Linux, Ruby, Blender, …

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

<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>

  1. Testing Flowplayer embedding script
  2. New Scribus Video Published
  3. Taming Flowplayer Pt. 1

ShowMeDo - Free Video Tutorials - Python, Java, Linux, Ruby, Blender, ...

Possibly related posts: (automatically generated)

Comments are closed.