fava.util¶
Some small utility functions.
- fava.util.filter_api_changed(record)¶
Filter out LogRecords for requests that poll for changes.
- Return type:
- fava.util.get_translations(locale)¶
Check whether Fava has translations for the locale.
- Parameters:
locale (Locale) – The locale to search for
- Returns:
str | None – The path to the found translations or None if none matched.
- fava.util.listify(func)¶
Make generator function return a list (decorator).
- fava.util.next_key(basekey, keys)¶
Return the next unused key for basekey in the supplied dictionary.
The first try is basekey, followed by basekey-2, basekey-3, etc until a free one is found.
- Return type:
- fava.util.send_file_inline(filename)¶
Send a file inline, including the original filename.
Ref: http://test.greenbytes.de/tech/tc2231/.
- Return type:
Response
- fava.util.simple_wsgi(_, start_response)¶
Return an empty response (a simple WSGI app).
- Return type:
list[bytes]
- fava.util.slugify(string)¶
Slugify a string.
- fava.util.timefunc(func)¶
Time function for debugging (decorator).
fava.util.date¶
Date-related functionality.
Note
Date ranges are always tuples (start, end) from the (inclusive) start date to the (exclusive) end date.
- class fava.util.date.DateRange(begin, end)¶
A range of dates, usually matching an interval.
- class fava.util.date.FiscalYearEnd(month, day)¶
Month and day that specify the end of the fiscal year.
- exception fava.util.date.FyeHasNoQuartersError¶
Only fiscal year that start on the first of a month have quarters.
- class fava.util.date.Interval(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)¶
The possible intervals.
- DAY = 'day'¶
- MONTH = 'month'¶
- QUARTER = 'quarter'¶
- WEEK = 'week'¶
- YEAR = 'year'¶
- format_date_filter(date)¶
Format a date for this interval for the Fava time filter.
- Return type:
- fava.util.date.dateranges(begin, end, interval)¶
Get date ranges for the given begin and end date.
- Parameters:
- Yields:
Date ranges for all intervals of the given in the
- Return type:
- fava.util.date.days_in_daterange(start_date, end_date)¶
Yield a datetime for every day in the specified interval.
- fava.util.date.get_fiscal_period(year, fye, quarter=None)¶
Calculate fiscal periods.
Uses the fava option “fiscal-year-end” which should be in “%m-%d” format. Defaults to calendar year [12-31]
- fava.util.date.get_next_interval(date, interval)¶
Get the start date of the next interval.
- fava.util.date.get_prev_interval(date, interval)¶
Get the start date of the interval in which the date falls.
- fava.util.date.interval_ends(first, last, interval)¶
Get interval ends.
- fava.util.date.month_offset(date, months)¶
Offsets a date by a given number of months.
Maintains the day, unless that day is invalid when it will raise a ValueError
- Return type:
- fava.util.date.number_of_days_in_period(interval, date)¶
Get number of days in the surrounding interval.
- fava.util.date.parse_date(string, fye=None)¶
Parse a date.
Example of supported formats:
2010-03-15, 2010-03, 2010
2010-W01, 2010-Q3
FY2012, FY2012-Q2
Ranges of dates can be expressed in the following forms:
start - end
start to end
where start and end look like one of the above examples
- fava.util.date.parse_fye_string(fye)¶
Parse a string option for the fiscal year end.
- Parameters:
fye (
str
) – The end of the fiscal year to parse.- Return type:
- fava.util.date.substitute(string, fye=None)¶
Replace variables referring to the current day.
- Parameters:
string (
str
) – A string, possibly containing variables for today.fye (
FiscalYearEnd
|None
) – Use a specific fiscal-year-end
- Returns:
str
– A string, where variables referring to the current day, like ‘year’ or ‘week’ have been replaced by the corresponding string understood byparse_date()
. Can compute addition and subtraction.
fava.util.excel¶
Writing query results to CSV and spreadsheet documents.
- exception fava.util.excel.InvalidResultFormatError(result_format)¶
- fava.util.excel.to_csv(types, rows)¶
Save result to CSV.
- fava.util.excel.to_excel(types, rows, result_format, query_string)¶
Save result to spreadsheet document.
fava.util.ranking¶
Ranking utilities.
- class fava.util.ranking.ExponentialDecayRanker(list_=None, rate=0.0018990333713971104)¶
Rank a list by exponential decay.
Maintains scores for the items in a list. We can think of this as the sum of all ‘likes’, where the value of a ‘like’ starts at 1 and decays exponentially. So the current score would be given by (where t is the current time and l is the time of the ‘like’)
s = Σ exp(-RATE * (t - l))
As only the relative order on the items is relevant, we can multiply all scores by exp(RATE * t) and so we need to compute the following score:
s = Σ exp(RATE * l)
To avoid huge numbers, we actually compute and store the logarithm of that sum.
- Parameters:
list – If given, this list is ranked is by
.sort()
otherwise all items with at least one ‘like’ will be ranked.rate (
float
) – This sets the rate of decay.1/rate
will be the time (in days) that it takes for the value of a ‘like’ to decrease by1/e
. The default rate is set tomath.log(2) * 1/365
so that a ‘like’ from a year ago will count half as much as one from today.
- list¶
- rate¶
fava.util.sets¶
Utils for Python sets.
fava.util.unreachable¶
Type checking helper for unreachable code.
- exception fava.util.unreachable.UnreachableCodeAssertionError¶
Expected code to be unreachable.
- fava.util.unreachable.assert_never(_)¶
Assert that this code is unreachable.
- Return type:
Never