def __call__(self, name: Optional[str] = None, key: Optional[Callable] = None, **kwargs) -> Any: """Override standard call. This magic method override standard call so it's take any string which represents name of the any method of any supported data provider and the ``**kwargs`` of this method. ..note:: Some data providers have methods with the same name and in such cases, you can explicitly define that the method belongs to data-provider ``name='provider.name'``. You can apply a *key function* to result returned by the method, to do it, just pass parameter ``key`` with a callable object which returns final result. :param name: Name of the method. :param key: A key function (or other callable object) which will be applied to result. :param kwargs: Kwargs of method. :return: Value which represented by method. :raises ValueError: if provider is not supported or if field is not defined. """ if name is None: raise UndefinedField() def tail_parser(tails: str, obj: Any) -> Any: """Return method from end of tail. :param tails: Tail string :param obj: Search tail from this object :return last tailed method """ first, second = tails.split('.', 1) if hasattr(obj, first): attr = getattr(obj, first) if '.' in second: return tail_parser(attr, second) else: return getattr(attr, second) try: if name not in self._table: if '.' not in name: for provider in dir(self._gen): provider = getattr(self._gen, provider) if name in dir(provider): self._table[name] = getattr(provider, name) else: self._table[name] = tail_parser(name, self._gen) result = self._table[name](**kwargs) if key and callable(key): return key(result) return result except Exception: raise UnsupportedField(name)
def __call__(self, name: Optional[str] = None, key: Optional[Callable] = None, **kwargs) -> Any: """This magic override standard call so it's take any string which represents name of the any method of any supported data provider and the ``**kwargs`` of this method. :param name: Name of method. :param key: A key function (or other callable object) which will be applied to result. :param kwargs: Kwargs of method. :return: Value which represented by method. :raises ValueError: if provider is not supported or if field is not defined. """ if name is None: raise UndefinedField() # TODO: This is a really slow solution. Fix it. for provider in dir(self.gen): if hasattr(self.gen, provider): provider = getattr(self.gen, provider) if name in dir(provider): method = getattr(provider, name) result = method(**kwargs) if key and callable(key): return key(result) return result else: continue else: raise UnsupportedField(name)
def __call__(self, name: Optional[str] = None, key: Optional[Callable] = None, **kwargs) -> Any: """Override standard call. This magic method overrides standard call so it takes any string which represents the name of any method of any supported data provider and the ``**kwargs`` of this method. .. note:: Some data providers have methods with the same names and in such cases, you can explicitly define that the method belongs to data-provider ``name='provider.name'`` otherwise it will return the data from the first provider which has a method ``name``. You can apply a *key function* to the result returned by the method, bt passing a parameter **key** with a callable object which returns the final result. :param name: Name of the method. :param key: A key function (or other callable object) which will be applied to result. :param kwargs: Kwargs of method. :return: Value which represented by method. :raises ValueError: if provider not supported or if field not defined. """ if name is None: raise UndefinedField() def tail_parser(tails: str, obj: Any) -> Any: """Return method from end of tail. :param tails: Tail string :param obj: Search tail from this object :return last tailed method """ provider_name, method_name = tails.split('.', 1) if '.' in method_name: raise UnacceptableField() attr = getattr(obj, provider_name) if attr is not None: return getattr(attr, method_name) try: if name not in self._table: if '.' not in name: # Fix https://github.com/lk-geimfari/mimesis/issues/619 if name == self._gen.choice.Meta.name: self._table[name] = self._gen.choice else: for provider in dir(self._gen): provider = getattr(self._gen, provider) if name in dir(provider): self._table[name] = getattr(provider, name) else: self._table[name] = tail_parser(name, self._gen) result = self._table[name](**kwargs) if key and callable(key): return key(result) return result except KeyError: raise UnsupportedField(name)