I have a situation as follows, I have a relative path that I want to get for a directory. The directory structure is as follows,
Windows Folder Structure
C:\FileFolder\LowerLevel\ThirdLevel\script.py
C:\FileFolder\FolderOfInterest\filesStuff.txt
Linux Structure
PathAbove/LowerLevel/ThirdLevel/script.py
PathAbove/FileFolder/FolderOfInterest/filesStuff.txt
Top Level
args = parse_args(sys.argv[1:])
main(args.filepath, args.repository)
Function Definitions
main(filepath,repopath)
{
//do stuff with filepath, repopath
do_stuff()
}
do_stuff()
path_to_repo = rel_path()
#use path_to_repo
def rel_path():
"""
Gets the relative path two directory levels up where FileFolder folder lives
"""
return os.path.abspath(os.path.join(os.path.dirname( __file__), '../../', 'FileFolder/FolderOfInterest'))
I have been asked to make this more general so I don't have to rely on the FileFolder name being 'FileFolder' in case somebody has it named differently.
I pass in the path directly to repopath at the start, so I could use that since it's validated before use. My usual solution is this
My Usual Solution
Pass repository_path from the top level.
main(args.filepath,args.repository)
repository_path = args.repository
do_stuff(repository_path)
rel_path(repository_path)
def rel_path(repository_path):
"""
Gets the relative path two directory levels up where galaxy folder lives
"""
return os.path.abspath(os.path.join(os.path.dirname( repository_path ), '/FileOfInterest'))
It comes up often that I need to pass in information from a higher level function to a lower level function, usually after I realize that info is needed and I want to refactor something to use it for whatever reason. This requires adding an extra argument to multiple functions and changing functionality slightly. There is actually a third function in-between this in my real code, but this illustrates the issue.
Here is my question
My question is this, is there a best practice for passing in information to a lower level function used only inside another function that isn't called by the main function? Or am I way off base here? Is there any easier way to get the relative path of FolderOfInterest
that I'm interested in getting? Typically I have historically programmed in procedural languages and this has come up plenty of times before in the past. But, it also comes up in OOP programming I've done before.
This comes up often enough that I thought it was worth asking here. How do I pass information around without requiring rework of multiple parts of my code when that information is embedded at a higher level than where it needs to be used? I'm trying to make this as agnostic as possible with relative paths, function arguments so it's easier for others to use/modify later, and so people using it don't have to make changes depending on whether they run it on Linux or Windows.
I hope I've written this somewhat clear and this is a useful enough question to be here. I originally had this on stackoverflow, but since it's more about best practices in software engineering, I put it here.
I've marked this as python because that's what I'm writing in, but this comes up just as much in c, matlab, c++, and other languages I've written in that I'm not necessarily tied to the answer being specific to python syntax