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

Modify load_feature_tables() function to be also used in a Notebook setting #58

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

vishpillai123
Copy link

@vishpillai123 vishpillai123 commented Jan 24, 2025

I was running into issues with __file__ in a Jupyter notebook. I found that the issue was happening because typically __file__ is defined in a script or module setting, but not in a notebook. Also added a unit test.

changes

  • Imported os package
  • Added try/catch whether module is being used in a notebook or in a script setting

context

  • I ran into an issue using this function for one of my schools.
  • This can be imported directly in our inference notebooks
  • Otherwise, I am running into the following error: "NameError: __file__ is not defined".

questions

None

I was running into issues with __file__ in a Jupyter notebook. I found that the issue was happening because typically __file__ is defined in a script or module setting, but not in a notebook. Also added a unit test.
@vishpillai123 vishpillai123 removed the request for review from bdewilde January 24, 2025 19:59
Copy link
Member

@bdewilde bdewilde left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you show me an example of where the current code doesn't work?

)
except NameError:
# If __file__ is not available (i.e. Jupyter notebook), use the current working directory
pkg_root_dir = pathlib.Path(os.getcwd())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pkg_root_dir = pathlib.Path(os.getcwd())
pkg_root_dir = pathlib.Path.cwd()

if p.parts[-1] == "student_success_tool"
)
except NameError:
# If __file__ is not available (i.e. Jupyter notebook), use the current working directory
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm very surprised by this! I thought __file__ refers to the path to this module, no matter the env in which it's being imported. In fact I don't believe I've ever had this issue in a Notebook.

if toml_content:
toml_file = tmpdir.join("features_table.toml")
toml_file.write(toml_content)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: remove whitespace line

Comment on lines +136 to +138
if expect_exception:
with pytest.raises(expect_exception):
utils.load_features_table(file_path)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the case of "no error", standard practice is to use from contextlib import nullcontext as does_not_raise then with pytest.raises(does_not_raise) works in the same manner as an exception. You can find a couple examples of this pattern in other unit tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants