* src/gen/lib/date.py: added comparison operator for match()

* src/Utils.py: uses new match comparison

2007-11-21  Douglas S. Blank  <dblank@cs.brynmawr.edu>


svn: r9382
This commit is contained in:
Doug Blank
2007-11-21 20:05:18 +00:00
parent d692868ce0
commit d6deb0c95b
3 changed files with 49 additions and 34 deletions

View File

@@ -1,3 +1,7 @@
2007-11-21 Douglas S. Blank <dblank@cs.brynmawr.edu>
* src/gen/lib/date.py: added comparison operator for match()
* src/Utils.py: uses new match comparison
2007-11-21 Douglas S. Blank <dblank@cs.brynmawr.edu> 2007-11-21 Douglas S. Blank <dblank@cs.brynmawr.edu>
* src/Utils.py: probably_alive now takes date rather than year * src/Utils.py: probably_alive now takes date rather than year
* src/gen/proxy/living.py: create date from year * src/gen/proxy/living.py: create date from year

View File

@@ -576,9 +576,6 @@ def probably_alive(person, db, current_date=None, limit=0):
current_date = gen.lib.Date() current_date = gen.lib.Date()
# yr, mon, day: # yr, mon, day:
current_date.set_yr_mon_day(*time.localtime(time.time())[0:3]) current_date.set_yr_mon_day(*time.localtime(time.time())[0:3])
if (current_date.get_quality() == gen.lib.Date.QUAL_ESTIMATED or
current_date.get_modifier() != gen.lib.Date.MOD_NONE):
raise AttributeError, "probably_alive cannot take esitmated or modified dates"
death_date = None death_date = None
# If the recorded death year is before current year then # If the recorded death year is before current year then
@@ -588,7 +585,7 @@ def probably_alive(person, db, current_date=None, limit=0):
death = db.get_event_from_handle(death_ref.ref) death = db.get_event_from_handle(death_ref.ref)
if death.get_date_object().get_start_date() != gen.lib.Date.EMPTY: if death.get_date_object().get_start_date() != gen.lib.Date.EMPTY:
death_date = death.get_date_object() death_date = death.get_date_object()
if death_date.copy_offset_ymd(year=limit) < current_date: if death_date.copy_offset_ymd(year=limit).match(current_date, "<<"):
return False return False
# Look for Cause Of Death, Burial or Cremation events. # Look for Cause Of Death, Burial or Cremation events.
@@ -601,8 +598,7 @@ def probably_alive(person, db, current_date=None, limit=0):
if not death_date: if not death_date:
death_date = ev.get_date_object() death_date = ev.get_date_object()
if ev.get_date_object().get_start_date() != gen.lib.Date.EMPTY: if ev.get_date_object().get_start_date() != gen.lib.Date.EMPTY:
if (ev.get_date_object().copy_offset_ymd(year=limit) < if ev.get_date_object().copy_offset_ymd(year=limit).match(current_date,"<<"):
current_date):
return False return False
# For any other event of this person, check whether it happened # For any other event of this person, check whether it happened
# too long ago. If so then the person is likely dead now. # too long ago. If so then the person is likely dead now.
@@ -626,7 +622,7 @@ def probably_alive(person, db, current_date=None, limit=0):
return True return True
if not birth_date and death_date: if not birth_date and death_date:
if death_date > current_date.copy_offset_ymd(year=_MAX_AGE_PROB_ALIVE): if death_date.match(current_date.copy_offset_ymd(year=_MAX_AGE_PROB_ALIVE), ">>"):
# person died more than MAX after current year # person died more than MAX after current year
return False return False
@@ -650,8 +646,8 @@ def probably_alive(person, db, current_date=None, limit=0):
# if sibling birth date too far away, then not alive: # if sibling birth date too far away, then not alive:
year = dobj.get_year() year = dobj.get_year()
if year != 0: if year != 0:
if not (current_date.copy_offset_ymd(-(_MAX_AGE_PROB_ALIVE + _MAX_SIB_AGE_DIFF)) < if not (current_date.copy_offset_ymd(-(_MAX_AGE_PROB_ALIVE + _MAX_SIB_AGE_DIFF)).match(dobj,"<<") and
dobj < current_date.copy_offset_ymd(_MAX_SIB_AGE_DIFF)): dobj.match(current_date.copy_offset_ymd(_MAX_SIB_AGE_DIFF),"<<")):
return False return False
child_death_ref = child.get_death_ref() child_death_ref = child.get_death_ref()
if child_death_ref: if child_death_ref:
@@ -661,8 +657,8 @@ def probably_alive(person, db, current_date=None, limit=0):
# if sibling death date too far away, then not alive: # if sibling death date too far away, then not alive:
year = dobj.get_year() year = dobj.get_year()
if year != 0: if year != 0:
if not (current_date.copy_offset_ymd(-(_MAX_AGE_PROB_ALIVE + _MAX_SIB_AGE_DIFF)) < if not (current_date.copy_offset_ymd(-(_MAX_AGE_PROB_ALIVE + _MAX_SIB_AGE_DIFF)).match(dobj,"<<") and
dobj < current_date.copy_offset_ymd(_MAX_AGE_PROB_ALIVE)): dobj.match(current_date.copy_offset_ymd(_MAX_AGE_PROB_ALIVE),"<<")):
return False return False
# Try looking for descendants that were born more than a lifespan # Try looking for descendants that were born more than a lifespan

View File

@@ -305,40 +305,55 @@ class Date:
# return tuples not lists, for comparisons # return tuples not lists, for comparisons
return (tuple(startmin), tuple(stopmax)) return (tuple(startmin), tuple(stopmax))
def match(self, other_date): def match(self, other_date, comparison="="):
""" """
The other comparisons for Date don't actually look for anything The other comparisons for Date don't actually look for anything
other than a straight match, or a simple comparison of the sortval. other than a straight match, or a simple comparison of the sortval.
This method allows a more sophisticated comparison looking for This method allows a more sophisticated comparison looking for
any overlap between two possible dates, date spans, and qualities. any match between two possible dates, date spans, and qualities.
Returns True if part of other_date matches part of the date-span comparison =,== :
defined by self Returns True if any part of other_date matches any part of self
comparison < :
Returns True if any part of other_date < any part of self
comparison << :
Returns True if all parts of other_date < all parts of self
comparison > :
Returns True if any part of other_date > any part of self
comparison >> :
Returns True if all parts of other_date > all parts of self
""" """
if (other_date.modifier == Date.MOD_TEXTONLY or if (other_date.modifier == Date.MOD_TEXTONLY or
self.modifier == Date.MOD_TEXTONLY): self.modifier == Date.MOD_TEXTONLY):
###from DateHandler import displayer if comparison in ["=", "=="]:
# If either date is just text, then we can only compare textual return (self.text.upper().find(other_date.text.upper()) != -1)
# representations else:
# Use text as originally given or display date to format return False
# in preferences? That is use self.text or displayer ?
# It is unclean to import DateHandler in gen.lib !
###self_text = displayer.display(self)
self_text = self.text
##DEBUG: print ' TEXT COMPARE ONLY '
return (self_text.upper().find(other_date.text.upper()) != -1)
# Obtain minimal start and maximal stop in Gregorian calendar # Obtain minimal start and maximal stop in Gregorian calendar
other_start, other_stop = other_date.get_start_stop_range() other_start, other_stop = other_date.get_start_stop_range()
self_start, self_stop = self.get_start_stop_range() self_start, self_stop = self.get_start_stop_range()
#DEBUG print "compare:",self_start,self_stop,other_start,other_stop if comparison in ["=", "=="]:
# If some overlap then match is True, otherwise False.
# If some overlap then match is True, otherwise False. return ((self_start <= other_start <= self_stop) or
return ((self_start <= other_start <= self_stop) or (self_start <= other_stop <= self_stop) or
(self_start <= other_stop <= self_stop) or (other_start <= self_start <= other_stop) or
(other_start <= self_start <= other_stop) or (other_start <= self_stop <= other_stop))
(other_start <= self_stop <= other_stop)) elif comparison == "<":
# If any < any
return self_start < other_stop
elif comparison == "<<":
# If all < all
return self_stop < other_start
elif comparison == ">":
# If any > any
return self_stop > other_start
elif comparison == ">>":
# If all > all
return self_start > other_stop
else:
raise AttributeError, ("invalid match comparison operator: '%s'" % comparison)
def __str__(self): def __str__(self):
""" """