In Python, I wanted to reuse a piece of code - a Python module -
which printed it's output, rather than returning an output value.
The object oriented nature of Python made it easy to write an
output redirector that caused printed output from a function to
be returned as a list of strings. Any object that supports read/
write/readline(s)/writelines/etc. can be used in place of a file,
including the stdin and stdout.
tolines( func, arg1 [, ... argn ] )
applies func to args, returning stdout output as a list of lines,
and is typically used in the calling program as:
for line in tolines( func, arg1 [,... argn] ) :
# process line
# and write output or accumulate results
( In the particular case that inspired this, I wanted to insert
source code statements into the output of the python byte-code
disassembler, but I didn't want to have to read and understand
the disassembler to modify it. )
If one were planning for code reuse in the first place, rather
that printing output, it would be better to return an object
that could be processed further if desired, but which had a
formatted print representation, so that out could either:
print func_a( args ) # print returned objects print repr string,or
func_b( func_a( args )) # compose it's result with another function.
But nested function would get awkward if the got to be nested very
deep. It would be possible to return a class that had a 'pipe' method,
fa( args ).pipe( fb, args ).pipe( fc, args ).pipe( fd, args ).print()
or maybe, to make it look more regular:
Mkpipe( fa,args ).pipe( fb, args )...
with a helper function like tolines to coerce output from functions
that weren't written to return the proper object.
Python also has a module that sets up pipeline templates for external
commands, fills in filenames or other variables into the template and
executes them. It should be possible to do the same sort of thing
with internal functions - build a pipeline template and execute then
execute it with args.
-- Steve Majewski (804-982-0831) <sdm7g@Virginia.EDU> --
-- UVA Department of Molecular Physiology and Biological Physics --
"Cognitive Science is where Philosopy goes when it dies, if it hasn't
been good" - Jerry Fodor.
-- Steve Majewski (804-982-0831) <sdm7g@Virginia.EDU> -- -- UVA Department of Molecular Physiology and Biological Physics -- -- Box 449 Health Science Center Charlottesville,VA 22908 --