Built-in predicate checkers

These are the predicate checkers that are included with repoze.what:

The base Predicate class

Predicate is the parent class of every predicate checker and its API is described below:

class repoze.what.predicates.Predicate

Generic predicate checker.

This is the base predicate class. It won’t do anything useful for you, unless you subclass it.

__init__

x.__init__(...) initializes x; see x.__class__.__doc__ for signature

Single predicate checkers

class repoze.what.predicates.Not(predicate)

Negate the specified predicate.

Parameters:predicate – The predicate to be negated.

Example:

# The user *must* be anonymous:
p = Not(NotAnonymous())

Compound predicate checkers

You may create a compound predicate by aggregating single (or even compound) predicate checkers with the functions below:

class repoze.what.predicates.All(*predicates)

Check that all of the specified predicates are met.

Parameters:predicates – All of the predicates that must be met.

Example:

# Grant access if the current month is July and the user belongs to
# the human resources group.
p = All(IsMonth(7), InGroup('hr'))

This predicate is met when all the inner predicates are met, unmet when at least one of them is unmet and indeterminate for the rest of the situation.

class repoze.what.predicates.Any(*predicates)

Check that at least one of the specified predicates is met.

Parameters:predicates – Any of the predicates that must be met.

Example:

# Grant access if the currest user is Richard Stallman or Linus
# Torvalds.
p = Any(IsUser("rms"), IsUser("linus"))

This predicate is met when at least one of the inner predicates is met and unmet when all of the inner predicates are unmet. If none of the predicates are met and also there’s at least one indeterminate predicate, this predicate is indeterminate.

But you can also nest compound predicates:

p = All(Any(is_month(4), is_month(10)), has_permission('release'))

Which may be translated as “Anyone granted the ‘release’ permission may release a version of Ubuntu, if and only if it’s April or October”.

Predicate errors

Table Of Contents

Previous topic

Controlling access with predicates

Next topic

Evaluating your predicates

This Page