Python interpreter error: unsupported operand type(s) for -: 'tuple' and 'int'

R

Rakesh

In my Python code fragment, I want to write a code fragment such that
the minimum element of a tuple is subtracted from all the elements of a
given
tuple.

When I execute the following python script I get the following
interpreter
error.


C:\>python test.py
200 ddf DNEWS Version 5.7b1,, S0, posting OK
Reading messages from newsgroup comp.lang.c ...
Newsgroup responded 211 3377 67734 71134 comp.lang.c selected
Read 3377 messages from the newsgroup
Converting date to seconds since epoch ..
Traceback (most recent call last):
File "test.py", line 111, in ?
main()
File "test.py", line 101, in main
processNewsgroup(s, 'comp.lang.c');
File "test.py", line 80, in processNewsgroup
relativetime = thissecond - minsec
TypeError: unsupported operand type(s) for -: 'tuple' and 'int'

How would I fix this type error ?

<--->
from nntplib import NNTP

import sys
import time
import string

senders = ' ';
dates = ' ';
seconds = ' ';
rangetime = ' ';

## --------------------------------------------
## Parse a given date to be converted to
## milliseconds from epoch time.
## Input - datestr - String in date format
## Jan 13 2004, 13:23:34 +0800
## Output :- number of milliseconds since epoch
##
## --------------------------------------------
def parseDate(datestr):
timezone = '';
index = string.find(datestr, '+');
if (index != -1): # Date not found
timezone = datestr[index:];
if ( len(timezone) == 0):
negindex = string.find(datestr, '-');
if (negindex != -1):
timezone = datestr[negindex:];
if ( len(timezone) == 0):
timezone = ' %Z';

datefound = 0;
try:
mydate = time.strptime(datestr, '%a, %d %b %Y %H:%M:%S ' +
timezone)
except:
try:
mydate = time.strptime(datestr, '%d %b %Y %H:%M:%S ' +
timezone)
except:
print "Unexpected error:", sys.exc_info()[0]

# Get number of milliseconds since epoch
return int( time.mktime(mydate) );


## ------------------------------------------- ----
# Function to deal with a given newsgroups
# newsgroup - comp.lang.c etc.
# s - NNTP Host instance ( an instance from NNTP )
## ----------------------------------------------------
def processNewsgroup(s, newsgroup):
global senders;
global dates;
global seconds;
global rangetime;

print 'Reading messages from newsgroup %s ...' % (newsgroup)

# Newsgroup - comp.lang.c for now
resp, count, first, last, name = s.group(newsgroup);

print 'Newsgroup responded %s' % resp;

# XHDR command as per the NNTP RFC specification
resp, senders = s.xhdr('from', first + '-' + last)

# XHDR command as per the NNTP RFC specification
resp, dates = s.xhdr('date', first + '-' + last)

print 'Read %s messages from the newsgroup ' % (count)
print 'Converting date to seconds since epoch ..'

# Convert the date format to the seconds since epoch
for i in xrange( len(dates) ):
thissecond = parseDate(dates[1]);
seconds = (seconds, thissecond );

minsec = int( min(seconds) );
for thissecond in seconds:
# in xrange( len(seconds) ):
relativetime = thissecond - minsec
#TODO: INTERPRETER ERRORS ABOVE.

rangetime = (rangetime, relativetime );

rangetime
print 'Converted date to seconds since epoch ..'





## -----------------------------------------------
# Entry Point to the whole program
## -----------------------------------------------
def main():
# Read the message from the given news server.
s = NNTP('news.mynewserver');

# Print the welcome message
# More of a test than anything else
print s.getwelcome()

processNewsgroup(s, 'comp.lang.c');

# Quit from the NNTPLib after using the same
s.quit()



## --------------------------------
# Entry-point to the whole program
## --------------------------------
main()
<--->
 
R

Rakesh

To quote a much smaller trimmed-down example, here is how it looks
like:

<-->
import sys

## --------------------------------------------
## --------------------------------------------
def GenerateList():
array = ' '
for i in xrange(10):
array = (array, i)
return array


## -----------------------------------------------
# Entry Point to the whole program
## -----------------------------------------------
def main():
mylist = GenerateList()
minnumber = min(mylist)
for num in mylist:
print num - minnumber
## TODO: Interpreter errors above.

## --------------------------------
# Entry-point to the whole program
## --------------------------------
main()

<-- >
 
G

George Yoshida

Rakesh said:
> To quote a much smaller trimmed-down example, here is how it looks
> like:
> ## -----------------------------------------------
> # Entry Point to the whole program
> ## -----------------------------------------------
> def main():
> mylist = GenerateList()
> minnumber = min(mylist)
> for num in mylist:
> print num - minnumber
> ## TODO: Interpreter errors above.


Try printing mylist. Then you'll know why - operand doesn't work.
You're creating a nested tuple.

I'd recommend changing the original script to something like::

seconds = []
[snip]

# Convert the date format to the seconds since epoch
for i in xrange( len(dates) ):
thissecond = parseDate(dates[1])
seconds.append(thissecond)

or if you want to stick with tuple::

seconds = ()
[snip]

# Convert the date format to the seconds since epoch
for i in xrange( len(dates) ):
thissecond = parseDate(dates[1])
seconds += (thissecond, )

-- george
 
R

Rakesh

George said:
Rakesh said:
To quote a much smaller trimmed-down example, here is how it looks
like:
## -----------------------------------------------
# Entry Point to the whole program
## -----------------------------------------------
def main():
mylist = GenerateList()
minnumber = min(mylist)
for num in mylist:
print num - minnumber
## TODO: Interpreter errors above.


Try printing mylist. Then you'll know why - operand doesn't work.
You're creating a nested tuple.

I'd recommend changing the original script to something like::

seconds = []
[snip]

# Convert the date format to the seconds since epoch
for i in xrange( len(dates) ):
thissecond = parseDate(dates[1])
seconds.append(thissecond)

or if you want to stick with tuple::

seconds = ()
[snip]

# Convert the date format to the seconds since epoch
for i in xrange( len(dates) ):
thissecond = parseDate(dates[1])
seconds += (thissecond, )

-- george


Thanks. That fixed the problem.

## --------------------------------------------
## Generate a list
## --------------------------------------------
def GenerateList():
array = []
for i in xrange(10):
array.append(i)
return array


## -----------------------------------------------
# Entry Point to the whole program
## -----------------------------------------------
def main():
mylist = GenerateList()
minnumber = min(mylist)
for num in mylist:
print num - minnumber

## --------------------------------
# Entry-point to the whole program
## --------------------------------
main()

This is how my revised code looks like
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,961
Messages
2,570,131
Members
46,689
Latest member
liammiller

Latest Threads

Top