By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,222 Members | 2,478 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,222 IT Pros & Developers. It's quick & easy.

Assertion in list comprehension

P: n/a
Hi,

Does anyone know how to put an assertion in list comprehension? I have
the following list comprehension, but I want to use an assertion to
check the contents of rec_stdl. I ended up using another loop which
essentially duplicates the functions of list comprehension. It just
look like a waste of coding and computer time to me.

I just wish I could put the assertions into list comprehensions.

x=[(rec_stdl[0].st/10000.0,
rec_stdl[0].cl,
rec_stdl[0].bb,
rec_stdl[0].bo,
rec_stdl[1].bb,
rec_stdl[1].bo,
rec_stdl[0].ex
)
for rec_stdl in rec_by_ex if len(rec_stdl)==2
]

#duplicated loop
if __debug__:
for rec_stdl in rec_by_ex:
l=len(rec_stdl)
assert(l<=2 and l>0)
if l==2:
assert(rec_stdl[0].c=="C" and rec_stdl[1].c=="P")
assert(rec_stdl[0].ex==rec_stdl[1].ex)
assert(rec_stdl[0].st==rec_stdl[1].st)
assert(rec_stdl[0].cp==rec_stdl[1].cp)

Thanks,
Geoffrey

Aug 1 '07 #1
Share this Question
Share on Google+
11 Replies


P: n/a
On Aug 1, 9:37 am, beginner <zyzhu2...@gmail.comwrote:
Hi,

Does anyone know how to put an assertion in list comprehension? I have
the following list comprehension, but I want to use an assertion to
check the contents of rec_stdl. I ended up using another loop which
essentially duplicates the functions of list comprehension. It just
look like a waste of coding and computer time to me.

I just wish I could put the assertions into list comprehensions.

x=[(rec_stdl[0].st/10000.0,
rec_stdl[0].cl,
rec_stdl[0].bb,
rec_stdl[0].bo,
rec_stdl[1].bb,
rec_stdl[1].bo,
rec_stdl[0].ex
)
for rec_stdl in rec_by_ex if len(rec_stdl)==2
]

#duplicated loop
if __debug__:
for rec_stdl in rec_by_ex:
l=len(rec_stdl)
assert(l<=2 and l>0)
if l==2:
assert(rec_stdl[0].c=="C" and rec_stdl[1].c=="P")
assert(rec_stdl[0].ex==rec_stdl[1].ex)
assert(rec_stdl[0].st==rec_stdl[1].st)
assert(rec_stdl[0].cp==rec_stdl[1].cp)

Thanks,
Geoffrey
Can't you just call a function from within your list comprehension and
do whatever you want for each item? Something like this (not tested):

def checker(item):
assert(len(item) <= 2 and len(item) 0)
if len(item) == 2:
assert(item[0].c == "C" and item[1].c == "P"

return len(item) == 2

x = [whatever for item in all_items if checker(item = item)]

Aug 1 '07 #2

P: n/a
On Aug 1, 11:09 am, "danmcle...@yahoo.com" <danmcle...@yahoo.com>
wrote:
On Aug 1, 9:37 am, beginner <zyzhu2...@gmail.comwrote:


Hi,
Does anyone know how to put an assertion in list comprehension? I have
the following list comprehension, but I want to use an assertion to
check the contents of rec_stdl. I ended up using another loop which
essentially duplicates the functions of list comprehension. It just
look like a waste of coding and computer time to me.
I just wish I could put the assertions into list comprehensions.
x=[(rec_stdl[0].st/10000.0,
rec_stdl[0].cl,
rec_stdl[0].bb,
rec_stdl[0].bo,
rec_stdl[1].bb,
rec_stdl[1].bo,
rec_stdl[0].ex
)
for rec_stdl in rec_by_ex if len(rec_stdl)==2
]
#duplicated loop
if __debug__:
for rec_stdl in rec_by_ex:
l=len(rec_stdl)
assert(l<=2 and l>0)
if l==2:
assert(rec_stdl[0].c=="C" and rec_stdl[1].c=="P")
assert(rec_stdl[0].ex==rec_stdl[1].ex)
assert(rec_stdl[0].st==rec_stdl[1].st)
assert(rec_stdl[0].cp==rec_stdl[1].cp)
Thanks,
Geoffrey

Can't you just call a function from within your list comprehension and
do whatever you want for each item? Something like this (not tested):

def checker(item):
assert(len(item) <= 2 and len(item) 0)
if len(item) == 2:
assert(item[0].c == "C" and item[1].c == "P"

return len(item) == 2

x = [whatever for item in all_items if checker(item = item)]- Hide quoted text -

- Show quoted text -
Good idea! Thank you!

Aug 1 '07 #3

P: n/a
On 8/1/07, beginner <zy*******@gmail.comwrote:
Hi,

Does anyone know how to put an assertion in list comprehension? I have
the following list comprehension, but I want to use an assertion to
check the contents of rec_stdl. I ended up using another loop which
essentially duplicates the functions of list comprehension. It just
look like a waste of coding and computer time to me.

I just wish I could put the assertions into list comprehensions.

x=[(rec_stdl[0].st/10000.0,
rec_stdl[0].cl,
rec_stdl[0].bb,
rec_stdl[0].bo,
rec_stdl[1].bb,
rec_stdl[1].bo,
rec_stdl[0].ex
)
for rec_stdl in rec_by_ex if len(rec_stdl)==2
]

#duplicated loop
if __debug__:
for rec_stdl in rec_by_ex:
l=len(rec_stdl)
assert(l<=2 and l>0)
if l==2:
assert(rec_stdl[0].c=="C" and rec_stdl[1].c=="P")
assert(rec_stdl[0].ex==rec_stdl[1].ex)
assert(rec_stdl[0].st==rec_stdl[1].st)
assert(rec_stdl[0].cp==rec_stdl[1].cp)
First: All your asserts are wrong. Assert is a statement, not a
function. These specific ones will behave as expected, but it's easy
to accidentally write ones that always pass this way.

Secondly: This is a waste of code, because if __debug__ is not defined
asserts will be skipped by the compiler. You could use the same loop
block for both branches.

Thirdly: This sort of testing is precisely what unit tests and/or
doctests are for.
Aug 1 '07 #4

P: n/a
On Wed, 01 Aug 2007 11:28:48 -0500, Chris Mellon wrote:
On 8/1/07, beginner <zy*******@gmail.comwrote:
>Hi,

Does anyone know how to put an assertion in list comprehension? I have
the following list comprehension, but I want to use an assertion to
check the contents of rec_stdl. I ended up using another loop which
essentially duplicates the functions of list comprehension. It just
look like a waste of coding and computer time to me.

I just wish I could put the assertions into list comprehensions.

x=[(rec_stdl[0].st/10000.0,
rec_stdl[0].cl,
rec_stdl[0].bb,
rec_stdl[0].bo,
rec_stdl[1].bb,
rec_stdl[1].bo,
rec_stdl[0].ex
)
for rec_stdl in rec_by_ex if len(rec_stdl)==2
]

#duplicated loop
if __debug__:
for rec_stdl in rec_by_ex:
l=len(rec_stdl)
assert(l<=2 and l>0)
if l==2:
assert(rec_stdl[0].c=="C" and rec_stdl[1].c=="P")
assert(rec_stdl[0].ex==rec_stdl[1].ex)
assert(rec_stdl[0].st==rec_stdl[1].st)
assert(rec_stdl[0].cp==rec_stdl[1].cp)

First: All your asserts are wrong. Assert is a statement, not a
function. These specific ones will behave as expected, but it's easy to
accidentally write ones that always pass this way.
Could you come up with an example? I can only think of accidentally
injecting a comma, what would create a (true, in a boolean context) tuple.

And, well, if you're only using () for readabilty, this might sometimes
look messy when calling assert with the extended syntax::

assert(False), "error text"

Where one could expect the construction of a tuple.
Secondly: This is a waste of code, because if __debug__ is not defined
asserts will be skipped by the compiler. You could use the same loop
block for both branches.
Well, the `assert` isn't there for no reason, but if you're serious about
it, `raise` could be better.
Thirdly: This sort of testing is precisely what unit tests and/or
doctests are for.
Huh? What beginner is doing there seems more like input validation than
testing. Unit or doctests are meant for testing (and in case of doctests,
showing) whether a function works as expected.
Aug 1 '07 #5

P: n/a
On Wed, 01 Aug 2007 16:55:53 +0000, Stargaming wrote:
>Thirdly: This sort of testing is precisely what unit tests and/or
doctests are for.

Huh? What beginner is doing there seems more like input validation than
testing. Unit or doctests are meant for testing (and in case of doctests,
showing) whether a function works as expected.
If it is input validation I wouldn't expect it protected by a ``if
__debug__:``. That looks more like debugging/testing.

Ciao,
Marc 'BlackJack' Rintsch
Aug 1 '07 #6

P: n/a
On Aug 1, 11:28 am, "Chris Mellon" <arka...@gmail.comwrote:
On 8/1/07, beginner <zyzhu2...@gmail.comwrote:


Hi,
Does anyone know how to put an assertion in list comprehension? I have
the following list comprehension, but I want to use an assertion to
check the contents of rec_stdl. I ended up using another loop which
essentially duplicates the functions of list comprehension. It just
look like a waste of coding and computer time to me.
I just wish I could put the assertions into list comprehensions.
x=[(rec_stdl[0].st/10000.0,
rec_stdl[0].cl,
rec_stdl[0].bb,
rec_stdl[0].bo,
rec_stdl[1].bb,
rec_stdl[1].bo,
rec_stdl[0].ex
)
for rec_stdl in rec_by_ex if len(rec_stdl)==2
]
#duplicated loop
if __debug__:
for rec_stdl in rec_by_ex:
l=len(rec_stdl)
assert(l<=2 and l>0)
if l==2:
assert(rec_stdl[0].c=="C" and rec_stdl[1].c=="P")
assert(rec_stdl[0].ex==rec_stdl[1].ex)
assert(rec_stdl[0].st==rec_stdl[1].st)
assert(rec_stdl[0].cp==rec_stdl[1].cp)

First: All your asserts are wrong. Assert is a statement, not a
function. These specific ones will behave as expected, but it's easy
to accidentally write ones that always pass this way.

Do you mean I should not use the parentheses?

Secondly: This is a waste of code, because if __debug__ is not defined
asserts will be skipped by the compiler. You could use the same loop
block for both branches.
I know. My original question was how. Dan suggested to write a checker
function.

Thirdly: This sort of testing is precisely what unit tests and/or
doctests are for.
Agreed.

Aug 1 '07 #7

P: n/a
On 8/1/07, beginner <zy*******@gmail.comwrote:
On Aug 1, 11:28 am, "Chris Mellon" <arka...@gmail.comwrote:
On 8/1/07, beginner <zyzhu2...@gmail.comwrote:


Hi,
Does anyone know how to put an assertion in list comprehension? I have
the following list comprehension, but I want to use an assertion to
check the contents of rec_stdl. I ended up using another loop which
essentially duplicates the functions of list comprehension. It just
look like a waste of coding and computer time to me.
I just wish I could put the assertions into list comprehensions.
x=[(rec_stdl[0].st/10000.0,
rec_stdl[0].cl,
rec_stdl[0].bb,
rec_stdl[0].bo,
rec_stdl[1].bb,
rec_stdl[1].bo,
rec_stdl[0].ex
)
for rec_stdl in rec_by_ex if len(rec_stdl)==2
]
#duplicated loop
if __debug__:
for rec_stdl in rec_by_ex:
l=len(rec_stdl)
assert(l<=2 and l>0)
if l==2:
assert(rec_stdl[0].c=="C" and rec_stdl[1].c=="P")
assert(rec_stdl[0].ex==rec_stdl[1].ex)
assert(rec_stdl[0].st==rec_stdl[1].st)
assert(rec_stdl[0].cp==rec_stdl[1].cp)
First: All your asserts are wrong. Assert is a statement, not a
function. These specific ones will behave as expected, but it's easy
to accidentally write ones that always pass this way.


Do you mean I should not use the parentheses?
Yes.
>
Secondly: This is a waste of code, because if __debug__ is not defined
asserts will be skipped by the compiler. You could use the same loop
block for both branches.

I know. My original question was how. Dan suggested to write a checker
function.
Use the second block in all cases. In any situation where "if
__debug__" is False, the asserts are a noop and in fact won't even be
present in the bytecode.
>
Thirdly: This sort of testing is precisely what unit tests and/or
doctests are for.

Agreed.

--
http://mail.python.org/mailman/listinfo/python-list
Aug 1 '07 #8

P: n/a
On 01 Aug 2007 16:55:53 GMT, Stargaming <st********@gmail.comwrote:
On Wed, 01 Aug 2007 11:28:48 -0500, Chris Mellon wrote:
On 8/1/07, beginner <zy*******@gmail.comwrote:
Hi,

Does anyone know how to put an assertion in list comprehension? I have
the following list comprehension, but I want to use an assertion to
check the contents of rec_stdl. I ended up using another loop which
essentially duplicates the functions of list comprehension. It just
look like a waste of coding and computer time to me.

I just wish I could put the assertions into list comprehensions.

x=[(rec_stdl[0].st/10000.0,
rec_stdl[0].cl,
rec_stdl[0].bb,
rec_stdl[0].bo,
rec_stdl[1].bb,
rec_stdl[1].bo,
rec_stdl[0].ex
)
for rec_stdl in rec_by_ex if len(rec_stdl)==2
]

#duplicated loop
if __debug__:
for rec_stdl in rec_by_ex:
l=len(rec_stdl)
assert(l<=2 and l>0)
if l==2:
assert(rec_stdl[0].c=="C" and rec_stdl[1].c=="P")
assert(rec_stdl[0].ex==rec_stdl[1].ex)
assert(rec_stdl[0].st==rec_stdl[1].st)
assert(rec_stdl[0].cp==rec_stdl[1].cp)
First: All your asserts are wrong. Assert is a statement, not a
function. These specific ones will behave as expected, but it's easy to
accidentally write ones that always pass this way.

Could you come up with an example? I can only think of accidentally
injecting a comma, what would create a (true, in a boolean context) tuple.

And, well, if you're only using () for readabilty, this might sometimes
look messy when calling assert with the extended syntax::

assert(False), "error text"
It's very easy to write this as assert(False, "error text") if you're
in the habit of thinking that assert is a function.
Where one could expect the construction of a tuple.
Secondly: This is a waste of code, because if __debug__ is not defined
asserts will be skipped by the compiler. You could use the same loop
block for both branches.

Well, the `assert` isn't there for no reason, but if you're serious about
it, `raise` could be better.
Thirdly: This sort of testing is precisely what unit tests and/or
doctests are for.

Huh? What beginner is doing there seems more like input validation than
testing. Unit or doctests are meant for testing (and in case of doctests,
showing) whether a function works as expected.
Not in a big __debug__ block it isn't.
--
http://mail.python.org/mailman/listinfo/python-list
Aug 1 '07 #9

P: n/a
On Aug 1, 12:35 pm, "Chris Mellon" <arka...@gmail.comwrote:
On 8/1/07, beginner <zyzhu2...@gmail.comwrote:


On Aug 1, 11:28 am, "Chris Mellon" <arka...@gmail.comwrote:
On 8/1/07, beginner <zyzhu2...@gmail.comwrote:
Hi,
Does anyone know how to put an assertion in list comprehension? I have
the following list comprehension, but I want to use an assertion to
check the contents of rec_stdl. I ended up using another loop which
essentially duplicates the functions of list comprehension. It just
look like a waste of coding and computer time to me.
I just wish I could put the assertions into list comprehensions.
x=[(rec_stdl[0].st/10000.0,
rec_stdl[0].cl,
rec_stdl[0].bb,
rec_stdl[0].bo,
rec_stdl[1].bb,
rec_stdl[1].bo,
rec_stdl[0].ex
)
for rec_stdl in rec_by_ex if len(rec_stdl)==2
]
#duplicated loop
if __debug__:
for rec_stdl in rec_by_ex:
l=len(rec_stdl)
assert(l<=2 and l>0)
if l==2:
assert(rec_stdl[0].c=="C" and rec_stdl[1].c=="P")
assert(rec_stdl[0].ex==rec_stdl[1].ex)
assert(rec_stdl[0].st==rec_stdl[1].st)
assert(rec_stdl[0].cp==rec_stdl[1].cp)
First: All your asserts are wrong. Assert is a statement, not a
function. These specific ones will behave as expected, but it's easy
to accidentally write ones that always pass this way.
Do you mean I should not use the parentheses?

Yes.
Secondly: This is a waste of code, because if __debug__ is not defined
asserts will be skipped by the compiler. You could use the same loop
block for both branches.
I know. My original question was how. Dan suggested to write a checker
function.

Use the second block in all cases. In any situation where "if
__debug__" is False, the asserts are a noop and in fact won't even be
present in the bytecode.


Thirdly: This sort of testing is precisely what unit tests and/or
doctests are for.
Agreed.
--
http://mail.python.org/mailman/listinfo/python-list- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -
I see. In fact I want to whole block surrounded by __debug__ to be
optimized away in non-debug runs. If the logic of my program is
correct, the asserts are guaranteed to be true, no matter what the
input is. It is not input checking.

Aug 1 '07 #10

P: n/a
On Aug 1, 12:38 pm, "Chris Mellon" <arka...@gmail.comwrote:
On 01 Aug 2007 16:55:53 GMT, Stargaming <stargam...@gmail.comwrote:


On Wed, 01 Aug 2007 11:28:48 -0500, Chris Mellon wrote:
On 8/1/07, beginner <zyzhu2...@gmail.comwrote:
>Hi,
>Does anyone know how to put an assertion in list comprehension? I have
>the following list comprehension, but I want to use an assertion to
>check the contents of rec_stdl. I ended up using another loop which
>essentially duplicates the functions of list comprehension. It just
>look like a waste of coding and computer time to me.
>I just wish I could put the assertions into list comprehensions.
> x=[(rec_stdl[0].st/10000.0,
> rec_stdl[0].cl,
> rec_stdl[0].bb,
> rec_stdl[0].bo,
> rec_stdl[1].bb,
> rec_stdl[1].bo,
> rec_stdl[0].ex
> )
> for rec_stdl in rec_by_ex if len(rec_stdl)==2
> ]
> #duplicated loop
> if __debug__:
> for rec_stdl in rec_by_ex:
> l=len(rec_stdl)
> assert(l<=2 and l>0)
> if l==2:
> assert(rec_stdl[0].c=="C" and rec_stdl[1].c=="P")
> assert(rec_stdl[0].ex==rec_stdl[1].ex)
> assert(rec_stdl[0].st==rec_stdl[1].st)
> assert(rec_stdl[0].cp==rec_stdl[1].cp)
First: All your asserts are wrong. Assert is a statement, not a
function. These specific ones will behave as expected, but it's easy to
accidentally write ones that always pass this way.
Could you come up with an example? I can only think of accidentally
injecting a comma, what would create a (true, in a boolean context) tuple.
And, well, if you're only using () for readabilty, this might sometimes
look messy when calling assert with the extended syntax::
assert(False), "error text"

It's very easy to write this as assert(False, "error text") if you're
in the habit of thinking that assert is a function.
Where one could expect the construction of a tuple.
Secondly: This is a waste of code, because if __debug__ is not defined
asserts will be skipped by the compiler. You could use the same loop
block for both branches.
Well, the `assert` isn't there for no reason, but if you're serious about
it, `raise` could be better.
Thirdly: This sort of testing is precisely what unit tests and/or
doctests are for.
Huh? What beginner is doing there seems more like input validation than
testing. Unit or doctests are meant for testing (and in case of doctests,
showing) whether a function works as expected.

Not in a big __debug__ block it isn't.

No I was trying to test the logic of my code not validating the
contents of the data. Thanks.
>
--
http://mail.python.org/mailman/listinfo/python-list- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -

Aug 1 '07 #11

P: n/a
On 8/1/07, beginner <zy*******@gmail.comwrote:
On Aug 1, 12:35 pm, "Chris Mellon" <arka...@gmail.comwrote:
On 8/1/07, beginner <zyzhu2...@gmail.comwrote:
<much snippage>
I see. In fact I want to whole block surrounded by __debug__ to be
optimized away in non-debug runs. If the logic of my program is
correct, the asserts are guaranteed to be true, no matter what the
input is. It is not input checking.
I inferred as much. That's why placing it in a unit or doctest is even better.
Aug 1 '07 #12

This discussion thread is closed

Replies have been disabled for this discussion.