Skip to content

Commit 23319fe

Browse files
authored
Merge pull request #46 from BoboTiG/fix-resource-leak
Fix several ResourceWarnings: unclose file
2 parents 067247c + 53ee780 commit 23319fe

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

bashplotlib/histogram.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ def read_numbers(numbers):
4242
for number in numbers:
4343
yield float(str(number).strip())
4444
else:
45-
for number in open(numbers):
46-
yield float(number.strip())
45+
with open(numbers) as fh:
46+
for number in fh:
47+
yield float(number.strip())
4748

4849

4950
def run_demo():
@@ -106,7 +107,8 @@ def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="def
106107
pch = "o"
107108

108109
if isinstance(f, str):
109-
f = open(f).readlines()
110+
with open(f) as fh:
111+
f = fh.readlines()
110112

111113
min_val, max_val = None, None
112114
n, mean, sd = 0.0, 0.0, 0.0

bashplotlib/scatterplot.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,22 @@ def plot_scatter(f, xs, ys, size, pch, colour, title):
4242
title -- title of the plot
4343
"""
4444

45+
cs = None
4546
if f:
4647
if isinstance(f, str):
47-
f = open(f)
48-
49-
data = [tuple(line.strip().split(',')) for line in f]
48+
with open(f) as fh:
49+
data = [tuple(line.strip().split(',')) for line in fh]
50+
else:
51+
data = [tuple(line.strip().split(',')) for line in f]
5052
xs = [float(i[0]) for i in data]
5153
ys = [float(i[1]) for i in data]
5254
if len(data[0]) > 2:
5355
cs = [i[2].strip() for i in data]
54-
else:
55-
cs = None
5656
else:
57-
xs = [float(str(row).strip()) for row in open(xs)]
58-
ys = [float(str(row).strip()) for row in open(ys)]
57+
with open(xs) as fh:
58+
xs = [float(str(row).strip()) for row in fh]
59+
with open(ys) as fh:
60+
ys = [float(str(row).strip()) for row in fh]
5961

6062
plotted = set()
6163

setup.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
from setuptools import find_packages, setup
44

5+
with open("README.rst") as fh:
6+
long_description = fh.read()
7+
58
setup(
69
name="bashplotlib",
710
version="0.6.5",
@@ -11,7 +14,7 @@
1114
license="BSD",
1215
packages=find_packages(),
1316
description="plotting in the terminal",
14-
long_description=open("README.rst").read(),
17+
long_description=long_description,
1518
entry_points = {
1619
'console_scripts': [
1720
'hist=bashplotlib.histogram:main',

0 commit comments

Comments
 (0)