Skip to content

Commit

Permalink
Merge pull request #42 from VianneyMI/41-core-changes-to-package
Browse files Browse the repository at this point in the history
41 core changes to package
  • Loading branch information
VianneyMI committed Jun 11, 2023
2 parents 8f3b213 + 93c59b2 commit c36bdd4
Show file tree
Hide file tree
Showing 50 changed files with 181 additions and 146 deletions.
2 changes: 1 addition & 1 deletion monggregate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from monggregate.pipeline import Pipeline
from monggregate.expressions.expressions import Expression

__version__ = "0.11.0"
__version__ = "0.12.0"
__author__ = "Vianney Mixtur"
__contact__ = "prenom.nom@outlook.fr"
__copyright__ = "Copyright © 2022 Vianney Mixtur"
Expand Down
58 changes: 35 additions & 23 deletions monggregate/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,17 @@

# 3rd Party imports
# ---------------------------
from pydantic import BaseModel as PydanticBaseModel, BaseConfig, validator
from pydantic import BaseModel as PydanticBaseModel, BaseConfig
from humps import camelize

class BaseModel(PydanticBaseModel, ABC):
"""Mongreggate base class"""

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

def isbasemodel(instance:Any)->bool:
"""Returns true if instance is an instance of BaseModel"""

return isinstance(instance, BaseModel)

if isbasemodel(expression):
output:dict|list = expression.statement
elif isinstance(expression, list) and any(map(isbasemodel, expression)):
output = []
for element in expression:
if isinstance(element, BaseModel):
output.append(element.statement)
else:
output.append(element)
#elif isinstance(expression, dict): # Does this case really exist ?
else:
output = expression

return output
return resolve(obj)

@property
@abstractmethod
Expand All @@ -45,7 +27,6 @@ def statement(self)->dict:
# this is a lazy attribute
# what is currently in generate statement should go in here


def __call__(self)->dict:
"""Makes an instance of any class inheriting from this class callable"""

Expand All @@ -56,3 +37,34 @@ class Config(BaseConfig):

allow_population_by_field_name = True
underscore_attrs_are_private = True
alias_generator = camelize


def isbasemodel(instance:Any)->bool:
"""Returns true if instance is an instance of BaseModel"""

return isinstance(instance, BaseModel)

def resolve(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
elif isinstance(obj, list) and any(map(isbasemodel, obj)):
output = []
for element in obj:
if isinstance(element, BaseModel):
output.append(element.statement)
else:
output.append(element)
elif isinstance(obj, dict):
output = {}
for key, value in obj.items():
if isinstance(value, BaseModel):
output[key] = value.statement
else:
output[key] = resolve(value)
else:
output = obj

return output
5 changes: 3 additions & 2 deletions monggregate/operators/accumulators/avg.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,10 @@ class Average(Accumulator):
@property
def statement(self) -> dict:

return {
return self.resolve({
"$avg" : self.expression
}
})

Avg = Average

def average(expression:Any)->dict:
Expand Down
4 changes: 2 additions & 2 deletions monggregate/operators/accumulators/count.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ class Count(Accumulator):
@property
def statement(self) -> dict:

return {
return self.resolve({
"$count" : {}
}
})

def count()->dict:
"""Creates a $count statement"""
Expand Down
4 changes: 2 additions & 2 deletions monggregate/operators/accumulators/first.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ class First(Accumulator):
@property
def statement(self) -> dict:

return {
return self.resolve({
"$first" : self.expression
}
})

def first(expression:Any)->dict:
"""Creates a push statement"""
Expand Down
4 changes: 2 additions & 2 deletions monggregate/operators/accumulators/last.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ class Last(Accumulator):
@property
def statement(self) -> dict:

return {
return self.resolve({
"$last" : self.expression
}
})

def last(expression:Any)->dict:
"""Creates a push statement"""
Expand Down
4 changes: 2 additions & 2 deletions monggregate/operators/accumulators/max.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ class Max(Accumulator):
@property
def statement(self) -> dict:

return {
return self.resolve({
"$max" : self.expression
}
})

def max(expression:Any)->dict: # pylint: disable=redefined-builtin
"""Creates a push statement"""
Expand Down
4 changes: 2 additions & 2 deletions monggregate/operators/accumulators/min.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ class Min(Accumulator):
@property
def statement(self) -> dict:

return {
return self.resolve({
"$min" : self.expression
}
})

def min(expression:Any)->dict:
"""Creates a $min statement"""
Expand Down
4 changes: 2 additions & 2 deletions monggregate/operators/accumulators/push.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ class Push(Accumulator):
@property
def statement(self) -> dict:

return {
return self.resolve({
"$push" : self.expression
}
})

def push(expression:Any)->dict:
"""Creates a push statement"""
Expand Down
4 changes: 2 additions & 2 deletions monggregate/operators/accumulators/sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ class Sum(Accumulator):
@property
def statement(self) -> dict:

return {
return self.resolve({
"$sum" : self.expression
}
})

def sum(*args:Content)->dict: # pylint: disable=redefined-builtin
"""Creates a $sum statement"""
Expand Down
4 changes: 2 additions & 2 deletions monggregate/operators/array/array_to_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ class ArrayToObject(ArrayOperator):

@property
def statement(self) -> dict:
return {
return self.resolve({
"$arrayToObject" : self.expression
}
})

def array_to_object(expression:Any)->dict:
"""Returns an $arrayToObject statement"""
Expand Down
4 changes: 2 additions & 2 deletions monggregate/operators/array/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ class Filter(ArrayOperator):

@property
def statement(self) -> dict:
return {
return self.resolve({
"$filter":{
"input" : self.expression,
"cond" : self.query,
"as" : self.let,
"limit" : self.limit
}
}
})

def filter(expression:Any, let:str, query:Any, limit:int|None=None)->dict: # pylint: disable=redefined-builtin
"""Returns a $filter statement"""
Expand Down
4 changes: 2 additions & 2 deletions monggregate/operators/array/first.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ class First(ArrayOnlyOperator):

@property
def statement(self) -> dict:
return {
return self.resolve({
"$first":self.expression
}
})

def first(array:Any)->dict:
"""Returns a $first statement"""
Expand Down
4 changes: 2 additions & 2 deletions monggregate/operators/array/in_.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ class In(ArrayOperator):

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

def in_(left:Any, right:Any)->dict:
"""Returns a $maxN statement"""
Expand Down
4 changes: 2 additions & 2 deletions monggregate/operators/array/is_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ class IsArray(ArrayOnlyOperator):

@property
def statement(self) -> dict:
return {
return self.resolve({
"$isArray":self.expression
}
})

def is_array(array:Any)->dict:
"""Returns a $isArray statement"""
Expand Down
4 changes: 2 additions & 2 deletions monggregate/operators/array/last.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ class Last(ArrayOnlyOperator):

@property
def statement(self) -> dict:
return {
return self.resolve({
"$last":self.expression
}
})

def last(array:Any)->dict:
"""Returns a $last statement"""
Expand Down
4 changes: 2 additions & 2 deletions monggregate/operators/array/max_n.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ class MaxN(ArrayOperator):

@property
def statement(self) -> dict:
return {
return self.resolve({
"$maxN" : {
"n" : self.limit,
"input" : self.expression
}
}
})

def max_n(expression:Any, limit:Any=1)->dict:
"""Returns a $maxN statement"""
Expand Down
4 changes: 2 additions & 2 deletions monggregate/operators/array/min_n.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ class MinN(ArrayOperator):

@property
def statement(self) -> dict:
return {
return self.resolve({
"$minN" : {
"n" : self.limit,
"input" : self.expression
}
}
})

def min_n(expression:Any, limit:Any=1)->dict:
"""Returns a $minN statement"""
Expand Down
4 changes: 2 additions & 2 deletions monggregate/operators/array/size.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ class Size(ArrayOnlyOperator):

@property
def statement(self) -> dict:
return {
return self.resolve({
"$size":self.expression
}
})

def size(array:Any)->dict:
"""Returns a $size statement"""
Expand Down
4 changes: 2 additions & 2 deletions monggregate/operators/array/sort_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ class SortArray(ArrayOperator):

@property
def statement(self) -> dict:
return {
return self.resolve({
"$sortArray":{
"input" : self.expression,
"sortBy" : self.by
}
}
})

def sort_array(expression:Any, sort_by:dict[str, Literal[1, -1]])->dict:
"""Returns a $first statement"""
Expand Down
5 changes: 3 additions & 2 deletions monggregate/operators/boolean/and_.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,14 @@ class And(BooleanOperator):

@property
def statement(self) -> dict:
return {
return self.resolve({
"$and" : self.expressions
}
})

def and_(*args:Any)->dict:
"""Returns an $and statement"""

return And(
expressions=list(args)
).statement

4 changes: 2 additions & 2 deletions monggregate/operators/boolean/not_.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ class Not(BooleanOperator):

@property
def statement(self) -> dict:
return {
return self.resolve({
"$not" : [self.expression]
}
})

def not_(expression:Any)->dict:
"""Returns an $not statement"""
Expand Down
4 changes: 2 additions & 2 deletions monggregate/operators/boolean/or_.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ class Or(BooleanOperator):

@property
def statement(self) -> dict:
return {
return self.resolve({
"$or" : self.expressions
}
})

def or_(*args:Any)->dict:
"""Returns an $or statement"""
Expand Down
4 changes: 2 additions & 2 deletions monggregate/operators/comparison/cmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ class Compare(Comparator):
@property
def statement(self) -> dict:

return {
return self.resolve({
"$cmp":[self.left, self.right]
}
})

Cmp = Compare

Expand Down
4 changes: 2 additions & 2 deletions monggregate/operators/comparison/eq.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ class Equal(Comparator):
@property
def statement(self) -> dict:

return {
return self.resolve({
"$eq":[self.left, self.right]
}
})

Eq = Equal

Expand Down
4 changes: 2 additions & 2 deletions monggregate/operators/comparison/gt.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ class GreatherThan(Comparator):
@property
def statement(self) -> dict:

return {
return self.resolve({
"$gt":[self.left, self.right]
}
})

Gt = GreatherThan

Expand Down
Loading

0 comments on commit c36bdd4

Please sign in to comment.