From 67f6e7bede23bed377a3192a2dd28bafd7266e86 Mon Sep 17 00:00:00 2001 From: Wirawan Purwanto Date: Wed, 19 Aug 2015 17:44:38 -0400 Subject: [PATCH] * Module wpylib.py.introspection: Tools for aiding introspection in python. * Added function: name_rlookup(). Imported from Cr2 analysis script, original name was: search_var_name. No edit has been made yet. --- py/introspection.py | 57 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 py/introspection.py diff --git a/py/introspection.py b/py/introspection.py new file mode 100644 index 0000000..810bd7f --- /dev/null +++ b/py/introspection.py @@ -0,0 +1,57 @@ +# -*- python -*- +# +# wpylib.py.introspection +# Created: 20150819 +# Wirawan Purwanto +# +# Advanced introspection tools for python +# + +def name_rlookup(val, namespace, prefix=None, rx_match=None, lookup="dict"): + """Reverse lookup of variable/object name. + + Yields the list of object names in the namespace whose respective values are + equal to `val`. + + This is useful for example to reverse-lookup an object's name, but only in + restricted contexts (e.g. when the value to look for is unique). + """ + # Imported 20150819 + # Original subroutine name: search_var_name (from Cr2_analysis_cbs.py). + if lookup == "dict": + names = namespace.keys() + else: + # attribute lookup + names = dir(namespace) + if prefix != None: + names_filt = [ n for n in names if n.startswith(prefix) ] + elif rx_match != None: + # TODO later: for my regexp object? + if True: + if isinstance(rx_match, basestring): + names_filt = [ n for n in names if re.search(rx_match, n) ] + else: + # assume a re.RegexObject-like object: + names_filt = [ n for n in names if rx_match.search(n) ] + else: + names_filt = names + names_match = [] + type_val = type(val) + if lookup == "dict": + for n in names_filt: + try: + v = namespace[n] + except: + continue + if type(v) == type_val and v == val: + names_match.append(n) + else: + for n in names_filt: + try: + v = getattr(namespace, n) + except: + continue + if type(v) == type_val and v == val: + names_match.append(n) + return names_match +