Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
unit_str = kwargs[kwarg]
remove_kwargs.append(kwarg)
if not isinstance(unit_str, string_types):
raise TypeError('units must be strings')
self.units[var] = cfunits.Units(unit_str)
for kwarg in remove_kwargs:
kwargs.pop(kwarg)
# make sure the remaining variables are quantities
self._ensure_quantities(*kwargs.keys())
# convert quantities to reference units
for kwarg in kwargs:
if (kwarg in self.units and
self.units[kwarg] != self._ref_units[kwarg]):
# special unit defined
# convert to reference unit for calculations
kwargs[kwarg] = cfunits.Units.conform(
kwargs[kwarg], self.units[kwarg], self._ref_units[kwarg])
# also store the quantities
self.vars = kwargs
else: # no solution caching for this class
funcs, func_args, extra_values = \
_get_shortest_solution(args, tuple(self.vars.keys()), (),
self.methods)
# Above method completed successfully if no ValueError has been raised
# Calculate each quantity we need to calculate in order
for i, func in enumerate(funcs):
# Compute this quantity
value = func(*[self.vars[varname] for varname in func_args[i]])
# Add it to our dictionary of quantities for successive functions
self.vars[extra_values[i]] = value
return_list = []
for arg in args:
# do corrections for non-standard units
if arg in self.units and self.units[arg] != self._ref_units[arg]:
self.vars[arg] = cfunits.Units.conform(
self.vars[arg], self._ref_units[arg], self.units[arg])
return_list.append(self.vars[arg])
if self._debug:
# We should return a list of funcs as the last item returned
if len(return_list) == 1:
return _check_scalar(return_list[0]), funcs
else:
return ([_check_scalar(val) for val in return_list] +
[funcs, ])
else:
# no function debugging, just return the quantities
if len(args) == 1:
return _check_scalar(return_list[0])
else:
return [_check_scalar(val) for val in return_list]