+ @classmethod
+ def _fromast(cls, node):
+ if isinstance(node, ast.Module) and len(node.body) == 1:
+ return cls._fromast(node.body[0])
+ elif isinstance(node, ast.Expr):
+ return cls._fromast(node.value)
+ elif isinstance(node, ast.BinOp) and isinstance(node.op, ast.BitAnd):
+ equalities1, inequalities1 = cls._fromast(node.left)
+ equalities2, inequalities2 = cls._fromast(node.right)
+ equalities = equalities1 + equalities2
+ inequalities = inequalities1 + inequalities2
+ return equalities, inequalities
+ elif isinstance(node, ast.Compare):
+ equalities = []
+ inequalities = []
+ left = Expression._fromast(node.left)
+ for i in range(len(node.ops)):
+ op = node.ops[i]
+ right = Expression._fromast(node.comparators[i])
+ if isinstance(op, ast.Lt):
+ inequalities.append(right - left - 1)
+ elif isinstance(op, ast.LtE):
+ inequalities.append(right - left)
+ elif isinstance(op, ast.Eq):
+ equalities.append(left - right)
+ elif isinstance(op, ast.GtE):
+ inequalities.append(left - right)
+ elif isinstance(op, ast.Gt):
+ inequalities.append(left - right - 1)
+ else:
+ break
+ left = right
+ else:
+ return equalities, inequalities
+ raise SyntaxError('invalid syntax')
+
+ @classmethod
+ def fromstring(cls, string):
+ string = string.strip()
+ string = re.sub(r'^\{\s*|\s*\}$', '', string)
+ string = re.sub(r'([^<=>])=([^<=>])', r'\1==\2', string)
+ string = re.sub(r'(\d+|\))\s*([^\W\d_]\w*|\()', r'\1*\2', string)
+ tokens = re.split(r',|;|and|&&|/\\|∧', string, flags=re.I)
+ tokens = ['({})'.format(token) for token in tokens]
+ string = ' & '.join(tokens)
+ tree = ast.parse(string, 'eval')
+ equalities, inequalities = cls._fromast(tree)
+ return cls(equalities, inequalities)