Where should I put helper functions for scripts that utilize a common module?
I've been developing a set of Python scripts that, to keep things simple, manipulate a set of input files using a parser that I wrote specifically for those scripts. The project's design generally looked like this:
/project/project/__init__.py
/project/project/parser.py
/project/project/helperfunctions.py
/project/preferences.ini
/project/script1.py
/project/script2.py
Each script calls certain functions from helperfunctions.py
for tasks like reading the preferences file -- code which would otherwise have to be duplicated for each script. The helper functions are not used from the parser at all.
But now I've decided that I want to gear the project toward the parser itself, while still keeping the scripts off to the side. So now the project looks like this:
/project/scripts/preferences.ini
/project/scripts/script1.py
/project/scripts/script2.py
/project/parser.py
/project/setup.py
I think this is a much cleaner design, but the problem is that I'm not sure where to put those helper functions anymore. The ideas I've come up with are:
setup.py
install helperfunctions.py
alongside the parser. I'm opposed to this because the helper functions just provide convenience methods that aren't really related to the parser. /project/scripts/
, sidestepping installation. I don't like this idea because that would suggest helperfunctions.py
is a script itself. What's the most Pythonic way to solve this problem?
One option is putting helper functions in __init__.py
, hence you can just call them later from the parse module?
I don't know how complex your functions are... They are just functions or maybe helper class(es)?
The way to think is from the angle of user (of the parser and those helper function). How will you (or whoever) call those functions?
链接地址: http://www.djcxy.com/p/54164.html上一篇: 为什么我不能使用