Link to module Stack

K

kzagradskiy

Link to module Stack:
http://groups.google.com/group/comp...aeca2?lnk=gst&q=stack+module#64cb44a120baeca2

Here's the stack module for py4th.
nick
---------------<cut here>----------------
#!/usr/Util/bin/python
#
# @(#)stack.py 1.1
#
# stack.py
# generic stack class.
class Stack:
def __init__(self):
self.__heap = []
def push (self, word):
self.__heap.append (word)
def pop (self):
if len(self.__heap) == 0:
raise InnerInterpreterError, "stack underflow"
result = self.__heap[-1]
del self.__heap[-1]
return result
def __repr__(self):
return `self.__heap`
def __str__(self):
return `self.__heap`
def flush (self):
self.__heap = []
 
S

Steven D'Aprano

class Stack:
def __init__(self):
self.__heap = []

A "heap" has a technical meaning in programming. To describe the
internals of a stack as "heap" will be disconcerting and confusing to
anyone who knows about stacks and heaps.

def push (self, word):
self.__heap.append (word)
def pop (self):
if len(self.__heap) == 0:
raise InnerInterpreterError, "stack underflow"

"InnerInterpreterError" is the most inappropriate exception name I've
ever seen. It has nothing to do with the interpreter, it's a stack error.
result = self.__heap[-1]
del self.__heap[-1]

That is better written as result = self.__heap.pop().
 
D

Dave Angel

Steven said:
class Stack:
def __init__(self):
self.__heap = []

A "heap" has a technical meaning in programming. To describe the
internals of a stack as "heap" will be disconcerting and confusing to
anyone who knows about stacks and heaps.


def push (self, word):
self.__heap.append (word)
def pop (self):
if len(self.__heap) == 0:
raise InnerInterpreterError, "stack underflow"

"InnerInterpreterError" is the most inappropriate exception name I've
ever seen. It has nothing to do with the interpreter, it's a stack error.
It has everything to do with the (Forth) interpreter. Exceptions can
readily be named according to their application -- it's not always about
Python. Anyway, Forth has an inner-interpreter and an
outer-interpreter, and the name will make sense to a Forth programmer.
result = self.__heap[-1]
del self.__heap[-1]

That is better written as result = self.__heap.pop().
or even better, without the extra local var:

def pop (self):
if len(self.__heap) == 0:
raise InnerInterpreterError, "stack underflow"
return self.__heap.pop(1)

P.S. - I'm puzzled why the OP even put this message here. There's no
question posted with it.

DaveA
 
S

Steven D'Aprano

It has everything to do with the (Forth) interpreter. Exceptions can
readily be named according to their application -- it's not always about
Python. Anyway, Forth has an inner-interpreter and an
outer-interpreter, and the name will make sense to a Forth programmer.

Pardon me, but I *am* a Forth programmer. Or was, it's been many years,
and I'm rusty. I guess this is a difference of terminology: what you're
calling an inner interpreter and an outer interpreter I know of as the
Forth engine and the (text) interpreter. Gforth refers to them as such,
so did Leo Brodie's Forth books, and the (ancient) Macintosh Forth
compiler "Mach 2".

But in any case... a stack is an general-purpose data structure, and the
error message shouldn't be coupled so tightly to one use. That would be
like this (made-up) example:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
GraphicsApplicationError: too few pixels to calculate average


Ridiculous, yes?


Yes, Forth uses a stack (technically two, a parameter stack and a return
stack, and some implementations include a third, floating point, stack).
Virtually all languages use stacks in their implementation, and Python
byte-code is also stack-based.

result = self.__heap[-1]
del self.__heap[-1]
That is better written as result = self.__heap.pop().
or even better, without the extra local var:

def pop (self):
if len(self.__heap) == 0:
raise InnerInterpreterError, "stack underflow"
return self.__heap.pop(1)

pop(1)? I don't think so.
['a', 'c', 'd', 'e', 'f']


You probably meant pop(-1), but that's unnecessary because pop defaults
to popping from the end of the list.


P.S. - I'm puzzled why the OP even put this message here. There's no
question posted with it.

Me too.
 
S

Steve Holden

Steven said:
It has everything to do with the (Forth) interpreter. Exceptions can
readily be named according to their application -- it's not always about
Python. Anyway, Forth has an inner-interpreter and an
outer-interpreter, and the name will make sense to a Forth programmer.

Pardon me, but I *am* a Forth programmer. Or was, it's been many years,
and I'm rusty. I guess this is a difference of terminology: what you're
calling an inner interpreter and an outer interpreter I know of as the
Forth engine and the (text) interpreter. Gforth refers to them as such,
so did Leo Brodie's Forth books, and the (ancient) Macintosh Forth
compiler "Mach 2".

But in any case... a stack is an general-purpose data structure, and the
error message shouldn't be coupled so tightly to one use. That would be
like this (made-up) example:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
GraphicsApplicationError: too few pixels to calculate average


Ridiculous, yes?


Yes, Forth uses a stack (technically two, a parameter stack and a return
stack, and some implementations include a third, floating point, stack).
Virtually all languages use stacks in their implementation, and Python
byte-code is also stack-based.

result = self.__heap[-1]
del self.__heap[-1]


That is better written as result = self.__heap.pop().
or even better, without the extra local var:

def pop (self):
if len(self.__heap) == 0:

Since self.__heap is a list, the canonical Python for the above test
would, of course, be the much simpler

if not self.__heap

regards
Steve
 
D

Dave Angel

Steven said:
Pardon me, but I *am* a Forth programmer. Or was, it's been many years,
and I'm rusty. I guess this is a difference of terminology: what you're
calling an inner interpreter and an outer interpreter I know of as the
Forth engine and the (text) interpreter. Gforth refers to them as such,
so did Leo Brodie's Forth books, and the (ancient) Macintosh Forth
compiler "Mach 2".
I'm pretty sure FIG-Forth called them an inner interpreter and outer
interpreter, but I don't remember other sources. FIG-Forth was my first
Forth system, gotten on an 8" diskette. The inner interpreter was
LOADSW, JMP AX, I believe, as it was an indirected threaded interpreter
implementation.
pop(1)? I don't think so.
That was a typo; I meant pop(). And of course others have improved on
my remarks anyway.

DaveA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,149
Latest member
Vinay Kumar Nevatia0
Top