Package lastfm :: Module safelist

[hide private]
[frames] | no frames]

# Source Code for Module lastfm.safelist

     1  #!/usr/bin/env python
     2
     3  __author__ = "Abhinav Sarkar <abhinav@abhinavsarkar.net>"
     4  __version__ = "0.2"
     5  __license__ = "GNU Lesser General Public License"
     6
     7  import sys
     8 -class SafeList(object):
     9 -    def __init__(self, lst, add_func, remove_func):
    10          self._list = lst
    11          self._add_func = add_func
    12          self._remove_func = remove_func
    13
    14 -    def add(self, lst):
    15          if not isinstance(lst, (list, tuple)):
    16              lst = [lst]
    17          self._add_func(lst)
    18
    19 -    def remove(self, lst):
    20          if not isinstance(lst, (list, tuple)):
    21              lst = [lst]
    22          for l in lst:
    23              self._remove_func(l)
    24
    25 -    def __iter__(self):
    26          for i in xrange(len(self._list)):
    27              yield self._list[i]
    28
    29 -    def _tuple_from_slice(self, i):
    30          """
    31          Get (start, end, step) tuple from slice object.
    32          """
    33          (start, end, step) = i.indices(len(self._list))
    34          # Replace (0, -1, 1) with (0, 0, 1) (misfeature in .indices()).
    35          if step == 1:
    36              if end < start:
    37                  end = start
    38              step = None
    39          if i.step == None:
    40              step = None
    41              return (start, end, step)
    42
    43
    44 -    def __getitem__(self, i):
    45          if isinstance(i, slice):
    46              (start, end, step) = self._tuple_from_slice(i)
    47              if step == None:
    48                  indices = xrange(start, end)
    49              else:
    50                  indices = xrange(start, end, step)
    51              return [self._list[i] for i in indices]
    52          else:
    53              return self._list[i]
    54
    55 -    def index(self, x, i=0, j=None):
    56          if i != 0 or j is not None:
    57              (i, j, ignore) = self._tuple_from_slice(slice(i, j))
    58          if j is None:
    59              j = len(self)
    60          for k in xrange(i, j):
    61              if self._list[k] == x:
    62                  return k
    63          raise ValueError('index(x): x not in list')
    64
    65      # Define sort() as appropriate for the Python version.
    66      if sys.version_info[:3] < (2, 4, 0):
    67 -        def sort(self, cmpfunc=None):
    68              ans = list(self._list)
    69              ans.sort(cmpfunc)
    70              self._list[:] = ans
    71      else:
    72 -        def sort(self, cmpfunc=None, key=None, reverse=False):
    73              ans = list(self._list)
    74              if reverse == True:
    75                  ans.sort(cmpfunc, key, reverse)
    76              elif key != None:
    77                  ans.sort(cmpfunc, key)
    78              else:
    79                  ans.sort(cmpfunc)
    80              self._list[:] = ans
    81
    82 -    def __len__(self):
    83          return len(self._list)
    84
    85 -    def __repr__(self):
    86          return repr(self._list)
    87