473,404 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

exit to interpreter?

Hi,

I'm writing a function that polls the user for keyboard input,
looping until it has determined that the user has entered a valid
string of characters, in which case it returns that string so it can
be processed up the call stack. My problem is this. I'd also like it
to handle a special string (e.g. 'quit'), in which case control
should return to the Python command line as opposed to returning the
string up the call stack.

sys.exit seemed like a good choice, but it exits the python interpreter.

I could use an exception for this purpose, but was wondering if
there's a better way?

--b
Mar 23 '07 #1
7 2415
On Mar 23, 12:52 pm, belinda thom <b...@cs.hmc.eduwrote:
Hi,

I'm writing a function that polls the user for keyboard input,
looping until it has determined that the user has entered a valid
string of characters, in which case it returns that string so it can
be processed up the call stack. My problem is this. I'd also like it
to handle a special string (e.g. 'quit'), in which case control
should return to the Python command line as opposed to returning the
string up the call stack.

sys.exit seemed like a good choice, but it exits the python interpreter.

I could use an exception for this purpose, but was wondering if
there's a better way?

--b
If you're using a function, wouldn't using the keyword "return" work?

Mike

Mar 23 '07 #2

On Mar 23, 2007, at 11:04 AM, ky******@gmail.com wrote:
On Mar 23, 12:52 pm, belinda thom <b...@cs.hmc.eduwrote:
>Hi,

I'm writing a function that polls the user for keyboard input,
looping until it has determined that the user has entered a valid
string of characters, in which case it returns that string so it can
be processed up the call stack. My problem is this. I'd also like it
to handle a special string (e.g. 'quit'), in which case control
should return to the Python command line as opposed to returning the
string up the call stack.

sys.exit seemed like a good choice, but it exits the python
interpreter.

I could use an exception for this purpose, but was wondering if
there's a better way?

--b

If you're using a function, wouldn't using the keyword "return" work?

Mike
No, because that just returns to the caller, which is not the Python
interpreter.

Mar 23 '07 #3
belinda thom wrote:
On Mar 23, 2007, at 11:04 AM, ky******@gmail.com wrote:
>On Mar 23, 12:52 pm, belinda thom <b...@cs.hmc.eduwrote:
>>Hi,

I'm writing a function that polls the user for keyboard input,
looping until it has determined that the user has entered a valid
string of characters, in which case it returns that string so it can
be processed up the call stack. My problem is this. I'd also like it
to handle a special string (e.g. 'quit'), in which case control
should return to the Python command line as opposed to returning the
string up the call stack.

sys.exit seemed like a good choice, but it exits the python
interpreter.

I could use an exception for this purpose, but was wondering if
there's a better way?
A custom-defined exception is probably the best way to jump out of a
stack of nested calls.
Mel.

Mar 23 '07 #4
On Mar 23, 1:20 pm, belinda thom <b...@cs.hmc.eduwrote:
On Mar 23, 2007, at 11:04 AM, kyoso...@gmail.com wrote:
On Mar 23, 12:52 pm, belinda thom <b...@cs.hmc.eduwrote:
Hi,
I'm writing a function that polls the user for keyboard input,
looping until it has determined that the user has entered a valid
string of characters, in which case it returns that string so it can
be processed up the call stack. My problem is this. I'd also like it
to handle a special string (e.g. 'quit'), in which case control
should return to the Python command line as opposed to returning the
string up the call stack.
sys.exit seemed like a good choice, but it exits the python
interpreter.
I could use an exception for this purpose, but was wondering if
there's a better way?
--b
If you're using a function, wouldn't using the keyword "return" work?
Mike

No, because that just returns to the caller, which is not the Python
interpreter.
Sorry...I didn't realize you were calling from another function or
functions. Duh. I agree with the other writer. Use a custom exception.

Mar 23 '07 #5
Thanks to all. I had suspected this was the best way to go, but as
I'm fairly new to Python, it seemed worth a check.

--b

On Mar 23, 2007, at 12:48 PM, ky******@gmail.com wrote:
On Mar 23, 1:20 pm, belinda thom <b...@cs.hmc.eduwrote:
>On Mar 23, 2007, at 11:04 AM, kyoso...@gmail.com wrote:
>>On Mar 23, 12:52 pm, belinda thom <b...@cs.hmc.eduwrote:
Hi,
>>>I'm writing a function that polls the user for keyboard input,
looping until it has determined that the user has entered a valid
string of characters, in which case it returns that string so it
can
be processed up the call stack. My problem is this. I'd also
like it
to handle a special string (e.g. 'quit'), in which case control
should return to the Python command line as opposed to returning
the
string up the call stack.
>>>sys.exit seemed like a good choice, but it exits the python
interpreter.
>>>I could use an exception for this purpose, but was wondering if
there's a better way?
>>>--b
>>If you're using a function, wouldn't using the keyword "return"
work?
>>Mike

No, because that just returns to the caller, which is not the Python
interpreter.

Sorry...I didn't realize you were calling from another function or
functions. Duh. I agree with the other writer. Use a custom exception.

--
http://mail.python.org/mailman/listinfo/python-list
Mar 24 '07 #6
On Mar 23, 12:52 pm, belinda thom <b...@cs.hmc.eduwrote:
be processed up the call stack. My problem is this. I'd also like it
to handle a special string (e.g. 'quit'), in which case control
should return to the Python command line as opposed to returning the
string up the call stack.
Maybe you are looking for the 'code' module.

http://docs.python.org/lib/module-code.html

....
Jay Graves

Mar 27 '07 #7

On Mar 24, 2007, at 4:30 AM, Dennis Lee Bieber wrote:
On Fri, 23 Mar 2007 10:52:09 -0700, belinda thom <bt***@cs.hmc.edu>
declaimed the following in comp.lang.python:
>Hi,

I'm writing a function that polls the user for keyboard input,
looping until it has determined that the user has entered a valid
string of characters, in which case it returns that string so it can
be processed up the call stack. My problem is this. I'd also like it
to handle a special string (e.g. 'quit'), in which case control
should return to the Python command line as opposed to returning the
string up the call stack.
To the Python command line? That seems to imply that you started
Python first, then imported the module with the function and
invoked it.
Yup
Surely you don't expect this to be the normal operational mode for
the program?
Its more for pedagogical purposes. I'm using Python in an undergrad
prog class and I'd like students to be able to either enter one of a
set of simple menu choices or quit to the interpreter. It should be
command-based as opposed to GUI based. Hence the request.
About the only way I know of to short circuit the calling stack
requires using an exception -- and an exception handler at the top-
level
(which, for your stated goal, probably has a "pass" for its body,
so the
top-level will "exit" to the interpreter prompt).
This is similar to Mel's advice, which was what I'd expected but
wanted to verify.

Thanks. For those who've been following this thread, this is the
approach I took.

--b

Apr 3 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: David Leon | last post by:
Is there any way to stop php.exe closing after it processes a PHP script? It doesn't seem to have the traditional options of an MS-DOS program. I am using Windows XP Pro and have associated .php...
2
by: Ishwar Rattan | last post by:
Here is a piece code (according to blurb on os.wait, the lower order 7 bits of exit status of process should contain the signal number of signal that terminated the process..) and signal number...
4
by: Bryan | last post by:
Quick question: Why does os._exit called from a Python Timer kill the whole process while sys.exit does not? On Suse. Bryan
0
by: Jacob Kroon | last post by:
Hi, I'm working on a library written in C using GObject, which i provide python bindings for using pygtk. When I checked the library for memory leaks using valgrind, I noticed that none of the...
9
by: gangesmaster | last post by:
calling sys.exit() from a thread does nothing... the thread dies, but the interpreter remains. i guess the interpreter just catches and ignore the SystemExit exception... does anybody know of a...
3
by: carl.dhalluin | last post by:
Hi, I am playing with the atexit module but I don't find a way to see the difference between a script calling sys.exit(<returncode>) and the interpreting arriving at the end of the source code...
3
by: Matthew Fitzgibbons | last post by:
I've got a pretty complex interactive command line program. Instead of writing my own REPL, I'm using the Python interpreter (an infinitely better solution). This program has two threads, a...
0
by: Matthew Fitzgibbons | last post by:
Matthew Fitzgibbons wrote: Here's a modified example that _almost_ works: #!/usr/bin/env python import code import time import threading
0
by: Gary Robinson | last post by:
In Python 2.5.2, I notice that, in the interpreter or in a script, I can exit with: exit() But I don't see exit() mentioned as a built-in function; rather the Python Library Reference says we...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.