Another question from me :sweat_smile: What would ...
# questions
n
Another question from me 😅 What would be the proper way to describe a dataset in the catalog with mongodb source? I can see there is pandas.SQLDataset, but is there something similar for mongodb?
d
there isn’t unfortunately
z
I create some prototype for now
Copy code
class MongoDBDataSet(AbstractDataSet[Collection, Dict]):
    def __init__(
            self,
            database: str,
            collection: str,
            params: Dict[str, Any] = None,
            load_from_func: Dict[str, Any] = None,
            save_from_func: Dict[str, Any] = None

    ) -> None:
        self.database = database
        self.collection = collection
        self.params = params
        self.load_from_func = load_from_func,
        self.save_from_func = save_from_func,
        super().__init__()

    def _load(self):
        mongodb_client = MongoClient(**self.params)
        if self.load_from_func:
            return self.call_function_from_obj(mongodb_client.get_database(self.database).get_collection(self.collection), self.load_from_func[0])
        else:
            return mongodb_client.get_database(self.database).get_collection(self.collection).find()

    def _save(self, data: Dict) -> None:
        mongodb_client = MongoClient(**self.params)
        if self.save_from_func:
            self.call_function_from_obj(mongodb_client.get_database(self.database).get_collection(self.collection), self.save_from_func[0])
        else:
            mongodb_client.get_database(self.database).get_collection(self.collection).insert_one(data)

    def _exists(self) -> bool:
        mongodb_client = MongoClient(**self.params)
        if mongodb_client.get_database(self.database).get_collection(self.collection).find():
            return True
        else:
            return False

    def _describe(self):
        return dict(database=self.database,
                    collection=self.collection,
                    params=self.params)

    @staticmethod
    def call_function_from_obj(obj, dict_map: Dict[Any, Any]):
        def _getattr(obj, attr):

            if re.match(r"(.*?)", attr):
                """Eval if only there is function call"""
                return eval(f'obj.{attr}')
            else:
                """Recurses through an attribute chain to get the ultimate value."""
                return reduce(getattr, attr.split('.'), obj)

        if dict_map and dict_map.get('func', ''):
            try:
                # _call = getattr(obj, f"{dict_map.get('func', '')}")
                _call = _getattr(obj, f"{dict_map.get('func', '')}")
                _args = dict_map.get('args', [])
                _kwargs = dict_map.get('kwargs', {})
                return _call(*_args, **_kwargs)
            except Exception as err:
                raise DataSetError(err)
                
        else:
            return None
kedroid 1
K 1
also i add option if soment whant to use specific function
Copy code
load_from_func
save_from_func
it is in test phase
Copy code
load_from_func:
  func: ''
  args: []
  kwargs: {}
forgot in last picture to add func
d
AMAZING!
thanks @Zoran you dropped this 👑
z
you are welcome