Skip to content

Commit 7853c76

Browse files
committed
ENH : NumpyDocString subclass collections.Mapping
Sub-classing `collections.Mapping` and providing a __iter__ and __len__ methods (completely delegated to _parsed_data) makes NumpyDocString behave as a dictionary with fixed keys.
1 parent 51982b2 commit 7853c76

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

numpydoc/docscrape.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def is_empty(self):
8686
return not ''.join(self._str).strip()
8787

8888

89-
class NumpyDocString(object):
89+
class NumpyDocString(collections.Mapping):
9090
def __init__(self, docstring, config={}):
9191
docstring = textwrap.dedent(docstring).split('\n')
9292

@@ -112,15 +112,21 @@ def __init__(self, docstring, config={}):
112112

113113
self._parse()
114114

115-
def __getitem__(self,key):
115+
def __getitem__(self, key):
116116
return self._parsed_data[key]
117117

118-
def __setitem__(self,key,val):
118+
def __setitem__(self, key, val):
119119
if key not in self._parsed_data:
120120
warn("Unknown section %s" % key)
121121
else:
122122
self._parsed_data[key] = val
123123

124+
def __iter__(self):
125+
return iter(self._parsed_data)
126+
127+
def __len__(self):
128+
return len(self._parsed_data)
129+
124130
def _is_at_section(self):
125131
self._doc.seek_next_non_empty_line()
126132

0 commit comments

Comments
 (0)