473,387 Members | 1,542 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.

Augmented Assignment question

I have a function that returns a tuple:

def checkdoc(self, document):
blen = document['length']
bates = document['obates']

normal, truncated, semicolon = 0,0,0
for bat in bates:
if len(bat) == 2 * blen:
semicolon += 1
if len(bat) == blen - 1:
truncated += 1
if len(bat) == blen:
normal += 1

return normal, truncated, semicolon

on the other side I have 3 variables:
normal, truncated and semicolon

I would like to be able to do an augmented assignment such as:

normal, truncated, semicol += self.checkdoc(mydoc)

however this returns the following error:
SyntaxError: augmented assign to tuple not possible

I did some reading and it seems that an augmented assignment is
specifically verboten on tuples and lists. Is there a clean way to
accomplish this? I dislike in the extreme what I've resorted to:

fnormal, ftruncated, fsemicolon = 0,0,0

// loop through a file and process all documents:
normal, truncated, semicolon = self.checkdoc(curdoc)
fnormal += normal
ftruncated += truncated
fsemicolon += semicolon

This solution strikes me as inelegant and ugly. Is there a cleaner
way of accomplishing this?

Thanks in advance,
Doug Tolton
dougt at<remove this>case<remove this too>data dot com
Jul 18 '05 #1
6 2027
In <21********************************@4ax.com> Doug Tolton wrote:
I have a function that returns a tuple:

def checkdoc(self, document):
blen = document['length']
bates = document['obates']

normal, truncated, semicolon = 0,0,0
for bat in bates:
if len(bat) == 2 * blen:
semicolon += 1
if len(bat) == blen - 1:
truncated += 1
if len(bat) == blen:
normal += 1

return normal, truncated, semicolon

on the other side I have 3 variables:
normal, truncated and semicolon

I would like to be able to do an augmented assignment such as:

normal, truncated, semicol += self.checkdoc(mydoc)

however this returns the following error:
SyntaxError: augmented assign to tuple not possible

I did some reading and it seems that an augmented assignment is
specifically verboten on tuples and lists. Is there a clean way to
accomplish this? I dislike in the extreme what I've resorted to:

fnormal, ftruncated, fsemicolon = 0,0,0

// loop through a file and process all documents:
normal, truncated, semicolon = self.checkdoc(curdoc)
fnormal += normal
ftruncated += truncated
fsemicolon += semicolon

This solution strikes me as inelegant and ugly. Is there a cleaner
way of accomplishing this?

Thanks in advance,
Doug Tolton
dougt at<remove this>case<remove this too>data dot com

I don't think it's all that inelegant, and it's definitely clear. I can
see, though, why a one liner would seem a little cleaner.

Off the top of my head, I'd say to change your checkdoc function to this:
def checkdoc(self, document, normal=0, truncated=0, semicolon=0):
blen = document['length']
bates = document['obates']
for bat in bates:
if len(bat) == 2 * blen:
semicolon += 1
if len(bat) == blen - 1:
truncated += 1
if len(bat) == blen:
normal += 1

return normal, truncated, semicolon
And then call it like this:
normal, truncated, semicolon = self.checkdoc(curdoc, normal,
truncated, semicolon)
As a side note, I wouldn't have thought that the augmented assign would
work the way you tried to use it. I would have thought that it would be
analagous to the + operator and sequences:
x = [1,2,3]
y = [1,2,3]
x + y [1,2,3,1,2,3]

So that the agumented form would be:
x = [1,2,3]
x += [1,2,3]

[1,2,3,1,2,3]

But I've never tried it before and didn't know that it didn't work with
sequences. You learn something new every day.

Adam Ruth
Jul 18 '05 #2
In article <21********************************@4ax.com>,
Doug Tolton <dt*****@yahoo.com> wrote:

I did some reading and it seems that an augmented assignment is
specifically verboten on tuples and lists. Is there a clean way to
accomplish this?


Really?
l = []
l+=[1]
l [1] l+=['foo']
l

[1, 'foo']
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

A: No.
Q: Is top-posting okay?
Jul 18 '05 #3
Doug Tolton <dt*****@yahoo.com> wrote in message news:<21********************************@4ax.com>. ..
This solution strikes me as inelegant and ugly. Is there a cleaner
way of accomplishing this?


The problem is this: performing addition of tuples is concatenation,
not addition of the elements. If you try to decompose the augmented
addition into an addition and an assignment, you get a tuple that is
(in your case) the concatenation of the two 3-tuples, or a 6-tuple,
which can't unpack into a 3-tuple.

That said, for some definition of "cleaner", you could do:
def accumulate(i,j):
return map(int.__add__, i, j)

// loop
i,j,k = accumulate((i,j,k), fn())
Jul 18 '05 #4
Quoth Doug Tolton:
[...]
fnormal, ftruncated, fsemicolon = 0,0,0

// loop through a file and process all documents:
normal, truncated, semicolon = self.checkdoc(curdoc)
fnormal += normal
ftruncated += truncated
fsemicolon += semicolon

This solution strikes me as inelegant and ugly. Is there a cleaner
way of accomplishing this?


It seems possible that your normal, truncated and semicolon
variables should be bundled into a single object:

class DocumentStatistics(object):
# ...
def __iadd__(self, (deltanormal, deltatruncated, deltasemicolon)):
self.normal += deltanormal
self.truncated += deltatruncated
self.semicolon += deltasemicolon

Then you can just do
docstats = DocumentStatistics()
# ...
docstats += self.checkdoc(curdoc)
If these variables usually change in synch, this seems a natural
way to organize them.

You might also want to look into Numeric's arrays.

(And there's always

normal, truncated, semicolon = map(operator.add,
(normal, truncated, semicolon),
self.checkdoc(curdoc))

which I consider inferior to your straightforward approach with
three augmented assignments.)

--
Steven Taschuk st******@telusplanet.net
"Please don't damage the horticulturalist."
-- _Little Shop of Horrors_ (1960)

Jul 18 '05 #5
On Wed, 16 Jul 2003 22:17:40 GMT, Doug Tolton <dt*****@yahoo.com> wrote:
On 16 Jul 2003 16:51:38 -0400, aa**@pythoncraft.com (Aahz) wrote:
In article <21********************************@4ax.com>,
Doug Tolton <dt*****@yahoo.com> wrote:

I did some reading and it seems that an augmented assignment is
specifically verboten on tuples and lists. Is there a clean way to
accomplish this?


Really?
> l = []
> l+=[1]
> l

[1]
> l+=['foo']
> l

[1, 'foo']

I mis-spoke, lists are not included. You cannot do augmented
assignments on tuples or multiple targets.

you can do what you typed, but you can't do.
a,b = 0,0
a,b += 1,1SyntaxError: augmented assign to tuple not possible

or this:
a,b = [],[]
a,b += [1],[1]SyntaxError: augmented assign to tuple not possible

That is specifically the point I was getting at. Forgive the
technical slip of including lists in the discussion.

A little experiment (not tested beyond what you see ;-):
class TX(tuple): ... def __add__(self, other):
... return TX([s+o for s,o in zip(self, other)])
... tx = TX((2,2))
tx (2, 2) tx + (3,5) (5, 7) tx (2, 2) tx += (3,5)
tx (5, 7) tx += 10,20
tx (15, 27) tt = TX(('ab','cd','ef'))
tt ('ab', 'cd', 'ef') tt += '123'
tt

('ab1', 'cd2', 'ef3')
Regards,
Bengt Richter
Jul 18 '05 #6
Doug Tolton <dt*****@yahoo.com> wrote in message news:<21********************************@4ax.com>. ..
I have a function that returns a tuple:

def checkdoc(self, document):
blen = document['length']
bates = document['obates']

normal, truncated, semicolon = 0,0,0
for bat in bates:
if len(bat) == 2 * blen:
semicolon += 1
if len(bat) == blen - 1:
truncated += 1
if len(bat) == blen:
normal += 1
self.total_normal += normal
# etc etc
# someone else suggested a separate class just for statistics
# --- this is overkill IMHO
return normal, truncated, semicolon

Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Pierre Barbier de Reuille | last post by:
Hello, a discussion began on python-dev about this. It began by a bug report, but is shifted and it now belongs to this discussion group. The problem I find with augmented assignment is it's...
8
by: Suresh Jeevanandam | last post by:
Hi, Is there any gain in performance because of augmented assignments. x += 1 vs x = x+1 Or are both of them the same. regards, Suresh
37
by: Tim N. van der Leeuw | last post by:
Hi, The following might be documented somewhere, but it hit me unexpectedly and I couldn't exactly find this in the manual either. Problem is, that I cannot use augmented assignment operators...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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.