Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
136 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from instructor import * | ||
from instructor_utility import * | ||
|
||
def files_not_handled_correctly(*filenames): | ||
if filenames and isinstance(filenames[0], int): | ||
num_filenames = filenames[0] | ||
actual_filenames = False | ||
else: | ||
num_filenames = len(filenames) | ||
actual_filenames = True | ||
ast = parse_program() | ||
calls = ast.find_all("Call") | ||
called_open = [] | ||
closed = [] | ||
for a_call in calls: | ||
if a_call.func.ast_name == 'Name': | ||
if a_call.func.id == 'open': | ||
if not a_call.args: | ||
gently("You have called the <code>open</code> function without any arguments. It needs a filename.") | ||
return True | ||
called_open.append(a_call) | ||
elif a_call.func.id == 'close': | ||
explain("You have attempted to call <code>close</code> as a function, but it is actually a method of the file object.") | ||
return True | ||
elif a_call.func.ast_name == 'Attribute': | ||
if a_call.func.attr == 'open': | ||
gently("You have attempted to call <code>open</code> as a method, but it is actually a built-in function.") | ||
return True | ||
elif a_call.func.attr == 'close': | ||
closed.append(a_call) | ||
|
||
if len(called_open) < num_filenames: | ||
gently("You have not opened all the files you were supposed to.") | ||
return True | ||
elif len(called_open) > num_filenames: | ||
gently("You have opened more files than you were supposed to.") | ||
return True | ||
withs = ast.find_all("With") | ||
if len(withs) + len(closed) < num_filenames: | ||
gently("You have not closed all the files you were supposed to.") | ||
return True | ||
elif len(withs) + len(closed) > num_filenames: | ||
gently("You have closed more files than you were supposed to.") | ||
return True | ||
if actual_filenames: | ||
ensure_literal(*filenames) | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from instructor import * | ||
|
||
def ensure_imports(*modules): | ||
ast = parse_program() | ||
for module in modules: | ||
imports= ast.find_all("Import") | ||
import_froms = ast.find_all("ImportFrom") | ||
if not imports and not import_froms: | ||
gently("You need to import the <code>{}</code> module!".format(module)) | ||
return True | ||
success = False | ||
if imports: | ||
if any(alias._name == module | ||
for i in imports | ||
for alias in i.names): | ||
success = True | ||
if import_froms: | ||
if any(i.module == module for i in import_froms): | ||
success = True | ||
if not success: | ||
gently("You need to import the <code>{}</code> module.".format(module)) | ||
return True | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.