6

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()
Right leg
  • 16,080
  • 7
  • 48
  • 81
user_loser
  • 881
  • 1
  • 13
  • 27

2 Answers2

5

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

wong2
  • 34,358
  • 48
  • 134
  • 179
  • No, its not the same: `curses.LINES` is a constant showing the default no. of lines, which is independent of the window settings, whereas `window.getmaxyx()` is a function, and the no. of lines it returns may be different than the default ones, if it is applied to a window created e.g. with `curses.newwin()`. – Apostolos Sep 16 '20 at 06:33
2

That code is written for Python 3. You can see curses.LINES is now in that API, though it was not in Python 2.7:

https://docs.python.org/3/howto/curses.html

If you need to get the terminal width and height in Python 2, see here: How to get Linux console window width in Python

Community
  • 1
  • 1
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • 1
    If I open up a Python3 interactive shell and import `curses` then type `curses.LINES` I still receive an error. :/ Thank-you for responding though. – user_loser Feb 10 '15 at 03:20
  • Holy cow, I tried Python 3.3 just now and you're right, it doesn't work. Maybe 3.4? – John Zwinck Feb 10 '15 at 03:23
  • 1
    I am using Python 3.2.3 :/ I may have to try the upgrade. The book I am reading was published in 2008, so I doubt they were using Python 3000. :D Thanks for the help John. The SO thread on terminal width and height you linked maybe my best option. – user_loser Feb 10 '15 at 03:31
  • 1
    wong2's answer is correct. The linked document is a tutorial, not API docs, so these variables are undocumented in both versions. Note that since they're only added after initscr, you can't get them by `from curses import *`. It appears that they update dynamically when the terminal is resized (so much for PEP8 which would suggest uppercase is for constants). – jwelsh Feb 15 '16 at 20:42