SunQuest
           Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old August 4th, 2003, 05:42 AM
telex4's Avatar
telex4 telex4 is offline
Wacky hack
Dev Shed Novice (500 - 999 posts)
 
Join Date: Apr 2001
Location: London, England
Posts: 512 telex4 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 25 m 29 sec
Reputation Power: 8
Returning a value without breaking from function?

Hullo,

I'm trying to solve a bit of a conundrum here. I have a function (well, to be precise a class method) that opens a pipe and uses a while loop to go through the output line by line. It needs to return some information from each line as it comes, and previously I've achieved that by having a function called "printProgress" that is called in the while loop and that, well, prints the progress!

The problem is that for various reasons this isn't satisfactory any more. I need to be able to do something like:

Code:
def run():
    (open the pipe, etc.)
    while pipe.read(1000):
        return

while run():
    (do something here)


The problem of course is that "return" will break from the function on the first call.

I've read a little about yield, and think it might be what I'm looking for, but I can't figure out how to use it. I've also considered opening a fifo, but that seems a little extreme.

Ideas?

Reply With Quote
  #2  
Old August 4th, 2003, 07:14 AM
sacrilege sacrilege is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Location: Norwich, UK
Posts: 53 sacrilege User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 1 m 53 sec
Reputation Power: 6
From the looks of this page it seems yield does what you want. There's a few examples/explanations on it's usage which might help.

Reply With Quote
  #3  
Old August 4th, 2003, 08:24 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Yeah, yield is looking pretty good. What we need added is a return that keeps going without exiting. Yield would be nicer still if it returned a readable value instead of a <generator.. but it works, you just need to call list or tuple on the function (you can't make a string or dictionary this way which sucks, infact i can't).

Can't see me ever using it unless i have too i really don't like how it works..

Mark.

Reply With Quote
  #4  
Old August 4th, 2003, 08:41 AM
telex4's Avatar
telex4 telex4 is offline
Wacky hack
Dev Shed Novice (500 - 999 posts)
 
Join Date: Apr 2001
Location: London, England
Posts: 512 telex4 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 25 m 29 sec
Reputation Power: 8
I can't get how it works at the moment; every time I experiment I get an error. I'm going to have to find some examples, since the python docs only seem to mention it in the "language lawyers" section.

Reply With Quote
  #5  
Old August 4th, 2003, 10:54 AM
telex4's Avatar
telex4 telex4 is offline
Wacky hack
Dev Shed Novice (500 - 999 posts)
 
Join Date: Apr 2001
Location: London, England
Posts: 512 telex4 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 25 m 29 sec
Reputation Power: 8
Aha! I correct myself... here's a working example:

Code:
# This is needed in Python < 2.3
from __future__ import generators
import os, popen2

def run(self, program, arguments):
	"""Runs a program; supply program name (string) and arguments (list)"""
	command = arguments
	command[:0] = [program]

	self.pipe = popen2.Popen4(command)
	pid = self.pipe.pid
	while 1:
		line = self.pipe.fromchild.read(1000)
		if not line:
			break
		yield line

	# Clean up process table
	try:
		self.kill_pipe()
		os.waitpid(pid, os.WNOHANG)
	except:
		pass

for line in run('runme', ['real', 'quick']):
	print line


That function lets me safely handle running external commands and parse the output line by line (in conjuction with another function to kill the pipe, kill_pipe()).

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Returning a value without breaking from function?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway