<commit_before># Copyright 2019 DeepMind Technologies Limited. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or  implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
"""An implementation of a dm_env environment using dm_env_rpc."""

import collections
from typing import Any, Iterable, Mapping, Optional, Sequence, Tuple
import dm_env
import frozendict

from dm_env_rpc.v1 import connection as dm_env_rpc_connection
from dm_env_rpc.v1 import dm_env_flatten_utils
from dm_env_rpc.v1 import dm_env_rpc_pb2
from dm_env_rpc.v1 import dm_env_utils
from dm_env_rpc.v1 import error
from dm_env_rpc.v1 import spec_manager
from dm_env_rpc.v1 import tensor_utils

# Default observation names for common RL concepts.  By default the dm_env
# wrapper will use these for reward and discount if available, but this behavior
# can be overridden.
DEFAULT_REWARD_KEY = 'reward'
DEFAULT_DISCOUNT_KEY = 'discount'

# Default key separator, used in flattening/unflattening nested structures.
DEFAULT_KEY_SEPARATOR = '.'


class DmEnvAdaptor(dm_env.Environment):
  """An implementation of dm_env using dm_env_rpc as the data protocol.

  Users can also optionally provide a mapping of objects to DmEnvAdaptor
  attributes. This is to accomodate user-created protocol extensions that
  compliment the core protocol.

  """

  def __init__(self,
               connection: dm_env_rpc_connection.Connection,
               specs: dm_env_rpc_pb2.ActionObservationSpecs,
               requested_observations: Optional[Sequence[str]] = None,
               nested_tensors: bool = True,
               extensions: Optional[Mapping[str,
                                            Any]] = frozendict.frozendict()):
    """Initializes the environment with the provided dm_env_rpc connection.

    Args:
      connection: An instance of Connection already connected to a dm_env_rpc
        server and after a successful JoinWorldRequest has been sent.
      specs: A dm_env_rpc ActionObservationSpecs message for the environment.
      requested_observations: List of observation names to be requested from the
        environment when step is called. If None is specified then all
        observations will be requested.
      nested_tensors: Boolean to determine whether to flatten/unflatten tensors.
      extensions: Optional mapping of extension instances to DmEnvAdaptor
        attributes. Raises ValueError if attribute already exists.
    """
    self._dm_env_rpc_specs = specs
    self._action_specs = spec_manager.SpecManager(specs.actions)<commit_msg>Disable pytype attribute checking for DmEnvAdaptor extension attributes.<commit_after># Copyright 2019 DeepMind Technologies Limited. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or  implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
"""An implementation of a dm_env environment using dm_env_rpc."""

import collections
from typing import Any, Iterable, Mapping, Optional, Sequence, Tuple
import dm_env
import frozendict

from dm_env_rpc.v1 import connection as dm_env_rpc_connection
from dm_env_rpc.v1 import dm_env_flatten_utils
from dm_env_rpc.v1 import dm_env_rpc_pb2
from dm_env_rpc.v1 import dm_env_utils
from dm_env_rpc.v1 import error
from dm_env_rpc.v1 import spec_manager
from dm_env_rpc.v1 import tensor_utils

# Default observation names for common RL concepts.  By default the dm_env
# wrapper will use these for reward and discount if available, but this behavior
# can be overridden.
DEFAULT_REWARD_KEY = 'reward'
DEFAULT_DISCOUNT_KEY = 'discount'

# Default key separator, used in flattening/unflattening nested structures.
DEFAULT_KEY_SEPARATOR = '.'


class DmEnvAdaptor(dm_env.Environment):
  """An implementation of dm_env using dm_env_rpc as the data protocol.

  Users can also optionally provide a mapping of objects to DmEnvAdaptor
  attributes. This is to accomodate user-created protocol extensions that
  compliment the core protocol.

  """

  # Disable pytype attribute checking for dynamically created extension attrs.
  _HAS_DYNAMIC_ATTRIBUTES = True

  def __init__(self,
               connection: dm_env_rpc_connection.Connection,
               specs: dm_env_rpc_pb2.ActionObservationSpecs,
               requested_observations: Optional[Sequence[str]] = None,
               nested_tensors: bool = True,
               extensions: Optional[Mapping[str,
                                            Any]] = frozendict.frozendict()):
    """Initializes the environment with the provided dm_env_rpc connection.

    Args:
      connection: An instance of Connection already connected to a dm_env_rpc
        server and after a successful JoinWorldRequest has been sent.
      specs: A dm_env_rpc ActionObservationSpecs message for the environment.
      requested_observations: List of observation names to be requested from the
        environment when step is called. If None is specified then all
        observations will be requested.
      nested_tensors: Boolean to determine whether to flatten/unflatten tensors.
      extensions: Optional mapping of extension instances to DmEnvAdaptor
        attributes. Raises ValueError if attribute already exists.
    """
    self._dm_env_rpc_specs = specs
    self._action_specs = spec_manager.SpecManager(specs.actions)
{'fraction_non_alphanumeric': 0.0736875395319418, 'fraction_numerical': 0.005376344086021506, 'mean_word_length': 3.956896551724138, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4416706', 'n_tokens_mistral': 1714, 'n_tokens_neox': 1626, 'n_words': 657}