[Tutor] Need URGENT support ... PL

Steven D'Aprano steve at pearwood.info
Wed Aug 25 23:19:29 CEST 2010


On Thu, 26 Aug 2010 05:40:33 am nitin chandra wrote:
> Hi all,
>
> I have been getting support on this from the list, but unfortunately
> now it has become URGENT that i get some solution to this problem i
> need to resolve.

Is it "URGENT" enough that you would consider paid support?

Or are you asking for free support *and* immediate responsiveness as 
well? If so, why is *your* urgency important to us?


> Through the following code I am trying to read the TWO input files,
> extract columns, Add , Divide (take mean of the values) and write to
> THEIR respective OUTPUT files.
>
> My script reads the first file and displays all the rows (print) from
> PR1/File1 and then program terminates. IT does not read the Second
> file (PR3/File1) and therefore i am unable to write the result out to 
> the OUTPUT file (PR2/File1).

"Divide and conquer" is the most important programming principle. When a 
task is too complicated, break it into little pieces and solve each 
little piece alone. That's why functions exist! Instead of one great 
big script that tries to do everything, break the problem into 
functions.

You have a file that has the format:

filename1,filename3,filename2

repeated for many lines. Write a function that takes a *single* line and 
splits it into three filenames. That's easy:

def get_names(input_line):
    return input_line.strip().split(',')

Now write a function that does that to each and every line in a file:

def process_input_file(filename):
    for line in open(filename):
        f1, f3, f2 = input_line.strip().split(',')
        process(f1, f3, f2)


Now write the process function:

def process(infile1, infile2, outfile):
    """Read files infile1 and infile2, do something to their 
    content line-by-line, and write the output to outfile.
    """
    # whatever...

And then write your calculate function that operates on one row at a 
time:

def calculate(line1, line2):
    # whatever...


Just make sure it returns a string ending with a newline "\n".


And finally, put it all together:

if __name__ == '__main__':
    filename = raw_input("Enter the name of the file: ")
    process_input_file(filename.strip())


And that should be it.

If there are any problems, they will be easy to diagnose because you can 
isolate them to one small function instead of one big, confusing 
script. Do not use try...except and sys.exit to disguise where 
exceptions occur. If there is a bug or an error, you want to see the 
full traceback, not just a generic, useless error message.

I can't remember how much the company I work for charges for a monthly 
four-hour service level agreement, but we charge AUD$300 for ad-hoc 
priority service plus $150 an hour for consulting. Since I'm doing this 
in my own time, I'll do it for half that. You get the above for free 
because I'm a nice guy. (Or maybe a sucker.) If you still need "URGENT" 
support, contact me and we'll make arrangements.

Either that, or just wait until somebody feels like answering.



-- 
Steven D'Aprano


More information about the Tutor mailing list