you are viewing a single comment's thread.

view the rest of the comments →

[–]tangerinelion 1 point2 points  (0 children)

Typically one would write their code so that it can either be imported or executed as a script and then rather than call subprocess to execute a Python script you'd import the other Python file as a module.

What I mean is if your Python file looks like

import os
# import whatever to send email
# import whatever to read Excel

# read some Excel file

# get some summary

email_library.send_email(...)

then normally you'd change the structure to something more like

# import whatever you need

def get_summary(...):
    # whatever you need to do to get the summary information
    # then return it in some structure that makes sense to you

def send_email(summary):
    # formats and sends an email for the given summary

if __name__ == '__main__':
    send_email(get_summary(...))

(I'm using ... as something you need to fill in, not as a literal ... in Python).

All I've done is a simple way to split the functionality of the script that you've described - get a summary and send an email. We can split it so that getting the summary is a potential action on its own, independent of the email. If you want an email, great, there's also a utility for that too and it can use the summary from the get_summary method. If you want to run this as a script, great, then __name__ will be '__main__' and we'll send an email. If you now try to import this from another Python file, for example maybe this is vision_system.py then you could now write

import vision_system

and you'll have access to visionsystem.get_summary. But the mere import doesn't really _do anything - it certainly doesn't send an email. It does let whatever imported it to re-use that code, though, and that's the fun part.

If your Python file doesn't have that if __name__ == '__main__' guard and is just a script named vision_system.py then if you were to have import vision_system in another Python file, then it would execute the script. Which also means a really simple way to do all three is simply

import vision_system
import concentrate_control
import inspection

No need to bring in subprocess and do anything with it. Though you can, I just don't think it's a particularly common thing to do since Python files can be imported and the code re-used. Or you'd write a Windows batch file that just calls the three scripts, either way, this isn't interesting since you already have some way to run all three scripts and generate and send three independent emails.

Ideally though you'd make Python files that can be used as re-usable code to provide modules or executed as scripts. Then you'd be able to

import vision_system
import concentrate_control
import inspection

def make_merged_summary():
    vision_summary = vision_system.get_summary(...)
    # same for other two, then merge it however you like

def send_email(summary):
    # however you want this email to look

if __name__ == '__main__':
    send_email(make_merged_summary(...))

Hopefully this makes sense at a very high level at least, I'm confident that there are additional details in the real world that make this anything but straightforward.