Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.026 X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; 'exception.': 0.09; 'pm,': 0.11; 'slightly': 0.12; 'binary': 0.14; 'wrote:': 0.14; '(another': 0.16; '(last': 0.16; 'boolean': 0.16; 'contrived': 0.16; 'exploits': 0.16; 'expression)': 0.16; 'mean,': 0.16; 'met.': 0.16; 'packet': 0.16; 'situations,': 0.16; 'subject:() ': 0.16; 'subject:function': 0.16; 'tue,': 0.20; 'header:In-Reply- To:1': 0.22; 'posted': 0.22; 'subject:code': 0.23; 'example': 0.24; 'compare': 0.26; 'equivalent': 0.26; 'chris': 0.27; 'message-id:@mail.gmail.com': 0.28; 'raise': 0.29; "won't": 0.30; 'guarantees': 0.31; 'recall': 0.31; 'does': 0.31; "can't": 0.31; 'to:addr:python-list': 0.32; '...': 0.32; "i've": 0.33; 'done,': 0.33; 'fairly': 0.33; "isn't": 0.34; 'christian': 0.35; 'flag': 0.35; 'case': 0.37; 'received:209.85': 0.37; 'useful': 0.37; 'apr': 0.38; 'steven': 0.38; 'received:google.com': 0.38; 'but': 0.38; 'explain': 0.39; 'to:addr:python.org': 0.39; 'where': 0.39; 'received:209': 0.39; 'how': 0.39; 'would': 0.40; 'header:Received:5': 0.40; 'direct': 0.61; '2011': 0.62; 'guarantee': 0.75; 'satisfied': 0.83; 'concept:': 0.84; 'received:209.85.210.174': 0.84; 'received:mail- iy0-f174.google.com': 0.84; 'specifics': 0.84; 'suspicious': 0.84; 'sum': 0.89 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:content-type; bh=TcDy7zO6b/+lM/TKIe4kn8DrtyVr1JpNv6pREmcCe7I=; b=WjBpCWSYtujqLbLhOJ30982vlOH+LY70BUmWEFQXFjUg3j37Pt8zXR4NPlDUiX/mDT OIqTeRjuVfOSYSU/e6+eV/vHt1iwbZU+2x3SraPCMCi2Vru+va4Fsf5Pk1f+SfypDYTo sJ/NmO5bF3ulbMLpF8zyNKxRkgiZYuVPqpkdI= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; b=nePTopL1qEX7cDWDZ3RctGggmq6FQcdP7V7lVurC5Dcw3f3QW9JPeicTk7DLuSQVwH UC4gAcaClzTzAiaegWKsRtZ6dzkaMdi4T9rMWsXSH8OnBkfgM8xnVCVEcfBeagJCH2EL 4K9gzoj5fof68jvybIytn9Stk3mNWSXOWQMV8= MIME-Version: 1.0 In-Reply-To: <4dad4b29$0$29984$c3e8da3$5496439d@news.astraweb.com> References: <4da9fb0b$0$13696$426a74cc@news.free.fr> <87k4etho6e.fsf@benfinney.id.au> <4daa2b72$0$32037$426a74cc@news.free.fr> <87d3klha85.fsf@benfinney.id.au> <4daaa6f6$0$20187$426a74cc@news.free.fr> <911dvfF6ocU1@mid.individual.net> <4dac97ca$0$10609$742ec2ed@news.sonic.net> <4dad4b29$0$29984$c3e8da3$5496439d@news.astraweb.com> Date: Tue, 19 Apr 2011 19:00:19 +1000 Subject: Re: Equivalent code to the bool() built-in function From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 30 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1303203623 news.xs4all.nl 65870 [::ffff:82.94.164.166]:56237 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:3553 On Tue, Apr 19, 2011 at 6:43 PM, Steven D'Aprano wrote: > but I don't see how > > (arbitrary expression) + (another expression) + ... + (last expression) > > can have any guarantees applied. I mean, you can't even guarantee that > they won't raise an exception. Can you explain what you mean? What Christian posted isn't something I've often done, but here's something slightly different that exploits the same comparisons-return-summable-values concept: A condition with N subconditions is deemed to be satisfied if a minimum of M of them are true. This is a general case of the boolean Or (N = 2, M = 1) and And (N = 2, M = 2), but does not have a direct equivalent in binary operators. You simply sum the subconditions, compare against M, and you have your answer. if (((port<1024) + (!ip.startswith("192.168.")) + (greylist[ip]>time()) + (++spewcnt>10))>=3) // flag this packet as suspicious Contrived example as I don't recall any specifics right now, but this will pick up any packets where three or more of the conditions are met. Useful only in fairly specific situations, but I don't know of any way to do this with just AND/OR/NOT that would be as clear and simple. Chris Angelico