Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What are the drawbacks of such a solution:</p> <pre><code>ch = 'serveradress = %s\nport = %s\nssl = %s' a = raw_input("Enter the server's address : ") b = 'a' bla = "\nEnter the port : " while not all(x.isdigit() for x in b): b = raw_input(bla) bla = "Take care: you must enter digits exclusively\n"\ +" Re-enter the port (digits only) : " c = '' bla = "\nChoose the ssl option (t or f) : " while c not in ('t','f'): c = raw_input(bla) bla = "Take care: you must type f or t exclusively\n"\ +" Re-choose the ssl option : " with open('configfile.txt','w') as f: f.write(ch % (a,b,c)) </code></pre> <p>.</p> <p>PS</p> <p>I've read in the jonesy's post that the value in a config file may have to be quoted. If so, and you want the user not to have to write him/her-self the quotes, you simply add </p> <pre><code>a = a.join('""') b = b.join('""') c = c.join('""') </code></pre> <p>.</p> <h2>EDIT</h2> <pre><code>ch = 'serveradress = %s\nport = %s\nssl = %s' d = {0:('', "Enter the server's address : "), 1:("Take care: you must enter digits exclusively", "Enter the port : "), 2:("Take care: you must type f or t exclusively", "Choose the ssl option (t or f) : ") } def func(i,x): if x is None: return False if i==0: return True elif i==1: try: ess = int(x) return True except: return False elif i==2: if x in ('t','f'): return True else: return False li = len(d)*[None] L = range(len(d)) while True: for n in sorted(L): bla = d[n][1] val = None while not func(n,val): val = raw_input(bla) bla = '\n '.join(d[n]) li[n] = val.join('""') decision = '' disp = "\n====== If you choose to process, =============="\ +"\n the content of the file will be :\n\n" \ + ch % tuple(li) \ + "\n==============================================="\ + "\n\nDo you want to process (type y) or to correct (type c) : " while decision not in ('y','c'): decision = raw_input(disp) disp = "Do you want to process (type y) or to correct (type c) ? : " if decision=='y': break else: diag = False while not diag: vi = '\nWhat lines do you want to correct ?\n'\ +'\n'.join(str(j)+' - '+line for j,line in enumerate((ch % tuple(li)).splitlines()))\ +'\nType numbers of lines belonging to range(0,'+str(len(d))+') separated by spaces) :\n' to_modify = raw_input(vi) try: diag = all(int(entry) in xrange(len(d)) for entry in to_modify.split()) L = [int(entry) for entry in to_modify.split()] except: diag = False with open('configfile.txt','w') as f: f.write(ch % tuple(li)) print '-*- Recording of the config file : done -*-' </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload