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


Groups > comp.lang.python > #100896

Re: Importing constantly changing variables

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Dennis Lee Bieber <wlfraed@ix.netcom.com>
Newsgroups comp.lang.python
Subject Re: Importing constantly changing variables
Date Sat, 26 Dec 2015 13:14:18 -0500
Organization IISS Elusive Unicorn
Lines 85
Message-ID <mailman.22.1451153681.11925.python-list@python.org> (permalink)
References <944a9d35-dc31-4074-8d56-bb6e9a0d1d15@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=us-ascii
Content-Transfer-Encoding 7bit
X-Trace news.uni-berlin.de VXiBxkR/SkAk3I7YyKKuZwZ2CdzxL0J7RfZqDLv4wMYQ==
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'assignment': 0.07; '"r")': 0.09; '(first': 0.09; 'deletes': 0.09; 'imported': 0.09; 'imports': 0.09; 'loaded,': 0.09; 'loop.': 0.09; 'message-id:@4ax.com': 0.09; 'open()': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'english.': 0.13; 'def': 0.13; 'file,': 0.15; 'interpreter': 0.15; '"import"': 0.16; 'dummy': 0.16; 'executed,': 0.16; 'files)': 0.16; 'line);': 0.16; 'main():': 0.16; 'operation.': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'sees': 0.16; 'student,': 0.16; 'subject:changing': 0.16; 'true:': 0.16; 'later': 0.16; 'basically': 0.18; 'integer': 0.18; 'url:home': 0.18; '(in': 0.18; 'all,': 0.20; 'changes': 0.20; '2015': 0.20; 'meant': 0.22; '2.x': 0.22; 'assumes': 0.22; 'trying': 0.22; 'code,': 0.23; '(or': 0.23; 'dec': 0.23; 'references': 0.23; 'sat,': 0.23; 'second': 0.24; 'import': 0.24; 'written': 0.24; 'sort': 0.25; 'module': 0.25; "doesn't": 0.26; 'header:X-Complaints-To:1': 0.26; 'rest': 0.26; 'error': 0.27; 'values': 0.28; '---': 0.28; 'actual': 0.28; 'print': 0.30; 'code': 0.30; "i'd": 0.31; 'everyone': 0.31; 'point': 0.33; 'skip:- 10': 0.34; 'changing': 0.34; 'file': 0.34; 'files,': 0.35; 'replace': 0.35; 'but': 0.36; 'skip:i 20': 0.36; 'there': 0.36; '(and': 0.36; 'data.': 0.36; 'loaded': 0.36; 'modules': 0.36; 'projects.': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'charset:us-ascii': 0.37; 'skip:o 20': 0.38; 'data': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'save': 0.60; 'your': 0.60; 'entire': 0.61; 'email addr:gmail.com': 0.62; 'show': 0.62; 'real': 0.62; 'goal': 0.64; 'between': 0.65; '****': 0.66; 'one-time': 0.66; 'repeat': 0.67; 'physical': 0.72; 'gain': 0.82; '>def': 0.84; 'fin': 0.84; 'horrible': 0.84; 'wipe': 0.84; 'dennis': 0.91; 'sensors': 0.91; 'received:108': 0.93; 'electronics': 0.96
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host adsl-108-79-218-6.dsl.klmzmi.sbcglobal.net
X-Newsreader Forte Agent 6.00/32.1186
X-No-Archive YES
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:100896

Show key headers only | 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