ANN: RPQueue 0.22

2013-10-10 Thread Josiah Carlson
Hello everyone,

For those of you who didn't know, if you are interested in a Redis-backed
time and/or fifo-queue with priorities, retries, etc., to be used with
Python, one exists and is mature: it's called RPQueue, and it seeks to
simplify your life of task execution. The recent changelog entries are
below my signature.

The package has been around for two years next month, and does exactly what
you expect it to do - no more, no less.

You can find the package at:
https://github.com/josiahcarlson/rpqueue/
https://pypi.python.org/pypi/rpqueue

Please CC me on any replies if you have any questions or comments.

Thank you,
 - Josiah

#--- 0.22

[fixed] setup.py-based installations. Ran into the bug myself :/
#--- 0.21
 
[changed] where available, rpqueue will now use Lua to move delayed tasks
from
the time-based priority queue to fifo queues. This should reduce
overhead
in all cases, and should drastically improve performance for those that
use large numbers of delayed tasks.
[changed] the version number is now PEP 386/440 compliant.
[added] this changelog that will document updates/changes/improvements in an
easily referenced location.
[fixed] thanks to https://github.com/dmaust , rpqueue added a missing
'redis'
requirement.
-- 
https://mail.python.org/mailman/listinfo/python-announce-list

Support the Python Software Foundation:
http://www.python.org/psf/donations/


Re: Cookie gets changed when hit comes from a referrer

2013-10-10 Thread Ben Finney
rusi rustompm...@gmail.com writes:

 On Thursday, October 10, 2013 6:40:19 AM UTC+5:30, Steven D'Aprano wrote:
  If *anyone else* asked for help on these sorts of network and
  browser questions, we'd give them more constructive pointers than
  just google it.

Someone who has so consistently demonstrated their unwillingness to
learn, and their persistent arrogance in flooding this forum with
demands for assistance, is quite deserving of curt “go and find out for
yourself, leave this forum alone” responses.

We are a, and should remain, an open and tolerant community. But the
persistently disrespectful actions of Nikos threaten the forum's value
and usefulness for everyone else. There are limits to how much
disruption and obstinacy this community should tolerate from a given
individual.

 https://mail.python.org/pipermail/python-list/2013-October/657221.html
 https://mail.python.org/pipermail/python-list/2013-October/657034.html

Both of which got quite helpful responses: a brief, polite referral to
seek a specific discussion forum where their requests would be on topic.

-- 
 \ “If nature has made any one thing less susceptible than all |
  `\others of exclusive property, it is the action of the thinking |
_o__)  power called an idea” —Thomas Jefferson |
Ben Finney

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


Re: Python's and and Pythons or

2013-10-10 Thread Peter Cacioppi
On Wednesday, October 9, 2013 4:54:03 PM UTC-7, Peter Cacioppi wrote:
 I really like the logic that Pythons or is not only short-circuit but 
 non-typed.
 
 
 
 So I can say
 
 
 
 y = override or default
 
 
 
 and y won't necc be True or False. If override boolean evaluates to True 
 (which, for most classes, means not None) than y will be equal to override. 
 Otherwise it will be equal to default.
 
 
 
 I have two questions
 
 -- Is there a handy name for this type of conditional (something as catchy 
 as short circuit or)
 
 
 
 and 
 
 
 
 -- Is there a common idiom for taking advantage of the similar behavior of 
 and. The override or default just makes me grin every time I use it.
 
 
 
 Thanks

ok, since someone asked, I suggest we call the return it's arguments behavior 
echo-argument.

That is to say, the reason we can write 

y = override or default 

is because Python implements echo-argument or. That is to say, or doesn't 
necc return True or False, or returns the first truthy argument it 
encounters. 

and behaves similarly, in that it returns the first falsey argument it 
encounters.

I'm trying to think of a good example usage of echo-argument and. Maybe 
something like

possible = foo and foo.allowsit()
if (possible is None) :
   print foo not provided
if (possible is False) :
   print foo doesn't allow it

A bit awkward, echo-argument or is more naturally useful to me then 
echo-argument and.



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


Re: I am never going to complain about Python again

2013-10-10 Thread Christian Gollwitzer

Am 10.10.13 06:36, schrieb Steven D'Aprano:

Just came across this little Javascript gem:

,,, == Array((null,'cool',false,NaN,4));

= evaluates as true

http://wtfjs.com/2011/02/11/all-your-commas-are-belong-to-Array

I swear, I am never going to complain about Python again.


More of this fun for polyglott programmers:
https://www.destroyallsoftware.com/talks/wat

Christian

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


Re: Python's and and Pythons or

2013-10-10 Thread Chris Angelico
On Thu, Oct 10, 2013 at 5:12 PM, Peter Cacioppi
peter.cacio...@gmail.com wrote:
 I'm trying to think of a good example usage of echo-argument and. Maybe 
 something like

 possible = foo and foo.allowsit()
 if (possible is None) :
print foo not provided
 if (possible is False) :
print foo doesn't allow it

 A bit awkward, echo-argument or is more naturally useful to me then 
 echo-argument and.

first_element = some_list[0]# Oops, may crash

try:
first_element = some_list[0]
except IndexError:
firstelement = None   # A bit verbose

first_element = some_list and some_list[0]

# or if you want a zero instead of an empty list:
first_element = len(some_list) and some_list[0]


Also, consider the case where you have a function, or None:

result = func(*args,**kwargs)   # NoneType is not callable

result = func and func(*args,**kwargs)

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


Re: Python's and and Pythons or

2013-10-10 Thread Peter Cacioppi
On Wednesday, October 9, 2013 4:54:03 PM UTC-7, Peter Cacioppi wrote:
 I really like the logic that Pythons or is not only short-circuit but 
 non-typed.
 
 
 
 So I can say
 
 
 
 y = override or default
 
 
 
 and y won't necc be True or False. If override boolean evaluates to True 
 (which, for most classes, means not None) than y will be equal to override. 
 Otherwise it will be equal to default.
 
 
 
 I have two questions
 
 -- Is there a handy name for this type of conditional (something as catchy 
 as short circuit or)
 
 
 
 and 
 
 
 
 -- Is there a common idiom for taking advantage of the similar behavior of 
 and. The override or default just makes me grin every time I use it.
 
 
 
 Thanks

So you can wrap it all up in one big example

y = (overrideprovider and overrideprovdider() ) or default

echo-argument and/or is a beautiful thing

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


Re: Python's and and Pythons or

2013-10-10 Thread Ethan Furman

On 10/09/2013 11:12 PM, Peter Cacioppi wrote:


I'm trying to think of a good example usage of echo-argument and. Maybe 
something like

possible = foo and foo.allowsit()
if (possible is None) :
print foo not provided
if (possible is False) :
print foo doesn't allow it

A bit awkward, echo-argument or is more naturally useful to me then 
echo-argument and.


It's used as a guard:

if some_list and some_list[0] == something_or_other:
 do some_work()

Without the 'some_list and' portion when some_list was either empty or, say, None, the some_list[0] would fail with an 
error (IndexError, TypeError, etc.).


--
~Ethan~
--
https://mail.python.org/mailman/listinfo/python-list


Re: Cookie gets changed when hit comes from a referrer

2013-10-10 Thread Steven D'Aprano
On Wed, 09 Oct 2013 22:36:54 -0700, rusi wrote:

 On Thursday, October 10, 2013 6:40:19 AM UTC+5:30, Steven D'Aprano
 wrote:
 
 I have no objection to encouraging people to read the fine manual, and
 I don't intend to be Nikos' (or anyone else's) unpaid full-time help
 desk and troubleshooter. But I do think it is simply unfair to treat
 him more harshly than we would others in the same position. If *anyone
 else* asked for help on these sorts of network and browser questions,
 we'd give them more constructive pointers than just google it.
 
 https://mail.python.org/pipermail/python-list/2013-October/657221.html

That's a good example of exactly the sort of thing I'm talking about. 
Joel responded with have you checked the pysvn mailing list and gave a 
URL to that list. That is a good, helpful response, given that we can't 
be expected to know everything about every arbitrary package that might 
use Python.


 https://mail.python.org/pipermail/python-list/2013-October/657034.html

And even this got a response suggesting the poster look for an nginx 
mailing list, which while less helpful than it could have been, was still 
a concrete, helpful response: not RTFM, or just google it, but 
that's a problem with nginx, you need an nginx forum, not a Python one.



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


Re: super in Python 3 and variadic arguments

2013-10-10 Thread Marco Buttu

On 10/09/2013 06:47 PM, Ned Batchelder wrote:


 class B(A):
... def bfoo(*args):
... super().afoo(*args[1:])
...
 B().bfoo(1, 2, 3)
Traceback (most recent call last):
  File stdin, line 1, in module
  File stdin, line 3, in bfoo
RuntimeError: super(): no arguments

How come?


The no-args super() call inspects the calling environment to determine
the class and self.  self is the first local name stored in
frame.f_code.co_localsplus, but *args doesn't put args into that entry
of the code object


But is it a bug or the behavior we want? The first (implicit) argument 
is stored as expected as the first one in the args tuple, and the args 
tuple is inserted as expected in frame.f_locals:


 import inspect
 class B(A):
... def bfoo(*args):
... frame = inspect.currentframe()
... for obj, value in frame.f_locals.items():
... print(obj, value, sep=' -- ')
... # super().afoo(*args[1:])
...
 B().bfoo(1, 2, 3)
args -- (__main__.B object at 0x7f28c960a590, 1, 2, 3)
frame -- frame object at 0x7f28cad4b240

So, why does not super use it?

--
Marco Buttu
--
https://mail.python.org/mailman/listinfo/python-list


Re: Cookie gets changed when hit comes from a referrer

2013-10-10 Thread Steven D'Aprano
On Thu, 10 Oct 2013 17:01:25 +1100, Ben Finney wrote:

 There are limits to how much disruption and obstinacy this community
 should tolerate from a given individual.

I think we are in violent agreement :-)

It's possible we disagree about where to draw the line, and whether 
persistent cluelessness counts as wilful disruption, but otherwise I'm 
not disagreeing with you.



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


Re: Python's and and Pythons or

2013-10-10 Thread Terry Reedy

On 10/10/2013 2:45 AM, Chris Angelico wrote:

On Thu, Oct 10, 2013 at 5:12 PM, Peter Cacioppi
peter.cacio...@gmail.com wrote:

I'm trying to think of a good example usage of echo-argument and. Maybe 
something like



A bit awkward, echo-argument or is more naturally useful to me then 
echo-argument and.


first_element = some_list[0]# Oops, may crash


some_list[0:1] always works, and sometimes is usable, but you still 
cannot index the slice.



try:
 first_element = some_list[0]
except IndexError:
 firstelement = None   # A bit verbose

first_element = some_list and some_list[0]

# or if you want a zero instead of an empty list:
first_element = len(some_list) and some_list[0]


Also, consider the case where you have a function, or None:

result = func(*args,**kwargs)   # NoneType is not callable

result = func and func(*args,**kwargs)


y = x and 1/x
One just has to remember that y==0 effectively means y==+-infinity ;-).

--
Terry Jan Reedy

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


Learning about HTTP [was: Cookie gets changed when hit comes from a referrer]

2013-10-10 Thread Tim Golden
On 10/10/2013 00:48, Steven D'Aprano wrote:
 So, for the benefit of anyone, not just Nikos, who wants to learn about 
 how browsers connect to web sites and how to run a web server, does 
 anyone have any recommendation for tutorials, mailing lists, web forums 
 or books which are suitable? Preferably things you have used yourself, 
 and can personally vouch for being useful.

Not that it's a free resource, but I found this O'Reilly pocket guide
useful as a starter. It's not too expensive either.

  http://shop.oreilly.com/product/9781565928626.do

I bought it for my Dad who's a retired civil engineer now happily
engaged in setting up small websites for Parish groups and the like.
Exactly the kind of thing where he can experiment with a bit of
wackiness without people minding too much if things go wrong... (And he
does have a test rig at home).

As clearly evidenced by the wide range of reactions one sees in answer
to What's the best book...? questions here, different people have
wildly different learning styles. For some, going to the RFCs is
*exactly* what they find interesting  illuminating. For others, a
step-by-step with screenshots is more the thing, etc.

TJG
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python's and and Pythons or

2013-10-10 Thread Chris Angelico
On Thu, Oct 10, 2013 at 6:43 PM, Terry Reedy tjre...@udel.edu wrote:
 y = x and 1/x
 One just has to remember that y==0 effectively means y==+-infinity ;-).

Good example. Extremely appropriate to situations where you're showing
a set of figures and their average:

Foo   1
Bar   3
Quux  7
Asdf  9
= 5

Let the average show as zero if there are none, it won't hurt:

print(=,count and total/count)

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


Re: I am never going to complain about Python again

2013-10-10 Thread Frank Millman

Steven D'Aprano st...@pearwood.info wrote in message 
news:52562ee3$0$2931$c3e8da3$76491...@news.astraweb.com...
 Just came across this little Javascript gem:

 ,,, == Array((null,'cool',false,NaN,4));

 = evaluates as true

 http://wtfjs.com/2011/02/11/all-your-commas-are-belong-to-Array

 I swear, I am never going to complain about Python again.


I am sure you know this, but for the record, Javascript has two equality 
operators, '==' and '==='.

The double form attempts to coerce the left and right sides to the same 
type, the triple form does not.

'1' == 1 returns true

'1' === 1 returns false

The moment I discovered this, I changed all my operators from the double 
form to the triple form. Now I no longer have surprises of this nature.

Frank Millman



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


Re: Cookie gets changed when hit comes from a referrer

2013-10-10 Thread Antoon Pardon
Op 10-10-13 03:10, Steven D'Aprano schreef:
 On Thu, 10 Oct 2013 00:31:06 +, Denis McMahon wrote:

 If Nikos can't even figure out the right queries to feed into a search
 engine to get started on such matters as looking at the cookie jar in a
 browser or enabling cookie logging on a server, then he probably
 shouldn't be trying to code at this level.
 
 Nikos Nikos Nikos... and what about me? If I asked you for a few pointers 
 about a good place to learn about running a web site, would you tell me 
 to fuck off too? I wonder what you are doing here, if you are so 
 unwilling to share your hard-earned knowledge with others as you seem in 
 this post. This attitude is not the Denis McMahon I'm used to.

A year ago no. Now yes! Because you are behaving like an enabler. Nikos
is the prime cause of the disruptions here and you are asking we should
treat him as if he is a well behaving regular that comes with the
occasional off topic question.

You seem unable or unwilling to draw a line in how far we should go in
enduring Nikos's anti-social behaviour. You seem unable to grasp that
how we treat people depends on their behaviour in the past.

 Honestly, your response seems to me to be just a more verbose way of 
 saying RTFM n00b, and about as helpful. Speaking for myself, I don't 
 want this forum to be the sort of place where that is considered an 
 appropriate response, no matter how politely the dismissal is dressed up.

But you are cooperating very hard in producing just that. By ignoring
the reasons people react harshly to Nikos and pretending such a
reaction is a reflection on how they would treat newbies in general you
are being extremely unfair.

 I'm here to help people, and yes, that even means Nikos. To give back to 
 the community that helped me (and continues to help me). In my opinion, 
 if we're going to tell people to RTFM the least we can do is point them 
 at the right manual, and not just dismiss them by telling them to google 
 it. I don't think that's too much to ask.

And how far are you willing to go with this help? Right now you are
helping Nikos in his anti-social behaviour. Is that your idea of giving
back to the community? Annoying a significant part of it in your quest
to help people?

 (On the other hand, it's okay to say I don't know of any good forums for 
 learning this stuff. Sorry mate, I can't help.)
 
 I have no objection to encouraging people to read the fine manual, and I 
 don't intend to be Nikos' (or anyone else's) unpaid full-time help desk 
 and troubleshooter. But I do think it is simply unfair to treat him more 
 harshly than we would others in the same position. If *anyone else* asked 
 for help on these sorts of network and browser questions, we'd give them 
 more constructive pointers than just google it.

We don't. The history peope have on this forum is part of the position
they are in. If someone has abused the hospitality of an environment,
then there is nothing wrong if that environment starts reacting hostile.
That is the *situation* Nikos is in now. So stop pretending any regular
asking an occasional off topic question would be the same situation.

-- 
Antoon Pardon
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: super in Python 3 and variadic arguments

2013-10-10 Thread Ned Batchelder

On 10/10/13 3:22 AM, Marco Buttu wrote:

On 10/09/2013 06:47 PM, Ned Batchelder wrote:


 class B(A):
... def bfoo(*args):
... super().afoo(*args[1:])
...
 B().bfoo(1, 2, 3)
Traceback (most recent call last):
  File stdin, line 1, in module
  File stdin, line 3, in bfoo
RuntimeError: super(): no arguments

How come?


The no-args super() call inspects the calling environment to determine
the class and self.  self is the first local name stored in
frame.f_code.co_localsplus, but *args doesn't put args into that entry
of the code object


But is it a bug or the behavior we want? The first (implicit) argument 
is stored as expected as the first one in the args tuple, and the args 
tuple is inserted as expected in frame.f_locals:


 import inspect
 class B(A):
... def bfoo(*args):
... frame = inspect.currentframe()
... for obj, value in frame.f_locals.items():
... print(obj, value, sep=' -- ')
... # super().afoo(*args[1:])
...
 B().bfoo(1, 2, 3)
args -- (__main__.B object at 0x7f28c960a590, 1, 2, 3)
frame -- frame object at 0x7f28cad4b240

So, why does not super use it?



I haven't seen the discussion that decided the behavior of super(), but 
I'd guess that if you reported this as a bug, it would be closed as 
wontfix, because: 1) the use case you describe isn't something people 
actually write, 2) it would add to the complexity of super() to support 
it, and 3) there's a simple way to write your code that does work:


class B(A):
def bfoo(self, *args):
super().afoo(*args)

(though it's a bit odd to call afoo from bfoo.)

Python has never claimed the kind of purity that makes everything work 
in a totally simple consistent way.  super() with no args is a kind of 
hack to begin with.  It involves a special case in the compiler (so that 
using the name super as a function call will act as if you had 
accessed the name __class__ so that super can find it later), and 
inspecting the stack frame during execution.


It's an interesting case of the Zen of Python.  It violates one 
(explicit is better than implicit), but only because of another one 
(practicality beats purity).  super(MyClass, self) in Python 2 is the 
kind of brain-bender that so many people get wrong at first, that it's 
helped plenty of people to do the arguments implicitly, even if there 
are oddball edge cases that it doesn't seem to handle properly.


--Ned.
--
https://mail.python.org/mailman/listinfo/python-list


Re: I am never going to complain about Python again

2013-10-10 Thread MRAB

On 10/10/2013 09:23, Frank Millman wrote:


Steven D'Aprano st...@pearwood.info wrote in message
news:52562ee3$0$2931$c3e8da3$76491...@news.astraweb.com...

Just came across this little Javascript gem:

,,, == Array((null,'cool',false,NaN,4));

= evaluates as true

http://wtfjs.com/2011/02/11/all-your-commas-are-belong-to-Array

I swear, I am never going to complain about Python again.



I am sure you know this, but for the record, Javascript has two equality
operators, '==' and '==='.

The double form attempts to coerce the left and right sides to the same
type, the triple form does not.


Re ==, this page:

http://php.net/manual/en/language.operators.comparison.php

states:

If you compare a number with a string or the *comparison involves
numerical strings*, then each string is converted to a number and the
comparison performed numerically. (emphasis added)

So they get coerced to numbers if they _look_ like numbers!


'1' == 1 returns true

'1' === 1 returns false

The moment I discovered this, I changed all my operators from the double
form to the triple form. Now I no longer have surprises of this nature.



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


Re: I am never going to complain about Python again

2013-10-10 Thread Tim Chase
On 2013-10-10 12:10, MRAB wrote:
 Re ==, this page:
 
  http://php.net/manual/en/language.operators.comparison.php
 
 states:
 
 If you compare a number with a string or the *comparison involves
 numerical strings*, then each string is converted to a number and
 the comparison performed numerically. (emphasis added)
 
 So they get coerced to numbers if they _look_ like numbers!

BEDEVERE: How do you know she is a number?

VILLAGER1: She looks like one!

CROWD: Right! Yeah! Yeah!

BEDEVERE: Bring her forward.

STRING: I'm not a number. I'm not a number.

BEDEVERE: Uh, but you are dressed as one.

STRING: They dressed me up like this. 

Tenuously-trying-to-keep-python-related'ly yours,

-tkc
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: I am never going to complain about Python again

2013-10-10 Thread Frank Millman

MRAB pyt...@mrabarnett.plus.com wrote in message 
news:52568b30.8040...@mrabarnett.plus.com...
 On 10/10/2013 09:23, Frank Millman wrote:

 Steven D'Aprano st...@pearwood.info wrote in message
 news:52562ee3$0$2931$c3e8da3$76491...@news.astraweb.com...
 Just came across this little Javascript gem:

 ,,, == Array((null,'cool',false,NaN,4));

 = evaluates as true

 http://wtfjs.com/2011/02/11/all-your-commas-are-belong-to-Array

 I swear, I am never going to complain about Python again.


 I am sure you know this, but for the record, Javascript has two equality
 operators, '==' and '==='.

 The double form attempts to coerce the left and right sides to the same
 type, the triple form does not.

 Re ==, this page:

 http://php.net/manual/en/language.operators.comparison.php

 states:

 If you compare a number with a string or the *comparison involves
 numerical strings*, then each string is converted to a number and the
 comparison performed numerically. (emphasis added)

 So they get coerced to numbers if they _look_ like numbers!


I just tested Steven's example.

,,, == Array((null,'cool',false,NaN,4)) evaluates to true

,,, === Array((null,'cool',false,NaN,4)) evaluates to false

I did look at the article that Steven linked to, but it made my eyes glaze, 
so don't ask me to explain it. I am prepared to use up a few brain cells 
trying to improve my own programming, but not trying to understand someone 
else's 'wtf' moments! [1]

Frank

[1]  Unless I have to maintain it, of course. I have been there before, and 
I have some dark memories!



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


Re: Good Python Book

2013-10-10 Thread kristof leroux
Python in Practice - Mark Summerfield

On Wednesday, October 9, 2013 2:55:17 PM UTC+2, Schneider wrote:
 Hi List,
 
 
 
 I'm looking for a good advanced python book. Most books I looked at up 
 
 to now are on beginners level.
 
 I don't need a reference (that's online) or a book explaining how to use 
 
 the interpreter or how to use list comprehensions on the one side and 
 
 skipping topics like decorators, metaclasses on the other side.
 
 
 
 any suggestions?
 
 
 
 bg,
 
 Johannes
 
 
 
 -- 
 
 GLOBE Development GmbH
 
 Königsberger Strasse 260
 
 48157 MünsterGLOBE Development GmbH
 
 Königsberger Strasse 260
 
 48157 Münster
 
 0251/5205 390

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


Re: super in Python 3 and variadic arguments

2013-10-10 Thread Marco Buttu

On 10/10/2013 01:04 PM, Ned Batchelder wrote:


On 10/10/13 3:22 AM, Marco Buttu wrote:

 import inspect
 class B(A):
... def bfoo(*args):
... frame = inspect.currentframe()
... for obj, value in frame.f_locals.items():
... print(obj, value, sep=' -- ')
... # super().afoo(*args[1:])
...
 B().bfoo(1, 2, 3)
args -- (__main__.B object at 0x7f28c960a590, 1, 2, 3)
frame -- frame object at 0x7f28cad4b240

So, why does not super use it?



Python has never claimed the kind of purity that makes everything work
in a totally simple consistent way.  super() with no args is a kind of
hack to begin with.  It involves a special case in the compiler (so that
using the name super as a function call will act as if you had
accessed the name __class__ so that super can find it later), and
inspecting the stack frame during execution.


It seems reasonable


It's an interesting case of the Zen of Python.  It violates one
(explicit is better than implicit), but only because of another one
(practicality beats purity).  super(MyClass, self) in Python 2 is the
kind of brain-bender that so many people get wrong at first, that it's
helped plenty of people to do the arguments implicitly, even if there
are oddball edge cases that it doesn't seem to handle properly.

--Ned.


Thanks for the comprehensive answer ;)

--
Marco Buttu
--
https://mail.python.org/mailman/listinfo/python-list


Re: I am never going to complain about Python again

2013-10-10 Thread Roy Smith
In article 52562ee3$0$2931$c3e8da3$76491...@news.astraweb.com,
 Steven D'Aprano st...@pearwood.info wrote:

 Just came across this little Javascript gem:
 
 ,,, == Array((null,'cool',false,NaN,4));
 
 = evaluates as true
 
 http://wtfjs.com/2011/02/11/all-your-commas-are-belong-to-Array
 
 I swear, I am never going to complain about Python again.

I've just finished reading JavaScript: The Good Parts, by Douglas 
Crockford (now I'm working on the harder part of re-reading it slowly, 
to make sure I really understand it).  Anybody who is forced to work 
with javascript should read this book.  It's the KR of JS.

Anyway, one of the pieces of advice he gives is to pretend that == 
doesn't exist, and always use ===.  PHP suffers from much the same 
problem.

BTW, here's a Python equality oddity:

 r = 0.0
 c = 0 + 0j
 r == c
True
 int(r) == int(c)
Traceback (most recent call last):
  File stdin, line 1, in module
TypeError: can't convert complex to int

If x == y, then f(x) should also equal f(y).  More specifically, if x == 
y, and x is in the domain of f(), then y should also be in the domain of 
f().

BTW, one of the earliest things that turned me on to Python was when I 
discovered that it uses j as the imaginary unit, not i.  All 
right-thinking people will agree with me on this.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Good Python Book

2013-10-10 Thread David
On 9 October 2013 23:55, Schneider j...@globe.de wrote:

 I'm looking for a good advanced python book. Most books I looked at up to
 now are on beginners level.
 I don't need a reference (that's online) or a book explaining how to use the
 interpreter or how to use list comprehensions on the one side and skipping
 topics like decorators, metaclasses on the other side.

 any suggestions?

https://wiki.python.org/moin/AdvancedBooks
-- 
https://mail.python.org/mailman/listinfo/python-list


Complex literals (was Re: I am never going to complain about Python again)

2013-10-10 Thread Chris Angelico
On Fri, Oct 11, 2013 at 12:09 AM, Roy Smith r...@panix.com wrote:
 BTW, one of the earliest things that turned me on to Python was when I
 discovered that it uses j as the imaginary unit, not i.  All
 right-thinking people will agree with me on this.

I've never been well-up on complex numbers; can you elaborate on this,
please? All I know is that I was taught that the square root of -1 is
called i, and that hypercomplex numbers include i, j, k, and maybe
even other terms, and I never understood where j comes from. Why is
Python better for using j?

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


Re: Complex literals (was Re: I am never going to complain about Python again)

2013-10-10 Thread Robin Becker

On 10/10/2013 14:25, Chris Angelico wrote:

On Fri, Oct 11, 2013 at 12:09 AM, Roy Smith r...@panix.com wrote:

BTW, one of the earliest things that turned me on to Python was when I
discovered that it uses j as the imaginary unit, not i.  All
right-thinking people will agree with me on this.


I've never been well-up on complex numbers; can you elaborate on this,
please? All I know is that I was taught that the square root of -1 is
called i, and that hypercomplex numbers include i, j, k, and maybe
even other terms, and I never understood where j comes from. Why is
Python better for using j?

ChrisA


Electrical Engineers  use j (probably to avoid conflicts with i (current).
--electrically yrs--
Robin Becker

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


Re: Cookie gets changed when hit comes from a referrer

2013-10-10 Thread Denis McMahon
On Thu, 10 Oct 2013 01:10:19 +, Steven D'Aprano wrote:

 If Nikos wants to write programs that communicate using internet
 protocols, Nikos really needs to learn where internet protocols are
 defined, how to read and interpret those protocol definitions, and how
 to check that the data he's sending or receiving is the data that he
 thinks he's sending or receiving.
 
 You can't seriously mean that everyone who runs a website has to become
 skilled at reading and interpreting the RFCs for internet protocols.
 Which protocols? All the way down to TCP/IP?

I'm not talking about running a website, I'm talking about writing code 
that uses specific protocols to transfer data. If Nikos is writing code 
that uses http, then he needs to understand http.

His previous questions relating to his wish to spoof ip addresses 
suggests that in his case, he needs to understand tcp/ip as well.

I'm not suggesting that every web admin needs to know these, but anyone 
who is trying to talk at the protocol level needs to understand the 
protocol, yes.

 Nikos Nikos Nikos... and what about me? If I asked you for a few
 pointers about a good place to learn about running a web site, would you
 tell me to fuck off too? I wonder what you are doing here, if you are so
 unwilling to share your hard-earned knowledge with others as you seem in
 this post. This attitude is not the Denis McMahon I'm used to.

I'm not unwilling, but this forum is not the place for tcp/ip 101, or 
http 101, or smtp 101, or dns 101, or geolocation 101, despite Nkos' 
attempts to turn it into one. This forum is a place for python coding.

 Nikos, are you reading this? This is what happens when you behave like a
 royal pain in the arse and annoy people. They stop wanting to help you.
 Be told. Learn from this. Don't repeat this mistake in the next forum.
 If you learn nothing else, learn that lesson.

Yes, this is exactly the issue. I am so pissed off with trying to help 
Nikos and being told things like Your solution is crap because you use 
too many lines, even though it works, unlike my [Nikos's] single line 
effort which I [Nikos] think looks aesthetically wonderful and which must 
therefore be the superior solution even though it doesn't work.

I went so far as to set up mod-wsgi on my server simply to try and 
understand his cookie issues - before that I hadn't used python on my 
apache server except at the basic cgi level. I managed to get a 
functional cookie implementation up and running within a few hours, 
primarily by reading the relevant api documentation, and looking up a few 
examples on slashdot and similar.

Given that Nikos presents as being a professional coder developing client 
facing facing python code for a hosting provider, his inability to do 
something similar, indeed his inability to locate relevant sources of 
information, is frankly quite astounding, and I am in agreement with the 
many others on this forum who regularly and frequently voice the opinion 
that Nikos, specifically, has no business coding anything on a web server.

-- 
Denis McMahon, denismfmcma...@gmail.com
-- 
https://mail.python.org/mailman/listinfo/python-list


How to make Tkinter Listbox entry insensitive?

2013-10-10 Thread Skip Montanaro
I have a pair of Listboxes, each with ranges of the same text.  For example:

ABCABC
DEFDEF
GHIGHI
JKLJKL

(My apologies, I'm sure Gmail is going to butcher that.)

If I select ABC from the first list, only the last three values in
the second list are appropriate selections. Is there a way to make a
list entry insensitive? No amount of Googling found anything. Does Tk
perhaps use a word other than insensitive to describe an item which
you'd normally be able to click?

Thx,

Skip
-- 
https://mail.python.org/mailman/listinfo/python-list


closure = decorator?

2013-10-10 Thread Tim
I've read a couple of articles about this, but still not sure.
When someone talks about a closure in another language (I'm learning Lua on the 
side), is that the same concept as a decorator in Python?

It sure looks like it.
thanks,
--Tim

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


Re: closure = decorator?

2013-10-10 Thread Chris Angelico
On Fri, Oct 11, 2013 at 12:51 AM, Tim jtim.arn...@gmail.com wrote:
 I've read a couple of articles about this, but still not sure.
 When someone talks about a closure in another language (I'm learning Lua on 
 the side), is that the same concept as a decorator in Python?

No, they're quite different. A decorator (let's look at function
decorators; classes can have them too, and they work pretty much the
same way) is syntactic sugar for this:

def func(args):
blah blah blah
func = decorator(func)

You can do all sorts of things with that. Even stupid things:

 @print
def foo():
pass

function foo at 0x00F5D8A0
 foo is None
True

Using print as a decorator does work, though hardly usefully :)

A closure, on the other hand, is a function that has some extra context:

def outer(x):
x += 1
def inner():
return x
return inner

The function inner() knows its context. When it's called, it'll use
the same value for x that would have been used in the outer function,
even though it's a separate function:

 foo = outer(5)
 foo()
6

The terminology is that inner() closes over x, if I have that
correct (I've not been all that big in functional programming and
lambda calculus). Someone will correct me if I'm not.

It's very common for a decorator to use closures, but the two are
completely different. They're no more connected than, say, for loops
and lists. They just happen to work well together.

Closures in other languages will, as far as I know, be the same thing
as closures in Python. (And their presence and functionality in
JavaScript leaves me wondering why on earth the 'this' reference can't
be considered closed over in the same way. But that's hardly the
worst of the language's warts.)

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


Skipping decorators in unit tests

2013-10-10 Thread Gilles Lenfant
Hi,

(explaining the title) : my app has functions and methods (and maybe classes in 
the future) that are decorated by decorators provided by the standard library 
or 3rd party packages.

But I need to test undecorated functions and methods in my unit tests, 
preferably without adding special stuffs in my target tested modules.

Can someone point out good practices or dedicated tools that remove 
temporarily the decorations.

I pasted a small example of what I heed at http://pastebin.com/20CmHQ7Y

Many thanks in advance
-- 
Gilles Lenfant
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: closure = decorator?

2013-10-10 Thread Jussi Piitulainen
Tim writes:

 I've read a couple of articles about this, but still not sure.

 When someone talks about a closure in another language (I'm learning
 Lua on the side), is that the same concept as a decorator in Python?
 
 It sure looks like it.

I don't see how. Wikipedia's opening paragraph on closure seems good
to me - closures are a way to implement lexical scoping when functions
that have free variables are passed as arguments and returned as
values:

http://en.wikipedia.org/wiki/Closure_(computer_science)
# In programming languages, a closure (also lexical closure or
# function closure) is a function or reference to a function together
# with a referencing environment—a table storing a reference to each
# of the non-local variables (also called free variables or upvalues)
# of that function.[1] A closure—unlike a plain function
# pointer—allows a function to access those non-local variables even
# when invoked outside its immediate lexical scope.

There's an example in Python on that page.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to make Tkinter Listbox entry insensitive?

2013-10-10 Thread Chris Angelico
On Fri, Oct 11, 2013 at 12:50 AM, Skip Montanaro s...@pobox.com wrote:
 Does Tk
 perhaps use a word other than insensitive to describe an item which
 you'd normally be able to click?

Other synonyms from GUI toolkits are inactive and disabled. I
don't know if that'll be any help though.

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


Re: Complex literals (was Re: I am never going to complain about Python again)

2013-10-10 Thread Grant Edwards
On 2013-10-10, Chris Angelico ros...@gmail.com wrote:
 On Fri, Oct 11, 2013 at 12:09 AM, Roy Smith r...@panix.com wrote:
 BTW, one of the earliest things that turned me on to Python was when I
 discovered that it uses j as the imaginary unit, not i.  All
 right-thinking people will agree with me on this.

 I've never been well-up on complex numbers; can you elaborate on this,
 please? All I know is that I was taught that the square root of -1 is
 called i,

Nope.  i is electical current (though it's more customary to use
upper case).  j is the square root of -1.

 and that hypercomplex numbers include i, j, k, and maybe even other
 terms, and I never understood where j comes from. Why is Python
 better for using j?

Because that's the way we do it in electrical engineering. 

;)

-- 
Grant Edwards   grant.b.edwardsYow! Are we THERE yet?
  at   My MIND is a SUBMARINE!!
  gmail.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Complex literals (was Re: I am never going to complain about Python again)

2013-10-10 Thread Chris Angelico
On Fri, Oct 11, 2013 at 1:12 AM, Grant Edwards invalid@invalid.invalid wrote:
 Nope.  i is electical current (though it's more customary to use
 upper case).  j is the square root of -1.

 and that hypercomplex numbers include i, j, k, and maybe even other
 terms, and I never understood where j comes from. Why is Python
 better for using j?

 Because that's the way we do it in electrical engineering.


Okay, so hold on a minute... a hypercomplex number is the sum of a
real number, some electrical current, an imaginary number, and k?

This belongs in the Izzet League, I think.

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


Re: closure = decorator?

2013-10-10 Thread Jussi Piitulainen
Chris Angelico writes:

 def outer(x):
 x += 1
 def inner():
 return x
 return inner
...
 The terminology is that inner() closes over x, if I have that
 correct (I've not been all that big in functional programming and
 lambda calculus). Someone will correct me if I'm not.

I don't actually know for sure what the most correct terminology is,
but I like to think that a closure is a procedure (function) closed in
the environment (namespace) where it was created, so even when it is
called from another environment, it uses the one where it was born.

But that may be private to me.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Complex literals (was Re: I am never going to complain about Python again)

2013-10-10 Thread David
On 11 October 2013 00:25, Chris Angelico ros...@gmail.com wrote:
 On Fri, Oct 11, 2013 at 12:09 AM, Roy Smith r...@panix.com wrote:

 I've never been well-up on complex numbers; can you elaborate on this,
 please? All I know is that I was taught that the square root of -1 is
 called i, and that hypercomplex numbers include i, j, k, and maybe
 even other terms, and I never understood where j comes from. Why is
 Python better for using j?

Pretty well covered here: http://en.wikipedia.org/wiki/Complex_number

Plus, the simple overview is that they are useful because they are
two-dimensional, and so can be used to simply calculations involving
two-dimensional quantities. Very useful for electrical engineers who
use them to represent the two dimensions of amplitude,phase in
Fourier or Laplace circuit analysis. As others have pointed out, they
use the symbol j for the square root of -1 to avoid confusion with the
symbol i used for current.

I have never heard the term hypercomplex numbers. I guess you
are referring to vectors with more dimensions than two. A three
dimensional vector is described as having components in i,j,k
directions. Although this is very like an extension of complex numbers
into higher dimensions, the symbols used (i,j,k) are not the same
as the i or j used for complex numbers. Instead they represent
orthogonal unit vectors; which are similar in concept (because
real and imaginary components of complex numbers are orthogonal),
but not the *same*. So don't think of the i *or* j of a complex number
being related to the i *and* j etc components of a vector.

These are useful for example to describe three dimensional space, and
scalar or vector functions in that space.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python's and and Pythons or

2013-10-10 Thread Ethan Furman

On 10/10/2013 12:43 AM, Terry Reedy wrote:

On 10/10/2013 2:45 AM, Chris Angelico wrote:

On Thu, Oct 10, 2013 at 5:12 PM, Peter Cacioppi
peter.cacio...@gmail.com wrote:

I'm trying to think of a good example usage of echo-argument and. Maybe 
something like



A bit awkward, echo-argument or is more naturally useful to me then 
echo-argument and.


first_element = some_list[0]# Oops, may crash


some_list[0:1] always works, and sometimes is usable, but you still cannot 
index the slice.


Not if some_list is None, False, or another non-indexable type.

--
~Ethan~
--
https://mail.python.org/mailman/listinfo/python-list


Re: How to make Tkinter Listbox entry insensitive?

2013-10-10 Thread Skip Montanaro
 Other synonyms from GUI toolkits are inactive and disabled. I
 don't know if that'll be any help though.

Thanks. disabled did the trick. Turns out you can't disable
individual items. Instead, you have to (hackishly) change the display
of entries somehow to indicate their inappropriateness...

Skip
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Complex literals (was Re: I am never going to complain about Python again)

2013-10-10 Thread rusi
On Thursday, October 10, 2013 8:04:00 PM UTC+5:30, David wrote:
 I have never heard the term hypercomplex numbers. I guess you
 are referring to vectors with more dimensions than two. A three

A generalization of quaternions :
http://en.wikipedia.org/wiki/Hypercomplex_number
http://en.wikipedia.org/wiki/Quaternion

And now where's the policeman out to catch the OT threads??
[Ducks the eggs and runs...]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to make Tkinter Listbox entry insensitive?

2013-10-10 Thread Chris “Kwpolska” Warrick
On Thu, Oct 10, 2013 at 4:37 PM, Skip Montanaro s...@pobox.com wrote:
 Thanks. disabled did the trick. Turns out you can't disable
 individual items. Instead, you have to (hackishly) change the display
 of entries somehow to indicate their inappropriateness...

Removing inappropriate entries is not much of a hack.

-- 
Chris “Kwpolska” Warrick http://kwpolska.tk
PGP: 5EAAEA16
stop html mail | always bottom-post | only UTF-8 makes sense
-- 
https://mail.python.org/mailman/listinfo/python-list


Python 3.2 | WIndows 7 -- Multiprocessing and files not closing

2013-10-10 Thread Isaac Gerg
I have a function that looks like the following:

#-
filename = 'c:\testfile.h5'
f = open(filename,'r')
data = f.read()

q = multiprocessing.Queue()
p = multiprocess.Process(target=myFunction,args=(data,q))
p.start()
result = q.get()
p.join()
q.close()

f.close()

os.remove(filename)
#-

When I run this code, I get an error on the last line when I try to remove the 
file.  It tells me that someone has access to the file.  When I remove the 
queue and multiprocessing stuff, the function works fine.

What is going on here?

Thanks in advance,
Isaac


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


Passing C pionters to Python for use with cffi

2013-10-10 Thread Eric Frederich
Hello,

I'm extending an application that supports customization using the C
language.
I am able to write standalone python applications that use the C API's
using cffi.
This is good, but only a first step.

This application allows me to register code that will run on various events
but it has to be C code.
I'd like to write Python code instead.
So basically, my C code will use the Python C API to get a handle to the
module and function (like how they do it here
http://docs.python.org/2/extending/embedding.html#pure-embedding)
What would be the proper way to pass a pointer to a C structure from C to
Python so that I can use ffi.cast and be able to use it from within Python?

I have got this to work but I'm not certain that it is correct, fool-proof,
or portable.

This is how I got it to work from the C side

PyObject* pArgs = Py_BuildValue((k), some_structure);
PyObject_CallObject(pFunc, pArgs)

... and from the Python side...

def my_function(struct_ptr):
struct = ffi.cast(mystruct_t *, struct_ptr)


Like I said, this works fine.  I am able to manipulate the structure from
within Python.
I just want to know the correct way to to this.

Thanks,
~Eric
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: I am never going to complain about Python again

2013-10-10 Thread Neil Cerutti
On 2013-10-10, Roy Smith r...@panix.com wrote:
 In article 52562ee3$0$2931$c3e8da3$76491...@news.astraweb.com,
  Steven D'Aprano st...@pearwood.info wrote:

 Just came across this little Javascript gem:
 
 ,,, == Array((null,'cool',false,NaN,4));
 
 = evaluates as true
 
 http://wtfjs.com/2011/02/11/all-your-commas-are-belong-to-Array
 
 I swear, I am never going to complain about Python again.

 I've just finished reading JavaScript: The Good Parts, by Douglas 
 Crockford (now I'm working on the harder part of re-reading it slowly, 
 to make sure I really understand it).  Anybody who is forced to work 
 with javascript should read this book.  It's the KR of JS.

 Anyway, one of the pieces of advice he gives is to pretend that == 
 doesn't exist, and always use ===.  PHP suffers from much the same 
 problem.

 BTW, here's a Python equality oddity:

 r = 0.0
 c = 0 + 0j
 r == c
 True
 int(r) == int(c)
 Traceback (most recent call last):
   File stdin, line 1, in module
 TypeError: can't convert complex to int

 If x == y, then f(x) should also equal f(y).  More
 specifically, if x == y, and x is in the domain of f(), then y
 should also be in the domain of f().

Mixed arithmetic always promotes to the wider type (except in
the case of complex numbers (Ha!)).

r == c is equivalent to r == abs(c), which returns the magintude
of the complex number.

I wonder why it was deemed reasonable to do that but not for the
float constructor to do the same, or even int.

 BTW, one of the earliest things that turned me on to Python was
 when I discovered that it uses j as the imaginary unit, not i.
 All right-thinking people will agree with me on this.

On top of the engineering origin, j is more noticeable.

-- 
Neil Cerutti
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: I am never going to complain about Python again

2013-10-10 Thread Rotwang

On 10/10/2013 16:51, Neil Cerutti wrote:

[...]

Mixed arithmetic always promotes to the wider type (except in
the case of complex numbers (Ha!)).

r == c is equivalent to r == abs(c), which returns the magintude
of the complex number.


What?

 -1 == -1 + 0j
True
 -1 == abs(-1 + 0j)
False
 1 == 0 + 1j
False
 1 == abs(0 + 1j)
True
--
https://mail.python.org/mailman/listinfo/python-list


Re: I am never going to complain about Python again

2013-10-10 Thread MRAB

On 10/10/2013 16:57, Rotwang wrote:

On 10/10/2013 16:51, Neil Cerutti wrote:

[...]

Mixed arithmetic always promotes to the wider type (except in
the case of complex numbers (Ha!)).

r == c is equivalent to r == abs(c), which returns the magintude
of the complex number.


What?

   -1 == -1 + 0j
True
   -1 == abs(-1 + 0j)
False
   1 == 0 + 1j
False
   1 == abs(0 + 1j)
True


Indeed.

If r is real (float) and c is complex:

r == c means r == c.real and c.imag == 0.0

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


Re: closure = decorator?

2013-10-10 Thread Piet van Oostrum
Jussi Piitulainen jpiit...@ling.helsinki.fi writes:

 I don't actually know for sure what the most correct terminology is,
 but I like to think that a closure is a procedure (function) closed in
 the environment (namespace) where it was created, so even when it is
 called from another environment, it uses the one where it was born.

I usually say that a closure is a package, containing a function with
some additional data it needs. The data usually is in the form of name
bindings.

On the other hand, an object (instance) is a package containg data, with
one of more functions that work on this data.

So an object is more or less the dual of a closure, and in many cases
they can be used for the same purpose. In most programming languages the
difference is that closures can be called directly, whereas an object
needs to be used with a method call to do the same (this makes Java so
ugly in this area). In Python, however, you can define the __call__
method and with this they become almost identical in behaviour.
-- 
Piet van Oostrum p...@vanoostrum.org
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Complex literals (was Re: I am never going to complain about Python again)

2013-10-10 Thread William Ray Wing
On Oct 10, 2013, at 10:12 AM, Grant Edwards invalid@invalid.invalid wrote:

 On 2013-10-10, Chris Angelico ros...@gmail.com wrote:
 On Fri, Oct 11, 2013 at 12:09 AM, Roy Smith r...@panix.com wrote:
 BTW, one of the earliest things that turned me on to Python was when I
 discovered that it uses j as the imaginary unit, not i.  All
 right-thinking people will agree with me on this.
 
 I've never been well-up on complex numbers; can you elaborate on this,
 please? All I know is that I was taught that the square root of -1 is
 called i,
 
 Nope.  i is electical current (though it's more customary to use
 upper case).  j is the square root of -1.
 

It all depends on where (in what field) you learned about complex numbers.  
Mathematicians and Physicists use i, engineers use j.

-Bill 

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


Re: Python 3.2 | WIndows 7 -- Multiprocessing and files not closing

2013-10-10 Thread Piet van Oostrum
Isaac Gerg isaac.g...@gergltd.com writes:

 I have a function that looks like the following:

That doesn't look like a function


 #-
 filename = 'c:\testfile.h5'

Your filename is most probably wrong. It should be something like:

filename = 'c:/testfile.h5'
filename = 'c:\\testfile.h5'
filename = r'c:\testfile.h5'
-- 
Piet van Oostrum p...@vanoostrum.org
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python 3.2 | WIndows 7 -- Multiprocessing and files not closing

2013-10-10 Thread Isaac Gerg
Sorry, I am just providing pseudo code since I the code i have is quite large.

As I mentioned, the code works fine when I remove the multirpcessing stuff so 
the filename is not the issue (though you are right in your correction).

Someone with the same problem posted a smaller, more complete example here:

http://stackoverflow.com/questions/948119/preventing-file-handle-inheritance-in-multiprocessing-lib

None of the solutions posted work.

On Thursday, October 10, 2013 12:38:19 PM UTC-4, Piet van Oostrum wrote:
 Isaac Gerg isaac.g...@gergltd.com writes:
 
 
 
  I have a function that looks like the following:
 
 
 
 That doesn't look like a function
 
 
 
 
 
  #-
 
  filename = 'c:\testfile.h5'
 
 
 
 Your filename is most probably wrong. It should be something like:
 
 
 
 filename = 'c:/testfile.h5'
 
 filename = 'c:\\testfile.h5'
 
 filename = r'c:\testfile.h5'
 
 -- 
 
 Piet van Oostrum p...@vanoostrum.org
 
 WWW: http://pietvanoostrum.com/
 
 PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Complex literals (was Re: I am never going to complain about Python again)

2013-10-10 Thread Grant Edwards
On 2013-10-10, Chris Angelico ros...@gmail.com wrote:
 On Fri, Oct 11, 2013 at 1:12 AM, Grant Edwards invalid@invalid.invalid 
 wrote:
 Nope.  i is electical current (though it's more customary to use
 upper case).  j is the square root of -1.

 and that hypercomplex numbers include i, j, k, and maybe even other
 terms, and I never understood where j comes from. Why is Python
 better for using j?

 Because that's the way we do it in electrical engineering.

 Okay, so hold on a minute... a hypercomplex number is the sum of a
 real number, some electrical current, an imaginary number, and k?

I don't know that EE's ever encounter hypercomplex numbers (I
certainly never have), nor does Python support them, so in _practice_
there isn't really a conflict.

-- 
Grant Edwards   grant.b.edwardsYow! I want EARS!  I want
  at   two ROUND BLACK EARS
  gmail.comto make me feel warm
   'n secure!!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: I am never going to complain about Python again

2013-10-10 Thread Neil Cerutti
On 2013-10-10, MRAB pyt...@mrabarnett.plus.com wrote:
 On 10/10/2013 16:57, Rotwang wrote:
 On 10/10/2013 16:51, Neil Cerutti wrote:
 [...]

 Mixed arithmetic always promotes to the wider type (except in
 the case of complex numbers (Ha!)).

 r == c is equivalent to r == abs(c), which returns the magintude
 of the complex number.

 What?

-1 == -1 + 0j
 True
-1 == abs(-1 + 0j)
 False
1 == 0 + 1j
 False
1 == abs(0 + 1j)
 True

 Indeed.

 If r is real (float) and c is complex:

  r == c means r == c.real and c.imag == 0.0

Woah. I thought I was going by what the docs say:

  Python fully supports mixed arithmetic: when a binary
  arithmetic operator has operands of different numeric types,
  the operand with the “narrower” type is widened to that of the
  other, where integer is narrower than floating point, which is
  narrower than complex. Comparisons between numbers of mixed
  type use the same rule. [2] The constructors int(), float(),
  and complex() can be used to produce numbers of a specific
  type.

[...]

  [2] Not for complex numbers. Instead convert to floats using
 abs() if appropriate.

I guess the if appropriate part eluded my eye. When *is* it
appropriate? Apparently not during an equal test.

 5.0 == abs(3 + 4j)
False

-- 
Neil Cerutti
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Complex literals (was Re: I am never going to complain about Python again)

2013-10-10 Thread Ethan Furman

On 10/10/2013 07:20 AM, Chris Angelico wrote:

On Fri, Oct 11, 2013 at 1:12 AM, Grant Edwards wrote:


Nope.  i is electical current (though it's more customary to use
upper case).  j is the square root of -1.


and that hypercomplex numbers include i, j, k, and maybe even other
terms, and I never understood where j comes from. Why is Python
better for using j?


Because that's the way we do it in electrical engineering.


Okay, so hold on a minute... a hypercomplex number is the sum of a
real number, some electrical current, an imaginary number, and k?


That would certainly explain why it's hyper.  ;)

--
~Ethan~
--
https://mail.python.org/mailman/listinfo/python-list


Re: How to make Tkinter Listbox entry insensitive?

2013-10-10 Thread Skip Montanaro
 Removing inappropriate entries is not much of a hack.

True, but then I have to go through the trouble of adding them back in
should they become valid again. :-)

Skip
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: I am never going to complain about Python again

2013-10-10 Thread Ian Kelly
On Thu, Oct 10, 2013 at 11:48 AM, Neil Cerutti ne...@norwich.edu wrote:
 On 2013-10-10, MRAB pyt...@mrabarnett.plus.com wrote:
 On 10/10/2013 16:57, Rotwang wrote:
 On 10/10/2013 16:51, Neil Cerutti wrote:
 [...]

 Mixed arithmetic always promotes to the wider type (except in
 the case of complex numbers (Ha!)).

 r == c is equivalent to r == abs(c), which returns the magintude
 of the complex number.

 What?

-1 == -1 + 0j
 True
-1 == abs(-1 + 0j)
 False
1 == 0 + 1j
 False
1 == abs(0 + 1j)
 True

 Indeed.

 If r is real (float) and c is complex:

  r == c means r == c.real and c.imag == 0.0

 Woah. I thought I was going by what the docs say:

   Python fully supports mixed arithmetic: when a binary
   arithmetic operator has operands of different numeric types,
   the operand with the “narrower” type is widened to that of the
   other, where integer is narrower than floating point, which is
   narrower than complex. Comparisons between numbers of mixed
   type use the same rule. [2] The constructors int(), float(),
   and complex() can be used to produce numbers of a specific
   type.

 [...]

   [2] Not for complex numbers. Instead convert to floats using
  abs() if appropriate.

 I guess the if appropriate part eluded my eye. When *is* it
 appropriate? Apparently not during an equal test.

If you click on the footnote, it takes you to:

[2]As a consequence, the list [1, 2] is considered equal to [1.0,
2.0], and similarly for tuples.

The text that you have mistakenly identified as the footnote is
actually part of the key to the Notes column of the numeric
operations table, where it is referred to by the x % y and
divmod(x, y) operations.  Specifically, it warns of this error:

 3j % 2j
Traceback (most recent call last):
  File stdin, line 1, in module
TypeError: can't mod complex numbers.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: class implementation

2013-10-10 Thread markotaht
kolmapäev, 9. oktoober 2013 2:55.28 UTC+3 kirjutas Cameron Simpson:
 On 08Oct2013 01:20,  wrote:
 
  I cant just subclassing doesent work. It seem the init method of the source 
  class also calls out another class. And the problem is, i can subclass the 
  other class to with the required function but the end result is that it 
  doesent work, since the source class cant accsess the subclass functions. 
 
  
 
  The source code is pykkar. 
 
  
 
  https://courses.cs.ut.ee/all/MTAT.03.100/2012_fall/uploads/opik/_downloads/pykkar.py
 
  
 
  I want to add it a new ability called left(). I cant manipulate the source 
  class, cause then my comp will be the only one where the program runs.
 
  
 
  class pykkar_l(Pykkar):
 
  def left(self):
 
  self._world.execute(left)
 
 [...]
 
 
 
 You normally need to call the superclasses' __init__ method as well.
 
 Example:
 
 
 
 def __init__(self):
 
 Pykkar.__init__(self)
 
 ... any of your own init stuff ...
 
 
 
 Likewise for your world_l class.
 
 
 
 BTW, it is conventional to start class names with an upper case letters. Just
 
 style, but it helps other people when reading your code.
 
 
 
 Cheers,
 
 --  
 
 
 It looks like you've got Mister Bus Error installed.- tjc

OK so I did a took time of and read the pykkar code through. abd I found that 
there is a third class i have to implement. 

This Is the pykkar sourcecode
# coding=UTF-8

Intro and usage
===
Pykkar is a virtual robot living in a virtual world. It can step, turn 90 
degrees right,
paint floor tiles, take and put down traffic cones and push boxes. It can detect
whether there is a wall or other obstacle ahead or whether it's standing on a 
painted
tile. It can be commanded by Python code (either procedural or OOP style)

Pykkar's world can be created by a call to ``create_world`` (when using 
procedural style)
or by instantiating class ``World`` (OOP style). Both take single argument, 
which is
a string representation of the world. Lines in the string represent the rows in 
the world
map. Each character represents one tile. Meaning of characters:

#wall
space  plain floor
.painted floor
bbox on plain floor
Bbox on painted floor
^  v   pykkar on plain floor without cone 
 (caret, greater-than, lowercase v, less-than)
N E S W  pykkar on painted floor without cone
1 2 3 4 5 6 7 8 9cone stack on plain floor
Csingle cone on painted floor

Sample:

create_world('''

# #
# 3#

''')

this creates a world where 2x2 floor are is padded with walls. Pykkar is in 
north-west
corner of the floor, looking east and in south-east corner there is a stack of 
3 traffic
cones.

In procedural style, Pykkar can be commanded by calling global functions 
defined in this 
module (eg. ``step()``, ``right()``, etc). There are also functions for 
querying the 
world (``is_wall()``, ``is_box()``, etc). New compound commands can be defined 
by defining 
new functions in client module.

In OOP style, Pykkar is represented by a separate object of class ``Pykkar``. 
In the
start of the program, client code is supposed to create new world (eg. ``w = 
World(layout)``)
and a Pykkar living in that world (eg ``p = Pykkar(w)``). Commands are given by 
calling 
the methods of Pykkar object. New commands should be defined by subclassing 
``Pykkar``. 



Technical stuff

In order to reserve the main thread for executing commands (this way respective 
function calls
can be written in client module's toplevel), tkinter window must run in a 
different thread. 
Unfortunately, tkinter runs reliably only in the main thread of the process. 
For this reason the execution is divided into 2 processes: the main process, 
which is just a shallow command proxy and the child process, which runs actual 
program 
logic and presents the world state in a tkinter window.

Main process (ie. user module) normally begins by creating the world (with 
either 
``create_world(...)`` or ``World(...)``). This spawns a child process which 
creates
tkinter window representing the world. Main process then continues by executing
user-provided function/method calls, which amounts to writing respective 
command strings
to child process' stdin and reading results back from child process' stdout.

Main player in child process is an object of class ``_WorldProper``. It keeps 
the 
data structures about world layout, responds to commands that alter the world 
state and runs 
a tkinter window. It reads periodically next command from stdin, acts upon it 
and writes 
result (may be None) to stdout.

NB! as stdout from tkinter process is parsed, you can't just print out debug 
information
to sys.stdout. Use sys.stderr instead!

Reading from stdin blocks, as usual. This would make window temporariliy 
unresponsive 
when 

Re: Python 3.2 | WIndows 7 -- Multiprocessing and files not closing

2013-10-10 Thread Ned Batchelder

On 10/10/13 12:44 PM, Isaac Gerg wrote:

Sorry, I am just providing pseudo code since I the code i have is quite large.

As I mentioned, the code works fine when I remove the multirpcessing stuff so 
the filename is not the issue (though you are right in your correction).

Someone with the same problem posted a smaller, more complete example here:

http://stackoverflow.com/questions/948119/preventing-file-handle-inheritance-in-multiprocessing-lib

None of the solutions posted work.


(BTW: it's better form to reply beneath the original text, not above it.)

None of the solutions try the obvious thing of closing the file before 
spawning more processes.  Would that work for you?  A with statement 
is a convenient way to do this:


with open(filename,'r') as f:
data = f.read()

The file is closed automatically when the with statement ends.

--Ned.



On Thursday, October 10, 2013 12:38:19 PM UTC-4, Piet van Oostrum wrote:

Isaac Gerg isaac.g...@gergltd.com writes:




I have a function that looks like the following:



That doesn't look like a function




#-
filename = 'c:\testfile.h5'



Your filename is most probably wrong. It should be something like:



filename = 'c:/testfile.h5'

filename = 'c:\\testfile.h5'

filename = r'c:\testfile.h5'

--

Piet van Oostrum p...@vanoostrum.org

WWW: http://pietvanoostrum.com/

PGP key: [8DAE142BE17999C4]


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


Re: I am never going to complain about Python again

2013-10-10 Thread Neil Cerutti
On 2013-10-10, Ian Kelly ian.g.ke...@gmail.com wrote:
 On Thu, Oct 10, 2013 at 11:48 AM, Neil Cerutti ne...@norwich.edu wrote:
 Woah. I thought I was going by what the docs say:

   Python fully supports mixed arithmetic: when a binary
   arithmetic operator has operands of different numeric types,
   the operand with the ?narrower? type is widened to that of the
   other, where integer is narrower than floating point, which is
   narrower than complex. Comparisons between numbers of mixed
   type use the same rule. [2] The constructors int(), float(),
   and complex() can be used to produce numbers of a specific
   type.

 [...]

   [2] Not for complex numbers. Instead convert to floats using
  abs() if appropriate.

 I guess the if appropriate part eluded my eye. When *is* it
 appropriate? Apparently not during an equal test.

 If you click on the footnote, it takes you to:

 [2]As a consequence, the list [1, 2] is considered equal to [1.0,
 2.0], and similarly for tuples.

 The text that you have mistakenly identified as the footnote is
 actually part of the key to the Notes column of the numeric
 operations table, where it is referred to by the x % y and
 divmod(x, y) operations.  Specifically, it warns of this error:

 3j % 2j
 Traceback (most recent call last):
   File stdin, line 1, in module
 TypeError: can't mod complex numbers.

Doh!

Thanks, for that, and for the corrections. I could have avoided
all this by testing it correctly in the REPL, too.

I'll click on those footnotes instead of scanning to them from
now on.

-- 
Neil Cerutti
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python 3.2 | WIndows 7 -- Multiprocessing and files not closing

2013-10-10 Thread Isaac Gerg
On Thu, Oct 10, 2013 at 2:41 PM, Ned Batchelder n...@nedbatchelder.comwrote:

 On 10/10/13 12:44 PM, Isaac Gerg wrote:

 Sorry, I am just providing pseudo code since I the code i have is quite
 large.

 As I mentioned, the code works fine when I remove the multirpcessing
 stuff so the filename is not the issue (though you are right in your
 correction).

 Someone with the same problem posted a smaller, more complete example
 here:

 http://stackoverflow.com/**questions/948119/preventing-**
 file-handle-inheritance-in-**multiprocessing-libhttp://stackoverflow.com/questions/948119/preventing-file-handle-inheritance-in-multiprocessing-lib

 None of the solutions posted work.


 (BTW: it's better form to reply beneath the original text, not above it.)

 None of the solutions try the obvious thing of closing the file before
 spawning more processes.  Would that work for you?  A with statement is a
 convenient way to do this:

 with open(filename,'r') as f:
 data = f.read()

 The file is closed automatically when the with statement ends.

 --Ned.


 On Thursday, October 10, 2013 12:38:19 PM UTC-4, Piet van Oostrum wrote:

 Isaac Gerg isaac.g...@gergltd.com writes:



  I have a function that looks like the following:



 That doesn't look like a function



  #-**
 filename = 'c:\testfile.h5'



 Your filename is most probably wrong. It should be something like:



 filename = 'c:/testfile.h5'

 filename = 'c:\\testfile.h5'

 filename = r'c:\testfile.h5'

 --

 Piet van Oostrum p...@vanoostrum.org

 WWW: http://pietvanoostrum.com/

 PGP key: [8DAE142BE17999C4]



I will try what you suggest and see if it works.

Additionally, is there a place on the web to view this conversation and
reply?  Currently, I am only able to access this list through email.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python 3.2 | WIndows 7 -- Multiprocessing and files not closing

2013-10-10 Thread Isaac Gerg
On Thu, Oct 10, 2013 at 2:49 PM, Isaac Gerg isaac.g...@gergltd.com wrote:




 On Thu, Oct 10, 2013 at 2:41 PM, Ned Batchelder n...@nedbatchelder.comwrote:

 On 10/10/13 12:44 PM, Isaac Gerg wrote:

 Sorry, I am just providing pseudo code since I the code i have is quite
 large.

 As I mentioned, the code works fine when I remove the multirpcessing
 stuff so the filename is not the issue (though you are right in your
 correction).

 Someone with the same problem posted a smaller, more complete example
 here:

 http://stackoverflow.com/**questions/948119/preventing-**
 file-handle-inheritance-in-**multiprocessing-libhttp://stackoverflow.com/questions/948119/preventing-file-handle-inheritance-in-multiprocessing-lib

 None of the solutions posted work.


 (BTW: it's better form to reply beneath the original text, not above it.)

 None of the solutions try the obvious thing of closing the file before
 spawning more processes.  Would that work for you?  A with statement is a
 convenient way to do this:

 with open(filename,'r') as f:
 data = f.read()

 The file is closed automatically when the with statement ends.

 --Ned.


 On Thursday, October 10, 2013 12:38:19 PM UTC-4, Piet van Oostrum wrote:

 Isaac Gerg isaac.g...@gergltd.com writes:



  I have a function that looks like the following:



 That doesn't look like a function



  #-**
 filename = 'c:\testfile.h5'



 Your filename is most probably wrong. It should be something like:



 filename = 'c:/testfile.h5'

 filename = 'c:\\testfile.h5'

 filename = r'c:\testfile.h5'

 --

 Piet van Oostrum p...@vanoostrum.org

 WWW: http://pietvanoostrum.com/

 PGP key: [8DAE142BE17999C4]



 I will try what you suggest and see if it works.

 Additionally, is there a place on the web to view this conversation and
 reply?  Currently, I am only able to access this list through email.



Ned, I am unable to try what you suggest.  The multiprocess.Process call is
within a class but its target is a static method outside of the class thus
no pickling.  I cannot close the file and then reopen after the
multiprocess.Process call because other threads may be reading from the
file during that time.  Is there a way in Python 3.2 to prevent the
multiprocess.Process from inheriting the file descriptors from the parent
process OR, is there a way to ensure that the multiprocess is completely
closed and garbaged collected by the time I want to use os.remove()?

Isaac
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Complex literals (was Re: I am never going to complain about Python again)

2013-10-10 Thread Christian Gollwitzer

Am 10.10.13 18:54, schrieb Grant Edwards:

On 2013-10-10, Chris Angelico ros...@gmail.com wrote:

On Fri, Oct 11, 2013 at 1:12 AM, Grant Edwards invalid@invalid.invalid wrote:

Nope.  i is electical current (though it's more customary to use
upper case).  j is the square root of -1.


and that hypercomplex numbers include i, j, k, and maybe even other
terms, and I never understood where j comes from. Why is Python
better for using j?


Because that's the way we do it in electrical engineering.


Okay, so hold on a minute... a hypercomplex number is the sum of a
real number, some electrical current, an imaginary number, and k?


I don't know that EE's ever encounter hypercomplex numbers (I
certainly never have)


But they are very useful to represent 3D-rotation around an inclined 
axis (look up quaternion rotation). I don't know whether EEs work in 
aircraft navigation, but I suspect they do ;)


Christian

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


Re: Complex literals (was Re: I am never going to complain about Python again)

2013-10-10 Thread Oscar Benjamin
On 10 October 2013 15:34, David bouncingc...@gmail.com wrote:
 On 11 October 2013 00:25, Chris Angelico ros...@gmail.com wrote:
 On Fri, Oct 11, 2013 at 12:09 AM, Roy Smith r...@panix.com wrote:

 I've never been well-up on complex numbers; can you elaborate on this,
 please? All I know is that I was taught that the square root of -1 is
 called i, and that hypercomplex numbers include i, j, k, and maybe
 even other terms, and I never understood where j comes from. Why is
 Python better for using j?

 Pretty well covered here: http://en.wikipedia.org/wiki/Complex_number

 Plus, the simple overview is that they are useful because they are
 two-dimensional, and so can be used to simply calculations involving
 two-dimensional quantities. Very useful for electrical engineers who
 use them to represent the two dimensions of amplitude,phase in
 Fourier or Laplace circuit analysis. As others have pointed out, they
 use the symbol j for the square root of -1 to avoid confusion with the
 symbol i used for current.

I learned to use i for sqrt(-1) while studying theoretical physics.
When I later found myself teaching maths to engineers I asked why j
was used and was given this explanation. I'm still unconvinced by it
though. Physicists also have to deal with electric currents but they
can happily distinguish these two with upper and lower case. Upper
case J is used in electrodynamics for the free current density.
Lower case j is used by both engineers and physicists to denote a unit
vector in the y-direction though it would usually be in non-italic
bold-face when used in that context.

 I have never heard the term hypercomplex numbers. I guess you
 are referring to vectors with more dimensions than two. A three
 dimensional vector is described as having components in i,j,k
 directions. Although this is very like an extension of complex numbers
 into higher dimensions, the symbols used (i,j,k) are not the same
 as the i or j used for complex numbers. Instead they represent
 orthogonal unit vectors; which are similar in concept (because
 real and imaginary components of complex numbers are orthogonal),
 but not the *same*. So don't think of the i *or* j of a complex number
 being related to the i *and* j etc components of a vector.

 These are useful for example to describe three dimensional space, and
 scalar or vector functions in that space.

I've never heard the term hypercomplex before either although I did
intuitively understand its meaning (quaternions, octonions etc). These
are also useful in a number of contexts. For example, quaternions are
often used to represent orientations in 3D physical simulations
(including computer games). A quaternion is usually represented with
the basis i, j, k and 1 where a complex number would just have 1 and i
(or 1 and j if you're an engineer).


Oscar
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python 3.2 | WIndows 7 -- Multiprocessing and files not closing

2013-10-10 Thread Piet van Oostrum
Isaac Gerg isaac.g...@gergltd.com writes:

 Sorry, I am just providing pseudo code since I the code i have is quite large.

 As I mentioned, the code works fine when I remove the multirpcessing stuff so 
 the filename is not the issue (though you are right in your correction).

 Someone with the same problem posted a smaller, more complete example here:

Then you should give us real code (a minimal example) that we can try. You use 
myFunction in your example that isn't defined in your code. And by the way, why 
don't you close f just after reading? Or even better, use a with statement.
-- 
Piet van Oostrum p...@vanoostrum.org
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: I am never going to complain about Python again

2013-10-10 Thread Oscar Benjamin
On 10 October 2013 18:48, Neil Cerutti ne...@norwich.edu wrote:
 I guess the if appropriate part eluded my eye. When *is* it
 appropriate? Apparently not during an equal test.

 5.0 == abs(3 + 4j)
 False

If the above is genuine output then it's most likely floating point
error. I wouldn't expect any errors in that though. What version of
Python are you using and on what OS/hardware?

I get the following in Python 2.7 and 3.2 on Ubuntu 12.04 with a
32-bit AMD processor:
 5.0 == abs(3+4j)
True


Oscar
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: I am never going to complain about Python again

2013-10-10 Thread Neil Cerutti
On 2013-10-10, Oscar Benjamin oscar.j.benja...@gmail.com wrote:
 On 10 October 2013 18:48, Neil Cerutti ne...@norwich.edu wrote:
 I guess the if appropriate part eluded my eye. When *is* it
 appropriate? Apparently not during an equal test.

 5.0 == abs(3 + 4j)
 False

 If the above is genuine output then it's most likely floating point
 error. I wouldn't expect any errors in that though. What version of
 Python are you using and on what OS/hardware?

 I get the following in Python 2.7 and 3.2 on Ubuntu 12.04 with a
 32-bit AMD processor:
 5.0 == abs(3+4j)
 True

I get the same thing. I was apparently quite confused.

-- 
Neil Cerutti
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: UnicodeEncodeError: SOLVED

2013-10-10 Thread Walter Hurry
On Thu, 10 Oct 2013 01:47:52 +, Steven D'Aprano wrote:

 On Wed, 09 Oct 2013 14:41:53 +, Walter Hurry wrote:
 
 Many thanks to those prepared to forgive my transgression in the
 'Goodbye' thread. I mentioned there that I was puzzled by a
 UnicodeEncodeError, and said I would rise it as a separate thread.
 
 However, via this link, I was able to resolve the issue myself:
 
 http://stackoverflow.com/questions/3224268/python-unicode-encode-error
 
 I don't know what problem you had, and what your solution was, but the
 above link doesn't solve the problem, it just throws away data until the
 problem no longer appears, and never mind if it changes the semantics of
 the XML data.
 
 Instead of throwing away data, the right solution is likely to be, stop
 trying to deal with XML yourself, and use a proper UTF-8 compliant XML
 library.
 
 Or if you can't do that, at least open and read the XML file using UTF-8
 in the first place. In Python 3, you can pass a codec to open. In Python
 2, you can use codecs.open instead of the built-in open.

All true, but in *this* case, simply discarding the offending character 
was sufficient. Thanks anyway.

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


Re: Python 3.2 | WIndows 7 -- Multiprocessing and files not closing

2013-10-10 Thread Isaac Gerg
Hi Piet,

Here is a real code example: 
http://stackoverflow.com/questions/948119/preventing-file-handle-inheritance-in-multiprocessing-lib

As I said before, I had provide pseudocode.

I cannot close the file after reading because it is part of a class and other 
threads may be calling member functions which read from the file.

Isaac
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to make Tkinter Listbox entry insensitive?

2013-10-10 Thread Jason Swails
On Thu, Oct 10, 2013 at 2:29 PM, Skip Montanaro s...@pobox.com wrote:

  Removing inappropriate entries is not much of a hack.

 True, but then I have to go through the trouble of adding them back in
 should they become valid again. :-)


It seems that this could be handled fairly straight-forwardly by
subclassing either Listbox or Frame to implement your own, custom widget.
 The trick is to retain references to every entry within the widget, but
only embed it in the viewable area if it happens to be a valid entry at
that point.  Then all that's left is to hook events up to the proper
callbacks that implement various actions of your custom widgets using what
Tkinter is capable of doing.

Personally I prefer to subclass Frame since it allows me the maximum
flexibility (I think 90+% of the widgets I've written for my own
Tkinter-based programs do this).

All the best,
Jason
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: class implementation

2013-10-10 Thread Cameron Simpson
On 10Oct2013 11:34, markot...@gmail.com markot...@gmail.com wrote:
 OK so I did a took time of and read the pykkar code through. abd
 I found that there is a third class i have to implement.
 This Is the pykkar sourcecode
[... lots and lots of docstring and code ...]
[... and finally a little more messgae ...]
 I did not wan to but the source in here because it is just so god
 damn long. But some of you wanted it, so here it is :D

As a matter of readability, if I really need to include a huge body
of text I append it below the end of my message, and say something
like this (pretending I were writing your message):

  OK so I did a took time of and read the pykkar code through. abd
  I found that there is a third class i have to implement.

  I've appended the relevant pykkar source below this message.

  So I have come up with this code: [...]

That way your message does not get hidden by the (overly long IMO)
included material and readers can get on with looking at your stuff,
knowing that if necessary they can wade through the other stuff.

Cheers,
-- 
Cameron Simpson c...@zip.com.au

Any profit should go to Arnie's `get the daemon carved on Mount Rushmore' fund.
- Marty Albini, DOD0550, mar...@sdd.hp.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: I am never going to complain about Python again

2013-10-10 Thread Cameron Simpson
On 10Oct2013 17:48, Neil Cerutti ne...@norwich.edu wrote:
 On 2013-10-10, MRAB pyt...@mrabarnett.plus.com wrote:
  If r is real (float) and c is complex:
   r == c means r == c.real and c.imag == 0.0
 
 Woah. I thought I was going by what the docs say:
 
   Python fully supports mixed arithmetic: when a binary
   arithmetic operator has operands of different numeric types,
   the operand with the “narrower” type is widened to that of the
   other, where integer is narrower than floating point, which is
   narrower than complex. Comparisons between numbers of mixed
   type use the same rule. [2] The constructors int(), float(),
   and complex() can be used to produce numbers of a specific
   type.
 
 [...]
 
   [2] Not for complex numbers. Instead convert to floats using
  abs() if appropriate.
 
 I guess the if appropriate part eluded my eye. When *is* it
 appropriate? Apparently not during an equal test.

I must say that I read the footnote [2] as a directive to the
programmer. If you need to do this, a good way is to compare
magnitudes is appropriate.

Cheers,
-- 
Cameron Simpson c...@zip.com.au

My life is a never ending battle for the forces of good.
Unfortunately, unlike many other crusaders for righteousness, in my
system of morality, the right thing to do is very often to sit around
reading the paper or watching TV.   - Tim_Mefford t...@physics.orst.edu
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Skipping decorators in unit tests

2013-10-10 Thread Cameron Simpson
On 10Oct2013 07:00, Gilles Lenfant gilles.lenf...@gmail.com wrote:
 (explaining the title) : my app has functions and methods (and
 maybe classes in the future) that are decorated by decorators
 provided by the standard library or 3rd party packages.
 
 But I need to test undecorated functions and methods in my unit tests, 
 preferably without adding special stuffs in my target tested modules.
 
 Can someone point out good practices or dedicated tools that remove 
 temporarily the decorations.
 I pasted a small example of what I heed at http://pastebin.com/20CmHQ7Y

Speaking for myself, I would be include to recast this code:

  @absolutize
  def addition(a, b):
  return a + b

into:

  def _addition(a, b):
  return a + b

  addition = absolutize(_addition)

Then you can unit test both _addition() and addition().

And so forth.

Cheers,
-- 
Cameron Simpson c...@zip.com.au

1st Law Economists: For every economist there exists an equal and opposite
economist.
2nd Law Economists: They're both always wrong!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Complex literals (was Re: I am never going to complain about Python again)

2013-10-10 Thread David
On 11 October 2013 06:29, Oscar Benjamin oscar.j.benja...@gmail.com wrote:

 I learned to use i for sqrt(-1) while studying theoretical physics.
 When I later found myself teaching maths to engineers I asked why j
 was used and was given this explanation. I'm still unconvinced by it
 though.

Please don't be. We need different symbols to distinguish between so
many different aspects of current (average, dynamic, instantaneous,
rms, peak, sinusoidal-amplitude, sinusoidal-phasor) that we use up all
possible variations of bold, italic, subscript just to distinguish those
different aspects of i. It gets confusing enough as it is, because typically
we are describing many current variables (in one or more of the above
aspects) simultaneously, not just one. And the same holds for current
density, but less common. That's why we prefer j for sqrt(-1), not because
we are unconvincing :)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python 3.2 | WIndows 7 -- Multiprocessing and files not closing

2013-10-10 Thread mapoe
On Thu, 10 Oct 2013 08:31:21 -0700, Isaac Gerg wrote:

 I have a function that looks like the following:
 
 #-
 filename = 'c:\testfile.h5'
 f = open(filename,'r')
 data = f.read()

it seems kind of obvious from your sample:
add: f.close()

 q = multiprocessing.Queue()
 p = multiprocess.Process(target=myFunction,args=(data,q))
 p.start()
 result = q.get()
 p.join()
 q.close()
 
 f.close()
 
 os.remove(filename)
 #-
 
 When I run this code, I get an error on the last line when I try to
 remove the file.  It tells me that someone has access to the file.  When
 I remove the queue and multiprocessing stuff, the function works fine.
 
 What is going on here?
 
 Thanks in advance,
 Isaac

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


Re: Python 3.2 | WIndows 7 -- Multiprocessing and files not closing

2013-10-10 Thread mapoe
On Thu, 10 Oct 2013 18:16:07 -0500, mapoe wrote:

 On Thu, 10 Oct 2013 08:31:21 -0700, Isaac Gerg wrote:
 
 I have a function that looks like the following:
 
 #-
 filename = 'c:\testfile.h5'
 f = open(filename,'r')
 data = f.read()
 
 it seems kind of obvious from your sample:
 add: f.close()
should have read a little bit further :)


but I would close the file right after I have read all the data



 
 q = multiprocessing.Queue()
 p = multiprocess.Process(target=myFunction,args=(data,q))
 p.start()
 result = q.get()
 p.join()
 q.close()
 
 f.close()
 
 os.remove(filename)
 #-
 
 When I run this code, I get an error on the last line when I try to
 remove the file.  It tells me that someone has access to the file. 
 When I remove the queue and multiprocessing stuff, the function works
 fine.
 
 What is going on here?
 
 Thanks in advance,
 Isaac

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


Re: class implementation

2013-10-10 Thread Piet van Oostrum
markot...@gmail.com writes:


 OK so I did a took time of and read the pykkar code through. abd I found that 
 there is a third class i have to implement. 

[...]

 So I have come up with this code
 from pykkar import *

 create_world(
 
 #  #
 # v#
 #  #
 #  #
 #  #
 
 )

 class world_täiend(World):
 def left(self):
 world.excecute(left)
 
 class pykkar_täiend(Pykkar):
 def left(self):
 self._world.excecute(left)

 class _WorldProper_täiend(_WorldProper):
 def _cmd_right(self):

Should that not be _cmd_left?

 headings = (N,E,S,W)
 cur_tile = self._get_current_tile() 
 
 cur_heading_index = headings.index(cur_tile.pykkar_heading)
 new_heading_index = (cur_heading_index - 1) % 4
 cur_tile.pykkar_heading = headings[new_heading_index]
 
 self._update_pykkar_image(cur_tile)


 left()

 When I run my code I get this error.
 Traceback (most recent call last):
   File C:\Users\MarkoPC\Desktop\python\pykkar_test.py, line 21, in module
 class _WorldProper_täiend(_WorldProper):
 NameError: name '_WorldProper' is not defined

from import * doesn't import names that start with underscore (_). So therefore 
_WorldProper is not defined.

from import * is considered bad practice anyway. It is better just to import 
the things you need.

from pykkar import World, Pykkar, _WorldProper

I have looked a bit in this pykkar.py and I think it is badly structured for 
extension. The three classes are too strongly connected and it is difficult to 
get three subclasses connected in the proper way without duplicating code. But 
anyway you will have to do that when you create your world.

-- 
Piet van Oostrum p...@vanoostrum.org
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Skipping decorators in unit tests

2013-10-10 Thread Ned Batchelder

On 10/10/13 6:12 PM, Cameron Simpson wrote:

On 10Oct2013 07:00, Gilles Lenfant gilles.lenf...@gmail.com wrote:

(explaining the title) : my app has functions and methods (and
maybe classes in the future) that are decorated by decorators
provided by the standard library or 3rd party packages.

But I need to test undecorated functions and methods in my unit tests, preferably 
without adding special stuffs in my target tested modules.

Can someone point out good practices or dedicated tools that remove 
temporarily the decorations.
I pasted a small example of what I heed at http://pastebin.com/20CmHQ7Y

Speaking for myself, I would be include to recast this code:

   @absolutize
   def addition(a, b):
   return a + b

into:

   def _addition(a, b):
   return a + b

   addition = absolutize(_addition)

Then you can unit test both _addition() and addition().

And so forth.

Cheers,


I have to admit I'm having a hard time understanding why you'd need to 
test the undecorated functions.  After all, the undecorated functions 
aren't available to anyone.  All that matters is how they behave with 
the decorators.


But my imagination is weak: do you mind explaining more about what the 
functions do, what the decorators do, and why you need to test the 
undecorated functions?  I'll learn something, and with more information, 
we might be able to find a better solution.


--Ned.

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


Re: class implementation

2013-10-10 Thread Ben Finney
Piet van Oostrum p...@vanoostrum.org writes:

 from import * is considered bad practice anyway. It is better just to import 
 the things you need.

 from pykkar import World, Pykkar, _WorldProper

Or, even better, be explicit:

import pykkar

…

foo = pykkar.World()

-- 
 \  “Computer perspective on Moore's Law: Human effort becomes |
  `\   twice as expensive roughly every two years.” —anonymous |
_o__)  |
Ben Finney

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


Re: closure = decorator?

2013-10-10 Thread Roy Smith
In article m2a9ihxf3a@cochabamba.vanoostrum.org,
 Piet van Oostrum p...@vanoostrum.org wrote:

 I usually say that a closure is a package, containing a function with
 some additional data it needs. The data usually is in the form of name
 bindings.

That's pretty close to the way I think about it.  The way it was 
originally described to me is, A closure is a function bundled up with 
it's arguments.

To make a real-life analogy, let's say you're modeling a parking garage.  
I want to be able to walk up to the attendant and say, Please bring my 
car around front at 5 O'Clock.  It's that one (pointing to the slightly 
dented green Ford in spot 37).  So, you've got a class:

class DeliveryRequest:
   def __init__(self, spot, time):
  self.spot = spot
  self.time = time

Now, over the course of the day, the garage attendants shuffle cars 
around to make room and retrieve vehicles that packed in the back.  
Comes 5 O'Clock, what vehicle do you want the attendant to deliver to 
the front?  The one that was in spot 37 at the time you made the 
request, or the one that's in spot 37 at 5 O'Clock?

Unless you want somebody else's car (perhaps you'd like something better 
than a slightly dented Ford), you want the attendant to capture the 
current state of spot 37 and remember that until 5 O'Clock when it's 
time to go get the car, no matter where it happens to be right now.

That's a closure.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: I am never going to complain about Python again

2013-10-10 Thread Roy Smith
In article bbo7j0fbp3...@mid.individual.net,
 Neil Cerutti ne...@norwich.edu wrote:

  5.0 == abs(3 + 4j)
 False

I'd like an argument, please.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to make Tkinter Listbox entry insensitive?

2013-10-10 Thread Skip Montanaro
 It seems that this could be handled fairly straight-forwardly by
subclassing either Listbox or Frame to implement your own, custom widget.

I will freely admit that I am far from facile with Tk widgets. I've been
using GTK for most GUI apps for a long while (when I need to write such
things), but simple lists are such a PITA with GTK that I ran screaming to
Tkinter. If this small application gets much more complex, I will take a
look at subclassing Listbox.

Skip
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Skipping decorators in unit tests

2013-10-10 Thread Terry Reedy

On 10/10/2013 10:00 AM, Gilles Lenfant wrote:

To add to the other two responses so far...


(explaining the title) : my app has functions and methods (and maybe classes in 
the future) that are decorated by decorators provided by the standard library 
or 3rd party packages.

But I need to test undecorated functions and methods in my unit tests, preferably 
without adding special stuffs in my target tested modules.


Let's assume that the decorator wraps the function in a way that the 
wrapper has a reference to the original function, so it does not disappear.



Can someone point out good practices or dedicated tools that remove 
temporarily the decorations.


The easiest thing would be to have the decorator add the original 
function as an attribute .wrapped to the wrapper. Then test foo.wrapped. 
If you do not like this 'special stuff', then you would have to 
introspect the wrapper to access the wrapped function. How to do that 
depends on the wrapper.


--
Terry Jan Reedy

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


Re: Complex literals (was Re: I am never going to complain about Python again)

2013-10-10 Thread Steven D'Aprano
On Fri, 11 Oct 2013 00:25:27 +1100, Chris Angelico wrote:

 On Fri, Oct 11, 2013 at 12:09 AM, Roy Smith r...@panix.com wrote:
 BTW, one of the earliest things that turned me on to Python was when I
 discovered that it uses j as the imaginary unit, not i.  All
 right-thinking people will agree with me on this.
 
 I've never been well-up on complex numbers; can you elaborate on this,
 please? All I know is that I was taught that the square root of -1 is
 called i, and that hypercomplex numbers include i, j, k, and maybe even
 other terms, and I never understood where j comes from. Why is Python
 better for using j?

Being simple souls and not Real Mathematicians, electrical engineers get 
confused by the similarity between I (current) and i (square root of -1), 
so they used j instead. Real Mathematicians are hardy folk completely at 
home with such ambiguity -- if you can deal with superscript -1 meaning 
both inverse function and reciprocal *in the same equation*, i vs I 
hold no fears for you.

wink

But seriously... I think the convention to use j for complex numbers 
comes from the convention of using i, j, k as unit vectors, i being in 
the X direction (corresponding to the real axis), j being in the Y 
direction (corresponding to the imaginary axis), and k being in the Z 
direction.

For what it's worth, there is no three-dimensional extension to complex 
numbers, but there is a four-dimensional one, the quaternions or 
hypercomplex numbers. They look like 1 + 2i + 3j + 4k, where i, j and k 
are all distinct but i**2 == j**2 == k**2 == -1. Quaternions had a brief 
period of popularity during the late 19th century but fell out of 
popularity in the 20th. In recent years, they're making something of a 
comeback, as using quaternions for calculating rotations is more 
numerically stable than traditional matrix calculations.

Unlike reals and complex numbers, quaternions are non-commutative: in 
general, q1*q2 != q2*q1.

There are also octonions, eight-dimensional numbers which are non-
commutative and non-associative, (o1*o2)*o3 != o1*(o2*o3), and sedenions, 
a 16-dimensional number.



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


Re: Python's and and Pythons or

2013-10-10 Thread Terry Reedy

On 10/10/2013 9:33 AM, Ethan Furman wrote:


On 10/10/2013 12:43 AM, Terry Reedy wrote:



On 10/10/2013 2:45 AM, Chris Angelico wrote:

first_element = some_list[0]# Oops, may crash



some_list[0:1] always works, and sometimes is usable, but you still
cannot index the slice.



Not if some_list is None, False, or another non-indexable type.


Did you really not understand that some_list is intended to be a list? 
Just like my_string, for instance, would be a string? Chris's statement 
further specifies some_list as a list that is expected to not be empty, 
but might be -- so one has to guard against the possibility.


The trick of slicing instead of indexing in this context is not obvious 
to everyone learning Python. Most other languages only have indexing. I 
learned the trick years ago when someone posted it on this list.


--
Terry Jan Reedy

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


Re: Python 3.2 | WIndows 7 -- Multiprocessing and files not closing

2013-10-10 Thread Terry Reedy

On 10/10/2013 2:49 PM, Isaac Gerg wrote:


Additionally, is there a place on the web to view this conversation and
reply?  Currently, I am only able to access this list through email.


news.gmane.org newsgroup gmane.lang.python.general
Look at the headers for this message.
The site also has a searchable archive.

--
Terry Jan Reedy

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


Re: I am never going to complain about Python again

2013-10-10 Thread Steven D'Aprano
On Thu, 10 Oct 2013 09:09:42 -0400, Roy Smith wrote:

 BTW, here's a Python equality oddity:
 
 r = 0.0
 c = 0 + 0j
 r == c
 True

Mathematically, this is only to be expected.


 int(r) == int(c)
 Traceback (most recent call last):
   File stdin, line 1, in module
 TypeError: can't convert complex to int


This is also to be expected. What should int(a+bj) return?

★  int(a) + int(b)j

★  int(a)

★  int(b)

★  int(abs(a + bj))


It's quite ambiguous, there is no obvious mapping from complex to 
integers, and so should raise an exception.


 
 If x == y, then f(x) should also equal f(y).

Not necessarily. If x and y are different types, which they are here, 
then function f may be defined on one type but not the other. Which is 
exactly the case with int() on floats and complexes.


 More specifically, if x == y,
 and x is in the domain of f(), then y should also be in the domain of
 f().

Incorrect. By definition, complex numbers are in the Complex domain, not 
the Real domain.

Your mistake here seems to be that you're assuming that if two numbers 
are equal, they must be in the same domain, but that's not the case. 
(Perhaps you think that 0.0 == 0+0j should return False?) It's certainly 
not the case when it comes to types in Python, and it's not even the case 
in mathematics. Given:

x ∈ ℝ, x = 2  (reals)
y ∈ ℕ, y = 2  (natural numbers)

we have x = y, but since 1/y is undefined (there is no Natural number 
1/2), 1/x != 1/y.

Now, arguably in this case we could implicitly promote y to the reals 
before performing the division. I would consider that acceptable, since 
there is only one way to do the promotion: natural 2 - real 2. But going 
the other way certainly isn't: demoting real x to the naturals is 
ambiguous, and even if it weren't, then declaring that 1/x isn't defined 
would make the whole exercise pointless.

Bringing this back to the initial example of int(0.0) == int(0+0j), to 
have this work the way you want would require demoting the complex number 
to the reals, and that is ambiguous. There are three distinct ways to do 
this: take the real part, the imaginary part, or the absolute value. That 
makes the operation demote to real ambiguous, the mere fact that all 
three operations would happen to give the same result for this particular 
number is irrelevant.


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


Re: super in Python 3 and variadic arguments

2013-10-10 Thread Steven D'Aprano
On Thu, 10 Oct 2013 07:04:38 -0400, Ned Batchelder wrote:

 super() with no args is a kind of hack to begin with.  It involves a
 special case in the compiler (so that using the name super as a
 function call will act as if you had accessed the name __class__ so
 that super can find it later), and inspecting the stack frame during
 execution.

super() with no arguments is *completely* a hack[1], and one where GvR 
has said Never again! if I remember correctly. I don't think he regrets 
allowing the super compile-time magic, just that it really is magic and 
he doesn't want to make a habit of it.

One of the side-effects of this being a hack is that this doesn't work:

class X(Y):
def method(self, arg):
f = super
f().method(arg)





[1] Which is not necessarily a bad thing!

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


Re: I am never going to complain about Python again

2013-10-10 Thread Steven D'Aprano
On Thu, 10 Oct 2013 17:48:16 +, Neil Cerutti wrote:

  5.0 == abs(3 + 4j)
  False

Did you maybe accidentally rebind abs? If not, what version of Python are 
you using?


[steve@ando ~]$ for a in 2.4 2.5 2.6 2.7 3.2 3.3 ; do
 python$a -c print( 5.0 == abs(3 + 4j) ) ;
 done
True
True
True
True
True
True



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


Re: super in Python 3 and variadic arguments

2013-10-10 Thread Ian Kelly
On Thu, Oct 10, 2013 at 8:11 PM, Steven D'Aprano
steve+comp.lang.pyt...@pearwood.info wrote:
 One of the side-effects of this being a hack is that this doesn't work:

 class X(Y):
 def method(self, arg):
 f = super
 f().method(arg)

Actually, that works just fine.  The compiler sees that super is
accessed within the method and creates the closure necessary to make
it work.  This does fail, however:

f = super
class X(Y):
def method(self, arg):
f().method(arg)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Skipping decorators in unit tests

2013-10-10 Thread Cameron Simpson
On 10Oct2013 19:44, Ned Batchelder n...@nedbatchelder.com wrote:
 On 10/10/13 6:12 PM, Cameron Simpson wrote:
 Speaking for myself, I would be include to recast this code:
 
@absolutize
def addition(a, b):
return a + b
 
 into:
 
def _addition(a, b):
return a + b
 
addition = absolutize(_addition)
 
 Then you can unit test both _addition() and addition(). [...]
 
 I have to admit I'm having a hard time understanding why you'd need
 to test the undecorated functions.  After all, the undecorated
 functions aren't available to anyone.  All that matters is how they
 behave with the decorators.

If the undecorated function is buggy, the decorated function will
be buggy. But the bug will be harder to resolve, and if you're
especially lucky the decorator will often-but-not-always conceal
the bug in the inner function.

Wanting to test the core function is perfectly reasonable. You can in
principle write simpler and more direct tests of the core function.

Having an error report that points directly at an error instead of
an error report that points at some outer dysfunction (i.e. somewhere
deep inside here something is broken) is highly desirable in
general, and therefore also in a test suite.

Cheers,
-- 
Cameron Simpson c...@zip.com.au

They shouldn't get any new nuclear weapons until they've used the ones
they've got.- Murff
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python's and and Pythons or

2013-10-10 Thread Ethan Furman

On 10/10/2013 06:41 PM, Terry Reedy wrote:

On 10/10/2013 9:33 AM, Ethan Furman wrote:


On 10/10/2013 12:43 AM, Terry Reedy wrote:



On 10/10/2013 2:45 AM, Chris Angelico wrote:

first_element = some_list[0]# Oops, may crash



some_list[0:1] always works, and sometimes is usable, but you still
cannot index the slice.



Not if some_list is None, False, or another non-indexable type.


Did you really not understand that some_list is intended to be a list? Just 
like my_string, for instance, would be a
string? Chris's statement further specifies some_list as a list that is 
expected to not be empty, but might be -- so one
has to guard against the possibility.


I understood it just fine.  I'm also aware that at some point, in some program, 
it will be None (and it won't be a bug ;).



The trick of slicing instead of indexing in this context is not obvious to 
everyone learning Python. Most other
languages only have indexing. I learned the trick years ago when someone posted 
it on this list.


It's a good trick, I use it myself.

--
~Ethan~
--
https://mail.python.org/mailman/listinfo/python-list


Re: Skipping decorators in unit tests

2013-10-10 Thread Steven D'Aprano
On Fri, 11 Oct 2013 09:12:38 +1100, Cameron Simpson wrote:

 On 10Oct2013 07:00, Gilles Lenfant gilles.lenf...@gmail.com wrote:
 (explaining the title) : my app has functions and methods (and maybe
 classes in the future) that are decorated by decorators provided by the
 standard library or 3rd party packages.
 
 But I need to test undecorated functions and methods in my unit
 tests, preferably without adding special stuffs in my target tested
 modules.
 
 Can someone point out good practices or dedicated tools that remove
 temporarily the decorations. I pasted a small example of what I heed
 at http://pastebin.com/20CmHQ7Y
 
 Speaking for myself, I would be include to recast this code:
 
   @absolutize
   def addition(a, b):
   return a + b
 
 into:
 
   def _addition(a, b):
   return a + b
 
   addition = absolutize(_addition)
 
 Then you can unit test both _addition() and addition().

*shudders*

Ew ew ew ew.


I would much rather do something like this:


def undecorate(f):
Return the undecorated inner function from function f.
return f.func_closure[0].cell_contents

def decorate(func):
def inner(arg):
return func(arg) + 1
return inner

@decorate
def f(x):
return 2*x



And in use:


py f(100)
201
py undecorate(f)(100)
200



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


Re: super in Python 3 and variadic arguments

2013-10-10 Thread Steven D'Aprano
On Thu, 10 Oct 2013 20:33:37 -0600, Ian Kelly wrote:

 On Thu, Oct 10, 2013 at 8:11 PM, Steven D'Aprano
 steve+comp.lang.pyt...@pearwood.info wrote:
 One of the side-effects of this being a hack is that this doesn't work:

 class X(Y):
 def method(self, arg):
 f = super
 f().method(arg)
 
 Actually, that works just fine.  The compiler sees that super is
 accessed within the method and creates the closure necessary to make it
 work.  This does fail, however:
 
 f = super
 class X(Y):
 def method(self, arg):
 f().method(arg)



Ah, that's the one! Thanks for the correction.

I'll now go and write I will always test my code snippets before 
posting on the blackboard one hundred times.



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


Re: Skipping decorators in unit tests

2013-10-10 Thread Roy Smith
On 10Oct2013 19:44, Ned Batchelder n...@nedbatchelder.com wrote:
  I have to admit I'm having a hard time understanding why you'd need
  to test the undecorated functions.  After all, the undecorated
  functions aren't available to anyone.  All that matters is how they
  behave with the decorators.

In article mailman.978.1381459605.18130.python-l...@python.org,
 Cameron Simpson c...@zip.com.au wrote:
 If the undecorated function is buggy, the decorated function will
 be buggy. But the bug will be harder to resolve, and if you're
 especially lucky the decorator will often-but-not-always conceal
 the bug in the inner function.

And there lies the fundamental white-box vs. black-box testing conundrum.

The black-box camp (whose flag Ned is flying) says, There is an exposed 
interface which accepts certain inputs and promises certain outputs.  
That's all you know, that's all you ever can know, and that's all you 
should ever want to know.  The interface is constant.  The guts can 
change without notice.  That's a perfectly valid philosophy.

The white-box camp (under which banner Cameron rides) says, There's a 
lot of neat stuff under the covers, and I can do a better, faster, and 
more complete testing job if I take advantage of my knowledge of what's 
under the kimono.  That, too, is a valid philosophy.

We now return to reality, already in progress.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Skipping decorators in unit tests

2013-10-10 Thread Cameron Simpson
On 11Oct2013 02:55, Steven D'Aprano steve+comp.lang.pyt...@pearwood.info 
wrote:
 On Fri, 11 Oct 2013 09:12:38 +1100, Cameron Simpson wrote:
  Speaking for myself, I would be include to recast this code:
  
@absolutize
def addition(a, b):
return a + b
  
  into:
  
def _addition(a, b):
return a + b
addition = absolutize(_addition)
  
  Then you can unit test both _addition() and addition().
 
 *shudders*
 Ew ew ew ew.

Care to provide some technical discourse here? Aside from losing the neat
and evocative @decorator syntax, the above is simple and overt.

 I would much rather do something like this:
 
 def undecorate(f):
 Return the undecorated inner function from function f.
 return f.func_closure[0].cell_contents

Whereas this feels like black magic. Is this portable to any decorated
function? If so, I'd have hoped it was in the stdlib. If not: black magic.

 And in use:
 
 py f(100)
 201
 py undecorate(f)(100)
 200

All lovely, provided you can convince me that undecorate() is robust.
(And if you can, I'll certainly be filing it away in my funcutils
module for later use.)

Cheers,
-- 
Cameron Simpson c...@zip.com.au

DRM doesn't inconvenience pirates ¿ indeed, over time it trains
law-abiding users to become pirates out of sheer frustration.
- Charles Stross
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Skipping decorators in unit tests

2013-10-10 Thread Ben Finney
Cameron Simpson c...@zip.com.au writes:

 On 11Oct2013 02:55, Steven D'Aprano steve+comp.lang.pyt...@pearwood.info 
 wrote:
  def undecorate(f):
  Return the undecorated inner function from function f.
  return f.func_closure[0].cell_contents

 Whereas this feels like black magic. Is this portable to any decorated
 function? If so, I'd have hoped it was in the stdlib. If not: black
 magic.

What would you expect? The purpose of decorating functions is to do
magic to make it appear as though the original function isn't there any
more. Any technique to getting at that original function anyway is *of
course* going to look like black magic at the implementation level.

-- 
 \“What if the Hokey Pokey IS what it's all about?” —anonymous |
  `\   |
_o__)  |
Ben Finney

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


Is this the room for an argument?

2013-10-10 Thread John Ladasky
On Thursday, October 10, 2013 5:07:11 PM UTC-7, Roy Smith wrote:
 I'd like an argument, please.

'Receptionist' (Rita Davies) - Yes, sir?
'Man' (Michael Palin) - I'd like to have an argument please.
'Receptionist' - Certainly sir, have you been here before...?
'Man' - No, this is my first time.
'Receptionist' - I see. Do you want to have the full argument, or were you 
thinking of taking the course?
'Man' - Well, what would be the cost?
'Receptionist' - Yes, it's one pound for a five-minute argument, but only eight 
pounds for a course of ten.
'Man' - Well, I think it's probably best if I start with the five-minute one 
and see how it goes from there. OK?
'Receptionist' - Fine - I'll see who's free at the moment...Mr.Du-Bakey's free, 
but he's a little bit concilliatory...Yes, try Mr.Barnard - Room 12.
'Man' - Thank you.

:^)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Skipping decorators in unit tests

2013-10-10 Thread Cameron Simpson
On 11Oct2013 14:42, Ben Finney ben+pyt...@benfinney.id.au wrote:
 Cameron Simpson c...@zip.com.au writes:
  On 11Oct2013 02:55, Steven D'Aprano steve+comp.lang.pyt...@pearwood.info 
  wrote:
   def undecorate(f):
   Return the undecorated inner function from function f.
   return f.func_closure[0].cell_contents
 
  Whereas this feels like black magic. Is this portable to any decorated
  function? If so, I'd have hoped it was in the stdlib. If not: black
  magic.
 
 What would you expect? The purpose of decorating functions is to do
 magic to make it appear as though the original function isn't there any
 more. Any technique to getting at that original function anyway is *of
 course* going to look like black magic at the implementation level.

Sigh. Yes of course. It is no uglier than walking a frame stack etc.

But is it reliable? Will it work on any decorated function?

If so, fine, and I'd be happy to squirrel it away as a useful
standard incantation for something.

If not, then it's nasty only-mostly-reliable magic and I either
want little to do with it, OR I want to know its specific limitations
so I know when to use it and when not.
-- 
Cameron Simpson c...@zip.com.au

But pessimism IS realism!   - D.L.Bahr
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Skipping decorators in unit tests

2013-10-10 Thread Steven D'Aprano
On Fri, 11 Oct 2013 14:13:19 +1100, Cameron Simpson wrote:

 On 11Oct2013 02:55, Steven D'Aprano
 steve+comp.lang.pyt...@pearwood.info wrote:
 On Fri, 11 Oct 2013 09:12:38 +1100, Cameron Simpson wrote:
  Speaking for myself, I would be include to recast this code:
  
@absolutize
def addition(a, b):
return a + b
  
  into:
  
def _addition(a, b):
return a + b
addition = absolutize(_addition)
  
  Then you can unit test both _addition() and addition().
 
 *shudders*
 Ew ew ew ew.
 
 Care to provide some technical discourse here? Aside from losing the
 neat and evocative @decorator syntax, the above is simple and overt.


What part of Ew ew ew ew was not technical enough for you? Would it 
help if I add a few extra ews? 

*wink*

But seriously, I don't like doubling the number of names in the namespace 
just for the sake of white-box testing. That means you have to somehow 
document each and every one of the private functions that they aren't for 
using, just for testing.

For the avoidance of doubt, I understand you flagged them as private. But 
even for private use within the module, they're not supposed to be used. 
They are only for testing. So now you have to have a naming convention 
and/or documentation to ensure that they aren't used internally.

If there really is a good use-case for using both the decorated and 
undecorated version of the function (whether internally, or as part of 
the public API), then I'm completely with you. We don't have to use 
decorator syntax if we have good reason to keep the wrapped and unwrapped 
functions separate, just bind them to separate names.

I also like Terry Reedy's suggestion of having the decorator 
automatically add the unwrapped function to the wrapped function as an 
attribute:

def decorate(func):
@functools.wraps(func)
def inner(arg):
blah blah
inner._unwrapped = func  # make it public if you prefer
return inner

which makes it all nice and clean and above board. (I seem to recall a 
proposal to have functools.wraps do this automatically...)


 I would much rather do something like this:
 
 def undecorate(f):
 Return the undecorated inner function from function f. return
 f.func_closure[0].cell_contents
 
 Whereas this feels like black magic. Is this portable to any decorated
 function? If so, I'd have hoped it was in the stdlib. If not: black
 magic.

Not every one-line function needs to be in the std lib :-)

To go into the std lib, it would need to be a tad more bullet-proof. For 
instance, it should have better error checking for the case where 
func_closure is None, rather than just raise the cryptic error message:

TypeError: 'NoneType' object is not subscriptable

If would also need to deal with arbitrary closures, where item 0 is not 
necessarily a function, or where there might be multiple functions, or no 
functions at all.

But for purely internal use within a test suite, we can afford to be a 
little more ad hoc and just deal with those cases when and if they occur. 
Since we're white-box testing, we presumably know which functions are 
decorated and which ones aren't (we can read the source code!), and will 
only call undecorate on those which are decorated.

 And in use:
 
 py f(100)
 201
 py undecorate(f)(100)
 200
 
 All lovely, provided you can convince me that undecorate() is robust.
 (And if you can, I'll certainly be filing it away in my funcutils module
 for later use.)

It needs error handling. It assumes that the closure only references a 
single object, namely the function being wrapped. In that sense, it's not 
ready for production as a public utility function. But as a private 
function for use only in testing, under controlled conditions, I think it 
is robust enough, it works in CPython 2.5 through 2.7 and IronPython 2.6. 
In Python 3.x, you have to change func_closure to __closure__, but 
otherwise it works in 3.2 and 3.3. Jython 2.5 seems to choke on the 
decorator syntax:


 @decorate
  File stdin, line 1
@decorate
^
SyntaxError: mismatched input 'EOF' expecting CLASS


which surely is a bug in Jython. If I apply the decorator manually, it 
works.



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


Re: Skipping decorators in unit tests

2013-10-10 Thread Steven D'Aprano
On Fri, 11 Oct 2013 15:36:29 +1100, Cameron Simpson wrote:

 But is it reliable? Will it work on any decorated function?

*Any* decorated function? No, of course not, since decorators can do 
anything they like:

def decorate(func):
return lambda *args: Surprise!

@decorate
def something_useful(x, y):
return x+y


They don't even have to return a function. Or the function being 
decorated can end up in a different cell:

def factory(x, y):
def decorator(func):
def inner(*args):
_ = (x, y)  # pretend x and y are useful
return func(*args)
return inner
return decorator


@factory(lambda: None, 42)
def func(a):
return a



py func.func_closure[0].cell_contents
42
py func.func_closure[1].cell_contents
function lambda at 0xb7c4609c
py func.func_closure[2].cell_contents
function func at 0xb7c4617c


So consider this a *cooperative* undecorator. It can only undecorate 
things that are decorated the way you expect them to be decorated. 


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


[issue9548] locale can be imported at startup but relies on too many library modules

2013-10-10 Thread Raymond Hettinger

Raymond Hettinger added the comment:

+1 This seems like a reasonable solution.

--

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



[issue19201] lzma and 'x' mode open

2013-10-10 Thread STINNER Victor

STINNER Victor added the comment:

 Being strict this would be 3.4 material,

Why? The patch is trivial, I don't how it could cause a regression.

If you don't want regression, add a unit test to test_lzma.py.

--
nosy: +haypo

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



  1   2   >