0

I am working on an assignment with some requirements such as keep stdin at the bottom of the terminal window in curse, hence I tried this: Keep stdin line at top or bottom of terminal screen, and the answer to that question works on me perfectly in a test (without class). But when I merge that method in the answer into my class, some problems happened.

part of my code is like this (runnable):

import curses
import curses.ascii
from string import printable


class Node:
    def main(self):
        curses.wrapper(self.init) # start running the program

    def init(self, screen):
        global PRINTABLE

        PRINTABLE = map(ord, printable)
        Y, X = screen.getmaxyx()
        height = Y - 1
        type = 0
        command = self.stdin_fixed(screen, height, type)


    """From: https://stackoverflow.com/questions/30258999/keep-stdin-line-at-top-or-bottom-of-terminal-screen/30259422#30259422
    """
    def stdin_fixed(self, screen, height, type):
        global PRINTABLE

        screen.move(height, 0)
        screen.clrtoeol()
        if (type == 0):
            screen.addstr("[Command Line Interface]> ")
        elif (type == 1):
            screen.addstr(height, 0, "[RMP node " + str(self.HOST) + "]# ")

        ERASE = self.stdin_fixed.ERASE = getattr(self.stdin_fixed, "erasechar", ord(curses.erasechar()))
        Y, X = screen.getyx()
        s = []
        while True:
            c = screen.getch()
            if c in (curses.ascii.LF, curses.ascii.CR, curses.KEY_ENTER):
                break
            elif c == ERASE or c == curses.KEY_BACKSPACE:
                y, x = screen.getyx()
                if x > X:
                    del s[-1]
                    screen.move(y, (x - 1))
                    screen.clrtoeol()
                    screen.refresh()
            elif c in PRINTABLE:
                s.append(chr(c))
                screen.addch(c)
            else:
                pass
        return "".join(s)

if __name__ == '__main__':
    node = Node()
    node.main() # run the instance

In this case (when test in Class), the program runs with this error:

...
(some traceback)
...
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/curses/wrapper.py", line 43, in wrapper
  return func(stdscr, *args, **kwds)
File "RMP_noGPIO.py", line 341, in init
  command = self.stdin_fixed(screen, height, type)
File "RMP_noGPIO.py", line 518, in stdin_fixed
  ERASE = self.stdin_fixed.ERASE = getattr(self.stdin_fixed, "erasechar", ord(curses.erasechar()))
AttributeError: 'instancemethod' object has no attribute 'ERASE'

It says that the instance has no attribute named 'ERASE', and I cannot find the solution to this, cause I ran it perfectly in the test without using class, and this only occurs when I use it in class.

Community
  • 1
  • 1
nonemaw
  • 619
  • 1
  • 7
  • 16
  • I'm not sure if it's related to the cause of the error, but the error message tells me you're using an old-style class in Python 2. You almost always should be using new-style classes, by inheriting from `object` (unless you're trying to maintain compatibility with a 10+ year old program written for Python 2.1 or earlier). Try changing `class Node:` to `class Node(object):`. – Blckknght Jan 23 '16 at 00:38
  • Instance methods and their functions aren't the same thing. (I explain this some at http://stackoverflow.com/questions/2906177/what-is-the-difference-between-a-is-b-and-ida-idb-in-python ) – Mike Graham Jan 23 '16 at 00:40
  • You also snipped important code. In particular, it seems that `stdin_to_command` is indeed a method so assigning to `self.stdin_to_command.ERASE` fails correctly (where would `ERASE` end up? Somewhere in `self`? In `klass.stdin_to_command`?). Please give a [*self contained*](http://www.sscce.org/) example of the failing code. – filmor Jan 23 '16 at 00:53
  • I am sorry that's my bad, the "stdin_to_command" is the wrong name in posting this question, I changed them all to "stdin_fixed" now. the "ERASE" is only used in function "stdin_fixed", and I updated a runnable code example – nonemaw Jan 23 '16 at 01:11

0 Answers0