Skip to content

Commit

Permalink
add helper to move between tuple and list for dcc.store/freq table
Browse files Browse the repository at this point in the history
  • Loading branch information
interrogator committed Aug 21, 2019
1 parent a1278e0 commit c9a60d3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
27 changes: 17 additions & 10 deletions buzzword/parts/explore.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
_get_specs_and_corpus,
_translate_relative,
_update_datatable,
_get_table_for_chart,
_tuple_or_list,
)
from buzzword.parts.strings import (
_make_search_name,
Expand Down Expand Up @@ -90,7 +90,11 @@ def _new_chart(n_clicks, table_from, chart_type, top_n, transpose, session_table
if n_clicks is None:
raise PreventUpdate
# get correct dataset to chart
df = _get_table_for_chart(table_from, session_tables, FREQUENCY_TABLES)

this_table = session_tables[str(table_from)]
this_table = _tuple_or_list(this_table, tuple)
df = FREQUENCY_TABLES[_tuple_or_list(this_table[:6], tuple)]

# transpose and cut down items to plot
if transpose:
df = df.T
Expand Down Expand Up @@ -254,7 +258,7 @@ def _new_search(

this_search += [new_value, len(df), list(df["_n"])]
if found_results:
session_search[new_value] = this_search
session_search[new_value] = _tuple_or_list(this_search, list)
corpus = CORPORA[slug]
df = df.iloc[:max_row, :max_col]
current_cols, current_data = _update_datatable(
Expand Down Expand Up @@ -376,26 +380,29 @@ def _new_table(
((k, v) for k, v in session_tables.items() if this_table[:6] == v[:6]),
(False, False),
)
if exists is not False:
exists_as_tuple = _tuple_or_list(exists, tuple)

# if we are updating the table:
if updating:
table = FREQUENCY_TABLES[tuple(exists[:6])]
table = FREQUENCY_TABLES[exists_as_tuple]
exists[-1] += 1
# fix rows and columns
table = table[[i["id"] for i in current_cols[1:]]]
table = table.loc[[i["_" + table.index.name] for i in current_data]]
# store again
exists = _tuple_or_list(exists, list)
session_tables[key] = exists
FREQUENCY_TABLES[tuple(exists[:6])] = table
FREQUENCY_TABLES[exists_as_tuple] = table
elif exists:
msg = "Table already exists. Switching to that one to save memory."
table = FREQUENCY_TABLES[tuple(exists[:6])]
table = FREQUENCY_TABLES[exists_as_tuple]
# if there was a validation problem, juse use last table (?)
elif msg:
if session_tables:
# todo: figure this out...use current table instead?
key, value = list(session_tables.items())[-1]
table = FREQUENCY_TABLES[tuple(value[:6])]
table = FREQUENCY_TABLES[_tuple_or_list(value[:6], tuple)]
# todo: more here?
else:
table = INITIAL_TABLES[slug]
Expand All @@ -416,9 +423,9 @@ def _new_table(
if isinstance(relative, pd.DataFrame):
relative = None

# make show a tuple, then store the search information
this_table[1] = tuple(this_table[1])
session_tables[idx] = this_table
# then store the search information in store/freq table spaces
session_tables[idx] = _tuple_or_list(this_table, list)
FREQUENCY_TABLES[_tuple_or_list(this_table, tuple)] = table

if updating:
cols, data = current_cols, current_data
Expand Down
19 changes: 13 additions & 6 deletions buzzword/parts/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,19 @@ def _get_specs_and_corpus(search_from, searches, corpora, slug):
return exists, corpora[slug].iloc[exists[-1]]


def _get_table_for_chart(table_from, tables, freq_tables):
"""
Get the table that needs to be charted
"""
this_table = tables[str(table_from)]
return freq_tables[tuple(this_table[:6])]
def _tuple_or_list(this_identifier, typ):
"""
Turn all lists to tuple/list so this becomes hashable/dcc.storable
"""
opposite = tuple if typ == list else list
out = []
for i in this_identifier:
if isinstance(i, opposite):
i = _tuple_or_list(i, typ)
out.append(typ(i))
else:
out.append(i)
return typ(out)


def _translate_relative(inp, corpus):
Expand Down

0 comments on commit c9a60d3

Please sign in to comment.