def decompose_to_known_units(unit, func):
 from .. import core
 if isinstance(unit, core.CompositeUnit):
  new_unit = core.Unit(unit.scale)
  for (base, power) in zip(unit.bases, unit.powers):
   new_unit = (new_unit * (decompose_to_known_units(base, func) ** power))
  return new_unit
 elif isinstance(unit, core.NamedUnit):
  try:
   func(unit)
  except ValueError:
   if isinstance(unit, core.Unit):
    return decompose_to_known_units(unit._represents, func)
   raise
  return unit

def decompose_to_known_units ( unit , func ) : from . . import core if isinstance ( unit , core . CompositeUnit ) : new_unit = core . Unit ( unit . scale ) for ( base , power ) in zip ( unit . bases , unit . powers ) : new_unit = ( new_unit * ( decompose_to_known_units ( base , func ) ** power ) ) return new_unit elif isinstance ( unit , core . NamedUnit ) : try : func ( unit ) except ValueError : if isinstance ( unit , core . Unit ) : return decompose_to_known_units ( unit . _represents , func ) raise return unit
partially decomposes a unit so it is only composed of units that are "known" to a given format .