import six


def get_varval_from_locals(key, locals_, strict=False):
    """
    Returns a variable value from locals.
    Different from locals()['varname'] because
    get_varval_from_locals('varname.attribute', locals())
    is allowed
    """
    assert isinstance(key, six.string_types), 'must have parsed key into a string already'
    if key not in locals_:
        dotpos = key.find('.')
        if dotpos > -1:
            key_ = key[:dotpos]
            attrstr_ = key[dotpos:]
            try:
                baseval = locals_[key_]  # NOQA
                val = eval('baseval' + attrstr_)
            except Exception as ex:
                if strict:
                    raise
                val = ex
        else:
            raise AssertionError('%s = NameError' % (key))
    else:
        val = locals_[key]
    return val