Reducing cache/buffer for faster display

Rikishi42 skunkworks at rikishi42.net
Fri Sep 28 20:31:33 EDT 2012


On 2012-09-28, Chris Angelico <rosuav at gmail.com> wrote:
> On Fri, Sep 28, 2012 at 10:05 AM, Rikishi42 <skunkworks at rikishi42.net> wrote:
>> The scripts in question only increase numbers. But should that not be the
>> case, solutions are simple enough. The numbers can be formatted to have a
>> fixed size. In the case of random line contents (a list of filesnames, say)
>> it's enough to create an output function that is aware of the length of the
>> previously printed line, and add enough spaces to the current one to wipe
>> exess content.
>
> Yep, that's a pretty effective way to do it. One simple method to it
> is to format the whole string as a single whole, then left justify it
> in a field of (say) 79 characters, and output that:
>
> msg = "Progress: %d%% (%d/%d)... %s" % (done*100/total, done, total,
> current_file)
> print msg.ljust(79)+"\r",
> sys.stdout.flush()

Mmm, I allmost went for that. It's elegant, simple and clear. But there's
one drawback: I usually reduce the terminal's window to take up less desktop
surface during those long runs.  
So fixing it to 79 chars won't do.  And I'm not even tempted to go for a
detection of the width of the terminal from within the script.  The idea is
after all to keep the scripts simple (syntax) and light (execution).

Well, good night everyone.

-- 
When in doubt, use brute force.
                -- Ken Thompson



More information about the Python-list mailing list