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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rakesh

    #1

    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
    processNewsgrou p(s, 'comp.lang.c');
    File "test.py", line 80, in processNewsgrou p
    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(dates tr):
    timezone = '';
    index = string.find(dat estr, '+');
    if (index != -1): # Date not found
    timezone = datestr[index:];
    if ( len(timezone) == 0):
    negindex = string.find(dat estr, '-');
    if (negindex != -1):
    timezone = datestr[negindex:];
    if ( len(timezone) == 0):
    timezone = ' %Z';

    datefound = 0;
    try:
    mydate = time.strptime(d atestr, '%a, %d %b %Y %H:%M:%S ' +
    timezone)
    except:
    try:
    mydate = time.strptime(d atestr, '%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(myd ate) );


    ## ------------------------------------------- ----
    # Function to deal with a given newsgroups
    # newsgroup - comp.lang.c etc.
    # s - NNTP Host instance ( an instance from NNTP )
    ## ----------------------------------------------------
    def processNewsgrou p(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(newsgro up);

    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.myne wserver');

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

    processNewsgrou p(s, 'comp.lang.c');

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



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

  • Rakesh

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

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

    <-- >

    Comment

    • George Yoshida

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

      Rakesh wrote:
      [color=blue]
      > 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.[/color]


      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

      Comment

      • Rakesh

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

        George Yoshida wrote:[color=blue]
        > Rakesh wrote:
        >[color=green]
        > > 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.[/color]
        >
        >
        > 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[/color]

        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

        Comment

        Working...