Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #100894 > unrolled thread

Importing constantly changing variables

Started byariklapid.swim@gmail.com
First post2015-12-26 07:14 -0800
Last post2015-12-27 10:15 +1100
Articles 4 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  Importing constantly changing variables ariklapid.swim@gmail.com - 2015-12-26 07:14 -0800
    Re: Importing constantly changing variables Ian Kelly <ian.g.kelly@gmail.com> - 2015-12-26 10:11 -0700
    Re: Importing constantly changing variables Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-12-26 13:14 -0500
    Re: Importing constantly changing variables Ben Finney <ben+python@benfinney.id.au> - 2015-12-27 10:15 +1100

#100894 — Importing constantly changing variables

Fromariklapid.swim@gmail.com
Date2015-12-26 07:14 -0800
SubjectImporting constantly changing variables
Message-ID<944a9d35-dc31-4074-8d56-bb6e9a0d1d15@googlegroups.com>
Hello everyone !
First of all, excuse me for my horrible English.

I am an electronics engineering student, trying to use raspberry pi for one of my projects.
Meanwhile, my goal is simply to create a pair of files, written in python, which would do the following:

A file named "sensors.py" imports varying values from physical sensors. These values are constantly changing. 
(At this point - I intend to change them manually):

[code]
def import_soilMoisture():
	soilMoisture = 40 # Later - Imports value from corresponding RPi's GPIO
	return soilMoisture

def import_airTemperture():
	airTemperture = 24 # Later - Imports value from corresponding RPi's GPIO
	return airTemperture

def import_airHumidity():
	airHumidity = 69 # Later - Imports value from corresponding RPi's GPIO
	return airHumidity
[/code]

A second file, named "main.py" is intended to import and print the values from "sensors.py":

[code]
import time

def main():
	while True:
		import sensors
		print(sensors.import_soilMoisture())
		print(sensors.import_airTemperture())
		print(sensors.import_airHumidity())
		time.sleep(5)
main()
[/code]

As you can see, I want the program to print all values each 5 seconds.
When I run the file "main.py" it does print values every 5 seconds, BUT when I manually change
the values (e.g. airTemperture = 30 instead of 24) and save the file, nothing changes - 
the old values keep on printing.

[b]Help me please to understand what's wrong[/b]
[i]For who it may concern: My OS is Debian[/i]

Thanks to you all in advance!! :) :D

[toc] | [next] | [standalone]


#100895

FromIan Kelly <ian.g.kelly@gmail.com>
Date2015-12-26 10:11 -0700
Message-ID<mailman.21.1451149955.11925.python-list@python.org>
In reply to#100894
On Sat, Dec 26, 2015 at 8:14 AM,  <ariklapid.swim@gmail.com> wrote:
> As you can see, I want the program to print all values each 5 seconds.
> When I run the file "main.py" it does print values every 5 seconds, BUT when I manually change
> the values (e.g. airTemperture = 30 instead of 24) and save the file, nothing changes -
> the old values keep on printing.

Changing the source code of a module isn't going to affect that module
in a running program if it's already been imported. You can use the
importlib.reload function to force a module to be reloaded, but this
is generally anti-recommended. If not done correctly it can result in
multiple versions of the module being simultaneously in use, leading
to confusing error conditions.

It sounds like you're not trying to update the code anyway, just the
data used in the code. For that, you'd be better off putting the data
in a data file that your "sensors.py" would read from and re-read on
every iteration.

[toc] | [prev] | [next] | [standalone]


#100896

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2015-12-26 13:14 -0500
Message-ID<mailman.22.1451153681.11925.python-list@python.org>
In reply to#100894
On Sat, 26 Dec 2015 07:14:36 -0800 (PST), ariklapid.swim@gmail.com
declaimed the following:

>Hello everyone !
>First of all, excuse me for my horrible English.
>
>I am an electronics engineering student, trying to use raspberry pi for one of my projects.
>Meanwhile, my goal is simply to create a pair of files, written in python, which would do the following:
>
>A file named "sensors.py" imports varying values from physical sensors. These values are constantly changing. 
>(At this point - I intend to change them manually):
>
>[code]
>def import_soilMoisture():
>	soilMoisture = 40 # Later - Imports value from corresponding RPi's GPIO
>	return soilMoisture
>
>def import_airTemperture():
>	airTemperture = 24 # Later - Imports value from corresponding RPi's GPIO
>	return airTemperture
>
>def import_airHumidity():
>	airHumidity = 69 # Later - Imports value from corresponding RPi's GPIO
>	return airHumidity
>[/code]
>
>A second file, named "main.py" is intended to import and print the values from "sensors.py":
>
>[code]
>import time
>
>def main():
>	while True:
>		import sensors

	"import" is basically a one-time operation. The first time this is
executed, the interpreter will read/load the contents of sensors.py and
save it in a module object under the name "sensors".

	The second time this is executed, the interpreter sees that there is
already a "sensors" module loaded, and returns a reference to the loaded
copy (in your code you basically wipe out one reference with a new
reference to the same module -- but if you had other modules that also
imported it, only the first would read/load and the others just get
references to the loaded copy).


	Since the eventual assignment is to read some sort of hardware sensor,
I'd dummy it up by a file (or multiple files) and use actual file I/O.
"import" is meant to gain access to sharable/library code, not changing
data.

**** pseudo-code --- NOT TESTED (and 2.x syntax) ****

import sensors

while True:
	print sensors.readMoisture()
	print sensors.readTemp()
	print sensors.readHumidity()

-=-=-=-=-=-=-=-
#sensors

def readMoisture():
	fin = open("moisture.data", "r")
	value = int(fin.readln().strip())
	fin.close()
	return value

... repeat style for rest
-=-=-=-=-=-=-

	This form assumes 1) the data is integer values; 2) the file only has
one value in it (first line); 3) you will replace the entire file between
read operations; 4) there is NO error trapping (if the OS deletes the old
file but the new one doesn't show up until after the open() call it will
fail).

	The nice thing -- you can later change the inside of readXXXX() to
access the real sensor, and not make any changes to your main program loop.
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [next] | [standalone]


#100899

FromBen Finney <ben+python@benfinney.id.au>
Date2015-12-27 10:15 +1100
Message-ID<mailman.25.1451172008.11925.python-list@python.org>
In reply to#100894
ariklapid.swim@gmail.com writes:

> Hello everyone !
> First of all, excuse me for my horrible English.

As is often the case with people who make this apology, your English is
far better than most native English speakers can use any other language
:-)

> A file named "sensors.py" imports varying values from physical
> sensors. These values are constantly changing. 

Such values, then, should not be obtained from a Python ‘import’.

Instead, you need to come up with a data transfer protocol, or (more
likely) make use of an existing one. Your running program will make
function calls that get the data from “the outside world” – a file, or a
network interface, or some other input to the program.

So to solve this you will need to know the specifics of how the device
connects to the computer, what interfaces it presents for obtaining the
data at run time, and what Python libraries you can use for accessing
that interface.

-- 
 \           “If you do not trust the source do not use this program.” |
  `\                                —Microsoft Vista security dialogue |
_o__)                                                                  |
Ben Finney

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web