Showing posts with label rulers. Show all posts
Showing posts with label rulers. Show all posts

Wednesday, April 13, 2016

A quick console ruler in Python

By Vasudev Ram

I've done this ruler program a few times before, in various languages.

Here is an earlier version: Rule the command-line with ruler.py!

This one is a simplified and also slightly enhanced version of the one above.

It generates a simple text-based ruler on the console.

Can be useful for data processing tasks related to fixed-length or variable-length records, CSV files, etc.

With REPS set to 8, it works just right for a console of 80 columns.

Here is the code:
# ruler.py
"""
Program to display a ruler on the console.
Author: Vasudev Ram
Copyright 2016 Vasudev Ram - http://jugad2.blogspot.com
0123456789, concatenated.
Purpose: By running this program, you can use its output as a ruler,
to find the position of your own program's output on the line, or to 
find the positions and lengths of fields in fixed- or variable-length 
records in a text file, fields in CSV files, etc.
"""

REPS = 8

def ruler(sep=' ', reps=REPS):
    for i in range(reps):
        print str(i) + ' ' * 4 + sep + ' ' * 3,
    print '0123456789' * reps

def main():

    # Without divider.
    ruler()

    # With various dividers.
    for sep in '|+!':
        ruler(sep)

if __name__ == '__main__':
    main()
And the output:
$ python ruler.py
0         1         2         3         4         5         6         7         
01234567890123456789012345678901234567890123456789012345678901234567890123456789
0    |    1    |    2    |    3    |    4    |    5    |    6    |    7    |    
01234567890123456789012345678901234567890123456789012345678901234567890123456789
0    +    1    +    2    +    3    +    4    +    5    +    6    +    7    +    
01234567890123456789012345678901234567890123456789012345678901234567890123456789
0    !    1    !    2    !    3    !    4    !    5    !    6    !    7    !    
01234567890123456789012345678901234567890123456789012345678901234567890123456789
You can also import it as a module in your own program:
# test_ruler.py
from ruler import ruler
ruler()
# Code that outputs the data you want to measure 
# lengths or positions of, goes here ...
print 'NAME      AGE  CITY'
ruler()
# ... or here.
print 'SOME ONE   20  LON '
print 'ANOTHER    30  NYC '
$ python test_ruler.py
Output:
0         1         2         3         4         5         6         7         
01234567890123456789012345678901234567890123456789012345678901234567890123456789
NAME      AGE  CITY
0         1         2         3         4         5         6         7         
01234567890123456789012345678901234567890123456789012345678901234567890123456789
SOME ONE   20  LON 
ANOTHER    30  NYC 

- Enjoy.

- Vasudev Ram - Online Python training and consulting

Signup to hear about my new products and services.

My Python posts     Subscribe to my blog by email

My ActiveState recipes


Monday, May 19, 2014

Rule the command-line with ruler.py!

By Vasudev Ram


While working at the command-line, I came up with the idea for this little Python utility which displays a ruler on the console. You can use this program's output as a ruler, to find the character positions and lengths of parts of your own program's output on the line, or to measure the lengths of fields in fixed-length or variable-length records in a text file or CSV file which you need to process via your program. And of course, your program can be in any language, not just Python.

Note: the program is written assuming a console width of 80 characters. You can change some of the values in it if you need it to work with a console of a different width.

I've called it ruler.py. Here is the code for it:
"""
# ruler.py - A program to display a ruler on the command line.
Copyright 2014 Vasudev Ram - http://www.dancingbison.com
Program to display a line on the command-line screen.
The line consists of repeated occurrences of the characters:
0123456789, concatenated.
Purpose: By running this program, you can use its output as a ruler,
to find the position of your own program's output on the line, or to 
measure the lengths of fields in fixed- or variable-length records in a text file,  
fields in CSV files, etc.
"""

import sys

def ruler(units="0123456789", repeats=8, fives=True):
    for i in range(repeats):
        if i < repeats - 1:
            if fives:
                sys.stdout.write(str(i) + (" " * 4) + "5" + (" " * 4))
            else:
                sys.stdout.write(str(i) + (" " * 9))
        else:
            if fives:
                sys.stdout.write(str(i) + (" " * 4) + "5" + (" " * 3))
            else:
                sys.stdout.write(str(i) + (" " * 8))
    sys.stdout.write("\n")
    #sys.stdout.write(units * repeats + "\n")
    sys.stdout.write(units * repeats)
    sys.stdout.flush()

def main():
    ruler()

if __name__ == "__main__":
    main()
# EOF
And here is its output:
$ python ruler.py
0    5    1    5    2    5    3    5    4    5    5    5    6    5    7    5
01234567890123456789012345678901234567890123456789012345678901234567890123456789
$
And the output from this call to ruler():
ruler(repeats=8, fives=False)
0         1         2         3         4         5         6         7
01234567890123456789012345678901234567890123456789012345678901234567890123456789
I've parameterized the values it uses, to some extent, so you can customize them. You can also import the file ruler.py as a module and call its ruler() function in your own Python program. The code for doing that is as simple as:
# test_ruler.py

from ruler import ruler

ruler()
The Wikipedia article on rulers is interesting. So is the Golomb ruler. And here is a rolling ruler:

- Vasudev Ram - Dancing Bison Enterprises

Contact Page