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


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

Variables in a loop, Newby question

Started byvanommen.robert@gmail.com
First post2013-12-24 08:07 -0800
Last post2013-12-27 11:39 -0500
Articles 3 on this page of 23 — 13 participants

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


Contents

  Variables in a loop, Newby question vanommen.robert@gmail.com - 2013-12-24 08:07 -0800
    Re: Variables in a loop, Newby question Joel Goldstick <joel.goldstick@gmail.com> - 2013-12-24 11:20 -0500
    Re: Variables in a loop, Newby question "Tobias M." <tm@tobix.eu> - 2013-12-24 17:24 +0100
    Re: Variables in a loop, Newby question Peter Otten <__peter__@web.de> - 2013-12-24 17:29 +0100
    Re: Variables in a loop, Newby question bob gailer <bgailer@gmail.com> - 2013-12-24 12:26 -0500
    Re: Variables in a loop, Newby question vanommen.robert@gmail.com - 2013-12-24 09:54 -0800
      Re: Variables in a loop, Newby question Joel Goldstick <joel.goldstick@gmail.com> - 2013-12-24 13:10 -0500
      Re: Variables in a loop, Newby question Dave Angel <davea@davea.name> - 2013-12-24 13:42 -0500
      Re: Variables in a loop, Newby question Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-12-25 13:35 -0500
    Re: Variables in a loop, Newby question vanommen.robert@gmail.com - 2013-12-24 10:27 -0800
      Re: Variables in a loop, Newby question Denis McMahon <denismfmcmahon@gmail.com> - 2013-12-25 02:54 +0000
        Re: Variables in a loop, Newby question Cameron Simpson <cs@zip.com.au> - 2013-12-25 16:42 +1100
          Re: Variables in a loop, Newby question Denis McMahon <denismfmcmahon@gmail.com> - 2013-12-25 15:27 +0000
            Re: Variables in a loop, Newby question Cameron Simpson <cs@zip.com.au> - 2013-12-26 12:01 +1100
            Re: Variables in a loop, Newby question Chris Angelico <rosuav@gmail.com> - 2013-12-26 12:35 +1100
              Re: Variables in a loop, Newby question Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-12-26 16:41 +1100
                Re: Variables in a loop, Newby question Dave Angel <davea@davea.name> - 2013-12-26 03:14 -0500
                Re: Variables in a loop, Newby question Chris Angelico <rosuav@gmail.com> - 2013-12-26 19:24 +1100
      Re: Variables in a loop, Newby question Peter Otten <__peter__@web.de> - 2013-12-25 15:52 +0100
      Re: Variables in a loop, Newby question Michael Torrie <torriem@gmail.com> - 2013-12-25 23:34 -0700
    Re: Variables in a loop, Newby question Larry Hudson <orgnut@yahoo.com> - 2013-12-25 00:13 -0800
    Re: Variables in a loop, Newby question vanommen.robert@gmail.com - 2013-12-27 00:53 -0800
      Re: Variables in a loop, Newby question Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-12-27 11:39 -0500

Page 2 of 2 — ← Prev page 1 [2]


#62697

FromLarry Hudson <orgnut@yahoo.com>
Date2013-12-25 00:13 -0800
Message-ID<jYednUg-TpUsDifPnZ2dnUVZ_vqdnZ2d@giganews.com>
In reply to#62680
On 12/24/2013 08:07 AM, vanommen.robert@gmail.com wrote:
[snip...]
> x = 1
> while x <> 10
> 	var x = x
> 	x = x + 1
[snip...]

Besides the other valid answers you have received, I want to add one other minor nit.  The 
symbol <> for unequal is deprecated -- it's better to use != instead.  Although, as was also 
pointed out, in this particular case you want < (less than) rather than unequal.

      -=- Larry -=-

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


#62784

Fromvanommen.robert@gmail.com
Date2013-12-27 00:53 -0800
Message-ID<e183ed75-5ee8-429e-90b8-83e064295430@googlegroups.com>
In reply to#62680
Hello everyone, I have been away for a while.
I have been reading all the good advises and want to explain why I want to read the temperatures separately from the main script. It takes a long time to read out 10 temperatures. About 10 seconds. So that’s the reason why I had the idea to create a separate script and I thought by making te variables Global I could access them by other scripts. Now I know that’s not the purpose of Global.
Maybe I can create a loop that keeps running simultaneously with the rest of the script. 
I’ve downloaded a great student book about Python and learning a lot.
Thanks for all the answers and I’ll post more questions in the future, I’m sure of it.
Greetings Robert

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


#62809

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2013-12-27 11:39 -0500
Message-ID<mailman.4671.1388162351.18130.python-list@python.org>
In reply to#62784
On Fri, 27 Dec 2013 00:53:43 -0800 (PST), vanommen.robert@gmail.com
declaimed the following:

>Hello everyone, I have been away for a while.
>I have been reading all the good advises and want to explain why I want to read the temperatures separately from the main script. It takes a long time to read out 10 temperatures. About 10 seconds. So that’s the reason why I had the idea to create a separate script and I thought by making te variables Global I could access them by
other scripts. Now I know that’s not the purpose of Global.
>Maybe I can create a loop that keeps running simultaneously with the rest of the script. 
>I’ve downloaded a great student book about Python and learning a lot.
>Thanks for all the answers and I’ll post more questions in the future, I’m sure of it.
>Greetings Robert

	Check the class threading.Thread, along with Queue.Queue -- presuming
the sensor reads are blocking calls, they should have minimal effect on the
main thread...

PSEUDO-code:
-=-=-=-=-
import threading
import Queue
import time

list_of_sensors = [	whatever, is, needed, to, access, each, sensor ]
#list of sensors may just be the ID strings, or if you need to open 
#each, make it a list of the opened items, so the worker doesn't 
#keep opening, reading, closing, for each sensor

tempQ = Queue.Queue()

def sensorWork(retQ, list_of_sensors):
	while True:
		for sensor in list_of_sensors:
			temp = readSensor(sensor)		#presumed to be blocking
			retQ.put((sensor, temp, time.time())
						#if a timestamp is useful

worker = threading.Thread(target=sensorWork, 
							args=(tempQ, list_of_sensors))
worker.setDaemon()	#lets it die when the main program exits

worker.start()

#now do the main processing
while True:
	#check for available temps
	numTemps = tempQ.qsize()	#should be good as only one consumer
	if numTemps > 0:
		for i in range(numTemps - 1):		
					#I'm not iterating over the queue as
					#that might starve the main process
					#if the sensor reads produce a reading
					#just fast enough to keep up with 
					#this loop
			(sensor, temp, timestamp) = tempQ.get()
			#process the temp data
	#here you do the rest of the main process, which should be
	#something that returns here at some regular interval so that
	#the loop checks for new temp readings to be processed


	
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

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


csiph-web