473,387 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 9657
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: svilen | last post by:
hello again. i'm now into using python instead of another language(s) for describing structures of data, including names, structure, type-checks, conversions, value-validations, metadata etc....
20
by: Lucas Raab | last post by:
I'm done porting the C code, but now when running the script I continually run into problems with lists. I tried appending and extending the lists, but with no avail. Any help is much appreciated...
3
by: yaffa | last post by:
hey folks i get this error: Python interpreter error: unsupported operand type(s) for |: when i run this line of code: for incident in bs('tr', {'bgcolor' : '#eeeeee'} | {'bgcolor' :...
5
by: Allerdyce.John | last post by:
Do I need to convert string to integer in python? or it will do it for me (since dynamic type)? In my python script, I have this line: x /= 10; when i run it, I get this error: TypeError:...
0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 378 open ( +3) / 3298 closed (+34) / 3676 total (+37) Bugs : 886 open (-24) / 5926 closed (+75) / 6812 total (+51) RFE : 224 open...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.