469,626 Members | 1,512 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,626 developers. It's quick & easy.

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

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[i][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()
<--->

Jul 18 '05 #1
3 8985
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()

<-- >

Jul 18 '05 #2
Rakesh wrote:
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[i][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[i][1])
seconds += (thissecond, )

-- george
Jul 18 '05 #3
George Yoshida wrote:
Rakesh wrote:
> 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[i][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[i][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

Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

20 posts views Thread by Lucas Raab | last post: by
5 posts views Thread by Allerdyce.John | last post: by
reply views Thread by Kurt B. Kaiser | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.