def repeat(rule, from_count, to_count=None, allow_whitespace=False):
"""Allow between from_count and to_count repetitions of a rule.
If to_count is not given, then allow as many repetitions as can be
parsed.
"""
if to_count == 0:
return null
if from_count == 0 and to_count is None:
return many(rule, allow_whitespace=allow_whitespace)
op = operator.__add__ if allow_whitespace else operator.__xor__
next_to_count = to_count - 1 if to_count is not None else None
next_from_count = from_count - 1 if from_count > 0 else 0
first_part = optional(rule) if from_count == 0 else rule
return op(first_part, repeat(rule, next_from_count, next_to_count,
allow_whitespace))
python类__xor__()的实例源码
def __xor__(self, other_rule):
return ConjunctionRule(self, other_rule, allow_whitespace=False)
def __xor__(self, rhs):
return op_binary(self, rhs, operator.__xor__)
def __rxor__(self, lhs):
return op_binary(lhs, self, operator.__xor__)
def __ixor__(self, v):
return self.op_binary_inplace(v, operator.__xor__)
def __xor__(self, other):
with self._lock:
return operator.__xor__(self.__wrapped__, other)
def __rxor__(self, other):
with self._lock:
return operator.__xor__(other, self.__wrapped__)
def __xor__(self, other): return _op2(self, other, operator.__xor__)
def __rxor__(self, other): return _op2(self, other, operator.__xor__, rev=True)
def test_xor(self):
self._test_incompatible_types_fail(operator.__xor__)
self.assertEqual(no_flags ^ no_flags, no_flags)
self.assertEqual(no_flags ^ all_flags, all_flags)
self.assertEqual(no_flags ^ f0, f0)
self.assertEqual(no_flags ^ f1, f1)
self.assertEqual(no_flags ^ f2, f2)
self.assertEqual(no_flags ^ f01, f01)
self.assertEqual(no_flags ^ f02, f02)
self.assertEqual(no_flags ^ f12, f12)
self.assertEqual(f0 ^ no_flags, f0)
self.assertEqual(f0 ^ all_flags, f12)
self.assertEqual(f0 ^ f0, no_flags)
self.assertEqual(f0 ^ f1, f01)
self.assertEqual(f0 ^ f2, f02)
self.assertEqual(f0 ^ f01, f1)
self.assertEqual(f0 ^ f02, f2)
self.assertEqual(f0 ^ f12, all_flags)
self.assertEqual(f01 ^ no_flags, f01)
self.assertEqual(f01 ^ all_flags, f2)
self.assertEqual(f01 ^ f0, f1)
self.assertEqual(f01 ^ f1, f0)
self.assertEqual(f01 ^ f2, all_flags)
self.assertEqual(f01 ^ f01, no_flags)
self.assertEqual(f01 ^ f02, f12)
self.assertEqual(f01 ^ f12, f02)
self.assertEqual(all_flags ^ no_flags, all_flags)
self.assertEqual(all_flags ^ all_flags, no_flags)
self.assertEqual(all_flags ^ f0, f12)
self.assertEqual(all_flags ^ f1, f02)
self.assertEqual(all_flags ^ f2, f01)
self.assertEqual(all_flags ^ f01, f2)
self.assertEqual(all_flags ^ f02, f1)
self.assertEqual(all_flags ^ f12, f0)