Browse code
added python customizations
Showing 3 changed files
1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,42 @@ |
1 |
+__license__ = '''\ |
|
2 |
+# Copyright (c) 2011 Edward Langley |
|
3 |
+# All rights reserved. |
|
4 |
+# |
|
5 |
+# Redistribution and use in source and binary forms, with or without |
|
6 |
+# modification, are permitted provided that the following conditions |
|
7 |
+# are met: |
|
8 |
+# |
|
9 |
+# Redistributions of source code must retain the above copyright notice, |
|
10 |
+# this list of conditions and the following disclaimer. |
|
11 |
+# |
|
12 |
+# Redistributions in binary form must reproduce the above copyright |
|
13 |
+# notice, this list of conditions and the following disclaimer in the |
|
14 |
+# documentation and/or other materials provided with the distribution. |
|
15 |
+# |
|
16 |
+# Neither the name of the project's author nor the names of its |
|
17 |
+# contributors may be used to endorse or promote products derived from |
|
18 |
+# this software without specific prior written permission. |
|
19 |
+# |
|
20 |
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
21 |
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
22 |
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
|
23 |
+# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
24 |
+# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
25 |
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
|
26 |
+# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
27 |
+# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
|
28 |
+# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
29 |
+# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
30 |
+# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.''' |
|
31 |
+ |
|
32 |
+import sys |
|
33 |
+sys.path.append('/Users/edwlan/Library/Python/2.7/site-packages') |
|
34 |
+ |
|
35 |
+print 'hello' |
|
36 |
+if sys.version_info[0] == 3: |
|
37 |
+ try: |
|
38 |
+ from sitecustomize3 import * |
|
39 |
+ except ImportError: |
|
40 |
+ pass |
|
41 |
+else: |
|
42 |
+ from sitecustomize2 import * |
0 | 43 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,214 @@ |
1 |
+__license__ = '''\ |
|
2 |
+# Copyright (c) 2011 Edward Langley |
|
3 |
+# All rights reserved. |
|
4 |
+# |
|
5 |
+# Redistribution and use in source and binary forms, with or without |
|
6 |
+# modification, are permitted provided that the following conditions |
|
7 |
+# are met: |
|
8 |
+# |
|
9 |
+# Redistributions of source code must retain the above copyright notice, |
|
10 |
+# this list of conditions and the following disclaimer. |
|
11 |
+# |
|
12 |
+# Redistributions in binary form must reproduce the above copyright |
|
13 |
+# notice, this list of conditions and the following disclaimer in the |
|
14 |
+# documentation and/or other materials provided with the distribution. |
|
15 |
+# |
|
16 |
+# Neither the name of the project's author nor the names of its |
|
17 |
+# contributors may be used to endorse or promote products derived from |
|
18 |
+# this software without specific prior written permission. |
|
19 |
+# |
|
20 |
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
21 |
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
22 |
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
|
23 |
+# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
24 |
+# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
25 |
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
|
26 |
+# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
27 |
+# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
|
28 |
+# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
29 |
+# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
30 |
+# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.''' |
|
31 |
+ |
|
32 |
+import re |
|
33 |
+import functools |
|
34 |
+import os.path |
|
35 |
+import inspect |
|
36 |
+def inst(x): return x() |
|
37 |
+@inst |
|
38 |
+class __Null(object): pass |
|
39 |
+def getTerminalSize(): |
|
40 |
+ def ioctl_GWINSZ(fd): |
|
41 |
+ try: |
|
42 |
+ import fcntl, termios, struct, os |
|
43 |
+ cr = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234')) |
|
44 |
+ except: |
|
45 |
+ return None |
|
46 |
+ return cr |
|
47 |
+ cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2) |
|
48 |
+ if not cr: |
|
49 |
+ try: |
|
50 |
+ fd = os.open(os.ctermid(), os.O_RDONLY) |
|
51 |
+ cr = ioctl_GWINSZ(fd) |
|
52 |
+ os.close(fd) |
|
53 |
+ except: |
|
54 |
+ pass |
|
55 |
+ if not cr: |
|
56 |
+ try: |
|
57 |
+ cr = (env['LINES'], env['COLUMNS']) |
|
58 |
+ except: |
|
59 |
+ cr = (25, 80) |
|
60 |
+ return int(cr[1]), int(cr[0]) |
|
61 |
+ |
|
62 |
+ |
|
63 |
+def cmp(a,b): return (a > b) - (a < b) |
|
64 |
+def sort_(nms): |
|
65 |
+ return sorted(nms, cmp = lambda x,y: cmp(x.lower().lstrip('_').rpartition('__')[2], y.lower().lstrip('_').rpartition('__')[2])) |
|
66 |
+ |
|
67 |
+def sort_decorator(func): |
|
68 |
+ @functools.wraps(func) |
|
69 |
+ def _inner(*a, **kw): |
|
70 |
+ result = func(*a, **kw) |
|
71 |
+ return sort_(result) |
|
72 |
+ return _inner |
|
73 |
+ |
|
74 |
+def pprint_decorator(func): |
|
75 |
+ @functools.wraps(func) |
|
76 |
+ def _inner(*args, **kwargs): |
|
77 |
+ result = func(*args, **kwargs) |
|
78 |
+ return pprint(result) |
|
79 |
+ return _inner |
|
80 |
+ |
|
81 |
+def dir_decorator(func): |
|
82 |
+ @functools.wraps(func) |
|
83 |
+ def _inner(nm=__Null, match=__Null): |
|
84 |
+ print get_interpframe() |
|
85 |
+ result = None |
|
86 |
+ if nm is __Null: result = get_interpframe().f_globals.keys() |
|
87 |
+ else: |
|
88 |
+ result = func(nm) |
|
89 |
+ if match is not __Null: |
|
90 |
+ x = [x for x in result if x.startswith(match)] |
|
91 |
+ if x: result = x |
|
92 |
+ else: |
|
93 |
+ x = [x for x in result if x.endswith(match)] |
|
94 |
+ if x: result = x |
|
95 |
+ else: |
|
96 |
+ omatch = match |
|
97 |
+ if not hasattr(match, 'match'): match = re.compile(match) |
|
98 |
+ x = [x for x in result if match.match(x)] |
|
99 |
+ if x: result = x |
|
100 |
+ else: |
|
101 |
+ raise ValueError('No symbols match %s' % omatch) |
|
102 |
+ |
|
103 |
+ if x: result = x |
|
104 |
+ return result |
|
105 |
+ return _inner |
|
106 |
+ |
|
107 |
+def _pprint(nms): |
|
108 |
+ length = max(len(s) for s in nms) |
|
109 |
+ count = 0 |
|
110 |
+ cc = 1 |
|
111 |
+ while (cc*length) < ((getTerminalSize()[0])-2*cc): |
|
112 |
+ cc+=1 |
|
113 |
+ cc -= 1 |
|
114 |
+ if cc <= 0: cc = 1 |
|
115 |
+ for v, c in enumerate(nms): |
|
116 |
+ print c.strip().ljust(length), |
|
117 |
+ if v % cc == cc-1: print |
|
118 |
+pprint = _pprint |
|
119 |
+ |
|
120 |
+try: |
|
121 |
+ __builtins__['pdir'] = pprint_decorator(sort_decorator(dir_decorator(dir))) |
|
122 |
+ __builtins__['fwprint'] = pprint |
|
123 |
+except: |
|
124 |
+ __builtins__.pdir = pprint_decorator(sort_decorator(dir_decorator(dir))) |
|
125 |
+ __builtins__.fwprint = pprint |
|
126 |
+ |
|
127 |
+try: |
|
128 |
+ import rlcompleter |
|
129 |
+ import readline |
|
130 |
+except ImportError: |
|
131 |
+ pass |
|
132 |
+ |
|
133 |
+#readline.parse_and_bind ("bind ^I rl_complete") |
|
134 |
+ |
|
135 |
+import tempfile, subprocess |
|
136 |
+def get_interpframe(): return inspect.getouterframes(inspect.currentframe())[-1][0] |
|
137 |
+ |
|
138 |
+def e(name=None): |
|
139 |
+ def load_file(): |
|
140 |
+ result = tempfile.NamedTemporaryFile(delete=False, dir='/Users/edwlan/sandbox/unsorted', prefix='pythonsnippets_', suffix='.py') |
|
141 |
+ print >>result, __license__, '\n\n' |
|
142 |
+ result.flush() |
|
143 |
+ return result |
|
144 |
+ |
|
145 |
+ if name is not None: |
|
146 |
+ def load_file(): |
|
147 |
+ existed = True |
|
148 |
+ |
|
149 |
+ nm = r'/Users/edwlan/sandbox/%s.py' % name |
|
150 |
+ mode = 'r+' |
|
151 |
+ if not os.path.exists(nm): |
|
152 |
+ mode = 'w' |
|
153 |
+ existed = False |
|
154 |
+ |
|
155 |
+ result = open(nm, mode) |
|
156 |
+ if not existed: |
|
157 |
+ print >>result, __license__, '\n\n' |
|
158 |
+ result.flush() |
|
159 |
+ return result |
|
160 |
+ |
|
161 |
+ f = load_file() |
|
162 |
+ print ('editing %s, press Enter to continue...' % f.name) |
|
163 |
+ |
|
164 |
+ try: |
|
165 |
+ try: subprocess.call(['vim', '+31', r'+start', f.name]) |
|
166 |
+ except Exception, e: print e |
|
167 |
+ f.close() |
|
168 |
+ f = open(f.name) |
|
169 |
+ a = f.read() |
|
170 |
+ exec a in get_interpframe().f_globals |
|
171 |
+ finally: |
|
172 |
+ f.close() |
|
173 |
+ |
|
174 |
+def ls(name): |
|
175 |
+ load_file = lambda: open('/Users/edwlan/sandbox/unsorted/pythonsnippets_%s.py' % name) |
|
176 |
+ try: |
|
177 |
+ f = load_file() |
|
178 |
+ except IOError: |
|
179 |
+ load_file = lambda: open('/Users/edwlan/sandbox/%s.py' % name) |
|
180 |
+ f = load_file() |
|
181 |
+ |
|
182 |
+ raw_input('loading %s, press Enter to continue...' % f.name) |
|
183 |
+ try: |
|
184 |
+ exec f.read() in get_interpframe().f_globals |
|
185 |
+ finally: |
|
186 |
+ f.close() |
|
187 |
+ |
|
188 |
+ |
|
189 |
+try: |
|
190 |
+ __builtins__['e'] = e |
|
191 |
+ __builtins__['ls'] = ls |
|
192 |
+except: |
|
193 |
+ __builtins__.e = e |
|
194 |
+ __builtins__.ls = ls |
|
195 |
+ |
|
196 |
+#histfile = os.path.join(os.environ["HOME"], ".pyhist2.7") |
|
197 |
+#try: readline.read_history_file(histfile) |
|
198 |
+#except IOError: pass |
|
199 |
+ |
|
200 |
+try: |
|
201 |
+ import pip |
|
202 |
+ try: |
|
203 |
+ def ip(name): |
|
204 |
+ try: pip.main(['install', name]) |
|
205 |
+ except SystemExit: pass |
|
206 |
+ __builtins__['ip'] = ip |
|
207 |
+ except: |
|
208 |
+ __builtins__.ip = ip |
|
209 |
+except ImportError: |
|
210 |
+ pass |
|
211 |
+ |
|
212 |
+ |
|
213 |
+#import atexit |
|
214 |
+#atexit.register(readline.write_history_file, histfile) |