Processing functions

Surveys often require some business logic to be applied.

In the general case, this means deriving an output value for a single question id, based on the supplied data for a group of question ids.

In the simplest case, the size of that group is 1, and contains only the question for which data is calculated.

The processor module collects business logic processing functions under a single namespace so they can be used in Transformers.

class sdx.common.processor.Processor[source]

Business logic operations on data.

These methods are used to perform business logic on survey data. They are mostly concerned with combining multiple fields into a single field for output.

Principles for processor methods:

  • The method is responsible for range check according to its own logic.
  • Parametrisation is possible; use functools.partial to bind arguments.
  • Return data of the same type as the supplied default.
  • On any error, return the default.
static aggregate(qid, data, default, *args, weights=[], **kwargs)[source]

Calculate the weighted sum of a question group.

Parameters:
  • qid (str) – The question id.
  • data (dict(str, str)) – The full survey data.
  • default – The default value for the question.
  • weights ([(str, number)]) – A sequence of 2-tuples giving the weight value for each question in the group.
static evaluate(qid, data, default, *args, group=[], convert=<class 'bool'>, op=<built-in function or_>, **kwargs)[source]

Perform a map/reduce evaluation of a question group.

Parameters:
  • qid (str) – The question id.
  • data (dict(str, str)) – The full survey data.
  • default – The default value for the question.
  • group – A sequence of question ids.
  • convert – A type or function to convert the group values.
  • op – A binary operator or function to reduce data to a single value.
static mean(qid, data, default, *args, group=[], **kwargs)[source]

Calculate the mean of all fields in a question group.

Parameters:
  • qid (str) – The question id.
  • data (dict(str, str)) – The full survey data.
  • default – The default value for the question.
  • group – A sequence of question ids.
static events(qid, data, default, *args, group=[], **kwargs)[source]

Return a sequence of time events from a question group.

Parameters:
  • qid (str) – The question id.
  • data (dict(str, str)) – The full survey data.
  • default – The default value for the question.
  • group – A sequence of question ids.
static survey_string(qid, data, default, *args, survey=None, **kwargs)[source]

Accept a string as an option for a question.

This method provides an opportunity for validating the string against the survey definition, though this has not been a requirement so far.

Parameters:
  • qid (str) – The question id.
  • data (dict(str, str)) – The full survey data.
  • default – The default value for the question.
  • survey (dict) – The survey definition.
static unsigned_integer(qid, data, default, *args, **kwargs)[source]

Process a string as an unsigned integer.

Parameters:
  • qid (str) – The question id.
  • data (dict(str, str)) – The full survey data.
  • default – The default value for the question.
static percentage(qid, data, default, *args, **kwargs)[source]

Process a string as a number, checking that it is valid as a percentage.

Parameters:
  • qid (str) – The question id.
  • data (dict(str, str)) – The full survey data.
  • default – The default value for the question.