Assertion in list comprehension

B

beginner

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
 
D

danmcleran

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)]
 
B

beginner

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!
 
C

Chris Mellon

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.
 
S

Stargaming

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.
 
M

Marc 'BlackJack' Rintsch

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
 
B

beginner

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.
 
C

Chris Mellon

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.
 
C

Chris Mellon

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.


Well, the `assert` isn't there for no reason, but if you're serious about
it, `raise` could be better.


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.
 
B

beginner

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.
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.





- 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.
 
B

beginner

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.
Well, the `assert` isn't there for no reason, but if you're serious about
it, `raise` could be better.
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.
 
C

Chris Mellon

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,079
Latest member
ElidaWarin

Latest Threads

Top