Anonymous avatar Anonymous committed d01a200

[pdw/pd][m]: refactor all pd code to get rid of Parcel and use CaclResult and a simple work argument to get_work_status instead.

Comments (0)

Files changed (4)

pdw/pd/__init__.py

 
 import swiss
 
+logger = logging.getLogger('pdw.pd')
+
+
 OLDEST_PERSON = 100
 
-logger = logging.getLogger('pdw.pd')
 
 def float_date(year, month=0, day=0):
     return swiss.date.FlexiDate(year, month, day).as_float()
     else:
         logger.error('Jurisdiction "%s" not currently supported.' % jurisdiction)
         assert 0
+    return pd_calculator().get_work_status(work)
 
-    parcel = CalcParcel(work, jurisdiction)
-
-    return pd_calculator().get_work_status(parcel)
-
-
-class CalcParcel(object):
+class CalcResult(object):
+    '''A CalcResult object is returned by the calculator
     '''
-    A CalcParcel object is passed into the calculator
-    with the calculation request and stores calculation
-    results, to be returned to the caller.
-    '''
-    def __init__(self, work, jurisdiction, when=None):
-        # about the calc request
-        self.work = work
-        self.jurisdiction = jurisdiction
-        if when:
-            self.when = when
-        else:
-            self.when = float_date(datetime.date.today().year)
-
-        # results
+    def __init__(self):
         self.calc_finished = False
         self.date_pd = None
         self.pd_prob = 0.0 # P(is PD)
             self._now = float_date(datetime.date.today().year)
     
     def get_work_status(self, work):
-        raise NotImplementedError()
+        self.calc_result = CalcResult()
+        self.work = work
 
     def get_author_list(self, parcel):
         if self.author_list == None:
             self.author_list = []
-            for person in parcel.work.persons:
+            for person in self.work.persons:
                 self.author_list.append(person.name)
         return self.author_list
 
-    def calc_anon(self, parcel):
-        parcel.is_anon = False
-        for person in parcel.work.persons:
+    def calc_anon(self):
+        self.calc_result.is_anon = False
+        for person in self.work.persons:
             if person.name.lower() in ('anon', 'anon.', 'anonymous') :
-                parcel.is_anon = True
+                self.calc_result.is_anon = True
 
-    def calc_death_dates(self, parcel):
+    def calc_death_dates(self):
         death_dates = [] # list of: (name, float date)
         names = []
         most_recent_death_date = 0.0
-        for person in parcel.work.persons:
+        for person in self.work.persons:
             death_date = person.death_date_ordered
             # if we have no deathdate but do have a birthdate, assume
             # death OLDEST_PERSON years after birth
             if not death_date and person.birth_date_ordered:
                 death_date = person.birth_date_ordered + OLDEST_PERSON
-                parcel.log.append('Author "%s" death date not given - assuming died %s years after birth (%s + %s = %s)' % (person.name, OLDEST_PERSON, person.birth_date_ordered, OLDEST_PERSON, death_date))
+                self.calc_result.log.append('Author "%s" death date not given - assuming died %s years after birth (%s + %s = %s)' % (person.name, OLDEST_PERSON, person.birth_date_ordered, OLDEST_PERSON, death_date))
             death_dates.append((person.name, death_date))
             if death_date and death_date > most_recent_death_date:
                 most_recent_death_date = death_date
             if not death_date:
                 # no birthday
                 # alive: big assumption!
-                parcel.log.append('Author "%s" death date not given - assuming alive (!)' % name)
+                self.calc_result.log.append('Author "%s" death date not given - assuming alive (!)' % name)
                 an_author_lives = True
 
-        parcel.death_dates = death_dates
-        parcel.most_recent_death_date = most_recent_death_date
-        parcel.an_author_lives = an_author_lives
+        self.calc_result.death_dates = death_dates
+        self.calc_result.most_recent_death_date = most_recent_death_date
+        self.calc_result.an_author_lives = an_author_lives
 
 from uk import *
 from us import *
     def __init__(self, when=None):
         CalculatorBase.__init__(self, when)
 
-    def get_work_status(self, parcel):
-        work = parcel.work
+    def get_work_status(self, work):
+        super(CalculatorCanada, self).get_work_status(work)
         
         # Call this when we know when pd expires
-        def pd_expires(expiry_date, parcel):
+        def pd_expires(expiry_date):
             if expiry_date > self._now:
-                parcel.pd_prob = 0.0
+                self.calc_result.pd_prob = 0.0
             else:
-                parcel.pd_prob = 1.0
-            return parcel        
+                self.calc_result.pd_prob = 1.0
+            return self.calc_result
         
         # Is it a photograph?
         if work.type == 'photograph':
             assert NotImplementedError
         # NO
         if not work.type:
-            parcel.log.append('Work type not given - assuming it is not a photograph')
-        parcel.log.append('Work is not a photograph')
+            self.calc_result.log.append('Work type not given - assuming it is not a photograph')
+        self.calc_result.log.append('Work is not a photograph')
 
         # Is it crown author?
         # NO
-        parcel.log.append('Assuming not a crown author')
+        self.calc_result.log.append('Assuming not a crown author')
 
         # Is it an anonymous author?
-        self.calc_anon(parcel)
-        if parcel.is_anon:
+        self.calc_anon()
+        if self.calc_result.is_anon:
             # YES
             assert NotImplementedError
-        parcel.log.append('Author known')
+        self.calc_result.log.append('Author known')
         # NO
 
         # Are any authors living?
-        self.calc_death_dates(parcel)
-        if parcel.an_author_lives:
+        self.calc_death_dates()
+        if self.calc_result.an_author_lives:
             # YES
-            parcel.log.append('Author alive %s' % repr(parcel.death_dates))
-            parcel.pd_prob = 0.0
-            parcel.uncertainty = 0.0
-            return parcel
+            self.calc_result.log.append('Author alive %s' % repr(self.calc_result.death_dates))
+            self.calc_result.pd_prob = 0.0
+            self.calc_result.uncertainty = 0.0
+            return self.calc_result
         # NO
-        parcel.log.append('Author dead %s' % (repr(parcel.death_dates)))
+        self.calc_result.log.append('Author dead %s' % (repr(self.calc_result.death_dates)))
 
         # Is it a published work?
         if not work.items:
-            parcel.log.append('No item attached to work. Assuming this is a published work though.')
+            self.calc_result.log.append('No item attached to work. Assuming this is a published work though.')
         # YES
-        parcel.log.append('Published work')
+        self.calc_result.log.append('Published work')
 
         # Any authors living on date of publication?
-        if parcel.most_recent_death_date > work.date_ordered:
+        if self.calc_result.most_recent_death_date > work.date_ordered:
             # YES
-            parcel.log.append('Author living on date of publication (%s)' % work.date_ordered)
+            self.calc_result.log.append('Author living on date of publication (%s)' % work.date_ordered)
             # DEATH + 50
-            parcel.log.append('PD expires at "death + 50" (%s + 50 = %s)' % (parcel.most_recent_death_date, parcel.most_recent_death_date + 50))
-            return pd_expires(parcel.most_recent_death_date + 50, parcel)
+            self.calc_result.log.append('PD expires at "death + 50" (%s + 50 = %s)' % (self.calc_result.most_recent_death_date, self.calc_result.most_recent_death_date + 50))
+            return pd_expires(self.calc_result.most_recent_death_date + 50)
         #NO
-        parcel.log.append('Author dead on date of publication (%s)' % work.date_ordered)
+        self.calc_result.log.append('Author dead on date of publication (%s)' % work.date_ordered)
 
         # Published before Jan 1, 1999?
         if work.date_ordered < float_date(1999, 1, 1):
             # YES
-            parcel.log.append('Published before Jan 1, 1999 (%s)' % work.date_ordered)
+            self.calc_result.log.append('Published before Jan 1, 1999 (%s)' % work.date_ordered)
 
             # PUBLICATION + 50
-            parcel.log.append('PD expires at "publication + 50" (%s + 50 = %s)' % (work.date_ordered, work.date_ordered + 50))
-            return pd_expires(work.date_ordered + 50, parcel)
+            self.calc_result.log.append('PD expires at "publication + 50" (%s + 50 = %s)' % (work.date_ordered, work.date_ordered + 50))
+            return pd_expires(work.date_ordered + 50)
         # NO
-        parcel.log.append('Published after Jan 1, 1999 (%s)' % work.date_ordered)
+        self.calc_result.log.append('Published after Jan 1, 1999 (%s)' % work.date_ordered)
         # DEATH + 50
-        parcel.log.append('PD expires at "death + 50" (%s + 50 = %s)' % (parcel.most_recent_death_date, parcel.most_recent_death_date + 50))
-        return pd_expires(parcel.most_recent_death_date + 50, parcel)
+        self.calc_result.log.append('PD expires at "death + 50" (%s + 50 = %s)' % (self.calc_result.most_recent_death_date, self.calc_result.most_recent_death_date + 50))
+        return pd_expires(self.calc_result.most_recent_death_date + 50)
 
     def __init__(self, when=None):
         CalculatorBase.__init__(self, when)
 
-    def get_work_status(self, parcel):
-        work = parcel.work
+    def get_work_status(self, work):
+        super(CalculatorUk, self).get_work_status(work)
 
         # Call this when we know when pd expires
-        def pd_expires(expiry_date, parcel):
+        def pd_expires(expiry_date):
             if expiry_date > self._now:
-                parcel.pd_prob = 0.0
+                self.calc_result.pd_prob = 0.0
             else:
-                parcel.pd_prob = 1.0
-            parcel.uncertainty = 0.0
-            parcel.date_pd = expiry_date
-            parcel.calc_finished = True
-            return parcel
+                self.calc_result.pd_prob = 1.0
+            self.calc_result.uncertainty = 0.0
+            self.calc_result.date_pd = expiry_date
+            self.calc_result.calc_finished = True
+            return self.calc_result
         def dont_know():
-            parcel.uncertainty = 1.0
-            parcel.pd_prob = 0.0
-            parcel.calc_finished = True
-            return parcel
+            self.calc_result.uncertainty = 1.0
+            self.calc_result.pd_prob = 0.0
+            self.calc_result.calc_finished = True
+            return self.calc_result
         
         # Is it a sound recording?
         if work.type == 'recording':
             # YES
-            parcel.log.append('Sound recording')
+            self.calc_result.log.append('Sound recording')
             # PUB + 1st Jan + 50
             pub_date = work.date_ordered
-            parcel.log.append('Assuming the date given for the work is the date of first publication: %s' % pub_date)
+            self.calc_result.log.append('Assuming the date given for the work is the date of first publication: %s' % pub_date)
             if not pub_date:
                 # TODO
                 return dont_know()
             expiry = float(int(pub_date + 1 + 50))
-            parcel.log.append('PD expires at "first publication + 1st Jan + 50" (%s + 50 = %s)' % (pub_date, expiry))
-            return pd_expires(expiry, parcel)
+            self.calc_result.log.append('PD expires at "first publication + 1st Jan + 50" (%s + 50 = %s)' % (pub_date, expiry))
+            return pd_expires(expiry)
         elif not work.type:
             # assume no
-            parcel.log.append('No "type" stored - assuming literary work')
+            self.calc_result.log.append('No "type" stored - assuming literary work')
         else:
             # no
-            parcel.log.append('Literary / musical work (not sound recording) (%s)' % work.type)
+            self.calc_result.log.append('Literary / musical work (not sound recording) (%s)' % work.type)
         # NO
 
         # Is it anonymous?
-        self.calc_anon(parcel)
-        if parcel.is_anon:
+        self.calc_anon()
+        if self.calc_result.is_anon:
             # YES
-            parcel.log.append('Anonymous work - %s' % self.get_author_list(parcel))
+            self.calc_result.log.append('Anonymous work - %s' % self.get_author_list(self.calc_result))
             pub_date = work.date_ordered
             if not pub_date:
                 # TODO
             
             # PUB + 1st Jan + 70 years
             expiry = float(int(pub_date + 1 + 70))
-            parcel.log.append('PD expires at "first publication + 1st Jan + 70" (%s + 70 = %s)' % (pub_date, expiry))
-            return pd_expires(expiry, parcel)
+            self.calc_result.log.append('PD expires at "first publication + 1st Jan + 70" (%s + 70 = %s)' % (pub_date, expiry))
+            return pd_expires(expiry)
         # NO
         # DEATH + 1st Jan + 70 years
-        self.calc_death_dates(parcel)
-        if not parcel.most_recent_death_date:
+        self.calc_death_dates()
+        if not self.calc_result.most_recent_death_date:
             # TODO
             return dont_know()
-        expiry = float(int(parcel.most_recent_death_date + 1 + 70))
-        parcel.log.append('PD expires at "death + 1st Jan + 70" (%s + 70 = %s)' % (parcel.most_recent_death_date, expiry))
-        return pd_expires(expiry, parcel)
+        expiry = float(int(self.calc_result.most_recent_death_date + 1 + 70))
+        self.calc_result.log.append('PD expires at "death + 1st Jan + 70" (%s + 70 = %s)' % (self.calc_result.most_recent_death_date, expiry))
+        return pd_expires(expiry)
 
     def __init__(self, when=None):
         CalculatorBase.__init__(self, when)
 
-    def get_work_status(self, parcel):
-        work = parcel.work
+    def get_work_status(self, work):
+        super(CalculatorUnitedStates, self).get_work_status(work)
         
         # Call this when we know when pd expires
-        def pd_expires(expiry_date, parcel):
+        def pd_expires(expiry_date):
             if expiry_date > self._now:
-                parcel.pd_prob = 0.0
+                self.calc_result.pd_prob = 0.0
             else:
-                parcel.pd_prob = 1.0
-            return parcel        
+                self.calc_result.pd_prob = 1.0
+            return self.calc_result        
         
-        parcel.log.append("We're assuming it was published.")
-        parcel.log.append("We're assuming it was published in the US.")
-        parcel.log.append("We're assuming it was published with a valid copyright notice.")
+        self.calc_result.log.append("We're assuming it was published.")
+        self.calc_result.log.append("We're assuming it was published in the US.")
+        self.calc_result.log.append("We're assuming it was published with a valid copyright notice.")
 
         # Different rules depending on when it was published
-        pubyear = work.date_ordered
+        pubyear = self.work.date_ordered
         if pubyear < 1923:
             # PUBLICATION + 28+28 years
-            parcel.log.append('Published before 1923 (actually %s)' % pubyear)
+            self.calc_result.log.append('Published before 1923 (actually %s)' % pubyear)
             expiry = float(int(pubyear + 28 + 28))
-            parcel.log.append('PD expires at "publication + 28 + 28" (%s + 28 + 28 = %s)' % (pubyear, expiry))
-            return pd_expires(expiry, parcel)
+            self.calc_result.log.append('PD expires at "publication + 28 + 28" (%s + 28 + 28 = %s)' % (pubyear, expiry))
+            return pd_expires(expiry)
         elif pubyear < 1964:
             # PUBLICATION + 95 years
-            parcel.log.append('Published between 1923 and 1964 (actually %s)' % pubyear)
-            parcel.log.append('Assuming its copyright was renewed')
+            self.calc_result.log.append('Published between 1923 and 1964 (actually %s)' % pubyear)
+            self.calc_result.log.append('Assuming its copyright was renewed')
             expiry = float(int(pubyear + 95))
-            parcel.log.append('PD expires at "publication + 95" (%s + 95 = %s)' % (pubyear, expiry))
-            return pd_expires(expiry, parcel)
+            self.calc_result.log.append('PD expires at "publication + 95" (%s + 95 = %s)' % (pubyear, expiry))
+            return pd_expires(expiry)
         elif pubyear < 1978:
             # PUBLICATION + 95 years
-            parcel.log.append('Published between 1964 and 1978 (actually %s)' % pubyear)
+            self.calc_result.log.append('Published between 1964 and 1978 (actually %s)' % pubyear)
             expiry = float(int(pubyear + 95))
-            parcel.log.append('PD expires at "publication + 95" (%s + 95 = %s)' % (pubyear, expiry))
-            return pd_expires(expiry, parcel)
+            self.calc_result.log.append('PD expires at "publication + 95" (%s + 95 = %s)' % (pubyear, expiry))
+            return pd_expires(expiry)
             pdyear = pubyear + 95
 
         # DEATH + 70 years
-        parcel.log.append('Published after 1978 (actually %s)' % pubyear)
-        parcel.log.append('Assuming it was not published by a corporation or under a pseudonym.')
-        self.calc_death_dates(parcel)
-        expiry = float(int(parcel.most_recent_death_date + 70))
-        parcel.log.append('PD expires at "death + 70" (%s + 70 = %s)' % (parcel.most_recent_death_date, expiry))
-        return pd_expires(expiry, parcel)
+        self.calc_result.log.append('Published after 1978 (actually %s)' % pubyear)
+        self.calc_result.log.append('Assuming it was not published by a corporation or under a pseudonym.')
+        self.calc_death_dates()
+        expiry = float(int(self.calc_result.most_recent_death_date + 70))
+        self.calc_result.log.append('PD expires at "death + 70" (%s + 70 = %s)' % (self.calc_result.most_recent_death_date, expiry))
+        return pd_expires(expiry)
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.