Author: bugman Date: Wed Feb 24 01:00:01 2010 New Revision: 10883 URL: http://svn.gna.org/viewcvs/relax?rev=10883&view=rev Log: Added a __getattr__() method for generating variables on the fly. This is being used to create the 'page_first' and 'page_last' objects. Modified: 1.3/info.py Modified: 1.3/info.py URL: http://svn.gna.org/viewcvs/relax/1.3/info.py?rev=10883&r1=10882&r2=10883&view=diff ============================================================================== --- 1.3/info.py (original) +++ 1.3/info.py Wed Feb 24 01:00:01 2010 @@ -23,6 +23,9 @@ # Module docstring. """Module containing the introductory text container.""" +# Python module imports. +from string import split + # relax module imports. import dep_check import numpy @@ -392,6 +395,42 @@ url = None pages = None year = None + + + def __getattr__(self, name): + """Generate some variables on the fly. + + This is only called for objects not found in the class. + + @param name: The name of the object. + @type name: str + @raises: AttributeError if the object cannot be created. + @returns: The generated object. + @rtype: anything + """ + + # Page numbers. + if name in ['page_first', 'page_last']: + # No page info. + if not self.pages: + return None + + # First split the page range. + vals = split(self.pages, '-') + + # Single page. + if len(vals) == 1: + return vals[0] + + # First page. + if name == 'page_first': + return vals[0] + + # Last page. + if name == 'page_last': + return vals[1] + + raise AttributeError, name def cite_short(self, author=True, title=True, journal=True, volume=True, number=True, pages=True, year=True, doi=True, url=True):