Skip to content

Commit

Permalink
make use of 'with' statement
Browse files Browse the repository at this point in the history
  • Loading branch information
Benedikt Waldvogel committed Feb 23, 2012
1 parent 710c65a commit ab3ca92
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions gmetric-mongodb.py
Expand Up @@ -117,10 +117,9 @@ def ops(self):
out = {}
cur_ops = self.status["opcounters"]
try:
f = open(self.ops_tmp_file, "r")
content = f.read()
prev_ops = json.loads(content)
f.close()
with open(self.ops_tmp_file, "r") as f:
content = f.read()
prev_ops = json.loads(content)
except (ValueError, IOError):
prev_ops = {}

Expand All @@ -131,11 +130,8 @@ def ops(self):
name = "queries_per_second"
out[name] = (max(0, float(v) - float(prev_ops[k])) / 60, "ops/s")

f = open(self.ops_tmp_file, 'w')
try:
with open(self.ops_tmp_file, 'w') as f:
f.write(json.dumps(cur_ops))
finally:
f.close()

self.callGmetric(out)

Expand Down

0 comments on commit ab3ca92

Please sign in to comment.