From 53ee78074e30cce19aa38520e7c7dcc626680d83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Schoentgen?= Date: Wed, 2 Jan 2019 15:41:13 +0100 Subject: [PATCH] Fix several ResourceWarnings: unclose file MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mickaƫl Schoentgen --- bashplotlib/histogram.py | 8 +++++--- bashplotlib/scatterplot.py | 16 +++++++++------- setup.py | 5 ++++- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/bashplotlib/histogram.py b/bashplotlib/histogram.py index 43b7dd3..80a6368 100644 --- a/bashplotlib/histogram.py +++ b/bashplotlib/histogram.py @@ -42,8 +42,9 @@ def read_numbers(numbers): for number in numbers: yield float(str(number).strip()) else: - for number in open(numbers): - yield float(number.strip()) + with open(numbers) as fh: + for number in fh: + yield float(number.strip()) def run_demo(): @@ -106,7 +107,8 @@ def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="def pch = "o" if isinstance(f, str): - f = open(f).readlines() + with open(f) as fh: + f = fh.readlines() min_val, max_val = None, None n, mean, sd = 0.0, 0.0, 0.0 diff --git a/bashplotlib/scatterplot.py b/bashplotlib/scatterplot.py index b9a1948..6413da6 100644 --- a/bashplotlib/scatterplot.py +++ b/bashplotlib/scatterplot.py @@ -42,20 +42,22 @@ def plot_scatter(f, xs, ys, size, pch, colour, title): title -- title of the plot """ + cs = None if f: if isinstance(f, str): - f = open(f) - - data = [tuple(line.strip().split(',')) for line in f] + with open(f) as fh: + data = [tuple(line.strip().split(',')) for line in fh] + else: + data = [tuple(line.strip().split(',')) for line in f] xs = [float(i[0]) for i in data] ys = [float(i[1]) for i in data] if len(data[0]) > 2: cs = [i[2].strip() for i in data] - else: - cs = None else: - xs = [float(str(row).strip()) for row in open(xs)] - ys = [float(str(row).strip()) for row in open(ys)] + with open(xs) as fh: + xs = [float(str(row).strip()) for row in fh] + with open(ys) as fh: + ys = [float(str(row).strip()) for row in fh] plotted = set() diff --git a/setup.py b/setup.py index da6e1af..f85f54b 100644 --- a/setup.py +++ b/setup.py @@ -2,6 +2,9 @@ from setuptools import find_packages, setup +with open("README.rst") as fh: + long_description = fh.read() + setup( name="bashplotlib", version="0.6.5", @@ -11,7 +14,7 @@ license="BSD", packages=find_packages(), description="plotting in the terminal", - long_description=open("README.rst").read(), + long_description=long_description, entry_points = { 'console_scripts': [ 'hist=bashplotlib.histogram:main',