Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #58348 > unrolled thread
| Started by | juel4700@gmail.com |
|---|---|
| First post | 2013-11-02 14:33 -0700 |
| Last post | 2013-11-03 05:48 +0000 |
| Articles | 4 — 4 participants |
Back to article view | Back to comp.lang.python
How to use variables across modules juel4700@gmail.com - 2013-11-02 14:33 -0700
Re: How to use variables across modules Chris Angelico <rosuav@gmail.com> - 2013-11-03 09:08 +1100
Re: How to use variables across modules Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-02 22:32 +0000
Re: How to use variables across modules Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-11-03 05:48 +0000
| From | juel4700@gmail.com |
|---|---|
| Date | 2013-11-02 14:33 -0700 |
| Subject | How to use variables across modules |
| Message-ID | <7a0df271-0961-4f0e-8aac-bb9ecd1bf011@googlegroups.com> |
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
[toc] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-11-03 09:08 +1100 |
| Message-ID | <mailman.1960.1383430108.18130.python-list@python.org> |
| In reply to | #58348 |
On Sun, Nov 3, 2013 at 8:33 AM, <juel4700@gmail.com> wrote:
> 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
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2013-11-02 22:32 +0000 |
| Message-ID | <mailman.1962.1383431545.18130.python-list@python.org> |
| In reply to | #58348 |
On 02/11/2013 21:33, juel4700@gmail.com wrote:
> 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?
If your code base gets too large to handle in one module sure. If it's
only a few hundred lines of code why bother?
>
> 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!!")
You catch the wrong error, it should be ImportError. Correct this and
you print a pretty message and continue so...
>
> GPIO.setmode(GPIO.BOARD)
...you'll get a traceback here as your code knows nothing about GPIO
because of the ImportError that you've so carefully mishandled. Don't
worry about it, we've all done it at one time or another :)
> 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..
>
Keeping everything in one module is actually the simplest approach. If
you want to keep separate modules repeat the GPIO import as needed. The
thing to beware of is circular imports if you have many modules.
> 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
>
Finally if you're using google groups would you be kind enough to read,
digest and action this https://wiki.python.org/moin/GoogleGroupsPython
--
Python is the second best programming language in the world.
But the best has yet to be invented. Christian Tismer
Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-11-03 05:48 +0000 |
| Message-ID | <5275e3bd$0$29972$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #58348 |
On Sat, 02 Nov 2013 14:33:54 -0700, juel4700 wrote:
> 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".
--
Steven
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web