Hi :-)

do you know how to move up one line in the terminal?

Explanation with an example:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from time import sleep

def test():
	
	print "begin"
	
	i = 1
	s = "#"
	
	while i < 5:
		
		print s
		sleep(1)
		i += 1
		s = s + "#"
		
	print "end"

def main():
	
	test()

if __name__ == '__main__': main()

That code will output this:

$ ./test.py 
begin
#
##
###
####
end

But i'd like to modify it so that it outputs this:

$ ./test.py 
begin
#
$ ./test.py 
begin
##
$ ./test.py 
begin
###
$ ./test.py 
begin
#####
end

So that there is an "animation like" effect...

Using a comma with the print is not the solution because the program only outputs the string once the program goes out of the while. In my exemple it outputs this if i use a comma with the print

First

$ ./test.py 
begin

And then:

$ ./test.py 
begin
# ## ### #### end

I hope my explanation is clear and understandable...

Thank you very much for your help! :-)

In linux, you can print at a given position in a terminal using ANSI escape sequences, a problem being that lines are numbered from the top of the terminal
Save this post named 'Formatting with colors in a linux console' as a module termstyle.py http://www.daniweb.com/code/snippet232375-2.html#post1028718, then run the following code

from termstyle import Style
from time import sleep
import sys

LINENO = 24

for i in range(20):
    print "{0}".format(Style((LINENO, i), "yellow")("#")),
    sys.stdout.flush()
    sleep(0.3)
    
print "{0}".format(Style((LINENO, i))(" end"))

It will do what you want.

It's not the only way to do in linux, another way is to use module curses, but it's a lot of work, also you could use a progressbar of one of the gui toolkits instead of a terminal based progress bar.

Edited 5 Years Ago by Gribouillis: n/a

Sorry for the double post, but it turns out that the above solution is over-complicated: using the print function and stdout.flush() does the trick

from __future__ import print_function
from time import sleep
import sys

for i in range(20):
    print("#", end="")
    sys.stdout.flush()
    sleep(0.3)

print(" end")

Hooooo yeah Gribouillis! :-D

This is exactly the result i wanted to have.

Nice! Thank you very much!! :)

Ps: are you french? If not, do you know "gribouillis" is a french word meaning "scrawl".
Anyway it's a funny nickname...

Hooooo yeah Gribouillis! :-D

This is exactly the result i wanted to have.

Nice! Thank you very much!! :)

Ps: are you french? If not, do you know "gribouillis" is a french word meaning "scrawl".
Anyway it's a funny nickname...

Actually I'm french :)

Also notice that even with the second solution, you can still use termstyle to print a colorful progressbar

from __future__ import print_function
from time import sleep
import sys
from termstyle import Style

chars = ["{0}".format(Style(c)("#")) for c in "yellow green red cyan".split()]

for i in range(20):
    print(chars[i%len(chars)], end="")
    sys.stdout.flush()
    sleep(0.3)

print(" end")

Edited 5 Years Ago by Gribouillis: n/a

Ha ben zut! :/ I don't have the termstyle module...

How can i solve this?

This is not the first time i have this kind of errors about some missing modules. i wonder, may be i have to reinstall python according to a precise method...?

Ha ben zut! :/ I don't have the termstyle module...

How can i solve this?

This is not the first time i have this kind of errors about some missing modules. i wonder, may be i have to reinstall python according to a precise method...?

It's not a known module, it's only the content of this post http://www.daniweb.com/code/snippet232375-2.html#post1028718 . You can run the post to see what it can do.

It seems that someone else started a similar module here https://github.com/gfxmonk/termstyle . It may be based on my post :) ?

Edit: actually, the first commit seems older...

Edited 5 Years Ago by Gribouillis: n/a

Thank you Gribouillis, interesting stuff.

Even if i currently don't need to use such things, it could be useful later.


Since i started learning python i didn't thought about the possibility to install python modules, and now, thanks to you, i'm reading:

http://docs.python.org/install/index.html :)

... i'm reading:

http://docs.python.org/install/index.html :)

I don't understand this sentence:

You should always run the setup command from the distribution root directory, i.e. the top-level subdirectory that the module source distribution unpacks into.

What is the "distribution root directory"? It means the root folder of my files system?
And what is a "top-level subdirectory"?

I don't understand this sentence:

What is the "distribution root directory"? It means the root folder of my files system?
And what is a "top-level subdirectory"?

The documentation means that for most distributed python modules found on the net, say a module foo, you can install it by downloading a compressed file foo.tgz, uncompressing the file to get a directory foo/, then issue "cd foo" and "sudo python setup.py install" (don't forget to read the foo/README and foo/INSTALL files first).

However, since you have a linux system, many python modules may be distributed through your system's package manager, then it's much better to install them through the package manager.

There are other methods to install python modules. Modules found on http://pypi.python.org/pypi can be installed with one of the commands easy_install or pip (pip is newer).

Otherwise, if you wrote a file named zzz.py and you want to install it as a module named zzz, the simplest way is to copy the file to your /usr/lib/python2.6/site-packages directory (if you have python 2.6), or to your ~/.local/lib/python2.6/site-packages if you don't need to make it accessible to other users.

Another solution is to copy the file in a directory referenced in your PYTHONPATH environment variable.

Edited 5 Years Ago by Gribouillis: n/a

This question has already been answered. Start a new discussion instead.