Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #26406

Re: Error help

Date 2012-08-02 13:34 +0200
From Philipp Hagemeister <phihag@phihag.de>
Subject Re: Error help
References <ec01d1a9-f9e4-41ef-9e8a-71787bcd1aa9@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.2873.1343907302.4697.python-list@python.org> (permalink)

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

Let's go thorugh this program:

On 08/02/2012 12:58 PM, danielashiloh@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


Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Error help danielashiloh@googlemail.com - 2012-08-02 03:58 -0700
  Re: Error help marmata@gmail.com - 2012-08-02 04:23 -0700
  Re: Error help Peter Otten <__peter__@web.de> - 2012-08-02 13:18 +0200
    Re: Error help danielashiloh@googlemail.com - 2012-08-02 06:12 -0700
    Re: Error help danielashiloh@googlemail.com - 2012-08-02 06:12 -0700
  Re: Error help Dave Angel <d@davea.name> - 2012-08-02 07:27 -0400
  Re: Error help Philipp Hagemeister <phihag@phihag.de> - 2012-08-02 13:34 +0200

csiph-web