本文整理汇总了Python中validator.Validator.get_error方法的典型用法代码示例。如果您正苦于以下问题:Python Validator.get_error方法的具体用法?Python Validator.get_error怎么用?Python Validator.get_error使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类validator.Validator
的用法示例。
在下文中一共展示了Validator.get_error方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ValidatorTests
# 需要导入模块: from validator import Validator [as 别名]
# 或者: from validator.Validator import get_error [as 别名]
class ValidatorTests(unittest.TestCase):
def setUp(self):
self.validator = Validator()
def test_language_validator_sanity(self):
self.assertTrue(self.validator.language_validator("CSS"))
def test_language_validator_sanity_test_invalid_input(self):
self.assertFalse(self.validator.language_validator("abcd454"))
expected = {'error_code': 1, 'error': "invalid language"}
extracted = self.validator.get_error()
self.assertEqual(expected, extracted)
def test_creation_date_sanity(self):
self.assertTrue(self.validator.creation_date_validator("past-week"))
def test_creation_date_sanity_test_invalid_input(self):
self.assertFalse(self.validator.creation_date_validator("abcd454"))
expected = {'error_code': 2, 'error': "invalid creation date"}
extracted = self.validator.get_error()
self.assertEqual(expected, extracted)
示例2: GithubApiView
# 需要导入模块: from validator import Validator [as 别名]
# 或者: from validator.Validator import get_error [as 别名]
class GithubApiView(View):
""" The main API handler (Works for HTTP GET). """
methods = ['GET']
DATE_FORMAT = '%Y-%m-%d'
MAX_RESULTS = 10
def __init__(self):
self.validator = Validator()
def dispatch_request(self):
"""
Validates & Parses user input, then queries github API and returns
either an object (or an error object, if any).
"""
language = request.args.get('language') or 'Any'
creation_date = request.args.get('created')
if not self.validator.validate(language, creation_date):
return json.dumps(self.validator.get_error())
query = 'language:{0}+created:{1}'.format(language, self._parse_creation_date(creation_date))
repositories = requests.get(GITHUB_API_URL.format(query=query)).json()['items'][:self.MAX_RESULTS]
results = [self._build_repository_object(r) for r in repositories]
return json.dumps({'error_code': 0, 'error': None, 'results': results})
def _parse_creation_date(self, creation_date):
now = datetime.datetime.now()
date = {'past-week': '>=' + (now - relativedelta(weeks=1)).strftime(self.DATE_FORMAT),
'past-month': '>=' + (now - relativedelta(months=1)).strftime(self.DATE_FORMAT),
'past-year': '>=' + (now - relativedelta(years=1)).strftime(self.DATE_FORMAT),
None: '<=' + now.strftime(self.DATE_FORMAT)}
return date[creation_date]
@staticmethod
def _build_repository_object(repository):
return {'id': repository['id'],
'full_name': repository['full_name'],
'html_url': repository['html_url'],
'description': repository['description'],
'private': repository['private'],
'created_at': repository['created_at'],
'updated_at': repository['updated_at'],
'clone_url': repository['clone_url'],
'size': repository['size'],
'stargazers_count': repository['stargazers_count'],
'language': repository['language'],
'forks_count': repository['forks_count']}