473,597 Members | 2,269 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Assertion in list comprehension

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
11 7019
On Aug 1, 9:37 am, beginner <zyzhu2...@gmai l.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
On Aug 1, 11:09 am, "danmcle...@yah oo.com" <danmcle...@yah oo.com>
wrote:
On Aug 1, 9:37 am, beginner <zyzhu2...@gmai l.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
On 8/1/07, beginner <zy*******@gmai l.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
On Wed, 01 Aug 2007 11:28:48 -0500, Chris Mellon wrote:
On 8/1/07, beginner <zy*******@gmai l.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
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
On Aug 1, 11:28 am, "Chris Mellon" <arka...@gmail. comwrote:
On 8/1/07, beginner <zyzhu2...@gmai l.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
On 8/1/07, beginner <zy*******@gmai l.comwrote:
On Aug 1, 11:28 am, "Chris Mellon" <arka...@gmail. comwrote:
On 8/1/07, beginner <zyzhu2...@gmai l.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
On 01 Aug 2007 16:55:53 GMT, Stargaming <st********@gma il.comwrote:
On Wed, 01 Aug 2007 11:28:48 -0500, Chris Mellon wrote:
On 8/1/07, beginner <zy*******@gmai l.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
On Aug 1, 12:35 pm, "Chris Mellon" <arka...@gmail. comwrote:
On 8/1/07, beginner <zyzhu2...@gmai l.comwrote:


On Aug 1, 11:28 am, "Chris Mellon" <arka...@gmail. comwrote:
On 8/1/07, beginner <zyzhu2...@gmai l.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
40618
by: Fuzzyman | last post by:
Pythons internal 'pointers' system is certainly causing me a few headaches..... When I want to copy the contents of a variable I find it impossible to know whether I've copied the contents *or* just created a new pointer to the original value.... For example I wanted to initialize a list of empty lists.... a=, , , , ] I thought there has to be a *really* easy way of doing it - after a
35
2954
by: Moosebumps | last post by:
Does anyone here find the list comprehension syntax awkward? I like it because it is an expression rather than a series of statements, but it is a little harder to maintain it seems. e.g. you could do: result = for element in list: if element == 'blah':
18
460
by: a | last post by:
can someone tell me how to use them thanks
4
1803
by: Gregory Guthrie | last post by:
Sorry for a simple question- but I don't understand how to parse this use of a list comprehension. The "or" clauses are odd to me. It also seems like it is being overly clever (?) in using a lc expression as a for loop to drive the recursion. Thanks for any insight! Gregory
0
7971
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7893
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8276
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8040
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8259
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5847
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3889
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2408
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1495
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.