views:

535

answers:

7

This is to do with the following code, which uses a for loop to generate a series of random offsets for use elsewhere in the program.

The index of this for loop is unused, and this is resulting in the 'offending' code being highlighted as a warning by Eclipse / PyDev

def RandomSample(count):    
    pattern = []
    for i in range(count):
        pattern.append( (random() - 0.5, random() - 0.5) )

    return pattern

So I either need a better way to write this loop that doesn't need a loop index, or a way to tell PyDev to ignore this particular instance of an unused variable.

Does anyone have any suggestions?

+4  A: 
randomSample = [(random() - 0.5, random() - 0.5) for _ in range(count)]

Sample output, for count=10 and assuming that you mean the Standard Library random() function:

[(-0.07, -0.40), (0.39, 0.18), (0.13, 0.29), (-0.11, -0.15),\
(-0.49, 0.42), (-0.20, 0.21), (-0.44, 0.36), (0.22, -0.08),\
(0.21, 0.31), (0.33, 0.02)]

If you really need to make it a function, then you can abbreviate by using a lambda:

f = lambda count: [(random() - 0.5, random() - 0.5) for _ in range(count)]

This way you can call it like:

>>> f(1)
f(1)
[(0.03, -0.09)]
>>> f(2)
f(2)
[(-0.13, 0.38), (0.10, -0.04)]
>>> f(5)
f(5)
[(-0.38, -0.14), (0.31, -0.16), (-0.34, -0.46), (-0.45, 0.28), (-0.01, -0.18)]
>>> f(10)
f(10)
[(0.01, -0.24), (0.39, -0.11), (-0.06, 0.09), (0.42, -0.26), (0.24, -0.44) , (-0.29, -0.30), (-0.27, 0.45), (0.10, -0.41), (0.36, -0.07), (0.00, -0.42)]
>>>

you get the idea...

Arrieta
This just renamed the unused variable to _ and so isn't really any better, unless Eclipse ignores that specially.
McPherrinM
Does PyDev not give the same warning about _ being unused?
jboxer
'_' is a dummy variable, Eclipse should not complain, but I don't know, as I have never developed Python on that platform. Good luck, and please let me know if it does complain.
Arrieta
Nope, _ doesn't get the warning.
Adam Luchjenbroers
@Adam: Good to know. Thanks for letting us know.
Arrieta
'_' isn't special in any way. Really, Eclipse *should* complain if it does in other cases. Of course, in this case, I feel just ignoring eclipse's complaint is valid.
McPherrinM
_ isn't special by meaning, but it is special by convention (it's accepted as the way to represent an unused loop index). I'd just forgotten about that particular convention.
Adam Luchjenbroers
@WaffleMatt: Please refer to the post herein which explains the default dummy variables in PyDev.
Arrieta
Why use lambda if you're going to give the function a name anyway. `def f(count) : return `[(random() - 0.5, random() - 0.5) for _ in range(count)]` is definitely way more pythonic.Also, this is probably a good candidate for a generator expression (and xrange if we're talking about Python 2).
Parker
Who accepted underscore as "the way to represent an unused loop index" in Python? Underscore is already frequently used for gettext in Python code, not to mention the last statement return value in the REPL.
Jeffrey Harris
A: 

Try this:

while count > 0:
    pattern.append((random() - 0.5, random() - 0.5))
    count -= 1
jboxer
+1  A: 

There should be a way to suppress code analysis errors in PyDev, like this:

http://pydev.org/manual%5Fadv%5Fassistants.html

Also, PyDev will ignore unused variables that begin with an underscore, as shown here:

http://pydev.org/manual%5Fadv%5Fcode%5Fanalysis.html

Nick
+2  A: 

Late to the party, but here's a potential idea:

def RandomSample(count):
    f = lambda: random() - 0.5
    r = range if count < 100 else xrange # or some other number
    return [(f(), f()) for _ in r(count)]

Strictly speaking, this is more or less the same as the other answers, but it does two things that look kind of nice to me.

First, it removes that duplicate code you have from writing random() - 0.5 twice by putting that into a lambda.

Second, for a certain size range, it chooses to use xrange() instead of range() so as not to unnecessarily generate a giant list of numbers you're going to throw away. You may want to adjust the exact number, because I haven't played with it at all, I just thought it might be a potential efficiency concern.

Chris Lutz
Why not just _always_ use xrange?
chrispy
That's a good alternative. I'm making the assumption that creating a generator for only two iterations might be more expensive than creating a list of only two elements. I, of course, have no benchmarks on how expensive generators and lists may or may not be, but somehow I have a feeling that if `xrange(2)` is more efficient than `range(2)` then something is terribly wrong with the implementation.
Chris Lutz
+10  A: 

Just for reference for ignoring variables in PyDev

By default pydev will ignore following variables

['_', 'empty', 'unused', 'dummy']

You can add more by passing supression parameters

-E, --unusednames  ignore unused locals/arguments if name is one of these values

Ref: http://eclipse-pydev.sourcearchive.com/documentation/1.0.3/PyCheckerLauncher%5F8java-source.html

S.Mark
PyDev suppresses warnings for unused variables if they *start with* an underscore. So `_i` would also work.
Craig McQueen
+5  A: 

How about itertools.repeat:

import itertools
count = 5
def make_pat():
    return (random() - 0.5, random() - 0.5)
list(x() for x in itertools.repeat(make_pat, count))

Sample output:

[(-0.056940506273799985, 0.27886450895662607), 
(-0.48772848046066863, 0.24359038079935535), 
(0.1523758626306998, 0.34423337290256517), 
(-0.018504578280469697, 0.33002406492294756), 
(0.052096928160727196, -0.49089780124549254)]
Ryan Ginstrom
As a function: `fn = lambda count: itertools.repeat(lambda: (random() - 0.5, random() - 0.5), count)`
Chris Lutz
A: 
import itertools, random   

def RandomSample2D(npoints, get_random=lambda: random.uniform(-.5, .5)):
    return ((r(), r()) for r in itertools.repeat(get_random, npoints))
  • uses random.uniform() explicitly
  • returns an iterator instead of list
J.F. Sebastian