From: Grant Edwards on

I'm playign with Python bindings for libconfig, and one of the methods
does something that seems very odd to me. The purpose of the method
is to fetch the value found a a particular path within the
configuration namespace.

If the path exists and has a scalar value the method returns the tuple
(<value>,True). Otherwise it returns ('',False). AFAICT, the latter
happens in either of two cases: the path doesn't exist at all, or the
path exists but doesn't have a scalar value (it's an aggretate object
such as a list, group, or array).

This seems distinctly un-pythonic.

I would think that the right thing to do would be to either return the
requested value or raise an exception. The libconfig C++ API defines
a "SettingNotFound" exception and "SettingTypeException" that seem
appropriate in the two failure cases.

Is there something about implementing bindings using Boost that makes
this sort of (<value>,<success>) return tuple a desirable way to do
things?

FWIW, here's the method I find puzzling:

tuple value ( const char * path )
{
std::string v_string;
if ( config->lookupValue ( path, v_string ) )
return make_tuple ( v_string.c_str(), true );

unsigned int v_uint;
if ( config->lookupValue ( path, v_uint ) )
return make_tuple ( v_uint, true );

int v_int;
if ( config->lookupValue ( path, v_int ) )
return make_tuple ( v_int, true );

bool v_bool;
if ( config->lookupValue ( path, v_bool ) )
return make_tuple ( v_bool, true );

unsigned long long v_ulonglong;
if ( config->lookupValue ( path, v_ulonglong ) )
return make_tuple ( v_ulonglong, true );

long long v_longlong;
if ( config->lookupValue ( path, v_longlong ) )
return make_tuple ( v_longlong, true );

float v_float;
if ( config->lookupValue ( path, v_float ) )
return make_tuple ( v_float, true );

double v_double;
if ( config->lookupValue ( path, v_double ) )
return make_tuple ( v_double, true );

return make_tuple ( "", false );
}





--
Grant Edwards grant.b.edwards Yow! Don't SANFORIZE me!!
at
gmail.com