Welcome, guest | Sign In | My Account | Store | Cart

This script echos the input with a time delay after each line. The input can be from standard input or a list of files on the command line, like the Unix 'cat' command. The delay between lines is adjustable, and defaults to 1 second.

Python, 37 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
"""dribble.py: Echo input with a time delay after each line

Usage: dribble.py [-time] [files...]

    'time' is the delay after lines in seconds, with a default of 1 second.
    'files...' is a list of files to echo.  If no files a specified,
               echo the standard input.
               
Examples:

        dribble.py -2 test1.txt test2.txt
    
    Echo the files 'test1.txt' and 'test2.txt' with a delay of 2 seconds
    between each line.
    
        ls -l | dribble.py -0.5
        
    Echo the directory listing produced by 'ls -l' with a delay of 1/2 seconds.
"""

import sys
import fileinput
import time

if len(sys.argv)>1:
    if sys.argv[1] in ('-h', '--help', '/?'):
        print __doc__
        sys.exit(0)
        
    delay_time = 1.0  # Seconds
    if sys.argv[1][0] == '-' and len(sys.argv[1])>1:
        delay_time = float(sys.argv[1][1:])
        sys.argv.pop(1)
    
for line in fileinput.input():
    print line,
    time.sleep(delay_time)
    

I wrote this script to feed commands to other programs, and the time delay helps keep those other programs from staturating network resources.

As usual, the documentation and handling command options takes up most of the space. If you are willing to have a fixed delay time, the script could be 3 lines long, excluding the imports.

1 comment

Edi Hance (author) 18 years, 5 months ago  # | flag

One Way To Do It. "There should be one-- and preferably only one --obvious way to do it."

Python Zen, by Tim Peters

In a moment of Python Zen, I found that Dan Gunter had already written

the same program and given it the same name:

2005-03-10  Dan Gunter  [email removed]

    * python/tools: [...] Also added a little utility 'dribble.py'
    to slowly copy data from stdin to stdout (for testing).

http://www-didc.lbl.gov/NetLogger/ChangeLog

From now on, I'm not going to write code. I'm just going to pick a name and Google it.