title: Creating a list using a date as index
body: <p>I have made a list that uses a list of dates as an index, like so:</p>
  
  <pre><code>&gt;&gt;&gt; import datedlist
  &gt;&gt;&gt; import datetime
  &gt;&gt;&gt; dates = [datetime.date(2012,1,x) for x in range(2,6)]
  &gt;&gt;&gt; values = range(4,8)
  &gt;&gt;&gt; dates
  [datetime.date(2012, 1, 2), datetime.date(2012, 1, 3), datetime.date(2012, 1, 4), datetime.date(2012, 1, 5)]
  &gt;&gt;&gt; dl = datedlist.DatedList(values, dates)
  &gt;&gt;&gt; dl
  [4, 5, 6, 7]
  &gt;&gt;&gt; dl[datetime.date(2012,1,3)]
  5
  </code></pre>
  
  <p>So far all is well, but I also want to be able to use slicing (not extended slicing), like so (the following does <strong>not</strong> work - it is the result I want):</p>
  
  <pre><code>&gt;&gt;&gt; datedlist[datetime.date(2012,1,3):datetime.date(2012,1,4)]
  [5, 6]
  </code></pre>
  
  <p>Here is my attempt at this (which obviously doesn't work):</p>
  
  <pre><code>class DatedList(list):
  
      def __init__(self, values, dates):
          self.dates = dates
          list.__init__(self, values)
  
      def __getitem__(self, date):
          if isinstance(date, slice):
              start = self.dates.index(slice[0])
              end = self.dates.index(slice[1])
              return [list.__getitem__(self, index) for index in range(start, end)]
          elif isinstance( date, datetime.date ) :
              index = self.dates.index(date)
              return list.__getitem__(self, index)
          elif isinstance(date, int):
              if date &lt; 0:
                  date += len(self)
              if date &gt;= len(self):
                  raise IndexError, "index out of range {}".format(date)
              return list.__getitem__(self, date)
          else:
              raise TypeError, "Invalid argument type."
  </code></pre>
  
  <p>The slice[0] and slice[1] are only there to explain my intention. The isinstance(date, int) is only there for debugging - will be removed for production code.</p>
  
  <p><strong>Here is the question</strong>: how can I implement slicing that uses datetime.date objects as indices?</p>
  
  <p>EDIT (after gnibblers 2nd comment):
  I did try getslice too (even though the docs say that getslice is obsolete).
  The class then looks like this (isinstance-slice bit commented out because of syntax):</p>
  
  <pre><code>class DatedList(list):
  
      def __init__(self, values, dates):
          self.dates = dates
          list.__init__(self, values)
  
      def __contains__(self, date):
          return date in self.dates
  
      def __getslice__(self, fromdate, todate):
          i_from = self.get_index(fromdate)
          i_to = self.get_index(todate)
          print i_from, i_to
          return [list.__getitem__(self, i) for i in range(i_from, i_to)]
  
      def __getitem__(self, date):
          if isinstance(date, slice):
              pass
  #            start = self.dates.index(slice[0])
  #            end = self.dates.index(slice[1])
  #            return [list.__getitem__(self, i) for i in range(start, end)]
          elif isinstance(date, datetime.date):
              index = self.get_index(date)
              return list.__getitem__(self, index)
          elif isinstance(date, int):
              if date &lt; 0:
                  date += len(self)
              if date &gt;= len(self):
                  raise IndexError, "index out of range {}".format(date)
              return list.__getitem__(self, date)
          else:
              raise TypeError, "Invalid argument type."
  
      def get_index(self, date):
          if date in self.dates:
              index = self.dates.index(date)
          elif date &lt; self.dates[0]:
              index = 0
          elif date &gt; self.dates[-1]:
              index = len(self.dates) - 1
          return index
  </code></pre>
  
  <p>The result is:</p>
  
  <pre><code>&gt;&gt;&gt; print dl[datetime.date(2012,1,3):datetime.date(2012,1,5)]
  &gt;&gt;&gt; None
  </code></pre>
  
  <p>Apparently getslice does not get used at all, because the print is not executed. It appears that getitem is executed when a slice is requested, but I don't seem to be able to use a datetime.date in a slice.
  /EDIT</p>
  
  <p>Note: apparently it is not a good idea to subclass list, but none of the alternatives I tried so far seemed to work better (or at all):</p>
  
  <ul>
  <li><p>Building a class from scratch: I could not get the [] notation to work:</p>
  
  <blockquote>
  <pre><code>dl = DatedList(values, dates)
  value = dl[some_date]       # I want this to work
  value = dl.value(same_date) # I don't want this
  </code></pre>
  </blockquote></li>
  <li><p>I have considered using a dict, but my list needs to be ordered and I need to use slicing as well.</p></li>
  <li><p>I also tried to subclass collections.Sequence, but that resulted in:</p>
  
  <blockquote>
    <p>TypeError: descriptor '<strong>init</strong>' requires a 'list' object but received a 'DatedList'</p>
  </blockquote></li>
  </ul>