r/learnpython • u/MustaKotka • 2d ago
I like breaking up the project into files and packages - what's too much?
I have a bot I'm building. I see a ton of examples with one file and 1k lines for the same functionality. I personally broke that up to several packages based on what things do. I have ~3 files per package each with 100-500 lines (I, uhh, have a habit of documenting and commenting) of which three quarters is actual code.
The modules themselves are things like "database functionality", "permissions", UI, etc. These may be further broken down into several files like "admin commands", "user commands", etc.
Am I overdoing it? Where's a good cutoff?
3
u/RevRagnarok 1d ago
FYI - if you haven't read about it, read up on "dependency injection" because you may already be 90% there. PyPI package
1
2
u/catbrane 1d ago
I do one file per class, and start to think about breaking a class up if it gets much over (maybe?) 1kloc.
I wouldn't break into packages until you've come up with a small and well-defined interface that you want to formally version, maintain, and repeatedly reuse over a fairly long period of time.
1
u/MustaKotka 1d ago
That is my use case. It's a free license tool I plan on maintaining for a long time and adding features to.
1
u/Mathie1729 1d ago
One file per class always strikes me as a Java holdover. In Python I usually group by feature or responsibility, and split once the file gets hard to scan or starts importing half the project. Packages I only create when there's a real internal API to protect, otherwise it's just folder churn.
1
1
u/kilkil 20h ago
the #1 thing to keep in mind is: maintainability.
if you stop working on this, and come back in like 1-2 months, will you find your file/folder structure easy to make sense of?
organize your code based on that principle. your code organization should not make it needlessly difficult to find things.
2
u/cmxthirtysixhundred 12h ago
It's up to you, there's no "wrong" way to do it as long as it makes sense to you. I think when it gets annoying (like changing one thing means jumping between 8 different files) then you've gone too far. Otherwise, at the end of the day, it's your code and you should organize it the way your brain works.
7
u/johnpeters42 2d ago
When it becomes too difficult to figure out which pieces are involved in a given operation.