def table_definition(table_name): """ Get the source of a table function. If a table is registered DataFrame and not a function then all that is returned is {'type': 'dataframe'}. If the table is a registered function then the JSON returned has keys "type", "filename", "lineno", "text", and "html". "text" is the raw text of the function, "html" has been marked up by Pygments. """ if orca.table_type(table_name) == 'dataframe': return jsonify(type='dataframe') filename, lineno, source = \ orca.get_raw_table(table_name).func_source_data() html = highlight(source, PythonLexer(), HtmlFormatter()) return jsonify(type='function', filename=filename, lineno=lineno, text=source, html=html)
def orca_dataframe_tables(): """ Return a list of the neames of all currently registered dataframe tables """ return [ name for name in orca.list_tables() if orca.table_type(name) == 'dataframe' ]
def add_table(table_name, table, replace=False): """ Add new table and raise assertion error if the table already exists. Silently replace if replace=True. """ if not replace and orca.is_table(table_name) and orca.table_type(table_name) == 'dataframe': logger.warning("inject add_table replacing existing table %s" % table_name) assert False # FIXME - should add table.copy() instead, so it can't be modified behind our back? return orca.add_table(table_name, table, cache=False)
def table_definition(table_name): """ Get the source of a table function. If a table is registered DataFrame and not a function then all that is returned is {'type': 'dataframe'}. If the table is a registered function then the JSON returned has keys "type", "filename", "lineno", "text", and "html". "text" is the raw text of the function, "html" has been marked up by Pygments. """ if orca.table_type(table_name) == "dataframe": return jsonify(type="dataframe") filename, lineno, source = orca.get_raw_table(table_name).func_source_data() html = highlight(source, PythonLexer(), HtmlFormatter()) return jsonify(type="function", filename=filename, lineno=lineno, text=source, html=html)
def assert_table_can_be_generated(table_name): """ Does a registered table exist as a DataFrame? If a table was registered as a function wrapper, this assertion evaluates the function and fails is there are any errors. In other UrbanSim code, it seem like the accepted way of triggering a table to be evaluated is to run .to_frame() on it. I'm using ._call_func() instead, because I don't need the output and this saves the overhead of copying the DataFrame. Either of those methods will be aware of caching, and not regenerate the table if it already exists. There no way to tell externally whether a table is cached or not. That might be a useful thing to add to the orca API. """ assert_table_is_registered(table_name) if (orca.table_type(table_name) == 'function'): try: _ = orca.get_raw_table(table_name)._call_func() except: msg = "Table '%s' is registered but cannot be generated" % table_name raise OrcaAssertionError(msg) return