2009-04-13 7 views
1

Golf - implementieren Sie ein einfaches Template-Schema.Golf - Expand Vorlagen in Textdatei

Expansionen sind:

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

Kommandozeilenargumente:

  • ARG1: Wörterbuch-Datei, formatiert der key=value Stil wie in Beispiel
  • ARG2: templ aß Datei

Hier mein nicht ganz Golf Versuch (Python): 261 Zeichen.

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 

TEMPLATE

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

RESULT

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

Ja, ich weiß, das ist ein defektes Template-System ist, "schnappt" für eine kürzere und besser Implementierung.

Antwort

2

In Python können Sie die eingebaute Formatierung von Strings nutzen, um sie kürzer zu machen. Braucht nur ein wenig Regex-Hacking, um die Syntax passend zu bekommen.

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

Bis zu 139 Bytes. (Obwohl vielleicht die args/file-IO Sachen sollten wirklich nicht Teil eines Golf Herausforderung sein?)

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

Das ist 159 für den Algorithmus Teil, glaube ich. Beachten Sie, dass dies zu unerwarteten Ergebnissen führen kann, wenn Sie etwas wie "NAME = %%" füttern (es wird das %% weiter in% reduzieren), aber jeder kurze einfache Algorithmus zeigt ein Verhalten wie dieses oder das andere, da Sie haben um die String-Ersetzungen in einer bestimmten Reihenfolge auszuführen.

+0

+1 Nizza, war ich über so etwas schreiben –

0

Rubin, 114 Zeichen

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

in vC++ .. vielleicht der schnellste Weg sein :)

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

nicht wirklich eine Antwort auf die Frage, aber ich hoffe, Sie erkennen, dass, wenn Sie dies für jede Art von Echt Wort Aufgabe benötigen, hat Python ein viel besser lesbar Vorlagenformat mit {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) 

Wenn Sie möchten, können Sie Ihr Wörterbuch-Format erhalten:

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