tags:

views:

109

answers:

2

Reading the documentation it seems this might not be possible, but it seems that a lot of people have been able to beat more complicated functionality into pythons lambda function.

I'm leveraging the scapy libraries to do some packet creation. Specially this questions is about the ConditionalField which takes it a field and a comparison function, the field only being added to the packet if the comparison is true, but I need to do 2 comparisons.

Example with only one check, this works:

ConditionalField(XShortField("chksum",None),lambda pkt:pkt.chksumpresent==1)

What I want:

ConditionalField(XShortField("chksum",None),lambda pkt:pkt.chksumpresent==1 or (lamba pkt:pkt.special == 1))

This isn't giving expected results. Is there a way to do this?

+6  A: 
lambda pkt:((pkt.chksumpresent == 1) or (pkt.special == 1))
Ignacio Vazquez-Abrams
+5  A: 

Is lambda the most readable/maintainable? The following is just as performant:

def checksum_condition(pkt):
    return pkt.chksumpresent == 1 or pkt.special == 1

ConditionalField(XShortField("chksum",None), checksum_condition)
Joe Koberg