2009-04-13 16 views
1

高爾夫 - 實現簡單的模板方案。高爾夫 - 在文本文件中展開模板

展開包括:

  • %KEY% - > VALUE
  • %% - >%

的命令行參數:

  • ARG1:字典文件,在格式化樣例中的key=value樣式
  • ARG2:templ吃的文件

在這裏我不是很高爾夫的嘗試(python):261個字符。

import sys 
dd = dict([ll.split("=",2) for ll in open(sys.argv[1],'r') if len(ll.split("=", 2)) == 2]) 
tt = "".join([ ll for ll in open(sys.argv[2],'r')]) 
sys.stdout.write("".join([(((s == "") and "%") or ((s in dd) and dd[s]) or s) for s in tt.split("%")])) 

DICT

NAME=MyName 
ODDS=100 

模板

I, %NAME% am %ODDS% %% sure that that this a waste of time. 

結果

I, My Name am 100 % sure that this is a waste of time. 

是的,我意識到這是一個有缺陷的模板系統, 「卡」 在更短的和更好實現。

回答

2

在Python中,您可以利用內置字符串格式來縮短它。只需要一點點正則表達式來獲得匹配的語法。

import sys, re 
sys.stdout.write(re.sub(r'%(.+?)%',r'%(\1)s',open(sys.argv[2]).read())%dict(l.split("=",2) for l in open(sys.argv[1],'r'))) 

最多139個字節。 (雖然也許ARGS /文件IO的東西真的不應該是一個高爾夫挑戰的一部分?)

1

C#。

string s = "NAME=MyName ODDS=100"; // any whitespace separates entries 
string t = "I, %NAME% am %ODDS% %% sure that that this a waste of time."; 

foreach(var p in s.Split().Select(l=>l.Split('=')).ToDictionary(
e=>"%"+e[0]+"%",e=>e[1]))t=t.Replace(p.Key,p.Value);t=t.Replace("%%","%"); 
Console.WriteLine(t); 

對於算法部分,這是159,我相信。請注意,如果你給它提供類似「NAME = %%」(它會將%%進一步折成%),這可能會產生意想不到的結果,但是任何簡單的算法都會顯示出這樣或那樣的行爲,因爲你有以某種順序進行字符串替換。

+0

+1尼斯,我正要張貼類似的東西 – 2009-04-13 17:36:35

0

紅寶石,114個字符

d=Hash[*[open(ARGV[0]).read.scan(/(.+)=(.*)/),'','%'].flatten] 
print open(ARGV[1]).read.gsub(/%([^%]*)%/){d[$1]} 
1

在VC++ ..可能是最快的方式:)

void customFormat(const char* format, char dicItemSep, char dicValueSep, const char* dic, char* out) 
{ 
    if(out) 
    { 
     const char x = '%'; 
     while(*format) 
     { 
      while(*format && *format != x)*out++=*format++; 
     if(!*format || !*(++format))goto exit; 
      if(*format == x) 
      { 
       *out++=x; 
       continue; 
      } 
      const char *first = format; 
      const char *d = dic; 
      while(*first) 
      { 
       while(*d) 
       { 
        while(*d && (*d != *first))d++; 
        if(*d == *first) 
        { 
         while((*first++ == *d++) && (*d != dicValueSep)); 
         if(!*first)goto exit; 
         if(*d != dicValueSep || *first != x) 
         { 
          first = format; 
          while(*d && (*d != dicItemSep))d++; 
          continue; 
         } 
         break; 
        } 
       } 
       if(!*d)break; 
       d++; 
       while((*d != dicItemSep) && *d && (*out++ = *d++)); 
       format = ++first; 
       break; 
      } 
     } 
    exit: 
     *out = 0; 
    } 
} 
int _tmain(int argc, _TCHAR* argv[]) 
{ 
    char t[1024]; 
    customFormat 
     (
      "%NAME% am %ODDS% %% sure that that this a waste of time.", 
      '\n', 
      '=', 
      "NAME=MyName\nODDS=100", 
      t 
     ); 
    printf("%s", t); 
} 
0

不是一個真正的問題的答案,但我希望你明白,如果你需要這個任何一種真正的單詞任務,Python有與{variable}

一個 更可讀模板格式
# MyDict.py 
dict = { 
    some : 'every', 
    p : '?', 
    q : '?..' 
} 

# main file 
import MyDict 
print """ 
    What is {some}thing{p} 
    Who values this mysterious {some}thing{q} 
""".format(**dict) 

如果你願意,你可以保留你的字典格式:

with open(dictionary_file, 'r') as f: 
     for l in f.readlines(): 
      k, v = '='.split(l) 
      if v: dict[k] = v