Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

72 expressions improvements #117

Merged
merged 16 commits into from
Apr 23, 2024
Merged
23 changes: 14 additions & 9 deletions src/monggregate/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,20 @@ def __new__(cls, *args, **kwargs):
class BaseModel(pyd.BaseModel, ABC):
"""Mongreggate base class"""

def to_expression(self)->dict:
"""Converts an instance of a class inheriting from BaseModel to an expression"""

return self.express(self)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [mypy] reported by reviewdog 🐶
Incompatible return value type (got "dict[Any, Any] | list[dict[Any, Any]]", expected "dict[Any, Any]") [return-value]


@classmethod
def resolve(cls, obj:Any)->dict|list[dict]:
def express(cls, obj:Any)->dict|list[dict]:
"""Resolves an expression encapsulated in an object from a class inheriting from BaseModel"""

return resolve(obj)
return express(obj)

@property
@abstractmethod
def statement(self)->dict:
def expression(self)->dict:
"""Stage statement absctract method"""

# this is a lazy attribute
Expand All @@ -49,7 +54,7 @@ def statement(self)->dict:
def __call__(self)->dict:
"""Makes an instance of any class inheriting from this class callable"""

return self.resolve(self.statement)
return self.express(self.expression)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [mypy] reported by reviewdog 🐶
Incompatible return value type (got "dict[Any, Any] | list[dict[Any, Any]]", expected "dict[Any, Any]") [return-value]


class Config(pyd.BaseConfig):
"""Base configuration for classes inheriting from this"""
Expand All @@ -65,25 +70,25 @@ def isbasemodel(instance:Any)->TypeGuard[BaseModel]:

return isinstance(instance, BaseModel)

def resolve(obj:Any)->dict|list[dict]:
def express(obj:Any)->dict|list[dict]:
"""Resolves an expression encapsulated in an object from a class inheriting from BaseModel"""

if isbasemodel(obj):
output:dict|list = obj.statement
output:dict|list = obj.expression
elif isinstance(obj, list) and any(map(isbasemodel, obj)):
output = []
for element in obj:
if isinstance(element, BaseModel):
output.append(element.statement)
output.append(element.expression)
else:
output.append(element)
elif isinstance(obj, dict):
output = {}
for key, value in obj.items():
if isinstance(value, BaseModel):
output[key] = value.statement
output[key] = value.expression
else:
output[key] = resolve(value)
output[key] = express(value)
else:
output = obj

Expand Down
16 changes: 8 additions & 8 deletions src/monggregate/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

# 3rd Party imports
# ---------------------------
from monggregate.base import BaseModel, pyd
from monggregate.base import BaseModel

# Local imports
# ----------------------------
Expand Down Expand Up @@ -38,8 +38,8 @@ class Expression(BaseModel):
content : Any

@property
def statement(self)->Any:
return self.resolve(self.content)
def expression(self)->Any:
return self.express(self.content)

#----------------------------------------------------
# Expression Internal Methods
Expand All @@ -64,8 +64,8 @@ def field(cls, name:str)->Self:
def variable(cls, name:str)->Self:
"""Creates a variable expression."""

while not variable.startswith("$$"):
variable = "$" + variable
while not name.startswith("$$"):
name = "$" + name

return cls(content=Variable(name))

Expand Down Expand Up @@ -319,7 +319,7 @@ def __and__(self, other:Self)->Self:
"""


return self.__class__(content=boolean.And(expressions=[self, other]))
return self.__class__(content=boolean.And(operands=[self, other]))


def __or__(self, other:Self)->Self:
Expand All @@ -330,7 +330,7 @@ def __or__(self, other:Self)->Self:
"""


return self.__class__(content=boolean.Or(expressions=[self, other]))
return self.__class__(content=boolean.Or(operands=[self, other]))


def __invert__(self)->Self:
Expand Down Expand Up @@ -474,7 +474,7 @@ def concat(self, *args:Self)->Self:
"""Creates a $concat operator"""


return self.__class__(content=strings.Concat(expressions=[self, *args]))
return self.__class__(content=strings.Concat(operands=[self, *args]))

def date_from_string(self)->Self:
"""Creates a $dateFromString operator"""
Expand Down
10 changes: 5 additions & 5 deletions src/monggregate/operators/accumulators/avg.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class Average(Accumulator):

Attributes
----------
expression : Any valid expression
operand : Any valid expression

Online MongoDB documentation:
-----------------------------
Expand All @@ -105,17 +105,17 @@ class Average(Accumulator):
expression : Any

@property
def statement(self) -> dict:
def expression(self) -> dict:

return self.resolve({
return self.express({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [mypy] reported by reviewdog 🐶
Incompatible return value type (got "dict[Any, Any] | list[dict[Any, Any]]", expected "dict[Any, Any]") [return-value]

"$avg" : self.expression
})

Avg = Average

def average(expression:Any)->Average:
def average(operand:Any)->Average:
"""Returns a $avg operator"""

return Average(expression=expression)
return Average(expression=operand)

avg = average
4 changes: 2 additions & 2 deletions src/monggregate/operators/accumulators/count.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@

[Source](https://www.mongodb.com/docs/manual/reference/operator/aggregation/count-accumulator/#mongodb-group-grp.-count)
"""

Check warning on line 69 in src/monggregate/operators/accumulators/count.py

View workflow job for this annotation

GitHub Actions / lint-and-format-backend

[black-format] reported by reviewdog 🐶 Raw Output: /home/runner/work/monggregate/monggregate/src/monggregate/operators/accumulators/count.py:69:- /home/runner/work/monggregate/monggregate/src/monggregate/operators/accumulators/count.py:70:-


@property
def statement(self) -> dict:
def expression(self) -> dict:

return self.resolve({
return self.express({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [mypy] reported by reviewdog 🐶
Incompatible return value type (got "dict[Any, Any] | list[dict[Any, Any]]", expected "dict[Any, Any]") [return-value]

"$count" : {}
})

Expand Down
6 changes: 3 additions & 3 deletions src/monggregate/operators/accumulators/first.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@

Attributes
------------------------
- expression, Expression : Any valid expression
- operand, Any : Any valid expression

Online MongoDB documentation:
------------------------------
Expand All @@ -88,13 +88,13 @@
[Source](https://www.mongodb.com/docs/manual/reference/operator/aggregation/first/#mongodb-group-grp.-first)
"""

expression : Any

Check warning on line 91 in src/monggregate/operators/accumulators/first.py

View workflow job for this annotation

GitHub Actions / lint-and-format-backend

[black-format] reported by reviewdog 🐶 Raw Output: /home/runner/work/monggregate/monggregate/src/monggregate/operators/accumulators/first.py:91:- expression : Any /home/runner/work/monggregate/monggregate/src/monggregate/operators/accumulators/first.py:92:- /home/runner/work/monggregate/monggregate/src/monggregate/operators/accumulators/first.py:91:+ expression: Any


@property
def statement(self) -> dict:
def expression(self) -> dict:

return self.resolve({
return self.express({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [mypy] reported by reviewdog 🐶
Incompatible return value type (got "dict[Any, Any] | list[dict[Any, Any]]", expected "dict[Any, Any]") [return-value]

"$first" : self.expression
})

Expand Down
10 changes: 5 additions & 5 deletions src/monggregate/operators/accumulators/last.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@

Attributes
------------------------
- expression, Expression : Any valid expression
- operand, Any:Any valid expression

Online MongoDB documentation
----------------------------
Expand All @@ -80,18 +80,18 @@
[Source](https://www.mongodb.com/docs/manual/reference/operator/aggregation/last/#mongodb-group-grp.-last)
"""

expression : Any

Check warning on line 83 in src/monggregate/operators/accumulators/last.py

View workflow job for this annotation

GitHub Actions / lint-and-format-backend

[black-format] reported by reviewdog 🐶 Raw Output: /home/runner/work/monggregate/monggregate/src/monggregate/operators/accumulators/last.py:83:- expression : Any /home/runner/work/monggregate/monggregate/src/monggregate/operators/accumulators/last.py:84:- /home/runner/work/monggregate/monggregate/src/monggregate/operators/accumulators/last.py:85:- /home/runner/work/monggregate/monggregate/src/monggregate/operators/accumulators/last.py:83:+ expression: Any



@property
def statement(self) -> dict:
def expression(self) -> dict:

return self.resolve({
return self.express({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [mypy] reported by reviewdog 🐶
Incompatible return value type (got "dict[Any, Any] | list[dict[Any, Any]]", expected "dict[Any, Any]") [return-value]

"$last" : self.expression
})

def last(expression:Any)->Last:
def last(operand:Any)->Last:
"""Returns a $last operator"""

return Last(expression=expression)
return Last(expression=operand)
10 changes: 5 additions & 5 deletions src/monggregate/operators/accumulators/max.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@

Attributes
---------------------
- expression, Expression : Any valid expression
- operand, Any:Any valid expression

Online MongoDB documentation
----------------------------
Expand All @@ -109,18 +109,18 @@
[Source](https://www.mongodb.com/docs/manual/reference/operator/aggregation/max/#mongodb-group-grp.-max)
"""

expression : Any

Check warning on line 112 in src/monggregate/operators/accumulators/max.py

View workflow job for this annotation

GitHub Actions / lint-and-format-backend

[black-format] reported by reviewdog 🐶 Raw Output: /home/runner/work/monggregate/monggregate/src/monggregate/operators/accumulators/max.py:112:- expression : Any /home/runner/work/monggregate/monggregate/src/monggregate/operators/accumulators/max.py:113:- /home/runner/work/monggregate/monggregate/src/monggregate/operators/accumulators/max.py:114:- /home/runner/work/monggregate/monggregate/src/monggregate/operators/accumulators/max.py:112:+ expression: Any



@property
def statement(self) -> dict:
def expression(self) -> dict:

return self.resolve({
return self.express({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [mypy] reported by reviewdog 🐶
Incompatible return value type (got "dict[Any, Any] | list[dict[Any, Any]]", expected "dict[Any, Any]") [return-value]

"$max" : self.expression
})

def max(expression:Any)->Max:
def max(operand:Any)->Max:
"""Returns a $last operator"""

return Max(expression=expression)
return Max(expression=operand)
10 changes: 5 additions & 5 deletions src/monggregate/operators/accumulators/min.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@

Attributes
----------------------
- expression, Expression : Any valid expression
- operand, Any:Any valid expression


Online MongoDB documentation
Expand All @@ -109,18 +109,18 @@
[Source](https://www.mongodb.com/docs/manual/reference/operator/aggregation/min/#mongodb-group-grp.-min)
"""

expression : Any

Check warning on line 112 in src/monggregate/operators/accumulators/min.py

View workflow job for this annotation

GitHub Actions / lint-and-format-backend

[black-format] reported by reviewdog 🐶 Raw Output: /home/runner/work/monggregate/monggregate/src/monggregate/operators/accumulators/min.py:112:- expression : Any /home/runner/work/monggregate/monggregate/src/monggregate/operators/accumulators/min.py:113:- /home/runner/work/monggregate/monggregate/src/monggregate/operators/accumulators/min.py:114:- /home/runner/work/monggregate/monggregate/src/monggregate/operators/accumulators/min.py:113:+ expression: Any



@property
def statement(self) -> dict:
def expression(self) -> dict:

return self.resolve({
return self.express({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [mypy] reported by reviewdog 🐶
Incompatible return value type (got "dict[Any, Any] | list[dict[Any, Any]]", expected "dict[Any, Any]") [return-value]

"$min" : self.expression
})

def min(expression:Any)->Min:
def min(operand:Any)->Min:
"""Returns a $min operator"""

return Min(expression=expression)
return Min(expression=operand)
10 changes: 5 additions & 5 deletions src/monggregate/operators/accumulators/push.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

Attributes
-------------------
- expression, Expression : Any valid expression
- operand, Any:Any valid expression

Online MongoDB documentation
----------------------------
Expand All @@ -52,18 +52,18 @@
[Source](https://www.mongodb.com/docs/manual/reference/operator/aggregation/push/#mongodb-group-grp.-push)
"""

expression : Any

Check warning on line 55 in src/monggregate/operators/accumulators/push.py

View workflow job for this annotation

GitHub Actions / lint-and-format-backend

[black-format] reported by reviewdog 🐶 Raw Output: /home/runner/work/monggregate/monggregate/src/monggregate/operators/accumulators/push.py:55:- expression : Any /home/runner/work/monggregate/monggregate/src/monggregate/operators/accumulators/push.py:56:- /home/runner/work/monggregate/monggregate/src/monggregate/operators/accumulators/push.py:57:- /home/runner/work/monggregate/monggregate/src/monggregate/operators/accumulators/push.py:56:+ expression: Any



@property
def statement(self) -> dict:
def expression(self) -> dict:

return self.resolve({
return self.express({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [mypy] reported by reviewdog 🐶
Incompatible return value type (got "dict[Any, Any] | list[dict[Any, Any]]", expected "dict[Any, Any]") [return-value]

"$push" : self.expression
})

def push(expression:Any)->Push:
def push(operand:Any)->Push:
"""Returns a $push operator"""

return Push(expression=expression)
return Push(expression=operand)
6 changes: 3 additions & 3 deletions src/monggregate/operators/accumulators/sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
Attributes
-----------------------
- operands, list[Expression] : Any valid list of expressions
- operand, Expression : Any valid expression
- operand, Any :Any valid expression

Online MongoDB documentation
----------------------------
Expand All @@ -119,13 +119,13 @@
[Source](https://www.mongodb.com/docs/manual/reference/operator/aggregation/sum/#mongodb-group-grp.-sum)
"""

expression : Any

Check warning on line 122 in src/monggregate/operators/accumulators/sum.py

View workflow job for this annotation

GitHub Actions / lint-and-format-backend

[black-format] reported by reviewdog 🐶 Raw Output: /home/runner/work/monggregate/monggregate/src/monggregate/operators/accumulators/sum.py:122:- expression : Any /home/runner/work/monggregate/monggregate/src/monggregate/operators/accumulators/sum.py:123:- /home/runner/work/monggregate/monggregate/src/monggregate/operators/accumulators/sum.py:123:+ expression: Any


@property
def statement(self) -> dict:
def expression(self) -> dict:

return self.resolve({
return self.express({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [mypy] reported by reviewdog 🐶
Incompatible return value type (got "dict[Any, Any] | list[dict[Any, Any]]", expected "dict[Any, Any]") [return-value]

"$sum" : self.expression
})

Expand Down
10 changes: 5 additions & 5 deletions src/monggregate/operators/arithmetic/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,17 @@ class Add(ArithmeticOperator):
"""


expressions : list[Any]
operands : list[Any]

@property
def statement(self) -> dict:
return self.resolve({
"$add" : self.expressions
def expression(self) -> dict:
return self.express({
"$add" : self.operands
})

def add(*args:Any)->Add:
"""Returns a $add operator"""

return Add(
expressions=list(args)
operands=list(args)
)
4 changes: 2 additions & 2 deletions src/monggregate/operators/arithmetic/divide.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@
[Source](https://docs.mongodb.com/manual/reference/operator/aggregation/divide/#mongodb-expression-exp.-divide)
"""


Check warning on line 55 in src/monggregate/operators/arithmetic/divide.py

View workflow job for this annotation

GitHub Actions / lint-and-format-backend

[black-format] reported by reviewdog 🐶 Raw Output: /home/runner/work/monggregate/monggregate/src/monggregate/operators/arithmetic/divide.py:55:- /home/runner/work/monggregate/monggregate/src/monggregate/operators/arithmetic/divide.py:56:- numerator : Any /home/runner/work/monggregate/monggregate/src/monggregate/operators/arithmetic/divide.py:57:- denominator : Any /home/runner/work/monggregate/monggregate/src/monggregate/operators/arithmetic/divide.py:56:+ numerator: Any /home/runner/work/monggregate/monggregate/src/monggregate/operators/arithmetic/divide.py:57:+ denominator: Any
numerator : Any
denominator : Any

@property
def statement(self) -> dict:
return self.resolve({
def expression(self) -> dict:
return self.express({

Check warning on line 61 in src/monggregate/operators/arithmetic/divide.py

View workflow job for this annotation

GitHub Actions / lint-and-format-backend

[black-format] reported by reviewdog 🐶 Raw Output: /home/runner/work/monggregate/monggregate/src/monggregate/operators/arithmetic/divide.py:61:- return self.express({ /home/runner/work/monggregate/monggregate/src/monggregate/operators/arithmetic/divide.py:62:- "$divide" : [self.numerator, self.denominator] /home/runner/work/monggregate/monggregate/src/monggregate/operators/arithmetic/divide.py:63:- }) /home/runner/work/monggregate/monggregate/src/monggregate/operators/arithmetic/divide.py:64:- /home/runner/work/monggregate/monggregate/src/monggregate/operators/arithmetic/divide.py:65:-def divide(numerator:Any, denominator:Any)->Divide: /home/runner/work/monggregate/monggregate/src/monggregate/operators/arithmetic/divide.py:61:+ return self.express({"$divide": [self.numerator, self.denominator]}) /home/runner/work/monggregate/monggregate/src/monggregate/operators/arithmetic/divide.py:62:+ /home/runner/work/monggregate/monggregate/src/monggregate/operators/arithmetic/divide.py:63:+ /home/runner/work/monggregate/monggregate/src/monggregate/operators/arithmetic/divide.py:64:+def divide(numerator: Any, denominator: Any) -> Divide:
"$divide" : [self.numerator, self.denominator]
})

Expand Down
10 changes: 5 additions & 5 deletions src/monggregate/operators/arithmetic/multiply.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,17 @@ class Multiply(ArithmeticOperator):
"""


expressions : list[Any]
operands : list[Any]

@property
def statement(self) -> dict:
return self.resolve({
"$multiply" : self.expressions
def expression(self) -> dict:
return self.express({
"$multiply" : self.operands
})

def multiply(*args:Any)->Multiply:
"""Returns a $multiply operator"""

return Multiply(
expressions=list(args)
operands=list(args)
)
4 changes: 2 additions & 2 deletions src/monggregate/operators/arithmetic/pow.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@
[Source](https://docs.mongodb.com/manual/reference/operator/aggregation/pow/#mongodb-expression-exp.-pow)
"""


Check warning on line 71 in src/monggregate/operators/arithmetic/pow.py

View workflow job for this annotation

GitHub Actions / lint-and-format-backend

[black-format] reported by reviewdog 🐶 Raw Output: /home/runner/work/monggregate/monggregate/src/monggregate/operators/arithmetic/pow.py:71:- /home/runner/work/monggregate/monggregate/src/monggregate/operators/arithmetic/pow.py:72:- number : Any /home/runner/work/monggregate/monggregate/src/monggregate/operators/arithmetic/pow.py:73:- exponent : Any /home/runner/work/monggregate/monggregate/src/monggregate/operators/arithmetic/pow.py:72:+ number: Any /home/runner/work/monggregate/monggregate/src/monggregate/operators/arithmetic/pow.py:73:+ exponent: Any
number : Any
exponent : Any

@property
def statement(self) -> dict:
return self.resolve({
def expression(self) -> dict:
return self.express({
"$pow" : [self.number, self.exponent]
})

Expand Down
4 changes: 2 additions & 2 deletions src/monggregate/operators/arithmetic/subtract.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ class Subtract(ArithmeticOperator):
right: Any

@property
def statement(self) -> dict:
return self.resolve({
def expression(self) -> dict:
return self.express({
"$substract" : [self.left, self.right]
})

Expand Down
Loading
Loading