Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To speed up your existing code measurably, add <code>def main():</code> before the assignment to <code>tokenList</code>, indent everything after that 4 spaces, and at the end put the usual idiom</p> <pre><code>if __name__ == '__main__': main() </code></pre> <p>(The guard is not actually necessary, but it's a good habit to have nevertheless since, for scripts with reusable functions, it makes them importable from other modules).</p> <p>This has little to do with "object oriented" anything: it's simply faster, in Python, to keep all your substantial code in functions, <em>not</em> as top-level module code.</p> <p>Second speedup, change <code>cleanedInput</code> into a list, i.e., its first assignment should be <code>= []</code>, and wherever you now have <code>+=</code>, use <code>.append</code> instead. At the end, <code>''.join(cleanedInput)</code> to get the final resulting string. This makes your code take linear time as a function of input size (<code>O(N)</code> is the normal way of expressing this) while it currently takes quadratic time (<code>O(N squared)</code>).</p> <p>Then, correctness: the two statements right after <code>continue</code> never execute. Do you need them or not? Remove them (and the <code>continue</code>) if not needed, remove the <code>continue</code> if those two statements are actually needed. And the tests starting with <code>if diff</code> will fail dramatically unless the previous <code>if</code> was executed, because <code>diff</code> would be undefined then. Does your code as posted perhaps have indentation errors, i.e., is the indentation of what you posted different from that of your actual code?</p> <p>Considering these important needed enhancements, and the fact that it's hard to see what advantage you are pursuing in making this tiny code OO (and/or modular), I suggest clarifying the indenting / correctness situation, applying the enhancements I've proposed, and leaving it at that;-).</p> <p><strong>Edit</strong>: as the OP has now applied most of my suggestions, let me follow up with one reasonable way to hive off most functionality to a class in a separate module. In a new file, for example <code>foobar.py</code>, in the same directory as the original script (or in <code>site-packages</code>, or elsewhere on <code>sys.path</code>), place this code:</p> <pre><code>def token_of(line): return line.partition(':')[-1].strip() class FileParser(object): def __init__(self, filename): self.tokenList = open(filename, 'r') def cleaned_input(self): cleanedInput = [] prevLine = 0 for line in self.tokenList: if line.startswith('LINE:'): lineNo = int(token_of(line)) diff = lineNo - prevLine - 1 cleanedInput.append('\n' * (diff if diff&gt;1 else diff+1)) prevLine = lineNo else: cleanedLine = token_of(line) cleanedInput.append(cleanedLine + ' ') return cleanedInput </code></pre> <p>Your main script then becomes just:</p> <pre><code>import sys import foobar def main(): thefile = foobar.FileParser(sys.argv[1]) print thefile.cleaned_input() if __name__ == '__main__': main() </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. 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.
 

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