fava.util

Some small utility functions.

fava.util.filter_api_changed(record)

Filter out LogRecords for requests that poll for changes.

fava.util.listify(func)

Decorator to make generator function return a list.

fava.util.pairwise(iterable)

s -> (s0,s1), (s1,s2), (s2, s3), …

fava.util.resource_path(relative_path)

Get absolute path to resource.

fava.util.send_file_inline(filename)

Send a file inline, including the original filename.

Ref: http://test.greenbytes.de/tech/tc2231/.

fava.util.setup_logging()

Setup logging for Fava.

fava.util.simple_wsgi(_, start_response)

A simple wsgi app that always returns an empty response.

fava.util.slugify(string)

Slugify a string.

Parameters

string – A string.

Returns

A ‘slug’ of the string suitable for URLs. Retains non-ascii characters.

fava.util.timefunc(func)

Decorator to time function for debugging.

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.Interval

The possible intervals.

DAY = 'day'
MONTH = 'month'
QUARTER = 'quarter'
WEEK = 'week'
YEAR = 'year'
get = <function Interval.get>
label

The label for the interval.

members = <function Interval.members>
fava.util.date.days_in_daterange(start_date, end_date)

Yield a datetime for every day in the specified interval.

Parameters
  • start_date – A start date.

  • end_date – An end date (exclusive).

Returns

An iterator yielding all days between start_date to end_date.

fava.util.date.get_fiscal_period(year, fye, quarter=None)

Calculates fiscal periods

Uses the fava option “fiscal-year-end” which should be in “%m-%d” format. Defaults to calendar year [12-31]

Parameters
  • year – An integer year

  • fye – End date for period in “%m-%d” format

  • quarter – one of [None, 1, 2, 3 or 4]

Returns

A tuple (start, end) of dates.

fava.util.date.get_next_interval(date, interval)

Get the start date of the next interval.

Parameters
  • date (date) – A date.

  • interval (Interval) – An interval.

Returns

The start date of the next interval after date.

fava.util.date.interval_ends(first, last, interval)

List intervals.

Parameters
  • first – A datetime.date.

  • last – A datetime.date.

  • interval (Interval) – An interval.

Yields

Dates corresponding to the starts/ends of intervals between first and last.

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

fava.util.date.number_of_days_in_period(interval, date)

Number of days in the surrounding interval.

Parameters
  • interval – An interval.

  • date – A date.

Returns

A number, the number of days surrounding the given date in the 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

Parameters

string – A date(range) in our custom format.

Returns

A tuple (start, end) of dates.

fava.util.date.substitute(string, fye=None)

Replace variables referring to the current day.

Parameters
  • string – A string, possibly containing variables for today.

  • fye – Use a specific fiscal-year-end

Returns

A string, where variables referring to the current day, like ‘year’ or ‘week’ have been replaced by the corresponding string understood by parse_date(). Can compute addition and subtraction.

fava.util.excel

Writing query results to CSV and spreadsheet documents.

fava.util.excel.to_csv(types, rows)

Save result to CSV.

Parameters
  • types – query result_types.

  • rows – query result_rows.

Returns

The (binary) file contents.

fava.util.excel.to_excel(types, rows, result_format, query_string)

Save result to spreadsheet document.

Parameters
  • types – query result_types.

  • rows – query result_rows.

  • result_format – One of ‘xls’, ‘xlsx’, or ‘ods’.

  • query_string – The query string (is written to the document).

Returns

The (binary) file contents.

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 – 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 by 1/e. The default rate is set to math.log(2) * 1/365 so that a ‘like’ from a year ago will count half as much as one from today.

get(item)

Get the current score for an item, or zero.

list
rate
scores
sort()

Return items sorted by rank.

update(item, date)

Add ‘like’ for item.

Parameters
  • item – An item in the list that is being ranked.

  • date – The date on which the item has been liked.