Re: List comprehension for testing **params

2012-11-12 Thread Ulrich Eckhardt
Am 11.11.2012 23:24, schrieb Cantabile: I'm writing a small mail library for my own use, and at the time I'm testing parameters like this: Let's ignore the facts that there is an existing mail library, that you should use real parameters if they are required and that exit() is completely

Re: List comprehension for testing **params

2012-11-12 Thread Joshua Landau
Just a few tricks you may have missed: On 12 November 2012 10:48, Ulrich Eckhardt ulrich.eckha...@dominolaser.comwrote: Am 11.11.2012 23:24, schrieb Cantabile: if required.intersection(params.**keys()) != required: if required.issubset(params): missing = required -

Re: List comprehension for testing **params

2012-11-12 Thread Joshua Landau
On 12 November 2012 13:23, Joshua Landau joshua.landau...@gmail.com wrote: Just a few tricks you may have missed: On 12 November 2012 10:48, Ulrich Eckhardt ulrich.eckha...@dominolaser.com wrote: Am 11.11.2012 23:24, schrieb Cantabile: if required.intersection(params.**keys()) !=

Re: List comprehension for testing **params

2012-11-12 Thread Cantabile
Wow, lots of things I had never heard of in your posts. I guess I need to do some homework... Cantabile -- http://mail.python.org/mailman/listinfo/python-list

List comprehension for testing **params

2012-11-11 Thread Cantabile
Hi, I'm writing a small mail library for my own use, and at the time I'm testing parameters like this: class Mail(object): def __init__(self, smtp, login, **params) blah blah required = ['Subject', 'From', 'To', 'msg'] for i in required: if not i

Re: List comprehension for testing **params

2012-11-11 Thread Ian Kelly
On Sun, Nov 11, 2012 at 3:24 PM, Cantabile cantabile...@wanadoo.fr wrote: I'd like to do something like that instead of the 'for' loop in __init__: assert[key for key in required if key in params.keys()] A list evaluates as true if it is not empty. As long as at least one of the required

Re: List comprehension for testing **params

2012-11-11 Thread Tim Chase
assert[key for key in required if key in params.keys()] ... Could you explain why it doesn't work and do you have any idea of how it could work ? Well, here, if any of the items are found, you get a list that is non-False'ish, so the assert passes. It sounds like you want all() (available as

Re: List comprehension for testing **params

2012-11-11 Thread Steven D'Aprano
On Sun, 11 Nov 2012 23:24:14 +0100, Cantabile wrote: Hi, I'm writing a small mail library for my own use, and at the time I'm testing parameters like this: class Mail(object): def __init__(self, smtp, login, **params) blah blah required = ['Subject',

Re: List comprehension for testing **params

2012-11-11 Thread Terry Reedy
On 11/11/2012 5:56 PM, Ian Kelly wrote: On Sun, Nov 11, 2012 at 3:24 PM, Cantabile cantabile...@wanadoo.fr wrote: I'd like to do something like that instead of the 'for' loop in __init__: assert[key for key in required if key in params.keys()] A list evaluates as true if it is not empty. As

Re: List comprehension for testing **params

2012-11-11 Thread Steven D'Aprano
On Sun, 11 Nov 2012 18:37:05 -0500, Terry Reedy wrote: or if you want them to be identified by keyword only (since 7 positional args is a bit much) def __init__(self, smtp, login, *, subject, from, to, msg): (I forget when this feature was added) It's a Python 3 feature. -- Steven --

Re: List comprehension for testing **params

2012-11-11 Thread Cantabile
Thanks everyone for your answers. That's much clearer now. I see that I was somehow fighting python instead of using it. Lesson learned (for the time being at least) :) I'll probably get back with more questions... Cheers, Cantabile -- http://mail.python.org/mailman/listinfo/python-list

Re: List comprehension for testing **params

2012-11-11 Thread Tim Chase
On 11/11/12 17:18, Steven D'Aprano wrote: but that leaves you with the next two problems: 2) Fixing the assert still leaves you with the wrong exception. You wouldn't raise a ZeroDivisionError, or a UnicodeDecodeError, or an IOError would you? No of course not. So why are you suggesting

Re: List comprehension for testing **params

2012-11-11 Thread Steven D'Aprano
On Sun, 11 Nov 2012 18:21:32 -0600, Tim Chase wrote: On 11/11/12 17:18, Steven D'Aprano wrote: but that leaves you with the next two problems: 2) Fixing the assert still leaves you with the wrong exception. You wouldn't raise a ZeroDivisionError, or a UnicodeDecodeError, or an IOError