def prevent_incorrect_plt(): ast = parse_program() plts = [n for n in ast.find_all("Name") if n.id == 'plt'] if plts and def_use_error(plts[0]): explain( "You have imported the <code>matplotlib.pyplot</code> module, " "but you did not rename it to <code>plt</code> using " "<code>import matplotlib.pyplot as plt</code>.<br><br><i>(plt_rename_err)<i>", 'verifier') return True matplotlib_names = [ 'plot', 'hist', 'scatter', 'title', 'xlabel', 'ylabel', 'show' ] for name in matplotlib_names: for n in ast.find_all("Name"): if n.id == name: if def_use_error(n): explain(("You have attempted to use the MatPlotLib " "function named <code>{0}</code>. However, you " "imported MatPlotLib in a way that does not " "allow you to use the function directly. I " "recommend you use <code>plt.{0}</code> instead, " "after you use <code>import matplotlib.pyplot as " "plt</code>.<br><br><i>(plt_wrong_import)<i>" ).format(name), 'verifier') return True return False
def list_not_initialized_on_run(): match = find_match("for ___ in _item_:\n pass") if match: _item_ = match["_item_"][0].astNode if def_use_error(_item_): explain("The list in your for loop has not been initialized<br><br><i>(no_list_init)<i></br>") return True return False
def list_initialization_misplaced(): match = find_match("for ___ in _item_:\n pass") if match: _item_ = match["_item_"][0].astNode if data_state(_item_).was_type('list') and def_use_error(_item_): explain("Initialization of <code>{0!s}</code> is a list but either in the wrong place or redefined" "<br><br><i>(list_init_misplaced)<i></br>".format(_item_.id)) return True return False
def list_initialization_misplaced(): message = "Initialization of <code>{0!s}</code> is a list but either in the wrong place or redefined" code = "list_init_misplaced" tldr = "Iterating over Non-list" match = find_match("for ___ in _item_:\n pass") if match: _item_ = match["_item_"][0].astNode if data_state(_item_).was_type('list') and def_use_error(_item_): return explain_r(message.format(_item_.id), code, label=tldr) return False
def list_not_initialized_on_run(): message = "The list in your for loop has not been initialized." code = "no_list_init" tldr = "List Variable Uninitialized" match = find_match("for ___ in _item_:\n pass") if match: _item_ = match["_item_"][0].astNode if def_use_error(_item_): return explain_r(message, code, label=tldr) return False
def condition(self): ast = parse_program(report=self.report) matplotlib_names = ['plot', 'hist', 'scatter', 'title', 'xlabel', 'ylabel', 'show'] for name in matplotlib_names: for n in ast.find_all("Name"): if n.id == name: if def_use_error(n): self.fields['actual'] = name self.fields['expected'] = "plt." + name return True return False
def prevent_incorrect_plt(): ast = parse_program() plts = [n for n in ast.find_all("Name") if n.id == 'plt'] if plts and def_use_error(plts[0]): explain("You have imported the <code>matplotlib.pyplot</code> module, " "but you did not rename it to <code>plt</code> using " "<code>import matplotlib.pyplot as plt</code>.<br><br><i>(plt_rename_err)<i>", 'verifier') return True matplotlib_names = ['plot', 'hist', 'scatter', 'title', 'xlabel', 'ylabel', 'show'] for name in matplotlib_names: for n in ast.find_all("Name"): if n.id == name: if def_use_error(n): explain(("You have attempted to use the MatPlotLib " "function named <code>{0}</code>. However, you " "imported MatPlotLib in a way that does not " "allow you to use the function directly. I " "recommend you use <code>plt.{0}</code> instead, " "after you use <code>import matplotlib.pyplot as " "plt</code>.<br><br><i>(plt_wrong_import)<i>").format(name), 'verifier') return True return False
def condition(self): ast = parse_program(report=self.report) plts = [n for n in ast.find_all("Name") if n.id == 'plt'] if plts and any(def_use_error(plt) for plt in plts): return True return False