Created
March 30, 2011 00:46
-
-
Save pgollakota/893658 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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