[Tutor] Module Architecture

Gordon Stewart gastew at gmail.com
Mon Aug 15 12:07:46 EDT 2022


I'd like some recommendations on how to design and layout a module.
There are any number of sources to help with discrete coding problems (on
this site for instance), but I'm interested in a higher level view on how
to assemble these discrete components into something easily maintained and
understood.  I'm a solo hobby developer with no external collaboration.

I have a working Python program I want to refactor.

   - It is run twice a month and produces membership cards and address
   labels for a member organization.
   - It has about 600 lines of code
   - It makes an API call to a central server to collect and update records
   on all recent new or renewed members.
   - It creates a couple of tab delimited files used by MS Word to print
   labels and membership cards.

At the moment it is a single module with data definitions grouped towards
the top, followed by many discrete functions followed by procedural code
which invokes the functions as necessary.
It uses functions rather than classes although I'd like to convert lists
and dictionaries to use @dataclass where appropriate.

What are your recommendations as to how to structure the refactored module?

My current thinking is that I'll have three files:

   - my_data.py which contains all lists, dicts, dataclasses.
   - my_funcs.py which for all function definitions.  Where possible
   functions will have calling arguments and will 'return' their output
   without modifying any external data.
   - my_procedures.py which contains all the logic to run the functions in
   my_funcs and update data in my_data.

Is this a "Best Procedures" approach or do you recommend something else?

If this is the way to go, I have a question about the my_funcs file.  Some
of the functions are complex and I'd like to test them on a 'stand alone'
basis.  I could use an ' if __name__ == "__main__ :" '  approach but only
if the function is in its own file.  Should I split some or all functions
into their own files or is there a better way to do it?

I use VSCode for editing and testing.

Thanks for any guidance you can provide.

Gordon S.


More information about the Tutor mailing list