Sfoglia il codice sorgente

Improve exception behavior; cleanup.

tags/v1.0^2
Ben Kurtovic 10 anni fa
parent
commit
4fa8b9f444
3 ha cambiato i file con 15 aggiunte e 4 eliminazioni
  1. +6
    -0
      bitshift/query/__init__.py
  2. +4
    -4
      bitshift/query/nodes.py
  3. +5
    -0
      bitshift/query/tree.py

+ 6
- 0
bitshift/query/__init__.py Vedi File

@@ -209,6 +209,9 @@ class _QueryParser(object):
def parse_binary_op(op):
"""Parse a binary operator in a nested query list."""
index = nest.index(op)
if index == 0 or index == len(nest) - 1:
err = "Invalid query: '%s' given without argument."
raise QueryParseException(err % BinaryOp.OPS[op])
left = self._parse_nest(nest[:index])
right = self._parse_nest(nest[index + 1:])
return BinaryOp(left, op, right)
@@ -222,6 +225,9 @@ class _QueryParser(object):
return parse_binary_op(BinaryOp.AND)
elif UnaryOp.NOT in nest:
index = nest.index(UnaryOp.NOT)
if index == len(nest) - 1:
err = "Invalid query: '%s' given without argument."
raise QueryParseException(err % UnaryOp.OPS[op])
right = UnaryOp(UnaryOp.NOT, self._parse_nest(nest[index + 1:]))
if index > 0:
left = self._parse_nest(nest[:index])


+ 4
- 4
bitshift/query/nodes.py Vedi File

@@ -176,6 +176,7 @@ class BinaryOp(_Node):
"""Represents a relationship between two nodes: ``and``, ``or``."""
AND = object()
OR = object()
OPS = {AND: "AND", OR: "OR"}

def __init__(self, left, op, right):
self.left = left
@@ -183,9 +184,8 @@ class BinaryOp(_Node):
self.right = right

def __repr__(self):
ops = {self.AND: "AND", self.OR: "OR"}
tmpl = "BinaryOp({0}, {1}, {2})"
return tmpl.format(self.left, ops[self.op], self.right)
return tmpl.format(self.left, self.OPS[self.op], self.right)

def sortkey(self):
return self.left.sortkey() + self.right.sortkey()
@@ -194,14 +194,14 @@ class BinaryOp(_Node):
class UnaryOp(_Node):
"""Represents a transformation applied to one node: ``not``."""
NOT = object()
OPS = {NOT: "NOT"}

def __init__(self, op, node):
self.op = op
self.node = node

def __repr__(self):
ops = {self.NOT: "NOT"}
return "UnaryOp({0}, {1})".format(ops[self.op], self.node)
return "UnaryOp({0}, {1})".format(self.OPS[self.op], self.node)

def sortkey(self):
return self.node.sortkey()

+ 5
- 0
bitshift/query/tree.py Vedi File

@@ -9,6 +9,11 @@ class Tree(object):
def __repr__(self):
return "Tree({0})".format(self._root)

@property
def root(self):
"""The root node of the tree."""
return self._root

def sortkey(self):
"""Return a string sort key for the query tree."""
return self._root.sortkey()


Caricamento…
Annulla
Salva