'Set environment variables.  Assumptions are that either "val" is a
name in the user namespace, or val is something that evaluates to a
string.
Usage:\
%set_env var val: set value for var
%set_env var=val: set value for var
%set_env var=$val: set value for var, using python expansion if possible'

@line_magic
def set_env(self, parameter_s):

    split = ('=' if ('=' in parameter_s) else '    ')
    bits = parameter_s.split(split, 1)
    if ((not parameter_s.strip()) or (len(bits) < 2)):
        raise UsageError("usage    is    'set_env    var=val'")
    var = bits[0].strip()
    val = bits[1].strip()
    if re.match('.*\\s.*', var):
        err = "refusing    to    set    env    var    with    whitespace:    '{0}'"
        err = err.format(val)
        raise UsageError(err)
    os.environ[var] = val
    print 'env:    {0}={1}'.format(var, val)
