<commit_before>import logging
import uuid
import time
import sys

from pyhocon import ConfigTree
from typing import Tuple, List, Any, Dict, Union, Type, Optional

from .instancemanager import InstanceManager
from . import Instance
from ..utils import get_free_tcp_port, random_str, docker_available

log = logging.getLogger()


def _map_docker_to_instance(container, port=-1, is_broker: bool = False) -> Instance:
    return Instance(str(container.name), '127.0.0.1', port=port, is_broker=is_broker, **container.labels)


try:
    assert docker_available()
    import docker
    DockerContainerT = Type[docker.models.containers.Container]
except Exception:
    pass


class Local(InstanceManager[DockerContainerT]):
    def __init__(self, conf: ConfigTree, internal_conf: ConfigTree) -> None:
        if not docker_available():
            raise AssertionError('Trying to instantiate Local InstanceManager, but docker-py is not available.')
        self.docker_client = docker.from_env()

        # Because of the dynamic type variable, this fails to typecheck
        # since DockerContainerT will be typed to Type[Any]
        self.broker: DockerContainerT = ...  # type: ignore
        self.worker: DockerContainerT = ...  # type: ignore

        self.run_id = run_id = random_str(8)<commit_msg>Fix NameError when docker-py is not available<commit_after>import logging
import uuid
import time
import sys

from pyhocon import ConfigTree
from typing import Tuple, List, Any, Dict, Union, Type, Optional

from .instancemanager import InstanceManager
from . import Instance
from ..utils import get_free_tcp_port, random_str, docker_available

log = logging.getLogger()


def _map_docker_to_instance(container, port=-1, is_broker: bool = False) -> Instance:
    return Instance(str(container.name), '127.0.0.1', port=port, is_broker=is_broker, **container.labels)


try:
    assert docker_available()
    import docker
    DockerContainerT = Type[docker.models.containers.Container]
except Exception:
    DockerContainerT = ...
    pass


class Local(InstanceManager[DockerContainerT]):  # type: ignore
    def __init__(self, conf: ConfigTree, internal_conf: ConfigTree) -> None:
        if not docker_available():
            raise AssertionError('Trying to instantiate Local InstanceManager, but docker-py is not available.')
        self.docker_client = docker.from_env()

        # Because of the dynamic type variable, this fails to typecheck
        # since DockerContainerT will be typed to Type[Any]
        self.broker: DockerContainerT = ...  # type: ignore
        self.worker: DockerContainerT = ...  # type: ignore

        self.run_id = run_id = random_str(8)
{'fraction_non_alphanumeric': 0.08798202920254586, 'fraction_numerical': 0.005990265818045676, 'mean_word_length': 3.875912408759124, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5978245', 'n_tokens_mistral': 792, 'n_tokens_neox': 759, 'n_words': 234}