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

Add audeer.script_dir() #155

Merged
merged 9 commits into from
Jul 19, 2024
Merged

Add audeer.script_dir() #155

merged 9 commits into from
Jul 19, 2024

Conversation

hagenw
Copy link
Member

@hagenw hagenw commented Jul 18, 2024

Very often, I add the following to the top of a script:

CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))

and I need to google it every time.

To make it easier to remember, this pull request proposes to add audeer.script_dir() which can be used instead.
When called in an interactive session, it returns the directory the user is located in.

image

Copy link

codecov bot commented Jul 18, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.0%. Comparing base (b24d6c7) to head (66fc3c5).

Additional details and impacted files
Files Coverage Δ
audeer/__init__.py 100.0% <100.0%> (ø)
audeer/core/io.py 100.0% <100.0%> (ø)

@hagenw hagenw marked this pull request as draft July 18, 2024 09:00
@frankenjoe
Copy link
Collaborator

frankenjoe commented Jul 18, 2024

Haha, just this morning I had the very same idea that such a function would be a good extension to audeer :)

@hagenw
Copy link
Member Author

hagenw commented Jul 18, 2024

Hehe, great, I still wonder why I hadn't the idea earlier than today.

@hagenw hagenw marked this pull request as ready for review July 18, 2024 09:24
@hagenw hagenw requested a review from frankenjoe July 18, 2024 09:25
audeer/core/io.py Outdated Show resolved Hide resolved
@frankenjoe
Copy link
Collaborator

I wonder if the name current_dir() could be misleading. A user might expect it returns the current working directory. So maybe we should rename to script_dir()?

@hagenw
Copy link
Member Author

hagenw commented Jul 18, 2024

I found current_dir() a nice fit, as the variable name in the script was always CURRENT_DIR. But you are right, inside a script it might be misleading as it returns the script dir. On the other hand, in an interactive session it returns the current working directory.
So, it is actually a mixture of script_dir() and current_working_dir(). A correct term might be caller_dir(), but I doubt that this name is a good choice.

@frankenjoe
Copy link
Collaborator

On the other hand, in an interactive session it returns the current working directory.

You have already documented the behavior when no script file exists, e.g. in an interactive session. So I think it is ok to still call it script_dir() or maybe current_script_dir() to align it with current_working_dir().

@hagenw hagenw changed the title Add audeer.current_dir() Add audeer.script_dir() Jul 19, 2024
@hagenw
Copy link
Member Author

hagenw commented Jul 19, 2024

I changed the name to audeer.script_dir() and extended the tests to check that a change in current working directory, does not affect audeer.script_dir() when called in a file.

@frankenjoe frankenjoe merged commit 0867cdb into main Jul 19, 2024
19 checks passed
@frankenjoe frankenjoe deleted the current-dir branch July 19, 2024 10:09
@hagenw hagenw mentioned this pull request Jul 23, 2024
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