def anon_term(term, func): """lookup or anonomize any term, cache and return cached values :param term: term, or string, or date or whatever to anonymize :param func: callable to produce appropriate random version of term Returns the anonymized version of the term. Multiple calls with the same term return the same anonymized result, regardless of func. """ # difficult to tell if object supports len try: termlen = len(term) except TypeError: termlen = 1 if term is None or termlen == 0: return term cached = lookup_term(term) if cached is not None: return cached else: value = func(term) store_term(term, value) return value
def calculate_delta(ballpark_seconds): ballpark_seconds = delta_ballpark.total_seconds() if abs(ballpark_seconds) < \ datetime.timedelta(days=(5 * 365)).total_seconds(): raise ValueError("insignificant ballpark, increase delta " "magnitude to ensure unpredictable results") fudge = .10 * ballpark_seconds delta = random.triangular(ballpark_seconds - fudge, ballpark_seconds + fudge) store_term(cached_key, delta) return delta