def import_library(libfilepointer): """ Import a units library, replacing any existing definitions. Parameters ---------- libfilepointer : file new library file to work with Returns ------- ConfigParser newly updated units library for the module """ global _UNIT_LIB global _UNIT_CACHE _UNIT_CACHE = {} _UNIT_LIB = ConfigParser() _UNIT_LIB.optionxform = _do_nothing # New in Python 3.2: read_file() replaces readfp(). if sys.version_info >= (3, 2): _UNIT_LIB.read_file(libfilepointer) else: _UNIT_LIB.readfp(libfilepointer) required_base_types = ['length', 'mass', 'time', 'temperature', 'angle'] _UNIT_LIB.base_names = list() # used to is_angle() and other base type checking _UNIT_LIB.base_types = dict() _UNIT_LIB.unit_table = dict() _UNIT_LIB.prefixes = dict() _UNIT_LIB.help = list() for prefix, factor in _UNIT_LIB.items('prefixes'): factor, comma, comment = factor.partition(',') _UNIT_LIB.prefixes[prefix] = float(factor) base_list = [0] * len(_UNIT_LIB.items('base_units')) for i, (unit_type, name) in enumerate(_UNIT_LIB.items('base_units')): _UNIT_LIB.base_types[unit_type] = i powers = list(base_list) powers[i] = 1 # print '%20s'%unit_type, powers # cant use add_unit because no base units exist yet _new_unit(name, 1, powers) _UNIT_LIB.base_names.append(name) # test for required base types missing = [utype for utype in required_base_types if utype not in _UNIT_LIB.base_types] if missing: raise ValueError('Not all required base type were present in the' ' config file. missing: %s, at least %s required' % (missing, required_base_types)) _update_library(_UNIT_LIB) return _UNIT_LIB