SHARE
TWEET

Untitled

a guest Mar 16th, 2012 20 Never
  1. class Book(object):
  2.     """Book class to represent a book in the library."""
  3.    
  4.     known_fields = ['title', 'authors', 'pages' ... ]
  5.    
  6.     def __init__(self, **kwargs):
  7.         # internal dict to store book information
  8.         self._info = {}
  9.        
  10.         for k, v in kwargs.iteritems():
  11.             # check for typos
  12.             Book.check_field_name(k)
  13.             self._info[k] = v
  14.        
  15.         # either the title or ISBN needs to be provided to identify the book
  16.         if not self.title and not self.ISBN:
  17.             raise InputError('Title or ISBN needed to identify the book')
  18.        
  19.         self.owned = True if self.file_path != None else False
  20.         self.tags = set()
  21.            
  22.     def __getattr__(self, name):
  23.         return self._info.get(name, None)
  24.    
  25.     @staticmethod
  26.     def check_field_name(field_name):
  27.         if not field_name in Book.known_fields:
  28.             warnings.warn('Unknown field: {0}'.format(field_name), UserWarning)
RAW Paste Data
Top