# Python Etiquette for Functions in Multiple Files

Here's the context: My code is meant to automate testing of OCR (Image to Text programs) for a science project which could easily be read and understood my others. The code includes sets of functions which serve different purposes. Basically, there are functions which deal with the drawing and saving of pages (drawtext(), createpage(), etc.), functions which run various OCR on an image (tesseractocr(), etc.) and a function which computes the difference between two strings (levenshtein()).

Having all these functions in main.py results, in my opinion, in a cluttered piece of code. Is it better generally to:

1. Keep all those functions in main.py so that the entire code is in one file.
2. Put all these functions in one functions.py file
3. Put each set of functions in a file, resulting in a levenshtein.py, ocr.py, and pagemaker.py in addition to the main.py.

Honestly, I feel that either 2 or 3 is the choice to make, I'm just not sure. Additionally, what level of abstraction would you usually want in a main.py file? Should the file look like

import ocr, levenshtein, pagemaker
import "a few other things"
define vars, etc

for page in pages:
correct = pagemaker.drawpage(input1,input2,(input3,input4),etc) #also saves image
tesseract_out = ocr.tesseract("img.png")
tesseract_dist = levenshtein.levenshtein(correct, tesseract_out)
print(f"Tesseract Distance: {tesseract_dist}")
#etc for other ocr


Or is that too abstract? Also, is command-line argument parsing (which I didn't include above) generally in the main.py file or should you simply make a file called parseocrargs.py and run

pages, color, etc = parseocrargs.argparse(aruments grabbed earlier)

I guess the main question(s) are: Should a standalone (doesn't interact with other functions) but lengthy/ugly function go in a separate file than the other functions? Should almost all functions not be in main.py? If not, how many should be? Should main.py be extremely abstracted? If not, how abstracted should it be?

• @Linny I posted it on StackOverflow and was told it was a better question for CodeReview – Theo Dec 31 '19 at 4:45
• If that's the case, do you mind posting the code in question? – Linny Dec 31 '19 at 4:46
• @Linny I can post the whole code dump in my question, is that okay? – Theo Dec 31 '19 at 4:48
• @Linny The main reason I didn't dump the whole code is because only about 2/3 of it is done/in function form. The whole bottom of main.py is just a test of each function, and the arg parsing is still at the top without being in a function. – Theo Dec 31 '19 at 4:51
• We have a character limit of just over 65k for questions. If it fits (code + current description), feel free to post it. But I'm not sure you want a review. I think you're really asking a design question here, which would fit better elsewhere. But it doesn't feel right to redirect you again (don't mind the people from Stack Overflow, half the time they don't have a clue what we do and don't do), so we can review the code with your concerns in mind. Hint: if the code is long enough, the best answer is never option 1. – Mast Dec 31 '19 at 9:00

• One more question - If the main.py file only needs to call the create_page() function from drawimage.py, should I only from drawimage import create_page() since that function calls the other functions in its file by itself, or do i still import everything? – Theo Dec 31 '19 at 18:00
• Only import what you need (but don't include the () in the import statement, that's not how that works). In addition, if the other functions in that module are "private", you should prefix their names with underscores (e.g. _private_function()) to indicate that they're not meant to be imported or used elsewhere. – Sam Stafford Dec 31 '19 at 20:37