Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions ipython_genutils/ipstruct.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@

__all__ = ['Struct']

import collections

class Struct(dict):
# DictCls = dict # (ipython_genutils <= v0.1.0)
DictCls = collections.OrderedDict

class Struct(DictCls):
"""A dict subclass with attribute style access.

This dict subclass has a a few extra features:
Expand Down Expand Up @@ -46,7 +50,7 @@ def __init__(self, *args, **kw):
['a', 'b', 'c']
"""
object.__setattr__(self, '_allownew', True)
dict.__init__(self, *args, **kw)
DictCls.__init__(self, *args, **kw)

def __setitem__(self, key, value):
"""Set an item with check for allownew.
Expand All @@ -70,7 +74,7 @@ def __setitem__(self, key, value):
if not self._allownew and key not in self:
raise KeyError(
"can't create new attribute %s when allow_new_attr(False)" % key)
dict.__setitem__(self, key, value)
DictCls.__setitem__(self, key, value)

def __setattr__(self, key, value):
"""Set an attr with protection of class members.
Expand Down Expand Up @@ -108,7 +112,7 @@ def __setattr__(self, key, value):
raise AttributeError(e)

def __getattr__(self, key):
"""Get an attr by calling :meth:`dict.__getitem__`.
"""Get an attr by calling :meth:`DictCls.__getitem__`.

Like :meth:`__setattr__`, this method converts :exc:`KeyError` to
:exc:`AttributeError`.
Expand Down Expand Up @@ -227,7 +231,7 @@ def copy(self):
>>> type(s2) is Struct
True
"""
return Struct(dict.copy(self))
return Struct(DictCls.copy(self))

def hasattr(self, key):
"""hasattr function available as a method.
Expand Down Expand Up @@ -353,7 +357,7 @@ def merge(self, __loc_data__=None, __conflict_solve=None, **kw):
add_s = lambda old,new: old + ' ' + new

# default policy is to keep current keys when there's a conflict
conflict_solve = dict.fromkeys(self, preserve)
conflict_solve = DictCls.fromkeys(self, preserve)

# the conflict_solve dictionary is given by the user 'inverted': we
# need a name-function mapping, it comes as a function -> names
Expand Down