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


Groups > comp.lang.python > #100896

Re: Importing constantly changing variables

From Dennis Lee Bieber <wlfraed@ix.netcom.com>
Newsgroups comp.lang.python
Subject Re: Importing constantly changing variables
Date 2015-12-26 13:14 -0500
Organization IISS Elusive Unicorn
Message-ID <mailman.22.1451153681.11925.python-list@python.org> (permalink)
References <944a9d35-dc31-4074-8d56-bb6e9a0d1d15@googlegroups.com>

Show all headers | View raw


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/

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web