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

Rakesh rakesh_usenet at yahoo.com
Tue Mar 29 20:40:59 EST 2005


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()
<--->




More information about the Python-list mailing list