Skip to content

Commit 3bb0933

Browse files
authored
72 expressions improvements (#117)
* Renamed statement as expression * Renamed expression as operand * Renamed resolve as express * expression -> operand * follow up * Fixed tests * Added mypy.ini and updated gitignore * Replaced old dict typehints by Expression * Follow up * Replaced dict typehint by Expression in operators * Follow up for search operators * FIXME : Fix lookup, join, right join and associated tests following collection removal in pipeline * Removed right joins * Removed old expressions module * Fixed test
1 parent 733c372 commit 3bb0933

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

101 files changed

+644
-1203
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
venv
22
.venv
3+
.coverage
34
.env
45
notes.md
56
**/*~

docs/index.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ import os
127127

128128
from dotenv import load_dotenv
129129
import pymongo
130-
from monggregate import Pipeline, S, Expression
130+
from monggregate import Pipeline, S
131131

132132
# Creating connexion string securely
133133
load_dotenv(verbose=True)
@@ -142,8 +142,7 @@ client = pymongo.MongoClient(MONGODB_URI)
142142
db = client["sample_mflix"]
143143

144144
# Using expressions
145-
comments_count = Expression.field("comments").size()
146-
145+
comments_count = S.size(S.comments)
147146

148147
# Creating the pipeline
149148
pipeline = Pipeline()

mypy.ini

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[mypy]
2+
show_column_numbers = True
3+
implicit_optional = False
4+
disallow_untyped_defs = True
5+
disallow_untyped_calls = True
6+
follow_imports = silent
7+
ignore_missing_imports = True
8+
plugins = pydantic.mypy
9+
explicit_package_bases = True

readme.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ import os
127127

128128
from dotenv import load_dotenv
129129
import pymongo
130-
from monggregate import Pipeline, S, Expression
130+
from monggregate import Pipeline, S
131131

132132
# Creating connexion string securely
133133
load_dotenv(verbose=True)
@@ -142,8 +142,7 @@ client = pymongo.MongoClient(MONGODB_URI)
142142
db = client["sample_mflix"]
143143

144144
# Using expressions
145-
comments_count = Expression.field("comments").size()
146-
145+
comments_count = S.size(S.comments)
147146

148147
# Creating the pipeline
149148
pipeline = Pipeline()

src/monggregate/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
"""App Package"""
22

3-
from monggregate.expressions import Expression
43
from monggregate.dollar import S, SS
54
from monggregate.pipeline import Pipeline
65

7-
__all__ = ["Expression", "S", "SS", "Pipeline"]
6+
__all__ = ["Pipeline", "S", "SS"]
87

98
__version__ = "0.21.0"
109
__author__ = "Vianney Mixtur"

src/monggregate/_run.py

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/monggregate/base.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
#----------------------------
99
from abc import ABC, abstractmethod
1010
from typing import Any, TypeGuard
11+
from typing_extensions import Self
1112

1213

1314
# 3rd Party imports
1415
# ---------------------------
1516
try:
1617
import pydantic.v1 as pyd
1718
except ModuleNotFoundError:
18-
import pydantic as pyd
19+
import pydantic as pyd # type: ignore[no-redef]
1920

2021

2122
from humps.main import camelize
@@ -24,32 +25,39 @@ class Singleton:
2425
"""Singleton metaclass"""
2526

2627
_instance = None
27-
def __new__(cls, *args, **kwargs):
28+
def __new__(cls, *args:Any, **kwargs:Any)->Self:
2829
if not isinstance(cls._instance, cls):
2930
cls._instance = object.__new__(cls, *args, **kwargs)
3031
return cls._instance
32+
33+
Expression = dict[str, Any]
3134

3235
class BaseModel(pyd.BaseModel, ABC):
3336
"""Mongreggate base class"""
3437

38+
def to_expression(self)->Expression|list[Expression]:
39+
"""Converts an instance of a class inheriting from BaseModel to an expression"""
40+
41+
return self.express(self)
42+
3543
@classmethod
36-
def resolve(cls, obj:Any)->dict|list[dict]:
44+
def express(cls, obj:Any)->Expression|list[Expression]:
3745
"""Resolves an expression encapsulated in an object from a class inheriting from BaseModel"""
3846

39-
return resolve(obj)
47+
return express(obj)
4048

4149
@property
4250
@abstractmethod
43-
def statement(self)->dict:
51+
def expression(self)->Expression:
4452
"""Stage statement absctract method"""
4553

4654
# this is a lazy attribute
4755
# what is currently in generate statement should go in here
4856

49-
def __call__(self)->dict:
57+
def __call__(self)->Expression|list[Expression]:
5058
"""Makes an instance of any class inheriting from this class callable"""
5159

52-
return self.resolve(self.statement)
60+
return self.to_expression()
5361

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

6674
return isinstance(instance, BaseModel)
6775

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

7179
if isbasemodel(obj):
72-
output:dict|list = obj.statement
80+
output:dict|list = obj.expression
7381
elif isinstance(obj, list) and any(map(isbasemodel, obj)):
7482
output = []
7583
for element in obj:
7684
if isinstance(element, BaseModel):
77-
output.append(element.statement)
85+
output.append(element.expression)
7886
else:
7987
output.append(element)
8088
elif isinstance(obj, dict):
8189
output = {}
8290
for key, value in obj.items():
8391
if isinstance(value, BaseModel):
84-
output[key] = value.statement
92+
output[key] = value.expression
8593
else:
86-
output[key] = resolve(value)
94+
output[key] = express(value)
8795
else:
8896
output = obj
8997

0 commit comments

Comments
 (0)