How to use variables across modules

J

juel4700

Im a newbee at python, and im trying to figure out how to use variables and setups across modules.

Am I right when i think its smart to keep seperate functions of a program in seperate modules?

I have a main program module called main.py and in that main.py i have this:

# Sets GPIO's to HIGH = Relays OFF
try:
import RPi.GPIO as GPIO
except RuntimeError:
Print("Error importing RPi.GPIO!!")

GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
# GPIO16 is relay1
GPIO.setup(16, GPIO.OUT, initial=GPIO.HIGH)
# GPIO11 is relay2
GPIO.setup(11, GPIO.OUT, initial=GPIO.HIGH)

I then import a module (in that module1 i have a function called Relay) and try to use the function with module1.Relay(1,1)

But the function in module1 needs the GPIO from the main.py to Work. How do I go about with this? I really dont want the GPIO setting part in the module1, I dont want it to be run everytime I run the module1.Relay(1,1) call..

What is best practice for working across modules. (im making a controller for my house' heat system, so it would be nice, if I can do this the right way, the first time.)

Im and experienced vbs and php coder, but a real newbe when it comes to python ;)

I Really hope you Guys will lead me in the right direction..

Kind regards Juel
 
C

Chris Angelico

I have a main program module called main.py and in that main.py i have this:

# Sets GPIO's to HIGH = Relays OFF
try:
import RPi.GPIO as GPIO
except RuntimeError:
Print("Error importing RPi.GPIO!!")

GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
# GPIO16 is relay1
GPIO.setup(16, GPIO.OUT, initial=GPIO.HIGH)
# GPIO11 is relay2
GPIO.setup(11, GPIO.OUT, initial=GPIO.HIGH)


But the function in module1 needs the GPIO from the main.py to Work. How do I go about with this? I really dont want the GPIO setting part in the module1, I dont want it to be run everytime I run the module1.Relay(1,1) call..

I don't know what GPIO is, but I suspect that you'll do best to simply
import it in module1 same as you do in main. You'll get the same
module (Python caches imported modules), so any configuration done
will apply to both.

But you might not need to separate out your modules. It's quite
alright to have a fairly large main module in Python, with lots of
functions. You'd have to figure that out separately, as a design
question.

A few other points about your code, if I may!

1) Is Print a custom function of yours? If not, please be more careful
with copying and pasting code - it makes a lot of difference.

2) If RPi.GPIO can't be imported, you emit a message and (presumably)
keep going. That's pretty useless - your script will bomb with a
NameError as soon as it tries to use GPIO. Better to simply not catch
anything around that import, and if there's a problem, the exception
will be displayed (usefully!) and the script terminated. "Have you not
heard the expression 'Never test for an error condition you don't know
how to handle'?" [1]

3) You're using Google Groups, which means your post is misformatted -
and which means that replies will be, unless you specifically work to
prevent it, very much misformatted and ugly. Please don't do that. If
you must use Google Groups, please read
https://wiki.python.org/moin/GoogleGroupsPython and follow it; but due
to the many problems with GG, there are a number of people who simply
killfile every post from there, so they won't even see it. Switching,
either to a better newsreader or to email, will solve all those
problems at once.

[1] http://www.theregister.co.uk/2008/10/24/bofh_2008_episode_34/

ChrisA
 
S

Steven D'Aprano

Im a newbee at python, and im trying to figure out how to use variables
and setups across modules. [...]
What is best practice for working across modules.

Depends on whether you are working on a framework, a library or an
application.

Frameworks are monstrously large and complex beasts, not for beginners,
so I won't discuss them.

Libraries are collections of re-usable code. In general, a library will
often be in a single module. For example, the math library, which
contains a collection of re-usable, related functions like sin, cos, tan,
sqrt, etc.

Some code in an application is re-usable. For instance, if your
application needs to download data from the Internet, well, lots of apps
need to download data from the Internet, so it makes sense to pull out
the download-data-from-the-Internet parts of your application and put
them into a library module. But the rest of the app is likely to be too
specific to bother. In that case, it can stay within a single file, at
least until such point that it grows too large and unwieldy, in which
case you can start pulling bits out into separate files.

How large is "too large" is a matter of taste, but personally I consider
the decimal module to be about as large as a single file should get:
about 6000-7000 lines, including comments and blank lines:

http://hg.python.org/cpython/file/3.3/Lib/decimal.py


Regardless of what you are doing, best practice is that closely related
code should go together (in the same function, the same class, or the
same module) and unrelated code should be separate (in a separate
function, class or module).

Another thing to consider: best practice is to avoid using global
variables. If you must use them -- and you almost never *need* them --
they should be specific to a single module. But better is to not use
global variables at all. For example, code like this:


value = "something" # set the global
function() # call a function
print(result) # which reads the global "value", and sets global "result"


should be avoided like the plague. Better is to write your functions to
take input arguments, and return results:

result = function("something")
print(result)


One advantage -- one out of many -- is that if you write your functions
like this, the question of how to use variables across modules is
irrelevant. The right answer to the question "What's the best way to
manage global variables across modules?" is "Don't".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top