Skip to content

Instantly share code, notes, and snippets.

@figgis
Created July 1, 2011 09:23
Show Gist options
  • Save figgis/1058159 to your computer and use it in GitHub Desktop.
Save figgis/1058159 to your computer and use it in GitHub Desktop.
user validation
#!/usr/bin/env python
import string
def check_length(s):
'''return length
>>> check_length('this is a string')
16
'''
return len(s)
def check_start_with(s, token=' '):
'''return False if string begins with token
>>> check_start_with(' test')
False
>>> check_start_with('test')
True
'''
return not s.startswith(token)
def check_ends_with(s, token=' '):
'''return False if string begins with token
>>> check_ends_with('test ')
False
>>> check_ends_with('test')
True
'''
return not s.endswith(token)
def check_digits(s):
'''return #digits in string
>>> check_digits('abc123')
3
'''
return len([i for i in s if i in string.digits])
def check_uppercase(s):
'''return # of [A-Z] in string
>>> check_uppercase('abcABC')
3
'''
return len([i for i in s if i in string.ascii_uppercase])
def check_lowercase(s):
'''return # of [A-Z] in string
>>> check_lowercase('abcABC')
3
'''
return len([i for i in s if i in string.ascii_lowercase])
def get_occurence(s, token):
'''get # tokens in string
>>> get_occurence(r'1..@@__--', '@')
2
>>> get_occurence(r'1..@@__--', '_')
2
'''
return s.count(token)
checks = [check_lowercase, check_start_with, check_ends_with, check_digits,
check_uppercase, check_lowercase]
user = 'sometestuser@123ABC'
result=[]
for c in checks:
result.append(c(user))
print result
if __name__ == "__main__":
import doctest
doctest.testmod()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment