Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle lack of bokeh package #1

Merged
merged 6 commits into from
Oct 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions taxcalc/policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,20 +204,20 @@ def read_json_reform_text(text_string):
implement_reform(reform_dict) method (see below).
"""
# strip out //-comments without changing line numbers
json_without_comments = re.sub('//.*', '', text_string)
json_without_comments = re.sub('//.*', ' ', text_string)
# convert JSON text into a dictionary with year skeys as strings
try:
reform_dict_raw = json.loads(json_without_comments)
except ValueError:
msg = 'Policy reform text contains invalid JSON'
txt = ('\nTO FIND FIRST JSON SYNTAX ERROR,\n'
'COPY TEXT BETWEEN LINES AND '
'PASTE INTO BOX AT jsonlint.com\n')
line = '----------------------------------------------------------'
txt += line + '\n'
txt += json_without_comments.strip() + '\n'
txt += line + '\n'
raise ValueError(msg + txt)
except ValueError as valerr:
msg = 'Policy reform text below contains invalid JSON:\n'
msg += str(valerr) + '\n'
msg += 'The invalid JSON reform text is between the lines:\n'
line = '---------|---------|---------|'
line += '---------|---------|---------|'
msg += line + '\n'
msg += json_without_comments + '\n'
msg += line + '\n'
raise ValueError(msg)
return Policy.convert_reform_dictionary(reform_dict_raw)

def implement_reform(self, reform):
Expand Down
14 changes: 14 additions & 0 deletions taxcalc/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,20 @@ def test_mtr_plot(records_2009):
plot = mtr_plot(source)


def test_mtr_plot_force_no_bokeh(records_2009):
import taxcalc
taxcalc.utils.BOKEH_AVAILABLE = False
pol = Policy()
behv = Behavior()
calc = Calculator(policy=pol, records=records_2009, behavior=behv)
calc.calc_all()
source = get_mtr_data(calc, calc, weights=wage_weighted,
complex_weight=True)
with pytest.raises(RuntimeError):
plot = mtr_plot(source)
taxcalc.utils.BOKEH_AVAILABLE = True


def test_multiyear_diagnostic_table(records_2009):
pol = Policy()
behv = Behavior()
Expand Down
38 changes: 30 additions & 8 deletions taxcalc/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
import pandas as pd
from pandas import DataFrame
from collections import defaultdict, OrderedDict
from bokeh.models import Plot, Range1d, ImageURL, DataRange1d
from bokeh.embed import components
from bokeh.layouts import layout
from bokeh.palettes import Blues4, Reds4
from bokeh.plotting import figure, hplot, vplot, output_file, show
from bokeh.models import (ColumnDataSource, LogAxis, LinearAxis, Rect,
FactorRange, CategoricalAxis, Line, Text, Square,
HoverTool)

try:
import bokeh
BOKEH_AVAILABLE = True
from bokeh.palettes import Blues4, Reds4
from bokeh.plotting import figure

except ImportError:
BOKEH_AVAILABLE = False
#

STATS_COLUMNS = ['_expanded_income', 'c00100', '_standard',
'c04470', 'c04600', 'c04800', 'c05200', 'c62100', 'c09600',
Expand Down Expand Up @@ -733,6 +735,26 @@ def get_mtr_data(calcX, calcY, weights, MARS='ALL',
return merged


def requires_bokeh(fn):
"""
Decorator for functions that require bokeh.
If BOKEH_AVAILABLE=True, this does nothing.
IF BOKEH_AVAILABEL=False, we raise an exception and tell the caller
that they must install bokeh in order to use the function.
"""
def wrapped_f(*args, **kwargs):
if BOKEH_AVAILABLE:
return fn(*args, **kwargs)
else:
msg = ("`bokeh` is not installed. Please install "
"`bokeh` to use this package (`conda install "
"bokeh`)")
raise RuntimeError(msg)

return wrapped_f


@requires_bokeh
def mtr_plot(source, xlab='Percentile', ylab='Avg. MTR', title='MTR plot',
plot_width=425, plot_height=250, loc='top_left'):
"""
Expand Down