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.
- 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:
- fava.util.simple_wsgi(_, start_response)¶
Return an empty response (a simple WSGI app).
- 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.
- fava.util.date.END_OF_YEAR = FiscalYearEnd(month=12, day=31)¶
Default fiscal year (12-31).
- class fava.util.date.FiscalQuarter(fye)¶
A fiscal quarter interval, for a specific fiscal year end.
- fye: FiscalYearEnd¶
- class fava.util.date.FiscalYear(fye)¶
A fiscal year interval, for a specific fiscal year end.
- fye: FiscalYearEnd¶
- class fava.util.date.FiscalYearEnd(month, day)¶
Month and day that specify the end of the fiscal year.
- start_day¶
- start_month_of_year¶
- class fava.util.date.FiscalYearEnds¶
Some common fiscal year ends, used in tests.
- AU_NZ = FiscalYearEnd(month=6, day=30)¶
Fiscal year for Australia / New Zealand (06-30).
- JP = FiscalYearEnd(month=15, day=31)¶
Fiscal year for Japan (15-31).
- UK = FiscalYearEnd(month=4, day=5)¶
Fiscal year for the UK (04-05).
- US = FiscalYearEnd(month=9, day=30)¶
Fiscal year for the US (09-30).
- ZA = FiscalYearEnd(month=2, day=28)¶
Fiscal year for the personal tax year in South Africa (02-28).
- exception fava.util.date.FyeHasNoQuartersError¶
Only fiscal years that start on the first of a month have quarters.
- class fava.util.date.Interval¶
An interval.
- abstractmethod format_date(date)¶
Format a date for this interval for the Fava time filter.
- Return type:
- abstractmethod get_next(date)¶
Get the start date of the next interval following the date.
- Return type:
- abstractmethod get_prev(date)¶
Get the start date of the interval in which the date falls.
- Return type:
- exception fava.util.date.InvalidDateRangeError¶
End date needs to be after begin date.
- fava.util.date.dateranges(begin, end, interval, *, complete)¶
Get date ranges for the given begin and end date.
- Parameters:
- Yields:
Date ranges for all intervals between begin and end date.
- 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_interval(value, fye)¶
Get the interval for a string name.
- fava.util.date.interval_ends(begin, end, interval, *, complete)¶
Get interval ends.
- Yields:
The ends of the intervals.
- fava.util.date.month_offset(date, months)¶
Offsets a date by a given number of months.
- Return type:
fava.util.date_parser¶
Parsing of the date expressions that Fava’s time filter supports.
See parse_date() for the supported syntax.
- class fava.util.date_parser.FiscalYearPeriod(begin, fye)¶
A fiscal year, which can be refined to one of its quarters.
- interval: FiscalYear¶
- class fava.util.date_parser.MonthPeriod(year, month)¶
A month, which can be refined to one of its days.
- exception fava.util.date_parser.NoSuchPeriodError¶
A date expression is valid syntax but denotes no existing period.
This covers dates that do not exist, like the 30th of February or week 99, years outside the range that dates support, and fiscal quarters for a fiscal year end that does not have any.
- class fava.util.date_parser.Period(begin, interval)¶
A period given by its first day and the interval that it spans.
- fava.util.date_parser.Variable¶
The variables that denote a period around the current day.
alias of
Literal[‘fiscal_year’, ‘fiscal_quarter’, ‘year’, ‘quarter’, ‘month’, ‘week’, ‘day’]
- class fava.util.date_parser.YearPeriod(year)¶
A calendar year, which can be refined to a month, quarter or week.
- fava.util.date_parser.parse_date(string, fye=FiscalYearEnd(month=12, day=31))¶
Parse a date.
Example of supported formats:
2010-03-15, 2010-03, 2010
2010-W01, 2010-Q3
FY2012, FY2012-Q2
Instead of a year, month, etc., one of the variables ‘year’, ‘quarter’, ‘month’, ‘week’, ‘day’, ‘fiscal_year’ and ‘fiscal_quarter’ can be used to refer to the period around the current day. They support addition and subtraction of an offset of up to three digits, e.g. ‘month-2’ - four digits are a year and hence start a range. To subtract from the date instead of shifting the period, put the variable in parentheses - ‘month-10’ is ten months ago whereas ‘(month)-10’ is the tenth of the current month.
A range of dates can be expressed as ‘start - end’, where start and end look like one of the above examples.
- Parameters:
string (
str) – A date(range) in our custom format.fye (
FiscalYearEnd) – The fiscal year end to consider.
- Returns:
DateRange– The range of dates.- Raises:
ParseError – If parsing the string failed.
InvalidDateRangeError – For an invalid date range, e.g. ‘2012 - 2010’
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.parsing¶
Lexing and parsing helpers.
- class fava.util.parsing.KeywordTokenKind(keywords)¶
A token for one of the keywords of a
typing.Literal.
- class fava.util.parsing.Lexer(rules, /, *, error=<class 'fava.util.parsing.UnexpectedTokenError'>, flags=0, skip='\\\\s+')¶
Splits strings into tokens of the given kinds.
They are matched in the order in which they are given. Text matching the skip pattern is ignored and any other character that does not start a token is an error.
- Parameters:
rules (
Sequence[TokenKind[Any]]) – The kinds of token to split the string into.error (
Callable[[str],Exception]) – The exception to raise for a character that cannot start a token - it is passed that character.flags (
int) – re flags to apply.skip (
str) – A pattern for the text between tokens, which is ignored.
- class fava.util.parsing.LiteralTokenKind(char)¶
A token for a literal character or string.
- exception fava.util.parsing.ParseError¶
An expression could not be parsed.
- class fava.util.parsing.ParserBase(tokens)¶
A parser base class.
- class fava.util.parsing.Token(kind, text)¶
A token, of some kind and with the text that it matched.
- class fava.util.parsing.TokenKind(pattern, convert)¶
A token type: pattern and how to get the value of such a token.
- exception fava.util.parsing.UnexpectedEndError¶
An expression ended before it was complete.
- exception fava.util.parsing.UnexpectedTokenError(unexpected)¶
An expression contained something that does not belong there.
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_ (
Sequence[str] |None) – 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/ratewill 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/365so 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.