Utility is a generic framework for command-line utility programs.
Copy template.py to make new programs, or see Filter.py for a complete
example app.

A grep/sed/awk-like filter built on Utility.
Use filename - to read stdin

Filter.py -s "(?i)copyright" license.txt
	# prints all lines mentioning copyright, ignoring case.
	# Use `Filter.py --rehelp` to look up regular expression usage!

Filter.py -E example-center.py license.txt
	# Runs all lines through example-center.py script, which centers
	# the text horizontally.

Filter.py -b "self.data.total = 0" \
	-s "^(-?[0-9.]*)\s+(.*)$" -r "\1" \
	-e "self.data.total += float(line)" \
	-a "print('T:', self.data.total, ', AVG:', self.data.total / max(1, self.matched) )" \
	# -b (--before) script creates a running total.
	# -s (--search) and -r (--replace) match the first column of numbers.
	# -e (--exec) adds the number column to the total.
	# -a (--after) script prints the total and average of all lines.
	#	(I used max() to make sure we don't divide by 0)
	# Result should be T: 15.5 , AVG: 5.166666666666667

Filter has these instance fields, in addition to Utility's:
* self.matched
	Count of all lines which match the -s pattern.
* self.filename
	Current filename.
* self.linenum
	Current line number.
Type `Utility.py -h` for more.

Leave a Reply