Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all, many string functions – including strip and replace – are <a href="http://docs.python.org/library/string.html#deprecated-string-functions" rel="nofollow noreferrer" title="Deprecated string functions">deprecated</a>. The following answer uses string methods instead. (Instead of <code>string.strip(" Hello ")</code>, I use the equivalent of <code>" Hello ".strip()</code>.)</p> <p>Here's some code that will simplify the job for you. The following code assumes that whatever methods you call on your string, that method will return another string.</p> <pre><code>class O(object): c = str.capitalize r = str.replace s = str.strip def process_line(line, *ops): i = iter(ops) while True: try: op = i.next() args = i.next() except StopIteration: break line = op(line, *args) return line </code></pre> <p>The <code>O</code> class exists so that your highly abbreviated method names don't pollute your namespace. When you want to add more string methods, you add them to <code>O</code> in the same format as those given.</p> <p>The <code>process_line</code> function is where all the interesting things happen. First, here is a description of the argument format:</p> <ul> <li>The first argument is the string to be processed.</li> <li>The remaining arguments must be given in pairs. <ul> <li>The first argument of the pair is a string method. Use the shortened method names here.</li> <li>The second argument of the pair is a list representing the arguments to that particular string method.</li> </ul></li> </ul> <p>The <code>process_line</code> function returns the string that emerges after all these operations have performed.</p> <p>Here is some example code showing how you would use the above code in your own scripts. I've separated the arguments of <code>process_line</code> across multiple lines to show the grouping of the arguments. Of course, if you're just hacking away and using this code in day-to-day scripts, you can compress all the arguments onto one line; this actually makes it a little easier to read.</p> <pre><code>f = open("parrot_sketch.txt") for line in f: p = process_line( line, O.r, ["He's resting...", "This is an ex-parrot!"], O.c, [], O.s, [] ) print p </code></pre> <p>Of course, if you very specifically wanted to use numerals, you could name your functions <code>O.f1</code>, <code>O.f2</code>, <code>O.f3</code>… but I'm assuming that wasn't the spirit of your question.</p>
    singulars
    1. This table or related slice is empty.
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    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