From 2f94281f983f86b23f8e5bcdc790fd4e032a0a2d Mon Sep 17 00:00:00 2001 From: Thibaut Horel Date: Thu, 4 Feb 2016 21:44:18 -0500 Subject: Modernizing Python style --- entry.py | 77 ++++++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 46 insertions(+), 31 deletions(-) (limited to 'entry.py') diff --git a/entry.py b/entry.py index 4be2bc2..e9dd6e3 100644 --- a/entry.py +++ b/entry.py @@ -15,40 +15,53 @@ WWW_FIELDS = ['www_section', 'www_important', 'www_remarks', 'www_excerpt_url', 'www_publisher_url', 'www_cache_section', 'www_tags'] +PROCEEDINGS_RE = re.compile( + r'((?:proceedings|workshop record) of(?: the)? )(.*)', + re.I) + + def author_url(author): """Given an author's name, return a URL for his/her homepage.""" for pat, url in config.AUTHOR_RE_LIST: if pat.search(author): return url return None -PROCEEDINGS_RE = re.compile( - r'((?:proceedings|workshop record) of(?: the)? )(.*)', - re.I) # List of fields that appear when we display the entries as BibTeX. -DISPLAYED_FIELDS = [ 'title', 'author', 'journal', 'booktitle', -'school', 'institution', 'organization', 'volume', 'number', 'year', -'month', 'address', 'location', 'chapter', 'edition', 'pages', 'editor', -'howpublished', 'key', 'publisher', 'type', 'note', 'series' ] +DISPLAYED_FIELDS = ['title', 'author', 'journal', 'booktitle', + 'school', 'institution', 'organization', 'volume', + 'number', 'year', 'month', 'address', 'location', + 'chapter', 'edition', 'pages', 'editor', 'howpublished', + 'key', 'publisher', 'type', 'note', 'series'] + class BibTeXEntry: """A single BibTeX entry.""" def __init__(self, type, key, entries): self.type = type # What kind of entry is it? (@book,@injournal,etc) - self.key = key # What key does it have? - self.entries = entries # Map from key to value. - self.entryLine = 0 # Defined on this line number + self.key = key # What key does it have? + self.entries = entries # Map from key to value. + self.entryLine = 0 # Defined on this line number + def get(self, k, v=None): - return self.entries.get(k,v) - def has_key(self, k): - return self.entries.has_key(k) + return self.entries.get(k, v) + + def __contains__(self, k): + return k in self.entries + def __getitem__(self, k): return self.entries[k] + def __setitem__(self, k, v): self.entries[k] = v + def __str__(self): - return self.format(70,1) + return self.format(70, 1) + + def __iter__(self): + return iter(self.entries.keys()) + def getURL(self): """Return the best URL to use for this paper, or None.""" best = None @@ -74,32 +87,34 @@ class BibTeXEntry: else: df = DISPLAYED_FIELDS for f in df: - if not self.entries.has_key(f): + if f not in self: continue v = self.entries[f] if v.startswith(""): d.append("%%%%% ERROR: Missing field\n") - d.append("%% %s = {?????},\n"%f) + d.append("%% %s = {?????},\n" % f) continue np = v.translate(ALLCHARS, PRINTINGCHARS) if np: - d.append("%%%%% "+("ERROR: Non-ASCII characters: '%r'\n"%np)) + d.append("%%%%% " + + ("ERROR: Non-ASCII characters: '%r'\n" % np)) d.append(" ") v = v.replace("&", "&") - if invStrings.has_key(v): - s = "%s = %s,\n" %(f, invStrings[v]) + if v in invStrings: + s = "%s = %s,\n" % (f, invStrings[v]) else: s = "%s = {%s},\n" % (f, v) - d.append(_split(s,width,indent)) + d.append(_split(s, width, indent)) d.append("}\n") return "".join(d) + def resolve(self): """Handle post-processing for this entry""" a = self.get('author') if a: self.parsedAuthor = parseAuthor(a) - #print a - #print " => ",repr(self.parsedAuthor) + # print a + # print " => ",repr(self.parsedAuthor) else: self.parsedAuthor = None @@ -136,15 +151,15 @@ class BibTeXEntry: fields = () else: fields = () - errs.append("ERROR: odd type %s"%self.type) + errs.append("ERROR: odd type %s" % self.type) if self.type != 'proceedings': fields += 'title', 'author', 'www_section', 'year' for field in fields: if self.get(field) is None or \ - self.get(field).startswith(""): + self.get(field).startswith(""): errs.append("ERROR: %s has no %s field" % (self.key, field)) - self.entries[field] = "%s:??"%field + self.entries[field] = "%s:??" % field if self.type == 'inproceedings': if self.get("booktitle"): @@ -152,8 +167,8 @@ class BibTeXEntry: not self['booktitle'].startswith("{Proceedings of"): errs.append("ERROR: %s's booktitle (%r) doesn't start with 'Proceedings of'" % (self.key, self['booktitle'])) - if self.has_key("pages") and not re.search(r'\d+--\d+', self['pages']): - errs.append("ERROR: Misformed pages in %s"%self.key) + if "pages" in self and not re.search(r'\d+--\d+', self['pages']): + errs.append("ERROR: Misformed pages in %s" % self.key) if self.type == 'proceedings': if self.get('title'): @@ -161,14 +176,14 @@ class BibTeXEntry: for field, value in self.entries.items(): if value.translate(ALLCHARS, PRINTINGCHARS): - errs.append("ERROR: %s.%s has non-ASCII characters"%( + errs.append("ERROR: %s.%s has non-ASCII characters" % ( self.key, field)) if field.startswith("www_") and field not in WWW_FIELDS: - errs.append("ERROR: unknown www field %s"% field) + errs.append("ERROR: unknown www field %s" % field) if value.strip()[-1:] == '.' and \ field not in ("notes", "www_remarks", "author"): - errs.append("ERROR: %s.%s has an extraneous period"%(self.key, - field)) + errs.append("ERROR: %s.%s has an extraneous period" % (self.key, + field)) return errs def biblio_to_html(self): -- cgit v1.2.3-70-g09d2