Skip to content
This repository has been archived by the owner on May 11, 2018. It is now read-only.

[docs] Context managers, file.writelines, etc #20

Open
wants to merge 1 commit into
base: docs
Choose a base branch
from
Open

[docs] Context managers, file.writelines, etc #20

wants to merge 1 commit into from

Conversation

delirious-lettuce
Copy link
Contributor

  • Use file.writelines instead of file.write for each line
  • Use with statement context managers instead of manually closing each file
  • Use a try/except inside main to unpack the three sys.argv arguments (raise ValueError if not enough/too many arguments)
    • Except for gosecure_client_install where only one argument is needed

I noticed quite a few blocks of code like this and thought it might be inefficient.

with open("/etc/modules", "r+") as f:
    for line in f:
        if "bcm2708-rng" in line:
            break
    else: # not found, we are at the eof
        call("sudo sh -c 'echo bcm2708-rng >> /etc/modules'")

It seemed odd to iterate through each separate line of f and check for the exact same string. I set up a small test to get an idea of how long it took compared to just searching f.read() for the one string.

In[20]: with open('treasure_test_output.txt', 'w') as f:
   ...:     f.writelines([('*' * 150) + '\n' for _ in xrange(999)])
   ...:     f.write(('*' * 150) + 'treasure\n')
   ...: 
   ...: one = """\
   ...: with open('treasure_test_output.txt', 'r') as f:
   ...:     for line in f:
   ...:         if 'treasure' in line:
   ...:             break
   ...: """
   ...: 
   ...: two = """\
   ...: with open('treasure_test_output.txt', 'r') as f:
   ...:     'treasure' in f.read()
   ...: """
In[21]: timeit(one, number=100000)
Out[21]: 
36.9743390083313
In[22]: timeit(two, number=100000)
Out[22]: 
10.249281167984009

To test the worst case scenario, I created a 1000 line file where the word "treasure" was at the end of the very last line. Using content = f.read() and searching for that one string was around 3 times faster every time I checked it.

I went through and changed the format of each of these instances to search for the desired string in the f.read() string.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant