User:Pablo-flores/Python WikiPerson

This is just a small script in Python to standardize and facilitate the creation of articles about people. Copy, paste into something like wikiperson.py, then import it, and then go for example:

wikiperson.WikiPerson( ("John Doe", "John Thisismyfullname Doe", "Doe, John"),                        ("People with short last names", ),                        ("1 January 1900", "31 December 2000") )

The first argument is a 3-tuple: article name, full person's name, name with surname first (for alphabetization). The second argument is a tuple or list of categories. The third is a 2-tuple of dates (either full "day month year" dates or just years). See the source code for more details.

The above gives John Thisismyfullname Doe 1 January 1900&amp;nbsp;&amp;ndash;&amp;nbsp;31 December 2000

This is a very preliminary version, and mostly intended for myself. It was tested using Python 2.4.1 (actually ActivePython, build 247, under Windows XP), but it should work fine with older versions (2.0+).

class WikiSimpleDate: def __init__(self, spec): if type(spec) is int: self.year = spec self.type = "y" else: if type(spec) not in (list, tuple): dmy = spec.split else: dmy = spec if len(dmy) != 3: raise ValueError, "Must be a 3-item sequence." else: self.day = int(dmy[0]) self.month = str(dmy[1]) self.year = int(dmy[2]) self.type = "dmy"

def make(self): if self.type == "y": self.value = "%d" % self.year elif self.type == "dmy": self.value = "%d %s %d" % (self.day, self.month, self.year) else: raise ValueError, "Cannot process." return self.value

class WikiPerson: def __init__(self, names, categories, dates): self.articlename = names[0] self.fullname = names[1] self.catname = names[2] self.categories = list(categories) self.datebirth = WikiSimpleDate(dates[0]) self.datedeath = len(dates) > 1 and WikiSimpleDate(dates[1]) or None

def output(self): o = "%s " % self.fullname o += self.datebirth.make self.categories.append("%s births" % self.datebirth.year) if self.datedeath: self.categories.append("%s deaths" % self.datedeath.year) spacer = ("dmy" in (self.datebirth.type, self.datedeath.type)) \ and "&amp;nbsp;" or "" o += "%s&amp;ndash;%s%s" % (spacer, spacer, self.datedeath.make) o += "\n\n" o += "\n".join(["" % (c, n) for (c, n) in                           [(cat, self.catname) for cat in self.categories]]) return o

def edit(self): pass # to open the edit page for the article