@@ -133,37 +133,34 @@ def obj_or_id(parameter, param_name, object_types):
133133 raise TypeError (message )
134134
135135
136- def obj_or_str (obj , attr , object_types ):
136+ def obj_or_str (parameter , param_name , object_types ):
137137 """
138- Accepts an object. If the object has the attribute, return the
138+ Accepts either an object or a string. If it is a string, return it directly.
139+ If it is an object and the object is of correct type, return the object's
139140 corresponding string. Otherwise, throw an exception.
140141
141- :param obj : object from which to retrieve attribute
142- :type obj: object
143- :param attr : name of the attribute to retrieve
144- :type attr : str
142+ :param parameter : object from which to retrieve attribute
143+ :type parameter: str or object
144+ :param param_name : name of the attribute to retrieve
145+ :type param_name : str
145146 :param object_types: tuple containing the types of the object being passed in
146147 :type object_types: tuple
147148 :rtype: str
148149 """
149- try :
150- return str (getattr (obj , attr ))
151- except (AttributeError , TypeError ):
152- if not isinstance (attr , str ):
153- raise TypeError (
154- "Atttibute parameter {} must be of type string" .format (attr )
155- )
156- for obj_type in object_types :
157- if isinstance (obj , obj_type ):
158- try :
159- return str (getattr (obj , attr ))
160- except AttributeError :
161- raise AttributeError ("{} object does not have {} attribute" ).format (
162- obj , attr
163- )
150+ if isinstance (parameter , str ):
151+ return parameter
164152
165- obj_type_list = "," .join ([obj_type .__name__ for obj_type in object_types ])
166- raise TypeError ("Parameter {} must be of type {}." .format (obj , obj_type_list ))
153+ for obj_type in object_types :
154+ if isinstance (parameter , obj_type ):
155+ try :
156+ return str (getattr (parameter , param_name ))
157+ except AttributeError :
158+ raise AttributeError ("{} object does not have {} attribute" ).format (
159+ parameter , param_name
160+ )
161+
162+ obj_type_list = "," .join ([obj_type .__name__ for obj_type in object_types ])
163+ raise TypeError ("Parameter {} must be of type {}." .format (parameter , obj_type_list ))
167164
168165
169166def get_institution_url (base_url ):
0 commit comments