You are not logged in.

#1 2005-09-13 17:05:53

xerxes2
Member
From: Malmoe, Sweden
Registered: 2004-04-23
Posts: 1,249
Website

python, printing to terminal

is there a way to control printing to a terminal?

i want to write to the same line and also change the same line,
like pacman does it when it shows how much you have downloaded of a file,


arch + gentoo + initng + python = enlisy

Offline

#2 2005-09-13 17:15:41

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Offline

#3 2005-09-13 17:58:15

xerxes2
Member
From: Malmoe, Sweden
Registered: 2004-04-23
Posts: 1,249
Website

Re: python, printing to terminal

that looks like what i was looking for,

couldn't you supply some code too tongue
now i have to rtfm also 'before' i start to code,


arch + gentoo + initng + python = enlisy

Offline

#4 2005-09-13 18:00:24

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: python, printing to terminal

Problem is, I've never used ncurses in any programming language. :-P I just knew that pacman used it to get that one lie output (from passing comment Xentac made once), and I remembered that Python had a curses module.

Its the best I could do on such short notice. Would you like to write my master's thesis? :-P

Dusty

Offline

#5 2005-09-13 18:07:22

xerxes2
Member
From: Malmoe, Sweden
Registered: 2004-04-23
Posts: 1,249
Website

Re: python, printing to terminal

give me a few weeks and i'll do it,

this curses stuff is not so easy to dig into, i just want to make oneliners, it shouldn't be like rocket science,


arch + gentoo + initng + python = enlisy

Offline

#6 2005-09-13 18:45:51

soniX
Member
From: Oslo, Norway
Registered: 2004-01-23
Posts: 161

Re: python, printing to terminal

Is this what you need ?(sorry for writing an example in your favourite language xerces2)

public class JCurses {
    public static void main(String[]args) throws Exception{
        String[] pacman = new String[]{
        "Pacmanr",
        "pAcmanr",
        "paCmanr",
        "pacManr",
        "pacmAnr",
        "pacmaNr",
        "pacmann"};
        
        for(String p:pacman){
            System.out.print(p);
            Thread.sleep(1000);
        }
    }
}

all pacman does is a simple carriage return at the end.
if python doesnt have a built in carriage return, just print its ascii value 13 as a character.

(put the code in a file named JCurses.java
compile it with "javac JCurses.java
run it with "java JCurses")

Offline

#7 2005-09-13 19:39:34

xerxes2
Member
From: Malmoe, Sweden
Registered: 2004-04-23
Posts: 1,249
Website

Re: python, printing to terminal

in python please,

and i don't understand what you mean anyway,


arch + gentoo + initng + python = enlisy

Offline

#8 2005-09-13 20:19:38

soniX
Member
From: Oslo, Norway
Registered: 2004-01-23
Posts: 161

Re: python, printing to terminal

Sorry for writing it in java (dont really know python).
anyway::
The curses part of pacmans "download printing" is printing the carriage return (ascii value 13) as a character to the end of each line until 100% download is reached.Then it prints linefeed (ascii 10) to change the line.
In other words : you should be able to overwrite your prints if you supply a carriage return at the end (like in my java prog and pacman) if python doesnt do anything strange behind your back when you do a print.

Offline

#9 2005-09-13 20:22:36

xerxes2
Member
From: Malmoe, Sweden
Registered: 2004-04-23
Posts: 1,249
Website

Re: python, printing to terminal

are you saying that i don't have to use curses?


arch + gentoo + initng + python = enlisy

Offline

#10 2005-09-13 20:27:17

soniX
Member
From: Oslo, Norway
Registered: 2004-01-23
Posts: 161

Re: python, printing to terminal

yes that is exactly what I'm saying.

edit: Well atleast I didnt need anything to make it work in java... I dont know python so I cant really say if you need it or not.

Offline

#11 2005-09-13 21:10:46

tmaynard
Member
Registered: 2005-07-29
Posts: 34

Re: python, printing to terminal

Try this :

import curses
import time

stdscr = curses.initscr()

myStrings=["Pacman working ...","pAcman working ...","paCman working ...",
           "pacMan working ...","pacmAn working ...","pacmaN working ...",
           "pacman Working ...","pacman wOrking ...","pacman woRking ...",
           "pacman worKing ...","pacman workIng ...","pacman workiNg ...",
           "pacman workinG ..."]

for eachString in myStrings:
    stdscr.addstr(0, 0, eachString,curses.A_REVERSE)
    time.sleep(1)
    stdscr.refresh()

curses.endwin()

Is this kinda what you are loooking for?

Offline

#12 2005-09-13 21:18:32

xerxes2
Member
From: Malmoe, Sweden
Registered: 2004-04-23
Posts: 1,249
Website

Re: python, printing to terminal

sonix stuff doesn't seem to work,
i will try tmaynards curses sollution also

my function is beeing fed by another function so how do i deal with that?
or should i have one curses function like tmaynards and use sleep()?


arch + gentoo + initng + python = enlisy

Offline

#13 2005-09-13 21:20:30

tmaynard
Member
Registered: 2005-07-29
Posts: 34

Re: python, printing to terminal

Non curses version:

import sys
import time



myStrings=["Pacman working ...","pAcman working ...","paCman working ...",
           "pacMan working ...","pacmAn working ...","pacmaN working ...",
           "pacman Working ...","pacman wOrking ...","pacman woRking ...",
           "pacman worKing ...","pacman workIng ...","pacman workiNg ...",
           "pacman workinG ..."]

for eachString in myStrings:
    sys.stdout.write(eachString)
    sys.stdout.flush()
    sys.stdout.write("r")
    time.sleep(1)
    

print("n")

Enjoy!

Offline

#14 2005-09-13 21:43:53

xerxes2
Member
From: Malmoe, Sweden
Registered: 2004-04-23
Posts: 1,249
Website

Re: python, printing to terminal

big_smile big_smile
*xerxes hugs tmaynard very hard* tongue

that last stuff works beutifully,

edit: if you feel lucky you can get the evidence on svn,
more info here: http://home.gna.org/libpypac/


arch + gentoo + initng + python = enlisy

Offline

#15 2005-09-14 05:59:21

tmaynard
Member
Registered: 2005-07-29
Posts: 34

Re: python, printing to terminal

I'd be curious to see your implementation of this.  There are a few different ways to do this if you are trying to do a "progress bar" type thing depending on what you are actually doing.

Offline

#16 2005-09-14 10:37:19

xerxes2
Member
From: Malmoe, Sweden
Registered: 2004-04-23
Posts: 1,249
Website

Re: python, printing to terminal

just follow the subversion installation instructions at the link above,
you need lazy-pac-cli-devel too which follow the same steps as libpypac-devel, you'll get it by the same checkout,


arch + gentoo + initng + python = enlisy

Offline

Board footer

Powered by FluxBB