[Tutor] How to get terminal settings
Hi, say you want to write a more-like program. How do you know how many lines the terminal window can display. /\ Vincent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to get terminal settings
On Thursday, 19 January 2006 at 11:29:29 -0800, Bill Campbell wrote: > On Thu, Jan 19, 2006, Vincent Zee wrote: > >Hi, > > > >say you want to write a more-like program. > >How do you know how many lines the terminal window > >can display. > > Use the curses library, and it will take care of this for you. > Hi Bill, thanks for your answer. Is there, except from using the curses library, any other method? I'm a bit afraid of using curses at the moment(;-)) /\ Vincent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to get terminal settings
On Thursday, 19 January 2006 at 12:37:38 -0800, Danny Yoo wrote: > > > On Thu, 19 Jan 2006, Vincent Zee wrote: > > > say you want to write a more-like program. How do you know how many > > lines the terminal window can display. > > Hi Vincent, > > It's possible that this information might already be in your environment. > If you're using the 'bash' shell, and if the 'checkwinsize' option is set > in bash, then bash should keep track of the window size through LINES and > COLUMNS. According to the "Art of Unix Programming": > > http://www.faqs.org/docs/artu/ch10s04.html > > those variables are fairly standard and lots of programs use them. But I > don't know if other shells go out of their way to maintain consistancy > with the current terminal size on terminal resizing. > > If you want to get at the environment variables, take a look at the > 'os.environ' dictionary: > > http://www.python.org/doc/lib/os-procinfo.html#l2h-1508 > > > Alternatively, if you're on Unix, the 'curses' module will get you the > information you want. > > http://www.python.org/doc/lib/module-curses.html Hi Danny, thank you for your reply. I want the program to run on Freebsd and on MacOSX. On FreeBSD I use the tcsh and on Mac its bash or tcsh. I looked at the curses module and also to the cursus howto on python.org but I find it still a bit unclear on how to use it. There being curses, ncurses and a curses wrapper. I'm a little confused. The os.environ didn't give me any hints to screen size so maybe curses is the way to go. Any pointers to other curses howto's? /\ Vincent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to get terminal settings
On Thursday, 19 January 2006 at 16:03:16 -0800, Terry Carroll wrote: > On Thu, 19 Jan 2006, Vincent Zee wrote: > > > Any pointers to other curses howto's? > > There's http://heather.cs.ucdavis.edu/~matloff/Python/PyCurses.pdf ; but > it's mostly a couple of example programs, without a lot of explanation. Hi Terry, thank you for the link, I'll have a look. /\ Vincent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to get terminal settings
On Thursday, 19 January 2006 at 23:53:06 -, Alan Gauld wrote: > Assuming you are on a Unix style OS/terminal you can read the > output of stty. Look at the rows value > > Here are a couple of runs of rows at different terminal sizes: > > $ stty -a | grep rows > speed 38400 baud; rows 41; columns 80; line = 0; > > $ stty -a | grep rows > speed 38400 baud; rows 26; columns 80; line = 0; > > A call to Popen should get you what you want. > > An alternative technique, and the one useed by the real > more/less programmes is to use curses to open a display > window. > Hi Alan, this has helped me very much, thank you. I'll use the popen() before venturing into curses land. /\ Vincent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] I'm puzzled
Hi all, I'm puzzled (:-)) Why will this little program crash when you enter the enter key? while True: a = raw_input('number? ') if a.isdigit(): print 'isdigit' elif a[0] == '-' and a[1:].isdigit(): print '- + isdigit' elif a == 'q': break else: print 'no digit' /\ Vincent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] I'm puzzled
On Sunday, 22 January 2006 at 15:54:06 -0800, Danny Yoo wrote: > > > Why will this little program crash when you enter the enter key? > > [program cut] > > Hi Vincent, > > What do you mean by "crash"? Do you get an error message? If so, can you > show us? > > > It will also help to think to try playing the situation out without > preconceptions. In the beginning of the loop, at: > > > a = raw_input('number? ') > > what does 'a' contain when you hit enter? > > Hi Danny, the program works with any input except when you just hit the enter key. [EMAIL PROTECTED]:~/Desktop% python untitled.py number? Traceback (most recent call last): File "untitled.py", line 12, in ? elif a[0] == '-' and a[1:].isdigit(): IndexError: string index out of range /\ Vincent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] I'm puzzled
On Sunday, 22 January 2006 at 16:07:06 -0800, bob wrote: > At 03:46 PM 1/22/2006, Vincent Zee wrote: > >Why will this little program crash when you enter the enter key? > > Thank you for including the "traceback" message in your 2nd post. > > Index error means you tried to reference an element of a sequence > that is not there. a is the empty string when you just hit enter to > the raw_input request. It therefore has no elements, so a[0] raises > the exception. > > To avoid this test first for len(a) > 0. > > >while True: > >a = raw_input('number? ') > >if a.isdigit(): > >print 'isdigit' > >elif a[0] == '-' and a[1:].isdigit(): > >print '- + isdigit' > >elif a == 'q': > >break > >else: > >print 'no digit' > Ah, thank you Bob for the explanation. I understand now. /\ Vincent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] I'm puzzled
On Sunday, 22 January 2006 at 18:11:09 -0600, Hugo González Monteverde wrote: > Hi Vincent, > > > the program works with any input except when you just hit the enter key. > > > To be able to understand why is the crash, take a look at what the > interpreter tells you: > > > File "untitled.py", line 12, in ? > >elif a[0] == '-' and a[1:].isdigit(): > > IndexError: string index out of range > > IndexError is raised whan you try to access an element in a list or > string, an element that does not exist. In the case where you only press > enter, what is the content of a How many characters? (hint, you may > try to > Hi Hugo, thank you for your reply. What confused me was the fact that the isdigit method didn't complain about the empty string, so I assumed that indexing an empty string wouldn't be a problem (:-)) But now I think of it that wouldn't be logical. Sometimes the 'intelligence' of python makes me lazy (;-)) /\ Vincent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] I'm puzzled
On Monday, 23 January 2006 at 10:59:24 +1100, Shuying Wang wrote: > I'm guessing when you did that, you got something like an IndexError. > That's because you didn't check the length of "a" from raw_input and > accessed a nonexistent array element (a string is an array of > characters). So if you changed the line: > elif a[0] == '-' and a[1:].isdigit(): > to : > elif len(a) > 1 and a[0] == '-' and a[1:].isdigit(): > > I expect you'll get what you want. > Hi Shuying, thank you for your solution. /\ Vincent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor