def __init__(self, descr=None, **kwargs): """Initialize ClassWithCollections object Parameters ---------- descr : str Description of the instance """ # Note: __params_set was initialized in __new__ if not self.__params_set: self.__descr = descr """Set humane description for the object""" # To avoid double initialization in case of multiple inheritance self.__params_set = True collections = self._collections # Assign attributes values if they are given among # **kwargs for arg, argument in kwargs.items(): isset = False for collection in collections.itervalues(): if collection._is_initializable(arg): collection._initialize(arg, argument) isset = True break if isset: _ = kwargs.pop(arg) else: known_params = reduce( lambda x,y:x+y, [x.keys() for x in collections.itervalues() if not isinstance(x, ConditionalAttributesCollection) ], []) _, kwargs_list, _ = get_docstring_split(self.__init__) known_params = sorted(known_params + [x[0] for x in kwargs_list]) raise TypeError( "Unexpected keyword argument %s=%s for %s." % (arg, argument, self) + "\n\tValid parameters are: %s" % ', '.join(known_params)) ## Initialize other base classes ## commented out since it seems to be of no use for now #if init_classes is not None: # # return back stateful arguments since they might be # # processed by underlying classes # kwargs.update(kwargs_stateful) # for cls in init_classes: # cls.__init__(self, **kwargs) #else: # if len(kwargs)>0: # known_params = reduce(lambda x, y: x + y, \ # [x.keys() for x in collections], # []) # raise TypeError, \ # "Unknown parameters %s for %s." % (kwargs.keys(), # self) \ # + " Valid parameters are %s" % known_params if __debug__: debug("COL", "ClassWithCollections.__init__ was done " "for %s%s with descr=%s", (self.__class__.__name__, _strid(self), descr))
def test_docstrings(self): #import mvpa2.suite as mv from mvpa2.suite import suite_stats # Lets do compliance checks # Get gross information on what we have in general #mv_scope = dict((x, getattr(mv, x)) for x in dir(mv)) gs = suite_stats() #mv_scope) # all functions/classes/types should have some docstring missing = [] # We should not have both :Parameters: and new style Parameters conflicting = [] con_re1 = re.compile(':Parameters?:') con_re2 = re.compile( '(?::Parameters?:.*Parameters?\s*\n\s*-------' '|Parameters?\s*\n\s*-------.*:Parameters?:)', flags=re.DOTALL) for c in ('functions', 'modules', 'objects', 'types') \ + ('classes',) if sys.version_info[0] < 3 else (): missing1 = [] conflicting1 = [] self.assertTrue(gs[c]) for k, i in gs[c].iteritems(): try: s = i.__doc__.strip() except: # pragma: no cover - should not be hit if ok_ s = "" if s == "": missing1.append(k) if hasattr(i, '__init__') and not c in ['objects']: # Smoke test get_docstring_split which would be used # if someone specifies incorrect keyword argument _ = get_docstring_split(i.__init__) #if not None in _: # print [x[0] for x in _[1]] si = i.__init__.__doc__ k += '.__init__' if si is None or si == "": try: i_file = inspect.getfile(i) if i_file == inspect.getfile(i.__init__) \ and 'mvpa' in i_file: # if __init__ wasn't simply taken from some parent # which is not within MVPA missing1.append(k) except TypeError: # for some things like 'debug' inspect can't figure path # just skip for now pass else: si = s if si is not None \ and con_re1.search(si) and con_re2.search(si): conflicting1.append(k) if len(missing1): # pragma: no cover - should not be hit if ok_ missing.append("%s: " % c + ', '.join(missing1)) if len(conflicting1 ): # pragma: no cover - should not be hit if ok_ conflicting.append("%s: " % c + ', '.join(conflicting1)) sfailures = [] if len(missing): # pragma: no cover - should not be hit if ok_ sfailures += [ "Some items have missing docstrings:\n " + '\n '.join(missing) ] if len(conflicting): # pragma: no cover - should not be hit if ok_ sfailures += [ "Some items have conflicting formats of docstrings:\n " + '\n '.join(conflicting) ] if len(sfailures): # pragma: no cover - should not be hit if ok_ self.fail('\n'.join(sfailures))
def __init__(self, descr=None, **kwargs): """Initialize ClassWithCollections object Parameters ---------- descr : str Description of the instance """ # Note: __params_set was initialized in __new__ if not self.__params_set: self.__descr = descr """Set humane description for the object""" # To avoid double initialization in case of multiple inheritance self.__params_set = True collections = self._collections # Assign attributes values if they are given among # **kwargs for arg, argument in self._custom_kwargs_sort_items(**kwargs): isset = False for collection in collections.itervalues(): if collection._is_initializable(arg): collection._initialize(arg, argument) isset = True break if isset: _ = kwargs.pop(arg) else: known_params = reduce( lambda x,y:x+y, [x.keys() for x in collections.itervalues() if not isinstance(x, ConditionalAttributesCollection) ], []) _, kwargs_list, _ = get_docstring_split(self.__init__) known_params = sorted(known_params + [x[0] for x in kwargs_list]) raise TypeError( "Unexpected keyword argument %s=%s for %s." % (arg, argument, self) + "\n\tValid parameters are: %s" % ', '.join(known_params)) ## Initialize other base classes ## commented out since it seems to be of no use for now #if init_classes is not None: # # return back stateful arguments since they might be # # processed by underlying classes # kwargs.update(kwargs_stateful) # for cls in init_classes: # cls.__init__(self, **kwargs) #else: # if len(kwargs)>0: # known_params = reduce(lambda x, y: x + y, \ # [x.keys() for x in collections], # []) # raise TypeError, \ # "Unknown parameters %s for %s." % (kwargs.keys(), # self) \ # + " Valid parameters are %s" % known_params if __debug__: debug("COL", "ClassWithCollections.__init__ was done " "for %s%s with descr=%s", (self.__class__.__name__, _strid(self), descr))
def test_docstrings(self): #import mvpa2.suite as mv from mvpa2.suite import suite_stats # Lets do compliance checks # Get gross information on what we have in general #mv_scope = dict((x, getattr(mv, x)) for x in dir(mv)) gs = suite_stats()#mv_scope) # all functions/classes/types should have some docstring missing = [] # We should not have both :Parameters: and new style Parameters conflicting = [] con_re1 = re.compile(':Parameters?:') con_re2 = re.compile('(?::Parameters?:.*Parameters?\s*\n\s*-------' '|Parameters?\s*\n\s*-------.*:Parameters?:)', flags=re.DOTALL) for c in ('functions', 'modules', 'objects', 'types') \ + ('classes',) if sys.version_info[0] < 3 else (): missing1 = [] conflicting1 = [] self.assertTrue(gs[c]) for k, i in gs[c].iteritems(): try: s = i.__doc__.strip() except: # pragma: no cover - should not be hit if ok_ s = "" if s == "": missing1.append(k) if hasattr(i, '__init__') and not c in ['objects']: # Smoke test get_docstring_split which would be used # if someone specifies incorrect keyword argument _ = get_docstring_split(i.__init__) #if not None in _: # print [x[0] for x in _[1]] si = i.__init__.__doc__ k += '.__init__' if si is None or si == "": try: i_file = inspect.getfile(i) if i_file == inspect.getfile(i.__init__) \ and 'mvpa' in i_file: # if __init__ wasn't simply taken from some parent # which is not within MVPA missing1.append(k) except TypeError: # for some things like 'debug' inspect can't figure path # just skip for now pass else: si = s if si is not None \ and con_re1.search(si) and con_re2.search(si): conflicting1.append(k) if len(missing1): # pragma: no cover - should not be hit if ok_ missing.append("%s: " % c + ', '.join(missing1)) if len(conflicting1): # pragma: no cover - should not be hit if ok_ conflicting.append("%s: " % c + ', '.join(conflicting1)) sfailures = [] if len(missing): # pragma: no cover - should not be hit if ok_ sfailures += ["Some items have missing docstrings:\n " + '\n '.join(missing)] if len(conflicting): # pragma: no cover - should not be hit if ok_ sfailures += ["Some items have conflicting formats of docstrings:\n " + '\n '.join(conflicting)] if len(sfailures): # pragma: no cover - should not be hit if ok_ self.fail('\n'.join(sfailures))