Skip to content

Instantly share code, notes, and snippets.

@pgollakota
Created March 30, 2011 00:46
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 pgollakota/893658 to your computer and use it in GitHub Desktop.
Save pgollakota/893658 to your computer and use it in GitHub Desktop.
# Implementation without the metaclass
class CharField(object):
def __init__(self, form, text=None):
self.form = form
self.text = text
def __str__(self):
return self.text
def is_valid_email(self):
return True if self.text.find('@')>0 else False
def getform(self):
return self.form
class Form(object):
fields = {}
class ContactForm(Form):
def __init__(self, data):
self.fields['first_name'] = CharField(self, data.get('first_name', ''))
self.fields['last_name'] = CharField(self, data.get('last_name', ''))
self.fields['email'] = CharField(self, data.get('email', ''))
c = ContactForm({'first_name':'John',
'last_name': 'Bond',
'email': 'jbond@jb.com'})
# The following line prints
# John Bond True
print c.fields['first_name'], c.fields['last_name'], c.fields['email'].is_valid_email()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment