Exemplo n.º 1
0
    def parse_avg(self, query, column):
        seq = self.parse_select_many(query)

        if len(seq) == 0:
            return 0

        error_message = (
            "The attribute '%s' was not found in the specified collection's items. If you meant to use the raw value of each item in the collection just use the word 'item' as a parameter to .avg or use .avg()"
            % column
        )

        Guard.against_empty(column, error_message)

        attribute = column.replace("item.", "")

        if "item." in column:
            try:
                seq = [self.rec_getattr(item, attribute) for item in seq]
            except AttributeError:
                raise ValueError(error_message)
        else:
            if attribute.lower() != "item":
                raise ValueError(error_message)

        return reduce(operator.add, seq) / len(seq)
Exemplo n.º 2
0
 def __init__(self, *args):
     Guard.against_empty(
         args,
         "In order to create a new attribute expression you need to provide some attributes."
     )
     self.attributes = []
     self.add_attributes(args)
Exemplo n.º 3
0
 def test_accepts_only_without_message(self):
     items = ["a", "b"]
     items_failing = ["a", "b", 1]
     message = u"All arguments in the given collection should be of type\(s\) \[str\] and at least one of them isn't."
     
     Guard.accepts_only(items, [str])
     
     self.assertRaisesEx(ValueError, Guard.accepts_only, items_failing, [str], exc_pattern=re.compile(message))
Exemplo n.º 4
0
 def test_accepts_only_with_message(self):
     items = ["a", "b"]
     items_failing = ["a", "b", 1]
     message = "There should be only strings."
     
     Guard.accepts_only(items, [str], message)
     
     self.assertRaisesEx(ValueError, Guard.accepts_only, items_failing, [str], message, exc_pattern=re.compile(message))
Exemplo n.º 5
0
    def test_accepts_only_without_message(self):
        items = ["a", "b"]
        items_failing = ["a", "b", 1]
        message = u"All arguments in the given collection should be of type\(s\) \[str\] and at least one of them isn't."

        Guard.accepts_only(items, [str])

        self.assertRaisesEx(ValueError,
                            Guard.accepts_only,
                            items_failing, [str],
                            exc_pattern=re.compile(message))
Exemplo n.º 6
0
 def __init__(self, provider):
     error_message = "The provider cannot be None. If you meant to use the CollectionProvider pass in a tuple or list"
     Guard.against_none(provider, error_message)
     if isinstance(provider, (list, tuple)):
         self.provider = CollectionProvider(provider)
     else:
         self.provider = provider
     self.expressions = [] 
     self.order_expressions = []
     self.group_expression = None
     self.parser = ExpressionParser()
Exemplo n.º 7
0
    def test_accepts_only_with_message(self):
        items = ["a", "b"]
        items_failing = ["a", "b", 1]
        message = "There should be only strings."

        Guard.accepts_only(items, [str], message)

        self.assertRaisesEx(ValueError,
                            Guard.accepts_only,
                            items_failing, [str],
                            message,
                            exc_pattern=re.compile(message))
Exemplo n.º 8
0
 def __init__(self, node_type, rhs):
     '''Initializes the UnaryExpression with the specified arguments.
     Arguments:
         node_type - Specifies the type of operation that this UnaryExpression represents
         rhs - Right-hand site of the operation. Since this is an unary operation, this is the only argument.
     '''
     Guard.against_empty(node_type, "The UnaryExpression node type is required")
     if node_type == self.CollectionLength:
         Guard.accepts(rhs, (ConstantExpression,), "The CollectionLength unary expression can only take ConstantExpressions that hold tuples or lists as parameters.")
         if not isinstance(rhs.evaluate(), (list, tuple)):
             raise ValueError("The CollectionLength unary expression can only take ConstantExpressions that hold tuples or lists as parameters.")
     self.node_type = node_type
     self.rhs = rhs
Exemplo n.º 9
0
 def __init__(self, node_type, rhs):
     '''Initializes the UnaryExpression with the specified arguments.
     Arguments:
         node_type - Specifies the type of operation that this UnaryExpression represents
         rhs - Right-hand site of the operation. Since this is an unary operation, this is the only argument.
     '''
     Guard.against_empty(node_type,
                         "The UnaryExpression node type is required")
     if node_type == self.CollectionLength:
         Guard.accepts(
             rhs, (ConstantExpression, ),
             "The CollectionLength unary expression can only take ConstantExpressions that hold tuples or lists as parameters."
         )
         if not isinstance(rhs.evaluate(), (list, tuple)):
             raise ValueError(
                 "The CollectionLength unary expression can only take ConstantExpressions that hold tuples or lists as parameters."
             )
     self.node_type = node_type
     self.rhs = rhs
Exemplo n.º 10
0
    def parse_avg(self, query, column):
        seq = self.parse_select_many(query)

        if len(seq) == 0:
            return 0

        error_message = "The attribute '%s' was not found in the specified collection's items. If you meant to use the raw value of each item in the collection just use the word 'item' as a parameter to .avg or use .avg()" % column

        Guard.against_empty(column, error_message)

        attribute = column.replace("item.", "")

        if "item." in column:
            try:
                seq = [self.rec_getattr(item, attribute) for item in seq]
            except AttributeError:
                raise ValueError(error_message)
        else:
            if attribute.lower() != "item":
                raise ValueError(error_message)

        return reduce(operator.add, seq) / len(seq)
Exemplo n.º 11
0
    def __perform_operation_on_all(self, query, column, operation,
                                   command_name):
        seq = self.parse_select_many(query)

        if len(seq) == 0:
            return 0

        error_message = "The attribute '%s' was not found in the specified collection's items. If you meant to use the raw value of each item in the collection just use the word 'item' as a parameter to .%s or use .%s()" % (
            column, command_name, command_name)

        Guard.against_empty(column, error_message)

        attribute = column.replace("item.", "")
        if "item." in column:
            try:
                seq = [self.rec_getattr(item, attribute) for item in seq]
            except AttributeError:
                raise ValueError(error_message)
        else:
            if attribute.lower() != "item":
                raise ValueError(error_message)

        return operation(seq)
Exemplo n.º 12
0
    def __perform_operation_on_all(self, query, column, operation, command_name):
        seq = self.parse_select_many(query)

        if len(seq) == 0:
            return 0

        error_message = (
            "The attribute '%s' was not found in the specified collection's items. If you meant to use the raw value of each item in the collection just use the word 'item' as a parameter to .%s or use .%s()"
            % (column, command_name, command_name)
        )

        Guard.against_empty(column, error_message)

        attribute = column.replace("item.", "")
        if "item." in column:
            try:
                seq = [self.rec_getattr(item, attribute) for item in seq]
            except AttributeError:
                raise ValueError(error_message)
        else:
            if attribute.lower() != "item":
                raise ValueError(error_message)

        return operation(seq)
Exemplo n.º 13
0
 def select(self, *cols):
     empty_message = "Selecting with no fields is not valid. " \
                               + "When using From(provider).select method, " \
                               + "please provide a list of expressions or strings as fields."
     Guard.against_empty(cols, empty_message)
     for col in cols:
         Guard.against_empty(col, empty_message)
     Guard.accepts_only(cols, [str, Expression], "Selecting with invalid type. " \
                                                 + "When using From(provider).select method, " \
                                                 + "please provide a list of expressions or strings as fields.")
                                                 
     return self.provider.parse(self, action=Actions.Select, cols=cols)
Exemplo n.º 14
0
 def select(self, *cols):
     empty_message = "Selecting with no fields is not valid. " \
                               + "When using From(provider).select method, " \
                               + "please provide a list of expressions or strings as fields."
     Guard.against_empty(cols, empty_message)
     for col in cols:
         Guard.against_empty(col, empty_message)
     Guard.accepts_only(cols, [str, Expression], "Selecting with invalid type. " \
                                                 + "When using From(provider).select method, " \
                                                 + "please provide a list of expressions or strings as fields.")
                                                 
     return self.provider.parse(self, action=Actions.Select, cols=cols)
Exemplo n.º 15
0
 def __init__(self, node_type, lhs, rhs):
     '''Initializes the BinaryExpression with the specified arguments.
     Arguments:
         node_type - Specifies the type of operation that this BinaryExpression represents
         lhs - Left-hand side of the operation (as in the first argument)
         rhs - Right-hand site of the operation (as in the second argument)
     '''
     Guard.against_empty(node_type, "The BinaryExpression node type is required")
     Guard.accepts(lhs, (Expression,), "Lhs must be an expression (an instance of a class that inherits from pynq.Expression), but was %s" % lhs.__class__.__name__)
     Guard.accepts(rhs, (Expression,), "Rhs must be an expression (an instance of a class that inherits from pynq.Expression) but was %s" % rhs.__class__.__name__)
     self.node_type = node_type
     self.lhs = lhs
     self.rhs = rhs
Exemplo n.º 16
0
 def __init__(self, node_type, lhs, rhs):
     '''Initializes the BinaryExpression with the specified arguments.
     Arguments:
         node_type - Specifies the type of operation that this BinaryExpression represents
         lhs - Left-hand side of the operation (as in the first argument)
         rhs - Right-hand site of the operation (as in the second argument)
     '''
     Guard.against_empty(node_type,
                         "The BinaryExpression node type is required")
     Guard.accepts(
         lhs, (Expression, ),
         "Lhs must be an expression (an instance of a class that inherits from pynq.Expression), but was %s"
         % lhs.__class__.__name__)
     Guard.accepts(
         rhs, (Expression, ),
         "Rhs must be an expression (an instance of a class that inherits from pynq.Expression) but was %s"
         % rhs.__class__.__name__)
     self.node_type = node_type
     self.lhs = lhs
     self.rhs = rhs
Exemplo n.º 17
0
 def do(self, a):
     Guard.against_empty(a)
     pass
Exemplo n.º 18
0
 def do(self, a):
     Guard.accepts(a, (int, float),
                   "Argument a must be an integer or a float")
     pass
Exemplo n.º 19
0
 def do(self, a):
     Guard.accepts(a, (int, float))
     pass
Exemplo n.º 20
0
 def do(self, a):
     Guard.against_empty(a, "Argument a is required")
     pass
Exemplo n.º 21
0
 def do(self, a):
     Guard.accepts(a, (int, float))
     pass
Exemplo n.º 22
0
 def do(self, a):
     Guard.accepts(a, (int, float), "Argument a must be an integer or a float")
     pass
Exemplo n.º 23
0
 def do(self, a):
     Guard.against_empty(a)
     pass
Exemplo n.º 24
0
 def do(self, a):
     Guard.against_empty(a, "Argument a is required")
     pass
Exemplo n.º 25
0
 def __init__(self, *args):
     Guard.against_empty(args, "In order to create a new attribute expression you need to provide some attributes.")
     self.attributes = []
     self.add_attributes(args)