I don't understand generator.send()

2011-05-14 Thread Victor Eijkhout
#! /usr/bin/env python

def ints():
i=0
while True:
yield i
i += 1

gen = ints()
while True:
i = gen.next()
print i
if i==5:
r = gen.send(2)
print "return:",r
if i>10:
break

I thought the send call would push the value "2" at the front of the
queue. Instead it coughs up the 2, which seems senseless to me.

1/ How should I view the send call? I'm reading the manual and dont' get
it
2/ Is there a way to push something in the generator object? So that it
becomes the next yield expression? In my code I was hoping to get
0,1,2,3,4,5,2,6,7 as yield expressions.

Victor.


-- 
Victor Eijkhout -- eijkhout at tacc utexas edu
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: I don't understand generator.send()

2011-05-14 Thread Victor Eijkhout
Chris Angelico  wrote:

> For what you're doing, there's a little complexity. If I understand,
> you want send() to be like an ungetc call... you could do that like
> this:
> 
> 
> def ints():
>i=0
>while True:
>sent=(yield i)
>if sent is not None:
>   yield None  # This becomes the return value from gen.send()
>   yield sent  # This is the next value yielded
>i += 1

I think this will serve my purposes.

Thanks everyone for broadening my understanding of generators.

Victor.
-- 
Victor Eijkhout -- eijkhout at tacc utexas edu
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: English Idiom in Unix: Directory Recursively

2011-05-18 Thread Victor Eijkhout
Harrison Hill  wrote:

> No need - I have the Dictionary definition of recursion here:
> 
> Recursion: (N). See recursion.

If you tell a joke, you have to tell it right.

Recursion: (N). See recursion. See also tail recursion.

Victor.
-- 
Victor Eijkhout -- eijkhout at tacc utexas edu
-- 
http://mail.python.org/mailman/listinfo/python-list


scipy sparse matrix question

2010-03-08 Thread Victor Eijkhout
I can't find any detailed information about scipy.sparse.

My specific question: what does "for x in A" give me when A is a sparse
matrix? It seems to yield all nonzero locations, but in what kind of
form? Very specifically: how do I get the (i,j) coordinates and the
value from x?

Victor.

-- 
Victor Eijkhout -- eijkhout at tacc utexas edu
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: scipy sparse matrix question

2010-03-09 Thread Victor Eijkhout
Terry Reedy  wrote:

> > My specific question: what does "for x in A" give me when A is a sparse
> > matrix?
> 
> Try it and see what you get.

Ah, how do I see what I get? If I print it it looks plausible, but I
don't know how to pull it apart. It doesn't seem to be an array.

Victor.

-- 
Victor Eijkhout -- eijkhout at tacc utexas edu
-- 
http://mail.python.org/mailman/listinfo/python-list


sort array, apply rearrangement to second

2010-03-30 Thread Victor Eijkhout
I have two arrays, made with numpy. The first one has values that I want
to use as sorting keys; the second one needs to be sorted by those keys.
Obviously I could turn them into a dictionary  of pairs and sort by the
first member, but I think that's not very efficient, at least in space,
and this needs to be done as efficiently as possible.

I could use a hand.

Victor.
-- 
Victor Eijkhout -- eijkhout at tacc utexas edu
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sort array, apply rearrangement to second

2010-03-31 Thread Victor Eijkhout
Robert Kern  wrote:

> second[first.argsort()]

Really cool. Thanks.

> Ask numpy questions on the numpy mailing list.

I will. I thought that this question would have an answer in a generic
python idiom.

Victor.
-- 
Victor Eijkhout -- eijkhout at tacc utexas edu
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: MATLAB to Python?

2010-11-27 Thread Victor Eijkhout
MATLABdude  wrote:

> Also the
> values themselves are not identical compared to the values of the
> MATLAB program.

In numerical analysis there is no such thing as identical. If they
differ by 1.e-10 I'd call it close enough. The difference comes from
differing numerical methods.

Victor.
-- 
Victor Eijkhout -- eijkhout at tacc utexas edu
-- 
http://mail.python.org/mailman/listinfo/python-list


Generator question

2010-12-22 Thread Victor Eijkhout
So I have a generator, either as a free function or in a class and I
want to generate objects that are initialized from the generated things.

def generator():
for whatever:
yield something
class Object():
def __init__(self):
self.data = # the next thing from generator

I have not been able to implement this elegantly. For external reasons
the following syntax is unacceptable:

for g in generator():
ob = Object(g)

I really want to be able to write "Object()" in any location and get a
properly initialized object.

Hints appreciated.

Victor.
-- 
Victor Eijkhout -- eijkhout at tacc utexas edu
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Generator question

2010-12-22 Thread Victor Eijkhout
Dan Stromberg  wrote:

> You likely want a class variable:

Sounds like an elegant solution. Thanks!

Victor.
-- 
Victor Eijkhout -- eijkhout at tacc utexas edu
-- 
http://mail.python.org/mailman/listinfo/python-list


long int computations

2010-05-03 Thread Victor Eijkhout
I have two long ints, both too long to convert to float, but their ratio
is something reasonable. How can I compute that? The obvious "(1.*x)/y"
does not work.

Victor.

-- 
Victor Eijkhout -- eijkhout at tacc utexas edu
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: long int computations

2010-05-03 Thread Victor Eijkhout
Jerry Hill  wrote:

> >>> from __future__ import division
> >>> long1/long2
> 0.5

Beautiful. Thanks so much guys.

Victor.
-- 
Victor Eijkhout -- eijkhout at tacc utexas edu
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: long int computations

2010-05-04 Thread Victor Eijkhout
Mensanator  wrote:

> You could try using the gmpy module. It supports arbitrary precision
> floats, so converting long to float is no problem.

I fear I may actually have to go symbolic. I'm now having to use the
12th root of 2, and I would like the twelfth power of that to be exactly
2.

Victor.
-- 
Victor Eijkhout -- eijkhout at tacc utexas edu
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python -- floating point arithmetic

2010-07-08 Thread Victor Eijkhout
Zooko O'Whielacronx  wrote:

> I'm starting to think that one should use Decimals by default and
> reserve floats for special cases.

Only if one has  Power6 (or 7) which has hardware support for BCD.
Otherwise you will have slow applications.

Victor.
-- 
Victor Eijkhout -- eijkhout at tacc utexas edu
-- 
http://mail.python.org/mailman/listinfo/python-list