Advice on spliiting code into modules

This is the place for queries that don't fit in any of the other categories.

Advice on spliiting code into modules

Postby micksulley » Tue Mar 26, 2013 9:33 pm

Hi,

I am writing some programs for controlling my heating system. I have a class which is rather large and is used by several programs so I thought it would be a good idea to place that in a separate file and include it in the programs which need it. The problem I have now hit is that global variables need to be defined in each file that references them, which introduces the possibility of errors if I change one and forget another.

So having a single definition of the class is an improvement but having multiple definitions of global variables is a definitie disadvantage.

Is there a better way to do this? Are there any guidelines on writing modular code?

Thanks
Mick
micksulley
 
Posts: 7
Joined: Tue Mar 26, 2013 9:08 pm

Re: Advice on spliiting code into modules

Postby Yoriz » Tue Mar 26, 2013 9:54 pm

That is why Global Variables are a bad idea, best thing to do is change your code so you don't have Global Variables, if you mean Constant variables you could put them in a separate module module of there own and import them into what ever module needs them. Put variables in one place so you then get them and change them in only one place.

Show some of your code and you might get some advise on a better way of doing what you are doing.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 782
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Advice on spliiting code into modules

Postby micksulley » Tue Mar 26, 2013 10:07 pm

Hi, thanks for the reply.
Yes they are constants rather than true global variables. I hadn't thought of having a separate module for them, makes good sense really.

I will give that a try and see how it goes. Thanks for your help,
Mick
micksulley
 
Posts: 7
Joined: Tue Mar 26, 2013 9:08 pm

Re: Advice on spliiting code into modules

Postby micksulley » Fri Mar 29, 2013 9:59 pm

I have hit a problem with this approach, can you give me any pointers on how to resolve this. Scenario is -

I have prog1.py, prog2.py, prog3.py, each imports
constants.py #which defines constants
classes.py #which defines classes, also imports constants

The problem is that I have a debug log file for each main program, this is written to from both the main program and also the classes. I want a different log file for each main program, so that is the place to define it, but then classes.py errors because the log file is not defined. Is there a way to overcome this?
micksulley
 
Posts: 7
Joined: Tue Mar 26, 2013 9:08 pm

Re: Advice on spliiting code into modules

Postby setrofim » Sat Mar 30, 2013 9:06 am

Use a logger (see this) and pass it to your classes on construction.
setrofim
 
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm

Re: Advice on spliiting code into modules

Postby micksulley » Sat Mar 30, 2013 11:54 am

Wow! That looks brilliant. I had developed something to create a log file with line numbers, time date etc, but this goes way beyond that, plus it is all standard functionality. I now need to do a lot of reading, learning and testing :)
Thanks for the advise
Mick
micksulley
 
Posts: 7
Joined: Tue Mar 26, 2013 9:08 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 3 guests