Skip to content

Instantly share code, notes, and snippets.

@alecmunro
Created September 13, 2011 17:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alecmunro/1214513 to your computer and use it in GitHub Desktop.
Save alecmunro/1214513 to your computer and use it in GitHub Desktop.
Strategy Pattern example
def not_empty_string(value):
assert(len(value) > 0)
def valid_email(value):
assert("@" in value)
def greater_than_zero(value):
assert(int(value) > 0)
FIELD_VALIDATORS = {
"name": not_empty_string,
"email": valid_email,
"age": greater_than_zero
}
def validate_form(form):
for field in form.fields:
FIELD_VALIDATORS[field.name](field.value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment