[Distutils] FINAL DRAFT: Dependency specifier PEP
Nathaniel Smith
njs at pobox.com
Mon Nov 16 22:37:58 EST 2015
On Mon, Nov 16, 2015 at 7:27 PM, Robert Collins
<robertc at robertcollins.net> wrote:
> On 17 November 2015 at 15:42, Nathaniel Smith <njs at pobox.com> wrote:
>>> So for clarity:
>>> True and -> right hand side evaluated stand alone
>>> False and -> False
>>> True or -> True
>>> False or -> right hand side evaluated stand alone
>>>
>>> We can roll that up using the parse tree:
>>> (('posix', '==', 'dud'), [('and', ('posix', '==', 'odd')), ('or',
>>> ('posix', '==', 'fred'))]))
>>> evaluate the start to get a 'result'
>>> pop an expression from the beginning of the list giving opcode, next.
>>> lookup (result, opcode) in the above truth table, giving one of True,
>>> False, None
>>> on None, result = next and loop.
>>> on True or False, return that.
>>
>> Not sure if we're communicating or not? The case I'm concerned about is
>>
>> a or b and c
>>
>> which Python parses as (a or (b and c)) because 'and' has higher
>> precedence than 'or'. So for example in Python,
>>
>> True or True and False
>>
>> returns True, but if you use a left-to-right evaluation rule than it
>> returns False.
>
>
> True or True and False
> ->
> lookup = {
> (True, 'and'): None,
> (False, 'and'): False,
> (True, 'or'): True,
> (False, 'or'): None}
>
> def evaluate(expr):
> result = expr[0]
> remainder = expr[1]
> while remainder:
> opcode, next = remainder.pop(0)
> branch = lookup[(result, opcode)]
> if branch is not None:
> return branch
> result = next
> return result
>
> expr = (True, [('or', True), ('and', False)])
> evaluate(expr)
>
> -> True
>
> So, I think what I describe does what you want it to.
In [7]: evaluate((False, [('and', False), ('or', True)]))
Out[7]: False
In [8]: False and False or True
Out[8]: True
-n
--
Nathaniel J. Smith -- http://vorpus.org
More information about the Distutils-SIG
mailing list