leaf_focus/report/processing/normalise.py
def date(self, value: str) -> Optional[date]:
        """Parse a date from a string."""
        # if no value, then no date
        if not value:
            return None

        # apply corrections
        value = self.correction_first(value, category="date")
        value = self.correction_first(value, category="generic")
        value = self.collapse_spaces(value)
        value = value.strip()

        # for days 1 - 9, pad with 0
        if len(value) == 24 and value[8] == " ":
            value = value[:8] + "0" + value[9:]

        # if the date contains a separator, which implies multiple dates,
        # use the last date
        date_seps = [" and ", " to ", "and ", "to ", " and", " to"]
        for date_sep in date_seps:
            if date_sep in value:
                start_index = value.index(date_sep) + len(date_sep)
                value = value[start_index:]

        # too many spaces: remove all spaces
        if ":" not in value and value.count(" ") > 2:
            value = value.replace(" ", "")

        # if the value is no longer valid after these changes,
        # no date is available
        if not value:
            return None

        for known_text in self._known_text:
            if known_text.category != "date-formats":
                continue
            for pattern in known_text.names:
                try:
                    parsed = datetime.strptime(value, pattern)
                    result = parsed.date()
                    return result
                except ValueError:
                    continue

        return None
        # raise ValueError(f"Could not parse date '{value}'.")
Parse a date from a string.
Parse a date from a string.
Parse a date from a string .
def date(self, value: str) -> Optional[date]:
        if not value:
            return None
        value = self.correction_first(value, category="date")
        value = self.correction_first(value, category="generic")
        value = self.collapse_spaces(value)
        value = value.strip()
        if len(value) == 24 and value[8] == " ":
            value = value[:8] + "0" + value[9:]
        date_seps = [" and ", " to ", "and ", "to ", " and", " to"]
        for date_sep in date_seps:
            if date_sep in value:
                start_index = value.index(date_sep) + len(date_sep)
                value = value[start_index:]
        if ":" not in value and value.count(" ") > 2:
            value = value.replace(" ", "")
        if not value:
            return None
        for known_text in self._known_text:
            if known_text.category != "date-formats":
                continue
            for pattern in known_text.names:
                try:
                    parsed = datetime.strptime(value, pattern)
                    result = parsed.date()
                    return result
                except ValueError:
                    continue
        return None
def date ( self , value : str ) -> Optional [ date ] : if not value : return None value = self . correction_first ( value , category = "date" ) value = self . correction_first ( value , category = "generic" ) value = self . collapse_spaces ( value ) value = value . strip ( ) if len ( value ) == 24 and value [ 8 ] == " " : value = value [ : 8 ] + "0" + value [ 9 : ] date_seps = [ " and " , " to " , "and " , "to " , " and" , " to" ] for date_sep in date_seps : if date_sep in value : start_index = value . index ( date_sep ) + len ( date_sep ) value = value [ start_index : ] if ":" not in value and value . count ( " " ) > 2 : value = value . replace ( " " , "" ) if not value : return None for known_text in self . _known_text : if known_text . category != "date-formats" : continue for pattern in known_text . names : try : parsed = datetime . strptime ( value , pattern ) result = parsed . date ( ) return result except ValueError : continue return None
Parse a date from a string.
Parse a date from a string .
"""Parse a date from a string.""" # if no value, then no date # apply corrections # for days 1 - 9, pad with 0 # if the date contains a separator, which implies multiple dates, # use the last date # too many spaces: remove all spaces # if the value is no longer valid after these changes, # no date is available # raise ValueError(f"Could not parse date '{value}'.")
self
value
str