Re: urllib.urlretrieve never returns???

2012-03-20 Thread Laszlo Nagy

Here you can find the example program and the original post.

http://code.activestate.com/lists/python-list/617894/


I gather you are running urlretrieve in a separate thread, inside a GUI?

Yes.


I have learned that whenever I have inexplicable behaviour in a function,
I should check my assumptions. In this case, (1) are you sure you have
the right urlretrieve, and (2) are you sure that your self.Log() method
is working correctly? Just before the problematic call, do this:

# was:
fpath = urllib.urlretrieve(imgurl)[0]

# becomes:
print(urllib.__file__, urlretrieve)
self.Log(urllib.__file__, urlretrieve)
fpath = urllib.urlretrieve(imgurl)[0]
I called self.Log() after each line, and also from a general "except:" 
clause. Definitely, the line after urlretrieve is not executed, and no 
exception is raised. Number of threads goes up (visible from task manager).


It is true that the program uses another module that uses the socket 
module and multiple threads. (These are written in pure python.)


If I remove the other module, then there is no error, however it renders 
the application useless. If I start the program with a console (e.g. 
with python.exe instead of pythonw.exe) then it works. Looks like 
opening a console solves the problem, although nothing is ever printed 
on the console.

and ensure that you haven't accidentally shadowed them with something
unexpected. Does the output printed to the console match the output
logged?
Well, this cannot be tested. If there is a console, then there is no 
problem.


What happens if you take the call to urlretrieve out of the thread and
call it by hand?

Then it works.

Run urllib.urlretrieve(imgurl) directly in the
interactive interpreter. Does it still hang forever?

Then it works perfectly.


When you say it "never" returns, do you mean *never* or do you mean "I
gave up waiting after five minutes"? What happens if you leave it to run
all day?
I did not try that. But I have already set socket timeout to 10 seconds, 
and definitely it is not waiting for a response from the server.


How big are the files you are trying to retrieve?

34 KB

Try retrieving a really small file. Then try retrieving a non-existent file.

Good point. I'll try to retrieve a nonexistent file when I get home. :)


What happens if you call urlretrieve with a reporthook argument? Does it
print anything?
I'll try this too. I'll also try using pycurl or the low level socket 
module instead.


What happens if you try to browse to imgurl in your web browser? Are you
sure the problem is with urlretrieve and not the source?

Yes.


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


Re: Currying in Python

2012-03-20 Thread Arnaud Delobelle
On 19 March 2012 23:20, Ian Kelly  wrote:
> I hope you don't mind if I critique your code a bit!
>
> On Fri, Mar 16, 2012 at 7:21 PM, Kiuhnm
>  wrote:
>> Here we go.
>>
>> --->
>> def genCur(f, unique = True, minArgs = -1):
>
> It is customary in Python for unsupplied arguments with no default to
> use the value None, not -1.  That's what it exists for.
>
>>    """ Generates a 'curried' version of a function. """
>>    def geng(curArgs, curKwargs):
>>        def g(*args, **kwargs):
>>            nonlocal f, curArgs, curKwargs, minArgs;    # our STATIC data

I don't know if all the rest of the code is below, but this line above
would only be necessary if you want to rebind f, curArgs, minArgs.
You don't seem to do it, so I think this line is unnecessary.

Also, your naming of variables disagrees with PEP 8 :)

>>            if len(args) or len(kwargs):
>
> Collections evaluate as true if they are not empty, so this could just be:
>
>            if args or kwargs:
>
>>                # Allocates data for the next 'g'. We don't want to modify our
>>                # static data.
>>                newArgs = curArgs[:];

Semicolon to end a statement?

>>                newKwargs = dict.copy(curKwargs);
>>
>>                # Adds positional arguments.
>>                newArgs += args;
>>
>>                # Adds/updates keyword arguments.
>>                if unique:
>>                    # We don't want repeated keyword arguments.
>>                    for k in kwargs.keys():
>>                        if k in newKwargs:
>>                            raise(Exception("Repeated kw arg while unique = 
>> True"));
>>                newKwargs.update(kwargs);
>
> Since you're writing this for Python 3 (as evidenced by the use of the
> nonlocal keyword), you could take advantage here of the fact that
> Python 3 dictionary views behave like sets.  Also, you should use a
> more specific exception type:
>
>                if unique and not kwargs.keys().isdisjoint(newKwargs):
>                    raise ValueError("A repeated keyword argument was 
> supplied")
>
>>                # Checks whether it's time to evaluate f.
>>                if minArgs >= 0 and minArgs <= len(newArgs) + len(newKwargs):
>
> With minArgs defaulting to None, that would be:
>
>                if minArgs is not None and minArgs <= len(newArgs) +
> len(newKwargs):
>
>>                    return f(*newArgs, **newKwargs);    # f has enough args
>>                else:
>>                    return geng(newArgs, newKwargs);    # f needs some more 
>> args
>>            else:
>>                return f(*curArgs, **curKwargs);    # the caller forced the 
>> evaluation
>>        return g;
>>    return geng([], {});
>>
>> def cur(f, minArgs = -1):
>>    return genCur(f, True, minArgs);
>>
>> def curr(f, minArgs = -1):
>>    return genCur(f, False, minArgs);
>
> The names "cur" and "curr" are terrible.  Good names should describe
> what the function does without being too onerous to type, and the
> addition of the duplicate "r" is not an obvious mnemonic for
> remembering that the first one prohibits duplicate keyword arguments
> and the second one allows them.  Why not more descriptive names like
> "curry" and "curry_unique"?
>
> That's all I've got.  All in all, it's pretty decent for a Python newbie.
>
> Cheers,
> Ian
> --
> http://mail.python.org/mailman/listinfo/python-list
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Distribution

2012-03-20 Thread Peter Otten
prince.pangeni wrote:

> Hi all,
>I am doing a simulation project using Python. In my project, I want
> to use some short of distribution to generate requests to a server.
> The request should have two distributions. One for request arrival
> rate (should be poisson) and another for request mix (i.e. out of the
> total requests defined in request arrival rate, how many requests are
> of which type).
>Example: Suppose the request rate is - 90 req/sec (generated using
> poisson distribution) at time t and we have 3 types of requests (i.e.
> r1, r2, r2). The request mix distribution output should be similar to:
> {r1 : 50 , r2 : 30 , r3 : 10} (i.e. out of 90 requests - 50 are of r1
> type, 30 are of r2 type and 10 are of r3 type).
>As I an new to python distribution module, I am not getting how to
> code this situation. Please help me out for the same.

You don't say what distribution module you're talking of, and I guess I'm 
not the only one who'd need to know that detail.

However, with sufficient resolution and duration the naive approach sketched 
below might be good enough.

# untested
DURATION = 3600 # run for one hour
RATE = 90 # requests/sec
RESOLUTION = 1000 # one msec

requests = ([r1]*50 + [r2]*30 + [r3]*10)
time_slots = [0]*(RESOLUTION*DURATION)
times = range(RESOLUTION*DURATION)

for _ in range(DURATION*RATE):
   time_slots[random.choice(times)] += 1

for time, count in enumerate(time_slots):
for _ in range(count):
issue_request_at(random.choice(requests), time)


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


Re: Currying in Python

2012-03-20 Thread Kiuhnm

On 3/20/2012 0:20, Ian Kelly wrote:

Since you're writing this for Python 3 (as evidenced by the use of the
nonlocal keyword), you could take advantage here of the fact that
Python 3 dictionary views behave like sets.  Also, you should use a
more specific exception type:


As a side note, "nonlocal" isn't needed in my code, in fact I removed it 
right after my first post.


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


Re: Currying in Python

2012-03-20 Thread Kiuhnm

On 3/20/2012 8:11, Arnaud Delobelle wrote:

On 19 March 2012 23:20, Ian Kelly  wrote:

I hope you don't mind if I critique your code a bit!

On Fri, Mar 16, 2012 at 7:21 PM, Kiuhnm
  wrote:

Here we go.

--->
def genCur(f, unique = True, minArgs = -1):


It is customary in Python for unsupplied arguments with no default to
use the value None, not -1.  That's what it exists for.


""" Generates a 'curried' version of a function. """
def geng(curArgs, curKwargs):
def g(*args, **kwargs):
nonlocal f, curArgs, curKwargs, minArgs;# our STATIC data


I don't know if all the rest of the code is below, but this line above
would only be necessary if you want to rebind f, curArgs, minArgs.
You don't seem to do it, so I think this line is unnecessary.


What a coincidence. I was just telling that to Ian Kelly. I removed it 
from the code in my article a few days ago but forgot to update my post 
on this ng.



Also, your naming of variables disagrees with PEP 8 :)


if len(args) or len(kwargs):


Collections evaluate as true if they are not empty, so this could just be:

if args or kwargs:


# Allocates data for the next 'g'. We don't want to modify our
# static data.
newArgs = curArgs[:];


Semicolon to end a statement?


As above. Too many years of C++.

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


Re: Eclipse, C, and Python

2012-03-20 Thread Martin P. Hellwig

On 20/03/2012 06:00, Richard Medina Calderon wrote:

Hello Forum. I have installed Python comnpiler in Eclipse Classic for Windows.
After a while I have installed the C compiler. However, somehow now when I try 
to run my code in Python it shows me for default Ant

Run -->Ant Build

I switched my workspace but still. Do you know how to solve this?..

Thanks


You might want to install the PyDev plugin and switch to that 
perspective (after configuring it).


Cheers,

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


Re: pypi and dependencies

2012-03-20 Thread Ben Finney
Andrea Crotti  writes:

> When I publish something on Pypi, is there a way to make it fetch the list
> of dependencies needed by my project automatically?
>
> It would be nice to have it in the Pypi page, without having to look at the
> actual code..

Sadly, no. The metadata available for packages on PyPI does not include
information about the dependencies.

(I'd love to be wrong about that, but I'm pretty certain that for most,
if not all, packages that's the case.)

> Any other possible solution?

All the solutions I've seen involve fetching the full package in order
to unpack it and *then* parse it for dependencies.

This is very sub-optimal, and I believe people are working on it; but
fixing it will at least require adjustment to all existing packages that
don't have dependencies in their metadata.

-- 
 \   “Some people have a problem, and they think “I know, I'll use |
  `\ Perl!”. Now they have some number of problems but they're not |
_o__) sure whether it's a string or an integer.” —Benno Rice, 2011 |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Distribution

2012-03-20 Thread Ben Finney
"prince.pangeni"  writes:

>I am doing a simulation project using Python. In my project, I want
> to use some short of distribution to generate requests to a server.

What is a distribution? That term already means something in Python
jargon, and it doesn't match the rest of your use case.

So what do you mean by “distribution”? Maybe we can find a less
confusing term.

-- 
 \ “I used to think that the brain was the most wonderful organ in |
  `\   my body. Then I realized who was telling me this.” —Emo Philips |
_o__)  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python is readable

2012-03-20 Thread Albert van der Horst
In article ,
Chris Angelico   wrote:
>On Mon, Mar 19, 2012 at 12:23 PM, Steven D'Aprano
> wrote:
>> On Mon, 19 Mar 2012 09:02:06 +1100, Chris Angelico wrote:
>>
>>> On Mon, Mar 19, 2012 at 8:30 AM, John Ladasky 
>>> wrote:
 What I would say is that, when PROGRAMMERS look at Python code for the
 first time, they will understand what it does more readily than they
 would understand other unfamiliar programming languages. =A0That has
 value.
>>>
>>> This is something that's never truly defined.
>>
>> I'm sorry, I don't understand what part of John's sentence you mean by
>> "this". "Programmers"? "Value"? "Understand"?
>
>I should have rewritten that into the next paragraph. Anyhow. Further
>explanation below.
>
>>> Everyone talks of how this
>>> language or that language is readable, but if you mean that you can look
>>> at a line of code and know what *that line* does then Python suffers
>>> badly and assembly language wins out;
>>
>> This is at least the second time you've alleged that assembly language is
>> more readable than Python. I think you're a raving nutter, no offence
>> Chris :-)
>
>None taken; guilty as charged. And unashamedly so. With that dealt
>with, though: My calling assembly "readable" is a form of argument by
>drawing to logical, but absurd, conclusion - by the given definition
>of readability, assembly is readable, ergo the definition sucks.
>(That's a term all logicians use, you know. Proper and formal jargon.)
>
>> Assignment (name binding) is close to the absolute simplest thing you can
>> do in a programming language. In Python, the syntax is intuitive to
>> anyone who has gone through high school, or possibly even primary school,
>> and been introduced to the equals sign.
>>
>> x =3D 1234
>> y =3D "Hello"
>
>Not quite. In mathematics, "x =3D 1234" is either a declaration of fact,
>or a statement that can be either true or false. In mathematics, "x =3D
>x + 1" is absurd and/or simply false. That's why Pascal has its :=3D
>operator, supposed to be read as "becomes" and not "equals". IMHO this
>is simply proof of one of the differences between programming and
>mathematics.
>
>> I don't know about anyone else, but I wouldn't have guessed that the way
>> to get x=3D1234 was with "x dw 1234".
>
>Except that it's not quite the same thing. That 8086 Assembly
>statement is more like the C statement:
>int x=3D1234;
>The nearest equivalent of assignment is:
>mov x,1234

I tried to mentally translate that to my ciasdis assembler syntax
and discovered that there is no instruction in the 8086 for that.
It would require using a scratch register like AX.

In my very precise ciasdis assembler syntax it would be  1]
XX: DL 0
MOVI, X| R| AX| 1234 IL,
MOV, X| F| R| [AX] XX L,

If you were restricted to the 8086, (not 80386 or better)
you could not have chosen AX, and you would have used BX instead.
[ The first MOVI, could be replaced by a LEA, instruction
LEA, AX'|  MEM|   XX L,
(Go figure!) ]

So a realistic fragment could have been
PUSH|X BX,
MOVI,  X| R| BX| 1234 IL,,
MOV,   X| F| R| [BX] XX L,
POP|X  BX,

The real unreadability comes from the fact that the novice would
ask herself why on earth the BX register was freed while the AX
register was free to use. And she is lucky, because no flags
were harmed in this sequence, another pitfall.

You can't blame me for the unreadibility of the ciasdis-syntax.
It merely reflects the abomination that the 8086/80386/Pentium
is.

Bottom line. A comparison between a HLL where the goal is abstraction
and assembly where the goal is precision and control, is unproductive.
And if you insist to do it, you better be a real expert.



>
>And that's where the nub of the question is. How well is sufficiently
>well? Clearly you do not require your code to be comprehensible to a
>non-programmer, or you would not write code at all. If you don't
>demand that the reader learn basic keywords of the language, then it's
>equally impossible to expect them to comprehend your code. If you're
>writing production code, I see no reason to avoid language features
>like Python's list comps, Pike's %{ %}  sprintf codes, or C's pointer
>arithmetic, just because they can confuse people. Learn the language,
>THEN start hacking on the code.

Can we just agree, that it is a compromise?

>
>ChrisA

1] ciasdis.html on the site in my sig.

Groetjes Albert

--
-- 
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- being exponential -- ultimately falters.
albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

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


Re: Distribution

2012-03-20 Thread Robert Kern

On 3/20/12 11:21 AM, Ben Finney wrote:

"prince.pangeni"  writes:


I am doing a simulation project using Python. In my project, I want
to use some short of distribution to generate requests to a server.


What is a distribution? That term already means something in Python
jargon, and it doesn't match the rest of your use case.

So what do you mean by “distribution”? Maybe we can find a less
confusing term.


Judging from the context, he means a probability distribution.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

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


Re: pypi and dependencies

2012-03-20 Thread Andrea Crotti

On 03/20/2012 11:18 AM, Ben Finney wrote:

Andrea Crotti  writes:


When I publish something on Pypi, is there a way to make it fetch the list
of dependencies needed by my project automatically?

It would be nice to have it in the Pypi page, without having to look at the
actual code..

Sadly, no. The metadata available for packages on PyPI does not include
information about the dependencies.

(I'd love to be wrong about that, but I'm pretty certain that for most,
if not all, packages that's the case.)


Any other possible solution?

All the solutions I've seen involve fetching the full package in order
to unpack it and *then* parse it for dependencies.

This is very sub-optimal, and I believe people are working on it; but
fixing it will at least require adjustment to all existing packages that
don't have dependencies in their metadata.



Yes that's not so nice, many projects write clearly the dependencies in 
the README file,

but that's annoying because it might get outdated.

And it's also sad that it's not automatically fetched from setuptools, 
because it's just


python setup.py egg-info && cat package.egg-info/requirements.txt

to actually extract them.
Should I file a bug maybe or is completely not feasible?
--
http://mail.python.org/mailman/listinfo/python-list


Re: pypi and dependencies

2012-03-20 Thread Donald Stufft
packaging (in 3.3) and distutils2 (2.x-3.2) is a new metadata format for python 
packages. It gets rid of setup.py and it includes a way to specify the 
requirements
that your package needs. This will show up on PyPI/Crate.


On Tuesday, March 20, 2012 at 8:01 AM, Andrea Crotti wrote:

> On 03/20/2012 11:18 AM, Ben Finney wrote:
> > Andrea Crottimailto:andrea.crott...@gmail.com)> 
> > writes:
> > 
> > > When I publish something on Pypi, is there a way to make it fetch the list
> > > of dependencies needed by my project automatically?
> > > 
> > > It would be nice to have it in the Pypi page, without having to look at 
> > > the
> > > actual code..
> > > 
> > 
> > Sadly, no. The metadata available for packages on PyPI does not include
> > information about the dependencies.
> > 
> > (I'd love to be wrong about that, but I'm pretty certain that for most,
> > if not all, packages that's the case.)
> > 
> > > Any other possible solution?
> > All the solutions I've seen involve fetching the full package in order
> > to unpack it and *then* parse it for dependencies.
> > 
> > This is very sub-optimal, and I believe people are working on it; but
> > fixing it will at least require adjustment to all existing packages that
> > don't have dependencies in their metadata.
> > 
> 
> 
> Yes that's not so nice, many projects write clearly the dependencies in 
> the README file,
> but that's annoying because it might get outdated.
> 
> And it's also sad that it's not automatically fetched from setuptools, 
> because it's just
> 
> python setup.py egg-info && cat package.egg-info/requirements.txt
> 
> to actually extract them.
> Should I file a bug maybe or is completely not feasible?
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 
> 


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


Re: Distribution

2012-03-20 Thread Robert Kern

On 3/20/12 4:31 AM, prince.pangeni wrote:

Hi all,
I am doing a simulation project using Python. In my project, I want
to use some short of distribution to generate requests to a server.
The request should have two distributions. One for request arrival
rate (should be poisson) and another for request mix (i.e. out of the
total requests defined in request arrival rate, how many requests are
of which type).
Example: Suppose the request rate is - 90 req/sec (generated using
poisson distribution)


Just a note on terminology to be sure we're clear: a Poisson *distribution* 
models the number of arrivals in a given time period if the events are from a 
Poisson *process* with a given mean rate. To model the inter-event arrival 
times, you use an exponential distribution. If you want to handle events 
individually in your simulation, you will need to use the exponential 
distribution to figure out the exact times for each. If you are handling all of 
the events in each second "in bulk" without regard to the exact times or 
ordering within that second, then you can use a Poisson distribution.



at time t and we have 3 types of requests (i.e.
r1, r2, r2). The request mix distribution output should be similar to:
{r1 : 50 , r2 : 30 , r3 : 10} (i.e. out of 90 requests - 50 are of r1
type, 30 are of r2 type and 10 are of r3 type).
As I an new to python distribution module, I am not getting how to
code this situation. Please help me out for the same.


I am going to assume that you want to handle each event independently. A basic 
strategy is to keep a time variable starting at 0 and use a while loop until the 
time reaches the end of the simulation time. Increment it using a draw from the 
exponential distribution each loop. Each iteration of the loop is an event. To 
determine the kind of event, you will need to draw from a weighted discrete 
distribution. What you want to do here is to do a cumulative sum of the weights, 
draw a uniform number from 0 to the total sum, then use bisect to find the item 
that matches.


import bisect
import random


# Use a seeded PRNG for repeatability. Use the methods on the Random
# object rather than the functions in the random module.
prng = random.Random(1234567890)

avg_rate = 90.0  # reqs/sec

kind_weights = [50.0, 30.0, 10.0]
kind_cumsum = [sum(kind_weights[:i+1]) for i in range(len(kind_weights))]
kind_max = kind_cumsum[-1]

max_time = 10.0  # sec
t = 0.0  # sec
events = []  # (t, kind)
while t < max_time:
dt = prng.expovariate(avg_rate)
u = prng.uniform(0.0, kind_max)
kind = bisect.bisect_left(kind_cumsum, u)
events.append((t, kind))
t += dt


--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

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


Re: Eclipse, C, and Python

2012-03-20 Thread Steven D'Aprano
On Mon, 19 Mar 2012 23:00:50 -0700, Richard Medina Calderon wrote:

> Hello Forum. I have installed Python comnpiler in Eclipse Classic for
> Windows. After a while I have installed the C compiler. However, somehow
> now when I try to run my code in Python it shows me for default Ant
> 
> Run -->Ant Build
> 
> I switched my workspace but still. Do you know how to solve this?..

This is an Eclipse problem, not a Python problem. Most people here don't 
use Eclipse. You might have better luck asking on a forum for Eclipse.


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


Re: s_push parser stack overflow

2012-03-20 Thread alister ware
On Tue, 20 Mar 2012 04:30:51 +, MRAB wrote:

> On 20/03/2012 03:19, Артём Назаров wrote:
>> Hi.
>> Sorry of my english :-)
>>
>> code:
>> print
>> 
(((0)))"
>>
>> message from python is: s_push parser stack overflow
>>
>> Can i configure phyton to solve this problem without change MAXSTACK in
>> " parser.c" and re-build the python?
> 
> A quick look at parser.h tells me that it's a hard-coded limit:
> 
> typedef struct {
>   stackentry  *s_top; /* Top entry */
>   stackentry   s_base[MAXSTACK];/* Array of stack entries */
>   /* NB The stack grows down */
> } stack;


But why such an obscure print statement anyway?
is it purely to show the issue or is there some actual reason behind it?


-- 
divorce, n:
A change of wife.
-- 
http://mail.python.org/mailman/listinfo/python-list


code for computing and printing list of combinations

2012-03-20 Thread Joi Mond
To All,
Can someone help me with the proper code to compute combinations for n=7, r=5 
for the following list of numbers: 7, 8, 10, 29, 41, 48, 55. There should be 21 
combination.
Also once there list is made can a code be written to add (sum) each of the set 
of five number in the the list. For example list: 8, 10, 29, 48, 55, = 150.
Thanks-- 
http://mail.python.org/mailman/listinfo/python-list


Re: code for computing and printing list of combinations

2012-03-20 Thread Peter Otten
Joi Mond wrote:

> Can someone help me with the proper code to compute combinations for n=7,
> r=5 for the following list of numbers: 7, 8, 10, 29, 41, 48, 55. There
> should be 21 combination. Also once there list is made can a code be
> written to add (sum) each of the set of five number in the the list. For
> example list: 8, 10, 29, 48, 55, = 150. Thanks

>>> [sum(x) for x in itertools.combinations([7,8,10,29,41,48,55], 5)]
[95, 102, 109, 114, 121, 128, 133, 140, 147, 159, 135, 142, 149, 161, 180, 
136, 143, 150, 162, 181, 183]

Best wishes to your teacher...

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


Re: code for computing and printing list of combinations

2012-03-20 Thread Tim Chase

On 03/20/12 09:59, Joi Mond wrote:

To All, Can someone help me with the proper code to compute
combinations for n=7, r=5 for the following list of numbers:
7, 8, 10, 29, 41, 48, 55. There should be 21 combination. Also
once there list is made can a code be written to add (sum)
each of the set of five number in the the list. For example
list: 8, 10, 29, 48, 55, = 150. Thanks


Sounds like you want to read up on itertools.combinations() and 
the sum() function.


http://docs.python.org/library/itertools.html#itertools.combinations

or

  >>> import itertools
  >>> help(itertools.combinations)

This sounds like a homework problem, so I won't hand you the 
answer, but you pass your list to combinations() with the 
grouping-size (r).  You then iterate over the results of that, 
and use sum() on the results.


-tkc



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


Re: Python is readable

2012-03-20 Thread Nathan Rice
Just to troll the discussion a little bit more...

On Sun, Mar 18, 2012 at 6:02 PM, Chris Angelico  wrote:
> On Mon, Mar 19, 2012 at 8:30 AM, John Ladasky  wrote:
>> What I would say is that, when PROGRAMMERS look at Python code for the
>> first time, they will understand what it does more readily than they
>> would understand other unfamiliar programming languages.  That has
>> value.
>
> This is something that's never truly defined. Everyone talks of how
> this language or that language is readable, but if you mean that you
> can look at a line of code and know what *that line* does, then Python
> suffers badly and assembly language wins out; but if you mean that you
> should be able to glance over an entire function and comprehend its
> algorithm, then I have yet to see any language in which it's not
> plainly easy to write bad code. Even with good code, anything more
> than trivial can't be eyeballed in that way - if you could, what would
> docstrings be for?

I agree, docstrings/code comments are a pretty obvious indication that
code (as it exists currently) fails as a human communication medium.
I suppose that I could make an exception for elaboration on statement
with subtle implications.  This seems like something people should
think more about fixing.

> Really, the metric MUST be Python programmers. Intuitiveness is of
> value, but readability among experienced programmers is far more
> useful. If I write a whole lot of code today, and next year I'm dead
> and someone else has replaced me, I frankly don't mind if he has to
> learn the language before he can grok my code. I _do_ mind if, even
> after he's learned the language, he can't figure out what my code's
> doing; and that's where Python's placed itself at about the right
> level - not so high that it's all in airy-fairy conceptual work, but
> not so low that it gets bogged down. There's a handful of other
> languages that are similarly placed, and they're the languages that I
> would call "readable".

In mathematics, when you perform global optimization you must be
willing to make moves in the solution space that may result in a
temporary reduction of your optimality condition.  If you just perform
naive gradient decent, only looking to the change that will induce the
greatest immediate improvement in optimality, you will usually end up
orbiting around a solution which is not globally optimal.  I mention
this because any readability or usability information gained using
trained programmers is simultaneously measuring the readability or
usability and its conformance to the programmer's cognitive model of
programming.  The result is local optimization around the
current-paradigm minimum.  This is why we have so many nearly
identical curly brace C-like languages.

In my opinion, language readability and usability should be determined
based on the following tests:

- Given users with no programming experience:
-- What is the probability that they can correctly guess the result of
a program, and how long does it take?
-- What is the probability that they can take a program and correctly
modify it to change its output to another specified output, or modify
it to support another input representation, and how long does it take?

- Given users with no programming experience who are provided with a
controlled training period:
-- What is the probability that they can produce a program that
correctly implements a simple, completely described algorithm for
solving a puzzle or winning a game, and how long does it take?

- Given users with previous (but not extensive) programming experience
who are provided with a controlled training period:
-- What is the probability that they can produce a program that
correctly implements a simple, partially described algorithm for
solving a puzzle or winning a game, and how long does it take?
-- What is the probability that they can produce a program that
correctly implements a complex, completely described algorithm for
solving a puzzle or winning a game, and how long does it take?

It would probably also be worth examining user behavior relating to
code organization and composition under a variety of circumstances.
It seems likely that if proper organization and composition are
intuitive, productivity would scale well with program size.

> Here's an analogy: One statement (aka line of code, etc) corresponds
> to one sentence in English. Massive one-liners are like some of the
> sentences in Paul's epistles; assembly language is like "The cat sat
> on the mat". Both are valid; both are hard to read.

This is one of my gripes with the dogmatic application of the "break
it into multiple statements" mantra of Python.  Not only are you
forced to use generators to maintain semantic equivalence in many
cases, in some cases a partial statement fragment doesn't have any
intuitive meaning.  The result is that readers are forced to hold the
value of intermediate_variable in their head while reading another
statement, then tran

Fabric Engine v1.0 released under AGPL

2012-03-20 Thread Fabric Paul
Hi everyone - just letting you know that we released v1.0 of Fabric
Engine today. We've open-sourced the core under AGPL, so I hope that
gives you an incentive to get started with high-performance for
Python :)

http://fabricengine.com/technology/benchmarks/ - to give you an idea
of the kind of performance possible. Most of these are with node, but
the core engine is the same - we just bound it to Python.

For those of you using Python on the desktop (particularly if you're
working with 3D), we've started a closed beta on a PyQt framework -
you can see more here: 
http://fabricengine.com/2012/03/pyqt-framework-for-fabric-engine/
- email b...@fabricengine.com if you'd like to take part in the
testing program.

Thanks for your time,

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


Re: Distribution

2012-03-20 Thread Laurent Claessens

Il 20/03/2012 12:21, Ben Finney ha scritto:

"prince.pangeni"  writes:


I am doing a simulation project using Python. In my project, I want
 to use some short of distribution to generate requests to a server.


I guess scipy is also available in plain python (didn't check), but the 
following works with Sage :


--
| Sage Version 4.8, Release Date: 2012-01-20 |
| Type notebook() for the GUI, and license() for information.|
--
sage: from scipy import stats
sage: X=stats.poisson.rvs
sage: X(4)
5
sage: X(4)
2
sage: X(4)
3


Hope it helps
Laurent
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python is readable

2012-03-20 Thread Steven D'Aprano
On Tue, 20 Mar 2012 12:55:07 -0400, Nathan Rice wrote:

> This is one of my gripes with the dogmatic application of the "break it
> into multiple statements" mantra of Python.

I must admit I don't recognise that one, unless you're talking about "not 
everything needs to be a one liner".


> Not only are you forced to
> use generators to maintain semantic equivalence in many cases, in some
> cases a partial statement fragment doesn't have any intuitive meaning. 
> The result is that readers are forced to hold the value of
> intermediate_variable in their head while reading another statement,
> then translate the statement to the conceptually complete form.  A
> statement should be an encoding from a conceptual space to a operation
> space, and ideally the two should be as similar as possible.
>  If a concept is atomic, it should not be comprised of multiple
> statements.

Perhaps you could give some examples (actual or contrived) of stuff where 
"breaking it into multiple statements" is a bad thing?


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


Re: Python is readable

2012-03-20 Thread Terry Reedy

On 3/20/2012 12:55 PM, Nathan Rice wrote:


I agree, docstrings/code comments are a pretty obvious indication that
code (as it exists currently) fails as a human communication medium.


The fact that scientific journal articles start with a documentation 
string called an abstract does not indicate that scientific English 
fails as a human communication medium. Function docstrings say what the 
function does and how to use it without reading the code. They can be 
pulled out and displayed elsewhere. They also guide the reading of the 
code. Abstracts serve the same functions.


--
Terry Jan Reedy

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


Enforcing hash randomization (was: [RELEASED] Second release candidates for Python 2.6.8, 2.7.3, 3.1.5, and 3.2.3)

2012-03-20 Thread Michael Ströder
Benjamin Peterson wrote:
> Hash randomization causes the iteration order of dicts and sets to be
> unpredictable and differ across Python runs. Python has never guaranteed
> iteration order of keys in a dict or set, and applications are advised to 
> never
> rely on it. Historically, dict iteration order has not changed very often 
> across
> releases and has always remained consistent between successive executions of
> Python. Thus, some existing applications may be relying on dict or set 
> ordering.
> Because of this and the fact that many Python applications which don't accept
> untrusted input are not vulnerable to this attack, in all stable Python 
> releases
> mentioned here, HASH RANDOMIZATION IS DISABLED BY DEFAULT. There are two ways 
> to
> enable it. The -R commandline option can be passed to the python executable. 
> It
> can also be enabled by setting an environmental variable PYTHONHASHSEED to
> "random". (Other values are accepted, too; pass -h to python for complete
> description.)

I wonder how I could enforce hash randomization from within a Python app
without too much hassle. I'd like to avoid having to rely on sys-admins doing
the right thing when installing my web2ldap.

I guess
os.environ['PYTHONHASHSEED'] = 'random'
before forking a process would be a solution. But is there another way?

Ciao, Michael.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python is readable

2012-03-20 Thread Nathan Rice
>> This is one of my gripes with the dogmatic application of the "break it
>> into multiple statements" mantra of Python.
>
> I must admit I don't recognise that one, unless you're talking about "not
> everything needs to be a one liner".
> ...
> Perhaps you could give some examples (actual or contrived) of stuff where
> "breaking it into multiple statements" is a bad thing?

One example is performing a series of transformations on a collection
of data, with the intent of finding an element of that collection that
satisfies a particular criterion.  If you separate out the individual
transformations, you need to understand generators or you will waste
space and perform many unnecessary calculations.  If you only ever do
a single transformation with a clear conceptual meaning, you could
create a "master transformation function," but what if you have a
large number of potential permutations of that function?  What if you
are composing three or four functions, each of which is conditional on
the data?  If you extract things from a statement and assign them
somewhat arbitrary names, you've just traded horizontal bloat for
vertical bloat (with a net increase in volume), while forcing a reader
to scan back and forth to different statements to understand what is
happening.

To steal a line from Einstein, "Make things as simple as possible, but
not simpler"

>> I agree, docstrings/code comments are a pretty obvious indication that
>> code (as it exists currently) fails as a human communication medium.
>
>
> The fact that scientific journal articles start with a documentation string
> called an abstract does not indicate that scientific English fails as a
> human communication medium. Function docstrings say what the function does
> and how to use it without reading the code. They can be pulled out and
> displayed elsewhere. They also guide the reading of the code. Abstracts
> serve the same functions.

A paper, with topic introduction, methods exposition, data/results
description and discussion is a poor analog to a function.  I would
compare the abstract of a scientific paper to the overview section of
a program's documentation.  The great majority of the docstrings I see
are basically signature rehashes with added type information and
assertions, followed by a single sentence English gloss-over.  If the
code were sufficiently intuitive and expressive, that would be
redundant.  Of course, there will always be morbidly obese functions
and coders that like to wax philosophical or give history lessons in
comments.

Also, because of Sphinx, it is very common in the Python community
weave documents and code together in a way that is convenient for
authors but irritating for readers.  I personally would prefer not to
have to scroll past 100 lines of a tutorial with examples, tests and
what not in order to go from one function to another.  It would be
really awesome if everyone used links to that material in docstrings,
and the default sphinx theme created an inline collapsible iframe that
included that material for the HTML version.  Don't get me wrong, I
adore Sphinx, the problem here is people who are lazy or don't know
the right way to structure docs.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python is readable

2012-03-20 Thread Terry Reedy

On 3/20/2012 3:28 PM, Nathan Rice wrote:

This is one of my gripes with the dogmatic application of the "break it
into multiple statements" mantra of Python.


I must admit I don't recognise that one, unless you're talking about "not
everything needs to be a one liner".
...
Perhaps you could give some examples (actual or contrived) of stuff where
"breaking it into multiple statements" is a bad thing?


One example is performing a series of transformations on a collection
of data, with the intent of finding an element of that collection that
satisfies a particular criterion.  If you separate out the individual
transformations, you need to understand generators or you will waste
space and perform many unnecessary calculations.  If you only ever do
a single transformation with a clear conceptual meaning, you could
create a "master transformation function," but what if you have a
large number of potential permutations of that function?  What if you
are composing three or four functions, each of which is conditional on
the data?  If you extract things from a statement and assign them
somewhat arbitrary names, you've just traded horizontal bloat for
vertical bloat (with a net increase in volume), while forcing a reader
to scan back and forth to different statements to understand what is
happening.

To steal a line from Einstein, "Make things as simple as possible, but
not simpler"


I agree, docstrings/code comments are a pretty obvious indication that
code (as it exists currently) fails as a human communication medium.



The fact that scientific journal articles start with a documentation string
called an abstract does not indicate that scientific English fails as a
human communication medium. Function docstrings say what the function does
and how to use it without reading the code. They can be pulled out and
displayed elsewhere. They also guide the reading of the code. Abstracts
serve the same functions.


A paper, with topic introduction, methods exposition, data/results
description and discussion is a poor analog to a function.  I would
compare the abstract of a scientific paper to the overview section of
a program's documentation.  The great majority of the docstrings I see
are basically signature rehashes with added type information and
assertions, followed by a single sentence English gloss-over.  If the
code were sufficiently intuitive and expressive, that would be
redundant.  Of course, there will always be morbidly obese functions
and coders that like to wax philosophical or give history lessons in
comments.


Both abstracts and doc strings are designed to be and are read 
independently of the stuff they summarize. Perhaps you do not use 
help(obj) as often as some other people do.



Also, because of Sphinx, it is very common in the Python community
weave documents and code together in a way that is convenient for
authors but irritating for readers.  I personally would prefer not to
have to scroll past 100 lines of a tutorial with examples, tests and
what not in order to go from one function to another.


If I understand you, some devs agree. Hence the increasing use of How-to 
docs with tutorial and example material for a module separate from the 
reference entries in its section of the Library Reference.


--
Terry Jan Reedy

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


Re: urllib.urlretrieve never returns???

2012-03-20 Thread Laszlo Nagy

2012.03.20. 8:08 keltezéssel, Laszlo Nagy írta:

Here you can find the example program and the original post.

http://code.activestate.com/lists/python-list/617894/


I gather you are running urlretrieve in a separate thread, inside a GUI?

Yes.


I have learned that whenever I have inexplicable behaviour in a 
function,

I should check my assumptions. In this case, (1) are you sure you have
the right urlretrieve, and (2) are you sure that your self.Log() method
is working correctly? Just before the problematic call, do this:

# was:
fpath = urllib.urlretrieve(imgurl)[0]

# becomes:
print(urllib.__file__, urlretrieve)
self.Log(urllib.__file__, urlretrieve)
fpath = urllib.urlretrieve(imgurl)[0]
I called self.Log() after each line, and also from a general "except:" 
clause. Definitely, the line after urlretrieve is not executed, and no 
exception is raised. Number of threads goes up (visible from task 
manager).


It is true that the program uses another module that uses the socket 
module and multiple threads. (These are written in pure python.)


If I remove the other module, then there is no error, however it 
renders the application useless. If I start the program with a console 
(e.g. with python.exe instead of pythonw.exe) then it works. Looks 
like opening a console solves the problem, although nothing is ever 
printed on the console.

and ensure that you haven't accidentally shadowed them with something
unexpected. Does the output printed to the console match the output
logged?
Well, this cannot be tested. If there is a console, then there is no 
problem.


What happens if you take the call to urlretrieve out of the thread and
call it by hand?

Then it works.

Run urllib.urlretrieve(imgurl) directly in the
interactive interpreter. Does it still hang forever?

Then it works perfectly.


When you say it "never" returns, do you mean *never* or do you mean "I
gave up waiting after five minutes"? What happens if you leave it to run
all day?
I did not try that. But I have already set socket timeout to 10 
seconds, and definitely it is not waiting for a response from the server.


How big are the files you are trying to retrieve?

34 KB
Try retrieving a really small file. Then try retrieving a 
non-existent file.

Good point. I'll try to retrieve a nonexistent file when I get home. :)


Today I got a different error message printed on console (program 
started with python.exe)




Unhandled exception in thread started by FrameLocEdit.GetThumbnail of Object of type 'wxPanel *' at 0x4f85300>
>>Unhandled exception in thread started by FrameLocEdit.GetThumbnail of Object of type 'wxPanel *' at 0x4f85300>
>>Unhandled exception in thread started by FrameLocEdit.GetThumbnail of Object of type 'wxPanel *' at 0x4f85300>

>>Unhandled exception in thread started by
Traceback (most recent call last):

Traceback (most recent call last):

Traceback (most recent call last):
of 
>>  File "C:\Python\Projects\Warehouserclient_v3\locedit.py", line 917, 
in GetThumbnail
  File "C:\Python\Projects\Warehouserclient_v3\locedit.py", line 917, 
in GetThumbnail
  File "C:\Python\Projects\Warehouserclient_v3\locedit.py", line 917, 
in GetThumbnail


sys.excepthook is missing
Traceback (most recent call last):

I have never seen a traceback like this before. I didn't install any 
excepthook myself.


Program is using wxPython, socket, threads, threading, PIL.

Here is something even more strange. If I click on the button three 
times, then absolutely nothing gets printed on stdout. However, if I 
close the program with file/exit (actually, calling 
wx.PySimpleApp.ExitMainLoop) then suddenly three stack traces are 
printed on stdout, all lines mixed up:


Unhandled exception in thread started by FrameLocEdit.GetThumbnail of Object of type 'wxPanel *' at 0x4fb530

0>
>>Unhandled exception in thread started by Unhandled exception in 
thread started by 0x4fb5300>
>>Unhandled exception in thread started by Unhandled exception in 
thread started by 0x4fb5300>
>>Traceback (most recent call last):FrameLocEdit.GetThumbnail of Object of type 'wxPanel *' at 0x4fb5300>
>>Traceback (most recent call last):FrameLocEdit.GetThumbnail of Object of type 'wxPanel *' at 0x4fb5300>
>>Traceback (most recent call last):  File 
"C:\Python\Projects\Warehouserclient_v3\locedit.py", line 917, in 
GetThumbnail



Traceback (most recent call last):

  File "C:\Python\Projects\Warehouserclient_v3\locedit.py", line 917, 
in GetThumbnail

Traceback (most recent call last):
  File "C:\Python\Projects\Warehouserclient_v3\locedit.py", line 917, 
in GetThumbnail
  File "C:\Python\Projects\Warehouserclient_v3\locedit.py", line 
917, in GetThumbnail
  File "C:\Python\Projects\Warehouserclient_v3\locedit.py", line 
917, in GetThumbnail

self.Log(traceback.format_exc())
self.Log(traceback.format_exc())
self.Log(traceback.format_exc())
AttributeErrorself.Log(traceback.format_exc())
AttributeErrorself.Log(traceback.format_exc())
AttributeError: AttributeE

Re: urllib.urlretrieve never returns???

2012-03-20 Thread John Nagle

On 3/17/2012 9:34 AM, Chris Angelico wrote:

2012/3/18 Laszlo Nagy:

In the later case, "log.txt" only contains "#1" and nothing else. If I look
at pythonw.exe from task manager, then its shows +1 thread every time I
click the button, and "#1" is appended to the file.


   Does it fail to retrieve on all URLs, or only on some of them?

   Running a web crawler, I've seen some pathological cases.
There are a very few sites that emit data very, very slowly,
but don't time out because they are making progress.  There are
also some sites where attempting to negotiate a SSL connection
results in the SSL protocol reaching a point where the host end
is supposed to finish the handshake, but it doesn't.

   The odds are against this being the problem. I see problems
like that in maybe 1 in 100,000 URLs.

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


Re: Distribution

2012-03-20 Thread duncan smith

On 20/03/12 04:31, prince.pangeni wrote:

Hi all,
I am doing a simulation project using Python. In my project, I want
to use some short of distribution to generate requests to a server.
The request should have two distributions. One for request arrival
rate (should be poisson) and another for request mix (i.e. out of the
total requests defined in request arrival rate, how many requests are
of which type).
Example: Suppose the request rate is - 90 req/sec (generated using
poisson distribution) at time t and we have 3 types of requests (i.e.
r1, r2, r2). The request mix distribution output should be similar to:
{r1 : 50 , r2 : 30 , r3 : 10} (i.e. out of 90 requests - 50 are of r1
type, 30 are of r2 type and 10 are of r3 type).
As I an new to python distribution module, I am not getting how to
code this situation. Please help me out for the same.

   Thanks in advance

Prince


Robert has given you a very good answer. The easiest way is to generate 
interarrival times using an exponential distribution, then for each 
event select the type from a categorical probability mass function. 
Perhaps the easiest and most efficient approach for the latter using 
your 'mix distribution' above is to create a list containing 5 instances 
of r1, 3 of r2 and 1 of r3. Then select the type by generating a random 
index into the list. It is not an ideal solution generally, but good 
when the parameters do not change and the required list is small.


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


List comprehension/genexp inconsistency.

2012-03-20 Thread J. Cliff Dyer
One of my coworkers just stumbled across an interesting issue.  I'm
hoping someone here can explain why it's happening.

When trying to create a class with a dual-loop generator expression in a
class definition, there is a strange scoping issue where the inner
variable is not found, (but the outer loop variable is found), while a
list comprehension has no problem finding both variables.

Demonstration:

>>> class Spam:
... foo, bar = 4, 4
... baz = dict(((x, y), x+y) for x in range(foo) for y in
range(bar))
... 
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 3, in Spam
  File "", line 3, in 
NameError: global name 'bar' is not defined
>>> class Eggs(object):
... foo, bar = 4, 4
... baz = dict([((x, y), x+y) for x in range(foo) for y in
range(bar)])
... 
>>> 

This was discovered in python 2.6.  In python 3.2, both versions fail
with the same NameError.  

Obviously, this is easy enough to work around.  I'm curious though:
What's going on under the hood to cause the nested generator expression
to fail while the list comprehension succeeds?

Cheers,
Cliff


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


Re: Fabric Engine v1.0 released under AGPL

2012-03-20 Thread Colin J. Williams

On 20/03/2012 12:51 PM, Fabric Paul wrote:

Hi everyone - just letting you know that we released v1.0 of Fabric
Engine today. We've open-sourced the core under AGPL, so I hope that
gives you an incentive to get started with high-performance for
Python :)

http://fabricengine.com/technology/benchmarks/ - to give you an idea
of the kind of performance possible. Most of these are with node, but
the core engine is the same - we just bound it to Python.

For those of you using Python on the desktop (particularly if you're
working with 3D), we've started a closed beta on a PyQt framework -
you can see more here: 
http://fabricengine.com/2012/03/pyqt-framework-for-fabric-engine/
- email b...@fabricengine.com if you'd like to take part in the
testing program.

Thanks for your time,

Paul



It seems that sing;e dimension arrays are used in KL.  How does this 
compare with Numpy?


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


Re: Python is readable

2012-03-20 Thread Nathan Rice
>>> The fact that scientific journal articles start with a documentation
>>> string
>>> called an abstract does not indicate that scientific English fails as a
>>> human communication medium. Function docstrings say what the function
>>> does
>>> and how to use it without reading the code. They can be pulled out and
>>> displayed elsewhere. They also guide the reading of the code. Abstracts
>>> serve the same functions.
>>
>>
>> A paper, with topic introduction, methods exposition, data/results
>> description and discussion is a poor analog to a function.  I would
>> compare the abstract of a scientific paper to the overview section of
>> a program's documentation.  The great majority of the docstrings I see
>> are basically signature rehashes with added type information and
>> assertions, followed by a single sentence English gloss-over.  If the
>> code were sufficiently intuitive and expressive, that would be
>> redundant.  Of course, there will always be morbidly obese functions
>> and coders that like to wax philosophical or give history lessons in
>> comments.
>
>
> Both abstracts and doc strings are designed to be and are read independently
> of the stuff they summarize. Perhaps you do not use help(obj) as often as
> some other people do.

I find help() to be mostly useless because of the clutter induced by
double under methods.  I use IPython, and I typically will either use
tab name completion with the "?" feature or %edit  if I really
need to dig around.  I teach Python to groups from time to time as
part of my job, and I usually only mention help() as something of an
afterthought, since typically people react to the output like a deer
in headlights.  Some sort of semantic function and class search from
the interpreter would probably win a lot of fans, but I don't know
that it is possible without a standard annotation format and the
addition of a namespace cache to pyc files.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: urllib.urlretrieve never returns??? [SOLVED] - workaround

2012-03-20 Thread Laszlo Nagy



I'll be experimenting with pyCurl now.
By replacing the GetThumbnail method with this brainless example, taken 
from the pyCurl demo:



def GetThumbnail(self,imgurl):
class Test:
def __init__(self):
self.contents = ''

def body_callback(self, buf):
self.contents = self.contents + buf

self.Log("#1: "+repr(imgurl))
try:
t = Test()
c = pycurl.Curl()
c.setopt(c.URL, imgurl)
c.setopt(c.WRITEFUNCTION, t.body_callback)
self.Log("#2")
c.perform()
self.Log("#3")
c.close()
self.Log("#4")
fpath = os.path.join(os.environ["TEMP"],"thumbnail.jpg")
fout = open(fpath,"wb+")
self.Log("#5: "+repr(fpath))
try:
fout.write(t.contents)
finally:
fout.close()
self.Log("#6")
except:
self.Log(traceback.format_exc())
return
self.Log("#7")
wx.CallAfter(self.imgProduct.SetPage,"""src="%s">"""%fpath)

self.Log("#8")

Everything works perfectly, in all modes: console, no console, started 
directly and started in separate thread.


So the problem with urllib must be. Maybe wxPython installs some except 
hooks, or who knows? If somebody feels up to it, I can start narrowing 
down the problem to the smallest possible application. But only if 
someone knows how to debug core code because I don't. Otherwise I'll 
just use pyCURL.


Thank you for your help!

   Laszlo

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


Released: stackprint -- analyze python stack dumps in server logs

2012-03-20 Thread Roy Smith
Stackprint is a little tool for finding, formatting, and categorizing
python stack dumps in server log files.   We've found it useful for
monitoring the health of our django applications.

https://bitbucket.org/roysmith/python-tools.  BSD license.
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: urllib.urlretrieve never returns???

2012-03-20 Thread Prasad, Ramit
> Today I got a different error message printed on console (program
> started with python.exe)
> 
> 
> 
> Unhandled exception in thread started by  FrameLocEdit.GetThumbnail of  Object of type 'wxPanel *' at 0x4f85300>
>  >>Unhandled exception in thread started by  FrameLocEdit.GetThumbnail of  Object of type 'wxPanel *' at 0x4f85300>
>  >>Unhandled exception in thread started by  FrameLocEdit.GetThumbnail of  Object of type 'wxPanel *' at 0x4f85300>
>  >>Unhandled exception in thread started by
> Traceback (most recent call last):
> 
> Traceback (most recent call last):
> 
> Traceback (most recent call last):
>  of 
>  >>  File "C:\Python\Projects\Warehouserclient_v3\locedit.py", line 917,
> in GetThumbnail
>File "C:\Python\Projects\Warehouserclient_v3\locedit.py", line 917,
> in GetThumbnail
>File "C:\Python\Projects\Warehouserclient_v3\locedit.py", line 917,
> in GetThumbnail
> 
> sys.excepthook is missing
> Traceback (most recent call last):
> 
> I have never seen a traceback like this before. I didn't install any
> excepthook myself.
> 
> Program is using wxPython, socket, threads, threading, PIL.
> 
> Here is something even more strange. If I click on the button three
> times, then absolutely nothing gets printed on stdout. However, if I
> close the program with file/exit (actually, calling
> wx.PySimpleApp.ExitMainLoop) then suddenly three stack traces are
> printed on stdout, all lines mixed up:
> 

Maybe in self.Log you should add a sys.stdout.flush  and 
sys.stderr.flush(). I am not very familiar with stdin/out but
maybe it will help.

> Unhandled exception in thread started by  FrameLocEdit.GetThumbnail of  Object of type 'wxPanel *' at 0x4fb530
> 0>
>  >>Unhandled exception in thread started by Unhandled exception in
> thread started by   0x4fb5300>
>  >>Unhandled exception in thread started by Unhandled exception in
> thread started by   0x4fb5300>
>  >>Traceback (most recent call last): FrameLocEdit.GetThumbnail of  Object of type 'wxPanel *' at 0x4fb5300>
>  >>Traceback (most recent call last): FrameLocEdit.GetThumbnail of  Object of type 'wxPanel *' at 0x4fb5300>
>  >>Traceback (most recent call last):  File
> "C:\Python\Projects\Warehouserclient_v3\locedit.py", line 917, in
> GetThumbnail
> 
> 
> Traceback (most recent call last):
> 
>File "C:\Python\Projects\Warehouserclient_v3\locedit.py", line 917,
> in GetThumbnail
> Traceback (most recent call last):
>File "C:\Python\Projects\Warehouserclient_v3\locedit.py", line 917,
> in GetThumbnail
>File "C:\Python\Projects\Warehouserclient_v3\locedit.py", line
> 917, in GetThumbnail
>File "C:\Python\Projects\Warehouserclient_v3\locedit.py", line
> 917, in GetThumbnail
>  self.Log(traceback.format_exc())
>  self.Log(traceback.format_exc())
>  self.Log(traceback.format_exc())
> AttributeErrorself.Log(traceback.format_exc())
> AttributeErrorself.Log(traceback.format_exc())
> AttributeError: AttributeError: AttributeError:
> C:\Python\Projects\Warehouserclient_v3>
> 
> 
> And the last attributerror doesn't tell what attribute is missing.
> Probably I'll have to use a different library (pycurl, for example). But
> the error itself is getting more interesting.

This makes me think self does not have a .Log() or the traceback
import is being overridden by something else that does not have
.format_exc(). Or possibly this is in a function that does not have a 
self (maybe a typo in the function def?).

> 
> Also tried to use an invalid file (that should return with HTTP 404 not
> found) but the effect is exactly the same. Nothing is printed on stdout
> until I try to close the program (stop wxPython's mainloop). Then all
> previously threads throw an AttributeError and all of them print a stack
> trace (at the same time, lines mixed up).
> 
> I'll be experimenting with pyCurl now.


Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--

This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: urllib.urlretrieve never returns??? [SOLVED] - workaround

2012-03-20 Thread Prasad, Ramit

> > Everything works perfectly, in all modes: console, no console, started
> > directly and started in separate thread.
> >
> > So the problem with urllib must be. Maybe wxPython installs some except
> > hooks, or who knows? If somebody feels up to it, I can start narrowing
> > down the problem to the smallest possible application. But only if
> > someone knows how to debug core code because I don't. Otherwise I'll
> > just use pyCURL.
> 
> Have you tried urllib2? Have you tried a small program without using wx?
> 
> To be honest, I doubt the problem is wx or urllib as they are both fairly
> broadly used. Try to come up with an example that is as minimal as
> possible.
> 
> >>> imgurl =
> "http://www.shopzeus.hu/thumbnail.php?width=200&image=pyramid/PP0830.jpg";
> >>> urllib.urlretrieve( imgurl )
> ('c:\\[...]\\tmpkhixgt.php', )
> 
> And I have Windows 7 64 with Python 2.6.6 (32 bit) and wx installed.
>


Your program on ActiveState worked for me which tells me that it might be 
a network or machine specific problem. You are missing an import which I 
mentioned in another post. Fixing that should tell what the error you are 
getting is; you would not be getting the AttributeError without some 
other error first.

Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--
 

This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: urllib.urlretrieve never returns??? [SOLVED] - workaround

2012-03-20 Thread Prasad, Ramit
> Everything works perfectly, in all modes: console, no console, started
> directly and started in separate thread.
> 
> So the problem with urllib must be. Maybe wxPython installs some except
> hooks, or who knows? If somebody feels up to it, I can start narrowing
> down the problem to the smallest possible application. But only if
> someone knows how to debug core code because I don't. Otherwise I'll
> just use pyCURL.

Have you tried urllib2? Have you tried a small program without using wx?

To be honest, I doubt the problem is wx or urllib as they are both fairly
broadly used. Try to come up with an example that is as minimal as possible. 

>>> imgurl = 
>>> "http://www.shopzeus.hu/thumbnail.php?width=200&image=pyramid/PP0830.jpg";
>>> urllib.urlretrieve( imgurl )
('c:\\[...]\\tmpkhixgt.php', )

And I have Windows 7 64 with Python 2.6.6 (32 bit) and wx installed.

Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: urllib.urlretrieve never returns???

2012-03-20 Thread Prasad, Ramit
> > Traceback (most recent call last):
> >
> >File "C:\Python\Projects\Warehouserclient_v3\locedit.py", line 917,
> > in GetThumbnail
> > Traceback (most recent call last):
> >File "C:\Python\Projects\Warehouserclient_v3\locedit.py", line 917,
> > in GetThumbnail
> >File "C:\Python\Projects\Warehouserclient_v3\locedit.py", line
> > 917, in GetThumbnail
> >File "C:\Python\Projects\Warehouserclient_v3\locedit.py", line
> > 917, in GetThumbnail
> >  self.Log(traceback.format_exc())
> >  self.Log(traceback.format_exc())
> >  self.Log(traceback.format_exc())
> > AttributeErrorself.Log(traceback.format_exc())
> > AttributeErrorself.Log(traceback.format_exc())
> > AttributeError: AttributeError: AttributeError:
> > C:\Python\Projects\Warehouserclient_v3>
> >
> >
> > And the last attributerror doesn't tell what attribute is missing.
> > Probably I'll have to use a different library (pycurl, for example). But
> > the error itself is getting more interesting.
> 
> This makes me think self does not have a .Log() or the traceback
> import is being overridden by something else that does not have
> .format_exc(). Or possibly this is in a function that does not have a
> self (maybe a typo in the function def?).


> Here you can find the example program and the original post.
>
> http://code.activestate.com/lists/python-list/617894/

I just looked at your source file on ActiveState and noticed that
you do not import traceback. That is why you are getting the 
AttributeError. Now you should be getting a much better error 
once you import it :)



Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--


> -Original Message-
> From: python-list-bounces+ramit.prasad=jpmorgan@python.org
> [mailto:python-list-bounces+ramit.prasad=jpmorgan@python.org] On Behalf
> Of Prasad, Ramit
> Sent: Tuesday, March 20, 2012 3:52 PM
> To: python-list@python.org
> Subject: RE: urllib.urlretrieve never returns???
> 
> > Today I got a different error message printed on console (program
> > started with python.exe)
> >
> >
> >
> > Unhandled exception in thread started by  > FrameLocEdit.GetThumbnail of  > Object of type 'wxPanel *' at 0x4f85300>
> >  >>Unhandled exception in thread started by  > FrameLocEdit.GetThumbnail of  > Object of type 'wxPanel *' at 0x4f85300>
> >  >>Unhandled exception in thread started by  > FrameLocEdit.GetThumbnail of  > Object of type 'wxPanel *' at 0x4f85300>
> >  >>Unhandled exception in thread started by
> > Traceback (most recent call last):
> >
> > Traceback (most recent call last):
> >
> > Traceback (most recent call last):
> >  > of 
> >  >>  File "C:\Python\Projects\Warehouserclient_v3\locedit.py", line 917,
> > in GetThumbnail
> >File "C:\Python\Projects\Warehouserclient_v3\locedit.py", line 917,
> > in GetThumbnail
> >File "C:\Python\Projects\Warehouserclient_v3\locedit.py", line 917,
> > in GetThumbnail
> >
> > sys.excepthook is missing
> > Traceback (most recent call last):
> >
> > I have never seen a traceback like this before. I didn't install any
> > excepthook myself.
> >
> > Program is using wxPython, socket, threads, threading, PIL.
> >
> > Here is something even more strange. If I click on the button three
> > times, then absolutely nothing gets printed on stdout. However, if I
> > close the program with file/exit (actually, calling
> > wx.PySimpleApp.ExitMainLoop) then suddenly three stack traces are
> > printed on stdout, all lines mixed up:
> >
> 
> Maybe in self.Log you should add a sys.stdout.flushand
> sys.stderr.flush(). I am not very familiar with stdin/out but
> maybe it will help.
> 
> > Unhandled exception in thread started by  > FrameLocEdit.GetThumbnail of  > Object of type 'wxPanel *' at 0x4fb530
> > 0>
> >  >>Unhandled exception in thread started by Unhandled exception in
> > thread started by  >  > 0x4fb5300>
> >  >>Unhandled exception in thread started by Unhandled exception in
> > thread started by  >  > 0x4fb5300>
> >  >>Traceback (most recent call last): > FrameLocEdit.GetThumbnail of  > Object of type 'wxPanel *' at 0x4fb5300>
> >  >>Traceback (most recent call last): > FrameLocEdit.GetThumbnail of  > Object of type 'wxPanel *' at 0x4fb5300>
> >  >>Traceback (most recent call last):  File
> > "C:\Python\Projects\Warehouserclient_v3\locedit.py", line 917, in
> > GetThumbnail
> >
> >
> 
> >
> > Also tried to use an invalid file (that should return with HTTP 404 not
> > found) but the effect is exactly the same. Nothing is printed on stdout
> > until I try to close the program (stop wxPython's mainloop). Then all
> > previously threads throw an AttributeError and all of them print a stack
> > trace (at the same time, lines mixed up).
> >
> > I'll be experimenting with pyCurl now.
> 
> 
> Ramit
> 
> 
> Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
> 712 Main Street | Houston, TX 77002
> work phone: 713 - 2

Re: Fabric Engine v1.0 released under AGPL

2012-03-20 Thread Paul Doyle
Hi Colin,

Fabric supports multi-dimensional arrays, and also provides support
for dictionaries. You can read more here:
http://documentation.fabric-engine.com/latest/FabricEngine-KLProgrammingGuide.html

In terms of comparison to Numpy - I'm not familiar with that product,
but some surface level similarities/differences:

- we don't provide high-level functions for scientific computing. This
is something we're looking at now.
- both products provide methods for including existing libraries
(http://documentation.fabric-engine.com/latest/FabricEngine-
ExtensionsReference.html)
- Fabric is a high-performance framework -
http://documentation.fabric-engine.com/latest/FabricEngine-Overview.html
- we haven't benchmarked against R, MatLab etc but we run at the same
speed as multi-threaded compiled code (since that's essentially what
we're doing).

Hope that helps,

Paul

>
> It seems that sing;e dimension arrays are used in KL.  How does this
> compare with Numpy?
>
> Colin W.

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


Re: List comprehension/genexp inconsistency.

2012-03-20 Thread Ian Kelly
On Tue, Mar 20, 2012 at 3:16 PM, Dennis Lee Bieber
 wrote:
> On Tue, 20 Mar 2012 16:23:22 -0400, "J. Cliff Dyer"
>  declaimed the following in
> gmane.comp.python.general:
>
>>
>> When trying to create a class with a dual-loop generator expression in a
>> class definition, there is a strange scoping issue where the inner
>> variable is not found, (but the outer loop variable is found), while a
>> list comprehension has no problem finding both variables.
>>
>        Read http://www.python.org/dev/peps/pep-0289/ -- in particular, look
> for the word "leak"

No, this has nothing to do with the loop variable leaking.  It appears
to have to do with the fact that the variables and the generator
expression are inside a class block.  I think that it's related to the
reason that this doesn't work:

class Foo(object):
x = 42
def foo():
print(x)
foo()

In this case, x is not a local variable of foo, nor is it a global.
In order for foo to access x, it would have to be a closure -- but
Python can't make it a closure in this case, because the variable it
accesses is (or rather, will become) a class attribute, not a local
variable of a function that can be stored in a cell.  Instead, the
compiler just makes it a global reference in the hope that such a
global will actually be defined when the code is run.

For that reason, what surprises me about Cliff's example is that a
generator expression works at all in that context.  It seems to work
as long as it contains only one loop, but not if it contains two.  To
find out why, I tried disassembling one:

>>> class Foo(object):
... x = 42
... y = 12
... g = (a+b for a in range(x) for b in range(y))
...
>>> dis.dis(Foo.g.gi_code)
  4   0 LOAD_FAST0 (.0)
>>3 FOR_ITER34 (to 40)
  6 STORE_FAST   1 (a)
  9 LOAD_GLOBAL  0 (range)
 12 LOAD_GLOBAL  1 (y)
 15 CALL_FUNCTION1
 18 GET_ITER
>>   19 FOR_ITER15 (to 37)
 22 STORE_FAST   2 (b)
 25 LOAD_FAST1 (a)
 28 LOAD_FAST2 (b)
 31 BINARY_ADD
 32 YIELD_VALUE
 33 POP_TOP
 34 JUMP_ABSOLUTE   19
>>   37 JUMP_ABSOLUTE3
>>   40 LOAD_CONST   0 (None)
 43 RETURN_VALUE

So that explains it.  Notice that "x" is never actually accessed in
that disassembly; only "y" is.  It turns out that the first iterator
[range(x)] is actually created before the generator ever starts
executing, and is stored as an anonymous local variable on the
generator's stack frame -- so it's created in the class scope, not in
the generator scope.  The second iterator, however, is recreated on
every iteration of the first iterator, so it can't be pre-built in
that manner.  It does get created in the generator scope, and when
that happens it blows up because it can't find the variable, just like
the function example above.

Cheers,
Ian
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python is readable

2012-03-20 Thread Kiuhnm

On 3/19/2012 16:27, Steven D'Aprano wrote:

I believe that you are misunderstanding the descriptivist position. There
are many sentences which are never said, or perhaps only said once. Most
non-trivial spoken or written sentences are unique. That doesn't make
them wrong or erroneous because "nobody says them".

Nobody says "hesitant teapots sleep artistically".


Let's not mix syntax with semantics. "If I'm ok: I'll go." represents 
all the possible sentences where the if-clause and the then-clause are 
separated by a colon. I believe that none of those are grammatically 
correct.


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


Re: Python is readable

2012-03-20 Thread Kiuhnm

On 3/18/2012 1:57, Steven D'Aprano wrote:

On 3/16/2012 21:04, Prasad, Ramit wrote:

People spell your name Stephen, sometimes too. Thinking of changing
it? Gore Vidal's quote has panache, a valid compensation for breaking

the usual rule. How many other uses on that page are similar?


He provided common examples and reference links. Seems like a pretty
reasonable way of trying to prove a point. If you don't like reference
links, what would convince you that the point was correct? I have not
seen any counter examples or counter references on your behalf...


He's referring to this "rule":
"A colon should not precede a list unless it follows a complete
sentence; however, the colon is a style choice that some publications
allow."
http://www.grammarbook.com/punctuation/colons.asp



That is an invented prescriptivist rule and not based on English grammar
as it actually is used by native English speakers. It is *bullshit*. Even
the author of that page breaks it. Immediately following the above
prohibition, she follows it with the sentence fragment:

"Examples:"

and then a list -- exactly what she says you may not do.


I never said that rule is acceptable. I agree with you on that.


People *do* precede lists by a colon following a sentence fragment. This
is unremarkable English grammar, with only a tiny number of arse-plugged
prescriptivists finding anything to complain about it, and even they
break their own bullshit made-up so-called rule.

The vast majority of English speakers write things like:

 TO DO:
 - mow the lawn
 - wash the car
 - take kids to the zoo
 - write book on grammar

and there is nothing wrong with doing so.


That's perfectly acceptable.
Robert Kern put it very well in his post:
"don't use a colon to separate a transitive verb from its objects".

You can't say
  TO DO
  - mow the lawn
  - ...
because "TO DO mow the lawn" doesn't "flow".
But why should we break a sentence when there's no need to do so?
Why should you write
  The matrix:

  is equal to
Why the colon? Why break the flow of a sentence without reason?

I would generalize Robert Kern's rule a little:
"don't put a colon into a sentence which is fine already".

Example:
  You should
  - mow the lawn
  - do the dishes
  - walk the dog

That's perfectly fine. Commas are conveniently omitted.

As a side note, titles of movies, newspapers etc... don't follow common 
rules. Articles may be omitted, verbs may be missing, etc... They're 
just titles.


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


Re: Python is readable

2012-03-20 Thread Steven D'Aprano
On Tue, 20 Mar 2012 16:34:21 -0400, Nathan Rice wrote:

> I find help() to be mostly useless because of the clutter induced by
> double under methods.

I feel your pain, but perhaps I've just learned how to skim the output 
without being bogged down in reading every line, or perhaps because I 
mostly call it on methods rather than on the class itself, I find help() 
is absolutely invaluable and would be lost without it.



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


Re: Python is readable

2012-03-20 Thread Steven D'Aprano
On Tue, 20 Mar 2012 15:28:25 -0400, Nathan Rice wrote:

>>> This is one of my gripes with the dogmatic application of the "break
>>> it into multiple statements" mantra of Python.
>>
>> I must admit I don't recognise that one, unless you're talking about
>> "not everything needs to be a one liner".
>> ...
>> Perhaps you could give some examples (actual or contrived) of stuff
>> where "breaking it into multiple statements" is a bad thing?
> 
> One example is performing a series of transformations on a collection of
> data, with the intent of finding an element of that collection that
> satisfies a particular criterion.  If you separate out the individual
> transformations, you need to understand generators or you will waste
> space and perform many unnecessary calculations.  If you only ever do a
> single transformation with a clear conceptual meaning, you could create
> a "master transformation function," but what if you have a large number
> of potential permutations of that function?  

I'm sorry, that is far too abstract for me. Do you have a *concrete* 
example, even an trivial one?



> What if you are composing
> three or four functions, each of which is conditional on the data?  If
> you extract things from a statement and assign them somewhat arbitrary
> names, you've just traded horizontal bloat for vertical bloat (with a
> net increase in volume), while forcing a reader to scan back and forth
> to different statements to understand what is happening.

First off, vertical bloat is easier to cope with than horizontal bloat, 
at least for people used to reading left-to-right rather than vertically. 
There are few anti-patterns worse that horizontal scrolling, especially 
for text.

Secondly, the human brain can only deal with a limited number of tokens 
at any one time. It's easier to remember large numbers when they are 
broken up into chunks:

824-791-259-401 versus 824791259401

(three tokens, versus twelve)

Likewise for reading code. Chunking code into multiple lines instead of 
one long expression, and temporary variables, make things easier to 
understand, not harder.

http://en.wikipedia.org/wiki/The_Magical_Number_Seven,_Plus_or_Minus_Two

And thirdly, you're not "forcing" the reader to scan back and forth -- or 
at least if you are, then you've chosen your functions badly. Functions 
should have descriptive names and should perform a meaningful task, not 
just an arbitrary collection of code.

When you read:

x = range(3, len(sequence), 5)

you're not forced to scan back and forth between that line and the code 
for range and len to understand it, because range and len are good 
abstractions that make sensible functions.

There is a lot of badly written code in existence. Don't blame poor 
execution of design principles on the design principle itself.


[...]
> Also, because of Sphinx, it is very common in the Python community weave
> documents and code together in a way that is convenient for authors but
> irritating for readers.

I don't know about "very common". I suspect, given the general paucity of 
documentation in the average software package, it is more like "very 
rare, but memorable when it does happen".


> I personally would prefer not to have to scroll
> past 100 lines of a tutorial with examples, tests and what not in order
> to go from one function to another.

Agreed. Docstrings should use a minimal number of examples and tests. 
Tutorials and extensive tests should be extracted into external documents.


> It would be really awesome if
> everyone used links to that material in docstrings, and the default
> sphinx theme created an inline collapsible iframe that included that
> material for the HTML version.  Don't get me wrong, I adore Sphinx, the
> problem here is people who are lazy or don't know the right way to
> structure docs.

+1000


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


Re: Python is readable

2012-03-20 Thread Steve Howell
On Mar 20, 5:22 pm, Steven D'Aprano  wrote:
> On Tue, 20 Mar 2012 15:28:25 -0400, Nathan Rice wrote:
>
> > What if you are composing
> > three or four functions, each of which is conditional on the data?  If
> > you extract things from a statement and assign them somewhat arbitrary
> > names, you've just traded horizontal bloat for vertical bloat (with a
> > net increase in volume), while forcing a reader to scan back and forth
> > to different statements to understand what is happening.
>
> First off, vertical bloat is easier to cope with than horizontal bloat,
> at least for people used to reading left-to-right rather than vertically.
> There are few anti-patterns worse that horizontal scrolling, especially
> for text.
>

I agree with Steven that horizontal bloat hurts readability more than
vertical bloat.  Of course, it's a subjective thing, and I get the
fact that the remedy to horizontal bloat often means more volume of
code overalls (i.e. introducing locals).

My main problem with horizontal bloat is that you often have to read
inside-outside or right to left:

verb3(verb2(verb1(noun)))
verb2(verb1(noun, adverb1), adverb2)

The vertical versions tend to be more verbose, but entirely
sequential:

noun1 = verb1(noun)
noun2 = verb2(noun1)
verb3(noun2)

and

noun1 = verb1(noun, adverb1)
verb2(noun1, adverb2)


There is a bit of an inflection point when the number of lines in the
"local" code exceeds the vertical real estate of your monitor.  I feel
like vertical real estate is still mostly constrained by the way we
build our monitors and our editors, and it's not so much a "human
brain" limitation.  With horizontally stretched code, I always feel
like my brain is the bottleneck.

The one place where I don't mind the horizontal approach is when code
is truly laid out in the order of execution:

  noun.verb1(adverb1).verb2(adverb2).verb3(adverb3).verb4(adverb4)

Even then, I'd prefer it to read vertically.  Also, while the above
idiom puts the verbs in the right order, it is still backward to me to
say "noun.verb."  You don't noun a verb.  You verb a noun.



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


Re: Python is readable

2012-03-20 Thread Ben Finney
Steve Howell  writes:

> Also, while the above idiom puts the verbs in the right order, it is
> still backward to me to say "noun.verb." You don't noun a verb. You
> verb a noun.

When calling a method, the program object is the grammatical subject.
You don't verb the noun, and you don't noun a verb. The noun verbs.

-- 
 \“Last year I went fishing with Salvador Dali. He was using a |
  `\  dotted line. He caught every other fish.” —Steven Wright |
_o__)  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python is readable

2012-03-20 Thread Steve Howell
On Mar 20, 7:28 pm, Ben Finney  wrote:
> Steve Howell  writes:
> > Also, while the above idiom puts the verbs in the right order, it is
> > still backward to me to say "noun.verb." You don't noun a verb. You
> > verb a noun.
>
> When calling a method, the program object is the grammatical subject.
> You don't verb the noun, and you don't noun a verb. The noun verbs.
>

I think it's a matter of perspective, so there's no right answer, but
I always think of the program object as also being the grammatical
object, with the implied subject/actor being Python itself.  For
example, consider this code:

  stack.push(item)

It's not the stack that's pushing.  It's the stack being pushed on
to.  So "stack" is the direct object, and "item" is the indirect
object.  When you say "stack.push(item)", I think of it as being that
Python pushes an item on to the stack.  I suppose you would argue that
the stack pushes item on to itself?  And even then, isn't it still the
grammatical object in the "itself" case?

Also, don't they call those thingies "object" for a reason? ;)



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


Re: List comprehension/genexp inconsistency.

2012-03-20 Thread Steve Howell
On Mar 20, 3:50 pm, Ian Kelly  wrote:
> On Tue, Mar 20, 2012 at 3:16 PM, Dennis Lee Bieber
>
>  wrote:
> > On Tue, 20 Mar 2012 16:23:22 -0400, "J. Cliff Dyer"
> >  declaimed the following in
> > gmane.comp.python.general:
>
> >> When trying to create a class with a dual-loop generator expression in a
> >> class definition, there is a strange scoping issue where the inner
> >> variable is not found, (but the outer loop variable is found), while a
> >> list comprehension has no problem finding both variables.
>
> >        Readhttp://www.python.org/dev/peps/pep-0289/-- in particular, look
> > for the word "leak"
>
> No, this has nothing to do with the loop variable leaking.  It appears
> to have to do with the fact that the variables and the generator
> expression are inside a class block.

Interesting.

Just for completeness, the code does seem to work fine when you take
it out of the class:

  Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49)
  [GCC 4.2.1 (Apple Inc. build 5646)] on darwin
  Type "help", "copyright", "credits" or "license" for more
information.
  >>> foo, bar = 4, 4
  >>> g = (((x, y), x+y) for x in range(foo) for y in range(bar))
  >>> dict(g)
  {(0, 1): 1, (1, 2): 3, (3, 2): 5, (0, 0): 0, (3, 3): 6, (3, 0): 3,
(3, 1): 4, (2, 1): 3, (0, 2): 2, (2, 0): 2, (1, 3): 4, (2, 3): 5, (2,
2): 4, (1, 0): 1, (0, 3): 3, (1, 1): 2}
  >>> import dis
  >>> dis.dis(g.gi_code)
1   0 SETUP_LOOP  57 (to 60)
3 LOAD_FAST0 (.0)
  >>6 FOR_ITER50 (to 59)
9 STORE_FAST   1 (x)
   12 SETUP_LOOP  41 (to 56)
   15 LOAD_GLOBAL  0 (range)
   18 LOAD_GLOBAL  1 (bar)
   21 CALL_FUNCTION1
   24 GET_ITER
  >>   25 FOR_ITER27 (to 55)
   28 STORE_FAST   2 (y)
   31 LOAD_FAST1 (x)
   34 LOAD_FAST2 (y)
   37 BUILD_TUPLE  2
   40 LOAD_FAST1 (x)
   43 LOAD_FAST2 (y)
   46 BINARY_ADD
   47 BUILD_TUPLE  2
   50 YIELD_VALUE
   51 POP_TOP
   52 JUMP_ABSOLUTE   25
  >>   55 POP_BLOCK
  >>   56 JUMP_ABSOLUTE6
  >>   59 POP_BLOCK
  >>   60 LOAD_CONST   0 (None)
   63 RETURN_VALUE
-- 
http://mail.python.org/mailman/listinfo/python-list


setup.py for an extension

2012-03-20 Thread Paulo da Silva
Hi all.

I have a python extension (bindings for a C lib - no swig) and I would
like to write a setup.py to build a source distribution pack.

The extension consists of 3 files:
foo.h
foo.c
foo.py
that are placed in a eclipse directory
/home//ECLIPSE/workspace/ext/src

foo.h+foo.c are to be compiled into _foo.so shared lib. _foo.so is
itself a module only called from foo.py.

The dir I wrote the setup.py is any arbitrary dir. I don't want to put
packaging stuff into the eclipse source.

I read the docs but have no idea on how to do this. Some tentatives I
did completely failed.

Any help?

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


Re: Python is readable

2012-03-20 Thread Chris Angelico
On Wed, Mar 21, 2012 at 1:44 PM, Steve Howell  wrote:
> I think it's a matter of perspective, so there's no right answer, but
> I always think of the program object as also being the grammatical
> object, with the implied subject/actor being Python itself.  For
> example, consider this code:
>
>  stack.push(item)
>
> It's not the stack that's pushing.  It's the stack being pushed on
> to.

In code, though, the push() method is defined in and on the stack
object (or more likely, on the class that instantiated it, but near
enough). So the stack is being asked to push something onto itself. It
is still viably the subject. Yes, the implied subject could be the
language interpreter; but it could just as easily be the CPU, or those
friendly nanobots, or that guy moving the rocks in XKCD 505. But in
the abstraction of the line of code, you don't care how CPython goes
about loading globals, calling bound methods, and incrementing object
reference counts - you just care that the stack is pushing this item
onto itself.

Some method names are definitely written as though their primary
argument is the object, not the subject. Either option works. Do you
think about code as the subject+verb and a data structure as the
object, or the object as the subject and the method as the verb?
Fundamentally no difference.

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


Re: Python is readable

2012-03-20 Thread Nathan Rice
>> One example is performing a series of transformations on a collection of
>> data, with the intent of finding an element of that collection that
>> satisfies a particular criterion.  If you separate out the individual
>> transformations, you need to understand generators or you will waste
>> space and perform many unnecessary calculations.  If you only ever do a
>> single transformation with a clear conceptual meaning, you could create
>> a "master transformation function," but what if you have a large number
>> of potential permutations of that function?
>
> I'm sorry, that is far too abstract for me. Do you have a *concrete*
> example, even an trivial one?

How about a hypothetical log analyzer that parses a log file that is
aggregated from multiple event sources with disparate record
structures.  You will need to perform a series of transformations on
the data to convert record elements from text to specific formats, and
your function for identifying "alarm" records is also dependent on
record structure (and possibly system state, imagining an intrusion
detection system).  Of course you could go through a lot of trouble to
dispatch and detect alarms over 6-7 statements, however given the
description "for each log record you receive, convert text elements to
native data types based on the value of the first three fields of the
record, then trigger an alert if that record meets defined
requirements" and assuming you have maps from record values to
conversion functions for record elements, and a map from record types
to alert criteria functions for record types already constructed, it
seems like a one liner to me.


>> What if you are composing
>> three or four functions, each of which is conditional on the data?  If
>> you extract things from a statement and assign them somewhat arbitrary
>> names, you've just traded horizontal bloat for vertical bloat (with a
>> net increase in volume), while forcing a reader to scan back and forth
>> to different statements to understand what is happening.
>
> First off, vertical bloat is easier to cope with than horizontal bloat,
> at least for people used to reading left-to-right rather than vertically.
> There are few anti-patterns worse that horizontal scrolling, especially
> for text.

I agree that if a line goes into horizontal scroll buffer, you have a
problem.  Of course, I often rail on parenthesized
function-taking-arguments expression structure for the fact that it
forces you to read inside out and right to left, and I'd prefer not to
conflate the two issues here.  My assertion is that given an
expression structure that reads naturally regardless, horizontal bloat
is better than larger vertical bloat, in particular when the vertical
bloat does not fall along clean semantic boundaries.


> Secondly, the human brain can only deal with a limited number of tokens
> at any one time. It's easier to remember large numbers when they are
> broken up into chunks:
>
> 824-791-259-401 versus 824791259401
>
> (three tokens, versus twelve)
>
> Likewise for reading code. Chunking code into multiple lines instead of
> one long expression, and temporary variables, make things easier to
> understand, not harder.

This is true, when the tokens are an abstraction.  I read some of the
research on chunking, basically it came down to people being able to
remember multiple numbers efficiently in an auditory fashion using
phonemes.  Words versus random letter combinations have the same
effect, only with visual images (which is why I think Paul Graham is
full of shit with regards to his "shorter is better than descriptive"
mantra in old essays).  This doesn't really apply if storing the
elements in batches doesn't provide a more efficient representation.
Of course, if you can get your statements to read like sensible
English sentences, there is definitely a reduction in cognitive load.


> And thirdly, you're not "forcing" the reader to scan back and forth -- or
> at least if you are, then you've chosen your functions badly. Functions
> should have descriptive names and should perform a meaningful task, not
> just an arbitrary collection of code.

This is why I quoted Einstein.  I support breaking compound logical
statements down to simple statements, then combining those simple
statements.  The problem arises when your compound statement still
looks like "A B C D E F G H I J K L M N", and portions of that
compound statement don't have a lot of meaning outside the larger
statement.  You could say X = A B C D E, Y = F G H I J, Z = K L M N,
then say X Y Z, but now you've created bloat and forced the reader to
backtrack.

>
> When you read:
>
> x = range(3, len(sequence), 5)
>
> you're not forced to scan back and forth between that line and the code
> for range and len to understand it, because range and len are good
> abstractions that make sensible functions.
>
> There is a lot of badly written code in existence. Don't blame poor
> execution of design principles on the design princip

Re: Python is readable

2012-03-20 Thread Steve Howell
On Mar 20, 9:16 pm, Chris Angelico  wrote:
> On Wed, Mar 21, 2012 at 1:44 PM, Steve Howell  wrote:
> > I think it's a matter of perspective, so there's no right answer, but
> > I always think of the program object as also being the grammatical
> > object, with the implied subject/actor being Python itself.  For
> > example, consider this code:
>
> >  stack.push(item)
>
> > It's not the stack that's pushing.  It's the stack being pushed on
> > to.
>
> In code, though, the push() method is defined in and on the stack
> object (or more likely, on the class that instantiated it, but near
> enough). [...]

The interpretation that the "subject" is the Stack class itself leads
to this coding style:

   Stack.push(stack, item)

The above code takes duck-typing to an extreme--you don't have to
assume that "stack" was instantiated from "Stack" in order to apply
"Stack.push" to "stack" (where "Stack" acts a the subject and "stack"
acts as a grammatical direct object).

Of course, 99% of the time, we want some sugar that makes Stack be the
implicit subject (given that "stack" was instantiated from "Stack"):

  stack.push(item) # push comes from Stack via stack


> Yes, the implied subject could be the
> language interpreter; but it could just as easily be the CPU, or those
> friendly nanobots, or that guy moving the rocks in XKCD 505. But in
> the abstraction of the line of code, you don't care how CPython goes
> about loading globals, calling bound methods, and incrementing object
> reference counts - you just care that the stack is pushing this item
> onto itself.

Sure, that's all good, but, colloquially, I bet you've probably said
at one time in your life, "And, here, we are pushing the item on to
the stack."  The subject is vague ("we"), but there is no assumption
of friendly nanobots (or vigorous hamsters, or CPUs, or any specific
mechanisms), just the idea that the stack is the object, not the
subject.

> Some method names are definitely written as though their primary
> argument is the object, not the subject. Either option works. Do you
> think about code as the subject+verb and a data structure as the
> object, or the object as the subject and the method as the verb?
> Fundamentally no difference.

At a certain fundamental level, sure, of course it's all just data
structure and code, so we shouldn't be quibbling about syntax or
grammar, and we certainly shouldn't be throwing around strained
analogies to natural languages.

But in this context, we are musing about grammar, so I would propose
this question:

   What's more important, the object or the method?

IMHO the method is usually more interesting than the object itself.
Of course, the "stack" itself is important, but on any given line of
code, the action is more interesting, so I'd want to lead with "push"
or "pop."

Verb-first gets a bad rap, because people tend to associate verb-first
syntax with early, primitive imperative/functional languages that had
no OO syntax.

Assembly tends to be very imperative:

  MOV AX, BX

So saying "push(stack, item)" or "push(item, stack)" seems very
unsophisticated, almost assembly-like in syntax, albeit at a higher
level conceptually than assembly.





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


Re: Python is readable

2012-03-20 Thread Chris Angelico
On Wed, Mar 21, 2012 at 3:58 PM, Steve Howell  wrote:
> So saying "push(stack, item)" or "push(item, stack)" seems very
> unsophisticated, almost assembly-like in syntax, albeit at a higher
> level conceptually than assembly.

Perhaps it does, but "push(stack, item)" and "stack.push(item)" are so
close to identical as makes no odds (in a number of languages, the
latter is just syntactic sugar for something like the former) - yet
they "read" quite differently, one with verb first, one with noun
first.

Code doesn't follow the same grammar as English prose, and forcing it
to usually makes it sound odd. Reader.can_comprehend(code) is True.

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


Re: Python is readable

2012-03-20 Thread Steve Howell
On Mar 20, 10:40 pm, Chris Angelico  wrote:
> On Wed, Mar 21, 2012 at 3:58 PM, Steve Howell  wrote:
> > So saying "push(stack, item)" or "push(item, stack)" seems very
> > unsophisticated, almost assembly-like in syntax, albeit at a higher
> > level conceptually than assembly.
>
> Perhaps it does, but "push(stack, item)" and "stack.push(item)" are so
> close to identical as makes no odds (in a number of languages, the
> latter is just syntactic sugar for something like the former) - yet
> they "read" quite differently, one with verb first, one with noun
> first.
>

On the one hand, you say that "push(stack, item)" reads quite
differently from "stack.push(item)".

On the other hand, you say they are "so close to identical as makes no
odds."

I'm trying to make sense of that.  Are you saying that the way the two
idioms read makes no odds, despite reading quite differently?

> Code doesn't follow the same grammar as English prose, and forcing it
> to usually makes it sound odd. Reader.can_comprehend(code) is True.
>

Code shouldn't necessarily follow the example of English prose, but it
seems that English has had some influence:

 1  push(stack, item) # Push on the stack the item
 2  push(item, stack) # Push the item on the stack
 3  stack.push(item)  # On the stack, push the item
 4  stack item push   # On the stack, take the item and push it
 5  item stack push   # Take the item and on the stack, push the
former.
 6  item push stack   # Take the item; push it on the stack.

The first three ways are the most common ways of arranging the grammar
in mainstream programming languages, and they are also the three most
natural ways in English (no pronouns required).

#1/2 are imperative.  #3 is OO.

#4 and #5 are sort of Forth-like, maybe?  #6 is just downright
strange.







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