Table of Contents

Class EnvSpecRegistry

Namespace
Gym.Envs.Registration
Assembly
Gym.dll

The registry's dict, env id to EnvSpec (Python's gymnasium.registry, a plain dict): iteration follows registration order, which pprint_registry and the "Did you mean" suggestions read, and re-registering an id replaces its spec in place. Tests and plugins edit it directly (del registry[id] is Remove(string)).

public sealed class EnvSpecRegistry : IDictionary<string, EnvSpec>, ICollection<KeyValuePair<string, EnvSpec>>, IReadOnlyDictionary<string, EnvSpec>, IReadOnlyCollection<KeyValuePair<string, EnvSpec>>, IEnumerable<KeyValuePair<string, EnvSpec>>, IEnumerable
Inheritance
EnvSpecRegistry
Implements
Inherited Members
Extension Methods

Remarks

A missing id raises KeyError with Python's message ('x'), not KeyNotFoundException; Get(string) and Pop(string, EnvSpec?) are the defaulting forms, as on a Python dict.

Unlike a Python dict it is synchronized: env assemblies register from module initializers, which may run on any thread while another thread makes envs. Enumerating walks a snapshot, so editing the registry during a loop over it is safe (Python raises RuntimeError: dictionary changed size during iteration instead).

Constructors

EnvSpecRegistry()

An empty registry, Python's {}.

public EnvSpecRegistry()

EnvSpecRegistry(IEnumerable<KeyValuePair<string, EnvSpec>>)

A registry holding entries in their order (a repeated id keeps its first position and last spec).

public EnvSpecRegistry(IEnumerable<KeyValuePair<string, EnvSpec>> entries)

Parameters

entries IEnumerable<KeyValuePair<string, EnvSpec>>

The entries, for example another registry.

Exceptions

ArgumentNullException

Thrown when entries, an id or a spec is null.

Properties

Count

The number of registered ids.

public int Count { get; }

Property Value

int

IsReadOnly

Always false: the registry can be edited, like a Python dict.

public bool IsReadOnly { get; }

Property Value

bool

this[string]

Python's registry[id]: reading a missing id raises KeyError; assigning adds the id at the end, or replaces the spec of a registered id in place. Assigning bypasses register's checks, as in Python.

public EnvSpec this[string key] { get; set; }

Parameters

key string

The env id.

Property Value

EnvSpec

The spec.

Exceptions

ArgumentNullException

Thrown when key or the assigned spec is null.

KeyError

Thrown when reading an id that is not registered; the message is the id's repr, 'x'.

Keys

The ids in registration order (a snapshot).

public ICollection<string> Keys { get; }

Property Value

ICollection<string>

Values

The specs in registration order (a snapshot).

public ICollection<EnvSpec> Values { get; }

Property Value

ICollection<EnvSpec>

Methods

Add(string, EnvSpec)

Adds a new id, as a collection initializer does; an id already registered is an error, as for every .NET dictionary.

public void Add(string key, EnvSpec value)

Parameters

key string

The env id.

value EnvSpec

The spec.

Exceptions

ArgumentNullException

Thrown when key or value is null.

ArgumentException

Thrown when key is already registered.

Clear()

Unregisters every id.

public void Clear()

ContainsKey(string)

Python's id in registry.

public bool ContainsKey(string key)

Parameters

key string

The env id.

Returns

bool

true when it is registered.

Exceptions

ArgumentNullException

Thrown when key is null.

Copy()

Python's registry.copy(): a new registry with the same entries (the specs are shared, not copied).

public EnvSpecRegistry Copy()

Returns

EnvSpecRegistry

The copy.

Get(string)

Python's registry.get(id): the spec, or null when the id is not registered.

public EnvSpec? Get(string key)

Parameters

key string

The env id.

Returns

EnvSpec

The spec, or null.

Exceptions

ArgumentNullException

Thrown when key is null.

GetEnumerator()

Enumerates a snapshot of the entries in registration order.

public IEnumerator<KeyValuePair<string, EnvSpec>> GetEnumerator()

Returns

IEnumerator<KeyValuePair<string, EnvSpec>>

The enumerator.

Pop(string)

Python's registry.pop(id): unregisters an id and returns its spec.

public EnvSpec Pop(string key)

Parameters

key string

The env id.

Returns

EnvSpec

The spec it had.

Exceptions

ArgumentNullException

Thrown when key is null.

KeyError

Thrown when the id is not registered.

Pop(string, EnvSpec?)

Python's registry.pop(id, default): unregisters an id if it is registered.

public EnvSpec? Pop(string key, EnvSpec? @default)

Parameters

key string

The env id.

default EnvSpec

What to return when it is not registered.

Returns

EnvSpec

The spec it had, or default.

Exceptions

ArgumentNullException

Thrown when key is null.

Remove(string)

Python's del registry[id] without its error: unregisters an id.

public bool Remove(string key)

Parameters

key string

The env id.

Returns

bool

true when it was registered.

Exceptions

ArgumentNullException

Thrown when key is null.

ToString()

Python's repr(registry): {'CartPole-v1': EnvSpec(...), ...}.

public override string ToString()

Returns

string

The text.

TryGetValue(string, out EnvSpec)

Reads an id's spec without raising when it is missing.

public bool TryGetValue(string key, out EnvSpec value)

Parameters

key string

The env id.

value EnvSpec

The spec, or null when the id is not registered.

Returns

bool

true when the id is registered.

Exceptions

ArgumentNullException

Thrown when key is null.