Show More
@@ -0,0 +1,98 b'' | |||
|
1 | """ Fun magic line editor for ipython | |
|
2 | ||
|
3 | Use this to easily edit lists of strings gradually without crafting long | |
|
4 | list comprehensions. | |
|
5 | ||
|
6 | 'l' is the magic variable name for every line (array element). Save the current | |
|
7 | result (or more exactly, retrieve the last ipython computation result into | |
|
8 | %led work area) by running '%led s'. Just run '%led' to show the current work | |
|
9 | area data. | |
|
10 | ||
|
11 | Example use: | |
|
12 | ||
|
13 | [ipython]|25> setups = !ls *setup*.py | |
|
14 | == | |
|
15 | ['eggsetup.py', 'setup.py', 'setup_bdist_egg.py'] | |
|
16 | [ipython]|26> setups | |
|
17 | <26> ['eggsetup.py', 'setup.py', 'setup_bdist_egg.py'] | |
|
18 | [ipython]|27> %led s | |
|
19 | Data set from last result (_) | |
|
20 | <27> ['eggsetup.py', 'setup.py', 'setup_bdist_egg.py'] | |
|
21 | [ipython]|28> %led upper | |
|
22 | cmd translated => l.upper() | |
|
23 | <28> ['EGGSETUP.PY', 'SETUP.PY', 'SETUP_BDIST_EGG.PY'] | |
|
24 | [ipython]|29> %led | |
|
25 | Magic line editor (for lists of strings) | |
|
26 | current data is: | |
|
27 | ['eggsetup.py', 'setup.py', 'setup_bdist_egg.py'] | |
|
28 | [ipython]|30> %led upper | |
|
29 | cmd translated => l.upper() | |
|
30 | <30> ['EGGSETUP.PY', 'SETUP.PY', 'SETUP_BDIST_EGG.PY'] | |
|
31 | [ipython]|31> %led s | |
|
32 | Data set from last result (_) | |
|
33 | <31> ['EGGSETUP.PY', 'SETUP.PY', 'SETUP_BDIST_EGG.PY'] | |
|
34 | [ipython]|32> %led "n:" + l | |
|
35 | <32> ['n:EGGSETUP.PY', 'n:SETUP.PY', 'n:SETUP_BDIST_EGG.PY'] | |
|
36 | [ipython]|33> %led s | |
|
37 | Data set from last result (_) | |
|
38 | <33> ['n:EGGSETUP.PY', 'n:SETUP.PY', 'n:SETUP_BDIST_EGG.PY'] | |
|
39 | [ipython]|34> %led l. | |
|
40 | l.__add__ l.__gt__ l.__reduce_ex__ l.endswith l.join l.rstrip | |
|
41 | l.__class__ l.__hash__ l.__repr__ l.expandtabs l.ljust l.split | |
|
42 | ||
|
43 | ... (completions for string variable shown ) ... | |
|
44 | ||
|
45 | """ | |
|
46 | import IPython.ipapi | |
|
47 | import pprint | |
|
48 | ip = IPython.ipapi.get() | |
|
49 | ||
|
50 | curdata = [] | |
|
51 | ||
|
52 | def line_edit_f(self, cmd ): | |
|
53 | global curdata | |
|
54 | ||
|
55 | if not cmd: | |
|
56 | ||
|
57 | print "Magic line editor (for lists of strings)" | |
|
58 | if curdata: | |
|
59 | print "current data is:" | |
|
60 | pprint.pprint(curdata) | |
|
61 | else: | |
|
62 | print "No current data, you should set it by running '%led s'" | |
|
63 | print "When you have your data in _ (result of last computation)." | |
|
64 | return | |
|
65 | ||
|
66 | if cmd == 's': | |
|
67 | curdata = ip.ev('_') | |
|
68 | print "Data set from last result (_)" | |
|
69 | newlines = curdata | |
|
70 | ||
|
71 | else: | |
|
72 | # simple method call, e.g. upper | |
|
73 | if cmd.isalpha(): | |
|
74 | cmd = 'l.' + cmd + '()' | |
|
75 | print "cmd translated =>",cmd | |
|
76 | ||
|
77 | newlines = [] | |
|
78 | for l in curdata: | |
|
79 | try: | |
|
80 | l2 = eval(cmd) | |
|
81 | except Exception,e: | |
|
82 | print "Dropping exception",e,"on line:",l | |
|
83 | continue | |
|
84 | newlines.append(l2) | |
|
85 | ||
|
86 | ||
|
87 | return newlines | |
|
88 | ||
|
89 | def line_edit_complete_f(self,event): | |
|
90 | """ Show all string methods in completions """ | |
|
91 | if event.symbol.startswith('l.'): | |
|
92 | return ['l.' + func for func in dir('')] | |
|
93 | ||
|
94 | return dir('') + ['l.' + func for func in dir('')] | |
|
95 | ||
|
96 | ip.set_hook('complete_command', line_edit_complete_f , str_key = '%led') | |
|
97 | ||
|
98 | ip.expose_magic('led', line_edit_f) No newline at end of file |
@@ -5,6 +5,10 b'' | |||
|
5 | 5 | trivial completer for 'import' (stdlib modules only). Rename |
|
6 | 6 | ipy_linux_package_managers.py to ipy_stock_completers.py. |
|
7 | 7 | SVN completer. |
|
8 | ||
|
9 | * Extensions/ledit.py: %magic line editor for easily and | |
|
10 | incrementally manipulating lists of strings. The magic command | |
|
11 | name is %led. | |
|
8 | 12 | |
|
9 | 13 | 2006-10-30 Ville Vainio <vivainio@gmail.com> |
|
10 | 14 |
General Comments 0
You need to be logged in to leave comments.
Login now