I have a program that produces an array of data and prints it out very nicely. The challenge for which I seek a solution is as follows:
- Muck up the data in the array before it gets printed, e.g., sticking in non-UTF-8 characters, putting the fields in the wrong order, changing the date format.
- Let the user pick which "muck ups" occur.
Here is a stripped-down version of my existing program that works, whose output I wish to intercept and muck up:
ex_matrix = [[402, 'Mayonnaise', '0123', 2014-12-18, '(Burlington, -, -)', 1.0],
[413, 'Peanuts', '0177', 2014-11-10, '(Place, Florida, South Afrika)', 1.0],
[415, 'Seaweed', '0713', 2014-12-02, '(The Pub, -, Newfoundland)', 1.0]]
def print_forecasts(matrix, num_rows):
for r in xrange(num_rows):
print("{%s, [%s, %s, %s, %s], %s}" % (matrix[r][0], matrix[r][1],
matrix[r][2], matrix[r][3], matrix[r][4], matrix[r][5]))
print "# Generated using pickles_program.py \n"
def main():
print_forecasts(ex_matrix, 3)
main()
From what I've read of Python's argparse tutorial, the PEP re. argparse, and several StackOverflow answers, it seems argparse is the key to this problem. Here is what I've written, just trying to get a feel for how argparse works:
import argparse
num_rows = 3
parser = argparse.ArgumentParser(description="base rate model: error adder")
parser.add_argument("muck2", help="muck up the population field", nargs='?')
args = parser.parse_args()
for i in xrange(num_rows):
matrix[i][1] = "^&*#$)(*DJJJJ)"
print matrix
There will be >10 types of muck-up's for the user to choose from. What I think I would like would be for the user to be able to say to the command-line, "python pickles_program.py 1 3 8 11," or something like that, and have the muck-up's 1, 3, 8 and 11 happen, and for "python pickles_program.py --help" to display all the muck-up options.
I hope you'll forgive me if this is a dull-witted question. I'm a Python novice and still learning how to use the many resources there are for learning this great language, but I've searched SO and Python documentation high and low -- please believe me, if there is an answer to my problem out there, it's either not been explained well enough for people like me, or it's too darned hard to find, because I haven't found it yet.
Please advise, esp. on how I can pose my question better/more clearly!