1398
1399
or escaping, keeping the original string untouched. The name come from Lisp's unquote.
1399
1400
One of the uses for this is to preserve or insert bare variable names within dicts during eval()
1400
1401
of a dict's repr(). Use with care.
1403
Some examples (notice that there are never quotes surrounding
1404
the ``active_id`` name:
1402
1406
>>> unquote('active_id')
1404
>>> repr(unquote('active_id'))
1406
1408
>>> d = {'test': unquote('active_id')}
1408
1410
{'test': active_id}
1410
"{'test': active_id}"
1412
1414
def __repr__(self):
1417
class UnquoteEvalContext(defaultdict):
1418
"""Defaultdict-based evaluation context that returns
1419
an ``unquote`` string for any missing name used during
1421
Mostly useful for evaluating OpenERP domains/contexts that
1422
may refer to names that are unknown at the time of eval,
1423
so that when the context/domain is converted back to a string,
1424
the original names are preserved.
1426
**Warning**: using an ``UnquoteEvalContext`` as context for ``eval()`` or
1427
``safe_eval()`` will shadow the builtins, which may cause other
1428
failures, depending on what is evaluated.
1430
Example (notice that ``section_id`` is preserved in the final
1433
>>> context_str = "{'default_user_id': uid, 'default_section_id': section_id}"
1434
>>> eval(context_str, UnquoteEvalContext(uid=1))
1435
{'default_user_id': 1, 'default_section_id': section_id}
1438
def __init__(self, *args, **kwargs):
1439
super(UnquoteEvalContext, self).__init__(None, *args, **kwargs)
1441
def __missing__(self, key):
1415
1444
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: