-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython.html
58 lines (54 loc) · 2.81 KB
/
python.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<link rel="stylesheet" href="styles.css" type="text/css" media="screen" charset="utf-8">
<link href="prettify/prettify.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="prettify/prettify.js"></script>
<!--link href="log.php"/-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script>
<script>
$().ready(function() {
$("#basicinfo tr:even").addClass("colored");
prettyPrint();
});
</script>
<title>Python Tips and Tricks</title>
</head>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-12221940-1");
pageTracker._trackPageview();
} catch(err) {}</script>
<body>
<h1>Python Tips and Tricks</h1>
<h2>What's in my <tt>.pythonstartup</tt>?</h2>
Not much. The current contents are:
<pre class="prettyprint">
from pprint import pprint as pp
from see import see
import numpy as np</pre>
<p>If you haven't already, make sure to set your <tt>PYTHONSTARTUP</tt> environment variable to point to <tt>~/.pythonstartup</tt>.</p>
However, I use <a href="http://ipython.scipy.org/moin/">IPython</a> as my python shell. Prior to IPython 0.11, you can get IPython to use your .pythonstartup by adding the following two lines to your <tt>~/.ipython/ipythonrc</tt>:
<pre class="prettyprint">
execute import os
execute execfile(os.environ['PYTHONSTARTUP'])</pre>
You may have noticed this nonstandard <tt>see</tt> module. <a href="http://inky.github.com/see/"><tt>see</tt></a> is an awesome alternative to python's built-in <tt>dir</tt> function, which I highly recommend.
<h2>I hate making all my GUI callback functions launch new threads!</h2>
Python decorators make this super easy. The following decorator will make any function run in its own thread.
<pre class="prettyprint">
from threading import Thread
from functools import wraps
def thread(func):
@wraps(func)
def wrap(*args, **kwargs):
t = Thread(target=func, args=args, kwargs=kwargs)
t.start()
return t
return wrap</pre>
Careful though. If any callbacks access global or class variables, you may start running into synchronization issues. In certain situations, python's <a href="http://docs.python.org/library/queue.html"/>synchronized queue class</a> can help. Otherwise, <a href="http://docs.python.org/library/threading.html?highlight=rlock#threading.RLock">RLocks</a> can be helpful.
</body>
</html>