def wrap_numpydoc_obj(obj_to_wrap): callable_methods = {} property_unit_map = {} for i in dir(obj_to_wrap): attr = getattr(obj_to_wrap, i) if isinstance(attr, types.FunctionType) or isinstance( attr, types.MethodType) or type(attr) == property: if type(attr) == property: name = attr.fget.__name__ else: name = attr.__name__ if hasattr(attr, '__doc__'): if type(attr) == property: property_unit_map[name] = u( match_parse_units(attr.fget.__doc__, i=0)) else: parsed = parse_numpydoc_variables_units(attr) callable_methods[name] = clean_parsed_info(parsed) if 'Attributes' in parsed: property_unit_map.update(parsed['Attributes']) # We need to parse the __doc__ for the main docstring of each of the inherited # objects, but in reverse order so older properties get overwritten by newer # properties. Ignore the object type as well. for inherited in reversed(list(obj_to_wrap.__mro__[0:-1])): parsed = parse_numpydoc_variables_units(inherited) callable_methods['__init__'] = clean_parsed_info(parsed) if 'Attributes' in parsed: property_unit_map.update({ var: u(unit) for var, unit in zip(parsed['Attributes']['vars'], parsed['Attributes']['units']) }) if 'Parameters' in parsed: property_unit_map.update({ var: u(unit) for var, unit in zip(parsed['Parameters']['vars'], parsed['Parameters']['units']) }) name = obj_to_wrap.__name__ fun = type( name, (UnitAwareClass, ), { 'wrapped': obj_to_wrap, #'__doc__': obj_to_wrap.__doc__, 'property_units': property_unit_map, 'method_units': callable_methods }) return fun
def wrap_numpydoc_obj(obj_to_wrap): callable_methods = {} property_unit_map = {} for i in dir(obj_to_wrap): attr = getattr(obj_to_wrap, i) if isinstance(attr, types.FunctionType) or isinstance(attr, types.MethodType) or type(attr) == property: if type(attr) == property: name = attr.fget.__name__ else: name = attr.__name__ if hasattr(attr, '__doc__'): if type(attr) == property: property_unit_map[name] = u(match_parse_units(attr.fget.__doc__, i=0)) else: parsed = parse_numpydoc_variables_units(attr) callable_methods[name] = clean_parsed_info(parsed) if 'Attributes' in parsed: property_unit_map.update(parsed['Attributes']) # We need to parse the __doc__ for the main docstring of each of the inherited # objects, but in reverse order so older properties get overwritten by newer # properties. Ignore the object type as well. for inherited in reversed(list(obj_to_wrap.__mro__[0:-1])): parsed = parse_numpydoc_variables_units(inherited) callable_methods['__init__'] = clean_parsed_info(parsed) if 'Attributes' in parsed: property_unit_map.update({var:u(unit) for var, unit in zip(parsed['Attributes']['vars'], parsed['Attributes']['units'])} ) if 'Parameters' in parsed: property_unit_map.update({var:u(unit) for var, unit in zip(parsed['Parameters']['vars'], parsed['Parameters']['units'])} ) name = obj_to_wrap.__name__ fun = type(name, (UnitAwareClass,), {'wrapped': obj_to_wrap, #'__doc__': obj_to_wrap.__doc__, 'property_units': property_unit_map, 'method_units': callable_methods}) return fun
def wrap_numpydoc_obj(obj_to_wrap): callable_methods = {} property_unit_map = {} static_methods = set([]) class_methods = set([]) for prop in dir(obj_to_wrap): attr = getattr(obj_to_wrap, prop) if isinstance(attr, types.FunctionType) or isinstance( attr, types.MethodType) or type(attr) == property: try: if isinstance(obj_to_wrap.__dict__[prop], staticmethod): static_methods.add(prop) if isinstance(obj_to_wrap.__dict__[prop], classmethod): class_methods.add(prop) except: pass if type(attr) is property: name = prop #name = attr.fget.__name__ else: name = attr.__name__ if hasattr(attr, '__doc__') and attr.__doc__ is not None: if type(attr) is property: try: docstring = attr.__doc__ if docstring is None: docstring = attr.fget.__doc__ # Is it a full style string? if 'Returns' in docstring and '-------' in docstring: found_unit = u( parse_numpydoc_variables_units_docstring( docstring)['Returns']['units'][0]) else: found_unit = u(match_parse_units(docstring, i=0)) except Exception as e: if name[0] == '_': found_unit = u.dimensionless else: print('Failed on attribute %s' % name) raise e property_unit_map[name] = found_unit else: parsed = parse_numpydoc_variables_units(attr) callable_methods[name] = clean_parsed_info(parsed) if 'Attributes' in parsed: property_unit_map.update(parsed['Attributes']) # We need to parse the __doc__ for the main docstring of each of the inherited # objects, but in reverse order so older properties get overwritten by newer # properties. Ignore the object type as well. for inherited in reversed(list(obj_to_wrap.__mro__[0:-1])): parsed = parse_numpydoc_variables_units(inherited) callable_methods['__init__'] = clean_parsed_info(parsed) if 'Attributes' in parsed: property_unit_map.update({ var: u(unit) for var, unit in zip(parsed['Attributes']['vars'], parsed['Attributes']['units']) }) if 'Parameters' in parsed: property_unit_map.update({ var: u(unit) for var, unit in zip(parsed['Parameters']['vars'], parsed['Parameters']['units']) }) name = obj_to_wrap.__name__ classkwargs = { 'wrapped': obj_to_wrap, 'property_units': property_unit_map, 'method_units': callable_methods, 'static_methods': static_methods, 'class_methods': class_methods } fun = type(name, (UnitAwareClass, ), classkwargs) for m in static_methods: #def a_static_method(*args, the_method=m, **kwargs): #return fun._another_getattr(the_method)(*args, **kwargs) setattr(fun, m, staticmethod(fun._another_getattr(m))) for m in class_methods: setattr(fun, m, classmethod(fun._another_getattr(m))) return fun