def extract_substructure(base, substructure):
 if ((type(substructure) is not dict) and (type(base) is not dict)):
  return base
 if (type(base) is not dict):
  raise MissingConfigError('Found non-dict value {} when expecting a sub-configuration {}.'.format(repr(base), repr(substructure)))
 if (type(substructure) is not dict):
  raise MissingConfigError('Found dict value {} when expecting a simple configuration value {}.'.format(repr(base), repr(substructure)))
 try:
  subdict = []
  for (key, value) in substructure.iteritems():
   if (type(value) is Optional):
    base_val = base.get(key, value.default)
   elif _is_optional(value):
    base_val = base.get(key, {})
   else:
    base_val = base[key]
   subdict.append((key, extract_substructure(base_val, value)))
  return dict(subdict)
 except KeyError as e:
  raise MissingConfigError('Missing key {} in configuration'.format(e.args[0]))

def extract_substructure ( base , substructure ) : if ( ( type ( substructure ) is not dict ) and ( type ( base ) is not dict ) ) : return base if ( type ( base ) is not dict ) : raise MissingConfigError ( 'Found non-dict value {} when expecting a sub-configuration {}.' . format ( repr ( base ) , repr ( substructure ) ) ) if ( type ( substructure ) is not dict ) : raise MissingConfigError ( 'Found dict value {} when expecting a simple configuration value {}.' . format ( repr ( base ) , repr ( substructure ) ) ) try : subdict = [ ] for ( key , value ) in substructure . iteritems ( ) : if ( type ( value ) is Optional ) : base_val = base . get ( key , value . default ) elif _is_optional ( value ) : base_val = base . get ( key , { } ) else : base_val = base [ key ] subdict . append ( ( key , extract_substructure ( base_val , value ) ) ) return dict ( subdict ) except KeyError as e : raise MissingConfigError ( 'Missing key {} in configuration' . format ( e . args [ 0 ] ) )
assuming that substructure is a possibly nested dictionary .