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


Groups > comp.lang.python > #26400 > unrolled thread

Error help

Started bydanielashiloh@googlemail.com
First post2012-08-02 03:58 -0700
Last post2012-08-02 13:34 +0200
Articles 7 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  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

#26400 — Error help

Fromdanielashiloh@googlemail.com
Date2012-08-02 03:58 -0700
SubjectError help
Message-ID<ec01d1a9-f9e4-41ef-9e8a-71787bcd1aa9@googlegroups.com>
Hi all

This error has been bugging me for days. I know it's minor, but it's really getting in the way of my programming. I'm programming a data analysis programme for a psychology experiment which takes pickled data from the experiment and produces averages. For some reason python is insisting there is an indentation error on line 18. I have untabified and retabified and nothing seems to work. Here is the full code:


import cPickle, pickle

print 'WELLCOME TO THE LIBET CLOCK EXPERIMENT DATA ANALYSIST'
file=raw_input('\nPLEASE ENTER THE NAME OF THE FILE YOU WISH TO ANALYSE: ')
pickle_file=open(file, 'r')
data=cPickle.load(pickle_file)
file_name=file-'pck' + 'txt'
partifipant_info=data'Participant Info']
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']


def firstBlockDifferences():
    print '\nCALCULATING AVERAGE DIFFERENCES FOR BLOCK 1' 
    count=0
    first_block_differences=[]
    total=0
    while count<len(first_block_estimates):
        differences=float(first_block_estimates[count])-float(first_block_times[count])
        if differences >= 180:
            differences-=360
        elif differences <=-180:
            differences+=360
        total+=differences
        differences=[differences]
        first_block_differences+=differences
        count+=1
    average_diff_first_block=total/len(first_block_estimates)
    text_file.write('\nAverage differences for block 1: ', average_diff_first_block)

def secondBlockDifferences():
    print '\nCALCULATING AVERAGE DIFFERENCES FOR BLOCK 2'
    count=0
    second_block_differences=[]
    total=0
    while count<len(second_block_estimates):
        differences=float(second_block_estimates[count])-float(second_block_times[count])
        if differences >= 180:
            differences-=360
        elif differences <=-180:
            differences+=360
        total+=differences
        differences=[differences]
        second_block_differences+=differences
        count+=1
    average_diff_second_block=total/len(second_block_estimates)
    text_file.write('\nAverage differences for block 2: ', average_diff_second_block)


text_file=open(file_name, 'w')
text_file.write('\nParticipant info: ', participant_info)
firstBlockDifferences()
secondBlockDifferences()

I apologise for the lack of annotation and it may be a little inefficient, but I am really only after a solution to the error on line 18.

Thanks.

[toc] | [next] | [standalone]


#26403

Frommarmata@gmail.com
Date2012-08-02 04:23 -0700
Message-ID<fbb623c8-c3e5-4d69-95a2-3c9e802946d5@googlegroups.com>
In reply to#26400
Hi,

> This error has been bugging me for days.

Doesn't look like a tab error but:

> partifipant_info=data'Participant Info']

This line is wrong, it should be:

partifipant_info=data['Participant Info']

That's probably what's triggering the error!

Cheers,
]\/[arco

[toc] | [prev] | [next] | [standalone]


#26404

FromPeter Otten <__peter__@web.de>
Date2012-08-02 13:18 +0200
Message-ID<mailman.2871.1343906691.4697.python-list@python.org>
In reply to#26400
danielashiloh@googlemail.com wrote:

> This error has been bugging me for days. I know it's minor, but it's
> really getting in the way of my programming. I'm programming a data
> analysis programme for a psychology experiment which takes pickled data
> from the experiment and produces averages. For some reason python is
> insisting there is an indentation error on line 18. 

I can't confirm that, I get an error in line 8

> partifipant_info=data'Participant Info']

where you forgot the opening '['. 

Please post your actual code and don't forget to include a cut-and-paste 
version of the traceback you get.

[toc] | [prev] | [next] | [standalone]


#26407

Fromdanielashiloh@googlemail.com
Date2012-08-02 06:12 -0700
Message-ID<c742a1a0-68a0-49ae-a0e1-e36dbd69105e@googlegroups.com>
In reply to#26404
On Thursday, 2 August 2012 12:18:59 UTC+1, Peter Otten  wrote:
> danielashiloh@googlemail.com wrote:
> 
> 
> 
> > This error has been bugging me for days. I know it's minor, but it's
> 
> > really getting in the way of my programming. I'm programming a data
> 
> > analysis programme for a psychology experiment which takes pickled data
> 
> > from the experiment and produces averages. For some reason python is
> 
> > insisting there is an indentation error on line 18. 
> 
> 
> 
> I can't confirm that, I get an error in line 8
> 
> 
> 
> > partifipant_info=data'Participant Info']
> 
> 
> 
> where you forgot the opening '['. 
> 
> 
> 
> Please post your actual code and don't forget to include a cut-and-paste 
> 
> version of the traceback you get.

Thank you, everyone, for the replies. It does seem to stem from that missing bracket, indeed the error did start coming up when I first included that line. Do I feel silly! Incidentally, the error message I got was for an unexpected indent. All is resolved now.

[toc] | [prev] | [next] | [standalone]


#26408

Fromdanielashiloh@googlemail.com
Date2012-08-02 06:12 -0700
Message-ID<mailman.2876.1343913144.4697.python-list@python.org>
In reply to#26404
On Thursday, 2 August 2012 12:18:59 UTC+1, Peter Otten  wrote:
> danielashiloh@googlemail.com wrote:
> 
> 
> 
> > This error has been bugging me for days. I know it's minor, but it's
> 
> > really getting in the way of my programming. I'm programming a data
> 
> > analysis programme for a psychology experiment which takes pickled data
> 
> > from the experiment and produces averages. For some reason python is
> 
> > insisting there is an indentation error on line 18. 
> 
> 
> 
> I can't confirm that, I get an error in line 8
> 
> 
> 
> > partifipant_info=data'Participant Info']
> 
> 
> 
> where you forgot the opening '['. 
> 
> 
> 
> Please post your actual code and don't forget to include a cut-and-paste 
> 
> version of the traceback you get.

Thank you, everyone, for the replies. It does seem to stem from that missing bracket, indeed the error did start coming up when I first included that line. Do I feel silly! Incidentally, the error message I got was for an unexpected indent. All is resolved now.

[toc] | [prev] | [next] | [standalone]


#26405

FromDave Angel <d@davea.name>
Date2012-08-02 07:27 -0400
Message-ID<mailman.2872.1343906863.4697.python-list@python.org>
In reply to#26400
On 08/02/2012 06:58 AM, danielashiloh@googlemail.com wrote:
> Hi all
>
> This error has been bugging me for days. I know it's minor, but it's really getting in the way of my programming. I'm programming a data analysis programme for a psychology experiment which takes pickled data from the experiment and produces averages. For some reason python is insisting there is an indentation error on line 18. I have untabified and retabified and nothing seems to work. Here is the full code:
>
>
> import cPickle, pickle
>
> print 'WELLCOME TO THE LIBET CLOCK EXPERIMENT DATA ANALYSIST'
> file=raw_input('\nPLEASE ENTER THE NAME OF THE FILE YOU WISH TO ANALYSE: ')
> pickle_file=open(file, 'r')
> data=cPickle.load(pickle_file)
> file_name=file-'pck' + 'txt'
> partifipant_info=data'Participant Info']
> 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']
>
>
> def firstBlockDifferences():
>     print '\nCALCULATING AVERAGE DIFFERENCES FOR BLOCK 1' 
>     count=0
>     first_block_differences=[]
>     total=0
>     while count<len(first_block_estimates):
>         differences=float(first_block_estimates[count])-float(first_block_times[count])
>         if differences >= 180:
>             differences-=360
>         elif differences <=-180:
>             differences+=360
>         total+=differences
>         differences=[differences]
>         first_block_differences+=differences
>         count+=1
>     average_diff_first_block=total/len(first_block_estimates)
>     text_file.write('\nAverage differences for block 1: ', average_diff_first_block)
>
> def secondBlockDifferences():
>     print '\nCALCULATING AVERAGE DIFFERENCES FOR BLOCK 2'
>     count=0
>     second_block_differences=[]
>     total=0
>     while count<len(second_block_estimates):
>         differences=float(second_block_estimates[count])-float(second_block_times[count])
>         if differences >= 180:
>             differences-=360
>         elif differences <=-180:
>             differences+=360
>         total+=differences
>         differences=[differences]
>         second_block_differences+=differences
>         count+=1
>     average_diff_second_block=total/len(second_block_estimates)
>     text_file.write('\nAverage differences for block 2: ', average_diff_second_block)
>
>
> text_file=open(file_name, 'w')
> text_file.write('\nParticipant info: ', participant_info)
> firstBlockDifferences()
> secondBlockDifferences()
>
> I apologise for the lack of annotation and it may be a little inefficient, but I am really only after a solution to the error on line 18.
>
> Thanks.

You didn't include the error message, so how are we supposed to know
which line is #18 ?  if I take your message literally, it's a blank line.

Even better, you could have reduced the code down to a minimum which
still shows the error.

As your code stands now, I get an error on line 10:

 File "daniel.py", line 10
    partifipant_info=data'Participant Info']
                                          ^
SyntaxError: invalid syntax

It appears you're missing a left bracket.

That line also has a strange spelling for participant, so you'd get an
error later when you tried to use participant_info.   Did you by any
chance retype your program into the message?  If you don't use
copy/paste, it's useless for us to study your code.

Finally, in case it matters, just what version of python did you use,
and on what OS?

-- 

DaveA

[toc] | [prev] | [next] | [standalone]


#26406

FromPhilipp Hagemeister <phihag@phihag.de>
Date2012-08-02 13:34 +0200
Message-ID<mailman.2873.1343907302.4697.python-list@python.org>
In reply to#26400

[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


[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web