def path_subst(path, mapping):
 newpath = []
 plen = len(path)
 n = 0
 while (n < plen):
  result = path[n]
  if (result == '%'):
   for (key, value) in mapping:
    if (path.startswith(key, n) and (not path.startswith('%ext', n))):
     n += (len(key) - 1)
     result = value
     break
  newpath.append(result)
  n += 1
 return ''.join(newpath)

def path_subst ( path , mapping ) : newpath = [ ] plen = len ( path ) n = 0 while ( n < plen ) : result = path [ n ] if ( result == '%' ) : for ( key , value ) in mapping : if ( path . startswith ( key , n ) and ( not path . startswith ( '%ext' , n ) ) ) : n += ( len ( key ) - 1 ) result = value break newpath . append ( result ) n += 1 return '' . join ( newpath )
replace the sort sting elements by real values .