From 957c15509fd0700bf80f7a19d99eeab4919764e3 Mon Sep 17 00:00:00 2001 From: Jan Pecinovsky Date: Wed, 4 Nov 2015 22:49:36 +0100 Subject: [PATCH] Python 3 compatibility Not thoroughly tested, but the tutorial runs in both Python 2 and 3 without errors (some warnings in Py3). --- charts/__init__.py | 10 +++++----- charts/chart.py | 2 +- charts/core.py | 8 ++++---- charts/data.py | 4 ++-- charts/plot.py | 10 +++++----- charts/server.py | 11 ++++++++--- charts/settings.py | 2 +- 7 files changed, 26 insertions(+), 21 deletions(-) diff --git a/charts/__init__.py b/charts/__init__.py index e167a62..e9eb52a 100644 --- a/charts/__init__.py +++ b/charts/__init__.py @@ -2,12 +2,12 @@ __version__ = '0.4.6' -from plot import plot, plotasync, line, area, spline, pie -from server import run_server +from .plot import plot, plotasync, line, area, spline, pie +from .server import run_server from IPython.core import getipython from IPython.core.display import display, HTML -from settings import default_settings, default_options, load_options -import data -import jsonencoder +from .settings import default_settings, default_options, load_options +import charts.data +import charts.jsonencoder diff --git a/charts/chart.py b/charts/chart.py index 8afa23c..3568f3d 100644 --- a/charts/chart.py +++ b/charts/chart.py @@ -1,7 +1,7 @@ __author__ = 'arnoutaertgeerts' -from core import to_series, to_json_files, show_plot, set_display +from .core import to_series, to_json_files, show_plot, set_display class Chart(): diff --git a/charts/core.py b/charts/core.py index 5d02bf2..f5319a2 100644 --- a/charts/core.py +++ b/charts/core.py @@ -1,8 +1,8 @@ __author__ = 'arnoutaertgeerts' from string import Template -from jsonencoder import ChartsJSONEncoder -from server import url +from .jsonencoder import ChartsJSONEncoder +from .server import url import os import json @@ -22,7 +22,7 @@ def show_plot(html, saveHTML, show, async=False): return HTML(html) elif show == 'tab': - print 'Opening new tab...' + print('Opening new tab...') if async: address = url(async) webbrowser.open_new_tab(address) @@ -30,7 +30,7 @@ def show_plot(html, saveHTML, show, async=False): webbrowser.open_new_tab('file://' + os.path.realpath(saveHTML)) elif show == 'window': - print 'Trying to open a window. If this fails we will open a tab...' + print('Trying to open a window. If this fails we will open a tab...') if async: address = url(async) webbrowser.open_new(address) diff --git a/charts/data.py b/charts/data.py index 2b1341d..6a36c99 100644 --- a/charts/data.py +++ b/charts/data.py @@ -24,8 +24,8 @@ def df_to_series(df, display=[]): display[col] display[col]=True except KeyError: - print 'Warning\n','-'*7,'\n',' '*4,'"{}"'.format(col), 'will not be displayed.\nVariable not found in DataFrame.\n' - print 'Try one the following columns:\n{}'.format(list(df.columns)) + print('Warning\n','-'*7,'\n',' '*4,'"{}"'.format(col), 'will not be displayed.\nVariable not found in DataFrame.\n') + print('Try one the following columns:\n{}'.format(list(df.columns))) except TypeError: display = dict(zip(df.columns,[plot_columns for x in df])) for col in df: diff --git a/charts/plot.py b/charts/plot.py index fc4d2e3..1501659 100644 --- a/charts/plot.py +++ b/charts/plot.py @@ -1,10 +1,10 @@ __author__ = 'Arnout Aertgeerts' -from core import MyTemplate, to_json_files, to_series, clean_dir, set_display, show_plot, make_dir, remove_quotes -from jsonencoder import ChartsJSONEncoder -from chart import Chart -from server import address -from settings import default_settings, load_options, default_options +from .core import MyTemplate, to_json_files, to_series, clean_dir, set_display, show_plot, make_dir, remove_quotes +from .jsonencoder import ChartsJSONEncoder +from .chart import Chart +from .server import address +from .settings import default_settings, load_options, default_options import os import json diff --git a/charts/server.py b/charts/server.py index f93abd1..aea40a5 100644 --- a/charts/server.py +++ b/charts/server.py @@ -1,7 +1,12 @@ __author__ = 'Arnout Aertgeerts' -import SimpleHTTPServer -import SocketServer +import sys +if sys.version_info.major == 3: + import http.server as SimpleHTTPServer + import socketserver as SocketServer +else: + import SimpleHTTPServer + import SocketServer import os import json @@ -42,7 +47,7 @@ def run_server(): server = ChartServer(address, ChartRequestHandler) ip, port = server.server_address # find out what port we were given - print 'Server running in the folder {0} at {1}:{2}'.format(os.path.abspath(os.getcwd()), ip, port) + print('Server running in the folder {0} at {1}:{2}'.format(os.path.abspath(os.getcwd()), ip, port)) t = threading.Thread(target=server.serve_forever) t.setDaemon(True) # don't hang on exit diff --git a/charts/settings.py b/charts/settings.py index a47212f..f42dea1 100644 --- a/charts/settings.py +++ b/charts/settings.py @@ -21,5 +21,5 @@ def load_options(path): with open(path, 'r') as json_file: return json.loads(json_file.read()) except IOError: - print 'No options file found. Did you spell the name correctly?' + print('No options file found. Did you spell the name correctly?') return dict() \ No newline at end of file