[Python-checkins] r54865 - in sandbox/trunk/pep0: pep0/pep.py test_pep0.py (original) (raw)
brett.cannon python-checkins at python.org
Wed Apr 18 06:07:00 CEST 2007
- Previous message: [Python-checkins] r54864 - python/tags/r251
- Next message: [Python-checkins] Python Regression Test Failures doc (1)
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Author: brett.cannon Date: Wed Apr 18 06:06:52 2007 New Revision: 54865
Modified: sandbox/trunk/pep0/pep0/pep.py sandbox/trunk/pep0/test_pep0.py Log: Beginnings of a PEP class. Testing of init is not complete, but initial coding of the method is done.
Modified: sandbox/trunk/pep0/pep0/pep.py
--- sandbox/trunk/pep0/pep0/pep.py (original) +++ sandbox/trunk/pep0/pep0/pep.py Wed Apr 18 06:06:52 2007 @@ -4,6 +4,7 @@ * Flesh out PEP class.
""" +import re
class PEP(object):
@@ -15,12 +16,75 @@ "Active", "Draft")
def __init__(self, metadata_dict):
"""Init object based on dict containing metadata from a file."""
pass
"""Init object based on dict containing metadata from a file.
Required keys are:
* PEP
value must be an integer.
* Type
Value must match a value in self.type_values.
* Status
Value must match a value in self.status_values.
* Author
Value must have at least one author in the string as returned
by self.parse_author.
"""
# Verify keys exist.
for required_key in ('PEP', 'Type', 'Status', 'Author'):
if required_key not in metadata_dict:
raise KeyError("required key %r not in dict")
# 'PEP'. PEP parsing should have already converted the number to an
# integer, so just being safe here.
self.number = int(metadata_dict['PEP'])
# 'Type'.
type_ = metadata_dict['Type']
if type_ not in self.type_values:
raise ValueError('%r is not a valid Type value (PEP %s)' %
(type_, self.number))
self.type_ = type_
# 'Status'.
status = metadata_dict['Status']
if status not in self.status_values:
raise ValueError("%r is not a valid Status value (PEP %s)" %
(status, self.number))
self.status = status
# 'Author'.
authors = self._parse_author(metadata_dict['Author'])
if len(authors) < 1:
raise ValueError("no authors found (PEP %s)" % self.number)
self.authors = authors
- def _parse_author(self, data):
"""Return a list of author names."""
angled = r'(?P<author>.+?) <.+?>'
paren = r'.+? \((?P<author>.+?)\)'
simple = r'(?P<author>[^,]+)'
author_list = []
for regex in (angled, paren, simple):
# Watch out for commas separating multiple names.
regex += '(,\s*)?'
for match in re.finditer(regex, data):
author = match.group('author')
# Watch out for suffixes like 'Jr.' when they are comma-separated
# from the name and thus cause issues when *all* names are only
# separated by commas.
author = match.group('author')
if not author.partition(' ')[1] and author.endswith('.'):
prev_author = author_list.pop()
author = ', '.join([prev_author, author])
author_list.append(author)
else:
# If authors were found then stop searching as only expect one
# style of author citation.
if author_list:
break
return author_list
- def validate(self):
"""Validate that the instance data."""
def str(self): """Return the line entry for the PEP."""pass
@@ -80,30 +144,5 @@ finished.append(pep) return meta, info, accepted, open_, finished, empty, dead
-def handle_author(data):
- """Return a list of author names."""
- angled = r'(?P.+?) <.+?>'
- paren = r'.+? ((?P.+?))'
- simple = r'(?P[^,]+)'
- author_list = []
- for regex in (angled, paren, simple):
# Watch out for commas separating multiple names.
regex += '(,\s+)?'
for match in re.finditer(regex, data):
author = match.group('author')
# Watch out for suffixes like 'Jr.' when they are comma-separated
# from the name and thus cause issues when *all* names are only
# separated by commas.
author = match.group('author')
if not author.partition(' ')[1] and author.endswith('.'):
prev_author = author_list.pop()
author = ', '.join([prev_author, author])
author_list.append(author)
else:
# If authors were found then stop searching as only expect one
# style of author citation.
if author_list:
break
- return author_list
Modified: sandbox/trunk/pep0/test_pep0.py
--- sandbox/trunk/pep0/test_pep0.py (original) +++ sandbox/trunk/pep0/test_pep0.py Wed Apr 18 06:06:52 2007 @@ -136,29 +136,30 @@ """Test the PEP class.""" - def test_valid_init(self): - # Make sure that a PEP instance can be created from a dict with valid - # entries. - pass
Number cannot be invalid as that is checked during parsing of the PEP.
- def setUp(self):
self.metadata = {'PEP': 42, 'Type': pep0.pep.PEP.type_values[0],
'Status': pep0.pep.PEP.status_values[0],
'Author': "Brett Cannon"}
- def test_init_number(self):
# PEP number must be an int.
pep = pep0.pep.PEP(self.metadata)
self.failUnlessEqual(pep.number, self.metadata['PEP'])
self.metadata['PEP'] = 'XXX'
self.failUnlessRaises(ValueError, pep0.pep.PEP, self.metadata)
- def test_invalid_status(self):
- def test_init_status(self): # Invalid Status entry raises an exception. pass
- def test_invalid_type(self):
- def test_init_type(self): # Invalid type should raise an exception. pass
- def test_invalid_authors(self):
- def test_init_authors(self): # There should be at least one author. pass
- def test_str(self):
# String representation of the PEP.
pass
@contextmanager def test_file(path): @@ -304,6 +305,7 @@ def test_main(): test_support.run_unittest( ParseTests, + PEPClassTests, )
- Previous message: [Python-checkins] r54864 - python/tags/r251
- Next message: [Python-checkins] Python Regression Test Failures doc (1)
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]