##// END OF EJS Templates
-Added a unit testing framework...
vivainio -
Show More

The requested changes are too big and content was truncated. Show full diff

@@ -0,0 +1,31 b''
1 """ Run ipython unit tests
2
3 This should be launched from inside ipython by "%run runtests.py"
4 or through ipython command line "ipython runtests.py".
5
6 """
7
8 from path import path
9 import pprint,os
10 import IPython.ipapi
11 ip = IPython.ipapi.get()
12
13 def main():
14 all = path('.').files('test_*py')
15 results = {}
16 res_exc = [None]
17 def exchook(self,*e):
18 res_exc[0] = [e]
19 ip.IP.set_custom_exc((Exception,), exchook)
20 startdir = os.getcwd()
21 for test in all:
22 print test
23 res_exc[0] = 'ok'
24 os.chdir(startdir)
25 ip.runlines(test.text())
26 results[str(test)] = res_exc[0]
27
28 os.chdir(startdir)
29 pprint.pprint(results)
30
31 main() No newline at end of file
@@ -0,0 +1,6 b''
1 import os
2 cd /
3 assert os.getcwd() == '/'
4 %cd /tmp
5
6 assert os.getcwd() == '/tmp'
@@ -0,0 +1,3 b''
1 # this will fail w/ assertionerror
2 cd /
3 assert os.getcwd() == '/does/not/exist'
@@ -1,184 +1,197 b''
1 ''' IPython customization API
1 ''' IPython customization API
2
2
3 Your one-stop module for configuring & extending ipython
3 Your one-stop module for configuring & extending ipython
4
4
5 The API will probably break when ipython 1.0 is released, but so
5 The API will probably break when ipython 1.0 is released, but so
6 will the other configuration method (rc files).
6 will the other configuration method (rc files).
7
7
8 All names prefixed by underscores are for internal use, not part
8 All names prefixed by underscores are for internal use, not part
9 of the public api.
9 of the public api.
10
10
11 Below is an example that you can just put to a module and import from ipython.
11 Below is an example that you can just put to a module and import from ipython.
12
12
13 A good practice is to install the config script below as e.g.
13 A good practice is to install the config script below as e.g.
14
14
15 ~/.ipython/my_private_conf.py
15 ~/.ipython/my_private_conf.py
16
16
17 And do
17 And do
18
18
19 import_mod my_private_conf
19 import_mod my_private_conf
20
20
21 in ~/.ipython/ipythonrc
21 in ~/.ipython/ipythonrc
22
22
23 That way the module is imported at startup and you can have all your
23 That way the module is imported at startup and you can have all your
24 personal configuration (as opposed to boilerplate ipythonrc-PROFILENAME
24 personal configuration (as opposed to boilerplate ipythonrc-PROFILENAME
25 stuff) in there.
25 stuff) in there.
26
26
27 -----------------------------------------------
27 -----------------------------------------------
28 import IPython.ipapi as ip
28 import IPython.ipapi as ip
29
29
30 def ankka_f(self, arg):
30 def ankka_f(self, arg):
31 print "Ankka",self,"says uppercase:",arg.upper()
31 print "Ankka",self,"says uppercase:",arg.upper()
32
32
33 ip.expose_magic("ankka",ankka_f)
33 ip.expose_magic("ankka",ankka_f)
34
34
35 ip.magic('alias sayhi echo "Testing, hi ok"')
35 ip.magic('alias sayhi echo "Testing, hi ok"')
36 ip.magic('alias helloworld echo "Hello world"')
36 ip.magic('alias helloworld echo "Hello world"')
37 ip.system('pwd')
37 ip.system('pwd')
38
38
39 ip.ex('import re')
39 ip.ex('import re')
40 ip.ex("""
40 ip.ex("""
41 def funcci(a,b):
41 def funcci(a,b):
42 print a+b
42 print a+b
43 print funcci(3,4)
43 print funcci(3,4)
44 """)
44 """)
45 ip.ex("funcci(348,9)")
45 ip.ex("funcci(348,9)")
46
46
47 def jed_editor(self,filename, linenum=None):
47 def jed_editor(self,filename, linenum=None):
48 print "Calling my own editor, jed ... via hook!"
48 print "Calling my own editor, jed ... via hook!"
49 import os
49 import os
50 if linenum is None: linenum = 0
50 if linenum is None: linenum = 0
51 os.system('jed +%d %s' % (linenum, filename))
51 os.system('jed +%d %s' % (linenum, filename))
52 print "exiting jed"
52 print "exiting jed"
53
53
54 ip.set_hook('editor',jed_editor)
54 ip.set_hook('editor',jed_editor)
55
55
56 o = ip.options()
56 o = ip.options()
57 o.autocall = 2 # FULL autocall mode
57 o.autocall = 2 # FULL autocall mode
58
58
59 print "done!"
59 print "done!"
60
60
61 '''
61 '''
62
62
63
63
64 class TryNext(Exception):
64 class TryNext(Exception):
65 """ Try next hook exception.
65 """ Try next hook exception.
66
66
67 Raise this in your hook function to indicate that the next
67 Raise this in your hook function to indicate that the next
68 hook handler should be used to handle the operation.
68 hook handler should be used to handle the operation.
69 """
69 """
70
70
71
71
72 # contains the most recently instantiated IPApi
72 # contains the most recently instantiated IPApi
73 _recent = None
73 _recent = None
74
74
75 def get():
75 def get():
76 """ Get an IPApi object, or None if not running under ipython
76 """ Get an IPApi object, or None if not running under ipython
77
77
78 Running this should be the first thing you do when writing
78 Running this should be the first thing you do when writing
79 extensions that can be imported as normal modules. You can then
79 extensions that can be imported as normal modules. You can then
80 direct all the configuration operations against the returned
80 direct all the configuration operations against the returned
81 object.
81 object.
82
82
83 """
83 """
84
84
85 return _recent
85 return _recent
86
86
87
87
88
88
89 class IPApi:
89 class IPApi:
90 """ The actual API class for configuring IPython
90 """ The actual API class for configuring IPython
91
91
92 You should do all of the IPython configuration by getting
92 You should do all of the IPython configuration by getting
93 an IPApi object with IPython.ipapi.get() and using the provided
93 an IPApi object with IPython.ipapi.get() and using the provided
94 methods.
94 methods.
95
95
96 """
96 """
97 def __init__(self,ip):
97 def __init__(self,ip):
98
98
99 self.magic = ip.ipmagic
99 self.magic = ip.ipmagic
100
100
101 self.system = ip.ipsystem
101 self.system = ip.ipsystem
102
102
103 self.set_hook = ip.set_hook
103 self.set_hook = ip.set_hook
104
104
105 self.set_custom_exc = ip.set_custom_exc
106
105 self.IP = ip
107 self.IP = ip
106 global _recent
108 global _recent
107 _recent = self
109 _recent = self
108
110
109
111
110
112
111 def options(self):
113 def options(self):
112 """ All configurable variables """
114 """ All configurable variables """
113 return self.IP.rc
115 return self.IP.rc
114
116
115 def user_ns(self):
117 def user_ns(self):
116 return self.IP.user_ns
118 return self.IP.user_ns
117
119
118 def expose_magic(self,magicname, func):
120 def expose_magic(self,magicname, func):
119 ''' Expose own function as magic function for ipython
121 ''' Expose own function as magic function for ipython
120
122
121 def foo_impl(self,parameter_s=''):
123 def foo_impl(self,parameter_s=''):
122 """My very own magic!. (Use docstrings, IPython reads them)."""
124 """My very own magic!. (Use docstrings, IPython reads them)."""
123 print 'Magic function. Passed parameter is between < >: <'+parameter_s+'>'
125 print 'Magic function. Passed parameter is between < >: <'+parameter_s+'>'
124 print 'The self object is:',self
126 print 'The self object is:',self
125
127
126 ipapi.expose_magic("foo",foo_impl)
128 ipapi.expose_magic("foo",foo_impl)
127 '''
129 '''
128
130
129 import new
131 import new
130 im = new.instancemethod(func,self.IP, self.IP.__class__)
132 im = new.instancemethod(func,self.IP, self.IP.__class__)
131 setattr(self.IP, "magic_" + magicname, im)
133 setattr(self.IP, "magic_" + magicname, im)
132
134
133
135
134 def ex(self,cmd):
136 def ex(self,cmd):
135 """ Execute a normal python statement in user namespace """
137 """ Execute a normal python statement in user namespace """
136 exec cmd in self.user_ns()
138 exec cmd in self.user_ns()
137
139
138 def ev(self,expr):
140 def ev(self,expr):
139 """ Evaluate python expression expr in user namespace
141 """ Evaluate python expression expr in user namespace
140
142
141 Returns the result of evaluation"""
143 Returns the result of evaluation"""
142 return eval(expr,self.user_ns())
144 return eval(expr,self.user_ns())
143
145
144 def meta(self):
146 def meta(self):
145 """ Get a session-specific data store
147 """ Get a session-specific data store
146
148
147 Object returned by this method can be used to store
149 Object returned by this method can be used to store
148 data that should persist through the ipython session.
150 data that should persist through the ipython session.
149 """
151 """
150 return self.IP.meta
152 return self.IP.meta
151
153
152 def getdb(self):
154 def getdb(self):
153 """ Return a handle to persistent dict-like database
155 """ Return a handle to persistent dict-like database
154
156
155 Return a PickleShareDB object.
157 Return a PickleShareDB object.
156 """
158 """
157 return self.IP.db
159 return self.IP.db
160 def runlines(self,lines):
161 """ Run the specified lines in interpreter, honoring ipython directives.
162
163 This allows %magic and !shell escape notations.
158
164
165 Takes either all lines in one string or list of lines.
166 """
167 if isinstance(lines,basestring):
168 self.IP.runlines(lines)
169 else:
170 self.IP.runlines('\n'.join(lines))
171
159
172
160 def launch_new_instance(user_ns = None):
173 def launch_new_instance(user_ns = None):
161 """ Create and start a new ipython instance.
174 """ Create and start a new ipython instance.
162
175
163 This can be called even without having an already initialized
176 This can be called even without having an already initialized
164 ipython session running.
177 ipython session running.
165
178
166 This is also used as the egg entry point for the 'ipython' script.
179 This is also used as the egg entry point for the 'ipython' script.
167
180
168 """
181 """
169 ses = create_session(user_ns)
182 ses = create_session(user_ns)
170 ses.mainloop()
183 ses.mainloop()
171
184
172
185
173 def create_session(user_ns = None):
186 def create_session(user_ns = None):
174 """ Creates, but does not launch an IPython session.
187 """ Creates, but does not launch an IPython session.
175
188
176 Later on you can call obj.mainloop() on the returned object.
189 Later on you can call obj.mainloop() on the returned object.
177
190
178 This should *not* be run when a session exists already.
191 This should *not* be run when a session exists already.
179
192
180 """
193 """
181 if user_ns is not None:
194 if user_ns is not None:
182 user_ns["__name__"] = user_ns.get("__name__",'ipy_session')
195 user_ns["__name__"] = user_ns.get("__name__",'ipy_session')
183 import IPython
196 import IPython
184 return IPython.Shell.start(user_ns = user_ns) No newline at end of file
197 return IPython.Shell.start(user_ns = user_ns)
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
General Comments 0
You need to be logged in to leave comments. Login now