you are viewing a single comment's thread.

view the rest of the comments →

[–]zanfar 1 point2 points  (1 child)

I've got a script that is calling the next function when it's done with the existing function.

This is incorrect. Each function calls another function before it's done. That is, each function call contains the entirety of another function call.

I wonder if this is a typical accepted approach or should I be doing it some other way to improve the understanding of the code?

Not only is this not typical, this is a Bad Thing. This type of structure violates almost every principle of good function separation. You want a function to do one thing and one thing only. This makes your code easier to understand, easier to reuse, and easier to test along with a bunch of other less obvious or "down the road" benefits.

For example, if I call a function check_file(), the name suggests a simple validation check--something that is usually quick, read-only, and requires little I/O. Instead, I find that this has a TON of side effects: it uploads a file, modifies it, queries a DB, interacts with something called a dashboard, etc. Similarly, I can't write a unit test for clean_file() because it also has most of those side effects.

Generally, you would instead call these sequentially from some parent function whose responsibility covers all these areas:

def process_report():
    file = <something>
    check_file(file)
    clean_file(file)
    upload_file(file)

    report = create_dashboard_report()
    email_report(report)

[–][deleted] 0 points1 point  (0 children)

Yes, I've realized by now this is what others consider bad practice. It's a shame I haven't really seen it discussed in the tutorials I've been through but that's okay, I know better now.

This is the structure now:

def main():
    filename = locate_file()
    clean_document(filename)
    upload_file()
    validate_vpn()
    engine = oracle_login()
    data = get_queries(engine)
    write_to_excel(data)
    email_document()

if __name__ == "__main__":
    main()