Exemplo n.º 1
0
    def __call__(cls, *args, **kwargs):
        signature = inspect.signature(cls.__init__)
        positional_in_kwargs = [
            p.name for p in signature.parameters.values()
            if p.kind == p.POSITIONAL_OR_KEYWORD and p.default == p.empty
            and p.name in kwargs
        ]

        if positional_in_kwargs:
            warn(f"Positional arguments {positional_in_kwargs} were "
                 f"supplied as keyword arguments to "
                 f"{cls.__init__}{signature}. "
                 f"This may create separate Multiton instances "
                 f"for what is intended to be a unique set of "
                 f"arguments.")

        key = freeze(args + (kwargs if kwargs else Multiton.MISSING, ))

        # Double-checked locking
        # https://en.wikipedia.org/wiki/Double-checked_locking
        try:
            return cls.__cache[key]
        except KeyError:
            pass

        with cls.__lock:
            try:
                return cls.__cache[key]
            except KeyError:
                instance = type.__call__(cls, *args, **kwargs)
                cls.__cache[key] = instance
                return instance
Exemplo n.º 2
0
 def __hash__(self):
     return hash(
         freeze(
             (
                 self.url,
                 self.storage_options,
             )
         )
     )
Exemplo n.º 3
0
 def __hash__(self):
     return hash((
         self.type,
         self.dims,
         self.dtype,
         self.chunks,
         self.shape,
         freeze(self.attrs),
     ))
Exemplo n.º 4
0
    def __call__(cls, *args, **kwargs):
        key = freeze((cls, ) + args + (kwargs, ))

        try:
            return _dataset_cache[key]
        except KeyError:
            with _dataset_lock:
                try:
                    return _dataset_cache[key]
                except KeyError:
                    instance = type.__call__(cls, *args, **kwargs)
                    _dataset_cache[key] = instance
                    return instance
Exemplo n.º 5
0
 def __hash__(self):
     return hash(freeze((self.data_vars, self.coords, self.attrs)))