Esempio 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)
Esempio 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)
Esempio 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))
Esempio 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))
Esempio 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))
Esempio 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()
Esempio 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))
Esempio 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
Esempio 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
Esempio 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)
Esempio 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)
Esempio 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)
Esempio 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)
Esempio 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)
Esempio 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
Esempio 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
Esempio n. 17
0
 def do(self, a):
     Guard.against_empty(a)
     pass
Esempio n. 18
0
 def do(self, a):
     Guard.accepts(a, (int, float),
                   "Argument a must be an integer or a float")
     pass
Esempio n. 19
0
 def do(self, a):
     Guard.accepts(a, (int, float))
     pass
Esempio n. 20
0
 def do(self, a):
     Guard.against_empty(a, "Argument a is required")
     pass
Esempio n. 21
0
 def do(self, a):
     Guard.accepts(a, (int, float))
     pass
Esempio n. 22
0
 def do(self, a):
     Guard.accepts(a, (int, float), "Argument a must be an integer or a float")
     pass
Esempio n. 23
0
 def do(self, a):
     Guard.against_empty(a)
     pass
Esempio n. 24
0
 def do(self, a):
     Guard.against_empty(a, "Argument a is required")
     pass
Esempio 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)