Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 10th, 2005, 9:21 AM   #21
Klipt
Hobbyist Programmer
 
Join Date: Dec 2005
Posts: 118
Rep Power: 0 Klipt is an unknown quantity at this point
Sorry. Maybe it depends on your operating system. It works in WinXP...
Klipt is offline   Reply With Quote
Old Dec 10th, 2005, 9:35 AM   #22
UnKnown X
Hobbyist Programmer
 
UnKnown X's Avatar
 
Join Date: Dec 2005
Location: Sandvika, Norway
Posts: 114
Rep Power: 0 UnKnown X is an unknown quantity at this point
Send a message via MSN to UnKnown X
Never mind. It worked after all. Thanks.


Another question though: Is there a way to remove a number of indefinate size (or, failing that, clear the screen)?
UnKnown X is offline   Reply With Quote
Old Dec 10th, 2005, 10:19 AM   #23
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Smile

In the Windows command shell you can use:
import os
os.system("CLS")
Otherwise use a for loop with lots of print.
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old Dec 10th, 2005, 10:48 AM   #24
UnKnown X
Hobbyist Programmer
 
UnKnown X's Avatar
 
Join Date: Dec 2005
Location: Sandvika, Norway
Posts: 114
Rep Power: 0 UnKnown X is an unknown quantity at this point
Send a message via MSN to UnKnown X
Thanks!
UnKnown X is offline   Reply With Quote
Old Dec 10th, 2005, 5:01 PM   #25
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 8 Ooble is on a distinguished road
Instead of a for loop:
print '\n' * 25
Does the same thing, but it's easier to read.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Dec 10th, 2005, 5:59 PM   #26
UnKnown X
Hobbyist Programmer
 
UnKnown X's Avatar
 
Join Date: Dec 2005
Location: Sandvika, Norway
Posts: 114
Rep Power: 0 UnKnown X is an unknown quantity at this point
Send a message via MSN to UnKnown X
Ooh, quite fancy
UnKnown X is offline   Reply With Quote
Old Dec 11th, 2005, 7:59 AM   #27
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4 Cerulean is on a distinguished road
Yeah. If you know how many characters wide your terminal is (you can find this by reading the output of `tput cols` on *nix) you can have you can easily clear the current line and write new text to it. So for example (untested):
import time, sys
# Get the amount of characters wide the terminal is. 
# On *nix you can do: int(os.popen("tput cols").read().strip())
width = getTerminalWidth()
message1 = "Message number 1"
# Print message1 to the terminal, padded to the end of the line with spaces. 
# If we pad the output then we know how many backspaces characters
# we need to write out to clear the whole line. width - len(message1) will
# give us the amount of space characters we need.
sys.stdout.write(message1 + " " * (width - len(message1)))
# Flush the text we've written to stdout. If Python doesn't see a newline
# it doesn't flush for you.
sys.stdout.flush()
# Pause execution for a second...
time.sleep(1)
# Now clear the line
sys.stdout.write("\b" * width)
# And write a new message
sys.stdout.write("Message number 2...\n")
I used this technique to make a command-line progress bar, for example.
Cerulean is offline   Reply With Quote
Old Dec 11th, 2005, 2:32 PM   #28
Klipt
Hobbyist Programmer
 
Join Date: Dec 2005
Posts: 118
Rep Power: 0 Klipt is an unknown quantity at this point
Doesn't '\r' take you to the beginning of the line you're on?
Klipt is offline   Reply With Quote
Old Dec 11th, 2005, 4:19 PM   #29
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 8 Ooble is on a distinguished road
Yip. Should help you a bit, Cerulean:
sys.stdout.write("\b" * width)
sys.stdout.write("\r")
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Dec 14th, 2005, 1:28 AM   #30
hydroxide
Programmer
 
Join Date: Apr 2005
Posts: 73
Rep Power: 4 hydroxide is on a distinguished road
Quote:
Originally Posted by Ooble
Same here, actually - I actually find it easier to read than the other stuff. But then I think x++; makes sense.
Proof of concept only - so fragile it'll break if you breathe on it. DO NOT USE THIS!:
class Evil(object):
    def __init__(self, val):
        self.val = int(val)
        self.state = False

    def __add__(self, other):
        return Evil(self.val + other)

    def __pos__(self):
        if self.state:
            self.val += 1
        self.state = not self.state
        return self

    def __repr__(self):
        return str(self.val)

x = Evil(2)
++x
print x
y = (++x) + 2
++y
print y
--OH.
hydroxide is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:00 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC