LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-26-2017, 03:46 AM   #1
biplabbijay
Member
 
Registered: Jul 2009
Posts: 89

Rep: Reputation: 15
TypeError: 'NoneType' object is not iterable


Hi. I am trying to learn python. I have python 2.7 installed in my laptop with ubuntu 16.10 as operating system. I wrote a python script (just for the sake of learning) which is attached here. It reads a datafile (attached here) and prints a list. In the code there is a function chunk(1, n). Now what I knew was this function creates n-sized chunks and if we list it each of the chunk must contains 5 elements.

Now please look into the following code (its name is len.py, which is attached here with name len.py.txt).

Code:
#!/usr/bin/python
import os, sys, math
from astropy.io import ascii
import multiprocessing as mp
numProcess = 20

list1, list2 = [123, 'xyz', 'zara'], [456, 'abc']

print "Length of list1 = ", len(list1)
print "Length of list2 = ", len(list2)
for i in range(len(list1)):
    print i, list1[i]

for var in list(range(len(list1))):
    print var

q = raw_input("Please enter the input file name with its full path:\n")
print q

table1 = ascii.read(q, names=('Cand_no' ,'Freq', 'Period(ms)', 'SNR', 'DM', 'Filterbank_file', 'Location')) 
print len(table1)

def chunk(List, n):

    if len(List) < n:
    #each list has only one element
        output = []
        for i in range(len(List)):
            output.append([])
        for i in range(len(List)):
            output[i].append(List[i])
        return output
    if len(List) > n:
        output=[]
        for i in range(n):
            output.append([])
        for i in range(len(List)):
            output[i%n].append(List[i])
        return output

subTable = list(chunk(range(len(table1)), int(numProcess)))
for var in list(chunk(range(len(table1)), int(numProcess))):
    print "subTable", var
But here if I put numProcess = 20, then I am getting an error
Quote:
Length of list1 = 3
Length of list2 = 2
0 123
1 xyz
2 zara
0
1
2
Please enter the input file name with its full path:
/home/biplabbijay/Desktop/script_test/dataset.fold
/home/biplabbijay/Desktop/script_test/dataset.fold
20
Traceback (most recent call last):
File "len.py", line 41, in <module>
subTable = list(chunk(range(len(table1)), int(numProcess)))
TypeError: 'NoneType' object is not iterable
Here is my data file "dataset.fold". I am also attaching it with .txt extension.

Quote:
Cand_no Freq Period(ms) SNR DM Filterbank_file Location
115 550.80627441 1.81552124 10.50 5.551279 378D-014R315.57753.307353220123.gpt.fil /Data/pulsar/filfiles/31dec2016/
116 9.12576008 109.58514404 11.60 24.716806 166D-005R317.57781.118710751311.gpt.fil /Data/pulsar/filfiles/28jan2017/
115 550.80627441 1.81552124 10.50 5.551279 378D-014R315.57753.307353220123.gpt.fil /Data/pulsar/filfiles/31dec2016/
116 9.12576008 109.58514404 11.60 24.716806 166D-005R317.57781.118710751311.gpt.fil /Data/pulsar/filfiles/28jan2017/
115 550.80627441 1.81552124 10.50 5.551279 378D-014R315.57753.307353220123.gpt.fil /Data/pulsar/filfiles/31dec2016/
116 9.12576008 109.58514404 11.60 24.716806 166D-005R317.57781.118710751311.gpt.fil /Data/pulsar/filfiles/28jan2017/
115 550.80627441 1.81552124 10.50 5.551279 378D-014R315.57753.307353220123.gpt.fil /Data/pulsar/filfiles/31dec2016/
116 9.12576008 109.58514404 11.60 24.716806 166D-005R317.57781.118710751311.gpt.fil /Data/pulsar/filfiles/28jan2017/
115 550.80627441 1.81552124 10.50 5.551279 378D-014R315.57753.307353220123.gpt.fil /Data/pulsar/filfiles/31dec2016/
116 9.12576008 109.58514404 11.60 24.716806 166D-005R317.57781.118710751311.gpt.fil /Data/pulsar/filfiles/28jan2017/
115 550.80627441 1.81552124 10.50 5.551279 378D-014R315.57753.307353220123.gpt.fil /Data/pulsar/filfiles/31dec2016/
116 9.12576008 109.58514404 11.60 24.716806 166D-005R317.57781.118710751311.gpt.fil /Data/pulsar/filfiles/28jan2017/
115 550.80627441 1.81552124 10.50 5.551279 378D-014R315.57753.307353220123.gpt.fil /Data/pulsar/filfiles/31dec2016/
116 9.12576008 109.58514404 11.60 24.716806 166D-005R317.57781.118710751311.gpt.fil /Data/pulsar/filfiles/28jan2017/
115 550.80627441 1.81552124 10.50 5.551279 378D-014R315.57753.307353220123.gpt.fil /Data/pulsar/filfiles/31dec2016/
116 9.12576008 109.58514404 11.60 24.716806 166D-005R317.57781.118710751311.gpt.fil /Data/pulsar/filfiles/28jan2017/
115 550.80627441 1.81552124 10.50 5.551279 378D-014R315.57753.307353220123.gpt.fil /Data/pulsar/filfiles/31dec2016/
116 9.12576008 109.58514404 11.60 24.716806 166D-005R317.57781.118710751311.gpt.fil /Data/pulsar/filfiles/28jan2017/
115 550.80627441 1.81552124 10.50 5.551279 378D-014R315.57753.307353220123.gpt.fil /Data/pulsar/filfiles/31dec2016/
116 9.12576008 109.58514404 11.60 24.716806 166D-005R317.57781.118710751311.gpt.fil /Data/pulsar/filfiles/28jan2017/
What is the reason behind this ? How can I get rid of this (debug) this. Can anyone explain whether I am doing it right or wrong ? Thanks in advance.
Attached Files
File Type: txt dataset.fold.txt (3.0 KB, 25 views)
File Type: txt len.py.txt (1.1 KB, 37 views)

Last edited by biplabbijay; 10-26-2017 at 03:52 AM. Reason: attaching files.
 
Old 10-26-2017, 08:22 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,816

Rep: Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071
I would suggest testing the variables you are passing in to your recursive function as at some point you are passing in something of the wrong type (as per error message)

IMHO the greatest tool at the developers finger tips is the print / output to display option we all learn in the original hello world script you first write
 
Old 10-26-2017, 10:12 PM   #3
biplabbijay
Member
 
Registered: Jul 2009
Posts: 89

Original Poster
Rep: Reputation: 15
I have done that too. I found that if len(table1) = numProcess, thios error comes out. I can bypass it. But I want to know why exactly it is happening. Please help me in this regard.



Biplab
 
Old 10-27-2017, 07:48 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,816

Rep: Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071
Well I have to say, after some investigation, for someone who is 'learning' python you have jumped in a deep end by using a non-standard library (astropy).
So this limits me as I am not prepared to install a library I don't need.

So here are some things to look at:

1. Confirm you are only running python 2.7 and not 3.X as you have only listed python as the interpreter which normally defaults to the higher version.

2. The error advises that one of the objects you are calling is of type None, hence you need to work out which that is?

3. Is the error actually on the first call to the function or is it inside the call?

Points to note (and can be taken with a grain of salt):

1. Try and have the code flow better by either having method definitions before or after all the individual code

2. (as not a python guru) I think you need to look over all your for loops as I am pretty sure you shouldn't need constructs like the following:
Code:
for var in list(range(len(list1))):

See how you go
 
Old 10-29-2017, 05:51 AM   #5
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 9,985

Rep: Reputation: 4569Reputation: 4569Reputation: 4569Reputation: 4569Reputation: 4569Reputation: 4569Reputation: 4569Reputation: 4569Reputation: 4569Reputation: 4569Reputation: 4569
Repost with the indendation added back to the exception message. That's necessary information.
 
Old 10-30-2017, 07:57 AM   #6
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,078
Blog Entries: 4

Rep: Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187
Also, please understand that "the 'Programming' forum is not a debugging service." I have plenty of my own bugs to deal with today.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Python 2.7 - AttributeError: 'NoneType' object has no attribute 'strip' glupa4e Slackware 14 08-29-2016 01:35 PM
[SOLVED] Python: error - TypeError: 'str' object is not callable angel115 Programming 3 06-24-2014 09:10 AM
django: NoneType stateless Programming 0 02-15-2013 02:45 AM
[SOLVED] python3 - trying to pickle object raises TypeError comp_brad1136 Programming 2 04-06-2011 07:37 AM
[SOLVED] Python cannot concatenate 'str' and 'NoneType' objects madsovenielsen Programming 4 06-24-2010 01:44 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration