Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Curses - module 'curses' has no attribute 'LINES'

I am looking at some source-code from a book and noticed that some of the code does not seem to be in the current Python2.7 API. The module curses is, according to this code, supposed to have a constant variable called LINES and another called COLS. I opened a Python interactive terminal and saw that there is no COLS or LINES variable or method.

My question is: How does this code even work?

def draw_loglines(self):
        self.screen.clear()
        status_col = 4
        bytes_col = 6 
        remote_host_col = 20
        status_start = 0 
        bytes_start = 4 
        remote_host_start = 10
        line_start = 26 
        logline_cols = curses.COLS - status_col - bytes_col - remote_host_col - 1
        for i in range(curses.LINES):
            c = self.curr_topline
            try:
                curr_line = self.loglines[c]
            except IndexError:
                break
            self.screen.addstr(i, status_start, str(curr_line[2]))
            self.screen.addstr(i, bytes_start, str(curr_line[3]))
            self.screen.addstr(i, remote_host_start, str(curr_line[1]))
            #self.screen.addstr(i, line_start, str(curr_line[4])[logline_cols])
            self.screen.addstr(i, line_start, str(curr_line[4]), logline_cols)
            self.curr_topline += 1 
        self.screen.refresh()
like image 924
user_loser Avatar asked Feb 10 '15 02:02

user_loser


1 Answers

I found that curses.LINES exists in Python2 & Python3, but you have to call curses.initscr before using it, or you will get AttributeError.

you can also use window.getmaxyx

[1] https://docs.python.org/2/library/curses.html#curses.window.getmaxyx

like image 95
wong2 Avatar answered Sep 23 '22 02:09

wong2