##// END OF EJS Templates
- Add numpy support to %whos...
fperez -
Show More
@@ -72,27 +72,18 b" ip.set_hook('complete_command', apt_completers, re_key = '.*yum')"
72 72 pkg_cache = None
73 73
74 74 def module_completer(self,event):
75 """ Give completions after user has typed 'import' """
76
77 # only a local version for py 2.4, pkgutil has no walk_packages() there
78 if sys.version_info < (2,5):
79 for el in [f[:-3] for f in glob.glob("*.py")]:
80 yield el
81 return
82
83 global pkg_cache
84 import pkgutil,imp,time
85 #current =
86 if pkg_cache is None:
87 print "\n\n[Standby while scanning modules, this can take a while]\n\n"
88 pkg_cache = list(pkgutil.walk_packages())
89
90 already = set()
91 for ld, name, ispkg in pkg_cache:
92 if name.count('.') < event.symbol.count('.') + 1:
93 if name not in already:
94 already.add(name)
95 yield name + (ispkg and '.' or '')
75 """ Give completions after user has typed 'import'.
76
77 Note that only possible completions in the local directory are returned."""
78
79 # This works in all versions of python. While 2.5 has
80 # pkgutil.walk_packages(), that particular routine is fairly dangerous,
81 # since it imports *EVERYTHING* on sys.path. That is: a) very slow b) full
82 # of possibly problematic side effects. At some point we may implement
83 # something that searches sys.path in a saner/safer way, but for now we'll
84 # restrict ourselves to local completions only.
85 for el in [f[:-3] for f in glob.glob("*.py")]:
86 yield el
96 87 return
97 88
98 89 ip.set_hook('complete_command', module_completer, str_key = 'import')
@@ -1,7 +1,7 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Magic functions for InteractiveShell.
3 3
4 $Id: Magic.py 2190 2007-03-30 18:35:46Z fperez $"""
4 $Id: Magic.py 2200 2007-04-03 05:24:30Z fperez $"""
5 5
6 6 #*****************************************************************************
7 7 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
@@ -936,12 +936,6 b' Currently the magic system has the following functions:\\n"""'
936 936 return
937 937
938 938 # if we have variables, move on...
939
940 # stupid flushing problem: when prompts have no separators, stdout is
941 # getting lost. I'm starting to think this is a python bug. I'm having
942 # to force a flush with a print because even a sys.stdout.flush
943 # doesn't seem to do anything!
944
945 939 count = 0
946 940 for i in varlist:
947 941 print i+'\t',
@@ -949,9 +943,7 b' Currently the magic system has the following functions:\\n"""'
949 943 if count > 8:
950 944 count = 0
951 945 print
952 sys.stdout.flush() # FIXME. Why the hell isn't this flushing???
953
954 print # well, this does force a flush at the expense of an extra \n
946 print
955 947
956 948 def magic_whos(self, parameter_s=''):
957 949 """Like %who, but gives some extra information about each variable.
@@ -962,8 +954,8 b' Currently the magic system has the following functions:\\n"""'
962 954
963 955 - For {},[],(): their length.
964 956
965 - For Numeric arrays, a summary with shape, number of elements,
966 typecode and size in memory.
957 - For numpy and Numeric arrays, a summary with shape, number of
958 elements, typecode and size in memory.
967 959
968 960 - Everything else: a string representation, snipping their middle if
969 961 too long."""
@@ -978,16 +970,21 b' Currently the magic system has the following functions:\\n"""'
978 970 # for these types, show len() instead of data:
979 971 seq_types = [types.DictType,types.ListType,types.TupleType]
980 972
981 # for Numeric arrays, display summary info
973 # for numpy/Numeric arrays, display summary info
974 try:
975 import numpy
976 except ImportError:
977 ndarray_type = None
978 else:
979 ndarray_type = numpy.ndarray.__name__
982 980 try:
983 981 import Numeric
984 982 except ImportError:
985 983 array_type = None
986 984 else:
987 985 array_type = Numeric.ArrayType.__name__
988
986
989 987 # Find all variable names and types so we can figure out column sizes
990
991 988 def get_vars(i):
992 989 return self.shell.user_ns[i]
993 990
@@ -1004,7 +1001,8 b' Currently the magic system has the following functions:\\n"""'
1004 1001 tt = type_name(vv)
1005 1002
1006 1003 if tt=='instance':
1007 typelist.append( abbrevs.get(str(vv.__class__),str(vv.__class__)))
1004 typelist.append( abbrevs.get(str(vv.__class__),
1005 str(vv.__class__)))
1008 1006 else:
1009 1007 typelist.append(tt)
1010 1008
@@ -1030,14 +1028,23 b' Currently the magic system has the following functions:\\n"""'
1030 1028 print itpl(vformat),
1031 1029 if vtype in seq_types:
1032 1030 print len(var)
1033 elif vtype==array_type:
1031 elif vtype in [array_type,ndarray_type]:
1034 1032 vshape = str(var.shape).replace(',','').replace(' ','x')[1:-1]
1035 vsize = Numeric.size(var)
1036 vbytes = vsize*var.itemsize()
1033 if vtype==ndarray_type:
1034 # numpy
1035 vsize = var.size
1036 vbytes = vsize*var.itemsize
1037 vdtype = var.dtype
1038 else:
1039 # Numeric
1040 vsize = Numeric.size(var)
1041 vbytes = vsize*var.itemsize()
1042 vdtype = var.typecode()
1043
1037 1044 if vbytes < 100000:
1038 print aformat % (vshape,vsize,var.typecode(),vbytes)
1045 print aformat % (vshape,vsize,vdtype,vbytes)
1039 1046 else:
1040 print aformat % (vshape,vsize,var.typecode(),vbytes),
1047 print aformat % (vshape,vsize,vdtype,vbytes),
1041 1048 if vbytes < Mb:
1042 1049 print '(%s kb)' % (vbytes/kb,)
1043 1050 else:
@@ -1,5 +1,12 b''
1 1 2007-04-02 Fernando Perez <Fernando.Perez@colorado.edu>
2 2
3 * IPython/Extensions/ipy_stock_completers.py (module_completer):
4 remove usage of the dangerous pkgutil.walk_packages(). See
5 details in comments left in the code.
6
7 * IPython/Magic.py (magic_whos): add support for numpy arrays
8 similar to what we had for Numeric.
9
3 10 * IPython/completer.py (IPCompleter.complete): extend the
4 11 complete() call API to support completions by other mechanisms
5 12 than readline. Closes #109.
General Comments 0
You need to be logged in to leave comments. Login now