Number formatting, numeric helpers, and numeric statistics.


webhelpers.number.percent_of(part, whole)

What percent of whole is part?

>>> percent_of(5, 100)
>>> percent_of(13, 26)



Return the mean (i.e., average) of a sequence of numbers.

>>> mean([5, 10])

Another name for mean(r).


Return the median of an iterable of numbers.

The median is the point at which half the numbers are lower than it and half the numbers are higher. This gives a better sense of the majority level than the mean (average) does, because the mean can be skewed by a few extreme numbers at either end. For instance, say you want to calculate the typical household income in a community and you’ve sampled four households:

>>> incomes = [18000]       # Fast food crew
>>> incomes.append(24000)   # Janitor
>>> incomes.append(32000)   # Journeyman
>>> incomes.append(44000)   # Experienced journeyman
>>> incomes.append(67000)   # Manager
>>> incomes.append(9999999) # Bill Gates
>>> median(incomes)
>>> mean(incomes)

The median here is somewhat close to the majority of incomes, while the mean is far from anybody’s income.

This implementation makes a temporary list of all numbers in memory.

webhelpers.number.standard_deviation(r, sample=True)

Standard deviation.

From the Python Cookbook. Population mode contributed by Lorenzo Catucci.

Standard deviation shows the variability within a sequence of numbers. A small standard deviation means the numbers are close to each other. A large standard deviation shows they are widely different. In fact it shows how far the numbers tend to deviate from the average. This can be used to detect whether the average has been skewed by a few extremely high or extremely low values.

Most natural and random phenomena follow the normal distribution (aka the bell curve), which says that most values are close to average but a few are extreme. E.g., most people are close to 5‘9” tall but a few are very tall or very short. If the data does follow the bell curve, 68% of the values will be within 1 standard deviation (stdev) of the average, and 95% will be within 2 standard deviations. So a university professor grading exams on a curve might give a “C” (mediocre) grade to students within 1 stdev of the average score, “B” (better than average) to those within 2 stdevs above, and “A” (perfect) to the 0.25% higher than 2 stdevs. Those between 1 and 2 stdevs below get a “D” (poor), and those below 2 stdevs... we won’t talk about them.

By default the helper computes the unbiased estimate for the population standard deviation, by applying an unbiasing factor of sqrt(N/(N-1)).

If you’d rather have the function compute the population standard deviation, pass sample=False.

The following examples are taken from Wikipedia. http://en.wikipedia.org/wiki/Standard_deviation

>>> standard_deviation([0, 0, 14, 14]) 
>>> standard_deviation([0, 6, 8, 14]) 
>>> standard_deviation([6, 6, 8, 8])
>>> standard_deviation([0, 0, 14, 14], sample=False)
>>> standard_deviation([0, 6, 8, 14], sample=False)
>>> standard_deviation([6, 6, 8, 8], sample=False)

(The results reported in Wikipedia are those expected for whole population statistics and therefore are equal to the ones we get by setting sample=False in the later tests.)

# Fictitious average monthly temperatures in Southern California.
#                       Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
>>> standard_deviation([70, 70, 70, 75, 80, 85, 90, 95, 90, 80, 75, 70]) 
>>> standard_deviation([70, 70, 70, 75, 80, 85, 90, 95, 90, 80, 75, 70], sample=False) 

# Fictitious average monthly temperatures in Montana.
#                       Jan  Feb  Mar Apr May Jun Jul  Aug Sep Oct Nov Dec
>>> standard_deviation([-32, -10, 20, 30, 60, 90, 100, 80, 60, 30, 10, -32]) 
>>> standard_deviation([-32, -10, 20, 30, 60, 90, 100, 80, 60, 30, 10, -32], sample=False) 
webhelpers.number.format_number(n, thousands=', ', decimal='.')

Format a number with a thousands separator and decimal delimiter.

n may be an int, long, float, or numeric string. thousands is a separator to put after each thousand. decimal is the delimiter to put before the fractional portion if any.

The default style has a thousands comma and decimal point per American usage:

>>> format_number(1234567.89)
>>> format_number(123456)
>>> format_number(-123)

Various European and international styles are also possible:

>>> format_number(1234567.89, " ")
'1 234 567.89'
>>> format_number(1234567.89, " ", ",")
'1 234 567,89'
>>> format_number(1234567.89, ".", ",")
class webhelpers.number.SimpleStats(numeric=False)

Calculate a few simple statistics on data.

This class calculates the minimum, maximum, and count of all the values given to it. The values are not saved in the object. Usage:

>>> stats = SimpleStats()
>>> stats(2)               # Add one data value.
>>> stats.extend([6, 4])   # Add several data values at once.  

The statistics are available as instance attributes:

>>> stats.count
>>> stats.min
>>> stats.max

Non-numeric data is also allowed:

>>> stats2 = SimpleStats()
>>> stats2("foo")
>>> stats2("bar")
>>> stats2.count
>>> stats2.min
>>> stats2.max

.min and .max are None until the first data value is registered.

Subclasses can override ._init_stats and ._update_stats to add additional statistics.

The constructor accepts one optional argument, numeric. If true, the instance accepts only values that are int, long, or float. The default is false, which accepts any value. This is meant for instances or subclasses that don’t want non-numeric values.


Add a data value.


Add several data values at once, akin to list.extend.

class webhelpers.number.Stats

A container for data and statistics.

This class extends SimpleStats by calculating additional statistics, and by storing all data seen. All values must be numeric (int, long, and/or float), and you must call .finish() to generate the additional statistics. That’s because the statistics here cannot be calculated incrementally, but only after all data is known.

>>> stats = Stats()
>>> stats.extend([5, 10, 10])
>>> stats.count
>>> stats.finish()
>>> stats.mean 
>>> stats.median
>>> stats.standard_deviation

All data is stored in a list and a set for later use:

>>> stats.list
[5, 10, 10]

>>  stats.set
set([5, 10])

(The double prompt “>>” is used to hide the example from doctest.)

The stat attributes are None until you call .finish(). It’s permissible – though not recommended – to add data after calling .finish() and then call .finish() again. This recalculates the stats over the entire data set.

In addition to the hook methods provided by SimpleStats, subclasses can override ._finish-stats to provide additional statistics.


Add a data value.


Add several data values at once, akin to list.extend.


Finish calculations. (Call after adding all data values.)

Call this after adding all data values, or the results will be incomplete.

Number formatting

webhelpers.number.format_data_size(size, unit, precision=1, binary=False, full_name=False)

Format a number using SI units (kilo, mega, etc.).

size: The number as a float or int.

unit: The unit name in plural form. Examples: “bytes”, “B”.

precision: How many digits to the right of the decimal point. Default is 1. 0 suppresses the decimal point.

binary: If false, use base-10 decimal prefixes (kilo = K = 1000). If true, use base-2 binary prefixes (kibi = Ki = 1024).

full_name: If false (default), use the prefix abbreviation (“k” or “Ki”). If true, use the full prefix (“kilo” or “kibi”). If false, use abbreviation (“k” or “Ki”).


>>> format_data_size(1024, "B")
'1.0 kB'
>>> format_data_size(1024, "B", 2)
'1.02 kB'
>>> format_data_size(1024, "B", 2, binary=True)
'1.00 KiB'
>>> format_data_size(54000, "Wh", 0)
'54 kWh'
>>> format_data_size(85000, "m/h", 0)
'85 km/h'
>>> format_data_size(85000, "m/h", 0).replace("km/h", "klicks")
'85 klicks'
webhelpers.number.format_byte_size(size, precision=1, binary=False, full_name=False)

Same as format_data_size but specifically for bytes.


>>> format_byte_size(2048)
'2.0 kB'
>>> format_byte_size(2048, full_name=True)
'2.0 kilobytes'
webhelpers.number.format_bit_size(size, precision=1, binary=False, full_name=False)

Same as format_data_size but specifically for bits.


>>> format_bit_size(2048)
'2.0 kb'
>>> format_bit_size(2048, full_name=True)
'2.0 kilobits'