[mythtv-users] Rename a file problems

Raymond Wagner raymond at wagnerrp.com
Fri Feb 10 15:36:40 UTC 2012


On 2/10/2012 09:12, David Crawford wrote:
> path = '%cn %cc %cN.srt'

Doing this in Python means you've stored that string to the variable 
'path'.  Now in Python, everything is object, and objects have methods.  
In this particular case, you want to use the 'replace' method, which 
replaces one string with another, and outputs the result.  That is this...

>     path = path.replace('%'+tag, tmp)

In Python, strings are 'mutable', meaning they cannot be changed.  The 
only way to modify the string stored to a variable is to store a new 
string to that variable, as described above.  Except you don't want that 
path string up there.  You want to use this one defined lower...

> '%m-%d_%H-%i-%s_%cN_%T'

So define that to a variable, and replace whatever the bindings can't 
handle.  That's what this does down here.

> for (tag, data) in (('cn', 'channum'),('cc','callsign'),('cN','name')):
>     tmp = unicode(chan[data]).replace('/','-')
>     path = path.replace('%'+tag, tmp)

It loops through those three sets of values, setting 'tag' to 'cn', and 
'data' to 'channum' on the first pass.  The Channel class is configured 
to imitate a dictionary, or map, or associative array, depending on your 
programming language of preference.  When it pulls 'chan[data]', it is 
pulling the value mapped to the string currently stored in the 'data' 
variable, which is 'channum', out of that associative array.

Now you're only trying to use %cN, so there is no need for this 
construct.  You can just do the following directly...

> path = '%m-%d_%H-%i-%s_%cN_%T'
> chan = Channel(chanid)
> path = path.replace('%cN', chan.name)

The Channel class makes all of those associative array values also 
available as class attributes.  Attributes are just variables stored 
against an object, and accessed in the same manner as methods (with a 
'.').  Remember, you still need to feed this new, modified path into the 
'formatPath' method, to get out your desired result.



More information about the mythtv-users mailing list