3

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?

2 Answers 2

8
lambda pkt:((pkt.chksumpresent == 1) or (pkt.special == 1))
Sign up to request clarification or add additional context in comments.

Comments

6

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.