""" :class:`~allennlp.common.tqdm.Tqdm` wraps tqdm so we can add configurable global defaults for certain tqdm parameters. """
from tqdm import tqdm as _tqdm # This is neccesary to stop tqdm from hanging # when exceptions are raised inside iterators. # It should have been fixed in 4.2.1, but it still # occurs. # TODO(Mark): Remove this once tqdm cleans up after itself properly. # https://github.com/tqdm/tqdm/issues/469 _tqdm.monitor_interval = 0
classTqdm: # These defaults are the same as the argument defaults in tqdm. default_mininterval: float = 0.1
@staticmethod defset_default_mininterval(value: float) -> None: Tqdm.default_mininterval = value
@staticmethod defset_slower_interval(use_slower_interval: bool) -> None: """ If ``use_slower_interval`` is ``True``, we will dramatically slow down ``tqdm's`` default output rate. ``tqdm's`` default output rate is great for interactively watching progress, but it is not great for log files. You might want to set this if you are primarily going to be looking at output through log files, not the terminal. """ if use_slower_interval: Tqdm.default_mininterval = 10.0 else: Tqdm.default_mininterval = 0.1
@classmethod defregister(cls: Type[T], name: str): registry = Registrable._registry[cls] defadd_subclass_to_registry(subclass: Type[T]): # Add to registry, raise an error if key has already been used. if name in registry: message = "Cannot register %s as %s; name already in use for %s" % ( name, cls.__name__, registry[name].__name__) raise ConfigurationError(message) registry[name] = subclass return subclass return add_subclass_to_registry
@classmethod defby_name(cls: Type[T], name: str) -> Type[T]: logger.debug(f"instantiating registered subclass {name} of {cls}") if name notin Registrable._registry[cls]: raise ConfigurationError("%s is not a registered name for %s" % (name, cls.__name__)) return Registrable._registry[cls].get(name)
classFromParams: """ Mixin to give a from_params method to classes. We create a distinct base class for this because sometimes we want non-Registrable classes to be instantiatable from_params. """ @classmethod deffrom_params(cls: Type[T], params: Params, **extras) -> T: """ This is the automatic implementation of `from_params`. Any class that subclasses `FromParams` (or `Registrable`, which itself subclasses `FromParams`) gets this implementation for free. If you want your class to be instantiated from params in the "obvious" way -- pop off parameters and hand them to your constructor with the same names -- this provides that functionality. If you need more complex logic in your from `from_params` method, you'll have to implement your own method that overrides this one. """ ... # implementation codes
# Extract archive to temp dir tempdir = tempfile.mkdtemp() logger.info(f"extracting archive file {resolved_archive_file} to temp dir {tempdir}") with tarfile.open(resolved_archive_file, 'r:gz') as archive: archive.extractall(tempdir) # Postpone cleanup until exit in case the unarchived contents are needed outside # this function. atexit.register(_cleanup_archive_dir, tempdir)