python - for statement and replace keyword in the code -


i new python, wk2 trying follow book, learn python hard way.

for code in ex41.py, have 2 questions.

  1. i don't quite understand why has following statement:

    for in range(0, snippet.count("@@@")):

    since snippet.count("@@@") 1, have 1 value 0. why loop through anyway? why can't go straight set variables param_count , param_names?

  2. i confused by:

    for word in param_names:     result = result.replace("@@@", word, 1) 

    i know param_names list of words 3 words, since @@@ show once in both snippet , phrase, how can replaced more 1 times?

thanks,

import random urllib import urlopen import sys  word_url = "http://learncodethehardway.org/words.txt" words = []  phrases = { "class %%%(%%%):":  "make class named %%% is-a %%%.", "class %%%(object):\n\tdef __init__(self, ***)" :  "class %%% has-a __init__ takes self , *** parameters.", "class %%%(object):\n\tdef ***(self, @@@)":  "class %%% has-a function named *** takes self , @@@ parameters.", "*** = %%%()":  "set *** instance of class %%%.", "***.***(@@@)":  "from *** *** function, , call parameters self, @@@.", "***.*** = '***'":  "from *** *** attribute , set '***'." }   phrase_first = false if len(sys.argv) == 2 , sys.argv[1] == "english":     phrase_first = true   word in urlopen(word_url).readlines():     words.append(word.strip())  def convert(snippet, phrase):     class_names = [w.capitalize() w in                     random.sample(words, snippet.count("%%%"))]     other_names = random.sample(words, snippet.count("***"))     results = []     param_names = []      in range(0, snippet.count("@@@")):         param_count = random.randint(1,3)         param_names.append(', '.join(random.sample(words, param_count)))      sentence in snippet, phrase:         result = sentence[:]            word in class_names:             result = result.replace("%%%", word, 1)          word in other_names:             result = result.replace("***", word, 1)          word in param_names:             result = result.replace("@@@", word, 1)          results.append(result)      return results   try:     while true:         snippets = phrases.keys()         random.shuffle(snippets)          snippet in snippets:             phrase = phrases[snippet]             question, answer = convert(snippet, phrase)              if phrase_first:                 question, answer = answer, question              print question              raw_input("> ")             print "answer: %s\n\n" % answer except eoferror:     print "\nbye" 

param_names contains subsequent names of parameters, if have ['param1', 'param2'] , string '(@@@, @@@)' want first replace result.replace("@@@", 'param1', 1) replace first occurrence of @@@ resulting in '(param1, @@@)'. then, want replace second @@@ have '(param1, param2)'. without constraint end '(param1, param1)', i.e. occurrences of @@@ replaced name of first parameter. same applies other place holders (classes , "others").

the fact seems redundant particular input data not imply it's redundant in general. needed other possible inputs example in script, without algorithm incorrect.


Comments

Popular posts from this blog

IF statement in MySQL trigger -

c++ - What does MSC in "// appease MSC" comments mean? -

android - MPAndroidChart - How to add Annotations or images to the chart -