[Yt-svn] yt: Adding a tab-completer for AMRData dict-lookups.
hg at spacepope.org
hg at spacepope.org
Mon Jan 24 18:10:56 PST 2011
hg Repository: yt
details: yt/rev/8df12c337844
changeset: 3689:8df12c337844
user: Matthew Turk <matthewturk at gmail.com>
date:
Mon Jan 24 21:10:49 2011 -0500
description:
Adding a tab-completer for AMRData dict-lookups.
diffstat:
scripts/iyt | 48 +++++++++++++++++++++++++++++++++++++++++++++++-
1 files changed, 47 insertions(+), 1 deletions(-)
diffs (62 lines):
diff -r e77d851f122f -r 8df12c337844 scripts/iyt
--- a/scripts/iyt Mon Jan 24 13:42:37 2011 -0500
+++ b/scripts/iyt Mon Jan 24 21:10:49 2011 -0500
@@ -1,6 +1,7 @@
#!python
-import os
+import os, re
from yt.mods import *
+from yt.data_objects.data_containers import AMRData
namespace = locals().copy()
doc = """\
@@ -265,4 +266,49 @@
#main()
+
+# Now we add some tab completers, in the vein of:
+# http://pymel.googlecode.com/svn/trunk/tools/ipymel.py
+# We'll start with some fields.
+
+def yt_fieldname_completer(self, event):
+ """Match dictionary completions"""
+ #print "python_matches", event.symbol
+ #text = event.symbol # Not sure why this no longer works
+ text = event.line
+ #print repr(text)
+ # Another option, seems to work great. Catches things like ''.<tab>
+ #print repr(text), dir(text)
+ #m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text)
+ m = re.match(r"(\S+(\.\w+)*)\[[\'\\\"](\w*)$", text)
+
+ if not m:
+ raise IPython.ipapi.TryNext
+
+ expr, attr = m.group(1, 3)
+ #print "COMPLETING ON ", expr, attr
+ #print type(self.Completer), dir(self.Completer)
+ #print self.Completer.namespace
+ #print self.Completer.global_namespace
+ try:
+ obj = eval(expr, self.Completer.namespace)
+ except:
+ try:
+ obj = eval(expr, self.Completer.global_namespace)
+ except:
+ raise IPython.ipapi.TryNext
+
+ if isinstance(obj, (AMRData, ) ):
+ #print "COMPLETING ON THIS THING"
+ all_fields = [f for f in sorted(
+ obj.pf.h.field_list + obj.pf.h.derived_field_list)]
+ #matches = self.Completer.python_matches(text)
+ #print "RETURNING ", all_fields
+ return all_fields
+
+
+ raise IPython.ipapi.TryNext
+
+ip.set_hook('complete_command', yt_fieldname_completer , re_key = ".*" )
+
ip_shell.mainloop(sys_exit=1,banner=doc)
More information about the yt-svn
mailing list