Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It sounds like you want to store a date interval. In Python this would (to my still-somewhat-noob understanding) most readily be implemented by storing two datetime.datetime objects, one specifying the start of the date range and the other specifying the end. In a manner similar to that used to specify list slices, the endpoint would not itself be included in the date range.</p> <p>For example, this code would implement a date range as a named tuple:</p> <pre><code>&gt;&gt;&gt; from datetime import datetime &gt;&gt;&gt; from collections import namedtuple &gt;&gt;&gt; DateRange = namedtuple('DateRange', 'start end') &gt;&gt;&gt; the_year_2010 = DateRange(datetime(2010, 1, 1), datetime(2011, 1, 1)) &gt;&gt;&gt; the_year_2010.start &lt;= datetime(2010, 4, 20) &lt; the_year_2010.end True &gt;&gt;&gt; the_year_2010.start &lt;= datetime(2009, 12, 31) &lt; the_year_2010.end False &gt;&gt;&gt; the_year_2010.start &lt;= datetime(2011, 1, 1) &lt; the_year_2010.end False </code></pre> <p>Or even add some magic:</p> <pre><code>&gt;&gt;&gt; DateRange.__contains__ = lambda self, x: self.start &lt;= x &lt; self.end &gt;&gt;&gt; datetime(2010, 4, 20) in the_year_2010 True &gt;&gt;&gt; datetime(2011, 4, 20) in the_year_2010 False </code></pre> <p>This is such a useful concept that I'm pretty sure that somebody has already made an implementation available. For example, a quick glance suggests that the <code>relativedate</code> class from the <a href="http://pypi.python.org/pypi/python-dateutil/1.5" rel="nofollow noreferrer">dateutil</a> package will do this, and more expressively, by allowing a 'years' keyword argument to be passed to the constructor.</p> <p>However, mapping such an object into database fields is somewhat more complicated, so you might be better off implementing it simply by just pulling both fields separately and then combining them. I guess this depends on the DB framework; I'm not very familiar with that aspect of Python yet.</p> <p>In any case, I think the key is to think of a "partial date" as a range rather than as a simple value.</p> <h3>edit</h3> <p>It's tempting, but I think inappropriate, to add more magic methods that will handle uses of the <code>&gt;</code> and <code>&lt;</code> operators. There's a bit of ambiguity there: does a date that's "greater than" a given range occur after the range's end, or after its beginning? It initially seems appropriate to use <code>&lt;=</code> to indicate that the date on the right-hand side of the equation is after the start of the range, and <code>&lt;</code> to indicate that it's after the end.</p> <p>However, this implies equality between the range and a date within the range, which is incorrect, since it implies that the month of May, 2010 is equal to the year 2010, because May the 4th, 2010 equates to the both of them. IE you would end up with falsisms like <code>2010-04-20 == 2010 == 2010-05-04</code> being true.</p> <p>So probably it would be better to implement a method like <code>isafterstart</code> to explicitly check if a date is after the beginning of the range. But again, somebody's probably already done it, so it's probably worth a look on <a href="http://pypi.python.org" rel="nofollow noreferrer">pypi</a> to see what's considered production-ready. This is indicated by the presence of "Development Status :: 5 - Production/Stable" in the "Categories" section of a given module's pypi page. Note that not all modules have been given a development status.</p> <p>Or you could just keep it simple, and using the basic namedtuple implementation, explicitly check</p> <pre><code>&gt;&gt;&gt; datetime(2012, 12, 21) &gt;= the_year_2010.start True </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload