Beispiel #1
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)
Beispiel #2
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)
Beispiel #3
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)
Beispiel #4
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)
Beispiel #5
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
Beispiel #6
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
Beispiel #7
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
Beispiel #8
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
Beispiel #9
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)
Beispiel #10
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)
Beispiel #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)
Beispiel #12
0
 def do(self, a):
     Guard.against_empty(a)
     pass
Beispiel #13
0
 def do(self, a):
     Guard.against_empty(a, "Argument a is required")
     pass
Beispiel #14
0
 def do(self, a):
     Guard.against_empty(a)
     pass
Beispiel #15
0
 def do(self, a):
     Guard.against_empty(a, "Argument a is required")
     pass
Beispiel #16
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)