configdict.py 1.88 KB
Newer Older
Christopher Shallue's avatar
Christopher Shallue committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# Copyright 2018 The TensorFlow Authors.
#
# 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.

"""Configuration container for TensorFlow models.

A ConfigDict is simply a dict whose values can be accessed via both dot syntax
(config.key) and dict syntax (config['key']).
"""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function


def _maybe_convert_dict(value):
  if isinstance(value, dict):
    return ConfigDict(value)

  return value


class ConfigDict(dict):
  """Configuration container class."""

  def __init__(self, initial_dictionary=None):
    """Creates an instance of ConfigDict.

    Args:
      initial_dictionary: Optional dictionary or ConfigDict containing initial
        parameters.
    """
    if initial_dictionary:
44
      for field, value in initial_dictionary.items():
Christopher Shallue's avatar
Christopher Shallue committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
        initial_dictionary[field] = _maybe_convert_dict(value)
    super(ConfigDict, self).__init__(initial_dictionary)

  def __setattr__(self, attribute, value):
    self[attribute] = _maybe_convert_dict(value)

  def __getattr__(self, attribute):
    try:
      return self[attribute]
    except KeyError as e:
      raise AttributeError(e)

  def __delattr__(self, attribute):
    try:
      del self[attribute]
    except KeyError as e:
      raise AttributeError(e)

  def __setitem__(self, key, value):
    super(ConfigDict, self).__setitem__(key, _maybe_convert_dict(value))