diff options
author | hut <hut@lepus.uberspace.de> | 2015-01-17 02:51:35 +0100 |
---|---|---|
committer | hut <hut@lepus.uberspace.de> | 2015-01-17 02:51:35 +0100 |
commit | d87ab338a45e13128615b5365a50e4cf727ae348 (patch) | |
tree | 3f934d44997418b83f53abd954abf1dd88dc44c8 | |
parent | 4ee16ef9291005add76e3ff7ede61c9d7b52389c (diff) | |
download | ranger-d87ab338a45e13128615b5365a50e4cf727ae348.tar.gz |
ext.openstruct: added DefaultOpenStruct
-rw-r--r-- | ranger/ext/openstruct.py | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/ranger/ext/openstruct.py b/ranger/ext/openstruct.py index 5881fd9f..55dd0a40 100644 --- a/ranger/ext/openstruct.py +++ b/ranger/ext/openstruct.py @@ -1,6 +1,8 @@ # Copyright (C) 2009-2013 Roman Zimbelmann <hut@hut.pm> # This software is distributed under the terms of the GNU GPL version 3. +import collections + # prepend __ to arguments because one might use "args" # or "keywords" as a keyword argument. @@ -9,3 +11,16 @@ class OpenStruct(dict): def __init__(self, *__args, **__keywords): dict.__init__(self, *__args, **__keywords) self.__dict__ = self + + +class DefaultOpenStruct(collections.defaultdict): + """The fusion of dict and struct, with default values""" + def __init__(self, *__args, **__keywords): + collections.defaultdict.__init__(self, None, *__args, **__keywords) + self.__dict__ = self + + def __getattr__(self, name): + if name not in self.__dict__: + return None + else: + return self.__dict__[name] |