Package libxyz :: Package core :: Module odict

[hide private]
[frames] | no frames]

# Source Code for Module libxyz.core.odict

      1  #-*- coding: utf8 -*
      2  #
      3  # Max E. Kuznecov ~syhpoon <syhpoon@syhpoon.name> 2008
      4  #
      5  # This file is part of XYZCommander.
      6  # XYZCommander is free software: you can redistribute it and/or modify
      7  # it under the terms of the GNU Lesser Public License as published by
      8  # the Free Software Foundation, either version 3 of the License, or
      9  # (at your option) any later version.
     10  # XYZCommander is distributed in the hope that it will be useful,
     11  # but WITHOUT ANY WARRANTY; without even the implied warranty of
     12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     13  # GNU Lesser Public License for more details.
     14  # You should have received a copy of the GNU Lesser Public License
     15  # along with XYZCommander. If not, see <http://www.gnu.org/licenses/>.
     16
     17  from UserDict import DictMixin
     18
     19 -class ODict(DictMixin):
     20      """
     21      Ordered dictionary
     22      Provides dictionary-like access to parsed values
     23      Input order is kept
     24      """
     25
     26 -    def __init__(self, name=None):
     27          self.name = name
     28          self._keys = []
     29          self._values = []
     30
     31      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     32
     33 -    def next(self):
     34          return self
     35
     36      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     37
     38 -    def iteritems(self):
     39          return ((_k, _v) for _k, _v in zip(self._keys, self._values))
     40
     41      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     42
     43 -    def keys(self):
     44          return self._keys
     45
     46      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     47
     48 -    def values(self):
     49          return self._values
     50
     51      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     52
     53 -    def __str__(self):
     54          return u"<ODict object: %s>" % unicode([(k, v)
     55                                                  for k, v in self.iteritems()])
     56
     57      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     58
     59 -    def __repr__(self):
     60          return self.__str__()
     61
     62      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     63
     64 -    def __getitem__(self, var):
     65          return self.lookup(var)
     66
     67      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     68
     69 -    def __setitem__(self, var, val):
     70          self.set(var, val)
     71
     72      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     73
     74 -    def __delitem__(self, var):
     75          try:
     76              _at = self._keys.index(var)
     77              del(self._keys[_at])
     78              del(self._values[_at])
     79          except ValueError:
     80              raise KeyError()
     81
     82      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     83
     84 -    def __contains__(self, key):
     85          return key in self._keys
     86
     87      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     88
     89 -    def __iter__(self):
     90          return (_k for _k in self._keys)
     91
     92      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     93
     94 -    def get(self, var, default):
     95          """
     96          Lookup for var or return default
     97          """
     98
     99          try:
    100              self.lookup(var)
    101          except KeyError:
    102              return default
    103
    104      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    105
    106 -    def index(self, index):
    107          """
    108          Lookup variable by index
    109          """
    110
    111          if index >= len(self._values):
    112              raise AttributeError()
    113          else:
    114              return self._values[index]
    115
    116      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    117
    118 -    def lookup(self, var):
    119          """
    120          Lookup for value of variable
    121          If variable does not exist raise KeyError
    122          """
    123
    124          for _k, _v in zip(self._keys, self._values):
    125              if var == _k:
    126                  return _v
    127          else:
    128              raise KeyError()
    129
    130      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    131
    132 -    def set(self, var, val):
    133          """
    134          Set new value to variable
    135          """
    136
    137          # Replace existing key
    138          try:
    139              _at = self._keys.index(var)
    140              self._keys[_at] = var
    141              self._values[_at] = val
    142          except ValueError:
    143              self._keys.append(var)
    144              self._values.append(val)
    145