Error help

Philipp Hagemeister phihag at phihag.de
Thu Aug 2 13:34:50 CEST 2012


Let's go thorugh this program:

On 08/02/2012 12:58 PM, danielashiloh at googlemail.com wrote:
> import cPickle, pickle
you probably want to transparently fall back to pickle if cPickle is not
available.

> print 'WELLCOME TO THE LIBET CLOCK EXPERIMENT DATA ANALYSIST'
> file=raw_input('\nPLEASE ENTER THE NAME OF THE FILE YOU WISH TO ANALYSE: ')
This should be a command-line option. Look for optparse, or use sys.argv[1]

> pickle_file=open(file, 'r')
> data=cPickle.load(pickle_file)
You're opening a file in text mode, and leaking the handle.

> file_name=file-'pck' + 'txt'
You cannot subtract strings. You probably want to use + . Also, move
this assignment down to where it's needed

> partifipant_info=data'Participant Info']
You later use participant_info. typo?

> first_block_data=data['First block data']
> second_block_data=data['Second block data']
> first_block_estimates=first_block_data['Estimated times']
> first_block_times=first_block_data['Actual times']
> second_block_estimates=second_block_data['Estimated times']
> second_block_times=second_block_data['Actual times']

This is not a useful data structure. You want:

blocks = [{
  'estimates': data[name]['Estimated times'],
  'actuals': data[name]['Actual Times'],
} for name in ["First block data", "Second block data"}]

or better yet, a sane pickle format.

> def firstBlockDifferences():
This method should be called blockDifferences, and take a block as an
argument, and return differenes. See
http://docs.python.org/tutorial/controlflow.html#defining-functions for
an introduction.

>     print '\nCALCULATING AVERAGE DIFFERENCES FOR BLOCK 1' 
This belongs in the calling method.

>     count=0
>     first_block_differences=[]
>     total=0
>     while count<len(first_block_estimates):
You want to use zip here, like
for estimate,actual in zip(block['estimates'], block['actuals']):

>         differences=float(first_block_estimates[count])-float(first_block_times[count])
>         if differences >= 180:
>             differences-=360
>         elif differences <=-180:
>             differences+=360
What if the value is larger than 540 or smaller than -540?
>         total+=differences

>         differences=[differences]
This line looks strange. Please use another variable name for values of
another type.

>         first_block_differences+=differences
If that's all there is to this function, have a look at writing a
generator. If you delete the previous line, you can also write jsut
first_block_differences.append(differences)

>         count+=1
Not required if you use zip above.

>     average_diff_first_block=total/len(first_block_estimates)
>     text_file.write('\nAverage differences for block 1: ', average_diff_first_block)
These two lines don't look as if they belong in this method. Better
write another one that wraps the calculation and outputs stuff.

> 
> def secondBlockDifferences():
Instead of writing two methods to do the same thing, write one with
arguments.
>     (...)

> text_file=open(file_name, 'w')
You're leaking the handle. Use with(open(file_name, 'w')) as text_file: .

> text_file.write('\nParticipant info: ', participant_info)
> firstBlockDifferences()
> secondBlockDifferences()



- Philipp


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20120802/4a8d06ab/attachment.sig>


More information about the Python-list mailing list