Re: Is `wsample` a proper name for a function like this?

2012-10-10 Thread suzaku
On Thursday, October 11, 2012 2:29:37 PM UTC+8, Steven D'Aprano wrote:
> On Wed, 10 Oct 2012 20:33:16 -0700, Satoru Logic wrote:
> 
> 
> 
> > I came across a function named `wsample` in a `utils` package of my
> 
> > workplace recently.
> 
> > 
> 
> > The "w" in `wsample` stands for `weighted`, and it randomly selects an
> 
> > element from a container according to relative weights of all the
> 
> > elements.
> 
> > 
> 
> > In most articles and codes I saw online, a function like this is often
> 
> > named `weighted_random_choice`, which sounds *correct* to me. So when I
> 
> > saw this `wsample` function, I considered it a improper name. Because
> 
> > `wsample`makes me think of `random.sample`, which returns a list of
> 
> > randomly generated elements, not a element.
> 
> 
> 
> You can have a sample size of one.
> 
> 
> 
> wsample sounds fine to me. weighted_random_choice is okay too. It depends 
> 
> whether you value brevity over explicitness. Explicit is good, but some 
> 
> names are just too long.
> 
> 
> 
> If this were my code base, I would probably go for weighted_sample 
> 
> without mentioning "random" in the name, the reasoning being that samples 
> 
> are almost always random so explicitly saying so doesn't help much.

I think if a programmer has used the built-in `random` module before, he would 
expect a function with "sample" in its name to return a population sequence.

If a function is to return scalar value instead of sequence, I would expect it 
to be named "choice".

> 
> 
> 
> 
> 
> -- 
> 
> Steven

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


Re: for-loop on cmd-line

2012-10-11 Thread suzaku
According to the document 
(http://docs.python.org/using/cmdline.html#interface-options),
> When called with -c command, it executes the Python statement(s) given as 
> command. Here command may contain multiple statements separated by newlines. 
> Leading whitespace is significant in Python statements!

So you should replace the semicolon with newline.

BTW, the loop can be simplified using `enumerate` like this:

   for i, p in enumerate(sys.path):
 

On Thursday, October 11, 2012 7:24:31 PM UTC+8, Gisle Vanem wrote:
> Hello list. I'm a newbie when it comes to Python.
> 
> 
> 
> I'm trying to turn this:
> 
> 
> 
>  def print_sys_path():
> 
> i = 0
> 
> for p in sys.path:
> 
>   print ('sys.path[%2d]: %s' % (i, p))
> 
>   i += 1
> 
> 
> 
> into a one-line python command (in a .bat file):
> 
> 
> 
>   python -c "import sys,os; i=0; for p in sys.path: print('sys.path[%%2d]: 
> %%s' %% (i, p)); i+=1"
> 
> 
> 
> But:
> 
>   File "", line 1
> 
> import sys,os; i=0; for p in sys.path: print('sys.path[%2d]: %s' % (i, 
> p)); i+=1
> 
>   ^
> 
> SyntaxError: invalid syntax
> 
> 
> 
> The caret is on the 'for'. What's the problem?
> 
> 
> 
> --gv
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is `wsample` a proper name for a function like this?

2012-10-11 Thread suzaku
On Friday, October 12, 2012 10:22:16 AM UTC+8, Steven D'Aprano wrote:
> On Wed, 10 Oct 2012 23:44:42 -0700, suzaku wrote:
> 
> 
> 
> > I think if a programmer has used the built-in `random` module before, he
> 
> > would expect a function with "sample" in its name to return a population
> 
> > sequence.
> 
> 
> 
> I have used the random module for about fifteen years, and I still write 
> 
> random.sample when I need to use random.choice.
> 
> 
> 
> In statistics, probability, and plain English, a sample can be a single 
> 
> item: that's why we can say "a sample" or "two samples".

Thanks for sharing your experience.

As I'm not a native speaker of English, 
when I learned that `random.choice` return single item,
  and `random.sample` return a sequence of items, 
I thought that the behaviour is determined by their definitions.

> 
> 
> 
> 
> 
> > If a function is to return scalar value instead of sequence, I would
> 
> > expect it to be named "choice".
> 
> 
> 
> And I wouldn't. But what do I care? I'm never going to use the code 
> 
> you're talking about, so call it "sasquatch" if you like, it's no skin 
> 
> off my nose.
> 
> 
> 
> :)
> 
> 
> 
> 
> 
> -- 
> 
> Steven

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


sum returns numpy.float64 when applied to a sequence of numpy.uint64

2012-12-15 Thread suzaku
I came across this question on StackOverflow today: 
http://stackoverflow.com/questions/13890451/why-numpy-sum-returns-a-float64-instead-of-an-uint64-when-adding-elements-of-a-g

I'm not familiar with `numpy` but I'm curious about this, so I started doing 
some experiments.

This is what I have discovered so far:

1. when a `generator ` is passed to `numpy.sum`, it fallback to use Python's 
built-in `sum`.

2. if elements of the sequence passed to `sum` is of type `numpy.uint64`, the 
result would be a number of type `numpy.float64`;

3. when I tried it with `numpy.int64`, the result is as expected: `numpy.int64`.

I guess the reason maybe that we don't really have `64 bits unsigned integer` 
in Python, so the numbers get converted to something different. And if so, I 
have no idea why it chose `float64` as the type.
-- 
http://mail.python.org/mailman/listinfo/python-list