[issue23787] sum() function docstring lists arguments incorrectly

2017-06-06 Thread Mariatta Wijaya

Mariatta Wijaya added the comment:

Raymond's patch has been applied to 2.7 branch.
Thanks :)

--
resolution:  -> fixed
stage: patch review -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue23787] sum() function docstring lists arguments incorrectly

2017-06-06 Thread Mariatta Wijaya

Mariatta Wijaya added the comment:


New changeset 536209ef92f16ea8823209a3c4b8763c0ec5d4bc by Mariatta in branch 
'2.7':
bpo-23787: Change sum() docstring from sequence to iterable (GH-1859)
https://github.com/python/cpython/commit/536209ef92f16ea8823209a3c4b8763c0ec5d4bc


--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue23787] sum() function docstring lists arguments incorrectly

2017-05-29 Thread Mariatta Wijaya

Changes by Mariatta Wijaya :


--
stage:  -> patch review

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue23787] sum() function docstring lists arguments incorrectly

2017-05-29 Thread Mariatta Wijaya

Changes by Mariatta Wijaya :


--
pull_requests: +1942

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue23787] sum() function docstring lists arguments incorrectly

2017-04-16 Thread Raymond Hettinger

Raymond Hettinger added the comment:

I believe this is just a 2.7 issue.

--
versions:  -Python 3.5, Python 3.6, Python 3.7

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue23787] sum() function docstring lists arguments incorrectly

2017-04-16 Thread Mariatta Wijaya

Changes by Mariatta Wijaya :


--
assignee: docs@python -> Mariatta
nosy: +Mariatta
versions: +Python 3.5, Python 3.6, Python 3.7

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue23787] sum() function docstring lists arguments incorrectly

2017-04-16 Thread Raymond Hettinger

Raymond Hettinger added the comment:

[Serhiy]
> Raymond, could you open a pull request?

Perhaps you could do it for me.  I still haven't had time to wrestle with the 
github switchover, so I'm effectively crippled for a while.

[Valentine]
> Seems like mentioning string was really a bad idea  that's
> why I considered the docstring [subtly] wrong.

Not really wrong in a way that confuses typical users.  That docstring has been 
successfully communicating the basic API for over a decade.

Over time, the docs have slowly converted the old "sequence" references to 
"iterable".  The docs were never really wrong; instead, we just got more 
precise by what we meant by sequence versus iterable (i.e. before the ABCs were 
introduced, the term "sequence" was used in a somewhat generic way to mean "a 
succession of data values"). 

Also note, it is an interesting paradox that docstrings that are the most 
helpful to most people most of the time are brief and little loose with 
terminology.  In general, they reward those who are doing quick lookups for API 
reminders, but do not reward pedantic close readings.

We'll go ahead and change "sequence" to "iterable" for sum(), but I think that 
is only a minor win.  The change makes it more technically correct but less 
friendly to some users (i.e. people need to be taught what "iterable" means 
while they tend to get the notion of "sequence of values" without any training).

As far as the exclusion of string goes, there were plenty of debate about 
whether to allow them or to more broadly disallow many data types where summing 
works quadratically.  The final decision was made by the BDFL and it seems to 
have been the right decision for just about everyone.  You can take issue with 
his decision, but that would be pointless.

--
nosy: +rhettinger
status: pending -> open

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue23787] sum() function docstring lists arguments incorrectly

2017-04-14 Thread Serhiy Storchaka

Serhiy Storchaka added the comment:

Raymond, could you open a pull request?

--
keywords: +easy
nosy: +serhiy.storchaka
priority: normal -> low
status: open -> pending

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue23787] sum() function docstring lists arguments incorrectly

2015-03-29 Thread Valentine Sinitsyn

Valentine Sinitsyn added the comment:

Seems like mentioning string was really a bad idea. They were only used as 
(poor) example, forget them if they are confusing in any way.

In my understanding, any sequence in Python is iterable, bit not all iterables 
are sequences (correct me if I'm wrong). Then, the purpose of my suggestion is 
to explicitly say that sum() accepts iterables. In its current form, it seems 
like it doesn't, that's why I considered the docstring [subtly] wrong.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue23787
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue23787] sum() function docstring lists arguments incorrectly

2015-03-28 Thread Wolfgang Maier

Wolfgang Maier added the comment:

This implies sum() should accept str, unicode, list, tuple, bytearray, buffer, 
and xrange.

and in fact it *does* accept all these as input. It just refuses to add the 
elements of the sequence if these elements are of certain types. Of course, the 
elements of a string are strings themselves so this does not work:

 sum('abc', '')
Traceback (most recent call last):
  File pyshell#88, line 1, in module
sum('abc', '')
TypeError: sum() can't sum strings [use ''.join(seq) instead]


compare with a bytes sequence in Python3, where the elements are ints:

 sum(b'abc', 0)
294


but strings are also perfectly accepatble as input if you do not try to add 
their str elements, but something else:

 class X (int):
def __add__(self, other):
return X(ord(other) + self)

 sum('abc', X(0))
294

= the docs are right and there is no issue here.

--
nosy: +wolma

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue23787
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue23787] sum() function docstring lists arguments incorrectly

2015-03-27 Thread Valentine Sinitsyn

New submission from Valentine Sinitsyn:

sum() function doctstring describes expected arguments as follows (Python 
2.7.6):

sum(...)
sum(sequence[, start]) - value
...

This implies sum() should accept str, unicode, list, tuple, bytearray, buffer, 
and xrange. However, you clearly can't use this function to sum strings (which 
is also mentioned in the docstring):

 sum('abc')
Traceback (most recent call last):
  File stdin, line 1, in module
TypeError: unsupported operand type(s) for +: 'int' and 'str'

I'd suggest to describe first argument as iterable, which is actually what 
sum() expects there.

--
assignee: docs@python
components: Documentation
messages: 239388
nosy: docs@python, vsinitsyn
priority: normal
severity: normal
status: open
title: sum() function docstring lists arguments incorrectly
type: enhancement
versions: Python 2.7

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue23787
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue23787] sum() function docstring lists arguments incorrectly

2015-03-27 Thread R. David Murray

R. David Murray added the comment:

In python3 the docstring does say iterable.  It wouldn't be a bad thing to 
change it in 2.7, but it is not much of a priority.  iterable vs sequence makes 
no difference to the str question: a string is an iterable.  The docstring 
explicitly says strings are excepted, as you mentioned, so there's nothing to 
do about that.

I note that python3 also does not support iterables of byte-like objects.  I'm 
not sure if this would actually be helpful to add to the docstring, though, 
since sum(b'abc') works and a docstring is probably not an appropriate place to 
go into detail as to why.

--
nosy: +r.david.murray

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue23787
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue23787] sum() function docstring lists arguments incorrectly

2015-03-27 Thread Valentine Sinitsyn

Valentine Sinitsyn added the comment:

Yes, strings aren't an issue. I only used them as an example.

I came across this issue during code review, discussing if it is okay to pass 
generator expression to sum() (like sum(x*2 for x in xrange(5)) or is it better 
to convert it to the list first (sum([x*2 for x in xrange(5)])). Both variants 
work so docstring is sort of specification here.

Surely, it's not a high priority task anyways.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue23787
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue23787] sum() function docstring lists arguments incorrectly

2015-03-27 Thread Raymond Hettinger

Changes by Raymond Hettinger raymond.hettin...@gmail.com:


--
keywords: +patch
Added file: http://bugs.python.org/file38716/sum_doc.diff

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue23787
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



Re: Simple recursive sum function | what's the cause of the weird behaviour?

2013-07-07 Thread Russel Walker
I read through all of the posts and thanks for helping. What was supposed to be 
simple a (recursively) straightforward, turned out to be quite tricky.

I've set up a small testing bench and tried all of the proposed solutions 
including my own but none pass. I'll post it below.

I've also discovered something about lists that explains the very first weird 
result I was observing, which I realized was because lists are mutable etc, but 
more specifically:

This

 a = [1, 2]
 a += [3]

is equivalent to, AFAIK, this

 a = [1, 2]
 a.extend([3])

So to overcome that you just have to do

 a = [1, 2]
 a = a + [3]

Which creates a new list. So any variables which were pointing to the same list 
as a, are unaffected.

Summary
- - - -

 # --- Bad ---
 a = [1, 2]
 b = a
 a += [3]
 print a
[1, 2, 3]
 print b
[1, 2, 3]

 # --- Good ---
 a = [1, 2]
 b = a
 a = a + [3]
 print a
[1, 2, 3]
 print b
[1, 2]


And as for the testbench:

def supersum(seq, start=0):
return


#  Testing  

testcases = [

# (seq, start, result)

# arithmetic sums
([],0,  0),
([[], []],  0,  0),
([[], [[],[]]], 0,  0),
([1],   0,  1),
([[], [1]], 0,  1),
([[], [[],[1, 1]]], 0,  2),
([[1], [1]],0,  2),
([[1], [[1],[1, 1]]],   0,  4),
([[1], [[1],[1, 1]]],   1,  5),

# list flattening
([],[], []),
([[], []],  [], []),
([[], [[],[]]], [], []),
([],[1],[1]),
([[], []],  [1],[1]),
([[], [[],[]]], [1],[1]),
([1],   [1],[1, 1]),
([[1], [1]],[1],[1, 1]),
([[1], [[1],[1]]],  [1],[1, 1, 1, 1]),

]


for seq, start, result in testcases:
try:
assert supersum(seq, start) == result
except Exception as er:
print seq:%s\t start:%s % (seq, start)
if type(er) is AssertionError:
print expected:, result
print got: , supersum(seq, start)
else:
print repr(er)
print ''

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Simple recursive sum function | what's the cause of the weird behaviour?

2013-07-07 Thread Russel Walker
I got it! One of the testcases was wrong,

([[1], [1]],[1],[1, 1]),

should be

([[1], [1]],[1],[1, 1, 1]),


And the working solution.

def supersum(sequence, start=0):
result = start
start = type(start)()
for item in sequence:
try:
item = supersum(item, start)
except TypeError:
pass
try:
result = result + item
except TypeError:
return result + sequence
return result


I couldn't yet get around doing type(start)() and it's pretty messy, but 
anyways...
-- 
http://mail.python.org/mailman/listinfo/python-list


Simple recursive sum function | what's the cause of the weird behaviour?

2013-07-06 Thread Russel Walker
I know this is simple but I've been starring at it for half an hour and trying 
all sorts of things in the interpreter but I just can't see where it's wrong.

def supersum(sequence, start=0):
result = start
for item in sequence:
try:
result += supersum(item, start)
except:
result += item
return result

It's supposed to work like the builtin sum, but on multidimensional lists and 
also with the optional start parameter accepting something like an empty list 
and so would also works as a robust list flattener. It's just for kicks, I'm 
not actually going to use it for anything.


This works:
- - - - - -
 x = [[1], [2], [3]]
 supersum(x)
6
 supersum(x, [])
[1, 2, 3]
 


This does not:
- - - - - - - 
 x = [[[1], [2]], [3]]
 supersum(x, [])
[1, 2, 1, 2, 3]
 

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Simple recursive sum function | what's the cause of the weird behaviour?

2013-07-06 Thread Russel Walker
Nevermind!

Stupid of me to forget that lists or mutable so result and start both point to 
the same list.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Simple recursive sum function | what's the cause of the weird behaviour?

2013-07-06 Thread Russel Walker
Since I've already wasted a thread I might as well...

Does this serve as an acceptable solution?

def supersum(sequence, start=0):
result = type(start)()
for item in sequence:
try:
result += supersum(item, start)
except:
result += item
return result
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Simple recursive sum function | what's the cause of the weird behaviour?

2013-07-06 Thread Peter Otten
Russel Walker wrote:

 Since I've already wasted a thread I might as well...
 
 Does this serve as an acceptable solution?
 
 def supersum(sequence, start=0):
 result = type(start)()
 for item in sequence:
 try:
 result += supersum(item, start)
 except:
 result += item
 return result

That depends on what is an acceptable result ;)
For instance:

 supersum([2, 3], 1)
5
 supersum([[1], [abc]], [])
[1, 'a', 'b', 'c']


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Simple recursive sum function | what's the cause of the weird behaviour?

2013-07-06 Thread Chris Angelico
On Sat, Jul 6, 2013 at 10:37 PM, Russel Walker russ.po...@gmail.com wrote:
 This works:
 - - - - - -
 x = [[1], [2], [3]]
 supersum(x)
 6
 supersum(x, [])
 [1, 2, 3]



 This does not:
 - - - - - - -
 x = [[[1], [2]], [3]]
 supersum(x, [])
 [1, 2, 1, 2, 3]


You have a problem of specification here. What should supersum do with
the list [1]? Should it recurse into it, or append it as a list? It
can't do both. For a list flattener, you would need to either use
.append for each element you come across, or .extend with each list,
with some kind of check to find whether you should recurse or not.

Still, it's a fun thing to play with. I like code golfing these sorts
of trinketty functions, just for fun :)

ChrisA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Simple recursive sum function | what's the cause of the weird behaviour?

2013-07-06 Thread Joshua Landau
On 6 July 2013 13:59, Russel Walker russ.po...@gmail.com wrote:
 Since I've already wasted a thread I might as well...

 Does this serve as an acceptable solution?

 def supersum(sequence, start=0):
 result = type(start)()
 for item in sequence:
 try:
 result += supersum(item, start)
 except:
 result += item
 return result

It's probably more robust to do:

def supersum(sequence, start=0):
for item in sequence:
try:
result = result + supersum(item, start)
   except:
result = result + item
return result

as that way you aren't assuming the signature of type(start).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Simple recursive sum function | what's the cause of the weird behaviour?

2013-07-06 Thread Terry Reedy

On 7/6/2013 8:37 AM, Russel Walker wrote:

I know this is simple but I've been starring at it for half an hour and trying 
all sorts of things in the interpreter but I just can't see where it's wrong.

def supersum(sequence, start=0):
 result = start
 for item in sequence:
 try:
 result += supersum(item, start)
 except:


Bare except statements cover up too many sins. I and others *strongly* 
recommend that you only catch what you *know* you actually want to (see 
below).



 result += item
 return result


I recommend that you start with at least one test case, and with an edge 
case at that. If you cannot bring yourself to do it before writing a 
draft of the function code, do it immediately after and run. If you do 
not want to use a framework, use assert.


assert supersum([]) == 0
assert supersum([], []) == []

Do the asserts match your intention? The tests amount to a specification 
by example. Any 'kind' of input that is not tested is not guaranteed to 
work.


Back to the except clause: only add try..except xxx when needed to pass 
a test.


--
Terry Jan Reedy

--
http://mail.python.org/mailman/listinfo/python-list


Re: Simple recursive sum function | what's the cause of the weird behaviour?

2013-07-06 Thread Rotwang

On 06/07/2013 19:43, Joshua Landau wrote:

On 6 July 2013 13:59, Russel Walker russ.po...@gmail.com wrote:

Since I've already wasted a thread I might as well...

Does this serve as an acceptable solution?

def supersum(sequence, start=0):
 result = type(start)()
 for item in sequence:
 try:
 result += supersum(item, start)
 except:
 result += item
 return result


It's probably more robust to do:

def supersum(sequence, start=0):
 for item in sequence:
 try:
 result = result + supersum(item, start)
except:
 result = result + item
 return result


I assume you meant to put result = start in there at the beginning.



as that way you aren't assuming the signature of type(start).


It's not quite clear to me what the OP's intentions are in the general 
case, but calling supersum(item, start) seems odd - for example, is the 
following desirable?


 supersum([[1], [2], [3]], 4)
22

I would have thought that the correct answer would be 10. How about 
the following?


def supersum(sequence, start = 0):
result = start
for item in reversed(sequence):
try:
result = supersum(item, result)
except:
result = item + result
return result
--
http://mail.python.org/mailman/listinfo/python-list


Re: Simple recursive sum function | what's the cause of the weird behaviour?

2013-07-06 Thread Rotwang

On 06/07/2013 21:10, Rotwang wrote:

[...]

It's not quite clear to me what the OP's intentions are in the general
case, but calling supersum(item, start) seems odd - for example, is the
following desirable?

  supersum([[1], [2], [3]], 4)
22

I would have thought that the correct answer would be 10. How about
the following?

def supersum(sequence, start = 0):
 result = start
 for item in reversed(sequence):
 try:
 result = supersum(item, result)
 except:
 result = item + result
 return result


Sorry, I've no idea what I was thinking with that reversed thing. The 
following seems better:


def supersum(sequence, start = 0):
result = start
for item in sequence:
try:
result = supersum(item, result)
except:
result = result + item
return result
--
http://mail.python.org/mailman/listinfo/python-list


Re: sum function

2012-10-06 Thread Ramchandra Apte
On Saturday, 6 October 2012 02:09:56 UTC+5:30, Dave Angel  wrote:
 On 10/05/2012 04:09 PM, Mike wrote:
 
  Terry,
 
 
 
  I am not using the mail client. I am just posting on the site.
 
 
 
 And which site would that be (that you're using)?  There are a few.  I'm
 
 guessing you use google-groups.  And all of them get gatewayed to the
 
 actual list, with differing numbers of bugs. 
 
 
 
 I use email to access it directly.  I solve one of the duplicate-message
 
 problems with google groups by automatically deleting any message
 
 addressed to google-groups.  There are about 100 such messages each month.
 
 
 
 Another problem is that lots of these gateways post to both the
 
 newsgroup and to the python-list.

I found out earlier that this was why my posts was being double-posted in 
Google Groups.
 
 
 
 
  Something wrong with this site. When you do individual reply, it does the 
  double posting which it shouldn't. See Ramachandra Apte's reply. It is 
  posted twice too.
 
 
 
  Thanks
 
 
 
 
 
 
 
 
 
 
 
 -- 
 
 
 
 DaveA

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sum function

2012-10-05 Thread Ramchandra Apte
On Friday, 5 October 2012 07:31:24 UTC+5:30, Mike  wrote:
 I agree with you, Ian. Thanks for all the help.  Now I get the below error.
 
 
 
   File test.py, line 17, in module
 
 total = sum(float(col.value) for r in iter(next_r, None) for col in 
 r[0].columns.itervalues())
 
   File test.py, line 17, in genexpr
 
 total = sum(float(col.value) for r in iter(next_r, None) for col in 
 r[0].columns.itervalues())
 
 
 
 Thanks

You have missed the last line of the traceback (error)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sum function

2012-10-05 Thread Mike
On Thursday, October 4, 2012 4:52:50 PM UTC-4, Mike wrote:
 Hi All,
 
 
 
 I am new to python and am getting the data from hbase. 
 
 I am trying to do sum on the column as below
 
 
 
 
 
 scanner = client.scannerOpenWithStop(tab, 10, 1000, [cf:col1])
 
 total = 0.0
 
 r = client.scannerGet(scanner)
 
 while r:
 
   for k in (r[0].columns):
 
 total += float(r[0].columns[k].value)
 
   r = client.scannerGet(scanner)
 
 
 
 print total
 
 
 
 Do you know of better (faster) way to do sum?
 
 
 
 Any thoughts please?
 
 
 
 Thanks

Sorry about that. Here you go

Traceback (most recent call last):
  File test.py, line 17, in module
total = sum(float(col.value) for r in iter(next_r, None) for col in 
r[0].columns.itervalues())
  File test.py, line 17, in genexpr
total = sum(float(col.value) for r in iter(next_r, None) for col in 
r[0].columns.itervalues())
IndexError: list index out of range
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sum function

2012-10-05 Thread Ramchandra Apte
On Friday, 5 October 2012 19:09:15 UTC+5:30, Mike  wrote:
 On Thursday, October 4, 2012 4:52:50 PM UTC-4, Mike wrote:
 
  Hi All,
 
  
 
  
 
  
 
  I am new to python and am getting the data from hbase. 
 
  
 
  I am trying to do sum on the column as below
 
  
 
  
 
  
 
  
 
  
 
  scanner = client.scannerOpenWithStop(tab, 10, 1000, [cf:col1])
 
  
 
  total = 0.0
 
  
 
  r = client.scannerGet(scanner)
 
  
 
  while r:
 
  
 
for k in (r[0].columns):
 
  
 
  total += float(r[0].columns[k].value)
 
  
 
r = client.scannerGet(scanner)
 
  
 
  
 
  
 
  print total
 
  
 
  
 
  
 
  Do you know of better (faster) way to do sum?
 
  
 
  
 
  
 
  Any thoughts please?
 
  
 
  
 
  
 
  Thanks
 
 
 
 Sorry about that. Here you go
 
 
 
 Traceback (most recent call last):
 
   File test.py, line 17, in module
 
 total = sum(float(col.value) for r in iter(next_r, None) for col in 
 r[0].columns.itervalues())
 
   File test.py, line 17, in genexpr
 
 total = sum(float(col.value) for r in iter(next_r, None) for col in 
 r[0].columns.itervalues())
 
 IndexError: list index out of range

the variable r is an empty list
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sum function

2012-10-05 Thread Mike
On Friday, October 5, 2012 9:41:44 AM UTC-4, Ramchandra Apte wrote:
 On Friday, 5 October 2012 19:09:15 UTC+5:30, Mike  wrote:
 
  On Thursday, October 4, 2012 4:52:50 PM UTC-4, Mike wrote:
 
  
 
   Hi All,
 
  
 
   
 
  
 
   
 
  
 
   
 
  
 
   I am new to python and am getting the data from hbase. 
 
  
 
   
 
  
 
   I am trying to do sum on the column as below
 
  
 
   
 
  
 
   
 
  
 
   
 
  
 
   
 
  
 
   
 
  
 
   scanner = client.scannerOpenWithStop(tab, 10, 1000, [cf:col1])
 
  
 
   
 
  
 
   total = 0.0
 
  
 
   
 
  
 
   r = client.scannerGet(scanner)
 
  
 
   
 
  
 
   while r:
 
  
 
   
 
  
 
 for k in (r[0].columns):
 
  
 
   
 
  
 
   total += float(r[0].columns[k].value)
 
  
 
   
 
  
 
 r = client.scannerGet(scanner)
 
  
 
   
 
  
 
   
 
  
 
   
 
  
 
   print total
 
  
 
   
 
  
 
   
 
  
 
   
 
  
 
   Do you know of better (faster) way to do sum?
 
  
 
   
 
  
 
   
 
  
 
   
 
  
 
   Any thoughts please?
 
  
 
   
 
  
 
   
 
  
 
   
 
  
 
   Thanks
 
  
 
  
 
  
 
  Sorry about that. Here you go
 
  
 
  
 
  
 
  Traceback (most recent call last):
 
  
 
File test.py, line 17, in module
 
  
 
  total = sum(float(col.value) for r in iter(next_r, None) for col in 
  r[0].columns.itervalues())
 
  
 
File test.py, line 17, in genexpr
 
  
 
  total = sum(float(col.value) for r in iter(next_r, None) for col in 
  r[0].columns.itervalues())
 
  
 
  IndexError: list index out of range
 
 
 
 the variable r is an empty list

Here is the actual code.

scanner = client.scannerOpenWithStop(tab, 10, 1000, [cf:col1]) 
next_r = functools.partial(client.scannerGet, scanner)
total = sum(float(col.value) for r in iter(next_r, None) for col in 
r[0].columns.itervalues())


Scanner does have rows.

Are we missing something please?

Thanks
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sum function

2012-10-05 Thread Terry Reedy

On 10/5/2012 9:47 AM, Mike wrote:

On Friday, October 5, 2012 9:41:44 AM UTC-4, Ramchandra Apte wrote:

On Friday, 5 October 2012 19:09:15 UTC+5:30, Mike  wrote:


On Thursday, October 4, 2012 4:52:50 PM UTC-4, Mike wrote:







Hi All,































I am new to python and am getting the data from hbase.


If you want as many people as possible to read your posts, stop using a 
mail-agent and site that spits in the face of readers by doubling blank 
lines each iteration. Alternatives have been discussed previously.


--
Terry Jan Reedy

--
http://mail.python.org/mailman/listinfo/python-list


Re: sum function

2012-10-05 Thread Ian Kelly
On Fri, Oct 5, 2012 at 7:39 AM, Mike mike20...@gmail.com wrote:
 Sorry about that. Here you go

 Traceback (most recent call last):
   File test.py, line 17, in module
 total = sum(float(col.value) for r in iter(next_r, None) for col in 
 r[0].columns.itervalues())
   File test.py, line 17, in genexpr
 total = sum(float(col.value) for r in iter(next_r, None) for col in 
 r[0].columns.itervalues())
 IndexError: list index out of range

Maybe the sentinel value is not None as I assumed, and it's
overrunning the end of the data?  What does
client.scannerGet return when there is no more data?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sum function

2012-10-05 Thread Mike
I added the print command.

It prints [] when there is no data.

Thanks
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sum function

2012-10-05 Thread Mike
Terry,

I am not using the mail client. I am just posting on the site.

Something wrong with this site. When you do individual reply, it does the 
double posting which it shouldn't. See Ramachandra Apte's reply. It is posted 
twice too.

Thanks



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sum function

2012-10-05 Thread Ian Kelly
On Fri, Oct 5, 2012 at 2:03 PM, Mike mike20...@gmail.com wrote:
 I added the print command.

 It prints [] when there is no data.

Change iter(next_r, None) to iter(next_r, [])
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sum function

2012-10-05 Thread Dave Angel
On 10/05/2012 04:09 PM, Mike wrote:
 Terry,

 I am not using the mail client. I am just posting on the site.

And which site would that be (that you're using)?  There are a few.  I'm
guessing you use google-groups.  And all of them get gatewayed to the
actual list, with differing numbers of bugs. 

I use email to access it directly.  I solve one of the duplicate-message
problems with google groups by automatically deleting any message
addressed to google-groups.  There are about 100 such messages each month.

Another problem is that lots of these gateways post to both the
newsgroup and to the python-list.


 Something wrong with this site. When you do individual reply, it does the 
 double posting which it shouldn't. See Ramachandra Apte's reply. It is 
 posted twice too.

 Thanks





-- 

DaveA

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sum function

2012-10-05 Thread Mike
That worked, Ian.

Thanks
-- 
http://mail.python.org/mailman/listinfo/python-list


sum function

2012-10-04 Thread mike20007
Hi All,

I am new to python and am getting the data from hbase. 
I am trying to do sum on the column as below


scanner = client.scannerOpenWithStop(tab, 10, 1000, [cf:col1])
total = 0.0
r = client.scannerGet(scanner)
while r:
  for k in (r[0].columns):
total += float(r[0].columns[k].value)
  r = client.scannerGet(scanner)

print total

Do you know of better (faster) way to do sum?

Any thoughts please?

Thanks
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sum function

2012-10-04 Thread Ian Kelly
On Thu, Oct 4, 2012 at 2:52 PM,  mike20...@gmail.com wrote:
 scanner = client.scannerOpenWithStop(tab, 10, 1000, [cf:col1])
 total = 0.0
 r = client.scannerGet(scanner)
 while r:
   for k in (r[0].columns):
 total += float(r[0].columns[k].value)
   r = client.scannerGet(scanner)

 print total

 Do you know of better (faster) way to do sum?

scanner = client.scannerOpenWithStop(tab, 10, 1000, [cf:col1])
next_r = itertools.partial(client.scannerGet, scanner)
total = sum(float(col.value) for r in iter(next_r, None) for col in
r.itervalues())
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sum function

2012-10-04 Thread Ian Kelly
On Thu, Oct 4, 2012 at 3:04 PM, Ian Kelly ian.g.ke...@gmail.com wrote:
 scanner = client.scannerOpenWithStop(tab, 10, 1000, [cf:col1])
 next_r = itertools.partial(client.scannerGet, scanner)
 total = sum(float(col.value) for r in iter(next_r, None) for col in
 r.itervalues())

That should be functools above, not itertools. :-P
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sum function

2012-10-04 Thread Mike
I get below error

NameError: name 'functools' is not defined

Thanks

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sum function

2012-10-04 Thread Mike
Thanks Ian for the quick reply.

I get the below error.

NameError: name 'itertools' is not defined

Thanks
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sum function

2012-10-04 Thread Dave Angel
On 10/04/2012 05:29 PM, Mike wrote:
 I get below error

 NameError: name 'functools' is not defined


functools is a module in the standard library. You need to import it.

import functools



-- 

DaveA

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sum function

2012-10-04 Thread Chris Angelico
On Fri, Oct 5, 2012 at 7:29 AM, Mike mike20...@gmail.com wrote:
 I get below error

 NameError: name 'functools' is not defined

 Thanks

functools is a module:

import functools

ChrisA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sum function

2012-10-04 Thread Mike
On Thursday, October 4, 2012 5:40:26 PM UTC-4, Dave Angel wrote:
 On 10/04/2012 05:29 PM, Mike wrote:
 
  I get below error
 
 
 
  NameError: name 'functools' is not defined
 
 
 
 
 
 functools is a module in the standard library. You need to import it.
 
 
 
 import functools
 
 
 
 
 
 
 
 -- 
 
 
 
 DaveA

I imported functools. Now I get the below error please.


Traceback (most recent call last):
  File test.py, line 16, in module
total = sum(float(col.value) for r in iter(next_r, None) for col in 
r.itervalues()) 
  File test.py, line 16, in genexpr
total = sum(float(col.value) for r in iter(next_r, None) for col in 
r.itervalues()) 
AttributeError: 'list' object has no attribute 'itervalues'

Thanks
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sum function

2012-10-04 Thread Ian Kelly
On Thu, Oct 4, 2012 at 6:40 PM, Mike mike20...@gmail.com wrote:
 Traceback (most recent call last):
   File test.py, line 16, in module
 total = sum(float(col.value) for r in iter(next_r, None) for col in 
 r.itervalues())
   File test.py, line 16, in genexpr
 total = sum(float(col.value) for r in iter(next_r, None) for col in 
 r.itervalues())
 AttributeError: 'list' object has no attribute 'itervalues'

r.itervalues() should have been r[0].columns.itervalues(), I
think.  It's hard to test code against an API that you don't have. :-)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sum function

2012-10-04 Thread Mike

I agree with you, Ian. Thanks for all the help.  Now I get the below error.

  File test.py, line 17, in module
total = sum(float(col.value) for r in iter(next_r, None) for col in 
r[0].columns.itervalues())
  File test.py, line 17, in genexpr
total = sum(float(col.value) for r in iter(next_r, None) for col in 
r[0].columns.itervalues())

Thanks
-- 
http://mail.python.org/mailman/listinfo/python-list