Re: List objects are un-hashable

2007-04-29 Thread Andy
Thanks Michael and Ant.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: List objects are un-hashable

2007-04-27 Thread Michael Hoffman
Andy wrote:
> Hi, I'm trying to search and print any no# of Python keywords present
> in a text file (say - foo.txt), and getting the above error. Sad for
> not being able to decipher such a simple problem (I can come up with
> other ways - but want to fix this one FFS).

It helps a lot of if you post the traceback with your problem. The line 
it occurred on is crucial for debugging.

But I will use my psychic debugger which tells me that the error is here:

> if keyword.iskeyword(tempwords):

tempwords is a list. You need to iterate over the contents of the list 
and run keyword.iskeyword() for each one. Here's an example for Python 
2.5. I've cleaned up some extra lines of code that didn't have any 
eventual effects in your current implementation

from __future__ import with_statement

import keyword

INFILENAME = "foo.txt"

with open(INFILENAME) as infile:
 for line in infile:
 words = line.split()
 for word in words:
 if keyword.iskeyword(word):
 print word

This will print multiple lines per input line. If you wanted one line 
per input line then try:

with open(INFILENAME) as infile:
 for line in infile:
 words = line.split()
 print " ".join(word for word in words
if keyword.iskeyword(word))
-- 
Michael Hoffman
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: List objects are un-hashable

2007-04-27 Thread Ganesan Rajagopal
> "Andy" == Andy  <[EMAIL PROTECTED]> writes:

> if keyword.iskeyword(tempwords):
> print tempwords

  for word in tempwords:
  if keyword.iskeyword(word):
  print word

Ganesan 


-- 
Ganesan Rajagopal

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: List objects are un-hashable

2007-04-27 Thread Ant
> I think it should be:
>
>  if keyword.iskeyword(k):
>   print k

Whoops - yes of course!

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: List objects are un-hashable

2007-04-27 Thread Andy

> What you want is something like:
>
>  for line in inp:
>  lines +=1
>  # a list of words
>  tempwords = line.split()
>  for k in tempwords:
>  if keyword.iskeyword(k):
> print tempwords


I think it should be:

 if keyword.iskeyword(k):
  print k

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: List objects are un-hashable

2007-04-27 Thread Ant
> for line in inp:
> lines +=1
> # a list of words
> tempwords = line.split(None)
> if keyword.iskeyword(tempwords):
> print tempwords

You are trying here to ask if a list of words (tempwords) is a
keyword. The error is due to the implementation of the iskeyword
function which converts the keyword list into a frozenset (in which
elements must be hashable) for, I presume, performance reasons:

>>> f_set = frozenset((1,2,3,4))
>>> ["test"] in f_set
Traceback (most recent call last):
  File "", line 1, in 
TypeError: list objects are unhashable

What you want is something like:

 for line in inp:
 lines +=1
 # a list of words
 tempwords = line.split()
 for k in tempwords:
 if keyword.iskeyword(k):
print tempwords

Which iterates over each word in your tempwords list in turn. Note
though the following:

>>> if(True):print"Hey!"
...
Hey!
>>> s = 'if(True):print"Hey!"'
>>> s.split()
['if(True):print"Hey!"']

Which may be a problem for you if you are trying to parse badly spaced
python source files!

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: List objects are un-hashable

2007-04-27 Thread Alexander Schmolck
Andy <[EMAIL PROTECTED]> writes:

> Hi, I'm trying to search and print any no# of Python keywords present
> in a text file (say - foo.txt), and getting the above error. Sad for
> not being able to decipher such a simple problem (I can come up with

Without looking at the docs, it seems save to assume keywords.iskeyword would
expect a string. You pass it a list.

> other ways - but want to fix this one FFS). Any help is appreciated.
> Thanks!!
>
> import keyword, re, sys, string
> inp = open("foo.txt", "r")
  words = sum(1 for line in inp for w in line.split() if keyword.iskeyword(w))

'as
-- 
http://mail.python.org/mailman/listinfo/python-list


List objects are un-hashable

2007-04-27 Thread Andy
Hi, I'm trying to search and print any no# of Python keywords present
in a text file (say - foo.txt), and getting the above error. Sad for
not being able to decipher such a simple problem (I can come up with
other ways - but want to fix this one FFS). Any help is appreciated.
Thanks!!

import keyword, re, sys, string
inp = open("foo.txt", "r")
words,lines = 0, 0

for line in inp:
lines +=1
# a list of words
tempwords = line.split(None)
if keyword.iskeyword(tempwords):
print tempwords

inp.close()

-- 
http://mail.python.org/mailman/listinfo/python-list